libzypp  17.38.7
iterators.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_CORE_NG_BASE_ITERATORS_H_INCLUDED
13 #define ZYPP_CORE_NG_BASE_ITERATORS_H_INCLUDED
14 
15 #include <iterator>
16 #include <utility>
17 
18 namespace zyppng {
19 
38  template<class Pred, class Base>
40  public:
41  using iterator_category = std::forward_iterator_tag;
42  using value_type = typename std::iterator_traits<Base>::value_type;
43  using difference_type = typename std::iterator_traits<Base>::difference_type;
44  using pointer = typename std::iterator_traits<Base>::pointer;
45  using reference = typename std::iterator_traits<Base>::reference;
46 
48  FilterIterator() = default;
49 
56  FilterIterator(Pred p, Base it, Base end)
57  : _pred(std::move(p)), _it(std::move(it)), _end(std::move(end))
58  {
60  }
61 
63  reference operator*() const { return *_it; }
64 
66  pointer operator->() const { return &(*_it); }
67 
70  ++_it;
72  return *this;
73  }
74 
77  FilterIterator tmp = *this;
78  ++(*this);
79  return tmp;
80  }
81 
83  bool operator==(const FilterIterator& other) const {
84  return _it == other._it;
85  }
86 
88  bool operator!=(const FilterIterator& other) const {
89  return !(*this == other);
90  }
91 
93  Base const & base() const { return _it; }
94 
95  private:
98  while (_it != _end && !_pred(*_it)) {
99  ++_it;
100  }
101  }
102 
103  Pred _pred;
106  };
107 
115  template<class Pred, class Base>
117  return FilterIterator<Pred, Base>(std::move(p), std::move(it), std::move(end));
118  }
119 
120 } // namespace zyppng
121 
122 #endif
FilterIterator(Pred p, Base it, Base end)
Constructs a FilterIterator.
Definition: iterators.h:56
A simple forward iterator that filters a base range according to a predicate.
Definition: iterators.h:39
typename std::iterator_traits< Base >::difference_type difference_type
Definition: iterators.h:43
Definition: ansi.h:854
typename std::iterator_traits< Base >::pointer pointer
Definition: iterators.h:44
bool operator!=(const FilterIterator &other) const
Inequality comparison.
Definition: iterators.h:88
pointer operator->() const
Arrow operator.
Definition: iterators.h:66
typename std::iterator_traits< Base >::reference reference
Definition: iterators.h:45
void satisfy_predicate()
Advances the internal iterator until the predicate is met or _end is reached.
Definition: iterators.h:97
FilterIterator operator++(int)
Post-increment operator.
Definition: iterators.h:76
bool operator==(const FilterIterator &other) const
Equality comparison.
Definition: iterators.h:83
std::forward_iterator_tag iterator_category
Definition: iterators.h:41
FilterIterator< Pred, Base > make_filter_iterator(Pred p, Base it, Base end)
Helper function to deduce types and construct a FilterIterator.
Definition: iterators.h:116
FilterIterator()=default
Default constructor.
reference operator*() const
Dereference operator.
Definition: iterators.h:63
typename std::iterator_traits< Base >::value_type value_type
Definition: iterators.h:42
Base const & base() const
Returns the underlying base iterator at its current position.
Definition: iterators.h:93
FilterIterator & operator++()
Pre-increment operator.
Definition: iterators.h:69