hash_map.hpp 16 KB

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