48template<
typename... Components>
52 using PoolsTuple = std::tuple<SparseSet<Components>*...>;
61 : entities_(entities), index_(index), pools_(pools) {
63 if (entities_ && index_ < entities_->size()) {
78 ES_ASSERT(entities_ !=
nullptr,
"Dereferencing null ViewIterator");
79 return (*entities_)[index_];
98 return index_ != other.index_;
107 return index_ == other.index_;
118 if (!entities_)
return;
119 while (index_ < entities_->size()) {
120 Entity entity = (*entities_)[index_];
121 if (allHave(entity)) {
133 bool allHave(Entity entity)
const {
134 return allHaveImpl(entity, std::index_sequence_for<Components...>{});
143 template<
usize... Is>
144 bool allHaveImpl(Entity entity, std::index_sequence<Is...>)
const {
145 return (std::get<Is>(pools_)->contains(entity) && ...);
148 const std::vector<Entity>* entities_;
183template<
typename... Components>
205 if (!smallest_)
return end();
206 return Iterator(&smallest_->entities(), 0, pools_);
214 if (!smallest_)
return Iterator(
nullptr, 0, pools_);
215 return Iterator(&smallest_->entities(), smallest_->size(), pools_);
230 return std::get<SparseSet<T>*>(pools_)->
get(entity);
236 return std::get<SparseSet<T>*>(pools_)->
get(entity);
245 return std::tuple<Components&...>(
246 std::get<SparseSet<Components>*>(pools_)->get(entity)...);
258 return !smallest_ || smallest_->empty();
269 return smallest_ ? smallest_->size() : 0;
276 template<
typename Func>
278 if (!smallest_)
return;
281 std::vector<Entity> entities = smallest_->entities();
283 for (
Entity entity : entities) {
284 if (!smallest_->contains(entity))
continue;
285 if (!allHaveExceptSmallest(entity))
continue;
287 if constexpr (std::is_invocable_v<Func,
Entity, Components&...>) {
288 func(entity, getByEntity<Components>(entity)...);
289 }
else if constexpr (std::is_invocable_v<Func, Components&...>) {
290 func(getByEntity<Components>(entity)...);
303 template<
typename Func>
305 if (!smallest_)
return;
307 const std::vector<Entity>& dense = smallest_->entities();
308 for (
usize i = 0; i < dense.size(); ++i) {
310 if (!allHaveExceptSmallest(entity))
continue;
312 if constexpr (std::is_invocable_v<Func,
Entity, Components&...>) {
313 func(entity, getByEntity<Components>(entity)...);
314 }
else if constexpr (std::is_invocable_v<Func, Components&...>) {
315 func(getByEntity<Components>(entity)...);
324 T& getByEntity(
Entity entity) {
325 return std::get<SparseSet<T>*>(pools_)->getUnchecked(entity);
328 bool allHaveExceptSmallest(
Entity entity)
const {
329 return allHaveExceptSmallestImpl(entity, std::index_sequence_for<Components...>{});
332 template<
usize... Is>
333 bool allHaveExceptSmallestImpl(Entity entity, std::index_sequence<Is...>)
const {
334 return ((
static_cast<SparseSetBase*
>(std::get<Is>(pools_)) == smallest_ ||
335 std::get<Is>(pools_)->contains(entity)) && ...);
344 void findSmallestPool() {
346 usize minSize = std::numeric_limits<usize>::max();
348 findSmallestImpl(minSize, std::index_sequence_for<Components...>{});
351 template<
usize... Is>
352 void findSmallestImpl(
usize& minSize, std::index_sequence<Is...>) {
353 ((checkPool<Is>(minSize)), ...);
357 void checkPool(
usize& minSize) {
358 auto* pool = std::get<I>(pools_);
359 if (pool && pool->size() < minSize) {
360 minSize = pool->size();
361 smallest_ =
static_cast<SparseSetBase*
>(pool);
368 SparseSetBase* smallest_ =
nullptr;
400 using Iterator =
typename std::vector<Entity>::const_iterator;
428 const T&
get(
Entity entity)
const {
return pool_->get(entity); }
435 std::tuple<T&>
getAll(
Entity entity) {
return std::tuple<T&>(pool_->get(entity)); }
441 bool empty()
const {
return !pool_ || pool_->empty(); }
447 usize size()
const {
return pool_ ? pool_->size() : 0; }
466 template<
typename Func>
473 std::vector<Entity> entities = pool_->entities();
475 for (
Entity entity : entities) {
476 if (!pool_->contains(entity))
continue;
477 if constexpr (std::is_invocable_v<Func, Entity, T&>) {
478 func(entity, pool_->getUnchecked(entity));
479 }
else if constexpr (std::is_invocable_v<Func, T&>) {
480 func(pool_->getUnchecked(entity));
493 template<
typename Func>
497 const std::vector<Entity>& dense = pool_->entities();
498 for (
usize i = 0; i < dense.size(); ++i) {
500 if constexpr (std::is_invocable_v<Func, Entity, T&>) {
501 func(entity, pool_->getUnchecked(entity));
502 }
else if constexpr (std::is_invocable_v<Func, T&>) {
503 func(pool_->getUnchecked(entity));
Entity utilities for the ECS system.
Logging utilities for ESEngine.
#define ES_ASSERT(condition, message)
Assert macro (debug builds only).
Sparse set data structure for efficient ECS component storage.
Core type definitions and utilities for ESEngine.
Central container for all entities and components.
Sparse set container for efficient entity-component storage.
Iterator for multi-component views.
bool operator==(const ViewIterator &other) const
Equality comparison.
Entity operator*() const
Dereferences to get the current entity.
bool operator!=(const ViewIterator &other) const
Inequality comparison.
ViewIterator(const std::vector< Entity > *entities, usize index, PoolsTuple pools)
Constructs an iterator.
std::tuple< SparseSet< Components > *... > PoolsTuple
Tuple type holding pointers to all component pools.
ViewIterator & operator++()
Advances to the next matching entity.
const T & get(Entity entity) const
Gets the component for an entity.
void each(Func &&func)
Executes a function for each entity.
typename std::vector< Entity >::const_iterator Iterator
Iterator type (directly iterates pool).
usize size() const
Gets the exact number of entities in the view.
void eachLive(Func &&func)
Like each(), but iterates the live dense array with no defensive copy.
Iterator end() const
Returns iterator past the last entity.
View(SparseSet< T > *pool)
Constructs a single-component view.
usize sizeHint() const
Upper bound on matching entities — exact here, since nothing filters.
bool empty() const
Checks if the view is empty.
T & get(Entity entity)
Gets the component for an entity.
std::tuple< T & > getAll(Entity entity)
Gets all components for an entity as a tuple.
Iterator begin() const
Returns iterator to the first entity.
ViewIterator< Components... > Iterator
Iterator type.
Iterator begin() const
Returns iterator to the first matching entity.
T & get(Entity entity)
Gets a specific component from an entity.
std::tuple< Components &... > getAll(Entity entity)
Gets all components for an entity as a tuple.
std::tuple< SparseSet< Components > *... > PoolsTuple
Tuple type for pool pointers.
void eachLive(Func &&func)
Like each(), but iterates the live dense array with no defensive copy.
View(PoolsTuple pools)
Constructs a view from component pools.
const T & get(Entity entity) const
Gets a specific component from an entity.
usize sizeHint() const
Gets the estimated number of matching entities.
bool empty() const
Checks if the view is empty.
Iterator end() const
Returns iterator past the last entity.
std::size_t usize
Size type for indexing and sizes.
Entity identifier with packed index + generation.