deba@782
|
1 |
// -*- c++ -*-
|
deba@782
|
2 |
#ifndef VECTOR_MAP_H
|
deba@782
|
3 |
#define VECTOR_MAP_H
|
deba@782
|
4 |
|
deba@782
|
5 |
#include <vector>
|
deba@782
|
6 |
|
deba@782
|
7 |
#include <hugo/extended_pair.h>
|
deba@782
|
8 |
|
alpar@785
|
9 |
///\ingroup graphmapfactory
|
alpar@785
|
10 |
///\file
|
alpar@785
|
11 |
///\brief Vector based graph maps.
|
alpar@785
|
12 |
|
deba@782
|
13 |
namespace hugo {
|
alpar@785
|
14 |
|
alpar@785
|
15 |
/// \addtogroup graphmapfactory
|
alpar@785
|
16 |
/// @{
|
alpar@785
|
17 |
|
deba@782
|
18 |
/** The VectorMapFactory template class is a factory class
|
deba@782
|
19 |
* to create maps for the edge and nodes. This map factory
|
deba@798
|
20 |
* uses the std::vector to implement the container function.
|
deba@782
|
21 |
*
|
deba@782
|
22 |
* The template parameter is the MapRegistry that the maps
|
deba@782
|
23 |
* will belong to.
|
deba@782
|
24 |
*/
|
deba@782
|
25 |
|
deba@782
|
26 |
template <typename MapRegistry>
|
deba@782
|
27 |
class VectorMapFactory {
|
deba@782
|
28 |
public:
|
deba@782
|
29 |
|
deba@782
|
30 |
/// The graph type of the maps.
|
deba@782
|
31 |
typedef typename MapRegistry::Graph Graph;
|
deba@782
|
32 |
/// The key type of the maps.
|
alpar@786
|
33 |
typedef typename MapRegistry::KeyType KeyType;
|
deba@782
|
34 |
/// The iterator to iterate on the keys.
|
deba@782
|
35 |
typedef typename MapRegistry::KeyIt KeyIt;
|
deba@782
|
36 |
|
deba@782
|
37 |
/// The MapBase of the Map which imlements the core regisitry function.
|
deba@782
|
38 |
typedef typename MapRegistry::MapBase MapBase;
|
deba@782
|
39 |
|
deba@782
|
40 |
|
deba@782
|
41 |
/** The template Map type.
|
deba@782
|
42 |
*/
|
deba@782
|
43 |
template <typename V>
|
deba@782
|
44 |
class Map : public MapBase {
|
deba@798
|
45 |
|
deba@798
|
46 |
typedef std::vector<V> Container;
|
deba@798
|
47 |
|
deba@782
|
48 |
public:
|
deba@782
|
49 |
|
deba@782
|
50 |
/// The value type of the map.
|
deba@798
|
51 |
typedef V ValueType;
|
deba@798
|
52 |
|
deba@798
|
53 |
/// The value type of the map.
|
deba@782
|
54 |
typedef V Value;
|
deba@798
|
55 |
/// The reference type of the map;
|
deba@798
|
56 |
typedef typename Container::reference Reference;
|
deba@798
|
57 |
/// The pointer type of the map;
|
deba@798
|
58 |
typedef typename Container::pointer Pointer;
|
deba@782
|
59 |
|
deba@798
|
60 |
/// The const value type of the map.
|
deba@798
|
61 |
typedef const Value ConstValue;
|
deba@798
|
62 |
/// The const reference type of the map;
|
deba@798
|
63 |
typedef typename Container::const_reference ConstReference;
|
deba@798
|
64 |
/// The pointer type of the map;
|
deba@798
|
65 |
typedef typename Container::const_pointer ConstPointer;
|
deba@782
|
66 |
|
deba@782
|
67 |
/** Default constructor for the map.
|
deba@782
|
68 |
*/
|
deba@782
|
69 |
Map() {}
|
deba@782
|
70 |
|
deba@782
|
71 |
/** Graph and Registry initialized map constructor.
|
deba@782
|
72 |
*/
|
deba@782
|
73 |
Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
|
deba@782
|
74 |
init();
|
deba@782
|
75 |
}
|
deba@782
|
76 |
|
deba@782
|
77 |
/** Constructor to use default value to initialize the map.
|
deba@782
|
78 |
*/
|
deba@782
|
79 |
Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
|
deba@782
|
80 |
for (KeyIt it(*getGraph()); it != INVALID; ++it) {
|
deba@782
|
81 |
int id = getGraph()->id(it);
|
deba@798
|
82 |
if (id >= (int)container.size()) {
|
deba@782
|
83 |
container.resize(id + 1);
|
deba@782
|
84 |
}
|
deba@782
|
85 |
set(it, v);
|
deba@782
|
86 |
}
|
deba@782
|
87 |
}
|
deba@782
|
88 |
|
deba@782
|
89 |
/** Constructor to copy a map of an other map type.
|
deba@782
|
90 |
*/
|
deba@782
|
91 |
template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
|
deba@782
|
92 |
if (getGraph()) {
|
deba@782
|
93 |
for (KeyIt it(*getGraph()); it != INVALID; ++it) {
|
deba@782
|
94 |
int id = getGraph()->id(it);
|
deba@798
|
95 |
if (id >= (int)container.size()) {
|
deba@782
|
96 |
container.resize(id + 1);
|
deba@782
|
97 |
}
|
deba@782
|
98 |
set(it, copy[it]);
|
deba@782
|
99 |
}
|
deba@782
|
100 |
}
|
deba@782
|
101 |
}
|
deba@782
|
102 |
|
deba@782
|
103 |
/** Assign operator to copy a map an other map type.
|
deba@782
|
104 |
*/
|
deba@782
|
105 |
template <typename CMap> Map& operator=(const CMap& copy) {
|
deba@782
|
106 |
if (getGraph()) {
|
deba@782
|
107 |
destroy();
|
deba@782
|
108 |
}
|
deba@782
|
109 |
this->MapBase::operator=(copy);
|
deba@782
|
110 |
if (getGraph()) {
|
deba@782
|
111 |
for (KeyIt it(*getGraph()); it != INVALID; ++it) {
|
deba@782
|
112 |
int id = getGraph()->id(it);
|
deba@798
|
113 |
if (id >= (int)container.size()) {
|
deba@782
|
114 |
container.resize(id + 1);
|
deba@782
|
115 |
}
|
deba@782
|
116 |
set(it, copy[it]);
|
deba@782
|
117 |
}
|
deba@782
|
118 |
}
|
deba@798
|
119 |
return *this;
|
deba@782
|
120 |
}
|
deba@782
|
121 |
|
deba@782
|
122 |
/** The destructor of the map.
|
deba@782
|
123 |
*/
|
deba@782
|
124 |
virtual ~Map() {
|
deba@782
|
125 |
}
|
deba@782
|
126 |
|
deba@782
|
127 |
/**
|
deba@782
|
128 |
* The subscript operator. The map can be subscripted by the
|
deba@782
|
129 |
* actual keys of the graph.
|
deba@782
|
130 |
*/
|
deba@799
|
131 |
Reference operator[](const KeyType& key) {
|
deba@782
|
132 |
int id = getGraph()->id(key);
|
deba@782
|
133 |
return container[id];
|
deba@782
|
134 |
}
|
deba@782
|
135 |
|
deba@782
|
136 |
/**
|
deba@782
|
137 |
* The const subscript operator. The map can be subscripted by the
|
deba@782
|
138 |
* actual keys of the graph.
|
deba@782
|
139 |
*/
|
deba@799
|
140 |
ConstReference operator[](const KeyType& key) const {
|
deba@782
|
141 |
int id = getGraph()->id(key);
|
deba@782
|
142 |
return container[id];
|
deba@782
|
143 |
}
|
deba@782
|
144 |
|
deba@782
|
145 |
/** Setter function of the map. Equivalent with map[key] = val.
|
deba@782
|
146 |
* This is a compatibility feature with the not dereferable maps.
|
deba@782
|
147 |
*/
|
alpar@786
|
148 |
void set(const KeyType& key, const Value& val) {
|
deba@782
|
149 |
int id = getGraph()->id(key);
|
deba@782
|
150 |
container[id] = val;
|
deba@782
|
151 |
}
|
deba@782
|
152 |
|
deba@782
|
153 |
/** Add a new key to the map. It called by the map registry.
|
deba@782
|
154 |
*/
|
alpar@786
|
155 |
void add(const KeyType& key) {
|
deba@782
|
156 |
int id = getGraph()->id(key);
|
deba@798
|
157 |
if (id >= (int)container.size()) {
|
deba@782
|
158 |
container.resize(id + 1);
|
deba@782
|
159 |
}
|
deba@782
|
160 |
}
|
deba@782
|
161 |
|
deba@782
|
162 |
/** Erase a key from the map. It called by the map registry.
|
deba@782
|
163 |
*/
|
alpar@786
|
164 |
void erase(const KeyType& key) {}
|
deba@782
|
165 |
|
deba@782
|
166 |
/** Clear the data structure.
|
deba@782
|
167 |
*/
|
deba@782
|
168 |
void clear() {
|
deba@782
|
169 |
container.clear();
|
deba@782
|
170 |
}
|
deba@782
|
171 |
|
deba@782
|
172 |
/** Compatible iterator with the stl maps' iterators.
|
deba@782
|
173 |
* It iterates on pairs of a key and a value.
|
deba@782
|
174 |
*/
|
deba@782
|
175 |
class iterator {
|
deba@782
|
176 |
friend class Map;
|
deba@782
|
177 |
friend class const_iterator;
|
deba@782
|
178 |
private:
|
deba@782
|
179 |
|
deba@782
|
180 |
/** Private constructor to initalize the the iterators returned
|
deba@782
|
181 |
* by the begin() and end().
|
deba@782
|
182 |
*/
|
deba@782
|
183 |
iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
|
deba@782
|
184 |
|
deba@782
|
185 |
public:
|
deba@782
|
186 |
|
deba@782
|
187 |
/** Default constructor.
|
deba@782
|
188 |
*/
|
deba@782
|
189 |
iterator() {}
|
deba@782
|
190 |
|
alpar@786
|
191 |
typedef extended_pair<const KeyType&, const KeyType&,
|
deba@798
|
192 |
Map::Reference, Map::Reference> Reference;
|
deba@782
|
193 |
|
deba@782
|
194 |
/** Dereference operator for map.
|
deba@782
|
195 |
*/
|
deba@782
|
196 |
Reference operator*() {
|
deba@782
|
197 |
return Reference(it, (*map)[it]);
|
deba@782
|
198 |
}
|
deba@782
|
199 |
|
deba@782
|
200 |
class Pointer {
|
deba@782
|
201 |
friend class iterator;
|
deba@782
|
202 |
private:
|
deba@782
|
203 |
Reference data;
|
deba@799
|
204 |
Pointer(const KeyType& key, Map::Reference val) : data(key, val) {}
|
deba@782
|
205 |
public:
|
deba@782
|
206 |
Reference* operator->() {return &data;}
|
deba@782
|
207 |
};
|
deba@782
|
208 |
|
deba@782
|
209 |
/** Arrow operator for map.
|
deba@782
|
210 |
*/
|
deba@782
|
211 |
Pointer operator->() {
|
deba@782
|
212 |
return Pointer(it, ((*map)[it]));
|
deba@782
|
213 |
}
|
deba@782
|
214 |
|
deba@782
|
215 |
/** The pre increment operator of the map.
|
deba@782
|
216 |
*/
|
deba@782
|
217 |
iterator& operator++() {
|
deba@782
|
218 |
++it;
|
deba@782
|
219 |
return *this;
|
deba@782
|
220 |
}
|
deba@782
|
221 |
|
deba@782
|
222 |
/** The post increment operator of the map.
|
deba@782
|
223 |
*/
|
deba@782
|
224 |
iterator operator++(int) {
|
deba@782
|
225 |
iterator tmp(it);
|
deba@782
|
226 |
++it;
|
deba@782
|
227 |
return tmp;
|
deba@782
|
228 |
}
|
deba@782
|
229 |
|
deba@782
|
230 |
/** The equality operator of the map.
|
deba@782
|
231 |
*/
|
deba@782
|
232 |
bool operator==(const_iterator p_it) {
|
deba@782
|
233 |
return p_it.it == it;
|
deba@782
|
234 |
}
|
deba@782
|
235 |
|
deba@782
|
236 |
/** The not-equality operator of the map.
|
deba@782
|
237 |
*/
|
deba@782
|
238 |
bool operator!=(const_iterator p_it) {
|
deba@782
|
239 |
return !(*this == p_it);
|
deba@782
|
240 |
}
|
deba@782
|
241 |
|
deba@782
|
242 |
|
deba@782
|
243 |
private:
|
deba@782
|
244 |
Map* map;
|
deba@782
|
245 |
KeyIt it;
|
deba@782
|
246 |
};
|
deba@782
|
247 |
|
deba@782
|
248 |
/** Returns the begin iterator of the map.
|
deba@782
|
249 |
*/
|
deba@782
|
250 |
iterator begin() {
|
deba@782
|
251 |
return iterator(*this, KeyIt(*getGraph()));
|
deba@782
|
252 |
}
|
deba@782
|
253 |
|
deba@782
|
254 |
/** Returns the end iterator of the map.
|
deba@782
|
255 |
*/
|
deba@782
|
256 |
iterator end() {
|
deba@782
|
257 |
return iterator(*this, INVALID);
|
deba@782
|
258 |
}
|
deba@782
|
259 |
|
deba@782
|
260 |
class const_iterator {
|
deba@782
|
261 |
friend class Map;
|
deba@782
|
262 |
friend class iterator;
|
deba@782
|
263 |
private:
|
deba@782
|
264 |
|
deba@782
|
265 |
/** Private constructor to initalize the the iterators returned
|
deba@782
|
266 |
* by the begin() and end().
|
deba@782
|
267 |
*/
|
deba@782
|
268 |
const_iterator (const Map& pmap, const KeyIt& pit)
|
deba@782
|
269 |
: map(&pmap), it(pit) {}
|
deba@782
|
270 |
|
deba@782
|
271 |
public:
|
deba@782
|
272 |
|
deba@782
|
273 |
/** Default constructor.
|
deba@782
|
274 |
*/
|
deba@782
|
275 |
const_iterator() {}
|
deba@782
|
276 |
|
deba@782
|
277 |
/** Constructor to convert iterator to const_iterator.
|
deba@782
|
278 |
*/
|
deba@782
|
279 |
const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
|
deba@782
|
280 |
|
alpar@786
|
281 |
typedef extended_pair<const KeyType&, const KeyType&,
|
deba@798
|
282 |
Map::ConstReference, Map::ConstReference> Reference;
|
deba@782
|
283 |
|
deba@782
|
284 |
/** Dereference operator for map.
|
deba@782
|
285 |
*/
|
deba@782
|
286 |
Reference operator*() {
|
deba@782
|
287 |
return Reference(it, (*map)[it]);
|
deba@782
|
288 |
}
|
deba@782
|
289 |
|
deba@782
|
290 |
|
deba@782
|
291 |
class Pointer {
|
deba@782
|
292 |
friend class const_iterator;
|
deba@782
|
293 |
private:
|
deba@782
|
294 |
Reference data;
|
deba@799
|
295 |
Pointer(const KeyType& key, Map::ConstReference val)
|
deba@799
|
296 |
: data(key, val) {}
|
deba@782
|
297 |
public:
|
deba@782
|
298 |
Reference* operator->() {return &data;}
|
deba@782
|
299 |
};
|
deba@782
|
300 |
|
deba@782
|
301 |
/** Arrow operator for map.
|
deba@782
|
302 |
*/
|
deba@782
|
303 |
Pointer operator->() {
|
deba@782
|
304 |
return Pointer(it, ((*map)[it]));
|
deba@782
|
305 |
}
|
deba@782
|
306 |
|
deba@782
|
307 |
/** The pre increment operator of the map.
|
deba@782
|
308 |
*/
|
deba@782
|
309 |
const_iterator& operator++() {
|
deba@782
|
310 |
++it;
|
deba@782
|
311 |
return *this;
|
deba@782
|
312 |
}
|
deba@782
|
313 |
|
deba@782
|
314 |
/** The post increment operator of the map.
|
deba@782
|
315 |
*/
|
deba@782
|
316 |
const_iterator operator++(int) {
|
deba@782
|
317 |
const_iterator tmp(it);
|
deba@782
|
318 |
++it;
|
deba@782
|
319 |
return tmp;
|
deba@782
|
320 |
}
|
deba@782
|
321 |
|
deba@782
|
322 |
/** The equality operator of the map.
|
deba@782
|
323 |
*/
|
deba@782
|
324 |
bool operator==(const_iterator p_it) {
|
deba@782
|
325 |
return p_it.it == it;
|
deba@782
|
326 |
}
|
deba@782
|
327 |
|
deba@782
|
328 |
/** The not-equality operator of the map.
|
deba@782
|
329 |
*/
|
deba@782
|
330 |
bool operator!=(const_iterator p_it) {
|
deba@782
|
331 |
return !(*this == p_it);
|
deba@782
|
332 |
}
|
deba@782
|
333 |
|
deba@782
|
334 |
|
deba@782
|
335 |
private:
|
deba@782
|
336 |
const Map* map;
|
deba@782
|
337 |
KeyIt it;
|
deba@782
|
338 |
};
|
deba@782
|
339 |
|
deba@782
|
340 |
/** Returns the begin const_iterator of the map.
|
deba@782
|
341 |
*/
|
deba@782
|
342 |
const_iterator begin() const {
|
deba@782
|
343 |
return const_iterator(*this, KeyIt(*getGraph()));
|
deba@782
|
344 |
}
|
deba@782
|
345 |
|
deba@782
|
346 |
/** Returns the end const_iterator of the map.
|
deba@782
|
347 |
*/
|
deba@782
|
348 |
const_iterator end() const {
|
deba@782
|
349 |
return const_iterator(*this, INVALID);
|
deba@782
|
350 |
}
|
deba@782
|
351 |
|
deba@782
|
352 |
private:
|
deba@782
|
353 |
|
deba@782
|
354 |
Container container;
|
deba@782
|
355 |
|
deba@782
|
356 |
};
|
deba@782
|
357 |
|
deba@782
|
358 |
};
|
deba@782
|
359 |
|
alpar@785
|
360 |
|
alpar@785
|
361 |
/// @}
|
alpar@785
|
362 |
|
alpar@785
|
363 |
|
deba@782
|
364 |
}
|
deba@782
|
365 |
|
deba@782
|
366 |
#endif
|