Removed BOOST_CLASS_REQUIRE macros + added file description.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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>
47 /// The key type of the map.
49 /// The value type of the map. (The type of objects associated with the keys).
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 \ref concepts::ReadMap "readable" map which assigns a
85 /// specified value to each key.
86 /// In other aspects it is equivalent to \c NullMap.
87 template<typename K, typename T>
88 class ConstMap : public MapBase<K, T> {
93 typedef MapBase<K, T> Parent;
94 typedef typename Parent::Key Key;
95 typedef typename Parent::Value Value;
97 /// Default constructor
99 /// Default constructor.
100 /// The value of the map will be uninitialized.
101 /// (More exactly it will be default constructed.)
104 /// Constructor with specified initial value
106 /// Constructor with specified initial value.
107 /// \param _v is the initial value of the map.
108 ConstMap(const T &_v) : v(_v) {}
111 T operator[](const K&) const { return v; }
114 void setAll(const T &t) {
118 template<typename T1>
119 ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
122 ///Returns a \c ConstMap class
124 ///This function just returns a \c ConstMap class.
126 template<typename K, typename V>
127 inline ConstMap<K, V> constMap(const V &v) {
128 return ConstMap<K, V>(v);
132 template<typename T, T v>
135 /// Constant map with inlined constant value.
137 /// This is a \ref concepts::ReadMap "readable" map which assigns a
138 /// specified value to each key.
139 /// In other aspects it is equivalent to \c NullMap.
140 template<typename K, typename V, V v>
141 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
143 typedef MapBase<K, V> Parent;
144 typedef typename Parent::Key Key;
145 typedef typename Parent::Value Value;
149 V operator[](const K&) const { return v; }
151 void set(const K&, const V&) { }
154 ///Returns a \c ConstMap class with inlined value
156 ///This function just returns a \c ConstMap class with inlined value.
158 template<typename K, typename V, V v>
159 inline ConstMap<K, Const<V, v> > constMap() {
160 return ConstMap<K, Const<V, v> >();
163 ///Map based on \c std::map
165 ///This is essentially a wrapper for \c std::map with addition that
166 ///you can specify a default value different from \c Value().
167 ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
168 template <typename K, typename T, typename Compare = std::less<K> >
169 class StdMap : public MapBase<K, T> {
170 template <typename K1, typename T1, typename C1>
174 typedef MapBase<K, T> Parent;
176 typedef typename Parent::Key Key;
178 typedef typename Parent::Value Value;
180 typedef T& Reference;
181 ///Const reference type
182 typedef const T& ConstReference;
184 typedef True ReferenceMapTag;
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 \c std::map, and
197 /// explicitly 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 \ref 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) {
248 ///Returns a \c StdMap class
250 ///This function just returns a \c StdMap class with specified
253 template<typename K, typename V, typename Compare = std::less<K> >
254 inline StdMap<K, V, Compare> stdMap(const V& value = V()) {
255 return StdMap<K, V, Compare>(value);
258 ///Returns a \c StdMap class created from an appropriate std::map
260 ///This function just returns a \c StdMap class created from an
261 ///appropriate std::map.
263 template<typename K, typename V, typename Compare = std::less<K> >
264 inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map,
265 const V& value = V() ) {
266 return StdMap<K, V, Compare>(map, value);
269 /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
271 /// This map has the <tt>[0..size-1]</tt> keyset and the values
272 /// are stored in a \c std::vector<T> container. It can be used with
273 /// some data structures, for example \c UnionFind, \c BinHeap, when
274 /// the used items are small integer numbers.
275 /// This map meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
277 /// \todo Revise its name
278 template <typename T>
279 class IntegerMap : public MapBase<int, T> {
281 template <typename T1>
282 friend class IntegerMap;
286 typedef MapBase<int, T> Parent;
288 typedef typename Parent::Key Key;
290 typedef typename Parent::Value Value;
292 typedef T& Reference;
294 typedef const T& ConstReference;
296 typedef True ReferenceMapTag;
300 typedef std::vector<T> Vector;
305 /// Constructor with specified default value
306 IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
308 /// \brief Constructs the map from an appropriate \c std::vector.
309 template <typename T1>
310 IntegerMap(const std::vector<T1>& vector)
311 : _vector(vector.begin(), vector.end()) {}
313 /// \brief Constructs a map from an other \ref IntegerMap.
314 template <typename T1>
315 IntegerMap(const IntegerMap<T1> &c)
316 : _vector(c._vector.begin(), c._vector.end()) {}
318 /// \brief Resize the container
319 void resize(int size, const T& value = T()) {
320 _vector.resize(size, value);
325 IntegerMap& operator=(const IntegerMap&);
330 Reference operator[](Key k) {
335 ConstReference operator[](Key k) const {
340 void set(const Key &k, const T& t) {
346 ///Returns an \c IntegerMap class
348 ///This function just returns an \c IntegerMap class.
349 ///\relates IntegerMap
351 inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) {
352 return IntegerMap<T>(size, value);
357 /// \addtogroup map_adaptors
360 /// \brief Identity map.
362 /// This map gives back the given key as value without any
364 template <typename T>
365 class IdentityMap : public MapBase<T, T> {
367 typedef MapBase<T, T> Parent;
368 typedef typename Parent::Key Key;
369 typedef typename Parent::Value Value;
372 const T& operator[](const T& t) const {
377 ///Returns an \c IdentityMap class
379 ///This function just returns an \c IdentityMap class.
380 ///\relates IdentityMap
382 inline IdentityMap<T> identityMap() {
383 return IdentityMap<T>();
387 ///\brief Convert the \c Value of a map to another type using
388 ///the default conversion.
390 ///This \ref concepts::ReadMap "read only map"
391 ///converts the \c Value of a map to type \c T.
392 ///Its \c Key is inherited from \c M.
393 template <typename M, typename T>
394 class ConvertMap : public MapBase<typename M::Key, T> {
397 typedef MapBase<typename M::Key, T> Parent;
398 typedef typename Parent::Key Key;
399 typedef typename Parent::Value Value;
404 ///\param _m is the underlying map.
405 ConvertMap(const M &_m) : m(_m) {};
408 Value operator[](const Key& k) const {return m[k];}
411 ///Returns a \c ConvertMap class
413 ///This function just returns a \c ConvertMap class.
414 ///\relates ConvertMap
415 template<typename T, typename M>
416 inline ConvertMap<M, T> convertMap(const M &m) {
417 return ConvertMap<M, T>(m);
420 ///Simple wrapping of a map
422 ///This \ref concepts::ReadMap "read only map" returns the simple
423 ///wrapping of the given map. Sometimes the reference maps cannot be
424 ///combined with simple read maps. This map adaptor wraps the given
425 ///map to simple read map.
427 ///\sa SimpleWriteMap
429 /// \todo Revise the misleading name
431 class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
435 typedef MapBase<typename M::Key, typename M::Value> Parent;
436 typedef typename Parent::Key Key;
437 typedef typename Parent::Value Value;
440 SimpleMap(const M &_m) : m(_m) {};
442 Value operator[](Key k) const {return m[k];}
445 ///Returns a \c SimpleMap class
447 ///This function just returns a \c SimpleMap class.
448 ///\relates SimpleMap
450 inline SimpleMap<M> simpleMap(const M &m) {
451 return SimpleMap<M>(m);
454 ///Simple writable wrapping of a map
456 ///This \ref concepts::ReadWriteMap "read-write map" returns the simple
457 ///wrapping of the given map. Sometimes the reference maps cannot be
458 ///combined with simple read-write maps. This map adaptor wraps the
459 ///given map to simple read-write map.
463 /// \todo Revise the misleading name
465 class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
469 typedef MapBase<typename M::Key, typename M::Value> Parent;
470 typedef typename Parent::Key Key;
471 typedef typename Parent::Value Value;
474 SimpleWriteMap(M &_m) : m(_m) {};
476 Value operator[](Key k) const {return m[k];}
478 void set(Key k, const Value& c) { m.set(k, c); }
481 ///Returns a \c SimpleWriteMap class
483 ///This function just returns a \c SimpleWriteMap class.
484 ///\relates SimpleWriteMap
486 inline SimpleWriteMap<M> simpleWriteMap(M &m) {
487 return SimpleWriteMap<M>(m);
492 ///This \ref concepts::ReadMap "read only map" returns the sum of the two
494 ///Its \c Key and \c Value are inherited from \c M1.
495 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
496 template<typename M1, typename M2>
497 class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
502 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
503 typedef typename Parent::Key Key;
504 typedef typename Parent::Value Value;
507 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
509 Value operator[](Key k) const {return m1[k]+m2[k];}
512 ///Returns an \c AddMap class
514 ///This function just returns an \c AddMap class.
515 ///\todo Extend the documentation: how to call these type of functions?
518 template<typename M1, typename M2>
519 inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
520 return AddMap<M1, M2>(m1,m2);
523 ///Shift a map with a constant.
525 ///This \ref concepts::ReadMap "read only map" returns the sum of the
526 ///given map and a constant value.
527 ///Its \c Key and \c Value are inherited from \c M.
531 /// ShiftMap<X> sh(x,v);
535 /// ConstMap<X::Key, X::Value> c_tmp(v);
536 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
540 template<typename M, typename C = typename M::Value>
541 class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
545 typedef MapBase<typename M::Key, typename M::Value> Parent;
546 typedef typename Parent::Key Key;
547 typedef typename Parent::Value Value;
552 ///\param _m is the undelying map.
553 ///\param _v is the shift value.
554 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
556 Value operator[](Key k) const {return m[k] + v;}
559 ///Shift a map with a constant (ReadWrite version).
561 ///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the
562 ///given map and a constant value. It makes also possible to write the map.
563 ///Its \c Key and \c Value are inherited from \c M.
566 template<typename M, typename C = typename M::Value>
567 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
571 typedef MapBase<typename M::Key, typename M::Value> Parent;
572 typedef typename Parent::Key Key;
573 typedef typename Parent::Value Value;
578 ///\param _m is the undelying map.
579 ///\param _v is the shift value.
580 ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
582 Value operator[](Key k) const {return m[k] + v;}
584 void set(Key k, const Value& c) { m.set(k, c - v); }
587 ///Returns a \c ShiftMap class
589 ///This function just returns a \c ShiftMap class.
591 template<typename M, typename C>
592 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
593 return ShiftMap<M, C>(m,v);
596 ///Returns a \c ShiftWriteMap class
598 ///This function just returns a \c ShiftWriteMap class.
599 ///\relates ShiftWriteMap
600 template<typename M, typename C>
601 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
602 return ShiftWriteMap<M, C>(m,v);
605 ///Difference of two maps
607 ///This \ref concepts::ReadMap "read only map" returns the difference
608 ///of the values of the two given maps.
609 ///Its \c Key and \c Value are inherited from \c M1.
610 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
612 /// \todo Revise the misleading name
613 template<typename M1, typename M2>
614 class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
618 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
619 typedef typename Parent::Key Key;
620 typedef typename Parent::Value Value;
623 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
625 Value operator[](Key k) const {return m1[k]-m2[k];}
628 ///Returns a \c SubMap class
630 ///This function just returns a \c SubMap class.
633 template<typename M1, typename M2>
634 inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
635 return SubMap<M1, M2>(m1, m2);
638 ///Product of two maps
640 ///This \ref concepts::ReadMap "read only map" returns the product of the
641 ///values of the two given maps.
642 ///Its \c Key and \c Value are inherited from \c M1.
643 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
644 template<typename M1, typename M2>
645 class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
649 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
650 typedef typename Parent::Key Key;
651 typedef typename Parent::Value Value;
654 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
656 Value operator[](Key k) const {return m1[k]*m2[k];}
659 ///Returns a \c MulMap class
661 ///This function just returns a \c MulMap class.
663 template<typename M1, typename M2>
664 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
665 return MulMap<M1, M2>(m1,m2);
668 ///Scales a map with a constant.
670 ///This \ref concepts::ReadMap "read only map" returns the value of the
671 ///given map multiplied from the left side with a constant value.
672 ///Its \c Key and \c Value are inherited from \c M.
676 /// ScaleMap<X> sc(x,v);
680 /// ConstMap<X::Key, X::Value> c_tmp(v);
681 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
685 template<typename M, typename C = typename M::Value>
686 class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
690 typedef MapBase<typename M::Key, typename M::Value> Parent;
691 typedef typename Parent::Key Key;
692 typedef typename Parent::Value Value;
697 ///\param _m is the undelying map.
698 ///\param _v is the scaling value.
699 ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
701 Value operator[](Key k) const {return v * m[k];}
704 ///Scales a map with a constant (ReadWrite version).
706 ///This \ref concepts::ReadWriteMap "read-write map" returns the value of the
707 ///given map multiplied from the left side with a constant value. It can
708 ///also be used as write map if the \c / operator is defined between
709 ///\c Value and \c C and the given multiplier is not zero.
710 ///Its \c Key and \c Value are inherited from \c M.
713 template<typename M, typename C = typename M::Value>
714 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
718 typedef MapBase<typename M::Key, typename M::Value> Parent;
719 typedef typename Parent::Key Key;
720 typedef typename Parent::Value Value;
725 ///\param _m is the undelying map.
726 ///\param _v is the scaling value.
727 ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
729 Value operator[](Key k) const {return v * m[k];}
731 void set(Key k, const Value& c) { m.set(k, c / v);}
734 ///Returns a \c ScaleMap class
736 ///This function just returns a \c ScaleMap class.
738 template<typename M, typename C>
739 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
740 return ScaleMap<M, C>(m,v);
743 ///Returns a \c ScaleWriteMap class
745 ///This function just returns a \c ScaleWriteMap class.
746 ///\relates ScaleWriteMap
747 template<typename M, typename C>
748 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
749 return ScaleWriteMap<M, C>(m,v);
752 ///Quotient of two maps
754 ///This \ref concepts::ReadMap "read only map" returns the quotient of the
755 ///values of the two given maps.
756 ///Its \c Key and \c Value are inherited from \c M1.
757 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
758 template<typename M1, typename M2>
759 class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
763 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
764 typedef typename Parent::Key Key;
765 typedef typename Parent::Value Value;
768 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
770 Value operator[](Key k) const {return m1[k]/m2[k];}
773 ///Returns a \c DivMap class
775 ///This function just returns a \c DivMap class.
777 template<typename M1, typename M2>
778 inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
779 return DivMap<M1, M2>(m1,m2);
782 ///Composition of two maps
784 ///This \ref concepts::ReadMap "read only map" returns the composition of
786 ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2,
789 /// ComposeMap<M1, M2> cm(m1,m2);
791 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
793 ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1.
794 ///\c M2::Value must be convertible to \c M1::Key.
798 ///\todo Check the requirements.
799 template <typename M1, typename M2>
800 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
804 typedef MapBase<typename M2::Key, typename M1::Value> Parent;
805 typedef typename Parent::Key Key;
806 typedef typename Parent::Value Value;
809 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
814 /// \todo Use the MapTraits once it is ported.
817 //typename MapTraits<M1>::ConstReturnValue
819 operator[](Key k) const {return m1[m2[k]];}
822 ///Returns a \c ComposeMap class
824 ///This function just returns a \c ComposeMap class.
825 ///\relates ComposeMap
826 template <typename M1, typename M2>
827 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
828 return ComposeMap<M1, M2>(m1,m2);
831 ///Combine of two maps using an STL (binary) functor.
833 ///Combine of two maps using an STL (binary) functor.
835 ///This \ref concepts::ReadMap "read only map" takes two maps and a
836 ///binary functor and returns the composition of the two
837 ///given maps unsing the functor.
838 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
839 ///and \c f is of \c F, then for
841 /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
843 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
845 ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
846 ///\c M2::Value and \c M1::Value must be convertible to the corresponding
847 ///input parameter of \c F and the return type of \c F must be convertible
852 ///\todo Check the requirements.
853 template<typename M1, typename M2, typename F,
854 typename V = typename F::result_type>
855 class CombineMap : public MapBase<typename M1::Key, V> {
860 typedef MapBase<typename M1::Key, V> Parent;
861 typedef typename Parent::Key Key;
862 typedef typename Parent::Value Value;
865 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
866 : m1(_m1), m2(_m2), f(_f) {};
868 Value operator[](Key k) const {return f(m1[k],m2[k]);}
871 ///Returns a \c CombineMap class
873 ///This function just returns a \c CombineMap class.
875 ///For example if \c m1 and \c m2 are both \c double valued maps, then
877 ///combineMap(m1,m2,std::plus<double>())
884 ///This function is specialized for adaptable binary function
885 ///classes and C++ functions.
887 ///\relates CombineMap
888 template<typename M1, typename M2, typename F, typename V>
889 inline CombineMap<M1, M2, F, V>
890 combineMap(const M1& m1,const M2& m2, const F& f) {
891 return CombineMap<M1, M2, F, V>(m1,m2,f);
894 template<typename M1, typename M2, typename F>
895 inline CombineMap<M1, M2, F, typename F::result_type>
896 combineMap(const M1& m1, const M2& m2, const F& f) {
897 return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
900 template<typename M1, typename M2, typename K1, typename K2, typename V>
901 inline CombineMap<M1, M2, V (*)(K1, K2), V>
902 combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
903 return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
906 ///Negative value of a map
908 ///This \ref concepts::ReadMap "read only map" returns the negative
909 ///value of the value returned by the given map.
910 ///Its \c Key and \c Value are inherited from \c M.
911 ///The unary \c - operator must be defined for \c Value, of course.
915 class NegMap : public MapBase<typename M::Key, typename M::Value> {
918 typedef MapBase<typename M::Key, typename M::Value> Parent;
919 typedef typename Parent::Key Key;
920 typedef typename Parent::Value Value;
923 NegMap(const M &_m) : m(_m) {};
925 Value operator[](Key k) const {return -m[k];}
928 ///Negative value of a map (ReadWrite version)
930 ///This \ref concepts::ReadWriteMap "read-write map" returns the negative
931 ///value of the value returned by the given map.
932 ///Its \c Key and \c Value are inherited from \c M.
933 ///The unary \c - operator must be defined for \c Value, of course.
937 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
940 typedef MapBase<typename M::Key, typename M::Value> Parent;
941 typedef typename Parent::Key Key;
942 typedef typename Parent::Value Value;
945 NegWriteMap(M &_m) : m(_m) {};
947 Value operator[](Key k) const {return -m[k];}
949 void set(Key k, const Value& v) { m.set(k, -v); }
952 ///Returns a \c NegMap class
954 ///This function just returns a \c NegMap class.
956 template <typename M>
957 inline NegMap<M> negMap(const M &m) {
961 ///Returns a \c NegWriteMap class
963 ///This function just returns a \c NegWriteMap class.
964 ///\relates NegWriteMap
965 template <typename M>
966 inline NegWriteMap<M> negMap(M &m) {
967 return NegWriteMap<M>(m);
970 ///Absolute value of a map
972 ///This \ref concepts::ReadMap "read only map" returns the absolute value
973 ///of the value returned by the given map.
974 ///Its \c Key and \c Value are inherited from \c M.
975 ///\c Value must be comparable to \c 0 and the unary \c -
976 ///operator must be defined for it, of course.
978 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
981 typedef MapBase<typename M::Key, typename M::Value> Parent;
982 typedef typename Parent::Key Key;
983 typedef typename Parent::Value Value;
986 AbsMap(const M &_m) : m(_m) {};
988 Value operator[](Key k) const {
990 return tmp >= 0 ? tmp : -tmp;
995 ///Returns an \c AbsMap class
997 ///This function just returns an \c AbsMap class.
1000 inline AbsMap<M> absMap(const M &m) {
1001 return AbsMap<M>(m);
1004 ///Converts an STL style functor to a map
1006 ///This \ref concepts::ReadMap "read only map" returns the value
1007 ///of a given functor.
1009 ///Template parameters \c K and \c V will become its
1010 ///\c Key and \c Value.
1011 ///In most cases they have to be given explicitly because a
1012 ///functor typically does not provide \c argument_type and
1013 ///\c result_type typedefs.
1015 ///Parameter \c F is the type of the used functor.
1018 template<typename F,
1019 typename K = typename F::argument_type,
1020 typename V = typename F::result_type>
1021 class FunctorMap : public MapBase<K, V> {
1024 typedef MapBase<K, V> Parent;
1025 typedef typename Parent::Key Key;
1026 typedef typename Parent::Value Value;
1029 FunctorMap(const F &_f = F()) : f(_f) {}
1031 Value operator[](Key k) const { return f(k);}
1034 ///Returns a \c FunctorMap class
1036 ///This function just returns a \c FunctorMap class.
1038 ///This function is specialized for adaptable binary function
1039 ///classes and C++ functions.
1041 ///\relates FunctorMap
1042 template<typename K, typename V, typename F> inline
1043 FunctorMap<F, K, V> functorMap(const F &f) {
1044 return FunctorMap<F, K, V>(f);
1047 template <typename F> inline
1048 FunctorMap<F, typename F::argument_type, typename F::result_type>
1049 functorMap(const F &f) {
1050 return FunctorMap<F, typename F::argument_type,
1051 typename F::result_type>(f);
1054 template <typename K, typename V> inline
1055 FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
1056 return FunctorMap<V (*)(K), K, V>(f);
1060 ///Converts a map to an STL style (unary) functor
1062 ///This class Converts a map to an STL style (unary) functor.
1063 ///That is it provides an <tt>operator()</tt> to read its values.
1065 ///For the sake of convenience it also works as
1066 ///a ususal \ref concepts::ReadMap "readable map",
1067 ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
1070 template <typename M>
1071 class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
1074 typedef MapBase<typename M::Key, typename M::Value> Parent;
1075 typedef typename Parent::Key Key;
1076 typedef typename Parent::Value Value;
1078 typedef typename M::Key argument_type;
1079 typedef typename M::Value result_type;
1082 MapFunctor(const M &_m) : m(_m) {};
1084 Value operator()(Key k) const {return m[k];}
1086 Value operator[](Key k) const {return m[k];}
1089 ///Returns a \c MapFunctor class
1091 ///This function just returns a \c MapFunctor class.
1092 ///\relates MapFunctor
1093 template<typename M>
1094 inline MapFunctor<M> mapFunctor(const M &m) {
1095 return MapFunctor<M>(m);
1098 ///Just readable version of \ref ForkWriteMap
1100 ///This map has two \ref concepts::ReadMap "readable map"
1101 ///parameters and each read request will be passed just to the
1102 ///first map. This class is the just readable map type of \c ForkWriteMap.
1104 ///The \c Key and \c Value are inherited from \c M1.
1105 ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
1109 /// \todo Why is it needed?
1110 template<typename M1, typename M2>
1111 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
1115 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1116 typedef typename Parent::Key Key;
1117 typedef typename Parent::Value Value;
1120 ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
1122 Value operator[](Key k) const {return m1[k];}
1126 ///Applies all map setting operations to two maps
1128 ///This map has two \ref concepts::WriteMap "writable map"
1129 ///parameters and each write request will be passed to both of them.
1130 ///If \c M1 is also \ref concepts::ReadMap "readable",
1131 ///then the read operations will return the
1132 ///corresponding values of \c M1.
1134 ///The \c Key and \c Value are inherited from \c M1.
1135 ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
1138 template<typename M1, typename M2>
1139 class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
1143 typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1144 typedef typename Parent::Key Key;
1145 typedef typename Parent::Value Value;
1148 ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
1150 Value operator[](Key k) const {return m1[k];}
1152 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
1155 ///Returns a \c ForkMap class
1157 ///This function just returns a \c ForkMap class.
1159 template <typename M1, typename M2>
1160 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
1161 return ForkMap<M1, M2>(m1,m2);
1164 ///Returns a \c ForkWriteMap class
1166 ///This function just returns a \c ForkWriteMap class.
1167 ///\relates ForkWriteMap
1168 template <typename M1, typename M2>
1169 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
1170 return ForkWriteMap<M1, M2>(m1,m2);
1175 /* ************* BOOL MAPS ******************* */
1177 ///Logical 'not' of a map
1179 ///This bool \ref concepts::ReadMap "read only map" returns the
1180 ///logical negation of the value returned by the given map.
1181 ///Its \c Key is inherited from \c M, its \c Value is \c bool.
1184 template <typename M>
1185 class NotMap : public MapBase<typename M::Key, bool> {
1188 typedef MapBase<typename M::Key, bool> Parent;
1189 typedef typename Parent::Key Key;
1190 typedef typename Parent::Value Value;
1193 NotMap(const M &_m) : m(_m) {};
1195 Value operator[](Key k) const {return !m[k];}
1198 ///Logical 'not' of a map (ReadWrie version)
1200 ///This bool \ref concepts::ReadWriteMap "read-write map" returns the
1201 ///logical negation of the value returned by the given map. When it is set,
1202 ///the opposite value is set to the original map.
1203 ///Its \c Key is inherited from \c M, its \c Value is \c bool.
1206 template <typename M>
1207 class NotWriteMap : public MapBase<typename M::Key, bool> {
1210 typedef MapBase<typename M::Key, bool> Parent;
1211 typedef typename Parent::Key Key;
1212 typedef typename Parent::Value Value;
1215 NotWriteMap(M &_m) : m(_m) {};
1217 Value operator[](Key k) const {return !m[k];}
1219 void set(Key k, bool v) { m.set(k, !v); }
1222 ///Returns a \c NotMap class
1224 ///This function just returns a \c NotMap class.
1226 template <typename M>
1227 inline NotMap<M> notMap(const M &m) {
1228 return NotMap<M>(m);
1231 ///Returns a \c NotWriteMap class
1233 ///This function just returns a \c NotWriteMap class.
1234 ///\relates NotWriteMap
1235 template <typename M>
1236 inline NotWriteMap<M> notMap(M &m) {
1237 return NotWriteMap<M>(m);
1240 namespace _maps_bits {
1242 template <typename Value>
1244 typedef Value argument_type;
1245 typedef Value result_type;
1246 Value operator()(const Value& val) const {
1251 template <typename _Iterator, typename Enable = void>
1252 struct IteratorTraits {
1253 typedef typename std::iterator_traits<_Iterator>::value_type Value;
1256 template <typename _Iterator>
1257 struct IteratorTraits<_Iterator,
1258 typename exists<typename _Iterator::container_type>::type>
1260 typedef typename _Iterator::container_type::value_type Value;
1266 /// \brief Writable bool map for logging each \c true assigned element
1268 /// A \ref concepts::ReadWriteMap "read-write" bool map for logging
1269 /// each \c true assigned element, i.e it copies all the keys set
1270 /// to \c true to the given iterator.
1272 /// \note The container of the iterator should contain space
1273 /// for each element.
1275 /// The following example shows how you can write the edges found by
1276 /// the \ref Prim algorithm directly to the standard output.
1278 /// typedef IdMap<Graph, Edge> EdgeIdMap;
1279 /// EdgeIdMap edgeId(graph);
1281 /// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;
1282 /// EdgeIdFunctor edgeIdFunctor(edgeId);
1284 /// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor>
1285 /// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor);
1287 /// prim(graph, cost, writerMap);
1290 ///\sa BackInserterBoolMap
1291 ///\sa FrontInserterBoolMap
1292 ///\sa InserterBoolMap
1294 ///\todo Revise the name of this class and the related ones.
1295 template <typename _Iterator,
1297 _maps_bits::Identity<typename _maps_bits::
1298 IteratorTraits<_Iterator>::Value> >
1299 class StoreBoolMap {
1301 typedef _Iterator Iterator;
1303 typedef typename _Functor::argument_type Key;
1306 typedef _Functor Functor;
1309 StoreBoolMap(Iterator it, const Functor& functor = Functor())
1310 : _begin(it), _end(it), _functor(functor) {}
1312 /// Gives back the given iterator set for the first key
1313 Iterator begin() const {
1317 /// Gives back the the 'after the last' iterator
1318 Iterator end() const {
1322 /// The \c set function of the map
1323 void set(const Key& key, Value value) const {
1325 *_end++ = _functor(key);
1331 mutable Iterator _end;
1335 /// \brief Writable bool map for logging each \c true assigned element in
1336 /// a back insertable container.
1338 /// Writable bool map for logging each \c true assigned element by pushing
1339 /// them into a back insertable container.
1340 /// It can be used to retrieve the items into a standard
1341 /// container. The next example shows how you can store the
1342 /// edges found by the Prim algorithm in a vector.
1345 /// vector<Edge> span_tree_edges;
1346 /// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges);
1347 /// prim(graph, cost, inserter_map);
1351 ///\sa FrontInserterBoolMap
1352 ///\sa InserterBoolMap
1353 template <typename Container,
1355 _maps_bits::Identity<typename Container::value_type> >
1356 class BackInserterBoolMap {
1358 typedef typename Functor::argument_type Key;
1362 BackInserterBoolMap(Container& _container,
1363 const Functor& _functor = Functor())
1364 : container(_container), functor(_functor) {}
1366 /// The \c set function of the map
1367 void set(const Key& key, Value value) {
1369 container.push_back(functor(key));
1374 Container& container;
1378 /// \brief Writable bool map for logging each \c true assigned element in
1379 /// a front insertable container.
1381 /// Writable bool map for logging each \c true assigned element by pushing
1382 /// them into a front insertable container.
1383 /// It can be used to retrieve the items into a standard
1384 /// container. For example see \ref BackInserterBoolMap.
1386 ///\sa BackInserterBoolMap
1387 ///\sa InserterBoolMap
1388 template <typename Container,
1390 _maps_bits::Identity<typename Container::value_type> >
1391 class FrontInserterBoolMap {
1393 typedef typename Functor::argument_type Key;
1397 FrontInserterBoolMap(Container& _container,
1398 const Functor& _functor = Functor())
1399 : container(_container), functor(_functor) {}
1401 /// The \c set function of the map
1402 void set(const Key& key, Value value) {
1404 container.push_front(functor(key));
1409 Container& container;
1413 /// \brief Writable bool map for storing each \c true assigned element in
1414 /// an insertable container.
1416 /// Writable bool map for storing each \c true assigned element in an
1417 /// insertable container. It will insert all the keys set to \c true into
1420 /// For example, if you want to store the cut arcs of the strongly
1421 /// connected components in a set you can use the next code:
1424 /// set<Arc> cut_arcs;
1425 /// InserterBoolMap<set<Arc> > inserter_map(cut_arcs);
1426 /// stronglyConnectedCutArcs(digraph, cost, inserter_map);
1429 ///\sa BackInserterBoolMap
1430 ///\sa FrontInserterBoolMap
1431 template <typename Container,
1433 _maps_bits::Identity<typename Container::value_type> >
1434 class InserterBoolMap {
1436 typedef typename Container::value_type Key;
1439 /// Constructor with specified iterator
1441 /// Constructor with specified iterator.
1442 /// \param _container The container for storing the elements.
1443 /// \param _it The elements will be inserted before this iterator.
1444 /// \param _functor The functor that is used when an element is stored.
1445 InserterBoolMap(Container& _container, typename Container::iterator _it,
1446 const Functor& _functor = Functor())
1447 : container(_container), it(_it), functor(_functor) {}
1451 /// Constructor without specified iterator.
1452 /// The elements will be inserted before <tt>_container.end()</tt>.
1453 /// \param _container The container for storing the elements.
1454 /// \param _functor The functor that is used when an element is stored.
1455 InserterBoolMap(Container& _container, const Functor& _functor = Functor())
1456 : container(_container), it(_container.end()), functor(_functor) {}
1458 /// The \c set function of the map
1459 void set(const Key& key, Value value) {
1461 it = container.insert(it, functor(key));
1467 Container& container;
1468 typename Container::iterator it;
1472 /// \brief Writable bool map for filling each \c true assigned element with a
1475 /// Writable bool map for filling each \c true assigned element with a
1476 /// given value. The value can set the container.
1478 /// The following code finds the connected components of a graph
1479 /// and stores it in the \c comp map:
1481 /// typedef Graph::NodeMap<int> ComponentMap;
1482 /// ComponentMap comp(graph);
1483 /// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap;
1484 /// ComponentFillerMap filler(comp, 0);
1486 /// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph);
1487 /// dfs.processedMap(filler);
1489 /// for (NodeIt it(graph); it != INVALID; ++it) {
1490 /// if (!dfs.reached(it)) {
1491 /// dfs.addSource(it);
1493 /// ++filler.fillValue();
1497 template <typename Map>
1500 typedef typename Map::Key Key;
1504 FillBoolMap(Map& _map, const typename Map::Value& _fill)
1505 : map(_map), fill(_fill) {}
1508 FillBoolMap(Map& _map)
1509 : map(_map), fill() {}
1511 /// Gives back the current fill value
1512 const typename Map::Value& fillValue() const {
1516 /// Gives back the current fill value
1517 typename Map::Value& fillValue() {
1521 /// Sets the current fill value
1522 void fillValue(const typename Map::Value& _fill) {
1526 /// The \c set function of the map
1527 void set(const Key& key, Value value) {
1535 typename Map::Value fill;
1539 /// \brief Writable bool map for storing the sequence number of
1540 /// \c true assignments.
1542 /// Writable bool map that stores for each \c true assigned elements
1543 /// the sequence number of this setting.
1544 /// It makes it easy to calculate the leaving
1545 /// order of the nodes in the \c Dfs algorithm.
1548 /// typedef Digraph::NodeMap<int> OrderMap;
1549 /// OrderMap order(digraph);
1550 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1551 /// OrderSetterMap setter(order);
1552 /// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
1553 /// dfs.processedMap(setter);
1555 /// for (NodeIt it(digraph); it != INVALID; ++it) {
1556 /// if (!dfs.reached(it)) {
1557 /// dfs.addSource(it);
1563 /// The storing of the discovering order is more difficult because the
1564 /// ReachedMap should be readable in the dfs algorithm but the setting
1565 /// order map is not readable. Thus we must use the fork map:
1568 /// typedef Digraph::NodeMap<int> OrderMap;
1569 /// OrderMap order(digraph);
1570 /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1571 /// OrderSetterMap setter(order);
1572 /// typedef Digraph::NodeMap<bool> StoreMap;
1573 /// StoreMap store(digraph);
1575 /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1576 /// ReachedMap reached(store, setter);
1578 /// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
1579 /// dfs.reachedMap(reached);
1581 /// for (NodeIt it(digraph); it != INVALID; ++it) {
1582 /// if (!dfs.reached(it)) {
1583 /// dfs.addSource(it);
1588 template <typename Map>
1589 class SettingOrderBoolMap {
1591 typedef typename Map::Key Key;
1595 SettingOrderBoolMap(Map& _map)
1596 : map(_map), counter(0) {}
1598 /// Number of set operations.
1603 /// The \c set function of the map
1604 void set(const Key& key, Value value) {
1606 map.set(key, counter++);
1618 #endif // LEMON_MAPS_H