3 * $Id: darray.h,v 1.3 1999/11/05 14:32:43 mdw Exp $
5 * Dynamically growing dense arrays
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/11/05 14:32:43 mdw
34 * Minor change in argument naming.
36 * Revision 1.2 1999/10/29 22:59:22 mdw
37 * New array adjustment macros for unsigned arguments.
39 * Revision 1.1 1999/10/22 22:37:26 mdw
40 * New dynamic array implementation replaces `dynarray.h'.
51 /*----- Header files ------------------------------------------------------*/
65 /*----- Various important constants ---------------------------------------*/
67 /* --- @DAEXC_UFLOW@, @DAEXC_OFLOW@ --- *
69 * Underflow and overflow exceptions raised by @DA_SHIFT@, and by @DA_POP@
73 #define DAEXC_UFLOW EXC_ALLOCN(EXC_MLIB, 0)
74 #define DAEXC_OFLOW EXC_ALLOCN(EXC_MLIB, 1)
76 /*----- Data structures ---------------------------------------------------*/
78 /* --- Base structure for dynamic arrays --- *
80 * An actual array has a `vector' @v@ in addition to this data (which is
81 * tucked away in the @b@ member). The vector contains the actual storage
82 * for the array elements.
84 * The vector pointer @v@ potentially points somewhere in the middle of the
85 * allocated storage. The @off@ member explains how far into the storage the
86 * vector points. The allocated storage is sufficient for @sz + off@ items
87 * to be stored. Valid array indices are integers between 0 (inclusive) and
88 * @len@ (exclusive). Thus, from @v@ onwards, there is space for @sz@
89 * elements, and of these, @sz - len@ are currently not considered to be
90 * within the array's bounds.
92 * The @push@ and @unshift@ counts record how many times these operations
93 * have been performed since the last extension of the array. They are used
94 * by the extension algorithm to decide how to position the data offset.
96 * Try to use the access macros rather than the structure members.
99 typedef struct da_base {
100 size_t sz; /* Size of allocated vector */
101 size_t len; /* Length of useful portion */
102 size_t off; /* Offset of @v@ into space */
103 unsigned push, unshift; /* Pushes/unshifts since growth */
106 /* --- @DA_DECL@ --- *
108 * Arguments: @atype@ = type name for the array
109 * @type@ = item type for the array
111 * Use: Declares a structure for decribing a dynamic array.
114 #define DA_DECL(type_v, type) \
115 typedef struct type_v { da_base b; type *v; } type_v
117 /*----- Initialization, creation and destruction --------------------------*/
119 #define DA_INIT { { 0, 0, 0, 0, 0 }, 0 } /* Standard initializer */
121 /* --- @DA_CREATE@ --- *
123 * Arguments: @a@ = pointer to an array block (multiply evaluated)
125 * Use: Initializes an array block.
128 #define DA_CREATE(a) do { \
129 (a)->b.sz = (a)->b.len = 0; \
131 (a)->b.push = (a)->b.unshift = 0; \
135 /* --- @DA_DESTROY@ --- *
137 * Arguments: @a@ = pointer to an array block (multiply evaluated)
139 * Use: Destroys an array. The array is left valid but empty.
142 #define DA_DESTROY(a) do { \
144 free((a)->v - (a)->b.off); \
148 /*----- Storage reservation -----------------------------------------------*/
150 /* --- @DA_ENSURE@ --- *
152 * Arguments: @a@ = pointer to an array block (multiply evaluated)
153 * @n@ = required number of spare items in the array
155 * Use: Ensures that there are at least @n@ spare slots at the end of
159 #define DA_ENSURE(a, n) do { \
161 if (_n > (a)->b.sz - (a)->b.len) \
162 (a)->v = da_ensure(&(a)->b, (a)->v, sizeof((a)->v[0]), _n); \
167 /* --- @DA_SHUNT@ --- *
169 * Arguments: @a@ = pointer to an array block (multiply evaluated)
170 * @n@ = required number of spare items in the array
172 * Use: Ensures that there are at least @n@ spare slots at the start
176 #define DA_SHUNT(a, n) do { \
178 if (_n > (a)->b.off) \
179 (a)->v = da_shunt(&(a)->b, (a)->v, sizeof((a)->v[0]), _n); \
181 (a)->b.unshift += _n; \
184 /* --- @DA_TIDY@ --- *
186 * Arguments: @a@ = pointer to an array block (multiply evaluated)
188 * Use: Reduces the amount of storage required by an array to its
192 #define DA_TIDY(a) ((a)->v = da_tidy(&(a)->b, (a)->v, sizeof((a)->v[0])))
194 /* --- @DA_RESET@ --- *
196 * Arguments: @a@ = pointer to array block
198 * Use: Removes all the items from the named array. This might not
199 * be a good idea. No storage is freed.
202 #define DA_RESET(a) ((a)->b.len = 0)
204 /*----- Access operations -------------------------------------------------*/
208 * Arguments: @a@ = pointer to array block
210 * Use: Expands to a reference to the array proper. Given an array
211 * @a@, item @i@ may be located using the expression @DA(a)[i]@.
214 #define DA(a) ((a)->v + 0)
216 /* --- @DA_LEN@ --- *
218 * Arguments: @a@ = pointer to array block
220 * Use: Expands to the number of elements in the array. Elements are
221 * assigned integer indices in the half-open interval
222 * [0, @DA_LEN(a)@). Don't change the length directly; use
223 * @DA_EXTEND@ instead.
226 #define DA_LEN(a) ((a)->b.len + 0)
228 /* --- @DA_SPARE@ --- *
230 * Arguments: @a@ = pointer to array block (multiply evaluated)
232 * Use: Evaluates to the number of spare array elements above the
236 #define DA_SPARE(a) ((a)->b.sz - (a)->b.len)
238 /* --- @DA_INCLUDE@ --- *
240 * Arguments: @a@ = pointer to array block (multiply evaluated)
241 * @i@ = index into array
243 * Use: Extends the array (if necessary) so that it includes the
247 #define DA_INCLUDE(a, i) do { \
249 size_t _len = DA_LEN(a); \
251 size_t _nn = _i - _len + 1; \
253 DA_UNSAFE_EXTEND(a, _nn); \
257 /* --- @DA_OFFSET@ --- *
259 * Arguments: @a@ = pointer to array block
261 * Use: Evaluates to the number of spare elements before the
262 * beginning of the array. Don't modify the offset directly;
263 * use @DA_SLIDE@ instead.
266 #define DA_OFFSET(a) ((a)->b.off + 0)
268 /* --- @DA_EXTEND@ --- *
270 * Arguments: @a@ = pointer to array block (multiply evaluated)
271 * @n@ = number of slots to add (multiply evaluated)
273 * Use: Extends the end of the array by @n@ elements. The exception
274 * @DAEXC_OFLOW@ is thrown if there is not enough space for the
275 * new elements (i.e., @n > DA_SPARE(a)@) -- call @DA_ENSURE@ to
276 * prevent this from happening. The integer @n@ may be
277 * negative; @DAEXC_UFLOW@ is called if @n < DA_LEN(a)@.
280 #define DA_EXTEND(a, n) do { \
281 if ((n) > 0 && (n) > DA_SPARE(a)) \
282 THROW(DAEXC_OFLOW); \
283 else if ((n) < 0 && -(n) > DA_LEN(a)) \
284 THROW(DAEXC_UFLOW); \
285 DA_UNSAFE_EXTEND(a, n); \
288 /* --- @DA_UNSAFE_EXTEND@ --- *
290 * Arguments: @a@ = pointer to array block (multiply evaluated)
291 * @n@ = number of slots to add (multiply evaluated)
293 * Use: As for @DA_EXTEND@, only it doesn't check for errors.
296 #define DA_UNSAFE_EXTEND(a, n) do { \
300 /* --- @DA_SLIDE@ --- *
302 * Arguments: @a@ = pointer to array block (multiply evaluated)
303 * @n@ = number of positions to slide the array (multiply
307 * Use: Slides the array elements by @n@ positions. A positive @n@
308 * slides upwards, introducing new elements at the bottom; a
309 * negative @n@ slides downwards, removing low-numbered
310 * elements. Formally, what was at index @i - n@ before the
311 * slide is moved to index @i@. It is an error to slide by more
312 * than @DA_OFFSET(a)@ or less than @-DA_LEN(a)@. The exception
313 * @DAEXC_OFLOW@ is raised in the former case, and @DAEXC_UFLOW@
317 #define DA_SLIDE(a, n) do { \
318 if ((n) > 0 && (n) > DA_OFFSET(a)) \
319 THROW(DAEXC_OFLOW); \
320 else if ((n) < 0 && -(n) > DA_LEN(a)) \
321 THROW(DAEXC_UFLOW); \
322 DA_UNSAFE_SLIDE((a), (n)); \
325 /* --- @DA_UNSAFE_SLIDE@ --- *
327 * Arguments: @a@ = pointer to array block (multiply evaluated)
328 * @n@ = number of positions to slide the array (multiply
331 * Use: As for @DA_SLIDE@, only it doesn't check for errors.
334 #define DA_UNSAFE_SLIDE(a, n) do { \
341 /* --- @DA_SHRINK@ --- *
343 * Arguments: @a@ = pointer to array block (multiply evaluated)
344 * @n@ = number of slots to remove (multiply evaluated)
346 * Use: As for @DA_EXTEND@, with the sense of the argument reversed.
349 #define DA_SHRINK(a, n) do { \
350 if ((n) > 0 && (n) > DA_LEN(a)) \
351 THROW(DAEXC_UFLOW); \
352 else if ((n) < 0 && -(n) > DA_SPARE(a)) \
353 THROW(DAEXC_OFLOW); \
354 DA_UNSAFE_SHRINK(a, n); \
357 /* --- @DA_UNSAFE_SHRINK@ --- *
359 * Arguments: @a@ = pointer to array block (multiply evaluated)
360 * @n@ = number of slots to add (multiply evaluated)
362 * Use: As for @DA_SHRINK@, only it doesn't check for errors.
365 #define DA_UNSAFE_SHRINK(a, n) do { \
369 /* --- @DA_UNSLIDE@ --- *
371 * Arguments: @a@ = pointer to array block (multiply evaluated)
372 * @n@ = number of positions to slide the array (multiply
376 * Use: As for @DA_SLIDE@, only in the other direction.
379 #define DA_UNSLIDE(a, n) do { \
380 if ((n) > 0 && (n) > DA_LEN(a)) \
381 THROW(DAEXC_UFLOW); \
382 else if ((n) < 0 && -(n) > DA_OFFSET(a)) \
383 THROW(DAEXC_OFLOW); \
384 DA_UNSAFE_UNSLIDE((a), (n)); \
387 /* --- @DA_UNSAFE_UNSLIDE@ --- *
389 * Arguments: @a@ = pointer to array block (multiply evaluated)
390 * @n@ = number of positions to slide the array (multiply
393 * Use: As for @DA_UNSLIDE@, only it doesn't check for errors.
396 #define DA_UNSAFE_UNSLIDE(a, n) do { \
403 /*----- Stack-like operations ---------------------------------------------*/
405 /* --- @DA_PUSH@ --- *
407 * Arguments: @a@ = pointer to an array block (multiply evaluated)
408 * @x@ = item to append to the end
410 * Use: Appends @x@ as the final element in the array @a@.
413 #define DA_PUSH(a, x) do { \
415 DA(a)[(a)->b.len++] = x; \
418 /* --- @DA_POP@ --- *
420 * Arguments: @a@ = pointer to an array block (multiply evaluated)
422 * Use: Evaluates to the final element in array @a@. The element is
423 * removed. An exception @DAEXC_UFLOW@ is raised if there is
424 * no item available to pop.
428 ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW), \
431 /* --- @DA_UNSHIFT@ --- *
433 * Arguments: @a@ = pointer to an array block (multiply evaluated)
434 * @x@ = the new element to insert
436 * Use: Inserts a new element at the beginning of an array. This
437 * might take a while.
440 #define DA_UNSHIFT(a, x) do { \
442 DA_UNSAFE_SLIDE(a, 1); \
446 /* --- @DA_SHIFT@ --- *
448 * Arguments: @a@ = pointer to an array block (multiply evaluated)
450 * Use: Evaluates to the initial element in array @a@. The element
451 * is removed, and all other elements are shifted down one
452 * place. The exception @DAEXC_UFLOW@ is raised if there is no
456 #define DA_SHIFT(a) \
457 ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW), \
463 /*----- Functions provided ------------------------------------------------*/
465 /* --- @da_ensure@ --- *
467 * Arguments: @da_base *b@ = pointer to array base structure
468 * @void *v@ = pointer to array vector
469 * @size_t sz@ = size of individual array elements
470 * @size_t n@ = number of items required at the end
472 * Returns: Pointer to newly allocated or adjusted array vector.
474 * Use: Extends a dynamic array to accommodate a number of new items
475 * at its end. This function is a helper for the @DA_ENSURE@
476 * macro, which should be used by preference.
479 extern void *da_ensure(da_base */*b*/, void */*v*/,
480 size_t /*sz*/, size_t /*n*/);
482 /* --- @da_shunt@ --- *
484 * Arguments: @da_base *b@ = pointer to array base structure
485 * @void *v@ = pointer to array vector
486 * @size_t sz@ = size of the array elements
487 * @size_t n@ = number of items required at the start
489 * Returns: Pointer to appropriately bodged vector.
491 * Use: Extends an array to accommodate items inserted at its front.
492 * This function is a helper for the @DA_SHUNT@ macro, which
493 * should be used by preference.
496 extern void *da_shunt(da_base */*b*/, void */*v*/,
497 size_t /*sz*/, size_t /*n*/);
499 /* --- @da_tidy@ --- *
501 * Arguments: @da_base *b@ = pointer to array base structure
502 * @void *v@ = pointer to vector
503 * @size_t sz@ = size of the array elements
505 * Returns: Newly allocated vector.
507 * Use: Minimizes the space occupied by an array. This function is a
508 * helper for the @DA_TIDY@ macro, which should be used by
512 extern void *da_tidy(da_base */*b*/, void */*v*/, size_t /*sz*/);
514 /*----- That's all, folks -------------------------------------------------*/