Tulip  5.7.4
Large graphs analysis and drawing
StableIterator.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 TULIP_STABLEITERATOR_H
21 #define TULIP_STABLEITERATOR_H
22 
23 #include <vector>
24 #include <cstdlib>
25 #include <algorithm>
26 
27 #include <tulip/Iterator.h>
28 #include <tulip/StlIterator.h>
29 #include <tulip/tulipconf.h>
30 
31 namespace tlp {
32 /**
33  * @class StableIterator
34  * @ingroup Iterators
35  * @brief Stores the elements of an iterator and iterates a copy.
36  *
37  * This Iterator stores all the elements accessible by another Iterator into an internal data
38  * structure (created at the construction), and then uses this structure for the iteration.
39  * Iteration order is the same.
40  *
41  * @warning By default StableIterator takes ownership of the iterator given in parameter, (ie,
42  * delete will be called on the input iterator). The deletion takes place when constructing the
43  *StableIterator.
44  *
45  * This class is really useful when one needs to modify the graph during an iteration. For
46  * instance the following code remove all nodes that match the function myfunc().
47  * Using standard iterators for that operation is not possible since we modify the graph.
48  *
49  * @code
50  * StableIterator<node> it(graph->getNodes());
51  * while(it.hasNext()) {
52  * node n = it.next();
53  * if (myfunc(n))
54  * graph->delNode(n);
55  * }
56  * @endcode
57  *
58  **/
59 template <typename T>
60 struct StableIterator : public Iterator<T> {
61  //=============================
62  /**
63  * @brief Creates a stable Iterator, that allows to delete elements from a graph while iterating
64  *on them.
65  *
66  * @param inputIterator Input Iterator, which defines the sequence on which this Iterator will
67  *iterate.
68  * @param nbElements The number of elements the iteration will take place on. Defaults to 0.
69  * @param deleteIterator Whether or not to delete the Iterator given as first parameter. Defaults
70  *to true.
71  **/
72  StableIterator(Iterator<T> *inputIterator, size_t nbElements = 0, bool deleteIterator = true,
73  bool sortCopy = false) {
74  sequenceCopy.reserve(nbElements);
75 
76  for (; inputIterator->hasNext();) {
77  sequenceCopy.push_back(inputIterator->next());
78  }
79 
80  if (deleteIterator)
81  delete inputIterator;
82 
83  if (sortCopy)
84  std::sort(sequenceCopy.begin(), sequenceCopy.end());
85 
86  copyIterator = sequenceCopy.begin();
87  }
88  //=============================
89  ~StableIterator() {}
90  //=============================
91  T next() {
92  T tmp(*copyIterator);
93  ++copyIterator;
94  return tmp;
95  }
96  //=============================
97  bool hasNext() {
98  return (copyIterator != sequenceCopy.end());
99  }
100  //=============================
101 
102  /**
103  * @brief Restarts the iteration by moving the Iterator to the beginning of the sequence.
104  *
105  * @return void
106  **/
107  void restart() {
108  copyIterator = sequenceCopy.begin();
109  }
110  //=============================
111 protected:
112  /**
113  * @brief A copy of the sequence of the elements to iterate.
114  **/
115  std::vector<T> sequenceCopy;
116 
117  /**
118  * @brief STL const_iterator on the cloned sequence.
119  **/
120  typename std::vector<T>::const_iterator copyIterator;
121 };
122 
123 /**
124  * @brief Convenient function for creating a StableIterator.
125  * @ingroup Iterators
126  *
127  * @since Tulip 5.2
128  *
129  * Creates a StableIterator from another Iterator.
130  * The returned Iterator takes ownership of the one provided as parameter.
131  *
132  * @param it a Tulip Iterator
133  * @return a StableIterator
134  **/
135 template <class T>
137  return new StableIterator<T>(it);
138 }
139 
140 /**
141  * @brief Convenient function for creating a StableIterator from a STL container.
142  * @ingroup Iterators
143  *
144  * @since Tulip 5.2
145  *
146  * Creates a StableIterator from a STL container (std::list, std::vector, std::set, std::map, ...).
147  *
148  * @param stlContainer any STL container
149  * @return a StableIterator
150  **/
151 template <typename Container>
152 typename std::enable_if<has_const_iterator<Container>::value,
153  StableIterator<typename Container::value_type>
154  *>::type inline stableIterator(const Container &stlContainer) {
156 }
157 } // namespace tlp
158 #endif
StableIterator< T > * stableIterator(Iterator< T > *it)
Convenient function for creating a StableIterator.
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
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.
Stores the elements of an iterator and iterates a copy.
StableIterator(Iterator< T > *inputIterator, size_t nbElements=0, bool deleteIterator=true, bool sortCopy=false)
Creates a stable Iterator, that allows to delete elements from a graph while iterating on them.
void restart()
Restarts the iteration by moving the Iterator to the beginning of the sequence.
std::vector< T >::const_iterator copyIterator
STL const_iterator on the cloned sequence.
T next()
Moves the Iterator on the next element.
std::vector< T > sequenceCopy
A copy of the sequence of the elements to iterate.
bool hasNext()
Tells if the sequence is at its end.