1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
26 #include <lemon/core.h>
27 #include <lemon/smart_graph.h>
31 ///\brief Miscellaneous property maps
40 /// Base class of maps.
42 /// Base class of maps. It provides the necessary type definitions
43 /// required by the map %concepts.
44 template<typename K, typename V>
47 /// \brief The key type of the map.
49 /// \brief The value type of the map.
50 /// (The type of objects associated with the keys).
55 /// Null map. (a.k.a. DoNothingMap)
57 /// This map can be used if you have to provide a map only for
58 /// its type definitions, or if you have to provide a writable map,
59 /// but data written to it is not required (i.e. it will be sent to
60 /// <tt>/dev/null</tt>).
61 /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
64 template<typename K, typename V>
65 class NullMap : public MapBase<K, V> {
72 /// Gives back a default constructed element.
73 Value operator[](const Key&) const { return Value(); }
74 /// Absorbs the value.
75 void set(const Key&, const Value&) {}
78 /// Returns a \c NullMap class
80 /// This function just returns a \c NullMap class.
82 template <typename K, typename V>
83 NullMap<K, V> nullMap() {
84 return NullMap<K, V>();
90 /// This \ref concepts::ReadMap "readable map" assigns a specified
91 /// value to each key.
93 /// In other aspects it is equivalent to \c NullMap.
94 /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
95 /// concept, but it absorbs the data written to it.
97 /// The simplest way of using this map is through the constMap()
102 template<typename K, typename V>
103 class ConstMap : public MapBase<K, V> {
112 /// Default constructor
114 /// Default constructor.
115 /// The value of the map will be default constructed.
118 /// Constructor with specified initial value
120 /// Constructor with specified initial value.
121 /// \param v The initial value of the map.
122 ConstMap(const Value &v) : _value(v) {}
124 /// Gives back the specified value.
125 Value operator[](const Key&) const { return _value; }
127 /// Absorbs the value.
128 void set(const Key&, const Value&) {}
130 /// Sets the value that is assigned to each key.
131 void setAll(const Value &v) {
135 template<typename V1>
136 ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
139 /// Returns a \c ConstMap class
141 /// This function just returns a \c ConstMap class.
142 /// \relates ConstMap
143 template<typename K, typename V>
144 inline ConstMap<K, V> constMap(const V &v) {
145 return ConstMap<K, V>(v);
148 template<typename K, typename V>
149 inline ConstMap<K, V> constMap() {
150 return ConstMap<K, V>();
154 template<typename T, T v>
157 /// Constant map with inlined constant value.
159 /// This \ref concepts::ReadMap "readable map" assigns a specified
160 /// value to each key.
162 /// In other aspects it is equivalent to \c NullMap.
163 /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
164 /// concept, but it absorbs the data written to it.
166 /// The simplest way of using this map is through the constMap()
171 template<typename K, typename V, V v>
172 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
182 /// Gives back the specified value.
183 Value operator[](const Key&) const { return v; }
185 /// Absorbs the value.
186 void set(const Key&, const Value&) {}
189 /// Returns a \c ConstMap class with inlined constant value
191 /// This function just returns a \c ConstMap class with inlined
193 /// \relates ConstMap
194 template<typename K, typename V, V v>
195 inline ConstMap<K, Const<V, v> > constMap() {
196 return ConstMap<K, Const<V, v> >();
202 /// This \ref concepts::ReadMap "read-only map" gives back the given
203 /// key as value without any modification.
206 template <typename T>
207 class IdentityMap : public MapBase<T, T> {
214 /// Gives back the given value without any modification.
215 Value operator[](const Key &k) const {
220 /// Returns an \c IdentityMap class
222 /// This function just returns an \c IdentityMap class.
223 /// \relates IdentityMap
225 inline IdentityMap<T> identityMap() {
226 return IdentityMap<T>();
230 /// \brief Map for storing values for integer keys from the range
231 /// <tt>[0..size-1]</tt>.
233 /// This map is essentially a wrapper for \c std::vector. It assigns
234 /// values to integer keys from the range <tt>[0..size-1]</tt>.
235 /// It can be used with some data structures, for example
236 /// \c UnionFind, \c BinHeap, when the used items are small
237 /// integers. This map conforms the \ref concepts::ReferenceMap
238 /// "ReferenceMap" concept.
240 /// The simplest way of using this map is through the rangeMap()
242 template <typename V>
243 class RangeMap : public MapBase<int, V> {
244 template <typename V1>
245 friend class RangeMap;
248 typedef std::vector<V> Vector;
258 typedef typename Vector::reference Reference;
259 /// Const reference type
260 typedef typename Vector::const_reference ConstReference;
262 typedef True ReferenceMapTag;
266 /// Constructor with specified default value.
267 RangeMap(int size = 0, const Value &value = Value())
268 : _vector(size, value) {}
270 /// Constructs the map from an appropriate \c std::vector.
271 template <typename V1>
272 RangeMap(const std::vector<V1>& vector)
273 : _vector(vector.begin(), vector.end()) {}
275 /// Constructs the map from another \c RangeMap.
276 template <typename V1>
277 RangeMap(const RangeMap<V1> &c)
278 : _vector(c._vector.begin(), c._vector.end()) {}
280 /// Returns the size of the map.
282 return _vector.size();
287 /// Resizes the underlying \c std::vector container, so changes the
288 /// keyset of the map.
289 /// \param size The new size of the map. The new keyset will be the
290 /// range <tt>[0..size-1]</tt>.
291 /// \param value The default value to assign to the new keys.
292 void resize(int size, const Value &value = Value()) {
293 _vector.resize(size, value);
298 RangeMap& operator=(const RangeMap&);
303 Reference operator[](const Key &k) {
308 ConstReference operator[](const Key &k) const {
313 void set(const Key &k, const Value &v) {
318 /// Returns a \c RangeMap class
320 /// This function just returns a \c RangeMap class.
321 /// \relates RangeMap
323 inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
324 return RangeMap<V>(size, value);
327 /// \brief Returns a \c RangeMap class created from an appropriate
330 /// This function just returns a \c RangeMap class created from an
331 /// appropriate \c std::vector.
332 /// \relates RangeMap
334 inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
335 return RangeMap<V>(vector);
339 /// Map type based on \c std::map
341 /// This map is essentially a wrapper for \c std::map with addition
342 /// that you can specify a default value for the keys that are not
343 /// stored actually. This value can be different from the default
344 /// contructed value (i.e. \c %Value()).
345 /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
348 /// This map is useful if a default value should be assigned to most of
349 /// the keys and different values should be assigned only to a few
350 /// keys (i.e. the map is "sparse").
351 /// The name of this type also refers to this important usage.
353 /// Apart form that this map can be used in many other cases since it
354 /// is based on \c std::map, which is a general associative container.
355 /// However keep in mind that it is usually not as efficient as other
358 /// The simplest way of using this map is through the sparseMap()
360 template <typename K, typename V, typename Comp = std::less<K> >
361 class SparseMap : public MapBase<K, V> {
362 template <typename K1, typename V1, typename C1>
363 friend class SparseMap;
371 typedef Value& Reference;
372 /// Const reference type
373 typedef const Value& ConstReference;
375 typedef True ReferenceMapTag;
379 typedef std::map<K, V, Comp> Map;
385 /// \brief Constructor with specified default value.
386 SparseMap(const Value &value = Value()) : _value(value) {}
387 /// \brief Constructs the map from an appropriate \c std::map, and
388 /// explicitly specifies a default value.
389 template <typename V1, typename Comp1>
390 SparseMap(const std::map<Key, V1, Comp1> &map,
391 const Value &value = Value())
392 : _map(map.begin(), map.end()), _value(value) {}
394 /// \brief Constructs the map from another \c SparseMap.
395 template<typename V1, typename Comp1>
396 SparseMap(const SparseMap<Key, V1, Comp1> &c)
397 : _map(c._map.begin(), c._map.end()), _value(c._value) {}
401 SparseMap& operator=(const SparseMap&);
406 Reference operator[](const Key &k) {
407 typename Map::iterator it = _map.lower_bound(k);
408 if (it != _map.end() && !_map.key_comp()(k, it->first))
411 return _map.insert(it, std::make_pair(k, _value))->second;
415 ConstReference operator[](const Key &k) const {
416 typename Map::const_iterator it = _map.find(k);
417 if (it != _map.end())
424 void set(const Key &k, const Value &v) {
425 typename Map::iterator it = _map.lower_bound(k);
426 if (it != _map.end() && !_map.key_comp()(k, it->first))
429 _map.insert(it, std::make_pair(k, v));
433 void setAll(const Value &v) {
439 /// Returns a \c SparseMap class
441 /// This function just returns a \c SparseMap class with specified
443 /// \relates SparseMap
444 template<typename K, typename V, typename Compare>
445 inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
446 return SparseMap<K, V, Compare>(value);
449 template<typename K, typename V>
450 inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
451 return SparseMap<K, V, std::less<K> >(value);
454 /// \brief Returns a \c SparseMap class created from an appropriate
457 /// This function just returns a \c SparseMap class created from an
458 /// appropriate \c std::map.
459 /// \relates SparseMap
460 template<typename K, typename V, typename Compare>
461 inline SparseMap<K, V, Compare>
462 sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
464 return SparseMap<K, V, Compare>(map, value);
469 /// \addtogroup map_adaptors
472 /// Composition of two maps
474 /// This \ref concepts::ReadMap "read-only map" returns the
475 /// composition of two given maps. That is to say, if \c m1 is of
476 /// type \c M1 and \c m2 is of \c M2, then for
478 /// ComposeMap<M1, M2> cm(m1,m2);
480 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
482 /// The \c Key type of the map is inherited from \c M2 and the
483 /// \c Value type is from \c M1.
484 /// \c M2::Value must be convertible to \c M1::Key.
486 /// The simplest way of using this map is through the composeMap()
490 template <typename M1, typename M2>
491 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
496 typedef typename M2::Key Key;
498 typedef typename M1::Value Value;
501 ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
504 typename MapTraits<M1>::ConstReturnValue
505 operator[](const Key &k) const { return _m1[_m2[k]]; }
508 /// Returns a \c ComposeMap class
510 /// This function just returns a \c ComposeMap class.
512 /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
513 /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
514 /// will be equal to <tt>m1[m2[x]]</tt>.
516 /// \relates ComposeMap
517 template <typename M1, typename M2>
518 inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
519 return ComposeMap<M1, M2>(m1, m2);
523 /// Combination of two maps using an STL (binary) functor.
525 /// This \ref concepts::ReadMap "read-only map" takes two maps and a
526 /// binary functor and returns the combination of the two given maps
527 /// using the functor.
528 /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
529 /// and \c f is of \c F, then for
531 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
533 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
535 /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
536 /// must be convertible to \c M2::Key) and the \c Value type is \c V.
537 /// \c M2::Value and \c M1::Value must be convertible to the
538 /// corresponding input parameter of \c F and the return type of \c F
539 /// must be convertible to \c V.
541 /// The simplest way of using this map is through the combineMap()
545 template<typename M1, typename M2, typename F,
546 typename V = typename F::result_type>
547 class CombineMap : public MapBase<typename M1::Key, V> {
553 typedef typename M1::Key Key;
558 CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
559 : _m1(m1), _m2(m2), _f(f) {}
561 Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
564 /// Returns a \c CombineMap class
566 /// This function just returns a \c CombineMap class.
568 /// For example, if \c m1 and \c m2 are both maps with \c double
571 /// combineMap(m1,m2,std::plus<double>())
578 /// This function is specialized for adaptable binary function
579 /// classes and C++ functions.
581 /// \relates CombineMap
582 template<typename M1, typename M2, typename F, typename V>
583 inline CombineMap<M1, M2, F, V>
584 combineMap(const M1 &m1, const M2 &m2, const F &f) {
585 return CombineMap<M1, M2, F, V>(m1,m2,f);
588 template<typename M1, typename M2, typename F>
589 inline CombineMap<M1, M2, F, typename F::result_type>
590 combineMap(const M1 &m1, const M2 &m2, const F &f) {
591 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
594 template<typename M1, typename M2, typename K1, typename K2, typename V>
595 inline CombineMap<M1, M2, V (*)(K1, K2), V>
596 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
597 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
601 /// Converts an STL style (unary) functor to a map
603 /// This \ref concepts::ReadMap "read-only map" returns the value
604 /// of a given functor. Actually, it just wraps the functor and
605 /// provides the \c Key and \c Value typedefs.
607 /// Template parameters \c K and \c V will become its \c Key and
608 /// \c Value. In most cases they have to be given explicitly because
609 /// a functor typically does not provide \c argument_type and
610 /// \c result_type typedefs.
611 /// Parameter \c F is the type of the used functor.
613 /// The simplest way of using this map is through the functorToMap()
618 typename K = typename F::argument_type,
619 typename V = typename F::result_type>
620 class FunctorToMap : public MapBase<K, V> {
629 FunctorToMap(const F &f = F()) : _f(f) {}
631 Value operator[](const Key &k) const { return _f(k); }
634 /// Returns a \c FunctorToMap class
636 /// This function just returns a \c FunctorToMap class.
638 /// This function is specialized for adaptable binary function
639 /// classes and C++ functions.
641 /// \relates FunctorToMap
642 template<typename K, typename V, typename F>
643 inline FunctorToMap<F, K, V> functorToMap(const F &f) {
644 return FunctorToMap<F, K, V>(f);
647 template <typename F>
648 inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
649 functorToMap(const F &f)
651 return FunctorToMap<F, typename F::argument_type,
652 typename F::result_type>(f);
655 template <typename K, typename V>
656 inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
657 return FunctorToMap<V (*)(K), K, V>(f);
661 /// Converts a map to an STL style (unary) functor
663 /// This class converts a map to an STL style (unary) functor.
664 /// That is it provides an <tt>operator()</tt> to read its values.
666 /// For the sake of convenience it also works as a usual
667 /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
668 /// and the \c Key and \c Value typedefs also exist.
670 /// The simplest way of using this map is through the mapToFunctor()
674 template <typename M>
675 class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
679 typedef typename M::Key Key;
681 typedef typename M::Value Value;
683 typedef typename M::Key argument_type;
684 typedef typename M::Value result_type;
687 MapToFunctor(const M &m) : _m(m) {}
689 Value operator()(const Key &k) const { return _m[k]; }
691 Value operator[](const Key &k) const { return _m[k]; }
694 /// Returns a \c MapToFunctor class
696 /// This function just returns a \c MapToFunctor class.
697 /// \relates MapToFunctor
699 inline MapToFunctor<M> mapToFunctor(const M &m) {
700 return MapToFunctor<M>(m);
704 /// \brief Map adaptor to convert the \c Value type of a map to
705 /// another type using the default conversion.
707 /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
708 /// "readable map" to another type using the default conversion.
709 /// The \c Key type of it is inherited from \c M and the \c Value
711 /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
713 /// The simplest way of using this map is through the convertMap()
715 template <typename M, typename V>
716 class ConvertMap : public MapBase<typename M::Key, V> {
720 typedef typename M::Key Key;
727 /// \param m The underlying map.
728 ConvertMap(const M &m) : _m(m) {}
731 Value operator[](const Key &k) const { return _m[k]; }
734 /// Returns a \c ConvertMap class
736 /// This function just returns a \c ConvertMap class.
737 /// \relates ConvertMap
738 template<typename V, typename M>
739 inline ConvertMap<M, V> convertMap(const M &map) {
740 return ConvertMap<M, V>(map);
744 /// Applies all map setting operations to two maps
746 /// This map has two \ref concepts::WriteMap "writable map" parameters
747 /// and each write request will be passed to both of them.
748 /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
749 /// operations will return the corresponding values of \c M1.
751 /// The \c Key and \c Value types are inherited from \c M1.
752 /// The \c Key and \c Value of \c M2 must be convertible from those
755 /// The simplest way of using this map is through the forkMap()
757 template<typename M1, typename M2>
758 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
763 typedef typename M1::Key Key;
765 typedef typename M1::Value Value;
768 ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
769 /// Returns the value associated with the given key in the first map.
770 Value operator[](const Key &k) const { return _m1[k]; }
771 /// Sets the value associated with the given key in both maps.
772 void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
775 /// Returns a \c ForkMap class
777 /// This function just returns a \c ForkMap class.
779 template <typename M1, typename M2>
780 inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
781 return ForkMap<M1,M2>(m1,m2);
787 /// This \ref concepts::ReadMap "read-only map" returns the sum
788 /// of the values of the two given maps.
789 /// Its \c Key and \c Value types are inherited from \c M1.
790 /// The \c Key and \c Value of \c M2 must be convertible to those of
793 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
795 /// AddMap<M1,M2> am(m1,m2);
797 /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
799 /// The simplest way of using this map is through the addMap()
802 /// \sa SubMap, MulMap, DivMap
803 /// \sa ShiftMap, ShiftWriteMap
804 template<typename M1, typename M2>
805 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
810 typedef typename M1::Key Key;
812 typedef typename M1::Value Value;
815 AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
817 Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
820 /// Returns an \c AddMap class
822 /// This function just returns an \c AddMap class.
824 /// For example, if \c m1 and \c m2 are both maps with \c double
825 /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
826 /// <tt>m1[x]+m2[x]</tt>.
829 template<typename M1, typename M2>
830 inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
831 return AddMap<M1, M2>(m1,m2);
835 /// Difference of two maps
837 /// This \ref concepts::ReadMap "read-only map" returns the difference
838 /// of the values of the two given maps.
839 /// Its \c Key and \c Value types are inherited from \c M1.
840 /// The \c Key and \c Value of \c M2 must be convertible to those of
843 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
845 /// SubMap<M1,M2> sm(m1,m2);
847 /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
849 /// The simplest way of using this map is through the subMap()
852 /// \sa AddMap, MulMap, DivMap
853 template<typename M1, typename M2>
854 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
859 typedef typename M1::Key Key;
861 typedef typename M1::Value Value;
864 SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
866 Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
869 /// Returns a \c SubMap class
871 /// This function just returns a \c SubMap class.
873 /// For example, if \c m1 and \c m2 are both maps with \c double
874 /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
875 /// <tt>m1[x]-m2[x]</tt>.
878 template<typename M1, typename M2>
879 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
880 return SubMap<M1, M2>(m1,m2);
884 /// Product of two maps
886 /// This \ref concepts::ReadMap "read-only map" returns the product
887 /// of the values of the two given maps.
888 /// Its \c Key and \c Value types are inherited from \c M1.
889 /// The \c Key and \c Value of \c M2 must be convertible to those of
892 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
894 /// MulMap<M1,M2> mm(m1,m2);
896 /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
898 /// The simplest way of using this map is through the mulMap()
901 /// \sa AddMap, SubMap, DivMap
902 /// \sa ScaleMap, ScaleWriteMap
903 template<typename M1, typename M2>
904 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
909 typedef typename M1::Key Key;
911 typedef typename M1::Value Value;
914 MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
916 Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
919 /// Returns a \c MulMap class
921 /// This function just returns a \c MulMap class.
923 /// For example, if \c m1 and \c m2 are both maps with \c double
924 /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
925 /// <tt>m1[x]*m2[x]</tt>.
928 template<typename M1, typename M2>
929 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
930 return MulMap<M1, M2>(m1,m2);
934 /// Quotient of two maps
936 /// This \ref concepts::ReadMap "read-only map" returns the quotient
937 /// of the values of the two given maps.
938 /// Its \c Key and \c Value types are inherited from \c M1.
939 /// The \c Key and \c Value of \c M2 must be convertible to those of
942 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
944 /// DivMap<M1,M2> dm(m1,m2);
946 /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
948 /// The simplest way of using this map is through the divMap()
951 /// \sa AddMap, SubMap, MulMap
952 template<typename M1, typename M2>
953 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
958 typedef typename M1::Key Key;
960 typedef typename M1::Value Value;
963 DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
965 Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
968 /// Returns a \c DivMap class
970 /// This function just returns a \c DivMap class.
972 /// For example, if \c m1 and \c m2 are both maps with \c double
973 /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
974 /// <tt>m1[x]/m2[x]</tt>.
977 template<typename M1, typename M2>
978 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
979 return DivMap<M1, M2>(m1,m2);
983 /// Shifts a map with a constant.
985 /// This \ref concepts::ReadMap "read-only map" returns the sum of
986 /// the given map and a constant value (i.e. it shifts the map with
987 /// the constant). Its \c Key and \c Value are inherited from \c M.
991 /// ShiftMap<M> sh(m,v);
995 /// ConstMap<M::Key, M::Value> cm(v);
996 /// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
999 /// The simplest way of using this map is through the shiftMap()
1002 /// \sa ShiftWriteMap
1003 template<typename M, typename C = typename M::Value>
1004 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
1009 typedef typename M::Key Key;
1011 typedef typename M::Value Value;
1016 /// \param m The undelying map.
1017 /// \param v The constant value.
1018 ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1020 Value operator[](const Key &k) const { return _m[k]+_v; }
1023 /// Shifts a map with a constant (read-write version).
1025 /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1026 /// of the given map and a constant value (i.e. it shifts the map with
1027 /// the constant). Its \c Key and \c Value are inherited from \c M.
1028 /// It makes also possible to write the map.
1030 /// The simplest way of using this map is through the shiftWriteMap()
1034 template<typename M, typename C = typename M::Value>
1035 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1040 typedef typename M::Key Key;
1042 typedef typename M::Value Value;
1047 /// \param m The undelying map.
1048 /// \param v The constant value.
1049 ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1051 Value operator[](const Key &k) const { return _m[k]+_v; }
1053 void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1056 /// Returns a \c ShiftMap class
1058 /// This function just returns a \c ShiftMap class.
1060 /// For example, if \c m is a map with \c double values and \c v is
1061 /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1062 /// <tt>m[x]+v</tt>.
1064 /// \relates ShiftMap
1065 template<typename M, typename C>
1066 inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1067 return ShiftMap<M, C>(m,v);
1070 /// Returns a \c ShiftWriteMap class
1072 /// This function just returns a \c ShiftWriteMap class.
1074 /// For example, if \c m is a map with \c double values and \c v is
1075 /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1076 /// <tt>m[x]+v</tt>.
1077 /// Moreover it makes also possible to write the map.
1079 /// \relates ShiftWriteMap
1080 template<typename M, typename C>
1081 inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1082 return ShiftWriteMap<M, C>(m,v);
1086 /// Scales a map with a constant.
1088 /// This \ref concepts::ReadMap "read-only map" returns the value of
1089 /// the given map multiplied from the left side with a constant value.
1090 /// Its \c Key and \c Value are inherited from \c M.
1094 /// ScaleMap<M> sc(m,v);
1096 /// is equivalent to
1098 /// ConstMap<M::Key, M::Value> cm(v);
1099 /// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1102 /// The simplest way of using this map is through the scaleMap()
1105 /// \sa ScaleWriteMap
1106 template<typename M, typename C = typename M::Value>
1107 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1112 typedef typename M::Key Key;
1114 typedef typename M::Value Value;
1119 /// \param m The undelying map.
1120 /// \param v The constant value.
1121 ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1123 Value operator[](const Key &k) const { return _v*_m[k]; }
1126 /// Scales a map with a constant (read-write version).
1128 /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1129 /// the given map multiplied from the left side with a constant value.
1130 /// Its \c Key and \c Value are inherited from \c M.
1131 /// It can also be used as write map if the \c / operator is defined
1132 /// between \c Value and \c C and the given multiplier is not zero.
1134 /// The simplest way of using this map is through the scaleWriteMap()
1138 template<typename M, typename C = typename M::Value>
1139 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1144 typedef typename M::Key Key;
1146 typedef typename M::Value Value;
1151 /// \param m The undelying map.
1152 /// \param v The constant value.
1153 ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1155 Value operator[](const Key &k) const { return _v*_m[k]; }
1157 void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1160 /// Returns a \c ScaleMap class
1162 /// This function just returns a \c ScaleMap class.
1164 /// For example, if \c m is a map with \c double values and \c v is
1165 /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1166 /// <tt>v*m[x]</tt>.
1168 /// \relates ScaleMap
1169 template<typename M, typename C>
1170 inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1171 return ScaleMap<M, C>(m,v);
1174 /// Returns a \c ScaleWriteMap class
1176 /// This function just returns a \c ScaleWriteMap class.
1178 /// For example, if \c m is a map with \c double values and \c v is
1179 /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1180 /// <tt>v*m[x]</tt>.
1181 /// Moreover it makes also possible to write the map.
1183 /// \relates ScaleWriteMap
1184 template<typename M, typename C>
1185 inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1186 return ScaleWriteMap<M, C>(m,v);
1190 /// Negative of a map
1192 /// This \ref concepts::ReadMap "read-only map" returns the negative
1193 /// of the values of the given map (using the unary \c - operator).
1194 /// Its \c Key and \c Value are inherited from \c M.
1196 /// If M::Value is \c int, \c double etc., then
1198 /// NegMap<M> neg(m);
1200 /// is equivalent to
1202 /// ScaleMap<M> neg(m,-1);
1205 /// The simplest way of using this map is through the negMap()
1209 template<typename M>
1210 class NegMap : public MapBase<typename M::Key, typename M::Value> {
1214 typedef typename M::Key Key;
1216 typedef typename M::Value Value;
1219 NegMap(const M &m) : _m(m) {}
1221 Value operator[](const Key &k) const { return -_m[k]; }
1224 /// Negative of a map (read-write version)
1226 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1227 /// negative of the values of the given map (using the unary \c -
1229 /// Its \c Key and \c Value are inherited from \c M.
1230 /// It makes also possible to write the map.
1232 /// If M::Value is \c int, \c double etc., then
1234 /// NegWriteMap<M> neg(m);
1236 /// is equivalent to
1238 /// ScaleWriteMap<M> neg(m,-1);
1241 /// The simplest way of using this map is through the negWriteMap()
1245 template<typename M>
1246 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1250 typedef typename M::Key Key;
1252 typedef typename M::Value Value;
1255 NegWriteMap(M &m) : _m(m) {}
1257 Value operator[](const Key &k) const { return -_m[k]; }
1259 void set(const Key &k, const Value &v) { _m.set(k, -v); }
1262 /// Returns a \c NegMap class
1264 /// This function just returns a \c NegMap class.
1266 /// For example, if \c m is a map with \c double values, then
1267 /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1270 template <typename M>
1271 inline NegMap<M> negMap(const M &m) {
1272 return NegMap<M>(m);
1275 /// Returns a \c NegWriteMap class
1277 /// This function just returns a \c NegWriteMap class.
1279 /// For example, if \c m is a map with \c double values, then
1280 /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1281 /// Moreover it makes also possible to write the map.
1283 /// \relates NegWriteMap
1284 template <typename M>
1285 inline NegWriteMap<M> negWriteMap(M &m) {
1286 return NegWriteMap<M>(m);
1290 /// Absolute value of a map
1292 /// This \ref concepts::ReadMap "read-only map" returns the absolute
1293 /// value of the values of the given map.
1294 /// Its \c Key and \c Value are inherited from \c M.
1295 /// \c Value must be comparable to \c 0 and the unary \c -
1296 /// operator must be defined for it, of course.
1298 /// The simplest way of using this map is through the absMap()
1300 template<typename M>
1301 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1305 typedef typename M::Key Key;
1307 typedef typename M::Value Value;
1310 AbsMap(const M &m) : _m(m) {}
1312 Value operator[](const Key &k) const {
1314 return tmp >= 0 ? tmp : -tmp;
1319 /// Returns an \c AbsMap class
1321 /// This function just returns an \c AbsMap class.
1323 /// For example, if \c m is a map with \c double values, then
1324 /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1325 /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1329 template<typename M>
1330 inline AbsMap<M> absMap(const M &m) {
1331 return AbsMap<M>(m);
1336 // Logical maps and map adaptors:
1338 /// \addtogroup maps
1341 /// Constant \c true map.
1343 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1350 /// is equivalent to
1352 /// ConstMap<K,bool> tm(true);
1357 template <typename K>
1358 class TrueMap : public MapBase<K, bool> {
1365 /// Gives back \c true.
1366 Value operator[](const Key&) const { return true; }
1369 /// Returns a \c TrueMap class
1371 /// This function just returns a \c TrueMap class.
1372 /// \relates TrueMap
1373 template<typename K>
1374 inline TrueMap<K> trueMap() {
1375 return TrueMap<K>();
1379 /// Constant \c false map.
1381 /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1388 /// is equivalent to
1390 /// ConstMap<K,bool> fm(false);
1395 template <typename K>
1396 class FalseMap : public MapBase<K, bool> {
1403 /// Gives back \c false.
1404 Value operator[](const Key&) const { return false; }
1407 /// Returns a \c FalseMap class
1409 /// This function just returns a \c FalseMap class.
1410 /// \relates FalseMap
1411 template<typename K>
1412 inline FalseMap<K> falseMap() {
1413 return FalseMap<K>();
1418 /// \addtogroup map_adaptors
1421 /// Logical 'and' of two maps
1423 /// This \ref concepts::ReadMap "read-only map" returns the logical
1424 /// 'and' of the values of the two given maps.
1425 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1426 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1428 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1430 /// AndMap<M1,M2> am(m1,m2);
1432 /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1434 /// The simplest way of using this map is through the andMap()
1438 /// \sa NotMap, NotWriteMap
1439 template<typename M1, typename M2>
1440 class AndMap : public MapBase<typename M1::Key, bool> {
1445 typedef typename M1::Key Key;
1450 AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1452 Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1455 /// Returns an \c AndMap class
1457 /// This function just returns an \c AndMap class.
1459 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1460 /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1461 /// <tt>m1[x]&&m2[x]</tt>.
1464 template<typename M1, typename M2>
1465 inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1466 return AndMap<M1, M2>(m1,m2);
1470 /// Logical 'or' of two maps
1472 /// This \ref concepts::ReadMap "read-only map" returns the logical
1473 /// 'or' of the values of the two given maps.
1474 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1475 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1477 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1479 /// OrMap<M1,M2> om(m1,m2);
1481 /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1483 /// The simplest way of using this map is through the orMap()
1487 /// \sa NotMap, NotWriteMap
1488 template<typename M1, typename M2>
1489 class OrMap : public MapBase<typename M1::Key, bool> {
1494 typedef typename M1::Key Key;
1499 OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1501 Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1504 /// Returns an \c OrMap class
1506 /// This function just returns an \c OrMap class.
1508 /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1509 /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1510 /// <tt>m1[x]||m2[x]</tt>.
1513 template<typename M1, typename M2>
1514 inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1515 return OrMap<M1, M2>(m1,m2);
1519 /// Logical 'not' of a map
1521 /// This \ref concepts::ReadMap "read-only map" returns the logical
1522 /// negation of the values of the given map.
1523 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1525 /// The simplest way of using this map is through the notMap()
1529 template <typename M>
1530 class NotMap : public MapBase<typename M::Key, bool> {
1534 typedef typename M::Key Key;
1539 NotMap(const M &m) : _m(m) {}
1541 Value operator[](const Key &k) const { return !_m[k]; }
1544 /// Logical 'not' of a map (read-write version)
1546 /// This \ref concepts::ReadWriteMap "read-write map" returns the
1547 /// logical negation of the values of the given map.
1548 /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1549 /// It makes also possible to write the map. When a value is set,
1550 /// the opposite value is set to the original map.
1552 /// The simplest way of using this map is through the notWriteMap()
1556 template <typename M>
1557 class NotWriteMap : public MapBase<typename M::Key, bool> {
1561 typedef typename M::Key Key;
1566 NotWriteMap(M &m) : _m(m) {}
1568 Value operator[](const Key &k) const { return !_m[k]; }
1570 void set(const Key &k, bool v) { _m.set(k, !v); }
1573 /// Returns a \c NotMap class
1575 /// This function just returns a \c NotMap class.
1577 /// For example, if \c m is a map with \c bool values, then
1578 /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1581 template <typename M>
1582 inline NotMap<M> notMap(const M &m) {
1583 return NotMap<M>(m);
1586 /// Returns a \c NotWriteMap class
1588 /// This function just returns a \c NotWriteMap class.
1590 /// For example, if \c m is a map with \c bool values, then
1591 /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1592 /// Moreover it makes also possible to write the map.
1594 /// \relates NotWriteMap
1595 template <typename M>
1596 inline NotWriteMap<M> notWriteMap(M &m) {
1597 return NotWriteMap<M>(m);
1601 /// Combination of two maps using the \c == operator
1603 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1604 /// the keys for which the corresponding values of the two maps are
1606 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1607 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1609 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1611 /// EqualMap<M1,M2> em(m1,m2);
1613 /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1615 /// The simplest way of using this map is through the equalMap()
1619 template<typename M1, typename M2>
1620 class EqualMap : public MapBase<typename M1::Key, bool> {
1625 typedef typename M1::Key Key;
1630 EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1632 Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1635 /// Returns an \c EqualMap class
1637 /// This function just returns an \c EqualMap class.
1639 /// For example, if \c m1 and \c m2 are maps with keys and values of
1640 /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1641 /// <tt>m1[x]==m2[x]</tt>.
1643 /// \relates EqualMap
1644 template<typename M1, typename M2>
1645 inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1646 return EqualMap<M1, M2>(m1,m2);
1650 /// Combination of two maps using the \c < operator
1652 /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1653 /// the keys for which the corresponding value of the first map is
1654 /// less then the value of the second map.
1655 /// Its \c Key type is inherited from \c M1 and its \c Value type is
1656 /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1658 /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1660 /// LessMap<M1,M2> lm(m1,m2);
1662 /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1664 /// The simplest way of using this map is through the lessMap()
1668 template<typename M1, typename M2>
1669 class LessMap : public MapBase<typename M1::Key, bool> {
1674 typedef typename M1::Key Key;
1679 LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1681 Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1684 /// Returns an \c LessMap class
1686 /// This function just returns an \c LessMap class.
1688 /// For example, if \c m1 and \c m2 are maps with keys and values of
1689 /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1690 /// <tt>m1[x]<m2[x]</tt>.
1692 /// \relates LessMap
1693 template<typename M1, typename M2>
1694 inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1695 return LessMap<M1, M2>(m1,m2);
1698 namespace _maps_bits {
1700 template <typename _Iterator, typename Enable = void>
1701 struct IteratorTraits {
1702 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1705 template <typename _Iterator>
1706 struct IteratorTraits<_Iterator,
1707 typename exists<typename _Iterator::container_type>::type>
1709 typedef typename _Iterator::container_type::value_type Value;
1716 /// \addtogroup maps
1719 /// \brief Writable bool map for logging each \c true assigned element
1721 /// A \ref concepts::WriteMap "writable" bool map for logging
1722 /// each \c true assigned element, i.e it copies subsequently each
1723 /// keys set to \c true to the given iterator.
1724 /// The most important usage of it is storing certain nodes or arcs
1725 /// that were marked \c true by an algorithm.
1727 /// There are several algorithms that provide solutions through bool
1728 /// maps and most of them assign \c true at most once for each key.
1729 /// In these cases it is a natural request to store each \c true
1730 /// assigned elements (in order of the assignment), which can be
1731 /// easily done with LoggerBoolMap.
1733 /// The simplest way of using this map is through the loggerBoolMap()
1736 /// \tparam IT The type of the iterator.
1737 /// \tparam KEY The key type of the map. The default value set
1738 /// according to the iterator type should work in most cases.
1740 /// \note The container of the iterator must contain enough space
1741 /// for the elements or the iterator should be an inserter iterator.
1743 template <typename IT, typename KEY>
1745 template <typename IT,
1746 typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1748 class LoggerBoolMap : public MapBase<KEY, bool> {
1756 typedef IT Iterator;
1759 LoggerBoolMap(Iterator it)
1760 : _begin(it), _end(it) {}
1762 /// Gives back the given iterator set for the first key
1763 Iterator begin() const {
1767 /// Gives back the the 'after the last' iterator
1768 Iterator end() const {
1772 /// The set function of the map
1773 void set(const Key& key, Value value) {
1784 /// Returns a \c LoggerBoolMap class
1786 /// This function just returns a \c LoggerBoolMap class.
1788 /// The most important usage of it is storing certain nodes or arcs
1789 /// that were marked \c true by an algorithm.
1790 /// For example it makes easier to store the nodes in the processing
1791 /// order of Dfs algorithm, as the following examples show.
1793 /// std::vector<Node> v;
1794 /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1797 /// std::vector<Node> v(countNodes(g));
1798 /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1801 /// \note The container of the iterator must contain enough space
1802 /// for the elements or the iterator should be an inserter iterator.
1804 /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1805 /// it cannot be used when a readable map is needed, for example as
1806 /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1808 /// \relates LoggerBoolMap
1809 template<typename Iterator>
1810 inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1811 return LoggerBoolMap<Iterator>(it);
1816 /// \addtogroup graph_maps
1819 /// \brief Provides an immutable and unique id for each item in a graph.
1821 /// IdMap provides a unique and immutable id for each item of the
1822 /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1823 /// - \b unique: different items get different ids,
1824 /// - \b immutable: the id of an item does not change (even if you
1825 /// delete other nodes).
1827 /// Using this map you get access (i.e. can read) the inner id values of
1828 /// the items stored in the graph, which is returned by the \c id()
1829 /// function of the graph. This map can be inverted with its member
1830 /// class \c InverseMap or with the \c operator() member.
1832 /// \tparam GR The graph type.
1833 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1837 template <typename GR, typename K>
1838 class IdMap : public MapBase<K, int> {
1840 /// The graph type of IdMap.
1843 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1845 /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1847 /// The value type of IdMap.
1850 /// \brief Constructor.
1852 /// Constructor of the map.
1853 explicit IdMap(const Graph& graph) : _graph(&graph) {}
1855 /// \brief Gives back the \e id of the item.
1857 /// Gives back the immutable and unique \e id of the item.
1858 int operator[](const Item& item) const { return _graph->id(item);}
1860 /// \brief Gives back the \e item by its id.
1862 /// Gives back the \e item by its id.
1863 Item operator()(int id) { return _graph->fromId(id, Item()); }
1866 const Graph* _graph;
1870 /// \brief This class represents the inverse of its owner (IdMap).
1872 /// This class represents the inverse of its owner (IdMap).
1877 /// \brief Constructor.
1879 /// Constructor for creating an id-to-item map.
1880 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1882 /// \brief Constructor.
1884 /// Constructor for creating an id-to-item map.
1885 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1887 /// \brief Gives back the given item from its id.
1889 /// Gives back the given item from its id.
1890 Item operator[](int id) const { return _graph->fromId(id, Item());}
1893 const Graph* _graph;
1896 /// \brief Gives back the inverse of the map.
1898 /// Gives back the inverse of the IdMap.
1899 InverseMap inverse() const { return InverseMap(*_graph);}
1903 /// \brief General cross reference graph map type.
1905 /// This class provides simple invertable graph maps.
1906 /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
1907 /// and if a key is set to a new value then store it
1908 /// in the inverse map.
1910 /// The values of the map can be accessed
1911 /// with stl compatible forward iterator.
1913 /// \tparam GR The graph type.
1914 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1916 /// \tparam V The value type of the map.
1918 /// \see IterableValueMap
1919 template <typename GR, typename K, typename V>
1921 : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1924 typedef typename ItemSetTraits<GR, K>::
1925 template Map<V>::Type Map;
1927 typedef std::map<V, K> Container;
1932 /// The graph type of CrossRefMap.
1935 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1937 /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1939 /// The value type of CrossRefMap.
1942 /// \brief Constructor.
1944 /// Construct a new CrossRefMap for the given graph.
1945 explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1947 /// \brief Forward iterator for values.
1949 /// This iterator is an stl compatible forward
1950 /// iterator on the values of the map. The values can
1951 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1953 : public std::iterator<std::forward_iterator_tag, Value> {
1954 friend class CrossRefMap;
1956 ValueIterator(typename Container::const_iterator _it)
1962 ValueIterator& operator++() { ++it; return *this; }
1963 ValueIterator operator++(int) {
1964 ValueIterator tmp(*this);
1969 const Value& operator*() const { return it->first; }
1970 const Value* operator->() const { return &(it->first); }
1972 bool operator==(ValueIterator jt) const { return it == jt.it; }
1973 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1976 typename Container::const_iterator it;
1979 /// \brief Returns an iterator to the first value.
1981 /// Returns an stl compatible iterator to the
1982 /// first value of the map. The values of the
1983 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1985 ValueIterator beginValue() const {
1986 return ValueIterator(_inv_map.begin());
1989 /// \brief Returns an iterator after the last value.
1991 /// Returns an stl compatible iterator after the
1992 /// last value of the map. The values of the
1993 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1995 ValueIterator endValue() const {
1996 return ValueIterator(_inv_map.end());
1999 /// \brief Sets the value associated with the given key.
2001 /// Sets the value associated with the given key.
2002 void set(const Key& key, const Value& val) {
2003 Value oldval = Map::operator[](key);
2004 typename Container::iterator it = _inv_map.find(oldval);
2005 if (it != _inv_map.end() && it->second == key) {
2008 _inv_map.insert(std::make_pair(val, key));
2012 /// \brief Returns the value associated with the given key.
2014 /// Returns the value associated with the given key.
2015 typename MapTraits<Map>::ConstReturnValue
2016 operator[](const Key& key) const {
2017 return Map::operator[](key);
2020 /// \brief Gives back the item by its value.
2022 /// Gives back the item by its value.
2023 Key operator()(const Value& key) const {
2024 typename Container::const_iterator it = _inv_map.find(key);
2025 return it != _inv_map.end() ? it->second : INVALID;
2030 /// \brief Erase the key from the map and the inverse map.
2032 /// Erase the key from the map and the inverse map. It is called by the
2033 /// \c AlterationNotifier.
2034 virtual void erase(const Key& key) {
2035 Value val = Map::operator[](key);
2036 typename Container::iterator it = _inv_map.find(val);
2037 if (it != _inv_map.end() && it->second == key) {
2043 /// \brief Erase more keys from the map and the inverse map.
2045 /// Erase more keys from the map and the inverse map. It is called by the
2046 /// \c AlterationNotifier.
2047 virtual void erase(const std::vector<Key>& keys) {
2048 for (int i = 0; i < int(keys.size()); ++i) {
2049 Value val = Map::operator[](keys[i]);
2050 typename Container::iterator it = _inv_map.find(val);
2051 if (it != _inv_map.end() && it->second == keys[i]) {
2058 /// \brief Clear the keys from the map and the inverse map.
2060 /// Clear the keys from the map and the inverse map. It is called by the
2061 /// \c AlterationNotifier.
2062 virtual void clear() {
2069 /// \brief The inverse map type.
2071 /// The inverse of this map. The subscript operator of the map
2072 /// gives back the item that was last assigned to the value.
2075 /// \brief Constructor
2077 /// Constructor of the InverseMap.
2078 explicit InverseMap(const CrossRefMap& inverted)
2079 : _inverted(inverted) {}
2081 /// The value type of the InverseMap.
2082 typedef typename CrossRefMap::Key Value;
2083 /// The key type of the InverseMap.
2084 typedef typename CrossRefMap::Value Key;
2086 /// \brief Subscript operator.
2088 /// Subscript operator. It gives back the item
2089 /// that was last assigned to the given value.
2090 Value operator[](const Key& key) const {
2091 return _inverted(key);
2095 const CrossRefMap& _inverted;
2098 /// \brief It gives back the read-only inverse map.
2100 /// It gives back the read-only inverse map.
2101 InverseMap inverse() const {
2102 return InverseMap(*this);
2107 /// \brief Provides continuous and unique ID for the
2108 /// items of a graph.
2110 /// RangeIdMap provides a unique and continuous
2111 /// ID for each item of a given type (\c Node, \c Arc or
2112 /// \c Edge) in a graph. This id is
2113 /// - \b unique: different items get different ids,
2114 /// - \b continuous: the range of the ids is the set of integers
2115 /// between 0 and \c n-1, where \c n is the number of the items of
2116 /// this type (\c Node, \c Arc or \c Edge).
2117 /// - So, the ids can change when deleting an item of the same type.
2119 /// Thus this id is not (necessarily) the same as what can get using
2120 /// the \c id() function of the graph or \ref IdMap.
2121 /// This map can be inverted with its member class \c InverseMap,
2122 /// or with the \c operator() member.
2124 /// \tparam GR The graph type.
2125 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2129 template <typename GR, typename K>
2131 : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2133 typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2136 /// The graph type of RangeIdMap.
2139 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2141 /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2143 /// The value type of RangeIdMap.
2146 /// \brief Constructor.
2149 explicit RangeIdMap(const Graph& gr) : Map(gr) {
2151 const typename Map::Notifier* nf = Map::notifier();
2152 for (nf->first(it); it != INVALID; nf->next(it)) {
2153 Map::set(it, _inv_map.size());
2154 _inv_map.push_back(it);
2160 /// \brief Adds a new key to the map.
2162 /// Add a new key to the map. It is called by the
2163 /// \c AlterationNotifier.
2164 virtual void add(const Item& item) {
2166 Map::set(item, _inv_map.size());
2167 _inv_map.push_back(item);
2170 /// \brief Add more new keys to the map.
2172 /// Add more new keys to the map. It is called by the
2173 /// \c AlterationNotifier.
2174 virtual void add(const std::vector<Item>& items) {
2176 for (int i = 0; i < int(items.size()); ++i) {
2177 Map::set(items[i], _inv_map.size());
2178 _inv_map.push_back(items[i]);
2182 /// \brief Erase the key from the map.
2184 /// Erase the key from the map. It is called by the
2185 /// \c AlterationNotifier.
2186 virtual void erase(const Item& item) {
2187 Map::set(_inv_map.back(), Map::operator[](item));
2188 _inv_map[Map::operator[](item)] = _inv_map.back();
2189 _inv_map.pop_back();
2193 /// \brief Erase more keys from the map.
2195 /// Erase more keys from the map. It is called by the
2196 /// \c AlterationNotifier.
2197 virtual void erase(const std::vector<Item>& items) {
2198 for (int i = 0; i < int(items.size()); ++i) {
2199 Map::set(_inv_map.back(), Map::operator[](items[i]));
2200 _inv_map[Map::operator[](items[i])] = _inv_map.back();
2201 _inv_map.pop_back();
2206 /// \brief Build the unique map.
2208 /// Build the unique map. It is called by the
2209 /// \c AlterationNotifier.
2210 virtual void build() {
2213 const typename Map::Notifier* nf = Map::notifier();
2214 for (nf->first(it); it != INVALID; nf->next(it)) {
2215 Map::set(it, _inv_map.size());
2216 _inv_map.push_back(it);
2220 /// \brief Clear the keys from the map.
2222 /// Clear the keys from the map. It is called by the
2223 /// \c AlterationNotifier.
2224 virtual void clear() {
2231 /// \brief Returns the maximal value plus one.
2233 /// Returns the maximal value plus one in the map.
2234 unsigned int size() const {
2235 return _inv_map.size();
2238 /// \brief Swaps the position of the two items in the map.
2240 /// Swaps the position of the two items in the map.
2241 void swap(const Item& p, const Item& q) {
2242 int pi = Map::operator[](p);
2243 int qi = Map::operator[](q);
2250 /// \brief Gives back the \e RangeId of the item
2252 /// Gives back the \e RangeId of the item.
2253 int operator[](const Item& item) const {
2254 return Map::operator[](item);
2257 /// \brief Gives back the item belonging to a \e RangeId
2259 /// Gives back the item belonging to a \e RangeId.
2260 Item operator()(int id) const {
2261 return _inv_map[id];
2266 typedef std::vector<Item> Container;
2271 /// \brief The inverse map type of RangeIdMap.
2273 /// The inverse map type of RangeIdMap.
2276 /// \brief Constructor
2278 /// Constructor of the InverseMap.
2279 explicit InverseMap(const RangeIdMap& inverted)
2280 : _inverted(inverted) {}
2283 /// The value type of the InverseMap.
2284 typedef typename RangeIdMap::Key Value;
2285 /// The key type of the InverseMap.
2286 typedef typename RangeIdMap::Value Key;
2288 /// \brief Subscript operator.
2290 /// Subscript operator. It gives back the item
2291 /// that the descriptor currently belongs to.
2292 Value operator[](const Key& key) const {
2293 return _inverted(key);
2296 /// \brief Size of the map.
2298 /// Returns the size of the map.
2299 unsigned int size() const {
2300 return _inverted.size();
2304 const RangeIdMap& _inverted;
2307 /// \brief Gives back the inverse of the map.
2309 /// Gives back the inverse of the map.
2310 const InverseMap inverse() const {
2311 return InverseMap(*this);
2315 /// \brief Dynamic iterable bool map.
2317 /// This class provides a special graph map type which can store for
2318 /// each graph item(node, arc, edge, etc.) a bool value. For both
2319 /// the true and the false values it is possible to iterate on the
2322 /// \param GR The graph type.
2323 /// \param ITEM One of the graph's item types, the key of the map.
2324 template <typename GR, typename ITEM>
2325 class IterableBoolMap
2326 : protected ItemSetTraits<GR, ITEM>::template Map<int>::Type {
2330 typedef typename ItemSetTraits<Graph, ITEM>::ItemIt KeyIt;
2331 typedef typename ItemSetTraits<GR, ITEM>::template Map<int>::Type Parent;
2333 std::vector<ITEM> _array;
2338 /// Indicates that the map if reference map.
2339 typedef True ReferenceMapTag;
2345 /// The const reference type.
2346 typedef const Value& ConstReference;
2350 int position(const Key& key) const {
2351 return Parent::operator[](key);
2356 /// \brief Refernce to the value of the map.
2358 /// This class is similar to the bool type. It can be converted to
2359 /// bool and it provides the same operators.
2361 friend class IterableBoolMap;
2363 Reference(IterableBoolMap& map, const Key& key)
2364 : _key(key), _map(map) {}
2367 Reference& operator=(const Reference& value) {
2368 _map.set(_key, static_cast<bool>(value));
2372 operator bool() const {
2373 return static_cast<const IterableBoolMap&>(_map)[_key];
2376 Reference& operator=(bool value) {
2377 _map.set(_key, value);
2380 Reference& operator&=(bool value) {
2381 _map.set(_key, _map[_key] & value);
2384 Reference& operator|=(bool value) {
2385 _map.set(_key, _map[_key] | value);
2388 Reference& operator^=(bool value) {
2389 _map.set(_key, _map[_key] ^ value);
2394 IterableBoolMap& _map;
2397 /// \brief Constructor of the map with a default value.
2399 /// Constructor of the map with a default value.
2400 explicit IterableBoolMap(const Graph& graph, bool def = false)
2402 typename Parent::Notifier* nf = Parent::notifier();
2404 for (nf->first(it); it != INVALID; nf->next(it)) {
2405 Parent::set(it, _array.size());
2406 _array.push_back(it);
2408 _sep = (def ? _array.size() : 0);
2411 /// \brief Const subscript operator of the map.
2413 /// Const subscript operator of the map.
2414 bool operator[](const Key& key) const {
2415 return position(key) < _sep;
2418 /// \brief Subscript operator of the map.
2420 /// Subscript operator of the map.
2421 Reference operator[](const Key& key) {
2422 return Reference(*this, key);
2425 /// \brief Set operation of the map.
2427 /// Set operation of the map.
2428 void set(const Key& key, bool value) {
2429 int pos = position(key);
2431 if (pos < _sep) return;
2432 Key tmp = _array[_sep];
2434 Parent::set(key, _sep);
2436 Parent::set(tmp, pos);
2439 if (pos >= _sep) return;
2441 Key tmp = _array[_sep];
2443 Parent::set(key, _sep);
2445 Parent::set(tmp, pos);
2449 /// \brief Set all items.
2451 /// Set all items in the map.
2452 /// \note Constant time operation.
2453 void setAll(bool value) {
2454 _sep = (value ? _array.size() : 0);
2457 /// \brief Returns the number of the keys mapped to true.
2459 /// Returns the number of the keys mapped to true.
2460 int trueNum() const {
2464 /// \brief Returns the number of the keys mapped to false.
2466 /// Returns the number of the keys mapped to false.
2467 int falseNum() const {
2468 return _array.size() - _sep;
2471 /// \brief Iterator for the keys mapped to true.
2473 /// Iterator for the keys mapped to true. It works
2474 /// like a graph item iterator in the map, it can be converted
2475 /// the key type of the map, incremented with \c ++ operator, and
2476 /// if the iterator leave the last valid key it will be equal to
2478 class TrueIt : public Key {
2482 /// \brief Creates an iterator.
2484 /// Creates an iterator. It iterates on the
2485 /// keys which mapped to true.
2486 /// \param map The IterableIntMap
2487 explicit TrueIt(const IterableBoolMap& map)
2488 : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2491 /// \brief Invalid constructor \& conversion.
2493 /// This constructor initializes the key to be invalid.
2494 /// \sa Invalid for more details.
2495 TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2497 /// \brief Increment operator.
2499 /// Increment Operator.
2500 TrueIt& operator++() {
2501 int pos = _map->position(*this);
2502 Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
2508 const IterableBoolMap* _map;
2511 /// \brief Iterator for the keys mapped to false.
2513 /// Iterator for the keys mapped to false. It works
2514 /// like a graph item iterator in the map, it can be converted
2515 /// the key type of the map, incremented with \c ++ operator, and
2516 /// if the iterator leave the last valid key it will be equal to
2518 class FalseIt : public Key {
2522 /// \brief Creates an iterator.
2524 /// Creates an iterator. It iterates on the
2525 /// keys which mapped to false.
2526 /// \param map The IterableIntMap
2527 explicit FalseIt(const IterableBoolMap& map)
2528 : Parent(map._sep < int(map._array.size()) ?
2529 map._array.back() : INVALID), _map(&map) {}
2531 /// \brief Invalid constructor \& conversion.
2533 /// This constructor initializes the key to be invalid.
2534 /// \sa Invalid for more details.
2535 FalseIt(Invalid) : Parent(INVALID), _map(0) {}
2537 /// \brief Increment operator.
2539 /// Increment Operator.
2540 FalseIt& operator++() {
2541 int pos = _map->position(*this);
2542 Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
2547 const IterableBoolMap* _map;
2550 /// \brief Iterator for the keys mapped to a given value.
2552 /// Iterator for the keys mapped to a given value. It works
2553 /// like a graph item iterator in the map, it can be converted
2554 /// the key type of the map, incremented with \c ++ operator, and
2555 /// if the iterator leave the last valid key it will be equal to
2557 class ItemIt : public Key {
2561 /// \brief Creates an iterator.
2563 /// Creates an iterator. It iterates on the
2564 /// keys which mapped to false.
2565 /// \param map The IterableIntMap
2566 /// \param value Which elements should be iterated.
2567 ItemIt(const IterableBoolMap& map, bool value)
2570 map._array[map._sep - 1] : INVALID) :
2571 (map._sep < int(map._array.size()) ?
2572 map._array.back() : INVALID)), _map(&map) {}
2574 /// \brief Invalid constructor \& conversion.
2576 /// This constructor initializes the key to be invalid.
2577 /// \sa Invalid for more details.
2578 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2580 /// \brief Increment operator.
2582 /// Increment Operator.
2583 ItemIt& operator++() {
2584 int pos = _map->position(*this);
2585 int _sep = pos >= _map->_sep ? _map->_sep : 0;
2586 Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
2591 const IterableBoolMap* _map;
2596 virtual void add(const Key& key) {
2598 Parent::set(key, _array.size());
2599 _array.push_back(key);
2602 virtual void add(const std::vector<Key>& keys) {
2604 for (int i = 0; i < int(keys.size()); ++i) {
2605 Parent::set(keys[i], _array.size());
2606 _array.push_back(keys[i]);
2610 virtual void erase(const Key& key) {
2611 int pos = position(key);
2614 Parent::set(_array[_sep], pos);
2615 _array[pos] = _array[_sep];
2616 Parent::set(_array.back(), _sep);
2617 _array[_sep] = _array.back();
2620 Parent::set(_array.back(), pos);
2621 _array[pos] = _array.back();
2627 virtual void erase(const std::vector<Key>& keys) {
2628 for (int i = 0; i < int(keys.size()); ++i) {
2629 int pos = position(keys[i]);
2632 Parent::set(_array[_sep], pos);
2633 _array[pos] = _array[_sep];
2634 Parent::set(_array.back(), _sep);
2635 _array[_sep] = _array.back();
2638 Parent::set(_array.back(), pos);
2639 _array[pos] = _array.back();
2643 Parent::erase(keys);
2646 virtual void build() {
2648 typename Parent::Notifier* nf = Parent::notifier();
2650 for (nf->first(it); it != INVALID; nf->next(it)) {
2651 Parent::set(it, _array.size());
2652 _array.push_back(it);
2657 virtual void clear() {
2666 namespace _maps_bits {
2667 template <typename Item>
2668 struct IterableIntMapNode {
2669 IterableIntMapNode() : value(-1) {}
2670 IterableIntMapNode(int _value) : value(_value) {}
2676 ///\ingroup graph_maps
2678 /// \brief Dynamic iterable integer map.
2680 /// This class provides a special graph map type which can store
2681 /// for each graph item(node, edge, etc.) an integer value. For each
2682 /// non negative value it is possible to iterate on the keys which
2683 /// mapped to the given value.
2685 /// \note The size of the data structure depends on the highest
2686 /// value in the map.
2688 /// \param GR The graph type.
2689 /// \param ITEM One of the graph's item type, the key of the map.
2690 template <typename GR, typename ITEM>
2691 class IterableIntMap
2692 : protected ItemSetTraits<GR, ITEM>::
2693 template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type {
2695 typedef typename ItemSetTraits<GR, ITEM>::
2696 template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type Parent;
2705 /// \brief Constructor of the map.
2707 /// Constructor of the map. It set all values to -1.
2708 explicit IterableIntMap(const Graph& graph)
2711 /// \brief Constructor of the map with a given value.
2713 /// Constructor of the map with a given value.
2714 explicit IterableIntMap(const Graph& graph, int value)
2715 : Parent(graph, _maps_bits::IterableIntMapNode<ITEM>(value)) {
2717 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
2725 void unlace(const Key& key) {
2726 typename Parent::Value& node = Parent::operator[](key);
2727 if (node.value < 0) return;
2728 if (node.prev != INVALID) {
2729 Parent::operator[](node.prev).next = node.next;
2731 _first[node.value] = node.next;
2733 if (node.next != INVALID) {
2734 Parent::operator[](node.next).prev = node.prev;
2736 while (!_first.empty() && _first.back() == INVALID) {
2741 void lace(const Key& key) {
2742 typename Parent::Value& node = Parent::operator[](key);
2743 if (node.value < 0) return;
2744 if (node.value >= int(_first.size())) {
2745 _first.resize(node.value + 1, INVALID);
2747 node.prev = INVALID;
2748 node.next = _first[node.value];
2749 if (node.next != INVALID) {
2750 Parent::operator[](node.next).prev = key;
2752 _first[node.value] = key;
2757 /// Indicates that the map if reference map.
2758 typedef True ReferenceMapTag;
2760 /// \brief Refernce to the value of the map.
2762 /// This class is similar to the int type. It can
2763 /// be converted to int and it has the same operators.
2765 friend class IterableIntMap;
2767 Reference(IterableIntMap& map, const Key& key)
2768 : _key(key), _map(map) {}
2771 Reference& operator=(const Reference& value) {
2772 _map.set(_key, static_cast<const int&>(value));
2776 operator const int&() const {
2777 return static_cast<const IterableIntMap&>(_map)[_key];
2780 Reference& operator=(int value) {
2781 _map.set(_key, value);
2784 Reference& operator++() {
2785 _map.set(_key, _map[_key] + 1);
2788 int operator++(int) {
2789 int value = _map[_key];
2790 _map.set(_key, value + 1);
2793 Reference& operator--() {
2794 _map.set(_key, _map[_key] - 1);
2797 int operator--(int) {
2798 int value = _map[_key];
2799 _map.set(_key, value - 1);
2802 Reference& operator+=(int value) {
2803 _map.set(_key, _map[_key] + value);
2806 Reference& operator-=(int value) {
2807 _map.set(_key, _map[_key] - value);
2810 Reference& operator*=(int value) {
2811 _map.set(_key, _map[_key] * value);
2814 Reference& operator/=(int value) {
2815 _map.set(_key, _map[_key] / value);
2818 Reference& operator%=(int value) {
2819 _map.set(_key, _map[_key] % value);
2822 Reference& operator&=(int value) {
2823 _map.set(_key, _map[_key] & value);
2826 Reference& operator|=(int value) {
2827 _map.set(_key, _map[_key] | value);
2830 Reference& operator^=(int value) {
2831 _map.set(_key, _map[_key] ^ value);
2834 Reference& operator<<=(int value) {
2835 _map.set(_key, _map[_key] << value);
2838 Reference& operator>>=(int value) {
2839 _map.set(_key, _map[_key] >> value);
2845 IterableIntMap& _map;
2848 /// The const reference type.
2849 typedef const Value& ConstReference;
2851 /// \brief Gives back the maximal value plus one.
2853 /// Gives back the maximal value plus one.
2855 return _first.size();
2858 /// \brief Set operation of the map.
2860 /// Set operation of the map.
2861 void set(const Key& key, const Value& value) {
2863 Parent::operator[](key).value = value;
2867 /// \brief Const subscript operator of the map.
2869 /// Const subscript operator of the map.
2870 const Value& operator[](const Key& key) const {
2871 return Parent::operator[](key).value;
2874 /// \brief Subscript operator of the map.
2876 /// Subscript operator of the map.
2877 Reference operator[](const Key& key) {
2878 return Reference(*this, key);
2881 /// \brief Iterator for the keys with the same value.
2883 /// Iterator for the keys with the same value. It works
2884 /// like a graph item iterator in the map, it can be converted
2885 /// the item type of the map, incremented with \c ++ operator, and
2886 /// if the iterator leave the last valid item it will be equal to
2888 class ItemIt : public ITEM {
2890 typedef ITEM Parent;
2892 /// \brief Invalid constructor \& conversion.
2894 /// This constructor initializes the item to be invalid.
2895 /// \sa Invalid for more details.
2896 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2898 /// \brief Creates an iterator with a value.
2900 /// Creates an iterator with a value. It iterates on the
2901 /// keys which have the given value.
2902 /// \param map The IterableIntMap
2903 /// \param value The value
2904 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
2905 if (value < 0 || value >= int(_map->_first.size())) {
2906 Parent::operator=(INVALID);
2908 Parent::operator=(_map->_first[value]);
2912 /// \brief Increment operator.
2914 /// Increment Operator.
2915 ItemIt& operator++() {
2916 Parent::operator=(_map->IterableIntMap::Parent::
2917 operator[](static_cast<Parent&>(*this)).next);
2923 const IterableIntMap* _map;
2928 virtual void erase(const Key& key) {
2933 virtual void erase(const std::vector<Key>& keys) {
2934 for (int i = 0; i < int(keys.size()); ++i) {
2937 Parent::erase(keys);
2940 virtual void clear() {
2946 std::vector<ITEM> _first;
2949 namespace _maps_bits {
2950 template <typename Item, typename Value>
2951 struct IterableValueMapNode {
2952 IterableValueMapNode(Value _value = Value()) : value(_value) {}
2958 ///\ingroup graph_maps
2960 /// \brief Dynamic iterable map for comparable values.
2962 /// This class provides a special graph map type which can store
2963 /// for each graph item(node, edge, etc.) a value. For each
2964 /// value it is possible to iterate on the keys which mapped to the
2965 /// given value. The type stores for each value a linked list with
2966 /// the items which mapped to the value, and the values are stored
2967 /// in balanced binary tree. The values of the map can be accessed
2968 /// with stl compatible forward iterator.
2970 /// This type is not reference map so it cannot be modified with
2971 /// the subscription operator.
2973 /// \see InvertableMap
2975 /// \param GR The graph type.
2976 /// \param ITEM One of the graph's item type, the key of the map.
2977 /// \param VAL Any comparable value type.
2978 template <typename GR, typename ITEM, typename VAL>
2979 class IterableValueMap
2980 : protected ItemSetTraits<GR, ITEM>::
2981 template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type {
2983 typedef typename ItemSetTraits<GR, ITEM>::
2984 template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type Parent;
2995 /// \brief Constructor of the Map with a given value.
2997 /// Constructor of the Map with a given value.
2998 explicit IterableValueMap(const Graph& graph,
2999 const Value& value = Value())
3000 : Parent(graph, _maps_bits::IterableValueMapNode<ITEM, VAL>(value)) {
3001 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3008 void unlace(const Key& key) {
3009 typename Parent::Value& node = Parent::operator[](key);
3010 if (node.prev != INVALID) {
3011 Parent::operator[](node.prev).next = node.next;
3013 if (node.next != INVALID) {
3014 _first[node.value] = node.next;
3016 _first.erase(node.value);
3019 if (node.next != INVALID) {
3020 Parent::operator[](node.next).prev = node.prev;
3024 void lace(const Key& key) {
3025 typename Parent::Value& node = Parent::operator[](key);
3026 typename std::map<Value, Key>::iterator it = _first.find(node.value);
3027 if (it == _first.end()) {
3028 node.prev = node.next = INVALID;
3029 if (node.next != INVALID) {
3030 Parent::operator[](node.next).prev = key;
3032 _first.insert(std::make_pair(node.value, key));
3034 node.prev = INVALID;
3035 node.next = it->second;
3036 if (node.next != INVALID) {
3037 Parent::operator[](node.next).prev = key;
3045 /// \brief Forward iterator for values.
3047 /// This iterator is an stl compatible forward
3048 /// iterator on the values of the map. The values can
3049 /// be accessed in the [beginValue, endValue) range.
3052 : public std::iterator<std::forward_iterator_tag, Value> {
3053 friend class IterableValueMap;
3055 ValueIterator(typename std::map<Value, Key>::const_iterator _it)
3061 ValueIterator& operator++() { ++it; return *this; }
3062 ValueIterator operator++(int) {
3063 ValueIterator tmp(*this);
3068 const Value& operator*() const { return it->first; }
3069 const Value* operator->() const { return &(it->first); }
3071 bool operator==(ValueIterator jt) const { return it == jt.it; }
3072 bool operator!=(ValueIterator jt) const { return it != jt.it; }
3075 typename std::map<Value, Key>::const_iterator it;
3078 /// \brief Returns an iterator to the first value.
3080 /// Returns an stl compatible iterator to the
3081 /// first value of the map. The values of the
3082 /// map can be accessed in the [beginValue, endValue)
3084 ValueIterator beginValue() const {
3085 return ValueIterator(_first.begin());
3088 /// \brief Returns an iterator after the last value.
3090 /// Returns an stl compatible iterator after the
3091 /// last value of the map. The values of the
3092 /// map can be accessed in the [beginValue, endValue)
3094 ValueIterator endValue() const {
3095 return ValueIterator(_first.end());
3098 /// \brief Set operation of the map.
3100 /// Set operation of the map.
3101 void set(const Key& key, const Value& value) {
3103 Parent::operator[](key).value = value;
3107 /// \brief Const subscript operator of the map.
3109 /// Const subscript operator of the map.
3110 const Value& operator[](const Key& key) const {
3111 return Parent::operator[](key).value;
3114 /// \brief Iterator for the keys with the same value.
3116 /// Iterator for the keys with the same value. It works
3117 /// like a graph item iterator in the map, it can be converted
3118 /// the item type of the map, incremented with \c ++ operator, and
3119 /// if the iterator leave the last valid item it will be equal to
3121 class ItemIt : public ITEM {
3123 typedef ITEM Parent;
3125 /// \brief Invalid constructor \& conversion.
3127 /// This constructor initializes the item to be invalid.
3128 /// \sa Invalid for more details.
3129 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3131 /// \brief Creates an iterator with a value.
3133 /// Creates an iterator with a value. It iterates on the
3134 /// keys which have the given value.
3135 /// \param map The IterableValueMap
3136 /// \param value The value
3137 ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
3138 typename std::map<Value, Key>::const_iterator it =
3139 map._first.find(value);
3140 if (it == map._first.end()) {
3141 Parent::operator=(INVALID);
3143 Parent::operator=(it->second);
3147 /// \brief Increment operator.
3149 /// Increment Operator.
3150 ItemIt& operator++() {
3151 Parent::operator=(_map->IterableValueMap::Parent::
3152 operator[](static_cast<Parent&>(*this)).next);
3158 const IterableValueMap* _map;
3163 virtual void add(const Key& key) {
3168 virtual void add(const std::vector<Key>& keys) {
3170 for (int i = 0; i < int(keys.size()); ++i) {
3175 virtual void erase(const Key& key) {
3180 virtual void erase(const std::vector<Key>& keys) {
3181 for (int i = 0; i < int(keys.size()); ++i) {
3184 Parent::erase(keys);
3187 virtual void build() {
3189 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3194 virtual void clear() {
3200 std::map<Value, Key> _first;
3203 /// \brief Map of the source nodes of arcs in a digraph.
3205 /// SourceMap provides access for the source node of each arc in a digraph,
3206 /// which is returned by the \c source() function of the digraph.
3207 /// \tparam GR The digraph type.
3209 template <typename GR>
3214 typedef typename GR::Arc Key;
3216 typedef typename GR::Node Value;
3218 /// \brief Constructor
3221 /// \param digraph The digraph that the map belongs to.
3222 explicit SourceMap(const GR& digraph) : _graph(digraph) {}
3224 /// \brief Returns the source node of the given arc.
3226 /// Returns the source node of the given arc.
3227 Value operator[](const Key& arc) const {
3228 return _graph.source(arc);
3235 /// \brief Returns a \c SourceMap class.
3237 /// This function just returns an \c SourceMap class.
3238 /// \relates SourceMap
3239 template <typename GR>
3240 inline SourceMap<GR> sourceMap(const GR& graph) {
3241 return SourceMap<GR>(graph);
3244 /// \brief Map of the target nodes of arcs in a digraph.
3246 /// TargetMap provides access for the target node of each arc in a digraph,
3247 /// which is returned by the \c target() function of the digraph.
3248 /// \tparam GR The digraph type.
3250 template <typename GR>
3255 typedef typename GR::Arc Key;
3257 typedef typename GR::Node Value;
3259 /// \brief Constructor
3262 /// \param digraph The digraph that the map belongs to.
3263 explicit TargetMap(const GR& digraph) : _graph(digraph) {}
3265 /// \brief Returns the target node of the given arc.
3267 /// Returns the target node of the given arc.
3268 Value operator[](const Key& e) const {
3269 return _graph.target(e);
3276 /// \brief Returns a \c TargetMap class.
3278 /// This function just returns a \c TargetMap class.
3279 /// \relates TargetMap
3280 template <typename GR>
3281 inline TargetMap<GR> targetMap(const GR& graph) {
3282 return TargetMap<GR>(graph);
3285 /// \brief Map of the "forward" directed arc view of edges in a graph.
3287 /// ForwardMap provides access for the "forward" directed arc view of
3288 /// each edge in a graph, which is returned by the \c direct() function
3289 /// of the graph with \c true parameter.
3290 /// \tparam GR The graph type.
3291 /// \see BackwardMap
3292 template <typename GR>
3296 typedef typename GR::Arc Value;
3297 typedef typename GR::Edge Key;
3299 /// \brief Constructor
3302 /// \param graph The graph that the map belongs to.
3303 explicit ForwardMap(const GR& graph) : _graph(graph) {}
3305 /// \brief Returns the "forward" directed arc view of the given edge.
3307 /// Returns the "forward" directed arc view of the given edge.
3308 Value operator[](const Key& key) const {
3309 return _graph.direct(key, true);
3316 /// \brief Returns a \c ForwardMap class.
3318 /// This function just returns an \c ForwardMap class.
3319 /// \relates ForwardMap
3320 template <typename GR>
3321 inline ForwardMap<GR> forwardMap(const GR& graph) {
3322 return ForwardMap<GR>(graph);
3325 /// \brief Map of the "backward" directed arc view of edges in a graph.
3327 /// BackwardMap provides access for the "backward" directed arc view of
3328 /// each edge in a graph, which is returned by the \c direct() function
3329 /// of the graph with \c false parameter.
3330 /// \tparam GR The graph type.
3332 template <typename GR>
3336 typedef typename GR::Arc Value;
3337 typedef typename GR::Edge Key;
3339 /// \brief Constructor
3342 /// \param graph The graph that the map belongs to.
3343 explicit BackwardMap(const GR& graph) : _graph(graph) {}
3345 /// \brief Returns the "backward" directed arc view of the given edge.
3347 /// Returns the "backward" directed arc view of the given edge.
3348 Value operator[](const Key& key) const {
3349 return _graph.direct(key, false);
3356 /// \brief Returns a \c BackwardMap class
3358 /// This function just returns a \c BackwardMap class.
3359 /// \relates BackwardMap
3360 template <typename GR>
3361 inline BackwardMap<GR> backwardMap(const GR& graph) {
3362 return BackwardMap<GR>(graph);
3365 /// \brief Map of the in-degrees of nodes in a digraph.
3367 /// This map returns the in-degree of a node. Once it is constructed,
3368 /// the degrees are stored in a standard \c NodeMap, so each query is done
3369 /// in constant time. On the other hand, the values are updated automatically
3370 /// whenever the digraph changes.
3372 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3373 /// may provide alternative ways to modify the digraph.
3374 /// The correct behavior of InDegMap is not guarantied if these additional
3375 /// features are used. For example the functions
3376 /// \ref ListDigraph::changeSource() "changeSource()",
3377 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3378 /// \ref ListDigraph::reverseArc() "reverseArc()"
3379 /// of \ref ListDigraph will \e not update the degree values correctly.
3382 template <typename GR>
3384 : protected ItemSetTraits<GR, typename GR::Arc>
3385 ::ItemNotifier::ObserverBase {
3389 /// The graph type of InDegMap
3393 typedef typename Digraph::Node Key;
3397 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3398 ::ItemNotifier::ObserverBase Parent;
3403 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3406 typedef typename ItemSetTraits<Digraph, Key>::
3407 template Map<int>::Type Parent;
3409 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3411 virtual void add(const Key& key) {
3413 Parent::set(key, 0);
3416 virtual void add(const std::vector<Key>& keys) {
3418 for (int i = 0; i < int(keys.size()); ++i) {
3419 Parent::set(keys[i], 0);
3423 virtual void build() {
3426 typename Parent::Notifier* nf = Parent::notifier();
3427 for (nf->first(it); it != INVALID; nf->next(it)) {
3435 /// \brief Constructor.
3437 /// Constructor for creating an in-degree map.
3438 explicit InDegMap(const Digraph& graph)
3439 : _digraph(graph), _deg(graph) {
3440 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3442 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3443 _deg[it] = countInArcs(_digraph, it);
3447 /// \brief Gives back the in-degree of a Node.
3449 /// Gives back the in-degree of a Node.
3450 int operator[](const Key& key) const {
3456 typedef typename Digraph::Arc Arc;
3458 virtual void add(const Arc& arc) {
3459 ++_deg[_digraph.target(arc)];
3462 virtual void add(const std::vector<Arc>& arcs) {
3463 for (int i = 0; i < int(arcs.size()); ++i) {
3464 ++_deg[_digraph.target(arcs[i])];
3468 virtual void erase(const Arc& arc) {
3469 --_deg[_digraph.target(arc)];
3472 virtual void erase(const std::vector<Arc>& arcs) {
3473 for (int i = 0; i < int(arcs.size()); ++i) {
3474 --_deg[_digraph.target(arcs[i])];
3478 virtual void build() {
3479 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3480 _deg[it] = countInArcs(_digraph, it);
3484 virtual void clear() {
3485 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3491 const Digraph& _digraph;
3495 /// \brief Map of the out-degrees of nodes in a digraph.
3497 /// This map returns the out-degree of a node. Once it is constructed,
3498 /// the degrees are stored in a standard \c NodeMap, so each query is done
3499 /// in constant time. On the other hand, the values are updated automatically
3500 /// whenever the digraph changes.
3502 /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3503 /// may provide alternative ways to modify the digraph.
3504 /// The correct behavior of OutDegMap is not guarantied if these additional
3505 /// features are used. For example the functions
3506 /// \ref ListDigraph::changeSource() "changeSource()",
3507 /// \ref ListDigraph::changeTarget() "changeTarget()" and
3508 /// \ref ListDigraph::reverseArc() "reverseArc()"
3509 /// of \ref ListDigraph will \e not update the degree values correctly.
3512 template <typename GR>
3514 : protected ItemSetTraits<GR, typename GR::Arc>
3515 ::ItemNotifier::ObserverBase {
3519 /// The graph type of OutDegMap
3523 typedef typename Digraph::Node Key;
3527 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
3528 ::ItemNotifier::ObserverBase Parent;
3533 : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
3536 typedef typename ItemSetTraits<Digraph, Key>::
3537 template Map<int>::Type Parent;
3539 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
3541 virtual void add(const Key& key) {
3543 Parent::set(key, 0);
3545 virtual void add(const std::vector<Key>& keys) {
3547 for (int i = 0; i < int(keys.size()); ++i) {
3548 Parent::set(keys[i], 0);
3551 virtual void build() {
3554 typename Parent::Notifier* nf = Parent::notifier();
3555 for (nf->first(it); it != INVALID; nf->next(it)) {
3563 /// \brief Constructor.
3565 /// Constructor for creating an out-degree map.
3566 explicit OutDegMap(const Digraph& graph)
3567 : _digraph(graph), _deg(graph) {
3568 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
3570 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3571 _deg[it] = countOutArcs(_digraph, it);
3575 /// \brief Gives back the out-degree of a Node.
3577 /// Gives back the out-degree of a Node.
3578 int operator[](const Key& key) const {
3584 typedef typename Digraph::Arc Arc;
3586 virtual void add(const Arc& arc) {
3587 ++_deg[_digraph.source(arc)];
3590 virtual void add(const std::vector<Arc>& arcs) {
3591 for (int i = 0; i < int(arcs.size()); ++i) {
3592 ++_deg[_digraph.source(arcs[i])];
3596 virtual void erase(const Arc& arc) {
3597 --_deg[_digraph.source(arc)];
3600 virtual void erase(const std::vector<Arc>& arcs) {
3601 for (int i = 0; i < int(arcs.size()); ++i) {
3602 --_deg[_digraph.source(arcs[i])];
3606 virtual void build() {
3607 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3608 _deg[it] = countOutArcs(_digraph, it);
3612 virtual void clear() {
3613 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3619 const Digraph& _digraph;
3623 /// \brief Potential difference map
3625 /// PotentialDifferenceMap returns the difference between the potentials of
3626 /// the source and target nodes of each arc in a digraph, i.e. it returns
3628 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3630 /// \tparam GR The digraph type.
3631 /// \tparam POT A node map storing the potentials.
3632 template <typename GR, typename POT>
3633 class PotentialDifferenceMap {
3636 typedef typename GR::Arc Key;
3638 typedef typename POT::Value Value;
3640 /// \brief Constructor
3642 /// Contructor of the map.
3643 explicit PotentialDifferenceMap(const GR& gr,
3644 const POT& potential)
3645 : _digraph(gr), _potential(potential) {}
3647 /// \brief Returns the potential difference for the given arc.
3649 /// Returns the potential difference for the given arc, i.e.
3651 /// potential[gr.target(arc)] - potential[gr.source(arc)].
3653 Value operator[](const Key& arc) const {
3654 return _potential[_digraph.target(arc)] -
3655 _potential[_digraph.source(arc)];
3660 const POT& _potential;
3663 /// \brief Returns a PotentialDifferenceMap.
3665 /// This function just returns a PotentialDifferenceMap.
3666 /// \relates PotentialDifferenceMap
3667 template <typename GR, typename POT>
3668 PotentialDifferenceMap<GR, POT>
3669 potentialDifferenceMap(const GR& gr, const POT& potential) {
3670 return PotentialDifferenceMap<GR, POT>(gr, potential);
3676 #endif // LEMON_MAPS_H