alpar@906: /* -*- C++ -*- alpar@906: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2391: * Copyright (C) 2003-2007 alpar@1956: * 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@1778: #include deba@2091: #include deba@1778: deba@1993: #include deba@1993: #include alpar@1041: klao@286: ///\file alpar@1041: ///\ingroup maps klao@286: ///\brief Miscellaneous property maps 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@1705: template deba@1675: class MapBase { 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... deba@1705: template deba@1705: class NullMap : public MapBase { klao@286: public: deba@1705: 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@1705: NullMap nullMap() { deba@1705: 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. deba@2489: /// In other aspects it is equivalent to the \c NullMap. deba@1705: template deba@1705: class ConstMap : public MapBase { deba@1675: private: klao@286: T v; klao@286: public: klao@286: deba@1705: 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) {} deba@2489: deba@2489: ///\e deba@2489: T operator[](const K&) const { return v; } klao@286: deba@2489: ///\e deba@2489: void setAll(const T &t) { deba@2489: v = t; deba@2489: } 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: deba@2489: ///Returns a \c ConstMap class alpar@1076: deba@2489: ///This function just returns a \c ConstMap class. alpar@1076: ///\relates ConstMap deba@1675: template deba@1705: inline ConstMap constMap(const V &v) { deba@1705: return ConstMap(v); alpar@1076: } alpar@1076: alpar@1076: marci@890: template marci@890: struct Const { }; deba@1675: deba@2489: /// Constant map with inlined constant value. deba@2489: deba@2489: /// This is a readable map which assigns a specified value to each key. deba@2489: /// In other aspects it is equivalent to the \c NullMap. deba@1705: template deba@1705: class ConstMap > : public MapBase { marci@890: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1675: marci@890: ConstMap() { } deba@2489: ///\e marci@890: V operator[](const K&) const { return v; } deba@2489: ///\e marci@890: void set(const K&, const V&) { } marci@890: }; klao@286: deba@2489: ///Returns a \c ConstMap class deba@1675: deba@2489: ///This function just returns a \c ConstMap class with inlined value. deba@1675: ///\relates ConstMap deba@1675: template deba@1705: inline ConstMap > constMap() { deba@1705: return ConstMap >(); deba@1675: } deba@1675: deba@2489: ///Map based on std::map klao@286: deba@2489: ///This is essentially a wrapper for \c std::map. With addition that deba@2489: ///you can specify a default value different from \c Value() . alpar@987: template > deba@2489: class StdMap { deba@2489: template deba@2489: friend class StdMap; deba@2489: public: klao@286: deba@2489: typedef True ReferenceMapTag; 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: deba@2489: private: deba@2489: deba@2489: typedef std::map Map; deba@2489: Value _value; deba@2489: Map _map; klao@286: deba@2489: public: deba@2489: klao@286: /// Constructor with specified default value deba@2489: StdMap(const T& value = T()) : _value(value) {} klao@286: /// \brief Constructs the map from an appropriate std::map, and explicitly klao@286: /// specifies a default value. deba@2489: template deba@2489: StdMap(const std::map &map, const T& value = T()) deba@2489: : _map(map.begin(), map.end()), _value(value) {} klao@286: deba@2489: /// \brief Constructs a map from an other StdMap. klao@286: template deba@2489: StdMap(const StdMap &c) deba@2489: : _map(c._map.begin(), c._map.end()), _value(c._value) {} deba@2489: deba@2489: private: deba@2489: deba@2489: StdMap& operator=(const StdMap&); deba@2489: deba@2489: public: deba@2489: deba@2489: ///\e deba@2489: Reference operator[](const Key &k) { deba@2489: typename Map::iterator it = _map.lower_bound(k); deba@2489: if (it != _map.end() && !_map.key_comp()(k, it->first)) deba@2489: return it->second; deba@2489: else deba@2489: return _map.insert(it, std::make_pair(k, _value))->second; marci@389: } klao@286: deba@2489: /// \e deba@2489: ConstReference operator[](const Key &k) const { deba@2489: typename Map::const_iterator it = _map.find(k); deba@2489: if (it != _map.end()) deba@2489: return it->second; deba@2489: else deba@2489: return _value; klao@286: } deba@1675: deba@2489: /// \e klao@345: void set(const Key &k, const T &t) { deba@2489: typename Map::iterator it = _map.lower_bound(k); deba@2489: if (it != _map.end() && !_map.key_comp()(k, it->first)) deba@2489: it->second = t; deba@2489: else deba@2489: _map.insert(it, std::make_pair(k, t)); klao@345: } klao@286: deba@2489: /// \e deba@2489: void setAll(const T &t) { deba@2489: _value = t; deba@2489: _map.clear(); deba@2489: } klao@286: deba@2489: template > klao@286: struct rebind { deba@2489: 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@1705: template deba@1705: class IdentityMap : public MapBase { deba@1531: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1531: deba@2489: /// \e deba@1675: const T& operator[](const T& t) const { deba@1531: return t; deba@1531: } deba@1531: }; alpar@1402: deba@2489: ///Returns an \c IdentityMap class deba@1675: deba@2489: ///This function just returns an \c IdentityMap class. deba@1675: ///\relates IdentityMap deba@1675: template deba@1705: inline IdentityMap identityMap() { deba@1705: return IdentityMap(); deba@1675: } deba@1675: deba@1675: alpar@1547: ///Convert the \c Value of a map to another type. alpar@1178: deba@2489: ///This \c concepts::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@1705: template deba@1705: class ConvertMap : public MapBase { deba@1705: const M& m; alpar@1178: public: deba@1705: 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: deba@2489: ///Returns an \c ConvertMap class alpar@1178: deba@2489: ///This function just returns an \c ConvertMap class. alpar@1178: ///\relates ConvertMap deba@1675: template deba@1705: inline ConvertMap convertMap(const M &m) { deba@1705: return ConvertMap(m); alpar@1178: } alpar@1041: deba@2248: ///Simple wrapping of the map deba@2248: deba@2489: ///This \c concepts::ReadMap "read only map" returns the simple deba@2248: ///wrapping of the given map. Sometimes the reference maps cannot be deba@2248: ///combined with simple read maps. This map adaptor wraps the given deba@2248: ///map to simple read map. deba@2248: template deba@2248: class SimpleMap : public MapBase { deba@2248: const M& m; deba@2248: deba@2248: public: deba@2248: typedef MapBase Parent; deba@2248: typedef typename Parent::Key Key; deba@2248: typedef typename Parent::Value Value; deba@2248: deba@2248: ///Constructor deba@2248: SimpleMap(const M &_m) : m(_m) {}; deba@2489: ///\e deba@2248: Value operator[](Key k) const {return m[k];} deba@2248: }; deba@2248: deba@2248: ///Simple writeable wrapping of the map deba@2248: deba@2489: ///This \c concepts::ReadMap "read only map" returns the simple deba@2248: ///wrapping of the given map. Sometimes the reference maps cannot be deba@2248: ///combined with simple read-write maps. This map adaptor wraps the deba@2248: ///given map to simple read-write map. deba@2248: template deba@2248: class SimpleWriteMap : public MapBase { deba@2248: M& m; deba@2248: deba@2248: public: deba@2248: typedef MapBase Parent; deba@2248: typedef typename Parent::Key Key; deba@2248: typedef typename Parent::Value Value; deba@2248: deba@2248: ///Constructor deba@2248: SimpleWriteMap(M &_m) : m(_m) {}; deba@2489: ///\e deba@2248: Value operator[](Key k) const {return m[k];} deba@2489: ///\e deba@2248: void set(Key k, const Value& c) { m.set(k, c); } deba@2248: }; deba@2248: alpar@1041: ///Sum of two maps alpar@1041: deba@2489: ///This \c concepts::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@1705: template deba@1705: class AddMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; deba@1420: alpar@1041: public: deba@1705: 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) {}; deba@2489: ///\e alpar@1044: Value operator[](Key k) const {return m1[k]+m2[k];} alpar@1041: }; alpar@1041: deba@2489: ///Returns an \c AddMap class alpar@1041: deba@2489: ///This function just returns an \c AddMap class. alpar@1041: ///\todo How to call these type of functions? alpar@1041: /// alpar@1041: ///\relates AddMap deba@1675: template deba@1705: inline AddMap addMap(const M1 &m1,const M2 &m2) { deba@1705: return AddMap(m1,m2); alpar@1041: } alpar@1041: alpar@1547: ///Shift a map with a constant. alpar@1070: deba@2489: ///This \c concepts::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@1705: template deba@1705: class ShiftMap : public MapBase { deba@1705: const M& m; deba@1691: C v; alpar@1070: public: deba@1705: 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@2489: ///\e deba@1691: Value operator[](Key k) const {return m[k] + v;} alpar@1070: }; deba@2032: deba@2032: ///Shift a map with a constant. deba@2032: deba@2489: ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the deba@2032: ///given map and a constant value. It makes also possible to write the map. deba@2032: ///Its \c Key and \c Value is inherited from \c M. deba@2032: /// deba@2032: ///Actually, deba@2032: ///\code deba@2032: /// ShiftMap sh(x,v); deba@2032: ///\endcode deba@2032: ///is equivalent with deba@2032: ///\code deba@2032: /// ConstMap c_tmp(v); deba@2032: /// AddMap > sh(x,v); deba@2032: ///\endcode deba@2032: template deba@2032: class ShiftWriteMap : public MapBase { deba@2032: M& m; deba@2032: C v; deba@2032: public: deba@2032: typedef MapBase Parent; deba@2032: typedef typename Parent::Key Key; deba@2032: typedef typename Parent::Value Value; deba@2032: deba@2032: ///Constructor deba@2032: deba@2032: ///Constructor deba@2032: ///\param _m is the undelying map deba@2032: ///\param _v is the shift value deba@2080: ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; deba@2489: /// \e deba@2032: Value operator[](Key k) const {return m[k] + v;} deba@2489: /// \e deba@2032: void set(Key k, const Value& c) { m.set(k, c - v); } deba@2032: }; alpar@1070: deba@2489: ///Returns an \c ShiftMap class alpar@1070: deba@2489: ///This function just returns an \c ShiftMap class. alpar@1070: ///\relates ShiftMap deba@1691: template deba@1705: inline ShiftMap shiftMap(const M &m,const C &v) { deba@1705: return ShiftMap(m,v); alpar@1070: } alpar@1070: deba@2032: template deba@2032: inline ShiftWriteMap shiftMap(M &m,const C &v) { deba@2032: return ShiftWriteMap(m,v); deba@2032: } deba@2032: alpar@1041: ///Difference of two maps alpar@1041: deba@2489: ///This \c concepts::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@1705: template deba@1705: class SubMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; alpar@1041: public: deba@1705: 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) {}; deba@2489: /// \e alpar@1044: Value operator[](Key k) const {return m1[k]-m2[k];} alpar@1041: }; alpar@1041: deba@2489: ///Returns a \c SubMap class alpar@1041: deba@2489: ///This function just returns a \c SubMap class. alpar@1041: /// alpar@1041: ///\relates SubMap deba@1675: template deba@1705: inline SubMap subMap(const M1 &m1, const M2 &m2) { deba@1705: return SubMap(m1, m2); alpar@1041: } alpar@1041: alpar@1041: ///Product of two maps alpar@1041: deba@2489: ///This \c concepts::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@1705: template deba@1705: class MulMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; alpar@1041: public: deba@1705: 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) {}; deba@2489: /// \e alpar@1044: Value operator[](Key k) const {return m1[k]*m2[k];} alpar@1041: }; alpar@1041: deba@2489: ///Returns a \c MulMap class alpar@1041: deba@2489: ///This function just returns a \c MulMap class. alpar@1041: ///\relates MulMap deba@1675: template deba@1705: inline MulMap mulMap(const M1 &m1,const M2 &m2) { deba@1705: return MulMap(m1,m2); alpar@1041: } alpar@1041: alpar@1547: ///Scales a maps with a constant. alpar@1070: deba@2489: ///This \c concepts::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@1705: template deba@1705: class ScaleMap : public MapBase { deba@1705: const M& m; deba@1691: C v; alpar@1070: public: deba@1705: 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@2489: /// \e deba@1691: Value operator[](Key k) const {return v * m[k];} alpar@1070: }; deba@2032: deba@2032: ///Scales a maps with a constant. deba@2032: deba@2489: ///This \c concepts::ReadWriteMap "read-write map" returns the value of the deba@2032: ///given map multiplied from the left side with a constant value. It can deba@2032: ///be used as write map also if the given multiplier is not zero. deba@2032: ///Its \c Key and \c Value is inherited from \c M. deba@2032: template deba@2032: class ScaleWriteMap : public MapBase { deba@2032: M& m; deba@2032: C v; deba@2032: public: deba@2032: typedef MapBase Parent; deba@2032: typedef typename Parent::Key Key; deba@2032: typedef typename Parent::Value Value; deba@2032: deba@2032: ///Constructor deba@2032: deba@2032: ///Constructor deba@2032: ///\param _m is the undelying map deba@2032: ///\param _v is the scaling value deba@2032: ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; deba@2489: /// \e deba@2032: Value operator[](Key k) const {return v * m[k];} deba@2489: /// \e deba@2032: void set(Key k, const Value& c) { m.set(k, c / v);} deba@2032: }; alpar@1070: deba@2489: ///Returns an \c ScaleMap class alpar@1070: deba@2489: ///This function just returns an \c ScaleMap class. alpar@1070: ///\relates ScaleMap deba@1691: template deba@1705: inline ScaleMap scaleMap(const M &m,const C &v) { deba@1705: return ScaleMap(m,v); alpar@1070: } alpar@1070: deba@2032: template deba@2032: inline ScaleWriteMap scaleMap(M &m,const C &v) { deba@2032: return ScaleWriteMap(m,v); deba@2032: } deba@2032: alpar@1041: ///Quotient of two maps alpar@1041: deba@2489: ///This \c concepts::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@1705: template deba@1705: class DivMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; alpar@1041: public: deba@1705: 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) {}; deba@2489: /// \e alpar@1044: Value operator[](Key k) const {return m1[k]/m2[k];} alpar@1041: }; alpar@1041: deba@2489: ///Returns a \c DivMap class alpar@1041: deba@2489: ///This function just returns a \c DivMap class. alpar@1041: ///\relates DivMap deba@1675: template deba@1705: inline DivMap divMap(const M1 &m1,const M2 &m2) { deba@1705: return DivMap(m1,m2); alpar@1041: } alpar@1041: alpar@1041: ///Composition of two maps alpar@1041: deba@2489: ///This \c concepts::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. deba@1705: template deba@1705: class ComposeMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; alpar@1041: public: deba@1705: 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) {}; deba@1725: deba@1725: typename MapTraits::ConstReturnValue deba@2489: /// \e deba@1725: operator[](Key k) const {return m1[m2[k]];} alpar@1041: }; deba@2489: ///Returns a \c ComposeMap class alpar@1041: deba@2489: ///This function just returns a \c ComposeMap class. alpar@1219: /// alpar@1041: ///\relates ComposeMap deba@1675: template deba@1705: inline ComposeMap composeMap(const M1 &m1,const M2 &m2) { deba@1705: 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: /// deba@2489: ///This \c concepts::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. deba@1675: template deba@1705: class CombineMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; deba@1420: F f; alpar@1219: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1219: alpar@1219: ///Constructor deba@2489: CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F()) alpar@1219: : m1(_m1), m2(_m2), f(_f) {}; deba@2489: /// \e alpar@1219: Value operator[](Key k) const {return f(m1[k],m2[k]);} alpar@1219: }; alpar@1219: deba@2489: ///Returns a \c CombineMap class alpar@1219: deba@2489: ///This function just returns a \c CombineMap class. alpar@1219: /// alpar@1219: ///For example if \c m1 and \c m2 are both \c double valued maps, then alpar@1219: ///\code deba@2489: ///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: /// deba@2489: ///This function is specialized for adaptable binary function deba@2489: ///classes and c++ functions. deba@2489: /// alpar@1219: ///\relates CombineMap deba@1675: template deba@1705: inline CombineMap deba@1675: combineMap(const M1& m1,const M2& m2, const F& f) { deba@1705: return CombineMap(m1,m2,f); deba@1675: } deba@1675: deba@1675: template deba@1705: 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@1705: 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: deba@2489: ///This \c concepts::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@1705: template deba@1705: class NegMap : public MapBase { deba@1705: const M& m; alpar@1041: public: deba@1705: 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) {}; deba@2489: /// \e alpar@1044: Value operator[](Key k) const {return -m[k];} alpar@1041: }; alpar@1041: deba@2032: ///Negative value of a map deba@2032: deba@2489: ///This \c concepts::ReadWriteMap "read-write map" returns the negative deba@2032: ///value of the value returned by the deba@2032: ///given map. Its \c Key and \c Value will be inherited from \c M. deba@2032: ///The unary \c - operator must be defined for \c Value, of course. deba@2032: deba@2032: template deba@2032: class NegWriteMap : public MapBase { deba@2032: M& m; deba@2032: public: deba@2032: typedef MapBase Parent; deba@2032: typedef typename Parent::Key Key; deba@2032: typedef typename Parent::Value Value; deba@2032: deba@2032: ///Constructor deba@2032: NegWriteMap(M &_m) : m(_m) {}; deba@2489: /// \e deba@2032: Value operator[](Key k) const {return -m[k];} deba@2489: /// \e deba@2032: void set(Key k, const Value& v) { m.set(k, -v); } deba@2032: }; deba@2032: deba@2489: ///Returns a \c NegMap class alpar@1041: deba@2489: ///This function just returns a \c NegMap class. alpar@1041: ///\relates NegMap deba@1675: template deba@1705: inline NegMap negMap(const M &m) { deba@1705: return NegMap(m); alpar@1041: } alpar@1041: deba@2032: template deba@2032: inline NegWriteMap negMap(M &m) { deba@2032: return NegWriteMap(m); deba@2032: } alpar@1041: alpar@1041: ///Absolute value of a map alpar@1041: deba@2489: ///This \c concepts::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@1705: template deba@1705: class AbsMap : public MapBase { deba@1705: const M& m; alpar@1041: public: deba@1705: 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@2489: /// \e 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: deba@2489: ///Returns a \c AbsMap class alpar@1041: deba@2489: ///This function just returns a \c AbsMap class. alpar@1041: ///\relates AbsMap deba@1675: template deba@1705: inline AbsMap absMap(const M &m) { deba@1705: return AbsMap(m); alpar@1041: } alpar@1041: alpar@1402: ///Converts an STL style functor to a map alpar@1076: deba@2489: ///This \c concepts::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. deba@1675: template deba@1705: class FunctorMap : public MapBase { deba@1679: F f; alpar@1076: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1076: alpar@1076: ///Constructor deba@2489: FunctorMap(const F &_f = F()) : f(_f) {} deba@2489: /// \e deba@1679: Value operator[](Key k) const { return f(k);} alpar@1076: }; alpar@1076: deba@2489: ///Returns a \c FunctorMap class alpar@1076: deba@2489: ///This function just returns a \c FunctorMap class. alpar@1076: /// deba@2489: ///It is specialized for adaptable function classes and deba@2489: ///c++ functions. alpar@1076: ///\relates FunctorMap deba@1675: template inline deba@1705: FunctorMap functorMap(const F &f) { deba@1705: return FunctorMap(f); alpar@1076: } alpar@1076: deba@1675: template inline deba@1705: FunctorMap deba@1675: functorMap(const F &f) { deba@1679: return FunctorMap(f); deba@1675: } deba@1675: deba@1675: template inline deba@1705: FunctorMap functorMap(V (*f)(K)) { deba@1705: 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 deba@2489: ///a ususal \c concepts::ReadMap "readable map", alpar@1537: ///i.e. operator[] and the \c Key and \c Value typedefs also exist. deba@1705: template deba@1705: class MapFunctor : public MapBase { deba@1705: const M& m; alpar@1076: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; deba@1420: alpar@1223: typedef typename M::Key argument_type; alpar@1223: typedef typename M::Value result_type; alpar@1076: alpar@1076: ///Constructor alpar@1076: MapFunctor(const M &_m) : m(_m) {}; deba@2489: ///\e 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: deba@2489: ///Returns a \c MapFunctor class alpar@1076: deba@2489: ///This function just returns a \c MapFunctor class. alpar@1076: ///\relates MapFunctor deba@1675: template deba@1705: inline MapFunctor mapFunctor(const M &m) { deba@1705: return MapFunctor(m); alpar@1076: } alpar@1076: alpar@1547: ///Applies all map setting operations to two maps alpar@1219: deba@2489: ///This map has two \c concepts::ReadMap "readable map" deba@2032: ///parameters and each read request will be passed just to the deba@2032: ///first map. This class is the just readable map type of the ForkWriteMap. 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. deba@1705: template deba@1705: class ForkMap : public MapBase { deba@1705: const M1& m1; deba@1705: const M2& m2; alpar@1219: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1219: alpar@1219: ///Constructor deba@2032: ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; deba@2489: /// \e alpar@1219: Value operator[](Key k) const {return m1[k];} deba@2032: }; deba@2032: deba@2032: deba@2032: ///Applies all map setting operations to two maps deba@2032: deba@2489: ///This map has two \c concepts::WriteMap "writable map" deba@2032: ///parameters and each write request will be passed to both of them. deba@2489: ///If \c M1 is also \c concepts::ReadMap "readable", deba@2032: ///then the read operations will return the deba@2032: ///corresponding values of \c M1. deba@2032: /// deba@2032: ///The \c Key and \c Value will be inherited from \c M1. deba@2032: ///The \c Key and \c Value of M2 must be convertible from those of \c M1. deba@2032: template deba@2032: class ForkWriteMap : public MapBase { deba@2032: M1& m1; deba@2032: M2& m2; deba@2032: public: deba@2032: typedef MapBase Parent; deba@2032: typedef typename Parent::Key Key; deba@2032: typedef typename Parent::Value Value; deba@2032: deba@2032: ///Constructor deba@2032: ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; deba@2489: ///\e deba@2032: Value operator[](Key k) const {return m1[k];} deba@2489: ///\e deba@2032: void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} alpar@1219: }; alpar@1219: deba@2489: ///Returns an \c ForkMap class alpar@1219: deba@2489: ///This function just returns an \c ForkMap class. alpar@1219: /// alpar@1219: ///\relates ForkMap deba@1675: template deba@2032: inline ForkMap forkMap(const M1 &m1, const M2 &m2) { deba@1705: return ForkMap(m1,m2); alpar@1219: } alpar@1219: deba@2032: template deba@2032: inline ForkWriteMap forkMap(M1 &m1, M2 &m2) { deba@2032: return ForkWriteMap(m1,m2); deba@2032: } deba@2032: alpar@1456: alpar@1456: alpar@1456: /* ************* BOOL MAPS ******************* */ alpar@1456: alpar@1456: ///Logical 'not' of a map alpar@1456: deba@2489: ///This bool \c concepts::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. deba@1705: template deba@1705: class NotMap : public MapBase { deba@1705: const M& m; alpar@1456: public: deba@1705: typedef MapBase Parent; deba@1675: typedef typename Parent::Key Key; deba@1675: typedef typename Parent::Value Value; alpar@1456: deba@1778: /// Constructor alpar@1456: NotMap(const M &_m) : m(_m) {}; deba@2489: ///\e alpar@1456: Value operator[](Key k) const {return !m[k];} alpar@1456: }; deba@2032: deba@2032: ///Logical 'not' of a map with writing possibility deba@2032: deba@2489: ///This bool \c concepts::ReadWriteMap "read-write map" returns the alpar@2258: ///logical negation of value returned by the given map. When it is set, alpar@2258: ///the opposite value is set to the original map. deba@2032: ///Its \c Key and will be inherited from \c M, deba@2032: ///its Value is bool. deba@2032: template deba@2032: class NotWriteMap : public MapBase { deba@2032: M& m; deba@2032: public: deba@2032: typedef MapBase Parent; deba@2032: typedef typename Parent::Key Key; deba@2032: typedef typename Parent::Value Value; deba@2032: deba@2032: /// Constructor deba@2032: NotWriteMap(M &_m) : m(_m) {}; deba@2489: ///\e deba@2032: Value operator[](Key k) const {return !m[k];} deba@2489: ///\e deba@2032: void set(Key k, bool v) { m.set(k, !v); } deba@2032: }; alpar@1456: deba@2489: ///Returns a \c NotMap class alpar@1456: deba@2489: ///This function just returns a \c NotMap class. alpar@1456: ///\relates NotMap deba@1675: template deba@1705: inline NotMap notMap(const M &m) { deba@1705: return NotMap(m); alpar@1456: } deba@2423: deba@2032: template deba@2032: inline NotWriteMap notMap(M &m) { deba@2032: return NotWriteMap(m); deba@2032: } deba@2032: deba@2091: namespace _maps_bits { deba@2423: deba@2091: template deba@2091: struct Identity { deba@2091: typedef Value argument_type; deba@2091: typedef Value result_type; deba@2423: Value operator()(const Value& val) const { deba@2091: return val; deba@2091: } deba@2091: }; deba@2423: deba@2423: template deba@2423: struct IteratorTraits { deba@2423: typedef typename std::iterator_traits<_Iterator>::value_type Value; deba@2423: }; deba@2423: deba@2423: template deba@2423: struct IteratorTraits<_Iterator, deba@2423: typename exists::type> deba@2423: { deba@2423: typedef typename _Iterator::container_type::value_type Value; deba@2423: }; deba@2423: deba@2091: } deba@2091: deba@2091: alpar@1808: /// \brief Writable bool map for store each true assigned elements. deba@1778: /// alpar@2258: /// Writable bool map to store each true assigned elements. It will alpar@2258: /// copies all the keys set to true to the given iterator. deba@1778: /// deba@2091: /// \note The container of the iterator should contain space deba@2091: /// for each element. deba@2091: /// deba@2091: /// The next example shows how can you write the nodes directly deba@2091: /// to the standard output. deba@2091: ///\code deba@2091: /// typedef IdMap UEdgeIdMap; deba@2091: /// UEdgeIdMap uedgeId(ugraph); deba@2091: /// deba@2091: /// typedef MapFunctor UEdgeIdFunctor; deba@2091: /// UEdgeIdFunctor uedgeIdFunctor(uedgeId); deba@2091: /// deba@2091: /// StoreBoolMap, UEdgeIdFunctor> deba@2091: /// writerMap(ostream_iterator(cout, " "), uedgeIdFunctor); deba@2091: /// deba@2091: /// prim(ugraph, cost, writerMap); deba@2091: ///\endcode deba@2091: template ::Value> > deba@1778: class StoreBoolMap { deba@1778: public: deba@1778: typedef _Iterator Iterator; deba@1778: deba@2091: typedef typename _Functor::argument_type Key; deba@1778: typedef bool Value; deba@1778: deba@2091: typedef _Functor Functor; deba@2091: deba@1778: /// Constructor deba@2091: StoreBoolMap(Iterator it, const Functor& functor = Functor()) deba@2091: : _begin(it), _end(it), _functor(functor) {} deba@1778: alpar@2258: /// Gives back the given iterator set for the first time. deba@1778: Iterator begin() const { deba@1778: return _begin; deba@1778: } deba@1778: alpar@2258: /// Gives back the iterator after the last set operation. deba@1778: Iterator end() const { deba@1778: return _end; deba@1778: } deba@1778: deba@1778: /// Setter function of the map deba@2423: void set(const Key& key, Value value) const { deba@1778: if (value) { deba@2091: *_end++ = _functor(key); deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@2423: Iterator _begin; deba@2423: mutable Iterator _end; deba@2091: Functor _functor; deba@1778: }; deba@1778: alpar@1808: /// \brief Writable bool map for store each true assigned elements in deba@1778: /// a back insertable container. deba@1778: /// alpar@1808: /// Writable bool map for store each true assigned elements in a back alpar@2258: /// insertable container. It will push back all the keys set to true into deba@2091: /// the container. It can be used to retrieve the items into a standard deba@2091: /// container. The next example shows how can you store the undirected deba@2091: /// edges in a vector with prim algorithm. deba@2091: /// deba@2091: ///\code deba@2091: /// vector span_tree_uedges; deba@2091: /// BackInserterBoolMap > inserter_map(span_tree_uedges); deba@2091: /// prim(ugraph, cost, inserter_map); deba@2091: ///\endcode deba@2091: template > deba@1778: class BackInserterBoolMap { deba@1778: public: deba@1778: typedef typename Container::value_type Key; deba@1778: typedef bool Value; deba@1778: deba@1778: /// Constructor deba@2091: BackInserterBoolMap(Container& _container, deba@2091: const Functor& _functor = Functor()) deba@2091: : container(_container), functor(_functor) {} deba@1778: deba@1778: /// Setter function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { deba@2091: container.push_back(functor(key)); deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@2091: Container& container; deba@2091: Functor functor; deba@1778: }; deba@1778: alpar@1808: /// \brief Writable bool map for store each true assigned elements in deba@1778: /// a front insertable container. deba@1778: /// alpar@1808: /// Writable bool map for store each true assigned elements in a front alpar@2258: /// insertable container. It will push front all the keys set to \c true into deba@2091: /// the container. For example see the BackInserterBoolMap. deba@2091: template > deba@1778: class FrontInserterBoolMap { deba@1778: public: deba@1778: typedef typename Container::value_type Key; deba@1778: typedef bool Value; deba@1778: deba@1778: /// Constructor deba@2091: FrontInserterBoolMap(Container& _container, deba@2091: const Functor& _functor = Functor()) deba@2091: : container(_container), functor(_functor) {} deba@1778: deba@1778: /// Setter function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { deba@1778: container.push_front(key); deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@1778: Container& container; deba@2091: Functor functor; deba@1778: }; deba@1778: alpar@1808: /// \brief Writable bool map for store each true assigned elements in deba@1778: /// an insertable container. deba@1778: /// alpar@1808: /// Writable bool map for store each true assigned elements in an alpar@2258: /// insertable container. It will insert all the keys set to \c true into deba@2091: /// the container. If you want to store the cut edges of the strongly deba@2091: /// connected components in a set you can use the next code: deba@2091: /// deba@2091: ///\code deba@2091: /// set cut_edges; deba@2091: /// InserterBoolMap > inserter_map(cut_edges); deba@2091: /// stronglyConnectedCutEdges(graph, cost, inserter_map); deba@2091: ///\endcode deba@2091: template > deba@1778: class InserterBoolMap { deba@1778: public: deba@1778: typedef typename Container::value_type Key; deba@1778: typedef bool Value; deba@1778: deba@1778: /// Constructor deba@2091: InserterBoolMap(Container& _container, typename Container::iterator _it, deba@2091: const Functor& _functor = Functor()) deba@2091: : container(_container), it(_it), functor(_functor) {} deba@2091: deba@2091: /// Constructor deba@2091: InserterBoolMap(Container& _container, const Functor& _functor = Functor()) deba@2091: : container(_container), it(_container.end()), functor(_functor) {} deba@1778: deba@1778: /// Setter function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { deba@2091: it = container.insert(it, key); deba@2091: ++it; deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@2091: Container& container; deba@2091: typename Container::iterator it; deba@2091: Functor functor; deba@1778: }; deba@1778: alpar@2258: /// \brief Fill the true set elements with a given value. deba@1778: /// alpar@2258: /// Writable bool map to fill the elements set to \c true with a given value. alpar@2258: /// The value can set deba@1778: /// the container. deba@2091: /// deba@2091: /// The next code finds the connected components of the undirected graph deba@2091: /// and stores it in the \c comp map: deba@2091: ///\code deba@2091: /// typedef UGraph::NodeMap ComponentMap; deba@2091: /// ComponentMap comp(ugraph); deba@2091: /// typedef FillBoolMap > ComponentFillerMap; deba@2091: /// ComponentFillerMap filler(comp, 0); deba@2091: /// deba@2091: /// Dfs::DefProcessedMap::Create dfs(ugraph); deba@2091: /// dfs.processedMap(filler); deba@2091: /// dfs.init(); deba@2091: /// for (NodeIt it(ugraph); it != INVALID; ++it) { deba@2091: /// if (!dfs.reached(it)) { deba@2091: /// dfs.addSource(it); deba@2091: /// dfs.start(); deba@2091: /// ++filler.fillValue(); deba@2091: /// } deba@2091: /// } deba@2091: ///\endcode deba@1778: template deba@1778: class FillBoolMap { deba@1778: public: deba@1778: typedef typename Map::Key Key; deba@1778: typedef bool Value; deba@1778: deba@1778: /// Constructor deba@1778: FillBoolMap(Map& _map, const typename Map::Value& _fill) deba@1778: : map(_map), fill(_fill) {} deba@1778: deba@1778: /// Constructor deba@1778: FillBoolMap(Map& _map) deba@1778: : map(_map), fill() {} deba@1778: deba@1778: /// Gives back the current fill value deba@2091: const typename Map::Value& fillValue() const { deba@2091: return fill; deba@2091: } deba@2091: deba@2091: /// Gives back the current fill value deba@2091: typename Map::Value& fillValue() { deba@1778: return fill; deba@1778: } deba@1778: deba@1778: /// Sets the current fill value deba@1778: void fillValue(const typename Map::Value& _fill) { deba@1778: fill = _fill; deba@1778: } deba@1778: deba@1778: /// Setter function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { deba@1778: map.set(key, fill); deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@1778: Map& map; deba@1778: typename Map::Value fill; deba@1778: }; deba@1778: deba@1778: alpar@1808: /// \brief Writable bool map which stores for each true assigned elements deba@1778: /// the setting order number. deba@1778: /// alpar@1808: /// Writable bool map which stores for each true assigned elements deba@2091: /// the setting order number. It make easy to calculate the leaving deba@2489: /// order of the nodes in the \c Dfs algorithm. deba@2091: /// deba@2091: ///\code deba@2091: /// typedef Graph::NodeMap OrderMap; deba@2091: /// OrderMap order(graph); deba@2091: /// typedef SettingOrderBoolMap OrderSetterMap; deba@2091: /// OrderSetterMap setter(order); deba@2091: /// Dfs::DefProcessedMap::Create dfs(graph); deba@2091: /// dfs.processedMap(setter); deba@2091: /// dfs.init(); deba@2091: /// for (NodeIt it(graph); it != INVALID; ++it) { deba@2091: /// if (!dfs.reached(it)) { deba@2091: /// dfs.addSource(it); deba@2091: /// dfs.start(); deba@2091: /// } deba@2091: /// } deba@2091: ///\endcode deba@2091: /// deba@2091: /// The discovering order can be stored a little harder because the deba@2091: /// ReachedMap should be readable in the dfs algorithm but the setting deba@2091: /// order map is not readable. Now we should use the fork map: deba@2091: /// deba@2091: ///\code deba@2091: /// typedef Graph::NodeMap OrderMap; deba@2091: /// OrderMap order(graph); deba@2091: /// typedef SettingOrderBoolMap OrderSetterMap; deba@2091: /// OrderSetterMap setter(order); deba@2091: /// typedef Graph::NodeMap StoreMap; deba@2091: /// StoreMap store(graph); deba@2091: /// deba@2091: /// typedef ForkWriteMap ReachedMap; deba@2091: /// ReachedMap reached(store, setter); deba@2091: /// deba@2091: /// Dfs::DefReachedMap::Create dfs(graph); deba@2091: /// dfs.reachedMap(reached); deba@2091: /// dfs.init(); deba@2091: /// for (NodeIt it(graph); it != INVALID; ++it) { deba@2091: /// if (!dfs.reached(it)) { deba@2091: /// dfs.addSource(it); deba@2091: /// dfs.start(); deba@2091: /// } deba@2091: /// } deba@2091: ///\endcode deba@1778: template deba@1778: class SettingOrderBoolMap { deba@1778: public: deba@1778: typedef typename Map::Key Key; deba@1778: typedef bool Value; deba@1778: deba@1778: /// Constructor deba@1778: SettingOrderBoolMap(Map& _map) deba@1778: : map(_map), counter(0) {} deba@1778: alpar@2258: /// Number of set operations. deba@1778: int num() const { deba@1778: return counter; deba@1778: } deba@1778: deba@1778: /// Setter function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { deba@1778: map.set(key, counter++); deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@1778: Map& map; deba@1778: int counter; deba@1778: }; deba@1778: alpar@1041: /// @} klao@286: } alpar@1041: alpar@921: #endif // LEMON_MAPS_H