# HG changeset patch # User alpar # Date 1105534290 0 # Node ID 67a115cdade4475f67f54795a5cd40f6c45e7840 # Parent 789bad021e2d2edfff2333aed76f74b57ef2a688 functor->map and map->functor converters added. diff -r 789bad021e2d -r 67a115cdade4 src/lemon/maps.h --- a/src/lemon/maps.h Tue Jan 11 17:16:29 2005 +0000 +++ b/src/lemon/maps.h Wed Jan 12 12:51:30 2005 +0000 @@ -98,6 +98,17 @@ ConstMap(const ConstMap &, const T &_v) : v(_v) {} }; + ///Returns a \ref ConstMap class + + ///This function just returns a \ref ConstMap class. + ///\relates ConstMap + template + inline ConstMap constMap(const K &k) + { + return ConstMap(k); + } + + //to document later template struct Const { }; @@ -532,6 +543,89 @@ return AbsMap(m); } + ///Converts an STL style functor to a a map + + ///This \ref concept::ReadMap "read only map" returns the value + ///of a + ///given map. + /// + ///Template parameters \c K and \c V will become its + ///\c Key and \c Value. They must be given explicitely + ///because a functor does not provide such typedefs. + /// + ///Parameter \c F is the type of the used functor. + + + template + class FunctorMap + { + const F &f; + public: + typedef K Key; + typedef V Value; + + ///Constructor + + ///\e + /// + FunctorMap(const F &_f) : f(_f) {}; + Value operator[](Key k) const {return f(k);} + }; + + ///Returns a \ref FunctorMap class + + ///This function just returns a \ref FunctorMap class. + /// + ///The third template parameter isn't necessary to be given. + ///\relates FunctorMap + template + inline FunctorMap functorMap(const F &f) + { + return FunctorMap(f); + } + + ///Converts a map to an STL style functor + + ///This class Converts a map to an STL style functor. + ///that is it provides an operator() to read its values. + /// + ///For the sake of convenience it also works as a ususal map, i.e + ///operator[] and the \c Key and \c Valu typedefs also exist. + + template + class MapFunctor + { + const M &m; + public: + typedef typename M::Key Key; + typedef typename M::Value Value; + + ///Constructor + + ///\e + /// + MapFunctor(const M &_m) : m(_m) {}; + ///Returns a value of the map + + ///\e + /// + Value operator()(Key k) const {return m[k];} + ///\e + /// + Value operator[](Key k) const {return m[k];} + }; + + ///Returns a \ref MapFunctor class + + ///This function just returns a \ref MapFunctor class. + ///\relates MapFunctor + template + inline MapFunctor mapFunctor(const M &m) + { + return MapFunctor(m); + } + + /// @} } diff -r 789bad021e2d -r 67a115cdade4 src/test/maps_test.cc --- a/src/test/maps_test.cc Tue Jan 11 17:16:29 2005 +0000 +++ b/src/test/maps_test.cc Wed Jan 12 12:51:30 2005 +0000 @@ -9,6 +9,13 @@ struct A {}; struct B {}; +class F +{ +public: + B operator()(const A &a) const {return B();} +}; + +int func(A a) {return 3;} typedef ReadMap DoubleMap; @@ -31,6 +38,19 @@ checkConcept, ComposeMap > >(); + checkConcept, FunctorMap >(); + + int a; + + a=mapFunctor(constMap(2))(A()); + check(a==2,"Something is wrong with mapFunctor"); + + B b; + b=functorMap(F())[A()]; + + a=functorMap(&func)[A()]; + check(a==3,"Something is wrong with functorMap"); + std::cout << __FILE__ ": All tests passed.\n"; return 0;