ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
SparseSet.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <vector> // to create _sparse, _dense, and _entitites
9#include <cstdint> // for uint32_t
10#include <spdlog/spdlog.h> // for error logging
11#include <utility> // for std::move
12#include <cassert>
13
14#include "Imago/ECS/Entity.hpp"
16
17namespace Imago::ECS
18{
32 template <typename T>
33 class SparseSet : public SparseSetBase {
34 private:
35
36 std::vector<uint32_t> _sparse;
37 std::vector<T> _dense;
38 std::vector<Entity> _entities;
39
40 static constexpr uint32_t INVALID_INDEX = 0xFFFFFFFFu;
41
42 public:
43
44 // default constructor
45 SparseSet() = default;
46
53 T& insert(Entity e, T component);
54
61 T& replace(Entity e, T component);
62
63
64 // NOTE: a const and a non const getter is an important C++ mechanism called overlaoding on const
65 // the compiler will pick which one to call based on whether the object calling it is itself const or not
66 // this is important for const correctness so that any const object can still get the entities in the pool
67
73 T& get(Entity e);
74 const T& get(Entity e) const;
75
81 T* try_to_get(Entity e);
82
83 // SpareSetBase overrides
84 void remove(Entity e) override;
85 bool has(Entity e) const override;
86 size_t get_size() const override;
87 const std::vector<Entity>& get_entities() const override;
88 size_t get_index(Entity e) const override;
89 void swap_entities(Entity e1, Entity e2) override;
90 };
91
92 //------------
93 //definitions
94 //------------
95
96 template <typename T>
97 T& SparseSet<T>::insert(Entity e, T component)
98 {
99
100 // extract the Entity's index
101 uint32_t index = get_entity_index(e);
102
103 // ensure that the Entity's size is under the maximum entities
104 assert(index < ENTITY_INDEX_MASK && "[SparseSet] Insert call with an out-of-range Entity index");
105
106 //TODO: intergrate spdlog into editor logger once it exists
107 // ensure that the Entity does not already have a component
108 if (index < _sparse.size() && _sparse[index] != INVALID_INDEX) {
109 spdlog::warn("[SparseSet] You are trying to add a component that Entity {} already has. Please use Nexus.Patch() instead", index);
110 return _dense[_sparse[index]];
111 }
112
113 // if the Entity's index is bigger than sparse, resize it to that index
114 if (index >= _sparse.size()) {
115 _sparse.resize(index + 1, INVALID_INDEX);
116 }
117
118 // the current size if _dense/_entities is the next index so add that number to _sparse at the Entity's index
119 _sparse[index] = static_cast<uint32_t>(_dense.size());
120
121 // add the component to _dense and entity to _entities
122 _dense.push_back(std::move(component));
123 _entities.push_back(e);
124
125 // return the component for future additions
126 return _dense.back();
127
128 }
129
130 template <typename T>
131 T& SparseSet<T>::replace(Entity e, T component)
132 {
133 // extract the Entity's index
134 uint32_t index = get_entity_index(e);
135
136 // ensure the entity has a component to replace
137 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX && "[SparseSet] Replace called on an Entity with no existing component");
138
139 // get the component index
140 uint32_t componentIndex = _sparse[index];
141
142 // use that index to move the new component into its olds slot
143 _dense[componentIndex] = std::move(component);
144
145 return _dense[componentIndex];
146
147 }
148
149 template <typename T>
151
152 // extract the index
153 uint32_t index = get_entity_index(e);
154
155 // ensure the index is in bounds
156 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX && "[SparseSet] Get called on an Entity with no component in this pool");
157
158 // return the componen
159 return _dense[_sparse[index]];
160 }
161
162 template <typename T>
163 const T& SparseSet<T>::get(Entity e) const {
164
165 // extract the index
166 uint32_t index = get_entity_index(e);
167
168 // ensure the index is in bounds
169 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX && "[SparseSet] Get called on an Entity with no component in this pool");
170
171 // return the component
172 return _dense[_sparse[index]];
173 }
174
175 template <typename T>
177
178 // extract the index
179 uint32_t index = get_entity_index(e);
180
181 // return nullptr if the index is not in bounds or does not exist
182 if (index >= _sparse.size() || _sparse[index] == INVALID_INDEX) {
183 return nullptr;
184 }
185
186 //othewrise return the component
187 return &_dense[_sparse[index]];
188 }
189
190
191 template <typename T>
193
194 // unpack the Entity index
195 uint32_t index = get_entity_index(e);
196
197 //TODO: intergrate spdlog into editor logger once it exists
198 // ensure that the entity actually does have a component in the sparse set
199 if (index >= _sparse.size() || _sparse[index] == INVALID_INDEX) {
200 spdlog::warn("[SparseSet] Cannot remove component at index {} because there is no component to remove.", index);
201 return;
202 }
203
204 // get the index to be removed
205 uint32_t removedIndex = _sparse[index];
206
207 // get the last index
208 uint32_t lastIndex = static_cast<uint32_t>(_dense.size() - 1);
209
210 // if the index is not the last index we to swap it to the last index
211 if (removedIndex != lastIndex) {
212
213 // overwrite the remove component with the last component for _dense and _entities
214 _dense[removedIndex] = _dense[lastIndex];
215 _entities[removedIndex] = _entities[lastIndex];
216
217 // get the index of the entity we just swapped
218 uint32_t swappedIndex = get_entity_index(_entities[removedIndex]);
219
220 // update _sparse to now contain the new index for the entity we just swapped
221 _sparse[swappedIndex] = removedIndex;
222 }
223
224 // remove the last element from both packed arrays
225 _dense.pop_back();
226 _entities.pop_back();
227
228 // reset the removed entity's index to invalid
229 _sparse[index] = INVALID_INDEX;
230 }
231
232 template <typename T>
233 bool SparseSet<T>::has(Entity e) const {
234
235 // extract index and return if it is in the sparse array
236 uint32_t index = get_entity_index(e);
237 return index < _sparse.size() && _sparse[index] != INVALID_INDEX;
238 }
239
240 template <typename T>
241 size_t SparseSet<T>::get_size() const {
242 return _dense.size();
243 }
244
245 template <typename T>
246 const std::vector<Entity>& SparseSet<T>::get_entities() const {
247 return _entities;
248 }
249
250 template <typename T>
252
253 // extract the Entity's index
254 uint32_t index = get_entity_index(e);
255
256 // make sure the entity is in the pool
257 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX && "[SparseSet] get_index called on an Entity with no component in this pool");
258
259 // return static cast of the dense index converted to size_t
260 return static_cast<size_t>(_sparse[index]);
261 }
262
263 template <typename T>
265
266 // extract both entity indexs
267 uint32_t index1 = get_entity_index(e1);
268 uint32_t index2 = get_entity_index(e2);
269
270 // make sure both entity's are in the pool
271 assert(index1 < _sparse.size() && _sparse[index1] != INVALID_INDEX && "[SparseSet] swap_entities called on an Entity with no component in this pool");
272 assert(index2 < _sparse.size() && _sparse[index2] != INVALID_INDEX && "[SparseSet] swap_entities called on an Entity with no component in this pool");
273
274 // get the dense positions from the _sparse array
275 uint32_t pos1 = _sparse[index1];
276 uint32_t pos2 = _sparse[index2];
277
278 // if they are the same position just return and do not do anything
279 if (pos1 == pos2) return;
280
281 // swap the elements in _dense and _entities
282 std::swap(_dense[pos1], _dense[pos2]);
283 std::swap(_entities[pos1], _entities[pos2]);
284
285 // fix _sparse so that it holds the proper indices still
286 std::swap(_sparse[index1], _sparse[index2]);
287 }
288}
Defines the Entity alias and the free functions used to pack and unpack it.
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20
constexpr uint32_t ENTITY_INDEX_MASK
Mask isolating the low 20 index bits of a packed Entity.
Definition Entity.hpp:31
constexpr uint32_t get_entity_index(Entity entity)
Extracts the index bits from a packed Entity.
Definition Entity.hpp:52
Declares the SparseSetBase interface.
Type-erased interface for SparseSet<T>.
Definition SparseSetBase.hpp:21
bool has(Entity e) const override
checks whether an Entity currently has a component stored in this pool
Definition SparseSet.hpp:233
void swap_entities(Entity e1, Entity e2) override
Swaps e1 with e2 in _dense, _entities, and _sparse.
Definition SparseSet.hpp:264
T & replace(Entity e, T component)
replaces a component with new data
Definition SparseSet.hpp:131
T * try_to_get(Entity e)
tries to get a component and returns nullptr if one does not exist
Definition SparseSet.hpp:176
size_t get_index(Entity e) const override
Returns the index of the specified Entity.
Definition SparseSet.hpp:251
size_t get_size() const override
return the number of Entities currently stored in this pool
Definition SparseSet.hpp:241
void remove(Entity e) override
removes an Entity's component from this pool, if it is currently present
Definition SparseSet.hpp:192
T & insert(Entity e, T component)
adds a component for an Entity, growing the sparse array if necessary
Definition SparseSet.hpp:97
T & get(Entity e)
returns a reference to the Entity's component
Definition SparseSet.hpp:150
const std::vector< Entity > & get_entities() const override
returns the dense array of entities currently stored in this pool
Definition SparseSet.hpp:246