diff -r 372f08e8f403 -r 9d503ce002db src/lemon/maps.h --- a/src/lemon/maps.h Thu Dec 16 12:44:49 2004 +0000 +++ b/src/lemon/maps.h Mon Jan 03 16:19:46 2005 +0000 @@ -17,7 +17,10 @@ #ifndef LEMON_MAPS_H #define LEMON_MAPS_H +#include + ///\file +///\ingroup maps ///\brief Miscellaneous property maps /// ///\todo This file has the same name as the concept file in concept/, @@ -27,6 +30,9 @@ namespace lemon { + /// \addtogroup maps + /// @{ + /// Base class of maps. /// Base class of maps. @@ -168,6 +174,277 @@ typedef StdMap other; }; }; + + + ///Sum of two maps + + ///This \ref concept::ReadMap "read only map" returns the sum of the two + ///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 + { + const M1 &m1; + const M2 &m2; + public: + typedef typename M1::Key Key; + typedef typename M1::Value Value; + + ///Constructor + + ///\e + /// + AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; + Value operator[](Key k) {return m1[k]+m2[k];} + }; + + ///Returns an \ref AddMap class + + ///This function just returns an \ref AddMap class. + ///\todo How to call these type of functions? + /// + ///\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); + } + + ///Difference of two maps + + ///This \ref concept::ReadMap "read only map" returns the difference + ///of the values returned by the two + ///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 + { + const M1 &m1; + const M2 &m2; + public: + typedef typename M1::Key Key; + typedef typename M1::Value Value; + + ///Constructor + + ///\e + /// + SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; + Value operator[](Key k) {return m1[k]-m2[k];} + }; + + ///Returns a \ref SubMap class + + ///This function just returns a \ref SubMap class. + /// + ///\relates SubMap + template + inline SubMap subMap(const M1 &m1,const M2 &m2) + { + return SubMap(m1,m2); + } + + ///Product of two maps + + ///This \ref concept::ReadMap "read only map" returns the product of the + ///values returned by the two + ///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 MulMap + { + const M1 &m1; + const M2 &m2; + public: + typedef typename M1::Key Key; + typedef typename M1::Value Value; + + ///Constructor + + ///\e + /// + MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; + Value operator[](Key k) {return m1[k]*m2[k];} + }; + + ///Returns a \ref MulMap class + + ///This function just returns a \ref MulMap class. + ///\relates MulMap + template + inline MulMap mulMap(const M1 &m1,const M2 &m2) + { + return MulMap(m1,m2); + } + + ///Quotient of two maps + + ///This \ref concept::ReadMap "read only map" returns the quotient of the + ///values returned by the two + ///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 + { + const M1 &m1; + const M2 &m2; + public: + typedef typename M1::Key Key; + typedef typename M1::Value Value; + + ///Constructor + + ///\e + /// + DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; + Value operator[](Key k) {return m1[k]/m2[k];} + }; + + ///Returns a \ref DivMap class + + ///This function just returns a \ref DivMap class. + ///\relates DivMap + template + inline DivMap divMap(const M1 &m1,const M2 &m2) + { + return DivMap(m1,m2); + } + + ///Composition of two maps + + ///This \ref concept::ReadMap "read only map" returns the composition of + ///two + ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is + ///of \c M2, + ///then for + ///\code + /// ComposeMap cm(m1,m2); + ///\endcode + ///cm[x] will be equal to m1[m2[x]] + /// + ///Its \c Key is inherited from \c M2 and its \c Value is from + ///\c M1. + ///The \c M2::Value must be convertible to \c M1::Key. + ///\todo Check the requirements. + + template + class ComposeMap + { + const M1 &m1; + const M2 &m2; + public: + typedef typename M2::Key Key; + typedef typename M1::Value Value; + + ///Constructor + + ///\e + /// + ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; + Value operator[](Key k) {return m1[m2[k]];} + }; + + ///Returns a \ref ComposeMap class + + ///This function just returns a \ref ComposeMap class. + ///\relates ComposeMap + template + inline ComposeMap composeMap(const M1 &m1,const M2 &m2) + { + return ComposeMap(m1,m2); + } + + ///Negative value of a map + + ///This \ref concept::ReadMap "read only 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 NegMap + { + const M &m; + public: + typedef typename M::Key Key; + typedef typename M::Value Value; + + ///Constructor + + ///\e + /// + NegMap(const M &_m) : m(_m) {}; + Value operator[](Key k) {return -m[k];} + }; + + ///Returns a \ref NegMap class + + ///This function just returns a \ref NegMap class. + ///\relates NegMap + template + inline NegMap negMap(const M &m) + { + return NegMap(m); + } + + + //\todo We need a unified way to handle the situation below! + struct _UnConvertible {}; + template inline A t_abs(A a) {return _UnConvertible();} + + template<> inline int t_abs<>(int n) {return abs(n);} + template<> inline long int t_abs<>(long int n) {return labs(n);} + //\bug llabs() is overloaded + template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} + + template<> inline float t_abs<>(float n) {return fabsf(n);} + template<> inline double t_abs<>(double n) {return fabs(n);} + template<> inline long double t_abs<>(long double n) {return fabsl(n);} + + ///Absolute value of a map + + ///This \ref concept::ReadMap "read only map" returns the absolute value + ///of the + ///value returned by the + ///given map. Its \c Key and \c Value will be inherited from \c M. + ///The function Value abs(Value) must be defined, of course. + + template + class AbsMap + { + const M &m; + public: + typedef typename M::Key Key; + typedef typename M::Value Value; + + ///Constructor + + ///\e + /// + AbsMap(const M &_m) : m(_m) {}; + Value operator[](Key k) {return t_abs(m[k]);} + }; + + ///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); + } + + /// @} } + + #endif // LEMON_MAPS_H