HashMap.h 9.1 KB

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