3 * Dynamically growing dense arrays
5 * (c) 1999 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
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public 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
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Header files ------------------------------------------------------*/
45 /*----- Various important constants ---------------------------------------*/
47 /* --- @DAEXC_UFLOW@, @DAEXC_OFLOW@ --- *
49 * Underflow and overflow exceptions raised by @DA_SHIFT@, and by @DA_POP@
53 #define DAEXC_UFLOW EXC_ALLOCN(EXC_MLIB, 0)
54 #define DAEXC_OFLOW EXC_ALLOCN(EXC_MLIB, 1)
56 /*----- Data structures ---------------------------------------------------*/
58 /* --- Base structure for dynamic arrays --- *
60 * An actual array has a `vector' @v@ in addition to this data (which is
61 * tucked away in the @b@ member). The vector contains the actual storage
62 * for the array elements.
64 * The vector pointer @v@ potentially points somewhere in the middle of the
65 * allocated storage. The @off@ member explains how far into the storage the
66 * vector points. The allocated storage is sufficient for @sz + off@ items
67 * to be stored. Valid array indices are integers between 0 (inclusive) and
68 * @len@ (exclusive). Thus, from @v@ onwards, there is space for @sz@
69 * elements, and of these, @sz - len@ are currently not considered to be
70 * within the array's bounds.
72 * The @push@ and @unshift@ counts record how many times these operations
73 * have been performed since the last extension of the array. They are used
74 * by the extension algorithm to decide how to position the data offset.
76 * Try to use the access macros rather than the structure members.
79 typedef struct da_base {
80 size_t sz; /* Size of allocated vector */
81 size_t len; /* Length of useful portion */
82 size_t off; /* Offset of @v@ into space */
83 unsigned push, unshift; /* Pushes/unshifts since growth */
84 arena *a; /* Pointer to allocation arena */
87 /* --- @DA_DECL@ --- *
89 * Arguments: @atype@ = type name for the array
90 * @type@ = item type for the array
92 * Use: Declares a structure for decribing a dynamic array.
95 #define DA_DECL(type_v, type) \
96 typedef struct type_v { da_base b; type *v; } type_v
98 /*----- Initialization, creation and destruction --------------------------*/
100 #define DA_INIT { { 0, 0, 0, 0, 0, &arena_stdlib }, 0 }
102 /* --- @DA_CREATE@ --- *
104 * Arguments: @a@ = pointer to an array block (multiply evaluated)
106 * Use: Initializes an array block.
109 #define DA_CREATE(aa) do { \
110 (aa)->b.sz = (aa)->b.len = 0; \
112 (aa)->b.push = (aa)->b.unshift = 0; \
113 (aa)->b.a = &arena_stdlib; \
117 /* --- @DA_DESTROY@ --- *
119 * Arguments: @a@ = pointer to an array block (multiply evaluated)
121 * Use: Destroys an array. The array is left valid but empty.
124 #define DA_DESTROY(aa) do { \
126 x_free((aa)->b.a, (aa)->v - (aa)->b.off); \
130 /*----- Storage reservation -----------------------------------------------*/
132 /* --- @DA_ENSURE@ --- *
134 * Arguments: @a@ = pointer to an array block (multiply evaluated)
135 * @n@ = required number of spare items in the array
137 * Use: Ensures that there are at least @n@ spare slots at the end of
141 #define DA_ENSURE(a, n) do { \
143 if (_n > (a)->b.sz - (a)->b.len) \
144 (a)->v = da_ensure(&(a)->b, (a)->v, sizeof((a)->v[0]), _n); \
149 /* --- @DA_SHUNT@ --- *
151 * Arguments: @a@ = pointer to an array block (multiply evaluated)
152 * @n@ = required number of spare items in the array
154 * Use: Ensures that there are at least @n@ spare slots at the start
158 #define DA_SHUNT(a, n) do { \
160 if (_n > (a)->b.off) \
161 (a)->v = da_shunt(&(a)->b, (a)->v, sizeof((a)->v[0]), _n); \
163 (a)->b.unshift += _n; \
166 /* --- @DA_TIDY@ --- *
168 * Arguments: @a@ = pointer to an array block (multiply evaluated)
170 * Use: Reduces the amount of storage required by an array to its
174 #define DA_TIDY(a) ((a)->v = da_tidy(&(a)->b, (a)->v, sizeof((a)->v[0])))
176 /* --- @DA_RESET@ --- *
178 * Arguments: @a@ = pointer to array block
180 * Use: Removes all the items from the named array. This might not
181 * be a good idea. No storage is freed.
184 #define DA_RESET(a) ((a)->b.len = 0)
186 /*----- Access operations -------------------------------------------------*/
190 * Arguments: @a@ = pointer to array block
192 * Use: Expands to a reference to the array proper. Given an array
193 * @a@, item @i@ may be located using the expression @DA(a)[i]@.
196 #define DA(a) ((a)->v + 0)
198 /* --- @DA_LEN@ --- *
200 * Arguments: @a@ = pointer to array block
202 * Use: Expands to the number of elements in the array. Elements are
203 * assigned integer indices in the half-open interval
204 * [0, @DA_LEN(a)@). Don't change the length directly; use
205 * @DA_EXTEND@ instead.
208 #define DA_LEN(a) ((a)->b.len + 0)
210 /* --- @DA_SPARE@ --- *
212 * Arguments: @a@ = pointer to array block (multiply evaluated)
214 * Use: Evaluates to the number of spare array elements above the
218 #define DA_SPARE(a) ((a)->b.sz - (a)->b.len)
220 /* --- @DA_INCLUDE@ --- *
222 * Arguments: @a@ = pointer to array block (multiply evaluated)
223 * @i@ = index into array
225 * Use: Extends the array (if necessary) so that it includes the
229 #define DA_INCLUDE(a, i) do { \
231 size_t _len = DA_LEN(a); \
233 size_t _nn = _i - _len + 1; \
235 DA_UNSAFE_EXTEND(a, _nn); \
239 /* --- @DA_OFFSET@ --- *
241 * Arguments: @a@ = pointer to array block
243 * Use: Evaluates to the number of spare elements before the
244 * beginning of the array. Don't modify the offset directly;
245 * use @DA_SLIDE@ instead.
248 #define DA_OFFSET(a) ((a)->b.off + 0)
250 /* --- @DA_EXTEND@ --- *
252 * Arguments: @a@ = pointer to array block (multiply evaluated)
253 * @n@ = number of slots to add (multiply evaluated)
255 * Use: Extends the end of the array by @n@ elements. The exception
256 * @DAEXC_OFLOW@ is thrown if there is not enough space for the
257 * new elements (i.e., @n > DA_SPARE(a)@) -- call @DA_ENSURE@ to
258 * prevent this from happening. The integer @n@ may be
259 * negative; @DAEXC_UFLOW@ is called if @n < DA_LEN(a)@.
262 #define DA_EXTEND(a, n) do { \
263 if ((n) > 0 && (n) > DA_SPARE(a)) \
264 THROW(DAEXC_OFLOW); \
265 else if ((n) < 0 && -(n) > DA_LEN(a)) \
266 THROW(DAEXC_UFLOW); \
267 DA_UNSAFE_EXTEND(a, n); \
270 /* --- @DA_UNSAFE_EXTEND@ --- *
272 * Arguments: @a@ = pointer to array block (multiply evaluated)
273 * @n@ = number of slots to add (multiply evaluated)
275 * Use: As for @DA_EXTEND@, only it doesn't check for errors.
278 #define DA_UNSAFE_EXTEND(a, n) do { \
282 /* --- @DA_SLIDE@ --- *
284 * Arguments: @a@ = pointer to array block (multiply evaluated)
285 * @n@ = number of positions to slide the array (multiply
289 * Use: Slides the array elements by @n@ positions. A positive @n@
290 * slides upwards, introducing new elements at the bottom; a
291 * negative @n@ slides downwards, removing low-numbered
292 * elements. Formally, what was at index @i - n@ before the
293 * slide is moved to index @i@. It is an error to slide by more
294 * than @DA_OFFSET(a)@ or less than @-DA_LEN(a)@. The exception
295 * @DAEXC_OFLOW@ is raised in the former case, and @DAEXC_UFLOW@
299 #define DA_SLIDE(a, n) do { \
300 if ((n) > 0 && (n) > DA_OFFSET(a)) \
301 THROW(DAEXC_OFLOW); \
302 else if ((n) < 0 && -(n) > DA_LEN(a)) \
303 THROW(DAEXC_UFLOW); \
304 DA_UNSAFE_SLIDE((a), (n)); \
307 /* --- @DA_UNSAFE_SLIDE@ --- *
309 * Arguments: @a@ = pointer to array block (multiply evaluated)
310 * @n@ = number of positions to slide the array (multiply
313 * Use: As for @DA_SLIDE@, only it doesn't check for errors.
316 #define DA_UNSAFE_SLIDE(a, n) do { \
323 /* --- @DA_SHRINK@ --- *
325 * Arguments: @a@ = pointer to array block (multiply evaluated)
326 * @n@ = number of slots to remove (multiply evaluated)
328 * Use: As for @DA_EXTEND@, with the sense of the argument reversed.
331 #define DA_SHRINK(a, n) do { \
332 if ((n) > 0 && (n) > DA_LEN(a)) \
333 THROW(DAEXC_UFLOW); \
334 else if ((n) < 0 && -(n) > DA_SPARE(a)) \
335 THROW(DAEXC_OFLOW); \
336 DA_UNSAFE_SHRINK(a, n); \
339 /* --- @DA_UNSAFE_SHRINK@ --- *
341 * Arguments: @a@ = pointer to array block (multiply evaluated)
342 * @n@ = number of slots to add (multiply evaluated)
344 * Use: As for @DA_SHRINK@, only it doesn't check for errors.
347 #define DA_UNSAFE_SHRINK(a, n) do { \
351 /* --- @DA_UNSLIDE@ --- *
353 * Arguments: @a@ = pointer to array block (multiply evaluated)
354 * @n@ = number of positions to slide the array (multiply
358 * Use: As for @DA_SLIDE@, only in the other direction.
361 #define DA_UNSLIDE(a, n) do { \
362 if ((n) > 0 && (n) > DA_LEN(a)) \
363 THROW(DAEXC_UFLOW); \
364 else if ((n) < 0 && -(n) > DA_OFFSET(a)) \
365 THROW(DAEXC_OFLOW); \
366 DA_UNSAFE_UNSLIDE((a), (n)); \
369 /* --- @DA_UNSAFE_UNSLIDE@ --- *
371 * Arguments: @a@ = pointer to array block (multiply evaluated)
372 * @n@ = number of positions to slide the array (multiply
375 * Use: As for @DA_UNSLIDE@, only it doesn't check for errors.
378 #define DA_UNSAFE_UNSLIDE(a, n) do { \
385 /*----- Stack-like operations ---------------------------------------------*/
387 /* --- @DA_FIRST@ --- *
389 * Arguments: @a@ = pointer to an array block (multiply evaluated)
391 * Use: Evaluates to the initial element in array @a@. It is unsafe
392 * to do this if the array is empty. The array is not changed.
395 #define DA_FIRST(a) (DA(a)[0])
397 /* --- @DA_LAST@ --- *
399 * Arguments: @a@ = pointer to an array block (multiply evaluated)
401 * Use: Evaluates to the final element in array @a@. It is unsafe
402 * to do this if the array is empty. The array is not changed.
405 #define DA_LAST(a) (DA(a)[(a)->b.len - 1])
407 /* --- @DA_PUSH@ --- *
409 * Arguments: @a@ = pointer to an array block (multiply evaluated)
410 * @x@ = item to append to the end
412 * Use: Appends @x@ as the final element in the array @a@.
415 #define DA_PUSH(a, x) do { \
417 DA(a)[(a)->b.len++] = x; \
420 /* --- @DA_POP@ --- *
422 * Arguments: @a@ = pointer to an array block (multiply evaluated)
424 * Use: Evaluates to the final element in array @a@. The element is
425 * removed. An exception @DAEXC_UFLOW@ is raised if there is
426 * no item available to pop.
430 ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW), \
433 /* --- @DA_UNSHIFT@ --- *
435 * Arguments: @a@ = pointer to an array block (multiply evaluated)
436 * @x@ = the new element to insert
438 * Use: Inserts a new element at the beginning of an array. This
439 * might take a while.
442 #define DA_UNSHIFT(a, x) do { \
444 DA_UNSAFE_SLIDE(a, 1); \
448 /* --- @DA_SHIFT@ --- *
450 * Arguments: @a@ = pointer to an array block (multiply evaluated)
452 * Use: Evaluates to the initial element in array @a@. The element
453 * is removed, and all other elements are shifted down one
454 * place. The exception @DAEXC_UFLOW@ is raised if there is no
458 #define DA_SHIFT(a) \
459 ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW), \
465 /*----- Functions provided ------------------------------------------------*/
467 /* --- @da_ensure@ --- *
469 * Arguments: @da_base *b@ = pointer to array base structure
470 * @void *v@ = pointer to array vector
471 * @size_t sz@ = size of individual array elements
472 * @size_t n@ = number of items required at the end
474 * Returns: Pointer to newly allocated or adjusted array vector.
476 * Use: Extends a dynamic array to accommodate a number of new items
477 * at its end. This function is a helper for the @DA_ENSURE@
478 * macro, which should be used by preference.
481 extern void *da_ensure(da_base */*b*/, void */*v*/,
482 size_t /*sz*/, size_t /*n*/);
484 /* --- @da_shunt@ --- *
486 * Arguments: @da_base *b@ = pointer to array base structure
487 * @void *v@ = pointer to array vector
488 * @size_t sz@ = size of the array elements
489 * @size_t n@ = number of items required at the start
491 * Returns: Pointer to appropriately bodged vector.
493 * Use: Extends an array to accommodate items inserted at its front.
494 * This function is a helper for the @DA_SHUNT@ macro, which
495 * should be used by preference.
498 extern void *da_shunt(da_base */*b*/, void */*v*/,
499 size_t /*sz*/, size_t /*n*/);
501 /* --- @da_tidy@ --- *
503 * Arguments: @da_base *b@ = pointer to array base structure
504 * @void *v@ = pointer to vector
505 * @size_t sz@ = size of the array elements
507 * Returns: Newly allocated vector.
509 * Use: Minimizes the space occupied by an array. This function is a
510 * helper for the @DA_TIDY@ macro, which should be used by
514 extern void *da_tidy(da_base */*b*/, void */*v*/, size_t /*sz*/);
516 /*----- That's all, folks -------------------------------------------------*/