67#ifndef CARRAY_INITIAL_SIZE
68#define CARRAY_INITIAL_SIZE 4
72#define CARRAY_RESIZE(current_size) \
77#ifndef CARRAY_STACKFUL
78#define __carray_init(carray, length, _type, _compare, _free) \
80 (carray)->realsize = length; \
82 (carray)->array = calloc(length, sizeof(_type)); \
85#define carray_init(carray, settings) \
86 __carray_init(carray, CARRAY_INITIAL_SIZE, settings)
88#define carray_init(carray, length, block) \
90 carray.realsize = length; \
92 carray.array = block; \
97#ifndef CARRAY_STACKFUL
98#define __carray_insert_handle_full(carray, index, value) \
99 (carray)->realsize = CARRAY_RESIZE((carray)->realsize); \
100 (carray)->array = realloc((carray)->array, sizeof(*(carray)->array) * (size_t) (carray)->realsize)
102#define __carray_insert_handle_full(carray, index, value) \
103 fprintf(stderr, "carray_insert: attempt to insert value '%s' into full array '%s'. (%s:%i)\n", #value, #carray, __FILE__, __LINE__); \
107#define carray_insert(carray, index, value) \
108 if((carray)->size == (carray)->realsize) { \
109 __carray_insert_handle_full(carray, index, value); \
112 if(index < 0 || index > (carray)->size) { \
113 fprintf(stderr, "carray_insert: attempt to insert at index %i, out of bounds of array '%s'. (%s:%i)\n", index, #carray, __FILE__, __LINE__); \
114 exit(EXIT_FAILURE); \
117 memmove((carray)->array + index + 1, (carray)->array + index, sizeof(*(carray)->array) * (size_t) ((carray)->size - index)); \
118 (carray)->array[index] = value; \
122#define carray_pop(carray, index, location) \
125 if(index < 0 || index >= (carray)->size) { \
126 fprintf(stderr, "carray_pop: attempt to pop index %i, out of bounds of array '%s'. (%s:%i)\n", index, #carray, __FILE__, __LINE__); \
127 exit(EXIT_FAILURE); \
131 (location) = (carray)->array[(index)]; \
132 memmove((carray)->array + index, (carray)->array + index + 1, sizeof(*(carray)->array) * (size_t) ((carray)->size - index))
135#define __carray_remove(carray, value, _type, _compare, _free) \
137 int __CARRAY_ITER_INDEX = 0; \
139 for(__CARRAY_ITER_INDEX = 0; __CARRAY_ITER_INDEX < (carray)->size; __CARRAY_ITER_INDEX++) { \
140 _type __CARRAY_OPERAND_A = (carray)->array[__CARRAY_ITER_INDEX]; \
141 _type __CARRAY_OPERAND_B = value; \
143 if((_compare) == 0) \
148 memmove((carray)->array + __CARRAY_ITER_INDEX, \
149 (carray)->array + __CARRAY_ITER_INDEX + 1, \
150 sizeof(*(carray)->array) * (size_t) ((carray)->size - __CARRAY_ITER_INDEX)); \
152 __CARRAY_ITER_INDEX = -1; \
156 if(__CARRAY_ITER_INDEX != -1) { \
157 fprintf(stderr, "carray_remove: attempt to remove value '%s' that is not in array '%s'. (%s:%i)\n", #value, #carray, __FILE__, __LINE__); \
158 exit(EXIT_FAILURE); \
162#define carray_remove(carray, value, settings) \
163 __carray_remove(carray, value, settings)
166#define __carray_find(carray, value, location, _type, _compare, _free) \
170 int __CARRAY_ITER_INDEX = 0; \
173 for(__CARRAY_ITER_INDEX = 0; __CARRAY_ITER_INDEX < (carray)->size; __CARRAY_ITER_INDEX++) { \
174 _type __CARRAY_OPERAND_A = (carray)->array[__CARRAY_ITER_INDEX]; \
175 _type __CARRAY_OPERAND_B = value; \
177 if((_compare) == 0) \
180 location = __CARRAY_ITER_INDEX; \
186#define carray_find(carray, value, location, settings) \
187 __carray_find(carray, value, location, settings)
189#ifndef CARRAY_STACKFUL
190#define __carray_free_array(carray) free((carray)->array);
192#define __carray_free_array(carray)
196#define __carray_free(carray, _type, _compare, _free) \
198 int __CARRAY_ITER_INDEX = 0; \
200 for(__CARRAY_ITER_INDEX = 0; __CARRAY_ITER_INDEX < (carray)->size; __CARRAY_ITER_INDEX++) { \
201 _type __CARRAY_OPERAND_A = (carray)->array[__CARRAY_ITER_INDEX]; \
202 (void) __CARRAY_OPERAND_A; \
207 __carray_free_array(carray); \
210#define carray_free(carray, settings) \
211 __carray_free(carray, settings)
214#ifndef CARRAY_STACKFUL
215#define __carray_append_handle_full(carray, value) \
216 (carray)->realsize = CARRAY_RESIZE((carray)->realsize); \
217 (carray)->array = realloc((carray)->array, sizeof(*(carray)->array) * (size_t) (carray)->realsize)
219#define __carray_append_handle_full(carray, value) \
220 fprintf(stderr, "carray_append: attempt to append value '%s' into full array '%s'. (%s:%i)\n", #value, #carray, __FILE__, __LINE__); \
224#define carray_append(carray, value) \
225 if((carray)->size == (carray)->realsize) { \
226 __carray_append_handle_full(carray, value); \
229 (carray)->array[(carray)->size] = value; \