HashMap.hpp 7.8 KB

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