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