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