Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/StableIterator.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 #ifndef TULIP_STABLEITERATOR_H
00022 #define TULIP_STABLEITERATOR_H
00023 #include <tulip/Iterator.h>
00024 #include <tulip/tulipconf.h>
00025 #include <vector>
00026 #include <cstdlib>
00027 
00028 namespace tlp {
00029 /**
00030   * @class StableIterator
00031   * @brief Stores the elements of an iterator and iterates a copy.
00032   *
00033   * This Iterator stores all the elements accessible by another Iterator into an internal data
00034   * structure (created at the construction), and then uses this structure for the iteration.
00035   * Iteration order is the same.
00036   *
00037   * @warning By default StableIterator takes ownership of the iterator given in parameter, (ie,
00038   * delete will be called on the input iterator). The deletion takes place when constructing the StableIterator.
00039   *
00040   * This class is really useful when one needs to modify the graph during an iteration. For
00041   * instance the following code remove all nodes that match the function myfunc().
00042   * Using standard iterators for that operations is not possible since we modify the graph.
00043   *
00044   * @code
00045   * StableIterator<node> it(graph->getNodes());
00046   * while(it.hasNext()) {
00047   *  node n = it.next();
00048   *  if (myfunc(n))
00049   *     graph->delNode(n);
00050   * }
00051   * @endcode
00052   *
00053   * @see stableForEach
00054   */
00055 template<class itType>
00056 struct StableIterator : public Iterator<itType> {
00057   //=============================
00058   /**
00059   * @brief Creates a stable Iterator, that allows to delete elements from a graph while iterating on them.
00060   *
00061   * @param inputIterator Input Iterator, which defines the sequence on which this Iterator will iterate.
00062   * @param nbElements The number of elements the iteration will take place on. Defaults to 0.
00063   * @param deleteIterator Whether or not to delete the Iterator given as first parameter. Defaults to true.
00064   **/
00065   StableIterator(Iterator<itType> *inputIterator, size_t nbElements = 0, bool deleteIterator = true) {
00066     sequenceCopy.reserve(nbElements);
00067 
00068     for (; inputIterator->hasNext();) {
00069       sequenceCopy.push_back(inputIterator->next());
00070     }
00071 
00072     if (deleteIterator)
00073       delete inputIterator;
00074 
00075     copyIterator = sequenceCopy.begin();
00076   }
00077   //=============================
00078   ~StableIterator() {}
00079   //=============================
00080   itType next() {
00081     itType tmp(*copyIterator);
00082     ++copyIterator;
00083     return tmp;
00084   }
00085   //=============================
00086   bool hasNext() {
00087     return (copyIterator != sequenceCopy.end());
00088   }
00089   //=============================
00090 
00091   /**
00092   * @brief Restarts the iteration by moving the Iterator to the beginning of the sequence.
00093   *
00094   * @return void
00095   **/
00096   void restart() {
00097     copyIterator = sequenceCopy.begin();
00098   }
00099   //=============================
00100 protected :
00101   /**
00102   * @brief A copy of the sequence of the elements to iterate.
00103   **/
00104   std::vector<itType> sequenceCopy;
00105 
00106   /**
00107   * @brief STL const_iterator on the cloned sequence.
00108   **/
00109   typename std::vector<itType>::const_iterator copyIterator;
00110 };
00111 
00112 }
00113 #endif
00114 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties