Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. vector<size_t> _count_sizes() {
  8. vector<size_t> res = {32};
  9. while (res.back() < 4e+6) {
  10. res.push_back(res.back() * 2);
  11. }
  12. return res;
  13. }
  14.  
  15. vector<size_t> _hash_map_sizes = _count_sizes();
  16.  
  17. template<class KeyType, class ValueType, class Hash = std::hash<KeyType> >
  18. class HashMap;
  19.  
  20. namespace impl {
  21.  
  22. template<class KeyType, class ValueType, class Hash = std::hash<KeyType>, bool Const = false>
  23. class Iterator {
  24. public:
  25. using reference = typename std::conditional<Const,
  26. const std::pair<const KeyType, ValueType>&, pair<const KeyType, ValueType>&>::type;
  27. using table_reference = typename std::conditional<Const,
  28. const HashMap<KeyType, ValueType, Hash>&, HashMap<KeyType, ValueType, Hash>&>::type;
  29.  
  30. using Pair = pair<KeyType, ValueType>;
  31.  
  32.  
  33. Iterator(table_reference t, size_t it):
  34. it(it),
  35. t(t)
  36. {}
  37.  
  38. Iterator& operator++() {
  39. ++it;
  40. while (it < t.size() && !t._exists(it)) {
  41. ++it;
  42. }
  43.  
  44. return *this;
  45. }
  46.  
  47. Iterator operator++(int) {
  48. Iterator t = *this;
  49. ++*this;
  50. return t;
  51. }
  52.  
  53. reference operator*() {
  54. return t.at(it);
  55. }
  56.  
  57. bool operator==(const Iterator& other) const {
  58. return (&t == &other.t) && (it == other.it);
  59. }
  60.  
  61. bool operator!=(const Iterator& other) const {
  62. return !(*this == other);
  63. }
  64. /*
  65. ValueType operator=(ValueType v) {
  66. table[
  67. }
  68. */ //!!!!!!!!!!!!!
  69. size_t get_index() const {
  70. return it;
  71. }
  72.  
  73. private:
  74. size_t it = -1;
  75. table_reference t;
  76. };
  77.  
  78. }
  79.  
  80. template<class KeyType, class ValueType, class Hash = std::hash<KeyType> >
  81. using Iterator = impl::Iterator<KeyType, ValueType, Hash, false>;
  82.  
  83. template<class KeyType, class ValueType, class Hash = std::hash<KeyType> >
  84. using ConstIterator = impl::Iterator<KeyType, ValueType, Hash, true>;
  85.  
  86.  
  87. template<class KeyType, class ValueType, class Hash>
  88. class HashMap {
  89. private:
  90. using Pair = pair<KeyType, ValueType>;
  91.  
  92. size_t n = 0;
  93. vector<size_t>& sizes = _hash_map_sizes;
  94. Hash hasher = Hash{};
  95.  
  96. vector<Pair> table;
  97. vector<bool> exists, deleted;
  98.  
  99. size_t get_hash(KeyType x) {
  100. return hasher(x) % table.size();
  101. }
  102.  
  103. void _realloc() {
  104. HashMap<KeyType, ValueType, Hash> t(begin(), end());
  105. *this = t;
  106. }
  107.  
  108. void _check() {
  109. if (table.size() * 7 <= n * 10) {
  110. _realloc();
  111. } else if (table.size() > n * 5 && table.size() > sizes[0]) {
  112. _realloc();
  113. }
  114. }
  115.  
  116. size_t _find_size(size_t x) {
  117. return 1;
  118. }
  119.  
  120. public:
  121.  
  122. HashMap() {
  123. table.resize(sizes[0]);
  124. exists.resize(sizes[0]);
  125. deleted.resize(sizes[0]);
  126. }
  127.  
  128. HashMap(Hash _hasher) {
  129. hasher = _hasher;
  130. table.resize(sizes[0]);
  131. exists.resize(sizes[0]);
  132. deleted.resize(sizes[0]);
  133. }
  134.  
  135. template<typename Iter>
  136. HashMap(Iter first, Iter last, Hash _hasher = Hash{}) {
  137. hasher = _hasher;
  138.  
  139. for (auto it = first; it != last; ++it) {
  140. ++n;
  141. }
  142. size_t new_size = _find_size(n);
  143.  
  144. table.resize(sizes[new_size]);
  145. exists.resize(sizes[new_size]);
  146. deleted.resize(sizes[new_size]);
  147.  
  148. for (auto it = first; it != last; ++it) {
  149. insert(*it, true);
  150. }
  151. }
  152.  
  153. HashMap(initializer_list<pair<KeyType, ValueType>> lst, Hash _hasher = Hash{}) {
  154. hasher = _hasher;
  155.  
  156. auto first = lst.begin();
  157. auto last = lst.end();
  158.  
  159. n = lst.size();
  160.  
  161. size_t new_size = _find_size(n);
  162.  
  163. table.resize(sizes[new_size]);
  164. exists.resize(sizes[new_size]);
  165. deleted.resize(sizes[new_size]);
  166.  
  167. for (auto it = first; it != last; ++it) {
  168. insert(*it, true);
  169. }
  170. }
  171.  
  172. int size() const {
  173. return n;
  174. }
  175.  
  176. bool empty() const {
  177. return n == 0;
  178. }
  179.  
  180. Hash hash_function() const {
  181. return hasher;
  182. }
  183.  
  184. void insert(Pair val, bool constructing = false) {
  185. auto it = find(val);
  186. if (it != end()) {
  187. return;
  188. }
  189.  
  190. ++n;
  191.  
  192. size_t index = get_hash(val);
  193.  
  194. while (exists[index]) {
  195. ++index;
  196. index %= table.size();
  197. }
  198.  
  199. table[index] = val;
  200. exists[index] = true;
  201. deleted[index] = false;
  202.  
  203. if (constructing) {
  204. return;
  205. }
  206. _check();
  207. }
  208.  
  209. void erase(Pair val) {
  210. auto it = find(val);
  211. if (it == end()) {
  212. return;
  213. }
  214.  
  215. --n;
  216.  
  217. size_t index = it.get_index();
  218. deleted[index] = true;
  219. exists[index] = false;
  220. table[index] = Pair{};
  221.  
  222. _check();
  223. }
  224.  
  225. Iterator<KeyType, ValueType, Hash> begin() {
  226. return Iterator<KeyType, ValueType, Hash>(*this, 0);
  227. }
  228.  
  229. Iterator<KeyType, ValueType, Hash> end() {
  230. return Iterator<KeyType, ValueType, Hash>(*this, table.size());
  231. }
  232.  
  233. ConstIterator<KeyType, ValueType, Hash> begin() const {
  234. return ConstIterator<KeyType, ValueType, Hash>(*this, 0);
  235. }
  236.  
  237. ConstIterator<KeyType, ValueType, Hash> end() const {
  238. return ConstIterator<KeyType, ValueType, Hash>(*this, table.size());
  239. }
  240.  
  241.  
  242. Iterator<KeyType, ValueType, Hash> find(Pair val) {
  243. auto it = get_hash(val.first);
  244. while (table[it] != val && (exists[it] || deleted[it])) {
  245. ++it;
  246. it %= table.size();
  247. }
  248. if (table[it] == val) {
  249. return Iterator<KeyType, ValueType, Hash>(*this, it);
  250. } else {
  251. return Iterator<KeyType, ValueType, Hash>(*this, table.size());
  252. }
  253. }
  254.  
  255. ConstIterator<KeyType, ValueType, Hash> find(Pair val) const {
  256. auto it = get_hash(val.first);
  257. while (table[it] != val && (exists[it] || deleted[it])) {
  258. ++it;
  259. it %= table.size();
  260. }
  261. if (table[it] == val) {
  262. return ConstIterator<KeyType, ValueType, Hash>(*this, it);
  263. } else {
  264. return ConstIterator<KeyType, ValueType, Hash>(*this, table.size());
  265. }
  266. }
  267.  
  268. Pair& operator[] (KeyType x) {
  269. auto it = find(x);
  270. if (it != this->end()) {
  271. return *it;
  272. } else {
  273. insert({x, ValueType{}});
  274. return *(find(x));
  275. }
  276. }
  277.  
  278. const Pair& at(KeyType x) {
  279. auto it = find(x);
  280. if (it != this->end()) {
  281. return *it;
  282. } else {
  283. throw out_of_range("no key we're so sorry but y'now");
  284. }
  285. }
  286.  
  287. bool _exists(size_t i) {
  288. if (i < table.size() && exists[i] && !deleted[i]) {
  289. return true;
  290. } else {
  291. return false;
  292. }
  293. }
  294.  
  295. void clear() {
  296. *this = HashMap(hasher);
  297. }
  298.  
  299. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement