retdec
filter_iterator.h
Go to the documentation of this file.
1 
8 #ifndef RETDEC_UTILS_FILTER_ITERATOR_H
9 #define RETDEC_UTILS_FILTER_ITERATOR_H
10 
11 #include <functional>
12 #include <iterator>
13 #include <utility>
14 
15 namespace retdec {
16 namespace utils {
17 
39 template<typename Iterator>
41 public:
42  // Standard typedefs.
43  using value_type = typename std::iterator_traits<Iterator>::value_type;
44  using reference = typename std::iterator_traits<Iterator>::reference;
45  using pointer = typename std::iterator_traits<Iterator>::pointer;
46  using difference_type = typename std::iterator_traits<Iterator>::difference_type;
47  using iterator_category = std::forward_iterator_tag;
48 
49 public:
58  template<typename Predicate>
59  FilterIterator(Iterator begin, Iterator end, Predicate &&predicate):
60  current(std::move(begin)),
61  end(std::move(end)),
62  predicate(std::forward<Predicate>(predicate)) {
64  }
65 
78  template<typename Container, typename Predicate>
79  FilterIterator(Container &container, Predicate &&predicate):
80  FilterIterator(std::begin(container), std::end(container),
81  std::forward<Predicate>(predicate)) {}
82 
86  FilterIterator(Iterator end): current(end), end(std::move(end)) {}
87 
88  FilterIterator(const FilterIterator &other) = default;
89 
90  FilterIterator(FilterIterator &&other) = default;
91 
92  ~FilterIterator() = default;
93 
94  FilterIterator &operator=(const FilterIterator &other) = default;
95 
96  reference operator*() const {
97  return *current;
98  }
99 
100  pointer operator->() const {
101  return &*current;
102  }
103 
104  bool operator==(const FilterIterator &other) const {
105  return current == other.current;
106  }
107 
108  bool operator!=(const FilterIterator &other) const {
109  return !(*this == other);
110  }
111 
113  ++current;
115  return *this;
116  }
117 
118 private:
120  while (current != end && !predicate(*current)) {
121  ++current;
122  }
123  }
124 
125 private:
126  Iterator current;
127  Iterator end;
128  std::function<bool (reference)> predicate;
129 };
130 
131 } // namespace utils
132 } // namespace retdec
133 
134 #endif
An adapter of an iterator range in which some elements of the range are skipped.
Definition: filter_iterator.h:40
FilterIterator & operator=(const FilterIterator &other)=default
FilterIterator(Iterator begin, Iterator end, Predicate &&predicate)
Creates an iterator over the given range.
Definition: filter_iterator.h:59
pointer operator->() const
Definition: filter_iterator.h:100
bool operator!=(const FilterIterator &other) const
Definition: filter_iterator.h:108
typename std::iterator_traits< Iterator >::pointer pointer
Definition: filter_iterator.h:45
bool operator==(const FilterIterator &other) const
Definition: filter_iterator.h:104
reference operator*() const
Definition: filter_iterator.h:96
std::forward_iterator_tag iterator_category
Definition: filter_iterator.h:47
typename std::iterator_traits< Iterator >::difference_type difference_type
Definition: filter_iterator.h:46
void skipElementsWhilePredicateIsFalse()
Definition: filter_iterator.h:119
FilterIterator & operator++()
Definition: filter_iterator.h:112
Iterator current
Definition: filter_iterator.h:126
typename std::iterator_traits< Iterator >::reference reference
Definition: filter_iterator.h:44
typename std::iterator_traits< Iterator >::value_type value_type
Definition: filter_iterator.h:43
std::function< bool(reference)> predicate
Definition: filter_iterator.h:128
FilterIterator(FilterIterator &&other)=default
FilterIterator(const FilterIterator &other)=default
FilterIterator(Iterator end)
Creates an end iterator.
Definition: filter_iterator.h:86
FilterIterator(Container &container, Predicate &&predicate)
Creates an iterator over the given container.
Definition: filter_iterator.h:79
Iterator end
Definition: filter_iterator.h:127
Definition: archive_wrapper.h:19