2#include "gaia/config/config.h"
6#include "gaia/cnt/darray.h"
7#include "gaia/cnt/map.h"
8#include "gaia/core/utility.h"
9#include "gaia/ecs/id.h"
18 struct PairSourceRecord {
22 uint32_t targetIdx = BadIndex;
32 uint32_t srcToTgtCnt = 0;
39 GAIA_NODISCARD uint32_t target_source_index(
Entity source)
const {
40 if GAIA_UNLIKELY (source.
pair()) {
42 return it != pairSrcToTgt.
end() ? it->second.targetIdx : BadIndex;
45 return source.
id() < srcToTgtIdx.
size() ? srcToTgtIdx[source.
id()] : BadIndex;
51 void set_target_source_index(
Entity source, uint32_t index) {
52 if GAIA_UNLIKELY (source.
pair()) {
54 GAIA_ASSERT(it != pairSrcToTgt.
end());
55 if (it != pairSrcToTgt.
end())
56 it->second.targetIdx = index;
60 GAIA_ASSERT(source.
id() < srcToTgtIdx.
size());
61 srcToTgtIdx[source.
id()] = index;
68 const auto required = (uint32_t)source.
id() + 1;
69 if (srcToTgt.
size() >= required)
72 const auto oldSize = (uint32_t)srcToTgt.
size();
73 auto newSize = oldSize == 0 ? 16U : oldSize;
74 while (newSize < required)
77 srcToTgt.
resize(newSize, EntityBad);
78 srcToTgtIdx.
resize(newSize, BadIndex);
84 const auto required =
target.
id() + 1;
85 if (tgtToSrc.
size() >= required)
88 const auto oldSize = (uint32_t)tgtToSrc.
size();
89 auto newSize = oldSize == 0 ? 16U : oldSize;
90 while (newSize < required)
100 if GAIA_UNLIKELY (source.
pair()) {
102 return it != pairSrcToTgt.
end() ? it->second.target : EntityBad;
105 if (source.
id() >= srcToTgt.
size())
108 return srcToTgt[source.
id()];
132 GAIA_FOR((uint32_t)srcToTgt.
size()) {
133 if (srcToTgt[i] == EntityBad)
143 const auto first = out.
size();
144 out.
reserve(first + (uint32_t)pairSrcToTgt.
size());
145 for (
const auto&
pair: pairSrcToTgt)
160 const auto idx = target_source_index(source);
161 GAIA_ASSERT(idx != BadIndex && idx <
sources.
size());
166 if (idx != lastIdx) {
167 const auto movedSource =
sources[lastIdx];
169 set_target_source_index(movedSource, idx);
180 if GAIA_LIKELY (!source.
pair())
182 const auto oldTarget = this->
target(source);
183 if (oldTarget != EntityBad) {
195 if GAIA_UNLIKELY (source.
pair())
199 srcToTgtIdx[source.
id()] = sourceIdx;
211 const auto oldTarget = this->
target(source);
212 if (oldTarget == EntityBad)
218 if GAIA_UNLIKELY (source.
pair())
221 srcToTgt[source.
id()] = EntityBad;
222 srcToTgtIdx[source.
id()] = BadIndex;
224 GAIA_ASSERT(srcToTgtCnt > 0);
232 return srcToTgtCnt == 0;
Array of elements of type.
Definition darray_ext_impl.h:31
void pop_back() noexcept
Removes the last element.
Definition darray_ext_impl.h:400
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_ext_impl.h:561
void reserve(size_type cap)
Ensures storage for at least the requested number of elements.
Definition darray_ext_impl.h:266
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_ext_impl.h:613
void push_back(const T &arg)
Appends an element.
Definition darray_ext_impl.h:360
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_ext_impl.h:649
void resize(size_type count, ResizePolicy policy=ResizePolicy::Exact)
Changes the number of elements.
Definition darray_ext_impl.h:289
iterator erase(iterator pos) noexcept
Removes the element at pos.
Definition darray_ext_impl.h:469
GAIA_NODISCARD bool empty() const noexcept
Checks whether the container has no elements.
Definition darray_ext_impl.h:567
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:197
Strict weak ordering functor using operator<.
Definition utility.h:1385
Hashmap lookup structure used for Entity.
Definition id.h:486
Identifier of an entity or component instance in the world. Packs the entity index,...
Definition id.h:231
GAIA_NODISCARD constexpr bool pair() const noexcept
Whether this id refers to a relationship pair.
Definition id.h:313
GAIA_NODISCARD constexpr auto id() const noexcept
Entity index in the entity array.
Definition id.h:295
Storage for exclusive relation pairs that do not fragment archetypes.
Definition nonfragmenting_relation_store.h:15
void collect_source_ids(cnt::darray< EntityId > &out) const
Appends ids of all bound source entities.
Definition nonfragmenting_relation_store.h:130
GAIA_NODISCARD bool remove(Entity source, Entity target)
Removes source from the store.
Definition nonfragmenting_relation_store.h:210
GAIA_NODISCARD const cnt::darray< Entity > * sources(Entity target) const
Returns sources currently bound to target.
Definition nonfragmenting_relation_store.h:114
GAIA_NODISCARD Entity target(Entity source) const
Returns the target currently bound to source.
Definition nonfragmenting_relation_store.h:99
GAIA_NODISCARD bool set(Entity source, Entity target)
Binds source to target.
Definition nonfragmenting_relation_store.h:179
void ensure_target_capacity(Entity target)
Ensures target-indexed storage can hold target.
Definition nonfragmenting_relation_store.h:83
void remove_target_source(Entity target, Entity source)
Removes source from the source bucket for target.
Definition nonfragmenting_relation_store.h:154
GAIA_NODISCARD uint32_t source_count() const
Returns the number of active source bindings.
Definition nonfragmenting_relation_store.h:124
GAIA_NODISCARD bool empty() const
Checks whether the store has no source bindings.
Definition nonfragmenting_relation_store.h:231
void collect_pair_sources(cnt::darray< Entity > &out) const
Appends all bound exact pair sources in deterministic entity order.
Definition nonfragmenting_relation_store.h:142
void ensure_source_capacity(Entity source)
Ensures source-indexed storage can hold source.
Definition nonfragmenting_relation_store.h:67