lemon/maps.h
author deba
Mon, 03 Oct 2005 10:14:49 +0000
changeset 1695 e6f99fe1723f
parent 1691 6be54bcc14ec
child 1705 3f63d9db307b
permissions -rw-r--r--
Potential difference map
NodeMatrixMap -- Matrix over the nodes
Indicators for common tags
     1 /* -*- C++ -*-
     2  * 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 <lemon/utility.h>
    21 
    22 
    23 ///\file
    24 ///\ingroup maps
    25 ///\brief Miscellaneous property maps
    26 ///
    27 ///\todo This file has the same name as the concept file in concept/,
    28 /// and this is not easily detectable in docs...
    29 
    30 #include <map>
    31 
    32 namespace lemon {
    33 
    34   /// \addtogroup maps
    35   /// @{
    36 
    37   /// Base class of maps.
    38 
    39   /// Base class of maps.
    40   /// It provides the necessary <tt>typedef</tt>s required by the map concept.
    41   template<typename K, typename T, typename _NeedCopy = False>
    42   class MapBase {
    43   public:
    44     /// \e
    45     typedef _NeedCopy NeedCopy;
    46     ///\e
    47     typedef K Key;
    48     ///\e
    49     typedef T Value;
    50   };
    51 
    52   /// Null map. (a.k.a. DoNothingMap)
    53 
    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, typename NC = False>
    58   class NullMap : public MapBase<K, T, NC> {
    59   public:
    60     typedef MapBase<K, T, NC> Parent;
    61     typedef typename Parent::Key Key;
    62     typedef typename Parent::Value Value;
    63     
    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&) {}
    68   };
    69 
    70   template <typename K, typename V> 
    71   NullMap<K, V, True> nullMap() {
    72     return NullMap<K, V, True>();
    73   }
    74 
    75 
    76   /// Constant map.
    77 
    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, typename NC = False>
    82   class ConstMap : public MapBase<K, T, NC> {
    83   private:
    84     T v;
    85   public:
    86 
    87     typedef MapBase<K, T, NC> Parent;
    88     typedef typename Parent::Key Key;
    89     typedef typename Parent::Value Value;
    90 
    91     /// Default constructor
    92 
    93     /// The value of the map will be uninitialized. 
    94     /// (More exactly it will be default constructed.)
    95     ConstMap() {}
    96     ///\e
    97 
    98     /// \param _v The initial value of the map.
    99     ///
   100     ConstMap(const T &_v) : v(_v) {}
   101 
   102     T operator[](const K&) const { return v; }
   103     void set(const K&, const T&) {}
   104 
   105     template<typename T1>
   106     struct rebind {
   107       typedef ConstMap<K, T1> other;
   108     };
   109 
   110     template<typename T1>
   111     ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
   112   };
   113 
   114   ///Returns a \ref ConstMap class
   115 
   116   ///This function just returns a \ref ConstMap class.
   117   ///\relates ConstMap
   118   template<typename K, typename V> 
   119   inline ConstMap<K, V, True> constMap(const V &v) {
   120     return ConstMap<K, V, True>(v);
   121   }
   122 
   123 
   124   //\todo to document later
   125   template<typename T, T v>
   126   struct Const { };
   127 
   128   //\todo to document later
   129   template<typename K, typename V, V v, typename NC>
   130   class ConstMap<K, Const<V, v>, NC > : public MapBase<K, V, NC> {
   131   public:
   132     typedef MapBase<K, V, False> Parent;
   133     typedef typename Parent::Key Key;
   134     typedef typename Parent::Value Value;
   135 
   136     ConstMap() { }
   137     V operator[](const K&) const { return v; }
   138     void set(const K&, const V&) { }
   139   };
   140 
   141   ///Returns a \ref ConstMap class
   142 
   143   ///This function just returns a \ref ConstMap class.
   144   ///\relates ConstMap
   145   template<typename K, typename V, V v> 
   146   inline ConstMap<K, Const<V, v>, True> constMap() {
   147     return ConstMap<K, Const<V, v>, True>();
   148   }
   149 
   150   /// \c std::map wrapper
   151 
   152   /// This is essentially a wrapper for \c std::map. With addition that
   153   /// you can specify a default value different from \c Value() .
   154   ///
   155   /// \todo Provide allocator parameter...
   156   template <typename K, typename T, typename Compare = std::less<K> >
   157   class StdMap : public std::map<K, T, Compare> {
   158     typedef std::map<K, T, Compare> parent;
   159     T v;
   160     typedef typename parent::value_type PairType;
   161 
   162   public:
   163     ///\e
   164     typedef K Key;
   165     ///\e
   166     typedef T Value;
   167     ///\e
   168     typedef T& Reference;
   169     ///\e
   170     typedef const T& ConstReference;
   171 
   172 
   173     StdMap() : v() {}
   174     /// Constructor with specified default value
   175     StdMap(const T& _v) : v(_v) {}
   176 
   177     /// \brief Constructs the map from an appropriate std::map.
   178     ///
   179     /// \warning Inefficient: copies the content of \c m !
   180     StdMap(const parent &m) : parent(m) {}
   181     /// \brief Constructs the map from an appropriate std::map, and explicitly
   182     /// specifies a default value.
   183     ///
   184     /// \warning Inefficient: copies the content of \c m !
   185     StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
   186     
   187     template<typename T1, typename Comp1>
   188     StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) { 
   189       //FIXME; 
   190     }
   191 
   192     Reference operator[](const Key &k) {
   193       return insert(PairType(k,v)).first -> second;
   194     }
   195 
   196     ConstReference operator[](const Key &k) const {
   197       typename parent::iterator i = lower_bound(k);
   198       if (i == parent::end() || parent::key_comp()(k, (*i).first))
   199 	return v;
   200       return (*i).second;
   201     }
   202     void set(const Key &k, const T &t) {
   203       parent::operator[](k) = t;
   204     }
   205 
   206     /// Changes the default value of the map.
   207     /// \return Returns the previous default value.
   208     ///
   209     /// \warning The value of some keys (which has already been queried, but
   210     /// the value has been unchanged from the default) may change!
   211     T setDefault(const T &_v) { T old=v; v=_v; return old; }
   212 
   213     template<typename T1>
   214     struct rebind {
   215       typedef StdMap<Key, T1,Compare> other;
   216     };
   217   };
   218 
   219   /// @}
   220 
   221   /// \addtogroup map_adaptors
   222   /// @{
   223 
   224   /// \brief Identity mapping.
   225   ///
   226   /// This mapping gives back the given key as value without any
   227   /// modification. 
   228   template <typename T, typename NC = False>
   229   class IdentityMap : public MapBase<T, T, NC> {
   230   public:
   231     typedef MapBase<T, T, NC> Parent;
   232     typedef typename Parent::Key Key;
   233     typedef typename Parent::Value Value;
   234 
   235     const T& operator[](const T& t) const {
   236       return t;
   237     }
   238   };
   239 
   240   ///Returns an \ref IdentityMap class
   241 
   242   ///This function just returns an \ref IdentityMap class.
   243   ///\relates IdentityMap
   244   template<typename T>
   245   inline IdentityMap<T, True> identityMap() {
   246     return IdentityMap<T, True>();
   247   }
   248   
   249 
   250   ///Convert the \c Value of a map to another type.
   251 
   252   ///This \ref concept::ReadMap "read only map"
   253   ///converts the \c Value of a maps to type \c T.
   254   ///Its \c Key is inherited from \c M.
   255   template <typename M, typename T, typename NC = False> 
   256   class ConvertMap : public MapBase<typename M::Key, T, NC> {
   257     typename SmartConstReference<M>::Type m;
   258   public:
   259     typedef MapBase<typename M::Key, T, NC> Parent;
   260     typedef typename Parent::Key Key;
   261     typedef typename Parent::Value Value;
   262 
   263     ///Constructor
   264 
   265     ///Constructor
   266     ///\param _m is the underlying map
   267     ConvertMap(const M &_m) : m(_m) {};
   268 
   269     /// \brief The subscript operator.
   270     ///
   271     /// The subscript operator.
   272     /// \param k The key
   273     /// \return The target of the edge 
   274     Value operator[](const Key& k) const {return m[k];}
   275   };
   276   
   277   ///Returns an \ref ConvertMap class
   278 
   279   ///This function just returns an \ref ConvertMap class.
   280   ///\relates ConvertMap
   281   ///\todo The order of the template parameters are changed.
   282   template<typename T, typename M>
   283   inline ConvertMap<M, T, True> convertMap(const M &m) {
   284     return ConvertMap<M, T, True>(m);
   285   }
   286 
   287   ///Sum of two maps
   288 
   289   ///This \ref concept::ReadMap "read only map" returns the sum of the two
   290   ///given maps. Its \c Key and \c Value will be inherited from \c M1.
   291   ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
   292 
   293   template<typename M1, typename M2, typename NC = False> 
   294   class AddMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
   295     typename SmartConstReference<M1>::Type m1;
   296     typename SmartConstReference<M2>::Type m2;
   297 
   298   public:
   299     typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
   300     typedef typename Parent::Key Key;
   301     typedef typename Parent::Value Value;
   302 
   303     ///Constructor
   304     AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   305     Value operator[](Key k) const {return m1[k]+m2[k];}
   306   };
   307   
   308   ///Returns an \ref AddMap class
   309 
   310   ///This function just returns an \ref AddMap class.
   311   ///\todo How to call these type of functions?
   312   ///
   313   ///\relates AddMap
   314   ///\todo Wrong scope in Doxygen when \c \\relates is used
   315   template<typename M1, typename M2> 
   316   inline AddMap<M1, M2, True> addMap(const M1 &m1,const M2 &m2) {
   317     return AddMap<M1, M2, True>(m1,m2);
   318   }
   319 
   320   ///Shift a map with a constant.
   321 
   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.
   325   ///
   326   ///Actually,
   327   ///\code
   328   ///  ShiftMap<X> sh(x,v);
   329   ///\endcode
   330   ///is equivalent with
   331   ///\code
   332   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   333   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   334   ///\endcode
   335   template<typename M, typename C = typename M::Value, typename NC = False> 
   336   class ShiftMap : public MapBase<typename M::Key, typename M::Value, NC> {
   337     typename SmartConstReference<M>::Type m;
   338     C v;
   339   public:
   340     typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
   341     typedef typename Parent::Key Key;
   342     typedef typename Parent::Value Value;
   343 
   344     ///Constructor
   345 
   346     ///Constructor
   347     ///\param _m is the undelying map
   348     ///\param _v is the shift value
   349     ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
   350     Value operator[](Key k) const {return m[k] + v;}
   351   };
   352   
   353   ///Returns an \ref ShiftMap class
   354 
   355   ///This function just returns an \ref ShiftMap class.
   356   ///\relates ShiftMap
   357   ///\todo A better name is required.
   358   template<typename M, typename C> 
   359   inline ShiftMap<M, C, True> shiftMap(const M &m,const C &v) {
   360     return ShiftMap<M, C, True>(m,v);
   361   }
   362 
   363   ///Difference of two maps
   364 
   365   ///This \ref concept::ReadMap "read only map" returns the difference
   366   ///of the values of the two
   367   ///given maps. Its \c Key and \c Value will be inherited from \c M1.
   368   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   369 
   370   template<typename M1, typename M2, typename NC = False> 
   371   class SubMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
   372     typename SmartConstReference<M1>::Type m1;
   373     typename SmartConstReference<M2>::Type m2;
   374   public:
   375     typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
   376     typedef typename Parent::Key Key;
   377     typedef typename Parent::Value Value;
   378 
   379     ///Constructor
   380     SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   381     Value operator[](Key k) const {return m1[k]-m2[k];}
   382   };
   383   
   384   ///Returns a \ref SubMap class
   385 
   386   ///This function just returns a \ref SubMap class.
   387   ///
   388   ///\relates SubMap
   389   template<typename M1, typename M2> 
   390   inline SubMap<M1, M2, True> subMap(const M1 &m1, const M2 &m2) {
   391     return SubMap<M1, M2, True>(m1, m2);
   392   }
   393 
   394   ///Product of two maps
   395 
   396   ///This \ref concept::ReadMap "read only map" returns the product of the
   397   ///values of the two
   398   ///given
   399   ///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<typename M1, typename M2, typename NC = False> 
   403   class MulMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
   404     typename SmartConstReference<M1>::Type m1;
   405     typename SmartConstReference<M2>::Type m2;
   406   public:
   407     typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
   408     typedef typename Parent::Key Key;
   409     typedef typename Parent::Value Value;
   410 
   411     ///Constructor
   412     MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   413     Value operator[](Key k) const {return m1[k]*m2[k];}
   414   };
   415   
   416   ///Returns a \ref MulMap class
   417 
   418   ///This function just returns a \ref MulMap class.
   419   ///\relates MulMap
   420   template<typename M1, typename M2> 
   421   inline MulMap<M1, M2, True> mulMap(const M1 &m1,const M2 &m2) {
   422     return MulMap<M1, M2, True>(m1,m2);
   423   }
   424  
   425   ///Scales a maps with a constant.
   426 
   427   ///This \ref concept::ReadMap "read only map" returns the value of the
   428   ///given map multiplied from the left side with a constant value.
   429   ///Its \c Key and \c Value is inherited from \c M.
   430   ///
   431   ///Actually,
   432   ///\code
   433   ///  ScaleMap<X> sc(x,v);
   434   ///\endcode
   435   ///is equivalent with
   436   ///\code
   437   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   438   ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
   439   ///\endcode
   440   template<typename M, typename C = typename M::Value, typename NC = False> 
   441   class ScaleMap : public MapBase<typename M::Key, typename M::Value, NC> {
   442     typename SmartConstReference<M>::Type m;
   443     C v;
   444   public:
   445     typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
   446     typedef typename Parent::Key Key;
   447     typedef typename Parent::Value Value;
   448 
   449     ///Constructor
   450 
   451     ///Constructor
   452     ///\param _m is the undelying map
   453     ///\param _v is the scaling value
   454     ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
   455     Value operator[](Key k) const {return v * m[k];}
   456   };
   457   
   458   ///Returns an \ref ScaleMap class
   459 
   460   ///This function just returns an \ref ScaleMap class.
   461   ///\relates ScaleMap
   462   ///\todo A better name is required.
   463   template<typename M, typename C> 
   464   inline ScaleMap<M, C, True> scaleMap(const M &m,const C &v) {
   465     return ScaleMap<M, C, True>(m,v);
   466   }
   467 
   468   ///Quotient of two maps
   469 
   470   ///This \ref concept::ReadMap "read only map" returns the quotient of the
   471   ///values of the two
   472   ///given maps. Its \c Key and \c Value will be inherited from \c M1.
   473   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   474 
   475   template<typename M1, typename M2, typename NC = False> 
   476   class DivMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
   477     typename SmartConstReference<M1>::Type m1;
   478     typename SmartConstReference<M2>::Type m2;
   479   public:
   480     typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
   481     typedef typename Parent::Key Key;
   482     typedef typename Parent::Value Value;
   483 
   484     ///Constructor
   485     DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   486     Value operator[](Key k) const {return m1[k]/m2[k];}
   487   };
   488   
   489   ///Returns a \ref DivMap class
   490 
   491   ///This function just returns a \ref DivMap class.
   492   ///\relates DivMap
   493   template<typename M1, typename M2> 
   494   inline DivMap<M1, M2, True> divMap(const M1 &m1,const M2 &m2) {
   495     return DivMap<M1, M2, True>(m1,m2);
   496   }
   497   
   498   ///Composition of two maps
   499 
   500   ///This \ref concept::ReadMap "read only map" returns the composition of
   501   ///two
   502   ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
   503   ///of \c M2,
   504   ///then for
   505   ///\code
   506   ///  ComposeMap<M1, M2> cm(m1,m2);
   507   ///\endcode
   508   /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
   509   ///
   510   ///Its \c Key is inherited from \c M2 and its \c Value is from
   511   ///\c M1.
   512   ///The \c M2::Value must be convertible to \c M1::Key.
   513   ///\todo Check the requirements.
   514 
   515   template <typename M1, typename M2, typename NC = False> 
   516   class ComposeMap : public MapBase<typename M2::Key, typename M1::Value, NC> {
   517     typename SmartConstReference<M1>::Type m1;
   518     typename SmartConstReference<M2>::Type m2;
   519   public:
   520     typedef MapBase<typename M2::Key, typename M1::Value, NC> Parent;
   521     typedef typename Parent::Key Key;
   522     typedef typename Parent::Value Value;
   523 
   524     ///Constructor
   525     ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   526     Value operator[](Key k) const {return m1[m2[k]];}
   527   };
   528   ///Returns a \ref ComposeMap class
   529 
   530   ///This function just returns a \ref ComposeMap class.
   531   ///
   532   ///\relates ComposeMap
   533   template <typename M1, typename M2> 
   534   inline ComposeMap<M1, M2, True> composeMap(const M1 &m1,const M2 &m2) {
   535     return ComposeMap<M1, M2, True>(m1,m2);
   536   }
   537   
   538   ///Combines of two maps using an STL (binary) functor.
   539 
   540   ///Combines of two maps using an STL (binary) functor.
   541   ///
   542   ///
   543   ///This \ref concept::ReadMap "read only map" takes two maps and a
   544   ///binary functor and returns the composition of
   545   ///the two
   546   ///given maps unsing the functor. 
   547   ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
   548   ///and \c f is of \c F,
   549   ///then for
   550   ///\code
   551   ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
   552   ///\endcode
   553   /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
   554   ///
   555   ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
   556   ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
   557   ///input parameter of \c F and the return type of \c F must be convertible
   558   ///to \c V.
   559   ///\todo Check the requirements.
   560 
   561   template<typename M1, typename M2, typename F,
   562 	   typename V = typename F::result_type,
   563 	   typename NC = False> 
   564   class CombineMap : public MapBase<typename M1::Key, V, NC> {
   565     typename SmartConstReference<M1>::Type m1;
   566     typename SmartConstReference<M2>::Type m2;
   567     F f;
   568   public:
   569     typedef MapBase<typename M1::Key, V, NC> Parent;
   570     typedef typename Parent::Key Key;
   571     typedef typename Parent::Value Value;
   572 
   573     ///Constructor
   574     CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
   575       : m1(_m1), m2(_m2), f(_f) {};
   576     Value operator[](Key k) const {return f(m1[k],m2[k]);}
   577   };
   578   
   579   ///Returns a \ref CombineMap class
   580 
   581   ///This function just returns a \ref CombineMap class.
   582   ///
   583   ///Only the first template parameter (the value type) must be given.
   584   ///
   585   ///For example if \c m1 and \c m2 are both \c double valued maps, then 
   586   ///\code
   587   ///combineMap<double>(m1,m2,std::plus<double>)
   588   ///\endcode
   589   ///is equivalent with
   590   ///\code
   591   ///addMap(m1,m2)
   592   ///\endcode
   593   ///
   594   ///\relates CombineMap
   595   template<typename M1, typename M2, typename F, typename V> 
   596   inline CombineMap<M1, M2, F, V, True> 
   597   combineMap(const M1& m1,const M2& m2, const F& f) {
   598     return CombineMap<M1, M2, F, V, True>(m1,m2,f);
   599   }
   600 
   601   template<typename M1, typename M2, typename F> 
   602   inline CombineMap<M1, M2, F, typename F::result_type, True> 
   603   combineMap(const M1& m1, const M2& m2, const F& f) {
   604     return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
   605   }
   606 
   607   template<typename M1, typename M2, typename K1, typename K2, typename V> 
   608   inline CombineMap<M1, M2, V (*)(K1, K2), V, True> 
   609   combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
   610     return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
   611   }
   612 
   613   ///Negative value of a map
   614 
   615   ///This \ref concept::ReadMap "read only map" returns the negative
   616   ///value of the
   617   ///value returned by the
   618   ///given map. Its \c Key and \c Value will be inherited from \c M.
   619   ///The unary \c - operator must be defined for \c Value, of course.
   620 
   621   template<typename M, typename NC = False> 
   622   class NegMap : public MapBase<typename M::Key, typename M::Value, NC> {
   623     typename SmartConstReference<M>::Type m;
   624   public:
   625     typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
   626     typedef typename Parent::Key Key;
   627     typedef typename Parent::Value Value;
   628 
   629     ///Constructor
   630     NegMap(const M &_m) : m(_m) {};
   631     Value operator[](Key k) const {return -m[k];}
   632   };
   633   
   634   ///Returns a \ref NegMap class
   635 
   636   ///This function just returns a \ref NegMap class.
   637   ///\relates NegMap
   638   template <typename M> 
   639   inline NegMap<M, True> negMap(const M &m) {
   640     return NegMap<M, True>(m);
   641   }
   642 
   643 
   644   ///Absolute value of a map
   645 
   646   ///This \ref concept::ReadMap "read only map" returns the absolute value
   647   ///of the
   648   ///value returned by the
   649   ///given map. Its \c Key and \c Value will be inherited
   650   ///from <tt>M</tt>. <tt>Value</tt>
   651   ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
   652   ///operator must be defined for it, of course.
   653   ///
   654   ///\bug We need a unified way to handle the situation below:
   655   ///\code
   656   ///  struct _UnConvertible {};
   657   ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
   658   ///  template<> inline int t_abs<>(int n) {return abs(n);}
   659   ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
   660   ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
   661   ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
   662   ///  template<> inline double t_abs<>(double n) {return fabs(n);}
   663   ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
   664   ///\endcode
   665   
   666 
   667   template<typename M, typename NC = False> 
   668   class AbsMap : public MapBase<typename M::Key, typename M::Value, NC> {
   669     typename SmartConstReference<M>::Type m;
   670   public:
   671     typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
   672     typedef typename Parent::Key Key;
   673     typedef typename Parent::Value Value;
   674 
   675     ///Constructor
   676     AbsMap(const M &_m) : m(_m) {};
   677     Value operator[](Key k) const {
   678       Value tmp = m[k]; 
   679       return tmp >= 0 ? tmp : -tmp;
   680     }
   681 
   682   };
   683   
   684   ///Returns a \ref AbsMap class
   685 
   686   ///This function just returns a \ref AbsMap class.
   687   ///\relates AbsMap
   688   template<typename M> 
   689   inline AbsMap<M, True> absMap(const M &m) {
   690     return AbsMap<M, True>(m);
   691   }
   692 
   693   ///Converts an STL style functor to a map
   694 
   695   ///This \ref concept::ReadMap "read only map" returns the value
   696   ///of a
   697   ///given map.
   698   ///
   699   ///Template parameters \c K and \c V will become its
   700   ///\c Key and \c Value. They must be given explicitely
   701   ///because a functor does not provide such typedefs.
   702   ///
   703   ///Parameter \c F is the type of the used functor.
   704   
   705 
   706   template<typename F, 
   707 	   typename K = typename F::argument_type, 
   708 	   typename V = typename F::result_type,
   709 	   typename NC = False> 
   710   class FunctorMap : public MapBase<K, V, NC> {
   711     F f;
   712   public:
   713     typedef MapBase<K, V, NC> Parent;
   714     typedef typename Parent::Key Key;
   715     typedef typename Parent::Value Value;
   716 
   717     ///Constructor
   718     FunctorMap(const F &_f) : f(_f) {}
   719 
   720     Value operator[](Key k) const { return f(k);}
   721   };
   722   
   723   ///Returns a \ref FunctorMap class
   724 
   725   ///This function just returns a \ref FunctorMap class.
   726   ///
   727   ///The third template parameter isn't necessary to be given.
   728   ///\relates FunctorMap
   729   template<typename K, typename V, typename F> inline 
   730   FunctorMap<F, K, V, True> functorMap(const F &f) {
   731     return FunctorMap<F, K, V, True>(f);
   732   }
   733 
   734   template <typename F> inline 
   735   FunctorMap<F, typename F::argument_type, typename F::result_type, True> 
   736   functorMap(const F &f) {
   737     return FunctorMap<F, typename F::argument_type, 
   738       typename F::result_type, True>(f);
   739   }
   740 
   741   template <typename K, typename V> inline 
   742   FunctorMap<V (*)(K), K, V, True> functorMap(V (*f)(K)) {
   743     return FunctorMap<V (*)(K), K, V, True>(f);
   744   }
   745 
   746 
   747   ///Converts a map to an STL style (unary) functor
   748 
   749   ///This class Converts a map to an STL style (unary) functor.
   750   ///that is it provides an <tt>operator()</tt> to read its values.
   751   ///
   752   ///For the sake of convenience it also works as
   753   ///a ususal \ref concept::ReadMap "readable map",
   754   ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
   755 
   756   template <typename M, typename NC = False> 
   757   class MapFunctor : public MapBase<typename M::Key, typename M::Value, NC> {
   758     typename SmartConstReference<M>::Type m;
   759   public:
   760     typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
   761     typedef typename Parent::Key Key;
   762     typedef typename Parent::Value Value;
   763 
   764     ///\e
   765     typedef typename M::Key argument_type;
   766     ///\e
   767     typedef typename M::Value result_type;
   768 
   769     ///Constructor
   770     MapFunctor(const M &_m) : m(_m) {};
   771     ///Returns a value of the map
   772     Value operator()(Key k) const {return m[k];}
   773     ///\e
   774     Value operator[](Key k) const {return m[k];}
   775   };
   776   
   777   ///Returns a \ref MapFunctor class
   778 
   779   ///This function just returns a \ref MapFunctor class.
   780   ///\relates MapFunctor
   781   template<typename M> 
   782   inline MapFunctor<M, True> mapFunctor(const M &m) {
   783     return MapFunctor<M, True>(m);
   784   }
   785 
   786 
   787   ///Applies all map setting operations to two maps
   788 
   789   ///This map has two \ref concept::WriteMap "writable map"
   790   ///parameters and each write request will be passed to both of them.
   791   ///If \c M1 is also \ref concept::ReadMap "readable",
   792   ///then the read operations will return the
   793   ///corresponding values of \c M1.
   794   ///
   795   ///The \c Key and \c Value will be inherited from \c M1.
   796   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   797 
   798   template<typename  M1, typename M2, typename NC = False> 
   799   class ForkMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
   800     typename SmartConstReference<M1>::Type m1;
   801     typename SmartConstReference<M2>::Type m2;
   802   public:
   803     typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
   804     typedef typename Parent::Key Key;
   805     typedef typename Parent::Value Value;
   806 
   807     ///Constructor
   808     ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   809     Value operator[](Key k) const {return m1[k];}
   810     //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
   811   };
   812   
   813   ///Returns an \ref ForkMap class
   814 
   815   ///This function just returns an \ref ForkMap class.
   816   ///\todo How to call these type of functions?
   817   ///
   818   ///\relates ForkMap
   819   ///\todo Wrong scope in Doxygen when \c \\relates is used
   820   template <typename M1, typename M2> 
   821   inline ForkMap<M1, M2, True> forkMap(const M1 &m1,const M2 &m2) {
   822     return ForkMap<M1, M2, True>(m1,m2);
   823   }
   824 
   825 
   826   
   827   /* ************* BOOL MAPS ******************* */
   828   
   829   ///Logical 'not' of a map
   830   
   831   ///This bool \ref concept::ReadMap "read only map" returns the 
   832   ///logical negation of
   833   ///value returned by the
   834   ///given map. Its \c Key and will be inherited from \c M,
   835   ///its Value is <tt>bool</tt>.
   836 
   837   template <typename M, typename NC = False> 
   838   class NotMap : public MapBase<typename M::Key, bool, NC> {
   839     typename SmartConstReference<M>::Type m;
   840   public:
   841     typedef MapBase<typename M::Key, bool, NC> Parent;
   842     typedef typename Parent::Key Key;
   843     typedef typename Parent::Value Value;
   844 
   845     ///Constructor
   846     NotMap(const M &_m) : m(_m) {};
   847     Value operator[](Key k) const {return !m[k];}
   848   };
   849   
   850   ///Returns a \ref NotMap class
   851   
   852   ///This function just returns a \ref NotMap class.
   853   ///\relates NotMap
   854   template <typename M> 
   855   inline NotMap<M, True> notMap(const M &m) {
   856     return NotMap<M, True>(m);
   857   }
   858 
   859   /// @}
   860 }
   861 
   862 #endif // LEMON_MAPS_H