|
Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
|
Sparse set container for efficient entity-component storage. More...
#include <SparseSet.hpp>

Public Types | |
| using | Iterator = typename std::vector<Entity>::const_iterator |
| Iterator type for entity iteration. | |
| using | Page = std::array<u32, SPARSE_PAGE_SIZE> |
| Page type: array of dense indices. | |
Public Member Functions | |
| SparseSet ()=default | |
| Default constructor. | |
| ~SparseSet () override=default | |
| Destructor. | |
| SparseSet (const SparseSet &)=delete | |
| SparseSet & | operator= (const SparseSet &)=delete |
| SparseSet (SparseSet &&) noexcept=default | |
| SparseSet & | operator= (SparseSet &&) noexcept=default |
| bool | contains (Entity entity) const override |
| Checks if an entity has a component in this set. | |
| T & | get (Entity entity) |
| Gets the component for an entity (must exist). | |
| const T & | get (Entity entity) const |
| Gets the component for an entity (must exist). | |
| T & | getUnchecked (Entity entity) |
| const T & | getUnchecked (Entity entity) const |
| T * | tryGet (Entity entity) |
| Tries to get a component (returns nullptr if not found). | |
| const T * | tryGet (Entity entity) const |
| Tries to get a component (returns nullptr if not found). | |
| u32 | changedTick (Entity entity) const |
The tick of entity's last write, 0 when it has none here. | |
| u32 | changedTickOf (Entity entity) const override |
The tick of entity's last write here, 0 where it has no component: the type-erased changedTick. | |
| void | markChanged (Entity entity, u32 tick) override |
Stamps entity's component with tick: it was written. No-op for an entity without one. | |
| u32 * | changeTicks () |
| The stamps, parallel to components(): where a writer that holds a component's place stamps it. | |
| const u32 * | changeTicks () const |
| u32 * | lastChangedSlot () |
| Where the pool's last-change tick lives, for a writer that stamps through changeTicks() to lift with it. | |
| u32 | lastChangedTick () const override |
| The tick of the last write to any component of this pool, or of the last add or remove: "anything since?" in O(1). | |
| void | noteStructural (u32 tick) override |
Notes an add or a remove at tick, which no stamp of a component that is still there records. | |
| void | clampStamps (u32 floor) override |
Lifts every stamp older than floor to it, so a stamp never reads as newer after the tick wraps. | |
| std::string_view | typeName () const override |
| The component type's name, for what the audit says. | |
| bool | auditable () const override |
| Whether this pool's components can be audited (see above). | |
| void | auditKeep (AuditCopy ©, u32 tick) const override |
Keeps the pool's state as it is at tick. | |
| u32 | auditCompare (const AuditCopy ©, void(*report)(void *, Entity), void *user) const override |
Calls report for each entity whose component changed since copy without a stamp after copy.tick. | |
| template<typename... Args> | |
| T & | emplace (Entity entity, Args &&... args) |
| Adds a component to an entity with in-place construction. | |
| void | remove (Entity entity) override |
| Removes a component from an entity. | |
| usize | size () const override |
| Gets the number of components in the set. | |
| bool | empty () const override |
| Checks if the set is empty. | |
| void | clear () override |
| Removes all components. | |
| u32 | version () const override |
| Monotonic version, bumped whenever stored components may have moved in memory (buffer reallocation on growth, or swap-and-pop relocation on removal). | |
| void | reorderBy (const std::vector< u32 > &rank) override |
Permutes storage so iteration follows rank. | |
| void | permute (std::vector< usize > &perm) |
| Puts slot perm[i] at position i, for every i: the entity, its component and its stamp together. | |
| void | rebuildSparse () |
| Iterator | begin () const |
| Returns iterator to the first entity. | |
| Iterator | end () const |
| Returns iterator past the last entity. | |
| const std::vector< Entity > & | entities () const override |
| Gets the dense entity array. | |
| std::vector< T > & | components () |
| Gets the component array for direct iteration. | |
| const std::vector< T > & | components () const |
| Gets the component array for direct iteration. | |
| usize | indexOf (Entity entity) const |
| Gets the dense array index for an entity. | |
| Public Member Functions inherited from esengine::ecs::SparseSetBase | |
| virtual | ~SparseSetBase ()=default |
| Virtual destructor for proper cleanup. | |
Static Public Attributes | |
| static constexpr usize | SPARSE_PAGE_SIZE = 4096 |
| Page size for paged sparse array (entities per page). | |
| static constexpr u32 | INVALID_INDEX = INVALID_DENSE_INDEX |
| Sentinel for empty sparse slots. | |
| static constexpr bool | kAuditBytes = std::is_trivially_copyable_v<T> |
| static constexpr bool | kAuditValues = !kAuditBytes && std::is_copy_constructible_v<T> && std::equality_comparable<T> |
| Static Public Attributes inherited from esengine::ecs::SparseSetBase | |
| static constexpr u32 | UNRANKED = 0xFFFFFFFFu |
| Rank for an entity the caller did not order — sorts last, stably. | |
Additional Inherited Members | |
| Static Public Member Functions inherited from esengine::ecs::SparseSetBase | |
| static std::vector< usize > | stablePermutation (const std::vector< Entity > &dense, const std::vector< u32 > &rank) |
| Which current slot belongs at each position when dense is put in rank order (stable). | |
Sparse set container for efficient entity-component storage.
| T | The component type to store |
The sparse set uses a paged sparse array and dense arrays:
This provides:
The "swap and pop" deletion strategy maintains density without gaps, at the cost of unstable ordering.
Memory efficiency: With paging, entity ID 1,000,000 only allocates 244 pages (~16KB) instead of 4MB.
Definition at line 256 of file SparseSet.hpp.
| using esengine::ecs::SparseSet< T >::Iterator = typename std::vector<Entity>::const_iterator |
Iterator type for entity iteration.
Definition at line 259 of file SparseSet.hpp.
| using esengine::ecs::SparseSet< T >::Page = std::array<u32, SPARSE_PAGE_SIZE> |
Page type: array of dense indices.
Definition at line 268 of file SparseSet.hpp.
|
default |
Default constructor.
Referenced by SparseSet(), SparseSet(), operator=(), and operator=().
|
overridedefault |
Destructor.
|
delete |
References SparseSet().
|
defaultnoexcept |
References SparseSet().
|
inlineoverridevirtual |
Whether this pool's components can be audited (see above).
Implements esengine::ecs::SparseSetBase.
Definition at line 386 of file SparseSet.hpp.
References kAuditBytes, and kAuditValues.
|
inlineoverridevirtual |
Calls report for each entity whose component changed since copy without a stamp after copy.tick.
Implements esengine::ecs::SparseSetBase.
Definition at line 399 of file SparseSet.hpp.
References esengine::ecs::SparseSetBase::AuditCopy::bytes, esengine::ecs::SparseSetBase::AuditCopy::entities, INVALID_INDEX, kAuditBytes, kAuditValues, esengine::ecs::SparseSetBase::AuditCopy::tick, esengine::ecs::tickAfter(), and esengine::ecs::SparseSetBase::AuditCopy::values.
|
inlineoverridevirtual |
Keeps the pool's state as it is at tick.
Implements esengine::ecs::SparseSetBase.
Definition at line 387 of file SparseSet.hpp.
References esengine::ecs::SparseSetBase::AuditCopy::bytes, esengine::ecs::SparseSetBase::AuditCopy::entities, kAuditBytes, kAuditValues, esengine::ecs::SparseSetBase::AuditCopy::tick, and esengine::ecs::SparseSetBase::AuditCopy::values.
|
inline |
Returns iterator to the first entity.
Definition at line 634 of file SparseSet.hpp.
Referenced by emplace(), and rebuildSparse().
|
inline |
The tick of entity's last write, 0 when it has none here.
Definition at line 352 of file SparseSet.hpp.
References INVALID_INDEX.
Referenced by changedTickOf().
|
inlineoverridevirtual |
The tick of entity's last write here, 0 where it has no component: the type-erased changedTick.
Implements esengine::ecs::SparseSetBase.
Definition at line 357 of file SparseSet.hpp.
References changedTick().
|
inline |
The stamps, parallel to components(): where a writer that holds a component's place stamps it.
Definition at line 368 of file SparseSet.hpp.
|
inline |
Definition at line 369 of file SparseSet.hpp.
|
inlineoverridevirtual |
Lifts every stamp older than floor to it, so a stamp never reads as newer after the tick wraps.
Implements esengine::ecs::SparseSetBase.
Definition at line 378 of file SparseSet.hpp.
References esengine::ecs::tickAfter().
|
inlineoverridevirtual |
Removes all components.
Implements esengine::ecs::SparseSetBase.
Definition at line 546 of file SparseSet.hpp.
|
inline |
Gets the component array for direct iteration.
Allows iterating components directly without looking up by entity. Components are stored in the same order as entities().
Definition at line 657 of file SparseSet.hpp.
|
inline |
Gets the component array for direct iteration.
Allows iterating components directly without looking up by entity. Components are stored in the same order as entities().
Definition at line 660 of file SparseSet.hpp.
|
inlineoverridevirtual |
Checks if an entity has a component in this set.
| entity | The entity to check |
O(1) complexity. Validates the sparse-dense mapping. Uses paged lookup to avoid unbounded memory growth.
Implements esengine::ecs::SparseSetBase.
Definition at line 294 of file SparseSet.hpp.
References contains(), and INVALID_INDEX.
Referenced by contains(), emplace(), and remove().
|
inline |
Adds a component to an entity with in-place construction.
| Args | Constructor argument types |
| entity | The entity to add a component to |
| args | Arguments forwarded to T's constructor |
O(1) amortized complexity. Allocates pages on-demand. Memory usage is bounded: O(num_pages * SPARSE_PAGE_SIZE) where num_pages = ceil(max_entity_id / SPARSE_PAGE_SIZE).
Definition at line 448 of file SparseSet.hpp.
References begin(), contains(), end(), ES_VERIFY, get(), esengine::Entity::index(), INVALID_INDEX, and SPARSE_PAGE_SIZE.
|
inlineoverridevirtual |
Checks if the set is empty.
Implements esengine::ecs::SparseSetBase.
Definition at line 541 of file SparseSet.hpp.
|
inline |
Returns iterator past the last entity.
Definition at line 640 of file SparseSet.hpp.
Referenced by emplace(), and rebuildSparse().
|
inlineoverridevirtual |
Gets the dense entity array.
Implements esengine::ecs::SparseSetBase.
Definition at line 646 of file SparseSet.hpp.
|
inline |
Gets the component for an entity (must exist).
| entity | The entity |
Definition at line 308 of file SparseSet.hpp.
References ES_VERIFY_FATAL, and INVALID_INDEX.
Referenced by emplace().
|
inline |
Gets the component for an entity (must exist).
| entity | The entity |
Definition at line 315 of file SparseSet.hpp.
References ES_VERIFY_FATAL, and INVALID_INDEX.
|
inline |
Definition at line 321 of file SparseSet.hpp.
|
inline |
Definition at line 325 of file SparseSet.hpp.
|
inline |
Gets the dense array index for an entity.
| entity | The entity |
Definition at line 669 of file SparseSet.hpp.
References ES_VERIFY, and INVALID_INDEX.
|
inline |
Where the pool's last-change tick lives, for a writer that stamps through changeTicks() to lift with it.
Definition at line 372 of file SparseSet.hpp.
|
inlineoverridevirtual |
The tick of the last write to any component of this pool, or of the last add or remove: "anything since?" in O(1).
Implements esengine::ecs::SparseSetBase.
Definition at line 374 of file SparseSet.hpp.
|
inlineoverridevirtual |
Stamps entity's component with tick: it was written. No-op for an entity without one.
Implements esengine::ecs::SparseSetBase.
Definition at line 360 of file SparseSet.hpp.
References INVALID_INDEX, and esengine::ecs::tickAfter().
|
inlineoverridevirtual |
Notes an add or a remove at tick, which no stamp of a component that is still there records.
Implements esengine::ecs::SparseSetBase.
Definition at line 375 of file SparseSet.hpp.
References esengine::ecs::tickAfter().
|
delete |
References SparseSet().
|
defaultnoexcept |
References SparseSet().
|
inline |
Puts slot perm[i] at position i, for every i: the entity, its component and its stamp together.
The one way storage is reordered (reorderBy, Registry::sort). A permutation that moves nothing leaves storage, and every handed-out pointer, alone. Consumes perm.
Definition at line 575 of file SparseSet.hpp.
References rebuildSparse().
Referenced by reorderBy().
|
inline |
Definition at line 606 of file SparseSet.hpp.
References begin(), end(), INVALID_INDEX, and SPARSE_PAGE_SIZE.
Referenced by permute().
|
inlineoverridevirtual |
Removes a component from an entity.
| entity | The entity whose component to remove |
O(1) complexity using swap-and-pop. The last element is moved to fill the gap, maintaining density. No-op if the entity doesn't have a component.
Implements esengine::ecs::SparseSetBase.
Definition at line 492 of file SparseSet.hpp.
References contains(), esengine::Entity::index(), INVALID_INDEX, and SPARSE_PAGE_SIZE.
|
inlineoverridevirtual |
Permutes storage so iteration follows rank.
| rank | Rank per ENTITY INDEX (Entity::index()); lower ranks iterate first |
Storage order is iteration order, and iteration order is the renderer's within-layer painter order, so this is how a scene's authored entity order reaches the draw list without respawning anything. Entities with no rank (index past the end, or the UNRANKED sentinel) keep their relative order after the ranked ones — the sort is stable. A no-op reorder leaves storage (and therefore every handed-out component pointer) untouched.
Implements esengine::ecs::SparseSetBase.
Definition at line 558 of file SparseSet.hpp.
References permute(), and esengine::ecs::SparseSetBase::stablePermutation().
|
inlineoverridevirtual |
Gets the number of components in the set.
Implements esengine::ecs::SparseSetBase.
Definition at line 535 of file SparseSet.hpp.
|
inline |
Tries to get a component (returns nullptr if not found).
| entity | The entity |
Definition at line 334 of file SparseSet.hpp.
References INVALID_INDEX.
|
inline |
Tries to get a component (returns nullptr if not found).
| entity | The entity |
Definition at line 341 of file SparseSet.hpp.
References INVALID_INDEX.
|
inlineoverridevirtual |
The component type's name, for what the audit says.
Implements esengine::ecs::SparseSetBase.
Definition at line 382 of file SparseSet.hpp.
References esengine::ecs::componentTypeName().
|
inlineoverridevirtual |
Monotonic version, bumped whenever stored components may have moved in memory (buffer reallocation on growth, or swap-and-pop relocation on removal).
Single source of truth for the cross-boundary pointer-staleness guard: a raw component pointer handed to JS (e.g. getTransformPtr) is only valid while this value is unchanged. Replaces the old SchemaComponentPool::poolVersion (which lived in a now-deleted parallel store and, notably, was not bumped on remove).
Implements esengine::ecs::SparseSetBase.
Definition at line 555 of file SparseSet.hpp.
|
staticconstexpr |
Sentinel for empty sparse slots.
Definition at line 265 of file SparseSet.hpp.
Referenced by auditCompare(), changedTick(), contains(), emplace(), get(), get(), indexOf(), markChanged(), rebuildSparse(), remove(), tryGet(), and tryGet().
|
staticconstexpr |
Definition at line 384 of file SparseSet.hpp.
Referenced by auditable(), auditCompare(), and auditKeep().
|
staticconstexpr |
Definition at line 385 of file SparseSet.hpp.
Referenced by auditable(), auditCompare(), and auditKeep().
|
staticconstexpr |
Page size for paged sparse array (entities per page).
Definition at line 262 of file SparseSet.hpp.
Referenced by emplace(), rebuildSparse(), and remove().