5#include <aegis2d/ecs/export.hpp>
43 [[nodiscard]]
bool contains(
Entity entity) const noexcept;
51 void clear() noexcept;
55 [[nodiscard]]
std::
size_t size() const noexcept {
return dense_.size(); }
59 [[nodiscard]]
bool empty() const noexcept {
return dense_.empty(); }
63 [[nodiscard]] std::span<const Entity>
entities() const noexcept {
return dense_; }
89 virtual void move_last_to(std::size_t position, std::size_t last)
noexcept = 0;
97 [[nodiscard]]
std::
size_t position_of(
Entity entity)
const {
98 return sparse_.at(entity.index());
102 static constexpr Position npos = ~Position{0};
104 std::vector<Entity> dense_;
105 std::vector<Position> sparse_;
114template <
typename Component>
115 requires std::movable<Component> && std::is_nothrow_move_assignable_v<Component> &&
116 (!std::is_same_v<Component, bool>)
126 template <typename... Args>
127 Component& emplace(
Entity entity, Args&&... args) {
128 check_insertable(entity);
129 components_.emplace_back(std::forward<Args>(args)...);
133 components_.pop_back();
136 return components_.back();
143 [[nodiscard]] Component&
get(
Entity entity) {
return components_.at(checked_position(entity)); }
146 [[nodiscard]]
const Component&
get(
Entity entity)
const {
147 return components_.at(checked_position(entity));
153 [[nodiscard]] std::optional<std::reference_wrapper<Component>>
try_get(
Entity entity) {
154 if (!contains(entity)) {
157 return std::ref(components_.at(position_of(entity)));
161 [[nodiscard]] std::optional<std::reference_wrapper<const Component>>
163 if (!contains(entity)) {
166 return std::cref(components_.at(position_of(entity)));
171 [[nodiscard]] std::span<Component>
components() noexcept {
return components_; }
174 [[nodiscard]] std::span<const Component>
components() const noexcept {
return components_; }
179 reserve_entities(count);
180 components_.reserve(count);
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"};
188 return position_of(entity);
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));
195 components_.pop_back();
198 void clear_components() noexcept
override { components_.clear(); }
200 std::vector<Component> components_;
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