COIN-OR::LEMON - Graph Library

Changeset 1076:67a115cdade4 in lemon-0.x for src


Ignore:
Timestamp:
01/12/05 13:51:30 (19 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1472
Message:

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

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/lemon/maps.h

    r1070 r1076  
    9999  };
    100100
     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
    101112  //to document later
    102113  template<typename T, T v>
     
    533544  }
    534545
     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
    535629  /// @}
    536630 
  • src/test/maps_test.cc

    r1070 r1076  
    1010struct A {};
    1111struct B {};
     12class F
     13{
     14public:
     15  B operator()(const A &a) const {return B();}
     16};
     17
     18int func(A a) {return 3;}
    1219
    1320typedef ReadMap<A,double> DoubleMap;
     
    3239  checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
    3340
     41  checkConcept<ReadMap<A,B>, FunctorMap<A,B,F> >();
     42
     43  int a;
     44 
     45  a=mapFunctor(constMap<A,int>(2))(A());
     46  check(a==2,"Something is wrong with mapFunctor");
     47
     48  B b;
     49  b=functorMap<A,B>(F())[A()];
     50
     51  a=functorMap<A,int>(&func)[A()];
     52  check(a==3,"Something is wrong with functorMap");
     53
    3454  std::cout << __FILE__ ": All tests passed.\n";
    3555 
Note: See TracChangeset for help on using the changeset viewer.