Shape keeping movement is implemented, at last. Many thanks to Alpar.
2 * lemon/maps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
20 #include <lemon/utility.h>
25 ///\brief Miscellaneous property maps
27 ///\todo This file has the same name as the concept file in concept/,
28 /// and this is not easily detectable in docs...
37 /// Base class of maps.
39 /// Base class of maps.
40 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
41 template<typename K, typename T>
50 /// Null map. (a.k.a. DoNothingMap)
52 /// If you have to provide a map only for its type definitions,
53 /// or if you have to provide a writable map, but
54 /// data written to it will sent to <tt>/dev/null</tt>...
55 template<typename K, typename T>
56 class NullMap : public MapBase<K, T> {
58 typedef MapBase<K, T> Parent;
59 typedef typename Parent::Key Key;
60 typedef typename Parent::Value Value;
62 /// Gives back a default constructed element.
63 T operator[](const K&) const { return T(); }
64 /// Absorbs the value.
65 void set(const K&, const T&) {}
68 template <typename K, typename V>
69 NullMap<K, V> nullMap() {
70 return NullMap<K, V>();
76 /// This is a readable map which assigns a specified value to each key.
77 /// In other aspects it is equivalent to the \ref NullMap.
78 /// \todo set could be used to set the value.
79 template<typename K, typename T>
80 class ConstMap : public MapBase<K, T> {
85 typedef MapBase<K, T> Parent;
86 typedef typename Parent::Key Key;
87 typedef typename Parent::Value Value;
89 /// Default constructor
91 /// The value of the map will be uninitialized.
92 /// (More exactly it will be default constructed.)
96 /// \param _v The initial value of the map.
98 ConstMap(const T &_v) : v(_v) {}
100 T operator[](const K&) const { return v; }
101 void set(const K&, const T&) {}
103 template<typename T1>
105 typedef ConstMap<K, T1> other;
108 template<typename T1>
109 ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
112 ///Returns a \ref ConstMap class
114 ///This function just returns a \ref ConstMap class.
116 template<typename K, typename V>
117 inline ConstMap<K, V> constMap(const V &v) {
118 return ConstMap<K, V>(v);
122 //\todo to document later
123 template<typename T, T v>
126 //\todo to document later
127 template<typename K, typename V, V v>
128 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
130 typedef MapBase<K, V> Parent;
131 typedef typename Parent::Key Key;
132 typedef typename Parent::Value Value;
135 V operator[](const K&) const { return v; }
136 void set(const K&, const V&) { }
139 ///Returns a \ref ConstMap class
141 ///This function just returns a \ref ConstMap class.
143 template<typename K, typename V, V v>
144 inline ConstMap<K, Const<V, v> > constMap() {
145 return ConstMap<K, Const<V, v> >();
148 /// \c std::map wrapper
150 /// This is essentially a wrapper for \c std::map. With addition that
151 /// you can specify a default value different from \c Value() .
153 /// \todo Provide allocator parameter...
154 template <typename K, typename T, typename Compare = std::less<K> >
155 class StdMap : public std::map<K, T, Compare> {
156 typedef std::map<K, T, Compare> parent;
158 typedef typename parent::value_type PairType;
166 typedef T& Reference;
168 typedef const T& ConstReference;
172 /// Constructor with specified default value
173 StdMap(const T& _v) : v(_v) {}
175 /// \brief Constructs the map from an appropriate std::map.
177 /// \warning Inefficient: copies the content of \c m !
178 StdMap(const parent &m) : parent(m) {}
179 /// \brief Constructs the map from an appropriate std::map, and explicitly
180 /// specifies a default value.
182 /// \warning Inefficient: copies the content of \c m !
183 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
185 template<typename T1, typename Comp1>
186 StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) {
190 Reference operator[](const Key &k) {
191 return insert(PairType(k,v)).first -> second;
194 ConstReference operator[](const Key &k) const {
195 typename parent::iterator i = lower_bound(k);
196 if (i == parent::end() || parent::key_comp()(k, (*i).first))
200 void set(const Key &k, const T &t) {
201 parent::operator[](k) = t;
204 /// Changes the default value of the map.
205 /// \return Returns the previous default value.
207 /// \warning The value of some keys (which has already been queried, but
208 /// the value has been unchanged from the default) may change!
209 T setDefault(const T &_v) { T old=v; v=_v; return old; }
211 template<typename T1>
213 typedef StdMap<Key, T1,Compare> other;
219 /// \addtogroup map_adaptors
222 /// \brief Identity mapping.
224 /// This mapping gives back the given key as value without any
226 template <typename T>
227 class IdentityMap : public MapBase<T, T> {
229 typedef MapBase<T, T> Parent;
230 typedef typename Parent::Key Key;
231 typedef typename Parent::Value Value;
233 const T& operator[](const T& t) const {
238 ///Returns an \ref IdentityMap class
240 ///This function just returns an \ref IdentityMap class.
241 ///\relates IdentityMap
243 inline IdentityMap<T> identityMap() {
244 return IdentityMap<T>();
248 ///Convert the \c Value of a map to another type.
250 ///This \ref concept::ReadMap "read only map"
251 ///converts the \c Value of a maps to type \c T.
252 ///Its \c Key is inherited from \c M.
253 template <typename M, typename T>
254 class ConvertMap : public MapBase<typename M::Key, T> {
257 typedef MapBase<typename M::Key, T> Parent;
258 typedef typename Parent::Key Key;
259 typedef typename Parent::Value Value;
264 ///\param _m is the underlying map
265 ConvertMap(const M &_m) : m(_m) {};
267 /// \brief The subscript operator.
269 /// The subscript operator.
271 /// \return The target of the edge
272 Value operator[](const Key& k) const {return m[k];}
275 ///Returns an \ref ConvertMap class
277 ///This function just returns an \ref ConvertMap class.
278 ///\relates ConvertMap
279 ///\todo The order of the template parameters are changed.
280 template<typename T, typename M>
281 inline ConvertMap<M, T> convertMap(const M &m) {
282 return ConvertMap<M, T>(m);
287 ///This \ref concept::ReadMap "read only map" returns the sum of the two
288 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
289 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
291 template<typename M1, typename M2>
292 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
297 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
298 typedef typename Parent::Key Key;
299 typedef typename Parent::Value Value;
302 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
303 Value operator[](Key k) const {return m1[k]+m2[k];}
306 ///Returns an \ref AddMap class
308 ///This function just returns an \ref AddMap class.
309 ///\todo How to call these type of functions?
312 ///\todo Wrong scope in Doxygen when \c \\relates is used
313 template<typename M1, typename M2>
314 inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
315 return AddMap<M1, M2>(m1,m2);
318 ///Shift a map with a constant.
320 ///This \ref concept::ReadMap "read only map" returns the sum of the
321 ///given map and a constant value.
322 ///Its \c Key and \c Value is inherited from \c M.
326 /// ShiftMap<X> sh(x,v);
328 ///is equivalent with
330 /// ConstMap<X::Key, X::Value> c_tmp(v);
331 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
333 template<typename M, typename C = typename M::Value>
334 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
338 typedef MapBase<typename M::Key, typename M::Value> Parent;
339 typedef typename Parent::Key Key;
340 typedef typename Parent::Value Value;
345 ///\param _m is the undelying map
346 ///\param _v is the shift value
347 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
348 Value operator[](Key k) const {return m[k] + v;}
351 ///Returns an \ref ShiftMap class
353 ///This function just returns an \ref ShiftMap class.
355 ///\todo A better name is required.
356 template<typename M, typename C>
357 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
358 return ShiftMap<M, C>(m,v);
361 ///Difference of two maps
363 ///This \ref concept::ReadMap "read only map" returns the difference
364 ///of the values of the two
365 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
366 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
368 template<typename M1, typename M2>
369 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
373 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
374 typedef typename Parent::Key Key;
375 typedef typename Parent::Value Value;
378 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
379 Value operator[](Key k) const {return m1[k]-m2[k];}
382 ///Returns a \ref SubMap class
384 ///This function just returns a \ref SubMap class.
387 template<typename M1, typename M2>
388 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
389 return SubMap<M1, M2>(m1, m2);
392 ///Product of two maps
394 ///This \ref concept::ReadMap "read only map" returns the product of the
397 ///maps. Its \c Key and \c Value will be inherited from \c M1.
398 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
400 template<typename M1, typename M2>
401 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
405 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
406 typedef typename Parent::Key Key;
407 typedef typename Parent::Value Value;
410 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
411 Value operator[](Key k) const {return m1[k]*m2[k];}
414 ///Returns a \ref MulMap class
416 ///This function just returns a \ref MulMap class.
418 template<typename M1, typename M2>
419 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
420 return MulMap<M1, M2>(m1,m2);
423 ///Scales a maps with a constant.
425 ///This \ref concept::ReadMap "read only map" returns the value of the
426 ///given map multiplied from the left side with a constant value.
427 ///Its \c Key and \c Value is inherited from \c M.
431 /// ScaleMap<X> sc(x,v);
433 ///is equivalent with
435 /// ConstMap<X::Key, X::Value> c_tmp(v);
436 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
438 template<typename M, typename C = typename M::Value>
439 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
443 typedef MapBase<typename M::Key, typename M::Value> Parent;
444 typedef typename Parent::Key Key;
445 typedef typename Parent::Value Value;
450 ///\param _m is the undelying map
451 ///\param _v is the scaling value
452 ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
453 Value operator[](Key k) const {return v * m[k];}
456 ///Returns an \ref ScaleMap class
458 ///This function just returns an \ref ScaleMap class.
460 ///\todo A better name is required.
461 template<typename M, typename C>
462 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
463 return ScaleMap<M, C>(m,v);
466 ///Quotient of two maps
468 ///This \ref concept::ReadMap "read only map" returns the quotient of the
470 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
471 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
473 template<typename M1, typename M2>
474 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
478 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
479 typedef typename Parent::Key Key;
480 typedef typename Parent::Value Value;
483 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
484 Value operator[](Key k) const {return m1[k]/m2[k];}
487 ///Returns a \ref DivMap class
489 ///This function just returns a \ref DivMap class.
491 template<typename M1, typename M2>
492 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
493 return DivMap<M1, M2>(m1,m2);
496 ///Composition of two maps
498 ///This \ref concept::ReadMap "read only map" returns the composition of
500 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
504 /// ComposeMap<M1, M2> cm(m1,m2);
506 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
508 ///Its \c Key is inherited from \c M2 and its \c Value is from
510 ///The \c M2::Value must be convertible to \c M1::Key.
511 ///\todo Check the requirements.
513 template <typename M1, typename M2>
514 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
518 typedef MapBase<typename M2::Key, typename M1::Value> Parent;
519 typedef typename Parent::Key Key;
520 typedef typename Parent::Value Value;
523 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
524 Value operator[](Key k) const {return m1[m2[k]];}
526 ///Returns a \ref ComposeMap class
528 ///This function just returns a \ref ComposeMap class.
530 ///\relates ComposeMap
531 template <typename M1, typename M2>
532 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
533 return ComposeMap<M1, M2>(m1,m2);
536 ///Combines of two maps using an STL (binary) functor.
538 ///Combines of two maps using an STL (binary) functor.
541 ///This \ref concept::ReadMap "read only map" takes two maps and a
542 ///binary functor and returns the composition of
544 ///given maps unsing the functor.
545 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
546 ///and \c f is of \c F,
549 /// CombineMap<M1, M2,F,V> cm(m1,m2,f);
551 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
553 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
554 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
555 ///input parameter of \c F and the return type of \c F must be convertible
557 ///\todo Check the requirements.
559 template<typename M1, typename M2, typename F,
560 typename V = typename F::result_type,
562 class CombineMap : public MapBase<typename M1::Key, V> {
567 typedef MapBase<typename M1::Key, V> Parent;
568 typedef typename Parent::Key Key;
569 typedef typename Parent::Value Value;
572 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
573 : m1(_m1), m2(_m2), f(_f) {};
574 Value operator[](Key k) const {return f(m1[k],m2[k]);}
577 ///Returns a \ref CombineMap class
579 ///This function just returns a \ref CombineMap class.
581 ///Only the first template parameter (the value type) must be given.
583 ///For example if \c m1 and \c m2 are both \c double valued maps, then
585 ///combineMap<double>(m1,m2,std::plus<double>)
587 ///is equivalent with
592 ///\relates CombineMap
593 template<typename M1, typename M2, typename F, typename V>
594 inline CombineMap<M1, M2, F, V>
595 combineMap(const M1& m1,const M2& m2, const F& f) {
596 return CombineMap<M1, M2, F, V>(m1,m2,f);
599 template<typename M1, typename M2, typename F>
600 inline CombineMap<M1, M2, F, typename F::result_type>
601 combineMap(const M1& m1, const M2& m2, const F& f) {
602 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
605 template<typename M1, typename M2, typename K1, typename K2, typename V>
606 inline CombineMap<M1, M2, V (*)(K1, K2), V>
607 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
608 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
611 ///Negative value of a map
613 ///This \ref concept::ReadMap "read only map" returns the negative
615 ///value returned by the
616 ///given map. Its \c Key and \c Value will be inherited from \c M.
617 ///The unary \c - operator must be defined for \c Value, of course.
620 class NegMap : public MapBase<typename M::Key, typename M::Value> {
623 typedef MapBase<typename M::Key, typename M::Value> Parent;
624 typedef typename Parent::Key Key;
625 typedef typename Parent::Value Value;
628 NegMap(const M &_m) : m(_m) {};
629 Value operator[](Key k) const {return -m[k];}
632 ///Returns a \ref NegMap class
634 ///This function just returns a \ref NegMap class.
636 template <typename M>
637 inline NegMap<M> negMap(const M &m) {
642 ///Absolute value of a map
644 ///This \ref concept::ReadMap "read only map" returns the absolute value
646 ///value returned by the
647 ///given map. Its \c Key and \c Value will be inherited
648 ///from <tt>M</tt>. <tt>Value</tt>
649 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
650 ///operator must be defined for it, of course.
652 ///\bug We need a unified way to handle the situation below:
654 /// struct _UnConvertible {};
655 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
656 /// template<> inline int t_abs<>(int n) {return abs(n);}
657 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
658 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
659 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
660 /// template<> inline double t_abs<>(double n) {return fabs(n);}
661 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
666 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
669 typedef MapBase<typename M::Key, typename M::Value> Parent;
670 typedef typename Parent::Key Key;
671 typedef typename Parent::Value Value;
674 AbsMap(const M &_m) : m(_m) {};
675 Value operator[](Key k) const {
677 return tmp >= 0 ? tmp : -tmp;
682 ///Returns a \ref AbsMap class
684 ///This function just returns a \ref AbsMap class.
687 inline AbsMap<M> absMap(const M &m) {
691 ///Converts an STL style functor to a map
693 ///This \ref concept::ReadMap "read only map" returns the value
697 ///Template parameters \c K and \c V will become its
698 ///\c Key and \c Value. They must be given explicitely
699 ///because a functor does not provide such typedefs.
701 ///Parameter \c F is the type of the used functor.
705 typename K = typename F::argument_type,
706 typename V = typename F::result_type,
708 class FunctorMap : public MapBase<K, V> {
711 typedef MapBase<K, V> Parent;
712 typedef typename Parent::Key Key;
713 typedef typename Parent::Value Value;
716 FunctorMap(const F &_f) : f(_f) {}
718 Value operator[](Key k) const { return f(k);}
721 ///Returns a \ref FunctorMap class
723 ///This function just returns a \ref FunctorMap class.
725 ///The third template parameter isn't necessary to be given.
726 ///\relates FunctorMap
727 template<typename K, typename V, typename F> inline
728 FunctorMap<F, K, V> functorMap(const F &f) {
729 return FunctorMap<F, K, V>(f);
732 template <typename F> inline
733 FunctorMap<F, typename F::argument_type, typename F::result_type>
734 functorMap(const F &f) {
735 return FunctorMap<F, typename F::argument_type,
736 typename F::result_type>(f);
739 template <typename K, typename V> inline
740 FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
741 return FunctorMap<V (*)(K), K, V>(f);
745 ///Converts a map to an STL style (unary) functor
747 ///This class Converts a map to an STL style (unary) functor.
748 ///that is it provides an <tt>operator()</tt> to read its values.
750 ///For the sake of convenience it also works as
751 ///a ususal \ref concept::ReadMap "readable map",
752 ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
754 template <typename M>
755 class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
758 typedef MapBase<typename M::Key, typename M::Value> Parent;
759 typedef typename Parent::Key Key;
760 typedef typename Parent::Value Value;
763 typedef typename M::Key argument_type;
765 typedef typename M::Value result_type;
768 MapFunctor(const M &_m) : m(_m) {};
769 ///Returns a value of the map
770 Value operator()(Key k) const {return m[k];}
772 Value operator[](Key k) const {return m[k];}
775 ///Returns a \ref MapFunctor class
777 ///This function just returns a \ref MapFunctor class.
778 ///\relates MapFunctor
780 inline MapFunctor<M> mapFunctor(const M &m) {
781 return MapFunctor<M>(m);
785 ///Applies all map setting operations to two maps
787 ///This map has two \ref concept::WriteMap "writable map"
788 ///parameters and each write request will be passed to both of them.
789 ///If \c M1 is also \ref concept::ReadMap "readable",
790 ///then the read operations will return the
791 ///corresponding values of \c M1.
793 ///The \c Key and \c Value will be inherited from \c M1.
794 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
796 template<typename M1, typename M2>
797 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
801 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
802 typedef typename Parent::Key Key;
803 typedef typename Parent::Value Value;
806 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
807 Value operator[](Key k) const {return m1[k];}
808 // void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
811 ///Returns an \ref ForkMap class
813 ///This function just returns an \ref ForkMap class.
814 ///\todo How to call these type of functions?
817 ///\todo Wrong scope in Doxygen when \c \\relates is used
818 template <typename M1, typename M2>
819 inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
820 return ForkMap<M1, M2>(m1,m2);
825 /* ************* BOOL MAPS ******************* */
827 ///Logical 'not' of a map
829 ///This bool \ref concept::ReadMap "read only map" returns the
830 ///logical negation of
831 ///value returned by the
832 ///given map. Its \c Key and will be inherited from \c M,
833 ///its Value is <tt>bool</tt>.
835 template <typename M>
836 class NotMap : public MapBase<typename M::Key, bool> {
839 typedef MapBase<typename M::Key, bool> Parent;
840 typedef typename Parent::Key Key;
841 typedef typename Parent::Value Value;
844 NotMap(const M &_m) : m(_m) {};
845 Value operator[](Key k) const {return !m[k];}
848 ///Returns a \ref NotMap class
850 ///This function just returns a \ref NotMap class.
852 template <typename M>
853 inline NotMap<M> notMap(const M &m) {
860 #endif // LEMON_MAPS_H