diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-29 14:07:04 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-29 14:07:04 -0400 |
commit | cfc66b3b567538b76c6170e3a86f0cabdd867b91 (patch) | |
tree | 36c359e218da15912399bba6e06dcff8583d4e59 /archive | |
parent | 1318715bdf9a7c7da86913094d9ad7ad52779333 (diff) |
made allocator; will switch to it soon
Diffstat (limited to 'archive')
-rw-r--r-- | archive/arr.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/archive/arr.c b/archive/arr.c new file mode 100644 index 0000000..2555d72 --- /dev/null +++ b/archive/arr.c @@ -0,0 +1,61 @@ +typedef struct { + void *data; + size_t len; + size_t cap; + size_t item_sz; +} Array; + +static void arr_create(Array *arr, size_t item_sz) { + arr->len = arr->cap = 0; + arr->item_sz = item_sz; + arr->data = NULL; +} + +static inline void arr_reserve(Array *arr, size_t n) { + arr->cap = n; + arr->data = err_realloc(arr->data, arr->item_sz * arr->cap); +} + +/* like arr_reserve, but sets the length of the array too */ +static inline void arr_set_len(Array *arr, size_t n) { + arr->len = arr->cap = n; + arr->data = err_realloc(arr->data, arr->item_sz * arr->cap); +} + +static inline void *arr_last(Array *arr) { + if (arr->data) + return (void*)((char*)arr->data + arr->item_sz * (arr->len - 1)); + else + return NULL; +} + +static void *arr_add(Array *arr) { + if (arr->len >= arr->cap) { + arr_reserve(arr, (arr->cap + 1) * 2); + } + arr->len++; + return (void*)((char*)arr->data + arr->item_sz * (arr->len - 1)); +} + +static void arr_clear(Array *arr) { + free(arr->data); + arr->len = arr->cap = 0; + arr->data = NULL; +} + +static void arr_remove_last(Array *arr) { + /* OPTIM (memory): Shorten array. */ + arr->len--; + if (!arr->len) { + arr_clear(arr); + } + +} + +static void arr_free(Array *arr) { + free(arr->data); +} + +/* NOTE: this will not work if type is a pointer! */ +#define arr_foreach(arr, type, var) for (type *var = (arr)->data, *var##_foreach_last = arr_last(arr); var; var == var##_foreach_last ? var = NULL : var++) +#define arr_foreach_reverse(arr, type, var) for (type *var = arr_last(arr); var; var == (arr)->data ? var = NULL : var--) |