HashMap.hpp 8.3 KB

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