Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/uniqueiterator.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 UNIQUEITERATOR_H
00022 #define UNIQUEITERATOR_H
00023 #include <set>
00024 #include <tulip/memorypool.h>
00025 
00026 
00027 //===============================================================================
00028 namespace tlp {
00029 /**
00030   * \brief that class implement a default functor that always return true
00031   *
00032   * @see UniqueIterator
00033   */
00034 template <typename TYPE>
00035 class CheckAllFunctor {
00036   bool operator()(const TYPE &) {
00037     return true;
00038   }
00039 };
00040 
00041 /**
00042   * @class UniqueIterator
00043   * @ingroup Iterators
00044   *
00045   * @brief UniqueIterator enables to remove duplicated elements in an iterator
00046   *
00047   * @param it the iterator in which we want to filter out duplicated elements
00048   * @param checkFuncor a functor that enable to indicate wheter or not the element could be duplicated (default test all elements)
00049   *
00050   * The functor function shoul have the following form
00051   * @code
00052   * class ACheckFunctor {
00053   *  bool operator(TYPE a) {
00054   *   return true if a could be duplicated else false;
00055   *  }
00056   * };
00057   * @endcode
00058   * checkFunctor are used for optimization purpose to prevent to log(n) test for all elements when not necessary.
00059   */
00060 template <typename TYPE, typename TOCHECKFUNCTOR = CheckAllFunctor<TYPE> >
00061 class UniqueIterator : public Iterator<TYPE> {
00062 public:
00063   UniqueIterator(Iterator<TYPE> *it, TOCHECKFUNCTOR checkFunctor = TOCHECKFUNCTOR()):_it(it), _checkFunctor(checkFunctor) {
00064     update();
00065   }
00066   //================================================
00067   ~UniqueIterator() {
00068     delete _it;
00069   }
00070   //================================================
00071   TYPE next() {
00072     TYPE tmp = curVal;
00073     update();
00074     return tmp;
00075   }
00076   //================================================
00077   inline bool hasNext() {
00078     return _hasNext;
00079   }
00080   //================================================
00081   void update() {
00082     _hasNext = false;
00083 
00084     while(_it->hasNext()) {
00085       curVal = _it->next();
00086 
00087       if (_checkFunctor(curVal) ) {
00088         if (_flag.find(curVal) == _flag.end()) {
00089           _hasNext = true;
00090           _flag.insert(curVal);
00091           return;
00092         }
00093       }
00094       else {
00095         _hasNext = true;
00096         return;
00097       }
00098     }
00099   }
00100 
00101 private:
00102   std::set<TYPE> _flag;
00103   bool _hasNext;
00104   Iterator<TYPE> *_it;
00105   TYPE curVal;
00106   TOCHECKFUNCTOR _checkFunctor;
00107 };
00108 /**
00109   * @class MPUniqueIterator
00110   * @ingroup Iterators
00111   * @brief MPUniqueIterator implements memory pool for UniqueIterator
00112   * @warning never inherit from that class
00113   * @see UniqueIterator
00114   */
00115 template <typename TYPE, typename TOCHECKFUNCTOR = CheckAllFunctor<TYPE> >
00116 class MPUniqueIterator : public UniqueIterator<TYPE, TOCHECKFUNCTOR> ,
00117   public MemoryPool<MPUniqueIterator<TYPE, TOCHECKFUNCTOR> > {
00118 public:
00119   MPUniqueIterator(Iterator<TYPE> *it, TOCHECKFUNCTOR checkFunctor = TOCHECKFUNCTOR()):
00120     UniqueIterator<TYPE, TOCHECKFUNCTOR>(it, checkFunctor) {
00121   }
00122 };
00123 
00124 }
00125 #endif // UNIQUEITERATOR_H
00126 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties