From 55991c396cddc8ec27182dc5cfe64fdef61da064 Mon Sep 17 00:00:00 2001 From: Fabrice Date: Tue, 10 Feb 2026 10:17:45 +0100 Subject: turning hashset into hashmap --- src/hashm.h | 84 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 21 deletions(-) (limited to 'src/hashm.h') diff --git a/src/hashm.h b/src/hashm.h index 67bb3e0..fb83391 100644 --- a/src/hashm.h +++ b/src/hashm.h @@ -8,40 +8,82 @@ #define WAYC_HASHMAP_SEED 0 #define WAYC_HASHMAP_CAP 16 -template struct hashmap_s { - struct hashmap *inner; +template struct hashentry_s { + K key; + V value; }; -template -static inline i32 wayc_hashmap_hash(const void *item, uint64_t seed0, - uint64_t seed1) { +template +inline i32 wayc_hashentry_hash(const void *item, uint64_t seed0, + uint64_t seed1) { wayc_notnull(item); - return hashmap_xxhash3(item, sizeof(T), seed0, seed1); + + const hashentry_s *entry = (const hashentry_s *)item; + return hashmap_murmur(&entry->key, sizeof(K), seed0, seed1); } -template -static inline bool wayc_hashmap_compare(const void *a, const void *b, - void *udata) { +template +inline bool wayc_hashentry_compare(const void *a, const void *b, void *udata) { (void)udata; wayc_notnull(a); wayc_notnull(b); - return memcmp(a, b, sizeof(T)) == 0; + + const hashentry_s *entry_a = (const hashentry_s *)a; + const hashentry_s *entry_b = (const hashentry_s *)b; + return memcmp(&entry_a->key, &entry_b->key, sizeof(K)) == 0; } -template inline hashmap_s wayc_hashmap_init(void) { - struct hashmap *map = hashmap_new_with_allocator( - mi_malloc, mi_realloc, mi_free, sizeof(T), WAYC_HASHMAP_CAP, - WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED, wayc_hashmap_hash, - wayc_hashmap_compare, nullptr, nullptr); +template struct hashmap_s { + struct hashmap *inner; +}; - hashmap_s result; - result.inner = map; +template +inline void wayc_hashmap_init(hashmap_s *map) { + wayc_notnull(map); + memset(map, 0, sizeof(*map)); - return result; + struct hashmap *inner; + inner = hashmap_new_with_allocator( + mi_malloc, mi_realloc, mi_free, sizeof(hashentry_s), + WAYC_HASHMAP_CAP, WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED, + wayc_hashentry_hash, wayc_hashentry_compare, nullptr, + nullptr); + + wayc_notnull(inner); + + map->inner = inner; } -template inline void wayc_hashmap_deinit(hashmap_s *map) { +template +inline void wayc_hashmap_deinit(hashmap_s *map) { + wayc_notnull(map); hashmap_free(map->inner); - map->inner = nullptr; -} \ No newline at end of file +} + +template +inline V *wayc_hashmap_insert(hashmap_s *map, const K *key) { + wayc_notnull(map); + wayc_notnull(key); + + hashentry_s entry{}; + entry.key = *key; + auto *stored = (hashentry_s *)hashmap_set(map->inner, &entry); + + wayc_notnull(stored); + return &stored->value; +} + +template +inline V *wayc_hashmap_get(hashmap_s *map, const K *key) { + wayc_notnull(map); + wayc_notnull(key); + + hashentry_s entry{}; + entry.key = *key; + auto *stored = (hashentry_s *)hashmap_get(map->inner, &entry); + if (!stored) + return nullptr; + + return &stored->value; +} -- cgit v1.2.3