[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[921] | 19 | #ifndef LEMON_MAPS_H |
---|
| 20 | #define LEMON_MAPS_H |
---|
[286] | 21 | |
---|
[1778] | 22 | #include <iterator> |
---|
[2091] | 23 | #include <functional> |
---|
[1778] | 24 | |
---|
[1993] | 25 | #include <lemon/bits/utility.h> |
---|
| 26 | #include <lemon/bits/traits.h> |
---|
[1041] | 27 | |
---|
[286] | 28 | ///\file |
---|
[1041] | 29 | ///\ingroup maps |
---|
[286] | 30 | ///\brief Miscellaneous property maps |
---|
| 31 | /// |
---|
[959] | 32 | ///\todo This file has the same name as the concept file in concept/, |
---|
[286] | 33 | /// and this is not easily detectable in docs... |
---|
| 34 | |
---|
| 35 | #include <map> |
---|
| 36 | |
---|
[921] | 37 | namespace lemon { |
---|
[286] | 38 | |
---|
[1041] | 39 | /// \addtogroup maps |
---|
| 40 | /// @{ |
---|
| 41 | |
---|
[720] | 42 | /// Base class of maps. |
---|
| 43 | |
---|
[805] | 44 | /// Base class of maps. |
---|
| 45 | /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
---|
[1705] | 46 | template<typename K, typename T> |
---|
[1675] | 47 | class MapBase { |
---|
[720] | 48 | public: |
---|
[911] | 49 | ///\e |
---|
[987] | 50 | typedef K Key; |
---|
[911] | 51 | ///\e |
---|
[987] | 52 | typedef T Value; |
---|
[720] | 53 | }; |
---|
| 54 | |
---|
[805] | 55 | /// Null map. (a.k.a. DoNothingMap) |
---|
[286] | 56 | |
---|
| 57 | /// If you have to provide a map only for its type definitions, |
---|
[805] | 58 | /// or if you have to provide a writable map, but |
---|
| 59 | /// data written to it will sent to <tt>/dev/null</tt>... |
---|
[1705] | 60 | template<typename K, typename T> |
---|
| 61 | class NullMap : public MapBase<K, T> { |
---|
[286] | 62 | public: |
---|
[1705] | 63 | typedef MapBase<K, T> Parent; |
---|
[1675] | 64 | typedef typename Parent::Key Key; |
---|
| 65 | typedef typename Parent::Value Value; |
---|
[1420] | 66 | |
---|
[805] | 67 | /// Gives back a default constructed element. |
---|
[286] | 68 | T operator[](const K&) const { return T(); } |
---|
[805] | 69 | /// Absorbs the value. |
---|
[286] | 70 | void set(const K&, const T&) {} |
---|
| 71 | }; |
---|
| 72 | |
---|
[1420] | 73 | template <typename K, typename V> |
---|
[1705] | 74 | NullMap<K, V> nullMap() { |
---|
| 75 | return NullMap<K, V>(); |
---|
[1420] | 76 | } |
---|
| 77 | |
---|
[286] | 78 | |
---|
| 79 | /// Constant map. |
---|
| 80 | |
---|
[805] | 81 | /// This is a readable map which assigns a specified value to each key. |
---|
| 82 | /// In other aspects it is equivalent to the \ref NullMap. |
---|
| 83 | /// \todo set could be used to set the value. |
---|
[1705] | 84 | template<typename K, typename T> |
---|
| 85 | class ConstMap : public MapBase<K, T> { |
---|
[1675] | 86 | private: |
---|
[286] | 87 | T v; |
---|
| 88 | public: |
---|
| 89 | |
---|
[1705] | 90 | typedef MapBase<K, T> Parent; |
---|
[1675] | 91 | typedef typename Parent::Key Key; |
---|
| 92 | typedef typename Parent::Value Value; |
---|
[1420] | 93 | |
---|
[805] | 94 | /// Default constructor |
---|
| 95 | |
---|
| 96 | /// The value of the map will be uninitialized. |
---|
| 97 | /// (More exactly it will be default constructed.) |
---|
[286] | 98 | ConstMap() {} |
---|
[911] | 99 | ///\e |
---|
[805] | 100 | |
---|
| 101 | /// \param _v The initial value of the map. |
---|
[911] | 102 | /// |
---|
[286] | 103 | ConstMap(const T &_v) : v(_v) {} |
---|
| 104 | |
---|
| 105 | T operator[](const K&) const { return v; } |
---|
| 106 | void set(const K&, const T&) {} |
---|
| 107 | |
---|
| 108 | template<typename T1> |
---|
| 109 | struct rebind { |
---|
[1675] | 110 | typedef ConstMap<K, T1> other; |
---|
[286] | 111 | }; |
---|
| 112 | |
---|
| 113 | template<typename T1> |
---|
[1675] | 114 | ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {} |
---|
[286] | 115 | }; |
---|
| 116 | |
---|
[1076] | 117 | ///Returns a \ref ConstMap class |
---|
| 118 | |
---|
| 119 | ///This function just returns a \ref ConstMap class. |
---|
| 120 | ///\relates ConstMap |
---|
[1675] | 121 | template<typename K, typename V> |
---|
[1705] | 122 | inline ConstMap<K, V> constMap(const V &v) { |
---|
| 123 | return ConstMap<K, V>(v); |
---|
[1076] | 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
[1660] | 127 | //\todo to document later |
---|
[890] | 128 | template<typename T, T v> |
---|
| 129 | struct Const { }; |
---|
[1675] | 130 | |
---|
[1660] | 131 | //\todo to document later |
---|
[1705] | 132 | template<typename K, typename V, V v> |
---|
| 133 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
---|
[890] | 134 | public: |
---|
[1705] | 135 | typedef MapBase<K, V> Parent; |
---|
[1675] | 136 | typedef typename Parent::Key Key; |
---|
| 137 | typedef typename Parent::Value Value; |
---|
| 138 | |
---|
[890] | 139 | ConstMap() { } |
---|
| 140 | V operator[](const K&) const { return v; } |
---|
| 141 | void set(const K&, const V&) { } |
---|
| 142 | }; |
---|
[286] | 143 | |
---|
[1675] | 144 | ///Returns a \ref ConstMap class |
---|
| 145 | |
---|
| 146 | ///This function just returns a \ref ConstMap class. |
---|
| 147 | ///\relates ConstMap |
---|
| 148 | template<typename K, typename V, V v> |
---|
[1705] | 149 | inline ConstMap<K, Const<V, v> > constMap() { |
---|
| 150 | return ConstMap<K, Const<V, v> >(); |
---|
[1675] | 151 | } |
---|
| 152 | |
---|
[286] | 153 | /// \c std::map wrapper |
---|
| 154 | |
---|
| 155 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
[987] | 156 | /// you can specify a default value different from \c Value() . |
---|
[286] | 157 | /// |
---|
| 158 | /// \todo Provide allocator parameter... |
---|
[987] | 159 | template <typename K, typename T, typename Compare = std::less<K> > |
---|
[1675] | 160 | class StdMap : public std::map<K, T, Compare> { |
---|
| 161 | typedef std::map<K, T, Compare> parent; |
---|
[286] | 162 | T v; |
---|
| 163 | typedef typename parent::value_type PairType; |
---|
| 164 | |
---|
| 165 | public: |
---|
[1456] | 166 | ///\e |
---|
[987] | 167 | typedef K Key; |
---|
[1456] | 168 | ///\e |
---|
[987] | 169 | typedef T Value; |
---|
[1456] | 170 | ///\e |
---|
[987] | 171 | typedef T& Reference; |
---|
[1456] | 172 | ///\e |
---|
[987] | 173 | typedef const T& ConstReference; |
---|
[286] | 174 | |
---|
| 175 | |
---|
[345] | 176 | StdMap() : v() {} |
---|
[286] | 177 | /// Constructor with specified default value |
---|
| 178 | StdMap(const T& _v) : v(_v) {} |
---|
| 179 | |
---|
| 180 | /// \brief Constructs the map from an appropriate std::map. |
---|
| 181 | /// |
---|
| 182 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 183 | StdMap(const parent &m) : parent(m) {} |
---|
| 184 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
| 185 | /// specifies a default value. |
---|
| 186 | /// |
---|
| 187 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 188 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
| 189 | |
---|
| 190 | template<typename T1, typename Comp1> |
---|
[1675] | 191 | StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) { |
---|
[389] | 192 | //FIXME; |
---|
| 193 | } |
---|
[286] | 194 | |
---|
[987] | 195 | Reference operator[](const Key &k) { |
---|
[346] | 196 | return insert(PairType(k,v)).first -> second; |
---|
[286] | 197 | } |
---|
[1675] | 198 | |
---|
[987] | 199 | ConstReference operator[](const Key &k) const { |
---|
[389] | 200 | typename parent::iterator i = lower_bound(k); |
---|
[391] | 201 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
[286] | 202 | return v; |
---|
| 203 | return (*i).second; |
---|
| 204 | } |
---|
[345] | 205 | void set(const Key &k, const T &t) { |
---|
[346] | 206 | parent::operator[](k) = t; |
---|
[345] | 207 | } |
---|
[286] | 208 | |
---|
| 209 | /// Changes the default value of the map. |
---|
| 210 | /// \return Returns the previous default value. |
---|
| 211 | /// |
---|
[805] | 212 | /// \warning The value of some keys (which has already been queried, but |
---|
[286] | 213 | /// the value has been unchanged from the default) may change! |
---|
| 214 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
| 215 | |
---|
| 216 | template<typename T1> |
---|
| 217 | struct rebind { |
---|
[1675] | 218 | typedef StdMap<Key, T1,Compare> other; |
---|
[286] | 219 | }; |
---|
| 220 | }; |
---|
[1041] | 221 | |
---|
[1402] | 222 | /// @} |
---|
| 223 | |
---|
| 224 | /// \addtogroup map_adaptors |
---|
| 225 | /// @{ |
---|
| 226 | |
---|
[1531] | 227 | /// \brief Identity mapping. |
---|
| 228 | /// |
---|
| 229 | /// This mapping gives back the given key as value without any |
---|
| 230 | /// modification. |
---|
[1705] | 231 | template <typename T> |
---|
| 232 | class IdentityMap : public MapBase<T, T> { |
---|
[1531] | 233 | public: |
---|
[1705] | 234 | typedef MapBase<T, T> Parent; |
---|
[1675] | 235 | typedef typename Parent::Key Key; |
---|
| 236 | typedef typename Parent::Value Value; |
---|
[1531] | 237 | |
---|
[1675] | 238 | const T& operator[](const T& t) const { |
---|
[1531] | 239 | return t; |
---|
| 240 | } |
---|
| 241 | }; |
---|
[1402] | 242 | |
---|
[1675] | 243 | ///Returns an \ref IdentityMap class |
---|
| 244 | |
---|
| 245 | ///This function just returns an \ref IdentityMap class. |
---|
| 246 | ///\relates IdentityMap |
---|
| 247 | template<typename T> |
---|
[1705] | 248 | inline IdentityMap<T> identityMap() { |
---|
| 249 | return IdentityMap<T>(); |
---|
[1675] | 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
[1547] | 253 | ///Convert the \c Value of a map to another type. |
---|
[1178] | 254 | |
---|
| 255 | ///This \ref concept::ReadMap "read only map" |
---|
| 256 | ///converts the \c Value of a maps to type \c T. |
---|
[1547] | 257 | ///Its \c Key is inherited from \c M. |
---|
[1705] | 258 | template <typename M, typename T> |
---|
| 259 | class ConvertMap : public MapBase<typename M::Key, T> { |
---|
| 260 | const M& m; |
---|
[1178] | 261 | public: |
---|
[1705] | 262 | typedef MapBase<typename M::Key, T> Parent; |
---|
[1675] | 263 | typedef typename Parent::Key Key; |
---|
| 264 | typedef typename Parent::Value Value; |
---|
[1178] | 265 | |
---|
| 266 | ///Constructor |
---|
| 267 | |
---|
| 268 | ///Constructor |
---|
[1536] | 269 | ///\param _m is the underlying map |
---|
[1178] | 270 | ConvertMap(const M &_m) : m(_m) {}; |
---|
[1346] | 271 | |
---|
| 272 | /// \brief The subscript operator. |
---|
| 273 | /// |
---|
| 274 | /// The subscript operator. |
---|
[1536] | 275 | /// \param k The key |
---|
[1346] | 276 | /// \return The target of the edge |
---|
[1675] | 277 | Value operator[](const Key& k) const {return m[k];} |
---|
[1178] | 278 | }; |
---|
| 279 | |
---|
| 280 | ///Returns an \ref ConvertMap class |
---|
| 281 | |
---|
| 282 | ///This function just returns an \ref ConvertMap class. |
---|
| 283 | ///\relates ConvertMap |
---|
| 284 | ///\todo The order of the template parameters are changed. |
---|
[1675] | 285 | template<typename T, typename M> |
---|
[1705] | 286 | inline ConvertMap<M, T> convertMap(const M &m) { |
---|
| 287 | return ConvertMap<M, T>(m); |
---|
[1178] | 288 | } |
---|
[1041] | 289 | |
---|
| 290 | ///Sum of two maps |
---|
| 291 | |
---|
| 292 | ///This \ref concept::ReadMap "read only map" returns the sum of the two |
---|
| 293 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 294 | ///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
---|
| 295 | |
---|
[1705] | 296 | template<typename M1, typename M2> |
---|
| 297 | class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 298 | const M1& m1; |
---|
| 299 | const M2& m2; |
---|
[1420] | 300 | |
---|
[1041] | 301 | public: |
---|
[1705] | 302 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
[1675] | 303 | typedef typename Parent::Key Key; |
---|
| 304 | typedef typename Parent::Value Value; |
---|
[1041] | 305 | |
---|
| 306 | ///Constructor |
---|
| 307 | AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 308 | Value operator[](Key k) const {return m1[k]+m2[k];} |
---|
[1041] | 309 | }; |
---|
| 310 | |
---|
| 311 | ///Returns an \ref AddMap class |
---|
| 312 | |
---|
| 313 | ///This function just returns an \ref AddMap class. |
---|
| 314 | ///\todo How to call these type of functions? |
---|
| 315 | /// |
---|
| 316 | ///\relates AddMap |
---|
| 317 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
[1675] | 318 | template<typename M1, typename M2> |
---|
[1705] | 319 | inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) { |
---|
| 320 | return AddMap<M1, M2>(m1,m2); |
---|
[1041] | 321 | } |
---|
| 322 | |
---|
[1547] | 323 | ///Shift a map with a constant. |
---|
[1070] | 324 | |
---|
| 325 | ///This \ref concept::ReadMap "read only map" returns the sum of the |
---|
| 326 | ///given map and a constant value. |
---|
| 327 | ///Its \c Key and \c Value is inherited from \c M. |
---|
| 328 | /// |
---|
| 329 | ///Actually, |
---|
| 330 | ///\code |
---|
| 331 | /// ShiftMap<X> sh(x,v); |
---|
| 332 | ///\endcode |
---|
[1547] | 333 | ///is equivalent with |
---|
[1070] | 334 | ///\code |
---|
| 335 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
| 336 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
| 337 | ///\endcode |
---|
[1705] | 338 | template<typename M, typename C = typename M::Value> |
---|
| 339 | class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 340 | const M& m; |
---|
[1691] | 341 | C v; |
---|
[1070] | 342 | public: |
---|
[1705] | 343 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
[1675] | 344 | typedef typename Parent::Key Key; |
---|
| 345 | typedef typename Parent::Value Value; |
---|
[1070] | 346 | |
---|
| 347 | ///Constructor |
---|
| 348 | |
---|
| 349 | ///Constructor |
---|
| 350 | ///\param _m is the undelying map |
---|
| 351 | ///\param _v is the shift value |
---|
[1691] | 352 | ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
---|
| 353 | Value operator[](Key k) const {return m[k] + v;} |
---|
[1070] | 354 | }; |
---|
[2032] | 355 | |
---|
| 356 | ///Shift a map with a constant. |
---|
| 357 | |
---|
| 358 | ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the |
---|
| 359 | ///given map and a constant value. It makes also possible to write the map. |
---|
| 360 | ///Its \c Key and \c Value is inherited from \c M. |
---|
| 361 | /// |
---|
| 362 | ///Actually, |
---|
| 363 | ///\code |
---|
| 364 | /// ShiftMap<X> sh(x,v); |
---|
| 365 | ///\endcode |
---|
| 366 | ///is equivalent with |
---|
| 367 | ///\code |
---|
| 368 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
| 369 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
| 370 | ///\endcode |
---|
| 371 | template<typename M, typename C = typename M::Value> |
---|
| 372 | class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 373 | M& m; |
---|
| 374 | C v; |
---|
| 375 | public: |
---|
| 376 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 377 | typedef typename Parent::Key Key; |
---|
| 378 | typedef typename Parent::Value Value; |
---|
| 379 | |
---|
| 380 | ///Constructor |
---|
| 381 | |
---|
| 382 | ///Constructor |
---|
| 383 | ///\param _m is the undelying map |
---|
| 384 | ///\param _v is the shift value |
---|
[2080] | 385 | ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
---|
[2032] | 386 | Value operator[](Key k) const {return m[k] + v;} |
---|
| 387 | void set(Key k, const Value& c) { m.set(k, c - v); } |
---|
| 388 | }; |
---|
[1070] | 389 | |
---|
| 390 | ///Returns an \ref ShiftMap class |
---|
| 391 | |
---|
| 392 | ///This function just returns an \ref ShiftMap class. |
---|
| 393 | ///\relates ShiftMap |
---|
| 394 | ///\todo A better name is required. |
---|
[1691] | 395 | template<typename M, typename C> |
---|
[1705] | 396 | inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { |
---|
| 397 | return ShiftMap<M, C>(m,v); |
---|
[1070] | 398 | } |
---|
| 399 | |
---|
[2032] | 400 | template<typename M, typename C> |
---|
| 401 | inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { |
---|
| 402 | return ShiftWriteMap<M, C>(m,v); |
---|
| 403 | } |
---|
| 404 | |
---|
[1041] | 405 | ///Difference of two maps |
---|
| 406 | |
---|
| 407 | ///This \ref concept::ReadMap "read only map" returns the difference |
---|
[1547] | 408 | ///of the values of the two |
---|
[1041] | 409 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 410 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
| 411 | |
---|
[1705] | 412 | template<typename M1, typename M2> |
---|
| 413 | class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 414 | const M1& m1; |
---|
| 415 | const M2& m2; |
---|
[1041] | 416 | public: |
---|
[1705] | 417 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
[1675] | 418 | typedef typename Parent::Key Key; |
---|
| 419 | typedef typename Parent::Value Value; |
---|
[1041] | 420 | |
---|
| 421 | ///Constructor |
---|
| 422 | SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 423 | Value operator[](Key k) const {return m1[k]-m2[k];} |
---|
[1041] | 424 | }; |
---|
| 425 | |
---|
| 426 | ///Returns a \ref SubMap class |
---|
| 427 | |
---|
| 428 | ///This function just returns a \ref SubMap class. |
---|
| 429 | /// |
---|
| 430 | ///\relates SubMap |
---|
[1675] | 431 | template<typename M1, typename M2> |
---|
[1705] | 432 | inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
---|
| 433 | return SubMap<M1, M2>(m1, m2); |
---|
[1041] | 434 | } |
---|
| 435 | |
---|
| 436 | ///Product of two maps |
---|
| 437 | |
---|
| 438 | ///This \ref concept::ReadMap "read only map" returns the product of the |
---|
[1547] | 439 | ///values of the two |
---|
[1041] | 440 | ///given |
---|
| 441 | ///maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 442 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
| 443 | |
---|
[1705] | 444 | template<typename M1, typename M2> |
---|
| 445 | class MulMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 446 | const M1& m1; |
---|
| 447 | const M2& m2; |
---|
[1041] | 448 | public: |
---|
[1705] | 449 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
[1675] | 450 | typedef typename Parent::Key Key; |
---|
| 451 | typedef typename Parent::Value Value; |
---|
[1041] | 452 | |
---|
| 453 | ///Constructor |
---|
| 454 | MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 455 | Value operator[](Key k) const {return m1[k]*m2[k];} |
---|
[1041] | 456 | }; |
---|
| 457 | |
---|
| 458 | ///Returns a \ref MulMap class |
---|
| 459 | |
---|
| 460 | ///This function just returns a \ref MulMap class. |
---|
| 461 | ///\relates MulMap |
---|
[1675] | 462 | template<typename M1, typename M2> |
---|
[1705] | 463 | inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
---|
| 464 | return MulMap<M1, M2>(m1,m2); |
---|
[1041] | 465 | } |
---|
| 466 | |
---|
[1547] | 467 | ///Scales a maps with a constant. |
---|
[1070] | 468 | |
---|
| 469 | ///This \ref concept::ReadMap "read only map" returns the value of the |
---|
[1691] | 470 | ///given map multiplied from the left side with a constant value. |
---|
[1070] | 471 | ///Its \c Key and \c Value is inherited from \c M. |
---|
| 472 | /// |
---|
| 473 | ///Actually, |
---|
| 474 | ///\code |
---|
| 475 | /// ScaleMap<X> sc(x,v); |
---|
| 476 | ///\endcode |
---|
[1547] | 477 | ///is equivalent with |
---|
[1070] | 478 | ///\code |
---|
| 479 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
| 480 | /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
---|
| 481 | ///\endcode |
---|
[1705] | 482 | template<typename M, typename C = typename M::Value> |
---|
| 483 | class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 484 | const M& m; |
---|
[1691] | 485 | C v; |
---|
[1070] | 486 | public: |
---|
[1705] | 487 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
[1675] | 488 | typedef typename Parent::Key Key; |
---|
| 489 | typedef typename Parent::Value Value; |
---|
[1070] | 490 | |
---|
| 491 | ///Constructor |
---|
| 492 | |
---|
| 493 | ///Constructor |
---|
| 494 | ///\param _m is the undelying map |
---|
| 495 | ///\param _v is the scaling value |
---|
[1691] | 496 | ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
---|
| 497 | Value operator[](Key k) const {return v * m[k];} |
---|
[1070] | 498 | }; |
---|
[2032] | 499 | |
---|
| 500 | ///Scales a maps with a constant. |
---|
| 501 | |
---|
| 502 | ///This \ref concept::ReadWriteMap "read-write map" returns the value of the |
---|
| 503 | ///given map multiplied from the left side with a constant value. It can |
---|
| 504 | ///be used as write map also if the given multiplier is not zero. |
---|
| 505 | ///Its \c Key and \c Value is inherited from \c M. |
---|
| 506 | template<typename M, typename C = typename M::Value> |
---|
| 507 | class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 508 | M& m; |
---|
| 509 | C v; |
---|
| 510 | public: |
---|
| 511 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 512 | typedef typename Parent::Key Key; |
---|
| 513 | typedef typename Parent::Value Value; |
---|
| 514 | |
---|
| 515 | ///Constructor |
---|
| 516 | |
---|
| 517 | ///Constructor |
---|
| 518 | ///\param _m is the undelying map |
---|
| 519 | ///\param _v is the scaling value |
---|
| 520 | ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
---|
| 521 | Value operator[](Key k) const {return v * m[k];} |
---|
| 522 | void set(Key k, const Value& c) { m.set(k, c / v);} |
---|
| 523 | }; |
---|
[1070] | 524 | |
---|
| 525 | ///Returns an \ref ScaleMap class |
---|
| 526 | |
---|
| 527 | ///This function just returns an \ref ScaleMap class. |
---|
| 528 | ///\relates ScaleMap |
---|
| 529 | ///\todo A better name is required. |
---|
[1691] | 530 | template<typename M, typename C> |
---|
[1705] | 531 | inline ScaleMap<M, C> scaleMap(const M &m,const C &v) { |
---|
| 532 | return ScaleMap<M, C>(m,v); |
---|
[1070] | 533 | } |
---|
| 534 | |
---|
[2032] | 535 | template<typename M, typename C> |
---|
| 536 | inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) { |
---|
| 537 | return ScaleWriteMap<M, C>(m,v); |
---|
| 538 | } |
---|
| 539 | |
---|
[1041] | 540 | ///Quotient of two maps |
---|
| 541 | |
---|
| 542 | ///This \ref concept::ReadMap "read only map" returns the quotient of the |
---|
[1547] | 543 | ///values of the two |
---|
[1041] | 544 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
| 545 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
| 546 | |
---|
[1705] | 547 | template<typename M1, typename M2> |
---|
| 548 | class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 549 | const M1& m1; |
---|
| 550 | const M2& m2; |
---|
[1041] | 551 | public: |
---|
[1705] | 552 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
[1675] | 553 | typedef typename Parent::Key Key; |
---|
| 554 | typedef typename Parent::Value Value; |
---|
[1041] | 555 | |
---|
| 556 | ///Constructor |
---|
| 557 | DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1044] | 558 | Value operator[](Key k) const {return m1[k]/m2[k];} |
---|
[1041] | 559 | }; |
---|
| 560 | |
---|
| 561 | ///Returns a \ref DivMap class |
---|
| 562 | |
---|
| 563 | ///This function just returns a \ref DivMap class. |
---|
| 564 | ///\relates DivMap |
---|
[1675] | 565 | template<typename M1, typename M2> |
---|
[1705] | 566 | inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) { |
---|
| 567 | return DivMap<M1, M2>(m1,m2); |
---|
[1041] | 568 | } |
---|
| 569 | |
---|
| 570 | ///Composition of two maps |
---|
| 571 | |
---|
| 572 | ///This \ref concept::ReadMap "read only map" returns the composition of |
---|
| 573 | ///two |
---|
| 574 | ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is |
---|
| 575 | ///of \c M2, |
---|
| 576 | ///then for |
---|
| 577 | ///\code |
---|
[1675] | 578 | /// ComposeMap<M1, M2> cm(m1,m2); |
---|
[1041] | 579 | ///\endcode |
---|
[1044] | 580 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt> |
---|
[1041] | 581 | /// |
---|
| 582 | ///Its \c Key is inherited from \c M2 and its \c Value is from |
---|
| 583 | ///\c M1. |
---|
| 584 | ///The \c M2::Value must be convertible to \c M1::Key. |
---|
| 585 | ///\todo Check the requirements. |
---|
| 586 | |
---|
[1705] | 587 | template <typename M1, typename M2> |
---|
| 588 | class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
---|
| 589 | const M1& m1; |
---|
| 590 | const M2& m2; |
---|
[1041] | 591 | public: |
---|
[1705] | 592 | typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
---|
[1675] | 593 | typedef typename Parent::Key Key; |
---|
| 594 | typedef typename Parent::Value Value; |
---|
[1041] | 595 | |
---|
| 596 | ///Constructor |
---|
| 597 | ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1725] | 598 | |
---|
| 599 | typename MapTraits<M1>::ConstReturnValue |
---|
| 600 | operator[](Key k) const {return m1[m2[k]];} |
---|
[1041] | 601 | }; |
---|
| 602 | ///Returns a \ref ComposeMap class |
---|
| 603 | |
---|
| 604 | ///This function just returns a \ref ComposeMap class. |
---|
[1219] | 605 | /// |
---|
[1041] | 606 | ///\relates ComposeMap |
---|
[1675] | 607 | template <typename M1, typename M2> |
---|
[1705] | 608 | inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) { |
---|
| 609 | return ComposeMap<M1, M2>(m1,m2); |
---|
[1041] | 610 | } |
---|
[1219] | 611 | |
---|
[1547] | 612 | ///Combines of two maps using an STL (binary) functor. |
---|
[1219] | 613 | |
---|
[1547] | 614 | ///Combines of two maps using an STL (binary) functor. |
---|
[1219] | 615 | /// |
---|
| 616 | /// |
---|
[1547] | 617 | ///This \ref concept::ReadMap "read only map" takes two maps and a |
---|
[1219] | 618 | ///binary functor and returns the composition of |
---|
[1547] | 619 | ///the two |
---|
[1219] | 620 | ///given maps unsing the functor. |
---|
| 621 | ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
---|
| 622 | ///and \c f is of \c F, |
---|
| 623 | ///then for |
---|
| 624 | ///\code |
---|
[1675] | 625 | /// CombineMap<M1, M2,F,V> cm(m1,m2,f); |
---|
[1219] | 626 | ///\endcode |
---|
| 627 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
---|
| 628 | /// |
---|
| 629 | ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
---|
| 630 | ///The \c M2::Value and \c M1::Value must be convertible to the corresponding |
---|
| 631 | ///input parameter of \c F and the return type of \c F must be convertible |
---|
| 632 | ///to \c V. |
---|
| 633 | ///\todo Check the requirements. |
---|
| 634 | |
---|
[1675] | 635 | template<typename M1, typename M2, typename F, |
---|
| 636 | typename V = typename F::result_type, |
---|
| 637 | typename NC = False> |
---|
[1705] | 638 | class CombineMap : public MapBase<typename M1::Key, V> { |
---|
| 639 | const M1& m1; |
---|
| 640 | const M2& m2; |
---|
[1420] | 641 | F f; |
---|
[1219] | 642 | public: |
---|
[1705] | 643 | typedef MapBase<typename M1::Key, V> Parent; |
---|
[1675] | 644 | typedef typename Parent::Key Key; |
---|
| 645 | typedef typename Parent::Value Value; |
---|
[1219] | 646 | |
---|
| 647 | ///Constructor |
---|
| 648 | CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) |
---|
| 649 | : m1(_m1), m2(_m2), f(_f) {}; |
---|
| 650 | Value operator[](Key k) const {return f(m1[k],m2[k]);} |
---|
| 651 | }; |
---|
| 652 | |
---|
| 653 | ///Returns a \ref CombineMap class |
---|
| 654 | |
---|
| 655 | ///This function just returns a \ref CombineMap class. |
---|
| 656 | /// |
---|
| 657 | ///Only the first template parameter (the value type) must be given. |
---|
| 658 | /// |
---|
| 659 | ///For example if \c m1 and \c m2 are both \c double valued maps, then |
---|
| 660 | ///\code |
---|
| 661 | ///combineMap<double>(m1,m2,std::plus<double>) |
---|
| 662 | ///\endcode |
---|
| 663 | ///is equivalent with |
---|
| 664 | ///\code |
---|
| 665 | ///addMap(m1,m2) |
---|
| 666 | ///\endcode |
---|
| 667 | /// |
---|
| 668 | ///\relates CombineMap |
---|
[1675] | 669 | template<typename M1, typename M2, typename F, typename V> |
---|
[1705] | 670 | inline CombineMap<M1, M2, F, V> |
---|
[1675] | 671 | combineMap(const M1& m1,const M2& m2, const F& f) { |
---|
[1705] | 672 | return CombineMap<M1, M2, F, V>(m1,m2,f); |
---|
[1675] | 673 | } |
---|
| 674 | |
---|
| 675 | template<typename M1, typename M2, typename F> |
---|
[1705] | 676 | inline CombineMap<M1, M2, F, typename F::result_type> |
---|
[1675] | 677 | combineMap(const M1& m1, const M2& m2, const F& f) { |
---|
| 678 | return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | template<typename M1, typename M2, typename K1, typename K2, typename V> |
---|
[1705] | 682 | inline CombineMap<M1, M2, V (*)(K1, K2), V> |
---|
[1675] | 683 | combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
---|
| 684 | return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
---|
[1219] | 685 | } |
---|
[1041] | 686 | |
---|
| 687 | ///Negative value of a map |
---|
| 688 | |
---|
| 689 | ///This \ref concept::ReadMap "read only map" returns the negative |
---|
| 690 | ///value of the |
---|
| 691 | ///value returned by the |
---|
| 692 | ///given map. Its \c Key and \c Value will be inherited from \c M. |
---|
| 693 | ///The unary \c - operator must be defined for \c Value, of course. |
---|
| 694 | |
---|
[1705] | 695 | template<typename M> |
---|
| 696 | class NegMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 697 | const M& m; |
---|
[1041] | 698 | public: |
---|
[1705] | 699 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
[1675] | 700 | typedef typename Parent::Key Key; |
---|
| 701 | typedef typename Parent::Value Value; |
---|
[1041] | 702 | |
---|
| 703 | ///Constructor |
---|
| 704 | NegMap(const M &_m) : m(_m) {}; |
---|
[1044] | 705 | Value operator[](Key k) const {return -m[k];} |
---|
[1041] | 706 | }; |
---|
| 707 | |
---|
[2032] | 708 | ///Negative value of a map |
---|
| 709 | |
---|
| 710 | ///This \ref concept::ReadWriteMap "read-write map" returns the negative |
---|
| 711 | ///value of the value returned by the |
---|
| 712 | ///given map. Its \c Key and \c Value will be inherited from \c M. |
---|
| 713 | ///The unary \c - operator must be defined for \c Value, of course. |
---|
| 714 | |
---|
| 715 | template<typename M> |
---|
| 716 | class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 717 | M& m; |
---|
| 718 | public: |
---|
| 719 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 720 | typedef typename Parent::Key Key; |
---|
| 721 | typedef typename Parent::Value Value; |
---|
| 722 | |
---|
| 723 | ///Constructor |
---|
| 724 | NegWriteMap(M &_m) : m(_m) {}; |
---|
| 725 | Value operator[](Key k) const {return -m[k];} |
---|
| 726 | void set(Key k, const Value& v) { m.set(k, -v); } |
---|
| 727 | }; |
---|
| 728 | |
---|
[1041] | 729 | ///Returns a \ref NegMap class |
---|
| 730 | |
---|
| 731 | ///This function just returns a \ref NegMap class. |
---|
| 732 | ///\relates NegMap |
---|
[1675] | 733 | template <typename M> |
---|
[1705] | 734 | inline NegMap<M> negMap(const M &m) { |
---|
| 735 | return NegMap<M>(m); |
---|
[1041] | 736 | } |
---|
| 737 | |
---|
[2032] | 738 | template <typename M> |
---|
| 739 | inline NegWriteMap<M> negMap(M &m) { |
---|
| 740 | return NegWriteMap<M>(m); |
---|
| 741 | } |
---|
[1041] | 742 | |
---|
| 743 | ///Absolute value of a map |
---|
| 744 | |
---|
| 745 | ///This \ref concept::ReadMap "read only map" returns the absolute value |
---|
| 746 | ///of the |
---|
| 747 | ///value returned by the |
---|
[1044] | 748 | ///given map. Its \c Key and \c Value will be inherited |
---|
| 749 | ///from <tt>M</tt>. <tt>Value</tt> |
---|
| 750 | ///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
---|
| 751 | ///operator must be defined for it, of course. |
---|
| 752 | /// |
---|
| 753 | ///\bug We need a unified way to handle the situation below: |
---|
| 754 | ///\code |
---|
| 755 | /// struct _UnConvertible {}; |
---|
| 756 | /// template<class A> inline A t_abs(A a) {return _UnConvertible();} |
---|
| 757 | /// template<> inline int t_abs<>(int n) {return abs(n);} |
---|
| 758 | /// template<> inline long int t_abs<>(long int n) {return labs(n);} |
---|
| 759 | /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} |
---|
| 760 | /// template<> inline float t_abs<>(float n) {return fabsf(n);} |
---|
| 761 | /// template<> inline double t_abs<>(double n) {return fabs(n);} |
---|
| 762 | /// template<> inline long double t_abs<>(long double n) {return fabsl(n);} |
---|
| 763 | ///\endcode |
---|
| 764 | |
---|
[1041] | 765 | |
---|
[1705] | 766 | template<typename M> |
---|
| 767 | class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 768 | const M& m; |
---|
[1041] | 769 | public: |
---|
[1705] | 770 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
[1675] | 771 | typedef typename Parent::Key Key; |
---|
| 772 | typedef typename Parent::Value Value; |
---|
[1041] | 773 | |
---|
| 774 | ///Constructor |
---|
| 775 | AbsMap(const M &_m) : m(_m) {}; |
---|
[1675] | 776 | Value operator[](Key k) const { |
---|
| 777 | Value tmp = m[k]; |
---|
| 778 | return tmp >= 0 ? tmp : -tmp; |
---|
| 779 | } |
---|
| 780 | |
---|
[1041] | 781 | }; |
---|
| 782 | |
---|
| 783 | ///Returns a \ref AbsMap class |
---|
| 784 | |
---|
| 785 | ///This function just returns a \ref AbsMap class. |
---|
| 786 | ///\relates AbsMap |
---|
[1675] | 787 | template<typename M> |
---|
[1705] | 788 | inline AbsMap<M> absMap(const M &m) { |
---|
| 789 | return AbsMap<M>(m); |
---|
[1041] | 790 | } |
---|
| 791 | |
---|
[1402] | 792 | ///Converts an STL style functor to a map |
---|
[1076] | 793 | |
---|
| 794 | ///This \ref concept::ReadMap "read only map" returns the value |
---|
| 795 | ///of a |
---|
| 796 | ///given map. |
---|
| 797 | /// |
---|
| 798 | ///Template parameters \c K and \c V will become its |
---|
| 799 | ///\c Key and \c Value. They must be given explicitely |
---|
| 800 | ///because a functor does not provide such typedefs. |
---|
| 801 | /// |
---|
| 802 | ///Parameter \c F is the type of the used functor. |
---|
| 803 | |
---|
| 804 | |
---|
[1675] | 805 | template<typename F, |
---|
| 806 | typename K = typename F::argument_type, |
---|
| 807 | typename V = typename F::result_type, |
---|
| 808 | typename NC = False> |
---|
[1705] | 809 | class FunctorMap : public MapBase<K, V> { |
---|
[1679] | 810 | F f; |
---|
[1076] | 811 | public: |
---|
[1705] | 812 | typedef MapBase<K, V> Parent; |
---|
[1675] | 813 | typedef typename Parent::Key Key; |
---|
| 814 | typedef typename Parent::Value Value; |
---|
[1076] | 815 | |
---|
| 816 | ///Constructor |
---|
[1679] | 817 | FunctorMap(const F &_f) : f(_f) {} |
---|
| 818 | |
---|
| 819 | Value operator[](Key k) const { return f(k);} |
---|
[1076] | 820 | }; |
---|
| 821 | |
---|
| 822 | ///Returns a \ref FunctorMap class |
---|
| 823 | |
---|
| 824 | ///This function just returns a \ref FunctorMap class. |
---|
| 825 | /// |
---|
| 826 | ///The third template parameter isn't necessary to be given. |
---|
| 827 | ///\relates FunctorMap |
---|
[1675] | 828 | template<typename K, typename V, typename F> inline |
---|
[1705] | 829 | FunctorMap<F, K, V> functorMap(const F &f) { |
---|
| 830 | return FunctorMap<F, K, V>(f); |
---|
[1076] | 831 | } |
---|
| 832 | |
---|
[1675] | 833 | template <typename F> inline |
---|
[1705] | 834 | FunctorMap<F, typename F::argument_type, typename F::result_type> |
---|
[1675] | 835 | functorMap(const F &f) { |
---|
[1679] | 836 | return FunctorMap<F, typename F::argument_type, |
---|
[1705] | 837 | typename F::result_type>(f); |
---|
[1675] | 838 | } |
---|
| 839 | |
---|
| 840 | template <typename K, typename V> inline |
---|
[1705] | 841 | FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) { |
---|
| 842 | return FunctorMap<V (*)(K), K, V>(f); |
---|
[1675] | 843 | } |
---|
| 844 | |
---|
| 845 | |
---|
[1219] | 846 | ///Converts a map to an STL style (unary) functor |
---|
[1076] | 847 | |
---|
[1219] | 848 | ///This class Converts a map to an STL style (unary) functor. |
---|
[1076] | 849 | ///that is it provides an <tt>operator()</tt> to read its values. |
---|
| 850 | /// |
---|
[1223] | 851 | ///For the sake of convenience it also works as |
---|
[1537] | 852 | ///a ususal \ref concept::ReadMap "readable map", |
---|
| 853 | ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
---|
[1076] | 854 | |
---|
[1705] | 855 | template <typename M> |
---|
| 856 | class MapFunctor : public MapBase<typename M::Key, typename M::Value> { |
---|
| 857 | const M& m; |
---|
[1076] | 858 | public: |
---|
[1705] | 859 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
[1675] | 860 | typedef typename Parent::Key Key; |
---|
| 861 | typedef typename Parent::Value Value; |
---|
[1420] | 862 | |
---|
[1456] | 863 | ///\e |
---|
[1223] | 864 | typedef typename M::Key argument_type; |
---|
[1456] | 865 | ///\e |
---|
[1223] | 866 | typedef typename M::Value result_type; |
---|
[1076] | 867 | |
---|
| 868 | ///Constructor |
---|
| 869 | MapFunctor(const M &_m) : m(_m) {}; |
---|
| 870 | ///Returns a value of the map |
---|
| 871 | Value operator()(Key k) const {return m[k];} |
---|
| 872 | ///\e |
---|
| 873 | Value operator[](Key k) const {return m[k];} |
---|
| 874 | }; |
---|
| 875 | |
---|
| 876 | ///Returns a \ref MapFunctor class |
---|
| 877 | |
---|
| 878 | ///This function just returns a \ref MapFunctor class. |
---|
| 879 | ///\relates MapFunctor |
---|
[1675] | 880 | template<typename M> |
---|
[1705] | 881 | inline MapFunctor<M> mapFunctor(const M &m) { |
---|
| 882 | return MapFunctor<M>(m); |
---|
[1076] | 883 | } |
---|
| 884 | |
---|
[1547] | 885 | ///Applies all map setting operations to two maps |
---|
[1219] | 886 | |
---|
[2032] | 887 | ///This map has two \ref concept::ReadMap "readable map" |
---|
| 888 | ///parameters and each read request will be passed just to the |
---|
| 889 | ///first map. This class is the just readable map type of the ForkWriteMap. |
---|
[1219] | 890 | /// |
---|
| 891 | ///The \c Key and \c Value will be inherited from \c M1. |
---|
| 892 | ///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
---|
| 893 | |
---|
[1705] | 894 | template<typename M1, typename M2> |
---|
| 895 | class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 896 | const M1& m1; |
---|
| 897 | const M2& m2; |
---|
[1219] | 898 | public: |
---|
[1705] | 899 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
[1675] | 900 | typedef typename Parent::Key Key; |
---|
| 901 | typedef typename Parent::Value Value; |
---|
[1219] | 902 | |
---|
| 903 | ///Constructor |
---|
[2032] | 904 | ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
[1219] | 905 | Value operator[](Key k) const {return m1[k];} |
---|
[2032] | 906 | }; |
---|
| 907 | |
---|
| 908 | |
---|
| 909 | ///Applies all map setting operations to two maps |
---|
| 910 | |
---|
| 911 | ///This map has two \ref concept::WriteMap "writable map" |
---|
| 912 | ///parameters and each write request will be passed to both of them. |
---|
| 913 | ///If \c M1 is also \ref concept::ReadMap "readable", |
---|
| 914 | ///then the read operations will return the |
---|
| 915 | ///corresponding values of \c M1. |
---|
| 916 | /// |
---|
| 917 | ///The \c Key and \c Value will be inherited from \c M1. |
---|
| 918 | ///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
---|
| 919 | |
---|
| 920 | template<typename M1, typename M2> |
---|
| 921 | class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 922 | M1& m1; |
---|
| 923 | M2& m2; |
---|
| 924 | public: |
---|
| 925 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
| 926 | typedef typename Parent::Key Key; |
---|
| 927 | typedef typename Parent::Value Value; |
---|
| 928 | |
---|
| 929 | ///Constructor |
---|
| 930 | ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
| 931 | Value operator[](Key k) const {return m1[k];} |
---|
| 932 | void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} |
---|
[1219] | 933 | }; |
---|
| 934 | |
---|
| 935 | ///Returns an \ref ForkMap class |
---|
| 936 | |
---|
| 937 | ///This function just returns an \ref ForkMap class. |
---|
| 938 | ///\todo How to call these type of functions? |
---|
| 939 | /// |
---|
| 940 | ///\relates ForkMap |
---|
| 941 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
[1675] | 942 | template <typename M1, typename M2> |
---|
[2032] | 943 | inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { |
---|
[1705] | 944 | return ForkMap<M1, M2>(m1,m2); |
---|
[1219] | 945 | } |
---|
| 946 | |
---|
[2032] | 947 | template <typename M1, typename M2> |
---|
| 948 | inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { |
---|
| 949 | return ForkWriteMap<M1, M2>(m1,m2); |
---|
| 950 | } |
---|
| 951 | |
---|
[1456] | 952 | |
---|
| 953 | |
---|
| 954 | /* ************* BOOL MAPS ******************* */ |
---|
| 955 | |
---|
| 956 | ///Logical 'not' of a map |
---|
| 957 | |
---|
| 958 | ///This bool \ref concept::ReadMap "read only map" returns the |
---|
| 959 | ///logical negation of |
---|
| 960 | ///value returned by the |
---|
| 961 | ///given map. Its \c Key and will be inherited from \c M, |
---|
| 962 | ///its Value is <tt>bool</tt>. |
---|
| 963 | |
---|
[1705] | 964 | template <typename M> |
---|
| 965 | class NotMap : public MapBase<typename M::Key, bool> { |
---|
| 966 | const M& m; |
---|
[1456] | 967 | public: |
---|
[1705] | 968 | typedef MapBase<typename M::Key, bool> Parent; |
---|
[1675] | 969 | typedef typename Parent::Key Key; |
---|
| 970 | typedef typename Parent::Value Value; |
---|
[1456] | 971 | |
---|
[1778] | 972 | /// Constructor |
---|
[1456] | 973 | NotMap(const M &_m) : m(_m) {}; |
---|
| 974 | Value operator[](Key k) const {return !m[k];} |
---|
| 975 | }; |
---|
[2032] | 976 | |
---|
| 977 | ///Logical 'not' of a map with writing possibility |
---|
| 978 | |
---|
| 979 | ///This bool \ref concept::ReadWriteMap "read-write map" returns the |
---|
| 980 | ///logical negation of value returned by the given map. It is setted |
---|
| 981 | ///then the negation of the value be setted to the original map. |
---|
| 982 | ///Its \c Key and will be inherited from \c M, |
---|
| 983 | ///its Value is <tt>bool</tt>. |
---|
| 984 | template <typename M> |
---|
| 985 | class NotWriteMap : public MapBase<typename M::Key, bool> { |
---|
| 986 | M& m; |
---|
| 987 | public: |
---|
| 988 | typedef MapBase<typename M::Key, bool> Parent; |
---|
| 989 | typedef typename Parent::Key Key; |
---|
| 990 | typedef typename Parent::Value Value; |
---|
| 991 | |
---|
| 992 | /// Constructor |
---|
| 993 | NotWriteMap(M &_m) : m(_m) {}; |
---|
| 994 | Value operator[](Key k) const {return !m[k];} |
---|
| 995 | void set(Key k, bool v) { m.set(k, !v); } |
---|
| 996 | }; |
---|
[1456] | 997 | |
---|
| 998 | ///Returns a \ref NotMap class |
---|
| 999 | |
---|
| 1000 | ///This function just returns a \ref NotMap class. |
---|
| 1001 | ///\relates NotMap |
---|
[1675] | 1002 | template <typename M> |
---|
[1705] | 1003 | inline NotMap<M> notMap(const M &m) { |
---|
| 1004 | return NotMap<M>(m); |
---|
[1456] | 1005 | } |
---|
| 1006 | |
---|
[2032] | 1007 | template <typename M> |
---|
| 1008 | inline NotWriteMap<M> notMap(M &m) { |
---|
| 1009 | return NotWriteMap<M>(m); |
---|
| 1010 | } |
---|
| 1011 | |
---|
[2091] | 1012 | namespace _maps_bits { |
---|
| 1013 | template <typename Value> |
---|
| 1014 | struct Identity { |
---|
| 1015 | typedef Value argument_type; |
---|
| 1016 | typedef Value result_type; |
---|
| 1017 | Value operator()(const Value& val) { |
---|
| 1018 | return val; |
---|
| 1019 | } |
---|
| 1020 | }; |
---|
| 1021 | } |
---|
| 1022 | |
---|
| 1023 | |
---|
[1808] | 1024 | /// \brief Writable bool map for store each true assigned elements. |
---|
[1778] | 1025 | /// |
---|
[1808] | 1026 | /// Writable bool map for store each true assigned elements. It will |
---|
[1778] | 1027 | /// copies all the true setted keys to the given iterator. |
---|
| 1028 | /// |
---|
[2091] | 1029 | /// \note The container of the iterator should contain space |
---|
| 1030 | /// for each element. |
---|
| 1031 | /// |
---|
| 1032 | /// The next example shows how can you write the nodes directly |
---|
| 1033 | /// to the standard output. |
---|
| 1034 | ///\code |
---|
| 1035 | /// typedef IdMap<UGraph, UEdge> UEdgeIdMap; |
---|
| 1036 | /// UEdgeIdMap uedgeId(ugraph); |
---|
| 1037 | /// |
---|
| 1038 | /// typedef MapFunctor<UEdgeIdMap> UEdgeIdFunctor; |
---|
| 1039 | /// UEdgeIdFunctor uedgeIdFunctor(uedgeId); |
---|
| 1040 | /// |
---|
| 1041 | /// StoreBoolMap<ostream_iterator<int>, UEdgeIdFunctor> |
---|
| 1042 | /// writerMap(ostream_iterator<int>(cout, " "), uedgeIdFunctor); |
---|
| 1043 | /// |
---|
| 1044 | /// prim(ugraph, cost, writerMap); |
---|
| 1045 | ///\endcode |
---|
| 1046 | template <typename _Iterator, |
---|
| 1047 | typename _Functor = |
---|
| 1048 | _maps_bits::Identity<typename std::iterator_traits<_Iterator>::value_type> > |
---|
[1778] | 1049 | class StoreBoolMap { |
---|
| 1050 | public: |
---|
| 1051 | typedef _Iterator Iterator; |
---|
| 1052 | |
---|
[2091] | 1053 | typedef typename _Functor::argument_type Key; |
---|
[1778] | 1054 | typedef bool Value; |
---|
| 1055 | |
---|
[2091] | 1056 | typedef _Functor Functor; |
---|
| 1057 | |
---|
[1778] | 1058 | /// Constructor |
---|
[2091] | 1059 | StoreBoolMap(Iterator it, const Functor& functor = Functor()) |
---|
| 1060 | : _begin(it), _end(it), _functor(functor) {} |
---|
[1778] | 1061 | |
---|
| 1062 | /// Gives back the given first setted iterator. |
---|
| 1063 | Iterator begin() const { |
---|
| 1064 | return _begin; |
---|
| 1065 | } |
---|
| 1066 | |
---|
| 1067 | /// Gives back the iterator after the last setted. |
---|
| 1068 | Iterator end() const { |
---|
| 1069 | return _end; |
---|
| 1070 | } |
---|
| 1071 | |
---|
| 1072 | /// Setter function of the map |
---|
| 1073 | void set(const Key& key, Value value) { |
---|
| 1074 | if (value) { |
---|
[2091] | 1075 | *_end++ = _functor(key); |
---|
[1778] | 1076 | } |
---|
| 1077 | } |
---|
| 1078 | |
---|
| 1079 | private: |
---|
| 1080 | Iterator _begin, _end; |
---|
[2091] | 1081 | Functor _functor; |
---|
[1778] | 1082 | }; |
---|
| 1083 | |
---|
[1808] | 1084 | /// \brief Writable bool map for store each true assigned elements in |
---|
[1778] | 1085 | /// a back insertable container. |
---|
| 1086 | /// |
---|
[1808] | 1087 | /// Writable bool map for store each true assigned elements in a back |
---|
[1778] | 1088 | /// insertable container. It will push back all the true setted keys into |
---|
[2091] | 1089 | /// the container. It can be used to retrieve the items into a standard |
---|
| 1090 | /// container. The next example shows how can you store the undirected |
---|
| 1091 | /// edges in a vector with prim algorithm. |
---|
| 1092 | /// |
---|
| 1093 | ///\code |
---|
| 1094 | /// vector<UEdge> span_tree_uedges; |
---|
| 1095 | /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges); |
---|
| 1096 | /// prim(ugraph, cost, inserter_map); |
---|
| 1097 | ///\endcode |
---|
| 1098 | template <typename Container, |
---|
| 1099 | typename Functor = |
---|
| 1100 | _maps_bits::Identity<typename Container::value_type> > |
---|
[1778] | 1101 | class BackInserterBoolMap { |
---|
| 1102 | public: |
---|
| 1103 | typedef typename Container::value_type Key; |
---|
| 1104 | typedef bool Value; |
---|
| 1105 | |
---|
| 1106 | /// Constructor |
---|
[2091] | 1107 | BackInserterBoolMap(Container& _container, |
---|
| 1108 | const Functor& _functor = Functor()) |
---|
| 1109 | : container(_container), functor(_functor) {} |
---|
[1778] | 1110 | |
---|
| 1111 | /// Setter function of the map |
---|
| 1112 | void set(const Key& key, Value value) { |
---|
| 1113 | if (value) { |
---|
[2091] | 1114 | container.push_back(functor(key)); |
---|
[1778] | 1115 | } |
---|
| 1116 | } |
---|
| 1117 | |
---|
| 1118 | private: |
---|
[2091] | 1119 | Container& container; |
---|
| 1120 | Functor functor; |
---|
[1778] | 1121 | }; |
---|
| 1122 | |
---|
[1808] | 1123 | /// \brief Writable bool map for store each true assigned elements in |
---|
[1778] | 1124 | /// a front insertable container. |
---|
| 1125 | /// |
---|
[1808] | 1126 | /// Writable bool map for store each true assigned elements in a front |
---|
[1778] | 1127 | /// insertable container. It will push front all the true setted keys into |
---|
[2091] | 1128 | /// the container. For example see the BackInserterBoolMap. |
---|
| 1129 | template <typename Container, |
---|
| 1130 | typename Functor = |
---|
| 1131 | _maps_bits::Identity<typename Container::value_type> > |
---|
[1778] | 1132 | class FrontInserterBoolMap { |
---|
| 1133 | public: |
---|
| 1134 | typedef typename Container::value_type Key; |
---|
| 1135 | typedef bool Value; |
---|
| 1136 | |
---|
| 1137 | /// Constructor |
---|
[2091] | 1138 | FrontInserterBoolMap(Container& _container, |
---|
| 1139 | const Functor& _functor = Functor()) |
---|
| 1140 | : container(_container), functor(_functor) {} |
---|
[1778] | 1141 | |
---|
| 1142 | /// Setter function of the map |
---|
| 1143 | void set(const Key& key, Value value) { |
---|
| 1144 | if (value) { |
---|
| 1145 | container.push_front(key); |
---|
| 1146 | } |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | private: |
---|
| 1150 | Container& container; |
---|
[2091] | 1151 | Functor functor; |
---|
[1778] | 1152 | }; |
---|
| 1153 | |
---|
[1808] | 1154 | /// \brief Writable bool map for store each true assigned elements in |
---|
[1778] | 1155 | /// an insertable container. |
---|
| 1156 | /// |
---|
[1808] | 1157 | /// Writable bool map for store each true assigned elements in an |
---|
[1778] | 1158 | /// insertable container. It will insert all the true setted keys into |
---|
[2091] | 1159 | /// the container. If you want to store the cut edges of the strongly |
---|
| 1160 | /// connected components in a set you can use the next code: |
---|
| 1161 | /// |
---|
| 1162 | ///\code |
---|
| 1163 | /// set<Edge> cut_edges; |
---|
| 1164 | /// InserterBoolMap<set<Edge> > inserter_map(cut_edges); |
---|
| 1165 | /// stronglyConnectedCutEdges(graph, cost, inserter_map); |
---|
| 1166 | ///\endcode |
---|
| 1167 | template <typename Container, |
---|
| 1168 | typename Functor = |
---|
| 1169 | _maps_bits::Identity<typename Container::value_type> > |
---|
[1778] | 1170 | class InserterBoolMap { |
---|
| 1171 | public: |
---|
| 1172 | typedef typename Container::value_type Key; |
---|
| 1173 | typedef bool Value; |
---|
| 1174 | |
---|
| 1175 | /// Constructor |
---|
[2091] | 1176 | InserterBoolMap(Container& _container, typename Container::iterator _it, |
---|
| 1177 | const Functor& _functor = Functor()) |
---|
| 1178 | : container(_container), it(_it), functor(_functor) {} |
---|
| 1179 | |
---|
| 1180 | /// Constructor |
---|
| 1181 | InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
---|
| 1182 | : container(_container), it(_container.end()), functor(_functor) {} |
---|
[1778] | 1183 | |
---|
| 1184 | /// Setter function of the map |
---|
| 1185 | void set(const Key& key, Value value) { |
---|
| 1186 | if (value) { |
---|
[2091] | 1187 | it = container.insert(it, key); |
---|
| 1188 | ++it; |
---|
[1778] | 1189 | } |
---|
| 1190 | } |
---|
| 1191 | |
---|
| 1192 | private: |
---|
[2091] | 1193 | Container& container; |
---|
| 1194 | typename Container::iterator it; |
---|
| 1195 | Functor functor; |
---|
[1778] | 1196 | }; |
---|
| 1197 | |
---|
| 1198 | /// \brief Fill the true setted elements with a given value. |
---|
| 1199 | /// |
---|
[1808] | 1200 | /// Writable bool map for fill the true setted elements with a given value. |
---|
[1778] | 1201 | /// The value can be setted |
---|
| 1202 | /// the container. |
---|
[2091] | 1203 | /// |
---|
| 1204 | /// The next code finds the connected components of the undirected graph |
---|
| 1205 | /// and stores it in the \c comp map: |
---|
| 1206 | ///\code |
---|
| 1207 | /// typedef UGraph::NodeMap<int> ComponentMap; |
---|
| 1208 | /// ComponentMap comp(ugraph); |
---|
| 1209 | /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap; |
---|
| 1210 | /// ComponentFillerMap filler(comp, 0); |
---|
| 1211 | /// |
---|
| 1212 | /// Dfs<UGraph>::DefProcessedMap<ComponentFillerMap>::Create dfs(ugraph); |
---|
| 1213 | /// dfs.processedMap(filler); |
---|
| 1214 | /// dfs.init(); |
---|
| 1215 | /// for (NodeIt it(ugraph); it != INVALID; ++it) { |
---|
| 1216 | /// if (!dfs.reached(it)) { |
---|
| 1217 | /// dfs.addSource(it); |
---|
| 1218 | /// dfs.start(); |
---|
| 1219 | /// ++filler.fillValue(); |
---|
| 1220 | /// } |
---|
| 1221 | /// } |
---|
| 1222 | ///\endcode |
---|
| 1223 | |
---|
[1778] | 1224 | template <typename Map> |
---|
| 1225 | class FillBoolMap { |
---|
| 1226 | public: |
---|
| 1227 | typedef typename Map::Key Key; |
---|
| 1228 | typedef bool Value; |
---|
| 1229 | |
---|
| 1230 | /// Constructor |
---|
| 1231 | FillBoolMap(Map& _map, const typename Map::Value& _fill) |
---|
| 1232 | : map(_map), fill(_fill) {} |
---|
| 1233 | |
---|
| 1234 | /// Constructor |
---|
| 1235 | FillBoolMap(Map& _map) |
---|
| 1236 | : map(_map), fill() {} |
---|
| 1237 | |
---|
| 1238 | /// Gives back the current fill value |
---|
[2091] | 1239 | const typename Map::Value& fillValue() const { |
---|
| 1240 | return fill; |
---|
| 1241 | } |
---|
| 1242 | |
---|
| 1243 | /// Gives back the current fill value |
---|
| 1244 | typename Map::Value& fillValue() { |
---|
[1778] | 1245 | return fill; |
---|
| 1246 | } |
---|
| 1247 | |
---|
| 1248 | /// Sets the current fill value |
---|
| 1249 | void fillValue(const typename Map::Value& _fill) { |
---|
| 1250 | fill = _fill; |
---|
| 1251 | } |
---|
| 1252 | |
---|
| 1253 | /// Setter function of the map |
---|
| 1254 | void set(const Key& key, Value value) { |
---|
| 1255 | if (value) { |
---|
| 1256 | map.set(key, fill); |
---|
| 1257 | } |
---|
| 1258 | } |
---|
| 1259 | |
---|
| 1260 | private: |
---|
| 1261 | Map& map; |
---|
| 1262 | typename Map::Value fill; |
---|
| 1263 | }; |
---|
| 1264 | |
---|
| 1265 | |
---|
[1808] | 1266 | /// \brief Writable bool map which stores for each true assigned elements |
---|
[1778] | 1267 | /// the setting order number. |
---|
| 1268 | /// |
---|
[1808] | 1269 | /// Writable bool map which stores for each true assigned elements |
---|
[2091] | 1270 | /// the setting order number. It make easy to calculate the leaving |
---|
| 1271 | /// order of the nodes in the \ref dfs "Dfs" algorithm. |
---|
| 1272 | /// |
---|
| 1273 | ///\code |
---|
| 1274 | /// typedef Graph::NodeMap<int> OrderMap; |
---|
| 1275 | /// OrderMap order(graph); |
---|
| 1276 | /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
---|
| 1277 | /// OrderSetterMap setter(order); |
---|
| 1278 | /// Dfs<Graph>::DefProcessedMap<OrderSetterMap>::Create dfs(graph); |
---|
| 1279 | /// dfs.processedMap(setter); |
---|
| 1280 | /// dfs.init(); |
---|
| 1281 | /// for (NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1282 | /// if (!dfs.reached(it)) { |
---|
| 1283 | /// dfs.addSource(it); |
---|
| 1284 | /// dfs.start(); |
---|
| 1285 | /// } |
---|
| 1286 | /// } |
---|
| 1287 | ///\endcode |
---|
| 1288 | /// |
---|
| 1289 | /// The discovering order can be stored a little harder because the |
---|
| 1290 | /// ReachedMap should be readable in the dfs algorithm but the setting |
---|
| 1291 | /// order map is not readable. Now we should use the fork map: |
---|
| 1292 | /// |
---|
| 1293 | ///\code |
---|
| 1294 | /// typedef Graph::NodeMap<int> OrderMap; |
---|
| 1295 | /// OrderMap order(graph); |
---|
| 1296 | /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
---|
| 1297 | /// OrderSetterMap setter(order); |
---|
| 1298 | /// typedef Graph::NodeMap<bool> StoreMap; |
---|
| 1299 | /// StoreMap store(graph); |
---|
| 1300 | /// |
---|
| 1301 | /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap; |
---|
| 1302 | /// ReachedMap reached(store, setter); |
---|
| 1303 | /// |
---|
| 1304 | /// Dfs<Graph>::DefReachedMap<ReachedMap>::Create dfs(graph); |
---|
| 1305 | /// dfs.reachedMap(reached); |
---|
| 1306 | /// dfs.init(); |
---|
| 1307 | /// for (NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1308 | /// if (!dfs.reached(it)) { |
---|
| 1309 | /// dfs.addSource(it); |
---|
| 1310 | /// dfs.start(); |
---|
| 1311 | /// } |
---|
| 1312 | /// } |
---|
| 1313 | ///\endcode |
---|
[1778] | 1314 | template <typename Map> |
---|
| 1315 | class SettingOrderBoolMap { |
---|
| 1316 | public: |
---|
| 1317 | typedef typename Map::Key Key; |
---|
| 1318 | typedef bool Value; |
---|
| 1319 | |
---|
| 1320 | /// Constructor |
---|
| 1321 | SettingOrderBoolMap(Map& _map) |
---|
| 1322 | : map(_map), counter(0) {} |
---|
| 1323 | |
---|
| 1324 | /// Number of setted keys. |
---|
| 1325 | int num() const { |
---|
| 1326 | return counter; |
---|
| 1327 | } |
---|
| 1328 | |
---|
| 1329 | /// Setter function of the map |
---|
| 1330 | void set(const Key& key, Value value) { |
---|
| 1331 | if (value) { |
---|
| 1332 | map.set(key, counter++); |
---|
| 1333 | } |
---|
| 1334 | } |
---|
| 1335 | |
---|
| 1336 | private: |
---|
| 1337 | Map& map; |
---|
| 1338 | int counter; |
---|
| 1339 | }; |
---|
| 1340 | |
---|
[1041] | 1341 | /// @} |
---|
[286] | 1342 | } |
---|
[1041] | 1343 | |
---|
[921] | 1344 | #endif // LEMON_MAPS_H |
---|