[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 6 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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 |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[921] | 17 | #ifndef LEMON_MAPS_H |
---|
| 18 | #define LEMON_MAPS_H |
---|
[286] | 19 | |
---|
[1420] | 20 | #include <lemon/graph_utils.h> |
---|
| 21 | #include <lemon/utility.h> |
---|
| 22 | |
---|
[1041] | 23 | |
---|
[286] | 24 | ///\file |
---|
[1041] | 25 | ///\ingroup maps |
---|
[286] | 26 | ///\brief Miscellaneous property maps |
---|
| 27 | /// |
---|
[959] | 28 | ///\todo This file has the same name as the concept file in concept/, |
---|
[286] | 29 | /// and this is not easily detectable in docs... |
---|
| 30 | |
---|
| 31 | #include <map> |
---|
| 32 | |
---|
[921] | 33 | namespace lemon { |
---|
[286] | 34 | |
---|
[1041] | 35 | /// \addtogroup maps |
---|
| 36 | /// @{ |
---|
| 37 | |
---|
[720] | 38 | /// Base class of maps. |
---|
| 39 | |
---|
[805] | 40 | /// Base class of maps. |
---|
| 41 | /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
---|
[720] | 42 | template<typename K, typename T> |
---|
| 43 | class MapBase |
---|
| 44 | { |
---|
| 45 | public: |
---|
[911] | 46 | ///\e |
---|
[987] | 47 | typedef K Key; |
---|
[911] | 48 | ///\e |
---|
[987] | 49 | typedef T Value; |
---|
[720] | 50 | }; |
---|
| 51 | |
---|
[805] | 52 | /// Null map. (a.k.a. DoNothingMap) |
---|
[286] | 53 | |
---|
| 54 | /// If you have to provide a map only for its type definitions, |
---|
[805] | 55 | /// or if you have to provide a writable map, but |
---|
| 56 | /// data written to it will sent to <tt>/dev/null</tt>... |
---|
[286] | 57 | template<typename K, typename T> |
---|
[720] | 58 | class NullMap : public MapBase<K,T> |
---|
[286] | 59 | { |
---|
| 60 | public: |
---|
[1420] | 61 | |
---|
| 62 | typedef True NeedCopy; |
---|
[286] | 63 | |
---|
[805] | 64 | /// Gives back a default constructed element. |
---|
[286] | 65 | T operator[](const K&) const { return T(); } |
---|
[805] | 66 | /// Absorbs the value. |
---|
[286] | 67 | void set(const K&, const T&) {} |
---|
| 68 | }; |
---|
| 69 | |
---|
[1420] | 70 | template <typename K, typename V> |
---|
| 71 | NullMap<K, V> nullMap() { |
---|
| 72 | return NullMap<K, V>(); |
---|
| 73 | } |
---|
| 74 | |
---|
[286] | 75 | |
---|
| 76 | /// Constant map. |
---|
| 77 | |
---|
[805] | 78 | /// This is a readable map which assigns a specified value to each key. |
---|
| 79 | /// In other aspects it is equivalent to the \ref NullMap. |
---|
| 80 | /// \todo set could be used to set the value. |
---|
[286] | 81 | template<typename K, typename T> |
---|
[720] | 82 | class ConstMap : public MapBase<K,T> |
---|
[286] | 83 | { |
---|
| 84 | T v; |
---|
| 85 | public: |
---|
| 86 | |
---|
[1420] | 87 | typedef True NeedCopy; |
---|
| 88 | |
---|
[805] | 89 | /// Default constructor |
---|
| 90 | |
---|
| 91 | /// The value of the map will be uninitialized. |
---|
| 92 | /// (More exactly it will be default constructed.) |
---|
[286] | 93 | ConstMap() {} |
---|
[911] | 94 | ///\e |
---|
[805] | 95 | |
---|
| 96 | /// \param _v The initial value of the map. |
---|
[911] | 97 | /// |
---|
[286] | 98 | ConstMap(const T &_v) : v(_v) {} |
---|
| 99 | |
---|
| 100 | T operator[](const K&) const { return v; } |
---|
| 101 | void set(const K&, const T&) {} |
---|
| 102 | |
---|
| 103 | template<typename T1> |
---|
| 104 | struct rebind { |
---|
| 105 | typedef ConstMap<K,T1> other; |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | template<typename T1> |
---|
| 109 | ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} |
---|
| 110 | }; |
---|
| 111 | |
---|
[1076] | 112 | ///Returns a \ref ConstMap class |
---|
| 113 | |
---|
| 114 | ///This function just returns a \ref ConstMap class. |
---|
| 115 | ///\relates ConstMap |
---|
| 116 | template<class V,class K> |
---|
| 117 | inline ConstMap<V,K> constMap(const K &k) |
---|
| 118 | { |
---|
| 119 | return ConstMap<V,K>(k); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[890] | 123 | //to document later |
---|
| 124 | template<typename T, T v> |
---|
| 125 | struct Const { }; |
---|
| 126 | //to document later |
---|
| 127 | template<typename K, typename V, V v> |
---|
| 128 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> |
---|
| 129 | { |
---|
| 130 | public: |
---|
| 131 | ConstMap() { } |
---|
| 132 | V operator[](const K&) const { return v; } |
---|
| 133 | void set(const K&, const V&) { } |
---|
| 134 | }; |
---|
[286] | 135 | |
---|
| 136 | /// \c std::map wrapper |
---|
| 137 | |
---|
| 138 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
[987] | 139 | /// you can specify a default value different from \c Value() . |
---|
[286] | 140 | /// |
---|
| 141 | /// \todo Provide allocator parameter... |
---|
[987] | 142 | template <typename K, typename T, typename Compare = std::less<K> > |
---|
| 143 | class StdMap : public std::map<K,T,Compare> { |
---|
| 144 | typedef std::map<K,T,Compare> parent; |
---|
[286] | 145 | T v; |
---|
| 146 | typedef typename parent::value_type PairType; |
---|
| 147 | |
---|
| 148 | public: |
---|
[987] | 149 | typedef K Key; |
---|
| 150 | typedef T Value; |
---|
| 151 | typedef T& Reference; |
---|
| 152 | typedef const T& ConstReference; |
---|
[286] | 153 | |
---|
| 154 | |
---|
[345] | 155 | StdMap() : v() {} |
---|
[286] | 156 | /// Constructor with specified default value |
---|
| 157 | StdMap(const T& _v) : v(_v) {} |
---|
| 158 | |
---|
| 159 | /// \brief Constructs the map from an appropriate std::map. |
---|
| 160 | /// |
---|
| 161 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 162 | StdMap(const parent &m) : parent(m) {} |
---|
| 163 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
| 164 | /// specifies a default value. |
---|
| 165 | /// |
---|
| 166 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 167 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
| 168 | |
---|
| 169 | template<typename T1, typename Comp1> |
---|
[389] | 170 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
| 171 | //FIXME; |
---|
| 172 | } |
---|
[286] | 173 | |
---|
[987] | 174 | Reference operator[](const Key &k) { |
---|
[346] | 175 | return insert(PairType(k,v)).first -> second; |
---|
[286] | 176 | } |
---|
[987] | 177 | ConstReference operator[](const Key &k) const { |
---|
[389] | 178 | typename parent::iterator i = lower_bound(k); |
---|
[391] | 179 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
[286] | 180 | return v; |
---|
| 181 | return (*i).second; |
---|
| 182 | } |
---|
[345] | 183 | void set(const Key &k, const T &t) { |
---|
[346] | 184 | parent::operator[](k) = t; |
---|
[345] | 185 | } |
---|
[286] | 186 | |
---|
| 187 | /// Changes the default value of the map. |
---|
| 188 | /// \return Returns the previous default value. |
---|
| 189 | /// |
---|
[805] | 190 | /// \warning The value of some keys (which has already been queried, but |
---|
[286] | 191 | /// the value has been unchanged from the default) may change! |
---|
| 192 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
| 193 | |
---|
| 194 | template<typename T1> |
---|
| 195 | struct rebind { |
---|
| 196 | typedef StdMap<Key,T1,Compare> other; |
---|
| 197 | }; |
---|
| 198 | }; |
---|
[1041] | 199 | |
---|
[1402] | 200 | /// @} |
---|
| 201 | |
---|
| 202 | /// \addtogroup map_adaptors |
---|
| 203 | /// @{ |
---|
| 204 | |
---|
| 205 | |
---|
[1178] | 206 | ///Convert the \c Value of a maps to another type. |
---|
| 207 | |
---|
| 208 | ///This \ref concept::ReadMap "read only map" |
---|
| 209 | ///converts the \c Value of a maps to type \c T. |
---|
| 210 | ///Its \c Value is inherited from \c M. |
---|
| 211 | /// |
---|
| 212 | ///Actually, |
---|
| 213 | ///\code |
---|
| 214 | /// ConvertMap<X> sh(x,v); |
---|
| 215 | ///\endcode |
---|
| 216 | ///it is equivalent with |
---|
| 217 | ///\code |
---|
| 218 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
| 219 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
| 220 | ///\endcode |
---|
| 221 | ///\bug wrong documentation |
---|
| 222 | template<class M, class T> |
---|
[1420] | 223 | class ConvertMap { |
---|
| 224 | typename SmartConstReference<M>::Type m; |
---|
[1178] | 225 | public: |
---|
[1420] | 226 | |
---|
| 227 | typedef True NeedCopy; |
---|
| 228 | |
---|
[1178] | 229 | typedef typename M::Key Key; |
---|
| 230 | typedef T Value; |
---|
| 231 | |
---|
| 232 | ///Constructor |
---|
| 233 | |
---|
| 234 | ///Constructor |
---|
| 235 | ///\param _m is the undelying map |
---|
| 236 | ///\param _v is the convert value |
---|
| 237 | ConvertMap(const M &_m) : m(_m) {}; |
---|
[1346] | 238 | |
---|
| 239 | /// \brief The subscript operator. |
---|
| 240 | /// |
---|
| 241 | /// The subscript operator. |
---|
| 242 | /// \param edge The edge |
---|
| 243 | /// \return The target of the edge |
---|
[1178] | 244 | Value operator[](Key k) const {return m[k];} |
---|
| 245 | }; |
---|
| 246 | |
---|
| 247 | ///Returns an \ref ConvertMap class |
---|
| 248 | |
---|
| 249 | ///This function just returns an \ref ConvertMap class. |
---|
| 250 | ///\relates ConvertMap |
---|
| 251 | ///\todo The order of the template parameters are changed. |
---|
| 252 | template<class T, class M> |
---|
| 253 | inline ConvertMap<M,T> convertMap(const M &m) |
---|
| 254 | { |
---|
| 255 | return ConvertMap<M,T>(m); |
---|
| 256 | } |
---|
[1041] | 257 | |
---|
| 258 | ///Sum of two maps |
---|
| 259 | |
---|
| 260 | ///This \ref concept::ReadMap "read only map" returns the sum of the two |
---|
| 261 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 262 | ///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
---|
| 263 | |
---|
| 264 | template<class M1,class M2> |
---|
| 265 | class AddMap |
---|
| 266 | { |
---|
[1420] | 267 | typename SmartConstReference<M1>::Type m1; |
---|
| 268 | typename SmartConstReference<M2>::Type m2; |
---|
| 269 | |
---|
[1041] | 270 | public: |
---|
[1420] | 271 | |
---|
| 272 | typedef True NeedCopy; |
---|
| 273 | |
---|
[1041] | 274 | typedef typename M1::Key Key; |
---|
| 275 | typedef typename M1::Value Value; |
---|
| 276 | |
---|
| 277 | ///Constructor |
---|
| 278 | |
---|
| 279 | ///\e |
---|
| 280 | /// |
---|
| 281 | AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 282 | Value operator[](Key k) const {return m1[k]+m2[k];} |
---|
[1041] | 283 | }; |
---|
| 284 | |
---|
| 285 | ///Returns an \ref AddMap class |
---|
| 286 | |
---|
| 287 | ///This function just returns an \ref AddMap class. |
---|
| 288 | ///\todo How to call these type of functions? |
---|
| 289 | /// |
---|
| 290 | ///\relates AddMap |
---|
| 291 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
| 292 | template<class M1,class M2> |
---|
| 293 | inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) |
---|
| 294 | { |
---|
| 295 | return AddMap<M1,M2>(m1,m2); |
---|
| 296 | } |
---|
| 297 | |
---|
[1070] | 298 | ///Shift a maps with a constant. |
---|
| 299 | |
---|
| 300 | ///This \ref concept::ReadMap "read only map" returns the sum of the |
---|
| 301 | ///given map and a constant value. |
---|
| 302 | ///Its \c Key and \c Value is inherited from \c M. |
---|
| 303 | /// |
---|
| 304 | ///Actually, |
---|
| 305 | ///\code |
---|
| 306 | /// ShiftMap<X> sh(x,v); |
---|
| 307 | ///\endcode |
---|
| 308 | ///it is equivalent with |
---|
| 309 | ///\code |
---|
| 310 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
| 311 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
| 312 | ///\endcode |
---|
| 313 | template<class M> |
---|
| 314 | class ShiftMap |
---|
| 315 | { |
---|
[1420] | 316 | typename SmartConstReference<M>::Type m; |
---|
[1070] | 317 | typename M::Value v; |
---|
| 318 | public: |
---|
[1420] | 319 | |
---|
| 320 | typedef True NeedCopy; |
---|
[1070] | 321 | typedef typename M::Key Key; |
---|
| 322 | typedef typename M::Value Value; |
---|
| 323 | |
---|
| 324 | ///Constructor |
---|
| 325 | |
---|
| 326 | ///Constructor |
---|
| 327 | ///\param _m is the undelying map |
---|
| 328 | ///\param _v is the shift value |
---|
| 329 | ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
| 330 | Value operator[](Key k) const {return m[k]+v;} |
---|
| 331 | }; |
---|
| 332 | |
---|
| 333 | ///Returns an \ref ShiftMap class |
---|
| 334 | |
---|
| 335 | ///This function just returns an \ref ShiftMap class. |
---|
| 336 | ///\relates ShiftMap |
---|
| 337 | ///\todo A better name is required. |
---|
| 338 | template<class M> |
---|
| 339 | inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) |
---|
| 340 | { |
---|
| 341 | return ShiftMap<M>(m,v); |
---|
| 342 | } |
---|
| 343 | |
---|
[1041] | 344 | ///Difference of two maps |
---|
| 345 | |
---|
| 346 | ///This \ref concept::ReadMap "read only map" returns the difference |
---|
| 347 | ///of the values returned by the two |
---|
| 348 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 349 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
| 350 | |
---|
| 351 | template<class M1,class M2> |
---|
| 352 | class SubMap |
---|
| 353 | { |
---|
[1420] | 354 | typename SmartConstReference<M1>::Type m1; |
---|
| 355 | typename SmartConstReference<M2>::Type m2; |
---|
[1041] | 356 | public: |
---|
[1420] | 357 | |
---|
| 358 | typedef True NeedCopy; |
---|
[1041] | 359 | typedef typename M1::Key Key; |
---|
| 360 | typedef typename M1::Value Value; |
---|
| 361 | |
---|
| 362 | ///Constructor |
---|
| 363 | |
---|
| 364 | ///\e |
---|
| 365 | /// |
---|
| 366 | SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 367 | Value operator[](Key k) const {return m1[k]-m2[k];} |
---|
[1041] | 368 | }; |
---|
| 369 | |
---|
| 370 | ///Returns a \ref SubMap class |
---|
| 371 | |
---|
| 372 | ///This function just returns a \ref SubMap class. |
---|
| 373 | /// |
---|
| 374 | ///\relates SubMap |
---|
| 375 | template<class M1,class M2> |
---|
| 376 | inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) |
---|
| 377 | { |
---|
| 378 | return SubMap<M1,M2>(m1,m2); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | ///Product of two maps |
---|
| 382 | |
---|
| 383 | ///This \ref concept::ReadMap "read only map" returns the product of the |
---|
| 384 | ///values returned by the two |
---|
| 385 | ///given |
---|
| 386 | ///maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 387 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
| 388 | |
---|
| 389 | template<class M1,class M2> |
---|
| 390 | class MulMap |
---|
| 391 | { |
---|
[1420] | 392 | typename SmartConstReference<M1>::Type m1; |
---|
| 393 | typename SmartConstReference<M2>::Type m2; |
---|
[1041] | 394 | public: |
---|
[1420] | 395 | |
---|
| 396 | typedef True NeedCopy; |
---|
[1041] | 397 | typedef typename M1::Key Key; |
---|
| 398 | typedef typename M1::Value Value; |
---|
| 399 | |
---|
| 400 | ///Constructor |
---|
| 401 | |
---|
| 402 | ///\e |
---|
| 403 | /// |
---|
| 404 | MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 405 | Value operator[](Key k) const {return m1[k]*m2[k];} |
---|
[1041] | 406 | }; |
---|
| 407 | |
---|
| 408 | ///Returns a \ref MulMap class |
---|
| 409 | |
---|
| 410 | ///This function just returns a \ref MulMap class. |
---|
| 411 | ///\relates MulMap |
---|
| 412 | template<class M1,class M2> |
---|
| 413 | inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) |
---|
| 414 | { |
---|
| 415 | return MulMap<M1,M2>(m1,m2); |
---|
| 416 | } |
---|
| 417 | |
---|
[1070] | 418 | ///Scale a maps with a constant. |
---|
| 419 | |
---|
| 420 | ///This \ref concept::ReadMap "read only map" returns the value of the |
---|
| 421 | ///given map multipied with a constant value. |
---|
| 422 | ///Its \c Key and \c Value is inherited from \c M. |
---|
| 423 | /// |
---|
| 424 | ///Actually, |
---|
| 425 | ///\code |
---|
| 426 | /// ScaleMap<X> sc(x,v); |
---|
| 427 | ///\endcode |
---|
| 428 | ///it is equivalent with |
---|
| 429 | ///\code |
---|
| 430 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
| 431 | /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
---|
| 432 | ///\endcode |
---|
| 433 | template<class M> |
---|
| 434 | class ScaleMap |
---|
| 435 | { |
---|
[1420] | 436 | typename SmartConstReference<M>::Type m; |
---|
[1070] | 437 | typename M::Value v; |
---|
| 438 | public: |
---|
[1420] | 439 | |
---|
| 440 | typedef True NeedCopy; |
---|
[1070] | 441 | typedef typename M::Key Key; |
---|
| 442 | typedef typename M::Value Value; |
---|
| 443 | |
---|
| 444 | ///Constructor |
---|
| 445 | |
---|
| 446 | ///Constructor |
---|
| 447 | ///\param _m is the undelying map |
---|
| 448 | ///\param _v is the scaling value |
---|
| 449 | ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
| 450 | Value operator[](Key k) const {return m[k]*v;} |
---|
| 451 | }; |
---|
| 452 | |
---|
| 453 | ///Returns an \ref ScaleMap class |
---|
| 454 | |
---|
| 455 | ///This function just returns an \ref ScaleMap class. |
---|
| 456 | ///\relates ScaleMap |
---|
| 457 | ///\todo A better name is required. |
---|
| 458 | template<class M> |
---|
| 459 | inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) |
---|
| 460 | { |
---|
| 461 | return ScaleMap<M>(m,v); |
---|
| 462 | } |
---|
| 463 | |
---|
[1041] | 464 | ///Quotient of two maps |
---|
| 465 | |
---|
| 466 | ///This \ref concept::ReadMap "read only map" returns the quotient of the |
---|
| 467 | ///values returned by the two |
---|
| 468 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 469 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
| 470 | |
---|
| 471 | template<class M1,class M2> |
---|
| 472 | class DivMap |
---|
| 473 | { |
---|
[1420] | 474 | typename SmartConstReference<M1>::Type m1; |
---|
| 475 | typename SmartConstReference<M2>::Type m2; |
---|
[1041] | 476 | public: |
---|
[1420] | 477 | |
---|
| 478 | typedef True NeedCopy; |
---|
[1041] | 479 | typedef typename M1::Key Key; |
---|
| 480 | typedef typename M1::Value Value; |
---|
| 481 | |
---|
| 482 | ///Constructor |
---|
| 483 | |
---|
| 484 | ///\e |
---|
| 485 | /// |
---|
| 486 | DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 487 | Value operator[](Key k) const {return m1[k]/m2[k];} |
---|
[1041] | 488 | }; |
---|
| 489 | |
---|
| 490 | ///Returns a \ref DivMap class |
---|
| 491 | |
---|
| 492 | ///This function just returns a \ref DivMap class. |
---|
| 493 | ///\relates DivMap |
---|
| 494 | template<class M1,class M2> |
---|
| 495 | inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) |
---|
| 496 | { |
---|
| 497 | return DivMap<M1,M2>(m1,m2); |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | ///Composition of two maps |
---|
| 501 | |
---|
| 502 | ///This \ref concept::ReadMap "read only map" returns the composition of |
---|
| 503 | ///two |
---|
| 504 | ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is |
---|
| 505 | ///of \c M2, |
---|
| 506 | ///then for |
---|
| 507 | ///\code |
---|
| 508 | /// ComposeMap<M1,M2> cm(m1,m2); |
---|
| 509 | ///\endcode |
---|
[1044] | 510 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt> |
---|
[1041] | 511 | /// |
---|
| 512 | ///Its \c Key is inherited from \c M2 and its \c Value is from |
---|
| 513 | ///\c M1. |
---|
| 514 | ///The \c M2::Value must be convertible to \c M1::Key. |
---|
| 515 | ///\todo Check the requirements. |
---|
| 516 | |
---|
| 517 | template<class M1,class M2> |
---|
| 518 | class ComposeMap |
---|
| 519 | { |
---|
[1420] | 520 | typename SmartConstReference<M1>::Type m1; |
---|
| 521 | typename SmartConstReference<M2>::Type m2; |
---|
[1041] | 522 | public: |
---|
[1420] | 523 | |
---|
| 524 | typedef True NeedCopy; |
---|
[1041] | 525 | typedef typename M2::Key Key; |
---|
| 526 | typedef typename M1::Value Value; |
---|
| 527 | |
---|
[1420] | 528 | typedef True NeedCopy; |
---|
| 529 | |
---|
[1041] | 530 | ///Constructor |
---|
| 531 | |
---|
| 532 | ///\e |
---|
| 533 | /// |
---|
| 534 | ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 535 | Value operator[](Key k) const {return m1[m2[k]];} |
---|
[1041] | 536 | }; |
---|
| 537 | ///Returns a \ref ComposeMap class |
---|
| 538 | |
---|
| 539 | ///This function just returns a \ref ComposeMap class. |
---|
[1219] | 540 | /// |
---|
[1041] | 541 | ///\relates ComposeMap |
---|
| 542 | template<class M1,class M2> |
---|
| 543 | inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) |
---|
| 544 | { |
---|
| 545 | return ComposeMap<M1,M2>(m1,m2); |
---|
| 546 | } |
---|
[1219] | 547 | |
---|
| 548 | ///Combine of two maps using an STL (binary) functor. |
---|
| 549 | |
---|
| 550 | ///Combine of two maps using an STL (binary) functor. |
---|
| 551 | /// |
---|
| 552 | /// |
---|
| 553 | ///This \ref concept::ReadMap "read only map" takes to maps and a |
---|
| 554 | ///binary functor and returns the composition of |
---|
| 555 | ///two |
---|
| 556 | ///given maps unsing the functor. |
---|
| 557 | ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
---|
| 558 | ///and \c f is of \c F, |
---|
| 559 | ///then for |
---|
| 560 | ///\code |
---|
| 561 | /// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
---|
| 562 | ///\endcode |
---|
| 563 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
---|
| 564 | /// |
---|
| 565 | ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
---|
| 566 | ///The \c M2::Value and \c M1::Value must be convertible to the corresponding |
---|
| 567 | ///input parameter of \c F and the return type of \c F must be convertible |
---|
| 568 | ///to \c V. |
---|
| 569 | ///\todo Check the requirements. |
---|
| 570 | |
---|
[1420] | 571 | template<class M1,class M2,class F,class V = typename F::result_type> |
---|
[1219] | 572 | class CombineMap |
---|
| 573 | { |
---|
[1420] | 574 | typename SmartConstReference<M1>::Type m1; |
---|
| 575 | typename SmartConstReference<M2>::Type m2; |
---|
| 576 | F f; |
---|
[1219] | 577 | public: |
---|
[1420] | 578 | |
---|
| 579 | typedef True NeedCopy; |
---|
[1219] | 580 | typedef typename M1::Key Key; |
---|
| 581 | typedef V Value; |
---|
| 582 | |
---|
| 583 | ///Constructor |
---|
| 584 | |
---|
| 585 | ///\e |
---|
| 586 | /// |
---|
| 587 | CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) |
---|
| 588 | : m1(_m1), m2(_m2), f(_f) {}; |
---|
| 589 | Value operator[](Key k) const {return f(m1[k],m2[k]);} |
---|
| 590 | }; |
---|
| 591 | |
---|
| 592 | ///Returns a \ref CombineMap class |
---|
| 593 | |
---|
| 594 | ///This function just returns a \ref CombineMap class. |
---|
| 595 | /// |
---|
| 596 | ///Only the first template parameter (the value type) must be given. |
---|
| 597 | /// |
---|
| 598 | ///For example if \c m1 and \c m2 are both \c double valued maps, then |
---|
| 599 | ///\code |
---|
| 600 | ///combineMap<double>(m1,m2,std::plus<double>) |
---|
| 601 | ///\endcode |
---|
| 602 | ///is equivalent with |
---|
| 603 | ///\code |
---|
| 604 | ///addMap(m1,m2) |
---|
| 605 | ///\endcode |
---|
| 606 | /// |
---|
| 607 | ///\relates CombineMap |
---|
[1420] | 608 | template<class M1,class M2,class F> |
---|
| 609 | inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) |
---|
[1219] | 610 | { |
---|
[1420] | 611 | return CombineMap<M1,M2,F>(m1,m2,f); |
---|
[1219] | 612 | } |
---|
[1041] | 613 | |
---|
| 614 | ///Negative value of a map |
---|
| 615 | |
---|
| 616 | ///This \ref concept::ReadMap "read only map" returns the negative |
---|
| 617 | ///value of the |
---|
| 618 | ///value returned by the |
---|
| 619 | ///given map. Its \c Key and \c Value will be inherited from \c M. |
---|
| 620 | ///The unary \c - operator must be defined for \c Value, of course. |
---|
| 621 | |
---|
| 622 | template<class M> |
---|
| 623 | class NegMap |
---|
| 624 | { |
---|
[1420] | 625 | typename SmartConstReference<M>::Type m; |
---|
[1041] | 626 | public: |
---|
[1420] | 627 | |
---|
| 628 | typedef True NeedCopy; |
---|
[1041] | 629 | typedef typename M::Key Key; |
---|
| 630 | typedef typename M::Value Value; |
---|
| 631 | |
---|
| 632 | ///Constructor |
---|
| 633 | |
---|
| 634 | ///\e |
---|
| 635 | /// |
---|
| 636 | NegMap(const M &_m) : m(_m) {}; |
---|
[1044] | 637 | Value operator[](Key k) const {return -m[k];} |
---|
[1041] | 638 | }; |
---|
| 639 | |
---|
| 640 | ///Returns a \ref NegMap class |
---|
| 641 | |
---|
| 642 | ///This function just returns a \ref NegMap class. |
---|
| 643 | ///\relates NegMap |
---|
| 644 | template<class M> |
---|
| 645 | inline NegMap<M> negMap(const M &m) |
---|
| 646 | { |
---|
| 647 | return NegMap<M>(m); |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | ///Absolute value of a map |
---|
| 652 | |
---|
| 653 | ///This \ref concept::ReadMap "read only map" returns the absolute value |
---|
| 654 | ///of the |
---|
| 655 | ///value returned by the |
---|
[1044] | 656 | ///given map. Its \c Key and \c Value will be inherited |
---|
| 657 | ///from <tt>M</tt>. <tt>Value</tt> |
---|
| 658 | ///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
---|
| 659 | ///operator must be defined for it, of course. |
---|
| 660 | /// |
---|
| 661 | ///\bug We need a unified way to handle the situation below: |
---|
| 662 | ///\code |
---|
| 663 | /// struct _UnConvertible {}; |
---|
| 664 | /// template<class A> inline A t_abs(A a) {return _UnConvertible();} |
---|
| 665 | /// template<> inline int t_abs<>(int n) {return abs(n);} |
---|
| 666 | /// template<> inline long int t_abs<>(long int n) {return labs(n);} |
---|
| 667 | /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} |
---|
| 668 | /// template<> inline float t_abs<>(float n) {return fabsf(n);} |
---|
| 669 | /// template<> inline double t_abs<>(double n) {return fabs(n);} |
---|
| 670 | /// template<> inline long double t_abs<>(long double n) {return fabsl(n);} |
---|
| 671 | ///\endcode |
---|
| 672 | |
---|
[1041] | 673 | |
---|
| 674 | template<class M> |
---|
| 675 | class AbsMap |
---|
| 676 | { |
---|
[1420] | 677 | typename SmartConstReference<M>::Type m; |
---|
[1041] | 678 | public: |
---|
[1420] | 679 | |
---|
| 680 | typedef True NeedCopy; |
---|
[1041] | 681 | typedef typename M::Key Key; |
---|
| 682 | typedef typename M::Value Value; |
---|
| 683 | |
---|
| 684 | ///Constructor |
---|
| 685 | |
---|
| 686 | ///\e |
---|
| 687 | /// |
---|
| 688 | AbsMap(const M &_m) : m(_m) {}; |
---|
[1044] | 689 | Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;} |
---|
[1041] | 690 | }; |
---|
| 691 | |
---|
| 692 | ///Returns a \ref AbsMap class |
---|
| 693 | |
---|
| 694 | ///This function just returns a \ref AbsMap class. |
---|
| 695 | ///\relates AbsMap |
---|
| 696 | template<class M> |
---|
| 697 | inline AbsMap<M> absMap(const M &m) |
---|
| 698 | { |
---|
| 699 | return AbsMap<M>(m); |
---|
| 700 | } |
---|
| 701 | |
---|
[1402] | 702 | ///Converts an STL style functor to a map |
---|
[1076] | 703 | |
---|
| 704 | ///This \ref concept::ReadMap "read only map" returns the value |
---|
| 705 | ///of a |
---|
| 706 | ///given map. |
---|
| 707 | /// |
---|
| 708 | ///Template parameters \c K and \c V will become its |
---|
| 709 | ///\c Key and \c Value. They must be given explicitely |
---|
| 710 | ///because a functor does not provide such typedefs. |
---|
| 711 | /// |
---|
| 712 | ///Parameter \c F is the type of the used functor. |
---|
| 713 | |
---|
| 714 | |
---|
| 715 | template<class K,class V,class F> |
---|
| 716 | class FunctorMap |
---|
| 717 | { |
---|
| 718 | const F &f; |
---|
| 719 | public: |
---|
[1420] | 720 | |
---|
| 721 | typedef True NeedCopy; |
---|
[1076] | 722 | typedef K Key; |
---|
| 723 | typedef V Value; |
---|
| 724 | |
---|
| 725 | ///Constructor |
---|
| 726 | |
---|
| 727 | ///\e |
---|
| 728 | /// |
---|
| 729 | FunctorMap(const F &_f) : f(_f) {}; |
---|
| 730 | Value operator[](Key k) const {return f(k);} |
---|
| 731 | }; |
---|
| 732 | |
---|
| 733 | ///Returns a \ref FunctorMap class |
---|
| 734 | |
---|
| 735 | ///This function just returns a \ref FunctorMap class. |
---|
| 736 | /// |
---|
| 737 | ///The third template parameter isn't necessary to be given. |
---|
| 738 | ///\relates FunctorMap |
---|
| 739 | template<class K,class V, class F> |
---|
| 740 | inline FunctorMap<K,V,F> functorMap(const F &f) |
---|
| 741 | { |
---|
| 742 | return FunctorMap<K,V,F>(f); |
---|
| 743 | } |
---|
| 744 | |
---|
[1219] | 745 | ///Converts a map to an STL style (unary) functor |
---|
[1076] | 746 | |
---|
[1219] | 747 | ///This class Converts a map to an STL style (unary) functor. |
---|
[1076] | 748 | ///that is it provides an <tt>operator()</tt> to read its values. |
---|
| 749 | /// |
---|
[1223] | 750 | ///For the sake of convenience it also works as |
---|
| 751 | ///a ususal \ref concept::ReadMap "readable map", i.e |
---|
[1172] | 752 | ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
---|
[1076] | 753 | |
---|
| 754 | template<class M> |
---|
| 755 | class MapFunctor |
---|
| 756 | { |
---|
[1420] | 757 | typename SmartConstReference<M>::Type m; |
---|
[1076] | 758 | public: |
---|
[1420] | 759 | |
---|
| 760 | typedef True NeedCopy; |
---|
[1223] | 761 | typedef typename M::Key argument_type; |
---|
| 762 | typedef typename M::Value result_type; |
---|
[1076] | 763 | typedef typename M::Key Key; |
---|
| 764 | typedef typename M::Value Value; |
---|
| 765 | |
---|
| 766 | ///Constructor |
---|
| 767 | |
---|
| 768 | ///\e |
---|
| 769 | /// |
---|
| 770 | MapFunctor(const M &_m) : m(_m) {}; |
---|
| 771 | ///Returns a value of the map |
---|
| 772 | |
---|
| 773 | ///\e |
---|
| 774 | /// |
---|
| 775 | Value operator()(Key k) const {return m[k];} |
---|
| 776 | ///\e |
---|
| 777 | /// |
---|
| 778 | Value operator[](Key k) const {return m[k];} |
---|
| 779 | }; |
---|
| 780 | |
---|
| 781 | ///Returns a \ref MapFunctor class |
---|
| 782 | |
---|
| 783 | ///This function just returns a \ref MapFunctor class. |
---|
| 784 | ///\relates MapFunctor |
---|
| 785 | template<class M> |
---|
| 786 | inline MapFunctor<M> mapFunctor(const M &m) |
---|
| 787 | { |
---|
| 788 | return MapFunctor<M>(m); |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | |
---|
[1219] | 792 | ///Apply all map setting operations to two maps |
---|
| 793 | |
---|
| 794 | ///This map has two \ref concept::WriteMap "writable map" |
---|
| 795 | ///parameters and each write request will be passed to both of them. |
---|
| 796 | ///If \c M1 is also \ref concept::ReadMap "readable", |
---|
| 797 | ///then the read operations will return the |
---|
[1317] | 798 | ///corresponding values of \c M1. |
---|
[1219] | 799 | /// |
---|
| 800 | ///The \c Key and \c Value will be inherited from \c M1. |
---|
| 801 | ///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
---|
| 802 | |
---|
| 803 | template<class M1,class M2> |
---|
| 804 | class ForkMap |
---|
| 805 | { |
---|
[1420] | 806 | typename SmartConstReference<M1>::Type m1; |
---|
| 807 | typename SmartConstReference<M2>::Type m2; |
---|
[1219] | 808 | public: |
---|
[1420] | 809 | |
---|
| 810 | typedef True NeedCopy; |
---|
[1219] | 811 | typedef typename M1::Key Key; |
---|
| 812 | typedef typename M1::Value Value; |
---|
| 813 | |
---|
| 814 | ///Constructor |
---|
| 815 | |
---|
| 816 | ///\e |
---|
| 817 | /// |
---|
| 818 | ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
| 819 | Value operator[](Key k) const {return m1[k];} |
---|
| 820 | void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);} |
---|
| 821 | }; |
---|
| 822 | |
---|
| 823 | ///Returns an \ref ForkMap class |
---|
| 824 | |
---|
| 825 | ///This function just returns an \ref ForkMap class. |
---|
| 826 | ///\todo How to call these type of functions? |
---|
| 827 | /// |
---|
| 828 | ///\relates ForkMap |
---|
| 829 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
| 830 | template<class M1,class M2> |
---|
| 831 | inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) |
---|
| 832 | { |
---|
| 833 | return ForkMap<M1,M2>(m1,m2); |
---|
| 834 | } |
---|
| 835 | |
---|
[1041] | 836 | /// @} |
---|
[286] | 837 | |
---|
| 838 | } |
---|
[1041] | 839 | |
---|
| 840 | |
---|
[921] | 841 | #endif // LEMON_MAPS_H |
---|