HashMap.h 8.9 KB

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