src/lemon/maps.h
changeset 1162 2f51fccbc261
parent 1070 6aa1520a0f2f
child 1164 80bb73097736
equal deleted inserted replaced
6:f814174df912 7:4cffddbd7fb4
    96 
    96 
    97     template<typename T1>
    97     template<typename T1>
    98     ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
    98     ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
    99   };
    99   };
   100 
   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 
   101   //to document later
   112   //to document later
   102   template<typename T, T v>
   113   template<typename T, T v>
   103   struct Const { };
   114   struct Const { };
   104   //to document later
   115   //to document later
   105   template<typename K, typename V, V v>
   116   template<typename K, typename V, V v>
   530   inline AbsMap<M> absMap(const M &m) 
   541   inline AbsMap<M> absMap(const M &m) 
   531   {
   542   {
   532     return AbsMap<M>(m);
   543     return AbsMap<M>(m);
   533   }
   544   }
   534 
   545 
       
   546   ///Converts an STL style functor to a a map
       
   547 
       
   548   ///This \ref concept::ReadMap "read only map" returns the value
       
   549   ///of a
       
   550   ///given map.
       
   551   ///
       
   552   ///Template parameters \c K and \c V will become its
       
   553   ///\c Key and \c Value. They must be given explicitely
       
   554   ///because a functor does not provide such typedefs.
       
   555   ///
       
   556   ///Parameter \c F is the type of the used functor.
       
   557   
       
   558 
       
   559   template<class K,class V,class F> 
       
   560   class FunctorMap
       
   561   {
       
   562     const F &f;
       
   563   public:
       
   564     typedef K Key;
       
   565     typedef V Value;
       
   566 
       
   567     ///Constructor
       
   568 
       
   569     ///\e
       
   570     ///
       
   571     FunctorMap(const F &_f) : f(_f) {};
       
   572     Value operator[](Key k) const {return f(k);}
       
   573   };
       
   574   
       
   575   ///Returns a \ref FunctorMap class
       
   576 
       
   577   ///This function just returns a \ref FunctorMap class.
       
   578   ///
       
   579   ///The third template parameter isn't necessary to be given.
       
   580   ///\relates FunctorMap
       
   581   template<class K,class V, class F>
       
   582   inline FunctorMap<K,V,F> functorMap(const F &f) 
       
   583   {
       
   584     return FunctorMap<K,V,F>(f);
       
   585   }
       
   586 
       
   587   ///Converts a map to an STL style functor
       
   588 
       
   589   ///This class Converts a map to an STL style functor.
       
   590   ///that is it provides an <tt>operator()</tt> to read its values.
       
   591   ///
       
   592   ///For the sake of convenience it also works as a ususal map, i.e
       
   593   ///<tt>operator[]</tt> and the \c Key and \c Valu typedefs also exist.
       
   594 
       
   595   template<class M> 
       
   596   class MapFunctor
       
   597   {
       
   598     const M &m;
       
   599   public:
       
   600     typedef typename M::Key Key;
       
   601     typedef typename M::Value Value;
       
   602 
       
   603     ///Constructor
       
   604 
       
   605     ///\e
       
   606     ///
       
   607     MapFunctor(const M &_m) : m(_m) {};
       
   608     ///Returns a value of the map
       
   609     
       
   610     ///\e
       
   611     ///
       
   612     Value operator()(Key k) const {return m[k];}
       
   613     ///\e
       
   614     ///
       
   615     Value operator[](Key k) const {return m[k];}
       
   616   };
       
   617   
       
   618   ///Returns a \ref MapFunctor class
       
   619 
       
   620   ///This function just returns a \ref MapFunctor class.
       
   621   ///\relates MapFunctor
       
   622   template<class M> 
       
   623   inline MapFunctor<M> mapFunctor(const M &m) 
       
   624   {
       
   625     return MapFunctor<M>(m);
       
   626   }
       
   627 
       
   628 
   535   /// @}
   629   /// @}
   536   
   630   
   537 }
   631 }
   538 
   632 
   539 
   633