1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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>
31 ///\brief Miscellaneous property maps
38 /// Base class of maps.
40 /// Base class of maps. It provides the necessary type definitions
41 /// required by the map %concepts.
42 template<typename K, typename V>
45 /// \brief The key type of the map.
47 /// \brief The value type of the map.
48 /// (The type of objects associated with the keys).
53 /// Null map. (a.k.a. DoNothingMap)
55 /// This map can be used if you have to provide a map only for
56 /// its type definitions, or if you have to provide a writable map,
57 /// but data written to it is not required (i.e. it will be sent to
58 /// <tt>/dev/null</tt>).
59 /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
62 template<typename K, typename V>
63 class NullMap : public MapBase<K, V> {
70 /// Gives back a default constructed element.
71 Value operator[](const Key&) const { return Value(); }
72 /// Absorbs the value.
73 void set(const Key&, const Value&) {}
76 /// Returns a \c NullMap class
78 /// This function just returns a \c NullMap class.
80 template <typename K, typename V>
81 NullMap<K, V> nullMap() {
82 return NullMap<K, V>();
88 /// This \ref concepts::ReadMap "readable map" assigns a specified
89 /// value to each key.
91 /// In other aspects it is equivalent to \c NullMap.
92 /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
93 /// concept, but it absorbs the data written to it.
95 /// The simplest way of using this map is through the constMap()
100 template<typename K, typename V>
101 class ConstMap : public MapBase<K, V> {
110 /// Default constructor
112 /// Default constructor.
113 /// The value of the map will be default constructed.
116 /// Constructor with specified initial value
118 /// Constructor with specified initial value.
119 /// \param v The initial value of the map.
120 ConstMap(const Value &v) : _value(v) {}
122 /// Gives back the specified value.
123 Value operator[](const Key&) const { return _value; }
125 /// Absorbs the value.
126 void set(const Key&, const Value&) {}
128 /// Sets the value that is assigned to each key.
129 void setAll(const Value &v) {
133 template<typename V1>
134 ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
137 /// Returns a \c ConstMap class
139 /// This function just returns a \c ConstMap class.
140 /// \relates ConstMap
141 template<typename K, typename V>
142 inline ConstMap<K, V> constMap(const V &v) {
143 return ConstMap<K, V>(v);
146 template<typename K, typename V>
147 inline ConstMap<K, V> constMap() {
148 return ConstMap<K, V>();
152 template<typename T, T v>
155 /// Constant map with inlined constant value.
157 /// This \ref concepts::ReadMap "readable map" assigns a specified
158 /// value to each key.
160 /// In other aspects it is equivalent to \c NullMap.
161 /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
162 /// concept, but it absorbs the data written to it.
164 /// The simplest way of using this map is through the constMap()
169 template<typename K, typename V, V v>
170 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
180 /// Gives back the specified value.
181 Value operator[](const Key&) const { return v; }
183 /// Absorbs the value.
184 void set(const Key&, const Value&) {}
187 /// Returns a \c ConstMap class with inlined constant value
189 /// This function just returns a \c ConstMap class with inlined
191 /// \relates ConstMap
192 template<typename K, typename V, V v>
193 inline ConstMap<K, Const<V, v> > constMap() {
194 return ConstMap<K, Const<V, v> >();
200 /// This \ref concepts::ReadMap "read-only map" gives back the given
201 /// key as value without any modification.
204 template <typename T>
205 class IdentityMap : public MapBase<T, T> {
212 /// Gives back the given value without any modification.
213 Value operator[](const Key &k) const {
218 /// Returns an \c IdentityMap class
220 /// This function just returns an \c IdentityMap class.
221 /// \relates IdentityMap
223 inline IdentityMap<T> identityMap() {
224 return IdentityMap<T>();
228 /// \brief Map for storing values for integer keys from the range
229 /// <tt>[0..size-1]</tt>.
231 /// This map is essentially a wrapper for \c std::vector. It assigns
232 /// values to integer keys from the range <tt>[0..size-1]</tt>.
233 /// It can be used with some data structures, for example
234 /// \c UnionFind, \c BinHeap, when the used items are small
235 /// integers. This map conforms to the \ref concepts::ReferenceMap
236 /// "ReferenceMap" concept.
238 /// The simplest way of using this map is through the rangeMap()
240 template <typename V>
241 class RangeMap : public MapBase<int, V> {
242 template <typename V1>
243 friend class RangeMap;
246 typedef std::vector<V> Vector;
256 typedef typename Vector::reference Reference;
257 /// Const reference type
258 typedef typename Vector::const_reference ConstReference;
260 typedef True ReferenceMapTag;
264 /// Constructor with specified default value.
265 RangeMap(int size = 0, const Value &value = Value())
266 : _vector(size, value) {}
268 /// Constructs the map from an appropriate \c std::vector.
269 template <typename V1>
270 RangeMap(const std::vector<V1>& vector)
271 : _vector(vector.begin(), vector.end()) {}
273 /// Constructs the map from another \c RangeMap.
274 template <typename V1>
275 RangeMap(const RangeMap<V1> &c)
276 : _vector(c._vector.begin(), c._vector.end()) {}
278 /// Returns the size of the map.
280 return _vector.size();
285 /// Resizes the underlying \c std::vector container, so changes the
286 /// keyset of the map.
287 /// \param size The new size of the map. The new keyset will be the
288 /// range <tt>[0..size-1]</tt>.
289 /// \param value The default value to assign to the new keys.
290 void resize(int size, const Value &value = Value()) {
291 _vector.resize(size, value);
296 RangeMap& operator=(const RangeMap&);
301 Reference operator[](const Key &k) {
306 ConstReference operator[](const Key &k) const {
311 void set(const Key &k, const Value &v) {
316 /// Returns a \c RangeMap class
318 /// This function just returns a \c RangeMap class.
319 /// \relates RangeMap
321 inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
322 return RangeMap<V>(size, value);
325 /// \brief Returns a \c RangeMap class created from an appropriate
328 /// This function just returns a \c RangeMap class created from an
329 /// appropriate \c std::vector.
330 /// \relates RangeMap
332 inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
333 return RangeMap<V>(vector);
337 /// Map type based on \c std::map
339 /// This map is essentially a wrapper for \c std::map with addition
340 /// that you can specify a default value for the keys that are not
341 /// stored actually. This value can be different from the default
342 /// contructed value (i.e. \c %Value()).
343 /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
346 /// This map is useful if a default value should be assigned to most of
347 /// the keys and different values should be assigned only to a few
348 /// keys (i.e. the map is "sparse").
349 /// The name of this type also refers to this important usage.
351 /// Apart form that this map can be used in many other cases since it
352 /// is based on \c std::map, which is a general associative container.
353 /// However keep in mind that it is usually not as efficient as other
356 /// The simplest way of using this map is through the sparseMap()
358 template <typename K, typename V, typename Comp = std::less<K> >
359 class SparseMap : public MapBase<K, V> {
360 template <typename K1, typename V1, typename C1>
361 friend class SparseMap;
369 typedef Value& Reference;
370 /// Const reference type
371 typedef const Value& ConstReference;
373 typedef True ReferenceMapTag;
377 typedef std::map<K, V, Comp> Map;
383 /// \brief Constructor with specified default value.
384 SparseMap(const Value &value = Value()) : _value(value) {}
385 /// \brief Constructs the map from an appropriate \c std::map, and
386 /// explicitly specifies a default value.
387 template <typename V1, typename Comp1>
388 SparseMap(const std::map<Key, V1, Comp1> &map,
389 const Value &value = Value())
390 : _map(map.begin(), map.end()), _value(value) {}
392 /// \brief Constructs the map from another \c SparseMap.
393 template<typename V1, typename Comp1>
394 SparseMap(const SparseMap<Key, V1, Comp1> &c)
395 : _map(c._map.begin(), c._map.end()), _value(c._value) {}
399 SparseMap& operator=(const SparseMap&);
404 Reference operator[](const Key &k) {
405 typename Map::iterator it = _map.lower_bound(k);
406 if (it != _map.end() && !_map.key_comp()(k, it->first))
409 return _map.insert(it, std::make_pair(k, _value))->second;
413 ConstReference operator[](const Key &k) const {
414 typename Map::const_iterator it = _map.find(k);
415 if (it != _map.end())
422 void set(const Key &k, const Value &v) {
423 typename Map::iterator it = _map.lower_bound(k);
424 if (it != _map.end() && !_map.key_comp()(k, it->first))
427 _map.insert(it, std::make_pair(k, v));
431 void setAll(const Value &v) {
437 /// Returns a \c SparseMap class
439 /// This function just returns a \c SparseMap class with specified
441 /// \relates SparseMap
442 template<typename K, typename V, typename Compare>
443 inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
444 return SparseMap<K, V, Compare>(value);
447 template<typename K, typename V>
448 inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
449 return SparseMap<K, V, std::less<K> >(value);
452 /// \brief Returns a \c SparseMap class created from an appropriate
455 /// This function just returns a \c SparseMap class created from an
456 /// appropriate \c std::map.
457 /// \relates SparseMap
458 template<typename K, typename V, typename Compare>
459 inline SparseMap<K, V, Compare>
460 sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
462 return SparseMap<K, V, Compare>(map, value);
467 /// \addtogroup map_adaptors
470 /// Composition of two maps
472 /// This \ref concepts::ReadMap "read-only map" returns the
473 /// composition of two given maps. That is to say, if \c m1 is of
474 /// type \c M1 and \c m2 is of \c M2, then for
476 /// ComposeMap<M1, M2> cm(m1,m2);
478 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
480 /// The \c Key type of the map is inherited from \c M2 and the
481 /// \c Value type is from \c M1.
482 /// \c M2::Value must be convertible to \c M1::Key.
484 /// The simplest way of using this map is through the composeMap()
488 template <typename M1, typename M2>
489 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
494 typedef typename M2::Key Key;
496 typedef typename M1::Value Value;
499 ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
502 typename MapTraits<M1>::ConstReturnValue
503 operator[](const Key &k) const { return _m1[_m2[k]]; }
506 /// Returns a \c ComposeMap class
508 /// This function just returns a \c ComposeMap class.
510 /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
511 /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
512 /// will be equal to <tt>m1[m2[x]]</tt>.
514 /// \relates ComposeMap
515 template <typename M1, typename M2>
516 inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
517 return ComposeMap<M1, M2>(m1, m2);
521 /// Combination of two maps using an STL (binary) functor.
523 /// This \ref concepts::ReadMap "read-only map" takes two maps and a
524 /// binary functor and returns the combination of the two given maps
525 /// using the functor.
526 /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
527 /// and \c f is of \c F, then for
529 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
531 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
533 /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
534 /// must be convertible to \c M2::Key) and the \c Value type is \c V.
535 /// \c M2::Value and \c M1::Value must be convertible to the
536 /// corresponding input parameter of \c F and the return type of \c F
537 /// must be convertible to \c V.
539 /// The simplest way of using this map is through the combineMap()
543 template<typename M1, typename M2, typename F,
544 typename V = typename F::result_type>
545 class CombineMap : public MapBase<typename M1::Key, V> {
551 typedef typename M1::Key Key;
556 CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
557 : _m1(m1), _m2(m2), _f(f) {}
559 Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
562 /// Returns a \c CombineMap class
564 /// This function just returns a \c CombineMap class.
566 /// For example, if \c m1 and \c m2 are both maps with \c double
569 /// combineMap(m1,m2,std::plus<double>())
576 /// This function is specialized for adaptable binary function
577 /// classes and C++ functions.
579 /// \relates CombineMap
580 template<typename M1, typename M2, typename F, typename V>
581 inline CombineMap<M1, M2, F, V>
582 combineMap(const M1 &m1, const M2 &m2, const F &f) {
583 return CombineMap<M1, M2, F, V>(m1,m2,f);
586 template<typename M1, typename M2, typename F>
587 inline CombineMap<M1, M2, F, typename F::result_type>
588 combineMap(const M1 &m1, const M2 &m2, const F &f) {
589 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
592 template<typename M1, typename M2, typename K1, typename K2, typename V>
593 inline CombineMap<M1, M2, V (*)(K1, K2), V>
594 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
595 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
599 /// Converts an STL style (unary) functor to a map
601 /// This \ref concepts::ReadMap "read-only map" returns the value
602 /// of a given functor. Actually, it just wraps the functor and
603 /// provides the \c Key and \c Value typedefs.
605 /// Template parameters \c K and \c V will become its \c Key and
606 /// \c Value. In most cases they have to be given explicitly because
607 /// a functor typically does not provide \c argument_type and
608 /// \c result_type typedefs.
609 /// Parameter \c F is the type of the used functor.
611 /// The simplest way of using this map is through the functorToMap()
616 typename K = typename F::argument_type,
617 typename V = typename F::result_type>
618 class FunctorToMap : public MapBase<K, V> {
627 FunctorToMap(const F &f = F()) : _f(f) {}
629 Value operator[](const Key &k) const { return _f(k); }
632 /// Returns a \c FunctorToMap class
634 /// This function just returns a \c FunctorToMap class.
636 /// This function is specialized for adaptable binary function
637 /// classes and C++ functions.
639 /// \relates FunctorToMap
640 template<typename K, typename V, typename F>
641 inline FunctorToMap<F, K, V> functorToMap(const F &f) {
642 return FunctorToMap<F, K, V>(f);
645 template <typename F>
646 inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
647 functorToMap(const F &f)
649 return FunctorToMap<F, typename F::argument_type,
650 typename F::result_type>(f);
653 template <typename K, typename V>
654 inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
655 return FunctorToMap<V (*)(K), K, V>(f);
659 /// Converts a map to an STL style (unary) functor
661 /// This class converts a map to an STL style (unary) functor.
662 /// That is it provides an <tt>operator()</tt> to read its values.
664 /// For the sake of convenience it also works as a usual
665 /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
666 /// and the \c Key and \c Value typedefs also exist.
668 /// The simplest way of using this map is through the mapToFunctor()
672 template <typename M>
673 class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
677 typedef typename M::Key Key;
679 typedef typename M::Value Value;
681 typedef typename M::Key argument_type;
682 typedef typename M::Value result_type;
685 MapToFunctor(const M &m) : _m(m) {}
687 Value operator()(const Key &k) const { return _m[k]; }
689 Value operator[](const Key &k) const { return _m[k]; }
692 /// Returns a \c MapToFunctor class
694 /// This function just returns a \c MapToFunctor class.
695 /// \relates MapToFunctor
697 inline MapToFunctor<M> mapToFunctor(const M &m) {
698 return MapToFunctor<M>(m);
702 /// \brief Map adaptor to convert the \c Value type of a map to
703 /// another type using the default conversion.
705 /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
706 /// "readable map" to another type using the default conversion.
707 /// The \c Key type of it is inherited from \c M and the \c Value
709 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
711 /// The simplest way of using this map is through the convertMap()
713 template <typename M, typename V>
714 class ConvertMap : public MapBase<typename M::Key, V> {
718 typedef typename M::Key Key;
725 /// \param m The underlying map.
726 ConvertMap(const M &m) : _m(m) {}
729 Value operator[](const Key &k) const { return _m[k]; }
732 /// Returns a \c ConvertMap class
734 /// This function just returns a \c ConvertMap class.
735 /// \relates ConvertMap
736 template<typename V, typename M>
737 inline ConvertMap<M, V> convertMap(const M &map) {
738 return ConvertMap<M, V>(map);
742 /// Applies all map setting operations to two maps
744 /// This map has two \ref concepts::WriteMap "writable map" parameters
745 /// and each write request will be passed to both of them.
746 /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
747 /// operations will return the corresponding values of \c M1.
749 /// The \c Key and \c Value types are inherited from \c M1.
750 /// The \c Key and \c Value of \c M2 must be convertible from those
753 /// The simplest way of using this map is through the forkMap()
755 template<typename M1, typename M2>
756 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
761 typedef typename M1::Key Key;
763 typedef typename M1::Value Value;
766 ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
767 /// Returns the value associated with the given key in the first map.
768 Value operator[](const Key &k) const { return _m1[k]; }
769 /// Sets the value associated with the given key in both maps.
770 void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
773 /// Returns a \c ForkMap class
775 /// This function just returns a \c ForkMap class.
777 template <typename M1, typename M2>
778 inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
779 return ForkMap<M1,M2>(m1,m2);
785 /// This \ref concepts::ReadMap "read-only map" returns the sum
786 /// of the values of the two given maps.
787 /// Its \c Key and \c Value types are inherited from \c M1.
788 /// The \c Key and \c Value of \c M2 must be convertible to those of
791 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
793 /// AddMap<M1,M2> am(m1,m2);
795 /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
797 /// The simplest way of using this map is through the addMap()
800 /// \sa SubMap, MulMap, DivMap
801 /// \sa ShiftMap, ShiftWriteMap
802 template<typename M1, typename M2>
803 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
808 typedef typename M1::Key Key;
810 typedef typename M1::Value Value;
813 AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
815 Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
818 /// Returns an \c AddMap class
820 /// This function just returns an \c AddMap class.
822 /// For example, if \c m1 and \c m2 are both maps with \c double
823 /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
824 /// <tt>m1[x]+m2[x]</tt>.
827 template<typename M1, typename M2>
828 inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
829 return AddMap<M1, M2>(m1,m2);
833 /// Difference of two maps
835 /// This \ref concepts::ReadMap "read-only map" returns the difference
836 /// of the values of the two given maps.
837 /// Its \c Key and \c Value types are inherited from \c M1.
838 /// The \c Key and \c Value of \c M2 must be convertible to those of
841 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
843 /// SubMap<M1,M2> sm(m1,m2);
845 /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
847 /// The simplest way of using this map is through the subMap()
850 /// \sa AddMap, MulMap, DivMap
851 template<typename M1, typename M2>
852 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
857 typedef typename M1::Key Key;
859 typedef typename M1::Value Value;
862 SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
864 Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
867 /// Returns a \c SubMap class
869 /// This function just returns a \c SubMap class.
871 /// For example, if \c m1 and \c m2 are both maps with \c double
872 /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
873 /// <tt>m1[x]-m2[x]</tt>.
876 template<typename M1, typename M2>
877 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
878 return SubMap<M1, M2>(m1,m2);
882 /// Product of two maps
884 /// This \ref concepts::ReadMap "read-only map" returns the product
885 /// of the values of the two given maps.
886 /// Its \c Key and \c Value types are inherited from \c M1.
887 /// The \c Key and \c Value of \c M2 must be convertible to those of
890 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
892 /// MulMap<M1,M2> mm(m1,m2);
894 /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
896 /// The simplest way of using this map is through the mulMap()
899 /// \sa AddMap, SubMap, DivMap
900 /// \sa ScaleMap, ScaleWriteMap
901 template<typename M1, typename M2>
902 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
907 typedef typename M1::Key Key;
909 typedef typename M1::Value Value;
912 MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
914 Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
917 /// Returns a \c MulMap class
919 /// This function just returns a \c MulMap class.
921 /// For example, if \c m1 and \c m2 are both maps with \c double
922 /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
923 /// <tt>m1[x]*m2[x]</tt>.
926 template<typename M1, typename M2>
927 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
928 return MulMap<M1, M2>(m1,m2);
932 /// Quotient of two maps
934 /// This \ref concepts::ReadMap "read-only map" returns the quotient
935 /// of the values of the two given maps.
936 /// Its \c Key and \c Value types are inherited from \c M1.
937 /// The \c Key and \c Value of \c M2 must be convertible to those of
940 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
942 /// DivMap<M1,M2> dm(m1,m2);
944 /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
946 /// The simplest way of using this map is through the divMap()
949 /// \sa AddMap, SubMap, MulMap
950 template<typename M1, typename M2>
951 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
956 typedef typename M1::Key Key;
958 typedef typename M1::Value Value;
961 DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
963 Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
966 /// Returns a \c DivMap class
968 /// This function just returns a \c DivMap class.
970 /// For example, if \c m1 and \c m2 are both maps with \c double
971 /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
972 /// <tt>m1[x]/m2[x]</tt>.
975 template<typename M1, typename M2>
976 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
977 return DivMap<M1, M2>(m1,m2);
981 /// Shifts a map with a constant.
983 /// This \ref concepts::ReadMap "read-only map" returns the sum of
984 /// the given map and a constant value (i.e. it shifts the map with
985 /// the constant). Its \c Key and \c Value are inherited from \c M.
989 /// ShiftMap<M> sh(m,v);
993 /// ConstMap<M::Key, M::Value> cm(v);
994 /// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
997 /// The simplest way of using this map is through the shiftMap()
1000 /// \sa ShiftWriteMap
1001 template<typename M, typename C = typename M::Value>
1002 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
1007 typedef typename M::Key Key;
1009 typedef typename M::Value Value;
1014 /// \param m The undelying map.
1015 /// \param v The constant value.
1016 ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1018 Value operator[](const Key &k) const { return _m[k]+_v; }
1021 /// Shifts a map with a constant (read-write version).
1023 /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1024 /// of the given map and a constant value (i.e. it shifts the map with
1025 /// the constant). Its \c Key and \c Value are inherited from \c M.
1026 /// It makes also possible to write the map.
1028 /// The simplest way of using this map is through the shiftWriteMap()
1032 template<typename M, typename C = typename M::Value>
1033 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1038 typedef typename M::Key Key;
1040 typedef typename M::Value Value;
1045 /// \param m The undelying map.
1046 /// \param v The constant value.
1047 ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1049 Value operator[](const Key &k) const { return _m[k]+_v; }
1051 void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1054 /// Returns a \c ShiftMap class
1056 /// This function just returns a \c ShiftMap class.
1058 /// For example, if \c m is a map with \c double values and \c v is
1059 /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1060 /// <tt>m[x]+v</tt>.
1062 /// \relates ShiftMap
1063 template<typename M, typename C>
1064 inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1065 return ShiftMap<M, C>(m,v);
1068 /// Returns a \c ShiftWriteMap class
1070 /// This function just returns a \c ShiftWriteMap class.
1072 /// For example, if \c m is a map with \c double values and \c v is
1073 /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1074 /// <tt>m[x]+v</tt>.
1075 /// Moreover it makes also possible to write the map.
1077 /// \relates ShiftWriteMap
1078 template<typename M, typename C>
1079 inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1080 return ShiftWriteMap<M, C>(m,v);
1084 /// Scales a map with a constant.
1086 /// This \ref concepts::ReadMap "read-only map" returns the value of
1087 /// the given map multiplied from the left side with a constant value.
1088 /// Its \c Key and \c Value are inherited from \c M.
1092 /// ScaleMap<M> sc(m,v);
1094 /// is equivalent to
1096 /// ConstMap<M::Key, M::Value> cm(v);
1097 /// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1100 /// The simplest way of using this map is through the scaleMap()
1103 /// \sa ScaleWriteMap
1104 template<typename M, typename C = typename M::Value>
1105 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1110 typedef typename M::Key Key;
1112 typedef typename M::Value Value;
1117 /// \param m The undelying map.
1118 /// \param v The constant value.
1119 ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1121 Value operator[](const Key &k) const { return _v*_m[k]; }
1124 /// Scales a map with a constant (read-write version).
1126 /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1127 /// the given map multiplied from the left side with a constant value.
1128 /// Its \c Key and \c Value are inherited from \c M.
1129 /// It can also be used as write map if the \c / operator is defined
1130 /// between \c Value and \c C and the given multiplier is not zero.
1132 /// The simplest way of using this map is through the scaleWriteMap()
1136 template<typename M, typename C = typename M::Value>
1137 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1142 typedef typename M::Key Key;
1144 typedef typename M::Value Value;
1149 /// \param m The undelying map.
1150 /// \param v The constant value.
1151 ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1153 Value operator[](const Key &k) const { return _v*_m[k]; }
1155 void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1158 /// Returns a \c ScaleMap class
1160 /// This function just returns a \c ScaleMap class.
1162 /// For example, if \c m is a map with \c double values and \c v is
1163 /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1164 /// <tt>v*m[x]</tt>.
1166 /// \relates ScaleMap
1167 template<typename M, typename C>
1168 inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1169 return ScaleMap<M, C>(m,v);
1172 /// Returns a \c ScaleWriteMap class
1174 /// This function just returns a \c ScaleWriteMap class.
1176 /// For example, if \c m is a map with \c double values and \c v is
1177 /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1178 /// <tt>v*m[x]</tt>.
1179 /// Moreover it makes also possible to write the map.
1181 /// \relates ScaleWriteMap
1182 template<typename M, typename C>
1183 inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1184 return ScaleWriteMap<M, C>(m,v);
1188 /// Negative of a map
1190 /// This \ref concepts::ReadMap "read-only map" returns the negative
1191 /// of the values of the given map (using the unary \c - operator).
1192 /// Its \c Key and \c Value are inherited from \c M.
1194 /// If M::Value is \c int, \c double etc., then
1196 /// NegMap<M> neg(m);
1198 /// is equivalent to
1200 /// ScaleMap<M> neg(m,-1);
1203 /// The simplest way of using this map is through the negMap()
1207 template<typename M>
1208 class NegMap : public MapBase<typename M::Key, typename M::Value> {
1212 typedef typename M::Key Key;
1214 typedef typename M::Value Value;
1217 NegMap(const M &m) : _m(m) {}
1219 Value operator[](const Key &k) const { return -_m[k]; }
1222 /// Negative of a map (read-write version)
1224 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1225 /// negative of the values of the given map (using the unary \c -
1227 /// Its \c Key and \c Value are inherited from \c M.
1228 /// It makes also possible to write the map.
1230 /// If M::Value is \c int, \c double etc., then
1232 /// NegWriteMap<M> neg(m);
1234 /// is equivalent to
1236 /// ScaleWriteMap<M> neg(m,-1);
1239 /// The simplest way of using this map is through the negWriteMap()
1243 template<typename M>
1244 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1248 typedef typename M::Key Key;
1250 typedef typename M::Value Value;
1253 NegWriteMap(M &m) : _m(m) {}
1255 Value operator[](const Key &k) const { return -_m[k]; }
1257 void set(const Key &k, const Value &v) { _m.set(k, -v); }
1260 /// Returns a \c NegMap class
1262 /// This function just returns a \c NegMap class.
1264 /// For example, if \c m is a map with \c double values, then
1265 /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1268 template <typename M>
1269 inline NegMap<M> negMap(const M &m) {
1270 return NegMap<M>(m);
1273 /// Returns a \c NegWriteMap class
1275 /// This function just returns a \c NegWriteMap class.
1277 /// For example, if \c m is a map with \c double values, then
1278 /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1279 /// Moreover it makes also possible to write the map.
1281 /// \relates NegWriteMap
1282 template <typename M>
1283 inline NegWriteMap<M> negWriteMap(M &m) {
1284 return NegWriteMap<M>(m);
1288 /// Absolute value of a map
1290 /// This \ref concepts::ReadMap "read-only map" returns the absolute
1291 /// value of the values of the given map.
1292 /// Its \c Key and \c Value are inherited from \c M.
1293 /// \c Value must be comparable to \c 0 and the unary \c -
1294 /// operator must be defined for it, of course.
1296 /// The simplest way of using this map is through the absMap()
1298 template<typename M>
1299 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1303 typedef typename M::Key Key;
1305 typedef typename M::Value Value;
1308 AbsMap(const M &m) : _m(m) {}
1310 Value operator[](const Key &k) const {
1312 return tmp >= 0 ? tmp : -tmp;
1317 /// Returns an \c AbsMap class
1319 /// This function just returns an \c AbsMap class.
1321 /// For example, if \c m is a map with \c double values, then
1322 /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1323 /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1327 template<typename M>
1328 inline AbsMap<M> absMap(const M &m) {
1329 return AbsMap<M>(m);
1334 // Logical maps and map adaptors:
1336 /// \addtogroup maps
1339 /// Constant \c true map.
1341 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1348 /// is equivalent to
1350 /// ConstMap<K,bool> tm(true);
1355 template <typename K>
1356 class TrueMap : public MapBase<K, bool> {
1363 /// Gives back \c true.
1364 Value operator[](const Key&) const { return true; }
1367 /// Returns a \c TrueMap class
1369 /// This function just returns a \c TrueMap class.
1370 /// \relates TrueMap
1371 template<typename K>
1372 inline TrueMap<K> trueMap() {
1373 return TrueMap<K>();
1377 /// Constant \c false map.
1379 /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1386 /// is equivalent to
1388 /// ConstMap<K,bool> fm(false);
1393 template <typename K>
1394 class FalseMap : public MapBase<K, bool> {
1401 /// Gives back \c false.
1402 Value operator[](const Key&) const { return false; }
1405 /// Returns a \c FalseMap class
1407 /// This function just returns a \c FalseMap class.
1408 /// \relates FalseMap
1409 template<typename K>
1410 inline FalseMap<K> falseMap() {
1411 return FalseMap<K>();
1416 /// \addtogroup map_adaptors
1419 /// Logical 'and' of two maps
1421 /// This \ref concepts::ReadMap "read-only map" returns the logical
1422 /// 'and' of the values of the two given maps.
1423 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1424 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1426 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1428 /// AndMap<M1,M2> am(m1,m2);
1430 /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1432 /// The simplest way of using this map is through the andMap()
1436 /// \sa NotMap, NotWriteMap
1437 template<typename M1, typename M2>
1438 class AndMap : public MapBase<typename M1::Key, bool> {
1443 typedef typename M1::Key Key;
1448 AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1450 Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1453 /// Returns an \c AndMap class
1455 /// This function just returns an \c AndMap class.
1457 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1458 /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1459 /// <tt>m1[x]&&m2[x]</tt>.
1462 template<typename M1, typename M2>
1463 inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1464 return AndMap<M1, M2>(m1,m2);
1468 /// Logical 'or' of two maps
1470 /// This \ref concepts::ReadMap "read-only map" returns the logical
1471 /// 'or' of the values of the two given maps.
1472 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1473 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1475 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1477 /// OrMap<M1,M2> om(m1,m2);
1479 /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1481 /// The simplest way of using this map is through the orMap()
1485 /// \sa NotMap, NotWriteMap
1486 template<typename M1, typename M2>
1487 class OrMap : public MapBase<typename M1::Key, bool> {
1492 typedef typename M1::Key Key;
1497 OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1499 Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1502 /// Returns an \c OrMap class
1504 /// This function just returns an \c OrMap class.
1506 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1507 /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1508 /// <tt>m1[x]||m2[x]</tt>.
1511 template<typename M1, typename M2>
1512 inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1513 return OrMap<M1, M2>(m1,m2);
1517 /// Logical 'not' of a map
1519 /// This \ref concepts::ReadMap "read-only map" returns the logical
1520 /// negation of the values of the given map.
1521 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1523 /// The simplest way of using this map is through the notMap()
1527 template <typename M>
1528 class NotMap : public MapBase<typename M::Key, bool> {
1532 typedef typename M::Key Key;
1537 NotMap(const M &m) : _m(m) {}
1539 Value operator[](const Key &k) const { return !_m[k]; }
1542 /// Logical 'not' of a map (read-write version)
1544 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1545 /// logical negation of the values of the given map.
1546 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1547 /// It makes also possible to write the map. When a value is set,
1548 /// the opposite value is set to the original map.
1550 /// The simplest way of using this map is through the notWriteMap()
1554 template <typename M>
1555 class NotWriteMap : public MapBase<typename M::Key, bool> {
1559 typedef typename M::Key Key;
1564 NotWriteMap(M &m) : _m(m) {}
1566 Value operator[](const Key &k) const { return !_m[k]; }
1568 void set(const Key &k, bool v) { _m.set(k, !v); }
1571 /// Returns a \c NotMap class
1573 /// This function just returns a \c NotMap class.
1575 /// For example, if \c m is a map with \c bool values, then
1576 /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1579 template <typename M>
1580 inline NotMap<M> notMap(const M &m) {
1581 return NotMap<M>(m);
1584 /// Returns a \c NotWriteMap class
1586 /// This function just returns a \c NotWriteMap class.
1588 /// For example, if \c m is a map with \c bool values, then
1589 /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1590 /// Moreover it makes also possible to write the map.
1592 /// \relates NotWriteMap
1593 template <typename M>
1594 inline NotWriteMap<M> notWriteMap(M &m) {
1595 return NotWriteMap<M>(m);
1599 /// Combination of two maps using the \c == operator
1601 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1602 /// the keys for which the corresponding values of the two maps are
1604 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1605 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1607 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1609 /// EqualMap<M1,M2> em(m1,m2);
1611 /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1613 /// The simplest way of using this map is through the equalMap()
1617 template<typename M1, typename M2>
1618 class EqualMap : public MapBase<typename M1::Key, bool> {
1623 typedef typename M1::Key Key;
1628 EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1630 Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1633 /// Returns an \c EqualMap class
1635 /// This function just returns an \c EqualMap class.
1637 /// For example, if \c m1 and \c m2 are maps with keys and values of
1638 /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1639 /// <tt>m1[x]==m2[x]</tt>.
1641 /// \relates EqualMap
1642 template<typename M1, typename M2>
1643 inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1644 return EqualMap<M1, M2>(m1,m2);
1648 /// Combination of two maps using the \c < operator
1650 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1651 /// the keys for which the corresponding value of the first map is
1652 /// less then the value of the second map.
1653 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1654 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1656 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1658 /// LessMap<M1,M2> lm(m1,m2);
1660 /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1662 /// The simplest way of using this map is through the lessMap()
1666 template<typename M1, typename M2>
1667 class LessMap : public MapBase<typename M1::Key, bool> {
1672 typedef typename M1::Key Key;
1677 LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1679 Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1682 /// Returns an \c LessMap class
1684 /// This function just returns an \c LessMap class.
1686 /// For example, if \c m1 and \c m2 are maps with keys and values of
1687 /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1688 /// <tt>m1[x]<m2[x]</tt>.
1690 /// \relates LessMap
1691 template<typename M1, typename M2>
1692 inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1693 return LessMap<M1, M2>(m1,m2);
1696 namespace _maps_bits {
1698 template <typename _Iterator, typename Enable = void>
1699 struct IteratorTraits {
1700 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1703 template <typename _Iterator>
1704 struct IteratorTraits<_Iterator,
1705 typename exists<typename _Iterator::container_type>::type>
1707 typedef typename _Iterator::container_type::value_type Value;
1714 /// \addtogroup maps
1717 /// \brief Writable bool map for logging each \c true assigned element
1719 /// A \ref concepts::WriteMap "writable" bool map for logging
1720 /// each \c true assigned element, i.e it copies subsequently each
1721 /// keys set to \c true to the given iterator.
1722 /// The most important usage of it is storing certain nodes or arcs
1723 /// that were marked \c true by an algorithm.
1725 /// There are several algorithms that provide solutions through bool
1726 /// maps and most of them assign \c true at most once for each key.
1727 /// In these cases it is a natural request to store each \c true
1728 /// assigned elements (in order of the assignment), which can be
1729 /// easily done with LoggerBoolMap.
1731 /// The simplest way of using this map is through the loggerBoolMap()
1734 /// \tparam IT The type of the iterator.
1735 /// \tparam KEY The key type of the map. The default value set
1736 /// according to the iterator type should work in most cases.
1738 /// \note The container of the iterator must contain enough space
1739 /// for the elements or the iterator should be an inserter iterator.
1741 template <typename IT, typename KEY>
1743 template <typename IT,
1744 typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1746 class LoggerBoolMap : public MapBase<KEY, bool> {
1754 typedef IT Iterator;
1757 LoggerBoolMap(Iterator it)
1758 : _begin(it), _end(it) {}
1760 /// Gives back the given iterator set for the first key
1761 Iterator begin() const {
1765 /// Gives back the the 'after the last' iterator
1766 Iterator end() const {
1770 /// The set function of the map
1771 void set(const Key& key, Value value) {
1782 /// Returns a \c LoggerBoolMap class
1784 /// This function just returns a \c LoggerBoolMap class.
1786 /// The most important usage of it is storing certain nodes or arcs
1787 /// that were marked \c true by an algorithm.
1788 /// For example it makes easier to store the nodes in the processing
1789 /// order of Dfs algorithm, as the following examples show.
1791 /// std::vector<Node> v;
1792 /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1795 /// std::vector<Node> v(countNodes(g));
1796 /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1799 /// \note The container of the iterator must contain enough space
1800 /// for the elements or the iterator should be an inserter iterator.
1802 /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1803 /// it cannot be used when a readable map is needed, for example as
1804 /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1806 /// \relates LoggerBoolMap
1807 template<typename Iterator>
1808 inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1809 return LoggerBoolMap<Iterator>(it);
1814 /// \addtogroup graph_maps
1817 /// \brief Provides an immutable and unique id for each item in a graph.
1819 /// IdMap provides a unique and immutable id for each item of the
1820 /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1821 /// - \b unique: different items get different ids,
1822 /// - \b immutable: the id of an item does not change (even if you
1823 /// delete other nodes).
1825 /// Using this map you get access (i.e. can read) the inner id values of
1826 /// the items stored in the graph, which is returned by the \c id()
1827 /// function of the graph. This map can be inverted with its member
1828 /// class \c InverseMap or with the \c operator()() member.
1830 /// \tparam GR The graph type.
1831 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1835 template <typename GR, typename K>
1836 class IdMap : public MapBase<K, int> {
1838 /// The graph type of IdMap.
1841 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1843 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1845 /// The value type of IdMap.
1848 /// \brief Constructor.
1850 /// Constructor of the map.
1851 explicit IdMap(const Graph& graph) : _graph(&graph) {}
1853 /// \brief Gives back the \e id of the item.
1855 /// Gives back the immutable and unique \e id of the item.
1856 int operator[](const Item& item) const { return _graph->id(item);}
1858 /// \brief Gives back the \e item by its id.
1860 /// Gives back the \e item by its id.
1861 Item operator()(int id) { return _graph->fromId(id, Item()); }
1864 const Graph* _graph;
1868 /// \brief The inverse map type of IdMap.
1870 /// The inverse map type of IdMap. The subscript operator gives back
1871 /// an item by its id.
1872 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1877 /// \brief Constructor.
1879 /// Constructor for creating an id-to-item map.
1880 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1882 /// \brief Constructor.
1884 /// Constructor for creating an id-to-item map.
1885 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1887 /// \brief Gives back an item by its id.
1889 /// Gives back an item by its id.
1890 Item operator[](int id) const { return _graph->fromId(id, Item());}
1893 const Graph* _graph;
1896 /// \brief Gives back the inverse of the map.
1898 /// Gives back the inverse of the IdMap.
1899 InverseMap inverse() const { return InverseMap(*_graph);}
1903 /// \brief General cross reference graph map type.
1905 /// This class provides simple invertable graph maps.
1906 /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1907 /// and if a key is set to a new value, then stores it in the inverse map.
1908 /// The graph items can be accessed by their values either using
1909 /// \c InverseMap or \c operator()(), and the values of the map can be
1910 /// accessed with an STL compatible forward iterator (\c ValueIterator).
1912 /// This map is intended to be used when all associated values are
1913 /// different (the map is actually invertable) or there are only a few
1914 /// items with the same value.
1915 /// Otherwise consider to use \c IterableValueMap, which is more
1916 /// suitable and more efficient for such cases. It provides iterators
1917 /// to traverse the items with the same associated value, however
1918 /// it does not have \c InverseMap.
1920 /// This type is not reference map, so it cannot be modified with
1921 /// the subscript operator.
1923 /// \tparam GR The graph type.
1924 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1926 /// \tparam V The value type of the map.
1928 /// \see IterableValueMap
1929 template <typename GR, typename K, typename V>
1931 : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1934 typedef typename ItemSetTraits<GR, K>::
1935 template Map<V>::Type Map;
1937 typedef std::multimap<V, K> Container;
1942 /// The graph type of CrossRefMap.
1945 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1947 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1949 /// The value type of CrossRefMap.
1952 /// \brief Constructor.
1954 /// Construct a new CrossRefMap for the given graph.
1955 explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1957 /// \brief Forward iterator for values.
1959 /// This iterator is an STL compatible forward
1960 /// iterator on the values of the map. The values can
1961 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1962 /// They are considered with multiplicity, so each value is
1963 /// traversed for each item it is assigned to.
1965 : public std::iterator<std::forward_iterator_tag, Value> {
1966 friend class CrossRefMap;
1968 ValueIterator(typename Container::const_iterator _it)
1976 ValueIterator& operator++() { ++it; return *this; }
1978 ValueIterator operator++(int) {
1979 ValueIterator tmp(*this);
1985 const Value& operator*() const { return it->first; }
1987 const Value* operator->() const { return &(it->first); }
1990 bool operator==(ValueIterator jt) const { return it == jt.it; }
1992 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1995 typename Container::const_iterator it;
1998 /// \brief Returns an iterator to the first value.
2000 /// Returns an STL compatible iterator to the
2001 /// first value of the map. The values of the
2002 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2004 ValueIterator beginValue() const {
2005 return ValueIterator(_inv_map.begin());
2008 /// \brief Returns an iterator after the last value.
2010 /// Returns an STL compatible iterator after the
2011 /// last value of the map. The values of the
2012 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2014 ValueIterator endValue() const {
2015 return ValueIterator(_inv_map.end());
2018 /// \brief Sets the value associated with the given key.
2020 /// Sets the value associated with the given key.
2021 void set(const Key& key, const Value& val) {
2022 Value oldval = Map::operator[](key);
2023 typename Container::iterator it;
2024 for (it = _inv_map.equal_range(oldval).first;
2025 it != _inv_map.equal_range(oldval).second; ++it) {
2026 if (it->second == key) {
2031 _inv_map.insert(std::make_pair(val, key));
2035 /// \brief Returns the value associated with the given key.
2037 /// Returns the value associated with the given key.
2038 typename MapTraits<Map>::ConstReturnValue
2039 operator[](const Key& key) const {
2040 return Map::operator[](key);
2043 /// \brief Gives back an item by its value.
2045 /// This function gives back an item that is assigned to
2046 /// the given value or \c INVALID if no such item exists.
2047 /// If there are more items with the same associated value,
2048 /// only one of them is returned.
2049 Key operator()(const Value& val) const {
2050 typename Container::const_iterator it = _inv_map.find(val);
2051 return it != _inv_map.end() ? it->second : INVALID;
2054 /// \brief Returns the number of items with the given value.
2056 /// This function returns the number of items with the given value
2057 /// associated with it.
2058 int count(const Value &val) const {
2059 return _inv_map.count(val);
2064 /// \brief Erase the key from the map and the inverse map.
2066 /// Erase the key from the map and the inverse map. It is called by the
2067 /// \c AlterationNotifier.
2068 virtual void erase(const Key& key) {
2069 Value val = Map::operator[](key);
2070 typename Container::iterator it;
2071 for (it = _inv_map.equal_range(val).first;
2072 it != _inv_map.equal_range(val).second; ++it) {
2073 if (it->second == key) {
2081 /// \brief Erase more keys from the map and the inverse map.
2083 /// Erase more keys from the map and the inverse map. It is called by the
2084 /// \c AlterationNotifier.
2085 virtual void erase(const std::vector<Key>& keys) {
2086 for (int i = 0; i < int(keys.size()); ++i) {
2087 Value val = Map::operator[](keys[i]);
2088 typename Container::iterator it;
2089 for (it = _inv_map.equal_range(val).first;
2090 it != _inv_map.equal_range(val).second; ++it) {
2091 if (it->second == keys[i]) {
2100 /// \brief Clear the keys from the map and the inverse map.
2102 /// Clear the keys from the map and the inverse map. It is called by the
2103 /// \c AlterationNotifier.
2104 virtual void clear() {
2111 /// \brief The inverse map type of CrossRefMap.
2113 /// The inverse map type of CrossRefMap. The subscript operator gives
2114 /// back an item by its value.
2115 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2119 /// \brief Constructor
2121 /// Constructor of the InverseMap.
2122 explicit InverseMap(const CrossRefMap& inverted)
2123 : _inverted(inverted) {}
2125 /// The value type of the InverseMap.
2126 typedef typename CrossRefMap::Key Value;
2127 /// The key type of the InverseMap.
2128 typedef typename CrossRefMap::Value Key;
2130 /// \brief Subscript operator.
2132 /// Subscript operator. It gives back an item
2133 /// that is assigned to the given value or \c INVALID
2134 /// if no such item exists.
2135 Value operator[](const Key& key) const {
2136 return _inverted(key);
2140 const CrossRefMap& _inverted;
2143 /// \brief Gives back the inverse of the map.
2145 /// Gives back the inverse of the CrossRefMap.
2146 InverseMap inverse() const {
2147 return InverseMap(*this);
2152 /// \brief Provides continuous and unique id for the
2153 /// items of a graph.
2155 /// RangeIdMap provides a unique and continuous
2156 /// id for each item of a given type (\c Node, \c Arc or
2157 /// \c Edge) in a graph. This id is
2158 /// - \b unique: different items get different ids,
2159 /// - \b continuous: the range of the ids is the set of integers
2160 /// between 0 and \c n-1, where \c n is the number of the items of
2161 /// this type (\c Node, \c Arc or \c Edge).
2162 /// - So, the ids can change when deleting an item of the same type.
2164 /// Thus this id is not (necessarily) the same as what can get using
2165 /// the \c id() function of the graph or \ref IdMap.
2166 /// This map can be inverted with its member class \c InverseMap,
2167 /// or with the \c operator()() member.
2169 /// \tparam GR The graph type.
2170 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2174 template <typename GR, typename K>
2176 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2178 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2181 /// The graph type of RangeIdMap.
2184 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2186 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2188 /// The value type of RangeIdMap.
2191 /// \brief Constructor.
2194 explicit RangeIdMap(const Graph& gr) : Map(gr) {
2196 const typename Map::Notifier* nf = Map::notifier();
2197 for (nf->first(it); it != INVALID; nf->next(it)) {
2198 Map::set(it, _inv_map.size());
2199 _inv_map.push_back(it);
2205 /// \brief Adds a new key to the map.
2207 /// Add a new key to the map. It is called by the
2208 /// \c AlterationNotifier.
2209 virtual void add(const Item& item) {
2211 Map::set(item, _inv_map.size());
2212 _inv_map.push_back(item);
2215 /// \brief Add more new keys to the map.
2217 /// Add more new keys to the map. It is called by the
2218 /// \c AlterationNotifier.
2219 virtual void add(const std::vector<Item>& items) {
2221 for (int i = 0; i < int(items.size()); ++i) {
2222 Map::set(items[i], _inv_map.size());
2223 _inv_map.push_back(items[i]);
2227 /// \brief Erase the key from the map.
2229 /// Erase the key from the map. It is called by the
2230 /// \c AlterationNotifier.
2231 virtual void erase(const Item& item) {
2232 Map::set(_inv_map.back(), Map::operator[](item));
2233 _inv_map[Map::operator[](item)] = _inv_map.back();
2234 _inv_map.pop_back();
2238 /// \brief Erase more keys from the map.
2240 /// Erase more keys from the map. It is called by the
2241 /// \c AlterationNotifier.
2242 virtual void erase(const std::vector<Item>& items) {
2243 for (int i = 0; i < int(items.size()); ++i) {
2244 Map::set(_inv_map.back(), Map::operator[](items[i]));
2245 _inv_map[Map::operator[](items[i])] = _inv_map.back();
2246 _inv_map.pop_back();
2251 /// \brief Build the unique map.
2253 /// Build the unique map. It is called by the
2254 /// \c AlterationNotifier.
2255 virtual void build() {
2258 const typename Map::Notifier* nf = Map::notifier();
2259 for (nf->first(it); it != INVALID; nf->next(it)) {
2260 Map::set(it, _inv_map.size());
2261 _inv_map.push_back(it);
2265 /// \brief Clear the keys from the map.
2267 /// Clear the keys from the map. It is called by the
2268 /// \c AlterationNotifier.
2269 virtual void clear() {
2276 /// \brief Returns the maximal value plus one.
2278 /// Returns the maximal value plus one in the map.
2279 unsigned int size() const {
2280 return _inv_map.size();
2283 /// \brief Swaps the position of the two items in the map.
2285 /// Swaps the position of the two items in the map.
2286 void swap(const Item& p, const Item& q) {
2287 int pi = Map::operator[](p);
2288 int qi = Map::operator[](q);
2295 /// \brief Gives back the \e range \e id of the item
2297 /// Gives back the \e range \e id of the item.
2298 int operator[](const Item& item) const {
2299 return Map::operator[](item);
2302 /// \brief Gives back the item belonging to a \e range \e id
2304 /// Gives back the item belonging to the given \e range \e id.
2305 Item operator()(int id) const {
2306 return _inv_map[id];
2311 typedef std::vector<Item> Container;
2316 /// \brief The inverse map type of RangeIdMap.
2318 /// The inverse map type of RangeIdMap. The subscript operator gives
2319 /// back an item by its \e range \e id.
2320 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2323 /// \brief Constructor
2325 /// Constructor of the InverseMap.
2326 explicit InverseMap(const RangeIdMap& inverted)
2327 : _inverted(inverted) {}
2330 /// The value type of the InverseMap.
2331 typedef typename RangeIdMap::Key Value;
2332 /// The key type of the InverseMap.
2333 typedef typename RangeIdMap::Value Key;
2335 /// \brief Subscript operator.
2337 /// Subscript operator. It gives back the item
2338 /// that the given \e range \e id currently belongs to.
2339 Value operator[](const Key& key) const {
2340 return _inverted(key);
2343 /// \brief Size of the map.
2345 /// Returns the size of the map.
2346 unsigned int size() const {
2347 return _inverted.size();
2351 const RangeIdMap& _inverted;
2354 /// \brief Gives back the inverse of the map.
2356 /// Gives back the inverse of the RangeIdMap.
2357 const InverseMap inverse() const {
2358 return InverseMap(*this);
2362 /// \brief Dynamic iterable \c bool map.
2364 /// This class provides a special graph map type which can store a
2365 /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
2366 /// For both \c true and \c false values it is possible to iterate on
2367 /// the keys mapped to the value.
2369 /// This type is a reference map, so it can be modified with the
2370 /// subscript operator.
2372 /// \tparam GR The graph type.
2373 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2376 /// \see IterableIntMap, IterableValueMap
2377 /// \see CrossRefMap
2378 template <typename GR, typename K>
2379 class IterableBoolMap
2380 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2384 typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
2385 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
2387 std::vector<K> _array;
2392 /// Indicates that the map is reference map.
2393 typedef True ReferenceMapTag;
2399 /// The const reference type.
2400 typedef const Value& ConstReference;
2404 int position(const Key& key) const {
2405 return Parent::operator[](key);
2410 /// \brief Reference to the value of the map.
2412 /// This class is similar to the \c bool type. It can be converted to
2413 /// \c bool and it provides the same operators.
2415 friend class IterableBoolMap;
2417 Reference(IterableBoolMap& map, const Key& key)
2418 : _key(key), _map(map) {}
2421 Reference& operator=(const Reference& value) {
2422 _map.set(_key, static_cast<bool>(value));
2426 operator bool() const {
2427 return static_cast<const IterableBoolMap&>(_map)[_key];
2430 Reference& operator=(bool value) {
2431 _map.set(_key, value);
2434 Reference& operator&=(bool value) {
2435 _map.set(_key, _map[_key] & value);
2438 Reference& operator|=(bool value) {
2439 _map.set(_key, _map[_key] | value);
2442 Reference& operator^=(bool value) {
2443 _map.set(_key, _map[_key] ^ value);
2448 IterableBoolMap& _map;
2451 /// \brief Constructor of the map with a default value.
2453 /// Constructor of the map with a default value.
2454 explicit IterableBoolMap(const Graph& graph, bool def = false)
2456 typename Parent::Notifier* nf = Parent::notifier();
2458 for (nf->first(it); it != INVALID; nf->next(it)) {
2459 Parent::set(it, _array.size());
2460 _array.push_back(it);
2462 _sep = (def ? _array.size() : 0);
2465 /// \brief Const subscript operator of the map.
2467 /// Const subscript operator of the map.
2468 bool operator[](const Key& key) const {
2469 return position(key) < _sep;
2472 /// \brief Subscript operator of the map.
2474 /// Subscript operator of the map.
2475 Reference operator[](const Key& key) {
2476 return Reference(*this, key);
2479 /// \brief Set operation of the map.
2481 /// Set operation of the map.
2482 void set(const Key& key, bool value) {
2483 int pos = position(key);
2485 if (pos < _sep) return;
2486 Key tmp = _array[_sep];
2488 Parent::set(key, _sep);
2490 Parent::set(tmp, pos);
2493 if (pos >= _sep) return;
2495 Key tmp = _array[_sep];
2497 Parent::set(key, _sep);
2499 Parent::set(tmp, pos);
2503 /// \brief Set all items.
2505 /// Set all items in the map.
2506 /// \note Constant time operation.
2507 void setAll(bool value) {
2508 _sep = (value ? _array.size() : 0);
2511 /// \brief Returns the number of the keys mapped to \c true.
2513 /// Returns the number of the keys mapped to \c true.
2514 int trueNum() const {
2518 /// \brief Returns the number of the keys mapped to \c false.
2520 /// Returns the number of the keys mapped to \c false.
2521 int falseNum() const {
2522 return _array.size() - _sep;
2525 /// \brief Iterator for the keys mapped to \c true.
2527 /// Iterator for the keys mapped to \c true. It works
2528 /// like a graph item iterator, it can be converted to
2529 /// the key type of the map, incremented with \c ++ operator, and
2530 /// if the iterator leaves the last valid key, it will be equal to
2532 class TrueIt : public Key {
2536 /// \brief Creates an iterator.
2538 /// Creates an iterator. It iterates on the
2539 /// keys mapped to \c true.
2540 /// \param map The IterableBoolMap.
2541 explicit TrueIt(const IterableBoolMap& map)
2542 : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2545 /// \brief Invalid constructor \& conversion.
2547 /// This constructor initializes the iterator to be invalid.
2548 /// \sa Invalid for more details.
2549 TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2551 /// \brief Increment operator.
2553 /// Increment operator.
2554 TrueIt& operator++() {
2555 int pos = _map->position(*this);
2556 Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
2561 const IterableBoolMap* _map;
2564 /// \brief Iterator for the keys mapped to \c false.
2566 /// Iterator for the keys mapped to \c false. It works
2567 /// like a graph item iterator, it can be converted to
2568 /// the key type of the map, incremented with \c ++ operator, and
2569 /// if the iterator leaves the last valid key, it will be equal to
2571 class FalseIt : public Key {
2575 /// \brief Creates an iterator.
2577 /// Creates an iterator. It iterates on the
2578 /// keys mapped to \c false.
2579 /// \param map The IterableBoolMap.
2580 explicit FalseIt(const IterableBoolMap& map)
2581 : Parent(map._sep < int(map._array.size()) ?
2582 map._array.back() : INVALID), _map(&map) {}
2584 /// \brief Invalid constructor \& conversion.
2586 /// This constructor initializes the iterator to be invalid.
2587 /// \sa Invalid for more details.
2588 FalseIt(Invalid) : Parent(INVALID), _map(0) {}
2590 /// \brief Increment operator.
2592 /// Increment operator.
2593 FalseIt& operator++() {
2594 int pos = _map->position(*this);
2595 Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
2600 const IterableBoolMap* _map;
2603 /// \brief Iterator for the keys mapped to a given value.
2605 /// Iterator for the keys mapped to a given value. It works
2606 /// like a graph item iterator, it can be converted to
2607 /// the key type of the map, incremented with \c ++ operator, and
2608 /// if the iterator leaves the last valid key, it will be equal to
2610 class ItemIt : public Key {
2614 /// \brief Creates an iterator with a value.
2616 /// Creates an iterator with a value. It iterates on the
2617 /// keys mapped to the given value.
2618 /// \param map The IterableBoolMap.
2619 /// \param value The value.
2620 ItemIt(const IterableBoolMap& map, bool value)
2623 map._array[map._sep - 1] : INVALID) :
2624 (map._sep < int(map._array.size()) ?
2625 map._array.back() : INVALID)), _map(&map) {}
2627 /// \brief Invalid constructor \& conversion.
2629 /// This constructor initializes the iterator to be invalid.
2630 /// \sa Invalid for more details.
2631 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2633 /// \brief Increment operator.
2635 /// Increment operator.
2636 ItemIt& operator++() {
2637 int pos = _map->position(*this);
2638 int _sep = pos >= _map->_sep ? _map->_sep : 0;
2639 Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
2644 const IterableBoolMap* _map;
2649 virtual void add(const Key& key) {
2651 Parent::set(key, _array.size());
2652 _array.push_back(key);
2655 virtual void add(const std::vector<Key>& keys) {
2657 for (int i = 0; i < int(keys.size()); ++i) {
2658 Parent::set(keys[i], _array.size());
2659 _array.push_back(keys[i]);
2663 virtual void erase(const Key& key) {
2664 int pos = position(key);
2667 Parent::set(_array[_sep], pos);
2668 _array[pos] = _array[_sep];
2669 Parent::set(_array.back(), _sep);
2670 _array[_sep] = _array.back();
2673 Parent::set(_array.back(), pos);
2674 _array[pos] = _array.back();
2680 virtual void erase(const std::vector<Key>& keys) {
2681 for (int i = 0; i < int(keys.size()); ++i) {
2682 int pos = position(keys[i]);
2685 Parent::set(_array[_sep], pos);
2686 _array[pos] = _array[_sep];
2687 Parent::set(_array.back(), _sep);
2688 _array[_sep] = _array.back();
2691 Parent::set(_array.back(), pos);
2692 _array[pos] = _array.back();
2696 Parent::erase(keys);
2699 virtual void build() {
2701 typename Parent::Notifier* nf = Parent::notifier();
2703 for (nf->first(it); it != INVALID; nf->next(it)) {
2704 Parent::set(it, _array.size());
2705 _array.push_back(it);
2710 virtual void clear() {
2719 namespace _maps_bits {
2720 template <typename Item>
2721 struct IterableIntMapNode {
2722 IterableIntMapNode() : value(-1) {}
2723 IterableIntMapNode(int _value) : value(_value) {}
2729 /// \brief Dynamic iterable integer map.
2731 /// This class provides a special graph map type which can store an
2732 /// integer value for graph items (\c Node, \c Arc or \c Edge).
2733 /// For each non-negative value it is possible to iterate on the keys
2734 /// mapped to the value.
2736 /// This map is intended to be used with small integer values, for which
2737 /// it is efficient, and supports iteration only for non-negative values.
2738 /// If you need large values and/or iteration for negative integers,
2739 /// consider to use \ref IterableValueMap instead.
2741 /// This type is a reference map, so it can be modified with the
2742 /// subscript operator.
2744 /// \note The size of the data structure depends on the largest
2745 /// value in the map.
2747 /// \tparam GR The graph type.
2748 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2751 /// \see IterableBoolMap, IterableValueMap
2752 /// \see CrossRefMap
2753 template <typename GR, typename K>
2754 class IterableIntMap
2755 : protected ItemSetTraits<GR, K>::
2756 template Map<_maps_bits::IterableIntMapNode<K> >::Type {
2758 typedef typename ItemSetTraits<GR, K>::
2759 template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
2768 /// \brief Constructor of the map.
2770 /// Constructor of the map. It sets all values to -1.
2771 explicit IterableIntMap(const Graph& graph)
2774 /// \brief Constructor of the map with a given value.
2776 /// Constructor of the map with a given value.
2777 explicit IterableIntMap(const Graph& graph, int value)
2778 : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
2780 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
2788 void unlace(const Key& key) {
2789 typename Parent::Value& node = Parent::operator[](key);
2790 if (node.value < 0) return;
2791 if (node.prev != INVALID) {
2792 Parent::operator[](node.prev).next = node.next;
2794 _first[node.value] = node.next;
2796 if (node.next != INVALID) {
2797 Parent::operator[](node.next).prev = node.prev;
2799 while (!_first.empty() && _first.back() == INVALID) {
2804 void lace(const Key& key) {
2805 typename Parent::Value& node = Parent::operator[](key);
2806 if (node.value < 0) return;
2807 if (node.value >= int(_first.size())) {
2808 _first.resize(node.value + 1, INVALID);
2810 node.prev = INVALID;
2811 node.next = _first[node.value];
2812 if (node.next != INVALID) {
2813 Parent::operator[](node.next).prev = key;
2815 _first[node.value] = key;
2820 /// Indicates that the map is reference map.
2821 typedef True ReferenceMapTag;
2823 /// \brief Reference to the value of the map.
2825 /// This class is similar to the \c int type. It can
2826 /// be converted to \c int and it has the same operators.
2828 friend class IterableIntMap;
2830 Reference(IterableIntMap& map, const Key& key)
2831 : _key(key), _map(map) {}
2834 Reference& operator=(const Reference& value) {
2835 _map.set(_key, static_cast<const int&>(value));
2839 operator const int&() const {
2840 return static_cast<const IterableIntMap&>(_map)[_key];
2843 Reference& operator=(int value) {
2844 _map.set(_key, value);
2847 Reference& operator++() {
2848 _map.set(_key, _map[_key] + 1);
2851 int operator++(int) {
2852 int value = _map[_key];
2853 _map.set(_key, value + 1);
2856 Reference& operator--() {
2857 _map.set(_key, _map[_key] - 1);
2860 int operator--(int) {
2861 int value = _map[_key];
2862 _map.set(_key, value - 1);
2865 Reference& operator+=(int value) {
2866 _map.set(_key, _map[_key] + value);
2869 Reference& operator-=(int value) {
2870 _map.set(_key, _map[_key] - value);
2873 Reference& operator*=(int value) {
2874 _map.set(_key, _map[_key] * value);
2877 Reference& operator/=(int value) {
2878 _map.set(_key, _map[_key] / value);
2881 Reference& operator%=(int value) {
2882 _map.set(_key, _map[_key] % value);
2885 Reference& operator&=(int value) {
2886 _map.set(_key, _map[_key] & value);
2889 Reference& operator|=(int value) {
2890 _map.set(_key, _map[_key] | value);
2893 Reference& operator^=(int value) {
2894 _map.set(_key, _map[_key] ^ value);
2897 Reference& operator<<=(int value) {
2898 _map.set(_key, _map[_key] << value);
2901 Reference& operator>>=(int value) {
2902 _map.set(_key, _map[_key] >> value);
2908 IterableIntMap& _map;
2911 /// The const reference type.
2912 typedef const Value& ConstReference;
2914 /// \brief Gives back the maximal value plus one.
2916 /// Gives back the maximal value plus one.
2918 return _first.size();
2921 /// \brief Set operation of the map.
2923 /// Set operation of the map.
2924 void set(const Key& key, const Value& value) {
2926 Parent::operator[](key).value = value;
2930 /// \brief Const subscript operator of the map.
2932 /// Const subscript operator of the map.
2933 const Value& operator[](const Key& key) const {
2934 return Parent::operator[](key).value;
2937 /// \brief Subscript operator of the map.
2939 /// Subscript operator of the map.
2940 Reference operator[](const Key& key) {
2941 return Reference(*this, key);
2944 /// \brief Iterator for the keys with the same value.
2946 /// Iterator for the keys with the same value. It works
2947 /// like a graph item iterator, it can be converted to
2948 /// the item type of the map, incremented with \c ++ operator, and
2949 /// if the iterator leaves the last valid item, it will be equal to
2951 class ItemIt : public Key {
2955 /// \brief Invalid constructor \& conversion.
2957 /// This constructor initializes the iterator to be invalid.
2958 /// \sa Invalid for more details.
2959 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2961 /// \brief Creates an iterator with a value.
2963 /// Creates an iterator with a value. It iterates on the
2964 /// keys mapped to the given value.
2965 /// \param map The IterableIntMap.
2966 /// \param value The value.
2967 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
2968 if (value < 0 || value >= int(_map->_first.size())) {
2969 Parent::operator=(INVALID);
2971 Parent::operator=(_map->_first[value]);
2975 /// \brief Increment operator.
2977 /// Increment operator.
2978 ItemIt& operator++() {
2979 Parent::operator=(_map->IterableIntMap::Parent::
2980 operator[](static_cast<Parent&>(*this)).next);
2985 const IterableIntMap* _map;
2990 virtual void erase(const Key& key) {
2995 virtual void erase(const std::vector<Key>& keys) {
2996 for (int i = 0; i < int(keys.size()); ++i) {
2999 Parent::erase(keys);
3002 virtual void clear() {
3008 std::vector<Key> _first;
3011 namespace _maps_bits {
3012 template <typename Item, typename Value>
3013 struct IterableValueMapNode {
3014 IterableValueMapNode(Value _value = Value()) : value(_value) {}
3020 /// \brief Dynamic iterable map for comparable values.
3022 /// This class provides a special graph map type which can store a
3023 /// comparable value for graph items (\c Node, \c Arc or \c Edge).
3024 /// For each value it is possible to iterate on the keys mapped to
3025 /// the value (\c ItemIt), and the values of the map can be accessed
3026 /// with an STL compatible forward iterator (\c ValueIterator).
3027 /// The map stores a linked list for each value, which contains
3028 /// the items mapped to the value, and the used values are stored
3029 /// in balanced binary tree (\c std::map).
3031 /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
3032 /// specialized for \c bool and \c int values, respectively.
3034 /// This type is not reference map, so it cannot be modified with
3035 /// the subscript operator.
3037 /// \tparam GR The graph type.
3038 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
3040 /// \tparam V The value type of the map. It can be any comparable
3043 /// \see IterableBoolMap, IterableIntMap
3044 /// \see CrossRefMap
3045 template <typename GR, typename K, typename V>
3046 class IterableValueMap
3047 : protected ItemSetTraits<GR, K>::
3048 template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
3050 typedef typename ItemSetTraits<GR, K>::
3051 template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
3062 /// \brief Constructor of the map with a given value.
3064 /// Constructor of the map with a given value.
3065 explicit IterableValueMap(const Graph& graph,
3066 const Value& value = Value())
3067 : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
3068 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3075 void unlace(const Key& key) {
3076 typename Parent::Value& node = Parent::operator[](key);
3077 if (node.prev != INVALID) {
3078 Parent::operator[](node.prev).next = node.next;
3080 if (node.next != INVALID) {
3081 _first[node.value] = node.next;
3083 _first.erase(node.value);
3086 if (node.next != INVALID) {
3087 Parent::operator[](node.next).prev = node.prev;
3091 void lace(const Key& key) {
3092 typename Parent::Value& node = Parent::operator[](key);
3093 typename std::map<Value, Key>::iterator it = _first.find(node.value);
3094 if (it == _first.end()) {
3095 node.prev = node.next = INVALID;
3096 _first.insert(std::make_pair(node.value, key));
3098 node.prev = INVALID;
3099 node.next = it->second;
3100 if (node.next != INVALID) {
3101 Parent::operator[](node.next).prev = key;
3109 /// \brief Forward iterator for values.
3111 /// This iterator is an STL compatible forward
3112 /// iterator on the values of the map. The values can
3113 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
3115 : public std::iterator<std::forward_iterator_tag, Value> {
3116 friend class IterableValueMap;
3118 ValueIterator(typename std::map<Value, Key>::const_iterator _it)
3126 ValueIterator& operator++() { ++it; return *this; }
3128 ValueIterator operator++(int) {
3129 ValueIterator tmp(*this);
3135 const Value& operator*() const { return it->first; }
3137 const Value* operator->() const { return &(it->first); }
3140 bool operator==(ValueIterator jt) const { return it == jt.it; }
3142 bool operator!=(ValueIterator jt) const { return it != jt.it; }
3145 typename std::map<Value, Key>::const_iterator it;
3148 /// \brief Returns an iterator to the first value.
3150 /// Returns an STL compatible iterator to the
3151 /// first value of the map. The values of the
3152 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3154 ValueIterator beginValue() const {
3155 return ValueIterator(_first.begin());
3158 /// \brief Returns an iterator after the last value.
3160 /// Returns an STL compatible iterator after the
3161 /// last value of the map. The values of the
3162 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3164 ValueIterator endValue() const {
3165 return ValueIterator(_first.end());
3168 /// \brief Set operation of the map.
3170 /// Set operation of the map.
3171 void set(const Key& key, const Value& value) {
3173 Parent::operator[](key).value = value;
3177 /// \brief Const subscript operator of the map.
3179 /// Const subscript operator of the map.
3180 const Value& operator[](const Key& key) const {
3181 return Parent::operator[](key).value;
3184 /// \brief Iterator for the keys with the same value.
3186 /// Iterator for the keys with the same value. It works
3187 /// like a graph item iterator, it can be converted to
3188 /// the item type of the map, incremented with \c ++ operator, and
3189 /// if the iterator leaves the last valid item, it will be equal to
3191 class ItemIt : public Key {
3195 /// \brief Invalid constructor \& conversion.
3197 /// This constructor initializes the iterator to be invalid.
3198 /// \sa Invalid for more details.
3199 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3201 /// \brief Creates an iterator with a value.
3203 /// Creates an iterator with a value. It iterates on the
3204 /// keys which have the given value.
3205 /// \param map The IterableValueMap
3206 /// \param value The value
3207 ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
3208 typename std::map<Value, Key>::const_iterator it =
3209 map._first.find(value);
3210 if (it == map._first.end()) {
3211 Parent::operator=(INVALID);
3213 Parent::operator=(it->second);
3217 /// \brief Increment operator.
3219 /// Increment Operator.
3220 ItemIt& operator++() {
3221 Parent::operator=(_map->IterableValueMap::Parent::
3222 operator[](static_cast<Parent&>(*this)).next);
3228 const IterableValueMap* _map;
3233 virtual void add(const Key& key) {
3238 virtual void add(const std::vector<Key>& keys) {
3240 for (int i = 0; i < int(keys.size()); ++i) {
3245 virtual void erase(const Key& key) {
3250 virtual void erase(const std::vector<Key>& keys) {
3251 for (int i = 0; i < int(keys.size()); ++i) {
3254 Parent::erase(keys);
3257 virtual void build() {
3259 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3264 virtual void clear() {
3270 std::map<Value, Key> _first;
3273 /// \brief Map of the source nodes of arcs in a digraph.
3275 /// SourceMap provides access for the source node of each arc in a digraph,
3276 /// which is returned by the \c source() function of the digraph.
3277 /// \tparam GR The digraph type.
3279 template <typename GR>
3284 typedef typename GR::Arc Key;
3286 typedef typename GR::Node Value;
3288 /// \brief Constructor
3291 /// \param digraph The digraph that the map belongs to.
3292 explicit SourceMap(const GR& digraph) : _graph(digraph) {}
3294 /// \brief Returns the source node of the given arc.
3296 /// Returns the source node of the given arc.
3297 Value operator[](const Key& arc) const {
3298 return _graph.source(arc);
3305 /// \brief Returns a \c SourceMap class.
3307 /// This function just returns an \c SourceMap class.
3308 /// \relates SourceMap
3309 template <typename GR>
3310 inline SourceMap<GR> sourceMap(const GR& graph) {
3311 return SourceMap<GR>(graph);
3314 /// \brief Map of the target nodes of arcs in a digraph.
3316 /// TargetMap provides access for the target node of each arc in a digraph,
3317 /// which is returned by the \c target() function of the digraph.
3318 /// \tparam GR The digraph type.
3320 template <typename GR>
3325 typedef typename GR::Arc Key;
3327 typedef typename GR::Node Value;
3329 /// \brief Constructor
3332 /// \param digraph The digraph that the map belongs to.
3333 explicit TargetMap(const GR& digraph) : _graph(digraph) {}
3335 /// \brief Returns the target node of the given arc.
3337 /// Returns the target node of the given arc.
3338 Value operator[](const Key& e) const {
3339 return _graph.target(e);
3346 /// \brief Returns a \c TargetMap class.
3348 /// This function just returns a \c TargetMap class.
3349 /// \relates TargetMap
3350 template <typename GR>
3351 inline TargetMap<GR> targetMap(const GR& graph) {
3352 return TargetMap<GR>(graph);
3355 /// \brief Map of the "forward" directed arc view of edges in a graph.
3357 /// ForwardMap provides access for the "forward" directed arc view of
3358 /// each edge in a graph, which is returned by the \c direct() function
3359 /// of the graph with \c true parameter.
3360 /// \tparam GR The graph type.
3361 /// \see BackwardMap
3362 template <typename GR>
3366 typedef typename GR::Arc Value;
3367 typedef typename GR::Edge Key;
3369 /// \brief Constructor
3372 /// \param graph The graph that the map belongs to.
3373 explicit ForwardMap(const GR& graph) : _graph(graph) {}
3375 /// \brief Returns the "forward" directed arc view of the given edge.
3377 /// Returns the "forward" directed arc view of the given edge.
3378 Value operator[](const Key& key) const {
3379 return _graph.direct(key, true);
3386 /// \brief Returns a \c ForwardMap class.
3388 /// This function just returns an \c ForwardMap class.
3389 /// \relates ForwardMap
3390 template <typename GR>
3391 inline ForwardMap<GR> forwardMap(const GR& graph) {
3392 return ForwardMap<GR>(graph);
3395 /// \brief Map of the "backward" directed arc view of edges in a graph.
3397 /// BackwardMap provides access for the "backward" directed arc view of
3398 /// each edge in a graph, which is returned by the \c direct() function
3399 /// of the graph with \c false parameter.
3400 /// \tparam GR The graph type.
3402 template <typename GR>
3406 typedef typename GR::Arc Value;
3407 typedef typename GR::Edge Key;
3409 /// \brief Constructor
3412 /// \param graph The graph that the map belongs to.
3413 explicit BackwardMap(const GR& graph) : _graph(graph) {}
3415 /// \brief Returns the "backward" directed arc view of the given edge.
3417 /// Returns the "backward" directed arc view of the given edge.
3418 Value operator[](const Key& key) const {
3419 return _graph.direct(key, false);
3426 /// \brief Returns a \c BackwardMap class
3428 /// This function just returns a \c BackwardMap class.
3429 /// \relates BackwardMap
3430 template <typename GR>
3431 inline BackwardMap<GR> backwardMap(const GR& graph) {
3432 return BackwardMap<GR>(graph);
3435 /// \brief Map of the in-degrees of nodes in a digraph.
3437 /// This map returns the in-degree of a node. Once it is constructed,
3438 /// the degrees are stored in a standard \c NodeMap, so each query is done
3439 /// in constant time. On the other hand, the values are updated automatically
3440 /// whenever the digraph changes.
3442 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3443 /// may provide alternative ways to modify the digraph.
3444 /// The correct behavior of InDegMap is not guarantied if these additional
3445 /// features are used. For example the functions
3446 /// \ref ListDigraph::changeSource() "changeSource()",
3447 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3448 /// \ref ListDigraph::reverseArc() "reverseArc()"
3449 /// of \ref ListDigraph will \e not update the degree values correctly.
3452 template <typename GR>
3454 : protected ItemSetTraits<GR, typename GR::Arc>
3455 ::ItemNotifier::ObserverBase {
3459 /// The graph type of InDegMap
3463 typedef typename Digraph::Node Key;
3467 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3468 ::ItemNotifier::ObserverBase Parent;
3473 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3476 typedef typename ItemSetTraits<Digraph, Key>::
3477 template Map<int>::Type Parent;
3479 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3481 virtual void add(const Key& key) {
3483 Parent::set(key, 0);
3486 virtual void add(const std::vector<Key>& keys) {
3488 for (int i = 0; i < int(keys.size()); ++i) {
3489 Parent::set(keys[i], 0);
3493 virtual void build() {
3496 typename Parent::Notifier* nf = Parent::notifier();
3497 for (nf->first(it); it != INVALID; nf->next(it)) {
3505 /// \brief Constructor.
3507 /// Constructor for creating an in-degree map.
3508 explicit InDegMap(const Digraph& graph)
3509 : _digraph(graph), _deg(graph) {
3510 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3512 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3513 _deg[it] = countInArcs(_digraph, it);
3517 /// \brief Gives back the in-degree of a Node.
3519 /// Gives back the in-degree of a Node.
3520 int operator[](const Key& key) const {
3526 typedef typename Digraph::Arc Arc;
3528 virtual void add(const Arc& arc) {
3529 ++_deg[_digraph.target(arc)];
3532 virtual void add(const std::vector<Arc>& arcs) {
3533 for (int i = 0; i < int(arcs.size()); ++i) {
3534 ++_deg[_digraph.target(arcs[i])];
3538 virtual void erase(const Arc& arc) {
3539 --_deg[_digraph.target(arc)];
3542 virtual void erase(const std::vector<Arc>& arcs) {
3543 for (int i = 0; i < int(arcs.size()); ++i) {
3544 --_deg[_digraph.target(arcs[i])];
3548 virtual void build() {
3549 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3550 _deg[it] = countInArcs(_digraph, it);
3554 virtual void clear() {
3555 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3561 const Digraph& _digraph;
3565 /// \brief Map of the out-degrees of nodes in a digraph.
3567 /// This map returns the out-degree of a node. Once it is constructed,
3568 /// the degrees are stored in a standard \c NodeMap, so each query is done
3569 /// in constant time. On the other hand, the values are updated automatically
3570 /// whenever the digraph changes.
3572 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3573 /// may provide alternative ways to modify the digraph.
3574 /// The correct behavior of OutDegMap is not guarantied if these additional
3575 /// features are used. For example the functions
3576 /// \ref ListDigraph::changeSource() "changeSource()",
3577 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3578 /// \ref ListDigraph::reverseArc() "reverseArc()"
3579 /// of \ref ListDigraph will \e not update the degree values correctly.
3582 template <typename GR>
3584 : protected ItemSetTraits<GR, typename GR::Arc>
3585 ::ItemNotifier::ObserverBase {
3589 /// The graph type of OutDegMap
3593 typedef typename Digraph::Node Key;
3597 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3598 ::ItemNotifier::ObserverBase Parent;
3603 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3606 typedef typename ItemSetTraits<Digraph, Key>::
3607 template Map<int>::Type Parent;
3609 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3611 virtual void add(const Key& key) {
3613 Parent::set(key, 0);
3615 virtual void add(const std::vector<Key>& keys) {
3617 for (int i = 0; i < int(keys.size()); ++i) {
3618 Parent::set(keys[i], 0);
3621 virtual void build() {
3624 typename Parent::Notifier* nf = Parent::notifier();
3625 for (nf->first(it); it != INVALID; nf->next(it)) {
3633 /// \brief Constructor.
3635 /// Constructor for creating an out-degree map.
3636 explicit OutDegMap(const Digraph& graph)
3637 : _digraph(graph), _deg(graph) {
3638 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3640 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3641 _deg[it] = countOutArcs(_digraph, it);
3645 /// \brief Gives back the out-degree of a Node.
3647 /// Gives back the out-degree of a Node.
3648 int operator[](const Key& key) const {
3654 typedef typename Digraph::Arc Arc;
3656 virtual void add(const Arc& arc) {
3657 ++_deg[_digraph.source(arc)];
3660 virtual void add(const std::vector<Arc>& arcs) {
3661 for (int i = 0; i < int(arcs.size()); ++i) {
3662 ++_deg[_digraph.source(arcs[i])];
3666 virtual void erase(const Arc& arc) {
3667 --_deg[_digraph.source(arc)];
3670 virtual void erase(const std::vector<Arc>& arcs) {
3671 for (int i = 0; i < int(arcs.size()); ++i) {
3672 --_deg[_digraph.source(arcs[i])];
3676 virtual void build() {
3677 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3678 _deg[it] = countOutArcs(_digraph, it);
3682 virtual void clear() {
3683 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3689 const Digraph& _digraph;
3693 /// \brief Potential difference map
3695 /// PotentialDifferenceMap returns the difference between the potentials of
3696 /// the source and target nodes of each arc in a digraph, i.e. it returns
3698 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3700 /// \tparam GR The digraph type.
3701 /// \tparam POT A node map storing the potentials.
3702 template <typename GR, typename POT>
3703 class PotentialDifferenceMap {
3706 typedef typename GR::Arc Key;
3708 typedef typename POT::Value Value;
3710 /// \brief Constructor
3712 /// Contructor of the map.
3713 explicit PotentialDifferenceMap(const GR& gr,
3714 const POT& potential)
3715 : _digraph(gr), _potential(potential) {}
3717 /// \brief Returns the potential difference for the given arc.
3719 /// Returns the potential difference for the given arc, i.e.
3721 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3723 Value operator[](const Key& arc) const {
3724 return _potential[_digraph.target(arc)] -
3725 _potential[_digraph.source(arc)];
3730 const POT& _potential;
3733 /// \brief Returns a PotentialDifferenceMap.
3735 /// This function just returns a PotentialDifferenceMap.
3736 /// \relates PotentialDifferenceMap
3737 template <typename GR, typename POT>
3738 PotentialDifferenceMap<GR, POT>
3739 potentialDifferenceMap(const GR& gr, const POT& potential) {
3740 return PotentialDifferenceMap<GR, POT>(gr, potential);
3746 #endif // LEMON_MAPS_H