darray.h
| 1 | /* |
| 2 | * Copyright (C) 2011 Joseph Adams <joeyadams3.14159@gmail.com> |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 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 |
| 20 | * THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef CCAN_DARRAY_H |
| 24 | #define CCAN_DARRAY_H |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | /* |
| 30 | * SYNOPSIS |
| 31 | * |
| 32 | * Life cycle of a darray (dynamically-allocated array): |
| 33 | * |
| 34 | * darray(int) a = darray_new(); |
| 35 | * darray_free(a); |
| 36 | * |
| 37 | * struct {darray(int) a;} foo; |
| 38 | * darray_init(foo.a); |
| 39 | * darray_free(foo.a); |
| 40 | * |
| 41 | * Typedefs for darrays of common types: |
| 42 | * |
| 43 | * darray_char, darray_schar, darray_uchar |
| 44 | * darray_short, darray_int, darray_long |
| 45 | * darray_ushort, darray_uint, darray_ulong |
| 46 | * |
| 47 | * Access: |
| 48 | * |
| 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); |
| 53 | * |
| 54 | * Insertion (single item): |
| 55 | * |
| 56 | * void darray_append(darray(T) arr, T item); |
| 57 | * void darray_prepend(darray(T) arr, T item); |
| 58 | * void darray_insert(darray(T) arr, size_t index, T item); |
| 59 | * void darray_push(darray(T) arr, T item); // same as darray_append |
| 60 | * |
| 61 | * Insertion (multiple items): |
| 62 | * |
| 63 | * void darray_append_items(darray(T) arr, T *items, size_t count); |
| 64 | * void darray_prepend_items(darray(T) arr, T *items, size_t count); |
| 65 | * |
| 66 | * void darray_appends(darray(T) arr, [T item, [...]]); |
| 67 | * void darray_prepends(darray(T) arr, [T item, [...]]); |
| 68 | * |
| 69 | * // Same functionality as above, but does not require typeof. |
| 70 | * void darray_appends_t(darray(T) arr, #T, [T item, [...]]); |
| 71 | * void darray_prepends_t(darray(T) arr, #T, [T item, [...]]); |
| 72 | * |
| 73 | * Removal: |
| 74 | * |
| 75 | * T darray_pop(darray(T) arr | darray_size(arr) != 0); |
| 76 | * T* darray_pop_check(darray(T*) arr); |
| 77 | * void darray_remove(darray(T) arr, size_t index); |
| 78 | * |
| 79 | * Replacement: |
| 80 | * |
| 81 | * void darray_from_items(darray(T) arr, T *items, size_t count); |
| 82 | * void darray_from_c(darray(T) arr, T c_array[N]); |
| 83 | * |
| 84 | * String buffer: |
| 85 | * |
| 86 | * void darray_append_string(darray(char) arr, const char *str); |
| 87 | * void darray_append_lit(darray(char) arr, char stringLiteral[N+1]); |
| 88 | * |
| 89 | * void darray_prepend_string(darray(char) arr, const char *str); |
| 90 | * void darray_prepend_lit(darray(char) arr, char stringLiteral[N+1]); |
| 91 | * |
| 92 | * void darray_from_string(darray(T) arr, const char *str); |
| 93 | * void darray_from_lit(darray(char) arr, char stringLiteral[N+1]); |
| 94 | * |
| 95 | * Size management: |
| 96 | * |
| 97 | * void darray_resize(darray(T) arr, size_t newSize); |
| 98 | * void darray_resize0(darray(T) arr, size_t newSize); |
| 99 | * |
| 100 | * void darray_realloc(darray(T) arr, size_t newAlloc); |
| 101 | * void darray_growalloc(darray(T) arr, size_t newAlloc); |
| 102 | * |
| 103 | * void darray_make_room(darray(T) arr, size_t room); |
| 104 | * |
| 105 | * Traversal: |
| 106 | * |
| 107 | * darray_foreach(T *&i, darray(T) arr) {...} |
| 108 | * darray_foreach_reverse(T *&i, darray(T) arr) {...} |
| 109 | * |
| 110 | * Except for darray_foreach, darray_foreach_reverse, and darray_remove, |
| 111 | * all macros evaluate their non-darray arguments only once. |
| 112 | */ |
| 113 | |
| 114 | /*** Life cycle ***/ |
| 115 | |
| 116 | #define darray(type) struct {type *item; size_t size; size_t alloc;} |
| 117 | |
| 118 | #define darray_new() {0,0,0} |
| 119 | #define darray_init(arr) do {(arr).item=0; (arr).size=0; (arr).alloc=0;} while(0) |
| 120 | #define darray_free(arr) do {free((arr).item);} while(0) |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * Typedefs for darrays of common types. These are useful |
| 125 | * when you want to pass a pointer to an darray(T) around. |
| 126 | * |
| 127 | * The following will produce an incompatible pointer warning: |
| 128 | * |
| 129 | * void foo(darray(int) *arr); |
| 130 | * darray(int) arr = darray_new(); |
| 131 | * foo(&arr); |
| 132 | * |
| 133 | * The workaround: |
| 134 | * |
| 135 | * void foo(darray_int *arr); |
| 136 | * darray_int arr = darray_new(); |
| 137 | * foo(&arr); |
| 138 | */ |
| 139 | |
| 140 | typedef darray(char) darray_char; |
| 141 | typedef darray(signed char) darray_schar; |
| 142 | typedef darray(unsigned char) darray_uchar; |
| 143 | |
| 144 | typedef darray(short) darray_short; |
| 145 | typedef darray(int) darray_int; |
| 146 | typedef darray(long) darray_long; |
| 147 | |
| 148 | typedef darray(unsigned short) darray_ushort; |
| 149 | typedef darray(unsigned int) darray_uint; |
| 150 | typedef darray(unsigned long) darray_ulong; |
| 151 | |
| 152 | |
| 153 | /*** Access ***/ |
| 154 | |
| 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) |
| 159 | |
| 160 | |
| 161 | /*** Insertion (single item) ***/ |
| 162 | |
| 163 | #define darray_append(arr, ...) do { \ |
| 164 | darray_resize(arr, (arr).size+1); \ |
| 165 | (arr).item[(arr).size-1] = (__VA_ARGS__); \ |
| 166 | } while(0) |
| 167 | #define darray_prepend(arr, ...) do { \ |
| 168 | darray_resize(arr, (arr).size+1); \ |
| 169 | memmove((arr).item+1, (arr).item, ((arr).size-1)*sizeof(*(arr).item)); \ |
| 170 | (arr).item[0] = (__VA_ARGS__); \ |
| 171 | } while(0) |
| 172 | #define darray_insert(arr, i, ...) do { \ |
| 173 | size_t index_ = (i); \ |
| 174 | darray_resize(arr, (arr).size+1); \ |
| 175 | memmove((arr).item+index_+1, (arr).item+index_, ((arr).size-index_-1)*sizeof(*(arr).item)); \ |
| 176 | (arr).item[index_] = (__VA_ARGS__); \ |
| 177 | } while(0) |
| 178 | #define darray_push(arr, ...) darray_append(arr, __VA_ARGS__) |
| 179 | |
| 180 | |
| 181 | /*** Insertion (multiple items) ***/ |
| 182 | |
| 183 | #define darray_append_items(arr, items, count) do { \ |
| 184 | size_t count_ = (count), oldSize_ = (arr).size; \ |
| 185 | darray_resize(arr, oldSize_ + count_); \ |
| 186 | memcpy((arr).item + oldSize_, items, count_ * sizeof(*(arr).item)); \ |
| 187 | } while(0) |
| 188 | |
| 189 | #define darray_prepend_items(arr, items, count) do { \ |
| 190 | size_t count_ = (count), oldSize_ = (arr).size; \ |
| 191 | darray_resize(arr, count_ + oldSize_); \ |
| 192 | memmove((arr).item + count_, (arr).item, oldSize_ * sizeof(*(arr).item)); \ |
| 193 | memcpy((arr).item, items, count_ * sizeof(*(arr).item)); \ |
| 194 | } while(0) |
| 195 | |
| 196 | #define darray_append_items_nullterminate(arr, items, count) do { \ |
| 197 | size_t count_ = (count), oldSize_ = (arr).size; \ |
| 198 | darray_resize(arr, oldSize_ + count_ + 1); \ |
| 199 | memcpy((arr).item + oldSize_, items, count_ * sizeof(*(arr).item)); \ |
| 200 | (arr).item[--(arr).size] = 0; \ |
| 201 | } while(0) |
| 202 | |
| 203 | #define darray_prepend_items_nullterminate(arr, items, count) do { \ |
| 204 | size_t count_ = (count), oldSize_ = (arr).size; \ |
| 205 | darray_resize(arr, count_ + oldSize_ + 1); \ |
| 206 | memmove((arr).item + count_, (arr).item, oldSize_ * sizeof(*(arr).item)); \ |
| 207 | memcpy((arr).item, items, count_ * sizeof(*(arr).item)); \ |
| 208 | (arr).item[--(arr).size] = 0; \ |
| 209 | } while(0) |
| 210 | |
| 211 | #if HAVE_TYPEOF |
| 212 | #define darray_appends(arr, ...) darray_appends_t(arr, typeof((*(arr).item)), __VA_ARGS__) |
| 213 | #define darray_prepends(arr, ...) darray_prepends_t(arr, typeof((*(arr).item)), __VA_ARGS__) |
| 214 | #endif |
| 215 | |
| 216 | #define darray_appends_t(arr, type, ...) do { \ |
| 217 | type src_[] = {__VA_ARGS__}; \ |
| 218 | darray_append_items(arr, src_, sizeof(src_)/sizeof(*src_)); \ |
| 219 | } while(0) |
| 220 | #define darray_prepends_t(arr, type, ...) do { \ |
| 221 | type src_[] = {__VA_ARGS__}; \ |
| 222 | darray_prepend_items(arr, src_, sizeof(src_)/sizeof(*src_)); \ |
| 223 | } while(0) |
| 224 | |
| 225 | |
| 226 | /*** Removal ***/ |
| 227 | |
| 228 | /* Warning: Do not call darray_pop on an empty darray. */ |
| 229 | #define darray_pop(arr) ((arr).item[--(arr).size]) |
| 230 | #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL) |
| 231 | /* Warning, slow: Requires copying all elements after removed item. */ |
| 232 | #define darray_remove(arr, i) do { \ |
| 233 | size_t index_ = (i); \ |
| 234 | if (index_ < arr.size-1) \ |
| 235 | memmove(&(arr).item[index_], &(arr).item[index_+1], ((arr).size-1-index_)*sizeof(*(arr).item)); \ |
| 236 | (arr).size--; \ |
| 237 | } while(0) |
| 238 | |
| 239 | |
| 240 | /*** Replacement ***/ |
| 241 | |
| 242 | #define darray_from_items(arr, items, count) do {size_t count_ = (count); darray_resize(arr, count_); memcpy((arr).item, items, count_*sizeof(*(arr).item));} while(0) |
| 243 | #define darray_from_c(arr, c_array) darray_from_items(arr, c_array, sizeof(c_array)/sizeof(*(c_array))) |
| 244 | |
| 245 | |
| 246 | /*** String buffer ***/ |
| 247 | |
| 248 | #define darray_append_string(arr, str) do {const char *str_ = (str); darray_append_items(arr, str_, strlen(str_)+1); (arr).size--;} while(0) |
| 249 | #define darray_append_lit(arr, stringLiteral) do {darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); (arr).size--;} while(0) |
| 250 | |
| 251 | #define darray_prepend_string(arr, str) do { \ |
| 252 | const char *str_ = (str); \ |
| 253 | darray_prepend_items_nullterminate(arr, str_, strlen(str_)); \ |
| 254 | } while(0) |
| 255 | #define darray_prepend_lit(arr, stringLiteral) \ |
| 256 | darray_prepend_items_nullterminate(arr, stringLiteral, sizeof(stringLiteral) - 1) |
| 257 | |
| 258 | #define darray_from_string(arr, str) do {const char *str_ = (str); darray_from_items(arr, str_, strlen(str_)+1); (arr).size--;} while(0) |
| 259 | #define darray_from_lit(arr, stringLiteral) do {darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); (arr).size--;} while(0) |
| 260 | |
| 261 | |
| 262 | /*** Size management ***/ |
| 263 | |
| 264 | #define darray_resize(arr, newSize) darray_growalloc(arr, (arr).size = (newSize)) |
| 265 | #define darray_resize0(arr, newSize) do { \ |
| 266 | size_t oldSize_ = (arr).size, newSize_ = (newSize); \ |
| 267 | (arr).size = newSize_; \ |
| 268 | if (newSize_ > oldSize_) { \ |
| 269 | darray_growalloc(arr, newSize_); \ |
| 270 | memset(&(arr).item[oldSize_], 0, (newSize_ - oldSize_) * sizeof(*(arr).item)); \ |
| 271 | } \ |
| 272 | } while(0) |
| 273 | |
| 274 | #define darray_realloc(arr, newAlloc) do { \ |
| 275 | (arr).item = xrealloc((arr).item, ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \ |
| 276 | } while(0) |
| 277 | #define darray_growalloc(arr, need) do { \ |
| 278 | size_t need_ = (need); \ |
| 279 | if (need_ > (arr).alloc) \ |
| 280 | darray_realloc(arr, darray_next_alloc((arr).alloc, need_)); \ |
| 281 | } while(0) |
| 282 | |
| 283 | #if HAVE_STATEMENT_EXPR==1 |
| 284 | #define darray_make_room(arr, room) ({size_t newAlloc = (arr).size+(room); if ((arr).alloc<newAlloc) darray_realloc(arr, newAlloc); (arr).item+(arr).size; }) |
| 285 | #endif |
| 286 | |
| 287 | static inline size_t darray_next_alloc(size_t alloc, size_t need) |
| 288 | { |
| 289 | if (alloc == 0) |
| 290 | alloc = 1; |
| 291 | while (alloc < need) |
| 292 | alloc *= 2; |
| 293 | return alloc; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /*** Traversal ***/ |
| 298 | |
| 299 | /* |
| 300 | * darray_foreach(T *&i, darray(T) arr) {...} |
| 301 | * |
| 302 | * Traverse a darray. `i` must be declared in advance as a pointer to an item. |
| 303 | */ |
| 304 | #define darray_foreach(i, arr) \ |
| 305 | for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++) |
| 306 | |
| 307 | /* |
| 308 | * darray_foreach_reverse(T *&i, darray(T) arr) {...} |
| 309 | * |
| 310 | * Like darray_foreach, but traverse in reverse order. |
| 311 | */ |
| 312 | #define darray_foreach_reverse(i, arr) \ |
| 313 | for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; ) |
| 314 | |
| 315 | |
| 316 | #endif /* CCAN_DARRAY_H */ |
| 317 | |
| 318 | /* |
| 319 | |
| 320 | darray_growalloc(arr, newAlloc) sees if the darray can currently hold newAlloc items; |
| 321 | if not, it increases the alloc to satisfy this requirement, allocating slack |
| 322 | space to avoid having to reallocate for every size increment. |
| 323 | |
| 324 | darray_from_string(arr, str) copies a string to an darray_char. |
| 325 | |
| 326 | darray_push(arr, item) pushes an item to the end of the darray. |
| 327 | darray_pop(arr) pops it back out. Be sure there is at least one item in the darray before calling. |
| 328 | darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray. |
| 329 | |
| 330 | 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. |
| 331 | Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function. |
| 332 | |
| 333 | The following require HAVE_TYPEOF==1 : |
| 334 | |
| 335 | darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray. |
| 336 | darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\ |
| 337 | |
| 338 | |
| 339 | Examples: |
| 340 | |
| 341 | darray(int) arr; |
| 342 | int *i; |
| 343 | |
| 344 | darray_appends(arr, 0,1,2,3,4); |
| 345 | darray_appends(arr, -5,-4,-3,-2,-1); |
| 346 | darray_foreach(i, arr) |
| 347 | printf("%d ", *i); |
| 348 | printf("\n"); |
| 349 | |
| 350 | darray_free(arr); |
| 351 | |
| 352 | |
| 353 | typedef struct {int n,d;} Fraction; |
| 354 | darray(Fraction) fractions; |
| 355 | Fraction *i; |
| 356 | |
| 357 | darray_appends(fractions, {3,4}, {3,5}, {2,1}); |
| 358 | darray_foreach(i, fractions) |
| 359 | printf("%d/%d\n", i->n, i->d); |
| 360 | |
| 361 | darray_free(fractions); |
| 362 | */ |
| 363 |