Writeable extension of some maps
authordeba
Mon, 03 Apr 2006 16:03:37 +0000
changeset 203218c08f9129e4
parent 2031 080d51024ac5
child 2033 7bf1f64962c2
Writeable extension of some maps
lemon/maps.h
test/maps_test.cc
     1.1 --- a/lemon/maps.h	Mon Apr 03 09:45:23 2006 +0000
     1.2 +++ b/lemon/maps.h	Mon Apr 03 16:03:37 2006 +0000
     1.3 @@ -351,6 +351,40 @@
     1.4      ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
     1.5      Value operator[](Key k) const {return m[k] + v;}
     1.6    };
     1.7 +
     1.8 +  ///Shift a map with a constant.
     1.9 +
    1.10 +  ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the
    1.11 +  ///given map and a constant value. It makes also possible to write the map.
    1.12 +  ///Its \c Key and \c Value is inherited from \c M.
    1.13 +  ///
    1.14 +  ///Actually,
    1.15 +  ///\code
    1.16 +  ///  ShiftMap<X> sh(x,v);
    1.17 +  ///\endcode
    1.18 +  ///is equivalent with
    1.19 +  ///\code
    1.20 +  ///  ConstMap<X::Key, X::Value> c_tmp(v);
    1.21 +  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
    1.22 +  ///\endcode
    1.23 +  template<typename M, typename C = typename M::Value> 
    1.24 +  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
    1.25 +    M& m;
    1.26 +    C v;
    1.27 +  public:
    1.28 +    typedef MapBase<typename M::Key, typename M::Value> Parent;
    1.29 +    typedef typename Parent::Key Key;
    1.30 +    typedef typename Parent::Value Value;
    1.31 +
    1.32 +    ///Constructor
    1.33 +
    1.34 +    ///Constructor
    1.35 +    ///\param _m is the undelying map
    1.36 +    ///\param _v is the shift value
    1.37 +    ShiftWriteMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
    1.38 +    Value operator[](Key k) const {return m[k] + v;}
    1.39 +    void set(Key k, const Value& c) { m.set(k, c - v); }
    1.40 +  };
    1.41    
    1.42    ///Returns an \ref ShiftMap class
    1.43  
    1.44 @@ -362,6 +396,11 @@
    1.45      return ShiftMap<M, C>(m,v);
    1.46    }
    1.47  
    1.48 +  template<typename M, typename C> 
    1.49 +  inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
    1.50 +    return ShiftWriteMap<M, C>(m,v);
    1.51 +  }
    1.52 +
    1.53    ///Difference of two maps
    1.54  
    1.55    ///This \ref concept::ReadMap "read only map" returns the difference
    1.56 @@ -456,6 +495,31 @@
    1.57      ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
    1.58      Value operator[](Key k) const {return v * m[k];}
    1.59    };
    1.60 +
    1.61 +  ///Scales a maps with a constant.
    1.62 +
    1.63 +  ///This \ref concept::ReadWriteMap "read-write map" returns the value of the
    1.64 +  ///given map multiplied from the left side with a constant value. It can
    1.65 +  ///be used as write map also if the given multiplier is not zero.
    1.66 +  ///Its \c Key and \c Value is inherited from \c M.
    1.67 +  template<typename M, typename C = typename M::Value> 
    1.68 +  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
    1.69 +    M& m;
    1.70 +    C v;
    1.71 +  public:
    1.72 +    typedef MapBase<typename M::Key, typename M::Value> Parent;
    1.73 +    typedef typename Parent::Key Key;
    1.74 +    typedef typename Parent::Value Value;
    1.75 +
    1.76 +    ///Constructor
    1.77 +
    1.78 +    ///Constructor
    1.79 +    ///\param _m is the undelying map
    1.80 +    ///\param _v is the scaling value
    1.81 +    ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
    1.82 +    Value operator[](Key k) const {return v * m[k];}
    1.83 +    void set(Key k, const Value& c) { m.set(k, c / v);}
    1.84 +  };
    1.85    
    1.86    ///Returns an \ref ScaleMap class
    1.87  
    1.88 @@ -467,6 +531,11 @@
    1.89      return ScaleMap<M, C>(m,v);
    1.90    }
    1.91  
    1.92 +  template<typename M, typename C> 
    1.93 +  inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
    1.94 +    return ScaleWriteMap<M, C>(m,v);
    1.95 +  }
    1.96 +
    1.97    ///Quotient of two maps
    1.98  
    1.99    ///This \ref concept::ReadMap "read only map" returns the quotient of the
   1.100 @@ -635,6 +704,27 @@
   1.101      Value operator[](Key k) const {return -m[k];}
   1.102    };
   1.103    
   1.104 +  ///Negative value of a map
   1.105 +
   1.106 +  ///This \ref concept::ReadWriteMap "read-write map" returns the negative
   1.107 +  ///value of the value returned by the
   1.108 +  ///given map. Its \c Key and \c Value will be inherited from \c M.
   1.109 +  ///The unary \c - operator must be defined for \c Value, of course.
   1.110 +
   1.111 +  template<typename M> 
   1.112 +  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
   1.113 +    M& m;
   1.114 +  public:
   1.115 +    typedef MapBase<typename M::Key, typename M::Value> Parent;
   1.116 +    typedef typename Parent::Key Key;
   1.117 +    typedef typename Parent::Value Value;
   1.118 +
   1.119 +    ///Constructor
   1.120 +    NegWriteMap(M &_m) : m(_m) {};
   1.121 +    Value operator[](Key k) const {return -m[k];}
   1.122 +    void set(Key k, const Value& v) { m.set(k, -v); }
   1.123 +  };
   1.124 +
   1.125    ///Returns a \ref NegMap class
   1.126  
   1.127    ///This function just returns a \ref NegMap class.
   1.128 @@ -644,6 +734,10 @@
   1.129      return NegMap<M>(m);
   1.130    }
   1.131  
   1.132 +  template <typename M> 
   1.133 +  inline NegWriteMap<M> negMap(M &m) {
   1.134 +    return NegWriteMap<M>(m);
   1.135 +  }
   1.136  
   1.137    ///Absolute value of a map
   1.138  
   1.139 @@ -787,14 +881,11 @@
   1.140      return MapFunctor<M>(m);
   1.141    }
   1.142  
   1.143 -
   1.144    ///Applies all map setting operations to two maps
   1.145  
   1.146 -  ///This map has two \ref concept::WriteMap "writable map"
   1.147 -  ///parameters and each write request will be passed to both of them.
   1.148 -  ///If \c M1 is also \ref concept::ReadMap "readable",
   1.149 -  ///then the read operations will return the
   1.150 -  ///corresponding values of \c M1.
   1.151 +  ///This map has two \ref concept::ReadMap "readable map"
   1.152 +  ///parameters and each read request will be passed just to the
   1.153 +  ///first map. This class is the just readable map type of the ForkWriteMap.
   1.154    ///
   1.155    ///The \c Key and \c Value will be inherited from \c M1.
   1.156    ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   1.157 @@ -809,9 +900,35 @@
   1.158      typedef typename Parent::Value Value;
   1.159  
   1.160      ///Constructor
   1.161 -    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   1.162 +    ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
   1.163      Value operator[](Key k) const {return m1[k];}
   1.164 -    //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
   1.165 +  };
   1.166 +
   1.167 +
   1.168 +  ///Applies all map setting operations to two maps
   1.169 +
   1.170 +  ///This map has two \ref concept::WriteMap "writable map"
   1.171 +  ///parameters and each write request will be passed to both of them.
   1.172 +  ///If \c M1 is also \ref concept::ReadMap "readable",
   1.173 +  ///then the read operations will return the
   1.174 +  ///corresponding values of \c M1.
   1.175 +  ///
   1.176 +  ///The \c Key and \c Value will be inherited from \c M1.
   1.177 +  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   1.178 +
   1.179 +  template<typename  M1, typename M2> 
   1.180 +  class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
   1.181 +    M1& m1;
   1.182 +    M2& m2;
   1.183 +  public:
   1.184 +    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
   1.185 +    typedef typename Parent::Key Key;
   1.186 +    typedef typename Parent::Value Value;
   1.187 +
   1.188 +    ///Constructor
   1.189 +    ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
   1.190 +    Value operator[](Key k) const {return m1[k];}
   1.191 +    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
   1.192    };
   1.193    
   1.194    ///Returns an \ref ForkMap class
   1.195 @@ -822,10 +939,15 @@
   1.196    ///\relates ForkMap
   1.197    ///\todo Wrong scope in Doxygen when \c \\relates is used
   1.198    template <typename M1, typename M2> 
   1.199 -  inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
   1.200 +  inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
   1.201      return ForkMap<M1, M2>(m1,m2);
   1.202    }
   1.203  
   1.204 +  template <typename M1, typename M2> 
   1.205 +  inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
   1.206 +    return ForkWriteMap<M1, M2>(m1,m2);
   1.207 +  }
   1.208 +
   1.209  
   1.210    
   1.211    /* ************* BOOL MAPS ******************* */
   1.212 @@ -850,6 +972,27 @@
   1.213      NotMap(const M &_m) : m(_m) {};
   1.214      Value operator[](Key k) const {return !m[k];}
   1.215    };
   1.216 +
   1.217 +  ///Logical 'not' of a map with writing possibility
   1.218 +  
   1.219 +  ///This bool \ref concept::ReadWriteMap "read-write map" returns the 
   1.220 +  ///logical negation of value returned by the given map. It is setted
   1.221 +  ///then the negation of the value be setted to the original map.
   1.222 +  ///Its \c Key and will be inherited from \c M,
   1.223 +  ///its Value is <tt>bool</tt>.
   1.224 +  template <typename M> 
   1.225 +  class NotWriteMap : public MapBase<typename M::Key, bool> {
   1.226 +    M& m;
   1.227 +  public:
   1.228 +    typedef MapBase<typename M::Key, bool> Parent;
   1.229 +    typedef typename Parent::Key Key;
   1.230 +    typedef typename Parent::Value Value;
   1.231 +
   1.232 +    /// Constructor
   1.233 +    NotWriteMap(M &_m) : m(_m) {};
   1.234 +    Value operator[](Key k) const {return !m[k];}
   1.235 +    void set(Key k, bool v) { m.set(k, !v); }
   1.236 +  };
   1.237    
   1.238    ///Returns a \ref NotMap class
   1.239    
   1.240 @@ -860,6 +1003,11 @@
   1.241      return NotMap<M>(m);
   1.242    }
   1.243  
   1.244 +  template <typename M> 
   1.245 +  inline NotWriteMap<M> notMap(M &m) {
   1.246 +    return NotWriteMap<M>(m);
   1.247 +  }
   1.248 +
   1.249    /// \brief Writable bool map for store each true assigned elements.
   1.250    ///
   1.251    /// Writable bool map for store each true assigned elements. It will
     2.1 --- a/test/maps_test.cc	Mon Apr 03 09:45:23 2006 +0000
     2.2 +++ b/test/maps_test.cc	Mon Apr 03 16:03:37 2006 +0000
     2.3 @@ -16,6 +16,9 @@
     2.4   *
     2.5   */
     2.6  
     2.7 +#include <deque>
     2.8 +#include <set>
     2.9 +
    2.10  #include <lemon/concept_check.h>
    2.11  #include <lemon/concept/maps.h>
    2.12  #include <lemon/maps.h>
    2.13 @@ -26,6 +29,7 @@
    2.14  using namespace lemon::concept;
    2.15  
    2.16  struct A {};
    2.17 +inline bool operator<(A, A) { return true; }
    2.18  struct B {};
    2.19  
    2.20  class F {
    2.21 @@ -41,6 +45,10 @@
    2.22  int binc(int, B) {return 4;}
    2.23  
    2.24  typedef ReadMap<A,double> DoubleMap;
    2.25 +typedef ReadWriteMap<A, double> WriteDoubleMap;
    2.26 +
    2.27 +typedef ReadMap<A,bool> BoolMap;
    2.28 +typedef ReadWriteMap<A, bool> BoolWriteMap;
    2.29  
    2.30  int main()
    2.31  { // checking graph components
    2.32 @@ -55,14 +63,30 @@
    2.33    checkConcept<ReadMap<A,double>, MulMap<DoubleMap,DoubleMap> >();
    2.34    checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
    2.35    checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
    2.36 +  checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
    2.37    checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
    2.38    checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
    2.39 +  checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
    2.40    checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >();
    2.41 +  checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >();
    2.42 +  checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >();
    2.43 +  checkConcept<ReadWriteMap<A,double>, 
    2.44 +    ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >();
    2.45    
    2.46    checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
    2.47  
    2.48    checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
    2.49  
    2.50 +  checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
    2.51 +  checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
    2.52 +
    2.53 +  checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
    2.54 +  checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
    2.55 +  checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
    2.56 +  checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
    2.57 +  checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
    2.58 +  checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
    2.59 +
    2.60    int a;
    2.61    
    2.62    a=mapFunctor(constMap<A,int>(2))(A());