17 \h'-\w'\\$1\ 'u'\\$1\ \c
22 .TH darray 3 "21 October 1999" mLib
24 darray \- dense, dynamically resizing arrays
52 .B "#include <mLib/darray.h>"
54 .BI DA_DECL( atype ", " type );
55 .IB atype " " a " = DA_INIT;"
56 .BI "void DA_CREATE(" atype " *" a );
57 .BI "void DA_DESTROY(" atype " *" a );
59 .BI "void DA_ENSURE(" atype " *" a ", size_t " n );
60 .BI "void DA_SHUNT(" atype " *" a ", size_t " n );
61 .BI "void DA_TIDY(" atype " *" a );
63 .IB type " *DA(" atype " *" a );
64 .BI "size_t DA_LEN(" atype " *" a );
65 .BI "size_t DA_SPARE(" atype " *" a );
66 .BI "size_t DA_OFFSET(" atype " *" a );
67 .BI "void DA_INCLUDE(" atype " *" a ", size_t " i );
68 .BI "void DA_EXTEND(" atype " *" a ", long " n );
69 .BI "void DA_UNSAFE_EXTEND(" atype " *" a ", long " n );
70 .BI "void DA_SLIDE(" atype " *" a ", long " n );
71 .BI "void DA_UNSAFE_SLIDE(" atype " *" a ", long " n );
73 .BI "void DA_PUSH(" atype " *" a ", " type " " x );
74 .IB type " DA_POP(" atype " *" a );
75 .BI "void DA_UNSHIFT(" atype " *" a ", " type " " x );
76 .IB type " DA_SHIFT(" atype " *" a );
78 .BI "void *da_ensure(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
79 .BI "void *da_shunt(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
80 .BI "void *da_tidy(da_base *" b ", void *" v ", size_t " sz );
85 declares a collection of types, macros and functions which implement
86 dynamically resizing arrays.
88 The macros described here may evaluate their arguments multiple times
89 unless otherwise specified.
90 .SS "Creation and destruction"
91 Each element type must have its own array
92 type declared using the
96 .BI DA_DECL( atype ", " type );
98 Declares a new dynamic array type
100 whose elements have type
105 is a valid static initializer for all types of dynamic arrays. For
106 cases where this isn't appropriate, a dynamic array may be initialized
109 passing it the address of the array.
111 Arrays may be disposed of using the
113 macro, which again takes the address of the array.
114 .SS "Storage allocation"
116 Space for new array elements may be reserved using the
120 macros, which reserve space at the end and beginning of the array
121 respectively. Both macros take two arguments: the address of an array
122 object and the number of spare elements required.
124 Neither of these macros actually extends the array; both merely allocate
125 memory for the array to extend itself into. Use the macros
129 to actually increase the bounds of the array.
131 Note that when space is reserved, all the array elements might move.
132 You should be careful not to depend on the addresses of array elements.
133 If sufficient storage cannot be allocated, the exception
140 takes one argument: the address of a dynamic array. It minimizes the
141 amount of memory used by the array. This is a useful function to call
142 when the array's size has finally settled down.
143 .SS "Accessing array elements"
146 is the address of a dynamic array object, then
148 is the base address of the actual array. The elements are stored
149 contiguously starting at this address. An element at index
151 may be referenced using the syntax
154 The number of elements in the array
158 An integer array index
167 There may be some spare slots at the end of the array. In particular,
169 .BI DA_ENSURE( a ", " n )
170 there will be at least
172 spare slots. The number of spare slots at the end of the array may be
174 .BI DA_SPARE( a )\fR.
176 Similarly, there may be spare slots before the start of the array. In
177 particular, after a call to
178 .BI DA_SHUNT( a ", " n )
179 there will be at least
181 spare slots. The number of spare slots before the start of the array
183 .BI DA_OFFSET( a )\fR.
186 .BI DA_INCLUDE( a ", " i )
187 ensures that the array's bounds include the index
189 by extending the array if necessary. The exception
191 is thrown if there isn't enough memory to do this.
193 The array's bounds may be extended by
196 .BI DA_EXTEND( a ", " n )\fR.
200 .BI DA_SPARE( a )\fR;
201 if this is not the case then the exception
206 may be negative to reduce the bounds of the array: in this case it must
213 does the same job, but performs no error checking.
216 .BI DA_SLIDE( a ", " n )
217 offsets the array elements by
221 is positive, the array elements are slid upwards, to higher-numbered
224 is negative, the elements are slid downwards. Precisely, what happens
225 is that elements which used to have index
236 .BI DA_OFFSET( a )\fR;
241 .BI \-DA_LEN( a )\fR.
244 does the same job, only without the error checking.
245 .SS "Stack operations"
246 Dynamic arrays support Perl-like stack operations. Given an array
249 and an object of the array's element type
251 the following operations are provided:
253 .BI DA_PUSH( a ", " x )
256 to the end of the array, increasing the array size by one.
258 .IB x " = DA_POP(" a )
259 Remove the final element of the array, assigning
261 its value and decreasing the array size by one.
263 .BI DA_UNSHIFT( a ", " x )
266 at the beginning of the array, shifting all the elements up one place
267 and increasing the array size by one.
269 .IB x " = DA_SHIFT(" a )
270 Remove the first element of the array, assigning
272 its value, shifting all the subsequent array items down one place and
273 decreasing the array size by one.
279 can fail due to lack of memory, in which case
281 is thrown. The operations
285 can fail because the array is empty, in which case
288 .SS "Low-level details"
289 This section describes low-level details of the dynamic array
290 implementation. You should try to avoid making use of this information
291 if you can, since the interface may change in later library versions.
292 In particular, more subtleties may be added which low-level access will
295 Dynamic arrays are structures with the format
297 .BI "typedef struct " atype " {"
304 indicates the current base of the array. This will move in the
305 allocated space as a result of
315 structure contains the following elements:
318 The number of allocated slots from
323 The number of items considered to be in the array. The allocated space
324 is usually larger than this.
327 The number of allocated slots preceding
329 The total number of allocated items is therefore
335 The number of items pushed or ensured since the last array expansion.
337 .B "unsigned unshift"
338 The number of items unshifted or shunted since the last array expansion.
344 members are used by the expansion routines to decide how to allocate
345 free space before and after the array elements following a reallocation.
346 The other members should be fairly self-explanatory.
348 The reallocation routines
353 have a regular interface. They're a bit
354 strange, though, because they have to deal with lots of different types
355 of arrays. The arguments they take are:
360 member of the array block.
363 The array base pointer from the array block (i.e., the
368 The element size for the array.
375 only.) The number of spare slots required.
377 The functions may modify the base structure, and return a newly
378 allocated (or at least repositioned) array base pointer, or throw
380 if there's not enough memory.
382 The three functions behave as follows:
385 Ensure that there are at least
387 spare slots after the end of the array.
390 Ensure that there are at least
392 spare slots preceding the start of the array.
395 Reallocate the array to use the smallest amount of memory possible.
400 Mark Wooding, <mdw@nsict.org>