lemon/maps.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 25 Sep 2009 12:22:42 +0200
changeset 724 d8073df341f6
parent 722 b52189c479fb
child 725 11404088d1a5
permissions -rw-r--r--
Rename ValueIterator to ValueIt in graph maps (#302)
but keep ValueIterator as an alias in CrossRefMap
(only for reverse compatibility).
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2009
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_MAPS_H
    20 #define LEMON_MAPS_H
    21 
    22 #include <iterator>
    23 #include <functional>
    24 #include <vector>
    25 #include <map>
    26 
    27 #include <lemon/core.h>
    28 
    29 ///\file
    30 ///\ingroup maps
    31 ///\brief Miscellaneous property maps
    32 
    33 namespace lemon {
    34 
    35   /// \addtogroup maps
    36   /// @{
    37 
    38   /// Base class of maps.
    39 
    40   /// Base class of maps. It provides the necessary type definitions
    41   /// required by the map %concepts.
    42   template<typename K, typename V>
    43   class MapBase {
    44   public:
    45     /// \brief The key type of the map.
    46     typedef K Key;
    47     /// \brief The value type of the map.
    48     /// (The type of objects associated with the keys).
    49     typedef V Value;
    50   };
    51 
    52 
    53   /// Null map. (a.k.a. DoNothingMap)
    54 
    55   /// This map can be used if you have to provide a map only for
    56   /// its type definitions, or if you have to provide a writable map,
    57   /// but data written to it is not required (i.e. it will be sent to
    58   /// <tt>/dev/null</tt>).
    59   /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
    60   ///
    61   /// \sa ConstMap
    62   template<typename K, typename V>
    63   class NullMap : public MapBase<K, V> {
    64   public:
    65     ///\e
    66     typedef K Key;
    67     ///\e
    68     typedef V Value;
    69 
    70     /// Gives back a default constructed element.
    71     Value operator[](const Key&) const { return Value(); }
    72     /// Absorbs the value.
    73     void set(const Key&, const Value&) {}
    74   };
    75 
    76   /// Returns a \c NullMap class
    77 
    78   /// This function just returns a \c NullMap class.
    79   /// \relates NullMap
    80   template <typename K, typename V>
    81   NullMap<K, V> nullMap() {
    82     return NullMap<K, V>();
    83   }
    84 
    85 
    86   /// Constant map.
    87 
    88   /// This \ref concepts::ReadMap "readable map" assigns a specified
    89   /// value to each key.
    90   ///
    91   /// In other aspects it is equivalent to \c NullMap.
    92   /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
    93   /// concept, but it absorbs the data written to it.
    94   ///
    95   /// The simplest way of using this map is through the constMap()
    96   /// function.
    97   ///
    98   /// \sa NullMap
    99   /// \sa IdentityMap
   100   template<typename K, typename V>
   101   class ConstMap : public MapBase<K, V> {
   102   private:
   103     V _value;
   104   public:
   105     ///\e
   106     typedef K Key;
   107     ///\e
   108     typedef V Value;
   109 
   110     /// Default constructor
   111 
   112     /// Default constructor.
   113     /// The value of the map will be default constructed.
   114     ConstMap() {}
   115 
   116     /// Constructor with specified initial value
   117 
   118     /// Constructor with specified initial value.
   119     /// \param v The initial value of the map.
   120     ConstMap(const Value &v) : _value(v) {}
   121 
   122     /// Gives back the specified value.
   123     Value operator[](const Key&) const { return _value; }
   124 
   125     /// Absorbs the value.
   126     void set(const Key&, const Value&) {}
   127 
   128     /// Sets the value that is assigned to each key.
   129     void setAll(const Value &v) {
   130       _value = v;
   131     }
   132 
   133     template<typename V1>
   134     ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
   135   };
   136 
   137   /// Returns a \c ConstMap class
   138 
   139   /// This function just returns a \c ConstMap class.
   140   /// \relates ConstMap
   141   template<typename K, typename V>
   142   inline ConstMap<K, V> constMap(const V &v) {
   143     return ConstMap<K, V>(v);
   144   }
   145 
   146   template<typename K, typename V>
   147   inline ConstMap<K, V> constMap() {
   148     return ConstMap<K, V>();
   149   }
   150 
   151 
   152   template<typename T, T v>
   153   struct Const {};
   154 
   155   /// Constant map with inlined constant value.
   156 
   157   /// This \ref concepts::ReadMap "readable map" assigns a specified
   158   /// value to each key.
   159   ///
   160   /// In other aspects it is equivalent to \c NullMap.
   161   /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
   162   /// concept, but it absorbs the data written to it.
   163   ///
   164   /// The simplest way of using this map is through the constMap()
   165   /// function.
   166   ///
   167   /// \sa NullMap
   168   /// \sa IdentityMap
   169   template<typename K, typename V, V v>
   170   class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
   171   public:
   172     ///\e
   173     typedef K Key;
   174     ///\e
   175     typedef V Value;
   176 
   177     /// Constructor.
   178     ConstMap() {}
   179 
   180     /// Gives back the specified value.
   181     Value operator[](const Key&) const { return v; }
   182 
   183     /// Absorbs the value.
   184     void set(const Key&, const Value&) {}
   185   };
   186 
   187   /// Returns a \c ConstMap class with inlined constant value
   188 
   189   /// This function just returns a \c ConstMap class with inlined
   190   /// constant value.
   191   /// \relates ConstMap
   192   template<typename K, typename V, V v>
   193   inline ConstMap<K, Const<V, v> > constMap() {
   194     return ConstMap<K, Const<V, v> >();
   195   }
   196 
   197 
   198   /// Identity map.
   199 
   200   /// This \ref concepts::ReadMap "read-only map" gives back the given
   201   /// key as value without any modification.
   202   ///
   203   /// \sa ConstMap
   204   template <typename T>
   205   class IdentityMap : public MapBase<T, T> {
   206   public:
   207     ///\e
   208     typedef T Key;
   209     ///\e
   210     typedef T Value;
   211 
   212     /// Gives back the given value without any modification.
   213     Value operator[](const Key &k) const {
   214       return k;
   215     }
   216   };
   217 
   218   /// Returns an \c IdentityMap class
   219 
   220   /// This function just returns an \c IdentityMap class.
   221   /// \relates IdentityMap
   222   template<typename T>
   223   inline IdentityMap<T> identityMap() {
   224     return IdentityMap<T>();
   225   }
   226 
   227 
   228   /// \brief Map for storing values for integer keys from the range
   229   /// <tt>[0..size-1]</tt>.
   230   ///
   231   /// This map is essentially a wrapper for \c std::vector. It assigns
   232   /// values to integer keys from the range <tt>[0..size-1]</tt>.
   233   /// It can be used with some data structures, for example
   234   /// \c UnionFind, \c BinHeap, when the used items are small
   235   /// integers. This map conforms to the \ref concepts::ReferenceMap
   236   /// "ReferenceMap" concept.
   237   ///
   238   /// The simplest way of using this map is through the rangeMap()
   239   /// function.
   240   template <typename V>
   241   class RangeMap : public MapBase<int, V> {
   242     template <typename V1>
   243     friend class RangeMap;
   244   private:
   245 
   246     typedef std::vector<V> Vector;
   247     Vector _vector;
   248 
   249   public:
   250 
   251     /// Key type
   252     typedef int Key;
   253     /// Value type
   254     typedef V Value;
   255     /// Reference type
   256     typedef typename Vector::reference Reference;
   257     /// Const reference type
   258     typedef typename Vector::const_reference ConstReference;
   259 
   260     typedef True ReferenceMapTag;
   261 
   262   public:
   263 
   264     /// Constructor with specified default value.
   265     RangeMap(int size = 0, const Value &value = Value())
   266       : _vector(size, value) {}
   267 
   268     /// Constructs the map from an appropriate \c std::vector.
   269     template <typename V1>
   270     RangeMap(const std::vector<V1>& vector)
   271       : _vector(vector.begin(), vector.end()) {}
   272 
   273     /// Constructs the map from another \c RangeMap.
   274     template <typename V1>
   275     RangeMap(const RangeMap<V1> &c)
   276       : _vector(c._vector.begin(), c._vector.end()) {}
   277 
   278     /// Returns the size of the map.
   279     int size() {
   280       return _vector.size();
   281     }
   282 
   283     /// Resizes the map.
   284 
   285     /// Resizes the underlying \c std::vector container, so changes the
   286     /// keyset of the map.
   287     /// \param size The new size of the map. The new keyset will be the
   288     /// range <tt>[0..size-1]</tt>.
   289     /// \param value The default value to assign to the new keys.
   290     void resize(int size, const Value &value = Value()) {
   291       _vector.resize(size, value);
   292     }
   293 
   294   private:
   295 
   296     RangeMap& operator=(const RangeMap&);
   297 
   298   public:
   299 
   300     ///\e
   301     Reference operator[](const Key &k) {
   302       return _vector[k];
   303     }
   304 
   305     ///\e
   306     ConstReference operator[](const Key &k) const {
   307       return _vector[k];
   308     }
   309 
   310     ///\e
   311     void set(const Key &k, const Value &v) {
   312       _vector[k] = v;
   313     }
   314   };
   315 
   316   /// Returns a \c RangeMap class
   317 
   318   /// This function just returns a \c RangeMap class.
   319   /// \relates RangeMap
   320   template<typename V>
   321   inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
   322     return RangeMap<V>(size, value);
   323   }
   324 
   325   /// \brief Returns a \c RangeMap class created from an appropriate
   326   /// \c std::vector
   327 
   328   /// This function just returns a \c RangeMap class created from an
   329   /// appropriate \c std::vector.
   330   /// \relates RangeMap
   331   template<typename V>
   332   inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
   333     return RangeMap<V>(vector);
   334   }
   335 
   336 
   337   /// Map type based on \c std::map
   338 
   339   /// This map is essentially a wrapper for \c std::map with addition
   340   /// that you can specify a default value for the keys that are not
   341   /// stored actually. This value can be different from the default
   342   /// contructed value (i.e. \c %Value()).
   343   /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
   344   /// concept.
   345   ///
   346   /// This map is useful if a default value should be assigned to most of
   347   /// the keys and different values should be assigned only to a few
   348   /// keys (i.e. the map is "sparse").
   349   /// The name of this type also refers to this important usage.
   350   ///
   351   /// Apart form that this map can be used in many other cases since it
   352   /// is based on \c std::map, which is a general associative container.
   353   /// However keep in mind that it is usually not as efficient as other
   354   /// maps.
   355   ///
   356   /// The simplest way of using this map is through the sparseMap()
   357   /// function.
   358   template <typename K, typename V, typename Comp = std::less<K> >
   359   class SparseMap : public MapBase<K, V> {
   360     template <typename K1, typename V1, typename C1>
   361     friend class SparseMap;
   362   public:
   363 
   364     /// Key type
   365     typedef K Key;
   366     /// Value type
   367     typedef V Value;
   368     /// Reference type
   369     typedef Value& Reference;
   370     /// Const reference type
   371     typedef const Value& ConstReference;
   372 
   373     typedef True ReferenceMapTag;
   374 
   375   private:
   376 
   377     typedef std::map<K, V, Comp> Map;
   378     Map _map;
   379     Value _value;
   380 
   381   public:
   382 
   383     /// \brief Constructor with specified default value.
   384     SparseMap(const Value &value = Value()) : _value(value) {}
   385     /// \brief Constructs the map from an appropriate \c std::map, and
   386     /// explicitly specifies a default value.
   387     template <typename V1, typename Comp1>
   388     SparseMap(const std::map<Key, V1, Comp1> &map,
   389               const Value &value = Value())
   390       : _map(map.begin(), map.end()), _value(value) {}
   391 
   392     /// \brief Constructs the map from another \c SparseMap.
   393     template<typename V1, typename Comp1>
   394     SparseMap(const SparseMap<Key, V1, Comp1> &c)
   395       : _map(c._map.begin(), c._map.end()), _value(c._value) {}
   396 
   397   private:
   398 
   399     SparseMap& operator=(const SparseMap&);
   400 
   401   public:
   402 
   403     ///\e
   404     Reference operator[](const Key &k) {
   405       typename Map::iterator it = _map.lower_bound(k);
   406       if (it != _map.end() && !_map.key_comp()(k, it->first))
   407         return it->second;
   408       else
   409         return _map.insert(it, std::make_pair(k, _value))->second;
   410     }
   411 
   412     ///\e
   413     ConstReference operator[](const Key &k) const {
   414       typename Map::const_iterator it = _map.find(k);
   415       if (it != _map.end())
   416         return it->second;
   417       else
   418         return _value;
   419     }
   420 
   421     ///\e
   422     void set(const Key &k, const Value &v) {
   423       typename Map::iterator it = _map.lower_bound(k);
   424       if (it != _map.end() && !_map.key_comp()(k, it->first))
   425         it->second = v;
   426       else
   427         _map.insert(it, std::make_pair(k, v));
   428     }
   429 
   430     ///\e
   431     void setAll(const Value &v) {
   432       _value = v;
   433       _map.clear();
   434     }
   435   };
   436 
   437   /// Returns a \c SparseMap class
   438 
   439   /// This function just returns a \c SparseMap class with specified
   440   /// default value.
   441   /// \relates SparseMap
   442   template<typename K, typename V, typename Compare>
   443   inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
   444     return SparseMap<K, V, Compare>(value);
   445   }
   446 
   447   template<typename K, typename V>
   448   inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
   449     return SparseMap<K, V, std::less<K> >(value);
   450   }
   451 
   452   /// \brief Returns a \c SparseMap class created from an appropriate
   453   /// \c std::map
   454 
   455   /// This function just returns a \c SparseMap class created from an
   456   /// appropriate \c std::map.
   457   /// \relates SparseMap
   458   template<typename K, typename V, typename Compare>
   459   inline SparseMap<K, V, Compare>
   460     sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
   461   {
   462     return SparseMap<K, V, Compare>(map, value);
   463   }
   464 
   465   /// @}
   466 
   467   /// \addtogroup map_adaptors
   468   /// @{
   469 
   470   /// Composition of two maps
   471 
   472   /// This \ref concepts::ReadMap "read-only map" returns the
   473   /// composition of two given maps. That is to say, if \c m1 is of
   474   /// type \c M1 and \c m2 is of \c M2, then for
   475   /// \code
   476   ///   ComposeMap<M1, M2> cm(m1,m2);
   477   /// \endcode
   478   /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
   479   ///
   480   /// The \c Key type of the map is inherited from \c M2 and the
   481   /// \c Value type is from \c M1.
   482   /// \c M2::Value must be convertible to \c M1::Key.
   483   ///
   484   /// The simplest way of using this map is through the composeMap()
   485   /// function.
   486   ///
   487   /// \sa CombineMap
   488   template <typename M1, typename M2>
   489   class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
   490     const M1 &_m1;
   491     const M2 &_m2;
   492   public:
   493     ///\e
   494     typedef typename M2::Key Key;
   495     ///\e
   496     typedef typename M1::Value Value;
   497 
   498     /// Constructor
   499     ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
   500 
   501     ///\e
   502     typename MapTraits<M1>::ConstReturnValue
   503     operator[](const Key &k) const { return _m1[_m2[k]]; }
   504   };
   505 
   506   /// Returns a \c ComposeMap class
   507 
   508   /// This function just returns a \c ComposeMap class.
   509   ///
   510   /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
   511   /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
   512   /// will be equal to <tt>m1[m2[x]]</tt>.
   513   ///
   514   /// \relates ComposeMap
   515   template <typename M1, typename M2>
   516   inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
   517     return ComposeMap<M1, M2>(m1, m2);
   518   }
   519 
   520 
   521   /// Combination of two maps using an STL (binary) functor.
   522 
   523   /// This \ref concepts::ReadMap "read-only map" takes two maps and a
   524   /// binary functor and returns the combination of the two given maps
   525   /// using the functor.
   526   /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
   527   /// and \c f is of \c F, then for
   528   /// \code
   529   ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
   530   /// \endcode
   531   /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
   532   ///
   533   /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
   534   /// must be convertible to \c M2::Key) and the \c Value type is \c V.
   535   /// \c M2::Value and \c M1::Value must be convertible to the
   536   /// corresponding input parameter of \c F and the return type of \c F
   537   /// must be convertible to \c V.
   538   ///
   539   /// The simplest way of using this map is through the combineMap()
   540   /// function.
   541   ///
   542   /// \sa ComposeMap
   543   template<typename M1, typename M2, typename F,
   544            typename V = typename F::result_type>
   545   class CombineMap : public MapBase<typename M1::Key, V> {
   546     const M1 &_m1;
   547     const M2 &_m2;
   548     F _f;
   549   public:
   550     ///\e
   551     typedef typename M1::Key Key;
   552     ///\e
   553     typedef V Value;
   554 
   555     /// Constructor
   556     CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
   557       : _m1(m1), _m2(m2), _f(f) {}
   558     ///\e
   559     Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
   560   };
   561 
   562   /// Returns a \c CombineMap class
   563 
   564   /// This function just returns a \c CombineMap class.
   565   ///
   566   /// For example, if \c m1 and \c m2 are both maps with \c double
   567   /// values, then
   568   /// \code
   569   ///   combineMap(m1,m2,std::plus<double>())
   570   /// \endcode
   571   /// is equivalent to
   572   /// \code
   573   ///   addMap(m1,m2)
   574   /// \endcode
   575   ///
   576   /// This function is specialized for adaptable binary function
   577   /// classes and C++ functions.
   578   ///
   579   /// \relates CombineMap
   580   template<typename M1, typename M2, typename F, typename V>
   581   inline CombineMap<M1, M2, F, V>
   582   combineMap(const M1 &m1, const M2 &m2, const F &f) {
   583     return CombineMap<M1, M2, F, V>(m1,m2,f);
   584   }
   585 
   586   template<typename M1, typename M2, typename F>
   587   inline CombineMap<M1, M2, F, typename F::result_type>
   588   combineMap(const M1 &m1, const M2 &m2, const F &f) {
   589     return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
   590   }
   591 
   592   template<typename M1, typename M2, typename K1, typename K2, typename V>
   593   inline CombineMap<M1, M2, V (*)(K1, K2), V>
   594   combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
   595     return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
   596   }
   597 
   598 
   599   /// Converts an STL style (unary) functor to a map
   600 
   601   /// This \ref concepts::ReadMap "read-only map" returns the value
   602   /// of a given functor. Actually, it just wraps the functor and
   603   /// provides the \c Key and \c Value typedefs.
   604   ///
   605   /// Template parameters \c K and \c V will become its \c Key and
   606   /// \c Value. In most cases they have to be given explicitly because
   607   /// a functor typically does not provide \c argument_type and
   608   /// \c result_type typedefs.
   609   /// Parameter \c F is the type of the used functor.
   610   ///
   611   /// The simplest way of using this map is through the functorToMap()
   612   /// function.
   613   ///
   614   /// \sa MapToFunctor
   615   template<typename F,
   616            typename K = typename F::argument_type,
   617            typename V = typename F::result_type>
   618   class FunctorToMap : public MapBase<K, V> {
   619     F _f;
   620   public:
   621     ///\e
   622     typedef K Key;
   623     ///\e
   624     typedef V Value;
   625 
   626     /// Constructor
   627     FunctorToMap(const F &f = F()) : _f(f) {}
   628     ///\e
   629     Value operator[](const Key &k) const { return _f(k); }
   630   };
   631 
   632   /// Returns a \c FunctorToMap class
   633 
   634   /// This function just returns a \c FunctorToMap class.
   635   ///
   636   /// This function is specialized for adaptable binary function
   637   /// classes and C++ functions.
   638   ///
   639   /// \relates FunctorToMap
   640   template<typename K, typename V, typename F>
   641   inline FunctorToMap<F, K, V> functorToMap(const F &f) {
   642     return FunctorToMap<F, K, V>(f);
   643   }
   644 
   645   template <typename F>
   646   inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
   647     functorToMap(const F &f)
   648   {
   649     return FunctorToMap<F, typename F::argument_type,
   650       typename F::result_type>(f);
   651   }
   652 
   653   template <typename K, typename V>
   654   inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
   655     return FunctorToMap<V (*)(K), K, V>(f);
   656   }
   657 
   658 
   659   /// Converts a map to an STL style (unary) functor
   660 
   661   /// This class converts a map to an STL style (unary) functor.
   662   /// That is it provides an <tt>operator()</tt> to read its values.
   663   ///
   664   /// For the sake of convenience it also works as a usual
   665   /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
   666   /// and the \c Key and \c Value typedefs also exist.
   667   ///
   668   /// The simplest way of using this map is through the mapToFunctor()
   669   /// function.
   670   ///
   671   ///\sa FunctorToMap
   672   template <typename M>
   673   class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
   674     const M &_m;
   675   public:
   676     ///\e
   677     typedef typename M::Key Key;
   678     ///\e
   679     typedef typename M::Value Value;
   680 
   681     typedef typename M::Key argument_type;
   682     typedef typename M::Value result_type;
   683 
   684     /// Constructor
   685     MapToFunctor(const M &m) : _m(m) {}
   686     ///\e
   687     Value operator()(const Key &k) const { return _m[k]; }
   688     ///\e
   689     Value operator[](const Key &k) const { return _m[k]; }
   690   };
   691 
   692   /// Returns a \c MapToFunctor class
   693 
   694   /// This function just returns a \c MapToFunctor class.
   695   /// \relates MapToFunctor
   696   template<typename M>
   697   inline MapToFunctor<M> mapToFunctor(const M &m) {
   698     return MapToFunctor<M>(m);
   699   }
   700 
   701 
   702   /// \brief Map adaptor to convert the \c Value type of a map to
   703   /// another type using the default conversion.
   704 
   705   /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
   706   /// "readable map" to another type using the default conversion.
   707   /// The \c Key type of it is inherited from \c M and the \c Value
   708   /// type is \c V.
   709   /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
   710   ///
   711   /// The simplest way of using this map is through the convertMap()
   712   /// function.
   713   template <typename M, typename V>
   714   class ConvertMap : public MapBase<typename M::Key, V> {
   715     const M &_m;
   716   public:
   717     ///\e
   718     typedef typename M::Key Key;
   719     ///\e
   720     typedef V Value;
   721 
   722     /// Constructor
   723 
   724     /// Constructor.
   725     /// \param m The underlying map.
   726     ConvertMap(const M &m) : _m(m) {}
   727 
   728     ///\e
   729     Value operator[](const Key &k) const { return _m[k]; }
   730   };
   731 
   732   /// Returns a \c ConvertMap class
   733 
   734   /// This function just returns a \c ConvertMap class.
   735   /// \relates ConvertMap
   736   template<typename V, typename M>
   737   inline ConvertMap<M, V> convertMap(const M &map) {
   738     return ConvertMap<M, V>(map);
   739   }
   740 
   741 
   742   /// Applies all map setting operations to two maps
   743 
   744   /// This map has two \ref concepts::WriteMap "writable map" parameters
   745   /// and each write request will be passed to both of them.
   746   /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
   747   /// operations will return the corresponding values of \c M1.
   748   ///
   749   /// The \c Key and \c Value types are inherited from \c M1.
   750   /// The \c Key and \c Value of \c M2 must be convertible from those
   751   /// of \c M1.
   752   ///
   753   /// The simplest way of using this map is through the forkMap()
   754   /// function.
   755   template<typename  M1, typename M2>
   756   class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
   757     M1 &_m1;
   758     M2 &_m2;
   759   public:
   760     ///\e
   761     typedef typename M1::Key Key;
   762     ///\e
   763     typedef typename M1::Value Value;
   764 
   765     /// Constructor
   766     ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
   767     /// Returns the value associated with the given key in the first map.
   768     Value operator[](const Key &k) const { return _m1[k]; }
   769     /// Sets the value associated with the given key in both maps.
   770     void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
   771   };
   772 
   773   /// Returns a \c ForkMap class
   774 
   775   /// This function just returns a \c ForkMap class.
   776   /// \relates ForkMap
   777   template <typename M1, typename M2>
   778   inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
   779     return ForkMap<M1,M2>(m1,m2);
   780   }
   781 
   782 
   783   /// Sum of two maps
   784 
   785   /// This \ref concepts::ReadMap "read-only map" returns the sum
   786   /// of the values of the two given maps.
   787   /// Its \c Key and \c Value types are inherited from \c M1.
   788   /// The \c Key and \c Value of \c M2 must be convertible to those of
   789   /// \c M1.
   790   ///
   791   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
   792   /// \code
   793   ///   AddMap<M1,M2> am(m1,m2);
   794   /// \endcode
   795   /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
   796   ///
   797   /// The simplest way of using this map is through the addMap()
   798   /// function.
   799   ///
   800   /// \sa SubMap, MulMap, DivMap
   801   /// \sa ShiftMap, ShiftWriteMap
   802   template<typename M1, typename M2>
   803   class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
   804     const M1 &_m1;
   805     const M2 &_m2;
   806   public:
   807     ///\e
   808     typedef typename M1::Key Key;
   809     ///\e
   810     typedef typename M1::Value Value;
   811 
   812     /// Constructor
   813     AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
   814     ///\e
   815     Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
   816   };
   817 
   818   /// Returns an \c AddMap class
   819 
   820   /// This function just returns an \c AddMap class.
   821   ///
   822   /// For example, if \c m1 and \c m2 are both maps with \c double
   823   /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
   824   /// <tt>m1[x]+m2[x]</tt>.
   825   ///
   826   /// \relates AddMap
   827   template<typename M1, typename M2>
   828   inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
   829     return AddMap<M1, M2>(m1,m2);
   830   }
   831 
   832 
   833   /// Difference of two maps
   834 
   835   /// This \ref concepts::ReadMap "read-only map" returns the difference
   836   /// of the values of the two given maps.
   837   /// Its \c Key and \c Value types are inherited from \c M1.
   838   /// The \c Key and \c Value of \c M2 must be convertible to those of
   839   /// \c M1.
   840   ///
   841   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
   842   /// \code
   843   ///   SubMap<M1,M2> sm(m1,m2);
   844   /// \endcode
   845   /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
   846   ///
   847   /// The simplest way of using this map is through the subMap()
   848   /// function.
   849   ///
   850   /// \sa AddMap, MulMap, DivMap
   851   template<typename M1, typename M2>
   852   class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
   853     const M1 &_m1;
   854     const M2 &_m2;
   855   public:
   856     ///\e
   857     typedef typename M1::Key Key;
   858     ///\e
   859     typedef typename M1::Value Value;
   860 
   861     /// Constructor
   862     SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
   863     ///\e
   864     Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
   865   };
   866 
   867   /// Returns a \c SubMap class
   868 
   869   /// This function just returns a \c SubMap class.
   870   ///
   871   /// For example, if \c m1 and \c m2 are both maps with \c double
   872   /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
   873   /// <tt>m1[x]-m2[x]</tt>.
   874   ///
   875   /// \relates SubMap
   876   template<typename M1, typename M2>
   877   inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
   878     return SubMap<M1, M2>(m1,m2);
   879   }
   880 
   881 
   882   /// Product of two maps
   883 
   884   /// This \ref concepts::ReadMap "read-only map" returns the product
   885   /// of the values of the two given maps.
   886   /// Its \c Key and \c Value types are inherited from \c M1.
   887   /// The \c Key and \c Value of \c M2 must be convertible to those of
   888   /// \c M1.
   889   ///
   890   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
   891   /// \code
   892   ///   MulMap<M1,M2> mm(m1,m2);
   893   /// \endcode
   894   /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
   895   ///
   896   /// The simplest way of using this map is through the mulMap()
   897   /// function.
   898   ///
   899   /// \sa AddMap, SubMap, DivMap
   900   /// \sa ScaleMap, ScaleWriteMap
   901   template<typename M1, typename M2>
   902   class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
   903     const M1 &_m1;
   904     const M2 &_m2;
   905   public:
   906     ///\e
   907     typedef typename M1::Key Key;
   908     ///\e
   909     typedef typename M1::Value Value;
   910 
   911     /// Constructor
   912     MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
   913     ///\e
   914     Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
   915   };
   916 
   917   /// Returns a \c MulMap class
   918 
   919   /// This function just returns a \c MulMap class.
   920   ///
   921   /// For example, if \c m1 and \c m2 are both maps with \c double
   922   /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
   923   /// <tt>m1[x]*m2[x]</tt>.
   924   ///
   925   /// \relates MulMap
   926   template<typename M1, typename M2>
   927   inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
   928     return MulMap<M1, M2>(m1,m2);
   929   }
   930 
   931 
   932   /// Quotient of two maps
   933 
   934   /// This \ref concepts::ReadMap "read-only map" returns the quotient
   935   /// of the values of the two given maps.
   936   /// Its \c Key and \c Value types are inherited from \c M1.
   937   /// The \c Key and \c Value of \c M2 must be convertible to those of
   938   /// \c M1.
   939   ///
   940   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
   941   /// \code
   942   ///   DivMap<M1,M2> dm(m1,m2);
   943   /// \endcode
   944   /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
   945   ///
   946   /// The simplest way of using this map is through the divMap()
   947   /// function.
   948   ///
   949   /// \sa AddMap, SubMap, MulMap
   950   template<typename M1, typename M2>
   951   class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
   952     const M1 &_m1;
   953     const M2 &_m2;
   954   public:
   955     ///\e
   956     typedef typename M1::Key Key;
   957     ///\e
   958     typedef typename M1::Value Value;
   959 
   960     /// Constructor
   961     DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
   962     ///\e
   963     Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
   964   };
   965 
   966   /// Returns a \c DivMap class
   967 
   968   /// This function just returns a \c DivMap class.
   969   ///
   970   /// For example, if \c m1 and \c m2 are both maps with \c double
   971   /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
   972   /// <tt>m1[x]/m2[x]</tt>.
   973   ///
   974   /// \relates DivMap
   975   template<typename M1, typename M2>
   976   inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
   977     return DivMap<M1, M2>(m1,m2);
   978   }
   979 
   980 
   981   /// Shifts a map with a constant.
   982 
   983   /// This \ref concepts::ReadMap "read-only map" returns the sum of
   984   /// the given map and a constant value (i.e. it shifts the map with
   985   /// the constant). Its \c Key and \c Value are inherited from \c M.
   986   ///
   987   /// Actually,
   988   /// \code
   989   ///   ShiftMap<M> sh(m,v);
   990   /// \endcode
   991   /// is equivalent to
   992   /// \code
   993   ///   ConstMap<M::Key, M::Value> cm(v);
   994   ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
   995   /// \endcode
   996   ///
   997   /// The simplest way of using this map is through the shiftMap()
   998   /// function.
   999   ///
  1000   /// \sa ShiftWriteMap
  1001   template<typename M, typename C = typename M::Value>
  1002   class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
  1003     const M &_m;
  1004     C _v;
  1005   public:
  1006     ///\e
  1007     typedef typename M::Key Key;
  1008     ///\e
  1009     typedef typename M::Value Value;
  1010 
  1011     /// Constructor
  1012 
  1013     /// Constructor.
  1014     /// \param m The undelying map.
  1015     /// \param v The constant value.
  1016     ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
  1017     ///\e
  1018     Value operator[](const Key &k) const { return _m[k]+_v; }
  1019   };
  1020 
  1021   /// Shifts a map with a constant (read-write version).
  1022 
  1023   /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
  1024   /// of the given map and a constant value (i.e. it shifts the map with
  1025   /// the constant). Its \c Key and \c Value are inherited from \c M.
  1026   /// It makes also possible to write the map.
  1027   ///
  1028   /// The simplest way of using this map is through the shiftWriteMap()
  1029   /// function.
  1030   ///
  1031   /// \sa ShiftMap
  1032   template<typename M, typename C = typename M::Value>
  1033   class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
  1034     M &_m;
  1035     C _v;
  1036   public:
  1037     ///\e
  1038     typedef typename M::Key Key;
  1039     ///\e
  1040     typedef typename M::Value Value;
  1041 
  1042     /// Constructor
  1043 
  1044     /// Constructor.
  1045     /// \param m The undelying map.
  1046     /// \param v The constant value.
  1047     ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
  1048     ///\e
  1049     Value operator[](const Key &k) const { return _m[k]+_v; }
  1050     ///\e
  1051     void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
  1052   };
  1053 
  1054   /// Returns a \c ShiftMap class
  1055 
  1056   /// This function just returns a \c ShiftMap class.
  1057   ///
  1058   /// For example, if \c m is a map with \c double values and \c v is
  1059   /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
  1060   /// <tt>m[x]+v</tt>.
  1061   ///
  1062   /// \relates ShiftMap
  1063   template<typename M, typename C>
  1064   inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
  1065     return ShiftMap<M, C>(m,v);
  1066   }
  1067 
  1068   /// Returns a \c ShiftWriteMap class
  1069 
  1070   /// This function just returns a \c ShiftWriteMap class.
  1071   ///
  1072   /// For example, if \c m is a map with \c double values and \c v is
  1073   /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
  1074   /// <tt>m[x]+v</tt>.
  1075   /// Moreover it makes also possible to write the map.
  1076   ///
  1077   /// \relates ShiftWriteMap
  1078   template<typename M, typename C>
  1079   inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
  1080     return ShiftWriteMap<M, C>(m,v);
  1081   }
  1082 
  1083 
  1084   /// Scales a map with a constant.
  1085 
  1086   /// This \ref concepts::ReadMap "read-only map" returns the value of
  1087   /// the given map multiplied from the left side with a constant value.
  1088   /// Its \c Key and \c Value are inherited from \c M.
  1089   ///
  1090   /// Actually,
  1091   /// \code
  1092   ///   ScaleMap<M> sc(m,v);
  1093   /// \endcode
  1094   /// is equivalent to
  1095   /// \code
  1096   ///   ConstMap<M::Key, M::Value> cm(v);
  1097   ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
  1098   /// \endcode
  1099   ///
  1100   /// The simplest way of using this map is through the scaleMap()
  1101   /// function.
  1102   ///
  1103   /// \sa ScaleWriteMap
  1104   template<typename M, typename C = typename M::Value>
  1105   class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
  1106     const M &_m;
  1107     C _v;
  1108   public:
  1109     ///\e
  1110     typedef typename M::Key Key;
  1111     ///\e
  1112     typedef typename M::Value Value;
  1113 
  1114     /// Constructor
  1115 
  1116     /// Constructor.
  1117     /// \param m The undelying map.
  1118     /// \param v The constant value.
  1119     ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
  1120     ///\e
  1121     Value operator[](const Key &k) const { return _v*_m[k]; }
  1122   };
  1123 
  1124   /// Scales a map with a constant (read-write version).
  1125 
  1126   /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
  1127   /// the given map multiplied from the left side with a constant value.
  1128   /// Its \c Key and \c Value are inherited from \c M.
  1129   /// It can also be used as write map if the \c / operator is defined
  1130   /// between \c Value and \c C and the given multiplier is not zero.
  1131   ///
  1132   /// The simplest way of using this map is through the scaleWriteMap()
  1133   /// function.
  1134   ///
  1135   /// \sa ScaleMap
  1136   template<typename M, typename C = typename M::Value>
  1137   class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
  1138     M &_m;
  1139     C _v;
  1140   public:
  1141     ///\e
  1142     typedef typename M::Key Key;
  1143     ///\e
  1144     typedef typename M::Value Value;
  1145 
  1146     /// Constructor
  1147 
  1148     /// Constructor.
  1149     /// \param m The undelying map.
  1150     /// \param v The constant value.
  1151     ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
  1152     ///\e
  1153     Value operator[](const Key &k) const { return _v*_m[k]; }
  1154     ///\e
  1155     void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
  1156   };
  1157 
  1158   /// Returns a \c ScaleMap class
  1159 
  1160   /// This function just returns a \c ScaleMap class.
  1161   ///
  1162   /// For example, if \c m is a map with \c double values and \c v is
  1163   /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
  1164   /// <tt>v*m[x]</tt>.
  1165   ///
  1166   /// \relates ScaleMap
  1167   template<typename M, typename C>
  1168   inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
  1169     return ScaleMap<M, C>(m,v);
  1170   }
  1171 
  1172   /// Returns a \c ScaleWriteMap class
  1173 
  1174   /// This function just returns a \c ScaleWriteMap class.
  1175   ///
  1176   /// For example, if \c m is a map with \c double values and \c v is
  1177   /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
  1178   /// <tt>v*m[x]</tt>.
  1179   /// Moreover it makes also possible to write the map.
  1180   ///
  1181   /// \relates ScaleWriteMap
  1182   template<typename M, typename C>
  1183   inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
  1184     return ScaleWriteMap<M, C>(m,v);
  1185   }
  1186 
  1187 
  1188   /// Negative of a map
  1189 
  1190   /// This \ref concepts::ReadMap "read-only map" returns the negative
  1191   /// of the values of the given map (using the unary \c - operator).
  1192   /// Its \c Key and \c Value are inherited from \c M.
  1193   ///
  1194   /// If M::Value is \c int, \c double etc., then
  1195   /// \code
  1196   ///   NegMap<M> neg(m);
  1197   /// \endcode
  1198   /// is equivalent to
  1199   /// \code
  1200   ///   ScaleMap<M> neg(m,-1);
  1201   /// \endcode
  1202   ///
  1203   /// The simplest way of using this map is through the negMap()
  1204   /// function.
  1205   ///
  1206   /// \sa NegWriteMap
  1207   template<typename M>
  1208   class NegMap : public MapBase<typename M::Key, typename M::Value> {
  1209     const M& _m;
  1210   public:
  1211     ///\e
  1212     typedef typename M::Key Key;
  1213     ///\e
  1214     typedef typename M::Value Value;
  1215 
  1216     /// Constructor
  1217     NegMap(const M &m) : _m(m) {}
  1218     ///\e
  1219     Value operator[](const Key &k) const { return -_m[k]; }
  1220   };
  1221 
  1222   /// Negative of a map (read-write version)
  1223 
  1224   /// This \ref concepts::ReadWriteMap "read-write map" returns the
  1225   /// negative of the values of the given map (using the unary \c -
  1226   /// operator).
  1227   /// Its \c Key and \c Value are inherited from \c M.
  1228   /// It makes also possible to write the map.
  1229   ///
  1230   /// If M::Value is \c int, \c double etc., then
  1231   /// \code
  1232   ///   NegWriteMap<M> neg(m);
  1233   /// \endcode
  1234   /// is equivalent to
  1235   /// \code
  1236   ///   ScaleWriteMap<M> neg(m,-1);
  1237   /// \endcode
  1238   ///
  1239   /// The simplest way of using this map is through the negWriteMap()
  1240   /// function.
  1241   ///
  1242   /// \sa NegMap
  1243   template<typename M>
  1244   class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
  1245     M &_m;
  1246   public:
  1247     ///\e
  1248     typedef typename M::Key Key;
  1249     ///\e
  1250     typedef typename M::Value Value;
  1251 
  1252     /// Constructor
  1253     NegWriteMap(M &m) : _m(m) {}
  1254     ///\e
  1255     Value operator[](const Key &k) const { return -_m[k]; }
  1256     ///\e
  1257     void set(const Key &k, const Value &v) { _m.set(k, -v); }
  1258   };
  1259 
  1260   /// Returns a \c NegMap class
  1261 
  1262   /// This function just returns a \c NegMap class.
  1263   ///
  1264   /// For example, if \c m is a map with \c double values, then
  1265   /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
  1266   ///
  1267   /// \relates NegMap
  1268   template <typename M>
  1269   inline NegMap<M> negMap(const M &m) {
  1270     return NegMap<M>(m);
  1271   }
  1272 
  1273   /// Returns a \c NegWriteMap class
  1274 
  1275   /// This function just returns a \c NegWriteMap class.
  1276   ///
  1277   /// For example, if \c m is a map with \c double values, then
  1278   /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
  1279   /// Moreover it makes also possible to write the map.
  1280   ///
  1281   /// \relates NegWriteMap
  1282   template <typename M>
  1283   inline NegWriteMap<M> negWriteMap(M &m) {
  1284     return NegWriteMap<M>(m);
  1285   }
  1286 
  1287 
  1288   /// Absolute value of a map
  1289 
  1290   /// This \ref concepts::ReadMap "read-only map" returns the absolute
  1291   /// value of the values of the given map.
  1292   /// Its \c Key and \c Value are inherited from \c M.
  1293   /// \c Value must be comparable to \c 0 and the unary \c -
  1294   /// operator must be defined for it, of course.
  1295   ///
  1296   /// The simplest way of using this map is through the absMap()
  1297   /// function.
  1298   template<typename M>
  1299   class AbsMap : public MapBase<typename M::Key, typename M::Value> {
  1300     const M &_m;
  1301   public:
  1302     ///\e
  1303     typedef typename M::Key Key;
  1304     ///\e
  1305     typedef typename M::Value Value;
  1306 
  1307     /// Constructor
  1308     AbsMap(const M &m) : _m(m) {}
  1309     ///\e
  1310     Value operator[](const Key &k) const {
  1311       Value tmp = _m[k];
  1312       return tmp >= 0 ? tmp : -tmp;
  1313     }
  1314 
  1315   };
  1316 
  1317   /// Returns an \c AbsMap class
  1318 
  1319   /// This function just returns an \c AbsMap class.
  1320   ///
  1321   /// For example, if \c m is a map with \c double values, then
  1322   /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
  1323   /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
  1324   /// negative.
  1325   ///
  1326   /// \relates AbsMap
  1327   template<typename M>
  1328   inline AbsMap<M> absMap(const M &m) {
  1329     return AbsMap<M>(m);
  1330   }
  1331 
  1332   /// @}
  1333 
  1334   // Logical maps and map adaptors:
  1335 
  1336   /// \addtogroup maps
  1337   /// @{
  1338 
  1339   /// Constant \c true map.
  1340 
  1341   /// This \ref concepts::ReadMap "read-only map" assigns \c true to
  1342   /// each key.
  1343   ///
  1344   /// Note that
  1345   /// \code
  1346   ///   TrueMap<K> tm;
  1347   /// \endcode
  1348   /// is equivalent to
  1349   /// \code
  1350   ///   ConstMap<K,bool> tm(true);
  1351   /// \endcode
  1352   ///
  1353   /// \sa FalseMap
  1354   /// \sa ConstMap
  1355   template <typename K>
  1356   class TrueMap : public MapBase<K, bool> {
  1357   public:
  1358     ///\e
  1359     typedef K Key;
  1360     ///\e
  1361     typedef bool Value;
  1362 
  1363     /// Gives back \c true.
  1364     Value operator[](const Key&) const { return true; }
  1365   };
  1366 
  1367   /// Returns a \c TrueMap class
  1368 
  1369   /// This function just returns a \c TrueMap class.
  1370   /// \relates TrueMap
  1371   template<typename K>
  1372   inline TrueMap<K> trueMap() {
  1373     return TrueMap<K>();
  1374   }
  1375 
  1376 
  1377   /// Constant \c false map.
  1378 
  1379   /// This \ref concepts::ReadMap "read-only map" assigns \c false to
  1380   /// each key.
  1381   ///
  1382   /// Note that
  1383   /// \code
  1384   ///   FalseMap<K> fm;
  1385   /// \endcode
  1386   /// is equivalent to
  1387   /// \code
  1388   ///   ConstMap<K,bool> fm(false);
  1389   /// \endcode
  1390   ///
  1391   /// \sa TrueMap
  1392   /// \sa ConstMap
  1393   template <typename K>
  1394   class FalseMap : public MapBase<K, bool> {
  1395   public:
  1396     ///\e
  1397     typedef K Key;
  1398     ///\e
  1399     typedef bool Value;
  1400 
  1401     /// Gives back \c false.
  1402     Value operator[](const Key&) const { return false; }
  1403   };
  1404 
  1405   /// Returns a \c FalseMap class
  1406 
  1407   /// This function just returns a \c FalseMap class.
  1408   /// \relates FalseMap
  1409   template<typename K>
  1410   inline FalseMap<K> falseMap() {
  1411     return FalseMap<K>();
  1412   }
  1413 
  1414   /// @}
  1415 
  1416   /// \addtogroup map_adaptors
  1417   /// @{
  1418 
  1419   /// Logical 'and' of two maps
  1420 
  1421   /// This \ref concepts::ReadMap "read-only map" returns the logical
  1422   /// 'and' of the values of the two given maps.
  1423   /// Its \c Key type is inherited from \c M1 and its \c Value type is
  1424   /// \c bool. \c M2::Key must be convertible to \c M1::Key.
  1425   ///
  1426   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
  1427   /// \code
  1428   ///   AndMap<M1,M2> am(m1,m2);
  1429   /// \endcode
  1430   /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
  1431   ///
  1432   /// The simplest way of using this map is through the andMap()
  1433   /// function.
  1434   ///
  1435   /// \sa OrMap
  1436   /// \sa NotMap, NotWriteMap
  1437   template<typename M1, typename M2>
  1438   class AndMap : public MapBase<typename M1::Key, bool> {
  1439     const M1 &_m1;
  1440     const M2 &_m2;
  1441   public:
  1442     ///\e
  1443     typedef typename M1::Key Key;
  1444     ///\e
  1445     typedef bool Value;
  1446 
  1447     /// Constructor
  1448     AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
  1449     ///\e
  1450     Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
  1451   };
  1452 
  1453   /// Returns an \c AndMap class
  1454 
  1455   /// This function just returns an \c AndMap class.
  1456   ///
  1457   /// For example, if \c m1 and \c m2 are both maps with \c bool values,
  1458   /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
  1459   /// <tt>m1[x]&&m2[x]</tt>.
  1460   ///
  1461   /// \relates AndMap
  1462   template<typename M1, typename M2>
  1463   inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
  1464     return AndMap<M1, M2>(m1,m2);
  1465   }
  1466 
  1467 
  1468   /// Logical 'or' of two maps
  1469 
  1470   /// This \ref concepts::ReadMap "read-only map" returns the logical
  1471   /// 'or' of the values of the two given maps.
  1472   /// Its \c Key type is inherited from \c M1 and its \c Value type is
  1473   /// \c bool. \c M2::Key must be convertible to \c M1::Key.
  1474   ///
  1475   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
  1476   /// \code
  1477   ///   OrMap<M1,M2> om(m1,m2);
  1478   /// \endcode
  1479   /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
  1480   ///
  1481   /// The simplest way of using this map is through the orMap()
  1482   /// function.
  1483   ///
  1484   /// \sa AndMap
  1485   /// \sa NotMap, NotWriteMap
  1486   template<typename M1, typename M2>
  1487   class OrMap : public MapBase<typename M1::Key, bool> {
  1488     const M1 &_m1;
  1489     const M2 &_m2;
  1490   public:
  1491     ///\e
  1492     typedef typename M1::Key Key;
  1493     ///\e
  1494     typedef bool Value;
  1495 
  1496     /// Constructor
  1497     OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
  1498     ///\e
  1499     Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
  1500   };
  1501 
  1502   /// Returns an \c OrMap class
  1503 
  1504   /// This function just returns an \c OrMap class.
  1505   ///
  1506   /// For example, if \c m1 and \c m2 are both maps with \c bool values,
  1507   /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
  1508   /// <tt>m1[x]||m2[x]</tt>.
  1509   ///
  1510   /// \relates OrMap
  1511   template<typename M1, typename M2>
  1512   inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
  1513     return OrMap<M1, M2>(m1,m2);
  1514   }
  1515 
  1516 
  1517   /// Logical 'not' of a map
  1518 
  1519   /// This \ref concepts::ReadMap "read-only map" returns the logical
  1520   /// negation of the values of the given map.
  1521   /// Its \c Key is inherited from \c M and its \c Value is \c bool.
  1522   ///
  1523   /// The simplest way of using this map is through the notMap()
  1524   /// function.
  1525   ///
  1526   /// \sa NotWriteMap
  1527   template <typename M>
  1528   class NotMap : public MapBase<typename M::Key, bool> {
  1529     const M &_m;
  1530   public:
  1531     ///\e
  1532     typedef typename M::Key Key;
  1533     ///\e
  1534     typedef bool Value;
  1535 
  1536     /// Constructor
  1537     NotMap(const M &m) : _m(m) {}
  1538     ///\e
  1539     Value operator[](const Key &k) const { return !_m[k]; }
  1540   };
  1541 
  1542   /// Logical 'not' of a map (read-write version)
  1543 
  1544   /// This \ref concepts::ReadWriteMap "read-write map" returns the
  1545   /// logical negation of the values of the given map.
  1546   /// Its \c Key is inherited from \c M and its \c Value is \c bool.
  1547   /// It makes also possible to write the map. When a value is set,
  1548   /// the opposite value is set to the original map.
  1549   ///
  1550   /// The simplest way of using this map is through the notWriteMap()
  1551   /// function.
  1552   ///
  1553   /// \sa NotMap
  1554   template <typename M>
  1555   class NotWriteMap : public MapBase<typename M::Key, bool> {
  1556     M &_m;
  1557   public:
  1558     ///\e
  1559     typedef typename M::Key Key;
  1560     ///\e
  1561     typedef bool Value;
  1562 
  1563     /// Constructor
  1564     NotWriteMap(M &m) : _m(m) {}
  1565     ///\e
  1566     Value operator[](const Key &k) const { return !_m[k]; }
  1567     ///\e
  1568     void set(const Key &k, bool v) { _m.set(k, !v); }
  1569   };
  1570 
  1571   /// Returns a \c NotMap class
  1572 
  1573   /// This function just returns a \c NotMap class.
  1574   ///
  1575   /// For example, if \c m is a map with \c bool values, then
  1576   /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
  1577   ///
  1578   /// \relates NotMap
  1579   template <typename M>
  1580   inline NotMap<M> notMap(const M &m) {
  1581     return NotMap<M>(m);
  1582   }
  1583 
  1584   /// Returns a \c NotWriteMap class
  1585 
  1586   /// This function just returns a \c NotWriteMap class.
  1587   ///
  1588   /// For example, if \c m is a map with \c bool values, then
  1589   /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
  1590   /// Moreover it makes also possible to write the map.
  1591   ///
  1592   /// \relates NotWriteMap
  1593   template <typename M>
  1594   inline NotWriteMap<M> notWriteMap(M &m) {
  1595     return NotWriteMap<M>(m);
  1596   }
  1597 
  1598 
  1599   /// Combination of two maps using the \c == operator
  1600 
  1601   /// This \ref concepts::ReadMap "read-only map" assigns \c true to
  1602   /// the keys for which the corresponding values of the two maps are
  1603   /// equal.
  1604   /// Its \c Key type is inherited from \c M1 and its \c Value type is
  1605   /// \c bool. \c M2::Key must be convertible to \c M1::Key.
  1606   ///
  1607   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
  1608   /// \code
  1609   ///   EqualMap<M1,M2> em(m1,m2);
  1610   /// \endcode
  1611   /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
  1612   ///
  1613   /// The simplest way of using this map is through the equalMap()
  1614   /// function.
  1615   ///
  1616   /// \sa LessMap
  1617   template<typename M1, typename M2>
  1618   class EqualMap : public MapBase<typename M1::Key, bool> {
  1619     const M1 &_m1;
  1620     const M2 &_m2;
  1621   public:
  1622     ///\e
  1623     typedef typename M1::Key Key;
  1624     ///\e
  1625     typedef bool Value;
  1626 
  1627     /// Constructor
  1628     EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
  1629     ///\e
  1630     Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
  1631   };
  1632 
  1633   /// Returns an \c EqualMap class
  1634 
  1635   /// This function just returns an \c EqualMap class.
  1636   ///
  1637   /// For example, if \c m1 and \c m2 are maps with keys and values of
  1638   /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
  1639   /// <tt>m1[x]==m2[x]</tt>.
  1640   ///
  1641   /// \relates EqualMap
  1642   template<typename M1, typename M2>
  1643   inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
  1644     return EqualMap<M1, M2>(m1,m2);
  1645   }
  1646 
  1647 
  1648   /// Combination of two maps using the \c < operator
  1649 
  1650   /// This \ref concepts::ReadMap "read-only map" assigns \c true to
  1651   /// the keys for which the corresponding value of the first map is
  1652   /// less then the value of the second map.
  1653   /// Its \c Key type is inherited from \c M1 and its \c Value type is
  1654   /// \c bool. \c M2::Key must be convertible to \c M1::Key.
  1655   ///
  1656   /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
  1657   /// \code
  1658   ///   LessMap<M1,M2> lm(m1,m2);
  1659   /// \endcode
  1660   /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
  1661   ///
  1662   /// The simplest way of using this map is through the lessMap()
  1663   /// function.
  1664   ///
  1665   /// \sa EqualMap
  1666   template<typename M1, typename M2>
  1667   class LessMap : public MapBase<typename M1::Key, bool> {
  1668     const M1 &_m1;
  1669     const M2 &_m2;
  1670   public:
  1671     ///\e
  1672     typedef typename M1::Key Key;
  1673     ///\e
  1674     typedef bool Value;
  1675 
  1676     /// Constructor
  1677     LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
  1678     ///\e
  1679     Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
  1680   };
  1681 
  1682   /// Returns an \c LessMap class
  1683 
  1684   /// This function just returns an \c LessMap class.
  1685   ///
  1686   /// For example, if \c m1 and \c m2 are maps with keys and values of
  1687   /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
  1688   /// <tt>m1[x]<m2[x]</tt>.
  1689   ///
  1690   /// \relates LessMap
  1691   template<typename M1, typename M2>
  1692   inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
  1693     return LessMap<M1, M2>(m1,m2);
  1694   }
  1695 
  1696   namespace _maps_bits {
  1697 
  1698     template <typename _Iterator, typename Enable = void>
  1699     struct IteratorTraits {
  1700       typedef typename std::iterator_traits<_Iterator>::value_type Value;
  1701     };
  1702 
  1703     template <typename _Iterator>
  1704     struct IteratorTraits<_Iterator,
  1705       typename exists<typename _Iterator::container_type>::type>
  1706     {
  1707       typedef typename _Iterator::container_type::value_type Value;
  1708     };
  1709 
  1710   }
  1711 
  1712   /// @}
  1713 
  1714   /// \addtogroup maps
  1715   /// @{
  1716 
  1717   /// \brief Writable bool map for logging each \c true assigned element
  1718   ///
  1719   /// A \ref concepts::WriteMap "writable" bool map for logging
  1720   /// each \c true assigned element, i.e it copies subsequently each
  1721   /// keys set to \c true to the given iterator.
  1722   /// The most important usage of it is storing certain nodes or arcs
  1723   /// that were marked \c true by an algorithm.
  1724   ///
  1725   /// There are several algorithms that provide solutions through bool
  1726   /// maps and most of them assign \c true at most once for each key.
  1727   /// In these cases it is a natural request to store each \c true
  1728   /// assigned elements (in order of the assignment), which can be
  1729   /// easily done with LoggerBoolMap.
  1730   ///
  1731   /// The simplest way of using this map is through the loggerBoolMap()
  1732   /// function.
  1733   ///
  1734   /// \tparam IT The type of the iterator.
  1735   /// \tparam KEY The key type of the map. The default value set
  1736   /// according to the iterator type should work in most cases.
  1737   ///
  1738   /// \note The container of the iterator must contain enough space
  1739   /// for the elements or the iterator should be an inserter iterator.
  1740 #ifdef DOXYGEN
  1741   template <typename IT, typename KEY>
  1742 #else
  1743   template <typename IT,
  1744             typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
  1745 #endif
  1746   class LoggerBoolMap : public MapBase<KEY, bool> {
  1747   public:
  1748 
  1749     ///\e
  1750     typedef KEY Key;
  1751     ///\e
  1752     typedef bool Value;
  1753     ///\e
  1754     typedef IT Iterator;
  1755 
  1756     /// Constructor
  1757     LoggerBoolMap(Iterator it)
  1758       : _begin(it), _end(it) {}
  1759 
  1760     /// Gives back the given iterator set for the first key
  1761     Iterator begin() const {
  1762       return _begin;
  1763     }
  1764 
  1765     /// Gives back the the 'after the last' iterator
  1766     Iterator end() const {
  1767       return _end;
  1768     }
  1769 
  1770     /// The set function of the map
  1771     void set(const Key& key, Value value) {
  1772       if (value) {
  1773         *_end++ = key;
  1774       }
  1775     }
  1776 
  1777   private:
  1778     Iterator _begin;
  1779     Iterator _end;
  1780   };
  1781 
  1782   /// Returns a \c LoggerBoolMap class
  1783 
  1784   /// This function just returns a \c LoggerBoolMap class.
  1785   ///
  1786   /// The most important usage of it is storing certain nodes or arcs
  1787   /// that were marked \c true by an algorithm.
  1788   /// For example it makes easier to store the nodes in the processing
  1789   /// order of Dfs algorithm, as the following examples show.
  1790   /// \code
  1791   ///   std::vector<Node> v;
  1792   ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
  1793   /// \endcode
  1794   /// \code
  1795   ///   std::vector<Node> v(countNodes(g));
  1796   ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
  1797   /// \endcode
  1798   ///
  1799   /// \note The container of the iterator must contain enough space
  1800   /// for the elements or the iterator should be an inserter iterator.
  1801   ///
  1802   /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
  1803   /// it cannot be used when a readable map is needed, for example as
  1804   /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
  1805   ///
  1806   /// \relates LoggerBoolMap
  1807   template<typename Iterator>
  1808   inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
  1809     return LoggerBoolMap<Iterator>(it);
  1810   }
  1811 
  1812   /// @}
  1813 
  1814   /// \addtogroup graph_maps
  1815   /// @{
  1816 
  1817   /// \brief Provides an immutable and unique id for each item in a graph.
  1818   ///
  1819   /// IdMap provides a unique and immutable id for each item of the
  1820   /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
  1821   ///  - \b unique: different items get different ids,
  1822   ///  - \b immutable: the id of an item does not change (even if you
  1823   ///    delete other nodes).
  1824   ///
  1825   /// Using this map you get access (i.e. can read) the inner id values of
  1826   /// the items stored in the graph, which is returned by the \c id()
  1827   /// function of the graph. This map can be inverted with its member
  1828   /// class \c InverseMap or with the \c operator()() member.
  1829   ///
  1830   /// \tparam GR The graph type.
  1831   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  1832   /// \c GR::Edge).
  1833   ///
  1834   /// \see RangeIdMap
  1835   template <typename GR, typename K>
  1836   class IdMap : public MapBase<K, int> {
  1837   public:
  1838     /// The graph type of IdMap.
  1839     typedef GR Graph;
  1840     typedef GR Digraph;
  1841     /// The key type of IdMap (\c Node, \c Arc or \c Edge).
  1842     typedef K Item;
  1843     /// The key type of IdMap (\c Node, \c Arc or \c Edge).
  1844     typedef K Key;
  1845     /// The value type of IdMap.
  1846     typedef int Value;
  1847 
  1848     /// \brief Constructor.
  1849     ///
  1850     /// Constructor of the map.
  1851     explicit IdMap(const Graph& graph) : _graph(&graph) {}
  1852 
  1853     /// \brief Gives back the \e id of the item.
  1854     ///
  1855     /// Gives back the immutable and unique \e id of the item.
  1856     int operator[](const Item& item) const { return _graph->id(item);}
  1857 
  1858     /// \brief Gives back the \e item by its id.
  1859     ///
  1860     /// Gives back the \e item by its id.
  1861     Item operator()(int id) { return _graph->fromId(id, Item()); }
  1862 
  1863   private:
  1864     const Graph* _graph;
  1865 
  1866   public:
  1867 
  1868     /// \brief The inverse map type of IdMap.
  1869     ///
  1870     /// The inverse map type of IdMap. The subscript operator gives back
  1871     /// an item by its id.
  1872     /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
  1873     /// \see inverse()
  1874     class InverseMap {
  1875     public:
  1876 
  1877       /// \brief Constructor.
  1878       ///
  1879       /// Constructor for creating an id-to-item map.
  1880       explicit InverseMap(const Graph& graph) : _graph(&graph) {}
  1881 
  1882       /// \brief Constructor.
  1883       ///
  1884       /// Constructor for creating an id-to-item map.
  1885       explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
  1886 
  1887       /// \brief Gives back an item by its id.
  1888       ///
  1889       /// Gives back an item by its id.
  1890       Item operator[](int id) const { return _graph->fromId(id, Item());}
  1891 
  1892     private:
  1893       const Graph* _graph;
  1894     };
  1895 
  1896     /// \brief Gives back the inverse of the map.
  1897     ///
  1898     /// Gives back the inverse of the IdMap.
  1899     InverseMap inverse() const { return InverseMap(*_graph);}
  1900   };
  1901 
  1902 
  1903   /// \brief General cross reference graph map type.
  1904 
  1905   /// This class provides simple invertable graph maps.
  1906   /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
  1907   /// and if a key is set to a new value, then stores it in the inverse map.
  1908   /// The graph items can be accessed by their values either using
  1909   /// \c InverseMap or \c operator()(), and the values of the map can be
  1910   /// accessed with an STL compatible forward iterator (\c ValueIt).
  1911   /// 
  1912   /// This map is intended to be used when all associated values are
  1913   /// different (the map is actually invertable) or there are only a few
  1914   /// items with the same value.
  1915   /// Otherwise consider to use \c IterableValueMap, which is more 
  1916   /// suitable and more efficient for such cases. It provides iterators
  1917   /// to traverse the items with the same associated value, however
  1918   /// it does not have \c InverseMap.
  1919   ///
  1920   /// This type is not reference map, so it cannot be modified with
  1921   /// the subscript operator.
  1922   ///
  1923   /// \tparam GR The graph type.
  1924   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  1925   /// \c GR::Edge).
  1926   /// \tparam V The value type of the map.
  1927   ///
  1928   /// \see IterableValueMap
  1929   template <typename GR, typename K, typename V>
  1930   class CrossRefMap
  1931     : protected ItemSetTraits<GR, K>::template Map<V>::Type {
  1932   private:
  1933 
  1934     typedef typename ItemSetTraits<GR, K>::
  1935       template Map<V>::Type Map;
  1936 
  1937     typedef std::multimap<V, K> Container;
  1938     Container _inv_map;
  1939 
  1940   public:
  1941 
  1942     /// The graph type of CrossRefMap.
  1943     typedef GR Graph;
  1944     typedef GR Digraph;
  1945     /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
  1946     typedef K Item;
  1947     /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
  1948     typedef K Key;
  1949     /// The value type of CrossRefMap.
  1950     typedef V Value;
  1951 
  1952     /// \brief Constructor.
  1953     ///
  1954     /// Construct a new CrossRefMap for the given graph.
  1955     explicit CrossRefMap(const Graph& graph) : Map(graph) {}
  1956 
  1957     /// \brief Forward iterator for values.
  1958     ///
  1959     /// This iterator is an STL compatible forward
  1960     /// iterator on the values of the map. The values can
  1961     /// be accessed in the <tt>[beginValue, endValue)</tt> range.
  1962     /// They are considered with multiplicity, so each value is
  1963     /// traversed for each item it is assigned to.
  1964     class ValueIt
  1965       : public std::iterator<std::forward_iterator_tag, Value> {
  1966       friend class CrossRefMap;
  1967     private:
  1968       ValueIt(typename Container::const_iterator _it)
  1969         : it(_it) {}
  1970     public:
  1971 
  1972       /// Constructor
  1973       ValueIt() {}
  1974 
  1975       /// \e
  1976       ValueIt& operator++() { ++it; return *this; }
  1977       /// \e
  1978       ValueIt operator++(int) {
  1979         ValueIt tmp(*this);
  1980         operator++();
  1981         return tmp;
  1982       }
  1983 
  1984       /// \e
  1985       const Value& operator*() const { return it->first; }
  1986       /// \e
  1987       const Value* operator->() const { return &(it->first); }
  1988 
  1989       /// \e
  1990       bool operator==(ValueIt jt) const { return it == jt.it; }
  1991       /// \e
  1992       bool operator!=(ValueIt jt) const { return it != jt.it; }
  1993 
  1994     private:
  1995       typename Container::const_iterator it;
  1996     };
  1997     
  1998     /// Alias for \c ValueIt
  1999     typedef ValueIt ValueIterator;
  2000 
  2001     /// \brief Returns an iterator to the first value.
  2002     ///
  2003     /// Returns an STL compatible iterator to the
  2004     /// first value of the map. The values of the
  2005     /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  2006     /// range.
  2007     ValueIt beginValue() const {
  2008       return ValueIt(_inv_map.begin());
  2009     }
  2010 
  2011     /// \brief Returns an iterator after the last value.
  2012     ///
  2013     /// Returns an STL compatible iterator after the
  2014     /// last value of the map. The values of the
  2015     /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  2016     /// range.
  2017     ValueIt endValue() const {
  2018       return ValueIt(_inv_map.end());
  2019     }
  2020 
  2021     /// \brief Sets the value associated with the given key.
  2022     ///
  2023     /// Sets the value associated with the given key.
  2024     void set(const Key& key, const Value& val) {
  2025       Value oldval = Map::operator[](key);
  2026       typename Container::iterator it;
  2027       for (it = _inv_map.equal_range(oldval).first;
  2028            it != _inv_map.equal_range(oldval).second; ++it) {
  2029         if (it->second == key) {
  2030           _inv_map.erase(it);
  2031           break;
  2032         }
  2033       }
  2034       _inv_map.insert(std::make_pair(val, key));
  2035       Map::set(key, val);
  2036     }
  2037 
  2038     /// \brief Returns the value associated with the given key.
  2039     ///
  2040     /// Returns the value associated with the given key.
  2041     typename MapTraits<Map>::ConstReturnValue
  2042     operator[](const Key& key) const {
  2043       return Map::operator[](key);
  2044     }
  2045 
  2046     /// \brief Gives back an item by its value.
  2047     ///
  2048     /// This function gives back an item that is assigned to
  2049     /// the given value or \c INVALID if no such item exists.
  2050     /// If there are more items with the same associated value,
  2051     /// only one of them is returned.
  2052     Key operator()(const Value& val) const {
  2053       typename Container::const_iterator it = _inv_map.find(val);
  2054       return it != _inv_map.end() ? it->second : INVALID;
  2055     }
  2056     
  2057     /// \brief Returns the number of items with the given value.
  2058     ///
  2059     /// This function returns the number of items with the given value
  2060     /// associated with it.
  2061     int count(const Value &val) const {
  2062       return _inv_map.count(val);
  2063     }
  2064 
  2065   protected:
  2066 
  2067     /// \brief Erase the key from the map and the inverse map.
  2068     ///
  2069     /// Erase the key from the map and the inverse map. It is called by the
  2070     /// \c AlterationNotifier.
  2071     virtual void erase(const Key& key) {
  2072       Value val = Map::operator[](key);
  2073       typename Container::iterator it;
  2074       for (it = _inv_map.equal_range(val).first;
  2075            it != _inv_map.equal_range(val).second; ++it) {
  2076         if (it->second == key) {
  2077           _inv_map.erase(it);
  2078           break;
  2079         }
  2080       }
  2081       Map::erase(key);
  2082     }
  2083 
  2084     /// \brief Erase more keys from the map and the inverse map.
  2085     ///
  2086     /// Erase more keys from the map and the inverse map. It is called by the
  2087     /// \c AlterationNotifier.
  2088     virtual void erase(const std::vector<Key>& keys) {
  2089       for (int i = 0; i < int(keys.size()); ++i) {
  2090         Value val = Map::operator[](keys[i]);
  2091         typename Container::iterator it;
  2092         for (it = _inv_map.equal_range(val).first;
  2093              it != _inv_map.equal_range(val).second; ++it) {
  2094           if (it->second == keys[i]) {
  2095             _inv_map.erase(it);
  2096             break;
  2097           }
  2098         }
  2099       }
  2100       Map::erase(keys);
  2101     }
  2102 
  2103     /// \brief Clear the keys from the map and the inverse map.
  2104     ///
  2105     /// Clear the keys from the map and the inverse map. It is called by the
  2106     /// \c AlterationNotifier.
  2107     virtual void clear() {
  2108       _inv_map.clear();
  2109       Map::clear();
  2110     }
  2111 
  2112   public:
  2113 
  2114     /// \brief The inverse map type of CrossRefMap.
  2115     ///
  2116     /// The inverse map type of CrossRefMap. The subscript operator gives
  2117     /// back an item by its value.
  2118     /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
  2119     /// \see inverse()
  2120     class InverseMap {
  2121     public:
  2122       /// \brief Constructor
  2123       ///
  2124       /// Constructor of the InverseMap.
  2125       explicit InverseMap(const CrossRefMap& inverted)
  2126         : _inverted(inverted) {}
  2127 
  2128       /// The value type of the InverseMap.
  2129       typedef typename CrossRefMap::Key Value;
  2130       /// The key type of the InverseMap.
  2131       typedef typename CrossRefMap::Value Key;
  2132 
  2133       /// \brief Subscript operator.
  2134       ///
  2135       /// Subscript operator. It gives back an item
  2136       /// that is assigned to the given value or \c INVALID
  2137       /// if no such item exists.
  2138       Value operator[](const Key& key) const {
  2139         return _inverted(key);
  2140       }
  2141 
  2142     private:
  2143       const CrossRefMap& _inverted;
  2144     };
  2145 
  2146     /// \brief Gives back the inverse of the map.
  2147     ///
  2148     /// Gives back the inverse of the CrossRefMap.
  2149     InverseMap inverse() const {
  2150       return InverseMap(*this);
  2151     }
  2152 
  2153   };
  2154 
  2155   /// \brief Provides continuous and unique id for the
  2156   /// items of a graph.
  2157   ///
  2158   /// RangeIdMap provides a unique and continuous
  2159   /// id for each item of a given type (\c Node, \c Arc or
  2160   /// \c Edge) in a graph. This id is
  2161   ///  - \b unique: different items get different ids,
  2162   ///  - \b continuous: the range of the ids is the set of integers
  2163   ///    between 0 and \c n-1, where \c n is the number of the items of
  2164   ///    this type (\c Node, \c Arc or \c Edge).
  2165   ///  - So, the ids can change when deleting an item of the same type.
  2166   ///
  2167   /// Thus this id is not (necessarily) the same as what can get using
  2168   /// the \c id() function of the graph or \ref IdMap.
  2169   /// This map can be inverted with its member class \c InverseMap,
  2170   /// or with the \c operator()() member.
  2171   ///
  2172   /// \tparam GR The graph type.
  2173   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  2174   /// \c GR::Edge).
  2175   ///
  2176   /// \see IdMap
  2177   template <typename GR, typename K>
  2178   class RangeIdMap
  2179     : protected ItemSetTraits<GR, K>::template Map<int>::Type {
  2180 
  2181     typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
  2182 
  2183   public:
  2184     /// The graph type of RangeIdMap.
  2185     typedef GR Graph;
  2186     typedef GR Digraph;
  2187     /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
  2188     typedef K Item;
  2189     /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
  2190     typedef K Key;
  2191     /// The value type of RangeIdMap.
  2192     typedef int Value;
  2193 
  2194     /// \brief Constructor.
  2195     ///
  2196     /// Constructor.
  2197     explicit RangeIdMap(const Graph& gr) : Map(gr) {
  2198       Item it;
  2199       const typename Map::Notifier* nf = Map::notifier();
  2200       for (nf->first(it); it != INVALID; nf->next(it)) {
  2201         Map::set(it, _inv_map.size());
  2202         _inv_map.push_back(it);
  2203       }
  2204     }
  2205 
  2206   protected:
  2207 
  2208     /// \brief Adds a new key to the map.
  2209     ///
  2210     /// Add a new key to the map. It is called by the
  2211     /// \c AlterationNotifier.
  2212     virtual void add(const Item& item) {
  2213       Map::add(item);
  2214       Map::set(item, _inv_map.size());
  2215       _inv_map.push_back(item);
  2216     }
  2217 
  2218     /// \brief Add more new keys to the map.
  2219     ///
  2220     /// Add more new keys to the map. It is called by the
  2221     /// \c AlterationNotifier.
  2222     virtual void add(const std::vector<Item>& items) {
  2223       Map::add(items);
  2224       for (int i = 0; i < int(items.size()); ++i) {
  2225         Map::set(items[i], _inv_map.size());
  2226         _inv_map.push_back(items[i]);
  2227       }
  2228     }
  2229 
  2230     /// \brief Erase the key from the map.
  2231     ///
  2232     /// Erase the key from the map. It is called by the
  2233     /// \c AlterationNotifier.
  2234     virtual void erase(const Item& item) {
  2235       Map::set(_inv_map.back(), Map::operator[](item));
  2236       _inv_map[Map::operator[](item)] = _inv_map.back();
  2237       _inv_map.pop_back();
  2238       Map::erase(item);
  2239     }
  2240 
  2241     /// \brief Erase more keys from the map.
  2242     ///
  2243     /// Erase more keys from the map. It is called by the
  2244     /// \c AlterationNotifier.
  2245     virtual void erase(const std::vector<Item>& items) {
  2246       for (int i = 0; i < int(items.size()); ++i) {
  2247         Map::set(_inv_map.back(), Map::operator[](items[i]));
  2248         _inv_map[Map::operator[](items[i])] = _inv_map.back();
  2249         _inv_map.pop_back();
  2250       }
  2251       Map::erase(items);
  2252     }
  2253 
  2254     /// \brief Build the unique map.
  2255     ///
  2256     /// Build the unique map. It is called by the
  2257     /// \c AlterationNotifier.
  2258     virtual void build() {
  2259       Map::build();
  2260       Item it;
  2261       const typename Map::Notifier* nf = Map::notifier();
  2262       for (nf->first(it); it != INVALID; nf->next(it)) {
  2263         Map::set(it, _inv_map.size());
  2264         _inv_map.push_back(it);
  2265       }
  2266     }
  2267 
  2268     /// \brief Clear the keys from the map.
  2269     ///
  2270     /// Clear the keys from the map. It is called by the
  2271     /// \c AlterationNotifier.
  2272     virtual void clear() {
  2273       _inv_map.clear();
  2274       Map::clear();
  2275     }
  2276 
  2277   public:
  2278 
  2279     /// \brief Returns the maximal value plus one.
  2280     ///
  2281     /// Returns the maximal value plus one in the map.
  2282     unsigned int size() const {
  2283       return _inv_map.size();
  2284     }
  2285 
  2286     /// \brief Swaps the position of the two items in the map.
  2287     ///
  2288     /// Swaps the position of the two items in the map.
  2289     void swap(const Item& p, const Item& q) {
  2290       int pi = Map::operator[](p);
  2291       int qi = Map::operator[](q);
  2292       Map::set(p, qi);
  2293       _inv_map[qi] = p;
  2294       Map::set(q, pi);
  2295       _inv_map[pi] = q;
  2296     }
  2297 
  2298     /// \brief Gives back the \e range \e id of the item
  2299     ///
  2300     /// Gives back the \e range \e id of the item.
  2301     int operator[](const Item& item) const {
  2302       return Map::operator[](item);
  2303     }
  2304 
  2305     /// \brief Gives back the item belonging to a \e range \e id
  2306     ///
  2307     /// Gives back the item belonging to the given \e range \e id.
  2308     Item operator()(int id) const {
  2309       return _inv_map[id];
  2310     }
  2311 
  2312   private:
  2313 
  2314     typedef std::vector<Item> Container;
  2315     Container _inv_map;
  2316 
  2317   public:
  2318 
  2319     /// \brief The inverse map type of RangeIdMap.
  2320     ///
  2321     /// The inverse map type of RangeIdMap. The subscript operator gives
  2322     /// back an item by its \e range \e id.
  2323     /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
  2324     class InverseMap {
  2325     public:
  2326       /// \brief Constructor
  2327       ///
  2328       /// Constructor of the InverseMap.
  2329       explicit InverseMap(const RangeIdMap& inverted)
  2330         : _inverted(inverted) {}
  2331 
  2332 
  2333       /// The value type of the InverseMap.
  2334       typedef typename RangeIdMap::Key Value;
  2335       /// The key type of the InverseMap.
  2336       typedef typename RangeIdMap::Value Key;
  2337 
  2338       /// \brief Subscript operator.
  2339       ///
  2340       /// Subscript operator. It gives back the item
  2341       /// that the given \e range \e id currently belongs to.
  2342       Value operator[](const Key& key) const {
  2343         return _inverted(key);
  2344       }
  2345 
  2346       /// \brief Size of the map.
  2347       ///
  2348       /// Returns the size of the map.
  2349       unsigned int size() const {
  2350         return _inverted.size();
  2351       }
  2352 
  2353     private:
  2354       const RangeIdMap& _inverted;
  2355     };
  2356 
  2357     /// \brief Gives back the inverse of the map.
  2358     ///
  2359     /// Gives back the inverse of the RangeIdMap.
  2360     const InverseMap inverse() const {
  2361       return InverseMap(*this);
  2362     }
  2363   };
  2364 
  2365   /// \brief Dynamic iterable \c bool map.
  2366   ///
  2367   /// This class provides a special graph map type which can store a
  2368   /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
  2369   /// For both \c true and \c false values it is possible to iterate on
  2370   /// the keys mapped to the value.
  2371   ///
  2372   /// This type is a reference map, so it can be modified with the
  2373   /// subscript operator.
  2374   ///
  2375   /// \tparam GR The graph type.
  2376   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  2377   /// \c GR::Edge).
  2378   ///
  2379   /// \see IterableIntMap, IterableValueMap
  2380   /// \see CrossRefMap
  2381   template <typename GR, typename K>
  2382   class IterableBoolMap
  2383     : protected ItemSetTraits<GR, K>::template Map<int>::Type {
  2384   private:
  2385     typedef GR Graph;
  2386 
  2387     typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
  2388     typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
  2389 
  2390     std::vector<K> _array;
  2391     int _sep;
  2392 
  2393   public:
  2394 
  2395     /// Indicates that the map is reference map.
  2396     typedef True ReferenceMapTag;
  2397 
  2398     /// The key type
  2399     typedef K Key;
  2400     /// The value type
  2401     typedef bool Value;
  2402     /// The const reference type.
  2403     typedef const Value& ConstReference;
  2404 
  2405   private:
  2406 
  2407     int position(const Key& key) const {
  2408       return Parent::operator[](key);
  2409     }
  2410 
  2411   public:
  2412 
  2413     /// \brief Reference to the value of the map.
  2414     ///
  2415     /// This class is similar to the \c bool type. It can be converted to
  2416     /// \c bool and it provides the same operators.
  2417     class Reference {
  2418       friend class IterableBoolMap;
  2419     private:
  2420       Reference(IterableBoolMap& map, const Key& key)
  2421         : _key(key), _map(map) {}
  2422     public:
  2423 
  2424       Reference& operator=(const Reference& value) {
  2425         _map.set(_key, static_cast<bool>(value));
  2426          return *this;
  2427       }
  2428 
  2429       operator bool() const {
  2430         return static_cast<const IterableBoolMap&>(_map)[_key];
  2431       }
  2432 
  2433       Reference& operator=(bool value) {
  2434         _map.set(_key, value);
  2435         return *this;
  2436       }
  2437       Reference& operator&=(bool value) {
  2438         _map.set(_key, _map[_key] & value);
  2439         return *this;
  2440       }
  2441       Reference& operator|=(bool value) {
  2442         _map.set(_key, _map[_key] | value);
  2443         return *this;
  2444       }
  2445       Reference& operator^=(bool value) {
  2446         _map.set(_key, _map[_key] ^ value);
  2447         return *this;
  2448       }
  2449     private:
  2450       Key _key;
  2451       IterableBoolMap& _map;
  2452     };
  2453 
  2454     /// \brief Constructor of the map with a default value.
  2455     ///
  2456     /// Constructor of the map with a default value.
  2457     explicit IterableBoolMap(const Graph& graph, bool def = false)
  2458       : Parent(graph) {
  2459       typename Parent::Notifier* nf = Parent::notifier();
  2460       Key it;
  2461       for (nf->first(it); it != INVALID; nf->next(it)) {
  2462         Parent::set(it, _array.size());
  2463         _array.push_back(it);
  2464       }
  2465       _sep = (def ? _array.size() : 0);
  2466     }
  2467 
  2468     /// \brief Const subscript operator of the map.
  2469     ///
  2470     /// Const subscript operator of the map.
  2471     bool operator[](const Key& key) const {
  2472       return position(key) < _sep;
  2473     }
  2474 
  2475     /// \brief Subscript operator of the map.
  2476     ///
  2477     /// Subscript operator of the map.
  2478     Reference operator[](const Key& key) {
  2479       return Reference(*this, key);
  2480     }
  2481 
  2482     /// \brief Set operation of the map.
  2483     ///
  2484     /// Set operation of the map.
  2485     void set(const Key& key, bool value) {
  2486       int pos = position(key);
  2487       if (value) {
  2488         if (pos < _sep) return;
  2489         Key tmp = _array[_sep];
  2490         _array[_sep] = key;
  2491         Parent::set(key, _sep);
  2492         _array[pos] = tmp;
  2493         Parent::set(tmp, pos);
  2494         ++_sep;
  2495       } else {
  2496         if (pos >= _sep) return;
  2497         --_sep;
  2498         Key tmp = _array[_sep];
  2499         _array[_sep] = key;
  2500         Parent::set(key, _sep);
  2501         _array[pos] = tmp;
  2502         Parent::set(tmp, pos);
  2503       }
  2504     }
  2505 
  2506     /// \brief Set all items.
  2507     ///
  2508     /// Set all items in the map.
  2509     /// \note Constant time operation.
  2510     void setAll(bool value) {
  2511       _sep = (value ? _array.size() : 0);
  2512     }
  2513 
  2514     /// \brief Returns the number of the keys mapped to \c true.
  2515     ///
  2516     /// Returns the number of the keys mapped to \c true.
  2517     int trueNum() const {
  2518       return _sep;
  2519     }
  2520 
  2521     /// \brief Returns the number of the keys mapped to \c false.
  2522     ///
  2523     /// Returns the number of the keys mapped to \c false.
  2524     int falseNum() const {
  2525       return _array.size() - _sep;
  2526     }
  2527 
  2528     /// \brief Iterator for the keys mapped to \c true.
  2529     ///
  2530     /// Iterator for the keys mapped to \c true. It works
  2531     /// like a graph item iterator, it can be converted to
  2532     /// the key type of the map, incremented with \c ++ operator, and
  2533     /// if the iterator leaves the last valid key, it will be equal to
  2534     /// \c INVALID.
  2535     class TrueIt : public Key {
  2536     public:
  2537       typedef Key Parent;
  2538 
  2539       /// \brief Creates an iterator.
  2540       ///
  2541       /// Creates an iterator. It iterates on the
  2542       /// keys mapped to \c true.
  2543       /// \param map The IterableBoolMap.
  2544       explicit TrueIt(const IterableBoolMap& map)
  2545         : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
  2546           _map(&map) {}
  2547 
  2548       /// \brief Invalid constructor \& conversion.
  2549       ///
  2550       /// This constructor initializes the iterator to be invalid.
  2551       /// \sa Invalid for more details.
  2552       TrueIt(Invalid) : Parent(INVALID), _map(0) {}
  2553 
  2554       /// \brief Increment operator.
  2555       ///
  2556       /// Increment operator.
  2557       TrueIt& operator++() {
  2558         int pos = _map->position(*this);
  2559         Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
  2560         return *this;
  2561       }
  2562 
  2563     private:
  2564       const IterableBoolMap* _map;
  2565     };
  2566 
  2567     /// \brief Iterator for the keys mapped to \c false.
  2568     ///
  2569     /// Iterator for the keys mapped to \c false. It works
  2570     /// like a graph item iterator, it can be converted to
  2571     /// the key type of the map, incremented with \c ++ operator, and
  2572     /// if the iterator leaves the last valid key, it will be equal to
  2573     /// \c INVALID.
  2574     class FalseIt : public Key {
  2575     public:
  2576       typedef Key Parent;
  2577 
  2578       /// \brief Creates an iterator.
  2579       ///
  2580       /// Creates an iterator. It iterates on the
  2581       /// keys mapped to \c false.
  2582       /// \param map The IterableBoolMap.
  2583       explicit FalseIt(const IterableBoolMap& map)
  2584         : Parent(map._sep < int(map._array.size()) ?
  2585                  map._array.back() : INVALID), _map(&map) {}
  2586 
  2587       /// \brief Invalid constructor \& conversion.
  2588       ///
  2589       /// This constructor initializes the iterator to be invalid.
  2590       /// \sa Invalid for more details.
  2591       FalseIt(Invalid) : Parent(INVALID), _map(0) {}
  2592 
  2593       /// \brief Increment operator.
  2594       ///
  2595       /// Increment operator.
  2596       FalseIt& operator++() {
  2597         int pos = _map->position(*this);
  2598         Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
  2599         return *this;
  2600       }
  2601 
  2602     private:
  2603       const IterableBoolMap* _map;
  2604     };
  2605 
  2606     /// \brief Iterator for the keys mapped to a given value.
  2607     ///
  2608     /// Iterator for the keys mapped to a given value. It works
  2609     /// like a graph item iterator, it can be converted to
  2610     /// the key type of the map, incremented with \c ++ operator, and
  2611     /// if the iterator leaves the last valid key, it will be equal to
  2612     /// \c INVALID.
  2613     class ItemIt : public Key {
  2614     public:
  2615       typedef Key Parent;
  2616 
  2617       /// \brief Creates an iterator with a value.
  2618       ///
  2619       /// Creates an iterator with a value. It iterates on the
  2620       /// keys mapped to the given value.
  2621       /// \param map The IterableBoolMap.
  2622       /// \param value The value.
  2623       ItemIt(const IterableBoolMap& map, bool value)
  2624         : Parent(value ? 
  2625                  (map._sep > 0 ?
  2626                   map._array[map._sep - 1] : INVALID) :
  2627                  (map._sep < int(map._array.size()) ?
  2628                   map._array.back() : INVALID)), _map(&map) {}
  2629 
  2630       /// \brief Invalid constructor \& conversion.
  2631       ///
  2632       /// This constructor initializes the iterator to be invalid.
  2633       /// \sa Invalid for more details.
  2634       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  2635 
  2636       /// \brief Increment operator.
  2637       ///
  2638       /// Increment operator.
  2639       ItemIt& operator++() {
  2640         int pos = _map->position(*this);
  2641         int _sep = pos >= _map->_sep ? _map->_sep : 0;
  2642         Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
  2643         return *this;
  2644       }
  2645 
  2646     private:
  2647       const IterableBoolMap* _map;
  2648     };
  2649 
  2650   protected:
  2651 
  2652     virtual void add(const Key& key) {
  2653       Parent::add(key);
  2654       Parent::set(key, _array.size());
  2655       _array.push_back(key);
  2656     }
  2657 
  2658     virtual void add(const std::vector<Key>& keys) {
  2659       Parent::add(keys);
  2660       for (int i = 0; i < int(keys.size()); ++i) {
  2661         Parent::set(keys[i], _array.size());
  2662         _array.push_back(keys[i]);
  2663       }
  2664     }
  2665 
  2666     virtual void erase(const Key& key) {
  2667       int pos = position(key);
  2668       if (pos < _sep) {
  2669         --_sep;
  2670         Parent::set(_array[_sep], pos);
  2671         _array[pos] = _array[_sep];
  2672         Parent::set(_array.back(), _sep);
  2673         _array[_sep] = _array.back();
  2674         _array.pop_back();
  2675       } else {
  2676         Parent::set(_array.back(), pos);
  2677         _array[pos] = _array.back();
  2678         _array.pop_back();
  2679       }
  2680       Parent::erase(key);
  2681     }
  2682 
  2683     virtual void erase(const std::vector<Key>& keys) {
  2684       for (int i = 0; i < int(keys.size()); ++i) {
  2685         int pos = position(keys[i]);
  2686         if (pos < _sep) {
  2687           --_sep;
  2688           Parent::set(_array[_sep], pos);
  2689           _array[pos] = _array[_sep];
  2690           Parent::set(_array.back(), _sep);
  2691           _array[_sep] = _array.back();
  2692           _array.pop_back();
  2693         } else {
  2694           Parent::set(_array.back(), pos);
  2695           _array[pos] = _array.back();
  2696           _array.pop_back();
  2697         }
  2698       }
  2699       Parent::erase(keys);
  2700     }
  2701 
  2702     virtual void build() {
  2703       Parent::build();
  2704       typename Parent::Notifier* nf = Parent::notifier();
  2705       Key it;
  2706       for (nf->first(it); it != INVALID; nf->next(it)) {
  2707         Parent::set(it, _array.size());
  2708         _array.push_back(it);
  2709       }
  2710       _sep = 0;
  2711     }
  2712 
  2713     virtual void clear() {
  2714       _array.clear();
  2715       _sep = 0;
  2716       Parent::clear();
  2717     }
  2718 
  2719   };
  2720 
  2721 
  2722   namespace _maps_bits {
  2723     template <typename Item>
  2724     struct IterableIntMapNode {
  2725       IterableIntMapNode() : value(-1) {}
  2726       IterableIntMapNode(int _value) : value(_value) {}
  2727       Item prev, next;
  2728       int value;
  2729     };
  2730   }
  2731 
  2732   /// \brief Dynamic iterable integer map.
  2733   ///
  2734   /// This class provides a special graph map type which can store an
  2735   /// integer value for graph items (\c Node, \c Arc or \c Edge).
  2736   /// For each non-negative value it is possible to iterate on the keys
  2737   /// mapped to the value.
  2738   ///
  2739   /// This map is intended to be used with small integer values, for which
  2740   /// it is efficient, and supports iteration only for non-negative values.
  2741   /// If you need large values and/or iteration for negative integers,
  2742   /// consider to use \ref IterableValueMap instead.
  2743   ///
  2744   /// This type is a reference map, so it can be modified with the
  2745   /// subscript operator.
  2746   ///
  2747   /// \note The size of the data structure depends on the largest
  2748   /// value in the map.
  2749   ///
  2750   /// \tparam GR The graph type.
  2751   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  2752   /// \c GR::Edge).
  2753   ///
  2754   /// \see IterableBoolMap, IterableValueMap
  2755   /// \see CrossRefMap
  2756   template <typename GR, typename K>
  2757   class IterableIntMap
  2758     : protected ItemSetTraits<GR, K>::
  2759         template Map<_maps_bits::IterableIntMapNode<K> >::Type {
  2760   public:
  2761     typedef typename ItemSetTraits<GR, K>::
  2762       template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
  2763 
  2764     /// The key type
  2765     typedef K Key;
  2766     /// The value type
  2767     typedef int Value;
  2768     /// The graph type
  2769     typedef GR Graph;
  2770 
  2771     /// \brief Constructor of the map.
  2772     ///
  2773     /// Constructor of the map. It sets all values to -1.
  2774     explicit IterableIntMap(const Graph& graph)
  2775       : Parent(graph) {}
  2776 
  2777     /// \brief Constructor of the map with a given value.
  2778     ///
  2779     /// Constructor of the map with a given value.
  2780     explicit IterableIntMap(const Graph& graph, int value)
  2781       : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
  2782       if (value >= 0) {
  2783         for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  2784           lace(it);
  2785         }
  2786       }
  2787     }
  2788 
  2789   private:
  2790 
  2791     void unlace(const Key& key) {
  2792       typename Parent::Value& node = Parent::operator[](key);
  2793       if (node.value < 0) return;
  2794       if (node.prev != INVALID) {
  2795         Parent::operator[](node.prev).next = node.next;
  2796       } else {
  2797         _first[node.value] = node.next;
  2798       }
  2799       if (node.next != INVALID) {
  2800         Parent::operator[](node.next).prev = node.prev;
  2801       }
  2802       while (!_first.empty() && _first.back() == INVALID) {
  2803         _first.pop_back();
  2804       }
  2805     }
  2806 
  2807     void lace(const Key& key) {
  2808       typename Parent::Value& node = Parent::operator[](key);
  2809       if (node.value < 0) return;
  2810       if (node.value >= int(_first.size())) {
  2811         _first.resize(node.value + 1, INVALID);
  2812       }
  2813       node.prev = INVALID;
  2814       node.next = _first[node.value];
  2815       if (node.next != INVALID) {
  2816         Parent::operator[](node.next).prev = key;
  2817       }
  2818       _first[node.value] = key;
  2819     }
  2820 
  2821   public:
  2822 
  2823     /// Indicates that the map is reference map.
  2824     typedef True ReferenceMapTag;
  2825 
  2826     /// \brief Reference to the value of the map.
  2827     ///
  2828     /// This class is similar to the \c int type. It can
  2829     /// be converted to \c int and it has the same operators.
  2830     class Reference {
  2831       friend class IterableIntMap;
  2832     private:
  2833       Reference(IterableIntMap& map, const Key& key)
  2834         : _key(key), _map(map) {}
  2835     public:
  2836 
  2837       Reference& operator=(const Reference& value) {
  2838         _map.set(_key, static_cast<const int&>(value));
  2839          return *this;
  2840       }
  2841 
  2842       operator const int&() const {
  2843         return static_cast<const IterableIntMap&>(_map)[_key];
  2844       }
  2845 
  2846       Reference& operator=(int value) {
  2847         _map.set(_key, value);
  2848         return *this;
  2849       }
  2850       Reference& operator++() {
  2851         _map.set(_key, _map[_key] + 1);
  2852         return *this;
  2853       }
  2854       int operator++(int) {
  2855         int value = _map[_key];
  2856         _map.set(_key, value + 1);
  2857         return value;
  2858       }
  2859       Reference& operator--() {
  2860         _map.set(_key, _map[_key] - 1);
  2861         return *this;
  2862       }
  2863       int operator--(int) {
  2864         int value = _map[_key];
  2865         _map.set(_key, value - 1);
  2866         return value;
  2867       }
  2868       Reference& operator+=(int value) {
  2869         _map.set(_key, _map[_key] + value);
  2870         return *this;
  2871       }
  2872       Reference& operator-=(int value) {
  2873         _map.set(_key, _map[_key] - value);
  2874         return *this;
  2875       }
  2876       Reference& operator*=(int value) {
  2877         _map.set(_key, _map[_key] * value);
  2878         return *this;
  2879       }
  2880       Reference& operator/=(int value) {
  2881         _map.set(_key, _map[_key] / value);
  2882         return *this;
  2883       }
  2884       Reference& operator%=(int value) {
  2885         _map.set(_key, _map[_key] % value);
  2886         return *this;
  2887       }
  2888       Reference& operator&=(int value) {
  2889         _map.set(_key, _map[_key] & value);
  2890         return *this;
  2891       }
  2892       Reference& operator|=(int value) {
  2893         _map.set(_key, _map[_key] | value);
  2894         return *this;
  2895       }
  2896       Reference& operator^=(int value) {
  2897         _map.set(_key, _map[_key] ^ value);
  2898         return *this;
  2899       }
  2900       Reference& operator<<=(int value) {
  2901         _map.set(_key, _map[_key] << value);
  2902         return *this;
  2903       }
  2904       Reference& operator>>=(int value) {
  2905         _map.set(_key, _map[_key] >> value);
  2906         return *this;
  2907       }
  2908 
  2909     private:
  2910       Key _key;
  2911       IterableIntMap& _map;
  2912     };
  2913 
  2914     /// The const reference type.
  2915     typedef const Value& ConstReference;
  2916 
  2917     /// \brief Gives back the maximal value plus one.
  2918     ///
  2919     /// Gives back the maximal value plus one.
  2920     int size() const {
  2921       return _first.size();
  2922     }
  2923 
  2924     /// \brief Set operation of the map.
  2925     ///
  2926     /// Set operation of the map.
  2927     void set(const Key& key, const Value& value) {
  2928       unlace(key);
  2929       Parent::operator[](key).value = value;
  2930       lace(key);
  2931     }
  2932 
  2933     /// \brief Const subscript operator of the map.
  2934     ///
  2935     /// Const subscript operator of the map.
  2936     const Value& operator[](const Key& key) const {
  2937       return Parent::operator[](key).value;
  2938     }
  2939 
  2940     /// \brief Subscript operator of the map.
  2941     ///
  2942     /// Subscript operator of the map.
  2943     Reference operator[](const Key& key) {
  2944       return Reference(*this, key);
  2945     }
  2946 
  2947     /// \brief Iterator for the keys with the same value.
  2948     ///
  2949     /// Iterator for the keys with the same value. It works
  2950     /// like a graph item iterator, it can be converted to
  2951     /// the item type of the map, incremented with \c ++ operator, and
  2952     /// if the iterator leaves the last valid item, it will be equal to
  2953     /// \c INVALID.
  2954     class ItemIt : public Key {
  2955     public:
  2956       typedef Key Parent;
  2957 
  2958       /// \brief Invalid constructor \& conversion.
  2959       ///
  2960       /// This constructor initializes the iterator to be invalid.
  2961       /// \sa Invalid for more details.
  2962       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  2963 
  2964       /// \brief Creates an iterator with a value.
  2965       ///
  2966       /// Creates an iterator with a value. It iterates on the
  2967       /// keys mapped to the given value.
  2968       /// \param map The IterableIntMap.
  2969       /// \param value The value.
  2970       ItemIt(const IterableIntMap& map, int value) : _map(&map) {
  2971         if (value < 0 || value >= int(_map->_first.size())) {
  2972           Parent::operator=(INVALID);
  2973         } else {
  2974           Parent::operator=(_map->_first[value]);
  2975         }
  2976       }
  2977 
  2978       /// \brief Increment operator.
  2979       ///
  2980       /// Increment operator.
  2981       ItemIt& operator++() {
  2982         Parent::operator=(_map->IterableIntMap::Parent::
  2983                           operator[](static_cast<Parent&>(*this)).next);
  2984         return *this;
  2985       }
  2986 
  2987     private:
  2988       const IterableIntMap* _map;
  2989     };
  2990 
  2991   protected:
  2992 
  2993     virtual void erase(const Key& key) {
  2994       unlace(key);
  2995       Parent::erase(key);
  2996     }
  2997 
  2998     virtual void erase(const std::vector<Key>& keys) {
  2999       for (int i = 0; i < int(keys.size()); ++i) {
  3000         unlace(keys[i]);
  3001       }
  3002       Parent::erase(keys);
  3003     }
  3004 
  3005     virtual void clear() {
  3006       _first.clear();
  3007       Parent::clear();
  3008     }
  3009 
  3010   private:
  3011     std::vector<Key> _first;
  3012   };
  3013 
  3014   namespace _maps_bits {
  3015     template <typename Item, typename Value>
  3016     struct IterableValueMapNode {
  3017       IterableValueMapNode(Value _value = Value()) : value(_value) {}
  3018       Item prev, next;
  3019       Value value;
  3020     };
  3021   }
  3022 
  3023   /// \brief Dynamic iterable map for comparable values.
  3024   ///
  3025   /// This class provides a special graph map type which can store a
  3026   /// comparable value for graph items (\c Node, \c Arc or \c Edge).
  3027   /// For each value it is possible to iterate on the keys mapped to
  3028   /// the value (\c ItemIt), and the values of the map can be accessed
  3029   /// with an STL compatible forward iterator (\c ValueIt).
  3030   /// The map stores a linked list for each value, which contains
  3031   /// the items mapped to the value, and the used values are stored
  3032   /// in balanced binary tree (\c std::map).
  3033   ///
  3034   /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
  3035   /// specialized for \c bool and \c int values, respectively.
  3036   ///
  3037   /// This type is not reference map, so it cannot be modified with
  3038   /// the subscript operator.
  3039   ///
  3040   /// \tparam GR The graph type.
  3041   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  3042   /// \c GR::Edge).
  3043   /// \tparam V The value type of the map. It can be any comparable
  3044   /// value type.
  3045   ///
  3046   /// \see IterableBoolMap, IterableIntMap
  3047   /// \see CrossRefMap
  3048   template <typename GR, typename K, typename V>
  3049   class IterableValueMap
  3050     : protected ItemSetTraits<GR, K>::
  3051         template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
  3052   public:
  3053     typedef typename ItemSetTraits<GR, K>::
  3054       template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
  3055 
  3056     /// The key type
  3057     typedef K Key;
  3058     /// The value type
  3059     typedef V Value;
  3060     /// The graph type
  3061     typedef GR Graph;
  3062 
  3063   public:
  3064 
  3065     /// \brief Constructor of the map with a given value.
  3066     ///
  3067     /// Constructor of the map with a given value.
  3068     explicit IterableValueMap(const Graph& graph,
  3069                               const Value& value = Value())
  3070       : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
  3071       for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  3072         lace(it);
  3073       }
  3074     }
  3075 
  3076   protected:
  3077 
  3078     void unlace(const Key& key) {
  3079       typename Parent::Value& node = Parent::operator[](key);
  3080       if (node.prev != INVALID) {
  3081         Parent::operator[](node.prev).next = node.next;
  3082       } else {
  3083         if (node.next != INVALID) {
  3084           _first[node.value] = node.next;
  3085         } else {
  3086           _first.erase(node.value);
  3087         }
  3088       }
  3089       if (node.next != INVALID) {
  3090         Parent::operator[](node.next).prev = node.prev;
  3091       }
  3092     }
  3093 
  3094     void lace(const Key& key) {
  3095       typename Parent::Value& node = Parent::operator[](key);
  3096       typename std::map<Value, Key>::iterator it = _first.find(node.value);
  3097       if (it == _first.end()) {
  3098         node.prev = node.next = INVALID;
  3099         _first.insert(std::make_pair(node.value, key));
  3100       } else {
  3101         node.prev = INVALID;
  3102         node.next = it->second;
  3103         if (node.next != INVALID) {
  3104           Parent::operator[](node.next).prev = key;
  3105         }
  3106         it->second = key;
  3107       }
  3108     }
  3109 
  3110   public:
  3111 
  3112     /// \brief Forward iterator for values.
  3113     ///
  3114     /// This iterator is an STL compatible forward
  3115     /// iterator on the values of the map. The values can
  3116     /// be accessed in the <tt>[beginValue, endValue)</tt> range.
  3117     class ValueIt
  3118       : public std::iterator<std::forward_iterator_tag, Value> {
  3119       friend class IterableValueMap;
  3120     private:
  3121       ValueIt(typename std::map<Value, Key>::const_iterator _it)
  3122         : it(_it) {}
  3123     public:
  3124 
  3125       /// Constructor
  3126       ValueIt() {}
  3127 
  3128       /// \e
  3129       ValueIt& operator++() { ++it; return *this; }
  3130       /// \e
  3131       ValueIt operator++(int) {
  3132         ValueIt tmp(*this);
  3133         operator++();
  3134         return tmp;
  3135       }
  3136 
  3137       /// \e
  3138       const Value& operator*() const { return it->first; }
  3139       /// \e
  3140       const Value* operator->() const { return &(it->first); }
  3141 
  3142       /// \e
  3143       bool operator==(ValueIt jt) const { return it == jt.it; }
  3144       /// \e
  3145       bool operator!=(ValueIt jt) const { return it != jt.it; }
  3146 
  3147     private:
  3148       typename std::map<Value, Key>::const_iterator it;
  3149     };
  3150 
  3151     /// \brief Returns an iterator to the first value.
  3152     ///
  3153     /// Returns an STL compatible iterator to the
  3154     /// first value of the map. The values of the
  3155     /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  3156     /// range.
  3157     ValueIt beginValue() const {
  3158       return ValueIt(_first.begin());
  3159     }
  3160 
  3161     /// \brief Returns an iterator after the last value.
  3162     ///
  3163     /// Returns an STL compatible iterator after the
  3164     /// last value of the map. The values of the
  3165     /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  3166     /// range.
  3167     ValueIt endValue() const {
  3168       return ValueIt(_first.end());
  3169     }
  3170 
  3171     /// \brief Set operation of the map.
  3172     ///
  3173     /// Set operation of the map.
  3174     void set(const Key& key, const Value& value) {
  3175       unlace(key);
  3176       Parent::operator[](key).value = value;
  3177       lace(key);
  3178     }
  3179 
  3180     /// \brief Const subscript operator of the map.
  3181     ///
  3182     /// Const subscript operator of the map.
  3183     const Value& operator[](const Key& key) const {
  3184       return Parent::operator[](key).value;
  3185     }
  3186 
  3187     /// \brief Iterator for the keys with the same value.
  3188     ///
  3189     /// Iterator for the keys with the same value. It works
  3190     /// like a graph item iterator, it can be converted to
  3191     /// the item type of the map, incremented with \c ++ operator, and
  3192     /// if the iterator leaves the last valid item, it will be equal to
  3193     /// \c INVALID.
  3194     class ItemIt : public Key {
  3195     public:
  3196       typedef Key Parent;
  3197 
  3198       /// \brief Invalid constructor \& conversion.
  3199       ///
  3200       /// This constructor initializes the iterator to be invalid.
  3201       /// \sa Invalid for more details.
  3202       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  3203 
  3204       /// \brief Creates an iterator with a value.
  3205       ///
  3206       /// Creates an iterator with a value. It iterates on the
  3207       /// keys which have the given value.
  3208       /// \param map The IterableValueMap
  3209       /// \param value The value
  3210       ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
  3211         typename std::map<Value, Key>::const_iterator it =
  3212           map._first.find(value);
  3213         if (it == map._first.end()) {
  3214           Parent::operator=(INVALID);
  3215         } else {
  3216           Parent::operator=(it->second);
  3217         }
  3218       }
  3219 
  3220       /// \brief Increment operator.
  3221       ///
  3222       /// Increment Operator.
  3223       ItemIt& operator++() {
  3224         Parent::operator=(_map->IterableValueMap::Parent::
  3225                           operator[](static_cast<Parent&>(*this)).next);
  3226         return *this;
  3227       }
  3228 
  3229 
  3230     private:
  3231       const IterableValueMap* _map;
  3232     };
  3233 
  3234   protected:
  3235 
  3236     virtual void add(const Key& key) {
  3237       Parent::add(key);
  3238       unlace(key);
  3239     }
  3240 
  3241     virtual void add(const std::vector<Key>& keys) {
  3242       Parent::add(keys);
  3243       for (int i = 0; i < int(keys.size()); ++i) {
  3244         lace(keys[i]);
  3245       }
  3246     }
  3247 
  3248     virtual void erase(const Key& key) {
  3249       unlace(key);
  3250       Parent::erase(key);
  3251     }
  3252 
  3253     virtual void erase(const std::vector<Key>& keys) {
  3254       for (int i = 0; i < int(keys.size()); ++i) {
  3255         unlace(keys[i]);
  3256       }
  3257       Parent::erase(keys);
  3258     }
  3259 
  3260     virtual void build() {
  3261       Parent::build();
  3262       for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  3263         lace(it);
  3264       }
  3265     }
  3266 
  3267     virtual void clear() {
  3268       _first.clear();
  3269       Parent::clear();
  3270     }
  3271 
  3272   private:
  3273     std::map<Value, Key> _first;
  3274   };
  3275 
  3276   /// \brief Map of the source nodes of arcs in a digraph.
  3277   ///
  3278   /// SourceMap provides access for the source node of each arc in a digraph,
  3279   /// which is returned by the \c source() function of the digraph.
  3280   /// \tparam GR The digraph type.
  3281   /// \see TargetMap
  3282   template <typename GR>
  3283   class SourceMap {
  3284   public:
  3285 
  3286     /// The key type (the \c Arc type of the digraph).
  3287     typedef typename GR::Arc Key;
  3288     /// The value type (the \c Node type of the digraph).
  3289     typedef typename GR::Node Value;
  3290 
  3291     /// \brief Constructor
  3292     ///
  3293     /// Constructor.
  3294     /// \param digraph The digraph that the map belongs to.
  3295     explicit SourceMap(const GR& digraph) : _graph(digraph) {}
  3296 
  3297     /// \brief Returns the source node of the given arc.
  3298     ///
  3299     /// Returns the source node of the given arc.
  3300     Value operator[](const Key& arc) const {
  3301       return _graph.source(arc);
  3302     }
  3303 
  3304   private:
  3305     const GR& _graph;
  3306   };
  3307 
  3308   /// \brief Returns a \c SourceMap class.
  3309   ///
  3310   /// This function just returns an \c SourceMap class.
  3311   /// \relates SourceMap
  3312   template <typename GR>
  3313   inline SourceMap<GR> sourceMap(const GR& graph) {
  3314     return SourceMap<GR>(graph);
  3315   }
  3316 
  3317   /// \brief Map of the target nodes of arcs in a digraph.
  3318   ///
  3319   /// TargetMap provides access for the target node of each arc in a digraph,
  3320   /// which is returned by the \c target() function of the digraph.
  3321   /// \tparam GR The digraph type.
  3322   /// \see SourceMap
  3323   template <typename GR>
  3324   class TargetMap {
  3325   public:
  3326 
  3327     /// The key type (the \c Arc type of the digraph).
  3328     typedef typename GR::Arc Key;
  3329     /// The value type (the \c Node type of the digraph).
  3330     typedef typename GR::Node Value;
  3331 
  3332     /// \brief Constructor
  3333     ///
  3334     /// Constructor.
  3335     /// \param digraph The digraph that the map belongs to.
  3336     explicit TargetMap(const GR& digraph) : _graph(digraph) {}
  3337 
  3338     /// \brief Returns the target node of the given arc.
  3339     ///
  3340     /// Returns the target node of the given arc.
  3341     Value operator[](const Key& e) const {
  3342       return _graph.target(e);
  3343     }
  3344 
  3345   private:
  3346     const GR& _graph;
  3347   };
  3348 
  3349   /// \brief Returns a \c TargetMap class.
  3350   ///
  3351   /// This function just returns a \c TargetMap class.
  3352   /// \relates TargetMap
  3353   template <typename GR>
  3354   inline TargetMap<GR> targetMap(const GR& graph) {
  3355     return TargetMap<GR>(graph);
  3356   }
  3357 
  3358   /// \brief Map of the "forward" directed arc view of edges in a graph.
  3359   ///
  3360   /// ForwardMap provides access for the "forward" directed arc view of
  3361   /// each edge in a graph, which is returned by the \c direct() function
  3362   /// of the graph with \c true parameter.
  3363   /// \tparam GR The graph type.
  3364   /// \see BackwardMap
  3365   template <typename GR>
  3366   class ForwardMap {
  3367   public:
  3368 
  3369     /// The key type (the \c Edge type of the digraph).
  3370     typedef typename GR::Edge Key;
  3371     /// The value type (the \c Arc type of the digraph).
  3372     typedef typename GR::Arc Value;
  3373 
  3374     /// \brief Constructor
  3375     ///
  3376     /// Constructor.
  3377     /// \param graph The graph that the map belongs to.
  3378     explicit ForwardMap(const GR& graph) : _graph(graph) {}
  3379 
  3380     /// \brief Returns the "forward" directed arc view of the given edge.
  3381     ///
  3382     /// Returns the "forward" directed arc view of the given edge.
  3383     Value operator[](const Key& key) const {
  3384       return _graph.direct(key, true);
  3385     }
  3386 
  3387   private:
  3388     const GR& _graph;
  3389   };
  3390 
  3391   /// \brief Returns a \c ForwardMap class.
  3392   ///
  3393   /// This function just returns an \c ForwardMap class.
  3394   /// \relates ForwardMap
  3395   template <typename GR>
  3396   inline ForwardMap<GR> forwardMap(const GR& graph) {
  3397     return ForwardMap<GR>(graph);
  3398   }
  3399 
  3400   /// \brief Map of the "backward" directed arc view of edges in a graph.
  3401   ///
  3402   /// BackwardMap provides access for the "backward" directed arc view of
  3403   /// each edge in a graph, which is returned by the \c direct() function
  3404   /// of the graph with \c false parameter.
  3405   /// \tparam GR The graph type.
  3406   /// \see ForwardMap
  3407   template <typename GR>
  3408   class BackwardMap {
  3409   public:
  3410 
  3411     /// The key type (the \c Edge type of the digraph).
  3412     typedef typename GR::Edge Key;
  3413     /// The value type (the \c Arc type of the digraph).
  3414     typedef typename GR::Arc Value;
  3415 
  3416     /// \brief Constructor
  3417     ///
  3418     /// Constructor.
  3419     /// \param graph The graph that the map belongs to.
  3420     explicit BackwardMap(const GR& graph) : _graph(graph) {}
  3421 
  3422     /// \brief Returns the "backward" directed arc view of the given edge.
  3423     ///
  3424     /// Returns the "backward" directed arc view of the given edge.
  3425     Value operator[](const Key& key) const {
  3426       return _graph.direct(key, false);
  3427     }
  3428 
  3429   private:
  3430     const GR& _graph;
  3431   };
  3432 
  3433   /// \brief Returns a \c BackwardMap class
  3434 
  3435   /// This function just returns a \c BackwardMap class.
  3436   /// \relates BackwardMap
  3437   template <typename GR>
  3438   inline BackwardMap<GR> backwardMap(const GR& graph) {
  3439     return BackwardMap<GR>(graph);
  3440   }
  3441 
  3442   /// \brief Map of the in-degrees of nodes in a digraph.
  3443   ///
  3444   /// This map returns the in-degree of a node. Once it is constructed,
  3445   /// the degrees are stored in a standard \c NodeMap, so each query is done
  3446   /// in constant time. On the other hand, the values are updated automatically
  3447   /// whenever the digraph changes.
  3448   ///
  3449   /// \warning Besides \c addNode() and \c addArc(), a digraph structure
  3450   /// may provide alternative ways to modify the digraph.
  3451   /// The correct behavior of InDegMap is not guarantied if these additional
  3452   /// features are used. For example the functions
  3453   /// \ref ListDigraph::changeSource() "changeSource()",
  3454   /// \ref ListDigraph::changeTarget() "changeTarget()" and
  3455   /// \ref ListDigraph::reverseArc() "reverseArc()"
  3456   /// of \ref ListDigraph will \e not update the degree values correctly.
  3457   ///
  3458   /// \sa OutDegMap
  3459   template <typename GR>
  3460   class InDegMap
  3461     : protected ItemSetTraits<GR, typename GR::Arc>
  3462       ::ItemNotifier::ObserverBase {
  3463 
  3464   public:
  3465 
  3466     /// The graph type of InDegMap
  3467     typedef GR Graph;
  3468     typedef GR Digraph;
  3469     /// The key type
  3470     typedef typename Digraph::Node Key;
  3471     /// The value type
  3472     typedef int Value;
  3473 
  3474     typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
  3475     ::ItemNotifier::ObserverBase Parent;
  3476 
  3477   private:
  3478 
  3479     class AutoNodeMap
  3480       : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
  3481     public:
  3482 
  3483       typedef typename ItemSetTraits<Digraph, Key>::
  3484       template Map<int>::Type Parent;
  3485 
  3486       AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
  3487 
  3488       virtual void add(const Key& key) {
  3489         Parent::add(key);
  3490         Parent::set(key, 0);
  3491       }
  3492 
  3493       virtual void add(const std::vector<Key>& keys) {
  3494         Parent::add(keys);
  3495         for (int i = 0; i < int(keys.size()); ++i) {
  3496           Parent::set(keys[i], 0);
  3497         }
  3498       }
  3499 
  3500       virtual void build() {
  3501         Parent::build();
  3502         Key it;
  3503         typename Parent::Notifier* nf = Parent::notifier();
  3504         for (nf->first(it); it != INVALID; nf->next(it)) {
  3505           Parent::set(it, 0);
  3506         }
  3507       }
  3508     };
  3509 
  3510   public:
  3511 
  3512     /// \brief Constructor.
  3513     ///
  3514     /// Constructor for creating an in-degree map.
  3515     explicit InDegMap(const Digraph& graph)
  3516       : _digraph(graph), _deg(graph) {
  3517       Parent::attach(_digraph.notifier(typename Digraph::Arc()));
  3518 
  3519       for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
  3520         _deg[it] = countInArcs(_digraph, it);
  3521       }
  3522     }
  3523 
  3524     /// \brief Gives back the in-degree of a Node.
  3525     ///
  3526     /// Gives back the in-degree of a Node.
  3527     int operator[](const Key& key) const {
  3528       return _deg[key];
  3529     }
  3530 
  3531   protected:
  3532 
  3533     typedef typename Digraph::Arc Arc;
  3534 
  3535     virtual void add(const Arc& arc) {
  3536       ++_deg[_digraph.target(arc)];
  3537     }
  3538 
  3539     virtual void add(const std::vector<Arc>& arcs) {
  3540       for (int i = 0; i < int(arcs.size()); ++i) {
  3541         ++_deg[_digraph.target(arcs[i])];
  3542       }
  3543     }
  3544 
  3545     virtual void erase(const Arc& arc) {
  3546       --_deg[_digraph.target(arc)];
  3547     }
  3548 
  3549     virtual void erase(const std::vector<Arc>& arcs) {
  3550       for (int i = 0; i < int(arcs.size()); ++i) {
  3551         --_deg[_digraph.target(arcs[i])];
  3552       }
  3553     }
  3554 
  3555     virtual void build() {
  3556       for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
  3557         _deg[it] = countInArcs(_digraph, it);
  3558       }
  3559     }
  3560 
  3561     virtual void clear() {
  3562       for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
  3563         _deg[it] = 0;
  3564       }
  3565     }
  3566   private:
  3567 
  3568     const Digraph& _digraph;
  3569     AutoNodeMap _deg;
  3570   };
  3571 
  3572   /// \brief Map of the out-degrees of nodes in a digraph.
  3573   ///
  3574   /// This map returns the out-degree of a node. Once it is constructed,
  3575   /// the degrees are stored in a standard \c NodeMap, so each query is done
  3576   /// in constant time. On the other hand, the values are updated automatically
  3577   /// whenever the digraph changes.
  3578   ///
  3579   /// \warning Besides \c addNode() and \c addArc(), a digraph structure
  3580   /// may provide alternative ways to modify the digraph.
  3581   /// The correct behavior of OutDegMap is not guarantied if these additional
  3582   /// features are used. For example the functions
  3583   /// \ref ListDigraph::changeSource() "changeSource()",
  3584   /// \ref ListDigraph::changeTarget() "changeTarget()" and
  3585   /// \ref ListDigraph::reverseArc() "reverseArc()"
  3586   /// of \ref ListDigraph will \e not update the degree values correctly.
  3587   ///
  3588   /// \sa InDegMap
  3589   template <typename GR>
  3590   class OutDegMap
  3591     : protected ItemSetTraits<GR, typename GR::Arc>
  3592       ::ItemNotifier::ObserverBase {
  3593 
  3594   public:
  3595 
  3596     /// The graph type of OutDegMap
  3597     typedef GR Graph;
  3598     typedef GR Digraph;
  3599     /// The key type
  3600     typedef typename Digraph::Node Key;
  3601     /// The value type
  3602     typedef int Value;
  3603 
  3604     typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
  3605     ::ItemNotifier::ObserverBase Parent;
  3606 
  3607   private:
  3608 
  3609     class AutoNodeMap
  3610       : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
  3611     public:
  3612 
  3613       typedef typename ItemSetTraits<Digraph, Key>::
  3614       template Map<int>::Type Parent;
  3615 
  3616       AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
  3617 
  3618       virtual void add(const Key& key) {
  3619         Parent::add(key);
  3620         Parent::set(key, 0);
  3621       }
  3622       virtual void add(const std::vector<Key>& keys) {
  3623         Parent::add(keys);
  3624         for (int i = 0; i < int(keys.size()); ++i) {
  3625           Parent::set(keys[i], 0);
  3626         }
  3627       }
  3628       virtual void build() {
  3629         Parent::build();
  3630         Key it;
  3631         typename Parent::Notifier* nf = Parent::notifier();
  3632         for (nf->first(it); it != INVALID; nf->next(it)) {
  3633           Parent::set(it, 0);
  3634         }
  3635       }
  3636     };
  3637 
  3638   public:
  3639 
  3640     /// \brief Constructor.
  3641     ///
  3642     /// Constructor for creating an out-degree map.
  3643     explicit OutDegMap(const Digraph& graph)
  3644       : _digraph(graph), _deg(graph) {
  3645       Parent::attach(_digraph.notifier(typename Digraph::Arc()));
  3646 
  3647       for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
  3648         _deg[it] = countOutArcs(_digraph, it);
  3649       }
  3650     }
  3651 
  3652     /// \brief Gives back the out-degree of a Node.
  3653     ///
  3654     /// Gives back the out-degree of a Node.
  3655     int operator[](const Key& key) const {
  3656       return _deg[key];
  3657     }
  3658 
  3659   protected:
  3660 
  3661     typedef typename Digraph::Arc Arc;
  3662 
  3663     virtual void add(const Arc& arc) {
  3664       ++_deg[_digraph.source(arc)];
  3665     }
  3666 
  3667     virtual void add(const std::vector<Arc>& arcs) {
  3668       for (int i = 0; i < int(arcs.size()); ++i) {
  3669         ++_deg[_digraph.source(arcs[i])];
  3670       }
  3671     }
  3672 
  3673     virtual void erase(const Arc& arc) {
  3674       --_deg[_digraph.source(arc)];
  3675     }
  3676 
  3677     virtual void erase(const std::vector<Arc>& arcs) {
  3678       for (int i = 0; i < int(arcs.size()); ++i) {
  3679         --_deg[_digraph.source(arcs[i])];
  3680       }
  3681     }
  3682 
  3683     virtual void build() {
  3684       for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
  3685         _deg[it] = countOutArcs(_digraph, it);
  3686       }
  3687     }
  3688 
  3689     virtual void clear() {
  3690       for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
  3691         _deg[it] = 0;
  3692       }
  3693     }
  3694   private:
  3695 
  3696     const Digraph& _digraph;
  3697     AutoNodeMap _deg;
  3698   };
  3699 
  3700   /// \brief Potential difference map
  3701   ///
  3702   /// PotentialDifferenceMap returns the difference between the potentials of
  3703   /// the source and target nodes of each arc in a digraph, i.e. it returns
  3704   /// \code
  3705   ///   potential[gr.target(arc)] - potential[gr.source(arc)].
  3706   /// \endcode
  3707   /// \tparam GR The digraph type.
  3708   /// \tparam POT A node map storing the potentials.
  3709   template <typename GR, typename POT>
  3710   class PotentialDifferenceMap {
  3711   public:
  3712     /// Key type
  3713     typedef typename GR::Arc Key;
  3714     /// Value type
  3715     typedef typename POT::Value Value;
  3716 
  3717     /// \brief Constructor
  3718     ///
  3719     /// Contructor of the map.
  3720     explicit PotentialDifferenceMap(const GR& gr,
  3721                                     const POT& potential)
  3722       : _digraph(gr), _potential(potential) {}
  3723 
  3724     /// \brief Returns the potential difference for the given arc.
  3725     ///
  3726     /// Returns the potential difference for the given arc, i.e.
  3727     /// \code
  3728     ///   potential[gr.target(arc)] - potential[gr.source(arc)].
  3729     /// \endcode
  3730     Value operator[](const Key& arc) const {
  3731       return _potential[_digraph.target(arc)] -
  3732         _potential[_digraph.source(arc)];
  3733     }
  3734 
  3735   private:
  3736     const GR& _digraph;
  3737     const POT& _potential;
  3738   };
  3739 
  3740   /// \brief Returns a PotentialDifferenceMap.
  3741   ///
  3742   /// This function just returns a PotentialDifferenceMap.
  3743   /// \relates PotentialDifferenceMap
  3744   template <typename GR, typename POT>
  3745   PotentialDifferenceMap<GR, POT>
  3746   potentialDifferenceMap(const GR& gr, const POT& potential) {
  3747     return PotentialDifferenceMap<GR, POT>(gr, potential);
  3748   }
  3749 
  3750   /// @}
  3751 }
  3752 
  3753 #endif // LEMON_MAPS_H