[Lemon-commits] [lemon_svn] alpar: r1472 - in hugo/trunk/src: lemon test

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:45:43 CET 2006


Author: alpar
Date: Wed Jan 12 13:51:30 2005
New Revision: 1472

Modified:
   hugo/trunk/src/lemon/maps.h
   hugo/trunk/src/test/maps_test.cc

Log:
functor->map and map->functor converters added.

Modified: hugo/trunk/src/lemon/maps.h
==============================================================================
--- hugo/trunk/src/lemon/maps.h	(original)
+++ hugo/trunk/src/lemon/maps.h	Wed Jan 12 13:51:30 2005
@@ -98,6 +98,17 @@
     ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
   };
 
+  ///Returns a \ref ConstMap class
+
+  ///This function just returns a \ref ConstMap class.
+  ///\relates ConstMap
+  template<class V,class K> 
+  inline ConstMap<V,K> constMap(const K &k) 
+  {
+    return ConstMap<V,K>(k);
+  }
+
+
   //to document later
   template<typename T, T v>
   struct Const { };
@@ -532,6 +543,89 @@
     return AbsMap<M>(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 K,class V,class F> 
+  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<class K,class V, class F>
+  inline FunctorMap<K,V,F> functorMap(const F &f) 
+  {
+    return FunctorMap<K,V,F>(f);
+  }
+
+  ///Converts a map to an STL style functor
+
+  ///This class Converts a map to an STL style functor.
+  ///that is it provides an <tt>operator()</tt> to read its values.
+  ///
+  ///For the sake of convenience it also works as a ususal map, i.e
+  ///<tt>operator[]</tt> and the \c Key and \c Valu typedefs also exist.
+
+  template<class M> 
+  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<class M> 
+  inline MapFunctor<M> mapFunctor(const M &m) 
+  {
+    return MapFunctor<M>(m);
+  }
+
+
   /// @}
   
 }

Modified: hugo/trunk/src/test/maps_test.cc
==============================================================================
--- hugo/trunk/src/test/maps_test.cc	(original)
+++ hugo/trunk/src/test/maps_test.cc	Wed Jan 12 13:51:30 2005
@@ -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<A,double> DoubleMap;
 
@@ -31,6 +38,19 @@
   
   checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
 
+  checkConcept<ReadMap<A,B>, FunctorMap<A,B,F> >();
+
+  int a;
+  
+  a=mapFunctor(constMap<A,int>(2))(A());
+  check(a==2,"Something is wrong with mapFunctor");
+
+  B b;
+  b=functorMap<A,B>(F())[A()];
+
+  a=functorMap<A,int>(&func)[A()];
+  check(a==3,"Something is wrong with functorMap");
+
   std::cout << __FILE__ ": All tests passed.\n";
   
   return 0;



More information about the Lemon-commits mailing list