IO with undirected edgesets and undirected graphs.
Missing features:
InfoReader,
aliased edges in undir edgesets
2 * src/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/graph_utils.h>
21 #include <lemon/utility.h>
26 ///\brief Miscellaneous property maps
28 ///\todo This file has the same name as the concept file in concept/,
29 /// and this is not easily detectable in docs...
38 /// Base class of maps.
40 /// Base class of maps.
41 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
42 template<typename K, typename T>
52 /// Null map. (a.k.a. DoNothingMap)
54 /// If you have to provide a map only for its type definitions,
55 /// or if you have to provide a writable map, but
56 /// data written to it will sent to <tt>/dev/null</tt>...
57 template<typename K, typename T>
58 class NullMap : public MapBase<K,T>
62 typedef True NeedCopy;
64 /// Gives back a default constructed element.
65 T operator[](const K&) const { return T(); }
66 /// Absorbs the value.
67 void set(const K&, const T&) {}
70 template <typename K, typename V>
71 NullMap<K, V> nullMap() {
72 return NullMap<K, V>();
78 /// This is a readable map which assigns a specified value to each key.
79 /// In other aspects it is equivalent to the \ref NullMap.
80 /// \todo set could be used to set the value.
81 template<typename K, typename T>
82 class ConstMap : public MapBase<K,T>
87 typedef True NeedCopy;
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<class V,class K>
117 inline ConstMap<V,K> constMap(const K &k)
119 return ConstMap<V,K>(k);
124 template<typename T, T v>
127 template<typename K, typename V, V v>
128 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
132 V operator[](const K&) const { return v; }
133 void set(const K&, const V&) { }
136 /// \c std::map wrapper
138 /// This is essentially a wrapper for \c std::map. With addition that
139 /// you can specify a default value different from \c Value() .
141 /// \todo Provide allocator parameter...
142 template <typename K, typename T, typename Compare = std::less<K> >
143 class StdMap : public std::map<K,T,Compare> {
144 typedef std::map<K,T,Compare> parent;
146 typedef typename parent::value_type PairType;
151 typedef T& Reference;
152 typedef const T& ConstReference;
156 /// Constructor with specified default value
157 StdMap(const T& _v) : v(_v) {}
159 /// \brief Constructs the map from an appropriate std::map.
161 /// \warning Inefficient: copies the content of \c m !
162 StdMap(const parent &m) : parent(m) {}
163 /// \brief Constructs the map from an appropriate std::map, and explicitly
164 /// specifies a default value.
166 /// \warning Inefficient: copies the content of \c m !
167 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
169 template<typename T1, typename Comp1>
170 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
174 Reference operator[](const Key &k) {
175 return insert(PairType(k,v)).first -> second;
177 ConstReference operator[](const Key &k) const {
178 typename parent::iterator i = lower_bound(k);
179 if (i == parent::end() || parent::key_comp()(k, (*i).first))
183 void set(const Key &k, const T &t) {
184 parent::operator[](k) = t;
187 /// Changes the default value of the map.
188 /// \return Returns the previous default value.
190 /// \warning The value of some keys (which has already been queried, but
191 /// the value has been unchanged from the default) may change!
192 T setDefault(const T &_v) { T old=v; v=_v; return old; }
194 template<typename T1>
196 typedef StdMap<Key,T1,Compare> other;
202 /// \addtogroup map_adaptors
206 ///Convert the \c Value of a maps to another type.
208 ///This \ref concept::ReadMap "read only map"
209 ///converts the \c Value of a maps to type \c T.
210 ///Its \c Value is inherited from \c M.
214 /// ConvertMap<X> sh(x,v);
216 ///it is equivalent with
218 /// ConstMap<X::Key, X::Value> c_tmp(v);
219 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
221 ///\bug wrong documentation
222 template<class M, class T>
224 typename SmartConstReference<M>::Type m;
227 typedef True NeedCopy;
229 typedef typename M::Key Key;
235 ///\param _m is the undelying map
236 ///\param _v is the convert value
237 ConvertMap(const M &_m) : m(_m) {};
239 /// \brief The subscript operator.
241 /// The subscript operator.
242 /// \param edge The edge
243 /// \return The target of the edge
244 Value operator[](Key k) const {return m[k];}
247 ///Returns an \ref ConvertMap class
249 ///This function just returns an \ref ConvertMap class.
250 ///\relates ConvertMap
251 ///\todo The order of the template parameters are changed.
252 template<class T, class M>
253 inline ConvertMap<M,T> convertMap(const M &m)
255 return ConvertMap<M,T>(m);
260 ///This \ref concept::ReadMap "read only map" returns the sum of the two
261 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
262 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
264 template<class M1,class M2>
267 typename SmartConstReference<M1>::Type m1;
268 typename SmartConstReference<M2>::Type m2;
272 typedef True NeedCopy;
274 typedef typename M1::Key Key;
275 typedef typename M1::Value Value;
281 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
282 Value operator[](Key k) const {return m1[k]+m2[k];}
285 ///Returns an \ref AddMap class
287 ///This function just returns an \ref AddMap class.
288 ///\todo How to call these type of functions?
291 ///\todo Wrong scope in Doxygen when \c \\relates is used
292 template<class M1,class M2>
293 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
295 return AddMap<M1,M2>(m1,m2);
298 ///Shift a maps with a constant.
300 ///This \ref concept::ReadMap "read only map" returns the sum of the
301 ///given map and a constant value.
302 ///Its \c Key and \c Value is inherited from \c M.
306 /// ShiftMap<X> sh(x,v);
308 ///it is equivalent with
310 /// ConstMap<X::Key, X::Value> c_tmp(v);
311 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
316 typename SmartConstReference<M>::Type m;
320 typedef True NeedCopy;
321 typedef typename M::Key Key;
322 typedef typename M::Value Value;
327 ///\param _m is the undelying map
328 ///\param _v is the shift value
329 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
330 Value operator[](Key k) const {return m[k]+v;}
333 ///Returns an \ref ShiftMap class
335 ///This function just returns an \ref ShiftMap class.
337 ///\todo A better name is required.
339 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
341 return ShiftMap<M>(m,v);
344 ///Difference of two maps
346 ///This \ref concept::ReadMap "read only map" returns the difference
347 ///of the values returned by the two
348 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
349 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
351 template<class M1,class M2>
354 typename SmartConstReference<M1>::Type m1;
355 typename SmartConstReference<M2>::Type m2;
358 typedef True NeedCopy;
359 typedef typename M1::Key Key;
360 typedef typename M1::Value Value;
366 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
367 Value operator[](Key k) const {return m1[k]-m2[k];}
370 ///Returns a \ref SubMap class
372 ///This function just returns a \ref SubMap class.
375 template<class M1,class M2>
376 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
378 return SubMap<M1,M2>(m1,m2);
381 ///Product of two maps
383 ///This \ref concept::ReadMap "read only map" returns the product of the
384 ///values returned by the two
386 ///maps. Its \c Key and \c Value will be inherited from \c M1.
387 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
389 template<class M1,class M2>
392 typename SmartConstReference<M1>::Type m1;
393 typename SmartConstReference<M2>::Type m2;
396 typedef True NeedCopy;
397 typedef typename M1::Key Key;
398 typedef typename M1::Value Value;
404 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
405 Value operator[](Key k) const {return m1[k]*m2[k];}
408 ///Returns a \ref MulMap class
410 ///This function just returns a \ref MulMap class.
412 template<class M1,class M2>
413 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
415 return MulMap<M1,M2>(m1,m2);
418 ///Scale a maps with a constant.
420 ///This \ref concept::ReadMap "read only map" returns the value of the
421 ///given map multipied with a constant value.
422 ///Its \c Key and \c Value is inherited from \c M.
426 /// ScaleMap<X> sc(x,v);
428 ///it is equivalent with
430 /// ConstMap<X::Key, X::Value> c_tmp(v);
431 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
436 typename SmartConstReference<M>::Type m;
440 typedef True NeedCopy;
441 typedef typename M::Key Key;
442 typedef typename M::Value Value;
447 ///\param _m is the undelying map
448 ///\param _v is the scaling value
449 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
450 Value operator[](Key k) const {return m[k]*v;}
453 ///Returns an \ref ScaleMap class
455 ///This function just returns an \ref ScaleMap class.
457 ///\todo A better name is required.
459 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
461 return ScaleMap<M>(m,v);
464 ///Quotient of two maps
466 ///This \ref concept::ReadMap "read only map" returns the quotient of the
467 ///values returned by the two
468 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
469 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
471 template<class M1,class M2>
474 typename SmartConstReference<M1>::Type m1;
475 typename SmartConstReference<M2>::Type m2;
478 typedef True NeedCopy;
479 typedef typename M1::Key Key;
480 typedef typename M1::Value Value;
486 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
487 Value operator[](Key k) const {return m1[k]/m2[k];}
490 ///Returns a \ref DivMap class
492 ///This function just returns a \ref DivMap class.
494 template<class M1,class M2>
495 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
497 return DivMap<M1,M2>(m1,m2);
500 ///Composition of two maps
502 ///This \ref concept::ReadMap "read only map" returns the composition of
504 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
508 /// ComposeMap<M1,M2> cm(m1,m2);
510 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
512 ///Its \c Key is inherited from \c M2 and its \c Value is from
514 ///The \c M2::Value must be convertible to \c M1::Key.
515 ///\todo Check the requirements.
517 template<class M1,class M2>
520 typename SmartConstReference<M1>::Type m1;
521 typename SmartConstReference<M2>::Type m2;
524 typedef True NeedCopy;
525 typedef typename M2::Key Key;
526 typedef typename M1::Value Value;
528 typedef True NeedCopy;
534 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
535 Value operator[](Key k) const {return m1[m2[k]];}
537 ///Returns a \ref ComposeMap class
539 ///This function just returns a \ref ComposeMap class.
541 ///\relates ComposeMap
542 template<class M1,class M2>
543 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
545 return ComposeMap<M1,M2>(m1,m2);
548 ///Combine of two maps using an STL (binary) functor.
550 ///Combine of two maps using an STL (binary) functor.
553 ///This \ref concept::ReadMap "read only map" takes to maps and a
554 ///binary functor and returns the composition of
556 ///given maps unsing the functor.
557 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
558 ///and \c f is of \c F,
561 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
563 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
565 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
566 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
567 ///input parameter of \c F and the return type of \c F must be convertible
569 ///\todo Check the requirements.
571 template<class M1,class M2,class F,class V = typename F::result_type>
574 typename SmartConstReference<M1>::Type m1;
575 typename SmartConstReference<M2>::Type m2;
579 typedef True NeedCopy;
580 typedef typename M1::Key Key;
587 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
588 : m1(_m1), m2(_m2), f(_f) {};
589 Value operator[](Key k) const {return f(m1[k],m2[k]);}
592 ///Returns a \ref CombineMap class
594 ///This function just returns a \ref CombineMap class.
596 ///Only the first template parameter (the value type) must be given.
598 ///For example if \c m1 and \c m2 are both \c double valued maps, then
600 ///combineMap<double>(m1,m2,std::plus<double>)
602 ///is equivalent with
607 ///\relates CombineMap
608 template<class M1,class M2,class F>
609 inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
611 return CombineMap<M1,M2,F>(m1,m2,f);
614 ///Negative value of a map
616 ///This \ref concept::ReadMap "read only map" returns the negative
618 ///value returned by the
619 ///given map. Its \c Key and \c Value will be inherited from \c M.
620 ///The unary \c - operator must be defined for \c Value, of course.
625 typename SmartConstReference<M>::Type m;
628 typedef True NeedCopy;
629 typedef typename M::Key Key;
630 typedef typename M::Value Value;
636 NegMap(const M &_m) : m(_m) {};
637 Value operator[](Key k) const {return -m[k];}
640 ///Returns a \ref NegMap class
642 ///This function just returns a \ref NegMap class.
645 inline NegMap<M> negMap(const M &m)
651 ///Absolute value of a map
653 ///This \ref concept::ReadMap "read only map" returns the absolute value
655 ///value returned by the
656 ///given map. Its \c Key and \c Value will be inherited
657 ///from <tt>M</tt>. <tt>Value</tt>
658 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
659 ///operator must be defined for it, of course.
661 ///\bug We need a unified way to handle the situation below:
663 /// struct _UnConvertible {};
664 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
665 /// template<> inline int t_abs<>(int n) {return abs(n);}
666 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
667 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
668 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
669 /// template<> inline double t_abs<>(double n) {return fabs(n);}
670 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
677 typename SmartConstReference<M>::Type m;
680 typedef True NeedCopy;
681 typedef typename M::Key Key;
682 typedef typename M::Value Value;
688 AbsMap(const M &_m) : m(_m) {};
689 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
692 ///Returns a \ref AbsMap class
694 ///This function just returns a \ref AbsMap class.
697 inline AbsMap<M> absMap(const M &m)
702 ///Converts an STL style functor to a map
704 ///This \ref concept::ReadMap "read only map" returns the value
708 ///Template parameters \c K and \c V will become its
709 ///\c Key and \c Value. They must be given explicitely
710 ///because a functor does not provide such typedefs.
712 ///Parameter \c F is the type of the used functor.
715 template<class K,class V,class F>
721 typedef True NeedCopy;
729 FunctorMap(const F &_f) : f(_f) {};
730 Value operator[](Key k) const {return f(k);}
733 ///Returns a \ref FunctorMap class
735 ///This function just returns a \ref FunctorMap class.
737 ///The third template parameter isn't necessary to be given.
738 ///\relates FunctorMap
739 template<class K,class V, class F>
740 inline FunctorMap<K,V,F> functorMap(const F &f)
742 return FunctorMap<K,V,F>(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", i.e
752 ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
757 typename SmartConstReference<M>::Type m;
760 typedef True NeedCopy;
761 typedef typename M::Key argument_type;
762 typedef typename M::Value result_type;
763 typedef typename M::Key Key;
764 typedef typename M::Value Value;
770 MapFunctor(const M &_m) : m(_m) {};
771 ///Returns a value of the map
775 Value operator()(Key k) const {return m[k];}
778 Value operator[](Key k) const {return m[k];}
781 ///Returns a \ref MapFunctor class
783 ///This function just returns a \ref MapFunctor class.
784 ///\relates MapFunctor
786 inline MapFunctor<M> mapFunctor(const M &m)
788 return MapFunctor<M>(m);
792 ///Apply all map setting operations to two maps
794 ///This map has two \ref concept::WriteMap "writable map"
795 ///parameters and each write request will be passed to both of them.
796 ///If \c M1 is also \ref concept::ReadMap "readable",
797 ///then the read operations will return the
798 ///corresponding values of \c M1.
800 ///The \c Key and \c Value will be inherited from \c M1.
801 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
803 template<class M1,class M2>
806 typename SmartConstReference<M1>::Type m1;
807 typename SmartConstReference<M2>::Type m2;
810 typedef True NeedCopy;
811 typedef typename M1::Key Key;
812 typedef typename M1::Value Value;
818 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
819 Value operator[](Key k) const {return m1[k];}
820 void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
823 ///Returns an \ref ForkMap class
825 ///This function just returns an \ref ForkMap class.
826 ///\todo How to call these type of functions?
829 ///\todo Wrong scope in Doxygen when \c \\relates is used
830 template<class M1,class M2>
831 inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
833 return ForkMap<M1,M2>(m1,m2);
841 #endif // LEMON_MAPS_H