[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

filter_iterator.hxx VIGRA

1 /************************************************************************/
2 /* */
3 /* Copyright 2014-2015 by Ullrich Koethe and Philip Schill */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 #ifndef VIGRA_FILTER_ITERATOR_HXX
36 #define VIGRA_FILTER_ITERATOR_HXX
37 
38 #include <type_traits>
39 #include <iterator>
40 
41 #include "iteratorfacade.hxx"
42 
43 namespace vigra
44 {
45 
46 namespace detail
47 {
48  template <typename T>
49  struct is_const_pointer
50  {
51  static bool const value = false;
52  };
53 
54  template <typename T>
55  struct is_const_pointer<T const *>
56  {
57  static bool const value = true;
58  };
59 
60  template <typename ITER>
61  struct is_const_iterator
62  {
63  typedef typename std::iterator_traits<ITER>::pointer pointer;
64  static bool const value = is_const_pointer<pointer>::value;
65  };
66 }
67 
68 
69 /********************************************************/
70 /* */
71 /* FilterIterator */
72 /* */
73 /********************************************************/
74 
75 /**
76  * @brief This iterator creates a view of another iterator and skips elements that
77  * do not fulfill a given predicate.
78  *
79  * The iterator is compatible to an STL forward iterator as defined in the C++ standard.
80  *
81  * @note The equality comparison only checks, whether the iterators point to the same place. The predicate is not checked.
82  */
83 template <typename PREDICATE, typename ITER>
85 : public ForwardIteratorFacade<FilterIterator<PREDICATE, ITER>,
86  typename std::iterator_traits<ITER>::value_type,
87  detail::is_const_iterator<ITER>::value>
88 {
89 public:
90 
91  typedef PREDICATE Predicate;
92  typedef ITER Iter;
93  typedef typename std::iterator_traits<Iter>::value_type IterValueType;
95  typedef ForwardIteratorFacade<SelfType,
96  IterValueType,
97  detail::is_const_iterator<ITER>::value> Parent;
98  typedef typename Parent::value_type value_type;
99  typedef typename Parent::reference reference;
100  typedef reference const const_reference;
101 
102  /// Construct a filter iterator with the given predicate for
103  /// a base iterator range \a iter to \a end.
104  FilterIterator(Predicate pred, Iter iter, Iter end = Iter())
105  :
106  pred_(pred),
107  iter_(iter),
108  end_(end)
109  {
110  satisfy_predicate();
111  }
112 
113 private:
114 
115  void satisfy_predicate()
116  {
117  while (iter_ != end_ && !pred_(*iter_))
118  ++iter_;
119  }
120 
121  void increment()
122  {
123  ++iter_;
124  satisfy_predicate();
125  }
126 
127  reference dereference() const
128  {
129  return *iter_;
130  }
131 
132  bool equal(FilterIterator const & other) const
133  {
134  return iter_ == other.iter_;
135  }
136 
137  Predicate pred_;
138  Iter iter_;
139  Iter end_;
140 
141  friend class vigra::IteratorFacadeCoreAccess;
142 
143 };
144 
145 template <typename PREDICATE, typename ITER>
146 FilterIterator<PREDICATE, ITER>
147 make_filter_iterator(PREDICATE pred, ITER iter, ITER end = ITER())
148 {
149  return FilterIterator<PREDICATE, ITER>(pred, iter, end);
150 }
151 
152 
153 
154 } // namespace vigra
155 
156 
157 
158 #endif
This iterator creates a view of another iterator and skips elements that do not fulfill a given predi...
Definition: filter_iterator.hxx:84
FilterIterator(Predicate pred, Iter iter, Iter end=Iter())
Definition: filter_iterator.hxx:104

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.11.1 (Fri May 19 2017)