ProbingHashMap.hpp 9.2 KB

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