Little beauty fault is corrected.
2 * lemon/maps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
20 #include <lemon/graph_utils.h>
21 #include <lemon/utility.h>
26 ///\brief Miscellaneous property maps
28 ///\todo This file has the same name as the concept file in concept/,
29 /// and this is not easily detectable in docs...
38 /// Base class of maps.
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>
52 /// Null map. (a.k.a. DoNothingMap)
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>
62 typedef True NeedCopy;
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&) {}
70 template <typename K, typename V>
71 NullMap<K, V> nullMap() {
72 return NullMap<K, V>();
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>
87 typedef True NeedCopy;
89 /// Default constructor
91 /// The value of the map will be uninitialized.
92 /// (More exactly it will be default constructed.)
96 /// \param _v The initial value of the map.
98 ConstMap(const T &_v) : v(_v) {}
100 T operator[](const K&) const { return v; }
101 void set(const K&, const T&) {}
103 template<typename T1>
105 typedef ConstMap<K,T1> other;
108 template<typename T1>
109 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
112 ///Returns a \ref ConstMap class
114 ///This function just returns a \ref ConstMap class.
116 template<class V,class K>
117 inline ConstMap<V,K> constMap(const K &k)
119 return ConstMap<V,K>(k);
124 template<typename T, T v>
127 template<typename K, typename V, V v>
128 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
132 V operator[](const K&) const { return v; }
133 void set(const K&, const V&) { }
136 /// \c std::map wrapper
138 /// This is essentially a wrapper for \c std::map. With addition that
139 /// you can specify a default value different from \c Value() .
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;
146 typedef typename parent::value_type PairType;
154 typedef T& Reference;
156 typedef const T& ConstReference;
160 /// Constructor with specified default value
161 StdMap(const T& _v) : v(_v) {}
163 /// \brief Constructs the map from an appropriate std::map.
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.
170 /// \warning Inefficient: copies the content of \c m !
171 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
173 template<typename T1, typename Comp1>
174 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
178 Reference operator[](const Key &k) {
179 return insert(PairType(k,v)).first -> second;
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))
187 void set(const Key &k, const T &t) {
188 parent::operator[](k) = t;
191 /// Changes the default value of the map.
192 /// \return Returns the previous default value.
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; }
198 template<typename T1>
200 typedef StdMap<Key,T1,Compare> other;
206 /// \addtogroup map_adaptors
210 ///Convert the \c Value of a maps to another type.
212 ///This \ref concept::ReadMap "read only map"
213 ///converts the \c Value of a maps to type \c T.
214 ///Its \c Value is inherited from \c M.
218 /// ConvertMap<X> sh(x,v);
220 ///it is equivalent with
222 /// ConstMap<X::Key, X::Value> c_tmp(v);
223 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
225 ///\bug wrong documentation
226 template<class M, class T>
228 typename SmartConstReference<M>::Type m;
231 typedef True NeedCopy;
234 typedef typename M::Key Key;
241 ///\param _m is the undelying map
242 ///\param _v is the convert value
243 ConvertMap(const M &_m) : m(_m) {};
245 /// \brief The subscript operator.
247 /// The subscript operator.
248 /// \param edge The edge
249 /// \return The target of the edge
250 Value operator[](Key k) const {return m[k];}
253 ///Returns an \ref ConvertMap class
255 ///This function just returns an \ref ConvertMap class.
256 ///\relates ConvertMap
257 ///\todo The order of the template parameters are changed.
258 template<class T, class M>
259 inline ConvertMap<M,T> convertMap(const M &m)
261 return ConvertMap<M,T>(m);
266 ///This \ref concept::ReadMap "read only map" returns the sum of the two
267 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
268 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
270 template<class M1,class M2>
273 typename SmartConstReference<M1>::Type m1;
274 typename SmartConstReference<M2>::Type m2;
278 typedef True NeedCopy;
281 typedef typename M1::Key Key;
283 typedef typename M1::Value Value;
289 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
290 Value operator[](Key k) const {return m1[k]+m2[k];}
293 ///Returns an \ref AddMap class
295 ///This function just returns an \ref AddMap class.
296 ///\todo How to call these type of functions?
299 ///\todo Wrong scope in Doxygen when \c \\relates is used
300 template<class M1,class M2>
301 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
303 return AddMap<M1,M2>(m1,m2);
306 ///Shift a maps with a constant.
308 ///This \ref concept::ReadMap "read only map" returns the sum of the
309 ///given map and a constant value.
310 ///Its \c Key and \c Value is inherited from \c M.
314 /// ShiftMap<X> sh(x,v);
316 ///it is equivalent with
318 /// ConstMap<X::Key, X::Value> c_tmp(v);
319 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
324 typename SmartConstReference<M>::Type m;
328 typedef True NeedCopy;
330 typedef typename M::Key Key;
332 typedef typename M::Value Value;
337 ///\param _m is the undelying map
338 ///\param _v is the shift value
339 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
340 Value operator[](Key k) const {return m[k]+v;}
343 ///Returns an \ref ShiftMap class
345 ///This function just returns an \ref ShiftMap class.
347 ///\todo A better name is required.
349 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
351 return ShiftMap<M>(m,v);
354 ///Difference of two maps
356 ///This \ref concept::ReadMap "read only map" returns the difference
357 ///of the values returned by the two
358 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
359 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
361 template<class M1,class M2>
364 typename SmartConstReference<M1>::Type m1;
365 typename SmartConstReference<M2>::Type m2;
368 typedef True NeedCopy;
370 typedef typename M1::Key Key;
372 typedef typename M1::Value Value;
378 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
379 Value operator[](Key k) const {return m1[k]-m2[k];}
382 ///Returns a \ref SubMap class
384 ///This function just returns a \ref SubMap class.
387 template<class M1,class M2>
388 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
390 return SubMap<M1,M2>(m1,m2);
393 ///Product of two maps
395 ///This \ref concept::ReadMap "read only map" returns the product of the
396 ///values returned by the two
398 ///maps. Its \c Key and \c Value will be inherited from \c M1.
399 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
401 template<class M1,class M2>
404 typename SmartConstReference<M1>::Type m1;
405 typename SmartConstReference<M2>::Type m2;
408 typedef True NeedCopy;
410 typedef typename M1::Key Key;
412 typedef typename M1::Value Value;
418 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
419 Value operator[](Key k) const {return m1[k]*m2[k];}
422 ///Returns a \ref MulMap class
424 ///This function just returns a \ref MulMap class.
426 template<class M1,class M2>
427 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
429 return MulMap<M1,M2>(m1,m2);
432 ///Scale a maps with a constant.
434 ///This \ref concept::ReadMap "read only map" returns the value of the
435 ///given map multipied with a constant value.
436 ///Its \c Key and \c Value is inherited from \c M.
440 /// ScaleMap<X> sc(x,v);
442 ///it is equivalent with
444 /// ConstMap<X::Key, X::Value> c_tmp(v);
445 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
450 typename SmartConstReference<M>::Type m;
454 typedef True NeedCopy;
456 typedef typename M::Key Key;
458 typedef typename M::Value Value;
463 ///\param _m is the undelying map
464 ///\param _v is the scaling value
465 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
466 Value operator[](Key k) const {return m[k]*v;}
469 ///Returns an \ref ScaleMap class
471 ///This function just returns an \ref ScaleMap class.
473 ///\todo A better name is required.
475 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
477 return ScaleMap<M>(m,v);
480 ///Quotient of two maps
482 ///This \ref concept::ReadMap "read only map" returns the quotient of the
483 ///values returned by the two
484 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
485 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
487 template<class M1,class M2>
490 typename SmartConstReference<M1>::Type m1;
491 typename SmartConstReference<M2>::Type m2;
494 typedef True NeedCopy;
496 typedef typename M1::Key Key;
498 typedef typename M1::Value Value;
504 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
505 Value operator[](Key k) const {return m1[k]/m2[k];}
508 ///Returns a \ref DivMap class
510 ///This function just returns a \ref DivMap class.
512 template<class M1,class M2>
513 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
515 return DivMap<M1,M2>(m1,m2);
518 ///Composition of two maps
520 ///This \ref concept::ReadMap "read only map" returns the composition of
522 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
526 /// ComposeMap<M1,M2> cm(m1,m2);
528 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
530 ///Its \c Key is inherited from \c M2 and its \c Value is from
532 ///The \c M2::Value must be convertible to \c M1::Key.
533 ///\todo Check the requirements.
535 template<class M1,class M2>
538 typename SmartConstReference<M1>::Type m1;
539 typename SmartConstReference<M2>::Type m2;
542 typedef True NeedCopy;
544 typedef typename M2::Key Key;
546 typedef typename M1::Value Value;
552 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
553 Value operator[](Key k) const {return m1[m2[k]];}
555 ///Returns a \ref ComposeMap class
557 ///This function just returns a \ref ComposeMap class.
559 ///\relates ComposeMap
560 template<class M1,class M2>
561 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
563 return ComposeMap<M1,M2>(m1,m2);
566 ///Combine of two maps using an STL (binary) functor.
568 ///Combine of two maps using an STL (binary) functor.
571 ///This \ref concept::ReadMap "read only map" takes to maps and a
572 ///binary functor and returns the composition of
574 ///given maps unsing the functor.
575 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
576 ///and \c f is of \c F,
579 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
581 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
583 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
584 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
585 ///input parameter of \c F and the return type of \c F must be convertible
587 ///\todo Check the requirements.
589 template<class M1,class M2,class F,class V = typename F::result_type>
592 typename SmartConstReference<M1>::Type m1;
593 typename SmartConstReference<M2>::Type m2;
597 typedef True NeedCopy;
599 typedef typename M1::Key Key;
607 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
608 : m1(_m1), m2(_m2), f(_f) {};
609 Value operator[](Key k) const {return f(m1[k],m2[k]);}
612 ///Returns a \ref CombineMap class
614 ///This function just returns a \ref CombineMap class.
616 ///Only the first template parameter (the value type) must be given.
618 ///For example if \c m1 and \c m2 are both \c double valued maps, then
620 ///combineMap<double>(m1,m2,std::plus<double>)
622 ///is equivalent with
627 ///\relates CombineMap
628 template<class M1,class M2,class F>
629 inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
631 return CombineMap<M1,M2,F>(m1,m2,f);
634 ///Negative value of a map
636 ///This \ref concept::ReadMap "read only map" returns the negative
638 ///value returned by the
639 ///given map. Its \c Key and \c Value will be inherited from \c M.
640 ///The unary \c - operator must be defined for \c Value, of course.
645 typename SmartConstReference<M>::Type m;
648 typedef True NeedCopy;
650 typedef typename M::Key Key;
652 typedef typename M::Value Value;
658 NegMap(const M &_m) : m(_m) {};
659 Value operator[](Key k) const {return -m[k];}
662 ///Returns a \ref NegMap class
664 ///This function just returns a \ref NegMap class.
667 inline NegMap<M> negMap(const M &m)
673 ///Absolute value of a map
675 ///This \ref concept::ReadMap "read only map" returns the absolute value
677 ///value returned by the
678 ///given map. Its \c Key and \c Value will be inherited
679 ///from <tt>M</tt>. <tt>Value</tt>
680 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
681 ///operator must be defined for it, of course.
683 ///\bug We need a unified way to handle the situation below:
685 /// struct _UnConvertible {};
686 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
687 /// template<> inline int t_abs<>(int n) {return abs(n);}
688 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
689 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
690 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
691 /// template<> inline double t_abs<>(double n) {return fabs(n);}
692 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
699 typename SmartConstReference<M>::Type m;
702 typedef True NeedCopy;
704 typedef typename M::Key Key;
706 typedef typename M::Value Value;
712 AbsMap(const M &_m) : m(_m) {};
713 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
716 ///Returns a \ref AbsMap class
718 ///This function just returns a \ref AbsMap class.
721 inline AbsMap<M> absMap(const M &m)
726 ///Converts an STL style functor to a map
728 ///This \ref concept::ReadMap "read only map" returns the value
732 ///Template parameters \c K and \c V will become its
733 ///\c Key and \c Value. They must be given explicitely
734 ///because a functor does not provide such typedefs.
736 ///Parameter \c F is the type of the used functor.
739 template<class K,class V,class F>
745 typedef True NeedCopy;
755 FunctorMap(const F &_f) : f(_f) {};
756 Value operator[](Key k) const {return f(k);}
759 ///Returns a \ref FunctorMap class
761 ///This function just returns a \ref FunctorMap class.
763 ///The third template parameter isn't necessary to be given.
764 ///\relates FunctorMap
765 template<class K,class V, class F>
766 inline FunctorMap<K,V,F> functorMap(const F &f)
768 return FunctorMap<K,V,F>(f);
771 ///Converts a map to an STL style (unary) functor
773 ///This class Converts a map to an STL style (unary) functor.
774 ///that is it provides an <tt>operator()</tt> to read its values.
776 ///For the sake of convenience it also works as
777 ///a ususal \ref concept::ReadMap "readable map", i.e
778 ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
783 typename SmartConstReference<M>::Type m;
786 typedef True NeedCopy;
788 typedef typename M::Key argument_type;
790 typedef typename M::Value result_type;
792 typedef typename M::Key Key;
794 typedef typename M::Value Value;
800 MapFunctor(const M &_m) : m(_m) {};
801 ///Returns a value of the map
805 Value operator()(Key k) const {return m[k];}
808 Value operator[](Key k) const {return m[k];}
811 ///Returns a \ref MapFunctor class
813 ///This function just returns a \ref MapFunctor class.
814 ///\relates MapFunctor
816 inline MapFunctor<M> mapFunctor(const M &m)
818 return MapFunctor<M>(m);
822 ///Apply all map setting operations to two maps
824 ///This map has two \ref concept::WriteMap "writable map"
825 ///parameters and each write request will be passed to both of them.
826 ///If \c M1 is also \ref concept::ReadMap "readable",
827 ///then the read operations will return the
828 ///corresponding values of \c M1.
830 ///The \c Key and \c Value will be inherited from \c M1.
831 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
833 template<class M1,class M2>
836 typename SmartConstReference<M1>::Type m1;
837 typename SmartConstReference<M2>::Type m2;
840 typedef True NeedCopy;
842 typedef typename M1::Key Key;
844 typedef typename M1::Value Value;
850 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
851 Value operator[](Key k) const {return m1[k];}
852 void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
855 ///Returns an \ref ForkMap class
857 ///This function just returns an \ref ForkMap class.
858 ///\todo How to call these type of functions?
861 ///\todo Wrong scope in Doxygen when \c \\relates is used
862 template<class M1,class M2>
863 inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
865 return ForkMap<M1,M2>(m1,m2);
870 /* ************* BOOL MAPS ******************* */
872 ///Logical 'not' of a map
874 ///This bool \ref concept::ReadMap "read only map" returns the
875 ///logical negation of
876 ///value returned by the
877 ///given map. Its \c Key and will be inherited from \c M,
878 ///its Value is <tt>bool</tt>.
883 typename SmartConstReference<M>::Type m;
886 typedef True NeedCopy;
888 typedef typename M::Key Key;
896 NotMap(const M &_m) : m(_m) {};
897 Value operator[](Key k) const {return !m[k];}
900 ///Returns a \ref NotMap class
902 ///This function just returns a \ref NotMap class.
905 inline NotMap<M> notMap(const M &m)
923 #endif // LEMON_MAPS_H