Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/hash_map.h
9973 views
1
/**************************************************************************/
2
/* hash_map.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/os/memory.h"
34
#include "core/templates/hashfuncs.h"
35
#include "core/templates/pair.h"
36
#include "core/templates/sort_list.h"
37
38
#include <initializer_list>
39
40
/**
41
* A HashMap implementation that uses open addressing with Robin Hood hashing.
42
* Robin Hood hashing swaps out entries that have a smaller probing distance
43
* than the to-be-inserted entry, that evens out the average probing distance
44
* and enables faster lookups. Backward shift deletion is employed to further
45
* improve the performance and to avoid infinite loops in rare cases.
46
*
47
* Keys and values are stored in a double linked list by insertion order. This
48
* has a slight performance overhead on lookup, which can be mostly compensated
49
* using a paged allocator if required.
50
*
51
* The assignment operator copy the pairs from one map to the other.
52
*/
53
54
template <typename TKey, typename TValue>
55
struct HashMapElement {
56
HashMapElement *next = nullptr;
57
HashMapElement *prev = nullptr;
58
KeyValue<TKey, TValue> data;
59
HashMapElement() {}
60
HashMapElement(const TKey &p_key, const TValue &p_value) :
61
data(p_key, p_value) {}
62
};
63
64
template <typename TKey, typename TValue,
65
typename Hasher = HashMapHasherDefault,
66
typename Comparator = HashMapComparatorDefault<TKey>,
67
typename Allocator = DefaultTypedAllocator<HashMapElement<TKey, TValue>>>
68
class HashMap : private Allocator {
69
public:
70
static constexpr uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
71
static constexpr float MAX_OCCUPANCY = 0.75;
72
static constexpr uint32_t EMPTY_HASH = 0;
73
74
private:
75
HashMapElement<TKey, TValue> **elements = nullptr;
76
uint32_t *hashes = nullptr;
77
HashMapElement<TKey, TValue> *head_element = nullptr;
78
HashMapElement<TKey, TValue> *tail_element = nullptr;
79
80
uint32_t capacity_index = 0;
81
uint32_t num_elements = 0;
82
83
_FORCE_INLINE_ static uint32_t _hash(const TKey &p_key) {
84
uint32_t hash = Hasher::hash(p_key);
85
86
if (unlikely(hash == EMPTY_HASH)) {
87
hash = EMPTY_HASH + 1;
88
}
89
90
return hash;
91
}
92
93
_FORCE_INLINE_ static constexpr void _increment_mod(uint32_t &r_pos, const uint32_t p_capacity) {
94
r_pos++;
95
// `if` is faster than both fastmod and mod.
96
if (unlikely(r_pos == p_capacity)) {
97
r_pos = 0;
98
}
99
}
100
101
static _FORCE_INLINE_ uint32_t _get_probe_length(const uint32_t p_pos, const uint32_t p_hash, const uint32_t p_capacity, const uint64_t p_capacity_inv) {
102
const uint32_t original_pos = fastmod(p_hash, p_capacity_inv, p_capacity);
103
const uint32_t distance_pos = p_pos - original_pos + p_capacity;
104
// At most p_capacity over 0, so we can use an if (faster than fastmod).
105
return distance_pos >= p_capacity ? distance_pos - p_capacity : distance_pos;
106
}
107
108
bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
109
return elements != nullptr && num_elements > 0 && _lookup_pos_unchecked(p_key, _hash(p_key), r_pos);
110
}
111
112
/// Note: Assumes that elements != nullptr
113
bool _lookup_pos_unchecked(const TKey &p_key, uint32_t p_hash, uint32_t &r_pos) const {
114
const uint32_t capacity = hash_table_size_primes[capacity_index];
115
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
116
uint32_t pos = fastmod(p_hash, capacity_inv, capacity);
117
uint32_t distance = 0;
118
119
while (true) {
120
if (hashes[pos] == EMPTY_HASH) {
121
return false;
122
}
123
124
if (distance > _get_probe_length(pos, hashes[pos], capacity, capacity_inv)) {
125
return false;
126
}
127
128
if (hashes[pos] == p_hash && Comparator::compare(elements[pos]->data.key, p_key)) {
129
r_pos = pos;
130
return true;
131
}
132
133
_increment_mod(pos, capacity);
134
distance++;
135
}
136
}
137
138
void _insert_element(uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
139
const uint32_t capacity = hash_table_size_primes[capacity_index];
140
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
141
uint32_t hash = p_hash;
142
HashMapElement<TKey, TValue> *value = p_value;
143
uint32_t distance = 0;
144
uint32_t pos = fastmod(hash, capacity_inv, capacity);
145
146
while (true) {
147
if (hashes[pos] == EMPTY_HASH) {
148
elements[pos] = value;
149
hashes[pos] = hash;
150
151
num_elements++;
152
153
return;
154
}
155
156
// Not an empty slot, let's check the probing length of the existing one.
157
uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity, capacity_inv);
158
if (existing_probe_len < distance) {
159
SWAP(hash, hashes[pos]);
160
SWAP(value, elements[pos]);
161
distance = existing_probe_len;
162
}
163
164
_increment_mod(pos, capacity);
165
distance++;
166
}
167
}
168
169
void _resize_and_rehash(uint32_t p_new_capacity_index) {
170
uint32_t old_capacity = hash_table_size_primes[capacity_index];
171
172
// Capacity can't be 0.
173
capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
174
175
uint32_t capacity = hash_table_size_primes[capacity_index];
176
177
HashMapElement<TKey, TValue> **old_elements = elements;
178
uint32_t *old_hashes = hashes;
179
180
num_elements = 0;
181
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
182
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
183
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static_zeroed(sizeof(HashMapElement<TKey, TValue> *) * capacity));
184
185
if (old_capacity == 0) {
186
// Nothing to do.
187
return;
188
}
189
190
for (uint32_t i = 0; i < old_capacity; i++) {
191
if (old_hashes[i] == EMPTY_HASH) {
192
continue;
193
}
194
195
_insert_element(old_hashes[i], old_elements[i]);
196
}
197
198
Memory::free_static(old_elements);
199
Memory::free_static(old_hashes);
200
}
201
202
_FORCE_INLINE_ HashMapElement<TKey, TValue> *_insert(const TKey &p_key, const TValue &p_value, uint32_t p_hash, bool p_front_insert = false) {
203
uint32_t capacity = hash_table_size_primes[capacity_index];
204
if (unlikely(elements == nullptr)) {
205
// Allocate on demand to save memory.
206
207
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
208
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
209
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static_zeroed(sizeof(HashMapElement<TKey, TValue> *) * capacity));
210
}
211
212
if (num_elements + 1 > MAX_OCCUPANCY * capacity) {
213
ERR_FAIL_COND_V_MSG(capacity_index + 1 == HASH_TABLE_SIZE_MAX, nullptr, "Hash table maximum capacity reached, aborting insertion.");
214
_resize_and_rehash(capacity_index + 1);
215
}
216
217
HashMapElement<TKey, TValue> *elem = Allocator::new_allocation(HashMapElement<TKey, TValue>(p_key, p_value));
218
219
if (tail_element == nullptr) {
220
head_element = elem;
221
tail_element = elem;
222
} else if (p_front_insert) {
223
head_element->prev = elem;
224
elem->next = head_element;
225
head_element = elem;
226
} else {
227
tail_element->next = elem;
228
elem->prev = tail_element;
229
tail_element = elem;
230
}
231
232
_insert_element(p_hash, elem);
233
return elem;
234
}
235
236
public:
237
_FORCE_INLINE_ uint32_t get_capacity() const { return hash_table_size_primes[capacity_index]; }
238
_FORCE_INLINE_ uint32_t size() const { return num_elements; }
239
240
/* Standard Godot Container API */
241
242
bool is_empty() const {
243
return num_elements == 0;
244
}
245
246
void clear() {
247
if (elements == nullptr || num_elements == 0) {
248
return;
249
}
250
uint32_t capacity = hash_table_size_primes[capacity_index];
251
for (uint32_t i = 0; i < capacity; i++) {
252
if (hashes[i] == EMPTY_HASH) {
253
continue;
254
}
255
256
hashes[i] = EMPTY_HASH;
257
Allocator::delete_allocation(elements[i]);
258
elements[i] = nullptr;
259
}
260
261
tail_element = nullptr;
262
head_element = nullptr;
263
num_elements = 0;
264
}
265
266
void sort() {
267
sort_custom<KeyValueSort<TKey, TValue>>();
268
}
269
270
template <typename C>
271
void sort_custom() {
272
if (size() < 2) {
273
return;
274
}
275
276
using E = HashMapElement<TKey, TValue>;
277
SortList<E, KeyValue<TKey, TValue>, &E::data, &E::prev, &E::next, C> sorter;
278
sorter.sort(head_element, tail_element);
279
}
280
281
TValue &get(const TKey &p_key) {
282
uint32_t pos = 0;
283
bool exists = _lookup_pos(p_key, pos);
284
CRASH_COND_MSG(!exists, "HashMap key not found.");
285
return elements[pos]->data.value;
286
}
287
288
const TValue &get(const TKey &p_key) const {
289
uint32_t pos = 0;
290
bool exists = _lookup_pos(p_key, pos);
291
CRASH_COND_MSG(!exists, "HashMap key not found.");
292
return elements[pos]->data.value;
293
}
294
295
const TValue *getptr(const TKey &p_key) const {
296
uint32_t pos = 0;
297
bool exists = _lookup_pos(p_key, pos);
298
299
if (exists) {
300
return &elements[pos]->data.value;
301
}
302
return nullptr;
303
}
304
305
TValue *getptr(const TKey &p_key) {
306
uint32_t pos = 0;
307
bool exists = _lookup_pos(p_key, pos);
308
309
if (exists) {
310
return &elements[pos]->data.value;
311
}
312
return nullptr;
313
}
314
315
_FORCE_INLINE_ bool has(const TKey &p_key) const {
316
uint32_t _pos = 0;
317
return _lookup_pos(p_key, _pos);
318
}
319
320
bool erase(const TKey &p_key) {
321
uint32_t pos = 0;
322
bool exists = _lookup_pos(p_key, pos);
323
324
if (!exists) {
325
return false;
326
}
327
328
const uint32_t capacity = hash_table_size_primes[capacity_index];
329
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
330
uint32_t next_pos = fastmod((pos + 1), capacity_inv, capacity);
331
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
332
SWAP(hashes[next_pos], hashes[pos]);
333
SWAP(elements[next_pos], elements[pos]);
334
pos = next_pos;
335
_increment_mod(next_pos, capacity);
336
}
337
338
hashes[pos] = EMPTY_HASH;
339
340
if (head_element == elements[pos]) {
341
head_element = elements[pos]->next;
342
}
343
344
if (tail_element == elements[pos]) {
345
tail_element = elements[pos]->prev;
346
}
347
348
if (elements[pos]->prev) {
349
elements[pos]->prev->next = elements[pos]->next;
350
}
351
352
if (elements[pos]->next) {
353
elements[pos]->next->prev = elements[pos]->prev;
354
}
355
356
Allocator::delete_allocation(elements[pos]);
357
elements[pos] = nullptr;
358
359
num_elements--;
360
return true;
361
}
362
363
// Replace the key of an entry in-place, without invalidating iterators or changing the entries position during iteration.
364
// p_old_key must exist in the map and p_new_key must not, unless it is equal to p_old_key.
365
bool replace_key(const TKey &p_old_key, const TKey &p_new_key) {
366
ERR_FAIL_COND_V(elements == nullptr || num_elements == 0, false);
367
if (p_old_key == p_new_key) {
368
return true;
369
}
370
const uint32_t new_hash = _hash(p_new_key);
371
uint32_t pos = 0;
372
ERR_FAIL_COND_V(_lookup_pos_unchecked(p_new_key, new_hash, pos), false);
373
ERR_FAIL_COND_V(!_lookup_pos(p_old_key, pos), false);
374
HashMapElement<TKey, TValue> *element = elements[pos];
375
376
// Delete the old entries in hashes and elements.
377
const uint32_t capacity = hash_table_size_primes[capacity_index];
378
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
379
uint32_t next_pos = fastmod((pos + 1), capacity_inv, capacity);
380
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
381
SWAP(hashes[next_pos], hashes[pos]);
382
SWAP(elements[next_pos], elements[pos]);
383
pos = next_pos;
384
_increment_mod(next_pos, capacity);
385
}
386
hashes[pos] = EMPTY_HASH;
387
elements[pos] = nullptr;
388
// _insert_element will increment this again.
389
num_elements--;
390
391
// Update the HashMapElement with the new key and reinsert it.
392
const_cast<TKey &>(element->data.key) = p_new_key;
393
_insert_element(new_hash, element);
394
395
return true;
396
}
397
398
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
399
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
400
void reserve(uint32_t p_new_capacity) {
401
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
402
uint32_t new_index = capacity_index;
403
404
while (hash_table_size_primes[new_index] < p_new_capacity) {
405
ERR_FAIL_COND_MSG(new_index + 1 == (uint32_t)HASH_TABLE_SIZE_MAX, nullptr);
406
new_index++;
407
}
408
409
if (new_index == capacity_index) {
410
return;
411
}
412
413
if (elements == nullptr) {
414
capacity_index = new_index;
415
return; // Unallocated yet.
416
}
417
_resize_and_rehash(new_index);
418
}
419
420
/** Iterator API **/
421
422
struct ConstIterator {
423
_FORCE_INLINE_ const KeyValue<TKey, TValue> &operator*() const {
424
return E->data;
425
}
426
_FORCE_INLINE_ const KeyValue<TKey, TValue> *operator->() const { return &E->data; }
427
_FORCE_INLINE_ ConstIterator &operator++() {
428
if (E) {
429
E = E->next;
430
}
431
return *this;
432
}
433
_FORCE_INLINE_ ConstIterator &operator--() {
434
if (E) {
435
E = E->prev;
436
}
437
return *this;
438
}
439
440
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
441
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
442
443
_FORCE_INLINE_ explicit operator bool() const {
444
return E != nullptr;
445
}
446
447
_FORCE_INLINE_ ConstIterator(const HashMapElement<TKey, TValue> *p_E) { E = p_E; }
448
_FORCE_INLINE_ ConstIterator() {}
449
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
450
_FORCE_INLINE_ void operator=(const ConstIterator &p_it) {
451
E = p_it.E;
452
}
453
454
private:
455
const HashMapElement<TKey, TValue> *E = nullptr;
456
};
457
458
struct Iterator {
459
_FORCE_INLINE_ KeyValue<TKey, TValue> &operator*() const {
460
return E->data;
461
}
462
_FORCE_INLINE_ KeyValue<TKey, TValue> *operator->() const { return &E->data; }
463
_FORCE_INLINE_ Iterator &operator++() {
464
if (E) {
465
E = E->next;
466
}
467
return *this;
468
}
469
_FORCE_INLINE_ Iterator &operator--() {
470
if (E) {
471
E = E->prev;
472
}
473
return *this;
474
}
475
476
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
477
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
478
479
_FORCE_INLINE_ explicit operator bool() const {
480
return E != nullptr;
481
}
482
483
_FORCE_INLINE_ Iterator(HashMapElement<TKey, TValue> *p_E) { E = p_E; }
484
_FORCE_INLINE_ Iterator() {}
485
_FORCE_INLINE_ Iterator(const Iterator &p_it) { E = p_it.E; }
486
_FORCE_INLINE_ void operator=(const Iterator &p_it) {
487
E = p_it.E;
488
}
489
490
operator ConstIterator() const {
491
return ConstIterator(E);
492
}
493
494
private:
495
HashMapElement<TKey, TValue> *E = nullptr;
496
};
497
498
_FORCE_INLINE_ Iterator begin() {
499
return Iterator(head_element);
500
}
501
_FORCE_INLINE_ Iterator end() {
502
return Iterator(nullptr);
503
}
504
_FORCE_INLINE_ Iterator last() {
505
return Iterator(tail_element);
506
}
507
508
_FORCE_INLINE_ Iterator find(const TKey &p_key) {
509
uint32_t pos = 0;
510
bool exists = _lookup_pos(p_key, pos);
511
if (!exists) {
512
return end();
513
}
514
return Iterator(elements[pos]);
515
}
516
517
_FORCE_INLINE_ void remove(const Iterator &p_iter) {
518
if (p_iter) {
519
erase(p_iter->key);
520
}
521
}
522
523
_FORCE_INLINE_ ConstIterator begin() const {
524
return ConstIterator(head_element);
525
}
526
_FORCE_INLINE_ ConstIterator end() const {
527
return ConstIterator(nullptr);
528
}
529
_FORCE_INLINE_ ConstIterator last() const {
530
return ConstIterator(tail_element);
531
}
532
533
_FORCE_INLINE_ ConstIterator find(const TKey &p_key) const {
534
uint32_t pos = 0;
535
bool exists = _lookup_pos(p_key, pos);
536
if (!exists) {
537
return end();
538
}
539
return ConstIterator(elements[pos]);
540
}
541
542
/* Indexing */
543
544
const TValue &operator[](const TKey &p_key) const {
545
uint32_t pos = 0;
546
bool exists = _lookup_pos(p_key, pos);
547
CRASH_COND(!exists);
548
return elements[pos]->data.value;
549
}
550
551
TValue &operator[](const TKey &p_key) {
552
const uint32_t hash = _hash(p_key);
553
uint32_t pos = 0;
554
bool exists = elements && num_elements > 0 && _lookup_pos_unchecked(p_key, hash, pos);
555
if (!exists) {
556
return _insert(p_key, TValue(), hash)->data.value;
557
} else {
558
return elements[pos]->data.value;
559
}
560
}
561
562
/* Insert */
563
564
Iterator insert(const TKey &p_key, const TValue &p_value, bool p_front_insert = false) {
565
const uint32_t hash = _hash(p_key);
566
uint32_t pos = 0;
567
bool exists = elements && num_elements > 0 && _lookup_pos_unchecked(p_key, hash, pos);
568
if (!exists) {
569
return Iterator(_insert(p_key, p_value, hash, p_front_insert));
570
} else {
571
elements[pos]->data.value = p_value;
572
return Iterator(elements[pos]);
573
}
574
}
575
576
/* Constructors */
577
578
HashMap(const HashMap &p_other) {
579
reserve(hash_table_size_primes[p_other.capacity_index]);
580
581
if (p_other.num_elements == 0) {
582
return;
583
}
584
585
for (const KeyValue<TKey, TValue> &E : p_other) {
586
insert(E.key, E.value);
587
}
588
}
589
590
void operator=(const HashMap &p_other) {
591
if (this == &p_other) {
592
return; // Ignore self assignment.
593
}
594
if (num_elements != 0) {
595
clear();
596
}
597
598
reserve(hash_table_size_primes[p_other.capacity_index]);
599
600
if (p_other.elements == nullptr) {
601
return; // Nothing to copy.
602
}
603
604
for (const KeyValue<TKey, TValue> &E : p_other) {
605
insert(E.key, E.value);
606
}
607
}
608
609
HashMap(uint32_t p_initial_capacity) {
610
// Capacity can't be 0.
611
capacity_index = 0;
612
reserve(p_initial_capacity);
613
}
614
HashMap() {
615
capacity_index = MIN_CAPACITY_INDEX;
616
}
617
618
HashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
619
reserve(p_init.size());
620
for (const KeyValue<TKey, TValue> &E : p_init) {
621
insert(E.key, E.value);
622
}
623
}
624
625
uint32_t debug_get_hash(uint32_t p_index) {
626
if (num_elements == 0) {
627
return 0;
628
}
629
ERR_FAIL_INDEX_V(p_index, get_capacity(), 0);
630
return hashes[p_index];
631
}
632
Iterator debug_get_element(uint32_t p_index) {
633
if (num_elements == 0) {
634
return Iterator();
635
}
636
ERR_FAIL_INDEX_V(p_index, get_capacity(), Iterator());
637
return Iterator(elements[p_index]);
638
}
639
640
~HashMap() {
641
clear();
642
643
if (elements != nullptr) {
644
Memory::free_static(elements);
645
Memory::free_static(hashes);
646
}
647
}
648
};
649
650