From 7124d541a132fc94b2c66c54bd73eb0d103ce3d3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 11 Jan 2023 18:10:27 +0000 Subject: General optimizations --- src/ptr_map.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/ptr_map.cpp') diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 89d2cbf9d..598904906 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -192,6 +192,26 @@ gb_internal void map_rehash(PtrMap *h, isize new_count) { template gb_internal V *map_get(PtrMap *h, K key) { + MapIndex hash_index = MAP_SENTINEL; + MapIndex entry_prev = MAP_SENTINEL; + MapIndex entry_index = MAP_SENTINEL; + if (h->hashes.count != 0) { + u32 hash = ptr_map_hash_key(key); + hash_index = cast(MapIndex)(hash & (h->hashes.count-1)); + entry_index = h->hashes.data[hash_index]; + while (entry_index != MAP_SENTINEL) { + auto *entry = &h->entries.data[entry_index]; + if (entry->key == key) { + return &entry->value; + } + entry_prev = entry_index; + entry_index = entry->next; + } + } + return nullptr; +} +template +gb_internal V *map_try_get(PtrMap *h, K key, MapFindResult *fr_) { MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; if (h->hashes.count != 0) { u32 hash = ptr_map_hash_key(key); @@ -206,9 +226,25 @@ gb_internal V *map_get(PtrMap *h, K key) { fr.entry_index = entry->next; } } + if (h->hashes.count == 0 || map__full(h)) { + map_grow(h); + } + if (fr_) *fr_ = fr; return nullptr; } + +template +gb_internal void map_set_internal_from_try_get(PtrMap *h, K key, V const &value, MapFindResult const &fr) { + MapIndex index = 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; +} + template gb_internal V &map_must_get(PtrMap *h, K key) { V *ptr = map_get(h, key); -- cgit v1.2.3