[Lemon-commits] [lemon_svn] deba: r2671 - in hugo/trunk: lemon test
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:54:22 CET 2006
Author: deba
Date: Mon Apr 3 18:03:37 2006
New Revision: 2671
Modified:
hugo/trunk/lemon/maps.h
hugo/trunk/test/maps_test.cc
Log:
Writeable extension of some maps
Modified: hugo/trunk/lemon/maps.h
==============================================================================
--- hugo/trunk/lemon/maps.h (original)
+++ hugo/trunk/lemon/maps.h Mon Apr 3 18:03:37 2006
@@ -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<X> sh(x,v);
+ ///\endcode
+ ///is equivalent with
+ ///\code
+ /// ConstMap<X::Key, X::Value> c_tmp(v);
+ /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
+ ///\endcode
+ template<typename M, typename C = typename M::Value>
+ class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
+ M& m;
+ C v;
+ public:
+ typedef MapBase<typename M::Key, typename M::Value> 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, C>(m,v);
}
+ template<typename M, typename C>
+ inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
+ return ShiftWriteMap<M, C>(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<typename M, typename C = typename M::Value>
+ class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
+ M& m;
+ C v;
+ public:
+ typedef MapBase<typename M::Key, typename M::Value> 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, C>(m,v);
}
+ template<typename M, typename C>
+ inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
+ return ScaleWriteMap<M, C>(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<typename M>
+ class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
+ M& m;
+ public:
+ typedef MapBase<typename M::Key, typename M::Value> 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>(m);
}
+ template <typename M>
+ inline NegWriteMap<M> negMap(M &m) {
+ return NegWriteMap<M>(m);
+ }
///Absolute value of a map
@@ -787,6 +881,29 @@
return MapFunctor<M>(m);
}
+ ///Applies all map setting operations to two maps
+
+ ///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.
+
+ template<typename M1, typename M2>
+ class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
+ const M1& m1;
+ const M2& m2;
+ public:
+ typedef MapBase<typename M1::Key, typename M1::Value> Parent;
+ typedef typename Parent::Key Key;
+ typedef typename Parent::Value Value;
+
+ ///Constructor
+ ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
+ Value operator[](Key k) const {return m1[k];}
+ };
+
///Applies all map setting operations to two maps
@@ -800,18 +917,18 @@
///The \c Key and \c Value of M2 must be convertible from those of \c M1.
template<typename M1, typename M2>
- class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
+ class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
+ M1& m1;
+ M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
- ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
+ 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);}
+ 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 <typename M1, typename M2>
- inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
+ inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
return ForkMap<M1, M2>(m1,m2);
}
+ template <typename M1, typename M2>
+ inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
+ return ForkWriteMap<M1, M2>(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 <tt>bool</tt>.
+ template <typename M>
+ class NotWriteMap : public MapBase<typename M::Key, bool> {
+ M& m;
+ public:
+ typedef MapBase<typename M::Key, bool> 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>(m);
}
+ template <typename M>
+ inline NotWriteMap<M> notMap(M &m) {
+ return NotWriteMap<M>(m);
+ }
+
/// \brief Writable bool map for store each true assigned elements.
///
/// Writable bool map for store each true assigned elements. It will
Modified: hugo/trunk/test/maps_test.cc
==============================================================================
--- hugo/trunk/test/maps_test.cc (original)
+++ hugo/trunk/test/maps_test.cc Mon Apr 3 18:03:37 2006
@@ -16,6 +16,9 @@
*
*/
+#include <deque>
+#include <set>
+
#include <lemon/concept_check.h>
#include <lemon/concept/maps.h>
#include <lemon/maps.h>
@@ -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<A,double> DoubleMap;
+typedef ReadWriteMap<A, double> WriteDoubleMap;
+
+typedef ReadMap<A,bool> BoolMap;
+typedef ReadWriteMap<A, bool> BoolWriteMap;
int main()
{ // checking graph components
@@ -55,14 +63,30 @@
checkConcept<ReadMap<A,double>, MulMap<DoubleMap,DoubleMap> >();
checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
+ checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
+ checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >();
+ checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >();
+ checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >();
+ checkConcept<ReadWriteMap<A,double>,
+ ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >();
checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
+ checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
+ checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
+
+ checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
+ checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
+ checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
+ checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
+ checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
+ checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
+
int a;
a=mapFunctor(constMap<A,int>(2))(A());
More information about the Lemon-commits
mailing list