alpar@25: /* -*- C++ -*- alpar@25: * alpar@25: * This file is a part of LEMON, a generic C++ optimization library alpar@25: * alpar@39: * Copyright (C) 2003-2008 alpar@25: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@25: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@25: * alpar@25: * Permission to use, modify and distribute this software is granted alpar@25: * provided that this copyright notice appears in all copies. For alpar@25: * precise terms see the accompanying LICENSE file. alpar@25: * alpar@25: * This software is provided "AS IS" with no warranty of any kind, alpar@25: * express or implied, and with no claim as to its suitability for any alpar@25: * purpose. alpar@25: * alpar@25: */ alpar@25: alpar@25: #ifndef LEMON_MAPS_H alpar@25: #define LEMON_MAPS_H alpar@25: alpar@25: #include alpar@25: #include alpar@25: #include alpar@25: alpar@25: #include alpar@25: // #include alpar@25: alpar@25: ///\file alpar@25: ///\ingroup maps alpar@25: ///\brief Miscellaneous property maps alpar@25: /// alpar@25: #include alpar@25: alpar@25: namespace lemon { alpar@25: alpar@25: /// \addtogroup maps alpar@25: /// @{ alpar@25: alpar@25: /// Base class of maps. alpar@25: alpar@25: /// Base class of maps. alpar@25: /// It provides the necessary typedefs required by the map concept. alpar@25: template alpar@25: class MapBase { alpar@25: public: kpeter@33: /// The key type of the map. alpar@25: typedef K Key; kpeter@33: /// The value type of the map. (The type of objects associated with the keys). alpar@25: typedef T Value; alpar@25: }; alpar@25: alpar@25: /// Null map. (a.k.a. DoNothingMap) alpar@25: kpeter@29: /// This map can be used if you have to provide a map only for kpeter@29: /// its type definitions, or if you have to provide a writable map, kpeter@29: /// but data written to it is not required (i.e. it will be sent to kpeter@29: /// /dev/null). alpar@25: template alpar@25: class NullMap : public MapBase { alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: /// Gives back a default constructed element. alpar@25: T operator[](const K&) const { return T(); } alpar@25: /// Absorbs the value. alpar@25: void set(const K&, const T&) {} alpar@25: }; alpar@25: kpeter@29: ///Returns a \c NullMap class kpeter@29: kpeter@29: ///This function just returns a \c NullMap class. kpeter@29: ///\relates NullMap alpar@25: template alpar@25: NullMap nullMap() { alpar@25: return NullMap(); alpar@25: } alpar@25: alpar@25: alpar@25: /// Constant map. alpar@25: alpar@25: /// This is a readable map which assigns a specified value to each key. alpar@25: /// In other aspects it is equivalent to the \c NullMap. alpar@25: template alpar@25: class ConstMap : public MapBase { alpar@25: private: alpar@25: T v; alpar@25: public: alpar@25: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: /// Default constructor alpar@25: kpeter@29: /// Default constructor. alpar@25: /// The value of the map will be uninitialized. alpar@25: /// (More exactly it will be default constructed.) alpar@25: ConstMap() {} kpeter@29: kpeter@29: /// Constructor with specified initial value alpar@25: kpeter@29: /// Constructor with specified initial value. kpeter@29: /// \param _v is the initial value of the map. alpar@25: ConstMap(const T &_v) : v(_v) {} alpar@25: alpar@25: ///\e alpar@25: T operator[](const K&) const { return v; } alpar@25: alpar@25: ///\e alpar@25: void setAll(const T &t) { alpar@25: v = t; alpar@25: } alpar@25: alpar@25: template alpar@25: struct rebind { alpar@25: typedef ConstMap other; alpar@25: }; alpar@25: alpar@25: template alpar@25: ConstMap(const ConstMap &, const T &_v) : v(_v) {} alpar@25: }; alpar@25: alpar@25: ///Returns a \c ConstMap class alpar@25: alpar@25: ///This function just returns a \c ConstMap class. alpar@25: ///\relates ConstMap alpar@25: template alpar@25: inline ConstMap constMap(const V &v) { alpar@25: return ConstMap(v); alpar@25: } alpar@25: alpar@25: alpar@25: template alpar@25: struct Const { }; alpar@25: alpar@25: /// Constant map with inlined constant value. alpar@25: alpar@25: /// This is a readable map which assigns a specified value to each key. alpar@25: /// In other aspects it is equivalent to the \c NullMap. alpar@25: template alpar@25: class ConstMap > : public MapBase { alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ConstMap() { } alpar@25: ///\e alpar@25: V operator[](const K&) const { return v; } alpar@25: ///\e alpar@25: void set(const K&, const V&) { } alpar@25: }; alpar@25: alpar@25: ///Returns a \c ConstMap class alpar@25: alpar@25: ///This function just returns a \c ConstMap class with inlined value. alpar@25: ///\relates ConstMap alpar@25: template alpar@25: inline ConstMap > constMap() { alpar@25: return ConstMap >(); alpar@25: } alpar@25: alpar@25: ///Map based on std::map alpar@25: kpeter@29: ///This is essentially a wrapper for \c std::map with addition that kpeter@29: ///you can specify a default value different from \c Value(). alpar@25: template > alpar@25: class StdMap { alpar@25: template alpar@25: friend class StdMap; alpar@25: public: alpar@25: alpar@25: typedef True ReferenceMapTag; alpar@42: ///Key type alpar@25: typedef K Key; alpar@42: ///Value type alpar@25: typedef T Value; alpar@42: ///Reference Type alpar@25: typedef T& Reference; alpar@42: ///Const reference type alpar@25: typedef const T& ConstReference; alpar@25: alpar@25: private: alpar@25: alpar@25: typedef std::map Map; alpar@25: Value _value; alpar@25: Map _map; alpar@25: alpar@25: public: alpar@25: alpar@25: /// Constructor with specified default value alpar@25: StdMap(const T& value = T()) : _value(value) {} alpar@25: /// \brief Constructs the map from an appropriate std::map, and explicitly alpar@25: /// specifies a default value. alpar@25: template alpar@25: StdMap(const std::map &map, const T& value = T()) alpar@25: : _map(map.begin(), map.end()), _value(value) {} alpar@25: alpar@25: /// \brief Constructs a map from an other StdMap. alpar@25: template alpar@25: StdMap(const StdMap &c) alpar@25: : _map(c._map.begin(), c._map.end()), _value(c._value) {} alpar@25: alpar@25: private: alpar@25: alpar@25: StdMap& operator=(const StdMap&); alpar@25: alpar@25: public: alpar@25: alpar@25: ///\e alpar@25: Reference operator[](const Key &k) { alpar@25: typename Map::iterator it = _map.lower_bound(k); alpar@25: if (it != _map.end() && !_map.key_comp()(k, it->first)) alpar@25: return it->second; alpar@25: else alpar@25: return _map.insert(it, std::make_pair(k, _value))->second; alpar@25: } alpar@25: alpar@25: /// \e alpar@25: ConstReference operator[](const Key &k) const { alpar@25: typename Map::const_iterator it = _map.find(k); alpar@25: if (it != _map.end()) alpar@25: return it->second; alpar@25: else alpar@25: return _value; alpar@25: } alpar@25: alpar@25: /// \e alpar@25: void set(const Key &k, const T &t) { alpar@25: typename Map::iterator it = _map.lower_bound(k); alpar@25: if (it != _map.end() && !_map.key_comp()(k, it->first)) alpar@25: it->second = t; alpar@25: else alpar@25: _map.insert(it, std::make_pair(k, t)); alpar@25: } alpar@25: alpar@25: /// \e alpar@25: void setAll(const T &t) { alpar@25: _value = t; alpar@25: _map.clear(); alpar@25: } alpar@25: alpar@25: template > alpar@25: struct rebind { alpar@25: typedef StdMap other; alpar@25: }; alpar@25: }; alpar@25: kpeter@33: /// \brief Map for storing values for keys from the range [0..size-1] alpar@25: /// kpeter@33: /// The current map has the [0..size-1] keyset and the values alpar@25: /// are stored in a \c std::vector container. It can be used with alpar@25: /// some data structures, for example \c UnionFind, \c BinHeap, when alpar@26: /// the used items are small integer numbers. alpar@26: /// alpar@26: /// \todo Revise its name alpar@25: template alpar@25: class IntegerMap { alpar@25: alpar@25: template alpar@25: friend class IntegerMap; alpar@25: alpar@25: public: alpar@25: alpar@25: typedef True ReferenceMapTag; alpar@25: ///\e alpar@25: typedef int Key; alpar@25: ///\e alpar@25: typedef T Value; alpar@25: ///\e alpar@25: typedef T& Reference; alpar@25: ///\e alpar@25: typedef const T& ConstReference; alpar@25: alpar@25: private: alpar@25: alpar@25: typedef std::vector Vector; alpar@25: Vector _vector; alpar@25: alpar@25: public: alpar@25: alpar@25: /// Constructor with specified default value alpar@25: IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} alpar@25: alpar@25: /// \brief Constructs the map from an appropriate std::vector. alpar@25: template alpar@25: IntegerMap(const std::vector& vector) alpar@25: : _vector(vector.begin(), vector.end()) {} alpar@25: alpar@25: /// \brief Constructs a map from an other IntegerMap. alpar@25: template alpar@25: IntegerMap(const IntegerMap &c) alpar@25: : _vector(c._vector.begin(), c._vector.end()) {} alpar@25: alpar@25: /// \brief Resize the container alpar@25: void resize(int size, const T& value = T()) { alpar@25: _vector.resize(size, value); alpar@25: } alpar@25: alpar@25: private: alpar@25: alpar@25: IntegerMap& operator=(const IntegerMap&); alpar@25: alpar@25: public: alpar@25: alpar@25: ///\e alpar@25: Reference operator[](Key k) { alpar@25: return _vector[k]; alpar@25: } alpar@25: alpar@25: /// \e alpar@25: ConstReference operator[](Key k) const { alpar@25: return _vector[k]; alpar@25: } alpar@25: alpar@25: /// \e alpar@25: void set(const Key &k, const T& t) { alpar@25: _vector[k] = t; alpar@25: } alpar@25: alpar@25: }; alpar@25: alpar@25: /// @} alpar@25: alpar@25: /// \addtogroup map_adaptors alpar@25: /// @{ alpar@25: kpeter@29: /// \brief Identity map. alpar@25: /// kpeter@29: /// This map gives back the given key as value without any alpar@25: /// modification. alpar@25: template alpar@25: class IdentityMap : public MapBase { alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: /// \e alpar@25: const T& operator[](const T& t) const { alpar@25: return t; alpar@25: } alpar@25: }; alpar@25: alpar@25: ///Returns an \c IdentityMap class alpar@25: alpar@25: ///This function just returns an \c IdentityMap class. alpar@25: ///\relates IdentityMap alpar@25: template alpar@25: inline IdentityMap identityMap() { alpar@25: return IdentityMap(); alpar@25: } alpar@25: alpar@25: alpar@26: ///\brief Convert the \c Value of a map to another type using alpar@26: ///the default conversion. alpar@26: /// alpar@25: ///This \c concepts::ReadMap "read only map" kpeter@29: ///converts the \c Value of a map to type \c T. alpar@25: ///Its \c Key is inherited from \c M. alpar@25: template alpar@25: class ConvertMap : public MapBase { alpar@25: const M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: kpeter@29: ///Constructor. kpeter@29: ///\param _m is the underlying map. alpar@25: ConvertMap(const M &_m) : m(_m) {}; alpar@25: alpar@25: /// \brief The subscript operator. alpar@25: /// alpar@25: /// The subscript operator. alpar@25: Value operator[](const Key& k) const {return m[k];} alpar@25: }; alpar@25: kpeter@29: ///Returns a \c ConvertMap class alpar@25: kpeter@29: ///This function just returns a \c ConvertMap class. alpar@25: ///\relates ConvertMap alpar@25: template alpar@25: inline ConvertMap convertMap(const M &m) { alpar@25: return ConvertMap(m); alpar@25: } alpar@25: kpeter@29: ///Simple wrapping of a map alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the simple alpar@25: ///wrapping of the given map. Sometimes the reference maps cannot be alpar@25: ///combined with simple read maps. This map adaptor wraps the given alpar@25: ///map to simple read map. alpar@26: /// kpeter@29: ///\sa SimpleWriteMap kpeter@29: /// kpeter@29: /// \todo Revise the misleading name alpar@25: template alpar@25: class SimpleMap : public MapBase { alpar@25: const M& m; alpar@25: alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: SimpleMap(const M &_m) : m(_m) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return m[k];} alpar@25: }; alpar@25: kpeter@29: ///Simple writable wrapping of the map alpar@25: alpar@26: ///This \c concepts::WriteMap "write map" returns the simple alpar@25: ///wrapping of the given map. Sometimes the reference maps cannot be alpar@25: ///combined with simple read-write maps. This map adaptor wraps the alpar@25: ///given map to simple read-write map. alpar@26: /// kpeter@29: ///\sa SimpleMap kpeter@29: /// alpar@26: /// \todo Revise the misleading name alpar@25: template alpar@25: class SimpleWriteMap : public MapBase { alpar@25: M& m; alpar@25: alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: SimpleWriteMap(M &_m) : m(_m) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return m[k];} alpar@25: ///\e alpar@25: void set(Key k, const Value& c) { m.set(k, c); } alpar@25: }; alpar@25: alpar@25: ///Sum of two maps alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the sum of the two kpeter@29: ///given maps. kpeter@29: ///Its \c Key and \c Value are inherited from \c M1. alpar@25: ///The \c Key and \c Value of M2 must be convertible to those of \c M1. alpar@25: template alpar@25: class AddMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return m1[k]+m2[k];} alpar@25: }; alpar@25: alpar@25: ///Returns an \c AddMap class alpar@25: alpar@25: ///This function just returns an \c AddMap class. alpar@25: ///\todo How to call these type of functions? alpar@25: /// alpar@25: ///\relates AddMap alpar@25: template alpar@25: inline AddMap addMap(const M1 &m1,const M2 &m2) { alpar@25: return AddMap(m1,m2); alpar@25: } alpar@25: alpar@25: ///Shift a map with a constant. alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the sum of the alpar@25: ///given map and a constant value. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. alpar@25: /// alpar@25: ///Actually, alpar@25: ///\code alpar@25: /// ShiftMap sh(x,v); alpar@25: ///\endcode kpeter@29: ///is equivalent to alpar@25: ///\code alpar@25: /// ConstMap c_tmp(v); alpar@25: /// AddMap > sh(x,v); alpar@25: ///\endcode kpeter@29: /// kpeter@29: ///\sa ShiftWriteMap alpar@25: template alpar@25: class ShiftMap : public MapBase { alpar@25: const M& m; alpar@25: C v; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: kpeter@29: ///Constructor. kpeter@29: ///\param _m is the undelying map. kpeter@29: ///\param _v is the shift value. alpar@25: ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return m[k] + v;} alpar@25: }; alpar@25: kpeter@29: ///Shift a map with a constant (ReadWrite version). alpar@25: alpar@25: ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the alpar@25: ///given map and a constant value. It makes also possible to write the map. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. alpar@25: /// kpeter@29: ///\sa ShiftMap alpar@25: template alpar@25: class ShiftWriteMap : public MapBase { alpar@25: M& m; alpar@25: C v; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: kpeter@29: ///Constructor. kpeter@29: ///\param _m is the undelying map. kpeter@29: ///\param _v is the shift value. alpar@25: ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return m[k] + v;} alpar@25: /// \e alpar@25: void set(Key k, const Value& c) { m.set(k, c - v); } alpar@25: }; alpar@25: kpeter@29: ///Returns a \c ShiftMap class alpar@25: kpeter@29: ///This function just returns a \c ShiftMap class. alpar@25: ///\relates ShiftMap alpar@25: template alpar@25: inline ShiftMap shiftMap(const M &m,const C &v) { alpar@25: return ShiftMap(m,v); alpar@25: } alpar@25: kpeter@29: ///Returns a \c ShiftWriteMap class kpeter@29: kpeter@29: ///This function just returns a \c ShiftWriteMap class. kpeter@29: ///\relates ShiftWriteMap alpar@25: template alpar@25: inline ShiftWriteMap shiftMap(M &m,const C &v) { alpar@25: return ShiftWriteMap(m,v); alpar@25: } alpar@25: alpar@25: ///Difference of two maps alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the difference kpeter@29: ///of the values of the two given maps. kpeter@29: ///Its \c Key and \c Value are inherited from \c M1. alpar@25: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. alpar@26: /// alpar@26: /// \todo Revise the misleading name alpar@25: template alpar@25: class SubMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return m1[k]-m2[k];} alpar@25: }; alpar@25: alpar@25: ///Returns a \c SubMap class alpar@25: alpar@25: ///This function just returns a \c SubMap class. alpar@25: /// alpar@25: ///\relates SubMap alpar@25: template alpar@25: inline SubMap subMap(const M1 &m1, const M2 &m2) { alpar@25: return SubMap(m1, m2); alpar@25: } alpar@25: alpar@25: ///Product of two maps alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the product of the kpeter@29: ///values of the two given maps. kpeter@29: ///Its \c Key and \c Value are inherited from \c M1. alpar@25: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. alpar@25: template alpar@25: class MulMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return m1[k]*m2[k];} alpar@25: }; alpar@25: alpar@25: ///Returns a \c MulMap class alpar@25: alpar@25: ///This function just returns a \c MulMap class. alpar@25: ///\relates MulMap alpar@25: template alpar@25: inline MulMap mulMap(const M1 &m1,const M2 &m2) { alpar@25: return MulMap(m1,m2); alpar@25: } alpar@25: kpeter@29: ///Scales a map with a constant. alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the value of the alpar@25: ///given map multiplied from the left side with a constant value. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. alpar@25: /// alpar@25: ///Actually, alpar@25: ///\code alpar@25: /// ScaleMap sc(x,v); alpar@25: ///\endcode kpeter@29: ///is equivalent to alpar@25: ///\code alpar@25: /// ConstMap c_tmp(v); alpar@25: /// MulMap > sc(x,v); alpar@25: ///\endcode kpeter@29: /// kpeter@29: ///\sa ScaleWriteMap alpar@25: template alpar@25: class ScaleMap : public MapBase { alpar@25: const M& m; alpar@25: C v; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: kpeter@29: ///Constructor. kpeter@29: ///\param _m is the undelying map. kpeter@29: ///\param _v is the scaling value. alpar@25: ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return v * m[k];} alpar@25: }; alpar@25: kpeter@29: ///Scales a map with a constant (ReadWrite version). alpar@25: alpar@25: ///This \c concepts::ReadWriteMap "read-write map" returns the value of the alpar@25: ///given map multiplied from the left side with a constant value. It can kpeter@29: ///also be used as write map if the \c / operator is defined between kpeter@29: ///\c Value and \c C and the given multiplier is not zero. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. kpeter@29: /// kpeter@29: ///\sa ScaleMap alpar@25: template alpar@25: class ScaleWriteMap : public MapBase { alpar@25: M& m; alpar@25: C v; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: kpeter@29: ///Constructor. kpeter@29: ///\param _m is the undelying map. kpeter@29: ///\param _v is the scaling value. alpar@25: ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return v * m[k];} alpar@25: /// \e alpar@25: void set(Key k, const Value& c) { m.set(k, c / v);} alpar@25: }; alpar@25: kpeter@29: ///Returns a \c ScaleMap class alpar@25: kpeter@29: ///This function just returns a \c ScaleMap class. alpar@25: ///\relates ScaleMap alpar@25: template alpar@25: inline ScaleMap scaleMap(const M &m,const C &v) { alpar@25: return ScaleMap(m,v); alpar@25: } alpar@25: kpeter@29: ///Returns a \c ScaleWriteMap class kpeter@29: kpeter@29: ///This function just returns a \c ScaleWriteMap class. kpeter@29: ///\relates ScaleWriteMap alpar@25: template alpar@25: inline ScaleWriteMap scaleMap(M &m,const C &v) { alpar@25: return ScaleWriteMap(m,v); alpar@25: } alpar@25: alpar@25: ///Quotient of two maps alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the quotient of the kpeter@29: ///values of the two given maps. kpeter@29: ///Its \c Key and \c Value are inherited from \c M1. alpar@25: ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. alpar@25: template alpar@25: class DivMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return m1[k]/m2[k];} alpar@25: }; alpar@25: alpar@25: ///Returns a \c DivMap class alpar@25: alpar@25: ///This function just returns a \c DivMap class. alpar@25: ///\relates DivMap alpar@25: template alpar@25: inline DivMap divMap(const M1 &m1,const M2 &m2) { alpar@25: return DivMap(m1,m2); alpar@25: } alpar@25: alpar@25: ///Composition of two maps alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the composition of kpeter@29: ///two given maps. kpeter@29: ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2, alpar@25: ///then for alpar@25: ///\code alpar@25: /// ComposeMap cm(m1,m2); alpar@25: ///\endcode kpeter@29: /// cm[x] will be equal to m1[m2[x]]. alpar@25: /// kpeter@29: ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1. kpeter@29: ///\c M2::Value must be convertible to \c M1::Key. kpeter@29: /// kpeter@29: ///\sa CombineMap kpeter@29: /// alpar@25: ///\todo Check the requirements. alpar@25: template alpar@25: class ComposeMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: alpar@25: /// \e alpar@25: alpar@25: alpar@25: /// \todo Use the MapTraits once it is ported. alpar@25: /// alpar@25: alpar@25: //typename MapTraits::ConstReturnValue alpar@25: typename M1::Value alpar@25: operator[](Key k) const {return m1[m2[k]];} alpar@25: }; kpeter@29: alpar@25: ///Returns a \c ComposeMap class alpar@25: alpar@25: ///This function just returns a \c ComposeMap class. alpar@25: ///\relates ComposeMap alpar@25: template alpar@25: inline ComposeMap composeMap(const M1 &m1,const M2 &m2) { alpar@25: return ComposeMap(m1,m2); alpar@25: } alpar@25: kpeter@29: ///Combine of two maps using an STL (binary) functor. alpar@25: kpeter@29: ///Combine of two maps using an STL (binary) functor. alpar@25: /// alpar@25: ///This \c concepts::ReadMap "read only map" takes two maps and a kpeter@29: ///binary functor and returns the composition of the two alpar@25: ///given maps unsing the functor. alpar@25: ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 kpeter@29: ///and \c f is of \c F, then for alpar@25: ///\code kpeter@29: /// CombineMap cm(m1,m2,f); alpar@25: ///\endcode alpar@25: /// cm[x] will be equal to f(m1[x],m2[x]) alpar@25: /// alpar@25: ///Its \c Key is inherited from \c M1 and its \c Value is \c V. kpeter@29: ///\c M2::Value and \c M1::Value must be convertible to the corresponding alpar@25: ///input parameter of \c F and the return type of \c F must be convertible alpar@25: ///to \c V. kpeter@29: /// kpeter@29: ///\sa ComposeMap kpeter@29: /// alpar@25: ///\todo Check the requirements. alpar@25: template alpar@25: class CombineMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: F f; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F()) alpar@25: : m1(_m1), m2(_m2), f(_f) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return f(m1[k],m2[k]);} alpar@25: }; alpar@25: alpar@25: ///Returns a \c CombineMap class alpar@25: alpar@25: ///This function just returns a \c CombineMap class. alpar@25: /// alpar@25: ///For example if \c m1 and \c m2 are both \c double valued maps, then alpar@25: ///\code kpeter@33: ///combineMap(m1,m2,std::plus()) alpar@25: ///\endcode kpeter@29: ///is equivalent to alpar@25: ///\code alpar@25: ///addMap(m1,m2) alpar@25: ///\endcode alpar@25: /// alpar@25: ///This function is specialized for adaptable binary function kpeter@29: ///classes and C++ functions. alpar@25: /// alpar@25: ///\relates CombineMap alpar@25: template alpar@25: inline CombineMap alpar@25: combineMap(const M1& m1,const M2& m2, const F& f) { alpar@25: return CombineMap(m1,m2,f); alpar@25: } alpar@25: alpar@25: template alpar@25: inline CombineMap alpar@25: combineMap(const M1& m1, const M2& m2, const F& f) { alpar@25: return combineMap(m1,m2,f); alpar@25: } alpar@25: alpar@25: template alpar@25: inline CombineMap alpar@25: combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { alpar@25: return combineMap(m1,m2,f); alpar@25: } alpar@25: alpar@25: ///Negative value of a map alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the negative kpeter@29: ///value of the value returned by the given map. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. alpar@25: ///The unary \c - operator must be defined for \c Value, of course. kpeter@29: /// kpeter@29: ///\sa NegWriteMap alpar@25: template alpar@25: class NegMap : public MapBase { alpar@25: const M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: NegMap(const M &_m) : m(_m) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return -m[k];} alpar@25: }; alpar@25: alpar@26: ///Negative value of a map (ReadWrite version) alpar@25: alpar@25: ///This \c concepts::ReadWriteMap "read-write map" returns the negative kpeter@29: ///value of the value returned by the given map. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. alpar@25: ///The unary \c - operator must be defined for \c Value, of course. kpeter@29: /// kpeter@29: /// \sa NegMap alpar@25: template alpar@25: class NegWriteMap : public MapBase { alpar@25: M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: NegWriteMap(M &_m) : m(_m) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return -m[k];} alpar@25: /// \e alpar@25: void set(Key k, const Value& v) { m.set(k, -v); } alpar@25: }; alpar@25: alpar@25: ///Returns a \c NegMap class alpar@25: alpar@25: ///This function just returns a \c NegMap class. alpar@25: ///\relates NegMap alpar@25: template alpar@25: inline NegMap negMap(const M &m) { alpar@25: return NegMap(m); alpar@25: } alpar@25: kpeter@29: ///Returns a \c NegWriteMap class kpeter@29: kpeter@29: ///This function just returns a \c NegWriteMap class. kpeter@29: ///\relates NegWriteMap alpar@25: template alpar@25: inline NegWriteMap negMap(M &m) { alpar@25: return NegWriteMap(m); alpar@25: } alpar@25: alpar@25: ///Absolute value of a map alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the absolute value kpeter@29: ///of the value returned by the given map. kpeter@29: ///Its \c Key and \c Value are inherited from \c M. kpeter@29: ///\c Value must be comparable to \c 0 and the unary \c - alpar@25: ///operator must be defined for it, of course. alpar@25: template alpar@25: class AbsMap : public MapBase { alpar@25: const M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: AbsMap(const M &_m) : m(_m) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const { alpar@25: Value tmp = m[k]; alpar@25: return tmp >= 0 ? tmp : -tmp; alpar@25: } alpar@25: alpar@25: }; alpar@25: kpeter@29: ///Returns an \c AbsMap class alpar@25: kpeter@29: ///This function just returns an \c AbsMap class. alpar@25: ///\relates AbsMap alpar@25: template alpar@25: inline AbsMap absMap(const M &m) { alpar@25: return AbsMap(m); alpar@25: } alpar@25: alpar@25: ///Converts an STL style functor to a map alpar@25: alpar@25: ///This \c concepts::ReadMap "read only map" returns the value kpeter@29: ///of a given functor. alpar@25: /// alpar@25: ///Template parameters \c K and \c V will become its kpeter@33: ///\c Key and \c Value. kpeter@33: ///In most cases they have to be given explicitly because a kpeter@33: ///functor typically does not provide such typedefs. alpar@25: /// alpar@25: ///Parameter \c F is the type of the used functor. kpeter@29: /// kpeter@29: ///\sa MapFunctor alpar@25: template alpar@25: class FunctorMap : public MapBase { alpar@25: F f; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: FunctorMap(const F &_f = F()) : f(_f) {} alpar@25: /// \e alpar@25: Value operator[](Key k) const { return f(k);} alpar@25: }; alpar@25: alpar@25: ///Returns a \c FunctorMap class alpar@25: alpar@25: ///This function just returns a \c FunctorMap class. alpar@25: /// alpar@25: ///It is specialized for adaptable function classes and kpeter@29: ///C++ functions. alpar@25: ///\relates FunctorMap alpar@25: template inline alpar@25: FunctorMap functorMap(const F &f) { alpar@25: return FunctorMap(f); alpar@25: } alpar@25: alpar@25: template inline alpar@25: FunctorMap alpar@25: functorMap(const F &f) { alpar@25: return FunctorMap(f); alpar@25: } alpar@25: alpar@25: template inline alpar@25: FunctorMap functorMap(V (*f)(K)) { alpar@25: return FunctorMap(f); alpar@25: } alpar@25: alpar@25: alpar@25: ///Converts a map to an STL style (unary) functor alpar@25: alpar@25: ///This class Converts a map to an STL style (unary) functor. alpar@25: ///that is it provides an operator() to read its values. alpar@25: /// alpar@25: ///For the sake of convenience it also works as alpar@25: ///a ususal \c concepts::ReadMap "readable map", alpar@25: ///i.e. operator[] and the \c Key and \c Value typedefs also exist. kpeter@29: /// kpeter@29: ///\sa FunctorMap alpar@25: template alpar@25: class MapFunctor : public MapBase { alpar@25: const M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: typedef typename M::Key argument_type; alpar@25: typedef typename M::Value result_type; alpar@25: alpar@25: ///Constructor alpar@25: MapFunctor(const M &_m) : m(_m) {}; alpar@25: ///\e alpar@25: Value operator()(Key k) const {return m[k];} alpar@25: ///\e alpar@25: Value operator[](Key k) const {return m[k];} alpar@25: }; alpar@25: alpar@25: ///Returns a \c MapFunctor class alpar@25: alpar@25: ///This function just returns a \c MapFunctor class. alpar@25: ///\relates MapFunctor alpar@25: template alpar@25: inline MapFunctor mapFunctor(const M &m) { alpar@25: return MapFunctor(m); alpar@25: } alpar@25: alpar@25: ///Applies all map setting operations to two maps alpar@25: alpar@25: ///This map has two \c concepts::ReadMap "readable map" alpar@25: ///parameters and each read request will be passed just to the alpar@25: ///first map. This class is the just readable map type of the ForkWriteMap. alpar@25: /// kpeter@29: ///The \c Key and \c Value are inherited from \c M1. alpar@25: ///The \c Key and \c Value of M2 must be convertible from those of \c M1. alpar@26: /// kpeter@29: ///\sa ForkWriteMap kpeter@29: /// alpar@26: /// \todo Why is it needed? alpar@25: template alpar@25: class ForkMap : public MapBase { alpar@25: const M1& m1; alpar@25: const M2& m2; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: /// \e alpar@25: Value operator[](Key k) const {return m1[k];} alpar@25: }; alpar@25: alpar@25: alpar@25: ///Applies all map setting operations to two maps alpar@25: alpar@25: ///This map has two \c concepts::WriteMap "writable map" alpar@25: ///parameters and each write request will be passed to both of them. alpar@25: ///If \c M1 is also \c concepts::ReadMap "readable", alpar@25: ///then the read operations will return the alpar@25: ///corresponding values of \c M1. alpar@25: /// kpeter@29: ///The \c Key and \c Value are inherited from \c M1. alpar@25: ///The \c Key and \c Value of M2 must be convertible from those of \c M1. kpeter@29: /// kpeter@29: ///\sa ForkMap alpar@25: template alpar@25: class ForkWriteMap : public MapBase { alpar@25: M1& m1; alpar@25: M2& m2; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: ///Constructor alpar@25: ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return m1[k];} alpar@25: ///\e alpar@25: void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} alpar@25: }; alpar@25: kpeter@29: ///Returns a \c ForkMap class alpar@25: kpeter@29: ///This function just returns a \c ForkMap class. alpar@25: ///\relates ForkMap alpar@25: template alpar@25: inline ForkMap forkMap(const M1 &m1, const M2 &m2) { alpar@25: return ForkMap(m1,m2); alpar@25: } alpar@25: kpeter@29: ///Returns a \c ForkWriteMap class kpeter@29: kpeter@29: ///This function just returns a \c ForkWriteMap class. kpeter@29: ///\relates ForkWriteMap alpar@25: template alpar@25: inline ForkWriteMap forkMap(M1 &m1, M2 &m2) { alpar@25: return ForkWriteMap(m1,m2); alpar@25: } alpar@25: alpar@25: alpar@25: alpar@25: /* ************* BOOL MAPS ******************* */ alpar@25: alpar@25: ///Logical 'not' of a map alpar@25: alpar@25: ///This bool \c concepts::ReadMap "read only map" returns the kpeter@29: ///logical negation of the value returned by the given map. kpeter@29: ///Its \c Key is inherited from \c M, its Value is \c bool. kpeter@29: /// kpeter@29: ///\sa NotWriteMap alpar@25: template alpar@25: class NotMap : public MapBase { alpar@25: const M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: /// Constructor alpar@25: NotMap(const M &_m) : m(_m) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return !m[k];} alpar@25: }; alpar@25: alpar@26: ///Logical 'not' of a map (ReadWrie version) alpar@25: alpar@25: ///This bool \c concepts::ReadWriteMap "read-write map" returns the kpeter@29: ///logical negation of the value returned by the given map. When it is set, alpar@25: ///the opposite value is set to the original map. kpeter@29: ///Its \c Key is inherited from \c M, its Value is \c bool. kpeter@29: /// kpeter@29: ///\sa NotMap alpar@25: template alpar@25: class NotWriteMap : public MapBase { alpar@25: M& m; alpar@25: public: alpar@25: typedef MapBase Parent; alpar@25: typedef typename Parent::Key Key; alpar@25: typedef typename Parent::Value Value; alpar@25: alpar@25: /// Constructor alpar@25: NotWriteMap(M &_m) : m(_m) {}; alpar@25: ///\e alpar@25: Value operator[](Key k) const {return !m[k];} alpar@25: ///\e alpar@25: void set(Key k, bool v) { m.set(k, !v); } alpar@25: }; alpar@25: alpar@25: ///Returns a \c NotMap class alpar@25: alpar@25: ///This function just returns a \c NotMap class. alpar@25: ///\relates NotMap alpar@25: template alpar@25: inline NotMap notMap(const M &m) { alpar@25: return NotMap(m); alpar@25: } alpar@25: kpeter@29: ///Returns a \c NotWriteMap class kpeter@29: kpeter@29: ///This function just returns a \c NotWriteMap class. kpeter@29: ///\relates NotWriteMap alpar@25: template alpar@25: inline NotWriteMap notMap(M &m) { alpar@25: return NotWriteMap(m); alpar@25: } alpar@25: alpar@25: namespace _maps_bits { alpar@25: alpar@25: template alpar@25: struct Identity { alpar@25: typedef Value argument_type; alpar@25: typedef Value result_type; alpar@25: Value operator()(const Value& val) const { alpar@25: return val; alpar@25: } alpar@25: }; alpar@25: alpar@25: template alpar@25: struct IteratorTraits { alpar@25: typedef typename std::iterator_traits<_Iterator>::value_type Value; alpar@25: }; alpar@25: alpar@25: template alpar@25: struct IteratorTraits<_Iterator, alpar@25: typename exists::type> alpar@25: { alpar@25: typedef typename _Iterator::container_type::value_type Value; alpar@25: }; alpar@25: alpar@25: } alpar@25: alpar@25: kpeter@29: /// \brief Writable bool map for logging each \c true assigned element alpar@25: /// kpeter@29: /// Writable bool map for logging each \c true assigned element, i.e it kpeter@29: /// copies all the keys set to \c true to the given iterator. alpar@25: /// alpar@25: /// \note The container of the iterator should contain space alpar@25: /// for each element. alpar@25: /// alpar@26: /// The following example shows how you can write the edges found by the Prim alpar@26: /// algorithm directly alpar@25: /// to the standard output. alpar@25: ///\code alpar@25: /// typedef IdMap EdgeIdMap; alpar@25: /// EdgeIdMap edgeId(graph); alpar@25: /// alpar@25: /// typedef MapFunctor EdgeIdFunctor; alpar@25: /// EdgeIdFunctor edgeIdFunctor(edgeId); alpar@25: /// alpar@25: /// StoreBoolMap, EdgeIdFunctor> alpar@25: /// writerMap(ostream_iterator(cout, " "), edgeIdFunctor); alpar@25: /// alpar@25: /// prim(graph, cost, writerMap); alpar@25: ///\endcode alpar@26: /// kpeter@29: ///\sa BackInserterBoolMap kpeter@33: ///\sa FrontInserterBoolMap kpeter@33: ///\sa InserterBoolMap kpeter@29: /// kpeter@29: ///\todo Revise the name of this class and the related ones. alpar@25: template ::Value> > alpar@25: class StoreBoolMap { alpar@25: public: alpar@25: typedef _Iterator Iterator; alpar@25: alpar@25: typedef typename _Functor::argument_type Key; alpar@25: typedef bool Value; alpar@25: alpar@25: typedef _Functor Functor; alpar@25: alpar@25: /// Constructor alpar@25: StoreBoolMap(Iterator it, const Functor& functor = Functor()) alpar@25: : _begin(it), _end(it), _functor(functor) {} alpar@25: alpar@26: /// Gives back the given iterator set for the first key alpar@25: Iterator begin() const { alpar@25: return _begin; alpar@25: } alpar@25: alpar@26: /// Gives back the the 'after the last' iterator alpar@25: Iterator end() const { alpar@25: return _end; alpar@25: } alpar@25: kpeter@29: /// The \c set function of the map alpar@25: void set(const Key& key, Value value) const { alpar@25: if (value) { alpar@25: *_end++ = _functor(key); alpar@25: } alpar@25: } alpar@25: alpar@25: private: alpar@25: Iterator _begin; alpar@25: mutable Iterator _end; alpar@25: Functor _functor; alpar@25: }; alpar@25: kpeter@29: /// \brief Writable bool map for logging each \c true assigned element in kpeter@29: /// a back insertable container. alpar@25: /// kpeter@29: /// Writable bool map for logging each \c true assigned element by pushing kpeter@29: /// them into a back insertable container. alpar@26: /// It can be used to retrieve the items into a standard alpar@26: /// container. The next example shows how you can store the alpar@26: /// edges found by the Prim algorithm in a vector. alpar@25: /// alpar@25: ///\code alpar@25: /// vector span_tree_edges; alpar@25: /// BackInserterBoolMap > inserter_map(span_tree_edges); alpar@25: /// prim(graph, cost, inserter_map); alpar@25: ///\endcode kpeter@29: /// kpeter@29: ///\sa StoreBoolMap kpeter@29: ///\sa FrontInserterBoolMap kpeter@29: ///\sa InserterBoolMap alpar@25: template > alpar@25: class BackInserterBoolMap { alpar@25: public: kpeter@34: typedef typename Functor::argument_type Key; alpar@25: typedef bool Value; alpar@25: alpar@25: /// Constructor alpar@25: BackInserterBoolMap(Container& _container, alpar@25: const Functor& _functor = Functor()) alpar@25: : container(_container), functor(_functor) {} alpar@25: kpeter@29: /// The \c set function of the map alpar@25: void set(const Key& key, Value value) { alpar@25: if (value) { alpar@25: container.push_back(functor(key)); alpar@25: } alpar@25: } alpar@25: alpar@25: private: alpar@25: Container& container; alpar@25: Functor functor; alpar@25: }; alpar@25: kpeter@29: /// \brief Writable bool map for logging each \c true assigned element in alpar@25: /// a front insertable container. alpar@25: /// kpeter@29: /// Writable bool map for logging each \c true assigned element by pushing kpeter@29: /// them into a front insertable container. kpeter@29: /// It can be used to retrieve the items into a standard kpeter@29: /// container. For example see \ref BackInserterBoolMap. kpeter@29: /// kpeter@29: ///\sa BackInserterBoolMap kpeter@29: ///\sa InserterBoolMap alpar@25: template > alpar@25: class FrontInserterBoolMap { alpar@25: public: kpeter@34: typedef typename Functor::argument_type Key; alpar@25: typedef bool Value; alpar@25: alpar@25: /// Constructor alpar@25: FrontInserterBoolMap(Container& _container, alpar@25: const Functor& _functor = Functor()) alpar@25: : container(_container), functor(_functor) {} alpar@25: kpeter@29: /// The \c set function of the map alpar@25: void set(const Key& key, Value value) { alpar@25: if (value) { kpeter@30: container.push_front(functor(key)); alpar@25: } alpar@25: } alpar@25: alpar@25: private: alpar@25: Container& container; alpar@25: Functor functor; alpar@25: }; alpar@25: kpeter@29: /// \brief Writable bool map for storing each \c true assigned element in alpar@25: /// an insertable container. alpar@25: /// kpeter@29: /// Writable bool map for storing each \c true assigned element in an alpar@25: /// insertable container. It will insert all the keys set to \c true into alpar@26: /// the container. alpar@26: /// alpar@26: /// For example, if you want to store the cut arcs of the strongly alpar@25: /// connected components in a set you can use the next code: alpar@25: /// alpar@25: ///\code alpar@25: /// set cut_arcs; alpar@25: /// InserterBoolMap > inserter_map(cut_arcs); alpar@25: /// stronglyConnectedCutArcs(digraph, cost, inserter_map); alpar@25: ///\endcode kpeter@29: /// kpeter@29: ///\sa BackInserterBoolMap kpeter@29: ///\sa FrontInserterBoolMap alpar@25: template > alpar@25: class InserterBoolMap { alpar@25: public: alpar@25: typedef typename Container::value_type Key; alpar@25: typedef bool Value; alpar@25: kpeter@29: /// Constructor with specified iterator kpeter@29: kpeter@29: /// Constructor with specified iterator. kpeter@29: /// \param _container The container for storing the elements. kpeter@29: /// \param _it The elements will be inserted before this iterator. kpeter@29: /// \param _functor The functor that is used when an element is stored. alpar@25: InserterBoolMap(Container& _container, typename Container::iterator _it, alpar@25: const Functor& _functor = Functor()) alpar@25: : container(_container), it(_it), functor(_functor) {} alpar@25: alpar@25: /// Constructor kpeter@29: kpeter@29: /// Constructor without specified iterator. kpeter@29: /// The elements will be inserted before _container.end(). kpeter@29: /// \param _container The container for storing the elements. kpeter@29: /// \param _functor The functor that is used when an element is stored. alpar@25: InserterBoolMap(Container& _container, const Functor& _functor = Functor()) alpar@25: : container(_container), it(_container.end()), functor(_functor) {} alpar@25: kpeter@29: /// The \c set function of the map alpar@25: void set(const Key& key, Value value) { alpar@25: if (value) { kpeter@30: it = container.insert(it, functor(key)); alpar@25: ++it; alpar@25: } alpar@25: } alpar@25: alpar@25: private: alpar@25: Container& container; alpar@25: typename Container::iterator it; alpar@25: Functor functor; alpar@25: }; alpar@25: kpeter@29: /// \brief Writable bool map for filling each \c true assigned element with a kpeter@29: /// given value. alpar@25: /// kpeter@29: /// Writable bool map for filling each \c true assigned element with a kpeter@29: /// given value. The value can set the container. alpar@25: /// alpar@26: /// The following code finds the connected components of a graph alpar@25: /// and stores it in the \c comp map: alpar@25: ///\code alpar@25: /// typedef Graph::NodeMap ComponentMap; alpar@25: /// ComponentMap comp(graph); alpar@25: /// typedef FillBoolMap > ComponentFillerMap; alpar@25: /// ComponentFillerMap filler(comp, 0); alpar@25: /// alpar@25: /// Dfs::DefProcessedMap::Create dfs(graph); alpar@25: /// dfs.processedMap(filler); alpar@25: /// dfs.init(); alpar@25: /// for (NodeIt it(graph); it != INVALID; ++it) { alpar@25: /// if (!dfs.reached(it)) { alpar@25: /// dfs.addSource(it); alpar@25: /// dfs.start(); alpar@25: /// ++filler.fillValue(); alpar@25: /// } alpar@25: /// } alpar@25: ///\endcode alpar@25: template alpar@25: class FillBoolMap { alpar@25: public: alpar@25: typedef typename Map::Key Key; alpar@25: typedef bool Value; alpar@25: alpar@25: /// Constructor alpar@25: FillBoolMap(Map& _map, const typename Map::Value& _fill) alpar@25: : map(_map), fill(_fill) {} alpar@25: alpar@25: /// Constructor alpar@25: FillBoolMap(Map& _map) alpar@25: : map(_map), fill() {} alpar@25: alpar@25: /// Gives back the current fill value alpar@25: const typename Map::Value& fillValue() const { alpar@25: return fill; alpar@25: } alpar@25: alpar@25: /// Gives back the current fill value alpar@25: typename Map::Value& fillValue() { alpar@25: return fill; alpar@25: } alpar@25: alpar@25: /// Sets the current fill value alpar@25: void fillValue(const typename Map::Value& _fill) { alpar@25: fill = _fill; alpar@25: } alpar@25: kpeter@29: /// The \c set function of the map alpar@25: void set(const Key& key, Value value) { alpar@25: if (value) { alpar@25: map.set(key, fill); alpar@25: } alpar@25: } alpar@25: alpar@25: private: alpar@25: Map& map; alpar@25: typename Map::Value fill; alpar@25: }; alpar@25: alpar@25: kpeter@29: /// \brief Writable bool map for storing the sequence number of kpeter@29: /// \c true assignments. alpar@26: /// kpeter@29: /// Writable bool map that stores for each \c true assigned elements alpar@26: /// the sequence number of this setting. alpar@26: /// It makes it easy to calculate the leaving alpar@25: /// order of the nodes in the \c Dfs algorithm. alpar@25: /// alpar@25: ///\code alpar@25: /// typedef Digraph::NodeMap OrderMap; alpar@25: /// OrderMap order(digraph); alpar@25: /// typedef SettingOrderBoolMap OrderSetterMap; alpar@25: /// OrderSetterMap setter(order); alpar@25: /// Dfs::DefProcessedMap::Create dfs(digraph); alpar@25: /// dfs.processedMap(setter); alpar@25: /// dfs.init(); alpar@25: /// for (NodeIt it(digraph); it != INVALID; ++it) { alpar@25: /// if (!dfs.reached(it)) { alpar@25: /// dfs.addSource(it); alpar@25: /// dfs.start(); alpar@25: /// } alpar@25: /// } alpar@25: ///\endcode alpar@25: /// alpar@26: /// The storing of the discovering order is more difficult because the alpar@25: /// ReachedMap should be readable in the dfs algorithm but the setting alpar@26: /// order map is not readable. Thus we must use the fork map: alpar@25: /// alpar@25: ///\code alpar@25: /// typedef Digraph::NodeMap OrderMap; alpar@25: /// OrderMap order(digraph); alpar@25: /// typedef SettingOrderBoolMap OrderSetterMap; alpar@25: /// OrderSetterMap setter(order); alpar@25: /// typedef Digraph::NodeMap StoreMap; alpar@25: /// StoreMap store(digraph); alpar@25: /// alpar@25: /// typedef ForkWriteMap ReachedMap; alpar@25: /// ReachedMap reached(store, setter); alpar@25: /// alpar@25: /// Dfs::DefReachedMap::Create dfs(digraph); alpar@25: /// dfs.reachedMap(reached); alpar@25: /// dfs.init(); alpar@25: /// for (NodeIt it(digraph); it != INVALID; ++it) { alpar@25: /// if (!dfs.reached(it)) { alpar@25: /// dfs.addSource(it); alpar@25: /// dfs.start(); alpar@25: /// } alpar@25: /// } alpar@25: ///\endcode alpar@25: template alpar@25: class SettingOrderBoolMap { alpar@25: public: alpar@25: typedef typename Map::Key Key; alpar@25: typedef bool Value; alpar@25: alpar@25: /// Constructor alpar@25: SettingOrderBoolMap(Map& _map) alpar@25: : map(_map), counter(0) {} alpar@25: alpar@25: /// Number of set operations. alpar@25: int num() const { alpar@25: return counter; alpar@25: } alpar@25: alpar@25: /// Setter function of the map alpar@25: void set(const Key& key, Value value) { alpar@25: if (value) { alpar@25: map.set(key, counter++); alpar@25: } alpar@25: } alpar@25: alpar@25: private: alpar@25: Map& map; alpar@25: int counter; alpar@25: }; alpar@25: alpar@25: /// @} alpar@25: } alpar@25: alpar@25: #endif // LEMON_MAPS_H