diff --git a/cig.h b/cig.h index 9fd6bc9..ca8b8c9 100644 --- a/cig.h +++ b/cig.h @@ -233,12 +233,12 @@ bool dyn_array_contains_eq_func(void *this, uint8_t *value, dyn_array_eq_fn eq); typedef int (*dyn_array_cmp_fn)(const void *a, const void *b); void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn); -// hashmaps //////////////////////////////////////////////////////////////////// +// maps //////////////////////////////////////////////////////////////////// typedef int (*hash_func_t)(void *key); typedef int (*equals_func_t)(void *key1, void *key2); -typedef struct hashmap_header { +typedef struct map_header { int n_items, capacity, itemsize, keysize, mapping_capacity; allocator_t allocator; hash_func_t hash; @@ -248,9 +248,9 @@ typedef struct hashmap_header { uint8_t bytes[1]; any_align_t _[1]; }; -} hashmap_header_t; +} map_header_t; -typedef struct hashmap_create_func_args { +typedef struct map_create_func_args { allocator_t allocator; const char *file; hash_func_t hash; // OPTIONAL @@ -259,12 +259,12 @@ typedef struct hashmap_create_func_args { int itemsize; int keysize; int line; -} hashmap_create_func_args_t; +} map_create_func_args_t; -void *hashmap_create_func(hashmap_create_func_args_t args); +void *map_create_func(map_create_func_args_t args); #define init_map(PTR, ...) do { \ - PTR = hashmap_create_func((hashmap_create_func_args_t) { \ + PTR = map_create_func((map_create_func_args_t) { \ .itemsize=sizeof(*PTR), \ .keysize=sizeof(PTR->key), \ .file=__FILE__, \ @@ -273,6 +273,9 @@ void *hashmap_create_func(hashmap_create_func_args_t args); }); \ } while(0); +int map_len(void *this); +int map_cap(void *this); + // CLI ///////////////////////////////////////////////////////////////////////// #define CLI_UNIQUE1 __macro_internal_34bba35b8b9b20a75f9881e3795630e25d36e620d9c9741e2e9141ba82ec6ef7__ @@ -478,6 +481,7 @@ void allocator_reset(allocator_t this) { #include "scanner.c" #include "string_builder.c" #include "file_io.c" +#include "map.c" #endif // CIG_IMPL diff --git a/hashmap.c b/map.c similarity index 64% rename from hashmap.c rename to map.c index d42b18e..67c1357 100644 --- a/hashmap.c +++ b/map.c @@ -4,8 +4,8 @@ static inline int mapping_cap(int capacity) { return capacity * 2; } -void *hashmap_create_func( - hashmap_create_func_args_t args +void *map_create_func( + map_create_func_args_t args ) { const size_t cap_of_index_arr = @@ -14,9 +14,9 @@ void *hashmap_create_func( const size_t SIZE = args.itemsize * args.initial_capacity; const size_t cap_of_items_arr = (SIZE + INC - 1) / INC; const size_t bytes = - sizeof(hashmap_header_t) + cap_of_items_arr + cap_of_index_arr; + sizeof(map_header_t) + cap_of_items_arr + cap_of_index_arr; - hashmap_header_t *header = + map_header_t *header = allocator_alloc_func( args.allocator, bytes, @@ -40,3 +40,22 @@ void *hashmap_create_func( return header->bytes; } + +void map_grow(void *this) { +} + +int map_len(void *this) { + if (this == NULL) { + return 0; + } + map_header_t *header = PTR_FROM_FIELD_PTR(map_header_t, bytes, this); + return header->n_items; +} + +int map_cap(void *this) { + if (this == NULL) { + return 0; + } + map_header_t *header = PTR_FROM_FIELD_PTR(map_header_t, bytes, this); + return header->capacity; +}