1 | /* -*- C++ -*- |
---|
2 | * lemon/maps.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_MAPS_H |
---|
18 | #define LEMON_MAPS_H |
---|
19 | |
---|
20 | #include <lemon/graph_utils.h> |
---|
21 | #include <lemon/utility.h> |
---|
22 | |
---|
23 | |
---|
24 | ///\file |
---|
25 | ///\ingroup maps |
---|
26 | ///\brief Miscellaneous property maps |
---|
27 | /// |
---|
28 | ///\todo This file has the same name as the concept file in concept/, |
---|
29 | /// and this is not easily detectable in docs... |
---|
30 | |
---|
31 | #include <map> |
---|
32 | |
---|
33 | namespace lemon { |
---|
34 | |
---|
35 | /// \addtogroup maps |
---|
36 | /// @{ |
---|
37 | |
---|
38 | /// Base class of maps. |
---|
39 | |
---|
40 | /// Base class of maps. |
---|
41 | /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
---|
42 | template<typename K, typename T> |
---|
43 | class MapBase |
---|
44 | { |
---|
45 | public: |
---|
46 | ///\e |
---|
47 | typedef K Key; |
---|
48 | ///\e |
---|
49 | typedef T Value; |
---|
50 | }; |
---|
51 | |
---|
52 | /// Null map. (a.k.a. DoNothingMap) |
---|
53 | |
---|
54 | /// If you have to provide a map only for its type definitions, |
---|
55 | /// or if you have to provide a writable map, but |
---|
56 | /// data written to it will sent to <tt>/dev/null</tt>... |
---|
57 | template<typename K, typename T> |
---|
58 | class NullMap : public MapBase<K,T> |
---|
59 | { |
---|
60 | public: |
---|
61 | |
---|
62 | typedef True NeedCopy; |
---|
63 | |
---|
64 | /// Gives back a default constructed element. |
---|
65 | T operator[](const K&) const { return T(); } |
---|
66 | /// Absorbs the value. |
---|
67 | void set(const K&, const T&) {} |
---|
68 | }; |
---|
69 | |
---|
70 | template <typename K, typename V> |
---|
71 | NullMap<K, V> nullMap() { |
---|
72 | return NullMap<K, V>(); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /// Constant map. |
---|
77 | |
---|
78 | /// This is a readable map which assigns a specified value to each key. |
---|
79 | /// In other aspects it is equivalent to the \ref NullMap. |
---|
80 | /// \todo set could be used to set the value. |
---|
81 | template<typename K, typename T> |
---|
82 | class ConstMap : public MapBase<K,T> |
---|
83 | { |
---|
84 | T v; |
---|
85 | public: |
---|
86 | |
---|
87 | typedef True NeedCopy; |
---|
88 | |
---|
89 | /// Default constructor |
---|
90 | |
---|
91 | /// The value of the map will be uninitialized. |
---|
92 | /// (More exactly it will be default constructed.) |
---|
93 | ConstMap() {} |
---|
94 | ///\e |
---|
95 | |
---|
96 | /// \param _v The initial value of the map. |
---|
97 | /// |
---|
98 | ConstMap(const T &_v) : v(_v) {} |
---|
99 | |
---|
100 | T operator[](const K&) const { return v; } |
---|
101 | void set(const K&, const T&) {} |
---|
102 | |
---|
103 | template<typename T1> |
---|
104 | struct rebind { |
---|
105 | typedef ConstMap<K,T1> other; |
---|
106 | }; |
---|
107 | |
---|
108 | template<typename T1> |
---|
109 | ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} |
---|
110 | }; |
---|
111 | |
---|
112 | ///Returns a \ref ConstMap class |
---|
113 | |
---|
114 | ///This function just returns a \ref ConstMap class. |
---|
115 | ///\relates ConstMap |
---|
116 | template<class K,class V> |
---|
117 | inline ConstMap<K,V> constMap(const V &v) |
---|
118 | { |
---|
119 | return ConstMap<K,V>(v); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | //\todo to document later |
---|
124 | template<typename T, T v> |
---|
125 | struct Const { }; |
---|
126 | //\todo to document later |
---|
127 | template<typename K, typename V, V v> |
---|
128 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> |
---|
129 | { |
---|
130 | public: |
---|
131 | ConstMap() { } |
---|
132 | V operator[](const K&) const { return v; } |
---|
133 | void set(const K&, const V&) { } |
---|
134 | }; |
---|
135 | |
---|
136 | /// \c std::map wrapper |
---|
137 | |
---|
138 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
139 | /// you can specify a default value different from \c Value() . |
---|
140 | /// |
---|
141 | /// \todo Provide allocator parameter... |
---|
142 | template <typename K, typename T, typename Compare = std::less<K> > |
---|
143 | class StdMap : public std::map<K,T,Compare> { |
---|
144 | typedef std::map<K,T,Compare> parent; |
---|
145 | T v; |
---|
146 | typedef typename parent::value_type PairType; |
---|
147 | |
---|
148 | public: |
---|
149 | ///\e |
---|
150 | typedef K Key; |
---|
151 | ///\e |
---|
152 | typedef T Value; |
---|
153 | ///\e |
---|
154 | typedef T& Reference; |
---|
155 | ///\e |
---|
156 | typedef const T& ConstReference; |
---|
157 | |
---|
158 | |
---|
159 | StdMap() : v() {} |
---|
160 | /// Constructor with specified default value |
---|
161 | StdMap(const T& _v) : v(_v) {} |
---|
162 | |
---|
163 | /// \brief Constructs the map from an appropriate std::map. |
---|
164 | /// |
---|
165 | /// \warning Inefficient: copies the content of \c m ! |
---|
166 | StdMap(const parent &m) : parent(m) {} |
---|
167 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
168 | /// specifies a default value. |
---|
169 | /// |
---|
170 | /// \warning Inefficient: copies the content of \c m ! |
---|
171 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
172 | |
---|
173 | template<typename T1, typename Comp1> |
---|
174 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
175 | //FIXME; |
---|
176 | } |
---|
177 | |
---|
178 | Reference operator[](const Key &k) { |
---|
179 | return insert(PairType(k,v)).first -> second; |
---|
180 | } |
---|
181 | ConstReference operator[](const Key &k) const { |
---|
182 | typename parent::iterator i = lower_bound(k); |
---|
183 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
184 | return v; |
---|
185 | return (*i).second; |
---|
186 | } |
---|
187 | void set(const Key &k, const T &t) { |
---|
188 | parent::operator[](k) = t; |
---|
189 | } |
---|
190 | |
---|
191 | /// Changes the default value of the map. |
---|
192 | /// \return Returns the previous default value. |
---|
193 | /// |
---|
194 | /// \warning The value of some keys (which has already been queried, but |
---|
195 | /// the value has been unchanged from the default) may change! |
---|
196 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
197 | |
---|
198 | template<typename T1> |
---|
199 | struct rebind { |
---|
200 | typedef StdMap<Key,T1,Compare> other; |
---|
201 | }; |
---|
202 | }; |
---|
203 | |
---|
204 | /// @} |
---|
205 | |
---|
206 | /// \addtogroup map_adaptors |
---|
207 | /// @{ |
---|
208 | |
---|
209 | /// \brief Identity mapping. |
---|
210 | /// |
---|
211 | /// This mapping gives back the given key as value without any |
---|
212 | /// modification. |
---|
213 | template <typename T> |
---|
214 | class IdentityMap { |
---|
215 | public: |
---|
216 | typedef T Key; |
---|
217 | typedef T Value; |
---|
218 | |
---|
219 | const Value& operator[](const Key& t) const { |
---|
220 | return t; |
---|
221 | } |
---|
222 | }; |
---|
223 | |
---|
224 | ///Convert the \c Value of a map to another type. |
---|
225 | |
---|
226 | ///This \ref concept::ReadMap "read only map" |
---|
227 | ///converts the \c Value of a maps to type \c T. |
---|
228 | ///Its \c Key is inherited from \c M. |
---|
229 | template<class M, class T> |
---|
230 | class ConvertMap { |
---|
231 | typename SmartConstReference<M>::Type m; |
---|
232 | public: |
---|
233 | |
---|
234 | typedef True NeedCopy; |
---|
235 | |
---|
236 | ///\e |
---|
237 | typedef typename M::Key Key; |
---|
238 | ///\e |
---|
239 | typedef T Value; |
---|
240 | |
---|
241 | ///Constructor |
---|
242 | |
---|
243 | ///Constructor |
---|
244 | ///\param _m is the underlying map |
---|
245 | ConvertMap(const M &_m) : m(_m) {}; |
---|
246 | |
---|
247 | /// \brief The subscript operator. |
---|
248 | /// |
---|
249 | /// The subscript operator. |
---|
250 | /// \param k The key |
---|
251 | /// \return The target of the edge |
---|
252 | Value operator[](Key k) const {return m[k];} |
---|
253 | }; |
---|
254 | |
---|
255 | ///Returns an \ref ConvertMap class |
---|
256 | |
---|
257 | ///This function just returns an \ref ConvertMap class. |
---|
258 | ///\relates ConvertMap |
---|
259 | ///\todo The order of the template parameters are changed. |
---|
260 | template<class T, class M> |
---|
261 | inline ConvertMap<M,T> convertMap(const M &m) |
---|
262 | { |
---|
263 | return ConvertMap<M,T>(m); |
---|
264 | } |
---|
265 | |
---|
266 | ///Sum of two maps |
---|
267 | |
---|
268 | ///This \ref concept::ReadMap "read only map" returns the sum of the two |
---|
269 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
270 | ///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
---|
271 | |
---|
272 | template<class M1,class M2> |
---|
273 | class AddMap |
---|
274 | { |
---|
275 | typename SmartConstReference<M1>::Type m1; |
---|
276 | typename SmartConstReference<M2>::Type m2; |
---|
277 | |
---|
278 | public: |
---|
279 | |
---|
280 | typedef True NeedCopy; |
---|
281 | |
---|
282 | ///\e |
---|
283 | typedef typename M1::Key Key; |
---|
284 | ///\e |
---|
285 | typedef typename M1::Value Value; |
---|
286 | |
---|
287 | ///Constructor |
---|
288 | AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
289 | Value operator[](Key k) const {return m1[k]+m2[k];} |
---|
290 | }; |
---|
291 | |
---|
292 | ///Returns an \ref AddMap class |
---|
293 | |
---|
294 | ///This function just returns an \ref AddMap class. |
---|
295 | ///\todo How to call these type of functions? |
---|
296 | /// |
---|
297 | ///\relates AddMap |
---|
298 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
299 | template<class M1,class M2> |
---|
300 | inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) |
---|
301 | { |
---|
302 | return AddMap<M1,M2>(m1,m2); |
---|
303 | } |
---|
304 | |
---|
305 | ///Shift a map with a constant. |
---|
306 | |
---|
307 | ///This \ref concept::ReadMap "read only map" returns the sum of the |
---|
308 | ///given map and a constant value. |
---|
309 | ///Its \c Key and \c Value is inherited from \c M. |
---|
310 | /// |
---|
311 | ///Actually, |
---|
312 | ///\code |
---|
313 | /// ShiftMap<X> sh(x,v); |
---|
314 | ///\endcode |
---|
315 | ///is equivalent with |
---|
316 | ///\code |
---|
317 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
318 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
319 | ///\endcode |
---|
320 | template<class M> |
---|
321 | class ShiftMap |
---|
322 | { |
---|
323 | typename SmartConstReference<M>::Type m; |
---|
324 | typename M::Value v; |
---|
325 | public: |
---|
326 | |
---|
327 | typedef True NeedCopy; |
---|
328 | ///\e |
---|
329 | typedef typename M::Key Key; |
---|
330 | ///\e |
---|
331 | typedef typename M::Value Value; |
---|
332 | |
---|
333 | ///Constructor |
---|
334 | |
---|
335 | ///Constructor |
---|
336 | ///\param _m is the undelying map |
---|
337 | ///\param _v is the shift value |
---|
338 | ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
339 | Value operator[](Key k) const {return m[k]+v;} |
---|
340 | }; |
---|
341 | |
---|
342 | ///Returns an \ref ShiftMap class |
---|
343 | |
---|
344 | ///This function just returns an \ref ShiftMap class. |
---|
345 | ///\relates ShiftMap |
---|
346 | ///\todo A better name is required. |
---|
347 | template<class M> |
---|
348 | inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) |
---|
349 | { |
---|
350 | return ShiftMap<M>(m,v); |
---|
351 | } |
---|
352 | |
---|
353 | ///Difference of two maps |
---|
354 | |
---|
355 | ///This \ref concept::ReadMap "read only map" returns the difference |
---|
356 | ///of the values of the two |
---|
357 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
358 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
359 | |
---|
360 | template<class M1,class M2> |
---|
361 | class SubMap |
---|
362 | { |
---|
363 | typename SmartConstReference<M1>::Type m1; |
---|
364 | typename SmartConstReference<M2>::Type m2; |
---|
365 | public: |
---|
366 | |
---|
367 | typedef True NeedCopy; |
---|
368 | ///\e |
---|
369 | typedef typename M1::Key Key; |
---|
370 | ///\e |
---|
371 | typedef typename M1::Value Value; |
---|
372 | |
---|
373 | ///Constructor |
---|
374 | SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
375 | Value operator[](Key k) const {return m1[k]-m2[k];} |
---|
376 | }; |
---|
377 | |
---|
378 | ///Returns a \ref SubMap class |
---|
379 | |
---|
380 | ///This function just returns a \ref SubMap class. |
---|
381 | /// |
---|
382 | ///\relates SubMap |
---|
383 | template<class M1,class M2> |
---|
384 | inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) |
---|
385 | { |
---|
386 | return SubMap<M1,M2>(m1,m2); |
---|
387 | } |
---|
388 | |
---|
389 | ///Product of two maps |
---|
390 | |
---|
391 | ///This \ref concept::ReadMap "read only map" returns the product of the |
---|
392 | ///values of the two |
---|
393 | ///given |
---|
394 | ///maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
395 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
396 | |
---|
397 | template<class M1,class M2> |
---|
398 | class MulMap |
---|
399 | { |
---|
400 | typename SmartConstReference<M1>::Type m1; |
---|
401 | typename SmartConstReference<M2>::Type m2; |
---|
402 | public: |
---|
403 | |
---|
404 | typedef True NeedCopy; |
---|
405 | ///\e |
---|
406 | typedef typename M1::Key Key; |
---|
407 | ///\e |
---|
408 | typedef typename M1::Value Value; |
---|
409 | |
---|
410 | ///Constructor |
---|
411 | MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
412 | Value operator[](Key k) const {return m1[k]*m2[k];} |
---|
413 | }; |
---|
414 | |
---|
415 | ///Returns a \ref MulMap class |
---|
416 | |
---|
417 | ///This function just returns a \ref MulMap class. |
---|
418 | ///\relates MulMap |
---|
419 | template<class M1,class M2> |
---|
420 | inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) |
---|
421 | { |
---|
422 | return MulMap<M1,M2>(m1,m2); |
---|
423 | } |
---|
424 | |
---|
425 | ///Scales a maps with a constant. |
---|
426 | |
---|
427 | ///This \ref concept::ReadMap "read only map" returns the value of the |
---|
428 | ///given map multiplied with a constant value. |
---|
429 | ///Its \c Key and \c Value is inherited from \c M. |
---|
430 | /// |
---|
431 | ///Actually, |
---|
432 | ///\code |
---|
433 | /// ScaleMap<X> sc(x,v); |
---|
434 | ///\endcode |
---|
435 | ///is equivalent with |
---|
436 | ///\code |
---|
437 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
438 | /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
---|
439 | ///\endcode |
---|
440 | template<class M> |
---|
441 | class ScaleMap |
---|
442 | { |
---|
443 | typename SmartConstReference<M>::Type m; |
---|
444 | typename M::Value v; |
---|
445 | public: |
---|
446 | |
---|
447 | typedef True NeedCopy; |
---|
448 | ///\e |
---|
449 | typedef typename M::Key Key; |
---|
450 | ///\e |
---|
451 | typedef typename M::Value Value; |
---|
452 | |
---|
453 | ///Constructor |
---|
454 | |
---|
455 | ///Constructor |
---|
456 | ///\param _m is the undelying map |
---|
457 | ///\param _v is the scaling value |
---|
458 | ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
459 | Value operator[](Key k) const {return m[k]*v;} |
---|
460 | }; |
---|
461 | |
---|
462 | ///Returns an \ref ScaleMap class |
---|
463 | |
---|
464 | ///This function just returns an \ref ScaleMap class. |
---|
465 | ///\relates ScaleMap |
---|
466 | ///\todo A better name is required. |
---|
467 | template<class M> |
---|
468 | inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) |
---|
469 | { |
---|
470 | return ScaleMap<M>(m,v); |
---|
471 | } |
---|
472 | |
---|
473 | ///Quotient of two maps |
---|
474 | |
---|
475 | ///This \ref concept::ReadMap "read only map" returns the quotient of the |
---|
476 | ///values of the two |
---|
477 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
478 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
479 | |
---|
480 | template<class M1,class M2> |
---|
481 | class DivMap |
---|
482 | { |
---|
483 | typename SmartConstReference<M1>::Type m1; |
---|
484 | typename SmartConstReference<M2>::Type m2; |
---|
485 | public: |
---|
486 | |
---|
487 | typedef True NeedCopy; |
---|
488 | ///\e |
---|
489 | typedef typename M1::Key Key; |
---|
490 | ///\e |
---|
491 | typedef typename M1::Value Value; |
---|
492 | |
---|
493 | ///Constructor |
---|
494 | DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
495 | Value operator[](Key k) const {return m1[k]/m2[k];} |
---|
496 | }; |
---|
497 | |
---|
498 | ///Returns a \ref DivMap class |
---|
499 | |
---|
500 | ///This function just returns a \ref DivMap class. |
---|
501 | ///\relates DivMap |
---|
502 | template<class M1,class M2> |
---|
503 | inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) |
---|
504 | { |
---|
505 | return DivMap<M1,M2>(m1,m2); |
---|
506 | } |
---|
507 | |
---|
508 | ///Composition of two maps |
---|
509 | |
---|
510 | ///This \ref concept::ReadMap "read only map" returns the composition of |
---|
511 | ///two |
---|
512 | ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is |
---|
513 | ///of \c M2, |
---|
514 | ///then for |
---|
515 | ///\code |
---|
516 | /// ComposeMap<M1,M2> cm(m1,m2); |
---|
517 | ///\endcode |
---|
518 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt> |
---|
519 | /// |
---|
520 | ///Its \c Key is inherited from \c M2 and its \c Value is from |
---|
521 | ///\c M1. |
---|
522 | ///The \c M2::Value must be convertible to \c M1::Key. |
---|
523 | ///\todo Check the requirements. |
---|
524 | |
---|
525 | template<class M1,class M2> |
---|
526 | class ComposeMap |
---|
527 | { |
---|
528 | typename SmartConstReference<M1>::Type m1; |
---|
529 | typename SmartConstReference<M2>::Type m2; |
---|
530 | public: |
---|
531 | |
---|
532 | typedef True NeedCopy; |
---|
533 | ///\e |
---|
534 | typedef typename M2::Key Key; |
---|
535 | ///\e |
---|
536 | typedef typename M1::Value Value; |
---|
537 | |
---|
538 | ///Constructor |
---|
539 | ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
540 | Value operator[](Key k) const {return m1[m2[k]];} |
---|
541 | }; |
---|
542 | ///Returns a \ref ComposeMap class |
---|
543 | |
---|
544 | ///This function just returns a \ref ComposeMap class. |
---|
545 | /// |
---|
546 | ///\relates ComposeMap |
---|
547 | template<class M1,class M2> |
---|
548 | inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) |
---|
549 | { |
---|
550 | return ComposeMap<M1,M2>(m1,m2); |
---|
551 | } |
---|
552 | |
---|
553 | ///Combines of two maps using an STL (binary) functor. |
---|
554 | |
---|
555 | ///Combines of two maps using an STL (binary) functor. |
---|
556 | /// |
---|
557 | /// |
---|
558 | ///This \ref concept::ReadMap "read only map" takes two maps and a |
---|
559 | ///binary functor and returns the composition of |
---|
560 | ///the two |
---|
561 | ///given maps unsing the functor. |
---|
562 | ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
---|
563 | ///and \c f is of \c F, |
---|
564 | ///then for |
---|
565 | ///\code |
---|
566 | /// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
---|
567 | ///\endcode |
---|
568 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
---|
569 | /// |
---|
570 | ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
---|
571 | ///The \c M2::Value and \c M1::Value must be convertible to the corresponding |
---|
572 | ///input parameter of \c F and the return type of \c F must be convertible |
---|
573 | ///to \c V. |
---|
574 | ///\todo Check the requirements. |
---|
575 | |
---|
576 | template<class M1,class M2,class F,class V = typename F::result_type> |
---|
577 | class CombineMap |
---|
578 | { |
---|
579 | typename SmartConstReference<M1>::Type m1; |
---|
580 | typename SmartConstReference<M2>::Type m2; |
---|
581 | F f; |
---|
582 | public: |
---|
583 | |
---|
584 | typedef True NeedCopy; |
---|
585 | ///\e |
---|
586 | typedef typename M1::Key Key; |
---|
587 | ///\e |
---|
588 | typedef V Value; |
---|
589 | |
---|
590 | ///Constructor |
---|
591 | CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) |
---|
592 | : m1(_m1), m2(_m2), f(_f) {}; |
---|
593 | Value operator[](Key k) const {return f(m1[k],m2[k]);} |
---|
594 | }; |
---|
595 | |
---|
596 | ///Returns a \ref CombineMap class |
---|
597 | |
---|
598 | ///This function just returns a \ref CombineMap class. |
---|
599 | /// |
---|
600 | ///Only the first template parameter (the value type) must be given. |
---|
601 | /// |
---|
602 | ///For example if \c m1 and \c m2 are both \c double valued maps, then |
---|
603 | ///\code |
---|
604 | ///combineMap<double>(m1,m2,std::plus<double>) |
---|
605 | ///\endcode |
---|
606 | ///is equivalent with |
---|
607 | ///\code |
---|
608 | ///addMap(m1,m2) |
---|
609 | ///\endcode |
---|
610 | /// |
---|
611 | ///\relates CombineMap |
---|
612 | template<class M1,class M2,class F> |
---|
613 | inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) |
---|
614 | { |
---|
615 | return CombineMap<M1,M2,F>(m1,m2,f); |
---|
616 | } |
---|
617 | |
---|
618 | ///Negative value of a map |
---|
619 | |
---|
620 | ///This \ref concept::ReadMap "read only map" returns the negative |
---|
621 | ///value of the |
---|
622 | ///value returned by the |
---|
623 | ///given map. Its \c Key and \c Value will be inherited from \c M. |
---|
624 | ///The unary \c - operator must be defined for \c Value, of course. |
---|
625 | |
---|
626 | template<class M> |
---|
627 | class NegMap |
---|
628 | { |
---|
629 | typename SmartConstReference<M>::Type m; |
---|
630 | public: |
---|
631 | |
---|
632 | typedef True NeedCopy; |
---|
633 | ///\e |
---|
634 | typedef typename M::Key Key; |
---|
635 | ///\e |
---|
636 | typedef typename M::Value Value; |
---|
637 | |
---|
638 | ///Constructor |
---|
639 | NegMap(const M &_m) : m(_m) {}; |
---|
640 | Value operator[](Key k) const {return -m[k];} |
---|
641 | }; |
---|
642 | |
---|
643 | ///Returns a \ref NegMap class |
---|
644 | |
---|
645 | ///This function just returns a \ref NegMap class. |
---|
646 | ///\relates NegMap |
---|
647 | template<class M> |
---|
648 | inline NegMap<M> negMap(const M &m) |
---|
649 | { |
---|
650 | return NegMap<M>(m); |
---|
651 | } |
---|
652 | |
---|
653 | |
---|
654 | ///Absolute value of a map |
---|
655 | |
---|
656 | ///This \ref concept::ReadMap "read only map" returns the absolute value |
---|
657 | ///of the |
---|
658 | ///value returned by the |
---|
659 | ///given map. Its \c Key and \c Value will be inherited |
---|
660 | ///from <tt>M</tt>. <tt>Value</tt> |
---|
661 | ///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
---|
662 | ///operator must be defined for it, of course. |
---|
663 | /// |
---|
664 | ///\bug We need a unified way to handle the situation below: |
---|
665 | ///\code |
---|
666 | /// struct _UnConvertible {}; |
---|
667 | /// template<class A> inline A t_abs(A a) {return _UnConvertible();} |
---|
668 | /// template<> inline int t_abs<>(int n) {return abs(n);} |
---|
669 | /// template<> inline long int t_abs<>(long int n) {return labs(n);} |
---|
670 | /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} |
---|
671 | /// template<> inline float t_abs<>(float n) {return fabsf(n);} |
---|
672 | /// template<> inline double t_abs<>(double n) {return fabs(n);} |
---|
673 | /// template<> inline long double t_abs<>(long double n) {return fabsl(n);} |
---|
674 | ///\endcode |
---|
675 | |
---|
676 | |
---|
677 | template<class M> |
---|
678 | class AbsMap |
---|
679 | { |
---|
680 | typename SmartConstReference<M>::Type m; |
---|
681 | public: |
---|
682 | |
---|
683 | typedef True NeedCopy; |
---|
684 | ///\e |
---|
685 | typedef typename M::Key Key; |
---|
686 | ///\e |
---|
687 | typedef typename M::Value Value; |
---|
688 | |
---|
689 | ///Constructor |
---|
690 | AbsMap(const M &_m) : m(_m) {}; |
---|
691 | Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;} |
---|
692 | }; |
---|
693 | |
---|
694 | ///Returns a \ref AbsMap class |
---|
695 | |
---|
696 | ///This function just returns a \ref AbsMap class. |
---|
697 | ///\relates AbsMap |
---|
698 | template<class M> |
---|
699 | inline AbsMap<M> absMap(const M &m) |
---|
700 | { |
---|
701 | return AbsMap<M>(m); |
---|
702 | } |
---|
703 | |
---|
704 | ///Converts an STL style functor to a map |
---|
705 | |
---|
706 | ///This \ref concept::ReadMap "read only map" returns the value |
---|
707 | ///of a |
---|
708 | ///given map. |
---|
709 | /// |
---|
710 | ///Template parameters \c K and \c V will become its |
---|
711 | ///\c Key and \c Value. They must be given explicitely |
---|
712 | ///because a functor does not provide such typedefs. |
---|
713 | /// |
---|
714 | ///Parameter \c F is the type of the used functor. |
---|
715 | |
---|
716 | |
---|
717 | template<class K,class V,class F> |
---|
718 | class FunctorMap |
---|
719 | { |
---|
720 | const F &f; |
---|
721 | public: |
---|
722 | |
---|
723 | typedef True NeedCopy; |
---|
724 | ///\e |
---|
725 | typedef K Key; |
---|
726 | ///\e |
---|
727 | typedef V Value; |
---|
728 | |
---|
729 | ///Constructor |
---|
730 | FunctorMap(const F &_f) : f(_f) {}; |
---|
731 | Value operator[](Key k) const {return f(k);} |
---|
732 | }; |
---|
733 | |
---|
734 | ///Returns a \ref FunctorMap class |
---|
735 | |
---|
736 | ///This function just returns a \ref FunctorMap class. |
---|
737 | /// |
---|
738 | ///The third template parameter isn't necessary to be given. |
---|
739 | ///\relates FunctorMap |
---|
740 | template<class K,class V, class F> |
---|
741 | inline FunctorMap<K,V,F> functorMap(const F &f) |
---|
742 | { |
---|
743 | return FunctorMap<K,V,F>(f); |
---|
744 | } |
---|
745 | |
---|
746 | ///Converts a map to an STL style (unary) functor |
---|
747 | |
---|
748 | ///This class Converts a map to an STL style (unary) functor. |
---|
749 | ///that is it provides an <tt>operator()</tt> to read its values. |
---|
750 | /// |
---|
751 | ///For the sake of convenience it also works as |
---|
752 | ///a ususal \ref concept::ReadMap "readable map", |
---|
753 | ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
---|
754 | |
---|
755 | template<class M> |
---|
756 | class MapFunctor |
---|
757 | { |
---|
758 | typename SmartConstReference<M>::Type m; |
---|
759 | public: |
---|
760 | |
---|
761 | typedef True NeedCopy; |
---|
762 | ///\e |
---|
763 | typedef typename M::Key argument_type; |
---|
764 | ///\e |
---|
765 | typedef typename M::Value result_type; |
---|
766 | ///\e |
---|
767 | typedef typename M::Key Key; |
---|
768 | ///\e |
---|
769 | typedef typename M::Value Value; |
---|
770 | |
---|
771 | ///Constructor |
---|
772 | MapFunctor(const M &_m) : m(_m) {}; |
---|
773 | ///Returns a value of the map |
---|
774 | Value operator()(Key k) const {return m[k];} |
---|
775 | ///\e |
---|
776 | Value operator[](Key k) const {return m[k];} |
---|
777 | }; |
---|
778 | |
---|
779 | ///Returns a \ref MapFunctor class |
---|
780 | |
---|
781 | ///This function just returns a \ref MapFunctor class. |
---|
782 | ///\relates MapFunctor |
---|
783 | template<class M> |
---|
784 | inline MapFunctor<M> mapFunctor(const M &m) |
---|
785 | { |
---|
786 | return MapFunctor<M>(m); |
---|
787 | } |
---|
788 | |
---|
789 | |
---|
790 | ///Applies all map setting operations to two maps |
---|
791 | |
---|
792 | ///This map has two \ref concept::WriteMap "writable map" |
---|
793 | ///parameters and each write request will be passed to both of them. |
---|
794 | ///If \c M1 is also \ref concept::ReadMap "readable", |
---|
795 | ///then the read operations will return the |
---|
796 | ///corresponding values of \c M1. |
---|
797 | /// |
---|
798 | ///The \c Key and \c Value will be inherited from \c M1. |
---|
799 | ///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
---|
800 | |
---|
801 | template<class M1,class M2> |
---|
802 | class ForkMap |
---|
803 | { |
---|
804 | typename SmartConstReference<M1>::Type m1; |
---|
805 | typename SmartConstReference<M2>::Type m2; |
---|
806 | public: |
---|
807 | |
---|
808 | typedef True NeedCopy; |
---|
809 | ///\e |
---|
810 | typedef typename M1::Key Key; |
---|
811 | ///\e |
---|
812 | typedef typename M1::Value Value; |
---|
813 | |
---|
814 | ///Constructor |
---|
815 | ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
816 | Value operator[](Key k) const {return m1[k];} |
---|
817 | void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);} |
---|
818 | }; |
---|
819 | |
---|
820 | ///Returns an \ref ForkMap class |
---|
821 | |
---|
822 | ///This function just returns an \ref ForkMap class. |
---|
823 | ///\todo How to call these type of functions? |
---|
824 | /// |
---|
825 | ///\relates ForkMap |
---|
826 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
827 | template<class M1,class M2> |
---|
828 | inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) |
---|
829 | { |
---|
830 | return ForkMap<M1,M2>(m1,m2); |
---|
831 | } |
---|
832 | |
---|
833 | |
---|
834 | |
---|
835 | /* ************* BOOL MAPS ******************* */ |
---|
836 | |
---|
837 | ///Logical 'not' of a map |
---|
838 | |
---|
839 | ///This bool \ref concept::ReadMap "read only map" returns the |
---|
840 | ///logical negation of |
---|
841 | ///value returned by the |
---|
842 | ///given map. Its \c Key and will be inherited from \c M, |
---|
843 | ///its Value is <tt>bool</tt>. |
---|
844 | |
---|
845 | template<class M> |
---|
846 | class NotMap |
---|
847 | { |
---|
848 | typename SmartConstReference<M>::Type m; |
---|
849 | public: |
---|
850 | |
---|
851 | typedef True NeedCopy; |
---|
852 | ///\e |
---|
853 | typedef typename M::Key Key; |
---|
854 | ///\e |
---|
855 | typedef bool Value; |
---|
856 | |
---|
857 | ///Constructor |
---|
858 | NotMap(const M &_m) : m(_m) {}; |
---|
859 | Value operator[](Key k) const {return !m[k];} |
---|
860 | }; |
---|
861 | |
---|
862 | ///Returns a \ref NotMap class |
---|
863 | |
---|
864 | ///This function just returns a \ref NotMap class. |
---|
865 | ///\relates NotMap |
---|
866 | template<class M> |
---|
867 | inline NotMap<M> notMap(const M &m) |
---|
868 | { |
---|
869 | return NotMap<M>(m); |
---|
870 | } |
---|
871 | |
---|
872 | |
---|
873 | |
---|
874 | |
---|
875 | |
---|
876 | |
---|
877 | |
---|
878 | |
---|
879 | |
---|
880 | |
---|
881 | |
---|
882 | /// @} |
---|
883 | } |
---|
884 | |
---|
885 | #endif // LEMON_MAPS_H |
---|