mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Change map index types to u32 from isize
This commit is contained in:
+13
-14
@@ -8,7 +8,7 @@
|
|||||||
#define MAP_UTIL_STUFF
|
#define MAP_UTIL_STUFF
|
||||||
// NOTE(bill): This util stuff is the same for every `Map`
|
// NOTE(bill): This util stuff is the same for every `Map`
|
||||||
|
|
||||||
typedef isize MapIndex;
|
typedef u32 MapIndex;
|
||||||
|
|
||||||
struct MapFindResult {
|
struct MapFindResult {
|
||||||
MapIndex hash_index;
|
MapIndex hash_index;
|
||||||
@@ -58,13 +58,13 @@ gb_inline bool operator!=(HashKey a, HashKey b) { return !hash_key_equal(a, b);
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct MapEntry {
|
struct MapEntry {
|
||||||
HashKey key;
|
HashKey key;
|
||||||
isize next;
|
MapIndex next;
|
||||||
T value;
|
T value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct Map {
|
struct Map {
|
||||||
Slice<isize> hashes;
|
Slice<MapIndex> hashes;
|
||||||
Array<MapEntry<T> > entries;
|
Array<MapEntry<T> > entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -109,12 +109,12 @@ gb_inline void map_destroy(Map<T> *h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal isize map__add_entry(Map<T> *h, HashKey const &key) {
|
gb_internal MapIndex map__add_entry(Map<T> *h, HashKey const &key) {
|
||||||
MapEntry<T> e = {};
|
MapEntry<T> e = {};
|
||||||
e.key = key;
|
e.key = key;
|
||||||
e.next = MAP_SENTINEL;
|
e.next = MAP_SENTINEL;
|
||||||
array_add(&h->entries, e);
|
array_add(&h->entries, e);
|
||||||
return h->entries.count-1;
|
return cast(MapIndex)(h->entries.count-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -123,7 +123,7 @@ gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) {
|
|||||||
if (h->hashes.count == 0) {
|
if (h->hashes.count == 0) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
fr.hash_index = key.key & (h->hashes.count-1);
|
fr.hash_index = cast(MapIndex)(key.key & (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 (hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
|
if (hash_key_equal(h->entries.data[fr.entry_index].key, key)) {
|
||||||
@@ -141,7 +141,7 @@ gb_internal MapFindResult map__find_from_entry(Map<T> *h, MapEntry<T> *e) {
|
|||||||
if (h->hashes.count == 0) {
|
if (h->hashes.count == 0) {
|
||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
fr.hash_index = e->key.key & (h->hashes.count-1);
|
fr.hash_index = cast(MapIndex)(e->key.key & (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] == e) {
|
if (&h->entries.data[fr.entry_index] == e) {
|
||||||
@@ -166,19 +166,18 @@ gb_inline void map_grow(Map<T> *h) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void map_reset_entries(Map<T> *h) {
|
void map_reset_entries(Map<T> *h) {
|
||||||
isize i;
|
for (isize i = 0; i < h->hashes.count; i++) {
|
||||||
for (i = 0; i < h->hashes.count; i++) {
|
|
||||||
h->hashes.data[i] = MAP_SENTINEL;
|
h->hashes.data[i] = MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
for (i = 0; i < h->entries.count; i++) {
|
for (isize i = 0; i < h->entries.count; i++) {
|
||||||
MapFindResult fr;
|
MapFindResult fr;
|
||||||
MapEntry<T> *e = &h->entries.data[i];
|
MapEntry<T> *e = &h->entries.data[i];
|
||||||
e->next = MAP_SENTINEL;
|
e->next = MAP_SENTINEL;
|
||||||
fr = map__find_from_entry(h, e);
|
fr = map__find_from_entry(h, e);
|
||||||
if (fr.entry_prev == MAP_SENTINEL) {
|
if (fr.entry_prev == MAP_SENTINEL) {
|
||||||
h->hashes[fr.hash_index] = i;
|
h->hashes[fr.hash_index] = cast(MapIndex)i;
|
||||||
} else {
|
} else {
|
||||||
h->entries[fr.entry_prev].next = i;
|
h->entries[fr.entry_prev].next = cast(MapIndex)i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,7 +216,7 @@ T &map_must_get(Map<T> *h, HashKey const &key) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void map_set(Map<T> *h, HashKey const &key, T const &value) {
|
void map_set(Map<T> *h, HashKey const &key, T const &value) {
|
||||||
isize index;
|
MapIndex index;
|
||||||
MapFindResult fr;
|
MapFindResult fr;
|
||||||
if (h->hashes.count == 0) {
|
if (h->hashes.count == 0) {
|
||||||
map_grow(h);
|
map_grow(h);
|
||||||
@@ -327,7 +326,7 @@ void multi_map_get_all(Map<T> *h, HashKey const &key, T *items) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) {
|
void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) {
|
||||||
MapFindResult fr;
|
MapFindResult fr;
|
||||||
isize i;
|
MapIndex i;
|
||||||
if (h->hashes.count == 0) {
|
if (h->hashes.count == 0) {
|
||||||
map_grow(h);
|
map_grow(h);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-5
@@ -111,19 +111,18 @@ 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) {
|
||||||
PtrSetIndex i;
|
for (isize i = 0; i < s->hashes.count; i++) {
|
||||||
for (i = 0; i < cast(PtrSetIndex)s->hashes.count; i++) {
|
|
||||||
s->hashes.data[i] = PTR_SET_SENTINEL;
|
s->hashes.data[i] = PTR_SET_SENTINEL;
|
||||||
}
|
}
|
||||||
for (i = 0; i < cast(PtrSetIndex)s->entries.count; i++) {
|
for (isize i = 0; i < s->entries.count; i++) {
|
||||||
PtrSetFindResult fr;
|
PtrSetFindResult fr;
|
||||||
PtrSetEntry<T> *e = &s->entries.data[i];
|
PtrSetEntry<T> *e = &s->entries.data[i];
|
||||||
e->next = PTR_SET_SENTINEL;
|
e->next = PTR_SET_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 == PTR_SET_SENTINEL) {
|
||||||
s->hashes[fr.hash_index] = i;
|
s->hashes[fr.hash_index] = cast(PtrSetIndex)i;
|
||||||
} else {
|
} else {
|
||||||
s->entries[fr.entry_prev].next = i;
|
s->entries[fr.entry_prev].next = cast(PtrSetIndex)i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-15
@@ -1,6 +1,6 @@
|
|||||||
// NOTE(bill): This util stuff is the same for every `Map`
|
// NOTE(bill): This util stuff is the same for every `Map`
|
||||||
|
|
||||||
typedef isize StringMapIndex;
|
typedef u32 StringMapIndex;
|
||||||
|
|
||||||
struct StringMapFindResult {
|
struct StringMapFindResult {
|
||||||
StringMapIndex hash_index;
|
StringMapIndex hash_index;
|
||||||
@@ -40,14 +40,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;
|
||||||
isize next;
|
StringMapIndex next;
|
||||||
T value;
|
T value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct StringMap {
|
struct StringMap {
|
||||||
Slice<isize> hashes;
|
Slice<StringMapIndex> hashes;
|
||||||
Array<StringMapEntry<T> > entries;
|
Array<StringMapEntry<T> > entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -90,19 +90,19 @@ gb_inline void string_map_destroy(StringMap<T> *h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal isize string_map__add_entry(StringMap<T> *h, StringHashKey const &key) {
|
gb_internal StringMapIndex 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 = STRING_MAP_SENTINEL;
|
||||||
array_add(&h->entries, e);
|
array_add(&h->entries, e);
|
||||||
return h->entries.count-1;
|
return cast(StringMapIndex)(h->entries.count-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) {
|
gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) {
|
||||||
StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
|
StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
|
||||||
if (h->hashes.count != 0) {
|
if (h->hashes.count != 0) {
|
||||||
fr.hash_index = key.hash & (h->hashes.count-1);
|
fr.hash_index = cast(StringMapIndex)(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 != STRING_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)) {
|
||||||
@@ -119,7 +119,7 @@ template <typename T>
|
|||||||
gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
|
gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
|
||||||
StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
|
StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL};
|
||||||
if (h->hashes.count != 0) {
|
if (h->hashes.count != 0) {
|
||||||
fr.hash_index = e->key.hash & (h->hashes.count-1);
|
fr.hash_index = cast(StringMapIndex)(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 != STRING_MAP_SENTINEL) {
|
||||||
if (&h->entries.data[fr.entry_index] == e) {
|
if (&h->entries.data[fr.entry_index] == e) {
|
||||||
@@ -146,19 +146,18 @@ 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) {
|
||||||
isize i;
|
for (isize i = 0; i < h->hashes.count; i++) {
|
||||||
for (i = 0; i < h->hashes.count; i++) {
|
|
||||||
h->hashes.data[i] = STRING_MAP_SENTINEL;
|
h->hashes.data[i] = STRING_MAP_SENTINEL;
|
||||||
}
|
}
|
||||||
for (i = 0; i < h->entries.count; i++) {
|
for (isize i = 0; i < h->entries.count; i++) {
|
||||||
StringMapFindResult fr;
|
StringMapFindResult fr;
|
||||||
StringMapEntry<T> *e = &h->entries.data[i];
|
StringMapEntry<T> *e = &h->entries.data[i];
|
||||||
e->next = STRING_MAP_SENTINEL;
|
e->next = STRING_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 == STRING_MAP_SENTINEL) {
|
||||||
h->hashes[fr.hash_index] = i;
|
h->hashes[fr.hash_index] = cast(StringMapIndex)i;
|
||||||
} else {
|
} else {
|
||||||
h->entries[fr.entry_prev].next = i;
|
h->entries[fr.entry_prev].next = cast(StringMapIndex)i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,7 +216,7 @@ 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) {
|
||||||
isize index;
|
StringMapIndex index;
|
||||||
StringMapFindResult fr;
|
StringMapFindResult fr;
|
||||||
if (h->hashes.count == 0) {
|
if (h->hashes.count == 0) {
|
||||||
string_map_grow(h);
|
string_map_grow(h);
|
||||||
|
|||||||
Reference in New Issue
Block a user