SparseSet Class Template
A generic sparse set providing O(1) insertion, lookup, and removal. More...
Declaration
Base class
| class | SparseSetBase |
|
Abstract base class for type-erased sparse set access. More... | |
Public Constructors Index
template <typename T> | |
| SparseSet ()=default | |
|
Default constructor creating an empty sparse set. More... | |
template <typename T> | |
| SparseSet (const size_t capacity) | |
|
Constructs a sparse set with pre-allocated capacity. More... | |
template <typename T> | |
| SparseSet (const SparseSet &)=delete | |
|
Copy operations are deleted to prevent accidental duplication. More... | |
template <typename T> | |
| SparseSet (SparseSet &&) noexcept=default | |
|
Move constructor. More... | |
Public Operators Index
template <typename T> | |
| SparseSet & | operator= (const SparseSet &)=delete |
|
Copy assignment is deleted. More... | |
template <typename T> | |
| SparseSet & | operator= (SparseSet &&) noexcept=default |
|
Move assignment operator. More... | |
Public Member Functions Index
template <typename... Args> | |
| T * | emplace (const EntityId idx, Args &&...args) |
|
Constructs and inserts an element at the given index. More... | |
template <typename T> | |
| T * | insert (const EntityId idx, T &&obj) |
|
Inserts an element at the given index. More... | |
template <typename T> | |
| bool | remove (const EntityId idx) override |
|
Removes the element at the given index using swap-and-pop. More... | |
template <typename T> | |
| T * | get (const EntityId idx) |
|
Retrieves the element at the given index. More... | |
template <typename T> | |
| const T * | get (const EntityId idx) const |
|
Retrieves the element at the given index. More... | |
template <typename T> | |
| bool | contains (const EntityId idx) const override |
|
Checks whether an element is registered for the specified EntityId. More... | |
template <typename T> | |
| void * | raw (const EntityId id) override |
|
Returns a raw void pointer to the element at the given index. More... | |
template <typename T> | |
| Iterator | begin () |
|
Returns an iterator to the beginning of the dense storage. More... | |
template <typename T> | |
| Iterator | end () |
|
Returns an iterator to the end of the dense storage. More... | |
template <typename T> | |
| ConstIterator | begin () const |
|
Returns a const iterator to the beginning of the dense storage. More... | |
template <typename T> | |
| ConstIterator | end () const |
|
Returns a const iterator to the end of the dense storage. More... | |
Private Member Attributes Index
template <typename T> | |
| std::vector< size_t > | sparse_ |
|
Maps EntityId to dense storage index. More... | |
template <typename T> | |
| std::vector< EntityId > | denseToSparse_ |
|
Reverse mapping from dense index to EntityId. More... | |
template <typename T> | |
| std::vector< T > | storage_ |
|
Contiguous storage of elements. More... | |
Description
A generic sparse set providing O(1) insertion, lookup, and removal.
SparseSet implements the sparse set data structure pattern, commonly used in Entity Component Systems (ECS) for efficient storage. It maps EntityId indices to densely packed data of type T.
Definition at line 118 of file SparseSet.ixx.
Public Constructors
SparseSet()
| default |
Default constructor creating an empty sparse set.
Definition at line 145 of file SparseSet.ixx.
SparseSet()
| inline explicit |
Constructs a sparse set with pre-allocated capacity.
- Parameters
-
capacity The initial capacity to reserve for all internal vectors.
Definition at line 152 of file SparseSet.ixx.
SparseSet()
| delete |
Copy operations are deleted to prevent accidental duplication.
Definition at line 162 of file SparseSet.ixx.
SparseSet()
| noexcept default |
Move constructor.
Definition at line 172 of file SparseSet.ixx.
Public Operators
operator=()
| delete |
Copy assignment is deleted.
Definition at line 167 of file SparseSet.ixx.
operator=()
| noexcept default |
Move assignment operator.
Definition at line 177 of file SparseSet.ixx.
Public Member Functions
begin()
| inline |
begin()
| inline |
Returns a const iterator to the beginning of the dense storage.
- Returns
ConstIterator pointing to the first element.
Definition at line 468 of file SparseSet.ixx.
contains()
| inline virtual |
Checks whether an element is registered for the specified EntityId.
- Parameters
-
idx The EntityId to test.
- Returns
True if this sparse set contains the EntityId.
Definition at line 325 of file SparseSet.ixx.
Reference helios::ecs::Tombstone.
emplace()
| inline |
Constructs and inserts an element at the given index.
Forwards arguments to construct T in-place.
- Template Parameters
-
Args Constructor argument types.
- Parameters
-
idx The EntityId to associate with the element.
args Arguments forwarded to the T constructor.
- Returns
Pointer to the inserted element, or nullptr if the index is already occupied.
Definition at line 193 of file SparseSet.ixx.
Reference helios::ecs::Tombstone.
Referenced by helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::emplace.
end()
| inline |
end()
| inline |
Returns a const iterator to the end of the dense storage.
- Returns
ConstIterator pointing past the last element.
Definition at line 477 of file SparseSet.ixx.
get()
| inline |
Retrieves the element at the given index.
- Parameters
-
idx The EntityId to look up.
- Returns
Pointer to the element, or nullptr if not found.
Definition at line 292 of file SparseSet.ixx.
Reference helios::ecs::Tombstone.
Referenced by helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::get and helios::ecs::SparseSet< T >::raw.
get()
| inline |
Retrieves the element at the given index.
- Parameters
-
idx The EntityId to look up.
- Returns
Const pointer to the element, or nullptr if not found.
Definition at line 308 of file SparseSet.ixx.
Reference helios::ecs::Tombstone.
insert()
| inline |
Inserts an element at the given index.
If the sparse array is too small, it is resized to accommodate the index. Empty slots are filled with Tombstone.
- Parameters
-
idx The EntityId to associate with the element.
obj The element to insert (moved).
- Returns
Pointer to the inserted element, or nullptr if the index is already occupied.
Definition at line 225 of file SparseSet.ixx.
Reference helios::ecs::Tombstone.
raw()
| inline virtual |
Returns a raw void pointer to the element at the given index.
- Parameters
-
id The EntityId to look up.
- Returns
Raw pointer to the element, or nullptr if not found.
Definition at line 332 of file SparseSet.ixx.
Reference helios::ecs::SparseSet< T >::get.
remove()
| inline virtual |
Removes the element at the given index using swap-and-pop.
Uses the swap-and-pop technique for O(1) removal:
- Move the last element to the position of the removed element
- Update the sparse array entry for the moved element
- Pop the last element from dense storage
- Mark the removed slot as Tombstone
- Parameters
-
idx The EntityId of the element to remove.
- Returns
True if the element was removed, false if not found.
Definition at line 259 of file SparseSet.ixx.
Reference helios::ecs::Tombstone.
Private Member Attributes
denseToSparse_
|
Reverse mapping from dense index to EntityId.
Used during swap-and-pop removal to update the sparse array.
Definition at line 133 of file SparseSet.ixx.
sparse_
|
Maps EntityId to dense storage index.
Contains Tombstone for empty slots.
Definition at line 126 of file SparseSet.ixx.
storage_
|
Contiguous storage of elements.
Definition at line 138 of file SparseSet.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.