functor->map and map->functor converters added.
authoralpar
Wed, 12 Jan 2005 12:51:30 +0000
changeset 107667a115cdade4
parent 1075 789bad021e2d
child 1077 784a8bc07316
functor->map and map->functor converters added.
src/lemon/maps.h
src/test/maps_test.cc
     1.1 --- a/src/lemon/maps.h	Tue Jan 11 17:16:29 2005 +0000
     1.2 +++ b/src/lemon/maps.h	Wed Jan 12 12:51:30 2005 +0000
     1.3 @@ -98,6 +98,17 @@
     1.4      ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
     1.5    };
     1.6  
     1.7 +  ///Returns a \ref ConstMap class
     1.8 +
     1.9 +  ///This function just returns a \ref ConstMap class.
    1.10 +  ///\relates ConstMap
    1.11 +  template<class V,class K> 
    1.12 +  inline ConstMap<V,K> constMap(const K &k) 
    1.13 +  {
    1.14 +    return ConstMap<V,K>(k);
    1.15 +  }
    1.16 +
    1.17 +
    1.18    //to document later
    1.19    template<typename T, T v>
    1.20    struct Const { };
    1.21 @@ -532,6 +543,89 @@
    1.22      return AbsMap<M>(m);
    1.23    }
    1.24  
    1.25 +  ///Converts an STL style functor to a a map
    1.26 +
    1.27 +  ///This \ref concept::ReadMap "read only map" returns the value
    1.28 +  ///of a
    1.29 +  ///given map.
    1.30 +  ///
    1.31 +  ///Template parameters \c K and \c V will become its
    1.32 +  ///\c Key and \c Value. They must be given explicitely
    1.33 +  ///because a functor does not provide such typedefs.
    1.34 +  ///
    1.35 +  ///Parameter \c F is the type of the used functor.
    1.36 +  
    1.37 +
    1.38 +  template<class K,class V,class F> 
    1.39 +  class FunctorMap
    1.40 +  {
    1.41 +    const F &f;
    1.42 +  public:
    1.43 +    typedef K Key;
    1.44 +    typedef V Value;
    1.45 +
    1.46 +    ///Constructor
    1.47 +
    1.48 +    ///\e
    1.49 +    ///
    1.50 +    FunctorMap(const F &_f) : f(_f) {};
    1.51 +    Value operator[](Key k) const {return f(k);}
    1.52 +  };
    1.53 +  
    1.54 +  ///Returns a \ref FunctorMap class
    1.55 +
    1.56 +  ///This function just returns a \ref FunctorMap class.
    1.57 +  ///
    1.58 +  ///The third template parameter isn't necessary to be given.
    1.59 +  ///\relates FunctorMap
    1.60 +  template<class K,class V, class F>
    1.61 +  inline FunctorMap<K,V,F> functorMap(const F &f) 
    1.62 +  {
    1.63 +    return FunctorMap<K,V,F>(f);
    1.64 +  }
    1.65 +
    1.66 +  ///Converts a map to an STL style functor
    1.67 +
    1.68 +  ///This class Converts a map to an STL style functor.
    1.69 +  ///that is it provides an <tt>operator()</tt> to read its values.
    1.70 +  ///
    1.71 +  ///For the sake of convenience it also works as a ususal map, i.e
    1.72 +  ///<tt>operator[]</tt> and the \c Key and \c Valu typedefs also exist.
    1.73 +
    1.74 +  template<class M> 
    1.75 +  class MapFunctor
    1.76 +  {
    1.77 +    const M &m;
    1.78 +  public:
    1.79 +    typedef typename M::Key Key;
    1.80 +    typedef typename M::Value Value;
    1.81 +
    1.82 +    ///Constructor
    1.83 +
    1.84 +    ///\e
    1.85 +    ///
    1.86 +    MapFunctor(const M &_m) : m(_m) {};
    1.87 +    ///Returns a value of the map
    1.88 +    
    1.89 +    ///\e
    1.90 +    ///
    1.91 +    Value operator()(Key k) const {return m[k];}
    1.92 +    ///\e
    1.93 +    ///
    1.94 +    Value operator[](Key k) const {return m[k];}
    1.95 +  };
    1.96 +  
    1.97 +  ///Returns a \ref MapFunctor class
    1.98 +
    1.99 +  ///This function just returns a \ref MapFunctor class.
   1.100 +  ///\relates MapFunctor
   1.101 +  template<class M> 
   1.102 +  inline MapFunctor<M> mapFunctor(const M &m) 
   1.103 +  {
   1.104 +    return MapFunctor<M>(m);
   1.105 +  }
   1.106 +
   1.107 +
   1.108    /// @}
   1.109    
   1.110  }
     2.1 --- a/src/test/maps_test.cc	Tue Jan 11 17:16:29 2005 +0000
     2.2 +++ b/src/test/maps_test.cc	Wed Jan 12 12:51:30 2005 +0000
     2.3 @@ -9,6 +9,13 @@
     2.4  
     2.5  struct A {};
     2.6  struct B {};
     2.7 +class F
     2.8 +{
     2.9 +public:
    2.10 +  B operator()(const A &a) const {return B();}
    2.11 +};
    2.12 +
    2.13 +int func(A a) {return 3;}
    2.14  
    2.15  typedef ReadMap<A,double> DoubleMap;
    2.16  
    2.17 @@ -31,6 +38,19 @@
    2.18    
    2.19    checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
    2.20  
    2.21 +  checkConcept<ReadMap<A,B>, FunctorMap<A,B,F> >();
    2.22 +
    2.23 +  int a;
    2.24 +  
    2.25 +  a=mapFunctor(constMap<A,int>(2))(A());
    2.26 +  check(a==2,"Something is wrong with mapFunctor");
    2.27 +
    2.28 +  B b;
    2.29 +  b=functorMap<A,B>(F())[A()];
    2.30 +
    2.31 +  a=functorMap<A,int>(&func)[A()];
    2.32 +  check(a==3,"Something is wrong with functorMap");
    2.33 +
    2.34    std::cout << __FILE__ ": All tests passed.\n";
    2.35    
    2.36    return 0;