oa_hash_map.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 robinhood hashing.
  38. * Robinhood 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.
  41. *
  42. * The entries are stored inplace, so huge keys or values might fill cache lines
  43. * a lot faster.
  44. */
  45. template <class TKey, class TValue,
  46. class Hasher = HashMapHasherDefault,
  47. class Comparator = HashMapComparatorDefault<TKey> >
  48. class OAHashMap {
  49. private:
  50. TValue *values;
  51. TKey *keys;
  52. uint32_t *hashes;
  53. uint32_t capacity;
  54. uint32_t num_elements;
  55. static const uint32_t EMPTY_HASH = 0;
  56. static const uint32_t DELETED_HASH_BIT = 1 << 31;
  57. _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) {
  58. uint32_t hash = Hasher::hash(p_key);
  59. if (hash == EMPTY_HASH) {
  60. hash = EMPTY_HASH + 1;
  61. } else if (hash & DELETED_HASH_BIT) {
  62. hash &= ~DELETED_HASH_BIT;
  63. }
  64. return hash;
  65. }
  66. _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) {
  67. p_hash = p_hash & ~DELETED_HASH_BIT; // we don't care if it was deleted or not
  68. uint32_t original_pos = p_hash % capacity;
  69. return p_pos - original_pos;
  70. }
  71. _FORCE_INLINE_ void _construct(uint32_t p_pos, uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
  72. memnew_placement(&keys[p_pos], TKey(p_key));
  73. memnew_placement(&values[p_pos], TValue(p_value));
  74. hashes[p_pos] = p_hash;
  75. num_elements++;
  76. }
  77. bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) {
  78. uint32_t hash = _hash(p_key);
  79. uint32_t pos = hash % capacity;
  80. uint32_t distance = 0;
  81. while (42) {
  82. if (hashes[pos] == EMPTY_HASH) {
  83. return false;
  84. }
  85. if (distance > _get_probe_length(pos, hashes[pos])) {
  86. return false;
  87. }
  88. if (hashes[pos] == hash && Comparator::compare(keys[pos], p_key)) {
  89. r_pos = pos;
  90. return true;
  91. }
  92. pos = (pos + 1) % capacity;
  93. distance++;
  94. }
  95. }
  96. void _insert_with_hash(uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
  97. uint32_t hash = p_hash;
  98. uint32_t distance = 0;
  99. uint32_t pos = hash % capacity;
  100. TKey key = p_key;
  101. TValue value = p_value;
  102. while (42) {
  103. if (hashes[pos] == EMPTY_HASH) {
  104. _construct(pos, hash, key, value);
  105. return;
  106. }
  107. // not an empty slot, let's check the probing length of the existing one
  108. uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos]);
  109. if (existing_probe_len < distance) {
  110. if (hashes[pos] & DELETED_HASH_BIT) {
  111. // we found a place where we can fit in!
  112. _construct(pos, hash, key, value);
  113. return;
  114. }
  115. SWAP(hash, hashes[pos]);
  116. SWAP(key, keys[pos]);
  117. SWAP(value, values[pos]);
  118. distance = existing_probe_len;
  119. }
  120. pos = (pos + 1) % capacity;
  121. distance++;
  122. }
  123. }
  124. void _resize_and_rehash() {
  125. TKey *old_keys = keys;
  126. TValue *old_values = values;
  127. uint32_t *old_hashes = hashes;
  128. uint32_t old_capacity = capacity;
  129. capacity = old_capacity * 2;
  130. num_elements = 0;
  131. keys = memnew_arr(TKey, capacity);
  132. values = memnew_arr(TValue, capacity);
  133. hashes = memnew_arr(uint32_t, capacity);
  134. for (uint32_t i = 0; i < capacity; i++) {
  135. hashes[i] = 0;
  136. }
  137. for (uint32_t i = 0; i < old_capacity; i++) {
  138. if (old_hashes[i] == EMPTY_HASH) {
  139. continue;
  140. }
  141. if (old_hashes[i] & DELETED_HASH_BIT) {
  142. continue;
  143. }
  144. _insert_with_hash(old_hashes[i], old_keys[i], old_values[i]);
  145. }
  146. memdelete_arr(old_keys);
  147. memdelete_arr(old_values);
  148. memdelete_arr(old_hashes);
  149. }
  150. public:
  151. _FORCE_INLINE_ uint32_t get_capacity() const { return capacity; }
  152. _FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; }
  153. void insert(const TKey &p_key, const TValue &p_value) {
  154. if ((float)num_elements / (float)capacity > 0.9) {
  155. _resize_and_rehash();
  156. }
  157. uint32_t hash = _hash(p_key);
  158. _insert_with_hash(hash, p_key, p_value);
  159. }
  160. void set(const TKey &p_key, const TValue &p_data) {
  161. uint32_t pos = 0;
  162. bool exists = _lookup_pos(p_key, pos);
  163. if (exists) {
  164. values[pos].~TValue();
  165. memnew_placement(&values[pos], TValue(p_data));
  166. } else {
  167. insert(p_key, p_data);
  168. }
  169. }
  170. /**
  171. * returns true if the value was found, false otherwise.
  172. *
  173. * if r_data is not NULL then the value will be written to the object
  174. * it points to.
  175. */
  176. bool lookup(const TKey &p_key, TValue &r_data) {
  177. uint32_t pos = 0;
  178. bool exists = _lookup_pos(p_key, pos);
  179. if (exists) {
  180. r_data.~TValue();
  181. memnew_placement(&r_data, TValue(values[pos]));
  182. return true;
  183. }
  184. return false;
  185. }
  186. _FORCE_INLINE_ bool has(const TKey &p_key) {
  187. uint32_t _pos = 0;
  188. return _lookup_pos(p_key, _pos);
  189. }
  190. void remove(const TKey &p_key) {
  191. uint32_t pos = 0;
  192. bool exists = _lookup_pos(p_key, pos);
  193. if (!exists) {
  194. return;
  195. }
  196. hashes[pos] |= DELETED_HASH_BIT;
  197. values[pos].~TValue();
  198. keys[pos].~TKey();
  199. num_elements--;
  200. }
  201. struct Iterator {
  202. bool valid;
  203. const TKey *key;
  204. const TValue *value;
  205. private:
  206. uint32_t pos;
  207. friend class OAHashMap;
  208. };
  209. Iterator iter() const {
  210. Iterator it;
  211. it.valid = true;
  212. it.pos = 0;
  213. return next_iter(it);
  214. }
  215. Iterator next_iter(const Iterator &p_iter) const {
  216. if (!p_iter.valid) {
  217. return p_iter;
  218. }
  219. Iterator it;
  220. it.valid = false;
  221. it.pos = p_iter.pos;
  222. it.key = NULL;
  223. it.value = NULL;
  224. for (uint32_t i = it.pos; i < capacity; i++) {
  225. it.pos = i + 1;
  226. if (hashes[i] == EMPTY_HASH) {
  227. continue;
  228. }
  229. if (hashes[i] & DELETED_HASH_BIT) {
  230. continue;
  231. }
  232. it.valid = true;
  233. it.key = &keys[i];
  234. it.value = &values[i];
  235. return it;
  236. }
  237. return it;
  238. }
  239. OAHashMap(uint32_t p_initial_capacity = 64) {
  240. capacity = p_initial_capacity;
  241. num_elements = 0;
  242. keys = memnew_arr(TKey, p_initial_capacity);
  243. values = memnew_arr(TValue, p_initial_capacity);
  244. hashes = memnew_arr(uint32_t, p_initial_capacity);
  245. for (uint32_t i = 0; i < p_initial_capacity; i++) {
  246. hashes[i] = 0;
  247. }
  248. }
  249. ~OAHashMap() {
  250. memdelete_arr(keys);
  251. memdelete_arr(values);
  252. memdelete_arr(hashes);
  253. }
  254. };
  255. #endif