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