10 #ifndef TEUCHOS_FILTERED_ITERATOR_HPP
11 #define TEUCHOS_FILTERED_ITERATOR_HPP
14 #include "Teuchos_Assert.hpp"
16 #include "Teuchos_Exceptions.hpp"
27 template<
class IteratorType,
class Predicate>
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;
57 Predicate pred_in = Predicate()
59 :current_(current_in), begin_(begin_in), end_(end_in), pred_(pred_in)
60 { advanceForwardToValid(); }
62 template<
class IteratorType2,
class Predicate2>
67 template<
class IteratorType2,
class Predicate2>
87 {
return current_.operator->(); }
97 assertNotIterateForwardPastEnd();
99 advanceForwardToValid();
112 assertNotIterateBackwardPastBegin();
114 advanceBackwardToValid();
131 IteratorType
current()
const {
return current_; }
133 IteratorType
begin()
const {
return begin_; }
135 IteratorType
end()
const {
return end_; }
137 Predicate
pred()
const{
return pred_; }
144 IteratorType current_;
155 void advanceForwardToValid();
157 void advanceBackwardToValid();
159 void assertNotIterateForwardPastEnd()
160 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
166 void assertNotIterateBackwardPastBegin()
167 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
179 template<
class IteratorType,
class Predicate>
190 template<
class IteratorType,
class Predicate>
204 template<
class IteratorType,
class Predicate>
205 std::ostream& operator<<(std::ostream &out, const FilteredIterator<IteratorType,Predicate>& itr)
216 template<
class IteratorType,
class Predicate>
219 while (current_ != end_ && !pred_(*current_)) {
225 template<
class IteratorType,
class Predicate>
226 void FilteredIterator<IteratorType,Predicate>::advanceBackwardToValid()
228 while (current_ != begin_ && !pred_(*current_)) {
234 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
237 template<
class IteratorType,
class Predicate>
238 void FilteredIterator<IteratorType,Predicate>::assertNotIterateForwardPastEnd()
240 const bool current_is_at_end = (current_ == end_);
242 "Error, trying to iterate " << *
this <<
" forward ++ past end!");
246 template<
class IteratorType,
class Predicate>
247 void FilteredIterator<IteratorType,Predicate>::assertNotIterateBackwardPastBegin()
249 const bool current_is_at_begin = (current_ == begin_);
251 "Error, trying to iterate " << *
this <<
" backward -- past begin!");
255 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
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.
IteratorType current() const
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
IteratorType begin() const
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->member
static std::string name()