1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
27 #include <lemon/core.h>
28 #include <lemon/bits/stl_iterators.h>
32 ///\brief Miscellaneous property maps
39 /// Base class of maps.
41 /// Base class of maps. It provides the necessary type definitions
42 /// required by the map %concepts.
43 template<typename K, typename V>
46 /// \brief The key type of the map.
48 /// \brief The value type of the map.
49 /// (The type of objects associated with the keys).
54 /// Null map. (a.k.a. DoNothingMap)
56 /// This map can be used if you have to provide a map only for
57 /// its type definitions, or if you have to provide a writable map,
58 /// but data written to it is not required (i.e. it will be sent to
59 /// <tt>/dev/null</tt>).
60 /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
63 template<typename K, typename V>
64 class NullMap : public MapBase<K, V> {
71 /// Gives back a default constructed element.
72 Value operator[](const Key&) const { return Value(); }
73 /// Absorbs the value.
74 void set(const Key&, const Value&) {}
77 /// Returns a \c NullMap class
79 /// This function just returns a \c NullMap class.
81 template <typename K, typename V>
82 NullMap<K, V> nullMap() {
83 return NullMap<K, V>();
89 /// This \ref concepts::ReadMap "readable map" assigns a specified
90 /// value to each key.
92 /// In other aspects it is equivalent to \c NullMap.
93 /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
94 /// concept, but it absorbs the data written to it.
96 /// The simplest way of using this map is through the constMap()
101 template<typename K, typename V>
102 class ConstMap : public MapBase<K, V> {
111 /// Default constructor
113 /// Default constructor.
114 /// The value of the map will be default constructed.
117 /// Constructor with specified initial value
119 /// Constructor with specified initial value.
120 /// \param v The initial value of the map.
121 ConstMap(const Value &v) : _value(v) {}
123 /// Gives back the specified value.
124 Value operator[](const Key&) const { return _value; }
126 /// Absorbs the value.
127 void set(const Key&, const Value&) {}
129 /// Sets the value that is assigned to each key.
130 void setAll(const Value &v) {
134 template<typename V1>
135 ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
138 /// Returns a \c ConstMap class
140 /// This function just returns a \c ConstMap class.
141 /// \relates ConstMap
142 template<typename K, typename V>
143 inline ConstMap<K, V> constMap(const V &v) {
144 return ConstMap<K, V>(v);
147 template<typename K, typename V>
148 inline ConstMap<K, V> constMap() {
149 return ConstMap<K, V>();
153 template<typename T, T v>
156 /// Constant map with inlined constant value.
158 /// This \ref concepts::ReadMap "readable map" assigns a specified
159 /// value to each key.
161 /// In other aspects it is equivalent to \c NullMap.
162 /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
163 /// concept, but it absorbs the data written to it.
165 /// The simplest way of using this map is through the constMap()
170 template<typename K, typename V, V v>
171 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
181 /// Gives back the specified value.
182 Value operator[](const Key&) const { return v; }
184 /// Absorbs the value.
185 void set(const Key&, const Value&) {}
188 /// Returns a \c ConstMap class with inlined constant value
190 /// This function just returns a \c ConstMap class with inlined
192 /// \relates ConstMap
193 template<typename K, typename V, V v>
194 inline ConstMap<K, Const<V, v> > constMap() {
195 return ConstMap<K, Const<V, v> >();
201 /// This \ref concepts::ReadMap "read-only map" gives back the given
202 /// key as value without any modification.
205 template <typename T>
206 class IdentityMap : public MapBase<T, T> {
213 /// Gives back the given value without any modification.
214 Value operator[](const Key &k) const {
219 /// Returns an \c IdentityMap class
221 /// This function just returns an \c IdentityMap class.
222 /// \relates IdentityMap
224 inline IdentityMap<T> identityMap() {
225 return IdentityMap<T>();
229 /// \brief Map for storing values for integer keys from the range
230 /// <tt>[0..size-1]</tt>.
232 /// This map is essentially a wrapper for \c std::vector. It assigns
233 /// values to integer keys from the range <tt>[0..size-1]</tt>.
234 /// It can be used together with some data structures, e.g.
235 /// heap types and \c UnionFind, when the used items are small
236 /// integers. This map conforms to the \ref concepts::ReferenceMap
237 /// "ReferenceMap" concept.
239 /// The simplest way of using this map is through the rangeMap()
241 template <typename V>
242 class RangeMap : public MapBase<int, V> {
243 template <typename V1>
244 friend class RangeMap;
247 typedef std::vector<V> Vector;
257 typedef typename Vector::reference Reference;
258 /// Const reference type
259 typedef typename Vector::const_reference ConstReference;
261 typedef True ReferenceMapTag;
265 /// Constructor with specified default value.
266 RangeMap(int size = 0, const Value &value = Value())
267 : _vector(size, value) {}
269 /// Constructs the map from an appropriate \c std::vector.
270 template <typename V1>
271 RangeMap(const std::vector<V1>& vector)
272 : _vector(vector.begin(), vector.end()) {}
274 /// Constructs the map from another \c RangeMap.
275 template <typename V1>
276 RangeMap(const RangeMap<V1> &c)
277 : _vector(c._vector.begin(), c._vector.end()) {}
279 /// Returns the size of the map.
281 return _vector.size();
286 /// Resizes the underlying \c std::vector container, so changes the
287 /// keyset of the map.
288 /// \param size The new size of the map. The new keyset will be the
289 /// range <tt>[0..size-1]</tt>.
290 /// \param value The default value to assign to the new keys.
291 void resize(int size, const Value &value = Value()) {
292 _vector.resize(size, value);
297 // RangeMap& operator=(const RangeMap&);
302 // RangeMap(const RangeMap&);
305 Reference operator[](const Key &k) {
310 ConstReference operator[](const Key &k) const {
315 void set(const Key &k, const Value &v) {
320 /// Returns a \c RangeMap class
322 /// This function just returns a \c RangeMap class.
323 /// \relates RangeMap
325 inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
326 return RangeMap<V>(size, value);
329 /// \brief Returns a \c RangeMap class created from an appropriate
332 /// This function just returns a \c RangeMap class created from an
333 /// appropriate \c std::vector.
334 /// \relates RangeMap
336 inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
337 return RangeMap<V>(vector);
341 /// Map type based on \c std::map
343 /// This map is essentially a wrapper for \c std::map with addition
344 /// that you can specify a default value for the keys that are not
345 /// stored actually. This value can be different from the default
346 /// contructed value (i.e. \c %Value()).
347 /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
350 /// This map is useful if a default value should be assigned to most of
351 /// the keys and different values should be assigned only to a few
352 /// keys (i.e. the map is "sparse").
353 /// The name of this type also refers to this important usage.
355 /// Apart form that, this map can be used in many other cases since it
356 /// is based on \c std::map, which is a general associative container.
357 /// However, keep in mind that it is usually not as efficient as other
360 /// The simplest way of using this map is through the sparseMap()
362 template <typename K, typename V, typename Comp = std::less<K> >
363 class SparseMap : public MapBase<K, V> {
364 template <typename K1, typename V1, typename C1>
365 friend class SparseMap;
373 typedef Value& Reference;
374 /// Const reference type
375 typedef const Value& ConstReference;
377 typedef True ReferenceMapTag;
381 typedef std::map<K, V, Comp> Map;
387 /// \brief Constructor with specified default value.
388 SparseMap(const Value &value = Value()) : _value(value) {}
389 /// \brief Constructs the map from an appropriate \c std::map, and
390 /// explicitly specifies a default value.
391 template <typename V1, typename Comp1>
392 SparseMap(const std::map<Key, V1, Comp1> &map,
393 const Value &value = Value())
394 : _map(map.begin(), map.end()), _value(value) {}
396 /// \brief Constructs the map from another \c SparseMap.
397 template<typename V1, typename Comp1>
398 SparseMap(const SparseMap<Key, V1, Comp1> &c)
399 : _map(c._map.begin(), c._map.end()), _value(c._value) {}
403 SparseMap& operator=(const SparseMap&);
408 Reference operator[](const Key &k) {
409 typename Map::iterator it = _map.lower_bound(k);
410 if (it != _map.end() && !_map.key_comp()(k, it->first))
413 return _map.insert(it, std::make_pair(k, _value))->second;
417 ConstReference operator[](const Key &k) const {
418 typename Map::const_iterator it = _map.find(k);
419 if (it != _map.end())
426 void set(const Key &k, const Value &v) {
427 typename Map::iterator it = _map.lower_bound(k);
428 if (it != _map.end() && !_map.key_comp()(k, it->first))
431 _map.insert(it, std::make_pair(k, v));
435 void setAll(const Value &v) {
441 /// Returns a \c SparseMap class
443 /// This function just returns a \c SparseMap class with specified
445 /// \relates SparseMap
446 template<typename K, typename V, typename Compare>
447 inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
448 return SparseMap<K, V, Compare>(value);
451 template<typename K, typename V>
452 inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
453 return SparseMap<K, V, std::less<K> >(value);
456 /// \brief Returns a \c SparseMap class created from an appropriate
459 /// This function just returns a \c SparseMap class created from an
460 /// appropriate \c std::map.
461 /// \relates SparseMap
462 template<typename K, typename V, typename Compare>
463 inline SparseMap<K, V, Compare>
464 sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
466 return SparseMap<K, V, Compare>(map, value);
471 /// \addtogroup map_adaptors
474 /// Composition of two maps
476 /// This \ref concepts::ReadMap "read-only map" returns the
477 /// composition of two given maps. That is to say, if \c m1 is of
478 /// type \c M1 and \c m2 is of \c M2, then for
480 /// ComposeMap<M1, M2> cm(m1,m2);
482 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
484 /// The \c Key type of the map is inherited from \c M2 and the
485 /// \c Value type is from \c M1.
486 /// \c M2::Value must be convertible to \c M1::Key.
488 /// The simplest way of using this map is through the composeMap()
492 template <typename M1, typename M2>
493 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
498 typedef typename M2::Key Key;
500 typedef typename M1::Value Value;
503 ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
506 typename MapTraits<M1>::ConstReturnValue
507 operator[](const Key &k) const { return _m1[_m2[k]]; }
510 /// Returns a \c ComposeMap class
512 /// This function just returns a \c ComposeMap class.
514 /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
515 /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
516 /// will be equal to <tt>m1[m2[x]]</tt>.
518 /// \relates ComposeMap
519 template <typename M1, typename M2>
520 inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
521 return ComposeMap<M1, M2>(m1, m2);
525 /// Combination of two maps using an STL (binary) functor.
527 /// This \ref concepts::ReadMap "read-only map" takes two maps and a
528 /// binary functor and returns the combination of the two given maps
529 /// using the functor.
530 /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
531 /// and \c f is of \c F, then for
533 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
535 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
537 /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
538 /// must be convertible to \c M2::Key) and the \c Value type is \c V.
539 /// \c M2::Value and \c M1::Value must be convertible to the
540 /// corresponding input parameter of \c F and the return type of \c F
541 /// must be convertible to \c V.
543 /// The simplest way of using this map is through the combineMap()
547 template<typename M1, typename M2, typename F,
548 typename V = typename F::result_type>
549 class CombineMap : public MapBase<typename M1::Key, V> {
555 typedef typename M1::Key Key;
560 CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
561 : _m1(m1), _m2(m2), _f(f) {}
563 Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
566 /// Returns a \c CombineMap class
568 /// This function just returns a \c CombineMap class.
570 /// For example, if \c m1 and \c m2 are both maps with \c double
573 /// combineMap(m1,m2,std::plus<double>())
580 /// This function is specialized for adaptable binary function
581 /// classes and C++ functions.
583 /// \relates CombineMap
584 template<typename M1, typename M2, typename F, typename V>
585 inline CombineMap<M1, M2, F, V>
586 combineMap(const M1 &m1, const M2 &m2, const F &f) {
587 return CombineMap<M1, M2, F, V>(m1,m2,f);
590 template<typename M1, typename M2, typename F>
591 inline CombineMap<M1, M2, F, typename F::result_type>
592 combineMap(const M1 &m1, const M2 &m2, const F &f) {
593 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
596 template<typename M1, typename M2, typename K1, typename K2, typename V>
597 inline CombineMap<M1, M2, V (*)(K1, K2), V>
598 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
599 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
603 /// Converts an STL style (unary) functor to a map
605 /// This \ref concepts::ReadMap "read-only map" returns the value
606 /// of a given functor. Actually, it just wraps the functor and
607 /// provides the \c Key and \c Value typedefs.
609 /// Template parameters \c K and \c V will become its \c Key and
610 /// \c Value. In most cases they have to be given explicitly because
611 /// a functor typically does not provide \c argument_type and
612 /// \c result_type typedefs.
613 /// Parameter \c F is the type of the used functor.
615 /// The simplest way of using this map is through the functorToMap()
620 typename K = typename F::argument_type,
621 typename V = typename F::result_type>
622 class FunctorToMap : public MapBase<K, V> {
631 FunctorToMap(const F &f = F()) : _f(f) {}
633 Value operator[](const Key &k) const { return _f(k); }
636 /// Returns a \c FunctorToMap class
638 /// This function just returns a \c FunctorToMap class.
640 /// This function is specialized for adaptable binary function
641 /// classes and C++ functions.
643 /// \relates FunctorToMap
644 template<typename K, typename V, typename F>
645 inline FunctorToMap<F, K, V> functorToMap(const F &f) {
646 return FunctorToMap<F, K, V>(f);
649 template <typename F>
650 inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
651 functorToMap(const F &f)
653 return FunctorToMap<F, typename F::argument_type,
654 typename F::result_type>(f);
657 template <typename K, typename V>
658 inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
659 return FunctorToMap<V (*)(K), K, V>(f);
663 /// Converts a map to an STL style (unary) functor
665 /// This class converts a map to an STL style (unary) functor.
666 /// That is it provides an <tt>operator()</tt> to read its values.
668 /// For the sake of convenience it also works as a usual
669 /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
670 /// and the \c Key and \c Value typedefs also exist.
672 /// The simplest way of using this map is through the mapToFunctor()
676 template <typename M>
677 class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
681 typedef typename M::Key Key;
683 typedef typename M::Value Value;
685 typedef typename M::Key argument_type;
686 typedef typename M::Value result_type;
689 MapToFunctor(const M &m) : _m(m) {}
691 Value operator()(const Key &k) const { return _m[k]; }
693 Value operator[](const Key &k) const { return _m[k]; }
696 /// Returns a \c MapToFunctor class
698 /// This function just returns a \c MapToFunctor class.
699 /// \relates MapToFunctor
701 inline MapToFunctor<M> mapToFunctor(const M &m) {
702 return MapToFunctor<M>(m);
706 /// \brief Map adaptor to convert the \c Value type of a map to
707 /// another type using the default conversion.
709 /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
710 /// "readable map" to another type using the default conversion.
711 /// The \c Key type of it is inherited from \c M and the \c Value
713 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
715 /// The simplest way of using this map is through the convertMap()
717 template <typename M, typename V>
718 class ConvertMap : public MapBase<typename M::Key, V> {
722 typedef typename M::Key Key;
729 /// \param m The underlying map.
730 ConvertMap(const M &m) : _m(m) {}
733 Value operator[](const Key &k) const { return _m[k]; }
736 /// Returns a \c ConvertMap class
738 /// This function just returns a \c ConvertMap class.
739 /// \relates ConvertMap
740 template<typename V, typename M>
741 inline ConvertMap<M, V> convertMap(const M &map) {
742 return ConvertMap<M, V>(map);
746 /// Applies all map setting operations to two maps
748 /// This map has two \ref concepts::WriteMap "writable map" parameters
749 /// and each write request will be passed to both of them.
750 /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
751 /// operations will return the corresponding values of \c M1.
753 /// The \c Key and \c Value types are inherited from \c M1.
754 /// The \c Key and \c Value of \c M2 must be convertible from those
757 /// The simplest way of using this map is through the forkMap()
759 template<typename M1, typename M2>
760 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
765 typedef typename M1::Key Key;
767 typedef typename M1::Value Value;
770 ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
771 /// Returns the value associated with the given key in the first map.
772 Value operator[](const Key &k) const { return _m1[k]; }
773 /// Sets the value associated with the given key in both maps.
774 void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
777 /// Returns a \c ForkMap class
779 /// This function just returns a \c ForkMap class.
781 template <typename M1, typename M2>
782 inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
783 return ForkMap<M1,M2>(m1,m2);
789 /// This \ref concepts::ReadMap "read-only map" returns the sum
790 /// of the values of the two given maps.
791 /// Its \c Key and \c Value types are inherited from \c M1.
792 /// The \c Key and \c Value of \c M2 must be convertible to those of
795 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
797 /// AddMap<M1,M2> am(m1,m2);
799 /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
801 /// The simplest way of using this map is through the addMap()
804 /// \sa SubMap, MulMap, DivMap
805 /// \sa ShiftMap, ShiftWriteMap
806 template<typename M1, typename M2>
807 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
812 typedef typename M1::Key Key;
814 typedef typename M1::Value Value;
817 AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
819 Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
822 /// Returns an \c AddMap class
824 /// This function just returns an \c AddMap class.
826 /// For example, if \c m1 and \c m2 are both maps with \c double
827 /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
828 /// <tt>m1[x]+m2[x]</tt>.
831 template<typename M1, typename M2>
832 inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
833 return AddMap<M1, M2>(m1,m2);
837 /// Difference of two maps
839 /// This \ref concepts::ReadMap "read-only map" returns the difference
840 /// of the values of the two given maps.
841 /// Its \c Key and \c Value types are inherited from \c M1.
842 /// The \c Key and \c Value of \c M2 must be convertible to those of
845 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
847 /// SubMap<M1,M2> sm(m1,m2);
849 /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
851 /// The simplest way of using this map is through the subMap()
854 /// \sa AddMap, MulMap, DivMap
855 template<typename M1, typename M2>
856 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
861 typedef typename M1::Key Key;
863 typedef typename M1::Value Value;
866 SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
868 Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
871 /// Returns a \c SubMap class
873 /// This function just returns a \c SubMap class.
875 /// For example, if \c m1 and \c m2 are both maps with \c double
876 /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
877 /// <tt>m1[x]-m2[x]</tt>.
880 template<typename M1, typename M2>
881 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
882 return SubMap<M1, M2>(m1,m2);
886 /// Product of two maps
888 /// This \ref concepts::ReadMap "read-only map" returns the product
889 /// of the values of the two given maps.
890 /// Its \c Key and \c Value types are inherited from \c M1.
891 /// The \c Key and \c Value of \c M2 must be convertible to those of
894 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
896 /// MulMap<M1,M2> mm(m1,m2);
898 /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
900 /// The simplest way of using this map is through the mulMap()
903 /// \sa AddMap, SubMap, DivMap
904 /// \sa ScaleMap, ScaleWriteMap
905 template<typename M1, typename M2>
906 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
911 typedef typename M1::Key Key;
913 typedef typename M1::Value Value;
916 MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
918 Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
921 /// Returns a \c MulMap class
923 /// This function just returns a \c MulMap class.
925 /// For example, if \c m1 and \c m2 are both maps with \c double
926 /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
927 /// <tt>m1[x]*m2[x]</tt>.
930 template<typename M1, typename M2>
931 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
932 return MulMap<M1, M2>(m1,m2);
936 /// Quotient of two maps
938 /// This \ref concepts::ReadMap "read-only map" returns the quotient
939 /// of the values of the two given maps.
940 /// Its \c Key and \c Value types are inherited from \c M1.
941 /// The \c Key and \c Value of \c M2 must be convertible to those of
944 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
946 /// DivMap<M1,M2> dm(m1,m2);
948 /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
950 /// The simplest way of using this map is through the divMap()
953 /// \sa AddMap, SubMap, MulMap
954 template<typename M1, typename M2>
955 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
960 typedef typename M1::Key Key;
962 typedef typename M1::Value Value;
965 DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
967 Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
970 /// Returns a \c DivMap class
972 /// This function just returns a \c DivMap class.
974 /// For example, if \c m1 and \c m2 are both maps with \c double
975 /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
976 /// <tt>m1[x]/m2[x]</tt>.
979 template<typename M1, typename M2>
980 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
981 return DivMap<M1, M2>(m1,m2);
985 /// Shifts a map with a constant.
987 /// This \ref concepts::ReadMap "read-only map" returns the sum of
988 /// the given map and a constant value (i.e. it shifts the map with
989 /// the constant). Its \c Key and \c Value are inherited from \c M.
993 /// ShiftMap<M> sh(m,v);
997 /// ConstMap<M::Key, M::Value> cm(v);
998 /// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
1001 /// The simplest way of using this map is through the shiftMap()
1004 /// \sa ShiftWriteMap
1005 template<typename M, typename C = typename M::Value>
1006 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
1011 typedef typename M::Key Key;
1013 typedef typename M::Value Value;
1018 /// \param m The undelying map.
1019 /// \param v The constant value.
1020 ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1022 Value operator[](const Key &k) const { return _m[k]+_v; }
1025 /// Shifts a map with a constant (read-write version).
1027 /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1028 /// of the given map and a constant value (i.e. it shifts the map with
1029 /// the constant). Its \c Key and \c Value are inherited from \c M.
1030 /// It makes also possible to write the map.
1032 /// The simplest way of using this map is through the shiftWriteMap()
1036 template<typename M, typename C = typename M::Value>
1037 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1042 typedef typename M::Key Key;
1044 typedef typename M::Value Value;
1049 /// \param m The undelying map.
1050 /// \param v The constant value.
1051 ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1053 Value operator[](const Key &k) const { return _m[k]+_v; }
1055 void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1058 /// Returns a \c ShiftMap class
1060 /// This function just returns a \c ShiftMap class.
1062 /// For example, if \c m is a map with \c double values and \c v is
1063 /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1064 /// <tt>m[x]+v</tt>.
1066 /// \relates ShiftMap
1067 template<typename M, typename C>
1068 inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1069 return ShiftMap<M, C>(m,v);
1072 /// Returns a \c ShiftWriteMap class
1074 /// This function just returns a \c ShiftWriteMap class.
1076 /// For example, if \c m is a map with \c double values and \c v is
1077 /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1078 /// <tt>m[x]+v</tt>.
1079 /// Moreover it makes also possible to write the map.
1081 /// \relates ShiftWriteMap
1082 template<typename M, typename C>
1083 inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1084 return ShiftWriteMap<M, C>(m,v);
1088 /// Scales a map with a constant.
1090 /// This \ref concepts::ReadMap "read-only map" returns the value of
1091 /// the given map multiplied from the left side with a constant value.
1092 /// Its \c Key and \c Value are inherited from \c M.
1096 /// ScaleMap<M> sc(m,v);
1098 /// is equivalent to
1100 /// ConstMap<M::Key, M::Value> cm(v);
1101 /// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1104 /// The simplest way of using this map is through the scaleMap()
1107 /// \sa ScaleWriteMap
1108 template<typename M, typename C = typename M::Value>
1109 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1114 typedef typename M::Key Key;
1116 typedef typename M::Value Value;
1121 /// \param m The undelying map.
1122 /// \param v The constant value.
1123 ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1125 Value operator[](const Key &k) const { return _v*_m[k]; }
1128 /// Scales a map with a constant (read-write version).
1130 /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1131 /// the given map multiplied from the left side with a constant value.
1132 /// Its \c Key and \c Value are inherited from \c M.
1133 /// It can also be used as write map if the \c / operator is defined
1134 /// between \c Value and \c C and the given multiplier is not zero.
1136 /// The simplest way of using this map is through the scaleWriteMap()
1140 template<typename M, typename C = typename M::Value>
1141 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1146 typedef typename M::Key Key;
1148 typedef typename M::Value Value;
1153 /// \param m The undelying map.
1154 /// \param v The constant value.
1155 ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1157 Value operator[](const Key &k) const { return _v*_m[k]; }
1159 void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1162 /// Returns a \c ScaleMap class
1164 /// This function just returns a \c ScaleMap class.
1166 /// For example, if \c m is a map with \c double values and \c v is
1167 /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1168 /// <tt>v*m[x]</tt>.
1170 /// \relates ScaleMap
1171 template<typename M, typename C>
1172 inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1173 return ScaleMap<M, C>(m,v);
1176 /// Returns a \c ScaleWriteMap class
1178 /// This function just returns a \c ScaleWriteMap class.
1180 /// For example, if \c m is a map with \c double values and \c v is
1181 /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1182 /// <tt>v*m[x]</tt>.
1183 /// Moreover it makes also possible to write the map.
1185 /// \relates ScaleWriteMap
1186 template<typename M, typename C>
1187 inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1188 return ScaleWriteMap<M, C>(m,v);
1192 /// Negative of a map
1194 /// This \ref concepts::ReadMap "read-only map" returns the negative
1195 /// of the values of the given map (using the unary \c - operator).
1196 /// Its \c Key and \c Value are inherited from \c M.
1198 /// If M::Value is \c int, \c double etc., then
1200 /// NegMap<M> neg(m);
1202 /// is equivalent to
1204 /// ScaleMap<M> neg(m,-1);
1207 /// The simplest way of using this map is through the negMap()
1211 template<typename M>
1212 class NegMap : public MapBase<typename M::Key, typename M::Value> {
1216 typedef typename M::Key Key;
1218 typedef typename M::Value Value;
1221 NegMap(const M &m) : _m(m) {}
1223 Value operator[](const Key &k) const { return -_m[k]; }
1226 /// Negative of a map (read-write version)
1228 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1229 /// negative of the values of the given map (using the unary \c -
1231 /// Its \c Key and \c Value are inherited from \c M.
1232 /// It makes also possible to write the map.
1234 /// If M::Value is \c int, \c double etc., then
1236 /// NegWriteMap<M> neg(m);
1238 /// is equivalent to
1240 /// ScaleWriteMap<M> neg(m,-1);
1243 /// The simplest way of using this map is through the negWriteMap()
1247 template<typename M>
1248 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1252 typedef typename M::Key Key;
1254 typedef typename M::Value Value;
1257 NegWriteMap(M &m) : _m(m) {}
1259 Value operator[](const Key &k) const { return -_m[k]; }
1261 void set(const Key &k, const Value &v) { _m.set(k, -v); }
1264 /// Returns a \c NegMap class
1266 /// This function just returns a \c NegMap class.
1268 /// For example, if \c m is a map with \c double values, then
1269 /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1272 template <typename M>
1273 inline NegMap<M> negMap(const M &m) {
1274 return NegMap<M>(m);
1277 /// Returns a \c NegWriteMap class
1279 /// This function just returns a \c NegWriteMap class.
1281 /// For example, if \c m is a map with \c double values, then
1282 /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1283 /// Moreover it makes also possible to write the map.
1285 /// \relates NegWriteMap
1286 template <typename M>
1287 inline NegWriteMap<M> negWriteMap(M &m) {
1288 return NegWriteMap<M>(m);
1292 /// Absolute value of a map
1294 /// This \ref concepts::ReadMap "read-only map" returns the absolute
1295 /// value of the values of the given map.
1296 /// Its \c Key and \c Value are inherited from \c M.
1297 /// \c Value must be comparable to \c 0 and the unary \c -
1298 /// operator must be defined for it, of course.
1300 /// The simplest way of using this map is through the absMap()
1302 template<typename M>
1303 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1307 typedef typename M::Key Key;
1309 typedef typename M::Value Value;
1312 AbsMap(const M &m) : _m(m) {}
1314 Value operator[](const Key &k) const {
1316 return tmp >= 0 ? tmp : -tmp;
1321 /// Returns an \c AbsMap class
1323 /// This function just returns an \c AbsMap class.
1325 /// For example, if \c m is a map with \c double values, then
1326 /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1327 /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1331 template<typename M>
1332 inline AbsMap<M> absMap(const M &m) {
1333 return AbsMap<M>(m);
1338 // Logical maps and map adaptors:
1340 /// \addtogroup maps
1343 /// Constant \c true map.
1345 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1352 /// is equivalent to
1354 /// ConstMap<K,bool> tm(true);
1359 template <typename K>
1360 class TrueMap : public MapBase<K, bool> {
1367 /// Gives back \c true.
1368 Value operator[](const Key&) const { return true; }
1371 /// Returns a \c TrueMap class
1373 /// This function just returns a \c TrueMap class.
1374 /// \relates TrueMap
1375 template<typename K>
1376 inline TrueMap<K> trueMap() {
1377 return TrueMap<K>();
1381 /// Constant \c false map.
1383 /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1390 /// is equivalent to
1392 /// ConstMap<K,bool> fm(false);
1397 template <typename K>
1398 class FalseMap : public MapBase<K, bool> {
1405 /// Gives back \c false.
1406 Value operator[](const Key&) const { return false; }
1409 /// Returns a \c FalseMap class
1411 /// This function just returns a \c FalseMap class.
1412 /// \relates FalseMap
1413 template<typename K>
1414 inline FalseMap<K> falseMap() {
1415 return FalseMap<K>();
1420 /// \addtogroup map_adaptors
1423 /// Logical 'and' of two maps
1425 /// This \ref concepts::ReadMap "read-only map" returns the logical
1426 /// 'and' of the values of the two given maps.
1427 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1428 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1430 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1432 /// AndMap<M1,M2> am(m1,m2);
1434 /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1436 /// The simplest way of using this map is through the andMap()
1440 /// \sa NotMap, NotWriteMap
1441 template<typename M1, typename M2>
1442 class AndMap : public MapBase<typename M1::Key, bool> {
1447 typedef typename M1::Key Key;
1452 AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1454 Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1457 /// Returns an \c AndMap class
1459 /// This function just returns an \c AndMap class.
1461 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1462 /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1463 /// <tt>m1[x]&&m2[x]</tt>.
1466 template<typename M1, typename M2>
1467 inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1468 return AndMap<M1, M2>(m1,m2);
1472 /// Logical 'or' of two maps
1474 /// This \ref concepts::ReadMap "read-only map" returns the logical
1475 /// 'or' of the values of the two given maps.
1476 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1477 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1479 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1481 /// OrMap<M1,M2> om(m1,m2);
1483 /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1485 /// The simplest way of using this map is through the orMap()
1489 /// \sa NotMap, NotWriteMap
1490 template<typename M1, typename M2>
1491 class OrMap : public MapBase<typename M1::Key, bool> {
1496 typedef typename M1::Key Key;
1501 OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1503 Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1506 /// Returns an \c OrMap class
1508 /// This function just returns an \c OrMap class.
1510 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1511 /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1512 /// <tt>m1[x]||m2[x]</tt>.
1515 template<typename M1, typename M2>
1516 inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1517 return OrMap<M1, M2>(m1,m2);
1521 /// Logical 'not' of a map
1523 /// This \ref concepts::ReadMap "read-only map" returns the logical
1524 /// negation of the values of the given map.
1525 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1527 /// The simplest way of using this map is through the notMap()
1531 template <typename M>
1532 class NotMap : public MapBase<typename M::Key, bool> {
1536 typedef typename M::Key Key;
1541 NotMap(const M &m) : _m(m) {}
1543 Value operator[](const Key &k) const { return !_m[k]; }
1546 /// Logical 'not' of a map (read-write version)
1548 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1549 /// logical negation of the values of the given map.
1550 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1551 /// It makes also possible to write the map. When a value is set,
1552 /// the opposite value is set to the original map.
1554 /// The simplest way of using this map is through the notWriteMap()
1558 template <typename M>
1559 class NotWriteMap : public MapBase<typename M::Key, bool> {
1563 typedef typename M::Key Key;
1568 NotWriteMap(M &m) : _m(m) {}
1570 Value operator[](const Key &k) const { return !_m[k]; }
1572 void set(const Key &k, bool v) { _m.set(k, !v); }
1575 /// Returns a \c NotMap class
1577 /// This function just returns a \c NotMap class.
1579 /// For example, if \c m is a map with \c bool values, then
1580 /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1583 template <typename M>
1584 inline NotMap<M> notMap(const M &m) {
1585 return NotMap<M>(m);
1588 /// Returns a \c NotWriteMap class
1590 /// This function just returns a \c NotWriteMap class.
1592 /// For example, if \c m is a map with \c bool values, then
1593 /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1594 /// Moreover it makes also possible to write the map.
1596 /// \relates NotWriteMap
1597 template <typename M>
1598 inline NotWriteMap<M> notWriteMap(M &m) {
1599 return NotWriteMap<M>(m);
1603 /// Combination of two maps using the \c == operator
1605 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1606 /// the keys for which the corresponding values of the two maps are
1608 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1609 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1611 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1613 /// EqualMap<M1,M2> em(m1,m2);
1615 /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1617 /// The simplest way of using this map is through the equalMap()
1621 template<typename M1, typename M2>
1622 class EqualMap : public MapBase<typename M1::Key, bool> {
1627 typedef typename M1::Key Key;
1632 EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1634 Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1637 /// Returns an \c EqualMap class
1639 /// This function just returns an \c EqualMap class.
1641 /// For example, if \c m1 and \c m2 are maps with keys and values of
1642 /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1643 /// <tt>m1[x]==m2[x]</tt>.
1645 /// \relates EqualMap
1646 template<typename M1, typename M2>
1647 inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1648 return EqualMap<M1, M2>(m1,m2);
1652 /// Combination of two maps using the \c < operator
1654 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1655 /// the keys for which the corresponding value of the first map is
1656 /// less then the value of the second map.
1657 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1658 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1660 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1662 /// LessMap<M1,M2> lm(m1,m2);
1664 /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1666 /// The simplest way of using this map is through the lessMap()
1670 template<typename M1, typename M2>
1671 class LessMap : public MapBase<typename M1::Key, bool> {
1676 typedef typename M1::Key Key;
1681 LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1683 Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1686 /// Returns an \c LessMap class
1688 /// This function just returns an \c LessMap class.
1690 /// For example, if \c m1 and \c m2 are maps with keys and values of
1691 /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1692 /// <tt>m1[x]<m2[x]</tt>.
1694 /// \relates LessMap
1695 template<typename M1, typename M2>
1696 inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1697 return LessMap<M1, M2>(m1,m2);
1700 namespace _maps_bits {
1702 template <typename _Iterator, typename Enable = void>
1703 struct IteratorTraits {
1704 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1707 template <typename _Iterator>
1708 struct IteratorTraits<_Iterator,
1709 typename exists<typename _Iterator::container_type>::type>
1711 typedef typename _Iterator::container_type::value_type Value;
1718 /// \addtogroup maps
1721 /// \brief Writable bool map for logging each \c true assigned element
1723 /// A \ref concepts::WriteMap "writable" bool map for logging
1724 /// each \c true assigned element, i.e it copies subsequently each
1725 /// keys set to \c true to the given iterator.
1726 /// The most important usage of it is storing certain nodes or arcs
1727 /// that were marked \c true by an algorithm.
1729 /// There are several algorithms that provide solutions through bool
1730 /// maps and most of them assign \c true at most once for each key.
1731 /// In these cases it is a natural request to store each \c true
1732 /// assigned elements (in order of the assignment), which can be
1733 /// easily done with LoggerBoolMap.
1735 /// The simplest way of using this map is through the loggerBoolMap()
1738 /// \tparam IT The type of the iterator.
1739 /// \tparam KEY The key type of the map. The default value set
1740 /// according to the iterator type should work in most cases.
1742 /// \note The container of the iterator must contain enough space
1743 /// for the elements or the iterator should be an inserter iterator.
1745 template <typename IT, typename KEY>
1747 template <typename IT,
1748 typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1750 class LoggerBoolMap : public MapBase<KEY, bool> {
1758 typedef IT Iterator;
1761 LoggerBoolMap(Iterator it)
1762 : _begin(it), _end(it) {}
1764 /// Gives back the given iterator set for the first key
1765 Iterator begin() const {
1769 /// Gives back the the 'after the last' iterator
1770 Iterator end() const {
1774 /// The set function of the map
1775 void set(const Key& key, Value value) {
1786 /// Returns a \c LoggerBoolMap class
1788 /// This function just returns a \c LoggerBoolMap class.
1790 /// The most important usage of it is storing certain nodes or arcs
1791 /// that were marked \c true by an algorithm.
1792 /// For example, it makes easier to store the nodes in the processing
1793 /// order of Dfs algorithm, as the following examples show.
1795 /// std::vector<Node> v;
1796 /// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
1799 /// std::vector<Node> v(countNodes(g));
1800 /// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
1803 /// \note The container of the iterator must contain enough space
1804 /// for the elements or the iterator should be an inserter iterator.
1806 /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1807 /// it cannot be used when a readable map is needed, for example, as
1808 /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1810 /// \relates LoggerBoolMap
1811 template<typename Iterator>
1812 inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1813 return LoggerBoolMap<Iterator>(it);
1818 /// \addtogroup graph_maps
1821 /// \brief Provides an immutable and unique id for each item in a graph.
1823 /// IdMap provides a unique and immutable id for each item of the
1824 /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1825 /// - \b unique: different items get different ids,
1826 /// - \b immutable: the id of an item does not change (even if you
1827 /// delete other nodes).
1829 /// Using this map you get access (i.e. can read) the inner id values of
1830 /// the items stored in the graph, which is returned by the \c id()
1831 /// function of the graph. This map can be inverted with its member
1832 /// class \c InverseMap or with the \c operator()() member.
1834 /// \tparam GR The graph type.
1835 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1839 template <typename GR, typename K>
1840 class IdMap : public MapBase<K, int> {
1842 /// The graph type of IdMap.
1845 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1847 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1849 /// The value type of IdMap.
1852 /// \brief Constructor.
1854 /// Constructor of the map.
1855 explicit IdMap(const Graph& graph) : _graph(&graph) {}
1857 /// \brief Gives back the \e id of the item.
1859 /// Gives back the immutable and unique \e id of the item.
1860 int operator[](const Item& item) const { return _graph->id(item);}
1862 /// \brief Gives back the \e item by its id.
1864 /// Gives back the \e item by its id.
1865 Item operator()(int id) { return _graph->fromId(id, Item()); }
1868 const Graph* _graph;
1872 /// \brief The inverse map type of IdMap.
1874 /// The inverse map type of IdMap. The subscript operator gives back
1875 /// an item by its id.
1876 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1881 /// \brief Constructor.
1883 /// Constructor for creating an id-to-item map.
1884 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1886 /// \brief Constructor.
1888 /// Constructor for creating an id-to-item map.
1889 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1891 /// \brief Gives back an item by its id.
1893 /// Gives back an item by its id.
1894 Item operator[](int id) const { return _graph->fromId(id, Item());}
1897 const Graph* _graph;
1900 /// \brief Gives back the inverse of the map.
1902 /// Gives back the inverse of the IdMap.
1903 InverseMap inverse() const { return InverseMap(*_graph);}
1906 /// \brief Returns an \c IdMap class.
1908 /// This function just returns an \c IdMap class.
1910 template <typename K, typename GR>
1911 inline IdMap<GR, K> idMap(const GR& graph) {
1912 return IdMap<GR, K>(graph);
1915 /// \brief General cross reference graph map type.
1917 /// This class provides simple invertable graph maps.
1918 /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1919 /// and if a key is set to a new value, then stores it in the inverse map.
1920 /// The graph items can be accessed by their values either using
1921 /// \c InverseMap or \c operator()(), and the values of the map can be
1922 /// accessed with an STL compatible forward iterator (\c ValueIt).
1924 /// This map is intended to be used when all associated values are
1925 /// different (the map is actually invertable) or there are only a few
1926 /// items with the same value.
1927 /// Otherwise consider to use \c IterableValueMap, which is more
1928 /// suitable and more efficient for such cases. It provides iterators
1929 /// to traverse the items with the same associated value, but
1930 /// it does not have \c InverseMap.
1932 /// This type is not reference map, so it cannot be modified with
1933 /// the subscript operator.
1935 /// \tparam GR The graph type.
1936 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1938 /// \tparam V The value type of the map.
1940 /// \see IterableValueMap
1941 template <typename GR, typename K, typename V>
1943 : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1946 typedef typename ItemSetTraits<GR, K>::
1947 template Map<V>::Type Map;
1949 typedef std::multimap<V, K> Container;
1954 /// The graph type of CrossRefMap.
1957 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1959 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1961 /// The value type of CrossRefMap.
1964 /// \brief Constructor.
1966 /// Construct a new CrossRefMap for the given graph.
1967 explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1969 /// \brief Forward iterator for values.
1971 /// This iterator is an STL compatible forward
1972 /// iterator on the values of the map. The values can
1973 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1974 /// They are considered with multiplicity, so each value is
1975 /// traversed for each item it is assigned to.
1977 : public std::iterator<std::forward_iterator_tag, Value> {
1978 friend class CrossRefMap;
1980 ValueIt(typename Container::const_iterator _it)
1988 ValueIt& operator++() { ++it; return *this; }
1990 ValueIt operator++(int) {
1997 const Value& operator*() const { return it->first; }
1999 const Value* operator->() const { return &(it->first); }
2002 bool operator==(ValueIt jt) const { return it == jt.it; }
2004 bool operator!=(ValueIt jt) const { return it != jt.it; }
2007 typename Container::const_iterator it;
2010 /// Alias for \c ValueIt
2011 typedef ValueIt ValueIterator;
2013 /// \brief Returns an iterator to the first value.
2015 /// Returns an STL compatible iterator to the
2016 /// first value of the map. The values of the
2017 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2019 ValueIt beginValue() const {
2020 return ValueIt(_inv_map.begin());
2023 /// \brief Returns an iterator after the last value.
2025 /// Returns an STL compatible iterator after the
2026 /// last value of the map. The values of the
2027 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2029 ValueIt endValue() const {
2030 return ValueIt(_inv_map.end());
2033 /// \brief Sets the value associated with the given key.
2035 /// Sets the value associated with the given key.
2036 void set(const Key& key, const Value& val) {
2037 Value oldval = Map::operator[](key);
2038 typename Container::iterator it;
2039 for (it = _inv_map.equal_range(oldval).first;
2040 it != _inv_map.equal_range(oldval).second; ++it) {
2041 if (it->second == key) {
2046 _inv_map.insert(std::make_pair(val, key));
2050 /// \brief Returns the value associated with the given key.
2052 /// Returns the value associated with the given key.
2053 typename MapTraits<Map>::ConstReturnValue
2054 operator[](const Key& key) const {
2055 return Map::operator[](key);
2058 /// \brief Gives back an item by its value.
2060 /// This function gives back an item that is assigned to
2061 /// the given value or \c INVALID if no such item exists.
2062 /// If there are more items with the same associated value,
2063 /// only one of them is returned.
2064 Key operator()(const Value& val) const {
2065 typename Container::const_iterator it = _inv_map.find(val);
2066 return it != _inv_map.end() ? it->second : INVALID;
2069 /// \brief Returns the number of items with the given value.
2071 /// This function returns the number of items with the given value
2072 /// associated with it.
2073 int count(const Value &val) const {
2074 return _inv_map.count(val);
2079 /// \brief Erase the key from the map and the inverse map.
2081 /// Erase the key from the map and the inverse map. It is called by the
2082 /// \c AlterationNotifier.
2083 virtual void erase(const Key& key) {
2084 Value val = Map::operator[](key);
2085 typename Container::iterator it;
2086 for (it = _inv_map.equal_range(val).first;
2087 it != _inv_map.equal_range(val).second; ++it) {
2088 if (it->second == key) {
2096 /// \brief Erase more keys from the map and the inverse map.
2098 /// Erase more keys from the map and the inverse map. It is called by the
2099 /// \c AlterationNotifier.
2100 virtual void erase(const std::vector<Key>& keys) {
2101 for (int i = 0; i < int(keys.size()); ++i) {
2102 Value val = Map::operator[](keys[i]);
2103 typename Container::iterator it;
2104 for (it = _inv_map.equal_range(val).first;
2105 it != _inv_map.equal_range(val).second; ++it) {
2106 if (it->second == keys[i]) {
2115 /// \brief Clear the keys from the map and the inverse map.
2117 /// Clear the keys from the map and the inverse map. It is called by the
2118 /// \c AlterationNotifier.
2119 virtual void clear() {
2126 /// \brief The inverse map type of CrossRefMap.
2128 /// The inverse map type of CrossRefMap. The subscript operator gives
2129 /// back an item by its value.
2130 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2134 /// \brief Constructor
2136 /// Constructor of the InverseMap.
2137 explicit InverseMap(const CrossRefMap& inverted)
2138 : _inverted(inverted) {}
2140 /// The value type of the InverseMap.
2141 typedef typename CrossRefMap::Key Value;
2142 /// The key type of the InverseMap.
2143 typedef typename CrossRefMap::Value Key;
2145 /// \brief Subscript operator.
2147 /// Subscript operator. It gives back an item
2148 /// that is assigned to the given value or \c INVALID
2149 /// if no such item exists.
2150 Value operator[](const Key& key) const {
2151 return _inverted(key);
2155 const CrossRefMap& _inverted;
2158 /// \brief Gives back the inverse of the map.
2160 /// Gives back the inverse of the CrossRefMap.
2161 InverseMap inverse() const {
2162 return InverseMap(*this);
2167 /// \brief Provides continuous and unique id for the
2168 /// items of a graph.
2170 /// RangeIdMap provides a unique and continuous
2171 /// id for each item of a given type (\c Node, \c Arc or
2172 /// \c Edge) in a graph. This id is
2173 /// - \b unique: different items get different ids,
2174 /// - \b continuous: the range of the ids is the set of integers
2175 /// between 0 and \c n-1, where \c n is the number of the items of
2176 /// this type (\c Node, \c Arc or \c Edge).
2177 /// - So, the ids can change when deleting an item of the same type.
2179 /// Thus this id is not (necessarily) the same as what can get using
2180 /// the \c id() function of the graph or \ref IdMap.
2181 /// This map can be inverted with its member class \c InverseMap,
2182 /// or with the \c operator()() member.
2184 /// \tparam GR The graph type.
2185 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2189 template <typename GR, typename K>
2191 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2193 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2196 /// The graph type of RangeIdMap.
2199 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2201 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2203 /// The value type of RangeIdMap.
2206 /// \brief Constructor.
2209 explicit RangeIdMap(const Graph& gr) : Map(gr) {
2211 const typename Map::Notifier* nf = Map::notifier();
2212 for (nf->first(it); it != INVALID; nf->next(it)) {
2213 Map::set(it, _inv_map.size());
2214 _inv_map.push_back(it);
2220 /// \brief Adds a new key to the map.
2222 /// Add a new key to the map. It is called by the
2223 /// \c AlterationNotifier.
2224 virtual void add(const Item& item) {
2226 Map::set(item, _inv_map.size());
2227 _inv_map.push_back(item);
2230 /// \brief Add more new keys to the map.
2232 /// Add more new keys to the map. It is called by the
2233 /// \c AlterationNotifier.
2234 virtual void add(const std::vector<Item>& items) {
2236 for (int i = 0; i < int(items.size()); ++i) {
2237 Map::set(items[i], _inv_map.size());
2238 _inv_map.push_back(items[i]);
2242 /// \brief Erase the key from the map.
2244 /// Erase the key from the map. It is called by the
2245 /// \c AlterationNotifier.
2246 virtual void erase(const Item& item) {
2247 Map::set(_inv_map.back(), Map::operator[](item));
2248 _inv_map[Map::operator[](item)] = _inv_map.back();
2249 _inv_map.pop_back();
2253 /// \brief Erase more keys from the map.
2255 /// Erase more keys from the map. It is called by the
2256 /// \c AlterationNotifier.
2257 virtual void erase(const std::vector<Item>& items) {
2258 for (int i = 0; i < int(items.size()); ++i) {
2259 Map::set(_inv_map.back(), Map::operator[](items[i]));
2260 _inv_map[Map::operator[](items[i])] = _inv_map.back();
2261 _inv_map.pop_back();
2266 /// \brief Build the unique map.
2268 /// Build the unique map. It is called by the
2269 /// \c AlterationNotifier.
2270 virtual void build() {
2273 const typename Map::Notifier* nf = Map::notifier();
2274 for (nf->first(it); it != INVALID; nf->next(it)) {
2275 Map::set(it, _inv_map.size());
2276 _inv_map.push_back(it);
2280 /// \brief Clear the keys from the map.
2282 /// Clear the keys from the map. It is called by the
2283 /// \c AlterationNotifier.
2284 virtual void clear() {
2291 /// \brief Returns the maximal value plus one.
2293 /// Returns the maximal value plus one in the map.
2294 unsigned int size() const {
2295 return _inv_map.size();
2298 /// \brief Swaps the position of the two items in the map.
2300 /// Swaps the position of the two items in the map.
2301 void swap(const Item& p, const Item& q) {
2302 int pi = Map::operator[](p);
2303 int qi = Map::operator[](q);
2310 /// \brief Gives back the \e range \e id of the item
2312 /// Gives back the \e range \e id of the item.
2313 int operator[](const Item& item) const {
2314 return Map::operator[](item);
2317 /// \brief Gives back the item belonging to a \e range \e id
2319 /// Gives back the item belonging to the given \e range \e id.
2320 Item operator()(int id) const {
2321 return _inv_map[id];
2326 typedef std::vector<Item> Container;
2331 /// \brief The inverse map type of RangeIdMap.
2333 /// The inverse map type of RangeIdMap. The subscript operator gives
2334 /// back an item by its \e range \e id.
2335 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2338 /// \brief Constructor
2340 /// Constructor of the InverseMap.
2341 explicit InverseMap(const RangeIdMap& inverted)
2342 : _inverted(inverted) {}
2345 /// The value type of the InverseMap.
2346 typedef typename RangeIdMap::Key Value;
2347 /// The key type of the InverseMap.
2348 typedef typename RangeIdMap::Value Key;
2350 /// \brief Subscript operator.
2352 /// Subscript operator. It gives back the item
2353 /// that the given \e range \e id currently belongs to.
2354 Value operator[](const Key& key) const {
2355 return _inverted(key);
2358 /// \brief Size of the map.
2360 /// Returns the size of the map.
2361 unsigned int size() const {
2362 return _inverted.size();
2366 const RangeIdMap& _inverted;
2369 /// \brief Gives back the inverse of the map.
2371 /// Gives back the inverse of the RangeIdMap.
2372 const InverseMap inverse() const {
2373 return InverseMap(*this);
2377 /// \brief Returns a \c RangeIdMap class.
2379 /// This function just returns an \c RangeIdMap class.
2380 /// \relates RangeIdMap
2381 template <typename K, typename GR>
2382 inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
2383 return RangeIdMap<GR, K>(graph);
2386 /// \brief Dynamic iterable \c bool map.
2388 /// This class provides a special graph map type which can store a
2389 /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
2390 /// For both \c true and \c false values it is possible to iterate on
2391 /// the keys mapped to the value.
2393 /// This type is a reference map, so it can be modified with the
2394 /// subscript operator.
2396 /// \tparam GR The graph type.
2397 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2400 /// \see IterableIntMap, IterableValueMap
2401 /// \see CrossRefMap
2402 template <typename GR, typename K>
2403 class IterableBoolMap
2404 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2408 typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
2409 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
2411 std::vector<K> _array;
2416 /// Indicates that the map is reference map.
2417 typedef True ReferenceMapTag;
2423 /// The const reference type.
2424 typedef const Value& ConstReference;
2428 int position(const Key& key) const {
2429 return Parent::operator[](key);
2434 /// \brief Reference to the value of the map.
2436 /// This class is similar to the \c bool type. It can be converted to
2437 /// \c bool and it provides the same operators.
2439 friend class IterableBoolMap;
2441 Reference(IterableBoolMap& map, const Key& key)
2442 : _key(key), _map(map) {}
2445 Reference& operator=(const Reference& value) {
2446 _map.set(_key, static_cast<bool>(value));
2450 operator bool() const {
2451 return static_cast<const IterableBoolMap&>(_map)[_key];
2454 Reference& operator=(bool value) {
2455 _map.set(_key, value);
2458 Reference& operator&=(bool value) {
2459 _map.set(_key, _map[_key] & value);
2462 Reference& operator|=(bool value) {
2463 _map.set(_key, _map[_key] | value);
2466 Reference& operator^=(bool value) {
2467 _map.set(_key, _map[_key] ^ value);
2472 IterableBoolMap& _map;
2475 /// \brief Constructor of the map with a default value.
2477 /// Constructor of the map with a default value.
2478 explicit IterableBoolMap(const Graph& graph, bool def = false)
2480 typename Parent::Notifier* nf = Parent::notifier();
2482 for (nf->first(it); it != INVALID; nf->next(it)) {
2483 Parent::set(it, _array.size());
2484 _array.push_back(it);
2486 _sep = (def ? _array.size() : 0);
2489 /// \brief Const subscript operator of the map.
2491 /// Const subscript operator of the map.
2492 bool operator[](const Key& key) const {
2493 return position(key) < _sep;
2496 /// \brief Subscript operator of the map.
2498 /// Subscript operator of the map.
2499 Reference operator[](const Key& key) {
2500 return Reference(*this, key);
2503 /// \brief Set operation of the map.
2505 /// Set operation of the map.
2506 void set(const Key& key, bool value) {
2507 int pos = position(key);
2509 if (pos < _sep) return;
2510 Key tmp = _array[_sep];
2512 Parent::set(key, _sep);
2514 Parent::set(tmp, pos);
2517 if (pos >= _sep) return;
2519 Key tmp = _array[_sep];
2521 Parent::set(key, _sep);
2523 Parent::set(tmp, pos);
2527 /// \brief Set all items.
2529 /// Set all items in the map.
2530 /// \note Constant time operation.
2531 void setAll(bool value) {
2532 _sep = (value ? _array.size() : 0);
2535 /// \brief Returns the number of the keys mapped to \c true.
2537 /// Returns the number of the keys mapped to \c true.
2538 int trueNum() const {
2542 /// \brief Returns the number of the keys mapped to \c false.
2544 /// Returns the number of the keys mapped to \c false.
2545 int falseNum() const {
2546 return _array.size() - _sep;
2549 /// \brief Iterator for the keys mapped to \c true.
2551 /// Iterator for the keys mapped to \c true. It works
2552 /// like a graph item iterator, it can be converted to
2553 /// the key type of the map, incremented with \c ++ operator, and
2554 /// if the iterator leaves the last valid key, it will be equal to
2556 class TrueIt : public Key {
2560 /// \brief Creates an iterator.
2562 /// Creates an iterator. It iterates on the
2563 /// keys mapped to \c true.
2564 /// \param map The IterableBoolMap.
2565 explicit TrueIt(const IterableBoolMap& map)
2566 : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2569 /// \brief Invalid constructor \& conversion.
2571 /// This constructor initializes the iterator to be invalid.
2572 /// \sa Invalid for more details.
2573 TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2575 /// \brief Increment operator.
2577 /// Increment operator.
2578 TrueIt& operator++() {
2579 int pos = _map->position(*this);
2580 Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
2585 const IterableBoolMap* _map;
2588 /// \brief STL style iterator for the keys mapped to \c true.
2590 /// This is an STL style wrapper for \ref TrueIt.
2591 /// It can be used in range-based for loops, STL algorithms, etc.
2592 LemonRangeWrapper1<TrueIt, IterableBoolMap>
2594 return LemonRangeWrapper1<TrueIt, IterableBoolMap>(*this);
2598 /// \brief Iterator for the keys mapped to \c false.
2600 /// Iterator for the keys mapped to \c false. It works
2601 /// like a graph item iterator, it can be converted to
2602 /// the key type of the map, incremented with \c ++ operator, and
2603 /// if the iterator leaves the last valid key, it will be equal to
2605 class FalseIt : public Key {
2609 /// \brief Creates an iterator.
2611 /// Creates an iterator. It iterates on the
2612 /// keys mapped to \c false.
2613 /// \param map The IterableBoolMap.
2614 explicit FalseIt(const IterableBoolMap& map)
2615 : Parent(map._sep < int(map._array.size()) ?
2616 map._array.back() : INVALID), _map(&map) {}
2618 /// \brief Invalid constructor \& conversion.
2620 /// This constructor initializes the iterator to be invalid.
2621 /// \sa Invalid for more details.
2622 FalseIt(Invalid) : Parent(INVALID), _map(0) {}
2624 /// \brief Increment operator.
2626 /// Increment operator.
2627 FalseIt& operator++() {
2628 int pos = _map->position(*this);
2629 Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
2634 const IterableBoolMap* _map;
2637 /// \brief STL style iterator for the keys mapped to \c false.
2639 /// This is an STL style wrapper for \ref FalseIt.
2640 /// It can be used in range-based for loops, STL algorithms, etc.
2641 LemonRangeWrapper1<FalseIt, IterableBoolMap>
2643 return LemonRangeWrapper1<FalseIt, IterableBoolMap>(*this);
2647 /// \brief Iterator for the keys mapped to a given value.
2649 /// Iterator for the keys mapped to a given value. It works
2650 /// like a graph item iterator, it can be converted to
2651 /// the key type of the map, incremented with \c ++ operator, and
2652 /// if the iterator leaves the last valid key, it will be equal to
2654 class ItemIt : public Key {
2658 /// \brief Creates an iterator with a value.
2660 /// Creates an iterator with a value. It iterates on the
2661 /// keys mapped to the given value.
2662 /// \param map The IterableBoolMap.
2663 /// \param value The value.
2664 ItemIt(const IterableBoolMap& map, bool value)
2667 map._array[map._sep - 1] : INVALID) :
2668 (map._sep < int(map._array.size()) ?
2669 map._array.back() : INVALID)), _map(&map) {}
2671 /// \brief Invalid constructor \& conversion.
2673 /// This constructor initializes the iterator to be invalid.
2674 /// \sa Invalid for more details.
2675 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2677 /// \brief Increment operator.
2679 /// Increment operator.
2680 ItemIt& operator++() {
2681 int pos = _map->position(*this);
2682 int _sep = pos >= _map->_sep ? _map->_sep : 0;
2683 Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
2688 const IterableBoolMap* _map;
2691 /// \brief STL style iterator for the keys mapped to a given value.
2693 /// This is an STL style wrapper for \ref ItemIt.
2694 /// It can be used in range-based for loops, STL algorithms, etc.
2695 LemonRangeWrapper2<ItemIt, IterableBoolMap, bool>
2697 return LemonRangeWrapper2<ItemIt, IterableBoolMap, bool>(*this, value);
2702 virtual void add(const Key& key) {
2704 Parent::set(key, _array.size());
2705 _array.push_back(key);
2708 virtual void add(const std::vector<Key>& keys) {
2710 for (int i = 0; i < int(keys.size()); ++i) {
2711 Parent::set(keys[i], _array.size());
2712 _array.push_back(keys[i]);
2716 virtual void erase(const Key& key) {
2717 int pos = position(key);
2720 Parent::set(_array[_sep], pos);
2721 _array[pos] = _array[_sep];
2722 Parent::set(_array.back(), _sep);
2723 _array[_sep] = _array.back();
2726 Parent::set(_array.back(), pos);
2727 _array[pos] = _array.back();
2733 virtual void erase(const std::vector<Key>& keys) {
2734 for (int i = 0; i < int(keys.size()); ++i) {
2735 int pos = position(keys[i]);
2738 Parent::set(_array[_sep], pos);
2739 _array[pos] = _array[_sep];
2740 Parent::set(_array.back(), _sep);
2741 _array[_sep] = _array.back();
2744 Parent::set(_array.back(), pos);
2745 _array[pos] = _array.back();
2749 Parent::erase(keys);
2752 virtual void build() {
2754 typename Parent::Notifier* nf = Parent::notifier();
2756 for (nf->first(it); it != INVALID; nf->next(it)) {
2757 Parent::set(it, _array.size());
2758 _array.push_back(it);
2763 virtual void clear() {
2772 namespace _maps_bits {
2773 template <typename Item>
2774 struct IterableIntMapNode {
2775 IterableIntMapNode() : value(-1) {}
2776 IterableIntMapNode(int _value) : value(_value) {}
2782 /// \brief Dynamic iterable integer map.
2784 /// This class provides a special graph map type which can store an
2785 /// integer value for graph items (\c Node, \c Arc or \c Edge).
2786 /// For each non-negative value it is possible to iterate on the keys
2787 /// mapped to the value.
2789 /// This map is intended to be used with small integer values, for which
2790 /// it is efficient, and supports iteration only for non-negative values.
2791 /// If you need large values and/or iteration for negative integers,
2792 /// consider to use \ref IterableValueMap instead.
2794 /// This type is a reference map, so it can be modified with the
2795 /// subscript operator.
2797 /// \note The size of the data structure depends on the largest
2798 /// value in the map.
2800 /// \tparam GR The graph type.
2801 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2804 /// \see IterableBoolMap, IterableValueMap
2805 /// \see CrossRefMap
2806 template <typename GR, typename K>
2807 class IterableIntMap
2808 : protected ItemSetTraits<GR, K>::
2809 template Map<_maps_bits::IterableIntMapNode<K> >::Type {
2811 typedef typename ItemSetTraits<GR, K>::
2812 template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
2821 /// \brief Constructor of the map.
2823 /// Constructor of the map. It sets all values to -1.
2824 explicit IterableIntMap(const Graph& graph)
2827 /// \brief Constructor of the map with a given value.
2829 /// Constructor of the map with a given value.
2830 explicit IterableIntMap(const Graph& graph, int value)
2831 : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
2833 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
2841 void unlace(const Key& key) {
2842 typename Parent::Value& node = Parent::operator[](key);
2843 if (node.value < 0) return;
2844 if (node.prev != INVALID) {
2845 Parent::operator[](node.prev).next = node.next;
2847 _first[node.value] = node.next;
2849 if (node.next != INVALID) {
2850 Parent::operator[](node.next).prev = node.prev;
2852 while (!_first.empty() && _first.back() == INVALID) {
2857 void lace(const Key& key) {
2858 typename Parent::Value& node = Parent::operator[](key);
2859 if (node.value < 0) return;
2860 if (node.value >= int(_first.size())) {
2861 _first.resize(node.value + 1, INVALID);
2863 node.prev = INVALID;
2864 node.next = _first[node.value];
2865 if (node.next != INVALID) {
2866 Parent::operator[](node.next).prev = key;
2868 _first[node.value] = key;
2873 /// Indicates that the map is reference map.
2874 typedef True ReferenceMapTag;
2876 /// \brief Reference to the value of the map.
2878 /// This class is similar to the \c int type. It can
2879 /// be converted to \c int and it has the same operators.
2881 friend class IterableIntMap;
2883 Reference(IterableIntMap& map, const Key& key)
2884 : _key(key), _map(map) {}
2887 Reference& operator=(const Reference& value) {
2888 _map.set(_key, static_cast<const int&>(value));
2892 operator const int&() const {
2893 return static_cast<const IterableIntMap&>(_map)[_key];
2896 Reference& operator=(int value) {
2897 _map.set(_key, value);
2900 Reference& operator++() {
2901 _map.set(_key, _map[_key] + 1);
2904 int operator++(int) {
2905 int value = _map[_key];
2906 _map.set(_key, value + 1);
2909 Reference& operator--() {
2910 _map.set(_key, _map[_key] - 1);
2913 int operator--(int) {
2914 int value = _map[_key];
2915 _map.set(_key, value - 1);
2918 Reference& operator+=(int value) {
2919 _map.set(_key, _map[_key] + value);
2922 Reference& operator-=(int value) {
2923 _map.set(_key, _map[_key] - value);
2926 Reference& operator*=(int value) {
2927 _map.set(_key, _map[_key] * value);
2930 Reference& operator/=(int value) {
2931 _map.set(_key, _map[_key] / value);
2934 Reference& operator%=(int value) {
2935 _map.set(_key, _map[_key] % value);
2938 Reference& operator&=(int value) {
2939 _map.set(_key, _map[_key] & value);
2942 Reference& operator|=(int value) {
2943 _map.set(_key, _map[_key] | value);
2946 Reference& operator^=(int value) {
2947 _map.set(_key, _map[_key] ^ value);
2950 Reference& operator<<=(int value) {
2951 _map.set(_key, _map[_key] << value);
2954 Reference& operator>>=(int value) {
2955 _map.set(_key, _map[_key] >> value);
2961 IterableIntMap& _map;
2964 /// The const reference type.
2965 typedef const Value& ConstReference;
2967 /// \brief Gives back the maximal value plus one.
2969 /// Gives back the maximal value plus one.
2971 return _first.size();
2974 /// \brief Set operation of the map.
2976 /// Set operation of the map.
2977 void set(const Key& key, const Value& value) {
2979 Parent::operator[](key).value = value;
2983 /// \brief Const subscript operator of the map.
2985 /// Const subscript operator of the map.
2986 const Value& operator[](const Key& key) const {
2987 return Parent::operator[](key).value;
2990 /// \brief Subscript operator of the map.
2992 /// Subscript operator of the map.
2993 Reference operator[](const Key& key) {
2994 return Reference(*this, key);
2997 /// \brief Iterator for the keys with the same value.
2999 /// Iterator for the keys with the same value. It works
3000 /// like a graph item iterator, it can be converted to
3001 /// the item type of the map, incremented with \c ++ operator, and
3002 /// if the iterator leaves the last valid item, it will be equal to
3004 class ItemIt : public Key {
3008 /// \brief Invalid constructor \& conversion.
3010 /// This constructor initializes the iterator to be invalid.
3011 /// \sa Invalid for more details.
3012 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3014 /// \brief Creates an iterator with a value.
3016 /// Creates an iterator with a value. It iterates on the
3017 /// keys mapped to the given value.
3018 /// \param map The IterableIntMap.
3019 /// \param value The value.
3020 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
3021 if (value < 0 || value >= int(_map->_first.size())) {
3022 Parent::operator=(INVALID);
3024 Parent::operator=(_map->_first[value]);
3028 /// \brief Increment operator.
3030 /// Increment operator.
3031 ItemIt& operator++() {
3032 Parent::operator=(_map->IterableIntMap::Parent::
3033 operator[](static_cast<Parent&>(*this)).next);
3038 const IterableIntMap* _map;
3041 /// \brief STL style iterator for the keys with the same value.
3043 /// This is an STL style wrapper for \ref ItemIt.
3044 /// It can be used in range-based for loops, STL algorithms, etc.
3045 LemonRangeWrapper2<ItemIt, IterableIntMap, int>
3047 return LemonRangeWrapper2<ItemIt, IterableIntMap, int>(*this, value);
3053 virtual void erase(const Key& key) {
3058 virtual void erase(const std::vector<Key>& keys) {
3059 for (int i = 0; i < int(keys.size()); ++i) {
3062 Parent::erase(keys);
3065 virtual void clear() {
3071 std::vector<Key> _first;
3074 namespace _maps_bits {
3075 template <typename Item, typename Value>
3076 struct IterableValueMapNode {
3077 IterableValueMapNode(Value _value = Value()) : value(_value) {}
3083 /// \brief Dynamic iterable map for comparable values.
3085 /// This class provides a special graph map type which can store a
3086 /// comparable value for graph items (\c Node, \c Arc or \c Edge).
3087 /// For each value it is possible to iterate on the keys mapped to
3088 /// the value (\c ItemIt), and the values of the map can be accessed
3089 /// with an STL compatible forward iterator (\c ValueIt).
3090 /// The map stores a linked list for each value, which contains
3091 /// the items mapped to the value, and the used values are stored
3092 /// in balanced binary tree (\c std::map).
3094 /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
3095 /// specialized for \c bool and \c int values, respectively.
3097 /// This type is not reference map, so it cannot be modified with
3098 /// the subscript operator.
3100 /// \tparam GR The graph type.
3101 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
3103 /// \tparam V The value type of the map. It can be any comparable
3106 /// \see IterableBoolMap, IterableIntMap
3107 /// \see CrossRefMap
3108 template <typename GR, typename K, typename V>
3109 class IterableValueMap
3110 : protected ItemSetTraits<GR, K>::
3111 template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
3113 typedef typename ItemSetTraits<GR, K>::
3114 template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
3125 /// \brief Constructor of the map with a given value.
3127 /// Constructor of the map with a given value.
3128 explicit IterableValueMap(const Graph& graph,
3129 const Value& value = Value())
3130 : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
3131 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3138 void unlace(const Key& key) {
3139 typename Parent::Value& node = Parent::operator[](key);
3140 if (node.prev != INVALID) {
3141 Parent::operator[](node.prev).next = node.next;
3143 if (node.next != INVALID) {
3144 _first[node.value] = node.next;
3146 _first.erase(node.value);
3149 if (node.next != INVALID) {
3150 Parent::operator[](node.next).prev = node.prev;
3154 void lace(const Key& key) {
3155 typename Parent::Value& node = Parent::operator[](key);
3156 typename std::map<Value, Key>::iterator it = _first.find(node.value);
3157 if (it == _first.end()) {
3158 node.prev = node.next = INVALID;
3159 _first.insert(std::make_pair(node.value, key));
3161 node.prev = INVALID;
3162 node.next = it->second;
3163 if (node.next != INVALID) {
3164 Parent::operator[](node.next).prev = key;
3172 /// \brief Forward iterator for values.
3174 /// This iterator is an STL compatible forward
3175 /// iterator on the values of the map. The values can
3176 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
3178 : public std::iterator<std::forward_iterator_tag, Value> {
3179 friend class IterableValueMap;
3181 ValueIt(typename std::map<Value, Key>::const_iterator _it)
3189 ValueIt& operator++() { ++it; return *this; }
3191 ValueIt operator++(int) {
3198 const Value& operator*() const { return it->first; }
3200 const Value* operator->() const { return &(it->first); }
3203 bool operator==(ValueIt jt) const { return it == jt.it; }
3205 bool operator!=(ValueIt jt) const { return it != jt.it; }
3208 typename std::map<Value, Key>::const_iterator it;
3211 /// \brief Returns an iterator to the first value.
3213 /// Returns an STL compatible iterator to the
3214 /// first value of the map. The values of the
3215 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3217 ValueIt beginValue() const {
3218 return ValueIt(_first.begin());
3221 /// \brief Returns an iterator after the last value.
3223 /// Returns an STL compatible iterator after the
3224 /// last value of the map. The values of the
3225 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3227 ValueIt endValue() const {
3228 return ValueIt(_first.end());
3231 /// \brief Set operation of the map.
3233 /// Set operation of the map.
3234 void set(const Key& key, const Value& value) {
3236 Parent::operator[](key).value = value;
3240 /// \brief Const subscript operator of the map.
3242 /// Const subscript operator of the map.
3243 const Value& operator[](const Key& key) const {
3244 return Parent::operator[](key).value;
3247 /// \brief Iterator for the keys with the same value.
3249 /// Iterator for the keys with the same value. It works
3250 /// like a graph item iterator, it can be converted to
3251 /// the item type of the map, incremented with \c ++ operator, and
3252 /// if the iterator leaves the last valid item, it will be equal to
3254 class ItemIt : public Key {
3258 /// \brief Invalid constructor \& conversion.
3260 /// This constructor initializes the iterator to be invalid.
3261 /// \sa Invalid for more details.
3262 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3264 /// \brief Creates an iterator with a value.
3266 /// Creates an iterator with a value. It iterates on the
3267 /// keys which have the given value.
3268 /// \param map The IterableValueMap
3269 /// \param value The value
3270 ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
3271 typename std::map<Value, Key>::const_iterator it =
3272 map._first.find(value);
3273 if (it == map._first.end()) {
3274 Parent::operator=(INVALID);
3276 Parent::operator=(it->second);
3280 /// \brief Increment operator.
3282 /// Increment Operator.
3283 ItemIt& operator++() {
3284 Parent::operator=(_map->IterableValueMap::Parent::
3285 operator[](static_cast<Parent&>(*this)).next);
3291 const IterableValueMap* _map;
3294 /// \brief STL style iterator for the keys with the same value.
3296 /// This is an STL style wrapper for \ref ItemIt.
3297 /// It can be used in range-based for loops, STL algorithms, etc.
3298 LemonRangeWrapper2<ItemIt, IterableValueMap, V>
3299 items(const V& value) {
3300 return LemonRangeWrapper2<ItemIt, IterableValueMap, V>(*this, value);
3306 virtual void add(const Key& key) {
3311 virtual void add(const std::vector<Key>& keys) {
3313 for (int i = 0; i < int(keys.size()); ++i) {
3318 virtual void erase(const Key& key) {
3323 virtual void erase(const std::vector<Key>& keys) {
3324 for (int i = 0; i < int(keys.size()); ++i) {
3327 Parent::erase(keys);
3330 virtual void build() {
3332 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3337 virtual void clear() {
3343 std::map<Value, Key> _first;
3346 /// \brief Map of the source nodes of arcs in a digraph.
3348 /// SourceMap provides access for the source node of each arc in a digraph,
3349 /// which is returned by the \c source() function of the digraph.
3350 /// \tparam GR The digraph type.
3352 template <typename GR>
3356 /// The key type (the \c Arc type of the digraph).
3357 typedef typename GR::Arc Key;
3358 /// The value type (the \c Node type of the digraph).
3359 typedef typename GR::Node Value;
3361 /// \brief Constructor
3364 /// \param digraph The digraph that the map belongs to.
3365 explicit SourceMap(const GR& digraph) : _graph(digraph) {}
3367 /// \brief Returns the source node of the given arc.
3369 /// Returns the source node of the given arc.
3370 Value operator[](const Key& arc) const {
3371 return _graph.source(arc);
3378 /// \brief Returns a \c SourceMap class.
3380 /// This function just returns an \c SourceMap class.
3381 /// \relates SourceMap
3382 template <typename GR>
3383 inline SourceMap<GR> sourceMap(const GR& graph) {
3384 return SourceMap<GR>(graph);
3387 /// \brief Map of the target nodes of arcs in a digraph.
3389 /// TargetMap provides access for the target node of each arc in a digraph,
3390 /// which is returned by the \c target() function of the digraph.
3391 /// \tparam GR The digraph type.
3393 template <typename GR>
3397 /// The key type (the \c Arc type of the digraph).
3398 typedef typename GR::Arc Key;
3399 /// The value type (the \c Node type of the digraph).
3400 typedef typename GR::Node Value;
3402 /// \brief Constructor
3405 /// \param digraph The digraph that the map belongs to.
3406 explicit TargetMap(const GR& digraph) : _graph(digraph) {}
3408 /// \brief Returns the target node of the given arc.
3410 /// Returns the target node of the given arc.
3411 Value operator[](const Key& e) const {
3412 return _graph.target(e);
3419 /// \brief Returns a \c TargetMap class.
3421 /// This function just returns a \c TargetMap class.
3422 /// \relates TargetMap
3423 template <typename GR>
3424 inline TargetMap<GR> targetMap(const GR& graph) {
3425 return TargetMap<GR>(graph);
3428 /// \brief Map of the "forward" directed arc view of edges in a graph.
3430 /// ForwardMap provides access for the "forward" directed arc view of
3431 /// each edge in a graph, which is returned by the \c direct() function
3432 /// of the graph with \c true parameter.
3433 /// \tparam GR The graph type.
3434 /// \see BackwardMap
3435 template <typename GR>
3439 /// The key type (the \c Edge type of the digraph).
3440 typedef typename GR::Edge Key;
3441 /// The value type (the \c Arc type of the digraph).
3442 typedef typename GR::Arc Value;
3444 /// \brief Constructor
3447 /// \param graph The graph that the map belongs to.
3448 explicit ForwardMap(const GR& graph) : _graph(graph) {}
3450 /// \brief Returns the "forward" directed arc view of the given edge.
3452 /// Returns the "forward" directed arc view of the given edge.
3453 Value operator[](const Key& key) const {
3454 return _graph.direct(key, true);
3461 /// \brief Returns a \c ForwardMap class.
3463 /// This function just returns an \c ForwardMap class.
3464 /// \relates ForwardMap
3465 template <typename GR>
3466 inline ForwardMap<GR> forwardMap(const GR& graph) {
3467 return ForwardMap<GR>(graph);
3470 /// \brief Map of the "backward" directed arc view of edges in a graph.
3472 /// BackwardMap provides access for the "backward" directed arc view of
3473 /// each edge in a graph, which is returned by the \c direct() function
3474 /// of the graph with \c false parameter.
3475 /// \tparam GR The graph type.
3477 template <typename GR>
3481 /// The key type (the \c Edge type of the digraph).
3482 typedef typename GR::Edge Key;
3483 /// The value type (the \c Arc type of the digraph).
3484 typedef typename GR::Arc Value;
3486 /// \brief Constructor
3489 /// \param graph The graph that the map belongs to.
3490 explicit BackwardMap(const GR& graph) : _graph(graph) {}
3492 /// \brief Returns the "backward" directed arc view of the given edge.
3494 /// Returns the "backward" directed arc view of the given edge.
3495 Value operator[](const Key& key) const {
3496 return _graph.direct(key, false);
3503 /// \brief Returns a \c BackwardMap class
3505 /// This function just returns a \c BackwardMap class.
3506 /// \relates BackwardMap
3507 template <typename GR>
3508 inline BackwardMap<GR> backwardMap(const GR& graph) {
3509 return BackwardMap<GR>(graph);
3512 /// \brief Map of the in-degrees of nodes in a digraph.
3514 /// This map returns the in-degree of a node. Once it is constructed,
3515 /// the degrees are stored in a standard \c NodeMap, so each query is done
3516 /// in constant time. On the other hand, the values are updated automatically
3517 /// whenever the digraph changes.
3519 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3520 /// may provide alternative ways to modify the digraph.
3521 /// The correct behavior of InDegMap is not guarantied if these additional
3522 /// features are used. For example, the functions
3523 /// \ref ListDigraph::changeSource() "changeSource()",
3524 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3525 /// \ref ListDigraph::reverseArc() "reverseArc()"
3526 /// of \ref ListDigraph will \e not update the degree values correctly.
3529 template <typename GR>
3531 : protected ItemSetTraits<GR, typename GR::Arc>
3532 ::ItemNotifier::ObserverBase {
3536 /// The graph type of InDegMap
3540 typedef typename Digraph::Node Key;
3544 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3545 ::ItemNotifier::ObserverBase Parent;
3550 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3553 typedef typename ItemSetTraits<Digraph, Key>::
3554 template Map<int>::Type Parent;
3556 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3558 virtual void add(const Key& key) {
3560 Parent::set(key, 0);
3563 virtual void add(const std::vector<Key>& keys) {
3565 for (int i = 0; i < int(keys.size()); ++i) {
3566 Parent::set(keys[i], 0);
3570 virtual void build() {
3573 typename Parent::Notifier* nf = Parent::notifier();
3574 for (nf->first(it); it != INVALID; nf->next(it)) {
3582 /// \brief Constructor.
3584 /// Constructor for creating an in-degree map.
3585 explicit InDegMap(const Digraph& graph)
3586 : _digraph(graph), _deg(graph) {
3587 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3589 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3590 _deg[it] = countInArcs(_digraph, it);
3594 /// \brief Gives back the in-degree of a Node.
3596 /// Gives back the in-degree of a Node.
3597 int operator[](const Key& key) const {
3603 typedef typename Digraph::Arc Arc;
3605 virtual void add(const Arc& arc) {
3606 ++_deg[_digraph.target(arc)];
3609 virtual void add(const std::vector<Arc>& arcs) {
3610 for (int i = 0; i < int(arcs.size()); ++i) {
3611 ++_deg[_digraph.target(arcs[i])];
3615 virtual void erase(const Arc& arc) {
3616 --_deg[_digraph.target(arc)];
3619 virtual void erase(const std::vector<Arc>& arcs) {
3620 for (int i = 0; i < int(arcs.size()); ++i) {
3621 --_deg[_digraph.target(arcs[i])];
3625 virtual void build() {
3626 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3627 _deg[it] = countInArcs(_digraph, it);
3631 virtual void clear() {
3632 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3638 const Digraph& _digraph;
3642 /// \brief Map of the out-degrees of nodes in a digraph.
3644 /// This map returns the out-degree of a node. Once it is constructed,
3645 /// the degrees are stored in a standard \c NodeMap, so each query is done
3646 /// in constant time. On the other hand, the values are updated automatically
3647 /// whenever the digraph changes.
3649 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3650 /// may provide alternative ways to modify the digraph.
3651 /// The correct behavior of OutDegMap is not guarantied if these additional
3652 /// features are used. For example, the functions
3653 /// \ref ListDigraph::changeSource() "changeSource()",
3654 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3655 /// \ref ListDigraph::reverseArc() "reverseArc()"
3656 /// of \ref ListDigraph will \e not update the degree values correctly.
3659 template <typename GR>
3661 : protected ItemSetTraits<GR, typename GR::Arc>
3662 ::ItemNotifier::ObserverBase {
3666 /// The graph type of OutDegMap
3670 typedef typename Digraph::Node Key;
3674 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3675 ::ItemNotifier::ObserverBase Parent;
3680 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3683 typedef typename ItemSetTraits<Digraph, Key>::
3684 template Map<int>::Type Parent;
3686 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3688 virtual void add(const Key& key) {
3690 Parent::set(key, 0);
3692 virtual void add(const std::vector<Key>& keys) {
3694 for (int i = 0; i < int(keys.size()); ++i) {
3695 Parent::set(keys[i], 0);
3698 virtual void build() {
3701 typename Parent::Notifier* nf = Parent::notifier();
3702 for (nf->first(it); it != INVALID; nf->next(it)) {
3710 /// \brief Constructor.
3712 /// Constructor for creating an out-degree map.
3713 explicit OutDegMap(const Digraph& graph)
3714 : _digraph(graph), _deg(graph) {
3715 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3717 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3718 _deg[it] = countOutArcs(_digraph, it);
3722 /// \brief Gives back the out-degree of a Node.
3724 /// Gives back the out-degree of a Node.
3725 int operator[](const Key& key) const {
3731 typedef typename Digraph::Arc Arc;
3733 virtual void add(const Arc& arc) {
3734 ++_deg[_digraph.source(arc)];
3737 virtual void add(const std::vector<Arc>& arcs) {
3738 for (int i = 0; i < int(arcs.size()); ++i) {
3739 ++_deg[_digraph.source(arcs[i])];
3743 virtual void erase(const Arc& arc) {
3744 --_deg[_digraph.source(arc)];
3747 virtual void erase(const std::vector<Arc>& arcs) {
3748 for (int i = 0; i < int(arcs.size()); ++i) {
3749 --_deg[_digraph.source(arcs[i])];
3753 virtual void build() {
3754 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3755 _deg[it] = countOutArcs(_digraph, it);
3759 virtual void clear() {
3760 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3766 const Digraph& _digraph;
3770 /// \brief Potential difference map
3772 /// PotentialDifferenceMap returns the difference between the potentials of
3773 /// the source and target nodes of each arc in a digraph, i.e. it returns
3775 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3777 /// \tparam GR The digraph type.
3778 /// \tparam POT A node map storing the potentials.
3779 template <typename GR, typename POT>
3780 class PotentialDifferenceMap {
3783 typedef typename GR::Arc Key;
3785 typedef typename POT::Value Value;
3787 /// \brief Constructor
3789 /// Contructor of the map.
3790 explicit PotentialDifferenceMap(const GR& gr,
3791 const POT& potential)
3792 : _digraph(gr), _potential(potential) {}
3794 /// \brief Returns the potential difference for the given arc.
3796 /// Returns the potential difference for the given arc, i.e.
3798 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3800 Value operator[](const Key& arc) const {
3801 return _potential[_digraph.target(arc)] -
3802 _potential[_digraph.source(arc)];
3807 const POT& _potential;
3810 /// \brief Returns a PotentialDifferenceMap.
3812 /// This function just returns a PotentialDifferenceMap.
3813 /// \relates PotentialDifferenceMap
3814 template <typename GR, typename POT>
3815 PotentialDifferenceMap<GR, POT>
3816 potentialDifferenceMap(const GR& gr, const POT& potential) {
3817 return PotentialDifferenceMap<GR, POT>(gr, potential);
3821 /// \brief Copy the values of a graph map to another map.
3823 /// This function copies the values of a graph map to another graph map.
3824 /// \c To::Key must be equal or convertible to \c From::Key and
3825 /// \c From::Value must be equal or convertible to \c To::Value.
3827 /// For example, an edge map of \c int value type can be copied to
3828 /// an arc map of \c double value type in an undirected graph, but
3829 /// an arc map cannot be copied to an edge map.
3830 /// Note that even a \ref ConstMap can be copied to a standard graph map,
3831 /// but \ref mapFill() can also be used for this purpose.
3833 /// \param gr The graph for which the maps are defined.
3834 /// \param from The map from which the values have to be copied.
3835 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
3836 /// \param to The map to which the values have to be copied.
3837 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3838 template <typename GR, typename From, typename To>
3839 void mapCopy(const GR& gr, const From& from, To& to) {
3840 typedef typename To::Key Item;
3841 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3843 for (ItemIt it(gr); it != INVALID; ++it) {
3844 to.set(it, from[it]);
3848 /// \brief Compare two graph maps.
3850 /// This function compares the values of two graph maps. It returns
3851 /// \c true if the maps assign the same value for all items in the graph.
3852 /// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
3853 /// and their \c Value types must be comparable using \c %operator==().
3855 /// \param gr The graph for which the maps are defined.
3856 /// \param map1 The first map.
3857 /// \param map2 The second map.
3858 template <typename GR, typename Map1, typename Map2>
3859 bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
3860 typedef typename Map2::Key Item;
3861 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3863 for (ItemIt it(gr); it != INVALID; ++it) {
3864 if (!(map1[it] == map2[it])) return false;
3869 /// \brief Return an item having minimum value of a graph map.
3871 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3872 /// minimum value of the given graph map.
3873 /// If the item set is empty, it returns \c INVALID.
3875 /// \param gr The graph for which the map is defined.
3876 /// \param map The graph map.
3877 template <typename GR, typename Map>
3878 typename Map::Key mapMin(const GR& gr, const Map& map) {
3879 return mapMin(gr, map, std::less<typename Map::Value>());
3882 /// \brief Return an item having minimum value of a graph map.
3884 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3885 /// minimum value of the given graph map.
3886 /// If the item set is empty, it returns \c INVALID.
3888 /// \param gr The graph for which the map is defined.
3889 /// \param map The graph map.
3890 /// \param comp Comparison function object.
3891 template <typename GR, typename Map, typename Comp>
3892 typename Map::Key mapMin(const GR& gr, const Map& map, const Comp& comp) {
3893 typedef typename Map::Key Item;
3894 typedef typename Map::Value Value;
3895 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3897 ItemIt min_item(gr);
3898 if (min_item == INVALID) return INVALID;
3899 Value min = map[min_item];
3900 for (ItemIt it(gr); it != INVALID; ++it) {
3901 if (comp(map[it], min)) {
3909 /// \brief Return an item having maximum value of a graph map.
3911 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3912 /// maximum value of the given graph map.
3913 /// If the item set is empty, it returns \c INVALID.
3915 /// \param gr The graph for which the map is defined.
3916 /// \param map The graph map.
3917 template <typename GR, typename Map>
3918 typename Map::Key mapMax(const GR& gr, const Map& map) {
3919 return mapMax(gr, map, std::less<typename Map::Value>());
3922 /// \brief Return an item having maximum value of a graph map.
3924 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3925 /// maximum value of the given graph map.
3926 /// If the item set is empty, it returns \c INVALID.
3928 /// \param gr The graph for which the map is defined.
3929 /// \param map The graph map.
3930 /// \param comp Comparison function object.
3931 template <typename GR, typename Map, typename Comp>
3932 typename Map::Key mapMax(const GR& gr, const Map& map, const Comp& comp) {
3933 typedef typename Map::Key Item;
3934 typedef typename Map::Value Value;
3935 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3937 ItemIt max_item(gr);
3938 if (max_item == INVALID) return INVALID;
3939 Value max = map[max_item];
3940 for (ItemIt it(gr); it != INVALID; ++it) {
3941 if (comp(max, map[it])) {
3949 /// \brief Return the minimum value of a graph map.
3951 /// This function returns the minimum value of the given graph map.
3952 /// The corresponding item set of the graph must not be empty.
3954 /// \param gr The graph for which the map is defined.
3955 /// \param map The graph map.
3956 template <typename GR, typename Map>
3957 typename Map::Value mapMinValue(const GR& gr, const Map& map) {
3958 return map[mapMin(gr, map, std::less<typename Map::Value>())];
3961 /// \brief Return the minimum value of a graph map.
3963 /// This function returns the minimum value of the given graph map.
3964 /// The corresponding item set of the graph must not be empty.
3966 /// \param gr The graph for which the map is defined.
3967 /// \param map The graph map.
3968 /// \param comp Comparison function object.
3969 template <typename GR, typename Map, typename Comp>
3971 mapMinValue(const GR& gr, const Map& map, const Comp& comp) {
3972 return map[mapMin(gr, map, comp)];
3975 /// \brief Return the maximum value of a graph map.
3977 /// This function returns the maximum value of the given graph map.
3978 /// The corresponding item set of the graph must not be empty.
3980 /// \param gr The graph for which the map is defined.
3981 /// \param map The graph map.
3982 template <typename GR, typename Map>
3983 typename Map::Value mapMaxValue(const GR& gr, const Map& map) {
3984 return map[mapMax(gr, map, std::less<typename Map::Value>())];
3987 /// \brief Return the maximum value of a graph map.
3989 /// This function returns the maximum value of the given graph map.
3990 /// The corresponding item set of the graph must not be empty.
3992 /// \param gr The graph for which the map is defined.
3993 /// \param map The graph map.
3994 /// \param comp Comparison function object.
3995 template <typename GR, typename Map, typename Comp>
3997 mapMaxValue(const GR& gr, const Map& map, const Comp& comp) {
3998 return map[mapMax(gr, map, comp)];
4001 /// \brief Return an item having a specified value in a graph map.
4003 /// This function returns an item (\c Node, \c Arc or \c Edge) having
4004 /// the specified assigned value in the given graph map.
4005 /// If no such item exists, it returns \c INVALID.
4007 /// \param gr The graph for which the map is defined.
4008 /// \param map The graph map.
4009 /// \param val The value that have to be found.
4010 template <typename GR, typename Map>
4012 mapFind(const GR& gr, const Map& map, const typename Map::Value& val) {
4013 typedef typename Map::Key Item;
4014 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4016 for (ItemIt it(gr); it != INVALID; ++it) {
4017 if (map[it] == val) return it;
4022 /// \brief Return an item having value for which a certain predicate is
4023 /// true in a graph map.
4025 /// This function returns an item (\c Node, \c Arc or \c Edge) having
4026 /// such assigned value for which the specified predicate is true
4027 /// in the given graph map.
4028 /// If no such item exists, it returns \c INVALID.
4030 /// \param gr The graph for which the map is defined.
4031 /// \param map The graph map.
4032 /// \param pred The predicate function object.
4033 template <typename GR, typename Map, typename Pred>
4035 mapFindIf(const GR& gr, const Map& map, const Pred& pred) {
4036 typedef typename Map::Key Item;
4037 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4039 for (ItemIt it(gr); it != INVALID; ++it) {
4040 if (pred(map[it])) return it;
4045 /// \brief Return the number of items having a specified value in a
4048 /// This function returns the number of items (\c Node, \c Arc or \c Edge)
4049 /// having the specified assigned value in the given graph map.
4051 /// \param gr The graph for which the map is defined.
4052 /// \param map The graph map.
4053 /// \param val The value that have to be counted.
4054 template <typename GR, typename Map>
4055 int mapCount(const GR& gr, const Map& map, const typename Map::Value& val) {
4056 typedef typename Map::Key Item;
4057 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4060 for (ItemIt it(gr); it != INVALID; ++it) {
4061 if (map[it] == val) ++cnt;
4066 /// \brief Return the number of items having values for which a certain
4067 /// predicate is true in a graph map.
4069 /// This function returns the number of items (\c Node, \c Arc or \c Edge)
4070 /// having such assigned values for which the specified predicate is true
4071 /// in the given graph map.
4073 /// \param gr The graph for which the map is defined.
4074 /// \param map The graph map.
4075 /// \param pred The predicate function object.
4076 template <typename GR, typename Map, typename Pred>
4077 int mapCountIf(const GR& gr, const Map& map, const Pred& pred) {
4078 typedef typename Map::Key Item;
4079 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4082 for (ItemIt it(gr); it != INVALID; ++it) {
4083 if (pred(map[it])) ++cnt;
4088 /// \brief Fill a graph map with a certain value.
4090 /// This function sets the specified value for all items (\c Node,
4091 /// \c Arc or \c Edge) in the given graph map.
4093 /// \param gr The graph for which the map is defined.
4094 /// \param map The graph map. It must conform to the
4095 /// \ref concepts::WriteMap "WriteMap" concept.
4096 /// \param val The value.
4097 template <typename GR, typename Map>
4098 void mapFill(const GR& gr, Map& map, const typename Map::Value& val) {
4099 typedef typename Map::Key Item;
4100 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4102 for (ItemIt it(gr); it != INVALID; ++it) {
4110 #endif // LEMON_MAPS_H