From 91400197671812cc25fa406eef58f1ec1f8b48fd Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sat, 28 Feb 2026 00:05:18 +0100 Subject: [PATCH] hashmap init seems to be done missing any testing though --- cig.h | 16 ++-------------- hashmap.c | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/cig.h b/cig.h index ea372a1..9fd6bc9 100644 --- a/cig.h +++ b/cig.h @@ -238,26 +238,16 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn); typedef int (*hash_func_t)(void *key); typedef int (*equals_func_t)(void *key1, void *key2); -// TODO: after this comes the actual hash map. it maps keys to indexes into the -// key-value pair array. (which does not actually need values, only a key -// field.) -// TODO: This means that when allocating memory for the array of key-value -// pairs, we actually need to allocate it in terms of the max_align type, and -// increase in size by using that size as the smallest increment. Which will -// keep the hash array aligned properly in memory. - typedef struct hashmap_header { - int n_items, capacity, itemsize, keysize; + int n_items, capacity, itemsize, keysize, mapping_capacity; allocator_t allocator; hash_func_t hash; equals_func_t equals; + int *mapping_arr; union { uint8_t bytes[1]; any_align_t _[1]; }; - // The above union is actually a different size. Then comes an array of - // integers which is double the size of n_items * sizeof(int). int - // indexes[...]; } hashmap_header_t; typedef struct hashmap_create_func_args { @@ -283,8 +273,6 @@ void *hashmap_create_func(hashmap_create_func_args_t args); }); \ } while(0); -// TODO this shite - // CLI ///////////////////////////////////////////////////////////////////////// #define CLI_UNIQUE1 __macro_internal_34bba35b8b9b20a75f9881e3795630e25d36e620d9c9741e2e9141ba82ec6ef7__ diff --git a/hashmap.c b/hashmap.c index 0469efa..d42b18e 100644 --- a/hashmap.c +++ b/hashmap.c @@ -8,27 +8,35 @@ void *hashmap_create_func( hashmap_create_func_args_t args ) { - size_t size_of_index_arr = sizeof(int) * mapping_cap(args.initial_capacity); - size_t INC = ALIGN_OF(int); - size_t SIZE = args.itemsize * args.initial_capacity; - size_t size_of_items_arr = (SIZE + INC - 1) / INC; - size_t bytes = - sizeof(hashmap_header_t) + size_of_items_arr + size_of_index_arr; + const size_t cap_of_index_arr = + sizeof(int) * mapping_cap(args.initial_capacity); + const size_t INC = ALIGN_OF(int); + 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; - hashmap_header_t *header = - allocator_alloc_func(args.allocator, bytes, args.file, args.line); + hashmap_header_t *header = + allocator_alloc_func( + args.allocator, + bytes, + args.file, + args.line + ); - header->n_items = 0; - header->capacity = args.initial_capacity; - header->itemsize = args.itemsize; - header->keysize = args.keysize; - header->allocator = args.allocator; - header->hash = args.hash; - header->equals = args.equals; + header->n_items = 0; + header->capacity = args.initial_capacity; + header->itemsize = args.itemsize; + header->keysize = args.keysize; + header->allocator = args.allocator; + header->hash = args.hash; + header->equals = args.equals; + header->mapping_arr = (int*) &header->bytes[cap_of_items_arr]; + header->mapping_capacity = mapping_cap(args.initial_capacity); - return header->bytes; -} - -int *hashmap_get_mapping_arr(hashmap_header_t *this) { - // TODO + for ( int i = 0; i < header->mapping_capacity; i++ ) { + header->mapping_arr[i] = -1; + } + + return header->bytes; }