Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
rtp::ecs::SparseArray< T > Class Template Referencefinal

Sparse array container for component storage. More...

#include <SparseArray.hpp>

Inheritance diagram for rtp::ecs::SparseArray< T >:
Collaboration diagram for rtp::ecs::SparseArray< T >:

Public Types

using value_type = T
 
using container_t = std::vector< value_type >
 

Public Member Functions

 SparseArray ()=default
 
 SparseArray (const SparseArray &)=default
 
 SparseArray (SparseArray &&) noexcept=default
 
SparseArrayoperator= (const SparseArray &)=default
 
SparseArrayoperator= (SparseArray &&) noexcept=default
 
 ~SparseArray () override=default
 
void erase (Entity entity) noexcept override final
 Remove a component from an entity.
 
bool has (Entity entity) const noexcept override final
 Check if an entity has this component.
 
void clear (void) noexcept override final
 Remove all components.
 
template<typename Self >
auto && operator[] (this Self &self, Entity entity) noexcept
 Access component by entity.
 
template<typename... Args>
T & emplace (Entity entity, Args &&...args)
 Construct a component in-place for an entity.
 
std::span< T > data () noexcept
 Get the underlying dense component array.
 
std::span< const T > data () const noexcept
 
std::span< Entityentities () noexcept
 Get the entity array corresponding to components.
 
std::span< const Entityentities () const noexcept
 
std::size_t size (void) const noexcept
 Get the number of components stored.
 
bool empty (void) const noexcept
 Check if the array is empty.
 
- Public Member Functions inherited from rtp::ecs::ISparseArray
virtual ~ISparseArray () noexcept=default
 

Static Public Attributes

static constexpr std::size_t NullIndex
 

Private Attributes

std::vector< size_t > _sparse
 The Sparse Array (The Map)
 
std::vector< Entity_dense
 The Dense Entity Array (The Reverse Lookup)
 
container_t _data
 The Dense Component Array (The Cache Friendly Data)
 

Detailed Description

template<Component T>
class rtp::ecs::SparseArray< T >

Sparse array container for component storage.

Template Parameters
TThe component type to store

Implements an efficient sparse set data structure that:

  • Provides O(1) component access by entity ID
  • Allows efficient iteration over all components
  • Maintains entity-to-component mapping
  • Supports component addition, removal, and querying

Definition at line 95 of file SparseArray.hpp.

Member Typedef Documentation

◆ container_t

template<Component T>
using rtp::ecs::SparseArray< T >::container_t = std::vector<value_type>

Definition at line 98 of file SparseArray.hpp.

◆ value_type

template<Component T>
using rtp::ecs::SparseArray< T >::value_type = T

Definition at line 97 of file SparseArray.hpp.

Constructor & Destructor Documentation

◆ SparseArray() [1/3]

template<Component T>
rtp::ecs::SparseArray< T >::SparseArray ( )
default

◆ SparseArray() [2/3]

template<Component T>
rtp::ecs::SparseArray< T >::SparseArray ( const SparseArray< T > &  )
default

◆ SparseArray() [3/3]

template<Component T>
rtp::ecs::SparseArray< T >::SparseArray ( SparseArray< T > &&  )
defaultnoexcept

◆ ~SparseArray()

template<Component T>
rtp::ecs::SparseArray< T >::~SparseArray ( )
overridedefault

Member Function Documentation

◆ clear()

template<Component T>
void rtp::ecs::SparseArray< T >::clear ( void  )
finaloverridevirtualnoexcept

Remove all components.

Implements rtp::ecs::ISparseArray.

◆ data() [1/2]

template<Component T>
std::span< const T > rtp::ecs::SparseArray< T >::data ( ) const
noexcept

◆ data() [2/2]

template<Component T>
std::span< T > rtp::ecs::SparseArray< T >::data ( )
noexcept

Get the underlying dense component array.

Returns
Reference to the dense component container

◆ emplace()

template<Component T>
template<typename... Args>
T & rtp::ecs::SparseArray< T >::emplace ( Entity  entity,
Args &&...  args 
)

Construct a component in-place for an entity.

Template Parameters
ArgsConstructor argument types
Parameters
entityThe entity to add the component to
argsArguments to construct the component with
Returns
Reference to the newly created component

Referenced by TEST(), TEST(), and TEST().

◆ empty()

template<Component T>
bool rtp::ecs::SparseArray< T >::empty ( void  ) const
noexcept

Check if the array is empty.

Returns
true if no components are stored

Referenced by TEST().

◆ entities() [1/2]

template<Component T>
std::span< const Entity > rtp::ecs::SparseArray< T >::entities ( ) const
noexcept

◆ entities() [2/2]

template<Component T>
std::span< Entity > rtp::ecs::SparseArray< T >::entities ( )
noexcept

Get the entity array corresponding to components.

Returns
Reference to the entity container

Referenced by TEST().

◆ erase()

template<Component T>
void rtp::ecs::SparseArray< T >::erase ( Entity  entity)
finaloverridevirtualnoexcept

Remove a component from an entity.

Parameters
entityThe entity whose component to remove

Implements rtp::ecs::ISparseArray.

Referenced by TEST(), TEST(), and TEST().

◆ has()

template<Component T>
bool rtp::ecs::SparseArray< T >::has ( Entity  entity) const
finaloverridevirtualnoexcept

Check if an entity has this component.

Parameters
entityThe entity to check
Returns
true if the entity has this component

Implements rtp::ecs::ISparseArray.

Referenced by TEST().

◆ operator=() [1/2]

template<Component T>
SparseArray & rtp::ecs::SparseArray< T >::operator= ( const SparseArray< T > &  )
default

◆ operator=() [2/2]

template<Component T>
SparseArray & rtp::ecs::SparseArray< T >::operator= ( SparseArray< T > &&  )
defaultnoexcept

◆ operator[]()

template<Component T>
template<typename Self >
auto && rtp::ecs::SparseArray< T >::operator[] ( this Self &  self,
Entity  entity 
)
noexcept

Access component by entity.

Template Parameters
SelfDeduced self type (const or non-const)
Parameters
entityThe entity to get the component for
Returns
Reference to the component
Note
Asserts if the entity does not have this component If the entity does not have the component, behavior is undefined

◆ size()

template<Component T>
std::size_t rtp::ecs::SparseArray< T >::size ( void  ) const
noexcept

Get the number of components stored.

Returns
Number of components

Referenced by TEST(), TEST(), TEST(), and TEST().

Member Data Documentation

◆ _data

template<Component T>
container_t rtp::ecs::SparseArray< T >::_data
private

The Dense Component Array (The Cache Friendly Data)

Definition at line 204 of file SparseArray.hpp.

◆ _dense

template<Component T>
std::vector<Entity> rtp::ecs::SparseArray< T >::_dense
private

The Dense Entity Array (The Reverse Lookup)

Definition at line 203 of file SparseArray.hpp.

◆ _sparse

template<Component T>
std::vector<size_t> rtp::ecs::SparseArray< T >::_sparse
private

The Sparse Array (The Map)

Definition at line 202 of file SparseArray.hpp.

◆ NullIndex

template<Component T>
constexpr std::size_t rtp::ecs::SparseArray< T >::NullIndex
staticconstexpr
Initial value:
=
std::numeric_limits<size_t>::max()

Definition at line 100 of file SparseArray.hpp.


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