Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
19namespace Teuchos {
20
21
27template<class IteratorType, class Predicate>
29public:
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
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
141private: // Data members
142
144 IteratorType current_;
146 IteratorType begin_;
148 IteratorType end_;
150 Predicate pred_;
151
152private: // Functions
153
155 void advanceForwardToValid();
157 void advanceBackwardToValid();
159 void assertNotIterateForwardPastEnd()
160#ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
161 {}
162#else
163 ;
164#endif
166 void assertNotIterateBackwardPastBegin()
167#ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
168 {}
169#else
170 ;
171#endif
172
173};
174
175
179template<class IteratorType, class Predicate>
182{
183 return itr1.current() == itr2.current();
184}
185
186
190template<class IteratorType, class Predicate>
193{
194 return itr1.current() != itr2.current();
195}
196
197
204template<class IteratorType, class Predicate>
205std::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
216template<class IteratorType, class Predicate>
218{
219 while (current_ != end_ && !pred_(*current_)) {
220 ++current_;
221 }
222}
223
224
225template<class IteratorType, class Predicate>
226void 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
237template<class IteratorType, class Predicate>
238void 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
246template<class IteratorType, class Predicate>
247void 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
Defines basic traits returning the name of a type in a portable and readable way.
C++ Standard Library compatable filtered iterator.
std::iterator_traits< IteratorType >::value_type value_type
std::ostream & operator<<(std::ostream &out, const FilteredIterator< IteratorType, Predicate > &itr)
ostream operator.
std::iterator_traits< IteratorType >::reference reference
FilteredIterator & operator=(const FilteredIterator< IteratorType2, Predicate2 > &rhs)
Assign different types of iterators (mainly for non-const to const).
std::iterator_traits< IteratorType >::difference_type difference_type
FilteredIterator(IteratorType current_in, IteratorType begin_in, IteratorType end_in, Predicate pred_in=Predicate())
Construct with iterator and range.
std::bidirectional_iterator_tag iterator_category
const FilteredIterator operator--(int)
itr–
FilteredIterator(const FilteredIterator< IteratorType2, Predicate2 > &rhs)
Convert type of iterators (mainly for non-const to const).
pointer operator->() const
itr->member
FilteredIterator & operator++()
++itr
FilteredIterator & operator--()
–itr
bool operator!=(const FilteredIterator< IteratorType, Predicate > &itr1, const FilteredIterator< IteratorType, Predicate > &itr2)
itr1 != itr2.
const FilteredIterator operator++(int)
itr++
bool operator==(const FilteredIterator< IteratorType, Predicate > &itr1, const FilteredIterator< IteratorType, Predicate > &itr2)
itr1 == itr2.
std::iterator_traits< IteratorType >::pointer pointer
FilteredIterator()
construct to a null iterator.
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...