hash_map.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. #ifndef HASH_MAP_H
  31. #define HASH_MAP_H
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/memory.h"
  34. #include "core/templates/hashfuncs.h"
  35. #include "core/templates/paged_allocator.h"
  36. #include "core/templates/pair.h"
  37. /**
  38. * A HashMap implementation that uses open addressing with Robin Hood hashing.
  39. * Robin Hood hashing swaps out entries that have a smaller probing distance
  40. * than the to-be-inserted entry, that evens out the average probing distance
  41. * and enables faster lookups. Backward shift deletion is employed to further
  42. * improve the performance and to avoid infinite loops in rare cases.
  43. *
  44. * Keys and values are stored in a double linked list by insertion order. This
  45. * has a slight performance overhead on lookup, which can be mostly compensated
  46. * using a paged allocator if required.
  47. *
  48. * The assignment operator copy the pairs from one map to the other.
  49. */
  50. template <typename TKey, typename TValue>
  51. struct HashMapElement {
  52. HashMapElement *next = nullptr;
  53. HashMapElement *prev = nullptr;
  54. KeyValue<TKey, TValue> data;
  55. HashMapElement() {}
  56. HashMapElement(const TKey &p_key, const TValue &p_value) :
  57. data(p_key, p_value) {}
  58. };
  59. bool _hashmap_variant_less_than(const Variant &p_left, const Variant &p_right);
  60. template <typename TKey, typename TValue,
  61. typename Hasher = HashMapHasherDefault,
  62. typename Comparator = HashMapComparatorDefault<TKey>,
  63. typename Allocator = DefaultTypedAllocator<HashMapElement<TKey, TValue>>>
  64. class HashMap {
  65. public:
  66. static constexpr uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
  67. static constexpr float MAX_OCCUPANCY = 0.75;
  68. static constexpr uint32_t EMPTY_HASH = 0;
  69. private:
  70. Allocator element_alloc;
  71. HashMapElement<TKey, TValue> **elements = nullptr;
  72. uint32_t *hashes = nullptr;
  73. HashMapElement<TKey, TValue> *head_element = nullptr;
  74. HashMapElement<TKey, TValue> *tail_element = nullptr;
  75. uint32_t capacity_index = 0;
  76. uint32_t num_elements = 0;
  77. _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const {
  78. uint32_t hash = Hasher::hash(p_key);
  79. if (unlikely(hash == EMPTY_HASH)) {
  80. hash = EMPTY_HASH + 1;
  81. }
  82. return hash;
  83. }
  84. 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) {
  85. const uint32_t original_pos = fastmod(p_hash, p_capacity_inv, p_capacity);
  86. return fastmod(p_pos - original_pos + p_capacity, p_capacity_inv, p_capacity);
  87. }
  88. bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
  89. if (elements == nullptr || num_elements == 0) {
  90. return false; // Failed lookups, no elements
  91. }
  92. const uint32_t capacity = hash_table_size_primes[capacity_index];
  93. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  94. uint32_t hash = _hash(p_key);
  95. uint32_t pos = fastmod(hash, capacity_inv, capacity);
  96. uint32_t distance = 0;
  97. while (true) {
  98. if (hashes[pos] == EMPTY_HASH) {
  99. return false;
  100. }
  101. if (distance > _get_probe_length(pos, hashes[pos], capacity, capacity_inv)) {
  102. return false;
  103. }
  104. if (hashes[pos] == hash && Comparator::compare(elements[pos]->data.key, p_key)) {
  105. r_pos = pos;
  106. return true;
  107. }
  108. pos = fastmod((pos + 1), capacity_inv, capacity);
  109. distance++;
  110. }
  111. }
  112. void _insert_with_hash(uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
  113. const uint32_t capacity = hash_table_size_primes[capacity_index];
  114. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  115. uint32_t hash = p_hash;
  116. HashMapElement<TKey, TValue> *value = p_value;
  117. uint32_t distance = 0;
  118. uint32_t pos = fastmod(hash, capacity_inv, capacity);
  119. while (true) {
  120. if (hashes[pos] == EMPTY_HASH) {
  121. elements[pos] = value;
  122. hashes[pos] = hash;
  123. num_elements++;
  124. return;
  125. }
  126. // Not an empty slot, let's check the probing length of the existing one.
  127. uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity, capacity_inv);
  128. if (existing_probe_len < distance) {
  129. SWAP(hash, hashes[pos]);
  130. SWAP(value, elements[pos]);
  131. distance = existing_probe_len;
  132. }
  133. pos = fastmod((pos + 1), capacity_inv, capacity);
  134. distance++;
  135. }
  136. }
  137. void _resize_and_rehash(uint32_t p_new_capacity_index) {
  138. uint32_t old_capacity = hash_table_size_primes[capacity_index];
  139. // Capacity can't be 0.
  140. capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
  141. uint32_t capacity = hash_table_size_primes[capacity_index];
  142. HashMapElement<TKey, TValue> **old_elements = elements;
  143. uint32_t *old_hashes = hashes;
  144. num_elements = 0;
  145. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  146. elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
  147. for (uint32_t i = 0; i < capacity; i++) {
  148. hashes[i] = 0;
  149. elements[i] = nullptr;
  150. }
  151. if (old_capacity == 0) {
  152. // Nothing to do.
  153. return;
  154. }
  155. for (uint32_t i = 0; i < old_capacity; i++) {
  156. if (old_hashes[i] == EMPTY_HASH) {
  157. continue;
  158. }
  159. _insert_with_hash(old_hashes[i], old_elements[i]);
  160. }
  161. Memory::free_static(old_elements);
  162. Memory::free_static(old_hashes);
  163. }
  164. _FORCE_INLINE_ HashMapElement<TKey, TValue> *_insert(const TKey &p_key, const TValue &p_value, bool p_front_insert = false) {
  165. uint32_t capacity = hash_table_size_primes[capacity_index];
  166. if (unlikely(elements == nullptr)) {
  167. // Allocate on demand to save memory.
  168. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  169. elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
  170. for (uint32_t i = 0; i < capacity; i++) {
  171. hashes[i] = EMPTY_HASH;
  172. elements[i] = nullptr;
  173. }
  174. }
  175. uint32_t pos = 0;
  176. bool exists = _lookup_pos(p_key, pos);
  177. if (exists) {
  178. elements[pos]->data.value = p_value;
  179. return elements[pos];
  180. } else {
  181. if (num_elements + 1 > MAX_OCCUPANCY * capacity) {
  182. ERR_FAIL_COND_V_MSG(capacity_index + 1 == HASH_TABLE_SIZE_MAX, nullptr, "Hash table maximum capacity reached, aborting insertion.");
  183. _resize_and_rehash(capacity_index + 1);
  184. }
  185. HashMapElement<TKey, TValue> *elem = element_alloc.new_allocation(HashMapElement<TKey, TValue>(p_key, p_value));
  186. if (tail_element == nullptr) {
  187. head_element = elem;
  188. tail_element = elem;
  189. } else if (p_front_insert) {
  190. head_element->prev = elem;
  191. elem->next = head_element;
  192. head_element = elem;
  193. } else {
  194. tail_element->next = elem;
  195. elem->prev = tail_element;
  196. tail_element = elem;
  197. }
  198. uint32_t hash = _hash(p_key);
  199. _insert_with_hash(hash, elem);
  200. return elem;
  201. }
  202. }
  203. public:
  204. _FORCE_INLINE_ uint32_t get_capacity() const { return hash_table_size_primes[capacity_index]; }
  205. _FORCE_INLINE_ uint32_t size() const { return num_elements; }
  206. /* Standard Godot Container API */
  207. bool is_empty() const {
  208. return num_elements == 0;
  209. }
  210. void clear() {
  211. if (elements == nullptr || num_elements == 0) {
  212. return;
  213. }
  214. uint32_t capacity = hash_table_size_primes[capacity_index];
  215. for (uint32_t i = 0; i < capacity; i++) {
  216. if (hashes[i] == EMPTY_HASH) {
  217. continue;
  218. }
  219. hashes[i] = EMPTY_HASH;
  220. element_alloc.delete_allocation(elements[i]);
  221. elements[i] = nullptr;
  222. }
  223. tail_element = nullptr;
  224. head_element = nullptr;
  225. num_elements = 0;
  226. }
  227. void sort() {
  228. if (elements == nullptr || num_elements < 2) {
  229. return; // An empty or single element HashMap is already sorted.
  230. }
  231. // Use insertion sort because we want this operation to be fast for the
  232. // common case where the input is already sorted or nearly sorted.
  233. HashMapElement<TKey, TValue> *inserting = head_element->next;
  234. while (inserting != nullptr) {
  235. HashMapElement<TKey, TValue> *after = nullptr;
  236. for (HashMapElement<TKey, TValue> *current = inserting->prev; current != nullptr; current = current->prev) {
  237. if (_hashmap_variant_less_than(inserting->data.key, current->data.key)) {
  238. after = current;
  239. } else {
  240. break;
  241. }
  242. }
  243. HashMapElement<TKey, TValue> *next = inserting->next;
  244. if (after != nullptr) {
  245. // Modify the elements around `inserting` to remove it from its current position.
  246. inserting->prev->next = next;
  247. if (next == nullptr) {
  248. tail_element = inserting->prev;
  249. } else {
  250. next->prev = inserting->prev;
  251. }
  252. // Modify `before` and `after` to insert `inserting` between them.
  253. HashMapElement<TKey, TValue> *before = after->prev;
  254. if (before == nullptr) {
  255. head_element = inserting;
  256. } else {
  257. before->next = inserting;
  258. }
  259. after->prev = inserting;
  260. // Point `inserting` to its new surroundings.
  261. inserting->prev = before;
  262. inserting->next = after;
  263. }
  264. inserting = next;
  265. }
  266. }
  267. TValue &get(const TKey &p_key) {
  268. uint32_t pos = 0;
  269. bool exists = _lookup_pos(p_key, pos);
  270. CRASH_COND_MSG(!exists, "HashMap key not found.");
  271. return elements[pos]->data.value;
  272. }
  273. const TValue &get(const TKey &p_key) const {
  274. uint32_t pos = 0;
  275. bool exists = _lookup_pos(p_key, pos);
  276. CRASH_COND_MSG(!exists, "HashMap key not found.");
  277. return elements[pos]->data.value;
  278. }
  279. const TValue *getptr(const TKey &p_key) const {
  280. uint32_t pos = 0;
  281. bool exists = _lookup_pos(p_key, pos);
  282. if (exists) {
  283. return &elements[pos]->data.value;
  284. }
  285. return nullptr;
  286. }
  287. TValue *getptr(const TKey &p_key) {
  288. uint32_t pos = 0;
  289. bool exists = _lookup_pos(p_key, pos);
  290. if (exists) {
  291. return &elements[pos]->data.value;
  292. }
  293. return nullptr;
  294. }
  295. _FORCE_INLINE_ bool has(const TKey &p_key) const {
  296. uint32_t _pos = 0;
  297. return _lookup_pos(p_key, _pos);
  298. }
  299. bool erase(const TKey &p_key) {
  300. uint32_t pos = 0;
  301. bool exists = _lookup_pos(p_key, pos);
  302. if (!exists) {
  303. return false;
  304. }
  305. const uint32_t capacity = hash_table_size_primes[capacity_index];
  306. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  307. uint32_t next_pos = fastmod((pos + 1), capacity_inv, capacity);
  308. while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
  309. SWAP(hashes[next_pos], hashes[pos]);
  310. SWAP(elements[next_pos], elements[pos]);
  311. pos = next_pos;
  312. next_pos = fastmod((pos + 1), capacity_inv, capacity);
  313. }
  314. hashes[pos] = EMPTY_HASH;
  315. if (head_element == elements[pos]) {
  316. head_element = elements[pos]->next;
  317. }
  318. if (tail_element == elements[pos]) {
  319. tail_element = elements[pos]->prev;
  320. }
  321. if (elements[pos]->prev) {
  322. elements[pos]->prev->next = elements[pos]->next;
  323. }
  324. if (elements[pos]->next) {
  325. elements[pos]->next->prev = elements[pos]->prev;
  326. }
  327. element_alloc.delete_allocation(elements[pos]);
  328. elements[pos] = nullptr;
  329. num_elements--;
  330. return true;
  331. }
  332. // Replace the key of an entry in-place, without invalidating iterators or changing the entries position during iteration.
  333. // p_old_key must exist in the map and p_new_key must not, unless it is equal to p_old_key.
  334. bool replace_key(const TKey &p_old_key, const TKey &p_new_key) {
  335. if (p_old_key == p_new_key) {
  336. return true;
  337. }
  338. uint32_t pos = 0;
  339. ERR_FAIL_COND_V(_lookup_pos(p_new_key, pos), false);
  340. ERR_FAIL_COND_V(!_lookup_pos(p_old_key, pos), false);
  341. HashMapElement<TKey, TValue> *element = elements[pos];
  342. // Delete the old entries in hashes and elements.
  343. const uint32_t capacity = hash_table_size_primes[capacity_index];
  344. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  345. uint32_t next_pos = fastmod((pos + 1), capacity_inv, capacity);
  346. while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
  347. SWAP(hashes[next_pos], hashes[pos]);
  348. SWAP(elements[next_pos], elements[pos]);
  349. pos = next_pos;
  350. next_pos = fastmod((pos + 1), capacity_inv, capacity);
  351. }
  352. hashes[pos] = EMPTY_HASH;
  353. elements[pos] = nullptr;
  354. // _insert_with_hash will increment this again.
  355. num_elements--;
  356. // Update the HashMapElement with the new key and reinsert it.
  357. const_cast<TKey &>(element->data.key) = p_new_key;
  358. uint32_t hash = _hash(p_new_key);
  359. _insert_with_hash(hash, element);
  360. return true;
  361. }
  362. // Reserves space for a number of elements, useful to avoid many resizes and rehashes.
  363. // If adding a known (possibly large) number of elements at once, must be larger than old capacity.
  364. void reserve(uint32_t p_new_capacity) {
  365. uint32_t new_index = capacity_index;
  366. while (hash_table_size_primes[new_index] < p_new_capacity) {
  367. ERR_FAIL_COND_MSG(new_index + 1 == (uint32_t)HASH_TABLE_SIZE_MAX, nullptr);
  368. new_index++;
  369. }
  370. if (new_index == capacity_index) {
  371. return;
  372. }
  373. if (elements == nullptr) {
  374. capacity_index = new_index;
  375. return; // Unallocated yet.
  376. }
  377. _resize_and_rehash(new_index);
  378. }
  379. /** Iterator API **/
  380. struct ConstIterator {
  381. _FORCE_INLINE_ const KeyValue<TKey, TValue> &operator*() const {
  382. return E->data;
  383. }
  384. _FORCE_INLINE_ const KeyValue<TKey, TValue> *operator->() const { return &E->data; }
  385. _FORCE_INLINE_ ConstIterator &operator++() {
  386. if (E) {
  387. E = E->next;
  388. }
  389. return *this;
  390. }
  391. _FORCE_INLINE_ ConstIterator &operator--() {
  392. if (E) {
  393. E = E->prev;
  394. }
  395. return *this;
  396. }
  397. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
  398. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
  399. _FORCE_INLINE_ explicit operator bool() const {
  400. return E != nullptr;
  401. }
  402. _FORCE_INLINE_ ConstIterator(const HashMapElement<TKey, TValue> *p_E) { E = p_E; }
  403. _FORCE_INLINE_ ConstIterator() {}
  404. _FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
  405. _FORCE_INLINE_ void operator=(const ConstIterator &p_it) {
  406. E = p_it.E;
  407. }
  408. private:
  409. const HashMapElement<TKey, TValue> *E = nullptr;
  410. };
  411. struct Iterator {
  412. _FORCE_INLINE_ KeyValue<TKey, TValue> &operator*() const {
  413. return E->data;
  414. }
  415. _FORCE_INLINE_ KeyValue<TKey, TValue> *operator->() const { return &E->data; }
  416. _FORCE_INLINE_ Iterator &operator++() {
  417. if (E) {
  418. E = E->next;
  419. }
  420. return *this;
  421. }
  422. _FORCE_INLINE_ Iterator &operator--() {
  423. if (E) {
  424. E = E->prev;
  425. }
  426. return *this;
  427. }
  428. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
  429. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
  430. _FORCE_INLINE_ explicit operator bool() const {
  431. return E != nullptr;
  432. }
  433. _FORCE_INLINE_ Iterator(HashMapElement<TKey, TValue> *p_E) { E = p_E; }
  434. _FORCE_INLINE_ Iterator() {}
  435. _FORCE_INLINE_ Iterator(const Iterator &p_it) { E = p_it.E; }
  436. _FORCE_INLINE_ void operator=(const Iterator &p_it) {
  437. E = p_it.E;
  438. }
  439. operator ConstIterator() const {
  440. return ConstIterator(E);
  441. }
  442. private:
  443. HashMapElement<TKey, TValue> *E = nullptr;
  444. };
  445. _FORCE_INLINE_ Iterator begin() {
  446. return Iterator(head_element);
  447. }
  448. _FORCE_INLINE_ Iterator end() {
  449. return Iterator(nullptr);
  450. }
  451. _FORCE_INLINE_ Iterator last() {
  452. return Iterator(tail_element);
  453. }
  454. _FORCE_INLINE_ Iterator find(const TKey &p_key) {
  455. uint32_t pos = 0;
  456. bool exists = _lookup_pos(p_key, pos);
  457. if (!exists) {
  458. return end();
  459. }
  460. return Iterator(elements[pos]);
  461. }
  462. _FORCE_INLINE_ void remove(const Iterator &p_iter) {
  463. if (p_iter) {
  464. erase(p_iter->key);
  465. }
  466. }
  467. _FORCE_INLINE_ ConstIterator begin() const {
  468. return ConstIterator(head_element);
  469. }
  470. _FORCE_INLINE_ ConstIterator end() const {
  471. return ConstIterator(nullptr);
  472. }
  473. _FORCE_INLINE_ ConstIterator last() const {
  474. return ConstIterator(tail_element);
  475. }
  476. _FORCE_INLINE_ ConstIterator find(const TKey &p_key) const {
  477. uint32_t pos = 0;
  478. bool exists = _lookup_pos(p_key, pos);
  479. if (!exists) {
  480. return end();
  481. }
  482. return ConstIterator(elements[pos]);
  483. }
  484. /* Indexing */
  485. const TValue &operator[](const TKey &p_key) const {
  486. uint32_t pos = 0;
  487. bool exists = _lookup_pos(p_key, pos);
  488. CRASH_COND(!exists);
  489. return elements[pos]->data.value;
  490. }
  491. TValue &operator[](const TKey &p_key) {
  492. uint32_t pos = 0;
  493. bool exists = _lookup_pos(p_key, pos);
  494. if (!exists) {
  495. return _insert(p_key, TValue())->data.value;
  496. } else {
  497. return elements[pos]->data.value;
  498. }
  499. }
  500. /* Insert */
  501. Iterator insert(const TKey &p_key, const TValue &p_value, bool p_front_insert = false) {
  502. return Iterator(_insert(p_key, p_value, p_front_insert));
  503. }
  504. /* Constructors */
  505. HashMap(const HashMap &p_other) {
  506. reserve(hash_table_size_primes[p_other.capacity_index]);
  507. if (p_other.num_elements == 0) {
  508. return;
  509. }
  510. for (const KeyValue<TKey, TValue> &E : p_other) {
  511. insert(E.key, E.value);
  512. }
  513. }
  514. void operator=(const HashMap &p_other) {
  515. if (this == &p_other) {
  516. return; // Ignore self assignment.
  517. }
  518. if (num_elements != 0) {
  519. clear();
  520. }
  521. reserve(hash_table_size_primes[p_other.capacity_index]);
  522. if (p_other.elements == nullptr) {
  523. return; // Nothing to copy.
  524. }
  525. for (const KeyValue<TKey, TValue> &E : p_other) {
  526. insert(E.key, E.value);
  527. }
  528. }
  529. HashMap(uint32_t p_initial_capacity) {
  530. // Capacity can't be 0.
  531. capacity_index = 0;
  532. reserve(p_initial_capacity);
  533. }
  534. HashMap() {
  535. capacity_index = MIN_CAPACITY_INDEX;
  536. }
  537. uint32_t debug_get_hash(uint32_t p_index) {
  538. if (num_elements == 0) {
  539. return 0;
  540. }
  541. ERR_FAIL_INDEX_V(p_index, get_capacity(), 0);
  542. return hashes[p_index];
  543. }
  544. Iterator debug_get_element(uint32_t p_index) {
  545. if (num_elements == 0) {
  546. return Iterator();
  547. }
  548. ERR_FAIL_INDEX_V(p_index, get_capacity(), Iterator());
  549. return Iterator(elements[p_index]);
  550. }
  551. ~HashMap() {
  552. clear();
  553. if (elements != nullptr) {
  554. Memory::free_static(elements);
  555. Memory::free_static(hashes);
  556. }
  557. }
  558. };
  559. #endif // HASH_MAP_H