COIN-OR::LEMON - Graph Library

Changeset 2032:18c08f9129e4 in lemon-0.x


Ignore:
Timestamp:
04/03/06 18:03:37 (14 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2671
Message:

Writeable extension of some maps

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lemon/maps.h

    r1993 r2032  
    352352    Value operator[](Key k) const {return m[k] + v;}
    353353  };
     354
     355  ///Shift a map with a constant.
     356
     357  ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the
     358  ///given map and a constant value. It makes also possible to write the map.
     359  ///Its \c Key and \c Value is inherited from \c M.
     360  ///
     361  ///Actually,
     362  ///\code
     363  ///  ShiftMap<X> sh(x,v);
     364  ///\endcode
     365  ///is equivalent with
     366  ///\code
     367  ///  ConstMap<X::Key, X::Value> c_tmp(v);
     368  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
     369  ///\endcode
     370  template<typename M, typename C = typename M::Value>
     371  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
     372    M& m;
     373    C v;
     374  public:
     375    typedef MapBase<typename M::Key, typename M::Value> Parent;
     376    typedef typename Parent::Key Key;
     377    typedef typename Parent::Value Value;
     378
     379    ///Constructor
     380
     381    ///Constructor
     382    ///\param _m is the undelying map
     383    ///\param _v is the shift value
     384    ShiftWriteMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
     385    Value operator[](Key k) const {return m[k] + v;}
     386    void set(Key k, const Value& c) { m.set(k, c - v); }
     387  };
    354388 
    355389  ///Returns an \ref ShiftMap class
     
    361395  inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
    362396    return ShiftMap<M, C>(m,v);
     397  }
     398
     399  template<typename M, typename C>
     400  inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
     401    return ShiftWriteMap<M, C>(m,v);
    363402  }
    364403
     
    457496    Value operator[](Key k) const {return v * m[k];}
    458497  };
     498
     499  ///Scales a maps with a constant.
     500
     501  ///This \ref concept::ReadWriteMap "read-write map" returns the value of the
     502  ///given map multiplied from the left side with a constant value. It can
     503  ///be used as write map also if the given multiplier is not zero.
     504  ///Its \c Key and \c Value is inherited from \c M.
     505  template<typename M, typename C = typename M::Value>
     506  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
     507    M& m;
     508    C v;
     509  public:
     510    typedef MapBase<typename M::Key, typename M::Value> Parent;
     511    typedef typename Parent::Key Key;
     512    typedef typename Parent::Value Value;
     513
     514    ///Constructor
     515
     516    ///Constructor
     517    ///\param _m is the undelying map
     518    ///\param _v is the scaling value
     519    ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
     520    Value operator[](Key k) const {return v * m[k];}
     521    void set(Key k, const Value& c) { m.set(k, c / v);}
     522  };
    459523 
    460524  ///Returns an \ref ScaleMap class
     
    466530  inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
    467531    return ScaleMap<M, C>(m,v);
     532  }
     533
     534  template<typename M, typename C>
     535  inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
     536    return ScaleWriteMap<M, C>(m,v);
    468537  }
    469538
     
    636705  };
    637706 
     707  ///Negative value of a map
     708
     709  ///This \ref concept::ReadWriteMap "read-write map" returns the negative
     710  ///value of the value returned by the
     711  ///given map. Its \c Key and \c Value will be inherited from \c M.
     712  ///The unary \c - operator must be defined for \c Value, of course.
     713
     714  template<typename M>
     715  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
     716    M& m;
     717  public:
     718    typedef MapBase<typename M::Key, typename M::Value> Parent;
     719    typedef typename Parent::Key Key;
     720    typedef typename Parent::Value Value;
     721
     722    ///Constructor
     723    NegWriteMap(M &_m) : m(_m) {};
     724    Value operator[](Key k) const {return -m[k];}
     725    void set(Key k, const Value& v) { m.set(k, -v); }
     726  };
     727
    638728  ///Returns a \ref NegMap class
    639729
     
    645735  }
    646736
     737  template <typename M>
     738  inline NegWriteMap<M> negMap(M &m) {
     739    return NegWriteMap<M>(m);
     740  }
    647741
    648742  ///Absolute value of a map
     
    788882  }
    789883
     884  ///Applies all map setting operations to two maps
     885
     886  ///This map has two \ref concept::ReadMap "readable map"
     887  ///parameters and each read request will be passed just to the
     888  ///first map. This class is the just readable map type of the ForkWriteMap.
     889  ///
     890  ///The \c Key and \c Value will be inherited from \c M1.
     891  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
     892
     893  template<typename  M1, typename M2>
     894  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
     895    const M1& m1;
     896    const M2& m2;
     897  public:
     898    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
     899    typedef typename Parent::Key Key;
     900    typedef typename Parent::Value Value;
     901
     902    ///Constructor
     903    ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
     904    Value operator[](Key k) const {return m1[k];}
     905  };
     906
    790907
    791908  ///Applies all map setting operations to two maps
     
    801918
    802919  template<typename  M1, typename M2>
    803   class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
    804     const M1& m1;
    805     const M2& m2;
     920  class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
     921    M1& m1;
     922    M2& m2;
    806923  public:
    807924    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
     
    810927
    811928    ///Constructor
    812     ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
     929    ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
    813930    Value operator[](Key k) const {return m1[k];}
    814     //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
     931    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
    815932  };
    816933 
     
    823940  ///\todo Wrong scope in Doxygen when \c \\relates is used
    824941  template <typename M1, typename M2>
    825   inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
     942  inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
    826943    return ForkMap<M1, M2>(m1,m2);
     944  }
     945
     946  template <typename M1, typename M2>
     947  inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
     948    return ForkWriteMap<M1, M2>(m1,m2);
    827949  }
    828950
     
    851973    Value operator[](Key k) const {return !m[k];}
    852974  };
     975
     976  ///Logical 'not' of a map with writing possibility
     977 
     978  ///This bool \ref concept::ReadWriteMap "read-write map" returns the
     979  ///logical negation of value returned by the given map. It is setted
     980  ///then the negation of the value be setted to the original map.
     981  ///Its \c Key and will be inherited from \c M,
     982  ///its Value is <tt>bool</tt>.
     983  template <typename M>
     984  class NotWriteMap : public MapBase<typename M::Key, bool> {
     985    M& m;
     986  public:
     987    typedef MapBase<typename M::Key, bool> Parent;
     988    typedef typename Parent::Key Key;
     989    typedef typename Parent::Value Value;
     990
     991    /// Constructor
     992    NotWriteMap(M &_m) : m(_m) {};
     993    Value operator[](Key k) const {return !m[k];}
     994    void set(Key k, bool v) { m.set(k, !v); }
     995  };
    853996 
    854997  ///Returns a \ref NotMap class
     
    8611004  }
    8621005
     1006  template <typename M>
     1007  inline NotWriteMap<M> notMap(M &m) {
     1008    return NotWriteMap<M>(m);
     1009  }
     1010
    8631011  /// \brief Writable bool map for store each true assigned elements.
    8641012  ///
  • test/maps_test.cc

    r1956 r2032  
    1717 */
    1818
     19#include <deque>
     20#include <set>
     21
    1922#include <lemon/concept_check.h>
    2023#include <lemon/concept/maps.h>
     
    2730
    2831struct A {};
     32inline bool operator<(A, A) { return true; }
    2933struct B {};
    3034
     
    4246
    4347typedef ReadMap<A,double> DoubleMap;
     48typedef ReadWriteMap<A, double> WriteDoubleMap;
     49
     50typedef ReadMap<A,bool> BoolMap;
     51typedef ReadWriteMap<A, bool> BoolWriteMap;
    4452
    4553int main()
     
    5664  checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
    5765  checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
     66  checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
    5867  checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
    5968  checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
     69  checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
    6070  checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >();
     71  checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >();
     72  checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >();
     73  checkConcept<ReadWriteMap<A,double>,
     74    ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >();
    6175 
    6276  checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
    6377
    6478  checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
     79
     80  checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
     81  checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
     82
     83  checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
     84  checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
     85  checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
     86  checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
     87  checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
     88  checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
    6589
    6690  int a;
Note: See TracChangeset for help on using the changeset viewer.