vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
sparse_set.h
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5#include <vector>
6#include <type_traits>
7
8#include "l_assert.h"
9
10namespace Vkm::Engine {
11
15class ISparseSet {
16 public:
17 ISparseSet() = default;
18 virtual ~ISparseSet() = default;
19
20 ISparseSet(const ISparseSet& other) = delete;
21 ISparseSet& operator=(const ISparseSet& other) = delete;
22
23 ISparseSet(ISparseSet && other) = delete;
24 ISparseSet& operator=(ISparseSet && other) = delete;
25
31 virtual void removeIfPresent(uint32_t key) = 0;
32
38 virtual void compact() = 0;
39};
40
49template<typename T>
50class SparseSet : public ISparseSet {
51 public:
52 SparseSet() = default;
53 ~SparseSet() override = default;
54
55 SparseSet(const SparseSet& other) = delete;
56 SparseSet& operator=(const SparseSet& other) = delete;
57
58 SparseSet(SparseSet && other) = delete;
59 SparseSet& operator=(SparseSet && other) = delete;
60
61 public:
68 T& add(uint32_t key, T && value) { return addInternal(key, std::move(value)); }
69 T& add(uint32_t key, const T& value) { return addInternal(key, value); }
70
75 void remove(uint32_t key) {
76 VKM_ASSERT(contains(key), "SparseSet::remove called with invalid key");
77 // Guarded too: a build without asserts would index m_data with garbage.
78 if (!contains(key)) return;
79
80 uint32_t dataIdx = m_dataIndex[key];
81 uint32_t lastIdx = static_cast<uint32_t>(m_data.size() - 1);
82
83 if (dataIdx != lastIdx) {
84 m_data[dataIdx] = std::move(m_data[lastIdx]);
85
86 m_dataId[dataIdx] = m_dataId[lastIdx];
87 m_stamps[dataIdx] = m_stamps[lastIdx];
88 m_dataIndex[m_dataId[dataIdx]] = dataIdx;
89 }
90
91 m_data.pop_back();
92 m_dataId.pop_back();
93 m_stamps.pop_back();
94 m_dataIndex[key] = EMPTY;
95 ++m_structure;
96 }
97
103 void removeIfPresent(uint32_t key) override {
104 if (contains(key)) remove(key);
105 }
106
112 bool contains(uint32_t key) const {
113 return key < m_dataIndex.size() && m_dataIndex[key] != EMPTY;
114 }
115
121 T& get(uint32_t key) {
122 VKM_ASSERT(contains(key), "SparseSet::get called with invalid key");
123 return m_data[m_dataIndex[key]];
124 }
125
126 const T& get(uint32_t key) const {
127 VKM_ASSERT(contains(key), "SparseSet::get called with invalid key");
128 return m_data[m_dataIndex[key]];
129 }
130
131 public:
142 template<typename Fn>
143 void forEach(Fn&& fn) {
144 for (uint32_t i = 0; i < m_data.size(); ++i) {
145 fn(m_dataId[i], m_data[i]);
146 }
147 }
148
149 template<typename Fn>
150 void forEach(Fn&& fn) const {
151 for (uint32_t i = 0; i < m_data.size(); ++i) {
152 fn(m_dataId[i], m_data[i]);
153 }
154 }
155
161 size_t size() const { return m_data.size(); }
162
168 void clear() {
169 m_data.clear();
170 m_dataId.clear();
171 m_stamps.clear();
172 std::fill(m_dataIndex.begin(), m_dataIndex.end(), EMPTY);
173 ++m_structure;
174 }
175
183 void compact() override {
184 if (m_data.empty()) {
185 m_dataIndex.clear();
186 } else {
187 uint32_t maxKey = 0;
188 for (uint32_t i = 0; i < m_dataId.size(); ++i) {
189 if (m_dataId[i] > maxKey) maxKey = m_dataId[i];
190 }
191 if (maxKey + 1 < m_dataIndex.size()) m_dataIndex.resize(maxKey + 1);
192 }
193 m_dataIndex.shrink_to_fit();
194 }
195
204 uint32_t keyAt(uint32_t denseIndex) const { return m_dataId[denseIndex]; }
205
213 uint64_t structure() const { return m_structure; }
214
224 uint64_t stampAt(uint32_t denseIndex) const { return m_stamps[denseIndex]; }
225
232 T& dataAt(uint32_t denseIndex) { return m_data[denseIndex]; }
233 const T& dataAt(uint32_t denseIndex) const { return m_data[denseIndex]; }
234
235 private:
236 static constexpr uint32_t EMPTY = UINT32_MAX;
237
243 void ensureCapacity(uint32_t key) {
244 if (key >= m_dataIndex.size())
245 m_dataIndex.resize(key + 1, EMPTY);
246 }
247
256 template<typename... Args>
257 T& addInternal(uint32_t key, Args&&... args) {
258 VKM_ASSERT(key != EMPTY, "SparseSet::add key cannot be EMPTY sentinel");
259 VKM_ASSERT(key != 0, "SparseSet::add key 0 is reserved");
260 ensureCapacity(key);
261 VKM_ASSERT(!contains(key), "SparseSet::add key already present");
262 // Guarded too: without asserts the key would strand its first entry.
263 if (contains(key)) return m_data[m_dataIndex[key]];
264
265 uint32_t dataIdx = static_cast<uint32_t>(m_data.size());
266
267 m_data.emplace_back(std::forward<Args>(args)...);
268 m_dataId.push_back(key);
269 m_dataIndex[key] = dataIdx;
270 m_stamps.push_back(++m_structure);
271
272 return m_data[dataIdx];
273 }
274
275 private:
276 std::vector<uint32_t> m_dataIndex;
277 std::vector<uint32_t> m_dataId;
278 std::vector<T> m_data;
279 std::vector<uint64_t> m_stamps;
280 uint64_t m_structure = 0;
281};
282
283} // namespace Vkm::Engine
virtual void compact()=0
Release the sparse array's slack, keeping every live element.
virtual void removeIfPresent(uint32_t key)=0
Remove the element at the given key, if this set holds one.
uint64_t stampAt(uint32_t denseIndex) const
Which add put the element at a dense index there, unique for the set's life.
Definition sparse_set.h:224
void removeIfPresent(uint32_t key) override
Remove the element at the given key if one is present.
Definition sparse_set.h:103
bool contains(uint32_t key) const
Test whether a key is present.
Definition sparse_set.h:112
void compact() override
Shrink the sparse array to fit only live keys, reclaiming wasted memory.
Definition sparse_set.h:183
uint32_t keyAt(uint32_t denseIndex) const
Access the sparse key stored at a dense index.
Definition sparse_set.h:204
T & dataAt(uint32_t denseIndex)
Access the element stored at a dense index.
Definition sparse_set.h:232
uint64_t structure() const
Moved by every add, remove and clear: what a cache keyed on dense indices compares.
Definition sparse_set.h:213
void forEach(Fn &&fn)
Iterate all live elements densely (no holes).
Definition sparse_set.h:143
T & add(uint32_t key, T &&value)
Insert an element at the given key.
Definition sparse_set.h:68
void clear()
Drop every element.
Definition sparse_set.h:168
size_t size() const
Number of live elements.
Definition sparse_set.h:161
void remove(uint32_t key)
Remove the element at the given key via swap-and-pop.
Definition sparse_set.h:75
T & get(uint32_t key)
Access the element at the given key.
Definition sparse_set.h:121