2 * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
24 ///\brief Miscellaneous property maps
26 ///\todo This file has the same name as the concept file in concept/,
27 /// and this is not easily detectable in docs...
36 /// Base class of maps.
38 /// Base class of maps.
39 /// It provides the necessary <tt>typedef</tt>s required by the map concept.
40 template<typename K, typename T>
50 /// Null map. (a.k.a. DoNothingMap)
52 /// If you have to provide a map only for its type definitions,
53 /// or if you have to provide a writable map, but
54 /// data written to it will sent to <tt>/dev/null</tt>...
55 template<typename K, typename T>
56 class NullMap : public MapBase<K,T>
60 /// Gives back a default constructed element.
61 T operator[](const K&) const { return T(); }
62 /// Absorbs the value.
63 void set(const K&, const T&) {}
69 /// This is a readable map which assigns a specified value to each key.
70 /// In other aspects it is equivalent to the \ref NullMap.
71 /// \todo set could be used to set the value.
72 template<typename K, typename T>
73 class ConstMap : public MapBase<K,T>
78 /// Default constructor
80 /// The value of the map will be uninitialized.
81 /// (More exactly it will be default constructed.)
85 /// \param _v The initial value of the map.
87 ConstMap(const T &_v) : v(_v) {}
89 T operator[](const K&) const { return v; }
90 void set(const K&, const T&) {}
94 typedef ConstMap<K,T1> other;
98 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
101 ///Returns a \ref ConstMap class
103 ///This function just returns a \ref ConstMap class.
105 template<class V,class K>
106 inline ConstMap<V,K> constMap(const K &k)
108 return ConstMap<V,K>(k);
113 template<typename T, T v>
116 template<typename K, typename V, V v>
117 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
121 V operator[](const K&) const { return v; }
122 void set(const K&, const V&) { }
125 /// \c std::map wrapper
127 /// This is essentially a wrapper for \c std::map. With addition that
128 /// you can specify a default value different from \c Value() .
130 /// \todo Provide allocator parameter...
131 template <typename K, typename T, typename Compare = std::less<K> >
132 class StdMap : public std::map<K,T,Compare> {
133 typedef std::map<K,T,Compare> parent;
135 typedef typename parent::value_type PairType;
140 typedef T& Reference;
141 typedef const T& ConstReference;
145 /// Constructor with specified default value
146 StdMap(const T& _v) : v(_v) {}
148 /// \brief Constructs the map from an appropriate std::map.
150 /// \warning Inefficient: copies the content of \c m !
151 StdMap(const parent &m) : parent(m) {}
152 /// \brief Constructs the map from an appropriate std::map, and explicitly
153 /// specifies a default value.
155 /// \warning Inefficient: copies the content of \c m !
156 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
158 template<typename T1, typename Comp1>
159 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
163 Reference operator[](const Key &k) {
164 return insert(PairType(k,v)).first -> second;
166 ConstReference operator[](const Key &k) const {
167 typename parent::iterator i = lower_bound(k);
168 if (i == parent::end() || parent::key_comp()(k, (*i).first))
172 void set(const Key &k, const T &t) {
173 parent::operator[](k) = t;
176 /// Changes the default value of the map.
177 /// \return Returns the previous default value.
179 /// \warning The value of some keys (which has already been queried, but
180 /// the value has been unchanged from the default) may change!
181 T setDefault(const T &_v) { T old=v; v=_v; return old; }
183 template<typename T1>
185 typedef StdMap<Key,T1,Compare> other;
189 ///Convert the \c Value of a maps to another type.
191 ///This \ref concept::ReadMap "read only map"
192 ///converts the \c Value of a maps to type \c T.
193 ///Its \c Value is inherited from \c M.
197 /// ConvertMap<X> sh(x,v);
199 ///it is equivalent with
201 /// ConstMap<X::Key, X::Value> c_tmp(v);
202 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
204 ///\bug wrong documentation
205 template<class M, class T>
210 typedef typename M::Key Key;
216 ///\param _m is the undelying map
217 ///\param _v is the convert value
218 ConvertMap(const M &_m) : m(_m) {};
219 Value operator[](Key k) const {return m[k];}
222 ///Returns an \ref ConvertMap class
224 ///This function just returns an \ref ConvertMap class.
225 ///\relates ConvertMap
226 ///\todo The order of the template parameters are changed.
227 template<class T, class M>
228 inline ConvertMap<M,T> convertMap(const M &m)
230 return ConvertMap<M,T>(m);
235 ///This \ref concept::ReadMap "read only map" returns the sum of the two
236 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
237 ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
239 template<class M1,class M2>
245 typedef typename M1::Key Key;
246 typedef typename M1::Value Value;
252 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
253 Value operator[](Key k) const {return m1[k]+m2[k];}
256 ///Returns an \ref AddMap class
258 ///This function just returns an \ref AddMap class.
259 ///\todo How to call these type of functions?
262 ///\todo Wrong scope in Doxygen when \c \\relates is used
263 template<class M1,class M2>
264 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
266 return AddMap<M1,M2>(m1,m2);
269 ///Shift a maps with a constant.
271 ///This \ref concept::ReadMap "read only map" returns the sum of the
272 ///given map and a constant value.
273 ///Its \c Key and \c Value is inherited from \c M.
277 /// ShiftMap<X> sh(x,v);
279 ///it is equivalent with
281 /// ConstMap<X::Key, X::Value> c_tmp(v);
282 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
290 typedef typename M::Key Key;
291 typedef typename M::Value Value;
296 ///\param _m is the undelying map
297 ///\param _v is the shift value
298 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
299 Value operator[](Key k) const {return m[k]+v;}
302 ///Returns an \ref ShiftMap class
304 ///This function just returns an \ref ShiftMap class.
306 ///\todo A better name is required.
308 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
310 return ShiftMap<M>(m,v);
313 ///Difference of two maps
315 ///This \ref concept::ReadMap "read only map" returns the difference
316 ///of the values returned by the two
317 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
318 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
320 template<class M1,class M2>
326 typedef typename M1::Key Key;
327 typedef typename M1::Value Value;
333 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
334 Value operator[](Key k) const {return m1[k]-m2[k];}
337 ///Returns a \ref SubMap class
339 ///This function just returns a \ref SubMap class.
342 template<class M1,class M2>
343 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
345 return SubMap<M1,M2>(m1,m2);
348 ///Product of two maps
350 ///This \ref concept::ReadMap "read only map" returns the product of the
351 ///values returned by the two
353 ///maps. Its \c Key and \c Value will be inherited from \c M1.
354 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
356 template<class M1,class M2>
362 typedef typename M1::Key Key;
363 typedef typename M1::Value Value;
369 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
370 Value operator[](Key k) const {return m1[k]*m2[k];}
373 ///Returns a \ref MulMap class
375 ///This function just returns a \ref MulMap class.
377 template<class M1,class M2>
378 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
380 return MulMap<M1,M2>(m1,m2);
383 ///Scale a maps with a constant.
385 ///This \ref concept::ReadMap "read only map" returns the value of the
386 ///given map multipied with a constant value.
387 ///Its \c Key and \c Value is inherited from \c M.
391 /// ScaleMap<X> sc(x,v);
393 ///it is equivalent with
395 /// ConstMap<X::Key, X::Value> c_tmp(v);
396 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
404 typedef typename M::Key Key;
405 typedef typename M::Value Value;
410 ///\param _m is the undelying map
411 ///\param _v is the scaling value
412 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
413 Value operator[](Key k) const {return m[k]*v;}
416 ///Returns an \ref ScaleMap class
418 ///This function just returns an \ref ScaleMap class.
420 ///\todo A better name is required.
422 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
424 return ScaleMap<M>(m,v);
427 ///Quotient of two maps
429 ///This \ref concept::ReadMap "read only map" returns the quotient of the
430 ///values returned by the two
431 ///given maps. Its \c Key and \c Value will be inherited from \c M1.
432 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
434 template<class M1,class M2>
440 typedef typename M1::Key Key;
441 typedef typename M1::Value Value;
447 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
448 Value operator[](Key k) const {return m1[k]/m2[k];}
451 ///Returns a \ref DivMap class
453 ///This function just returns a \ref DivMap class.
455 template<class M1,class M2>
456 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
458 return DivMap<M1,M2>(m1,m2);
461 ///Composition of two maps
463 ///This \ref concept::ReadMap "read only map" returns the composition of
465 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
469 /// ComposeMap<M1,M2> cm(m1,m2);
471 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
473 ///Its \c Key is inherited from \c M2 and its \c Value is from
475 ///The \c M2::Value must be convertible to \c M1::Key.
476 ///\todo Check the requirements.
478 template<class M1,class M2>
484 typedef typename M2::Key Key;
485 typedef typename M1::Value Value;
491 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
492 Value operator[](Key k) const {return m1[m2[k]];}
495 ///Returns a \ref ComposeMap class
497 ///This function just returns a \ref ComposeMap class.
498 ///\relates ComposeMap
499 template<class M1,class M2>
500 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
502 return ComposeMap<M1,M2>(m1,m2);
505 ///Negative value of a map
507 ///This \ref concept::ReadMap "read only map" returns the negative
509 ///value returned by the
510 ///given map. Its \c Key and \c Value will be inherited from \c M.
511 ///The unary \c - operator must be defined for \c Value, of course.
518 typedef typename M::Key Key;
519 typedef typename M::Value Value;
525 NegMap(const M &_m) : m(_m) {};
526 Value operator[](Key k) const {return -m[k];}
529 ///Returns a \ref NegMap class
531 ///This function just returns a \ref NegMap class.
534 inline NegMap<M> negMap(const M &m)
540 ///Absolute value of a map
542 ///This \ref concept::ReadMap "read only map" returns the absolute value
544 ///value returned by the
545 ///given map. Its \c Key and \c Value will be inherited
546 ///from <tt>M</tt>. <tt>Value</tt>
547 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
548 ///operator must be defined for it, of course.
550 ///\bug We need a unified way to handle the situation below:
552 /// struct _UnConvertible {};
553 /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
554 /// template<> inline int t_abs<>(int n) {return abs(n);}
555 /// template<> inline long int t_abs<>(long int n) {return labs(n);}
556 /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
557 /// template<> inline float t_abs<>(float n) {return fabsf(n);}
558 /// template<> inline double t_abs<>(double n) {return fabs(n);}
559 /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
568 typedef typename M::Key Key;
569 typedef typename M::Value Value;
575 AbsMap(const M &_m) : m(_m) {};
576 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
579 ///Returns a \ref AbsMap class
581 ///This function just returns a \ref AbsMap class.
584 inline AbsMap<M> absMap(const M &m)
589 ///Converts an STL style functor to a a map
591 ///This \ref concept::ReadMap "read only map" returns the value
595 ///Template parameters \c K and \c V will become its
596 ///\c Key and \c Value. They must be given explicitely
597 ///because a functor does not provide such typedefs.
599 ///Parameter \c F is the type of the used functor.
602 template<class K,class V,class F>
614 FunctorMap(const F &_f) : f(_f) {};
615 Value operator[](Key k) const {return f(k);}
618 ///Returns a \ref FunctorMap class
620 ///This function just returns a \ref FunctorMap class.
622 ///The third template parameter isn't necessary to be given.
623 ///\relates FunctorMap
624 template<class K,class V, class F>
625 inline FunctorMap<K,V,F> functorMap(const F &f)
627 return FunctorMap<K,V,F>(f);
630 ///Converts a map to an STL style functor
632 ///This class Converts a map to an STL style functor.
633 ///that is it provides an <tt>operator()</tt> to read its values.
635 ///For the sake of convenience it also works as a ususal map, i.e
636 ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
643 typedef typename M::Key Key;
644 typedef typename M::Value Value;
650 MapFunctor(const M &_m) : m(_m) {};
651 ///Returns a value of the map
655 Value operator()(Key k) const {return m[k];}
658 Value operator[](Key k) const {return m[k];}
661 ///Returns a \ref MapFunctor class
663 ///This function just returns a \ref MapFunctor class.
664 ///\relates MapFunctor
666 inline MapFunctor<M> mapFunctor(const M &m)
668 return MapFunctor<M>(m);
677 #endif // LEMON_MAPS_H