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 |
} |
0 comments (0 inline)