2 * src/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
23 ///\brief Miscellaneous property maps
25 ///\todo This file has the same name as the concept file in concept/,
26 /// and this is not easily detectable in docs...
35 /// Base class of maps.
37 /// Base class of maps.
38 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
39 template<typename K, typename T>
49 /// Null map. (a.k.a. DoNothingMap)
51 /// If you have to provide a map only for its type definitions,
52 /// or if you have to provide a writable map, but
53 /// data written to it will sent to <tt>/dev/null</tt>...
54 template<typename K, typename T>
55 class NullMap : public MapBase<K,T>
59 /// Gives back a default constructed element.
60 T operator[](const K&) const { return T(); }
61 /// Absorbs the value.
62 void set(const K&, const T&) {}
68 /// This is a readable map which assigns a specified value to each key.
69 /// In other aspects it is equivalent to the \ref NullMap.
70 /// \todo set could be used to set the value.
71 template<typename K, typename T>
72 class ConstMap : public MapBase<K,T>
77 /// Default constructor
79 /// The value of the map will be uninitialized.
80 /// (More exactly it will be default constructed.)
84 /// \param _v The initial value of the map.
86 ConstMap(const T &_v) : v(_v) {}
88 T operator[](const K&) const { return v; }
89 void set(const K&, const T&) {}
93 typedef ConstMap<K,T1> other;
97 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
100 ///Returns a \ref ConstMap class
102 ///This function just returns a \ref ConstMap class.
104 template<class V,class K>
105 inline ConstMap<V,K> constMap(const K &k)
107 return ConstMap<V,K>(k);
112 template<typename T, T v>
115 template<typename K, typename V, V v>
116 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
120 V operator[](const K&) const { return v; }
121 void set(const K&, const V&) { }
124 /// \c std::map wrapper
126 /// This is essentially a wrapper for \c std::map. With addition that
127 /// you can specify a default value different from \c Value() .
129 /// \todo Provide allocator parameter...
130 template <typename K, typename T, typename Compare = std::less<K> >
131 class StdMap : public std::map<K,T,Compare> {
132 typedef std::map<K,T,Compare> parent;
134 typedef typename parent::value_type PairType;
139 typedef T& Reference;
140 typedef const T& ConstReference;
144 /// Constructor with specified default value
145 StdMap(const T& _v) : v(_v) {}
147 /// \brief Constructs the map from an appropriate std::map.
149 /// \warning Inefficient: copies the content of \c m !
150 StdMap(const parent &m) : parent(m) {}
151 /// \brief Constructs the map from an appropriate std::map, and explicitly
152 /// specifies a default value.
154 /// \warning Inefficient: copies the content of \c m !
155 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
157 template<typename T1, typename Comp1>
158 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
162 Reference operator[](const Key &k) {
163 return insert(PairType(k,v)).first -> second;
165 ConstReference operator[](const Key &k) const {
166 typename parent::iterator i = lower_bound(k);
167 if (i == parent::end() || parent::key_comp()(k, (*i).first))
171 void set(const Key &k, const T &t) {
172 parent::operator[](k) = t;
175 /// Changes the default value of the map.
176 /// \return Returns the previous default value.
178 /// \warning The value of some keys (which has already been queried, but
179 /// the value has been unchanged from the default) may change!
180 T setDefault(const T &_v) { T old=v; v=_v; return old; }
182 template<typename T1>
184 typedef StdMap<Key,T1,Compare> other;
190 /// \addtogroup map_adaptors
194 ///Convert the \c Value of a maps to another type.
196 ///This \ref concept::ReadMap "read only map"
197 ///converts the \c Value of a maps to type \c T.
198 ///Its \c Value is inherited from \c M.
202 /// ConvertMap<X> sh(x,v);
204 ///it is equivalent with
206 /// ConstMap<X::Key, X::Value> c_tmp(v);
207 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
209 ///\bug wrong documentation
210 template<class M, class T>
215 typedef typename M::Key Key;
221 ///\param _m is the undelying map
222 ///\param _v is the convert value
223 ConvertMap(const M &_m) : m(_m) {};
225 /// \brief The subscript operator.
227 /// The subscript operator.
228 /// \param edge The edge
229 /// \return The target of the edge
230 Value operator[](Key k) const {return m[k];}
233 ///Returns an \ref ConvertMap class
235 ///This function just returns an \ref ConvertMap class.
236 ///\relates ConvertMap
237 ///\todo The order of the template parameters are changed.
238 template<class T, class M>
239 inline ConvertMap<M,T> convertMap(const M &m)
241 return ConvertMap<M,T>(m);
246 ///This \ref concept::ReadMap "read only map" returns the sum of the two
247 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
248 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
250 template<class M1,class M2>
256 typedef typename M1::Key Key;
257 typedef typename M1::Value Value;
263 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
264 Value operator[](Key k) const {return m1[k]+m2[k];}
267 ///Returns an \ref AddMap class
269 ///This function just returns an \ref AddMap class.
270 ///\todo How to call these type of functions?
273 ///\todo Wrong scope in Doxygen when \c \\relates is used
274 template<class M1,class M2>
275 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
277 return AddMap<M1,M2>(m1,m2);
280 ///Shift a maps with a constant.
282 ///This \ref concept::ReadMap "read only map" returns the sum of the
283 ///given map and a constant value.
284 ///Its \c Key and \c Value is inherited from \c M.
288 /// ShiftMap<X> sh(x,v);
290 ///it is equivalent with
292 /// ConstMap<X::Key, X::Value> c_tmp(v);
293 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
301 typedef typename M::Key Key;
302 typedef typename M::Value Value;
307 ///\param _m is the undelying map
308 ///\param _v is the shift value
309 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
310 Value operator[](Key k) const {return m[k]+v;}
313 ///Returns an \ref ShiftMap class
315 ///This function just returns an \ref ShiftMap class.
317 ///\todo A better name is required.
319 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
321 return ShiftMap<M>(m,v);
324 ///Difference of two maps
326 ///This \ref concept::ReadMap "read only map" returns the difference
327 ///of the values returned by the two
328 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
329 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
331 template<class M1,class M2>
337 typedef typename M1::Key Key;
338 typedef typename M1::Value Value;
344 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
345 Value operator[](Key k) const {return m1[k]-m2[k];}
348 ///Returns a \ref SubMap class
350 ///This function just returns a \ref SubMap class.
353 template<class M1,class M2>
354 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
356 return SubMap<M1,M2>(m1,m2);
359 ///Product of two maps
361 ///This \ref concept::ReadMap "read only map" returns the product of the
362 ///values returned by the two
364 ///maps. Its \c Key and \c Value will be inherited from \c M1.
365 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
367 template<class M1,class M2>
373 typedef typename M1::Key Key;
374 typedef typename M1::Value Value;
380 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
381 Value operator[](Key k) const {return m1[k]*m2[k];}
384 ///Returns a \ref MulMap class
386 ///This function just returns a \ref MulMap class.
388 template<class M1,class M2>
389 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
391 return MulMap<M1,M2>(m1,m2);
394 ///Scale a maps with a constant.
396 ///This \ref concept::ReadMap "read only map" returns the value of the
397 ///given map multipied with a constant value.
398 ///Its \c Key and \c Value is inherited from \c M.
402 /// ScaleMap<X> sc(x,v);
404 ///it is equivalent with
406 /// ConstMap<X::Key, X::Value> c_tmp(v);
407 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
415 typedef typename M::Key Key;
416 typedef typename M::Value Value;
421 ///\param _m is the undelying map
422 ///\param _v is the scaling value
423 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
424 Value operator[](Key k) const {return m[k]*v;}
427 ///Returns an \ref ScaleMap class
429 ///This function just returns an \ref ScaleMap class.
431 ///\todo A better name is required.
433 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
435 return ScaleMap<M>(m,v);
438 ///Quotient of two maps
440 ///This \ref concept::ReadMap "read only map" returns the quotient of the
441 ///values returned by the two
442 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
443 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
445 template<class M1,class M2>
451 typedef typename M1::Key Key;
452 typedef typename M1::Value Value;
458 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
459 Value operator[](Key k) const {return m1[k]/m2[k];}
462 ///Returns a \ref DivMap class
464 ///This function just returns a \ref DivMap class.
466 template<class M1,class M2>
467 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
469 return DivMap<M1,M2>(m1,m2);
472 ///Composition of two maps
474 ///This \ref concept::ReadMap "read only map" returns the composition of
476 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
480 /// ComposeMap<M1,M2> cm(m1,m2);
482 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
484 ///Its \c Key is inherited from \c M2 and its \c Value is from
486 ///The \c M2::Value must be convertible to \c M1::Key.
487 ///\todo Check the requirements.
489 template<class M1,class M2>
495 typedef typename M2::Key Key;
496 typedef typename M1::Value Value;
502 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
503 Value operator[](Key k) const {return m1[m2[k]];}
505 ///Returns a \ref ComposeMap class
507 ///This function just returns a \ref ComposeMap class.
509 ///\relates ComposeMap
510 template<class M1,class M2>
511 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
513 return ComposeMap<M1,M2>(m1,m2);
516 ///Combine of two maps using an STL (binary) functor.
518 ///Combine of two maps using an STL (binary) functor.
521 ///This \ref concept::ReadMap "read only map" takes to maps and a
522 ///binary functor and returns the composition of
524 ///given maps unsing the functor.
525 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
526 ///and \c f is of \c F,
529 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
531 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
533 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
534 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
535 ///input parameter of \c F and the return type of \c F must be convertible
537 ///\todo Check the requirements.
539 template<class M1,class M2,class F,class V>
546 typedef typename M1::Key Key;
553 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
554 : m1(_m1), m2(_m2), f(_f) {};
555 Value operator[](Key k) const {return f(m1[k],m2[k]);}
558 ///Returns a \ref CombineMap class
560 ///This function just returns a \ref CombineMap class.
562 ///Only the first template parameter (the value type) must be given.
564 ///For example if \c m1 and \c m2 are both \c double valued maps, then
566 ///combineMap<double>(m1,m2,std::plus<double>)
568 ///is equivalent with
573 ///\relates CombineMap
574 template<class V,class M1,class M2,class F>
575 inline CombineMap<M1,M2,F,V> combineMap(const M1 &m1,const M2 &m2,const F &f)
577 return CombineMap<M1,M2,F,V>(m1,m2,f);
580 ///Negative value of a map
582 ///This \ref concept::ReadMap "read only map" returns the negative
584 ///value returned by the
585 ///given map. Its \c Key and \c Value will be inherited from \c M.
586 ///The unary \c - operator must be defined for \c Value, of course.
593 typedef typename M::Key Key;
594 typedef typename M::Value Value;
600 NegMap(const M &_m) : m(_m) {};
601 Value operator[](Key k) const {return -m[k];}
604 ///Returns a \ref NegMap class
606 ///This function just returns a \ref NegMap class.
609 inline NegMap<M> negMap(const M &m)
615 ///Absolute value of a map
617 ///This \ref concept::ReadMap "read only map" returns the absolute value
619 ///value returned by the
620 ///given map. Its \c Key and \c Value will be inherited
621 ///from <tt>M</tt>. <tt>Value</tt>
622 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
623 ///operator must be defined for it, of course.
625 ///\bug We need a unified way to handle the situation below:
627 /// struct _UnConvertible {};
628 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
629 /// template<> inline int t_abs<>(int n) {return abs(n);}
630 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
631 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
632 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
633 /// template<> inline double t_abs<>(double n) {return fabs(n);}
634 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
643 typedef typename M::Key Key;
644 typedef typename M::Value Value;
650 AbsMap(const M &_m) : m(_m) {};
651 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
654 ///Returns a \ref AbsMap class
656 ///This function just returns a \ref AbsMap class.
659 inline AbsMap<M> absMap(const M &m)
664 ///Converts an STL style functor to a map
666 ///This \ref concept::ReadMap "read only map" returns the value
670 ///Template parameters \c K and \c V will become its
671 ///\c Key and \c Value. They must be given explicitely
672 ///because a functor does not provide such typedefs.
674 ///Parameter \c F is the type of the used functor.
677 template<class K,class V,class F>
689 FunctorMap(const F &_f) : f(_f) {};
690 Value operator[](Key k) const {return f(k);}
693 ///Returns a \ref FunctorMap class
695 ///This function just returns a \ref FunctorMap class.
697 ///The third template parameter isn't necessary to be given.
698 ///\relates FunctorMap
699 template<class K,class V, class F>
700 inline FunctorMap<K,V,F> functorMap(const F &f)
702 return FunctorMap<K,V,F>(f);
705 ///Converts a map to an STL style (unary) functor
707 ///This class Converts a map to an STL style (unary) functor.
708 ///that is it provides an <tt>operator()</tt> to read its values.
710 ///For the sake of convenience it also works as
711 ///a ususal \ref concept::ReadMap "readable map", i.e
712 ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
719 typedef typename M::Key argument_type;
720 typedef typename M::Value result_type;
721 typedef typename M::Key Key;
722 typedef typename M::Value Value;
728 MapFunctor(const M &_m) : m(_m) {};
729 ///Returns a value of the map
733 Value operator()(Key k) const {return m[k];}
736 Value operator[](Key k) const {return m[k];}
739 ///Returns a \ref MapFunctor class
741 ///This function just returns a \ref MapFunctor class.
742 ///\relates MapFunctor
744 inline MapFunctor<M> mapFunctor(const M &m)
746 return MapFunctor<M>(m);
750 ///Apply all map setting operations to two maps
752 ///This map has two \ref concept::WriteMap "writable map"
753 ///parameters and each write request will be passed to both of them.
754 ///If \c M1 is also \ref concept::ReadMap "readable",
755 ///then the read operations will return the
756 ///corresponding values of \c M1.
758 ///The \c Key and \c Value will be inherited from \c M1.
759 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
761 template<class M1,class M2>
767 typedef typename M1::Key Key;
768 typedef typename M1::Value Value;
774 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
775 Value operator[](Key k) const {return m1[k];}
776 void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
779 ///Returns an \ref ForkMap class
781 ///This function just returns an \ref ForkMap class.
782 ///\todo How to call these type of functions?
785 ///\todo Wrong scope in Doxygen when \c \\relates is used
786 template<class M1,class M2>
787 inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
789 return ForkMap<M1,M2>(m1,m2);
797 #endif // LEMON_MAPS_H