ALP User Documentation  0.8.preview
Algebraic Programming User Documentation
filter.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2021 Huawei Technologies Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
27 #ifndef _H_ALP_UTILS_ITERATORS_FILTER
28 #define _H_ALP_UTILS_ITERATORS_FILTER
29 
30 #include <iterator>
31 
32 
33 namespace grb {
34 
35  namespace utils {
36 
37  namespace iterators {
38 
52  template< typename FwdSubIter >
54 
75  template< typename FwdSubIter >
77  FwdSubIter begin, const FwdSubIter end,
78  const std::function< bool(const typename FwdSubIter::value_type &) > func
79  );
80 
81  template< typename FwdSubIter >
82  class IteratorFilter {
83 
84  static_assert( std::is_base_of<
85  std::forward_iterator_tag,
86  typename std::iterator_traits< FwdSubIter >::iterator_category
87  >::value,
88  "The sub-iterator to IteratorFilter must be a forward iterator."
89  );
90 
91 
92  protected:
93 
95  typedef typename std::iterator_traits< FwdSubIter >::value_type ValT;
96 
98  typedef typename std::function< bool( const ValT & ) > FilterT;
99 
100 
101  private:
102 
107 
111  FwdSubIter it;
112 
116  const FwdSubIter end;
117 
121  FilterT filter;
122 
123 
124  protected:
125 
133  FwdSubIter it_in, const FwdSubIter end_in,
134  const FilterT func_in
135  ) :
136  it( it_in ), end( end_in ), filter( func_in )
137  {}
138 
139 
140  public:
141 
142  // friend functions
143 
154  friend void swap(
156  ) noexcept {
157  std::swap( left.it, right.it );
158  std::swap( left.end, right.end );
159  std::swap( left.filter, right.filter );
160  }
161 
162  template< typename T >
163  friend IteratorFilter< T > make_filtered_iterator(
164  T begin, const T end,
165  const std::function< bool(const typename T::value_type &) > func
166  );
167 
168  // STL typedefs
169 
170  typedef typename std::iterator_traits< self_type >::difference_type
171  difference_type;
172  typedef typename std::iterator_traits< self_type >::value_type value_type;
173  typedef typename std::iterator_traits< self_type >::pointer pointer;
174  typedef typename std::iterator_traits< self_type >::reference reference;
175  typedef typename std::iterator_traits< self_type >::iterator_category
176  iterator_category;
177 
184  IteratorFilter() = delete;
185 
192  it( toCopy.it ), end( toCopy.end ), filter( toCopy.filter )
193  {}
194 
199 
209  const IteratorFilter< FwdSubIter > &toCopy
210  ) {
211  it = toCopy.it;
212  filter = toCopy.filter;
213  return *this;
214  }
215 
229  (void) ++it;
230  while( it != end && filter( *it ) ) {
231  (void) ++it;
232  }
233  return *this;
234  }
235 
249  IteratorFilter< FwdSubIter > ret( *this );
250  (void) it++;
251  while( it != end && filter( *it ) ) {
252  (void) it++;
253  }
254  return ret;
255  }
256 
265  reference operator*() const {
266  return *it;
267  }
268 
277  pointer operator->() const {
278  return it.operator->();
279  }
280 
292  bool operator==( const IteratorFilter< FwdSubIter >& other ) const {
293  return it == other.it;
294  }
295 
307  bool operator!=( const IteratorFilter< FwdSubIter >& other ) const {
308  return it != other.it;
309  }
310 
311  };
312 
313  template< typename FwdSubIter >
315  FwdSubIter begin, const FwdSubIter end,
316  const std::function< bool(const typename FwdSubIter::value_type &) > func
317  ) {
319  IteratorFilter< FwdSubIter >( begin, end, func );
320  if( begin != end ) {
321  while( begin != end && func( *begin ) ) {
322  (void) ++begin;
323  }
324  ret = IteratorFilter< FwdSubIter >( begin, end, func );
325  }
326  return ret;
327  }
328 
329  } // end grb::utils::iterators namespace
330 
331  } // end grb::utils namespace
332 
333 } // end grb namespace
334 
335 namespace std {
336 
345  template< typename FwdSubIter >
346  struct iterator_traits<
347  grb::utils::iterators::IteratorFilter< FwdSubIter >
348  > {
349 
351  typedef typename std::iterator_traits< FwdSubIter >::difference_type
353 
355  typedef typename std::iterator_traits< FwdSubIter >::value_type value_type;
356 
358  typedef typename std::iterator_traits< FwdSubIter >::pointer pointer;
359 
362 
364  typedef typename std::forward_iterator_tag iterator_category;
365 
366  };
367 
368 }
369 
370 #endif // end _H_ALP_UTILS_ITERATORS_FILTER
371 
std::forward_iterator_tag iterator_category
This trait is always forward iterator.
Definition: filter.hpp:364
IteratorFilter< FwdSubIter > & operator=(const IteratorFilter< FwdSubIter > &toCopy)
Copy-assignment.
Definition: filter.hpp:208
std::iterator_traits< FwdSubIter >::pointer pointer
This trait is inherited from FwdSubIter.
Definition: filter.hpp:358
bool operator==(const IteratorFilter< FwdSubIter > &other) const
Equality check.
Definition: filter.hpp:292
This iterator filters elements from another iterator based on a user- specified filter function.
Definition: filter.hpp:53
IteratorFilter(const IteratorFilter< FwdSubIter > &toCopy)
Copy constructor.
Definition: filter.hpp:191
IteratorFilter< FwdSubIter > make_filtered_iterator(FwdSubIter begin, const FwdSubIter end, const std::function< bool(const typename FwdSubIter::value_type &) > func)
The factory function that takes any iterator pair over the same container and returns a filtered iter...
Definition: filter.hpp:314
reference operator*() const
Dereference operator.
Definition: filter.hpp:265
std::iterator_traits< FwdSubIter >::value_type value_type
This trait is inherited from FwdSubIter.
Definition: filter.hpp:355
IteratorFilter()=delete
An IteratorFilter may never be default-constructed.
bool operator!=(const IteratorFilter< FwdSubIter > &other) const
Inequality check.
Definition: filter.hpp:307
pointer operator->() const
Pointer operator.
Definition: filter.hpp:277
The sequential reference implementation.
Definition: backends.hpp:55
std::iterator_traits< FwdSubIter >::difference_type difference_type
This trait is inherited from FwdSubIter.
Definition: filter.hpp:352
~IteratorFilter()
Default destructor.
Definition: filter.hpp:198
IteratorFilter< FwdSubIter > & operator++()
Iterator increment operation.
Definition: filter.hpp:228
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
friend void swap(IteratorFilter< FwdSubIter > &left, IteratorFilter< FwdSubIter > &right) noexcept
Swap two instances of the grb::utils::iterators::IteratorFilter type.
Definition: filter.hpp:154
std::iterator_traits< FwdSubIter >::reference reference
This trait is inherited from FwdSubIter.
Definition: filter.hpp:361
IteratorFilter< FwdSubIter > operator++(int)
Iterator increment operation.
Definition: filter.hpp:248