2 * Copyright (C) 2011 Joseph Adams <joeyadams3.14159@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 * Life cycle of a darray (dynamically-allocated array):
34 * darray(int) a = darray_new();
37 * struct {darray(int) a;} foo;
41 * Typedefs for darrays of common types:
43 * darray_char, darray_schar, darray_uchar
44 * darray_short, darray_int, darray_long
45 * darray_ushort, darray_uint, darray_ulong
49 * T darray_item(darray(T) arr, size_t index);
50 * size_t darray_size(darray(T) arr);
51 * size_t darray_alloc(darray(T) arr);
52 * bool darray_empty(darray(T) arr);
54 * // Access raw memory, starting from the item in offset.
55 * // Not safe, be careful, etc.
56 * T* darray_mem(darray(T) arr, size_t offset);
58 * Insertion (single item):
60 * void darray_append(darray(T) arr, T item);
61 * void darray_prepend(darray(T) arr, T item);
62 * void darray_push(darray(T) arr, T item); // same as darray_append
64 * Insertion (multiple items):
66 * void darray_append_items(darray(T) arr, T *items, size_t count);
67 * void darray_prepend_items(darray(T) arr, T *items, size_t count);
69 * void darray_appends(darray(T) arr, [T item, [...]]);
70 * void darray_prepends(darray(T) arr, [T item, [...]]);
74 * T darray_pop(darray(T) arr | darray_size(arr) != 0);
75 * T* darray_pop_check(darray(T*) arr);
79 * void darray_from_items(darray(T) arr, T *items, size_t count);
80 * void darray_from_c(darray(T) arr, T c_array[N]);
84 * void darray_append_string(darray(char) arr, const char *str);
85 * void darray_append_lit(darray(char) arr, char stringLiteral[N+1]);
87 * void darray_prepend_string(darray(char) arr, const char *str);
88 * void darray_prepend_lit(darray(char) arr, char stringLiteral[N+1]);
90 * void darray_from_string(darray(T) arr, const char *str);
91 * void darray_from_lit(darray(char) arr, char stringLiteral[N+1]);
95 * void darray_resize(darray(T) arr, size_t newSize);
96 * void darray_resize0(darray(T) arr, size_t newSize);
98 * void darray_realloc(darray(T) arr, size_t newAlloc);
99 * void darray_growalloc(darray(T) arr, size_t newAlloc);
103 * darray_foreach(T *&i, darray(T) arr) {...}
104 * darray_foreach_reverse(T *&i, darray(T) arr) {...}
106 * Except for darray_foreach and darray_foreach_reverse,
107 * all macros evaluate their non-darray arguments only once.
112 #define darray(type) struct { type *item; size_t size; size_t alloc; }
114 #define darray_new() { 0, 0, 0 }
116 #define darray_init(arr) do { \
117 (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
120 #define darray_free(arr) do { \
121 free((arr).item); darray_init(arr); \
125 * Typedefs for darrays of common types. These are useful
126 * when you want to pass a pointer to an darray(T) around.
128 * The following will produce an incompatible pointer warning:
130 * void foo(darray(int) *arr);
131 * darray(int) arr = darray_new();
136 * void foo(darray_int *arr);
137 * darray_int arr = darray_new();
141 typedef darray (char) darray_char;
142 typedef darray (signed char) darray_schar;
143 typedef darray (unsigned char) darray_uchar;
145 typedef darray (short) darray_short;
146 typedef darray (int) darray_int;
147 typedef darray (long) darray_long;
149 typedef darray (unsigned short) darray_ushort;
150 typedef darray (unsigned int) darray_uint;
151 typedef darray (unsigned long) darray_ulong;
155 #define darray_item(arr, i) ((arr).item[i])
156 #define darray_size(arr) ((arr).size)
157 #define darray_alloc(arr) ((arr).alloc)
158 #define darray_empty(arr) ((arr).size == 0)
160 #define darray_mem(arr, offset) ((arr).item + (offset))
161 #define darray_same(arr1, arr2) ((arr1).item == (arr2).item)
163 /*** Insertion (single item) ***/
165 #define darray_append(arr, ...) do { \
166 darray_resize(arr, (arr).size + 1); \
167 (arr).item[(arr).size - 1] = (__VA_ARGS__); \
170 #define darray_prepend(arr, ...) do { \
171 darray_resize(arr, (arr).size + 1); \
172 memmove((arr).item + 1, (arr).item, \
173 ((arr).size - 1) * sizeof(*(arr).item)); \
174 (arr).item[0] = (__VA_ARGS__); \
177 #define darray_push(arr, ...) darray_append(arr, __VA_ARGS__)
179 /*** Insertion (multiple items) ***/
181 #define darray_append_items(arr, items, count) do { \
182 size_t __count = (count), __oldSize = (arr).size; \
183 darray_resize(arr, __oldSize + __count); \
184 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
187 #define darray_prepend_items(arr, items, count) do { \
188 size_t __count = (count), __oldSize = (arr).size; \
189 darray_resize(arr, __count + __oldSize); \
190 memmove((arr).item + __count, (arr).item, \
191 __oldSize * sizeof(*(arr).item)); \
192 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
195 #define darray_append_items_nullterminate(arr, items, count) do { \
196 size_t __count = (count), __oldSize = (arr).size; \
197 darray_resize(arr, __oldSize + __count + 1); \
198 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
199 (arr).item[--(arr).size] = 0; \
202 #define darray_prepend_items_nullterminate(arr, items, count) do { \
203 size_t __count = (count), __oldSize = (arr).size; \
204 darray_resize(arr, __count + __oldSize + 1); \
205 memmove((arr).item + __count, (arr).item, \
206 __oldSize * sizeof(*(arr).item)); \
207 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
208 (arr).item[--(arr).size] = 0; \
211 #define darray_appends_t(arr, type, ...) do { \
212 type __src[] = { __VA_ARGS__ }; \
213 darray_append_items(arr, __src, sizeof(__src) / sizeof(*__src)); \
216 #define darray_prepends_t(arr, type, ...) do { \
217 type __src[] = { __VA_ARGS__ }; \
218 darray_prepend_items(arr, __src, sizeof(__src) / sizeof(*__src)); \
223 /* Warning: Do not call darray_pop on an empty darray. */
224 #define darray_pop(arr) ((arr).item[--(arr).size])
225 #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
227 /*** Replacement ***/
229 #define darray_from_items(arr, items, count) do { \
230 size_t __count = (count); \
231 darray_resize(arr, __count); \
232 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
235 #define darray_from_c(arr, c_array) \
236 darray_from_items(arr, c_array, sizeof(c_array) / sizeof(*(c_array)))
238 #define darray_copy(arr_to, arr_from) \
239 darray_from_items((arr_to), (arr_from).item, (arr_from).size)
241 /*** String buffer ***/
243 #define darray_append_string(arr, str) do { \
244 const char *__str = (str); \
245 darray_append_items(arr, __str, strlen(__str) + 1); \
249 #define darray_append_lit(arr, stringLiteral) do { \
250 darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
254 #define darray_prepend_string(arr, str) do { \
255 const char *__str = (str); \
256 darray_prepend_items_nullterminate(arr, __str, strlen(__str)); \
259 #define darray_prepend_lit(arr, stringLiteral) \
260 darray_prepend_items_nullterminate(arr, stringLiteral, \
261 sizeof(stringLiteral) - 1)
263 #define darray_from_string(arr, str) do { \
264 const char *__str = (str); \
265 darray_from_items(arr, __str, strlen(__str) + 1); \
269 #define darray_from_lit(arr, stringLiteral) do { \
270 darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); \
274 /*** Size management ***/
276 #define darray_resize(arr, newSize) \
277 darray_growalloc(arr, (arr).size = (newSize))
279 #define darray_resize0(arr, newSize) do { \
280 size_t __oldSize = (arr).size, __newSize = (newSize); \
281 (arr).size = __newSize; \
282 if (__newSize > __oldSize) { \
283 darray_growalloc(arr, __newSize); \
284 memset(&(arr).item[__oldSize], 0, \
285 (__newSize - __oldSize) * sizeof(*(arr).item)); \
289 #define darray_realloc(arr, newAlloc) do { \
290 (arr).item = realloc((arr).item, \
291 ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
294 #define darray_growalloc(arr, need) do { \
295 size_t __need = (need); \
296 if (__need > (arr).alloc) \
297 darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
301 darray_next_alloc(size_t alloc, size_t need)
313 * darray_foreach(T *&i, darray(T) arr) {...}
315 * Traverse a darray. `i` must be declared in advance as a pointer to an item.
317 #define darray_foreach(i, arr) \
318 for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
320 #define darray_foreach_from(i, arr, from) \
321 for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
323 /* Iterate on index and value at the same time, like Python's enumerate. */
324 #define darray_enumerate(idx, val, arr) \
325 for ((idx) = 0, (val) = &(arr).item[0]; \
326 (idx) < (arr).size; \
329 #define darray_enumerate_from(idx, val, arr, from) \
330 for ((idx) = (from), (val) = &(arr).item[0]; \
331 (idx) < (arr).size; \
335 * darray_foreach_reverse(T *&i, darray(T) arr) {...}
337 * Like darray_foreach, but traverse in reverse order.
339 #define darray_foreach_reverse(i, arr) \
340 for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
342 #endif /* CCAN_DARRAY_H */
346 * darray_growalloc(arr, newAlloc) sees if the darray can currently hold newAlloc items;
347 * if not, it increases the alloc to satisfy this requirement, allocating slack
348 * space to avoid having to reallocate for every size increment.
350 * darray_from_string(arr, str) copies a string to an darray_char.
352 * darray_push(arr, item) pushes an item to the end of the darray.
353 * darray_pop(arr) pops it back out. Be sure there is at least one item in the darray before calling.
354 * darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray.
356 * darray_make_room(arr, room) ensures there's 'room' elements of space after the end of the darray, and it returns a pointer to this space.
357 * Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
359 * The following require HAVE_TYPEOF==1 :
361 * darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray.
362 * darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\
370 * darray_appends(arr, 0,1,2,3,4);
371 * darray_appends(arr, -5,-4,-3,-2,-1);
372 * darray_foreach(i, arr)
379 * typedef struct {int n,d;} Fraction;
380 * darray(Fraction) fractions;
383 * darray_appends(fractions, {3,4}, {3,5}, {2,1});
384 * darray_foreach(i, fractions)
385 * printf("%d/%d\n", i->n, i->d);
387 * darray_free(fractions);