mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Inline map gets; cast explicitly on TOMBSTONE checking
This commit is contained in:
+19
-8
@@ -112,11 +112,12 @@ gb_internal MapFindResult map__find(PtrMap<K, V> *h, K key) {
|
|||||||
fr.hash_index = cast(MapIndex)(hash & (h->hashes.count-1));
|
fr.hash_index = cast(MapIndex)(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 != MAP_SENTINEL) {
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
if (h->entries.data[fr.entry_index].key == key) {
|
auto *entry = &h->entries.data[fr.entry_index];
|
||||||
|
if (entry->key == key) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
fr.entry_prev = fr.entry_index;
|
fr.entry_prev = fr.entry_index;
|
||||||
fr.entry_index = h->entries.data[fr.entry_index].next;
|
fr.entry_index = entry->next;
|
||||||
}
|
}
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
@@ -190,18 +191,28 @@ gb_internal void map_rehash(PtrMap<K, V> *h, isize new_count) {
|
|||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
gb_internal V *map_get(PtrMap<K, V> *h, K key) {
|
gb_internal V *map_get(PtrMap<K, V> *h, K key) {
|
||||||
MapIndex index = map__find(h, key).entry_index;
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (index != MAP_SENTINEL) {
|
if (h->hashes.count != 0) {
|
||||||
return &h->entries.data[index].value;
|
u32 hash = ptr_map_hash_key(key);
|
||||||
|
fr.hash_index = cast(MapIndex)(hash & (h->hashes.count-1));
|
||||||
|
fr.entry_index = h->hashes.data[fr.hash_index];
|
||||||
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
|
auto *entry = &h->entries.data[fr.entry_index];
|
||||||
|
if (entry->key == key) {
|
||||||
|
return &entry->value;
|
||||||
|
}
|
||||||
|
fr.entry_prev = fr.entry_index;
|
||||||
|
fr.entry_index = entry->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
gb_internal V &map_must_get(PtrMap<K, V> *h, K key) {
|
gb_internal V &map_must_get(PtrMap<K, V> *h, K key) {
|
||||||
MapIndex index = map__find(h, key).entry_index;
|
V *ptr = map_get(h, key);
|
||||||
GB_ASSERT(index != MAP_SENTINEL);
|
GB_ASSERT(ptr != nullptr);
|
||||||
return h->entries.data[index].value;
|
return *ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
|
|||||||
+5
-5
@@ -12,7 +12,7 @@ struct TypeIsPointer<T *> {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct PtrSet {
|
struct PtrSet {
|
||||||
static_assert(TypeIsPointer<T>::value, "PtrSet::T must be a pointer");
|
static_assert(TypeIsPointer<T>::value, "PtrSet::T must be a pointer");
|
||||||
static constexpr T TOMBSTONE = (T)(~uintptr(0));
|
static constexpr uintptr TOMBSTONE = ~(uintptr)(0ull);
|
||||||
|
|
||||||
T * keys;
|
T * keys;
|
||||||
usize count;
|
usize count;
|
||||||
@@ -133,7 +133,7 @@ gb_internal bool ptr_set_update(PtrSet<T> *s, T ptr) { // returns true if it pre
|
|||||||
for (usize i = 0; i < s->capacity; i++) {
|
for (usize i = 0; i < s->capacity; i++) {
|
||||||
T *key = &s->keys[hash_index];
|
T *key = &s->keys[hash_index];
|
||||||
GB_ASSERT(*key != ptr);
|
GB_ASSERT(*key != ptr);
|
||||||
if (*key == PtrSet<T>::TOMBSTONE || *key == nullptr) {
|
if (*key == (T)PtrSet<T>::TOMBSTONE || *key == nullptr) {
|
||||||
*key = ptr;
|
*key = ptr;
|
||||||
s->count++;
|
s->count++;
|
||||||
return false;
|
return false;
|
||||||
@@ -157,7 +157,7 @@ gb_internal void ptr_set_remove(PtrSet<T> *s, T ptr) {
|
|||||||
isize index = ptr_set__find(s, ptr);
|
isize index = ptr_set__find(s, ptr);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
GB_ASSERT(s->count > 0);
|
GB_ASSERT(s->count > 0);
|
||||||
s->keys[index] = PtrSet<T>::TOMBSTONE;
|
s->keys[index] = (T)PtrSet<T>::TOMBSTONE;
|
||||||
s->count--;
|
s->count--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,7 +180,7 @@ struct PtrSetIterator {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
T key = set->keys[index];
|
T key = set->keys[index];
|
||||||
if (key != nullptr && key != PtrSet<T>::TOMBSTONE) {
|
if (key != nullptr && key != (T)PtrSet<T>::TOMBSTONE) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,7 +202,7 @@ gb_internal PtrSetIterator<T> begin(PtrSet<T> &set) noexcept {
|
|||||||
usize index = 0;
|
usize index = 0;
|
||||||
while (index < set.capacity) {
|
while (index < set.capacity) {
|
||||||
T key = set.keys[index];
|
T key = set.keys[index];
|
||||||
if (key != nullptr && key != PtrSet<T>::TOMBSTONE) {
|
if (key != nullptr && key != (T)PtrSet<T>::TOMBSTONE) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
|
|||||||
+12
-3
@@ -180,9 +180,18 @@ gb_internal void string_map_rehash(StringMap<T> *h, isize new_count) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal T *string_map_get(StringMap<T> *h, StringHashKey const &key) {
|
gb_internal T *string_map_get(StringMap<T> *h, StringHashKey const &key) {
|
||||||
isize index = string_map__find(h, key).entry_index;
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (index != MAP_SENTINEL) {
|
if (h->hashes.count != 0) {
|
||||||
return &h->entries.data[index].value;
|
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) {
|
||||||
|
auto *entry = &h->entries.data[fr.entry_index];
|
||||||
|
if (string_hash_key_equal(entry->key, key)) {
|
||||||
|
return &entry->value;
|
||||||
|
}
|
||||||
|
fr.entry_prev = fr.entry_index;
|
||||||
|
fr.entry_index = entry->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user