Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. #include <algorithm>
  2. #include <vector>
  3. #include <iostream>
  4. #include <initializer_list>
  5. #include <iterator>
  6. #include <list>
  7. #include <exception>
  8. #include <string>
  9. #include <ctime>
  10. #include <unordered_map>
  11. #include <utility>
  12.  
  13. template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
  14. class HashMap {
  15. public:
  16. typedef typename std::list<std::pair<const KeyType, ValueType>>::iterator iterator;
  17. typedef typename std::list<std::pair<const KeyType, ValueType>>::const_iterator const_iterator;
  18.  
  19. private:
  20. std::vector<std::vector<iterator>> Data;
  21. std::list<std::pair<const KeyType, ValueType>> Pairs;
  22. std::list<size_t> NotEmptyCells;
  23. Hash hasher;
  24. std::vector<bool> isEmpty;
  25. std::vector<std::list<size_t>::iterator> NotEmptyPtr;
  26. public:
  27. HashMap(Hash hasher_init = Hash()) : hasher(hasher_init),
  28. isEmpty(std::vector<bool>(765322, true)) {
  29. Data.resize(765322);
  30. NotEmptyPtr.resize(765322);
  31. }
  32.  
  33. Hash hash_function() const {
  34. return hasher;
  35. }
  36.  
  37. HashMap(const std::initializer_list<std::pair<const KeyType, ValueType>>& l,
  38. Hash hasher_init = Hash()) : hasher(hasher_init),
  39. isEmpty(std::vector<bool>(765322, true)) {
  40. Data.resize(765322);
  41. NotEmptyPtr.resize(765322);
  42. for (auto el : l) {
  43. insert(el);
  44. }
  45. }
  46.  
  47. template <class iterate>
  48. HashMap(iterate begin, iterate end, Hash hasher_init = Hash()) : hasher(hasher_init),
  49. isEmpty(std::vector<bool>(765322, true)) {
  50. Data.resize(765322);
  51. NotEmptyPtr.resize(765322);
  52. for (iterate it = begin; it != end; ++it) {
  53. insert(*it);
  54. }
  55. }
  56.  
  57. const_iterator begin() const {
  58. return const_iterator(Pairs.cbegin());
  59. }
  60.  
  61. iterator begin() {
  62. return iterator(Pairs.begin());
  63. }
  64.  
  65. const_iterator end() const {
  66. return const_iterator(Pairs.cend());
  67. }
  68.  
  69. iterator end() {
  70. return iterator(Pairs.end());
  71. }
  72.  
  73. void insert(const std::pair<const KeyType, ValueType>& key) {
  74. size_t hash = hasher(key.first) % 765322;
  75. if (find(key.first) == Pairs.end()) {
  76. Pairs.push_back(key);
  77. Data[hash].push_back(--Pairs.end());
  78. if (isEmpty[hash]) {
  79. NotEmptyCells.push_back(hash);
  80. isEmpty[hash] = true;
  81. NotEmptyPtr[hash] = --NotEmptyCells.end();
  82. }
  83. }
  84. }
  85.  
  86. void erase(const KeyType& key) {
  87. size_t hash = hasher(key) % 765322;
  88. auto it = Data[hash].begin();
  89. for (it = Data[hash].begin(); it != Data[hash].end(); ++it) {
  90. if ((*it)->first == key) {
  91. break;
  92. }
  93. }
  94. if (it != Data[hash].end()) {
  95. Pairs.erase(*it);
  96. Data[hash].erase(it);
  97. if (Data[hash].empty()) {
  98. isEmpty[hash] = true;
  99. NotEmptyCells.erase(NotEmptyPtr[hash]);
  100. }
  101. }
  102. }
  103.  
  104. size_t size() const {
  105. return Pairs.size();
  106. }
  107.  
  108. bool empty() const {
  109. return Pairs.empty();
  110. }
  111.  
  112. void clear() {
  113. Pairs.clear();
  114. for (auto empty : NotEmptyCells) {
  115. Data[empty].clear();
  116. isEmpty[empty] = true;
  117. }
  118. }
  119.  
  120. ValueType& operator[](const KeyType& key) {
  121. if (find(key) != Pairs.end()) {
  122. size_t hash = hasher(key) % 765322;
  123. for (auto i : Data[hash]) {
  124. if (i->first == key) {
  125. return i->second;
  126. }
  127. }
  128. }
  129. else {
  130. insert(std::make_pair(key, ValueType()));
  131. return (--Pairs.end())->second;
  132. }
  133. }
  134.  
  135. const ValueType& at(const KeyType& key) const {
  136. if (find(key) != Pairs.end()) {
  137. size_t hash = hasher(key) % 765322;
  138. for (auto i : Data[hash]) {
  139. if ((*i).first == key) {
  140. return (*i).second;
  141. }
  142. }
  143. }
  144. else {
  145. throw std::out_of_range("I HATE PCFS");
  146. }
  147. }
  148.  
  149. iterator find(const KeyType& key) {
  150. size_t hash = hasher(key) % 765322;
  151. for (auto it : Data[hash]) {
  152. if ((*it).first == key)
  153. return it;
  154. }
  155. return Pairs.end();
  156. }
  157.  
  158. const_iterator find(const KeyType& key) const {
  159. size_t hash = hasher(key) % 765322;
  160. for (auto it : Data[hash]) {
  161. if ((*it).first == key)
  162. return it;
  163. }
  164. return Pairs.cend();
  165. }
  166. };
  167.  
  168. std::string check(HashMap<int, int> a, std::unordered_map<int, int> b) {
  169. if (a.empty() && !b.empty()) {
  170. return "a is empty, b is not";
  171. }
  172. if (!a.empty() && b.empty()) {
  173. return "b is empty, a is not";
  174. }
  175. if (a.size() > b.size()) {
  176. return "a is bigger than b";
  177. }
  178. if (b.size() > a.size()) {
  179. return "b is bigger than a";
  180. }
  181. for (auto i : a) {
  182. if (b.find(i.first) == b.end()) {
  183. std::string err = "b does not have ";
  184. err += i.first;
  185. }
  186. if (b[i.first] != i.second) {
  187. std::string err = "b has another value for ";
  188. err += i.first;
  189. }
  190. }
  191. for (auto i : b) {
  192. if (a.find(i.first) == a.end()) {
  193. std::string err = "a does not have ";
  194. err += i.first;
  195. }
  196. if (a[i.first] != i.second) {
  197. std::string err = "a has another value for ";
  198. err += i.first;
  199. }
  200. }
  201. return "OK!";
  202. }
  203.  
  204.  
  205.  
  206. int main() {
  207. const size_t commands = 1001;
  208. const size_t maxrand = 603;
  209. const size_t maxnum = 200;
  210. srand(time(0));
  211. clock_t timer = clock();
  212. HashMap<int, int> a{ { 1,3 },{ 4,2 },{ 3,5 } };
  213. std::unordered_map<int, int> b{ { 1,3 },{ 4,2 },{ 3,5 } };
  214. for (size_t i = 0; i < commands; ++i) {
  215. size_t num = i;
  216. if (num < 100) {
  217. int key = rand() % maxnum - maxnum / 2;
  218. int value = rand() % maxnum - maxnum / 2;
  219. //std::cout << "Adding " << key << " " << value << std::endl;
  220. a[key] = value;
  221. //std::cout << a[key];
  222. b[key] = value;
  223. }
  224. else if (num < 500) {
  225. int key = rand() % maxnum - maxnum / 2;
  226. int value = rand() % maxnum - maxnum / 2;
  227. //std::cout << "Inserting " << key << " " << value << std::endl;
  228. a.insert(std::pair<int, int>(key, value));
  229. b.insert(std::pair<int, int>(key, value));
  230. }
  231. else if (num < 1000) {
  232. int key = rand() % maxnum - maxnum / 2;
  233. std::cout << "Erasing " << key << std::endl;
  234. a.erase(key);
  235. b.erase(key);
  236. }
  237. else {
  238. std::cout << "Clearing ";
  239. a.clear();
  240. b.clear();
  241. }
  242. std::string s = check(a, b);
  243. //std::cout << s << std::endl;
  244. if (s != "OK!") { throw(-1); }
  245. }
  246. std::cout << (float)(clock() - timer) / CLOCKS_PER_SEC;
  247. std::cout << std::endl;
  248. return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement