ProbingHashMap.hpp 9.7 KB

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