oa_hash_map.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**************************************************************************/
  2. /* oa_hash_map.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. #ifndef OA_HASH_MAP_H
  31. #define OA_HASH_MAP_H
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/memory.h"
  34. #include "core/templates/hashfuncs.h"
  35. #include "core/templates/pair.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. * Only used keys and values are constructed. For free positions there's space
  47. * in the arrays for each, but that memory is kept uninitialized.
  48. *
  49. * The assignment operator copy the pairs from one map to the other.
  50. */
  51. template <typename TKey, typename TValue,
  52. typename Hasher = HashMapHasherDefault,
  53. typename Comparator = HashMapComparatorDefault<TKey>>
  54. class OAHashMap {
  55. private:
  56. TValue *values = nullptr;
  57. TKey *keys = nullptr;
  58. uint32_t *hashes = nullptr;
  59. uint32_t capacity = 0;
  60. uint32_t num_elements = 0;
  61. static const uint32_t EMPTY_HASH = 0;
  62. _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const {
  63. uint32_t hash = Hasher::hash(p_key);
  64. if (hash == EMPTY_HASH) {
  65. hash = EMPTY_HASH + 1;
  66. }
  67. return hash;
  68. }
  69. _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) const {
  70. uint32_t original_pos = p_hash % capacity;
  71. return (p_pos - original_pos + capacity) % capacity;
  72. }
  73. _FORCE_INLINE_ void _construct(uint32_t p_pos, uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
  74. memnew_placement(&keys[p_pos], TKey(p_key));
  75. memnew_placement(&values[p_pos], TValue(p_value));
  76. hashes[p_pos] = p_hash;
  77. num_elements++;
  78. }
  79. bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
  80. uint32_t hash = _hash(p_key);
  81. uint32_t pos = hash % capacity;
  82. uint32_t distance = 0;
  83. while (true) {
  84. if (hashes[pos] == EMPTY_HASH) {
  85. return false;
  86. }
  87. if (distance > _get_probe_length(pos, hashes[pos])) {
  88. return false;
  89. }
  90. if (hashes[pos] == hash && Comparator::compare(keys[pos], p_key)) {
  91. r_pos = pos;
  92. return true;
  93. }
  94. pos = (pos + 1) % capacity;
  95. distance++;
  96. }
  97. }
  98. void _insert_with_hash(uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
  99. uint32_t hash = p_hash;
  100. uint32_t distance = 0;
  101. uint32_t pos = hash % capacity;
  102. TKey key = p_key;
  103. TValue value = p_value;
  104. while (true) {
  105. if (hashes[pos] == EMPTY_HASH) {
  106. _construct(pos, hash, key, value);
  107. return;
  108. }
  109. // not an empty slot, let's check the probing length of the existing one
  110. uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos]);
  111. if (existing_probe_len < distance) {
  112. SWAP(hash, hashes[pos]);
  113. SWAP(key, keys[pos]);
  114. SWAP(value, values[pos]);
  115. distance = existing_probe_len;
  116. }
  117. pos = (pos + 1) % capacity;
  118. distance++;
  119. }
  120. }
  121. void _resize_and_rehash(uint32_t p_new_capacity) {
  122. uint32_t old_capacity = capacity;
  123. // Capacity can't be 0.
  124. capacity = MAX(1u, p_new_capacity);
  125. TKey *old_keys = keys;
  126. TValue *old_values = values;
  127. uint32_t *old_hashes = hashes;
  128. num_elements = 0;
  129. keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
  130. values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity));
  131. hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  132. for (uint32_t i = 0; i < capacity; i++) {
  133. hashes[i] = 0;
  134. }
  135. if (old_capacity == 0) {
  136. // Nothing to do.
  137. return;
  138. }
  139. for (uint32_t i = 0; i < old_capacity; i++) {
  140. if (old_hashes[i] == EMPTY_HASH) {
  141. continue;
  142. }
  143. _insert_with_hash(old_hashes[i], old_keys[i], old_values[i]);
  144. old_keys[i].~TKey();
  145. old_values[i].~TValue();
  146. }
  147. Memory::free_static(old_keys);
  148. Memory::free_static(old_values);
  149. Memory::free_static(old_hashes);
  150. }
  151. void _resize_and_rehash() {
  152. _resize_and_rehash(capacity * 2);
  153. }
  154. public:
  155. _FORCE_INLINE_ uint32_t get_capacity() const { return capacity; }
  156. _FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; }
  157. bool is_empty() const {
  158. return num_elements == 0;
  159. }
  160. void clear() {
  161. for (uint32_t i = 0; i < capacity; i++) {
  162. if (hashes[i] == EMPTY_HASH) {
  163. continue;
  164. }
  165. hashes[i] = EMPTY_HASH;
  166. values[i].~TValue();
  167. keys[i].~TKey();
  168. }
  169. num_elements = 0;
  170. }
  171. void insert(const TKey &p_key, const TValue &p_value) {
  172. if (num_elements + 1 > 0.9 * capacity) {
  173. _resize_and_rehash();
  174. }
  175. uint32_t hash = _hash(p_key);
  176. _insert_with_hash(hash, p_key, p_value);
  177. }
  178. void set(const TKey &p_key, const TValue &p_data) {
  179. uint32_t pos = 0;
  180. bool exists = _lookup_pos(p_key, pos);
  181. if (exists) {
  182. values[pos] = p_data;
  183. } else {
  184. insert(p_key, p_data);
  185. }
  186. }
  187. /**
  188. * returns true if the value was found, false otherwise.
  189. *
  190. * if r_data is not nullptr then the value will be written to the object
  191. * it points to.
  192. */
  193. bool lookup(const TKey &p_key, TValue &r_data) const {
  194. uint32_t pos = 0;
  195. bool exists = _lookup_pos(p_key, pos);
  196. if (exists) {
  197. r_data = values[pos];
  198. return true;
  199. }
  200. return false;
  201. }
  202. const TValue *lookup_ptr(const TKey &p_key) const {
  203. uint32_t pos = 0;
  204. bool exists = _lookup_pos(p_key, pos);
  205. if (exists) {
  206. return &values[pos];
  207. }
  208. return nullptr;
  209. }
  210. TValue *lookup_ptr(const TKey &p_key) {
  211. uint32_t pos = 0;
  212. bool exists = _lookup_pos(p_key, pos);
  213. if (exists) {
  214. return &values[pos];
  215. }
  216. return nullptr;
  217. }
  218. _FORCE_INLINE_ bool has(const TKey &p_key) const {
  219. uint32_t _pos = 0;
  220. return _lookup_pos(p_key, _pos);
  221. }
  222. void remove(const TKey &p_key) {
  223. uint32_t pos = 0;
  224. bool exists = _lookup_pos(p_key, pos);
  225. if (!exists) {
  226. return;
  227. }
  228. uint32_t next_pos = (pos + 1) % capacity;
  229. while (hashes[next_pos] != EMPTY_HASH &&
  230. _get_probe_length(next_pos, hashes[next_pos]) != 0) {
  231. SWAP(hashes[next_pos], hashes[pos]);
  232. SWAP(keys[next_pos], keys[pos]);
  233. SWAP(values[next_pos], values[pos]);
  234. pos = next_pos;
  235. next_pos = (pos + 1) % capacity;
  236. }
  237. hashes[pos] = EMPTY_HASH;
  238. values[pos].~TValue();
  239. keys[pos].~TKey();
  240. num_elements--;
  241. }
  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
  245. * capacity.
  246. **/
  247. void reserve(uint32_t p_new_capacity) {
  248. ERR_FAIL_COND(p_new_capacity < capacity);
  249. _resize_and_rehash(p_new_capacity);
  250. }
  251. struct Iterator {
  252. bool valid;
  253. const TKey *key;
  254. TValue *value = nullptr;
  255. private:
  256. uint32_t pos;
  257. friend class OAHashMap;
  258. };
  259. Iterator iter() const {
  260. Iterator it;
  261. it.valid = true;
  262. it.pos = 0;
  263. return next_iter(it);
  264. }
  265. Iterator next_iter(const Iterator &p_iter) const {
  266. if (!p_iter.valid) {
  267. return p_iter;
  268. }
  269. Iterator it;
  270. it.valid = false;
  271. it.pos = p_iter.pos;
  272. it.key = nullptr;
  273. it.value = nullptr;
  274. for (uint32_t i = it.pos; i < capacity; i++) {
  275. it.pos = i + 1;
  276. if (hashes[i] == EMPTY_HASH) {
  277. continue;
  278. }
  279. it.valid = true;
  280. it.key = &keys[i];
  281. it.value = &values[i];
  282. return it;
  283. }
  284. return it;
  285. }
  286. OAHashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
  287. reserve(p_init.size());
  288. for (const KeyValue<TKey, TValue> &E : p_init) {
  289. set(E.key, E.value);
  290. }
  291. }
  292. OAHashMap(const OAHashMap &p_other) {
  293. (*this) = p_other;
  294. }
  295. void operator=(const OAHashMap &p_other) {
  296. if (capacity != 0) {
  297. clear();
  298. }
  299. _resize_and_rehash(p_other.capacity);
  300. for (Iterator it = p_other.iter(); it.valid; it = p_other.next_iter(it)) {
  301. set(*it.key, *it.value);
  302. }
  303. }
  304. OAHashMap(uint32_t p_initial_capacity = 64) {
  305. // Capacity can't be 0.
  306. capacity = MAX(1u, p_initial_capacity);
  307. keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
  308. values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity));
  309. hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
  310. for (uint32_t i = 0; i < capacity; i++) {
  311. hashes[i] = EMPTY_HASH;
  312. }
  313. }
  314. ~OAHashMap() {
  315. for (uint32_t i = 0; i < capacity; i++) {
  316. if (hashes[i] == EMPTY_HASH) {
  317. continue;
  318. }
  319. values[i].~TValue();
  320. keys[i].~TKey();
  321. }
  322. Memory::free_static(keys);
  323. Memory::free_static(values);
  324. Memory::free_static(hashes);
  325. }
  326. };
  327. #endif // OA_HASH_MAP_H