Doc improvements.
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 /// If you have to provide a map only for its type definitions,
56 /// or if you have to provide a writable map, but
57 /// data written to it will sent to <tt>/dev/null</tt>...
58 template<typename K, typename T>
59 class NullMap : public MapBase<K, T> {
61 typedef MapBase<K, T> Parent;
62 typedef typename Parent::Key Key;
63 typedef typename Parent::Value Value;
65 /// Gives back a default constructed element.
66 T operator[](const K&) const { return T(); }
67 /// Absorbs the value.
68 void set(const K&, const T&) {}
71 template <typename K, typename V>
72 NullMap<K, V> nullMap() {
73 return NullMap<K, V>();
79 /// This is a readable map which assigns a specified value to each key.
80 /// In other aspects it is equivalent to the \c NullMap.
81 template<typename K, typename T>
82 class ConstMap : public MapBase<K, T> {
87 typedef MapBase<K, T> Parent;
88 typedef typename Parent::Key Key;
89 typedef typename Parent::Value Value;
91 /// Default constructor
93 /// The value of the map will be uninitialized.
94 /// (More exactly it will be default constructed.)
98 /// \param _v The initial value of the map.
100 ConstMap(const T &_v) : v(_v) {}
103 T operator[](const K&) const { return v; }
106 void setAll(const T &t) {
110 template<typename T1>
112 typedef ConstMap<K, T1> other;
115 template<typename T1>
116 ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
119 ///Returns a \c ConstMap class
121 ///This function just returns a \c ConstMap class.
123 template<typename K, typename V>
124 inline ConstMap<K, V> constMap(const V &v) {
125 return ConstMap<K, V>(v);
129 template<typename T, T v>
132 /// Constant map with inlined constant value.
134 /// This is a readable map which assigns a specified value to each key.
135 /// In other aspects it is equivalent to the \c NullMap.
136 template<typename K, typename V, V v>
137 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
139 typedef MapBase<K, V> Parent;
140 typedef typename Parent::Key Key;
141 typedef typename Parent::Value Value;
145 V operator[](const K&) const { return v; }
147 void set(const K&, const V&) { }
150 ///Returns a \c ConstMap class
152 ///This function just returns a \c ConstMap class with inlined value.
154 template<typename K, typename V, V v>
155 inline ConstMap<K, Const<V, v> > constMap() {
156 return ConstMap<K, Const<V, v> >();
159 ///Map based on std::map
161 ///This is essentially a wrapper for \c std::map. With addition that
162 ///you can specify a default value different from \c Value() .
163 template <typename K, typename T, typename Compare = std::less<K> >
165 template <typename K1, typename T1, typename C1>
169 typedef True ReferenceMapTag;
175 typedef T& Reference;
177 typedef const T& ConstReference;
181 typedef std::map<K, T, Compare> Map;
187 /// Constructor with specified default value
188 StdMap(const T& value = T()) : _value(value) {}
189 /// \brief Constructs the map from an appropriate std::map, and explicitly
190 /// specifies a default value.
191 template <typename T1, typename Comp1>
192 StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T())
193 : _map(map.begin(), map.end()), _value(value) {}
195 /// \brief Constructs a map from an other StdMap.
196 template<typename T1, typename Comp1>
197 StdMap(const StdMap<Key, T1, Comp1> &c)
198 : _map(c._map.begin(), c._map.end()), _value(c._value) {}
202 StdMap& operator=(const StdMap&);
207 Reference operator[](const Key &k) {
208 typename Map::iterator it = _map.lower_bound(k);
209 if (it != _map.end() && !_map.key_comp()(k, it->first))
212 return _map.insert(it, std::make_pair(k, _value))->second;
216 ConstReference operator[](const Key &k) const {
217 typename Map::const_iterator it = _map.find(k);
218 if (it != _map.end())
225 void set(const Key &k, const T &t) {
226 typename Map::iterator it = _map.lower_bound(k);
227 if (it != _map.end() && !_map.key_comp()(k, it->first))
230 _map.insert(it, std::make_pair(k, t));
234 void setAll(const T &t) {
239 template <typename T1, typename C1 = std::less<T1> >
241 typedef StdMap<Key, T1, C1> other;
245 /// \brief Map for storing values for the range \c [0..size-1] range keys
247 /// The current map has the \c [0..size-1] keyset and the values
248 /// are stored in a \c std::vector<T> container. It can be used with
249 /// some data structures, for example \c UnionFind, \c BinHeap, when
250 /// the used items are small integer numbers.
252 /// \todo Revise its name
253 template <typename T>
256 template <typename T1>
257 friend class IntegerMap;
261 typedef True ReferenceMapTag;
267 typedef T& Reference;
269 typedef const T& ConstReference;
273 typedef std::vector<T> Vector;
278 /// Constructor with specified default value
279 IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
281 /// \brief Constructs the map from an appropriate std::vector.
282 template <typename T1>
283 IntegerMap(const std::vector<T1>& vector)
284 : _vector(vector.begin(), vector.end()) {}
286 /// \brief Constructs a map from an other IntegerMap.
287 template <typename T1>
288 IntegerMap(const IntegerMap<T1> &c)
289 : _vector(c._vector.begin(), c._vector.end()) {}
291 /// \brief Resize the container
292 void resize(int size, const T& value = T()) {
293 _vector.resize(size, value);
298 IntegerMap& operator=(const IntegerMap&);
303 Reference operator[](Key k) {
308 ConstReference operator[](Key k) const {
313 void set(const Key &k, const T& t) {
321 /// \addtogroup map_adaptors
324 /// \brief Identity mapping.
326 /// This mapping gives back the given key as value without any
328 template <typename T>
329 class IdentityMap : public MapBase<T, T> {
331 typedef MapBase<T, T> Parent;
332 typedef typename Parent::Key Key;
333 typedef typename Parent::Value Value;
336 const T& operator[](const T& t) const {
341 ///Returns an \c IdentityMap class
343 ///This function just returns an \c IdentityMap class.
344 ///\relates IdentityMap
346 inline IdentityMap<T> identityMap() {
347 return IdentityMap<T>();
351 ///\brief Convert the \c Value of a map to another type using
352 ///the default conversion.
354 ///This \c concepts::ReadMap "read only map"
355 ///converts the \c Value of a maps to type \c T.
356 ///Its \c Key is inherited from \c M.
357 template <typename M, typename T>
358 class ConvertMap : public MapBase<typename M::Key, T> {
361 typedef MapBase<typename M::Key, T> Parent;
362 typedef typename Parent::Key Key;
363 typedef typename Parent::Value Value;
368 ///\param _m is the underlying map
369 ConvertMap(const M &_m) : m(_m) {};
371 /// \brief The subscript operator.
373 /// The subscript operator.
374 Value operator[](const Key& k) const {return m[k];}
377 ///Returns an \c ConvertMap class
379 ///This function just returns an \c ConvertMap class.
380 ///\relates ConvertMap
381 template<typename T, typename M>
382 inline ConvertMap<M, T> convertMap(const M &m) {
383 return ConvertMap<M, T>(m);
386 ///Simple wrapping of the map
388 ///This \c concepts::ReadMap "read only map" returns the simple
389 ///wrapping of the given map. Sometimes the reference maps cannot be
390 ///combined with simple read maps. This map adaptor wraps the given
391 ///map to simple read map.
393 /// \todo Revise the misleading name
395 class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
399 typedef MapBase<typename M::Key, typename M::Value> Parent;
400 typedef typename Parent::Key Key;
401 typedef typename Parent::Value Value;
404 SimpleMap(const M &_m) : m(_m) {};
406 Value operator[](Key k) const {return m[k];}
409 ///Simple writeable wrapping of the map
411 ///This \c concepts::WriteMap "write map" returns the simple
412 ///wrapping of the given map. Sometimes the reference maps cannot be
413 ///combined with simple read-write maps. This map adaptor wraps the
414 ///given map to simple read-write map.
416 /// \todo Revise the misleading name
418 class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
422 typedef MapBase<typename M::Key, typename M::Value> Parent;
423 typedef typename Parent::Key Key;
424 typedef typename Parent::Value Value;
427 SimpleWriteMap(M &_m) : m(_m) {};
429 Value operator[](Key k) const {return m[k];}
431 void set(Key k, const Value& c) { m.set(k, c); }
436 ///This \c concepts::ReadMap "read only map" returns the sum of the two
437 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
438 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
440 template<typename M1, typename M2>
441 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
446 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
447 typedef typename Parent::Key Key;
448 typedef typename Parent::Value Value;
451 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
453 Value operator[](Key k) const {return m1[k]+m2[k];}
456 ///Returns an \c AddMap class
458 ///This function just returns an \c AddMap class.
459 ///\todo How to call these type of functions?
462 template<typename M1, typename M2>
463 inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
464 return AddMap<M1, M2>(m1,m2);
467 ///Shift a map with a constant.
469 ///This \c concepts::ReadMap "read only map" returns the sum of the
470 ///given map and a constant value.
471 ///Its \c Key and \c Value is inherited from \c M.
475 /// ShiftMap<X> sh(x,v);
477 ///is equivalent with
479 /// ConstMap<X::Key, X::Value> c_tmp(v);
480 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
482 template<typename M, typename C = typename M::Value>
483 class ShiftMap : 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 shift value
496 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
498 Value operator[](Key k) const {return m[k] + v;}
501 ///Shift a map with a constant. This map is also writable.
503 ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
504 ///given map and a constant value. It makes also possible to write the map.
505 ///Its \c Key and \c Value is inherited from \c M.
509 /// ShiftMap<X> sh(x,v);
511 ///is equivalent with
513 /// ConstMap<X::Key, X::Value> c_tmp(v);
514 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
516 template<typename M, typename C = typename M::Value>
517 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
521 typedef MapBase<typename M::Key, typename M::Value> Parent;
522 typedef typename Parent::Key Key;
523 typedef typename Parent::Value Value;
528 ///\param _m is the undelying map
529 ///\param _v is the shift value
530 ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
532 Value operator[](Key k) const {return m[k] + v;}
534 void set(Key k, const Value& c) { m.set(k, c - v); }
537 ///Returns an \c ShiftMap class
539 ///This function just returns an \c ShiftMap class.
541 template<typename M, typename C>
542 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
543 return ShiftMap<M, C>(m,v);
546 template<typename M, typename C>
547 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
548 return ShiftWriteMap<M, C>(m,v);
551 ///Difference of two maps
553 ///This \c concepts::ReadMap "read only map" returns the difference
554 ///of the values of the two
555 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
556 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
558 /// \todo Revise the misleading name
559 template<typename M1, typename M2>
560 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
564 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
565 typedef typename Parent::Key Key;
566 typedef typename Parent::Value Value;
569 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
571 Value operator[](Key k) const {return m1[k]-m2[k];}
574 ///Returns a \c SubMap class
576 ///This function just returns a \c SubMap class.
579 template<typename M1, typename M2>
580 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
581 return SubMap<M1, M2>(m1, m2);
584 ///Product of two maps
586 ///This \c concepts::ReadMap "read only map" returns the product of the
589 ///maps. Its \c Key and \c Value will be inherited from \c M1.
590 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
592 template<typename M1, typename M2>
593 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
597 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
598 typedef typename Parent::Key Key;
599 typedef typename Parent::Value Value;
602 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
604 Value operator[](Key k) const {return m1[k]*m2[k];}
607 ///Returns a \c MulMap class
609 ///This function just returns a \c MulMap class.
611 template<typename M1, typename M2>
612 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
613 return MulMap<M1, M2>(m1,m2);
616 ///Scales a maps with a constant.
618 ///This \c concepts::ReadMap "read only map" returns the value of the
619 ///given map multiplied from the left side with a constant value.
620 ///Its \c Key and \c Value is inherited from \c M.
624 /// ScaleMap<X> sc(x,v);
626 ///is equivalent with
628 /// ConstMap<X::Key, X::Value> c_tmp(v);
629 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
631 template<typename M, typename C = typename M::Value>
632 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
636 typedef MapBase<typename M::Key, typename M::Value> Parent;
637 typedef typename Parent::Key Key;
638 typedef typename Parent::Value Value;
643 ///\param _m is the undelying map
644 ///\param _v is the scaling value
645 ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
647 Value operator[](Key k) const {return v * m[k];}
650 ///Scales a maps with a constant (ReadWrite version).
652 ///This \c concepts::ReadWriteMap "read-write map" returns the value of the
653 ///given map multiplied from the left side with a constant value. It can
654 ///be used as write map also if the given multiplier is not zero.
655 ///Its \c Key and \c Value is inherited from \c M.
656 template<typename M, typename C = typename M::Value>
657 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
661 typedef MapBase<typename M::Key, typename M::Value> Parent;
662 typedef typename Parent::Key Key;
663 typedef typename Parent::Value Value;
668 ///\param _m is the undelying map
669 ///\param _v is the scaling value
670 ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
672 Value operator[](Key k) const {return v * m[k];}
674 void set(Key k, const Value& c) { m.set(k, c / v);}
677 ///Returns an \c ScaleMap class
679 ///This function just returns an \c ScaleMap class.
681 template<typename M, typename C>
682 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
683 return ScaleMap<M, C>(m,v);
686 template<typename M, typename C>
687 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
688 return ScaleWriteMap<M, C>(m,v);
691 ///Quotient of two maps
693 ///This \c concepts::ReadMap "read only map" returns the quotient of the
695 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
696 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
698 template<typename M1, typename M2>
699 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
703 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
704 typedef typename Parent::Key Key;
705 typedef typename Parent::Value Value;
708 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
710 Value operator[](Key k) const {return m1[k]/m2[k];}
713 ///Returns a \c DivMap class
715 ///This function just returns a \c DivMap class.
717 template<typename M1, typename M2>
718 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
719 return DivMap<M1, M2>(m1,m2);
722 ///Composition of two maps
724 ///This \c concepts::ReadMap "read only map" returns the composition of
726 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
730 /// ComposeMap<M1, M2> cm(m1,m2);
732 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
734 ///Its \c Key is inherited from \c M2 and its \c Value is from
736 ///The \c M2::Value must be convertible to \c M1::Key.
737 ///\todo Check the requirements.
738 template <typename M1, typename M2>
739 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
743 typedef MapBase<typename M2::Key, typename M1::Value> Parent;
744 typedef typename Parent::Key Key;
745 typedef typename Parent::Value Value;
748 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
753 /// \todo Use the MapTraits once it is ported.
756 //typename MapTraits<M1>::ConstReturnValue
758 operator[](Key k) const {return m1[m2[k]];}
760 ///Returns a \c ComposeMap class
762 ///This function just returns a \c ComposeMap class.
764 ///\relates ComposeMap
765 template <typename M1, typename M2>
766 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
767 return ComposeMap<M1, M2>(m1,m2);
770 ///Combines of two maps using an STL (binary) functor.
772 ///Combines of two maps using an STL (binary) functor.
775 ///This \c concepts::ReadMap "read only map" takes two maps and a
776 ///binary functor and returns the composition of
778 ///given maps unsing the functor.
779 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
780 ///and \c f is of \c F,
783 /// CombineMap<M1, M2,F,V> cm(m1,m2,f);
785 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
787 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
788 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
789 ///input parameter of \c F and the return type of \c F must be convertible
791 ///\todo Check the requirements.
792 template<typename M1, typename M2, typename F,
793 typename V = typename F::result_type>
794 class CombineMap : public MapBase<typename M1::Key, V> {
799 typedef MapBase<typename M1::Key, V> Parent;
800 typedef typename Parent::Key Key;
801 typedef typename Parent::Value Value;
804 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
805 : m1(_m1), m2(_m2), f(_f) {};
807 Value operator[](Key k) const {return f(m1[k],m2[k]);}
810 ///Returns a \c CombineMap class
812 ///This function just returns a \c CombineMap class.
814 ///For example if \c m1 and \c m2 are both \c double valued maps, then
816 ///combineMap<double>(m1,m2,std::plus<double>())
818 ///is equivalent with
823 ///This function is specialized for adaptable binary function
824 ///classes and c++ functions.
826 ///\relates CombineMap
827 template<typename M1, typename M2, typename F, typename V>
828 inline CombineMap<M1, M2, F, V>
829 combineMap(const M1& m1,const M2& m2, const F& f) {
830 return CombineMap<M1, M2, F, V>(m1,m2,f);
833 template<typename M1, typename M2, typename F>
834 inline CombineMap<M1, M2, F, typename F::result_type>
835 combineMap(const M1& m1, const M2& m2, const F& f) {
836 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
839 template<typename M1, typename M2, typename K1, typename K2, typename V>
840 inline CombineMap<M1, M2, V (*)(K1, K2), V>
841 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
842 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
845 ///Negative value of a map
847 ///This \c concepts::ReadMap "read only map" returns the negative
849 ///value returned by the
850 ///given map. Its \c Key and \c Value will be inherited from \c M.
851 ///The unary \c - operator must be defined for \c Value, of course.
854 class NegMap : public MapBase<typename M::Key, typename M::Value> {
857 typedef MapBase<typename M::Key, typename M::Value> Parent;
858 typedef typename Parent::Key Key;
859 typedef typename Parent::Value Value;
862 NegMap(const M &_m) : m(_m) {};
864 Value operator[](Key k) const {return -m[k];}
867 ///Negative value of a map (ReadWrite version)
869 ///This \c concepts::ReadWriteMap "read-write map" returns the negative
870 ///value of the value returned by the
871 ///given map. Its \c Key and \c Value will be inherited from \c M.
872 ///The unary \c - operator must be defined for \c Value, of course.
875 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
878 typedef MapBase<typename M::Key, typename M::Value> Parent;
879 typedef typename Parent::Key Key;
880 typedef typename Parent::Value Value;
883 NegWriteMap(M &_m) : m(_m) {};
885 Value operator[](Key k) const {return -m[k];}
887 void set(Key k, const Value& v) { m.set(k, -v); }
890 ///Returns a \c NegMap class
892 ///This function just returns a \c NegMap class.
894 template <typename M>
895 inline NegMap<M> negMap(const M &m) {
899 template <typename M>
900 inline NegWriteMap<M> negMap(M &m) {
901 return NegWriteMap<M>(m);
904 ///Absolute value of a map
906 ///This \c concepts::ReadMap "read only map" returns the absolute value
908 ///value returned by the
909 ///given map. Its \c Key and \c Value will be inherited
910 ///from <tt>M</tt>. <tt>Value</tt>
911 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
912 ///operator must be defined for it, of course.
916 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
919 typedef MapBase<typename M::Key, typename M::Value> Parent;
920 typedef typename Parent::Key Key;
921 typedef typename Parent::Value Value;
924 AbsMap(const M &_m) : m(_m) {};
926 Value operator[](Key k) const {
928 return tmp >= 0 ? tmp : -tmp;
933 ///Returns a \c AbsMap class
935 ///This function just returns a \c AbsMap class.
938 inline AbsMap<M> absMap(const M &m) {
942 ///Converts an STL style functor to a map
944 ///This \c concepts::ReadMap "read only map" returns the value
948 ///Template parameters \c K and \c V will become its
949 ///\c Key and \c Value. They must be given explicitely
950 ///because a functor does not provide such typedefs.
952 ///Parameter \c F is the type of the used functor.
954 typename K = typename F::argument_type,
955 typename V = typename F::result_type>
956 class FunctorMap : public MapBase<K, V> {
959 typedef MapBase<K, V> Parent;
960 typedef typename Parent::Key Key;
961 typedef typename Parent::Value Value;
964 FunctorMap(const F &_f = F()) : f(_f) {}
966 Value operator[](Key k) const { return f(k);}
969 ///Returns a \c FunctorMap class
971 ///This function just returns a \c FunctorMap class.
973 ///It is specialized for adaptable function classes and
975 ///\relates FunctorMap
976 template<typename K, typename V, typename F> inline
977 FunctorMap<F, K, V> functorMap(const F &f) {
978 return FunctorMap<F, K, V>(f);
981 template <typename F> inline
982 FunctorMap<F, typename F::argument_type, typename F::result_type>
983 functorMap(const F &f) {
984 return FunctorMap<F, typename F::argument_type,
985 typename F::result_type>(f);
988 template <typename K, typename V> inline
989 FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
990 return FunctorMap<V (*)(K), K, V>(f);
994 ///Converts a map to an STL style (unary) functor
996 ///This class Converts a map to an STL style (unary) functor.
997 ///that is it provides an <tt>operator()</tt> to read its values.
999 ///For the sake of convenience it also works as
1000 ///a ususal \c concepts::ReadMap "readable map",
1001 ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
1002 template <typename M>
1003 class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
1006 typedef MapBase<typename M::Key, typename M::Value> Parent;
1007 typedef typename Parent::Key Key;
1008 typedef typename Parent::Value Value;
1010 typedef typename M::Key argument_type;
1011 typedef typename M::Value result_type;
1014 MapFunctor(const M &_m) : m(_m) {};
1016 Value operator()(Key k) const {return m[k];}
1018 Value operator[](Key k) const {return m[k];}
1021 ///Returns a \c MapFunctor class
1023 ///This function just returns a \c MapFunctor class.
1024 ///\relates MapFunctor
1025 template<typename M>
1026 inline MapFunctor<M> mapFunctor(const M &m) {
1027 return MapFunctor<M>(m);
1030 ///Applies all map setting operations to two maps
1032 ///This map has two \c concepts::ReadMap "readable map"
1033 ///parameters and each read request will be passed just to the
1034 ///first map. This class is the just readable map type of the ForkWriteMap.
1036 ///The \c Key and \c Value will be inherited from \c M1.
1037 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1039 /// \todo Why is it needed?
1040 template<typename M1, typename M2>
1041 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
1045 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1046 typedef typename Parent::Key Key;
1047 typedef typename Parent::Value Value;
1050 ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
1052 Value operator[](Key k) const {return m1[k];}
1056 ///Applies all map setting operations to two maps
1058 ///This map has two \c concepts::WriteMap "writable map"
1059 ///parameters and each write request will be passed to both of them.
1060 ///If \c M1 is also \c concepts::ReadMap "readable",
1061 ///then the read operations will return the
1062 ///corresponding values of \c M1.
1064 ///The \c Key and \c Value will be inherited from \c M1.
1065 ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1066 template<typename M1, typename M2>
1067 class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
1071 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1072 typedef typename Parent::Key Key;
1073 typedef typename Parent::Value Value;
1076 ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
1078 Value operator[](Key k) const {return m1[k];}
1080 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
1083 ///Returns an \c ForkMap class
1085 ///This function just returns an \c ForkMap class.
1088 template <typename M1, typename M2>
1089 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
1090 return ForkMap<M1, M2>(m1,m2);
1093 template <typename M1, typename M2>
1094 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
1095 return ForkWriteMap<M1, M2>(m1,m2);
1100 /* ************* BOOL MAPS ******************* */
1102 ///Logical 'not' of a map
1104 ///This bool \c concepts::ReadMap "read only map" returns the
1105 ///logical negation of
1106 ///value returned by the
1107 ///given map. Its \c Key and will be inherited from \c M,
1108 ///its Value is <tt>bool</tt>.
1109 template <typename M>
1110 class NotMap : public MapBase<typename M::Key, bool> {
1113 typedef MapBase<typename M::Key, bool> Parent;
1114 typedef typename Parent::Key Key;
1115 typedef typename Parent::Value Value;
1118 NotMap(const M &_m) : m(_m) {};
1120 Value operator[](Key k) const {return !m[k];}
1123 ///Logical 'not' of a map (ReadWrie version)
1125 ///This bool \c concepts::ReadWriteMap "read-write map" returns the
1126 ///logical negation of value returned by the given map. When it is set,
1127 ///the opposite value is set to the original map.
1128 ///Its \c Key and will be inherited from \c M,
1129 ///its Value is <tt>bool</tt>.
1130 template <typename M>
1131 class NotWriteMap : public MapBase<typename M::Key, bool> {
1134 typedef MapBase<typename M::Key, bool> Parent;
1135 typedef typename Parent::Key Key;
1136 typedef typename Parent::Value Value;
1139 NotWriteMap(M &_m) : m(_m) {};
1141 Value operator[](Key k) const {return !m[k];}
1143 void set(Key k, bool v) { m.set(k, !v); }
1146 ///Returns a \c NotMap class
1148 ///This function just returns a \c NotMap class.
1150 template <typename M>
1151 inline NotMap<M> notMap(const M &m) {
1152 return NotMap<M>(m);
1155 template <typename M>
1156 inline NotWriteMap<M> notMap(M &m) {
1157 return NotWriteMap<M>(m);
1160 namespace _maps_bits {
1162 template <typename Value>
1164 typedef Value argument_type;
1165 typedef Value result_type;
1166 Value operator()(const Value& val) const {
1171 template <typename _Iterator, typename Enable = void>
1172 struct IteratorTraits {
1173 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1176 template <typename _Iterator>
1177 struct IteratorTraits<_Iterator,
1178 typename exists<typename _Iterator::container_type>::type>
1180 typedef typename _Iterator::container_type::value_type Value;
1186 /// \brief Writable bool map for logging each true assigned elements
1188 /// Writable bool map for logging each true assigned elements, i.e it
1189 /// copies all the keys set to true to the given iterator.
1191 /// \note The container of the iterator should contain space
1192 /// for each element.
1194 /// The following example shows how you can write the edges found by the Prim
1195 /// algorithm directly
1196 /// to the standard output.
1198 /// typedef IdMap<Graph, Edge> EdgeIdMap;
1199 /// EdgeIdMap edgeId(graph);
1201 /// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;
1202 /// EdgeIdFunctor edgeIdFunctor(edgeId);
1204 /// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor>
1205 /// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor);
1207 /// prim(graph, cost, writerMap);
1210 ///\todo Revise the name of this class and the relates ones.
1211 template <typename _Iterator,
1213 _maps_bits::Identity<typename _maps_bits::
1214 IteratorTraits<_Iterator>::Value> >
1215 class StoreBoolMap {
1217 typedef _Iterator Iterator;
1219 typedef typename _Functor::argument_type Key;
1222 typedef _Functor Functor;
1225 StoreBoolMap(Iterator it, const Functor& functor = Functor())
1226 : _begin(it), _end(it), _functor(functor) {}
1228 /// Gives back the given iterator set for the first key
1229 Iterator begin() const {
1233 /// Gives back the the 'after the last' iterator
1234 Iterator end() const {
1238 /// Setter function of the map
1239 void set(const Key& key, Value value) const {
1241 *_end++ = _functor(key);
1247 mutable Iterator _end;
1251 /// \brief Writable bool map for logging each true assigned elements in
1252 /// a back insertable container
1254 /// Writable bool map for logging each true assigned elements by pushing
1255 /// back them into a back insertable container.
1256 /// It can be used to retrieve the items into a standard
1257 /// container. The next example shows how you can store the
1258 /// edges found by the Prim algorithm in a vector.
1261 /// vector<Edge> span_tree_edges;
1262 /// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges);
1263 /// prim(graph, cost, inserter_map);
1265 template <typename Container,
1267 _maps_bits::Identity<typename Container::value_type> >
1268 class BackInserterBoolMap {
1270 typedef typename Container::value_type Key;
1274 BackInserterBoolMap(Container& _container,
1275 const Functor& _functor = Functor())
1276 : container(_container), functor(_functor) {}
1278 /// Setter function of the map
1279 void set(const Key& key, Value value) {
1281 container.push_back(functor(key));
1286 Container& container;
1290 /// \brief Writable bool map for storing each true assignments in
1291 /// a front insertable container.
1293 /// Writable bool map for storing each true assignment in a front
1294 /// insertable container. It will push front all the keys set to \c true into
1295 /// the container. For example see the BackInserterBoolMap.
1296 template <typename Container,
1298 _maps_bits::Identity<typename Container::value_type> >
1299 class FrontInserterBoolMap {
1301 typedef typename Container::value_type Key;
1305 FrontInserterBoolMap(Container& _container,
1306 const Functor& _functor = Functor())
1307 : container(_container), functor(_functor) {}
1309 /// Setter function of the map
1310 void set(const Key& key, Value value) {
1312 container.push_front(key);
1317 Container& container;
1321 /// \brief Writable bool map for storing each true assigned elements in
1322 /// an insertable container.
1324 /// Writable bool map for storing each true assigned elements in an
1325 /// insertable container. It will insert all the keys set to \c true into
1328 /// For example, if you want to store the cut arcs of the strongly
1329 /// connected components in a set you can use the next code:
1332 /// set<Arc> cut_arcs;
1333 /// InserterBoolMap<set<Arc> > inserter_map(cut_arcs);
1334 /// stronglyConnectedCutArcs(digraph, cost, inserter_map);
1336 template <typename Container,
1338 _maps_bits::Identity<typename Container::value_type> >
1339 class InserterBoolMap {
1341 typedef typename Container::value_type Key;
1345 InserterBoolMap(Container& _container, typename Container::iterator _it,
1346 const Functor& _functor = Functor())
1347 : container(_container), it(_it), functor(_functor) {}
1350 InserterBoolMap(Container& _container, const Functor& _functor = Functor())
1351 : container(_container), it(_container.end()), functor(_functor) {}
1353 /// Setter function of the map
1354 void set(const Key& key, Value value) {
1356 it = container.insert(it, key);
1362 Container& container;
1363 typename Container::iterator it;
1367 /// \brief Fill the true set elements with a given value.
1369 /// Writable bool map to fill the elements set to \c true with a given value.
1370 /// The value can set
1373 /// The following code finds the connected components of a graph
1374 /// and stores it in the \c comp map:
1376 /// typedef Graph::NodeMap<int> ComponentMap;
1377 /// ComponentMap comp(graph);
1378 /// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap;
1379 /// ComponentFillerMap filler(comp, 0);
1381 /// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph);
1382 /// dfs.processedMap(filler);
1384 /// for (NodeIt it(graph); it != INVALID; ++it) {
1385 /// if (!dfs.reached(it)) {
1386 /// dfs.addSource(it);
1388 /// ++filler.fillValue();
1392 template <typename Map>
1395 typedef typename Map::Key Key;
1399 FillBoolMap(Map& _map, const typename Map::Value& _fill)
1400 : map(_map), fill(_fill) {}
1403 FillBoolMap(Map& _map)
1404 : map(_map), fill() {}
1406 /// Gives back the current fill value
1407 const typename Map::Value& fillValue() const {
1411 /// Gives back the current fill value
1412 typename Map::Value& fillValue() {
1416 /// Sets the current fill value
1417 void fillValue(const typename Map::Value& _fill) {
1421 /// Set function of the map
1422 void set(const Key& key, Value value) {
1430 typename Map::Value fill;
1434 /// \brief Writable bool map which stores the sequence number of
1435 /// true assignments.
1437 /// Writable bool map which stores for each true assigned elements
1438 /// the sequence number of this setting.
1439 /// It makes it easy to calculate the leaving
1440 /// order of the nodes in the \c Dfs algorithm.
1443 /// typedef Digraph::NodeMap<int> OrderMap;
1444 /// OrderMap order(digraph);
1445 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1446 /// OrderSetterMap setter(order);
1447 /// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
1448 /// dfs.processedMap(setter);
1450 /// for (NodeIt it(digraph); it != INVALID; ++it) {
1451 /// if (!dfs.reached(it)) {
1452 /// dfs.addSource(it);
1458 /// The storing of the discovering order is more difficult because the
1459 /// ReachedMap should be readable in the dfs algorithm but the setting
1460 /// order map is not readable. Thus we must use the fork map:
1463 /// typedef Digraph::NodeMap<int> OrderMap;
1464 /// OrderMap order(digraph);
1465 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1466 /// OrderSetterMap setter(order);
1467 /// typedef Digraph::NodeMap<bool> StoreMap;
1468 /// StoreMap store(digraph);
1470 /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1471 /// ReachedMap reached(store, setter);
1473 /// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
1474 /// dfs.reachedMap(reached);
1476 /// for (NodeIt it(digraph); it != INVALID; ++it) {
1477 /// if (!dfs.reached(it)) {
1478 /// dfs.addSource(it);
1483 template <typename Map>
1484 class SettingOrderBoolMap {
1486 typedef typename Map::Key Key;
1490 SettingOrderBoolMap(Map& _map)
1491 : map(_map), counter(0) {}
1493 /// Number of set operations.
1498 /// Setter function of the map
1499 void set(const Key& key, Value value) {
1501 map.set(key, counter++);
1513 #endif // LEMON_MAPS_H