Tulip  4.7.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
StlIterator.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_STLITERATOR_H
22 #define TULIP_STLITERATOR_H
23 #include <map>
24 #include <tulip/Iterator.h>
25 #include <tulip/memorypool.h>
26 
27 namespace tlp {
28 
29 template<typename VALUE, typename ITERATOR>
30 struct StlIterator:public Iterator< VALUE > {
31  StlIterator(const ITERATOR &startIt, const ITERATOR &endIt):
32  it(startIt),
33  itEnd(endIt) {
34  }
35  VALUE next() {
36  VALUE tmp = *it;
37  ++it;
38  return tmp;
39  }
40  bool hasNext() {
41  return (itEnd!=it);
42  }
43 private:
44  ITERATOR it, itEnd;
45 };
46 //=================================================
47 /**
48  * @class MPStlIterator
49  * @ingroup Iterators
50  * @brief MPStlIterator implements memory pool for StlIterator
51  * @warning never inherit from that class
52  * @see StlIterator
53  */
54 template<typename VALUE, typename ITERATOR>
55 struct MPStlIterator:public StlIterator< VALUE, ITERATOR >,
56  public MemoryPool<MPStlIterator<VALUE, ITERATOR> > {
57  MPStlIterator(const ITERATOR &startIt, const ITERATOR &endIt):
58  StlIterator<VALUE, ITERATOR>(startIt, endIt) {
59  }
60 };
61 //=================================================
62 template<typename KEY, typename VALUE>
63 struct StlMapIterator:public Iterator< std::pair<KEY,VALUE> > {
64  StlMapIterator(typename std::map<KEY,VALUE>::const_iterator startIt, typename std::map<KEY,VALUE>::const_iterator endIt):
65  it(startIt),
66  itEnd(endIt) {
67  }
68  std::pair<KEY,VALUE> next();
69  bool hasNext();
70 private:
71  typename std::map<KEY,VALUE>::const_iterator it, itEnd;
72 };
73 //=================================================
74 /// StlMapIterator implemetation
75 template<typename KEY, typename VALUE>
76 std::pair<KEY,VALUE> StlMapIterator<KEY,VALUE>::next() {
77  std::pair<KEY,VALUE> tmp=*it;
78  ++it;
79  return tmp;
80 }
81 template<typename KEY, typename VALUE>
82 bool StlMapIterator<KEY,VALUE>::hasNext() {
83  return (itEnd!=it);
84 }
85 //=================================================
86 }
87 
88 template<typename KEY, typename VALUE>
89 struct StlMapKeyIterator : public tlp::Iterator<KEY> {
90  StlMapKeyIterator(typename std::map<KEY,VALUE>::const_iterator startIt, typename std::map<KEY,VALUE>::const_iterator endIt):
91  it(startIt),
92  itEnd(endIt) {
93  }
94  KEY next() {
95  const KEY tmp = it->first;
96  ++it;
97  return tmp;
98  }
99  bool hasNext() {
100  return it != itEnd;
101  }
102 private:
103  typename std::map<KEY,VALUE>::const_iterator it, itEnd;
104 };
105 
106 #endif
107 ///@endcond