diff --git a/src/common.cpp b/src/common.cpp index 5b007bf2c..d5fc1df4b 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -59,6 +59,11 @@ template struct TypeIsPtrSizedInteger { enum {value = false}; }; template <> struct TypeIsPtrSizedInteger { enum {value = true}; }; template <> struct TypeIsPtrSizedInteger { enum {value = true}; }; +template struct TypeIs64BitInteger { enum {value = false}; }; +template <> struct TypeIs64BitInteger { enum {value = true}; }; +template <> struct TypeIs64BitInteger { enum {value = true}; }; + + #include "unicode.cpp" #include "array.cpp" diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 61f703cf1..c72204a3d 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -10,11 +10,25 @@ enum { enum : MapIndex { MAP_SENTINEL = ~(MapIndex)0 }; -static void *const MAP_TOMBSTONE = (void *)~(uintptr)0; + +template +struct PtrMapConstant { + static constexpr T TOMBSTONE = (T)(void *)~(uintptr)0; +}; + +template <> +struct PtrMapConstant { + static constexpr u64 TOMBSTONE = ~(u64)0; +}; +template <> +struct PtrMapConstant { + static constexpr i64 TOMBSTONE = ~(i64)0; +}; template struct PtrMapEntry { - static_assert(sizeof(K) == sizeof(void *), "Key size must be pointer size"); + static_assert(TypeIsPointer::value || TypeIsPtrSizedInteger::value || TypeIs64BitInteger::value, + "PtrMapEntry::K must be a pointer or 8-byte integer"); K key; V value; @@ -99,7 +113,7 @@ gb_internal void map__insert(PtrMap *h, K key, V const &value) { MapIndex original_index = index; do { auto *entry = h->entries+index; - if (!entry->key || entry->key == cast(K)MAP_TOMBSTONE) { + if (!entry->key || entry->key == PtrMapConstant::TOMBSTONE) { entry->key = key; entry->value = value; h->count += 1; @@ -147,7 +161,7 @@ gb_internal void map_reserve(PtrMap *h, isize cap) { for (u32 i = 0; i < h->capacity; i++) { auto *entry = h->entries+i; if (entry->key && - entry->key != cast(K)MAP_TOMBSTONE) { + entry->key != PtrMapConstant::TOMBSTONE) { map__insert(&new_h, entry->key, entry->value); } } @@ -257,7 +271,7 @@ template gb_internal void map_remove(PtrMap *h, K key) { MapIndex found_index = 0; if (map_try_get(h, key, &found_index)) { - h->entries[found_index].key = cast(K)MAP_TOMBSTONE; + h->entries[found_index].key = PtrMapConstant::TOMBSTONE; h->count -= 1; } } @@ -367,7 +381,7 @@ struct PtrMapIterator { return *this; } PtrMapEntry *entry = map->entries+index; - if (entry->key && entry->key != cast(K)MAP_TOMBSTONE) { + if (entry->key && entry->key != PtrMapConstant::TOMBSTONE) { return *this; } } @@ -404,7 +418,7 @@ gb_internal PtrMapIterator begin(PtrMap &m) noexcept { MapIndex index = 0; while (index < m.capacity) { auto key = m.entries[index].key; - if (key && key != cast(K)MAP_TOMBSTONE) { + if (key && key != PtrMapConstant::TOMBSTONE) { break; } index++; @@ -420,7 +434,7 @@ gb_internal PtrMapIterator const begin(PtrMap const &m) noexcept { MapIndex index = 0; while (index < m.capacity) { auto key = m.entries[index].key; - if (key && key != cast(K)MAP_TOMBSTONE) { + if (key && key != PtrMapConstant::TOMBSTONE) { break; } index++;