Plasma Engine  2.0
Loading...
Searching...
No Matches
HashedString.h
1#pragma once
2
3#include <Foundation/Algorithm/HashingUtils.h>
4#include <Foundation/Containers/Map.h>
5#include <Foundation/Strings/String.h>
6#include <Foundation/Threading/AtomicInteger.h>
7
9
24class PL_FOUNDATION_DLL plHashedString
25{
26public:
28 {
29#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
30 plAtomicInteger32 m_iRefCount;
31#endif
32 plString m_sString;
33 };
34
35 // Do NOT use a hash-table! The map does not relocate memory when it resizes, which is a vital aspect for the hashed strings to work.
38
39#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
49 static plUInt32 ClearUnusedStrings();
50#endif
51
52 PL_DECLARE_MEM_RELOCATABLE_TYPE();
53
55 plHashedString(); // [tested]
56
58 plHashedString(const plHashedString& rhs); // [tested]
59
61 plHashedString(plHashedString&& rhs); // [tested]
62
63#if PL_ENABLED(PL_HASHED_STRING_REF_COUNTING)
66#endif
67
69 void operator=(const plHashedString& rhs); // [tested]
70
72 void operator=(plHashedString&& rhs); // [tested]
73
78 template <size_t N>
79 void Assign(const char (&string)[N]); // [tested]
80
81 template <size_t N>
82 void Assign(char (&string)[N]) = delete;
83
88 void Assign(plStringView sString); // [tested]
89
95 bool operator==(const plHashedString& rhs) const; // [tested]
96 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plHashedString&);
97
100 bool operator==(const plTempHashedString& rhs) const; // [tested]
101 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plTempHashedString&);
102
104 bool operator<(const plHashedString& rhs) const; // [tested]
105
107 bool operator<(const plTempHashedString& rhs) const; // [tested]
108
110 const plString& GetString() const; // [tested]
111
113 const char* GetData() const;
114
116 plUInt64 GetHash() const; // [tested]
117
119 bool IsEmpty() const;
120
122 void Clear();
123
125 PL_ALWAYS_INLINE operator plStringView() const { return GetString().GetView(); }
126
128 PL_ALWAYS_INLINE plStringView GetView() const { return GetString().GetView(); }
129
131 PL_ALWAYS_INLINE operator const char*() const { return GetData(); }
132
133private:
134 static void InitHashedString();
135 static HashedType AddHashedString(plStringView sString, plUInt64 uiHash);
136
137 HashedType m_Data;
138};
139
141template <size_t N>
142plHashedString plMakeHashedString(const char (&string)[N]);
143
144
150class PL_FOUNDATION_DLL plTempHashedString
151{
152 friend class plHashedString;
153
154public:
155 plTempHashedString(); // [tested]
156
158 template <size_t N>
159 plTempHashedString(const char (&string)[N]); // [tested]
160
161 template <size_t N>
162 plTempHashedString(char (&string)[N]) = delete;
163
166 explicit plTempHashedString(plStringView sString); // [tested]
167
169 plTempHashedString(const plTempHashedString& rhs); // [tested]
170
172 plTempHashedString(const plHashedString& rhs); // [tested]
173
174 explicit plTempHashedString(plUInt32 uiHash) = delete;
175
177 explicit plTempHashedString(plUInt64 uiHash);
178
180 template <size_t N>
181 void operator=(const char (&string)[N]); // [tested]
182
184 void operator=(plStringView sString); // [tested]
185
187 void operator=(const plTempHashedString& rhs); // [tested]
188
190 void operator=(const plHashedString& rhs); // [tested]
191
193 bool operator==(const plTempHashedString& rhs) const; // [tested]
194 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plTempHashedString&);
195
197 bool operator<(const plTempHashedString& rhs) const; // [tested]
198
200 bool IsEmpty() const; // [tested]
201
203 void Clear(); // [tested]
204
206 plUInt64 GetHash() const; // [tested]
207
208private:
209 plUInt64 m_uiHash;
210};
211
212// For plFormatString
213PL_FOUNDATION_DLL plStringView BuildString(char* szTmp, plUInt32 uiLength, const plHashedString& sArg);
214
215#include <Foundation/Strings/Implementation/HashedString_inl.h>
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...
PL_ALWAYS_INLINE plStringView GetView() const
Returns a string view to this string's data.
Definition HashedString.h:128
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
plTempHashedString(const char(&string)[N])
Creates an plTempHashedString object from the given string constant. The hash can be computed at comp...
void operator=(const char(&string)[N])
The hash of the given string can be computed at compile time.
Definition HashedString.h:28
Forward Iterator to iterate over all elements in sorted order.
Definition Map.h:103
Definition AllocatorWrapper.h:20