Tulip  5.6.3
Large graphs analysis and drawing
FilterIterator.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef FILTERITERATOR_H
21 #define FILTERITERATOR_H
22 
23 #include <tulip/Iterator.h>
24 #include <tulip/StlIterator.h>
25 #include <tulip/memorypool.h>
26 
27 namespace tlp {
28 /**
29  * @class FilterIterator
30  * @ingroup Iterators
31  * @brief Iterator that enables to filter an other Iterator
32  * @param it the iterator that should be filtered
33  * @param filter the functor or lambda function that enables to test whether or not an element is
34  *filtered
35  *
36  * The functor function should have the following form:
37  * @code
38  * class AFilterFunctor {
39  * bool operator()(TYPE a) {
40  * return true if a should be iterated, false if a should be removed;
41  * }
42  * };
43  * @endcode
44  **/
45 template <typename TYPE, typename FILTER>
46 class FilterIterator : public Iterator<TYPE> {
47 public:
48  FilterIterator(Iterator<TYPE> *it, FILTER filter) : _it(it), _filter(filter) {
49  update();
50  }
51  ~FilterIterator() {
52  delete _it;
53  }
54  inline TYPE next() {
55  TYPE tmp = _curVal;
56  update();
57  return tmp;
58  }
59  inline bool hasNext() {
60  return _hasNext;
61  }
62 
63 private:
64  void update() {
65  _hasNext = false;
66 
67  while (_it->hasNext()) {
68  _curVal = _it->next();
69 
70  if (_filter(_curVal)) {
71  _hasNext = true;
72  break;
73  }
74  }
75  }
76 
77  bool _hasNext;
78  Iterator<TYPE> *_it;
79  TYPE _curVal;
80  FILTER _filter;
81  size_t _nbele;
82 };
83 
84 template <typename TYPE, typename FILTER>
85 class MPFilterIterator : public FilterIterator<TYPE, FILTER>,
86  public MemoryPool<MPFilterIterator<TYPE, FILTER>> {
87 public:
88  MPFilterIterator(Iterator<TYPE> *it, FILTER filter) : FilterIterator<TYPE, FILTER>(it, filter) {}
89 };
90 
91 /**
92  * @brief Convenient function for creating a FilterIterator.
93  * @ingroup Iterators
94  *
95  * @since Tulip 5.2
96  *
97  * Creates a FilterIterator from another iterator and a filter function.
98  * The returned iterator takes ownership of the one provided as parameter.
99  *
100  * @param it a Tulip iterator
101  * @param filter the functor or lambda function that enables to test whether or not an element is
102  *filtered
103  * @return a FilterIterator
104  **/
105 template <typename TYPE, typename FILTER>
107  return new MPFilterIterator<TYPE, FILTER>(it, filter);
108 }
109 
110 /**
111  * @brief Convenient function for creating a FilterIterator from a STL container.
112  * @ingroup Iterators
113  *
114  * @since Tulip 5.2
115  *
116  * Creates a FilterIterator from a STL container (std::list, std::vector, std::set, std::map, ...)
117  * and a filter function.
118  *
119  * @param stlContainer any STL container
120  * @param filter the functor or lambda function that enables to test whether or not an element is
121  *filtered
122  * @return a FilterIterator
123  **/
124 template <typename Container, typename FILTER>
125 typename std::enable_if<has_const_iterator<Container>::value,
126  MPFilterIterator<typename Container::value_type, FILTER>
127  *>::type inline filterIterator(const Container &stlContainer,
128  FILTER filter) {
129  return new MPFilterIterator<typename Container::value_type, FILTER>(stlIterator(stlContainer),
130  filter);
131 }
132 } // namespace tlp
133 #endif // FILTERITERATOR_H
tlp::Iterator::next
virtual T next()=0
Moves the Iterator on the next element.
tlp::stlIterator
std::enable_if< has_const_iterator< Container >::value, StlIterator< typename Container::value_type, typename Container::const_iterator > * >::type stlIterator(const Container &stlContainer)
Convenient function for creating a StlIterator from a stl container.
Definition: StlIterator.h:95
tlp::FilterIterator
Iterator that enables to filter an other Iterator.
Definition: FilterIterator.h:46
tlp::filterIterator
FilterIterator< TYPE, FILTER > * filterIterator(Iterator< TYPE > *it, FILTER filter)
Convenient function for creating a FilterIterator.
Definition: FilterIterator.h:106
tlp::FilterIterator::hasNext
bool hasNext()
Tells if the sequence is at its end.
Definition: FilterIterator.h:59
tlp::Iterator
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Graph.h:43
tlp
Definition: AbstractProperty.h:34
tlp::Iterator::hasNext
virtual bool hasNext()=0
Tells if the sequence is at its end.
tlp::FilterIterator::next
TYPE next()
Moves the Iterator on the next element.
Definition: FilterIterator.h:54