10#include <spdlog/spdlog.h>
36 std::vector<uint32_t> _sparse;
37 std::vector<T> _dense;
38 std::vector<Entity> _entities;
40 static constexpr uint32_t INVALID_INDEX = 0xFFFFFFFFu;
45 SparseSet() =
default;
87 const std::vector<Entity>&
get_entities()
const override;
104 assert(index <
ENTITY_INDEX_MASK &&
"[SparseSet] Insert call with an out-of-range Entity index");
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]];
114 if (index >= _sparse.size()) {
115 _sparse.resize(index + 1, INVALID_INDEX);
119 _sparse[index] =
static_cast<uint32_t
>(_dense.size());
122 _dense.push_back(std::move(component));
123 _entities.push_back(e);
126 return _dense.back();
130 template <
typename T>
137 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX &&
"[SparseSet] Replace called on an Entity with no existing component");
140 uint32_t componentIndex = _sparse[index];
143 _dense[componentIndex] = std::move(component);
145 return _dense[componentIndex];
149 template <
typename T>
156 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX &&
"[SparseSet] Get called on an Entity with no component in this pool");
159 return _dense[_sparse[index]];
162 template <
typename T>
169 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX &&
"[SparseSet] Get called on an Entity with no component in this pool");
172 return _dense[_sparse[index]];
175 template <
typename T>
182 if (index >= _sparse.size() || _sparse[index] == INVALID_INDEX) {
187 return &_dense[_sparse[index]];
191 template <
typename T>
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);
205 uint32_t removedIndex = _sparse[index];
208 uint32_t lastIndex =
static_cast<uint32_t
>(_dense.size() - 1);
211 if (removedIndex != lastIndex) {
214 _dense[removedIndex] = _dense[lastIndex];
215 _entities[removedIndex] = _entities[lastIndex];
221 _sparse[swappedIndex] = removedIndex;
226 _entities.pop_back();
229 _sparse[index] = INVALID_INDEX;
232 template <
typename T>
237 return index < _sparse.size() && _sparse[index] != INVALID_INDEX;
240 template <
typename T>
242 return _dense.size();
245 template <
typename T>
250 template <
typename T>
257 assert(index < _sparse.size() && _sparse[index] != INVALID_INDEX &&
"[SparseSet] get_index called on an Entity with no component in this pool");
260 return static_cast<size_t>(_sparse[index]);
263 template <
typename T>
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");
275 uint32_t pos1 = _sparse[index1];
276 uint32_t pos2 = _sparse[index2];
279 if (pos1 == pos2)
return;
282 std::swap(_dense[pos1], _dense[pos2]);
283 std::swap(_entities[pos1], _entities[pos2]);
286 std::swap(_sparse[index1], _sparse[index2]);
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