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
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
1835 /// \see DescriptorMap
1836 template <typename GR, typename K>
1837 class IdMap : public MapBase<K, int> {
1839 /// 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 This class represents the inverse of its owner (IdMap).
1870 /// This class represents the inverse of its owner (IdMap).
1875 /// \brief Constructor.
1877 /// Constructor for creating an id-to-item map.
1878 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1880 /// \brief Constructor.
1882 /// Constructor for creating an id-to-item map.
1883 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1885 /// \brief Gives back the given item from its id.
1887 /// Gives back the given item from its id.
1888 Item operator[](int id) const { return _graph->fromId(id, Item());}
1891 const Graph* _graph;
1894 /// \brief Gives back the inverse of the map.
1896 /// Gives back the inverse of the IdMap.
1897 InverseMap inverse() const { return InverseMap(*_graph);}
1901 /// \brief General invertable graph map type.
1903 /// This class provides simple invertable graph maps.
1904 /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
1905 /// and if a key is set to a new value then store it
1906 /// in the inverse map.
1908 /// The values of the map can be accessed
1909 /// with stl compatible forward iterator.
1911 /// \tparam GR The graph type.
1912 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1914 /// \tparam V The value type of the map.
1916 /// \see IterableValueMap
1917 template <typename GR, typename K, typename V>
1919 : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1922 typedef typename ItemSetTraits<GR, K>::
1923 template Map<V>::Type Map;
1925 typedef std::map<V, K> Container;
1930 /// The graph type of InvertableMap.
1932 /// The key type of InvertableMap (\c Node, \c Arc or \c Edge).
1934 /// The key type of InvertableMap (\c Node, \c Arc or \c Edge).
1936 /// The value type of InvertableMap.
1939 /// \brief Constructor.
1941 /// Construct a new InvertableMap for the given graph.
1942 explicit InvertableMap(const Graph& graph) : Map(graph) {}
1944 /// \brief Forward iterator for values.
1946 /// This iterator is an stl compatible forward
1947 /// iterator on the values of the map. The values can
1948 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1950 : public std::iterator<std::forward_iterator_tag, Value> {
1951 friend class InvertableMap;
1953 ValueIterator(typename Container::const_iterator _it)
1959 ValueIterator& operator++() { ++it; return *this; }
1960 ValueIterator operator++(int) {
1961 ValueIterator tmp(*this);
1966 const Value& operator*() const { return it->first; }
1967 const Value* operator->() const { return &(it->first); }
1969 bool operator==(ValueIterator jt) const { return it == jt.it; }
1970 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1973 typename Container::const_iterator it;
1976 /// \brief Returns an iterator to the first value.
1978 /// Returns an stl compatible iterator to the
1979 /// first value of the map. The values of the
1980 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1982 ValueIterator beginValue() const {
1983 return ValueIterator(_inv_map.begin());
1986 /// \brief Returns an iterator after the last value.
1988 /// Returns an stl compatible iterator after the
1989 /// last value of the map. The values of the
1990 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1992 ValueIterator endValue() const {
1993 return ValueIterator(_inv_map.end());
1996 /// \brief Sets the value associated with the given key.
1998 /// Sets the value associated with the given key.
1999 void set(const Key& key, const Value& val) {
2000 Value oldval = Map::operator[](key);
2001 typename Container::iterator it = _inv_map.find(oldval);
2002 if (it != _inv_map.end() && it->second == key) {
2005 _inv_map.insert(make_pair(val, key));
2009 /// \brief Returns the value associated with the given key.
2011 /// Returns the value associated with the given key.
2012 typename MapTraits<Map>::ConstReturnValue
2013 operator[](const Key& key) const {
2014 return Map::operator[](key);
2017 /// \brief Gives back the item by its value.
2019 /// Gives back the item by its value.
2020 Key operator()(const Value& key) const {
2021 typename Container::const_iterator it = _inv_map.find(key);
2022 return it != _inv_map.end() ? it->second : INVALID;
2027 /// \brief Erase the key from the map and the inverse map.
2029 /// Erase the key from the map and the inverse map. It is called by the
2030 /// \c AlterationNotifier.
2031 virtual void erase(const Key& key) {
2032 Value val = Map::operator[](key);
2033 typename Container::iterator it = _inv_map.find(val);
2034 if (it != _inv_map.end() && it->second == key) {
2040 /// \brief Erase more keys from the map and the inverse map.
2042 /// Erase more keys from the map and the inverse map. It is called by the
2043 /// \c AlterationNotifier.
2044 virtual void erase(const std::vector<Key>& keys) {
2045 for (int i = 0; i < int(keys.size()); ++i) {
2046 Value val = Map::operator[](keys[i]);
2047 typename Container::iterator it = _inv_map.find(val);
2048 if (it != _inv_map.end() && it->second == keys[i]) {
2055 /// \brief Clear the keys from the map and the inverse map.
2057 /// Clear the keys from the map and the inverse map. It is called by the
2058 /// \c AlterationNotifier.
2059 virtual void clear() {
2066 /// \brief The inverse map type.
2068 /// The inverse of this map. The subscript operator of the map
2069 /// gives back the item that was last assigned to the value.
2072 /// \brief Constructor
2074 /// Constructor of the InverseMap.
2075 explicit InverseMap(const InvertableMap& inverted)
2076 : _inverted(inverted) {}
2078 /// The value type of the InverseMap.
2079 typedef typename InvertableMap::Key Value;
2080 /// The key type of the InverseMap.
2081 typedef typename InvertableMap::Value Key;
2083 /// \brief Subscript operator.
2085 /// Subscript operator. It gives back the item
2086 /// that was last assigned to the given value.
2087 Value operator[](const Key& key) const {
2088 return _inverted(key);
2092 const InvertableMap& _inverted;
2095 /// \brief It gives back the read-only inverse map.
2097 /// It gives back the read-only inverse map.
2098 InverseMap inverse() const {
2099 return InverseMap(*this);
2104 /// \brief Provides a mutable, continuous and unique descriptor for each
2105 /// item in a graph.
2107 /// DescriptorMap provides a unique and continuous (but mutable)
2108 /// descriptor (id) for each item of the same type (\c Node, \c Arc or
2109 /// \c Edge) in a graph. This id is
2110 /// - \b unique: different items get different ids,
2111 /// - \b continuous: the range of the ids is the set of integers
2112 /// between 0 and \c n-1, where \c n is the number of the items of
2113 /// this type (\c Node, \c Arc or \c Edge). So the id of an item can
2114 /// change if you delete an other item of the same type, i.e. this
2117 /// Thus this id is not (necessarily) the same as what can get using
2118 /// the \c id() function of the graph or \ref IdMap.
2119 /// This map can be inverted with its member class \c InverseMap,
2120 /// or with the \c operator() member.
2122 /// \tparam GR The graph type.
2123 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2127 template <typename GR, typename K>
2129 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2131 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2134 /// The graph type of DescriptorMap.
2136 /// The key type of DescriptorMap (\c Node, \c Arc or \c Edge).
2138 /// The key type of DescriptorMap (\c Node, \c Arc or \c Edge).
2140 /// The value type of DescriptorMap.
2143 /// \brief Constructor.
2145 /// Constructor for descriptor map.
2146 explicit DescriptorMap(const Graph& gr) : Map(gr) {
2148 const typename Map::Notifier* nf = Map::notifier();
2149 for (nf->first(it); it != INVALID; nf->next(it)) {
2150 Map::set(it, _inv_map.size());
2151 _inv_map.push_back(it);
2157 /// \brief Adds a new key to the map.
2159 /// Add a new key to the map. It is called by the
2160 /// \c AlterationNotifier.
2161 virtual void add(const Item& item) {
2163 Map::set(item, _inv_map.size());
2164 _inv_map.push_back(item);
2167 /// \brief Add more new keys to the map.
2169 /// Add more new keys to the map. It is called by the
2170 /// \c AlterationNotifier.
2171 virtual void add(const std::vector<Item>& items) {
2173 for (int i = 0; i < int(items.size()); ++i) {
2174 Map::set(items[i], _inv_map.size());
2175 _inv_map.push_back(items[i]);
2179 /// \brief Erase the key from the map.
2181 /// Erase the key from the map. It is called by the
2182 /// \c AlterationNotifier.
2183 virtual void erase(const Item& item) {
2184 Map::set(_inv_map.back(), Map::operator[](item));
2185 _inv_map[Map::operator[](item)] = _inv_map.back();
2186 _inv_map.pop_back();
2190 /// \brief Erase more keys from the map.
2192 /// Erase more keys from the map. It is called by the
2193 /// \c AlterationNotifier.
2194 virtual void erase(const std::vector<Item>& items) {
2195 for (int i = 0; i < int(items.size()); ++i) {
2196 Map::set(_inv_map.back(), Map::operator[](items[i]));
2197 _inv_map[Map::operator[](items[i])] = _inv_map.back();
2198 _inv_map.pop_back();
2203 /// \brief Build the unique map.
2205 /// Build the unique map. It is called by the
2206 /// \c AlterationNotifier.
2207 virtual void build() {
2210 const typename Map::Notifier* nf = Map::notifier();
2211 for (nf->first(it); it != INVALID; nf->next(it)) {
2212 Map::set(it, _inv_map.size());
2213 _inv_map.push_back(it);
2217 /// \brief Clear the keys from the map.
2219 /// Clear the keys from the map. It is called by the
2220 /// \c AlterationNotifier.
2221 virtual void clear() {
2228 /// \brief Returns the maximal value plus one.
2230 /// Returns the maximal value plus one in the map.
2231 unsigned int size() const {
2232 return _inv_map.size();
2235 /// \brief Swaps the position of the two items in the map.
2237 /// Swaps the position of the two items in the map.
2238 void swap(const Item& p, const Item& q) {
2239 int pi = Map::operator[](p);
2240 int qi = Map::operator[](q);
2247 /// \brief Gives back the \e descriptor of the item.
2249 /// Gives back the mutable and unique \e descriptor of the map.
2250 int operator[](const Item& item) const {
2251 return Map::operator[](item);
2254 /// \brief Gives back the item by its descriptor.
2256 /// Gives back th item by its descriptor.
2257 Item operator()(int id) const {
2258 return _inv_map[id];
2263 typedef std::vector<Item> Container;
2268 /// \brief The inverse map type of DescriptorMap.
2270 /// The inverse map type of DescriptorMap.
2273 /// \brief Constructor
2275 /// Constructor of the InverseMap.
2276 explicit InverseMap(const DescriptorMap& inverted)
2277 : _inverted(inverted) {}
2280 /// The value type of the InverseMap.
2281 typedef typename DescriptorMap::Key Value;
2282 /// The key type of the InverseMap.
2283 typedef typename DescriptorMap::Value Key;
2285 /// \brief Subscript operator.
2287 /// Subscript operator. It gives back the item
2288 /// that the descriptor currently belongs to.
2289 Value operator[](const Key& key) const {
2290 return _inverted(key);
2293 /// \brief Size of the map.
2295 /// Returns the size of the map.
2296 unsigned int size() const {
2297 return _inverted.size();
2301 const DescriptorMap& _inverted;
2304 /// \brief Gives back the inverse of the map.
2306 /// Gives back the inverse of the map.
2307 const InverseMap inverse() const {
2308 return InverseMap(*this);
2312 /// \brief Map of the source nodes of arcs in a digraph.
2314 /// SourceMap provides access for the source node of each arc in a digraph,
2315 /// which is returned by the \c source() function of the digraph.
2316 /// \tparam GR The digraph type.
2318 template <typename GR>
2323 typedef typename GR::Arc Key;
2325 typedef typename GR::Node Value;
2327 /// \brief Constructor
2330 /// \param digraph The digraph that the map belongs to.
2331 explicit SourceMap(const GR& digraph) : _graph(digraph) {}
2333 /// \brief Returns the source node of the given arc.
2335 /// Returns the source node of the given arc.
2336 Value operator[](const Key& arc) const {
2337 return _graph.source(arc);
2344 /// \brief Returns a \c SourceMap class.
2346 /// This function just returns an \c SourceMap class.
2347 /// \relates SourceMap
2348 template <typename GR>
2349 inline SourceMap<GR> sourceMap(const GR& graph) {
2350 return SourceMap<GR>(graph);
2353 /// \brief Map of the target nodes of arcs in a digraph.
2355 /// TargetMap provides access for the target node of each arc in a digraph,
2356 /// which is returned by the \c target() function of the digraph.
2357 /// \tparam GR The digraph type.
2359 template <typename GR>
2364 typedef typename GR::Arc Key;
2366 typedef typename GR::Node Value;
2368 /// \brief Constructor
2371 /// \param digraph The digraph that the map belongs to.
2372 explicit TargetMap(const GR& digraph) : _graph(digraph) {}
2374 /// \brief Returns the target node of the given arc.
2376 /// Returns the target node of the given arc.
2377 Value operator[](const Key& e) const {
2378 return _graph.target(e);
2385 /// \brief Returns a \c TargetMap class.
2387 /// This function just returns a \c TargetMap class.
2388 /// \relates TargetMap
2389 template <typename GR>
2390 inline TargetMap<GR> targetMap(const GR& graph) {
2391 return TargetMap<GR>(graph);
2394 /// \brief Map of the "forward" directed arc view of edges in a graph.
2396 /// ForwardMap provides access for the "forward" directed arc view of
2397 /// each edge in a graph, which is returned by the \c direct() function
2398 /// of the graph with \c true parameter.
2399 /// \tparam GR The graph type.
2400 /// \see BackwardMap
2401 template <typename GR>
2405 typedef typename GR::Arc Value;
2406 typedef typename GR::Edge Key;
2408 /// \brief Constructor
2411 /// \param graph The graph that the map belongs to.
2412 explicit ForwardMap(const GR& graph) : _graph(graph) {}
2414 /// \brief Returns the "forward" directed arc view of the given edge.
2416 /// Returns the "forward" directed arc view of the given edge.
2417 Value operator[](const Key& key) const {
2418 return _graph.direct(key, true);
2425 /// \brief Returns a \c ForwardMap class.
2427 /// This function just returns an \c ForwardMap class.
2428 /// \relates ForwardMap
2429 template <typename GR>
2430 inline ForwardMap<GR> forwardMap(const GR& graph) {
2431 return ForwardMap<GR>(graph);
2434 /// \brief Map of the "backward" directed arc view of edges in a graph.
2436 /// BackwardMap provides access for the "backward" directed arc view of
2437 /// each edge in a graph, which is returned by the \c direct() function
2438 /// of the graph with \c false parameter.
2439 /// \tparam GR The graph type.
2441 template <typename GR>
2445 typedef typename GR::Arc Value;
2446 typedef typename GR::Edge Key;
2448 /// \brief Constructor
2451 /// \param graph The graph that the map belongs to.
2452 explicit BackwardMap(const GR& graph) : _graph(graph) {}
2454 /// \brief Returns the "backward" directed arc view of the given edge.
2456 /// Returns the "backward" directed arc view of the given edge.
2457 Value operator[](const Key& key) const {
2458 return _graph.direct(key, false);
2465 /// \brief Returns a \c BackwardMap class
2467 /// This function just returns a \c BackwardMap class.
2468 /// \relates BackwardMap
2469 template <typename GR>
2470 inline BackwardMap<GR> backwardMap(const GR& graph) {
2471 return BackwardMap<GR>(graph);
2474 /// \brief Map of the in-degrees of nodes in a digraph.
2476 /// This map returns the in-degree of a node. Once it is constructed,
2477 /// the degrees are stored in a standard \c NodeMap, so each query is done
2478 /// in constant time. On the other hand, the values are updated automatically
2479 /// whenever the digraph changes.
2481 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
2482 /// may provide alternative ways to modify the digraph.
2483 /// The correct behavior of InDegMap is not guarantied if these additional
2484 /// features are used. For example the functions
2485 /// \ref ListDigraph::changeSource() "changeSource()",
2486 /// \ref ListDigraph::changeTarget() "changeTarget()" and
2487 /// \ref ListDigraph::reverseArc() "reverseArc()"
2488 /// of \ref ListDigraph will \e not update the degree values correctly.
2491 template <typename GR>
2493 : protected ItemSetTraits<GR, typename GR::Arc>
2494 ::ItemNotifier::ObserverBase {
2498 /// The digraph type
2501 typedef typename Digraph::Node Key;
2505 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2506 ::ItemNotifier::ObserverBase Parent;
2511 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2514 typedef typename ItemSetTraits<Digraph, Key>::
2515 template Map<int>::Type Parent;
2517 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2519 virtual void add(const Key& key) {
2521 Parent::set(key, 0);
2524 virtual void add(const std::vector<Key>& keys) {
2526 for (int i = 0; i < int(keys.size()); ++i) {
2527 Parent::set(keys[i], 0);
2531 virtual void build() {
2534 typename Parent::Notifier* nf = Parent::notifier();
2535 for (nf->first(it); it != INVALID; nf->next(it)) {
2543 /// \brief Constructor.
2545 /// Constructor for creating an in-degree map.
2546 explicit InDegMap(const Digraph& graph)
2547 : _digraph(graph), _deg(graph) {
2548 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2550 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2551 _deg[it] = countInArcs(_digraph, it);
2555 /// \brief Gives back the in-degree of a Node.
2557 /// Gives back the in-degree of a Node.
2558 int operator[](const Key& key) const {
2564 typedef typename Digraph::Arc Arc;
2566 virtual void add(const Arc& arc) {
2567 ++_deg[_digraph.target(arc)];
2570 virtual void add(const std::vector<Arc>& arcs) {
2571 for (int i = 0; i < int(arcs.size()); ++i) {
2572 ++_deg[_digraph.target(arcs[i])];
2576 virtual void erase(const Arc& arc) {
2577 --_deg[_digraph.target(arc)];
2580 virtual void erase(const std::vector<Arc>& arcs) {
2581 for (int i = 0; i < int(arcs.size()); ++i) {
2582 --_deg[_digraph.target(arcs[i])];
2586 virtual void build() {
2587 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2588 _deg[it] = countInArcs(_digraph, it);
2592 virtual void clear() {
2593 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2599 const Digraph& _digraph;
2603 /// \brief Map of the out-degrees of nodes in a digraph.
2605 /// This map returns the out-degree of a node. Once it is constructed,
2606 /// the degrees are stored in a standard \c NodeMap, so each query is done
2607 /// in constant time. On the other hand, the values are updated automatically
2608 /// whenever the digraph changes.
2610 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
2611 /// may provide alternative ways to modify the digraph.
2612 /// The correct behavior of OutDegMap is not guarantied if these additional
2613 /// features are used. For example the functions
2614 /// \ref ListDigraph::changeSource() "changeSource()",
2615 /// \ref ListDigraph::changeTarget() "changeTarget()" and
2616 /// \ref ListDigraph::reverseArc() "reverseArc()"
2617 /// of \ref ListDigraph will \e not update the degree values correctly.
2620 template <typename GR>
2622 : protected ItemSetTraits<GR, typename GR::Arc>
2623 ::ItemNotifier::ObserverBase {
2627 /// The digraph type
2630 typedef typename Digraph::Node Key;
2634 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2635 ::ItemNotifier::ObserverBase Parent;
2640 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2643 typedef typename ItemSetTraits<Digraph, Key>::
2644 template Map<int>::Type Parent;
2646 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2648 virtual void add(const Key& key) {
2650 Parent::set(key, 0);
2652 virtual void add(const std::vector<Key>& keys) {
2654 for (int i = 0; i < int(keys.size()); ++i) {
2655 Parent::set(keys[i], 0);
2658 virtual void build() {
2661 typename Parent::Notifier* nf = Parent::notifier();
2662 for (nf->first(it); it != INVALID; nf->next(it)) {
2670 /// \brief Constructor.
2672 /// Constructor for creating an out-degree map.
2673 explicit OutDegMap(const Digraph& graph)
2674 : _digraph(graph), _deg(graph) {
2675 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2677 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2678 _deg[it] = countOutArcs(_digraph, it);
2682 /// \brief Gives back the out-degree of a Node.
2684 /// Gives back the out-degree of a Node.
2685 int operator[](const Key& key) const {
2691 typedef typename Digraph::Arc Arc;
2693 virtual void add(const Arc& arc) {
2694 ++_deg[_digraph.source(arc)];
2697 virtual void add(const std::vector<Arc>& arcs) {
2698 for (int i = 0; i < int(arcs.size()); ++i) {
2699 ++_deg[_digraph.source(arcs[i])];
2703 virtual void erase(const Arc& arc) {
2704 --_deg[_digraph.source(arc)];
2707 virtual void erase(const std::vector<Arc>& arcs) {
2708 for (int i = 0; i < int(arcs.size()); ++i) {
2709 --_deg[_digraph.source(arcs[i])];
2713 virtual void build() {
2714 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2715 _deg[it] = countOutArcs(_digraph, it);
2719 virtual void clear() {
2720 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2726 const Digraph& _digraph;
2730 /// \brief Potential difference map
2732 /// PotentialMap returns the difference between the potentials of the
2733 /// source and target nodes of each arc in a digraph, i.e. it returns
2735 /// potential[gr.target(arc)] - potential[gr.source(arc)].
2737 /// \tparam GR The digraph type.
2738 /// \tparam POT A node map storing the potentials.
2739 template <typename GR, typename POT>
2740 class PotentialDifferenceMap {
2743 typedef typename GR::Arc Key;
2745 typedef typename POT::Value Value;
2747 /// \brief Constructor
2749 /// Contructor of the map.
2750 explicit PotentialDifferenceMap(const GR& gr,
2751 const POT& potential)
2752 : _digraph(gr), _potential(potential) {}
2754 /// \brief Returns the potential difference for the given arc.
2756 /// Returns the potential difference for the given arc, i.e.
2758 /// potential[gr.target(arc)] - potential[gr.source(arc)].
2760 Value operator[](const Key& arc) const {
2761 return _potential[_digraph.target(arc)] -
2762 _potential[_digraph.source(arc)];
2767 const POT& _potential;
2770 /// \brief Returns a PotentialDifferenceMap.
2772 /// This function just returns a PotentialDifferenceMap.
2773 /// \relates PotentialDifferenceMap
2774 template <typename GR, typename POT>
2775 PotentialDifferenceMap<GR, POT>
2776 potentialDifferenceMap(const GR& gr, const POT& potential) {
2777 return PotentialDifferenceMap<GR, POT>(gr, potential);
2783 #endif // LEMON_MAPS_H