Tulip 5.7.1
Large graphs analysis and drawing
Loading...
Searching...
No Matches
FilterIterator.h
1/*
2 *
3 * This file is part of Tulip (https://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
27namespace 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 **/
45template <typename TYPE, typename FILTER>
46class FilterIterator : public Iterator<TYPE> {
47public:
48 FilterIterator(Iterator<TYPE> *it, FILTER filter) : _it(it), _filter(filter) {
49 update();
50 }
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
63private:
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
84template <typename TYPE, typename FILTER>
85class MPFilterIterator : public FilterIterator<TYPE, FILTER>,
86 public MemoryPool<MPFilterIterator<TYPE, FILTER>> {
87public:
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 **/
105template <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 **/
124template <typename Container, typename FILTER>
125typename 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
Iterator that enables to filter an other Iterator.
bool hasNext()
Tells if the sequence is at its end.
TYPE next()
Moves the Iterator on the next element.
FilterIterator< TYPE, FILTER > * filterIterator(Iterator< TYPE > *it, FILTER filter)
Convenient function for creating a FilterIterator.
std::enable_if< has_const_iterator< Container >::value, StlIterator< typenameContainer::value_type, typenameContainer::const_iterator > * >::type stlIterator(const Container &stlContainer)
Convenient function for creating a StlIterator from a stl container.
Definition: StlIterator.h:95
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Iterator.h:74
virtual bool hasNext()=0
Tells if the sequence is at its end.
virtual T next()=0
Moves the Iterator on the next element.