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-2007 |
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 |
/// This is a readable map which assigns a specified value to each key. |
|
85 |
/// In other aspects it is equivalent to the \c NullMap. |
|
84 |
/// This is a \ref concepts::ReadMap "readable" map which assigns a |
|
85 |
/// specified value to each key. |
|
86 |
/// In other aspects it is equivalent to \c NullMap. |
|
86 | 87 |
template<typename K, typename T> |
87 | 88 |
class ConstMap : public MapBase<K, T> { |
88 | 89 |
private: |
89 | 90 |
T v; |
90 | 91 |
public: |
91 | 92 |
|
92 | 93 |
typedef MapBase<K, T> Parent; |
93 | 94 |
typedef typename Parent::Key Key; |
94 | 95 |
typedef typename Parent::Value Value; |
95 | 96 |
|
96 | 97 |
/// Default constructor |
97 | 98 |
|
98 | 99 |
/// Default constructor. |
99 | 100 |
/// The value of the map will be uninitialized. |
100 | 101 |
/// (More exactly it will be default constructed.) |
101 | 102 |
ConstMap() {} |
102 | 103 |
|
103 | 104 |
/// Constructor with specified initial value |
104 | 105 |
|
105 | 106 |
/// Constructor with specified initial value. |
106 | 107 |
/// \param _v is the initial value of the map. |
107 | 108 |
ConstMap(const T &_v) : v(_v) {} |
108 | 109 |
|
109 | 110 |
///\e |
110 | 111 |
T operator[](const K&) const { return v; } |
111 | 112 |
|
112 | 113 |
///\e |
113 | 114 |
void setAll(const T &t) { |
114 | 115 |
v = t; |
115 | 116 |
} |
116 | 117 |
|
117 | 118 |
template<typename T1> |
118 | 119 |
ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {} |
119 | 120 |
}; |
120 | 121 |
|
121 | 122 |
///Returns a \c ConstMap class |
122 | 123 |
|
123 | 124 |
///This function just returns a \c ConstMap class. |
124 | 125 |
///\relates ConstMap |
125 | 126 |
template<typename K, typename V> |
126 | 127 |
inline ConstMap<K, V> constMap(const V &v) { |
127 | 128 |
return ConstMap<K, V>(v); |
128 | 129 |
} |
129 | 130 |
|
130 | 131 |
|
131 | 132 |
template<typename T, T v> |
132 | 133 |
struct Const { }; |
133 | 134 |
|
134 | 135 |
/// Constant map with inlined constant value. |
135 | 136 |
|
136 |
/// This is a readable map which assigns a specified value to each key. |
|
137 |
/// In other aspects it is equivalent to the \c NullMap. |
|
137 |
/// This is a \ref concepts::ReadMap "readable" map which assigns a |
|
138 |
/// specified value to each key. |
|
139 |
/// In other aspects it is equivalent to \c NullMap. |
|
138 | 140 |
template<typename K, typename V, V v> |
139 | 141 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
140 | 142 |
public: |
141 | 143 |
typedef MapBase<K, V> Parent; |
142 | 144 |
typedef typename Parent::Key Key; |
143 | 145 |
typedef typename Parent::Value Value; |
144 | 146 |
|
145 | 147 |
ConstMap() { } |
146 | 148 |
///\e |
147 | 149 |
V operator[](const K&) const { return v; } |
148 | 150 |
///\e |
149 | 151 |
void set(const K&, const V&) { } |
150 | 152 |
}; |
151 | 153 |
|
152 |
///Returns a \c ConstMap class |
|
154 |
///Returns a \c ConstMap class with inlined value |
|
153 | 155 |
|
154 | 156 |
///This function just returns a \c ConstMap class with inlined value. |
155 | 157 |
///\relates ConstMap |
156 | 158 |
template<typename K, typename V, V v> |
157 | 159 |
inline ConstMap<K, Const<V, v> > constMap() { |
158 | 160 |
return ConstMap<K, Const<V, v> >(); |
159 | 161 |
} |
160 | 162 |
|
161 | 163 |
///Map based on \c std::map |
162 | 164 |
|
163 | 165 |
///This is essentially a wrapper for \c std::map with addition that |
164 | 166 |
///you can specify a default value different from \c Value(). |
167 |
///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept. |
|
165 | 168 |
template <typename K, typename T, typename Compare = std::less<K> > |
166 | 169 |
class StdMap : public MapBase<K, T> { |
167 | 170 |
template <typename K1, typename T1, typename C1> |
168 | 171 |
friend class StdMap; |
169 | 172 |
public: |
170 | 173 |
|
171 | 174 |
typedef MapBase<K, T> Parent; |
172 | 175 |
///\e |
173 | 176 |
typedef typename Parent::Key Key; |
174 | 177 |
///\e |
175 | 178 |
typedef typename Parent::Value Value; |
176 | 179 |
///\e |
177 | 180 |
typedef T& Reference; |
178 | 181 |
///\e |
179 | 182 |
typedef const T& ConstReference; |
180 | 183 |
|
181 | 184 |
typedef True ReferenceMapTag; |
182 | 185 |
|
183 | 186 |
private: |
184 | 187 |
|
185 | 188 |
typedef std::map<K, T, Compare> Map; |
186 | 189 |
Value _value; |
187 | 190 |
Map _map; |
188 | 191 |
|
189 | 192 |
public: |
190 | 193 |
|
191 | 194 |
/// Constructor with specified default value |
192 | 195 |
StdMap(const T& value = T()) : _value(value) {} |
193 |
/// \brief Constructs the map from an appropriate std::map, and explicitly |
|
194 |
/// specifies a default value. |
|
196 |
/// \brief Constructs the map from an appropriate \c std::map, and |
|
197 |
/// explicitly specifies a default value. |
|
195 | 198 |
template <typename T1, typename Comp1> |
196 | 199 |
StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) |
197 | 200 |
: _map(map.begin(), map.end()), _value(value) {} |
198 | 201 |
|
199 |
/// \brief Constructs a map from an other StdMap. |
|
202 |
/// \brief Constructs a map from an other \ref StdMap. |
|
200 | 203 |
template<typename T1, typename Comp1> |
201 | 204 |
StdMap(const StdMap<Key, T1, Comp1> &c) |
202 | 205 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {} |
203 | 206 |
|
204 | 207 |
private: |
205 | 208 |
|
206 | 209 |
StdMap& operator=(const StdMap&); |
207 | 210 |
|
208 | 211 |
public: |
209 | 212 |
|
210 | 213 |
///\e |
211 | 214 |
Reference operator[](const Key &k) { |
212 | 215 |
typename Map::iterator it = _map.lower_bound(k); |
213 | 216 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
214 | 217 |
return it->second; |
215 | 218 |
else |
216 | 219 |
return _map.insert(it, std::make_pair(k, _value))->second; |
217 | 220 |
} |
218 | 221 |
|
219 | 222 |
/// \e |
220 | 223 |
ConstReference operator[](const Key &k) const { |
221 | 224 |
typename Map::const_iterator it = _map.find(k); |
222 | 225 |
if (it != _map.end()) |
223 | 226 |
return it->second; |
224 | 227 |
else |
225 | 228 |
return _value; |
226 | 229 |
} |
227 | 230 |
|
228 | 231 |
/// \e |
229 | 232 |
void set(const Key &k, const T &t) { |
230 | 233 |
typename Map::iterator it = _map.lower_bound(k); |
231 | 234 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
232 | 235 |
it->second = t; |
233 | 236 |
else |
234 | 237 |
_map.insert(it, std::make_pair(k, t)); |
235 | 238 |
} |
236 | 239 |
|
237 | 240 |
/// \e |
238 | 241 |
void setAll(const T &t) { |
239 | 242 |
_value = t; |
240 | 243 |
_map.clear(); |
241 | 244 |
} |
242 | 245 |
|
243 | 246 |
}; |
244 | 247 |
|
245 | 248 |
///Returns a \c StdMap class |
246 | 249 |
|
247 | 250 |
///This function just returns a \c StdMap class with specified |
248 | 251 |
///default value. |
249 | 252 |
///\relates StdMap |
250 | 253 |
template<typename K, typename V, typename Compare = std::less<K> > |
251 | 254 |
inline StdMap<K, V, Compare> stdMap(const V& value = V()) { |
252 | 255 |
return StdMap<K, V, Compare>(value); |
253 | 256 |
} |
254 | 257 |
|
255 | 258 |
///Returns a \c StdMap class created from an appropriate std::map |
256 | 259 |
|
257 | 260 |
///This function just returns a \c StdMap class created from an |
258 | 261 |
///appropriate std::map. |
259 | 262 |
///\relates StdMap |
260 | 263 |
template<typename K, typename V, typename Compare = std::less<K> > |
261 | 264 |
inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, |
262 | 265 |
const V& value = V() ) { |
263 | 266 |
return StdMap<K, V, Compare>(map, value); |
264 | 267 |
} |
265 | 268 |
|
266 | 269 |
/// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt> |
267 | 270 |
/// |
268 |
/// |
|
271 |
/// This map has the <tt>[0..size-1]</tt> keyset and the values |
|
269 | 272 |
/// are stored in a \c std::vector<T> container. It can be used with |
270 | 273 |
/// some data structures, for example \c UnionFind, \c BinHeap, when |
271 | 274 |
/// the used items are small integer numbers. |
275 |
/// This map meets the \ref concepts::ReferenceMap "ReferenceMap" concept. |
|
272 | 276 |
/// |
273 | 277 |
/// \todo Revise its name |
274 | 278 |
template <typename T> |
275 | 279 |
class IntegerMap : public MapBase<int, T> { |
276 | 280 |
|
277 | 281 |
template <typename T1> |
278 | 282 |
friend class IntegerMap; |
279 | 283 |
|
280 | 284 |
public: |
281 | 285 |
|
282 | 286 |
typedef MapBase<int, T> Parent; |
283 | 287 |
///\e |
284 | 288 |
typedef typename Parent::Key Key; |
285 | 289 |
///\e |
286 | 290 |
typedef typename Parent::Value Value; |
287 | 291 |
///\e |
288 | 292 |
typedef T& Reference; |
289 | 293 |
///\e |
290 | 294 |
typedef const T& ConstReference; |
291 | 295 |
|
292 | 296 |
typedef True ReferenceMapTag; |
293 | 297 |
|
294 | 298 |
private: |
295 | 299 |
|
296 | 300 |
typedef std::vector<T> Vector; |
297 | 301 |
Vector _vector; |
298 | 302 |
|
299 | 303 |
public: |
300 | 304 |
|
301 | 305 |
/// Constructor with specified default value |
302 | 306 |
IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} |
303 | 307 |
|
304 |
/// \brief Constructs the map from an appropriate std::vector. |
|
308 |
/// \brief Constructs the map from an appropriate \c std::vector. |
|
305 | 309 |
template <typename T1> |
306 | 310 |
IntegerMap(const std::vector<T1>& vector) |
307 | 311 |
: _vector(vector.begin(), vector.end()) {} |
308 | 312 |
|
309 |
/// \brief Constructs a map from an other IntegerMap. |
|
313 |
/// \brief Constructs a map from an other \ref IntegerMap. |
|
310 | 314 |
template <typename T1> |
311 | 315 |
IntegerMap(const IntegerMap<T1> &c) |
312 | 316 |
: _vector(c._vector.begin(), c._vector.end()) {} |
313 | 317 |
|
314 | 318 |
/// \brief Resize the container |
315 | 319 |
void resize(int size, const T& value = T()) { |
316 | 320 |
_vector.resize(size, value); |
317 | 321 |
} |
318 | 322 |
|
319 | 323 |
private: |
320 | 324 |
|
321 | 325 |
IntegerMap& operator=(const IntegerMap&); |
322 | 326 |
|
323 | 327 |
public: |
324 | 328 |
|
325 | 329 |
///\e |
326 | 330 |
Reference operator[](Key k) { |
327 | 331 |
return _vector[k]; |
328 | 332 |
} |
329 | 333 |
|
330 | 334 |
/// \e |
331 | 335 |
ConstReference operator[](Key k) const { |
332 | 336 |
return _vector[k]; |
333 | 337 |
} |
334 | 338 |
|
335 | 339 |
/// \e |
336 | 340 |
void set(const Key &k, const T& t) { |
337 | 341 |
_vector[k] = t; |
338 | 342 |
} |
339 | 343 |
|
340 | 344 |
}; |
341 | 345 |
|
342 | 346 |
///Returns an \c IntegerMap class |
343 | 347 |
|
344 | 348 |
///This function just returns an \c IntegerMap class. |
345 | 349 |
///\relates IntegerMap |
346 | 350 |
template<typename T> |
347 | 351 |
inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) { |
348 | 352 |
return IntegerMap<T>(size, value); |
349 | 353 |
} |
350 | 354 |
|
351 | 355 |
/// @} |
352 | 356 |
|
353 | 357 |
/// \addtogroup map_adaptors |
354 | 358 |
/// @{ |
355 | 359 |
|
356 | 360 |
/// \brief Identity map. |
357 | 361 |
/// |
358 | 362 |
/// This map gives back the given key as value without any |
359 | 363 |
/// modification. |
360 | 364 |
template <typename T> |
361 | 365 |
class IdentityMap : public MapBase<T, T> { |
362 | 366 |
public: |
363 | 367 |
typedef MapBase<T, T> Parent; |
364 | 368 |
typedef typename Parent::Key Key; |
365 | 369 |
typedef typename Parent::Value Value; |
366 | 370 |
|
367 | 371 |
/// \e |
368 | 372 |
const T& operator[](const T& t) const { |
369 | 373 |
return t; |
370 | 374 |
} |
371 | 375 |
}; |
372 | 376 |
|
373 | 377 |
///Returns an \c IdentityMap class |
374 | 378 |
|
375 | 379 |
///This function just returns an \c IdentityMap class. |
376 | 380 |
///\relates IdentityMap |
377 | 381 |
template<typename T> |
378 | 382 |
inline IdentityMap<T> identityMap() { |
379 | 383 |
return IdentityMap<T>(); |
380 | 384 |
} |
381 | 385 |
|
382 | 386 |
|
383 | 387 |
///\brief Convert the \c Value of a map to another type using |
384 | 388 |
///the default conversion. |
385 | 389 |
/// |
386 | 390 |
///This \ref concepts::ReadMap "read only map" |
387 | 391 |
///converts the \c Value of a map to type \c T. |
388 | 392 |
///Its \c Key is inherited from \c M. |
389 | 393 |
template <typename M, typename T> |
390 | 394 |
class ConvertMap : public MapBase<typename M::Key, T> { |
391 | 395 |
const M& m; |
392 | 396 |
public: |
393 | 397 |
typedef MapBase<typename M::Key, T> Parent; |
394 | 398 |
typedef typename Parent::Key Key; |
395 | 399 |
typedef typename Parent::Value Value; |
396 | 400 |
|
397 | 401 |
///Constructor |
398 | 402 |
|
399 | 403 |
///Constructor. |
400 | 404 |
///\param _m is the underlying map. |
401 | 405 |
ConvertMap(const M &_m) : m(_m) {}; |
402 | 406 |
|
403 |
/// \brief The subscript operator. |
|
404 |
/// |
|
405 |
/// |
|
407 |
///\e |
|
406 | 408 |
Value operator[](const Key& k) const {return m[k];} |
407 | 409 |
}; |
408 | 410 |
|
409 | 411 |
///Returns a \c ConvertMap class |
410 | 412 |
|
411 | 413 |
///This function just returns a \c ConvertMap class. |
412 | 414 |
///\relates ConvertMap |
413 | 415 |
template<typename T, typename M> |
414 | 416 |
inline ConvertMap<M, T> convertMap(const M &m) { |
415 | 417 |
return ConvertMap<M, T>(m); |
416 | 418 |
} |
417 | 419 |
|
418 | 420 |
///Simple wrapping of a map |
419 | 421 |
|
420 | 422 |
///This \ref concepts::ReadMap "read only map" returns the simple |
421 | 423 |
///wrapping of the given map. Sometimes the reference maps cannot be |
422 | 424 |
///combined with simple read maps. This map adaptor wraps the given |
423 | 425 |
///map to simple read map. |
424 | 426 |
/// |
425 | 427 |
///\sa SimpleWriteMap |
426 | 428 |
/// |
427 | 429 |
/// \todo Revise the misleading name |
428 | 430 |
template<typename M> |
429 | 431 |
class SimpleMap : public MapBase<typename M::Key, typename M::Value> { |
430 | 432 |
const M& m; |
431 | 433 |
|
432 | 434 |
public: |
433 | 435 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
434 | 436 |
typedef typename Parent::Key Key; |
435 | 437 |
typedef typename Parent::Value Value; |
436 | 438 |
|
437 | 439 |
///Constructor |
438 | 440 |
SimpleMap(const M &_m) : m(_m) {}; |
439 | 441 |
///\e |
440 | 442 |
Value operator[](Key k) const {return m[k];} |
441 | 443 |
}; |
442 | 444 |
|
443 | 445 |
///Returns a \c SimpleMap class |
444 | 446 |
|
445 | 447 |
///This function just returns a \c SimpleMap class. |
446 | 448 |
///\relates SimpleMap |
447 | 449 |
template<typename M> |
448 | 450 |
inline SimpleMap<M> simpleMap(const M &m) { |
449 | 451 |
return SimpleMap<M>(m); |
450 | 452 |
} |
451 | 453 |
|
452 | 454 |
///Simple writable wrapping of a map |
453 | 455 |
|
454 | 456 |
///This \ref concepts::ReadWriteMap "read-write map" returns the simple |
455 | 457 |
///wrapping of the given map. Sometimes the reference maps cannot be |
456 | 458 |
///combined with simple read-write maps. This map adaptor wraps the |
457 | 459 |
///given map to simple read-write map. |
458 | 460 |
/// |
459 | 461 |
///\sa SimpleMap |
460 | 462 |
/// |
461 | 463 |
/// \todo Revise the misleading name |
462 | 464 |
template<typename M> |
463 | 465 |
class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
464 | 466 |
M& m; |
465 | 467 |
|
466 | 468 |
public: |
467 | 469 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
468 | 470 |
typedef typename Parent::Key Key; |
469 | 471 |
typedef typename Parent::Value Value; |
470 | 472 |
|
471 | 473 |
///Constructor |
472 | 474 |
SimpleWriteMap(M &_m) : m(_m) {}; |
473 | 475 |
///\e |
474 | 476 |
Value operator[](Key k) const {return m[k];} |
475 | 477 |
///\e |
476 | 478 |
void set(Key k, const Value& c) { m.set(k, c); } |
477 | 479 |
}; |
478 | 480 |
|
479 | 481 |
///Returns a \c SimpleWriteMap class |
480 | 482 |
|
481 | 483 |
///This function just returns a \c SimpleWriteMap class. |
482 | 484 |
///\relates SimpleWriteMap |
483 | 485 |
template<typename M> |
484 | 486 |
inline SimpleWriteMap<M> simpleWriteMap(M &m) { |
485 | 487 |
return SimpleWriteMap<M>(m); |
486 | 488 |
} |
487 | 489 |
|
488 | 490 |
///Sum of two maps |
489 | 491 |
|
490 | 492 |
///This \ref concepts::ReadMap "read only map" returns the sum of the two |
491 | 493 |
///given maps. |
492 | 494 |
///Its \c Key and \c Value are inherited from \c M1. |
493 |
///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
|
495 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
|
494 | 496 |
template<typename M1, typename M2> |
495 | 497 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
496 | 498 |
const M1& m1; |
497 | 499 |
const M2& m2; |
498 | 500 |
|
499 | 501 |
public: |
500 | 502 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
501 | 503 |
typedef typename Parent::Key Key; |
502 | 504 |
typedef typename Parent::Value Value; |
503 | 505 |
|
504 | 506 |
///Constructor |
505 | 507 |
AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
506 | 508 |
///\e |
507 | 509 |
Value operator[](Key k) const {return m1[k]+m2[k];} |
508 | 510 |
}; |
509 | 511 |
|
510 | 512 |
///Returns an \c AddMap class |
511 | 513 |
|
512 | 514 |
///This function just returns an \c AddMap class. |
513 |
///\todo |
|
515 |
///\todo Extend the documentation: how to call these type of functions? |
|
514 | 516 |
/// |
515 | 517 |
///\relates AddMap |
516 | 518 |
template<typename M1, typename M2> |
517 | 519 |
inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) { |
518 | 520 |
return AddMap<M1, M2>(m1,m2); |
519 | 521 |
} |
520 | 522 |
|
521 | 523 |
///Shift a map with a constant. |
522 | 524 |
|
523 | 525 |
///This \ref concepts::ReadMap "read only map" returns the sum of the |
524 | 526 |
///given map and a constant value. |
525 | 527 |
///Its \c Key and \c Value are inherited from \c M. |
526 | 528 |
/// |
527 | 529 |
///Actually, |
528 | 530 |
///\code |
529 | 531 |
/// ShiftMap<X> sh(x,v); |
530 | 532 |
///\endcode |
531 | 533 |
///is equivalent to |
532 | 534 |
///\code |
533 | 535 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
534 | 536 |
/// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
535 | 537 |
///\endcode |
536 | 538 |
/// |
537 | 539 |
///\sa ShiftWriteMap |
538 | 540 |
template<typename M, typename C = typename M::Value> |
539 | 541 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
540 | 542 |
const M& m; |
541 | 543 |
C v; |
542 | 544 |
public: |
543 | 545 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
544 | 546 |
typedef typename Parent::Key Key; |
545 | 547 |
typedef typename Parent::Value Value; |
546 | 548 |
|
547 | 549 |
///Constructor |
548 | 550 |
|
549 | 551 |
///Constructor. |
550 | 552 |
///\param _m is the undelying map. |
551 | 553 |
///\param _v is the shift value. |
552 | 554 |
ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
553 | 555 |
///\e |
554 | 556 |
Value operator[](Key k) const {return m[k] + v;} |
555 | 557 |
}; |
556 | 558 |
|
557 | 559 |
///Shift a map with a constant (ReadWrite version). |
558 | 560 |
|
559 | 561 |
///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the |
560 | 562 |
///given map and a constant value. It makes also possible to write the map. |
561 | 563 |
///Its \c Key and \c Value are inherited from \c M. |
562 | 564 |
/// |
563 | 565 |
///\sa ShiftMap |
564 | 566 |
template<typename M, typename C = typename M::Value> |
565 | 567 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
566 | 568 |
M& m; |
567 | 569 |
C v; |
568 | 570 |
public: |
569 | 571 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
570 | 572 |
typedef typename Parent::Key Key; |
571 | 573 |
typedef typename Parent::Value Value; |
572 | 574 |
|
573 | 575 |
///Constructor |
574 | 576 |
|
575 | 577 |
///Constructor. |
576 | 578 |
///\param _m is the undelying map. |
577 | 579 |
///\param _v is the shift value. |
578 | 580 |
ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
579 | 581 |
/// \e |
580 | 582 |
Value operator[](Key k) const {return m[k] + v;} |
581 | 583 |
/// \e |
582 | 584 |
void set(Key k, const Value& c) { m.set(k, c - v); } |
583 | 585 |
}; |
584 | 586 |
|
585 | 587 |
///Returns a \c ShiftMap class |
586 | 588 |
|
587 | 589 |
///This function just returns a \c ShiftMap class. |
588 | 590 |
///\relates ShiftMap |
589 | 591 |
template<typename M, typename C> |
590 | 592 |
inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { |
591 | 593 |
return ShiftMap<M, C>(m,v); |
592 | 594 |
} |
593 | 595 |
|
594 | 596 |
///Returns a \c ShiftWriteMap class |
595 | 597 |
|
596 | 598 |
///This function just returns a \c ShiftWriteMap class. |
597 | 599 |
///\relates ShiftWriteMap |
598 | 600 |
template<typename M, typename C> |
599 | 601 |
inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { |
600 | 602 |
return ShiftWriteMap<M, C>(m,v); |
601 | 603 |
} |
602 | 604 |
|
603 | 605 |
///Difference of two maps |
604 | 606 |
|
605 | 607 |
///This \ref concepts::ReadMap "read only map" returns the difference |
606 | 608 |
///of the values of the two given maps. |
607 | 609 |
///Its \c Key and \c Value are inherited from \c M1. |
608 | 610 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
609 | 611 |
/// |
610 | 612 |
/// \todo Revise the misleading name |
611 | 613 |
template<typename M1, typename M2> |
612 | 614 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
613 | 615 |
const M1& m1; |
614 | 616 |
const M2& m2; |
615 | 617 |
public: |
616 | 618 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
617 | 619 |
typedef typename Parent::Key Key; |
618 | 620 |
typedef typename Parent::Value Value; |
619 | 621 |
|
620 | 622 |
///Constructor |
621 | 623 |
SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
622 | 624 |
/// \e |
623 | 625 |
Value operator[](Key k) const {return m1[k]-m2[k];} |
624 | 626 |
}; |
625 | 627 |
|
626 | 628 |
///Returns a \c SubMap class |
627 | 629 |
|
628 | 630 |
///This function just returns a \c SubMap class. |
629 | 631 |
/// |
630 | 632 |
///\relates SubMap |
631 | 633 |
template<typename M1, typename M2> |
632 | 634 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
633 | 635 |
return SubMap<M1, M2>(m1, m2); |
634 | 636 |
} |
635 | 637 |
|
636 | 638 |
///Product of two maps |
637 | 639 |
|
638 | 640 |
///This \ref concepts::ReadMap "read only map" returns the product of the |
639 | 641 |
///values of the two given maps. |
640 | 642 |
///Its \c Key and \c Value are inherited from \c M1. |
641 | 643 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
... | ... |
@@ -882,520 +884,521 @@ |
882 | 884 |
///This function is specialized for adaptable binary function |
883 | 885 |
///classes and C++ functions. |
884 | 886 |
/// |
885 | 887 |
///\relates CombineMap |
886 | 888 |
template<typename M1, typename M2, typename F, typename V> |
887 | 889 |
inline CombineMap<M1, M2, F, V> |
888 | 890 |
combineMap(const M1& m1,const M2& m2, const F& f) { |
889 | 891 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
890 | 892 |
} |
891 | 893 |
|
892 | 894 |
template<typename M1, typename M2, typename F> |
893 | 895 |
inline CombineMap<M1, M2, F, typename F::result_type> |
894 | 896 |
combineMap(const M1& m1, const M2& m2, const F& f) { |
895 | 897 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
896 | 898 |
} |
897 | 899 |
|
898 | 900 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
899 | 901 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
900 | 902 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
901 | 903 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
902 | 904 |
} |
903 | 905 |
|
904 | 906 |
///Negative value of a map |
905 | 907 |
|
906 | 908 |
///This \ref concepts::ReadMap "read only map" returns the negative |
907 | 909 |
///value of the value returned by the given map. |
908 | 910 |
///Its \c Key and \c Value are inherited from \c M. |
909 | 911 |
///The unary \c - operator must be defined for \c Value, of course. |
910 | 912 |
/// |
911 | 913 |
///\sa NegWriteMap |
912 | 914 |
template<typename M> |
913 | 915 |
class NegMap : public MapBase<typename M::Key, typename M::Value> { |
914 | 916 |
const M& m; |
915 | 917 |
public: |
916 | 918 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
917 | 919 |
typedef typename Parent::Key Key; |
918 | 920 |
typedef typename Parent::Value Value; |
919 | 921 |
|
920 | 922 |
///Constructor |
921 | 923 |
NegMap(const M &_m) : m(_m) {}; |
922 | 924 |
/// \e |
923 | 925 |
Value operator[](Key k) const {return -m[k];} |
924 | 926 |
}; |
925 | 927 |
|
926 | 928 |
///Negative value of a map (ReadWrite version) |
927 | 929 |
|
928 | 930 |
///This \ref concepts::ReadWriteMap "read-write map" returns the negative |
929 | 931 |
///value of the value returned by the given map. |
930 | 932 |
///Its \c Key and \c Value are inherited from \c M. |
931 | 933 |
///The unary \c - operator must be defined for \c Value, of course. |
932 | 934 |
/// |
933 | 935 |
/// \sa NegMap |
934 | 936 |
template<typename M> |
935 | 937 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
936 | 938 |
M& m; |
937 | 939 |
public: |
938 | 940 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
939 | 941 |
typedef typename Parent::Key Key; |
940 | 942 |
typedef typename Parent::Value Value; |
941 | 943 |
|
942 | 944 |
///Constructor |
943 | 945 |
NegWriteMap(M &_m) : m(_m) {}; |
944 | 946 |
/// \e |
945 | 947 |
Value operator[](Key k) const {return -m[k];} |
946 | 948 |
/// \e |
947 | 949 |
void set(Key k, const Value& v) { m.set(k, -v); } |
948 | 950 |
}; |
949 | 951 |
|
950 | 952 |
///Returns a \c NegMap class |
951 | 953 |
|
952 | 954 |
///This function just returns a \c NegMap class. |
953 | 955 |
///\relates NegMap |
954 | 956 |
template <typename M> |
955 | 957 |
inline NegMap<M> negMap(const M &m) { |
956 | 958 |
return NegMap<M>(m); |
957 | 959 |
} |
958 | 960 |
|
959 | 961 |
///Returns a \c NegWriteMap class |
960 | 962 |
|
961 | 963 |
///This function just returns a \c NegWriteMap class. |
962 | 964 |
///\relates NegWriteMap |
963 | 965 |
template <typename M> |
964 | 966 |
inline NegWriteMap<M> negMap(M &m) { |
965 | 967 |
return NegWriteMap<M>(m); |
966 | 968 |
} |
967 | 969 |
|
968 | 970 |
///Absolute value of a map |
969 | 971 |
|
970 | 972 |
///This \ref concepts::ReadMap "read only map" returns the absolute value |
971 | 973 |
///of the value returned by the given map. |
972 | 974 |
///Its \c Key and \c Value are inherited from \c M. |
973 | 975 |
///\c Value must be comparable to \c 0 and the unary \c - |
974 | 976 |
///operator must be defined for it, of course. |
975 | 977 |
template<typename M> |
976 | 978 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
977 | 979 |
const M& m; |
978 | 980 |
public: |
979 | 981 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
980 | 982 |
typedef typename Parent::Key Key; |
981 | 983 |
typedef typename Parent::Value Value; |
982 | 984 |
|
983 | 985 |
///Constructor |
984 | 986 |
AbsMap(const M &_m) : m(_m) {}; |
985 | 987 |
/// \e |
986 | 988 |
Value operator[](Key k) const { |
987 | 989 |
Value tmp = m[k]; |
988 | 990 |
return tmp >= 0 ? tmp : -tmp; |
989 | 991 |
} |
990 | 992 |
|
991 | 993 |
}; |
992 | 994 |
|
993 | 995 |
///Returns an \c AbsMap class |
994 | 996 |
|
995 | 997 |
///This function just returns an \c AbsMap class. |
996 | 998 |
///\relates AbsMap |
997 | 999 |
template<typename M> |
998 | 1000 |
inline AbsMap<M> absMap(const M &m) { |
999 | 1001 |
return AbsMap<M>(m); |
1000 | 1002 |
} |
1001 | 1003 |
|
1002 | 1004 |
///Converts an STL style functor to a map |
1003 | 1005 |
|
1004 | 1006 |
///This \ref concepts::ReadMap "read only map" returns the value |
1005 | 1007 |
///of a given functor. |
1006 | 1008 |
/// |
1007 | 1009 |
///Template parameters \c K and \c V will become its |
1008 | 1010 |
///\c Key and \c Value. |
1009 | 1011 |
///In most cases they have to be given explicitly because a |
1010 |
///functor typically does not provide |
|
1012 |
///functor typically does not provide \c argument_type and |
|
1013 |
///\c result_type typedefs. |
|
1011 | 1014 |
/// |
1012 | 1015 |
///Parameter \c F is the type of the used functor. |
1013 | 1016 |
/// |
1014 | 1017 |
///\sa MapFunctor |
1015 | 1018 |
template<typename F, |
1016 | 1019 |
typename K = typename F::argument_type, |
1017 | 1020 |
typename V = typename F::result_type> |
1018 | 1021 |
class FunctorMap : public MapBase<K, V> { |
1019 | 1022 |
F f; |
1020 | 1023 |
public: |
1021 | 1024 |
typedef MapBase<K, V> Parent; |
1022 | 1025 |
typedef typename Parent::Key Key; |
1023 | 1026 |
typedef typename Parent::Value Value; |
1024 | 1027 |
|
1025 | 1028 |
///Constructor |
1026 | 1029 |
FunctorMap(const F &_f = F()) : f(_f) {} |
1027 | 1030 |
/// \e |
1028 | 1031 |
Value operator[](Key k) const { return f(k);} |
1029 | 1032 |
}; |
1030 | 1033 |
|
1031 | 1034 |
///Returns a \c FunctorMap class |
1032 | 1035 |
|
1033 | 1036 |
///This function just returns a \c FunctorMap class. |
1034 | 1037 |
/// |
1035 |
///It is specialized for adaptable function classes and |
|
1036 |
///C++ functions. |
|
1038 |
///This function is specialized for adaptable binary function |
|
1039 |
///classes and C++ functions. |
|
1040 |
/// |
|
1037 | 1041 |
///\relates FunctorMap |
1038 | 1042 |
template<typename K, typename V, typename F> inline |
1039 | 1043 |
FunctorMap<F, K, V> functorMap(const F &f) { |
1040 | 1044 |
return FunctorMap<F, K, V>(f); |
1041 | 1045 |
} |
1042 | 1046 |
|
1043 | 1047 |
template <typename F> inline |
1044 | 1048 |
FunctorMap<F, typename F::argument_type, typename F::result_type> |
1045 | 1049 |
functorMap(const F &f) { |
1046 | 1050 |
return FunctorMap<F, typename F::argument_type, |
1047 | 1051 |
typename F::result_type>(f); |
1048 | 1052 |
} |
1049 | 1053 |
|
1050 | 1054 |
template <typename K, typename V> inline |
1051 | 1055 |
FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) { |
1052 | 1056 |
return FunctorMap<V (*)(K), K, V>(f); |
1053 | 1057 |
} |
1054 | 1058 |
|
1055 | 1059 |
|
1056 | 1060 |
///Converts a map to an STL style (unary) functor |
1057 | 1061 |
|
1058 | 1062 |
///This class Converts a map to an STL style (unary) functor. |
1059 |
/// |
|
1063 |
///That is it provides an <tt>operator()</tt> to read its values. |
|
1060 | 1064 |
/// |
1061 | 1065 |
///For the sake of convenience it also works as |
1062 | 1066 |
///a ususal \ref concepts::ReadMap "readable map", |
1063 | 1067 |
///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
1064 | 1068 |
/// |
1065 | 1069 |
///\sa FunctorMap |
1066 | 1070 |
template <typename M> |
1067 | 1071 |
class MapFunctor : public MapBase<typename M::Key, typename M::Value> { |
1068 | 1072 |
const M& m; |
1069 | 1073 |
public: |
1070 | 1074 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
1071 | 1075 |
typedef typename Parent::Key Key; |
1072 | 1076 |
typedef typename Parent::Value Value; |
1073 | 1077 |
|
1074 | 1078 |
typedef typename M::Key argument_type; |
1075 | 1079 |
typedef typename M::Value result_type; |
1076 | 1080 |
|
1077 | 1081 |
///Constructor |
1078 | 1082 |
MapFunctor(const M &_m) : m(_m) {}; |
1079 | 1083 |
///\e |
1080 | 1084 |
Value operator()(Key k) const {return m[k];} |
1081 | 1085 |
///\e |
1082 | 1086 |
Value operator[](Key k) const {return m[k];} |
1083 | 1087 |
}; |
1084 | 1088 |
|
1085 | 1089 |
///Returns a \c MapFunctor class |
1086 | 1090 |
|
1087 | 1091 |
///This function just returns a \c MapFunctor class. |
1088 | 1092 |
///\relates MapFunctor |
1089 | 1093 |
template<typename M> |
1090 | 1094 |
inline MapFunctor<M> mapFunctor(const M &m) { |
1091 | 1095 |
return MapFunctor<M>(m); |
1092 | 1096 |
} |
1093 | 1097 |
|
1094 |
/// |
|
1098 |
///Just readable version of \ref ForkWriteMap |
|
1095 | 1099 |
|
1096 | 1100 |
///This map has two \ref concepts::ReadMap "readable map" |
1097 | 1101 |
///parameters and each read request will be passed just to the |
1098 |
///first map. This class is the just readable map type of |
|
1102 |
///first map. This class is the just readable map type of \c ForkWriteMap. |
|
1099 | 1103 |
/// |
1100 | 1104 |
///The \c Key and \c Value are inherited from \c M1. |
1101 |
///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
|
1105 |
///The \c Key and \c Value of \c M2 must be convertible from those of \c M1. |
|
1102 | 1106 |
/// |
1103 | 1107 |
///\sa ForkWriteMap |
1104 | 1108 |
/// |
1105 | 1109 |
/// \todo Why is it needed? |
1106 | 1110 |
template<typename M1, typename M2> |
1107 | 1111 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
1108 | 1112 |
const M1& m1; |
1109 | 1113 |
const M2& m2; |
1110 | 1114 |
public: |
1111 | 1115 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
1112 | 1116 |
typedef typename Parent::Key Key; |
1113 | 1117 |
typedef typename Parent::Value Value; |
1114 | 1118 |
|
1115 | 1119 |
///Constructor |
1116 | 1120 |
ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; |
1117 | 1121 |
/// \e |
1118 | 1122 |
Value operator[](Key k) const {return m1[k];} |
1119 | 1123 |
}; |
1120 | 1124 |
|
1121 | 1125 |
|
1122 | 1126 |
///Applies all map setting operations to two maps |
1123 | 1127 |
|
1124 | 1128 |
///This map has two \ref concepts::WriteMap "writable map" |
1125 | 1129 |
///parameters and each write request will be passed to both of them. |
1126 | 1130 |
///If \c M1 is also \ref concepts::ReadMap "readable", |
1127 | 1131 |
///then the read operations will return the |
1128 | 1132 |
///corresponding values of \c M1. |
1129 | 1133 |
/// |
1130 | 1134 |
///The \c Key and \c Value are inherited from \c M1. |
1131 |
///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
|
1135 |
///The \c Key and \c Value of \c M2 must be convertible from those of \c M1. |
|
1132 | 1136 |
/// |
1133 | 1137 |
///\sa ForkMap |
1134 | 1138 |
template<typename M1, typename M2> |
1135 | 1139 |
class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> { |
1136 | 1140 |
M1& m1; |
1137 | 1141 |
M2& m2; |
1138 | 1142 |
public: |
1139 | 1143 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
1140 | 1144 |
typedef typename Parent::Key Key; |
1141 | 1145 |
typedef typename Parent::Value Value; |
1142 | 1146 |
|
1143 | 1147 |
///Constructor |
1144 | 1148 |
ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; |
1145 | 1149 |
///\e |
1146 | 1150 |
Value operator[](Key k) const {return m1[k];} |
1147 | 1151 |
///\e |
1148 | 1152 |
void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} |
1149 | 1153 |
}; |
1150 | 1154 |
|
1151 | 1155 |
///Returns a \c ForkMap class |
1152 | 1156 |
|
1153 | 1157 |
///This function just returns a \c ForkMap class. |
1154 | 1158 |
///\relates ForkMap |
1155 | 1159 |
template <typename M1, typename M2> |
1156 | 1160 |
inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { |
1157 | 1161 |
return ForkMap<M1, M2>(m1,m2); |
1158 | 1162 |
} |
1159 | 1163 |
|
1160 | 1164 |
///Returns a \c ForkWriteMap class |
1161 | 1165 |
|
1162 | 1166 |
///This function just returns a \c ForkWriteMap class. |
1163 | 1167 |
///\relates ForkWriteMap |
1164 | 1168 |
template <typename M1, typename M2> |
1165 | 1169 |
inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { |
1166 | 1170 |
return ForkWriteMap<M1, M2>(m1,m2); |
1167 | 1171 |
} |
1168 | 1172 |
|
1169 | 1173 |
|
1170 | 1174 |
|
1171 | 1175 |
/* ************* BOOL MAPS ******************* */ |
1172 | 1176 |
|
1173 | 1177 |
///Logical 'not' of a map |
1174 | 1178 |
|
1175 | 1179 |
///This bool \ref concepts::ReadMap "read only map" returns the |
1176 | 1180 |
///logical negation of the value returned by the given map. |
1177 |
///Its \c Key is inherited from \c M, its Value is \c bool. |
|
1181 |
///Its \c Key is inherited from \c M, its \c Value is \c bool. |
|
1178 | 1182 |
/// |
1179 | 1183 |
///\sa NotWriteMap |
1180 | 1184 |
template <typename M> |
1181 | 1185 |
class NotMap : public MapBase<typename M::Key, bool> { |
1182 | 1186 |
const M& m; |
1183 | 1187 |
public: |
1184 | 1188 |
typedef MapBase<typename M::Key, bool> Parent; |
1185 | 1189 |
typedef typename Parent::Key Key; |
1186 | 1190 |
typedef typename Parent::Value Value; |
1187 | 1191 |
|
1188 | 1192 |
/// Constructor |
1189 | 1193 |
NotMap(const M &_m) : m(_m) {}; |
1190 | 1194 |
///\e |
1191 | 1195 |
Value operator[](Key k) const {return !m[k];} |
1192 | 1196 |
}; |
1193 | 1197 |
|
1194 | 1198 |
///Logical 'not' of a map (ReadWrie version) |
1195 | 1199 |
|
1196 | 1200 |
///This bool \ref concepts::ReadWriteMap "read-write map" returns the |
1197 | 1201 |
///logical negation of the value returned by the given map. When it is set, |
1198 | 1202 |
///the opposite value is set to the original map. |
1199 |
///Its \c Key is inherited from \c M, its Value is \c bool. |
|
1203 |
///Its \c Key is inherited from \c M, its \c Value is \c bool. |
|
1200 | 1204 |
/// |
1201 | 1205 |
///\sa NotMap |
1202 | 1206 |
template <typename M> |
1203 | 1207 |
class NotWriteMap : public MapBase<typename M::Key, bool> { |
1204 | 1208 |
M& m; |
1205 | 1209 |
public: |
1206 | 1210 |
typedef MapBase<typename M::Key, bool> Parent; |
1207 | 1211 |
typedef typename Parent::Key Key; |
1208 | 1212 |
typedef typename Parent::Value Value; |
1209 | 1213 |
|
1210 | 1214 |
/// Constructor |
1211 | 1215 |
NotWriteMap(M &_m) : m(_m) {}; |
1212 | 1216 |
///\e |
1213 | 1217 |
Value operator[](Key k) const {return !m[k];} |
1214 | 1218 |
///\e |
1215 | 1219 |
void set(Key k, bool v) { m.set(k, !v); } |
1216 | 1220 |
}; |
1217 | 1221 |
|
1218 | 1222 |
///Returns a \c NotMap class |
1219 | 1223 |
|
1220 | 1224 |
///This function just returns a \c NotMap class. |
1221 | 1225 |
///\relates NotMap |
1222 | 1226 |
template <typename M> |
1223 | 1227 |
inline NotMap<M> notMap(const M &m) { |
1224 | 1228 |
return NotMap<M>(m); |
1225 | 1229 |
} |
1226 | 1230 |
|
1227 | 1231 |
///Returns a \c NotWriteMap class |
1228 | 1232 |
|
1229 | 1233 |
///This function just returns a \c NotWriteMap class. |
1230 | 1234 |
///\relates NotWriteMap |
1231 | 1235 |
template <typename M> |
1232 | 1236 |
inline NotWriteMap<M> notMap(M &m) { |
1233 | 1237 |
return NotWriteMap<M>(m); |
1234 | 1238 |
} |
1235 | 1239 |
|
1236 | 1240 |
namespace _maps_bits { |
1237 | 1241 |
|
1238 | 1242 |
template <typename Value> |
1239 | 1243 |
struct Identity { |
1240 | 1244 |
typedef Value argument_type; |
1241 | 1245 |
typedef Value result_type; |
1242 | 1246 |
Value operator()(const Value& val) const { |
1243 | 1247 |
return val; |
1244 | 1248 |
} |
1245 | 1249 |
}; |
1246 | 1250 |
|
1247 | 1251 |
template <typename _Iterator, typename Enable = void> |
1248 | 1252 |
struct IteratorTraits { |
1249 | 1253 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1250 | 1254 |
}; |
1251 | 1255 |
|
1252 | 1256 |
template <typename _Iterator> |
1253 | 1257 |
struct IteratorTraits<_Iterator, |
1254 | 1258 |
typename exists<typename _Iterator::container_type>::type> |
1255 | 1259 |
{ |
1256 | 1260 |
typedef typename _Iterator::container_type::value_type Value; |
1257 | 1261 |
}; |
1258 | 1262 |
|
1259 | 1263 |
} |
1260 | 1264 |
|
1261 | 1265 |
|
1262 | 1266 |
/// \brief Writable bool map for logging each \c true assigned element |
1263 | 1267 |
/// |
1264 | 1268 |
/// A \ref concepts::ReadWriteMap "read-write" bool map for logging |
1265 |
/// each \c true assigned element, i.e it |
|
1269 |
/// each \c true assigned element, i.e it copies all the keys set |
|
1266 | 1270 |
/// to \c true to the given iterator. |
1267 | 1271 |
/// |
1268 | 1272 |
/// \note The container of the iterator should contain space |
1269 | 1273 |
/// for each element. |
1270 | 1274 |
/// |
1271 |
/// The following example shows how you can write the edges found by the Prim |
|
1272 |
/// algorithm directly |
|
1273 |
/// |
|
1275 |
/// The following example shows how you can write the edges found by |
|
1276 |
/// the \ref Prim algorithm directly to the standard output. |
|
1274 | 1277 |
///\code |
1275 | 1278 |
/// typedef IdMap<Graph, Edge> EdgeIdMap; |
1276 | 1279 |
/// EdgeIdMap edgeId(graph); |
1277 | 1280 |
/// |
1278 | 1281 |
/// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor; |
1279 | 1282 |
/// EdgeIdFunctor edgeIdFunctor(edgeId); |
1280 | 1283 |
/// |
1281 | 1284 |
/// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor> |
1282 | 1285 |
/// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor); |
1283 | 1286 |
/// |
1284 | 1287 |
/// prim(graph, cost, writerMap); |
1285 | 1288 |
///\endcode |
1286 | 1289 |
/// |
1287 | 1290 |
///\sa BackInserterBoolMap |
1288 | 1291 |
///\sa FrontInserterBoolMap |
1289 | 1292 |
///\sa InserterBoolMap |
1290 | 1293 |
/// |
1291 | 1294 |
///\todo Revise the name of this class and the related ones. |
1292 | 1295 |
template <typename _Iterator, |
1293 | 1296 |
typename _Functor = |
1294 | 1297 |
_maps_bits::Identity<typename _maps_bits:: |
1295 | 1298 |
IteratorTraits<_Iterator>::Value> > |
1296 | 1299 |
class StoreBoolMap { |
1297 | 1300 |
public: |
1298 | 1301 |
typedef _Iterator Iterator; |
1299 | 1302 |
|
1300 | 1303 |
typedef typename _Functor::argument_type Key; |
1301 | 1304 |
typedef bool Value; |
1302 | 1305 |
|
1303 | 1306 |
typedef _Functor Functor; |
1304 | 1307 |
|
1305 | 1308 |
/// Constructor |
1306 | 1309 |
StoreBoolMap(Iterator it, const Functor& functor = Functor()) |
1307 | 1310 |
: _begin(it), _end(it), _functor(functor) {} |
1308 | 1311 |
|
1309 | 1312 |
/// Gives back the given iterator set for the first key |
1310 | 1313 |
Iterator begin() const { |
1311 | 1314 |
return _begin; |
1312 | 1315 |
} |
1313 | 1316 |
|
1314 | 1317 |
/// Gives back the the 'after the last' iterator |
1315 | 1318 |
Iterator end() const { |
1316 | 1319 |
return _end; |
1317 | 1320 |
} |
1318 | 1321 |
|
1319 | 1322 |
/// The \c set function of the map |
1320 | 1323 |
void set(const Key& key, Value value) const { |
1321 | 1324 |
if (value) { |
1322 | 1325 |
*_end++ = _functor(key); |
1323 | 1326 |
} |
1324 | 1327 |
} |
1325 | 1328 |
|
1326 | 1329 |
private: |
1327 | 1330 |
Iterator _begin; |
1328 | 1331 |
mutable Iterator _end; |
1329 | 1332 |
Functor _functor; |
1330 | 1333 |
}; |
1331 | 1334 |
|
1332 | 1335 |
/// \brief Writable bool map for logging each \c true assigned element in |
1333 | 1336 |
/// a back insertable container. |
1334 | 1337 |
/// |
1335 | 1338 |
/// Writable bool map for logging each \c true assigned element by pushing |
1336 | 1339 |
/// them into a back insertable container. |
1337 | 1340 |
/// It can be used to retrieve the items into a standard |
1338 | 1341 |
/// container. The next example shows how you can store the |
1339 | 1342 |
/// edges found by the Prim algorithm in a vector. |
1340 | 1343 |
/// |
1341 | 1344 |
///\code |
1342 | 1345 |
/// vector<Edge> span_tree_edges; |
1343 | 1346 |
/// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges); |
1344 | 1347 |
/// prim(graph, cost, inserter_map); |
1345 | 1348 |
///\endcode |
1346 | 1349 |
/// |
1347 | 1350 |
///\sa StoreBoolMap |
1348 | 1351 |
///\sa FrontInserterBoolMap |
1349 | 1352 |
///\sa InserterBoolMap |
1350 | 1353 |
template <typename Container, |
1351 | 1354 |
typename Functor = |
1352 | 1355 |
_maps_bits::Identity<typename Container::value_type> > |
1353 | 1356 |
class BackInserterBoolMap { |
1354 | 1357 |
public: |
1355 | 1358 |
typedef typename Functor::argument_type Key; |
1356 | 1359 |
typedef bool Value; |
1357 | 1360 |
|
1358 | 1361 |
/// Constructor |
1359 | 1362 |
BackInserterBoolMap(Container& _container, |
1360 | 1363 |
const Functor& _functor = Functor()) |
1361 | 1364 |
: container(_container), functor(_functor) {} |
1362 | 1365 |
|
1363 | 1366 |
/// The \c set function of the map |
1364 | 1367 |
void set(const Key& key, Value value) { |
1365 | 1368 |
if (value) { |
1366 | 1369 |
container.push_back(functor(key)); |
1367 | 1370 |
} |
1368 | 1371 |
} |
1369 | 1372 |
|
1370 | 1373 |
private: |
1371 | 1374 |
Container& container; |
1372 | 1375 |
Functor functor; |
1373 | 1376 |
}; |
1374 | 1377 |
|
1375 | 1378 |
/// \brief Writable bool map for logging each \c true assigned element in |
1376 | 1379 |
/// a front insertable container. |
1377 | 1380 |
/// |
1378 | 1381 |
/// Writable bool map for logging each \c true assigned element by pushing |
1379 | 1382 |
/// them into a front insertable container. |
1380 | 1383 |
/// It can be used to retrieve the items into a standard |
1381 | 1384 |
/// container. For example see \ref BackInserterBoolMap. |
1382 | 1385 |
/// |
1383 | 1386 |
///\sa BackInserterBoolMap |
1384 | 1387 |
///\sa InserterBoolMap |
1385 | 1388 |
template <typename Container, |
1386 | 1389 |
typename Functor = |
1387 | 1390 |
_maps_bits::Identity<typename Container::value_type> > |
1388 | 1391 |
class FrontInserterBoolMap { |
1389 | 1392 |
public: |
1390 | 1393 |
typedef typename Functor::argument_type Key; |
1391 | 1394 |
typedef bool Value; |
1392 | 1395 |
|
1393 | 1396 |
/// Constructor |
1394 | 1397 |
FrontInserterBoolMap(Container& _container, |
1395 | 1398 |
const Functor& _functor = Functor()) |
1396 | 1399 |
: container(_container), functor(_functor) {} |
1397 | 1400 |
|
1398 | 1401 |
/// The \c set function of the map |
1399 | 1402 |
void set(const Key& key, Value value) { |
1400 | 1403 |
if (value) { |
1401 | 1404 |
container.push_front(functor(key)); |
0 comments (0 inline)