R-Type  2
Doom but in better
Loading...
Searching...
No Matches
SparseArray< Component, Allocator > Class Template Reference

A container that provides sparse storage for optional components. More...

#include <SparseArray.hpp>

Collaboration diagram for SparseArray< Component, Allocator >:
Collaboration graph

Public Types

using value_type = std::optional<Component>
 Type aliases for convenience.
 
using reference_type = value_type&
 
using const_reference_type = const value_type&
 
using container_t = std::vector<value_type, Allocator>
 
using size_type = typename container_t::size_type
 
using iterator = typename container_t::iterator
 
using const_iterator = typename container_t::const_iterator
 

Public Member Functions

 SparseArray ()=default
 Default constructor.
 
 SparseArray (const SparseArray &other)=default
 Copy constructor.
 
 SparseArray (SparseArray &&other) noexcept=default
 Move constructor.
 
 ~SparseArray ()=default
 Destructor.
 
SparseArrayoperator= (const SparseArray &other)=default
 Copy assignment operator.
 
SparseArrayoperator= (SparseArray &&other) noexcept=default
 Move assignment operator.
 
reference_type operator[] (std::size_t idx)
 Accesses the value at the given index.
 
const_reference_type operator[] (std::size_t idx) const
 Accesses the value at the given index (const version).
 
iterator begin ()
 Returns an iterator to the beginning of the array.
 
const_iterator begin () const
 Returns a const iterator to the beginning of the array.
 
const_iterator cbegin () const
 Returns a const iterator to the beginning of the array.
 
iterator end ()
 Returns an iterator to the end of the array.
 
const_iterator end () const
 Returns a const iterator to the end of the array.
 
const_iterator cend () const
 Returns a const iterator to the end of the array.
 
size_type size () const
 Returns the size of the array.
 
reference_type insert_at (size_type pos, const Component &component)
 Inserts a component at a specific position.
 
reference_type insert_at (size_type pos, Component &&component)
 Inserts a component at a specific position (move version).
 
template<class... Params>
reference_type emplace_at (size_type pos, Params &&... params)
 Constructs and inserts a component at a specific position.
 
void erase (size_type pos)
 Removes the component at a specific position.
 

Detailed Description

template<typename Component, typename Allocator = std::allocator<Component>>
class SparseArray< Component, Allocator >

A container that provides sparse storage for optional components.

The SparseArray class is designed for efficient storage of components where not all indices are occupied, allowing components to be stored sparsely with optional values.

Template Parameters
ComponentThe type of the components stored in the array.
AllocatorThe allocator type used for memory management (defaults to std::allocator<Component>).

Definition at line 19 of file SparseArray.hpp.

Member Typedef Documentation

◆ const_iterator

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::const_iterator = typename container_t::const_iterator

Const iterator type.

Definition at line 28 of file SparseArray.hpp.

◆ const_reference_type

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::const_reference_type = const value_type&

Const reference type to a value in the array.

Definition at line 24 of file SparseArray.hpp.

◆ container_t

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::container_t = std::vector<value_type, Allocator>

Internal storage container type.

Definition at line 25 of file SparseArray.hpp.

◆ iterator

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::iterator = typename container_t::iterator

Iterator type.

Definition at line 27 of file SparseArray.hpp.

◆ reference_type

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::reference_type = value_type&

Reference type to a value in the array.

Definition at line 23 of file SparseArray.hpp.

◆ size_type

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::size_type = typename container_t::size_type

Size type for the container.

Definition at line 26 of file SparseArray.hpp.

◆ value_type

template<typename Component , typename Allocator = std::allocator<Component>>
using SparseArray< Component, Allocator >::value_type = std::optional<Component>

Type aliases for convenience.

The type of value stored, wrapped in an std::optional.

Definition at line 22 of file SparseArray.hpp.

Constructor & Destructor Documentation

◆ SparseArray() [1/3]

template<typename Component , typename Allocator = std::allocator<Component>>
SparseArray< Component, Allocator >::SparseArray ( )
default

Default constructor.

◆ SparseArray() [2/3]

template<typename Component , typename Allocator = std::allocator<Component>>
SparseArray< Component, Allocator >::SparseArray ( const SparseArray< Component, Allocator > & other)
default

Copy constructor.

Parameters
otherThe SparseArray to copy from.

◆ SparseArray() [3/3]

template<typename Component , typename Allocator = std::allocator<Component>>
SparseArray< Component, Allocator >::SparseArray ( SparseArray< Component, Allocator > && other)
defaultnoexcept

Move constructor.

Parameters
otherThe SparseArray to move from.

◆ ~SparseArray()

template<typename Component , typename Allocator = std::allocator<Component>>
SparseArray< Component, Allocator >::~SparseArray ( )
default

Destructor.

Member Function Documentation

◆ begin() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
iterator SparseArray< Component, Allocator >::begin ( )
inline

Returns an iterator to the beginning of the array.

Returns
An iterator to the first element.

Definition at line 97 of file SparseArray.hpp.

◆ begin() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator SparseArray< Component, Allocator >::begin ( ) const
inline

Returns a const iterator to the beginning of the array.

Returns
A const iterator to the first element.

Definition at line 106 of file SparseArray.hpp.

◆ cbegin()

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator SparseArray< Component, Allocator >::cbegin ( ) const
inline

Returns a const iterator to the beginning of the array.

Returns
A const iterator to the first element.

Definition at line 115 of file SparseArray.hpp.

◆ cend()

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator SparseArray< Component, Allocator >::cend ( ) const
inline

Returns a const iterator to the end of the array.

Returns
A const iterator to one past the last element.

Definition at line 142 of file SparseArray.hpp.

◆ emplace_at()

template<typename Component , typename Allocator = std::allocator<Component>>
template<class... Params>
reference_type SparseArray< Component, Allocator >::emplace_at ( size_type pos,
Params &&... params )
inline

Constructs and inserts a component at a specific position.

Template Parameters
ParamsThe types of the parameters for the component constructor.
Parameters
posThe position to insert the component at.
paramsThe parameters for constructing the component.
Returns
A reference to the inserted component.

Definition at line 190 of file SparseArray.hpp.

◆ end() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
iterator SparseArray< Component, Allocator >::end ( )
inline

Returns an iterator to the end of the array.

Returns
An iterator to one past the last element.

Definition at line 124 of file SparseArray.hpp.

◆ end() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator SparseArray< Component, Allocator >::end ( ) const
inline

Returns a const iterator to the end of the array.

Returns
A const iterator to one past the last element.

Definition at line 133 of file SparseArray.hpp.

◆ erase()

template<typename Component , typename Allocator = std::allocator<Component>>
void SparseArray< Component, Allocator >::erase ( size_type pos)
inline

Removes the component at a specific position.

Parameters
posThe position of the component to erase.

Definition at line 203 of file SparseArray.hpp.

◆ insert_at() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
reference_type SparseArray< Component, Allocator >::insert_at ( size_type pos,
Component && component )
inline

Inserts a component at a specific position (move version).

Parameters
posThe position to insert the component at.
componentThe component to insert (moved).
Returns
A reference to the inserted component.

Definition at line 175 of file SparseArray.hpp.

◆ insert_at() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
reference_type SparseArray< Component, Allocator >::insert_at ( size_type pos,
const Component & component )
inline

Inserts a component at a specific position.

Parameters
posThe position to insert the component at.
componentThe component to insert.
Returns
A reference to the inserted component.

Definition at line 162 of file SparseArray.hpp.

◆ operator=() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
SparseArray & SparseArray< Component, Allocator >::operator= ( const SparseArray< Component, Allocator > & other)
default

Copy assignment operator.

Parameters
otherThe SparseArray to copy from.
Returns
A reference to the current SparseArray.

◆ operator=() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
SparseArray & SparseArray< Component, Allocator >::operator= ( SparseArray< Component, Allocator > && other)
defaultnoexcept

Move assignment operator.

Parameters
otherThe SparseArray to move from.
Returns
A reference to the current SparseArray.

◆ operator[]() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
reference_type SparseArray< Component, Allocator >::operator[] ( std::size_t idx)
inline

Accesses the value at the given index.

Parameters
idxThe index to access.
Returns
A reference to the optional value at the specified index.

Definition at line 76 of file SparseArray.hpp.

◆ operator[]() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
const_reference_type SparseArray< Component, Allocator >::operator[] ( std::size_t idx) const
inline

Accesses the value at the given index (const version).

Parameters
idxThe index to access.
Returns
A const reference to the optional value at the specified index.

Definition at line 87 of file SparseArray.hpp.

◆ size()

template<typename Component , typename Allocator = std::allocator<Component>>
size_type SparseArray< Component, Allocator >::size ( ) const
inline

Returns the size of the array.

Returns
The number of elements in the array.

Definition at line 151 of file SparseArray.hpp.


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