Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<list>
  4. #include<utility>
  5. #include<stdexcept>
  6.  
  7. using namespace std;
  8.  
  9. template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
  10. class HashMap {
  11. public:
  12.    
  13.     //1
  14.     HashMap(const Hash& hash = Hash()) : Hasher(hash) {
  15.             clear();
  16.         }
  17.    
  18.     //2
  19.     template<class Iter>
  20.     HashMap(Iter begin, Iter end, const Hash& hasher = Hash()) :
  21.         HashMap(hasher)
  22.     {
  23.         while (begin != end) {
  24.             insert(*begin);
  25.             ++begin;
  26.         }
  27.     }
  28.    
  29.     //3 , 4  
  30.     HashMap(const std::initializer_list<std::pair<KeyType, ValueType> >& list,
  31.             const Hash& hasher = Hash()) :
  32.         HashMap(hasher)
  33.     {
  34.         for (auto now : list) {
  35.             insert(now);
  36.         }
  37.     }
  38.    
  39.     //5
  40.     size_t size() const {
  41.         return Elements;
  42.     }
  43.    
  44.     bool empty() const {
  45.         return !Elements;
  46.     }
  47.    
  48.     //6
  49.     Hash hash_function() const {
  50.         return Hasher;
  51.     }
  52.    
  53.     //7
  54.     void insert(const pair<KeyType, ValueType>& a) {
  55.         if (5* static_cast<long long>(Elements + 1) > 3*static_cast<long long>(Size)) {
  56.             expand();
  57.         }
  58.         size_t i = hash_count(a.first);
  59.         if (Table[i].first == Data.end()) {
  60.             Data.push_front(a);
  61.             Table[i].first = Data.begin();
  62.             ++Table[i].second;
  63.             ++Elements;        
  64.         } else {
  65.             auto it = Table[i].first;
  66.             while (it != Data.end() && hash_count(it->first) == i) {
  67.                 if (it->first == a.first) {
  68.                     return;                    
  69.                 }
  70.                 ++it;
  71.             }
  72.             Data.insert(it, a);
  73.             ++Table[i].second;
  74.             ++Elements;
  75.         }
  76.     }
  77.    
  78.     //8
  79.     void erase(const KeyType& x) {
  80.         size_t i = hash_count(x);        
  81.         auto it = Table[i].first;
  82.         if (it != Data.end()) {
  83.             while (it != Data.end() && hash_count(it->first) == i) {
  84.                 if (it->first == x) {
  85.                     if (Table[i].second == 1) {
  86.                         Table[i].first = Data.end();
  87.                         Table[i].second = 0;
  88.                     }
  89.                     Data.erase(it);
  90.                     --Elements;
  91.                     return;                    
  92.                 }
  93.                 ++it;
  94.             }
  95.         }
  96.     }
  97.    
  98.     //9
  99.     using iterator = typename list<pair<const KeyType, ValueType>>::iterator;
  100.     using const_iterator = typename list<pair<const KeyType, ValueType>>::const_iterator;
  101.    
  102.     iterator begin() {
  103.         return Data.begin();
  104.     }
  105.  
  106.     const_iterator begin() const {
  107.         return Data.begin();
  108.     }
  109.  
  110.     iterator end() {
  111.         return Data.end();
  112.     }
  113.  
  114.     const_iterator end() const {
  115.         return Data.end();
  116.     }
  117.    
  118.     //10
  119.     iterator find(const KeyType& x) {
  120.         size_t i = hash_count(x);        
  121.         iterator it = Table[i].first;
  122.         if (Table[i].first != Data.end()) {
  123.             while (it != Data.end() && hash_count(it->first) == i) {
  124.                 if (it->first == x) {
  125.                     return it;                    
  126.                 }
  127.                 ++it;
  128.             }
  129.         }
  130.         return (Data.end());
  131.     }
  132.    
  133.     const_iterator find(const KeyType& x) const {
  134.         size_t i = hash_count(x);        
  135.         const_iterator it = Table[i].first;
  136.         if (Table[i].first != Data.end()) {
  137.             while (it != Data.end() && hash_count(it->first) == i) {
  138.                 if (it->first == x) {
  139.                     return it;                    
  140.                 }
  141.                 ++it;
  142.             }
  143.         }
  144.         return (Data.end());
  145.     }
  146.    
  147.     //11
  148.     ValueType& operator [] (const KeyType& x) {
  149.         auto it = find(x);
  150.         if (it == Data.end()) {
  151.             insert({x, ValueType()});
  152.  
  153.             return find(x)->second;
  154.         } else {
  155.             return it->second;
  156.         }
  157.     }
  158.    
  159.     //12
  160.     const ValueType& at(const KeyType& x) const {
  161.         auto it = find(x);
  162.         if (it == Data.end()) {
  163.             throw std::out_of_range("I do not have dis key\n");
  164.         } else {
  165.             return it->second;
  166.         }
  167.     }
  168.    
  169.     //13  
  170.     void clear() {
  171.         Data.clear();
  172.         Data.resize(0);
  173.         Table.clear();
  174.         Table.resize(1,make_pair(Data.end(), 0));
  175.         Size = 1;
  176.         Elements = 0;
  177.     }
  178.    
  179.    
  180.    HashMap& operator = (const HashMap other) {
  181.         clear();
  182.         for (auto now : other) {
  183.             insert(now);
  184.         }
  185.         return *this;
  186.     }
  187.    
  188.     void print() {
  189.         cout << "Таблица содержит:\n";
  190.         for (auto now : Data) {
  191.             cout <<"элемент " << now.first << " " << now.second << "\n";  
  192.         }
  193.     }
  194.    
  195.  
  196.     vector<pair <typename list< pair<const KeyType, ValueType>>::iterator,size_t>> Table;
  197.     list<pair<const KeyType, ValueType>> Data;
  198.     Hash Hasher;
  199.     size_t Size, Elements;
  200.    
  201.    
  202.  
  203.     void expand() {
  204.         vector<pair<const KeyType, ValueType> > CurPairs;
  205.         for (auto a : Data) {
  206.                 CurPairs.emplace_back(a);
  207.         }
  208.         size_t nSize = 2*Size;
  209.         clear();
  210.         Table.resize(nSize, make_pair(Data.end(),0));
  211.         Size = nSize;
  212.         for (auto now : CurPairs) {
  213.             insert(now);
  214.         }
  215.     }
  216.    
  217.  
  218.    
  219.     size_t hash_count(const KeyType& key) const {
  220.         return Hasher(key)%Size;
  221.     }
  222.    
  223.    
  224. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement