HashMap.h 8.6 KB

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