hash_set.hpp 14 KB

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