Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. #ifndef hash_map_h
  2. #define hash_map_h
  3.  
  4.  
  5. #endif
  6.  
  7. #include <iostream>
  8. #include <functional>
  9. #include <list>
  10. #include <vector>
  11. #include <stdexcept>
  12.  
  13. template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
  14. class HashMap {
  15. public:
  16. using const_iterator = typename std::list<std::pair<const KeyType, ValueType>>::const_iterator;
  17. using iterator = typename std::list<std::pair<const KeyType, ValueType>>::iterator;
  18.  
  19. HashMap() {
  20. table.resize(8, lst.end());
  21. hasher = std::hash<KeyType>();
  22. way = 0;
  23. }
  24.  
  25. template<class Iterator>
  26. HashMap(Iterator left, Iterator right, Hash hasher = std::hash<KeyType>()) : hasher(hasher) {
  27. while (left != right) {
  28. insert(*left);
  29. ++left;
  30. }
  31. }
  32.  
  33. HashMap(const std::initializer_list<std::pair<KeyType, ValueType>>& list,
  34. Hash hasher = std::hash<KeyType>()) : hasher(hasher) {
  35. for (auto x : list)
  36. insert(x);
  37. }
  38.  
  39. HashMap(const HashMap<KeyType, ValueType>& other) {
  40. for (auto x : other)
  41. insert(x);
  42. }
  43.  
  44. HashMap<KeyType, ValueType, Hash>& operator=(const HashMap<KeyType, ValueType>& other) {
  45. for (auto x : other)
  46. insert(x);
  47. return *this;
  48. }
  49.  
  50. size_t size() const {
  51. return lst.size();
  52. }
  53.  
  54. bool empty() const {
  55. return size() == 0;
  56. }
  57.  
  58. Hash hash_function() const {
  59. return hasher;
  60. }
  61.  
  62. void erase(const KeyType& el) {
  63. size_t i = hasher(el) % table.size();
  64. if (table[i] == lst.end()) {
  65. return;
  66. } else {
  67. auto it = table[i];
  68. while (it != lst.end() && hasher(it->first) % table.size() == i) {
  69. if (it->first == el) {
  70. if (it == table[i]) {
  71. auto nextIt = it;
  72. ++nextIt;
  73. if (nextIt == lst.end()) {
  74. table[i] = lst.end();
  75. } else {
  76. if (hasher(nextIt->first) % table.size() == i)
  77. table[i] = nextIt;
  78. else
  79. table[i] = lst.end();
  80. }
  81. }
  82. lst.erase(it);
  83. return;
  84. }
  85. ++it;
  86. }
  87. }
  88. return;
  89. }
  90.  
  91. void insert(const std::pair<const KeyType, ValueType>& el, bool fl = true) {
  92. if (fl && way > MAX_WAY)
  93. recalc();
  94.  
  95. size_t i = hasher(el.first) % table.size();
  96. if (table[i] == lst.end()) {
  97. lst.push_front(el);
  98. table[i] = lst.begin();
  99. } else {
  100. size_t curWay = 0;
  101. auto it = table[i];
  102. while (it != lst.end() && hasher(it->first) % table.size() == i) {
  103. if (it->first == el.first)
  104. break;
  105. ++it;
  106. ++curWay;
  107. }
  108. way = std::max(curWay, way);
  109. }
  110. }
  111.  
  112. iterator<KeyType, ValueType> begin() {
  113. return lst.begin();
  114. }
  115.  
  116. const_iterator<KeyType, ValueType> begin() const {
  117. return lst.begin();
  118. }
  119.  
  120. iterator<KeyType, ValueType> end() {
  121. return lst.end();
  122. }
  123.  
  124. const_iterator<KeyType, ValueType> end() const {
  125. return lst.end();
  126. }
  127.  
  128. iterator<KeyType, ValueType> find(const KeyType& el) {
  129. size_t i = hasher(el) % table.size();
  130. if (table[i] == lst.end()) {
  131. return lst.end();
  132. } else {
  133. auto it = table[i];
  134. while (it != lst.end() && hasher(it->first) % table.size() == i) {
  135. if (it->first == el)
  136. return it;
  137. ++it;
  138. }
  139. }
  140. return l.end();
  141. }
  142.  
  143. const_iterator<KeyType, ValueType> find(const KeyType& el) const {
  144. auto it = find(el);
  145. return it;
  146. }
  147.  
  148. ValueType& operator[](const KeyType& key) {
  149. auto it = find(key);
  150. if (it == end()) {
  151. insert(std::make_pair(key, 0));
  152. return (find(key))->second;
  153. } else {
  154. return it->second;
  155. }
  156. }
  157.  
  158. const ValueType& at(const KeyType& key) const {
  159. auto it = find(key);
  160. if (it == end())
  161. throw std::out_of_range("out_of_range");
  162.  
  163. return (find(key))->second;
  164. }
  165.  
  166. void clear() {
  167. table.clear();
  168. lst.clear();
  169. table.resize(8, lst.end());
  170. }
  171.  
  172. private:
  173. std::vector<typename std::list<std::pair<const KeyType, ValueType>>::iterator> table;
  174. std::list<std::pair<const KeyType, ValueType>> lst;
  175. Hash hasher;
  176. size_t way;
  177. size_t MAX_WAY = 50;
  178.  
  179. void recalc() {
  180. std::vector<std::pair<const KeyType, ValueType>> tmp;
  181. for (auto x : lst)
  182. tmp.push_back(x);
  183.  
  184. lst.clear();
  185. table.resize(table.size() * 2, lst.end());
  186. for (auto x : tmp)
  187. insert(x, false);
  188. }
  189. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement