From 8be1c2254b2e4a13364ec1e4088ec698cba859e6 Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Mon, 25 May 2026 21:11:46 +0200 Subject: [PATCH] some more tests --- cig.h | 3 ++- map.c | 8 ++++---- test_map_hash_key.c | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test_map_hash_key.c diff --git a/cig.h b/cig.h index b178392..3f6e88b 100644 --- a/cig.h +++ b/cig.h @@ -235,7 +235,7 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn); // maps //////////////////////////////////////////////////////////////////// -typedef unsigned int (*key_hash_func_t)(const void *key); +typedef uint32_t (*key_hash_func_t)(const void *key); typedef bool (*key_equals_func_t)(const void *key1, const void *key2); typedef struct map_header { @@ -323,6 +323,7 @@ unsigned int map_place(void *this, index_pair_t idx_pair); // Exists to take a hash of a key and modulo it so it fits within the maps // capacity. uint32_t map_mod_index(const map_header_t *header, uint32_t i); +uint32_t map_hash_key(const map_header_t *header, const uint8_t *key); // TODO: NOT DONE!!! FIRST try to get the existing in #define set_add(THIS, VALUE) do { \ diff --git a/map.c b/map.c index f1b69dd..546089d 100644 --- a/map.c +++ b/map.c @@ -133,11 +133,11 @@ unsigned int map_place(void *this, index_pair_t idx) { return header->mapping_arr[idx.new_i_into_mapping]; } -static uint32_t __map_hash_key_into_mapping_i(const map_header_t *header, const uint8_t *key) { +uint32_t map_hash_key(const map_header_t *header, const uint8_t *key) { if (header->hash != NULL) { - return header->hash(key) % header->mapping_capacity; + return map_mod_index(header, header->hash(key)); } else { - return fnv_1a(key, header->key_size) % header->mapping_capacity; + return map_mod_index(header, fnv_1a(key, header->key_size)); } } @@ -166,7 +166,7 @@ index_pair_t map_pair_hash(void *this, const uint8_t *pair) { const uint8_t *new_key = __cig_key_ptr_from_pair_ptr(header, pair); // find the initial index based on hash - uint32_t i_into_mapping = __map_hash_key_into_mapping_i(header, new_key); + uint32_t i_into_mapping = map_hash_key(header, new_key); int existing_i_into_bytes = header->mapping_arr[i_into_mapping]; if (existing_i_into_bytes == FLAG_FREE) { diff --git a/test_map_hash_key.c b/test_map_hash_key.c new file mode 100644 index 0000000..9ffcf57 --- /dev/null +++ b/test_map_hash_key.c @@ -0,0 +1,17 @@ +#include +#include "cig.h" + +#define KEYS_LEN 100 + +Test(map_hash_key, foo) { + map_header_t header = (map_header_t){0}; + long long key_hashes[KEYS_LEN]; + + for (long long i = 0; i < KEYS_LEN; i++) { + header.key_size = sizeof(i); + key_hashes[i] = map_hash_key(&header, (const uint8_t*)(&i)); + // TODO + } + + map_hash_key(&header) +}