17 \h'-\w'\\$1\ 'u'\\$1\ \c
22 .TH darray 3 "21 October 1999" mLib
24 darray \- dense, dynamically resizing arrays
45 .\" @DA_UNSAFE_UNSLIDE
57 .B "#include <mLib/darray.h>"
59 .BI DA_DECL( atype ", " type );
60 .IB atype " " a " = DA_INIT;"
61 .BI "void DA_CREATE(" atype " *" a );
62 .BI "void DA_DESTROY(" atype " *" a );
64 .BI "void DA_ENSURE(" atype " *" a ", size_t " n );
65 .BI "void DA_SHUNT(" atype " *" a ", size_t " n );
66 .BI "void DA_TIDY(" atype " *" a );
67 .BI "void DA_RESET(" atype " *" a );
69 .IB type " *DA(" atype " *" a );
70 .BI "size_t DA_LEN(" atype " *" a );
71 .BI "size_t DA_SPARE(" atype " *" a );
72 .BI "size_t DA_OFFSET(" atype " *" a );
73 .BI "void DA_INCLUDE(" atype " *" a ", size_t " i );
75 .BI "void DA_EXTEND(" atype " *" a ", long " n );
76 .BI "void DA_SHRINK(" atype " *" a ", long " n );
77 .BI "void DA_SLIDE(" atype " *" a ", long " n );
78 .BI "void DA_UNSLIDE(" atype " *" a ", long " n );
80 .BI "void DA_UNSAFE_EXTEND(" atype " *" a ", long " n );
81 .BI "void DA_UNSAFE_SHRINK(" atype " *" a ", long " n );
82 .BI "void DA_UNSAFE_SLIDE(" atype " *" a ", long " n );
83 .BI "void DA_UNSAFE_UNSLIDE(" atype " *" a ", long " n );
85 .BI "void DA_PUSH(" atype " *" a ", " type " " x );
86 .IB type " DA_POP(" atype " *" a );
87 .BI "void DA_UNSHIFT(" atype " *" a ", " type " " x );
88 .IB type " DA_SHIFT(" atype " *" a );
90 .BI "void *da_ensure(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
91 .BI "void *da_shunt(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
92 .BI "void *da_tidy(da_base *" b ", void *" v ", size_t " sz );
97 declares a collection of types, macros and functions which implement
98 dynamically resizing arrays.
100 The macros described here may evaluate their arguments multiple times
101 unless otherwise specified.
102 .SS "Creation and destruction"
103 Each element type must have its own array
104 type declared using the
108 .BI DA_DECL( atype ", " type );
110 Declares a new dynamic array type
112 whose elements have type
117 is a valid static initializer for all types of dynamic arrays. For
118 cases where this isn't appropriate, a dynamic array may be initialized
121 passing it the address of the array.
123 Arrays may be disposed of using the
125 macro, which again takes the address of the array.
126 .SS "Storage allocation"
128 Space for new array elements may be reserved using the
132 macros, which reserve space at the end and beginning of the array
133 respectively. Both macros take two arguments: the address of an array
134 object and the number of spare elements required.
136 Neither of these macros actually extends the array; both merely allocate
137 memory for the array to extend itself into. Use the macros
141 to actually increase the bounds of the array.
143 Note that when space is reserved, all the array elements might move.
144 You should be careful not to depend on the addresses of array elements.
145 If sufficient storage cannot be allocated, the exception
152 takes one argument: the address of a dynamic array. It minimizes the
153 amount of memory used by the array. This is a useful function to call
154 when the array's size has finally settled down.
158 accepts the address of an array. It reduces the length of the array to
159 zero. No storage is deallocated. Resetting arrays might not be a good
160 idea if the objects in the array are dynamically allocated.
161 .SS "Accessing array elements"
164 is the address of a dynamic array object, then
166 is the base address of the actual array. The elements are stored
167 contiguously starting at this address. An element at index
169 may be referenced using the syntax
172 The number of elements in the array
176 An integer array index
185 There may be some spare slots at the end of the array. In particular,
187 .BI DA_ENSURE( a ", " n )
188 there will be at least
190 spare slots. The number of spare slots at the end of the array may be
192 .BI DA_SPARE( a )\fR.
194 Similarly, there may be spare slots before the start of the array. In
195 particular, after a call to
196 .BI DA_SHUNT( a ", " n )
197 there will be at least
199 spare slots. The number of spare slots before the start of the array
201 .BI DA_OFFSET( a )\fR.
204 .BI DA_INCLUDE( a ", " i )
205 ensures that the array's bounds include the index
207 by extending the array if necessary. The exception
209 is thrown if there isn't enough memory to do this.
211 The array's bounds may be extended by
214 .BI DA_EXTEND( a ", " n )\fR.
218 .BI DA_SPARE( a )\fR;
219 if this is not the case then the exception
224 may be negative to reduce the bounds of the array: in this case it must
231 does the same job, but performs no error checking.
234 .BI DA_SLIDE( a ", " n )
235 offsets the array elements by
239 is positive, the array elements are slid upwards, to higher-numbered
242 is negative, the elements are slid downwards. Precisely, what happens
243 is that elements which used to have index
254 .BI DA_OFFSET( a )\fR;
259 .BI \-DA_LEN( a )\fR.
262 does the same job, only without the error checking.
268 do the same things as
272 respectively, except that they interpret the sign of their second
273 arguments in the opposite way. This is useful if the argument is
274 unsigned (e.g., if it's based on
275 .BR DA_LEN ). There are unsafed versions of both these macros too.
276 .SS "Stack operations"
277 Dynamic arrays support Perl-like stack operations. Given an array
280 and an object of the array's element type
282 the following operations are provided:
284 .BI DA_PUSH( a ", " x )
287 to the end of the array, increasing the array size by one.
289 .IB x " = DA_POP(" a )
290 Remove the final element of the array, assigning
292 its value and decreasing the array size by one.
294 .BI DA_UNSHIFT( a ", " x )
297 at the beginning of the array, shifting all the elements up one place
298 and increasing the array size by one.
300 .IB x " = DA_SHIFT(" a )
301 Remove the first element of the array, assigning
303 its value, shifting all the subsequent array items down one place and
304 decreasing the array size by one.
310 can fail due to lack of memory, in which case
312 is thrown. The operations
316 can fail because the array is empty, in which case
319 .SS "Low-level details"
320 This section describes low-level details of the dynamic array
321 implementation. You should try to avoid making use of this information
322 if you can, since the interface may change in later library versions.
323 In particular, more subtleties may be added which low-level access will
326 Dynamic arrays are structures with the format
328 .BI "typedef struct " atype " {"
335 indicates the current base of the array. This will move in the
336 allocated space as a result of
346 structure contains the following elements:
349 The number of allocated slots from
354 The number of items considered to be in the array. The allocated space
355 is usually larger than this.
358 The number of allocated slots preceding
360 The total number of allocated items is therefore
366 The number of items pushed or ensured since the last array expansion.
368 .B "unsigned unshift"
369 The number of items unshifted or shunted since the last array expansion.
375 members are used by the expansion routines to decide how to allocate
376 free space before and after the array elements following a reallocation.
377 The other members should be fairly self-explanatory.
379 The reallocation routines
384 have a regular interface. They're a bit
385 strange, though, because they have to deal with lots of different types
386 of arrays. The arguments they take are:
391 member of the array block.
394 The array base pointer from the array block (i.e., the
399 The element size for the array.
406 only.) The number of spare slots required.
408 The functions may modify the base structure, and return a newly
409 allocated (or at least repositioned) array base pointer, or throw
411 if there's not enough memory.
413 The three functions behave as follows:
416 Ensure that there are at least
418 spare slots after the end of the array.
421 Ensure that there are at least
423 spare slots preceding the start of the array.
426 Reallocate the array to use the smallest amount of memory possible.
431 Mark Wooding, <mdw@nsict.org>