30 for (
size_t i = Home(key);; i = (i + 1) & Mask())
32 Slot& slot = m_slots[i];
33 if (slot.key == key)
return &slot.value;
46 return {
nullptr,
false };
47 if ((m_size + 1) * 2 > m_slots.size())
49 for (
size_t i = Home(key);; i = (i + 1) & Mask())
51 Slot& slot = m_slots[i];
53 return { &slot.value,
false };
56 slot = Slot{ key, value };
58 return { &slot.value,
true };
69 if (m_slots.size() > kMinCapacity && m_size * 16 < m_slots.size())
71 size_t capacity = kMinCapacity;
72 while (capacity < m_size * 4)
74 std::vector<Slot>(capacity).swap(m_slots);
75 m_shift = 64u - uint32_t(std::countr_zero(capacity));
79 for (Slot& slot : m_slots)
85 template<
typename Visit>
90 for (Slot& slot : m_slots)
92 visit(slot.key, slot.value);
97 template<
typename Erase>
103 for (Slot& slot : m_slots)
106 if (!erase(slot.key, slot.value))
107 m_kept.push_back(slot);
111 for (
const Slot& slot : m_kept)
115 size_t Size()
const {
return m_size; }
125 static constexpr size_t kMinCapacity = 64;
127 size_t Mask()
const {
return m_slots.size() - 1; }
130 size_t Home(ID key)
const {
return size_t((key.Value() * 0x9E3779B97F4A7C15ull) >> m_shift); }
134 std::vector<Slot> old = std::move(m_slots);
135 const size_t capacity = old.empty() ? kMinCapacity : old.size() * 2;
136 m_slots.assign(capacity, Slot{});
137 m_shift = 64u - uint32_t(std::countr_zero(capacity));
139 for (
const Slot& slot : old)
144 std::vector<Slot> m_slots;
145 std::vector<Slot> m_kept;
147 uint32_t m_shift = 64;
A hash map from ID to a small value, for indexes rebuilt or pruned every frame: open addressing in on...
Definition gui_id_map.h:23
size_t Capacity() const
Definition gui_id_map.h:116
const T * Find(ID key) const
Definition gui_id_map.h:37
std::pair< T *, bool > TryEmplace(ID key, const T &value)
Adds key with value unless the key is there already.
Definition gui_id_map.h:42
void ForEach(Visit &&visit)
Calls visit(key, value) for each entry, in no particular order.
Definition gui_id_map.h:86
T * Find(ID key)
The value held for key, or null.
Definition gui_id_map.h:26
void Clear()
Empties the map, keeping its room, unless that is far more than this fill needed: after one huge fram...
Definition gui_id_map.h:65
void EraseIf(Erase &&erase)
Drops each entry erase(key, value) is true for.
Definition gui_id_map.h:98
size_t Size() const
Definition gui_id_map.h:115
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66