Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
StableIterator.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
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 TULIP_STABLEITERATOR_H
22 #define TULIP_STABLEITERATOR_H
23 #include <tulip/Iterator.h>
24 #include <tulip/tulipconf.h>
25 #include <vector>
26 #include <cstdlib>
27 
28 namespace tlp {
29 /**
30  * @class StableIterator
31  * @brief Stores the elements of an iterator and iterates on a copy.
32  *
33  * THis Iterator stores all the elements accessible by another Iterator into an internal data
34  * structure (created at the construction), and then uses this structure for the iteration.
35  * Iteration order is the same.
36  *
37  * @warning By default StableIterator takes ownership of the iterator given in parameter, (ie,
38  * delete will be called on the input iterator). The deletion takes place when constructing the StableIterator.
39  *
40  * This class is really useful when one needs to modify the graph during an iteration. For
41  * instance the following code remove all nodes that match the function myfunc().
42  * Using standard iterators for that operations is not possible since we modify the graph.
43  *
44  * @code
45  * StableIterator<node> it(graph->getNodes());
46  * while(it.hasNext()) {
47  * node n = it.next();
48  * if (myfunc(n))
49  * graph->delNode(n);
50  * }
51  * @endcode
52  *
53  * @see stableForEach
54  */
55 template<class itType>
56 struct StableIterator : public Iterator<itType> {
57  //=============================
58  /**
59  * @brief Creates a stable Iterator, that allows to delete elements from a graph while iterating on them.
60  *
61  * @param inputIterator Input Iterator, which defines the sequence on which this Iterator will iterate.
62  * @param nbElements The number of elements the iteration will take place on. Defaults to 0.
63  * @param deleteIterator Whether or not to delete the Iterator given as first parameter. Defaults to true.
64  **/
65  StableIterator(Iterator<itType> *inputIterator, size_t nbElements = 0, bool deleteIterator = true) {
66  sequenceCopy.reserve(nbElements);
67 
68  for (; inputIterator->hasNext();) {
69  sequenceCopy.push_back(inputIterator->next());
70  }
71 
72  if (deleteIterator)
73  delete inputIterator;
74 
75  copyIterator = sequenceCopy.begin();
76  }
77  //=============================
78  ~StableIterator() {}
79  //=============================
80  itType next() {
81  itType tmp(*copyIterator);
82  ++copyIterator;
83  return tmp;
84  }
85  //=============================
86  bool hasNext() {
87  return (copyIterator != sequenceCopy.end());
88  }
89  //=============================
90 
91  /**
92  * @brief Restarts the iteration by moving the Iterator to the beginning of the sequence.
93  *
94  * @return void
95  **/
96  void restart() {
97  copyIterator = sequenceCopy.begin();
98  }
99  //=============================
100 protected :
101  /**
102  * @brief A copy of the sequence of elements to iterate on.
103  **/
104  std::vector<itType> sequenceCopy;
105 
106  /**
107  * @brief STL const_iterator on the cloned sequence.
108  **/
109  typename std::vector<itType>::const_iterator copyIterator;
110 };
111 
112 }
113 #endif
114 ///@endcond