1 /* dynarr - dynamic resizable C array data structure
2 * author: John Tsiombikas <nuclear@member.fsf.org>
3 * license: public domain
10 * int *arr = ts_dynarr_alloc(0, sizeof *arr);
13 * arr = ts_dynarr_push(arr, &x);
15 * arr = ts_dynarr_push(arr, &x);
17 * arr = ts_dynarr_push(arr, &x);
19 * for(i=0; i<ts_dynarr_size(arr); i++) {
20 * printf("%d\n", arr[i]);
22 * ts_dynarr_free(arr);
25 void *ts_dynarr_alloc(int elem, int szelem);
26 void ts_dynarr_free(void *da);
27 void *ts_dynarr_resize(void *da, int elem);
29 int ts_dynarr_empty(void *da);
30 int ts_dynarr_size(void *da);
32 void *ts_dynarr_clear(void *da);
35 void *ts_dynarr_push(void *da, void *item);
36 void *ts_dynarr_pop(void *da);
40 #define DYNARR_RESIZE(da, n) \
41 do { (da) = ts_dynarr_resize((da), (n)); } while(0)
42 #define DYNARR_CLEAR(da) \
43 do { (da) = ts_dynarr_clear(da); } while(0)
44 #define DYNARR_PUSH(da, item) \
45 do { (da) = ts_dynarr_push((da), (item)); } while(0)
46 #define DYNARR_POP(da) \
47 do { (da) = ts_dynarr_pop(da); } while(0)
49 /* utility macros to push characters to a string. assumes and maintains
50 * the invariant that the last element is always a zero
52 #define DYNARR_STRPUSH(da, c) \
54 char cnull = 0, ch = (char)(c); \
55 (da) = ts_dynarr_pop(da); \
56 (da) = ts_dynarr_push((da), &ch); \
57 (da) = ts_dynarr_push((da), &cnull); \
60 #define DYNARR_STRPOP(da) \
63 (da) = ts_dynarr_pop(da); \
64 (da) = ts_dynarr_pop(da); \
65 (da) = ts_dynarr_push((da), &cnull); \
69 #endif /* DYNARR_H_ */