| 1 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
| 2 |
2 |
*
|
| 3 |
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
| 4 |
4 |
*
|
| 5 |
5 |
* Copyright (C) 2003-2009
|
| 6 |
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
| 7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
| 8 |
8 |
*
|
| 9 |
9 |
* Permission to use, modify and distribute this software is granted
|
| 10 |
10 |
* provided that this copyright notice appears in all copies. For
|
| 11 |
11 |
* precise terms see the accompanying LICENSE file.
|
| 12 |
12 |
*
|
| 13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
| 14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
| 15 |
15 |
* purpose.
|
| 16 |
16 |
*
|
| 17 |
17 |
*/
|
| 18 |
18 |
|
| 19 |
19 |
#ifndef LEMON_MAPS_H
|
| 20 |
20 |
#define LEMON_MAPS_H
|
| 21 |
21 |
|
| 22 |
22 |
#include <iterator>
|
| 23 |
23 |
#include <functional>
|
| 24 |
24 |
#include <vector>
|
| 25 |
25 |
|
| 26 |
26 |
#include <lemon/core.h>
|
|
27 |
#include <lemon/smart_graph.h>
|
| 27 |
28 |
|
| 28 |
29 |
///\file
|
| 29 |
30 |
///\ingroup maps
|
| 30 |
31 |
///\brief Miscellaneous property maps
|
| 31 |
32 |
|
| 32 |
33 |
#include <map>
|
| 33 |
34 |
|
| 34 |
35 |
namespace lemon {
|
| 35 |
36 |
|
| 36 |
37 |
/// \addtogroup maps
|
| 37 |
38 |
/// @{
|
| 38 |
39 |
|
| 39 |
40 |
/// Base class of maps.
|
| 40 |
41 |
|
| 41 |
42 |
/// Base class of maps. It provides the necessary type definitions
|
| 42 |
43 |
/// required by the map %concepts.
|
| 43 |
44 |
template<typename K, typename V>
|
| 44 |
45 |
class MapBase {
|
| 45 |
46 |
public:
|
| 46 |
47 |
/// \brief The key type of the map.
|
| 47 |
48 |
typedef K Key;
|
| 48 |
49 |
/// \brief The value type of the map.
|
| 49 |
50 |
/// (The type of objects associated with the keys).
|
| 50 |
51 |
typedef V Value;
|
| 51 |
52 |
};
|
| 52 |
53 |
|
| 53 |
54 |
|
| 54 |
55 |
/// Null map. (a.k.a. DoNothingMap)
|
| 55 |
56 |
|
| 56 |
57 |
/// This map can be used if you have to provide a map only for
|
| 57 |
58 |
/// its type definitions, or if you have to provide a writable map,
|
| 58 |
59 |
/// but data written to it is not required (i.e. it will be sent to
|
| 59 |
60 |
/// <tt>/dev/null</tt>).
|
| 60 |
61 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
| 61 |
62 |
///
|
| 62 |
63 |
/// \sa ConstMap
|
| 63 |
64 |
template<typename K, typename V>
|
| 64 |
65 |
class NullMap : public MapBase<K, V> {
|
| 65 |
66 |
public:
|
| 66 |
67 |
///\e
|
| 67 |
68 |
typedef K Key;
|
| 68 |
69 |
///\e
|
| 69 |
70 |
typedef V Value;
|
| 70 |
71 |
|
| 71 |
72 |
/// Gives back a default constructed element.
|
| 72 |
73 |
Value operator[](const Key&) const { return Value(); }
|
| 73 |
74 |
/// Absorbs the value.
|
| 74 |
75 |
void set(const Key&, const Value&) {}
|
| 75 |
76 |
};
|
| 76 |
77 |
|
| 77 |
78 |
/// Returns a \c NullMap class
|
| 78 |
79 |
|
| 79 |
80 |
/// This function just returns a \c NullMap class.
|
| 80 |
81 |
/// \relates NullMap
|
| 81 |
82 |
template <typename K, typename V>
|
| 82 |
83 |
NullMap<K, V> nullMap() {
|
| 83 |
84 |
return NullMap<K, V>();
|
| 84 |
85 |
}
|
| 85 |
86 |
|
| 86 |
87 |
|
| 87 |
88 |
/// Constant map.
|
| 88 |
89 |
|
| 89 |
90 |
/// This \ref concepts::ReadMap "readable map" assigns a specified
|
| 90 |
91 |
/// value to each key.
|
| 91 |
92 |
///
|
| 92 |
93 |
/// In other aspects it is equivalent to \c NullMap.
|
| 93 |
94 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
|
| 94 |
95 |
/// concept, but it absorbs the data written to it.
|
| 95 |
96 |
///
|
| 96 |
97 |
/// The simplest way of using this map is through the constMap()
|
| 97 |
98 |
/// function.
|
| 98 |
99 |
///
|
| 99 |
100 |
/// \sa NullMap
|
| 100 |
101 |
/// \sa IdentityMap
|
| 101 |
102 |
template<typename K, typename V>
|
| 102 |
103 |
class ConstMap : public MapBase<K, V> {
|
| 103 |
104 |
private:
|
| 104 |
105 |
V _value;
|
| 105 |
106 |
public:
|
| 106 |
107 |
///\e
|
| 107 |
108 |
typedef K Key;
|
| 108 |
109 |
///\e
|
| 109 |
110 |
typedef V Value;
|
| 110 |
111 |
|
| 111 |
112 |
/// Default constructor
|
| 112 |
113 |
|
| 113 |
114 |
/// Default constructor.
|
| 114 |
115 |
/// The value of the map will be default constructed.
|
| 115 |
116 |
ConstMap() {}
|
| 116 |
117 |
|
| 117 |
118 |
/// Constructor with specified initial value
|
| 118 |
119 |
|
| 119 |
120 |
/// Constructor with specified initial value.
|
| 120 |
121 |
/// \param v The initial value of the map.
|
| 121 |
122 |
ConstMap(const Value &v) : _value(v) {}
|
| 122 |
123 |
|
| 123 |
124 |
/// Gives back the specified value.
|
| 124 |
125 |
Value operator[](const Key&) const { return _value; }
|
| 125 |
126 |
|
| 126 |
127 |
/// Absorbs the value.
|
| 127 |
128 |
void set(const Key&, const Value&) {}
|
| 128 |
129 |
|
| 129 |
130 |
/// Sets the value that is assigned to each key.
|
| 130 |
131 |
void setAll(const Value &v) {
|
| 131 |
132 |
_value = v;
|
| 132 |
133 |
}
|
| 133 |
134 |
|
| 134 |
135 |
template<typename V1>
|
| 135 |
136 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
|
| 136 |
137 |
};
|
| 137 |
138 |
|
| 138 |
139 |
/// Returns a \c ConstMap class
|
| 139 |
140 |
|
| 140 |
141 |
/// This function just returns a \c ConstMap class.
|
| 141 |
142 |
/// \relates ConstMap
|
| 142 |
143 |
template<typename K, typename V>
|
| 143 |
144 |
inline ConstMap<K, V> constMap(const V &v) {
|
| 144 |
145 |
return ConstMap<K, V>(v);
|
| 145 |
146 |
}
|
| 146 |
147 |
|
| 147 |
148 |
template<typename K, typename V>
|
| 148 |
149 |
inline ConstMap<K, V> constMap() {
|
| 149 |
150 |
return ConstMap<K, V>();
|
| 150 |
151 |
}
|
| 151 |
152 |
|
| 152 |
153 |
|
| 153 |
154 |
template<typename T, T v>
|
| 154 |
155 |
struct Const {};
|
| 155 |
156 |
|
| 156 |
157 |
/// Constant map with inlined constant value.
|
| 157 |
158 |
|
| 158 |
159 |
/// This \ref concepts::ReadMap "readable map" assigns a specified
|
| 159 |
160 |
/// value to each key.
|
| 160 |
161 |
///
|
| 161 |
162 |
/// In other aspects it is equivalent to \c NullMap.
|
| 162 |
163 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
|
| 163 |
164 |
/// concept, but it absorbs the data written to it.
|
| 164 |
165 |
///
|
| 165 |
166 |
/// The simplest way of using this map is through the constMap()
|
| 166 |
167 |
/// function.
|
| 167 |
168 |
///
|
| 168 |
169 |
/// \sa NullMap
|
| 169 |
170 |
/// \sa IdentityMap
|
| 170 |
171 |
template<typename K, typename V, V v>
|
| 171 |
172 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
|
| 172 |
173 |
public:
|
| 173 |
174 |
///\e
|
| 174 |
175 |
typedef K Key;
|
| 175 |
176 |
///\e
|
| 176 |
177 |
typedef V Value;
|
| 177 |
178 |
|
| 178 |
179 |
/// Constructor.
|
| 179 |
180 |
ConstMap() {}
|
| 180 |
181 |
|
| 181 |
182 |
/// Gives back the specified value.
|
| 182 |
183 |
Value operator[](const Key&) const { return v; }
|
| 183 |
184 |
|
| 184 |
185 |
/// Absorbs the value.
|
| 185 |
186 |
void set(const Key&, const Value&) {}
|
| 186 |
187 |
};
|
| 187 |
188 |
|
| 188 |
189 |
/// Returns a \c ConstMap class with inlined constant value
|
| 189 |
190 |
|
| 190 |
191 |
/// This function just returns a \c ConstMap class with inlined
|
| 191 |
192 |
/// constant value.
|
| 192 |
193 |
/// \relates ConstMap
|
| 193 |
194 |
template<typename K, typename V, V v>
|
| 194 |
195 |
inline ConstMap<K, Const<V, v> > constMap() {
|
| 195 |
196 |
return ConstMap<K, Const<V, v> >();
|
| 196 |
197 |
}
|
| 197 |
198 |
|
| 198 |
199 |
|
| 199 |
200 |
/// Identity map.
|
| 200 |
201 |
|
| 201 |
202 |
/// This \ref concepts::ReadMap "read-only map" gives back the given
|
| 202 |
203 |
/// key as value without any modification.
|
| 203 |
204 |
///
|
| 204 |
205 |
/// \sa ConstMap
|
| 205 |
206 |
template <typename T>
|
| 206 |
207 |
class IdentityMap : public MapBase<T, T> {
|
| 207 |
208 |
public:
|
| 208 |
209 |
///\e
|
| 209 |
210 |
typedef T Key;
|
| 210 |
211 |
///\e
|
| 211 |
212 |
typedef T Value;
|
| 212 |
213 |
|
| 213 |
214 |
/// Gives back the given value without any modification.
|
| 214 |
215 |
Value operator[](const Key &k) const {
|
| 215 |
216 |
return k;
|
| 216 |
217 |
}
|
| 217 |
218 |
};
|
| 218 |
219 |
|
| 219 |
220 |
/// Returns an \c IdentityMap class
|
| 220 |
221 |
|
| 221 |
222 |
/// This function just returns an \c IdentityMap class.
|
| 222 |
223 |
/// \relates IdentityMap
|
| 223 |
224 |
template<typename T>
|
| 224 |
225 |
inline IdentityMap<T> identityMap() {
|
| 225 |
226 |
return IdentityMap<T>();
|
| 226 |
227 |
}
|
| 227 |
228 |
|
| 228 |
229 |
|
| 229 |
230 |
/// \brief Map for storing values for integer keys from the range
|
| 230 |
231 |
/// <tt>[0..size-1]</tt>.
|
| 231 |
232 |
///
|
| 232 |
233 |
/// This map is essentially a wrapper for \c std::vector. It assigns
|
| 233 |
234 |
/// values to integer keys from the range <tt>[0..size-1]</tt>.
|
| 234 |
235 |
/// It can be used with some data structures, for example
|
| 235 |
236 |
/// \c UnionFind, \c BinHeap, when the used items are small
|
| 236 |
237 |
/// integers. This map conforms the \ref concepts::ReferenceMap
|
| 237 |
238 |
/// "ReferenceMap" concept.
|
| 238 |
239 |
///
|
| 239 |
240 |
/// The simplest way of using this map is through the rangeMap()
|
| 240 |
241 |
/// function.
|
| 241 |
242 |
template <typename V>
|
| 242 |
243 |
class RangeMap : public MapBase<int, V> {
|
| 243 |
244 |
template <typename V1>
|
| 244 |
245 |
friend class RangeMap;
|
| 245 |
246 |
private:
|
| 246 |
247 |
|
| 247 |
248 |
typedef std::vector<V> Vector;
|
| 248 |
249 |
Vector _vector;
|
| 249 |
250 |
|
| 250 |
251 |
public:
|
| 251 |
252 |
|
| 252 |
253 |
/// Key type
|
| 253 |
254 |
typedef int Key;
|
| 254 |
255 |
/// Value type
|
| 255 |
256 |
typedef V Value;
|
| 256 |
257 |
/// Reference type
|
| 257 |
258 |
typedef typename Vector::reference Reference;
|
| 258 |
259 |
/// Const reference type
|
| 259 |
260 |
typedef typename Vector::const_reference ConstReference;
|
| 260 |
261 |
|
| 261 |
262 |
typedef True ReferenceMapTag;
|
| 262 |
263 |
|
| 263 |
264 |
public:
|
| 264 |
265 |
|
| 265 |
266 |
/// Constructor with specified default value.
|
| 266 |
267 |
RangeMap(int size = 0, const Value &value = Value())
|
| 267 |
268 |
: _vector(size, value) {}
|
| 268 |
269 |
|
| 269 |
270 |
/// Constructs the map from an appropriate \c std::vector.
|
| 270 |
271 |
template <typename V1>
|
| 271 |
272 |
RangeMap(const std::vector<V1>& vector)
|
| 272 |
273 |
: _vector(vector.begin(), vector.end()) {}
|
| 273 |
274 |
|
| 274 |
275 |
/// Constructs the map from another \c RangeMap.
|
| 275 |
276 |
template <typename V1>
|
| 276 |
277 |
RangeMap(const RangeMap<V1> &c)
|
| 277 |
278 |
: _vector(c._vector.begin(), c._vector.end()) {}
|
| 278 |
279 |
|
| 279 |
280 |
/// Returns the size of the map.
|
| 280 |
281 |
int size() {
|
| 281 |
282 |
return _vector.size();
|
| 282 |
283 |
}
|
| 283 |
284 |
|
| 284 |
285 |
/// Resizes the map.
|
| 285 |
286 |
|
| 286 |
287 |
/// Resizes the underlying \c std::vector container, so changes the
|
| 287 |
288 |
/// keyset of the map.
|
| 288 |
289 |
/// \param size The new size of the map. The new keyset will be the
|
| 289 |
290 |
/// range <tt>[0..size-1]</tt>.
|
| 290 |
291 |
/// \param value The default value to assign to the new keys.
|
| 291 |
292 |
void resize(int size, const Value &value = Value()) {
|
| 292 |
293 |
_vector.resize(size, value);
|
| 293 |
294 |
}
|
| 294 |
295 |
|
| 295 |
296 |
private:
|
| 296 |
297 |
|
| 297 |
298 |
RangeMap& operator=(const RangeMap&);
|
| 298 |
299 |
|
| 299 |
300 |
public:
|
| 300 |
301 |
|
| 301 |
302 |
///\e
|
| 302 |
303 |
Reference operator[](const Key &k) {
|
| 303 |
304 |
return _vector[k];
|
| 304 |
305 |
}
|
| 305 |
306 |
|
| 306 |
307 |
///\e
|
| 307 |
308 |
ConstReference operator[](const Key &k) const {
|
| 308 |
309 |
return _vector[k];
|
| 309 |
310 |
}
|
| 310 |
311 |
|
| 311 |
312 |
///\e
|
| 312 |
313 |
void set(const Key &k, const Value &v) {
|
| 313 |
314 |
_vector[k] = v;
|
| 314 |
315 |
}
|
| 315 |
316 |
};
|
| 316 |
317 |
|
| 317 |
318 |
/// Returns a \c RangeMap class
|
| 318 |
319 |
|
| 319 |
320 |
/// This function just returns a \c RangeMap class.
|
| 320 |
321 |
/// \relates RangeMap
|
| 321 |
322 |
template<typename V>
|
| 322 |
323 |
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
|
| 323 |
324 |
return RangeMap<V>(size, value);
|
| 324 |
325 |
}
|
| 325 |
326 |
|
| 326 |
327 |
/// \brief Returns a \c RangeMap class created from an appropriate
|
| 327 |
328 |
/// \c std::vector
|
| 328 |
329 |
|
| 329 |
330 |
/// This function just returns a \c RangeMap class created from an
|
| 330 |
331 |
/// appropriate \c std::vector.
|
| 331 |
332 |
/// \relates RangeMap
|
| 332 |
333 |
template<typename V>
|
| 333 |
334 |
inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
|
| 334 |
335 |
return RangeMap<V>(vector);
|
| 335 |
336 |
}
|
| 336 |
337 |
|
| 337 |
338 |
|
| 338 |
339 |
/// Map type based on \c std::map
|
| 339 |
340 |
|
| 340 |
341 |
/// This map is essentially a wrapper for \c std::map with addition
|
| 341 |
342 |
/// that you can specify a default value for the keys that are not
|
| 342 |
343 |
/// stored actually. This value can be different from the default
|
| 343 |
344 |
/// contructed value (i.e. \c %Value()).
|
| 344 |
345 |
/// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
|
| 345 |
346 |
/// concept.
|
| 346 |
347 |
///
|
| 347 |
348 |
/// This map is useful if a default value should be assigned to most of
|
| 348 |
349 |
/// the keys and different values should be assigned only to a few
|
| 349 |
350 |
/// keys (i.e. the map is "sparse").
|
| 350 |
351 |
/// The name of this type also refers to this important usage.
|
| 351 |
352 |
///
|
| 352 |
353 |
/// Apart form that this map can be used in many other cases since it
|
| 353 |
354 |
/// is based on \c std::map, which is a general associative container.
|
| 354 |
355 |
/// However keep in mind that it is usually not as efficient as other
|
| 355 |
356 |
/// maps.
|
| 356 |
357 |
///
|
| 357 |
358 |
/// The simplest way of using this map is through the sparseMap()
|
| 358 |
359 |
/// function.
|
| 359 |
360 |
template <typename K, typename V, typename Comp = std::less<K> >
|
| 360 |
361 |
class SparseMap : public MapBase<K, V> {
|
| 361 |
362 |
template <typename K1, typename V1, typename C1>
|
| 362 |
363 |
friend class SparseMap;
|
| 363 |
364 |
public:
|
| 364 |
365 |
|
| 365 |
366 |
/// Key type
|
| 366 |
367 |
typedef K Key;
|
| 367 |
368 |
/// Value type
|
| 368 |
369 |
typedef V Value;
|
| 369 |
370 |
/// Reference type
|
| 370 |
371 |
typedef Value& Reference;
|
| 371 |
372 |
/// Const reference type
|
| 372 |
373 |
typedef const Value& ConstReference;
|
| 373 |
374 |
|
| 374 |
375 |
typedef True ReferenceMapTag;
|
| 375 |
376 |
|
| 376 |
377 |
private:
|
| 377 |
378 |
|
| 378 |
379 |
typedef std::map<K, V, Comp> Map;
|
| 379 |
380 |
Map _map;
|
| 380 |
381 |
Value _value;
|
| 381 |
382 |
|
| 382 |
383 |
public:
|
| 383 |
384 |
|
| 384 |
385 |
/// \brief Constructor with specified default value.
|
| 385 |
386 |
SparseMap(const Value &value = Value()) : _value(value) {}
|
| 386 |
387 |
/// \brief Constructs the map from an appropriate \c std::map, and
|
| 387 |
388 |
/// explicitly specifies a default value.
|
| 388 |
389 |
template <typename V1, typename Comp1>
|
| 389 |
390 |
SparseMap(const std::map<Key, V1, Comp1> &map,
|
| 390 |
391 |
const Value &value = Value())
|
| 391 |
392 |
: _map(map.begin(), map.end()), _value(value) {}
|
| 392 |
393 |
|
| 393 |
394 |
/// \brief Constructs the map from another \c SparseMap.
|
| 394 |
395 |
template<typename V1, typename Comp1>
|
| 395 |
396 |
SparseMap(const SparseMap<Key, V1, Comp1> &c)
|
| 396 |
397 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {}
|
| 397 |
398 |
|
| 398 |
399 |
private:
|
| 399 |
400 |
|
| 400 |
401 |
SparseMap& operator=(const SparseMap&);
|
| 401 |
402 |
|
| 402 |
403 |
public:
|
| 403 |
404 |
|
| 404 |
405 |
///\e
|
| 405 |
406 |
Reference operator[](const Key &k) {
|
| 406 |
407 |
typename Map::iterator it = _map.lower_bound(k);
|
| 407 |
408 |
if (it != _map.end() && !_map.key_comp()(k, it->first))
|
| 408 |
409 |
return it->second;
|
| 409 |
410 |
else
|
| 410 |
411 |
return _map.insert(it, std::make_pair(k, _value))->second;
|
| ... |
... |
@@ -1437,1351 +1438,2239 @@
|
| 1437 |
1438 |
/// \sa NotMap, NotWriteMap
|
| 1438 |
1439 |
template<typename M1, typename M2>
|
| 1439 |
1440 |
class AndMap : public MapBase<typename M1::Key, bool> {
|
| 1440 |
1441 |
const M1 &_m1;
|
| 1441 |
1442 |
const M2 &_m2;
|
| 1442 |
1443 |
public:
|
| 1443 |
1444 |
///\e
|
| 1444 |
1445 |
typedef typename M1::Key Key;
|
| 1445 |
1446 |
///\e
|
| 1446 |
1447 |
typedef bool Value;
|
| 1447 |
1448 |
|
| 1448 |
1449 |
/// Constructor
|
| 1449 |
1450 |
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1450 |
1451 |
///\e
|
| 1451 |
1452 |
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
|
| 1452 |
1453 |
};
|
| 1453 |
1454 |
|
| 1454 |
1455 |
/// Returns an \c AndMap class
|
| 1455 |
1456 |
|
| 1456 |
1457 |
/// This function just returns an \c AndMap class.
|
| 1457 |
1458 |
///
|
| 1458 |
1459 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values,
|
| 1459 |
1460 |
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to
|
| 1460 |
1461 |
/// <tt>m1[x]&&m2[x]</tt>.
|
| 1461 |
1462 |
///
|
| 1462 |
1463 |
/// \relates AndMap
|
| 1463 |
1464 |
template<typename M1, typename M2>
|
| 1464 |
1465 |
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
|
| 1465 |
1466 |
return AndMap<M1, M2>(m1,m2);
|
| 1466 |
1467 |
}
|
| 1467 |
1468 |
|
| 1468 |
1469 |
|
| 1469 |
1470 |
/// Logical 'or' of two maps
|
| 1470 |
1471 |
|
| 1471 |
1472 |
/// This \ref concepts::ReadMap "read-only map" returns the logical
|
| 1472 |
1473 |
/// 'or' of the values of the two given maps.
|
| 1473 |
1474 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
| 1474 |
1475 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
| 1475 |
1476 |
///
|
| 1476 |
1477 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
| 1477 |
1478 |
/// \code
|
| 1478 |
1479 |
/// OrMap<M1,M2> om(m1,m2);
|
| 1479 |
1480 |
/// \endcode
|
| 1480 |
1481 |
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
|
| 1481 |
1482 |
///
|
| 1482 |
1483 |
/// The simplest way of using this map is through the orMap()
|
| 1483 |
1484 |
/// function.
|
| 1484 |
1485 |
///
|
| 1485 |
1486 |
/// \sa AndMap
|
| 1486 |
1487 |
/// \sa NotMap, NotWriteMap
|
| 1487 |
1488 |
template<typename M1, typename M2>
|
| 1488 |
1489 |
class OrMap : public MapBase<typename M1::Key, bool> {
|
| 1489 |
1490 |
const M1 &_m1;
|
| 1490 |
1491 |
const M2 &_m2;
|
| 1491 |
1492 |
public:
|
| 1492 |
1493 |
///\e
|
| 1493 |
1494 |
typedef typename M1::Key Key;
|
| 1494 |
1495 |
///\e
|
| 1495 |
1496 |
typedef bool Value;
|
| 1496 |
1497 |
|
| 1497 |
1498 |
/// Constructor
|
| 1498 |
1499 |
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1499 |
1500 |
///\e
|
| 1500 |
1501 |
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
|
| 1501 |
1502 |
};
|
| 1502 |
1503 |
|
| 1503 |
1504 |
/// Returns an \c OrMap class
|
| 1504 |
1505 |
|
| 1505 |
1506 |
/// This function just returns an \c OrMap class.
|
| 1506 |
1507 |
///
|
| 1507 |
1508 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values,
|
| 1508 |
1509 |
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to
|
| 1509 |
1510 |
/// <tt>m1[x]||m2[x]</tt>.
|
| 1510 |
1511 |
///
|
| 1511 |
1512 |
/// \relates OrMap
|
| 1512 |
1513 |
template<typename M1, typename M2>
|
| 1513 |
1514 |
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
|
| 1514 |
1515 |
return OrMap<M1, M2>(m1,m2);
|
| 1515 |
1516 |
}
|
| 1516 |
1517 |
|
| 1517 |
1518 |
|
| 1518 |
1519 |
/// Logical 'not' of a map
|
| 1519 |
1520 |
|
| 1520 |
1521 |
/// This \ref concepts::ReadMap "read-only map" returns the logical
|
| 1521 |
1522 |
/// negation of the values of the given map.
|
| 1522 |
1523 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool.
|
| 1523 |
1524 |
///
|
| 1524 |
1525 |
/// The simplest way of using this map is through the notMap()
|
| 1525 |
1526 |
/// function.
|
| 1526 |
1527 |
///
|
| 1527 |
1528 |
/// \sa NotWriteMap
|
| 1528 |
1529 |
template <typename M>
|
| 1529 |
1530 |
class NotMap : public MapBase<typename M::Key, bool> {
|
| 1530 |
1531 |
const M &_m;
|
| 1531 |
1532 |
public:
|
| 1532 |
1533 |
///\e
|
| 1533 |
1534 |
typedef typename M::Key Key;
|
| 1534 |
1535 |
///\e
|
| 1535 |
1536 |
typedef bool Value;
|
| 1536 |
1537 |
|
| 1537 |
1538 |
/// Constructor
|
| 1538 |
1539 |
NotMap(const M &m) : _m(m) {}
|
| 1539 |
1540 |
///\e
|
| 1540 |
1541 |
Value operator[](const Key &k) const { return !_m[k]; }
|
| 1541 |
1542 |
};
|
| 1542 |
1543 |
|
| 1543 |
1544 |
/// Logical 'not' of a map (read-write version)
|
| 1544 |
1545 |
|
| 1545 |
1546 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the
|
| 1546 |
1547 |
/// logical negation of the values of the given map.
|
| 1547 |
1548 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool.
|
| 1548 |
1549 |
/// It makes also possible to write the map. When a value is set,
|
| 1549 |
1550 |
/// the opposite value is set to the original map.
|
| 1550 |
1551 |
///
|
| 1551 |
1552 |
/// The simplest way of using this map is through the notWriteMap()
|
| 1552 |
1553 |
/// function.
|
| 1553 |
1554 |
///
|
| 1554 |
1555 |
/// \sa NotMap
|
| 1555 |
1556 |
template <typename M>
|
| 1556 |
1557 |
class NotWriteMap : public MapBase<typename M::Key, bool> {
|
| 1557 |
1558 |
M &_m;
|
| 1558 |
1559 |
public:
|
| 1559 |
1560 |
///\e
|
| 1560 |
1561 |
typedef typename M::Key Key;
|
| 1561 |
1562 |
///\e
|
| 1562 |
1563 |
typedef bool Value;
|
| 1563 |
1564 |
|
| 1564 |
1565 |
/// Constructor
|
| 1565 |
1566 |
NotWriteMap(M &m) : _m(m) {}
|
| 1566 |
1567 |
///\e
|
| 1567 |
1568 |
Value operator[](const Key &k) const { return !_m[k]; }
|
| 1568 |
1569 |
///\e
|
| 1569 |
1570 |
void set(const Key &k, bool v) { _m.set(k, !v); }
|
| 1570 |
1571 |
};
|
| 1571 |
1572 |
|
| 1572 |
1573 |
/// Returns a \c NotMap class
|
| 1573 |
1574 |
|
| 1574 |
1575 |
/// This function just returns a \c NotMap class.
|
| 1575 |
1576 |
///
|
| 1576 |
1577 |
/// For example, if \c m is a map with \c bool values, then
|
| 1577 |
1578 |
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
|
| 1578 |
1579 |
///
|
| 1579 |
1580 |
/// \relates NotMap
|
| 1580 |
1581 |
template <typename M>
|
| 1581 |
1582 |
inline NotMap<M> notMap(const M &m) {
|
| 1582 |
1583 |
return NotMap<M>(m);
|
| 1583 |
1584 |
}
|
| 1584 |
1585 |
|
| 1585 |
1586 |
/// Returns a \c NotWriteMap class
|
| 1586 |
1587 |
|
| 1587 |
1588 |
/// This function just returns a \c NotWriteMap class.
|
| 1588 |
1589 |
///
|
| 1589 |
1590 |
/// For example, if \c m is a map with \c bool values, then
|
| 1590 |
1591 |
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
|
| 1591 |
1592 |
/// Moreover it makes also possible to write the map.
|
| 1592 |
1593 |
///
|
| 1593 |
1594 |
/// \relates NotWriteMap
|
| 1594 |
1595 |
template <typename M>
|
| 1595 |
1596 |
inline NotWriteMap<M> notWriteMap(M &m) {
|
| 1596 |
1597 |
return NotWriteMap<M>(m);
|
| 1597 |
1598 |
}
|
| 1598 |
1599 |
|
| 1599 |
1600 |
|
| 1600 |
1601 |
/// Combination of two maps using the \c == operator
|
| 1601 |
1602 |
|
| 1602 |
1603 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
|
| 1603 |
1604 |
/// the keys for which the corresponding values of the two maps are
|
| 1604 |
1605 |
/// equal.
|
| 1605 |
1606 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
| 1606 |
1607 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
| 1607 |
1608 |
///
|
| 1608 |
1609 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
| 1609 |
1610 |
/// \code
|
| 1610 |
1611 |
/// EqualMap<M1,M2> em(m1,m2);
|
| 1611 |
1612 |
/// \endcode
|
| 1612 |
1613 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
|
| 1613 |
1614 |
///
|
| 1614 |
1615 |
/// The simplest way of using this map is through the equalMap()
|
| 1615 |
1616 |
/// function.
|
| 1616 |
1617 |
///
|
| 1617 |
1618 |
/// \sa LessMap
|
| 1618 |
1619 |
template<typename M1, typename M2>
|
| 1619 |
1620 |
class EqualMap : public MapBase<typename M1::Key, bool> {
|
| 1620 |
1621 |
const M1 &_m1;
|
| 1621 |
1622 |
const M2 &_m2;
|
| 1622 |
1623 |
public:
|
| 1623 |
1624 |
///\e
|
| 1624 |
1625 |
typedef typename M1::Key Key;
|
| 1625 |
1626 |
///\e
|
| 1626 |
1627 |
typedef bool Value;
|
| 1627 |
1628 |
|
| 1628 |
1629 |
/// Constructor
|
| 1629 |
1630 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1630 |
1631 |
///\e
|
| 1631 |
1632 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
|
| 1632 |
1633 |
};
|
| 1633 |
1634 |
|
| 1634 |
1635 |
/// Returns an \c EqualMap class
|
| 1635 |
1636 |
|
| 1636 |
1637 |
/// This function just returns an \c EqualMap class.
|
| 1637 |
1638 |
///
|
| 1638 |
1639 |
/// For example, if \c m1 and \c m2 are maps with keys and values of
|
| 1639 |
1640 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
|
| 1640 |
1641 |
/// <tt>m1[x]==m2[x]</tt>.
|
| 1641 |
1642 |
///
|
| 1642 |
1643 |
/// \relates EqualMap
|
| 1643 |
1644 |
template<typename M1, typename M2>
|
| 1644 |
1645 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
|
| 1645 |
1646 |
return EqualMap<M1, M2>(m1,m2);
|
| 1646 |
1647 |
}
|
| 1647 |
1648 |
|
| 1648 |
1649 |
|
| 1649 |
1650 |
/// Combination of two maps using the \c < operator
|
| 1650 |
1651 |
|
| 1651 |
1652 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
|
| 1652 |
1653 |
/// the keys for which the corresponding value of the first map is
|
| 1653 |
1654 |
/// less then the value of the second map.
|
| 1654 |
1655 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
| 1655 |
1656 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
| 1656 |
1657 |
///
|
| 1657 |
1658 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
| 1658 |
1659 |
/// \code
|
| 1659 |
1660 |
/// LessMap<M1,M2> lm(m1,m2);
|
| 1660 |
1661 |
/// \endcode
|
| 1661 |
1662 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
|
| 1662 |
1663 |
///
|
| 1663 |
1664 |
/// The simplest way of using this map is through the lessMap()
|
| 1664 |
1665 |
/// function.
|
| 1665 |
1666 |
///
|
| 1666 |
1667 |
/// \sa EqualMap
|
| 1667 |
1668 |
template<typename M1, typename M2>
|
| 1668 |
1669 |
class LessMap : public MapBase<typename M1::Key, bool> {
|
| 1669 |
1670 |
const M1 &_m1;
|
| 1670 |
1671 |
const M2 &_m2;
|
| 1671 |
1672 |
public:
|
| 1672 |
1673 |
///\e
|
| 1673 |
1674 |
typedef typename M1::Key Key;
|
| 1674 |
1675 |
///\e
|
| 1675 |
1676 |
typedef bool Value;
|
| 1676 |
1677 |
|
| 1677 |
1678 |
/// Constructor
|
| 1678 |
1679 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1679 |
1680 |
///\e
|
| 1680 |
1681 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
|
| 1681 |
1682 |
};
|
| 1682 |
1683 |
|
| 1683 |
1684 |
/// Returns an \c LessMap class
|
| 1684 |
1685 |
|
| 1685 |
1686 |
/// This function just returns an \c LessMap class.
|
| 1686 |
1687 |
///
|
| 1687 |
1688 |
/// For example, if \c m1 and \c m2 are maps with keys and values of
|
| 1688 |
1689 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
|
| 1689 |
1690 |
/// <tt>m1[x]<m2[x]</tt>.
|
| 1690 |
1691 |
///
|
| 1691 |
1692 |
/// \relates LessMap
|
| 1692 |
1693 |
template<typename M1, typename M2>
|
| 1693 |
1694 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
|
| 1694 |
1695 |
return LessMap<M1, M2>(m1,m2);
|
| 1695 |
1696 |
}
|
| 1696 |
1697 |
|
| 1697 |
1698 |
namespace _maps_bits {
|
| 1698 |
1699 |
|
| 1699 |
1700 |
template <typename _Iterator, typename Enable = void>
|
| 1700 |
1701 |
struct IteratorTraits {
|
| 1701 |
1702 |
typedef typename std::iterator_traits<_Iterator>::value_type Value;
|
| 1702 |
1703 |
};
|
| 1703 |
1704 |
|
| 1704 |
1705 |
template <typename _Iterator>
|
| 1705 |
1706 |
struct IteratorTraits<_Iterator,
|
| 1706 |
1707 |
typename exists<typename _Iterator::container_type>::type>
|
| 1707 |
1708 |
{
|
| 1708 |
1709 |
typedef typename _Iterator::container_type::value_type Value;
|
| 1709 |
1710 |
};
|
| 1710 |
1711 |
|
| 1711 |
1712 |
}
|
| 1712 |
1713 |
|
| 1713 |
1714 |
/// @}
|
| 1714 |
1715 |
|
| 1715 |
1716 |
/// \addtogroup maps
|
| 1716 |
1717 |
/// @{
|
| 1717 |
1718 |
|
| 1718 |
1719 |
/// \brief Writable bool map for logging each \c true assigned element
|
| 1719 |
1720 |
///
|
| 1720 |
1721 |
/// A \ref concepts::WriteMap "writable" bool map for logging
|
| 1721 |
1722 |
/// each \c true assigned element, i.e it copies subsequently each
|
| 1722 |
1723 |
/// keys set to \c true to the given iterator.
|
| 1723 |
1724 |
/// The most important usage of it is storing certain nodes or arcs
|
| 1724 |
1725 |
/// that were marked \c true by an algorithm.
|
| 1725 |
1726 |
///
|
| 1726 |
1727 |
/// There are several algorithms that provide solutions through bool
|
| 1727 |
1728 |
/// maps and most of them assign \c true at most once for each key.
|
| 1728 |
1729 |
/// In these cases it is a natural request to store each \c true
|
| 1729 |
1730 |
/// assigned elements (in order of the assignment), which can be
|
| 1730 |
1731 |
/// easily done with LoggerBoolMap.
|
| 1731 |
1732 |
///
|
| 1732 |
1733 |
/// The simplest way of using this map is through the loggerBoolMap()
|
| 1733 |
1734 |
/// function.
|
| 1734 |
1735 |
///
|
| 1735 |
1736 |
/// \tparam IT The type of the iterator.
|
| 1736 |
1737 |
/// \tparam KEY The key type of the map. The default value set
|
| 1737 |
1738 |
/// according to the iterator type should work in most cases.
|
| 1738 |
1739 |
///
|
| 1739 |
1740 |
/// \note The container of the iterator must contain enough space
|
| 1740 |
1741 |
/// for the elements or the iterator should be an inserter iterator.
|
| 1741 |
1742 |
#ifdef DOXYGEN
|
| 1742 |
1743 |
template <typename IT, typename KEY>
|
| 1743 |
1744 |
#else
|
| 1744 |
1745 |
template <typename IT,
|
| 1745 |
1746 |
typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
|
| 1746 |
1747 |
#endif
|
| 1747 |
1748 |
class LoggerBoolMap : public MapBase<KEY, bool> {
|
| 1748 |
1749 |
public:
|
| 1749 |
1750 |
|
| 1750 |
1751 |
///\e
|
| 1751 |
1752 |
typedef KEY Key;
|
| 1752 |
1753 |
///\e
|
| 1753 |
1754 |
typedef bool Value;
|
| 1754 |
1755 |
///\e
|
| 1755 |
1756 |
typedef IT Iterator;
|
| 1756 |
1757 |
|
| 1757 |
1758 |
/// Constructor
|
| 1758 |
1759 |
LoggerBoolMap(Iterator it)
|
| 1759 |
1760 |
: _begin(it), _end(it) {}
|
| 1760 |
1761 |
|
| 1761 |
1762 |
/// Gives back the given iterator set for the first key
|
| 1762 |
1763 |
Iterator begin() const {
|
| 1763 |
1764 |
return _begin;
|
| 1764 |
1765 |
}
|
| 1765 |
1766 |
|
| 1766 |
1767 |
/// Gives back the the 'after the last' iterator
|
| 1767 |
1768 |
Iterator end() const {
|
| 1768 |
1769 |
return _end;
|
| 1769 |
1770 |
}
|
| 1770 |
1771 |
|
| 1771 |
1772 |
/// The set function of the map
|
| 1772 |
1773 |
void set(const Key& key, Value value) {
|
| 1773 |
1774 |
if (value) {
|
| 1774 |
1775 |
*_end++ = key;
|
| 1775 |
1776 |
}
|
| 1776 |
1777 |
}
|
| 1777 |
1778 |
|
| 1778 |
1779 |
private:
|
| 1779 |
1780 |
Iterator _begin;
|
| 1780 |
1781 |
Iterator _end;
|
| 1781 |
1782 |
};
|
| 1782 |
1783 |
|
| 1783 |
1784 |
/// Returns a \c LoggerBoolMap class
|
| 1784 |
1785 |
|
| 1785 |
1786 |
/// This function just returns a \c LoggerBoolMap class.
|
| 1786 |
1787 |
///
|
| 1787 |
1788 |
/// The most important usage of it is storing certain nodes or arcs
|
| 1788 |
1789 |
/// that were marked \c true by an algorithm.
|
| 1789 |
1790 |
/// For example it makes easier to store the nodes in the processing
|
| 1790 |
1791 |
/// order of Dfs algorithm, as the following examples show.
|
| 1791 |
1792 |
/// \code
|
| 1792 |
1793 |
/// std::vector<Node> v;
|
| 1793 |
1794 |
/// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
|
| 1794 |
1795 |
/// \endcode
|
| 1795 |
1796 |
/// \code
|
| 1796 |
1797 |
/// std::vector<Node> v(countNodes(g));
|
| 1797 |
1798 |
/// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
|
| 1798 |
1799 |
/// \endcode
|
| 1799 |
1800 |
///
|
| 1800 |
1801 |
/// \note The container of the iterator must contain enough space
|
| 1801 |
1802 |
/// for the elements or the iterator should be an inserter iterator.
|
| 1802 |
1803 |
///
|
| 1803 |
1804 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
|
| 1804 |
1805 |
/// it cannot be used when a readable map is needed, for example as
|
| 1805 |
1806 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
|
| 1806 |
1807 |
///
|
| 1807 |
1808 |
/// \relates LoggerBoolMap
|
| 1808 |
1809 |
template<typename Iterator>
|
| 1809 |
1810 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
|
| 1810 |
1811 |
return LoggerBoolMap<Iterator>(it);
|
| 1811 |
1812 |
}
|
| 1812 |
1813 |
|
| 1813 |
1814 |
/// @}
|
| 1814 |
1815 |
|
| 1815 |
1816 |
/// \addtogroup graph_maps
|
| 1816 |
1817 |
/// @{
|
| 1817 |
1818 |
|
| 1818 |
1819 |
/// \brief Provides an immutable and unique id for each item in a graph.
|
| 1819 |
1820 |
///
|
| 1820 |
1821 |
/// IdMap provides a unique and immutable id for each item of the
|
| 1821 |
|
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
|
|
1822 |
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
|
| 1822 |
1823 |
/// - \b unique: different items get different ids,
|
| 1823 |
1824 |
/// - \b immutable: the id of an item does not change (even if you
|
| 1824 |
1825 |
/// delete other nodes).
|
| 1825 |
1826 |
///
|
| 1826 |
1827 |
/// Using this map you get access (i.e. can read) the inner id values of
|
| 1827 |
1828 |
/// the items stored in the graph, which is returned by the \c id()
|
| 1828 |
1829 |
/// function of the graph. This map can be inverted with its member
|
| 1829 |
1830 |
/// class \c InverseMap or with the \c operator() member.
|
| 1830 |
1831 |
///
|
| 1831 |
1832 |
/// \tparam GR The graph type.
|
| 1832 |
1833 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 1833 |
1834 |
/// \c GR::Edge).
|
| 1834 |
1835 |
///
|
| 1835 |
1836 |
/// \see RangeIdMap
|
| 1836 |
1837 |
template <typename GR, typename K>
|
| 1837 |
1838 |
class IdMap : public MapBase<K, int> {
|
| 1838 |
1839 |
public:
|
| 1839 |
1840 |
/// The graph type of IdMap.
|
| 1840 |
1841 |
typedef GR Graph;
|
| 1841 |
1842 |
typedef GR Digraph;
|
| 1842 |
1843 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge).
|
| 1843 |
1844 |
typedef K Item;
|
| 1844 |
1845 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge).
|
| 1845 |
1846 |
typedef K Key;
|
| 1846 |
1847 |
/// The value type of IdMap.
|
| 1847 |
1848 |
typedef int Value;
|
| 1848 |
1849 |
|
| 1849 |
1850 |
/// \brief Constructor.
|
| 1850 |
1851 |
///
|
| 1851 |
1852 |
/// Constructor of the map.
|
| 1852 |
1853 |
explicit IdMap(const Graph& graph) : _graph(&graph) {}
|
| 1853 |
1854 |
|
| 1854 |
1855 |
/// \brief Gives back the \e id of the item.
|
| 1855 |
1856 |
///
|
| 1856 |
1857 |
/// Gives back the immutable and unique \e id of the item.
|
| 1857 |
1858 |
int operator[](const Item& item) const { return _graph->id(item);}
|
| 1858 |
1859 |
|
| 1859 |
1860 |
/// \brief Gives back the \e item by its id.
|
| 1860 |
1861 |
///
|
| 1861 |
1862 |
/// Gives back the \e item by its id.
|
| 1862 |
1863 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
| 1863 |
1864 |
|
| 1864 |
1865 |
private:
|
| 1865 |
1866 |
const Graph* _graph;
|
| 1866 |
1867 |
|
| 1867 |
1868 |
public:
|
| 1868 |
1869 |
|
| 1869 |
1870 |
/// \brief This class represents the inverse of its owner (IdMap).
|
| 1870 |
1871 |
///
|
| 1871 |
1872 |
/// This class represents the inverse of its owner (IdMap).
|
| 1872 |
1873 |
/// \see inverse()
|
| 1873 |
1874 |
class InverseMap {
|
| 1874 |
1875 |
public:
|
| 1875 |
1876 |
|
| 1876 |
1877 |
/// \brief Constructor.
|
| 1877 |
1878 |
///
|
| 1878 |
1879 |
/// Constructor for creating an id-to-item map.
|
| 1879 |
1880 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
| 1880 |
1881 |
|
| 1881 |
1882 |
/// \brief Constructor.
|
| 1882 |
1883 |
///
|
| 1883 |
1884 |
/// Constructor for creating an id-to-item map.
|
| 1884 |
1885 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
| 1885 |
1886 |
|
| 1886 |
1887 |
/// \brief Gives back the given item from its id.
|
| 1887 |
1888 |
///
|
| 1888 |
1889 |
/// Gives back the given item from its id.
|
| 1889 |
1890 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
| 1890 |
1891 |
|
| 1891 |
1892 |
private:
|
| 1892 |
1893 |
const Graph* _graph;
|
| 1893 |
1894 |
};
|
| 1894 |
1895 |
|
| 1895 |
1896 |
/// \brief Gives back the inverse of the map.
|
| 1896 |
1897 |
///
|
| 1897 |
1898 |
/// Gives back the inverse of the IdMap.
|
| 1898 |
1899 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1899 |
1900 |
};
|
| 1900 |
1901 |
|
| 1901 |
1902 |
|
| 1902 |
1903 |
/// \brief General cross reference graph map type.
|
| 1903 |
1904 |
|
| 1904 |
1905 |
/// This class provides simple invertable graph maps.
|
| 1905 |
1906 |
/// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
|
| 1906 |
1907 |
/// and if a key is set to a new value then store it
|
| 1907 |
1908 |
/// in the inverse map.
|
| 1908 |
1909 |
///
|
| 1909 |
1910 |
/// The values of the map can be accessed
|
| 1910 |
1911 |
/// with stl compatible forward iterator.
|
| 1911 |
1912 |
///
|
| 1912 |
1913 |
/// \tparam GR The graph type.
|
| 1913 |
1914 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 1914 |
1915 |
/// \c GR::Edge).
|
| 1915 |
1916 |
/// \tparam V The value type of the map.
|
| 1916 |
1917 |
///
|
| 1917 |
1918 |
/// \see IterableValueMap
|
| 1918 |
1919 |
template <typename GR, typename K, typename V>
|
| 1919 |
1920 |
class CrossRefMap
|
| 1920 |
1921 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
| 1921 |
1922 |
private:
|
| 1922 |
1923 |
|
| 1923 |
1924 |
typedef typename ItemSetTraits<GR, K>::
|
| 1924 |
1925 |
template Map<V>::Type Map;
|
| 1925 |
1926 |
|
| 1926 |
1927 |
typedef std::map<V, K> Container;
|
| 1927 |
1928 |
Container _inv_map;
|
| 1928 |
1929 |
|
| 1929 |
1930 |
public:
|
| 1930 |
1931 |
|
| 1931 |
1932 |
/// The graph type of CrossRefMap.
|
| 1932 |
1933 |
typedef GR Graph;
|
| 1933 |
1934 |
typedef GR Digraph;
|
| 1934 |
1935 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
| 1935 |
1936 |
typedef K Item;
|
| 1936 |
1937 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
| 1937 |
1938 |
typedef K Key;
|
| 1938 |
1939 |
/// The value type of CrossRefMap.
|
| 1939 |
1940 |
typedef V Value;
|
| 1940 |
1941 |
|
| 1941 |
1942 |
/// \brief Constructor.
|
| 1942 |
1943 |
///
|
| 1943 |
1944 |
/// Construct a new CrossRefMap for the given graph.
|
| 1944 |
1945 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
|
| 1945 |
1946 |
|
| 1946 |
1947 |
/// \brief Forward iterator for values.
|
| 1947 |
1948 |
///
|
| 1948 |
1949 |
/// This iterator is an stl compatible forward
|
| 1949 |
1950 |
/// iterator on the values of the map. The values can
|
| 1950 |
1951 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
|
| 1951 |
1952 |
class ValueIterator
|
| 1952 |
1953 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1953 |
1954 |
friend class CrossRefMap;
|
| 1954 |
1955 |
private:
|
| 1955 |
1956 |
ValueIterator(typename Container::const_iterator _it)
|
| 1956 |
1957 |
: it(_it) {}
|
| 1957 |
1958 |
public:
|
| 1958 |
1959 |
|
| 1959 |
1960 |
ValueIterator() {}
|
| 1960 |
1961 |
|
| 1961 |
1962 |
ValueIterator& operator++() { ++it; return *this; }
|
| 1962 |
1963 |
ValueIterator operator++(int) {
|
| 1963 |
1964 |
ValueIterator tmp(*this);
|
| 1964 |
1965 |
operator++();
|
| 1965 |
1966 |
return tmp;
|
| 1966 |
1967 |
}
|
| 1967 |
1968 |
|
| 1968 |
1969 |
const Value& operator*() const { return it->first; }
|
| 1969 |
1970 |
const Value* operator->() const { return &(it->first); }
|
| 1970 |
1971 |
|
| 1971 |
1972 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
| 1972 |
1973 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
| 1973 |
1974 |
|
| 1974 |
1975 |
private:
|
| 1975 |
1976 |
typename Container::const_iterator it;
|
| 1976 |
1977 |
};
|
| 1977 |
1978 |
|
| 1978 |
1979 |
/// \brief Returns an iterator to the first value.
|
| 1979 |
1980 |
///
|
| 1980 |
1981 |
/// Returns an stl compatible iterator to the
|
| 1981 |
1982 |
/// first value of the map. The values of the
|
| 1982 |
1983 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 1983 |
1984 |
/// range.
|
| 1984 |
1985 |
ValueIterator beginValue() const {
|
| 1985 |
1986 |
return ValueIterator(_inv_map.begin());
|
| 1986 |
1987 |
}
|
| 1987 |
1988 |
|
| 1988 |
1989 |
/// \brief Returns an iterator after the last value.
|
| 1989 |
1990 |
///
|
| 1990 |
1991 |
/// Returns an stl compatible iterator after the
|
| 1991 |
1992 |
/// last value of the map. The values of the
|
| 1992 |
1993 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 1993 |
1994 |
/// range.
|
| 1994 |
1995 |
ValueIterator endValue() const {
|
| 1995 |
1996 |
return ValueIterator(_inv_map.end());
|
| 1996 |
1997 |
}
|
| 1997 |
1998 |
|
| 1998 |
1999 |
/// \brief Sets the value associated with the given key.
|
| 1999 |
2000 |
///
|
| 2000 |
2001 |
/// Sets the value associated with the given key.
|
| 2001 |
2002 |
void set(const Key& key, const Value& val) {
|
| 2002 |
2003 |
Value oldval = Map::operator[](key);
|
| 2003 |
2004 |
typename Container::iterator it = _inv_map.find(oldval);
|
| 2004 |
2005 |
if (it != _inv_map.end() && it->second == key) {
|
| 2005 |
2006 |
_inv_map.erase(it);
|
| 2006 |
2007 |
}
|
| 2007 |
|
_inv_map.insert(make_pair(val, key));
|
|
2008 |
_inv_map.insert(std::make_pair(val, key));
|
| 2008 |
2009 |
Map::set(key, val);
|
| 2009 |
2010 |
}
|
| 2010 |
2011 |
|
| 2011 |
2012 |
/// \brief Returns the value associated with the given key.
|
| 2012 |
2013 |
///
|
| 2013 |
2014 |
/// Returns the value associated with the given key.
|
| 2014 |
2015 |
typename MapTraits<Map>::ConstReturnValue
|
| 2015 |
2016 |
operator[](const Key& key) const {
|
| 2016 |
2017 |
return Map::operator[](key);
|
| 2017 |
2018 |
}
|
| 2018 |
2019 |
|
| 2019 |
2020 |
/// \brief Gives back the item by its value.
|
| 2020 |
2021 |
///
|
| 2021 |
2022 |
/// Gives back the item by its value.
|
| 2022 |
2023 |
Key operator()(const Value& key) const {
|
| 2023 |
2024 |
typename Container::const_iterator it = _inv_map.find(key);
|
| 2024 |
2025 |
return it != _inv_map.end() ? it->second : INVALID;
|
| 2025 |
2026 |
}
|
| 2026 |
2027 |
|
| 2027 |
2028 |
protected:
|
| 2028 |
2029 |
|
| 2029 |
2030 |
/// \brief Erase the key from the map and the inverse map.
|
| 2030 |
2031 |
///
|
| 2031 |
2032 |
/// Erase the key from the map and the inverse map. It is called by the
|
| 2032 |
2033 |
/// \c AlterationNotifier.
|
| 2033 |
2034 |
virtual void erase(const Key& key) {
|
| 2034 |
2035 |
Value val = Map::operator[](key);
|
| 2035 |
2036 |
typename Container::iterator it = _inv_map.find(val);
|
| 2036 |
2037 |
if (it != _inv_map.end() && it->second == key) {
|
| 2037 |
2038 |
_inv_map.erase(it);
|
| 2038 |
2039 |
}
|
| 2039 |
2040 |
Map::erase(key);
|
| 2040 |
2041 |
}
|
| 2041 |
2042 |
|
| 2042 |
2043 |
/// \brief Erase more keys from the map and the inverse map.
|
| 2043 |
2044 |
///
|
| 2044 |
2045 |
/// Erase more keys from the map and the inverse map. It is called by the
|
| 2045 |
2046 |
/// \c AlterationNotifier.
|
| 2046 |
2047 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2047 |
2048 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2048 |
2049 |
Value val = Map::operator[](keys[i]);
|
| 2049 |
2050 |
typename Container::iterator it = _inv_map.find(val);
|
| 2050 |
2051 |
if (it != _inv_map.end() && it->second == keys[i]) {
|
| 2051 |
2052 |
_inv_map.erase(it);
|
| 2052 |
2053 |
}
|
| 2053 |
2054 |
}
|
| 2054 |
2055 |
Map::erase(keys);
|
| 2055 |
2056 |
}
|
| 2056 |
2057 |
|
| 2057 |
2058 |
/// \brief Clear the keys from the map and the inverse map.
|
| 2058 |
2059 |
///
|
| 2059 |
2060 |
/// Clear the keys from the map and the inverse map. It is called by the
|
| 2060 |
2061 |
/// \c AlterationNotifier.
|
| 2061 |
2062 |
virtual void clear() {
|
| 2062 |
2063 |
_inv_map.clear();
|
| 2063 |
2064 |
Map::clear();
|
| 2064 |
2065 |
}
|
| 2065 |
2066 |
|
| 2066 |
2067 |
public:
|
| 2067 |
2068 |
|
| 2068 |
2069 |
/// \brief The inverse map type.
|
| 2069 |
2070 |
///
|
| 2070 |
2071 |
/// The inverse of this map. The subscript operator of the map
|
| 2071 |
2072 |
/// gives back the item that was last assigned to the value.
|
| 2072 |
2073 |
class InverseMap {
|
| 2073 |
2074 |
public:
|
| 2074 |
2075 |
/// \brief Constructor
|
| 2075 |
2076 |
///
|
| 2076 |
2077 |
/// Constructor of the InverseMap.
|
| 2077 |
2078 |
explicit InverseMap(const CrossRefMap& inverted)
|
| 2078 |
2079 |
: _inverted(inverted) {}
|
| 2079 |
2080 |
|
| 2080 |
2081 |
/// The value type of the InverseMap.
|
| 2081 |
2082 |
typedef typename CrossRefMap::Key Value;
|
| 2082 |
2083 |
/// The key type of the InverseMap.
|
| 2083 |
2084 |
typedef typename CrossRefMap::Value Key;
|
| 2084 |
2085 |
|
| 2085 |
2086 |
/// \brief Subscript operator.
|
| 2086 |
2087 |
///
|
| 2087 |
2088 |
/// Subscript operator. It gives back the item
|
| 2088 |
2089 |
/// that was last assigned to the given value.
|
| 2089 |
2090 |
Value operator[](const Key& key) const {
|
| 2090 |
2091 |
return _inverted(key);
|
| 2091 |
2092 |
}
|
| 2092 |
2093 |
|
| 2093 |
2094 |
private:
|
| 2094 |
2095 |
const CrossRefMap& _inverted;
|
| 2095 |
2096 |
};
|
| 2096 |
2097 |
|
| 2097 |
2098 |
/// \brief It gives back the read-only inverse map.
|
| 2098 |
2099 |
///
|
| 2099 |
2100 |
/// It gives back the read-only inverse map.
|
| 2100 |
2101 |
InverseMap inverse() const {
|
| 2101 |
2102 |
return InverseMap(*this);
|
| 2102 |
2103 |
}
|
| 2103 |
2104 |
|
| 2104 |
2105 |
};
|
| 2105 |
2106 |
|
| 2106 |
2107 |
/// \brief Provides continuous and unique ID for the
|
| 2107 |
2108 |
/// items of a graph.
|
| 2108 |
2109 |
///
|
| 2109 |
2110 |
/// RangeIdMap provides a unique and continuous
|
| 2110 |
2111 |
/// ID for each item of a given type (\c Node, \c Arc or
|
| 2111 |
2112 |
/// \c Edge) in a graph. This id is
|
| 2112 |
2113 |
/// - \b unique: different items get different ids,
|
| 2113 |
2114 |
/// - \b continuous: the range of the ids is the set of integers
|
| 2114 |
2115 |
/// between 0 and \c n-1, where \c n is the number of the items of
|
| 2115 |
2116 |
/// this type (\c Node, \c Arc or \c Edge).
|
| 2116 |
2117 |
/// - So, the ids can change when deleting an item of the same type.
|
| 2117 |
2118 |
///
|
| 2118 |
2119 |
/// Thus this id is not (necessarily) the same as what can get using
|
| 2119 |
2120 |
/// the \c id() function of the graph or \ref IdMap.
|
| 2120 |
2121 |
/// This map can be inverted with its member class \c InverseMap,
|
| 2121 |
2122 |
/// or with the \c operator() member.
|
| 2122 |
2123 |
///
|
| 2123 |
2124 |
/// \tparam GR The graph type.
|
| 2124 |
2125 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 2125 |
2126 |
/// \c GR::Edge).
|
| 2126 |
2127 |
///
|
| 2127 |
2128 |
/// \see IdMap
|
| 2128 |
2129 |
template <typename GR, typename K>
|
| 2129 |
2130 |
class RangeIdMap
|
| 2130 |
2131 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2131 |
2132 |
|
| 2132 |
2133 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
|
| 2133 |
2134 |
|
| 2134 |
2135 |
public:
|
| 2135 |
2136 |
/// The graph type of RangeIdMap.
|
| 2136 |
2137 |
typedef GR Graph;
|
| 2137 |
2138 |
typedef GR Digraph;
|
| 2138 |
2139 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
|
| 2139 |
2140 |
typedef K Item;
|
| 2140 |
2141 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
|
| 2141 |
2142 |
typedef K Key;
|
| 2142 |
2143 |
/// The value type of RangeIdMap.
|
| 2143 |
2144 |
typedef int Value;
|
| 2144 |
2145 |
|
| 2145 |
2146 |
/// \brief Constructor.
|
| 2146 |
2147 |
///
|
| 2147 |
2148 |
/// Constructor.
|
| 2148 |
2149 |
explicit RangeIdMap(const Graph& gr) : Map(gr) {
|
| 2149 |
2150 |
Item it;
|
| 2150 |
2151 |
const typename Map::Notifier* nf = Map::notifier();
|
| 2151 |
2152 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2152 |
2153 |
Map::set(it, _inv_map.size());
|
| 2153 |
2154 |
_inv_map.push_back(it);
|
| 2154 |
2155 |
}
|
| 2155 |
2156 |
}
|
| 2156 |
2157 |
|
| 2157 |
2158 |
protected:
|
| 2158 |
2159 |
|
| 2159 |
2160 |
/// \brief Adds a new key to the map.
|
| 2160 |
2161 |
///
|
| 2161 |
2162 |
/// Add a new key to the map. It is called by the
|
| 2162 |
2163 |
/// \c AlterationNotifier.
|
| 2163 |
2164 |
virtual void add(const Item& item) {
|
| 2164 |
2165 |
Map::add(item);
|
| 2165 |
2166 |
Map::set(item, _inv_map.size());
|
| 2166 |
2167 |
_inv_map.push_back(item);
|
| 2167 |
2168 |
}
|
| 2168 |
2169 |
|
| 2169 |
2170 |
/// \brief Add more new keys to the map.
|
| 2170 |
2171 |
///
|
| 2171 |
2172 |
/// Add more new keys to the map. It is called by the
|
| 2172 |
2173 |
/// \c AlterationNotifier.
|
| 2173 |
2174 |
virtual void add(const std::vector<Item>& items) {
|
| 2174 |
2175 |
Map::add(items);
|
| 2175 |
2176 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2176 |
2177 |
Map::set(items[i], _inv_map.size());
|
| 2177 |
2178 |
_inv_map.push_back(items[i]);
|
| 2178 |
2179 |
}
|
| 2179 |
2180 |
}
|
| 2180 |
2181 |
|
| 2181 |
2182 |
/// \brief Erase the key from the map.
|
| 2182 |
2183 |
///
|
| 2183 |
2184 |
/// Erase the key from the map. It is called by the
|
| 2184 |
2185 |
/// \c AlterationNotifier.
|
| 2185 |
2186 |
virtual void erase(const Item& item) {
|
| 2186 |
2187 |
Map::set(_inv_map.back(), Map::operator[](item));
|
| 2187 |
2188 |
_inv_map[Map::operator[](item)] = _inv_map.back();
|
| 2188 |
2189 |
_inv_map.pop_back();
|
| 2189 |
2190 |
Map::erase(item);
|
| 2190 |
2191 |
}
|
| 2191 |
2192 |
|
| 2192 |
2193 |
/// \brief Erase more keys from the map.
|
| 2193 |
2194 |
///
|
| 2194 |
2195 |
/// Erase more keys from the map. It is called by the
|
| 2195 |
2196 |
/// \c AlterationNotifier.
|
| 2196 |
2197 |
virtual void erase(const std::vector<Item>& items) {
|
| 2197 |
2198 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2198 |
2199 |
Map::set(_inv_map.back(), Map::operator[](items[i]));
|
| 2199 |
2200 |
_inv_map[Map::operator[](items[i])] = _inv_map.back();
|
| 2200 |
2201 |
_inv_map.pop_back();
|
| 2201 |
2202 |
}
|
| 2202 |
2203 |
Map::erase(items);
|
| 2203 |
2204 |
}
|
| 2204 |
2205 |
|
| 2205 |
2206 |
/// \brief Build the unique map.
|
| 2206 |
2207 |
///
|
| 2207 |
2208 |
/// Build the unique map. It is called by the
|
| 2208 |
2209 |
/// \c AlterationNotifier.
|
| 2209 |
2210 |
virtual void build() {
|
| 2210 |
2211 |
Map::build();
|
| 2211 |
2212 |
Item it;
|
| 2212 |
2213 |
const typename Map::Notifier* nf = Map::notifier();
|
| 2213 |
2214 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2214 |
2215 |
Map::set(it, _inv_map.size());
|
| 2215 |
2216 |
_inv_map.push_back(it);
|
| 2216 |
2217 |
}
|
| 2217 |
2218 |
}
|
| 2218 |
2219 |
|
| 2219 |
2220 |
/// \brief Clear the keys from the map.
|
| 2220 |
2221 |
///
|
| 2221 |
2222 |
/// Clear the keys from the map. It is called by the
|
| 2222 |
2223 |
/// \c AlterationNotifier.
|
| 2223 |
2224 |
virtual void clear() {
|
| 2224 |
2225 |
_inv_map.clear();
|
| 2225 |
2226 |
Map::clear();
|
| 2226 |
2227 |
}
|
| 2227 |
2228 |
|
| 2228 |
2229 |
public:
|
| 2229 |
2230 |
|
| 2230 |
2231 |
/// \brief Returns the maximal value plus one.
|
| 2231 |
2232 |
///
|
| 2232 |
2233 |
/// Returns the maximal value plus one in the map.
|
| 2233 |
2234 |
unsigned int size() const {
|
| 2234 |
2235 |
return _inv_map.size();
|
| 2235 |
2236 |
}
|
| 2236 |
2237 |
|
| 2237 |
2238 |
/// \brief Swaps the position of the two items in the map.
|
| 2238 |
2239 |
///
|
| 2239 |
2240 |
/// Swaps the position of the two items in the map.
|
| 2240 |
2241 |
void swap(const Item& p, const Item& q) {
|
| 2241 |
2242 |
int pi = Map::operator[](p);
|
| 2242 |
2243 |
int qi = Map::operator[](q);
|
| 2243 |
2244 |
Map::set(p, qi);
|
| 2244 |
2245 |
_inv_map[qi] = p;
|
| 2245 |
2246 |
Map::set(q, pi);
|
| 2246 |
2247 |
_inv_map[pi] = q;
|
| 2247 |
2248 |
}
|
| 2248 |
2249 |
|
| 2249 |
2250 |
/// \brief Gives back the \e RangeId of the item
|
| 2250 |
2251 |
///
|
| 2251 |
2252 |
/// Gives back the \e RangeId of the item.
|
| 2252 |
2253 |
int operator[](const Item& item) const {
|
| 2253 |
2254 |
return Map::operator[](item);
|
| 2254 |
2255 |
}
|
| 2255 |
2256 |
|
| 2256 |
2257 |
/// \brief Gives back the item belonging to a \e RangeId
|
| 2257 |
|
///
|
|
2258 |
///
|
| 2258 |
2259 |
/// Gives back the item belonging to a \e RangeId.
|
| 2259 |
2260 |
Item operator()(int id) const {
|
| 2260 |
2261 |
return _inv_map[id];
|
| 2261 |
2262 |
}
|
| 2262 |
2263 |
|
| 2263 |
2264 |
private:
|
| 2264 |
2265 |
|
| 2265 |
2266 |
typedef std::vector<Item> Container;
|
| 2266 |
2267 |
Container _inv_map;
|
| 2267 |
2268 |
|
| 2268 |
2269 |
public:
|
| 2269 |
2270 |
|
| 2270 |
2271 |
/// \brief The inverse map type of RangeIdMap.
|
| 2271 |
2272 |
///
|
| 2272 |
2273 |
/// The inverse map type of RangeIdMap.
|
| 2273 |
2274 |
class InverseMap {
|
| 2274 |
2275 |
public:
|
| 2275 |
2276 |
/// \brief Constructor
|
| 2276 |
2277 |
///
|
| 2277 |
2278 |
/// Constructor of the InverseMap.
|
| 2278 |
2279 |
explicit InverseMap(const RangeIdMap& inverted)
|
| 2279 |
2280 |
: _inverted(inverted) {}
|
| 2280 |
2281 |
|
| 2281 |
2282 |
|
| 2282 |
2283 |
/// The value type of the InverseMap.
|
| 2283 |
2284 |
typedef typename RangeIdMap::Key Value;
|
| 2284 |
2285 |
/// The key type of the InverseMap.
|
| 2285 |
2286 |
typedef typename RangeIdMap::Value Key;
|
| 2286 |
2287 |
|
| 2287 |
2288 |
/// \brief Subscript operator.
|
| 2288 |
2289 |
///
|
| 2289 |
2290 |
/// Subscript operator. It gives back the item
|
| 2290 |
2291 |
/// that the descriptor currently belongs to.
|
| 2291 |
2292 |
Value operator[](const Key& key) const {
|
| 2292 |
2293 |
return _inverted(key);
|
| 2293 |
2294 |
}
|
| 2294 |
2295 |
|
| 2295 |
2296 |
/// \brief Size of the map.
|
| 2296 |
2297 |
///
|
| 2297 |
2298 |
/// Returns the size of the map.
|
| 2298 |
2299 |
unsigned int size() const {
|
| 2299 |
2300 |
return _inverted.size();
|
| 2300 |
2301 |
}
|
| 2301 |
2302 |
|
| 2302 |
2303 |
private:
|
| 2303 |
2304 |
const RangeIdMap& _inverted;
|
| 2304 |
2305 |
};
|
| 2305 |
2306 |
|
| 2306 |
2307 |
/// \brief Gives back the inverse of the map.
|
| 2307 |
2308 |
///
|
| 2308 |
2309 |
/// Gives back the inverse of the map.
|
| 2309 |
2310 |
const InverseMap inverse() const {
|
| 2310 |
2311 |
return InverseMap(*this);
|
| 2311 |
2312 |
}
|
| 2312 |
2313 |
};
|
| 2313 |
2314 |
|
|
2315 |
/// \brief Dynamic iterable bool map.
|
|
2316 |
///
|
|
2317 |
/// This class provides a special graph map type which can store for
|
|
2318 |
/// each graph item(node, arc, edge, etc.) a bool value. For both
|
|
2319 |
/// the true and the false values it is possible to iterate on the
|
|
2320 |
/// keys.
|
|
2321 |
///
|
|
2322 |
/// \param GR The graph type.
|
|
2323 |
/// \param ITEM One of the graph's item types, the key of the map.
|
|
2324 |
template <typename GR, typename ITEM>
|
|
2325 |
class IterableBoolMap
|
|
2326 |
: protected ItemSetTraits<GR, ITEM>::template Map<int>::Type {
|
|
2327 |
private:
|
|
2328 |
typedef GR Graph;
|
|
2329 |
|
|
2330 |
typedef typename ItemSetTraits<Graph, ITEM>::ItemIt KeyIt;
|
|
2331 |
typedef typename ItemSetTraits<GR, ITEM>::template Map<int>::Type Parent;
|
|
2332 |
|
|
2333 |
std::vector<ITEM> _array;
|
|
2334 |
int _sep;
|
|
2335 |
|
|
2336 |
public:
|
|
2337 |
|
|
2338 |
/// Indicates that the map if reference map.
|
|
2339 |
typedef True ReferenceMapTag;
|
|
2340 |
|
|
2341 |
/// The key type
|
|
2342 |
typedef ITEM Key;
|
|
2343 |
/// The value type
|
|
2344 |
typedef bool Value;
|
|
2345 |
/// The const reference type.
|
|
2346 |
typedef const Value& ConstReference;
|
|
2347 |
|
|
2348 |
private:
|
|
2349 |
|
|
2350 |
int position(const Key& key) const {
|
|
2351 |
return Parent::operator[](key);
|
|
2352 |
}
|
|
2353 |
|
|
2354 |
public:
|
|
2355 |
|
|
2356 |
/// \brief Refernce to the value of the map.
|
|
2357 |
///
|
|
2358 |
/// This class is similar to the bool type. It can be converted to
|
|
2359 |
/// bool and it provides the same operators.
|
|
2360 |
class Reference {
|
|
2361 |
friend class IterableBoolMap;
|
|
2362 |
private:
|
|
2363 |
Reference(IterableBoolMap& map, const Key& key)
|
|
2364 |
: _key(key), _map(map) {}
|
|
2365 |
public:
|
|
2366 |
|
|
2367 |
Reference& operator=(const Reference& value) {
|
|
2368 |
_map.set(_key, static_cast<bool>(value));
|
|
2369 |
return *this;
|
|
2370 |
}
|
|
2371 |
|
|
2372 |
operator bool() const {
|
|
2373 |
return static_cast<const IterableBoolMap&>(_map)[_key];
|
|
2374 |
}
|
|
2375 |
|
|
2376 |
Reference& operator=(bool value) {
|
|
2377 |
_map.set(_key, value);
|
|
2378 |
return *this;
|
|
2379 |
}
|
|
2380 |
Reference& operator&=(bool value) {
|
|
2381 |
_map.set(_key, _map[_key] & value);
|
|
2382 |
return *this;
|
|
2383 |
}
|
|
2384 |
Reference& operator|=(bool value) {
|
|
2385 |
_map.set(_key, _map[_key] | value);
|
|
2386 |
return *this;
|
|
2387 |
}
|
|
2388 |
Reference& operator^=(bool value) {
|
|
2389 |
_map.set(_key, _map[_key] ^ value);
|
|
2390 |
return *this;
|
|
2391 |
}
|
|
2392 |
private:
|
|
2393 |
Key _key;
|
|
2394 |
IterableBoolMap& _map;
|
|
2395 |
};
|
|
2396 |
|
|
2397 |
/// \brief Constructor of the map with a default value.
|
|
2398 |
///
|
|
2399 |
/// Constructor of the map with a default value.
|
|
2400 |
explicit IterableBoolMap(const Graph& graph, bool def = false)
|
|
2401 |
: Parent(graph) {
|
|
2402 |
typename Parent::Notifier* nf = Parent::notifier();
|
|
2403 |
Key it;
|
|
2404 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
2405 |
Parent::set(it, _array.size());
|
|
2406 |
_array.push_back(it);
|
|
2407 |
}
|
|
2408 |
_sep = (def ? _array.size() : 0);
|
|
2409 |
}
|
|
2410 |
|
|
2411 |
/// \brief Const subscript operator of the map.
|
|
2412 |
///
|
|
2413 |
/// Const subscript operator of the map.
|
|
2414 |
bool operator[](const Key& key) const {
|
|
2415 |
return position(key) < _sep;
|
|
2416 |
}
|
|
2417 |
|
|
2418 |
/// \brief Subscript operator of the map.
|
|
2419 |
///
|
|
2420 |
/// Subscript operator of the map.
|
|
2421 |
Reference operator[](const Key& key) {
|
|
2422 |
return Reference(*this, key);
|
|
2423 |
}
|
|
2424 |
|
|
2425 |
/// \brief Set operation of the map.
|
|
2426 |
///
|
|
2427 |
/// Set operation of the map.
|
|
2428 |
void set(const Key& key, bool value) {
|
|
2429 |
int pos = position(key);
|
|
2430 |
if (value) {
|
|
2431 |
if (pos < _sep) return;
|
|
2432 |
Key tmp = _array[_sep];
|
|
2433 |
_array[_sep] = key;
|
|
2434 |
Parent::set(key, _sep);
|
|
2435 |
_array[pos] = tmp;
|
|
2436 |
Parent::set(tmp, pos);
|
|
2437 |
++_sep;
|
|
2438 |
} else {
|
|
2439 |
if (pos >= _sep) return;
|
|
2440 |
--_sep;
|
|
2441 |
Key tmp = _array[_sep];
|
|
2442 |
_array[_sep] = key;
|
|
2443 |
Parent::set(key, _sep);
|
|
2444 |
_array[pos] = tmp;
|
|
2445 |
Parent::set(tmp, pos);
|
|
2446 |
}
|
|
2447 |
}
|
|
2448 |
|
|
2449 |
/// \brief Set all items.
|
|
2450 |
///
|
|
2451 |
/// Set all items in the map.
|
|
2452 |
/// \note Constant time operation.
|
|
2453 |
void setAll(bool value) {
|
|
2454 |
_sep = (value ? _array.size() : 0);
|
|
2455 |
}
|
|
2456 |
|
|
2457 |
/// \brief Returns the number of the keys mapped to true.
|
|
2458 |
///
|
|
2459 |
/// Returns the number of the keys mapped to true.
|
|
2460 |
int trueNum() const {
|
|
2461 |
return _sep;
|
|
2462 |
}
|
|
2463 |
|
|
2464 |
/// \brief Returns the number of the keys mapped to false.
|
|
2465 |
///
|
|
2466 |
/// Returns the number of the keys mapped to false.
|
|
2467 |
int falseNum() const {
|
|
2468 |
return _array.size() - _sep;
|
|
2469 |
}
|
|
2470 |
|
|
2471 |
/// \brief Iterator for the keys mapped to true.
|
|
2472 |
///
|
|
2473 |
/// Iterator for the keys mapped to true. It works
|
|
2474 |
/// like a graph item iterator in the map, it can be converted
|
|
2475 |
/// the key type of the map, incremented with \c ++ operator, and
|
|
2476 |
/// if the iterator leave the last valid key it will be equal to
|
|
2477 |
/// \c INVALID.
|
|
2478 |
class TrueIt : public Key {
|
|
2479 |
public:
|
|
2480 |
typedef Key Parent;
|
|
2481 |
|
|
2482 |
/// \brief Creates an iterator.
|
|
2483 |
///
|
|
2484 |
/// Creates an iterator. It iterates on the
|
|
2485 |
/// keys which mapped to true.
|
|
2486 |
/// \param map The IterableIntMap
|
|
2487 |
explicit TrueIt(const IterableBoolMap& map)
|
|
2488 |
: Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
|
|
2489 |
_map(&map) {}
|
|
2490 |
|
|
2491 |
/// \brief Invalid constructor \& conversion.
|
|
2492 |
///
|
|
2493 |
/// This constructor initializes the key to be invalid.
|
|
2494 |
/// \sa Invalid for more details.
|
|
2495 |
TrueIt(Invalid) : Parent(INVALID), _map(0) {}
|
|
2496 |
|
|
2497 |
/// \brief Increment operator.
|
|
2498 |
///
|
|
2499 |
/// Increment Operator.
|
|
2500 |
TrueIt& operator++() {
|
|
2501 |
int pos = _map->position(*this);
|
|
2502 |
Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
|
|
2503 |
return *this;
|
|
2504 |
}
|
|
2505 |
|
|
2506 |
|
|
2507 |
private:
|
|
2508 |
const IterableBoolMap* _map;
|
|
2509 |
};
|
|
2510 |
|
|
2511 |
/// \brief Iterator for the keys mapped to false.
|
|
2512 |
///
|
|
2513 |
/// Iterator for the keys mapped to false. It works
|
|
2514 |
/// like a graph item iterator in the map, it can be converted
|
|
2515 |
/// the key type of the map, incremented with \c ++ operator, and
|
|
2516 |
/// if the iterator leave the last valid key it will be equal to
|
|
2517 |
/// \c INVALID.
|
|
2518 |
class FalseIt : public Key {
|
|
2519 |
public:
|
|
2520 |
typedef Key Parent;
|
|
2521 |
|
|
2522 |
/// \brief Creates an iterator.
|
|
2523 |
///
|
|
2524 |
/// Creates an iterator. It iterates on the
|
|
2525 |
/// keys which mapped to false.
|
|
2526 |
/// \param map The IterableIntMap
|
|
2527 |
explicit FalseIt(const IterableBoolMap& map)
|
|
2528 |
: Parent(map._sep < int(map._array.size()) ?
|
|
2529 |
map._array.back() : INVALID), _map(&map) {}
|
|
2530 |
|
|
2531 |
/// \brief Invalid constructor \& conversion.
|
|
2532 |
///
|
|
2533 |
/// This constructor initializes the key to be invalid.
|
|
2534 |
/// \sa Invalid for more details.
|
|
2535 |
FalseIt(Invalid) : Parent(INVALID), _map(0) {}
|
|
2536 |
|
|
2537 |
/// \brief Increment operator.
|
|
2538 |
///
|
|
2539 |
/// Increment Operator.
|
|
2540 |
FalseIt& operator++() {
|
|
2541 |
int pos = _map->position(*this);
|
|
2542 |
Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
|
|
2543 |
return *this;
|
|
2544 |
}
|
|
2545 |
|
|
2546 |
private:
|
|
2547 |
const IterableBoolMap* _map;
|
|
2548 |
};
|
|
2549 |
|
|
2550 |
/// \brief Iterator for the keys mapped to a given value.
|
|
2551 |
///
|
|
2552 |
/// Iterator for the keys mapped to a given value. It works
|
|
2553 |
/// like a graph item iterator in the map, it can be converted
|
|
2554 |
/// the key type of the map, incremented with \c ++ operator, and
|
|
2555 |
/// if the iterator leave the last valid key it will be equal to
|
|
2556 |
/// \c INVALID.
|
|
2557 |
class ItemIt : public Key {
|
|
2558 |
public:
|
|
2559 |
typedef Key Parent;
|
|
2560 |
|
|
2561 |
/// \brief Creates an iterator.
|
|
2562 |
///
|
|
2563 |
/// Creates an iterator. It iterates on the
|
|
2564 |
/// keys which mapped to false.
|
|
2565 |
/// \param map The IterableIntMap
|
|
2566 |
/// \param value Which elements should be iterated.
|
|
2567 |
ItemIt(const IterableBoolMap& map, bool value)
|
|
2568 |
: Parent(value ?
|
|
2569 |
(map._sep > 0 ?
|
|
2570 |
map._array[map._sep - 1] : INVALID) :
|
|
2571 |
(map._sep < int(map._array.size()) ?
|
|
2572 |
map._array.back() : INVALID)), _map(&map) {}
|
|
2573 |
|
|
2574 |
/// \brief Invalid constructor \& conversion.
|
|
2575 |
///
|
|
2576 |
/// This constructor initializes the key to be invalid.
|
|
2577 |
/// \sa Invalid for more details.
|
|
2578 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
|
2579 |
|
|
2580 |
/// \brief Increment operator.
|
|
2581 |
///
|
|
2582 |
/// Increment Operator.
|
|
2583 |
ItemIt& operator++() {
|
|
2584 |
int pos = _map->position(*this);
|
|
2585 |
int _sep = pos >= _map->_sep ? _map->_sep : 0;
|
|
2586 |
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
|
|
2587 |
return *this;
|
|
2588 |
}
|
|
2589 |
|
|
2590 |
private:
|
|
2591 |
const IterableBoolMap* _map;
|
|
2592 |
};
|
|
2593 |
|
|
2594 |
protected:
|
|
2595 |
|
|
2596 |
virtual void add(const Key& key) {
|
|
2597 |
Parent::add(key);
|
|
2598 |
Parent::set(key, _array.size());
|
|
2599 |
_array.push_back(key);
|
|
2600 |
}
|
|
2601 |
|
|
2602 |
virtual void add(const std::vector<Key>& keys) {
|
|
2603 |
Parent::add(keys);
|
|
2604 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
2605 |
Parent::set(keys[i], _array.size());
|
|
2606 |
_array.push_back(keys[i]);
|
|
2607 |
}
|
|
2608 |
}
|
|
2609 |
|
|
2610 |
virtual void erase(const Key& key) {
|
|
2611 |
int pos = position(key);
|
|
2612 |
if (pos < _sep) {
|
|
2613 |
--_sep;
|
|
2614 |
Parent::set(_array[_sep], pos);
|
|
2615 |
_array[pos] = _array[_sep];
|
|
2616 |
Parent::set(_array.back(), _sep);
|
|
2617 |
_array[_sep] = _array.back();
|
|
2618 |
_array.pop_back();
|
|
2619 |
} else {
|
|
2620 |
Parent::set(_array.back(), pos);
|
|
2621 |
_array[pos] = _array.back();
|
|
2622 |
_array.pop_back();
|
|
2623 |
}
|
|
2624 |
Parent::erase(key);
|
|
2625 |
}
|
|
2626 |
|
|
2627 |
virtual void erase(const std::vector<Key>& keys) {
|
|
2628 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
2629 |
int pos = position(keys[i]);
|
|
2630 |
if (pos < _sep) {
|
|
2631 |
--_sep;
|
|
2632 |
Parent::set(_array[_sep], pos);
|
|
2633 |
_array[pos] = _array[_sep];
|
|
2634 |
Parent::set(_array.back(), _sep);
|
|
2635 |
_array[_sep] = _array.back();
|
|
2636 |
_array.pop_back();
|
|
2637 |
} else {
|
|
2638 |
Parent::set(_array.back(), pos);
|
|
2639 |
_array[pos] = _array.back();
|
|
2640 |
_array.pop_back();
|
|
2641 |
}
|
|
2642 |
}
|
|
2643 |
Parent::erase(keys);
|
|
2644 |
}
|
|
2645 |
|
|
2646 |
virtual void build() {
|
|
2647 |
Parent::build();
|
|
2648 |
typename Parent::Notifier* nf = Parent::notifier();
|
|
2649 |
Key it;
|
|
2650 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
2651 |
Parent::set(it, _array.size());
|
|
2652 |
_array.push_back(it);
|
|
2653 |
}
|
|
2654 |
_sep = 0;
|
|
2655 |
}
|
|
2656 |
|
|
2657 |
virtual void clear() {
|
|
2658 |
_array.clear();
|
|
2659 |
_sep = 0;
|
|
2660 |
Parent::clear();
|
|
2661 |
}
|
|
2662 |
|
|
2663 |
};
|
|
2664 |
|
|
2665 |
|
|
2666 |
namespace _maps_bits {
|
|
2667 |
template <typename Item>
|
|
2668 |
struct IterableIntMapNode {
|
|
2669 |
IterableIntMapNode() : value(-1) {}
|
|
2670 |
IterableIntMapNode(int _value) : value(_value) {}
|
|
2671 |
Item prev, next;
|
|
2672 |
int value;
|
|
2673 |
};
|
|
2674 |
}
|
|
2675 |
|
|
2676 |
///\ingroup graph_maps
|
|
2677 |
///
|
|
2678 |
/// \brief Dynamic iterable integer map.
|
|
2679 |
///
|
|
2680 |
/// This class provides a special graph map type which can store
|
|
2681 |
/// for each graph item(node, edge, etc.) an integer value. For each
|
|
2682 |
/// non negative value it is possible to iterate on the keys which
|
|
2683 |
/// mapped to the given value.
|
|
2684 |
///
|
|
2685 |
/// \note The size of the data structure depends on the highest
|
|
2686 |
/// value in the map.
|
|
2687 |
///
|
|
2688 |
/// \param GR The graph type.
|
|
2689 |
/// \param ITEM One of the graph's item type, the key of the map.
|
|
2690 |
template <typename GR, typename ITEM>
|
|
2691 |
class IterableIntMap
|
|
2692 |
: protected ItemSetTraits<GR, ITEM>::
|
|
2693 |
template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type {
|
|
2694 |
public:
|
|
2695 |
typedef typename ItemSetTraits<GR, ITEM>::
|
|
2696 |
template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type Parent;
|
|
2697 |
|
|
2698 |
/// The key type
|
|
2699 |
typedef ITEM Key;
|
|
2700 |
/// The value type
|
|
2701 |
typedef int Value;
|
|
2702 |
/// The graph type
|
|
2703 |
typedef GR Graph;
|
|
2704 |
|
|
2705 |
/// \brief Constructor of the map.
|
|
2706 |
///
|
|
2707 |
/// Constructor of the map. It set all values to -1.
|
|
2708 |
explicit IterableIntMap(const Graph& graph)
|
|
2709 |
: Parent(graph) {}
|
|
2710 |
|
|
2711 |
/// \brief Constructor of the map with a given value.
|
|
2712 |
///
|
|
2713 |
/// Constructor of the map with a given value.
|
|
2714 |
explicit IterableIntMap(const Graph& graph, int value)
|
|
2715 |
: Parent(graph, _maps_bits::IterableIntMapNode<ITEM>(value)) {
|
|
2716 |
if (value >= 0) {
|
|
2717 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
|
2718 |
lace(it);
|
|
2719 |
}
|
|
2720 |
}
|
|
2721 |
}
|
|
2722 |
|
|
2723 |
private:
|
|
2724 |
|
|
2725 |
void unlace(const Key& key) {
|
|
2726 |
typename Parent::Value& node = Parent::operator[](key);
|
|
2727 |
if (node.value < 0) return;
|
|
2728 |
if (node.prev != INVALID) {
|
|
2729 |
Parent::operator[](node.prev).next = node.next;
|
|
2730 |
} else {
|
|
2731 |
_first[node.value] = node.next;
|
|
2732 |
}
|
|
2733 |
if (node.next != INVALID) {
|
|
2734 |
Parent::operator[](node.next).prev = node.prev;
|
|
2735 |
}
|
|
2736 |
while (!_first.empty() && _first.back() == INVALID) {
|
|
2737 |
_first.pop_back();
|
|
2738 |
}
|
|
2739 |
}
|
|
2740 |
|
|
2741 |
void lace(const Key& key) {
|
|
2742 |
typename Parent::Value& node = Parent::operator[](key);
|
|
2743 |
if (node.value < 0) return;
|
|
2744 |
if (node.value >= int(_first.size())) {
|
|
2745 |
_first.resize(node.value + 1, INVALID);
|
|
2746 |
}
|
|
2747 |
node.prev = INVALID;
|
|
2748 |
node.next = _first[node.value];
|
|
2749 |
if (node.next != INVALID) {
|
|
2750 |
Parent::operator[](node.next).prev = key;
|
|
2751 |
}
|
|
2752 |
_first[node.value] = key;
|
|
2753 |
}
|
|
2754 |
|
|
2755 |
public:
|
|
2756 |
|
|
2757 |
/// Indicates that the map if reference map.
|
|
2758 |
typedef True ReferenceMapTag;
|
|
2759 |
|
|
2760 |
/// \brief Refernce to the value of the map.
|
|
2761 |
///
|
|
2762 |
/// This class is similar to the int type. It can
|
|
2763 |
/// be converted to int and it has the same operators.
|
|
2764 |
class Reference {
|
|
2765 |
friend class IterableIntMap;
|
|
2766 |
private:
|
|
2767 |
Reference(IterableIntMap& map, const Key& key)
|
|
2768 |
: _key(key), _map(map) {}
|
|
2769 |
public:
|
|
2770 |
|
|
2771 |
Reference& operator=(const Reference& value) {
|
|
2772 |
_map.set(_key, static_cast<const int&>(value));
|
|
2773 |
return *this;
|
|
2774 |
}
|
|
2775 |
|
|
2776 |
operator const int&() const {
|
|
2777 |
return static_cast<const IterableIntMap&>(_map)[_key];
|
|
2778 |
}
|
|
2779 |
|
|
2780 |
Reference& operator=(int value) {
|
|
2781 |
_map.set(_key, value);
|
|
2782 |
return *this;
|
|
2783 |
}
|
|
2784 |
Reference& operator++() {
|
|
2785 |
_map.set(_key, _map[_key] + 1);
|
|
2786 |
return *this;
|
|
2787 |
}
|
|
2788 |
int operator++(int) {
|
|
2789 |
int value = _map[_key];
|
|
2790 |
_map.set(_key, value + 1);
|
|
2791 |
return value;
|
|
2792 |
}
|
|
2793 |
Reference& operator--() {
|
|
2794 |
_map.set(_key, _map[_key] - 1);
|
|
2795 |
return *this;
|
|
2796 |
}
|
|
2797 |
int operator--(int) {
|
|
2798 |
int value = _map[_key];
|
|
2799 |
_map.set(_key, value - 1);
|
|
2800 |
return value;
|
|
2801 |
}
|
|
2802 |
Reference& operator+=(int value) {
|
|
2803 |
_map.set(_key, _map[_key] + value);
|
|
2804 |
return *this;
|
|
2805 |
}
|
|
2806 |
Reference& operator-=(int value) {
|
|
2807 |
_map.set(_key, _map[_key] - value);
|
|
2808 |
return *this;
|
|
2809 |
}
|
|
2810 |
Reference& operator*=(int value) {
|
|
2811 |
_map.set(_key, _map[_key] * value);
|
|
2812 |
return *this;
|
|
2813 |
}
|
|
2814 |
Reference& operator/=(int value) {
|
|
2815 |
_map.set(_key, _map[_key] / value);
|
|
2816 |
return *this;
|
|
2817 |
}
|
|
2818 |
Reference& operator%=(int value) {
|
|
2819 |
_map.set(_key, _map[_key] % value);
|
|
2820 |
return *this;
|
|
2821 |
}
|
|
2822 |
Reference& operator&=(int value) {
|
|
2823 |
_map.set(_key, _map[_key] & value);
|
|
2824 |
return *this;
|
|
2825 |
}
|
|
2826 |
Reference& operator|=(int value) {
|
|
2827 |
_map.set(_key, _map[_key] | value);
|
|
2828 |
return *this;
|
|
2829 |
}
|
|
2830 |
Reference& operator^=(int value) {
|
|
2831 |
_map.set(_key, _map[_key] ^ value);
|
|
2832 |
return *this;
|
|
2833 |
}
|
|
2834 |
Reference& operator<<=(int value) {
|
|
2835 |
_map.set(_key, _map[_key] << value);
|
|
2836 |
return *this;
|
|
2837 |
}
|
|
2838 |
Reference& operator>>=(int value) {
|
|
2839 |
_map.set(_key, _map[_key] >> value);
|
|
2840 |
return *this;
|
|
2841 |
}
|
|
2842 |
|
|
2843 |
private:
|
|
2844 |
Key _key;
|
|
2845 |
IterableIntMap& _map;
|
|
2846 |
};
|
|
2847 |
|
|
2848 |
/// The const reference type.
|
|
2849 |
typedef const Value& ConstReference;
|
|
2850 |
|
|
2851 |
/// \brief Gives back the maximal value plus one.
|
|
2852 |
///
|
|
2853 |
/// Gives back the maximal value plus one.
|
|
2854 |
int size() const {
|
|
2855 |
return _first.size();
|
|
2856 |
}
|
|
2857 |
|
|
2858 |
/// \brief Set operation of the map.
|
|
2859 |
///
|
|
2860 |
/// Set operation of the map.
|
|
2861 |
void set(const Key& key, const Value& value) {
|
|
2862 |
unlace(key);
|
|
2863 |
Parent::operator[](key).value = value;
|
|
2864 |
lace(key);
|
|
2865 |
}
|
|
2866 |
|
|
2867 |
/// \brief Const subscript operator of the map.
|
|
2868 |
///
|
|
2869 |
/// Const subscript operator of the map.
|
|
2870 |
const Value& operator[](const Key& key) const {
|
|
2871 |
return Parent::operator[](key).value;
|
|
2872 |
}
|
|
2873 |
|
|
2874 |
/// \brief Subscript operator of the map.
|
|
2875 |
///
|
|
2876 |
/// Subscript operator of the map.
|
|
2877 |
Reference operator[](const Key& key) {
|
|
2878 |
return Reference(*this, key);
|
|
2879 |
}
|
|
2880 |
|
|
2881 |
/// \brief Iterator for the keys with the same value.
|
|
2882 |
///
|
|
2883 |
/// Iterator for the keys with the same value. It works
|
|
2884 |
/// like a graph item iterator in the map, it can be converted
|
|
2885 |
/// the item type of the map, incremented with \c ++ operator, and
|
|
2886 |
/// if the iterator leave the last valid item it will be equal to
|
|
2887 |
/// \c INVALID.
|
|
2888 |
class ItemIt : public ITEM {
|
|
2889 |
public:
|
|
2890 |
typedef ITEM Parent;
|
|
2891 |
|
|
2892 |
/// \brief Invalid constructor \& conversion.
|
|
2893 |
///
|
|
2894 |
/// This constructor initializes the item to be invalid.
|
|
2895 |
/// \sa Invalid for more details.
|
|
2896 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
|
2897 |
|
|
2898 |
/// \brief Creates an iterator with a value.
|
|
2899 |
///
|
|
2900 |
/// Creates an iterator with a value. It iterates on the
|
|
2901 |
/// keys which have the given value.
|
|
2902 |
/// \param map The IterableIntMap
|
|
2903 |
/// \param value The value
|
|
2904 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
|
2905 |
if (value < 0 || value >= int(_map->_first.size())) {
|
|
2906 |
Parent::operator=(INVALID);
|
|
2907 |
} else {
|
|
2908 |
Parent::operator=(_map->_first[value]);
|
|
2909 |
}
|
|
2910 |
}
|
|
2911 |
|
|
2912 |
/// \brief Increment operator.
|
|
2913 |
///
|
|
2914 |
/// Increment Operator.
|
|
2915 |
ItemIt& operator++() {
|
|
2916 |
Parent::operator=(_map->IterableIntMap::Parent::
|
|
2917 |
operator[](static_cast<Parent&>(*this)).next);
|
|
2918 |
return *this;
|
|
2919 |
}
|
|
2920 |
|
|
2921 |
|
|
2922 |
private:
|
|
2923 |
const IterableIntMap* _map;
|
|
2924 |
};
|
|
2925 |
|
|
2926 |
protected:
|
|
2927 |
|
|
2928 |
virtual void erase(const Key& key) {
|
|
2929 |
unlace(key);
|
|
2930 |
Parent::erase(key);
|
|
2931 |
}
|
|
2932 |
|
|
2933 |
virtual void erase(const std::vector<Key>& keys) {
|
|
2934 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
2935 |
unlace(keys[i]);
|
|
2936 |
}
|
|
2937 |
Parent::erase(keys);
|
|
2938 |
}
|
|
2939 |
|
|
2940 |
virtual void clear() {
|
|
2941 |
_first.clear();
|
|
2942 |
Parent::clear();
|
|
2943 |
}
|
|
2944 |
|
|
2945 |
private:
|
|
2946 |
std::vector<ITEM> _first;
|
|
2947 |
};
|
|
2948 |
|
|
2949 |
namespace _maps_bits {
|
|
2950 |
template <typename Item, typename Value>
|
|
2951 |
struct IterableValueMapNode {
|
|
2952 |
IterableValueMapNode(Value _value = Value()) : value(_value) {}
|
|
2953 |
Item prev, next;
|
|
2954 |
Value value;
|
|
2955 |
};
|
|
2956 |
}
|
|
2957 |
|
|
2958 |
///\ingroup graph_maps
|
|
2959 |
///
|
|
2960 |
/// \brief Dynamic iterable map for comparable values.
|
|
2961 |
///
|
|
2962 |
/// This class provides a special graph map type which can store
|
|
2963 |
/// for each graph item(node, edge, etc.) a value. For each
|
|
2964 |
/// value it is possible to iterate on the keys which mapped to the
|
|
2965 |
/// given value. The type stores for each value a linked list with
|
|
2966 |
/// the items which mapped to the value, and the values are stored
|
|
2967 |
/// in balanced binary tree. The values of the map can be accessed
|
|
2968 |
/// with stl compatible forward iterator.
|
|
2969 |
///
|
|
2970 |
/// This type is not reference map so it cannot be modified with
|
|
2971 |
/// the subscription operator.
|
|
2972 |
///
|
|
2973 |
/// \see InvertableMap
|
|
2974 |
///
|
|
2975 |
/// \param GR The graph type.
|
|
2976 |
/// \param ITEM One of the graph's item type, the key of the map.
|
|
2977 |
/// \param VAL Any comparable value type.
|
|
2978 |
template <typename GR, typename ITEM, typename VAL>
|
|
2979 |
class IterableValueMap
|
|
2980 |
: protected ItemSetTraits<GR, ITEM>::
|
|
2981 |
template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type {
|
|
2982 |
public:
|
|
2983 |
typedef typename ItemSetTraits<GR, ITEM>::
|
|
2984 |
template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type Parent;
|
|
2985 |
|
|
2986 |
/// The key type
|
|
2987 |
typedef ITEM Key;
|
|
2988 |
/// The value type
|
|
2989 |
typedef VAL Value;
|
|
2990 |
/// The graph type
|
|
2991 |
typedef GR Graph;
|
|
2992 |
|
|
2993 |
public:
|
|
2994 |
|
|
2995 |
/// \brief Constructor of the Map with a given value.
|
|
2996 |
///
|
|
2997 |
/// Constructor of the Map with a given value.
|
|
2998 |
explicit IterableValueMap(const Graph& graph,
|
|
2999 |
const Value& value = Value())
|
|
3000 |
: Parent(graph, _maps_bits::IterableValueMapNode<ITEM, VAL>(value)) {
|
|
3001 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
|
3002 |
lace(it);
|
|
3003 |
}
|
|
3004 |
}
|
|
3005 |
|
|
3006 |
protected:
|
|
3007 |
|
|
3008 |
void unlace(const Key& key) {
|
|
3009 |
typename Parent::Value& node = Parent::operator[](key);
|
|
3010 |
if (node.prev != INVALID) {
|
|
3011 |
Parent::operator[](node.prev).next = node.next;
|
|
3012 |
} else {
|
|
3013 |
if (node.next != INVALID) {
|
|
3014 |
_first[node.value] = node.next;
|
|
3015 |
} else {
|
|
3016 |
_first.erase(node.value);
|
|
3017 |
}
|
|
3018 |
}
|
|
3019 |
if (node.next != INVALID) {
|
|
3020 |
Parent::operator[](node.next).prev = node.prev;
|
|
3021 |
}
|
|
3022 |
}
|
|
3023 |
|
|
3024 |
void lace(const Key& key) {
|
|
3025 |
typename Parent::Value& node = Parent::operator[](key);
|
|
3026 |
typename std::map<Value, Key>::iterator it = _first.find(node.value);
|
|
3027 |
if (it == _first.end()) {
|
|
3028 |
node.prev = node.next = INVALID;
|
|
3029 |
if (node.next != INVALID) {
|
|
3030 |
Parent::operator[](node.next).prev = key;
|
|
3031 |
}
|
|
3032 |
_first.insert(std::make_pair(node.value, key));
|
|
3033 |
} else {
|
|
3034 |
node.prev = INVALID;
|
|
3035 |
node.next = it->second;
|
|
3036 |
if (node.next != INVALID) {
|
|
3037 |
Parent::operator[](node.next).prev = key;
|
|
3038 |
}
|
|
3039 |
it->second = key;
|
|
3040 |
}
|
|
3041 |
}
|
|
3042 |
|
|
3043 |
public:
|
|
3044 |
|
|
3045 |
/// \brief Forward iterator for values.
|
|
3046 |
///
|
|
3047 |
/// This iterator is an stl compatible forward
|
|
3048 |
/// iterator on the values of the map. The values can
|
|
3049 |
/// be accessed in the [beginValue, endValue) range.
|
|
3050 |
///
|
|
3051 |
class ValueIterator
|
|
3052 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
|
3053 |
friend class IterableValueMap;
|
|
3054 |
private:
|
|
3055 |
ValueIterator(typename std::map<Value, Key>::const_iterator _it)
|
|
3056 |
: it(_it) {}
|
|
3057 |
public:
|
|
3058 |
|
|
3059 |
ValueIterator() {}
|
|
3060 |
|
|
3061 |
ValueIterator& operator++() { ++it; return *this; }
|
|
3062 |
ValueIterator operator++(int) {
|
|
3063 |
ValueIterator tmp(*this);
|
|
3064 |
operator++();
|
|
3065 |
return tmp;
|
|
3066 |
}
|
|
3067 |
|
|
3068 |
const Value& operator*() const { return it->first; }
|
|
3069 |
const Value* operator->() const { return &(it->first); }
|
|
3070 |
|
|
3071 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
|
3072 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
|
3073 |
|
|
3074 |
private:
|
|
3075 |
typename std::map<Value, Key>::const_iterator it;
|
|
3076 |
};
|
|
3077 |
|
|
3078 |
/// \brief Returns an iterator to the first value.
|
|
3079 |
///
|
|
3080 |
/// Returns an stl compatible iterator to the
|
|
3081 |
/// first value of the map. The values of the
|
|
3082 |
/// map can be accessed in the [beginValue, endValue)
|
|
3083 |
/// range.
|
|
3084 |
ValueIterator beginValue() const {
|
|
3085 |
return ValueIterator(_first.begin());
|
|
3086 |
}
|
|
3087 |
|
|
3088 |
/// \brief Returns an iterator after the last value.
|
|
3089 |
///
|
|
3090 |
/// Returns an stl compatible iterator after the
|
|
3091 |
/// last value of the map. The values of the
|
|
3092 |
/// map can be accessed in the [beginValue, endValue)
|
|
3093 |
/// range.
|
|
3094 |
ValueIterator endValue() const {
|
|
3095 |
return ValueIterator(_first.end());
|
|
3096 |
}
|
|
3097 |
|
|
3098 |
/// \brief Set operation of the map.
|
|
3099 |
///
|
|
3100 |
/// Set operation of the map.
|
|
3101 |
void set(const Key& key, const Value& value) {
|
|
3102 |
unlace(key);
|
|
3103 |
Parent::operator[](key).value = value;
|
|
3104 |
lace(key);
|
|
3105 |
}
|
|
3106 |
|
|
3107 |
/// \brief Const subscript operator of the map.
|
|
3108 |
///
|
|
3109 |
/// Const subscript operator of the map.
|
|
3110 |
const Value& operator[](const Key& key) const {
|
|
3111 |
return Parent::operator[](key).value;
|
|
3112 |
}
|
|
3113 |
|
|
3114 |
/// \brief Iterator for the keys with the same value.
|
|
3115 |
///
|
|
3116 |
/// Iterator for the keys with the same value. It works
|
|
3117 |
/// like a graph item iterator in the map, it can be converted
|
|
3118 |
/// the item type of the map, incremented with \c ++ operator, and
|
|
3119 |
/// if the iterator leave the last valid item it will be equal to
|
|
3120 |
/// \c INVALID.
|
|
3121 |
class ItemIt : public ITEM {
|
|
3122 |
public:
|
|
3123 |
typedef ITEM Parent;
|
|
3124 |
|
|
3125 |
/// \brief Invalid constructor \& conversion.
|
|
3126 |
///
|
|
3127 |
/// This constructor initializes the item to be invalid.
|
|
3128 |
/// \sa Invalid for more details.
|
|
3129 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
|
3130 |
|
|
3131 |
/// \brief Creates an iterator with a value.
|
|
3132 |
///
|
|
3133 |
/// Creates an iterator with a value. It iterates on the
|
|
3134 |
/// keys which have the given value.
|
|
3135 |
/// \param map The IterableValueMap
|
|
3136 |
/// \param value The value
|
|
3137 |
ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
|
|
3138 |
typename std::map<Value, Key>::const_iterator it =
|
|
3139 |
map._first.find(value);
|
|
3140 |
if (it == map._first.end()) {
|
|
3141 |
Parent::operator=(INVALID);
|
|
3142 |
} else {
|
|
3143 |
Parent::operator=(it->second);
|
|
3144 |
}
|
|
3145 |
}
|
|
3146 |
|
|
3147 |
/// \brief Increment operator.
|
|
3148 |
///
|
|
3149 |
/// Increment Operator.
|
|
3150 |
ItemIt& operator++() {
|
|
3151 |
Parent::operator=(_map->IterableValueMap::Parent::
|
|
3152 |
operator[](static_cast<Parent&>(*this)).next);
|
|
3153 |
return *this;
|
|
3154 |
}
|
|
3155 |
|
|
3156 |
|
|
3157 |
private:
|
|
3158 |
const IterableValueMap* _map;
|
|
3159 |
};
|
|
3160 |
|
|
3161 |
protected:
|
|
3162 |
|
|
3163 |
virtual void add(const Key& key) {
|
|
3164 |
Parent::add(key);
|
|
3165 |
unlace(key);
|
|
3166 |
}
|
|
3167 |
|
|
3168 |
virtual void add(const std::vector<Key>& keys) {
|
|
3169 |
Parent::add(keys);
|
|
3170 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
3171 |
lace(keys[i]);
|
|
3172 |
}
|
|
3173 |
}
|
|
3174 |
|
|
3175 |
virtual void erase(const Key& key) {
|
|
3176 |
unlace(key);
|
|
3177 |
Parent::erase(key);
|
|
3178 |
}
|
|
3179 |
|
|
3180 |
virtual void erase(const std::vector<Key>& keys) {
|
|
3181 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
3182 |
unlace(keys[i]);
|
|
3183 |
}
|
|
3184 |
Parent::erase(keys);
|
|
3185 |
}
|
|
3186 |
|
|
3187 |
virtual void build() {
|
|
3188 |
Parent::build();
|
|
3189 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
|
3190 |
lace(it);
|
|
3191 |
}
|
|
3192 |
}
|
|
3193 |
|
|
3194 |
virtual void clear() {
|
|
3195 |
_first.clear();
|
|
3196 |
Parent::clear();
|
|
3197 |
}
|
|
3198 |
|
|
3199 |
private:
|
|
3200 |
std::map<Value, Key> _first;
|
|
3201 |
};
|
|
3202 |
|
| 2314 |
3203 |
/// \brief Map of the source nodes of arcs in a digraph.
|
| 2315 |
3204 |
///
|
| 2316 |
3205 |
/// SourceMap provides access for the source node of each arc in a digraph,
|
| 2317 |
3206 |
/// which is returned by the \c source() function of the digraph.
|
| 2318 |
3207 |
/// \tparam GR The digraph type.
|
| 2319 |
3208 |
/// \see TargetMap
|
| 2320 |
3209 |
template <typename GR>
|
| 2321 |
3210 |
class SourceMap {
|
| 2322 |
3211 |
public:
|
| 2323 |
3212 |
|
| 2324 |
3213 |
///\e
|
| 2325 |
3214 |
typedef typename GR::Arc Key;
|
| 2326 |
3215 |
///\e
|
| 2327 |
3216 |
typedef typename GR::Node Value;
|
| 2328 |
3217 |
|
| 2329 |
3218 |
/// \brief Constructor
|
| 2330 |
3219 |
///
|
| 2331 |
3220 |
/// Constructor.
|
| 2332 |
3221 |
/// \param digraph The digraph that the map belongs to.
|
| 2333 |
3222 |
explicit SourceMap(const GR& digraph) : _graph(digraph) {}
|
| 2334 |
3223 |
|
| 2335 |
3224 |
/// \brief Returns the source node of the given arc.
|
| 2336 |
3225 |
///
|
| 2337 |
3226 |
/// Returns the source node of the given arc.
|
| 2338 |
3227 |
Value operator[](const Key& arc) const {
|
| 2339 |
3228 |
return _graph.source(arc);
|
| 2340 |
3229 |
}
|
| 2341 |
3230 |
|
| 2342 |
3231 |
private:
|
| 2343 |
3232 |
const GR& _graph;
|
| 2344 |
3233 |
};
|
| 2345 |
3234 |
|
| 2346 |
3235 |
/// \brief Returns a \c SourceMap class.
|
| 2347 |
3236 |
///
|
| 2348 |
3237 |
/// This function just returns an \c SourceMap class.
|
| 2349 |
3238 |
/// \relates SourceMap
|
| 2350 |
3239 |
template <typename GR>
|
| 2351 |
3240 |
inline SourceMap<GR> sourceMap(const GR& graph) {
|
| 2352 |
3241 |
return SourceMap<GR>(graph);
|
| 2353 |
3242 |
}
|
| 2354 |
3243 |
|
| 2355 |
3244 |
/// \brief Map of the target nodes of arcs in a digraph.
|
| 2356 |
3245 |
///
|
| 2357 |
3246 |
/// TargetMap provides access for the target node of each arc in a digraph,
|
| 2358 |
3247 |
/// which is returned by the \c target() function of the digraph.
|
| 2359 |
3248 |
/// \tparam GR The digraph type.
|
| 2360 |
3249 |
/// \see SourceMap
|
| 2361 |
3250 |
template <typename GR>
|
| 2362 |
3251 |
class TargetMap {
|
| 2363 |
3252 |
public:
|
| 2364 |
3253 |
|
| 2365 |
3254 |
///\e
|
| 2366 |
3255 |
typedef typename GR::Arc Key;
|
| 2367 |
3256 |
///\e
|
| 2368 |
3257 |
typedef typename GR::Node Value;
|
| 2369 |
3258 |
|
| 2370 |
3259 |
/// \brief Constructor
|
| 2371 |
3260 |
///
|
| 2372 |
3261 |
/// Constructor.
|
| 2373 |
3262 |
/// \param digraph The digraph that the map belongs to.
|
| 2374 |
3263 |
explicit TargetMap(const GR& digraph) : _graph(digraph) {}
|
| 2375 |
3264 |
|
| 2376 |
3265 |
/// \brief Returns the target node of the given arc.
|
| 2377 |
3266 |
///
|
| 2378 |
3267 |
/// Returns the target node of the given arc.
|
| 2379 |
3268 |
Value operator[](const Key& e) const {
|
| 2380 |
3269 |
return _graph.target(e);
|
| 2381 |
3270 |
}
|
| 2382 |
3271 |
|
| 2383 |
3272 |
private:
|
| 2384 |
3273 |
const GR& _graph;
|
| 2385 |
3274 |
};
|
| 2386 |
3275 |
|
| 2387 |
3276 |
/// \brief Returns a \c TargetMap class.
|
| 2388 |
3277 |
///
|
| 2389 |
3278 |
/// This function just returns a \c TargetMap class.
|
| 2390 |
3279 |
/// \relates TargetMap
|
| 2391 |
3280 |
template <typename GR>
|
| 2392 |
3281 |
inline TargetMap<GR> targetMap(const GR& graph) {
|
| 2393 |
3282 |
return TargetMap<GR>(graph);
|
| 2394 |
3283 |
}
|
| 2395 |
3284 |
|
| 2396 |
3285 |
/// \brief Map of the "forward" directed arc view of edges in a graph.
|
| 2397 |
3286 |
///
|
| 2398 |
3287 |
/// ForwardMap provides access for the "forward" directed arc view of
|
| 2399 |
3288 |
/// each edge in a graph, which is returned by the \c direct() function
|
| 2400 |
3289 |
/// of the graph with \c true parameter.
|
| 2401 |
3290 |
/// \tparam GR The graph type.
|
| 2402 |
3291 |
/// \see BackwardMap
|
| 2403 |
3292 |
template <typename GR>
|
| 2404 |
3293 |
class ForwardMap {
|
| 2405 |
3294 |
public:
|
| 2406 |
3295 |
|
| 2407 |
3296 |
typedef typename GR::Arc Value;
|
| 2408 |
3297 |
typedef typename GR::Edge Key;
|
| 2409 |
3298 |
|
| 2410 |
3299 |
/// \brief Constructor
|
| 2411 |
3300 |
///
|
| 2412 |
3301 |
/// Constructor.
|
| 2413 |
3302 |
/// \param graph The graph that the map belongs to.
|
| 2414 |
3303 |
explicit ForwardMap(const GR& graph) : _graph(graph) {}
|
| 2415 |
3304 |
|
| 2416 |
3305 |
/// \brief Returns the "forward" directed arc view of the given edge.
|
| 2417 |
3306 |
///
|
| 2418 |
3307 |
/// Returns the "forward" directed arc view of the given edge.
|
| 2419 |
3308 |
Value operator[](const Key& key) const {
|
| 2420 |
3309 |
return _graph.direct(key, true);
|
| 2421 |
3310 |
}
|
| 2422 |
3311 |
|
| 2423 |
3312 |
private:
|
| 2424 |
3313 |
const GR& _graph;
|
| 2425 |
3314 |
};
|
| 2426 |
3315 |
|
| 2427 |
3316 |
/// \brief Returns a \c ForwardMap class.
|
| 2428 |
3317 |
///
|
| 2429 |
3318 |
/// This function just returns an \c ForwardMap class.
|
| 2430 |
3319 |
/// \relates ForwardMap
|
| 2431 |
3320 |
template <typename GR>
|
| 2432 |
3321 |
inline ForwardMap<GR> forwardMap(const GR& graph) {
|
| 2433 |
3322 |
return ForwardMap<GR>(graph);
|
| 2434 |
3323 |
}
|
| 2435 |
3324 |
|
| 2436 |
3325 |
/// \brief Map of the "backward" directed arc view of edges in a graph.
|
| 2437 |
3326 |
///
|
| 2438 |
3327 |
/// BackwardMap provides access for the "backward" directed arc view of
|
| 2439 |
3328 |
/// each edge in a graph, which is returned by the \c direct() function
|
| 2440 |
3329 |
/// of the graph with \c false parameter.
|
| 2441 |
3330 |
/// \tparam GR The graph type.
|
| 2442 |
3331 |
/// \see ForwardMap
|
| 2443 |
3332 |
template <typename GR>
|
| 2444 |
3333 |
class BackwardMap {
|
| 2445 |
3334 |
public:
|
| 2446 |
3335 |
|
| 2447 |
3336 |
typedef typename GR::Arc Value;
|
| 2448 |
3337 |
typedef typename GR::Edge Key;
|
| 2449 |
3338 |
|
| 2450 |
3339 |
/// \brief Constructor
|
| 2451 |
3340 |
///
|
| 2452 |
3341 |
/// Constructor.
|
| 2453 |
3342 |
/// \param graph The graph that the map belongs to.
|
| 2454 |
3343 |
explicit BackwardMap(const GR& graph) : _graph(graph) {}
|
| 2455 |
3344 |
|
| 2456 |
3345 |
/// \brief Returns the "backward" directed arc view of the given edge.
|
| 2457 |
3346 |
///
|
| 2458 |
3347 |
/// Returns the "backward" directed arc view of the given edge.
|
| 2459 |
3348 |
Value operator[](const Key& key) const {
|
| 2460 |
3349 |
return _graph.direct(key, false);
|
| 2461 |
3350 |
}
|
| 2462 |
3351 |
|
| 2463 |
3352 |
private:
|
| 2464 |
3353 |
const GR& _graph;
|
| 2465 |
3354 |
};
|
| 2466 |
3355 |
|
| 2467 |
3356 |
/// \brief Returns a \c BackwardMap class
|
| 2468 |
3357 |
|
| 2469 |
3358 |
/// This function just returns a \c BackwardMap class.
|
| 2470 |
3359 |
/// \relates BackwardMap
|
| 2471 |
3360 |
template <typename GR>
|
| 2472 |
3361 |
inline BackwardMap<GR> backwardMap(const GR& graph) {
|
| 2473 |
3362 |
return BackwardMap<GR>(graph);
|
| 2474 |
3363 |
}
|
| 2475 |
3364 |
|
| 2476 |
3365 |
/// \brief Map of the in-degrees of nodes in a digraph.
|
| 2477 |
3366 |
///
|
| 2478 |
3367 |
/// This map returns the in-degree of a node. Once it is constructed,
|
| 2479 |
3368 |
/// the degrees are stored in a standard \c NodeMap, so each query is done
|
| 2480 |
3369 |
/// in constant time. On the other hand, the values are updated automatically
|
| 2481 |
3370 |
/// whenever the digraph changes.
|
| 2482 |
3371 |
///
|
| 2483 |
|
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
|
3372 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
| 2484 |
3373 |
/// may provide alternative ways to modify the digraph.
|
| 2485 |
3374 |
/// The correct behavior of InDegMap is not guarantied if these additional
|
| 2486 |
3375 |
/// features are used. For example the functions
|
| 2487 |
3376 |
/// \ref ListDigraph::changeSource() "changeSource()",
|
| 2488 |
3377 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
| 2489 |
3378 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
| 2490 |
3379 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
| 2491 |
3380 |
///
|
| 2492 |
3381 |
/// \sa OutDegMap
|
| 2493 |
3382 |
template <typename GR>
|
| 2494 |
3383 |
class InDegMap
|
| 2495 |
3384 |
: protected ItemSetTraits<GR, typename GR::Arc>
|
| 2496 |
3385 |
::ItemNotifier::ObserverBase {
|
| 2497 |
3386 |
|
| 2498 |
3387 |
public:
|
| 2499 |
|
|
|
3388 |
|
| 2500 |
3389 |
/// The graph type of InDegMap
|
| 2501 |
3390 |
typedef GR Graph;
|
| 2502 |
3391 |
typedef GR Digraph;
|
| 2503 |
3392 |
/// The key type
|
| 2504 |
3393 |
typedef typename Digraph::Node Key;
|
| 2505 |
3394 |
/// The value type
|
| 2506 |
3395 |
typedef int Value;
|
| 2507 |
3396 |
|
| 2508 |
3397 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
| 2509 |
3398 |
::ItemNotifier::ObserverBase Parent;
|
| 2510 |
3399 |
|
| 2511 |
3400 |
private:
|
| 2512 |
3401 |
|
| 2513 |
3402 |
class AutoNodeMap
|
| 2514 |
3403 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
| 2515 |
3404 |
public:
|
| 2516 |
3405 |
|
| 2517 |
3406 |
typedef typename ItemSetTraits<Digraph, Key>::
|
| 2518 |
3407 |
template Map<int>::Type Parent;
|
| 2519 |
3408 |
|
| 2520 |
3409 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 2521 |
3410 |
|
| 2522 |
3411 |
virtual void add(const Key& key) {
|
| 2523 |
3412 |
Parent::add(key);
|
| 2524 |
3413 |
Parent::set(key, 0);
|
| 2525 |
3414 |
}
|
| 2526 |
3415 |
|
| 2527 |
3416 |
virtual void add(const std::vector<Key>& keys) {
|
| 2528 |
3417 |
Parent::add(keys);
|
| 2529 |
3418 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2530 |
3419 |
Parent::set(keys[i], 0);
|
| 2531 |
3420 |
}
|
| 2532 |
3421 |
}
|
| 2533 |
3422 |
|
| 2534 |
3423 |
virtual void build() {
|
| 2535 |
3424 |
Parent::build();
|
| 2536 |
3425 |
Key it;
|
| 2537 |
3426 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 2538 |
3427 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2539 |
3428 |
Parent::set(it, 0);
|
| 2540 |
3429 |
}
|
| 2541 |
3430 |
}
|
| 2542 |
3431 |
};
|
| 2543 |
3432 |
|
| 2544 |
3433 |
public:
|
| 2545 |
3434 |
|
| 2546 |
3435 |
/// \brief Constructor.
|
| 2547 |
3436 |
///
|
| 2548 |
3437 |
/// Constructor for creating an in-degree map.
|
| 2549 |
3438 |
explicit InDegMap(const Digraph& graph)
|
| 2550 |
3439 |
: _digraph(graph), _deg(graph) {
|
| 2551 |
3440 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
| 2552 |
3441 |
|
| 2553 |
3442 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2554 |
3443 |
_deg[it] = countInArcs(_digraph, it);
|
| 2555 |
3444 |
}
|
| 2556 |
3445 |
}
|
| 2557 |
3446 |
|
| 2558 |
3447 |
/// \brief Gives back the in-degree of a Node.
|
| 2559 |
3448 |
///
|
| 2560 |
3449 |
/// Gives back the in-degree of a Node.
|
| 2561 |
3450 |
int operator[](const Key& key) const {
|
| 2562 |
3451 |
return _deg[key];
|
| 2563 |
3452 |
}
|
| 2564 |
3453 |
|
| 2565 |
3454 |
protected:
|
| 2566 |
3455 |
|
| 2567 |
3456 |
typedef typename Digraph::Arc Arc;
|
| 2568 |
3457 |
|
| 2569 |
3458 |
virtual void add(const Arc& arc) {
|
| 2570 |
3459 |
++_deg[_digraph.target(arc)];
|
| 2571 |
3460 |
}
|
| 2572 |
3461 |
|
| 2573 |
3462 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 2574 |
3463 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2575 |
3464 |
++_deg[_digraph.target(arcs[i])];
|
| 2576 |
3465 |
}
|
| 2577 |
3466 |
}
|
| 2578 |
3467 |
|
| 2579 |
3468 |
virtual void erase(const Arc& arc) {
|
| 2580 |
3469 |
--_deg[_digraph.target(arc)];
|
| 2581 |
3470 |
}
|
| 2582 |
3471 |
|
| 2583 |
3472 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 2584 |
3473 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2585 |
3474 |
--_deg[_digraph.target(arcs[i])];
|
| 2586 |
3475 |
}
|
| 2587 |
3476 |
}
|
| 2588 |
3477 |
|
| 2589 |
3478 |
virtual void build() {
|
| 2590 |
3479 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2591 |
3480 |
_deg[it] = countInArcs(_digraph, it);
|
| 2592 |
3481 |
}
|
| 2593 |
3482 |
}
|
| 2594 |
3483 |
|
| 2595 |
3484 |
virtual void clear() {
|
| 2596 |
3485 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2597 |
3486 |
_deg[it] = 0;
|
| 2598 |
3487 |
}
|
| 2599 |
3488 |
}
|
| 2600 |
3489 |
private:
|
| 2601 |
3490 |
|
| 2602 |
3491 |
const Digraph& _digraph;
|
| 2603 |
3492 |
AutoNodeMap _deg;
|
| 2604 |
3493 |
};
|
| 2605 |
3494 |
|
| 2606 |
3495 |
/// \brief Map of the out-degrees of nodes in a digraph.
|
| 2607 |
3496 |
///
|
| 2608 |
3497 |
/// This map returns the out-degree of a node. Once it is constructed,
|
| 2609 |
3498 |
/// the degrees are stored in a standard \c NodeMap, so each query is done
|
| 2610 |
3499 |
/// in constant time. On the other hand, the values are updated automatically
|
| 2611 |
3500 |
/// whenever the digraph changes.
|
| 2612 |
3501 |
///
|
| 2613 |
|
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
|
3502 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
| 2614 |
3503 |
/// may provide alternative ways to modify the digraph.
|
| 2615 |
3504 |
/// The correct behavior of OutDegMap is not guarantied if these additional
|
| 2616 |
3505 |
/// features are used. For example the functions
|
| 2617 |
3506 |
/// \ref ListDigraph::changeSource() "changeSource()",
|
| 2618 |
3507 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
| 2619 |
3508 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
| 2620 |
3509 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
| 2621 |
3510 |
///
|
| 2622 |
3511 |
/// \sa InDegMap
|
| 2623 |
3512 |
template <typename GR>
|
| 2624 |
3513 |
class OutDegMap
|
| 2625 |
3514 |
: protected ItemSetTraits<GR, typename GR::Arc>
|
| 2626 |
3515 |
::ItemNotifier::ObserverBase {
|
| 2627 |
3516 |
|
| 2628 |
3517 |
public:
|
| 2629 |
3518 |
|
| 2630 |
3519 |
/// The graph type of OutDegMap
|
| 2631 |
3520 |
typedef GR Graph;
|
| 2632 |
3521 |
typedef GR Digraph;
|
| 2633 |
3522 |
/// The key type
|
| 2634 |
3523 |
typedef typename Digraph::Node Key;
|
| 2635 |
3524 |
/// The value type
|
| 2636 |
3525 |
typedef int Value;
|
| 2637 |
3526 |
|
| 2638 |
3527 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
| 2639 |
3528 |
::ItemNotifier::ObserverBase Parent;
|
| 2640 |
3529 |
|
| 2641 |
3530 |
private:
|
| 2642 |
3531 |
|
| 2643 |
3532 |
class AutoNodeMap
|
| 2644 |
3533 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
| 2645 |
3534 |
public:
|
| 2646 |
3535 |
|
| 2647 |
3536 |
typedef typename ItemSetTraits<Digraph, Key>::
|
| 2648 |
3537 |
template Map<int>::Type Parent;
|
| 2649 |
3538 |
|
| 2650 |
3539 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 2651 |
3540 |
|
| 2652 |
3541 |
virtual void add(const Key& key) {
|
| 2653 |
3542 |
Parent::add(key);
|
| 2654 |
3543 |
Parent::set(key, 0);
|
| 2655 |
3544 |
}
|
| 2656 |
3545 |
virtual void add(const std::vector<Key>& keys) {
|
| 2657 |
3546 |
Parent::add(keys);
|
| 2658 |
3547 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2659 |
3548 |
Parent::set(keys[i], 0);
|
| 2660 |
3549 |
}
|
| 2661 |
3550 |
}
|
| 2662 |
3551 |
virtual void build() {
|
| 2663 |
3552 |
Parent::build();
|
| 2664 |
3553 |
Key it;
|
| 2665 |
3554 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 2666 |
3555 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2667 |
3556 |
Parent::set(it, 0);
|
| 2668 |
3557 |
}
|
| 2669 |
3558 |
}
|
| 2670 |
3559 |
};
|
| 2671 |
3560 |
|
| 2672 |
3561 |
public:
|
| 2673 |
3562 |
|
| 2674 |
3563 |
/// \brief Constructor.
|
| 2675 |
3564 |
///
|
| 2676 |
3565 |
/// Constructor for creating an out-degree map.
|
| 2677 |
3566 |
explicit OutDegMap(const Digraph& graph)
|
| 2678 |
3567 |
: _digraph(graph), _deg(graph) {
|
| 2679 |
3568 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
| 2680 |
3569 |
|
| 2681 |
3570 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2682 |
3571 |
_deg[it] = countOutArcs(_digraph, it);
|
| 2683 |
3572 |
}
|
| 2684 |
3573 |
}
|
| 2685 |
3574 |
|
| 2686 |
3575 |
/// \brief Gives back the out-degree of a Node.
|
| 2687 |
3576 |
///
|
| 2688 |
3577 |
/// Gives back the out-degree of a Node.
|
| 2689 |
3578 |
int operator[](const Key& key) const {
|
| 2690 |
3579 |
return _deg[key];
|
| 2691 |
3580 |
}
|
| 2692 |
3581 |
|
| 2693 |
3582 |
protected:
|
| 2694 |
3583 |
|
| 2695 |
3584 |
typedef typename Digraph::Arc Arc;
|
| 2696 |
3585 |
|
| 2697 |
3586 |
virtual void add(const Arc& arc) {
|
| 2698 |
3587 |
++_deg[_digraph.source(arc)];
|
| 2699 |
3588 |
}
|
| 2700 |
3589 |
|
| 2701 |
3590 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 2702 |
3591 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2703 |
3592 |
++_deg[_digraph.source(arcs[i])];
|
| 2704 |
3593 |
}
|
| 2705 |
3594 |
}
|
| 2706 |
3595 |
|
| 2707 |
3596 |
virtual void erase(const Arc& arc) {
|
| 2708 |
3597 |
--_deg[_digraph.source(arc)];
|
| 2709 |
3598 |
}
|
| 2710 |
3599 |
|
| 2711 |
3600 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 2712 |
3601 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2713 |
3602 |
--_deg[_digraph.source(arcs[i])];
|
| 2714 |
3603 |
}
|
| 2715 |
3604 |
}
|
| 2716 |
3605 |
|
| 2717 |
3606 |
virtual void build() {
|
| 2718 |
3607 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2719 |
3608 |
_deg[it] = countOutArcs(_digraph, it);
|
| 2720 |
3609 |
}
|
| 2721 |
3610 |
}
|
| 2722 |
3611 |
|
| 2723 |
3612 |
virtual void clear() {
|
| 2724 |
3613 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2725 |
3614 |
_deg[it] = 0;
|
| 2726 |
3615 |
}
|
| 2727 |
3616 |
}
|
| 2728 |
3617 |
private:
|
| 2729 |
3618 |
|
| 2730 |
3619 |
const Digraph& _digraph;
|
| 2731 |
3620 |
AutoNodeMap _deg;
|
| 2732 |
3621 |
};
|
| 2733 |
3622 |
|
| 2734 |
3623 |
/// \brief Potential difference map
|
| 2735 |
3624 |
///
|
| 2736 |
3625 |
/// PotentialDifferenceMap returns the difference between the potentials of
|
| 2737 |
3626 |
/// the source and target nodes of each arc in a digraph, i.e. it returns
|
| 2738 |
3627 |
/// \code
|
| 2739 |
3628 |
/// potential[gr.target(arc)] - potential[gr.source(arc)].
|
| 2740 |
3629 |
/// \endcode
|
| 2741 |
3630 |
/// \tparam GR The digraph type.
|
| 2742 |
3631 |
/// \tparam POT A node map storing the potentials.
|
| 2743 |
3632 |
template <typename GR, typename POT>
|
| 2744 |
3633 |
class PotentialDifferenceMap {
|
| 2745 |
3634 |
public:
|
| 2746 |
3635 |
/// Key type
|
| 2747 |
3636 |
typedef typename GR::Arc Key;
|
| 2748 |
3637 |
/// Value type
|
| 2749 |
3638 |
typedef typename POT::Value Value;
|
| 2750 |
3639 |
|
| 2751 |
3640 |
/// \brief Constructor
|
| 2752 |
3641 |
///
|
| 2753 |
3642 |
/// Contructor of the map.
|
| 2754 |
3643 |
explicit PotentialDifferenceMap(const GR& gr,
|
| 2755 |
3644 |
const POT& potential)
|
| 2756 |
3645 |
: _digraph(gr), _potential(potential) {}
|
| 2757 |
3646 |
|
| 2758 |
3647 |
/// \brief Returns the potential difference for the given arc.
|
| 2759 |
3648 |
///
|
| 2760 |
3649 |
/// Returns the potential difference for the given arc, i.e.
|
| 2761 |
3650 |
/// \code
|
| 2762 |
3651 |
/// potential[gr.target(arc)] - potential[gr.source(arc)].
|
| 2763 |
3652 |
/// \endcode
|
| 2764 |
3653 |
Value operator[](const Key& arc) const {
|
| 2765 |
3654 |
return _potential[_digraph.target(arc)] -
|
| 2766 |
3655 |
_potential[_digraph.source(arc)];
|
| 2767 |
3656 |
}
|
| 2768 |
3657 |
|
| 2769 |
3658 |
private:
|
| 2770 |
3659 |
const GR& _digraph;
|
| 2771 |
3660 |
const POT& _potential;
|
| 2772 |
3661 |
};
|
| 2773 |
3662 |
|
| 2774 |
3663 |
/// \brief Returns a PotentialDifferenceMap.
|
| 2775 |
3664 |
///
|
| 2776 |
3665 |
/// This function just returns a PotentialDifferenceMap.
|
| 2777 |
3666 |
/// \relates PotentialDifferenceMap
|
| 2778 |
3667 |
template <typename GR, typename POT>
|
| 2779 |
3668 |
PotentialDifferenceMap<GR, POT>
|
| 2780 |
3669 |
potentialDifferenceMap(const GR& gr, const POT& potential) {
|
| 2781 |
3670 |
return PotentialDifferenceMap<GR, POT>(gr, potential);
|
| 2782 |
3671 |
}
|
| 2783 |
3672 |
|
| 2784 |
3673 |
/// @}
|
| 2785 |
3674 |
}
|
| 2786 |
3675 |
|
| 2787 |
3676 |
#endif // LEMON_MAPS_H
|