darray.h 12 KB

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