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
26 #include <lemon/bits/utility.h>
27 // #include <lemon/bits/traits.h>
31 ///\brief Miscellaneous property maps
40 /// Base class of maps.
42 /// Base class of maps.
43 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
44 template<typename K, typename T>
53 /// Null map. (a.k.a. DoNothingMap)
55 /// This map can be used if you have to provide a map only for
56 /// its type definitions, or if you have to provide a writable map,
57 /// but data written to it is not required (i.e. it will be sent to
58 /// <tt>/dev/null</tt>).
59 template<typename K, typename T>
60 class NullMap : public MapBase<K, T> {
62 typedef MapBase<K, T> Parent;
63 typedef typename Parent::Key Key;
64 typedef typename Parent::Value Value;
66 /// Gives back a default constructed element.
67 T operator[](const K&) const { return T(); }
68 /// Absorbs the value.
69 void set(const K&, const T&) {}
72 ///Returns a \c NullMap class
74 ///This function just returns a \c NullMap class.
76 template <typename K, typename V>
77 NullMap<K, V> nullMap() {
78 return NullMap<K, V>();
84 /// This is a readable map which assigns a specified value to each key.
85 /// In other aspects it is equivalent to the \c NullMap.
86 template<typename K, typename T>
87 class ConstMap : public MapBase<K, T> {
92 typedef MapBase<K, T> Parent;
93 typedef typename Parent::Key Key;
94 typedef typename Parent::Value Value;
96 /// Default constructor
98 /// Default constructor.
99 /// The value of the map will be uninitialized.
100 /// (More exactly it will be default constructed.)
103 /// Constructor with specified initial value
105 /// Constructor with specified initial value.
106 /// \param _v is the initial value of the map.
107 ConstMap(const T &_v) : v(_v) {}
110 T operator[](const K&) const { return v; }
113 void setAll(const T &t) {
117 template<typename T1>
119 typedef ConstMap<K, T1> other;
122 template<typename T1>
123 ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
126 ///Returns a \c ConstMap class
128 ///This function just returns a \c ConstMap class.
130 template<typename K, typename V>
131 inline ConstMap<K, V> constMap(const V &v) {
132 return ConstMap<K, V>(v);
136 template<typename T, T v>
139 /// Constant map with inlined constant value.
141 /// This is a readable map which assigns a specified value to each key.
142 /// In other aspects it is equivalent to the \c NullMap.
143 template<typename K, typename V, V v>
144 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
146 typedef MapBase<K, V> Parent;
147 typedef typename Parent::Key Key;
148 typedef typename Parent::Value Value;
152 V operator[](const K&) const { return v; }
154 void set(const K&, const V&) { }
157 ///Returns a \c ConstMap class
159 ///This function just returns a \c ConstMap class with inlined value.
161 template<typename K, typename V, V v>
162 inline ConstMap<K, Const<V, v> > constMap() {
163 return ConstMap<K, Const<V, v> >();
166 ///Map based on std::map
168 ///This is essentially a wrapper for \c std::map with addition that
169 ///you can specify a default value different from \c Value().
170 template <typename K, typename T, typename Compare = std::less<K> >
172 template <typename K1, typename T1, typename C1>
176 typedef True ReferenceMapTag;
182 typedef T& Reference;
184 typedef const T& ConstReference;
188 typedef std::map<K, T, Compare> Map;
194 /// Constructor with specified default value
195 StdMap(const T& value = T()) : _value(value) {}
196 /// \brief Constructs the map from an appropriate std::map, and explicitly
197 /// specifies a default value.
198 template <typename T1, typename Comp1>
199 StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T())
200 : _map(map.begin(), map.end()), _value(value) {}
202 /// \brief Constructs a map from an other StdMap.
203 template<typename T1, typename Comp1>
204 StdMap(const StdMap<Key, T1, Comp1> &c)
205 : _map(c._map.begin(), c._map.end()), _value(c._value) {}
209 StdMap& operator=(const StdMap&);
214 Reference operator[](const Key &k) {
215 typename Map::iterator it = _map.lower_bound(k);
216 if (it != _map.end() && !_map.key_comp()(k, it->first))
219 return _map.insert(it, std::make_pair(k, _value))->second;
223 ConstReference operator[](const Key &k) const {
224 typename Map::const_iterator it = _map.find(k);
225 if (it != _map.end())
232 void set(const Key &k, const T &t) {
233 typename Map::iterator it = _map.lower_bound(k);
234 if (it != _map.end() && !_map.key_comp()(k, it->first))
237 _map.insert(it, std::make_pair(k, t));
241 void setAll(const T &t) {
246 template <typename T1, typename C1 = std::less<T1> >
248 typedef StdMap<Key, T1, C1> other;
252 /// \brief Map for storing values for the range \c [0..size-1] range keys
254 /// The current map has the \c [0..size-1] keyset and the values
255 /// are stored in a \c std::vector<T> container. It can be used with
256 /// some data structures, for example \c UnionFind, \c BinHeap, when
257 /// the used items are small integer numbers.
259 /// \todo Revise its name
260 template <typename T>
263 template <typename T1>
264 friend class IntegerMap;
268 typedef True ReferenceMapTag;
274 typedef T& Reference;
276 typedef const T& ConstReference;
280 typedef std::vector<T> Vector;
285 /// Constructor with specified default value
286 IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
288 /// \brief Constructs the map from an appropriate std::vector.
289 template <typename T1>
290 IntegerMap(const std::vector<T1>& vector)
291 : _vector(vector.begin(), vector.end()) {}
293 /// \brief Constructs a map from an other IntegerMap.
294 template <typename T1>
295 IntegerMap(const IntegerMap<T1> &c)
296 : _vector(c._vector.begin(), c._vector.end()) {}
298 /// \brief Resize the container
299 void resize(int size, const T& value = T()) {
300 _vector.resize(size, value);
305 IntegerMap& operator=(const IntegerMap&);
310 Reference operator[](Key k) {
315 ConstReference operator[](Key k) const {
320 void set(const Key &k, const T& t) {
328 /// \addtogroup map_adaptors
331 /// \brief Identity map.
333 /// This map gives back the given key as value without any
335 template <typename T>
336 class IdentityMap : public MapBase<T, T> {
338 typedef MapBase<T, T> Parent;
339 typedef typename Parent::Key Key;
340 typedef typename Parent::Value Value;
343 const T& operator[](const T& t) const {
348 ///Returns an \c IdentityMap class
350 ///This function just returns an \c IdentityMap class.
351 ///\relates IdentityMap
353 inline IdentityMap<T> identityMap() {
354 return IdentityMap<T>();
358 ///\brief Convert the \c Value of a map to another type using
359 ///the default conversion.
361 ///This \c concepts::ReadMap "read only map"
362 ///converts the \c Value of a map to type \c T.
363 ///Its \c Key is inherited from \c M.
364 template <typename M, typename T>
365 class ConvertMap : public MapBase<typename M::Key, T> {
368 typedef MapBase<typename M::Key, T> Parent;
369 typedef typename Parent::Key Key;
370 typedef typename Parent::Value Value;
375 ///\param _m is the underlying map.
376 ConvertMap(const M &_m) : m(_m) {};
378 /// \brief The subscript operator.
380 /// The subscript operator.
381 Value operator[](const Key& k) const {return m[k];}
384 ///Returns a \c ConvertMap class
386 ///This function just returns a \c ConvertMap class.
387 ///\relates ConvertMap
388 template<typename T, typename M>
389 inline ConvertMap<M, T> convertMap(const M &m) {
390 return ConvertMap<M, T>(m);
393 ///Simple wrapping of a map
395 ///This \c concepts::ReadMap "read only map" returns the simple
396 ///wrapping of the given map. Sometimes the reference maps cannot be
397 ///combined with simple read maps. This map adaptor wraps the given
398 ///map to simple read map.
400 ///\sa SimpleWriteMap
402 /// \todo Revise the misleading name
404 class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
408 typedef MapBase<typename M::Key, typename M::Value> Parent;
409 typedef typename Parent::Key Key;
410 typedef typename Parent::Value Value;
413 SimpleMap(const M &_m) : m(_m) {};
415 Value operator[](Key k) const {return m[k];}
418 ///Simple writable wrapping of the map
420 ///This \c concepts::WriteMap "write map" returns the simple
421 ///wrapping of the given map. Sometimes the reference maps cannot be
422 ///combined with simple read-write maps. This map adaptor wraps the
423 ///given map to simple read-write map.
427 /// \todo Revise the misleading name
429 class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
433 typedef MapBase<typename M::Key, typename M::Value> Parent;
434 typedef typename Parent::Key Key;
435 typedef typename Parent::Value Value;
438 SimpleWriteMap(M &_m) : m(_m) {};
440 Value operator[](Key k) const {return m[k];}
442 void set(Key k, const Value& c) { m.set(k, c); }
447 ///This \c concepts::ReadMap "read only map" returns the sum of the two
449 ///Its \c Key and \c Value are inherited from \c M1.
450 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
451 template<typename M1, typename M2>
452 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
457 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
458 typedef typename Parent::Key Key;
459 typedef typename Parent::Value Value;
462 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
464 Value operator[](Key k) const {return m1[k]+m2[k];}
467 ///Returns an \c AddMap class
469 ///This function just returns an \c AddMap class.
470 ///\todo How to call these type of functions?
473 template<typename M1, typename M2>
474 inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
475 return AddMap<M1, M2>(m1,m2);
478 ///Shift a map with a constant.
480 ///This \c concepts::ReadMap "read only map" returns the sum of the
481 ///given map and a constant value.
482 ///Its \c Key and \c Value are inherited from \c M.
486 /// ShiftMap<X> sh(x,v);
490 /// ConstMap<X::Key, X::Value> c_tmp(v);
491 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
495 template<typename M, typename C = typename M::Value>
496 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
500 typedef MapBase<typename M::Key, typename M::Value> Parent;
501 typedef typename Parent::Key Key;
502 typedef typename Parent::Value Value;
507 ///\param _m is the undelying map.
508 ///\param _v is the shift value.
509 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
511 Value operator[](Key k) const {return m[k] + v;}
514 ///Shift a map with a constant (ReadWrite version).
516 ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
517 ///given map and a constant value. It makes also possible to write the map.
518 ///Its \c Key and \c Value are inherited from \c M.
521 template<typename M, typename C = typename M::Value>
522 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
526 typedef MapBase<typename M::Key, typename M::Value> Parent;
527 typedef typename Parent::Key Key;
528 typedef typename Parent::Value Value;
533 ///\param _m is the undelying map.
534 ///\param _v is the shift value.
535 ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
537 Value operator[](Key k) const {return m[k] + v;}
539 void set(Key k, const Value& c) { m.set(k, c - v); }
542 ///Returns a \c ShiftMap class
544 ///This function just returns a \c ShiftMap class.
546 template<typename M, typename C>
547 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
548 return ShiftMap<M, C>(m,v);
551 ///Returns a \c ShiftWriteMap class
553 ///This function just returns a \c ShiftWriteMap class.
554 ///\relates ShiftWriteMap
555 template<typename M, typename C>
556 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
557 return ShiftWriteMap<M, C>(m,v);
560 ///Difference of two maps
562 ///This \c concepts::ReadMap "read only map" returns the difference
563 ///of the values of the two given maps.
564 ///Its \c Key and \c Value are inherited from \c M1.
565 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
567 /// \todo Revise the misleading name
568 template<typename M1, typename M2>
569 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
573 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
574 typedef typename Parent::Key Key;
575 typedef typename Parent::Value Value;
578 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
580 Value operator[](Key k) const {return m1[k]-m2[k];}
583 ///Returns a \c SubMap class
585 ///This function just returns a \c SubMap class.
588 template<typename M1, typename M2>
589 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
590 return SubMap<M1, M2>(m1, m2);
593 ///Product of two maps
595 ///This \c concepts::ReadMap "read only map" returns the product of the
596 ///values of the two given maps.
597 ///Its \c Key and \c Value are inherited from \c M1.
598 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
599 template<typename M1, typename M2>
600 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
604 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
605 typedef typename Parent::Key Key;
606 typedef typename Parent::Value Value;
609 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
611 Value operator[](Key k) const {return m1[k]*m2[k];}
614 ///Returns a \c MulMap class
616 ///This function just returns a \c MulMap class.
618 template<typename M1, typename M2>
619 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
620 return MulMap<M1, M2>(m1,m2);
623 ///Scales a map with a constant.
625 ///This \c concepts::ReadMap "read only map" returns the value of the
626 ///given map multiplied from the left side with a constant value.
627 ///Its \c Key and \c Value are inherited from \c M.
631 /// ScaleMap<X> sc(x,v);
635 /// ConstMap<X::Key, X::Value> c_tmp(v);
636 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
640 template<typename M, typename C = typename M::Value>
641 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
645 typedef MapBase<typename M::Key, typename M::Value> Parent;
646 typedef typename Parent::Key Key;
647 typedef typename Parent::Value Value;
652 ///\param _m is the undelying map.
653 ///\param _v is the scaling value.
654 ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
656 Value operator[](Key k) const {return v * m[k];}
659 ///Scales a map with a constant (ReadWrite version).
661 ///This \c concepts::ReadWriteMap "read-write map" returns the value of the
662 ///given map multiplied from the left side with a constant value. It can
663 ///also be used as write map if the \c / operator is defined between
664 ///\c Value and \c C and the given multiplier is not zero.
665 ///Its \c Key and \c Value are inherited from \c M.
668 template<typename M, typename C = typename M::Value>
669 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
673 typedef MapBase<typename M::Key, typename M::Value> Parent;
674 typedef typename Parent::Key Key;
675 typedef typename Parent::Value Value;
680 ///\param _m is the undelying map.
681 ///\param _v is the scaling value.
682 ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
684 Value operator[](Key k) const {return v * m[k];}
686 void set(Key k, const Value& c) { m.set(k, c / v);}
689 ///Returns a \c ScaleMap class
691 ///This function just returns a \c ScaleMap class.
693 template<typename M, typename C>
694 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
695 return ScaleMap<M, C>(m,v);
698 ///Returns a \c ScaleWriteMap class
700 ///This function just returns a \c ScaleWriteMap class.
701 ///\relates ScaleWriteMap
702 template<typename M, typename C>
703 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
704 return ScaleWriteMap<M, C>(m,v);
707 ///Quotient of two maps
709 ///This \c concepts::ReadMap "read only map" returns the quotient of the
710 ///values of the two given maps.
711 ///Its \c Key and \c Value are inherited from \c M1.
712 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
713 template<typename M1, typename M2>
714 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
718 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
719 typedef typename Parent::Key Key;
720 typedef typename Parent::Value Value;
723 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
725 Value operator[](Key k) const {return m1[k]/m2[k];}
728 ///Returns a \c DivMap class
730 ///This function just returns a \c DivMap class.
732 template<typename M1, typename M2>
733 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
734 return DivMap<M1, M2>(m1,m2);
737 ///Composition of two maps
739 ///This \c concepts::ReadMap "read only map" returns the composition of
741 ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2,
744 /// ComposeMap<M1, M2> cm(m1,m2);
746 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
748 ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1.
749 ///\c M2::Value must be convertible to \c M1::Key.
753 ///\todo Check the requirements.
754 template <typename M1, typename M2>
755 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
759 typedef MapBase<typename M2::Key, typename M1::Value> Parent;
760 typedef typename Parent::Key Key;
761 typedef typename Parent::Value Value;
764 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
769 /// \todo Use the MapTraits once it is ported.
772 //typename MapTraits<M1>::ConstReturnValue
774 operator[](Key k) const {return m1[m2[k]];}
777 ///Returns a \c ComposeMap class
779 ///This function just returns a \c ComposeMap class.
780 ///\relates ComposeMap
781 template <typename M1, typename M2>
782 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
783 return ComposeMap<M1, M2>(m1,m2);
786 ///Combine of two maps using an STL (binary) functor.
788 ///Combine of two maps using an STL (binary) functor.
790 ///This \c concepts::ReadMap "read only map" takes two maps and a
791 ///binary functor and returns the composition of the two
792 ///given maps unsing the functor.
793 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
794 ///and \c f is of \c F, then for
796 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
798 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
800 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
801 ///\c M2::Value and \c M1::Value must be convertible to the corresponding
802 ///input parameter of \c F and the return type of \c F must be convertible
807 ///\todo Check the requirements.
808 template<typename M1, typename M2, typename F,
809 typename V = typename F::result_type>
810 class CombineMap : public MapBase<typename M1::Key, V> {
815 typedef MapBase<typename M1::Key, V> Parent;
816 typedef typename Parent::Key Key;
817 typedef typename Parent::Value Value;
820 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
821 : m1(_m1), m2(_m2), f(_f) {};
823 Value operator[](Key k) const {return f(m1[k],m2[k]);}
826 ///Returns a \c CombineMap class
828 ///This function just returns a \c CombineMap class.
830 ///For example if \c m1 and \c m2 are both \c double valued maps, then
832 ///combineMap<double>(m1,m2,std::plus<double>())
839 ///This function is specialized for adaptable binary function
840 ///classes and C++ functions.
842 ///\relates CombineMap
843 template<typename M1, typename M2, typename F, typename V>
844 inline CombineMap<M1, M2, F, V>
845 combineMap(const M1& m1,const M2& m2, const F& f) {
846 return CombineMap<M1, M2, F, V>(m1,m2,f);
849 template<typename M1, typename M2, typename F>
850 inline CombineMap<M1, M2, F, typename F::result_type>
851 combineMap(const M1& m1, const M2& m2, const F& f) {
852 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
855 template<typename M1, typename M2, typename K1, typename K2, typename V>
856 inline CombineMap<M1, M2, V (*)(K1, K2), V>
857 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
858 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
861 ///Negative value of a map
863 ///This \c concepts::ReadMap "read only map" returns the negative
864 ///value of the value returned by the given map.
865 ///Its \c Key and \c Value are inherited from \c M.
866 ///The unary \c - operator must be defined for \c Value, of course.
870 class NegMap : public MapBase<typename M::Key, typename M::Value> {
873 typedef MapBase<typename M::Key, typename M::Value> Parent;
874 typedef typename Parent::Key Key;
875 typedef typename Parent::Value Value;
878 NegMap(const M &_m) : m(_m) {};
880 Value operator[](Key k) const {return -m[k];}
883 ///Negative value of a map (ReadWrite version)
885 ///This \c concepts::ReadWriteMap "read-write map" returns the negative
886 ///value of the value returned by the given map.
887 ///Its \c Key and \c Value are inherited from \c M.
888 ///The unary \c - operator must be defined for \c Value, of course.
892 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
895 typedef MapBase<typename M::Key, typename M::Value> Parent;
896 typedef typename Parent::Key Key;
897 typedef typename Parent::Value Value;
900 NegWriteMap(M &_m) : m(_m) {};
902 Value operator[](Key k) const {return -m[k];}
904 void set(Key k, const Value& v) { m.set(k, -v); }
907 ///Returns a \c NegMap class
909 ///This function just returns a \c NegMap class.
911 template <typename M>
912 inline NegMap<M> negMap(const M &m) {
916 ///Returns a \c NegWriteMap class
918 ///This function just returns a \c NegWriteMap class.
919 ///\relates NegWriteMap
920 template <typename M>
921 inline NegWriteMap<M> negMap(M &m) {
922 return NegWriteMap<M>(m);
925 ///Absolute value of a map
927 ///This \c concepts::ReadMap "read only map" returns the absolute value
928 ///of the value returned by the given map.
929 ///Its \c Key and \c Value are inherited from \c M.
930 ///\c Value must be comparable to \c 0 and the unary \c -
931 ///operator must be defined for it, of course.
933 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
936 typedef MapBase<typename M::Key, typename M::Value> Parent;
937 typedef typename Parent::Key Key;
938 typedef typename Parent::Value Value;
941 AbsMap(const M &_m) : m(_m) {};
943 Value operator[](Key k) const {
945 return tmp >= 0 ? tmp : -tmp;
950 ///Returns an \c AbsMap class
952 ///This function just returns an \c AbsMap class.
955 inline AbsMap<M> absMap(const M &m) {
959 ///Converts an STL style functor to a map
961 ///This \c concepts::ReadMap "read only map" returns the value
962 ///of a given functor.
964 ///Template parameters \c K and \c V will become its
965 ///\c Key and \c Value. They must be given explicitly
966 ///because a functor does not provide such typedefs.
968 ///Parameter \c F is the type of the used functor.
972 typename K = typename F::argument_type,
973 typename V = typename F::result_type>
974 class FunctorMap : public MapBase<K, V> {
977 typedef MapBase<K, V> Parent;
978 typedef typename Parent::Key Key;
979 typedef typename Parent::Value Value;
982 FunctorMap(const F &_f = F()) : f(_f) {}
984 Value operator[](Key k) const { return f(k);}
987 ///Returns a \c FunctorMap class
989 ///This function just returns a \c FunctorMap class.
991 ///It is specialized for adaptable function classes and
993 ///\relates FunctorMap
994 template<typename K, typename V, typename F> inline
995 FunctorMap<F, K, V> functorMap(const F &f) {
996 return FunctorMap<F, K, V>(f);
999 template <typename F> inline
1000 FunctorMap<F, typename F::argument_type, typename F::result_type>
1001 functorMap(const F &f) {
1002 return FunctorMap<F, typename F::argument_type,
1003 typename F::result_type>(f);
1006 template <typename K, typename V> inline
1007 FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
1008 return FunctorMap<V (*)(K), K, V>(f);
1012 ///Converts a map to an STL style (unary) functor
1014 ///This class Converts a map to an STL style (unary) functor.
1015 ///that is it provides an <tt>operator()</tt> to read its values.
1017 ///For the sake of convenience it also works as
1018 ///a ususal \c concepts::ReadMap "readable map",
1019 ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
1022 template <typename M>
1023 class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
1026 typedef MapBase<typename M::Key, typename M::Value> Parent;
1027 typedef typename Parent::Key Key;
1028 typedef typename Parent::Value Value;
1030 typedef typename M::Key argument_type;
1031 typedef typename M::Value result_type;
1034 MapFunctor(const M &_m) : m(_m) {};
1036 Value operator()(Key k) const {return m[k];}
1038 Value operator[](Key k) const {return m[k];}
1041 ///Returns a \c MapFunctor class
1043 ///This function just returns a \c MapFunctor class.
1044 ///\relates MapFunctor
1045 template<typename M>
1046 inline MapFunctor<M> mapFunctor(const M &m) {
1047 return MapFunctor<M>(m);
1050 ///Applies all map setting operations to two maps
1052 ///This map has two \c concepts::ReadMap "readable map"
1053 ///parameters and each read request will be passed just to the
1054 ///first map. This class is the just readable map type of the ForkWriteMap.
1056 ///The \c Key and \c Value are inherited from \c M1.
1057 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1061 /// \todo Why is it needed?
1062 template<typename M1, typename M2>
1063 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
1067 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1068 typedef typename Parent::Key Key;
1069 typedef typename Parent::Value Value;
1072 ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
1074 Value operator[](Key k) const {return m1[k];}
1078 ///Applies all map setting operations to two maps
1080 ///This map has two \c concepts::WriteMap "writable map"
1081 ///parameters and each write request will be passed to both of them.
1082 ///If \c M1 is also \c concepts::ReadMap "readable",
1083 ///then the read operations will return the
1084 ///corresponding values of \c M1.
1086 ///The \c Key and \c Value are inherited from \c M1.
1087 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1090 template<typename M1, typename M2>
1091 class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
1095 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1096 typedef typename Parent::Key Key;
1097 typedef typename Parent::Value Value;
1100 ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
1102 Value operator[](Key k) const {return m1[k];}
1104 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
1107 ///Returns a \c ForkMap class
1109 ///This function just returns a \c ForkMap class.
1111 template <typename M1, typename M2>
1112 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
1113 return ForkMap<M1, M2>(m1,m2);
1116 ///Returns a \c ForkWriteMap class
1118 ///This function just returns a \c ForkWriteMap class.
1119 ///\relates ForkWriteMap
1120 template <typename M1, typename M2>
1121 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
1122 return ForkWriteMap<M1, M2>(m1,m2);
1127 /* ************* BOOL MAPS ******************* */
1129 ///Logical 'not' of a map
1131 ///This bool \c concepts::ReadMap "read only map" returns the
1132 ///logical negation of the value returned by the given map.
1133 ///Its \c Key is inherited from \c M, its Value is \c bool.
1136 template <typename M>
1137 class NotMap : public MapBase<typename M::Key, bool> {
1140 typedef MapBase<typename M::Key, bool> Parent;
1141 typedef typename Parent::Key Key;
1142 typedef typename Parent::Value Value;
1145 NotMap(const M &_m) : m(_m) {};
1147 Value operator[](Key k) const {return !m[k];}
1150 ///Logical 'not' of a map (ReadWrie version)
1152 ///This bool \c concepts::ReadWriteMap "read-write map" returns the
1153 ///logical negation of the value returned by the given map. When it is set,
1154 ///the opposite value is set to the original map.
1155 ///Its \c Key is inherited from \c M, its Value is \c bool.
1158 template <typename M>
1159 class NotWriteMap : public MapBase<typename M::Key, bool> {
1162 typedef MapBase<typename M::Key, bool> Parent;
1163 typedef typename Parent::Key Key;
1164 typedef typename Parent::Value Value;
1167 NotWriteMap(M &_m) : m(_m) {};
1169 Value operator[](Key k) const {return !m[k];}
1171 void set(Key k, bool v) { m.set(k, !v); }
1174 ///Returns a \c NotMap class
1176 ///This function just returns a \c NotMap class.
1178 template <typename M>
1179 inline NotMap<M> notMap(const M &m) {
1180 return NotMap<M>(m);
1183 ///Returns a \c NotWriteMap class
1185 ///This function just returns a \c NotWriteMap class.
1186 ///\relates NotWriteMap
1187 template <typename M>
1188 inline NotWriteMap<M> notMap(M &m) {
1189 return NotWriteMap<M>(m);
1192 namespace _maps_bits {
1194 template <typename Value>
1196 typedef Value argument_type;
1197 typedef Value result_type;
1198 Value operator()(const Value& val) const {
1203 template <typename _Iterator, typename Enable = void>
1204 struct IteratorTraits {
1205 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1208 template <typename _Iterator>
1209 struct IteratorTraits<_Iterator,
1210 typename exists<typename _Iterator::container_type>::type>
1212 typedef typename _Iterator::container_type::value_type Value;
1218 /// \brief Writable bool map for logging each \c true assigned element
1220 /// Writable bool map for logging each \c true assigned element, i.e it
1221 /// copies all the keys set to \c true to the given iterator.
1223 /// \note The container of the iterator should contain space
1224 /// for each element.
1226 /// The following example shows how you can write the edges found by the Prim
1227 /// algorithm directly
1228 /// to the standard output.
1230 /// typedef IdMap<Graph, Edge> EdgeIdMap;
1231 /// EdgeIdMap edgeId(graph);
1233 /// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;
1234 /// EdgeIdFunctor edgeIdFunctor(edgeId);
1236 /// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor>
1237 /// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor);
1239 /// prim(graph, cost, writerMap);
1242 ///\sa BackInserterBoolMap
1244 ///\todo Revise the name of this class and the related ones.
1245 template <typename _Iterator,
1247 _maps_bits::Identity<typename _maps_bits::
1248 IteratorTraits<_Iterator>::Value> >
1249 class StoreBoolMap {
1251 typedef _Iterator Iterator;
1253 typedef typename _Functor::argument_type Key;
1256 typedef _Functor Functor;
1259 StoreBoolMap(Iterator it, const Functor& functor = Functor())
1260 : _begin(it), _end(it), _functor(functor) {}
1262 /// Gives back the given iterator set for the first key
1263 Iterator begin() const {
1267 /// Gives back the the 'after the last' iterator
1268 Iterator end() const {
1272 /// The \c set function of the map
1273 void set(const Key& key, Value value) const {
1275 *_end++ = _functor(key);
1281 mutable Iterator _end;
1285 /// \brief Writable bool map for logging each \c true assigned element in
1286 /// a back insertable container.
1288 /// Writable bool map for logging each \c true assigned element by pushing
1289 /// them into a back insertable container.
1290 /// It can be used to retrieve the items into a standard
1291 /// container. The next example shows how you can store the
1292 /// edges found by the Prim algorithm in a vector.
1295 /// vector<Edge> span_tree_edges;
1296 /// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges);
1297 /// prim(graph, cost, inserter_map);
1301 ///\sa FrontInserterBoolMap
1302 ///\sa InserterBoolMap
1303 template <typename Container,
1305 _maps_bits::Identity<typename Container::value_type> >
1306 class BackInserterBoolMap {
1308 typedef typename Container::value_type Key;
1312 BackInserterBoolMap(Container& _container,
1313 const Functor& _functor = Functor())
1314 : container(_container), functor(_functor) {}
1316 /// The \c set function of the map
1317 void set(const Key& key, Value value) {
1319 container.push_back(functor(key));
1324 Container& container;
1328 /// \brief Writable bool map for logging each \c true assigned element in
1329 /// a front insertable container.
1331 /// Writable bool map for logging each \c true assigned element by pushing
1332 /// them into a front insertable container.
1333 /// It can be used to retrieve the items into a standard
1334 /// container. For example see \ref BackInserterBoolMap.
1336 ///\sa BackInserterBoolMap
1337 ///\sa InserterBoolMap
1338 template <typename Container,
1340 _maps_bits::Identity<typename Container::value_type> >
1341 class FrontInserterBoolMap {
1343 typedef typename Container::value_type Key;
1347 FrontInserterBoolMap(Container& _container,
1348 const Functor& _functor = Functor())
1349 : container(_container), functor(_functor) {}
1351 /// The \c set function of the map
1352 void set(const Key& key, Value value) {
1354 container.push_front(functor(key));
1359 Container& container;
1363 /// \brief Writable bool map for storing each \c true assigned element in
1364 /// an insertable container.
1366 /// Writable bool map for storing each \c true assigned element in an
1367 /// insertable container. It will insert all the keys set to \c true into
1370 /// For example, if you want to store the cut arcs of the strongly
1371 /// connected components in a set you can use the next code:
1374 /// set<Arc> cut_arcs;
1375 /// InserterBoolMap<set<Arc> > inserter_map(cut_arcs);
1376 /// stronglyConnectedCutArcs(digraph, cost, inserter_map);
1379 ///\sa BackInserterBoolMap
1380 ///\sa FrontInserterBoolMap
1381 template <typename Container,
1383 _maps_bits::Identity<typename Container::value_type> >
1384 class InserterBoolMap {
1386 typedef typename Container::value_type Key;
1389 /// Constructor with specified iterator
1391 /// Constructor with specified iterator.
1392 /// \param _container The container for storing the elements.
1393 /// \param _it The elements will be inserted before this iterator.
1394 /// \param _functor The functor that is used when an element is stored.
1395 InserterBoolMap(Container& _container, typename Container::iterator _it,
1396 const Functor& _functor = Functor())
1397 : container(_container), it(_it), functor(_functor) {}
1401 /// Constructor without specified iterator.
1402 /// The elements will be inserted before <tt>_container.end()</tt>.
1403 /// \param _container The container for storing the elements.
1404 /// \param _functor The functor that is used when an element is stored.
1405 InserterBoolMap(Container& _container, const Functor& _functor = Functor())
1406 : container(_container), it(_container.end()), functor(_functor) {}
1408 /// The \c set function of the map
1409 void set(const Key& key, Value value) {
1411 it = container.insert(it, functor(key));
1417 Container& container;
1418 typename Container::iterator it;
1422 /// \brief Writable bool map for filling each \c true assigned element with a
1425 /// Writable bool map for filling each \c true assigned element with a
1426 /// given value. The value can set the container.
1428 /// The following code finds the connected components of a graph
1429 /// and stores it in the \c comp map:
1431 /// typedef Graph::NodeMap<int> ComponentMap;
1432 /// ComponentMap comp(graph);
1433 /// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap;
1434 /// ComponentFillerMap filler(comp, 0);
1436 /// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph);
1437 /// dfs.processedMap(filler);
1439 /// for (NodeIt it(graph); it != INVALID; ++it) {
1440 /// if (!dfs.reached(it)) {
1441 /// dfs.addSource(it);
1443 /// ++filler.fillValue();
1447 template <typename Map>
1450 typedef typename Map::Key Key;
1454 FillBoolMap(Map& _map, const typename Map::Value& _fill)
1455 : map(_map), fill(_fill) {}
1458 FillBoolMap(Map& _map)
1459 : map(_map), fill() {}
1461 /// Gives back the current fill value
1462 const typename Map::Value& fillValue() const {
1466 /// Gives back the current fill value
1467 typename Map::Value& fillValue() {
1471 /// Sets the current fill value
1472 void fillValue(const typename Map::Value& _fill) {
1476 /// The \c set function of the map
1477 void set(const Key& key, Value value) {
1485 typename Map::Value fill;
1489 /// \brief Writable bool map for storing the sequence number of
1490 /// \c true assignments.
1492 /// Writable bool map that stores for each \c true assigned elements
1493 /// the sequence number of this setting.
1494 /// It makes it easy to calculate the leaving
1495 /// order of the nodes in the \c Dfs algorithm.
1498 /// typedef Digraph::NodeMap<int> OrderMap;
1499 /// OrderMap order(digraph);
1500 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1501 /// OrderSetterMap setter(order);
1502 /// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
1503 /// dfs.processedMap(setter);
1505 /// for (NodeIt it(digraph); it != INVALID; ++it) {
1506 /// if (!dfs.reached(it)) {
1507 /// dfs.addSource(it);
1513 /// The storing of the discovering order is more difficult because the
1514 /// ReachedMap should be readable in the dfs algorithm but the setting
1515 /// order map is not readable. Thus we must use the fork map:
1518 /// typedef Digraph::NodeMap<int> OrderMap;
1519 /// OrderMap order(digraph);
1520 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1521 /// OrderSetterMap setter(order);
1522 /// typedef Digraph::NodeMap<bool> StoreMap;
1523 /// StoreMap store(digraph);
1525 /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1526 /// ReachedMap reached(store, setter);
1528 /// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
1529 /// dfs.reachedMap(reached);
1531 /// for (NodeIt it(digraph); it != INVALID; ++it) {
1532 /// if (!dfs.reached(it)) {
1533 /// dfs.addSource(it);
1538 template <typename Map>
1539 class SettingOrderBoolMap {
1541 typedef typename Map::Key Key;
1545 SettingOrderBoolMap(Map& _map)
1546 : map(_map), counter(0) {}
1548 /// Number of set operations.
1553 /// Setter function of the map
1554 void set(const Key& key, Value value) {
1556 map.set(key, counter++);
1568 #endif // LEMON_MAPS_H