# HG changeset patch # User deba # Date 1126190090 0 # Node ID fa89ffb27a6dafa691b7f79db9f6427430315e3b # Parent 648aa2f33dc8a6db8e092a6b57d9f3dc4f16971d Redesign of the map adaptors. /smart reference handling only used by functions/ Better handling of the function objects and functions. \\\todo May we use operators instead of the addMap, subMap...? diff -r 648aa2f33dc8 -r fa89ffb27a6d lemon/maps.h --- a/lemon/maps.h Thu Sep 01 20:35:30 2005 +0000 +++ b/lemon/maps.h Thu Sep 08 14:34:50 2005 +0000 @@ -39,10 +39,11 @@ /// Base class of maps. /// It provides the necessary typedefs required by the map concept. - template - class MapBase - { + template + class MapBase { public: + /// \e + typedef _NeedCopy NeedCopy; ///\e typedef K Key; ///\e @@ -54,13 +55,13 @@ /// If you have to provide a map only for its type definitions, /// or if you have to provide a writable map, but /// data written to it will sent to /dev/null... - template - class NullMap : public MapBase - { + template + class NullMap : public MapBase { public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; - typedef True NeedCopy; - /// Gives back a default constructed element. T operator[](const K&) const { return T(); } /// Absorbs the value. @@ -68,8 +69,8 @@ }; template - NullMap nullMap() { - return NullMap(); + NullMap nullMap() { + return NullMap(); } @@ -78,13 +79,15 @@ /// This is a readable map which assigns a specified value to each key. /// In other aspects it is equivalent to the \ref NullMap. /// \todo set could be used to set the value. - template - class ConstMap : public MapBase - { + template + class ConstMap : public MapBase { + private: T v; public: - typedef True NeedCopy; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; /// Default constructor @@ -102,37 +105,49 @@ template struct rebind { - typedef ConstMap other; + typedef ConstMap other; }; template - ConstMap(const ConstMap &, const T &_v) : v(_v) {} + ConstMap(const ConstMap &, const T &_v) : v(_v) {} }; ///Returns a \ref ConstMap class ///This function just returns a \ref ConstMap class. ///\relates ConstMap - template - inline ConstMap constMap(const V &v) - { - return ConstMap(v); + template + inline ConstMap constMap(const V &v) { + return ConstMap(v); } //\todo to document later template struct Const { }; + //\todo to document later - template - class ConstMap > : public MapBase - { + template + class ConstMap, NC > : public MapBase { public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + ConstMap() { } V operator[](const K&) const { return v; } void set(const K&, const V&) { } }; + ///Returns a \ref ConstMap class + + ///This function just returns a \ref ConstMap class. + ///\relates ConstMap + template + inline ConstMap, True> constMap() { + return ConstMap, True>(); + } + /// \c std::map wrapper /// This is essentially a wrapper for \c std::map. With addition that @@ -140,8 +155,8 @@ /// /// \todo Provide allocator parameter... template > - class StdMap : public std::map { - typedef std::map parent; + class StdMap : public std::map { + typedef std::map parent; T v; typedef typename parent::value_type PairType; @@ -171,13 +186,14 @@ StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} template - StdMap(const StdMap &m, const T &_v) { + StdMap(const StdMap &m, const T &_v) { //FIXME; } Reference operator[](const Key &k) { return insert(PairType(k,v)).first -> second; } + ConstReference operator[](const Key &k) const { typename parent::iterator i = lower_bound(k); if (i == parent::end() || parent::key_comp()(k, (*i).first)) @@ -197,7 +213,7 @@ template struct rebind { - typedef StdMap other; + typedef StdMap other; }; }; @@ -210,33 +226,40 @@ /// /// This mapping gives back the given key as value without any /// modification. - template - class IdentityMap { + template + class IdentityMap : public MapBase { public: - typedef T Key; - typedef T Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; - const Value& operator[](const Key& t) const { + const T& operator[](const T& t) const { return t; } }; + ///Returns an \ref IdentityMap class + + ///This function just returns an \ref IdentityMap class. + ///\relates IdentityMap + template + inline IdentityMap identityMap() { + return IdentityMap(); + } + + ///Convert the \c Value of a map to another type. ///This \ref concept::ReadMap "read only map" ///converts the \c Value of a maps to type \c T. ///Its \c Key is inherited from \c M. - template - class ConvertMap { + template + class ConvertMap : public MapBase { typename SmartConstReference::Type m; public: - - typedef True NeedCopy; - - ///\e - typedef typename M::Key Key; - ///\e - typedef T Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor @@ -249,7 +272,7 @@ /// The subscript operator. /// \param k The key /// \return The target of the edge - Value operator[](Key k) const {return m[k];} + Value operator[](const Key& k) const {return m[k];} }; ///Returns an \ref ConvertMap class @@ -257,10 +280,9 @@ ///This function just returns an \ref ConvertMap class. ///\relates ConvertMap ///\todo The order of the template parameters are changed. - template - inline ConvertMap convertMap(const M &m) - { - return ConvertMap(m); + template + inline ConvertMap convertMap(const M &m) { + return ConvertMap(m); } ///Sum of two maps @@ -269,20 +291,15 @@ ///given maps. Its \c Key and \c Value will be inherited from \c M1. ///The \c Key and \c Value of M2 must be convertible to those of \c M1. - template - class AddMap - { + template + class AddMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; public: - - typedef True NeedCopy; - - ///\e - typedef typename M1::Key Key; - ///\e - typedef typename M1::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; @@ -296,10 +313,9 @@ /// ///\relates AddMap ///\todo Wrong scope in Doxygen when \c \\relates is used - template - inline AddMap addMap(const M1 &m1,const M2 &m2) - { - return AddMap(m1,m2); + template + inline AddMap addMap(const M1 &m1,const M2 &m2) { + return AddMap(m1,m2); } ///Shift a map with a constant. @@ -317,25 +333,21 @@ /// ConstMap c_tmp(v); /// AddMap > sh(x,v); ///\endcode - template - class ShiftMap - { + template + class ShiftMap : public MapBase { typename SmartConstReference::Type m; typename M::Value v; public: - - typedef True NeedCopy; - ///\e - typedef typename M::Key Key; - ///\e - typedef typename M::Value Value; + 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 - ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; + ShiftMap(const M &_m, const Value &_v ) : m(_m), v(_v) {}; Value operator[](Key k) const {return m[k]+v;} }; @@ -344,10 +356,9 @@ ///This function just returns an \ref ShiftMap class. ///\relates ShiftMap ///\todo A better name is required. - template - inline ShiftMap shiftMap(const M &m,const typename M::Value &v) - { - return ShiftMap(m,v); + template + inline ShiftMap shiftMap(const M &m,const typename M::Value &v) { + return ShiftMap(m,v); } ///Difference of two maps @@ -357,18 +368,14 @@ ///given maps. Its \c Key and \c Value will be inherited from \c M1. ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. - template - class SubMap - { + template + class SubMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; public: - - typedef True NeedCopy; - ///\e - typedef typename M1::Key Key; - ///\e - typedef typename M1::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; @@ -380,10 +387,9 @@ ///This function just returns a \ref SubMap class. /// ///\relates SubMap - template - inline SubMap subMap(const M1 &m1,const M2 &m2) - { - return SubMap(m1,m2); + template + inline SubMap subMap(const M1 &m1, const M2 &m2) { + return SubMap(m1, m2); } ///Product of two maps @@ -394,18 +400,14 @@ ///maps. Its \c Key and \c Value will be inherited from \c M1. ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. - template - class MulMap - { + template + class MulMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; public: - - typedef True NeedCopy; - ///\e - typedef typename M1::Key Key; - ///\e - typedef typename M1::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; @@ -416,10 +418,9 @@ ///This function just returns a \ref MulMap class. ///\relates MulMap - template - inline MulMap mulMap(const M1 &m1,const M2 &m2) - { - return MulMap(m1,m2); + template + inline MulMap mulMap(const M1 &m1,const M2 &m2) { + return MulMap(m1,m2); } ///Scales a maps with a constant. @@ -437,18 +438,14 @@ /// ConstMap c_tmp(v); /// MulMap > sc(x,v); ///\endcode - template - class ScaleMap - { + template + class ScaleMap : public MapBase { typename SmartConstReference::Type m; typename M::Value v; public: - - typedef True NeedCopy; - ///\e - typedef typename M::Key Key; - ///\e - typedef typename M::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor @@ -464,10 +461,9 @@ ///This function just returns an \ref ScaleMap class. ///\relates ScaleMap ///\todo A better name is required. - template - inline ScaleMap scaleMap(const M &m,const typename M::Value &v) - { - return ScaleMap(m,v); + template + inline ScaleMap scaleMap(const M &m,const typename M::Value &v) { + return ScaleMap(m,v); } ///Quotient of two maps @@ -477,18 +473,14 @@ ///given maps. Its \c Key and \c Value will be inherited from \c M1. ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. - template - class DivMap - { + template + class DivMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; public: - - typedef True NeedCopy; - ///\e - typedef typename M1::Key Key; - ///\e - typedef typename M1::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; @@ -499,10 +491,9 @@ ///This function just returns a \ref DivMap class. ///\relates DivMap - template - inline DivMap divMap(const M1 &m1,const M2 &m2) - { - return DivMap(m1,m2); + template + inline DivMap divMap(const M1 &m1,const M2 &m2) { + return DivMap(m1,m2); } ///Composition of two maps @@ -513,7 +504,7 @@ ///of \c M2, ///then for ///\code - /// ComposeMap cm(m1,m2); + /// ComposeMap cm(m1,m2); ///\endcode /// cm[x] will be equal to m1[m2[x]] /// @@ -522,18 +513,14 @@ ///The \c M2::Value must be convertible to \c M1::Key. ///\todo Check the requirements. - template - class ComposeMap - { + template + class ComposeMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; public: - - typedef True NeedCopy; - ///\e - typedef typename M2::Key Key; - ///\e - typedef typename M1::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; @@ -544,10 +531,9 @@ ///This function just returns a \ref ComposeMap class. /// ///\relates ComposeMap - template - inline ComposeMap composeMap(const M1 &m1,const M2 &m2) - { - return ComposeMap(m1,m2); + template + inline ComposeMap composeMap(const M1 &m1,const M2 &m2) { + return ComposeMap(m1,m2); } ///Combines of two maps using an STL (binary) functor. @@ -563,7 +549,7 @@ ///and \c f is of \c F, ///then for ///\code - /// CombineMap cm(m1,m2,f); + /// CombineMap cm(m1,m2,f); ///\endcode /// cm[x] will be equal to f(m1[x],m2[x]) /// @@ -573,19 +559,17 @@ ///to \c V. ///\todo Check the requirements. - template - class CombineMap - { + template + class CombineMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; F f; public: - - typedef True NeedCopy; - ///\e - typedef typename M1::Key Key; - ///\e - typedef V Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) @@ -609,10 +593,22 @@ ///\endcode /// ///\relates CombineMap - template - inline CombineMap combineMap(const M1 &m1,const M2 &m2,const F &f) - { - return CombineMap(m1,m2,f); + template + inline CombineMap + combineMap(const M1& m1,const M2& m2, const F& f) { + return CombineMap(m1,m2,f); + } + + template + inline CombineMap + combineMap(const M1& m1, const M2& m2, const F& f) { + return combineMap(m1,m2,f); + } + + template + inline CombineMap + combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { + return combineMap(m1,m2,f); } ///Negative value of a map @@ -623,17 +619,13 @@ ///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 NegMap - { + template + class NegMap : public MapBase { typename SmartConstReference::Type m; public: - - typedef True NeedCopy; - ///\e - typedef typename M::Key Key; - ///\e - typedef typename M::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor NegMap(const M &_m) : m(_m) {}; @@ -644,10 +636,9 @@ ///This function just returns a \ref NegMap class. ///\relates NegMap - template - inline NegMap negMap(const M &m) - { - return NegMap(m); + template + inline NegMap negMap(const M &m) { + return NegMap(m); } @@ -674,31 +665,30 @@ ///\endcode - template - class AbsMap - { + template + class AbsMap : public MapBase { typename SmartConstReference::Type m; public: - - typedef True NeedCopy; - ///\e - typedef typename M::Key Key; - ///\e - typedef typename M::Value Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor AbsMap(const M &_m) : m(_m) {}; - Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;} + Value operator[](Key k) const { + Value tmp = m[k]; + return tmp >= 0 ? tmp : -tmp; + } + }; ///Returns a \ref AbsMap class ///This function just returns a \ref AbsMap class. ///\relates AbsMap - template - inline AbsMap absMap(const M &m) - { - return AbsMap(m); + template + inline AbsMap absMap(const M &m) { + return AbsMap(m); } ///Converts an STL style functor to a map @@ -714,17 +704,16 @@ ///Parameter \c F is the type of the used functor. - template - class FunctorMap - { + template + class FunctorMap : public MapBase { const F &f; public: - - typedef True NeedCopy; - ///\e - typedef K Key; - ///\e - typedef V Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor FunctorMap(const F &_f) : f(_f) {}; @@ -737,12 +726,24 @@ /// ///The third template parameter isn't necessary to be given. ///\relates FunctorMap - template - inline FunctorMap functorMap(const F &f) - { - return FunctorMap(f); + template inline + FunctorMap functorMap(const F &f) { + return FunctorMap(f); } + template inline + FunctorMap + functorMap(const F &f) { + return functorMap(f); + } + + template inline + FunctorMap functorMap(V (*f)(K)) { + return functorMap(f); + } + + ///Converts a map to an STL style (unary) functor ///This class Converts a map to an STL style (unary) functor. @@ -752,21 +753,18 @@ ///a ususal \ref concept::ReadMap "readable map", ///i.e. operator[] and the \c Key and \c Value typedefs also exist. - template - class MapFunctor - { + template + class MapFunctor : public MapBase { typename SmartConstReference::Type m; public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; - typedef True NeedCopy; ///\e typedef typename M::Key argument_type; ///\e typedef typename M::Value result_type; - ///\e - typedef typename M::Key Key; - ///\e - typedef typename M::Value Value; ///Constructor MapFunctor(const M &_m) : m(_m) {}; @@ -780,10 +778,9 @@ ///This function just returns a \ref MapFunctor class. ///\relates MapFunctor - template - inline MapFunctor mapFunctor(const M &m) - { - return MapFunctor(m); + template + inline MapFunctor mapFunctor(const M &m) { + return MapFunctor(m); } @@ -798,23 +795,19 @@ ///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 ForkMap - { + template + class ForkMap : public MapBase { typename SmartConstReference::Type m1; typename SmartConstReference::Type m2; public: - - typedef True NeedCopy; - ///\e - typedef typename M1::Key Key; - ///\e - typedef typename M1::Value Value; + typedef MapBase 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];} - 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 @@ -824,10 +817,9 @@ /// ///\relates ForkMap ///\todo Wrong scope in Doxygen when \c \\relates is used - template - inline ForkMap forkMap(const M1 &m1,const M2 &m2) - { - return ForkMap(m1,m2); + template + inline ForkMap forkMap(const M1 &m1,const M2 &m2) { + return ForkMap(m1,m2); } @@ -842,17 +834,13 @@ ///given map. Its \c Key and will be inherited from \c M, ///its Value is bool. - template - class NotMap - { + template + class NotMap : public MapBase { typename SmartConstReference::Type m; public: - - typedef True NeedCopy; - ///\e - typedef typename M::Key Key; - ///\e - typedef bool Value; + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; ///Constructor NotMap(const M &_m) : m(_m) {}; @@ -863,22 +851,15 @@ ///This function just returns a \ref NotMap class. ///\relates NotMap - template - inline NotMap notMap(const M &m) - { - return NotMap(m); + template + inline NotMap notMap(const M &m) { + return NotMap(m); } - - - - - - /// @} } diff -r 648aa2f33dc8 -r fa89ffb27a6d test/map_test.h --- a/test/map_test.h Thu Sep 01 20:35:30 2005 +0000 +++ b/test/map_test.h Thu Sep 08 14:34:50 2005 +0000 @@ -49,6 +49,10 @@ nodes.push_back(graph.addNode()); map[nodes.back()] = 23; } + map = constMap(12); + for (int i = 0; i < (int)nodes.size(); ++i) { + check(map[nodes[i]] == 12, "Wrong map constructor."); + } graph.clear(); nodes.clear(); } @@ -86,6 +90,10 @@ map[edges.back()] = 23; } } + map = constMap(12); + for (int i = 0; i < (int)edges.size(); ++i) { + check(map[edges[i]] == 12, "Wrong map constructor."); + } graph.clear(); edges.clear(); } diff -r 648aa2f33dc8 -r fa89ffb27a6d test/maps_test.cc --- a/test/maps_test.cc Thu Sep 01 20:35:30 2005 +0000 +++ b/test/maps_test.cc Thu Sep 08 14:34:50 2005 +0000 @@ -9,14 +9,19 @@ struct A {}; struct B {}; -class F -{ + +class F { public: + typedef A argument_type; + typedef B result_type; + B operator()(const A &) const {return B();} }; int func(A) {return 3;} +int binc(int, B) {return 4;} + typedef ReadMap DoubleMap; int main() @@ -38,7 +43,7 @@ checkConcept, ComposeMap > >(); - checkConcept, FunctorMap >(); + checkConcept, FunctorMap >(); int a; @@ -46,11 +51,15 @@ check(a==2,"Something is wrong with mapFunctor"); B b; - b=functorMap(F())[A()]; + b=functorMap(F())[A()]; - a=functorMap(&func)[A()]; + a=functorMap(&func)[A()]; check(a==3,"Something is wrong with functorMap"); + a=combineMap(constMap(), identityMap(), &binc)[B()]; + check(a==4,"Something is wrong with combineMap"); + + std::cout << __FILE__ ": All tests passed.\n"; return 0;