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