- Use messages similar to stock autoconf macros'.
2 * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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
24 ///\brief Miscellaneous property maps
26 ///\todo This file has the same name as the concept file in concept/,
27 /// and this is not easily detectable in docs...
36 /// Base class of maps.
38 /// Base class of maps.
39 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
40 template<typename K, typename T>
50 /// Null map. (a.k.a. DoNothingMap)
52 /// If you have to provide a map only for its type definitions,
53 /// or if you have to provide a writable map, but
54 /// data written to it will sent to <tt>/dev/null</tt>...
55 template<typename K, typename T>
56 class NullMap : public MapBase<K,T>
60 /// Gives back a default constructed element.
61 T operator[](const K&) const { return T(); }
62 /// Absorbs the value.
63 void set(const K&, const T&) {}
69 /// This is a readable map which assigns a specified value to each key.
70 /// In other aspects it is equivalent to the \ref NullMap.
71 /// \todo set could be used to set the value.
72 template<typename K, typename T>
73 class ConstMap : public MapBase<K,T>
78 /// Default constructor
80 /// The value of the map will be uninitialized.
81 /// (More exactly it will be default constructed.)
85 /// \param _v The initial value of the map.
87 ConstMap(const T &_v) : v(_v) {}
89 T operator[](const K&) const { return v; }
90 void set(const K&, const T&) {}
94 typedef ConstMap<K,T1> other;
98 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
101 ///Returns a \ref ConstMap class
103 ///This function just returns a \ref ConstMap class.
105 template<class V,class K>
106 inline ConstMap<V,K> constMap(const K &k)
108 return ConstMap<V,K>(k);
113 template<typename T, T v>
116 template<typename K, typename V, V v>
117 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
121 V operator[](const K&) const { return v; }
122 void set(const K&, const V&) { }
125 /// \c std::map wrapper
127 /// This is essentially a wrapper for \c std::map. With addition that
128 /// you can specify a default value different from \c Value() .
130 /// \todo Provide allocator parameter...
131 template <typename K, typename T, typename Compare = std::less<K> >
132 class StdMap : public std::map<K,T,Compare> {
133 typedef std::map<K,T,Compare> parent;
135 typedef typename parent::value_type PairType;
140 typedef T& Reference;
141 typedef const T& ConstReference;
145 /// Constructor with specified default value
146 StdMap(const T& _v) : v(_v) {}
148 /// \brief Constructs the map from an appropriate std::map.
150 /// \warning Inefficient: copies the content of \c m !
151 StdMap(const parent &m) : parent(m) {}
152 /// \brief Constructs the map from an appropriate std::map, and explicitly
153 /// specifies a default value.
155 /// \warning Inefficient: copies the content of \c m !
156 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
158 template<typename T1, typename Comp1>
159 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
163 Reference operator[](const Key &k) {
164 return insert(PairType(k,v)).first -> second;
166 ConstReference operator[](const Key &k) const {
167 typename parent::iterator i = lower_bound(k);
168 if (i == parent::end() || parent::key_comp()(k, (*i).first))
172 void set(const Key &k, const T &t) {
173 parent::operator[](k) = t;
176 /// Changes the default value of the map.
177 /// \return Returns the previous default value.
179 /// \warning The value of some keys (which has already been queried, but
180 /// the value has been unchanged from the default) may change!
181 T setDefault(const T &_v) { T old=v; v=_v; return old; }
183 template<typename T1>
185 typedef StdMap<Key,T1,Compare> other;
189 ///Convert the \c Value of a maps to another type.
191 ///This \ref concept::ReadMap "read only map"
192 ///converts the \c Value of a maps to type \c T.
193 ///Its \c Value is inherited from \c M.
197 /// ConvertMap<X> sh(x,v);
199 ///it is equivalent with
201 /// ConstMap<X::Key, X::Value> c_tmp(v);
202 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
204 ///\bug wrong documentation
205 template<class M, class T>
210 typedef typename M::Key Key;
216 ///\param _m is the undelying map
217 ///\param _v is the convert value
218 ConvertMap(const M &_m) : m(_m) {};
220 /// \brief The subscript operator.
222 /// The subscript operator.
223 /// \param edge The edge
224 /// \return The target of the edge
225 Value operator[](Key k) const {return m[k];}
228 ///Returns an \ref ConvertMap class
230 ///This function just returns an \ref ConvertMap class.
231 ///\relates ConvertMap
232 ///\todo The order of the template parameters are changed.
233 template<class T, class M>
234 inline ConvertMap<M,T> convertMap(const M &m)
236 return ConvertMap<M,T>(m);
239 /// \brief Returns the source of the given edge.
241 /// The SourceMap gives back the source Node of the given edge.
242 /// \author Balazs Dezso
243 template <typename Graph>
246 typedef typename Graph::Node Value;
247 typedef typename Graph::Edge Key;
249 /// \brief Constructor
252 /// \param _graph The graph that the map belongs to.
253 SourceMap(const Graph& _graph) : graph(_graph) {}
255 /// \brief The subscript operator.
257 /// The subscript operator.
258 /// \param edge The edge
259 /// \return The source of the edge
260 Value operator[](const Key& edge) {
261 return graph.source(edge);
268 /// \brief Returns a \ref SourceMap class
270 /// This function just returns an \ref SourceMap class.
271 /// \relates SourceMap
272 template <typename Graph>
273 inline SourceMap<Graph> sourceMap(const Graph&) {
274 return SourceMap<Graph>(graph);
277 /// \brief Returns the target of the given edge.
279 /// The TargetMap gives back the target Node of the given edge.
280 /// \author Balazs Dezso
281 template <typename Graph>
284 typedef typename Graph::Node Value;
285 typedef typename Graph::Edge Key;
287 /// \brief Constructor
290 /// \param _graph The graph that the map belongs to.
291 TargetMap(const Graph& _graph) : graph(_graph) {}
293 /// \brief The subscript operator.
295 /// The subscript operator.
296 /// \param edge The edge
297 /// \return The target of the edge
298 Value operator[](const Key& key) {
299 return graph.target(key);
306 /// \brief Returns a \ref TargetMap class
308 /// This function just returns an \ref TargetMap class.
309 /// \relates TargetMap
310 template <typename Graph>
311 inline TargetMap<Graph> targetMap(const Graph&) {
312 return TargetMap<Graph>(graph);
317 ///This \ref concept::ReadMap "read only map" returns the sum of the two
318 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
319 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
321 template<class M1,class M2>
327 typedef typename M1::Key Key;
328 typedef typename M1::Value Value;
334 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
335 Value operator[](Key k) const {return m1[k]+m2[k];}
338 ///Returns an \ref AddMap class
340 ///This function just returns an \ref AddMap class.
341 ///\todo How to call these type of functions?
344 ///\todo Wrong scope in Doxygen when \c \\relates is used
345 template<class M1,class M2>
346 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
348 return AddMap<M1,M2>(m1,m2);
351 ///Shift a maps with a constant.
353 ///This \ref concept::ReadMap "read only map" returns the sum of the
354 ///given map and a constant value.
355 ///Its \c Key and \c Value is inherited from \c M.
359 /// ShiftMap<X> sh(x,v);
361 ///it is equivalent with
363 /// ConstMap<X::Key, X::Value> c_tmp(v);
364 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
372 typedef typename M::Key Key;
373 typedef typename M::Value Value;
378 ///\param _m is the undelying map
379 ///\param _v is the shift value
380 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
381 Value operator[](Key k) const {return m[k]+v;}
384 ///Returns an \ref ShiftMap class
386 ///This function just returns an \ref ShiftMap class.
388 ///\todo A better name is required.
390 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
392 return ShiftMap<M>(m,v);
395 ///Difference of two maps
397 ///This \ref concept::ReadMap "read only map" returns the difference
398 ///of the values returned by the two
399 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
400 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
402 template<class M1,class M2>
408 typedef typename M1::Key Key;
409 typedef typename M1::Value Value;
415 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
416 Value operator[](Key k) const {return m1[k]-m2[k];}
419 ///Returns a \ref SubMap class
421 ///This function just returns a \ref SubMap class.
424 template<class M1,class M2>
425 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
427 return SubMap<M1,M2>(m1,m2);
430 ///Product of two maps
432 ///This \ref concept::ReadMap "read only map" returns the product of the
433 ///values returned by the two
435 ///maps. Its \c Key and \c Value will be inherited from \c M1.
436 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
438 template<class M1,class M2>
444 typedef typename M1::Key Key;
445 typedef typename M1::Value Value;
451 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
452 Value operator[](Key k) const {return m1[k]*m2[k];}
455 ///Returns a \ref MulMap class
457 ///This function just returns a \ref MulMap class.
459 template<class M1,class M2>
460 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
462 return MulMap<M1,M2>(m1,m2);
465 ///Scale a maps with a constant.
467 ///This \ref concept::ReadMap "read only map" returns the value of the
468 ///given map multipied with a constant value.
469 ///Its \c Key and \c Value is inherited from \c M.
473 /// ScaleMap<X> sc(x,v);
475 ///it is equivalent with
477 /// ConstMap<X::Key, X::Value> c_tmp(v);
478 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
486 typedef typename M::Key Key;
487 typedef typename M::Value Value;
492 ///\param _m is the undelying map
493 ///\param _v is the scaling value
494 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
495 Value operator[](Key k) const {return m[k]*v;}
498 ///Returns an \ref ScaleMap class
500 ///This function just returns an \ref ScaleMap class.
502 ///\todo A better name is required.
504 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
506 return ScaleMap<M>(m,v);
509 ///Quotient of two maps
511 ///This \ref concept::ReadMap "read only map" returns the quotient of the
512 ///values returned by the two
513 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
514 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
516 template<class M1,class M2>
522 typedef typename M1::Key Key;
523 typedef typename M1::Value Value;
529 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
530 Value operator[](Key k) const {return m1[k]/m2[k];}
533 ///Returns a \ref DivMap class
535 ///This function just returns a \ref DivMap class.
537 template<class M1,class M2>
538 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
540 return DivMap<M1,M2>(m1,m2);
543 ///Composition of two maps
545 ///This \ref concept::ReadMap "read only map" returns the composition of
547 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
551 /// ComposeMap<M1,M2> cm(m1,m2);
553 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
555 ///Its \c Key is inherited from \c M2 and its \c Value is from
557 ///The \c M2::Value must be convertible to \c M1::Key.
558 ///\todo Check the requirements.
560 template<class M1,class M2>
566 typedef typename M2::Key Key;
567 typedef typename M1::Value Value;
573 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
574 Value operator[](Key k) const {return m1[m2[k]];}
576 ///Returns a \ref ComposeMap class
578 ///This function just returns a \ref ComposeMap class.
580 ///\relates ComposeMap
581 template<class M1,class M2>
582 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
584 return ComposeMap<M1,M2>(m1,m2);
587 ///Combine of two maps using an STL (binary) functor.
589 ///Combine of two maps using an STL (binary) functor.
592 ///This \ref concept::ReadMap "read only map" takes to maps and a
593 ///binary functor and returns the composition of
595 ///given maps unsing the functor.
596 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
597 ///and \c f is of \c F,
600 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
602 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
604 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
605 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
606 ///input parameter of \c F and the return type of \c F must be convertible
608 ///\todo Check the requirements.
610 template<class M1,class M2,class F,class V>
617 typedef typename M1::Key Key;
624 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
625 : m1(_m1), m2(_m2), f(_f) {};
626 Value operator[](Key k) const {return f(m1[k],m2[k]);}
629 ///Returns a \ref CombineMap class
631 ///This function just returns a \ref CombineMap class.
633 ///Only the first template parameter (the value type) must be given.
635 ///For example if \c m1 and \c m2 are both \c double valued maps, then
637 ///combineMap<double>(m1,m2,std::plus<double>)
639 ///is equivalent with
644 ///\relates CombineMap
645 template<class V,class M1,class M2,class F>
646 inline CombineMap<M1,M2,F,V> combineMap(const M1 &m1,const M2 &m2,const F &f)
648 return CombineMap<M1,M2,F,V>(m1,m2,f);
651 ///Negative value of a map
653 ///This \ref concept::ReadMap "read only map" returns the negative
655 ///value returned by the
656 ///given map. Its \c Key and \c Value will be inherited from \c M.
657 ///The unary \c - operator must be defined for \c Value, of course.
664 typedef typename M::Key Key;
665 typedef typename M::Value Value;
671 NegMap(const M &_m) : m(_m) {};
672 Value operator[](Key k) const {return -m[k];}
675 ///Returns a \ref NegMap class
677 ///This function just returns a \ref NegMap class.
680 inline NegMap<M> negMap(const M &m)
686 ///Absolute value of a map
688 ///This \ref concept::ReadMap "read only map" returns the absolute value
690 ///value returned by the
691 ///given map. Its \c Key and \c Value will be inherited
692 ///from <tt>M</tt>. <tt>Value</tt>
693 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
694 ///operator must be defined for it, of course.
696 ///\bug We need a unified way to handle the situation below:
698 /// struct _UnConvertible {};
699 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
700 /// template<> inline int t_abs<>(int n) {return abs(n);}
701 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
702 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
703 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
704 /// template<> inline double t_abs<>(double n) {return fabs(n);}
705 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
714 typedef typename M::Key Key;
715 typedef typename M::Value Value;
721 AbsMap(const M &_m) : m(_m) {};
722 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
725 ///Returns a \ref AbsMap class
727 ///This function just returns a \ref AbsMap class.
730 inline AbsMap<M> absMap(const M &m)
735 ///Converts an STL style functor to a a map
737 ///This \ref concept::ReadMap "read only map" returns the value
741 ///Template parameters \c K and \c V will become its
742 ///\c Key and \c Value. They must be given explicitely
743 ///because a functor does not provide such typedefs.
745 ///Parameter \c F is the type of the used functor.
748 template<class K,class V,class F>
760 FunctorMap(const F &_f) : f(_f) {};
761 Value operator[](Key k) const {return f(k);}
764 ///Returns a \ref FunctorMap class
766 ///This function just returns a \ref FunctorMap class.
768 ///The third template parameter isn't necessary to be given.
769 ///\relates FunctorMap
770 template<class K,class V, class F>
771 inline FunctorMap<K,V,F> functorMap(const F &f)
773 return FunctorMap<K,V,F>(f);
776 ///Converts a map to an STL style (unary) functor
778 ///This class Converts a map to an STL style (unary) functor.
779 ///that is it provides an <tt>operator()</tt> to read its values.
781 ///For the sake of convenience it also works as
782 ///a ususal \ref concept::ReadMap "readable map", i.e
783 ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
790 typedef typename M::Key argument_type;
791 typedef typename M::Value result_type;
792 typedef typename M::Key Key;
793 typedef typename M::Value Value;
799 MapFunctor(const M &_m) : m(_m) {};
800 ///Returns a value of the map
804 Value operator()(Key k) const {return m[k];}
807 Value operator[](Key k) const {return m[k];}
810 ///Returns a \ref MapFunctor class
812 ///This function just returns a \ref MapFunctor class.
813 ///\relates MapFunctor
815 inline MapFunctor<M> mapFunctor(const M &m)
817 return MapFunctor<M>(m);
821 ///Apply all map setting operations to two maps
823 ///This map has two \ref concept::WriteMap "writable map"
824 ///parameters and each write request will be passed to both of them.
825 ///If \c M1 is also \ref concept::ReadMap "readable",
826 ///then the read operations will return the
827 ///corresponding values of \c M1.
829 ///The \c Key and \c Value will be inherited from \c M1.
830 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
832 template<class M1,class M2>
838 typedef typename M1::Key Key;
839 typedef typename M1::Value Value;
845 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
846 Value operator[](Key k) const {return m1[k];}
847 void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
850 ///Returns an \ref ForkMap class
852 ///This function just returns an \ref ForkMap class.
853 ///\todo How to call these type of functions?
856 ///\todo Wrong scope in Doxygen when \c \\relates is used
857 template<class M1,class M2>
858 inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
860 return ForkMap<M1,M2>(m1,m2);
868 #endif // LEMON_MAPS_H