Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_FilteredIterator.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_FILTERED_ITERATOR_HPP
11 #define TEUCHOS_FILTERED_ITERATOR_HPP
12 
13 
14 #include "Teuchos_Assert.hpp"
16 #include "Teuchos_Exceptions.hpp"
17 
18 
19 namespace Teuchos {
20 
21 
27 template<class IteratorType, class Predicate>
29 public:
30 
33 
35  typedef std::bidirectional_iterator_tag iterator_category;
37  typedef typename std::iterator_traits<IteratorType>::value_type value_type;
39  typedef typename std::iterator_traits<IteratorType>::reference reference;
41  typedef typename std::iterator_traits<IteratorType>::pointer pointer;
43  typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
44 
46 
49 
52  {}
56  FilteredIterator(IteratorType current_in, IteratorType begin_in, IteratorType end_in,
57  Predicate pred_in = Predicate()
58  )
59  :current_(current_in), begin_(begin_in), end_(end_in), pred_(pred_in)
60  { advanceForwardToValid(); }
62  template<class IteratorType2, class Predicate2>
64  :current_(rhs.current()), begin_(rhs.begin()), end_(rhs.end()), pred_(rhs.pred())
65  {}
67  template<class IteratorType2, class Predicate2>
69  {
70  current_ = rhs.current();
71  begin_ = rhs.begin();
72  end_ = rhs.end();
73  pred_ = rhs.pred();
74  return *this;
75  }
76 
78 
81 
84  { return *current_; }
87  { return current_.operator->(); }
88 
90 
93 
96  {
97  assertNotIterateForwardPastEnd();
98  ++current_;
99  advanceForwardToValid();
100  return *this;
101  }
104  {
105  FilteredIterator tmp = *this;
106  ++*this;
107  return tmp;
108  }
111  {
112  assertNotIterateBackwardPastBegin();
113  --current_;
114  advanceBackwardToValid();
115  return *this;
116  }
119  {
120  FilteredIterator tmp = *this;
121  --*this;
122  return tmp;
123  }
124 
126 
129 
131  IteratorType current() const { return current_; }
133  IteratorType begin() const { return begin_; }
135  IteratorType end() const { return end_; }
137  Predicate pred() const{ return pred_; }
138 
140 
141 private: // Data members
142 
144  IteratorType current_;
146  IteratorType begin_;
148  IteratorType end_;
150  Predicate pred_;
151 
152 private: // Functions
153 
155  void advanceForwardToValid();
157  void advanceBackwardToValid();
159  void assertNotIterateForwardPastEnd()
160 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
161  {}
162 #else
163  ;
164 #endif
165 
166  void assertNotIterateBackwardPastBegin()
167 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
168  {}
169 #else
170  ;
171 #endif
172 
173 };
174 
175 
179 template<class IteratorType, class Predicate>
182 {
183  return itr1.current() == itr2.current();
184 }
185 
186 
190 template<class IteratorType, class Predicate>
193 {
194  return itr1.current() != itr2.current();
195 }
196 
197 
204 template<class IteratorType, class Predicate>
205 std::ostream& operator<<(std::ostream &out, const FilteredIterator<IteratorType,Predicate>& itr)
206 {
207  out << "FilteredIterator{current=???, end=???, pred="<<TypeNameTraits<Predicate>::name()<<"}";
208  return out;
209 }
210 
211 //
212 // Template definitions
213 //
214 
215 
216 template<class IteratorType, class Predicate>
218 {
219  while (current_ != end_ && !pred_(*current_)) {
220  ++current_;
221  }
222 }
223 
224 
225 template<class IteratorType, class Predicate>
226 void FilteredIterator<IteratorType,Predicate>::advanceBackwardToValid()
227 {
228  while (current_ != begin_ && !pred_(*current_)) {
229  --current_;
230  }
231 }
232 
233 
234 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
235 
236 
237 template<class IteratorType, class Predicate>
238 void FilteredIterator<IteratorType,Predicate>::assertNotIterateForwardPastEnd()
239 {
240  const bool current_is_at_end = (current_ == end_);
241  TEUCHOS_TEST_FOR_EXCEPTION( current_is_at_end, RangeError,
242  "Error, trying to iterate " << *this << " forward ++ past end!");
243 }
244 
245 
246 template<class IteratorType, class Predicate>
247 void FilteredIterator<IteratorType,Predicate>::assertNotIterateBackwardPastBegin()
248 {
249  const bool current_is_at_begin = (current_ == begin_);
250  TEUCHOS_TEST_FOR_EXCEPTION( current_is_at_begin, RangeError,
251  "Error, trying to iterate " << *this << " backward -- past begin!");
252 }
253 
254 
255 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
256 
257 
258 } // end namespace Teuchos
259 
260 
261 #endif // TEUCHOS_FILTERED_ITERATOR_HPP
FilteredIterator(IteratorType current_in, IteratorType begin_in, IteratorType end_in, Predicate pred_in=Predicate())
Construct with iterator and range.
C++ Standard Library compatable filtered iterator.
const FilteredIterator operator--(int)
itr–
const FilteredIterator operator++(int)
itr++
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
std::iterator_traits< IteratorType >::pointer pointer
std::iterator_traits< IteratorType >::difference_type difference_type
std::iterator_traits< IteratorType >::value_type value_type
FilteredIterator()
construct to a null iterator.
bool operator!=(const FilteredIterator< IteratorType, Predicate > &itr1, const FilteredIterator< IteratorType, Predicate > &itr2)
itr1 != itr2.
bool operator==(const FilteredIterator< IteratorType, Predicate > &itr1, const FilteredIterator< IteratorType, Predicate > &itr2)
itr1 == itr2.
FilteredIterator & operator--()
–itr
std::iterator_traits< IteratorType >::reference reference
reference operator*() const
itr*
FilteredIterator & operator=(const FilteredIterator< IteratorType2, Predicate2 > &rhs)
Assign different types of iterators (mainly for non-const to const).
std::bidirectional_iterator_tag iterator_category
FilteredIterator & operator++()
++itr
FilteredIterator(const FilteredIterator< IteratorType2, Predicate2 > &rhs)
Convert type of iterators (mainly for non-const to const).
Defines basic traits returning the name of a type in a portable and readable way. ...
pointer operator->() const
itr-&gt;member