deba@822
|
1 |
// -*- c++ -*-
|
deba@822
|
2 |
#ifndef MAP_ITERATOR_H
|
deba@822
|
3 |
#define MAP_ITERATOR_H
|
deba@822
|
4 |
|
deba@822
|
5 |
#include <hugo/extended_pair.h>
|
deba@822
|
6 |
|
deba@822
|
7 |
namespace hugo {
|
deba@822
|
8 |
|
deba@822
|
9 |
|
deba@822
|
10 |
template <typename Map>
|
deba@822
|
11 |
class MapIterator;
|
deba@822
|
12 |
|
deba@822
|
13 |
template <typename Map>
|
deba@822
|
14 |
class MapConstIterator;
|
deba@822
|
15 |
|
deba@822
|
16 |
/** Compatible iterator with the stl maps' iterators.
|
deba@822
|
17 |
* It iterates on pairs of a key and a value.
|
deba@822
|
18 |
*/
|
deba@822
|
19 |
template <typename Map>
|
deba@822
|
20 |
class MapIterator {
|
deba@822
|
21 |
// friend class Map;
|
deba@822
|
22 |
friend class MapConstIterator<Map>;
|
deba@822
|
23 |
|
deba@822
|
24 |
public:
|
deba@822
|
25 |
|
deba@822
|
26 |
/// The key type of the iterator.
|
deba@822
|
27 |
typedef typename Map::KeyType KeyType;
|
deba@822
|
28 |
/// The iterator to iterate on the keys.
|
deba@822
|
29 |
typedef typename Map::KeyIt KeyIt;
|
deba@822
|
30 |
|
deba@822
|
31 |
/// The value type of the iterator.
|
deba@822
|
32 |
typedef typename Map::ValueType ValueType;
|
deba@822
|
33 |
/// The reference type of the iterator.
|
deba@822
|
34 |
typedef typename Map::ReferenceType ReferenceType;
|
deba@822
|
35 |
/// The pointer type of the iterator.
|
deba@822
|
36 |
typedef typename Map::PointerType PointerType;
|
deba@822
|
37 |
|
deba@822
|
38 |
/// The const value type of the iterator.
|
deba@822
|
39 |
typedef typename Map::ConstValueType ConstValueType;
|
deba@822
|
40 |
/// The const reference type of the iterator.
|
deba@822
|
41 |
typedef typename Map::ConstReferenceType ConstReferenceType;
|
deba@822
|
42 |
/// The pointer type of the iterator.
|
deba@822
|
43 |
typedef typename Map::ConstPointerType ConstPointerType;
|
deba@822
|
44 |
|
deba@822
|
45 |
public:
|
deba@822
|
46 |
|
deba@822
|
47 |
/** Constructor to initalize the the iterators returned
|
deba@822
|
48 |
* by the begin() and end().
|
deba@822
|
49 |
*/
|
deba@822
|
50 |
MapIterator (Map& pmap, const KeyIt& pit)
|
deba@822
|
51 |
: map(&pmap), it(pit) {}
|
deba@822
|
52 |
|
deba@822
|
53 |
public:
|
deba@822
|
54 |
|
deba@822
|
55 |
/** Default constructor.
|
deba@822
|
56 |
*/
|
deba@822
|
57 |
MapIterator() {}
|
deba@822
|
58 |
|
deba@822
|
59 |
typedef extended_pair<const KeyType&, const KeyType&,
|
deba@822
|
60 |
ReferenceType, ReferenceType> PairReferenceType;
|
deba@822
|
61 |
|
deba@822
|
62 |
/** Dereference operator for map.
|
deba@822
|
63 |
*/
|
deba@822
|
64 |
PairReferenceType operator*() {
|
deba@822
|
65 |
return PairReferenceType(it, (*map)[it]);
|
deba@822
|
66 |
}
|
deba@822
|
67 |
|
deba@822
|
68 |
class PairPointerType {
|
deba@822
|
69 |
friend class MapIterator;
|
deba@822
|
70 |
private:
|
deba@822
|
71 |
PairReferenceType data;
|
deba@822
|
72 |
PairPointerType(const KeyType& key, ReferenceType val)
|
deba@822
|
73 |
: data(key, val) {}
|
deba@822
|
74 |
public:
|
deba@822
|
75 |
PairReferenceType* operator->() {return &data;}
|
deba@822
|
76 |
};
|
deba@822
|
77 |
|
deba@822
|
78 |
/** Arrow operator for map.
|
deba@822
|
79 |
*/
|
deba@822
|
80 |
PairPointerType operator->() {
|
deba@822
|
81 |
return PairPointerType(it, ((*map)[it]));
|
deba@822
|
82 |
}
|
deba@822
|
83 |
|
deba@822
|
84 |
/** The pre increment operator of the map.
|
deba@822
|
85 |
*/
|
deba@822
|
86 |
MapIterator& operator++() {
|
deba@822
|
87 |
++it;
|
deba@822
|
88 |
return *this;
|
deba@822
|
89 |
}
|
deba@822
|
90 |
|
deba@822
|
91 |
/** The post increment operator of the map.
|
deba@822
|
92 |
*/
|
deba@822
|
93 |
MapIterator operator++(int) {
|
deba@822
|
94 |
MapIterator tmp(it);
|
deba@822
|
95 |
++it;
|
deba@822
|
96 |
return tmp;
|
deba@822
|
97 |
}
|
deba@822
|
98 |
|
deba@822
|
99 |
/** The equality operator of the map.
|
deba@822
|
100 |
*/
|
deba@822
|
101 |
bool operator==(const MapIterator& p_it) const {
|
deba@822
|
102 |
return p_it.it == it;
|
deba@822
|
103 |
}
|
deba@822
|
104 |
|
deba@822
|
105 |
/** The not-equality operator of the map.
|
deba@822
|
106 |
*/
|
deba@822
|
107 |
bool operator!=(const MapIterator& p_it) const {
|
deba@822
|
108 |
return !(*this == p_it);
|
deba@822
|
109 |
}
|
deba@822
|
110 |
|
deba@822
|
111 |
/** The equality operator of the map.
|
deba@822
|
112 |
*/
|
deba@822
|
113 |
bool operator==(const MapConstIterator<Map>& p_it) const {
|
deba@822
|
114 |
return p_it.it == it;
|
deba@822
|
115 |
}
|
deba@822
|
116 |
|
deba@822
|
117 |
/** The not-equality operator of the map.
|
deba@822
|
118 |
*/
|
deba@822
|
119 |
bool operator!=(const MapConstIterator<Map>& p_it) const {
|
deba@822
|
120 |
return !(*this == p_it);
|
deba@822
|
121 |
}
|
deba@822
|
122 |
|
deba@822
|
123 |
private:
|
deba@822
|
124 |
Map* map;
|
deba@822
|
125 |
KeyIt it;
|
deba@822
|
126 |
};
|
deba@822
|
127 |
|
deba@822
|
128 |
/** Compatible iterator with the stl maps' iterators.
|
deba@822
|
129 |
* It iterates on pairs of a key and a value.
|
deba@822
|
130 |
*/
|
deba@822
|
131 |
template <typename Map>
|
deba@822
|
132 |
class MapConstIterator {
|
deba@822
|
133 |
// friend class Map;
|
deba@822
|
134 |
friend class MapIterator<Map>;
|
deba@822
|
135 |
|
deba@822
|
136 |
public:
|
deba@822
|
137 |
|
deba@822
|
138 |
/// The key type of the iterator.
|
deba@822
|
139 |
typedef typename Map::KeyType KeyType;
|
deba@822
|
140 |
/// The iterator to iterate on the keys.
|
deba@822
|
141 |
typedef typename Map::KeyIt KeyIt;
|
deba@822
|
142 |
|
deba@822
|
143 |
/// The value type of the iterator.
|
deba@822
|
144 |
typedef typename Map::ValueType ValueType;
|
deba@822
|
145 |
/// The reference type of the iterator.
|
deba@822
|
146 |
typedef typename Map::ReferenceType ReferenceType;
|
deba@822
|
147 |
/// The pointer type of the iterator.
|
deba@822
|
148 |
typedef typename Map::PointerType PointerType;
|
deba@822
|
149 |
|
deba@822
|
150 |
/// The const value type of the iterator.
|
deba@822
|
151 |
typedef typename Map::ConstValueType ConstValueType;
|
deba@822
|
152 |
/// The const reference type of the iterator.
|
deba@822
|
153 |
typedef typename Map::ConstReferenceType ConstReferenceType;
|
deba@822
|
154 |
/// The pointer type of the iterator.
|
deba@822
|
155 |
typedef typename Map::ConstPointerType ConstPointerType;
|
deba@822
|
156 |
|
deba@822
|
157 |
public:
|
deba@822
|
158 |
|
deba@822
|
159 |
/** Constructor to initalize the the iterators returned
|
deba@822
|
160 |
* by the begin() and end().
|
deba@822
|
161 |
*/
|
deba@822
|
162 |
|
deba@822
|
163 |
MapConstIterator (const Map& pmap, const KeyIt& pit)
|
deba@822
|
164 |
: map(&pmap), it(pit) {}
|
deba@822
|
165 |
|
deba@822
|
166 |
public:
|
deba@822
|
167 |
|
deba@822
|
168 |
/** Default constructor.
|
deba@822
|
169 |
*/
|
deba@822
|
170 |
MapConstIterator() {}
|
deba@822
|
171 |
|
deba@822
|
172 |
typedef extended_pair<const KeyType&, const KeyType&,
|
deba@822
|
173 |
ConstReferenceType, ConstReferenceType> PairReferenceType;
|
deba@822
|
174 |
|
deba@822
|
175 |
/** Dereference operator for map.
|
deba@822
|
176 |
*/
|
deba@822
|
177 |
PairReferenceType operator*() {
|
deba@822
|
178 |
return PairReferenceType(it, (*map)[it]);
|
deba@822
|
179 |
}
|
deba@822
|
180 |
|
deba@822
|
181 |
class PairPointerType {
|
deba@822
|
182 |
friend class MapConstIterator;
|
deba@822
|
183 |
private:
|
deba@822
|
184 |
PairReferenceType data;
|
deba@822
|
185 |
PairPointerType(const KeyType& key, ConstReferenceType val)
|
deba@822
|
186 |
: data(key, val) {}
|
deba@822
|
187 |
public:
|
deba@822
|
188 |
PairReferenceType* operator->() {return &data;}
|
deba@822
|
189 |
};
|
deba@822
|
190 |
|
deba@822
|
191 |
/** Arrow operator for map.
|
deba@822
|
192 |
*/
|
deba@822
|
193 |
PairPointerType operator->() {
|
deba@822
|
194 |
return PairPointerType(it, ((*map)[it]));
|
deba@822
|
195 |
}
|
deba@822
|
196 |
|
deba@822
|
197 |
/** The pre increment operator of the map.
|
deba@822
|
198 |
*/
|
deba@822
|
199 |
MapConstIterator& operator++() {
|
deba@822
|
200 |
++it;
|
deba@822
|
201 |
return *this;
|
deba@822
|
202 |
}
|
deba@822
|
203 |
|
deba@822
|
204 |
/** The post increment operator of the map.
|
deba@822
|
205 |
*/
|
deba@822
|
206 |
MapConstIterator operator++(int) {
|
deba@822
|
207 |
MapConstIterator<Map> tmp(it);
|
deba@822
|
208 |
++it;
|
deba@822
|
209 |
return tmp;
|
deba@822
|
210 |
}
|
deba@822
|
211 |
|
deba@822
|
212 |
/** The equality operator of the map.
|
deba@822
|
213 |
*/
|
deba@822
|
214 |
bool operator==(const MapIterator<Map>& p_it) const {
|
deba@822
|
215 |
return p_it.it == it;
|
deba@822
|
216 |
}
|
deba@822
|
217 |
|
deba@822
|
218 |
/** The not-equality operator of the map.
|
deba@822
|
219 |
*/
|
deba@822
|
220 |
bool operator!=(const MapIterator<Map>& p_it) const {
|
deba@822
|
221 |
return !(*this == p_it);
|
deba@822
|
222 |
}
|
deba@822
|
223 |
|
deba@822
|
224 |
/** The equality operator of the map.
|
deba@822
|
225 |
*/
|
deba@822
|
226 |
bool operator==(const MapConstIterator& p_it) const {
|
deba@822
|
227 |
return p_it.it == it;
|
deba@822
|
228 |
}
|
deba@822
|
229 |
|
deba@822
|
230 |
/** The not-equality operator of the map.
|
deba@822
|
231 |
*/
|
deba@822
|
232 |
bool operator!=(const MapConstIterator& p_it) const {
|
deba@822
|
233 |
return !(*this == p_it);
|
deba@822
|
234 |
}
|
deba@822
|
235 |
|
deba@822
|
236 |
private:
|
deba@822
|
237 |
const Map* map;
|
deba@822
|
238 |
KeyIt it;
|
deba@822
|
239 |
};
|
deba@822
|
240 |
|
deba@822
|
241 |
}
|
deba@822
|
242 |
|
deba@822
|
243 |
#endif
|