HashMap.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 ValueIteratorAdapter = IteratorAdapter<HashMap, ValueIterator>;
  88. using ConstValueIteratorAdapter =
  89. IteratorAdapter<const HashMap, ConstValueIterator>;
  90. using ConstKeyIteratorAdapter =
  91. IteratorAdapter<const HashMap, ConstKeyIterator>;
  92. private:
  93. LinkedList<Node> nodes;
  94. List<NodePointerList> nodePointers;
  95. public:
  96. check_return Error copyFrom(const HashMap& other) {
  97. HashMap copy;
  98. for(const auto& en : other) {
  99. CORE_RETURN_ERROR(copy.add(en.getKey(), en.value));
  100. }
  101. swap(copy.nodes, nodes);
  102. swap(copy.nodePointers, nodePointers);
  103. return Error::NONE;
  104. }
  105. check_return Error rehash(int minCapacity) {
  106. if(minCapacity <= nodePointers.getLength()) {
  107. return Error::NONE;
  108. }
  109. HashMap<K, V> map;
  110. int l = Core::Math::max(1 << Math::roundUpLog2(minCapacity), 8);
  111. CORE_RETURN_ERROR(map.nodePointers.resize(l));
  112. for(NodePointerList& list : nodePointers) {
  113. for(NodePointer& n : list) {
  114. int h = map.hashIndex(n->data.key);
  115. CORE_RETURN_ERROR(map.nodePointers[h].add(n));
  116. }
  117. }
  118. Core::swap(map.nodePointers, nodePointers);
  119. return Error::NONE;
  120. }
  121. template<typename... Args>
  122. check_return Error tryEmplace(V*& v, const K& key, Args&&... args) {
  123. CORE_RETURN_ERROR(rehash(nodes.getLength() + 1));
  124. int h = hashIndex(key);
  125. v = searchList(key, h);
  126. if(v != nullptr) {
  127. return Error::EXISTING_KEY;
  128. }
  129. NodePointer np = nullptr;
  130. CORE_RETURN_ERROR(nodes.put(np, key, Core::forward<Args>(args)...));
  131. Error e = Error::NONE;
  132. if(checkError(e, nodePointers[h].add(np))) {
  133. nodes.remove(np);
  134. return e;
  135. }
  136. v = &(np->data.value);
  137. return Error::NONE;
  138. }
  139. template<typename VA>
  140. check_return Error put(V*& v, const K& key, VA&& value) {
  141. CORE_RETURN_ERROR(rehash(nodes.getLength() + 1));
  142. int h = hashIndex(key);
  143. v = searchList(key, h);
  144. if(v != nullptr) {
  145. *v = Core::forward<VA>(value);
  146. return Error::NONE;
  147. }
  148. NodePointer np = nullptr;
  149. CORE_RETURN_ERROR(nodes.put(np, key, Core::forward<VA>(value)));
  150. Error e = Error::NONE;
  151. if(checkError(e, nodePointers[h].add(np))) {
  152. nodes.remove(np);
  153. return e;
  154. }
  155. v = &(np->data.value);
  156. return Error::NONE;
  157. }
  158. template<typename VA>
  159. check_return Error add(const K& key, VA&& value) {
  160. V* v = nullptr;
  161. return put(v, key, Core::forward<VA>(value));
  162. }
  163. check_return Error remove(const K& key) {
  164. NodePointerList& list = nodePointers[hashIndex(key)];
  165. for(int i = 0; i < list.getLength(); i++) {
  166. if(list[i]->data.key == key) {
  167. nodes.remove(list[i]);
  168. return list.removeBySwap(i);
  169. }
  170. }
  171. return Error::NOT_FOUND;
  172. }
  173. const V* search(const K& key) const {
  174. return searchList(key, hashIndex(key));
  175. }
  176. V* search(const K& key) {
  177. return searchList(key, hashIndex(key));
  178. }
  179. bool contains(const K& key) const {
  180. return search(key) != nullptr;
  181. }
  182. HashMap& clear() {
  183. nodes.clear();
  184. for(NodePointerList& n : nodePointers) {
  185. n.clear();
  186. }
  187. return *this;
  188. }
  189. ConstKeyIteratorAdapter getKeys() const {
  190. return {*this};
  191. }
  192. ValueIteratorAdapter getValues() {
  193. return {*this};
  194. }
  195. ConstValueIteratorAdapter getValues() const {
  196. return {*this};
  197. }
  198. EntryIterator begin() {
  199. return EntryIterator(nodes.begin());
  200. }
  201. EntryIterator end() {
  202. return EntryIterator(nodes.end());
  203. }
  204. ConstEntryIterator begin() const {
  205. return ConstEntryIterator(nodes.begin());
  206. }
  207. ConstEntryIterator end() const {
  208. return ConstEntryIterator(nodes.end());
  209. }
  210. template<typename String>
  211. check_return Error toString(String& s) const {
  212. return Core::toString(s, *this);
  213. }
  214. private:
  215. template<typename H>
  216. int hashIndex(const H& key) const {
  217. return static_cast<int>(hashCode(key)) &
  218. (nodePointers.getLength() - 1);
  219. }
  220. const V* searchList(const K& key, int h) const {
  221. if(nodePointers.getLength() == 0) {
  222. return nullptr;
  223. }
  224. for(const NodePointer& n : nodePointers[h]) {
  225. if(n->data.key == key) {
  226. return &n->data.value;
  227. }
  228. }
  229. return nullptr;
  230. }
  231. V* searchList(const K& key, int h) {
  232. return const_cast<V*>(
  233. static_cast<const HashMap*>(this)->searchList(key, h));
  234. }
  235. };
  236. }
  237. #endif