2#include "gaia/config/config.h"
4#include "gaia/cnt/darray.h"
5#include "gaia/cnt/map.h"
6#include "gaia/core/utility.h"
7#include "gaia/ecs/component.h"
8#include "gaia/ecs/id.h"
9#include "gaia/ecs/query_common.h"
10#include "gaia/ecs/query_info.h"
19 static constexpr bool IsDirectHashKey =
true;
25 return (
size_t)m_hash.hash;
31 if GAIA_LIKELY (m_hash != other.m_hash)
34 const auto id = m_pCtx->
q.
handle.id();
38 return *m_pCtx == *other.m_pCtx;
43 return m_pCtx == other.m_pCtx;
61 m_queryArr.reserve(256);
71 GAIA_NODISCARD
bool valid(
QueryHandle handle)
const {
72 if (handle.id() == QueryIdBad)
76 if (handle.id() >= m_queryArr.size())
79 const auto& h = m_queryArr[handle.id()];
80 return h.idx == handle.id() && h.data.gen == handle.gen();
86 m_entityToQuery.clear();
96 auto& info = m_queryArr[handle.id()];
97 GAIA_ASSERT(info.idx == handle.id());
98 GAIA_ASSERT(info.data.gen == handle.gen());
106 GAIA_ASSERT(valid(handle));
108 auto& info = m_queryArr[handle.id()];
109 GAIA_ASSERT(info.idx == handle.id());
110 GAIA_ASSERT(info.data.gen == handle.gen());
123 GAIA_ASSERT(ctx.hashLookup.hash != 0);
126 auto ret = m_queryCache.try_emplace(
QueryLookupKey(ctx.hashLookup, &ctx));
128 const auto idx = ret.first->second;
129 auto& info = m_queryArr[idx];
130 GAIA_ASSERT(idx == info.idx);
137 creationCtx.pQueryCtx = &ctx;
138 creationCtx.pEntityToArchetypeMap = &entityToArchetypeMap;
139 creationCtx.pAllArchetypes = &allArchetypes;
140 auto handle = m_queryArr.
alloc(&creationCtx);
144 auto& info =
get(handle);
147 ret.first->swap(new_p);
150 add_entity_to_query_pairs(info.ctx().data.ids_view(), handle);
160 if (pInfo ==
nullptr)
164 if (pInfo->refs() != 0)
168 auto it = m_queryCache.find(
QueryLookupKey(pInfo->ctx().hashLookup, &pInfo->ctx()));
169 GAIA_ASSERT(it != m_queryCache.end());
170 m_queryCache.erase(it);
171 m_queryArr.
free(handle);
174 del_entity_to_query_pairs(pInfo->ctx().data.ids_view(), handle);
179 cnt::darray<QueryInfo>::iterator begin() {
180 return m_queryArr.begin();
183 cnt::darray<QueryInfo>::iterator end() {
184 return m_queryArr.end();
194 auto it = m_entityToQuery.find(entityKey);
195 if (it == m_entityToQuery.end())
198 const auto& handles = it->second;
199 for (
auto& handle: handles) {
200 auto& info =
get(handle);
202 info.ctx().refresh();
212 const auto it = m_entityToQuery.find(entityKey);
213 if (it == m_entityToQuery.end()) {
218 auto& handles = it->second;
219 if (!core::has(handles, handle))
220 handles.push_back(handle);
226 void del_entity_archetype_pair(Entity entity, QueryHandle handle) {
227 auto it = m_entityToQuery.find(EntityLookupKey(entity));
228 if (it == m_entityToQuery.end())
231 auto& handles = it->second;
232 const auto idx = core::get_index_unsafe(handles, handle);
233 core::swap_erase_unsafe(handles, idx);
237 m_entityToQuery.erase(it);
242 void add_entity_to_query_pairs(EntitySpan entities, QueryHandle handle) {
243 for (
auto entity: entities) {
244 add_entity_query_pair(entity, handle);
250 void del_entity_to_query_pairs(EntitySpan entities, QueryHandle handle) {
251 for (
auto entity: entities) {
252 add_entity_query_pair(entity, handle);
Array with variable size of elements of type.
Definition darray_impl.h:25
Definition query_cache.h:47
QueryInfo & add(QueryCtx &&ctx, const EntityToArchetypeMap &entityToArchetypeMap, const ArchetypeDArray &allArchetypes)
Registers the provided query lookup context ctx. If it already exists it is returned.
Definition query_cache.h:120
QueryInfo & get(QueryHandle handle)
Returns a QueryInfo object associated with handle.
Definition query_cache.h:105
bool del(QueryHandle handle)
Deletes an existing QueryInfo object given the provided query handle.
Definition query_cache.h:158
QueryInfo * try_get(QueryHandle handle)
Returns a QueryInfo object associated with handle.
Definition query_cache.h:92
void invalidate_queries_for_entity(EntityLookupKey entityKey)
Invalidates all cached queries that work with the given entity This covers the following kinds of que...
Definition query_cache.h:193
Definition query_info.h:37
Definition query_cache.h:14
Definition robin_hood.h:720
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Implicit list. Rather than with pointers, items.
Definition ilist.h:76
TListItem & free(TItemHandle handle)
Invalidates handle. Every time an item is deallocated its generation is increased by one.
Definition ilist.h:235
GAIA_NODISCARD TItemHandle alloc(void *ctx)
Allocates a new item in the list.
Definition ilist.h:175
Hashmap lookup structure used for Entity.
Definition id.h:336
Definition query_common.h:197
QueryIdentity q
Query identity.
Definition query_common.h:205
Definition query_common.h:50
QueryHandle handle
Query id.
Definition query_common.h:185
Definition query_info.h:31
Definition robin_hood.h:418