Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
archetype_graph.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdint>
5
6#include "gaia/cnt/map.h"
7#include "gaia/ecs/api.h"
8#include "gaia/ecs/archetype_common.h"
9#include "gaia/ecs/component.h"
10#include "gaia/ecs/id.h"
11#include "gaia/util/logging.h"
12
13namespace gaia {
14 namespace ecs {
15 class World;
16
17 using ArchetypeGraphEdge = ArchetypeIdHashPair;
18
21
23 EdgeMap m_edgesAdd;
25 EdgeMap m_edgesDel;
26
27 private:
28 void add_edge(EdgeMap& edges, Entity entity, ArchetypeId archetypeId, ArchetypeIdHash hash) {
29#if GAIA_ASSERT_ENABLED
30 const auto ret =
31#endif
32 edges.try_emplace(EntityLookupKey(entity), ArchetypeGraphEdge{archetypeId, hash});
33#if GAIA_ASSERT_ENABLED
34 // If the result already exists make sure the new one is the same
35 if (!ret.second) {
36 const auto it = edges.find(EntityLookupKey(entity));
37 GAIA_ASSERT(it != edges.end());
38 GAIA_ASSERT(it->second.id == archetypeId);
39 GAIA_ASSERT(it->second.hash == hash);
40 }
41#endif
42 }
43
44 void del_edge(EdgeMap& edges, Entity entity) {
45 edges.erase(EntityLookupKey(entity));
46 }
47
48 GAIA_NODISCARD ArchetypeGraphEdge find_edge(const EdgeMap& edges, Entity entity) const {
49 const auto it = edges.find(EntityLookupKey(entity));
50 return it != edges.end() ? it->second : ArchetypeIdHashPairBad;
51 }
52
53 public:
58 void add_edge_right(Entity entity, ArchetypeId archetypeId, ArchetypeIdHash hash) {
59 add_edge(m_edgesAdd, entity, archetypeId, hash);
60 }
61
66 void add_edge_left(Entity entity, ArchetypeId archetypeId, ArchetypeIdHash hash) {
67 add_edge(m_edgesDel, entity, archetypeId, hash);
68 }
69
72 void del_edge_right(Entity entity) {
73 del_edge(m_edgesAdd, entity);
74 }
75
78 void del_edge_left(Entity entity) {
79 del_edge(m_edgesDel, entity);
80 }
81
85 GAIA_NODISCARD ArchetypeGraphEdge find_edge_right(Entity entity) const {
86 return find_edge(m_edgesAdd, entity);
87 }
88
92 GAIA_NODISCARD ArchetypeGraphEdge find_edge_left(Entity entity) const {
93 return find_edge(m_edgesDel, entity);
94 }
95
96 GAIA_NODISCARD auto& right_edges() {
97 return m_edgesAdd;
98 }
99
100 GAIA_NODISCARD const auto& right_edges() const {
101 return m_edgesAdd;
102 }
103
104 GAIA_NODISCARD auto& left_edges() {
105 return m_edgesDel;
106 }
107
108 GAIA_NODISCARD const auto& left_edges() const {
109 return m_edgesDel;
110 }
111
112 void diag(const World& world) const {
113 auto diagEdge = [&](const auto& edges) {
114 for (const auto& edge: edges) {
115 const auto entity = edge.first.entity();
116 if (entity.pair()) {
117 const auto name0 = entity_name(world, entity.id());
118 const auto name1 = entity_name(world, entity.gen());
119 GAIA_LOG_N(
120 " pair [%u:%u], %.*s -> %.*s, aid:%u",
121 //
122 entity.id(), entity.gen(), (int)name0.size(), name0.empty() ? "" : name0.data(),
123 (int)name1.size(), name1.empty() ? "" : name1.data(), edge.second.id);
124 } else {
125 const auto name = entity_name(world, entity);
126 GAIA_LOG_N(
127 " ent [%u:%u], %.*s [%s], aid:%u",
128 //
129 entity.id(), entity.gen(), (int)name.size(), name.empty() ? "" : name.data(),
130 EntityKindString[entity.kind()], edge.second.id);
131 }
132 }
133 };
134
135 // Add edges (movement towards the leafs)
136 if (!m_edgesAdd.empty()) {
137 GAIA_LOG_N(" Add edges - count:%u", (uint32_t)m_edgesAdd.size());
138 diagEdge(m_edgesAdd);
139 }
140
141 // Delete edges (movement towards the root)
142 if (!m_edgesDel.empty()) {
143 GAIA_LOG_N(" Del edges - count:%u", (uint32_t)m_edgesDel.size());
144 diagEdge(m_edgesDel);
145 }
146 }
147 };
148 } // namespace ecs
149} // namespace gaia
Definition archetype_graph.h:19
void add_edge_right(Entity entity, ArchetypeId archetypeId, ArchetypeIdHash hash)
Creates an "add" edge in the graph leading to the target archetype.
Definition archetype_graph.h:58
GAIA_NODISCARD ArchetypeGraphEdge find_edge_left(Entity entity) const
Checks if an archetype graph "del" edge with entity entity exists.
Definition archetype_graph.h:92
void del_edge_left(Entity entity)
Deletes the "del" edge formed by the entity entity.
Definition archetype_graph.h:78
GAIA_NODISCARD ArchetypeGraphEdge find_edge_right(Entity entity) const
Checks if an archetype graph "add" edge with entity entity exists.
Definition archetype_graph.h:85
void del_edge_right(Entity entity)
Deletes the "add" edge formed by the entity entity.
Definition archetype_graph.h:72
void add_edge_left(Entity entity, ArchetypeId archetypeId, ArchetypeIdHash hash)
Creates a "del" edge in the graph leading to the target archetype.
Definition archetype_graph.h:66
Definition robin_hood.h:720
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition archetype_common.h:19
Hashmap lookup structure used for Entity.
Definition id.h:439
Definition id.h:241