Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MOD = 61007;
  4. using namespace std;
  5.  
  6. template<class KeyType, class ValueType> class IteratorHash {
  7. public:
  8. vector<std::list<pair<const KeyType, ValueType>>> *table_num;
  9. size_t key, ind;
  10. IteratorHash(vector<std::list<pair<const KeyType, ValueType>>> *table_num, size_t key, size_t ind) {
  11. this->table_num = table_num;
  12. this->key = key;
  13. this->ind = ind;
  14. }
  15.  
  16. std::pair<const KeyType, ValueType> &operator*() {
  17. return *table_num[key][ind];
  18. }
  19.  
  20. std::pair<const KeyType, ValueType> *operator->() {
  21. return &*table_num[key][ind];
  22. }
  23.  
  24. IteratorHash &operator++() {
  25. ++ind;
  26. if (ind < *table_num[key].size()) {
  27. return *this;
  28. }
  29.  
  30. ind = 0;
  31. ++key;
  32. return *this;
  33. }
  34.  
  35. bool operator==(const IteratorHash &HashOther) const {
  36. return ((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
  37. }
  38.  
  39. bool operator!=(const IteratorHash &HashOther) const {
  40. return !((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
  41. }
  42.  
  43. IteratorHash operator++(int) {
  44. IteratorHash ans = *this;
  45. ++*this;
  46. return ans;
  47. }
  48.  
  49. };
  50.  
  51. template<class KeyType, class ValueType> class IteratorHashConst {
  52. public:
  53. vector<std::list<pair<const KeyType, ValueType>>> *table_num;
  54. size_t key, ind;
  55. IteratorHashConst(vector<std::list<pair<const KeyType, ValueType>>> *table_num, size_t key, size_t ind) {
  56. this->table_num = table_num;
  57. this->key = key;
  58. this->ind = ind;
  59. }
  60.  
  61. const std::pair<const KeyType, ValueType> &operator*() const {
  62. return *table_num[key][ind];
  63. }
  64.  
  65. const std::pair<const KeyType, ValueType> *operator->() const {
  66. return &*table_num[key][ind];
  67. }
  68.  
  69. IteratorHashConst &operator++() {
  70. ++ind;
  71. if (ind < *table_num[key].size()) {
  72. return *this;
  73. }
  74.  
  75. ind = 0;
  76. ++key;
  77. return *this;
  78. }
  79.  
  80. bool operator==(const IteratorHashConst &HashOther) const {
  81. return ((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
  82. }
  83.  
  84. bool operator!=(const IteratorHashConst &HashOther) const {
  85. return !((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
  86. }
  87.  
  88. IteratorHashConst operator++(int) {
  89. IteratorHashConst ans = *this;
  90. ++*this;
  91. return ans;
  92. }
  93.  
  94. };
  95.  
  96.  
  97. template<class KeyType, class ValueType, class Hash = std::hash<KeyType> > class HashMap {
  98. public:
  99. vector<vector<pair<KeyType, ValueType>>> table;
  100. typedef IteratorHash<const KeyType, ValueType> iterator;
  101. typedef IteratorHashConst<const KeyType, ValueType> const_iterator;
  102. Hash get_hash;
  103. size_t length;
  104. HashMap (Hash val = Hash()) {
  105. get_hash = val;
  106. table.resize(MOD);
  107. length = 0;
  108. }
  109.  
  110. template <class IteratorOther> HashMap(IteratorOther left, IteratorOther right, Hash val = Hash()) {
  111. get_hash = val;
  112. table.resize(0);
  113. length = 0;
  114. while (left != right) {
  115. insert(*left++);
  116. }
  117. }
  118.  
  119. HashMap(std::initializer_list<std::pair<KeyType, ValueType>> add, Hash val = Hash()) {
  120. get_hash = val;
  121. table.resize(0);
  122. length = 0;
  123. for (auto cur = add.begin(); cur != add.end(); ++cur) {
  124. insert(*cur);
  125. }
  126. }
  127.  
  128. size_t size() const {
  129. return length;
  130. }
  131.  
  132. bool empty() const {
  133. return (length == 0);
  134. }
  135.  
  136. Hash hash_function() const {
  137. return get_hash;
  138. }
  139.  
  140. void insert(const std::pair<KeyType, ValueType> &add) {
  141. size_t ind = get_hash(add.first) % MOD;
  142. if (!table[ind].empty()) {
  143. for (auto cur : table[ind]) {
  144. if (cur == add.first) {
  145. return;
  146. }
  147. }
  148. }
  149.  
  150. table[ind].push_back(add);
  151. }
  152.  
  153. void erase(const KeyType &to_del) {
  154. size_t ind = get_hash(to_del) % MOD;
  155. if (table[ind].empty()) {
  156. return;
  157. }
  158.  
  159. for (size_t i = 0; i < table[ind].size(); ++i) {
  160. if (table[ind][i] == to_del) {
  161. for (size_t j = i; j < table[ind].size() - 1; ++i) {
  162. table[ind][j] = table[ind][j + 1];
  163. }
  164. table[ind].pop_back();
  165. --length;
  166. return;
  167. }
  168. }
  169. }
  170.  
  171. iterator begin() {
  172. if (length == 0) {
  173. return this->end;
  174. }
  175. return iterator(table, 0, 0);
  176. }
  177.  
  178. iterator end() {
  179. return iterator(table, table.size(), 0);
  180. }
  181.  
  182. /*const_iterator begin() const {
  183. if (length == 0) {
  184. return this->end();
  185. }
  186. return const_iterator(table, 0, 0);
  187. }
  188.  
  189. const_iterator end() const {
  190. return const_iterator(table, table.size(), 0);
  191. }*/
  192.  
  193. iterator find(const KeyType &val) {
  194. size_t ind = get_hash(val) % MOD;
  195. if (table[val].empty) {
  196. return this->end();
  197. }
  198.  
  199. for (size_t i = 0; i < table[ind].size(); ++i) {
  200. if (table[ind][i] == val) {
  201. return iterator(table, val, i);
  202. }
  203. }
  204.  
  205. return this->end();
  206. }
  207.  
  208. const_iterator find(const KeyType &val) const {
  209. size_t ind = get_hash(val) % MOD;
  210. if (table[val].empty) {
  211. return this->end();
  212. }
  213.  
  214. for (size_t i = 0; i < table[ind].size(); ++i) {
  215. if (table[ind][i] == val) {
  216. return const_iterator(table, val, i);
  217. }
  218. }
  219.  
  220. return this->end();
  221. }
  222.  
  223. ValueType& operator[](const KeyType &val) {
  224. iterator cur = find(val);
  225. if (cur != this->end()) {
  226. return cur->second;
  227. }
  228.  
  229. insert(std::make_pair(val, ValueType()));
  230. return find(val)->second;
  231. }
  232.  
  233. const ValueType& at(const KeyType val) const {
  234. if (find(val) == end()) {
  235. throw std::out_of_range("");
  236. } else {
  237. return find(val)->second;
  238. }
  239. }
  240.  
  241. void clear() {
  242. if (length == 0) {
  243. return;
  244. }
  245.  
  246. length = 0;
  247. table.clear();
  248. table.resize(MOD);
  249. }
  250. };
  251.  
  252. int main() {
  253. int n;
  254. cin >> n;
  255. vector<pair<int, string> > a(n);
  256. for (int i = 0; i < n; i++)
  257. cin >> a[i].first >> a[i].second;
  258. const HashMap<int, std::string> map(a.begin(), a.end());
  259. auto it = map.begin();
  260. ++it;
  261. auto other_it = it;
  262.  
  263. //map.print();
  264. return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement