ProbingHashMap.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef CORE_PROBING_HPPASHMAP_HPP
  2. #define CORE_PROBING_HPPASHMAP_HPP
  3. #include "core/data/List.hpp"
  4. #include "core/utils/AlignedData.hpp"
  5. #include "core/utils/ArrayString.hpp"
  6. #include "core/utils/HashCode.hpp"
  7. #include "core/utils/Logger.hpp"
  8. namespace Core {
  9. template<typename K, typename V>
  10. struct ProbingHashMap final {
  11. template<typename Value>
  12. class Node final {
  13. friend ProbingHashMap;
  14. friend List<Node>;
  15. K key;
  16. public:
  17. Value& value;
  18. const K& getKey() const {
  19. return key;
  20. }
  21. template<typename String>
  22. check_return Error toString(String& s) const {
  23. CORE_RETURN_ERROR(s.append(key));
  24. CORE_RETURN_ERROR(s.append(" = "));
  25. return s.append(value);
  26. }
  27. private:
  28. Node(const K& key_, Value& value_) : key(key_), value(value_) {
  29. }
  30. };
  31. private:
  32. template<typename Value, typename R, R (*A)(const K&, Value&)>
  33. class Iterator final {
  34. const K* currentKey;
  35. const K* endKey;
  36. Value* currentValue;
  37. public:
  38. Iterator(const K* key, const K* endKey_, Value* value)
  39. : currentKey(key), endKey(endKey_), currentValue(value) {
  40. skip();
  41. }
  42. Iterator& operator++() {
  43. ++currentKey;
  44. ++currentValue;
  45. skip();
  46. return *this;
  47. }
  48. bool operator!=(const Iterator& other) const {
  49. return currentKey != other.currentKey;
  50. }
  51. R operator*() const {
  52. return A(*currentKey, *currentValue);
  53. }
  54. private:
  55. void skip() {
  56. while(currentKey != endKey && *currentKey == emptyValue<K>()) {
  57. ++currentKey;
  58. ++currentValue;
  59. }
  60. }
  61. };
  62. template<typename Value>
  63. static Node<Value> access(const K& key, Value& value) {
  64. return Node<Value>(key, value);
  65. }
  66. template<typename Value>
  67. static Value& accessValue(const K&, Value& value) {
  68. return value;
  69. }
  70. static const K& accessKey(const K& key, const V&) {
  71. return key;
  72. }
  73. template<typename Value>
  74. using BaseEntryIterator = Iterator<Value, Node<Value>, access<Value>>;
  75. using EntryIterator = BaseEntryIterator<V>;
  76. using ConstEntryIterator = BaseEntryIterator<const V>;
  77. template<typename Value>
  78. using BaseValueIterator = Iterator<Value, Value&, accessValue<Value>>;
  79. using ValueIterator = BaseValueIterator<V>;
  80. using ConstValueIterator = BaseValueIterator<const V>;
  81. using ConstKeyIterator = Iterator<const V, const K&, accessKey>;
  82. template<typename M, typename I>
  83. struct IteratorAdapter final {
  84. M& map;
  85. I begin() const {
  86. return {map.keys.begin(), map.keys.end(), map.values};
  87. }
  88. I end() const {
  89. return {map.keys.end(), map.keys.end(), nullptr};
  90. }
  91. };
  92. using ValueIteratorAdapter =
  93. IteratorAdapter<ProbingHashMap, ValueIterator>;
  94. using ConstValueIteratorAdapter =
  95. IteratorAdapter<const ProbingHashMap, ConstValueIterator>;
  96. using ConstKeyIteratorAdapter =
  97. IteratorAdapter<const ProbingHashMap, ConstKeyIterator>;
  98. private:
  99. List<K> keys;
  100. V* values;
  101. i64 entries;
  102. public:
  103. ProbingHashMap() : keys(), values(nullptr), entries(0) {
  104. }
  105. ProbingHashMap(const ProbingHashMap& other) = delete;
  106. ProbingHashMap(ProbingHashMap&& other) : ProbingHashMap() {
  107. swap(other);
  108. }
  109. ~ProbingHashMap() {
  110. for(i64 i = 0; i < keys.getLength(); i++) {
  111. if(keys[i] != emptyValue<K>()) {
  112. values[i].~V();
  113. }
  114. }
  115. delete[] reinterpret_cast<AlignedType<V>*>(values);
  116. }
  117. ProbingHashMap& operator=(const ProbingHashMap& other) = delete;
  118. ProbingHashMap& operator=(ProbingHashMap&& other) {
  119. swap(other);
  120. return *this;
  121. }
  122. check_return Error copyFrom(const ProbingHashMap& other) {
  123. ProbingHashMap copy;
  124. for(const auto& e : other) {
  125. CORE_RETURN_ERROR(copy.add(e.getKey(), e.value));
  126. }
  127. swap(copy);
  128. return Error::NONE;
  129. }
  130. check_return Error rehash(i64 minCapacity) {
  131. if(minCapacity <= keys.getLength()) {
  132. return Error::NONE;
  133. }
  134. ProbingHashMap<K, V> map;
  135. i64 l = 1l << Math::roundUpLog2(Core::Math::max(minCapacity, 8l));
  136. CORE_RETURN_ERROR(map.keys.resize(l, emptyValue<K>()));
  137. map.values = reinterpret_cast<V*>(new AlignedType<V>[l]);
  138. if(map.values == nullptr) {
  139. return Error::OUT_OF_MEMORY;
  140. }
  141. for(i64 i = 0; i < keys.getLength(); i++) {
  142. if(keys[i] != emptyValue<K>()) {
  143. CORE_RETURN_ERROR(map.add(keys[i], values[i]));
  144. }
  145. }
  146. swap(map);
  147. return Error::NONE;
  148. }
  149. template<typename... Args>
  150. check_return Error tryEmplace(V*& v, const K& key, Args&&... args) {
  151. if(key == emptyValue<K>()) {
  152. return Error::INVALID_ARGUMENT;
  153. }
  154. i64 index = 0;
  155. CORE_RETURN_ERROR(searchSlot(index, key));
  156. if(keys[index] == key) {
  157. return Error::EXISTING_KEY;
  158. }
  159. keys[index] = key;
  160. v = new(values + index) V(Core::forward<Args>(args)...);
  161. entries++;
  162. return Error::NONE;
  163. }
  164. template<typename VA>
  165. check_return Error put(V*& v, const K& key, VA&& value) {
  166. if(key == emptyValue<K>()) {
  167. return Error::INVALID_ARGUMENT;
  168. }
  169. i64 index = 0;
  170. CORE_RETURN_ERROR(searchSlot(index, key));
  171. if(keys[index] == key) {
  172. values[index] = Core::forward<VA>(value);
  173. } else {
  174. new(values + index) V(Core::forward<VA>(value));
  175. entries++;
  176. }
  177. keys[index] = key;
  178. v = reinterpret_cast<V*>(values + index);
  179. return Error::NONE;
  180. }
  181. template<typename VA>
  182. check_return Error add(const K& key, VA&& value) {
  183. V* v = nullptr;
  184. return put(v, key, Core::forward<VA>(value));
  185. }
  186. const V* search(const K& key) const {
  187. return searchValue<const V>(key);
  188. }
  189. V* search(const K& key) {
  190. return searchValue<V>(key);
  191. }
  192. bool contains(const K& key) const {
  193. return search(key) != nullptr;
  194. }
  195. ProbingHashMap& clear() {
  196. ProbingHashMap<K, V> map;
  197. swap(map);
  198. return *this;
  199. }
  200. ConstKeyIteratorAdapter getKeys() const {
  201. return {*this};
  202. }
  203. ValueIteratorAdapter getValues() {
  204. return {*this};
  205. }
  206. ConstValueIteratorAdapter getValues() const {
  207. return {*this};
  208. }
  209. EntryIterator begin() {
  210. return {keys.begin(), keys.end(), values};
  211. }
  212. EntryIterator end() {
  213. return {keys.end(), keys.end(), nullptr};
  214. }
  215. ConstEntryIterator begin() const {
  216. return {keys.begin(), keys.end(), values};
  217. }
  218. ConstEntryIterator end() const {
  219. return {keys.end(), keys.end(), nullptr};
  220. }
  221. template<typename String>
  222. check_return Error toString(String& s) const {
  223. return Core::toString(s, *this);
  224. }
  225. void swap(ProbingHashMap& o) {
  226. Core::swap(o.keys, keys);
  227. Core::swap(o.values, values);
  228. Core::swap(o.entries, entries);
  229. }
  230. private:
  231. check_return Error searchSlot(i64& slot, const K& key) {
  232. i64 rehashFactor = 2;
  233. while(true) {
  234. CORE_RETURN_ERROR(rehash(entries * rehashFactor + 1));
  235. u64 baseHash = hashCode(key) * 514685581u;
  236. u64 end = static_cast<u64>(keys.getLength()) - 1;
  237. // rehash on bad clustering
  238. for(u64 i = 0; i <= 5; i++) {
  239. i64 hash = static_cast<i64>((baseHash + i) & end);
  240. if(keys[hash] == emptyValue<K>() || keys[hash] == key) {
  241. slot = hash;
  242. return Core::Error::NONE;
  243. }
  244. }
  245. rehashFactor *= 2;
  246. }
  247. }
  248. template<typename Value>
  249. Value* searchValue(const K& key) const {
  250. if(keys.getLength() != 0) {
  251. u64 baseHash = hashCode(key) * 514685581u;
  252. u64 end = static_cast<u64>(keys.getLength()) - 1;
  253. for(u32 i = 0; i <= end; i++) [[unlikely]] {
  254. i64 hash = static_cast<i64>((baseHash + i) & end);
  255. if(keys[hash] == key) [[likely]] {
  256. return values + hash;
  257. } else if(keys[hash] == emptyValue<K>()) {
  258. return nullptr;
  259. }
  260. }
  261. }
  262. return nullptr;
  263. }
  264. };
  265. }
  266. #endif