class ecs::SparseSet

Similar to an array, but saves memory by not allocating data if there are large gaps in the stored indices. i.e. while a regular array would require you to allocate an array of size 100 000 to store an entry at index 99 999, sparse set will only allocate a single page of data.

Internally the set works by allocating two arrays:

  • Sparse: Stores lookup from the user-provided index to the internal packed array index. Uses pages to avoid allocating memory for unused index ranges.
  • Packed: Stores all the data in one contiguous array.

Packed array can be iterated as efficiently as a regular array, while lookups based on index come with a cost of an additional layer of indirection (packed[sparse[index]]), as well as some arithmetic for page calculation.

Public

Constructors

SparseSet

SparseSet(TypeHash elementTypeHash = B3DGetTypeHash<void>())

Methods

~SparseSet

virtual ~SparseSet() noexcept

GetElementTypeHash

TypeHash GetElementTypeHash() const

Returns a hash value that represents the element type stored in this set.

Add

Iterator Add(Entity entity)

Adds a new entity to the set and returns an iterator of the added entity.

Caller must ensure not to add an entity with an index that's already in the set.

Add

Iterator Add(Iterator first, Iterator last)

Adds all entities from the provided iterator range to the set, and returns a pointer to the last added entity.

Caller must ensure not to add an entity with an index that's already in the set.

Erase

void Erase(Entity entity)

Removes an entity from the set.

Caller must ensure that entity is actually part of the set.

Erase

void Erase(Iterator first, Iterator last)

Removes all entities from the provided iterator range from the set.

Caller must ensure that entity is actually part of the set.

EraseIfValid

bool EraseIfValid(Entity entity)

Removes an entity from the set if it exists, otherwise does nothing.

Returns true if entity was removed.

EraseIfValid

u64 EraseIfValid(Iterator first, Iterator last)

Removes all existing entities from the provided iterator range from the set.

Ignores entities that are not part of the set. Returns number of entities that were removed.

Clear

virtual void Clear()

Removes everything from the set and clears the internal arrays.

ClearInvalid

virtual void ClearInvalid()

Removes all invalid entities from the set.

Only relevant for sets using in-place deletion policy.

Shrink

virtual void Shrink()

Shrinks the memory use of the set to accomodate the currently assigned entries, without any reserve for new entries.

Reserve

virtual void Reserve(u64 capacity)

Reserves internal memory in order to fit entries.

Capacity

virtual u64 Capacity() const

Returns how many entries can fit into the internal memory.

GetVersion

Entity::VersionType GetVersion(Entity entity) const

Returns the current version of the provided entity, or invalid version if entity is not part of this set.

UpdateVersion

void UpdateVersion(Entity entity)

Updates the version for the entity with identifier as specified by .

New version is taken from the provided parameter.

Find

Iterator Find(Entity entity) const

Returns the iterator to an entity with the provided identifier and version.

Returns an iterator past the edge of the set if no entity is found.

Contains

bool Contains(Entity entity) const

Returns true if the set contains an entity with the provided identifier and version.

GetPackedIndex

u64 GetPackedIndex(Entity entity) const

Returns an index of an entity in the internal packed array.

If the entity is not part of the set, behaviour is undefined.

Size

u64 Size() const

Returns the number of entities in the set.

Note for in-place and swap-only deletion policies this will also count the number of invalid entries.

IsEmpty

bool IsEmpty() const

Returns true if no entries are stored in the set.

Data

const Entity *Data() const

Returns raw pointer to the internal packed entity array.

Swap

virtual void Swap(Entity lhs, Entity rhs) = 0

Swaps the location of the provided entities in the packed array, as well as the relevant payload (data associated with the entities), if any.

GetDeletePolicy

virtual SparseSetDeletePolicy GetDeletePolicy() const

Returns the current delete policy.

GetFirstFreeElementPackedIndex

virtual u64 GetFirstFreeElementPackedIndex() const

When using in-place deletion policy returns the packed index to the first invalid element.

When using swap-only deletion policy returns the number of valid entities in the set. Not relevant if using swap-and-erase deletion policy.

Begin

Iterator Begin() const

Returns an iterator to the start of the internal packed entity array.

Cbegin

ConstIterator Cbegin() const

End

Iterator End() const

Returns an iterator to the past the end of the internal packed entity array.

Cend

ConstIterator Cend() const

Rbegin

ReverseIterator Rbegin() const

Returns a reverse iterator to the last element of the internal packed entity array.

Crbegin

ConstReverseIterator Crbegin() const

Rend

ReverseIterator Rend() const

Returns a reverse iterator before the first element of the internal packed entity array.

Crend

ConstReverseIterator Crend() const

begin

iterator begin() const

cbegin

const_iterator cbegin() const

end

iterator end() const

cend

const_iterator cend() const

rbegin

reverse_iterator rbegin() const

crbegin

const_reverse_iterator crbegin() const

rend

reverse_iterator rend() const

crend

const_reverse_iterator crend() const

Fields

OnWasAdded

Event<void (Entity), ThreadUnsafe> OnWasAdded

Triggers any time a new entity is added to the set.

OnWillRemove

Event<void (Entity), ThreadUnsafe> OnWillRemove

Triggers right before an entity is removed from the set.

OnWasUpdated

Event<void (Entity), ThreadUnsafe> OnWasUpdated

Triggers when an existing component is updated or replaced.

Must be explicitly triggered by the caller.

Operators

operator[]

Entity operator[](u64 index)

Protected

Methods

AddInternal

virtual Iterator AddInternal(Entity entity, bool forceAddAtEnd)

Adds a new entity to the set.

entity
Entity to add.
forceAddAtEnd
Only relevant when using in-place deletion policy. When true it will add an entity at the end of the packaged data array, rather than re-using the first available invalid entity entry.

Returns: Iterator to the added entity.

EraseInternal

virtual void EraseInternal(Entity entity)

Removed an entity from the sparse set.

Entity must be a part of the sparse set.

ClearInternal

void ClearInternal()

Removes everything from the set and clears the internal arrays.

SwapEntities

void SwapEntities(u64 lhsPackedIndex, u64 rhsPackedIndex)

Swaps the location of two entities.

lhsPackedIndex
Index within the packed array of the first element to swap.
rhsPackedIndex
Index within the packed array of the second element to swap.

GetOrCreateSparseEntryReference

Entity &GetOrCreateSparseEntryReference(Entity entity)

Retrieves an existing entry from the sparse array, or adds a new entry if one doesn't already exist.

Note that returned entity is not a regular entity, but rather its identifier is an index into the packed array.

GetSparseEntryReference

Entity &GetSparseEntryReference(Entity value) const

Retrieves an existing entry from the sparse array.

Note that returned entity is not a regular entity, but rather its identifier is an index into the packed array. Caller must ensure the entity is part of the set before calling.

GetSparseEntryPointer

Entity *GetSparseEntryPointer(Entity value) const

Attempts to retrieve an existing entry from the sparse array, or null if one cannot be found.

Note that returned entity is not a regular entity, but rather its identifier is an index into the packed array.

GetIterator

Iterator GetIterator(Entity entity) const

Returns an iterator to the provided entity.

Caller must ensure the entity is part of the set before calling.

FreeSparsePages

void FreeSparsePages()

Frees any sparse pages that don't contain any entries.

staticGetPackedIndexAsEntryIdentifier

static constexpr Entity::IdentifierType GetPackedIndexAsEntryIdentifier(u64 packedIndex)

Converts an index into the packed array into an entity identifier.

staticGetSparseIndexWithinPage

static constexpr u64 GetSparseIndexWithinPage(u64 entityIdentifier)

Calculates an index within a page, for an entity with the provided identifier.

staticGetSparsePage

static constexpr u64 GetSparsePage(u64 entityIdentifier)

Calculates the page at which to store the entity with the provided identifier.

Fields

mSparseIndices

SparseContainerType mSparseIndices

List of pages that map entity identifier into packed array indices.

mPackedEntities

PackedContainerType mPackedEntities

Packed array of entities.

mElementTypeHash

TypeHash mElementTypeHash