123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- struct StringHashKey {
- u32 hash;
- String string;
- };
- gb_internal gb_inline StringHashKey string_hash_string(String const &s) {
- StringHashKey hash_key = {};
- hash_key.hash = fnv32a(s.text, s.len);
- hash_key.string = s;
- return hash_key;
- }
- gb_internal gb_inline bool string_hash_key_equal(StringHashKey const &a, StringHashKey const &b) {
- if (a.hash == b.hash) {
- // NOTE(bill): If two string's hashes collide, compare the strings themselves
- return a.string == b.string;
- }
- return false;
- }
- template <typename T>
- struct StringMapEntry {
- StringHashKey key;
- MapIndex next;
- T value;
- };
- template <typename T>
- struct StringMap {
- using K = String;
- using V = T;
- Slice<MapIndex> hashes;
- Array<StringMapEntry<T> > entries;
- };
- template <typename T> gb_internal void string_map_init (StringMap<T> *h, isize capacity = 16);
- template <typename T> gb_internal void string_map_destroy (StringMap<T> *h);
- template <typename T> gb_internal T * string_map_get (StringMap<T> *h, char const *key);
- template <typename T> gb_internal T * string_map_get (StringMap<T> *h, String const &key);
- template <typename T> gb_internal T * string_map_get (StringMap<T> *h, StringHashKey const &key);
- template <typename T> gb_internal T & string_map_must_get (StringMap<T> *h, char const *key);
- template <typename T> gb_internal T & string_map_must_get (StringMap<T> *h, String const &key);
- template <typename T> gb_internal T & string_map_must_get (StringMap<T> *h, StringHashKey const &key);
- template <typename T> gb_internal void string_map_set (StringMap<T> *h, StringHashKey const &key, T const &value);
- template <typename T> gb_internal void string_map_set (StringMap<T> *h, String const &key, T const &value);
- template <typename T> gb_internal void string_map_set (StringMap<T> *h, char const *key, T const &value);
- template <typename T> gb_internal void string_map_remove (StringMap<T> *h, StringHashKey const &key);
- template <typename T> gb_internal void string_map_clear (StringMap<T> *h);
- template <typename T> gb_internal void string_map_grow (StringMap<T> *h);
- template <typename T> gb_internal void string_map_rehash (StringMap<T> *h, isize new_count);
- template <typename T> gb_internal void string_map_reserve (StringMap<T> *h, isize cap);
- gb_internal gbAllocator string_map_allocator(void) {
- return heap_allocator();
- }
- template <typename T>
- gb_internal gb_inline void string_map_init(StringMap<T> *h, isize capacity) {
- capacity = next_pow2_isize(capacity);
- slice_init(&h->hashes, string_map_allocator(), capacity);
- array_init(&h->entries, string_map_allocator(), 0, capacity);
- for (isize i = 0; i < capacity; i++) {
- h->hashes.data[i] = MAP_SENTINEL;
- }
- }
- template <typename T>
- gb_internal gb_inline void string_map_destroy(StringMap<T> *h) {
- if (h->entries.allocator.proc == nullptr) {
- h->entries.allocator = string_map_allocator();
- }
- slice_free(&h->hashes, h->entries.allocator);
- array_free(&h->entries);
- }
- template <typename T>
- gb_internal MapIndex string_map__add_entry(StringMap<T> *h, StringHashKey const &key) {
- StringMapEntry<T> e = {};
- e.key = key;
- e.next = MAP_SENTINEL;
- array_add(&h->entries, e);
- return cast(MapIndex)(h->entries.count-1);
- }
- template <typename T>
- gb_internal MapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) {
- MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
- if (h->hashes.count != 0) {
- fr.hash_index = cast(MapIndex)(key.hash & (h->hashes.count-1));
- fr.entry_index = h->hashes.data[fr.hash_index];
- while (fr.entry_index != MAP_SENTINEL) {
- if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
- return fr;
- }
- fr.entry_prev = fr.entry_index;
- fr.entry_index = h->entries.data[fr.entry_index].next;
- }
- }
- return fr;
- }
- template <typename T>
- gb_internal MapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
- MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
- if (h->hashes.count != 0) {
- fr.hash_index = cast(MapIndex)(e->key.hash & (h->hashes.count-1));
- fr.entry_index = h->hashes.data[fr.hash_index];
- while (fr.entry_index != MAP_SENTINEL) {
- if (&h->entries.data[fr.entry_index] == e) {
- return fr;
- }
- fr.entry_prev = fr.entry_index;
- fr.entry_index = h->entries.data[fr.entry_index].next;
- }
- }
- return fr;
- }
- template <typename T>
- gb_internal b32 string_map__full(StringMap<T> *h) {
- return 0.75f * h->hashes.count <= h->entries.count;
- }
- template <typename T>
- gb_inline void string_map_grow(StringMap<T> *h) {
- isize new_count = gb_max(h->hashes.count<<1, 16);
- string_map_rehash(h, new_count);
- }
- template <typename T>
- gb_internal void string_map_reset_entries(StringMap<T> *h) {
- for (isize i = 0; i < h->hashes.count; i++) {
- h->hashes.data[i] = MAP_SENTINEL;
- }
- for (isize i = 0; i < h->entries.count; i++) {
- MapFindResult fr;
- StringMapEntry<T> *e = &h->entries.data[i];
- e->next = MAP_SENTINEL;
- fr = string_map__find_from_entry(h, e);
- if (fr.entry_prev == MAP_SENTINEL) {
- h->hashes[fr.hash_index] = cast(MapIndex)i;
- } else {
- h->entries[fr.entry_prev].next = cast(MapIndex)i;
- }
- }
- }
- template <typename T>
- gb_internal void string_map_reserve(StringMap<T> *h, isize cap) {
- if (h->entries.allocator.proc == nullptr) {
- h->entries.allocator = string_map_allocator();
- }
- array_reserve(&h->entries, cap);
- if (h->entries.count*2 < h->hashes.count) {
- return;
- }
- slice_resize(&h->hashes, h->entries.allocator, cap*2);
- string_map_reset_entries(h);
- }
- template <typename T>
- gb_internal void string_map_rehash(StringMap<T> *h, isize new_count) {
- string_map_reserve(h, new_count);
- }
- template <typename T>
- gb_internal T *string_map_get(StringMap<T> *h, StringHashKey const &key) {
- isize index = string_map__find(h, key).entry_index;
- if (index != MAP_SENTINEL) {
- return &h->entries.data[index].value;
- }
- return nullptr;
- }
- template <typename T>
- gb_internal gb_inline T *string_map_get(StringMap<T> *h, String const &key) {
- return string_map_get(h, string_hash_string(key));
- }
- template <typename T>
- gb_internal gb_inline T *string_map_get(StringMap<T> *h, char const *key) {
- return string_map_get(h, string_hash_string(make_string_c(key)));
- }
- template <typename T>
- gb_internal T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) {
- isize index = string_map__find(h, key).entry_index;
- GB_ASSERT(index != MAP_SENTINEL);
- return h->entries.data[index].value;
- }
- template <typename T>
- gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, String const &key) {
- return string_map_must_get(h, string_hash_string(key));
- }
- template <typename T>
- gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, char const *key) {
- return string_map_must_get(h, string_hash_string(make_string_c(key)));
- }
- template <typename T>
- gb_internal void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) {
- MapIndex index;
- MapFindResult fr;
- if (h->hashes.count == 0) {
- string_map_grow(h);
- }
- fr = string_map__find(h, key);
- if (fr.entry_index != MAP_SENTINEL) {
- index = fr.entry_index;
- } else {
- index = string_map__add_entry(h, key);
- if (fr.entry_prev != MAP_SENTINEL) {
- h->entries.data[fr.entry_prev].next = index;
- } else {
- h->hashes.data[fr.hash_index] = index;
- }
- }
- h->entries.data[index].value = value;
- if (string_map__full(h)) {
- string_map_grow(h);
- }
- }
- template <typename T>
- gb_internal gb_inline void string_map_set(StringMap<T> *h, String const &key, T const &value) {
- string_map_set(h, string_hash_string(key), value);
- }
- template <typename T>
- gb_internal gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value) {
- string_map_set(h, string_hash_string(make_string_c(key)), value);
- }
- template <typename T>
- gb_internal void string_map__erase(StringMap<T> *h, MapFindResult const &fr) {
- MapFindResult last;
- if (fr.entry_prev == MAP_SENTINEL) {
- h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
- } else {
- h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
- }
- if (fr.entry_index == h->entries.count-1) {
- array_pop(&h->entries);
- return;
- }
- h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1];
- last = string_map__find(h, h->entries.data[fr.entry_index].key);
- if (last.entry_prev != MAP_SENTINEL) {
- h->entries.data[last.entry_prev].next = fr.entry_index;
- } else {
- h->hashes.data[last.hash_index] = fr.entry_index;
- }
- }
- template <typename T>
- gb_internal void string_map_remove(StringMap<T> *h, StringHashKey const &key) {
- MapFindResult fr = string_map__find(h, key);
- if (fr.entry_index != MAP_SENTINEL) {
- string_map__erase(h, fr);
- }
- }
- template <typename T>
- gb_internal gb_inline void string_map_clear(StringMap<T> *h) {
- array_clear(&h->entries);
- for (isize i = 0; i < h->hashes.count; i++) {
- h->hashes.data[i] = MAP_SENTINEL;
- }
- }
- template <typename T>
- gb_internal StringMapEntry<T> *begin(StringMap<T> &m) {
- return m.entries.data;
- }
- template <typename T>
- gb_internal StringMapEntry<T> const *begin(StringMap<T> const &m) {
- return m.entries.data;
- }
- template <typename T>
- gb_internal StringMapEntry<T> *end(StringMap<T> &m) {
- return m.entries.data + m.entries.count;
- }
- template <typename T>
- gb_internal StringMapEntry<T> const *end(StringMap<T> const &m) {
- return m.entries.data + m.entries.count;
- }
|