HashMap.h 9.0 KB

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