1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/maps.h Mon May 23 04:48:14 2005 +0000
1.3 @@ -0,0 +1,841 @@
1.4 +/* -*- C++ -*-
1.5 + * lemon/maps.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_MAPS_H
1.21 +#define LEMON_MAPS_H
1.22 +
1.23 +#include <lemon/graph_utils.h>
1.24 +#include <lemon/utility.h>
1.25 +
1.26 +
1.27 +///\file
1.28 +///\ingroup maps
1.29 +///\brief Miscellaneous property maps
1.30 +///
1.31 +///\todo This file has the same name as the concept file in concept/,
1.32 +/// and this is not easily detectable in docs...
1.33 +
1.34 +#include <map>
1.35 +
1.36 +namespace lemon {
1.37 +
1.38 + /// \addtogroup maps
1.39 + /// @{
1.40 +
1.41 + /// Base class of maps.
1.42 +
1.43 + /// Base class of maps.
1.44 + /// It provides the necessary <tt>typedef</tt>s required by the map concept.
1.45 + template<typename K, typename T>
1.46 + class MapBase
1.47 + {
1.48 + public:
1.49 + ///\e
1.50 + typedef K Key;
1.51 + ///\e
1.52 + typedef T Value;
1.53 + };
1.54 +
1.55 + /// Null map. (a.k.a. DoNothingMap)
1.56 +
1.57 + /// If you have to provide a map only for its type definitions,
1.58 + /// or if you have to provide a writable map, but
1.59 + /// data written to it will sent to <tt>/dev/null</tt>...
1.60 + template<typename K, typename T>
1.61 + class NullMap : public MapBase<K,T>
1.62 + {
1.63 + public:
1.64 +
1.65 + typedef True NeedCopy;
1.66 +
1.67 + /// Gives back a default constructed element.
1.68 + T operator[](const K&) const { return T(); }
1.69 + /// Absorbs the value.
1.70 + void set(const K&, const T&) {}
1.71 + };
1.72 +
1.73 + template <typename K, typename V>
1.74 + NullMap<K, V> nullMap() {
1.75 + return NullMap<K, V>();
1.76 + }
1.77 +
1.78 +
1.79 + /// Constant map.
1.80 +
1.81 + /// This is a readable map which assigns a specified value to each key.
1.82 + /// In other aspects it is equivalent to the \ref NullMap.
1.83 + /// \todo set could be used to set the value.
1.84 + template<typename K, typename T>
1.85 + class ConstMap : public MapBase<K,T>
1.86 + {
1.87 + T v;
1.88 + public:
1.89 +
1.90 + typedef True NeedCopy;
1.91 +
1.92 + /// Default constructor
1.93 +
1.94 + /// The value of the map will be uninitialized.
1.95 + /// (More exactly it will be default constructed.)
1.96 + ConstMap() {}
1.97 + ///\e
1.98 +
1.99 + /// \param _v The initial value of the map.
1.100 + ///
1.101 + ConstMap(const T &_v) : v(_v) {}
1.102 +
1.103 + T operator[](const K&) const { return v; }
1.104 + void set(const K&, const T&) {}
1.105 +
1.106 + template<typename T1>
1.107 + struct rebind {
1.108 + typedef ConstMap<K,T1> other;
1.109 + };
1.110 +
1.111 + template<typename T1>
1.112 + ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
1.113 + };
1.114 +
1.115 + ///Returns a \ref ConstMap class
1.116 +
1.117 + ///This function just returns a \ref ConstMap class.
1.118 + ///\relates ConstMap
1.119 + template<class V,class K>
1.120 + inline ConstMap<V,K> constMap(const K &k)
1.121 + {
1.122 + return ConstMap<V,K>(k);
1.123 + }
1.124 +
1.125 +
1.126 + //to document later
1.127 + template<typename T, T v>
1.128 + struct Const { };
1.129 + //to document later
1.130 + template<typename K, typename V, V v>
1.131 + class ConstMap<K, Const<V, v> > : public MapBase<K, V>
1.132 + {
1.133 + public:
1.134 + ConstMap() { }
1.135 + V operator[](const K&) const { return v; }
1.136 + void set(const K&, const V&) { }
1.137 + };
1.138 +
1.139 + /// \c std::map wrapper
1.140 +
1.141 + /// This is essentially a wrapper for \c std::map. With addition that
1.142 + /// you can specify a default value different from \c Value() .
1.143 + ///
1.144 + /// \todo Provide allocator parameter...
1.145 + template <typename K, typename T, typename Compare = std::less<K> >
1.146 + class StdMap : public std::map<K,T,Compare> {
1.147 + typedef std::map<K,T,Compare> parent;
1.148 + T v;
1.149 + typedef typename parent::value_type PairType;
1.150 +
1.151 + public:
1.152 + typedef K Key;
1.153 + typedef T Value;
1.154 + typedef T& Reference;
1.155 + typedef const T& ConstReference;
1.156 +
1.157 +
1.158 + StdMap() : v() {}
1.159 + /// Constructor with specified default value
1.160 + StdMap(const T& _v) : v(_v) {}
1.161 +
1.162 + /// \brief Constructs the map from an appropriate std::map.
1.163 + ///
1.164 + /// \warning Inefficient: copies the content of \c m !
1.165 + StdMap(const parent &m) : parent(m) {}
1.166 + /// \brief Constructs the map from an appropriate std::map, and explicitly
1.167 + /// specifies a default value.
1.168 + ///
1.169 + /// \warning Inefficient: copies the content of \c m !
1.170 + StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
1.171 +
1.172 + template<typename T1, typename Comp1>
1.173 + StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
1.174 + //FIXME;
1.175 + }
1.176 +
1.177 + Reference operator[](const Key &k) {
1.178 + return insert(PairType(k,v)).first -> second;
1.179 + }
1.180 + ConstReference operator[](const Key &k) const {
1.181 + typename parent::iterator i = lower_bound(k);
1.182 + if (i == parent::end() || parent::key_comp()(k, (*i).first))
1.183 + return v;
1.184 + return (*i).second;
1.185 + }
1.186 + void set(const Key &k, const T &t) {
1.187 + parent::operator[](k) = t;
1.188 + }
1.189 +
1.190 + /// Changes the default value of the map.
1.191 + /// \return Returns the previous default value.
1.192 + ///
1.193 + /// \warning The value of some keys (which has already been queried, but
1.194 + /// the value has been unchanged from the default) may change!
1.195 + T setDefault(const T &_v) { T old=v; v=_v; return old; }
1.196 +
1.197 + template<typename T1>
1.198 + struct rebind {
1.199 + typedef StdMap<Key,T1,Compare> other;
1.200 + };
1.201 + };
1.202 +
1.203 + /// @}
1.204 +
1.205 + /// \addtogroup map_adaptors
1.206 + /// @{
1.207 +
1.208 +
1.209 + ///Convert the \c Value of a maps to another type.
1.210 +
1.211 + ///This \ref concept::ReadMap "read only map"
1.212 + ///converts the \c Value of a maps to type \c T.
1.213 + ///Its \c Value is inherited from \c M.
1.214 + ///
1.215 + ///Actually,
1.216 + ///\code
1.217 + /// ConvertMap<X> sh(x,v);
1.218 + ///\endcode
1.219 + ///it is equivalent with
1.220 + ///\code
1.221 + /// ConstMap<X::Key, X::Value> c_tmp(v);
1.222 + /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
1.223 + ///\endcode
1.224 + ///\bug wrong documentation
1.225 + template<class M, class T>
1.226 + class ConvertMap {
1.227 + typename SmartConstReference<M>::Type m;
1.228 + public:
1.229 +
1.230 + typedef True NeedCopy;
1.231 +
1.232 + typedef typename M::Key Key;
1.233 + typedef T Value;
1.234 +
1.235 + ///Constructor
1.236 +
1.237 + ///Constructor
1.238 + ///\param _m is the undelying map
1.239 + ///\param _v is the convert value
1.240 + ConvertMap(const M &_m) : m(_m) {};
1.241 +
1.242 + /// \brief The subscript operator.
1.243 + ///
1.244 + /// The subscript operator.
1.245 + /// \param edge The edge
1.246 + /// \return The target of the edge
1.247 + Value operator[](Key k) const {return m[k];}
1.248 + };
1.249 +
1.250 + ///Returns an \ref ConvertMap class
1.251 +
1.252 + ///This function just returns an \ref ConvertMap class.
1.253 + ///\relates ConvertMap
1.254 + ///\todo The order of the template parameters are changed.
1.255 + template<class T, class M>
1.256 + inline ConvertMap<M,T> convertMap(const M &m)
1.257 + {
1.258 + return ConvertMap<M,T>(m);
1.259 + }
1.260 +
1.261 + ///Sum of two maps
1.262 +
1.263 + ///This \ref concept::ReadMap "read only map" returns the sum of the two
1.264 + ///given maps. Its \c Key and \c Value will be inherited from \c M1.
1.265 + ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
1.266 +
1.267 + template<class M1,class M2>
1.268 + class AddMap
1.269 + {
1.270 + typename SmartConstReference<M1>::Type m1;
1.271 + typename SmartConstReference<M2>::Type m2;
1.272 +
1.273 + public:
1.274 +
1.275 + typedef True NeedCopy;
1.276 +
1.277 + typedef typename M1::Key Key;
1.278 + typedef typename M1::Value Value;
1.279 +
1.280 + ///Constructor
1.281 +
1.282 + ///\e
1.283 + ///
1.284 + AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
1.285 + Value operator[](Key k) const {return m1[k]+m2[k];}
1.286 + };
1.287 +
1.288 + ///Returns an \ref AddMap class
1.289 +
1.290 + ///This function just returns an \ref AddMap class.
1.291 + ///\todo How to call these type of functions?
1.292 + ///
1.293 + ///\relates AddMap
1.294 + ///\todo Wrong scope in Doxygen when \c \\relates is used
1.295 + template<class M1,class M2>
1.296 + inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
1.297 + {
1.298 + return AddMap<M1,M2>(m1,m2);
1.299 + }
1.300 +
1.301 + ///Shift a maps with a constant.
1.302 +
1.303 + ///This \ref concept::ReadMap "read only map" returns the sum of the
1.304 + ///given map and a constant value.
1.305 + ///Its \c Key and \c Value is inherited from \c M.
1.306 + ///
1.307 + ///Actually,
1.308 + ///\code
1.309 + /// ShiftMap<X> sh(x,v);
1.310 + ///\endcode
1.311 + ///it is equivalent with
1.312 + ///\code
1.313 + /// ConstMap<X::Key, X::Value> c_tmp(v);
1.314 + /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
1.315 + ///\endcode
1.316 + template<class M>
1.317 + class ShiftMap
1.318 + {
1.319 + typename SmartConstReference<M>::Type m;
1.320 + typename M::Value v;
1.321 + public:
1.322 +
1.323 + typedef True NeedCopy;
1.324 + typedef typename M::Key Key;
1.325 + typedef typename M::Value Value;
1.326 +
1.327 + ///Constructor
1.328 +
1.329 + ///Constructor
1.330 + ///\param _m is the undelying map
1.331 + ///\param _v is the shift value
1.332 + ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
1.333 + Value operator[](Key k) const {return m[k]+v;}
1.334 + };
1.335 +
1.336 + ///Returns an \ref ShiftMap class
1.337 +
1.338 + ///This function just returns an \ref ShiftMap class.
1.339 + ///\relates ShiftMap
1.340 + ///\todo A better name is required.
1.341 + template<class M>
1.342 + inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
1.343 + {
1.344 + return ShiftMap<M>(m,v);
1.345 + }
1.346 +
1.347 + ///Difference of two maps
1.348 +
1.349 + ///This \ref concept::ReadMap "read only map" returns the difference
1.350 + ///of the values returned by the two
1.351 + ///given maps. Its \c Key and \c Value will be inherited from \c M1.
1.352 + ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
1.353 +
1.354 + template<class M1,class M2>
1.355 + class SubMap
1.356 + {
1.357 + typename SmartConstReference<M1>::Type m1;
1.358 + typename SmartConstReference<M2>::Type m2;
1.359 + public:
1.360 +
1.361 + typedef True NeedCopy;
1.362 + typedef typename M1::Key Key;
1.363 + typedef typename M1::Value Value;
1.364 +
1.365 + ///Constructor
1.366 +
1.367 + ///\e
1.368 + ///
1.369 + SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
1.370 + Value operator[](Key k) const {return m1[k]-m2[k];}
1.371 + };
1.372 +
1.373 + ///Returns a \ref SubMap class
1.374 +
1.375 + ///This function just returns a \ref SubMap class.
1.376 + ///
1.377 + ///\relates SubMap
1.378 + template<class M1,class M2>
1.379 + inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
1.380 + {
1.381 + return SubMap<M1,M2>(m1,m2);
1.382 + }
1.383 +
1.384 + ///Product of two maps
1.385 +
1.386 + ///This \ref concept::ReadMap "read only map" returns the product of the
1.387 + ///values returned by the two
1.388 + ///given
1.389 + ///maps. Its \c Key and \c Value will be inherited from \c M1.
1.390 + ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
1.391 +
1.392 + template<class M1,class M2>
1.393 + class MulMap
1.394 + {
1.395 + typename SmartConstReference<M1>::Type m1;
1.396 + typename SmartConstReference<M2>::Type m2;
1.397 + public:
1.398 +
1.399 + typedef True NeedCopy;
1.400 + typedef typename M1::Key Key;
1.401 + typedef typename M1::Value Value;
1.402 +
1.403 + ///Constructor
1.404 +
1.405 + ///\e
1.406 + ///
1.407 + MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
1.408 + Value operator[](Key k) const {return m1[k]*m2[k];}
1.409 + };
1.410 +
1.411 + ///Returns a \ref MulMap class
1.412 +
1.413 + ///This function just returns a \ref MulMap class.
1.414 + ///\relates MulMap
1.415 + template<class M1,class M2>
1.416 + inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
1.417 + {
1.418 + return MulMap<M1,M2>(m1,m2);
1.419 + }
1.420 +
1.421 + ///Scale a maps with a constant.
1.422 +
1.423 + ///This \ref concept::ReadMap "read only map" returns the value of the
1.424 + ///given map multipied with a constant value.
1.425 + ///Its \c Key and \c Value is inherited from \c M.
1.426 + ///
1.427 + ///Actually,
1.428 + ///\code
1.429 + /// ScaleMap<X> sc(x,v);
1.430 + ///\endcode
1.431 + ///it is equivalent with
1.432 + ///\code
1.433 + /// ConstMap<X::Key, X::Value> c_tmp(v);
1.434 + /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
1.435 + ///\endcode
1.436 + template<class M>
1.437 + class ScaleMap
1.438 + {
1.439 + typename SmartConstReference<M>::Type m;
1.440 + typename M::Value v;
1.441 + public:
1.442 +
1.443 + typedef True NeedCopy;
1.444 + typedef typename M::Key Key;
1.445 + typedef typename M::Value Value;
1.446 +
1.447 + ///Constructor
1.448 +
1.449 + ///Constructor
1.450 + ///\param _m is the undelying map
1.451 + ///\param _v is the scaling value
1.452 + ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
1.453 + Value operator[](Key k) const {return m[k]*v;}
1.454 + };
1.455 +
1.456 + ///Returns an \ref ScaleMap class
1.457 +
1.458 + ///This function just returns an \ref ScaleMap class.
1.459 + ///\relates ScaleMap
1.460 + ///\todo A better name is required.
1.461 + template<class M>
1.462 + inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
1.463 + {
1.464 + return ScaleMap<M>(m,v);
1.465 + }
1.466 +
1.467 + ///Quotient of two maps
1.468 +
1.469 + ///This \ref concept::ReadMap "read only map" returns the quotient of the
1.470 + ///values returned by the two
1.471 + ///given maps. Its \c Key and \c Value will be inherited from \c M1.
1.472 + ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
1.473 +
1.474 + template<class M1,class M2>
1.475 + class DivMap
1.476 + {
1.477 + typename SmartConstReference<M1>::Type m1;
1.478 + typename SmartConstReference<M2>::Type m2;
1.479 + public:
1.480 +
1.481 + typedef True NeedCopy;
1.482 + typedef typename M1::Key Key;
1.483 + typedef typename M1::Value Value;
1.484 +
1.485 + ///Constructor
1.486 +
1.487 + ///\e
1.488 + ///
1.489 + DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
1.490 + Value operator[](Key k) const {return m1[k]/m2[k];}
1.491 + };
1.492 +
1.493 + ///Returns a \ref DivMap class
1.494 +
1.495 + ///This function just returns a \ref DivMap class.
1.496 + ///\relates DivMap
1.497 + template<class M1,class M2>
1.498 + inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
1.499 + {
1.500 + return DivMap<M1,M2>(m1,m2);
1.501 + }
1.502 +
1.503 + ///Composition of two maps
1.504 +
1.505 + ///This \ref concept::ReadMap "read only map" returns the composition of
1.506 + ///two
1.507 + ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
1.508 + ///of \c M2,
1.509 + ///then for
1.510 + ///\code
1.511 + /// ComposeMap<M1,M2> cm(m1,m2);
1.512 + ///\endcode
1.513 + /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
1.514 + ///
1.515 + ///Its \c Key is inherited from \c M2 and its \c Value is from
1.516 + ///\c M1.
1.517 + ///The \c M2::Value must be convertible to \c M1::Key.
1.518 + ///\todo Check the requirements.
1.519 +
1.520 + template<class M1,class M2>
1.521 + class ComposeMap
1.522 + {
1.523 + typename SmartConstReference<M1>::Type m1;
1.524 + typename SmartConstReference<M2>::Type m2;
1.525 + public:
1.526 +
1.527 + typedef True NeedCopy;
1.528 + typedef typename M2::Key Key;
1.529 + typedef typename M1::Value Value;
1.530 +
1.531 + typedef True NeedCopy;
1.532 +
1.533 + ///Constructor
1.534 +
1.535 + ///\e
1.536 + ///
1.537 + ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
1.538 + Value operator[](Key k) const {return m1[m2[k]];}
1.539 + };
1.540 + ///Returns a \ref ComposeMap class
1.541 +
1.542 + ///This function just returns a \ref ComposeMap class.
1.543 + ///
1.544 + ///\relates ComposeMap
1.545 + template<class M1,class M2>
1.546 + inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
1.547 + {
1.548 + return ComposeMap<M1,M2>(m1,m2);
1.549 + }
1.550 +
1.551 + ///Combine of two maps using an STL (binary) functor.
1.552 +
1.553 + ///Combine of two maps using an STL (binary) functor.
1.554 + ///
1.555 + ///
1.556 + ///This \ref concept::ReadMap "read only map" takes to maps and a
1.557 + ///binary functor and returns the composition of
1.558 + ///two
1.559 + ///given maps unsing the functor.
1.560 + ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
1.561 + ///and \c f is of \c F,
1.562 + ///then for
1.563 + ///\code
1.564 + /// CombineMap<M1,M2,F,V> cm(m1,m2,f);
1.565 + ///\endcode
1.566 + /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
1.567 + ///
1.568 + ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
1.569 + ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
1.570 + ///input parameter of \c F and the return type of \c F must be convertible
1.571 + ///to \c V.
1.572 + ///\todo Check the requirements.
1.573 +
1.574 + template<class M1,class M2,class F,class V = typename F::result_type>
1.575 + class CombineMap
1.576 + {
1.577 + typename SmartConstReference<M1>::Type m1;
1.578 + typename SmartConstReference<M2>::Type m2;
1.579 + F f;
1.580 + public:
1.581 +
1.582 + typedef True NeedCopy;
1.583 + typedef typename M1::Key Key;
1.584 + typedef V Value;
1.585 +
1.586 + ///Constructor
1.587 +
1.588 + ///\e
1.589 + ///
1.590 + CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
1.591 + : m1(_m1), m2(_m2), f(_f) {};
1.592 + Value operator[](Key k) const {return f(m1[k],m2[k]);}
1.593 + };
1.594 +
1.595 + ///Returns a \ref CombineMap class
1.596 +
1.597 + ///This function just returns a \ref CombineMap class.
1.598 + ///
1.599 + ///Only the first template parameter (the value type) must be given.
1.600 + ///
1.601 + ///For example if \c m1 and \c m2 are both \c double valued maps, then
1.602 + ///\code
1.603 + ///combineMap<double>(m1,m2,std::plus<double>)
1.604 + ///\endcode
1.605 + ///is equivalent with
1.606 + ///\code
1.607 + ///addMap(m1,m2)
1.608 + ///\endcode
1.609 + ///
1.610 + ///\relates CombineMap
1.611 + template<class M1,class M2,class F>
1.612 + inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
1.613 + {
1.614 + return CombineMap<M1,M2,F>(m1,m2,f);
1.615 + }
1.616 +
1.617 + ///Negative value of a map
1.618 +
1.619 + ///This \ref concept::ReadMap "read only map" returns the negative
1.620 + ///value of the
1.621 + ///value returned by the
1.622 + ///given map. Its \c Key and \c Value will be inherited from \c M.
1.623 + ///The unary \c - operator must be defined for \c Value, of course.
1.624 +
1.625 + template<class M>
1.626 + class NegMap
1.627 + {
1.628 + typename SmartConstReference<M>::Type m;
1.629 + public:
1.630 +
1.631 + typedef True NeedCopy;
1.632 + typedef typename M::Key Key;
1.633 + typedef typename M::Value Value;
1.634 +
1.635 + ///Constructor
1.636 +
1.637 + ///\e
1.638 + ///
1.639 + NegMap(const M &_m) : m(_m) {};
1.640 + Value operator[](Key k) const {return -m[k];}
1.641 + };
1.642 +
1.643 + ///Returns a \ref NegMap class
1.644 +
1.645 + ///This function just returns a \ref NegMap class.
1.646 + ///\relates NegMap
1.647 + template<class M>
1.648 + inline NegMap<M> negMap(const M &m)
1.649 + {
1.650 + return NegMap<M>(m);
1.651 + }
1.652 +
1.653 +
1.654 + ///Absolute value of a map
1.655 +
1.656 + ///This \ref concept::ReadMap "read only map" returns the absolute value
1.657 + ///of the
1.658 + ///value returned by the
1.659 + ///given map. Its \c Key and \c Value will be inherited
1.660 + ///from <tt>M</tt>. <tt>Value</tt>
1.661 + ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
1.662 + ///operator must be defined for it, of course.
1.663 + ///
1.664 + ///\bug We need a unified way to handle the situation below:
1.665 + ///\code
1.666 + /// struct _UnConvertible {};
1.667 + /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
1.668 + /// template<> inline int t_abs<>(int n) {return abs(n);}
1.669 + /// template<> inline long int t_abs<>(long int n) {return labs(n);}
1.670 + /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
1.671 + /// template<> inline float t_abs<>(float n) {return fabsf(n);}
1.672 + /// template<> inline double t_abs<>(double n) {return fabs(n);}
1.673 + /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
1.674 + ///\endcode
1.675 +
1.676 +
1.677 + template<class M>
1.678 + class AbsMap
1.679 + {
1.680 + typename SmartConstReference<M>::Type m;
1.681 + public:
1.682 +
1.683 + typedef True NeedCopy;
1.684 + typedef typename M::Key Key;
1.685 + typedef typename M::Value Value;
1.686 +
1.687 + ///Constructor
1.688 +
1.689 + ///\e
1.690 + ///
1.691 + AbsMap(const M &_m) : m(_m) {};
1.692 + Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
1.693 + };
1.694 +
1.695 + ///Returns a \ref AbsMap class
1.696 +
1.697 + ///This function just returns a \ref AbsMap class.
1.698 + ///\relates AbsMap
1.699 + template<class M>
1.700 + inline AbsMap<M> absMap(const M &m)
1.701 + {
1.702 + return AbsMap<M>(m);
1.703 + }
1.704 +
1.705 + ///Converts an STL style functor to a map
1.706 +
1.707 + ///This \ref concept::ReadMap "read only map" returns the value
1.708 + ///of a
1.709 + ///given map.
1.710 + ///
1.711 + ///Template parameters \c K and \c V will become its
1.712 + ///\c Key and \c Value. They must be given explicitely
1.713 + ///because a functor does not provide such typedefs.
1.714 + ///
1.715 + ///Parameter \c F is the type of the used functor.
1.716 +
1.717 +
1.718 + template<class K,class V,class F>
1.719 + class FunctorMap
1.720 + {
1.721 + const F &f;
1.722 + public:
1.723 +
1.724 + typedef True NeedCopy;
1.725 + typedef K Key;
1.726 + typedef V Value;
1.727 +
1.728 + ///Constructor
1.729 +
1.730 + ///\e
1.731 + ///
1.732 + FunctorMap(const F &_f) : f(_f) {};
1.733 + Value operator[](Key k) const {return f(k);}
1.734 + };
1.735 +
1.736 + ///Returns a \ref FunctorMap class
1.737 +
1.738 + ///This function just returns a \ref FunctorMap class.
1.739 + ///
1.740 + ///The third template parameter isn't necessary to be given.
1.741 + ///\relates FunctorMap
1.742 + template<class K,class V, class F>
1.743 + inline FunctorMap<K,V,F> functorMap(const F &f)
1.744 + {
1.745 + return FunctorMap<K,V,F>(f);
1.746 + }
1.747 +
1.748 + ///Converts a map to an STL style (unary) functor
1.749 +
1.750 + ///This class Converts a map to an STL style (unary) functor.
1.751 + ///that is it provides an <tt>operator()</tt> to read its values.
1.752 + ///
1.753 + ///For the sake of convenience it also works as
1.754 + ///a ususal \ref concept::ReadMap "readable map", i.e
1.755 + ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
1.756 +
1.757 + template<class M>
1.758 + class MapFunctor
1.759 + {
1.760 + typename SmartConstReference<M>::Type m;
1.761 + public:
1.762 +
1.763 + typedef True NeedCopy;
1.764 + typedef typename M::Key argument_type;
1.765 + typedef typename M::Value result_type;
1.766 + typedef typename M::Key Key;
1.767 + typedef typename M::Value Value;
1.768 +
1.769 + ///Constructor
1.770 +
1.771 + ///\e
1.772 + ///
1.773 + MapFunctor(const M &_m) : m(_m) {};
1.774 + ///Returns a value of the map
1.775 +
1.776 + ///\e
1.777 + ///
1.778 + Value operator()(Key k) const {return m[k];}
1.779 + ///\e
1.780 + ///
1.781 + Value operator[](Key k) const {return m[k];}
1.782 + };
1.783 +
1.784 + ///Returns a \ref MapFunctor class
1.785 +
1.786 + ///This function just returns a \ref MapFunctor class.
1.787 + ///\relates MapFunctor
1.788 + template<class M>
1.789 + inline MapFunctor<M> mapFunctor(const M &m)
1.790 + {
1.791 + return MapFunctor<M>(m);
1.792 + }
1.793 +
1.794 +
1.795 + ///Apply all map setting operations to two maps
1.796 +
1.797 + ///This map has two \ref concept::WriteMap "writable map"
1.798 + ///parameters and each write request will be passed to both of them.
1.799 + ///If \c M1 is also \ref concept::ReadMap "readable",
1.800 + ///then the read operations will return the
1.801 + ///corresponding values of \c M1.
1.802 + ///
1.803 + ///The \c Key and \c Value will be inherited from \c M1.
1.804 + ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1.805 +
1.806 + template<class M1,class M2>
1.807 + class ForkMap
1.808 + {
1.809 + typename SmartConstReference<M1>::Type m1;
1.810 + typename SmartConstReference<M2>::Type m2;
1.811 + public:
1.812 +
1.813 + typedef True NeedCopy;
1.814 + typedef typename M1::Key Key;
1.815 + typedef typename M1::Value Value;
1.816 +
1.817 + ///Constructor
1.818 +
1.819 + ///\e
1.820 + ///
1.821 + ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
1.822 + Value operator[](Key k) const {return m1[k];}
1.823 + void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
1.824 + };
1.825 +
1.826 + ///Returns an \ref ForkMap class
1.827 +
1.828 + ///This function just returns an \ref ForkMap class.
1.829 + ///\todo How to call these type of functions?
1.830 + ///
1.831 + ///\relates ForkMap
1.832 + ///\todo Wrong scope in Doxygen when \c \\relates is used
1.833 + template<class M1,class M2>
1.834 + inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
1.835 + {
1.836 + return ForkMap<M1,M2>(m1,m2);
1.837 + }
1.838 +
1.839 + /// @}
1.840 +
1.841 +}
1.842 +
1.843 +
1.844 +#endif // LEMON_MAPS_H