1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
/*************************************************************************/
/* hash_map.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef HASH_MAP_H
#define HASH_MAP_H
#include "error_macros.h"
#include "hashfuncs.h"
#include "list.h"
#include "math_funcs.h"
#include "os/memory.h"
#include "ustring.h"
struct HashMapHasherDefault {
static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); }
static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); }
static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
//static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); }
};
template <typename T>
struct HashMapComparatorDefault {
static bool compare(const T &p_lhs, const T &p_rhs) {
return p_lhs == p_rhs;
}
bool compare(const float &p_lhs, const float &p_rhs) {
return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
}
bool compare(const double &p_lhs, const double &p_rhs) {
return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
}
};
/**
* @class HashMap
* @author Juan Linietsky <reduzio@gmail.com>
*
* Implementation of a standard Hashing HashMap, for quick lookups of Data associated with a Key.
* The implementation provides hashers for the default types, if you need a special kind of hasher, provide
* your own.
* @param TKey Key, search is based on it, needs to be hasheable. It is unique in this container.
* @param TData Data, data associated with the key
* @param Hasher Hasher object, needs to provide a valid static hash function for TKey
* @param Comparator comparator object, needs to be able to safely compare two TKey values. It needs to ensure that x == x for any items inserted in the map. Bear in mind that nan != nan when implementing an equality check.
* @param MIN_HASH_TABLE_POWER Miminum size of the hash table, as a power of two. You rarely need to change this parameter.
* @param RELATIONSHIP Relationship at which the hash table is resized. if amount of elements is RELATIONSHIP
* times bigger than the hash table, table is resized to solve this condition. if RELATIONSHIP is zero, table is always MIN_HASH_TABLE_POWER.
*
*/
template <class TKey, class TData, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<TKey>, uint8_t MIN_HASH_TABLE_POWER = 3, uint8_t RELATIONSHIP = 8>
class HashMap {
public:
struct Pair {
TKey key;
TData data;
Pair() {}
Pair(const TKey &p_key, const TData &p_data) {
key = p_key;
data = p_data;
}
};
private:
struct Entry {
uint32_t hash;
Entry *next;
Pair pair;
Entry() { next = 0; }
};
Entry **hash_table;
uint8_t hash_table_power;
uint32_t elements;
void make_hash_table() {
ERR_FAIL_COND(hash_table);
hash_table = memnew_arr(Entry *, (1 << MIN_HASH_TABLE_POWER));
hash_table_power = MIN_HASH_TABLE_POWER;
elements = 0;
for (int i = 0; i < (1 << MIN_HASH_TABLE_POWER); i++)
hash_table[i] = 0;
}
void erase_hash_table() {
ERR_FAIL_COND(elements);
memdelete_arr(hash_table);
hash_table = 0;
hash_table_power = 0;
elements = 0;
}
void check_hash_table() {
int new_hash_table_power = -1;
if ((int)elements > ((1 << hash_table_power) * RELATIONSHIP)) {
/* rehash up */
new_hash_table_power = hash_table_power + 1;
while ((int)elements > ((1 << new_hash_table_power) * RELATIONSHIP)) {
new_hash_table_power++;
}
} else if ((hash_table_power > (int)MIN_HASH_TABLE_POWER) && ((int)elements < ((1 << (hash_table_power - 1)) * RELATIONSHIP))) {
/* rehash down */
new_hash_table_power = hash_table_power - 1;
while ((int)elements < ((1 << (new_hash_table_power - 1)) * RELATIONSHIP)) {
new_hash_table_power--;
}
if (new_hash_table_power < (int)MIN_HASH_TABLE_POWER)
new_hash_table_power = MIN_HASH_TABLE_POWER;
}
if (new_hash_table_power == -1)
return;
Entry **new_hash_table = memnew_arr(Entry *, (1 << new_hash_table_power));
if (!new_hash_table) {
ERR_PRINT("Out of Memory");
return;
}
for (int i = 0; i < (1 << new_hash_table_power); i++) {
new_hash_table[i] = 0;
}
for (int i = 0; i < (1 << hash_table_power); i++) {
while (hash_table[i]) {
Entry *se = hash_table[i];
hash_table[i] = se->next;
int new_pos = se->hash & ((1 << new_hash_table_power) - 1);
se->next = new_hash_table[new_pos];
new_hash_table[new_pos] = se;
}
}
if (hash_table)
memdelete_arr(hash_table);
hash_table = new_hash_table;
hash_table_power = new_hash_table_power;
}
/* I want to have only one function.. */
_FORCE_INLINE_ const Entry *get_entry(const TKey &p_key) const {
uint32_t hash = Hasher::hash(p_key);
uint32_t index = hash & ((1 << hash_table_power) - 1);
Entry *e = hash_table[index];
while (e) {
/* checking hash first avoids comparing key, which may take longer */
if (e->hash == hash && Comparator::compare(e->pair.key, p_key)) {
/* the pair exists in this hashtable, so just update data */
return e;
}
e = e->next;
}
return NULL;
}
Entry *create_entry(const TKey &p_key) {
/* if entry doesn't exist, create it */
Entry *e = memnew(Entry);
ERR_FAIL_COND_V(!e, NULL); /* out of memory */
uint32_t hash = Hasher::hash(p_key);
uint32_t index = hash & ((1 << hash_table_power) - 1);
e->next = hash_table[index];
e->hash = hash;
e->pair.key = p_key;
hash_table[index] = e;
elements++;
return e;
}
void copy_from(const HashMap &p_t) {
if (&p_t == this)
return; /* much less bother with that */
clear();
if (!p_t.hash_table || p_t.hash_table_power == 0)
return; /* not copying from empty table */
hash_table = memnew_arr(Entry *, 1 << p_t.hash_table_power);
hash_table_power = p_t.hash_table_power;
elements = p_t.elements;
for (int i = 0; i < (1 << p_t.hash_table_power); i++) {
hash_table[i] = NULL;
const Entry *e = p_t.hash_table[i];
while (e) {
Entry *le = memnew(Entry); /* local entry */
*le = *e; /* copy data */
/* add to list and reassign pointers */
le->next = hash_table[i];
hash_table[i] = le;
e = e->next;
}
}
}
public:
void set(const TKey &p_key, const TData &p_data) {
set(Pair(p_key, p_data));
}
void set(const Pair &p_pair) {
Entry *e = NULL;
if (!hash_table)
make_hash_table(); // if no table, make one
else
e = const_cast<Entry *>(get_entry(p_pair.key));
/* if we made it up to here, the pair doesn't exist, create and assign */
if (!e) {
e = create_entry(p_pair.key);
if (!e)
return;
check_hash_table(); // perform mantenience routine
}
e->pair.data = p_pair.data;
}
bool has(const TKey &p_key) const {
return getptr(p_key) != NULL;
}
/**
* Get a key from data, return a const reference.
* WARNING: this doesn't check errors, use either getptr and check NULL, or check
* first with has(key)
*/
const TData &get(const TKey &p_key) const {
const TData *res = getptr(p_key);
ERR_FAIL_COND_V(!res, *res);
return *res;
}
TData &get(const TKey &p_key) {
TData *res = getptr(p_key);
ERR_FAIL_COND_V(!res, *res);
return *res;
}
/**
* Same as get, except it can return NULL when item was not found.
* This is mainly used for speed purposes.
*/
_FORCE_INLINE_ TData *getptr(const TKey &p_key) {
if (!hash_table)
return NULL;
Entry *e = const_cast<Entry *>(get_entry(p_key));
if (e)
return &e->pair.data;
return NULL;
}
_FORCE_INLINE_ const TData *getptr(const TKey &p_key) const {
if (!hash_table)
return NULL;
const Entry *e = const_cast<Entry *>(get_entry(p_key));
if (e)
return &e->pair.data;
return NULL;
}
/**
* Same as get, except it can return NULL when item was not found.
* This version is custom, will take a hash and a custom key (that should support operator==()
*/
template <class C>
_FORCE_INLINE_ TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) {
if (!hash_table)
return NULL;
uint32_t hash = p_custom_hash;
uint32_t index = hash & ((1 << hash_table_power) - 1);
Entry *e = hash_table[index];
while (e) {
/* checking hash first avoids comparing key, which may take longer */
if (e->hash == hash && Comparator::compare(e->pair.key, p_custom_key)) {
/* the pair exists in this hashtable, so just update data */
return &e->pair.data;
}
e = e->next;
}
return NULL;
}
template <class C>
_FORCE_INLINE_ const TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) const {
if (!hash_table)
return NULL;
uint32_t hash = p_custom_hash;
uint32_t index = hash & ((1 << hash_table_power) - 1);
const Entry *e = hash_table[index];
while (e) {
/* checking hash first avoids comparing key, which may take longer */
if (e->hash == hash && Comparator::compare(e->pair.key, p_custom_key)) {
/* the pair exists in this hashtable, so just update data */
return &e->pair.data;
}
e = e->next;
}
return NULL;
}
/**
* Erase an item, return true if erasing was successful
*/
bool erase(const TKey &p_key) {
if (!hash_table)
return false;
uint32_t hash = Hasher::hash(p_key);
uint32_t index = hash & ((1 << hash_table_power) - 1);
Entry *e = hash_table[index];
Entry *p = NULL;
while (e) {
/* checking hash first avoids comparing key, which may take longer */
if (e->hash == hash && Comparator::compare(e->pair.key, p_key)) {
if (p) {
p->next = e->next;
} else {
//begin of list
hash_table[index] = e->next;
}
memdelete(e);
elements--;
if (elements == 0)
erase_hash_table();
else
check_hash_table();
return true;
}
p = e;
e = e->next;
}
return false;
}
inline const TData &operator[](const TKey &p_key) const { //constref
return get(p_key);
}
inline TData &operator[](const TKey &p_key) { //assignment
Entry *e = NULL;
if (!hash_table)
make_hash_table(); // if no table, make one
else
e = const_cast<Entry *>(get_entry(p_key));
/* if we made it up to here, the pair doesn't exist, create */
if (!e) {
e = create_entry(p_key);
if (!e)
return *(TData *)NULL; /* panic! */
check_hash_table(); // perform mantenience routine
}
return e->pair.data;
}
/**
* Get the next key to p_key, and the first key if p_key is null.
* Returns a pointer to the next key if found, NULL otherwise.
* Adding/Removing elements while iterating will, of course, have unexpected results, don't do it.
*
* Example:
*
* const TKey *k=NULL;
*
* while( (k=table.next(k)) ) {
*
* print( *k );
* }
*
*/
const TKey *next(const TKey *p_key) const {
if (!hash_table) return NULL;
if (!p_key) { /* get the first key */
for (int i = 0; i < (1 << hash_table_power); i++) {
if (hash_table[i]) {
return &hash_table[i]->pair.key;
}
}
} else { /* get the next key */
const Entry *e = get_entry(*p_key);
ERR_FAIL_COND_V(!e, NULL); /* invalid key supplied */
if (e->next) {
/* if there is a "next" in the list, return that */
return &e->next->pair.key;
} else {
/* go to next entries */
uint32_t index = e->hash & ((1 << hash_table_power) - 1);
index++;
for (int i = index; i < (1 << hash_table_power); i++) {
if (hash_table[i]) {
return &hash_table[i]->pair.key;
}
}
}
/* nothing found, was at end */
}
return NULL; /* nothing found */
}
inline unsigned int size() const {
return elements;
}
inline bool empty() const {
return elements == 0;
}
void clear() {
/* clean up */
if (hash_table) {
for (int i = 0; i < (1 << hash_table_power); i++) {
while (hash_table[i]) {
Entry *e = hash_table[i];
hash_table[i] = e->next;
memdelete(e);
}
}
memdelete_arr(hash_table);
}
hash_table = 0;
hash_table_power = 0;
elements = 0;
}
void operator=(const HashMap &p_table) {
copy_from(p_table);
}
HashMap() {
hash_table = NULL;
elements = 0;
hash_table_power = 0;
}
void get_key_value_ptr_array(const Pair **p_pairs) const {
if (!hash_table)
return;
for (int i = 0; i < (1 << hash_table_power); i++) {
Entry *e = hash_table[i];
while (e) {
*p_pairs = &e->pair;
p_pairs++;
e = e->next;
}
}
}
void get_key_list(List<TKey> *p_keys) const {
if (!hash_table)
return;
for (int i = 0; i < (1 << hash_table_power); i++) {
Entry *e = hash_table[i];
while (e) {
p_keys->push_back(e->pair.key);
e = e->next;
}
}
}
HashMap(const HashMap &p_table) {
hash_table = NULL;
elements = 0;
hash_table_power = 0;
copy_from(p_table);
}
~HashMap() {
clear();
}
};
#endif
|