Plasma Engine  2.0
Loading...
Searching...
No Matches
HashedString_inl.h
1#pragma once
2
3#include <Foundation/Algorithm/HashingUtils.h>
4
6{
7 m_Data = rhs.m_Data;
8
9#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
10 // the string has a refcount of at least one (rhs holds a reference), thus it will definitely not get deleted on some other thread
11 // therefore we can simply increase the refcount without locking
12 m_Data.Value().m_iRefCount.Increment();
13#endif
14}
15
17{
18 m_Data = rhs.m_Data;
19 rhs.m_Data = HashedType(); // This leaves the string in an invalid state, all operations will fail except the destructor
20}
21
22#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
23inline plHashedString::~plHashedString()
24{
25 // Explicit check if data is still valid. It can be invalid if this string has been moved.
26 if (m_Data.IsValid())
27 {
28 // just decrease the refcount of the object that we are set to, it might reach refcount zero, but we don't care about that here
29 m_Data.Value().m_iRefCount.Decrement();
30 }
31}
32#endif
33
35{
36 // first increase the other refcount, then decrease ours
37 HashedType tmp = rhs.m_Data;
38
39#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
40 tmp.Value().m_iRefCount.Increment();
41
42 m_Data.Value().m_iRefCount.Decrement();
43#endif
44
45 m_Data = tmp;
46}
47
48PL_FORCE_INLINE void plHashedString::operator=(plHashedString&& rhs)
49{
50#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
51 m_Data.Value().m_iRefCount.Decrement();
52#endif
53
54 m_Data = rhs.m_Data;
55 rhs.m_Data = HashedType();
56}
57
58template <size_t N>
59PL_FORCE_INLINE void plHashedString::Assign(const char (&string)[N])
60{
61#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
62 HashedType tmp = m_Data;
63#endif
64 // this function will already increase the refcount as needed
65 m_Data = AddHashedString(string, plHashingUtils::StringHash(string));
66
67#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
68 tmp.Value().m_iRefCount.Decrement();
69#endif
70}
71
72PL_FORCE_INLINE void plHashedString::Assign(plStringView sString)
73{
74#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
75 HashedType tmp = m_Data;
76#endif
77 // this function will already increase the refcount as needed
78 m_Data = AddHashedString(sString, plHashingUtils::StringHash(sString));
79
80#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
81 tmp.Value().m_iRefCount.Decrement();
82#endif
83}
84
85inline bool plHashedString::operator==(const plHashedString& rhs) const
86{
87 return m_Data == rhs.m_Data;
88}
89
91{
92 return m_Data.Key() == rhs.m_uiHash;
93}
94
95inline bool plHashedString::operator<(const plHashedString& rhs) const
96{
97 return m_Data.Key() < rhs.m_Data.Key();
98}
99
101{
102 return m_Data.Key() < rhs.m_uiHash;
103}
104
105PL_ALWAYS_INLINE const plString& plHashedString::GetString() const
106{
107 return m_Data.Value().m_sString;
108}
109
110PL_ALWAYS_INLINE const char* plHashedString::GetData() const
111{
112 return m_Data.Value().m_sString.GetData();
113}
114
115PL_ALWAYS_INLINE plUInt64 plHashedString::GetHash() const
116{
117 return m_Data.Key();
118}
119
120template <size_t N>
121PL_FORCE_INLINE plHashedString plMakeHashedString(const char (&string)[N])
122{
123 plHashedString sResult;
124 sResult.Assign(string);
125 return sResult;
126}
127
129
130PL_ALWAYS_INLINE plTempHashedString::plTempHashedString()
131{
132 constexpr plUInt64 uiEmptyHash = plHashingUtils::StringHash("");
133 m_uiHash = uiEmptyHash;
134}
135
136template <size_t N>
137PL_ALWAYS_INLINE plTempHashedString::plTempHashedString(const char (&string)[N])
138{
139 m_uiHash = plHashingUtils::StringHash<N>(string);
140}
141
142PL_ALWAYS_INLINE plTempHashedString::plTempHashedString(plStringView sString)
143{
144 m_uiHash = plHashingUtils::StringHash(sString);
145}
146
147PL_ALWAYS_INLINE plTempHashedString::plTempHashedString(const plTempHashedString& rhs)
148{
149 m_uiHash = rhs.m_uiHash;
150}
151
152PL_ALWAYS_INLINE plTempHashedString::plTempHashedString(const plHashedString& rhs)
153{
154 m_uiHash = rhs.GetHash();
155}
156
157PL_ALWAYS_INLINE plTempHashedString::plTempHashedString(plUInt64 uiHash)
158{
159 m_uiHash = uiHash;
160}
161
162template <size_t N>
163PL_ALWAYS_INLINE void plTempHashedString::operator=(const char (&string)[N])
164{
165 m_uiHash = plHashingUtils::StringHash<N>(string);
166}
167
168PL_ALWAYS_INLINE void plTempHashedString::operator=(plStringView sString)
169{
170 m_uiHash = plHashingUtils::StringHash(sString);
171}
172
173PL_ALWAYS_INLINE void plTempHashedString::operator=(const plTempHashedString& rhs)
174{
175 m_uiHash = rhs.m_uiHash;
176}
177
178PL_ALWAYS_INLINE void plTempHashedString::operator=(const plHashedString& rhs)
179{
180 m_uiHash = rhs.GetHash();
181}
182
183PL_ALWAYS_INLINE bool plTempHashedString::operator==(const plTempHashedString& rhs) const
184{
185 return m_uiHash == rhs.m_uiHash;
186}
187
188PL_ALWAYS_INLINE bool plTempHashedString::operator<(const plTempHashedString& rhs) const
189{
190 return m_uiHash < rhs.m_uiHash;
191}
192
193PL_ALWAYS_INLINE bool plTempHashedString::IsEmpty() const
194{
195 constexpr plUInt64 uiEmptyHash = plHashingUtils::StringHash("");
196 return m_uiHash == uiEmptyHash;
197}
198
199PL_ALWAYS_INLINE void plTempHashedString::Clear()
200{
201 *this = plTempHashedString();
202}
203
204PL_ALWAYS_INLINE plUInt64 plTempHashedString::GetHash() const
205{
206 return m_uiHash;
207}
208
210
211template <>
213{
214 PL_ALWAYS_INLINE static plUInt32 Hash(const plHashedString& value)
215 {
217 }
218
219 PL_ALWAYS_INLINE static plUInt32 Hash(const plTempHashedString& value)
220 {
222 }
223
224 PL_ALWAYS_INLINE static bool Equal(const plHashedString& a, const plHashedString& b) { return a == b; }
225
226 PL_ALWAYS_INLINE static bool Equal(const plHashedString& a, const plTempHashedString& b) { return a == b; }
227};
228
229template <>
231{
232 PL_ALWAYS_INLINE static plUInt32 Hash(const plTempHashedString& value)
233 {
235 }
236
237 PL_ALWAYS_INLINE static bool Equal(const plTempHashedString& a, const plTempHashedString& b) { return a == b; }
238};
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
void Assign(const char(&string)[N])
Assigning a new string from a string constant is a slow operation, but the hash computation can happe...
const plString & GetString() const
Gives access to the actual string data, so you can do all the typical (read-only) string operations o...
Definition HashedString_inl.h:105
bool operator==(const plHashedString &rhs) const
Comparing whether two hashed strings are identical is just a pointer comparison. This operation is wh...
Definition HashedString_inl.h:85
plHashedString()
Initializes this string to the empty string.
Definition HashedString.cpp:104
const char * GetData() const
Gives access to the actual string data, so you can do all the typical (read-only) string operations o...
Definition HashedString_inl.h:110
bool operator<(const plHashedString &rhs) const
This operator allows sorting objects by hash value, not by alphabetical order.
Definition HashedString_inl.h:95
void operator=(const plHashedString &rhs)
Copies the given plHashedString.
Definition HashedString_inl.h:34
plUInt64 GetHash() const
Returns the hash of the stored string.
Definition HashedString_inl.h:115
static constexpr plUInt32 StringHashTo32(plUInt64 uiHash)
Truncates a 64 bit string hash to 32 bit.
Definition HashingUtils_inl.h:138
static constexpr plUInt64 StringHash(const char(&str)[N], plUInt64 uiSeed=0)
Calculates the hash of the given string literal at compile time.
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
bool IsEmpty() const
Checks whether the plTempHashedString represents the empty string.
Definition HashedString_inl.h:193
bool operator==(const plTempHashedString &rhs) const
Compares the two objects by their hash value. Might report incorrect equality, if two strings have th...
Definition HashedString_inl.h:183
void Clear()
Resets the string to the empty string.
Definition HashedString_inl.h:199
bool operator<(const plTempHashedString &rhs) const
This operator allows soring objects by hash value, not by alphabetical order.
Definition HashedString_inl.h:188
plUInt64 GetHash() const
Returns the hash of the stored string.
Definition HashedString_inl.h:204
void operator=(const char(&string)[N])
The hash of the given string can be computed at compile time.
Helper struct to calculate the Hash of different types.
Definition HashingUtils.h:75
PL_ALWAYS_INLINE bool IsValid() const
Checks whether this iterator points to a valid element.
Definition Map.h:27
PL_FORCE_INLINE const KeyType & Key() const
Returns the 'key' of the element that this iterator points to.
Definition Map.h:34
Forward Iterator to iterate over all elements in sorted order.
Definition Map.h:103
PL_FORCE_INLINE ValueType & Value()
Returns the 'value' of the element that this iterator points to.
Definition Map.h:119