libzypp 17.38.15

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>
39 class FilterIterator {
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 {
59 satisfy_predicate();
60 }
61
63 reference operator*() const { return *_it; }
64
66 pointer operator->() const { return &(*_it); }
67
69 FilterIterator& operator++() {
70 ++_it;
71 satisfy_predicate();
72 return *this;
73 }
74
76 FilterIterator operator++(int) {
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:
97 void satisfy_predicate() {
98 while (_it != _end && !_pred(*_it)) {
99 ++_it;
100 }
101 }
102
103 Pred _pred;
104 Base _it;
105 Base _end;
106 };
107
115 template<class Pred, class Base>
116 FilterIterator<Pred, Base> make_filter_iterator(Pred p, Base it, Base end) {
117 return FilterIterator<Pred, Base>(std::move(p), std::move(it), std::move(end));
118 }
119
120} // namespace zyppng
121
122#endif
zyppng::Base
Definition base.h:61
zyppng::FilterIterator
A simple forward iterator that filters a base range according to a predicate.
Definition iterators.h:39
zyppng::FilterIterator::_end
Base _end
Definition iterators.h:105
zyppng::FilterIterator::base
Base const & base() const
Returns the underlying base iterator at its current position.
Definition iterators.h:93
zyppng::FilterIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition iterators.h:41
zyppng::FilterIterator::_it
Base _it
Definition iterators.h:104
zyppng::FilterIterator::operator==
bool operator==(const FilterIterator &other) const
Equality comparison.
Definition iterators.h:83
zyppng::FilterIterator::FilterIterator
FilterIterator()=default
Default constructor.
zyppng::FilterIterator::operator!=
bool operator!=(const FilterIterator &other) const
Inequality comparison.
Definition iterators.h:88
zyppng::FilterIterator::satisfy_predicate
void satisfy_predicate()
Advances the internal iterator until the predicate is met or _end is reached.
Definition iterators.h:97
zyppng::FilterIterator::operator*
reference operator*() const
Dereference operator.
Definition iterators.h:63
zyppng::FilterIterator::value_type
typename std::iterator_traits< Base >::value_type value_type
Definition iterators.h:42
zyppng::FilterIterator::operator++
FilterIterator & operator++()
Pre-increment operator.
Definition iterators.h:69
zyppng::FilterIterator::reference
typename std::iterator_traits< Base >::reference reference
Definition iterators.h:45
zyppng::FilterIterator::FilterIterator
FilterIterator(Pred p, Base it, Base end)
Constructs a FilterIterator.
Definition iterators.h:56
zyppng::FilterIterator::_pred
Pred _pred
Definition iterators.h:103
zyppng::FilterIterator::operator->
pointer operator->() const
Arrow operator.
Definition iterators.h:66
zyppng::FilterIterator::pointer
typename std::iterator_traits< Base >::pointer pointer
Definition iterators.h:44
zyppng::FilterIterator::difference_type
typename std::iterator_traits< Base >::difference_type difference_type
Definition iterators.h:43
zyppng::FilterIterator::operator++
FilterIterator operator++(int)
Post-increment operator.
Definition iterators.h:76
std
Definition ansi.h:855
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::make_filter_iterator
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