3 .\" Manual for dynamically growing arrays
5 .\" (c) 1999--2001, 2005, 2009, 2023, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH darray 3mLib "21 October 1999" "Straylight/Edgeware" "mLib utilities library"
52 .\" @DA_UNSAFE_UNSLIDE
65 .\"--------------------------------------------------------------------------
67 darray \- dense, dynamically resizing arrays
69 .\"--------------------------------------------------------------------------
73 .B "#include <mLib/darray.h>"
77 .B " size_t sz, len, off;"
78 .B " unsigned push, unshift;"
82 .B "#define DA_INIT ..."
84 .B "#define DAEXC_UFLOW EXC_ALLOCN(EXC_MLIB, ...)"
85 .B "#define DAEXC_OFLOW EXC_ALLOCN(EXC_MLIB, ...)"
87 .BI DA_DECL( type_v ", " type );
88 .BI "void DA_CREATE(" type_v " *" a );
89 .BI "void DA_DESTROY(" type_v " *" a );
91 .BI "void DA_ENSURE(" type_v " *" a ", size_t " n );
92 .BI "void DA_SHUNT(" type_v " *" a ", size_t " n );
93 .BI "void DA_TIDY(" type_v " *" a );
94 .BI "void DA_RESET(" type_v " *" a );
96 .IB type " *DA(" type_v " *" a );
97 .BI "size_t DA_LEN(" type_v " *" a );
98 .BI "size_t DA_SPARE(" type_v " *" a );
99 .BI "size_t DA_OFFSET(" type_v " *" a );
100 .BI "void DA_INCLUDE(" type_v " *" a ", size_t " i );
102 .BI "void DA_EXTEND(" type_v " *" a ", long " n );
103 .BI "void DA_SHRINK(" type_v " *" a ", long " n );
104 .BI "void DA_SLIDE(" type_v " *" a ", long " n );
105 .BI "void DA_UNSLIDE(" type_v " *" a ", long " n );
107 .BI "void DA_UNSAFE_EXTEND(" type_v " *" a ", long " n );
108 .BI "void DA_UNSAFE_SHRINK(" type_v " *" a ", long " n );
109 .BI "void DA_UNSAFE_SLIDE(" type_v " *" a ", long " n );
110 .BI "void DA_UNSAFE_UNSLIDE(" type_v " *" a ", long " n );
112 .IB type " DA_FIRST(" type_v " *" a );
113 .IB type " DA_LAST(" type_v " *" a );
114 .BI "void DA_PUSH(" type_v " *" a ", " type " " x );
115 .IB type " DA_POP(" type_v " *" a );
116 .BI "void DA_UNSHIFT(" type_v " *" a ", " type " " x );
117 .IB type " DA_SHIFT(" type_v " *" a );
119 .BI "void *da_ensure(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
120 .BI "void *da_shunt(da_base *" b ", void *" v ", size_t " sz ", size_t " n );
121 .BI "void *da_tidy(da_base *" b ", void *" v ", size_t " sz );
124 .\"--------------------------------------------------------------------------
129 header file declares a collection of types, macros and functions which
130 implement dynamically resizing arrays.
132 The macros described here may evaluate their arguments multiple times
133 unless otherwise specified.
135 .SS "Creation and destruction"
136 Each element type must have its own array
137 type declared using the
141 .BI DA_DECL( type_v ", " type );
143 Declares a new dynamic array type
145 whose elements have type
148 It is conventional to form the name of an array type by appending
150 to the base type name. If the base type is a standard type, or is
151 declared separately from the array, it's also conventional to declare a
152 macro with the same name as the array type only in uppercase which may
153 be used to prevent multiple declarations, e.g.,
163 is a valid static initializer for all types of dynamic arrays. For
164 cases where this isn't appropriate, a dynamic array may be initialized
167 passing it the address of the array.
169 Arrays may be disposed of using the
171 macro, which again takes the address of the array.
173 .SS "Storage allocation"
175 Space for new array elements may be reserved using the
179 macros, which reserve space at the end and beginning of the array
180 respectively. Both macros take two arguments: the address of an array
181 object and the number of spare elements required.
183 Neither of these macros actually extends the array; both merely allocate
184 memory for the array to extend itself into. Use the macros
188 to actually increase the bounds of the array.
190 Note that when space is reserved, all the array elements might move.
191 You should be careful not to depend on the addresses of array elements.
192 If sufficient storage cannot be allocated, the exception
199 takes one argument: the address of a dynamic array. It minimizes the
200 amount of memory used by the array. This is a useful function to call
201 when the array's size has finally settled down.
205 accepts the address of an array. It reduces the length of the array to
206 zero. No storage is deallocated. Resetting arrays might not be a good
207 idea if the objects in the array are dynamically allocated.
209 .SS "Accessing array elements"
212 is the address of a dynamic array object, then
214 is the base address of the actual array. The elements are stored
215 contiguously starting at this address. An element at index
217 may be referenced using the syntax
220 The number of elements in the array
224 An integer array index
233 There may be some spare slots at the end of the array. In particular,
235 .BI DA_ENSURE( a ", " n )
236 there will be at least
238 spare slots. The number of spare slots at the end of the array may be
240 .BI DA_SPARE( a )\fR.
242 Similarly, there may be spare slots before the start of the array. In
243 particular, after a call to
244 .BI DA_SHUNT( a ", " n )
245 there will be at least
247 spare slots. The number of spare slots before the start of the array
249 .BI DA_OFFSET( a )\fR.
252 .BI DA_INCLUDE( a ", " i )
253 ensures that the array's bounds include the index
255 by extending the array if necessary. The exception
257 is thrown if there isn't enough memory to do this.
259 The array's bounds may be extended by
262 .BI DA_EXTEND( a ", " n )\fR.
266 .BI DA_SPARE( a )\fR;
267 if this is not the case then the exception
272 may be negative to reduce the bounds of the array: in this case it must
279 does the same job, but performs no error checking.
282 .BI DA_SLIDE( a ", " n )
283 offsets the array elements by
287 is positive, the array elements are slid upwards, to higher-numbered
290 is negative, the elements are slid downwards. Precisely, what happens
291 is that elements which used to have index
302 .BI DA_OFFSET( a )\fR;
307 .BI \-DA_LEN( a )\fR.
310 does the same job, only without the error checking.
316 do the same things as
320 respectively, except that they interpret the sign of their second
321 arguments in the opposite way. This is useful if the argument is
322 unsigned (e.g., if it's based on
324 There are unsafe versions of both these macros too.
326 .SS "Stack operations"
327 Dynamic arrays support Perl-like stack operations. Given an array
330 and an object of the array's element type
332 the following operations are provided:
334 .BI DA_PUSH( a ", " x )
337 to the end of the array, increasing the array size by one.
339 .IB x " = DA_POP(" a )
340 Remove the final element of the array, assigning
342 its value and decreasing the array size by one.
344 .BI DA_UNSHIFT( a ", " x )
347 at the beginning of the array, shifting all the elements up one place
348 and increasing the array size by one.
350 .IB x " = DA_SHIFT(" a )
351 Remove the first element of the array, assigning
353 its value, shifting all the subsequent array items down one place and
354 decreasing the array size by one.
360 can fail due to lack of memory, in which case
362 is thrown. The operations
366 can fail because the array is empty, in which case
374 are lvalues referring to the first and last elements in the array
375 respectively. They are unsafe if the array is empty.
377 .SS "Low-level details"
378 This section describes low-level details of the dynamic array
379 implementation. You should try to avoid making use of this information
380 if you can, since the interface may change in later library versions.
381 In particular, more subtleties may be added which low-level access will
384 Dynamic arrays are structures with the format
387 .BI "typedef struct " type_v " {"
394 indicates the current base of the array. This will move in the
395 allocated space as a result of
405 structure contains the following elements:
408 The number of allocated slots from
413 The number of items considered to be in the array. The allocated space
414 is usually larger than this.
417 The number of allocated slots preceding
419 The total number of allocated items is therefore
425 The number of items pushed or ensured since the last array expansion.
427 .B "unsigned unshift"
428 The number of items unshifted or shunted since the last array expansion.
434 members are used by the expansion routines to decide how to allocate
435 free space before and after the array elements following a reallocation.
436 The other members should be fairly self-explanatory.
438 The reallocation routines
443 have a regular interface. They're a bit
444 strange, though, because they have to deal with lots of different types
445 of arrays. The arguments they take are:
450 member of the array block.
453 The array base pointer from the array block (i.e., the
458 The element size for the array.
465 only.) The number of spare slots required.
467 The functions may modify the base structure, and return a newly
468 allocated (or at least repositioned) array base pointer, or throw
470 if there's not enough memory.
472 The three functions behave as follows:
475 Ensure that there are at least
477 spare slots after the end of the array.
480 Ensure that there are at least
482 spare slots preceding the start of the array.
485 Reallocate the array to use the smallest amount of memory possible.
487 .\"--------------------------------------------------------------------------
493 .\"--------------------------------------------------------------------------
496 Mark Wooding, <mdw@distorted.org.uk>
498 .\"----- That's all, folks --------------------------------------------------