cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
THashMap.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
25#include <cppcore/Common/Hash.h>
27
28namespace cppcore {
29
30//-------------------------------------------------------------------------------------------------
46//-------------------------------------------------------------------------------------------------
47template <class T, class U, class TAlloc = TDefaultAllocator<T>>
48class THashMap {
49public:
51 using Hash = THash<T>;
52
54 static constexpr size_t InitSize = 1024;
56 static constexpr unsigned int UnsetNode = 999999999;
57
60 explicit THashMap(size_t init = InitSize);
61
63 ~THashMap();
64
67 size_t size() const;
68
71 size_t capacity() const;
72
75 bool isEmpty() const;
76
79 void init(size_t init);
80
82 void clear();
83
87 void insert(const T &key, const U &value);
88
92 bool remove(const T &key);
93
97 bool hasKey(const T &key) const;
98
103 bool getValue(const T &key, U &value) const;
104
108 U &operator[](const T &key) const;
109
111 THashMap<T, U, TAlloc>(const THashMap<T, U> &) = delete;
113
114private:
115 struct Node {
116 T mKey = UnsetNode;
117 U mValue;
118 Node *mNext;
119
120 Node();
121 ~Node() = default;
122 void append(T key, const U &value);
123 bool remove(T key);
124 void releaseList();
125 };
126
127 Node **mBuffer{nullptr};
128 size_t mNumItems{0u};
129 size_t mBuffersize{0u};
130};
131
132template <class T, class U, class TAlloc>
133inline THashMap<T, U, TAlloc>::THashMap(size_t initSize) {
134 init(initSize);
135}
136
137template <class T, class U, class TAlloc>
141
142template <class T, class U, class TAlloc>
143inline size_t THashMap<T, U, TAlloc>::size() const {
144 return mNumItems;
145}
146
147template <class T, class U, class TAlloc>
148inline size_t THashMap<T, U, TAlloc>::capacity() const {
149 return mBuffersize;
150}
151
152template <class T, class U, class TAlloc>
154 return (0u == mNumItems);
155}
156
157template <class T, class U, class TAlloc>
159 mBuffer = new Node *[init];
160 for (size_t i = 0; i < init; i++) {
161 mBuffer[i] = nullptr;
162 }
163 mBuffersize = init;
164}
165
166template <class T, class U, class TAlloc>
168 for (size_t i = 0; i < mBuffersize; ++i) {
169 if (nullptr != mBuffer[i]) {
170 mBuffer[i]->releaseList();
171 }
172 }
173 delete[] mBuffer;
174 mBuffer = nullptr;
175 mNumItems = 0;
176 mBuffersize = 0;
177}
178
179template <class T, class U, class TAlloc>
180inline void THashMap<T, U, TAlloc>::insert(const T &key, const U &value) {
181 const T hash = Hash::toHash(key, static_cast<T>(mBuffersize));
182 if (nullptr == mBuffer[hash]) {
183 Node *node = new Node;
184 node->mKey = key;
185 node->mValue = value;
186 mBuffer[hash] = node;
187 } else {
188 mBuffer[hash]->append(key, value);
189 }
190 ++mNumItems;
191}
192
193template <class T, class U, class TAlloc>
194inline bool THashMap<T, U, TAlloc>::remove(const T &key) {
195 const T hash = Hash::toHash(key, static_cast<unsigned int>(mBuffersize));
196 if (nullptr == mBuffer[hash]) {
197 return false;
198 }
199
200 bool found = false;
201 Node *current = mBuffer[hash];
202 if (current->mKey == key) {
203 Node *next = current->mNext;
204 mBuffer[hash] = next;
205 delete current;
206 --mNumItems;
207 found = true;
208 return true;
209 }
210
211 found = current->remove(key);
212 if (found) {
213 --mNumItems;
214 }
215
216 return found;
217}
218
219template <class T, class U, class TAlloc>
220inline bool THashMap<T, U, TAlloc>::hasKey(const T &key) const {
221 // no buffer, so no items stored
222 if (0 == mBuffersize) {
223 return false;
224 }
225 const T hash = THash<T>::toHash(key, (unsigned int)mBuffersize);
226 const Node *node = mBuffer[hash];
227 if (nullptr == node) {
228 return false;
229 }
230
231 if (node->mKey == key) {
232 return true;
233 }
234
235 if (nullptr != node->mNext) {
236 Node *next = node->mNext;
237 while (nullptr != next) {
238 if (next->mKey == key) {
239 return true;
240 }
241 next = next->mNext;
242 }
243 }
244
245 return false;
246}
247
248template <class T, class U, class TAlloc>
249inline bool THashMap<T, U, TAlloc>::getValue(const T &key, U &value) const {
250 const size_t pos = Hash::toHash(key, (unsigned int) mBuffersize);
251 if (mBuffer[pos]->mKey == key) {
252 value = mBuffer[pos]->mValue;
253 return true;
254 }
255
256 Node *node = mBuffer[pos];
257 Node *next = node->mNext;
258 while (next->mKey != key) {
259 next = next->mNext;
260 if (nullptr == next) {
261 return false;
262 }
263 }
264 value = next->mValue;
265
266 return true;
267}
268
269template <class T, class U, class TAlloc>
270inline U &THashMap<T, U, TAlloc>::operator[](const T &key) const {
271 static U dummy;
272 const unsigned int pos = Hash::toHash(key, mBuffersize);
273 if (mBuffer[pos]->mKey == key) {
274 return mBuffer[pos]->mValue;
275 }
276
277 Node *next = mBuffer[pos]->mNext;
278 while (next->mKey != key) {
279 next = next->mNext;
280 if (nullptr == next) {
281 return dummy;
282 }
283 }
284
285 return next->mValue;
286}
287
288template <class T, class U, class TAlloc>
289inline THashMap<T, U, TAlloc>::Node::Node() :
290 mValue(),
291 mNext(nullptr) {
292 // empty
293}
294
295template <class T, class U, class TAlloc>
296inline void THashMap<T, U, TAlloc>::Node::append(T key, const U &value) {
297 if (mKey == UnsetNode) {
298 mKey = key;
299 mValue = value;
300 return;
301 }
302
303 Node *current = mNext;
304 if (nullptr != current) {
305 // already collisions, traverse
306 while (current->mNext) {
307 current = current->mNext;
308 }
309 current->mNext = new Node;
310 current->mNext->mKey = key;
311 current->mNext->mValue = value;
312 } else {
313 // first collision
314 mNext = new Node;
315 current = mNext;
316 current->mKey = key;
317 current->mValue = value;
318 }
319}
320
321template <class T, class U, class TAlloc>
322inline bool THashMap<T, U, TAlloc>::Node::remove(T key) {
323 if (nullptr == mNext) {
324 return false;
325 }
326
327 bool found = false;
328 Node *current = mNext, *prev = nullptr;
329 while (nullptr != current) {
330 if (current->mKey == key) {
331 if (nullptr != prev) {
332 prev->mNext = current->mNext;
333 delete current;
334 }
335 found = true;
336 break;
337 }
338 prev = current;
339 current = current->mNext;
340 }
341
342 return found;
343}
344
345template <class T, class U, class TAlloc>
347 if (nullptr == mNext) {
348 return;
349 }
350
351 Node *current = mNext;
352 while (nullptr != current) {
353 Node *tmp = current;
354 current = current->mNext;
355 delete tmp;
356 }
357}
358
359} // Namespace cppcore
This class is used to calculate the hash value for a given integer or character buffer.
Definition Hash.h:36
static T toHash(const char *buffer, T base)
Computes the hash value for a given character buffer.
Definition Hash.h:94
static constexpr size_t InitSize
The initial hash size.
Definition THashMap.h:54
bool hasKey(const T &key) const
Looks for a given key and returns true, if a key-value pair is stored in the list.
Definition THashMap.h:220
bool isEmpty() const
Will return true, if hash-map is empty.
Definition THashMap.h:153
void insert(const T &key, const U &value)
A new key value pair will be entered.
Definition THashMap.h:180
U & operator[](const T &key) const
Returns the assigned value for the given key.
Definition THashMap.h:270
bool remove(const T &key)
Will remove a given key-value pair form the hash-map.
Definition THashMap.h:194
THashMap(size_t init=InitSize)
The class constructor.
Definition THashMap.h:133
bool getValue(const T &key, U &value) const
Returns the assigned value for the given key.
Definition THashMap.h:249
size_t size() const
Returns the number of stored items in the hash-map.
Definition THashMap.h:143
size_t capacity() const
Will return the number of possible hash items stored in the hash map.
Definition THashMap.h:148
THashMap< T, U, TAlloc > & operator=(const THashMap< T, U > &)=delete
void clear()
The hash-map will be cleared.
Definition THashMap.h:167
THash< T > Hash
The alias.
Definition THashMap.h:51
static constexpr unsigned int UnsetNode
Marker for unset node keys.
Definition THashMap.h:56
~THashMap()
The class destructor.
Definition THashMap.h:138
void init(size_t init)
Will init the hash-map with the given size.
Definition THashMap.h:158