Gaia-ECS v0.9.3
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
10namespace gaia {
11 namespace ecs {
12 class Archetype;
13
14 using ArchetypeId = uint32_t;
15 using ArchetypeDArray = cnt::darray<Archetype*>;
16 using CArchetypeDArray = cnt::darray<const Archetype*>;
17 using ArchetypeIdHash = core::direct_hash_key<uint32_t>;
18
20 ArchetypeId id;
21 ArchetypeIdHash hash;
22
23 GAIA_NODISCARD bool operator==(ArchetypeIdHashPair other) const {
24 return id == other.id;
25 }
26 GAIA_NODISCARD bool operator!=(ArchetypeIdHashPair other) const {
27 return id != other.id;
28 }
29 };
30
31 static constexpr ArchetypeId ArchetypeIdBad = (ArchetypeId)-1;
32 static constexpr ArchetypeIdHashPair ArchetypeIdHashPairBad = {ArchetypeIdBad, {0}};
33
35 public:
37
38 private:
39 ArchetypeId m_id;
40 ArchetypeIdHash m_hash;
41
42 public:
43 GAIA_NODISCARD static LookupHash calc(ArchetypeId id) {
44 return {static_cast<uint32_t>(core::calculate_hash64(id))};
45 }
46
47 static constexpr bool IsDirectHashKey = true;
48
49 ArchetypeIdLookupKey(): m_id(0), m_hash({0}) {}
50 ArchetypeIdLookupKey(ArchetypeIdHashPair pair): m_id(pair.id), m_hash(pair.hash) {}
51 explicit ArchetypeIdLookupKey(ArchetypeId id, LookupHash hash): m_id(id), m_hash(hash) {}
52
53 GAIA_NODISCARD size_t hash() const {
54 return (size_t)m_hash.hash;
55 }
56
57 GAIA_NODISCARD bool operator==(const ArchetypeIdLookupKey& other) const {
58 // Hash doesn't match we don't have a match.
59 // Hash collisions are expected to be very unlikely so optimize for this case.
60 if GAIA_LIKELY (m_hash != other.m_hash)
61 return false;
62
63 return m_id == other.m_id;
64 }
65 };
66
68 } // namespace ecs
69} // namespace gaia
Definition archetype_common.h:34
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:218
Definition robin_hood.h:720
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition archetype_common.h:19