ProbingHashMap.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef CORE_PROBING_HASHMAP_H
  2. #define CORE_PROBING_HASHMAP_H
  3. #include "data/List.h"
  4. #include "utils/ArrayString.h"
  5. #include "utils/HashCode.h"
  6. #include "utils/Logger.h"
  7. namespace Core {
  8. template<typename K, typename V>
  9. struct ProbingHashMap final {
  10. template<typename Value>
  11. class Node final {
  12. friend ProbingHashMap;
  13. friend List<Node>;
  14. K key;
  15. public:
  16. Value& value;
  17. const K& getKey() const {
  18. return key;
  19. }
  20. template<typename String>
  21. check_return Error toString(String& s) const {
  22. CORE_RETURN_ERROR(s.append(key));
  23. CORE_RETURN_ERROR(s.append(" = "));
  24. return s.append(value);
  25. }
  26. private:
  27. Node(const K& key_, Value& value_) : key(key_), value(value_) {
  28. }
  29. };
  30. private:
  31. template<typename Value, typename R, R (*A)(const K&, Value&)>
  32. class Iterator final {
  33. const K* currentKey;
  34. const K* endKey;
  35. Value* currentValue;
  36. public:
  37. Iterator(const K* key, const K* endKey_, Value* value)
  38. : currentKey(key), endKey(endKey_), currentValue(value) {
  39. skip();
  40. }
  41. Iterator& operator++() {
  42. ++currentKey;
  43. ++currentValue;
  44. skip();
  45. return *this;
  46. }
  47. bool operator!=(const Iterator& other) const {
  48. return currentKey != other.currentKey;
  49. }
  50. R operator*() const {
  51. return A(*currentKey, *currentValue);
  52. }
  53. private:
  54. void skip() {
  55. while(currentKey != endKey && *currentKey == emptyValue<K>()) {
  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. struct alignas(V) AlignedValue final {
  99. char data[sizeof(V)];
  100. };
  101. List<K> keys;
  102. V* values;
  103. int entries;
  104. public:
  105. ProbingHashMap() : values(nullptr), entries(0) {
  106. }
  107. ProbingHashMap(const ProbingHashMap& other) = delete;
  108. ProbingHashMap(ProbingHashMap&& other) : ProbingHashMap() {
  109. swap(other);
  110. }
  111. ~ProbingHashMap() {
  112. for(int i = 0; i < keys.getLength(); i++) {
  113. if(keys[i] != emptyValue<K>()) {
  114. values[i].~V();
  115. }
  116. }
  117. delete[] reinterpret_cast<AlignedValue*>(values);
  118. }
  119. ProbingHashMap& operator=(const ProbingHashMap& other) = delete;
  120. ProbingHashMap& operator=(ProbingHashMap&& other) {
  121. swap(other);
  122. return *this;
  123. }
  124. check_return Error copyFrom(const ProbingHashMap& other) {
  125. ProbingHashMap copy;
  126. for(const auto& e : other) {
  127. CORE_RETURN_ERROR(copy.add(e.getKey(), e.value));
  128. }
  129. swap(copy);
  130. return Error::NONE;
  131. }
  132. check_return Error rehash(int minCapacity) {
  133. if(minCapacity <= keys.getLength()) {
  134. return Error::NONE;
  135. }
  136. ProbingHashMap<K, V> map;
  137. int l = Core::Math::max(1 << Math::roundUpLog2(minCapacity), 8);
  138. CORE_RETURN_ERROR(map.keys.resize(l, emptyValue<K>()));
  139. map.values = reinterpret_cast<V*>(new AlignedValue[l]);
  140. if(map.values == nullptr) {
  141. return Error::OUT_OF_MEMORY;
  142. }
  143. for(int i = 0; i < keys.getLength(); i++) {
  144. if(keys[i] != emptyValue<K>()) {
  145. CORE_RETURN_ERROR(map.add(keys[i], values[i]));
  146. }
  147. }
  148. swap(map);
  149. return Error::NONE;
  150. }
  151. template<typename... Args>
  152. check_return Error tryEmplace(V*& v, const K& key, Args&&... args) {
  153. if(key == emptyValue<K>()) {
  154. return Error::INVALID_ARGUMENT;
  155. }
  156. CORE_RETURN_ERROR(rehash(entries * 2 + 1));
  157. int index = searchSlot(key);
  158. if(index < 0) {
  159. return Error::CAPACITY_REACHED;
  160. } else if(keys[index] == key) {
  161. return Error::EXISTING_KEY;
  162. }
  163. keys[index] = key;
  164. v = new(values + index) V(Core::forward<Args>(args)...);
  165. entries++;
  166. return Error::NONE;
  167. }
  168. template<typename VA>
  169. check_return Error put(V*& v, const K& key, VA&& value) {
  170. if(key == emptyValue<K>()) {
  171. return Error::INVALID_ARGUMENT;
  172. }
  173. CORE_RETURN_ERROR(rehash(entries * 2 + 1));
  174. int index = searchSlot(key);
  175. if(index < 0) {
  176. return Error::CAPACITY_REACHED;
  177. }
  178. if(keys[index] == key) {
  179. values[index] = Core::forward<VA>(value);
  180. } else {
  181. new(values + index) V(Core::forward<VA>(value));
  182. entries++;
  183. }
  184. keys[index] = key;
  185. v = reinterpret_cast<V*>(values + index);
  186. return Error::NONE;
  187. }
  188. template<typename VA>
  189. check_return Error add(const K& key, VA&& value) {
  190. V* v = nullptr;
  191. return put(v, key, Core::forward<VA>(value));
  192. }
  193. const V* search(const K& key) const {
  194. return searchValue<const V>(key);
  195. }
  196. V* search(const K& key) {
  197. return searchValue<V>(key);
  198. }
  199. bool contains(const K& key) const {
  200. return search(key) != nullptr;
  201. }
  202. ProbingHashMap& clear() {
  203. ProbingHashMap<K, V> map;
  204. swap(map);
  205. return *this;
  206. }
  207. ConstKeyIteratorAdapter getKeys() const {
  208. return {*this};
  209. }
  210. ValueIteratorAdapter getValues() {
  211. return {*this};
  212. }
  213. ConstValueIteratorAdapter getValues() const {
  214. return {*this};
  215. }
  216. EntryIterator begin() {
  217. return {keys.begin(), keys.end(), values};
  218. }
  219. EntryIterator end() {
  220. return {keys.end(), keys.end(), nullptr};
  221. }
  222. ConstEntryIterator begin() const {
  223. return {keys.begin(), keys.end(), values};
  224. }
  225. ConstEntryIterator end() const {
  226. return {keys.end(), keys.end(), nullptr};
  227. }
  228. template<typename String>
  229. check_return Error toString(String& s) const {
  230. return Core::toString(s, *this);
  231. }
  232. void swap(ProbingHashMap& o) {
  233. Core::swap(o.keys, keys);
  234. Core::swap(o.values, values);
  235. Core::swap(o.entries, entries);
  236. }
  237. private:
  238. int searchSlot(const K& key) const {
  239. int baseHash = static_cast<int>(hashCode(key) * 514685581u);
  240. int end = keys.getLength() - 1;
  241. for(int i = 0; i <= end; i++) {
  242. int hash = (baseHash + i) & end;
  243. if(keys[hash] == emptyValue<K>() || keys[hash] == key) {
  244. return hash;
  245. }
  246. }
  247. return -1;
  248. }
  249. template<typename Value>
  250. Value* searchValue(const K& key) const {
  251. int baseHash = static_cast<int>(hashCode(key) * 514685581u);
  252. int end = keys.getLength() - 1;
  253. for(int i = 0; i <= end; i++) [[unlikely]] {
  254. int hash = (baseHash + i) & end;
  255. if(keys[hash] == key) [[likely]] {
  256. return values + hash;
  257. } else if(keys[hash] == emptyValue<K>()) {
  258. return nullptr;
  259. }
  260. }
  261. return nullptr;
  262. }
  263. };
  264. }
  265. #endif