playerr17

Untitled

Jan 19th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #ifndef MY_OWN_HASH_TABLE_HASH_MAP_H
  2. #define MY_OWN_HASH_TABLE_HASH_MAP_H
  3.  
  4. #include <unordered_map>
  5. #include <list>
  6. #include <iterator>
  7. #include <utility>
  8.  
  9. template<class KeyType, class ValueType, class Hash = std::hash<KeyType> >
  10. class HashMap {
  11. public:
  12.     class iterator {
  13.     public:
  14.         iterator() = default;
  15.  
  16.         explicit iterator(typename std::unordered_map<KeyType, ValueType, Hash>::iterator it) : it_(it) {}
  17.  
  18.         iterator& operator++() {
  19.             ++it_;
  20.             return *this;
  21.         }
  22.  
  23.         iterator operator++(int) {
  24.             iterator tmp = *this;
  25.             ++it_;
  26.             return tmp;
  27.         }
  28.  
  29.         std::pair<const KeyType, ValueType>& operator*() {
  30.             return *it_;
  31.         }
  32.  
  33.         std::pair<const KeyType, ValueType>* operator->() {
  34.             return &*it_;
  35.         }
  36.  
  37.         bool operator==(const iterator& other) {
  38.             return other.it_ == it_;
  39.         }
  40.  
  41.         bool operator!=(const iterator& other) {
  42.             return !(*this == other);
  43.         }
  44.  
  45.     private:
  46.         typename std::unordered_map<KeyType, ValueType, Hash>::iterator it_;
  47.  
  48.     };
  49.  
  50.     class const_iterator {
  51.     public:
  52.         const_iterator() = default;
  53.  
  54.         explicit const_iterator(typename std::unordered_map<KeyType, ValueType, Hash>::const_iterator it) : it_(it) {}
  55.  
  56.         const_iterator& operator++() {
  57.             ++it_;
  58.             return *this;
  59.         }
  60.  
  61.         const_iterator operator++(int) {
  62.             const_iterator tmp = *this;
  63.             ++it_;
  64.             return tmp;
  65.         }
  66.  
  67.         const std::pair<const KeyType, ValueType>& operator*() {
  68.             return *it_;
  69.         }
  70.  
  71.         const std::pair<const KeyType, ValueType>* operator->() {
  72.             return &*it_;
  73.         }
  74.  
  75.         bool operator==(const const_iterator& other) {
  76.             return other.it_ == it_;
  77.         }
  78.  
  79.         bool operator!=(const const_iterator& other) {
  80.             return !(*this == other);
  81.         }
  82.  
  83.     private:
  84.         typename std::unordered_map<KeyType, ValueType, Hash>::const_iterator it_;
  85.  
  86.     };
  87.  
  88.     explicit HashMap(Hash hasher = Hash()) : map_(1, hasher) {}
  89.  
  90.     template <class InputIterator>
  91.     HashMap(InputIterator begin,
  92.             InputIterator end,
  93.             Hash hasher = Hash()) : map_(1, hasher) {
  94.         for (auto it = begin; it != end; ++it) {
  95.             map_.insert(*it);
  96.         }
  97.     }
  98.  
  99.     HashMap(std::initializer_list<std::pair<KeyType, ValueType>> list, Hash hasher = Hash()) : map_(1, hasher) {
  100.         for (auto &item : list) {
  101.             map_.insert(item);
  102.         }
  103.     }
  104.  
  105.     size_t size() const {
  106.         return map_.size();
  107.     }
  108.  
  109.     bool empty() const {
  110.         return map_.empty();
  111.     }
  112.  
  113.     Hash hash_function() const {
  114.         return map_.hash_function();
  115.     }
  116.  
  117.     void insert(std::pair<KeyType, ValueType> element) {
  118.         map_.insert(element);
  119.     }
  120.  
  121.     void erase(KeyType key) {
  122.         map_.erase(key);
  123.     }
  124.  
  125.     const_iterator find(KeyType key) const {
  126.         return const_iterator(map_.find(key));
  127.     }
  128.  
  129.     iterator find(KeyType key) {
  130.         return iterator(map_.find(key));
  131.     }
  132.  
  133.     ValueType &operator[](KeyType key) {
  134.         return map_[key];
  135.     }
  136.  
  137.     const ValueType &at(KeyType key) const {
  138.         return map_.at(key);
  139.     }
  140.  
  141.     iterator begin() {
  142.         return iterator(map_.begin());
  143.     }
  144.  
  145.     iterator end() {
  146.         return iterator(map_.end());
  147.     }
  148.  
  149.     const_iterator begin() const {
  150.         return const_iterator(map_.cbegin());
  151.     }
  152.  
  153.     const_iterator end() const {
  154.         return const_iterator(map_.cend());
  155.     }
  156.  
  157.     void clear() {
  158.         map_.clear();
  159.     }
  160.  
  161. private:
  162.     std::unordered_map<KeyType, ValueType, Hash> map_;
  163.  
  164. };
  165.  
  166. #endif //MY_OWN_HASH_TABLE_HASH_MAP_H
  167.  
  168.  
  169.  
Add Comment
Please, Sign In to add comment