Small changes in min. cost flow algorithms.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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
25 #include <lemon/bits/utility.h>
26 #include <lemon/bits/traits.h>
30 ///\brief Miscellaneous property maps
32 ///\todo This file has the same name as the concept file in concepts/,
33 /// and this is not easily detectable in docs...
42 /// Base class of maps.
44 /// Base class of maps.
45 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
46 template<typename K, typename T>
55 /// Null map. (a.k.a. DoNothingMap)
57 /// If you have to provide a map only for its type definitions,
58 /// or if you have to provide a writable map, but
59 /// data written to it will sent to <tt>/dev/null</tt>...
60 template<typename K, typename T>
61 class NullMap : public MapBase<K, T> {
63 typedef MapBase<K, T> Parent;
64 typedef typename Parent::Key Key;
65 typedef typename Parent::Value Value;
67 /// Gives back a default constructed element.
68 T operator[](const K&) const { return T(); }
69 /// Absorbs the value.
70 void set(const K&, const T&) {}
73 template <typename K, typename V>
74 NullMap<K, V> nullMap() {
75 return NullMap<K, V>();
81 /// This is a readable map which assigns a specified value to each key.
82 /// In other aspects it is equivalent to the \ref NullMap.
83 /// \todo set could be used to set the value.
84 template<typename K, typename T>
85 class ConstMap : public MapBase<K, T> {
90 typedef MapBase<K, T> Parent;
91 typedef typename Parent::Key Key;
92 typedef typename Parent::Value Value;
94 /// Default constructor
96 /// The value of the map will be uninitialized.
97 /// (More exactly it will be default constructed.)
101 /// \param _v The initial value of the map.
103 ConstMap(const T &_v) : v(_v) {}
105 T operator[](const K&) const { return v; }
106 void set(const K&, const T&) {}
108 template<typename T1>
110 typedef ConstMap<K, T1> other;
113 template<typename T1>
114 ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
117 ///Returns a \ref ConstMap class
119 ///This function just returns a \ref ConstMap class.
121 template<typename K, typename V>
122 inline ConstMap<K, V> constMap(const V &v) {
123 return ConstMap<K, V>(v);
127 //\todo to document later
128 template<typename T, T v>
131 //\todo to document later
132 template<typename K, typename V, V v>
133 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
135 typedef MapBase<K, V> Parent;
136 typedef typename Parent::Key Key;
137 typedef typename Parent::Value Value;
140 V operator[](const K&) const { return v; }
141 void set(const K&, const V&) { }
144 ///Returns a \ref ConstMap class
146 ///This function just returns a \ref ConstMap class.
148 template<typename K, typename V, V v>
149 inline ConstMap<K, Const<V, v> > constMap() {
150 return ConstMap<K, Const<V, v> >();
153 /// \c std::map wrapper
155 /// This is essentially a wrapper for \c std::map. With addition that
156 /// you can specify a default value different from \c Value() .
158 /// \todo Provide allocator parameter...
159 template <typename K, typename T, typename Compare = std::less<K> >
160 class StdMap : public std::map<K, T, Compare> {
161 typedef std::map<K, T, Compare> parent;
163 typedef typename parent::value_type PairType;
171 typedef T& Reference;
173 typedef const T& ConstReference;
177 /// Constructor with specified default value
178 StdMap(const T& _v) : v(_v) {}
180 /// \brief Constructs the map from an appropriate std::map.
182 /// \warning Inefficient: copies the content of \c m !
183 StdMap(const parent &m) : parent(m) {}
184 /// \brief Constructs the map from an appropriate std::map, and explicitly
185 /// specifies a default value.
187 /// \warning Inefficient: copies the content of \c m !
188 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
190 template<typename T1, typename Comp1>
191 StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) {
195 Reference operator[](const Key &k) {
196 return insert(PairType(k,v)).first -> second;
199 ConstReference operator[](const Key &k) const {
200 typename parent::iterator i = lower_bound(k);
201 if (i == parent::end() || parent::key_comp()(k, (*i).first))
205 void set(const Key &k, const T &t) {
206 parent::operator[](k) = t;
209 /// Changes the default value of the map.
210 /// \return Returns the previous default value.
212 /// \warning The value of some keys (which has already been queried, but
213 /// the value has been unchanged from the default) may change!
214 T setDefault(const T &_v) { T old=v; v=_v; return old; }
216 template<typename T1>
218 typedef StdMap<Key, T1,Compare> other;
224 /// \addtogroup map_adaptors
227 /// \brief Identity mapping.
229 /// This mapping gives back the given key as value without any
231 template <typename T>
232 class IdentityMap : public MapBase<T, T> {
234 typedef MapBase<T, T> Parent;
235 typedef typename Parent::Key Key;
236 typedef typename Parent::Value Value;
238 const T& operator[](const T& t) const {
243 ///Returns an \ref IdentityMap class
245 ///This function just returns an \ref IdentityMap class.
246 ///\relates IdentityMap
248 inline IdentityMap<T> identityMap() {
249 return IdentityMap<T>();
253 ///Convert the \c Value of a map to another type.
255 ///This \ref concepts::ReadMap "read only map"
256 ///converts the \c Value of a maps to type \c T.
257 ///Its \c Key is inherited from \c M.
258 template <typename M, typename T>
259 class ConvertMap : public MapBase<typename M::Key, T> {
262 typedef MapBase<typename M::Key, T> Parent;
263 typedef typename Parent::Key Key;
264 typedef typename Parent::Value Value;
269 ///\param _m is the underlying map
270 ConvertMap(const M &_m) : m(_m) {};
272 /// \brief The subscript operator.
274 /// The subscript operator.
276 /// \return The target of the edge
277 Value operator[](const Key& k) const {return m[k];}
280 ///Returns an \ref ConvertMap class
282 ///This function just returns an \ref ConvertMap class.
283 ///\relates ConvertMap
284 ///\todo The order of the template parameters are changed.
285 template<typename T, typename M>
286 inline ConvertMap<M, T> convertMap(const M &m) {
287 return ConvertMap<M, T>(m);
290 ///Simple wrapping of the map
292 ///This \ref concepts::ReadMap "read only map" returns the simple
293 ///wrapping of the given map. Sometimes the reference maps cannot be
294 ///combined with simple read maps. This map adaptor wraps the given
295 ///map to simple read map.
297 class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
301 typedef MapBase<typename M::Key, typename M::Value> Parent;
302 typedef typename Parent::Key Key;
303 typedef typename Parent::Value Value;
306 SimpleMap(const M &_m) : m(_m) {};
307 Value operator[](Key k) const {return m[k];}
310 ///Simple writeable wrapping of the map
312 ///This \ref concepts::ReadMap "read only map" returns the simple
313 ///wrapping of the given map. Sometimes the reference maps cannot be
314 ///combined with simple read-write maps. This map adaptor wraps the
315 ///given map to simple read-write map.
317 class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
321 typedef MapBase<typename M::Key, typename M::Value> Parent;
322 typedef typename Parent::Key Key;
323 typedef typename Parent::Value Value;
326 SimpleWriteMap(M &_m) : m(_m) {};
327 Value operator[](Key k) const {return m[k];}
328 void set(Key k, const Value& c) { m.set(k, c); }
333 ///This \ref concepts::ReadMap "read only map" returns the sum of the two
334 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
335 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
337 template<typename M1, typename M2>
338 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
343 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
344 typedef typename Parent::Key Key;
345 typedef typename Parent::Value Value;
348 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
349 Value operator[](Key k) const {return m1[k]+m2[k];}
352 ///Returns an \ref AddMap class
354 ///This function just returns an \ref AddMap class.
355 ///\todo How to call these type of functions?
358 ///\todo Wrong scope in Doxygen when \c \\relates is used
359 template<typename M1, typename M2>
360 inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
361 return AddMap<M1, M2>(m1,m2);
364 ///Shift a map with a constant.
366 ///This \ref concepts::ReadMap "read only map" returns the sum of the
367 ///given map and a constant value.
368 ///Its \c Key and \c Value is inherited from \c M.
372 /// ShiftMap<X> sh(x,v);
374 ///is equivalent with
376 /// ConstMap<X::Key, X::Value> c_tmp(v);
377 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
379 template<typename M, typename C = typename M::Value>
380 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
384 typedef MapBase<typename M::Key, typename M::Value> Parent;
385 typedef typename Parent::Key Key;
386 typedef typename Parent::Value Value;
391 ///\param _m is the undelying map
392 ///\param _v is the shift value
393 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
394 Value operator[](Key k) const {return m[k] + v;}
397 ///Shift a map with a constant.
399 ///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the
400 ///given map and a constant value. It makes also possible to write the map.
401 ///Its \c Key and \c Value is inherited from \c M.
405 /// ShiftMap<X> sh(x,v);
407 ///is equivalent with
409 /// ConstMap<X::Key, X::Value> c_tmp(v);
410 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
412 template<typename M, typename C = typename M::Value>
413 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
417 typedef MapBase<typename M::Key, typename M::Value> Parent;
418 typedef typename Parent::Key Key;
419 typedef typename Parent::Value Value;
424 ///\param _m is the undelying map
425 ///\param _v is the shift value
426 ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
427 Value operator[](Key k) const {return m[k] + v;}
428 void set(Key k, const Value& c) { m.set(k, c - v); }
431 ///Returns an \ref ShiftMap class
433 ///This function just returns an \ref ShiftMap class.
435 ///\todo A better name is required.
436 template<typename M, typename C>
437 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
438 return ShiftMap<M, C>(m,v);
441 template<typename M, typename C>
442 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
443 return ShiftWriteMap<M, C>(m,v);
446 ///Difference of two maps
448 ///This \ref concepts::ReadMap "read only map" returns the difference
449 ///of the values of the two
450 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
451 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
453 template<typename M1, typename M2>
454 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
458 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
459 typedef typename Parent::Key Key;
460 typedef typename Parent::Value Value;
463 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
464 Value operator[](Key k) const {return m1[k]-m2[k];}
467 ///Returns a \ref SubMap class
469 ///This function just returns a \ref SubMap class.
472 template<typename M1, typename M2>
473 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
474 return SubMap<M1, M2>(m1, m2);
477 ///Product of two maps
479 ///This \ref concepts::ReadMap "read only map" returns the product of the
482 ///maps. Its \c Key and \c Value will be inherited from \c M1.
483 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
485 template<typename M1, typename M2>
486 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
490 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
491 typedef typename Parent::Key Key;
492 typedef typename Parent::Value Value;
495 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
496 Value operator[](Key k) const {return m1[k]*m2[k];}
499 ///Returns a \ref MulMap class
501 ///This function just returns a \ref MulMap class.
503 template<typename M1, typename M2>
504 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
505 return MulMap<M1, M2>(m1,m2);
508 ///Scales a maps with a constant.
510 ///This \ref concepts::ReadMap "read only map" returns the value of the
511 ///given map multiplied from the left side with a constant value.
512 ///Its \c Key and \c Value is inherited from \c M.
516 /// ScaleMap<X> sc(x,v);
518 ///is equivalent with
520 /// ConstMap<X::Key, X::Value> c_tmp(v);
521 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
523 template<typename M, typename C = typename M::Value>
524 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
528 typedef MapBase<typename M::Key, typename M::Value> Parent;
529 typedef typename Parent::Key Key;
530 typedef typename Parent::Value Value;
535 ///\param _m is the undelying map
536 ///\param _v is the scaling value
537 ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
538 Value operator[](Key k) const {return v * m[k];}
541 ///Scales a maps with a constant.
543 ///This \ref concepts::ReadWriteMap "read-write map" returns the value of the
544 ///given map multiplied from the left side with a constant value. It can
545 ///be used as write map also if the given multiplier is not zero.
546 ///Its \c Key and \c Value is inherited from \c M.
547 template<typename M, typename C = typename M::Value>
548 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
552 typedef MapBase<typename M::Key, typename M::Value> Parent;
553 typedef typename Parent::Key Key;
554 typedef typename Parent::Value Value;
559 ///\param _m is the undelying map
560 ///\param _v is the scaling value
561 ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
562 Value operator[](Key k) const {return v * m[k];}
563 void set(Key k, const Value& c) { m.set(k, c / v);}
566 ///Returns an \ref ScaleMap class
568 ///This function just returns an \ref ScaleMap class.
570 ///\todo A better name is required.
571 template<typename M, typename C>
572 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
573 return ScaleMap<M, C>(m,v);
576 template<typename M, typename C>
577 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
578 return ScaleWriteMap<M, C>(m,v);
581 ///Quotient of two maps
583 ///This \ref concepts::ReadMap "read only map" returns the quotient of the
585 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
586 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
588 template<typename M1, typename M2>
589 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
593 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
594 typedef typename Parent::Key Key;
595 typedef typename Parent::Value Value;
598 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
599 Value operator[](Key k) const {return m1[k]/m2[k];}
602 ///Returns a \ref DivMap class
604 ///This function just returns a \ref DivMap class.
606 template<typename M1, typename M2>
607 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
608 return DivMap<M1, M2>(m1,m2);
611 ///Composition of two maps
613 ///This \ref concepts::ReadMap "read only map" returns the composition of
615 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
619 /// ComposeMap<M1, M2> cm(m1,m2);
621 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
623 ///Its \c Key is inherited from \c M2 and its \c Value is from
625 ///The \c M2::Value must be convertible to \c M1::Key.
626 ///\todo Check the requirements.
628 template <typename M1, typename M2>
629 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
633 typedef MapBase<typename M2::Key, typename M1::Value> Parent;
634 typedef typename Parent::Key Key;
635 typedef typename Parent::Value Value;
638 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
640 typename MapTraits<M1>::ConstReturnValue
641 operator[](Key k) const {return m1[m2[k]];}
643 ///Returns a \ref ComposeMap class
645 ///This function just returns a \ref ComposeMap class.
647 ///\relates ComposeMap
648 template <typename M1, typename M2>
649 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
650 return ComposeMap<M1, M2>(m1,m2);
653 ///Combines of two maps using an STL (binary) functor.
655 ///Combines of two maps using an STL (binary) functor.
658 ///This \ref concepts::ReadMap "read only map" takes two maps and a
659 ///binary functor and returns the composition of
661 ///given maps unsing the functor.
662 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
663 ///and \c f is of \c F,
666 /// CombineMap<M1, M2,F,V> cm(m1,m2,f);
668 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
670 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
671 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
672 ///input parameter of \c F and the return type of \c F must be convertible
674 ///\todo Check the requirements.
676 template<typename M1, typename M2, typename F,
677 typename V = typename F::result_type,
679 class CombineMap : public MapBase<typename M1::Key, V> {
684 typedef MapBase<typename M1::Key, V> Parent;
685 typedef typename Parent::Key Key;
686 typedef typename Parent::Value Value;
689 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
690 : m1(_m1), m2(_m2), f(_f) {};
691 Value operator[](Key k) const {return f(m1[k],m2[k]);}
694 ///Returns a \ref CombineMap class
696 ///This function just returns a \ref CombineMap class.
698 ///Only the first template parameter (the value type) must be given.
700 ///For example if \c m1 and \c m2 are both \c double valued maps, then
702 ///combineMap<double>(m1,m2,std::plus<double>)
704 ///is equivalent with
709 ///\relates CombineMap
710 template<typename M1, typename M2, typename F, typename V>
711 inline CombineMap<M1, M2, F, V>
712 combineMap(const M1& m1,const M2& m2, const F& f) {
713 return CombineMap<M1, M2, F, V>(m1,m2,f);
716 template<typename M1, typename M2, typename F>
717 inline CombineMap<M1, M2, F, typename F::result_type>
718 combineMap(const M1& m1, const M2& m2, const F& f) {
719 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
722 template<typename M1, typename M2, typename K1, typename K2, typename V>
723 inline CombineMap<M1, M2, V (*)(K1, K2), V>
724 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
725 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
728 ///Negative value of a map
730 ///This \ref concepts::ReadMap "read only map" returns the negative
732 ///value returned by the
733 ///given map. Its \c Key and \c Value will be inherited from \c M.
734 ///The unary \c - operator must be defined for \c Value, of course.
737 class NegMap : public MapBase<typename M::Key, typename M::Value> {
740 typedef MapBase<typename M::Key, typename M::Value> Parent;
741 typedef typename Parent::Key Key;
742 typedef typename Parent::Value Value;
745 NegMap(const M &_m) : m(_m) {};
746 Value operator[](Key k) const {return -m[k];}
749 ///Negative value of a map
751 ///This \ref concepts::ReadWriteMap "read-write map" returns the negative
752 ///value of the value returned by the
753 ///given map. Its \c Key and \c Value will be inherited from \c M.
754 ///The unary \c - operator must be defined for \c Value, of course.
757 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
760 typedef MapBase<typename M::Key, typename M::Value> Parent;
761 typedef typename Parent::Key Key;
762 typedef typename Parent::Value Value;
765 NegWriteMap(M &_m) : m(_m) {};
766 Value operator[](Key k) const {return -m[k];}
767 void set(Key k, const Value& v) { m.set(k, -v); }
770 ///Returns a \ref NegMap class
772 ///This function just returns a \ref NegMap class.
774 template <typename M>
775 inline NegMap<M> negMap(const M &m) {
779 template <typename M>
780 inline NegWriteMap<M> negMap(M &m) {
781 return NegWriteMap<M>(m);
784 ///Absolute value of a map
786 ///This \ref concepts::ReadMap "read only map" returns the absolute value
788 ///value returned by the
789 ///given map. Its \c Key and \c Value will be inherited
790 ///from <tt>M</tt>. <tt>Value</tt>
791 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
792 ///operator must be defined for it, of course.
794 ///\bug We need a unified way to handle the situation below:
796 /// struct _UnConvertible {};
797 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
798 /// template<> inline int t_abs<>(int n) {return abs(n);}
799 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
800 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
801 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
802 /// template<> inline double t_abs<>(double n) {return fabs(n);}
803 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
808 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
811 typedef MapBase<typename M::Key, typename M::Value> Parent;
812 typedef typename Parent::Key Key;
813 typedef typename Parent::Value Value;
816 AbsMap(const M &_m) : m(_m) {};
817 Value operator[](Key k) const {
819 return tmp >= 0 ? tmp : -tmp;
824 ///Returns a \ref AbsMap class
826 ///This function just returns a \ref AbsMap class.
829 inline AbsMap<M> absMap(const M &m) {
833 ///Converts an STL style functor to a map
835 ///This \ref concepts::ReadMap "read only map" returns the value
839 ///Template parameters \c K and \c V will become its
840 ///\c Key and \c Value. They must be given explicitely
841 ///because a functor does not provide such typedefs.
843 ///Parameter \c F is the type of the used functor.
847 typename K = typename F::argument_type,
848 typename V = typename F::result_type,
850 class FunctorMap : public MapBase<K, V> {
853 typedef MapBase<K, V> Parent;
854 typedef typename Parent::Key Key;
855 typedef typename Parent::Value Value;
858 FunctorMap(const F &_f) : f(_f) {}
860 Value operator[](Key k) const { return f(k);}
863 ///Returns a \ref FunctorMap class
865 ///This function just returns a \ref FunctorMap class.
867 ///The third template parameter isn't necessary to be given.
868 ///\relates FunctorMap
869 template<typename K, typename V, typename F> inline
870 FunctorMap<F, K, V> functorMap(const F &f) {
871 return FunctorMap<F, K, V>(f);
874 template <typename F> inline
875 FunctorMap<F, typename F::argument_type, typename F::result_type>
876 functorMap(const F &f) {
877 return FunctorMap<F, typename F::argument_type,
878 typename F::result_type>(f);
881 template <typename K, typename V> inline
882 FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
883 return FunctorMap<V (*)(K), K, V>(f);
887 ///Converts a map to an STL style (unary) functor
889 ///This class Converts a map to an STL style (unary) functor.
890 ///that is it provides an <tt>operator()</tt> to read its values.
892 ///For the sake of convenience it also works as
893 ///a ususal \ref concepts::ReadMap "readable map",
894 ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
896 template <typename M>
897 class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
900 typedef MapBase<typename M::Key, typename M::Value> Parent;
901 typedef typename Parent::Key Key;
902 typedef typename Parent::Value Value;
905 typedef typename M::Key argument_type;
907 typedef typename M::Value result_type;
910 MapFunctor(const M &_m) : m(_m) {};
911 ///Returns a value of the map
912 Value operator()(Key k) const {return m[k];}
914 Value operator[](Key k) const {return m[k];}
917 ///Returns a \ref MapFunctor class
919 ///This function just returns a \ref MapFunctor class.
920 ///\relates MapFunctor
922 inline MapFunctor<M> mapFunctor(const M &m) {
923 return MapFunctor<M>(m);
926 ///Applies all map setting operations to two maps
928 ///This map has two \ref concepts::ReadMap "readable map"
929 ///parameters and each read request will be passed just to the
930 ///first map. This class is the just readable map type of the ForkWriteMap.
932 ///The \c Key and \c Value will be inherited from \c M1.
933 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
935 template<typename M1, typename M2>
936 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
940 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
941 typedef typename Parent::Key Key;
942 typedef typename Parent::Value Value;
945 ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
946 Value operator[](Key k) const {return m1[k];}
950 ///Applies all map setting operations to two maps
952 ///This map has two \ref concepts::WriteMap "writable map"
953 ///parameters and each write request will be passed to both of them.
954 ///If \c M1 is also \ref concepts::ReadMap "readable",
955 ///then the read operations will return the
956 ///corresponding values of \c M1.
958 ///The \c Key and \c Value will be inherited from \c M1.
959 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
961 template<typename M1, typename M2>
962 class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
966 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
967 typedef typename Parent::Key Key;
968 typedef typename Parent::Value Value;
971 ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
972 Value operator[](Key k) const {return m1[k];}
973 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
976 ///Returns an \ref ForkMap class
978 ///This function just returns an \ref ForkMap class.
979 ///\todo How to call these type of functions?
982 ///\todo Wrong scope in Doxygen when \c \\relates is used
983 template <typename M1, typename M2>
984 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
985 return ForkMap<M1, M2>(m1,m2);
988 template <typename M1, typename M2>
989 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
990 return ForkWriteMap<M1, M2>(m1,m2);
995 /* ************* BOOL MAPS ******************* */
997 ///Logical 'not' of a map
999 ///This bool \ref concepts::ReadMap "read only map" returns the
1000 ///logical negation of
1001 ///value returned by the
1002 ///given map. Its \c Key and will be inherited from \c M,
1003 ///its Value is <tt>bool</tt>.
1005 template <typename M>
1006 class NotMap : public MapBase<typename M::Key, bool> {
1009 typedef MapBase<typename M::Key, bool> Parent;
1010 typedef typename Parent::Key Key;
1011 typedef typename Parent::Value Value;
1014 NotMap(const M &_m) : m(_m) {};
1015 Value operator[](Key k) const {return !m[k];}
1018 ///Logical 'not' of a map with writing possibility
1020 ///This bool \ref concepts::ReadWriteMap "read-write map" returns the
1021 ///logical negation of value returned by the given map. When it is set,
1022 ///the opposite value is set to the original map.
1023 ///Its \c Key and will be inherited from \c M,
1024 ///its Value is <tt>bool</tt>.
1025 template <typename M>
1026 class NotWriteMap : public MapBase<typename M::Key, bool> {
1029 typedef MapBase<typename M::Key, bool> Parent;
1030 typedef typename Parent::Key Key;
1031 typedef typename Parent::Value Value;
1034 NotWriteMap(M &_m) : m(_m) {};
1035 Value operator[](Key k) const {return !m[k];}
1036 void set(Key k, bool v) { m.set(k, !v); }
1039 ///Returns a \ref NotMap class
1041 ///This function just returns a \ref NotMap class.
1043 template <typename M>
1044 inline NotMap<M> notMap(const M &m) {
1045 return NotMap<M>(m);
1048 template <typename M>
1049 inline NotWriteMap<M> notMap(M &m) {
1050 return NotWriteMap<M>(m);
1053 namespace _maps_bits {
1055 template <typename Value>
1057 typedef Value argument_type;
1058 typedef Value result_type;
1059 Value operator()(const Value& val) const {
1064 template <typename _Iterator, typename Enable = void>
1065 struct IteratorTraits {
1066 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1069 template <typename _Iterator>
1070 struct IteratorTraits<_Iterator,
1071 typename exists<typename _Iterator::container_type>::type>
1073 typedef typename _Iterator::container_type::value_type Value;
1079 /// \brief Writable bool map for store each true assigned elements.
1081 /// Writable bool map to store each true assigned elements. It will
1082 /// copies all the keys set to true to the given iterator.
1084 /// \note The container of the iterator should contain space
1085 /// for each element.
1087 /// The next example shows how can you write the nodes directly
1088 /// to the standard output.
1090 /// typedef IdMap<UGraph, UEdge> UEdgeIdMap;
1091 /// UEdgeIdMap uedgeId(ugraph);
1093 /// typedef MapFunctor<UEdgeIdMap> UEdgeIdFunctor;
1094 /// UEdgeIdFunctor uedgeIdFunctor(uedgeId);
1096 /// StoreBoolMap<ostream_iterator<int>, UEdgeIdFunctor>
1097 /// writerMap(ostream_iterator<int>(cout, " "), uedgeIdFunctor);
1099 /// prim(ugraph, cost, writerMap);
1101 template <typename _Iterator,
1103 _maps_bits::Identity<typename _maps_bits::
1104 IteratorTraits<_Iterator>::Value> >
1105 class StoreBoolMap {
1107 typedef _Iterator Iterator;
1109 typedef typename _Functor::argument_type Key;
1112 typedef _Functor Functor;
1115 StoreBoolMap(Iterator it, const Functor& functor = Functor())
1116 : _begin(it), _end(it), _functor(functor) {}
1118 /// Gives back the given iterator set for the first time.
1119 Iterator begin() const {
1123 /// Gives back the iterator after the last set operation.
1124 Iterator end() const {
1128 /// Setter function of the map
1129 void set(const Key& key, Value value) const {
1131 *_end++ = _functor(key);
1137 mutable Iterator _end;
1141 /// \brief Writable bool map for store each true assigned elements in
1142 /// a back insertable container.
1144 /// Writable bool map for store each true assigned elements in a back
1145 /// insertable container. It will push back all the keys set to true into
1146 /// the container. It can be used to retrieve the items into a standard
1147 /// container. The next example shows how can you store the undirected
1148 /// edges in a vector with prim algorithm.
1151 /// vector<UEdge> span_tree_uedges;
1152 /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges);
1153 /// prim(ugraph, cost, inserter_map);
1155 template <typename Container,
1157 _maps_bits::Identity<typename Container::value_type> >
1158 class BackInserterBoolMap {
1160 typedef typename Container::value_type Key;
1164 BackInserterBoolMap(Container& _container,
1165 const Functor& _functor = Functor())
1166 : container(_container), functor(_functor) {}
1168 /// Setter function of the map
1169 void set(const Key& key, Value value) {
1171 container.push_back(functor(key));
1176 Container& container;
1180 /// \brief Writable bool map for store each true assigned elements in
1181 /// a front insertable container.
1183 /// Writable bool map for store each true assigned elements in a front
1184 /// insertable container. It will push front all the keys set to \c true into
1185 /// the container. For example see the BackInserterBoolMap.
1186 template <typename Container,
1188 _maps_bits::Identity<typename Container::value_type> >
1189 class FrontInserterBoolMap {
1191 typedef typename Container::value_type Key;
1195 FrontInserterBoolMap(Container& _container,
1196 const Functor& _functor = Functor())
1197 : container(_container), functor(_functor) {}
1199 /// Setter function of the map
1200 void set(const Key& key, Value value) {
1202 container.push_front(key);
1207 Container& container;
1211 /// \brief Writable bool map for store each true assigned elements in
1212 /// an insertable container.
1214 /// Writable bool map for store each true assigned elements in an
1215 /// insertable container. It will insert all the keys set to \c true into
1216 /// the container. If you want to store the cut edges of the strongly
1217 /// connected components in a set you can use the next code:
1220 /// set<Edge> cut_edges;
1221 /// InserterBoolMap<set<Edge> > inserter_map(cut_edges);
1222 /// stronglyConnectedCutEdges(graph, cost, inserter_map);
1224 template <typename Container,
1226 _maps_bits::Identity<typename Container::value_type> >
1227 class InserterBoolMap {
1229 typedef typename Container::value_type Key;
1233 InserterBoolMap(Container& _container, typename Container::iterator _it,
1234 const Functor& _functor = Functor())
1235 : container(_container), it(_it), functor(_functor) {}
1238 InserterBoolMap(Container& _container, const Functor& _functor = Functor())
1239 : container(_container), it(_container.end()), functor(_functor) {}
1241 /// Setter function of the map
1242 void set(const Key& key, Value value) {
1244 it = container.insert(it, key);
1250 Container& container;
1251 typename Container::iterator it;
1255 /// \brief Fill the true set elements with a given value.
1257 /// Writable bool map to fill the elements set to \c true with a given value.
1258 /// The value can set
1261 /// The next code finds the connected components of the undirected graph
1262 /// and stores it in the \c comp map:
1264 /// typedef UGraph::NodeMap<int> ComponentMap;
1265 /// ComponentMap comp(ugraph);
1266 /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap;
1267 /// ComponentFillerMap filler(comp, 0);
1269 /// Dfs<UGraph>::DefProcessedMap<ComponentFillerMap>::Create dfs(ugraph);
1270 /// dfs.processedMap(filler);
1272 /// for (NodeIt it(ugraph); it != INVALID; ++it) {
1273 /// if (!dfs.reached(it)) {
1274 /// dfs.addSource(it);
1276 /// ++filler.fillValue();
1281 template <typename Map>
1284 typedef typename Map::Key Key;
1288 FillBoolMap(Map& _map, const typename Map::Value& _fill)
1289 : map(_map), fill(_fill) {}
1292 FillBoolMap(Map& _map)
1293 : map(_map), fill() {}
1295 /// Gives back the current fill value
1296 const typename Map::Value& fillValue() const {
1300 /// Gives back the current fill value
1301 typename Map::Value& fillValue() {
1305 /// Sets the current fill value
1306 void fillValue(const typename Map::Value& _fill) {
1310 /// Setter function of the map
1311 void set(const Key& key, Value value) {
1319 typename Map::Value fill;
1323 /// \brief Writable bool map which stores for each true assigned elements
1324 /// the setting order number.
1326 /// Writable bool map which stores for each true assigned elements
1327 /// the setting order number. It make easy to calculate the leaving
1328 /// order of the nodes in the \ref dfs "Dfs" algorithm.
1331 /// typedef Graph::NodeMap<int> OrderMap;
1332 /// OrderMap order(graph);
1333 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1334 /// OrderSetterMap setter(order);
1335 /// Dfs<Graph>::DefProcessedMap<OrderSetterMap>::Create dfs(graph);
1336 /// dfs.processedMap(setter);
1338 /// for (NodeIt it(graph); it != INVALID; ++it) {
1339 /// if (!dfs.reached(it)) {
1340 /// dfs.addSource(it);
1346 /// The discovering order can be stored a little harder because the
1347 /// ReachedMap should be readable in the dfs algorithm but the setting
1348 /// order map is not readable. Now we should use the fork map:
1351 /// typedef Graph::NodeMap<int> OrderMap;
1352 /// OrderMap order(graph);
1353 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1354 /// OrderSetterMap setter(order);
1355 /// typedef Graph::NodeMap<bool> StoreMap;
1356 /// StoreMap store(graph);
1358 /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1359 /// ReachedMap reached(store, setter);
1361 /// Dfs<Graph>::DefReachedMap<ReachedMap>::Create dfs(graph);
1362 /// dfs.reachedMap(reached);
1364 /// for (NodeIt it(graph); it != INVALID; ++it) {
1365 /// if (!dfs.reached(it)) {
1366 /// dfs.addSource(it);
1371 template <typename Map>
1372 class SettingOrderBoolMap {
1374 typedef typename Map::Key Key;
1378 SettingOrderBoolMap(Map& _map)
1379 : map(_map), counter(0) {}
1381 /// Number of set operations.
1386 /// Setter function of the map
1387 void set(const Key& key, Value value) {
1389 map.set(key, counter++);
1401 #endif // LEMON_MAPS_H