GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32)); struct StringHashKey { String string; u32 hash; operator String() const noexcept { return this->string; } operator String const &() const noexcept { return this->string; } }; gb_internal gb_inline u32 string_hash(String const &s) { u32 res = fnv32a(s.text, s.len) & 0x7fffffff; return res | (res == 0); } gb_internal gb_inline StringHashKey string_hash_string(String const &s) { StringHashKey hash_key = {}; hash_key.hash = string_hash(s); hash_key.string = s; return hash_key; } template struct StringMapEntry { String key; u32 hash; T value; }; template struct StringMap { StringMapEntry *entries; u32 count; u32 capacity; }; template gb_internal void string_map_init (StringMap *h, usize capacity = 16); template gb_internal void string_map_destroy (StringMap *h); template gb_internal T * string_map_get (StringMap *h, char const *key); template gb_internal T * string_map_get (StringMap *h, String const &key); template gb_internal T * string_map_get (StringMap *h, StringHashKey const &key); template gb_internal T & string_map_must_get(StringMap *h, char const *key); template gb_internal T & string_map_must_get(StringMap *h, String const &key); template gb_internal T & string_map_must_get(StringMap *h, StringHashKey const &key); template gb_internal void string_map_set (StringMap *h, char const *key, T const &value); template gb_internal void string_map_set (StringMap *h, String const &key, T const &value); template gb_internal void string_map_set (StringMap *h, StringHashKey const &key, T const &value); // template gb_internal void string_map_remove (StringMap *h, StringHashKey const &key); template gb_internal void string_map_clear (StringMap *h); template gb_internal void string_map_grow (StringMap *h); template gb_internal void string_map_reserve (StringMap *h, usize new_count); gb_internal gbAllocator string_map_allocator(void) { return heap_allocator(); } template gb_internal gb_inline void string_map_init(StringMap *h, usize capacity) { capacity = next_pow2_isize(capacity); string_map_reserve(h, capacity); } template gb_internal gb_inline void string_map_destroy(StringMap *h) { gb_free(string_map_allocator(), h->entries); } template gb_internal void string_map__insert(StringMap *h, u32 hash, String const &key, T const &value) { if (h->count+1 >= h->capacity) { string_map_grow(h); } GB_ASSERT(h->count+1 < h->capacity); u32 mask = h->capacity-1; MapIndex index = hash & mask; MapIndex original_index = index; do { auto *entry = h->entries+index; if (entry->hash == 0) { entry->key = key; entry->hash = hash; entry->value = value; h->count += 1; return; } index = (index+1)&mask; } while (index != original_index); GB_PANIC("Full map"); } template gb_internal b32 string_map__full(StringMap *h) { return 0.75f * h->count <= h->capacity; } template gb_inline void string_map_grow(StringMap *h) { isize new_capacity = gb_max(h->capacity<<1, 16); string_map_reserve(h, new_capacity); } template gb_internal void string_map_reserve(StringMap *h, usize cap) { if (cap < h->capacity) { return; } cap = next_pow2_isize(cap); StringMap new_h = {}; new_h.count = 0; new_h.capacity = cast(u32)cap; new_h.entries = gb_alloc_array(string_map_allocator(), StringMapEntry, new_h.capacity); if (h->count) { for (u32 i = 0; i < h->capacity; i++) { auto *entry = h->entries+i; if (entry->hash) { string_map__insert(&new_h, entry->hash, entry->key, entry->value); } } } string_map_destroy(h); *h = new_h; } template gb_internal T *string_map_get(StringMap *h, u32 hash, String const &key) { if (h->count == 0) { return nullptr; } u32 mask = (h->capacity-1); u32 index = hash & mask; u32 original_index = index; do { auto *entry = h->entries+index; u32 curr_hash = entry->hash; if (curr_hash == 0) { // NOTE(bill): no found, but there isn't any key removal for this hash map return nullptr; } else if (curr_hash == hash && entry->key == key) { return &entry->value; } index = (index+1) & mask; } while (original_index != index); return nullptr; } template gb_internal gb_inline T *string_map_get(StringMap *h, StringHashKey const &key) { return string_map_get(h, key.hash, key.string); } template gb_internal gb_inline T *string_map_get(StringMap *h, String const &key) { return string_map_get(h, string_hash(key), key); } template gb_internal gb_inline T *string_map_get(StringMap *h, char const *key) { String k = make_string_c(key); return string_map_get(h, string_hash(k), k); } template gb_internal T &string_map_must_get(StringMap *h, u32 hash, String const &key) { T *found = string_map_get(h, hash, key); GB_ASSERT(found != nullptr); return *found; } template gb_internal T &string_map_must_get(StringMap *h, StringHashKey const &key) { return string_map_must_get(h, key.hash, key.string); } template gb_internal gb_inline T &string_map_must_get(StringMap *h, String const &key) { return string_map_must_get(h, string_hash(key), key); } template gb_internal gb_inline T &string_map_must_get(StringMap *h, char const *key) { String k = make_string_c(key); return string_map_must_get(h, string_hash(k), k); } template gb_internal void string_map_set(StringMap *h, u32 hash, String const &key, T const &value) { if (h->count == 0) { string_map_grow(h); } auto *found = string_map_get(h, hash, key); if (found) { *found = value; return; } string_map__insert(h, hash, key, value); } template gb_internal gb_inline void string_map_set(StringMap *h, String const &key, T const &value) { string_map_set(h, string_hash_string(key), value); } template gb_internal gb_inline void string_map_set(StringMap *h, char const *key, T const &value) { string_map_set(h, string_hash_string(make_string_c(key)), value); } template gb_internal gb_inline void string_map_set(StringMap *h, StringHashKey const &key, T const &value) { string_map_set(h, key.hash, key.string, value); } template gb_internal gb_inline void string_map_clear(StringMap *h) { h->count = 0; gb_zero_array(h->entries, h->capacity); } template struct StringMapIterator { StringMap *map; MapIndex index; StringMapIterator &operator++() noexcept { for (;;) { ++index; if (map->capacity == index) { return *this; } StringMapEntry *entry = map->entries+index; if (entry->hash != 0) { return *this; } } } bool operator==(StringMapIterator const &other) const noexcept { return this->map == other->map && this->index == other->index; } operator StringMapEntry *() const { return map->entries+index; } }; template gb_internal StringMapIterator end(StringMap &m) noexcept { return StringMapIterator{&m, m.capacity}; } template gb_internal StringMapIterator const end(StringMap const &m) noexcept { return StringMapIterator{&m, m.capacity}; } template gb_internal StringMapIterator begin(StringMap &m) noexcept { if (m.count == 0) { return end(m); } MapIndex index = 0; while (index < m.capacity) { if (m.entries[index].hash) { break; } index++; } return StringMapIterator{&m, index}; } template gb_internal StringMapIterator const begin(StringMap const &m) noexcept { if (m.count == 0) { return end(m); } MapIndex index = 0; while (index < m.capacity) { if (m.entries[index].hash) { break; } index++; } return StringMapIterator{&m, index}; }