alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@906: * Copyright (C) 2004 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: 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@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: 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@1041: Value operator[](Key k) {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@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@1041: Value operator[](Key k) {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@1041: Value operator[](Key k) {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@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@1041: Value operator[](Key k) {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@1041: ///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@1041: Value operator[](Key k) {return m1[m2[k]];} alpar@1041: }; alpar@1041: alpar@1041: ///Returns a \ref ComposeMap class alpar@1041: alpar@1041: ///This function just returns a \ref ComposeMap class. 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@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@1041: Value operator[](Key k) {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: //\todo We need a unified way to handle the situation below! alpar@1041: struct _UnConvertible {}; alpar@1041: template inline A t_abs(A a) {return _UnConvertible();} alpar@1041: alpar@1041: template<> inline int t_abs<>(int n) {return abs(n);} alpar@1041: template<> inline long int t_abs<>(long int n) {return labs(n);} alpar@1041: //\bug llabs() is overloaded alpar@1041: template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} alpar@1041: alpar@1041: template<> inline float t_abs<>(float n) {return fabsf(n);} alpar@1041: template<> inline double t_abs<>(double n) {return fabs(n);} alpar@1041: template<> inline long double t_abs<>(long double n) {return fabsl(n);} 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@1041: ///given map. Its \c Key and \c Value will be inherited from \c M. alpar@1041: ///The function Value abs(Value) must be defined, of course. 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@1041: Value operator[](Key k) {return t_abs(m[k]);} 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@1041: /// @} klao@286: klao@286: } alpar@1041: alpar@1041: alpar@921: #endif // LEMON_MAPS_H