1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_MAPS_H |
20 | 20 |
#define LEMON_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
|
26 | 26 |
#include <lemon/bits/utility.h> |
27 | 27 |
// #include <lemon/bits/traits.h> |
28 | 28 |
|
29 | 29 |
///\file |
30 | 30 |
///\ingroup maps |
31 | 31 |
///\brief Miscellaneous property maps |
32 | 32 |
/// |
33 | 33 |
#include <map> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \addtogroup maps |
38 | 38 |
/// @{ |
39 | 39 |
|
40 | 40 |
/// Base class of maps. |
41 | 41 |
|
42 | 42 |
/// Base class of maps. |
43 | 43 |
/// It provides the necessary <tt>typedef</tt>s required by the map concept. |
44 | 44 |
template<typename K, typename T> |
45 | 45 |
class MapBase { |
46 | 46 |
public: |
47 | 47 |
/// The key type of the map. |
48 | 48 |
typedef K Key; |
49 | 49 |
/// The value type of the map. (The type of objects associated with the keys). |
50 | 50 |
typedef T Value; |
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 |
template<typename K, typename T> |
60 | 60 |
class NullMap : public MapBase<K, T> { |
61 | 61 |
public: |
62 | 62 |
typedef MapBase<K, T> Parent; |
63 | 63 |
typedef typename Parent::Key Key; |
64 | 64 |
typedef typename Parent::Value Value; |
65 | 65 |
|
66 | 66 |
/// Gives back a default constructed element. |
67 | 67 |
T operator[](const K&) const { return T(); } |
68 | 68 |
/// Absorbs the value. |
69 | 69 |
void set(const K&, const T&) {} |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
///Returns a \c NullMap class |
73 | 73 |
|
74 | 74 |
///This function just returns a \c NullMap class. |
75 | 75 |
///\relates NullMap |
76 | 76 |
template <typename K, typename V> |
77 | 77 |
NullMap<K, V> nullMap() { |
78 | 78 |
return NullMap<K, V>(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
|
82 | 82 |
/// Constant map. |
83 | 83 |
|
84 | 84 |
/// This is a \ref concepts::ReadMap "readable" map which assigns a |
85 | 85 |
/// specified value to each key. |
86 | 86 |
/// In other aspects it is equivalent to \c NullMap. |
87 | 87 |
template<typename K, typename T> |
88 | 88 |
class ConstMap : public MapBase<K, T> { |
89 | 89 |
private: |
90 | 90 |
T v; |
91 | 91 |
public: |
92 | 92 |
|
93 | 93 |
typedef MapBase<K, T> Parent; |
94 | 94 |
typedef typename Parent::Key Key; |
95 | 95 |
typedef typename Parent::Value Value; |
96 | 96 |
|
97 | 97 |
/// Default constructor |
98 | 98 |
|
99 | 99 |
/// Default constructor. |
100 | 100 |
/// The value of the map will be uninitialized. |
101 | 101 |
/// (More exactly it will be default constructed.) |
102 | 102 |
ConstMap() {} |
103 | 103 |
|
104 | 104 |
/// Constructor with specified initial value |
105 | 105 |
|
106 | 106 |
/// Constructor with specified initial value. |
107 | 107 |
/// \param _v is the initial value of the map. |
108 | 108 |
ConstMap(const T &_v) : v(_v) {} |
109 | 109 |
|
110 | 110 |
///\e |
111 | 111 |
T operator[](const K&) const { return v; } |
112 | 112 |
|
113 | 113 |
///\e |
114 | 114 |
void setAll(const T &t) { |
115 | 115 |
v = t; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
template<typename T1> |
119 | 119 |
ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {} |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
///Returns a \c ConstMap class |
123 | 123 |
|
124 | 124 |
///This function just returns a \c ConstMap class. |
125 | 125 |
///\relates ConstMap |
126 | 126 |
template<typename K, typename V> |
127 | 127 |
inline ConstMap<K, V> constMap(const V &v) { |
128 | 128 |
return ConstMap<K, V>(v); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
|
132 | 132 |
template<typename T, T v> |
133 | 133 |
struct Const { }; |
134 | 134 |
|
135 | 135 |
/// Constant map with inlined constant value. |
136 | 136 |
|
137 | 137 |
/// This is a \ref concepts::ReadMap "readable" map which assigns a |
138 | 138 |
/// specified value to each key. |
139 | 139 |
/// In other aspects it is equivalent to \c NullMap. |
140 | 140 |
template<typename K, typename V, V v> |
141 | 141 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
142 | 142 |
public: |
143 | 143 |
typedef MapBase<K, V> Parent; |
144 | 144 |
typedef typename Parent::Key Key; |
145 | 145 |
typedef typename Parent::Value Value; |
146 | 146 |
|
147 | 147 |
ConstMap() { } |
148 | 148 |
///\e |
149 | 149 |
V operator[](const K&) const { return v; } |
150 | 150 |
///\e |
151 | 151 |
void set(const K&, const V&) { } |
152 | 152 |
}; |
153 | 153 |
|
154 | 154 |
///Returns a \c ConstMap class with inlined value |
155 | 155 |
|
156 | 156 |
///This function just returns a \c ConstMap class with inlined value. |
157 | 157 |
///\relates ConstMap |
158 | 158 |
template<typename K, typename V, V v> |
159 | 159 |
inline ConstMap<K, Const<V, v> > constMap() { |
160 | 160 |
return ConstMap<K, Const<V, v> >(); |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
///Map based on \c std::map |
164 | 164 |
|
165 | 165 |
///This is essentially a wrapper for \c std::map with addition that |
166 | 166 |
///you can specify a default value different from \c Value(). |
167 | 167 |
///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept. |
168 | 168 |
template <typename K, typename T, typename Compare = std::less<K> > |
169 | 169 |
class StdMap : public MapBase<K, T> { |
170 | 170 |
template <typename K1, typename T1, typename C1> |
171 | 171 |
friend class StdMap; |
172 | 172 |
public: |
173 | 173 |
|
174 | 174 |
typedef MapBase<K, T> Parent; |
175 | 175 |
///Key type |
176 | 176 |
typedef typename Parent::Key Key; |
177 | 177 |
///Value type |
178 | 178 |
typedef typename Parent::Value Value; |
179 | 179 |
///Reference Type |
180 | 180 |
typedef T& Reference; |
181 | 181 |
///Const reference type |
182 | 182 |
typedef const T& ConstReference; |
183 | 183 |
|
184 | 184 |
typedef True ReferenceMapTag; |
185 | 185 |
|
186 | 186 |
private: |
187 | 187 |
|
188 | 188 |
typedef std::map<K, T, Compare> Map; |
189 | 189 |
Value _value; |
190 | 190 |
Map _map; |
191 | 191 |
|
192 | 192 |
public: |
193 | 193 |
|
194 | 194 |
/// Constructor with specified default value |
195 | 195 |
StdMap(const T& value = T()) : _value(value) {} |
196 | 196 |
/// \brief Constructs the map from an appropriate \c std::map, and |
197 | 197 |
/// explicitly specifies a default value. |
198 | 198 |
template <typename T1, typename Comp1> |
199 | 199 |
StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) |
200 | 200 |
: _map(map.begin(), map.end()), _value(value) {} |
201 | 201 |
|
202 | 202 |
/// \brief Constructs a map from an other \ref StdMap. |
203 | 203 |
template<typename T1, typename Comp1> |
204 | 204 |
StdMap(const StdMap<Key, T1, Comp1> &c) |
205 | 205 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {} |
206 | 206 |
|
207 | 207 |
private: |
208 | 208 |
|
209 | 209 |
StdMap& operator=(const StdMap&); |
210 | 210 |
|
211 | 211 |
public: |
212 | 212 |
|
213 | 213 |
///\e |
214 | 214 |
Reference operator[](const Key &k) { |
215 | 215 |
typename Map::iterator it = _map.lower_bound(k); |
216 | 216 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
217 | 217 |
return it->second; |
218 | 218 |
else |
219 | 219 |
return _map.insert(it, std::make_pair(k, _value))->second; |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
/// \e |
223 | 223 |
ConstReference operator[](const Key &k) const { |
224 | 224 |
typename Map::const_iterator it = _map.find(k); |
225 | 225 |
if (it != _map.end()) |
226 | 226 |
return it->second; |
227 | 227 |
else |
228 | 228 |
return _value; |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
/// \e |
232 | 232 |
void set(const Key &k, const T &t) { |
233 | 233 |
typename Map::iterator it = _map.lower_bound(k); |
234 | 234 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
235 | 235 |
it->second = t; |
236 | 236 |
else |
237 | 237 |
_map.insert(it, std::make_pair(k, t)); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
/// \e |
241 | 241 |
void setAll(const T &t) { |
242 | 242 |
_value = t; |
243 | 243 |
_map.clear(); |
244 | 244 |
} |
245 | 245 |
|
246 | 246 |
}; |
247 | 247 |
|
248 | 248 |
///Returns a \c StdMap class |
249 | 249 |
|
250 | 250 |
///This function just returns a \c StdMap class with specified |
251 | 251 |
///default value. |
252 | 252 |
///\relates StdMap |
253 |
template<typename K, typename V, typename Compare |
|
253 |
template<typename K, typename V, typename Compare> |
|
254 | 254 |
inline StdMap<K, V, Compare> stdMap(const V& value = V()) { |
255 | 255 |
return StdMap<K, V, Compare>(value); |
256 | 256 |
} |
257 | 257 |
|
258 |
///Returns a \c StdMap class |
|
259 |
|
|
260 |
///This function just returns a \c StdMap class with specified |
|
261 |
///default value. |
|
262 |
///\relates StdMap |
|
263 |
template<typename K, typename V> |
|
264 |
inline StdMap<K, V, std::less<K> > stdMap(const V& value = V()) { |
|
265 |
return StdMap<K, V, std::less<K> >(value); |
|
266 |
} |
|
267 |
|
|
258 | 268 |
///Returns a \c StdMap class created from an appropriate std::map |
259 | 269 |
|
260 | 270 |
///This function just returns a \c StdMap class created from an |
261 | 271 |
///appropriate std::map. |
262 | 272 |
///\relates StdMap |
263 |
template<typename K, typename V, typename Compare |
|
273 |
template<typename K, typename V, typename Compare> |
|
264 | 274 |
inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, |
265 | 275 |
const V& value = V() ) { |
266 | 276 |
return StdMap<K, V, Compare>(map, value); |
267 | 277 |
} |
268 | 278 |
|
279 |
///Returns a \c StdMap class created from an appropriate std::map |
|
280 |
|
|
281 |
///This function just returns a \c StdMap class created from an |
|
282 |
///appropriate std::map. |
|
283 |
///\relates StdMap |
|
284 |
template<typename K, typename V> |
|
285 |
inline StdMap<K, V, std::less<K> > stdMap( const std::map<K, V, std::less<K> > &map, |
|
286 |
const V& value = V() ) { |
|
287 |
return StdMap<K, V, std::less<K> >(map, value); |
|
288 |
} |
|
289 |
|
|
269 | 290 |
/// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt> |
270 | 291 |
/// |
271 | 292 |
/// This map has the <tt>[0..size-1]</tt> keyset and the values |
272 | 293 |
/// are stored in a \c std::vector<T> container. It can be used with |
273 | 294 |
/// some data structures, for example \c UnionFind, \c BinHeap, when |
274 | 295 |
/// the used items are small integer numbers. |
275 | 296 |
/// This map meets the \ref concepts::ReferenceMap "ReferenceMap" concept. |
276 | 297 |
/// |
277 | 298 |
/// \todo Revise its name |
278 | 299 |
template <typename T> |
279 | 300 |
class IntegerMap : public MapBase<int, T> { |
280 | 301 |
|
281 | 302 |
template <typename T1> |
282 | 303 |
friend class IntegerMap; |
283 | 304 |
|
284 | 305 |
public: |
285 | 306 |
|
286 | 307 |
typedef MapBase<int, T> Parent; |
287 | 308 |
///\e |
288 | 309 |
typedef typename Parent::Key Key; |
289 | 310 |
///\e |
290 | 311 |
typedef typename Parent::Value Value; |
291 | 312 |
///\e |
292 | 313 |
typedef T& Reference; |
293 | 314 |
///\e |
294 | 315 |
typedef const T& ConstReference; |
295 | 316 |
|
296 | 317 |
typedef True ReferenceMapTag; |
297 | 318 |
|
298 | 319 |
private: |
299 | 320 |
|
300 | 321 |
typedef std::vector<T> Vector; |
301 | 322 |
Vector _vector; |
302 | 323 |
|
303 | 324 |
public: |
304 | 325 |
|
305 | 326 |
/// Constructor with specified default value |
306 | 327 |
IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} |
307 | 328 |
|
308 | 329 |
/// \brief Constructs the map from an appropriate \c std::vector. |
309 | 330 |
template <typename T1> |
310 | 331 |
IntegerMap(const std::vector<T1>& vector) |
311 | 332 |
: _vector(vector.begin(), vector.end()) {} |
312 | 333 |
|
313 | 334 |
/// \brief Constructs a map from an other \ref IntegerMap. |
314 | 335 |
template <typename T1> |
315 | 336 |
IntegerMap(const IntegerMap<T1> &c) |
316 | 337 |
: _vector(c._vector.begin(), c._vector.end()) {} |
317 | 338 |
|
318 | 339 |
/// \brief Resize the container |
319 | 340 |
void resize(int size, const T& value = T()) { |
320 | 341 |
_vector.resize(size, value); |
321 | 342 |
} |
322 | 343 |
|
323 | 344 |
private: |
324 | 345 |
|
325 | 346 |
IntegerMap& operator=(const IntegerMap&); |
326 | 347 |
|
327 | 348 |
public: |
328 | 349 |
|
329 | 350 |
///\e |
330 | 351 |
Reference operator[](Key k) { |
331 | 352 |
return _vector[k]; |
332 | 353 |
} |
333 | 354 |
|
334 | 355 |
/// \e |
335 | 356 |
ConstReference operator[](Key k) const { |
336 | 357 |
return _vector[k]; |
337 | 358 |
} |
338 | 359 |
|
339 | 360 |
/// \e |
340 | 361 |
void set(const Key &k, const T& t) { |
341 | 362 |
_vector[k] = t; |
342 | 363 |
} |
343 | 364 |
|
344 | 365 |
}; |
345 | 366 |
|
346 | 367 |
///Returns an \c IntegerMap class |
347 | 368 |
|
348 | 369 |
///This function just returns an \c IntegerMap class. |
349 | 370 |
///\relates IntegerMap |
350 | 371 |
template<typename T> |
351 | 372 |
inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) { |
352 | 373 |
return IntegerMap<T>(size, value); |
353 | 374 |
} |
354 | 375 |
|
355 | 376 |
/// @} |
356 | 377 |
|
357 | 378 |
/// \addtogroup map_adaptors |
358 | 379 |
/// @{ |
359 | 380 |
|
360 | 381 |
/// \brief Identity map. |
361 | 382 |
/// |
362 | 383 |
/// This map gives back the given key as value without any |
363 | 384 |
/// modification. |
364 | 385 |
template <typename T> |
365 | 386 |
class IdentityMap : public MapBase<T, T> { |
366 | 387 |
public: |
367 | 388 |
typedef MapBase<T, T> Parent; |
368 | 389 |
typedef typename Parent::Key Key; |
369 | 390 |
typedef typename Parent::Value Value; |
370 | 391 |
|
371 | 392 |
/// \e |
372 | 393 |
const T& operator[](const T& t) const { |
373 | 394 |
return t; |
374 | 395 |
} |
375 | 396 |
}; |
376 | 397 |
|
377 | 398 |
///Returns an \c IdentityMap class |
378 | 399 |
|
379 | 400 |
///This function just returns an \c IdentityMap class. |
380 | 401 |
///\relates IdentityMap |
381 | 402 |
template<typename T> |
382 | 403 |
inline IdentityMap<T> identityMap() { |
383 | 404 |
return IdentityMap<T>(); |
384 | 405 |
} |
385 | 406 |
|
386 | 407 |
|
387 | 408 |
///\brief Convert the \c Value of a map to another type using |
388 | 409 |
///the default conversion. |
389 | 410 |
/// |
390 | 411 |
///This \ref concepts::ReadMap "read only map" |
391 | 412 |
///converts the \c Value of a map to type \c T. |
392 | 413 |
///Its \c Key is inherited from \c M. |
393 | 414 |
template <typename M, typename T> |
394 | 415 |
class ConvertMap : public MapBase<typename M::Key, T> { |
395 | 416 |
const M& m; |
396 | 417 |
public: |
397 | 418 |
typedef MapBase<typename M::Key, T> Parent; |
398 | 419 |
typedef typename Parent::Key Key; |
399 | 420 |
typedef typename Parent::Value Value; |
400 | 421 |
|
401 | 422 |
///Constructor |
402 | 423 |
|
403 | 424 |
///Constructor. |
404 | 425 |
///\param _m is the underlying map. |
405 | 426 |
ConvertMap(const M &_m) : m(_m) {}; |
406 | 427 |
|
407 | 428 |
///\e |
408 | 429 |
Value operator[](const Key& k) const {return m[k];} |
409 | 430 |
}; |
410 | 431 |
|
411 | 432 |
///Returns a \c ConvertMap class |
412 | 433 |
|
413 | 434 |
///This function just returns a \c ConvertMap class. |
414 | 435 |
///\relates ConvertMap |
415 | 436 |
template<typename T, typename M> |
416 | 437 |
inline ConvertMap<M, T> convertMap(const M &m) { |
417 | 438 |
return ConvertMap<M, T>(m); |
418 | 439 |
} |
419 | 440 |
|
420 | 441 |
///Simple wrapping of a map |
421 | 442 |
|
422 | 443 |
///This \ref concepts::ReadMap "read only map" returns the simple |
423 | 444 |
///wrapping of the given map. Sometimes the reference maps cannot be |
424 | 445 |
///combined with simple read maps. This map adaptor wraps the given |
425 | 446 |
///map to simple read map. |
426 | 447 |
/// |
427 | 448 |
///\sa SimpleWriteMap |
428 | 449 |
/// |
429 | 450 |
/// \todo Revise the misleading name |
430 | 451 |
template<typename M> |
431 | 452 |
class SimpleMap : public MapBase<typename M::Key, typename M::Value> { |
432 | 453 |
const M& m; |
433 | 454 |
|
434 | 455 |
public: |
435 | 456 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
436 | 457 |
typedef typename Parent::Key Key; |
437 | 458 |
typedef typename Parent::Value Value; |
438 | 459 |
|
439 | 460 |
///Constructor |
440 | 461 |
SimpleMap(const M &_m) : m(_m) {}; |
441 | 462 |
///\e |
442 | 463 |
Value operator[](Key k) const {return m[k];} |
443 | 464 |
}; |
444 | 465 |
|
445 | 466 |
///Returns a \c SimpleMap class |
446 | 467 |
|
447 | 468 |
///This function just returns a \c SimpleMap class. |
448 | 469 |
///\relates SimpleMap |
449 | 470 |
template<typename M> |
450 | 471 |
inline SimpleMap<M> simpleMap(const M &m) { |
451 | 472 |
return SimpleMap<M>(m); |
452 | 473 |
} |
453 | 474 |
|
454 | 475 |
///Simple writable wrapping of a map |
455 | 476 |
|
456 | 477 |
///This \ref concepts::ReadWriteMap "read-write map" returns the simple |
457 | 478 |
///wrapping of the given map. Sometimes the reference maps cannot be |
458 | 479 |
///combined with simple read-write maps. This map adaptor wraps the |
459 | 480 |
///given map to simple read-write map. |
460 | 481 |
/// |
461 | 482 |
///\sa SimpleMap |
462 | 483 |
/// |
463 | 484 |
/// \todo Revise the misleading name |
464 | 485 |
template<typename M> |
465 | 486 |
class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
466 | 487 |
M& m; |
467 | 488 |
|
468 | 489 |
public: |
469 | 490 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
470 | 491 |
typedef typename Parent::Key Key; |
471 | 492 |
typedef typename Parent::Value Value; |
472 | 493 |
|
473 | 494 |
///Constructor |
474 | 495 |
SimpleWriteMap(M &_m) : m(_m) {}; |
475 | 496 |
///\e |
476 | 497 |
Value operator[](Key k) const {return m[k];} |
477 | 498 |
///\e |
478 | 499 |
void set(Key k, const Value& c) { m.set(k, c); } |
479 | 500 |
}; |
480 | 501 |
|
481 | 502 |
///Returns a \c SimpleWriteMap class |
482 | 503 |
|
483 | 504 |
///This function just returns a \c SimpleWriteMap class. |
484 | 505 |
///\relates SimpleWriteMap |
485 | 506 |
template<typename M> |
486 | 507 |
inline SimpleWriteMap<M> simpleWriteMap(M &m) { |
487 | 508 |
return SimpleWriteMap<M>(m); |
488 | 509 |
} |
489 | 510 |
|
490 | 511 |
///Sum of two maps |
491 | 512 |
|
492 | 513 |
///This \ref concepts::ReadMap "read only map" returns the sum of the two |
493 | 514 |
///given maps. |
494 | 515 |
///Its \c Key and \c Value are inherited from \c M1. |
495 | 516 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
496 | 517 |
template<typename M1, typename M2> |
497 | 518 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
498 | 519 |
const M1& m1; |
499 | 520 |
const M2& m2; |
500 | 521 |
|
501 | 522 |
public: |
502 | 523 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
503 | 524 |
typedef typename Parent::Key Key; |
504 | 525 |
typedef typename Parent::Value Value; |
505 | 526 |
|
506 | 527 |
///Constructor |
507 | 528 |
AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
508 | 529 |
///\e |
509 | 530 |
Value operator[](Key k) const {return m1[k]+m2[k];} |
510 | 531 |
}; |
511 | 532 |
|
512 | 533 |
///Returns an \c AddMap class |
513 | 534 |
|
514 | 535 |
///This function just returns an \c AddMap class. |
515 | 536 |
///\todo Extend the documentation: how to call these type of functions? |
516 | 537 |
/// |
517 | 538 |
///\relates AddMap |
518 | 539 |
template<typename M1, typename M2> |
519 | 540 |
inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) { |
520 | 541 |
return AddMap<M1, M2>(m1,m2); |
521 | 542 |
} |
522 | 543 |
|
523 | 544 |
///Shift a map with a constant. |
524 | 545 |
|
525 | 546 |
///This \ref concepts::ReadMap "read only map" returns the sum of the |
526 | 547 |
///given map and a constant value. |
527 | 548 |
///Its \c Key and \c Value are inherited from \c M. |
528 | 549 |
/// |
529 | 550 |
///Actually, |
530 | 551 |
///\code |
531 | 552 |
/// ShiftMap<X> sh(x,v); |
532 | 553 |
///\endcode |
533 | 554 |
///is equivalent to |
534 | 555 |
///\code |
535 | 556 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
536 | 557 |
/// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
537 | 558 |
///\endcode |
538 | 559 |
/// |
539 | 560 |
///\sa ShiftWriteMap |
540 | 561 |
template<typename M, typename C = typename M::Value> |
541 | 562 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
542 | 563 |
const M& m; |
543 | 564 |
C v; |
544 | 565 |
public: |
545 | 566 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
546 | 567 |
typedef typename Parent::Key Key; |
547 | 568 |
typedef typename Parent::Value Value; |
548 | 569 |
|
549 | 570 |
///Constructor |
550 | 571 |
|
551 | 572 |
///Constructor. |
552 | 573 |
///\param _m is the undelying map. |
553 | 574 |
///\param _v is the shift value. |
554 | 575 |
ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
555 | 576 |
///\e |
556 | 577 |
Value operator[](Key k) const {return m[k] + v;} |
557 | 578 |
}; |
558 | 579 |
|
559 | 580 |
///Shift a map with a constant (ReadWrite version). |
560 | 581 |
|
561 | 582 |
///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the |
562 | 583 |
///given map and a constant value. It makes also possible to write the map. |
563 | 584 |
///Its \c Key and \c Value are inherited from \c M. |
564 | 585 |
/// |
565 | 586 |
///\sa ShiftMap |
566 | 587 |
template<typename M, typename C = typename M::Value> |
567 | 588 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
568 | 589 |
M& m; |
569 | 590 |
C v; |
570 | 591 |
public: |
571 | 592 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
572 | 593 |
typedef typename Parent::Key Key; |
573 | 594 |
typedef typename Parent::Value Value; |
574 | 595 |
|
575 | 596 |
///Constructor |
576 | 597 |
|
577 | 598 |
///Constructor. |
578 | 599 |
///\param _m is the undelying map. |
579 | 600 |
///\param _v is the shift value. |
580 | 601 |
ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
581 | 602 |
/// \e |
582 | 603 |
Value operator[](Key k) const {return m[k] + v;} |
583 | 604 |
/// \e |
584 | 605 |
void set(Key k, const Value& c) { m.set(k, c - v); } |
585 | 606 |
}; |
586 | 607 |
|
587 | 608 |
///Returns a \c ShiftMap class |
588 | 609 |
|
589 | 610 |
///This function just returns a \c ShiftMap class. |
590 | 611 |
///\relates ShiftMap |
591 | 612 |
template<typename M, typename C> |
592 | 613 |
inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { |
593 | 614 |
return ShiftMap<M, C>(m,v); |
594 | 615 |
} |
595 | 616 |
|
596 | 617 |
///Returns a \c ShiftWriteMap class |
597 | 618 |
|
598 | 619 |
///This function just returns a \c ShiftWriteMap class. |
599 | 620 |
///\relates ShiftWriteMap |
600 | 621 |
template<typename M, typename C> |
601 | 622 |
inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { |
602 | 623 |
return ShiftWriteMap<M, C>(m,v); |
603 | 624 |
} |
604 | 625 |
|
605 | 626 |
///Difference of two maps |
606 | 627 |
|
607 | 628 |
///This \ref concepts::ReadMap "read only map" returns the difference |
608 | 629 |
///of the values of the two given maps. |
609 | 630 |
///Its \c Key and \c Value are inherited from \c M1. |
610 | 631 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
611 | 632 |
/// |
612 | 633 |
/// \todo Revise the misleading name |
613 | 634 |
template<typename M1, typename M2> |
614 | 635 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
615 | 636 |
const M1& m1; |
616 | 637 |
const M2& m2; |
617 | 638 |
public: |
618 | 639 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
619 | 640 |
typedef typename Parent::Key Key; |
620 | 641 |
typedef typename Parent::Value Value; |
621 | 642 |
|
622 | 643 |
///Constructor |
623 | 644 |
SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
624 | 645 |
/// \e |
625 | 646 |
Value operator[](Key k) const {return m1[k]-m2[k];} |
626 | 647 |
}; |
627 | 648 |
|
628 | 649 |
///Returns a \c SubMap class |
629 | 650 |
|
630 | 651 |
///This function just returns a \c SubMap class. |
631 | 652 |
/// |
632 | 653 |
///\relates SubMap |
633 | 654 |
template<typename M1, typename M2> |
634 | 655 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
635 | 656 |
return SubMap<M1, M2>(m1, m2); |
636 | 657 |
} |
637 | 658 |
|
638 | 659 |
///Product of two maps |
639 | 660 |
|
640 | 661 |
///This \ref concepts::ReadMap "read only map" returns the product of the |
641 | 662 |
///values of the two given maps. |
642 | 663 |
///Its \c Key and \c Value are inherited from \c M1. |
643 | 664 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
644 | 665 |
template<typename M1, typename M2> |
645 | 666 |
class MulMap : public MapBase<typename M1::Key, typename M1::Value> { |
646 | 667 |
const M1& m1; |
647 | 668 |
const M2& m2; |
648 | 669 |
public: |
649 | 670 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
650 | 671 |
typedef typename Parent::Key Key; |
651 | 672 |
typedef typename Parent::Value Value; |
652 | 673 |
|
653 | 674 |
///Constructor |
654 | 675 |
MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
655 | 676 |
/// \e |
656 | 677 |
Value operator[](Key k) const {return m1[k]*m2[k];} |
657 | 678 |
}; |
658 | 679 |
|
659 | 680 |
///Returns a \c MulMap class |
660 | 681 |
|
661 | 682 |
///This function just returns a \c MulMap class. |
662 | 683 |
///\relates MulMap |
663 | 684 |
template<typename M1, typename M2> |
664 | 685 |
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
665 | 686 |
return MulMap<M1, M2>(m1,m2); |
666 | 687 |
} |
667 | 688 |
|
668 | 689 |
///Scales a map with a constant. |
669 | 690 |
|
670 | 691 |
///This \ref concepts::ReadMap "read only map" returns the value of the |
671 | 692 |
///given map multiplied from the left side with a constant value. |
672 | 693 |
///Its \c Key and \c Value are inherited from \c M. |
673 | 694 |
/// |
674 | 695 |
///Actually, |
675 | 696 |
///\code |
676 | 697 |
/// ScaleMap<X> sc(x,v); |
677 | 698 |
///\endcode |
678 | 699 |
///is equivalent to |
679 | 700 |
///\code |
680 | 701 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
681 | 702 |
/// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
682 | 703 |
///\endcode |
683 | 704 |
/// |
684 | 705 |
///\sa ScaleWriteMap |
685 | 706 |
template<typename M, typename C = typename M::Value> |
686 | 707 |
class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
687 | 708 |
const M& m; |
688 | 709 |
C v; |
689 | 710 |
public: |
690 | 711 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
691 | 712 |
typedef typename Parent::Key Key; |
692 | 713 |
typedef typename Parent::Value Value; |
693 | 714 |
|
694 | 715 |
///Constructor |
695 | 716 |
|
696 | 717 |
///Constructor. |
697 | 718 |
///\param _m is the undelying map. |
698 | 719 |
///\param _v is the scaling value. |
699 | 720 |
ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
700 | 721 |
/// \e |
701 | 722 |
Value operator[](Key k) const {return v * m[k];} |
702 | 723 |
}; |
703 | 724 |
|
704 | 725 |
///Scales a map with a constant (ReadWrite version). |
705 | 726 |
|
706 | 727 |
///This \ref concepts::ReadWriteMap "read-write map" returns the value of the |
707 | 728 |
///given map multiplied from the left side with a constant value. It can |
708 | 729 |
///also be used as write map if the \c / operator is defined between |
709 | 730 |
///\c Value and \c C and the given multiplier is not zero. |
710 | 731 |
///Its \c Key and \c Value are inherited from \c M. |
711 | 732 |
/// |
712 | 733 |
///\sa ScaleMap |
713 | 734 |
template<typename M, typename C = typename M::Value> |
714 | 735 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
715 | 736 |
M& m; |
716 | 737 |
C v; |
717 | 738 |
public: |
718 | 739 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
719 | 740 |
typedef typename Parent::Key Key; |
720 | 741 |
typedef typename Parent::Value Value; |
721 | 742 |
|
722 | 743 |
///Constructor |
723 | 744 |
|
724 | 745 |
///Constructor. |
725 | 746 |
///\param _m is the undelying map. |
726 | 747 |
///\param _v is the scaling value. |
727 | 748 |
ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
728 | 749 |
/// \e |
729 | 750 |
Value operator[](Key k) const {return v * m[k];} |
730 | 751 |
/// \e |
731 | 752 |
void set(Key k, const Value& c) { m.set(k, c / v);} |
732 | 753 |
}; |
733 | 754 |
|
734 | 755 |
///Returns a \c ScaleMap class |
735 | 756 |
|
736 | 757 |
///This function just returns a \c ScaleMap class. |
737 | 758 |
///\relates ScaleMap |
738 | 759 |
template<typename M, typename C> |
739 | 760 |
inline ScaleMap<M, C> scaleMap(const M &m,const C &v) { |
740 | 761 |
return ScaleMap<M, C>(m,v); |
741 | 762 |
} |
742 | 763 |
|
743 | 764 |
///Returns a \c ScaleWriteMap class |
744 | 765 |
|
745 | 766 |
///This function just returns a \c ScaleWriteMap class. |
746 | 767 |
///\relates ScaleWriteMap |
747 | 768 |
template<typename M, typename C> |
748 | 769 |
inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) { |
749 | 770 |
return ScaleWriteMap<M, C>(m,v); |
750 | 771 |
} |
751 | 772 |
|
752 | 773 |
///Quotient of two maps |
753 | 774 |
|
754 | 775 |
///This \ref concepts::ReadMap "read only map" returns the quotient of the |
755 | 776 |
///values of the two given maps. |
756 | 777 |
///Its \c Key and \c Value are inherited from \c M1. |
757 | 778 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
758 | 779 |
template<typename M1, typename M2> |
759 | 780 |
class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
760 | 781 |
const M1& m1; |
761 | 782 |
const M2& m2; |
762 | 783 |
public: |
763 | 784 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
764 | 785 |
typedef typename Parent::Key Key; |
765 | 786 |
typedef typename Parent::Value Value; |
766 | 787 |
|
767 | 788 |
///Constructor |
768 | 789 |
DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
769 | 790 |
/// \e |
770 | 791 |
Value operator[](Key k) const {return m1[k]/m2[k];} |
771 | 792 |
}; |
772 | 793 |
|
773 | 794 |
///Returns a \c DivMap class |
774 | 795 |
|
775 | 796 |
///This function just returns a \c DivMap class. |
776 | 797 |
///\relates DivMap |
777 | 798 |
template<typename M1, typename M2> |
778 | 799 |
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) { |
779 | 800 |
return DivMap<M1, M2>(m1,m2); |
780 | 801 |
} |
781 | 802 |
|
782 | 803 |
///Composition of two maps |
783 | 804 |
|
784 | 805 |
///This \ref concepts::ReadMap "read only map" returns the composition of |
785 | 806 |
///two given maps. |
786 | 807 |
///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2, |
787 | 808 |
///then for |
788 | 809 |
///\code |
789 | 810 |
/// ComposeMap<M1, M2> cm(m1,m2); |
790 | 811 |
///\endcode |
791 | 812 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
792 | 813 |
/// |
793 | 814 |
///Its \c Key is inherited from \c M2 and its \c Value is from \c M1. |
794 | 815 |
///\c M2::Value must be convertible to \c M1::Key. |
795 | 816 |
/// |
796 | 817 |
///\sa CombineMap |
797 | 818 |
/// |
798 | 819 |
///\todo Check the requirements. |
799 | 820 |
template <typename M1, typename M2> |
800 | 821 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
801 | 822 |
const M1& m1; |
802 | 823 |
const M2& m2; |
803 | 824 |
public: |
804 | 825 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
805 | 826 |
typedef typename Parent::Key Key; |
806 | 827 |
typedef typename Parent::Value Value; |
807 | 828 |
|
808 | 829 |
///Constructor |
809 | 830 |
ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
810 | 831 |
|
811 | 832 |
/// \e |
812 | 833 |
|
813 | 834 |
|
814 | 835 |
/// \todo Use the MapTraits once it is ported. |
815 | 836 |
/// |
816 | 837 |
|
817 | 838 |
//typename MapTraits<M1>::ConstReturnValue |
818 | 839 |
typename M1::Value |
819 | 840 |
operator[](Key k) const {return m1[m2[k]];} |
820 | 841 |
}; |
821 | 842 |
|
822 | 843 |
///Returns a \c ComposeMap class |
823 | 844 |
|
824 | 845 |
///This function just returns a \c ComposeMap class. |
825 | 846 |
///\relates ComposeMap |
826 | 847 |
template <typename M1, typename M2> |
827 | 848 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) { |
828 | 849 |
return ComposeMap<M1, M2>(m1,m2); |
829 | 850 |
} |
830 | 851 |
|
831 | 852 |
///Combine of two maps using an STL (binary) functor. |
832 | 853 |
|
833 | 854 |
///Combine of two maps using an STL (binary) functor. |
834 | 855 |
/// |
835 | 856 |
///This \ref concepts::ReadMap "read only map" takes two maps and a |
836 | 857 |
///binary functor and returns the composition of the two |
837 | 858 |
///given maps unsing the functor. |
838 | 859 |
///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
839 | 860 |
///and \c f is of \c F, then for |
840 | 861 |
///\code |
841 | 862 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
842 | 863 |
///\endcode |
843 | 864 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
844 | 865 |
/// |
845 | 866 |
///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
846 | 867 |
///\c M2::Value and \c M1::Value must be convertible to the corresponding |
847 | 868 |
///input parameter of \c F and the return type of \c F must be convertible |
848 | 869 |
///to \c V. |
849 | 870 |
/// |
850 | 871 |
///\sa ComposeMap |
851 | 872 |
/// |
852 | 873 |
///\todo Check the requirements. |
853 | 874 |
template<typename M1, typename M2, typename F, |
854 | 875 |
typename V = typename F::result_type> |
855 | 876 |
class CombineMap : public MapBase<typename M1::Key, V> { |
856 | 877 |
const M1& m1; |
857 | 878 |
const M2& m2; |
858 | 879 |
F f; |
859 | 880 |
public: |
860 | 881 |
typedef MapBase<typename M1::Key, V> Parent; |
861 | 882 |
typedef typename Parent::Key Key; |
862 | 883 |
typedef typename Parent::Value Value; |
863 | 884 |
|
864 | 885 |
///Constructor |
865 | 886 |
CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F()) |
866 | 887 |
: m1(_m1), m2(_m2), f(_f) {}; |
867 | 888 |
/// \e |
868 | 889 |
Value operator[](Key k) const {return f(m1[k],m2[k]);} |
869 | 890 |
}; |
870 | 891 |
|
871 | 892 |
///Returns a \c CombineMap class |
872 | 893 |
|
873 | 894 |
///This function just returns a \c CombineMap class. |
874 | 895 |
/// |
875 | 896 |
///For example if \c m1 and \c m2 are both \c double valued maps, then |
876 | 897 |
///\code |
877 | 898 |
///combineMap(m1,m2,std::plus<double>()) |
878 | 899 |
///\endcode |
879 | 900 |
///is equivalent to |
880 | 901 |
///\code |
881 | 902 |
///addMap(m1,m2) |
882 | 903 |
///\endcode |
883 | 904 |
/// |
884 | 905 |
///This function is specialized for adaptable binary function |
885 | 906 |
///classes and C++ functions. |
886 | 907 |
/// |
887 | 908 |
///\relates CombineMap |
888 | 909 |
template<typename M1, typename M2, typename F, typename V> |
889 | 910 |
inline CombineMap<M1, M2, F, V> |
890 | 911 |
combineMap(const M1& m1,const M2& m2, const F& f) { |
891 | 912 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
892 | 913 |
} |
893 | 914 |
|
894 | 915 |
template<typename M1, typename M2, typename F> |
895 | 916 |
inline CombineMap<M1, M2, F, typename F::result_type> |
896 | 917 |
combineMap(const M1& m1, const M2& m2, const F& f) { |
897 | 918 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
898 | 919 |
} |
899 | 920 |
|
900 | 921 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
901 | 922 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
902 | 923 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
903 | 924 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
904 | 925 |
} |
905 | 926 |
|
906 | 927 |
///Negative value of a map |
907 | 928 |
|
908 | 929 |
///This \ref concepts::ReadMap "read only map" returns the negative |
909 | 930 |
///value of the value returned by the given map. |
910 | 931 |
///Its \c Key and \c Value are inherited from \c M. |
911 | 932 |
///The unary \c - operator must be defined for \c Value, of course. |
912 | 933 |
/// |
913 | 934 |
///\sa NegWriteMap |
914 | 935 |
template<typename M> |
915 | 936 |
class NegMap : public MapBase<typename M::Key, typename M::Value> { |
916 | 937 |
const M& m; |
917 | 938 |
public: |
918 | 939 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
919 | 940 |
typedef typename Parent::Key Key; |
920 | 941 |
typedef typename Parent::Value Value; |
921 | 942 |
|
922 | 943 |
///Constructor |
923 | 944 |
NegMap(const M &_m) : m(_m) {}; |
924 | 945 |
/// \e |
925 | 946 |
Value operator[](Key k) const {return -m[k];} |
926 | 947 |
}; |
927 | 948 |
|
928 | 949 |
///Negative value of a map (ReadWrite version) |
929 | 950 |
|
930 | 951 |
///This \ref concepts::ReadWriteMap "read-write map" returns the negative |
931 | 952 |
///value of the value returned by the given map. |
932 | 953 |
///Its \c Key and \c Value are inherited from \c M. |
933 | 954 |
///The unary \c - operator must be defined for \c Value, of course. |
934 | 955 |
/// |
935 | 956 |
/// \sa NegMap |
936 | 957 |
template<typename M> |
937 | 958 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
938 | 959 |
M& m; |
939 | 960 |
public: |
940 | 961 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
941 | 962 |
typedef typename Parent::Key Key; |
942 | 963 |
typedef typename Parent::Value Value; |
943 | 964 |
|
944 | 965 |
///Constructor |
945 | 966 |
NegWriteMap(M &_m) : m(_m) {}; |
946 | 967 |
/// \e |
947 | 968 |
Value operator[](Key k) const {return -m[k];} |
948 | 969 |
/// \e |
949 | 970 |
void set(Key k, const Value& v) { m.set(k, -v); } |
950 | 971 |
}; |
951 | 972 |
|
952 | 973 |
///Returns a \c NegMap class |
953 | 974 |
|
954 | 975 |
///This function just returns a \c NegMap class. |
955 | 976 |
///\relates NegMap |
956 | 977 |
template <typename M> |
957 | 978 |
inline NegMap<M> negMap(const M &m) { |
958 | 979 |
return NegMap<M>(m); |
959 | 980 |
} |
960 | 981 |
|
961 | 982 |
///Returns a \c NegWriteMap class |
962 | 983 |
|
963 | 984 |
///This function just returns a \c NegWriteMap class. |
964 | 985 |
///\relates NegWriteMap |
965 | 986 |
template <typename M> |
966 | 987 |
inline NegWriteMap<M> negMap(M &m) { |
967 | 988 |
return NegWriteMap<M>(m); |
968 | 989 |
} |
969 | 990 |
|
970 | 991 |
///Absolute value of a map |
971 | 992 |
|
972 | 993 |
///This \ref concepts::ReadMap "read only map" returns the absolute value |
973 | 994 |
///of the value returned by the given map. |
974 | 995 |
///Its \c Key and \c Value are inherited from \c M. |
975 | 996 |
///\c Value must be comparable to \c 0 and the unary \c - |
976 | 997 |
///operator must be defined for it, of course. |
977 | 998 |
template<typename M> |
978 | 999 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
979 | 1000 |
const M& m; |
980 | 1001 |
public: |
981 | 1002 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
982 | 1003 |
typedef typename Parent::Key Key; |
983 | 1004 |
typedef typename Parent::Value Value; |
984 | 1005 |
|
985 | 1006 |
///Constructor |
986 | 1007 |
AbsMap(const M &_m) : m(_m) {}; |
987 | 1008 |
/// \e |
988 | 1009 |
Value operator[](Key k) const { |
989 | 1010 |
Value tmp = m[k]; |
990 | 1011 |
return tmp >= 0 ? tmp : -tmp; |
991 | 1012 |
} |
992 | 1013 |
|
993 | 1014 |
}; |
994 | 1015 |
|
995 | 1016 |
///Returns an \c AbsMap class |
996 | 1017 |
|
997 | 1018 |
///This function just returns an \c AbsMap class. |
998 | 1019 |
///\relates AbsMap |
999 | 1020 |
template<typename M> |
1000 | 1021 |
inline AbsMap<M> absMap(const M &m) { |
1001 | 1022 |
return AbsMap<M>(m); |
1002 | 1023 |
} |
1003 | 1024 |
|
1004 | 1025 |
///Converts an STL style functor to a map |
1005 | 1026 |
|
1006 | 1027 |
///This \ref concepts::ReadMap "read only map" returns the value |
1007 | 1028 |
///of a given functor. |
1008 | 1029 |
/// |
1009 | 1030 |
///Template parameters \c K and \c V will become its |
1010 | 1031 |
///\c Key and \c Value. |
1011 | 1032 |
///In most cases they have to be given explicitly because a |
1012 | 1033 |
///functor typically does not provide \c argument_type and |
1013 | 1034 |
///\c result_type typedefs. |
1014 | 1035 |
/// |
1015 | 1036 |
///Parameter \c F is the type of the used functor. |
1016 | 1037 |
/// |
1017 | 1038 |
///\sa MapFunctor |
1018 | 1039 |
template<typename F, |
1019 | 1040 |
typename K = typename F::argument_type, |
1020 | 1041 |
typename V = typename F::result_type> |
1021 | 1042 |
class FunctorMap : public MapBase<K, V> { |
1022 | 1043 |
F f; |
1023 | 1044 |
public: |
1024 | 1045 |
typedef MapBase<K, V> Parent; |
1025 | 1046 |
typedef typename Parent::Key Key; |
1026 | 1047 |
typedef typename Parent::Value Value; |
1027 | 1048 |
|
1028 | 1049 |
///Constructor |
1029 | 1050 |
FunctorMap(const F &_f = F()) : f(_f) {} |
1030 | 1051 |
/// \e |
1031 | 1052 |
Value operator[](Key k) const { return f(k);} |
1032 | 1053 |
}; |
1033 | 1054 |
|
1034 | 1055 |
///Returns a \c FunctorMap class |
1035 | 1056 |
|
1036 | 1057 |
///This function just returns a \c FunctorMap class. |
1037 | 1058 |
/// |
1038 | 1059 |
///This function is specialized for adaptable binary function |
1039 | 1060 |
///classes and C++ functions. |
1040 | 1061 |
/// |
1041 | 1062 |
///\relates FunctorMap |
1042 | 1063 |
template<typename K, typename V, typename F> inline |
1043 | 1064 |
FunctorMap<F, K, V> functorMap(const F &f) { |
1044 | 1065 |
return FunctorMap<F, K, V>(f); |
1045 | 1066 |
} |
1046 | 1067 |
|
1047 | 1068 |
template <typename F> inline |
1048 | 1069 |
FunctorMap<F, typename F::argument_type, typename F::result_type> |
1049 | 1070 |
functorMap(const F &f) { |
1050 | 1071 |
return FunctorMap<F, typename F::argument_type, |
1051 | 1072 |
typename F::result_type>(f); |
1052 | 1073 |
} |
1053 | 1074 |
|
1054 | 1075 |
template <typename K, typename V> inline |
1055 | 1076 |
FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) { |
1056 | 1077 |
return FunctorMap<V (*)(K), K, V>(f); |
1057 | 1078 |
} |
1058 | 1079 |
|
1059 | 1080 |
|
1060 | 1081 |
///Converts a map to an STL style (unary) functor |
1061 | 1082 |
|
1062 | 1083 |
///This class Converts a map to an STL style (unary) functor. |
1063 | 1084 |
///That is it provides an <tt>operator()</tt> to read its values. |
1064 | 1085 |
/// |
1065 | 1086 |
///For the sake of convenience it also works as |
1066 | 1087 |
///a ususal \ref concepts::ReadMap "readable map", |
1067 | 1088 |
///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
1068 | 1089 |
/// |
1069 | 1090 |
///\sa FunctorMap |
1070 | 1091 |
template <typename M> |
1071 | 1092 |
class MapFunctor : public MapBase<typename M::Key, typename M::Value> { |
1072 | 1093 |
const M& m; |
1073 | 1094 |
public: |
1074 | 1095 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
1075 | 1096 |
typedef typename Parent::Key Key; |
1076 | 1097 |
typedef typename Parent::Value Value; |
1077 | 1098 |
|
1078 | 1099 |
typedef typename M::Key argument_type; |
1079 | 1100 |
typedef typename M::Value result_type; |
1080 | 1101 |
|
1081 | 1102 |
///Constructor |
1082 | 1103 |
MapFunctor(const M &_m) : m(_m) {}; |
1083 | 1104 |
///\e |
1084 | 1105 |
Value operator()(Key k) const {return m[k];} |
1085 | 1106 |
///\e |
1086 | 1107 |
Value operator[](Key k) const {return m[k];} |
1087 | 1108 |
}; |
1088 | 1109 |
|
1089 | 1110 |
///Returns a \c MapFunctor class |
1090 | 1111 |
|
1091 | 1112 |
///This function just returns a \c MapFunctor class. |
1092 | 1113 |
///\relates MapFunctor |
1093 | 1114 |
template<typename M> |
1094 | 1115 |
inline MapFunctor<M> mapFunctor(const M &m) { |
1095 | 1116 |
return MapFunctor<M>(m); |
1096 | 1117 |
} |
1097 | 1118 |
|
1098 | 1119 |
///Just readable version of \ref ForkWriteMap |
1099 | 1120 |
|
1100 | 1121 |
///This map has two \ref concepts::ReadMap "readable map" |
1101 | 1122 |
///parameters and each read request will be passed just to the |
1102 | 1123 |
///first map. This class is the just readable map type of \c ForkWriteMap. |
1103 | 1124 |
/// |
1104 | 1125 |
///The \c Key and \c Value are inherited from \c M1. |
1105 | 1126 |
///The \c Key and \c Value of \c M2 must be convertible from those of \c M1. |
1106 | 1127 |
/// |
1107 | 1128 |
///\sa ForkWriteMap |
1108 | 1129 |
/// |
1109 | 1130 |
/// \todo Why is it needed? |
1110 | 1131 |
template<typename M1, typename M2> |
1111 | 1132 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
1112 | 1133 |
const M1& m1; |
1113 | 1134 |
const M2& m2; |
1114 | 1135 |
public: |
1115 | 1136 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
1116 | 1137 |
typedef typename Parent::Key Key; |
1117 | 1138 |
typedef typename Parent::Value Value; |
1118 | 1139 |
|
1119 | 1140 |
///Constructor |
1120 | 1141 |
ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; |
1121 | 1142 |
/// \e |
1122 | 1143 |
Value operator[](Key k) const {return m1[k];} |
1123 | 1144 |
}; |
1124 | 1145 |
|
1125 | 1146 |
|
1126 | 1147 |
///Applies all map setting operations to two maps |
1127 | 1148 |
|
1128 | 1149 |
///This map has two \ref concepts::WriteMap "writable map" |
1129 | 1150 |
///parameters and each write request will be passed to both of them. |
1130 | 1151 |
///If \c M1 is also \ref concepts::ReadMap "readable", |
1131 | 1152 |
///then the read operations will return the |
1132 | 1153 |
///corresponding values of \c M1. |
1133 | 1154 |
/// |
1134 | 1155 |
///The \c Key and \c Value are inherited from \c M1. |
1135 | 1156 |
///The \c Key and \c Value of \c M2 must be convertible from those of \c M1. |
1136 | 1157 |
/// |
1137 | 1158 |
///\sa ForkMap |
1138 | 1159 |
template<typename M1, typename M2> |
1139 | 1160 |
class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> { |
1140 | 1161 |
M1& m1; |
1141 | 1162 |
M2& m2; |
1142 | 1163 |
public: |
1143 | 1164 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
1144 | 1165 |
typedef typename Parent::Key Key; |
1145 | 1166 |
typedef typename Parent::Value Value; |
1146 | 1167 |
|
1147 | 1168 |
///Constructor |
1148 | 1169 |
ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; |
1149 | 1170 |
///\e |
1150 | 1171 |
Value operator[](Key k) const {return m1[k];} |
1151 | 1172 |
///\e |
1152 | 1173 |
void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} |
1153 | 1174 |
}; |
1154 | 1175 |
|
1155 | 1176 |
///Returns a \c ForkMap class |
1156 | 1177 |
|
1157 | 1178 |
///This function just returns a \c ForkMap class. |
1158 | 1179 |
///\relates ForkMap |
1159 | 1180 |
template <typename M1, typename M2> |
1160 | 1181 |
inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { |
1161 | 1182 |
return ForkMap<M1, M2>(m1,m2); |
1162 | 1183 |
} |
1163 | 1184 |
|
1164 | 1185 |
///Returns a \c ForkWriteMap class |
1165 | 1186 |
|
1166 | 1187 |
///This function just returns a \c ForkWriteMap class. |
1167 | 1188 |
///\relates ForkWriteMap |
1168 | 1189 |
template <typename M1, typename M2> |
1169 | 1190 |
inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { |
1170 | 1191 |
return ForkWriteMap<M1, M2>(m1,m2); |
1171 | 1192 |
} |
1172 | 1193 |
|
1173 | 1194 |
|
1174 | 1195 |
|
1175 | 1196 |
/* ************* BOOL MAPS ******************* */ |
1176 | 1197 |
|
1177 | 1198 |
///Logical 'not' of a map |
1178 | 1199 |
|
1179 | 1200 |
///This bool \ref concepts::ReadMap "read only map" returns the |
1180 | 1201 |
///logical negation of the value returned by the given map. |
1181 | 1202 |
///Its \c Key is inherited from \c M, its \c Value is \c bool. |
1182 | 1203 |
/// |
1183 | 1204 |
///\sa NotWriteMap |
1184 | 1205 |
template <typename M> |
1185 | 1206 |
class NotMap : public MapBase<typename M::Key, bool> { |
1186 | 1207 |
const M& m; |
1187 | 1208 |
public: |
1188 | 1209 |
typedef MapBase<typename M::Key, bool> Parent; |
1189 | 1210 |
typedef typename Parent::Key Key; |
1190 | 1211 |
typedef typename Parent::Value Value; |
1191 | 1212 |
|
1192 | 1213 |
/// Constructor |
1193 | 1214 |
NotMap(const M &_m) : m(_m) {}; |
1194 | 1215 |
///\e |
1195 | 1216 |
Value operator[](Key k) const {return !m[k];} |
1196 | 1217 |
}; |
1197 | 1218 |
|
1198 | 1219 |
///Logical 'not' of a map (ReadWrie version) |
1199 | 1220 |
|
1200 | 1221 |
///This bool \ref concepts::ReadWriteMap "read-write map" returns the |
1201 | 1222 |
///logical negation of the value returned by the given map. When it is set, |
1202 | 1223 |
///the opposite value is set to the original map. |
1203 | 1224 |
///Its \c Key is inherited from \c M, its \c Value is \c bool. |
1204 | 1225 |
/// |
1205 | 1226 |
///\sa NotMap |
1206 | 1227 |
template <typename M> |
1207 | 1228 |
class NotWriteMap : public MapBase<typename M::Key, bool> { |
1208 | 1229 |
M& m; |
1209 | 1230 |
public: |
1210 | 1231 |
typedef MapBase<typename M::Key, bool> Parent; |
1211 | 1232 |
typedef typename Parent::Key Key; |
1212 | 1233 |
typedef typename Parent::Value Value; |
1213 | 1234 |
|
1214 | 1235 |
/// Constructor |
1215 | 1236 |
NotWriteMap(M &_m) : m(_m) {}; |
1216 | 1237 |
///\e |
1217 | 1238 |
Value operator[](Key k) const {return !m[k];} |
1218 | 1239 |
///\e |
1219 | 1240 |
void set(Key k, bool v) { m.set(k, !v); } |
1220 | 1241 |
}; |
1221 | 1242 |
|
1222 | 1243 |
///Returns a \c NotMap class |
1223 | 1244 |
|
1224 | 1245 |
///This function just returns a \c NotMap class. |
1225 | 1246 |
///\relates NotMap |
1226 | 1247 |
template <typename M> |
1227 | 1248 |
inline NotMap<M> notMap(const M &m) { |
1228 | 1249 |
return NotMap<M>(m); |
1229 | 1250 |
} |
1230 | 1251 |
|
1231 | 1252 |
///Returns a \c NotWriteMap class |
1232 | 1253 |
|
1233 | 1254 |
///This function just returns a \c NotWriteMap class. |
1234 | 1255 |
///\relates NotWriteMap |
1235 | 1256 |
template <typename M> |
1236 | 1257 |
inline NotWriteMap<M> notMap(M &m) { |
1237 | 1258 |
return NotWriteMap<M>(m); |
1238 | 1259 |
} |
1239 | 1260 |
|
1240 | 1261 |
namespace _maps_bits { |
1241 | 1262 |
|
1242 | 1263 |
template <typename Value> |
1243 | 1264 |
struct Identity { |
1244 | 1265 |
typedef Value argument_type; |
1245 | 1266 |
typedef Value result_type; |
1246 | 1267 |
Value operator()(const Value& val) const { |
1247 | 1268 |
return val; |
1248 | 1269 |
} |
1249 | 1270 |
}; |
1250 | 1271 |
|
1251 | 1272 |
template <typename _Iterator, typename Enable = void> |
1252 | 1273 |
struct IteratorTraits { |
1253 | 1274 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1254 | 1275 |
}; |
1255 | 1276 |
|
1256 | 1277 |
template <typename _Iterator> |
1257 | 1278 |
struct IteratorTraits<_Iterator, |
1258 | 1279 |
typename exists<typename _Iterator::container_type>::type> |
1259 | 1280 |
{ |
1260 | 1281 |
typedef typename _Iterator::container_type::value_type Value; |
1261 | 1282 |
}; |
1262 | 1283 |
|
1263 | 1284 |
} |
1264 | 1285 |
|
1265 | 1286 |
|
1266 | 1287 |
/// \brief Writable bool map for logging each \c true assigned element |
1267 | 1288 |
/// |
1268 | 1289 |
/// A \ref concepts::ReadWriteMap "read-write" bool map for logging |
1269 | 1290 |
/// each \c true assigned element, i.e it copies all the keys set |
1270 | 1291 |
/// to \c true to the given iterator. |
1271 | 1292 |
/// |
1272 | 1293 |
/// \note The container of the iterator should contain space |
1273 | 1294 |
/// for each element. |
1274 | 1295 |
/// |
1275 | 1296 |
/// The following example shows how you can write the edges found by |
1276 | 1297 |
/// the \ref Prim algorithm directly to the standard output. |
1277 | 1298 |
///\code |
1278 | 1299 |
/// typedef IdMap<Graph, Edge> EdgeIdMap; |
1279 | 1300 |
/// EdgeIdMap edgeId(graph); |
1280 | 1301 |
/// |
1281 | 1302 |
/// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor; |
1282 | 1303 |
/// EdgeIdFunctor edgeIdFunctor(edgeId); |
1283 | 1304 |
/// |
1284 | 1305 |
/// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor> |
1285 | 1306 |
/// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor); |
1286 | 1307 |
/// |
1287 | 1308 |
/// prim(graph, cost, writerMap); |
1288 | 1309 |
///\endcode |
1289 | 1310 |
/// |
1290 | 1311 |
///\sa BackInserterBoolMap |
1291 | 1312 |
///\sa FrontInserterBoolMap |
1292 | 1313 |
///\sa InserterBoolMap |
1293 | 1314 |
/// |
1294 | 1315 |
///\todo Revise the name of this class and the related ones. |
1295 | 1316 |
template <typename _Iterator, |
1296 | 1317 |
typename _Functor = |
1297 | 1318 |
_maps_bits::Identity<typename _maps_bits:: |
1298 | 1319 |
IteratorTraits<_Iterator>::Value> > |
1299 | 1320 |
class StoreBoolMap { |
1300 | 1321 |
public: |
1301 | 1322 |
typedef _Iterator Iterator; |
1302 | 1323 |
|
1303 | 1324 |
typedef typename _Functor::argument_type Key; |
1304 | 1325 |
typedef bool Value; |
1305 | 1326 |
|
1306 | 1327 |
typedef _Functor Functor; |
1307 | 1328 |
|
1308 | 1329 |
/// Constructor |
1309 | 1330 |
StoreBoolMap(Iterator it, const Functor& functor = Functor()) |
1310 | 1331 |
: _begin(it), _end(it), _functor(functor) {} |
1311 | 1332 |
|
1312 | 1333 |
/// Gives back the given iterator set for the first key |
1313 | 1334 |
Iterator begin() const { |
1314 | 1335 |
return _begin; |
1315 | 1336 |
} |
1316 | 1337 |
|
1317 | 1338 |
/// Gives back the the 'after the last' iterator |
1318 | 1339 |
Iterator end() const { |
1319 | 1340 |
return _end; |
1320 | 1341 |
} |
1321 | 1342 |
|
1322 | 1343 |
/// The \c set function of the map |
1323 | 1344 |
void set(const Key& key, Value value) const { |
1324 | 1345 |
if (value) { |
1325 | 1346 |
*_end++ = _functor(key); |
1326 | 1347 |
} |
1327 | 1348 |
} |
1328 | 1349 |
|
1329 | 1350 |
private: |
1330 | 1351 |
Iterator _begin; |
1331 | 1352 |
mutable Iterator _end; |
1332 | 1353 |
Functor _functor; |
1333 | 1354 |
}; |
1334 | 1355 |
|
1335 | 1356 |
/// \brief Writable bool map for logging each \c true assigned element in |
1336 | 1357 |
/// a back insertable container. |
1337 | 1358 |
/// |
1338 | 1359 |
/// Writable bool map for logging each \c true assigned element by pushing |
1339 | 1360 |
/// them into a back insertable container. |
1340 | 1361 |
/// It can be used to retrieve the items into a standard |
1341 | 1362 |
/// container. The next example shows how you can store the |
1342 | 1363 |
/// edges found by the Prim algorithm in a vector. |
1343 | 1364 |
/// |
1344 | 1365 |
///\code |
1345 | 1366 |
/// vector<Edge> span_tree_edges; |
1346 | 1367 |
/// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges); |
1347 | 1368 |
/// prim(graph, cost, inserter_map); |
1348 | 1369 |
///\endcode |
1349 | 1370 |
/// |
1350 | 1371 |
///\sa StoreBoolMap |
1351 | 1372 |
///\sa FrontInserterBoolMap |
1352 | 1373 |
///\sa InserterBoolMap |
1353 | 1374 |
template <typename Container, |
1354 | 1375 |
typename Functor = |
1355 | 1376 |
_maps_bits::Identity<typename Container::value_type> > |
1356 | 1377 |
class BackInserterBoolMap { |
1357 | 1378 |
public: |
1358 | 1379 |
typedef typename Functor::argument_type Key; |
1359 | 1380 |
typedef bool Value; |
1360 | 1381 |
|
1361 | 1382 |
/// Constructor |
1362 | 1383 |
BackInserterBoolMap(Container& _container, |
1363 | 1384 |
const Functor& _functor = Functor()) |
1364 | 1385 |
: container(_container), functor(_functor) {} |
1365 | 1386 |
|
1366 | 1387 |
/// The \c set function of the map |
1367 | 1388 |
void set(const Key& key, Value value) { |
1368 | 1389 |
if (value) { |
1369 | 1390 |
container.push_back(functor(key)); |
1370 | 1391 |
} |
1371 | 1392 |
} |
1372 | 1393 |
|
1373 | 1394 |
private: |
1374 | 1395 |
Container& container; |
1375 | 1396 |
Functor functor; |
1376 | 1397 |
}; |
1377 | 1398 |
|
1378 | 1399 |
/// \brief Writable bool map for logging each \c true assigned element in |
1379 | 1400 |
/// a front insertable container. |
1380 | 1401 |
/// |
1381 | 1402 |
/// Writable bool map for logging each \c true assigned element by pushing |
1382 | 1403 |
/// them into a front insertable container. |
1383 | 1404 |
/// It can be used to retrieve the items into a standard |
1384 | 1405 |
/// container. For example see \ref BackInserterBoolMap. |
1385 | 1406 |
/// |
1386 | 1407 |
///\sa BackInserterBoolMap |
1387 | 1408 |
///\sa InserterBoolMap |
1388 | 1409 |
template <typename Container, |
1389 | 1410 |
typename Functor = |
1390 | 1411 |
_maps_bits::Identity<typename Container::value_type> > |
1391 | 1412 |
class FrontInserterBoolMap { |
1392 | 1413 |
public: |
1393 | 1414 |
typedef typename Functor::argument_type Key; |
1394 | 1415 |
typedef bool Value; |
1395 | 1416 |
|
1396 | 1417 |
/// Constructor |
1397 | 1418 |
FrontInserterBoolMap(Container& _container, |
1398 | 1419 |
const Functor& _functor = Functor()) |
1399 | 1420 |
: container(_container), functor(_functor) {} |
1400 | 1421 |
|
1401 | 1422 |
/// The \c set function of the map |
1402 | 1423 |
void set(const Key& key, Value value) { |
1403 | 1424 |
if (value) { |
1404 | 1425 |
container.push_front(functor(key)); |
1405 | 1426 |
} |
1406 | 1427 |
} |
1407 | 1428 |
|
1408 | 1429 |
private: |
1409 | 1430 |
Container& container; |
1410 | 1431 |
Functor functor; |
1411 | 1432 |
}; |
1412 | 1433 |
|
1413 | 1434 |
/// \brief Writable bool map for storing each \c true assigned element in |
1414 | 1435 |
/// an insertable container. |
1415 | 1436 |
/// |
1416 | 1437 |
/// Writable bool map for storing each \c true assigned element in an |
1417 | 1438 |
/// insertable container. It will insert all the keys set to \c true into |
1418 | 1439 |
/// the container. |
1419 | 1440 |
/// |
1420 | 1441 |
/// For example, if you want to store the cut arcs of the strongly |
1421 | 1442 |
/// connected components in a set you can use the next code: |
1422 | 1443 |
/// |
1423 | 1444 |
///\code |
1424 | 1445 |
/// set<Arc> cut_arcs; |
1425 | 1446 |
/// InserterBoolMap<set<Arc> > inserter_map(cut_arcs); |
1426 | 1447 |
/// stronglyConnectedCutArcs(digraph, cost, inserter_map); |
1427 | 1448 |
///\endcode |
1428 | 1449 |
/// |
1429 | 1450 |
///\sa BackInserterBoolMap |
1430 | 1451 |
///\sa FrontInserterBoolMap |
1431 | 1452 |
template <typename Container, |
1432 | 1453 |
typename Functor = |
1433 | 1454 |
_maps_bits::Identity<typename Container::value_type> > |
1434 | 1455 |
class InserterBoolMap { |
1435 | 1456 |
public: |
1436 | 1457 |
typedef typename Container::value_type Key; |
1437 | 1458 |
typedef bool Value; |
1438 | 1459 |
|
1439 | 1460 |
/// Constructor with specified iterator |
1440 | 1461 |
|
1441 | 1462 |
/// Constructor with specified iterator. |
1442 | 1463 |
/// \param _container The container for storing the elements. |
1443 | 1464 |
/// \param _it The elements will be inserted before this iterator. |
1444 | 1465 |
/// \param _functor The functor that is used when an element is stored. |
1445 | 1466 |
InserterBoolMap(Container& _container, typename Container::iterator _it, |
1446 | 1467 |
const Functor& _functor = Functor()) |
1447 | 1468 |
: container(_container), it(_it), functor(_functor) {} |
1448 | 1469 |
|
1449 | 1470 |
/// Constructor |
1450 | 1471 |
|
1451 | 1472 |
/// Constructor without specified iterator. |
1452 | 1473 |
/// The elements will be inserted before <tt>_container.end()</tt>. |
1453 | 1474 |
/// \param _container The container for storing the elements. |
1454 | 1475 |
/// \param _functor The functor that is used when an element is stored. |
1455 | 1476 |
InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
1456 | 1477 |
: container(_container), it(_container.end()), functor(_functor) {} |
1457 | 1478 |
|
1458 | 1479 |
/// The \c set function of the map |
1459 | 1480 |
void set(const Key& key, Value value) { |
1460 | 1481 |
if (value) { |
1461 | 1482 |
it = container.insert(it, functor(key)); |
1462 | 1483 |
++it; |
1463 | 1484 |
} |
1464 | 1485 |
} |
1465 | 1486 |
|
1466 | 1487 |
private: |
1467 | 1488 |
Container& container; |
1468 | 1489 |
typename Container::iterator it; |
1469 | 1490 |
Functor functor; |
1470 | 1491 |
}; |
1471 | 1492 |
|
1472 | 1493 |
/// \brief Writable bool map for filling each \c true assigned element with a |
1473 | 1494 |
/// given value. |
1474 | 1495 |
/// |
1475 | 1496 |
/// Writable bool map for filling each \c true assigned element with a |
1476 | 1497 |
/// given value. The value can set the container. |
1477 | 1498 |
/// |
1478 | 1499 |
/// The following code finds the connected components of a graph |
1479 | 1500 |
/// and stores it in the \c comp map: |
1480 | 1501 |
///\code |
1481 | 1502 |
/// typedef Graph::NodeMap<int> ComponentMap; |
1482 | 1503 |
/// ComponentMap comp(graph); |
1483 | 1504 |
/// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap; |
1484 | 1505 |
/// ComponentFillerMap filler(comp, 0); |
1485 | 1506 |
/// |
1486 | 1507 |
/// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph); |
1487 | 1508 |
/// dfs.processedMap(filler); |
1488 | 1509 |
/// dfs.init(); |
1489 | 1510 |
/// for (NodeIt it(graph); it != INVALID; ++it) { |
1490 | 1511 |
/// if (!dfs.reached(it)) { |
1491 | 1512 |
/// dfs.addSource(it); |
1492 | 1513 |
/// dfs.start(); |
1493 | 1514 |
/// ++filler.fillValue(); |
1494 | 1515 |
/// } |
1495 | 1516 |
/// } |
1496 | 1517 |
///\endcode |
1497 | 1518 |
template <typename Map> |
1498 | 1519 |
class FillBoolMap { |
1499 | 1520 |
public: |
1500 | 1521 |
typedef typename Map::Key Key; |
1501 | 1522 |
typedef bool Value; |
1502 | 1523 |
|
1503 | 1524 |
/// Constructor |
1504 | 1525 |
FillBoolMap(Map& _map, const typename Map::Value& _fill) |
1505 | 1526 |
: map(_map), fill(_fill) {} |
1506 | 1527 |
|
1507 | 1528 |
/// Constructor |
1508 | 1529 |
FillBoolMap(Map& _map) |
1509 | 1530 |
: map(_map), fill() {} |
1510 | 1531 |
|
1511 | 1532 |
/// Gives back the current fill value |
1512 | 1533 |
const typename Map::Value& fillValue() const { |
1513 | 1534 |
return fill; |
1514 | 1535 |
} |
1515 | 1536 |
|
1516 | 1537 |
/// Gives back the current fill value |
1517 | 1538 |
typename Map::Value& fillValue() { |
1518 | 1539 |
return fill; |
1519 | 1540 |
} |
1520 | 1541 |
|
1521 | 1542 |
/// Sets the current fill value |
1522 | 1543 |
void fillValue(const typename Map::Value& _fill) { |
1523 | 1544 |
fill = _fill; |
1524 | 1545 |
} |
1525 | 1546 |
|
1526 | 1547 |
/// The \c set function of the map |
1527 | 1548 |
void set(const Key& key, Value value) { |
1528 | 1549 |
if (value) { |
1529 | 1550 |
map.set(key, fill); |
1530 | 1551 |
} |
1531 | 1552 |
} |
1532 | 1553 |
|
1533 | 1554 |
private: |
1534 | 1555 |
Map& map; |
1535 | 1556 |
typename Map::Value fill; |
1536 | 1557 |
}; |
1537 | 1558 |
|
1538 | 1559 |
|
1539 | 1560 |
/// \brief Writable bool map for storing the sequence number of |
1540 | 1561 |
/// \c true assignments. |
1541 | 1562 |
/// |
1542 | 1563 |
/// Writable bool map that stores for each \c true assigned elements |
1543 | 1564 |
/// the sequence number of this setting. |
1544 | 1565 |
/// It makes it easy to calculate the leaving |
1545 | 1566 |
/// order of the nodes in the \c Dfs algorithm. |
1546 | 1567 |
/// |
1547 | 1568 |
///\code |
1548 | 1569 |
/// typedef Digraph::NodeMap<int> OrderMap; |
1549 | 1570 |
/// OrderMap order(digraph); |
1550 | 1571 |
/// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
1551 | 1572 |
/// OrderSetterMap setter(order); |
1552 | 1573 |
/// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph); |
1553 | 1574 |
/// dfs.processedMap(setter); |
1554 | 1575 |
/// dfs.init(); |
1555 | 1576 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1556 | 1577 |
/// if (!dfs.reached(it)) { |
1557 | 1578 |
/// dfs.addSource(it); |
1558 | 1579 |
/// dfs.start(); |
1559 | 1580 |
/// } |
1560 | 1581 |
/// } |
1561 | 1582 |
///\endcode |
1562 | 1583 |
/// |
1563 | 1584 |
/// The storing of the discovering order is more difficult because the |
1564 | 1585 |
/// ReachedMap should be readable in the dfs algorithm but the setting |
1565 | 1586 |
/// order map is not readable. Thus we must use the fork map: |
1566 | 1587 |
/// |
1567 | 1588 |
///\code |
1568 | 1589 |
/// typedef Digraph::NodeMap<int> OrderMap; |
1569 | 1590 |
/// OrderMap order(digraph); |
1570 | 1591 |
/// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
1571 | 1592 |
/// OrderSetterMap setter(order); |
1572 | 1593 |
/// typedef Digraph::NodeMap<bool> StoreMap; |
1573 | 1594 |
/// StoreMap store(digraph); |
1574 | 1595 |
/// |
1575 | 1596 |
/// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap; |
1576 | 1597 |
/// ReachedMap reached(store, setter); |
1577 | 1598 |
/// |
1578 | 1599 |
/// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph); |
1579 | 1600 |
/// dfs.reachedMap(reached); |
1580 | 1601 |
/// dfs.init(); |
1581 | 1602 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1582 | 1603 |
/// if (!dfs.reached(it)) { |
1583 | 1604 |
/// dfs.addSource(it); |
1584 | 1605 |
/// dfs.start(); |
1585 | 1606 |
/// } |
1586 | 1607 |
/// } |
1587 | 1608 |
///\endcode |
1588 | 1609 |
template <typename Map> |
1589 | 1610 |
class SettingOrderBoolMap { |
1590 | 1611 |
public: |
1591 | 1612 |
typedef typename Map::Key Key; |
1592 | 1613 |
typedef bool Value; |
1593 | 1614 |
|
1594 | 1615 |
/// Constructor |
1595 | 1616 |
SettingOrderBoolMap(Map& _map) |
1596 | 1617 |
: map(_map), counter(0) {} |
1597 | 1618 |
|
1598 | 1619 |
/// Number of set operations. |
1599 | 1620 |
int num() const { |
1600 | 1621 |
return counter; |
1601 | 1622 |
} |
1602 | 1623 |
|
1603 | 1624 |
/// The \c set function of the map |
1604 | 1625 |
void set(const Key& key, Value value) { |
1605 | 1626 |
if (value) { |
1606 | 1627 |
map.set(key, counter++); |
1607 | 1628 |
} |
1608 | 1629 |
} |
1609 | 1630 |
|
1610 | 1631 |
private: |
1611 | 1632 |
Map& map; |
1612 | 1633 |
int counter; |
1613 | 1634 |
}; |
1614 | 1635 |
|
1615 | 1636 |
/// @} |
1616 | 1637 |
} |
1617 | 1638 |
|
1618 | 1639 |
#endif // LEMON_MAPS_H |
0 comments (0 inline)