Tulip  5.1.0
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 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef FILTERITERATOR_H
22 #define FILTERITERATOR_H
23 
24 #include <tulip/memorypool.h>
25 
26 
27 
28 namespace tlp {
29 /**
30  * @class FilterIterator
31  * @brief Iterator that enables to filter an other Iterator
32  * @param it the iterator that should be filtered
33  * @param filter the functor that enables to test wheter or not an element is filtered
34  *
35  * The functor function shoul have the following form
36  * @code
37  * class AFilterFunctor {
38  * bool operator()(TYPE a) {
39  * return true if a should be iterated, false if a should be removed;
40  * }
41  * };
42  * @endcode
43  */
44 template <typename TYPE, typename FILTER>
45 class FilterIterator : public Iterator<TYPE> {
46 public:
47  FilterIterator(Iterator<TYPE> *it, FILTER filter):
48  _it(it),
49  _filter(filter) {
50  update();
51  }
52  ~FilterIterator() {
53  delete _it;
54  }
55  inline TYPE next() {
56  TYPE tmp = _curVal;
57  update();
58  return tmp;
59  }
60  inline bool hasNext() {
61  return _hasNext;
62  }
63 
64 private:
65  void update() {
66  _hasNext = false;
67 
68  while (_it->hasNext()) {
69  _curVal = _it->next();
70 
71  if (_filter(_curVal)) {
72  _hasNext = true;
73  break;
74  }
75  }
76  }
77 
78  bool _hasNext;
79  Iterator<TYPE> *_it;
80  TYPE _curVal;
81  FILTER _filter;
82  size_t _nbele;
83 };
84 /**
85  * @class MPFilterIterator
86  * @brief MPFilterIterator implements memory pool for FilterIterator
87  * @warning never inherit from that class
88  * @see FilterIterator
89  */
90 template <typename TYPE, typename FILTER>
91 class MPFilterIterator : public FilterIterator<TYPE, FILTER>, public MemoryPool<MPFilterIterator<TYPE, FILTER> > {
92 public:
93  MPFilterIterator(Iterator<TYPE> *it, FILTER filter):
94  FilterIterator<TYPE, FILTER>(it, filter) {
95  }
96 };
97 
98 }
99 #endif // FILTERITERATOR_H
100 ///@endcond
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Iterator.h:39