Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
hashing_string.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdint>
5
6#include "gaia/core/hashing_policy.h"
7#include "gaia/core/utility.h"
8
9namespace gaia {
10 namespace core {
11 template <uint32_t MaxLen>
14
15 private:
17 const char* m_pStr;
19 uint32_t m_len : 31;
21 uint32_t m_owned : 1;
23 LookupHash m_hash;
24
25 static uint32_t len(const char* pStr) {
26 GAIA_FOR(MaxLen) {
27 if (pStr[i] == 0)
28 return i;
29 }
30 GAIA_ASSERT2(false, "Only null-terminated strings up to MaxLen characters are supported");
31 return BadIndex;
32 }
33
34 static LookupHash calc(const char* pStr, uint32_t len) {
35 return {static_cast<uint32_t>(core::calculate_hash64(pStr, len))};
36 }
37
38 public:
39 static constexpr bool IsDirectHashKey = true;
40
41 StringLookupKey(): m_pStr(nullptr), m_len(0), m_owned(0), m_hash({0}) {}
42
49 explicit StringLookupKey(const char* pStr, uint32_t len, uint32_t owned):
50 m_pStr(pStr), m_len(len), m_owned(owned), m_hash(calc(pStr, len)) {}
51
57 explicit StringLookupKey(const char* pStr, uint32_t len, uint32_t owned, LookupHash hash):
58 m_pStr(pStr), m_len(len), m_owned(owned), m_hash(hash) {}
59
60 const char* str() const {
61 return m_pStr;
62 }
63
64 uint32_t len() const {
65 return m_len;
66 }
67
68 bool owned() const {
69 return m_owned == 1;
70 }
71
72 uint32_t hash() const {
73 return m_hash.hash;
74 }
75
76 bool operator==(const StringLookupKey& other) const {
77 // Hash doesn't match we don't have a match.
78 // Hash collisions are expected to be very unlikely so optimize for this case.
79 if GAIA_LIKELY (m_hash != other.m_hash)
80 return false;
81
82 // Lengths have to match
83 if (m_len != other.m_len)
84 return false;
85
86 // Contents have to match
87 const auto l = m_len;
88 GAIA_ASSUME(l < MaxLen);
89 GAIA_FOR(l) {
90 if (m_pStr[i] != other.m_pStr[i])
91 return false;
92 }
93
94 return true;
95 }
96
97 bool operator!=(const StringLookupKey& other) const {
98 return !operator==(other);
99 }
100 };
101 } // namespace core
102} // namespace gaia
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition hashing_string.h:12
StringLookupKey(const char *pStr, uint32_t len, uint32_t owned)
Constructor calculating hash from the provided string pStr and len.
Definition hashing_string.h:49
StringLookupKey(const char *pStr, uint32_t len, uint32_t owned, LookupHash hash)
Constructor just for setting values.
Definition hashing_string.h:57