Debian Codegraph @

core/templates/a_hash_map.h

raw symbols SCIP
1 /**************************************************************************/
2 /* a_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
38 #include <initializer_list>
39
40 class String;
41 class StringName;
42 class Variant;
43
44 /**
45 * An array-based implementation of a hash map. It is very efficient in terms of performance and
46 * memory usage. Works like a dynamic array, adding elements to the end of the array, and
47 * allows you to access array elements by their index by using `get_by_index` method.
48 * Example:
49 * ```
50 * AHashMap<int, Object *> map;
51 *
52 * int get_object_id_by_number(int p_number) {
53 * int id = map.get_index(p_number);
54 * return id;
55 * }
56 *
57 * Object *get_object_by_id(int p_id) {
58 * map.get_by_index(p_id).value;
59 * }
60 * ```
61 * Still, don`t erase the elements because ID can break.
62 *
63 * When an element erase, its place is taken by the element from the end.
64 *
65 * <-------------
66 * | |
67 * 6 8 X 9 32 -1 5 -10 7 X X X
68 * 6 8 7 9 32 -1 5 -10 X X X X
69 *
70 *
71 * Use RBMap if you need to iterate over sorted elements.
72 *
73 * Use HashMap if:
74 * - You need to keep an iterator or const pointer to Key and you intend to add/remove elements in the meantime.
75 * - You need to preserve the insertion order when using erase.
76 *
77 * It is recommended to use `HashMap` if `KeyValue` size is very large.
78 */
79 template <typename TKey, typename TValue,
80 typename Hasher = HashMapHasherDefault,
81 typename Comparator = HashMapComparatorDefault<TKey>>
82 class AHashMap {
83 public:
84 // Must be a power of two.
85 static constexpr uint32_t INITIAL_CAPACITY = 16;
86 static constexpr uint32_t EMPTY_HASH = 0;
87 static_assert(EMPTY_HASH == 0, "EMPTY_HASH must always be 0 for the memcpy() optimization.");
88
89 private:
90 struct Metadata {
91 uint32_t hash;
92 uint32_t element_idx;
93 };
94
95 static_assert(sizeof(Metadata) == 8);
96
97 typedef KeyValue<TKey, TValue> MapKeyValue;
98 MapKeyValue *_elements = nullptr;
99 Metadata *_metadata = nullptr;
100
101 // Due to optimization, this is `capacity - 1`. Use + 1 to get normal capacity.
102 uint32_t _capacity_mask = 0;
103 uint32_t _size = 0;
104
105 uint32_t _hash(const TKey &p_key) const {
106 uint32_t hash = Hasher::hash(p_key);
107
108 if (unlikely(hash == EMPTY_HASH)) {
109 hash = EMPTY_HASH + 1;
110 }
111
112 return hash;
113 }
114
115 static _FORCE_INLINE_ uint32_t _get_resize_count(uint32_t p_capacity_mask) {
116 return p_capacity_mask ^ (p_capacity_mask + 1) >> 2; // = get_capacity() * 0.75 - 1; Works only if p_capacity_mask = 2^n - 1.
117 }
118
119 static _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_meta_idx, uint32_t p_hash, uint32_t p_capacity) {
120 const uint32_t original_idx = p_hash & p_capacity;
121 return (p_meta_idx - original_idx + p_capacity + 1) & p_capacity;
122 }
123
124 bool _lookup_idx(const TKey &p_key, uint32_t &r_element_idx, uint32_t &r_meta_idx) const {
125 if (unlikely(_elements == nullptr)) {
126 return false; // Failed lookups, no _elements.
127 }
128 return _lookup_idx_with_hash(p_key, r_element_idx, r_meta_idx, _hash(p_key));
129 }
130
131 bool _lookup_idx_with_hash(const TKey &p_key, uint32_t &r_element_idx, uint32_t &r_meta_idx, uint32_t p_hash) const {
132 if (unlikely(_elements == nullptr)) {
133 return false; // Failed lookups, no _elements.
134 }
135
136 uint32_t meta_idx = p_hash & _capacity_mask;
137 Metadata metadata = _metadata[meta_idx];
138 if (metadata.hash == p_hash && Comparator::compare(_elements[metadata.element_idx].key, p_key)) {
139 r_element_idx = metadata.element_idx;
140 r_meta_idx = meta_idx;
141 return true;
142 }
143
144 if (metadata.hash == EMPTY_HASH) {
145 return false;
146 }
147
148 // A collision occurred.
149 meta_idx = (meta_idx + 1) & _capacity_mask;
150 uint32_t distance = 1;
151 while (true) {
152 metadata = _metadata[meta_idx];
153 if (metadata.hash == p_hash && Comparator::compare(_elements[metadata.element_idx].key, p_key)) {
154 r_element_idx = metadata.element_idx;
155 r_meta_idx = meta_idx;
156 return true;
157 }
158
159 if (metadata.hash == EMPTY_HASH) {
160 return false;
161 }
162
163 if (distance > _get_probe_length(meta_idx, metadata.hash, _capacity_mask)) {
164 return false;
165 }
166
167 meta_idx = (meta_idx + 1) & _capacity_mask;
168 distance++;
169 }
170 }
171
172 uint32_t _insert_metadata(uint32_t p_hash, uint32_t p_element_idx) {
173 uint32_t meta_idx = p_hash & _capacity_mask;
174
175 if (_metadata[meta_idx].hash == EMPTY_HASH) {
176 _metadata[meta_idx] = Metadata{ p_hash, p_element_idx };
177 return meta_idx;
178 }
179
180 uint32_t distance = 1;
181 meta_idx = (meta_idx + 1) & _capacity_mask;
182 Metadata metadata;
183 metadata.hash = p_hash;
184 metadata.element_idx = p_element_idx;
185
186 while (true) {
187 if (_metadata[meta_idx].hash == EMPTY_HASH) {
188 #ifdef DEV_ENABLED
189 if (unlikely(distance > 12)) {
190 WARN_PRINT("Excessive collision count, is the right hash function being used?");
191 }
192 #endif
193 _metadata[meta_idx] = metadata;
194 return meta_idx;
195 }
196
197 // Not an empty slot, let's check the probing length of the existing one.
198 uint32_t existing_probe_len = _get_probe_length(meta_idx, _metadata[meta_idx].hash, _capacity_mask);
199 if (existing_probe_len < distance) {
200 SWAP(metadata, _metadata[meta_idx]);
201 distance = existing_probe_len;
202 }
203
204 meta_idx = (meta_idx + 1) & _capacity_mask;
205 distance++;
206 }
207 }
208
209 void _resize_and_rehash(uint32_t p_new_capacity) {
210 uint32_t real_old_capacity = _capacity_mask + 1;
211 // Capacity can't be 0 and must be 2^n - 1.
212 _capacity_mask = MAX(4u, p_new_capacity);
213 uint32_t real_capacity = next_power_of_2(_capacity_mask);
214 _capacity_mask = real_capacity - 1;
215
216 Metadata *old_map_data = _metadata;
217
218 _metadata = reinterpret_cast<Metadata *>(Memory::alloc_static_zeroed(sizeof(Metadata) * real_capacity));
219 _elements = reinterpret_cast<MapKeyValue *>(Memory::realloc_static(_elements, sizeof(MapKeyValue) * (_get_resize_count(_capacity_mask) + 1)));
220
221 if (_size != 0) {
222 for (uint32_t i = 0; i < real_old_capacity; i++) {
223 Metadata metadata = old_map_data[i];
224 if (metadata.hash != EMPTY_HASH) {
225 _insert_metadata(metadata.hash, metadata.element_idx);
226 }
227 }
228 }
229
230 Memory::free_static(old_map_data);
231 }
232
233 int32_t _insert_element(const TKey &p_key, const TValue &p_value, uint32_t p_hash) {
234 if (unlikely(_elements == nullptr)) {
235 // Allocate on demand to save memory.
236
237 uint32_t real_capacity = _capacity_mask + 1;
238 _metadata = reinterpret_cast<Metadata *>(Memory::alloc_static_zeroed(sizeof(Metadata) * real_capacity));
239 _elements = reinterpret_cast<MapKeyValue *>(Memory::alloc_static(sizeof(MapKeyValue) * (_get_resize_count(_capacity_mask) + 1)));
240 }
241
242 if (unlikely(_size > _get_resize_count(_capacity_mask))) {
243 _resize_and_rehash(_capacity_mask * 2);
244 }
245
246 memnew_placement(&_elements[_size], MapKeyValue(p_key, p_value));
247
248 _insert_metadata(p_hash, _size);
249 _size++;
250 return _size - 1;
251 }
252
253 void _init_from(const AHashMap &p_other) {
254 _capacity_mask = p_other._capacity_mask;
255 uint32_t real_capacity = _capacity_mask + 1;
256 _size = p_other._size;
257
258 if (p_other._size == 0) {
259 return;
260 }
261
262 _metadata = reinterpret_cast<Metadata *>(Memory::alloc_static(sizeof(Metadata) * real_capacity));
263 _elements = reinterpret_cast<MapKeyValue *>(Memory::alloc_static(sizeof(MapKeyValue) * (_get_resize_count(_capacity_mask) + 1)));
264
265 if constexpr (std::is_trivially_copyable_v<TKey> && std::is_trivially_copyable_v<TValue>) {
266 void *destination = _elements;
267 const void *source = p_other._elements;
268 memcpy(destination, source, sizeof(MapKeyValue) * _size);
269 } else {
270 for (uint32_t i = 0; i < _size; i++) {
271 memnew_placement(&_elements[i], MapKeyValue(p_other._elements[i]));
272 }
273 }
274
275 memcpy(_metadata, p_other._metadata, sizeof(Metadata) * real_capacity);
276 }
277
278 public:
279 /* Standard Godot Container API */
280
281 _FORCE_INLINE_ uint32_t get_capacity() const { return _capacity_mask + 1; }
282 _FORCE_INLINE_ uint32_t size() const { return _size; }
283
284 _FORCE_INLINE_ bool is_empty() const {
285 return _size == 0;
286 }
287
288 void clear() {
289 if (_elements == nullptr || _size == 0) {
290 return;
291 }
292
293 memset(_metadata, EMPTY_HASH, (_capacity_mask + 1) * sizeof(Metadata));
294 if constexpr (!(std::is_trivially_destructible_v<TKey> && std::is_trivially_destructible_v<TValue>)) {
295 for (uint32_t i = 0; i < _size; i++) {
296 _elements[i].key.~TKey();
297 _elements[i].value.~TValue();
298 }
299 }
300
301 _size = 0;
302 }
303
304 TValue &get(const TKey &p_key) {
305 uint32_t element_idx = 0;
306 uint32_t meta_idx = 0;
307 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
308 CRASH_COND_MSG(!exists, "AHashMap key not found.");
309 return _elements[element_idx].value;
310 }
311
312 const TValue &get(const TKey &p_key) const {
313 uint32_t element_idx = 0;
314 uint32_t meta_idx = 0;
315 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
316 CRASH_COND_MSG(!exists, "AHashMap key not found.");
317 return _elements[element_idx].value;
318 }
319
320 const TValue *getptr(const TKey &p_key) const {
321 uint32_t element_idx = 0;
322 uint32_t meta_idx = 0;
323 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
324
325 if (exists) {
326 return &_elements[element_idx].value;
327 }
328 return nullptr;
329 }
330
331 TValue *getptr(const TKey &p_key) {
332 uint32_t element_idx = 0;
333 uint32_t meta_idx = 0;
334 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
335
336 if (exists) {
337 return &_elements[element_idx].value;
338 }
339 return nullptr;
340 }
341
342 bool has(const TKey &p_key) const {
343 uint32_t _idx = 0;
344 uint32_t meta_idx = 0;
345 return _lookup_idx(p_key, _idx, meta_idx);
346 }
347
348 bool erase(const TKey &p_key) {
349 uint32_t meta_idx = 0;
350 uint32_t element_idx = 0;
351 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
352
353 if (!exists) {
354 return false;
355 }
356
357 uint32_t next_meta_idx = (meta_idx + 1) & _capacity_mask;
358 while (_metadata[next_meta_idx].hash != EMPTY_HASH && _get_probe_length(next_meta_idx, _metadata[next_meta_idx].hash, _capacity_mask) != 0) {
359 SWAP(_metadata[next_meta_idx], _metadata[meta_idx]);
360
361 meta_idx = next_meta_idx;
362 next_meta_idx = (next_meta_idx + 1) & _capacity_mask;
363 }
364
365 _metadata[meta_idx].hash = EMPTY_HASH;
366 _elements[element_idx].key.~TKey();
367 _elements[element_idx].value.~TValue();
368 _size--;
369
370 if (element_idx < _size) {
371 memcpy((void *)&_elements[element_idx], (const void *)&_elements[_size], sizeof(MapKeyValue));
372 uint32_t moved_element_idx = 0;
373 uint32_t moved_meta_idx = 0;
374 _lookup_idx(_elements[_size].key, moved_element_idx, moved_meta_idx);
375 _metadata[moved_meta_idx].element_idx = element_idx;
376 }
377
378 return true;
379 }
380
381 // Replace the key of an entry in-place, without invalidating iterators or changing the entries position during iteration.
382 // p_old_key must exist in the map and p_new_key must not, unless it is equal to p_old_key.
383 bool replace_key(const TKey &p_old_key, const TKey &p_new_key) {
384 if (p_old_key == p_new_key) {
385 return true;
386 }
387 uint32_t meta_idx = 0;
388 uint32_t element_idx = 0;
389 ERR_FAIL_COND_V(_lookup_idx(p_new_key, element_idx, meta_idx), false);
390 ERR_FAIL_COND_V(!_lookup_idx(p_old_key, element_idx, meta_idx), false);
391 MapKeyValue &element = _elements[element_idx];
392 const_cast<TKey &>(element.key) = p_new_key;
393
394 uint32_t next_meta_idx = (meta_idx + 1) & _capacity_mask;
395 while (_metadata[next_meta_idx].hash != EMPTY_HASH && _get_probe_length(next_meta_idx, _metadata[next_meta_idx].hash, _capacity_mask) != 0) {
396 SWAP(_metadata[next_meta_idx], _metadata[meta_idx]);
397
398 meta_idx = next_meta_idx;
399 next_meta_idx = (next_meta_idx + 1) & _capacity_mask;
400 }
401
402 _metadata[meta_idx].hash = EMPTY_HASH;
403
404 uint32_t hash = _hash(p_new_key);
405 _insert_metadata(hash, element_idx);
406
407 return true;
408 }
409
410 // Reserves space for a number of elements, useful to avoid many resizes and rehashes.
411 // If adding a known (possibly large) number of elements at once, must be larger than old capacity.
412 void reserve(uint32_t p_new_capacity) {
413 if (_elements == nullptr) {
414 _capacity_mask = MAX(4u, p_new_capacity);
415 _capacity_mask = next_power_of_2(_capacity_mask) - 1;
416 return; // Unallocated yet.
417 }
418 if (p_new_capacity <= get_capacity()) {
419 if (p_new_capacity < size()) {
420 WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
421 }
422 return;
423 }
424 _resize_and_rehash(p_new_capacity);
425 }
426
427 /** Iterator API **/
428
429 struct ConstIterator {
430 _FORCE_INLINE_ const MapKeyValue &operator*() const {
431 return *pair;
432 }
433 _FORCE_INLINE_ const MapKeyValue *operator->() const {
434 return pair;
435 }
436 _FORCE_INLINE_ ConstIterator &operator++() {
437 pair++;
438 return *this;
439 }
440
441 _FORCE_INLINE_ ConstIterator &operator--() {
442 pair--;
443 if (pair < begin) {
444 pair = end;
445 }
446 return *this;
447 }
448
449 _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return pair == b.pair; }
450 _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return pair != b.pair; }
451
452 _FORCE_INLINE_ explicit operator bool() const {
453 return pair != end;
454 }
455
456 _FORCE_INLINE_ ConstIterator(MapKeyValue *p_key, MapKeyValue *p_begin, MapKeyValue *p_end) {
457 pair = p_key;
458 begin = p_begin;
459 end = p_end;
460 }
461 _FORCE_INLINE_ ConstIterator() {}
462 _FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) {
463 pair = p_it.pair;
464 begin = p_it.begin;
465 end = p_it.end;
466 }
467 _FORCE_INLINE_ void operator=(const ConstIterator &p_it) {
468 pair = p_it.pair;
469 begin = p_it.begin;
470 end = p_it.end;
471 }
472
473 private:
474 MapKeyValue *pair = nullptr;
475 MapKeyValue *begin = nullptr;
476 MapKeyValue *end = nullptr;
477 };
478
479 struct Iterator {
480 _FORCE_INLINE_ MapKeyValue &operator*() const {
481 return *pair;
482 }
483 _FORCE_INLINE_ MapKeyValue *operator->() const {
484 return pair;
485 }
486 _FORCE_INLINE_ Iterator &operator++() {
487 pair++;
488 return *this;
489 }
490 _FORCE_INLINE_ Iterator &operator--() {
491 pair--;
492 if (pair < begin) {
493 pair = end;
494 }
495 return *this;
496 }
497
498 _FORCE_INLINE_ bool operator==(const Iterator &b) const { return pair == b.pair; }
499 _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return pair != b.pair; }
500
501 _FORCE_INLINE_ explicit operator bool() const {
502 return pair != end;
503 }
504
505 _FORCE_INLINE_ Iterator(MapKeyValue *p_key, MapKeyValue *p_begin, MapKeyValue *p_end) {
506 pair = p_key;
507 begin = p_begin;
508 end = p_end;
509 }
510 _FORCE_INLINE_ Iterator() {}
511 _FORCE_INLINE_ Iterator(const Iterator &p_it) {
512 pair = p_it.pair;
513 begin = p_it.begin;
514 end = p_it.end;
515 }
516 _FORCE_INLINE_ void operator=(const Iterator &p_it) {
517 pair = p_it.pair;
518 begin = p_it.begin;
519 end = p_it.end;
520 }
521
522 operator ConstIterator() const {
523 return ConstIterator(pair, begin, end);
524 }
525
526 private:
527 MapKeyValue *pair = nullptr;
528 MapKeyValue *begin = nullptr;
529 MapKeyValue *end = nullptr;
530 };
531
532 _FORCE_INLINE_ Iterator begin() {
533 return Iterator(_elements, _elements, _elements + _size);
534 }
535 _FORCE_INLINE_ Iterator end() {
536 return Iterator(_elements + _size, _elements, _elements + _size);
537 }
538 _FORCE_INLINE_ Iterator last() {
539 if (unlikely(_size == 0)) {
540 return Iterator(nullptr, nullptr, nullptr);
541 }
542 return Iterator(_elements + _size - 1, _elements, _elements + _size);
543 }
544
545 Iterator find(const TKey &p_key) {
546 uint32_t meta_idx = 0;
547 uint32_t element_idx = 0;
548 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
549 if (!exists) {
550 return end();
551 }
552 return Iterator(_elements + element_idx, _elements, _elements + _size);
553 }
554
555 void remove(const Iterator &p_iter) {
556 if (p_iter) {
557 erase(p_iter->key);
558 }
559 }
560
561 _FORCE_INLINE_ ConstIterator begin() const {
562 return ConstIterator(_elements, _elements, _elements + _size);
563 }
564 _FORCE_INLINE_ ConstIterator end() const {
565 return ConstIterator(_elements + _size, _elements, _elements + _size);
566 }
567 _FORCE_INLINE_ ConstIterator last() const {
568 if (unlikely(_size == 0)) {
569 return ConstIterator(nullptr, nullptr, nullptr);
570 }
571 return ConstIterator(_elements + _size - 1, _elements, _elements + _size);
572 }
573
574 ConstIterator find(const TKey &p_key) const {
575 uint32_t element_idx = 0;
576 uint32_t meta_idx = 0;
577 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
578 if (!exists) {
579 return end();
580 }
581 return ConstIterator(_elements + element_idx, _elements, _elements + _size);
582 }
583
584 /* Indexing */
585
586 const TValue &operator[](const TKey &p_key) const {
587 uint32_t element_idx = 0;
588 uint32_t meta_idx = 0;
589 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
590 CRASH_COND(!exists);
591 return _elements[element_idx].value;
592 }
593
594 TValue &operator[](const TKey &p_key) {
595 uint32_t element_idx = 0;
596 uint32_t meta_idx = 0;
597 uint32_t hash = _hash(p_key);
598 bool exists = _lookup_idx_with_hash(p_key, element_idx, meta_idx, hash);
599
600 if (exists) {
601 return _elements[element_idx].value;
602 } else {
603 element_idx = _insert_element(p_key, TValue(), hash);
604 return _elements[element_idx].value;
605 }
606 }
607
608 /* Insert */
609
610 Iterator insert(const TKey &p_key, const TValue &p_value) {
611 uint32_t element_idx = 0;
612 uint32_t meta_idx = 0;
613 uint32_t hash = _hash(p_key);
614 bool exists = _lookup_idx_with_hash(p_key, element_idx, meta_idx, hash);
615
616 if (!exists) {
617 element_idx = _insert_element(p_key, p_value, hash);
618 } else {
619 _elements[element_idx].value = p_value;
620 }
621 return Iterator(_elements + element_idx, _elements, _elements + _size);
622 }
623
624 // Inserts an element without checking if it already exists.
625 Iterator insert_new(const TKey &p_key, const TValue &p_value) {
626 DEV_ASSERT(!has(p_key));
627 uint32_t hash = _hash(p_key);
628 uint32_t element_idx = _insert_element(p_key, p_value, hash);
629 return Iterator(_elements + element_idx, _elements, _elements + _size);
630 }
631
632 /* Array methods. */
633
634 // Unsafe. Changing keys and going outside the bounds of an array can lead to undefined behavior.
635 KeyValue<TKey, TValue> *get_elements_ptr() {
636 return _elements;
637 }
638
639 // Returns the element index. If not found, returns -1.
640 int get_index(const TKey &p_key) {
641 uint32_t element_idx = 0;
642 uint32_t meta_idx = 0;
643 bool exists = _lookup_idx(p_key, element_idx, meta_idx);
644 if (!exists) {
645 return -1;
646 }
647 return element_idx;
648 }
649
650 KeyValue<TKey, TValue> &get_by_index(uint32_t p_index) {
651 CRASH_BAD_UNSIGNED_INDEX(p_index, _size);
652 return _elements[p_index];
653 }
654
655 bool erase_by_index(uint32_t p_index) {
656 if (p_index >= size()) {
657 return false;
658 }
659 return erase(_elements[p_index].key);
660 }
661
662 /* Constructors */
663
664 AHashMap(AHashMap &&p_other) {
665 _elements = p_other._elements;
666 _metadata = p_other._metadata;
667 _capacity_mask = p_other._capacity_mask;
668 _size = p_other._size;
669
670 p_other._elements = nullptr;
671 p_other._metadata = nullptr;
672 p_other._capacity_mask = 0;
673 p_other._size = 0;
674 }
675
676 AHashMap(const AHashMap &p_other) {
677 _init_from(p_other);
678 }
679
680 void operator=(const AHashMap &p_other) {
681 if (this == &p_other) {
682 return; // Ignore self assignment.
683 }
684
685 reset();
686
687 _init_from(p_other);
688 }
689
690 AHashMap(uint32_t p_initial_capacity) {
691 // Capacity can't be 0 and must be 2^n - 1.
692 _capacity_mask = MAX(4u, p_initial_capacity);
693 _capacity_mask = next_power_of_2(_capacity_mask) - 1;
694 }
695 AHashMap() :
696 _capacity_mask(INITIAL_CAPACITY - 1) {
697 }
698
699 AHashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
700 reserve(p_init.size());
701 for (const KeyValue<TKey, TValue> &E : p_init) {
702 insert(E.key, E.value);
703 }
704 }
705
706 void reset() {
707 if (_elements != nullptr) {
708 if constexpr (!(std::is_trivially_destructible_v<TKey> && std::is_trivially_destructible_v<TValue>)) {
709 for (uint32_t i = 0; i < _size; i++) {
710 _elements[i].key.~TKey();
711 _elements[i].value.~TValue();
712 }
713 }
714 Memory::free_static(_elements);
715 Memory::free_static(_metadata);
716 _elements = nullptr;
717 }
718 _capacity_mask = INITIAL_CAPACITY - 1;
719 _size = 0;
720 }
721
722 ~AHashMap() {
723 reset();
724 }
725 };
726
727 extern template class AHashMap<int, int>;
728 extern template class AHashMap<String, int>;
729 extern template class AHashMap<StringName, StringName>;
730 extern template class AHashMap<StringName, Variant>;
731 extern template class AHashMap<StringName, int>;