|
Aegis2D
Modular C++26 2D game engine for networked games
|
Type-erased part of a sparse set: which entities are in the pool, and where. More...
#include <aegis2d/ecs/sparse_set.hpp>
Public Member Functions | |
| SparseSetBase ()=default | |
| SparseSetBase (const SparseSetBase &)=delete | |
| SparseSetBase & | operator= (const SparseSetBase &)=delete |
| SparseSetBase (SparseSetBase &&) noexcept=default | |
| SparseSetBase & | operator= (SparseSetBase &&) noexcept=default |
| virtual | ~SparseSetBase ()=default |
| bool | contains (Entity entity) const noexcept |
| Tells whether an entity is in the pool. | |
| bool | remove (Entity entity) |
| Removes an entity and its component; the last entry moves into its place. | |
| void | clear () noexcept |
| Removes every entity and component. | |
| std::size_t | size () const noexcept |
| Number of entities in the pool. | |
| bool | empty () const noexcept |
| Tells whether the pool is empty. | |
| std::span< const Entity > | entities () const noexcept |
| Entities of the pool, packed, in the same order as the components. | |
Protected Types | |
| using | Position = std::uint32_t |
| Position of an entity in the dense array. | |
Protected Member Functions | |
| 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. | |
| 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. | |
| virtual void | clear_components () noexcept=0 |
| Drops every component. | |
| std::size_t | position_of (Entity entity) const |
| Position of an entity in the dense array. | |
Type-erased part of a sparse set: which entities are in the pool, and where.
The dense array lists the entities of the pool contiguously; the sparse array maps an entity index to its position in the dense array. Removing an entity moves the last one into its hole, so the dense array stays packed and iteration touches only live entries. contains(), insertion and removal are O(1).
The registry keeps one pool per component type behind this class, so it can remove the components of a destroyed entity without knowing their types.
The sparse array grows up to the highest entity index stored (4 bytes per index).
|
protected |
Position of an entity in the dense array.
|
default |
|
delete |
|
defaultnoexcept |
|
virtualdefault |
|
protected |
Throws if an entity cannot be inserted.
| entity | Handle about to be inserted. |
| std::invalid_argument | For the null handle. |
| std::logic_error | If the index is already in the pool, at any generation. |
|
noexcept |
Removes every entity and component.
|
protectedpure virtualnoexcept |
Drops every component.
|
noexcept |
Tells whether an entity is in the pool.
| entity | Handle to look up. |
true only for the exact handle that was inserted: a stale handle to the same index (other generation) and the null handle are not contained.
|
inlinenoexcept |
Tells whether the pool is empty.
true when size() is 0.
|
inlinenoexcept |
Entities of the pool, packed, in the same order as the components.
|
protected |
Appends an entity at the end of the dense array.
Called by the derived pool once its component is constructed; on failure (allocation) nothing is inserted and the exception propagates.
| entity | Handle checked with check_insertable(). |
|
protectedpure virtualnoexcept |
Moves the component at last into position, then drops the last one.
| position | Dense position of the removed entity. |
| last | Dense position of the last entity (equal to position when it is the removed one). |
|
delete |
|
defaultnoexcept |
|
inlineprotected |
Position of an entity in the dense array.
| entity | Handle that contains() accepts. |
| bool aegis2d::ecs::SparseSetBase::remove | ( | Entity | entity | ) |
Removes an entity and its component; the last entry moves into its place.
| entity | Handle to remove. |
true if the entity was in the pool; false (and nothing changes) otherwise.
|
protected |
Prepares room in the dense array.
| count | Number of entities. |
|
inlinenoexcept |
Number of entities in the pool.