Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::SparseSetBase Class Referenceabstract

Type-erased part of a sparse set: which entities are in the pool, and where. More...

#include <aegis2d/ecs/sparse_set.hpp>

Inheritance diagram for aegis2d::ecs::SparseSetBase:
aegis2d::ecs::SparseSet< Component >

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.
 

Detailed Description

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).

Member Typedef Documentation

◆ Position

using aegis2d::ecs::SparseSetBase::Position = std::uint32_t
protected

Position of an entity in the dense array.

Constructor & Destructor Documentation

◆ SparseSetBase() [1/3]

aegis2d::ecs::SparseSetBase::SparseSetBase ( )
default

◆ SparseSetBase() [2/3]

aegis2d::ecs::SparseSetBase::SparseSetBase ( const SparseSetBase &  )
delete

◆ SparseSetBase() [3/3]

aegis2d::ecs::SparseSetBase::SparseSetBase ( SparseSetBase &&  )
defaultnoexcept

◆ ~SparseSetBase()

virtual aegis2d::ecs::SparseSetBase::~SparseSetBase ( )
virtualdefault

Member Function Documentation

◆ check_insertable()

void aegis2d::ecs::SparseSetBase::check_insertable ( Entity  entity) const
protected

Throws if an entity cannot be inserted.

Parameters
entityHandle about to be inserted.
Exceptions
std::invalid_argumentFor the null handle.
std::logic_errorIf the index is already in the pool, at any generation.

◆ clear()

void aegis2d::ecs::SparseSetBase::clear ( )
noexcept

Removes every entity and component.

◆ clear_components()

virtual void aegis2d::ecs::SparseSetBase::clear_components ( )
protectedpure virtualnoexcept

Drops every component.

◆ contains()

bool aegis2d::ecs::SparseSetBase::contains ( Entity  entity) const
noexcept

Tells whether an entity is in the pool.

Parameters
entityHandle to look up.
Returns
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.

◆ empty()

bool aegis2d::ecs::SparseSetBase::empty ( ) const
inlinenoexcept

Tells whether the pool is empty.

Returns
true when size() is 0.

◆ entities()

std::span< const Entity > aegis2d::ecs::SparseSetBase::entities ( ) const
inlinenoexcept

Entities of the pool, packed, in the same order as the components.

Returns
A view invalidated by any insertion or removal.

◆ insert()

void aegis2d::ecs::SparseSetBase::insert ( Entity  entity)
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.

Parameters
entityHandle checked with check_insertable().

◆ move_last_to()

virtual void aegis2d::ecs::SparseSetBase::move_last_to ( std::size_t  position,
std::size_t  last 
)
protectedpure virtualnoexcept

Moves the component at last into position, then drops the last one.

Parameters
positionDense position of the removed entity.
lastDense position of the last entity (equal to position when it is the removed one).

◆ operator=() [1/2]

SparseSetBase & aegis2d::ecs::SparseSetBase::operator= ( const SparseSetBase &  )
delete

◆ operator=() [2/2]

SparseSetBase & aegis2d::ecs::SparseSetBase::operator= ( SparseSetBase &&  )
defaultnoexcept

◆ position_of()

std::size_t aegis2d::ecs::SparseSetBase::position_of ( Entity  entity) const
inlineprotected

Position of an entity in the dense array.

Parameters
entityHandle that contains() accepts.
Returns
Its position.

◆ remove()

bool aegis2d::ecs::SparseSetBase::remove ( Entity  entity)

Removes an entity and its component; the last entry moves into its place.

Parameters
entityHandle to remove.
Returns
true if the entity was in the pool; false (and nothing changes) otherwise.

◆ reserve_entities()

void aegis2d::ecs::SparseSetBase::reserve_entities ( std::size_t  count)
protected

Prepares room in the dense array.

Parameters
countNumber of entities.

◆ size()

std::size_t aegis2d::ecs::SparseSetBase::size ( ) const
inlinenoexcept

Number of entities in the pool.

Returns
Size of the dense array.

The documentation for this class was generated from the following file: