1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
27 #include <lemon/core.h>
28 #include <lemon/bits/stl_iterators.h>
32 ///\brief Miscellaneous property maps
39 /// Base class of maps.
41 /// Base class of maps. It provides the necessary type definitions
42 /// required by the map %concepts.
43 template<typename K, typename V>
46 /// \brief The key type of the map.
48 /// \brief The value type of the map.
49 /// (The type of objects associated with the keys).
54 /// Null map. (a.k.a. DoNothingMap)
56 /// This map can be used if you have to provide a map only for
57 /// its type definitions, or if you have to provide a writable map,
58 /// but data written to it is not required (i.e. it will be sent to
59 /// <tt>/dev/null</tt>).
60 /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
63 template<typename K, typename V>
64 class NullMap : public MapBase<K, V> {
71 /// Gives back a default constructed element.
72 Value operator[](const Key&) const { return Value(); }
73 /// Absorbs the value.
74 void set(const Key&, const Value&) {}
77 /// Returns a \c NullMap class
79 /// This function just returns a \c NullMap class.
81 template <typename K, typename V>
82 NullMap<K, V> nullMap() {
83 return NullMap<K, V>();
89 /// This \ref concepts::ReadMap "readable map" assigns a specified
90 /// value to each key.
92 /// In other aspects it is equivalent to \c NullMap.
93 /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
94 /// concept, but it absorbs the data written to it.
96 /// The simplest way of using this map is through the constMap()
101 template<typename K, typename V>
102 class ConstMap : public MapBase<K, V> {
111 /// Default constructor
113 /// Default constructor.
114 /// The value of the map will be default constructed.
117 /// Constructor with specified initial value
119 /// Constructor with specified initial value.
120 /// \param v The initial value of the map.
121 ConstMap(const Value &v) : _value(v) {}
123 /// Gives back the specified value.
124 Value operator[](const Key&) const { return _value; }
126 /// Absorbs the value.
127 void set(const Key&, const Value&) {}
129 /// Sets the value that is assigned to each key.
130 void setAll(const Value &v) {
134 template<typename V1>
135 ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
138 /// Returns a \c ConstMap class
140 /// This function just returns a \c ConstMap class.
141 /// \relates ConstMap
142 template<typename K, typename V>
143 inline ConstMap<K, V> constMap(const V &v) {
144 return ConstMap<K, V>(v);
147 template<typename K, typename V>
148 inline ConstMap<K, V> constMap() {
149 return ConstMap<K, V>();
153 template<typename T, T v>
156 /// Constant map with inlined constant value.
158 /// This \ref concepts::ReadMap "readable map" assigns a specified
159 /// value to each key.
161 /// In other aspects it is equivalent to \c NullMap.
162 /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
163 /// concept, but it absorbs the data written to it.
165 /// The simplest way of using this map is through the constMap()
170 template<typename K, typename V, V v>
171 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
181 /// Gives back the specified value.
182 Value operator[](const Key&) const { return v; }
184 /// Absorbs the value.
185 void set(const Key&, const Value&) {}
188 /// Returns a \c ConstMap class with inlined constant value
190 /// This function just returns a \c ConstMap class with inlined
192 /// \relates ConstMap
193 template<typename K, typename V, V v>
194 inline ConstMap<K, Const<V, v> > constMap() {
195 return ConstMap<K, Const<V, v> >();
201 /// This \ref concepts::ReadMap "read-only map" gives back the given
202 /// key as value without any modification.
205 template <typename T>
206 class IdentityMap : public MapBase<T, T> {
213 /// Gives back the given value without any modification.
214 Value operator[](const Key &k) const {
219 /// Returns an \c IdentityMap class
221 /// This function just returns an \c IdentityMap class.
222 /// \relates IdentityMap
224 inline IdentityMap<T> identityMap() {
225 return IdentityMap<T>();
229 /// \brief Map for storing values for integer keys from the range
230 /// <tt>[0..size-1]</tt>.
232 /// This map is essentially a wrapper for \c std::vector. It assigns
233 /// values to integer keys from the range <tt>[0..size-1]</tt>.
234 /// It can be used together with some data structures, e.g.
235 /// heap types and \c UnionFind, when the used items are small
236 /// integers. This map conforms to the \ref concepts::ReferenceMap
237 /// "ReferenceMap" concept.
239 /// The simplest way of using this map is through the rangeMap()
241 template <typename V>
242 class RangeMap : public MapBase<int, V> {
243 template <typename V1>
244 friend class RangeMap;
247 typedef std::vector<V> Vector;
257 typedef typename Vector::reference Reference;
258 /// Const reference type
259 typedef typename Vector::const_reference ConstReference;
261 typedef True ReferenceMapTag;
265 /// Constructor with specified default value.
266 RangeMap(int size = 0, const Value &value = Value())
267 : _vector(size, value) {}
269 /// Constructs the map from an appropriate \c std::vector.
270 template <typename V1>
271 RangeMap(const std::vector<V1>& vector)
272 : _vector(vector.begin(), vector.end()) {}
274 /// Constructs the map from another \c RangeMap.
275 template <typename V1>
276 RangeMap(const RangeMap<V1> &c)
277 : _vector(c._vector.begin(), c._vector.end()) {}
279 /// Returns the size of the map.
281 return _vector.size();
286 /// Resizes the underlying \c std::vector container, so changes the
287 /// keyset of the map.
288 /// \param size The new size of the map. The new keyset will be the
289 /// range <tt>[0..size-1]</tt>.
290 /// \param value The default value to assign to the new keys.
291 void resize(int size, const Value &value = Value()) {
292 _vector.resize(size, value);
297 RangeMap& operator=(const RangeMap&);
302 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 to 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 to 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).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
1796 /// std::vector<Node> v(countNodes(g));
1797 /// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
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 The inverse map type of IdMap.
1871 /// The inverse map type of IdMap. The subscript operator gives back
1872 /// an item by its id.
1873 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1878 /// \brief Constructor.
1880 /// Constructor for creating an id-to-item map.
1881 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1883 /// \brief Constructor.
1885 /// Constructor for creating an id-to-item map.
1886 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1888 /// \brief Gives back an item by its id.
1890 /// Gives back an item by its id.
1891 Item operator[](int id) const { return _graph->fromId(id, Item());}
1894 const Graph* _graph;
1897 /// \brief Gives back the inverse of the map.
1899 /// Gives back the inverse of the IdMap.
1900 InverseMap inverse() const { return InverseMap(*_graph);}
1903 /// \brief Returns an \c IdMap class.
1905 /// This function just returns an \c IdMap class.
1907 template <typename K, typename GR>
1908 inline IdMap<GR, K> idMap(const GR& graph) {
1909 return IdMap<GR, K>(graph);
1912 /// \brief General cross reference graph map type.
1914 /// This class provides simple invertable graph maps.
1915 /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1916 /// and if a key is set to a new value, then stores it in the inverse map.
1917 /// The graph items can be accessed by their values either using
1918 /// \c InverseMap or \c operator()(), and the values of the map can be
1919 /// accessed with an STL compatible forward iterator (\c ValueIt).
1921 /// This map is intended to be used when all associated values are
1922 /// different (the map is actually invertable) or there are only a few
1923 /// items with the same value.
1924 /// Otherwise consider to use \c IterableValueMap, which is more
1925 /// suitable and more efficient for such cases. It provides iterators
1926 /// to traverse the items with the same associated value, but
1927 /// it does not have \c InverseMap.
1929 /// This type is not reference map, so it cannot be modified with
1930 /// the subscript operator.
1932 /// \tparam GR The graph type.
1933 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1935 /// \tparam V The value type of the map.
1937 /// \see IterableValueMap
1938 template <typename GR, typename K, typename V>
1940 : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1943 typedef typename ItemSetTraits<GR, K>::
1944 template Map<V>::Type Map;
1946 typedef std::multimap<V, K> Container;
1951 /// The graph type of CrossRefMap.
1954 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1956 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1958 /// The value type of CrossRefMap.
1961 /// \brief Constructor.
1963 /// Construct a new CrossRefMap for the given graph.
1964 explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1966 /// \brief Forward iterator for values.
1968 /// This iterator is an STL compatible forward
1969 /// iterator on the values of the map. The values can
1970 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1971 /// They are considered with multiplicity, so each value is
1972 /// traversed for each item it is assigned to.
1974 : public std::iterator<std::forward_iterator_tag, Value> {
1975 friend class CrossRefMap;
1977 ValueIt(typename Container::const_iterator _it)
1985 ValueIt& operator++() { ++it; return *this; }
1987 ValueIt operator++(int) {
1994 const Value& operator*() const { return it->first; }
1996 const Value* operator->() const { return &(it->first); }
1999 bool operator==(ValueIt jt) const { return it == jt.it; }
2001 bool operator!=(ValueIt jt) const { return it != jt.it; }
2004 typename Container::const_iterator it;
2007 /// Alias for \c ValueIt
2008 typedef ValueIt ValueIterator;
2010 /// \brief Returns an iterator to the first value.
2012 /// Returns an STL compatible iterator to the
2013 /// first value of the map. The values of the
2014 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2016 ValueIt beginValue() const {
2017 return ValueIt(_inv_map.begin());
2020 /// \brief Returns an iterator after the last value.
2022 /// Returns an STL compatible iterator after the
2023 /// last value of the map. The values of the
2024 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2026 ValueIt endValue() const {
2027 return ValueIt(_inv_map.end());
2030 /// \brief Sets the value associated with the given key.
2032 /// Sets the value associated with the given key.
2033 void set(const Key& key, const Value& val) {
2034 Value oldval = Map::operator[](key);
2035 typename Container::iterator it;
2036 for (it = _inv_map.equal_range(oldval).first;
2037 it != _inv_map.equal_range(oldval).second; ++it) {
2038 if (it->second == key) {
2043 _inv_map.insert(std::make_pair(val, key));
2047 /// \brief Returns the value associated with the given key.
2049 /// Returns the value associated with the given key.
2050 typename MapTraits<Map>::ConstReturnValue
2051 operator[](const Key& key) const {
2052 return Map::operator[](key);
2055 /// \brief Gives back an item by its value.
2057 /// This function gives back an item that is assigned to
2058 /// the given value or \c INVALID if no such item exists.
2059 /// If there are more items with the same associated value,
2060 /// only one of them is returned.
2061 Key operator()(const Value& val) const {
2062 typename Container::const_iterator it = _inv_map.find(val);
2063 return it != _inv_map.end() ? it->second : INVALID;
2066 /// \brief Returns the number of items with the given value.
2068 /// This function returns the number of items with the given value
2069 /// associated with it.
2070 int count(const Value &val) const {
2071 return _inv_map.count(val);
2076 /// \brief Erase the key from the map and the inverse map.
2078 /// Erase the key from the map and the inverse map. It is called by the
2079 /// \c AlterationNotifier.
2080 virtual void erase(const Key& key) {
2081 Value val = Map::operator[](key);
2082 typename Container::iterator it;
2083 for (it = _inv_map.equal_range(val).first;
2084 it != _inv_map.equal_range(val).second; ++it) {
2085 if (it->second == key) {
2093 /// \brief Erase more keys from the map and the inverse map.
2095 /// Erase more keys from the map and the inverse map. It is called by the
2096 /// \c AlterationNotifier.
2097 virtual void erase(const std::vector<Key>& keys) {
2098 for (int i = 0; i < int(keys.size()); ++i) {
2099 Value val = Map::operator[](keys[i]);
2100 typename Container::iterator it;
2101 for (it = _inv_map.equal_range(val).first;
2102 it != _inv_map.equal_range(val).second; ++it) {
2103 if (it->second == keys[i]) {
2112 /// \brief Clear the keys from the map and the inverse map.
2114 /// Clear the keys from the map and the inverse map. It is called by the
2115 /// \c AlterationNotifier.
2116 virtual void clear() {
2123 /// \brief The inverse map type of CrossRefMap.
2125 /// The inverse map type of CrossRefMap. The subscript operator gives
2126 /// back an item by its value.
2127 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2131 /// \brief Constructor
2133 /// Constructor of the InverseMap.
2134 explicit InverseMap(const CrossRefMap& inverted)
2135 : _inverted(inverted) {}
2137 /// The value type of the InverseMap.
2138 typedef typename CrossRefMap::Key Value;
2139 /// The key type of the InverseMap.
2140 typedef typename CrossRefMap::Value Key;
2142 /// \brief Subscript operator.
2144 /// Subscript operator. It gives back an item
2145 /// that is assigned to the given value or \c INVALID
2146 /// if no such item exists.
2147 Value operator[](const Key& key) const {
2148 return _inverted(key);
2152 const CrossRefMap& _inverted;
2155 /// \brief Gives back the inverse of the map.
2157 /// Gives back the inverse of the CrossRefMap.
2158 InverseMap inverse() const {
2159 return InverseMap(*this);
2164 /// \brief Provides continuous and unique id for the
2165 /// items of a graph.
2167 /// RangeIdMap provides a unique and continuous
2168 /// id for each item of a given type (\c Node, \c Arc or
2169 /// \c Edge) in a graph. This id is
2170 /// - \b unique: different items get different ids,
2171 /// - \b continuous: the range of the ids is the set of integers
2172 /// between 0 and \c n-1, where \c n is the number of the items of
2173 /// this type (\c Node, \c Arc or \c Edge).
2174 /// - So, the ids can change when deleting an item of the same type.
2176 /// Thus this id is not (necessarily) the same as what can get using
2177 /// the \c id() function of the graph or \ref IdMap.
2178 /// This map can be inverted with its member class \c InverseMap,
2179 /// or with the \c operator()() member.
2181 /// \tparam GR The graph type.
2182 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2186 template <typename GR, typename K>
2188 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2190 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2193 /// The graph type of RangeIdMap.
2196 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2198 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2200 /// The value type of RangeIdMap.
2203 /// \brief Constructor.
2206 explicit RangeIdMap(const Graph& gr) : Map(gr) {
2208 const typename Map::Notifier* nf = Map::notifier();
2209 for (nf->first(it); it != INVALID; nf->next(it)) {
2210 Map::set(it, _inv_map.size());
2211 _inv_map.push_back(it);
2217 /// \brief Adds a new key to the map.
2219 /// Add a new key to the map. It is called by the
2220 /// \c AlterationNotifier.
2221 virtual void add(const Item& item) {
2223 Map::set(item, _inv_map.size());
2224 _inv_map.push_back(item);
2227 /// \brief Add more new keys to the map.
2229 /// Add more new keys to the map. It is called by the
2230 /// \c AlterationNotifier.
2231 virtual void add(const std::vector<Item>& items) {
2233 for (int i = 0; i < int(items.size()); ++i) {
2234 Map::set(items[i], _inv_map.size());
2235 _inv_map.push_back(items[i]);
2239 /// \brief Erase the key from the map.
2241 /// Erase the key from the map. It is called by the
2242 /// \c AlterationNotifier.
2243 virtual void erase(const Item& item) {
2244 Map::set(_inv_map.back(), Map::operator[](item));
2245 _inv_map[Map::operator[](item)] = _inv_map.back();
2246 _inv_map.pop_back();
2250 /// \brief Erase more keys from the map.
2252 /// Erase more keys from the map. It is called by the
2253 /// \c AlterationNotifier.
2254 virtual void erase(const std::vector<Item>& items) {
2255 for (int i = 0; i < int(items.size()); ++i) {
2256 Map::set(_inv_map.back(), Map::operator[](items[i]));
2257 _inv_map[Map::operator[](items[i])] = _inv_map.back();
2258 _inv_map.pop_back();
2263 /// \brief Build the unique map.
2265 /// Build the unique map. It is called by the
2266 /// \c AlterationNotifier.
2267 virtual void build() {
2270 const typename Map::Notifier* nf = Map::notifier();
2271 for (nf->first(it); it != INVALID; nf->next(it)) {
2272 Map::set(it, _inv_map.size());
2273 _inv_map.push_back(it);
2277 /// \brief Clear the keys from the map.
2279 /// Clear the keys from the map. It is called by the
2280 /// \c AlterationNotifier.
2281 virtual void clear() {
2288 /// \brief Returns the maximal value plus one.
2290 /// Returns the maximal value plus one in the map.
2291 unsigned int size() const {
2292 return _inv_map.size();
2295 /// \brief Swaps the position of the two items in the map.
2297 /// Swaps the position of the two items in the map.
2298 void swap(const Item& p, const Item& q) {
2299 int pi = Map::operator[](p);
2300 int qi = Map::operator[](q);
2307 /// \brief Gives back the \e range \e id of the item
2309 /// Gives back the \e range \e id of the item.
2310 int operator[](const Item& item) const {
2311 return Map::operator[](item);
2314 /// \brief Gives back the item belonging to a \e range \e id
2316 /// Gives back the item belonging to the given \e range \e id.
2317 Item operator()(int id) const {
2318 return _inv_map[id];
2323 typedef std::vector<Item> Container;
2328 /// \brief The inverse map type of RangeIdMap.
2330 /// The inverse map type of RangeIdMap. The subscript operator gives
2331 /// back an item by its \e range \e id.
2332 /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2335 /// \brief Constructor
2337 /// Constructor of the InverseMap.
2338 explicit InverseMap(const RangeIdMap& inverted)
2339 : _inverted(inverted) {}
2342 /// The value type of the InverseMap.
2343 typedef typename RangeIdMap::Key Value;
2344 /// The key type of the InverseMap.
2345 typedef typename RangeIdMap::Value Key;
2347 /// \brief Subscript operator.
2349 /// Subscript operator. It gives back the item
2350 /// that the given \e range \e id currently belongs to.
2351 Value operator[](const Key& key) const {
2352 return _inverted(key);
2355 /// \brief Size of the map.
2357 /// Returns the size of the map.
2358 unsigned int size() const {
2359 return _inverted.size();
2363 const RangeIdMap& _inverted;
2366 /// \brief Gives back the inverse of the map.
2368 /// Gives back the inverse of the RangeIdMap.
2369 const InverseMap inverse() const {
2370 return InverseMap(*this);
2374 /// \brief Returns a \c RangeIdMap class.
2376 /// This function just returns an \c RangeIdMap class.
2377 /// \relates RangeIdMap
2378 template <typename K, typename GR>
2379 inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
2380 return RangeIdMap<GR, K>(graph);
2383 /// \brief Dynamic iterable \c bool map.
2385 /// This class provides a special graph map type which can store a
2386 /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
2387 /// For both \c true and \c false values it is possible to iterate on
2388 /// the keys mapped to the value.
2390 /// This type is a reference map, so it can be modified with the
2391 /// subscript operator.
2393 /// \tparam GR The graph type.
2394 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2397 /// \see IterableIntMap, IterableValueMap
2398 /// \see CrossRefMap
2399 template <typename GR, typename K>
2400 class IterableBoolMap
2401 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2405 typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
2406 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
2408 std::vector<K> _array;
2413 /// Indicates that the map is reference map.
2414 typedef True ReferenceMapTag;
2420 /// The const reference type.
2421 typedef const Value& ConstReference;
2425 int position(const Key& key) const {
2426 return Parent::operator[](key);
2431 /// \brief Reference to the value of the map.
2433 /// This class is similar to the \c bool type. It can be converted to
2434 /// \c bool and it provides the same operators.
2436 friend class IterableBoolMap;
2438 Reference(IterableBoolMap& map, const Key& key)
2439 : _key(key), _map(map) {}
2442 Reference& operator=(const Reference& value) {
2443 _map.set(_key, static_cast<bool>(value));
2447 operator bool() const {
2448 return static_cast<const IterableBoolMap&>(_map)[_key];
2451 Reference& operator=(bool value) {
2452 _map.set(_key, value);
2455 Reference& operator&=(bool value) {
2456 _map.set(_key, _map[_key] & value);
2459 Reference& operator|=(bool value) {
2460 _map.set(_key, _map[_key] | value);
2463 Reference& operator^=(bool value) {
2464 _map.set(_key, _map[_key] ^ value);
2469 IterableBoolMap& _map;
2472 /// \brief Constructor of the map with a default value.
2474 /// Constructor of the map with a default value.
2475 explicit IterableBoolMap(const Graph& graph, bool def = false)
2477 typename Parent::Notifier* nf = Parent::notifier();
2479 for (nf->first(it); it != INVALID; nf->next(it)) {
2480 Parent::set(it, _array.size());
2481 _array.push_back(it);
2483 _sep = (def ? _array.size() : 0);
2486 /// \brief Const subscript operator of the map.
2488 /// Const subscript operator of the map.
2489 bool operator[](const Key& key) const {
2490 return position(key) < _sep;
2493 /// \brief Subscript operator of the map.
2495 /// Subscript operator of the map.
2496 Reference operator[](const Key& key) {
2497 return Reference(*this, key);
2500 /// \brief Set operation of the map.
2502 /// Set operation of the map.
2503 void set(const Key& key, bool value) {
2504 int pos = position(key);
2506 if (pos < _sep) return;
2507 Key tmp = _array[_sep];
2509 Parent::set(key, _sep);
2511 Parent::set(tmp, pos);
2514 if (pos >= _sep) return;
2516 Key tmp = _array[_sep];
2518 Parent::set(key, _sep);
2520 Parent::set(tmp, pos);
2524 /// \brief Set all items.
2526 /// Set all items in the map.
2527 /// \note Constant time operation.
2528 void setAll(bool value) {
2529 _sep = (value ? _array.size() : 0);
2532 /// \brief Returns the number of the keys mapped to \c true.
2534 /// Returns the number of the keys mapped to \c true.
2535 int trueNum() const {
2539 /// \brief Returns the number of the keys mapped to \c false.
2541 /// Returns the number of the keys mapped to \c false.
2542 int falseNum() const {
2543 return _array.size() - _sep;
2546 /// \brief Iterator for the keys mapped to \c true.
2548 /// Iterator for the keys mapped to \c true. It works
2549 /// like a graph item iterator, it can be converted to
2550 /// the key type of the map, incremented with \c ++ operator, and
2551 /// if the iterator leaves the last valid key, it will be equal to
2553 class TrueIt : public Key {
2557 /// \brief Creates an iterator.
2559 /// Creates an iterator. It iterates on the
2560 /// keys mapped to \c true.
2561 /// \param map The IterableBoolMap.
2562 explicit TrueIt(const IterableBoolMap& map)
2563 : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2566 /// \brief Invalid constructor \& conversion.
2568 /// This constructor initializes the iterator to be invalid.
2569 /// \sa Invalid for more details.
2570 TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2572 /// \brief Increment operator.
2574 /// Increment operator.
2575 TrueIt& operator++() {
2576 int pos = _map->position(*this);
2577 Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
2582 const IterableBoolMap* _map;
2585 /// \brief STL style iterator for the keys mapped to \c true.
2587 /// This is an STL style wrapper for \ref TrueIt.
2588 /// It can be used in range-based for loops, STL algorithms, etc.
2589 LemonRangeWrapper1<TrueIt, IterableBoolMap>
2591 return LemonRangeWrapper1<TrueIt, IterableBoolMap>(*this);
2595 /// \brief Iterator for the keys mapped to \c false.
2597 /// Iterator for the keys mapped to \c false. It works
2598 /// like a graph item iterator, it can be converted to
2599 /// the key type of the map, incremented with \c ++ operator, and
2600 /// if the iterator leaves the last valid key, it will be equal to
2602 class FalseIt : public Key {
2606 /// \brief Creates an iterator.
2608 /// Creates an iterator. It iterates on the
2609 /// keys mapped to \c false.
2610 /// \param map The IterableBoolMap.
2611 explicit FalseIt(const IterableBoolMap& map)
2612 : Parent(map._sep < int(map._array.size()) ?
2613 map._array.back() : INVALID), _map(&map) {}
2615 /// \brief Invalid constructor \& conversion.
2617 /// This constructor initializes the iterator to be invalid.
2618 /// \sa Invalid for more details.
2619 FalseIt(Invalid) : Parent(INVALID), _map(0) {}
2621 /// \brief Increment operator.
2623 /// Increment operator.
2624 FalseIt& operator++() {
2625 int pos = _map->position(*this);
2626 Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
2631 const IterableBoolMap* _map;
2634 /// \brief STL style iterator for the keys mapped to \c false.
2636 /// This is an STL style wrapper for \ref FalseIt.
2637 /// It can be used in range-based for loops, STL algorithms, etc.
2638 LemonRangeWrapper1<FalseIt, IterableBoolMap>
2640 return LemonRangeWrapper1<FalseIt, IterableBoolMap>(*this);
2644 /// \brief Iterator for the keys mapped to a given value.
2646 /// Iterator for the keys mapped to a given value. It works
2647 /// like a graph item iterator, it can be converted to
2648 /// the key type of the map, incremented with \c ++ operator, and
2649 /// if the iterator leaves the last valid key, it will be equal to
2651 class ItemIt : public Key {
2655 /// \brief Creates an iterator with a value.
2657 /// Creates an iterator with a value. It iterates on the
2658 /// keys mapped to the given value.
2659 /// \param map The IterableBoolMap.
2660 /// \param value The value.
2661 ItemIt(const IterableBoolMap& map, bool value)
2664 map._array[map._sep - 1] : INVALID) :
2665 (map._sep < int(map._array.size()) ?
2666 map._array.back() : INVALID)), _map(&map) {}
2668 /// \brief Invalid constructor \& conversion.
2670 /// This constructor initializes the iterator to be invalid.
2671 /// \sa Invalid for more details.
2672 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2674 /// \brief Increment operator.
2676 /// Increment operator.
2677 ItemIt& operator++() {
2678 int pos = _map->position(*this);
2679 int _sep = pos >= _map->_sep ? _map->_sep : 0;
2680 Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
2685 const IterableBoolMap* _map;
2688 /// \brief STL style iterator for the keys mapped to a given value.
2690 /// This is an STL style wrapper for \ref ItemIt.
2691 /// It can be used in range-based for loops, STL algorithms, etc.
2692 LemonRangeWrapper2<ItemIt, IterableBoolMap, bool>
2694 return LemonRangeWrapper2<ItemIt, IterableBoolMap, bool>(*this, value);
2699 virtual void add(const Key& key) {
2701 Parent::set(key, _array.size());
2702 _array.push_back(key);
2705 virtual void add(const std::vector<Key>& keys) {
2707 for (int i = 0; i < int(keys.size()); ++i) {
2708 Parent::set(keys[i], _array.size());
2709 _array.push_back(keys[i]);
2713 virtual void erase(const Key& key) {
2714 int pos = position(key);
2717 Parent::set(_array[_sep], pos);
2718 _array[pos] = _array[_sep];
2719 Parent::set(_array.back(), _sep);
2720 _array[_sep] = _array.back();
2723 Parent::set(_array.back(), pos);
2724 _array[pos] = _array.back();
2730 virtual void erase(const std::vector<Key>& keys) {
2731 for (int i = 0; i < int(keys.size()); ++i) {
2732 int pos = position(keys[i]);
2735 Parent::set(_array[_sep], pos);
2736 _array[pos] = _array[_sep];
2737 Parent::set(_array.back(), _sep);
2738 _array[_sep] = _array.back();
2741 Parent::set(_array.back(), pos);
2742 _array[pos] = _array.back();
2746 Parent::erase(keys);
2749 virtual void build() {
2751 typename Parent::Notifier* nf = Parent::notifier();
2753 for (nf->first(it); it != INVALID; nf->next(it)) {
2754 Parent::set(it, _array.size());
2755 _array.push_back(it);
2760 virtual void clear() {
2769 namespace _maps_bits {
2770 template <typename Item>
2771 struct IterableIntMapNode {
2772 IterableIntMapNode() : value(-1) {}
2773 IterableIntMapNode(int _value) : value(_value) {}
2779 /// \brief Dynamic iterable integer map.
2781 /// This class provides a special graph map type which can store an
2782 /// integer value for graph items (\c Node, \c Arc or \c Edge).
2783 /// For each non-negative value it is possible to iterate on the keys
2784 /// mapped to the value.
2786 /// This map is intended to be used with small integer values, for which
2787 /// it is efficient, and supports iteration only for non-negative values.
2788 /// If you need large values and/or iteration for negative integers,
2789 /// consider to use \ref IterableValueMap instead.
2791 /// This type is a reference map, so it can be modified with the
2792 /// subscript operator.
2794 /// \note The size of the data structure depends on the largest
2795 /// value in the map.
2797 /// \tparam GR The graph type.
2798 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2801 /// \see IterableBoolMap, IterableValueMap
2802 /// \see CrossRefMap
2803 template <typename GR, typename K>
2804 class IterableIntMap
2805 : protected ItemSetTraits<GR, K>::
2806 template Map<_maps_bits::IterableIntMapNode<K> >::Type {
2808 typedef typename ItemSetTraits<GR, K>::
2809 template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
2818 /// \brief Constructor of the map.
2820 /// Constructor of the map. It sets all values to -1.
2821 explicit IterableIntMap(const Graph& graph)
2824 /// \brief Constructor of the map with a given value.
2826 /// Constructor of the map with a given value.
2827 explicit IterableIntMap(const Graph& graph, int value)
2828 : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
2830 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
2838 void unlace(const Key& key) {
2839 typename Parent::Value& node = Parent::operator[](key);
2840 if (node.value < 0) return;
2841 if (node.prev != INVALID) {
2842 Parent::operator[](node.prev).next = node.next;
2844 _first[node.value] = node.next;
2846 if (node.next != INVALID) {
2847 Parent::operator[](node.next).prev = node.prev;
2849 while (!_first.empty() && _first.back() == INVALID) {
2854 void lace(const Key& key) {
2855 typename Parent::Value& node = Parent::operator[](key);
2856 if (node.value < 0) return;
2857 if (node.value >= int(_first.size())) {
2858 _first.resize(node.value + 1, INVALID);
2860 node.prev = INVALID;
2861 node.next = _first[node.value];
2862 if (node.next != INVALID) {
2863 Parent::operator[](node.next).prev = key;
2865 _first[node.value] = key;
2870 /// Indicates that the map is reference map.
2871 typedef True ReferenceMapTag;
2873 /// \brief Reference to the value of the map.
2875 /// This class is similar to the \c int type. It can
2876 /// be converted to \c int and it has the same operators.
2878 friend class IterableIntMap;
2880 Reference(IterableIntMap& map, const Key& key)
2881 : _key(key), _map(map) {}
2884 Reference& operator=(const Reference& value) {
2885 _map.set(_key, static_cast<const int&>(value));
2889 operator const int&() const {
2890 return static_cast<const IterableIntMap&>(_map)[_key];
2893 Reference& operator=(int value) {
2894 _map.set(_key, value);
2897 Reference& operator++() {
2898 _map.set(_key, _map[_key] + 1);
2901 int operator++(int) {
2902 int value = _map[_key];
2903 _map.set(_key, value + 1);
2906 Reference& operator--() {
2907 _map.set(_key, _map[_key] - 1);
2910 int operator--(int) {
2911 int value = _map[_key];
2912 _map.set(_key, value - 1);
2915 Reference& operator+=(int value) {
2916 _map.set(_key, _map[_key] + value);
2919 Reference& operator-=(int value) {
2920 _map.set(_key, _map[_key] - value);
2923 Reference& operator*=(int value) {
2924 _map.set(_key, _map[_key] * value);
2927 Reference& operator/=(int value) {
2928 _map.set(_key, _map[_key] / value);
2931 Reference& operator%=(int value) {
2932 _map.set(_key, _map[_key] % value);
2935 Reference& operator&=(int value) {
2936 _map.set(_key, _map[_key] & value);
2939 Reference& operator|=(int value) {
2940 _map.set(_key, _map[_key] | value);
2943 Reference& operator^=(int value) {
2944 _map.set(_key, _map[_key] ^ value);
2947 Reference& operator<<=(int value) {
2948 _map.set(_key, _map[_key] << value);
2951 Reference& operator>>=(int value) {
2952 _map.set(_key, _map[_key] >> value);
2958 IterableIntMap& _map;
2961 /// The const reference type.
2962 typedef const Value& ConstReference;
2964 /// \brief Gives back the maximal value plus one.
2966 /// Gives back the maximal value plus one.
2968 return _first.size();
2971 /// \brief Set operation of the map.
2973 /// Set operation of the map.
2974 void set(const Key& key, const Value& value) {
2976 Parent::operator[](key).value = value;
2980 /// \brief Const subscript operator of the map.
2982 /// Const subscript operator of the map.
2983 const Value& operator[](const Key& key) const {
2984 return Parent::operator[](key).value;
2987 /// \brief Subscript operator of the map.
2989 /// Subscript operator of the map.
2990 Reference operator[](const Key& key) {
2991 return Reference(*this, key);
2994 /// \brief Iterator for the keys with the same value.
2996 /// Iterator for the keys with the same value. It works
2997 /// like a graph item iterator, it can be converted to
2998 /// the item type of the map, incremented with \c ++ operator, and
2999 /// if the iterator leaves the last valid item, it will be equal to
3001 class ItemIt : public Key {
3005 /// \brief Invalid constructor \& conversion.
3007 /// This constructor initializes the iterator to be invalid.
3008 /// \sa Invalid for more details.
3009 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3011 /// \brief Creates an iterator with a value.
3013 /// Creates an iterator with a value. It iterates on the
3014 /// keys mapped to the given value.
3015 /// \param map The IterableIntMap.
3016 /// \param value The value.
3017 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
3018 if (value < 0 || value >= int(_map->_first.size())) {
3019 Parent::operator=(INVALID);
3021 Parent::operator=(_map->_first[value]);
3025 /// \brief Increment operator.
3027 /// Increment operator.
3028 ItemIt& operator++() {
3029 Parent::operator=(_map->IterableIntMap::Parent::
3030 operator[](static_cast<Parent&>(*this)).next);
3035 const IterableIntMap* _map;
3038 /// \brief STL style iterator for the keys with the same value.
3040 /// This is an STL style wrapper for \ref ItemIt.
3041 /// It can be used in range-based for loops, STL algorithms, etc.
3042 LemonRangeWrapper2<ItemIt, IterableIntMap, int>
3044 return LemonRangeWrapper2<ItemIt, IterableIntMap, int>(*this, value);
3050 virtual void erase(const Key& key) {
3055 virtual void erase(const std::vector<Key>& keys) {
3056 for (int i = 0; i < int(keys.size()); ++i) {
3059 Parent::erase(keys);
3062 virtual void clear() {
3068 std::vector<Key> _first;
3071 namespace _maps_bits {
3072 template <typename Item, typename Value>
3073 struct IterableValueMapNode {
3074 IterableValueMapNode(Value _value = Value()) : value(_value) {}
3080 /// \brief Dynamic iterable map for comparable values.
3082 /// This class provides a special graph map type which can store a
3083 /// comparable value for graph items (\c Node, \c Arc or \c Edge).
3084 /// For each value it is possible to iterate on the keys mapped to
3085 /// the value (\c ItemIt), and the values of the map can be accessed
3086 /// with an STL compatible forward iterator (\c ValueIt).
3087 /// The map stores a linked list for each value, which contains
3088 /// the items mapped to the value, and the used values are stored
3089 /// in balanced binary tree (\c std::map).
3091 /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
3092 /// specialized for \c bool and \c int values, respectively.
3094 /// This type is not reference map, so it cannot be modified with
3095 /// the subscript operator.
3097 /// \tparam GR The graph type.
3098 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
3100 /// \tparam V The value type of the map. It can be any comparable
3103 /// \see IterableBoolMap, IterableIntMap
3104 /// \see CrossRefMap
3105 template <typename GR, typename K, typename V>
3106 class IterableValueMap
3107 : protected ItemSetTraits<GR, K>::
3108 template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
3110 typedef typename ItemSetTraits<GR, K>::
3111 template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
3122 /// \brief Constructor of the map with a given value.
3124 /// Constructor of the map with a given value.
3125 explicit IterableValueMap(const Graph& graph,
3126 const Value& value = Value())
3127 : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
3128 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3135 void unlace(const Key& key) {
3136 typename Parent::Value& node = Parent::operator[](key);
3137 if (node.prev != INVALID) {
3138 Parent::operator[](node.prev).next = node.next;
3140 if (node.next != INVALID) {
3141 _first[node.value] = node.next;
3143 _first.erase(node.value);
3146 if (node.next != INVALID) {
3147 Parent::operator[](node.next).prev = node.prev;
3151 void lace(const Key& key) {
3152 typename Parent::Value& node = Parent::operator[](key);
3153 typename std::map<Value, Key>::iterator it = _first.find(node.value);
3154 if (it == _first.end()) {
3155 node.prev = node.next = INVALID;
3156 _first.insert(std::make_pair(node.value, key));
3158 node.prev = INVALID;
3159 node.next = it->second;
3160 if (node.next != INVALID) {
3161 Parent::operator[](node.next).prev = key;
3169 /// \brief Forward iterator for values.
3171 /// This iterator is an STL compatible forward
3172 /// iterator on the values of the map. The values can
3173 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
3175 : public std::iterator<std::forward_iterator_tag, Value> {
3176 friend class IterableValueMap;
3178 ValueIt(typename std::map<Value, Key>::const_iterator _it)
3186 ValueIt& operator++() { ++it; return *this; }
3188 ValueIt operator++(int) {
3195 const Value& operator*() const { return it->first; }
3197 const Value* operator->() const { return &(it->first); }
3200 bool operator==(ValueIt jt) const { return it == jt.it; }
3202 bool operator!=(ValueIt jt) const { return it != jt.it; }
3205 typename std::map<Value, Key>::const_iterator it;
3208 /// \brief Returns an iterator to the first value.
3210 /// Returns an STL compatible iterator to the
3211 /// first value of the map. The values of the
3212 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3214 ValueIt beginValue() const {
3215 return ValueIt(_first.begin());
3218 /// \brief Returns an iterator after the last value.
3220 /// Returns an STL compatible iterator after the
3221 /// last value of the map. The values of the
3222 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3224 ValueIt endValue() const {
3225 return ValueIt(_first.end());
3228 /// \brief Set operation of the map.
3230 /// Set operation of the map.
3231 void set(const Key& key, const Value& value) {
3233 Parent::operator[](key).value = value;
3237 /// \brief Const subscript operator of the map.
3239 /// Const subscript operator of the map.
3240 const Value& operator[](const Key& key) const {
3241 return Parent::operator[](key).value;
3244 /// \brief Iterator for the keys with the same value.
3246 /// Iterator for the keys with the same value. It works
3247 /// like a graph item iterator, it can be converted to
3248 /// the item type of the map, incremented with \c ++ operator, and
3249 /// if the iterator leaves the last valid item, it will be equal to
3251 class ItemIt : public Key {
3255 /// \brief Invalid constructor \& conversion.
3257 /// This constructor initializes the iterator to be invalid.
3258 /// \sa Invalid for more details.
3259 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3261 /// \brief Creates an iterator with a value.
3263 /// Creates an iterator with a value. It iterates on the
3264 /// keys which have the given value.
3265 /// \param map The IterableValueMap
3266 /// \param value The value
3267 ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
3268 typename std::map<Value, Key>::const_iterator it =
3269 map._first.find(value);
3270 if (it == map._first.end()) {
3271 Parent::operator=(INVALID);
3273 Parent::operator=(it->second);
3277 /// \brief Increment operator.
3279 /// Increment Operator.
3280 ItemIt& operator++() {
3281 Parent::operator=(_map->IterableValueMap::Parent::
3282 operator[](static_cast<Parent&>(*this)).next);
3288 const IterableValueMap* _map;
3291 /// \brief STL style iterator for the keys with the same value.
3293 /// This is an STL style wrapper for \ref ItemIt.
3294 /// It can be used in range-based for loops, STL algorithms, etc.
3295 LemonRangeWrapper2<ItemIt, IterableValueMap, V>
3296 items(const V& value) {
3297 return LemonRangeWrapper2<ItemIt, IterableValueMap, V>(*this, value);
3303 virtual void add(const Key& key) {
3308 virtual void add(const std::vector<Key>& keys) {
3310 for (int i = 0; i < int(keys.size()); ++i) {
3315 virtual void erase(const Key& key) {
3320 virtual void erase(const std::vector<Key>& keys) {
3321 for (int i = 0; i < int(keys.size()); ++i) {
3324 Parent::erase(keys);
3327 virtual void build() {
3329 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3334 virtual void clear() {
3340 std::map<Value, Key> _first;
3343 /// \brief Map of the source nodes of arcs in a digraph.
3345 /// SourceMap provides access for the source node of each arc in a digraph,
3346 /// which is returned by the \c source() function of the digraph.
3347 /// \tparam GR The digraph type.
3349 template <typename GR>
3353 /// The key type (the \c Arc type of the digraph).
3354 typedef typename GR::Arc Key;
3355 /// The value type (the \c Node type of the digraph).
3356 typedef typename GR::Node Value;
3358 /// \brief Constructor
3361 /// \param digraph The digraph that the map belongs to.
3362 explicit SourceMap(const GR& digraph) : _graph(digraph) {}
3364 /// \brief Returns the source node of the given arc.
3366 /// Returns the source node of the given arc.
3367 Value operator[](const Key& arc) const {
3368 return _graph.source(arc);
3375 /// \brief Returns a \c SourceMap class.
3377 /// This function just returns an \c SourceMap class.
3378 /// \relates SourceMap
3379 template <typename GR>
3380 inline SourceMap<GR> sourceMap(const GR& graph) {
3381 return SourceMap<GR>(graph);
3384 /// \brief Map of the target nodes of arcs in a digraph.
3386 /// TargetMap provides access for the target node of each arc in a digraph,
3387 /// which is returned by the \c target() function of the digraph.
3388 /// \tparam GR The digraph type.
3390 template <typename GR>
3394 /// The key type (the \c Arc type of the digraph).
3395 typedef typename GR::Arc Key;
3396 /// The value type (the \c Node type of the digraph).
3397 typedef typename GR::Node Value;
3399 /// \brief Constructor
3402 /// \param digraph The digraph that the map belongs to.
3403 explicit TargetMap(const GR& digraph) : _graph(digraph) {}
3405 /// \brief Returns the target node of the given arc.
3407 /// Returns the target node of the given arc.
3408 Value operator[](const Key& e) const {
3409 return _graph.target(e);
3416 /// \brief Returns a \c TargetMap class.
3418 /// This function just returns a \c TargetMap class.
3419 /// \relates TargetMap
3420 template <typename GR>
3421 inline TargetMap<GR> targetMap(const GR& graph) {
3422 return TargetMap<GR>(graph);
3425 /// \brief Map of the "forward" directed arc view of edges in a graph.
3427 /// ForwardMap provides access for the "forward" directed arc view of
3428 /// each edge in a graph, which is returned by the \c direct() function
3429 /// of the graph with \c true parameter.
3430 /// \tparam GR The graph type.
3431 /// \see BackwardMap
3432 template <typename GR>
3436 /// The key type (the \c Edge type of the digraph).
3437 typedef typename GR::Edge Key;
3438 /// The value type (the \c Arc type of the digraph).
3439 typedef typename GR::Arc Value;
3441 /// \brief Constructor
3444 /// \param graph The graph that the map belongs to.
3445 explicit ForwardMap(const GR& graph) : _graph(graph) {}
3447 /// \brief Returns the "forward" directed arc view of the given edge.
3449 /// Returns the "forward" directed arc view of the given edge.
3450 Value operator[](const Key& key) const {
3451 return _graph.direct(key, true);
3458 /// \brief Returns a \c ForwardMap class.
3460 /// This function just returns an \c ForwardMap class.
3461 /// \relates ForwardMap
3462 template <typename GR>
3463 inline ForwardMap<GR> forwardMap(const GR& graph) {
3464 return ForwardMap<GR>(graph);
3467 /// \brief Map of the "backward" directed arc view of edges in a graph.
3469 /// BackwardMap provides access for the "backward" directed arc view of
3470 /// each edge in a graph, which is returned by the \c direct() function
3471 /// of the graph with \c false parameter.
3472 /// \tparam GR The graph type.
3474 template <typename GR>
3478 /// The key type (the \c Edge type of the digraph).
3479 typedef typename GR::Edge Key;
3480 /// The value type (the \c Arc type of the digraph).
3481 typedef typename GR::Arc Value;
3483 /// \brief Constructor
3486 /// \param graph The graph that the map belongs to.
3487 explicit BackwardMap(const GR& graph) : _graph(graph) {}
3489 /// \brief Returns the "backward" directed arc view of the given edge.
3491 /// Returns the "backward" directed arc view of the given edge.
3492 Value operator[](const Key& key) const {
3493 return _graph.direct(key, false);
3500 /// \brief Returns a \c BackwardMap class
3502 /// This function just returns a \c BackwardMap class.
3503 /// \relates BackwardMap
3504 template <typename GR>
3505 inline BackwardMap<GR> backwardMap(const GR& graph) {
3506 return BackwardMap<GR>(graph);
3509 /// \brief Map of the in-degrees of nodes in a digraph.
3511 /// This map returns the in-degree of a node. Once it is constructed,
3512 /// the degrees are stored in a standard \c NodeMap, so each query is done
3513 /// in constant time. On the other hand, the values are updated automatically
3514 /// whenever the digraph changes.
3516 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3517 /// may provide alternative ways to modify the digraph.
3518 /// The correct behavior of InDegMap is not guarantied if these additional
3519 /// features are used. For example, the functions
3520 /// \ref ListDigraph::changeSource() "changeSource()",
3521 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3522 /// \ref ListDigraph::reverseArc() "reverseArc()"
3523 /// of \ref ListDigraph will \e not update the degree values correctly.
3526 template <typename GR>
3528 : protected ItemSetTraits<GR, typename GR::Arc>
3529 ::ItemNotifier::ObserverBase {
3533 /// The graph type of InDegMap
3537 typedef typename Digraph::Node Key;
3541 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3542 ::ItemNotifier::ObserverBase Parent;
3547 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3550 typedef typename ItemSetTraits<Digraph, Key>::
3551 template Map<int>::Type Parent;
3553 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3555 virtual void add(const Key& key) {
3557 Parent::set(key, 0);
3560 virtual void add(const std::vector<Key>& keys) {
3562 for (int i = 0; i < int(keys.size()); ++i) {
3563 Parent::set(keys[i], 0);
3567 virtual void build() {
3570 typename Parent::Notifier* nf = Parent::notifier();
3571 for (nf->first(it); it != INVALID; nf->next(it)) {
3579 /// \brief Constructor.
3581 /// Constructor for creating an in-degree map.
3582 explicit InDegMap(const Digraph& graph)
3583 : _digraph(graph), _deg(graph) {
3584 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3586 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3587 _deg[it] = countInArcs(_digraph, it);
3591 /// \brief Gives back the in-degree of a Node.
3593 /// Gives back the in-degree of a Node.
3594 int operator[](const Key& key) const {
3600 typedef typename Digraph::Arc Arc;
3602 virtual void add(const Arc& arc) {
3603 ++_deg[_digraph.target(arc)];
3606 virtual void add(const std::vector<Arc>& arcs) {
3607 for (int i = 0; i < int(arcs.size()); ++i) {
3608 ++_deg[_digraph.target(arcs[i])];
3612 virtual void erase(const Arc& arc) {
3613 --_deg[_digraph.target(arc)];
3616 virtual void erase(const std::vector<Arc>& arcs) {
3617 for (int i = 0; i < int(arcs.size()); ++i) {
3618 --_deg[_digraph.target(arcs[i])];
3622 virtual void build() {
3623 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3624 _deg[it] = countInArcs(_digraph, it);
3628 virtual void clear() {
3629 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3635 const Digraph& _digraph;
3639 /// \brief Map of the out-degrees of nodes in a digraph.
3641 /// This map returns the out-degree of a node. Once it is constructed,
3642 /// the degrees are stored in a standard \c NodeMap, so each query is done
3643 /// in constant time. On the other hand, the values are updated automatically
3644 /// whenever the digraph changes.
3646 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3647 /// may provide alternative ways to modify the digraph.
3648 /// The correct behavior of OutDegMap is not guarantied if these additional
3649 /// features are used. For example, the functions
3650 /// \ref ListDigraph::changeSource() "changeSource()",
3651 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3652 /// \ref ListDigraph::reverseArc() "reverseArc()"
3653 /// of \ref ListDigraph will \e not update the degree values correctly.
3656 template <typename GR>
3658 : protected ItemSetTraits<GR, typename GR::Arc>
3659 ::ItemNotifier::ObserverBase {
3663 /// The graph type of OutDegMap
3667 typedef typename Digraph::Node Key;
3671 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3672 ::ItemNotifier::ObserverBase Parent;
3677 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3680 typedef typename ItemSetTraits<Digraph, Key>::
3681 template Map<int>::Type Parent;
3683 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3685 virtual void add(const Key& key) {
3687 Parent::set(key, 0);
3689 virtual void add(const std::vector<Key>& keys) {
3691 for (int i = 0; i < int(keys.size()); ++i) {
3692 Parent::set(keys[i], 0);
3695 virtual void build() {
3698 typename Parent::Notifier* nf = Parent::notifier();
3699 for (nf->first(it); it != INVALID; nf->next(it)) {
3707 /// \brief Constructor.
3709 /// Constructor for creating an out-degree map.
3710 explicit OutDegMap(const Digraph& graph)
3711 : _digraph(graph), _deg(graph) {
3712 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3714 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3715 _deg[it] = countOutArcs(_digraph, it);
3719 /// \brief Gives back the out-degree of a Node.
3721 /// Gives back the out-degree of a Node.
3722 int operator[](const Key& key) const {
3728 typedef typename Digraph::Arc Arc;
3730 virtual void add(const Arc& arc) {
3731 ++_deg[_digraph.source(arc)];
3734 virtual void add(const std::vector<Arc>& arcs) {
3735 for (int i = 0; i < int(arcs.size()); ++i) {
3736 ++_deg[_digraph.source(arcs[i])];
3740 virtual void erase(const Arc& arc) {
3741 --_deg[_digraph.source(arc)];
3744 virtual void erase(const std::vector<Arc>& arcs) {
3745 for (int i = 0; i < int(arcs.size()); ++i) {
3746 --_deg[_digraph.source(arcs[i])];
3750 virtual void build() {
3751 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3752 _deg[it] = countOutArcs(_digraph, it);
3756 virtual void clear() {
3757 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3763 const Digraph& _digraph;
3767 /// \brief Potential difference map
3769 /// PotentialDifferenceMap returns the difference between the potentials of
3770 /// the source and target nodes of each arc in a digraph, i.e. it returns
3772 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3774 /// \tparam GR The digraph type.
3775 /// \tparam POT A node map storing the potentials.
3776 template <typename GR, typename POT>
3777 class PotentialDifferenceMap {
3780 typedef typename GR::Arc Key;
3782 typedef typename POT::Value Value;
3784 /// \brief Constructor
3786 /// Contructor of the map.
3787 explicit PotentialDifferenceMap(const GR& gr,
3788 const POT& potential)
3789 : _digraph(gr), _potential(potential) {}
3791 /// \brief Returns the potential difference for the given arc.
3793 /// Returns the potential difference for the given arc, i.e.
3795 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3797 Value operator[](const Key& arc) const {
3798 return _potential[_digraph.target(arc)] -
3799 _potential[_digraph.source(arc)];
3804 const POT& _potential;
3807 /// \brief Returns a PotentialDifferenceMap.
3809 /// This function just returns a PotentialDifferenceMap.
3810 /// \relates PotentialDifferenceMap
3811 template <typename GR, typename POT>
3812 PotentialDifferenceMap<GR, POT>
3813 potentialDifferenceMap(const GR& gr, const POT& potential) {
3814 return PotentialDifferenceMap<GR, POT>(gr, potential);
3818 /// \brief Copy the values of a graph map to another map.
3820 /// This function copies the values of a graph map to another graph map.
3821 /// \c To::Key must be equal or convertible to \c From::Key and
3822 /// \c From::Value must be equal or convertible to \c To::Value.
3824 /// For example, an edge map of \c int value type can be copied to
3825 /// an arc map of \c double value type in an undirected graph, but
3826 /// an arc map cannot be copied to an edge map.
3827 /// Note that even a \ref ConstMap can be copied to a standard graph map,
3828 /// but \ref mapFill() can also be used for this purpose.
3830 /// \param gr The graph for which the maps are defined.
3831 /// \param from The map from which the values have to be copied.
3832 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
3833 /// \param to The map to which the values have to be copied.
3834 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3835 template <typename GR, typename From, typename To>
3836 void mapCopy(const GR& gr, const From& from, To& to) {
3837 typedef typename To::Key Item;
3838 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3840 for (ItemIt it(gr); it != INVALID; ++it) {
3841 to.set(it, from[it]);
3845 /// \brief Compare two graph maps.
3847 /// This function compares the values of two graph maps. It returns
3848 /// \c true if the maps assign the same value for all items in the graph.
3849 /// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
3850 /// and their \c Value types must be comparable using \c %operator==().
3852 /// \param gr The graph for which the maps are defined.
3853 /// \param map1 The first map.
3854 /// \param map2 The second map.
3855 template <typename GR, typename Map1, typename Map2>
3856 bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
3857 typedef typename Map2::Key Item;
3858 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3860 for (ItemIt it(gr); it != INVALID; ++it) {
3861 if (!(map1[it] == map2[it])) return false;
3866 /// \brief Return an item having minimum value of a graph map.
3868 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3869 /// minimum value of the given graph map.
3870 /// If the item set is empty, it returns \c INVALID.
3872 /// \param gr The graph for which the map is defined.
3873 /// \param map The graph map.
3874 template <typename GR, typename Map>
3875 typename Map::Key mapMin(const GR& gr, const Map& map) {
3876 return mapMin(gr, map, std::less<typename Map::Value>());
3879 /// \brief Return an item having minimum value of a graph map.
3881 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3882 /// minimum value of the given graph map.
3883 /// If the item set is empty, it returns \c INVALID.
3885 /// \param gr The graph for which the map is defined.
3886 /// \param map The graph map.
3887 /// \param comp Comparison function object.
3888 template <typename GR, typename Map, typename Comp>
3889 typename Map::Key mapMin(const GR& gr, const Map& map, const Comp& comp) {
3890 typedef typename Map::Key Item;
3891 typedef typename Map::Value Value;
3892 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3894 ItemIt min_item(gr);
3895 if (min_item == INVALID) return INVALID;
3896 Value min = map[min_item];
3897 for (ItemIt it(gr); it != INVALID; ++it) {
3898 if (comp(map[it], min)) {
3906 /// \brief Return an item having maximum value of a graph map.
3908 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3909 /// maximum value of the given graph map.
3910 /// If the item set is empty, it returns \c INVALID.
3912 /// \param gr The graph for which the map is defined.
3913 /// \param map The graph map.
3914 template <typename GR, typename Map>
3915 typename Map::Key mapMax(const GR& gr, const Map& map) {
3916 return mapMax(gr, map, std::less<typename Map::Value>());
3919 /// \brief Return an item having maximum value of a graph map.
3921 /// This function returns an item (\c Node, \c Arc or \c Edge) having
3922 /// maximum value of the given graph map.
3923 /// If the item set is empty, it returns \c INVALID.
3925 /// \param gr The graph for which the map is defined.
3926 /// \param map The graph map.
3927 /// \param comp Comparison function object.
3928 template <typename GR, typename Map, typename Comp>
3929 typename Map::Key mapMax(const GR& gr, const Map& map, const Comp& comp) {
3930 typedef typename Map::Key Item;
3931 typedef typename Map::Value Value;
3932 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3934 ItemIt max_item(gr);
3935 if (max_item == INVALID) return INVALID;
3936 Value max = map[max_item];
3937 for (ItemIt it(gr); it != INVALID; ++it) {
3938 if (comp(max, map[it])) {
3946 /// \brief Return the minimum value of a graph map.
3948 /// This function returns the minimum value of the given graph map.
3949 /// The corresponding item set of the graph must not be empty.
3951 /// \param gr The graph for which the map is defined.
3952 /// \param map The graph map.
3953 template <typename GR, typename Map>
3954 typename Map::Value mapMinValue(const GR& gr, const Map& map) {
3955 return map[mapMin(gr, map, std::less<typename Map::Value>())];
3958 /// \brief Return the minimum value of a graph map.
3960 /// This function returns the minimum value of the given graph map.
3961 /// The corresponding item set of the graph must not be empty.
3963 /// \param gr The graph for which the map is defined.
3964 /// \param map The graph map.
3965 /// \param comp Comparison function object.
3966 template <typename GR, typename Map, typename Comp>
3968 mapMinValue(const GR& gr, const Map& map, const Comp& comp) {
3969 return map[mapMin(gr, map, comp)];
3972 /// \brief Return the maximum value of a graph map.
3974 /// This function returns the maximum value of the given graph map.
3975 /// The corresponding item set of the graph must not be empty.
3977 /// \param gr The graph for which the map is defined.
3978 /// \param map The graph map.
3979 template <typename GR, typename Map>
3980 typename Map::Value mapMaxValue(const GR& gr, const Map& map) {
3981 return map[mapMax(gr, map, std::less<typename Map::Value>())];
3984 /// \brief Return the maximum value of a graph map.
3986 /// This function returns the maximum value of the given graph map.
3987 /// The corresponding item set of the graph must not be empty.
3989 /// \param gr The graph for which the map is defined.
3990 /// \param map The graph map.
3991 /// \param comp Comparison function object.
3992 template <typename GR, typename Map, typename Comp>
3994 mapMaxValue(const GR& gr, const Map& map, const Comp& comp) {
3995 return map[mapMax(gr, map, comp)];
3998 /// \brief Return an item having a specified value in a graph map.
4000 /// This function returns an item (\c Node, \c Arc or \c Edge) having
4001 /// the specified assigned value in the given graph map.
4002 /// If no such item exists, it returns \c INVALID.
4004 /// \param gr The graph for which the map is defined.
4005 /// \param map The graph map.
4006 /// \param val The value that have to be found.
4007 template <typename GR, typename Map>
4009 mapFind(const GR& gr, const Map& map, const typename Map::Value& val) {
4010 typedef typename Map::Key Item;
4011 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4013 for (ItemIt it(gr); it != INVALID; ++it) {
4014 if (map[it] == val) return it;
4019 /// \brief Return an item having value for which a certain predicate is
4020 /// true in a graph map.
4022 /// This function returns an item (\c Node, \c Arc or \c Edge) having
4023 /// such assigned value for which the specified predicate is true
4024 /// in the given graph map.
4025 /// If no such item exists, it returns \c INVALID.
4027 /// \param gr The graph for which the map is defined.
4028 /// \param map The graph map.
4029 /// \param pred The predicate function object.
4030 template <typename GR, typename Map, typename Pred>
4032 mapFindIf(const GR& gr, const Map& map, const Pred& pred) {
4033 typedef typename Map::Key Item;
4034 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4036 for (ItemIt it(gr); it != INVALID; ++it) {
4037 if (pred(map[it])) return it;
4042 /// \brief Return the number of items having a specified value in a
4045 /// This function returns the number of items (\c Node, \c Arc or \c Edge)
4046 /// having the specified assigned value in the given graph map.
4048 /// \param gr The graph for which the map is defined.
4049 /// \param map The graph map.
4050 /// \param val The value that have to be counted.
4051 template <typename GR, typename Map>
4052 int mapCount(const GR& gr, const Map& map, const typename Map::Value& val) {
4053 typedef typename Map::Key Item;
4054 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4057 for (ItemIt it(gr); it != INVALID; ++it) {
4058 if (map[it] == val) ++cnt;
4063 /// \brief Return the number of items having values for which a certain
4064 /// predicate is true in a graph map.
4066 /// This function returns the number of items (\c Node, \c Arc or \c Edge)
4067 /// having such assigned values for which the specified predicate is true
4068 /// in the given graph map.
4070 /// \param gr The graph for which the map is defined.
4071 /// \param map The graph map.
4072 /// \param pred The predicate function object.
4073 template <typename GR, typename Map, typename Pred>
4074 int mapCountIf(const GR& gr, const Map& map, const Pred& pred) {
4075 typedef typename Map::Key Item;
4076 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4079 for (ItemIt it(gr); it != INVALID; ++it) {
4080 if (pred(map[it])) ++cnt;
4085 /// \brief Fill a graph map with a certain value.
4087 /// This function sets the specified value for all items (\c Node,
4088 /// \c Arc or \c Edge) in the given graph map.
4090 /// \param gr The graph for which the map is defined.
4091 /// \param map The graph map. It must conform to the
4092 /// \ref concepts::WriteMap "WriteMap" concept.
4093 /// \param val The value.
4094 template <typename GR, typename Map>
4095 void mapFill(const GR& gr, Map& map, const typename Map::Value& val) {
4096 typedef typename Map::Key Item;
4097 typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4099 for (ItemIt it(gr); it != INVALID; ++it) {
4107 #endif // LEMON_MAPS_H