Minor improvements to Map and StringMap

This commit is contained in:
gingerBill
2021-07-10 21:51:39 +01:00
parent 8a6b743d2a
commit 6de0181c75
2 changed files with 54 additions and 54 deletions
+31 -31
View File
@@ -118,13 +118,13 @@ gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) {
if (h->hashes.count > 0) { if (h->hashes.count > 0) {
// fr.hash_index = u128_to_i64(key.key % u128_from_i64(h->hashes.count)); // fr.hash_index = u128_to_i64(key.key % u128_from_i64(h->hashes.count));
fr.hash_index = key.key % h->hashes.count; fr.hash_index = key.key % h->hashes.count;
fr.entry_index = h->hashes[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index >= 0) {
if (hash_key_equal(h->entries[fr.entry_index].key, key)) { if (hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
return fr; return fr;
} }
fr.entry_prev = fr.entry_index; fr.entry_prev = fr.entry_index;
fr.entry_index = h->entries[fr.entry_index].next; fr.entry_index = h->entries.data[fr.entry_index].next;
} }
} }
return fr; return fr;
@@ -135,13 +135,13 @@ gb_internal MapFindResult map__find_from_entry(Map<T> *h, MapEntry<T> *e) {
MapFindResult fr = {-1, -1, -1}; MapFindResult fr = {-1, -1, -1};
if (h->hashes.count > 0) { if (h->hashes.count > 0) {
fr.hash_index = e->key.key % h->hashes.count; fr.hash_index = e->key.key % h->hashes.count;
fr.entry_index = h->hashes[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index >= 0) {
if (&h->entries[fr.entry_index] == e) { if (&h->entries.data[fr.entry_index] == e) {
return fr; return fr;
} }
fr.entry_prev = fr.entry_index; fr.entry_prev = fr.entry_index;
fr.entry_index = h->entries[fr.entry_index].next; fr.entry_index = h->entries.data[fr.entry_index].next;
} }
} }
return fr; return fr;
@@ -169,10 +169,10 @@ void map_rehash(Map<T> *h, isize new_count) {
array_resize(&nh.hashes, new_count); array_resize(&nh.hashes, new_count);
array_reserve(&nh.entries, h->entries.count); array_reserve(&nh.entries, h->entries.count);
for (i = 0; i < new_count; i++) { for (i = 0; i < new_count; i++) {
nh.hashes[i] = -1; nh.hashes.data[i] = -1;
} }
for (i = 0; i < h->entries.count; i++) { for (i = 0; i < h->entries.count; i++) {
MapEntry<T> *e = &h->entries[i]; MapEntry<T> *e = &h->entries.data[i];
MapFindResult fr; MapFindResult fr;
if (nh.hashes.count == 0) { if (nh.hashes.count == 0) {
map_grow(&nh); map_grow(&nh);
@@ -180,12 +180,12 @@ void map_rehash(Map<T> *h, isize new_count) {
fr = map__find(&nh, e->key); fr = map__find(&nh, e->key);
j = map__add_entry(&nh, e->key); j = map__add_entry(&nh, e->key);
if (fr.entry_prev < 0) { if (fr.entry_prev < 0) {
nh.hashes[fr.hash_index] = j; nh.hashes.data[fr.hash_index] = j;
} else { } else {
nh.entries[fr.entry_prev].next = j; nh.entries.data[fr.entry_prev].next = j;
} }
nh.entries[j].next = fr.entry_index; nh.entries.data[j].next = fr.entry_index;
nh.entries[j].value = e->value; nh.entries.data[j].value = e->value;
if (map__full(&nh)) { if (map__full(&nh)) {
map_grow(&nh); map_grow(&nh);
} }
@@ -198,7 +198,7 @@ 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 >= 0) {
return &h->entries[index].value; return &h->entries.data[index].value;
} }
return nullptr; return nullptr;
} }
@@ -207,7 +207,7 @@ 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 >= 0);
return h->entries[index].value; return h->entries.data[index].value;
} }
template <typename T> template <typename T>
@@ -223,12 +223,12 @@ void map_set(Map<T> *h, HashKey const &key, T const &value) {
} else { } else {
index = map__add_entry(h, key); index = map__add_entry(h, key);
if (fr.entry_prev >= 0) { if (fr.entry_prev >= 0) {
h->entries[fr.entry_prev].next = index; h->entries.data[fr.entry_prev].next = index;
} else { } else {
h->hashes[fr.hash_index] = index; h->hashes.data[fr.hash_index] = index;
} }
} }
h->entries[index].value = value; h->entries.data[index].value = value;
if (map__full(h)) { if (map__full(h)) {
map_grow(h); map_grow(h);
@@ -240,20 +240,20 @@ 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 < 0) {
h->hashes[fr.hash_index] = h->entries[fr.entry_index].next; h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
} else { } else {
h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next; h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
} }
if (fr.entry_index == h->entries.count-1) { if (fr.entry_index == h->entries.count-1) {
array_pop(&h->entries); array_pop(&h->entries);
return; return;
} }
h->entries[fr.entry_index] = h->entries[h->entries.count-1]; h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1];
last = map__find(h, h->entries[fr.entry_index].key); last = map__find(h, h->entries.data[fr.entry_index].key);
if (last.entry_prev >= 0) { if (last.entry_prev >= 0) {
h->entries[last.entry_prev].next = fr.entry_index; h->entries.data[last.entry_prev].next = fr.entry_index;
} else { } else {
h->hashes[last.hash_index] = fr.entry_index; h->hashes.data[last.hash_index] = fr.entry_index;
} }
} }
@@ -279,17 +279,17 @@ MapEntry<T> *multi_map_find_first(Map<T> *h, HashKey const &key) {
if (i < 0) { if (i < 0) {
return nullptr; return nullptr;
} }
return &h->entries[i]; return &h->entries.data[i];
} }
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 >= 0) {
if (hash_key_equal(h->entries[i].key, e->key)) { if (hash_key_equal(h->entries.data[i].key, e->key)) {
return &h->entries[i]; return &h->entries.data[i];
} }
i = h->entries[i].next; i = h->entries.data[i].next;
} }
return nullptr; return nullptr;
} }
@@ -326,12 +326,12 @@ void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) {
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 < 0) {
h->hashes[fr.hash_index] = i; h->hashes.data[fr.hash_index] = i;
} else { } else {
h->entries[fr.entry_prev].next = i; h->entries.data[fr.entry_prev].next = i;
} }
h->entries[i].next = fr.entry_index; h->entries.data[i].next = fr.entry_index;
h->entries[i].value = value; h->entries.data[i].value = value;
// Grow if needed // Grow if needed
if (map__full(h)) { if (map__full(h)) {
map_grow(h); map_grow(h);
+23 -23
View File
@@ -96,13 +96,13 @@ gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey
StringMapFindResult fr = {-1, -1, -1}; StringMapFindResult fr = {-1, -1, -1};
if (h->hashes.count > 0) { if (h->hashes.count > 0) {
fr.hash_index = key.hash % h->hashes.count; fr.hash_index = key.hash % h->hashes.count;
fr.entry_index = h->hashes[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index >= 0) {
if (string_hash_key_equal(h->entries[fr.entry_index].key, key)) { if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
return fr; return fr;
} }
fr.entry_prev = fr.entry_index; fr.entry_prev = fr.entry_index;
fr.entry_index = h->entries[fr.entry_index].next; fr.entry_index = h->entries.data[fr.entry_index].next;
} }
} }
return fr; return fr;
@@ -113,13 +113,13 @@ gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, Str
StringMapFindResult fr = {-1, -1, -1}; StringMapFindResult fr = {-1, -1, -1};
if (h->hashes.count > 0) { if (h->hashes.count > 0) {
fr.hash_index = e->key.hash % h->hashes.count; fr.hash_index = e->key.hash % h->hashes.count;
fr.entry_index = h->hashes[fr.hash_index]; fr.entry_index = h->hashes.data[fr.hash_index];
while (fr.entry_index >= 0) { while (fr.entry_index >= 0) {
if (&h->entries[fr.entry_index] == e) { if (&h->entries.data[fr.entry_index] == e) {
return fr; return fr;
} }
fr.entry_prev = fr.entry_index; fr.entry_prev = fr.entry_index;
fr.entry_index = h->entries[fr.entry_index].next; fr.entry_index = h->entries.data[fr.entry_index].next;
} }
} }
return fr; return fr;
@@ -147,10 +147,10 @@ void string_map_rehash(StringMap<T> *h, isize new_count) {
array_resize(&nh.hashes, new_count); array_resize(&nh.hashes, new_count);
array_reserve(&nh.entries, h->entries.count); array_reserve(&nh.entries, h->entries.count);
for (i = 0; i < new_count; i++) { for (i = 0; i < new_count; i++) {
nh.hashes[i] = -1; nh.hashes.data[i] = -1;
} }
for (i = 0; i < h->entries.count; i++) { for (i = 0; i < h->entries.count; i++) {
StringMapEntry<T> *e = &h->entries[i]; StringMapEntry<T> *e = &h->entries.data[i];
StringMapFindResult fr; StringMapFindResult fr;
if (nh.hashes.count == 0) { if (nh.hashes.count == 0) {
string_map_grow(&nh); string_map_grow(&nh);
@@ -158,12 +158,12 @@ void string_map_rehash(StringMap<T> *h, isize new_count) {
fr = string_map__find(&nh, e->key); fr = string_map__find(&nh, e->key);
j = string_map__add_entry(&nh, e->key); j = string_map__add_entry(&nh, e->key);
if (fr.entry_prev < 0) { if (fr.entry_prev < 0) {
nh.hashes[fr.hash_index] = j; nh.hashes.data[fr.hash_index] = j;
} else { } else {
nh.entries[fr.entry_prev].next = j; nh.entries.data[fr.entry_prev].next = j;
} }
nh.entries[j].next = fr.entry_index; nh.entries.data[j].next = fr.entry_index;
nh.entries[j].value = e->value; nh.entries.data[j].value = e->value;
if (string_map__full(&nh)) { if (string_map__full(&nh)) {
string_map_grow(&nh); string_map_grow(&nh);
} }
@@ -176,7 +176,7 @@ 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 >= 0) {
return &h->entries[index].value; return &h->entries.data[index].value;
} }
return nullptr; return nullptr;
} }
@@ -195,7 +195,7 @@ 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 >= 0);
return h->entries[index].value; return h->entries.data[index].value;
} }
template <typename T> template <typename T>
@@ -221,12 +221,12 @@ void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) {
} 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 >= 0) {
h->entries[fr.entry_prev].next = index; h->entries.data[fr.entry_prev].next = index;
} else { } else {
h->hashes[fr.hash_index] = index; h->hashes.data[fr.hash_index] = index;
} }
} }
h->entries[index].value = value; h->entries.data[index].value = value;
if (string_map__full(h)) { if (string_map__full(h)) {
string_map_grow(h); string_map_grow(h);
@@ -248,20 +248,20 @@ 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 < 0) {
h->hashes[fr.hash_index] = h->entries[fr.entry_index].next; h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
} else { } else {
h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next; h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
} }
if (fr.entry_index == h->entries.count-1) { if (fr.entry_index == h->entries.count-1) {
array_pop(&h->entries); array_pop(&h->entries);
return; return;
} }
h->entries[fr.entry_index] = h->entries[h->entries.count-1]; h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1];
last = string_map__find(h, h->entries[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 >= 0) {
h->entries[last.entry_prev].next = fr.entry_index; h->entries.data[last.entry_prev].next = fr.entry_index;
} else { } else {
h->hashes[last.hash_index] = fr.entry_index; h->hashes.data[last.hash_index] = fr.entry_index;
} }
} }