ProbingHashMap.hpp 9.7 KB

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