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
29 #define darray(type) struct { type *item; size_t size; size_t alloc; }
31 #define darray_new() { 0, 0, 0 }
33 #define darray_init(arr) do { \
34 (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
37 #define darray_free(arr) do { \
43 * Typedefs for darrays of common types. These are useful
44 * when you want to pass a pointer to an darray(T) around.
46 * The following will produce an incompatible pointer warning:
48 * void foo(darray(int) *arr);
49 * darray(int) arr = darray_new();
54 * void foo(darray_int *arr);
55 * darray_int arr = darray_new();
59 typedef darray (char) darray_char;
60 typedef darray (signed char) darray_schar;
61 typedef darray (unsigned char) darray_uchar;
63 typedef darray (short) darray_short;
64 typedef darray (int) darray_int;
65 typedef darray (long) darray_long;
67 typedef darray (unsigned short) darray_ushort;
68 typedef darray (unsigned int) darray_uint;
69 typedef darray (unsigned long) darray_ulong;
73 #define darray_item(arr, i) ((arr).item[i])
74 #define darray_size(arr) ((arr).size)
75 #define darray_empty(arr) ((arr).size == 0)
76 #define darray_mem(arr, offset) ((arr).item + (offset))
78 /*** Insertion (single item) ***/
80 #define darray_append(arr, ...) do { \
81 darray_resize(arr, (arr).size + 1); \
82 (arr).item[(arr).size - 1] = (__VA_ARGS__); \
85 /*** Insertion (multiple items) ***/
87 #define darray_append_items(arr, items, count) do { \
88 size_t __count = (count), __oldSize = (arr).size; \
89 darray_resize(arr, __oldSize + __count); \
90 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
93 #define darray_from_items(arr, items, count) do { \
94 size_t __count = (count); \
95 darray_resize(arr, __count); \
96 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
99 #define darray_copy(arr_to, arr_from) \
100 darray_from_items((arr_to), (arr_from).item, (arr_from).size)
102 /*** String buffer ***/
104 #define darray_append_string(arr, str) do { \
105 const char *__str = (str); \
106 darray_append_items(arr, __str, strlen(__str) + 1); \
110 #define darray_append_lit(arr, stringLiteral) do { \
111 darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
115 #define darray_appends_nullterminate(arr, items, count) do { \
116 size_t __count = (count), __oldSize = (arr).size; \
117 darray_resize(arr, __oldSize + __count + 1); \
118 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
119 (arr).item[--(arr).size] = 0; \
122 #define darray_prepends_nullterminate(arr, items, count) do { \
123 size_t __count = (count), __oldSize = (arr).size; \
124 darray_resize(arr, __count + __oldSize + 1); \
125 memmove((arr).item + __count, (arr).item, \
126 __oldSize * sizeof(*(arr).item)); \
127 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
128 (arr).item[--(arr).size] = 0; \
131 /*** Size management ***/
133 #define darray_resize(arr, newSize) \
134 darray_growalloc(arr, (arr).size = (newSize))
136 #define darray_resize0(arr, newSize) do { \
137 size_t __oldSize = (arr).size, __newSize = (newSize); \
138 (arr).size = __newSize; \
139 if (__newSize > __oldSize) { \
140 darray_growalloc(arr, __newSize); \
141 memset(&(arr).item[__oldSize], 0, \
142 (__newSize - __oldSize) * sizeof(*(arr).item)); \
146 #define darray_realloc(arr, newAlloc) do { \
147 (arr).item = realloc((arr).item, \
148 ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
151 #define darray_growalloc(arr, need) do { \
152 size_t __need = (need); \
153 if (__need > (arr).alloc) \
154 darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
158 darray_next_alloc(size_t alloc, size_t need)
169 #define darray_foreach(i, arr) \
170 for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
172 #define darray_foreach_from(i, arr, from) \
173 for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
175 /* Iterate on index and value at the same time, like Python's enumerate. */
176 #define darray_enumerate(idx, val, arr) \
177 for ((idx) = 0, (val) = &(arr).item[0]; \
178 (idx) < (arr).size; \
181 #define darray_enumerate_from(idx, val, arr, from) \
182 for ((idx) = (from), (val) = &(arr).item[0]; \
183 (idx) < (arr).size; \
186 #define darray_foreach_reverse(i, arr) \
187 for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
189 #endif /* CCAN_DARRAY_H */