alpar@906: /* -*- C++ -*- alpar@906: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2553: * Copyright (C) 2003-2008 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@2511: #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: kpeter@2564: /// The key type of the map. alpar@987: typedef K Key; kpeter@2564: /// The value type of the map. (The type of objects associated with the keys). alpar@987: typedef T Value; alpar@720: }; alpar@720: alpar@805: /// Null map. (a.k.a. DoNothingMap) klao@286: kpeter@2564: /// This map can be used if you have to provide a map only for kpeter@2564: /// its type definitions, or if you have to provide a writable map, kpeter@2564: /// but data written to it is not required (i.e. it will be sent to kpeter@2564: /// /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: kpeter@2564: ///Returns a \c NullMap class kpeter@2564: kpeter@2564: ///This function just returns a \c NullMap class. kpeter@2564: ///\relates NullMap deba@1420: template deba@1705: NullMap nullMap() { deba@1705: return NullMap(); deba@1420: } deba@1420: klao@286: klao@286: /// Constant map. klao@286: kpeter@2564: /// This is a \ref concepts::ReadMap "readable" map which assigns a kpeter@2564: /// specified value to each key. kpeter@2564: /// In other aspects it is equivalent to \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: kpeter@2564: /// Default constructor. alpar@805: /// The value of the map will be uninitialized. alpar@805: /// (More exactly it will be default constructed.) klao@286: ConstMap() {} kpeter@2564: kpeter@2564: /// Constructor with specified initial value alpar@805: kpeter@2564: /// Constructor with specified initial value. kpeter@2564: /// \param _v is the initial value of the map. 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: kpeter@2564: /// This is a \ref concepts::ReadMap "readable" map which assigns a kpeter@2564: /// specified value to each key. kpeter@2564: /// In other aspects it is equivalent to \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: kpeter@2564: ///Returns a \c ConstMap class with inlined value 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: kpeter@2564: ///Map based on \c std::map klao@286: kpeter@2564: ///This is essentially a wrapper for \c std::map with addition that deba@2489: ///you can specify a default value different from \c Value() . kpeter@2564: ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept. alpar@987: template > kpeter@2564: class StdMap : public MapBase { deba@2489: template deba@2489: friend class StdMap; deba@2489: public: klao@286: kpeter@2564: typedef MapBase Parent; kpeter@2564: ///Key type kpeter@2564: typedef typename Parent::Key Key; kpeter@2564: ///Value type kpeter@2564: typedef typename Parent::Value Value; kpeter@2564: ///Reference Type kpeter@2564: typedef T& Reference; kpeter@2564: ///Const reference type kpeter@2564: typedef const T& ConstReference; kpeter@2564: deba@2489: typedef True ReferenceMapTag; 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) {} kpeter@2564: /// \brief Constructs the map from an appropriate \c std::map, and kpeter@2564: /// explicitly 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: kpeter@2564: /// \brief Constructs a map from an other \ref 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: kpeter@2564: ///Returns a \c StdMap class kpeter@2564: kpeter@2564: ///This function just returns a \c StdMap class with specified kpeter@2564: ///default value. kpeter@2564: ///\relates StdMap kpeter@2564: template kpeter@2564: inline StdMap stdMap(const V& value = V()) { kpeter@2564: return StdMap(value); kpeter@2564: } kpeter@2564: kpeter@2564: template kpeter@2564: inline StdMap > stdMap(const V& value = V()) { kpeter@2564: return StdMap >(value); kpeter@2564: } kpeter@2564: kpeter@2564: ///Returns a \c StdMap class created from an appropriate \c std::map kpeter@2564: kpeter@2564: ///This function just returns a \c StdMap class created from an kpeter@2564: ///appropriate \c std::map. kpeter@2564: ///\relates StdMap kpeter@2564: template kpeter@2564: inline StdMap stdMap( const std::map &map, kpeter@2564: const V& value = V() ) { kpeter@2564: return StdMap(map, value); kpeter@2564: } kpeter@2564: kpeter@2564: /// \brief Map for storing values for keys from the range [0..size-1] deba@2511: /// kpeter@2564: /// This map has the [0..size-1] keyset and the values deba@2511: /// are stored in a \c std::vector container. It can be used with deba@2511: /// some data structures, for example \c UnionFind, \c BinHeap, when deba@2511: /// the used items are small integer numbers. deba@2511: template kpeter@2564: class IntegerMap : public MapBase { deba@2511: deba@2511: template deba@2511: friend class IntegerMap; deba@2511: deba@2511: public: deba@2511: kpeter@2564: typedef MapBase Parent; deba@2511: ///\e kpeter@2564: typedef typename Parent::Key Key; deba@2511: ///\e kpeter@2564: typedef typename Parent::Value Value; deba@2511: ///\e deba@2511: typedef T& Reference; deba@2511: ///\e deba@2511: typedef const T& ConstReference; deba@2511: kpeter@2564: typedef True ReferenceMapTag; kpeter@2564: deba@2511: private: deba@2511: deba@2511: typedef std::vector Vector; deba@2511: Vector _vector; deba@2511: deba@2511: public: deba@2511: deba@2511: /// Constructor with specified default value deba@2511: IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} deba@2511: kpeter@2564: /// \brief Constructs the map from an appropriate \c std::vector. deba@2511: template deba@2511: IntegerMap(const std::vector& vector) deba@2511: : _vector(vector.begin(), vector.end()) {} deba@2511: kpeter@2564: /// \brief Constructs a map from an other \ref IntegerMap. deba@2511: template deba@2511: IntegerMap(const IntegerMap &c) deba@2511: : _vector(c._vector.begin(), c._vector.end()) {} deba@2511: deba@2511: /// \brief Resize the container deba@2511: void resize(int size, const T& value = T()) { deba@2511: _vector.resize(size, value); deba@2511: } deba@2511: deba@2511: private: deba@2511: deba@2511: IntegerMap& operator=(const IntegerMap&); deba@2511: deba@2511: public: deba@2511: deba@2511: ///\e deba@2511: Reference operator[](Key k) { deba@2511: return _vector[k]; deba@2511: } deba@2511: deba@2511: /// \e deba@2511: ConstReference operator[](Key k) const { deba@2511: return _vector[k]; deba@2511: } deba@2511: deba@2511: /// \e deba@2511: void set(const Key &k, const T& t) { deba@2511: _vector[k] = t; deba@2511: } deba@2511: deba@2511: }; deba@2511: kpeter@2564: ///Returns an \c IntegerMap class kpeter@2564: kpeter@2564: ///This function just returns an \c IntegerMap class. kpeter@2564: ///\relates IntegerMap kpeter@2564: template kpeter@2564: inline IntegerMap integerMap(int size = 0, const T& value = T()) { kpeter@2564: return IntegerMap(size, value); kpeter@2564: } kpeter@2564: alpar@1402: /// @} alpar@1402: alpar@1402: /// \addtogroup map_adaptors alpar@1402: /// @{ alpar@1402: kpeter@2564: /// \brief Identity map. deba@1531: /// kpeter@2564: /// This map 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: kpeter@2564: ///\brief Convert the \c Value of a map to another type using kpeter@2564: ///the default conversion. kpeter@2564: /// kpeter@2564: ///This \ref concepts::ReadMap "read only map" kpeter@2564: ///converts the \c Value of a map 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: kpeter@2564: ///\e deba@1675: Value operator[](const Key& k) const {return m[k];} alpar@1178: }; alpar@1178: kpeter@2564: ///Returns a \c ConvertMap class alpar@1178: kpeter@2564: ///This function just returns a \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: kpeter@2564: ///Simple wrapping of a map deba@2248: kpeter@2564: ///This \ref 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. kpeter@2564: /// kpeter@2564: ///\sa SimpleWriteMap kpeter@2564: /// kpeter@2564: /// \todo Revise the misleading name 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: kpeter@2564: ///Returns a \c SimpleMap class deba@2248: kpeter@2564: ///This function just returns a \c SimpleMap class. kpeter@2564: ///\relates SimpleMap kpeter@2564: template kpeter@2564: inline SimpleMap simpleMap(const M &m) { kpeter@2564: return SimpleMap(m); kpeter@2564: } kpeter@2564: kpeter@2564: ///Simple writable wrapping of a map kpeter@2564: kpeter@2564: ///This \ref concepts::ReadWriteMap "read-write 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. kpeter@2564: /// kpeter@2564: ///\sa SimpleMap kpeter@2564: /// kpeter@2564: /// \todo Revise the misleading name 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: kpeter@2564: ///Returns a \c SimpleWriteMap class kpeter@2564: kpeter@2564: ///This function just returns a \c SimpleWriteMap class. kpeter@2564: ///\relates SimpleWriteMap kpeter@2564: template kpeter@2564: inline SimpleWriteMap simpleWriteMap(M &m) { kpeter@2564: return SimpleWriteMap(m); kpeter@2564: } kpeter@2564: alpar@1041: ///Sum of two maps alpar@1041: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the sum of the two kpeter@2564: ///given maps. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M1. kpeter@2564: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. 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: kpeter@2564: ///This \ref 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 kpeter@2564: ///is equivalent to alpar@1070: ///\code alpar@1070: /// ConstMap c_tmp(v); alpar@1070: /// AddMap > sh(x,v); alpar@1070: ///\endcode kpeter@2564: /// kpeter@2564: ///\sa ShiftWriteMap 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: kpeter@2564: ///Shift a map with a constant (ReadWrite version). deba@2032: kpeter@2564: ///This \ref 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. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M. deba@2032: /// kpeter@2564: ///\sa ShiftMap 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: kpeter@2564: ///Returns a \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: kpeter@2564: ///Returns a \c ShiftWriteMap class kpeter@2564: kpeter@2564: ///This function just returns a \c ShiftWriteMap class. kpeter@2564: ///\relates ShiftWriteMap 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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the difference kpeter@2564: ///of the values of the two given maps. kpeter@2564: ///Its \c Key and \c Value are 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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the product of the kpeter@2564: ///values of the two given maps. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M1. alpar@1041: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. 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: kpeter@2564: ///Scales a map with a constant. alpar@1070: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the value of the deba@1691: ///given map multiplied from the left side with a constant value. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M. alpar@1070: /// alpar@1070: ///Actually, alpar@1070: ///\code alpar@1070: /// ScaleMap sc(x,v); alpar@1070: ///\endcode kpeter@2564: ///is equivalent to alpar@1070: ///\code alpar@1070: /// ConstMap c_tmp(v); alpar@1070: /// MulMap > sc(x,v); alpar@1070: ///\endcode kpeter@2564: /// kpeter@2564: ///\sa ScaleWriteMap 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: kpeter@2564: ///Scales a map with a constant (ReadWrite version). deba@2032: kpeter@2564: ///This \ref 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 kpeter@2564: ///also be used as write map if the \c / operator is defined between kpeter@2564: ///\c Value and \c C and the given multiplier is not zero. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M. kpeter@2564: /// kpeter@2564: ///\sa ScaleMap 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: kpeter@2564: ///Returns a \c ScaleMap class alpar@1070: kpeter@2564: ///This function just returns a \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: kpeter@2564: ///Returns a \c ScaleWriteMap class kpeter@2564: kpeter@2564: ///This function just returns a \c ScaleWriteMap class. kpeter@2564: ///\relates ScaleWriteMap 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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the quotient of the kpeter@2564: ///values of the two given maps. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M1. alpar@1041: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. 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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the composition of kpeter@2564: ///two given maps. kpeter@2564: ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2, alpar@1041: ///then for alpar@1041: ///\code deba@1675: /// ComposeMap cm(m1,m2); alpar@1041: ///\endcode kpeter@2564: /// cm[x] will be equal to m1[m2[x]]. alpar@1041: /// kpeter@2564: ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1. kpeter@2564: ///\c M2::Value must be convertible to \c M1::Key. kpeter@2564: /// kpeter@2564: ///\sa CombineMap kpeter@2564: /// 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: kpeter@2564: ///Combine of two maps using an STL (binary) functor. alpar@1219: kpeter@2564: ///Combine of two maps using an STL (binary) functor. alpar@1219: /// kpeter@2564: ///This \ref concepts::ReadMap "read only map" takes two maps and a kpeter@2564: ///binary functor and returns the composition of 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 kpeter@2564: ///and \c f is of \c F, 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. kpeter@2564: ///\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. kpeter@2564: /// kpeter@2564: ///\sa ComposeMap kpeter@2564: /// 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 kpeter@2564: ///combineMap(m1,m2,std::plus()) alpar@1219: ///\endcode kpeter@2564: ///is equivalent to alpar@1219: ///\code alpar@1219: ///addMap(m1,m2) alpar@1219: ///\endcode alpar@1219: /// deba@2489: ///This function is specialized for adaptable binary function kpeter@2564: ///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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the negative kpeter@2564: ///value of the value returned by the given map. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M. alpar@1041: ///The unary \c - operator must be defined for \c Value, of course. kpeter@2564: /// kpeter@2564: ///\sa NegWriteMap 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: kpeter@2564: ///Negative value of a map (ReadWrite version) deba@2032: kpeter@2564: ///This \ref concepts::ReadWriteMap "read-write map" returns the negative kpeter@2564: ///value of the value returned by the given map. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M. deba@2032: ///The unary \c - operator must be defined for \c Value, of course. kpeter@2564: /// kpeter@2564: /// \sa NegMap 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: kpeter@2564: ///Returns a \c NegWriteMap class kpeter@2564: kpeter@2564: ///This function just returns a \c NegWriteMap class. kpeter@2564: ///\relates NegWriteMap 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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the absolute value kpeter@2564: ///of the value returned by the given map. kpeter@2564: ///Its \c Key and \c Value are inherited from \c M. kpeter@2564: ///\c Value must be comparable to \c 0 and the unary \c - 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: kpeter@2564: ///Returns an \c AbsMap class alpar@1041: kpeter@2564: ///This function just returns an \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: kpeter@2564: ///This \ref concepts::ReadMap "read only map" returns the value kpeter@2564: ///of a given functor. alpar@1076: /// alpar@1076: ///Template parameters \c K and \c V will become its kpeter@2564: ///\c Key and \c Value. kpeter@2564: ///In most cases they have to be given explicitly because a kpeter@2564: ///functor typically does not provide \c argument_type and kpeter@2564: ///\c result_type typedefs. alpar@1076: /// alpar@1076: ///Parameter \c F is the type of the used functor. kpeter@2564: /// kpeter@2564: ///\sa MapFunctor 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: /// kpeter@2564: ///This function is specialized for adaptable binary function kpeter@2564: ///classes and C++ functions. kpeter@2564: /// 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. kpeter@2564: ///That is it provides an operator() to read its values. alpar@1076: /// alpar@1223: ///For the sake of convenience it also works as kpeter@2564: ///a ususal \ref concepts::ReadMap "readable map", alpar@1537: ///i.e. operator[] and the \c Key and \c Value typedefs also exist. kpeter@2564: /// kpeter@2564: ///\sa FunctorMap 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: kpeter@2564: ///Just readable version of \ref ForkWriteMap alpar@1219: kpeter@2564: ///This map has two \ref concepts::ReadMap "readable map" deba@2032: ///parameters and each read request will be passed just to the kpeter@2564: ///first map. This class is the just readable map type of \c ForkWriteMap. alpar@1219: /// kpeter@2564: ///The \c Key and \c Value are inherited from \c M1. kpeter@2564: ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1. kpeter@2564: /// kpeter@2564: ///\sa ForkWriteMap kpeter@2564: 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: kpeter@2564: ///This map has two \ref concepts::WriteMap "writable map" deba@2032: ///parameters and each write request will be passed to both of them. kpeter@2564: ///If \c M1 is also \ref concepts::ReadMap "readable", deba@2032: ///then the read operations will return the deba@2032: ///corresponding values of \c M1. deba@2032: /// kpeter@2564: ///The \c Key and \c Value are inherited from \c M1. kpeter@2564: ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1. kpeter@2564: /// kpeter@2564: ///\sa ForkMap 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: kpeter@2564: ///Returns a \c ForkMap class alpar@1219: kpeter@2564: ///This function just returns a \c ForkMap class. 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: kpeter@2564: ///Returns a \c ForkWriteMap class kpeter@2564: kpeter@2564: ///This function just returns a \c ForkWriteMap class. kpeter@2564: ///\relates ForkWriteMap 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: kpeter@2564: ///This bool \ref concepts::ReadMap "read only map" returns the kpeter@2564: ///logical negation of the value returned by the given map. kpeter@2564: ///Its \c Key is inherited from \c M, its \c Value is \c bool. kpeter@2564: /// kpeter@2564: ///\sa NotWriteMap 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: kpeter@2564: ///Logical 'not' of a map (ReadWrie version) deba@2032: kpeter@2564: ///This bool \ref concepts::ReadWriteMap "read-write map" returns the kpeter@2564: ///logical negation of the value returned by the given map. When it is set, alpar@2258: ///the opposite value is set to the original map. kpeter@2564: ///Its \c Key is inherited from \c M, its \c Value is \c bool. kpeter@2564: /// kpeter@2564: ///\sa NotMap 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: kpeter@2564: ///Returns a \c NotWriteMap class kpeter@2564: kpeter@2564: ///This function just returns a \c NotWriteMap class. kpeter@2564: ///\relates NotWriteMap 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: kpeter@2564: /// \brief Writable bool map for logging each \c true assigned element deba@1778: /// kpeter@2564: /// A \ref concepts::ReadWriteMap "read-write" bool map for logging kpeter@2564: /// each \c true assigned element, i.e it copies all the keys set kpeter@2564: /// to \c 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: /// kpeter@2564: /// The following example shows how you can write the edges found by kpeter@2564: /// the \ref Prim algorithm directly 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 kpeter@2564: /// kpeter@2564: ///\sa BackInserterBoolMap kpeter@2564: ///\sa FrontInserterBoolMap kpeter@2564: ///\sa InserterBoolMap 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: kpeter@2564: /// Gives back the given iterator set for the first key deba@1778: Iterator begin() const { deba@1778: return _begin; deba@1778: } deba@1778: kpeter@2564: /// Gives back the the 'after the last' iterator deba@1778: Iterator end() const { deba@1778: return _end; deba@1778: } deba@1778: kpeter@2564: /// The \c set 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: kpeter@2564: /// \brief Writable bool map for logging each \c true assigned element in deba@1778: /// a back insertable container. deba@1778: /// kpeter@2564: /// Writable bool map for logging each \c true assigned element by pushing kpeter@2564: /// them into a back insertable container. kpeter@2564: /// It can be used to retrieve the items into a standard kpeter@2564: /// container. The next example shows how you can store the kpeter@2564: /// edges found by the Prim algorithm in a vector. 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 kpeter@2564: /// kpeter@2564: ///\sa StoreBoolMap kpeter@2564: ///\sa FrontInserterBoolMap kpeter@2564: ///\sa InserterBoolMap deba@2091: template > deba@1778: class BackInserterBoolMap { deba@1778: public: kpeter@2564: typedef typename Functor::argument_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: kpeter@2564: /// The \c set 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: kpeter@2564: /// \brief Writable bool map for logging each \c true assigned element in deba@1778: /// a front insertable container. deba@1778: /// kpeter@2564: /// Writable bool map for logging each \c true assigned element by pushing kpeter@2564: /// them into a front insertable container. kpeter@2564: /// It can be used to retrieve the items into a standard kpeter@2564: /// container. For example see \ref BackInserterBoolMap. kpeter@2564: /// kpeter@2564: ///\sa BackInserterBoolMap kpeter@2564: ///\sa InserterBoolMap deba@2091: template > deba@1778: class FrontInserterBoolMap { deba@1778: public: kpeter@2564: typedef typename Functor::argument_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: kpeter@2564: /// The \c set function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { kpeter@2564: container.push_front(functor(key)); deba@1778: } deba@1778: } deba@1778: deba@1778: private: deba@1778: Container& container; deba@2091: Functor functor; deba@1778: }; deba@1778: kpeter@2564: /// \brief Writable bool map for storing each \c true assigned element in deba@1778: /// an insertable container. deba@1778: /// kpeter@2564: /// Writable bool map for storing each \c true assigned element in an alpar@2258: /// insertable container. It will insert all the keys set to \c true into kpeter@2564: /// the container. kpeter@2564: /// kpeter@2564: /// For example, if you want to store the cut arcs 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 kpeter@2564: /// kpeter@2564: ///\sa BackInserterBoolMap kpeter@2564: ///\sa FrontInserterBoolMap 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: kpeter@2564: /// Constructor with specified iterator kpeter@2564: kpeter@2564: /// Constructor with specified iterator. kpeter@2564: /// \param _container The container for storing the elements. kpeter@2564: /// \param _it The elements will be inserted before this iterator. kpeter@2564: /// \param _functor The functor that is used when an element is stored. 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 kpeter@2564: kpeter@2564: /// Constructor without specified iterator. kpeter@2564: /// The elements will be inserted before _container.end(). kpeter@2564: /// \param _container The container for storing the elements. kpeter@2564: /// \param _functor The functor that is used when an element is stored. deba@2091: InserterBoolMap(Container& _container, const Functor& _functor = Functor()) deba@2091: : container(_container), it(_container.end()), functor(_functor) {} deba@1778: kpeter@2564: /// The \c set function of the map deba@1778: void set(const Key& key, Value value) { deba@1778: if (value) { kpeter@2564: it = container.insert(it, functor(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: kpeter@2564: /// \brief Writable bool map for filling each \c true assigned element with a kpeter@2564: /// given value. deba@1778: /// kpeter@2564: /// Writable bool map for filling each \c true assigned element with a kpeter@2564: /// given value. The value can set the container. deba@2091: /// kpeter@2564: /// The following code finds the connected components of a 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: kpeter@2564: /// The \c set 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: kpeter@2564: /// \brief Writable bool map for storing the sequence number of kpeter@2564: /// \c true assignments. deba@1778: /// kpeter@2564: /// Writable bool map that stores for each \c true assigned elements kpeter@2564: /// the sequence number of this setting. kpeter@2564: /// It makes it 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: /// kpeter@2564: /// The storing of the discovering order is more difficult because the deba@2091: /// ReachedMap should be readable in the dfs algorithm but the setting kpeter@2564: /// order map is not readable. Thus we must 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: kpeter@2564: /// The \c set 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