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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_FILTERED_ITERATOR_HPP
43 #define TEUCHOS_FILTERED_ITERATOR_HPP
44 
45 
46 #include "Teuchos_Assert.hpp"
48 #include "Teuchos_Exceptions.hpp"
49 
50 
51 namespace Teuchos {
52 
53 
59 template<class IteratorType, class Predicate>
61 public:
62 
65 
67  typedef std::bidirectional_iterator_tag iterator_category;
69  typedef typename std::iterator_traits<IteratorType>::value_type value_type;
71  typedef typename std::iterator_traits<IteratorType>::reference reference;
73  typedef typename std::iterator_traits<IteratorType>::pointer pointer;
75  typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
76 
78 
81 
84  {}
88  FilteredIterator(IteratorType current_in, IteratorType begin_in, IteratorType end_in,
89  Predicate pred_in = Predicate()
90  )
91  :current_(current_in), begin_(begin_in), end_(end_in), pred_(pred_in)
92  { advanceForwardToValid(); }
94  template<class IteratorType2, class Predicate2>
96  :current_(rhs.current()), begin_(rhs.begin()), end_(rhs.end()), pred_(rhs.pred())
97  {}
99  template<class IteratorType2, class Predicate2>
101  {
102  current_ = rhs.current();
103  begin_ = rhs.begin();
104  end_ = rhs.end();
105  pred_ = rhs.pred();
106  return *this;
107  }
108 
110 
113 
116  { return *current_; }
119  { return current_.operator->(); }
120 
122 
125 
128  {
129  assertNotIterateForwardPastEnd();
130  ++current_;
131  advanceForwardToValid();
132  return *this;
133  }
136  {
137  FilteredIterator tmp = *this;
138  ++*this;
139  return tmp;
140  }
143  {
144  assertNotIterateBackwardPastBegin();
145  --current_;
146  advanceBackwardToValid();
147  return *this;
148  }
151  {
152  FilteredIterator tmp = *this;
153  --*this;
154  return tmp;
155  }
156 
158 
161 
163  IteratorType current() const { return current_; }
165  IteratorType begin() const { return begin_; }
167  IteratorType end() const { return end_; }
169  Predicate pred() const{ return pred_; }
170 
172 
173 private: // Data members
174 
176  IteratorType current_;
178  IteratorType begin_;
180  IteratorType end_;
182  Predicate pred_;
183 
184 private: // Functions
185 
187  void advanceForwardToValid();
189  void advanceBackwardToValid();
191  void assertNotIterateForwardPastEnd()
192 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
193  {}
194 #else
195  ;
196 #endif
197 
198  void assertNotIterateBackwardPastBegin()
199 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
200  {}
201 #else
202  ;
203 #endif
204 
205 };
206 
207 
211 template<class IteratorType, class Predicate>
214 {
215  return itr1.current() == itr2.current();
216 }
217 
218 
222 template<class IteratorType, class Predicate>
225 {
226  return itr1.current() != itr2.current();
227 }
228 
229 
236 template<class IteratorType, class Predicate>
237 std::ostream& operator<<(std::ostream &out, const FilteredIterator<IteratorType,Predicate>& itr)
238 {
239  out << "FilteredIterator{current=???, end=???, pred="<<TypeNameTraits<Predicate>::name()<<"}";
240  return out;
241 }
242 
243 //
244 // Template definitions
245 //
246 
247 
248 template<class IteratorType, class Predicate>
250 {
251  while (current_ != end_ && !pred_(*current_)) {
252  ++current_;
253  }
254 }
255 
256 
257 template<class IteratorType, class Predicate>
258 void FilteredIterator<IteratorType,Predicate>::advanceBackwardToValid()
259 {
260  while (current_ != begin_ && !pred_(*current_)) {
261  --current_;
262  }
263 }
264 
265 
266 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
267 
268 
269 template<class IteratorType, class Predicate>
270 void FilteredIterator<IteratorType,Predicate>::assertNotIterateForwardPastEnd()
271 {
272  const bool current_is_at_end = (current_ == end_);
273  TEUCHOS_TEST_FOR_EXCEPTION( current_is_at_end, RangeError,
274  "Error, trying to iterate " << *this << " forward ++ past end!");
275 }
276 
277 
278 template<class IteratorType, class Predicate>
279 void FilteredIterator<IteratorType,Predicate>::assertNotIterateBackwardPastBegin()
280 {
281  const bool current_is_at_begin = (current_ == begin_);
282  TEUCHOS_TEST_FOR_EXCEPTION( current_is_at_begin, RangeError,
283  "Error, trying to iterate " << *this << " backward -- past begin!");
284 }
285 
286 
287 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
288 
289 
290 } // end namespace Teuchos
291 
292 
293 #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
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