17 \h'-\w'\\$1\ 'u'\\$1\ \c
22 .TH darray 3 "21 October 1999" "Straylight/Edgeware" "mLib utilities library"
24 darray \- dense, dynamically resizing arrays
45 .\" @DA_UNSAFE_UNSLIDE
59 .B "#include <mLib/darray.h>"
62 .B "\h'4n'size_t sz, len, off;"
63 .B "\h'4n'unsigned push, unshift;"
67 .B "#define DA_INIT ..."
69 .B "#define DAEXC_UFLOW EXC_ALLOCN(EXC_MLIB, ...)"
70 .B "#define DAEXC_OFLOW EXC_ALLOCN(EXC_MLIB, ...)"
72 .BI DA_DECL( type_v ", " type );
73 .BI "void DA_CREATE(" type_v " *" a );
74 .BI "void DA_DESTROY(" type_v " *" a );
76 .BI "void DA_ENSURE(" type_v " *" a ", size_t " n );
77 .BI "void DA_SHUNT(" type_v " *" a ", size_t " n );
78 .BI "void DA_TIDY(" type_v " *" a );
79 .BI "void DA_RESET(" type_v " *" a );
81 .IB type " *DA(" type_v " *" a );
82 .BI "size_t DA_LEN(" type_v " *" a );
83 .BI "size_t DA_SPARE(" type_v " *" a );
84 .BI "size_t DA_OFFSET(" type_v " *" a );
85 .BI "void DA_INCLUDE(" type_v " *" a ", size_t " i );
87 .BI "void DA_EXTEND(" type_v " *" a ", long " n );
88 .BI "void DA_SHRINK(" type_v " *" a ", long " n );
89 .BI "void DA_SLIDE(" type_v " *" a ", long " n );
90 .BI "void DA_UNSLIDE(" type_v " *" a ", long " n );
92 .BI "void DA_UNSAFE_EXTEND(" type_v " *" a ", long " n );
93 .BI "void DA_UNSAFE_SHRINK(" type_v " *" a ", long " n );
94 .BI "void DA_UNSAFE_SLIDE(" type_v " *" a ", long " n );
95 .BI "void DA_UNSAFE_UNSLIDE(" type_v " *" a ", long " n );
97 .IB type " DA_FIRST(" type_v " *" a );
98 .IB type " DA_LAST(" type_v " *" a );
99 .BI "void DA_PUSH(" type_v " *" a ", " type " " x );
100 .IB type " DA_POP(" type_v " *" a );
101 .BI "void DA_UNSHIFT(" type_v " *" a ", " type " " x );
102 .IB type " DA_SHIFT(" type_v " *" a );
104 .BI "void *da_ensure(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
105 .BI "void *da_shunt(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
106 .BI "void *da_tidy(da_base *" b ", void *" v ", size_t " sz );
111 header file declares a collection of types, macros and functions which
112 implement dynamically resizing arrays.
114 The macros described here may evaluate their arguments multiple times
115 unless otherwise specified.
116 .SS "Creation and destruction"
117 Each element type must have its own array
118 type declared using the
122 .BI DA_DECL( type_v ", " type );
124 Declares a new dynamic array type
126 whose elements have type
129 It is conventional to form the name of an array type by appending
131 to the base type name. If the base type is a standard type, or is
132 declared separately from the array, it's also conventional to declare a
133 macro with the same name as the array type only in uppercase which may
134 be used to prevent multiple declarations, e.g.,
143 is a valid static initializer for all types of dynamic arrays. For
144 cases where this isn't appropriate, a dynamic array may be initialized
147 passing it the address of the array.
149 Arrays may be disposed of using the
151 macro, which again takes the address of the array.
152 .SS "Storage allocation"
154 Space for new array elements may be reserved using the
158 macros, which reserve space at the end and beginning of the array
159 respectively. Both macros take two arguments: the address of an array
160 object and the number of spare elements required.
162 Neither of these macros actually extends the array; both merely allocate
163 memory for the array to extend itself into. Use the macros
167 to actually increase the bounds of the array.
169 Note that when space is reserved, all the array elements might move.
170 You should be careful not to depend on the addresses of array elements.
171 If sufficient storage cannot be allocated, the exception
178 takes one argument: the address of a dynamic array. It minimizes the
179 amount of memory used by the array. This is a useful function to call
180 when the array's size has finally settled down.
184 accepts the address of an array. It reduces the length of the array to
185 zero. No storage is deallocated. Resetting arrays might not be a good
186 idea if the objects in the array are dynamically allocated.
187 .SS "Accessing array elements"
190 is the address of a dynamic array object, then
192 is the base address of the actual array. The elements are stored
193 contiguously starting at this address. An element at index
195 may be referenced using the syntax
198 The number of elements in the array
202 An integer array index
211 There may be some spare slots at the end of the array. In particular,
213 .BI DA_ENSURE( a ", " n )
214 there will be at least
216 spare slots. The number of spare slots at the end of the array may be
218 .BI DA_SPARE( a )\fR.
220 Similarly, there may be spare slots before the start of the array. In
221 particular, after a call to
222 .BI DA_SHUNT( a ", " n )
223 there will be at least
225 spare slots. The number of spare slots before the start of the array
227 .BI DA_OFFSET( a )\fR.
230 .BI DA_INCLUDE( a ", " i )
231 ensures that the array's bounds include the index
233 by extending the array if necessary. The exception
235 is thrown if there isn't enough memory to do this.
237 The array's bounds may be extended by
240 .BI DA_EXTEND( a ", " n )\fR.
244 .BI DA_SPARE( a )\fR;
245 if this is not the case then the exception
250 may be negative to reduce the bounds of the array: in this case it must
257 does the same job, but performs no error checking.
260 .BI DA_SLIDE( a ", " n )
261 offsets the array elements by
265 is positive, the array elements are slid upwards, to higher-numbered
268 is negative, the elements are slid downwards. Precisely, what happens
269 is that elements which used to have index
280 .BI DA_OFFSET( a )\fR;
285 .BI \-DA_LEN( a )\fR.
288 does the same job, only without the error checking.
294 do the same things as
298 respectively, except that they interpret the sign of their second
299 arguments in the opposite way. This is useful if the argument is
300 unsigned (e.g., if it's based on
302 There are unsafe versions of both these macros too.
303 .SS "Stack operations"
304 Dynamic arrays support Perl-like stack operations. Given an array
307 and an object of the array's element type
309 the following operations are provided:
311 .BI DA_PUSH( a ", " x )
314 to the end of the array, increasing the array size by one.
316 .IB x " = DA_POP(" a )
317 Remove the final element of the array, assigning
319 its value and decreasing the array size by one.
321 .BI DA_UNSHIFT( a ", " x )
324 at the beginning of the array, shifting all the elements up one place
325 and increasing the array size by one.
327 .IB x " = DA_SHIFT(" a )
328 Remove the first element of the array, assigning
330 its value, shifting all the subsequent array items down one place and
331 decreasing the array size by one.
337 can fail due to lack of memory, in which case
339 is thrown. The operations
343 can fail because the array is empty, in which case
351 are lvalues referring to the first and last elements in the array
352 respectively. They are unsafe if the array is empty.
353 .SS "Low-level details"
354 This section describes low-level details of the dynamic array
355 implementation. You should try to avoid making use of this information
356 if you can, since the interface may change in later library versions.
357 In particular, more subtleties may be added which low-level access will
360 Dynamic arrays are structures with the format
362 .BI "typedef struct " type_v " {"
369 indicates the current base of the array. This will move in the
370 allocated space as a result of
380 structure contains the following elements:
383 The number of allocated slots from
388 The number of items considered to be in the array. The allocated space
389 is usually larger than this.
392 The number of allocated slots preceding
394 The total number of allocated items is therefore
400 The number of items pushed or ensured since the last array expansion.
402 .B "unsigned unshift"
403 The number of items unshifted or shunted since the last array expansion.
409 members are used by the expansion routines to decide how to allocate
410 free space before and after the array elements following a reallocation.
411 The other members should be fairly self-explanatory.
413 The reallocation routines
418 have a regular interface. They're a bit
419 strange, though, because they have to deal with lots of different types
420 of arrays. The arguments they take are:
425 member of the array block.
428 The array base pointer from the array block (i.e., the
433 The element size for the array.
440 only.) The number of spare slots required.
442 The functions may modify the base structure, and return a newly
443 allocated (or at least repositioned) array base pointer, or throw
445 if there's not enough memory.
447 The three functions behave as follows:
450 Ensure that there are at least
452 spare slots after the end of the array.
455 Ensure that there are at least
457 spare slots preceding the start of the array.
460 Reallocate the array to use the smallest amount of memory possible.
465 Mark Wooding, <mdw@distorted.org.uk>