Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
archetype_common.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdint>
5
6#include "gaia/cnt/darray.h"
7#include "gaia/cnt/map.h"
8#include "gaia/core/hashing_policy.h"
9
11namespace gaia {
12 namespace ecs {
13 class Archetype;
14
15 using ArchetypeId = uint32_t;
16 using ArchetypeDArray = cnt::darray<Archetype*>;
17 using CArchetypeDArray = cnt::darray<const Archetype*>;
18 using ArchetypeIdHash = core::direct_hash_key<uint32_t>;
19
20 struct ArchetypeIdHashPair {
21 ArchetypeId id;
22 ArchetypeIdHash hash;
23
24 GAIA_NODISCARD bool operator==(ArchetypeIdHashPair other) const {
25 return id == other.id;
26 }
27 GAIA_NODISCARD bool operator!=(ArchetypeIdHashPair other) const {
28 return id != other.id;
29 }
30 };
31
32 static constexpr ArchetypeId ArchetypeIdBad = (ArchetypeId)-1;
33 static constexpr ArchetypeIdHashPair ArchetypeIdHashPairBad = {ArchetypeIdBad, {0}};
34
35 class ArchetypeIdLookupKey final {
36 public:
37 using LookupHash = core::direct_hash_key<uint32_t>;
38
39 private:
40 ArchetypeId m_id;
41 ArchetypeIdHash m_hash;
42
43 public:
44 GAIA_NODISCARD static LookupHash calc(ArchetypeId id) {
45 return {static_cast<uint32_t>(core::calculate_hash64(id))};
46 }
47
48 static constexpr bool IsDirectHashKey = true;
49
50 ArchetypeIdLookupKey(): m_id(0), m_hash({0}) {}
51 ArchetypeIdLookupKey(ArchetypeIdHashPair pair): m_id(pair.id), m_hash(pair.hash) {}
52 explicit ArchetypeIdLookupKey(ArchetypeId id, LookupHash hash): m_id(id), m_hash(hash) {}
53
54 GAIA_NODISCARD size_t hash() const {
55 return (size_t)m_hash.hash;
56 }
57
58 GAIA_NODISCARD bool operator==(const ArchetypeIdLookupKey& other) const {
59 // Hash doesn't match we don't have a match.
60 // Hash collisions are expected to be very unlikely so optimize for this case.
61 if GAIA_LIKELY (m_hash != other.m_hash)
62 return false;
63
64 return m_id == other.m_id;
65 }
66 };
67
68 using ArchetypeMapById = cnt::map<ArchetypeIdLookupKey, Archetype*>;
69 } // namespace ecs
70} // namespace gaia
71