HashMap.h 9.3 KB

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