vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::SparseSet< T > Class Template Reference

Dense-packed storage indexed by external uint32_t keys. More...

#include <sparse_set.h>

Inheritance diagram for Vkm::Engine::SparseSet< T >:
Vkm::Engine::ISparseSet

Public Member Functions

T & add (uint32_t key, T &&value)
 Insert an element at the given key.
void remove (uint32_t key)
 Remove the element at the given key via swap-and-pop.
void removeIfPresent (uint32_t key) override
 Remove the element at the given key if one is present.
bool contains (uint32_t key) const
 Test whether a key is present.
T & get (uint32_t key)
 Access the element at the given key.
template<typename Fn>
void forEach (Fn &&fn)
 Iterate all live elements densely (no holes).
size_t size () const
 Number of live elements.
void clear ()
 Drop every element.
void compact () override
 Shrink the sparse array to fit only live keys, reclaiming wasted memory.
uint32_t keyAt (uint32_t denseIndex) const
 Access the sparse key stored at a dense index.
T & dataAt (uint32_t denseIndex)
 Access the element stored at a dense index.

Detailed Description

template<typename T>
class Vkm::Engine::SparseSet< T >

Dense-packed storage indexed by external uint32_t keys.

Dense iteration; O(1) add, remove, contains and get; swap-and-pop removal. The caller owns the key lifecycle and generations.

Template Parameters
TElement type to store.

Member Function Documentation

◆ add()

template<typename T>
T & Vkm::Engine::SparseSet< T >::add ( uint32_t key,
T && value )
inline

Insert an element at the given key.

Parameters
keyExternal sparse key; not 0, not already present.
valueElement to insert.
Returns
The stored element.

◆ remove()

template<typename T>
void Vkm::Engine::SparseSet< T >::remove ( uint32_t key)
inline

Remove the element at the given key via swap-and-pop.

Parameters
keyExternal sparse key; must be present.

◆ removeIfPresent()

template<typename T>
void Vkm::Engine::SparseSet< T >::removeIfPresent ( uint32_t key)
inlineoverridevirtual

Remove the element at the given key if one is present.

Parameters
keyExternal sparse key; absent is a no-op.

Implements Vkm::Engine::ISparseSet.

◆ contains()

template<typename T>
bool Vkm::Engine::SparseSet< T >::contains ( uint32_t key) const
inline

Test whether a key is present.

Parameters
keyExternal sparse key.
Returns
True if the key maps to a live element.

◆ get()

template<typename T>
T & Vkm::Engine::SparseSet< T >::get ( uint32_t key)
inline

Access the element at the given key.

Parameters
keyExternal sparse key; must be present.
Returns
The stored element.

◆ forEach()

template<typename T>
template<typename Fn>
void Vkm::Engine::SparseSet< T >::forEach ( Fn && fn)
inline

Iterate all live elements densely (no holes).

Do not add or remove while this runs: swap-and-pop skips the moved element, and an add can reallocate under fn. Collect keys and act afterwards.

Template Parameters
FnCallable as void(uint32_t, T&).
Parameters
fnCalled once per live element, with its key, in packed order.

◆ size()

template<typename T>
size_t Vkm::Engine::SparseSet< T >::size ( ) const
inline

Number of live elements.

Returns
The dense array's length.

◆ clear()

template<typename T>
void Vkm::Engine::SparseSet< T >::clear ( )
inline

Drop every element.

Capacity is kept, so a rebuild does not allocate again.

◆ compact()

template<typename T>
void Vkm::Engine::SparseSet< T >::compact ( )
inlineoverridevirtual

Shrink the sparse array to fit only live keys, reclaiming wasted memory.

Always shrinks, since a bulk build in ascending key order leaves mostly geometric over-allocation rather than entries past the highest key. The dense arrays are unaffected.

Implements Vkm::Engine::ISparseSet.

◆ keyAt()

template<typename T>
uint32_t Vkm::Engine::SparseSet< T >::keyAt ( uint32_t denseIndex) const
inline

Access the sparse key stored at a dense index.

With size() and dataAt(), for index-based parallel iteration.

Parameters
denseIndexPosition in packed order (< size()).
Returns
The key at that slot.

◆ dataAt()

template<typename T>
T & Vkm::Engine::SparseSet< T >::dataAt ( uint32_t denseIndex)
inline

Access the element stored at a dense index.

Parameters
denseIndexPosition in packed order (< size()).
Returns
The element at that slot.

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