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 |
#include <map>
|
26 |
26 |
|
27 |
27 |
#include <lemon/core.h>
|
28 |
28 |
|
29 |
29 |
///\file
|
30 |
30 |
///\ingroup maps
|
31 |
31 |
///\brief Miscellaneous property maps
|
32 |
32 |
|
33 |
33 |
namespace lemon {
|
34 |
34 |
|
35 |
35 |
/// \addtogroup maps
|
36 |
36 |
/// @{
|
37 |
37 |
|
38 |
38 |
/// Base class of maps.
|
39 |
39 |
|
40 |
40 |
/// Base class of maps. It provides the necessary type definitions
|
41 |
41 |
/// required by the map %concepts.
|
42 |
42 |
template<typename K, typename V>
|
43 |
43 |
class MapBase {
|
44 |
44 |
public:
|
45 |
45 |
/// \brief The key type of the map.
|
46 |
46 |
typedef K Key;
|
47 |
47 |
/// \brief The value type of the map.
|
48 |
48 |
/// (The type of objects associated with the keys).
|
49 |
49 |
typedef V Value;
|
50 |
50 |
};
|
51 |
51 |
|
52 |
52 |
|
53 |
53 |
/// Null map. (a.k.a. DoNothingMap)
|
54 |
54 |
|
55 |
55 |
/// This map can be used if you have to provide a map only for
|
56 |
56 |
/// its type definitions, or if you have to provide a writable map,
|
57 |
57 |
/// but data written to it is not required (i.e. it will be sent to
|
58 |
58 |
/// <tt>/dev/null</tt>).
|
59 |
59 |
/// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
60 |
60 |
///
|
61 |
61 |
/// \sa ConstMap
|
62 |
62 |
template<typename K, typename V>
|
63 |
63 |
class NullMap : public MapBase<K, V> {
|
64 |
64 |
public:
|
65 |
65 |
///\e
|
66 |
66 |
typedef K Key;
|
67 |
67 |
///\e
|
68 |
68 |
typedef V Value;
|
69 |
69 |
|
70 |
70 |
/// Gives back a default constructed element.
|
71 |
71 |
Value operator[](const Key&) const { return Value(); }
|
72 |
72 |
/// Absorbs the value.
|
73 |
73 |
void set(const Key&, const Value&) {}
|
74 |
74 |
};
|
75 |
75 |
|
76 |
76 |
/// Returns a \c NullMap class
|
77 |
77 |
|
78 |
78 |
/// This function just returns a \c NullMap class.
|
79 |
79 |
/// \relates NullMap
|
80 |
80 |
template <typename K, typename V>
|
81 |
81 |
NullMap<K, V> nullMap() {
|
82 |
82 |
return NullMap<K, V>();
|
83 |
83 |
}
|
84 |
84 |
|
85 |
85 |
|
86 |
86 |
/// Constant map.
|
87 |
87 |
|
88 |
88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified
|
89 |
89 |
/// value to each key.
|
90 |
90 |
///
|
91 |
91 |
/// In other aspects it is equivalent to \c NullMap.
|
92 |
92 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
|
93 |
93 |
/// concept, but it absorbs the data written to it.
|
94 |
94 |
///
|
95 |
95 |
/// The simplest way of using this map is through the constMap()
|
96 |
96 |
/// function.
|
97 |
97 |
///
|
98 |
98 |
/// \sa NullMap
|
99 |
99 |
/// \sa IdentityMap
|
100 |
100 |
template<typename K, typename V>
|
101 |
101 |
class ConstMap : public MapBase<K, V> {
|
102 |
102 |
private:
|
103 |
103 |
V _value;
|
104 |
104 |
public:
|
105 |
105 |
///\e
|
106 |
106 |
typedef K Key;
|
107 |
107 |
///\e
|
108 |
108 |
typedef V Value;
|
109 |
109 |
|
110 |
110 |
/// Default constructor
|
111 |
111 |
|
112 |
112 |
/// Default constructor.
|
113 |
113 |
/// The value of the map will be default constructed.
|
114 |
114 |
ConstMap() {}
|
115 |
115 |
|
116 |
116 |
/// Constructor with specified initial value
|
117 |
117 |
|
118 |
118 |
/// Constructor with specified initial value.
|
119 |
119 |
/// \param v The initial value of the map.
|
120 |
120 |
ConstMap(const Value &v) : _value(v) {}
|
121 |
121 |
|
122 |
122 |
/// Gives back the specified value.
|
123 |
123 |
Value operator[](const Key&) const { return _value; }
|
124 |
124 |
|
125 |
125 |
/// Absorbs the value.
|
126 |
126 |
void set(const Key&, const Value&) {}
|
127 |
127 |
|
128 |
128 |
/// Sets the value that is assigned to each key.
|
129 |
129 |
void setAll(const Value &v) {
|
130 |
130 |
_value = v;
|
131 |
131 |
}
|
132 |
132 |
|
133 |
133 |
template<typename V1>
|
134 |
134 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
|
135 |
135 |
};
|
136 |
136 |
|
137 |
137 |
/// Returns a \c ConstMap class
|
138 |
138 |
|
139 |
139 |
/// This function just returns a \c ConstMap class.
|
140 |
140 |
/// \relates ConstMap
|
141 |
141 |
template<typename K, typename V>
|
142 |
142 |
inline ConstMap<K, V> constMap(const V &v) {
|
143 |
143 |
return ConstMap<K, V>(v);
|
144 |
144 |
}
|
145 |
145 |
|
146 |
146 |
template<typename K, typename V>
|
147 |
147 |
inline ConstMap<K, V> constMap() {
|
148 |
148 |
return ConstMap<K, V>();
|
149 |
149 |
}
|
150 |
150 |
|
151 |
151 |
|
152 |
152 |
template<typename T, T v>
|
153 |
153 |
struct Const {};
|
154 |
154 |
|
155 |
155 |
/// Constant map with inlined constant value.
|
156 |
156 |
|
157 |
157 |
/// This \ref concepts::ReadMap "readable map" assigns a specified
|
158 |
158 |
/// value to each key.
|
159 |
159 |
///
|
160 |
160 |
/// In other aspects it is equivalent to \c NullMap.
|
161 |
161 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
|
162 |
162 |
/// concept, but it absorbs the data written to it.
|
163 |
163 |
///
|
164 |
164 |
/// The simplest way of using this map is through the constMap()
|
165 |
165 |
/// function.
|
166 |
166 |
///
|
167 |
167 |
/// \sa NullMap
|
168 |
168 |
/// \sa IdentityMap
|
169 |
169 |
template<typename K, typename V, V v>
|
170 |
170 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
|
171 |
171 |
public:
|
172 |
172 |
///\e
|
173 |
173 |
typedef K Key;
|
174 |
174 |
///\e
|
175 |
175 |
typedef V Value;
|
176 |
176 |
|
177 |
177 |
/// Constructor.
|
178 |
178 |
ConstMap() {}
|
179 |
179 |
|
180 |
180 |
/// Gives back the specified value.
|
181 |
181 |
Value operator[](const Key&) const { return v; }
|
182 |
182 |
|
183 |
183 |
/// Absorbs the value.
|
184 |
184 |
void set(const Key&, const Value&) {}
|
185 |
185 |
};
|
186 |
186 |
|
187 |
187 |
/// Returns a \c ConstMap class with inlined constant value
|
188 |
188 |
|
189 |
189 |
/// This function just returns a \c ConstMap class with inlined
|
190 |
190 |
/// constant value.
|
191 |
191 |
/// \relates ConstMap
|
192 |
192 |
template<typename K, typename V, V v>
|
193 |
193 |
inline ConstMap<K, Const<V, v> > constMap() {
|
194 |
194 |
return ConstMap<K, Const<V, v> >();
|
195 |
195 |
}
|
196 |
196 |
|
197 |
197 |
|
198 |
198 |
/// Identity map.
|
199 |
199 |
|
200 |
200 |
/// This \ref concepts::ReadMap "read-only map" gives back the given
|
201 |
201 |
/// key as value without any modification.
|
202 |
202 |
///
|
203 |
203 |
/// \sa ConstMap
|
204 |
204 |
template <typename T>
|
205 |
205 |
class IdentityMap : public MapBase<T, T> {
|
206 |
206 |
public:
|
207 |
207 |
///\e
|
208 |
208 |
typedef T Key;
|
209 |
209 |
///\e
|
210 |
210 |
typedef T Value;
|
211 |
211 |
|
212 |
212 |
/// Gives back the given value without any modification.
|
213 |
213 |
Value operator[](const Key &k) const {
|
214 |
214 |
return k;
|
215 |
215 |
}
|
216 |
216 |
};
|
217 |
217 |
|
218 |
218 |
/// Returns an \c IdentityMap class
|
219 |
219 |
|
220 |
220 |
/// This function just returns an \c IdentityMap class.
|
221 |
221 |
/// \relates IdentityMap
|
222 |
222 |
template<typename T>
|
223 |
223 |
inline IdentityMap<T> identityMap() {
|
224 |
224 |
return IdentityMap<T>();
|
225 |
225 |
}
|
226 |
226 |
|
227 |
227 |
|
228 |
228 |
/// \brief Map for storing values for integer keys from the range
|
229 |
229 |
/// <tt>[0..size-1]</tt>.
|
230 |
230 |
///
|
231 |
231 |
/// This map is essentially a wrapper for \c std::vector. It assigns
|
232 |
232 |
/// values to integer keys from the range <tt>[0..size-1]</tt>.
|
233 |
233 |
/// It can be used together with some data structures, e.g.
|
234 |
234 |
/// heap types and \c UnionFind, when the used items are small
|
235 |
235 |
/// integers. This map conforms to the \ref concepts::ReferenceMap
|
236 |
236 |
/// "ReferenceMap" concept.
|
237 |
237 |
///
|
238 |
238 |
/// The simplest way of using this map is through the rangeMap()
|
239 |
239 |
/// function.
|
240 |
240 |
template <typename V>
|
241 |
241 |
class RangeMap : public MapBase<int, V> {
|
242 |
242 |
template <typename V1>
|
243 |
243 |
friend class RangeMap;
|
244 |
244 |
private:
|
245 |
245 |
|
246 |
246 |
typedef std::vector<V> Vector;
|
247 |
247 |
Vector _vector;
|
248 |
248 |
|
249 |
249 |
public:
|
250 |
250 |
|
251 |
251 |
/// Key type
|
252 |
252 |
typedef int Key;
|
253 |
253 |
/// Value type
|
254 |
254 |
typedef V Value;
|
255 |
255 |
/// Reference type
|
256 |
256 |
typedef typename Vector::reference Reference;
|
257 |
257 |
/// Const reference type
|
258 |
258 |
typedef typename Vector::const_reference ConstReference;
|
259 |
259 |
|
260 |
260 |
typedef True ReferenceMapTag;
|
261 |
261 |
|
262 |
262 |
public:
|
263 |
263 |
|
264 |
264 |
/// Constructor with specified default value.
|
265 |
265 |
RangeMap(int size = 0, const Value &value = Value())
|
266 |
266 |
: _vector(size, value) {}
|
267 |
267 |
|
268 |
268 |
/// Constructs the map from an appropriate \c std::vector.
|
269 |
269 |
template <typename V1>
|
270 |
270 |
RangeMap(const std::vector<V1>& vector)
|
271 |
271 |
: _vector(vector.begin(), vector.end()) {}
|
272 |
272 |
|
273 |
273 |
/// Constructs the map from another \c RangeMap.
|
274 |
274 |
template <typename V1>
|
275 |
275 |
RangeMap(const RangeMap<V1> &c)
|
276 |
276 |
: _vector(c._vector.begin(), c._vector.end()) {}
|
277 |
277 |
|
278 |
278 |
/// Returns the size of the map.
|
279 |
279 |
int size() {
|
280 |
280 |
return _vector.size();
|
281 |
281 |
}
|
282 |
282 |
|
283 |
283 |
/// Resizes the map.
|
284 |
284 |
|
285 |
285 |
/// Resizes the underlying \c std::vector container, so changes the
|
286 |
286 |
/// keyset of the map.
|
287 |
287 |
/// \param size The new size of the map. The new keyset will be the
|
288 |
288 |
/// range <tt>[0..size-1]</tt>.
|
289 |
289 |
/// \param value The default value to assign to the new keys.
|
290 |
290 |
void resize(int size, const Value &value = Value()) {
|
291 |
291 |
_vector.resize(size, value);
|
292 |
292 |
}
|
293 |
293 |
|
294 |
294 |
private:
|
295 |
295 |
|
296 |
296 |
RangeMap& operator=(const RangeMap&);
|
297 |
297 |
|
298 |
298 |
public:
|
299 |
299 |
|
300 |
300 |
///\e
|
301 |
301 |
Reference operator[](const Key &k) {
|
302 |
302 |
return _vector[k];
|
303 |
303 |
}
|
304 |
304 |
|
305 |
305 |
///\e
|
306 |
306 |
ConstReference operator[](const Key &k) const {
|
307 |
307 |
return _vector[k];
|
308 |
308 |
}
|
309 |
309 |
|
310 |
310 |
///\e
|
311 |
311 |
void set(const Key &k, const Value &v) {
|
312 |
312 |
_vector[k] = v;
|
313 |
313 |
}
|
314 |
314 |
};
|
315 |
315 |
|
316 |
316 |
/// Returns a \c RangeMap class
|
317 |
317 |
|
318 |
318 |
/// This function just returns a \c RangeMap class.
|
319 |
319 |
/// \relates RangeMap
|
320 |
320 |
template<typename V>
|
321 |
321 |
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
|
322 |
322 |
return RangeMap<V>(size, value);
|
323 |
323 |
}
|
324 |
324 |
|
325 |
325 |
/// \brief Returns a \c RangeMap class created from an appropriate
|
326 |
326 |
/// \c std::vector
|
327 |
327 |
|
328 |
328 |
/// This function just returns a \c RangeMap class created from an
|
329 |
329 |
/// appropriate \c std::vector.
|
330 |
330 |
/// \relates RangeMap
|
331 |
331 |
template<typename V>
|
332 |
332 |
inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
|
333 |
333 |
return RangeMap<V>(vector);
|
334 |
334 |
}
|
335 |
335 |
|
336 |
336 |
|
337 |
337 |
/// Map type based on \c std::map
|
338 |
338 |
|
339 |
339 |
/// This map is essentially a wrapper for \c std::map with addition
|
340 |
340 |
/// that you can specify a default value for the keys that are not
|
341 |
341 |
/// stored actually. This value can be different from the default
|
342 |
342 |
/// contructed value (i.e. \c %Value()).
|
343 |
343 |
/// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
|
344 |
344 |
/// concept.
|
345 |
345 |
///
|
346 |
346 |
/// This map is useful if a default value should be assigned to most of
|
347 |
347 |
/// the keys and different values should be assigned only to a few
|
348 |
348 |
/// keys (i.e. the map is "sparse").
|
349 |
349 |
/// The name of this type also refers to this important usage.
|
350 |
350 |
///
|
351 |
351 |
/// Apart form that, this map can be used in many other cases since it
|
352 |
352 |
/// is based on \c std::map, which is a general associative container.
|
353 |
353 |
/// However, keep in mind that it is usually not as efficient as other
|
354 |
354 |
/// maps.
|
355 |
355 |
///
|
356 |
356 |
/// The simplest way of using this map is through the sparseMap()
|
357 |
357 |
/// function.
|
358 |
358 |
template <typename K, typename V, typename Comp = std::less<K> >
|
359 |
359 |
class SparseMap : public MapBase<K, V> {
|
360 |
360 |
template <typename K1, typename V1, typename C1>
|
361 |
361 |
friend class SparseMap;
|
362 |
362 |
public:
|
363 |
363 |
|
364 |
364 |
/// Key type
|
365 |
365 |
typedef K Key;
|
366 |
366 |
/// Value type
|
367 |
367 |
typedef V Value;
|
368 |
368 |
/// Reference type
|
369 |
369 |
typedef Value& Reference;
|
370 |
370 |
/// Const reference type
|
371 |
371 |
typedef const Value& ConstReference;
|
372 |
372 |
|
373 |
373 |
typedef True ReferenceMapTag;
|
374 |
374 |
|
375 |
375 |
private:
|
376 |
376 |
|
377 |
377 |
typedef std::map<K, V, Comp> Map;
|
378 |
378 |
Map _map;
|
379 |
379 |
Value _value;
|
380 |
380 |
|
381 |
381 |
public:
|
382 |
382 |
|
383 |
383 |
/// \brief Constructor with specified default value.
|
384 |
384 |
SparseMap(const Value &value = Value()) : _value(value) {}
|
385 |
385 |
/// \brief Constructs the map from an appropriate \c std::map, and
|
386 |
386 |
/// explicitly specifies a default value.
|
387 |
387 |
template <typename V1, typename Comp1>
|
388 |
388 |
SparseMap(const std::map<Key, V1, Comp1> &map,
|
389 |
389 |
const Value &value = Value())
|
390 |
390 |
: _map(map.begin(), map.end()), _value(value) {}
|
391 |
391 |
|
392 |
392 |
/// \brief Constructs the map from another \c SparseMap.
|
393 |
393 |
template<typename V1, typename Comp1>
|
394 |
394 |
SparseMap(const SparseMap<Key, V1, Comp1> &c)
|
395 |
395 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {}
|
396 |
396 |
|
397 |
397 |
private:
|
398 |
398 |
|
399 |
399 |
SparseMap& operator=(const SparseMap&);
|
400 |
400 |
|
401 |
401 |
public:
|
402 |
402 |
|
403 |
403 |
///\e
|
404 |
404 |
Reference operator[](const Key &k) {
|
405 |
405 |
typename Map::iterator it = _map.lower_bound(k);
|
406 |
406 |
if (it != _map.end() && !_map.key_comp()(k, it->first))
|
407 |
407 |
return it->second;
|
408 |
408 |
else
|
409 |
409 |
return _map.insert(it, std::make_pair(k, _value))->second;
|
410 |
410 |
}
|
411 |
411 |
|
412 |
412 |
///\e
|
413 |
413 |
ConstReference operator[](const Key &k) const {
|
414 |
414 |
typename Map::const_iterator it = _map.find(k);
|
415 |
415 |
if (it != _map.end())
|
416 |
416 |
return it->second;
|
417 |
417 |
else
|
418 |
418 |
return _value;
|
419 |
419 |
}
|
420 |
420 |
|
421 |
421 |
///\e
|
422 |
422 |
void set(const Key &k, const Value &v) {
|
423 |
423 |
typename Map::iterator it = _map.lower_bound(k);
|
424 |
424 |
if (it != _map.end() && !_map.key_comp()(k, it->first))
|
425 |
425 |
it->second = v;
|
426 |
426 |
else
|
427 |
427 |
_map.insert(it, std::make_pair(k, v));
|
428 |
428 |
}
|
429 |
429 |
|
430 |
430 |
///\e
|
431 |
431 |
void setAll(const Value &v) {
|
432 |
432 |
_value = v;
|
433 |
433 |
_map.clear();
|
434 |
434 |
}
|
435 |
435 |
};
|
436 |
436 |
|
437 |
437 |
/// Returns a \c SparseMap class
|
438 |
438 |
|
439 |
439 |
/// This function just returns a \c SparseMap class with specified
|
440 |
440 |
/// default value.
|
441 |
441 |
/// \relates SparseMap
|
442 |
442 |
template<typename K, typename V, typename Compare>
|
443 |
443 |
inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
|
444 |
444 |
return SparseMap<K, V, Compare>(value);
|
445 |
445 |
}
|
446 |
446 |
|
447 |
447 |
template<typename K, typename V>
|
448 |
448 |
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
|
449 |
449 |
return SparseMap<K, V, std::less<K> >(value);
|
450 |
450 |
}
|
451 |
451 |
|
452 |
452 |
/// \brief Returns a \c SparseMap class created from an appropriate
|
453 |
453 |
/// \c std::map
|
454 |
454 |
|
455 |
455 |
/// This function just returns a \c SparseMap class created from an
|
456 |
456 |
/// appropriate \c std::map.
|
457 |
457 |
/// \relates SparseMap
|
458 |
458 |
template<typename K, typename V, typename Compare>
|
459 |
459 |
inline SparseMap<K, V, Compare>
|
460 |
460 |
sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
|
461 |
461 |
{
|
462 |
462 |
return SparseMap<K, V, Compare>(map, value);
|
463 |
463 |
}
|
464 |
464 |
|
465 |
465 |
/// @}
|
466 |
466 |
|
467 |
467 |
/// \addtogroup map_adaptors
|
468 |
468 |
/// @{
|
469 |
469 |
|
470 |
470 |
/// Composition of two maps
|
471 |
471 |
|
472 |
472 |
/// This \ref concepts::ReadMap "read-only map" returns the
|
473 |
473 |
/// composition of two given maps. That is to say, if \c m1 is of
|
474 |
474 |
/// type \c M1 and \c m2 is of \c M2, then for
|
475 |
475 |
/// \code
|
476 |
476 |
/// ComposeMap<M1, M2> cm(m1,m2);
|
477 |
477 |
/// \endcode
|
478 |
478 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
|
479 |
479 |
///
|
480 |
480 |
/// The \c Key type of the map is inherited from \c M2 and the
|
481 |
481 |
/// \c Value type is from \c M1.
|
482 |
482 |
/// \c M2::Value must be convertible to \c M1::Key.
|
483 |
483 |
///
|
484 |
484 |
/// The simplest way of using this map is through the composeMap()
|
485 |
485 |
/// function.
|
486 |
486 |
///
|
487 |
487 |
/// \sa CombineMap
|
488 |
488 |
template <typename M1, typename M2>
|
489 |
489 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
|
490 |
490 |
const M1 &_m1;
|
491 |
491 |
const M2 &_m2;
|
492 |
492 |
public:
|
493 |
493 |
///\e
|
494 |
494 |
typedef typename M2::Key Key;
|
495 |
495 |
///\e
|
496 |
496 |
typedef typename M1::Value Value;
|
497 |
497 |
|
498 |
498 |
/// Constructor
|
499 |
499 |
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
500 |
500 |
|
501 |
501 |
///\e
|
502 |
502 |
typename MapTraits<M1>::ConstReturnValue
|
503 |
503 |
operator[](const Key &k) const { return _m1[_m2[k]]; }
|
504 |
504 |
};
|
505 |
505 |
|
506 |
506 |
/// Returns a \c ComposeMap class
|
507 |
507 |
|
508 |
508 |
/// This function just returns a \c ComposeMap class.
|
509 |
509 |
///
|
510 |
510 |
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
|
511 |
511 |
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
|
512 |
512 |
/// will be equal to <tt>m1[m2[x]]</tt>.
|
513 |
513 |
///
|
514 |
514 |
/// \relates ComposeMap
|
515 |
515 |
template <typename M1, typename M2>
|
516 |
516 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
|
517 |
517 |
return ComposeMap<M1, M2>(m1, m2);
|
518 |
518 |
}
|
519 |
519 |
|
520 |
520 |
|
521 |
521 |
/// Combination of two maps using an STL (binary) functor.
|
522 |
522 |
|
523 |
523 |
/// This \ref concepts::ReadMap "read-only map" takes two maps and a
|
524 |
524 |
/// binary functor and returns the combination of the two given maps
|
525 |
525 |
/// using the functor.
|
526 |
526 |
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
|
527 |
527 |
/// and \c f is of \c F, then for
|
528 |
528 |
/// \code
|
529 |
529 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f);
|
530 |
530 |
/// \endcode
|
531 |
531 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
|
532 |
532 |
///
|
533 |
533 |
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key
|
534 |
534 |
/// must be convertible to \c M2::Key) and the \c Value type is \c V.
|
535 |
535 |
/// \c M2::Value and \c M1::Value must be convertible to the
|
536 |
536 |
/// corresponding input parameter of \c F and the return type of \c F
|
537 |
537 |
/// must be convertible to \c V.
|
538 |
538 |
///
|
539 |
539 |
/// The simplest way of using this map is through the combineMap()
|
540 |
540 |
/// function.
|
541 |
541 |
///
|
542 |
542 |
/// \sa ComposeMap
|
543 |
543 |
template<typename M1, typename M2, typename F,
|
544 |
544 |
typename V = typename F::result_type>
|
545 |
545 |
class CombineMap : public MapBase<typename M1::Key, V> {
|
546 |
546 |
const M1 &_m1;
|
547 |
547 |
const M2 &_m2;
|
548 |
548 |
F _f;
|
549 |
549 |
public:
|
550 |
550 |
///\e
|
551 |
551 |
typedef typename M1::Key Key;
|
552 |
552 |
///\e
|
553 |
553 |
typedef V Value;
|
554 |
554 |
|
555 |
555 |
/// Constructor
|
556 |
556 |
CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
|
557 |
557 |
: _m1(m1), _m2(m2), _f(f) {}
|
558 |
558 |
///\e
|
559 |
559 |
Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
|
560 |
560 |
};
|
561 |
561 |
|
562 |
562 |
/// Returns a \c CombineMap class
|
563 |
563 |
|
564 |
564 |
/// This function just returns a \c CombineMap class.
|
565 |
565 |
///
|
566 |
566 |
/// For example, if \c m1 and \c m2 are both maps with \c double
|
567 |
567 |
/// values, then
|
568 |
568 |
/// \code
|
569 |
569 |
/// combineMap(m1,m2,std::plus<double>())
|
570 |
570 |
/// \endcode
|
571 |
571 |
/// is equivalent to
|
572 |
572 |
/// \code
|
573 |
573 |
/// addMap(m1,m2)
|
574 |
574 |
/// \endcode
|
575 |
575 |
///
|
576 |
576 |
/// This function is specialized for adaptable binary function
|
577 |
577 |
/// classes and C++ functions.
|
578 |
578 |
///
|
579 |
579 |
/// \relates CombineMap
|
580 |
580 |
template<typename M1, typename M2, typename F, typename V>
|
581 |
581 |
inline CombineMap<M1, M2, F, V>
|
582 |
582 |
combineMap(const M1 &m1, const M2 &m2, const F &f) {
|
583 |
583 |
return CombineMap<M1, M2, F, V>(m1,m2,f);
|
584 |
584 |
}
|
585 |
585 |
|
586 |
586 |
template<typename M1, typename M2, typename F>
|
587 |
587 |
inline CombineMap<M1, M2, F, typename F::result_type>
|
588 |
588 |
combineMap(const M1 &m1, const M2 &m2, const F &f) {
|
589 |
589 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
|
590 |
590 |
}
|
591 |
591 |
|
592 |
592 |
template<typename M1, typename M2, typename K1, typename K2, typename V>
|
593 |
593 |
inline CombineMap<M1, M2, V (*)(K1, K2), V>
|
594 |
594 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
|
595 |
595 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
|
596 |
596 |
}
|
597 |
597 |
|
598 |
598 |
|
599 |
599 |
/// Converts an STL style (unary) functor to a map
|
600 |
600 |
|
601 |
601 |
/// This \ref concepts::ReadMap "read-only map" returns the value
|
602 |
602 |
/// of a given functor. Actually, it just wraps the functor and
|
603 |
603 |
/// provides the \c Key and \c Value typedefs.
|
604 |
604 |
///
|
605 |
605 |
/// Template parameters \c K and \c V will become its \c Key and
|
606 |
606 |
/// \c Value. In most cases they have to be given explicitly because
|
607 |
607 |
/// a functor typically does not provide \c argument_type and
|
608 |
608 |
/// \c result_type typedefs.
|
609 |
609 |
/// Parameter \c F is the type of the used functor.
|
610 |
610 |
///
|
611 |
611 |
/// The simplest way of using this map is through the functorToMap()
|
612 |
612 |
/// function.
|
613 |
613 |
///
|
614 |
614 |
/// \sa MapToFunctor
|
615 |
615 |
template<typename F,
|
616 |
616 |
typename K = typename F::argument_type,
|
617 |
617 |
typename V = typename F::result_type>
|
618 |
618 |
class FunctorToMap : public MapBase<K, V> {
|
619 |
619 |
F _f;
|
620 |
620 |
public:
|
621 |
621 |
///\e
|
622 |
622 |
typedef K Key;
|
623 |
623 |
///\e
|
624 |
624 |
typedef V Value;
|
625 |
625 |
|
626 |
626 |
/// Constructor
|
627 |
627 |
FunctorToMap(const F &f = F()) : _f(f) {}
|
628 |
628 |
///\e
|
629 |
629 |
Value operator[](const Key &k) const { return _f(k); }
|
630 |
630 |
};
|
631 |
631 |
|
632 |
632 |
/// Returns a \c FunctorToMap class
|
633 |
633 |
|
634 |
634 |
/// This function just returns a \c FunctorToMap class.
|
635 |
635 |
///
|
636 |
636 |
/// This function is specialized for adaptable binary function
|
637 |
637 |
/// classes and C++ functions.
|
638 |
638 |
///
|
639 |
639 |
/// \relates FunctorToMap
|
640 |
640 |
template<typename K, typename V, typename F>
|
641 |
641 |
inline FunctorToMap<F, K, V> functorToMap(const F &f) {
|
642 |
642 |
return FunctorToMap<F, K, V>(f);
|
643 |
643 |
}
|
644 |
644 |
|
645 |
645 |
template <typename F>
|
646 |
646 |
inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
|
647 |
647 |
functorToMap(const F &f)
|
648 |
648 |
{
|
649 |
649 |
return FunctorToMap<F, typename F::argument_type,
|
650 |
650 |
typename F::result_type>(f);
|
651 |
651 |
}
|
652 |
652 |
|
653 |
653 |
template <typename K, typename V>
|
654 |
654 |
inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
|
655 |
655 |
return FunctorToMap<V (*)(K), K, V>(f);
|
656 |
656 |
}
|
657 |
657 |
|
658 |
658 |
|
659 |
659 |
/// Converts a map to an STL style (unary) functor
|
660 |
660 |
|
661 |
661 |
/// This class converts a map to an STL style (unary) functor.
|
662 |
662 |
/// That is it provides an <tt>operator()</tt> to read its values.
|
663 |
663 |
///
|
664 |
664 |
/// For the sake of convenience it also works as a usual
|
665 |
665 |
/// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
|
666 |
666 |
/// and the \c Key and \c Value typedefs also exist.
|
667 |
667 |
///
|
668 |
668 |
/// The simplest way of using this map is through the mapToFunctor()
|
669 |
669 |
/// function.
|
670 |
670 |
///
|
671 |
671 |
///\sa FunctorToMap
|
672 |
672 |
template <typename M>
|
673 |
673 |
class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
|
674 |
674 |
const M &_m;
|
675 |
675 |
public:
|
676 |
676 |
///\e
|
677 |
677 |
typedef typename M::Key Key;
|
678 |
678 |
///\e
|
679 |
679 |
typedef typename M::Value Value;
|
680 |
680 |
|
681 |
681 |
typedef typename M::Key argument_type;
|
682 |
682 |
typedef typename M::Value result_type;
|
683 |
683 |
|
684 |
684 |
/// Constructor
|
685 |
685 |
MapToFunctor(const M &m) : _m(m) {}
|
686 |
686 |
///\e
|
687 |
687 |
Value operator()(const Key &k) const { return _m[k]; }
|
688 |
688 |
///\e
|
689 |
689 |
Value operator[](const Key &k) const { return _m[k]; }
|
690 |
690 |
};
|
691 |
691 |
|
692 |
692 |
/// Returns a \c MapToFunctor class
|
693 |
693 |
|
694 |
694 |
/// This function just returns a \c MapToFunctor class.
|
695 |
695 |
/// \relates MapToFunctor
|
696 |
696 |
template<typename M>
|
697 |
697 |
inline MapToFunctor<M> mapToFunctor(const M &m) {
|
698 |
698 |
return MapToFunctor<M>(m);
|
699 |
699 |
}
|
700 |
700 |
|
701 |
701 |
|
702 |
702 |
/// \brief Map adaptor to convert the \c Value type of a map to
|
703 |
703 |
/// another type using the default conversion.
|
704 |
704 |
|
705 |
705 |
/// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
|
706 |
706 |
/// "readable map" to another type using the default conversion.
|
707 |
707 |
/// The \c Key type of it is inherited from \c M and the \c Value
|
708 |
708 |
/// type is \c V.
|
709 |
709 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
|
710 |
710 |
///
|
711 |
711 |
/// The simplest way of using this map is through the convertMap()
|
712 |
712 |
/// function.
|
713 |
713 |
template <typename M, typename V>
|
714 |
714 |
class ConvertMap : public MapBase<typename M::Key, V> {
|
715 |
715 |
const M &_m;
|
716 |
716 |
public:
|
717 |
717 |
///\e
|
718 |
718 |
typedef typename M::Key Key;
|
719 |
719 |
///\e
|
720 |
720 |
typedef V Value;
|
721 |
721 |
|
722 |
722 |
/// Constructor
|
723 |
723 |
|
724 |
724 |
/// Constructor.
|
725 |
725 |
/// \param m The underlying map.
|
726 |
726 |
ConvertMap(const M &m) : _m(m) {}
|
727 |
727 |
|
728 |
728 |
///\e
|
729 |
729 |
Value operator[](const Key &k) const { return _m[k]; }
|
730 |
730 |
};
|
731 |
731 |
|
732 |
732 |
/// Returns a \c ConvertMap class
|
733 |
733 |
|
734 |
734 |
/// This function just returns a \c ConvertMap class.
|
735 |
735 |
/// \relates ConvertMap
|
736 |
736 |
template<typename V, typename M>
|
737 |
737 |
inline ConvertMap<M, V> convertMap(const M &map) {
|
738 |
738 |
return ConvertMap<M, V>(map);
|
739 |
739 |
}
|
740 |
740 |
|
741 |
741 |
|
742 |
742 |
/// Applies all map setting operations to two maps
|
743 |
743 |
|
744 |
744 |
/// This map has two \ref concepts::WriteMap "writable map" parameters
|
745 |
745 |
/// and each write request will be passed to both of them.
|
746 |
746 |
/// If \c M1 is also \ref concepts::ReadMap "readable", then the read
|
747 |
747 |
/// operations will return the corresponding values of \c M1.
|
748 |
748 |
///
|
749 |
749 |
/// The \c Key and \c Value types are inherited from \c M1.
|
750 |
750 |
/// The \c Key and \c Value of \c M2 must be convertible from those
|
751 |
751 |
/// of \c M1.
|
752 |
752 |
///
|
753 |
753 |
/// The simplest way of using this map is through the forkMap()
|
754 |
754 |
/// function.
|
755 |
755 |
template<typename M1, typename M2>
|
756 |
756 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
|
757 |
757 |
M1 &_m1;
|
758 |
758 |
M2 &_m2;
|
759 |
759 |
public:
|
760 |
760 |
///\e
|
761 |
761 |
typedef typename M1::Key Key;
|
762 |
762 |
///\e
|
763 |
763 |
typedef typename M1::Value Value;
|
764 |
764 |
|
765 |
765 |
/// Constructor
|
766 |
766 |
ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
|
767 |
767 |
/// Returns the value associated with the given key in the first map.
|
768 |
768 |
Value operator[](const Key &k) const { return _m1[k]; }
|
769 |
769 |
/// Sets the value associated with the given key in both maps.
|
770 |
770 |
void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
|
771 |
771 |
};
|
772 |
772 |
|
773 |
773 |
/// Returns a \c ForkMap class
|
774 |
774 |
|
775 |
775 |
/// This function just returns a \c ForkMap class.
|
776 |
776 |
/// \relates ForkMap
|
777 |
777 |
template <typename M1, typename M2>
|
778 |
778 |
inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
|
779 |
779 |
return ForkMap<M1,M2>(m1,m2);
|
780 |
780 |
}
|
781 |
781 |
|
782 |
782 |
|
783 |
783 |
/// Sum of two maps
|
784 |
784 |
|
785 |
785 |
/// This \ref concepts::ReadMap "read-only map" returns the sum
|
786 |
786 |
/// of the values of the two given maps.
|
787 |
787 |
/// Its \c Key and \c Value types are inherited from \c M1.
|
788 |
788 |
/// The \c Key and \c Value of \c M2 must be convertible to those of
|
789 |
789 |
/// \c M1.
|
790 |
790 |
///
|
791 |
791 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
792 |
792 |
/// \code
|
793 |
793 |
/// AddMap<M1,M2> am(m1,m2);
|
794 |
794 |
/// \endcode
|
795 |
795 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
|
796 |
796 |
///
|
797 |
797 |
/// The simplest way of using this map is through the addMap()
|
798 |
798 |
/// function.
|
799 |
799 |
///
|
800 |
800 |
/// \sa SubMap, MulMap, DivMap
|
801 |
801 |
/// \sa ShiftMap, ShiftWriteMap
|
802 |
802 |
template<typename M1, typename M2>
|
803 |
803 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
|
804 |
804 |
const M1 &_m1;
|
805 |
805 |
const M2 &_m2;
|
806 |
806 |
public:
|
807 |
807 |
///\e
|
808 |
808 |
typedef typename M1::Key Key;
|
809 |
809 |
///\e
|
810 |
810 |
typedef typename M1::Value Value;
|
811 |
811 |
|
812 |
812 |
/// Constructor
|
813 |
813 |
AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
814 |
814 |
///\e
|
815 |
815 |
Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
|
816 |
816 |
};
|
817 |
817 |
|
818 |
818 |
/// Returns an \c AddMap class
|
819 |
819 |
|
820 |
820 |
/// This function just returns an \c AddMap class.
|
821 |
821 |
///
|
822 |
822 |
/// For example, if \c m1 and \c m2 are both maps with \c double
|
823 |
823 |
/// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
|
824 |
824 |
/// <tt>m1[x]+m2[x]</tt>.
|
825 |
825 |
///
|
826 |
826 |
/// \relates AddMap
|
827 |
827 |
template<typename M1, typename M2>
|
828 |
828 |
inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
|
829 |
829 |
return AddMap<M1, M2>(m1,m2);
|
830 |
830 |
}
|
831 |
831 |
|
832 |
832 |
|
833 |
833 |
/// Difference of two maps
|
834 |
834 |
|
835 |
835 |
/// This \ref concepts::ReadMap "read-only map" returns the difference
|
836 |
836 |
/// of the values of the two given maps.
|
837 |
837 |
/// Its \c Key and \c Value types are inherited from \c M1.
|
838 |
838 |
/// The \c Key and \c Value of \c M2 must be convertible to those of
|
839 |
839 |
/// \c M1.
|
840 |
840 |
///
|
841 |
841 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
842 |
842 |
/// \code
|
843 |
843 |
/// SubMap<M1,M2> sm(m1,m2);
|
844 |
844 |
/// \endcode
|
845 |
845 |
/// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
|
846 |
846 |
///
|
847 |
847 |
/// The simplest way of using this map is through the subMap()
|
848 |
848 |
/// function.
|
849 |
849 |
///
|
850 |
850 |
/// \sa AddMap, MulMap, DivMap
|
851 |
851 |
template<typename M1, typename M2>
|
852 |
852 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
|
853 |
853 |
const M1 &_m1;
|
854 |
854 |
const M2 &_m2;
|
855 |
855 |
public:
|
856 |
856 |
///\e
|
857 |
857 |
typedef typename M1::Key Key;
|
858 |
858 |
///\e
|
859 |
859 |
typedef typename M1::Value Value;
|
860 |
860 |
|
861 |
861 |
/// Constructor
|
862 |
862 |
SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
863 |
863 |
///\e
|
864 |
864 |
Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
|
865 |
865 |
};
|
866 |
866 |
|
867 |
867 |
/// Returns a \c SubMap class
|
868 |
868 |
|
869 |
869 |
/// This function just returns a \c SubMap class.
|
870 |
870 |
///
|
871 |
871 |
/// For example, if \c m1 and \c m2 are both maps with \c double
|
872 |
872 |
/// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
|
873 |
873 |
/// <tt>m1[x]-m2[x]</tt>.
|
874 |
874 |
///
|
875 |
875 |
/// \relates SubMap
|
876 |
876 |
template<typename M1, typename M2>
|
877 |
877 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
|
878 |
878 |
return SubMap<M1, M2>(m1,m2);
|
879 |
879 |
}
|
880 |
880 |
|
881 |
881 |
|
882 |
882 |
/// Product of two maps
|
883 |
883 |
|
884 |
884 |
/// This \ref concepts::ReadMap "read-only map" returns the product
|
885 |
885 |
/// of the values of the two given maps.
|
886 |
886 |
/// Its \c Key and \c Value types are inherited from \c M1.
|
887 |
887 |
/// The \c Key and \c Value of \c M2 must be convertible to those of
|
888 |
888 |
/// \c M1.
|
889 |
889 |
///
|
890 |
890 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
891 |
891 |
/// \code
|
892 |
892 |
/// MulMap<M1,M2> mm(m1,m2);
|
893 |
893 |
/// \endcode
|
894 |
894 |
/// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
|
895 |
895 |
///
|
896 |
896 |
/// The simplest way of using this map is through the mulMap()
|
897 |
897 |
/// function.
|
898 |
898 |
///
|
899 |
899 |
/// \sa AddMap, SubMap, DivMap
|
900 |
900 |
/// \sa ScaleMap, ScaleWriteMap
|
901 |
901 |
template<typename M1, typename M2>
|
902 |
902 |
class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
|
903 |
903 |
const M1 &_m1;
|
904 |
904 |
const M2 &_m2;
|
905 |
905 |
public:
|
906 |
906 |
///\e
|
907 |
907 |
typedef typename M1::Key Key;
|
908 |
908 |
///\e
|
909 |
909 |
typedef typename M1::Value Value;
|
910 |
910 |
|
911 |
911 |
/// Constructor
|
912 |
912 |
MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
|
913 |
913 |
///\e
|
914 |
914 |
Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
|
915 |
915 |
};
|
916 |
916 |
|
917 |
917 |
/// Returns a \c MulMap class
|
918 |
918 |
|
919 |
919 |
/// This function just returns a \c MulMap class.
|
920 |
920 |
///
|
921 |
921 |
/// For example, if \c m1 and \c m2 are both maps with \c double
|
922 |
922 |
/// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
|
923 |
923 |
/// <tt>m1[x]*m2[x]</tt>.
|
924 |
924 |
///
|
925 |
925 |
/// \relates MulMap
|
926 |
926 |
template<typename M1, typename M2>
|
927 |
927 |
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
|
928 |
928 |
return MulMap<M1, M2>(m1,m2);
|
929 |
929 |
}
|
930 |
930 |
|
931 |
931 |
|
932 |
932 |
/// Quotient of two maps
|
933 |
933 |
|
934 |
934 |
/// This \ref concepts::ReadMap "read-only map" returns the quotient
|
935 |
935 |
/// of the values of the two given maps.
|
936 |
936 |
/// Its \c Key and \c Value types are inherited from \c M1.
|
937 |
937 |
/// The \c Key and \c Value of \c M2 must be convertible to those of
|
938 |
938 |
/// \c M1.
|
939 |
939 |
///
|
940 |
940 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
941 |
941 |
/// \code
|
942 |
942 |
/// DivMap<M1,M2> dm(m1,m2);
|
943 |
943 |
/// \endcode
|
944 |
944 |
/// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
|
945 |
945 |
///
|
946 |
946 |
/// The simplest way of using this map is through the divMap()
|
947 |
947 |
/// function.
|
948 |
948 |
///
|
949 |
949 |
/// \sa AddMap, SubMap, MulMap
|
950 |
950 |
template<typename M1, typename M2>
|
951 |
951 |
class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
|
952 |
952 |
const M1 &_m1;
|
953 |
953 |
const M2 &_m2;
|
954 |
954 |
public:
|
955 |
955 |
///\e
|
956 |
956 |
typedef typename M1::Key Key;
|
957 |
957 |
///\e
|
958 |
958 |
typedef typename M1::Value Value;
|
959 |
959 |
|
960 |
960 |
/// Constructor
|
961 |
961 |
DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
|
962 |
962 |
///\e
|
963 |
963 |
Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
|
964 |
964 |
};
|
965 |
965 |
|
966 |
966 |
/// Returns a \c DivMap class
|
967 |
967 |
|
968 |
968 |
/// This function just returns a \c DivMap class.
|
969 |
969 |
///
|
970 |
970 |
/// For example, if \c m1 and \c m2 are both maps with \c double
|
971 |
971 |
/// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
|
972 |
972 |
/// <tt>m1[x]/m2[x]</tt>.
|
973 |
973 |
///
|
974 |
974 |
/// \relates DivMap
|
975 |
975 |
template<typename M1, typename M2>
|
976 |
976 |
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
|
977 |
977 |
return DivMap<M1, M2>(m1,m2);
|
978 |
978 |
}
|
979 |
979 |
|
980 |
980 |
|
981 |
981 |
/// Shifts a map with a constant.
|
982 |
982 |
|
983 |
983 |
/// This \ref concepts::ReadMap "read-only map" returns the sum of
|
984 |
984 |
/// the given map and a constant value (i.e. it shifts the map with
|
985 |
985 |
/// the constant). Its \c Key and \c Value are inherited from \c M.
|
986 |
986 |
///
|
987 |
987 |
/// Actually,
|
988 |
988 |
/// \code
|
989 |
989 |
/// ShiftMap<M> sh(m,v);
|
990 |
990 |
/// \endcode
|
991 |
991 |
/// is equivalent to
|
992 |
992 |
/// \code
|
993 |
993 |
/// ConstMap<M::Key, M::Value> cm(v);
|
994 |
994 |
/// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
|
995 |
995 |
/// \endcode
|
996 |
996 |
///
|
997 |
997 |
/// The simplest way of using this map is through the shiftMap()
|
998 |
998 |
/// function.
|
999 |
999 |
///
|
1000 |
1000 |
/// \sa ShiftWriteMap
|
1001 |
1001 |
template<typename M, typename C = typename M::Value>
|
1002 |
1002 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
|
1003 |
1003 |
const M &_m;
|
1004 |
1004 |
C _v;
|
1005 |
1005 |
public:
|
1006 |
1006 |
///\e
|
1007 |
1007 |
typedef typename M::Key Key;
|
1008 |
1008 |
///\e
|
1009 |
1009 |
typedef typename M::Value Value;
|
1010 |
1010 |
|
1011 |
1011 |
/// Constructor
|
1012 |
1012 |
|
1013 |
1013 |
/// Constructor.
|
1014 |
1014 |
/// \param m The undelying map.
|
1015 |
1015 |
/// \param v The constant value.
|
1016 |
1016 |
ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
|
1017 |
1017 |
///\e
|
1018 |
1018 |
Value operator[](const Key &k) const { return _m[k]+_v; }
|
1019 |
1019 |
};
|
1020 |
1020 |
|
1021 |
1021 |
/// Shifts a map with a constant (read-write version).
|
1022 |
1022 |
|
1023 |
1023 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the sum
|
1024 |
1024 |
/// of the given map and a constant value (i.e. it shifts the map with
|
1025 |
1025 |
/// the constant). Its \c Key and \c Value are inherited from \c M.
|
1026 |
1026 |
/// It makes also possible to write the map.
|
1027 |
1027 |
///
|
1028 |
1028 |
/// The simplest way of using this map is through the shiftWriteMap()
|
1029 |
1029 |
/// function.
|
1030 |
1030 |
///
|
1031 |
1031 |
/// \sa ShiftMap
|
1032 |
1032 |
template<typename M, typename C = typename M::Value>
|
1033 |
1033 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
1034 |
1034 |
M &_m;
|
1035 |
1035 |
C _v;
|
1036 |
1036 |
public:
|
1037 |
1037 |
///\e
|
1038 |
1038 |
typedef typename M::Key Key;
|
1039 |
1039 |
///\e
|
1040 |
1040 |
typedef typename M::Value Value;
|
1041 |
1041 |
|
1042 |
1042 |
/// Constructor
|
1043 |
1043 |
|
1044 |
1044 |
/// Constructor.
|
1045 |
1045 |
/// \param m The undelying map.
|
1046 |
1046 |
/// \param v The constant value.
|
1047 |
1047 |
ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
|
1048 |
1048 |
///\e
|
1049 |
1049 |
Value operator[](const Key &k) const { return _m[k]+_v; }
|
1050 |
1050 |
///\e
|
1051 |
1051 |
void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
|
1052 |
1052 |
};
|
1053 |
1053 |
|
1054 |
1054 |
/// Returns a \c ShiftMap class
|
1055 |
1055 |
|
1056 |
1056 |
/// This function just returns a \c ShiftMap class.
|
1057 |
1057 |
///
|
1058 |
1058 |
/// For example, if \c m is a map with \c double values and \c v is
|
1059 |
1059 |
/// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
|
1060 |
1060 |
/// <tt>m[x]+v</tt>.
|
1061 |
1061 |
///
|
1062 |
1062 |
/// \relates ShiftMap
|
1063 |
1063 |
template<typename M, typename C>
|
1064 |
1064 |
inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
|
1065 |
1065 |
return ShiftMap<M, C>(m,v);
|
1066 |
1066 |
}
|
1067 |
1067 |
|
1068 |
1068 |
/// Returns a \c ShiftWriteMap class
|
1069 |
1069 |
|
1070 |
1070 |
/// This function just returns a \c ShiftWriteMap class.
|
1071 |
1071 |
///
|
1072 |
1072 |
/// For example, if \c m is a map with \c double values and \c v is
|
1073 |
1073 |
/// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
|
1074 |
1074 |
/// <tt>m[x]+v</tt>.
|
1075 |
1075 |
/// Moreover it makes also possible to write the map.
|
1076 |
1076 |
///
|
1077 |
1077 |
/// \relates ShiftWriteMap
|
1078 |
1078 |
template<typename M, typename C>
|
1079 |
1079 |
inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
|
1080 |
1080 |
return ShiftWriteMap<M, C>(m,v);
|
1081 |
1081 |
}
|
1082 |
1082 |
|
1083 |
1083 |
|
1084 |
1084 |
/// Scales a map with a constant.
|
1085 |
1085 |
|
1086 |
1086 |
/// This \ref concepts::ReadMap "read-only map" returns the value of
|
1087 |
1087 |
/// the given map multiplied from the left side with a constant value.
|
1088 |
1088 |
/// Its \c Key and \c Value are inherited from \c M.
|
1089 |
1089 |
///
|
1090 |
1090 |
/// Actually,
|
1091 |
1091 |
/// \code
|
1092 |
1092 |
/// ScaleMap<M> sc(m,v);
|
1093 |
1093 |
/// \endcode
|
1094 |
1094 |
/// is equivalent to
|
1095 |
1095 |
/// \code
|
1096 |
1096 |
/// ConstMap<M::Key, M::Value> cm(v);
|
1097 |
1097 |
/// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
|
1098 |
1098 |
/// \endcode
|
1099 |
1099 |
///
|
1100 |
1100 |
/// The simplest way of using this map is through the scaleMap()
|
1101 |
1101 |
/// function.
|
1102 |
1102 |
///
|
1103 |
1103 |
/// \sa ScaleWriteMap
|
1104 |
1104 |
template<typename M, typename C = typename M::Value>
|
1105 |
1105 |
class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
|
1106 |
1106 |
const M &_m;
|
1107 |
1107 |
C _v;
|
1108 |
1108 |
public:
|
1109 |
1109 |
///\e
|
1110 |
1110 |
typedef typename M::Key Key;
|
1111 |
1111 |
///\e
|
1112 |
1112 |
typedef typename M::Value Value;
|
1113 |
1113 |
|
1114 |
1114 |
/// Constructor
|
1115 |
1115 |
|
1116 |
1116 |
/// Constructor.
|
1117 |
1117 |
/// \param m The undelying map.
|
1118 |
1118 |
/// \param v The constant value.
|
1119 |
1119 |
ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
|
1120 |
1120 |
///\e
|
1121 |
1121 |
Value operator[](const Key &k) const { return _v*_m[k]; }
|
1122 |
1122 |
};
|
1123 |
1123 |
|
1124 |
1124 |
/// Scales a map with a constant (read-write version).
|
1125 |
1125 |
|
1126 |
1126 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the value of
|
1127 |
1127 |
/// the given map multiplied from the left side with a constant value.
|
1128 |
1128 |
/// Its \c Key and \c Value are inherited from \c M.
|
1129 |
1129 |
/// It can also be used as write map if the \c / operator is defined
|
1130 |
1130 |
/// between \c Value and \c C and the given multiplier is not zero.
|
1131 |
1131 |
///
|
1132 |
1132 |
/// The simplest way of using this map is through the scaleWriteMap()
|
1133 |
1133 |
/// function.
|
1134 |
1134 |
///
|
1135 |
1135 |
/// \sa ScaleMap
|
1136 |
1136 |
template<typename M, typename C = typename M::Value>
|
1137 |
1137 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
1138 |
1138 |
M &_m;
|
1139 |
1139 |
C _v;
|
1140 |
1140 |
public:
|
1141 |
1141 |
///\e
|
1142 |
1142 |
typedef typename M::Key Key;
|
1143 |
1143 |
///\e
|
1144 |
1144 |
typedef typename M::Value Value;
|
1145 |
1145 |
|
1146 |
1146 |
/// Constructor
|
1147 |
1147 |
|
1148 |
1148 |
/// Constructor.
|
1149 |
1149 |
/// \param m The undelying map.
|
1150 |
1150 |
/// \param v The constant value.
|
1151 |
1151 |
ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
|
1152 |
1152 |
///\e
|
1153 |
1153 |
Value operator[](const Key &k) const { return _v*_m[k]; }
|
1154 |
1154 |
///\e
|
1155 |
1155 |
void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
|
1156 |
1156 |
};
|
1157 |
1157 |
|
1158 |
1158 |
/// Returns a \c ScaleMap class
|
1159 |
1159 |
|
1160 |
1160 |
/// This function just returns a \c ScaleMap class.
|
1161 |
1161 |
///
|
1162 |
1162 |
/// For example, if \c m is a map with \c double values and \c v is
|
1163 |
1163 |
/// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
|
1164 |
1164 |
/// <tt>v*m[x]</tt>.
|
1165 |
1165 |
///
|
1166 |
1166 |
/// \relates ScaleMap
|
1167 |
1167 |
template<typename M, typename C>
|
1168 |
1168 |
inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
|
1169 |
1169 |
return ScaleMap<M, C>(m,v);
|
1170 |
1170 |
}
|
1171 |
1171 |
|
1172 |
1172 |
/// Returns a \c ScaleWriteMap class
|
1173 |
1173 |
|
1174 |
1174 |
/// This function just returns a \c ScaleWriteMap class.
|
1175 |
1175 |
///
|
1176 |
1176 |
/// For example, if \c m is a map with \c double values and \c v is
|
1177 |
1177 |
/// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
|
1178 |
1178 |
/// <tt>v*m[x]</tt>.
|
1179 |
1179 |
/// Moreover it makes also possible to write the map.
|
1180 |
1180 |
///
|
1181 |
1181 |
/// \relates ScaleWriteMap
|
1182 |
1182 |
template<typename M, typename C>
|
1183 |
1183 |
inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
|
1184 |
1184 |
return ScaleWriteMap<M, C>(m,v);
|
1185 |
1185 |
}
|
1186 |
1186 |
|
1187 |
1187 |
|
1188 |
1188 |
/// Negative of a map
|
1189 |
1189 |
|
1190 |
1190 |
/// This \ref concepts::ReadMap "read-only map" returns the negative
|
1191 |
1191 |
/// of the values of the given map (using the unary \c - operator).
|
1192 |
1192 |
/// Its \c Key and \c Value are inherited from \c M.
|
1193 |
1193 |
///
|
1194 |
1194 |
/// If M::Value is \c int, \c double etc., then
|
1195 |
1195 |
/// \code
|
1196 |
1196 |
/// NegMap<M> neg(m);
|
1197 |
1197 |
/// \endcode
|
1198 |
1198 |
/// is equivalent to
|
1199 |
1199 |
/// \code
|
1200 |
1200 |
/// ScaleMap<M> neg(m,-1);
|
1201 |
1201 |
/// \endcode
|
1202 |
1202 |
///
|
1203 |
1203 |
/// The simplest way of using this map is through the negMap()
|
1204 |
1204 |
/// function.
|
1205 |
1205 |
///
|
1206 |
1206 |
/// \sa NegWriteMap
|
1207 |
1207 |
template<typename M>
|
1208 |
1208 |
class NegMap : public MapBase<typename M::Key, typename M::Value> {
|
1209 |
1209 |
const M& _m;
|
1210 |
1210 |
public:
|
1211 |
1211 |
///\e
|
1212 |
1212 |
typedef typename M::Key Key;
|
1213 |
1213 |
///\e
|
1214 |
1214 |
typedef typename M::Value Value;
|
1215 |
1215 |
|
1216 |
1216 |
/// Constructor
|
1217 |
1217 |
NegMap(const M &m) : _m(m) {}
|
1218 |
1218 |
///\e
|
1219 |
1219 |
Value operator[](const Key &k) const { return -_m[k]; }
|
1220 |
1220 |
};
|
1221 |
1221 |
|
1222 |
1222 |
/// Negative of a map (read-write version)
|
1223 |
1223 |
|
1224 |
1224 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the
|
1225 |
1225 |
/// negative of the values of the given map (using the unary \c -
|
1226 |
1226 |
/// operator).
|
1227 |
1227 |
/// Its \c Key and \c Value are inherited from \c M.
|
1228 |
1228 |
/// It makes also possible to write the map.
|
1229 |
1229 |
///
|
1230 |
1230 |
/// If M::Value is \c int, \c double etc., then
|
1231 |
1231 |
/// \code
|
1232 |
1232 |
/// NegWriteMap<M> neg(m);
|
1233 |
1233 |
/// \endcode
|
1234 |
1234 |
/// is equivalent to
|
1235 |
1235 |
/// \code
|
1236 |
1236 |
/// ScaleWriteMap<M> neg(m,-1);
|
1237 |
1237 |
/// \endcode
|
1238 |
1238 |
///
|
1239 |
1239 |
/// The simplest way of using this map is through the negWriteMap()
|
1240 |
1240 |
/// function.
|
1241 |
1241 |
///
|
1242 |
1242 |
/// \sa NegMap
|
1243 |
1243 |
template<typename M>
|
1244 |
1244 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
1245 |
1245 |
M &_m;
|
1246 |
1246 |
public:
|
1247 |
1247 |
///\e
|
1248 |
1248 |
typedef typename M::Key Key;
|
1249 |
1249 |
///\e
|
1250 |
1250 |
typedef typename M::Value Value;
|
1251 |
1251 |
|
1252 |
1252 |
/// Constructor
|
1253 |
1253 |
NegWriteMap(M &m) : _m(m) {}
|
1254 |
1254 |
///\e
|
1255 |
1255 |
Value operator[](const Key &k) const { return -_m[k]; }
|
1256 |
1256 |
///\e
|
1257 |
1257 |
void set(const Key &k, const Value &v) { _m.set(k, -v); }
|
1258 |
1258 |
};
|
1259 |
1259 |
|
1260 |
1260 |
/// Returns a \c NegMap class
|
1261 |
1261 |
|
1262 |
1262 |
/// This function just returns a \c NegMap class.
|
1263 |
1263 |
///
|
1264 |
1264 |
/// For example, if \c m is a map with \c double values, then
|
1265 |
1265 |
/// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
|
1266 |
1266 |
///
|
1267 |
1267 |
/// \relates NegMap
|
1268 |
1268 |
template <typename M>
|
1269 |
1269 |
inline NegMap<M> negMap(const M &m) {
|
1270 |
1270 |
return NegMap<M>(m);
|
1271 |
1271 |
}
|
1272 |
1272 |
|
1273 |
1273 |
/// Returns a \c NegWriteMap class
|
1274 |
1274 |
|
1275 |
1275 |
/// This function just returns a \c NegWriteMap class.
|
1276 |
1276 |
///
|
1277 |
1277 |
/// For example, if \c m is a map with \c double values, then
|
1278 |
1278 |
/// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
|
1279 |
1279 |
/// Moreover it makes also possible to write the map.
|
1280 |
1280 |
///
|
1281 |
1281 |
/// \relates NegWriteMap
|
1282 |
1282 |
template <typename M>
|
1283 |
1283 |
inline NegWriteMap<M> negWriteMap(M &m) {
|
1284 |
1284 |
return NegWriteMap<M>(m);
|
1285 |
1285 |
}
|
1286 |
1286 |
|
1287 |
1287 |
|
1288 |
1288 |
/// Absolute value of a map
|
1289 |
1289 |
|
1290 |
1290 |
/// This \ref concepts::ReadMap "read-only map" returns the absolute
|
1291 |
1291 |
/// value of the values of the given map.
|
1292 |
1292 |
/// Its \c Key and \c Value are inherited from \c M.
|
1293 |
1293 |
/// \c Value must be comparable to \c 0 and the unary \c -
|
1294 |
1294 |
/// operator must be defined for it, of course.
|
1295 |
1295 |
///
|
1296 |
1296 |
/// The simplest way of using this map is through the absMap()
|
1297 |
1297 |
/// function.
|
1298 |
1298 |
template<typename M>
|
1299 |
1299 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> {
|
1300 |
1300 |
const M &_m;
|
1301 |
1301 |
public:
|
1302 |
1302 |
///\e
|
1303 |
1303 |
typedef typename M::Key Key;
|
1304 |
1304 |
///\e
|
1305 |
1305 |
typedef typename M::Value Value;
|
1306 |
1306 |
|
1307 |
1307 |
/// Constructor
|
1308 |
1308 |
AbsMap(const M &m) : _m(m) {}
|
1309 |
1309 |
///\e
|
1310 |
1310 |
Value operator[](const Key &k) const {
|
1311 |
1311 |
Value tmp = _m[k];
|
1312 |
1312 |
return tmp >= 0 ? tmp : -tmp;
|
1313 |
1313 |
}
|
1314 |
1314 |
|
1315 |
1315 |
};
|
1316 |
1316 |
|
1317 |
1317 |
/// Returns an \c AbsMap class
|
1318 |
1318 |
|
1319 |
1319 |
/// This function just returns an \c AbsMap class.
|
1320 |
1320 |
///
|
1321 |
1321 |
/// For example, if \c m is a map with \c double values, then
|
1322 |
1322 |
/// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
|
1323 |
1323 |
/// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
|
1324 |
1324 |
/// negative.
|
1325 |
1325 |
///
|
1326 |
1326 |
/// \relates AbsMap
|
1327 |
1327 |
template<typename M>
|
1328 |
1328 |
inline AbsMap<M> absMap(const M &m) {
|
1329 |
1329 |
return AbsMap<M>(m);
|
1330 |
1330 |
}
|
1331 |
1331 |
|
1332 |
1332 |
/// @}
|
1333 |
1333 |
|
1334 |
1334 |
// Logical maps and map adaptors:
|
1335 |
1335 |
|
1336 |
1336 |
/// \addtogroup maps
|
1337 |
1337 |
/// @{
|
1338 |
1338 |
|
1339 |
1339 |
/// Constant \c true map.
|
1340 |
1340 |
|
1341 |
1341 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
|
1342 |
1342 |
/// each key.
|
1343 |
1343 |
///
|
1344 |
1344 |
/// Note that
|
1345 |
1345 |
/// \code
|
1346 |
1346 |
/// TrueMap<K> tm;
|
1347 |
1347 |
/// \endcode
|
1348 |
1348 |
/// is equivalent to
|
1349 |
1349 |
/// \code
|
1350 |
1350 |
/// ConstMap<K,bool> tm(true);
|
1351 |
1351 |
/// \endcode
|
1352 |
1352 |
///
|
1353 |
1353 |
/// \sa FalseMap
|
1354 |
1354 |
/// \sa ConstMap
|
1355 |
1355 |
template <typename K>
|
1356 |
1356 |
class TrueMap : public MapBase<K, bool> {
|
1357 |
1357 |
public:
|
1358 |
1358 |
///\e
|
1359 |
1359 |
typedef K Key;
|
1360 |
1360 |
///\e
|
1361 |
1361 |
typedef bool Value;
|
1362 |
1362 |
|
1363 |
1363 |
/// Gives back \c true.
|
1364 |
1364 |
Value operator[](const Key&) const { return true; }
|
1365 |
1365 |
};
|
1366 |
1366 |
|
1367 |
1367 |
/// Returns a \c TrueMap class
|
1368 |
1368 |
|
1369 |
1369 |
/// This function just returns a \c TrueMap class.
|
1370 |
1370 |
/// \relates TrueMap
|
1371 |
1371 |
template<typename K>
|
1372 |
1372 |
inline TrueMap<K> trueMap() {
|
1373 |
1373 |
return TrueMap<K>();
|
1374 |
1374 |
}
|
1375 |
1375 |
|
1376 |
1376 |
|
1377 |
1377 |
/// Constant \c false map.
|
1378 |
1378 |
|
1379 |
1379 |
/// This \ref concepts::ReadMap "read-only map" assigns \c false to
|
1380 |
1380 |
/// each key.
|
1381 |
1381 |
///
|
1382 |
1382 |
/// Note that
|
1383 |
1383 |
/// \code
|
1384 |
1384 |
/// FalseMap<K> fm;
|
1385 |
1385 |
/// \endcode
|
1386 |
1386 |
/// is equivalent to
|
1387 |
1387 |
/// \code
|
1388 |
1388 |
/// ConstMap<K,bool> fm(false);
|
1389 |
1389 |
/// \endcode
|
1390 |
1390 |
///
|
1391 |
1391 |
/// \sa TrueMap
|
1392 |
1392 |
/// \sa ConstMap
|
1393 |
1393 |
template <typename K>
|
1394 |
1394 |
class FalseMap : public MapBase<K, bool> {
|
1395 |
1395 |
public:
|
1396 |
1396 |
///\e
|
1397 |
1397 |
typedef K Key;
|
1398 |
1398 |
///\e
|
1399 |
1399 |
typedef bool Value;
|
1400 |
1400 |
|
1401 |
1401 |
/// Gives back \c false.
|
1402 |
1402 |
Value operator[](const Key&) const { return false; }
|
1403 |
1403 |
};
|
1404 |
1404 |
|
1405 |
1405 |
/// Returns a \c FalseMap class
|
1406 |
1406 |
|
1407 |
1407 |
/// This function just returns a \c FalseMap class.
|
1408 |
1408 |
/// \relates FalseMap
|
1409 |
1409 |
template<typename K>
|
1410 |
1410 |
inline FalseMap<K> falseMap() {
|
1411 |
1411 |
return FalseMap<K>();
|
1412 |
1412 |
}
|
1413 |
1413 |
|
1414 |
1414 |
/// @}
|
1415 |
1415 |
|
1416 |
1416 |
/// \addtogroup map_adaptors
|
1417 |
1417 |
/// @{
|
1418 |
1418 |
|
1419 |
1419 |
/// Logical 'and' of two maps
|
1420 |
1420 |
|
1421 |
1421 |
/// This \ref concepts::ReadMap "read-only map" returns the logical
|
1422 |
1422 |
/// 'and' of the values of the two given maps.
|
1423 |
1423 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
1424 |
1424 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
1425 |
1425 |
///
|
1426 |
1426 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
1427 |
1427 |
/// \code
|
1428 |
1428 |
/// AndMap<M1,M2> am(m1,m2);
|
1429 |
1429 |
/// \endcode
|
1430 |
1430 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
|
1431 |
1431 |
///
|
1432 |
1432 |
/// The simplest way of using this map is through the andMap()
|
1433 |
1433 |
/// function.
|
1434 |
1434 |
///
|
1435 |
1435 |
/// \sa OrMap
|
1436 |
1436 |
/// \sa NotMap, NotWriteMap
|
1437 |
1437 |
template<typename M1, typename M2>
|
1438 |
1438 |
class AndMap : public MapBase<typename M1::Key, bool> {
|
1439 |
1439 |
const M1 &_m1;
|
1440 |
1440 |
const M2 &_m2;
|
1441 |
1441 |
public:
|
1442 |
1442 |
///\e
|
1443 |
1443 |
typedef typename M1::Key Key;
|
1444 |
1444 |
///\e
|
1445 |
1445 |
typedef bool Value;
|
1446 |
1446 |
|
1447 |
1447 |
/// Constructor
|
1448 |
1448 |
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
1449 |
1449 |
///\e
|
1450 |
1450 |
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
|
1451 |
1451 |
};
|
1452 |
1452 |
|
1453 |
1453 |
/// Returns an \c AndMap class
|
1454 |
1454 |
|
1455 |
1455 |
/// This function just returns an \c AndMap class.
|
1456 |
1456 |
///
|
1457 |
1457 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values,
|
1458 |
1458 |
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to
|
1459 |
1459 |
/// <tt>m1[x]&&m2[x]</tt>.
|
1460 |
1460 |
///
|
1461 |
1461 |
/// \relates AndMap
|
1462 |
1462 |
template<typename M1, typename M2>
|
1463 |
1463 |
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
|
1464 |
1464 |
return AndMap<M1, M2>(m1,m2);
|
1465 |
1465 |
}
|
1466 |
1466 |
|
1467 |
1467 |
|
1468 |
1468 |
/// Logical 'or' of two maps
|
1469 |
1469 |
|
1470 |
1470 |
/// This \ref concepts::ReadMap "read-only map" returns the logical
|
1471 |
1471 |
/// 'or' of the values of the two given maps.
|
1472 |
1472 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
1473 |
1473 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
1474 |
1474 |
///
|
1475 |
1475 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
1476 |
1476 |
/// \code
|
1477 |
1477 |
/// OrMap<M1,M2> om(m1,m2);
|
1478 |
1478 |
/// \endcode
|
1479 |
1479 |
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
|
1480 |
1480 |
///
|
1481 |
1481 |
/// The simplest way of using this map is through the orMap()
|
1482 |
1482 |
/// function.
|
1483 |
1483 |
///
|
1484 |
1484 |
/// \sa AndMap
|
1485 |
1485 |
/// \sa NotMap, NotWriteMap
|
1486 |
1486 |
template<typename M1, typename M2>
|
1487 |
1487 |
class OrMap : public MapBase<typename M1::Key, bool> {
|
1488 |
1488 |
const M1 &_m1;
|
1489 |
1489 |
const M2 &_m2;
|
1490 |
1490 |
public:
|
1491 |
1491 |
///\e
|
1492 |
1492 |
typedef typename M1::Key Key;
|
1493 |
1493 |
///\e
|
1494 |
1494 |
typedef bool Value;
|
1495 |
1495 |
|
1496 |
1496 |
/// Constructor
|
1497 |
1497 |
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
1498 |
1498 |
///\e
|
1499 |
1499 |
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
|
1500 |
1500 |
};
|
1501 |
1501 |
|
1502 |
1502 |
/// Returns an \c OrMap class
|
1503 |
1503 |
|
1504 |
1504 |
/// This function just returns an \c OrMap class.
|
1505 |
1505 |
///
|
1506 |
1506 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values,
|
1507 |
1507 |
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to
|
1508 |
1508 |
/// <tt>m1[x]||m2[x]</tt>.
|
1509 |
1509 |
///
|
1510 |
1510 |
/// \relates OrMap
|
1511 |
1511 |
template<typename M1, typename M2>
|
1512 |
1512 |
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
|
1513 |
1513 |
return OrMap<M1, M2>(m1,m2);
|
1514 |
1514 |
}
|
1515 |
1515 |
|
1516 |
1516 |
|
1517 |
1517 |
/// Logical 'not' of a map
|
1518 |
1518 |
|
1519 |
1519 |
/// This \ref concepts::ReadMap "read-only map" returns the logical
|
1520 |
1520 |
/// negation of the values of the given map.
|
1521 |
1521 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool.
|
1522 |
1522 |
///
|
1523 |
1523 |
/// The simplest way of using this map is through the notMap()
|
1524 |
1524 |
/// function.
|
1525 |
1525 |
///
|
1526 |
1526 |
/// \sa NotWriteMap
|
1527 |
1527 |
template <typename M>
|
1528 |
1528 |
class NotMap : public MapBase<typename M::Key, bool> {
|
1529 |
1529 |
const M &_m;
|
1530 |
1530 |
public:
|
1531 |
1531 |
///\e
|
1532 |
1532 |
typedef typename M::Key Key;
|
1533 |
1533 |
///\e
|
1534 |
1534 |
typedef bool Value;
|
1535 |
1535 |
|
1536 |
1536 |
/// Constructor
|
1537 |
1537 |
NotMap(const M &m) : _m(m) {}
|
1538 |
1538 |
///\e
|
1539 |
1539 |
Value operator[](const Key &k) const { return !_m[k]; }
|
1540 |
1540 |
};
|
1541 |
1541 |
|
1542 |
1542 |
/// Logical 'not' of a map (read-write version)
|
1543 |
1543 |
|
1544 |
1544 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the
|
1545 |
1545 |
/// logical negation of the values of the given map.
|
1546 |
1546 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool.
|
1547 |
1547 |
/// It makes also possible to write the map. When a value is set,
|
1548 |
1548 |
/// the opposite value is set to the original map.
|
1549 |
1549 |
///
|
1550 |
1550 |
/// The simplest way of using this map is through the notWriteMap()
|
1551 |
1551 |
/// function.
|
1552 |
1552 |
///
|
1553 |
1553 |
/// \sa NotMap
|
1554 |
1554 |
template <typename M>
|
1555 |
1555 |
class NotWriteMap : public MapBase<typename M::Key, bool> {
|
1556 |
1556 |
M &_m;
|
1557 |
1557 |
public:
|
1558 |
1558 |
///\e
|
1559 |
1559 |
typedef typename M::Key Key;
|
1560 |
1560 |
///\e
|
1561 |
1561 |
typedef bool Value;
|
1562 |
1562 |
|
1563 |
1563 |
/// Constructor
|
1564 |
1564 |
NotWriteMap(M &m) : _m(m) {}
|
1565 |
1565 |
///\e
|
1566 |
1566 |
Value operator[](const Key &k) const { return !_m[k]; }
|
1567 |
1567 |
///\e
|
1568 |
1568 |
void set(const Key &k, bool v) { _m.set(k, !v); }
|
1569 |
1569 |
};
|
1570 |
1570 |
|
1571 |
1571 |
/// Returns a \c NotMap class
|
1572 |
1572 |
|
1573 |
1573 |
/// This function just returns a \c NotMap class.
|
1574 |
1574 |
///
|
1575 |
1575 |
/// For example, if \c m is a map with \c bool values, then
|
1576 |
1576 |
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
|
1577 |
1577 |
///
|
1578 |
1578 |
/// \relates NotMap
|
1579 |
1579 |
template <typename M>
|
1580 |
1580 |
inline NotMap<M> notMap(const M &m) {
|
1581 |
1581 |
return NotMap<M>(m);
|
1582 |
1582 |
}
|
1583 |
1583 |
|
1584 |
1584 |
/// Returns a \c NotWriteMap class
|
1585 |
1585 |
|
1586 |
1586 |
/// This function just returns a \c NotWriteMap class.
|
1587 |
1587 |
///
|
1588 |
1588 |
/// For example, if \c m is a map with \c bool values, then
|
1589 |
1589 |
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
|
1590 |
1590 |
/// Moreover it makes also possible to write the map.
|
1591 |
1591 |
///
|
1592 |
1592 |
/// \relates NotWriteMap
|
1593 |
1593 |
template <typename M>
|
1594 |
1594 |
inline NotWriteMap<M> notWriteMap(M &m) {
|
1595 |
1595 |
return NotWriteMap<M>(m);
|
1596 |
1596 |
}
|
1597 |
1597 |
|
1598 |
1598 |
|
1599 |
1599 |
/// Combination of two maps using the \c == operator
|
1600 |
1600 |
|
1601 |
1601 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
|
1602 |
1602 |
/// the keys for which the corresponding values of the two maps are
|
1603 |
1603 |
/// equal.
|
1604 |
1604 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
1605 |
1605 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
1606 |
1606 |
///
|
1607 |
1607 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
1608 |
1608 |
/// \code
|
1609 |
1609 |
/// EqualMap<M1,M2> em(m1,m2);
|
1610 |
1610 |
/// \endcode
|
1611 |
1611 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
|
1612 |
1612 |
///
|
1613 |
1613 |
/// The simplest way of using this map is through the equalMap()
|
1614 |
1614 |
/// function.
|
1615 |
1615 |
///
|
1616 |
1616 |
/// \sa LessMap
|
1617 |
1617 |
template<typename M1, typename M2>
|
1618 |
1618 |
class EqualMap : public MapBase<typename M1::Key, bool> {
|
1619 |
1619 |
const M1 &_m1;
|
1620 |
1620 |
const M2 &_m2;
|
1621 |
1621 |
public:
|
1622 |
1622 |
///\e
|
1623 |
1623 |
typedef typename M1::Key Key;
|
1624 |
1624 |
///\e
|
1625 |
1625 |
typedef bool Value;
|
1626 |
1626 |
|
1627 |
1627 |
/// Constructor
|
1628 |
1628 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
1629 |
1629 |
///\e
|
1630 |
1630 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
|
1631 |
1631 |
};
|
1632 |
1632 |
|
1633 |
1633 |
/// Returns an \c EqualMap class
|
1634 |
1634 |
|
1635 |
1635 |
/// This function just returns an \c EqualMap class.
|
1636 |
1636 |
///
|
1637 |
1637 |
/// For example, if \c m1 and \c m2 are maps with keys and values of
|
1638 |
1638 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
|
1639 |
1639 |
/// <tt>m1[x]==m2[x]</tt>.
|
1640 |
1640 |
///
|
1641 |
1641 |
/// \relates EqualMap
|
1642 |
1642 |
template<typename M1, typename M2>
|
1643 |
1643 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
|
1644 |
1644 |
return EqualMap<M1, M2>(m1,m2);
|
1645 |
1645 |
}
|
1646 |
1646 |
|
1647 |
1647 |
|
1648 |
1648 |
/// Combination of two maps using the \c < operator
|
1649 |
1649 |
|
1650 |
1650 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
|
1651 |
1651 |
/// the keys for which the corresponding value of the first map is
|
1652 |
1652 |
/// less then the value of the second map.
|
1653 |
1653 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is
|
1654 |
1654 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
|
1655 |
1655 |
///
|
1656 |
1656 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
1657 |
1657 |
/// \code
|
1658 |
1658 |
/// LessMap<M1,M2> lm(m1,m2);
|
1659 |
1659 |
/// \endcode
|
1660 |
1660 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
|
1661 |
1661 |
///
|
1662 |
1662 |
/// The simplest way of using this map is through the lessMap()
|
1663 |
1663 |
/// function.
|
1664 |
1664 |
///
|
1665 |
1665 |
/// \sa EqualMap
|
1666 |
1666 |
template<typename M1, typename M2>
|
1667 |
1667 |
class LessMap : public MapBase<typename M1::Key, bool> {
|
1668 |
1668 |
const M1 &_m1;
|
1669 |
1669 |
const M2 &_m2;
|
1670 |
1670 |
public:
|
1671 |
1671 |
///\e
|
1672 |
1672 |
typedef typename M1::Key Key;
|
1673 |
1673 |
///\e
|
1674 |
1674 |
typedef bool Value;
|
1675 |
1675 |
|
1676 |
1676 |
/// Constructor
|
1677 |
1677 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
1678 |
1678 |
///\e
|
1679 |
1679 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
|
1680 |
1680 |
};
|
1681 |
1681 |
|
1682 |
1682 |
/// Returns an \c LessMap class
|
1683 |
1683 |
|
1684 |
1684 |
/// This function just returns an \c LessMap class.
|
1685 |
1685 |
///
|
1686 |
1686 |
/// For example, if \c m1 and \c m2 are maps with keys and values of
|
1687 |
1687 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
|
1688 |
1688 |
/// <tt>m1[x]<m2[x]</tt>.
|
1689 |
1689 |
///
|
1690 |
1690 |
/// \relates LessMap
|
1691 |
1691 |
template<typename M1, typename M2>
|
1692 |
1692 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
|
1693 |
1693 |
return LessMap<M1, M2>(m1,m2);
|
1694 |
1694 |
}
|
1695 |
1695 |
|
1696 |
1696 |
namespace _maps_bits {
|
1697 |
1697 |
|
1698 |
1698 |
template <typename _Iterator, typename Enable = void>
|
1699 |
1699 |
struct IteratorTraits {
|
1700 |
1700 |
typedef typename std::iterator_traits<_Iterator>::value_type Value;
|
1701 |
1701 |
};
|
1702 |
1702 |
|
1703 |
1703 |
template <typename _Iterator>
|
1704 |
1704 |
struct IteratorTraits<_Iterator,
|
1705 |
1705 |
typename exists<typename _Iterator::container_type>::type>
|
1706 |
1706 |
{
|
1707 |
1707 |
typedef typename _Iterator::container_type::value_type Value;
|
1708 |
1708 |
};
|
1709 |
1709 |
|
1710 |
1710 |
}
|
1711 |
1711 |
|
1712 |
1712 |
/// @}
|
1713 |
1713 |
|
1714 |
1714 |
/// \addtogroup maps
|
1715 |
1715 |
/// @{
|
1716 |
1716 |
|
1717 |
1717 |
/// \brief Writable bool map for logging each \c true assigned element
|
1718 |
1718 |
///
|
1719 |
1719 |
/// A \ref concepts::WriteMap "writable" bool map for logging
|
1720 |
1720 |
/// each \c true assigned element, i.e it copies subsequently each
|
1721 |
1721 |
/// keys set to \c true to the given iterator.
|
1722 |
1722 |
/// The most important usage of it is storing certain nodes or arcs
|
1723 |
1723 |
/// that were marked \c true by an algorithm.
|
1724 |
1724 |
///
|
1725 |
1725 |
/// There are several algorithms that provide solutions through bool
|
1726 |
1726 |
/// maps and most of them assign \c true at most once for each key.
|
1727 |
1727 |
/// In these cases it is a natural request to store each \c true
|
1728 |
1728 |
/// assigned elements (in order of the assignment), which can be
|
1729 |
1729 |
/// easily done with LoggerBoolMap.
|
1730 |
1730 |
///
|
1731 |
1731 |
/// The simplest way of using this map is through the loggerBoolMap()
|
1732 |
1732 |
/// function.
|
1733 |
1733 |
///
|
1734 |
1734 |
/// \tparam IT The type of the iterator.
|
1735 |
1735 |
/// \tparam KEY The key type of the map. The default value set
|
1736 |
1736 |
/// according to the iterator type should work in most cases.
|
1737 |
1737 |
///
|
1738 |
1738 |
/// \note The container of the iterator must contain enough space
|
1739 |
1739 |
/// for the elements or the iterator should be an inserter iterator.
|
1740 |
1740 |
#ifdef DOXYGEN
|
1741 |
1741 |
template <typename IT, typename KEY>
|
1742 |
1742 |
#else
|
1743 |
1743 |
template <typename IT,
|
1744 |
1744 |
typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
|
1745 |
1745 |
#endif
|
1746 |
1746 |
class LoggerBoolMap : public MapBase<KEY, bool> {
|
1747 |
1747 |
public:
|
1748 |
1748 |
|
1749 |
1749 |
///\e
|
1750 |
1750 |
typedef KEY Key;
|
1751 |
1751 |
///\e
|
1752 |
1752 |
typedef bool Value;
|
1753 |
1753 |
///\e
|
1754 |
1754 |
typedef IT Iterator;
|
1755 |
1755 |
|
1756 |
1756 |
/// Constructor
|
1757 |
1757 |
LoggerBoolMap(Iterator it)
|
1758 |
1758 |
: _begin(it), _end(it) {}
|
1759 |
1759 |
|
1760 |
1760 |
/// Gives back the given iterator set for the first key
|
1761 |
1761 |
Iterator begin() const {
|
1762 |
1762 |
return _begin;
|
1763 |
1763 |
}
|
1764 |
1764 |
|
1765 |
1765 |
/// Gives back the the 'after the last' iterator
|
1766 |
1766 |
Iterator end() const {
|
1767 |
1767 |
return _end;
|
1768 |
1768 |
}
|
1769 |
1769 |
|
1770 |
1770 |
/// The set function of the map
|
1771 |
1771 |
void set(const Key& key, Value value) {
|
1772 |
1772 |
if (value) {
|
1773 |
1773 |
*_end++ = key;
|
1774 |
1774 |
}
|
1775 |
1775 |
}
|
1776 |
1776 |
|
1777 |
1777 |
private:
|
1778 |
1778 |
Iterator _begin;
|
1779 |
1779 |
Iterator _end;
|
1780 |
1780 |
};
|
1781 |
1781 |
|
1782 |
1782 |
/// Returns a \c LoggerBoolMap class
|
1783 |
1783 |
|
1784 |
1784 |
/// This function just returns a \c LoggerBoolMap class.
|
1785 |
1785 |
///
|
1786 |
1786 |
/// The most important usage of it is storing certain nodes or arcs
|
1787 |
1787 |
/// that were marked \c true by an algorithm.
|
1788 |
1788 |
/// For example, it makes easier to store the nodes in the processing
|
1789 |
1789 |
/// order of Dfs algorithm, as the following examples show.
|
1790 |
1790 |
/// \code
|
1791 |
1791 |
/// std::vector<Node> v;
|
1792 |
1792 |
/// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
|
1793 |
1793 |
/// \endcode
|
1794 |
1794 |
/// \code
|
1795 |
1795 |
/// std::vector<Node> v(countNodes(g));
|
1796 |
1796 |
/// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
|
1797 |
1797 |
/// \endcode
|
1798 |
1798 |
///
|
1799 |
1799 |
/// \note The container of the iterator must contain enough space
|
1800 |
1800 |
/// for the elements or the iterator should be an inserter iterator.
|
1801 |
1801 |
///
|
1802 |
1802 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
|
1803 |
1803 |
/// it cannot be used when a readable map is needed, for example, as
|
1804 |
1804 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
|
1805 |
1805 |
///
|
1806 |
1806 |
/// \relates LoggerBoolMap
|
1807 |
1807 |
template<typename Iterator>
|
1808 |
1808 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
|
1809 |
1809 |
return LoggerBoolMap<Iterator>(it);
|
1810 |
1810 |
}
|
1811 |
1811 |
|
1812 |
1812 |
/// @}
|
1813 |
1813 |
|
1814 |
1814 |
/// \addtogroup graph_maps
|
1815 |
1815 |
/// @{
|
1816 |
1816 |
|
1817 |
1817 |
/// \brief Provides an immutable and unique id for each item in a graph.
|
1818 |
1818 |
///
|
1819 |
1819 |
/// IdMap provides a unique and immutable id for each item of the
|
1820 |
1820 |
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
|
1821 |
1821 |
/// - \b unique: different items get different ids,
|
1822 |
1822 |
/// - \b immutable: the id of an item does not change (even if you
|
1823 |
1823 |
/// delete other nodes).
|
1824 |
1824 |
///
|
1825 |
1825 |
/// Using this map you get access (i.e. can read) the inner id values of
|
1826 |
1826 |
/// the items stored in the graph, which is returned by the \c id()
|
1827 |
1827 |
/// function of the graph. This map can be inverted with its member
|
1828 |
1828 |
/// class \c InverseMap or with the \c operator()() member.
|
1829 |
1829 |
///
|
1830 |
1830 |
/// \tparam GR The graph type.
|
1831 |
1831 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
1832 |
1832 |
/// \c GR::Edge).
|
1833 |
1833 |
///
|
1834 |
1834 |
/// \see RangeIdMap
|
1835 |
1835 |
template <typename GR, typename K>
|
1836 |
1836 |
class IdMap : public MapBase<K, int> {
|
1837 |
1837 |
public:
|
1838 |
1838 |
/// The graph type of IdMap.
|
1839 |
1839 |
typedef GR Graph;
|
1840 |
1840 |
typedef GR Digraph;
|
1841 |
1841 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge).
|
1842 |
1842 |
typedef K Item;
|
1843 |
1843 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge).
|
1844 |
1844 |
typedef K Key;
|
1845 |
1845 |
/// The value type of IdMap.
|
1846 |
1846 |
typedef int Value;
|
1847 |
1847 |
|
1848 |
1848 |
/// \brief Constructor.
|
1849 |
1849 |
///
|
1850 |
1850 |
/// Constructor of the map.
|
1851 |
1851 |
explicit IdMap(const Graph& graph) : _graph(&graph) {}
|
1852 |
1852 |
|
1853 |
1853 |
/// \brief Gives back the \e id of the item.
|
1854 |
1854 |
///
|
1855 |
1855 |
/// Gives back the immutable and unique \e id of the item.
|
1856 |
1856 |
int operator[](const Item& item) const { return _graph->id(item);}
|
1857 |
1857 |
|
1858 |
1858 |
/// \brief Gives back the \e item by its id.
|
1859 |
1859 |
///
|
1860 |
1860 |
/// Gives back the \e item by its id.
|
1861 |
1861 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
1862 |
1862 |
|
1863 |
1863 |
private:
|
1864 |
1864 |
const Graph* _graph;
|
1865 |
1865 |
|
1866 |
1866 |
public:
|
1867 |
1867 |
|
1868 |
1868 |
/// \brief The inverse map type of IdMap.
|
1869 |
1869 |
///
|
1870 |
1870 |
/// The inverse map type of IdMap. The subscript operator gives back
|
1871 |
1871 |
/// an item by its id.
|
1872 |
1872 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
|
1873 |
1873 |
/// \see inverse()
|
1874 |
1874 |
class InverseMap {
|
1875 |
1875 |
public:
|
1876 |
1876 |
|
1877 |
1877 |
/// \brief Constructor.
|
1878 |
1878 |
///
|
1879 |
1879 |
/// Constructor for creating an id-to-item map.
|
1880 |
1880 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
1881 |
1881 |
|
1882 |
1882 |
/// \brief Constructor.
|
1883 |
1883 |
///
|
1884 |
1884 |
/// Constructor for creating an id-to-item map.
|
1885 |
1885 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
1886 |
1886 |
|
1887 |
1887 |
/// \brief Gives back an item by its id.
|
1888 |
1888 |
///
|
1889 |
1889 |
/// Gives back an item by its id.
|
1890 |
1890 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
1891 |
1891 |
|
1892 |
1892 |
private:
|
1893 |
1893 |
const Graph* _graph;
|
1894 |
1894 |
};
|
1895 |
1895 |
|
1896 |
1896 |
/// \brief Gives back the inverse of the map.
|
1897 |
1897 |
///
|
1898 |
1898 |
/// Gives back the inverse of the IdMap.
|
1899 |
1899 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
1900 |
1900 |
};
|
1901 |
1901 |
|
1902 |
1902 |
/// \brief Returns an \c IdMap class.
|
1903 |
1903 |
///
|
1904 |
1904 |
/// This function just returns an \c IdMap class.
|
1905 |
1905 |
/// \relates IdMap
|
1906 |
1906 |
template <typename K, typename GR>
|
1907 |
1907 |
inline IdMap<GR, K> idMap(const GR& graph) {
|
1908 |
1908 |
return IdMap<GR, K>(graph);
|
1909 |
1909 |
}
|
1910 |
1910 |
|
1911 |
1911 |
/// \brief General cross reference graph map type.
|
1912 |
1912 |
|
1913 |
1913 |
/// This class provides simple invertable graph maps.
|
1914 |
1914 |
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
|
1915 |
1915 |
/// and if a key is set to a new value, then stores it in the inverse map.
|
1916 |
1916 |
/// The graph items can be accessed by their values either using
|
1917 |
1917 |
/// \c InverseMap or \c operator()(), and the values of the map can be
|
1918 |
1918 |
/// accessed with an STL compatible forward iterator (\c ValueIt).
|
1919 |
1919 |
///
|
1920 |
1920 |
/// This map is intended to be used when all associated values are
|
1921 |
1921 |
/// different (the map is actually invertable) or there are only a few
|
1922 |
1922 |
/// items with the same value.
|
1923 |
1923 |
/// Otherwise consider to use \c IterableValueMap, which is more
|
1924 |
1924 |
/// suitable and more efficient for such cases. It provides iterators
|
1925 |
1925 |
/// to traverse the items with the same associated value, but
|
1926 |
1926 |
/// it does not have \c InverseMap.
|
1927 |
1927 |
///
|
1928 |
1928 |
/// This type is not reference map, so it cannot be modified with
|
1929 |
1929 |
/// the subscript operator.
|
1930 |
1930 |
///
|
1931 |
1931 |
/// \tparam GR The graph type.
|
1932 |
1932 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
1933 |
1933 |
/// \c GR::Edge).
|
1934 |
1934 |
/// \tparam V The value type of the map.
|
1935 |
1935 |
///
|
1936 |
1936 |
/// \see IterableValueMap
|
1937 |
1937 |
template <typename GR, typename K, typename V>
|
1938 |
1938 |
class CrossRefMap
|
1939 |
1939 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
1940 |
1940 |
private:
|
1941 |
1941 |
|
1942 |
1942 |
typedef typename ItemSetTraits<GR, K>::
|
1943 |
1943 |
template Map<V>::Type Map;
|
1944 |
1944 |
|
1945 |
1945 |
typedef std::multimap<V, K> Container;
|
1946 |
1946 |
Container _inv_map;
|
1947 |
1947 |
|
1948 |
1948 |
public:
|
1949 |
1949 |
|
1950 |
1950 |
/// The graph type of CrossRefMap.
|
1951 |
1951 |
typedef GR Graph;
|
1952 |
1952 |
typedef GR Digraph;
|
1953 |
1953 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
1954 |
1954 |
typedef K Item;
|
1955 |
1955 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
1956 |
1956 |
typedef K Key;
|
1957 |
1957 |
/// The value type of CrossRefMap.
|
1958 |
1958 |
typedef V Value;
|
1959 |
1959 |
|
1960 |
1960 |
/// \brief Constructor.
|
1961 |
1961 |
///
|
1962 |
1962 |
/// Construct a new CrossRefMap for the given graph.
|
1963 |
1963 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
|
1964 |
1964 |
|
1965 |
1965 |
/// \brief Forward iterator for values.
|
1966 |
1966 |
///
|
1967 |
1967 |
/// This iterator is an STL compatible forward
|
1968 |
1968 |
/// iterator on the values of the map. The values can
|
1969 |
1969 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
|
1970 |
1970 |
/// They are considered with multiplicity, so each value is
|
1971 |
1971 |
/// traversed for each item it is assigned to.
|
1972 |
1972 |
class ValueIt
|
1973 |
1973 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
1974 |
1974 |
friend class CrossRefMap;
|
1975 |
1975 |
private:
|
1976 |
1976 |
ValueIt(typename Container::const_iterator _it)
|
1977 |
1977 |
: it(_it) {}
|
1978 |
1978 |
public:
|
1979 |
1979 |
|
1980 |
1980 |
/// Constructor
|
1981 |
1981 |
ValueIt() {}
|
1982 |
1982 |
|
1983 |
1983 |
/// \e
|
1984 |
1984 |
ValueIt& operator++() { ++it; return *this; }
|
1985 |
1985 |
/// \e
|
1986 |
1986 |
ValueIt operator++(int) {
|
1987 |
1987 |
ValueIt tmp(*this);
|
1988 |
1988 |
operator++();
|
1989 |
1989 |
return tmp;
|
1990 |
1990 |
}
|
1991 |
1991 |
|
1992 |
1992 |
/// \e
|
1993 |
1993 |
const Value& operator*() const { return it->first; }
|
1994 |
1994 |
/// \e
|
1995 |
1995 |
const Value* operator->() const { return &(it->first); }
|
1996 |
1996 |
|
1997 |
1997 |
/// \e
|
1998 |
1998 |
bool operator==(ValueIt jt) const { return it == jt.it; }
|
1999 |
1999 |
/// \e
|
2000 |
2000 |
bool operator!=(ValueIt jt) const { return it != jt.it; }
|
2001 |
2001 |
|
2002 |
2002 |
private:
|
2003 |
2003 |
typename Container::const_iterator it;
|
2004 |
2004 |
};
|
2005 |
2005 |
|
2006 |
2006 |
/// Alias for \c ValueIt
|
2007 |
2007 |
typedef ValueIt ValueIterator;
|
2008 |
2008 |
|
2009 |
2009 |
/// \brief Returns an iterator to the first value.
|
2010 |
2010 |
///
|
2011 |
2011 |
/// Returns an STL compatible iterator to the
|
2012 |
2012 |
/// first value of the map. The values of the
|
2013 |
2013 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
2014 |
2014 |
/// range.
|
2015 |
2015 |
ValueIt beginValue() const {
|
2016 |
2016 |
return ValueIt(_inv_map.begin());
|
2017 |
2017 |
}
|
2018 |
2018 |
|
2019 |
2019 |
/// \brief Returns an iterator after the last value.
|
2020 |
2020 |
///
|
2021 |
2021 |
/// Returns an STL compatible iterator after the
|
2022 |
2022 |
/// last value of the map. The values of the
|
2023 |
2023 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
2024 |
2024 |
/// range.
|
2025 |
2025 |
ValueIt endValue() const {
|
2026 |
2026 |
return ValueIt(_inv_map.end());
|
2027 |
2027 |
}
|
2028 |
2028 |
|
2029 |
2029 |
/// \brief Sets the value associated with the given key.
|
2030 |
2030 |
///
|
2031 |
2031 |
/// Sets the value associated with the given key.
|
2032 |
2032 |
void set(const Key& key, const Value& val) {
|
2033 |
2033 |
Value oldval = Map::operator[](key);
|
2034 |
2034 |
typename Container::iterator it;
|
2035 |
2035 |
for (it = _inv_map.equal_range(oldval).first;
|
2036 |
2036 |
it != _inv_map.equal_range(oldval).second; ++it) {
|
2037 |
2037 |
if (it->second == key) {
|
2038 |
2038 |
_inv_map.erase(it);
|
2039 |
2039 |
break;
|
2040 |
2040 |
}
|
2041 |
2041 |
}
|
2042 |
2042 |
_inv_map.insert(std::make_pair(val, key));
|
2043 |
2043 |
Map::set(key, val);
|
2044 |
2044 |
}
|
2045 |
2045 |
|
2046 |
2046 |
/// \brief Returns the value associated with the given key.
|
2047 |
2047 |
///
|
2048 |
2048 |
/// Returns the value associated with the given key.
|
2049 |
2049 |
typename MapTraits<Map>::ConstReturnValue
|
2050 |
2050 |
operator[](const Key& key) const {
|
2051 |
2051 |
return Map::operator[](key);
|
2052 |
2052 |
}
|
2053 |
2053 |
|
2054 |
2054 |
/// \brief Gives back an item by its value.
|
2055 |
2055 |
///
|
2056 |
2056 |
/// This function gives back an item that is assigned to
|
2057 |
2057 |
/// the given value or \c INVALID if no such item exists.
|
2058 |
2058 |
/// If there are more items with the same associated value,
|
2059 |
2059 |
/// only one of them is returned.
|
2060 |
2060 |
Key operator()(const Value& val) const {
|
2061 |
2061 |
typename Container::const_iterator it = _inv_map.find(val);
|
2062 |
2062 |
return it != _inv_map.end() ? it->second : INVALID;
|
2063 |
2063 |
}
|
2064 |
2064 |
|
2065 |
2065 |
/// \brief Returns the number of items with the given value.
|
2066 |
2066 |
///
|
2067 |
2067 |
/// This function returns the number of items with the given value
|
2068 |
2068 |
/// associated with it.
|
2069 |
2069 |
int count(const Value &val) const {
|
2070 |
2070 |
return _inv_map.count(val);
|
2071 |
2071 |
}
|
2072 |
2072 |
|
2073 |
2073 |
protected:
|
2074 |
2074 |
|
2075 |
2075 |
/// \brief Erase the key from the map and the inverse map.
|
2076 |
2076 |
///
|
2077 |
2077 |
/// Erase the key from the map and the inverse map. It is called by the
|
2078 |
2078 |
/// \c AlterationNotifier.
|
2079 |
2079 |
virtual void erase(const Key& key) {
|
2080 |
2080 |
Value val = Map::operator[](key);
|
2081 |
2081 |
typename Container::iterator it;
|
2082 |
2082 |
for (it = _inv_map.equal_range(val).first;
|
2083 |
2083 |
it != _inv_map.equal_range(val).second; ++it) {
|
2084 |
2084 |
if (it->second == key) {
|
2085 |
2085 |
_inv_map.erase(it);
|
2086 |
2086 |
break;
|
2087 |
2087 |
}
|
2088 |
2088 |
}
|
2089 |
2089 |
Map::erase(key);
|
2090 |
2090 |
}
|
2091 |
2091 |
|
2092 |
2092 |
/// \brief Erase more keys from the map and the inverse map.
|
2093 |
2093 |
///
|
2094 |
2094 |
/// Erase more keys from the map and the inverse map. It is called by the
|
2095 |
2095 |
/// \c AlterationNotifier.
|
2096 |
2096 |
virtual void erase(const std::vector<Key>& keys) {
|
2097 |
2097 |
for (int i = 0; i < int(keys.size()); ++i) {
|
2098 |
2098 |
Value val = Map::operator[](keys[i]);
|
2099 |
2099 |
typename Container::iterator it;
|
2100 |
2100 |
for (it = _inv_map.equal_range(val).first;
|
2101 |
2101 |
it != _inv_map.equal_range(val).second; ++it) {
|
2102 |
2102 |
if (it->second == keys[i]) {
|
2103 |
2103 |
_inv_map.erase(it);
|
2104 |
2104 |
break;
|
2105 |
2105 |
}
|
2106 |
2106 |
}
|
2107 |
2107 |
}
|
2108 |
2108 |
Map::erase(keys);
|
2109 |
2109 |
}
|
2110 |
2110 |
|
2111 |
2111 |
/// \brief Clear the keys from the map and the inverse map.
|
2112 |
2112 |
///
|
2113 |
2113 |
/// Clear the keys from the map and the inverse map. It is called by the
|
2114 |
2114 |
/// \c AlterationNotifier.
|
2115 |
2115 |
virtual void clear() {
|
2116 |
2116 |
_inv_map.clear();
|
2117 |
2117 |
Map::clear();
|
2118 |
2118 |
}
|
2119 |
2119 |
|
2120 |
2120 |
public:
|
2121 |
2121 |
|
2122 |
2122 |
/// \brief The inverse map type of CrossRefMap.
|
2123 |
2123 |
///
|
2124 |
2124 |
/// The inverse map type of CrossRefMap. The subscript operator gives
|
2125 |
2125 |
/// back an item by its value.
|
2126 |
2126 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
|
2127 |
2127 |
/// \see inverse()
|
2128 |
2128 |
class InverseMap {
|
2129 |
2129 |
public:
|
2130 |
2130 |
/// \brief Constructor
|
2131 |
2131 |
///
|
2132 |
2132 |
/// Constructor of the InverseMap.
|
2133 |
2133 |
explicit InverseMap(const CrossRefMap& inverted)
|
2134 |
2134 |
: _inverted(inverted) {}
|
2135 |
2135 |
|
2136 |
2136 |
/// The value type of the InverseMap.
|
2137 |
2137 |
typedef typename CrossRefMap::Key Value;
|
2138 |
2138 |
/// The key type of the InverseMap.
|
2139 |
2139 |
typedef typename CrossRefMap::Value Key;
|
2140 |
2140 |
|
2141 |
2141 |
/// \brief Subscript operator.
|
2142 |
2142 |
///
|
2143 |
2143 |
/// Subscript operator. It gives back an item
|
2144 |
2144 |
/// that is assigned to the given value or \c INVALID
|
2145 |
2145 |
/// if no such item exists.
|
2146 |
2146 |
Value operator[](const Key& key) const {
|
2147 |
2147 |
return _inverted(key);
|
2148 |
2148 |
}
|
2149 |
2149 |
|
2150 |
2150 |
private:
|
2151 |
2151 |
const CrossRefMap& _inverted;
|
2152 |
2152 |
};
|
2153 |
2153 |
|
2154 |
2154 |
/// \brief Gives back the inverse of the map.
|
2155 |
2155 |
///
|
2156 |
2156 |
/// Gives back the inverse of the CrossRefMap.
|
2157 |
2157 |
InverseMap inverse() const {
|
2158 |
2158 |
return InverseMap(*this);
|
2159 |
2159 |
}
|
2160 |
2160 |
|
2161 |
2161 |
};
|
2162 |
2162 |
|
2163 |
2163 |
/// \brief Provides continuous and unique id for the
|
2164 |
2164 |
/// items of a graph.
|
2165 |
2165 |
///
|
2166 |
2166 |
/// RangeIdMap provides a unique and continuous
|
2167 |
2167 |
/// id for each item of a given type (\c Node, \c Arc or
|
2168 |
2168 |
/// \c Edge) in a graph. This id is
|
2169 |
2169 |
/// - \b unique: different items get different ids,
|
2170 |
2170 |
/// - \b continuous: the range of the ids is the set of integers
|
2171 |
2171 |
/// between 0 and \c n-1, where \c n is the number of the items of
|
2172 |
2172 |
/// this type (\c Node, \c Arc or \c Edge).
|
2173 |
2173 |
/// - So, the ids can change when deleting an item of the same type.
|
2174 |
2174 |
///
|
2175 |
2175 |
/// Thus this id is not (necessarily) the same as what can get using
|
2176 |
2176 |
/// the \c id() function of the graph or \ref IdMap.
|
2177 |
2177 |
/// This map can be inverted with its member class \c InverseMap,
|
2178 |
2178 |
/// or with the \c operator()() member.
|
2179 |
2179 |
///
|
2180 |
2180 |
/// \tparam GR The graph type.
|
2181 |
2181 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
2182 |
2182 |
/// \c GR::Edge).
|
2183 |
2183 |
///
|
2184 |
2184 |
/// \see IdMap
|
2185 |
2185 |
template <typename GR, typename K>
|
2186 |
2186 |
class RangeIdMap
|
2187 |
2187 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
2188 |
2188 |
|
2189 |
2189 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
|
2190 |
2190 |
|
2191 |
2191 |
public:
|
2192 |
2192 |
/// The graph type of RangeIdMap.
|
2193 |
2193 |
typedef GR Graph;
|
2194 |
2194 |
typedef GR Digraph;
|
2195 |
2195 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
|
2196 |
2196 |
typedef K Item;
|
2197 |
2197 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
|
2198 |
2198 |
typedef K Key;
|
2199 |
2199 |
/// The value type of RangeIdMap.
|
2200 |
2200 |
typedef int Value;
|
2201 |
2201 |
|
2202 |
2202 |
/// \brief Constructor.
|
2203 |
2203 |
///
|
2204 |
2204 |
/// Constructor.
|
2205 |
2205 |
explicit RangeIdMap(const Graph& gr) : Map(gr) {
|
2206 |
2206 |
Item it;
|
2207 |
2207 |
const typename Map::Notifier* nf = Map::notifier();
|
2208 |
2208 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
2209 |
2209 |
Map::set(it, _inv_map.size());
|
2210 |
2210 |
_inv_map.push_back(it);
|
2211 |
2211 |
}
|
2212 |
2212 |
}
|
2213 |
2213 |
|
2214 |
2214 |
protected:
|
2215 |
2215 |
|
2216 |
2216 |
/// \brief Adds a new key to the map.
|
2217 |
2217 |
///
|
2218 |
2218 |
/// Add a new key to the map. It is called by the
|
2219 |
2219 |
/// \c AlterationNotifier.
|
2220 |
2220 |
virtual void add(const Item& item) {
|
2221 |
2221 |
Map::add(item);
|
2222 |
2222 |
Map::set(item, _inv_map.size());
|
2223 |
2223 |
_inv_map.push_back(item);
|
2224 |
2224 |
}
|
2225 |
2225 |
|
2226 |
2226 |
/// \brief Add more new keys to the map.
|
2227 |
2227 |
///
|
2228 |
2228 |
/// Add more new keys to the map. It is called by the
|
2229 |
2229 |
/// \c AlterationNotifier.
|
2230 |
2230 |
virtual void add(const std::vector<Item>& items) {
|
2231 |
2231 |
Map::add(items);
|
2232 |
2232 |
for (int i = 0; i < int(items.size()); ++i) {
|
2233 |
2233 |
Map::set(items[i], _inv_map.size());
|
2234 |
2234 |
_inv_map.push_back(items[i]);
|
2235 |
2235 |
}
|
2236 |
2236 |
}
|
2237 |
2237 |
|
2238 |
2238 |
/// \brief Erase the key from the map.
|
2239 |
2239 |
///
|
2240 |
2240 |
/// Erase the key from the map. It is called by the
|
2241 |
2241 |
/// \c AlterationNotifier.
|
2242 |
2242 |
virtual void erase(const Item& item) {
|
2243 |
2243 |
Map::set(_inv_map.back(), Map::operator[](item));
|
2244 |
2244 |
_inv_map[Map::operator[](item)] = _inv_map.back();
|
2245 |
2245 |
_inv_map.pop_back();
|
2246 |
2246 |
Map::erase(item);
|
2247 |
2247 |
}
|
2248 |
2248 |
|
2249 |
2249 |
/// \brief Erase more keys from the map.
|
2250 |
2250 |
///
|
2251 |
2251 |
/// Erase more keys from the map. It is called by the
|
2252 |
2252 |
/// \c AlterationNotifier.
|
2253 |
2253 |
virtual void erase(const std::vector<Item>& items) {
|
2254 |
2254 |
for (int i = 0; i < int(items.size()); ++i) {
|
2255 |
2255 |
Map::set(_inv_map.back(), Map::operator[](items[i]));
|
2256 |
2256 |
_inv_map[Map::operator[](items[i])] = _inv_map.back();
|
2257 |
2257 |
_inv_map.pop_back();
|
2258 |
2258 |
}
|
2259 |
2259 |
Map::erase(items);
|
2260 |
2260 |
}
|
2261 |
2261 |
|
2262 |
2262 |
/// \brief Build the unique map.
|
2263 |
2263 |
///
|
2264 |
2264 |
/// Build the unique map. It is called by the
|
2265 |
2265 |
/// \c AlterationNotifier.
|
2266 |
2266 |
virtual void build() {
|
2267 |
2267 |
Map::build();
|
2268 |
2268 |
Item it;
|
2269 |
2269 |
const typename Map::Notifier* nf = Map::notifier();
|
2270 |
2270 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
2271 |
2271 |
Map::set(it, _inv_map.size());
|
2272 |
2272 |
_inv_map.push_back(it);
|
2273 |
2273 |
}
|
2274 |
2274 |
}
|
2275 |
2275 |
|
2276 |
2276 |
/// \brief Clear the keys from the map.
|
2277 |
2277 |
///
|
2278 |
2278 |
/// Clear the keys from the map. It is called by the
|
2279 |
2279 |
/// \c AlterationNotifier.
|
2280 |
2280 |
virtual void clear() {
|
2281 |
2281 |
_inv_map.clear();
|
2282 |
2282 |
Map::clear();
|
2283 |
2283 |
}
|
2284 |
2284 |
|
2285 |
2285 |
public:
|
2286 |
2286 |
|
2287 |
2287 |
/// \brief Returns the maximal value plus one.
|
2288 |
2288 |
///
|
2289 |
2289 |
/// Returns the maximal value plus one in the map.
|
2290 |
2290 |
unsigned int size() const {
|
2291 |
2291 |
return _inv_map.size();
|
2292 |
2292 |
}
|
2293 |
2293 |
|
2294 |
2294 |
/// \brief Swaps the position of the two items in the map.
|
2295 |
2295 |
///
|
2296 |
2296 |
/// Swaps the position of the two items in the map.
|
2297 |
2297 |
void swap(const Item& p, const Item& q) {
|
2298 |
2298 |
int pi = Map::operator[](p);
|
2299 |
2299 |
int qi = Map::operator[](q);
|
2300 |
2300 |
Map::set(p, qi);
|
2301 |
2301 |
_inv_map[qi] = p;
|
2302 |
2302 |
Map::set(q, pi);
|
2303 |
2303 |
_inv_map[pi] = q;
|
2304 |
2304 |
}
|
2305 |
2305 |
|
2306 |
2306 |
/// \brief Gives back the \e range \e id of the item
|
2307 |
2307 |
///
|
2308 |
2308 |
/// Gives back the \e range \e id of the item.
|
2309 |
2309 |
int operator[](const Item& item) const {
|
2310 |
2310 |
return Map::operator[](item);
|
2311 |
2311 |
}
|
2312 |
2312 |
|
2313 |
2313 |
/// \brief Gives back the item belonging to a \e range \e id
|
2314 |
2314 |
///
|
2315 |
2315 |
/// Gives back the item belonging to the given \e range \e id.
|
2316 |
2316 |
Item operator()(int id) const {
|
2317 |
2317 |
return _inv_map[id];
|
2318 |
2318 |
}
|
2319 |
2319 |
|
2320 |
2320 |
private:
|
2321 |
2321 |
|
2322 |
2322 |
typedef std::vector<Item> Container;
|
2323 |
2323 |
Container _inv_map;
|
2324 |
2324 |
|
2325 |
2325 |
public:
|
2326 |
2326 |
|
2327 |
2327 |
/// \brief The inverse map type of RangeIdMap.
|
2328 |
2328 |
///
|
2329 |
2329 |
/// The inverse map type of RangeIdMap. The subscript operator gives
|
2330 |
2330 |
/// back an item by its \e range \e id.
|
2331 |
2331 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
|
2332 |
2332 |
class InverseMap {
|
2333 |
2333 |
public:
|
2334 |
2334 |
/// \brief Constructor
|
2335 |
2335 |
///
|
2336 |
2336 |
/// Constructor of the InverseMap.
|
2337 |
2337 |
explicit InverseMap(const RangeIdMap& inverted)
|
2338 |
2338 |
: _inverted(inverted) {}
|
2339 |
2339 |
|
2340 |
2340 |
|
2341 |
2341 |
/// The value type of the InverseMap.
|
2342 |
2342 |
typedef typename RangeIdMap::Key Value;
|
2343 |
2343 |
/// The key type of the InverseMap.
|
2344 |
2344 |
typedef typename RangeIdMap::Value Key;
|
2345 |
2345 |
|
2346 |
2346 |
/// \brief Subscript operator.
|
2347 |
2347 |
///
|
2348 |
2348 |
/// Subscript operator. It gives back the item
|
2349 |
2349 |
/// that the given \e range \e id currently belongs to.
|
2350 |
2350 |
Value operator[](const Key& key) const {
|
2351 |
2351 |
return _inverted(key);
|
2352 |
2352 |
}
|
2353 |
2353 |
|
2354 |
2354 |
/// \brief Size of the map.
|
2355 |
2355 |
///
|
2356 |
2356 |
/// Returns the size of the map.
|
2357 |
2357 |
unsigned int size() const {
|
2358 |
2358 |
return _inverted.size();
|
2359 |
2359 |
}
|
2360 |
2360 |
|
2361 |
2361 |
private:
|
2362 |
2362 |
const RangeIdMap& _inverted;
|
2363 |
2363 |
};
|
2364 |
2364 |
|
2365 |
2365 |
/// \brief Gives back the inverse of the map.
|
2366 |
2366 |
///
|
2367 |
2367 |
/// Gives back the inverse of the RangeIdMap.
|
2368 |
2368 |
const InverseMap inverse() const {
|
2369 |
2369 |
return InverseMap(*this);
|
2370 |
2370 |
}
|
2371 |
2371 |
};
|
2372 |
2372 |
|
2373 |
2373 |
/// \brief Returns a \c RangeIdMap class.
|
2374 |
2374 |
///
|
2375 |
2375 |
/// This function just returns an \c RangeIdMap class.
|
2376 |
2376 |
/// \relates RangeIdMap
|
2377 |
2377 |
template <typename K, typename GR>
|
2378 |
2378 |
inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
|
2379 |
2379 |
return RangeIdMap<GR, K>(graph);
|
2380 |
2380 |
}
|
2381 |
2381 |
|
2382 |
2382 |
/// \brief Dynamic iterable \c bool map.
|
2383 |
2383 |
///
|
2384 |
2384 |
/// This class provides a special graph map type which can store a
|
2385 |
2385 |
/// \c bool value for graph items (\c Node, \c Arc or \c Edge).
|
2386 |
2386 |
/// For both \c true and \c false values it is possible to iterate on
|
2387 |
2387 |
/// the keys mapped to the value.
|
2388 |
2388 |
///
|
2389 |
2389 |
/// This type is a reference map, so it can be modified with the
|
2390 |
2390 |
/// subscript operator.
|
2391 |
2391 |
///
|
2392 |
2392 |
/// \tparam GR The graph type.
|
2393 |
2393 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
2394 |
2394 |
/// \c GR::Edge).
|
2395 |
2395 |
///
|
2396 |
2396 |
/// \see IterableIntMap, IterableValueMap
|
2397 |
2397 |
/// \see CrossRefMap
|
2398 |
2398 |
template <typename GR, typename K>
|
2399 |
2399 |
class IterableBoolMap
|
2400 |
2400 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
2401 |
2401 |
private:
|
2402 |
2402 |
typedef GR Graph;
|
2403 |
2403 |
|
2404 |
2404 |
typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
|
2405 |
2405 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
|
2406 |
2406 |
|
2407 |
2407 |
std::vector<K> _array;
|
2408 |
2408 |
int _sep;
|
2409 |
2409 |
|
2410 |
2410 |
public:
|
2411 |
2411 |
|
2412 |
2412 |
/// Indicates that the map is reference map.
|
2413 |
2413 |
typedef True ReferenceMapTag;
|
2414 |
2414 |
|
2415 |
2415 |
/// The key type
|
2416 |
2416 |
typedef K Key;
|
2417 |
2417 |
/// The value type
|
2418 |
2418 |
typedef bool Value;
|
2419 |
2419 |
/// The const reference type.
|
2420 |
2420 |
typedef const Value& ConstReference;
|
2421 |
2421 |
|
2422 |
2422 |
private:
|
2423 |
2423 |
|
2424 |
2424 |
int position(const Key& key) const {
|
2425 |
2425 |
return Parent::operator[](key);
|
2426 |
2426 |
}
|
2427 |
2427 |
|
2428 |
2428 |
public:
|
2429 |
2429 |
|
2430 |
2430 |
/// \brief Reference to the value of the map.
|
2431 |
2431 |
///
|
2432 |
2432 |
/// This class is similar to the \c bool type. It can be converted to
|
2433 |
2433 |
/// \c bool and it provides the same operators.
|
2434 |
2434 |
class Reference {
|
2435 |
2435 |
friend class IterableBoolMap;
|
2436 |
2436 |
private:
|
2437 |
2437 |
Reference(IterableBoolMap& map, const Key& key)
|
2438 |
2438 |
: _key(key), _map(map) {}
|
2439 |
2439 |
public:
|
2440 |
2440 |
|
2441 |
2441 |
Reference& operator=(const Reference& value) {
|
2442 |
2442 |
_map.set(_key, static_cast<bool>(value));
|
2443 |
2443 |
return *this;
|
2444 |
2444 |
}
|
2445 |
2445 |
|
2446 |
2446 |
operator bool() const {
|
2447 |
2447 |
return static_cast<const IterableBoolMap&>(_map)[_key];
|
2448 |
2448 |
}
|
2449 |
2449 |
|
2450 |
2450 |
Reference& operator=(bool value) {
|
2451 |
2451 |
_map.set(_key, value);
|
2452 |
2452 |
return *this;
|
2453 |
2453 |
}
|
2454 |
2454 |
Reference& operator&=(bool value) {
|
2455 |
2455 |
_map.set(_key, _map[_key] & value);
|
2456 |
2456 |
return *this;
|
2457 |
2457 |
}
|
2458 |
2458 |
Reference& operator|=(bool value) {
|
2459 |
2459 |
_map.set(_key, _map[_key] | value);
|
2460 |
2460 |
return *this;
|
2461 |
2461 |
}
|
2462 |
2462 |
Reference& operator^=(bool value) {
|
2463 |
2463 |
_map.set(_key, _map[_key] ^ value);
|
2464 |
2464 |
return *this;
|
2465 |
2465 |
}
|
2466 |
2466 |
private:
|
2467 |
2467 |
Key _key;
|
2468 |
2468 |
IterableBoolMap& _map;
|
2469 |
2469 |
};
|
2470 |
2470 |
|
2471 |
2471 |
/// \brief Constructor of the map with a default value.
|
2472 |
2472 |
///
|
2473 |
2473 |
/// Constructor of the map with a default value.
|
2474 |
2474 |
explicit IterableBoolMap(const Graph& graph, bool def = false)
|
2475 |
2475 |
: Parent(graph) {
|
2476 |
2476 |
typename Parent::Notifier* nf = Parent::notifier();
|
2477 |
2477 |
Key it;
|
2478 |
2478 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
2479 |
2479 |
Parent::set(it, _array.size());
|
2480 |
2480 |
_array.push_back(it);
|
2481 |
2481 |
}
|
2482 |
2482 |
_sep = (def ? _array.size() : 0);
|
2483 |
2483 |
}
|
2484 |
2484 |
|
2485 |
2485 |
/// \brief Const subscript operator of the map.
|
2486 |
2486 |
///
|
2487 |
2487 |
/// Const subscript operator of the map.
|
2488 |
2488 |
bool operator[](const Key& key) const {
|
2489 |
2489 |
return position(key) < _sep;
|
2490 |
2490 |
}
|
2491 |
2491 |
|
2492 |
2492 |
/// \brief Subscript operator of the map.
|
2493 |
2493 |
///
|
2494 |
2494 |
/// Subscript operator of the map.
|
2495 |
2495 |
Reference operator[](const Key& key) {
|
2496 |
2496 |
return Reference(*this, key);
|
2497 |
2497 |
}
|
2498 |
2498 |
|
2499 |
2499 |
/// \brief Set operation of the map.
|
2500 |
2500 |
///
|
2501 |
2501 |
/// Set operation of the map.
|
2502 |
2502 |
void set(const Key& key, bool value) {
|
2503 |
2503 |
int pos = position(key);
|
2504 |
2504 |
if (value) {
|
2505 |
2505 |
if (pos < _sep) return;
|
2506 |
2506 |
Key tmp = _array[_sep];
|
2507 |
2507 |
_array[_sep] = key;
|
2508 |
2508 |
Parent::set(key, _sep);
|
2509 |
2509 |
_array[pos] = tmp;
|
2510 |
2510 |
Parent::set(tmp, pos);
|
2511 |
2511 |
++_sep;
|
2512 |
2512 |
} else {
|
2513 |
2513 |
if (pos >= _sep) return;
|
2514 |
2514 |
--_sep;
|
2515 |
2515 |
Key tmp = _array[_sep];
|
2516 |
2516 |
_array[_sep] = key;
|
2517 |
2517 |
Parent::set(key, _sep);
|
2518 |
2518 |
_array[pos] = tmp;
|
2519 |
2519 |
Parent::set(tmp, pos);
|
2520 |
2520 |
}
|
2521 |
2521 |
}
|
2522 |
2522 |
|
2523 |
2523 |
/// \brief Set all items.
|
2524 |
2524 |
///
|
2525 |
2525 |
/// Set all items in the map.
|
2526 |
2526 |
/// \note Constant time operation.
|
2527 |
2527 |
void setAll(bool value) {
|
2528 |
2528 |
_sep = (value ? _array.size() : 0);
|
2529 |
2529 |
}
|
2530 |
2530 |
|
2531 |
2531 |
/// \brief Returns the number of the keys mapped to \c true.
|
2532 |
2532 |
///
|
2533 |
2533 |
/// Returns the number of the keys mapped to \c true.
|
2534 |
2534 |
int trueNum() const {
|
2535 |
2535 |
return _sep;
|
2536 |
2536 |
}
|
2537 |
2537 |
|
2538 |
2538 |
/// \brief Returns the number of the keys mapped to \c false.
|
2539 |
2539 |
///
|
2540 |
2540 |
/// Returns the number of the keys mapped to \c false.
|
2541 |
2541 |
int falseNum() const {
|
2542 |
2542 |
return _array.size() - _sep;
|
2543 |
2543 |
}
|
2544 |
2544 |
|
2545 |
2545 |
/// \brief Iterator for the keys mapped to \c true.
|
2546 |
2546 |
///
|
2547 |
2547 |
/// Iterator for the keys mapped to \c true. It works
|
2548 |
2548 |
/// like a graph item iterator, it can be converted to
|
2549 |
2549 |
/// the key type of the map, incremented with \c ++ operator, and
|
2550 |
2550 |
/// if the iterator leaves the last valid key, it will be equal to
|
2551 |
2551 |
/// \c INVALID.
|
2552 |
2552 |
class TrueIt : public Key {
|
2553 |
2553 |
public:
|
2554 |
2554 |
typedef Key Parent;
|
2555 |
2555 |
|
2556 |
2556 |
/// \brief Creates an iterator.
|
2557 |
2557 |
///
|
2558 |
2558 |
/// Creates an iterator. It iterates on the
|
2559 |
2559 |
/// keys mapped to \c true.
|
2560 |
2560 |
/// \param map The IterableBoolMap.
|
2561 |
2561 |
explicit TrueIt(const IterableBoolMap& map)
|
2562 |
2562 |
: Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
|
2563 |
2563 |
_map(&map) {}
|
2564 |
2564 |
|
2565 |
2565 |
/// \brief Invalid constructor \& conversion.
|
2566 |
2566 |
///
|
2567 |
2567 |
/// This constructor initializes the iterator to be invalid.
|
2568 |
2568 |
/// \sa Invalid for more details.
|
2569 |
2569 |
TrueIt(Invalid) : Parent(INVALID), _map(0) {}
|
2570 |
2570 |
|
2571 |
2571 |
/// \brief Increment operator.
|
2572 |
2572 |
///
|
2573 |
2573 |
/// Increment operator.
|
2574 |
2574 |
TrueIt& operator++() {
|
2575 |
2575 |
int pos = _map->position(*this);
|
2576 |
2576 |
Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
|
2577 |
2577 |
return *this;
|
2578 |
2578 |
}
|
2579 |
2579 |
|
2580 |
2580 |
private:
|
2581 |
2581 |
const IterableBoolMap* _map;
|
2582 |
2582 |
};
|
2583 |
2583 |
|
2584 |
2584 |
/// \brief Iterator for the keys mapped to \c false.
|
2585 |
2585 |
///
|
2586 |
2586 |
/// Iterator for the keys mapped to \c false. It works
|
2587 |
2587 |
/// like a graph item iterator, it can be converted to
|
2588 |
2588 |
/// the key type of the map, incremented with \c ++ operator, and
|
2589 |
2589 |
/// if the iterator leaves the last valid key, it will be equal to
|
2590 |
2590 |
/// \c INVALID.
|
2591 |
2591 |
class FalseIt : public Key {
|
2592 |
2592 |
public:
|
2593 |
2593 |
typedef Key Parent;
|
2594 |
2594 |
|
2595 |
2595 |
/// \brief Creates an iterator.
|
2596 |
2596 |
///
|
2597 |
2597 |
/// Creates an iterator. It iterates on the
|
2598 |
2598 |
/// keys mapped to \c false.
|
2599 |
2599 |
/// \param map The IterableBoolMap.
|
2600 |
2600 |
explicit FalseIt(const IterableBoolMap& map)
|
2601 |
2601 |
: Parent(map._sep < int(map._array.size()) ?
|
2602 |
2602 |
map._array.back() : INVALID), _map(&map) {}
|
2603 |
2603 |
|
2604 |
2604 |
/// \brief Invalid constructor \& conversion.
|
2605 |
2605 |
///
|
2606 |
2606 |
/// This constructor initializes the iterator to be invalid.
|
2607 |
2607 |
/// \sa Invalid for more details.
|
2608 |
2608 |
FalseIt(Invalid) : Parent(INVALID), _map(0) {}
|
2609 |
2609 |
|
2610 |
2610 |
/// \brief Increment operator.
|
2611 |
2611 |
///
|
2612 |
2612 |
/// Increment operator.
|
2613 |
2613 |
FalseIt& operator++() {
|
2614 |
2614 |
int pos = _map->position(*this);
|
2615 |
2615 |
Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
|
2616 |
2616 |
return *this;
|
2617 |
2617 |
}
|
2618 |
2618 |
|
2619 |
2619 |
private:
|
2620 |
2620 |
const IterableBoolMap* _map;
|
2621 |
2621 |
};
|
2622 |
2622 |
|
2623 |
2623 |
/// \brief Iterator for the keys mapped to a given value.
|
2624 |
2624 |
///
|
2625 |
2625 |
/// Iterator for the keys mapped to a given value. It works
|
2626 |
2626 |
/// like a graph item iterator, it can be converted to
|
2627 |
2627 |
/// the key type of the map, incremented with \c ++ operator, and
|
2628 |
2628 |
/// if the iterator leaves the last valid key, it will be equal to
|
2629 |
2629 |
/// \c INVALID.
|
2630 |
2630 |
class ItemIt : public Key {
|
2631 |
2631 |
public:
|
2632 |
2632 |
typedef Key Parent;
|
2633 |
2633 |
|
2634 |
2634 |
/// \brief Creates an iterator with a value.
|
2635 |
2635 |
///
|
2636 |
2636 |
/// Creates an iterator with a value. It iterates on the
|
2637 |
2637 |
/// keys mapped to the given value.
|
2638 |
2638 |
/// \param map The IterableBoolMap.
|
2639 |
2639 |
/// \param value The value.
|
2640 |
2640 |
ItemIt(const IterableBoolMap& map, bool value)
|
2641 |
2641 |
: Parent(value ?
|
2642 |
2642 |
(map._sep > 0 ?
|
2643 |
2643 |
map._array[map._sep - 1] : INVALID) :
|
2644 |
2644 |
(map._sep < int(map._array.size()) ?
|
2645 |
2645 |
map._array.back() : INVALID)), _map(&map) {}
|
2646 |
2646 |
|
2647 |
2647 |
/// \brief Invalid constructor \& conversion.
|
2648 |
2648 |
///
|
2649 |
2649 |
/// This constructor initializes the iterator to be invalid.
|
2650 |
2650 |
/// \sa Invalid for more details.
|
2651 |
2651 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
2652 |
2652 |
|
2653 |
2653 |
/// \brief Increment operator.
|
2654 |
2654 |
///
|
2655 |
2655 |
/// Increment operator.
|
2656 |
2656 |
ItemIt& operator++() {
|
2657 |
2657 |
int pos = _map->position(*this);
|
2658 |
2658 |
int _sep = pos >= _map->_sep ? _map->_sep : 0;
|
2659 |
2659 |
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
|
2660 |
2660 |
return *this;
|
2661 |
2661 |
}
|
2662 |
2662 |
|
2663 |
2663 |
private:
|
2664 |
2664 |
const IterableBoolMap* _map;
|
2665 |
2665 |
};
|
2666 |
2666 |
|
2667 |
2667 |
protected:
|
2668 |
2668 |
|
2669 |
2669 |
virtual void add(const Key& key) {
|
2670 |
2670 |
Parent::add(key);
|
2671 |
2671 |
Parent::set(key, _array.size());
|
2672 |
2672 |
_array.push_back(key);
|
2673 |
2673 |
}
|
2674 |
2674 |
|
2675 |
2675 |
virtual void add(const std::vector<Key>& keys) {
|
2676 |
2676 |
Parent::add(keys);
|
2677 |
2677 |
for (int i = 0; i < int(keys.size()); ++i) {
|
2678 |
2678 |
Parent::set(keys[i], _array.size());
|
2679 |
2679 |
_array.push_back(keys[i]);
|
2680 |
2680 |
}
|
2681 |
2681 |
}
|
2682 |
2682 |
|
2683 |
2683 |
virtual void erase(const Key& key) {
|
2684 |
2684 |
int pos = position(key);
|
2685 |
2685 |
if (pos < _sep) {
|
2686 |
2686 |
--_sep;
|
2687 |
2687 |
Parent::set(_array[_sep], pos);
|
2688 |
2688 |
_array[pos] = _array[_sep];
|
2689 |
2689 |
Parent::set(_array.back(), _sep);
|
2690 |
2690 |
_array[_sep] = _array.back();
|
2691 |
2691 |
_array.pop_back();
|
2692 |
2692 |
} else {
|
2693 |
2693 |
Parent::set(_array.back(), pos);
|
2694 |
2694 |
_array[pos] = _array.back();
|
2695 |
2695 |
_array.pop_back();
|
2696 |
2696 |
}
|
2697 |
2697 |
Parent::erase(key);
|
2698 |
2698 |
}
|
2699 |
2699 |
|
2700 |
2700 |
virtual void erase(const std::vector<Key>& keys) {
|
2701 |
2701 |
for (int i = 0; i < int(keys.size()); ++i) {
|
2702 |
2702 |
int pos = position(keys[i]);
|
2703 |
2703 |
if (pos < _sep) {
|
2704 |
2704 |
--_sep;
|
2705 |
2705 |
Parent::set(_array[_sep], pos);
|
2706 |
2706 |
_array[pos] = _array[_sep];
|
2707 |
2707 |
Parent::set(_array.back(), _sep);
|
2708 |
2708 |
_array[_sep] = _array.back();
|
2709 |
2709 |
_array.pop_back();
|
2710 |
2710 |
} else {
|
2711 |
2711 |
Parent::set(_array.back(), pos);
|
2712 |
2712 |
_array[pos] = _array.back();
|
2713 |
2713 |
_array.pop_back();
|
2714 |
2714 |
}
|
2715 |
2715 |
}
|
2716 |
2716 |
Parent::erase(keys);
|
2717 |
2717 |
}
|
2718 |
2718 |
|
2719 |
2719 |
virtual void build() {
|
2720 |
2720 |
Parent::build();
|
2721 |
2721 |
typename Parent::Notifier* nf = Parent::notifier();
|
2722 |
2722 |
Key it;
|
2723 |
2723 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
2724 |
2724 |
Parent::set(it, _array.size());
|
2725 |
2725 |
_array.push_back(it);
|
2726 |
2726 |
}
|
2727 |
2727 |
_sep = 0;
|
2728 |
2728 |
}
|
2729 |
2729 |
|
2730 |
2730 |
virtual void clear() {
|
2731 |
2731 |
_array.clear();
|
2732 |
2732 |
_sep = 0;
|
2733 |
2733 |
Parent::clear();
|
2734 |
2734 |
}
|
2735 |
2735 |
|
2736 |
2736 |
};
|
2737 |
2737 |
|
2738 |
2738 |
|
2739 |
2739 |
namespace _maps_bits {
|
2740 |
2740 |
template <typename Item>
|
2741 |
2741 |
struct IterableIntMapNode {
|
2742 |
2742 |
IterableIntMapNode() : value(-1) {}
|
2743 |
2743 |
IterableIntMapNode(int _value) : value(_value) {}
|
2744 |
2744 |
Item prev, next;
|
2745 |
2745 |
int value;
|
2746 |
2746 |
};
|
2747 |
2747 |
}
|
2748 |
2748 |
|
2749 |
2749 |
/// \brief Dynamic iterable integer map.
|
2750 |
2750 |
///
|
2751 |
2751 |
/// This class provides a special graph map type which can store an
|
2752 |
2752 |
/// integer value for graph items (\c Node, \c Arc or \c Edge).
|
2753 |
2753 |
/// For each non-negative value it is possible to iterate on the keys
|
2754 |
2754 |
/// mapped to the value.
|
2755 |
2755 |
///
|
2756 |
2756 |
/// This map is intended to be used with small integer values, for which
|
2757 |
2757 |
/// it is efficient, and supports iteration only for non-negative values.
|
2758 |
2758 |
/// If you need large values and/or iteration for negative integers,
|
2759 |
2759 |
/// consider to use \ref IterableValueMap instead.
|
2760 |
2760 |
///
|
2761 |
2761 |
/// This type is a reference map, so it can be modified with the
|
2762 |
2762 |
/// subscript operator.
|
2763 |
2763 |
///
|
2764 |
2764 |
/// \note The size of the data structure depends on the largest
|
2765 |
2765 |
/// value in the map.
|
2766 |
2766 |
///
|
2767 |
2767 |
/// \tparam GR The graph type.
|
2768 |
2768 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
2769 |
2769 |
/// \c GR::Edge).
|
2770 |
2770 |
///
|
2771 |
2771 |
/// \see IterableBoolMap, IterableValueMap
|
2772 |
2772 |
/// \see CrossRefMap
|
2773 |
2773 |
template <typename GR, typename K>
|
2774 |
2774 |
class IterableIntMap
|
2775 |
2775 |
: protected ItemSetTraits<GR, K>::
|
2776 |
2776 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type {
|
2777 |
2777 |
public:
|
2778 |
2778 |
typedef typename ItemSetTraits<GR, K>::
|
2779 |
2779 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
|
2780 |
2780 |
|
2781 |
2781 |
/// The key type
|
2782 |
2782 |
typedef K Key;
|
2783 |
2783 |
/// The value type
|
2784 |
2784 |
typedef int Value;
|
2785 |
2785 |
/// The graph type
|
2786 |
2786 |
typedef GR Graph;
|
2787 |
2787 |
|
2788 |
2788 |
/// \brief Constructor of the map.
|
2789 |
2789 |
///
|
2790 |
2790 |
/// Constructor of the map. It sets all values to -1.
|
2791 |
2791 |
explicit IterableIntMap(const Graph& graph)
|
2792 |
2792 |
: Parent(graph) {}
|
2793 |
2793 |
|
2794 |
2794 |
/// \brief Constructor of the map with a given value.
|
2795 |
2795 |
///
|
2796 |
2796 |
/// Constructor of the map with a given value.
|
2797 |
2797 |
explicit IterableIntMap(const Graph& graph, int value)
|
2798 |
2798 |
: Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
|
2799 |
2799 |
if (value >= 0) {
|
2800 |
2800 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
2801 |
2801 |
lace(it);
|
2802 |
2802 |
}
|
2803 |
2803 |
}
|
2804 |
2804 |
}
|
2805 |
2805 |
|
2806 |
2806 |
private:
|
2807 |
2807 |
|
2808 |
2808 |
void unlace(const Key& key) {
|
2809 |
2809 |
typename Parent::Value& node = Parent::operator[](key);
|
2810 |
2810 |
if (node.value < 0) return;
|
2811 |
2811 |
if (node.prev != INVALID) {
|
2812 |
2812 |
Parent::operator[](node.prev).next = node.next;
|
2813 |
2813 |
} else {
|
2814 |
2814 |
_first[node.value] = node.next;
|
2815 |
2815 |
}
|
2816 |
2816 |
if (node.next != INVALID) {
|
2817 |
2817 |
Parent::operator[](node.next).prev = node.prev;
|
2818 |
2818 |
}
|
2819 |
2819 |
while (!_first.empty() && _first.back() == INVALID) {
|
2820 |
2820 |
_first.pop_back();
|
2821 |
2821 |
}
|
2822 |
2822 |
}
|
2823 |
2823 |
|
2824 |
2824 |
void lace(const Key& key) {
|
2825 |
2825 |
typename Parent::Value& node = Parent::operator[](key);
|
2826 |
2826 |
if (node.value < 0) return;
|
2827 |
2827 |
if (node.value >= int(_first.size())) {
|
2828 |
2828 |
_first.resize(node.value + 1, INVALID);
|
2829 |
2829 |
}
|
2830 |
2830 |
node.prev = INVALID;
|
2831 |
2831 |
node.next = _first[node.value];
|
2832 |
2832 |
if (node.next != INVALID) {
|
2833 |
2833 |
Parent::operator[](node.next).prev = key;
|
2834 |
2834 |
}
|
2835 |
2835 |
_first[node.value] = key;
|
2836 |
2836 |
}
|
2837 |
2837 |
|
2838 |
2838 |
public:
|
2839 |
2839 |
|
2840 |
2840 |
/// Indicates that the map is reference map.
|
2841 |
2841 |
typedef True ReferenceMapTag;
|
2842 |
2842 |
|
2843 |
2843 |
/// \brief Reference to the value of the map.
|
2844 |
2844 |
///
|
2845 |
2845 |
/// This class is similar to the \c int type. It can
|
2846 |
2846 |
/// be converted to \c int and it has the same operators.
|
2847 |
2847 |
class Reference {
|
2848 |
2848 |
friend class IterableIntMap;
|
2849 |
2849 |
private:
|
2850 |
2850 |
Reference(IterableIntMap& map, const Key& key)
|
2851 |
2851 |
: _key(key), _map(map) {}
|
2852 |
2852 |
public:
|
2853 |
2853 |
|
2854 |
2854 |
Reference& operator=(const Reference& value) {
|
2855 |
2855 |
_map.set(_key, static_cast<const int&>(value));
|
2856 |
2856 |
return *this;
|
2857 |
2857 |
}
|
2858 |
2858 |
|
2859 |
2859 |
operator const int&() const {
|
2860 |
2860 |
return static_cast<const IterableIntMap&>(_map)[_key];
|
2861 |
2861 |
}
|
2862 |
2862 |
|
2863 |
2863 |
Reference& operator=(int value) {
|
2864 |
2864 |
_map.set(_key, value);
|
2865 |
2865 |
return *this;
|
2866 |
2866 |
}
|
2867 |
2867 |
Reference& operator++() {
|
2868 |
2868 |
_map.set(_key, _map[_key] + 1);
|
2869 |
2869 |
return *this;
|
2870 |
2870 |
}
|
2871 |
2871 |
int operator++(int) {
|
2872 |
2872 |
int value = _map[_key];
|
2873 |
2873 |
_map.set(_key, value + 1);
|
2874 |
2874 |
return value;
|
2875 |
2875 |
}
|
2876 |
2876 |
Reference& operator--() {
|
2877 |
2877 |
_map.set(_key, _map[_key] - 1);
|
2878 |
2878 |
return *this;
|
2879 |
2879 |
}
|
2880 |
2880 |
int operator--(int) {
|
2881 |
2881 |
int value = _map[_key];
|
2882 |
2882 |
_map.set(_key, value - 1);
|
2883 |
2883 |
return value;
|
2884 |
2884 |
}
|
2885 |
2885 |
Reference& operator+=(int value) {
|
2886 |
2886 |
_map.set(_key, _map[_key] + value);
|
2887 |
2887 |
return *this;
|
2888 |
2888 |
}
|
2889 |
2889 |
Reference& operator-=(int value) {
|
2890 |
2890 |
_map.set(_key, _map[_key] - value);
|
2891 |
2891 |
return *this;
|
2892 |
2892 |
}
|
2893 |
2893 |
Reference& operator*=(int value) {
|
2894 |
2894 |
_map.set(_key, _map[_key] * value);
|
2895 |
2895 |
return *this;
|
2896 |
2896 |
}
|
2897 |
2897 |
Reference& operator/=(int value) {
|
2898 |
2898 |
_map.set(_key, _map[_key] / value);
|
2899 |
2899 |
return *this;
|
2900 |
2900 |
}
|
2901 |
2901 |
Reference& operator%=(int value) {
|
2902 |
2902 |
_map.set(_key, _map[_key] % value);
|
2903 |
2903 |
return *this;
|
2904 |
2904 |
}
|
2905 |
2905 |
Reference& operator&=(int value) {
|
2906 |
2906 |
_map.set(_key, _map[_key] & value);
|
2907 |
2907 |
return *this;
|
2908 |
2908 |
}
|
2909 |
2909 |
Reference& operator|=(int value) {
|
2910 |
2910 |
_map.set(_key, _map[_key] | value);
|
2911 |
2911 |
return *this;
|
2912 |
2912 |
}
|
2913 |
2913 |
Reference& operator^=(int value) {
|
2914 |
2914 |
_map.set(_key, _map[_key] ^ value);
|
2915 |
2915 |
return *this;
|
2916 |
2916 |
}
|
2917 |
2917 |
Reference& operator<<=(int value) {
|
2918 |
2918 |
_map.set(_key, _map[_key] << value);
|
2919 |
2919 |
return *this;
|
2920 |
2920 |
}
|
2921 |
2921 |
Reference& operator>>=(int value) {
|
2922 |
2922 |
_map.set(_key, _map[_key] >> value);
|
2923 |
2923 |
return *this;
|
2924 |
2924 |
}
|
2925 |
2925 |
|
2926 |
2926 |
private:
|
2927 |
2927 |
Key _key;
|
2928 |
2928 |
IterableIntMap& _map;
|
2929 |
2929 |
};
|
2930 |
2930 |
|
2931 |
2931 |
/// The const reference type.
|
2932 |
2932 |
typedef const Value& ConstReference;
|
2933 |
2933 |
|
2934 |
2934 |
/// \brief Gives back the maximal value plus one.
|
2935 |
2935 |
///
|
2936 |
2936 |
/// Gives back the maximal value plus one.
|
2937 |
2937 |
int size() const {
|
2938 |
2938 |
return _first.size();
|
2939 |
2939 |
}
|
2940 |
2940 |
|
2941 |
2941 |
/// \brief Set operation of the map.
|
2942 |
2942 |
///
|
2943 |
2943 |
/// Set operation of the map.
|
2944 |
2944 |
void set(const Key& key, const Value& value) {
|
2945 |
2945 |
unlace(key);
|
2946 |
2946 |
Parent::operator[](key).value = value;
|
2947 |
2947 |
lace(key);
|
2948 |
2948 |
}
|
2949 |
2949 |
|
2950 |
2950 |
/// \brief Const subscript operator of the map.
|
2951 |
2951 |
///
|
2952 |
2952 |
/// Const subscript operator of the map.
|
2953 |
2953 |
const Value& operator[](const Key& key) const {
|
2954 |
2954 |
return Parent::operator[](key).value;
|
2955 |
2955 |
}
|
2956 |
2956 |
|
2957 |
2957 |
/// \brief Subscript operator of the map.
|
2958 |
2958 |
///
|
2959 |
2959 |
/// Subscript operator of the map.
|
2960 |
2960 |
Reference operator[](const Key& key) {
|
2961 |
2961 |
return Reference(*this, key);
|
2962 |
2962 |
}
|
2963 |
2963 |
|
2964 |
2964 |
/// \brief Iterator for the keys with the same value.
|
2965 |
2965 |
///
|
2966 |
2966 |
/// Iterator for the keys with the same value. It works
|
2967 |
2967 |
/// like a graph item iterator, it can be converted to
|
2968 |
2968 |
/// the item type of the map, incremented with \c ++ operator, and
|
2969 |
2969 |
/// if the iterator leaves the last valid item, it will be equal to
|
2970 |
2970 |
/// \c INVALID.
|
2971 |
2971 |
class ItemIt : public Key {
|
2972 |
2972 |
public:
|
2973 |
2973 |
typedef Key Parent;
|
2974 |
2974 |
|
2975 |
2975 |
/// \brief Invalid constructor \& conversion.
|
2976 |
2976 |
///
|
2977 |
2977 |
/// This constructor initializes the iterator to be invalid.
|
2978 |
2978 |
/// \sa Invalid for more details.
|
2979 |
2979 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
2980 |
2980 |
|
2981 |
2981 |
/// \brief Creates an iterator with a value.
|
2982 |
2982 |
///
|
2983 |
2983 |
/// Creates an iterator with a value. It iterates on the
|
2984 |
2984 |
/// keys mapped to the given value.
|
2985 |
2985 |
/// \param map The IterableIntMap.
|
2986 |
2986 |
/// \param value The value.
|
2987 |
2987 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
2988 |
2988 |
if (value < 0 || value >= int(_map->_first.size())) {
|
2989 |
2989 |
Parent::operator=(INVALID);
|
2990 |
2990 |
} else {
|
2991 |
2991 |
Parent::operator=(_map->_first[value]);
|
2992 |
2992 |
}
|
2993 |
2993 |
}
|
2994 |
2994 |
|
2995 |
2995 |
/// \brief Increment operator.
|
2996 |
2996 |
///
|
2997 |
2997 |
/// Increment operator.
|
2998 |
2998 |
ItemIt& operator++() {
|
2999 |
2999 |
Parent::operator=(_map->IterableIntMap::Parent::
|
3000 |
3000 |
operator[](static_cast<Parent&>(*this)).next);
|
3001 |
3001 |
return *this;
|
3002 |
3002 |
}
|
3003 |
3003 |
|
3004 |
3004 |
private:
|
3005 |
3005 |
const IterableIntMap* _map;
|
3006 |
3006 |
};
|
3007 |
3007 |
|
3008 |
3008 |
protected:
|
3009 |
3009 |
|
3010 |
3010 |
virtual void erase(const Key& key) {
|
3011 |
3011 |
unlace(key);
|
3012 |
3012 |
Parent::erase(key);
|
3013 |
3013 |
}
|
3014 |
3014 |
|
3015 |
3015 |
virtual void erase(const std::vector<Key>& keys) {
|
3016 |
3016 |
for (int i = 0; i < int(keys.size()); ++i) {
|
3017 |
3017 |
unlace(keys[i]);
|
3018 |
3018 |
}
|
3019 |
3019 |
Parent::erase(keys);
|
3020 |
3020 |
}
|
3021 |
3021 |
|
3022 |
3022 |
virtual void clear() {
|
3023 |
3023 |
_first.clear();
|
3024 |
3024 |
Parent::clear();
|
3025 |
3025 |
}
|
3026 |
3026 |
|
3027 |
3027 |
private:
|
3028 |
3028 |
std::vector<Key> _first;
|
3029 |
3029 |
};
|
3030 |
3030 |
|
3031 |
3031 |
namespace _maps_bits {
|
3032 |
3032 |
template <typename Item, typename Value>
|
3033 |
3033 |
struct IterableValueMapNode {
|
3034 |
3034 |
IterableValueMapNode(Value _value = Value()) : value(_value) {}
|
3035 |
3035 |
Item prev, next;
|
3036 |
3036 |
Value value;
|
3037 |
3037 |
};
|
3038 |
3038 |
}
|
3039 |
3039 |
|
3040 |
3040 |
/// \brief Dynamic iterable map for comparable values.
|
3041 |
3041 |
///
|
3042 |
3042 |
/// This class provides a special graph map type which can store a
|
3043 |
3043 |
/// comparable value for graph items (\c Node, \c Arc or \c Edge).
|
3044 |
3044 |
/// For each value it is possible to iterate on the keys mapped to
|
3045 |
3045 |
/// the value (\c ItemIt), and the values of the map can be accessed
|
3046 |
3046 |
/// with an STL compatible forward iterator (\c ValueIt).
|
3047 |
3047 |
/// The map stores a linked list for each value, which contains
|
3048 |
3048 |
/// the items mapped to the value, and the used values are stored
|
3049 |
3049 |
/// in balanced binary tree (\c std::map).
|
3050 |
3050 |
///
|
3051 |
3051 |
/// \ref IterableBoolMap and \ref IterableIntMap are similar classes
|
3052 |
3052 |
/// specialized for \c bool and \c int values, respectively.
|
3053 |
3053 |
///
|
3054 |
3054 |
/// This type is not reference map, so it cannot be modified with
|
3055 |
3055 |
/// the subscript operator.
|
3056 |
3056 |
///
|
3057 |
3057 |
/// \tparam GR The graph type.
|
3058 |
3058 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
3059 |
3059 |
/// \c GR::Edge).
|
3060 |
3060 |
/// \tparam V The value type of the map. It can be any comparable
|
3061 |
3061 |
/// value type.
|
3062 |
3062 |
///
|
3063 |
3063 |
/// \see IterableBoolMap, IterableIntMap
|
3064 |
3064 |
/// \see CrossRefMap
|
3065 |
3065 |
template <typename GR, typename K, typename V>
|
3066 |
3066 |
class IterableValueMap
|
3067 |
3067 |
: protected ItemSetTraits<GR, K>::
|
3068 |
3068 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
|
3069 |
3069 |
public:
|
3070 |
3070 |
typedef typename ItemSetTraits<GR, K>::
|
3071 |
3071 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
|
3072 |
3072 |
|
3073 |
3073 |
/// The key type
|
3074 |
3074 |
typedef K Key;
|
3075 |
3075 |
/// The value type
|
3076 |
3076 |
typedef V Value;
|
3077 |
3077 |
/// The graph type
|
3078 |
3078 |
typedef GR Graph;
|
3079 |
3079 |
|
3080 |
3080 |
public:
|
3081 |
3081 |
|
3082 |
3082 |
/// \brief Constructor of the map with a given value.
|
3083 |
3083 |
///
|
3084 |
3084 |
/// Constructor of the map with a given value.
|
3085 |
3085 |
explicit IterableValueMap(const Graph& graph,
|
3086 |
3086 |
const Value& value = Value())
|
3087 |
3087 |
: Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
|
3088 |
3088 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
3089 |
3089 |
lace(it);
|
3090 |
3090 |
}
|
3091 |
3091 |
}
|
3092 |
3092 |
|
3093 |
3093 |
protected:
|
3094 |
3094 |
|
3095 |
3095 |
void unlace(const Key& key) {
|
3096 |
3096 |
typename Parent::Value& node = Parent::operator[](key);
|
3097 |
3097 |
if (node.prev != INVALID) {
|
3098 |
3098 |
Parent::operator[](node.prev).next = node.next;
|
3099 |
3099 |
} else {
|
3100 |
3100 |
if (node.next != INVALID) {
|
3101 |
3101 |
_first[node.value] = node.next;
|
3102 |
3102 |
} else {
|
3103 |
3103 |
_first.erase(node.value);
|
3104 |
3104 |
}
|
3105 |
3105 |
}
|
3106 |
3106 |
if (node.next != INVALID) {
|
3107 |
3107 |
Parent::operator[](node.next).prev = node.prev;
|
3108 |
3108 |
}
|
3109 |
3109 |
}
|
3110 |
3110 |
|
3111 |
3111 |
void lace(const Key& key) {
|
3112 |
3112 |
typename Parent::Value& node = Parent::operator[](key);
|
3113 |
3113 |
typename std::map<Value, Key>::iterator it = _first.find(node.value);
|
3114 |
3114 |
if (it == _first.end()) {
|
3115 |
3115 |
node.prev = node.next = INVALID;
|
3116 |
3116 |
_first.insert(std::make_pair(node.value, key));
|
3117 |
3117 |
} else {
|
3118 |
3118 |
node.prev = INVALID;
|
3119 |
3119 |
node.next = it->second;
|
3120 |
3120 |
if (node.next != INVALID) {
|
3121 |
3121 |
Parent::operator[](node.next).prev = key;
|
3122 |
3122 |
}
|
3123 |
3123 |
it->second = key;
|
3124 |
3124 |
}
|
3125 |
3125 |
}
|
3126 |
3126 |
|
3127 |
3127 |
public:
|
3128 |
3128 |
|
3129 |
3129 |
/// \brief Forward iterator for values.
|
3130 |
3130 |
///
|
3131 |
3131 |
/// This iterator is an STL compatible forward
|
3132 |
3132 |
/// iterator on the values of the map. The values can
|
3133 |
3133 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
|
3134 |
3134 |
class ValueIt
|
3135 |
3135 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
3136 |
3136 |
friend class IterableValueMap;
|
3137 |
3137 |
private:
|
3138 |
3138 |
ValueIt(typename std::map<Value, Key>::const_iterator _it)
|
3139 |
3139 |
: it(_it) {}
|
3140 |
3140 |
public:
|
3141 |
3141 |
|
3142 |
3142 |
/// Constructor
|
3143 |
3143 |
ValueIt() {}
|
3144 |
3144 |
|
3145 |
3145 |
/// \e
|
3146 |
3146 |
ValueIt& operator++() { ++it; return *this; }
|
3147 |
3147 |
/// \e
|
3148 |
3148 |
ValueIt operator++(int) {
|
3149 |
3149 |
ValueIt tmp(*this);
|
3150 |
3150 |
operator++();
|
3151 |
3151 |
return tmp;
|
3152 |
3152 |
}
|
3153 |
3153 |
|
3154 |
3154 |
/// \e
|
3155 |
3155 |
const Value& operator*() const { return it->first; }
|
3156 |
3156 |
/// \e
|
3157 |
3157 |
const Value* operator->() const { return &(it->first); }
|
3158 |
3158 |
|
3159 |
3159 |
/// \e
|
3160 |
3160 |
bool operator==(ValueIt jt) const { return it == jt.it; }
|
3161 |
3161 |
/// \e
|
3162 |
3162 |
bool operator!=(ValueIt jt) const { return it != jt.it; }
|
3163 |
3163 |
|
3164 |
3164 |
private:
|
3165 |
3165 |
typename std::map<Value, Key>::const_iterator it;
|
3166 |
3166 |
};
|
3167 |
3167 |
|
3168 |
3168 |
/// \brief Returns an iterator to the first value.
|
3169 |
3169 |
///
|
3170 |
3170 |
/// Returns an STL compatible iterator to the
|
3171 |
3171 |
/// first value of the map. The values of the
|
3172 |
3172 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
3173 |
3173 |
/// range.
|
3174 |
3174 |
ValueIt beginValue() const {
|
3175 |
3175 |
return ValueIt(_first.begin());
|
3176 |
3176 |
}
|
3177 |
3177 |
|
3178 |
3178 |
/// \brief Returns an iterator after the last value.
|
3179 |
3179 |
///
|
3180 |
3180 |
/// Returns an STL compatible iterator after the
|
3181 |
3181 |
/// last value of the map. The values of the
|
3182 |
3182 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
3183 |
3183 |
/// range.
|
3184 |
3184 |
ValueIt endValue() const {
|
3185 |
3185 |
return ValueIt(_first.end());
|
3186 |
3186 |
}
|
3187 |
3187 |
|
3188 |
3188 |
/// \brief Set operation of the map.
|
3189 |
3189 |
///
|
3190 |
3190 |
/// Set operation of the map.
|
3191 |
3191 |
void set(const Key& key, const Value& value) {
|
3192 |
3192 |
unlace(key);
|
3193 |
3193 |
Parent::operator[](key).value = value;
|
3194 |
3194 |
lace(key);
|
3195 |
3195 |
}
|
3196 |
3196 |
|
3197 |
3197 |
/// \brief Const subscript operator of the map.
|
3198 |
3198 |
///
|
3199 |
3199 |
/// Const subscript operator of the map.
|
3200 |
3200 |
const Value& operator[](const Key& key) const {
|
3201 |
3201 |
return Parent::operator[](key).value;
|
3202 |
3202 |
}
|
3203 |
3203 |
|
3204 |
3204 |
/// \brief Iterator for the keys with the same value.
|
3205 |
3205 |
///
|
3206 |
3206 |
/// Iterator for the keys with the same value. It works
|
3207 |
3207 |
/// like a graph item iterator, it can be converted to
|
3208 |
3208 |
/// the item type of the map, incremented with \c ++ operator, and
|
3209 |
3209 |
/// if the iterator leaves the last valid item, it will be equal to
|
3210 |
3210 |
/// \c INVALID.
|
3211 |
3211 |
class ItemIt : public Key {
|
3212 |
3212 |
public:
|
3213 |
3213 |
typedef Key Parent;
|
3214 |
3214 |
|
3215 |
3215 |
/// \brief Invalid constructor \& conversion.
|
3216 |
3216 |
///
|
3217 |
3217 |
/// This constructor initializes the iterator to be invalid.
|
3218 |
3218 |
/// \sa Invalid for more details.
|
3219 |
3219 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
3220 |
3220 |
|
3221 |
3221 |
/// \brief Creates an iterator with a value.
|
3222 |
3222 |
///
|
3223 |
3223 |
/// Creates an iterator with a value. It iterates on the
|
3224 |
3224 |
/// keys which have the given value.
|
3225 |
3225 |
/// \param map The IterableValueMap
|
3226 |
3226 |
/// \param value The value
|
3227 |
3227 |
ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
|
3228 |
3228 |
typename std::map<Value, Key>::const_iterator it =
|
3229 |
3229 |
map._first.find(value);
|
3230 |
3230 |
if (it == map._first.end()) {
|
3231 |
3231 |
Parent::operator=(INVALID);
|
3232 |
3232 |
} else {
|
3233 |
3233 |
Parent::operator=(it->second);
|
3234 |
3234 |
}
|
3235 |
3235 |
}
|
3236 |
3236 |
|
3237 |
3237 |
/// \brief Increment operator.
|
3238 |
3238 |
///
|
3239 |
3239 |
/// Increment Operator.
|
3240 |
3240 |
ItemIt& operator++() {
|
3241 |
3241 |
Parent::operator=(_map->IterableValueMap::Parent::
|
3242 |
3242 |
operator[](static_cast<Parent&>(*this)).next);
|
3243 |
3243 |
return *this;
|
3244 |
3244 |
}
|
3245 |
3245 |
|
3246 |
3246 |
|
3247 |
3247 |
private:
|
3248 |
3248 |
const IterableValueMap* _map;
|
3249 |
3249 |
};
|
3250 |
3250 |
|
3251 |
3251 |
protected:
|
3252 |
3252 |
|
3253 |
3253 |
virtual void add(const Key& key) {
|
3254 |
3254 |
Parent::add(key);
|
3255 |
3255 |
unlace(key);
|
3256 |
3256 |
}
|
3257 |
3257 |
|
3258 |
3258 |
virtual void add(const std::vector<Key>& keys) {
|
3259 |
3259 |
Parent::add(keys);
|
3260 |
3260 |
for (int i = 0; i < int(keys.size()); ++i) {
|
3261 |
3261 |
lace(keys[i]);
|
3262 |
3262 |
}
|
3263 |
3263 |
}
|
3264 |
3264 |
|
3265 |
3265 |
virtual void erase(const Key& key) {
|
3266 |
3266 |
unlace(key);
|
3267 |
3267 |
Parent::erase(key);
|
3268 |
3268 |
}
|
3269 |
3269 |
|
3270 |
3270 |
virtual void erase(const std::vector<Key>& keys) {
|
3271 |
3271 |
for (int i = 0; i < int(keys.size()); ++i) {
|
3272 |
3272 |
unlace(keys[i]);
|
3273 |
3273 |
}
|
3274 |
3274 |
Parent::erase(keys);
|
3275 |
3275 |
}
|
3276 |
3276 |
|
3277 |
3277 |
virtual void build() {
|
3278 |
3278 |
Parent::build();
|
3279 |
3279 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
3280 |
3280 |
lace(it);
|
3281 |
3281 |
}
|
3282 |
3282 |
}
|
3283 |
3283 |
|
3284 |
3284 |
virtual void clear() {
|
3285 |
3285 |
_first.clear();
|
3286 |
3286 |
Parent::clear();
|
3287 |
3287 |
}
|
3288 |
3288 |
|
3289 |
3289 |
private:
|
3290 |
3290 |
std::map<Value, Key> _first;
|
3291 |
3291 |
};
|
3292 |
3292 |
|
3293 |
3293 |
/// \brief Map of the source nodes of arcs in a digraph.
|
3294 |
3294 |
///
|
3295 |
3295 |
/// SourceMap provides access for the source node of each arc in a digraph,
|
3296 |
3296 |
/// which is returned by the \c source() function of the digraph.
|
3297 |
3297 |
/// \tparam GR The digraph type.
|
3298 |
3298 |
/// \see TargetMap
|
3299 |
3299 |
template <typename GR>
|
3300 |
3300 |
class SourceMap {
|
3301 |
3301 |
public:
|
3302 |
3302 |
|
3303 |
3303 |
/// The key type (the \c Arc type of the digraph).
|
3304 |
3304 |
typedef typename GR::Arc Key;
|
3305 |
3305 |
/// The value type (the \c Node type of the digraph).
|
3306 |
3306 |
typedef typename GR::Node Value;
|
3307 |
3307 |
|
3308 |
3308 |
/// \brief Constructor
|
3309 |
3309 |
///
|
3310 |
3310 |
/// Constructor.
|
3311 |
3311 |
/// \param digraph The digraph that the map belongs to.
|
3312 |
3312 |
explicit SourceMap(const GR& digraph) : _graph(digraph) {}
|
3313 |
3313 |
|
3314 |
3314 |
/// \brief Returns the source node of the given arc.
|
3315 |
3315 |
///
|
3316 |
3316 |
/// Returns the source node of the given arc.
|
3317 |
3317 |
Value operator[](const Key& arc) const {
|
3318 |
3318 |
return _graph.source(arc);
|
3319 |
3319 |
}
|
3320 |
3320 |
|
3321 |
3321 |
private:
|
3322 |
3322 |
const GR& _graph;
|
3323 |
3323 |
};
|
3324 |
3324 |
|
3325 |
3325 |
/// \brief Returns a \c SourceMap class.
|
3326 |
3326 |
///
|
3327 |
3327 |
/// This function just returns an \c SourceMap class.
|
3328 |
3328 |
/// \relates SourceMap
|
3329 |
3329 |
template <typename GR>
|
3330 |
3330 |
inline SourceMap<GR> sourceMap(const GR& graph) {
|
3331 |
3331 |
return SourceMap<GR>(graph);
|
3332 |
3332 |
}
|
3333 |
3333 |
|
3334 |
3334 |
/// \brief Map of the target nodes of arcs in a digraph.
|
3335 |
3335 |
///
|
3336 |
3336 |
/// TargetMap provides access for the target node of each arc in a digraph,
|
3337 |
3337 |
/// which is returned by the \c target() function of the digraph.
|
3338 |
3338 |
/// \tparam GR The digraph type.
|
3339 |
3339 |
/// \see SourceMap
|
3340 |
3340 |
template <typename GR>
|
3341 |
3341 |
class TargetMap {
|
3342 |
3342 |
public:
|
3343 |
3343 |
|
3344 |
3344 |
/// The key type (the \c Arc type of the digraph).
|
3345 |
3345 |
typedef typename GR::Arc Key;
|
3346 |
3346 |
/// The value type (the \c Node type of the digraph).
|
3347 |
3347 |
typedef typename GR::Node Value;
|
3348 |
3348 |
|
3349 |
3349 |
/// \brief Constructor
|
3350 |
3350 |
///
|
3351 |
3351 |
/// Constructor.
|
3352 |
3352 |
/// \param digraph The digraph that the map belongs to.
|
3353 |
3353 |
explicit TargetMap(const GR& digraph) : _graph(digraph) {}
|
3354 |
3354 |
|
3355 |
3355 |
/// \brief Returns the target node of the given arc.
|
3356 |
3356 |
///
|
3357 |
3357 |
/// Returns the target node of the given arc.
|
3358 |
3358 |
Value operator[](const Key& e) const {
|
3359 |
3359 |
return _graph.target(e);
|
3360 |
3360 |
}
|
3361 |
3361 |
|
3362 |
3362 |
private:
|
3363 |
3363 |
const GR& _graph;
|
3364 |
3364 |
};
|
3365 |
3365 |
|
3366 |
3366 |
/// \brief Returns a \c TargetMap class.
|
3367 |
3367 |
///
|
3368 |
3368 |
/// This function just returns a \c TargetMap class.
|
3369 |
3369 |
/// \relates TargetMap
|
3370 |
3370 |
template <typename GR>
|
3371 |
3371 |
inline TargetMap<GR> targetMap(const GR& graph) {
|
3372 |
3372 |
return TargetMap<GR>(graph);
|
3373 |
3373 |
}
|
3374 |
3374 |
|
3375 |
3375 |
/// \brief Map of the "forward" directed arc view of edges in a graph.
|
3376 |
3376 |
///
|
3377 |
3377 |
/// ForwardMap provides access for the "forward" directed arc view of
|
3378 |
3378 |
/// each edge in a graph, which is returned by the \c direct() function
|
3379 |
3379 |
/// of the graph with \c true parameter.
|
3380 |
3380 |
/// \tparam GR The graph type.
|
3381 |
3381 |
/// \see BackwardMap
|
3382 |
3382 |
template <typename GR>
|
3383 |
3383 |
class ForwardMap {
|
3384 |
3384 |
public:
|
3385 |
3385 |
|
3386 |
3386 |
/// The key type (the \c Edge type of the digraph).
|
3387 |
3387 |
typedef typename GR::Edge Key;
|
3388 |
3388 |
/// The value type (the \c Arc type of the digraph).
|
3389 |
3389 |
typedef typename GR::Arc Value;
|
3390 |
3390 |
|
3391 |
3391 |
/// \brief Constructor
|
3392 |
3392 |
///
|
3393 |
3393 |
/// Constructor.
|
3394 |
3394 |
/// \param graph The graph that the map belongs to.
|
3395 |
3395 |
explicit ForwardMap(const GR& graph) : _graph(graph) {}
|
3396 |
3396 |
|
3397 |
3397 |
/// \brief Returns the "forward" directed arc view of the given edge.
|
3398 |
3398 |
///
|
3399 |
3399 |
/// Returns the "forward" directed arc view of the given edge.
|
3400 |
3400 |
Value operator[](const Key& key) const {
|
3401 |
3401 |
return _graph.direct(key, true);
|
3402 |
3402 |
}
|
3403 |
3403 |
|
3404 |
3404 |
private:
|
3405 |
3405 |
const GR& _graph;
|
3406 |
3406 |
};
|
3407 |
3407 |
|
3408 |
3408 |
/// \brief Returns a \c ForwardMap class.
|
3409 |
3409 |
///
|
3410 |
3410 |
/// This function just returns an \c ForwardMap class.
|
3411 |
3411 |
/// \relates ForwardMap
|
3412 |
3412 |
template <typename GR>
|
3413 |
3413 |
inline ForwardMap<GR> forwardMap(const GR& graph) {
|
3414 |
3414 |
return ForwardMap<GR>(graph);
|
3415 |
3415 |
}
|
3416 |
3416 |
|
3417 |
3417 |
/// \brief Map of the "backward" directed arc view of edges in a graph.
|
3418 |
3418 |
///
|
3419 |
3419 |
/// BackwardMap provides access for the "backward" directed arc view of
|
3420 |
3420 |
/// each edge in a graph, which is returned by the \c direct() function
|
3421 |
3421 |
/// of the graph with \c false parameter.
|
3422 |
3422 |
/// \tparam GR The graph type.
|
3423 |
3423 |
/// \see ForwardMap
|
3424 |
3424 |
template <typename GR>
|
3425 |
3425 |
class BackwardMap {
|
3426 |
3426 |
public:
|
3427 |
3427 |
|
3428 |
3428 |
/// The key type (the \c Edge type of the digraph).
|
3429 |
3429 |
typedef typename GR::Edge Key;
|
3430 |
3430 |
/// The value type (the \c Arc type of the digraph).
|
3431 |
3431 |
typedef typename GR::Arc Value;
|
3432 |
3432 |
|
3433 |
3433 |
/// \brief Constructor
|
3434 |
3434 |
///
|
3435 |
3435 |
/// Constructor.
|
3436 |
3436 |
/// \param graph The graph that the map belongs to.
|
3437 |
3437 |
explicit BackwardMap(const GR& graph) : _graph(graph) {}
|
3438 |
3438 |
|
3439 |
3439 |
/// \brief Returns the "backward" directed arc view of the given edge.
|
3440 |
3440 |
///
|
3441 |
3441 |
/// Returns the "backward" directed arc view of the given edge.
|
3442 |
3442 |
Value operator[](const Key& key) const {
|
3443 |
3443 |
return _graph.direct(key, false);
|
3444 |
3444 |
}
|
3445 |
3445 |
|
3446 |
3446 |
private:
|
3447 |
3447 |
const GR& _graph;
|
3448 |
3448 |
};
|
3449 |
3449 |
|
3450 |
3450 |
/// \brief Returns a \c BackwardMap class
|
3451 |
3451 |
|
3452 |
3452 |
/// This function just returns a \c BackwardMap class.
|
3453 |
3453 |
/// \relates BackwardMap
|
3454 |
3454 |
template <typename GR>
|
3455 |
3455 |
inline BackwardMap<GR> backwardMap(const GR& graph) {
|
3456 |
3456 |
return BackwardMap<GR>(graph);
|
3457 |
3457 |
}
|
3458 |
3458 |
|
3459 |
3459 |
/// \brief Map of the in-degrees of nodes in a digraph.
|
3460 |
3460 |
///
|
3461 |
3461 |
/// This map returns the in-degree of a node. Once it is constructed,
|
3462 |
3462 |
/// the degrees are stored in a standard \c NodeMap, so each query is done
|
3463 |
3463 |
/// in constant time. On the other hand, the values are updated automatically
|
3464 |
3464 |
/// whenever the digraph changes.
|
3465 |
3465 |
///
|
3466 |
3466 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
3467 |
3467 |
/// may provide alternative ways to modify the digraph.
|
3468 |
3468 |
/// The correct behavior of InDegMap is not guarantied if these additional
|
3469 |
3469 |
/// features are used. For example, the functions
|
3470 |
3470 |
/// \ref ListDigraph::changeSource() "changeSource()",
|
3471 |
3471 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
3472 |
3472 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
3473 |
3473 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
3474 |
3474 |
///
|
3475 |
3475 |
/// \sa OutDegMap
|
3476 |
3476 |
template <typename GR>
|
3477 |
3477 |
class InDegMap
|
3478 |
3478 |
: protected ItemSetTraits<GR, typename GR::Arc>
|
3479 |
3479 |
::ItemNotifier::ObserverBase {
|
3480 |
3480 |
|
3481 |
3481 |
public:
|
3482 |
3482 |
|
3483 |
3483 |
/// The graph type of InDegMap
|
3484 |
3484 |
typedef GR Graph;
|
3485 |
3485 |
typedef GR Digraph;
|
3486 |
3486 |
/// The key type
|
3487 |
3487 |
typedef typename Digraph::Node Key;
|
3488 |
3488 |
/// The value type
|
3489 |
3489 |
typedef int Value;
|
3490 |
3490 |
|
3491 |
3491 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
3492 |
3492 |
::ItemNotifier::ObserverBase Parent;
|
3493 |
3493 |
|
3494 |
3494 |
private:
|
3495 |
3495 |
|
3496 |
3496 |
class AutoNodeMap
|
3497 |
3497 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
3498 |
3498 |
public:
|
3499 |
3499 |
|
3500 |
3500 |
typedef typename ItemSetTraits<Digraph, Key>::
|
3501 |
3501 |
template Map<int>::Type Parent;
|
3502 |
3502 |
|
3503 |
3503 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
3504 |
3504 |
|
3505 |
3505 |
virtual void add(const Key& key) {
|
3506 |
3506 |
Parent::add(key);
|
3507 |
3507 |
Parent::set(key, 0);
|
3508 |
3508 |
}
|
3509 |
3509 |
|
3510 |
3510 |
virtual void add(const std::vector<Key>& keys) {
|
3511 |
3511 |
Parent::add(keys);
|
3512 |
3512 |
for (int i = 0; i < int(keys.size()); ++i) {
|
3513 |
3513 |
Parent::set(keys[i], 0);
|
3514 |
3514 |
}
|
3515 |
3515 |
}
|
3516 |
3516 |
|
3517 |
3517 |
virtual void build() {
|
3518 |
3518 |
Parent::build();
|
3519 |
3519 |
Key it;
|
3520 |
3520 |
typename Parent::Notifier* nf = Parent::notifier();
|
3521 |
3521 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
3522 |
3522 |
Parent::set(it, 0);
|
3523 |
3523 |
}
|
3524 |
3524 |
}
|
3525 |
3525 |
};
|
3526 |
3526 |
|
3527 |
3527 |
public:
|
3528 |
3528 |
|
3529 |
3529 |
/// \brief Constructor.
|
3530 |
3530 |
///
|
3531 |
3531 |
/// Constructor for creating an in-degree map.
|
3532 |
3532 |
explicit InDegMap(const Digraph& graph)
|
3533 |
3533 |
: _digraph(graph), _deg(graph) {
|
3534 |
3534 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
3535 |
3535 |
|
3536 |
3536 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
3537 |
3537 |
_deg[it] = countInArcs(_digraph, it);
|
3538 |
3538 |
}
|
3539 |
3539 |
}
|
3540 |
3540 |
|
3541 |
3541 |
/// \brief Gives back the in-degree of a Node.
|
3542 |
3542 |
///
|
3543 |
3543 |
/// Gives back the in-degree of a Node.
|
3544 |
3544 |
int operator[](const Key& key) const {
|
3545 |
3545 |
return _deg[key];
|
3546 |
3546 |
}
|
3547 |
3547 |
|
3548 |
3548 |
protected:
|
3549 |
3549 |
|
3550 |
3550 |
typedef typename Digraph::Arc Arc;
|
3551 |
3551 |
|
3552 |
3552 |
virtual void add(const Arc& arc) {
|
3553 |
3553 |
++_deg[_digraph.target(arc)];
|
3554 |
3554 |
}
|
3555 |
3555 |
|
3556 |
3556 |
virtual void add(const std::vector<Arc>& arcs) {
|
3557 |
3557 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
3558 |
3558 |
++_deg[_digraph.target(arcs[i])];
|
3559 |
3559 |
}
|
3560 |
3560 |
}
|
3561 |
3561 |
|
3562 |
3562 |
virtual void erase(const Arc& arc) {
|
3563 |
3563 |
--_deg[_digraph.target(arc)];
|
3564 |
3564 |
}
|
3565 |
3565 |
|
3566 |
3566 |
virtual void erase(const std::vector<Arc>& arcs) {
|
3567 |
3567 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
3568 |
3568 |
--_deg[_digraph.target(arcs[i])];
|
3569 |
3569 |
}
|
3570 |
3570 |
}
|
3571 |
3571 |
|
3572 |
3572 |
virtual void build() {
|
3573 |
3573 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
3574 |
3574 |
_deg[it] = countInArcs(_digraph, it);
|
3575 |
3575 |
}
|
3576 |
3576 |
}
|
3577 |
3577 |
|
3578 |
3578 |
virtual void clear() {
|
3579 |
3579 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
3580 |
3580 |
_deg[it] = 0;
|
3581 |
3581 |
}
|
3582 |
3582 |
}
|
3583 |
3583 |
private:
|
3584 |
3584 |
|
3585 |
3585 |
const Digraph& _digraph;
|
3586 |
3586 |
AutoNodeMap _deg;
|
3587 |
3587 |
};
|
3588 |
3588 |
|
3589 |
3589 |
/// \brief Map of the out-degrees of nodes in a digraph.
|
3590 |
3590 |
///
|
3591 |
3591 |
/// This map returns the out-degree of a node. Once it is constructed,
|
3592 |
3592 |
/// the degrees are stored in a standard \c NodeMap, so each query is done
|
3593 |
3593 |
/// in constant time. On the other hand, the values are updated automatically
|
3594 |
3594 |
/// whenever the digraph changes.
|
3595 |
3595 |
///
|
3596 |
3596 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
3597 |
3597 |
/// may provide alternative ways to modify the digraph.
|
3598 |
3598 |
/// The correct behavior of OutDegMap is not guarantied if these additional
|
3599 |
3599 |
/// features are used. For example, the functions
|
3600 |
3600 |
/// \ref ListDigraph::changeSource() "changeSource()",
|
3601 |
3601 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
3602 |
3602 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
3603 |
3603 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
3604 |
3604 |
///
|
3605 |
3605 |
/// \sa InDegMap
|
3606 |
3606 |
template <typename GR>
|
3607 |
3607 |
class OutDegMap
|
3608 |
3608 |
: protected ItemSetTraits<GR, typename GR::Arc>
|
3609 |
3609 |
::ItemNotifier::ObserverBase {
|
3610 |
3610 |
|
3611 |
3611 |
public:
|
3612 |
3612 |
|
3613 |
3613 |
/// The graph type of OutDegMap
|
3614 |
3614 |
typedef GR Graph;
|
3615 |
3615 |
typedef GR Digraph;
|
3616 |
3616 |
/// The key type
|
3617 |
3617 |
typedef typename Digraph::Node Key;
|
3618 |
3618 |
/// The value type
|
3619 |
3619 |
typedef int Value;
|
3620 |
3620 |
|
3621 |
3621 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
3622 |
3622 |
::ItemNotifier::ObserverBase Parent;
|
3623 |
3623 |
|
3624 |
3624 |
private:
|
3625 |
3625 |
|
3626 |
3626 |
class AutoNodeMap
|
3627 |
3627 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
3628 |
3628 |
public:
|
3629 |
3629 |
|
3630 |
3630 |
typedef typename ItemSetTraits<Digraph, Key>::
|
3631 |
3631 |
template Map<int>::Type Parent;
|
3632 |
3632 |
|
3633 |
3633 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
3634 |
3634 |
|
3635 |
3635 |
virtual void add(const Key& key) {
|
3636 |
3636 |
Parent::add(key);
|
3637 |
3637 |
Parent::set(key, 0);
|
3638 |
3638 |
}
|
3639 |
3639 |
virtual void add(const std::vector<Key>& keys) {
|
3640 |
3640 |
Parent::add(keys);
|
3641 |
3641 |
for (int i = 0; i < int(keys.size()); ++i) {
|
3642 |
3642 |
Parent::set(keys[i], 0);
|
3643 |
3643 |
}
|
3644 |
3644 |
}
|
3645 |
3645 |
virtual void build() {
|
3646 |
3646 |
Parent::build();
|
3647 |
3647 |
Key it;
|
3648 |
3648 |
typename Parent::Notifier* nf = Parent::notifier();
|
3649 |
3649 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
3650 |
3650 |
Parent::set(it, 0);
|
3651 |
3651 |
}
|
3652 |
3652 |
}
|
3653 |
3653 |
};
|
3654 |
3654 |
|
3655 |
3655 |
public:
|
3656 |
3656 |
|
3657 |
3657 |
/// \brief Constructor.
|
3658 |
3658 |
///
|
3659 |
3659 |
/// Constructor for creating an out-degree map.
|
3660 |
3660 |
explicit OutDegMap(const Digraph& graph)
|
3661 |
3661 |
: _digraph(graph), _deg(graph) {
|
3662 |
3662 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
3663 |
3663 |
|
3664 |
3664 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
3665 |
3665 |
_deg[it] = countOutArcs(_digraph, it);
|
3666 |
3666 |
}
|
3667 |
3667 |
}
|
3668 |
3668 |
|
3669 |
3669 |
/// \brief Gives back the out-degree of a Node.
|
3670 |
3670 |
///
|
3671 |
3671 |
/// Gives back the out-degree of a Node.
|
3672 |
3672 |
int operator[](const Key& key) const {
|
3673 |
3673 |
return _deg[key];
|
3674 |
3674 |
}
|
3675 |
3675 |
|
3676 |
3676 |
protected:
|
3677 |
3677 |
|
3678 |
3678 |
typedef typename Digraph::Arc Arc;
|
3679 |
3679 |
|
3680 |
3680 |
virtual void add(const Arc& arc) {
|
3681 |
3681 |
++_deg[_digraph.source(arc)];
|
3682 |
3682 |
}
|
3683 |
3683 |
|
3684 |
3684 |
virtual void add(const std::vector<Arc>& arcs) {
|
3685 |
3685 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
3686 |
3686 |
++_deg[_digraph.source(arcs[i])];
|
3687 |
3687 |
}
|
3688 |
3688 |
}
|
3689 |
3689 |
|
3690 |
3690 |
virtual void erase(const Arc& arc) {
|
3691 |
3691 |
--_deg[_digraph.source(arc)];
|
3692 |
3692 |
}
|
3693 |
3693 |
|
3694 |
3694 |
virtual void erase(const std::vector<Arc>& arcs) {
|
3695 |
3695 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
3696 |
3696 |
--_deg[_digraph.source(arcs[i])];
|
3697 |
3697 |
}
|
3698 |
3698 |
}
|
3699 |
3699 |
|
3700 |
3700 |
virtual void build() {
|
3701 |
3701 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
3702 |
3702 |
_deg[it] = countOutArcs(_digraph, it);
|
3703 |
3703 |
}
|
3704 |
3704 |
}
|
3705 |
3705 |
|
3706 |
3706 |
virtual void clear() {
|
3707 |
3707 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
3708 |
3708 |
_deg[it] = 0;
|
3709 |
3709 |
}
|
3710 |
3710 |
}
|
3711 |
3711 |
private:
|
3712 |
3712 |
|
3713 |
3713 |
const Digraph& _digraph;
|
3714 |
3714 |
AutoNodeMap _deg;
|
3715 |
3715 |
};
|
3716 |
3716 |
|
3717 |
3717 |
/// \brief Potential difference map
|
3718 |
3718 |
///
|
3719 |
3719 |
/// PotentialDifferenceMap returns the difference between the potentials of
|
3720 |
3720 |
/// the source and target nodes of each arc in a digraph, i.e. it returns
|
3721 |
3721 |
/// \code
|
3722 |
3722 |
/// potential[gr.target(arc)] - potential[gr.source(arc)].
|
3723 |
3723 |
/// \endcode
|
3724 |
3724 |
/// \tparam GR The digraph type.
|
3725 |
3725 |
/// \tparam POT A node map storing the potentials.
|
3726 |
3726 |
template <typename GR, typename POT>
|
3727 |
3727 |
class PotentialDifferenceMap {
|
3728 |
3728 |
public:
|
3729 |
3729 |
/// Key type
|
3730 |
3730 |
typedef typename GR::Arc Key;
|
3731 |
3731 |
/// Value type
|
3732 |
3732 |
typedef typename POT::Value Value;
|
3733 |
3733 |
|
3734 |
3734 |
/// \brief Constructor
|
3735 |
3735 |
///
|
3736 |
3736 |
/// Contructor of the map.
|
3737 |
3737 |
explicit PotentialDifferenceMap(const GR& gr,
|
3738 |
3738 |
const POT& potential)
|
3739 |
3739 |
: _digraph(gr), _potential(potential) {}
|
3740 |
3740 |
|
3741 |
3741 |
/// \brief Returns the potential difference for the given arc.
|
3742 |
3742 |
///
|
3743 |
3743 |
/// Returns the potential difference for the given arc, i.e.
|
3744 |
3744 |
/// \code
|
3745 |
3745 |
/// potential[gr.target(arc)] - potential[gr.source(arc)].
|
3746 |
3746 |
/// \endcode
|
3747 |
3747 |
Value operator[](const Key& arc) const {
|
3748 |
3748 |
return _potential[_digraph.target(arc)] -
|
3749 |
3749 |
_potential[_digraph.source(arc)];
|
3750 |
3750 |
}
|
3751 |
3751 |
|
3752 |
3752 |
private:
|
3753 |
3753 |
const GR& _digraph;
|
3754 |
3754 |
const POT& _potential;
|
3755 |
3755 |
};
|
3756 |
3756 |
|
3757 |
3757 |
/// \brief Returns a PotentialDifferenceMap.
|
3758 |
3758 |
///
|
3759 |
3759 |
/// This function just returns a PotentialDifferenceMap.
|
3760 |
3760 |
/// \relates PotentialDifferenceMap
|
3761 |
3761 |
template <typename GR, typename POT>
|
3762 |
3762 |
PotentialDifferenceMap<GR, POT>
|
3763 |
3763 |
potentialDifferenceMap(const GR& gr, const POT& potential) {
|
3764 |
3764 |
return PotentialDifferenceMap<GR, POT>(gr, potential);
|
3765 |
3765 |
}
|
3766 |
3766 |
|
|
3767 |
|
|
3768 |
/// \brief Copy the values of a graph map to another map.
|
|
3769 |
///
|
|
3770 |
/// This function copies the values of a graph map to another graph map.
|
|
3771 |
/// \c To::Key must be equal or convertible to \c From::Key and
|
|
3772 |
/// \c From::Value must be equal or convertible to \c To::Value.
|
|
3773 |
///
|
|
3774 |
/// For example, an edge map of \c int value type can be copied to
|
|
3775 |
/// an arc map of \c double value type in an undirected graph, but
|
|
3776 |
/// an arc map cannot be copied to an edge map.
|
|
3777 |
/// Note that even a \ref ConstMap can be copied to a standard graph map,
|
|
3778 |
/// but \ref mapFill() can also be used for this purpose.
|
|
3779 |
///
|
|
3780 |
/// \param gr The graph for which the maps are defined.
|
|
3781 |
/// \param from The map from which the values have to be copied.
|
|
3782 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
|
|
3783 |
/// \param to The map to which the values have to be copied.
|
|
3784 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
|
|
3785 |
template <typename GR, typename From, typename To>
|
|
3786 |
void mapCopy(const GR& gr, const From& from, To& to) {
|
|
3787 |
typedef typename To::Key Item;
|
|
3788 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
3789 |
|
|
3790 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
3791 |
to.set(it, from[it]);
|
|
3792 |
}
|
|
3793 |
}
|
|
3794 |
|
|
3795 |
/// \brief Compare two graph maps.
|
|
3796 |
///
|
|
3797 |
/// This function compares the values of two graph maps. It returns
|
|
3798 |
/// \c true if the maps assign the same value for all items in the graph.
|
|
3799 |
/// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
|
|
3800 |
/// and their \c Value types must be comparable using \c %operator==().
|
|
3801 |
///
|
|
3802 |
/// \param gr The graph for which the maps are defined.
|
|
3803 |
/// \param map1 The first map.
|
|
3804 |
/// \param map2 The second map.
|
|
3805 |
template <typename GR, typename Map1, typename Map2>
|
|
3806 |
bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
|
|
3807 |
typedef typename Map2::Key Item;
|
|
3808 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
3809 |
|
|
3810 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
3811 |
if (!(map1[it] == map2[it])) return false;
|
|
3812 |
}
|
|
3813 |
return true;
|
|
3814 |
}
|
|
3815 |
|
|
3816 |
/// \brief Return an item having minimum value of a graph map.
|
|
3817 |
///
|
|
3818 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having
|
|
3819 |
/// minimum value of the given graph map.
|
|
3820 |
/// If the item set is empty, it returns \c INVALID.
|
|
3821 |
///
|
|
3822 |
/// \param gr The graph for which the map is defined.
|
|
3823 |
/// \param map The graph map.
|
|
3824 |
template <typename GR, typename Map>
|
|
3825 |
typename Map::Key mapMin(const GR& gr, const Map& map) {
|
|
3826 |
return mapMin(gr, map, std::less<typename Map::Value>());
|
|
3827 |
}
|
|
3828 |
|
|
3829 |
/// \brief Return an item having minimum value of a graph map.
|
|
3830 |
///
|
|
3831 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having
|
|
3832 |
/// minimum value of the given graph map.
|
|
3833 |
/// If the item set is empty, it returns \c INVALID.
|
|
3834 |
///
|
|
3835 |
/// \param gr The graph for which the map is defined.
|
|
3836 |
/// \param map The graph map.
|
|
3837 |
/// \param comp Comparison function object.
|
|
3838 |
template <typename GR, typename Map, typename Comp>
|
|
3839 |
typename Map::Key mapMin(const GR& gr, const Map& map, const Comp& comp) {
|
|
3840 |
typedef typename Map::Key Item;
|
|
3841 |
typedef typename Map::Value Value;
|
|
3842 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
3843 |
|
|
3844 |
ItemIt min_item(gr);
|
|
3845 |
if (min_item == INVALID) return INVALID;
|
|
3846 |
Value min = map[min_item];
|
|
3847 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
3848 |
if (comp(map[it], min)) {
|
|
3849 |
min = map[it];
|
|
3850 |
min_item = it;
|
|
3851 |
}
|
|
3852 |
}
|
|
3853 |
return min_item;
|
|
3854 |
}
|
|
3855 |
|
|
3856 |
/// \brief Return an item having maximum value of a graph map.
|
|
3857 |
///
|
|
3858 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having
|
|
3859 |
/// maximum value of the given graph map.
|
|
3860 |
/// If the item set is empty, it returns \c INVALID.
|
|
3861 |
///
|
|
3862 |
/// \param gr The graph for which the map is defined.
|
|
3863 |
/// \param map The graph map.
|
|
3864 |
template <typename GR, typename Map>
|
|
3865 |
typename Map::Key mapMax(const GR& gr, const Map& map) {
|
|
3866 |
return mapMax(gr, map, std::less<typename Map::Value>());
|
|
3867 |
}
|
|
3868 |
|
|
3869 |
/// \brief Return an item having maximum value of a graph map.
|
|
3870 |
///
|
|
3871 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having
|
|
3872 |
/// maximum value of the given graph map.
|
|
3873 |
/// If the item set is empty, it returns \c INVALID.
|
|
3874 |
///
|
|
3875 |
/// \param gr The graph for which the map is defined.
|
|
3876 |
/// \param map The graph map.
|
|
3877 |
/// \param comp Comparison function object.
|
|
3878 |
template <typename GR, typename Map, typename Comp>
|
|
3879 |
typename Map::Key mapMax(const GR& gr, const Map& map, const Comp& comp) {
|
|
3880 |
typedef typename Map::Key Item;
|
|
3881 |
typedef typename Map::Value Value;
|
|
3882 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
3883 |
|
|
3884 |
ItemIt max_item(gr);
|
|
3885 |
if (max_item == INVALID) return INVALID;
|
|
3886 |
Value max = map[max_item];
|
|
3887 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
3888 |
if (comp(max, map[it])) {
|
|
3889 |
max = map[it];
|
|
3890 |
max_item = it;
|
|
3891 |
}
|
|
3892 |
}
|
|
3893 |
return max_item;
|
|
3894 |
}
|
|
3895 |
|
|
3896 |
/// \brief Return the minimum value of a graph map.
|
|
3897 |
///
|
|
3898 |
/// This function returns the minimum value of the given graph map.
|
|
3899 |
/// The corresponding item set of the graph must not be empty.
|
|
3900 |
///
|
|
3901 |
/// \param gr The graph for which the map is defined.
|
|
3902 |
/// \param map The graph map.
|
|
3903 |
template <typename GR, typename Map>
|
|
3904 |
typename Map::Value mapMinValue(const GR& gr, const Map& map) {
|
|
3905 |
return map[mapMin(gr, map, std::less<typename Map::Value>())];
|
|
3906 |
}
|
|
3907 |
|
|
3908 |
/// \brief Return the minimum value of a graph map.
|
|
3909 |
///
|
|
3910 |
/// This function returns the minimum value of the given graph map.
|
|
3911 |
/// The corresponding item set of the graph must not be empty.
|
|
3912 |
///
|
|
3913 |
/// \param gr The graph for which the map is defined.
|
|
3914 |
/// \param map The graph map.
|
|
3915 |
/// \param comp Comparison function object.
|
|
3916 |
template <typename GR, typename Map, typename Comp>
|
|
3917 |
typename Map::Value
|
|
3918 |
mapMinValue(const GR& gr, const Map& map, const Comp& comp) {
|
|
3919 |
return map[mapMin(gr, map, comp)];
|
|
3920 |
}
|
|
3921 |
|
|
3922 |
/// \brief Return the maximum value of a graph map.
|
|
3923 |
///
|
|
3924 |
/// This function returns the maximum value of the given graph map.
|
|
3925 |
/// The corresponding item set of the graph must not be empty.
|
|
3926 |
///
|
|
3927 |
/// \param gr The graph for which the map is defined.
|
|
3928 |
/// \param map The graph map.
|
|
3929 |
template <typename GR, typename Map>
|
|
3930 |
typename Map::Value mapMaxValue(const GR& gr, const Map& map) {
|
|
3931 |
return map[mapMax(gr, map, std::less<typename Map::Value>())];
|
|
3932 |
}
|
|
3933 |
|
|
3934 |
/// \brief Return the maximum value of a graph map.
|
|
3935 |
///
|
|
3936 |
/// This function returns the maximum value of the given graph map.
|
|
3937 |
/// The corresponding item set of the graph must not be empty.
|
|
3938 |
///
|
|
3939 |
/// \param gr The graph for which the map is defined.
|
|
3940 |
/// \param map The graph map.
|
|
3941 |
/// \param comp Comparison function object.
|
|
3942 |
template <typename GR, typename Map, typename Comp>
|
|
3943 |
typename Map::Value
|
|
3944 |
mapMaxValue(const GR& gr, const Map& map, const Comp& comp) {
|
|
3945 |
return map[mapMax(gr, map, comp)];
|
|
3946 |
}
|
|
3947 |
|
|
3948 |
/// \brief Return an item having a specified value in a graph map.
|
|
3949 |
///
|
|
3950 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having
|
|
3951 |
/// the specified assigned value in the given graph map.
|
|
3952 |
/// If no such item exists, it returns \c INVALID.
|
|
3953 |
///
|
|
3954 |
/// \param gr The graph for which the map is defined.
|
|
3955 |
/// \param map The graph map.
|
|
3956 |
/// \param val The value that have to be found.
|
|
3957 |
template <typename GR, typename Map>
|
|
3958 |
typename Map::Key
|
|
3959 |
mapFind(const GR& gr, const Map& map, const typename Map::Value& val) {
|
|
3960 |
typedef typename Map::Key Item;
|
|
3961 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
3962 |
|
|
3963 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
3964 |
if (map[it] == val) return it;
|
|
3965 |
}
|
|
3966 |
return INVALID;
|
|
3967 |
}
|
|
3968 |
|
|
3969 |
/// \brief Return an item having value for which a certain predicate is
|
|
3970 |
/// true in a graph map.
|
|
3971 |
///
|
|
3972 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having
|
|
3973 |
/// such assigned value for which the specified predicate is true
|
|
3974 |
/// in the given graph map.
|
|
3975 |
/// If no such item exists, it returns \c INVALID.
|
|
3976 |
///
|
|
3977 |
/// \param gr The graph for which the map is defined.
|
|
3978 |
/// \param map The graph map.
|
|
3979 |
/// \param pred The predicate function object.
|
|
3980 |
template <typename GR, typename Map, typename Pred>
|
|
3981 |
typename Map::Key
|
|
3982 |
mapFindIf(const GR& gr, const Map& map, const Pred& pred) {
|
|
3983 |
typedef typename Map::Key Item;
|
|
3984 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
3985 |
|
|
3986 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
3987 |
if (pred(map[it])) return it;
|
|
3988 |
}
|
|
3989 |
return INVALID;
|
|
3990 |
}
|
|
3991 |
|
|
3992 |
/// \brief Return the number of items having a specified value in a
|
|
3993 |
/// graph map.
|
|
3994 |
///
|
|
3995 |
/// This function returns the number of items (\c Node, \c Arc or \c Edge)
|
|
3996 |
/// having the specified assigned value in the given graph map.
|
|
3997 |
///
|
|
3998 |
/// \param gr The graph for which the map is defined.
|
|
3999 |
/// \param map The graph map.
|
|
4000 |
/// \param val The value that have to be counted.
|
|
4001 |
template <typename GR, typename Map>
|
|
4002 |
int mapCount(const GR& gr, const Map& map, const typename Map::Value& val) {
|
|
4003 |
typedef typename Map::Key Item;
|
|
4004 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
4005 |
|
|
4006 |
int cnt = 0;
|
|
4007 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
4008 |
if (map[it] == val) ++cnt;
|
|
4009 |
}
|
|
4010 |
return cnt;
|
|
4011 |
}
|
|
4012 |
|
|
4013 |
/// \brief Return the number of items having values for which a certain
|
|
4014 |
/// predicate is true in a graph map.
|
|
4015 |
///
|
|
4016 |
/// This function returns the number of items (\c Node, \c Arc or \c Edge)
|
|
4017 |
/// having such assigned values for which the specified predicate is true
|
|
4018 |
/// in the given graph map.
|
|
4019 |
///
|
|
4020 |
/// \param gr The graph for which the map is defined.
|
|
4021 |
/// \param map The graph map.
|
|
4022 |
/// \param pred The predicate function object.
|
|
4023 |
template <typename GR, typename Map, typename Pred>
|
|
4024 |
int mapCountIf(const GR& gr, const Map& map, const Pred& pred) {
|
|
4025 |
typedef typename Map::Key Item;
|
|
4026 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
4027 |
|
|
4028 |
int cnt = 0;
|
|
4029 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
4030 |
if (pred(map[it])) ++cnt;
|
|
4031 |
}
|
|
4032 |
return cnt;
|
|
4033 |
}
|
|
4034 |
|
|
4035 |
/// \brief Fill a graph map with a certain value.
|
|
4036 |
///
|
|
4037 |
/// This function sets the specified value for all items (\c Node,
|
|
4038 |
/// \c Arc or \c Edge) in the given graph map.
|
|
4039 |
///
|
|
4040 |
/// \param gr The graph for which the map is defined.
|
|
4041 |
/// \param map The graph map. It must conform to the
|
|
4042 |
/// \ref concepts::WriteMap "WriteMap" concept.
|
|
4043 |
/// \param val The value.
|
|
4044 |
template <typename GR, typename Map>
|
|
4045 |
void mapFill(const GR& gr, Map& map, const typename Map::Value& val) {
|
|
4046 |
typedef typename Map::Key Item;
|
|
4047 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
|
|
4048 |
|
|
4049 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
|
4050 |
map.set(it, val);
|
|
4051 |
}
|
|
4052 |
}
|
|
4053 |
|
3767 |
4054 |
/// @}
|
3768 |
4055 |
}
|
3769 |
4056 |
|
3770 |
4057 |
#endif // LEMON_MAPS_H
|