Gaia-ECS v1.0.0
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 {
13 template <uint32_t MaxLen>
17
18 private:
20 const char* m_pStr;
22 uint32_t m_len : 31;
24 uint32_t m_owned : 1;
26 LookupHash m_hash;
27
28 static uint32_t len(const char* pStr) {
29 GAIA_FOR(MaxLen) {
30 if (pStr[i] == 0)
31 return i;
32 }
33 GAIA_ASSERT2(false, "Only null-terminated strings up to MaxLen characters are supported");
34 return BadIndex;
35 }
36
37 static LookupHash calc(const char* pStr, uint32_t len) {
38 return {static_cast<uint32_t>(core::calculate_hash64(pStr, len))};
39 }
40
41 public:
43 static constexpr bool IsDirectHashKey = true;
44
46 StringLookupKey(): m_pStr(nullptr), m_len(0), m_owned(0), m_hash({0}) {}
47
54 explicit StringLookupKey(const char* pStr, uint32_t len, uint32_t owned):
55 m_pStr(pStr), m_len(len), m_owned(owned), m_hash(calc(pStr, len)) {}
56
62 explicit StringLookupKey(const char* pStr, uint32_t len, uint32_t owned, LookupHash hash):
63 m_pStr(pStr), m_len(len), m_owned(owned), m_hash(hash) {}
64
67 const char* str() const {
68 return m_pStr;
69 }
70
73 uint32_t len() const {
74 return m_len;
75 }
76
79 bool owned() const {
80 return m_owned == 1;
81 }
82
85 uint32_t hash() const {
86 return m_hash.hash;
87 }
88
92 bool operator==(const StringLookupKey& other) const {
93 // Hash doesn't match we don't have a match.
94 // Hash collisions are expected to be very unlikely so optimize for this case.
95 if GAIA_LIKELY (m_hash != other.m_hash)
96 return false;
97
98 // Lengths have to match
99 if (m_len != other.m_len)
100 return false;
101
102 // Contents have to match
103 const auto l = m_len;
104 GAIA_ASSUME(l < MaxLen);
105 GAIA_FOR(l) {
106 if (m_pStr[i] != other.m_pStr[i])
107 return false;
108 }
109
110 return true;
111 }
112
116 bool operator!=(const StringLookupKey& other) const {
117 return !operator==(other);
118 }
119 };
120 } // namespace core
121} // namespace gaia
Fixed-limit string lookup key carrying a precomputed 32-bit hash.
Definition hashing_string.h:14
bool operator==(const StringLookupKey &other) const
Compares keys by hash, length, and string contents.
Definition hashing_string.h:92
bool owned() const
Reports whether Gaia-ECS manages the string lifetime.
Definition hashing_string.h:79
bool operator!=(const StringLookupKey &other) const
Compares keys for inequality.
Definition hashing_string.h:116
uint32_t hash() const
Returns the precomputed string hash.
Definition hashing_string.h:85
static constexpr bool IsDirectHashKey
Marker indicating that this key provides its hash directly.
Definition hashing_string.h:43
StringLookupKey(const char *pStr, uint32_t len, uint32_t owned)
Constructor calculating hash from the provided string pStr and len.
Definition hashing_string.h:54
uint32_t len() const
Returns the string length.
Definition hashing_string.h:73
core::direct_hash_key< uint32_t > LookupHash
Direct hash wrapper used by lookup containers.
Definition hashing_string.h:16
const char * str() const
Returns the referenced string.
Definition hashing_string.h:67
StringLookupKey(const char *pStr, uint32_t len, uint32_t owned, LookupHash hash)
Constructor just for setting values.
Definition hashing_string.h:62
StringLookupKey()
Constructs an empty lookup key.
Definition hashing_string.h:46
T hash
Precomputed hash value.
Definition hashing_policy.h:51