ProbingHashMap.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #ifndef CORE_PROBING_HPPASHMAP_HPP
  2. #define CORE_PROBING_HPPASHMAP_HPP
  3. #include "data/List.hpp"
  4. #include "utils/AlignedData.hpp"
  5. #include "utils/ArrayString.hpp"
  6. #include "utils/HashCode.hpp"
  7. #include "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. int 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(int 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(int minCapacity) {
  131. if(minCapacity <= keys.getLength()) {
  132. return Error::NONE;
  133. }
  134. ProbingHashMap<K, V> map;
  135. int l = Core::Math::max(1 << Math::roundUpLog2(minCapacity), 8);
  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(int 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. CORE_RETURN_ERROR(rehash(entries * 2 + 1));
  155. int index = searchSlot(key);
  156. if(index < 0) {
  157. return Error::CAPACITY_REACHED;
  158. } else if(keys[index] == key) {
  159. return Error::EXISTING_KEY;
  160. }
  161. keys[index] = key;
  162. v = new(values + index) V(Core::forward<Args>(args)...);
  163. entries++;
  164. return Error::NONE;
  165. }
  166. template<typename VA>
  167. check_return Error put(V*& v, const K& key, VA&& value) {
  168. if(key == emptyValue<K>()) {
  169. return Error::INVALID_ARGUMENT;
  170. }
  171. CORE_RETURN_ERROR(rehash(entries * 2 + 1));
  172. int index = searchSlot(key);
  173. if(index < 0) {
  174. return Error::CAPACITY_REACHED;
  175. }
  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. v = reinterpret_cast<V*>(values + index);
  184. return Error::NONE;
  185. }
  186. template<typename VA>
  187. check_return Error add(const K& key, VA&& value) {
  188. V* v = nullptr;
  189. return put(v, key, Core::forward<VA>(value));
  190. }
  191. const V* search(const K& key) const {
  192. return searchValue<const V>(key);
  193. }
  194. V* search(const K& key) {
  195. return searchValue<V>(key);
  196. }
  197. bool contains(const K& key) const {
  198. return search(key) != nullptr;
  199. }
  200. ProbingHashMap& clear() {
  201. ProbingHashMap<K, V> map;
  202. swap(map);
  203. return *this;
  204. }
  205. ConstKeyIteratorAdapter getKeys() const {
  206. return {*this};
  207. }
  208. ValueIteratorAdapter getValues() {
  209. return {*this};
  210. }
  211. ConstValueIteratorAdapter getValues() const {
  212. return {*this};
  213. }
  214. EntryIterator begin() {
  215. return {keys.begin(), keys.end(), values};
  216. }
  217. EntryIterator end() {
  218. return {keys.end(), keys.end(), nullptr};
  219. }
  220. ConstEntryIterator begin() const {
  221. return {keys.begin(), keys.end(), values};
  222. }
  223. ConstEntryIterator end() const {
  224. return {keys.end(), keys.end(), nullptr};
  225. }
  226. template<typename String>
  227. check_return Error toString(String& s) const {
  228. return Core::toString(s, *this);
  229. }
  230. void swap(ProbingHashMap& o) {
  231. Core::swap(o.keys, keys);
  232. Core::swap(o.values, values);
  233. Core::swap(o.entries, entries);
  234. }
  235. private:
  236. int searchSlot(const K& key) const {
  237. int baseHash = static_cast<int>(hashCode(key) * 514685581u);
  238. int end = keys.getLength() - 1;
  239. for(int i = 0; i <= end; i++) {
  240. int hash = (baseHash + i) & end;
  241. if(keys[hash] == emptyValue<K>() || keys[hash] == key) {
  242. return hash;
  243. }
  244. }
  245. return -1;
  246. }
  247. template<typename Value>
  248. Value* searchValue(const K& key) const {
  249. int baseHash = static_cast<int>(hashCode(key) * 514685581u);
  250. int end = keys.getLength() - 1;
  251. for(int i = 0; i <= end; i++) [[unlikely]] {
  252. int hash = (baseHash + i) & end;
  253. if(keys[hash] == key) [[likely]] {
  254. return values + hash;
  255. } else if(keys[hash] == emptyValue<K>()) {
  256. return nullptr;
  257. }
  258. }
  259. return nullptr;
  260. }
  261. };
  262. }
  263. #endif