hash_map.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**************************************************************************/
  2. /* hash_map.hpp */
  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. #pragma once
  31. #include <godot_cpp/core/error_macros.hpp>
  32. #include <godot_cpp/core/memory.hpp>
  33. #include <godot_cpp/templates/hashfuncs.hpp>
  34. #include <godot_cpp/templates/pair.hpp>
  35. namespace godot {
  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. template <typename TKey, typename TValue,
  59. typename Hasher = HashMapHasherDefault,
  60. typename Comparator = HashMapComparatorDefault<TKey>,
  61. typename Allocator = DefaultTypedAllocator<HashMapElement<TKey, TValue>>>
  62. class HashMap {
  63. public:
  64. const uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
  65. const float MAX_OCCUPANCY = 0.75;
  66. const uint32_t EMPTY_HASH = 0;
  67. private:
  68. Allocator element_alloc;
  69. HashMapElement<TKey, TValue> **elements = nullptr;
  70. uint32_t *hashes = nullptr;
  71. HashMapElement<TKey, TValue> *head_element = nullptr;
  72. HashMapElement<TKey, TValue> *tail_element = nullptr;
  73. uint32_t capacity_index = 0;
  74. uint32_t num_elements = 0;
  75. _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const {
  76. uint32_t hash = Hasher::hash(p_key);
  77. if (unlikely(hash == EMPTY_HASH)) {
  78. hash = EMPTY_HASH + 1;
  79. }
  80. return hash;
  81. }
  82. _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash, uint32_t p_capacity) const {
  83. uint32_t original_pos = p_hash % p_capacity;
  84. return (p_pos - original_pos + p_capacity) % p_capacity;
  85. }
  86. bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
  87. if (elements == nullptr) {
  88. return false; // Failed lookups, no elements
  89. }
  90. uint32_t capacity = hash_table_size_primes[capacity_index];
  91. uint32_t hash = _hash(p_key);
  92. uint32_t pos = hash % capacity;
  93. uint32_t distance = 0;
  94. while (true) {
  95. if (hashes[pos] == EMPTY_HASH) {
  96. return false;
  97. }
  98. if (distance > _get_probe_length(pos, hashes[pos], capacity)) {
  99. return false;
  100. }
  101. if (hashes[pos] == hash && Comparator::compare(elements[pos]->data.key, p_key)) {
  102. r_pos = pos;
  103. return true;
  104. }
  105. pos = (pos + 1) % capacity;
  106. distance++;
  107. }
  108. }
  109. void _insert_with_hash(uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
  110. uint32_t capacity = hash_table_size_primes[capacity_index];
  111. uint32_t hash = p_hash;
  112. HashMapElement<TKey, TValue> *value = p_value;
  113. uint32_t distance = 0;
  114. uint32_t pos = hash % capacity;
  115. while (true) {
  116. if (hashes[pos] == EMPTY_HASH) {
  117. elements[pos] = value;
  118. hashes[pos] = hash;
  119. num_elements++;
  120. return;
  121. }
  122. // Not an empty slot, let's check the probing length of the existing one.
  123. uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity);
  124. if (existing_probe_len < distance) {
  125. SWAP(hash, hashes[pos]);
  126. SWAP(value, elements[pos]);
  127. distance = existing_probe_len;
  128. }
  129. pos = (pos + 1) % capacity;
  130. distance++;
  131. }
  132. }
  133. void _resize_and_rehash(uint32_t p_new_capacity_index) {
  134. uint32_t old_capacity = hash_table_size_primes[capacity_index];
  135. // Capacity can't be 0.
  136. capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
  137. uint32_t capacity = hash_table_size_primes[capacity_index];
  138. HashMapElement<TKey, TValue> **old_elements = elements;
  139. uint32_t *old_hashes = hashes;
  140. num_elements = 0;
  141. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  142. elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
  143. for (uint32_t i = 0; i < capacity; i++) {
  144. hashes[i] = 0;
  145. elements[i] = nullptr;
  146. }
  147. if (old_capacity == 0) {
  148. // Nothing to do.
  149. return;
  150. }
  151. for (uint32_t i = 0; i < old_capacity; i++) {
  152. if (old_hashes[i] == EMPTY_HASH) {
  153. continue;
  154. }
  155. _insert_with_hash(old_hashes[i], old_elements[i]);
  156. }
  157. Memory::free_static(old_elements);
  158. Memory::free_static(old_hashes);
  159. }
  160. _FORCE_INLINE_ HashMapElement<TKey, TValue> *_insert(const TKey &p_key, const TValue &p_value, bool p_front_insert = false) {
  161. uint32_t capacity = hash_table_size_primes[capacity_index];
  162. if (unlikely(elements == nullptr)) {
  163. // Allocate on demand to save memory.
  164. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  165. elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
  166. for (uint32_t i = 0; i < capacity; i++) {
  167. hashes[i] = EMPTY_HASH;
  168. elements[i] = nullptr;
  169. }
  170. }
  171. uint32_t pos = 0;
  172. bool exists = _lookup_pos(p_key, pos);
  173. if (exists) {
  174. elements[pos]->data.value = p_value;
  175. return elements[pos];
  176. } else {
  177. if (num_elements + 1 > MAX_OCCUPANCY * capacity) {
  178. ERR_FAIL_COND_V_MSG(capacity_index + 1 == HASH_TABLE_SIZE_MAX, nullptr, "Hash table maximum capacity reached, aborting insertion.");
  179. _resize_and_rehash(capacity_index + 1);
  180. }
  181. HashMapElement<TKey, TValue> *elem = element_alloc.new_allocation(HashMapElement<TKey, TValue>(p_key, p_value));
  182. if (tail_element == nullptr) {
  183. head_element = elem;
  184. tail_element = elem;
  185. } else if (p_front_insert) {
  186. head_element->prev = elem;
  187. elem->next = head_element;
  188. head_element = elem;
  189. } else {
  190. tail_element->next = elem;
  191. elem->prev = tail_element;
  192. tail_element = elem;
  193. }
  194. uint32_t hash = _hash(p_key);
  195. _insert_with_hash(hash, elem);
  196. return elem;
  197. }
  198. }
  199. public:
  200. _FORCE_INLINE_ uint32_t get_capacity() const { return hash_table_size_primes[capacity_index]; }
  201. _FORCE_INLINE_ uint32_t size() const { return num_elements; }
  202. /* Standard Godot Container API */
  203. bool is_empty() const {
  204. return num_elements == 0;
  205. }
  206. void clear() {
  207. if (elements == nullptr) {
  208. return;
  209. }
  210. uint32_t capacity = hash_table_size_primes[capacity_index];
  211. for (uint32_t i = 0; i < capacity; i++) {
  212. if (hashes[i] == EMPTY_HASH) {
  213. continue;
  214. }
  215. hashes[i] = EMPTY_HASH;
  216. element_alloc.delete_allocation(elements[i]);
  217. elements[i] = nullptr;
  218. }
  219. tail_element = nullptr;
  220. head_element = nullptr;
  221. num_elements = 0;
  222. }
  223. TValue &get(const TKey &p_key) {
  224. uint32_t pos = 0;
  225. bool exists = _lookup_pos(p_key, pos);
  226. CRASH_COND_MSG(!exists, "HashMap key not found.");
  227. return elements[pos]->data.value;
  228. }
  229. const TValue &get(const TKey &p_key) const {
  230. uint32_t pos = 0;
  231. bool exists = _lookup_pos(p_key, pos);
  232. CRASH_COND_MSG(!exists, "HashMap key not found.");
  233. return elements[pos]->data.value;
  234. }
  235. const TValue *getptr(const TKey &p_key) const {
  236. uint32_t pos = 0;
  237. bool exists = _lookup_pos(p_key, pos);
  238. if (exists) {
  239. return &elements[pos]->data.value;
  240. }
  241. return nullptr;
  242. }
  243. TValue *getptr(const TKey &p_key) {
  244. uint32_t pos = 0;
  245. bool exists = _lookup_pos(p_key, pos);
  246. if (exists) {
  247. return &elements[pos]->data.value;
  248. }
  249. return nullptr;
  250. }
  251. _FORCE_INLINE_ bool has(const TKey &p_key) const {
  252. uint32_t _pos = 0;
  253. return _lookup_pos(p_key, _pos);
  254. }
  255. bool erase(const TKey &p_key) {
  256. uint32_t pos = 0;
  257. bool exists = _lookup_pos(p_key, pos);
  258. if (!exists) {
  259. return false;
  260. }
  261. uint32_t capacity = hash_table_size_primes[capacity_index];
  262. uint32_t next_pos = (pos + 1) % capacity;
  263. while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity) != 0) {
  264. SWAP(hashes[next_pos], hashes[pos]);
  265. SWAP(elements[next_pos], elements[pos]);
  266. pos = next_pos;
  267. next_pos = (pos + 1) % capacity;
  268. }
  269. hashes[pos] = EMPTY_HASH;
  270. if (head_element == elements[pos]) {
  271. head_element = elements[pos]->next;
  272. }
  273. if (tail_element == elements[pos]) {
  274. tail_element = elements[pos]->prev;
  275. }
  276. if (elements[pos]->prev) {
  277. elements[pos]->prev->next = elements[pos]->next;
  278. }
  279. if (elements[pos]->next) {
  280. elements[pos]->next->prev = elements[pos]->prev;
  281. }
  282. element_alloc.delete_allocation(elements[pos]);
  283. elements[pos] = nullptr;
  284. num_elements--;
  285. return true;
  286. }
  287. // Reserves space for a number of elements, useful to avoid many resizes and rehashes.
  288. // If adding a known (possibly large) number of elements at once, must be larger than old capacity.
  289. void reserve(uint32_t p_new_capacity) {
  290. uint32_t new_index = capacity_index;
  291. while (hash_table_size_primes[new_index] < p_new_capacity) {
  292. ERR_FAIL_COND_MSG(new_index + 1 == (uint32_t)HASH_TABLE_SIZE_MAX, nullptr);
  293. new_index++;
  294. }
  295. if (new_index == capacity_index) {
  296. return;
  297. }
  298. if (elements == nullptr) {
  299. capacity_index = new_index;
  300. return; // Unallocated yet.
  301. }
  302. _resize_and_rehash(new_index);
  303. }
  304. /** Iterator API **/
  305. struct ConstIterator {
  306. _FORCE_INLINE_ const KeyValue<TKey, TValue> &operator*() const {
  307. return E->data;
  308. }
  309. _FORCE_INLINE_ const KeyValue<TKey, TValue> *operator->() const { return &E->data; }
  310. _FORCE_INLINE_ ConstIterator &operator++() {
  311. if (E) {
  312. E = E->next;
  313. }
  314. return *this;
  315. }
  316. _FORCE_INLINE_ ConstIterator &operator--() {
  317. if (E) {
  318. E = E->prev;
  319. }
  320. return *this;
  321. }
  322. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
  323. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
  324. _FORCE_INLINE_ explicit operator bool() const {
  325. return E != nullptr;
  326. }
  327. _FORCE_INLINE_ ConstIterator(const HashMapElement<TKey, TValue> *p_E) { E = p_E; }
  328. _FORCE_INLINE_ ConstIterator() {}
  329. _FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
  330. _FORCE_INLINE_ void operator=(const ConstIterator &p_it) {
  331. E = p_it.E;
  332. }
  333. private:
  334. const HashMapElement<TKey, TValue> *E = nullptr;
  335. };
  336. struct Iterator {
  337. _FORCE_INLINE_ KeyValue<TKey, TValue> &operator*() const {
  338. return E->data;
  339. }
  340. _FORCE_INLINE_ KeyValue<TKey, TValue> *operator->() const { return &E->data; }
  341. _FORCE_INLINE_ Iterator &operator++() {
  342. if (E) {
  343. E = E->next;
  344. }
  345. return *this;
  346. }
  347. _FORCE_INLINE_ Iterator &operator--() {
  348. if (E) {
  349. E = E->prev;
  350. }
  351. return *this;
  352. }
  353. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
  354. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
  355. _FORCE_INLINE_ explicit operator bool() const {
  356. return E != nullptr;
  357. }
  358. _FORCE_INLINE_ Iterator(HashMapElement<TKey, TValue> *p_E) { E = p_E; }
  359. _FORCE_INLINE_ Iterator() {}
  360. _FORCE_INLINE_ Iterator(const Iterator &p_it) { E = p_it.E; }
  361. _FORCE_INLINE_ void operator=(const Iterator &p_it) {
  362. E = p_it.E;
  363. }
  364. operator ConstIterator() const {
  365. return ConstIterator(E);
  366. }
  367. private:
  368. HashMapElement<TKey, TValue> *E = nullptr;
  369. };
  370. _FORCE_INLINE_ Iterator begin() {
  371. return Iterator(head_element);
  372. }
  373. _FORCE_INLINE_ Iterator end() {
  374. return Iterator(nullptr);
  375. }
  376. _FORCE_INLINE_ Iterator last() {
  377. return Iterator(tail_element);
  378. }
  379. _FORCE_INLINE_ Iterator find(const TKey &p_key) {
  380. uint32_t pos = 0;
  381. bool exists = _lookup_pos(p_key, pos);
  382. if (!exists) {
  383. return end();
  384. }
  385. return Iterator(elements[pos]);
  386. }
  387. _FORCE_INLINE_ void remove(const Iterator &p_iter) {
  388. if (p_iter) {
  389. erase(p_iter->key);
  390. }
  391. }
  392. _FORCE_INLINE_ ConstIterator begin() const {
  393. return ConstIterator(head_element);
  394. }
  395. _FORCE_INLINE_ ConstIterator end() const {
  396. return ConstIterator(nullptr);
  397. }
  398. _FORCE_INLINE_ ConstIterator last() const {
  399. return ConstIterator(tail_element);
  400. }
  401. _FORCE_INLINE_ ConstIterator find(const TKey &p_key) const {
  402. uint32_t pos = 0;
  403. bool exists = _lookup_pos(p_key, pos);
  404. if (!exists) {
  405. return end();
  406. }
  407. return ConstIterator(elements[pos]);
  408. }
  409. /* Indexing */
  410. const TValue &operator[](const TKey &p_key) const {
  411. uint32_t pos = 0;
  412. bool exists = _lookup_pos(p_key, pos);
  413. CRASH_COND(!exists);
  414. return elements[pos]->data.value;
  415. }
  416. TValue &operator[](const TKey &p_key) {
  417. uint32_t pos = 0;
  418. bool exists = _lookup_pos(p_key, pos);
  419. if (!exists) {
  420. return _insert(p_key, TValue())->data.value;
  421. } else {
  422. return elements[pos]->data.value;
  423. }
  424. }
  425. /* Insert */
  426. Iterator insert(const TKey &p_key, const TValue &p_value, bool p_front_insert = false) {
  427. return Iterator(_insert(p_key, p_value, p_front_insert));
  428. }
  429. /* Constructors */
  430. HashMap(const HashMap &p_other) {
  431. reserve(hash_table_size_primes[p_other.capacity_index]);
  432. if (p_other.num_elements == 0) {
  433. return;
  434. }
  435. for (const KeyValue<TKey, TValue> &E : p_other) {
  436. insert(E.key, E.value);
  437. }
  438. }
  439. void operator=(const HashMap &p_other) {
  440. if (this == &p_other) {
  441. return; // Ignore self assignment.
  442. }
  443. if (num_elements != 0) {
  444. clear();
  445. }
  446. reserve(hash_table_size_primes[p_other.capacity_index]);
  447. if (p_other.elements == nullptr) {
  448. return; // Nothing to copy.
  449. }
  450. for (const KeyValue<TKey, TValue> &E : p_other) {
  451. insert(E.key, E.value);
  452. }
  453. }
  454. HashMap(uint32_t p_initial_capacity) {
  455. // Capacity can't be 0.
  456. capacity_index = 0;
  457. reserve(p_initial_capacity);
  458. }
  459. HashMap() {
  460. capacity_index = MIN_CAPACITY_INDEX;
  461. }
  462. uint32_t debug_get_hash(uint32_t p_index) {
  463. if (num_elements == 0) {
  464. return 0;
  465. }
  466. ERR_FAIL_INDEX_V(p_index, get_capacity(), 0);
  467. return hashes[p_index];
  468. }
  469. Iterator debug_get_element(uint32_t p_index) {
  470. if (num_elements == 0) {
  471. return Iterator();
  472. }
  473. ERR_FAIL_INDEX_V(p_index, get_capacity(), Iterator());
  474. return Iterator(elements[p_index]);
  475. }
  476. ~HashMap() {
  477. clear();
  478. if (elements != nullptr) {
  479. Memory::free_static(elements);
  480. Memory::free_static(hashes);
  481. }
  482. }
  483. };
  484. } // namespace godot