TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_id_map.h
Go to the documentation of this file.
1#pragma once
2
3#include <gui_core/gui_id.h>
4
5#include <bit>
6#include <cassert>
7#include <cstddef>
8#include <cstdint>
9#include <utility>
10#include <vector>
11
12namespace tachyon
13{
16
21 template<typename T>
22 class IDMap
23 {
24 public:
26 T* Find(ID key)
27 {
28 if (m_size == 0 || key == ID::Invalid())
29 return nullptr;
30 for (size_t i = Home(key);; i = (i + 1) & Mask())
31 {
32 Slot& slot = m_slots[i];
33 if (slot.key == key) return &slot.value;
34 if (slot.key == ID::Invalid()) return nullptr;
35 }
36 }
37 const T* Find(ID key) const { return const_cast<IDMap*>(this)->Find(key); }
38
42 std::pair<T*, bool> TryEmplace(ID key, const T& value)
43 {
44 assert(key != ID::Invalid());
45 if (key == ID::Invalid())
46 return { nullptr, false };
47 if ((m_size + 1) * 2 > m_slots.size())
48 Grow();
49 for (size_t i = Home(key);; i = (i + 1) & Mask())
50 {
51 Slot& slot = m_slots[i];
52 if (slot.key == key)
53 return { &slot.value, false };
54 if (slot.key == ID::Invalid())
55 {
56 slot = Slot{ key, value };
57 ++m_size;
58 return { &slot.value, true };
59 }
60 }
61 }
62
65 void Clear()
66 {
67 if (m_size == 0)
68 return;
69 if (m_slots.size() > kMinCapacity && m_size * 16 < m_slots.size())
70 {
71 size_t capacity = kMinCapacity;
72 while (capacity < m_size * 4)
73 capacity *= 2;
74 std::vector<Slot>(capacity).swap(m_slots);
75 m_shift = 64u - uint32_t(std::countr_zero(capacity));
76 m_size = 0;
77 return;
78 }
79 for (Slot& slot : m_slots)
80 slot.key = ID::Invalid();
81 m_size = 0;
82 }
83
85 template<typename Visit>
86 void ForEach(Visit&& visit)
87 {
88 if (m_size == 0)
89 return;
90 for (Slot& slot : m_slots)
91 if (slot.key != ID::Invalid())
92 visit(slot.key, slot.value);
93 }
94
97 template<typename Erase>
98 void EraseIf(Erase&& erase)
99 {
100 if (m_size == 0)
101 return;
102 m_kept.clear();
103 for (Slot& slot : m_slots)
104 if (slot.key != ID::Invalid())
105 {
106 if (!erase(slot.key, slot.value))
107 m_kept.push_back(slot);
108 slot.key = ID::Invalid();
109 }
110 m_size = 0;
111 for (const Slot& slot : m_kept)
112 TryEmplace(slot.key, slot.value);
113 }
114
115 size_t Size() const { return m_size; }
116 size_t Capacity() const { return m_slots.size(); }
117
118 private:
119 struct Slot
120 {
121 ID key = ID::Invalid();
122 T value {};
123 };
124
125 static constexpr size_t kMinCapacity = 64;
126
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); }
131
132 void Grow()
133 {
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));
138 m_size = 0;
139 for (const Slot& slot : old)
140 if (slot.key != ID::Invalid())
141 TryEmplace(slot.key, slot.value);
142 }
143
144 std::vector<Slot> m_slots;
145 std::vector<Slot> m_kept;
146 size_t m_size = 0;
147 uint32_t m_shift = 64;
148 };
149
151} // namespace tachyon
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