Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/StlIterator.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_STLITERATOR_H
00022 #define TULIP_STLITERATOR_H
00023 #include <map>
00024 #include <tulip/Iterator.h>
00025 #include <tulip/memorypool.h>
00026 
00027 namespace tlp {
00028 
00029 template<typename VALUE, typename ITERATOR>
00030 struct StlIterator:public Iterator< VALUE  > {
00031   StlIterator(const ITERATOR &startIt, const ITERATOR &endIt):
00032     it(startIt),
00033     itEnd(endIt) {
00034   }
00035   VALUE next() {
00036     VALUE tmp = *it;
00037     ++it;
00038     return tmp;
00039   }
00040   bool hasNext() {
00041     return (itEnd!=it);
00042   }
00043 private:
00044   ITERATOR it, itEnd;
00045 };
00046 //=================================================
00047 /**
00048   * @class MPStlIterator
00049   * @ingroup Iterators
00050   * @brief MPStlIterator implements memory pool for StlIterator
00051   * @warning never inherit from that class
00052   * @see StlIterator
00053   */
00054 template<typename VALUE, typename ITERATOR>
00055 struct MPStlIterator:public StlIterator< VALUE, ITERATOR >,
00056   public MemoryPool<MPStlIterator<VALUE, ITERATOR> > {
00057   MPStlIterator(const ITERATOR &startIt, const ITERATOR &endIt):
00058     StlIterator<VALUE, ITERATOR>(startIt, endIt) {
00059   }
00060 };
00061 //=================================================
00062 template<typename KEY, typename VALUE>
00063 struct StlMapIterator:public Iterator< std::pair<KEY,VALUE> > {
00064   StlMapIterator(typename std::map<KEY,VALUE>::const_iterator startIt, typename std::map<KEY,VALUE>::const_iterator endIt):
00065     it(startIt),
00066     itEnd(endIt)
00067   {}
00068   std::pair<KEY,VALUE> next();
00069   bool hasNext();
00070 private:
00071   typename std::map<KEY,VALUE>::const_iterator it, itEnd;
00072 };
00073 //=================================================
00074 ///  StlMapIterator implemetation
00075 template<typename KEY, typename VALUE>
00076 std::pair<KEY,VALUE> StlMapIterator<KEY,VALUE>::next() {
00077   std::pair<KEY,VALUE> tmp=*it;
00078   ++it;
00079   return tmp;
00080 }
00081 template<typename KEY, typename VALUE>
00082 bool StlMapIterator<KEY,VALUE>::hasNext() {
00083   return (itEnd!=it);
00084 }
00085 //=================================================
00086 }
00087 
00088 template<typename KEY, typename VALUE>
00089 struct StlMapKeyIterator : public tlp::Iterator<KEY> {
00090   StlMapKeyIterator(typename std::map<KEY,VALUE>::const_iterator startIt, typename std::map<KEY,VALUE>::const_iterator endIt):
00091     it(startIt),
00092     itEnd(endIt)
00093   {}
00094   KEY next() {
00095     const KEY tmp = it->first;
00096     ++it;
00097     return tmp;
00098   }
00099   bool hasNext() {
00100     return it != itEnd;
00101   }
00102 private:
00103   typename std::map<KEY,VALUE>::const_iterator it, itEnd;
00104 };
00105 
00106 #endif
00107 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties