oa_hash_map.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*************************************************************************/
  2. /* oa_hash_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 OA_HASH_MAP_H
  31. #define OA_HASH_MAP_H
  32. #include "core/hashfuncs.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/os/copymem.h"
  35. #include "core/os/memory.h"
  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. * The entries are stored inplace, so huge keys or values might fill cache lines
  44. * a lot faster.
  45. */
  46. template <class TKey, class TValue,
  47. class Hasher = HashMapHasherDefault,
  48. class Comparator = HashMapComparatorDefault<TKey>>
  49. class OAHashMap {
  50. private:
  51. TValue *values;
  52. TKey *keys;
  53. uint32_t *hashes;
  54. uint32_t capacity;
  55. uint32_t num_elements = 0;
  56. static const uint32_t EMPTY_HASH = 0;
  57. _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const {
  58. uint32_t hash = Hasher::hash(p_key);
  59. if (hash == EMPTY_HASH) {
  60. hash = EMPTY_HASH + 1;
  61. }
  62. return hash;
  63. }
  64. _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) const {
  65. uint32_t original_pos = p_hash % capacity;
  66. return (p_pos - original_pos + capacity) % capacity;
  67. }
  68. _FORCE_INLINE_ void _construct(uint32_t p_pos, uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
  69. memnew_placement(&keys[p_pos], TKey(p_key));
  70. memnew_placement(&values[p_pos], TValue(p_value));
  71. hashes[p_pos] = p_hash;
  72. num_elements++;
  73. }
  74. bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
  75. uint32_t hash = _hash(p_key);
  76. uint32_t pos = hash % capacity;
  77. uint32_t distance = 0;
  78. while (true) {
  79. if (hashes[pos] == EMPTY_HASH) {
  80. return false;
  81. }
  82. if (distance > _get_probe_length(pos, hashes[pos])) {
  83. return false;
  84. }
  85. if (hashes[pos] == hash && Comparator::compare(keys[pos], p_key)) {
  86. r_pos = pos;
  87. return true;
  88. }
  89. pos = (pos + 1) % capacity;
  90. distance++;
  91. }
  92. }
  93. void _insert_with_hash(uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
  94. uint32_t hash = p_hash;
  95. uint32_t distance = 0;
  96. uint32_t pos = hash % capacity;
  97. TKey key = p_key;
  98. TValue value = p_value;
  99. while (true) {
  100. if (hashes[pos] == EMPTY_HASH) {
  101. _construct(pos, hash, key, value);
  102. return;
  103. }
  104. // not an empty slot, let's check the probing length of the existing one
  105. uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos]);
  106. if (existing_probe_len < distance) {
  107. SWAP(hash, hashes[pos]);
  108. SWAP(key, keys[pos]);
  109. SWAP(value, values[pos]);
  110. distance = existing_probe_len;
  111. }
  112. pos = (pos + 1) % capacity;
  113. distance++;
  114. }
  115. }
  116. void _resize_and_rehash(uint32_t p_new_capacity) {
  117. uint32_t old_capacity = capacity;
  118. capacity = p_new_capacity;
  119. TKey *old_keys = keys;
  120. TValue *old_values = values;
  121. uint32_t *old_hashes = hashes;
  122. num_elements = 0;
  123. keys = memnew_arr(TKey, capacity);
  124. values = memnew_arr(TValue, capacity);
  125. hashes = memnew_arr(uint32_t, capacity);
  126. for (uint32_t i = 0; i < capacity; i++) {
  127. hashes[i] = 0;
  128. }
  129. for (uint32_t i = 0; i < old_capacity; i++) {
  130. if (old_hashes[i] == EMPTY_HASH) {
  131. continue;
  132. }
  133. _insert_with_hash(old_hashes[i], old_keys[i], old_values[i]);
  134. }
  135. memdelete_arr(old_keys);
  136. memdelete_arr(old_values);
  137. memdelete_arr(old_hashes);
  138. }
  139. void _resize_and_rehash() {
  140. _resize_and_rehash(capacity * 2);
  141. }
  142. public:
  143. _FORCE_INLINE_ uint32_t get_capacity() const { return capacity; }
  144. _FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; }
  145. bool empty() const {
  146. return num_elements == 0;
  147. }
  148. void clear() {
  149. for (uint32_t i = 0; i < capacity; i++) {
  150. if (hashes[i] == EMPTY_HASH) {
  151. continue;
  152. }
  153. hashes[i] = EMPTY_HASH;
  154. values[i].~TValue();
  155. keys[i].~TKey();
  156. }
  157. num_elements = 0;
  158. }
  159. void insert(const TKey &p_key, const TValue &p_value) {
  160. if (num_elements + 1 > 0.9 * capacity) {
  161. _resize_and_rehash();
  162. }
  163. uint32_t hash = _hash(p_key);
  164. _insert_with_hash(hash, p_key, p_value);
  165. }
  166. void set(const TKey &p_key, const TValue &p_data) {
  167. uint32_t pos = 0;
  168. bool exists = _lookup_pos(p_key, pos);
  169. if (exists) {
  170. values[pos].~TValue();
  171. memnew_placement(&values[pos], TValue(p_data));
  172. } else {
  173. insert(p_key, p_data);
  174. }
  175. }
  176. /**
  177. * returns true if the value was found, false otherwise.
  178. *
  179. * if r_data is not nullptr then the value will be written to the object
  180. * it points to.
  181. */
  182. bool lookup(const TKey &p_key, TValue &r_data) const {
  183. uint32_t pos = 0;
  184. bool exists = _lookup_pos(p_key, pos);
  185. if (exists) {
  186. r_data.~TValue();
  187. memnew_placement(&r_data, TValue(values[pos]));
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * returns true if the value was found, false otherwise.
  194. *
  195. * if r_data is not nullptr then the value will be written to the object
  196. * it points to.
  197. */
  198. TValue *lookup_ptr(const TKey &p_key) const {
  199. uint32_t pos = 0;
  200. bool exists = _lookup_pos(p_key, pos);
  201. if (exists) {
  202. return &values[pos];
  203. }
  204. return nullptr;
  205. }
  206. _FORCE_INLINE_ bool has(const TKey &p_key) const {
  207. uint32_t _pos = 0;
  208. return _lookup_pos(p_key, _pos);
  209. }
  210. void remove(const TKey &p_key) {
  211. uint32_t pos = 0;
  212. bool exists = _lookup_pos(p_key, pos);
  213. if (!exists) {
  214. return;
  215. }
  216. uint32_t next_pos = (pos + 1) % capacity;
  217. while (hashes[next_pos] != EMPTY_HASH &&
  218. _get_probe_length(next_pos, hashes[next_pos]) != 0) {
  219. SWAP(hashes[next_pos], hashes[pos]);
  220. SWAP(keys[next_pos], keys[pos]);
  221. SWAP(values[next_pos], values[pos]);
  222. pos = next_pos;
  223. next_pos = (pos + 1) % capacity;
  224. }
  225. hashes[pos] = EMPTY_HASH;
  226. values[pos].~TValue();
  227. keys[pos].~TKey();
  228. num_elements--;
  229. }
  230. /**
  231. * reserves space for a number of elements, useful to avoid many resizes and rehashes
  232. * if adding a known (possibly large) number of elements at once, must be larger than old
  233. * capacity.
  234. **/
  235. void reserve(uint32_t p_new_capacity) {
  236. ERR_FAIL_COND(p_new_capacity < capacity);
  237. _resize_and_rehash(p_new_capacity);
  238. }
  239. struct Iterator {
  240. bool valid;
  241. const TKey *key;
  242. const TValue *value;
  243. private:
  244. uint32_t pos;
  245. friend class OAHashMap;
  246. };
  247. Iterator iter() const {
  248. Iterator it;
  249. it.valid = true;
  250. it.pos = 0;
  251. return next_iter(it);
  252. }
  253. Iterator next_iter(const Iterator &p_iter) const {
  254. if (!p_iter.valid) {
  255. return p_iter;
  256. }
  257. Iterator it;
  258. it.valid = false;
  259. it.pos = p_iter.pos;
  260. it.key = nullptr;
  261. it.value = nullptr;
  262. for (uint32_t i = it.pos; i < capacity; i++) {
  263. it.pos = i + 1;
  264. if (hashes[i] == EMPTY_HASH) {
  265. continue;
  266. }
  267. it.valid = true;
  268. it.key = &keys[i];
  269. it.value = &values[i];
  270. return it;
  271. }
  272. return it;
  273. }
  274. OAHashMap(const OAHashMap &) = delete; // Delete the copy constructor so we don't get unexpected copies and dangling pointers.
  275. OAHashMap &operator=(const OAHashMap &) = delete; // Same for assignment operator.
  276. OAHashMap(uint32_t p_initial_capacity = 64) {
  277. capacity = p_initial_capacity;
  278. keys = memnew_arr(TKey, p_initial_capacity);
  279. values = memnew_arr(TValue, p_initial_capacity);
  280. hashes = memnew_arr(uint32_t, p_initial_capacity);
  281. for (uint32_t i = 0; i < p_initial_capacity; i++) {
  282. hashes[i] = EMPTY_HASH;
  283. }
  284. }
  285. ~OAHashMap() {
  286. memdelete_arr(keys);
  287. memdelete_arr(values);
  288. memdelete_arr(hashes);
  289. }
  290. };
  291. #endif // OA_HASH_MAP_H