ProbingHashMap.hpp 9.4 KB

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