lemon/maps.h
changeset 780 580af8cf2f6a
parent 725 11404088d1a5
parent 717 684964884a2e
child 786 e20173729589
child 789 8ddb7deabab9
     1.1 --- a/lemon/maps.h	Thu Nov 05 10:01:02 2009 +0100
     1.2 +++ b/lemon/maps.h	Thu Nov 05 10:23:16 2009 +0100
     1.3 @@ -22,6 +22,7 @@
     1.4  #include <iterator>
     1.5  #include <functional>
     1.6  #include <vector>
     1.7 +#include <map>
     1.8  
     1.9  #include <lemon/core.h>
    1.10  
    1.11 @@ -29,8 +30,6 @@
    1.12  ///\ingroup maps
    1.13  ///\brief Miscellaneous property maps
    1.14  
    1.15 -#include <map>
    1.16 -
    1.17  namespace lemon {
    1.18  
    1.19    /// \addtogroup maps
    1.20 @@ -57,7 +56,7 @@
    1.21    /// its type definitions, or if you have to provide a writable map,
    1.22    /// but data written to it is not required (i.e. it will be sent to
    1.23    /// <tt>/dev/null</tt>).
    1.24 -  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
    1.25 +  /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
    1.26    ///
    1.27    /// \sa ConstMap
    1.28    template<typename K, typename V>
    1.29 @@ -90,7 +89,7 @@
    1.30    /// value to each key.
    1.31    ///
    1.32    /// In other aspects it is equivalent to \c NullMap.
    1.33 -  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
    1.34 +  /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
    1.35    /// concept, but it absorbs the data written to it.
    1.36    ///
    1.37    /// The simplest way of using this map is through the constMap()
    1.38 @@ -159,7 +158,7 @@
    1.39    /// value to each key.
    1.40    ///
    1.41    /// In other aspects it is equivalent to \c NullMap.
    1.42 -  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
    1.43 +  /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
    1.44    /// concept, but it absorbs the data written to it.
    1.45    ///
    1.46    /// The simplest way of using this map is through the constMap()
    1.47 @@ -233,7 +232,7 @@
    1.48    /// values to integer keys from the range <tt>[0..size-1]</tt>.
    1.49    /// It can be used with some data structures, for example
    1.50    /// \c UnionFind, \c BinHeap, when the used items are small
    1.51 -  /// integers. This map conforms the \ref concepts::ReferenceMap
    1.52 +  /// integers. This map conforms to the \ref concepts::ReferenceMap
    1.53    /// "ReferenceMap" concept.
    1.54    ///
    1.55    /// The simplest way of using this map is through the rangeMap()
    1.56 @@ -341,7 +340,7 @@
    1.57    /// that you can specify a default value for the keys that are not
    1.58    /// stored actually. This value can be different from the default
    1.59    /// contructed value (i.e. \c %Value()).
    1.60 -  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
    1.61 +  /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
    1.62    /// concept.
    1.63    ///
    1.64    /// This map is useful if a default value should be assigned to most of
    1.65 @@ -707,7 +706,7 @@
    1.66    /// "readable map" to another type using the default conversion.
    1.67    /// The \c Key type of it is inherited from \c M and the \c Value
    1.68    /// type is \c V.
    1.69 -  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
    1.70 +  /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
    1.71    ///
    1.72    /// The simplest way of using this map is through the convertMap()
    1.73    /// function.
    1.74 @@ -1790,11 +1789,11 @@
    1.75    /// order of Dfs algorithm, as the following examples show.
    1.76    /// \code
    1.77    ///   std::vector<Node> v;
    1.78 -  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
    1.79 +  ///   dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
    1.80    /// \endcode
    1.81    /// \code
    1.82    ///   std::vector<Node> v(countNodes(g));
    1.83 -  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
    1.84 +  ///   dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
    1.85    /// \endcode
    1.86    ///
    1.87    /// \note The container of the iterator must contain enough space
    1.88 @@ -1818,7 +1817,7 @@
    1.89    /// \brief Provides an immutable and unique id for each item in a graph.
    1.90    ///
    1.91    /// IdMap provides a unique and immutable id for each item of the
    1.92 -  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is 
    1.93 +  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
    1.94    ///  - \b unique: different items get different ids,
    1.95    ///  - \b immutable: the id of an item does not change (even if you
    1.96    ///    delete other nodes).
    1.97 @@ -1826,7 +1825,7 @@
    1.98    /// Using this map you get access (i.e. can read) the inner id values of
    1.99    /// the items stored in the graph, which is returned by the \c id()
   1.100    /// function of the graph. This map can be inverted with its member
   1.101 -  /// class \c InverseMap or with the \c operator() member.
   1.102 +  /// class \c InverseMap or with the \c operator()() member.
   1.103    ///
   1.104    /// \tparam GR The graph type.
   1.105    /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
   1.106 @@ -1866,9 +1865,11 @@
   1.107  
   1.108    public:
   1.109  
   1.110 -    /// \brief This class represents the inverse of its owner (IdMap).
   1.111 +    /// \brief The inverse map type of IdMap.
   1.112      ///
   1.113 -    /// This class represents the inverse of its owner (IdMap).
   1.114 +    /// The inverse map type of IdMap. The subscript operator gives back
   1.115 +    /// an item by its id.
   1.116 +    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
   1.117      /// \see inverse()
   1.118      class InverseMap {
   1.119      public:
   1.120 @@ -1883,9 +1884,9 @@
   1.121        /// Constructor for creating an id-to-item map.
   1.122        explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
   1.123  
   1.124 -      /// \brief Gives back the given item from its id.
   1.125 +      /// \brief Gives back an item by its id.
   1.126        ///
   1.127 -      /// Gives back the given item from its id.
   1.128 +      /// Gives back an item by its id.
   1.129        Item operator[](int id) const { return _graph->fromId(id, Item());}
   1.130  
   1.131      private:
   1.132 @@ -1898,14 +1899,31 @@
   1.133      InverseMap inverse() const { return InverseMap(*_graph);}
   1.134    };
   1.135  
   1.136 +  /// \brief Returns an \c IdMap class.
   1.137 +  ///
   1.138 +  /// This function just returns an \c IdMap class.
   1.139 +  /// \relates IdMap
   1.140 +  template <typename K, typename GR>
   1.141 +  inline IdMap<GR, K> idMap(const GR& graph) {
   1.142 +    return IdMap<GR, K>(graph);
   1.143 +  }
   1.144  
   1.145    /// \brief General cross reference graph map type.
   1.146  
   1.147    /// This class provides simple invertable graph maps.
   1.148    /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
   1.149    /// and if a key is set to a new value, then stores it in the inverse map.
   1.150 -  /// The values of the map can be accessed
   1.151 -  /// with stl compatible forward iterator.
   1.152 +  /// The graph items can be accessed by their values either using
   1.153 +  /// \c InverseMap or \c operator()(), and the values of the map can be
   1.154 +  /// accessed with an STL compatible forward iterator (\c ValueIt).
   1.155 +  /// 
   1.156 +  /// This map is intended to be used when all associated values are
   1.157 +  /// different (the map is actually invertable) or there are only a few
   1.158 +  /// items with the same value.
   1.159 +  /// Otherwise consider to use \c IterableValueMap, which is more 
   1.160 +  /// suitable and more efficient for such cases. It provides iterators
   1.161 +  /// to traverse the items with the same associated value, however
   1.162 +  /// it does not have \c InverseMap.
   1.163    ///
   1.164    /// This type is not reference map, so it cannot be modified with
   1.165    /// the subscript operator.
   1.166 @@ -1946,56 +1964,66 @@
   1.167  
   1.168      /// \brief Forward iterator for values.
   1.169      ///
   1.170 -    /// This iterator is an stl compatible forward
   1.171 +    /// This iterator is an STL compatible forward
   1.172      /// iterator on the values of the map. The values can
   1.173      /// be accessed in the <tt>[beginValue, endValue)</tt> range.
   1.174      /// They are considered with multiplicity, so each value is
   1.175      /// traversed for each item it is assigned to.
   1.176 -    class ValueIterator
   1.177 +    class ValueIt
   1.178        : public std::iterator<std::forward_iterator_tag, Value> {
   1.179        friend class CrossRefMap;
   1.180      private:
   1.181 -      ValueIterator(typename Container::const_iterator _it)
   1.182 +      ValueIt(typename Container::const_iterator _it)
   1.183          : it(_it) {}
   1.184      public:
   1.185  
   1.186 -      ValueIterator() {}
   1.187 -
   1.188 -      ValueIterator& operator++() { ++it; return *this; }
   1.189 -      ValueIterator operator++(int) {
   1.190 -        ValueIterator tmp(*this);
   1.191 +      /// Constructor
   1.192 +      ValueIt() {}
   1.193 +
   1.194 +      /// \e
   1.195 +      ValueIt& operator++() { ++it; return *this; }
   1.196 +      /// \e
   1.197 +      ValueIt operator++(int) {
   1.198 +        ValueIt tmp(*this);
   1.199          operator++();
   1.200          return tmp;
   1.201        }
   1.202  
   1.203 +      /// \e
   1.204        const Value& operator*() const { return it->first; }
   1.205 +      /// \e
   1.206        const Value* operator->() const { return &(it->first); }
   1.207  
   1.208 -      bool operator==(ValueIterator jt) const { return it == jt.it; }
   1.209 -      bool operator!=(ValueIterator jt) const { return it != jt.it; }
   1.210 +      /// \e
   1.211 +      bool operator==(ValueIt jt) const { return it == jt.it; }
   1.212 +      /// \e
   1.213 +      bool operator!=(ValueIt jt) const { return it != jt.it; }
   1.214  
   1.215      private:
   1.216        typename Container::const_iterator it;
   1.217      };
   1.218 +    
   1.219 +    /// Alias for \c ValueIt
   1.220 +    typedef ValueIt ValueIterator;
   1.221  
   1.222      /// \brief Returns an iterator to the first value.
   1.223      ///
   1.224 -    /// Returns an stl compatible iterator to the
   1.225 +    /// Returns an STL compatible iterator to the
   1.226      /// first value of the map. The values of the
   1.227      /// map can be accessed in the <tt>[beginValue, endValue)</tt>
   1.228      /// range.
   1.229 -    ValueIterator beginValue() const {
   1.230 -      return ValueIterator(_inv_map.begin());
   1.231 +    ValueIt beginValue() const {
   1.232 +      return ValueIt(_inv_map.begin());
   1.233      }
   1.234  
   1.235      /// \brief Returns an iterator after the last value.
   1.236      ///
   1.237 -    /// Returns an stl compatible iterator after the
   1.238 +    /// Returns an STL compatible iterator after the
   1.239      /// last value of the map. The values of the
   1.240      /// map can be accessed in the <tt>[beginValue, endValue)</tt>
   1.241      /// range.
   1.242 -    ValueIterator endValue() const {
   1.243 -      return ValueIterator(_inv_map.end());
   1.244 +    ValueIt endValue() const {
   1.245 +      return ValueIt(_inv_map.end());
   1.246      }
   1.247  
   1.248      /// \brief Sets the value associated with the given key.
   1.249 @@ -2033,6 +2061,14 @@
   1.250        typename Container::const_iterator it = _inv_map.find(val);
   1.251        return it != _inv_map.end() ? it->second : INVALID;
   1.252      }
   1.253 +    
   1.254 +    /// \brief Returns the number of items with the given value.
   1.255 +    ///
   1.256 +    /// This function returns the number of items with the given value
   1.257 +    /// associated with it.
   1.258 +    int count(const Value &val) const {
   1.259 +      return _inv_map.count(val);
   1.260 +    }
   1.261  
   1.262    protected:
   1.263  
   1.264 @@ -2083,10 +2119,12 @@
   1.265  
   1.266    public:
   1.267  
   1.268 -    /// \brief The inverse map type.
   1.269 +    /// \brief The inverse map type of CrossRefMap.
   1.270      ///
   1.271 -    /// The inverse of this map. The subscript operator of the map
   1.272 -    /// gives back the item that was last assigned to the value.
   1.273 +    /// The inverse map type of CrossRefMap. The subscript operator gives
   1.274 +    /// back an item by its value.
   1.275 +    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
   1.276 +    /// \see inverse()
   1.277      class InverseMap {
   1.278      public:
   1.279        /// \brief Constructor
   1.280 @@ -2113,20 +2151,20 @@
   1.281        const CrossRefMap& _inverted;
   1.282      };
   1.283  
   1.284 -    /// \brief It gives back the read-only inverse map.
   1.285 +    /// \brief Gives back the inverse of the map.
   1.286      ///
   1.287 -    /// It gives back the read-only inverse map.
   1.288 +    /// Gives back the inverse of the CrossRefMap.
   1.289      InverseMap inverse() const {
   1.290        return InverseMap(*this);
   1.291      }
   1.292  
   1.293    };
   1.294  
   1.295 -  /// \brief Provides continuous and unique ID for the
   1.296 +  /// \brief Provides continuous and unique id for the
   1.297    /// items of a graph.
   1.298    ///
   1.299    /// RangeIdMap provides a unique and continuous
   1.300 -  /// ID for each item of a given type (\c Node, \c Arc or
   1.301 +  /// id for each item of a given type (\c Node, \c Arc or
   1.302    /// \c Edge) in a graph. This id is
   1.303    ///  - \b unique: different items get different ids,
   1.304    ///  - \b continuous: the range of the ids is the set of integers
   1.305 @@ -2137,7 +2175,7 @@
   1.306    /// Thus this id is not (necessarily) the same as what can get using
   1.307    /// the \c id() function of the graph or \ref IdMap.
   1.308    /// This map can be inverted with its member class \c InverseMap,
   1.309 -  /// or with the \c operator() member.
   1.310 +  /// or with the \c operator()() member.
   1.311    ///
   1.312    /// \tparam GR The graph type.
   1.313    /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
   1.314 @@ -2265,16 +2303,16 @@
   1.315        _inv_map[pi] = q;
   1.316      }
   1.317  
   1.318 -    /// \brief Gives back the \e RangeId of the item
   1.319 +    /// \brief Gives back the \e range \e id of the item
   1.320      ///
   1.321 -    /// Gives back the \e RangeId of the item.
   1.322 +    /// Gives back the \e range \e id of the item.
   1.323      int operator[](const Item& item) const {
   1.324        return Map::operator[](item);
   1.325      }
   1.326  
   1.327 -    /// \brief Gives back the item belonging to a \e RangeId
   1.328 -    /// 
   1.329 -    /// Gives back the item belonging to a \e RangeId.
   1.330 +    /// \brief Gives back the item belonging to a \e range \e id
   1.331 +    ///
   1.332 +    /// Gives back the item belonging to the given \e range \e id.
   1.333      Item operator()(int id) const {
   1.334        return _inv_map[id];
   1.335      }
   1.336 @@ -2288,7 +2326,9 @@
   1.337  
   1.338      /// \brief The inverse map type of RangeIdMap.
   1.339      ///
   1.340 -    /// The inverse map type of RangeIdMap.
   1.341 +    /// The inverse map type of RangeIdMap. The subscript operator gives
   1.342 +    /// back an item by its \e range \e id.
   1.343 +    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
   1.344      class InverseMap {
   1.345      public:
   1.346        /// \brief Constructor
   1.347 @@ -2306,7 +2346,7 @@
   1.348        /// \brief Subscript operator.
   1.349        ///
   1.350        /// Subscript operator. It gives back the item
   1.351 -      /// that the descriptor currently belongs to.
   1.352 +      /// that the given \e range \e id currently belongs to.
   1.353        Value operator[](const Key& key) const {
   1.354          return _inverted(key);
   1.355        }
   1.356 @@ -2324,12 +2364,932 @@
   1.357  
   1.358      /// \brief Gives back the inverse of the map.
   1.359      ///
   1.360 -    /// Gives back the inverse of the map.
   1.361 +    /// Gives back the inverse of the RangeIdMap.
   1.362      const InverseMap inverse() const {
   1.363        return InverseMap(*this);
   1.364      }
   1.365    };
   1.366  
   1.367 +  /// \brief Returns a \c RangeIdMap class.
   1.368 +  ///
   1.369 +  /// This function just returns an \c RangeIdMap class.
   1.370 +  /// \relates RangeIdMap
   1.371 +  template <typename K, typename GR>
   1.372 +  inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
   1.373 +    return RangeIdMap<GR, K>(graph);
   1.374 +  }
   1.375 +  
   1.376 +  /// \brief Dynamic iterable \c bool map.
   1.377 +  ///
   1.378 +  /// This class provides a special graph map type which can store a
   1.379 +  /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
   1.380 +  /// For both \c true and \c false values it is possible to iterate on
   1.381 +  /// the keys mapped to the value.
   1.382 +  ///
   1.383 +  /// This type is a reference map, so it can be modified with the
   1.384 +  /// subscript operator.
   1.385 +  ///
   1.386 +  /// \tparam GR The graph type.
   1.387 +  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
   1.388 +  /// \c GR::Edge).
   1.389 +  ///
   1.390 +  /// \see IterableIntMap, IterableValueMap
   1.391 +  /// \see CrossRefMap
   1.392 +  template <typename GR, typename K>
   1.393 +  class IterableBoolMap
   1.394 +    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
   1.395 +  private:
   1.396 +    typedef GR Graph;
   1.397 +
   1.398 +    typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
   1.399 +    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
   1.400 +
   1.401 +    std::vector<K> _array;
   1.402 +    int _sep;
   1.403 +
   1.404 +  public:
   1.405 +
   1.406 +    /// Indicates that the map is reference map.
   1.407 +    typedef True ReferenceMapTag;
   1.408 +
   1.409 +    /// The key type
   1.410 +    typedef K Key;
   1.411 +    /// The value type
   1.412 +    typedef bool Value;
   1.413 +    /// The const reference type.
   1.414 +    typedef const Value& ConstReference;
   1.415 +
   1.416 +  private:
   1.417 +
   1.418 +    int position(const Key& key) const {
   1.419 +      return Parent::operator[](key);
   1.420 +    }
   1.421 +
   1.422 +  public:
   1.423 +
   1.424 +    /// \brief Reference to the value of the map.
   1.425 +    ///
   1.426 +    /// This class is similar to the \c bool type. It can be converted to
   1.427 +    /// \c bool and it provides the same operators.
   1.428 +    class Reference {
   1.429 +      friend class IterableBoolMap;
   1.430 +    private:
   1.431 +      Reference(IterableBoolMap& map, const Key& key)
   1.432 +        : _key(key), _map(map) {}
   1.433 +    public:
   1.434 +
   1.435 +      Reference& operator=(const Reference& value) {
   1.436 +        _map.set(_key, static_cast<bool>(value));
   1.437 +         return *this;
   1.438 +      }
   1.439 +
   1.440 +      operator bool() const {
   1.441 +        return static_cast<const IterableBoolMap&>(_map)[_key];
   1.442 +      }
   1.443 +
   1.444 +      Reference& operator=(bool value) {
   1.445 +        _map.set(_key, value);
   1.446 +        return *this;
   1.447 +      }
   1.448 +      Reference& operator&=(bool value) {
   1.449 +        _map.set(_key, _map[_key] & value);
   1.450 +        return *this;
   1.451 +      }
   1.452 +      Reference& operator|=(bool value) {
   1.453 +        _map.set(_key, _map[_key] | value);
   1.454 +        return *this;
   1.455 +      }
   1.456 +      Reference& operator^=(bool value) {
   1.457 +        _map.set(_key, _map[_key] ^ value);
   1.458 +        return *this;
   1.459 +      }
   1.460 +    private:
   1.461 +      Key _key;
   1.462 +      IterableBoolMap& _map;
   1.463 +    };
   1.464 +
   1.465 +    /// \brief Constructor of the map with a default value.
   1.466 +    ///
   1.467 +    /// Constructor of the map with a default value.
   1.468 +    explicit IterableBoolMap(const Graph& graph, bool def = false)
   1.469 +      : Parent(graph) {
   1.470 +      typename Parent::Notifier* nf = Parent::notifier();
   1.471 +      Key it;
   1.472 +      for (nf->first(it); it != INVALID; nf->next(it)) {
   1.473 +        Parent::set(it, _array.size());
   1.474 +        _array.push_back(it);
   1.475 +      }
   1.476 +      _sep = (def ? _array.size() : 0);
   1.477 +    }
   1.478 +
   1.479 +    /// \brief Const subscript operator of the map.
   1.480 +    ///
   1.481 +    /// Const subscript operator of the map.
   1.482 +    bool operator[](const Key& key) const {
   1.483 +      return position(key) < _sep;
   1.484 +    }
   1.485 +
   1.486 +    /// \brief Subscript operator of the map.
   1.487 +    ///
   1.488 +    /// Subscript operator of the map.
   1.489 +    Reference operator[](const Key& key) {
   1.490 +      return Reference(*this, key);
   1.491 +    }
   1.492 +
   1.493 +    /// \brief Set operation of the map.
   1.494 +    ///
   1.495 +    /// Set operation of the map.
   1.496 +    void set(const Key& key, bool value) {
   1.497 +      int pos = position(key);
   1.498 +      if (value) {
   1.499 +        if (pos < _sep) return;
   1.500 +        Key tmp = _array[_sep];
   1.501 +        _array[_sep] = key;
   1.502 +        Parent::set(key, _sep);
   1.503 +        _array[pos] = tmp;
   1.504 +        Parent::set(tmp, pos);
   1.505 +        ++_sep;
   1.506 +      } else {
   1.507 +        if (pos >= _sep) return;
   1.508 +        --_sep;
   1.509 +        Key tmp = _array[_sep];
   1.510 +        _array[_sep] = key;
   1.511 +        Parent::set(key, _sep);
   1.512 +        _array[pos] = tmp;
   1.513 +        Parent::set(tmp, pos);
   1.514 +      }
   1.515 +    }
   1.516 +
   1.517 +    /// \brief Set all items.
   1.518 +    ///
   1.519 +    /// Set all items in the map.
   1.520 +    /// \note Constant time operation.
   1.521 +    void setAll(bool value) {
   1.522 +      _sep = (value ? _array.size() : 0);
   1.523 +    }
   1.524 +
   1.525 +    /// \brief Returns the number of the keys mapped to \c true.
   1.526 +    ///
   1.527 +    /// Returns the number of the keys mapped to \c true.
   1.528 +    int trueNum() const {
   1.529 +      return _sep;
   1.530 +    }
   1.531 +
   1.532 +    /// \brief Returns the number of the keys mapped to \c false.
   1.533 +    ///
   1.534 +    /// Returns the number of the keys mapped to \c false.
   1.535 +    int falseNum() const {
   1.536 +      return _array.size() - _sep;
   1.537 +    }
   1.538 +
   1.539 +    /// \brief Iterator for the keys mapped to \c true.
   1.540 +    ///
   1.541 +    /// Iterator for the keys mapped to \c true. It works
   1.542 +    /// like a graph item iterator, it can be converted to
   1.543 +    /// the key type of the map, incremented with \c ++ operator, and
   1.544 +    /// if the iterator leaves the last valid key, it will be equal to
   1.545 +    /// \c INVALID.
   1.546 +    class TrueIt : public Key {
   1.547 +    public:
   1.548 +      typedef Key Parent;
   1.549 +
   1.550 +      /// \brief Creates an iterator.
   1.551 +      ///
   1.552 +      /// Creates an iterator. It iterates on the
   1.553 +      /// keys mapped to \c true.
   1.554 +      /// \param map The IterableBoolMap.
   1.555 +      explicit TrueIt(const IterableBoolMap& map)
   1.556 +        : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
   1.557 +          _map(&map) {}
   1.558 +
   1.559 +      /// \brief Invalid constructor \& conversion.
   1.560 +      ///
   1.561 +      /// This constructor initializes the iterator to be invalid.
   1.562 +      /// \sa Invalid for more details.
   1.563 +      TrueIt(Invalid) : Parent(INVALID), _map(0) {}
   1.564 +
   1.565 +      /// \brief Increment operator.
   1.566 +      ///
   1.567 +      /// Increment operator.
   1.568 +      TrueIt& operator++() {
   1.569 +        int pos = _map->position(*this);
   1.570 +        Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
   1.571 +        return *this;
   1.572 +      }
   1.573 +
   1.574 +    private:
   1.575 +      const IterableBoolMap* _map;
   1.576 +    };
   1.577 +
   1.578 +    /// \brief Iterator for the keys mapped to \c false.
   1.579 +    ///
   1.580 +    /// Iterator for the keys mapped to \c false. It works
   1.581 +    /// like a graph item iterator, it can be converted to
   1.582 +    /// the key type of the map, incremented with \c ++ operator, and
   1.583 +    /// if the iterator leaves the last valid key, it will be equal to
   1.584 +    /// \c INVALID.
   1.585 +    class FalseIt : public Key {
   1.586 +    public:
   1.587 +      typedef Key Parent;
   1.588 +
   1.589 +      /// \brief Creates an iterator.
   1.590 +      ///
   1.591 +      /// Creates an iterator. It iterates on the
   1.592 +      /// keys mapped to \c false.
   1.593 +      /// \param map The IterableBoolMap.
   1.594 +      explicit FalseIt(const IterableBoolMap& map)
   1.595 +        : Parent(map._sep < int(map._array.size()) ?
   1.596 +                 map._array.back() : INVALID), _map(&map) {}
   1.597 +
   1.598 +      /// \brief Invalid constructor \& conversion.
   1.599 +      ///
   1.600 +      /// This constructor initializes the iterator to be invalid.
   1.601 +      /// \sa Invalid for more details.
   1.602 +      FalseIt(Invalid) : Parent(INVALID), _map(0) {}
   1.603 +
   1.604 +      /// \brief Increment operator.
   1.605 +      ///
   1.606 +      /// Increment operator.
   1.607 +      FalseIt& operator++() {
   1.608 +        int pos = _map->position(*this);
   1.609 +        Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
   1.610 +        return *this;
   1.611 +      }
   1.612 +
   1.613 +    private:
   1.614 +      const IterableBoolMap* _map;
   1.615 +    };
   1.616 +
   1.617 +    /// \brief Iterator for the keys mapped to a given value.
   1.618 +    ///
   1.619 +    /// Iterator for the keys mapped to a given value. It works
   1.620 +    /// like a graph item iterator, it can be converted to
   1.621 +    /// the key type of the map, incremented with \c ++ operator, and
   1.622 +    /// if the iterator leaves the last valid key, it will be equal to
   1.623 +    /// \c INVALID.
   1.624 +    class ItemIt : public Key {
   1.625 +    public:
   1.626 +      typedef Key Parent;
   1.627 +
   1.628 +      /// \brief Creates an iterator with a value.
   1.629 +      ///
   1.630 +      /// Creates an iterator with a value. It iterates on the
   1.631 +      /// keys mapped to the given value.
   1.632 +      /// \param map The IterableBoolMap.
   1.633 +      /// \param value The value.
   1.634 +      ItemIt(const IterableBoolMap& map, bool value)
   1.635 +        : Parent(value ? 
   1.636 +                 (map._sep > 0 ?
   1.637 +                  map._array[map._sep - 1] : INVALID) :
   1.638 +                 (map._sep < int(map._array.size()) ?
   1.639 +                  map._array.back() : INVALID)), _map(&map) {}
   1.640 +
   1.641 +      /// \brief Invalid constructor \& conversion.
   1.642 +      ///
   1.643 +      /// This constructor initializes the iterator to be invalid.
   1.644 +      /// \sa Invalid for more details.
   1.645 +      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
   1.646 +
   1.647 +      /// \brief Increment operator.
   1.648 +      ///
   1.649 +      /// Increment operator.
   1.650 +      ItemIt& operator++() {
   1.651 +        int pos = _map->position(*this);
   1.652 +        int _sep = pos >= _map->_sep ? _map->_sep : 0;
   1.653 +        Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
   1.654 +        return *this;
   1.655 +      }
   1.656 +
   1.657 +    private:
   1.658 +      const IterableBoolMap* _map;
   1.659 +    };
   1.660 +
   1.661 +  protected:
   1.662 +
   1.663 +    virtual void add(const Key& key) {
   1.664 +      Parent::add(key);
   1.665 +      Parent::set(key, _array.size());
   1.666 +      _array.push_back(key);
   1.667 +    }
   1.668 +
   1.669 +    virtual void add(const std::vector<Key>& keys) {
   1.670 +      Parent::add(keys);
   1.671 +      for (int i = 0; i < int(keys.size()); ++i) {
   1.672 +        Parent::set(keys[i], _array.size());
   1.673 +        _array.push_back(keys[i]);
   1.674 +      }
   1.675 +    }
   1.676 +
   1.677 +    virtual void erase(const Key& key) {
   1.678 +      int pos = position(key);
   1.679 +      if (pos < _sep) {
   1.680 +        --_sep;
   1.681 +        Parent::set(_array[_sep], pos);
   1.682 +        _array[pos] = _array[_sep];
   1.683 +        Parent::set(_array.back(), _sep);
   1.684 +        _array[_sep] = _array.back();
   1.685 +        _array.pop_back();
   1.686 +      } else {
   1.687 +        Parent::set(_array.back(), pos);
   1.688 +        _array[pos] = _array.back();
   1.689 +        _array.pop_back();
   1.690 +      }
   1.691 +      Parent::erase(key);
   1.692 +    }
   1.693 +
   1.694 +    virtual void erase(const std::vector<Key>& keys) {
   1.695 +      for (int i = 0; i < int(keys.size()); ++i) {
   1.696 +        int pos = position(keys[i]);
   1.697 +        if (pos < _sep) {
   1.698 +          --_sep;
   1.699 +          Parent::set(_array[_sep], pos);
   1.700 +          _array[pos] = _array[_sep];
   1.701 +          Parent::set(_array.back(), _sep);
   1.702 +          _array[_sep] = _array.back();
   1.703 +          _array.pop_back();
   1.704 +        } else {
   1.705 +          Parent::set(_array.back(), pos);
   1.706 +          _array[pos] = _array.back();
   1.707 +          _array.pop_back();
   1.708 +        }
   1.709 +      }
   1.710 +      Parent::erase(keys);
   1.711 +    }
   1.712 +
   1.713 +    virtual void build() {
   1.714 +      Parent::build();
   1.715 +      typename Parent::Notifier* nf = Parent::notifier();
   1.716 +      Key it;
   1.717 +      for (nf->first(it); it != INVALID; nf->next(it)) {
   1.718 +        Parent::set(it, _array.size());
   1.719 +        _array.push_back(it);
   1.720 +      }
   1.721 +      _sep = 0;
   1.722 +    }
   1.723 +
   1.724 +    virtual void clear() {
   1.725 +      _array.clear();
   1.726 +      _sep = 0;
   1.727 +      Parent::clear();
   1.728 +    }
   1.729 +
   1.730 +  };
   1.731 +
   1.732 +
   1.733 +  namespace _maps_bits {
   1.734 +    template <typename Item>
   1.735 +    struct IterableIntMapNode {
   1.736 +      IterableIntMapNode() : value(-1) {}
   1.737 +      IterableIntMapNode(int _value) : value(_value) {}
   1.738 +      Item prev, next;
   1.739 +      int value;
   1.740 +    };
   1.741 +  }
   1.742 +
   1.743 +  /// \brief Dynamic iterable integer map.
   1.744 +  ///
   1.745 +  /// This class provides a special graph map type which can store an
   1.746 +  /// integer value for graph items (\c Node, \c Arc or \c Edge).
   1.747 +  /// For each non-negative value it is possible to iterate on the keys
   1.748 +  /// mapped to the value.
   1.749 +  ///
   1.750 +  /// This map is intended to be used with small integer values, for which
   1.751 +  /// it is efficient, and supports iteration only for non-negative values.
   1.752 +  /// If you need large values and/or iteration for negative integers,
   1.753 +  /// consider to use \ref IterableValueMap instead.
   1.754 +  ///
   1.755 +  /// This type is a reference map, so it can be modified with the
   1.756 +  /// subscript operator.
   1.757 +  ///
   1.758 +  /// \note The size of the data structure depends on the largest
   1.759 +  /// value in the map.
   1.760 +  ///
   1.761 +  /// \tparam GR The graph type.
   1.762 +  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
   1.763 +  /// \c GR::Edge).
   1.764 +  ///
   1.765 +  /// \see IterableBoolMap, IterableValueMap
   1.766 +  /// \see CrossRefMap
   1.767 +  template <typename GR, typename K>
   1.768 +  class IterableIntMap
   1.769 +    : protected ItemSetTraits<GR, K>::
   1.770 +        template Map<_maps_bits::IterableIntMapNode<K> >::Type {
   1.771 +  public:
   1.772 +    typedef typename ItemSetTraits<GR, K>::
   1.773 +      template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
   1.774 +
   1.775 +    /// The key type
   1.776 +    typedef K Key;
   1.777 +    /// The value type
   1.778 +    typedef int Value;
   1.779 +    /// The graph type
   1.780 +    typedef GR Graph;
   1.781 +
   1.782 +    /// \brief Constructor of the map.
   1.783 +    ///
   1.784 +    /// Constructor of the map. It sets all values to -1.
   1.785 +    explicit IterableIntMap(const Graph& graph)
   1.786 +      : Parent(graph) {}
   1.787 +
   1.788 +    /// \brief Constructor of the map with a given value.
   1.789 +    ///
   1.790 +    /// Constructor of the map with a given value.
   1.791 +    explicit IterableIntMap(const Graph& graph, int value)
   1.792 +      : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
   1.793 +      if (value >= 0) {
   1.794 +        for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
   1.795 +          lace(it);
   1.796 +        }
   1.797 +      }
   1.798 +    }
   1.799 +
   1.800 +  private:
   1.801 +
   1.802 +    void unlace(const Key& key) {
   1.803 +      typename Parent::Value& node = Parent::operator[](key);
   1.804 +      if (node.value < 0) return;
   1.805 +      if (node.prev != INVALID) {
   1.806 +        Parent::operator[](node.prev).next = node.next;
   1.807 +      } else {
   1.808 +        _first[node.value] = node.next;
   1.809 +      }
   1.810 +      if (node.next != INVALID) {
   1.811 +        Parent::operator[](node.next).prev = node.prev;
   1.812 +      }
   1.813 +      while (!_first.empty() && _first.back() == INVALID) {
   1.814 +        _first.pop_back();
   1.815 +      }
   1.816 +    }
   1.817 +
   1.818 +    void lace(const Key& key) {
   1.819 +      typename Parent::Value& node = Parent::operator[](key);
   1.820 +      if (node.value < 0) return;
   1.821 +      if (node.value >= int(_first.size())) {
   1.822 +        _first.resize(node.value + 1, INVALID);
   1.823 +      }
   1.824 +      node.prev = INVALID;
   1.825 +      node.next = _first[node.value];
   1.826 +      if (node.next != INVALID) {
   1.827 +        Parent::operator[](node.next).prev = key;
   1.828 +      }
   1.829 +      _first[node.value] = key;
   1.830 +    }
   1.831 +
   1.832 +  public:
   1.833 +
   1.834 +    /// Indicates that the map is reference map.
   1.835 +    typedef True ReferenceMapTag;
   1.836 +
   1.837 +    /// \brief Reference to the value of the map.
   1.838 +    ///
   1.839 +    /// This class is similar to the \c int type. It can
   1.840 +    /// be converted to \c int and it has the same operators.
   1.841 +    class Reference {
   1.842 +      friend class IterableIntMap;
   1.843 +    private:
   1.844 +      Reference(IterableIntMap& map, const Key& key)
   1.845 +        : _key(key), _map(map) {}
   1.846 +    public:
   1.847 +
   1.848 +      Reference& operator=(const Reference& value) {
   1.849 +        _map.set(_key, static_cast<const int&>(value));
   1.850 +         return *this;
   1.851 +      }
   1.852 +
   1.853 +      operator const int&() const {
   1.854 +        return static_cast<const IterableIntMap&>(_map)[_key];
   1.855 +      }
   1.856 +
   1.857 +      Reference& operator=(int value) {
   1.858 +        _map.set(_key, value);
   1.859 +        return *this;
   1.860 +      }
   1.861 +      Reference& operator++() {
   1.862 +        _map.set(_key, _map[_key] + 1);
   1.863 +        return *this;
   1.864 +      }
   1.865 +      int operator++(int) {
   1.866 +        int value = _map[_key];
   1.867 +        _map.set(_key, value + 1);
   1.868 +        return value;
   1.869 +      }
   1.870 +      Reference& operator--() {
   1.871 +        _map.set(_key, _map[_key] - 1);
   1.872 +        return *this;
   1.873 +      }
   1.874 +      int operator--(int) {
   1.875 +        int value = _map[_key];
   1.876 +        _map.set(_key, value - 1);
   1.877 +        return value;
   1.878 +      }
   1.879 +      Reference& operator+=(int value) {
   1.880 +        _map.set(_key, _map[_key] + value);
   1.881 +        return *this;
   1.882 +      }
   1.883 +      Reference& operator-=(int value) {
   1.884 +        _map.set(_key, _map[_key] - value);
   1.885 +        return *this;
   1.886 +      }
   1.887 +      Reference& operator*=(int value) {
   1.888 +        _map.set(_key, _map[_key] * value);
   1.889 +        return *this;
   1.890 +      }
   1.891 +      Reference& operator/=(int value) {
   1.892 +        _map.set(_key, _map[_key] / value);
   1.893 +        return *this;
   1.894 +      }
   1.895 +      Reference& operator%=(int value) {
   1.896 +        _map.set(_key, _map[_key] % value);
   1.897 +        return *this;
   1.898 +      }
   1.899 +      Reference& operator&=(int value) {
   1.900 +        _map.set(_key, _map[_key] & value);
   1.901 +        return *this;
   1.902 +      }
   1.903 +      Reference& operator|=(int value) {
   1.904 +        _map.set(_key, _map[_key] | value);
   1.905 +        return *this;
   1.906 +      }
   1.907 +      Reference& operator^=(int value) {
   1.908 +        _map.set(_key, _map[_key] ^ value);
   1.909 +        return *this;
   1.910 +      }
   1.911 +      Reference& operator<<=(int value) {
   1.912 +        _map.set(_key, _map[_key] << value);
   1.913 +        return *this;
   1.914 +      }
   1.915 +      Reference& operator>>=(int value) {
   1.916 +        _map.set(_key, _map[_key] >> value);
   1.917 +        return *this;
   1.918 +      }
   1.919 +
   1.920 +    private:
   1.921 +      Key _key;
   1.922 +      IterableIntMap& _map;
   1.923 +    };
   1.924 +
   1.925 +    /// The const reference type.
   1.926 +    typedef const Value& ConstReference;
   1.927 +
   1.928 +    /// \brief Gives back the maximal value plus one.
   1.929 +    ///
   1.930 +    /// Gives back the maximal value plus one.
   1.931 +    int size() const {
   1.932 +      return _first.size();
   1.933 +    }
   1.934 +
   1.935 +    /// \brief Set operation of the map.
   1.936 +    ///
   1.937 +    /// Set operation of the map.
   1.938 +    void set(const Key& key, const Value& value) {
   1.939 +      unlace(key);
   1.940 +      Parent::operator[](key).value = value;
   1.941 +      lace(key);
   1.942 +    }
   1.943 +
   1.944 +    /// \brief Const subscript operator of the map.
   1.945 +    ///
   1.946 +    /// Const subscript operator of the map.
   1.947 +    const Value& operator[](const Key& key) const {
   1.948 +      return Parent::operator[](key).value;
   1.949 +    }
   1.950 +
   1.951 +    /// \brief Subscript operator of the map.
   1.952 +    ///
   1.953 +    /// Subscript operator of the map.
   1.954 +    Reference operator[](const Key& key) {
   1.955 +      return Reference(*this, key);
   1.956 +    }
   1.957 +
   1.958 +    /// \brief Iterator for the keys with the same value.
   1.959 +    ///
   1.960 +    /// Iterator for the keys with the same value. It works
   1.961 +    /// like a graph item iterator, it can be converted to
   1.962 +    /// the item type of the map, incremented with \c ++ operator, and
   1.963 +    /// if the iterator leaves the last valid item, it will be equal to
   1.964 +    /// \c INVALID.
   1.965 +    class ItemIt : public Key {
   1.966 +    public:
   1.967 +      typedef Key Parent;
   1.968 +
   1.969 +      /// \brief Invalid constructor \& conversion.
   1.970 +      ///
   1.971 +      /// This constructor initializes the iterator to be invalid.
   1.972 +      /// \sa Invalid for more details.
   1.973 +      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
   1.974 +
   1.975 +      /// \brief Creates an iterator with a value.
   1.976 +      ///
   1.977 +      /// Creates an iterator with a value. It iterates on the
   1.978 +      /// keys mapped to the given value.
   1.979 +      /// \param map The IterableIntMap.
   1.980 +      /// \param value The value.
   1.981 +      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
   1.982 +        if (value < 0 || value >= int(_map->_first.size())) {
   1.983 +          Parent::operator=(INVALID);
   1.984 +        } else {
   1.985 +          Parent::operator=(_map->_first[value]);
   1.986 +        }
   1.987 +      }
   1.988 +
   1.989 +      /// \brief Increment operator.
   1.990 +      ///
   1.991 +      /// Increment operator.
   1.992 +      ItemIt& operator++() {
   1.993 +        Parent::operator=(_map->IterableIntMap::Parent::
   1.994 +                          operator[](static_cast<Parent&>(*this)).next);
   1.995 +        return *this;
   1.996 +      }
   1.997 +
   1.998 +    private:
   1.999 +      const IterableIntMap* _map;
  1.1000 +    };
  1.1001 +
  1.1002 +  protected:
  1.1003 +
  1.1004 +    virtual void erase(const Key& key) {
  1.1005 +      unlace(key);
  1.1006 +      Parent::erase(key);
  1.1007 +    }
  1.1008 +
  1.1009 +    virtual void erase(const std::vector<Key>& keys) {
  1.1010 +      for (int i = 0; i < int(keys.size()); ++i) {
  1.1011 +        unlace(keys[i]);
  1.1012 +      }
  1.1013 +      Parent::erase(keys);
  1.1014 +    }
  1.1015 +
  1.1016 +    virtual void clear() {
  1.1017 +      _first.clear();
  1.1018 +      Parent::clear();
  1.1019 +    }
  1.1020 +
  1.1021 +  private:
  1.1022 +    std::vector<Key> _first;
  1.1023 +  };
  1.1024 +
  1.1025 +  namespace _maps_bits {
  1.1026 +    template <typename Item, typename Value>
  1.1027 +    struct IterableValueMapNode {
  1.1028 +      IterableValueMapNode(Value _value = Value()) : value(_value) {}
  1.1029 +      Item prev, next;
  1.1030 +      Value value;
  1.1031 +    };
  1.1032 +  }
  1.1033 +
  1.1034 +  /// \brief Dynamic iterable map for comparable values.
  1.1035 +  ///
  1.1036 +  /// This class provides a special graph map type which can store a
  1.1037 +  /// comparable value for graph items (\c Node, \c Arc or \c Edge).
  1.1038 +  /// For each value it is possible to iterate on the keys mapped to
  1.1039 +  /// the value (\c ItemIt), and the values of the map can be accessed
  1.1040 +  /// with an STL compatible forward iterator (\c ValueIt).
  1.1041 +  /// The map stores a linked list for each value, which contains
  1.1042 +  /// the items mapped to the value, and the used values are stored
  1.1043 +  /// in balanced binary tree (\c std::map).
  1.1044 +  ///
  1.1045 +  /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
  1.1046 +  /// specialized for \c bool and \c int values, respectively.
  1.1047 +  ///
  1.1048 +  /// This type is not reference map, so it cannot be modified with
  1.1049 +  /// the subscript operator.
  1.1050 +  ///
  1.1051 +  /// \tparam GR The graph type.
  1.1052 +  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  1.1053 +  /// \c GR::Edge).
  1.1054 +  /// \tparam V The value type of the map. It can be any comparable
  1.1055 +  /// value type.
  1.1056 +  ///
  1.1057 +  /// \see IterableBoolMap, IterableIntMap
  1.1058 +  /// \see CrossRefMap
  1.1059 +  template <typename GR, typename K, typename V>
  1.1060 +  class IterableValueMap
  1.1061 +    : protected ItemSetTraits<GR, K>::
  1.1062 +        template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
  1.1063 +  public:
  1.1064 +    typedef typename ItemSetTraits<GR, K>::
  1.1065 +      template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
  1.1066 +
  1.1067 +    /// The key type
  1.1068 +    typedef K Key;
  1.1069 +    /// The value type
  1.1070 +    typedef V Value;
  1.1071 +    /// The graph type
  1.1072 +    typedef GR Graph;
  1.1073 +
  1.1074 +  public:
  1.1075 +
  1.1076 +    /// \brief Constructor of the map with a given value.
  1.1077 +    ///
  1.1078 +    /// Constructor of the map with a given value.
  1.1079 +    explicit IterableValueMap(const Graph& graph,
  1.1080 +                              const Value& value = Value())
  1.1081 +      : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
  1.1082 +      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  1.1083 +        lace(it);
  1.1084 +      }
  1.1085 +    }
  1.1086 +
  1.1087 +  protected:
  1.1088 +
  1.1089 +    void unlace(const Key& key) {
  1.1090 +      typename Parent::Value& node = Parent::operator[](key);
  1.1091 +      if (node.prev != INVALID) {
  1.1092 +        Parent::operator[](node.prev).next = node.next;
  1.1093 +      } else {
  1.1094 +        if (node.next != INVALID) {
  1.1095 +          _first[node.value] = node.next;
  1.1096 +        } else {
  1.1097 +          _first.erase(node.value);
  1.1098 +        }
  1.1099 +      }
  1.1100 +      if (node.next != INVALID) {
  1.1101 +        Parent::operator[](node.next).prev = node.prev;
  1.1102 +      }
  1.1103 +    }
  1.1104 +
  1.1105 +    void lace(const Key& key) {
  1.1106 +      typename Parent::Value& node = Parent::operator[](key);
  1.1107 +      typename std::map<Value, Key>::iterator it = _first.find(node.value);
  1.1108 +      if (it == _first.end()) {
  1.1109 +        node.prev = node.next = INVALID;
  1.1110 +        _first.insert(std::make_pair(node.value, key));
  1.1111 +      } else {
  1.1112 +        node.prev = INVALID;
  1.1113 +        node.next = it->second;
  1.1114 +        if (node.next != INVALID) {
  1.1115 +          Parent::operator[](node.next).prev = key;
  1.1116 +        }
  1.1117 +        it->second = key;
  1.1118 +      }
  1.1119 +    }
  1.1120 +
  1.1121 +  public:
  1.1122 +
  1.1123 +    /// \brief Forward iterator for values.
  1.1124 +    ///
  1.1125 +    /// This iterator is an STL compatible forward
  1.1126 +    /// iterator on the values of the map. The values can
  1.1127 +    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
  1.1128 +    class ValueIt
  1.1129 +      : public std::iterator<std::forward_iterator_tag, Value> {
  1.1130 +      friend class IterableValueMap;
  1.1131 +    private:
  1.1132 +      ValueIt(typename std::map<Value, Key>::const_iterator _it)
  1.1133 +        : it(_it) {}
  1.1134 +    public:
  1.1135 +
  1.1136 +      /// Constructor
  1.1137 +      ValueIt() {}
  1.1138 +
  1.1139 +      /// \e
  1.1140 +      ValueIt& operator++() { ++it; return *this; }
  1.1141 +      /// \e
  1.1142 +      ValueIt operator++(int) {
  1.1143 +        ValueIt tmp(*this);
  1.1144 +        operator++();
  1.1145 +        return tmp;
  1.1146 +      }
  1.1147 +
  1.1148 +      /// \e
  1.1149 +      const Value& operator*() const { return it->first; }
  1.1150 +      /// \e
  1.1151 +      const Value* operator->() const { return &(it->first); }
  1.1152 +
  1.1153 +      /// \e
  1.1154 +      bool operator==(ValueIt jt) const { return it == jt.it; }
  1.1155 +      /// \e
  1.1156 +      bool operator!=(ValueIt jt) const { return it != jt.it; }
  1.1157 +
  1.1158 +    private:
  1.1159 +      typename std::map<Value, Key>::const_iterator it;
  1.1160 +    };
  1.1161 +
  1.1162 +    /// \brief Returns an iterator to the first value.
  1.1163 +    ///
  1.1164 +    /// Returns an STL compatible iterator to the
  1.1165 +    /// first value of the map. The values of the
  1.1166 +    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  1.1167 +    /// range.
  1.1168 +    ValueIt beginValue() const {
  1.1169 +      return ValueIt(_first.begin());
  1.1170 +    }
  1.1171 +
  1.1172 +    /// \brief Returns an iterator after the last value.
  1.1173 +    ///
  1.1174 +    /// Returns an STL compatible iterator after the
  1.1175 +    /// last value of the map. The values of the
  1.1176 +    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  1.1177 +    /// range.
  1.1178 +    ValueIt endValue() const {
  1.1179 +      return ValueIt(_first.end());
  1.1180 +    }
  1.1181 +
  1.1182 +    /// \brief Set operation of the map.
  1.1183 +    ///
  1.1184 +    /// Set operation of the map.
  1.1185 +    void set(const Key& key, const Value& value) {
  1.1186 +      unlace(key);
  1.1187 +      Parent::operator[](key).value = value;
  1.1188 +      lace(key);
  1.1189 +    }
  1.1190 +
  1.1191 +    /// \brief Const subscript operator of the map.
  1.1192 +    ///
  1.1193 +    /// Const subscript operator of the map.
  1.1194 +    const Value& operator[](const Key& key) const {
  1.1195 +      return Parent::operator[](key).value;
  1.1196 +    }
  1.1197 +
  1.1198 +    /// \brief Iterator for the keys with the same value.
  1.1199 +    ///
  1.1200 +    /// Iterator for the keys with the same value. It works
  1.1201 +    /// like a graph item iterator, it can be converted to
  1.1202 +    /// the item type of the map, incremented with \c ++ operator, and
  1.1203 +    /// if the iterator leaves the last valid item, it will be equal to
  1.1204 +    /// \c INVALID.
  1.1205 +    class ItemIt : public Key {
  1.1206 +    public:
  1.1207 +      typedef Key Parent;
  1.1208 +
  1.1209 +      /// \brief Invalid constructor \& conversion.
  1.1210 +      ///
  1.1211 +      /// This constructor initializes the iterator to be invalid.
  1.1212 +      /// \sa Invalid for more details.
  1.1213 +      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  1.1214 +
  1.1215 +      /// \brief Creates an iterator with a value.
  1.1216 +      ///
  1.1217 +      /// Creates an iterator with a value. It iterates on the
  1.1218 +      /// keys which have the given value.
  1.1219 +      /// \param map The IterableValueMap
  1.1220 +      /// \param value The value
  1.1221 +      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
  1.1222 +        typename std::map<Value, Key>::const_iterator it =
  1.1223 +          map._first.find(value);
  1.1224 +        if (it == map._first.end()) {
  1.1225 +          Parent::operator=(INVALID);
  1.1226 +        } else {
  1.1227 +          Parent::operator=(it->second);
  1.1228 +        }
  1.1229 +      }
  1.1230 +
  1.1231 +      /// \brief Increment operator.
  1.1232 +      ///
  1.1233 +      /// Increment Operator.
  1.1234 +      ItemIt& operator++() {
  1.1235 +        Parent::operator=(_map->IterableValueMap::Parent::
  1.1236 +                          operator[](static_cast<Parent&>(*this)).next);
  1.1237 +        return *this;
  1.1238 +      }
  1.1239 +
  1.1240 +
  1.1241 +    private:
  1.1242 +      const IterableValueMap* _map;
  1.1243 +    };
  1.1244 +
  1.1245 +  protected:
  1.1246 +
  1.1247 +    virtual void add(const Key& key) {
  1.1248 +      Parent::add(key);
  1.1249 +      unlace(key);
  1.1250 +    }
  1.1251 +
  1.1252 +    virtual void add(const std::vector<Key>& keys) {
  1.1253 +      Parent::add(keys);
  1.1254 +      for (int i = 0; i < int(keys.size()); ++i) {
  1.1255 +        lace(keys[i]);
  1.1256 +      }
  1.1257 +    }
  1.1258 +
  1.1259 +    virtual void erase(const Key& key) {
  1.1260 +      unlace(key);
  1.1261 +      Parent::erase(key);
  1.1262 +    }
  1.1263 +
  1.1264 +    virtual void erase(const std::vector<Key>& keys) {
  1.1265 +      for (int i = 0; i < int(keys.size()); ++i) {
  1.1266 +        unlace(keys[i]);
  1.1267 +      }
  1.1268 +      Parent::erase(keys);
  1.1269 +    }
  1.1270 +
  1.1271 +    virtual void build() {
  1.1272 +      Parent::build();
  1.1273 +      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  1.1274 +        lace(it);
  1.1275 +      }
  1.1276 +    }
  1.1277 +
  1.1278 +    virtual void clear() {
  1.1279 +      _first.clear();
  1.1280 +      Parent::clear();
  1.1281 +    }
  1.1282 +
  1.1283 +  private:
  1.1284 +    std::map<Value, Key> _first;
  1.1285 +  };
  1.1286 +
  1.1287    /// \brief Map of the source nodes of arcs in a digraph.
  1.1288    ///
  1.1289    /// SourceMap provides access for the source node of each arc in a digraph,
  1.1290 @@ -2340,9 +3300,9 @@
  1.1291    class SourceMap {
  1.1292    public:
  1.1293  
  1.1294 -    ///\e
  1.1295 +    /// The key type (the \c Arc type of the digraph).
  1.1296      typedef typename GR::Arc Key;
  1.1297 -    ///\e
  1.1298 +    /// The value type (the \c Node type of the digraph).
  1.1299      typedef typename GR::Node Value;
  1.1300  
  1.1301      /// \brief Constructor
  1.1302 @@ -2381,9 +3341,9 @@
  1.1303    class TargetMap {
  1.1304    public:
  1.1305  
  1.1306 -    ///\e
  1.1307 +    /// The key type (the \c Arc type of the digraph).
  1.1308      typedef typename GR::Arc Key;
  1.1309 -    ///\e
  1.1310 +    /// The value type (the \c Node type of the digraph).
  1.1311      typedef typename GR::Node Value;
  1.1312  
  1.1313      /// \brief Constructor
  1.1314 @@ -2423,8 +3383,10 @@
  1.1315    class ForwardMap {
  1.1316    public:
  1.1317  
  1.1318 +    /// The key type (the \c Edge type of the digraph).
  1.1319 +    typedef typename GR::Edge Key;
  1.1320 +    /// The value type (the \c Arc type of the digraph).
  1.1321      typedef typename GR::Arc Value;
  1.1322 -    typedef typename GR::Edge Key;
  1.1323  
  1.1324      /// \brief Constructor
  1.1325      ///
  1.1326 @@ -2463,8 +3425,10 @@
  1.1327    class BackwardMap {
  1.1328    public:
  1.1329  
  1.1330 +    /// The key type (the \c Edge type of the digraph).
  1.1331 +    typedef typename GR::Edge Key;
  1.1332 +    /// The value type (the \c Arc type of the digraph).
  1.1333      typedef typename GR::Arc Value;
  1.1334 -    typedef typename GR::Edge Key;
  1.1335  
  1.1336      /// \brief Constructor
  1.1337      ///
  1.1338 @@ -2499,7 +3463,7 @@
  1.1339    /// in constant time. On the other hand, the values are updated automatically
  1.1340    /// whenever the digraph changes.
  1.1341    ///
  1.1342 -  /// \warning Besides \c addNode() and \c addArc(), a digraph structure 
  1.1343 +  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
  1.1344    /// may provide alternative ways to modify the digraph.
  1.1345    /// The correct behavior of InDegMap is not guarantied if these additional
  1.1346    /// features are used. For example the functions
  1.1347 @@ -2515,7 +3479,7 @@
  1.1348        ::ItemNotifier::ObserverBase {
  1.1349  
  1.1350    public:
  1.1351 -    
  1.1352 +
  1.1353      /// The graph type of InDegMap
  1.1354      typedef GR Graph;
  1.1355      typedef GR Digraph;
  1.1356 @@ -2629,7 +3593,7 @@
  1.1357    /// in constant time. On the other hand, the values are updated automatically
  1.1358    /// whenever the digraph changes.
  1.1359    ///
  1.1360 -  /// \warning Besides \c addNode() and \c addArc(), a digraph structure 
  1.1361 +  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
  1.1362    /// may provide alternative ways to modify the digraph.
  1.1363    /// The correct behavior of OutDegMap is not guarantied if these additional
  1.1364    /// features are used. For example the functions