Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/hash_map.h
21085 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/string/print_string.h"
35
#include "core/templates/hashfuncs.h"
36
#include "core/templates/pair.h"
37
#include "core/templates/sort_list.h"
38
39
#include <initializer_list>
40
41
/**
42
* A HashMap implementation that uses open addressing with Robin Hood hashing.
43
* Robin Hood hashing swaps out entries that have a smaller probing distance
44
* than the to-be-inserted entry, that evens out the average probing distance
45
* and enables faster lookups. Backward shift deletion is employed to further
46
* improve the performance and to avoid infinite loops in rare cases.
47
*
48
* Keys and values are stored in a double linked list by insertion order. This
49
* has a slight performance overhead on lookup, which can be mostly compensated
50
* using a paged allocator if required.
51
*
52
* The assignment operator copy the pairs from one map to the other.
53
*/
54
55
template <typename TKey, typename TValue>
56
struct HashMapElement {
57
HashMapElement *next = nullptr;
58
HashMapElement *prev = nullptr;
59
KeyValue<TKey, TValue> data;
60
HashMapElement() {}
61
HashMapElement(const TKey &p_key, const TValue &p_value) :
62
data(p_key, p_value) {}
63
};
64
65
template <typename TKey, typename TValue,
66
typename Hasher = HashMapHasherDefault,
67
typename Comparator = HashMapComparatorDefault<TKey>,
68
typename Allocator = DefaultTypedAllocator<HashMapElement<TKey, TValue>>>
69
class HashMap : private Allocator {
70
public:
71
static constexpr uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
72
static constexpr float MAX_OCCUPANCY = 0.75;
73
static constexpr uint32_t EMPTY_HASH = 0;
74
using KV = KeyValue<TKey, TValue>; // Type alias for easier access to KeyValue.
75
76
private:
77
HashMapElement<TKey, TValue> **_elements = nullptr;
78
uint32_t *_hashes = nullptr;
79
HashMapElement<TKey, TValue> *_head_element = nullptr;
80
HashMapElement<TKey, TValue> *_tail_element = nullptr;
81
82
uint32_t _capacity_idx = 0;
83
uint32_t _size = 0;
84
85
_FORCE_INLINE_ static uint32_t _hash(const TKey &p_key) {
86
uint32_t hash = Hasher::hash(p_key);
87
88
if (unlikely(hash == EMPTY_HASH)) {
89
hash = EMPTY_HASH + 1;
90
}
91
92
return hash;
93
}
94
95
_FORCE_INLINE_ static constexpr void _increment_mod(uint32_t &r_idx, const uint32_t p_capacity) {
96
r_idx++;
97
// `if` is faster than both fastmod and mod.
98
if (unlikely(r_idx == p_capacity)) {
99
r_idx = 0;
100
}
101
}
102
103
static _FORCE_INLINE_ uint32_t _get_probe_length(const uint32_t p_idx, const uint32_t p_hash, const uint32_t p_capacity, const uint64_t p_capacity_inv) {
104
const uint32_t original_idx = fastmod(p_hash, p_capacity_inv, p_capacity);
105
const uint32_t distance_idx = p_idx - original_idx + p_capacity;
106
// At most p_capacity over 0, so we can use an if (faster than fastmod).
107
return distance_idx >= p_capacity ? distance_idx - p_capacity : distance_idx;
108
}
109
110
bool _lookup_idx(const TKey &p_key, uint32_t &r_idx) const {
111
return _elements != nullptr && _size > 0 && _lookup_idx_unchecked(p_key, _hash(p_key), r_idx);
112
}
113
114
/// Note: Assumes that _elements != nullptr
115
bool _lookup_idx_unchecked(const TKey &p_key, uint32_t p_hash, uint32_t &r_idx) const {
116
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
117
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
118
uint32_t idx = fastmod(p_hash, capacity_inv, capacity);
119
uint32_t distance = 0;
120
121
while (true) {
122
if (_hashes[idx] == EMPTY_HASH) {
123
return false;
124
}
125
126
if (distance > _get_probe_length(idx, _hashes[idx], capacity, capacity_inv)) {
127
return false;
128
}
129
130
if (_hashes[idx] == p_hash && Comparator::compare(_elements[idx]->data.key, p_key)) {
131
r_idx = idx;
132
return true;
133
}
134
135
_increment_mod(idx, capacity);
136
distance++;
137
}
138
}
139
140
void _insert_element(uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
141
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
142
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
143
uint32_t hash = p_hash;
144
HashMapElement<TKey, TValue> *value = p_value;
145
uint32_t distance = 0;
146
uint32_t idx = fastmod(hash, capacity_inv, capacity);
147
148
while (true) {
149
if (_hashes[idx] == EMPTY_HASH) {
150
_elements[idx] = value;
151
_hashes[idx] = hash;
152
153
_size++;
154
155
return;
156
}
157
158
// Not an empty slot, let's check the probing length of the existing one.
159
uint32_t existing_probe_len = _get_probe_length(idx, _hashes[idx], capacity, capacity_inv);
160
if (existing_probe_len < distance) {
161
SWAP(hash, _hashes[idx]);
162
SWAP(value, _elements[idx]);
163
distance = existing_probe_len;
164
}
165
166
_increment_mod(idx, capacity);
167
distance++;
168
}
169
}
170
171
void _resize_and_rehash(uint32_t p_new_capacity_idx) {
172
uint32_t old_capacity = hash_table_size_primes[_capacity_idx];
173
174
// Capacity can't be 0.
175
_capacity_idx = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_idx);
176
177
uint32_t capacity = hash_table_size_primes[_capacity_idx];
178
179
HashMapElement<TKey, TValue> **old_elements = _elements;
180
uint32_t *old_hashes = _hashes;
181
182
_size = 0;
183
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
184
185
_hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
186
_elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
187
188
if (old_capacity == 0) {
189
// Nothing to do.
190
return;
191
}
192
193
for (uint32_t i = 0; i < old_capacity; i++) {
194
if (old_hashes[i] == EMPTY_HASH) {
195
continue;
196
}
197
198
_insert_element(old_hashes[i], old_elements[i]);
199
}
200
201
Memory::free_static(old_elements);
202
Memory::free_static(old_hashes);
203
}
204
205
_FORCE_INLINE_ HashMapElement<TKey, TValue> *_insert(const TKey &p_key, const TValue &p_value, uint32_t p_hash, bool p_front_insert = false) {
206
uint32_t capacity = hash_table_size_primes[_capacity_idx];
207
if (unlikely(_elements == nullptr)) {
208
// Allocate on demand to save memory.
209
210
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
211
_hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
212
_elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
213
}
214
215
if (_size + 1 > MAX_OCCUPANCY * capacity) {
216
ERR_FAIL_COND_V_MSG(_capacity_idx + 1 == HASH_TABLE_SIZE_MAX, nullptr, "Hash table maximum capacity reached, aborting insertion.");
217
_resize_and_rehash(_capacity_idx + 1);
218
}
219
220
HashMapElement<TKey, TValue> *elem = Allocator::new_allocation(HashMapElement<TKey, TValue>(p_key, p_value));
221
222
if (_tail_element == nullptr) {
223
_head_element = elem;
224
_tail_element = elem;
225
} else if (p_front_insert) {
226
_head_element->prev = elem;
227
elem->next = _head_element;
228
_head_element = elem;
229
} else {
230
_tail_element->next = elem;
231
elem->prev = _tail_element;
232
_tail_element = elem;
233
}
234
235
_insert_element(p_hash, elem);
236
return elem;
237
}
238
239
void _clear_data() {
240
HashMapElement<TKey, TValue> *current = _tail_element;
241
while (current != nullptr) {
242
HashMapElement<TKey, TValue> *prev = current->prev;
243
Allocator::delete_allocation(current);
244
current = prev;
245
}
246
}
247
248
public:
249
_FORCE_INLINE_ uint32_t get_capacity() const { return hash_table_size_primes[_capacity_idx]; }
250
_FORCE_INLINE_ uint32_t size() const { return _size; }
251
252
/* Standard Godot Container API */
253
254
bool is_empty() const {
255
return _size == 0;
256
}
257
258
void clear() {
259
if (_elements == nullptr || _size == 0) {
260
return;
261
}
262
263
_clear_data();
264
memset(_hashes, EMPTY_HASH, get_capacity() * sizeof(uint32_t));
265
266
_tail_element = nullptr;
267
_head_element = nullptr;
268
_size = 0;
269
}
270
271
void sort() {
272
sort_custom<KeyValueSort<TKey, TValue>>();
273
}
274
275
template <typename C>
276
void sort_custom() {
277
if (size() < 2) {
278
return;
279
}
280
281
using E = HashMapElement<TKey, TValue>;
282
SortList<E, KeyValue<TKey, TValue>, &E::data, &E::prev, &E::next, C> sorter;
283
sorter.sort(_head_element, _tail_element);
284
}
285
286
TValue &get(const TKey &p_key) {
287
uint32_t idx = 0;
288
bool exists = _lookup_idx(p_key, idx);
289
CRASH_COND_MSG(!exists, "HashMap key not found.");
290
return _elements[idx]->data.value;
291
}
292
293
const TValue &get(const TKey &p_key) const {
294
uint32_t idx = 0;
295
bool exists = _lookup_idx(p_key, idx);
296
CRASH_COND_MSG(!exists, "HashMap key not found.");
297
return _elements[idx]->data.value;
298
}
299
300
const TValue *getptr(const TKey &p_key) const {
301
uint32_t idx = 0;
302
bool exists = _lookup_idx(p_key, idx);
303
304
if (exists) {
305
return &_elements[idx]->data.value;
306
}
307
return nullptr;
308
}
309
310
TValue *getptr(const TKey &p_key) {
311
uint32_t idx = 0;
312
bool exists = _lookup_idx(p_key, idx);
313
314
if (exists) {
315
return &_elements[idx]->data.value;
316
}
317
return nullptr;
318
}
319
320
_FORCE_INLINE_ bool has(const TKey &p_key) const {
321
uint32_t _idx = 0;
322
return _lookup_idx(p_key, _idx);
323
}
324
325
bool erase(const TKey &p_key) {
326
uint32_t idx = 0;
327
bool exists = _lookup_idx(p_key, idx);
328
329
if (!exists) {
330
return false;
331
}
332
333
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
334
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
335
uint32_t next_idx = fastmod((idx + 1), capacity_inv, capacity);
336
while (_hashes[next_idx] != EMPTY_HASH && _get_probe_length(next_idx, _hashes[next_idx], capacity, capacity_inv) != 0) {
337
SWAP(_hashes[next_idx], _hashes[idx]);
338
SWAP(_elements[next_idx], _elements[idx]);
339
idx = next_idx;
340
_increment_mod(next_idx, capacity);
341
}
342
343
_hashes[idx] = EMPTY_HASH;
344
345
if (_head_element == _elements[idx]) {
346
_head_element = _elements[idx]->next;
347
}
348
349
if (_tail_element == _elements[idx]) {
350
_tail_element = _elements[idx]->prev;
351
}
352
353
if (_elements[idx]->prev) {
354
_elements[idx]->prev->next = _elements[idx]->next;
355
}
356
357
if (_elements[idx]->next) {
358
_elements[idx]->next->prev = _elements[idx]->prev;
359
}
360
361
Allocator::delete_allocation(_elements[idx]);
362
363
_size--;
364
return true;
365
}
366
367
// Replace the key of an entry in-place, without invalidating iterators or changing the entries position during iteration.
368
// p_old_key must exist in the map and p_new_key must not, unless it is equal to p_old_key.
369
bool replace_key(const TKey &p_old_key, const TKey &p_new_key) {
370
ERR_FAIL_COND_V(_elements == nullptr || _size == 0, false);
371
if (p_old_key == p_new_key) {
372
return true;
373
}
374
const uint32_t new_hash = _hash(p_new_key);
375
uint32_t idx = 0;
376
ERR_FAIL_COND_V(_lookup_idx_unchecked(p_new_key, new_hash, idx), false);
377
ERR_FAIL_COND_V(!_lookup_idx(p_old_key, idx), false);
378
HashMapElement<TKey, TValue> *element = _elements[idx];
379
380
// Delete the old entries in _hashes and _elements.
381
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
382
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
383
uint32_t next_idx = fastmod((idx + 1), capacity_inv, capacity);
384
while (_hashes[next_idx] != EMPTY_HASH && _get_probe_length(next_idx, _hashes[next_idx], capacity, capacity_inv) != 0) {
385
SWAP(_hashes[next_idx], _hashes[idx]);
386
SWAP(_elements[next_idx], _elements[idx]);
387
idx = next_idx;
388
_increment_mod(next_idx, capacity);
389
}
390
391
_hashes[idx] = EMPTY_HASH;
392
393
// _insert_element will increment this again.
394
_size--;
395
396
// Update the HashMapElement with the new key and reinsert it.
397
const_cast<TKey &>(element->data.key) = p_new_key;
398
_insert_element(new_hash, element);
399
400
return true;
401
}
402
403
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
404
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
405
void reserve(uint32_t p_new_capacity) {
406
uint32_t new_idx = _capacity_idx;
407
408
while (hash_table_size_primes[new_idx] < p_new_capacity) {
409
ERR_FAIL_COND_MSG(new_idx + 1 == (uint32_t)HASH_TABLE_SIZE_MAX, nullptr);
410
new_idx++;
411
}
412
413
if (new_idx == _capacity_idx) {
414
if (p_new_capacity < _size) {
415
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
416
}
417
return;
418
}
419
420
if (_elements == nullptr) {
421
_capacity_idx = new_idx;
422
return; // Unallocated yet.
423
}
424
_resize_and_rehash(new_idx);
425
}
426
427
/** Iterator API **/
428
429
struct ConstIterator {
430
_FORCE_INLINE_ const KeyValue<TKey, TValue> &operator*() const {
431
return E->data;
432
}
433
_FORCE_INLINE_ const KeyValue<TKey, TValue> *operator->() const { return &E->data; }
434
_FORCE_INLINE_ ConstIterator &operator++() {
435
if (E) {
436
E = E->next;
437
}
438
return *this;
439
}
440
_FORCE_INLINE_ ConstIterator &operator--() {
441
if (E) {
442
E = E->prev;
443
}
444
return *this;
445
}
446
447
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
448
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
449
450
_FORCE_INLINE_ explicit operator bool() const {
451
return E != nullptr;
452
}
453
454
_FORCE_INLINE_ ConstIterator(const HashMapElement<TKey, TValue> *p_E) { E = p_E; }
455
_FORCE_INLINE_ ConstIterator() {}
456
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
457
_FORCE_INLINE_ void operator=(const ConstIterator &p_it) {
458
E = p_it.E;
459
}
460
461
private:
462
const HashMapElement<TKey, TValue> *E = nullptr;
463
};
464
465
struct Iterator {
466
_FORCE_INLINE_ KeyValue<TKey, TValue> &operator*() const {
467
return E->data;
468
}
469
_FORCE_INLINE_ KeyValue<TKey, TValue> *operator->() const { return &E->data; }
470
_FORCE_INLINE_ Iterator &operator++() {
471
if (E) {
472
E = E->next;
473
}
474
return *this;
475
}
476
_FORCE_INLINE_ Iterator &operator--() {
477
if (E) {
478
E = E->prev;
479
}
480
return *this;
481
}
482
483
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
484
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
485
486
_FORCE_INLINE_ explicit operator bool() const {
487
return E != nullptr;
488
}
489
490
_FORCE_INLINE_ Iterator(HashMapElement<TKey, TValue> *p_E) { E = p_E; }
491
_FORCE_INLINE_ Iterator() {}
492
_FORCE_INLINE_ Iterator(const Iterator &p_it) { E = p_it.E; }
493
_FORCE_INLINE_ void operator=(const Iterator &p_it) {
494
E = p_it.E;
495
}
496
497
operator ConstIterator() const {
498
return ConstIterator(E);
499
}
500
501
private:
502
HashMapElement<TKey, TValue> *E = nullptr;
503
};
504
505
_FORCE_INLINE_ Iterator begin() {
506
return Iterator(_head_element);
507
}
508
_FORCE_INLINE_ Iterator end() {
509
return Iterator(nullptr);
510
}
511
_FORCE_INLINE_ Iterator last() {
512
return Iterator(_tail_element);
513
}
514
515
_FORCE_INLINE_ Iterator find(const TKey &p_key) {
516
uint32_t idx = 0;
517
bool exists = _lookup_idx(p_key, idx);
518
if (!exists) {
519
return end();
520
}
521
return Iterator(_elements[idx]);
522
}
523
524
_FORCE_INLINE_ void remove(const Iterator &p_iter) {
525
if (p_iter) {
526
erase(p_iter->key);
527
}
528
}
529
530
_FORCE_INLINE_ ConstIterator begin() const {
531
return ConstIterator(_head_element);
532
}
533
_FORCE_INLINE_ ConstIterator end() const {
534
return ConstIterator(nullptr);
535
}
536
_FORCE_INLINE_ ConstIterator last() const {
537
return ConstIterator(_tail_element);
538
}
539
540
_FORCE_INLINE_ ConstIterator find(const TKey &p_key) const {
541
uint32_t idx = 0;
542
bool exists = _lookup_idx(p_key, idx);
543
if (!exists) {
544
return end();
545
}
546
return ConstIterator(_elements[idx]);
547
}
548
549
/* Indexing */
550
551
const TValue &operator[](const TKey &p_key) const {
552
uint32_t idx = 0;
553
bool exists = _lookup_idx(p_key, idx);
554
CRASH_COND(!exists);
555
return _elements[idx]->data.value;
556
}
557
558
TValue &operator[](const TKey &p_key) {
559
const uint32_t hash = _hash(p_key);
560
uint32_t idx = 0;
561
bool exists = _elements && _size > 0 && _lookup_idx_unchecked(p_key, hash, idx);
562
if (!exists) {
563
return _insert(p_key, TValue(), hash)->data.value;
564
} else {
565
return _elements[idx]->data.value;
566
}
567
}
568
569
/* Insert */
570
571
Iterator insert(const TKey &p_key, const TValue &p_value, bool p_front_insert = false) {
572
const uint32_t hash = _hash(p_key);
573
uint32_t idx = 0;
574
bool exists = _elements && _size > 0 && _lookup_idx_unchecked(p_key, hash, idx);
575
if (!exists) {
576
return Iterator(_insert(p_key, p_value, hash, p_front_insert));
577
} else {
578
_elements[idx]->data.value = p_value;
579
return Iterator(_elements[idx]);
580
}
581
}
582
583
/* Constructors */
584
585
explicit HashMap(const HashMap &p_other) {
586
reserve(hash_table_size_primes[p_other._capacity_idx]);
587
588
if (p_other._size == 0) {
589
return;
590
}
591
592
for (const KeyValue<TKey, TValue> &E : p_other) {
593
insert(E.key, E.value);
594
}
595
}
596
597
HashMap(HashMap &&p_other) {
598
_elements = p_other._elements;
599
_hashes = p_other._hashes;
600
_head_element = p_other._head_element;
601
_tail_element = p_other._tail_element;
602
_capacity_idx = p_other._capacity_idx;
603
_size = p_other._size;
604
605
p_other._elements = nullptr;
606
p_other._hashes = nullptr;
607
p_other._head_element = nullptr;
608
p_other._tail_element = nullptr;
609
p_other._capacity_idx = MIN_CAPACITY_INDEX;
610
p_other._size = 0;
611
}
612
613
void operator=(const HashMap &p_other) {
614
if (this == &p_other) {
615
return; // Ignore self assignment.
616
}
617
if (_size != 0) {
618
clear();
619
}
620
621
reserve(hash_table_size_primes[p_other._capacity_idx]);
622
623
if (p_other._elements == nullptr) {
624
return; // Nothing to copy.
625
}
626
627
for (const KeyValue<TKey, TValue> &E : p_other) {
628
insert(E.key, E.value);
629
}
630
}
631
632
HashMap &operator=(HashMap &&p_other) {
633
if (this == &p_other) {
634
return *this;
635
}
636
637
if (_size != 0) {
638
clear();
639
}
640
if (_elements != nullptr) {
641
Memory::free_static(_elements);
642
Memory::free_static(_hashes);
643
}
644
645
_elements = p_other._elements;
646
_hashes = p_other._hashes;
647
_head_element = p_other._head_element;
648
_tail_element = p_other._tail_element;
649
_capacity_idx = p_other._capacity_idx;
650
_size = p_other._size;
651
652
p_other._elements = nullptr;
653
p_other._hashes = nullptr;
654
p_other._head_element = nullptr;
655
p_other._tail_element = nullptr;
656
p_other._capacity_idx = MIN_CAPACITY_INDEX;
657
p_other._size = 0;
658
659
return *this;
660
}
661
662
HashMap(uint32_t p_initial_capacity) {
663
// Capacity can't be 0.
664
_capacity_idx = 0;
665
reserve(p_initial_capacity);
666
}
667
HashMap() {
668
_capacity_idx = MIN_CAPACITY_INDEX;
669
}
670
671
HashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
672
reserve(p_init.size());
673
for (const KeyValue<TKey, TValue> &E : p_init) {
674
insert(E.key, E.value);
675
}
676
}
677
678
uint32_t debug_get_hash(uint32_t p_idx) {
679
if (_size == 0) {
680
return 0;
681
}
682
ERR_FAIL_INDEX_V(p_idx, get_capacity(), 0);
683
return _hashes[p_idx];
684
}
685
Iterator debug_get_element(uint32_t p_idx) {
686
if (_size == 0) {
687
return Iterator();
688
}
689
ERR_FAIL_INDEX_V(p_idx, get_capacity(), Iterator());
690
return Iterator(_elements[p_idx]);
691
}
692
693
~HashMap() {
694
_clear_data();
695
696
if (_elements != nullptr) {
697
Memory::free_static(_elements);
698
Memory::free_static(_hashes);
699
}
700
}
701
};
702
703