HashMap.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. // returns true on error
  94. check_return bool copyFrom(const HashMap& other) {
  95. HashMap copy;
  96. for(const auto& e : other) {
  97. if(copy.add(e.getKey(), e.value) == nullptr) {
  98. return true;
  99. }
  100. }
  101. swap(copy.nodes, nodes);
  102. swap(copy.nodePointers, nodePointers);
  103. return false;
  104. }
  105. // returns true on error
  106. check_return bool rehash(int minCapacity) {
  107. if(minCapacity <= nodePointers.getLength()) {
  108. return false;
  109. }
  110. HashMap<K, V> map;
  111. int l = 1 << Math::roundUpLog2(Core::Math::max(minCapacity, 8));
  112. if(map.nodePointers.resize(l)) {
  113. return true;
  114. }
  115. for(NodePointerList& list : nodePointers) {
  116. for(NodePointer& n : list) {
  117. int h = map.hashIndex(n->data.key);
  118. if(map.nodePointers[h].add(n) == nullptr) {
  119. return true;
  120. }
  121. }
  122. }
  123. Core::swap(map.nodePointers, nodePointers);
  124. return false;
  125. }
  126. // returns a nullptr on error
  127. template<typename... Args>
  128. check_return V* tryEmplace(const K& key, Args&&... args) {
  129. if(rehash(nodes.getLength() + 1)) {
  130. return nullptr;
  131. }
  132. int h = hashIndex(key);
  133. V* v = searchList(key, h);
  134. if(v != nullptr) {
  135. return nullptr;
  136. }
  137. NodePointer np = nodes.add(key, Core::forward<Args>(args)...);
  138. if(np == nullptr) {
  139. return nullptr;
  140. } else if(nodePointers[h].add(np) == nullptr) {
  141. nodes.remove(np);
  142. return nullptr;
  143. }
  144. return &(np->data.value);
  145. }
  146. // returns a nullptr on error
  147. template<typename VA>
  148. check_return V* add(const K& key, VA&& value) {
  149. if(rehash(nodes.getLength() + 1)) {
  150. return nullptr;
  151. }
  152. int h = hashIndex(key);
  153. V* v = searchList(key, h);
  154. if(v != nullptr) {
  155. *v = Core::forward<VA>(value);
  156. return v;
  157. }
  158. NodePointer np = nodes.add(key, Core::forward<VA>(value));
  159. if(np == nullptr) {
  160. return nullptr;
  161. } else if(nodePointers[h].add(np) == nullptr) {
  162. nodes.remove(np);
  163. return nullptr;
  164. }
  165. return &(np->data.value);
  166. }
  167. // returns true when a value was removed
  168. check_return bool remove(const K& key) {
  169. NodePointerList& list = nodePointers[hashIndex(key)];
  170. for(int i = 0; i < list.getLength(); i++) {
  171. if(list[i]->data.key == key) {
  172. NodePointer np = list[i];
  173. bool r = list.removeBySwap(i);
  174. nodes.remove(np);
  175. return !r;
  176. }
  177. }
  178. return false;
  179. }
  180. const V* search(const K& key) const {
  181. return searchList(key, hashIndex(key));
  182. }
  183. V* search(const K& key) {
  184. return searchList(key, hashIndex(key));
  185. }
  186. bool contains(const K& key) const {
  187. return search(key) != nullptr;
  188. }
  189. HashMap& clear() {
  190. nodes.clear();
  191. for(NodePointerList& n : nodePointers) {
  192. n.clear();
  193. }
  194. return *this;
  195. }
  196. EntryIteratorAdapter entries() {
  197. return {*this};
  198. }
  199. ConstEntryIteratorAdapter entries() const {
  200. return {*this};
  201. }
  202. ConstKeyIteratorAdapter keys() const {
  203. return {*this};
  204. }
  205. ValueIteratorAdapter values() {
  206. return {*this};
  207. }
  208. ConstValueIteratorAdapter values() const {
  209. return {*this};
  210. }
  211. EntryIterator begin() {
  212. return EntryIterator(nodes.begin());
  213. }
  214. EntryIterator end() {
  215. return EntryIterator(nodes.end());
  216. }
  217. ConstEntryIterator begin() const {
  218. return ConstEntryIterator(nodes.begin());
  219. }
  220. ConstEntryIterator end() const {
  221. return ConstEntryIterator(nodes.end());
  222. }
  223. // returns true on error
  224. template<int L>
  225. check_return bool toString(ArrayString<L>& s) const {
  226. if(s.append("[")) {
  227. return true;
  228. }
  229. bool c = false;
  230. for(const NodePointerList& list : nodePointers) {
  231. for(const NodePointer& n : list) {
  232. if(c && s.append(", ")) {
  233. return true;
  234. } else if(s.append(n->data.key) || s.append(" = ") ||
  235. s.append(n->data.value)) {
  236. return true;
  237. }
  238. c = true;
  239. }
  240. }
  241. return s.append("]");
  242. }
  243. private:
  244. template<typename H>
  245. int hashIndex(const H& key) const {
  246. return static_cast<int>(hashCode(key)) &
  247. (nodePointers.getLength() - 1);
  248. }
  249. const V* searchList(const K& key, int h) const {
  250. if(nodePointers.getLength() == 0) {
  251. return nullptr;
  252. }
  253. for(const NodePointer& n : nodePointers[h]) {
  254. if(n->data.key == key) {
  255. return &n->data.value;
  256. }
  257. }
  258. return nullptr;
  259. }
  260. V* searchList(const K& key, int h) {
  261. return const_cast<V*>(
  262. static_cast<const HashMap*>(this)->searchList(key, h));
  263. }
  264. };
  265. }
  266. #endif