3 * $Id: darray.c,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'.
38 /*----- Header files ------------------------------------------------------*/
47 /*----- Magic numbers -----------------------------------------------------*/
49 #define DA_INITSZ 64 /* Default size for new array */
50 #define DA_SLOTS 8 /* Number of preshifted slots */
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @da_ensure@ --- *
56 * Arguments: @da_base *b@ = pointer to array base structure
57 * @void *v@ = pointer to array vector
58 * @size_t sz@ = size of individual array elements
59 * @size_t n@ = number of items required at the end
61 * Returns: Pointer to newly allocated or adjusted array vector.
63 * Use: Extends a dynamic array to accommodate a number of new items
64 * at its end. This function is a helper for the @DA_ENSURE@
65 * macro, which should be used by preference.
68 void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
70 size_t rq = n + b->len;
75 /* --- Make sure there's something which needs doing --- *
77 * If there's enough space already then return immediately.
83 /* --- Compute a number of `unshift' slots --- *
85 * When returning from this function, the offset will be set to @slots@.
86 * If @unshift@ is zero, there's no point in reserving slots. Otherwise
87 * choose a power of two greater than @unshift@, with a minimum of
88 * @DA_SLOTS@. Then add the number of slots to the requirement.
95 while (slots < b->unshift)
100 /* --- Maybe just shunt data around a bit --- *
102 * If the vector is large enough, then theoretically we could cope by
103 * moving the objects about in their existing storage.
106 if (rq < b->sz + b->off) {
107 q = p - (b->off - slots) * sz;
108 memmove(q, p, b->len * sz);
109 b->sz += b->off - slots;
111 b->unshift = b->push = 0;
115 /* --- Reallocate the array --- */
117 nsz = v ? b->sz + b->off : DA_INITSZ;
118 do nsz <<= 1; while (nsz < rq);
119 q = xmalloc(nsz * sz);
121 memcpy(q, p, b->len * sz);
123 free(p - b->off * sz);
126 b->unshift = b->push = 0;
130 /* --- @da_shunt@ --- *
132 * Arguments: @da_base *b@ = pointer to array base structure
133 * @void *v@ = pointer to array vector
134 * @size_t sz@ = size of the array elements
135 * @size_t n@ = number of items required at the start
137 * Returns: Pointer to appropriately bodged vector.
139 * Use: Extends an array to accommodate items inserted at its front.
140 * This function is a helper for the @DA_SHUNT@ macro, which
141 * should be used by preference.
144 void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
151 /* --- Make sure there's something which needs doing --- *
153 * If there's enough space already then return immediately.
159 /* --- Compute a number of `push' slots --- *
161 * When returning from this function, there will be @slots@ free spaces at
162 * the end of the array. If @push@ is zero, there's no point in reserving
163 * slots. Otherwise choose a power of two greater than @push@, with a
164 * minimum of @DA_SLOTS@. To simplify matters, add the number of items
165 * already in the array to @slots@, and then add the number of slots to the
173 while (slots < b->push)
179 /* --- Maybe just shunt data around a bit --- *
181 * If the vector is large enough, then theoretically we could cope by
182 * moving the objects about in their existing storage.
185 if (rq < b->sz + b->off) {
186 q = p + (b->sz - slots) * sz;
187 memmove(q, p, b->len * sz);
188 b->off += b->sz - slots;
190 b->unshift = b->push = 0;
194 /* --- Reallocate the array --- */
196 nsz = v ? b->sz + b->off : DA_INITSZ;
197 do nsz <<= 1; while (nsz < rq);
198 q = xmalloc(nsz * sz);
199 q += (nsz - slots) * sz;
200 memcpy(q, p, b->len * sz);
202 free(p - b->off * sz);
203 b->off = nsz - slots;
205 b->unshift = b->push = 0;
209 /* --- @da_tidy@ --- *
211 * Arguments: @da_base *b@ = pointer to array base structure
212 * @void *v@ = pointer to vector
213 * @size_t sz@ = size of the array elements
215 * Returns: Newly allocated vector.
217 * Use: Minimizes the space occupied by an array. This function is a
218 * helper for the @DA_TIDY@ macro, which should be used by
222 void *da_tidy(da_base *b, void *v, size_t sz)
226 b->unshift = b->push = 0;
230 if (b->sz == b->len && b->off == 0)
234 free(p - b->off * sz);
238 q = xmalloc(b->len * sz);
239 memcpy(q, p, b->len * sz);
240 free(p - b->off * sz);
246 /* --- Note about testing --- *
248 * The test rig for this code is split into three parts. There's `da-gtest',
249 * which is a Perl script which generates a list of commands. The `da-ref'
250 * Perl script interprets these commands as operations on a Perl array. It's
251 * relatively conservatively written and believed to be reliable. The
252 * `da-test.c' file implements a command reader for the same syntax and
253 * performs the operations on an integer darray, producing output in the same
254 * format. To test darray, generate a command script with `da-gtest', pass
255 * it through both `da-ref' and `da-test' (the result of compiling
256 * da-test.c'), and compare the results. If they're not byte-for-byte
257 * identical, there's something wrong.
260 /*----- That's all, folks -------------------------------------------------*/