Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement