3 * $Id: darray.h,v 1.1 1999/10/22 22:37:26 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.1 1999/10/22 22:37:26 mdw
34 * New dynamic array implementation replaces `dynarray.h'.
45 /*----- Header files ------------------------------------------------------*/
59 /*----- Various important constants ---------------------------------------*/
61 /* --- @DAEXC_UFLOW@, @DAEXC_OFLOW@ --- *
63 * Underflow and overflow exceptions raised by @DA_SHIFT@, and by @DA_POP@
67 #define DAEXC_UFLOW EXC_ALLOCN(EXC_MLIB, 0)
68 #define DAEXC_OFLOW EXC_ALLOCN(EXC_MLIB, 1)
70 /*----- Data structures ---------------------------------------------------*/
72 /* --- Base structure for dynamic arrays --- *
74 * An actual array has a `vector' @v@ in addition to this data (which is
75 * tucked away in the @b@ member). The vector contains the actual storage
76 * for the array elements.
78 * The vector pointer @v@ potentially points somewhere in the middle of the
79 * allocated storage. The @off@ member explains how far into the storage the
80 * vector points. The allocated storage is sufficient for @sz + off@ items
81 * to be stored. Valid array indices are integers between 0 (inclusive) and
82 * @len@ (exclusive). Thus, from @v@ onwards, there is space for @sz@
83 * elements, and of these, @sz - len@ are currently not considered to be
84 * within the array's bounds.
86 * The @push@ and @unshift@ counts record how many times these operations
87 * have been performed since the last extension of the array. They are used
88 * by the extension algorithm to decide how to position the data offset.
90 * Try to use the access macros rather than the structure members.
93 typedef struct da_base
{
94 size_t sz
; /* Size of allocated vector */
95 size_t len
; /* Length of useful portion */
96 size_t off
; /* Offset of @v@ into space */
97 unsigned push
, unshift
; /* Pushes/unshifts since growth */
100 /* --- @DA_DECL@ --- *
102 * Arguments: @atype@ = type name for the array
103 * @type@ = item type for the array
105 * Use: Declares a structure for decribing a dynamic array.
108 #define DA_DECL(atype, type) \
109 typedef struct atype { da_base b; type *v; } atype
111 /*----- Initialization, creation and destruction --------------------------*/
113 #define DA_INIT { { 0, 0, 0, 0, 0 }, 0 } /* Standard initializer */
115 /* --- @DA_CREATE@ --- *
117 * Arguments: @a@ = pointer to an array block (multiply evaluated)
119 * Use: Initializes an array block.
122 #define DA_CREATE(a) do { \
123 (a)->b.sz = (a)->b.len = 0; \
125 (a)->b.push = (a)->b.unshift = 0; \
129 /* --- @DA_DESTROY@ --- *
131 * Arguments: @a@ = pointer to an array block (multiply evaluated)
133 * Use: Destroys an array. The array is left valid but empty.
136 #define DA_DESTROY(a) do { \
138 free((a)->v - (a)->b.off); \
142 /*----- Storage reservation -----------------------------------------------*/
144 /* --- @DA_ENSURE@ --- *
146 * Arguments: @a@ = pointer to an array block (multiply evaluated)
147 * @n@ = required number of spare items in the array
149 * Use: Ensures that there are at least @n@ spare slots at the end of
153 #define DA_ENSURE(a, n) do { \
155 if (_n > (a)->b.sz - (a)->b.len) \
156 (a)->v = da_ensure(&(a)->b, (a)->v, sizeof((a)->v[0]), _n); \
161 /* --- @DA_SHUNT@ --- *
163 * Arguments: @a@ = pointer to an array block (multiply evaluated)
164 * @n@ = required number of spare items in the array
166 * Use: Ensures that there are at least @n@ spare slots at the start
170 #define DA_SHUNT(a, n) do { \
172 if (_n > (a)->b.off) \
173 (a)->v = da_shunt(&(a)->b, (a)->v, sizeof((a)->v[0]), _n); \
175 (a)->b.unshift += _n; \
178 /* --- @DA_TIDY@ --- *
180 * Arguments: @a@ = pointer to an array block (multiply evaluated)
182 * Use: Reduces the amount of storage required by an array to its
186 #define DA_TIDY(a) ((a)->v = da_tidy(&(a)->b, (a)->v, sizeof((a)->v[0])))
188 /*----- Access operations -------------------------------------------------*/
192 * Arguments: @a@ = pointer to array block
194 * Use: Expands to a reference to the array proper. Given an array
195 * @a@, item @i@ may be located using the expression @DA(a)[i]@.
198 #define DA(a) ((a)->v + 0)
200 /* --- @DA_LEN@ --- *
202 * Arguments: @a@ = pointer to array block
204 * Use: Expands to the number of elements in the array. Elements are
205 * assigned integer indices in the half-open interval
206 * [0, @DA_LEN(a)@). Don't change the length directly; use
207 * @DA_EXTEND@ instead.
210 #define DA_LEN(a) ((a)->b.len + 0)
212 /* --- @DA_SPARE@ --- *
214 * Arguments: @a@ = pointer to array block (multiply evaluated)
216 * Use: Evaluates to the number of spare array elements above the
220 #define DA_SPARE(a) ((a)->b.sz - (a)->b.len)
222 /* --- @DA_INCLUDE@ --- *
224 * Arguments: @a@ = pointer to array block (multiply evaluated)
225 * @i@ = index into array
227 * Use: Extends the array (if necessary) so that it includes the
231 #define DA_INCLUDE(a, i) do { \
233 size_t _len = DA_LEN(a); \
235 size_t _nn = _i - _len + 1; \
237 DA_UNSAFE_EXTEND(a, _nn); \
241 /* --- @DA_OFFSET@ --- *
243 * Arguments: @a@ = pointer to array block
245 * Use: Evaluates to the number of spare elements before the
246 * beginning of the array. Don't modify the offset directly;
247 * use @DA_SLIDE@ instead.
250 #define DA_OFFSET(a) ((a)->b.off + 0)
252 /* --- @DA_EXTEND@ --- *
254 * Arguments: @a@ = pointer to array block (multiply evaluated)
255 * @n@ = number of slots to add (multiply evaluated)
257 * Use: Extends the end of the array by @n@ elements. The exception
258 * @DAEXC_OFLOW@ is thrown if there is not enough space for the
259 * new elements (i.e., @n > DA_SPARE(a)@) -- call @DA_ENSURE@ to
260 * prevent this from happening. The integer @n@ may be
261 * negative; @DAEXC_UFLOW@ is called if @n < DA_LEN(a)@.
264 #define DA_EXTEND(a, n) do { \
265 if ((n) > 0 && (n) > DA_SPARE(a)) \
266 THROW(DAEXC_OFLOW); \
267 else if ((n) < 0 && -(n) > DA_LEN(a)) \
268 THROW(DAEXC_UFLOW); \
269 DA_UNSAFE_EXTEND(a, n); \
272 /* --- @DA_EXTEND@ --- *
274 * Arguments: @a@ = pointer to array block (multiply evaluated)
275 * @n@ = number of slots to add (multiply evaluated)
277 * Use: As for @DA_EXTEND@, only it doesn't check for errors.
280 #define DA_UNSAFE_EXTEND(a, n) do { \
284 /* --- @DA_SLIDE@ --- *
286 * Arguments: @a@ = pointer to array block (multiply evaluated)
287 * @n@ = number of positions to slide the array (multiply
291 * Use: Slides the array elements by @n@ positions. A positive @n@
292 * slides upwards, introducing new elements at the bottom; a
293 * negative @n@ slides downwards, removing low-numbered
294 * elements. Formally, what was at index @i - n@ before the
295 * slide is moved to index @i@. It is an error to slide by more
296 * than @DA_OFFSET(a)@ or less than @-DA_LEN(a)@. The exception
297 * @DAEXC_OFLOW@ is raised in the former case, and @DAEXC_UFLOW@
301 #define DA_SLIDE(a, n) do { \
302 if ((n) > 0 && (n) > DA_OFFSET(a)) \
303 THROW(DAEXC_OFLOW); \
304 else if ((n) < 0 && -(n) > DA_LEN(a)) \
305 THROW(DAEXC_UFLOW); \
306 DA_UNSAFE_SLIDE((a), (n)); \
309 /* --- @DA_UNSAFE_SLIDE@ --- *
311 * Arguments: @a@ = pointer to array block (multiply evaluated)
312 * @n@ = number of positions to slide the array (multiply
315 * Use: As for @DA_SLIDE@, only it doesn't check for errors.
318 #define DA_UNSAFE_SLIDE(a, n) do { \
325 /*----- Stack-like operations ---------------------------------------------*/
327 /* --- @DA_PUSH@ --- *
329 * Arguments: @a@ = pointer to an array block (multiply evaluated)
330 * @x@ = item to append to the end
332 * Use: Appends @x@ as the final element in the array @a@.
335 #define DA_PUSH(a, x) do { \
337 DA(a)[(a)->b.len++] = x; \
340 /* --- @DA_POP@ --- *
342 * Arguments: @a@ = pointer to an array block (multiply evaluated)
344 * Use: Evaluates to the final element in array @a@. The element is
345 * removed. An exception @DAEXC_UFLOW@ is raised if there is
346 * no item available to pop.
350 ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW), \
353 /* --- @DA_UNSHIFT@ --- *
355 * Arguments: @a@ = pointer to an array block (multiply evaluated)
356 * @x@ = the new element to insert
358 * Use: Inserts a new element at the beginning of an array. This
359 * might take a while.
362 #define DA_UNSHIFT(a, x) do { \
364 DA_UNSAFE_SLIDE(a, 1); \
368 /* --- @DA_SHIFT@ --- *
370 * Arguments: @a@ = pointer to an array block (multiply evaluated)
372 * Use: Evaluates to the initial element in array @a@. The element
373 * is removed, and all other elements are shifted down one
374 * place. The exception @DAEXC_UFLOW@ is raised if there is no
378 #define DA_SHIFT(a) \
379 ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW), \
385 /*----- Functions provided ------------------------------------------------*/
387 /* --- @da_ensure@ --- *
389 * Arguments: @da_base *b@ = pointer to array base structure
390 * @void *v@ = pointer to array vector
391 * @size_t sz@ = size of individual array elements
392 * @size_t n@ = number of items required at the end
394 * Returns: Pointer to newly allocated or adjusted array vector.
396 * Use: Extends a dynamic array to accommodate a number of new items
397 * at its end. This function is a helper for the @DA_ENSURE@
398 * macro, which should be used by preference.
401 extern void *da_ensure(da_base */
*b*/
, void */
*v*/
,
402 size_t /*sz*/, size_t /*n*/);
404 /* --- @da_shunt@ --- *
406 * Arguments: @da_base *b@ = pointer to array base structure
407 * @void *v@ = pointer to array vector
408 * @size_t sz@ = size of the array elements
409 * @size_t n@ = number of items required at the start
411 * Returns: Pointer to appropriately bodged vector.
413 * Use: Extends an array to accommodate items inserted at its front.
414 * This function is a helper for the @DA_SHUNT@ macro, which
415 * should be used by preference.
418 extern void *da_shunt(da_base */
*b*/
, void */
*v*/
,
419 size_t /*sz*/, size_t /*n*/);
421 /* --- @da_tidy@ --- *
423 * Arguments: @da_base *b@ = pointer to array base structure
424 * @void *v@ = pointer to vector
425 * @size_t sz@ = size of the array elements
427 * Returns: Newly allocated vector.
429 * Use: Minimizes the space occupied by an array. This function is a
430 * helper for the @DA_TIDY@ macro, which should be used by
434 extern void *da_tidy(da_base */
*b*/
, void */
*v*/
, size_t /*sz*/);
436 /*----- That's all, folks -------------------------------------------------*/