#pragma once #include #include #include "hashmap.h" #include "utils.h" #define WAYC_HASHMAP_SEED 0 #define WAYC_HASHMAP_CAP 16 template struct hashentry_s { K key; V value; }; template inline u64 wayc_hashentry_hash(const void* item, uint64_t seed0, uint64_t seed1) { wayc_notnull(item); const hashentry_s* entry = (const hashentry_s*)item; return hashmap_murmur(&entry->key, sizeof(K), seed0, seed1); } template inline i32 wayc_hashentry_compare(const void* a, const void* b, void* udata) { (void)udata; wayc_notnull(a); wayc_notnull(b); 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)); } template struct hashmap_s { struct hashmap* inner; }; template inline void wayc_hashmap_init(hashmap_s* map) { wayc_notnull(map); memset(map, 0, sizeof(*map)); 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) { wayc_notnull(map); hashmap_free(map->inner); } template inline V* wayc_hashmap_insert(hashmap_s* map, const K* key, const V* value) { wayc_notnull(map); wayc_notnull(key); wayc_notnull(value); hashentry_s entry{}; entry.key = *key; entry.value = *value; hashentry_s* stored = (hashentry_s*)hashmap_set(map->inner, &entry); 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; } template inline void wayc_hashmap_remove(hashmap_s* map, const K* key) { wayc_notnull(map); wayc_notnull(key); hashentry_s entry{}; entry.key = *key; hashmap_delete(map->inner, &entry); } template inline usize wayc_hashmap_count(hashmap_s* map) { wayc_notnull(map); return hashmap_count(map->inner); }