diff options
Diffstat (limited to 'common/dyn_arr.h')
| -rw-r--r-- | common/dyn_arr.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/common/dyn_arr.h b/common/dyn_arr.h new file mode 100644 index 0000000..85e6c15 --- /dev/null +++ b/common/dyn_arr.h @@ -0,0 +1,33 @@ +#ifndef __MICRO_VEC_H__ +#define __MICRO_VEC_H__ + +#include "common.h" + +#define DA_MIN_CAP 8 +#define DA_GROW_RATE 2 + +#define da_create(T, init_capacity) \ + ((T*)_da_create(sizeof(T), init_capacity)) + +#define da_len_ptr(arr) ((int*)arr - 2) +#define da_len(arr) (*da_len_ptr(arr)) + +#define da_cap_ptr(arr) ((int*)arr - 1) +#define da_cap(arr) (*da_cap_ptr(arr)) + +#define da_base(arr) ((void*)da_len_ptr(arr)) + +#define da_append_slot(T, arr) \ + ((T*)_da_append_slot(sizeof(T), (void**)arr)) + +#define da_append(T, arr, elem) \ + (*da_append_slot(T, arr) = elem) + +#define da_clear(arr) \ + (*da_len_ptr(arr) = 0) + +void *_da_create(size_t type_size, int init_cap); +void *_da_append_slot(size_t type_size, void **arr); +void da_free(void *arr); + +#endif // __MICRO_VEC_H__ |
