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)); \
221 #define darray_appends(arr, ...) \
222 darray_appends_t(arr, typeof((*(arr).item)), __VA_ARGS__)
224 #define darray_prepends(arr, ...) \
225 darray_prepends_t(arr, typeof((*(arr).item)), __VA_ARGS__)
229 /* Warning: Do not call darray_pop on an empty darray. */
230 #define darray_pop(arr) ((arr).item[--(arr).size])
231 #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
233 /*** Replacement ***/
235 #define darray_from_items(arr, items, count) do { \
236 size_t __count = (count); \
237 darray_resize(arr, __count); \
238 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
241 #define darray_from_c(arr, c_array) \
242 darray_from_items(arr, c_array, sizeof(c_array) / sizeof(*(c_array)))
244 #define darray_copy(arr_to, arr_from) \
245 darray_from_items((arr_to), (arr_from).item, (arr_from).size)
247 /*** String buffer ***/
249 #define darray_append_string(arr, str) do { \
250 const char *__str = (str); \
251 darray_append_items(arr, __str, strlen(__str) + 1); \
255 #define darray_append_lit(arr, stringLiteral) do { \
256 darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
260 #define darray_prepend_string(arr, str) do { \
261 const char *__str = (str); \
262 darray_prepend_items_nullterminate(arr, __str, strlen(__str)); \
265 #define darray_prepend_lit(arr, stringLiteral) \
266 darray_prepend_items_nullterminate(arr, stringLiteral, \
267 sizeof(stringLiteral) - 1)
269 #define darray_from_string(arr, str) do { \
270 const char *__str = (str); \
271 darray_from_items(arr, __str, strlen(__str) + 1); \
275 #define darray_from_lit(arr, stringLiteral) do { \
276 darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); \
280 /*** Size management ***/
282 #define darray_resize(arr, newSize) \
283 darray_growalloc(arr, (arr).size = (newSize))
285 #define darray_resize0(arr, newSize) do { \
286 size_t __oldSize = (arr).size, __newSize = (newSize); \
287 (arr).size = __newSize; \
288 if (__newSize > __oldSize) { \
289 darray_growalloc(arr, __newSize); \
290 memset(&(arr).item[__oldSize], 0, \
291 (__newSize - __oldSize) * sizeof(*(arr).item)); \
295 #define darray_realloc(arr, newAlloc) do { \
296 (arr).item = realloc((arr).item, \
297 ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
300 #define darray_growalloc(arr, need) do { \
301 size_t __need = (need); \
302 if (__need > (arr).alloc) \
303 darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
307 darray_next_alloc(size_t alloc, size_t need)
319 * darray_foreach(T *&i, darray(T) arr) {...}
321 * Traverse a darray. `i` must be declared in advance as a pointer to an item.
323 #define darray_foreach(i, arr) \
324 for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
326 #define darray_foreach_from(i, arr, from) \
327 for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
330 * darray_foreach_reverse(T *&i, darray(T) arr) {...}
332 * Like darray_foreach, but traverse in reverse order.
334 #define darray_foreach_reverse(i, arr) \
335 for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
337 #endif /* CCAN_DARRAY_H */
341 * darray_growalloc(arr, newAlloc) sees if the darray can currently hold newAlloc items;
342 * if not, it increases the alloc to satisfy this requirement, allocating slack
343 * space to avoid having to reallocate for every size increment.
345 * darray_from_string(arr, str) copies a string to an darray_char.
347 * darray_push(arr, item) pushes an item to the end of the darray.
348 * darray_pop(arr) pops it back out. Be sure there is at least one item in the darray before calling.
349 * darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray.
351 * 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.
352 * Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
354 * The following require HAVE_TYPEOF==1 :
356 * darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray.
357 * darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\
365 * darray_appends(arr, 0,1,2,3,4);
366 * darray_appends(arr, -5,-4,-3,-2,-1);
367 * darray_foreach(i, arr)
374 * typedef struct {int n,d;} Fraction;
375 * darray(Fraction) fractions;
378 * darray_appends(fractions, {3,4}, {3,5}, {2,1});
379 * darray_foreach(i, fractions)
380 * printf("%d/%d\n", i->n, i->d);
382 * darray_free(fractions);