1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2011
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
26 #include <lemon/core.h>
30 ///\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 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 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 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 with some data structures, for example
235 /// \c UnionFind, \c BinHeap, when the used items are small
236 /// integers. This map conforms 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 Reference operator[](const Key &k) {
307 ConstReference operator[](const Key &k) const {
312 void set(const Key &k, const Value &v) {
317 /// Returns a \c RangeMap class
319 /// This function just returns a \c RangeMap class.
320 /// \relates RangeMap
322 inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
323 return RangeMap<V>(size, value);
326 /// \brief Returns a \c RangeMap class created from an appropriate
329 /// This function just returns a \c RangeMap class created from an
330 /// appropriate \c std::vector.
331 /// \relates RangeMap
333 inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
334 return RangeMap<V>(vector);
338 /// Map type based on \c std::map
340 /// This map is essentially a wrapper for \c std::map with addition
341 /// that you can specify a default value for the keys that are not
342 /// stored actually. This value can be different from the default
343 /// contructed value (i.e. \c %Value()).
344 /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
347 /// This map is useful if a default value should be assigned to most of
348 /// the keys and different values should be assigned only to a few
349 /// keys (i.e. the map is "sparse").
350 /// The name of this type also refers to this important usage.
352 /// Apart form that this map can be used in many other cases since it
353 /// is based on \c std::map, which is a general associative container.
354 /// However keep in mind that it is usually not as efficient as other
357 /// The simplest way of using this map is through the sparseMap()
359 template <typename K, typename V, typename Comp = std::less<K> >
360 class SparseMap : public MapBase<K, V> {
361 template <typename K1, typename V1, typename C1>
362 friend class SparseMap;
370 typedef Value& Reference;
371 /// Const reference type
372 typedef const Value& ConstReference;
374 typedef True ReferenceMapTag;
378 typedef std::map<K, V, Comp> Map;
384 /// \brief Constructor with specified default value.
385 SparseMap(const Value &value = Value()) : _value(value) {}
386 /// \brief Constructs the map from an appropriate \c std::map, and
387 /// explicitly specifies a default value.
388 template <typename V1, typename Comp1>
389 SparseMap(const std::map<Key, V1, Comp1> &map,
390 const Value &value = Value())
391 : _map(map.begin(), map.end()), _value(value) {}
393 /// \brief Constructs the map from another \c SparseMap.
394 template<typename V1, typename Comp1>
395 SparseMap(const SparseMap<Key, V1, Comp1> &c)
396 : _map(c._map.begin(), c._map.end()), _value(c._value) {}
400 SparseMap& operator=(const SparseMap&);
405 Reference operator[](const Key &k) {
406 typename Map::iterator it = _map.lower_bound(k);
407 if (it != _map.end() && !_map.key_comp()(k, it->first))
410 return _map.insert(it, std::make_pair(k, _value))->second;
414 ConstReference operator[](const Key &k) const {
415 typename Map::const_iterator it = _map.find(k);
416 if (it != _map.end())
423 void set(const Key &k, const Value &v) {
424 typename Map::iterator it = _map.lower_bound(k);
425 if (it != _map.end() && !_map.key_comp()(k, it->first))
428 _map.insert(it, std::make_pair(k, v));
432 void setAll(const Value &v) {
438 /// Returns a \c SparseMap class
440 /// This function just returns a \c SparseMap class with specified
442 /// \relates SparseMap
443 template<typename K, typename V, typename Compare>
444 inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
445 return SparseMap<K, V, Compare>(value);
448 template<typename K, typename V>
449 inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
450 return SparseMap<K, V, std::less<K> >(value);
453 /// \brief Returns a \c SparseMap class created from an appropriate
456 /// This function just returns a \c SparseMap class created from an
457 /// appropriate \c std::map.
458 /// \relates SparseMap
459 template<typename K, typename V, typename Compare>
460 inline SparseMap<K, V, Compare>
461 sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
463 return SparseMap<K, V, Compare>(map, value);
468 /// \addtogroup map_adaptors
471 /// Composition of two maps
473 /// This \ref concepts::ReadMap "read-only map" returns the
474 /// composition of two given maps. That is to say, if \c m1 is of
475 /// type \c M1 and \c m2 is of \c M2, then for
477 /// ComposeMap<M1, M2> cm(m1,m2);
479 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
481 /// The \c Key type of the map is inherited from \c M2 and the
482 /// \c Value type is from \c M1.
483 /// \c M2::Value must be convertible to \c M1::Key.
485 /// The simplest way of using this map is through the composeMap()
489 template <typename M1, typename M2>
490 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
495 typedef typename M2::Key Key;
497 typedef typename M1::Value Value;
500 ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
503 typename MapTraits<M1>::ConstReturnValue
504 operator[](const Key &k) const { return _m1[_m2[k]]; }
507 /// Returns a \c ComposeMap class
509 /// This function just returns a \c ComposeMap class.
511 /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
512 /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
513 /// will be equal to <tt>m1[m2[x]]</tt>.
515 /// \relates ComposeMap
516 template <typename M1, typename M2>
517 inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
518 return ComposeMap<M1, M2>(m1, m2);
522 /// Combination of two maps using an STL (binary) functor.
524 /// This \ref concepts::ReadMap "read-only map" takes two maps and a
525 /// binary functor and returns the combination of the two given maps
526 /// using the functor.
527 /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
528 /// and \c f is of \c F, then for
530 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
532 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
534 /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
535 /// must be convertible to \c M2::Key) and the \c Value type is \c V.
536 /// \c M2::Value and \c M1::Value must be convertible to the
537 /// corresponding input parameter of \c F and the return type of \c F
538 /// must be convertible to \c V.
540 /// The simplest way of using this map is through the combineMap()
544 template<typename M1, typename M2, typename F,
545 typename V = typename F::result_type>
546 class CombineMap : public MapBase<typename M1::Key, V> {
552 typedef typename M1::Key Key;
557 CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
558 : _m1(m1), _m2(m2), _f(f) {}
560 Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
563 /// Returns a \c CombineMap class
565 /// This function just returns a \c CombineMap class.
567 /// For example, if \c m1 and \c m2 are both maps with \c double
570 /// combineMap(m1,m2,std::plus<double>())
577 /// This function is specialized for adaptable binary function
578 /// classes and C++ functions.
580 /// \relates CombineMap
581 template<typename M1, typename M2, typename F, typename V>
582 inline CombineMap<M1, M2, F, V>
583 combineMap(const M1 &m1, const M2 &m2, const F &f) {
584 return CombineMap<M1, M2, F, V>(m1,m2,f);
587 template<typename M1, typename M2, typename F>
588 inline CombineMap<M1, M2, F, typename F::result_type>
589 combineMap(const M1 &m1, const M2 &m2, const F &f) {
590 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
593 template<typename M1, typename M2, typename K1, typename K2, typename V>
594 inline CombineMap<M1, M2, V (*)(K1, K2), V>
595 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
596 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
600 /// Converts an STL style (unary) functor to a map
602 /// This \ref concepts::ReadMap "read-only map" returns the value
603 /// of a given functor. Actually, it just wraps the functor and
604 /// provides the \c Key and \c Value typedefs.
606 /// Template parameters \c K and \c V will become its \c Key and
607 /// \c Value. In most cases they have to be given explicitly because
608 /// a functor typically does not provide \c argument_type and
609 /// \c result_type typedefs.
610 /// Parameter \c F is the type of the used functor.
612 /// The simplest way of using this map is through the functorToMap()
617 typename K = typename F::argument_type,
618 typename V = typename F::result_type>
619 class FunctorToMap : public MapBase<K, V> {
628 FunctorToMap(const F &f = F()) : _f(f) {}
630 Value operator[](const Key &k) const { return _f(k); }
633 /// Returns a \c FunctorToMap class
635 /// This function just returns a \c FunctorToMap class.
637 /// This function is specialized for adaptable binary function
638 /// classes and C++ functions.
640 /// \relates FunctorToMap
641 template<typename K, typename V, typename F>
642 inline FunctorToMap<F, K, V> functorToMap(const F &f) {
643 return FunctorToMap<F, K, V>(f);
646 template <typename F>
647 inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
648 functorToMap(const F &f)
650 return FunctorToMap<F, typename F::argument_type,
651 typename F::result_type>(f);
654 template <typename K, typename V>
655 inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
656 return FunctorToMap<V (*)(K), K, V>(f);
660 /// Converts a map to an STL style (unary) functor
662 /// This class converts a map to an STL style (unary) functor.
663 /// That is it provides an <tt>operator()</tt> to read its values.
665 /// For the sake of convenience it also works as a usual
666 /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
667 /// and the \c Key and \c Value typedefs also exist.
669 /// The simplest way of using this map is through the mapToFunctor()
673 template <typename M>
674 class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
678 typedef typename M::Key Key;
680 typedef typename M::Value Value;
682 typedef typename M::Key argument_type;
683 typedef typename M::Value result_type;
686 MapToFunctor(const M &m) : _m(m) {}
688 Value operator()(const Key &k) const { return _m[k]; }
690 Value operator[](const Key &k) const { return _m[k]; }
693 /// Returns a \c MapToFunctor class
695 /// This function just returns a \c MapToFunctor class.
696 /// \relates MapToFunctor
698 inline MapToFunctor<M> mapToFunctor(const M &m) {
699 return MapToFunctor<M>(m);
703 /// \brief Map adaptor to convert the \c Value type of a map to
704 /// another type using the default conversion.
706 /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
707 /// "readable map" to another type using the default conversion.
708 /// The \c Key type of it is inherited from \c M and the \c Value
710 /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
712 /// The simplest way of using this map is through the convertMap()
714 template <typename M, typename V>
715 class ConvertMap : public MapBase<typename M::Key, V> {
719 typedef typename M::Key Key;
726 /// \param m The underlying map.
727 ConvertMap(const M &m) : _m(m) {}
730 Value operator[](const Key &k) const { return _m[k]; }
733 /// Returns a \c ConvertMap class
735 /// This function just returns a \c ConvertMap class.
736 /// \relates ConvertMap
737 template<typename V, typename M>
738 inline ConvertMap<M, V> convertMap(const M &map) {
739 return ConvertMap<M, V>(map);
743 /// Applies all map setting operations to two maps
745 /// This map has two \ref concepts::WriteMap "writable map" parameters
746 /// and each write request will be passed to both of them.
747 /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
748 /// operations will return the corresponding values of \c M1.
750 /// The \c Key and \c Value types are inherited from \c M1.
751 /// The \c Key and \c Value of \c M2 must be convertible from those
754 /// The simplest way of using this map is through the forkMap()
756 template<typename M1, typename M2>
757 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
762 typedef typename M1::Key Key;
764 typedef typename M1::Value Value;
767 ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
768 /// Returns the value associated with the given key in the first map.
769 Value operator[](const Key &k) const { return _m1[k]; }
770 /// Sets the value associated with the given key in both maps.
771 void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
774 /// Returns a \c ForkMap class
776 /// This function just returns a \c ForkMap class.
778 template <typename M1, typename M2>
779 inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
780 return ForkMap<M1,M2>(m1,m2);
786 /// This \ref concepts::ReadMap "read-only map" returns the sum
787 /// of the values of the two given maps.
788 /// Its \c Key and \c Value types are inherited from \c M1.
789 /// The \c Key and \c Value of \c M2 must be convertible to those of
792 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
794 /// AddMap<M1,M2> am(m1,m2);
796 /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
798 /// The simplest way of using this map is through the addMap()
801 /// \sa SubMap, MulMap, DivMap
802 /// \sa ShiftMap, ShiftWriteMap
803 template<typename M1, typename M2>
804 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
809 typedef typename M1::Key Key;
811 typedef typename M1::Value Value;
814 AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
816 Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
819 /// Returns an \c AddMap class
821 /// This function just returns an \c AddMap class.
823 /// For example, if \c m1 and \c m2 are both maps with \c double
824 /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
825 /// <tt>m1[x]+m2[x]</tt>.
828 template<typename M1, typename M2>
829 inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
830 return AddMap<M1, M2>(m1,m2);
834 /// Difference of two maps
836 /// This \ref concepts::ReadMap "read-only map" returns the difference
837 /// of the values of the two given maps.
838 /// Its \c Key and \c Value types are inherited from \c M1.
839 /// The \c Key and \c Value of \c M2 must be convertible to those of
842 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
844 /// SubMap<M1,M2> sm(m1,m2);
846 /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
848 /// The simplest way of using this map is through the subMap()
851 /// \sa AddMap, MulMap, DivMap
852 template<typename M1, typename M2>
853 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
858 typedef typename M1::Key Key;
860 typedef typename M1::Value Value;
863 SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
865 Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
868 /// Returns a \c SubMap class
870 /// This function just returns a \c SubMap class.
872 /// For example, if \c m1 and \c m2 are both maps with \c double
873 /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
874 /// <tt>m1[x]-m2[x]</tt>.
877 template<typename M1, typename M2>
878 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
879 return SubMap<M1, M2>(m1,m2);
883 /// Product of two maps
885 /// This \ref concepts::ReadMap "read-only map" returns the product
886 /// of the values of the two given maps.
887 /// Its \c Key and \c Value types are inherited from \c M1.
888 /// The \c Key and \c Value of \c M2 must be convertible to those of
891 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
893 /// MulMap<M1,M2> mm(m1,m2);
895 /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
897 /// The simplest way of using this map is through the mulMap()
900 /// \sa AddMap, SubMap, DivMap
901 /// \sa ScaleMap, ScaleWriteMap
902 template<typename M1, typename M2>
903 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
908 typedef typename M1::Key Key;
910 typedef typename M1::Value Value;
913 MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
915 Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
918 /// Returns a \c MulMap class
920 /// This function just returns a \c MulMap class.
922 /// For example, if \c m1 and \c m2 are both maps with \c double
923 /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
924 /// <tt>m1[x]*m2[x]</tt>.
927 template<typename M1, typename M2>
928 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
929 return MulMap<M1, M2>(m1,m2);
933 /// Quotient of two maps
935 /// This \ref concepts::ReadMap "read-only map" returns the quotient
936 /// of the values of the two given maps.
937 /// Its \c Key and \c Value types are inherited from \c M1.
938 /// The \c Key and \c Value of \c M2 must be convertible to those of
941 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
943 /// DivMap<M1,M2> dm(m1,m2);
945 /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
947 /// The simplest way of using this map is through the divMap()
950 /// \sa AddMap, SubMap, MulMap
951 template<typename M1, typename M2>
952 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
957 typedef typename M1::Key Key;
959 typedef typename M1::Value Value;
962 DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
964 Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
967 /// Returns a \c DivMap class
969 /// This function just returns a \c DivMap class.
971 /// For example, if \c m1 and \c m2 are both maps with \c double
972 /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
973 /// <tt>m1[x]/m2[x]</tt>.
976 template<typename M1, typename M2>
977 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
978 return DivMap<M1, M2>(m1,m2);
982 /// Shifts a map with a constant.
984 /// This \ref concepts::ReadMap "read-only map" returns the sum of
985 /// the given map and a constant value (i.e. it shifts the map with
986 /// the constant). Its \c Key and \c Value are inherited from \c M.
990 /// ShiftMap<M> sh(m,v);
994 /// ConstMap<M::Key, M::Value> cm(v);
995 /// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
998 /// The simplest way of using this map is through the shiftMap()
1001 /// \sa ShiftWriteMap
1002 template<typename M, typename C = typename M::Value>
1003 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
1008 typedef typename M::Key Key;
1010 typedef typename M::Value Value;
1015 /// \param m The undelying map.
1016 /// \param v The constant value.
1017 ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1019 Value operator[](const Key &k) const { return _m[k]+_v; }
1022 /// Shifts a map with a constant (read-write version).
1024 /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1025 /// of the given map and a constant value (i.e. it shifts the map with
1026 /// the constant). Its \c Key and \c Value are inherited from \c M.
1027 /// It makes also possible to write the map.
1029 /// The simplest way of using this map is through the shiftWriteMap()
1033 template<typename M, typename C = typename M::Value>
1034 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1039 typedef typename M::Key Key;
1041 typedef typename M::Value Value;
1046 /// \param m The undelying map.
1047 /// \param v The constant value.
1048 ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1050 Value operator[](const Key &k) const { return _m[k]+_v; }
1052 void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1055 /// Returns a \c ShiftMap class
1057 /// This function just returns a \c ShiftMap class.
1059 /// For example, if \c m is a map with \c double values and \c v is
1060 /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1061 /// <tt>m[x]+v</tt>.
1063 /// \relates ShiftMap
1064 template<typename M, typename C>
1065 inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1066 return ShiftMap<M, C>(m,v);
1069 /// Returns a \c ShiftWriteMap class
1071 /// This function just returns a \c ShiftWriteMap class.
1073 /// For example, if \c m is a map with \c double values and \c v is
1074 /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1075 /// <tt>m[x]+v</tt>.
1076 /// Moreover it makes also possible to write the map.
1078 /// \relates ShiftWriteMap
1079 template<typename M, typename C>
1080 inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1081 return ShiftWriteMap<M, C>(m,v);
1085 /// Scales a map with a constant.
1087 /// This \ref concepts::ReadMap "read-only map" returns the value of
1088 /// the given map multiplied from the left side with a constant value.
1089 /// Its \c Key and \c Value are inherited from \c M.
1093 /// ScaleMap<M> sc(m,v);
1095 /// is equivalent to
1097 /// ConstMap<M::Key, M::Value> cm(v);
1098 /// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1101 /// The simplest way of using this map is through the scaleMap()
1104 /// \sa ScaleWriteMap
1105 template<typename M, typename C = typename M::Value>
1106 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1111 typedef typename M::Key Key;
1113 typedef typename M::Value Value;
1118 /// \param m The undelying map.
1119 /// \param v The constant value.
1120 ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1122 Value operator[](const Key &k) const { return _v*_m[k]; }
1125 /// Scales a map with a constant (read-write version).
1127 /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1128 /// the given map multiplied from the left side with a constant value.
1129 /// Its \c Key and \c Value are inherited from \c M.
1130 /// It can also be used as write map if the \c / operator is defined
1131 /// between \c Value and \c C and the given multiplier is not zero.
1133 /// The simplest way of using this map is through the scaleWriteMap()
1137 template<typename M, typename C = typename M::Value>
1138 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1143 typedef typename M::Key Key;
1145 typedef typename M::Value Value;
1150 /// \param m The undelying map.
1151 /// \param v The constant value.
1152 ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1154 Value operator[](const Key &k) const { return _v*_m[k]; }
1156 void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1159 /// Returns a \c ScaleMap class
1161 /// This function just returns a \c ScaleMap class.
1163 /// For example, if \c m is a map with \c double values and \c v is
1164 /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1165 /// <tt>v*m[x]</tt>.
1167 /// \relates ScaleMap
1168 template<typename M, typename C>
1169 inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1170 return ScaleMap<M, C>(m,v);
1173 /// Returns a \c ScaleWriteMap class
1175 /// This function just returns a \c ScaleWriteMap class.
1177 /// For example, if \c m is a map with \c double values and \c v is
1178 /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1179 /// <tt>v*m[x]</tt>.
1180 /// Moreover it makes also possible to write the map.
1182 /// \relates ScaleWriteMap
1183 template<typename M, typename C>
1184 inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1185 return ScaleWriteMap<M, C>(m,v);
1189 /// Negative of a map
1191 /// This \ref concepts::ReadMap "read-only map" returns the negative
1192 /// of the values of the given map (using the unary \c - operator).
1193 /// Its \c Key and \c Value are inherited from \c M.
1195 /// If M::Value is \c int, \c double etc., then
1197 /// NegMap<M> neg(m);
1199 /// is equivalent to
1201 /// ScaleMap<M> neg(m,-1);
1204 /// The simplest way of using this map is through the negMap()
1208 template<typename M>
1209 class NegMap : public MapBase<typename M::Key, typename M::Value> {
1213 typedef typename M::Key Key;
1215 typedef typename M::Value Value;
1218 NegMap(const M &m) : _m(m) {}
1220 Value operator[](const Key &k) const { return -_m[k]; }
1223 /// Negative of a map (read-write version)
1225 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1226 /// negative of the values of the given map (using the unary \c -
1228 /// Its \c Key and \c Value are inherited from \c M.
1229 /// It makes also possible to write the map.
1231 /// If M::Value is \c int, \c double etc., then
1233 /// NegWriteMap<M> neg(m);
1235 /// is equivalent to
1237 /// ScaleWriteMap<M> neg(m,-1);
1240 /// The simplest way of using this map is through the negWriteMap()
1244 template<typename M>
1245 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1249 typedef typename M::Key Key;
1251 typedef typename M::Value Value;
1254 NegWriteMap(M &m) : _m(m) {}
1256 Value operator[](const Key &k) const { return -_m[k]; }
1258 void set(const Key &k, const Value &v) { _m.set(k, -v); }
1261 /// Returns a \c NegMap class
1263 /// This function just returns a \c NegMap class.
1265 /// For example, if \c m is a map with \c double values, then
1266 /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1269 template <typename M>
1270 inline NegMap<M> negMap(const M &m) {
1271 return NegMap<M>(m);
1274 /// Returns a \c NegWriteMap class
1276 /// This function just returns a \c NegWriteMap class.
1278 /// For example, if \c m is a map with \c double values, then
1279 /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1280 /// Moreover it makes also possible to write the map.
1282 /// \relates NegWriteMap
1283 template <typename M>
1284 inline NegWriteMap<M> negWriteMap(M &m) {
1285 return NegWriteMap<M>(m);
1289 /// Absolute value of a map
1291 /// This \ref concepts::ReadMap "read-only map" returns the absolute
1292 /// value of the values of the given map.
1293 /// Its \c Key and \c Value are inherited from \c M.
1294 /// \c Value must be comparable to \c 0 and the unary \c -
1295 /// operator must be defined for it, of course.
1297 /// The simplest way of using this map is through the absMap()
1299 template<typename M>
1300 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1304 typedef typename M::Key Key;
1306 typedef typename M::Value Value;
1309 AbsMap(const M &m) : _m(m) {}
1311 Value operator[](const Key &k) const {
1313 return tmp >= 0 ? tmp : -tmp;
1318 /// Returns an \c AbsMap class
1320 /// This function just returns an \c AbsMap class.
1322 /// For example, if \c m is a map with \c double values, then
1323 /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1324 /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1328 template<typename M>
1329 inline AbsMap<M> absMap(const M &m) {
1330 return AbsMap<M>(m);
1335 // Logical maps and map adaptors:
1337 /// \addtogroup maps
1340 /// Constant \c true map.
1342 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1349 /// is equivalent to
1351 /// ConstMap<K,bool> tm(true);
1356 template <typename K>
1357 class TrueMap : public MapBase<K, bool> {
1364 /// Gives back \c true.
1365 Value operator[](const Key&) const { return true; }
1368 /// Returns a \c TrueMap class
1370 /// This function just returns a \c TrueMap class.
1371 /// \relates TrueMap
1372 template<typename K>
1373 inline TrueMap<K> trueMap() {
1374 return TrueMap<K>();
1378 /// Constant \c false map.
1380 /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1387 /// is equivalent to
1389 /// ConstMap<K,bool> fm(false);
1394 template <typename K>
1395 class FalseMap : public MapBase<K, bool> {
1402 /// Gives back \c false.
1403 Value operator[](const Key&) const { return false; }
1406 /// Returns a \c FalseMap class
1408 /// This function just returns a \c FalseMap class.
1409 /// \relates FalseMap
1410 template<typename K>
1411 inline FalseMap<K> falseMap() {
1412 return FalseMap<K>();
1417 /// \addtogroup map_adaptors
1420 /// Logical 'and' of two maps
1422 /// This \ref concepts::ReadMap "read-only map" returns the logical
1423 /// 'and' of the values of the two given maps.
1424 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1425 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1427 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1429 /// AndMap<M1,M2> am(m1,m2);
1431 /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1433 /// The simplest way of using this map is through the andMap()
1437 /// \sa NotMap, NotWriteMap
1438 template<typename M1, typename M2>
1439 class AndMap : public MapBase<typename M1::Key, bool> {
1444 typedef typename M1::Key Key;
1449 AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1451 Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1454 /// Returns an \c AndMap class
1456 /// This function just returns an \c AndMap class.
1458 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1459 /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1460 /// <tt>m1[x]&&m2[x]</tt>.
1463 template<typename M1, typename M2>
1464 inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1465 return AndMap<M1, M2>(m1,m2);
1469 /// Logical 'or' of two maps
1471 /// This \ref concepts::ReadMap "read-only map" returns the logical
1472 /// 'or' of the values of the two given maps.
1473 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1474 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1476 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1478 /// OrMap<M1,M2> om(m1,m2);
1480 /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1482 /// The simplest way of using this map is through the orMap()
1486 /// \sa NotMap, NotWriteMap
1487 template<typename M1, typename M2>
1488 class OrMap : public MapBase<typename M1::Key, bool> {
1493 typedef typename M1::Key Key;
1498 OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1500 Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1503 /// Returns an \c OrMap class
1505 /// This function just returns an \c OrMap class.
1507 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1508 /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1509 /// <tt>m1[x]||m2[x]</tt>.
1512 template<typename M1, typename M2>
1513 inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1514 return OrMap<M1, M2>(m1,m2);
1518 /// Logical 'not' of a map
1520 /// This \ref concepts::ReadMap "read-only map" returns the logical
1521 /// negation of the values of the given map.
1522 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1524 /// The simplest way of using this map is through the notMap()
1528 template <typename M>
1529 class NotMap : public MapBase<typename M::Key, bool> {
1533 typedef typename M::Key Key;
1538 NotMap(const M &m) : _m(m) {}
1540 Value operator[](const Key &k) const { return !_m[k]; }
1543 /// Logical 'not' of a map (read-write version)
1545 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1546 /// logical negation of the values of the given map.
1547 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1548 /// It makes also possible to write the map. When a value is set,
1549 /// the opposite value is set to the original map.
1551 /// The simplest way of using this map is through the notWriteMap()
1555 template <typename M>
1556 class NotWriteMap : public MapBase<typename M::Key, bool> {
1560 typedef typename M::Key Key;
1565 NotWriteMap(M &m) : _m(m) {}
1567 Value operator[](const Key &k) const { return !_m[k]; }
1569 void set(const Key &k, bool v) { _m.set(k, !v); }
1572 /// Returns a \c NotMap class
1574 /// This function just returns a \c NotMap class.
1576 /// For example, if \c m is a map with \c bool values, then
1577 /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1580 template <typename M>
1581 inline NotMap<M> notMap(const M &m) {
1582 return NotMap<M>(m);
1585 /// Returns a \c NotWriteMap class
1587 /// This function just returns a \c NotWriteMap class.
1589 /// For example, if \c m is a map with \c bool values, then
1590 /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1591 /// Moreover it makes also possible to write the map.
1593 /// \relates NotWriteMap
1594 template <typename M>
1595 inline NotWriteMap<M> notWriteMap(M &m) {
1596 return NotWriteMap<M>(m);
1600 /// Combination of two maps using the \c == operator
1602 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1603 /// the keys for which the corresponding values of the two maps are
1605 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1606 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1608 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1610 /// EqualMap<M1,M2> em(m1,m2);
1612 /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1614 /// The simplest way of using this map is through the equalMap()
1618 template<typename M1, typename M2>
1619 class EqualMap : public MapBase<typename M1::Key, bool> {
1624 typedef typename M1::Key Key;
1629 EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1631 Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1634 /// Returns an \c EqualMap class
1636 /// This function just returns an \c EqualMap class.
1638 /// For example, if \c m1 and \c m2 are maps with keys and values of
1639 /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1640 /// <tt>m1[x]==m2[x]</tt>.
1642 /// \relates EqualMap
1643 template<typename M1, typename M2>
1644 inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1645 return EqualMap<M1, M2>(m1,m2);
1649 /// Combination of two maps using the \c < operator
1651 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1652 /// the keys for which the corresponding value of the first map is
1653 /// less then the value of the second map.
1654 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1655 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1657 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1659 /// LessMap<M1,M2> lm(m1,m2);
1661 /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1663 /// The simplest way of using this map is through the lessMap()
1667 template<typename M1, typename M2>
1668 class LessMap : public MapBase<typename M1::Key, bool> {
1673 typedef typename M1::Key Key;
1678 LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1680 Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1683 /// Returns an \c LessMap class
1685 /// This function just returns an \c LessMap class.
1687 /// For example, if \c m1 and \c m2 are maps with keys and values of
1688 /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1689 /// <tt>m1[x]<m2[x]</tt>.
1691 /// \relates LessMap
1692 template<typename M1, typename M2>
1693 inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1694 return LessMap<M1, M2>(m1,m2);
1697 namespace _maps_bits {
1699 template <typename _Iterator, typename Enable = void>
1700 struct IteratorTraits {
1701 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1704 template <typename _Iterator>
1705 struct IteratorTraits<_Iterator,
1706 typename exists<typename _Iterator::container_type>::type>
1708 typedef typename _Iterator::container_type::value_type Value;
1715 /// \addtogroup maps
1718 /// \brief Writable bool map for logging each \c true assigned element
1720 /// A \ref concepts::WriteMap "writable" bool map for logging
1721 /// each \c true assigned element, i.e it copies subsequently each
1722 /// keys set to \c true to the given iterator.
1723 /// The most important usage of it is storing certain nodes or arcs
1724 /// that were marked \c true by an algorithm.
1726 /// There are several algorithms that provide solutions through bool
1727 /// maps and most of them assign \c true at most once for each key.
1728 /// In these cases it is a natural request to store each \c true
1729 /// assigned elements (in order of the assignment), which can be
1730 /// easily done with LoggerBoolMap.
1732 /// The simplest way of using this map is through the loggerBoolMap()
1735 /// \tparam IT The type of the iterator.
1736 /// \tparam KEY The key type of the map. The default value set
1737 /// according to the iterator type should work in most cases.
1739 /// \note The container of the iterator must contain enough space
1740 /// for the elements or the iterator should be an inserter iterator.
1742 template <typename IT, typename KEY>
1744 template <typename IT,
1745 typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1747 class LoggerBoolMap : public MapBase<KEY, bool> {
1755 typedef IT Iterator;
1758 LoggerBoolMap(Iterator it)
1759 : _begin(it), _end(it) {}
1761 /// Gives back the given iterator set for the first key
1762 Iterator begin() const {
1766 /// Gives back the the 'after the last' iterator
1767 Iterator end() const {
1771 /// The set function of the map
1772 void set(const Key& key, Value value) {
1783 /// Returns a \c LoggerBoolMap class
1785 /// This function just returns a \c LoggerBoolMap class.
1787 /// The most important usage of it is storing certain nodes or arcs
1788 /// that were marked \c true by an algorithm.
1789 /// For example it makes easier to store the nodes in the processing
1790 /// order of Dfs algorithm, as the following examples show.
1792 /// std::vector<Node> v;
1793 /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1796 /// std::vector<Node> v(countNodes(g));
1797 /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1800 /// \note The container of the iterator must contain enough space
1801 /// for the elements or the iterator should be an inserter iterator.
1803 /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1804 /// it cannot be used when a readable map is needed, for example as
1805 /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1807 /// \relates LoggerBoolMap
1808 template<typename Iterator>
1809 inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1810 return LoggerBoolMap<Iterator>(it);
1815 /// \addtogroup graph_maps
1818 /// \brief Provides an immutable and unique id for each item in a graph.
1820 /// IdMap provides a unique and immutable id for each item of the
1821 /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1822 /// - \b unique: different items get different ids,
1823 /// - \b immutable: the id of an item does not change (even if you
1824 /// delete other nodes).
1826 /// Using this map you get access (i.e. can read) the inner id values of
1827 /// the items stored in the graph, which is returned by the \c id()
1828 /// function of the graph. This map can be inverted with its member
1829 /// class \c InverseMap or with the \c operator() member.
1831 /// \tparam GR The graph type.
1832 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1836 template <typename GR, typename K>
1837 class IdMap : public MapBase<K, int> {
1839 /// The graph type of IdMap.
1842 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1844 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1846 /// The value type of IdMap.
1849 /// \brief Constructor.
1851 /// Constructor of the map.
1852 explicit IdMap(const Graph& graph) : _graph(&graph) {}
1854 /// \brief Gives back the \e id of the item.
1856 /// Gives back the immutable and unique \e id of the item.
1857 int operator[](const Item& item) const { return _graph->id(item);}
1859 /// \brief Gives back the \e item by its id.
1861 /// Gives back the \e item by its id.
1862 Item operator()(int id) { return _graph->fromId(id, Item()); }
1865 const Graph* _graph;
1869 /// \brief This class represents the inverse of its owner (IdMap).
1871 /// This class represents the inverse of its owner (IdMap).
1876 /// \brief Constructor.
1878 /// Constructor for creating an id-to-item map.
1879 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1881 /// \brief Constructor.
1883 /// Constructor for creating an id-to-item map.
1884 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1886 /// \brief Gives back the given item from its id.
1888 /// Gives back the given item from its id.
1889 Item operator[](int id) const { return _graph->fromId(id, Item());}
1892 const Graph* _graph;
1895 /// \brief Gives back the inverse of the map.
1897 /// Gives back the inverse of the IdMap.
1898 InverseMap inverse() const { return InverseMap(*_graph);}
1902 /// \brief General cross reference graph map type.
1904 /// This class provides simple invertable graph maps.
1905 /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1906 /// and if a key is set to a new value, then stores it in the inverse map.
1907 /// The values of the map can be accessed
1908 /// with stl compatible forward iterator.
1910 /// This type is not reference map, so it cannot be modified with
1911 /// the subscript operator.
1913 /// \tparam GR The graph type.
1914 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1916 /// \tparam V The value type of the map.
1918 /// \see IterableValueMap
1919 template <typename GR, typename K, typename V>
1921 : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1924 typedef typename ItemSetTraits<GR, K>::
1925 template Map<V>::Type Map;
1927 typedef std::multimap<V, K> Container;
1932 /// The graph type of CrossRefMap.
1935 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1937 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1939 /// The value type of CrossRefMap.
1942 /// \brief Constructor.
1944 /// Construct a new CrossRefMap for the given graph.
1945 explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1947 /// \brief Forward iterator for values.
1949 /// This iterator is an stl compatible forward
1950 /// iterator on the values of the map. The values can
1951 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1952 /// They are considered with multiplicity, so each value is
1953 /// traversed for each item it is assigned to.
1955 : public std::iterator<std::forward_iterator_tag, Value> {
1956 friend class CrossRefMap;
1958 ValueIterator(typename Container::const_iterator _it)
1964 ValueIterator& operator++() { ++it; return *this; }
1965 ValueIterator operator++(int) {
1966 ValueIterator tmp(*this);
1971 const Value& operator*() const { return it->first; }
1972 const Value* operator->() const { return &(it->first); }
1974 bool operator==(ValueIterator jt) const { return it == jt.it; }
1975 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1978 typename Container::const_iterator it;
1981 /// \brief Returns an iterator to the first value.
1983 /// Returns an stl compatible iterator to the
1984 /// first value of the map. The values of the
1985 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1987 ValueIterator beginValue() const {
1988 return ValueIterator(_inv_map.begin());
1991 /// \brief Returns an iterator after the last value.
1993 /// Returns an stl compatible iterator after the
1994 /// last value of the map. The values of the
1995 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1997 ValueIterator endValue() const {
1998 return ValueIterator(_inv_map.end());
2001 /// \brief Sets the value associated with the given key.
2003 /// Sets the value associated with the given key.
2004 void set(const Key& key, const Value& val) {
2005 Value oldval = Map::operator[](key);
2006 typename Container::iterator it;
2007 for (it = _inv_map.equal_range(oldval).first;
2008 it != _inv_map.equal_range(oldval).second; ++it) {
2009 if (it->second == key) {
2014 _inv_map.insert(std::make_pair(val, key));
2018 /// \brief Returns the value associated with the given key.
2020 /// Returns the value associated with the given key.
2021 typename MapTraits<Map>::ConstReturnValue
2022 operator[](const Key& key) const {
2023 return Map::operator[](key);
2026 /// \brief Gives back an item by its value.
2028 /// This function gives back an item that is assigned to
2029 /// the given value or \c INVALID if no such item exists.
2030 /// If there are more items with the same associated value,
2031 /// only one of them is returned.
2032 Key operator()(const Value& val) const {
2033 typename Container::const_iterator it = _inv_map.find(val);
2034 return it != _inv_map.end() ? it->second : INVALID;
2039 /// \brief Erase the key from the map and the inverse map.
2041 /// Erase the key from the map and the inverse map. It is called by the
2042 /// \c AlterationNotifier.
2043 virtual void erase(const Key& key) {
2044 Value val = Map::operator[](key);
2045 typename Container::iterator it;
2046 for (it = _inv_map.equal_range(val).first;
2047 it != _inv_map.equal_range(val).second; ++it) {
2048 if (it->second == key) {
2056 /// \brief Erase more keys from the map and the inverse map.
2058 /// Erase more keys from the map and the inverse map. It is called by the
2059 /// \c AlterationNotifier.
2060 virtual void erase(const std::vector<Key>& keys) {
2061 for (int i = 0; i < int(keys.size()); ++i) {
2062 Value val = Map::operator[](keys[i]);
2063 typename Container::iterator it;
2064 for (it = _inv_map.equal_range(val).first;
2065 it != _inv_map.equal_range(val).second; ++it) {
2066 if (it->second == keys[i]) {
2075 /// \brief Clear the keys from the map and the inverse map.
2077 /// Clear the keys from the map and the inverse map. It is called by the
2078 /// \c AlterationNotifier.
2079 virtual void clear() {
2086 /// \brief The inverse map type.
2088 /// The inverse of this map. The subscript operator of the map
2089 /// gives back the item that was last assigned to the value.
2092 /// \brief Constructor
2094 /// Constructor of the InverseMap.
2095 explicit InverseMap(const CrossRefMap& inverted)
2096 : _inverted(inverted) {}
2098 /// The value type of the InverseMap.
2099 typedef typename CrossRefMap::Key Value;
2100 /// The key type of the InverseMap.
2101 typedef typename CrossRefMap::Value Key;
2103 /// \brief Subscript operator.
2105 /// Subscript operator. It gives back an item
2106 /// that is assigned to the given value or \c INVALID
2107 /// if no such item exists.
2108 Value operator[](const Key& key) const {
2109 return _inverted(key);
2113 const CrossRefMap& _inverted;
2116 /// \brief It gives back the read-only inverse map.
2118 /// It gives back the read-only inverse map.
2119 InverseMap inverse() const {
2120 return InverseMap(*this);
2125 /// \brief Provides continuous and unique ID for the
2126 /// items of a graph.
2128 /// RangeIdMap provides a unique and continuous
2129 /// ID for each item of a given type (\c Node, \c Arc or
2130 /// \c Edge) in a graph. This id is
2131 /// - \b unique: different items get different ids,
2132 /// - \b continuous: the range of the ids is the set of integers
2133 /// between 0 and \c n-1, where \c n is the number of the items of
2134 /// this type (\c Node, \c Arc or \c Edge).
2135 /// - So, the ids can change when deleting an item of the same type.
2137 /// Thus this id is not (necessarily) the same as what can get using
2138 /// the \c id() function of the graph or \ref IdMap.
2139 /// This map can be inverted with its member class \c InverseMap,
2140 /// or with the \c operator() member.
2142 /// \tparam GR The graph type.
2143 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2147 template <typename GR, typename K>
2149 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2151 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2154 /// The graph type of RangeIdMap.
2157 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2159 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2161 /// The value type of RangeIdMap.
2164 /// \brief Constructor.
2167 explicit RangeIdMap(const Graph& gr) : Map(gr) {
2169 const typename Map::Notifier* nf = Map::notifier();
2170 for (nf->first(it); it != INVALID; nf->next(it)) {
2171 Map::set(it, _inv_map.size());
2172 _inv_map.push_back(it);
2178 /// \brief Adds a new key to the map.
2180 /// Add a new key to the map. It is called by the
2181 /// \c AlterationNotifier.
2182 virtual void add(const Item& item) {
2184 Map::set(item, _inv_map.size());
2185 _inv_map.push_back(item);
2188 /// \brief Add more new keys to the map.
2190 /// Add more new keys to the map. It is called by the
2191 /// \c AlterationNotifier.
2192 virtual void add(const std::vector<Item>& items) {
2194 for (int i = 0; i < int(items.size()); ++i) {
2195 Map::set(items[i], _inv_map.size());
2196 _inv_map.push_back(items[i]);
2200 /// \brief Erase the key from the map.
2202 /// Erase the key from the map. It is called by the
2203 /// \c AlterationNotifier.
2204 virtual void erase(const Item& item) {
2205 Map::set(_inv_map.back(), Map::operator[](item));
2206 _inv_map[Map::operator[](item)] = _inv_map.back();
2207 _inv_map.pop_back();
2211 /// \brief Erase more keys from the map.
2213 /// Erase more keys from the map. It is called by the
2214 /// \c AlterationNotifier.
2215 virtual void erase(const std::vector<Item>& items) {
2216 for (int i = 0; i < int(items.size()); ++i) {
2217 Map::set(_inv_map.back(), Map::operator[](items[i]));
2218 _inv_map[Map::operator[](items[i])] = _inv_map.back();
2219 _inv_map.pop_back();
2224 /// \brief Build the unique map.
2226 /// Build the unique map. It is called by the
2227 /// \c AlterationNotifier.
2228 virtual void build() {
2231 const typename Map::Notifier* nf = Map::notifier();
2232 for (nf->first(it); it != INVALID; nf->next(it)) {
2233 Map::set(it, _inv_map.size());
2234 _inv_map.push_back(it);
2238 /// \brief Clear the keys from the map.
2240 /// Clear the keys from the map. It is called by the
2241 /// \c AlterationNotifier.
2242 virtual void clear() {
2249 /// \brief Returns the maximal value plus one.
2251 /// Returns the maximal value plus one in the map.
2252 unsigned int size() const {
2253 return _inv_map.size();
2256 /// \brief Swaps the position of the two items in the map.
2258 /// Swaps the position of the two items in the map.
2259 void swap(const Item& p, const Item& q) {
2260 int pi = Map::operator[](p);
2261 int qi = Map::operator[](q);
2268 /// \brief Gives back the \e RangeId of the item
2270 /// Gives back the \e RangeId of the item.
2271 int operator[](const Item& item) const {
2272 return Map::operator[](item);
2275 /// \brief Gives back the item belonging to a \e RangeId
2277 /// Gives back the item belonging to a \e RangeId.
2278 Item operator()(int id) const {
2279 return _inv_map[id];
2284 typedef std::vector<Item> Container;
2289 /// \brief The inverse map type of RangeIdMap.
2291 /// The inverse map type of RangeIdMap.
2294 /// \brief Constructor
2296 /// Constructor of the InverseMap.
2297 explicit InverseMap(const RangeIdMap& inverted)
2298 : _inverted(inverted) {}
2301 /// The value type of the InverseMap.
2302 typedef typename RangeIdMap::Key Value;
2303 /// The key type of the InverseMap.
2304 typedef typename RangeIdMap::Value Key;
2306 /// \brief Subscript operator.
2308 /// Subscript operator. It gives back the item
2309 /// that the descriptor currently belongs to.
2310 Value operator[](const Key& key) const {
2311 return _inverted(key);
2314 /// \brief Size of the map.
2316 /// Returns the size of the map.
2317 unsigned int size() const {
2318 return _inverted.size();
2322 const RangeIdMap& _inverted;
2325 /// \brief Gives back the inverse of the map.
2327 /// Gives back the inverse of the map.
2328 const InverseMap inverse() const {
2329 return InverseMap(*this);
2333 /// \brief Map of the source nodes of arcs in a digraph.
2335 /// SourceMap provides access for the source node of each arc in a digraph,
2336 /// which is returned by the \c source() function of the digraph.
2337 /// \tparam GR The digraph type.
2339 template <typename GR>
2344 typedef typename GR::Arc Key;
2346 typedef typename GR::Node Value;
2348 /// \brief Constructor
2351 /// \param digraph The digraph that the map belongs to.
2352 explicit SourceMap(const GR& digraph) : _graph(digraph) {}
2354 /// \brief Returns the source node of the given arc.
2356 /// Returns the source node of the given arc.
2357 Value operator[](const Key& arc) const {
2358 return _graph.source(arc);
2365 /// \brief Returns a \c SourceMap class.
2367 /// This function just returns an \c SourceMap class.
2368 /// \relates SourceMap
2369 template <typename GR>
2370 inline SourceMap<GR> sourceMap(const GR& graph) {
2371 return SourceMap<GR>(graph);
2374 /// \brief Map of the target nodes of arcs in a digraph.
2376 /// TargetMap provides access for the target node of each arc in a digraph,
2377 /// which is returned by the \c target() function of the digraph.
2378 /// \tparam GR The digraph type.
2380 template <typename GR>
2385 typedef typename GR::Arc Key;
2387 typedef typename GR::Node Value;
2389 /// \brief Constructor
2392 /// \param digraph The digraph that the map belongs to.
2393 explicit TargetMap(const GR& digraph) : _graph(digraph) {}
2395 /// \brief Returns the target node of the given arc.
2397 /// Returns the target node of the given arc.
2398 Value operator[](const Key& e) const {
2399 return _graph.target(e);
2406 /// \brief Returns a \c TargetMap class.
2408 /// This function just returns a \c TargetMap class.
2409 /// \relates TargetMap
2410 template <typename GR>
2411 inline TargetMap<GR> targetMap(const GR& graph) {
2412 return TargetMap<GR>(graph);
2415 /// \brief Map of the "forward" directed arc view of edges in a graph.
2417 /// ForwardMap provides access for the "forward" directed arc view of
2418 /// each edge in a graph, which is returned by the \c direct() function
2419 /// of the graph with \c true parameter.
2420 /// \tparam GR The graph type.
2421 /// \see BackwardMap
2422 template <typename GR>
2426 typedef typename GR::Arc Value;
2427 typedef typename GR::Edge Key;
2429 /// \brief Constructor
2432 /// \param graph The graph that the map belongs to.
2433 explicit ForwardMap(const GR& graph) : _graph(graph) {}
2435 /// \brief Returns the "forward" directed arc view of the given edge.
2437 /// Returns the "forward" directed arc view of the given edge.
2438 Value operator[](const Key& key) const {
2439 return _graph.direct(key, true);
2446 /// \brief Returns a \c ForwardMap class.
2448 /// This function just returns an \c ForwardMap class.
2449 /// \relates ForwardMap
2450 template <typename GR>
2451 inline ForwardMap<GR> forwardMap(const GR& graph) {
2452 return ForwardMap<GR>(graph);
2455 /// \brief Map of the "backward" directed arc view of edges in a graph.
2457 /// BackwardMap provides access for the "backward" directed arc view of
2458 /// each edge in a graph, which is returned by the \c direct() function
2459 /// of the graph with \c false parameter.
2460 /// \tparam GR The graph type.
2462 template <typename GR>
2466 typedef typename GR::Arc Value;
2467 typedef typename GR::Edge Key;
2469 /// \brief Constructor
2472 /// \param graph The graph that the map belongs to.
2473 explicit BackwardMap(const GR& graph) : _graph(graph) {}
2475 /// \brief Returns the "backward" directed arc view of the given edge.
2477 /// Returns the "backward" directed arc view of the given edge.
2478 Value operator[](const Key& key) const {
2479 return _graph.direct(key, false);
2486 /// \brief Returns a \c BackwardMap class
2488 /// This function just returns a \c BackwardMap class.
2489 /// \relates BackwardMap
2490 template <typename GR>
2491 inline BackwardMap<GR> backwardMap(const GR& graph) {
2492 return BackwardMap<GR>(graph);
2495 /// \brief Map of the in-degrees of nodes in a digraph.
2497 /// This map returns the in-degree of a node. Once it is constructed,
2498 /// the degrees are stored in a standard \c NodeMap, so each query is done
2499 /// in constant time. On the other hand, the values are updated automatically
2500 /// whenever the digraph changes.
2502 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
2503 /// may provide alternative ways to modify the digraph.
2504 /// The correct behavior of InDegMap is not guarantied if these additional
2505 /// features are used. For example the functions
2506 /// \ref ListDigraph::changeSource() "changeSource()",
2507 /// \ref ListDigraph::changeTarget() "changeTarget()" and
2508 /// \ref ListDigraph::reverseArc() "reverseArc()"
2509 /// of \ref ListDigraph will \e not update the degree values correctly.
2512 template <typename GR>
2514 : protected ItemSetTraits<GR, typename GR::Arc>
2515 ::ItemNotifier::ObserverBase {
2519 /// The graph type of InDegMap
2523 typedef typename Digraph::Node Key;
2527 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2528 ::ItemNotifier::ObserverBase Parent;
2533 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2536 typedef typename ItemSetTraits<Digraph, Key>::
2537 template Map<int>::Type Parent;
2539 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2541 virtual void add(const Key& key) {
2543 Parent::set(key, 0);
2546 virtual void add(const std::vector<Key>& keys) {
2548 for (int i = 0; i < int(keys.size()); ++i) {
2549 Parent::set(keys[i], 0);
2553 virtual void build() {
2556 typename Parent::Notifier* nf = Parent::notifier();
2557 for (nf->first(it); it != INVALID; nf->next(it)) {
2565 /// \brief Constructor.
2567 /// Constructor for creating an in-degree map.
2568 explicit InDegMap(const Digraph& graph)
2569 : _digraph(graph), _deg(graph) {
2570 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2572 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2573 _deg[it] = countInArcs(_digraph, it);
2577 /// \brief Gives back the in-degree of a Node.
2579 /// Gives back the in-degree of a Node.
2580 int operator[](const Key& key) const {
2586 typedef typename Digraph::Arc Arc;
2588 virtual void add(const Arc& arc) {
2589 ++_deg[_digraph.target(arc)];
2592 virtual void add(const std::vector<Arc>& arcs) {
2593 for (int i = 0; i < int(arcs.size()); ++i) {
2594 ++_deg[_digraph.target(arcs[i])];
2598 virtual void erase(const Arc& arc) {
2599 --_deg[_digraph.target(arc)];
2602 virtual void erase(const std::vector<Arc>& arcs) {
2603 for (int i = 0; i < int(arcs.size()); ++i) {
2604 --_deg[_digraph.target(arcs[i])];
2608 virtual void build() {
2609 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2610 _deg[it] = countInArcs(_digraph, it);
2614 virtual void clear() {
2615 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2621 const Digraph& _digraph;
2625 /// \brief Map of the out-degrees of nodes in a digraph.
2627 /// This map returns the out-degree of a node. Once it is constructed,
2628 /// the degrees are stored in a standard \c NodeMap, so each query is done
2629 /// in constant time. On the other hand, the values are updated automatically
2630 /// whenever the digraph changes.
2632 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
2633 /// may provide alternative ways to modify the digraph.
2634 /// The correct behavior of OutDegMap is not guarantied if these additional
2635 /// features are used. For example the functions
2636 /// \ref ListDigraph::changeSource() "changeSource()",
2637 /// \ref ListDigraph::changeTarget() "changeTarget()" and
2638 /// \ref ListDigraph::reverseArc() "reverseArc()"
2639 /// of \ref ListDigraph will \e not update the degree values correctly.
2642 template <typename GR>
2644 : protected ItemSetTraits<GR, typename GR::Arc>
2645 ::ItemNotifier::ObserverBase {
2649 /// The graph type of OutDegMap
2653 typedef typename Digraph::Node Key;
2657 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2658 ::ItemNotifier::ObserverBase Parent;
2663 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2666 typedef typename ItemSetTraits<Digraph, Key>::
2667 template Map<int>::Type Parent;
2669 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2671 virtual void add(const Key& key) {
2673 Parent::set(key, 0);
2675 virtual void add(const std::vector<Key>& keys) {
2677 for (int i = 0; i < int(keys.size()); ++i) {
2678 Parent::set(keys[i], 0);
2681 virtual void build() {
2684 typename Parent::Notifier* nf = Parent::notifier();
2685 for (nf->first(it); it != INVALID; nf->next(it)) {
2693 /// \brief Constructor.
2695 /// Constructor for creating an out-degree map.
2696 explicit OutDegMap(const Digraph& graph)
2697 : _digraph(graph), _deg(graph) {
2698 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2700 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2701 _deg[it] = countOutArcs(_digraph, it);
2705 /// \brief Gives back the out-degree of a Node.
2707 /// Gives back the out-degree of a Node.
2708 int operator[](const Key& key) const {
2714 typedef typename Digraph::Arc Arc;
2716 virtual void add(const Arc& arc) {
2717 ++_deg[_digraph.source(arc)];
2720 virtual void add(const std::vector<Arc>& arcs) {
2721 for (int i = 0; i < int(arcs.size()); ++i) {
2722 ++_deg[_digraph.source(arcs[i])];
2726 virtual void erase(const Arc& arc) {
2727 --_deg[_digraph.source(arc)];
2730 virtual void erase(const std::vector<Arc>& arcs) {
2731 for (int i = 0; i < int(arcs.size()); ++i) {
2732 --_deg[_digraph.source(arcs[i])];
2736 virtual void build() {
2737 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2738 _deg[it] = countOutArcs(_digraph, it);
2742 virtual void clear() {
2743 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2749 const Digraph& _digraph;
2753 /// \brief Potential difference map
2755 /// PotentialDifferenceMap returns the difference between the potentials of
2756 /// the source and target nodes of each arc in a digraph, i.e. it returns
2758 /// potential[gr.target(arc)] - potential[gr.source(arc)].
2760 /// \tparam GR The digraph type.
2761 /// \tparam POT A node map storing the potentials.
2762 template <typename GR, typename POT>
2763 class PotentialDifferenceMap {
2766 typedef typename GR::Arc Key;
2768 typedef typename POT::Value Value;
2770 /// \brief Constructor
2772 /// Contructor of the map.
2773 explicit PotentialDifferenceMap(const GR& gr,
2774 const POT& potential)
2775 : _digraph(gr), _potential(potential) {}
2777 /// \brief Returns the potential difference for the given arc.
2779 /// Returns the potential difference for the given arc, i.e.
2781 /// potential[gr.target(arc)] - potential[gr.source(arc)].
2783 Value operator[](const Key& arc) const {
2784 return _potential[_digraph.target(arc)] -
2785 _potential[_digraph.source(arc)];
2790 const POT& _potential;
2793 /// \brief Returns a PotentialDifferenceMap.
2795 /// This function just returns a PotentialDifferenceMap.
2796 /// \relates PotentialDifferenceMap
2797 template <typename GR, typename POT>
2798 PotentialDifferenceMap<GR, POT>
2799 potentialDifferenceMap(const GR& gr, const POT& potential) {
2800 return PotentialDifferenceMap<GR, POT>(gr, potential);
2806 #endif // LEMON_MAPS_H