diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2023-01-01 13:26:43 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-01 13:26:43 +0000 |
| commit | 28fb35f2f7a6ffd75e76dd95352f4194d79b3166 (patch) | |
| tree | eac021b897fe6525a076264d5545aac6c96cfbb5 /src/string_map.cpp | |
| parent | 547c7bce1b28757415c553830a18d94636cedbf8 (diff) | |
| parent | c1384afe2fd705ce075277aa8dc6bc259dc94cdc (diff) | |
Merge pull request #2263 from odin-lang/compiler-improvements-2022-12
Compiler Improvements for 2022-12
Diffstat (limited to 'src/string_map.cpp')
| -rw-r--r-- | src/string_map.cpp | 95 |
1 files changed, 58 insertions, 37 deletions
diff --git a/src/string_map.cpp b/src/string_map.cpp index 218a45482..9f9374ece 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -3,7 +3,7 @@ struct StringHashKey { String string; }; -gb_inline StringHashKey string_hash_string(String const &s) { +gb_internal gb_inline StringHashKey string_hash_string(String const &s) { StringHashKey hash_key = {}; hash_key.hash = fnv32a(s.text, s.len); hash_key.string = s; @@ -11,15 +11,13 @@ gb_inline StringHashKey string_hash_string(String const &s) { } -bool string_hash_key_equal(StringHashKey const &a, StringHashKey const &b) { +gb_internal gb_inline bool string_hash_key_equal(StringHashKey const &a, StringHashKey const &b) { if (a.hash == b.hash) { // NOTE(bill): If two string's hashes collide, compare the strings themselves return a.string == b.string; } return false; } -bool operator==(StringHashKey const &a, StringHashKey const &b) { return string_hash_key_equal(a, b); } -bool operator!=(StringHashKey const &a, StringHashKey const &b) { return !string_hash_key_equal(a, b); } template <typename T> struct StringMapEntry { @@ -30,34 +28,36 @@ struct StringMapEntry { template <typename T> struct StringMap { + using K = String; + using V = T; Slice<MapIndex> hashes; Array<StringMapEntry<T> > entries; }; -template <typename T> void string_map_init (StringMap<T> *h, gbAllocator a, isize capacity = 16); -template <typename T> void string_map_destroy (StringMap<T> *h); +template <typename T> gb_internal void string_map_init (StringMap<T> *h, gbAllocator a, isize capacity = 16); +template <typename T> gb_internal void string_map_destroy (StringMap<T> *h); -template <typename T> T * string_map_get (StringMap<T> *h, char const *key); -template <typename T> T * string_map_get (StringMap<T> *h, String const &key); -template <typename T> T * string_map_get (StringMap<T> *h, StringHashKey const &key); +template <typename T> gb_internal T * string_map_get (StringMap<T> *h, char const *key); +template <typename T> gb_internal T * string_map_get (StringMap<T> *h, String const &key); +template <typename T> gb_internal T * string_map_get (StringMap<T> *h, StringHashKey const &key); -template <typename T> T & string_map_must_get (StringMap<T> *h, char const *key); -template <typename T> T & string_map_must_get (StringMap<T> *h, String const &key); -template <typename T> T & string_map_must_get (StringMap<T> *h, StringHashKey const &key); +template <typename T> gb_internal T & string_map_must_get (StringMap<T> *h, char const *key); +template <typename T> gb_internal T & string_map_must_get (StringMap<T> *h, String const &key); +template <typename T> gb_internal T & string_map_must_get (StringMap<T> *h, StringHashKey const &key); -template <typename T> void string_map_set (StringMap<T> *h, StringHashKey const &key, T const &value); -template <typename T> void string_map_set (StringMap<T> *h, String const &key, T const &value); -template <typename T> void string_map_set (StringMap<T> *h, char const *key, T const &value); +template <typename T> gb_internal void string_map_set (StringMap<T> *h, StringHashKey const &key, T const &value); +template <typename T> gb_internal void string_map_set (StringMap<T> *h, String const &key, T const &value); +template <typename T> gb_internal void string_map_set (StringMap<T> *h, char const *key, T const &value); -template <typename T> void string_map_remove (StringMap<T> *h, StringHashKey const &key); -template <typename T> void string_map_clear (StringMap<T> *h); -template <typename T> void string_map_grow (StringMap<T> *h); -template <typename T> void string_map_rehash (StringMap<T> *h, isize new_count); -template <typename T> void string_map_reserve (StringMap<T> *h, isize cap); +template <typename T> gb_internal void string_map_remove (StringMap<T> *h, StringHashKey const &key); +template <typename T> gb_internal void string_map_clear (StringMap<T> *h); +template <typename T> gb_internal void string_map_grow (StringMap<T> *h); +template <typename T> gb_internal void string_map_rehash (StringMap<T> *h, isize new_count); +template <typename T> gb_internal void string_map_reserve (StringMap<T> *h, isize cap); template <typename T> -gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) { +gb_internal gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) { capacity = next_pow2_isize(capacity); slice_init(&h->hashes, a, capacity); array_init(&h->entries, a, 0, capacity); @@ -67,7 +67,7 @@ gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) { } template <typename T> -gb_inline void string_map_destroy(StringMap<T> *h) { +gb_internal gb_inline void string_map_destroy(StringMap<T> *h) { slice_free(&h->hashes, h->entries.allocator); array_free(&h->entries); } @@ -128,7 +128,7 @@ gb_inline void string_map_grow(StringMap<T> *h) { template <typename T> -void string_map_reset_entries(StringMap<T> *h) { +gb_internal void string_map_reset_entries(StringMap<T> *h) { for (isize i = 0; i < h->hashes.count; i++) { h->hashes.data[i] = MAP_SENTINEL; } @@ -146,7 +146,7 @@ void string_map_reset_entries(StringMap<T> *h) { } template <typename T> -void string_map_reserve(StringMap<T> *h, isize cap) { +gb_internal void string_map_reserve(StringMap<T> *h, isize cap) { array_reserve(&h->entries, cap); if (h->entries.count*2 < h->hashes.count) { return; @@ -157,12 +157,12 @@ void string_map_reserve(StringMap<T> *h, isize cap) { template <typename T> -void string_map_rehash(StringMap<T> *h, isize new_count) { +gb_internal void string_map_rehash(StringMap<T> *h, isize new_count) { string_map_reserve(h, new_count); } template <typename T> -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; if (index != MAP_SENTINEL) { return &h->entries.data[index].value; @@ -171,34 +171,34 @@ T *string_map_get(StringMap<T> *h, StringHashKey const &key) { } template <typename T> -gb_inline T *string_map_get(StringMap<T> *h, String const &key) { +gb_internal gb_inline T *string_map_get(StringMap<T> *h, String const &key) { return string_map_get(h, string_hash_string(key)); } template <typename T> -gb_inline T *string_map_get(StringMap<T> *h, char const *key) { +gb_internal gb_inline T *string_map_get(StringMap<T> *h, char const *key) { return string_map_get(h, string_hash_string(make_string_c(key))); } template <typename T> -T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) { +gb_internal T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) { isize index = string_map__find(h, key).entry_index; GB_ASSERT(index != MAP_SENTINEL); return h->entries.data[index].value; } template <typename T> -gb_inline T &string_map_must_get(StringMap<T> *h, String const &key) { +gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, String const &key) { return string_map_must_get(h, string_hash_string(key)); } template <typename T> -gb_inline T &string_map_must_get(StringMap<T> *h, char const *key) { +gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, char const *key) { return string_map_must_get(h, string_hash_string(make_string_c(key))); } template <typename T> -void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) { +gb_internal void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) { MapIndex index; MapFindResult fr; if (h->hashes.count == 0) { @@ -223,18 +223,18 @@ void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) { } template <typename T> -gb_inline void string_map_set(StringMap<T> *h, String const &key, T const &value) { +gb_internal gb_inline void string_map_set(StringMap<T> *h, String const &key, T const &value) { string_map_set(h, string_hash_string(key), value); } template <typename T> -gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value) { +gb_internal gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value) { string_map_set(h, string_hash_string(make_string_c(key)), value); } template <typename T> -void string_map__erase(StringMap<T> *h, MapFindResult const &fr) { +gb_internal void string_map__erase(StringMap<T> *h, MapFindResult const &fr) { MapFindResult last; if (fr.entry_prev == MAP_SENTINEL) { h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; @@ -255,7 +255,7 @@ void string_map__erase(StringMap<T> *h, MapFindResult const &fr) { } template <typename T> -void string_map_remove(StringMap<T> *h, StringHashKey const &key) { +gb_internal void string_map_remove(StringMap<T> *h, StringHashKey const &key) { MapFindResult fr = string_map__find(h, key); if (fr.entry_index != MAP_SENTINEL) { string_map__erase(h, fr); @@ -263,10 +263,31 @@ void string_map_remove(StringMap<T> *h, StringHashKey const &key) { } template <typename T> -gb_inline void string_map_clear(StringMap<T> *h) { +gb_internal gb_inline void string_map_clear(StringMap<T> *h) { array_clear(&h->entries); for (isize i = 0; i < h->hashes.count; i++) { h->hashes.data[i] = MAP_SENTINEL; } } + + +template <typename T> +gb_internal StringMapEntry<T> *begin(StringMap<T> &m) { + return m.entries.data; +} +template <typename T> +gb_internal StringMapEntry<T> const *begin(StringMap<T> const &m) { + return m.entries.data; +} + + +template <typename T> +gb_internal StringMapEntry<T> *end(StringMap<T> &m) { + return m.entries.data + m.entries.count; +} + +template <typename T> +gb_internal StringMapEntry<T> const *end(StringMap<T> const &m) { + return m.entries.data + m.entries.count; +}
\ No newline at end of file |