From 2d6c8fec45b23a8a28668ecf3ef281139ab778a7 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Fri, 29 Dec 2023 06:21:33 +0100 Subject: refactor dependencies; include dependencies source code --- source/kit/dynamic_array.h | 129 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 source/kit/dynamic_array.h (limited to 'source/kit/dynamic_array.h') diff --git a/source/kit/dynamic_array.h b/source/kit/dynamic_array.h new file mode 100644 index 0000000..e868917 --- /dev/null +++ b/source/kit/dynamic_array.h @@ -0,0 +1,129 @@ +#ifndef KIT_DYNAMIC_ARRAY_H +#define KIT_DYNAMIC_ARRAY_H + +#include "allocator.h" + +#include + +#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 + +#ifndef KIT_DISABLE_SHORT_NAMES +# 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 + +#endif -- cgit v1.2.3