lemon/maps.h
changeset 694 71939d63ae77
parent 693 7bda7860e0a8
child 695 8dae88c5943e
child 721 99124ea4f048
equal deleted inserted replaced
34:07d291aa7992 35:67ca3355d562
    20 #define LEMON_MAPS_H
    20 #define LEMON_MAPS_H
    21 
    21 
    22 #include <iterator>
    22 #include <iterator>
    23 #include <functional>
    23 #include <functional>
    24 #include <vector>
    24 #include <vector>
       
    25 #include <map>
    25 
    26 
    26 #include <lemon/core.h>
    27 #include <lemon/core.h>
    27 #include <lemon/smart_graph.h>
       
    28 
    28 
    29 ///\file
    29 ///\file
    30 ///\ingroup maps
    30 ///\ingroup maps
    31 ///\brief Miscellaneous property maps
    31 ///\brief Miscellaneous property maps
    32 
       
    33 #include <map>
       
    34 
    32 
    35 namespace lemon {
    33 namespace lemon {
    36 
    34 
    37   /// \addtogroup maps
    35   /// \addtogroup maps
    38   /// @{
    36   /// @{
  1904 
  1902 
  1905   /// This class provides simple invertable graph maps.
  1903   /// This class provides simple invertable graph maps.
  1906   /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
  1904   /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
  1907   /// and if a key is set to a new value then store it
  1905   /// and if a key is set to a new value then store it
  1908   /// in the inverse map.
  1906   /// in the inverse map.
  1909   ///
       
  1910   /// The values of the map can be accessed
  1907   /// The values of the map can be accessed
  1911   /// with stl compatible forward iterator.
  1908   /// with stl compatible forward iterator.
       
  1909   ///
       
  1910   /// This type is not reference map, so it cannot be modified with
       
  1911   /// the subscription operator.
  1912   ///
  1912   ///
  1913   /// \tparam GR The graph type.
  1913   /// \tparam GR The graph type.
  1914   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  1914   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  1915   /// \c GR::Edge).
  1915   /// \c GR::Edge).
  1916   /// \tparam V The value type of the map.
  1916   /// \tparam V The value type of the map.
  2310     const InverseMap inverse() const {
  2310     const InverseMap inverse() const {
  2311       return InverseMap(*this);
  2311       return InverseMap(*this);
  2312     }
  2312     }
  2313   };
  2313   };
  2314 
  2314 
  2315   /// \brief Dynamic iterable bool map.
  2315   /// \brief Dynamic iterable \c bool map.
  2316   ///
  2316   ///
  2317   /// This class provides a special graph map type which can store for
  2317   /// This class provides a special graph map type which can store a
  2318   /// each graph item(node, arc, edge, etc.) a bool value. For both
  2318   /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
  2319   /// the true and the false values it is possible to iterate on the
  2319   /// For both \c true and \c false values it is possible to iterate on
  2320   /// keys.
  2320   /// the keys.
  2321   ///
  2321   ///
  2322   /// \param GR The graph type.
  2322   /// This type is a reference map, so it can be modified with the
  2323   /// \param ITEM One of the graph's item types, the key of the map.
  2323   /// subscription operator.
  2324   template <typename GR, typename ITEM>
  2324   ///
       
  2325   /// \tparam GR The graph type.
       
  2326   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
       
  2327   /// \c GR::Edge).
       
  2328   ///
       
  2329   /// \see IterableIntMap, IterableValueMap
       
  2330   /// \see CrossRefMap
       
  2331   template <typename GR, typename K>
  2325   class IterableBoolMap
  2332   class IterableBoolMap
  2326     : protected ItemSetTraits<GR, ITEM>::template Map<int>::Type {
  2333     : protected ItemSetTraits<GR, K>::template Map<int>::Type {
  2327   private:
  2334   private:
  2328     typedef GR Graph;
  2335     typedef GR Graph;
  2329 
  2336 
  2330     typedef typename ItemSetTraits<Graph, ITEM>::ItemIt KeyIt;
  2337     typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
  2331     typedef typename ItemSetTraits<GR, ITEM>::template Map<int>::Type Parent;
  2338     typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
  2332 
  2339 
  2333     std::vector<ITEM> _array;
  2340     std::vector<K> _array;
  2334     int _sep;
  2341     int _sep;
  2335 
  2342 
  2336   public:
  2343   public:
  2337 
  2344 
  2338     /// Indicates that the map if reference map.
  2345     /// Indicates that the map is reference map.
  2339     typedef True ReferenceMapTag;
  2346     typedef True ReferenceMapTag;
  2340 
  2347 
  2341     /// The key type
  2348     /// The key type
  2342     typedef ITEM Key;
  2349     typedef K Key;
  2343     /// The value type
  2350     /// The value type
  2344     typedef bool Value;
  2351     typedef bool Value;
  2345     /// The const reference type.
  2352     /// The const reference type.
  2346     typedef const Value& ConstReference;
  2353     typedef const Value& ConstReference;
  2347 
  2354 
  2351       return Parent::operator[](key);
  2358       return Parent::operator[](key);
  2352     }
  2359     }
  2353 
  2360 
  2354   public:
  2361   public:
  2355 
  2362 
  2356     /// \brief Refernce to the value of the map.
  2363     /// \brief Reference to the value of the map.
  2357     ///
  2364     ///
  2358     /// This class is similar to the bool type. It can be converted to
  2365     /// This class is similar to the \c bool type. It can be converted to
  2359     /// bool and it provides the same operators.
  2366     /// \c bool and it provides the same operators.
  2360     class Reference {
  2367     class Reference {
  2361       friend class IterableBoolMap;
  2368       friend class IterableBoolMap;
  2362     private:
  2369     private:
  2363       Reference(IterableBoolMap& map, const Key& key)
  2370       Reference(IterableBoolMap& map, const Key& key)
  2364         : _key(key), _map(map) {}
  2371         : _key(key), _map(map) {}
  2452     /// \note Constant time operation.
  2459     /// \note Constant time operation.
  2453     void setAll(bool value) {
  2460     void setAll(bool value) {
  2454       _sep = (value ? _array.size() : 0);
  2461       _sep = (value ? _array.size() : 0);
  2455     }
  2462     }
  2456 
  2463 
  2457     /// \brief Returns the number of the keys mapped to true.
  2464     /// \brief Returns the number of the keys mapped to \c true.
  2458     ///
  2465     ///
  2459     /// Returns the number of the keys mapped to true.
  2466     /// Returns the number of the keys mapped to \c true.
  2460     int trueNum() const {
  2467     int trueNum() const {
  2461       return _sep;
  2468       return _sep;
  2462     }
  2469     }
  2463 
  2470 
  2464     /// \brief Returns the number of the keys mapped to false.
  2471     /// \brief Returns the number of the keys mapped to \c false.
  2465     ///
  2472     ///
  2466     /// Returns the number of the keys mapped to false.
  2473     /// Returns the number of the keys mapped to \c false.
  2467     int falseNum() const {
  2474     int falseNum() const {
  2468       return _array.size() - _sep;
  2475       return _array.size() - _sep;
  2469     }
  2476     }
  2470 
  2477 
  2471     /// \brief Iterator for the keys mapped to true.
  2478     /// \brief Iterator for the keys mapped to \c true.
  2472     ///
  2479     ///
  2473     /// Iterator for the keys mapped to true. It works
  2480     /// Iterator for the keys mapped to \c true. It works
  2474     /// like a graph item iterator in the map, it can be converted
  2481     /// like a graph item iterator, it can be converted to
  2475     /// the key type of the map, incremented with \c ++ operator, and
  2482     /// the key type of the map, incremented with \c ++ operator, and
  2476     /// if the iterator leave the last valid key it will be equal to
  2483     /// if the iterator leaves the last valid key, it will be equal to
  2477     /// \c INVALID.
  2484     /// \c INVALID.
  2478     class TrueIt : public Key {
  2485     class TrueIt : public Key {
  2479     public:
  2486     public:
  2480       typedef Key Parent;
  2487       typedef Key Parent;
  2481 
  2488 
  2482       /// \brief Creates an iterator.
  2489       /// \brief Creates an iterator.
  2483       ///
  2490       ///
  2484       /// Creates an iterator. It iterates on the
  2491       /// Creates an iterator. It iterates on the
  2485       /// keys which mapped to true.
  2492       /// keys mapped to \c true.
  2486       /// \param map The IterableIntMap
  2493       /// \param map The IterableBoolMap.
  2487       explicit TrueIt(const IterableBoolMap& map)
  2494       explicit TrueIt(const IterableBoolMap& map)
  2488         : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
  2495         : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
  2489           _map(&map) {}
  2496           _map(&map) {}
  2490 
  2497 
  2491       /// \brief Invalid constructor \& conversion.
  2498       /// \brief Invalid constructor \& conversion.
  2492       ///
  2499       ///
  2493       /// This constructor initializes the key to be invalid.
  2500       /// This constructor initializes the iterator to be invalid.
  2494       /// \sa Invalid for more details.
  2501       /// \sa Invalid for more details.
  2495       TrueIt(Invalid) : Parent(INVALID), _map(0) {}
  2502       TrueIt(Invalid) : Parent(INVALID), _map(0) {}
  2496 
  2503 
  2497       /// \brief Increment operator.
  2504       /// \brief Increment operator.
  2498       ///
  2505       ///
  2499       /// Increment Operator.
  2506       /// Increment operator.
  2500       TrueIt& operator++() {
  2507       TrueIt& operator++() {
  2501         int pos = _map->position(*this);
  2508         int pos = _map->position(*this);
  2502         Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
  2509         Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
  2503         return *this;
  2510         return *this;
  2504       }
  2511       }
  2505 
  2512 
  2506 
       
  2507     private:
  2513     private:
  2508       const IterableBoolMap* _map;
  2514       const IterableBoolMap* _map;
  2509     };
  2515     };
  2510 
  2516 
  2511     /// \brief Iterator for the keys mapped to false.
  2517     /// \brief Iterator for the keys mapped to \c false.
  2512     ///
  2518     ///
  2513     /// Iterator for the keys mapped to false. It works
  2519     /// Iterator for the keys mapped to \c false. It works
  2514     /// like a graph item iterator in the map, it can be converted
  2520     /// like a graph item iterator, it can be converted to
  2515     /// the key type of the map, incremented with \c ++ operator, and
  2521     /// the key type of the map, incremented with \c ++ operator, and
  2516     /// if the iterator leave the last valid key it will be equal to
  2522     /// if the iterator leaves the last valid key, it will be equal to
  2517     /// \c INVALID.
  2523     /// \c INVALID.
  2518     class FalseIt : public Key {
  2524     class FalseIt : public Key {
  2519     public:
  2525     public:
  2520       typedef Key Parent;
  2526       typedef Key Parent;
  2521 
  2527 
  2522       /// \brief Creates an iterator.
  2528       /// \brief Creates an iterator.
  2523       ///
  2529       ///
  2524       /// Creates an iterator. It iterates on the
  2530       /// Creates an iterator. It iterates on the
  2525       /// keys which mapped to false.
  2531       /// keys mapped to \c false.
  2526       /// \param map The IterableIntMap
  2532       /// \param map The IterableBoolMap.
  2527       explicit FalseIt(const IterableBoolMap& map)
  2533       explicit FalseIt(const IterableBoolMap& map)
  2528         : Parent(map._sep < int(map._array.size()) ?
  2534         : Parent(map._sep < int(map._array.size()) ?
  2529                  map._array.back() : INVALID), _map(&map) {}
  2535                  map._array.back() : INVALID), _map(&map) {}
  2530 
  2536 
  2531       /// \brief Invalid constructor \& conversion.
  2537       /// \brief Invalid constructor \& conversion.
  2532       ///
  2538       ///
  2533       /// This constructor initializes the key to be invalid.
  2539       /// This constructor initializes the iterator to be invalid.
  2534       /// \sa Invalid for more details.
  2540       /// \sa Invalid for more details.
  2535       FalseIt(Invalid) : Parent(INVALID), _map(0) {}
  2541       FalseIt(Invalid) : Parent(INVALID), _map(0) {}
  2536 
  2542 
  2537       /// \brief Increment operator.
  2543       /// \brief Increment operator.
  2538       ///
  2544       ///
  2539       /// Increment Operator.
  2545       /// Increment operator.
  2540       FalseIt& operator++() {
  2546       FalseIt& operator++() {
  2541         int pos = _map->position(*this);
  2547         int pos = _map->position(*this);
  2542         Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
  2548         Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
  2543         return *this;
  2549         return *this;
  2544       }
  2550       }
  2548     };
  2554     };
  2549 
  2555 
  2550     /// \brief Iterator for the keys mapped to a given value.
  2556     /// \brief Iterator for the keys mapped to a given value.
  2551     ///
  2557     ///
  2552     /// Iterator for the keys mapped to a given value. It works
  2558     /// Iterator for the keys mapped to a given value. It works
  2553     /// like a graph item iterator in the map, it can be converted
  2559     /// like a graph item iterator, it can be converted to
  2554     /// the key type of the map, incremented with \c ++ operator, and
  2560     /// the key type of the map, incremented with \c ++ operator, and
  2555     /// if the iterator leave the last valid key it will be equal to
  2561     /// if the iterator leaves the last valid key, it will be equal to
  2556     /// \c INVALID.
  2562     /// \c INVALID.
  2557     class ItemIt : public Key {
  2563     class ItemIt : public Key {
  2558     public:
  2564     public:
  2559       typedef Key Parent;
  2565       typedef Key Parent;
  2560 
  2566 
  2561       /// \brief Creates an iterator.
  2567       /// \brief Creates an iterator with a value.
  2562       ///
  2568       ///
  2563       /// Creates an iterator. It iterates on the
  2569       /// Creates an iterator with a value. It iterates on the
  2564       /// keys which mapped to false.
  2570       /// keys mapped to the given value.
  2565       /// \param map The IterableIntMap
  2571       /// \param map The IterableBoolMap.
  2566       /// \param value Which elements should be iterated.
  2572       /// \param value The value.
  2567       ItemIt(const IterableBoolMap& map, bool value)
  2573       ItemIt(const IterableBoolMap& map, bool value)
  2568         : Parent(value ? 
  2574         : Parent(value ? 
  2569                  (map._sep > 0 ?
  2575                  (map._sep > 0 ?
  2570                   map._array[map._sep - 1] : INVALID) :
  2576                   map._array[map._sep - 1] : INVALID) :
  2571                  (map._sep < int(map._array.size()) ?
  2577                  (map._sep < int(map._array.size()) ?
  2572                   map._array.back() : INVALID)), _map(&map) {}
  2578                   map._array.back() : INVALID)), _map(&map) {}
  2573 
  2579 
  2574       /// \brief Invalid constructor \& conversion.
  2580       /// \brief Invalid constructor \& conversion.
  2575       ///
  2581       ///
  2576       /// This constructor initializes the key to be invalid.
  2582       /// This constructor initializes the iterator to be invalid.
  2577       /// \sa Invalid for more details.
  2583       /// \sa Invalid for more details.
  2578       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  2584       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  2579 
  2585 
  2580       /// \brief Increment operator.
  2586       /// \brief Increment operator.
  2581       ///
  2587       ///
  2582       /// Increment Operator.
  2588       /// Increment operator.
  2583       ItemIt& operator++() {
  2589       ItemIt& operator++() {
  2584         int pos = _map->position(*this);
  2590         int pos = _map->position(*this);
  2585         int _sep = pos >= _map->_sep ? _map->_sep : 0;
  2591         int _sep = pos >= _map->_sep ? _map->_sep : 0;
  2586         Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
  2592         Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
  2587         return *this;
  2593         return *this;
  2671       Item prev, next;
  2677       Item prev, next;
  2672       int value;
  2678       int value;
  2673     };
  2679     };
  2674   }
  2680   }
  2675 
  2681 
  2676   ///\ingroup graph_maps
       
  2677   ///
       
  2678   /// \brief Dynamic iterable integer map.
  2682   /// \brief Dynamic iterable integer map.
  2679   ///
  2683   ///
  2680   /// This class provides a special graph map type which can store
  2684   /// This class provides a special graph map type which can store an
  2681   /// for each graph item(node, edge, etc.) an integer value. For each
  2685   /// integer value for graph items (\c Node, \c Arc or \c Edge).
  2682   /// non negative value it is possible to iterate on the keys which
  2686   /// For each non-negative value it is possible to iterate on the keys
  2683   /// mapped to the given value.
  2687   /// mapped to the value.
  2684   ///
  2688   ///
  2685   /// \note The size of the data structure depends on the highest
  2689   /// This type is a reference map, so it can be modified with the
       
  2690   /// subscription operator.
       
  2691   ///
       
  2692   /// \note The size of the data structure depends on the largest
  2686   /// value in the map.
  2693   /// value in the map.
  2687   ///
  2694   ///
  2688   /// \param GR The graph type.
  2695   /// \tparam GR The graph type.
  2689   /// \param ITEM One of the graph's item type, the key of the map.
  2696   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  2690   template <typename GR, typename ITEM>
  2697   /// \c GR::Edge).
       
  2698   ///
       
  2699   /// \see IterableBoolMap, IterableValueMap
       
  2700   /// \see CrossRefMap
       
  2701   template <typename GR, typename K>
  2691   class IterableIntMap
  2702   class IterableIntMap
  2692     : protected ItemSetTraits<GR, ITEM>::
  2703     : protected ItemSetTraits<GR, K>::
  2693         template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type {
  2704         template Map<_maps_bits::IterableIntMapNode<K> >::Type {
  2694   public:
  2705   public:
  2695     typedef typename ItemSetTraits<GR, ITEM>::
  2706     typedef typename ItemSetTraits<GR, K>::
  2696       template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type Parent;
  2707       template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
  2697 
  2708 
  2698     /// The key type
  2709     /// The key type
  2699     typedef ITEM Key;
  2710     typedef K Key;
  2700     /// The value type
  2711     /// The value type
  2701     typedef int Value;
  2712     typedef int Value;
  2702     /// The graph type
  2713     /// The graph type
  2703     typedef GR Graph;
  2714     typedef GR Graph;
  2704 
  2715 
  2705     /// \brief Constructor of the map.
  2716     /// \brief Constructor of the map.
  2706     ///
  2717     ///
  2707     /// Constructor of the map. It set all values to -1.
  2718     /// Constructor of the map. It sets all values to -1.
  2708     explicit IterableIntMap(const Graph& graph)
  2719     explicit IterableIntMap(const Graph& graph)
  2709       : Parent(graph) {}
  2720       : Parent(graph) {}
  2710 
  2721 
  2711     /// \brief Constructor of the map with a given value.
  2722     /// \brief Constructor of the map with a given value.
  2712     ///
  2723     ///
  2713     /// Constructor of the map with a given value.
  2724     /// Constructor of the map with a given value.
  2714     explicit IterableIntMap(const Graph& graph, int value)
  2725     explicit IterableIntMap(const Graph& graph, int value)
  2715       : Parent(graph, _maps_bits::IterableIntMapNode<ITEM>(value)) {
  2726       : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
  2716       if (value >= 0) {
  2727       if (value >= 0) {
  2717         for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  2728         for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  2718           lace(it);
  2729           lace(it);
  2719         }
  2730         }
  2720       }
  2731       }
  2752       _first[node.value] = key;
  2763       _first[node.value] = key;
  2753     }
  2764     }
  2754 
  2765 
  2755   public:
  2766   public:
  2756 
  2767 
  2757     /// Indicates that the map if reference map.
  2768     /// Indicates that the map is reference map.
  2758     typedef True ReferenceMapTag;
  2769     typedef True ReferenceMapTag;
  2759 
  2770 
  2760     /// \brief Refernce to the value of the map.
  2771     /// \brief Reference to the value of the map.
  2761     ///
  2772     ///
  2762     /// This class is similar to the int type. It can
  2773     /// This class is similar to the \c int type. It can
  2763     /// be converted to int and it has the same operators.
  2774     /// be converted to \c int and it has the same operators.
  2764     class Reference {
  2775     class Reference {
  2765       friend class IterableIntMap;
  2776       friend class IterableIntMap;
  2766     private:
  2777     private:
  2767       Reference(IterableIntMap& map, const Key& key)
  2778       Reference(IterableIntMap& map, const Key& key)
  2768         : _key(key), _map(map) {}
  2779         : _key(key), _map(map) {}
  2879     }
  2890     }
  2880 
  2891 
  2881     /// \brief Iterator for the keys with the same value.
  2892     /// \brief Iterator for the keys with the same value.
  2882     ///
  2893     ///
  2883     /// Iterator for the keys with the same value. It works
  2894     /// Iterator for the keys with the same value. It works
  2884     /// like a graph item iterator in the map, it can be converted
  2895     /// like a graph item iterator, it can be converted to
  2885     /// the item type of the map, incremented with \c ++ operator, and
  2896     /// the item type of the map, incremented with \c ++ operator, and
  2886     /// if the iterator leave the last valid item it will be equal to
  2897     /// if the iterator leaves the last valid item, it will be equal to
  2887     /// \c INVALID.
  2898     /// \c INVALID.
  2888     class ItemIt : public ITEM {
  2899     class ItemIt : public Key {
  2889     public:
  2900     public:
  2890       typedef ITEM Parent;
  2901       typedef Key Parent;
  2891 
  2902 
  2892       /// \brief Invalid constructor \& conversion.
  2903       /// \brief Invalid constructor \& conversion.
  2893       ///
  2904       ///
  2894       /// This constructor initializes the item to be invalid.
  2905       /// This constructor initializes the iterator to be invalid.
  2895       /// \sa Invalid for more details.
  2906       /// \sa Invalid for more details.
  2896       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  2907       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  2897 
  2908 
  2898       /// \brief Creates an iterator with a value.
  2909       /// \brief Creates an iterator with a value.
  2899       ///
  2910       ///
  2900       /// Creates an iterator with a value. It iterates on the
  2911       /// Creates an iterator with a value. It iterates on the
  2901       /// keys which have the given value.
  2912       /// keys mapped to the given value.
  2902       /// \param map The IterableIntMap
  2913       /// \param map The IterableIntMap.
  2903       /// \param value The value
  2914       /// \param value The value.
  2904       ItemIt(const IterableIntMap& map, int value) : _map(&map) {
  2915       ItemIt(const IterableIntMap& map, int value) : _map(&map) {
  2905         if (value < 0 || value >= int(_map->_first.size())) {
  2916         if (value < 0 || value >= int(_map->_first.size())) {
  2906           Parent::operator=(INVALID);
  2917           Parent::operator=(INVALID);
  2907         } else {
  2918         } else {
  2908           Parent::operator=(_map->_first[value]);
  2919           Parent::operator=(_map->_first[value]);
  2909         }
  2920         }
  2910       }
  2921       }
  2911 
  2922 
  2912       /// \brief Increment operator.
  2923       /// \brief Increment operator.
  2913       ///
  2924       ///
  2914       /// Increment Operator.
  2925       /// Increment operator.
  2915       ItemIt& operator++() {
  2926       ItemIt& operator++() {
  2916         Parent::operator=(_map->IterableIntMap::Parent::
  2927         Parent::operator=(_map->IterableIntMap::Parent::
  2917                           operator[](static_cast<Parent&>(*this)).next);
  2928                           operator[](static_cast<Parent&>(*this)).next);
  2918         return *this;
  2929         return *this;
  2919       }
  2930       }
  2920 
  2931 
  2921 
       
  2922     private:
  2932     private:
  2923       const IterableIntMap* _map;
  2933       const IterableIntMap* _map;
  2924     };
  2934     };
  2925 
  2935 
  2926   protected:
  2936   protected:
  2941       _first.clear();
  2951       _first.clear();
  2942       Parent::clear();
  2952       Parent::clear();
  2943     }
  2953     }
  2944 
  2954 
  2945   private:
  2955   private:
  2946     std::vector<ITEM> _first;
  2956     std::vector<Key> _first;
  2947   };
  2957   };
  2948 
  2958 
  2949   namespace _maps_bits {
  2959   namespace _maps_bits {
  2950     template <typename Item, typename Value>
  2960     template <typename Item, typename Value>
  2951     struct IterableValueMapNode {
  2961     struct IterableValueMapNode {
  2953       Item prev, next;
  2963       Item prev, next;
  2954       Value value;
  2964       Value value;
  2955     };
  2965     };
  2956   }
  2966   }
  2957 
  2967 
  2958   ///\ingroup graph_maps
       
  2959   ///
       
  2960   /// \brief Dynamic iterable map for comparable values.
  2968   /// \brief Dynamic iterable map for comparable values.
  2961   ///
  2969   ///
  2962   /// This class provides a special graph map type which can store
  2970   /// This class provides a special graph map type which can store an
  2963   /// for each graph item(node, edge, etc.) a value. For each
  2971   /// comparable value for graph items (\c Node, \c Arc or \c Edge).
  2964   /// value it is possible to iterate on the keys which mapped to the
  2972   /// For each value it is possible to iterate on the keys mapped to
  2965   /// given value. The type stores for each value a linked list with
  2973   /// the value.
       
  2974   ///
       
  2975   /// The map stores for each value a linked list with
  2966   /// the items which mapped to the value, and the values are stored
  2976   /// the items which mapped to the value, and the values are stored
  2967   /// in balanced binary tree. The values of the map can be accessed
  2977   /// in balanced binary tree. The values of the map can be accessed
  2968   /// with stl compatible forward iterator.
  2978   /// with stl compatible forward iterator.
  2969   ///
  2979   ///
  2970   /// This type is not reference map so it cannot be modified with
  2980   /// This type is not reference map, so it cannot be modified with
  2971   /// the subscription operator.
  2981   /// the subscription operator.
  2972   ///
  2982   ///
  2973   /// \see InvertableMap
  2983   /// \tparam GR The graph type.
  2974   ///
  2984   /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  2975   /// \param GR The graph type.
  2985   /// \c GR::Edge).
  2976   /// \param ITEM One of the graph's item type, the key of the map.
  2986   /// \tparam V The value type of the map. It can be any comparable
  2977   /// \param VAL Any comparable value type.
  2987   /// value type.
  2978   template <typename GR, typename ITEM, typename VAL>
  2988   ///
       
  2989   /// \see IterableBoolMap, IterableIntMap
       
  2990   /// \see CrossRefMap
       
  2991   template <typename GR, typename K, typename V>
  2979   class IterableValueMap
  2992   class IterableValueMap
  2980     : protected ItemSetTraits<GR, ITEM>::
  2993     : protected ItemSetTraits<GR, K>::
  2981         template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type {
  2994         template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
  2982   public:
  2995   public:
  2983     typedef typename ItemSetTraits<GR, ITEM>::
  2996     typedef typename ItemSetTraits<GR, K>::
  2984       template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type Parent;
  2997       template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
  2985 
  2998 
  2986     /// The key type
  2999     /// The key type
  2987     typedef ITEM Key;
  3000     typedef K Key;
  2988     /// The value type
  3001     /// The value type
  2989     typedef VAL Value;
  3002     typedef V Value;
  2990     /// The graph type
  3003     /// The graph type
  2991     typedef GR Graph;
  3004     typedef GR Graph;
  2992 
  3005 
  2993   public:
  3006   public:
  2994 
  3007 
  2995     /// \brief Constructor of the Map with a given value.
  3008     /// \brief Constructor of the map with a given value.
  2996     ///
  3009     ///
  2997     /// Constructor of the Map with a given value.
  3010     /// Constructor of the map with a given value.
  2998     explicit IterableValueMap(const Graph& graph,
  3011     explicit IterableValueMap(const Graph& graph,
  2999                               const Value& value = Value())
  3012                               const Value& value = Value())
  3000       : Parent(graph, _maps_bits::IterableValueMapNode<ITEM, VAL>(value)) {
  3013       : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
  3001       for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  3014       for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
  3002         lace(it);
  3015         lace(it);
  3003       }
  3016       }
  3004     }
  3017     }
  3005 
  3018 
  3024     void lace(const Key& key) {
  3037     void lace(const Key& key) {
  3025       typename Parent::Value& node = Parent::operator[](key);
  3038       typename Parent::Value& node = Parent::operator[](key);
  3026       typename std::map<Value, Key>::iterator it = _first.find(node.value);
  3039       typename std::map<Value, Key>::iterator it = _first.find(node.value);
  3027       if (it == _first.end()) {
  3040       if (it == _first.end()) {
  3028         node.prev = node.next = INVALID;
  3041         node.prev = node.next = INVALID;
  3029         if (node.next != INVALID) {
       
  3030           Parent::operator[](node.next).prev = key;
       
  3031         }
       
  3032         _first.insert(std::make_pair(node.value, key));
  3042         _first.insert(std::make_pair(node.value, key));
  3033       } else {
  3043       } else {
  3034         node.prev = INVALID;
  3044         node.prev = INVALID;
  3035         node.next = it->second;
  3045         node.next = it->second;
  3036         if (node.next != INVALID) {
  3046         if (node.next != INVALID) {
  3044 
  3054 
  3045     /// \brief Forward iterator for values.
  3055     /// \brief Forward iterator for values.
  3046     ///
  3056     ///
  3047     /// This iterator is an stl compatible forward
  3057     /// This iterator is an stl compatible forward
  3048     /// iterator on the values of the map. The values can
  3058     /// iterator on the values of the map. The values can
  3049     /// be accessed in the [beginValue, endValue) range.
  3059     /// be accessed in the <tt>[beginValue, endValue)</tt> range.
  3050     ///
       
  3051     class ValueIterator
  3060     class ValueIterator
  3052       : public std::iterator<std::forward_iterator_tag, Value> {
  3061       : public std::iterator<std::forward_iterator_tag, Value> {
  3053       friend class IterableValueMap;
  3062       friend class IterableValueMap;
  3054     private:
  3063     private:
  3055       ValueIterator(typename std::map<Value, Key>::const_iterator _it)
  3064       ValueIterator(typename std::map<Value, Key>::const_iterator _it)
  3077 
  3086 
  3078     /// \brief Returns an iterator to the first value.
  3087     /// \brief Returns an iterator to the first value.
  3079     ///
  3088     ///
  3080     /// Returns an stl compatible iterator to the
  3089     /// Returns an stl compatible iterator to the
  3081     /// first value of the map. The values of the
  3090     /// first value of the map. The values of the
  3082     /// map can be accessed in the [beginValue, endValue)
  3091     /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  3083     /// range.
  3092     /// range.
  3084     ValueIterator beginValue() const {
  3093     ValueIterator beginValue() const {
  3085       return ValueIterator(_first.begin());
  3094       return ValueIterator(_first.begin());
  3086     }
  3095     }
  3087 
  3096 
  3088     /// \brief Returns an iterator after the last value.
  3097     /// \brief Returns an iterator after the last value.
  3089     ///
  3098     ///
  3090     /// Returns an stl compatible iterator after the
  3099     /// Returns an stl compatible iterator after the
  3091     /// last value of the map. The values of the
  3100     /// last value of the map. The values of the
  3092     /// map can be accessed in the [beginValue, endValue)
  3101     /// map can be accessed in the <tt>[beginValue, endValue)</tt>
  3093     /// range.
  3102     /// range.
  3094     ValueIterator endValue() const {
  3103     ValueIterator endValue() const {
  3095       return ValueIterator(_first.end());
  3104       return ValueIterator(_first.end());
  3096     }
  3105     }
  3097 
  3106 
  3112     }
  3121     }
  3113 
  3122 
  3114     /// \brief Iterator for the keys with the same value.
  3123     /// \brief Iterator for the keys with the same value.
  3115     ///
  3124     ///
  3116     /// Iterator for the keys with the same value. It works
  3125     /// Iterator for the keys with the same value. It works
  3117     /// like a graph item iterator in the map, it can be converted
  3126     /// like a graph item iterator, it can be converted to
  3118     /// the item type of the map, incremented with \c ++ operator, and
  3127     /// the item type of the map, incremented with \c ++ operator, and
  3119     /// if the iterator leave the last valid item it will be equal to
  3128     /// if the iterator leaves the last valid item, it will be equal to
  3120     /// \c INVALID.
  3129     /// \c INVALID.
  3121     class ItemIt : public ITEM {
  3130     class ItemIt : public Key {
  3122     public:
  3131     public:
  3123       typedef ITEM Parent;
  3132       typedef Key Parent;
  3124 
  3133 
  3125       /// \brief Invalid constructor \& conversion.
  3134       /// \brief Invalid constructor \& conversion.
  3126       ///
  3135       ///
  3127       /// This constructor initializes the item to be invalid.
  3136       /// This constructor initializes the iterator to be invalid.
  3128       /// \sa Invalid for more details.
  3137       /// \sa Invalid for more details.
  3129       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  3138       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
  3130 
  3139 
  3131       /// \brief Creates an iterator with a value.
  3140       /// \brief Creates an iterator with a value.
  3132       ///
  3141       ///