cubos::core::ecs::ArchetypeGraph class

Stores which column types each archetype holds and the edges which connect them.

These edges are bidirectional and indicate the addition or removal of column types, depending on the direction being taken. Neighboring archetypes (which have only one differing column type) are not always connected by an edge, as they are only generated the first time the traversal is done between those two archetypes.

In practice, this means that the first time a component is added to an entity of a given archetype, even if the target archetype already exists, a slow lookup must first be made. We cache the result of the lookup as a new edge, such that the next time the same component type is added, we just traverse the edge to find the target archetype's id.

Constructors, destructors, conversion operators

ArchetypeGraph()
Constructs.

Public functions

void reset()
Resets the graph to its initial state.
auto contains(ArchetypeId archetype, ColumnId id) const -> bool
Checks if the given archetype contains the column type with the given id.
auto with(ArchetypeId source, ColumnId id) -> ArchetypeId
Returns an archetype with same identifiers as the given source archetype, but with an extra column type with the given id.
auto without(ArchetypeId source, ColumnId id) -> ArchetypeId
Returns an archetype with same identifiers as the given source archetype, except for the column type with the given id, which is excluded.
auto first(ArchetypeId archetype) const -> ColumnId
Returns the first column type in the set of column types held by the given archetype.
auto next(ArchetypeId archetype, ColumnId id) const -> ColumnId
Returns the next column type in the set of column types held by the given archetype.
auto collect(ArchetypeId archetype, std::vector<ArchetypeId>& supersets, std::size_t seen = 0) const -> std::size_t
Collects all of the archetypes which are supersets of the given archetype.

Function documentation

void cubos::core::ecs::ArchetypeGraph::reset()

Resets the graph to its initial state.

Previously returned identifiers become invalid, as they might be reused.

bool cubos::core::ecs::ArchetypeGraph::contains(ArchetypeId archetype, ColumnId id) const

Checks if the given archetype contains the column type with the given id.

Parameters
archetype Archetype.
id Column type identifier.
Returns Whether the archetype contains the column type.

ArchetypeId cubos::core::ecs::ArchetypeGraph::with(ArchetypeId source, ColumnId id)

Returns an archetype with same identifiers as the given source archetype, but with an extra column type with the given id.

Parameters
source Source archetype.
id Extra column type identifier.
Returns Target archetype.

The column type with the given id must not already be present in source.

ArchetypeId cubos::core::ecs::ArchetypeGraph::without(ArchetypeId source, ColumnId id)

Returns an archetype with same identifiers as the given source archetype, except for the column type with the given id, which is excluded.

Parameters
source Source archetype.
id Excluded column type identifier.
Returns Target archetype.

The given column type with the given id must be present in source.

ColumnId cubos::core::ecs::ArchetypeGraph::first(ArchetypeId archetype) const

Returns the first column type in the set of column types held by the given archetype.

Parameters
archetype Archetype.
Returns First column type, or ColumnId::Invalid if the archetype is Empty.

ColumnId cubos::core::ecs::ArchetypeGraph::next(ArchetypeId archetype, ColumnId id) const

Returns the next column type in the set of column types held by the given archetype.

Parameters
archetype Archetype.
id Current column type.
Returns Next column type, or ColumnId::Invalid if there is no next column type.

std::size_t cubos::core::ecs::ArchetypeGraph::collect(ArchetypeId archetype, std::vector<ArchetypeId>& supersets, std::size_t seen = 0) const

Collects all of the archetypes which are supersets of the given archetype.

Parameters
archetype Base archetype.
supersets out Set to insert new found archetypes into.
seen Maximum previously seen archetype. To be used after the first call to this.
Returns Maximum seen archetype.

Returns the largest identifier checked during the call, which can then be used to only check new archetypes in later calls.