src/lemon/maps.h
author alpar
Fri, 22 Apr 2005 17:47:01 +0000
changeset 1381 998e8def9676
parent 1357 8d9c47f31699
child 1402 655d8e78454d
permissions -rw-r--r--
- lp_cplex.h, lp_cplex.cc added
- lp_demo.cc and lp_maxflow_demo.cc uses GLPK is it is found CPLEX otherwise
     1 /* -*- C++ -*-
     2  * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     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.
    10  *
    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
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_MAPS_H
    18 #define LEMON_MAPS_H
    19 
    20 #include<math.h>
    21 
    22 ///\file
    23 ///\ingroup maps
    24 ///\brief Miscellaneous property maps
    25 ///
    26 ///\todo This file has the same name as the concept file in concept/,
    27 /// and this is not easily detectable in docs...
    28 
    29 #include <map>
    30 
    31 namespace lemon {
    32 
    33   /// \addtogroup maps
    34   /// @{
    35 
    36   /// Base class of maps.
    37 
    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>
    41   class MapBase
    42   {
    43   public:
    44     ///\e
    45     typedef K Key;
    46     ///\e
    47     typedef T Value;
    48   };
    49 
    50   /// Null map. (a.k.a. DoNothingMap)
    51 
    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>
    57   {
    58   public:
    59 
    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&) {}
    64   };
    65 
    66 
    67   /// Constant map.
    68 
    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>
    74   {
    75     T v;
    76   public:
    77 
    78     /// Default constructor
    79 
    80     /// The value of the map will be uninitialized. 
    81     /// (More exactly it will be default constructed.)
    82     ConstMap() {}
    83     ///\e
    84 
    85     /// \param _v The initial value of the map.
    86     ///
    87     ConstMap(const T &_v) : v(_v) {}
    88 
    89     T operator[](const K&) const { return v; }
    90     void set(const K&, const T&) {}
    91 
    92     template<typename T1>
    93     struct rebind {
    94       typedef ConstMap<K,T1> other;
    95     };
    96 
    97     template<typename T1>
    98     ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
    99   };
   100 
   101   ///Returns a \ref ConstMap class
   102 
   103   ///This function just returns a \ref ConstMap class.
   104   ///\relates ConstMap
   105   template<class V,class K> 
   106   inline ConstMap<V,K> constMap(const K &k) 
   107   {
   108     return ConstMap<V,K>(k);
   109   }
   110 
   111 
   112   //to document later
   113   template<typename T, T v>
   114   struct Const { };
   115   //to document later
   116   template<typename K, typename V, V v>
   117   class ConstMap<K, Const<V, v> > : public MapBase<K, V>
   118   {
   119   public:
   120     ConstMap() { }
   121     V operator[](const K&) const { return v; }
   122     void set(const K&, const V&) { }
   123   };
   124 
   125   /// \c std::map wrapper
   126 
   127   /// This is essentially a wrapper for \c std::map. With addition that
   128   /// you can specify a default value different from \c Value() .
   129   ///
   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;
   134     T v;
   135     typedef typename parent::value_type PairType;
   136 
   137   public:
   138     typedef K Key;
   139     typedef T Value;
   140     typedef T& Reference;
   141     typedef const T& ConstReference;
   142 
   143 
   144     StdMap() : v() {}
   145     /// Constructor with specified default value
   146     StdMap(const T& _v) : v(_v) {}
   147 
   148     /// \brief Constructs the map from an appropriate std::map.
   149     ///
   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.
   154     ///
   155     /// \warning Inefficient: copies the content of \c m !
   156     StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
   157     
   158     template<typename T1, typename Comp1>
   159     StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
   160       //FIXME; 
   161     }
   162 
   163     Reference operator[](const Key &k) {
   164       return insert(PairType(k,v)).first -> second;
   165     }
   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))
   169 	return v;
   170       return (*i).second;
   171     }
   172     void set(const Key &k, const T &t) {
   173       parent::operator[](k) = t;
   174     }
   175 
   176     /// Changes the default value of the map.
   177     /// \return Returns the previous default value.
   178     ///
   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; }
   182 
   183     template<typename T1>
   184     struct rebind {
   185       typedef StdMap<Key,T1,Compare> other;
   186     };
   187   };
   188 
   189   ///Convert the \c Value of a maps to another type.
   190 
   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.
   194   ///
   195   ///Actually,
   196   ///\code
   197   ///  ConvertMap<X> sh(x,v);
   198   ///\endcode
   199   ///it is equivalent with
   200   ///\code
   201   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   202   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   203   ///\endcode
   204   ///\bug wrong documentation
   205   template<class M, class T> 
   206   class ConvertMap
   207   {
   208     const M &m;
   209   public:
   210     typedef typename M::Key Key;
   211     typedef T Value;
   212 
   213     ///Constructor
   214 
   215     ///Constructor
   216     ///\param _m is the undelying map
   217     ///\param _v is the convert value
   218     ConvertMap(const M &_m) : m(_m) {};
   219 
   220     /// \brief The subscript operator.
   221     ///
   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];}
   226   };
   227   
   228   ///Returns an \ref ConvertMap class
   229 
   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) 
   235   {
   236     return ConvertMap<M,T>(m);
   237   }
   238 
   239   /// \brief Returns the source of the given edge.
   240   ///
   241   /// The SourceMap gives back the source Node of the given edge. 
   242   /// \author Balazs Dezso
   243   template <typename Graph>
   244   class SourceMap {
   245   public:
   246     typedef typename Graph::Node Value;
   247     typedef typename Graph::Edge Key;
   248 
   249     /// \brief Constructor
   250     ///
   251     /// Constructor
   252     /// \param _graph The graph that the map belongs to.
   253     SourceMap(const Graph& _graph) : graph(_graph) {}
   254 
   255     /// \brief The subscript operator.
   256     ///
   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);
   262     }
   263 
   264   private:
   265     const Graph& graph;
   266   };
   267 
   268   /// \brief Returns a \ref SourceMap class
   269 
   270   /// This function just returns an \ref SourceMap class.
   271   /// \relates SourceMap
   272   template <typename Graph>
   273   inline SourceMap<Graph> sourceMap(const Graph& graph) {
   274     return SourceMap<Graph>(graph);
   275   } 
   276 
   277   /// \brief Returns the target of the given edge.
   278   ///
   279   /// The TargetMap gives back the target Node of the given edge. 
   280   /// \author Balazs Dezso
   281   template <typename Graph>
   282   class TargetMap {
   283   public:
   284     typedef typename Graph::Node Value;
   285     typedef typename Graph::Edge Key;
   286 
   287     /// \brief Constructor
   288     ///
   289     /// Constructor
   290     /// \param _graph The graph that the map belongs to.
   291     TargetMap(const Graph& _graph) : graph(_graph) {}
   292 
   293     /// \brief The subscript operator.
   294     ///
   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);
   300     }
   301 
   302   private:
   303     const Graph& graph;
   304   };
   305 
   306   /// \brief Returns a \ref TargetMap class
   307 
   308   /// This function just returns an \ref TargetMap class.
   309   /// \relates TargetMap
   310   template <typename Graph>
   311   inline TargetMap<Graph> targetMap(const Graph& graph) {
   312     return TargetMap<Graph>(graph);
   313   }
   314 
   315   ///Sum of two maps
   316 
   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.
   320 
   321   template<class M1,class M2> 
   322   class AddMap
   323   {
   324     const M1 &m1;
   325     const M2 &m2;
   326   public:
   327     typedef typename M1::Key Key;
   328     typedef typename M1::Value Value;
   329 
   330     ///Constructor
   331 
   332     ///\e
   333     ///
   334     AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   335     Value operator[](Key k) const {return m1[k]+m2[k];}
   336   };
   337   
   338   ///Returns an \ref AddMap class
   339 
   340   ///This function just returns an \ref AddMap class.
   341   ///\todo How to call these type of functions?
   342   ///
   343   ///\relates AddMap
   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) 
   347   {
   348     return AddMap<M1,M2>(m1,m2);
   349   }
   350 
   351   ///Shift a maps with a constant.
   352 
   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.
   356   ///
   357   ///Actually,
   358   ///\code
   359   ///  ShiftMap<X> sh(x,v);
   360   ///\endcode
   361   ///it is equivalent with
   362   ///\code
   363   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   364   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   365   ///\endcode
   366   template<class M> 
   367   class ShiftMap
   368   {
   369     const M &m;
   370     typename M::Value v;
   371   public:
   372     typedef typename M::Key Key;
   373     typedef typename M::Value Value;
   374 
   375     ///Constructor
   376 
   377     ///Constructor
   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;}
   382   };
   383   
   384   ///Returns an \ref ShiftMap class
   385 
   386   ///This function just returns an \ref ShiftMap class.
   387   ///\relates ShiftMap
   388   ///\todo A better name is required.
   389   template<class M> 
   390   inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) 
   391   {
   392     return ShiftMap<M>(m,v);
   393   }
   394 
   395   ///Difference of two maps
   396 
   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.
   401 
   402   template<class M1,class M2> 
   403   class SubMap
   404   {
   405     const M1 &m1;
   406     const M2 &m2;
   407   public:
   408     typedef typename M1::Key Key;
   409     typedef typename M1::Value Value;
   410 
   411     ///Constructor
   412 
   413     ///\e
   414     ///
   415     SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   416     Value operator[](Key k) const {return m1[k]-m2[k];}
   417   };
   418   
   419   ///Returns a \ref SubMap class
   420 
   421   ///This function just returns a \ref SubMap class.
   422   ///
   423   ///\relates SubMap
   424   template<class M1,class M2> 
   425   inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) 
   426   {
   427     return SubMap<M1,M2>(m1,m2);
   428   }
   429 
   430   ///Product of two maps
   431 
   432   ///This \ref concept::ReadMap "read only map" returns the product of the
   433   ///values returned by the two
   434   ///given
   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.
   437 
   438   template<class M1,class M2> 
   439   class MulMap
   440   {
   441     const M1 &m1;
   442     const M2 &m2;
   443   public:
   444     typedef typename M1::Key Key;
   445     typedef typename M1::Value Value;
   446 
   447     ///Constructor
   448 
   449     ///\e
   450     ///
   451     MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   452     Value operator[](Key k) const {return m1[k]*m2[k];}
   453   };
   454   
   455   ///Returns a \ref MulMap class
   456 
   457   ///This function just returns a \ref MulMap class.
   458   ///\relates MulMap
   459   template<class M1,class M2> 
   460   inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) 
   461   {
   462     return MulMap<M1,M2>(m1,m2);
   463   }
   464  
   465   ///Scale a maps with a constant.
   466 
   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.
   470   ///
   471   ///Actually,
   472   ///\code
   473   ///  ScaleMap<X> sc(x,v);
   474   ///\endcode
   475   ///it is equivalent with
   476   ///\code
   477   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   478   ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
   479   ///\endcode
   480   template<class M> 
   481   class ScaleMap
   482   {
   483     const M &m;
   484     typename M::Value v;
   485   public:
   486     typedef typename M::Key Key;
   487     typedef typename M::Value Value;
   488 
   489     ///Constructor
   490 
   491     ///Constructor
   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;}
   496   };
   497   
   498   ///Returns an \ref ScaleMap class
   499 
   500   ///This function just returns an \ref ScaleMap class.
   501   ///\relates ScaleMap
   502   ///\todo A better name is required.
   503   template<class M> 
   504   inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) 
   505   {
   506     return ScaleMap<M>(m,v);
   507   }
   508 
   509   ///Quotient of two maps
   510 
   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.
   515 
   516   template<class M1,class M2> 
   517   class DivMap
   518   {
   519     const M1 &m1;
   520     const M2 &m2;
   521   public:
   522     typedef typename M1::Key Key;
   523     typedef typename M1::Value Value;
   524 
   525     ///Constructor
   526 
   527     ///\e
   528     ///
   529     DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   530     Value operator[](Key k) const {return m1[k]/m2[k];}
   531   };
   532   
   533   ///Returns a \ref DivMap class
   534 
   535   ///This function just returns a \ref DivMap class.
   536   ///\relates DivMap
   537   template<class M1,class M2> 
   538   inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) 
   539   {
   540     return DivMap<M1,M2>(m1,m2);
   541   }
   542   
   543   ///Composition of two maps
   544 
   545   ///This \ref concept::ReadMap "read only map" returns the composition of
   546   ///two
   547   ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
   548   ///of \c M2,
   549   ///then for
   550   ///\code
   551   ///  ComposeMap<M1,M2> cm(m1,m2);
   552   ///\endcode
   553   /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
   554   ///
   555   ///Its \c Key is inherited from \c M2 and its \c Value is from
   556   ///\c M1.
   557   ///The \c M2::Value must be convertible to \c M1::Key.
   558   ///\todo Check the requirements.
   559 
   560   template<class M1,class M2> 
   561   class ComposeMap
   562   {
   563     const M1 &m1;
   564     const M2 &m2;
   565   public:
   566     typedef typename M2::Key Key;
   567     typedef typename M1::Value Value;
   568 
   569     ///Constructor
   570 
   571     ///\e
   572     ///
   573     ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   574     Value operator[](Key k) const {return m1[m2[k]];}
   575   };
   576   ///Returns a \ref ComposeMap class
   577 
   578   ///This function just returns a \ref ComposeMap class.
   579   ///
   580   ///\relates ComposeMap
   581   template<class M1,class M2> 
   582   inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) 
   583   {
   584     return ComposeMap<M1,M2>(m1,m2);
   585   }
   586   
   587   ///Combine of two maps using an STL (binary) functor.
   588 
   589   ///Combine of two maps using an STL (binary) functor.
   590   ///
   591   ///
   592   ///This \ref concept::ReadMap "read only map" takes to maps and a
   593   ///binary functor and returns the composition of
   594   ///two
   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,
   598   ///then for
   599   ///\code
   600   ///  CombineMap<M1,M2,F,V> cm(m1,m2,f);
   601   ///\endcode
   602   /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
   603   ///
   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
   607   ///to \c V.
   608   ///\todo Check the requirements.
   609 
   610   template<class M1,class M2,class F,class V> 
   611   class CombineMap
   612   {
   613     const M1 &m1;
   614     const M2 &m2;
   615     const F &f;
   616   public:
   617     typedef typename M1::Key Key;
   618     typedef V Value;
   619 
   620     ///Constructor
   621 
   622     ///\e
   623     ///
   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]);}
   627   };
   628   
   629   ///Returns a \ref CombineMap class
   630 
   631   ///This function just returns a \ref CombineMap class.
   632   ///
   633   ///Only the first template parameter (the value type) must be given.
   634   ///
   635   ///For example if \c m1 and \c m2 are both \c double valued maps, then 
   636   ///\code
   637   ///combineMap<double>(m1,m2,std::plus<double>)
   638   ///\endcode
   639   ///is equivalent with
   640   ///\code
   641   ///addMap(m1,m2)
   642   ///\endcode
   643   ///
   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) 
   647   {
   648     return CombineMap<M1,M2,F,V>(m1,m2,f);
   649   }
   650 
   651   ///Negative value of a map
   652 
   653   ///This \ref concept::ReadMap "read only map" returns the negative
   654   ///value of the
   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.
   658 
   659   template<class M> 
   660   class NegMap
   661   {
   662     const M &m;
   663   public:
   664     typedef typename M::Key Key;
   665     typedef typename M::Value Value;
   666 
   667     ///Constructor
   668 
   669     ///\e
   670     ///
   671     NegMap(const M &_m) : m(_m) {};
   672     Value operator[](Key k) const {return -m[k];}
   673   };
   674   
   675   ///Returns a \ref NegMap class
   676 
   677   ///This function just returns a \ref NegMap class.
   678   ///\relates NegMap
   679   template<class M> 
   680   inline NegMap<M> negMap(const M &m) 
   681   {
   682     return NegMap<M>(m);
   683   }
   684 
   685 
   686   ///Absolute value of a map
   687 
   688   ///This \ref concept::ReadMap "read only map" returns the absolute value
   689   ///of the
   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.
   695   ///
   696   ///\bug We need a unified way to handle the situation below:
   697   ///\code
   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);}
   706   ///\endcode
   707   
   708 
   709   template<class M> 
   710   class AbsMap
   711   {
   712     const M &m;
   713   public:
   714     typedef typename M::Key Key;
   715     typedef typename M::Value Value;
   716 
   717     ///Constructor
   718 
   719     ///\e
   720     ///
   721     AbsMap(const M &_m) : m(_m) {};
   722     Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
   723   };
   724   
   725   ///Returns a \ref AbsMap class
   726 
   727   ///This function just returns a \ref AbsMap class.
   728   ///\relates AbsMap
   729   template<class M> 
   730   inline AbsMap<M> absMap(const M &m) 
   731   {
   732     return AbsMap<M>(m);
   733   }
   734 
   735   ///Converts an STL style functor to a a map
   736 
   737   ///This \ref concept::ReadMap "read only map" returns the value
   738   ///of a
   739   ///given map.
   740   ///
   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.
   744   ///
   745   ///Parameter \c F is the type of the used functor.
   746   
   747 
   748   template<class K,class V,class F> 
   749   class FunctorMap
   750   {
   751     const F &f;
   752   public:
   753     typedef K Key;
   754     typedef V Value;
   755 
   756     ///Constructor
   757 
   758     ///\e
   759     ///
   760     FunctorMap(const F &_f) : f(_f) {};
   761     Value operator[](Key k) const {return f(k);}
   762   };
   763   
   764   ///Returns a \ref FunctorMap class
   765 
   766   ///This function just returns a \ref FunctorMap class.
   767   ///
   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) 
   772   {
   773     return FunctorMap<K,V,F>(f);
   774   }
   775 
   776   ///Converts a map to an STL style (unary) functor
   777 
   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.
   780   ///
   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.
   784 
   785   template<class M> 
   786   class MapFunctor
   787   {
   788     const M &m;
   789   public:
   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;
   794 
   795     ///Constructor
   796 
   797     ///\e
   798     ///
   799     MapFunctor(const M &_m) : m(_m) {};
   800     ///Returns a value of the map
   801     
   802     ///\e
   803     ///
   804     Value operator()(Key k) const {return m[k];}
   805     ///\e
   806     ///
   807     Value operator[](Key k) const {return m[k];}
   808   };
   809   
   810   ///Returns a \ref MapFunctor class
   811 
   812   ///This function just returns a \ref MapFunctor class.
   813   ///\relates MapFunctor
   814   template<class M> 
   815   inline MapFunctor<M> mapFunctor(const M &m) 
   816   {
   817     return MapFunctor<M>(m);
   818   }
   819 
   820 
   821   ///Apply all map setting operations to two maps
   822 
   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.
   828   ///
   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.
   831 
   832   template<class M1,class M2> 
   833   class ForkMap
   834   {
   835     const M1 &m1;
   836     const M2 &m2;
   837   public:
   838     typedef typename M1::Key Key;
   839     typedef typename M1::Value Value;
   840 
   841     ///Constructor
   842 
   843     ///\e
   844     ///
   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);}
   848   };
   849   
   850   ///Returns an \ref ForkMap class
   851 
   852   ///This function just returns an \ref ForkMap class.
   853   ///\todo How to call these type of functions?
   854   ///
   855   ///\relates ForkMap
   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) 
   859   {
   860     return ForkMap<M1,M2>(m1,m2);
   861   }
   862 
   863   /// @}
   864   
   865 }
   866 
   867 
   868 #endif // LEMON_MAPS_H