Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
sparse_set.hpp
Go to the documentation of this file.
1
3#pragma once
5#include <aegis2d/ecs/export.hpp>
6
7#include <concepts>
8#include <cstddef>
9#include <cstdint>
10#include <functional>
11#include <optional>
12#include <span>
13#include <stdexcept>
14#include <type_traits>
15#include <utility>
16#include <vector>
17
18namespace aegis2d::ecs {
19
30class AEGIS2D_ECS_EXPORT SparseSetBase {
31public:
32 SparseSetBase() = default;
33 SparseSetBase(const SparseSetBase&) = delete;
35 SparseSetBase(SparseSetBase&&) noexcept = default;
36 SparseSetBase& operator=(SparseSetBase&&) noexcept = default;
37 virtual ~SparseSetBase() = default;
38
43 [[nodiscard]] bool contains(Entity entity) const noexcept;
44
48 bool remove(Entity entity);
49
51 void clear() noexcept;
52
55 [[nodiscard]] std::size_t size() const noexcept { return dense_.size(); }
56
59 [[nodiscard]] bool empty() const noexcept { return dense_.empty(); }
60
63 [[nodiscard]] std::span<const Entity> entities() const noexcept { return dense_; }
64
65protected:
67 using Position = std::uint32_t;
68
73 void check_insertable(Entity entity) const;
74
79 void insert(Entity entity);
80
83 void reserve_entities(std::size_t count);
84
89 virtual void move_last_to(std::size_t position, std::size_t last) noexcept = 0;
90
92 virtual void clear_components() noexcept = 0;
93
97 [[nodiscard]] std::size_t position_of(Entity entity) const {
98 return sparse_.at(entity.index());
99 }
100
101private:
102 static constexpr Position npos = ~Position{0};
103
104 std::vector<Entity> dense_;
105 std::vector<Position> sparse_;
106};
107
114template <typename Component>
115 requires std::movable<Component> && std::is_nothrow_move_assignable_v<Component> &&
116 (!std::is_same_v<Component, bool>)
117class SparseSet final : public SparseSetBase {
118public:
126 template <typename... Args>
127 Component& emplace(Entity entity, Args&&... args) {
128 check_insertable(entity);
129 components_.emplace_back(std::forward<Args>(args)...);
130 try {
131 insert(entity);
132 } catch (...) {
133 components_.pop_back();
134 throw;
135 }
136 return components_.back();
137 }
138
143 [[nodiscard]] Component& get(Entity entity) { return components_.at(checked_position(entity)); }
144
146 [[nodiscard]] const Component& get(Entity entity) const {
147 return components_.at(checked_position(entity));
148 }
149
153 [[nodiscard]] std::optional<std::reference_wrapper<Component>> try_get(Entity entity) {
154 if (!contains(entity)) {
155 return std::nullopt;
156 }
157 return std::ref(components_.at(position_of(entity)));
158 }
159
161 [[nodiscard]] std::optional<std::reference_wrapper<const Component>>
162 try_get(Entity entity) const {
163 if (!contains(entity)) {
164 return std::nullopt;
165 }
166 return std::cref(components_.at(position_of(entity)));
167 }
168
171 [[nodiscard]] std::span<Component> components() noexcept { return components_; }
172
174 [[nodiscard]] std::span<const Component> components() const noexcept { return components_; }
175
178 void reserve(std::size_t count) {
179 reserve_entities(count);
180 components_.reserve(count);
181 }
182
183private:
184 [[nodiscard]] std::size_t checked_position(Entity entity) const {
185 if (!contains(entity)) {
186 throw std::out_of_range{"SparseSet: the entity has no such component"};
187 }
188 return position_of(entity);
189 }
190
191 void move_last_to(std::size_t position, std::size_t last) noexcept override {
192 if (position != last) {
193 components_.at(position) = std::move(components_.at(last));
194 }
195 components_.pop_back();
196 }
197
198 void clear_components() noexcept override { components_.clear(); }
199
200 std::vector<Component> components_;
201};
202
203} // namespace aegis2d::ecs
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Type-erased part of a sparse set: which entities are in the pool, and where.
Definition sparse_set.hpp:30
std::uint32_t Position
Position of an entity in the dense array.
Definition sparse_set.hpp:67
SparseSetBase(SparseSetBase &&) noexcept=default
std::span< const Entity > entities() const noexcept
Entities of the pool, packed, in the same order as the components.
Definition sparse_set.hpp:63
void check_insertable(Entity entity) const
Throws if an entity cannot be inserted.
void insert(Entity entity)
Appends an entity at the end of the dense array.
void reserve_entities(std::size_t count)
Prepares room in the dense array.
bool empty() const noexcept
Tells whether the pool is empty.
Definition sparse_set.hpp:59
virtual void clear_components() noexcept=0
Drops every component.
virtual void move_last_to(std::size_t position, std::size_t last) noexcept=0
Moves the component at last into position, then drops the last one.
SparseSetBase & operator=(const SparseSetBase &)=delete
SparseSetBase(const SparseSetBase &)=delete
Pool of one component type, stored in a sparse set.
Definition sparse_set.hpp:117
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition sparse_set.hpp:153
std::span< const Component > components() const noexcept
Components of the pool, packed, in the same order as entities().
Definition sparse_set.hpp:174
const Component & get(Entity entity) const
Component of an entity.
Definition sparse_set.hpp:146
std::optional< std::reference_wrapper< const Component > > try_get(Entity entity) const
Component of an entity, if it has one.
Definition sparse_set.hpp:162
Component & get(Entity entity)
Component of an entity.
Definition sparse_set.hpp:143
std::span< Component > components() noexcept
Components of the pool, packed, in the same order as entities().
Definition sparse_set.hpp:171
void reserve(std::size_t count)
Prepares room for a number of components without reallocating.
Definition sparse_set.hpp:178
Generational entity handle: an index into the component pools plus a generation.
Entity-Component-System used by client and server.
Definition animation.hpp:13
Definition endpoint.hpp:85