ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
Imago::ECS::SparseSet< T > Class Template Reference

SparseSet concrete class templated to a component type, acts as Imago ECS's component pool. More...

#include <Imago/ECS/SparseSet.hpp>

Inheritance diagram for Imago::ECS::SparseSet< T >:
Collaboration diagram for Imago::ECS::SparseSet< T >:

Public Member Functions

T & insert (Entity e, T component)
 adds a component for an Entity, growing the sparse array if necessary
T & replace (Entity e, T component)
 replaces a component with new data
T & get (Entity e)
 returns a reference to the Entity's component
const T & get (Entity e) const
T * try_to_get (Entity e)
 tries to get a component and returns nullptr if one does not exist
void remove (Entity e) override
 removes an Entity's component from this pool, if it is currently present
bool has (Entity e) const override
 checks whether an Entity currently has a component stored in this pool
size_t get_size () const override
 return the number of Entities currently stored in this pool
const std::vector< Entity > & get_entities () const override
 returns the dense array of entities currently stored in this pool
size_t get_index (Entity e) const override
 Returns the index of the specified Entity.
void swap_entities (Entity e1, Entity e2) override
 Swaps e1 with e2 in _dense, _entities, and _sparse.

Detailed Description

template<typename T>
class Imago::ECS::SparseSet< T >

SparseSet concrete class templated to a component type, acts as Imago ECS's component pool.

Packed, cache friendly storage for one component type. SparseSet<T> stores every entity's T component contiguously in memory, with no gaps. This allows iterating every instance of T to be a straight linear scan with no pointer chasing and no skipped/empty slots. Lookups, insertions, and removals are all O(1), implemented via three parallel arrays:

  • _sparse maps an entitiy's raw index to its slot in _dense/_entities
  • _dense is the packed component data itself
  • _entities is the packed entities, parallel to _dense, used to patch _sparse after a swap-and-pop removal
Template Parameters
Tthe component type stored in the pool.

Member Function Documentation

◆ get()

template<typename T>
T & Imago::ECS::SparseSet< T >::get ( Entity e)

returns a reference to the Entity's component

Parameters
ethe Entity whose component you want
Returns
the referance to the Entity's component

◆ get_entities()

template<typename T>
const std::vector< Entity > & Imago::ECS::SparseSet< T >::get_entities ( ) const
overridevirtual

returns the dense array of entities currently stored in this pool

Returns
the dense array of Entities

Implements Imago::ECS::SparseSetBase.

◆ get_index()

template<typename T>
size_t Imago::ECS::SparseSet< T >::get_index ( Entity e) const
overridevirtual

Returns the index of the specified Entity.

Parameters
eThe entity's whose index to return.

Implements Imago::ECS::SparseSetBase.

◆ get_size()

template<typename T>
size_t Imago::ECS::SparseSet< T >::get_size ( ) const
overridevirtual

return the number of Entities currently stored in this pool

Returns
the number of Entitities currently stored in the pool

Implements Imago::ECS::SparseSetBase.

◆ has()

template<typename T>
bool Imago::ECS::SparseSet< T >::has ( Entity e) const
overridevirtual

checks whether an Entity currently has a component stored in this pool

Parameters
ethe Entity to check
Returns
true if the Entity has a component stored in this pool
false if the Entity does not have a component stored in this pool

Implements Imago::ECS::SparseSetBase.

◆ insert()

template<typename T>
T & Imago::ECS::SparseSet< T >::insert ( Entity e,
T component )

adds a component for an Entity, growing the sparse array if necessary

Parameters
ethe Entity to place the component on
componentthe component to insert
Returns
a reference to the component

◆ remove()

template<typename T>
void Imago::ECS::SparseSet< T >::remove ( Entity e)
overridevirtual

removes an Entity's component from this pool, if it is currently present

Parameters
ethe Entity to remove

Implements Imago::ECS::SparseSetBase.

◆ replace()

template<typename T>
T & Imago::ECS::SparseSet< T >::replace ( Entity e,
T component )

replaces a component with new data

Parameters
ethe Entity whose component needs to be replaced
componentthe component to replace it with
Returns
a reference to the component

◆ swap_entities()

template<typename T>
void Imago::ECS::SparseSet< T >::swap_entities ( Entity e1,
Entity e2 )
overridevirtual

Swaps e1 with e2 in _dense, _entities, and _sparse.

Parameters
e1First Entity to swap.
e2Second Entity to swap.

Implements Imago::ECS::SparseSetBase.

◆ try_to_get()

template<typename T>
T * Imago::ECS::SparseSet< T >::try_to_get ( Entity e)

tries to get a component and returns nullptr if one does not exist

Parameters
ethe entity to try to get
Returns
component pointer or nullptr if no component exists

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