Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
SparseSet.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Common/Assert.hpp>
5#include <cstddef>
6#include <type_traits>
7
8namespace shkyera {
9
11 public:
12 virtual ~SparseSetBase() = default;
13 virtual bool remove(Entity entity) = 0;
14 virtual std::unique_ptr<SparseSetBase> clone() const = 0;
15};
16
21template <typename Component>
22class SparseSet : public SparseSetBase {
23 public:
27 SparseSet() = default;
28
32 ~SparseSet() = default;
33
34 std::unique_ptr<SparseSetBase> clone() const override {
35 if constexpr (std::is_copy_constructible_v<Component>) {
36 return std::make_unique<SparseSet<Component>>(*this);
37 } else {
38 SHKYERA_ASSERT(false, "{} is not copyable, cannot clone the SparseSet", typeid(Component).name());
39 return nullptr;
40 }
41 }
42
50 if (contains(entity)) {
51 return false;
52 }
54 _entities.emplace_back(entity);
55 _components.push_back(std::move(component));
56
57 return true;
58 }
59
66 bool remove(Entity entity) override {
67 if (!contains(entity)) {
68 return false;
69 }
70
71 const auto& index = _entityToComponent.at(entity);
72 const auto& lastIndex = _entities.size() - 1;
73
75 _components[index] = std::move(_components[lastIndex]);
76
77 _entityToComponent[_entities.at(index)] = index;
78
79 _entities.pop_back();
80 _components.pop_back();
82
83 return true;
84 }
85
86 void clear() {
87 _entities.clear();
88 _entityToComponent.clear();
89 _components.clear();
90 }
91
98 bool contains(Entity entity) const { return _entityToComponent.find(entity) != _entityToComponent.end(); }
99
107
115
121 std::vector<Component>& getComponents() { return _components; }
122
128 const std::vector<Component>& getComponents() const { return _components; }
129
135 bool empty() const { return _components.empty(); }
136
137 size_t size() const { return _components.size(); }
138
139 template <bool IsConst>
140 class Iterator {
141 public:
142 using iterator_category = std::forward_iterator_tag;
143 using reference = std::pair<Entity, std::conditional_t<IsConst, const Component&, Component&>>;
144
145 // Constructor
146 Iterator(typename std::conditional_t<IsConst, const std::vector<Entity>*, std::vector<Entity>*> entities,
147 typename std::conditional_t<IsConst, const std::vector<Component>*, std::vector<Component>*> components,
148 size_t index)
150
151 // Dereference operator
152 reference operator*() const { return {(*_entities)[_index], (*_components)[_index]}; }
153
154 // Pre-increment operator
156 ++_index;
157 return *this;
158 }
159
160 // Post-increment operator
162 Iterator tmp = *this;
163 ++(*this);
164 return tmp;
165 }
166
167 // Equality comparison
168 bool operator==(const Iterator& other) const { return _index == other._index; }
169
170 // Inequality comparison
171 bool operator!=(const Iterator& other) const { return _index != other._index; }
172
173 private:
174 typename std::conditional_t<IsConst, const std::vector<Entity>*, std::vector<Entity>*> _entities;
175 typename std::conditional_t<IsConst, const std::vector<Component>*, std::vector<Component>*> _components;
176 size_t _index;
177 };
178
181
183
185
187
189
190 private:
191 std::vector<Entity> _entities;
192 std::unordered_map<Entity, size_t> _entityToComponent;
193 std::vector<Component> _components;
194};
195
196} // namespace shkyera
#define SHKYERA_ASSERT(predicate,...)
Definition Assert.hpp:7
Manages a pool of unused entity identifiers and provides mechanisms for requesting and removing them.
Definition SparseSet.hpp:140
std::forward_iterator_tag iterator_category
Definition SparseSet.hpp:142
std::conditional_t< IsConst, const std::vector< Entity > *, std::vector< Entity > * > _entities
Definition SparseSet.hpp:174
std::conditional_t< IsConst, const std::vector< Component > *, std::vector< Component > * > _components
Definition SparseSet.hpp:175
std::pair< Entity, std::conditional_t< IsConst, const Component &, Component & > > reference
Definition SparseSet.hpp:143
bool operator!=(const Iterator &other) const
Definition SparseSet.hpp:171
Iterator operator++(int)
Definition SparseSet.hpp:161
Iterator(typename std::conditional_t< IsConst, const std::vector< Entity > *, std::vector< Entity > * > entities, typename std::conditional_t< IsConst, const std::vector< Component > *, std::vector< Component > * > components, size_t index)
Definition SparseSet.hpp:146
reference operator*() const
Definition SparseSet.hpp:152
size_t _index
Definition SparseSet.hpp:176
Iterator & operator++()
Definition SparseSet.hpp:155
bool operator==(const Iterator &other) const
Definition SparseSet.hpp:168
Definition SparseSet.hpp:10
virtual bool remove(Entity entity)=0
virtual std::unique_ptr< SparseSetBase > clone() const =0
virtual ~SparseSetBase()=default
Definition SparseSet.hpp:22
const std::vector< Component > & getComponents() const
Definition SparseSet.hpp:128
iterator begin()
Definition SparseSet.hpp:182
bool empty() const
Definition SparseSet.hpp:135
Iterator< false > iterator
Definition SparseSet.hpp:179
bool remove(Entity entity) override
Definition SparseSet.hpp:66
void clear()
Definition SparseSet.hpp:86
std::vector< Component > _components
Definition SparseSet.hpp:193
const_iterator begin() const
Definition SparseSet.hpp:186
const Component & get(Entity entity) const
Definition SparseSet.hpp:114
const_iterator end() const
Definition SparseSet.hpp:188
Iterator< true > const_iterator
Definition SparseSet.hpp:180
~SparseSet()=default
std::vector< Entity > _entities
Definition SparseSet.hpp:191
std::unordered_map< Entity, size_t > _entityToComponent
Definition SparseSet.hpp:192
size_t size() const
Definition SparseSet.hpp:137
bool contains(Entity entity) const
Definition SparseSet.hpp:98
Component & get(Entity entity)
Definition SparseSet.hpp:106
iterator end()
Definition SparseSet.hpp:184
std::unique_ptr< SparseSetBase > clone() const override
Definition SparseSet.hpp:34
std::vector< Component > & getComponents()
Definition SparseSet.hpp:121
bool add(Entity entity, Component component)
Definition SparseSet.hpp:49
Definition Asset.hpp:6
uint32_t Entity
Definition Entity.hpp:7
Definition Clock.hpp:9