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 K,class V>
117 inline ConstMap<K,V> constMap(const V &v)
119 return ConstMap<K,V>(v);
123 //\todo to document later
124 template<typename T, T v>
126 //\todo to document later
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 map 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 Key is inherited from \c M.
229 template<class M, class T>
231 typename SmartConstReference<M>::Type m;
234 typedef True NeedCopy;
237 typedef typename M::Key Key;
244 ///\param _m is the underlying map
245 ConvertMap(const M &_m) : m(_m) {};
247 /// \brief The subscript operator.
249 /// The subscript operator.
251 /// \return The target of the edge
252 Value operator[](Key k) const {return m[k];}
255 ///Returns an \ref ConvertMap class
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)
263 return ConvertMap<M,T>(m);
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.
272 template<class M1,class M2>
275 typename SmartConstReference<M1>::Type m1;
276 typename SmartConstReference<M2>::Type m2;
280 typedef True NeedCopy;
283 typedef typename M1::Key Key;
285 typedef typename M1::Value Value;
288 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
289 Value operator[](Key k) const {return m1[k]+m2[k];}
292 ///Returns an \ref AddMap class
294 ///This function just returns an \ref AddMap class.
295 ///\todo How to call these type of functions?
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)
302 return AddMap<M1,M2>(m1,m2);
305 ///Shift a map with a constant.
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.
313 /// ShiftMap<X> sh(x,v);
315 ///is equivalent with
317 /// ConstMap<X::Key, X::Value> c_tmp(v);
318 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
323 typename SmartConstReference<M>::Type m;
327 typedef True NeedCopy;
329 typedef typename M::Key Key;
331 typedef typename M::Value Value;
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;}
342 ///Returns an \ref ShiftMap class
344 ///This function just returns an \ref ShiftMap class.
346 ///\todo A better name is required.
348 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
350 return ShiftMap<M>(m,v);
353 ///Difference of two maps
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.
360 template<class M1,class M2>
363 typename SmartConstReference<M1>::Type m1;
364 typename SmartConstReference<M2>::Type m2;
367 typedef True NeedCopy;
369 typedef typename M1::Key Key;
371 typedef typename M1::Value Value;
374 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
375 Value operator[](Key k) const {return m1[k]-m2[k];}
378 ///Returns a \ref SubMap class
380 ///This function just returns a \ref SubMap class.
383 template<class M1,class M2>
384 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
386 return SubMap<M1,M2>(m1,m2);
389 ///Product of two maps
391 ///This \ref concept::ReadMap "read only map" returns the product of the
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.
397 template<class M1,class M2>
400 typename SmartConstReference<M1>::Type m1;
401 typename SmartConstReference<M2>::Type m2;
404 typedef True NeedCopy;
406 typedef typename M1::Key Key;
408 typedef typename M1::Value Value;
411 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
412 Value operator[](Key k) const {return m1[k]*m2[k];}
415 ///Returns a \ref MulMap class
417 ///This function just returns a \ref MulMap class.
419 template<class M1,class M2>
420 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
422 return MulMap<M1,M2>(m1,m2);
425 ///Scales a maps with a constant.
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.
433 /// ScaleMap<X> sc(x,v);
435 ///is equivalent with
437 /// ConstMap<X::Key, X::Value> c_tmp(v);
438 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
443 typename SmartConstReference<M>::Type m;
447 typedef True NeedCopy;
449 typedef typename M::Key Key;
451 typedef typename M::Value Value;
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;}
462 ///Returns an \ref ScaleMap class
464 ///This function just returns an \ref ScaleMap class.
466 ///\todo A better name is required.
468 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
470 return ScaleMap<M>(m,v);
473 ///Quotient of two maps
475 ///This \ref concept::ReadMap "read only map" returns the quotient of the
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.
480 template<class M1,class M2>
483 typename SmartConstReference<M1>::Type m1;
484 typename SmartConstReference<M2>::Type m2;
487 typedef True NeedCopy;
489 typedef typename M1::Key Key;
491 typedef typename M1::Value Value;
494 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
495 Value operator[](Key k) const {return m1[k]/m2[k];}
498 ///Returns a \ref DivMap class
500 ///This function just returns a \ref DivMap class.
502 template<class M1,class M2>
503 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
505 return DivMap<M1,M2>(m1,m2);
508 ///Composition of two maps
510 ///This \ref concept::ReadMap "read only map" returns the composition of
512 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
516 /// ComposeMap<M1,M2> cm(m1,m2);
518 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
520 ///Its \c Key is inherited from \c M2 and its \c Value is from
522 ///The \c M2::Value must be convertible to \c M1::Key.
523 ///\todo Check the requirements.
525 template<class M1,class M2>
528 typename SmartConstReference<M1>::Type m1;
529 typename SmartConstReference<M2>::Type m2;
532 typedef True NeedCopy;
534 typedef typename M2::Key Key;
536 typedef typename M1::Value Value;
539 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
540 Value operator[](Key k) const {return m1[m2[k]];}
542 ///Returns a \ref ComposeMap class
544 ///This function just returns a \ref ComposeMap class.
546 ///\relates ComposeMap
547 template<class M1,class M2>
548 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
550 return ComposeMap<M1,M2>(m1,m2);
553 ///Combines of two maps using an STL (binary) functor.
555 ///Combines of two maps using an STL (binary) functor.
558 ///This \ref concept::ReadMap "read only map" takes two maps and a
559 ///binary functor and returns the composition of
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,
566 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
568 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
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
574 ///\todo Check the requirements.
576 template<class M1,class M2,class F,class V = typename F::result_type>
579 typename SmartConstReference<M1>::Type m1;
580 typename SmartConstReference<M2>::Type m2;
584 typedef True NeedCopy;
586 typedef typename M1::Key Key;
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]);}
596 ///Returns a \ref CombineMap class
598 ///This function just returns a \ref CombineMap class.
600 ///Only the first template parameter (the value type) must be given.
602 ///For example if \c m1 and \c m2 are both \c double valued maps, then
604 ///combineMap<double>(m1,m2,std::plus<double>)
606 ///is equivalent with
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)
615 return CombineMap<M1,M2,F>(m1,m2,f);
618 ///Negative value of a map
620 ///This \ref concept::ReadMap "read only map" returns the negative
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.
629 typename SmartConstReference<M>::Type m;
632 typedef True NeedCopy;
634 typedef typename M::Key Key;
636 typedef typename M::Value Value;
639 NegMap(const M &_m) : m(_m) {};
640 Value operator[](Key k) const {return -m[k];}
643 ///Returns a \ref NegMap class
645 ///This function just returns a \ref NegMap class.
648 inline NegMap<M> negMap(const M &m)
654 ///Absolute value of a map
656 ///This \ref concept::ReadMap "read only map" returns the absolute value
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.
664 ///\bug We need a unified way to handle the situation below:
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);}
680 typename SmartConstReference<M>::Type m;
683 typedef True NeedCopy;
685 typedef typename M::Key Key;
687 typedef typename M::Value Value;
690 AbsMap(const M &_m) : m(_m) {};
691 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
694 ///Returns a \ref AbsMap class
696 ///This function just returns a \ref AbsMap class.
699 inline AbsMap<M> absMap(const M &m)
704 ///Converts an STL style functor to a map
706 ///This \ref concept::ReadMap "read only map" returns the value
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.
714 ///Parameter \c F is the type of the used functor.
717 template<class K,class V,class F>
723 typedef True NeedCopy;
730 FunctorMap(const F &_f) : f(_f) {};
731 Value operator[](Key k) const {return f(k);}
734 ///Returns a \ref FunctorMap class
736 ///This function just returns a \ref FunctorMap class.
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)
743 return FunctorMap<K,V,F>(f);
746 ///Converts a map to an STL style (unary) functor
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.
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.
758 typename SmartConstReference<M>::Type m;
761 typedef True NeedCopy;
763 typedef typename M::Key argument_type;
765 typedef typename M::Value result_type;
767 typedef typename M::Key Key;
769 typedef typename M::Value Value;
772 MapFunctor(const M &_m) : m(_m) {};
773 ///Returns a value of the map
774 Value operator()(Key k) const {return m[k];}
776 Value operator[](Key k) const {return m[k];}
779 ///Returns a \ref MapFunctor class
781 ///This function just returns a \ref MapFunctor class.
782 ///\relates MapFunctor
784 inline MapFunctor<M> mapFunctor(const M &m)
786 return MapFunctor<M>(m);
790 ///Applies all map setting operations to two maps
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.
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.
801 template<class M1,class M2>
804 typename SmartConstReference<M1>::Type m1;
805 typename SmartConstReference<M2>::Type m2;
808 typedef True NeedCopy;
810 typedef typename M1::Key Key;
812 typedef typename M1::Value Value;
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);}
820 ///Returns an \ref ForkMap class
822 ///This function just returns an \ref ForkMap class.
823 ///\todo How to call these type of functions?
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)
830 return ForkMap<M1,M2>(m1,m2);
835 /* ************* BOOL MAPS ******************* */
837 ///Logical 'not' of a map
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>.
848 typename SmartConstReference<M>::Type m;
851 typedef True NeedCopy;
853 typedef typename M::Key Key;
858 NotMap(const M &_m) : m(_m) {};
859 Value operator[](Key k) const {return !m[k];}
862 ///Returns a \ref NotMap class
864 ///This function just returns a \ref NotMap class.
867 inline NotMap<M> notMap(const M &m)
885 #endif // LEMON_MAPS_H