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