Doc.
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
209 /// \brief Identity mapping.
211 /// This mapping gives back the given key as value without any
213 template <typename T>
219 const Value& operator[](const Key& t) const {
224 ///Convert the \c Value of a maps to another type.
226 ///This \ref concept::ReadMap "read only map"
227 ///converts the \c Value of a maps to type \c T.
228 ///Its \c Value is inherited from \c M.
232 /// ConvertMap<X> sh(x,v);
234 ///it is equivalent with
236 /// ConstMap<X::Key, X::Value> c_tmp(v);
237 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
239 ///\bug wrong documentation
240 template<class M, class T>
242 typename SmartConstReference<M>::Type m;
245 typedef True NeedCopy;
248 typedef typename M::Key Key;
255 ///\param _m is the undelying map
256 ///\param _v is the convert value
257 ConvertMap(const M &_m) : m(_m) {};
259 /// \brief The subscript operator.
261 /// The subscript operator.
262 /// \param edge The edge
263 /// \return The target of the edge
264 Value operator[](Key k) const {return m[k];}
267 ///Returns an \ref ConvertMap class
269 ///This function just returns an \ref ConvertMap class.
270 ///\relates ConvertMap
271 ///\todo The order of the template parameters are changed.
272 template<class T, class M>
273 inline ConvertMap<M,T> convertMap(const M &m)
275 return ConvertMap<M,T>(m);
280 ///This \ref concept::ReadMap "read only map" returns the sum of the two
281 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
282 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
284 template<class M1,class M2>
287 typename SmartConstReference<M1>::Type m1;
288 typename SmartConstReference<M2>::Type m2;
292 typedef True NeedCopy;
295 typedef typename M1::Key Key;
297 typedef typename M1::Value Value;
303 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
304 Value operator[](Key k) const {return m1[k]+m2[k];}
307 ///Returns an \ref AddMap class
309 ///This function just returns an \ref AddMap class.
310 ///\todo How to call these type of functions?
313 ///\todo Wrong scope in Doxygen when \c \\relates is used
314 template<class M1,class M2>
315 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
317 return AddMap<M1,M2>(m1,m2);
320 ///Shift a maps with a constant.
322 ///This \ref concept::ReadMap "read only map" returns the sum of the
323 ///given map and a constant value.
324 ///Its \c Key and \c Value is inherited from \c M.
328 /// ShiftMap<X> sh(x,v);
330 ///it is equivalent with
332 /// ConstMap<X::Key, X::Value> c_tmp(v);
333 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
338 typename SmartConstReference<M>::Type m;
342 typedef True NeedCopy;
344 typedef typename M::Key Key;
346 typedef typename M::Value Value;
351 ///\param _m is the undelying map
352 ///\param _v is the shift value
353 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
354 Value operator[](Key k) const {return m[k]+v;}
357 ///Returns an \ref ShiftMap class
359 ///This function just returns an \ref ShiftMap class.
361 ///\todo A better name is required.
363 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
365 return ShiftMap<M>(m,v);
368 ///Difference of two maps
370 ///This \ref concept::ReadMap "read only map" returns the difference
371 ///of the values returned by the two
372 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
373 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
375 template<class M1,class M2>
378 typename SmartConstReference<M1>::Type m1;
379 typename SmartConstReference<M2>::Type m2;
382 typedef True NeedCopy;
384 typedef typename M1::Key Key;
386 typedef typename M1::Value Value;
392 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
393 Value operator[](Key k) const {return m1[k]-m2[k];}
396 ///Returns a \ref SubMap class
398 ///This function just returns a \ref SubMap class.
401 template<class M1,class M2>
402 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
404 return SubMap<M1,M2>(m1,m2);
407 ///Product of two maps
409 ///This \ref concept::ReadMap "read only map" returns the product of the
410 ///values returned by the two
412 ///maps. Its \c Key and \c Value will be inherited from \c M1.
413 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
415 template<class M1,class M2>
418 typename SmartConstReference<M1>::Type m1;
419 typename SmartConstReference<M2>::Type m2;
422 typedef True NeedCopy;
424 typedef typename M1::Key Key;
426 typedef typename M1::Value Value;
432 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
433 Value operator[](Key k) const {return m1[k]*m2[k];}
436 ///Returns a \ref MulMap class
438 ///This function just returns a \ref MulMap class.
440 template<class M1,class M2>
441 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
443 return MulMap<M1,M2>(m1,m2);
446 ///Scale a maps with a constant.
448 ///This \ref concept::ReadMap "read only map" returns the value of the
449 ///given map multipied with a constant value.
450 ///Its \c Key and \c Value is inherited from \c M.
454 /// ScaleMap<X> sc(x,v);
456 ///it is equivalent with
458 /// ConstMap<X::Key, X::Value> c_tmp(v);
459 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
464 typename SmartConstReference<M>::Type m;
468 typedef True NeedCopy;
470 typedef typename M::Key Key;
472 typedef typename M::Value Value;
477 ///\param _m is the undelying map
478 ///\param _v is the scaling value
479 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
480 Value operator[](Key k) const {return m[k]*v;}
483 ///Returns an \ref ScaleMap class
485 ///This function just returns an \ref ScaleMap class.
487 ///\todo A better name is required.
489 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
491 return ScaleMap<M>(m,v);
494 ///Quotient of two maps
496 ///This \ref concept::ReadMap "read only map" returns the quotient of the
497 ///values returned by the two
498 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
499 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
501 template<class M1,class M2>
504 typename SmartConstReference<M1>::Type m1;
505 typename SmartConstReference<M2>::Type m2;
508 typedef True NeedCopy;
510 typedef typename M1::Key Key;
512 typedef typename M1::Value Value;
518 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
519 Value operator[](Key k) const {return m1[k]/m2[k];}
522 ///Returns a \ref DivMap class
524 ///This function just returns a \ref DivMap class.
526 template<class M1,class M2>
527 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
529 return DivMap<M1,M2>(m1,m2);
532 ///Composition of two maps
534 ///This \ref concept::ReadMap "read only map" returns the composition of
536 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
540 /// ComposeMap<M1,M2> cm(m1,m2);
542 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
544 ///Its \c Key is inherited from \c M2 and its \c Value is from
546 ///The \c M2::Value must be convertible to \c M1::Key.
547 ///\todo Check the requirements.
549 template<class M1,class M2>
552 typename SmartConstReference<M1>::Type m1;
553 typename SmartConstReference<M2>::Type m2;
556 typedef True NeedCopy;
558 typedef typename M2::Key Key;
560 typedef typename M1::Value Value;
566 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
567 Value operator[](Key k) const {return m1[m2[k]];}
569 ///Returns a \ref ComposeMap class
571 ///This function just returns a \ref ComposeMap class.
573 ///\relates ComposeMap
574 template<class M1,class M2>
575 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
577 return ComposeMap<M1,M2>(m1,m2);
580 ///Combine of two maps using an STL (binary) functor.
582 ///Combine of two maps using an STL (binary) functor.
585 ///This \ref concept::ReadMap "read only map" takes to maps and a
586 ///binary functor and returns the composition of
588 ///given maps unsing the functor.
589 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
590 ///and \c f is of \c F,
593 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
595 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
597 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
598 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
599 ///input parameter of \c F and the return type of \c F must be convertible
601 ///\todo Check the requirements.
603 template<class M1,class M2,class F,class V = typename F::result_type>
606 typename SmartConstReference<M1>::Type m1;
607 typename SmartConstReference<M2>::Type m2;
611 typedef True NeedCopy;
613 typedef typename M1::Key Key;
621 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
622 : m1(_m1), m2(_m2), f(_f) {};
623 Value operator[](Key k) const {return f(m1[k],m2[k]);}
626 ///Returns a \ref CombineMap class
628 ///This function just returns a \ref CombineMap class.
630 ///Only the first template parameter (the value type) must be given.
632 ///For example if \c m1 and \c m2 are both \c double valued maps, then
634 ///combineMap<double>(m1,m2,std::plus<double>)
636 ///is equivalent with
641 ///\relates CombineMap
642 template<class M1,class M2,class F>
643 inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
645 return CombineMap<M1,M2,F>(m1,m2,f);
648 ///Negative value of a map
650 ///This \ref concept::ReadMap "read only map" returns the negative
652 ///value returned by the
653 ///given map. Its \c Key and \c Value will be inherited from \c M.
654 ///The unary \c - operator must be defined for \c Value, of course.
659 typename SmartConstReference<M>::Type m;
662 typedef True NeedCopy;
664 typedef typename M::Key Key;
666 typedef typename M::Value Value;
672 NegMap(const M &_m) : m(_m) {};
673 Value operator[](Key k) const {return -m[k];}
676 ///Returns a \ref NegMap class
678 ///This function just returns a \ref NegMap class.
681 inline NegMap<M> negMap(const M &m)
687 ///Absolute value of a map
689 ///This \ref concept::ReadMap "read only map" returns the absolute value
691 ///value returned by the
692 ///given map. Its \c Key and \c Value will be inherited
693 ///from <tt>M</tt>. <tt>Value</tt>
694 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
695 ///operator must be defined for it, of course.
697 ///\bug We need a unified way to handle the situation below:
699 /// struct _UnConvertible {};
700 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
701 /// template<> inline int t_abs<>(int n) {return abs(n);}
702 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
703 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
704 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
705 /// template<> inline double t_abs<>(double n) {return fabs(n);}
706 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
713 typename SmartConstReference<M>::Type m;
716 typedef True NeedCopy;
718 typedef typename M::Key Key;
720 typedef typename M::Value Value;
726 AbsMap(const M &_m) : m(_m) {};
727 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
730 ///Returns a \ref AbsMap class
732 ///This function just returns a \ref AbsMap class.
735 inline AbsMap<M> absMap(const M &m)
740 ///Converts an STL style functor to a map
742 ///This \ref concept::ReadMap "read only map" returns the value
746 ///Template parameters \c K and \c V will become its
747 ///\c Key and \c Value. They must be given explicitely
748 ///because a functor does not provide such typedefs.
750 ///Parameter \c F is the type of the used functor.
753 template<class K,class V,class F>
759 typedef True NeedCopy;
769 FunctorMap(const F &_f) : f(_f) {};
770 Value operator[](Key k) const {return f(k);}
773 ///Returns a \ref FunctorMap class
775 ///This function just returns a \ref FunctorMap class.
777 ///The third template parameter isn't necessary to be given.
778 ///\relates FunctorMap
779 template<class K,class V, class F>
780 inline FunctorMap<K,V,F> functorMap(const F &f)
782 return FunctorMap<K,V,F>(f);
785 ///Converts a map to an STL style (unary) functor
787 ///This class Converts a map to an STL style (unary) functor.
788 ///that is it provides an <tt>operator()</tt> to read its values.
790 ///For the sake of convenience it also works as
791 ///a ususal \ref concept::ReadMap "readable map", i.e
792 ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
797 typename SmartConstReference<M>::Type m;
800 typedef True NeedCopy;
802 typedef typename M::Key argument_type;
804 typedef typename M::Value result_type;
806 typedef typename M::Key Key;
808 typedef typename M::Value Value;
814 MapFunctor(const M &_m) : m(_m) {};
815 ///Returns a value of the map
819 Value operator()(Key k) const {return m[k];}
822 Value operator[](Key k) const {return m[k];}
825 ///Returns a \ref MapFunctor class
827 ///This function just returns a \ref MapFunctor class.
828 ///\relates MapFunctor
830 inline MapFunctor<M> mapFunctor(const M &m)
832 return MapFunctor<M>(m);
836 ///Apply all map setting operations to two maps
838 ///This map has two \ref concept::WriteMap "writable map"
839 ///parameters and each write request will be passed to both of them.
840 ///If \c M1 is also \ref concept::ReadMap "readable",
841 ///then the read operations will return the
842 ///corresponding values of \c M1.
844 ///The \c Key and \c Value will be inherited from \c M1.
845 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
847 template<class M1,class M2>
850 typename SmartConstReference<M1>::Type m1;
851 typename SmartConstReference<M2>::Type m2;
854 typedef True NeedCopy;
856 typedef typename M1::Key Key;
858 typedef typename M1::Value Value;
864 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
865 Value operator[](Key k) const {return m1[k];}
866 void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
869 ///Returns an \ref ForkMap class
871 ///This function just returns an \ref ForkMap class.
872 ///\todo How to call these type of functions?
875 ///\todo Wrong scope in Doxygen when \c \\relates is used
876 template<class M1,class M2>
877 inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
879 return ForkMap<M1,M2>(m1,m2);
884 /* ************* BOOL MAPS ******************* */
886 ///Logical 'not' of a map
888 ///This bool \ref concept::ReadMap "read only map" returns the
889 ///logical negation of
890 ///value returned by the
891 ///given map. Its \c Key and will be inherited from \c M,
892 ///its Value is <tt>bool</tt>.
897 typename SmartConstReference<M>::Type m;
900 typedef True NeedCopy;
902 typedef typename M::Key Key;
910 NotMap(const M &_m) : m(_m) {};
911 Value operator[](Key k) const {return !m[k];}
914 ///Returns a \ref NotMap class
916 ///This function just returns a \ref NotMap class.
919 inline NotMap<M> notMap(const M &m)
937 #endif // LEMON_MAPS_H