Tulip  5.0.0
Large graphs analysis and drawing
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
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 #include <algorithm>
28 
29 namespace tlp {
30 /**
31  * @class StableIterator
32  * @brief Stores the elements of an iterator and iterates a copy.
33  *
34  * This Iterator stores all the elements accessible by another Iterator into an internal data
35  * structure (created at the construction), and then uses this structure for the iteration.
36  * Iteration order is the same.
37  *
38  * @warning By default StableIterator takes ownership of the iterator given in parameter, (ie,
39  * delete will be called on the input iterator). The deletion takes place when constructing the StableIterator.
40  *
41  * This class is really useful when one needs to modify the graph during an iteration. For
42  * instance the following code remove all nodes that match the function myfunc().
43  * Using standard iterators for that operations is not possible since we modify the graph.
44  *
45  * @code
46  * StableIterator<node> it(graph->getNodes());
47  * while(it.hasNext()) {
48  * node n = it.next();
49  * if (myfunc(n))
50  * graph->delNode(n);
51  * }
52  * @endcode
53  *
54  * @see stableForEach
55  */
56 template<class itType>
57 struct StableIterator : public Iterator<itType> {
58  //=============================
59  /**
60  * @brief Creates a stable Iterator, that allows to delete elements from a graph while iterating on them.
61  *
62  * @param inputIterator Input Iterator, which defines the sequence on which this Iterator will iterate.
63  * @param nbElements The number of elements the iteration will take place on. Defaults to 0.
64  * @param deleteIterator Whether or not to delete the Iterator given as first parameter. Defaults to true.
65  **/
66  StableIterator(Iterator<itType> *inputIterator, size_t nbElements = 0, bool deleteIterator = true, bool sortCopy = false) {
67  sequenceCopy.reserve(nbElements);
68 
69  for (; inputIterator->hasNext();) {
70  sequenceCopy.push_back(inputIterator->next());
71  }
72 
73  if (deleteIterator)
74  delete inputIterator;
75 
76  if (sortCopy)
77  std::sort(sequenceCopy.begin(), sequenceCopy.end());
78 
79  copyIterator = sequenceCopy.begin();
80  }
81  //=============================
82  ~StableIterator() {}
83  //=============================
84  itType next() {
85  itType tmp(*copyIterator);
86  ++copyIterator;
87  return tmp;
88  }
89  //=============================
90  bool hasNext() {
91  return (copyIterator != sequenceCopy.end());
92  }
93  //=============================
94 
95  /**
96  * @brief Restarts the iteration by moving the Iterator to the beginning of the sequence.
97  *
98  * @return void
99  **/
100  void restart() {
101  copyIterator = sequenceCopy.begin();
102  }
103  //=============================
104 protected :
105  /**
106  * @brief A copy of the sequence of the elements to iterate.
107  **/
108  std::vector<itType> sequenceCopy;
109 
110  /**
111  * @brief STL const_iterator on the cloned sequence.
112  **/
113  typename std::vector<itType>::const_iterator copyIterator;
114 };
115 
116 }
117 #endif
118 ///@endcond
virtual itType next()=0
Moves the Iterator on the next element.
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Iterator.h:39
virtual bool hasNext()=0
Tells if the sequence is at its end.