mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Unify MapFindResult types
This commit is contained in:
+1
-2
@@ -275,10 +275,9 @@ gb_global String global_module_path = {0};
|
|||||||
gb_global bool global_module_path_set = false;
|
gb_global bool global_module_path_set = false;
|
||||||
|
|
||||||
|
|
||||||
#include "string_map.cpp"
|
|
||||||
#include "map.cpp"
|
|
||||||
#include "ptr_map.cpp"
|
#include "ptr_map.cpp"
|
||||||
#include "ptr_set.cpp"
|
#include "ptr_set.cpp"
|
||||||
|
#include "string_map.cpp"
|
||||||
#include "string_set.cpp"
|
#include "string_set.cpp"
|
||||||
#include "priority_queue.cpp"
|
#include "priority_queue.cpp"
|
||||||
#include "thread_pool.cpp"
|
#include "thread_pool.cpp"
|
||||||
|
|||||||
+2
-2
@@ -29,11 +29,11 @@ struct PtrMap {
|
|||||||
|
|
||||||
u32 ptr_map_hash_key(void const *key) {
|
u32 ptr_map_hash_key(void const *key) {
|
||||||
// TODO(bill): Improve ptr_map_hash_key
|
// TODO(bill): Improve ptr_map_hash_key
|
||||||
return gb_fnv32a(&key, gb_size_of(key));
|
return fnv32a(&key, gb_size_of(key));
|
||||||
}
|
}
|
||||||
u32 ptr_map_hash_key(uintptr key) {
|
u32 ptr_map_hash_key(uintptr key) {
|
||||||
// TODO(bill): Improve ptr_map_hash_key
|
// TODO(bill): Improve ptr_map_hash_key
|
||||||
return gb_fnv32a(&key, gb_size_of(key));
|
return fnv32a(&key, gb_size_of(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename K, typename V> void map_init (PtrMap<K, V> *h, gbAllocator a, isize capacity = 16);
|
template <typename K, typename V> void map_init (PtrMap<K, V> *h, gbAllocator a, isize capacity = 16);
|
||||||
|
|||||||
+37
-48
@@ -1,23 +1,12 @@
|
|||||||
typedef u32 PtrSetIndex;
|
|
||||||
|
|
||||||
struct PtrSetFindResult {
|
|
||||||
PtrSetIndex hash_index;
|
|
||||||
PtrSetIndex entry_prev;
|
|
||||||
PtrSetIndex entry_index;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum : PtrSetIndex { PTR_SET_SENTINEL = ~(PtrSetIndex)0 };
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct PtrSetEntry {
|
struct PtrSetEntry {
|
||||||
T ptr;
|
T ptr;
|
||||||
PtrSetIndex next;
|
MapIndex next;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct PtrSet {
|
struct PtrSet {
|
||||||
Slice<PtrSetIndex> hashes;
|
Slice<MapIndex> hashes;
|
||||||
Array<PtrSetEntry<T>> entries;
|
Array<PtrSetEntry<T>> entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,7 +29,7 @@ void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) {
|
|||||||
slice_init(&s->hashes, a, capacity);
|
slice_init(&s->hashes, a, capacity);
|
||||||
array_init(&s->entries, a, 0, capacity);
|
array_init(&s->entries, a, 0, capacity);
|
||||||
for (isize i = 0; i < capacity; i++) {
|
for (isize i = 0; i < capacity; i++) {
|
||||||
s->hashes.data[i] = PTR_SET_SENTINEL;
|
s->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,24 +40,24 @@ void ptr_set_destroy(PtrSet<T> *s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal PtrSetIndex ptr_set__add_entry(PtrSet<T> *s, T ptr) {
|
gb_internal MapIndex ptr_set__add_entry(PtrSet<T> *s, T ptr) {
|
||||||
PtrSetEntry<T> e = {};
|
PtrSetEntry<T> e = {};
|
||||||
e.ptr = ptr;
|
e.ptr = ptr;
|
||||||
e.next = PTR_SET_SENTINEL;
|
e.next = MAP_SENTINEL;
|
||||||
array_add(&s->entries, e);
|
array_add(&s->entries, e);
|
||||||
return cast(PtrSetIndex)(s->entries.count-1);
|
return cast(MapIndex)(s->entries.count-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
|
gb_internal MapFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
|
||||||
PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL};
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (s->hashes.count != 0) {
|
if (s->hashes.count != 0) {
|
||||||
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr;
|
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr;
|
||||||
u64 n = cast(u64)s->hashes.count;
|
u64 n = cast(u64)s->hashes.count;
|
||||||
fr.hash_index = cast(PtrSetIndex)(hash & (n-1));
|
fr.hash_index = cast(MapIndex)(hash & (n-1));
|
||||||
fr.entry_index = s->hashes.data[fr.hash_index];
|
fr.entry_index = s->hashes.data[fr.hash_index];
|
||||||
while (fr.entry_index != PTR_SET_SENTINEL) {
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
if (s->entries.data[fr.entry_index].ptr == ptr) {
|
if (s->entries.data[fr.entry_index].ptr == ptr) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
@@ -80,14 +69,14 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal PtrSetFindResult ptr_set__find_from_entry(PtrSet<T> *s, PtrSetEntry<T> *e) {
|
gb_internal MapFindResult ptr_set__find_from_entry(PtrSet<T> *s, PtrSetEntry<T> *e) {
|
||||||
PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL};
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (s->hashes.count != 0) {
|
if (s->hashes.count != 0) {
|
||||||
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)e->ptr;
|
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)e->ptr;
|
||||||
u64 n = cast(u64)s->hashes.count;
|
u64 n = cast(u64)s->hashes.count;
|
||||||
fr.hash_index = cast(PtrSetIndex)(hash & (n-1));
|
fr.hash_index = cast(MapIndex)(hash & (n-1));
|
||||||
fr.entry_index = s->hashes.data[fr.hash_index];
|
fr.entry_index = s->hashes.data[fr.hash_index];
|
||||||
while (fr.entry_index != PTR_SET_SENTINEL) {
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
if (&s->entries.data[fr.entry_index] == e) {
|
if (&s->entries.data[fr.entry_index] == e) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
@@ -112,17 +101,17 @@ gb_inline void ptr_set_grow(PtrSet<T> *s) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
void ptr_set_reset_entries(PtrSet<T> *s) {
|
void ptr_set_reset_entries(PtrSet<T> *s) {
|
||||||
for (isize i = 0; i < s->hashes.count; i++) {
|
for (isize i = 0; i < s->hashes.count; i++) {
|
||||||
s->hashes.data[i] = PTR_SET_SENTINEL;
|
s->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
for (isize i = 0; i < s->entries.count; i++) {
|
for (isize i = 0; i < s->entries.count; i++) {
|
||||||
PtrSetFindResult fr;
|
MapFindResult fr;
|
||||||
PtrSetEntry<T> *e = &s->entries.data[i];
|
PtrSetEntry<T> *e = &s->entries.data[i];
|
||||||
e->next = PTR_SET_SENTINEL;
|
e->next = MAP_SENTINEL;
|
||||||
fr = ptr_set__find_from_entry(s, e);
|
fr = ptr_set__find_from_entry(s, e);
|
||||||
if (fr.entry_prev == PTR_SET_SENTINEL) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
s->hashes[fr.hash_index] = cast(PtrSetIndex)i;
|
s->hashes[fr.hash_index] = cast(MapIndex)i;
|
||||||
} else {
|
} else {
|
||||||
s->entries[fr.entry_prev].next = cast(PtrSetIndex)i;
|
s->entries[fr.entry_prev].next = cast(MapIndex)i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,21 +135,21 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) {
|
gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) {
|
||||||
isize index = ptr_set__find(s, ptr).entry_index;
|
isize index = ptr_set__find(s, ptr).entry_index;
|
||||||
return index != PTR_SET_SENTINEL;
|
return index != MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if it already exists
|
// Returns true if it already exists
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ptr_set_add(PtrSet<T> *s, T ptr) {
|
T ptr_set_add(PtrSet<T> *s, T ptr) {
|
||||||
PtrSetIndex index;
|
MapIndex index;
|
||||||
PtrSetFindResult fr;
|
MapFindResult fr;
|
||||||
if (s->hashes.count == 0) {
|
if (s->hashes.count == 0) {
|
||||||
ptr_set_grow(s);
|
ptr_set_grow(s);
|
||||||
}
|
}
|
||||||
fr = ptr_set__find(s, ptr);
|
fr = ptr_set__find(s, ptr);
|
||||||
if (fr.entry_index == PTR_SET_SENTINEL) {
|
if (fr.entry_index == MAP_SENTINEL) {
|
||||||
index = ptr_set__add_entry(s, ptr);
|
index = ptr_set__add_entry(s, ptr);
|
||||||
if (fr.entry_prev != PTR_SET_SENTINEL) {
|
if (fr.entry_prev != MAP_SENTINEL) {
|
||||||
s->entries.data[fr.entry_prev].next = index;
|
s->entries.data[fr.entry_prev].next = index;
|
||||||
} else {
|
} else {
|
||||||
s->hashes.data[fr.hash_index] = index;
|
s->hashes.data[fr.hash_index] = index;
|
||||||
@@ -175,17 +164,17 @@ T ptr_set_add(PtrSet<T> *s, T ptr) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
bool ptr_set_update(PtrSet<T> *s, T ptr) { // returns true if it previously existsed
|
bool ptr_set_update(PtrSet<T> *s, T ptr) { // returns true if it previously existsed
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
PtrSetIndex index;
|
MapIndex index;
|
||||||
PtrSetFindResult fr;
|
MapFindResult fr;
|
||||||
if (s->hashes.count == 0) {
|
if (s->hashes.count == 0) {
|
||||||
ptr_set_grow(s);
|
ptr_set_grow(s);
|
||||||
}
|
}
|
||||||
fr = ptr_set__find(s, ptr);
|
fr = ptr_set__find(s, ptr);
|
||||||
if (fr.entry_index != PTR_SET_SENTINEL) {
|
if (fr.entry_index != MAP_SENTINEL) {
|
||||||
exists = true;
|
exists = true;
|
||||||
} else {
|
} else {
|
||||||
index = ptr_set__add_entry(s, ptr);
|
index = ptr_set__add_entry(s, ptr);
|
||||||
if (fr.entry_prev != PTR_SET_SENTINEL) {
|
if (fr.entry_prev != MAP_SENTINEL) {
|
||||||
s->entries.data[fr.entry_prev].next = index;
|
s->entries.data[fr.entry_prev].next = index;
|
||||||
} else {
|
} else {
|
||||||
s->hashes.data[fr.hash_index] = index;
|
s->hashes.data[fr.hash_index] = index;
|
||||||
@@ -200,9 +189,9 @@ bool ptr_set_update(PtrSet<T> *s, T ptr) { // returns true if it previously exis
|
|||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
|
void ptr_set__erase(PtrSet<T> *s, MapFindResult fr) {
|
||||||
PtrSetFindResult last;
|
MapFindResult last;
|
||||||
if (fr.entry_prev == PTR_SET_SENTINEL) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
s->hashes.data[fr.hash_index] = s->entries.data[fr.entry_index].next;
|
s->hashes.data[fr.hash_index] = s->entries.data[fr.entry_index].next;
|
||||||
} else {
|
} else {
|
||||||
s->entries.data[fr.entry_prev].next = s->entries.data[fr.entry_index].next;
|
s->entries.data[fr.entry_prev].next = s->entries.data[fr.entry_index].next;
|
||||||
@@ -213,7 +202,7 @@ void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
|
|||||||
}
|
}
|
||||||
s->entries.data[fr.entry_index] = s->entries.data[s->entries.count-1];
|
s->entries.data[fr.entry_index] = s->entries.data[s->entries.count-1];
|
||||||
last = ptr_set__find(s, s->entries.data[fr.entry_index].ptr);
|
last = ptr_set__find(s, s->entries.data[fr.entry_index].ptr);
|
||||||
if (last.entry_prev != PTR_SET_SENTINEL) {
|
if (last.entry_prev != MAP_SENTINEL) {
|
||||||
s->entries.data[last.entry_prev].next = fr.entry_index;
|
s->entries.data[last.entry_prev].next = fr.entry_index;
|
||||||
} else {
|
} else {
|
||||||
s->hashes.data[last.hash_index] = fr.entry_index;
|
s->hashes.data[last.hash_index] = fr.entry_index;
|
||||||
@@ -222,8 +211,8 @@ void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void ptr_set_remove(PtrSet<T> *s, T ptr) {
|
void ptr_set_remove(PtrSet<T> *s, T ptr) {
|
||||||
PtrSetFindResult fr = ptr_set__find(s, ptr);
|
MapFindResult fr = ptr_set__find(s, ptr);
|
||||||
if (fr.entry_index != PTR_SET_SENTINEL) {
|
if (fr.entry_index != MAP_SENTINEL) {
|
||||||
ptr_set__erase(s, fr);
|
ptr_set__erase(s, fr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -232,6 +221,6 @@ template <typename T>
|
|||||||
gb_inline void ptr_set_clear(PtrSet<T> *s) {
|
gb_inline void ptr_set_clear(PtrSet<T> *s) {
|
||||||
array_clear(&s->entries);
|
array_clear(&s->entries);
|
||||||
for (isize i = 0; i < s->hashes.count; i++) {
|
for (isize i = 0; i < s->hashes.count; i++) {
|
||||||
s->hashes.data[i] = PTR_SET_SENTINEL;
|
s->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-54
@@ -1,28 +1,11 @@
|
|||||||
// NOTE(bill): This util stuff is the same for every `Map`
|
|
||||||
|
|
||||||
typedef u32 StringMapIndex;
|
|
||||||
|
|
||||||
struct StringMapFindResult {
|
|
||||||
StringMapIndex hash_index;
|
|
||||||
StringMapIndex entry_prev;
|
|
||||||
StringMapIndex entry_index;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum : StringMapIndex { STRING_MAP_SENTINEL = ~(StringMapIndex)0 };
|
|
||||||
|
|
||||||
|
|
||||||
struct StringHashKey {
|
struct StringHashKey {
|
||||||
u64 hash;
|
u32 hash;
|
||||||
String string;
|
String string;
|
||||||
};
|
};
|
||||||
|
|
||||||
u64 string_hashing_proc(void const *data, isize len) {
|
|
||||||
return fnv64a(data, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
gb_inline StringHashKey string_hash_string(String const &s) {
|
gb_inline StringHashKey string_hash_string(String const &s) {
|
||||||
StringHashKey hash_key = {};
|
StringHashKey hash_key = {};
|
||||||
hash_key.hash = string_hashing_proc(s.text, s.len);
|
hash_key.hash = fnv32a(s.text, s.len);
|
||||||
hash_key.string = s;
|
hash_key.string = s;
|
||||||
return hash_key;
|
return hash_key;
|
||||||
}
|
}
|
||||||
@@ -40,14 +23,14 @@ bool operator!=(StringHashKey const &a, StringHashKey const &b) { return !string
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct StringMapEntry {
|
struct StringMapEntry {
|
||||||
StringHashKey key;
|
StringHashKey key;
|
||||||
StringMapIndex next;
|
MapIndex next;
|
||||||
T value;
|
T value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct StringMap {
|
struct StringMap {
|
||||||
Slice<StringMapIndex> hashes;
|
Slice<MapIndex> hashes;
|
||||||
Array<StringMapEntry<T> > entries;
|
Array<StringMapEntry<T> > entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -79,7 +62,7 @@ gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) {
|
|||||||
slice_init(&h->hashes, a, capacity);
|
slice_init(&h->hashes, a, capacity);
|
||||||
array_init(&h->entries, a, 0, capacity);
|
array_init(&h->entries, a, 0, capacity);
|
||||||
for (isize i = 0; i < capacity; i++) {
|
for (isize i = 0; i < capacity; i++) {
|
||||||
h->hashes.data[i] = STRING_MAP_SENTINEL;
|
h->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,21 +73,21 @@ gb_inline void string_map_destroy(StringMap<T> *h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal StringMapIndex string_map__add_entry(StringMap<T> *h, StringHashKey const &key) {
|
gb_internal MapIndex string_map__add_entry(StringMap<T> *h, StringHashKey const &key) {
|
||||||
StringMapEntry<T> e = {};
|
StringMapEntry<T> e = {};
|
||||||
e.key = key;
|
e.key = key;
|
||||||
e.next = STRING_MAP_SENTINEL;
|
e.next = MAP_SENTINEL;
|
||||||
array_add(&h->entries, e);
|
array_add(&h->entries, e);
|
||||||
return cast(StringMapIndex)(h->entries.count-1);
|
return cast(MapIndex)(h->entries.count-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) {
|
gb_internal MapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) {
|
||||||
StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (h->hashes.count != 0) {
|
if (h->hashes.count != 0) {
|
||||||
fr.hash_index = cast(StringMapIndex)(key.hash & (h->hashes.count-1));
|
fr.hash_index = cast(MapIndex)(key.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 != STRING_MAP_SENTINEL) {
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
|
if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
@@ -116,12 +99,12 @@ gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
|
gb_internal MapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
|
||||||
StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (h->hashes.count != 0) {
|
if (h->hashes.count != 0) {
|
||||||
fr.hash_index = cast(StringMapIndex)(e->key.hash & (h->hashes.count-1));
|
fr.hash_index = cast(MapIndex)(e->key.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 != STRING_MAP_SENTINEL) {
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
if (&h->entries.data[fr.entry_index] == e) {
|
if (&h->entries.data[fr.entry_index] == e) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
@@ -147,17 +130,17 @@ gb_inline void string_map_grow(StringMap<T> *h) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
void string_map_reset_entries(StringMap<T> *h) {
|
void string_map_reset_entries(StringMap<T> *h) {
|
||||||
for (isize i = 0; i < h->hashes.count; i++) {
|
for (isize i = 0; i < h->hashes.count; i++) {
|
||||||
h->hashes.data[i] = STRING_MAP_SENTINEL;
|
h->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
for (isize i = 0; i < h->entries.count; i++) {
|
for (isize i = 0; i < h->entries.count; i++) {
|
||||||
StringMapFindResult fr;
|
MapFindResult fr;
|
||||||
StringMapEntry<T> *e = &h->entries.data[i];
|
StringMapEntry<T> *e = &h->entries.data[i];
|
||||||
e->next = STRING_MAP_SENTINEL;
|
e->next = MAP_SENTINEL;
|
||||||
fr = string_map__find_from_entry(h, e);
|
fr = string_map__find_from_entry(h, e);
|
||||||
if (fr.entry_prev == STRING_MAP_SENTINEL) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
h->hashes[fr.hash_index] = cast(StringMapIndex)i;
|
h->hashes[fr.hash_index] = cast(MapIndex)i;
|
||||||
} else {
|
} else {
|
||||||
h->entries[fr.entry_prev].next = cast(StringMapIndex)i;
|
h->entries[fr.entry_prev].next = cast(MapIndex)i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +164,7 @@ void string_map_rehash(StringMap<T> *h, isize new_count) {
|
|||||||
template <typename T>
|
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 != STRING_MAP_SENTINEL) {
|
if (index != MAP_SENTINEL) {
|
||||||
return &h->entries.data[index].value;
|
return &h->entries.data[index].value;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -200,7 +183,7 @@ gb_inline T *string_map_get(StringMap<T> *h, char const *key) {
|
|||||||
template <typename T>
|
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 != STRING_MAP_SENTINEL);
|
GB_ASSERT(index != MAP_SENTINEL);
|
||||||
return h->entries.data[index].value;
|
return h->entries.data[index].value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,17 +199,17 @@ gb_inline T &string_map_must_get(StringMap<T> *h, char const *key) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) {
|
void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) {
|
||||||
StringMapIndex index;
|
MapIndex index;
|
||||||
StringMapFindResult fr;
|
MapFindResult fr;
|
||||||
if (h->hashes.count == 0) {
|
if (h->hashes.count == 0) {
|
||||||
string_map_grow(h);
|
string_map_grow(h);
|
||||||
}
|
}
|
||||||
fr = string_map__find(h, key);
|
fr = string_map__find(h, key);
|
||||||
if (fr.entry_index != STRING_MAP_SENTINEL) {
|
if (fr.entry_index != MAP_SENTINEL) {
|
||||||
index = fr.entry_index;
|
index = fr.entry_index;
|
||||||
} else {
|
} else {
|
||||||
index = string_map__add_entry(h, key);
|
index = string_map__add_entry(h, key);
|
||||||
if (fr.entry_prev != STRING_MAP_SENTINEL) {
|
if (fr.entry_prev != MAP_SENTINEL) {
|
||||||
h->entries.data[fr.entry_prev].next = index;
|
h->entries.data[fr.entry_prev].next = index;
|
||||||
} else {
|
} else {
|
||||||
h->hashes.data[fr.hash_index] = index;
|
h->hashes.data[fr.hash_index] = index;
|
||||||
@@ -251,9 +234,9 @@ gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value)
|
|||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) {
|
void string_map__erase(StringMap<T> *h, MapFindResult const &fr) {
|
||||||
StringMapFindResult last;
|
MapFindResult last;
|
||||||
if (fr.entry_prev == STRING_MAP_SENTINEL) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
|
h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next;
|
||||||
} else {
|
} else {
|
||||||
h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
|
h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next;
|
||||||
@@ -264,7 +247,7 @@ void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) {
|
|||||||
}
|
}
|
||||||
h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1];
|
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);
|
last = string_map__find(h, h->entries.data[fr.entry_index].key);
|
||||||
if (last.entry_prev != STRING_MAP_SENTINEL) {
|
if (last.entry_prev != MAP_SENTINEL) {
|
||||||
h->entries.data[last.entry_prev].next = fr.entry_index;
|
h->entries.data[last.entry_prev].next = fr.entry_index;
|
||||||
} else {
|
} else {
|
||||||
h->hashes.data[last.hash_index] = fr.entry_index;
|
h->hashes.data[last.hash_index] = fr.entry_index;
|
||||||
@@ -273,8 +256,8 @@ void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void string_map_remove(StringMap<T> *h, StringHashKey const &key) {
|
void string_map_remove(StringMap<T> *h, StringHashKey const &key) {
|
||||||
StringMapFindResult fr = string_map__find(h, key);
|
MapFindResult fr = string_map__find(h, key);
|
||||||
if (fr.entry_index != STRING_MAP_SENTINEL) {
|
if (fr.entry_index != MAP_SENTINEL) {
|
||||||
string_map__erase(h, fr);
|
string_map__erase(h, fr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,7 +266,7 @@ template <typename T>
|
|||||||
gb_inline void string_map_clear(StringMap<T> *h) {
|
gb_inline void string_map_clear(StringMap<T> *h) {
|
||||||
array_clear(&h->entries);
|
array_clear(&h->entries);
|
||||||
for (isize i = 0; i < h->hashes.count; i++) {
|
for (isize i = 0; i < h->hashes.count; i++) {
|
||||||
h->hashes.data[i] = STRING_MAP_SENTINEL;
|
h->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+30
-34
@@ -1,17 +1,11 @@
|
|||||||
struct StringSetFindResult {
|
|
||||||
isize hash_index;
|
|
||||||
isize entry_prev;
|
|
||||||
isize entry_index;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct StringSetEntry {
|
struct StringSetEntry {
|
||||||
u64 hash;
|
u32 hash;
|
||||||
isize next;
|
MapIndex next;
|
||||||
String value;
|
String value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StringSet {
|
struct StringSet {
|
||||||
Array<isize> hashes;
|
Array<MapIndex> hashes;
|
||||||
Array<StringSetEntry> entries;
|
Array<StringSetEntry> entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -36,22 +30,22 @@ gb_inline void string_set_destroy(StringSet *s) {
|
|||||||
array_free(&s->hashes);
|
array_free(&s->hashes);
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_internal isize string_set__add_entry(StringSet *s, StringHashKey const &key) {
|
gb_internal MapIndex string_set__add_entry(StringSet *s, StringHashKey const &key) {
|
||||||
StringSetEntry e = {};
|
StringSetEntry e = {};
|
||||||
e.hash = key.hash;
|
e.hash = key.hash;
|
||||||
e.next = -1;
|
e.next = MAP_SENTINEL;
|
||||||
e.value = key.string;
|
e.value = key.string;
|
||||||
array_add(&s->entries, e);
|
array_add(&s->entries, e);
|
||||||
return s->entries.count-1;
|
return cast(MapIndex)(s->entries.count-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_internal StringSetFindResult string_set__find(StringSet *s, StringHashKey const &key) {
|
gb_internal MapFindResult string_set__find(StringSet *s, StringHashKey const &key) {
|
||||||
StringSetFindResult fr = {-1, -1, -1};
|
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
|
||||||
if (s->hashes.count > 0) {
|
if (s->hashes.count > 0) {
|
||||||
// fr.hash_index = u128_to_i64(key.key % u128_from_i64(s->hashes.count));
|
// fr.hash_index = u128_to_i64(key.key % u128_from_i64(s->hashes.count));
|
||||||
fr.hash_index = key.hash % s->hashes.count;
|
fr.hash_index = cast(MapIndex)(((u64)key.hash) % s->hashes.count);
|
||||||
fr.entry_index = s->hashes[fr.hash_index];
|
fr.entry_index = s->hashes[fr.hash_index];
|
||||||
while (fr.entry_index >= 0) {
|
while (fr.entry_index != MAP_SENTINEL) {
|
||||||
auto const &entry = s->entries[fr.entry_index];
|
auto const &entry = s->entries[fr.entry_index];
|
||||||
if (entry.hash == key.hash && entry.value == key.string) {
|
if (entry.hash == key.hash && entry.value == key.string) {
|
||||||
return fr;
|
return fr;
|
||||||
@@ -79,21 +73,21 @@ void string_set_rehash(StringSet *s, isize new_count) {
|
|||||||
array_resize(&ns.hashes, new_count);
|
array_resize(&ns.hashes, new_count);
|
||||||
array_reserve(&ns.entries, s->entries.count);
|
array_reserve(&ns.entries, s->entries.count);
|
||||||
for (i = 0; i < new_count; i++) {
|
for (i = 0; i < new_count; i++) {
|
||||||
ns.hashes[i] = -1;
|
ns.hashes[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
for (i = 0; i < s->entries.count; i++) {
|
for (i = 0; i < s->entries.count; i++) {
|
||||||
StringSetEntry *e = &s->entries[i];
|
StringSetEntry *e = &s->entries[i];
|
||||||
StringSetFindResult fr;
|
MapFindResult fr;
|
||||||
if (ns.hashes.count == 0) {
|
if (ns.hashes.count == 0) {
|
||||||
string_set_grow(&ns);
|
string_set_grow(&ns);
|
||||||
}
|
}
|
||||||
StringHashKey key = {e->hash, e->value};
|
StringHashKey key = {e->hash, e->value};
|
||||||
fr = string_set__find(&ns, key);
|
fr = string_set__find(&ns, key);
|
||||||
j = string_set__add_entry(&ns, key);
|
j = string_set__add_entry(&ns, key);
|
||||||
if (fr.entry_prev < 0) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
ns.hashes[fr.hash_index] = j;
|
ns.hashes[fr.hash_index] = cast(MapIndex)j;
|
||||||
} else {
|
} else {
|
||||||
ns.entries[fr.entry_prev].next = j;
|
ns.entries[fr.entry_prev].next = cast(MapIndex)j;
|
||||||
}
|
}
|
||||||
ns.entries[j].next = fr.entry_index;
|
ns.entries[j].next = fr.entry_index;
|
||||||
ns.entries[j].value = e->value;
|
ns.entries[j].value = e->value;
|
||||||
@@ -108,22 +102,22 @@ void string_set_rehash(StringSet *s, isize new_count) {
|
|||||||
gb_inline bool string_set_exists(StringSet *s, String const &str) {
|
gb_inline bool string_set_exists(StringSet *s, String const &str) {
|
||||||
StringHashKey key = string_hash_string(str);
|
StringHashKey key = string_hash_string(str);
|
||||||
isize index = string_set__find(s, key).entry_index;
|
isize index = string_set__find(s, key).entry_index;
|
||||||
return index >= 0;
|
return index != MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_set_add(StringSet *s, String const &str) {
|
void string_set_add(StringSet *s, String const &str) {
|
||||||
isize index;
|
MapIndex index;
|
||||||
StringSetFindResult fr;
|
MapFindResult fr;
|
||||||
StringHashKey key = string_hash_string(str);
|
StringHashKey key = string_hash_string(str);
|
||||||
if (s->hashes.count == 0) {
|
if (s->hashes.count == 0) {
|
||||||
string_set_grow(s);
|
string_set_grow(s);
|
||||||
}
|
}
|
||||||
fr = string_set__find(s, key);
|
fr = string_set__find(s, key);
|
||||||
if (fr.entry_index >= 0) {
|
if (fr.entry_index != MAP_SENTINEL) {
|
||||||
index = fr.entry_index;
|
index = fr.entry_index;
|
||||||
} else {
|
} else {
|
||||||
index = string_set__add_entry(s, key);
|
index = string_set__add_entry(s, key);
|
||||||
if (fr.entry_prev >= 0) {
|
if (fr.entry_prev != MAP_SENTINEL) {
|
||||||
s->entries[fr.entry_prev].next = index;
|
s->entries[fr.entry_prev].next = index;
|
||||||
} else {
|
} else {
|
||||||
s->hashes[fr.hash_index] = index;
|
s->hashes[fr.hash_index] = index;
|
||||||
@@ -137,9 +131,9 @@ void string_set_add(StringSet *s, String const &str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void string_set__erase(StringSet *s, StringSetFindResult fr) {
|
void string_set__erase(StringSet *s, MapFindResult fr) {
|
||||||
StringSetFindResult last;
|
MapFindResult last;
|
||||||
if (fr.entry_prev < 0) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
s->hashes[fr.hash_index] = s->entries[fr.entry_index].next;
|
s->hashes[fr.hash_index] = s->entries[fr.entry_index].next;
|
||||||
} else {
|
} else {
|
||||||
s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next;
|
s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next;
|
||||||
@@ -152,7 +146,7 @@ void string_set__erase(StringSet *s, StringSetFindResult fr) {
|
|||||||
*entry = s->entries[s->entries.count-1];
|
*entry = s->entries[s->entries.count-1];
|
||||||
StringHashKey key = {entry->hash, entry->value};
|
StringHashKey key = {entry->hash, entry->value};
|
||||||
last = string_set__find(s, key);
|
last = string_set__find(s, key);
|
||||||
if (last.entry_prev >= 0) {
|
if (last.entry_prev != MAP_SENTINEL) {
|
||||||
s->entries[last.entry_prev].next = fr.entry_index;
|
s->entries[last.entry_prev].next = fr.entry_index;
|
||||||
} else {
|
} else {
|
||||||
s->hashes[last.hash_index] = fr.entry_index;
|
s->hashes[last.hash_index] = fr.entry_index;
|
||||||
@@ -161,13 +155,15 @@ void string_set__erase(StringSet *s, StringSetFindResult fr) {
|
|||||||
|
|
||||||
void string_set_remove(StringSet *s, String const &str) {
|
void string_set_remove(StringSet *s, String const &str) {
|
||||||
StringHashKey key = string_hash_string(str);
|
StringHashKey key = string_hash_string(str);
|
||||||
StringSetFindResult fr = string_set__find(s, key);
|
MapFindResult fr = string_set__find(s, key);
|
||||||
if (fr.entry_index >= 0) {
|
if (fr.entry_index != MAP_SENTINEL) {
|
||||||
string_set__erase(s, fr);
|
string_set__erase(s, fr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_inline void string_set_clear(StringSet *s) {
|
gb_inline void string_set_clear(StringSet *s) {
|
||||||
array_clear(&s->hashes);
|
|
||||||
array_clear(&s->entries);
|
array_clear(&s->entries);
|
||||||
|
for_array(i, s->hashes) {
|
||||||
|
s->hashes.data[i] = MAP_SENTINEL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user