Improve performance of the compiler hash table types and unify behaviour

This commit is contained in:
gingerBill
2021-11-05 12:42:19 +00:00
parent 0010e882a7
commit 439fc86740
3 changed files with 155 additions and 120 deletions
+53 -47
View File
@@ -7,12 +7,17 @@
#ifndef MAP_UTIL_STUFF #ifndef MAP_UTIL_STUFF
#define MAP_UTIL_STUFF #define MAP_UTIL_STUFF
// NOTE(bill): This util stuff is the same for every `Map` // NOTE(bill): This util stuff is the same for every `Map`
typedef isize MapIndex;
struct MapFindResult { struct MapFindResult {
isize hash_index; MapIndex hash_index;
isize entry_prev; MapIndex entry_prev;
isize entry_index; MapIndex entry_index;
}; };
enum : MapIndex { MAP_SENTINEL = ~(MapIndex)0 };
struct HashKey { struct HashKey {
u64 key; u64 key;
@@ -73,6 +78,7 @@ template <typename T> void map_remove (Map<T> *h, HashKey const &key);
template <typename T> void map_clear (Map<T> *h); template <typename T> void map_clear (Map<T> *h);
template <typename T> void map_grow (Map<T> *h); template <typename T> void map_grow (Map<T> *h);
template <typename T> void map_rehash (Map<T> *h, isize new_count); template <typename T> void map_rehash (Map<T> *h, isize new_count);
template <typename T> void map_reserve (Map<T> *h, isize cap);
#if MAP_ENABLE_MULTI_MAP #if MAP_ENABLE_MULTI_MAP
// Mutlivalued map procedure // Mutlivalued map procedure
@@ -92,7 +98,7 @@ gb_inline void map_init(Map<T> *h, gbAllocator a, isize capacity) {
slice_init(&h->hashes, a, capacity); slice_init(&h->hashes, a, capacity);
array_init(&h->entries, a, 0, capacity); array_init(&h->entries, a, 0, capacity);
for (isize i = 0; i < capacity; i++) { for (isize i = 0; i < capacity; i++) {
h->hashes.data[i] = -1; h->hashes.data[i] = MAP_SENTINEL;
} }
} }
@@ -106,20 +112,20 @@ template <typename T>
gb_internal isize map__add_entry(Map<T> *h, HashKey const &key) { gb_internal isize map__add_entry(Map<T> *h, HashKey const &key) {
MapEntry<T> e = {}; MapEntry<T> e = {};
e.key = key; e.key = key;
e.next = -1; e.next = MAP_SENTINEL;
array_add(&h->entries, e); array_add(&h->entries, e);
return h->entries.count-1; return h->entries.count-1;
} }
template <typename T> template <typename T>
gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) { gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) {
MapFindResult fr = {-1, -1, -1}; MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
if (h->hashes.count == 0) { if (h->hashes.count == 0) {
return fr; return fr;
} }
fr.hash_index = key.key & (h->hashes.count-1); fr.hash_index = key.key & (h->hashes.count-1);
fr.entry_index = h->hashes.data[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index != MAP_SENTINEL) {
if (hash_key_equal(h->entries.data[fr.entry_index].key, key)) { if (hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
return fr; return fr;
} }
@@ -131,13 +137,13 @@ gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) {
template <typename T> template <typename T>
gb_internal MapFindResult map__find_from_entry(Map<T> *h, MapEntry<T> *e) { gb_internal MapFindResult map__find_from_entry(Map<T> *h, MapEntry<T> *e) {
MapFindResult fr = {-1, -1, -1}; MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
if (h->hashes.count == 0) { if (h->hashes.count == 0) {
return fr; return fr;
} }
fr.hash_index = e->key.key & (h->hashes.count-1); fr.hash_index = e->key.key & (h->hashes.count-1);
fr.entry_index = h->hashes.data[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index != MAP_SENTINEL) {
if (&h->entries.data[fr.entry_index] == e) { if (&h->entries.data[fr.entry_index] == e) {
return fr; return fr;
} }
@@ -159,44 +165,44 @@ gb_inline void map_grow(Map<T> *h) {
} }
template <typename T> template <typename T>
void map_rehash(Map<T> *h, isize new_count) { void map_reset_entries(Map<T> *h) {
isize i, j; isize i;
Map<T> nh = {}; for (i = 0; i < h->hashes.count; i++) {
new_count = next_pow2_isize(new_count); h->hashes.data[i] = MAP_SENTINEL;
nh.hashes = h->hashes;
nh.entries.allocator = h->entries.allocator;
slice_resize(&nh.hashes, h->entries.allocator, new_count);
for (i = 0; i < new_count; i++) {
nh.hashes.data[i] = -1;
} }
array_reserve(&nh.entries, ARRAY_GROW_FORMULA(h->entries.count));
for (i = 0; i < h->entries.count; i++) { for (i = 0; i < h->entries.count; i++) {
MapEntry<T> *e = &h->entries.data[i];
MapFindResult fr; MapFindResult fr;
if (nh.hashes.count == 0) { MapEntry<T> *e = &h->entries.data[i];
map_grow(&nh); e->next = MAP_SENTINEL;
} fr = map__find_from_entry(h, e);
fr = map__find(&nh, e->key); if (fr.entry_prev == MAP_SENTINEL) {
j = map__add_entry(&nh, e->key); h->hashes[fr.hash_index] = i;
if (fr.entry_prev < 0) {
nh.hashes.data[fr.hash_index] = j;
} else { } else {
nh.entries.data[fr.entry_prev].next = j; h->entries[fr.entry_prev].next = i;
}
nh.entries.data[j].next = fr.entry_index;
nh.entries.data[j].value = e->value;
if (map__full(&nh)) {
map_grow(&nh);
} }
} }
array_free(&h->entries); }
*h = nh;
template <typename T>
void map_reserve(Map<T> *h, isize cap) {
array_reserve(&h->entries, cap);
if (h->entries.count*2 < h->hashes.count) {
return;
}
slice_resize(&h->hashes, h->entries.allocator, cap*2);
map_reset_entries(h);
}
template <typename T>
void map_rehash(Map<T> *h, isize new_count) {
map_reserve(h, new_count);
} }
template <typename T> template <typename T>
T *map_get(Map<T> *h, HashKey const &key) { T *map_get(Map<T> *h, HashKey const &key) {
isize index = map__find(h, key).entry_index; isize index = map__find(h, key).entry_index;
if (index >= 0) { if (index != MAP_SENTINEL) {
return &h->entries.data[index].value; return &h->entries.data[index].value;
} }
return nullptr; return nullptr;
@@ -205,7 +211,7 @@ T *map_get(Map<T> *h, HashKey const &key) {
template <typename T> template <typename T>
T &map_must_get(Map<T> *h, HashKey const &key) { T &map_must_get(Map<T> *h, HashKey const &key) {
isize index = map__find(h, key).entry_index; isize index = map__find(h, key).entry_index;
GB_ASSERT(index >= 0); GB_ASSERT(index != MAP_SENTINEL);
return h->entries.data[index].value; return h->entries.data[index].value;
} }
@@ -217,11 +223,11 @@ void map_set(Map<T> *h, HashKey const &key, T const &value) {
map_grow(h); map_grow(h);
} }
fr = map__find(h, key); fr = map__find(h, key);
if (fr.entry_index >= 0) { if (fr.entry_index != MAP_SENTINEL) {
index = fr.entry_index; index = fr.entry_index;
} else { } else {
index = map__add_entry(h, key); index = map__add_entry(h, key);
if (fr.entry_prev >= 0) { if (fr.entry_prev != MAP_SENTINEL) {
h->entries.data[fr.entry_prev].next = index; h->entries.data[fr.entry_prev].next = index;
} else { } else {
h->hashes.data[fr.hash_index] = index; h->hashes.data[fr.hash_index] = index;
@@ -238,7 +244,7 @@ void map_set(Map<T> *h, HashKey const &key, T const &value) {
template <typename T> template <typename T>
void map__erase(Map<T> *h, MapFindResult const &fr) { void map__erase(Map<T> *h, MapFindResult const &fr) {
MapFindResult last; MapFindResult last;
if (fr.entry_prev < 0) { if (fr.entry_prev == MAP_SENTINEL) {
h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
} else { } else {
h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
@@ -251,7 +257,7 @@ void map__erase(Map<T> *h, MapFindResult const &fr) {
array_pop(&h->entries); array_pop(&h->entries);
last = map__find(h, h->entries.data[fr.entry_index].key); last = map__find(h, h->entries.data[fr.entry_index].key);
if (last.entry_prev >= 0) { if (last.entry_prev != MAP_SENTINEL) {
h->entries.data[last.entry_prev].next = fr.entry_index; h->entries.data[last.entry_prev].next = fr.entry_index;
} else { } else {
h->hashes.data[last.hash_index] = fr.entry_index; h->hashes.data[last.hash_index] = fr.entry_index;
@@ -261,7 +267,7 @@ void map__erase(Map<T> *h, MapFindResult const &fr) {
template <typename T> template <typename T>
void map_remove(Map<T> *h, HashKey const &key) { void map_remove(Map<T> *h, HashKey const &key) {
MapFindResult fr = map__find(h, key); MapFindResult fr = map__find(h, key);
if (fr.entry_index >= 0) { if (fr.entry_index != MAP_SENTINEL) {
map__erase(h, fr); map__erase(h, fr);
} }
} }
@@ -270,7 +276,7 @@ template <typename T>
gb_inline void map_clear(Map<T> *h) { gb_inline void map_clear(Map<T> *h) {
array_clear(&h->entries); array_clear(&h->entries);
for (isize i = 0; i < h->hashes.count; i++) { for (isize i = 0; i < h->hashes.count; i++) {
h->hashes.data[i] = -1; h->hashes.data[i] = MAP_SENTINEL;
} }
} }
@@ -279,7 +285,7 @@ gb_inline void map_clear(Map<T> *h) {
template <typename T> template <typename T>
MapEntry<T> *multi_map_find_first(Map<T> *h, HashKey const &key) { MapEntry<T> *multi_map_find_first(Map<T> *h, HashKey const &key) {
isize i = map__find(h, key).entry_index; isize i = map__find(h, key).entry_index;
if (i < 0) { if (i == MAP_SENTINEL) {
return nullptr; return nullptr;
} }
return &h->entries.data[i]; return &h->entries.data[i];
@@ -288,7 +294,7 @@ MapEntry<T> *multi_map_find_first(Map<T> *h, HashKey const &key) {
template <typename T> template <typename T>
MapEntry<T> *multi_map_find_next(Map<T> *h, MapEntry<T> *e) { MapEntry<T> *multi_map_find_next(Map<T> *h, MapEntry<T> *e) {
isize i = e->next; isize i = e->next;
while (i >= 0) { while (i != MAP_SENTINEL) {
if (hash_key_equal(h->entries.data[i].key, e->key)) { if (hash_key_equal(h->entries.data[i].key, e->key)) {
return &h->entries.data[i]; return &h->entries.data[i];
} }
@@ -328,7 +334,7 @@ void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) {
// Make // Make
fr = map__find(h, key); fr = map__find(h, key);
i = map__add_entry(h, key); i = map__add_entry(h, key);
if (fr.entry_prev < 0) { if (fr.entry_prev == MAP_SENTINEL) {
h->hashes.data[fr.hash_index] = i; h->hashes.data[fr.hash_index] = i;
} else { } else {
h->entries.data[fr.entry_prev].next = i; h->entries.data[fr.entry_prev].next = i;
@@ -344,7 +350,7 @@ void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) {
template <typename T> template <typename T>
void multi_map_remove(Map<T> *h, HashKey const &key, MapEntry<T> *e) { void multi_map_remove(Map<T> *h, HashKey const &key, MapEntry<T> *e) {
MapFindResult fr = map__find_from_entry(h, e); MapFindResult fr = map__find_from_entry(h, e);
if (fr.entry_index >= 0) { if (fr.entry_index != MAP_SENTINEL) {
map__erase(h, fr); map__erase(h, fr);
} }
} }
+46 -25
View File
@@ -30,6 +30,7 @@ template <typename T> void ptr_set_remove (PtrSet<T> *s, T ptr);
template <typename T> void ptr_set_clear (PtrSet<T> *s); template <typename T> void ptr_set_clear (PtrSet<T> *s);
template <typename T> void ptr_set_grow (PtrSet<T> *s); template <typename T> void ptr_set_grow (PtrSet<T> *s);
template <typename T> void ptr_set_rehash (PtrSet<T> *s, isize new_count); template <typename T> void ptr_set_rehash (PtrSet<T> *s, isize new_count);
template <typename T> void ptr_set_reserve(PtrSet<T> *h, isize cap);
template <typename T> template <typename T>
@@ -78,6 +79,25 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
return fr; return fr;
} }
template <typename T>
gb_internal PtrSetFindResult ptr_set__find_from_entry(PtrSet<T> *s, PtrSetEntry<T> *e) {
PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL};
if (s->hashes.count != 0) {
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)e->ptr;
u64 n = cast(u64)s->hashes.count;
fr.hash_index = cast(PtrSetIndex)(hash & (n-1));
fr.entry_index = s->hashes.data[fr.hash_index];
while (fr.entry_index != PTR_SET_SENTINEL) {
if (&s->entries.data[fr.entry_index] == e) {
return fr;
}
fr.entry_prev = fr.entry_index;
fr.entry_index = s->entries.data[fr.entry_index].next;
}
}
return fr;
}
template <typename T> template <typename T>
gb_internal bool ptr_set__full(PtrSet<T> *s) { gb_internal bool ptr_set__full(PtrSet<T> *s) {
return 0.75f * s->hashes.count <= s->entries.count; return 0.75f * s->hashes.count <= s->entries.count;
@@ -90,37 +110,38 @@ gb_inline void ptr_set_grow(PtrSet<T> *s) {
} }
template <typename T> template <typename T>
void ptr_set_rehash(PtrSet<T> *s, isize new_count) { void ptr_set_reset_entries(PtrSet<T> *s) {
isize i, j; PtrSetIndex i;
PtrSet<T> ns = {}; for (i = 0; i < cast(PtrSetIndex)s->hashes.count; i++) {
new_count = next_pow2_isize(new_count); s->hashes.data[i] = PTR_SET_SENTINEL;
ns.hashes = s->hashes;
ns.entries.allocator = s->entries.allocator;
slice_resize(&ns.hashes, s->entries.allocator, new_count);
for (i = 0; i < new_count; i++) {
ns.hashes.data[i] = PTR_SET_SENTINEL;
} }
array_reserve(&ns.entries, ARRAY_GROW_FORMULA(s->entries.count)); for (i = 0; i < cast(PtrSetIndex)s->entries.count; i++) {
for (i = 0; i < s->entries.count; i++) {
PtrSetEntry<T> *e = &s->entries.data[i];
PtrSetFindResult fr; PtrSetFindResult fr;
if (ns.hashes.count == 0) { PtrSetEntry<T> *e = &s->entries.data[i];
ptr_set_grow(&ns); e->next = PTR_SET_SENTINEL;
} fr = ptr_set__find_from_entry(s, e);
fr = ptr_set__find(&ns, e->ptr);
j = ptr_set__add_entry(&ns, e->ptr);
if (fr.entry_prev == PTR_SET_SENTINEL) { if (fr.entry_prev == PTR_SET_SENTINEL) {
ns.hashes.data[fr.hash_index] = cast(PtrSetIndex)j; s->hashes[fr.hash_index] = i;
} else { } else {
ns.entries.data[fr.entry_prev].next = cast(PtrSetIndex)j; s->entries[fr.entry_prev].next = i;
}
ns.entries.data[j].next = fr.entry_index;
if (ptr_set__full(&ns)) {
ptr_set_grow(&ns);
} }
} }
array_free(&s->entries); }
*s = ns;
template <typename T>
void ptr_set_reserve(PtrSet<T> *s, isize cap) {
array_reserve(&s->entries, cap);
if (s->entries.count*2 < s->hashes.count) {
return;
}
slice_resize(&s->hashes, s->entries.allocator, cap*2);
ptr_set_reset_entries(s);
}
template <typename T>
void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
ptr_set_reserve(s, new_count);
} }
template <typename T> template <typename T>
+56 -48
View File
@@ -1,10 +1,16 @@
// NOTE(bill): This util stuff is the same for every `Map` // NOTE(bill): This util stuff is the same for every `Map`
typedef isize StringMapIndex;
struct StringMapFindResult { struct StringMapFindResult {
isize hash_index; StringMapIndex hash_index;
isize entry_prev; StringMapIndex entry_prev;
isize entry_index; StringMapIndex entry_index;
}; };
enum : StringMapIndex { STRING_MAP_SENTINEL = ~(StringMapIndex)0 };
struct StringHashKey { struct StringHashKey {
u64 hash; u64 hash;
String string; String string;
@@ -65,6 +71,7 @@ template <typename T> void string_map_remove (StringMap<T> *h, StringH
template <typename T> void string_map_clear (StringMap<T> *h); template <typename T> void string_map_clear (StringMap<T> *h);
template <typename T> void string_map_grow (StringMap<T> *h); template <typename T> void string_map_grow (StringMap<T> *h);
template <typename T> void string_map_rehash (StringMap<T> *h, isize new_count); template <typename T> void string_map_rehash (StringMap<T> *h, isize new_count);
template <typename T> void string_map_reserve (StringMap<T> *h, isize cap);
template <typename T> template <typename T>
gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) { gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) {
@@ -72,7 +79,7 @@ gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) {
slice_init(&h->hashes, a, capacity); slice_init(&h->hashes, a, capacity);
array_init(&h->entries, a, 0, capacity); array_init(&h->entries, a, 0, capacity);
for (isize i = 0; i < capacity; i++) { for (isize i = 0; i < capacity; i++) {
h->hashes.data[i] = -1; h->hashes.data[i] = STRING_MAP_SENTINEL;
} }
} }
@@ -86,18 +93,18 @@ template <typename T>
gb_internal isize string_map__add_entry(StringMap<T> *h, StringHashKey const &key) { gb_internal isize string_map__add_entry(StringMap<T> *h, StringHashKey const &key) {
StringMapEntry<T> e = {}; StringMapEntry<T> e = {};
e.key = key; e.key = key;
e.next = -1; e.next = STRING_MAP_SENTINEL;
array_add(&h->entries, e); array_add(&h->entries, e);
return h->entries.count-1; return h->entries.count-1;
} }
template <typename T> template <typename T>
gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) { gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) {
StringMapFindResult fr = {-1, -1, -1}; StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
if (h->hashes.count != 0) { if (h->hashes.count != 0) {
fr.hash_index = key.hash & (h->hashes.count-1); fr.hash_index = key.hash & (h->hashes.count-1);
fr.entry_index = h->hashes.data[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index != STRING_MAP_SENTINEL) {
if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) { if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
return fr; return fr;
} }
@@ -110,11 +117,11 @@ gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey
template <typename T> template <typename T>
gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) { gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
StringMapFindResult fr = {-1, -1, -1}; StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
if (h->hashes.count != 0) { if (h->hashes.count != 0) {
fr.hash_index = e->key.hash & (h->hashes.count-1); fr.hash_index = e->key.hash & (h->hashes.count-1);
fr.entry_index = h->hashes.data[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index != STRING_MAP_SENTINEL) {
if (&h->entries.data[fr.entry_index] == e) { if (&h->entries.data[fr.entry_index] == e) {
return fr; return fr;
} }
@@ -136,45 +143,46 @@ gb_inline void string_map_grow(StringMap<T> *h) {
string_map_rehash(h, new_count); string_map_rehash(h, new_count);
} }
template <typename T>
void string_map_reset_entries(StringMap<T> *h) {
isize i;
for (i = 0; i < h->hashes.count; i++) {
h->hashes.data[i] = STRING_MAP_SENTINEL;
}
for (i = 0; i < h->entries.count; i++) {
StringMapFindResult fr;
StringMapEntry<T> *e = &h->entries.data[i];
e->next = STRING_MAP_SENTINEL;
fr = string_map__find_from_entry(h, e);
if (fr.entry_prev == STRING_MAP_SENTINEL) {
h->hashes[fr.hash_index] = i;
} else {
h->entries[fr.entry_prev].next = i;
}
}
}
template <typename T>
void string_map_reserve(StringMap<T> *h, isize cap) {
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> template <typename T>
void string_map_rehash(StringMap<T> *h, isize new_count) { void string_map_rehash(StringMap<T> *h, isize new_count) {
isize i, j; string_map_reserve(h, new_count);
StringMap<T> nh = {};
new_count = next_pow2_isize(new_count);
nh.hashes = h->hashes;
nh.entries.allocator = h->entries.allocator;
slice_resize(&nh.hashes, h->entries.allocator, new_count);
for (i = 0; i < new_count; i++) {
nh.hashes.data[i] = -1;
}
array_reserve(&nh.entries, ARRAY_GROW_FORMULA(h->entries.count));
for (i = 0; i < h->entries.count; i++) {
StringMapEntry<T> *e = &h->entries.data[i];
StringMapFindResult fr;
if (nh.hashes.count == 0) {
string_map_grow(&nh);
}
fr = string_map__find(&nh, e->key);
j = string_map__add_entry(&nh, e->key);
if (fr.entry_prev < 0) {
nh.hashes.data[fr.hash_index] = j;
} else {
nh.entries.data[fr.entry_prev].next = j;
}
nh.entries.data[j].next = fr.entry_index;
nh.entries.data[j].value = e->value;
if (string_map__full(&nh)) {
string_map_grow(&nh);
}
}
array_free(&h->entries);
*h = nh;
} }
template <typename T> template <typename T>
T *string_map_get(StringMap<T> *h, StringHashKey const &key) { T *string_map_get(StringMap<T> *h, StringHashKey const &key) {
isize index = string_map__find(h, key).entry_index; isize index = string_map__find(h, key).entry_index;
if (index >= 0) { if (index != STRING_MAP_SENTINEL) {
return &h->entries.data[index].value; return &h->entries.data[index].value;
} }
return nullptr; return nullptr;
@@ -193,7 +201,7 @@ gb_inline T *string_map_get(StringMap<T> *h, char const *key) {
template <typename T> template <typename T>
T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) { T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) {
isize index = string_map__find(h, key).entry_index; isize index = string_map__find(h, key).entry_index;
GB_ASSERT(index >= 0); GB_ASSERT(index != STRING_MAP_SENTINEL);
return h->entries.data[index].value; return h->entries.data[index].value;
} }
@@ -215,11 +223,11 @@ void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) {
string_map_grow(h); string_map_grow(h);
} }
fr = string_map__find(h, key); fr = string_map__find(h, key);
if (fr.entry_index >= 0) { if (fr.entry_index != STRING_MAP_SENTINEL) {
index = fr.entry_index; index = fr.entry_index;
} else { } else {
index = string_map__add_entry(h, key); index = string_map__add_entry(h, key);
if (fr.entry_prev >= 0) { if (fr.entry_prev != STRING_MAP_SENTINEL) {
h->entries.data[fr.entry_prev].next = index; h->entries.data[fr.entry_prev].next = index;
} else { } else {
h->hashes.data[fr.hash_index] = index; h->hashes.data[fr.hash_index] = index;
@@ -246,7 +254,7 @@ gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value)
template <typename T> template <typename T>
void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) { void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) {
StringMapFindResult last; StringMapFindResult last;
if (fr.entry_prev < 0) { if (fr.entry_prev == STRING_MAP_SENTINEL) {
h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
} else { } else {
h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
@@ -257,7 +265,7 @@ void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) {
} }
h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1]; 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); last = string_map__find(h, h->entries.data[fr.entry_index].key);
if (last.entry_prev >= 0) { if (last.entry_prev != STRING_MAP_SENTINEL) {
h->entries.data[last.entry_prev].next = fr.entry_index; h->entries.data[last.entry_prev].next = fr.entry_index;
} else { } else {
h->hashes.data[last.hash_index] = fr.entry_index; h->hashes.data[last.hash_index] = fr.entry_index;
@@ -267,7 +275,7 @@ void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) {
template <typename T> template <typename T>
void string_map_remove(StringMap<T> *h, StringHashKey const &key) { void string_map_remove(StringMap<T> *h, StringHashKey const &key) {
StringMapFindResult fr = string_map__find(h, key); StringMapFindResult fr = string_map__find(h, key);
if (fr.entry_index >= 0) { if (fr.entry_index != STRING_MAP_SENTINEL) {
string_map__erase(h, fr); string_map__erase(h, fr);
} }
} }
@@ -276,7 +284,7 @@ template <typename T>
gb_inline void string_map_clear(StringMap<T> *h) { gb_inline void string_map_clear(StringMap<T> *h) {
array_clear(&h->entries); array_clear(&h->entries);
for (isize i = 0; i < h->hashes.count; i++) { for (isize i = 0; i < h->hashes.count; i++) {
h->hashes.data[i] = -1; h->hashes.data[i] = STRING_MAP_SENTINEL;
} }
} }