hash_set.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**************************************************************************/
  2. /* hash_set.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. #pragma once
  31. #include "core/os/memory.h"
  32. #include "core/templates/hashfuncs.h"
  33. /**
  34. * Implementation of Set using a bidi indexed hash map.
  35. * Use RBSet instead of this only if the following conditions are met:
  36. *
  37. * - You need to keep an iterator or const pointer to Key and you intend to add/remove elements in the meantime.
  38. * - Iteration order does matter (via operator<)
  39. *
  40. */
  41. template <typename TKey,
  42. typename Hasher = HashMapHasherDefault,
  43. typename Comparator = HashMapComparatorDefault<TKey>>
  44. class HashSet {
  45. public:
  46. static constexpr uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
  47. static constexpr float MAX_OCCUPANCY = 0.75;
  48. static constexpr uint32_t EMPTY_HASH = 0;
  49. private:
  50. TKey *keys = nullptr;
  51. uint32_t *hash_to_key = nullptr;
  52. uint32_t *key_to_hash = nullptr;
  53. uint32_t *hashes = nullptr;
  54. uint32_t capacity_index = 0;
  55. uint32_t num_elements = 0;
  56. _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const {
  57. uint32_t hash = Hasher::hash(p_key);
  58. if (unlikely(hash == EMPTY_HASH)) {
  59. hash = EMPTY_HASH + 1;
  60. }
  61. return hash;
  62. }
  63. _FORCE_INLINE_ static constexpr void _increment_mod(uint32_t &r_pos, const uint32_t p_capacity) {
  64. r_pos++;
  65. // `if` is faster than both fastmod and mod.
  66. if (unlikely(r_pos == p_capacity)) {
  67. r_pos = 0;
  68. }
  69. }
  70. 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) {
  71. const uint32_t original_pos = fastmod(p_hash, p_capacity_inv, p_capacity);
  72. const uint32_t distance_pos = p_pos - original_pos + p_capacity;
  73. // At most p_capacity over 0, so we can use an if (faster than fastmod).
  74. return distance_pos >= p_capacity ? distance_pos - p_capacity : distance_pos;
  75. }
  76. bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
  77. if (keys == nullptr || num_elements == 0) {
  78. return false; // Failed lookups, no elements
  79. }
  80. const uint32_t capacity = hash_table_size_primes[capacity_index];
  81. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  82. uint32_t hash = _hash(p_key);
  83. uint32_t pos = fastmod(hash, capacity_inv, capacity);
  84. uint32_t distance = 0;
  85. while (true) {
  86. if (hashes[pos] == EMPTY_HASH) {
  87. return false;
  88. }
  89. if (hashes[pos] == hash && Comparator::compare(keys[hash_to_key[pos]], p_key)) {
  90. r_pos = hash_to_key[pos];
  91. return true;
  92. }
  93. if (distance > _get_probe_length(pos, hashes[pos], capacity, capacity_inv)) {
  94. return false;
  95. }
  96. _increment_mod(pos, capacity);
  97. distance++;
  98. }
  99. }
  100. uint32_t _insert_with_hash(uint32_t p_hash, uint32_t p_index) {
  101. const uint32_t capacity = hash_table_size_primes[capacity_index];
  102. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  103. uint32_t hash = p_hash;
  104. uint32_t index = p_index;
  105. uint32_t distance = 0;
  106. uint32_t pos = fastmod(hash, capacity_inv, capacity);
  107. while (true) {
  108. if (hashes[pos] == EMPTY_HASH) {
  109. hashes[pos] = hash;
  110. key_to_hash[index] = pos;
  111. hash_to_key[pos] = index;
  112. return pos;
  113. }
  114. // Not an empty slot, let's check the probing length of the existing one.
  115. uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity, capacity_inv);
  116. if (existing_probe_len < distance) {
  117. key_to_hash[index] = pos;
  118. SWAP(hash, hashes[pos]);
  119. SWAP(index, hash_to_key[pos]);
  120. distance = existing_probe_len;
  121. }
  122. _increment_mod(pos, capacity);
  123. distance++;
  124. }
  125. }
  126. void _resize_and_rehash(uint32_t p_new_capacity_index) {
  127. // Capacity can't be 0.
  128. capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
  129. uint32_t capacity = hash_table_size_primes[capacity_index];
  130. uint32_t *old_hashes = hashes;
  131. uint32_t *old_key_to_hash = key_to_hash;
  132. static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
  133. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
  134. keys = reinterpret_cast<TKey *>(Memory::realloc_static(keys, sizeof(TKey) * capacity));
  135. key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  136. hash_to_key = reinterpret_cast<uint32_t *>(Memory::realloc_static(hash_to_key, sizeof(uint32_t) * capacity));
  137. for (uint32_t i = 0; i < num_elements; i++) {
  138. uint32_t h = old_hashes[old_key_to_hash[i]];
  139. _insert_with_hash(h, i);
  140. }
  141. Memory::free_static(old_hashes);
  142. Memory::free_static(old_key_to_hash);
  143. }
  144. _FORCE_INLINE_ int32_t _insert(const TKey &p_key) {
  145. uint32_t capacity = hash_table_size_primes[capacity_index];
  146. if (unlikely(keys == nullptr)) {
  147. // Allocate on demand to save memory.
  148. static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
  149. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
  150. keys = reinterpret_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
  151. key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  152. hash_to_key = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  153. }
  154. uint32_t pos = 0;
  155. bool exists = _lookup_pos(p_key, pos);
  156. if (exists) {
  157. return pos;
  158. } else {
  159. if (num_elements + 1 > MAX_OCCUPANCY * capacity) {
  160. ERR_FAIL_COND_V_MSG(capacity_index + 1 == HASH_TABLE_SIZE_MAX, -1, "Hash table maximum capacity reached, aborting insertion.");
  161. _resize_and_rehash(capacity_index + 1);
  162. }
  163. uint32_t hash = _hash(p_key);
  164. memnew_placement(&keys[num_elements], TKey(p_key));
  165. _insert_with_hash(hash, num_elements);
  166. num_elements++;
  167. return num_elements - 1;
  168. }
  169. }
  170. void _init_from(const HashSet &p_other) {
  171. capacity_index = p_other.capacity_index;
  172. num_elements = p_other.num_elements;
  173. if (p_other.num_elements == 0) {
  174. return;
  175. }
  176. uint32_t capacity = hash_table_size_primes[capacity_index];
  177. hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  178. keys = reinterpret_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
  179. key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  180. hash_to_key = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  181. for (uint32_t i = 0; i < num_elements; i++) {
  182. memnew_placement(&keys[i], TKey(p_other.keys[i]));
  183. key_to_hash[i] = p_other.key_to_hash[i];
  184. }
  185. for (uint32_t i = 0; i < capacity; i++) {
  186. hashes[i] = p_other.hashes[i];
  187. hash_to_key[i] = p_other.hash_to_key[i];
  188. }
  189. }
  190. public:
  191. _FORCE_INLINE_ uint32_t get_capacity() const { return hash_table_size_primes[capacity_index]; }
  192. _FORCE_INLINE_ uint32_t size() const { return num_elements; }
  193. /* Standard Godot Container API */
  194. bool is_empty() const {
  195. return num_elements == 0;
  196. }
  197. void clear() {
  198. if (keys == nullptr || num_elements == 0) {
  199. return;
  200. }
  201. uint32_t capacity = hash_table_size_primes[capacity_index];
  202. for (uint32_t i = 0; i < capacity; i++) {
  203. hashes[i] = EMPTY_HASH;
  204. }
  205. for (uint32_t i = 0; i < num_elements; i++) {
  206. keys[i].~TKey();
  207. }
  208. num_elements = 0;
  209. }
  210. _FORCE_INLINE_ bool has(const TKey &p_key) const {
  211. uint32_t _pos = 0;
  212. return _lookup_pos(p_key, _pos);
  213. }
  214. bool erase(const TKey &p_key) {
  215. uint32_t pos = 0;
  216. bool exists = _lookup_pos(p_key, pos);
  217. if (!exists) {
  218. return false;
  219. }
  220. uint32_t key_pos = pos;
  221. pos = key_to_hash[pos]; //make hash pos
  222. const uint32_t capacity = hash_table_size_primes[capacity_index];
  223. const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
  224. uint32_t next_pos = fastmod(pos + 1, capacity_inv, capacity);
  225. while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
  226. uint32_t kpos = hash_to_key[pos];
  227. uint32_t kpos_next = hash_to_key[next_pos];
  228. SWAP(key_to_hash[kpos], key_to_hash[kpos_next]);
  229. SWAP(hashes[next_pos], hashes[pos]);
  230. SWAP(hash_to_key[next_pos], hash_to_key[pos]);
  231. pos = next_pos;
  232. _increment_mod(next_pos, capacity);
  233. }
  234. hashes[pos] = EMPTY_HASH;
  235. keys[key_pos].~TKey();
  236. num_elements--;
  237. if (key_pos < num_elements) {
  238. // Not the last key, move the last one here to keep keys lineal
  239. memnew_placement(&keys[key_pos], TKey(keys[num_elements]));
  240. keys[num_elements].~TKey();
  241. key_to_hash[key_pos] = key_to_hash[num_elements];
  242. hash_to_key[key_to_hash[num_elements]] = key_pos;
  243. }
  244. return true;
  245. }
  246. // Reserves space for a number of elements, useful to avoid many resizes and rehashes.
  247. // If adding a known (possibly large) number of elements at once, must be larger than old capacity.
  248. void reserve(uint32_t p_new_capacity) {
  249. ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
  250. uint32_t new_index = capacity_index;
  251. while (hash_table_size_primes[new_index] < p_new_capacity) {
  252. ERR_FAIL_COND_MSG(new_index + 1 == (uint32_t)HASH_TABLE_SIZE_MAX, nullptr);
  253. new_index++;
  254. }
  255. if (new_index == capacity_index) {
  256. return;
  257. }
  258. if (keys == nullptr) {
  259. capacity_index = new_index;
  260. return; // Unallocated yet.
  261. }
  262. _resize_and_rehash(new_index);
  263. }
  264. /** Iterator API **/
  265. struct Iterator {
  266. _FORCE_INLINE_ const TKey &operator*() const {
  267. return keys[index];
  268. }
  269. _FORCE_INLINE_ const TKey *operator->() const {
  270. return &keys[index];
  271. }
  272. _FORCE_INLINE_ Iterator &operator++() {
  273. index++;
  274. if (index >= (int32_t)num_keys) {
  275. index = -1;
  276. keys = nullptr;
  277. num_keys = 0;
  278. }
  279. return *this;
  280. }
  281. _FORCE_INLINE_ Iterator &operator--() {
  282. index--;
  283. if (index < 0) {
  284. index = -1;
  285. keys = nullptr;
  286. num_keys = 0;
  287. }
  288. return *this;
  289. }
  290. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return keys == b.keys && index == b.index; }
  291. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return keys != b.keys || index != b.index; }
  292. _FORCE_INLINE_ explicit operator bool() const {
  293. return keys != nullptr;
  294. }
  295. _FORCE_INLINE_ Iterator(const TKey *p_keys, uint32_t p_num_keys, int32_t p_index = -1) {
  296. keys = p_keys;
  297. num_keys = p_num_keys;
  298. index = p_index;
  299. }
  300. _FORCE_INLINE_ Iterator() {}
  301. _FORCE_INLINE_ Iterator(const Iterator &p_it) {
  302. keys = p_it.keys;
  303. num_keys = p_it.num_keys;
  304. index = p_it.index;
  305. }
  306. _FORCE_INLINE_ void operator=(const Iterator &p_it) {
  307. keys = p_it.keys;
  308. num_keys = p_it.num_keys;
  309. index = p_it.index;
  310. }
  311. private:
  312. const TKey *keys = nullptr;
  313. uint32_t num_keys = 0;
  314. int32_t index = -1;
  315. };
  316. _FORCE_INLINE_ Iterator begin() const {
  317. return num_elements ? Iterator(keys, num_elements, 0) : Iterator();
  318. }
  319. _FORCE_INLINE_ Iterator end() const {
  320. return Iterator();
  321. }
  322. _FORCE_INLINE_ Iterator last() const {
  323. if (num_elements == 0) {
  324. return Iterator();
  325. }
  326. return Iterator(keys, num_elements, num_elements - 1);
  327. }
  328. _FORCE_INLINE_ Iterator find(const TKey &p_key) const {
  329. uint32_t pos = 0;
  330. bool exists = _lookup_pos(p_key, pos);
  331. if (!exists) {
  332. return end();
  333. }
  334. return Iterator(keys, num_elements, pos);
  335. }
  336. _FORCE_INLINE_ void remove(const Iterator &p_iter) {
  337. if (p_iter) {
  338. erase(*p_iter);
  339. }
  340. }
  341. /* Insert */
  342. Iterator insert(const TKey &p_key) {
  343. uint32_t pos = _insert(p_key);
  344. return Iterator(keys, num_elements, pos);
  345. }
  346. /* Constructors */
  347. HashSet(const HashSet &p_other) {
  348. _init_from(p_other);
  349. }
  350. void operator=(const HashSet &p_other) {
  351. if (this == &p_other) {
  352. return; // Ignore self assignment.
  353. }
  354. clear();
  355. if (keys != nullptr) {
  356. Memory::free_static(keys);
  357. Memory::free_static(key_to_hash);
  358. Memory::free_static(hash_to_key);
  359. Memory::free_static(hashes);
  360. keys = nullptr;
  361. hashes = nullptr;
  362. hash_to_key = nullptr;
  363. key_to_hash = nullptr;
  364. }
  365. _init_from(p_other);
  366. }
  367. bool operator==(const HashSet &p_other) const {
  368. if (num_elements != p_other.num_elements) {
  369. return false;
  370. }
  371. for (uint32_t i = 0; i < num_elements; i++) {
  372. if (!p_other.has(keys[i])) {
  373. return false;
  374. }
  375. }
  376. return true;
  377. }
  378. bool operator!=(const HashSet &p_other) const {
  379. return !(*this == p_other);
  380. }
  381. HashSet(uint32_t p_initial_capacity) {
  382. // Capacity can't be 0.
  383. capacity_index = 0;
  384. reserve(p_initial_capacity);
  385. }
  386. HashSet() {
  387. capacity_index = MIN_CAPACITY_INDEX;
  388. }
  389. HashSet(std::initializer_list<TKey> p_init) {
  390. reserve(p_init.size());
  391. for (const TKey &E : p_init) {
  392. insert(E);
  393. }
  394. }
  395. void reset() {
  396. clear();
  397. if (keys != nullptr) {
  398. Memory::free_static(keys);
  399. Memory::free_static(key_to_hash);
  400. Memory::free_static(hash_to_key);
  401. Memory::free_static(hashes);
  402. keys = nullptr;
  403. hashes = nullptr;
  404. hash_to_key = nullptr;
  405. key_to_hash = nullptr;
  406. }
  407. capacity_index = MIN_CAPACITY_INDEX;
  408. }
  409. ~HashSet() {
  410. clear();
  411. if (keys != nullptr) {
  412. Memory::free_static(keys);
  413. Memory::free_static(key_to_hash);
  414. Memory::free_static(hash_to_key);
  415. Memory::free_static(hashes);
  416. }
  417. }
  418. };