bezier.h is no longer in the repository.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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 concept/,
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 concept::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);
292 ///This \ref concept::ReadMap "read only map" returns the sum of the two
293 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
294 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
296 template<typename M1, typename M2>
297 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
302 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
303 typedef typename Parent::Key Key;
304 typedef typename Parent::Value Value;
307 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
308 Value operator[](Key k) const {return m1[k]+m2[k];}
311 ///Returns an \ref AddMap class
313 ///This function just returns an \ref AddMap class.
314 ///\todo How to call these type of functions?
317 ///\todo Wrong scope in Doxygen when \c \\relates is used
318 template<typename M1, typename M2>
319 inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
320 return AddMap<M1, M2>(m1,m2);
323 ///Shift a map with a constant.
325 ///This \ref concept::ReadMap "read only map" returns the sum of the
326 ///given map and a constant value.
327 ///Its \c Key and \c Value is inherited from \c M.
331 /// ShiftMap<X> sh(x,v);
333 ///is equivalent with
335 /// ConstMap<X::Key, X::Value> c_tmp(v);
336 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
338 template<typename M, typename C = typename M::Value>
339 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
343 typedef MapBase<typename M::Key, typename M::Value> Parent;
344 typedef typename Parent::Key Key;
345 typedef typename Parent::Value Value;
350 ///\param _m is the undelying map
351 ///\param _v is the shift value
352 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
353 Value operator[](Key k) const {return m[k] + v;}
356 ///Shift a map with a constant.
358 ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the
359 ///given map and a constant value. It makes also possible to write the map.
360 ///Its \c Key and \c Value is inherited from \c M.
364 /// ShiftMap<X> sh(x,v);
366 ///is equivalent with
368 /// ConstMap<X::Key, X::Value> c_tmp(v);
369 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
371 template<typename M, typename C = typename M::Value>
372 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
376 typedef MapBase<typename M::Key, typename M::Value> Parent;
377 typedef typename Parent::Key Key;
378 typedef typename Parent::Value Value;
383 ///\param _m is the undelying map
384 ///\param _v is the shift value
385 ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
386 Value operator[](Key k) const {return m[k] + v;}
387 void set(Key k, const Value& c) { m.set(k, c - v); }
390 ///Returns an \ref ShiftMap class
392 ///This function just returns an \ref ShiftMap class.
394 ///\todo A better name is required.
395 template<typename M, typename C>
396 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
397 return ShiftMap<M, C>(m,v);
400 template<typename M, typename C>
401 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
402 return ShiftWriteMap<M, C>(m,v);
405 ///Difference of two maps
407 ///This \ref concept::ReadMap "read only map" returns the difference
408 ///of the values of the two
409 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
410 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
412 template<typename M1, typename M2>
413 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
417 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
418 typedef typename Parent::Key Key;
419 typedef typename Parent::Value Value;
422 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
423 Value operator[](Key k) const {return m1[k]-m2[k];}
426 ///Returns a \ref SubMap class
428 ///This function just returns a \ref SubMap class.
431 template<typename M1, typename M2>
432 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
433 return SubMap<M1, M2>(m1, m2);
436 ///Product of two maps
438 ///This \ref concept::ReadMap "read only map" returns the product of the
441 ///maps. Its \c Key and \c Value will be inherited from \c M1.
442 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
444 template<typename M1, typename M2>
445 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
449 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
450 typedef typename Parent::Key Key;
451 typedef typename Parent::Value Value;
454 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
455 Value operator[](Key k) const {return m1[k]*m2[k];}
458 ///Returns a \ref MulMap class
460 ///This function just returns a \ref MulMap class.
462 template<typename M1, typename M2>
463 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
464 return MulMap<M1, M2>(m1,m2);
467 ///Scales a maps with a constant.
469 ///This \ref concept::ReadMap "read only map" returns the value of the
470 ///given map multiplied from the left side with a constant value.
471 ///Its \c Key and \c Value is inherited from \c M.
475 /// ScaleMap<X> sc(x,v);
477 ///is equivalent with
479 /// ConstMap<X::Key, X::Value> c_tmp(v);
480 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
482 template<typename M, typename C = typename M::Value>
483 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
487 typedef MapBase<typename M::Key, typename M::Value> Parent;
488 typedef typename Parent::Key Key;
489 typedef typename Parent::Value Value;
494 ///\param _m is the undelying map
495 ///\param _v is the scaling value
496 ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
497 Value operator[](Key k) const {return v * m[k];}
500 ///Scales a maps with a constant.
502 ///This \ref concept::ReadWriteMap "read-write map" returns the value of the
503 ///given map multiplied from the left side with a constant value. It can
504 ///be used as write map also if the given multiplier is not zero.
505 ///Its \c Key and \c Value is inherited from \c M.
506 template<typename M, typename C = typename M::Value>
507 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
511 typedef MapBase<typename M::Key, typename M::Value> Parent;
512 typedef typename Parent::Key Key;
513 typedef typename Parent::Value Value;
518 ///\param _m is the undelying map
519 ///\param _v is the scaling value
520 ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
521 Value operator[](Key k) const {return v * m[k];}
522 void set(Key k, const Value& c) { m.set(k, c / v);}
525 ///Returns an \ref ScaleMap class
527 ///This function just returns an \ref ScaleMap class.
529 ///\todo A better name is required.
530 template<typename M, typename C>
531 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
532 return ScaleMap<M, C>(m,v);
535 template<typename M, typename C>
536 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
537 return ScaleWriteMap<M, C>(m,v);
540 ///Quotient of two maps
542 ///This \ref concept::ReadMap "read only map" returns the quotient of the
544 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
545 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
547 template<typename M1, typename M2>
548 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
552 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
553 typedef typename Parent::Key Key;
554 typedef typename Parent::Value Value;
557 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
558 Value operator[](Key k) const {return m1[k]/m2[k];}
561 ///Returns a \ref DivMap class
563 ///This function just returns a \ref DivMap class.
565 template<typename M1, typename M2>
566 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
567 return DivMap<M1, M2>(m1,m2);
570 ///Composition of two maps
572 ///This \ref concept::ReadMap "read only map" returns the composition of
574 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
578 /// ComposeMap<M1, M2> cm(m1,m2);
580 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
582 ///Its \c Key is inherited from \c M2 and its \c Value is from
584 ///The \c M2::Value must be convertible to \c M1::Key.
585 ///\todo Check the requirements.
587 template <typename M1, typename M2>
588 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
592 typedef MapBase<typename M2::Key, typename M1::Value> Parent;
593 typedef typename Parent::Key Key;
594 typedef typename Parent::Value Value;
597 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
599 typename MapTraits<M1>::ConstReturnValue
600 operator[](Key k) const {return m1[m2[k]];}
602 ///Returns a \ref ComposeMap class
604 ///This function just returns a \ref ComposeMap class.
606 ///\relates ComposeMap
607 template <typename M1, typename M2>
608 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
609 return ComposeMap<M1, M2>(m1,m2);
612 ///Combines of two maps using an STL (binary) functor.
614 ///Combines of two maps using an STL (binary) functor.
617 ///This \ref concept::ReadMap "read only map" takes two maps and a
618 ///binary functor and returns the composition of
620 ///given maps unsing the functor.
621 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
622 ///and \c f is of \c F,
625 /// CombineMap<M1, M2,F,V> cm(m1,m2,f);
627 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
629 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
630 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
631 ///input parameter of \c F and the return type of \c F must be convertible
633 ///\todo Check the requirements.
635 template<typename M1, typename M2, typename F,
636 typename V = typename F::result_type,
638 class CombineMap : public MapBase<typename M1::Key, V> {
643 typedef MapBase<typename M1::Key, V> Parent;
644 typedef typename Parent::Key Key;
645 typedef typename Parent::Value Value;
648 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
649 : m1(_m1), m2(_m2), f(_f) {};
650 Value operator[](Key k) const {return f(m1[k],m2[k]);}
653 ///Returns a \ref CombineMap class
655 ///This function just returns a \ref CombineMap class.
657 ///Only the first template parameter (the value type) must be given.
659 ///For example if \c m1 and \c m2 are both \c double valued maps, then
661 ///combineMap<double>(m1,m2,std::plus<double>)
663 ///is equivalent with
668 ///\relates CombineMap
669 template<typename M1, typename M2, typename F, typename V>
670 inline CombineMap<M1, M2, F, V>
671 combineMap(const M1& m1,const M2& m2, const F& f) {
672 return CombineMap<M1, M2, F, V>(m1,m2,f);
675 template<typename M1, typename M2, typename F>
676 inline CombineMap<M1, M2, F, typename F::result_type>
677 combineMap(const M1& m1, const M2& m2, const F& f) {
678 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
681 template<typename M1, typename M2, typename K1, typename K2, typename V>
682 inline CombineMap<M1, M2, V (*)(K1, K2), V>
683 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
684 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
687 ///Negative value of a map
689 ///This \ref concept::ReadMap "read only map" returns the negative
691 ///value returned by the
692 ///given map. Its \c Key and \c Value will be inherited from \c M.
693 ///The unary \c - operator must be defined for \c Value, of course.
696 class NegMap : public MapBase<typename M::Key, typename M::Value> {
699 typedef MapBase<typename M::Key, typename M::Value> Parent;
700 typedef typename Parent::Key Key;
701 typedef typename Parent::Value Value;
704 NegMap(const M &_m) : m(_m) {};
705 Value operator[](Key k) const {return -m[k];}
708 ///Negative value of a map
710 ///This \ref concept::ReadWriteMap "read-write map" returns the negative
711 ///value of the value returned by the
712 ///given map. Its \c Key and \c Value will be inherited from \c M.
713 ///The unary \c - operator must be defined for \c Value, of course.
716 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
719 typedef MapBase<typename M::Key, typename M::Value> Parent;
720 typedef typename Parent::Key Key;
721 typedef typename Parent::Value Value;
724 NegWriteMap(M &_m) : m(_m) {};
725 Value operator[](Key k) const {return -m[k];}
726 void set(Key k, const Value& v) { m.set(k, -v); }
729 ///Returns a \ref NegMap class
731 ///This function just returns a \ref NegMap class.
733 template <typename M>
734 inline NegMap<M> negMap(const M &m) {
738 template <typename M>
739 inline NegWriteMap<M> negMap(M &m) {
740 return NegWriteMap<M>(m);
743 ///Absolute value of a map
745 ///This \ref concept::ReadMap "read only map" returns the absolute value
747 ///value returned by the
748 ///given map. Its \c Key and \c Value will be inherited
749 ///from <tt>M</tt>. <tt>Value</tt>
750 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
751 ///operator must be defined for it, of course.
753 ///\bug We need a unified way to handle the situation below:
755 /// struct _UnConvertible {};
756 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
757 /// template<> inline int t_abs<>(int n) {return abs(n);}
758 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
759 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
760 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
761 /// template<> inline double t_abs<>(double n) {return fabs(n);}
762 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
767 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
770 typedef MapBase<typename M::Key, typename M::Value> Parent;
771 typedef typename Parent::Key Key;
772 typedef typename Parent::Value Value;
775 AbsMap(const M &_m) : m(_m) {};
776 Value operator[](Key k) const {
778 return tmp >= 0 ? tmp : -tmp;
783 ///Returns a \ref AbsMap class
785 ///This function just returns a \ref AbsMap class.
788 inline AbsMap<M> absMap(const M &m) {
792 ///Converts an STL style functor to a map
794 ///This \ref concept::ReadMap "read only map" returns the value
798 ///Template parameters \c K and \c V will become its
799 ///\c Key and \c Value. They must be given explicitely
800 ///because a functor does not provide such typedefs.
802 ///Parameter \c F is the type of the used functor.
806 typename K = typename F::argument_type,
807 typename V = typename F::result_type,
809 class FunctorMap : public MapBase<K, V> {
812 typedef MapBase<K, V> Parent;
813 typedef typename Parent::Key Key;
814 typedef typename Parent::Value Value;
817 FunctorMap(const F &_f) : f(_f) {}
819 Value operator[](Key k) const { return f(k);}
822 ///Returns a \ref FunctorMap class
824 ///This function just returns a \ref FunctorMap class.
826 ///The third template parameter isn't necessary to be given.
827 ///\relates FunctorMap
828 template<typename K, typename V, typename F> inline
829 FunctorMap<F, K, V> functorMap(const F &f) {
830 return FunctorMap<F, K, V>(f);
833 template <typename F> inline
834 FunctorMap<F, typename F::argument_type, typename F::result_type>
835 functorMap(const F &f) {
836 return FunctorMap<F, typename F::argument_type,
837 typename F::result_type>(f);
840 template <typename K, typename V> inline
841 FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
842 return FunctorMap<V (*)(K), K, V>(f);
846 ///Converts a map to an STL style (unary) functor
848 ///This class Converts a map to an STL style (unary) functor.
849 ///that is it provides an <tt>operator()</tt> to read its values.
851 ///For the sake of convenience it also works as
852 ///a ususal \ref concept::ReadMap "readable map",
853 ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
855 template <typename M>
856 class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
859 typedef MapBase<typename M::Key, typename M::Value> Parent;
860 typedef typename Parent::Key Key;
861 typedef typename Parent::Value Value;
864 typedef typename M::Key argument_type;
866 typedef typename M::Value result_type;
869 MapFunctor(const M &_m) : m(_m) {};
870 ///Returns a value of the map
871 Value operator()(Key k) const {return m[k];}
873 Value operator[](Key k) const {return m[k];}
876 ///Returns a \ref MapFunctor class
878 ///This function just returns a \ref MapFunctor class.
879 ///\relates MapFunctor
881 inline MapFunctor<M> mapFunctor(const M &m) {
882 return MapFunctor<M>(m);
885 ///Applies all map setting operations to two maps
887 ///This map has two \ref concept::ReadMap "readable map"
888 ///parameters and each read request will be passed just to the
889 ///first map. This class is the just readable map type of the ForkWriteMap.
891 ///The \c Key and \c Value will be inherited from \c M1.
892 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
894 template<typename M1, typename M2>
895 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
899 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
900 typedef typename Parent::Key Key;
901 typedef typename Parent::Value Value;
904 ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
905 Value operator[](Key k) const {return m1[k];}
909 ///Applies all map setting operations to two maps
911 ///This map has two \ref concept::WriteMap "writable map"
912 ///parameters and each write request will be passed to both of them.
913 ///If \c M1 is also \ref concept::ReadMap "readable",
914 ///then the read operations will return the
915 ///corresponding values of \c M1.
917 ///The \c Key and \c Value will be inherited from \c M1.
918 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
920 template<typename M1, typename M2>
921 class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
925 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
926 typedef typename Parent::Key Key;
927 typedef typename Parent::Value Value;
930 ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
931 Value operator[](Key k) const {return m1[k];}
932 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
935 ///Returns an \ref ForkMap class
937 ///This function just returns an \ref ForkMap class.
938 ///\todo How to call these type of functions?
941 ///\todo Wrong scope in Doxygen when \c \\relates is used
942 template <typename M1, typename M2>
943 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
944 return ForkMap<M1, M2>(m1,m2);
947 template <typename M1, typename M2>
948 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
949 return ForkWriteMap<M1, M2>(m1,m2);
954 /* ************* BOOL MAPS ******************* */
956 ///Logical 'not' of a map
958 ///This bool \ref concept::ReadMap "read only map" returns the
959 ///logical negation of
960 ///value returned by the
961 ///given map. Its \c Key and will be inherited from \c M,
962 ///its Value is <tt>bool</tt>.
964 template <typename M>
965 class NotMap : public MapBase<typename M::Key, bool> {
968 typedef MapBase<typename M::Key, bool> Parent;
969 typedef typename Parent::Key Key;
970 typedef typename Parent::Value Value;
973 NotMap(const M &_m) : m(_m) {};
974 Value operator[](Key k) const {return !m[k];}
977 ///Logical 'not' of a map with writing possibility
979 ///This bool \ref concept::ReadWriteMap "read-write map" returns the
980 ///logical negation of value returned by the given map. It is setted
981 ///then the negation of the value be setted to the original map.
982 ///Its \c Key and will be inherited from \c M,
983 ///its Value is <tt>bool</tt>.
984 template <typename M>
985 class NotWriteMap : public MapBase<typename M::Key, bool> {
988 typedef MapBase<typename M::Key, bool> Parent;
989 typedef typename Parent::Key Key;
990 typedef typename Parent::Value Value;
993 NotWriteMap(M &_m) : m(_m) {};
994 Value operator[](Key k) const {return !m[k];}
995 void set(Key k, bool v) { m.set(k, !v); }
998 ///Returns a \ref NotMap class
1000 ///This function just returns a \ref NotMap class.
1002 template <typename M>
1003 inline NotMap<M> notMap(const M &m) {
1004 return NotMap<M>(m);
1007 template <typename M>
1008 inline NotWriteMap<M> notMap(M &m) {
1009 return NotWriteMap<M>(m);
1012 namespace _maps_bits {
1013 template <typename Value>
1015 typedef Value argument_type;
1016 typedef Value result_type;
1017 Value operator()(const Value& val) {
1024 /// \brief Writable bool map for store each true assigned elements.
1026 /// Writable bool map for store each true assigned elements. It will
1027 /// copies all the true setted keys to the given iterator.
1029 /// \note The container of the iterator should contain space
1030 /// for each element.
1032 /// The next example shows how can you write the nodes directly
1033 /// to the standard output.
1035 /// typedef IdMap<UGraph, UEdge> UEdgeIdMap;
1036 /// UEdgeIdMap uedgeId(ugraph);
1038 /// typedef MapFunctor<UEdgeIdMap> UEdgeIdFunctor;
1039 /// UEdgeIdFunctor uedgeIdFunctor(uedgeId);
1041 /// StoreBoolMap<ostream_iterator<int>, UEdgeIdFunctor>
1042 /// writerMap(ostream_iterator<int>(cout, " "), uedgeIdFunctor);
1044 /// prim(ugraph, cost, writerMap);
1046 template <typename _Iterator,
1048 _maps_bits::Identity<typename std::iterator_traits<_Iterator>::value_type> >
1049 class StoreBoolMap {
1051 typedef _Iterator Iterator;
1053 typedef typename _Functor::argument_type Key;
1056 typedef _Functor Functor;
1059 StoreBoolMap(Iterator it, const Functor& functor = Functor())
1060 : _begin(it), _end(it), _functor(functor) {}
1062 /// Gives back the given first setted iterator.
1063 Iterator begin() const {
1067 /// Gives back the iterator after the last setted.
1068 Iterator end() const {
1072 /// Setter function of the map
1073 void set(const Key& key, Value value) {
1075 *_end++ = _functor(key);
1080 Iterator _begin, _end;
1084 /// \brief Writable bool map for store each true assigned elements in
1085 /// a back insertable container.
1087 /// Writable bool map for store each true assigned elements in a back
1088 /// insertable container. It will push back all the true setted keys into
1089 /// the container. It can be used to retrieve the items into a standard
1090 /// container. The next example shows how can you store the undirected
1091 /// edges in a vector with prim algorithm.
1094 /// vector<UEdge> span_tree_uedges;
1095 /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges);
1096 /// prim(ugraph, cost, inserter_map);
1098 template <typename Container,
1100 _maps_bits::Identity<typename Container::value_type> >
1101 class BackInserterBoolMap {
1103 typedef typename Container::value_type Key;
1107 BackInserterBoolMap(Container& _container,
1108 const Functor& _functor = Functor())
1109 : container(_container), functor(_functor) {}
1111 /// Setter function of the map
1112 void set(const Key& key, Value value) {
1114 container.push_back(functor(key));
1119 Container& container;
1123 /// \brief Writable bool map for store each true assigned elements in
1124 /// a front insertable container.
1126 /// Writable bool map for store each true assigned elements in a front
1127 /// insertable container. It will push front all the true setted keys into
1128 /// the container. For example see the BackInserterBoolMap.
1129 template <typename Container,
1131 _maps_bits::Identity<typename Container::value_type> >
1132 class FrontInserterBoolMap {
1134 typedef typename Container::value_type Key;
1138 FrontInserterBoolMap(Container& _container,
1139 const Functor& _functor = Functor())
1140 : container(_container), functor(_functor) {}
1142 /// Setter function of the map
1143 void set(const Key& key, Value value) {
1145 container.push_front(key);
1150 Container& container;
1154 /// \brief Writable bool map for store each true assigned elements in
1155 /// an insertable container.
1157 /// Writable bool map for store each true assigned elements in an
1158 /// insertable container. It will insert all the true setted keys into
1159 /// the container. If you want to store the cut edges of the strongly
1160 /// connected components in a set you can use the next code:
1163 /// set<Edge> cut_edges;
1164 /// InserterBoolMap<set<Edge> > inserter_map(cut_edges);
1165 /// stronglyConnectedCutEdges(graph, cost, inserter_map);
1167 template <typename Container,
1169 _maps_bits::Identity<typename Container::value_type> >
1170 class InserterBoolMap {
1172 typedef typename Container::value_type Key;
1176 InserterBoolMap(Container& _container, typename Container::iterator _it,
1177 const Functor& _functor = Functor())
1178 : container(_container), it(_it), functor(_functor) {}
1181 InserterBoolMap(Container& _container, const Functor& _functor = Functor())
1182 : container(_container), it(_container.end()), functor(_functor) {}
1184 /// Setter function of the map
1185 void set(const Key& key, Value value) {
1187 it = container.insert(it, key);
1193 Container& container;
1194 typename Container::iterator it;
1198 /// \brief Fill the true setted elements with a given value.
1200 /// Writable bool map for fill the true setted elements with a given value.
1201 /// The value can be setted
1204 /// The next code finds the connected components of the undirected graph
1205 /// and stores it in the \c comp map:
1207 /// typedef UGraph::NodeMap<int> ComponentMap;
1208 /// ComponentMap comp(ugraph);
1209 /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap;
1210 /// ComponentFillerMap filler(comp, 0);
1212 /// Dfs<UGraph>::DefProcessedMap<ComponentFillerMap>::Create dfs(ugraph);
1213 /// dfs.processedMap(filler);
1215 /// for (NodeIt it(ugraph); it != INVALID; ++it) {
1216 /// if (!dfs.reached(it)) {
1217 /// dfs.addSource(it);
1219 /// ++filler.fillValue();
1224 template <typename Map>
1227 typedef typename Map::Key Key;
1231 FillBoolMap(Map& _map, const typename Map::Value& _fill)
1232 : map(_map), fill(_fill) {}
1235 FillBoolMap(Map& _map)
1236 : map(_map), fill() {}
1238 /// Gives back the current fill value
1239 const typename Map::Value& fillValue() const {
1243 /// Gives back the current fill value
1244 typename Map::Value& fillValue() {
1248 /// Sets the current fill value
1249 void fillValue(const typename Map::Value& _fill) {
1253 /// Setter function of the map
1254 void set(const Key& key, Value value) {
1262 typename Map::Value fill;
1266 /// \brief Writable bool map which stores for each true assigned elements
1267 /// the setting order number.
1269 /// Writable bool map which stores for each true assigned elements
1270 /// the setting order number. It make easy to calculate the leaving
1271 /// order of the nodes in the \ref dfs "Dfs" algorithm.
1274 /// typedef Graph::NodeMap<int> OrderMap;
1275 /// OrderMap order(graph);
1276 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1277 /// OrderSetterMap setter(order);
1278 /// Dfs<Graph>::DefProcessedMap<OrderSetterMap>::Create dfs(graph);
1279 /// dfs.processedMap(setter);
1281 /// for (NodeIt it(graph); it != INVALID; ++it) {
1282 /// if (!dfs.reached(it)) {
1283 /// dfs.addSource(it);
1289 /// The discovering order can be stored a little harder because the
1290 /// ReachedMap should be readable in the dfs algorithm but the setting
1291 /// order map is not readable. Now we should use the fork map:
1294 /// typedef Graph::NodeMap<int> OrderMap;
1295 /// OrderMap order(graph);
1296 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1297 /// OrderSetterMap setter(order);
1298 /// typedef Graph::NodeMap<bool> StoreMap;
1299 /// StoreMap store(graph);
1301 /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1302 /// ReachedMap reached(store, setter);
1304 /// Dfs<Graph>::DefReachedMap<ReachedMap>::Create dfs(graph);
1305 /// dfs.reachedMap(reached);
1307 /// for (NodeIt it(graph); it != INVALID; ++it) {
1308 /// if (!dfs.reached(it)) {
1309 /// dfs.addSource(it);
1314 template <typename Map>
1315 class SettingOrderBoolMap {
1317 typedef typename Map::Key Key;
1321 SettingOrderBoolMap(Map& _map)
1322 : map(_map), counter(0) {}
1324 /// Number of setted keys.
1329 /// Setter function of the map
1330 void set(const Key& key, Value value) {
1332 map.set(key, counter++);
1344 #endif // LEMON_MAPS_H