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