hash_map.h 20 KB

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