HashMap.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef HASHMAP_H
  2. #define HASHMAP_H
  3. #include "utils/Array.h"
  4. #include "utils/List.h"
  5. #include "utils/StringBuffer.h"
  6. #include "utils/Types.h"
  7. #include "utils/Utils.h"
  8. template<typename K, typename V>
  9. struct HashMap final {
  10. class Node final {
  11. friend HashMap;
  12. friend List<Node>;
  13. K key;
  14. public:
  15. V value;
  16. const K& getKey() const {
  17. return key;
  18. }
  19. private:
  20. int next;
  21. Node(const K& key, const V& value) : key(key), value(value), next(-1) {
  22. }
  23. Node(const K& key, V&& value)
  24. : key(key), value(std::move(value)), next(-1) {
  25. }
  26. template<typename... Args>
  27. Node(const K& key, Args&&... args)
  28. : key(key), value(std::forward<Args>(args)...), next(-1) {
  29. }
  30. };
  31. template<typename N, typename R>
  32. class BaseEntryIterator final {
  33. N& nodes;
  34. int indexA;
  35. int indexB;
  36. public:
  37. BaseEntryIterator(N& nodes, int indexA, int indexB)
  38. : nodes(nodes), indexA(indexA), indexB(indexB) {
  39. skip();
  40. }
  41. BaseEntryIterator& operator++() {
  42. indexB++;
  43. skip();
  44. return *this;
  45. }
  46. bool operator!=(const BaseEntryIterator& other) const {
  47. return indexA != other.indexA || indexB != other.indexB;
  48. }
  49. R& operator*() {
  50. return nodes[indexA][indexB];
  51. }
  52. private:
  53. void skip() {
  54. while(indexA < nodes.getLength() &&
  55. indexB >= nodes[indexA].getLength()) {
  56. indexA++;
  57. indexB = 0;
  58. }
  59. }
  60. };
  61. typedef BaseEntryIterator<List<List<Node>>, Node> EntryIterator;
  62. typedef BaseEntryIterator<const List<List<Node>>, const Node>
  63. ConstEntryIterator;
  64. struct EntryIteratorAdapter final {
  65. HashMap& map;
  66. EntryIterator begin() {
  67. return EntryIterator(map.nodes, 0, 0);
  68. }
  69. EntryIterator end() {
  70. return EntryIterator(map.nodes, map.nodes.getLength(), 0);
  71. }
  72. };
  73. struct ConstEntryIteratorAdapter final {
  74. const HashMap& map;
  75. ConstEntryIterator begin() const {
  76. return ConstEntryIterator(map.nodes, 0, 0);
  77. }
  78. ConstEntryIterator end() const {
  79. return ConstEntryIterator(map.nodes, map.nodes.getLength(), 0);
  80. }
  81. };
  82. template<typename N, typename R>
  83. class BaseValueIterator final {
  84. N& nodes;
  85. int indexA;
  86. int indexB;
  87. public:
  88. BaseValueIterator(N& nodes, int indexA, int indexB)
  89. : nodes(nodes), indexA(indexA), indexB(indexB) {
  90. skip();
  91. }
  92. BaseValueIterator& operator++() {
  93. indexB++;
  94. skip();
  95. return *this;
  96. }
  97. bool operator!=(const BaseValueIterator& other) const {
  98. return indexA != other.indexA || indexB != other.indexB;
  99. }
  100. R& operator*() {
  101. return nodes[indexA][indexB].value;
  102. }
  103. private:
  104. void skip() {
  105. while(indexA < nodes.getLength() &&
  106. indexB >= nodes[indexA].getLength()) {
  107. indexA++;
  108. indexB = 0;
  109. }
  110. }
  111. };
  112. typedef BaseValueIterator<List<List<Node>>, V> ValueIterator;
  113. typedef BaseValueIterator<const List<List<Node>>, const V>
  114. ConstValueIterator;
  115. struct ValueIteratorAdapter final {
  116. HashMap& map;
  117. ValueIterator begin() {
  118. return ValueIterator(map.nodes, 0, 0);
  119. }
  120. ValueIterator end() {
  121. return ValueIterator(map.nodes, map.nodes.getLength(), 0);
  122. }
  123. };
  124. struct ConstValueIteratorAdapter final {
  125. const HashMap& map;
  126. ConstValueIterator begin() const {
  127. return ConstValueIterator(map.nodes, 0, 0);
  128. }
  129. ConstValueIterator end() const {
  130. return ConstValueIterator(map.nodes, map.nodes.getLength(), 0);
  131. }
  132. };
  133. class ConstKeyIterator final {
  134. const List<List<Node>>& nodes;
  135. int indexA;
  136. int indexB;
  137. public:
  138. ConstKeyIterator(const List<List<Node>>& nodes, int indexA, int indexB)
  139. : nodes(nodes), indexA(indexA), indexB(indexB) {
  140. skip();
  141. }
  142. ConstKeyIterator& operator++() {
  143. indexB++;
  144. skip();
  145. return *this;
  146. }
  147. bool operator!=(const ConstKeyIterator& other) const {
  148. return indexA != other.indexA || indexB != other.indexB;
  149. }
  150. const K& operator*() {
  151. return nodes[indexA][indexB].getKey();
  152. }
  153. private:
  154. void skip() {
  155. while(indexA < nodes.getLength() &&
  156. indexB >= nodes[indexA].getLength()) {
  157. indexA++;
  158. indexB = 0;
  159. }
  160. }
  161. };
  162. struct ConstKeyIteratorAdapter final {
  163. const HashMap& map;
  164. ConstKeyIterator begin() const {
  165. return ConstKeyIterator(map.nodes, 0, 0);
  166. }
  167. ConstKeyIterator end() const {
  168. return ConstKeyIterator(map.nodes, map.nodes.getLength(), 0);
  169. }
  170. };
  171. private:
  172. List<List<Node>> nodes;
  173. int elements;
  174. public:
  175. HashMap(int minCapacity = 8) : elements(0) {
  176. nodes.resize(1 << Utils::roundUpLog2(minCapacity));
  177. }
  178. template<typename... Args>
  179. bool tryEmplace(const K& key, Args&&... args) {
  180. rehash();
  181. Hash h = hash(key);
  182. V* v = searchList(key, h);
  183. if(v == nullptr) {
  184. nodes[h].add(key, std::forward<Args>(args)...);
  185. elements++;
  186. return false;
  187. }
  188. return true;
  189. }
  190. HashMap& add(const K& key, const V& value) {
  191. rehash();
  192. Hash h = hash(key);
  193. V* v = searchList(key, h);
  194. if(v == nullptr) {
  195. nodes[h].add(key, value);
  196. elements++;
  197. } else {
  198. *v = value;
  199. }
  200. return *this;
  201. }
  202. HashMap& add(const K& key, V&& value) {
  203. rehash();
  204. Hash h = hash(key);
  205. V* v = searchList(key, h);
  206. if(v == nullptr) {
  207. nodes[h].add(key, std::move(value));
  208. elements++;
  209. } else {
  210. *v = std::move(value);
  211. }
  212. return *this;
  213. }
  214. bool remove(const K& key) {
  215. List<Node>& list = nodes[hash(key)];
  216. for(int i = 0; i < list.getLength(); i++) {
  217. if(list[i].key == key) {
  218. list.removeBySwap(i);
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. const V* search(const K& key) const {
  225. return searchList(key, hash(key));
  226. }
  227. V* search(const K& key) {
  228. return searchList(key, hash(key));
  229. }
  230. bool contains(const K& key) const {
  231. return search(key) != nullptr;
  232. }
  233. HashMap& clear() {
  234. for(List<Node>& n : nodes) {
  235. n.clear();
  236. }
  237. elements = 0;
  238. return *this;
  239. }
  240. EntryIteratorAdapter entries() {
  241. return {*this};
  242. }
  243. const ConstEntryIteratorAdapter entries() const {
  244. return {*this};
  245. }
  246. const ConstKeyIteratorAdapter keys() const {
  247. return {*this};
  248. }
  249. ValueIteratorAdapter values() {
  250. return {*this};
  251. }
  252. const ConstValueIteratorAdapter values() const {
  253. return {*this};
  254. }
  255. EntryIterator begin() {
  256. return EntryIterator(nodes, 0, 0);
  257. }
  258. EntryIterator end() {
  259. return EntryIterator(nodes, nodes.getLength(), 0);
  260. }
  261. ConstEntryIterator begin() const {
  262. return ConstEntryIterator(nodes, 0, 0);
  263. }
  264. ConstEntryIterator end() const {
  265. return ConstEntryIterator(nodes, nodes.getLength(), 0);
  266. }
  267. template<int L>
  268. void toString(StringBuffer<L>& s) const {
  269. s.append("[");
  270. bool c = false;
  271. for(const List<Node>& list : nodes) {
  272. for(const Node& n : list) {
  273. if(c) {
  274. s.append(", ");
  275. }
  276. s.append(n.key).append(" = ").append(n.value);
  277. c = true;
  278. }
  279. }
  280. s.append("]");
  281. }
  282. private:
  283. template<typename H>
  284. Hash hash(const H& key) const {
  285. return fullHash(key) & (nodes.getLength() - 1);
  286. }
  287. template<typename H>
  288. Hash fullHash(const H& key) const {
  289. return key.hashCode();
  290. }
  291. Hash fullHash(int key) const {
  292. return key;
  293. }
  294. Hash fullHash(unsigned int key) const {
  295. return key;
  296. }
  297. void rehash() {
  298. if(elements < nodes.getLength()) {
  299. return;
  300. }
  301. HashMap<K, V> map(nodes.getLength() * 2);
  302. for(List<Node>& list : nodes) {
  303. for(Node& n : list) {
  304. map.tryEmplace(n.key, std::move(n.value));
  305. }
  306. }
  307. *this = std::move(map);
  308. }
  309. const V* searchList(const K& key, Hash h) const {
  310. for(const Node& n : nodes[h]) {
  311. if(n.key == key) {
  312. return &n.value;
  313. }
  314. }
  315. return nullptr;
  316. }
  317. V* searchList(const K& key, Hash h) {
  318. return const_cast<V*>(
  319. static_cast<const HashMap*>(this)->searchList(key, h));
  320. }
  321. };
  322. #endif