Advertisement
Guest User

iterator

a guest
Nov 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. class allocator;
  2.  
  3.     template<typename K, typename T,
  4.             typename Hash = std::hash<K>,
  5.             typename Pred = std::equal_to<K>,
  6.             typename Alloc = allocator<std::pair<const K, T>>>
  7.     class hash_map;  
  8.  
  9.  
  10.  template<typename ValueType>
  11.     class hash_map_iterator {
  12.     public:
  13.         using iterator_category = std::forward_iterator_tag;
  14.         using value_type = ValueType;
  15.         using difference_type = std::ptrdiff_t;
  16.         using reference = ValueType&;
  17.         using pointer = ValueType*;
  18.  
  19.         hash_map_iterator() noexcept {
  20.  
  21.         }
  22.  
  23.         hash_map_iterator(const hash_map_iterator &other) noexcept {
  24.             point = other.point;
  25.             state = other.state;
  26.             position = other.position;
  27.         }
  28.  
  29.         reference operator*() const {
  30.             return &point + position;
  31.         }
  32.  
  33.         pointer operator->() const {
  34.             return point + position;
  35.         }
  36.  
  37.         // prefix ++
  38.         hash_map_iterator &operator++() {
  39.             int i = 1;
  40.             while (*(state->begin() + i + position) != State::_using) {
  41.                 ++i;
  42.             }
  43.             position += i;
  44.             point = point + position;
  45.         }
  46.         // postfix ++
  47.         hash_map_iterator operator++(int) {
  48.             hash_map_iterator res(*this);
  49.             ++this;
  50.             return res;
  51.         }
  52.  
  53.         friend bool operator==(const hash_map_iterator<ValueType> &a, const hash_map_iterator<ValueType> &b) {
  54.             return a.position == b.position;
  55.         }
  56.  
  57.         friend bool operator!=(const hash_map_iterator<ValueType> &a, const hash_map_iterator<ValueType> &b) {
  58.             return a.position != b.position;
  59.         }
  60.  
  61.         template<typename K, typename T,
  62.                 typename Hash,
  63.                 typename Pred,
  64.                 typename Alloc>
  65.         friend
  66.         class hash_map;
  67.  
  68.     private:
  69.         hash_map_iterator(value_type *map_cell, size_t pos, std::vector<State> *states) noexcept {
  70.             point = map_cell;
  71.             position = pos;
  72.             state = states;
  73.         }
  74.         value_type *point;
  75.         std::vector<State> *state;
  76.         size_t position;
  77.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement