| 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. It provides the necessary type definitions
|
| 43 |
43 |
/// required by the map %concepts.
|
| 44 |
44 |
template<typename K, typename V>
|
| 45 |
45 |
class MapBase {
|
| 46 |
46 |
public:
|
| 47 |
47 |
/// \biref The key type of the map.
|
| 48 |
48 |
typedef K Key;
|
| 49 |
49 |
/// \brief The value type of the map.
|
| 50 |
50 |
/// (The type of objects associated with the keys).
|
| 51 |
51 |
typedef V Value;
|
| 52 |
52 |
};
|
| 53 |
53 |
|
| 54 |
54 |
|
| 55 |
55 |
/// Null map. (a.k.a. DoNothingMap)
|
| 56 |
56 |
|
| 57 |
57 |
/// This map can be used if you have to provide a map only for
|
| 58 |
58 |
/// its type definitions, or if you have to provide a writable map,
|
| 59 |
59 |
/// but data written to it is not required (i.e. it will be sent to
|
| 60 |
60 |
/// <tt>/dev/null</tt>).
|
| 61 |
61 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
| 62 |
62 |
///
|
| 63 |
63 |
/// \sa ConstMap
|
| 64 |
64 |
template<typename K, typename V>
|
| 65 |
65 |
class NullMap : public MapBase<K, V> {
|
| 66 |
66 |
public:
|
| 67 |
67 |
typedef MapBase<K, V> Parent;
|
| 68 |
68 |
typedef typename Parent::Key Key;
|
| 69 |
69 |
typedef typename Parent::Value Value;
|
| 70 |
70 |
|
| 71 |
71 |
/// Gives back a default constructed element.
|
| 72 |
72 |
Value operator[](const Key&) const { return Value(); }
|
| 73 |
73 |
/// Absorbs the value.
|
| 74 |
74 |
void set(const Key&, const Value&) {}
|
| 75 |
75 |
};
|
| 76 |
76 |
|
| 77 |
77 |
/// Returns a \ref NullMap class
|
| 78 |
78 |
|
| 79 |
79 |
/// This function just returns a \ref NullMap class.
|
| 80 |
80 |
/// \relates NullMap
|
| 81 |
81 |
template <typename K, typename V>
|
| 82 |
82 |
NullMap<K, V> nullMap() {
|
| 83 |
83 |
return NullMap<K, V>();
|
| 84 |
84 |
}
|
| 85 |
85 |
|
| 86 |
86 |
|
| 87 |
87 |
/// Constant map.
|
| 88 |
88 |
|
| 89 |
89 |
/// This \ref concepts::ReadMap "readable map" assigns a specified
|
| 90 |
90 |
/// value to each key.
|
| 91 |
91 |
///
|
| 92 |
92 |
/// In other aspects it is equivalent to \ref NullMap.
|
| 93 |
93 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
|
| 94 |
94 |
/// concept, but it absorbs the data written to it.
|
| 95 |
95 |
///
|
| 96 |
96 |
/// The simplest way of using this map is through the constMap()
|
| 97 |
97 |
/// function.
|
| 98 |
98 |
///
|
| 99 |
99 |
/// \sa NullMap
|
| 100 |
100 |
/// \sa IdentityMap
|
| 101 |
101 |
template<typename K, typename V>
|
| 102 |
102 |
class ConstMap : public MapBase<K, V> {
|
| 103 |
103 |
private:
|
| 104 |
104 |
V _value;
|
| 105 |
105 |
public:
|
| 106 |
106 |
typedef MapBase<K, V> Parent;
|
| 107 |
107 |
typedef typename Parent::Key Key;
|
| 108 |
108 |
typedef typename Parent::Value Value;
|
| 109 |
109 |
|
| 110 |
110 |
/// Default constructor
|
| 111 |
111 |
|
| 112 |
112 |
/// Default constructor.
|
| 113 |
113 |
/// The value of the map will be default constructed.
|
| 114 |
114 |
ConstMap() {}
|
| 115 |
115 |
|
| 116 |
116 |
/// Constructor with specified initial value
|
| 117 |
117 |
|
| 118 |
118 |
/// Constructor with specified initial value.
|
| 119 |
|
/// \param v is the initial value of the map.
|
|
119 |
/// \param v The initial value of the map.
|
| 120 |
120 |
ConstMap(const Value &v) : _value(v) {}
|
| 121 |
121 |
|
| 122 |
122 |
/// Gives back the specified value.
|
| 123 |
123 |
Value operator[](const Key&) const { return _value; }
|
| 124 |
124 |
|
| 125 |
125 |
/// Absorbs the value.
|
| 126 |
126 |
void set(const Key&, const Value&) {}
|
| 127 |
127 |
|
| 128 |
128 |
/// Sets the value that is assigned to each key.
|
| 129 |
129 |
void setAll(const Value &v) {
|
| 130 |
130 |
_value = v;
|
| 131 |
131 |
}
|
| 132 |
132 |
|
| 133 |
133 |
template<typename V1>
|
| 134 |
134 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
|
| 135 |
135 |
};
|
| 136 |
136 |
|
| 137 |
137 |
/// Returns a \ref ConstMap class
|
| 138 |
138 |
|
| 139 |
139 |
/// This function just returns a \ref ConstMap class.
|
| 140 |
140 |
/// \relates ConstMap
|
| 141 |
141 |
template<typename K, typename V>
|
| 142 |
142 |
inline ConstMap<K, V> constMap(const V &v) {
|
| 143 |
143 |
return ConstMap<K, V>(v);
|
| 144 |
144 |
}
|
| 145 |
145 |
|
|
146 |
template<typename K, typename V>
|
|
147 |
inline ConstMap<K, V> constMap() {
|
|
148 |
return ConstMap<K, V>();
|
|
149 |
}
|
|
150 |
|
| 146 |
151 |
|
| 147 |
152 |
template<typename T, T v>
|
| 148 |
153 |
struct Const {};
|
| 149 |
154 |
|
| 150 |
155 |
/// Constant map with inlined constant value.
|
| 151 |
156 |
|
| 152 |
157 |
/// This \ref concepts::ReadMap "readable map" assigns a specified
|
| 153 |
158 |
/// value to each key.
|
| 154 |
159 |
///
|
| 155 |
160 |
/// In other aspects it is equivalent to \ref NullMap.
|
| 156 |
161 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
|
| 157 |
162 |
/// concept, but it absorbs the data written to it.
|
| 158 |
163 |
///
|
| 159 |
164 |
/// The simplest way of using this map is through the constMap()
|
| 160 |
165 |
/// function.
|
| 161 |
166 |
///
|
| 162 |
167 |
/// \sa NullMap
|
| 163 |
168 |
/// \sa IdentityMap
|
| 164 |
169 |
template<typename K, typename V, V v>
|
| 165 |
170 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
|
| 166 |
171 |
public:
|
| 167 |
172 |
typedef MapBase<K, V> Parent;
|
| 168 |
173 |
typedef typename Parent::Key Key;
|
| 169 |
174 |
typedef typename Parent::Value Value;
|
| 170 |
175 |
|
| 171 |
176 |
/// Constructor.
|
| 172 |
177 |
ConstMap() {}
|
| 173 |
178 |
|
| 174 |
179 |
/// Gives back the specified value.
|
| 175 |
180 |
Value operator[](const Key&) const { return v; }
|
| 176 |
181 |
|
| 177 |
182 |
/// Absorbs the value.
|
| 178 |
183 |
void set(const Key&, const Value&) {}
|
| 179 |
184 |
};
|
| 180 |
185 |
|
| 181 |
186 |
/// Returns a \ref ConstMap class with inlined constant value
|
| 182 |
187 |
|
| 183 |
188 |
/// This function just returns a \ref ConstMap class with inlined
|
| 184 |
189 |
/// constant value.
|
| 185 |
190 |
/// \relates ConstMap
|
| 186 |
191 |
template<typename K, typename V, V v>
|
| 187 |
192 |
inline ConstMap<K, Const<V, v> > constMap() {
|
| 188 |
193 |
return ConstMap<K, Const<V, v> >();
|
| 189 |
194 |
}
|
| 190 |
195 |
|
| 191 |
196 |
|
| 192 |
197 |
/// Identity map.
|
| 193 |
198 |
|
| 194 |
199 |
/// This \ref concepts::ReadMap "read-only map" gives back the given
|
| 195 |
200 |
/// key as value without any modification.
|
| 196 |
201 |
///
|
| 197 |
202 |
/// \sa ConstMap
|
| 198 |
203 |
template <typename T>
|
| 199 |
204 |
class IdentityMap : public MapBase<T, T> {
|
| 200 |
205 |
public:
|
| 201 |
206 |
typedef MapBase<T, T> Parent;
|
| 202 |
207 |
typedef typename Parent::Key Key;
|
| 203 |
208 |
typedef typename Parent::Value Value;
|
| 204 |
209 |
|
| 205 |
210 |
/// Gives back the given value without any modification.
|
| 206 |
211 |
Value operator[](const Key &k) const {
|
| 207 |
212 |
return k;
|
| 208 |
213 |
}
|
| 209 |
214 |
};
|
| 210 |
215 |
|
| 211 |
216 |
/// Returns an \ref IdentityMap class
|
| 212 |
217 |
|
| 213 |
218 |
/// This function just returns an \ref IdentityMap class.
|
| 214 |
219 |
/// \relates IdentityMap
|
| 215 |
220 |
template<typename T>
|
| 216 |
221 |
inline IdentityMap<T> identityMap() {
|
| 217 |
222 |
return IdentityMap<T>();
|
| 218 |
223 |
}
|
| 219 |
224 |
|
| 220 |
225 |
|
| 221 |
226 |
/// \brief Map for storing values for integer keys from the range
|
| 222 |
227 |
/// <tt>[0..size-1]</tt>.
|
| 223 |
228 |
///
|
| 224 |
229 |
/// This map is essentially a wrapper for \c std::vector. It assigns
|
| 225 |
230 |
/// values to integer keys from the range <tt>[0..size-1]</tt>.
|
| 226 |
231 |
/// It can be used with some data structures, for example
|
| 227 |
232 |
/// \ref UnionFind, \ref BinHeap, when the used items are small
|
| 228 |
233 |
/// integers. This map conforms the \ref concepts::ReferenceMap
|
| 229 |
234 |
/// "ReferenceMap" concept.
|
| 230 |
235 |
///
|
| 231 |
236 |
/// The simplest way of using this map is through the rangeMap()
|
| 232 |
237 |
/// function.
|
| 233 |
238 |
template <typename V>
|
| 234 |
239 |
class RangeMap : public MapBase<int, V> {
|
| 235 |
240 |
template <typename V1>
|
| 236 |
241 |
friend class RangeMap;
|
| 237 |
242 |
private:
|
| 238 |
243 |
|
| 239 |
244 |
typedef std::vector<V> Vector;
|
| 240 |
245 |
Vector _vector;
|
| 241 |
246 |
|
| 242 |
247 |
public:
|
| 243 |
248 |
|
| 244 |
249 |
typedef MapBase<int, V> Parent;
|
| 245 |
250 |
/// Key type
|
| 246 |
251 |
typedef typename Parent::Key Key;
|
| 247 |
252 |
/// Value type
|
| 248 |
253 |
typedef typename Parent::Value Value;
|
| 249 |
254 |
/// Reference type
|
| 250 |
255 |
typedef typename Vector::reference Reference;
|
| 251 |
256 |
/// Const reference type
|
| 252 |
257 |
typedef typename Vector::const_reference ConstReference;
|
| 253 |
258 |
|
| 254 |
259 |
typedef True ReferenceMapTag;
|
| 255 |
260 |
|
| 256 |
261 |
public:
|
| 257 |
262 |
|
| 258 |
263 |
/// Constructor with specified default value.
|
| 259 |
264 |
RangeMap(int size = 0, const Value &value = Value())
|
| 260 |
265 |
: _vector(size, value) {}
|
| 261 |
266 |
|
| 262 |
267 |
/// Constructs the map from an appropriate \c std::vector.
|
| 263 |
268 |
template <typename V1>
|
| 264 |
269 |
RangeMap(const std::vector<V1>& vector)
|
| 265 |
270 |
: _vector(vector.begin(), vector.end()) {}
|
| 266 |
271 |
|
| 267 |
272 |
/// Constructs the map from another \ref RangeMap.
|
| 268 |
273 |
template <typename V1>
|
| 269 |
274 |
RangeMap(const RangeMap<V1> &c)
|
| 270 |
275 |
: _vector(c._vector.begin(), c._vector.end()) {}
|
| 271 |
276 |
|
| 272 |
277 |
/// Returns the size of the map.
|
| 273 |
278 |
int size() {
|
| 274 |
279 |
return _vector.size();
|
| 275 |
280 |
}
|
| 276 |
281 |
|
| 277 |
282 |
/// Resizes the map.
|
| 278 |
283 |
|
| 279 |
284 |
/// Resizes the underlying \c std::vector container, so changes the
|
| 280 |
285 |
/// keyset of the map.
|
| 281 |
286 |
/// \param size The new size of the map. The new keyset will be the
|
| 282 |
287 |
/// range <tt>[0..size-1]</tt>.
|
| 283 |
288 |
/// \param value The default value to assign to the new keys.
|
| 284 |
289 |
void resize(int size, const Value &value = Value()) {
|
| 285 |
290 |
_vector.resize(size, value);
|
| 286 |
291 |
}
|
| 287 |
292 |
|
| 288 |
293 |
private:
|
| 289 |
294 |
|
| 290 |
295 |
RangeMap& operator=(const RangeMap&);
|
| 291 |
296 |
|
| 292 |
297 |
public:
|
| 293 |
298 |
|
| 294 |
299 |
///\e
|
| 295 |
300 |
Reference operator[](const Key &k) {
|
| 296 |
301 |
return _vector[k];
|
| 297 |
302 |
}
|
| 298 |
303 |
|
| 299 |
304 |
///\e
|
| 300 |
305 |
ConstReference operator[](const Key &k) const {
|
| 301 |
306 |
return _vector[k];
|
| 302 |
307 |
}
|
| 303 |
308 |
|
| 304 |
309 |
///\e
|
| 305 |
310 |
void set(const Key &k, const Value &v) {
|
| 306 |
311 |
_vector[k] = v;
|
| 307 |
312 |
}
|
| 308 |
313 |
};
|
| 309 |
314 |
|
| 310 |
315 |
/// Returns a \ref RangeMap class
|
| 311 |
316 |
|
| 312 |
317 |
/// This function just returns a \ref RangeMap class.
|
| 313 |
318 |
/// \relates RangeMap
|
| 314 |
319 |
template<typename V>
|
| 315 |
320 |
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
|
| 316 |
321 |
return RangeMap<V>(size, value);
|
| 317 |
322 |
}
|
| 318 |
323 |
|
| 319 |
324 |
/// \brief Returns a \ref RangeMap class created from an appropriate
|
| 320 |
325 |
/// \c std::vector
|
| 321 |
326 |
|
| 322 |
327 |
/// This function just returns a \ref RangeMap class created from an
|
| 323 |
328 |
/// appropriate \c std::vector.
|
| 324 |
329 |
/// \relates RangeMap
|
| 325 |
330 |
template<typename V>
|
| 326 |
331 |
inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
|
| 327 |
332 |
return RangeMap<V>(vector);
|
| 328 |
333 |
}
|
| 329 |
334 |
|
| 330 |
335 |
|
| 331 |
336 |
/// Map type based on \c std::map
|
| 332 |
337 |
|
| 333 |
338 |
/// This map is essentially a wrapper for \c std::map with addition
|
| 334 |
339 |
/// that you can specify a default value for the keys that are not
|
| 335 |
340 |
/// stored actually. This value can be different from the default
|
| 336 |
341 |
/// contructed value (i.e. \c %Value()).
|
| 337 |
342 |
/// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
|
| ... |
... |
@@ -424,385 +429,385 @@
|
| 424 |
429 |
|
| 425 |
430 |
///\e
|
| 426 |
431 |
void setAll(const Value &v) {
|
| 427 |
432 |
_value = v;
|
| 428 |
433 |
_map.clear();
|
| 429 |
434 |
}
|
| 430 |
435 |
};
|
| 431 |
436 |
|
| 432 |
437 |
/// Returns a \ref SparseMap class
|
| 433 |
438 |
|
| 434 |
439 |
/// This function just returns a \ref SparseMap class with specified
|
| 435 |
440 |
/// default value.
|
| 436 |
441 |
/// \relates SparseMap
|
| 437 |
442 |
template<typename K, typename V, typename Compare>
|
| 438 |
443 |
inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
|
| 439 |
444 |
return SparseMap<K, V, Compare>(value);
|
| 440 |
445 |
}
|
| 441 |
446 |
|
| 442 |
447 |
template<typename K, typename V>
|
| 443 |
448 |
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
|
| 444 |
449 |
return SparseMap<K, V, std::less<K> >(value);
|
| 445 |
450 |
}
|
| 446 |
451 |
|
| 447 |
452 |
/// \brief Returns a \ref SparseMap class created from an appropriate
|
| 448 |
453 |
/// \c std::map
|
| 449 |
454 |
|
| 450 |
455 |
/// This function just returns a \ref SparseMap class created from an
|
| 451 |
456 |
/// appropriate \c std::map.
|
| 452 |
457 |
/// \relates SparseMap
|
| 453 |
458 |
template<typename K, typename V, typename Compare>
|
| 454 |
459 |
inline SparseMap<K, V, Compare>
|
| 455 |
460 |
sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
|
| 456 |
461 |
{
|
| 457 |
462 |
return SparseMap<K, V, Compare>(map, value);
|
| 458 |
463 |
}
|
| 459 |
464 |
|
| 460 |
465 |
/// @}
|
| 461 |
466 |
|
| 462 |
467 |
/// \addtogroup map_adaptors
|
| 463 |
468 |
/// @{
|
| 464 |
469 |
|
| 465 |
470 |
/// Composition of two maps
|
| 466 |
471 |
|
| 467 |
472 |
/// This \ref concepts::ReadMap "read-only map" returns the
|
| 468 |
473 |
/// composition of two given maps. That is to say, if \c m1 is of
|
| 469 |
474 |
/// type \c M1 and \c m2 is of \c M2, then for
|
| 470 |
475 |
/// \code
|
| 471 |
476 |
/// ComposeMap<M1, M2> cm(m1,m2);
|
| 472 |
477 |
/// \endcode
|
| 473 |
478 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
|
| 474 |
479 |
///
|
| 475 |
480 |
/// The \c Key type of the map is inherited from \c M2 and the
|
| 476 |
481 |
/// \c Value type is from \c M1.
|
| 477 |
482 |
/// \c M2::Value must be convertible to \c M1::Key.
|
| 478 |
483 |
///
|
| 479 |
484 |
/// The simplest way of using this map is through the composeMap()
|
| 480 |
485 |
/// function.
|
| 481 |
486 |
///
|
| 482 |
487 |
/// \sa CombineMap
|
| 483 |
488 |
///
|
| 484 |
489 |
/// \todo Check the requirements.
|
| 485 |
490 |
template <typename M1, typename M2>
|
| 486 |
491 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
|
| 487 |
492 |
const M1 &_m1;
|
| 488 |
493 |
const M2 &_m2;
|
| 489 |
494 |
public:
|
| 490 |
495 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent;
|
| 491 |
496 |
typedef typename Parent::Key Key;
|
| 492 |
497 |
typedef typename Parent::Value Value;
|
| 493 |
498 |
|
| 494 |
499 |
/// Constructor
|
| 495 |
500 |
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 496 |
501 |
|
| 497 |
502 |
/// \e
|
| 498 |
503 |
typename MapTraits<M1>::ConstReturnValue
|
| 499 |
504 |
operator[](const Key &k) const { return _m1[_m2[k]]; }
|
| 500 |
505 |
};
|
| 501 |
506 |
|
| 502 |
507 |
/// Returns a \ref ComposeMap class
|
| 503 |
508 |
|
| 504 |
509 |
/// This function just returns a \ref ComposeMap class.
|
| 505 |
510 |
///
|
| 506 |
511 |
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
|
| 507 |
512 |
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
|
| 508 |
513 |
/// will be equal to <tt>m1[m2[x]]</tt>.
|
| 509 |
514 |
///
|
| 510 |
515 |
/// \relates ComposeMap
|
| 511 |
516 |
template <typename M1, typename M2>
|
| 512 |
517 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
|
| 513 |
518 |
return ComposeMap<M1, M2>(m1, m2);
|
| 514 |
519 |
}
|
| 515 |
520 |
|
| 516 |
521 |
|
| 517 |
522 |
/// Combination of two maps using an STL (binary) functor.
|
| 518 |
523 |
|
| 519 |
524 |
/// This \ref concepts::ReadMap "read-only map" takes two maps and a
|
| 520 |
525 |
/// binary functor and returns the combination of the two given maps
|
| 521 |
526 |
/// using the functor.
|
| 522 |
527 |
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
|
| 523 |
528 |
/// and \c f is of \c F, then for
|
| 524 |
529 |
/// \code
|
| 525 |
530 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f);
|
| 526 |
531 |
/// \endcode
|
| 527 |
532 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
|
| 528 |
533 |
///
|
| 529 |
534 |
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key
|
| 530 |
535 |
/// must be convertible to \c M2::Key) and the \c Value type is \c V.
|
| 531 |
536 |
/// \c M2::Value and \c M1::Value must be convertible to the
|
| 532 |
537 |
/// corresponding input parameter of \c F and the return type of \c F
|
| 533 |
538 |
/// must be convertible to \c V.
|
| 534 |
539 |
///
|
| 535 |
540 |
/// The simplest way of using this map is through the combineMap()
|
| 536 |
541 |
/// function.
|
| 537 |
542 |
///
|
| 538 |
543 |
/// \sa ComposeMap
|
| 539 |
544 |
///
|
| 540 |
545 |
/// \todo Check the requirements.
|
| 541 |
546 |
template<typename M1, typename M2, typename F,
|
| 542 |
547 |
typename V = typename F::result_type>
|
| 543 |
548 |
class CombineMap : public MapBase<typename M1::Key, V> {
|
| 544 |
549 |
const M1 &_m1;
|
| 545 |
550 |
const M2 &_m2;
|
| 546 |
551 |
F _f;
|
| 547 |
552 |
public:
|
| 548 |
553 |
typedef MapBase<typename M1::Key, V> Parent;
|
| 549 |
554 |
typedef typename Parent::Key Key;
|
| 550 |
555 |
typedef typename Parent::Value Value;
|
| 551 |
556 |
|
| 552 |
557 |
/// Constructor
|
| 553 |
558 |
CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
|
| 554 |
559 |
: _m1(m1), _m2(m2), _f(f) {}
|
| 555 |
560 |
/// \e
|
| 556 |
561 |
Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
|
| 557 |
562 |
};
|
| 558 |
563 |
|
| 559 |
564 |
/// Returns a \ref CombineMap class
|
| 560 |
565 |
|
| 561 |
566 |
/// This function just returns a \ref CombineMap class.
|
| 562 |
567 |
///
|
| 563 |
568 |
/// For example, if \c m1 and \c m2 are both maps with \c double
|
| 564 |
569 |
/// values, then
|
| 565 |
570 |
/// \code
|
| 566 |
571 |
/// combineMap(m1,m2,std::plus<double>())
|
| 567 |
572 |
/// \endcode
|
| 568 |
573 |
/// is equivalent to
|
| 569 |
574 |
/// \code
|
| 570 |
575 |
/// addMap(m1,m2)
|
| 571 |
576 |
/// \endcode
|
| 572 |
577 |
///
|
| 573 |
578 |
/// This function is specialized for adaptable binary function
|
| 574 |
579 |
/// classes and C++ functions.
|
| 575 |
580 |
///
|
| 576 |
581 |
/// \relates CombineMap
|
| 577 |
582 |
template<typename M1, typename M2, typename F, typename V>
|
| 578 |
583 |
inline CombineMap<M1, M2, F, V>
|
| 579 |
584 |
combineMap(const M1 &m1, const M2 &m2, const F &f) {
|
| 580 |
585 |
return CombineMap<M1, M2, F, V>(m1,m2,f);
|
| 581 |
586 |
}
|
| 582 |
587 |
|
| 583 |
588 |
template<typename M1, typename M2, typename F>
|
| 584 |
589 |
inline CombineMap<M1, M2, F, typename F::result_type>
|
| 585 |
590 |
combineMap(const M1 &m1, const M2 &m2, const F &f) {
|
| 586 |
591 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
|
| 587 |
592 |
}
|
| 588 |
593 |
|
| 589 |
594 |
template<typename M1, typename M2, typename K1, typename K2, typename V>
|
| 590 |
595 |
inline CombineMap<M1, M2, V (*)(K1, K2), V>
|
| 591 |
596 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
|
| 592 |
597 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
|
| 593 |
598 |
}
|
| 594 |
599 |
|
| 595 |
600 |
|
| 596 |
601 |
/// Converts an STL style (unary) functor to a map
|
| 597 |
602 |
|
| 598 |
603 |
/// This \ref concepts::ReadMap "read-only map" returns the value
|
| 599 |
604 |
/// of a given functor. Actually, it just wraps the functor and
|
| 600 |
605 |
/// provides the \c Key and \c Value typedefs.
|
| 601 |
606 |
///
|
| 602 |
607 |
/// Template parameters \c K and \c V will become its \c Key and
|
| 603 |
608 |
/// \c Value. In most cases they have to be given explicitly because
|
| 604 |
609 |
/// a functor typically does not provide \c argument_type and
|
| 605 |
610 |
/// \c result_type typedefs.
|
| 606 |
611 |
/// Parameter \c F is the type of the used functor.
|
| 607 |
612 |
///
|
| 608 |
613 |
/// The simplest way of using this map is through the functorToMap()
|
| 609 |
614 |
/// function.
|
| 610 |
615 |
///
|
| 611 |
616 |
/// \sa MapToFunctor
|
| 612 |
617 |
template<typename F,
|
| 613 |
618 |
typename K = typename F::argument_type,
|
| 614 |
619 |
typename V = typename F::result_type>
|
| 615 |
620 |
class FunctorToMap : public MapBase<K, V> {
|
| 616 |
|
const F &_f;
|
|
621 |
F _f;
|
| 617 |
622 |
public:
|
| 618 |
623 |
typedef MapBase<K, V> Parent;
|
| 619 |
624 |
typedef typename Parent::Key Key;
|
| 620 |
625 |
typedef typename Parent::Value Value;
|
| 621 |
626 |
|
| 622 |
627 |
/// Constructor
|
| 623 |
628 |
FunctorToMap(const F &f = F()) : _f(f) {}
|
| 624 |
629 |
/// \e
|
| 625 |
630 |
Value operator[](const Key &k) const { return _f(k); }
|
| 626 |
631 |
};
|
| 627 |
632 |
|
| 628 |
633 |
/// Returns a \ref FunctorToMap class
|
| 629 |
634 |
|
| 630 |
635 |
/// This function just returns a \ref FunctorToMap class.
|
| 631 |
636 |
///
|
| 632 |
637 |
/// This function is specialized for adaptable binary function
|
| 633 |
638 |
/// classes and C++ functions.
|
| 634 |
639 |
///
|
| 635 |
640 |
/// \relates FunctorToMap
|
| 636 |
641 |
template<typename K, typename V, typename F>
|
| 637 |
642 |
inline FunctorToMap<F, K, V> functorToMap(const F &f) {
|
| 638 |
643 |
return FunctorToMap<F, K, V>(f);
|
| 639 |
644 |
}
|
| 640 |
645 |
|
| 641 |
646 |
template <typename F>
|
| 642 |
647 |
inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
|
| 643 |
648 |
functorToMap(const F &f)
|
| 644 |
649 |
{
|
| 645 |
650 |
return FunctorToMap<F, typename F::argument_type,
|
| 646 |
651 |
typename F::result_type>(f);
|
| 647 |
652 |
}
|
| 648 |
653 |
|
| 649 |
654 |
template <typename K, typename V>
|
| 650 |
655 |
inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
|
| 651 |
656 |
return FunctorToMap<V (*)(K), K, V>(f);
|
| 652 |
657 |
}
|
| 653 |
658 |
|
| 654 |
659 |
|
| 655 |
660 |
/// Converts a map to an STL style (unary) functor
|
| 656 |
661 |
|
| 657 |
662 |
/// This class converts a map to an STL style (unary) functor.
|
| 658 |
663 |
/// That is it provides an <tt>operator()</tt> to read its values.
|
| 659 |
664 |
///
|
| 660 |
665 |
/// For the sake of convenience it also works as a usual
|
| 661 |
666 |
/// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
|
| 662 |
667 |
/// and the \c Key and \c Value typedefs also exist.
|
| 663 |
668 |
///
|
| 664 |
669 |
/// The simplest way of using this map is through the mapToFunctor()
|
| 665 |
670 |
/// function.
|
| 666 |
671 |
///
|
| 667 |
672 |
///\sa FunctorToMap
|
| 668 |
673 |
template <typename M>
|
| 669 |
674 |
class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
|
| 670 |
675 |
const M &_m;
|
| 671 |
676 |
public:
|
| 672 |
677 |
typedef MapBase<typename M::Key, typename M::Value> Parent;
|
| 673 |
678 |
typedef typename Parent::Key Key;
|
| 674 |
679 |
typedef typename Parent::Value Value;
|
| 675 |
680 |
|
| 676 |
681 |
typedef typename Parent::Key argument_type;
|
| 677 |
682 |
typedef typename Parent::Value result_type;
|
| 678 |
683 |
|
| 679 |
684 |
/// Constructor
|
| 680 |
685 |
MapToFunctor(const M &m) : _m(m) {}
|
| 681 |
686 |
/// \e
|
| 682 |
687 |
Value operator()(const Key &k) const { return _m[k]; }
|
| 683 |
688 |
/// \e
|
| 684 |
689 |
Value operator[](const Key &k) const { return _m[k]; }
|
| 685 |
690 |
};
|
| 686 |
691 |
|
| 687 |
692 |
/// Returns a \ref MapToFunctor class
|
| 688 |
693 |
|
| 689 |
694 |
/// This function just returns a \ref MapToFunctor class.
|
| 690 |
695 |
/// \relates MapToFunctor
|
| 691 |
696 |
template<typename M>
|
| 692 |
697 |
inline MapToFunctor<M> mapToFunctor(const M &m) {
|
| 693 |
698 |
return MapToFunctor<M>(m);
|
| 694 |
699 |
}
|
| 695 |
700 |
|
| 696 |
701 |
|
| 697 |
702 |
/// \brief Map adaptor to convert the \c Value type of a map to
|
| 698 |
703 |
/// another type using the default conversion.
|
| 699 |
704 |
|
| 700 |
705 |
/// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
|
| 701 |
706 |
/// "readable map" to another type using the default conversion.
|
| 702 |
707 |
/// The \c Key type of it is inherited from \c M and the \c Value
|
| 703 |
708 |
/// type is \c V.
|
| 704 |
709 |
/// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
|
| 705 |
710 |
///
|
| 706 |
711 |
/// The simplest way of using this map is through the convertMap()
|
| 707 |
712 |
/// function.
|
| 708 |
713 |
template <typename M, typename V>
|
| 709 |
714 |
class ConvertMap : public MapBase<typename M::Key, V> {
|
| 710 |
715 |
const M &_m;
|
| 711 |
716 |
public:
|
| 712 |
717 |
typedef MapBase<typename M::Key, V> Parent;
|
| 713 |
718 |
typedef typename Parent::Key Key;
|
| 714 |
719 |
typedef typename Parent::Value Value;
|
| 715 |
720 |
|
| 716 |
721 |
/// Constructor
|
| 717 |
722 |
|
| 718 |
723 |
/// Constructor.
|
| 719 |
724 |
/// \param m The underlying map.
|
| 720 |
725 |
ConvertMap(const M &m) : _m(m) {}
|
| 721 |
726 |
|
| 722 |
727 |
/// \e
|
| 723 |
728 |
Value operator[](const Key &k) const { return _m[k]; }
|
| 724 |
729 |
};
|
| 725 |
730 |
|
| 726 |
731 |
/// Returns a \ref ConvertMap class
|
| 727 |
732 |
|
| 728 |
733 |
/// This function just returns a \ref ConvertMap class.
|
| 729 |
734 |
/// \relates ConvertMap
|
| 730 |
735 |
template<typename V, typename M>
|
| 731 |
736 |
inline ConvertMap<M, V> convertMap(const M &map) {
|
| 732 |
737 |
return ConvertMap<M, V>(map);
|
| 733 |
738 |
}
|
| 734 |
739 |
|
| 735 |
740 |
|
| 736 |
741 |
/// Applies all map setting operations to two maps
|
| 737 |
742 |
|
| 738 |
743 |
/// This map has two \ref concepts::WriteMap "writable map" parameters
|
| 739 |
744 |
/// and each write request will be passed to both of them.
|
| 740 |
745 |
/// If \c M1 is also \ref concepts::ReadMap "readable", then the read
|
| 741 |
746 |
/// operations will return the corresponding values of \c M1.
|
| 742 |
747 |
///
|
| 743 |
748 |
/// The \c Key and \c Value types are inherited from \c M1.
|
| 744 |
749 |
/// The \c Key and \c Value of \c M2 must be convertible from those
|
| 745 |
750 |
/// of \c M1.
|
| 746 |
751 |
///
|
| 747 |
752 |
/// The simplest way of using this map is through the forkMap()
|
| 748 |
753 |
/// function.
|
| 749 |
754 |
template<typename M1, typename M2>
|
| 750 |
755 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 751 |
756 |
M1 &_m1;
|
| 752 |
757 |
M2 &_m2;
|
| 753 |
758 |
public:
|
| 754 |
759 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
|
| 755 |
760 |
typedef typename Parent::Key Key;
|
| 756 |
761 |
typedef typename Parent::Value Value;
|
| 757 |
762 |
|
| 758 |
763 |
/// Constructor
|
| 759 |
764 |
ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
|
| 760 |
765 |
/// Returns the value associated with the given key in the first map.
|
| 761 |
766 |
Value operator[](const Key &k) const { return _m1[k]; }
|
| 762 |
767 |
/// Sets the value associated with the given key in both maps.
|
| 763 |
768 |
void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
|
| 764 |
769 |
};
|
| 765 |
770 |
|
| 766 |
771 |
/// Returns a \ref ForkMap class
|
| 767 |
772 |
|
| 768 |
773 |
/// This function just returns a \ref ForkMap class.
|
| 769 |
774 |
/// \relates ForkMap
|
| 770 |
775 |
template <typename M1, typename M2>
|
| 771 |
776 |
inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
|
| 772 |
777 |
return ForkMap<M1,M2>(m1,m2);
|
| 773 |
778 |
}
|
| 774 |
779 |
|
| 775 |
780 |
|
| 776 |
781 |
/// Sum of two maps
|
| 777 |
782 |
|
| 778 |
783 |
/// This \ref concepts::ReadMap "read-only map" returns the sum
|
| 779 |
784 |
/// of the values of the two given maps.
|
| 780 |
785 |
/// Its \c Key and \c Value types are inherited from \c M1.
|
| 781 |
786 |
/// The \c Key and \c Value of \c M2 must be convertible to those of
|
| 782 |
787 |
/// \c M1.
|
| 783 |
788 |
///
|
| 784 |
789 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
|
| 785 |
790 |
/// \code
|
| 786 |
791 |
/// AddMap<M1,M2> am(m1,m2);
|
| 787 |
792 |
/// \endcode
|
| 788 |
793 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
|
| 789 |
794 |
///
|
| 790 |
795 |
/// The simplest way of using this map is through the addMap()
|
| 791 |
796 |
/// function.
|
| 792 |
797 |
///
|
| 793 |
798 |
/// \sa SubMap, MulMap, DivMap
|
| 794 |
799 |
/// \sa ShiftMap, ShiftWriteMap
|
| 795 |
800 |
template<typename M1, typename M2>
|
| 796 |
801 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 797 |
802 |
const M1 &_m1;
|
| 798 |
803 |
const M2 &_m2;
|
| 799 |
804 |
public:
|
| 800 |
805 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
|
| 801 |
806 |
typedef typename Parent::Key Key;
|
| 802 |
807 |
typedef typename Parent::Value Value;
|
| 803 |
808 |
|
| 804 |
809 |
/// Constructor
|
| 805 |
810 |
AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 806 |
811 |
/// \e
|
| 807 |
812 |
Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
|
| 808 |
813 |
};
|