OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
FilterIterator.hpp
Go to the documentation of this file.
1 #ifndef FOCS_FILTERITERATOR
2 #define FOCS_FILTERITERATOR
3 
4 #include <iterator>
5 #include <string>
6 #include <unordered_map>
7 #include <vector>
8 
9 namespace focs {
26 template <class InputIterator, class UnaryPredicate>
28 public:
37  FilterIterator(InputIterator first, InputIterator last, UnaryPredicate pred) : first_{first}, it_{first}, last_{last}, pred_{pred} {
38  while (it_ != last_) {
39  if (pred_(*it_)) {
40  break;
41  }
42  it_++;
43  }
44  }
45 
47  bool operator!=(const FilterIterator& it) {
48  return it.it_ != this->it_;
49  }
50 
52  bool operator==(const FilterIterator& it) {
53  return it.it_ == this->it_;
54  }
55 
57  InputIterator operator*() {
58  return it_;
59  }
60 
63  while (it_ != last_) {
64  it_++;
65  if (it_ == last_ || pred_(*it_)) {
66  return *this;
67  }
68  }
69  return end();
70  }
71 
73  FilterIterator begin() const {return FilterIterator(first_, last_, pred_);}
75  FilterIterator end() const {return FilterIterator(last_, last_, pred_);}
76 private:
77  const InputIterator first_;
78  InputIterator it_;
79  const InputIterator last_;
80  const UnaryPredicate pred_;
81 };
82 } // namespace focs
83 
84 #endif // FOCS_FILTERITERATOR
FilterIterator operator++()
Find the next value that fulfills the UnaryPredicate's condition, or return end().
InputIterator operator*()
Return the underlying iterator.
bool operator==(const FilterIterator &it)
Check if the underlying iterators are equal.
FilterIterator(InputIterator first, InputIterator last, UnaryPredicate pred)
Sole and cumbersome constructor.
Iterator for filtering the results of other iterators.
bool operator!=(const FilterIterator &it)
Check if the underlying iterators are not equal.
FilterIterator begin() const
Return a copy of the iterator reset back to the start.
FilterIterator end() const
Return an iterator with no values left to check.