# HG changeset patch # User deba # Date 1144080217 0 # Node ID 18c08f9129e461d924a9dc3206e86ad034b76d7b # Parent 080d51024ac534b7f81c0fd5d417233e90b7cec9 Writeable extension of some maps diff -r 080d51024ac5 -r 18c08f9129e4 lemon/maps.h --- a/lemon/maps.h Mon Apr 03 09:45:23 2006 +0000 +++ b/lemon/maps.h Mon Apr 03 16:03:37 2006 +0000 @@ -351,6 +351,40 @@ ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; Value operator[](Key k) const {return m[k] + v;} }; + + ///Shift a map with a constant. + + ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the + ///given map and a constant value. It makes also possible to write the map. + ///Its \c Key and \c Value is inherited from \c M. + /// + ///Actually, + ///\code + /// ShiftMap sh(x,v); + ///\endcode + ///is equivalent with + ///\code + /// ConstMap c_tmp(v); + /// AddMap > sh(x,v); + ///\endcode + template + class ShiftWriteMap : public MapBase { + M& m; + C v; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + ///Constructor + + ///Constructor + ///\param _m is the undelying map + ///\param _v is the shift value + ShiftWriteMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; + Value operator[](Key k) const {return m[k] + v;} + void set(Key k, const Value& c) { m.set(k, c - v); } + }; ///Returns an \ref ShiftMap class @@ -362,6 +396,11 @@ return ShiftMap(m,v); } + template + inline ShiftWriteMap shiftMap(M &m,const C &v) { + return ShiftWriteMap(m,v); + } + ///Difference of two maps ///This \ref concept::ReadMap "read only map" returns the difference @@ -456,6 +495,31 @@ ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; Value operator[](Key k) const {return v * m[k];} }; + + ///Scales a maps with a constant. + + ///This \ref concept::ReadWriteMap "read-write map" returns the value of the + ///given map multiplied from the left side with a constant value. It can + ///be used as write map also if the given multiplier is not zero. + ///Its \c Key and \c Value is inherited from \c M. + template + class ScaleWriteMap : public MapBase { + M& m; + C v; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + ///Constructor + + ///Constructor + ///\param _m is the undelying map + ///\param _v is the scaling value + ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; + Value operator[](Key k) const {return v * m[k];} + void set(Key k, const Value& c) { m.set(k, c / v);} + }; ///Returns an \ref ScaleMap class @@ -467,6 +531,11 @@ return ScaleMap(m,v); } + template + inline ScaleWriteMap scaleMap(M &m,const C &v) { + return ScaleWriteMap(m,v); + } + ///Quotient of two maps ///This \ref concept::ReadMap "read only map" returns the quotient of the @@ -635,6 +704,27 @@ Value operator[](Key k) const {return -m[k];} }; + ///Negative value of a map + + ///This \ref concept::ReadWriteMap "read-write map" returns the negative + ///value of the value returned by the + ///given map. Its \c Key and \c Value will be inherited from \c M. + ///The unary \c - operator must be defined for \c Value, of course. + + template + class NegWriteMap : public MapBase { + M& m; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + ///Constructor + NegWriteMap(M &_m) : m(_m) {}; + Value operator[](Key k) const {return -m[k];} + void set(Key k, const Value& v) { m.set(k, -v); } + }; + ///Returns a \ref NegMap class ///This function just returns a \ref NegMap class. @@ -644,6 +734,10 @@ return NegMap(m); } + template + inline NegWriteMap negMap(M &m) { + return NegWriteMap(m); + } ///Absolute value of a map @@ -787,14 +881,11 @@ return MapFunctor(m); } - ///Applies all map setting operations to two maps - ///This map has two \ref concept::WriteMap "writable map" - ///parameters and each write request will be passed to both of them. - ///If \c M1 is also \ref concept::ReadMap "readable", - ///then the read operations will return the - ///corresponding values of \c M1. + ///This map has two \ref concept::ReadMap "readable map" + ///parameters and each read request will be passed just to the + ///first map. This class is the just readable map type of the ForkWriteMap. /// ///The \c Key and \c Value will be inherited from \c M1. ///The \c Key and \c Value of M2 must be convertible from those of \c M1. @@ -809,9 +900,35 @@ typedef typename Parent::Value Value; ///Constructor - ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; + ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; Value operator[](Key k) const {return m1[k];} - // void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} + }; + + + ///Applies all map setting operations to two maps + + ///This map has two \ref concept::WriteMap "writable map" + ///parameters and each write request will be passed to both of them. + ///If \c M1 is also \ref concept::ReadMap "readable", + ///then the read operations will return the + ///corresponding values of \c M1. + /// + ///The \c Key and \c Value will be inherited from \c M1. + ///The \c Key and \c Value of M2 must be convertible from those of \c M1. + + template + class ForkWriteMap : public MapBase { + M1& m1; + M2& m2; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + ///Constructor + ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; + Value operator[](Key k) const {return m1[k];} + void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} }; ///Returns an \ref ForkMap class @@ -822,10 +939,15 @@ ///\relates ForkMap ///\todo Wrong scope in Doxygen when \c \\relates is used template - inline ForkMap forkMap(const M1 &m1,const M2 &m2) { + inline ForkMap forkMap(const M1 &m1, const M2 &m2) { return ForkMap(m1,m2); } + template + inline ForkWriteMap forkMap(M1 &m1, M2 &m2) { + return ForkWriteMap(m1,m2); + } + /* ************* BOOL MAPS ******************* */ @@ -850,6 +972,27 @@ NotMap(const M &_m) : m(_m) {}; Value operator[](Key k) const {return !m[k];} }; + + ///Logical 'not' of a map with writing possibility + + ///This bool \ref concept::ReadWriteMap "read-write map" returns the + ///logical negation of value returned by the given map. It is setted + ///then the negation of the value be setted to the original map. + ///Its \c Key and will be inherited from \c M, + ///its Value is bool. + template + class NotWriteMap : public MapBase { + M& m; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Constructor + NotWriteMap(M &_m) : m(_m) {}; + Value operator[](Key k) const {return !m[k];} + void set(Key k, bool v) { m.set(k, !v); } + }; ///Returns a \ref NotMap class @@ -860,6 +1003,11 @@ return NotMap(m); } + template + inline NotWriteMap notMap(M &m) { + return NotWriteMap(m); + } + /// \brief Writable bool map for store each true assigned elements. /// /// Writable bool map for store each true assigned elements. It will diff -r 080d51024ac5 -r 18c08f9129e4 test/maps_test.cc --- a/test/maps_test.cc Mon Apr 03 09:45:23 2006 +0000 +++ b/test/maps_test.cc Mon Apr 03 16:03:37 2006 +0000 @@ -16,6 +16,9 @@ * */ +#include +#include + #include #include #include @@ -26,6 +29,7 @@ using namespace lemon::concept; struct A {}; +inline bool operator<(A, A) { return true; } struct B {}; class F { @@ -41,6 +45,10 @@ int binc(int, B) {return 4;} typedef ReadMap DoubleMap; +typedef ReadWriteMap WriteDoubleMap; + +typedef ReadMap BoolMap; +typedef ReadWriteMap BoolWriteMap; int main() { // checking graph components @@ -55,14 +63,30 @@ checkConcept, MulMap >(); checkConcept, DivMap >(); checkConcept, NegMap >(); + checkConcept, NegWriteMap >(); checkConcept, AbsMap >(); checkConcept, ShiftMap >(); + checkConcept, ShiftWriteMap >(); checkConcept, ScaleMap >(); + checkConcept, ScaleWriteMap >(); + checkConcept, ForkMap >(); + checkConcept, + ForkWriteMap >(); checkConcept, ComposeMap > >(); checkConcept, FunctorMap >(); + checkConcept, NotMap >(); + checkConcept, NotWriteMap >(); + + checkConcept, StoreBoolMap >(); + checkConcept, BackInserterBoolMap > >(); + checkConcept, FrontInserterBoolMap > >(); + checkConcept, InserterBoolMap > >(); + checkConcept, FillBoolMap > >(); + checkConcept, SettingOrderBoolMap > >(); + int a; a=mapFunctor(constMap(2))(A());