diff options
| author | gingerBill <bill@gingerbill.org> | 2023-01-11 18:10:27 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-01-11 18:10:27 +0000 |
| commit | 7124d541a132fc94b2c66c54bd73eb0d103ce3d3 (patch) | |
| tree | f0eb8f42e5ebc9ec6ac1ff332e09754a804c9a86 /src/ptr_map.cpp | |
| parent | 3c7e45a46fc68426641047a540d4cb50b0fbd9c8 (diff) | |
General optimizations
Diffstat (limited to 'src/ptr_map.cpp')
| -rw-r--r-- | src/ptr_map.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
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<K, V> *h, isize new_count) { template <typename K, typename V> gb_internal V *map_get(PtrMap<K, V> *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 <typename K, typename V> +gb_internal V *map_try_get(PtrMap<K, V> *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<K, V> *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 <typename K, typename V> +gb_internal void map_set_internal_from_try_get(PtrMap<K, V> *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 <typename K, typename V> gb_internal V &map_must_get(PtrMap<K, V> *h, K key) { V *ptr = map_get(h, key); |