ProbingHashMap.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. class Node final {
  11. friend ProbingHashMap;
  12. friend List<Node>;
  13. K key;
  14. public:
  15. V& value;
  16. const K& getKey() const {
  17. return key;
  18. }
  19. template<typename String>
  20. check_return Error toString(String& s) const {
  21. CORE_RETURN_ERROR(s.append(key));
  22. CORE_RETURN_ERROR(s.append(" = "));
  23. return s.append(value);
  24. }
  25. private:
  26. Node(const K& key_, V& value_) : key(key_), value(value_) {
  27. }
  28. };
  29. private:
  30. /*template<typename N, typename I, typename R, R& (*A)(I&)>
  31. class Iterator final {
  32. N iterator;
  33. public:
  34. Iterator(N iterator_) : iterator(iterator_) {
  35. }
  36. Iterator& operator++() {
  37. ++iterator;
  38. return *this;
  39. }
  40. bool operator!=(const Iterator& other) const {
  41. return iterator != other.iterator;
  42. }
  43. R& operator*() const {
  44. return A(*iterator);
  45. }
  46. };
  47. template<typename R>
  48. static R& access(R& node) {
  49. return node;
  50. }
  51. template<typename I, typename R>
  52. static R& accessValue(I& node) {
  53. return node.value;
  54. }
  55. static const K& accessKey(const Node& node) {
  56. return node.getKey();
  57. }
  58. template<typename N, typename R>
  59. using BaseEntryIterator = Iterator<N, R, R, access<R>>;
  60. using EntryIterator = BaseEntryIterator<NodeIterator, Node>;
  61. using ConstEntryIterator =
  62. BaseEntryIterator<ConstNodeIterator, const Node>;
  63. template<typename N, typename I, typename R>
  64. using BaseValueIterator = Iterator<N, I, R, accessValue<I, R>>;
  65. using ValueIterator = BaseValueIterator<NodeIterator, Node, V>;
  66. using ConstValueIterator =
  67. BaseValueIterator<ConstNodeIterator, const Node, const V>;
  68. using ConstKeyIterator =
  69. Iterator<ConstNodeIterator, const Node, const K, accessKey>;
  70. template<typename M, typename I>
  71. struct IteratorAdapter final {
  72. M& map;
  73. I begin() const {
  74. return I(map.nodes.begin());
  75. }
  76. I end() const {
  77. return I(map.nodes.end());
  78. }
  79. };
  80. using EntryIteratorAdapter =
  81. IteratorAdapter<ProbingHashMap, EntryIterator>;
  82. using ConstEntryIteratorAdapter =
  83. IteratorAdapter<const ProbingHashMap, ConstEntryIterator>;
  84. using ValueIteratorAdapter =
  85. IteratorAdapter<ProbingHashMap, ValueIterator>;
  86. using ConstValueIteratorAdapter =
  87. IteratorAdapter<const ProbingHashMap, ConstValueIterator>;
  88. using ConstKeyIteratorAdapter =
  89. IteratorAdapter<const ProbingHashMap, ConstKeyIterator>;*/
  90. private:
  91. List<K> keys;
  92. List<V> values;
  93. K emptyKey;
  94. int entries;
  95. public:
  96. ProbingHashMap(const K& emptyKey_) : emptyKey(emptyKey_), entries(0) {
  97. }
  98. /*check_return Error copyFrom(const ProbingHashMap& other) {
  99. ProbingHashMap copy;
  100. for(const auto& en : other) {
  101. CORE_RETURN_ERROR(copy.add(en.getKey(), en.value));
  102. }
  103. swap(copy.nodes, nodes);
  104. swap(copy.nodePointers, nodePointers);
  105. return Error::NONE;
  106. }*/
  107. check_return Error rehash(int minCapacity) {
  108. minCapacity *= 2;
  109. if(minCapacity <= keys.getLength()) {
  110. return Error::NONE;
  111. }
  112. ProbingHashMap<K, V> map(emptyKey);
  113. int l = 1 << Math::roundUpLog2(Core::Math::max(minCapacity, 8));
  114. CORE_RETURN_ERROR(map.keys.resize(l, emptyKey));
  115. CORE_RETURN_ERROR(map.values.resize(l));
  116. for(int i = 0; i < keys.getLength(); i++) {
  117. if(keys[i] != emptyKey) {
  118. CORE_RETURN_ERROR(map.add(keys[i], values[i]));
  119. }
  120. }
  121. Core::swap(map.keys, keys);
  122. Core::swap(map.values, values);
  123. return Error::NONE;
  124. }
  125. /*template<typename... Args>
  126. check_return Error tryEmplace(V*& v, const K& key, Args&&... args) {
  127. CORE_RETURN_ERROR(rehash(nodes.getLength() + 1));
  128. int h = hashIndex(key);
  129. v = searchList(key, h);
  130. if(v != nullptr) {
  131. return Error::EXISTING_KEY;
  132. }
  133. NodePointer np = nullptr;
  134. CORE_RETURN_ERROR(nodes.put(np, key, Core::forward<Args>(args)...));
  135. Error e = Error::NONE;
  136. if(checkError(e, nodePointers[h].add(np))) {
  137. nodes.remove(np);
  138. return e;
  139. }
  140. v = &(np->data.value);
  141. return Error::NONE;
  142. }*/
  143. template<typename VA>
  144. check_return Error put(V*& v, const K& key, VA&& value) {
  145. CORE_RETURN_ERROR(rehash(entries + 1));
  146. int index = searchList(key);
  147. if(index < 0) {
  148. return Error::CAPACITY_REACHED;
  149. }
  150. entries += keys[index] != key;
  151. keys[index] = key;
  152. values[index] = Core::forward<VA>(value);
  153. v = &(values[index]);
  154. return Error::NONE;
  155. }
  156. template<typename VA>
  157. check_return Error add(const K& key, VA&& value) {
  158. V* v = nullptr;
  159. return put(v, key, Core::forward<VA>(value));
  160. }
  161. /*check_return Error remove(const K& key) {
  162. NodePointerList& list = nodePointers[hashIndex(key)];
  163. for(int i = 0; i < list.getLength(); i++) {
  164. if(list[i]->data.key == key) {
  165. nodes.remove(list[i]);
  166. return list.removeBySwap(i);
  167. }
  168. }
  169. return Error::NOT_FOUND;
  170. }*/
  171. const V* search(const K& key) const {
  172. int i = searchList(key);
  173. return i < 0 || keys[i] == emptyKey ? nullptr : &(values[i]);
  174. }
  175. V* search(const K& key) {
  176. int i = searchList(key);
  177. return i < 0 || keys[i] == emptyKey ? nullptr : &(values[i]);
  178. }
  179. bool contains(const K& key) const {
  180. return search(key) != nullptr;
  181. }
  182. /*ProbingHashMap& clear() {
  183. nodes.clear();
  184. for(NodePointerList& n : nodePointers) {
  185. n.clear();
  186. }
  187. return *this;
  188. }*/
  189. /*EntryIteratorAdapter entries() {
  190. return {*this};
  191. }
  192. ConstEntryIteratorAdapter entries() const {
  193. return {*this};
  194. }*/
  195. /*ConstKeyIteratorAdapter keys() const {
  196. return keys.begin();
  197. }
  198. ValueIteratorAdapter values() {
  199. return values.begin();
  200. }
  201. ConstValueIteratorAdapter values() const {
  202. return values.begin();
  203. }*/
  204. /*EntryIterator begin() {
  205. return EntryIterator(nodes.begin());
  206. }
  207. EntryIterator end() {
  208. return EntryIterator(nodes.end());
  209. }
  210. ConstEntryIterator begin() const {
  211. return ConstEntryIterator(nodes.begin());
  212. }
  213. ConstEntryIterator end() const {
  214. return ConstEntryIterator(nodes.end());
  215. }
  216. template<typename String>
  217. check_return Error toString(String& s) const {
  218. return Core::toString(s, *this);
  219. }*/
  220. private:
  221. int searchList(const K& key) const {
  222. int baseHash = static_cast<int>(hashCode(key));
  223. int end = keys.getLength() - 1;
  224. for(int i = 0; i <= end; i++) {
  225. int hash = (baseHash + i) & end;
  226. if(keys[hash] == emptyKey || keys[hash] == key) {
  227. return hash;
  228. }
  229. }
  230. return -1;
  231. }
  232. /*V* searchList(const K& key, int h) {
  233. return const_cast<V*>(
  234. static_cast<const ProbingHashMap*>(this)->searchList(key, h));
  235. }*/
  236. };
  237. }
  238. #endif