alpar@906: /* -*- C++ -*- ladanyi@1435: * lemon/maps.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_MAPS_H alpar@921: #define LEMON_MAPS_H klao@286: deba@1420: #include deba@1420: alpar@1041: klao@286: ///\file alpar@1041: ///\ingroup maps klao@286: ///\brief Miscellaneous property maps klao@286: /// klao@959: ///\todo This file has the same name as the concept file in concept/, klao@286: /// and this is not easily detectable in docs... klao@286: klao@286: #include klao@286: alpar@921: namespace lemon { klao@286: alpar@1041: /// \addtogroup maps alpar@1041: /// @{ alpar@1041: alpar@720: /// Base class of maps. alpar@720: alpar@805: /// Base class of maps. alpar@805: /// It provides the necessary typedefs required by the map concept. deba@1675: template deba@1675: class MapBase { alpar@720: public: deba@1675: /// \e deba@1675: typedef _NeedCopy NeedCopy; alpar@911: ///\e alpar@987: typedef K Key; alpar@911: ///\e alpar@987: typedef T Value; alpar@720: }; alpar@720: alpar@805: /// Null map. (a.k.a. DoNothingMap) klao@286: klao@286: /// If you have to provide a map only for its type definitions, alpar@805: /// or if you have to provide a writable map, but alpar@805: /// data written to it will sent to /dev/null... deba@1675: template deba@1675: class NullMap : public MapBase { klao@286: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1420: alpar@805: /// Gives back a default constructed element. klao@286: T operator[](const K&) const { return T(); } alpar@805: /// Absorbs the value. klao@286: void set(const K&, const T&) {} klao@286: }; klao@286: deba@1420: template deba@1675: NullMap nullMap() { deba@1675: return NullMap(); deba@1420: } deba@1420: klao@286: klao@286: /// Constant map. klao@286: alpar@805: /// This is a readable map which assigns a specified value to each key. alpar@805: /// In other aspects it is equivalent to the \ref NullMap. alpar@805: /// \todo set could be used to set the value. deba@1675: template deba@1675: class ConstMap : public MapBase { deba@1675: private: klao@286: T v; klao@286: public: klao@286: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1420: alpar@805: /// Default constructor alpar@805: alpar@805: /// The value of the map will be uninitialized. alpar@805: /// (More exactly it will be default constructed.) klao@286: ConstMap() {} alpar@911: ///\e alpar@805: alpar@805: /// \param _v The initial value of the map. alpar@911: /// klao@286: ConstMap(const T &_v) : v(_v) {} klao@286: klao@286: T operator[](const K&) const { return v; } klao@286: void set(const K&, const T&) {} klao@286: klao@286: template klao@286: struct rebind { deba@1675: typedef ConstMap other; klao@286: }; klao@286: klao@286: template deba@1675: ConstMap(const ConstMap &, const T &_v) : v(_v) {} klao@286: }; klao@286: alpar@1076: ///Returns a \ref ConstMap class alpar@1076: alpar@1076: ///This function just returns a \ref ConstMap class. alpar@1076: ///\relates ConstMap deba@1675: template deba@1675: inline ConstMap constMap(const V &v) { deba@1675: return ConstMap(v); alpar@1076: } alpar@1076: alpar@1076: alpar@1660: //\todo to document later marci@890: template marci@890: struct Const { }; deba@1675: alpar@1660: //\todo to document later deba@1675: template deba@1675: class ConstMap, NC > : public MapBase { marci@890: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1675: marci@890: ConstMap() { } marci@890: V operator[](const K&) const { return v; } marci@890: void set(const K&, const V&) { } marci@890: }; klao@286: deba@1675: ///Returns a \ref ConstMap class deba@1675: deba@1675: ///This function just returns a \ref ConstMap class. deba@1675: ///\relates ConstMap deba@1675: template deba@1675: inline ConstMap, True> constMap() { deba@1675: return ConstMap, True>(); deba@1675: } deba@1675: klao@286: /// \c std::map wrapper klao@286: klao@286: /// This is essentially a wrapper for \c std::map. With addition that alpar@987: /// you can specify a default value different from \c Value() . klao@286: /// klao@286: /// \todo Provide allocator parameter... alpar@987: template > deba@1675: class StdMap : public std::map { deba@1675: typedef std::map parent; klao@286: T v; klao@286: typedef typename parent::value_type PairType; klao@286: klao@286: public: alpar@1456: ///\e alpar@987: typedef K Key; alpar@1456: ///\e alpar@987: typedef T Value; alpar@1456: ///\e alpar@987: typedef T& Reference; alpar@1456: ///\e alpar@987: typedef const T& ConstReference; klao@286: klao@286: klao@345: StdMap() : v() {} klao@286: /// Constructor with specified default value klao@286: StdMap(const T& _v) : v(_v) {} klao@286: klao@286: /// \brief Constructs the map from an appropriate std::map. klao@286: /// klao@286: /// \warning Inefficient: copies the content of \c m ! klao@286: StdMap(const parent &m) : parent(m) {} klao@286: /// \brief Constructs the map from an appropriate std::map, and explicitly klao@286: /// specifies a default value. klao@286: /// klao@286: /// \warning Inefficient: copies the content of \c m ! klao@286: StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} klao@286: klao@286: template deba@1675: StdMap(const StdMap &m, const T &_v) { marci@389: //FIXME; marci@389: } klao@286: alpar@987: Reference operator[](const Key &k) { klao@346: return insert(PairType(k,v)).first -> second; klao@286: } deba@1675: alpar@987: ConstReference operator[](const Key &k) const { marci@389: typename parent::iterator i = lower_bound(k); beckerjc@391: if (i == parent::end() || parent::key_comp()(k, (*i).first)) klao@286: return v; klao@286: return (*i).second; klao@286: } klao@345: void set(const Key &k, const T &t) { klao@346: parent::operator[](k) = t; klao@345: } klao@286: klao@286: /// Changes the default value of the map. klao@286: /// \return Returns the previous default value. klao@286: /// alpar@805: /// \warning The value of some keys (which has already been queried, but klao@286: /// the value has been unchanged from the default) may change! klao@286: T setDefault(const T &_v) { T old=v; v=_v; return old; } klao@286: klao@286: template klao@286: struct rebind { deba@1675: typedef StdMap other; klao@286: }; klao@286: }; alpar@1041: alpar@1402: /// @} alpar@1402: alpar@1402: /// \addtogroup map_adaptors alpar@1402: /// @{ alpar@1402: deba@1531: /// \brief Identity mapping. deba@1531: /// deba@1531: /// This mapping gives back the given key as value without any deba@1531: /// modification. deba@1675: template deba@1675: class IdentityMap : public MapBase { deba@1531: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1531: deba@1675: const T& operator[](const T& t) const { deba@1531: return t; deba@1531: } deba@1531: }; alpar@1402: deba@1675: ///Returns an \ref IdentityMap class deba@1675: deba@1675: ///This function just returns an \ref IdentityMap class. deba@1675: ///\relates IdentityMap deba@1675: template deba@1675: inline IdentityMap identityMap() { deba@1675: return IdentityMap(); deba@1675: } deba@1675: deba@1675: alpar@1547: ///Convert the \c Value of a map to another type. alpar@1178: alpar@1178: ///This \ref concept::ReadMap "read only map" alpar@1178: ///converts the \c Value of a maps to type \c T. alpar@1547: ///Its \c Key is inherited from \c M. deba@1675: template deba@1675: class ConvertMap : public MapBase { deba@1420: typename SmartConstReference::Type m; alpar@1178: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1178: alpar@1178: ///Constructor alpar@1178: alpar@1178: ///Constructor alpar@1536: ///\param _m is the underlying map alpar@1178: ConvertMap(const M &_m) : m(_m) {}; deba@1346: deba@1346: /// \brief The subscript operator. deba@1346: /// deba@1346: /// The subscript operator. alpar@1536: /// \param k The key deba@1346: /// \return The target of the edge deba@1675: Value operator[](const Key& k) const {return m[k];} alpar@1178: }; alpar@1178: alpar@1178: ///Returns an \ref ConvertMap class alpar@1178: alpar@1178: ///This function just returns an \ref ConvertMap class. alpar@1178: ///\relates ConvertMap alpar@1178: ///\todo The order of the template parameters are changed. deba@1675: template deba@1675: inline ConvertMap convertMap(const M &m) { deba@1675: return ConvertMap(m); alpar@1178: } alpar@1041: alpar@1041: ///Sum of two maps alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the sum of the two alpar@1041: ///given maps. Its \c Key and \c Value will be inherited from \c M1. alpar@1041: ///The \c Key and \c Value of M2 must be convertible to those of \c M1. alpar@1041: deba@1675: template deba@1675: class AddMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; deba@1420: alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1044: Value operator[](Key k) const {return m1[k]+m2[k];} alpar@1041: }; alpar@1041: alpar@1041: ///Returns an \ref AddMap class alpar@1041: alpar@1041: ///This function just returns an \ref AddMap class. alpar@1041: ///\todo How to call these type of functions? alpar@1041: /// alpar@1041: ///\relates AddMap alpar@1041: ///\todo Wrong scope in Doxygen when \c \\relates is used deba@1675: template deba@1675: inline AddMap addMap(const M1 &m1,const M2 &m2) { deba@1675: return AddMap(m1,m2); alpar@1041: } alpar@1041: alpar@1547: ///Shift a map with a constant. alpar@1070: alpar@1070: ///This \ref concept::ReadMap "read only map" returns the sum of the alpar@1070: ///given map and a constant value. alpar@1070: ///Its \c Key and \c Value is inherited from \c M. alpar@1070: /// alpar@1070: ///Actually, alpar@1070: ///\code alpar@1070: /// ShiftMap sh(x,v); alpar@1070: ///\endcode alpar@1547: ///is equivalent with alpar@1070: ///\code alpar@1070: /// ConstMap c_tmp(v); alpar@1070: /// AddMap > sh(x,v); alpar@1070: ///\endcode deba@1691: template deba@1675: class ShiftMap : public MapBase { deba@1420: typename SmartConstReference::Type m; deba@1691: C v; alpar@1070: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1070: alpar@1070: ///Constructor alpar@1070: alpar@1070: ///Constructor alpar@1070: ///\param _m is the undelying map alpar@1070: ///\param _v is the shift value deba@1691: ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; deba@1691: Value operator[](Key k) const {return m[k] + v;} alpar@1070: }; alpar@1070: alpar@1070: ///Returns an \ref ShiftMap class alpar@1070: alpar@1070: ///This function just returns an \ref ShiftMap class. alpar@1070: ///\relates ShiftMap alpar@1070: ///\todo A better name is required. deba@1691: template deba@1691: inline ShiftMap shiftMap(const M &m,const C &v) { deba@1691: return ShiftMap(m,v); alpar@1070: } alpar@1070: alpar@1041: ///Difference of two maps alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the difference alpar@1547: ///of the values of the two alpar@1041: ///given maps. Its \c Key and \c Value will be inherited from \c M1. alpar@1041: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. alpar@1041: deba@1675: template deba@1675: class SubMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1044: Value operator[](Key k) const {return m1[k]-m2[k];} alpar@1041: }; alpar@1041: alpar@1041: ///Returns a \ref SubMap class alpar@1041: alpar@1041: ///This function just returns a \ref SubMap class. alpar@1041: /// alpar@1041: ///\relates SubMap deba@1675: template deba@1675: inline SubMap subMap(const M1 &m1, const M2 &m2) { deba@1675: return SubMap(m1, m2); alpar@1041: } alpar@1041: alpar@1041: ///Product of two maps alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the product of the alpar@1547: ///values of the two alpar@1041: ///given alpar@1041: ///maps. Its \c Key and \c Value will be inherited from \c M1. alpar@1041: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. alpar@1041: deba@1675: template deba@1675: class MulMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1044: Value operator[](Key k) const {return m1[k]*m2[k];} alpar@1041: }; alpar@1041: alpar@1041: ///Returns a \ref MulMap class alpar@1041: alpar@1041: ///This function just returns a \ref MulMap class. alpar@1041: ///\relates MulMap deba@1675: template deba@1675: inline MulMap mulMap(const M1 &m1,const M2 &m2) { deba@1675: return MulMap(m1,m2); alpar@1041: } alpar@1041: alpar@1547: ///Scales a maps with a constant. alpar@1070: alpar@1070: ///This \ref concept::ReadMap "read only map" returns the value of the deba@1691: ///given map multiplied from the left side with a constant value. alpar@1070: ///Its \c Key and \c Value is inherited from \c M. alpar@1070: /// alpar@1070: ///Actually, alpar@1070: ///\code alpar@1070: /// ScaleMap sc(x,v); alpar@1070: ///\endcode alpar@1547: ///is equivalent with alpar@1070: ///\code alpar@1070: /// ConstMap c_tmp(v); alpar@1070: /// MulMap > sc(x,v); alpar@1070: ///\endcode deba@1691: template deba@1675: class ScaleMap : public MapBase { deba@1420: typename SmartConstReference::Type m; deba@1691: C v; alpar@1070: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1070: alpar@1070: ///Constructor alpar@1070: alpar@1070: ///Constructor alpar@1070: ///\param _m is the undelying map alpar@1070: ///\param _v is the scaling value deba@1691: ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; deba@1691: Value operator[](Key k) const {return v * m[k];} alpar@1070: }; alpar@1070: alpar@1070: ///Returns an \ref ScaleMap class alpar@1070: alpar@1070: ///This function just returns an \ref ScaleMap class. alpar@1070: ///\relates ScaleMap alpar@1070: ///\todo A better name is required. deba@1691: template deba@1691: inline ScaleMap scaleMap(const M &m,const C &v) { deba@1691: return ScaleMap(m,v); alpar@1070: } alpar@1070: alpar@1041: ///Quotient of two maps alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the quotient of the alpar@1547: ///values of the two alpar@1041: ///given maps. Its \c Key and \c Value will be inherited from \c M1. alpar@1041: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. alpar@1041: deba@1675: template deba@1675: class DivMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1044: Value operator[](Key k) const {return m1[k]/m2[k];} alpar@1041: }; alpar@1041: alpar@1041: ///Returns a \ref DivMap class alpar@1041: alpar@1041: ///This function just returns a \ref DivMap class. alpar@1041: ///\relates DivMap deba@1675: template deba@1675: inline DivMap divMap(const M1 &m1,const M2 &m2) { deba@1675: return DivMap(m1,m2); alpar@1041: } alpar@1041: alpar@1041: ///Composition of two maps alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the composition of alpar@1041: ///two alpar@1041: ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is alpar@1041: ///of \c M2, alpar@1041: ///then for alpar@1041: ///\code deba@1675: /// ComposeMap cm(m1,m2); alpar@1041: ///\endcode alpar@1044: /// cm[x] will be equal to m1[m2[x]] alpar@1041: /// alpar@1041: ///Its \c Key is inherited from \c M2 and its \c Value is from alpar@1041: ///\c M1. alpar@1041: ///The \c M2::Value must be convertible to \c M1::Key. alpar@1041: ///\todo Check the requirements. alpar@1041: deba@1675: template deba@1675: class ComposeMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1044: Value operator[](Key k) const {return m1[m2[k]];} alpar@1041: }; alpar@1041: ///Returns a \ref ComposeMap class alpar@1041: alpar@1041: ///This function just returns a \ref ComposeMap class. alpar@1219: /// alpar@1041: ///\relates ComposeMap deba@1675: template deba@1675: inline ComposeMap composeMap(const M1 &m1,const M2 &m2) { deba@1675: return ComposeMap(m1,m2); alpar@1041: } alpar@1219: alpar@1547: ///Combines of two maps using an STL (binary) functor. alpar@1219: alpar@1547: ///Combines of two maps using an STL (binary) functor. alpar@1219: /// alpar@1219: /// alpar@1547: ///This \ref concept::ReadMap "read only map" takes two maps and a alpar@1219: ///binary functor and returns the composition of alpar@1547: ///the two alpar@1219: ///given maps unsing the functor. alpar@1219: ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 alpar@1219: ///and \c f is of \c F, alpar@1219: ///then for alpar@1219: ///\code deba@1675: /// CombineMap cm(m1,m2,f); alpar@1219: ///\endcode alpar@1219: /// cm[x] will be equal to f(m1[x],m2[x]) alpar@1219: /// alpar@1219: ///Its \c Key is inherited from \c M1 and its \c Value is \c V. alpar@1219: ///The \c M2::Value and \c M1::Value must be convertible to the corresponding alpar@1219: ///input parameter of \c F and the return type of \c F must be convertible alpar@1219: ///to \c V. alpar@1219: ///\todo Check the requirements. alpar@1219: deba@1675: template deba@1675: class CombineMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; deba@1420: F f; alpar@1219: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1219: alpar@1219: ///Constructor alpar@1219: CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) alpar@1219: : m1(_m1), m2(_m2), f(_f) {}; alpar@1219: Value operator[](Key k) const {return f(m1[k],m2[k]);} alpar@1219: }; alpar@1219: alpar@1219: ///Returns a \ref CombineMap class alpar@1219: alpar@1219: ///This function just returns a \ref CombineMap class. alpar@1219: /// alpar@1219: ///Only the first template parameter (the value type) must be given. alpar@1219: /// alpar@1219: ///For example if \c m1 and \c m2 are both \c double valued maps, then alpar@1219: ///\code alpar@1219: ///combineMap(m1,m2,std::plus) alpar@1219: ///\endcode alpar@1219: ///is equivalent with alpar@1219: ///\code alpar@1219: ///addMap(m1,m2) alpar@1219: ///\endcode alpar@1219: /// alpar@1219: ///\relates CombineMap deba@1675: template deba@1675: inline CombineMap deba@1675: combineMap(const M1& m1,const M2& m2, const F& f) { deba@1675: return CombineMap(m1,m2,f); deba@1675: } deba@1675: deba@1675: template deba@1675: inline CombineMap deba@1675: combineMap(const M1& m1, const M2& m2, const F& f) { deba@1675: return combineMap(m1,m2,f); deba@1675: } deba@1675: deba@1675: template deba@1675: inline CombineMap deba@1675: combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { deba@1675: return combineMap(m1,m2,f); alpar@1219: } alpar@1041: alpar@1041: ///Negative value of a map alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the negative alpar@1041: ///value of the alpar@1041: ///value returned by the alpar@1041: ///given map. Its \c Key and \c Value will be inherited from \c M. alpar@1041: ///The unary \c - operator must be defined for \c Value, of course. alpar@1041: deba@1675: template deba@1675: class NegMap : public MapBase { deba@1420: typename SmartConstReference::Type m; alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: NegMap(const M &_m) : m(_m) {}; alpar@1044: Value operator[](Key k) const {return -m[k];} alpar@1041: }; alpar@1041: alpar@1041: ///Returns a \ref NegMap class alpar@1041: alpar@1041: ///This function just returns a \ref NegMap class. alpar@1041: ///\relates NegMap deba@1675: template deba@1675: inline NegMap negMap(const M &m) { deba@1675: return NegMap(m); alpar@1041: } alpar@1041: alpar@1041: alpar@1041: ///Absolute value of a map alpar@1041: alpar@1041: ///This \ref concept::ReadMap "read only map" returns the absolute value alpar@1041: ///of the alpar@1041: ///value returned by the alpar@1044: ///given map. Its \c Key and \c Value will be inherited alpar@1044: ///from M. Value alpar@1044: ///must be comparable to 0 and the unary - alpar@1044: ///operator must be defined for it, of course. alpar@1044: /// alpar@1044: ///\bug We need a unified way to handle the situation below: alpar@1044: ///\code alpar@1044: /// struct _UnConvertible {}; alpar@1044: /// template inline A t_abs(A a) {return _UnConvertible();} alpar@1044: /// template<> inline int t_abs<>(int n) {return abs(n);} alpar@1044: /// template<> inline long int t_abs<>(long int n) {return labs(n);} alpar@1044: /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} alpar@1044: /// template<> inline float t_abs<>(float n) {return fabsf(n);} alpar@1044: /// template<> inline double t_abs<>(double n) {return fabs(n);} alpar@1044: /// template<> inline long double t_abs<>(long double n) {return fabsl(n);} alpar@1044: ///\endcode alpar@1044: alpar@1041: deba@1675: template deba@1675: class AbsMap : public MapBase { deba@1420: typename SmartConstReference::Type m; alpar@1041: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: AbsMap(const M &_m) : m(_m) {}; deba@1675: Value operator[](Key k) const { deba@1675: Value tmp = m[k]; deba@1675: return tmp >= 0 ? tmp : -tmp; deba@1675: } deba@1675: alpar@1041: }; alpar@1041: alpar@1041: ///Returns a \ref AbsMap class alpar@1041: alpar@1041: ///This function just returns a \ref AbsMap class. alpar@1041: ///\relates AbsMap deba@1675: template deba@1675: inline AbsMap absMap(const M &m) { deba@1675: return AbsMap(m); alpar@1041: } alpar@1041: alpar@1402: ///Converts an STL style functor to a map alpar@1076: alpar@1076: ///This \ref concept::ReadMap "read only map" returns the value alpar@1076: ///of a alpar@1076: ///given map. alpar@1076: /// alpar@1076: ///Template parameters \c K and \c V will become its alpar@1076: ///\c Key and \c Value. They must be given explicitely alpar@1076: ///because a functor does not provide such typedefs. alpar@1076: /// alpar@1076: ///Parameter \c F is the type of the used functor. alpar@1076: alpar@1076: deba@1675: template deba@1675: class FunctorMap : public MapBase { deba@1679: F f; alpar@1076: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1076: alpar@1076: ///Constructor deba@1679: FunctorMap(const F &_f) : f(_f) {} deba@1679: deba@1679: Value operator[](Key k) const { return f(k);} alpar@1076: }; alpar@1076: alpar@1076: ///Returns a \ref FunctorMap class alpar@1076: alpar@1076: ///This function just returns a \ref FunctorMap class. alpar@1076: /// alpar@1076: ///The third template parameter isn't necessary to be given. alpar@1076: ///\relates FunctorMap deba@1675: template inline deba@1675: FunctorMap functorMap(const F &f) { deba@1675: return FunctorMap(f); alpar@1076: } alpar@1076: deba@1675: template inline deba@1675: FunctorMap deba@1675: functorMap(const F &f) { deba@1679: return FunctorMap(f); deba@1675: } deba@1675: deba@1675: template inline deba@1675: FunctorMap functorMap(V (*f)(K)) { deba@1679: return FunctorMap(f); deba@1675: } deba@1675: deba@1675: alpar@1219: ///Converts a map to an STL style (unary) functor alpar@1076: alpar@1219: ///This class Converts a map to an STL style (unary) functor. alpar@1076: ///that is it provides an operator() to read its values. alpar@1076: /// alpar@1223: ///For the sake of convenience it also works as alpar@1537: ///a ususal \ref concept::ReadMap "readable map", alpar@1537: ///i.e. operator[] and the \c Key and \c Value typedefs also exist. alpar@1076: deba@1675: template deba@1675: class MapFunctor : public MapBase { deba@1420: typename SmartConstReference::Type m; alpar@1076: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1420: alpar@1456: ///\e alpar@1223: typedef typename M::Key argument_type; alpar@1456: ///\e alpar@1223: typedef typename M::Value result_type; alpar@1076: alpar@1076: ///Constructor alpar@1076: MapFunctor(const M &_m) : m(_m) {}; alpar@1076: ///Returns a value of the map alpar@1076: Value operator()(Key k) const {return m[k];} alpar@1076: ///\e alpar@1076: Value operator[](Key k) const {return m[k];} alpar@1076: }; alpar@1076: alpar@1076: ///Returns a \ref MapFunctor class alpar@1076: alpar@1076: ///This function just returns a \ref MapFunctor class. alpar@1076: ///\relates MapFunctor deba@1675: template deba@1675: inline MapFunctor mapFunctor(const M &m) { deba@1675: return MapFunctor(m); alpar@1076: } alpar@1076: alpar@1076: alpar@1547: ///Applies all map setting operations to two maps alpar@1219: alpar@1219: ///This map has two \ref concept::WriteMap "writable map" alpar@1219: ///parameters and each write request will be passed to both of them. alpar@1219: ///If \c M1 is also \ref concept::ReadMap "readable", alpar@1219: ///then the read operations will return the alpar@1317: ///corresponding values of \c M1. alpar@1219: /// alpar@1219: ///The \c Key and \c Value will be inherited from \c M1. alpar@1219: ///The \c Key and \c Value of M2 must be convertible from those of \c M1. alpar@1219: deba@1675: template deba@1675: class ForkMap : public MapBase { deba@1420: typename SmartConstReference::Type m1; deba@1420: typename SmartConstReference::Type m2; alpar@1219: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1219: alpar@1219: ///Constructor alpar@1219: ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1219: Value operator[](Key k) const {return m1[k];} deba@1675: // void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} alpar@1219: }; alpar@1219: alpar@1219: ///Returns an \ref ForkMap class alpar@1219: alpar@1219: ///This function just returns an \ref ForkMap class. alpar@1219: ///\todo How to call these type of functions? alpar@1219: /// alpar@1219: ///\relates ForkMap alpar@1219: ///\todo Wrong scope in Doxygen when \c \\relates is used deba@1675: template deba@1675: inline ForkMap forkMap(const M1 &m1,const M2 &m2) { deba@1675: return ForkMap(m1,m2); alpar@1219: } alpar@1219: alpar@1456: alpar@1456: alpar@1456: /* ************* BOOL MAPS ******************* */ alpar@1456: alpar@1456: ///Logical 'not' of a map alpar@1456: alpar@1456: ///This bool \ref concept::ReadMap "read only map" returns the alpar@1456: ///logical negation of alpar@1456: ///value returned by the alpar@1456: ///given map. Its \c Key and will be inherited from \c M, alpar@1456: ///its Value is bool. alpar@1456: deba@1675: template deba@1675: class NotMap : public MapBase { alpar@1456: typename SmartConstReference::Type m; alpar@1456: public: deba@1675: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1456: alpar@1456: ///Constructor alpar@1456: NotMap(const M &_m) : m(_m) {}; alpar@1456: Value operator[](Key k) const {return !m[k];} alpar@1456: }; alpar@1456: alpar@1456: ///Returns a \ref NotMap class alpar@1456: alpar@1456: ///This function just returns a \ref NotMap class. alpar@1456: ///\relates NotMap deba@1675: template deba@1675: inline NotMap notMap(const M &m) { deba@1675: return NotMap(m); alpar@1456: } alpar@1456: alpar@1041: /// @} klao@286: } alpar@1041: alpar@921: #endif // LEMON_MAPS_H