HashMap.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #ifndef HASHMAP_H
  2. #define HASHMAP_H
  3. #include <iostream>
  4. constexpr static int NUMBER_OF_PRIMES = 26;
  5. constexpr static int PRIMES[26] =
  6. {
  7. 17, 37, 79, 163, 331, 673, 1361, 2729, 5471, 10949, 21911, 43853, 87719,
  8. 175447, 350899, 701819, 1403641, 2807303, 5614657, 11229331, 22458671,
  9. 44917381, 89834777, 179669557, 359339171, 718678369
  10. };
  11. template<class K, class V>
  12. class HashMap
  13. {
  14. public:
  15. HashMap(int initialLoad, unsigned int (*hasher)(K)) : hasher(hasher)
  16. {
  17. primeIndex = getHigherPrimeIndex(initialLoad);
  18. capacity = PRIMES[primeIndex];
  19. resizeCap = (capacity >> 2) * 3;
  20. data = new Node*[capacity];
  21. for(int i = 0; i < capacity; i++)
  22. {
  23. data[i] = nullptr;
  24. }
  25. entries = 0;
  26. }
  27. ~HashMap()
  28. {
  29. for(int i = 0; i < capacity; i++)
  30. {
  31. Node* n = data[i];
  32. while(n != nullptr)
  33. {
  34. Node* next = n->next;
  35. delete n;
  36. n = next;
  37. }
  38. }
  39. delete[] data;
  40. }
  41. int getCapacity() const
  42. {
  43. return capacity;
  44. }
  45. int getSize() const
  46. {
  47. return entries;
  48. }
  49. void put(K k, V v)
  50. {
  51. ensureCapacity();
  52. unsigned int hash = hasher(k) % capacity;
  53. if(data[hash] == nullptr)
  54. {
  55. data[hash] = new Node(k, v);
  56. entries++;
  57. }
  58. else
  59. {
  60. Node* n = data[hash];
  61. while(true)
  62. {
  63. if(n->k == k)
  64. {
  65. n->v = v;
  66. break;
  67. }
  68. else if(n->next == nullptr)
  69. {
  70. n->next = new Node(k, v);
  71. entries++;
  72. break;
  73. }
  74. n = n->next;
  75. }
  76. }
  77. }
  78. V remove(K k, V notFound)
  79. {
  80. unsigned int hash = hasher(k) % capacity;
  81. if(data[hash] == nullptr)
  82. {
  83. return notFound;
  84. }
  85. else
  86. {
  87. Node* n = data[hash];
  88. if(n->k == k)
  89. {
  90. V v = n->v;
  91. data[hash] = n->next;
  92. delete n;
  93. entries--;
  94. return v;
  95. }
  96. while(n->next != nullptr)
  97. {
  98. if(n->next->k == k)
  99. {
  100. Node* n = n->next;
  101. n->next = n->next->next;
  102. V v = n->v;
  103. delete n;
  104. entries--;
  105. return v;
  106. }
  107. }
  108. return notFound;
  109. }
  110. }
  111. bool remove(K k)
  112. {
  113. unsigned int hash = hasher(k) % capacity;
  114. if(data[hash] == nullptr)
  115. {
  116. return false;
  117. }
  118. else
  119. {
  120. Node* n = data[hash];
  121. if(n->k == k)
  122. {
  123. data[hash] = n->next;
  124. delete n;
  125. entries--;
  126. return true;
  127. }
  128. while(n->next != nullptr)
  129. {
  130. if(n->next->k == k)
  131. {
  132. Node* n = n->next;
  133. n->next = n->next->next;
  134. delete n;
  135. entries--;
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. }
  142. V get(K k, V error) const
  143. {
  144. unsigned int hash = hasher(k) % capacity;
  145. if(data[hash] == nullptr)
  146. {
  147. return error;
  148. }
  149. else
  150. {
  151. for(Node* n = data[hash]; n != nullptr; n = n->next)
  152. {
  153. if(n->k == k)
  154. {
  155. return n->v;
  156. }
  157. }
  158. return error;
  159. }
  160. }
  161. void clear()
  162. {
  163. for(int i = 0; i < capacity; i++)
  164. {
  165. Node* n = data[i];
  166. while(n != nullptr)
  167. {
  168. Node* next = n->next;
  169. delete n;
  170. n = next;
  171. }
  172. data[i] = nullptr;
  173. }
  174. entries = 0;
  175. }
  176. void print() const
  177. {
  178. for(int i = 0; i < capacity; i++)
  179. {
  180. Node* n = data[i];
  181. std::cout << i << ": ";
  182. if(n != nullptr)
  183. {
  184. std::cout << "(" << n->k << " = " << n->v << ")";
  185. while(n->next != nullptr)
  186. {
  187. n = n->next;
  188. std::cout << " -> (" << n->k << " = " << n->v << ")";
  189. }
  190. }
  191. std::cout << std::endl;
  192. }
  193. }
  194. void printStats() const
  195. {
  196. int fill = capacity;
  197. int cells = capacity;
  198. for(int i = 0; i < capacity; i++)
  199. {
  200. Node* n = data[i];
  201. if(n != nullptr)
  202. {
  203. while(n->next != nullptr)
  204. {
  205. fill++;
  206. n = n->next;
  207. }
  208. }
  209. }
  210. std::cout << (double) fill / cells << std::endl;
  211. }
  212. private:
  213. class Node
  214. {
  215. public:
  216. Node* next = nullptr;
  217. K k;
  218. V v;
  219. Node(K k, V v) : k(k), v(v)
  220. {
  221. }
  222. };
  223. int getHigherPrimeIndex(int lower) const
  224. {
  225. int low = 0;
  226. int high = NUMBER_OF_PRIMES - 1;
  227. int mid;
  228. while(true)
  229. {
  230. if(low == high)
  231. {
  232. return low;
  233. }
  234. mid = (high + low) >> 1;
  235. if(PRIMES[mid] >= lower)
  236. {
  237. high = mid;
  238. }
  239. else
  240. {
  241. low = mid + 1;
  242. }
  243. }
  244. }
  245. void ensureCapacity()
  246. {
  247. if(entries < resizeCap)
  248. {
  249. return;
  250. }
  251. primeIndex++;
  252. if(primeIndex >= NUMBER_OF_PRIMES)
  253. {
  254. return;
  255. }
  256. int oldCapacity = capacity;
  257. capacity = PRIMES[primeIndex];
  258. //std::cout << "resize from " << oldCapacity << " to " << capacity << std::endl;
  259. resizeCap = (capacity >> 2) * 3;
  260. Node** newData = new Node*[capacity];
  261. for(int i = 0; i < capacity; i++)
  262. {
  263. newData[i] = nullptr;
  264. }
  265. for(int i = 0; i < oldCapacity; i++)
  266. {
  267. Node* old = data[i];
  268. if(old != nullptr)
  269. {
  270. unsigned int hash = hasher(old->k) % capacity;
  271. Node* n = newData[hash];
  272. if(n == nullptr)
  273. {
  274. newData[hash] = old;
  275. }
  276. else
  277. {
  278. while(n->next != nullptr)
  279. {
  280. n = n->next;
  281. }
  282. n->next = old;
  283. }
  284. while(old->next != nullptr)
  285. {
  286. n = old->next;
  287. old->next = nullptr;
  288. hash = hasher(n->k) % capacity;
  289. Node* m = newData[hash];
  290. if(m == nullptr)
  291. {
  292. newData[hash] = n;
  293. }
  294. else
  295. {
  296. while(m->next != nullptr)
  297. {
  298. m = m->next;
  299. }
  300. m->next = n;
  301. }
  302. old = n;
  303. }
  304. }
  305. }
  306. delete[] data;
  307. data = newData;
  308. }
  309. unsigned int (*hasher)(K);
  310. int primeIndex;
  311. int capacity;
  312. int resizeCap;
  313. Node** data;
  314. int entries;
  315. };
  316. #endif