alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, 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: alpar@1041: #include 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. alpar@720: template alpar@720: class MapBase alpar@720: { alpar@720: public: 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... klao@286: template alpar@720: class NullMap : public MapBase klao@286: { klao@286: public: klao@286: 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: 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. klao@286: template alpar@720: class ConstMap : public MapBase klao@286: { klao@286: T v; klao@286: public: klao@286: 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 { klao@286: typedef ConstMap other; klao@286: }; klao@286: klao@286: template klao@286: 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 alpar@1076: template alpar@1076: inline ConstMap constMap(const K &k) alpar@1076: { alpar@1076: return ConstMap(k); alpar@1076: } alpar@1076: alpar@1076: marci@890: //to document later marci@890: template marci@890: struct Const { }; marci@890: //to document later marci@890: template marci@890: class ConstMap > : public MapBase marci@890: { marci@890: public: marci@890: ConstMap() { } marci@890: V operator[](const K&) const { return v; } marci@890: void set(const K&, const V&) { } marci@890: }; klao@286: 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 > alpar@987: class StdMap : public std::map { alpar@987: typedef std::map parent; klao@286: T v; klao@286: typedef typename parent::value_type PairType; klao@286: klao@286: public: alpar@987: typedef K Key; alpar@987: typedef T Value; alpar@987: typedef T& Reference; 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 marci@389: 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: } 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 { klao@286: typedef StdMap other; klao@286: }; klao@286: }; alpar@1041: alpar@1178: ///Convert the \c Value of a maps 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@1178: ///Its \c Value is inherited from \c M. alpar@1178: /// alpar@1178: ///Actually, alpar@1178: ///\code alpar@1178: /// ConvertMap sh(x,v); alpar@1178: ///\endcode alpar@1178: ///it is equivalent with alpar@1178: ///\code alpar@1178: /// ConstMap c_tmp(v); alpar@1178: /// AddMap > sh(x,v); alpar@1178: ///\endcode alpar@1178: ///\bug wrong documentation alpar@1178: template alpar@1178: class ConvertMap alpar@1178: { alpar@1178: const M &m; alpar@1178: public: alpar@1178: typedef typename M::Key Key; alpar@1178: typedef T Value; alpar@1178: alpar@1178: ///Constructor alpar@1178: alpar@1178: ///Constructor alpar@1178: ///\param _m is the undelying map alpar@1178: ///\param _v is the convert value alpar@1178: ConvertMap(const M &_m) : m(_m) {}; deba@1346: deba@1346: /// \brief The subscript operator. deba@1346: /// deba@1346: /// The subscript operator. deba@1346: /// \param edge The edge deba@1346: /// \return The target of the edge alpar@1178: Value operator[](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. alpar@1178: template alpar@1178: inline ConvertMap convertMap(const M &m) alpar@1178: { alpar@1178: return ConvertMap(m); alpar@1178: } alpar@1041: deba@1346: /// \brief Returns the source of the given edge. deba@1346: /// deba@1346: /// The SourceMap gives back the source Node of the given edge. deba@1346: /// \author Balazs Dezso deba@1346: template deba@1346: class SourceMap { deba@1346: public: deba@1346: typedef typename Graph::Node Value; deba@1346: typedef typename Graph::Edge Key; deba@1346: deba@1346: /// \brief Constructor deba@1346: /// deba@1346: /// Constructor deba@1346: /// \param _graph The graph that the map belongs to. deba@1346: SourceMap(const Graph& _graph) : graph(_graph) {} deba@1346: deba@1346: /// \brief The subscript operator. deba@1346: /// deba@1346: /// The subscript operator. deba@1346: /// \param edge The edge deba@1346: /// \return The source of the edge deba@1346: Value operator[](const Key& edge) { deba@1346: return graph.source(edge); deba@1346: } deba@1346: deba@1346: private: deba@1346: const Graph& graph; deba@1346: }; deba@1346: deba@1346: /// \brief Returns a \ref SourceMap class deba@1346: deba@1346: /// This function just returns an \ref SourceMap class. deba@1346: /// \relates SourceMap deba@1346: template deba@1346: inline SourceMap sourceMap(const Graph&) { deba@1346: return SourceMap(graph); deba@1346: } deba@1346: deba@1346: /// \brief Returns the target of the given edge. deba@1346: /// deba@1346: /// The TargetMap gives back the target Node of the given edge. deba@1346: /// \author Balazs Dezso deba@1346: template deba@1346: class TargetMap { deba@1346: public: deba@1346: typedef typename Graph::Node Value; deba@1346: typedef typename Graph::Edge Key; deba@1346: deba@1346: /// \brief Constructor deba@1346: /// deba@1346: /// Constructor deba@1346: /// \param _graph The graph that the map belongs to. deba@1346: TargetMap(const Graph& _graph) : graph(_graph) {} deba@1346: deba@1346: /// \brief The subscript operator. deba@1346: /// deba@1346: /// The subscript operator. deba@1346: /// \param edge The edge deba@1346: /// \return The target of the edge deba@1346: Value operator[](const Key& key) { deba@1346: return graph.target(key); deba@1346: } deba@1346: deba@1346: private: deba@1346: const Graph& graph; deba@1346: }; deba@1346: deba@1346: /// \brief Returns a \ref TargetMap class deba@1346: deba@1346: /// This function just returns an \ref TargetMap class. deba@1346: /// \relates TargetMap deba@1346: template deba@1346: inline TargetMap targetMap(const Graph&) { deba@1346: return TargetMap(graph); deba@1346: } deba@1346: 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: alpar@1041: template alpar@1041: class AddMap alpar@1041: { alpar@1041: const M1 &m1; alpar@1041: const M2 &m2; alpar@1041: public: alpar@1041: typedef typename M1::Key Key; alpar@1041: typedef typename M1::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// 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 alpar@1041: template alpar@1041: inline AddMap addMap(const M1 &m1,const M2 &m2) alpar@1041: { alpar@1041: return AddMap(m1,m2); alpar@1041: } alpar@1041: alpar@1070: ///Shift a maps 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@1070: ///it is equivalent with alpar@1070: ///\code alpar@1070: /// ConstMap c_tmp(v); alpar@1070: /// AddMap > sh(x,v); alpar@1070: ///\endcode alpar@1070: template alpar@1070: class ShiftMap alpar@1070: { alpar@1070: const M &m; alpar@1070: typename M::Value v; alpar@1070: public: alpar@1070: typedef typename M::Key Key; alpar@1070: typedef typename M::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 alpar@1070: ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; alpar@1070: 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. alpar@1070: template alpar@1070: inline ShiftMap shiftMap(const M &m,const typename M::Value &v) alpar@1070: { alpar@1070: 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@1041: ///of the values returned by 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: alpar@1041: template alpar@1041: class SubMap alpar@1041: { alpar@1041: const M1 &m1; alpar@1041: const M2 &m2; alpar@1041: public: alpar@1041: typedef typename M1::Key Key; alpar@1041: typedef typename M1::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// 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 alpar@1041: template alpar@1041: inline SubMap subMap(const M1 &m1,const M2 &m2) alpar@1041: { alpar@1041: 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@1041: ///values returned by 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: alpar@1041: template alpar@1041: class MulMap alpar@1041: { alpar@1041: const M1 &m1; alpar@1041: const M2 &m2; alpar@1041: public: alpar@1041: typedef typename M1::Key Key; alpar@1041: typedef typename M1::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// 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 alpar@1041: template alpar@1041: inline MulMap mulMap(const M1 &m1,const M2 &m2) alpar@1041: { alpar@1041: return MulMap(m1,m2); alpar@1041: } alpar@1041: alpar@1070: ///Scale a maps with a constant. alpar@1070: alpar@1070: ///This \ref concept::ReadMap "read only map" returns the value of the alpar@1070: ///given map multipied 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@1070: ///it is equivalent with alpar@1070: ///\code alpar@1070: /// ConstMap c_tmp(v); alpar@1070: /// MulMap > sc(x,v); alpar@1070: ///\endcode alpar@1070: template alpar@1070: class ScaleMap alpar@1070: { alpar@1070: const M &m; alpar@1070: typename M::Value v; alpar@1070: public: alpar@1070: typedef typename M::Key Key; alpar@1070: typedef typename M::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 alpar@1070: ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; alpar@1070: Value operator[](Key k) const {return m[k]*v;} 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. alpar@1070: template alpar@1070: inline ScaleMap scaleMap(const M &m,const typename M::Value &v) alpar@1070: { alpar@1070: 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@1041: ///values returned by 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: alpar@1041: template alpar@1041: class DivMap alpar@1041: { alpar@1041: const M1 &m1; alpar@1041: const M2 &m2; alpar@1041: public: alpar@1041: typedef typename M1::Key Key; alpar@1041: typedef typename M1::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// 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 alpar@1041: template alpar@1041: inline DivMap divMap(const M1 &m1,const M2 &m2) alpar@1041: { alpar@1041: 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 alpar@1041: /// 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: alpar@1041: template alpar@1041: class ComposeMap alpar@1041: { alpar@1041: const M1 &m1; alpar@1041: const M2 &m2; alpar@1041: public: alpar@1041: typedef typename M2::Key Key; alpar@1041: typedef typename M1::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// 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 alpar@1041: template alpar@1041: inline ComposeMap composeMap(const M1 &m1,const M2 &m2) alpar@1041: { alpar@1041: return ComposeMap(m1,m2); alpar@1041: } alpar@1219: alpar@1219: ///Combine of two maps using an STL (binary) functor. alpar@1219: alpar@1219: ///Combine of two maps using an STL (binary) functor. alpar@1219: /// alpar@1219: /// alpar@1219: ///This \ref concept::ReadMap "read only map" takes to maps and a alpar@1219: ///binary functor and returns the composition of alpar@1219: ///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 alpar@1219: /// 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: alpar@1219: template alpar@1219: class CombineMap alpar@1219: { alpar@1219: const M1 &m1; alpar@1219: const M2 &m2; alpar@1219: const F &f; alpar@1219: public: alpar@1219: typedef typename M1::Key Key; alpar@1219: typedef V Value; alpar@1219: alpar@1219: ///Constructor alpar@1219: alpar@1219: ///\e alpar@1219: /// 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 alpar@1219: template alpar@1219: inline CombineMap combineMap(const M1 &m1,const M2 &m2,const F &f) alpar@1219: { alpar@1219: 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: alpar@1041: template alpar@1041: class NegMap alpar@1041: { alpar@1041: const M &m; alpar@1041: public: alpar@1041: typedef typename M::Key Key; alpar@1041: typedef typename M::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// 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 alpar@1041: template alpar@1041: inline NegMap negMap(const M &m) alpar@1041: { alpar@1041: 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: alpar@1041: template alpar@1041: class AbsMap alpar@1041: { alpar@1041: const M &m; alpar@1041: public: alpar@1041: typedef typename M::Key Key; alpar@1041: typedef typename M::Value Value; alpar@1041: alpar@1041: ///Constructor alpar@1041: alpar@1041: ///\e alpar@1041: /// alpar@1041: AbsMap(const M &_m) : m(_m) {}; alpar@1044: Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;} 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 alpar@1041: template alpar@1041: inline AbsMap absMap(const M &m) alpar@1041: { alpar@1041: return AbsMap(m); alpar@1041: } alpar@1041: alpar@1076: ///Converts an STL style functor to a 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: alpar@1076: template alpar@1076: class FunctorMap alpar@1076: { alpar@1076: const F &f; alpar@1076: public: alpar@1076: typedef K Key; alpar@1076: typedef V Value; alpar@1076: alpar@1076: ///Constructor alpar@1076: alpar@1076: ///\e alpar@1076: /// alpar@1076: FunctorMap(const F &_f) : f(_f) {}; alpar@1076: 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 alpar@1076: template alpar@1076: inline FunctorMap functorMap(const F &f) alpar@1076: { alpar@1076: return FunctorMap(f); alpar@1076: } alpar@1076: 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@1223: ///a ususal \ref concept::ReadMap "readable map", i.e marci@1172: ///operator[] and the \c Key and \c Value typedefs also exist. alpar@1076: alpar@1076: template alpar@1076: class MapFunctor alpar@1076: { alpar@1076: const M &m; alpar@1076: public: alpar@1223: typedef typename M::Key argument_type; alpar@1223: typedef typename M::Value result_type; alpar@1076: typedef typename M::Key Key; alpar@1076: typedef typename M::Value Value; alpar@1076: alpar@1076: ///Constructor alpar@1076: alpar@1076: ///\e alpar@1076: /// alpar@1076: MapFunctor(const M &_m) : m(_m) {}; alpar@1076: ///Returns a value of the map alpar@1076: alpar@1076: ///\e alpar@1076: /// alpar@1076: Value operator()(Key k) const {return m[k];} alpar@1076: ///\e alpar@1076: /// 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 alpar@1076: template alpar@1076: inline MapFunctor mapFunctor(const M &m) alpar@1076: { alpar@1076: return MapFunctor(m); alpar@1076: } alpar@1076: alpar@1076: alpar@1219: ///Apply 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: alpar@1219: template alpar@1219: class ForkMap alpar@1219: { alpar@1219: const M1 &m1; alpar@1219: const M2 &m2; alpar@1219: public: alpar@1219: typedef typename M1::Key Key; alpar@1219: typedef typename M1::Value Value; alpar@1219: alpar@1219: ///Constructor alpar@1219: alpar@1219: ///\e alpar@1219: /// alpar@1219: ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@1219: Value operator[](Key k) const {return m1[k];} alpar@1219: 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 alpar@1219: template alpar@1219: inline ForkMap forkMap(const M1 &m1,const M2 &m2) alpar@1219: { alpar@1219: return ForkMap(m1,m2); alpar@1219: } alpar@1219: alpar@1041: /// @} klao@286: klao@286: } alpar@1041: alpar@1041: alpar@921: #endif // LEMON_MAPS_H