Tulip 5.7.1
Large graphs analysis and drawing
Loading...
Searching...
No Matches
StlIterator.h
1/*
2 *
3 * This file is part of Tulip (https://tulip.labri.fr)
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
20#ifndef TULIP_STLITERATOR_H
21#define TULIP_STLITERATOR_H
22
23#include <map>
24#include <type_traits>
25
26#include <tulip/Iterator.h>
27#include <tulip/memorypool.h>
28
29namespace tlp {
30
31/**
32 * @class StlIterator
33 * @ingroup Iterators
34 *
35 * @brief StlIterator wraps a stl iterator.
36 *
37 * Creates a Tulip Iterator from two stl iterators.
38 *
39 * @param startIt a stl iterator from which to begin the iteration
40 * @param endIt a stl iterator on which to end the iteration
41 *
42 **/
43template <typename T, typename ITERATOR>
44struct StlIterator : public Iterator<T> {
45 StlIterator(const ITERATOR &startIt, const ITERATOR &endIt) : it(startIt), itEnd(endIt) {}
46 T next() {
47 T tmp = *it;
48 ++it;
49 return tmp;
50 }
51 bool hasNext() {
52 return (itEnd != it);
53 }
54
55private:
56 ITERATOR it, itEnd;
57};
58//=================================================
59template <typename T, typename ITERATOR>
60struct MPStlIterator : public StlIterator<T, ITERATOR>,
61 public MemoryPool<MPStlIterator<T, ITERATOR>> {
62 MPStlIterator(const ITERATOR &startIt, const ITERATOR &endIt)
63 : StlIterator<T, ITERATOR>(startIt, endIt) {}
64};
65//=================================================
66
67// Helper to determine whether there's a const_iterator for T.
68template <typename T>
69struct has_const_iterator {
70private:
71 template <typename C>
72 static char test(typename C::const_iterator *);
73 template <typename C>
74 static int test(...);
75
76public:
77 enum { value = sizeof(test<T>(0)) == sizeof(char) };
78};
79
80/**
81 * @brief Convenient function for creating a StlIterator from a stl container.
82 * @ingroup Iterators
83 *
84 * @since Tulip 5.2
85 *
86 * Creates a StlIterator from a STL container (std::list, std::vector, std::set, std::map, ...).
87 *
88 * @param stlContainer any STL container
89 * @return a StlIterator
90 **/
91template <typename Container>
92typename std::enable_if<
93 has_const_iterator<Container>::value,
94 StlIterator<typename Container::value_type, typename Container::const_iterator>
95 *>::type inline stlIterator(const Container &stlContainer) {
96 return new MPStlIterator<typename Container::value_type, typename Container::const_iterator>(
97 stlContainer.begin(), stlContainer.end());
98}
99
100//=================================================
101template <typename KEY, typename VALUE>
102struct StlMapIterator : public Iterator<std::pair<KEY, VALUE>> {
103 StlMapIterator(typename std::map<KEY, VALUE>::const_iterator startIt,
104 typename std::map<KEY, VALUE>::const_iterator endIt)
105 : it(startIt), itEnd(endIt) {}
106
107 std::pair<KEY, VALUE> next() {
108 std::pair<KEY, VALUE> tmp = *it;
109 ++it;
110 return tmp;
111 }
112
113 bool hasNext() {
114 return (itEnd != it);
115 }
116
117private:
118 typename std::map<KEY, VALUE>::const_iterator it, itEnd;
119};
120//=================================================
121template <typename KEY, typename VALUE>
122struct StlMapKeyIterator : public tlp::Iterator<KEY> {
123 StlMapKeyIterator(typename std::map<KEY, VALUE>::const_iterator startIt,
124 typename std::map<KEY, VALUE>::const_iterator endIt)
125 : it(startIt), itEnd(endIt) {}
126
127 KEY next() {
128 const KEY tmp = it->first;
129 ++it;
130 return tmp;
131 }
132
133 bool hasNext() {
134 return it != itEnd;
135 }
136
137private:
138 typename std::map<KEY, VALUE>::const_iterator it, itEnd;
139};
140} // namespace tlp
141
142#endif
std::enable_if< has_const_iterator< Container >::value, StlIterator< typenameContainer::value_type, typenameContainer::const_iterator > * >::type stlIterator(const Container &stlContainer)
Convenient function for creating a StlIterator from a stl container.
Definition: StlIterator.h:95
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Iterator.h:74
StlIterator wraps a stl iterator.
Definition: StlIterator.h:44
bool hasNext()
Tells if the sequence is at its end.
Definition: StlIterator.h:51
T next()
Moves the Iterator on the next element.
Definition: StlIterator.h:46