diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-07-14 21:12:37 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-07-14 21:12:37 +0200 |
commit | 30740ca4131d1f574718262451b4410207dc8d4e (patch) | |
tree | fc88b16a216079397ad85b9c6b1a1c1c5712a814 /kit/dynamic_array.h | |
parent | 5e3c99bb1cf1d03ea006300121265571f5008fd2 (diff) | |
download | saw-30740ca4131d1f574718262451b4410207dc8d4e.zip |
Reworking the build system
Diffstat (limited to 'kit/dynamic_array.h')
-rw-r--r-- | kit/dynamic_array.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/kit/dynamic_array.h b/kit/dynamic_array.h new file mode 100644 index 0000000..571f344 --- /dev/null +++ b/kit/dynamic_array.h @@ -0,0 +1,126 @@ +#ifndef KIT_DYNAMIC_ARRAY_H +#define KIT_DYNAMIC_ARRAY_H + +#include "allocator.h" + +#include <string.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + i64 capacity; + i64 size; + void *values; + kit_allocator_t *alloc; +} kit_da_void_t; + +void kit_da_init(kit_da_void_t *array, i64 element_size, i64 size, + kit_allocator_t *alloc); + +void kit_da_resize(kit_da_void_t *array, i64 element_size, i64 size); + +void kit_da_resize_exact(kit_da_void_t *array, i64 element_size, + i64 size); + +/* Dynamic array type definition. + */ +#define KIT_DA(element_type_) \ + struct { \ + i64 capacity; \ + i64 size; \ + element_type_ *values; \ + kit_allocator_t *alloc; \ + } + +/* Initialize dynamic array. + */ +#define KIT_DA_INIT(array_, size_, alloc_) \ + kit_da_init((kit_da_void_t *) &(array_), \ + sizeof((array_).values[0]), (size_), (alloc_)) + +/* Declare and initialize dynamic array. + */ +#define KIT_DA_CREATE(name_, element_type_, size_) \ + KIT_DA(element_type_) name_; \ + KIT_DA_INIT(name_, (size_), NULL) + +/* Destroy dynamic array. + */ +#define KIT_DA_DESTROY(array_) \ + do { \ + if ((array_).values != NULL) \ + kit_alloc_dispatch((array_).alloc, KIT_DEALLOCATE, 0, \ + (array_).capacity * \ + sizeof((array_).values[0]), \ + (array_).values); \ + memset(&(array_), 0, sizeof(array_)); \ + } while (0) + +/* Resize dynamic array. + */ +#define KIT_DA_RESIZE(array_, size_) \ + kit_da_resize((kit_da_void_t *) &(array_), \ + sizeof((array_).values[0]), size_) + +/* Resize dynamic array with exact capacity. + */ +#define KIT_DA_RESIZE_EXACT(array_, capacity_) \ + kit_da_resize_exact((kit_da_void_t *) &(array_), \ + sizeof((array_).values[0]), capacity_) + +/* Append a value to dynamic array. + */ +#define KIT_DA_APPEND(array_, value_) \ + do { \ + i64 kit_index_back_ = (array_).size; \ + KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ + if (kit_index_back_ < (array_).size) \ + (array_).values[kit_index_back_] = (value_); \ + } while (0) + +/* Insert a value into dynamic array. + */ +#define KIT_DA_INSERT(array_, index_, value_) \ + do { \ + i64 kit_i_; \ + i64 kit_index_back_ = (array_).size; \ + i64 kit_indert_n_ = (index_); \ + KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ + if (kit_index_back_ + 1 == (array_).size) { \ + for (kit_i_ = kit_index_back_; kit_i_ > kit_indert_n_; \ + kit_i_--) \ + (array_).values[kit_i_] = (array_).values[kit_i_ - 1]; \ + (array_).values[kit_indert_n_] = (value_); \ + } \ + } while (0) + +/* Erase a value from dynamic array. + */ +#define KIT_DA_ERASE(array_, index_) \ + do { \ + i64 i_; \ + for (i_ = (index_) + 1; i_ < (array_).size; i_++) \ + (array_).values[i_ - 1] = (array_).values[i_]; \ + KIT_DA_RESIZE((array_), (array_).size - 1); \ + } while (0) + +#ifdef __cplusplus +} +#endif + +#define da_void_t kit_da_void_t +#define da_init kit_da_init +#define da_resize kit_da_resize +#define DA KIT_DA +#define DA_INIT KIT_DA_INIT +#define DA_CREATE KIT_DA_CREATE +#define DA_DESTROY KIT_DA_DESTROY +#define DA_RESIZE KIT_DA_RESIZE +#define DA_RESIZE_EXACT KIT_DA_RESIZE_EXACT +#define DA_APPEND KIT_DA_APPEND +#define DA_INSERT KIT_DA_INSERT +#define DA_ERASE KIT_DA_ERASE + +#endif |