[25] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[39] | 5 | * Copyright (C) 2003-2008 |
---|
[25] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 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 | |
---|
| 19 | #ifndef LEMON_MAPS_H |
---|
| 20 | #define LEMON_MAPS_H |
---|
| 21 | |
---|
| 22 | #include <iterator> |
---|
| 23 | #include <functional> |
---|
| 24 | #include <vector> |
---|
| 25 | |
---|
| 26 | #include <lemon/bits/utility.h> |
---|
[80] | 27 | #include <lemon/bits/traits.h> |
---|
[25] | 28 | |
---|
| 29 | ///\file |
---|
| 30 | ///\ingroup maps |
---|
| 31 | ///\brief Miscellaneous property maps |
---|
[80] | 32 | |
---|
[25] | 33 | #include <map> |
---|
| 34 | |
---|
| 35 | namespace lemon { |
---|
| 36 | |
---|
| 37 | /// \addtogroup maps |
---|
| 38 | /// @{ |
---|
| 39 | |
---|
| 40 | /// Base class of maps. |
---|
| 41 | |
---|
[80] | 42 | /// Base class of maps. It provides the necessary type definitions |
---|
| 43 | /// required by the map %concepts. |
---|
| 44 | template<typename K, typename V> |
---|
[25] | 45 | class MapBase { |
---|
| 46 | public: |
---|
[80] | 47 | /// \biref The key type of the map. |
---|
[25] | 48 | typedef K Key; |
---|
[80] | 49 | /// \brief The value type of the map. |
---|
| 50 | /// (The type of objects associated with the keys). |
---|
| 51 | typedef V Value; |
---|
[25] | 52 | }; |
---|
| 53 | |
---|
[80] | 54 | |
---|
[25] | 55 | /// Null map. (a.k.a. DoNothingMap) |
---|
| 56 | |
---|
[29] | 57 | /// This map can be used if you have to provide a map only for |
---|
[80] | 58 | /// its type definitions, or if you have to provide a writable map, |
---|
| 59 | /// but data written to it is not required (i.e. it will be sent to |
---|
[29] | 60 | /// <tt>/dev/null</tt>). |
---|
[80] | 61 | /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
---|
| 62 | /// |
---|
| 63 | /// \sa ConstMap |
---|
| 64 | template<typename K, typename V> |
---|
| 65 | class NullMap : public MapBase<K, V> { |
---|
[25] | 66 | public: |
---|
[80] | 67 | typedef MapBase<K, V> Parent; |
---|
[25] | 68 | typedef typename Parent::Key Key; |
---|
| 69 | typedef typename Parent::Value Value; |
---|
[80] | 70 | |
---|
[25] | 71 | /// Gives back a default constructed element. |
---|
[80] | 72 | Value operator[](const Key&) const { return Value(); } |
---|
[25] | 73 | /// Absorbs the value. |
---|
[80] | 74 | void set(const Key&, const Value&) {} |
---|
[25] | 75 | }; |
---|
| 76 | |
---|
[80] | 77 | /// Returns a \ref NullMap class |
---|
[29] | 78 | |
---|
[80] | 79 | /// This function just returns a \ref NullMap class. |
---|
| 80 | /// \relates NullMap |
---|
| 81 | template <typename K, typename V> |
---|
[25] | 82 | NullMap<K, V> nullMap() { |
---|
| 83 | return NullMap<K, V>(); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | /// Constant map. |
---|
| 88 | |
---|
[80] | 89 | /// This is a \ref concepts::ReadMap "readable" map which assigns a |
---|
[47] | 90 | /// specified value to each key. |
---|
[80] | 91 | /// |
---|
| 92 | /// In other aspects it is equivalent to \ref NullMap. |
---|
| 93 | /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
---|
| 94 | /// concept, but it absorbs the data written to it. |
---|
| 95 | /// |
---|
| 96 | /// The simplest way of using this map is through the constMap() |
---|
| 97 | /// function. |
---|
| 98 | /// |
---|
| 99 | /// \sa NullMap |
---|
| 100 | /// \sa IdentityMap |
---|
| 101 | template<typename K, typename V> |
---|
| 102 | class ConstMap : public MapBase<K, V> { |
---|
[25] | 103 | private: |
---|
[80] | 104 | V _value; |
---|
[25] | 105 | public: |
---|
[80] | 106 | typedef MapBase<K, V> Parent; |
---|
[25] | 107 | typedef typename Parent::Key Key; |
---|
| 108 | typedef typename Parent::Value Value; |
---|
| 109 | |
---|
| 110 | /// Default constructor |
---|
| 111 | |
---|
[29] | 112 | /// Default constructor. |
---|
[80] | 113 | /// The value of the map will be default constructed. |
---|
[25] | 114 | ConstMap() {} |
---|
[80] | 115 | |
---|
[29] | 116 | /// Constructor with specified initial value |
---|
[25] | 117 | |
---|
[29] | 118 | /// Constructor with specified initial value. |
---|
[80] | 119 | /// \param v is the initial value of the map. |
---|
| 120 | ConstMap(const Value &v) : _value(v) {} |
---|
[25] | 121 | |
---|
[80] | 122 | /// Gives back the specified value. |
---|
| 123 | Value operator[](const Key&) const { return _value; } |
---|
[25] | 124 | |
---|
[80] | 125 | /// Absorbs the value. |
---|
| 126 | void set(const Key&, const Value&) {} |
---|
| 127 | |
---|
| 128 | /// Sets the value that is assigned to each key. |
---|
| 129 | void setAll(const Value &v) { |
---|
| 130 | _value = v; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | template<typename V1> |
---|
| 134 | ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {} |
---|
[25] | 135 | }; |
---|
| 136 | |
---|
[80] | 137 | /// Returns a \ref ConstMap class |
---|
[25] | 138 | |
---|
[80] | 139 | /// This function just returns a \ref ConstMap class. |
---|
| 140 | /// \relates ConstMap |
---|
| 141 | template<typename K, typename V> |
---|
[25] | 142 | inline ConstMap<K, V> constMap(const V &v) { |
---|
| 143 | return ConstMap<K, V>(v); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | template<typename T, T v> |
---|
[80] | 148 | struct Const {}; |
---|
[25] | 149 | |
---|
| 150 | /// Constant map with inlined constant value. |
---|
| 151 | |
---|
[80] | 152 | /// This is a \ref concepts::ReadMap "readable" map which assigns a |
---|
[47] | 153 | /// specified value to each key. |
---|
[80] | 154 | /// |
---|
| 155 | /// In other aspects it is equivalent to \ref NullMap. |
---|
| 156 | /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
---|
| 157 | /// concept, but it absorbs the data written to it. |
---|
| 158 | /// |
---|
| 159 | /// The simplest way of using this map is through the constMap() |
---|
| 160 | /// function. |
---|
| 161 | /// |
---|
| 162 | /// \sa NullMap |
---|
| 163 | /// \sa IdentityMap |
---|
[25] | 164 | template<typename K, typename V, V v> |
---|
| 165 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
---|
| 166 | public: |
---|
| 167 | typedef MapBase<K, V> Parent; |
---|
| 168 | typedef typename Parent::Key Key; |
---|
| 169 | typedef typename Parent::Value Value; |
---|
| 170 | |
---|
[80] | 171 | /// Constructor. |
---|
| 172 | ConstMap() {} |
---|
| 173 | |
---|
| 174 | /// Gives back the specified value. |
---|
| 175 | Value operator[](const Key&) const { return v; } |
---|
| 176 | |
---|
| 177 | /// Absorbs the value. |
---|
| 178 | void set(const Key&, const Value&) {} |
---|
[25] | 179 | }; |
---|
| 180 | |
---|
[80] | 181 | /// Returns a \ref ConstMap class with inlined constant value |
---|
[25] | 182 | |
---|
[80] | 183 | /// This function just returns a \ref ConstMap class with inlined |
---|
| 184 | /// constant value. |
---|
| 185 | /// \relates ConstMap |
---|
| 186 | template<typename K, typename V, V v> |
---|
[25] | 187 | inline ConstMap<K, Const<V, v> > constMap() { |
---|
| 188 | return ConstMap<K, Const<V, v> >(); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
[80] | 192 | /// \brief Identity map. |
---|
| 193 | /// |
---|
| 194 | /// This map gives back the given key as value without any |
---|
| 195 | /// modification. |
---|
| 196 | /// |
---|
| 197 | /// \sa ConstMap |
---|
| 198 | template <typename T> |
---|
| 199 | class IdentityMap : public MapBase<T, T> { |
---|
| 200 | public: |
---|
| 201 | typedef MapBase<T, T> Parent; |
---|
| 202 | typedef typename Parent::Key Key; |
---|
| 203 | typedef typename Parent::Value Value; |
---|
| 204 | |
---|
| 205 | /// Gives back the given value without any modification. |
---|
| 206 | const T& operator[](const T& t) const { |
---|
| 207 | return t; |
---|
| 208 | } |
---|
| 209 | }; |
---|
| 210 | |
---|
| 211 | /// Returns an \ref IdentityMap class |
---|
| 212 | |
---|
| 213 | /// This function just returns an \ref IdentityMap class. |
---|
| 214 | /// \relates IdentityMap |
---|
| 215 | template<typename T> |
---|
| 216 | inline IdentityMap<T> identityMap() { |
---|
| 217 | return IdentityMap<T>(); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /// \brief Map for storing values for integer keys from the range |
---|
| 222 | /// <tt>[0..size-1]</tt>. |
---|
| 223 | /// |
---|
| 224 | /// This map is essentially a wrapper for \c std::vector. It assigns |
---|
| 225 | /// values to integer keys from the range <tt>[0..size-1]</tt>. |
---|
| 226 | /// It can be used with some data structures, for example |
---|
| 227 | /// \ref UnionFind, \ref BinHeap, when the used items are small |
---|
| 228 | /// integers. This map conforms the \ref concepts::ReferenceMap |
---|
| 229 | /// "ReferenceMap" concept. |
---|
| 230 | /// |
---|
| 231 | /// The simplest way of using this map is through the rangeMap() |
---|
| 232 | /// function. |
---|
| 233 | template <typename V> |
---|
| 234 | class RangeMap : public MapBase<int, V> { |
---|
| 235 | template <typename V1> |
---|
| 236 | friend class RangeMap; |
---|
| 237 | private: |
---|
| 238 | |
---|
| 239 | typedef std::vector<V> Vector; |
---|
| 240 | Vector _vector; |
---|
| 241 | |
---|
[25] | 242 | public: |
---|
| 243 | |
---|
[80] | 244 | typedef MapBase<int, V> Parent; |
---|
| 245 | /// Key type |
---|
[45] | 246 | typedef typename Parent::Key Key; |
---|
[80] | 247 | /// Value type |
---|
[45] | 248 | typedef typename Parent::Value Value; |
---|
[80] | 249 | /// Reference type |
---|
| 250 | typedef typename Vector::reference Reference; |
---|
| 251 | /// Const reference type |
---|
| 252 | typedef typename Vector::const_reference ConstReference; |
---|
| 253 | |
---|
| 254 | typedef True ReferenceMapTag; |
---|
| 255 | |
---|
| 256 | public: |
---|
| 257 | |
---|
| 258 | /// Constructor with specified default value. |
---|
| 259 | RangeMap(int size = 0, const Value &value = Value()) |
---|
| 260 | : _vector(size, value) {} |
---|
| 261 | |
---|
| 262 | /// Constructs the map from an appropriate \c std::vector. |
---|
| 263 | template <typename V1> |
---|
| 264 | RangeMap(const std::vector<V1>& vector) |
---|
| 265 | : _vector(vector.begin(), vector.end()) {} |
---|
| 266 | |
---|
| 267 | /// Constructs the map from another \ref RangeMap. |
---|
| 268 | template <typename V1> |
---|
| 269 | RangeMap(const RangeMap<V1> &c) |
---|
| 270 | : _vector(c._vector.begin(), c._vector.end()) {} |
---|
| 271 | |
---|
| 272 | /// Returns the size of the map. |
---|
| 273 | int size() { |
---|
| 274 | return _vector.size(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /// Resizes the map. |
---|
| 278 | |
---|
| 279 | /// Resizes the underlying \c std::vector container, so changes the |
---|
| 280 | /// keyset of the map. |
---|
| 281 | /// \param size The new size of the map. The new keyset will be the |
---|
| 282 | /// range <tt>[0..size-1]</tt>. |
---|
| 283 | /// \param value The default value to assign to the new keys. |
---|
| 284 | void resize(int size, const Value &value = Value()) { |
---|
| 285 | _vector.resize(size, value); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | private: |
---|
| 289 | |
---|
| 290 | RangeMap& operator=(const RangeMap&); |
---|
| 291 | |
---|
| 292 | public: |
---|
| 293 | |
---|
| 294 | ///\e |
---|
| 295 | Reference operator[](const Key &k) { |
---|
| 296 | return _vector[k]; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | ///\e |
---|
| 300 | ConstReference operator[](const Key &k) const { |
---|
| 301 | return _vector[k]; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | ///\e |
---|
| 305 | void set(const Key &k, const Value &v) { |
---|
| 306 | _vector[k] = v; |
---|
| 307 | } |
---|
| 308 | }; |
---|
| 309 | |
---|
| 310 | /// Returns a \ref RangeMap class |
---|
| 311 | |
---|
| 312 | /// This function just returns a \ref RangeMap class. |
---|
| 313 | /// \relates RangeMap |
---|
| 314 | template<typename V> |
---|
| 315 | inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) { |
---|
| 316 | return RangeMap<V>(size, value); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /// \brief Returns a \ref RangeMap class created from an appropriate |
---|
| 320 | /// \c std::vector |
---|
| 321 | |
---|
| 322 | /// This function just returns a \ref RangeMap class created from an |
---|
| 323 | /// appropriate \c std::vector. |
---|
| 324 | /// \relates RangeMap |
---|
| 325 | template<typename V> |
---|
| 326 | inline RangeMap<V> rangeMap(const std::vector<V> &vector) { |
---|
| 327 | return RangeMap<V>(vector); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | /// Map type based on \c std::map |
---|
| 332 | |
---|
| 333 | /// This map is essentially a wrapper for \c std::map with addition |
---|
| 334 | /// that you can specify a default value for the keys that are not |
---|
| 335 | /// stored actually. This value can be different from the default |
---|
| 336 | /// contructed value (i.e. \c %Value()). |
---|
| 337 | /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap" |
---|
| 338 | /// concept. |
---|
| 339 | /// |
---|
| 340 | /// This map is useful if a default value should be assigned to most of |
---|
| 341 | /// the keys and different values should be assigned only to a few |
---|
| 342 | /// keys (i.e. the map is "sparse"). |
---|
| 343 | /// The name of this type also refers to this important usage. |
---|
| 344 | /// |
---|
| 345 | /// Apart form that this map can be used in many other cases since it |
---|
| 346 | /// is based on \c std::map, which is a general associative container. |
---|
| 347 | /// However keep in mind that it is usually not as efficient as other |
---|
| 348 | /// maps. |
---|
| 349 | /// |
---|
| 350 | /// The simplest way of using this map is through the sparseMap() |
---|
| 351 | /// function. |
---|
| 352 | template <typename K, typename V, typename Compare = std::less<K> > |
---|
| 353 | class SparseMap : public MapBase<K, V> { |
---|
| 354 | template <typename K1, typename V1, typename C1> |
---|
| 355 | friend class SparseMap; |
---|
| 356 | public: |
---|
| 357 | |
---|
| 358 | typedef MapBase<K, V> Parent; |
---|
| 359 | /// Key type |
---|
| 360 | typedef typename Parent::Key Key; |
---|
| 361 | /// Value type |
---|
| 362 | typedef typename Parent::Value Value; |
---|
| 363 | /// Reference type |
---|
| 364 | typedef Value& Reference; |
---|
| 365 | /// Const reference type |
---|
| 366 | typedef const Value& ConstReference; |
---|
[25] | 367 | |
---|
[45] | 368 | typedef True ReferenceMapTag; |
---|
| 369 | |
---|
[25] | 370 | private: |
---|
[80] | 371 | |
---|
| 372 | typedef std::map<K, V, Compare> Map; |
---|
| 373 | Map _map; |
---|
[25] | 374 | Value _value; |
---|
| 375 | |
---|
| 376 | public: |
---|
| 377 | |
---|
[80] | 378 | /// \brief Constructor with specified default value. |
---|
| 379 | SparseMap(const Value &value = Value()) : _value(value) {} |
---|
| 380 | /// \brief Constructs the map from an appropriate \c std::map, and |
---|
[47] | 381 | /// explicitly specifies a default value. |
---|
[80] | 382 | template <typename V1, typename Comp1> |
---|
| 383 | SparseMap(const std::map<Key, V1, Comp1> &map, |
---|
| 384 | const Value &value = Value()) |
---|
[25] | 385 | : _map(map.begin(), map.end()), _value(value) {} |
---|
[80] | 386 | |
---|
| 387 | /// \brief Constructs the map from another \ref SparseMap. |
---|
| 388 | template<typename V1, typename Comp1> |
---|
| 389 | SparseMap(const SparseMap<Key, V1, Comp1> &c) |
---|
[25] | 390 | : _map(c._map.begin(), c._map.end()), _value(c._value) {} |
---|
| 391 | |
---|
| 392 | private: |
---|
| 393 | |
---|
[80] | 394 | SparseMap& operator=(const SparseMap&); |
---|
[25] | 395 | |
---|
| 396 | public: |
---|
| 397 | |
---|
| 398 | ///\e |
---|
| 399 | Reference operator[](const Key &k) { |
---|
| 400 | typename Map::iterator it = _map.lower_bound(k); |
---|
| 401 | if (it != _map.end() && !_map.key_comp()(k, it->first)) |
---|
| 402 | return it->second; |
---|
| 403 | else |
---|
| 404 | return _map.insert(it, std::make_pair(k, _value))->second; |
---|
| 405 | } |
---|
| 406 | |
---|
[80] | 407 | ///\e |
---|
[25] | 408 | ConstReference operator[](const Key &k) const { |
---|
| 409 | typename Map::const_iterator it = _map.find(k); |
---|
| 410 | if (it != _map.end()) |
---|
| 411 | return it->second; |
---|
| 412 | else |
---|
| 413 | return _value; |
---|
| 414 | } |
---|
| 415 | |
---|
[80] | 416 | ///\e |
---|
| 417 | void set(const Key &k, const Value &v) { |
---|
[25] | 418 | typename Map::iterator it = _map.lower_bound(k); |
---|
| 419 | if (it != _map.end() && !_map.key_comp()(k, it->first)) |
---|
[80] | 420 | it->second = v; |
---|
[25] | 421 | else |
---|
[80] | 422 | _map.insert(it, std::make_pair(k, v)); |
---|
[25] | 423 | } |
---|
| 424 | |
---|
[80] | 425 | ///\e |
---|
| 426 | void setAll(const Value &v) { |
---|
| 427 | _value = v; |
---|
[25] | 428 | _map.clear(); |
---|
[80] | 429 | } |
---|
| 430 | }; |
---|
[25] | 431 | |
---|
[80] | 432 | /// Returns a \ref SparseMap class |
---|
[45] | 433 | |
---|
[80] | 434 | /// This function just returns a \ref SparseMap class with specified |
---|
| 435 | /// default value. |
---|
| 436 | /// \relates SparseMap |
---|
| 437 | template<typename K, typename V, typename Compare> |
---|
| 438 | inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) { |
---|
| 439 | return SparseMap<K, V, Compare>(value); |
---|
[54] | 440 | } |
---|
[45] | 441 | |
---|
[80] | 442 | template<typename K, typename V> |
---|
| 443 | inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) { |
---|
| 444 | return SparseMap<K, V, std::less<K> >(value); |
---|
[45] | 445 | } |
---|
[25] | 446 | |
---|
[80] | 447 | /// \brief Returns a \ref SparseMap class created from an appropriate |
---|
| 448 | /// \c std::map |
---|
[25] | 449 | |
---|
[80] | 450 | /// This function just returns a \ref SparseMap class created from an |
---|
| 451 | /// appropriate \c std::map. |
---|
| 452 | /// \relates SparseMap |
---|
| 453 | template<typename K, typename V, typename Compare> |
---|
| 454 | inline SparseMap<K, V, Compare> |
---|
| 455 | sparseMap(const std::map<K, V, Compare> &map, const V& value = V()) |
---|
| 456 | { |
---|
| 457 | return SparseMap<K, V, Compare>(map, value); |
---|
[45] | 458 | } |
---|
[25] | 459 | |
---|
| 460 | /// @} |
---|
| 461 | |
---|
| 462 | /// \addtogroup map_adaptors |
---|
| 463 | /// @{ |
---|
| 464 | |
---|
[80] | 465 | /// Composition of two maps |
---|
| 466 | |
---|
| 467 | /// This \ref concepts::ReadMap "read only map" returns the |
---|
| 468 | /// composition of two given maps. That is to say, if \c m1 is of |
---|
| 469 | /// type \c M1 and \c m2 is of \c M2, then for |
---|
| 470 | /// \code |
---|
| 471 | /// ComposeMap<M1, M2> cm(m1,m2); |
---|
| 472 | /// \endcode |
---|
| 473 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
---|
[25] | 474 | /// |
---|
[80] | 475 | /// The \c Key type of the map is inherited from \c M2 and the |
---|
| 476 | /// \c Value type is from \c M1. |
---|
| 477 | /// \c M2::Value must be convertible to \c M1::Key. |
---|
| 478 | /// |
---|
| 479 | /// The simplest way of using this map is through the composeMap() |
---|
| 480 | /// function. |
---|
| 481 | /// |
---|
| 482 | /// \sa CombineMap |
---|
| 483 | /// |
---|
| 484 | /// \todo Check the requirements. |
---|
| 485 | template <typename M1, typename M2> |
---|
| 486 | class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
---|
| 487 | const M1 &_m1; |
---|
| 488 | const M2 &_m2; |
---|
[25] | 489 | public: |
---|
[80] | 490 | typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
---|
[25] | 491 | typedef typename Parent::Key Key; |
---|
| 492 | typedef typename Parent::Value Value; |
---|
| 493 | |
---|
[80] | 494 | /// Constructor |
---|
| 495 | ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 496 | |
---|
[25] | 497 | /// \e |
---|
[80] | 498 | typename MapTraits<M1>::ConstReturnValue |
---|
| 499 | operator[](const Key &k) const { return _m1[_m2[k]]; } |
---|
[25] | 500 | }; |
---|
| 501 | |
---|
[80] | 502 | /// Returns a \ref ComposeMap class |
---|
[25] | 503 | |
---|
[80] | 504 | /// This function just returns a \ref ComposeMap class. |
---|
| 505 | /// |
---|
| 506 | /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is |
---|
| 507 | /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt> |
---|
| 508 | /// will be equal to <tt>m1[m2[x]]</tt>. |
---|
| 509 | /// |
---|
| 510 | /// \relates ComposeMap |
---|
| 511 | template <typename M1, typename M2> |
---|
| 512 | inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) { |
---|
| 513 | return ComposeMap<M1, M2>(m1, m2); |
---|
[25] | 514 | } |
---|
| 515 | |
---|
[80] | 516 | |
---|
| 517 | /// Combination of two maps using an STL (binary) functor. |
---|
| 518 | |
---|
| 519 | /// This \ref concepts::ReadMap "read only map" takes two maps and a |
---|
| 520 | /// binary functor and returns the combination of the two given maps |
---|
| 521 | /// using the functor. |
---|
| 522 | /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 |
---|
| 523 | /// and \c f is of \c F, then for |
---|
| 524 | /// \code |
---|
| 525 | /// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
---|
| 526 | /// \endcode |
---|
| 527 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>. |
---|
[26] | 528 | /// |
---|
[80] | 529 | /// The \c Key type of the map is inherited from \c M1 (\c M1::Key |
---|
| 530 | /// must be convertible to \c M2::Key) and the \c Value type is \c V. |
---|
| 531 | /// \c M2::Value and \c M1::Value must be convertible to the |
---|
| 532 | /// corresponding input parameter of \c F and the return type of \c F |
---|
| 533 | /// must be convertible to \c V. |
---|
| 534 | /// |
---|
| 535 | /// The simplest way of using this map is through the combineMap() |
---|
| 536 | /// function. |
---|
| 537 | /// |
---|
| 538 | /// \sa ComposeMap |
---|
| 539 | /// |
---|
| 540 | /// \todo Check the requirements. |
---|
| 541 | template<typename M1, typename M2, typename F, |
---|
| 542 | typename V = typename F::result_type> |
---|
| 543 | class CombineMap : public MapBase<typename M1::Key, V> { |
---|
| 544 | const M1 &_m1; |
---|
| 545 | const M2 &_m2; |
---|
| 546 | F _f; |
---|
[25] | 547 | public: |
---|
[80] | 548 | typedef MapBase<typename M1::Key, V> Parent; |
---|
[25] | 549 | typedef typename Parent::Key Key; |
---|
| 550 | typedef typename Parent::Value Value; |
---|
| 551 | |
---|
[80] | 552 | /// Constructor |
---|
| 553 | CombineMap(const M1 &m1, const M2 &m2, const F &f = F()) |
---|
| 554 | : _m1(m1), _m2(m2), _f(f) {} |
---|
| 555 | /// \e |
---|
| 556 | Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); } |
---|
| 557 | }; |
---|
[25] | 558 | |
---|
[80] | 559 | /// Returns a \ref CombineMap class |
---|
[25] | 560 | |
---|
[80] | 561 | /// This function just returns a \ref CombineMap class. |
---|
| 562 | /// |
---|
| 563 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 564 | /// values, then |
---|
| 565 | /// \code |
---|
| 566 | /// combineMap(m1,m2,std::plus<double>()) |
---|
| 567 | /// \endcode |
---|
| 568 | /// is equivalent to |
---|
| 569 | /// \code |
---|
| 570 | /// addMap(m1,m2) |
---|
| 571 | /// \endcode |
---|
| 572 | /// |
---|
| 573 | /// This function is specialized for adaptable binary function |
---|
| 574 | /// classes and C++ functions. |
---|
| 575 | /// |
---|
| 576 | /// \relates CombineMap |
---|
| 577 | template<typename M1, typename M2, typename F, typename V> |
---|
| 578 | inline CombineMap<M1, M2, F, V> |
---|
| 579 | combineMap(const M1 &m1, const M2 &m2, const F &f) { |
---|
| 580 | return CombineMap<M1, M2, F, V>(m1,m2,f); |
---|
[25] | 581 | } |
---|
| 582 | |
---|
[80] | 583 | template<typename M1, typename M2, typename F> |
---|
| 584 | inline CombineMap<M1, M2, F, typename F::result_type> |
---|
| 585 | combineMap(const M1 &m1, const M2 &m2, const F &f) { |
---|
| 586 | return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
---|
| 587 | } |
---|
[25] | 588 | |
---|
[80] | 589 | template<typename M1, typename M2, typename K1, typename K2, typename V> |
---|
| 590 | inline CombineMap<M1, M2, V (*)(K1, K2), V> |
---|
| 591 | combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
---|
| 592 | return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | /// Converts an STL style (unary) functor to a map |
---|
| 597 | |
---|
| 598 | /// This \ref concepts::ReadMap "read only map" returns the value |
---|
| 599 | /// of a given functor. Actually, it just wraps the functor and |
---|
| 600 | /// provides the \c Key and \c Value typedefs. |
---|
[26] | 601 | /// |
---|
[80] | 602 | /// Template parameters \c K and \c V will become its \c Key and |
---|
| 603 | /// \c Value. In most cases they have to be given explicitly because |
---|
| 604 | /// a functor typically does not provide \c argument_type and |
---|
| 605 | /// \c result_type typedefs. |
---|
| 606 | /// Parameter \c F is the type of the used functor. |
---|
[29] | 607 | /// |
---|
[80] | 608 | /// The simplest way of using this map is through the functorToMap() |
---|
| 609 | /// function. |
---|
| 610 | /// |
---|
| 611 | /// \sa MapToFunctor |
---|
| 612 | template<typename F, |
---|
| 613 | typename K = typename F::argument_type, |
---|
| 614 | typename V = typename F::result_type> |
---|
| 615 | class FunctorToMap : public MapBase<K, V> { |
---|
| 616 | const F &_f; |
---|
| 617 | public: |
---|
| 618 | typedef MapBase<K, V> Parent; |
---|
| 619 | typedef typename Parent::Key Key; |
---|
| 620 | typedef typename Parent::Value Value; |
---|
[25] | 621 | |
---|
[80] | 622 | /// Constructor |
---|
| 623 | FunctorToMap(const F &f = F()) : _f(f) {} |
---|
| 624 | /// \e |
---|
| 625 | Value operator[](const Key &k) const { return _f(k); } |
---|
| 626 | }; |
---|
| 627 | |
---|
| 628 | /// Returns a \ref FunctorToMap class |
---|
| 629 | |
---|
| 630 | /// This function just returns a \ref FunctorToMap class. |
---|
| 631 | /// |
---|
| 632 | /// This function is specialized for adaptable binary function |
---|
| 633 | /// classes and C++ functions. |
---|
| 634 | /// |
---|
| 635 | /// \relates FunctorToMap |
---|
| 636 | template<typename K, typename V, typename F> |
---|
| 637 | inline FunctorToMap<F, K, V> functorToMap(const F &f) { |
---|
| 638 | return FunctorToMap<F, K, V>(f); |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | template <typename F> |
---|
| 642 | inline FunctorToMap<F, typename F::argument_type, typename F::result_type> |
---|
| 643 | functorToMap(const F &f) |
---|
| 644 | { |
---|
| 645 | return FunctorToMap<F, typename F::argument_type, |
---|
| 646 | typename F::result_type>(f); |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | template <typename K, typename V> |
---|
| 650 | inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) { |
---|
| 651 | return FunctorToMap<V (*)(K), K, V>(f); |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | |
---|
| 655 | /// Converts a map to an STL style (unary) functor |
---|
| 656 | |
---|
| 657 | /// This class converts a map to an STL style (unary) functor. |
---|
| 658 | /// That is it provides an <tt>operator()</tt> to read its values. |
---|
| 659 | /// |
---|
| 660 | /// For the sake of convenience it also works as a usual |
---|
| 661 | /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt> |
---|
| 662 | /// and the \c Key and \c Value typedefs also exist. |
---|
| 663 | /// |
---|
| 664 | /// The simplest way of using this map is through the mapToFunctor() |
---|
| 665 | /// function. |
---|
| 666 | /// |
---|
| 667 | ///\sa FunctorToMap |
---|
| 668 | template <typename M> |
---|
| 669 | class MapToFunctor : public MapBase<typename M::Key, typename M::Value> { |
---|
| 670 | const M &_m; |
---|
[25] | 671 | public: |
---|
| 672 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 673 | typedef typename Parent::Key Key; |
---|
| 674 | typedef typename Parent::Value Value; |
---|
| 675 | |
---|
[80] | 676 | typedef typename Parent::Key argument_type; |
---|
| 677 | typedef typename Parent::Value result_type; |
---|
| 678 | |
---|
| 679 | /// Constructor |
---|
| 680 | MapToFunctor(const M &m) : _m(m) {} |
---|
| 681 | /// \e |
---|
| 682 | Value operator()(const Key &k) const { return _m[k]; } |
---|
| 683 | /// \e |
---|
| 684 | Value operator[](const Key &k) const { return _m[k]; } |
---|
[25] | 685 | }; |
---|
[45] | 686 | |
---|
[80] | 687 | /// Returns a \ref MapToFunctor class |
---|
| 688 | |
---|
| 689 | /// This function just returns a \ref MapToFunctor class. |
---|
| 690 | /// \relates MapToFunctor |
---|
[45] | 691 | template<typename M> |
---|
[80] | 692 | inline MapToFunctor<M> mapToFunctor(const M &m) { |
---|
| 693 | return MapToFunctor<M>(m); |
---|
[45] | 694 | } |
---|
[25] | 695 | |
---|
| 696 | |
---|
[80] | 697 | /// \brief Map adaptor to convert the \c Value type of a map to |
---|
| 698 | /// another type using the default conversion. |
---|
| 699 | |
---|
| 700 | /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap |
---|
| 701 | /// "readable map" to another type using the default conversion. |
---|
| 702 | /// The \c Key type of it is inherited from \c M and the \c Value |
---|
| 703 | /// type is \c V. |
---|
| 704 | /// This type conforms the \ref concepts::ReadMap "ReadMap" concept. |
---|
[26] | 705 | /// |
---|
[80] | 706 | /// The simplest way of using this map is through the convertMap() |
---|
| 707 | /// function. |
---|
| 708 | template <typename M, typename V> |
---|
| 709 | class ConvertMap : public MapBase<typename M::Key, V> { |
---|
| 710 | const M &_m; |
---|
| 711 | public: |
---|
| 712 | typedef MapBase<typename M::Key, V> Parent; |
---|
| 713 | typedef typename Parent::Key Key; |
---|
| 714 | typedef typename Parent::Value Value; |
---|
| 715 | |
---|
| 716 | /// Constructor |
---|
| 717 | |
---|
| 718 | /// Constructor. |
---|
| 719 | /// \param m The underlying map. |
---|
| 720 | ConvertMap(const M &m) : _m(m) {} |
---|
| 721 | |
---|
| 722 | /// \e |
---|
| 723 | Value operator[](const Key &k) const { return _m[k]; } |
---|
| 724 | }; |
---|
| 725 | |
---|
| 726 | /// Returns a \ref ConvertMap class |
---|
| 727 | |
---|
| 728 | /// This function just returns a \ref ConvertMap class. |
---|
| 729 | /// \relates ConvertMap |
---|
| 730 | template<typename V, typename M> |
---|
| 731 | inline ConvertMap<M, V> convertMap(const M &map) { |
---|
| 732 | return ConvertMap<M, V>(map); |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | |
---|
| 736 | /// Applies all map setting operations to two maps |
---|
| 737 | |
---|
| 738 | /// This map has two \ref concepts::WriteMap "writable map" parameters |
---|
| 739 | /// and each write request will be passed to both of them. |
---|
| 740 | /// If \c M1 is also \ref concepts::ReadMap "readable", then the read |
---|
| 741 | /// operations will return the corresponding values of \c M1. |
---|
[29] | 742 | /// |
---|
[80] | 743 | /// The \c Key and \c Value types are inherited from \c M1. |
---|
| 744 | /// The \c Key and \c Value of \c M2 must be convertible from those |
---|
| 745 | /// of \c M1. |
---|
| 746 | /// |
---|
| 747 | /// The simplest way of using this map is through the forkMap() |
---|
| 748 | /// function. |
---|
| 749 | template<typename M1, typename M2> |
---|
| 750 | class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 751 | M1 &_m1; |
---|
| 752 | M2 &_m2; |
---|
| 753 | public: |
---|
| 754 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
| 755 | typedef typename Parent::Key Key; |
---|
| 756 | typedef typename Parent::Value Value; |
---|
[25] | 757 | |
---|
[80] | 758 | /// Constructor |
---|
| 759 | ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 760 | /// Returns the value associated with the given key in the first map. |
---|
| 761 | Value operator[](const Key &k) const { return _m1[k]; } |
---|
| 762 | /// Sets the value associated with the given key in both maps. |
---|
| 763 | void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); } |
---|
| 764 | }; |
---|
| 765 | |
---|
| 766 | /// Returns a \ref ForkMap class |
---|
| 767 | |
---|
| 768 | /// This function just returns a \ref ForkMap class. |
---|
| 769 | /// \relates ForkMap |
---|
| 770 | template <typename M1, typename M2> |
---|
| 771 | inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) { |
---|
| 772 | return ForkMap<M1,M2>(m1,m2); |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | |
---|
| 776 | /// Simple wrapping of a map |
---|
| 777 | |
---|
| 778 | /// This \ref concepts::ReadMap "read only map" returns the simple |
---|
| 779 | /// wrapping of the given map. Sometimes the reference maps cannot be |
---|
| 780 | /// combined with simple read maps. This map adaptor wraps the given |
---|
| 781 | /// map to simple read map. |
---|
| 782 | /// |
---|
| 783 | /// The simplest way of using this map is through the wrapMap() |
---|
| 784 | /// function. |
---|
| 785 | /// |
---|
| 786 | /// \sa WrapWriteMap |
---|
| 787 | template<typename M> |
---|
| 788 | class WrapMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 789 | const M &_m; |
---|
[25] | 790 | public: |
---|
| 791 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 792 | typedef typename Parent::Key Key; |
---|
| 793 | typedef typename Parent::Value Value; |
---|
| 794 | |
---|
[80] | 795 | /// Constructor |
---|
| 796 | WrapMap(const M &m) : _m(m) {} |
---|
| 797 | /// \e |
---|
| 798 | Value operator[](const Key &k) const { return _m[k]; } |
---|
[25] | 799 | }; |
---|
| 800 | |
---|
[80] | 801 | /// Returns a \ref WrapMap class |
---|
[45] | 802 | |
---|
[80] | 803 | /// This function just returns a \ref WrapMap class. |
---|
| 804 | /// \relates WrapMap |
---|
[45] | 805 | template<typename M> |
---|
[80] | 806 | inline WrapMap<M> wrapMap(const M &map) { |
---|
| 807 | return WrapMap<M>(map); |
---|
[45] | 808 | } |
---|
| 809 | |
---|
[25] | 810 | |
---|
[80] | 811 | /// Simple writable wrapping of a map |
---|
| 812 | |
---|
| 813 | /// This \ref concepts::ReadWriteMap "read-write map" returns the simple |
---|
| 814 | /// wrapping of the given map. Sometimes the reference maps cannot be |
---|
| 815 | /// combined with simple read-write maps. This map adaptor wraps the |
---|
| 816 | /// given map to simple read-write map. |
---|
| 817 | /// |
---|
| 818 | /// The simplest way of using this map is through the wrapWriteMap() |
---|
| 819 | /// function. |
---|
| 820 | /// |
---|
| 821 | /// \sa WrapMap |
---|
| 822 | template<typename M> |
---|
| 823 | class WrapWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
| 824 | M &_m; |
---|
| 825 | public: |
---|
| 826 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 827 | typedef typename Parent::Key Key; |
---|
| 828 | typedef typename Parent::Value Value; |
---|
| 829 | |
---|
| 830 | /// Constructor |
---|
| 831 | WrapWriteMap(M &m) : _m(m) {} |
---|
| 832 | /// \e |
---|
| 833 | Value operator[](const Key &k) const { return _m[k]; } |
---|
| 834 | /// \e |
---|
| 835 | void set(const Key &k, const Value &c) { _m.set(k, c); } |
---|
| 836 | }; |
---|
| 837 | |
---|
| 838 | ///Returns a \ref WrapWriteMap class |
---|
| 839 | |
---|
| 840 | ///This function just returns a \ref WrapWriteMap class. |
---|
| 841 | ///\relates WrapWriteMap |
---|
| 842 | template<typename M> |
---|
| 843 | inline WrapWriteMap<M> wrapWriteMap(M &map) { |
---|
| 844 | return WrapWriteMap<M>(map); |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | |
---|
| 848 | /// Sum of two maps |
---|
| 849 | |
---|
| 850 | /// This \ref concepts::ReadMap "read only map" returns the sum |
---|
| 851 | /// of the values of the two given maps. |
---|
| 852 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 853 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 854 | /// \c M1. |
---|
| 855 | /// |
---|
| 856 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 857 | /// \code |
---|
| 858 | /// AddMap<M1,M2> am(m1,m2); |
---|
| 859 | /// \endcode |
---|
| 860 | /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>. |
---|
| 861 | /// |
---|
| 862 | /// The simplest way of using this map is through the addMap() |
---|
| 863 | /// function. |
---|
| 864 | /// |
---|
| 865 | /// \sa SubMap, MulMap, DivMap |
---|
| 866 | /// \sa ShiftMap, ShiftWriteMap |
---|
| 867 | template<typename M1, typename M2> |
---|
[25] | 868 | class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
[80] | 869 | const M1 &_m1; |
---|
| 870 | const M2 &_m2; |
---|
[25] | 871 | public: |
---|
| 872 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
| 873 | typedef typename Parent::Key Key; |
---|
| 874 | typedef typename Parent::Value Value; |
---|
| 875 | |
---|
[80] | 876 | /// Constructor |
---|
| 877 | AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 878 | /// \e |
---|
| 879 | Value operator[](const Key &k) const { return _m1[k]+_m2[k]; } |
---|
[25] | 880 | }; |
---|
| 881 | |
---|
[80] | 882 | /// Returns an \ref AddMap class |
---|
| 883 | |
---|
| 884 | /// This function just returns an \ref AddMap class. |
---|
[25] | 885 | /// |
---|
[80] | 886 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 887 | /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to |
---|
| 888 | /// <tt>m1[x]+m2[x]</tt>. |
---|
| 889 | /// |
---|
| 890 | /// \relates AddMap |
---|
| 891 | template<typename M1, typename M2> |
---|
| 892 | inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) { |
---|
[25] | 893 | return AddMap<M1, M2>(m1,m2); |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | |
---|
[80] | 897 | /// Difference of two maps |
---|
| 898 | |
---|
| 899 | /// This \ref concepts::ReadMap "read only map" returns the difference |
---|
| 900 | /// of the values of the two given maps. |
---|
| 901 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 902 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 903 | /// \c M1. |
---|
[25] | 904 | /// |
---|
[80] | 905 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 906 | /// \code |
---|
| 907 | /// SubMap<M1,M2> sm(m1,m2); |
---|
| 908 | /// \endcode |
---|
| 909 | /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>. |
---|
[29] | 910 | /// |
---|
[80] | 911 | /// The simplest way of using this map is through the subMap() |
---|
| 912 | /// function. |
---|
| 913 | /// |
---|
| 914 | /// \sa AddMap, MulMap, DivMap |
---|
| 915 | template<typename M1, typename M2> |
---|
| 916 | class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 917 | const M1 &_m1; |
---|
| 918 | const M2 &_m2; |
---|
| 919 | public: |
---|
| 920 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
| 921 | typedef typename Parent::Key Key; |
---|
| 922 | typedef typename Parent::Value Value; |
---|
| 923 | |
---|
| 924 | /// Constructor |
---|
| 925 | SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 926 | /// \e |
---|
| 927 | Value operator[](const Key &k) const { return _m1[k]-_m2[k]; } |
---|
| 928 | }; |
---|
| 929 | |
---|
| 930 | /// Returns a \ref SubMap class |
---|
| 931 | |
---|
| 932 | /// This function just returns a \ref SubMap class. |
---|
| 933 | /// |
---|
| 934 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 935 | /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to |
---|
| 936 | /// <tt>m1[x]-m2[x]</tt>. |
---|
| 937 | /// |
---|
| 938 | /// \relates SubMap |
---|
| 939 | template<typename M1, typename M2> |
---|
| 940 | inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
---|
| 941 | return SubMap<M1, M2>(m1,m2); |
---|
| 942 | } |
---|
| 943 | |
---|
| 944 | |
---|
| 945 | /// Product of two maps |
---|
| 946 | |
---|
| 947 | /// This \ref concepts::ReadMap "read only map" returns the product |
---|
| 948 | /// of the values of the two given maps. |
---|
| 949 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 950 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 951 | /// \c M1. |
---|
| 952 | /// |
---|
| 953 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 954 | /// \code |
---|
| 955 | /// MulMap<M1,M2> mm(m1,m2); |
---|
| 956 | /// \endcode |
---|
| 957 | /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>. |
---|
| 958 | /// |
---|
| 959 | /// The simplest way of using this map is through the mulMap() |
---|
| 960 | /// function. |
---|
| 961 | /// |
---|
| 962 | /// \sa AddMap, SubMap, DivMap |
---|
| 963 | /// \sa ScaleMap, ScaleWriteMap |
---|
| 964 | template<typename M1, typename M2> |
---|
| 965 | class MulMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 966 | const M1 &_m1; |
---|
| 967 | const M2 &_m2; |
---|
| 968 | public: |
---|
| 969 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
| 970 | typedef typename Parent::Key Key; |
---|
| 971 | typedef typename Parent::Value Value; |
---|
| 972 | |
---|
| 973 | /// Constructor |
---|
| 974 | MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 975 | /// \e |
---|
| 976 | Value operator[](const Key &k) const { return _m1[k]*_m2[k]; } |
---|
| 977 | }; |
---|
| 978 | |
---|
| 979 | /// Returns a \ref MulMap class |
---|
| 980 | |
---|
| 981 | /// This function just returns a \ref MulMap class. |
---|
| 982 | /// |
---|
| 983 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 984 | /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to |
---|
| 985 | /// <tt>m1[x]*m2[x]</tt>. |
---|
| 986 | /// |
---|
| 987 | /// \relates MulMap |
---|
| 988 | template<typename M1, typename M2> |
---|
| 989 | inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
---|
| 990 | return MulMap<M1, M2>(m1,m2); |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | |
---|
| 994 | /// Quotient of two maps |
---|
| 995 | |
---|
| 996 | /// This \ref concepts::ReadMap "read only map" returns the quotient |
---|
| 997 | /// of the values of the two given maps. |
---|
| 998 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 999 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 1000 | /// \c M1. |
---|
| 1001 | /// |
---|
| 1002 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 1003 | /// \code |
---|
| 1004 | /// DivMap<M1,M2> dm(m1,m2); |
---|
| 1005 | /// \endcode |
---|
| 1006 | /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>. |
---|
| 1007 | /// |
---|
| 1008 | /// The simplest way of using this map is through the divMap() |
---|
| 1009 | /// function. |
---|
| 1010 | /// |
---|
| 1011 | /// \sa AddMap, SubMap, MulMap |
---|
| 1012 | template<typename M1, typename M2> |
---|
| 1013 | class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 1014 | const M1 &_m1; |
---|
| 1015 | const M2 &_m2; |
---|
| 1016 | public: |
---|
| 1017 | typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
---|
| 1018 | typedef typename Parent::Key Key; |
---|
| 1019 | typedef typename Parent::Value Value; |
---|
| 1020 | |
---|
| 1021 | /// Constructor |
---|
| 1022 | DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 1023 | /// \e |
---|
| 1024 | Value operator[](const Key &k) const { return _m1[k]/_m2[k]; } |
---|
| 1025 | }; |
---|
| 1026 | |
---|
| 1027 | /// Returns a \ref DivMap class |
---|
| 1028 | |
---|
| 1029 | /// This function just returns a \ref DivMap class. |
---|
| 1030 | /// |
---|
| 1031 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 1032 | /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to |
---|
| 1033 | /// <tt>m1[x]/m2[x]</tt>. |
---|
| 1034 | /// |
---|
| 1035 | /// \relates DivMap |
---|
| 1036 | template<typename M1, typename M2> |
---|
| 1037 | inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) { |
---|
| 1038 | return DivMap<M1, M2>(m1,m2); |
---|
| 1039 | } |
---|
| 1040 | |
---|
| 1041 | |
---|
| 1042 | /// Shifts a map with a constant. |
---|
| 1043 | |
---|
| 1044 | /// This \ref concepts::ReadMap "read only map" returns the sum of |
---|
| 1045 | /// the given map and a constant value (i.e. it shifts the map with |
---|
| 1046 | /// the constant). Its \c Key and \c Value are inherited from \c M. |
---|
| 1047 | /// |
---|
| 1048 | /// Actually, |
---|
| 1049 | /// \code |
---|
| 1050 | /// ShiftMap<M> sh(m,v); |
---|
| 1051 | /// \endcode |
---|
| 1052 | /// is equivalent to |
---|
| 1053 | /// \code |
---|
| 1054 | /// ConstMap<M::Key, M::Value> cm(v); |
---|
| 1055 | /// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm); |
---|
| 1056 | /// \endcode |
---|
| 1057 | /// |
---|
| 1058 | /// The simplest way of using this map is through the shiftMap() |
---|
| 1059 | /// function. |
---|
| 1060 | /// |
---|
| 1061 | /// \sa ShiftWriteMap |
---|
| 1062 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1063 | class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1064 | const M &_m; |
---|
| 1065 | C _v; |
---|
[25] | 1066 | public: |
---|
| 1067 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1068 | typedef typename Parent::Key Key; |
---|
| 1069 | typedef typename Parent::Value Value; |
---|
| 1070 | |
---|
[80] | 1071 | /// Constructor |
---|
[25] | 1072 | |
---|
[80] | 1073 | /// Constructor. |
---|
| 1074 | /// \param m The undelying map. |
---|
| 1075 | /// \param v The constant value. |
---|
| 1076 | ShiftMap(const M &m, const C &v) : _m(m), _v(v) {} |
---|
| 1077 | /// \e |
---|
| 1078 | Value operator[](const Key &k) const { return _m[k]+_v; } |
---|
[25] | 1079 | }; |
---|
| 1080 | |
---|
[80] | 1081 | /// Shifts a map with a constant (read-write version). |
---|
[25] | 1082 | |
---|
[80] | 1083 | /// This \ref concepts::ReadWriteMap "read-write map" returns the sum |
---|
| 1084 | /// of the given map and a constant value (i.e. it shifts the map with |
---|
| 1085 | /// the constant). Its \c Key and \c Value are inherited from \c M. |
---|
| 1086 | /// It makes also possible to write the map. |
---|
[25] | 1087 | /// |
---|
[80] | 1088 | /// The simplest way of using this map is through the shiftWriteMap() |
---|
| 1089 | /// function. |
---|
| 1090 | /// |
---|
| 1091 | /// \sa ShiftMap |
---|
| 1092 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1093 | class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1094 | M &_m; |
---|
| 1095 | C _v; |
---|
[25] | 1096 | public: |
---|
| 1097 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1098 | typedef typename Parent::Key Key; |
---|
| 1099 | typedef typename Parent::Value Value; |
---|
| 1100 | |
---|
[80] | 1101 | /// Constructor |
---|
[25] | 1102 | |
---|
[80] | 1103 | /// Constructor. |
---|
| 1104 | /// \param m The undelying map. |
---|
| 1105 | /// \param v The constant value. |
---|
| 1106 | ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {} |
---|
[25] | 1107 | /// \e |
---|
[80] | 1108 | Value operator[](const Key &k) const { return _m[k]+_v; } |
---|
[25] | 1109 | /// \e |
---|
[80] | 1110 | void set(const Key &k, const Value &v) { _m.set(k, v-_v); } |
---|
[25] | 1111 | }; |
---|
| 1112 | |
---|
[80] | 1113 | /// Returns a \ref ShiftMap class |
---|
| 1114 | |
---|
| 1115 | /// This function just returns a \ref ShiftMap class. |
---|
| 1116 | /// |
---|
| 1117 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1118 | /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to |
---|
| 1119 | /// <tt>m[x]+v</tt>. |
---|
| 1120 | /// |
---|
| 1121 | /// \relates ShiftMap |
---|
| 1122 | template<typename M, typename C> |
---|
| 1123 | inline ShiftMap<M, C> shiftMap(const M &m, const C &v) { |
---|
[25] | 1124 | return ShiftMap<M, C>(m,v); |
---|
| 1125 | } |
---|
| 1126 | |
---|
[80] | 1127 | /// Returns a \ref ShiftWriteMap class |
---|
[29] | 1128 | |
---|
[80] | 1129 | /// This function just returns a \ref ShiftWriteMap class. |
---|
| 1130 | /// |
---|
| 1131 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1132 | /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to |
---|
| 1133 | /// <tt>m[x]+v</tt>. |
---|
| 1134 | /// Moreover it makes also possible to write the map. |
---|
| 1135 | /// |
---|
| 1136 | /// \relates ShiftWriteMap |
---|
| 1137 | template<typename M, typename C> |
---|
| 1138 | inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) { |
---|
[25] | 1139 | return ShiftWriteMap<M, C>(m,v); |
---|
| 1140 | } |
---|
| 1141 | |
---|
| 1142 | |
---|
[80] | 1143 | /// Scales a map with a constant. |
---|
| 1144 | |
---|
| 1145 | /// This \ref concepts::ReadMap "read only map" returns the value of |
---|
| 1146 | /// the given map multiplied from the left side with a constant value. |
---|
| 1147 | /// Its \c Key and \c Value are inherited from \c M. |
---|
[26] | 1148 | /// |
---|
[80] | 1149 | /// Actually, |
---|
| 1150 | /// \code |
---|
| 1151 | /// ScaleMap<M> sc(m,v); |
---|
| 1152 | /// \endcode |
---|
| 1153 | /// is equivalent to |
---|
| 1154 | /// \code |
---|
| 1155 | /// ConstMap<M::Key, M::Value> cm(v); |
---|
| 1156 | /// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m); |
---|
| 1157 | /// \endcode |
---|
[25] | 1158 | /// |
---|
[80] | 1159 | /// The simplest way of using this map is through the scaleMap() |
---|
| 1160 | /// function. |
---|
[25] | 1161 | /// |
---|
[80] | 1162 | /// \sa ScaleWriteMap |
---|
| 1163 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1164 | class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1165 | const M &_m; |
---|
| 1166 | C _v; |
---|
[25] | 1167 | public: |
---|
| 1168 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1169 | typedef typename Parent::Key Key; |
---|
| 1170 | typedef typename Parent::Value Value; |
---|
| 1171 | |
---|
[80] | 1172 | /// Constructor |
---|
[25] | 1173 | |
---|
[80] | 1174 | /// Constructor. |
---|
| 1175 | /// \param m The undelying map. |
---|
| 1176 | /// \param v The constant value. |
---|
| 1177 | ScaleMap(const M &m, const C &v) : _m(m), _v(v) {} |
---|
[25] | 1178 | /// \e |
---|
[80] | 1179 | Value operator[](const Key &k) const { return _v*_m[k]; } |
---|
[25] | 1180 | }; |
---|
| 1181 | |
---|
[80] | 1182 | /// Scales a map with a constant (read-write version). |
---|
[25] | 1183 | |
---|
[80] | 1184 | /// This \ref concepts::ReadWriteMap "read-write map" returns the value of |
---|
| 1185 | /// the given map multiplied from the left side with a constant value. |
---|
| 1186 | /// Its \c Key and \c Value are inherited from \c M. |
---|
| 1187 | /// It can also be used as write map if the \c / operator is defined |
---|
| 1188 | /// between \c Value and \c C and the given multiplier is not zero. |
---|
[29] | 1189 | /// |
---|
[80] | 1190 | /// The simplest way of using this map is through the scaleWriteMap() |
---|
| 1191 | /// function. |
---|
| 1192 | /// |
---|
| 1193 | /// \sa ScaleMap |
---|
| 1194 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1195 | class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1196 | M &_m; |
---|
| 1197 | C _v; |
---|
[25] | 1198 | public: |
---|
| 1199 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1200 | typedef typename Parent::Key Key; |
---|
| 1201 | typedef typename Parent::Value Value; |
---|
| 1202 | |
---|
[80] | 1203 | /// Constructor |
---|
[25] | 1204 | |
---|
[80] | 1205 | /// Constructor. |
---|
| 1206 | /// \param m The undelying map. |
---|
| 1207 | /// \param v The constant value. |
---|
| 1208 | ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {} |
---|
[25] | 1209 | /// \e |
---|
[80] | 1210 | Value operator[](const Key &k) const { return _v*_m[k]; } |
---|
[25] | 1211 | /// \e |
---|
[80] | 1212 | void set(const Key &k, const Value &v) { _m.set(k, v/_v); } |
---|
[25] | 1213 | }; |
---|
| 1214 | |
---|
[80] | 1215 | /// Returns a \ref ScaleMap class |
---|
| 1216 | |
---|
| 1217 | /// This function just returns a \ref ScaleMap class. |
---|
| 1218 | /// |
---|
| 1219 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1220 | /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to |
---|
| 1221 | /// <tt>v*m[x]</tt>. |
---|
| 1222 | /// |
---|
| 1223 | /// \relates ScaleMap |
---|
| 1224 | template<typename M, typename C> |
---|
| 1225 | inline ScaleMap<M, C> scaleMap(const M &m, const C &v) { |
---|
[25] | 1226 | return ScaleMap<M, C>(m,v); |
---|
| 1227 | } |
---|
| 1228 | |
---|
[80] | 1229 | /// Returns a \ref ScaleWriteMap class |
---|
[29] | 1230 | |
---|
[80] | 1231 | /// This function just returns a \ref ScaleWriteMap class. |
---|
| 1232 | /// |
---|
| 1233 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1234 | /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to |
---|
| 1235 | /// <tt>v*m[x]</tt>. |
---|
| 1236 | /// Moreover it makes also possible to write the map. |
---|
| 1237 | /// |
---|
| 1238 | /// \relates ScaleWriteMap |
---|
| 1239 | template<typename M, typename C> |
---|
| 1240 | inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) { |
---|
[25] | 1241 | return ScaleWriteMap<M, C>(m,v); |
---|
| 1242 | } |
---|
| 1243 | |
---|
| 1244 | |
---|
[80] | 1245 | /// Negative of a map |
---|
[25] | 1246 | |
---|
[80] | 1247 | /// This \ref concepts::ReadMap "read only map" returns the negative |
---|
| 1248 | /// of the values of the given map (using the unary \c - operator). |
---|
| 1249 | /// Its \c Key and \c Value are inherited from \c M. |
---|
[25] | 1250 | /// |
---|
[80] | 1251 | /// If M::Value is \c int, \c double etc., then |
---|
| 1252 | /// \code |
---|
| 1253 | /// NegMap<M> neg(m); |
---|
| 1254 | /// \endcode |
---|
| 1255 | /// is equivalent to |
---|
| 1256 | /// \code |
---|
| 1257 | /// ScaleMap<M> neg(m,-1); |
---|
| 1258 | /// \endcode |
---|
[29] | 1259 | /// |
---|
[80] | 1260 | /// The simplest way of using this map is through the negMap() |
---|
| 1261 | /// function. |
---|
[29] | 1262 | /// |
---|
[80] | 1263 | /// \sa NegWriteMap |
---|
| 1264 | template<typename M> |
---|
[25] | 1265 | class NegMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1266 | const M& _m; |
---|
[25] | 1267 | public: |
---|
| 1268 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1269 | typedef typename Parent::Key Key; |
---|
| 1270 | typedef typename Parent::Value Value; |
---|
| 1271 | |
---|
[80] | 1272 | /// Constructor |
---|
| 1273 | NegMap(const M &m) : _m(m) {} |
---|
[25] | 1274 | /// \e |
---|
[80] | 1275 | Value operator[](const Key &k) const { return -_m[k]; } |
---|
[25] | 1276 | }; |
---|
| 1277 | |
---|
[80] | 1278 | /// Negative of a map (read-write version) |
---|
| 1279 | |
---|
| 1280 | /// This \ref concepts::ReadWriteMap "read-write map" returns the |
---|
| 1281 | /// negative of the values of the given map (using the unary \c - |
---|
| 1282 | /// operator). |
---|
| 1283 | /// Its \c Key and \c Value are inherited from \c M. |
---|
| 1284 | /// It makes also possible to write the map. |
---|
| 1285 | /// |
---|
| 1286 | /// If M::Value is \c int, \c double etc., then |
---|
| 1287 | /// \code |
---|
| 1288 | /// NegWriteMap<M> neg(m); |
---|
| 1289 | /// \endcode |
---|
| 1290 | /// is equivalent to |
---|
| 1291 | /// \code |
---|
| 1292 | /// ScaleWriteMap<M> neg(m,-1); |
---|
| 1293 | /// \endcode |
---|
| 1294 | /// |
---|
| 1295 | /// The simplest way of using this map is through the negWriteMap() |
---|
| 1296 | /// function. |
---|
[29] | 1297 | /// |
---|
| 1298 | /// \sa NegMap |
---|
[80] | 1299 | template<typename M> |
---|
[25] | 1300 | class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1301 | M &_m; |
---|
[25] | 1302 | public: |
---|
| 1303 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1304 | typedef typename Parent::Key Key; |
---|
| 1305 | typedef typename Parent::Value Value; |
---|
| 1306 | |
---|
[80] | 1307 | /// Constructor |
---|
| 1308 | NegWriteMap(M &m) : _m(m) {} |
---|
[25] | 1309 | /// \e |
---|
[80] | 1310 | Value operator[](const Key &k) const { return -_m[k]; } |
---|
[25] | 1311 | /// \e |
---|
[80] | 1312 | void set(const Key &k, const Value &v) { _m.set(k, -v); } |
---|
[25] | 1313 | }; |
---|
| 1314 | |
---|
[80] | 1315 | /// Returns a \ref NegMap class |
---|
[25] | 1316 | |
---|
[80] | 1317 | /// This function just returns a \ref NegMap class. |
---|
| 1318 | /// |
---|
| 1319 | /// For example, if \c m is a map with \c double values, then |
---|
| 1320 | /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
---|
| 1321 | /// |
---|
| 1322 | /// \relates NegMap |
---|
| 1323 | template <typename M> |
---|
[25] | 1324 | inline NegMap<M> negMap(const M &m) { |
---|
| 1325 | return NegMap<M>(m); |
---|
| 1326 | } |
---|
| 1327 | |
---|
[80] | 1328 | /// Returns a \ref NegWriteMap class |
---|
[29] | 1329 | |
---|
[80] | 1330 | /// This function just returns a \ref NegWriteMap class. |
---|
| 1331 | /// |
---|
| 1332 | /// For example, if \c m is a map with \c double values, then |
---|
| 1333 | /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
---|
| 1334 | /// Moreover it makes also possible to write the map. |
---|
| 1335 | /// |
---|
| 1336 | /// \relates NegWriteMap |
---|
| 1337 | template <typename M> |
---|
| 1338 | inline NegWriteMap<M> negWriteMap(M &m) { |
---|
[25] | 1339 | return NegWriteMap<M>(m); |
---|
| 1340 | } |
---|
| 1341 | |
---|
| 1342 | |
---|
[80] | 1343 | /// Absolute value of a map |
---|
| 1344 | |
---|
| 1345 | /// This \ref concepts::ReadMap "read only map" returns the absolute |
---|
| 1346 | /// value of the values of the given map. |
---|
| 1347 | /// Its \c Key and \c Value are inherited from \c M. |
---|
| 1348 | /// \c Value must be comparable to \c 0 and the unary \c - |
---|
| 1349 | /// operator must be defined for it, of course. |
---|
| 1350 | /// |
---|
| 1351 | /// The simplest way of using this map is through the absMap() |
---|
| 1352 | /// function. |
---|
| 1353 | template<typename M> |
---|
[25] | 1354 | class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1355 | const M &_m; |
---|
[25] | 1356 | public: |
---|
| 1357 | typedef MapBase<typename M::Key, typename M::Value> Parent; |
---|
| 1358 | typedef typename Parent::Key Key; |
---|
| 1359 | typedef typename Parent::Value Value; |
---|
| 1360 | |
---|
[80] | 1361 | /// Constructor |
---|
| 1362 | AbsMap(const M &m) : _m(m) {} |
---|
[25] | 1363 | /// \e |
---|
[80] | 1364 | Value operator[](const Key &k) const { |
---|
| 1365 | Value tmp = _m[k]; |
---|
[25] | 1366 | return tmp >= 0 ? tmp : -tmp; |
---|
| 1367 | } |
---|
| 1368 | |
---|
| 1369 | }; |
---|
| 1370 | |
---|
[80] | 1371 | /// Returns an \ref AbsMap class |
---|
| 1372 | |
---|
| 1373 | /// This function just returns an \ref AbsMap class. |
---|
| 1374 | /// |
---|
| 1375 | /// For example, if \c m is a map with \c double values, then |
---|
| 1376 | /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if |
---|
| 1377 | /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is |
---|
| 1378 | /// negative. |
---|
| 1379 | /// |
---|
| 1380 | /// \relates AbsMap |
---|
| 1381 | template<typename M> |
---|
[25] | 1382 | inline AbsMap<M> absMap(const M &m) { |
---|
| 1383 | return AbsMap<M>(m); |
---|
| 1384 | } |
---|
| 1385 | |
---|
| 1386 | |
---|
[80] | 1387 | /// Logical 'not' of a map |
---|
| 1388 | |
---|
| 1389 | /// This \ref concepts::ReadMap "read only map" returns the logical |
---|
| 1390 | /// negation of the values of the given map. |
---|
| 1391 | /// Its \c Key is inherited from \c M and its \c Value is \c bool. |
---|
[25] | 1392 | /// |
---|
[80] | 1393 | /// The simplest way of using this map is through the notMap() |
---|
| 1394 | /// function. |
---|
[25] | 1395 | /// |
---|
[80] | 1396 | /// \sa NotWriteMap |
---|
| 1397 | template <typename M> |
---|
[25] | 1398 | class NotMap : public MapBase<typename M::Key, bool> { |
---|
[80] | 1399 | const M &_m; |
---|
[25] | 1400 | public: |
---|
| 1401 | typedef MapBase<typename M::Key, bool> Parent; |
---|
| 1402 | typedef typename Parent::Key Key; |
---|
| 1403 | typedef typename Parent::Value Value; |
---|
| 1404 | |
---|
| 1405 | /// Constructor |
---|
[80] | 1406 | NotMap(const M &m) : _m(m) {} |
---|
| 1407 | /// \e |
---|
| 1408 | Value operator[](const Key &k) const { return !_m[k]; } |
---|
[25] | 1409 | }; |
---|
| 1410 | |
---|
[80] | 1411 | /// Logical 'not' of a map (read-write version) |
---|
| 1412 | |
---|
| 1413 | /// This \ref concepts::ReadWriteMap "read-write map" returns the |
---|
| 1414 | /// logical negation of the values of the given map. |
---|
| 1415 | /// Its \c Key is inherited from \c M and its \c Value is \c bool. |
---|
| 1416 | /// It makes also possible to write the map. When a value is set, |
---|
| 1417 | /// the opposite value is set to the original map. |
---|
[29] | 1418 | /// |
---|
[80] | 1419 | /// The simplest way of using this map is through the notWriteMap() |
---|
| 1420 | /// function. |
---|
| 1421 | /// |
---|
| 1422 | /// \sa NotMap |
---|
| 1423 | template <typename M> |
---|
[25] | 1424 | class NotWriteMap : public MapBase<typename M::Key, bool> { |
---|
[80] | 1425 | M &_m; |
---|
[25] | 1426 | public: |
---|
| 1427 | typedef MapBase<typename M::Key, bool> Parent; |
---|
| 1428 | typedef typename Parent::Key Key; |
---|
| 1429 | typedef typename Parent::Value Value; |
---|
| 1430 | |
---|
| 1431 | /// Constructor |
---|
[80] | 1432 | NotWriteMap(M &m) : _m(m) {} |
---|
| 1433 | /// \e |
---|
| 1434 | Value operator[](const Key &k) const { return !_m[k]; } |
---|
| 1435 | /// \e |
---|
| 1436 | void set(const Key &k, bool v) { _m.set(k, !v); } |
---|
[25] | 1437 | }; |
---|
[80] | 1438 | |
---|
| 1439 | /// Returns a \ref NotMap class |
---|
| 1440 | |
---|
| 1441 | /// This function just returns a \ref NotMap class. |
---|
| 1442 | /// |
---|
| 1443 | /// For example, if \c m is a map with \c bool values, then |
---|
| 1444 | /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
---|
| 1445 | /// |
---|
| 1446 | /// \relates NotMap |
---|
| 1447 | template <typename M> |
---|
[25] | 1448 | inline NotMap<M> notMap(const M &m) { |
---|
| 1449 | return NotMap<M>(m); |
---|
| 1450 | } |
---|
[80] | 1451 | |
---|
| 1452 | /// Returns a \ref NotWriteMap class |
---|
| 1453 | |
---|
| 1454 | /// This function just returns a \ref NotWriteMap class. |
---|
| 1455 | /// |
---|
| 1456 | /// For example, if \c m is a map with \c bool values, then |
---|
| 1457 | /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
---|
| 1458 | /// Moreover it makes also possible to write the map. |
---|
| 1459 | /// |
---|
| 1460 | /// \relates NotWriteMap |
---|
| 1461 | template <typename M> |
---|
| 1462 | inline NotWriteMap<M> notWriteMap(M &m) { |
---|
[25] | 1463 | return NotWriteMap<M>(m); |
---|
| 1464 | } |
---|
| 1465 | |
---|
[80] | 1466 | |
---|
[25] | 1467 | namespace _maps_bits { |
---|
| 1468 | |
---|
| 1469 | template <typename Value> |
---|
| 1470 | struct Identity { |
---|
| 1471 | typedef Value argument_type; |
---|
| 1472 | typedef Value result_type; |
---|
| 1473 | Value operator()(const Value& val) const { |
---|
| 1474 | return val; |
---|
| 1475 | } |
---|
| 1476 | }; |
---|
| 1477 | |
---|
| 1478 | template <typename _Iterator, typename Enable = void> |
---|
| 1479 | struct IteratorTraits { |
---|
| 1480 | typedef typename std::iterator_traits<_Iterator>::value_type Value; |
---|
| 1481 | }; |
---|
| 1482 | |
---|
| 1483 | template <typename _Iterator> |
---|
| 1484 | struct IteratorTraits<_Iterator, |
---|
[80] | 1485 | typename exists<typename _Iterator::container_type>::type> |
---|
[25] | 1486 | { |
---|
| 1487 | typedef typename _Iterator::container_type::value_type Value; |
---|
| 1488 | }; |
---|
| 1489 | |
---|
| 1490 | } |
---|
[80] | 1491 | |
---|
[25] | 1492 | |
---|
[29] | 1493 | /// \brief Writable bool map for logging each \c true assigned element |
---|
[25] | 1494 | /// |
---|
[80] | 1495 | /// A \ref concepts::ReadWriteMap "read-write" bool map for logging |
---|
| 1496 | /// each \c true assigned element, i.e it copies all the keys set |
---|
[46] | 1497 | /// to \c true to the given iterator. |
---|
[25] | 1498 | /// |
---|
[80] | 1499 | /// \note The container of the iterator should contain space |
---|
[25] | 1500 | /// for each element. |
---|
| 1501 | /// |
---|
[80] | 1502 | /// The following example shows how you can write the edges found by |
---|
[47] | 1503 | /// the \ref Prim algorithm directly to the standard output. |
---|
[80] | 1504 | /// \code |
---|
| 1505 | /// typedef IdMap<Graph, Edge> EdgeIdMap; |
---|
| 1506 | /// EdgeIdMap edgeId(graph); |
---|
[25] | 1507 | /// |
---|
[80] | 1508 | /// typedef MapToFunctor<EdgeIdMap> EdgeIdFunctor; |
---|
| 1509 | /// EdgeIdFunctor edgeIdFunctor(edgeId); |
---|
[25] | 1510 | /// |
---|
[80] | 1511 | /// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor> |
---|
| 1512 | /// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor); |
---|
[25] | 1513 | /// |
---|
[80] | 1514 | /// prim(graph, cost, writerMap); |
---|
| 1515 | /// \endcode |
---|
[26] | 1516 | /// |
---|
[80] | 1517 | /// \sa BackInserterBoolMap |
---|
| 1518 | /// \sa FrontInserterBoolMap |
---|
| 1519 | /// \sa InserterBoolMap |
---|
[29] | 1520 | /// |
---|
[80] | 1521 | /// \todo Revise the name of this class and the related ones. |
---|
| 1522 | template <typename _Iterator, |
---|
[25] | 1523 | typename _Functor = |
---|
| 1524 | _maps_bits::Identity<typename _maps_bits:: |
---|
| 1525 | IteratorTraits<_Iterator>::Value> > |
---|
| 1526 | class StoreBoolMap { |
---|
| 1527 | public: |
---|
| 1528 | typedef _Iterator Iterator; |
---|
| 1529 | |
---|
| 1530 | typedef typename _Functor::argument_type Key; |
---|
| 1531 | typedef bool Value; |
---|
| 1532 | |
---|
| 1533 | typedef _Functor Functor; |
---|
| 1534 | |
---|
| 1535 | /// Constructor |
---|
[80] | 1536 | StoreBoolMap(Iterator it, const Functor& functor = Functor()) |
---|
[25] | 1537 | : _begin(it), _end(it), _functor(functor) {} |
---|
| 1538 | |
---|
[26] | 1539 | /// Gives back the given iterator set for the first key |
---|
[25] | 1540 | Iterator begin() const { |
---|
| 1541 | return _begin; |
---|
| 1542 | } |
---|
[80] | 1543 | |
---|
[26] | 1544 | /// Gives back the the 'after the last' iterator |
---|
[25] | 1545 | Iterator end() const { |
---|
| 1546 | return _end; |
---|
| 1547 | } |
---|
| 1548 | |
---|
[80] | 1549 | /// The set function of the map |
---|
[25] | 1550 | void set(const Key& key, Value value) const { |
---|
| 1551 | if (value) { |
---|
| 1552 | *_end++ = _functor(key); |
---|
| 1553 | } |
---|
| 1554 | } |
---|
[80] | 1555 | |
---|
[25] | 1556 | private: |
---|
| 1557 | Iterator _begin; |
---|
| 1558 | mutable Iterator _end; |
---|
| 1559 | Functor _functor; |
---|
| 1560 | }; |
---|
| 1561 | |
---|
[80] | 1562 | /// \brief Writable bool map for logging each \c true assigned element in |
---|
[29] | 1563 | /// a back insertable container. |
---|
[25] | 1564 | /// |
---|
[29] | 1565 | /// Writable bool map for logging each \c true assigned element by pushing |
---|
| 1566 | /// them into a back insertable container. |
---|
[26] | 1567 | /// It can be used to retrieve the items into a standard |
---|
| 1568 | /// container. The next example shows how you can store the |
---|
| 1569 | /// edges found by the Prim algorithm in a vector. |
---|
[25] | 1570 | /// |
---|
[80] | 1571 | /// \code |
---|
| 1572 | /// vector<Edge> span_tree_edges; |
---|
| 1573 | /// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges); |
---|
| 1574 | /// prim(graph, cost, inserter_map); |
---|
| 1575 | /// \endcode |
---|
[29] | 1576 | /// |
---|
[80] | 1577 | /// \sa StoreBoolMap |
---|
| 1578 | /// \sa FrontInserterBoolMap |
---|
| 1579 | /// \sa InserterBoolMap |
---|
[25] | 1580 | template <typename Container, |
---|
| 1581 | typename Functor = |
---|
| 1582 | _maps_bits::Identity<typename Container::value_type> > |
---|
| 1583 | class BackInserterBoolMap { |
---|
| 1584 | public: |
---|
[34] | 1585 | typedef typename Functor::argument_type Key; |
---|
[25] | 1586 | typedef bool Value; |
---|
| 1587 | |
---|
| 1588 | /// Constructor |
---|
[80] | 1589 | BackInserterBoolMap(Container& _container, |
---|
| 1590 | const Functor& _functor = Functor()) |
---|
[25] | 1591 | : container(_container), functor(_functor) {} |
---|
| 1592 | |
---|
[80] | 1593 | /// The set function of the map |
---|
[25] | 1594 | void set(const Key& key, Value value) { |
---|
| 1595 | if (value) { |
---|
| 1596 | container.push_back(functor(key)); |
---|
| 1597 | } |
---|
| 1598 | } |
---|
[80] | 1599 | |
---|
[25] | 1600 | private: |
---|
| 1601 | Container& container; |
---|
| 1602 | Functor functor; |
---|
| 1603 | }; |
---|
| 1604 | |
---|
[80] | 1605 | /// \brief Writable bool map for logging each \c true assigned element in |
---|
[25] | 1606 | /// a front insertable container. |
---|
| 1607 | /// |
---|
[29] | 1608 | /// Writable bool map for logging each \c true assigned element by pushing |
---|
| 1609 | /// them into a front insertable container. |
---|
| 1610 | /// It can be used to retrieve the items into a standard |
---|
| 1611 | /// container. For example see \ref BackInserterBoolMap. |
---|
| 1612 | /// |
---|
[80] | 1613 | /// \sa BackInserterBoolMap |
---|
| 1614 | /// \sa InserterBoolMap |
---|
[25] | 1615 | template <typename Container, |
---|
| 1616 | typename Functor = |
---|
| 1617 | _maps_bits::Identity<typename Container::value_type> > |
---|
| 1618 | class FrontInserterBoolMap { |
---|
| 1619 | public: |
---|
[34] | 1620 | typedef typename Functor::argument_type Key; |
---|
[25] | 1621 | typedef bool Value; |
---|
| 1622 | |
---|
| 1623 | /// Constructor |
---|
| 1624 | FrontInserterBoolMap(Container& _container, |
---|
[80] | 1625 | const Functor& _functor = Functor()) |
---|
[25] | 1626 | : container(_container), functor(_functor) {} |
---|
| 1627 | |
---|
[80] | 1628 | /// The set function of the map |
---|
[25] | 1629 | void set(const Key& key, Value value) { |
---|
| 1630 | if (value) { |
---|
[30] | 1631 | container.push_front(functor(key)); |
---|
[25] | 1632 | } |
---|
| 1633 | } |
---|
[80] | 1634 | |
---|
[25] | 1635 | private: |
---|
[80] | 1636 | Container& container; |
---|
[25] | 1637 | Functor functor; |
---|
| 1638 | }; |
---|
| 1639 | |
---|
[80] | 1640 | /// \brief Writable bool map for storing each \c true assigned element in |
---|
[25] | 1641 | /// an insertable container. |
---|
| 1642 | /// |
---|
[80] | 1643 | /// Writable bool map for storing each \c true assigned element in an |
---|
[25] | 1644 | /// insertable container. It will insert all the keys set to \c true into |
---|
[26] | 1645 | /// the container. |
---|
| 1646 | /// |
---|
| 1647 | /// For example, if you want to store the cut arcs of the strongly |
---|
[25] | 1648 | /// connected components in a set you can use the next code: |
---|
| 1649 | /// |
---|
[80] | 1650 | /// \code |
---|
| 1651 | /// set<Arc> cut_arcs; |
---|
| 1652 | /// InserterBoolMap<set<Arc> > inserter_map(cut_arcs); |
---|
| 1653 | /// stronglyConnectedCutArcs(digraph, cost, inserter_map); |
---|
| 1654 | /// \endcode |
---|
[29] | 1655 | /// |
---|
[80] | 1656 | /// \sa BackInserterBoolMap |
---|
| 1657 | /// \sa FrontInserterBoolMap |
---|
[25] | 1658 | template <typename Container, |
---|
| 1659 | typename Functor = |
---|
| 1660 | _maps_bits::Identity<typename Container::value_type> > |
---|
| 1661 | class InserterBoolMap { |
---|
| 1662 | public: |
---|
| 1663 | typedef typename Container::value_type Key; |
---|
| 1664 | typedef bool Value; |
---|
| 1665 | |
---|
[29] | 1666 | /// Constructor with specified iterator |
---|
[80] | 1667 | |
---|
[29] | 1668 | /// Constructor with specified iterator. |
---|
| 1669 | /// \param _container The container for storing the elements. |
---|
| 1670 | /// \param _it The elements will be inserted before this iterator. |
---|
| 1671 | /// \param _functor The functor that is used when an element is stored. |
---|
[25] | 1672 | InserterBoolMap(Container& _container, typename Container::iterator _it, |
---|
[80] | 1673 | const Functor& _functor = Functor()) |
---|
[25] | 1674 | : container(_container), it(_it), functor(_functor) {} |
---|
| 1675 | |
---|
| 1676 | /// Constructor |
---|
[29] | 1677 | |
---|
| 1678 | /// Constructor without specified iterator. |
---|
| 1679 | /// The elements will be inserted before <tt>_container.end()</tt>. |
---|
| 1680 | /// \param _container The container for storing the elements. |
---|
| 1681 | /// \param _functor The functor that is used when an element is stored. |
---|
[25] | 1682 | InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
---|
| 1683 | : container(_container), it(_container.end()), functor(_functor) {} |
---|
| 1684 | |
---|
[80] | 1685 | /// The set function of the map |
---|
[25] | 1686 | void set(const Key& key, Value value) { |
---|
| 1687 | if (value) { |
---|
[30] | 1688 | it = container.insert(it, functor(key)); |
---|
[25] | 1689 | ++it; |
---|
| 1690 | } |
---|
| 1691 | } |
---|
[80] | 1692 | |
---|
[25] | 1693 | private: |
---|
| 1694 | Container& container; |
---|
| 1695 | typename Container::iterator it; |
---|
| 1696 | Functor functor; |
---|
| 1697 | }; |
---|
| 1698 | |
---|
[80] | 1699 | /// \brief Writable bool map for filling each \c true assigned element with a |
---|
[29] | 1700 | /// given value. |
---|
[25] | 1701 | /// |
---|
[80] | 1702 | /// Writable bool map for filling each \c true assigned element with a |
---|
[29] | 1703 | /// given value. The value can set the container. |
---|
[25] | 1704 | /// |
---|
[26] | 1705 | /// The following code finds the connected components of a graph |
---|
[25] | 1706 | /// and stores it in the \c comp map: |
---|
[80] | 1707 | /// \code |
---|
| 1708 | /// typedef Graph::NodeMap<int> ComponentMap; |
---|
| 1709 | /// ComponentMap comp(graph); |
---|
| 1710 | /// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap; |
---|
| 1711 | /// ComponentFillerMap filler(comp, 0); |
---|
[25] | 1712 | /// |
---|
[80] | 1713 | /// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph); |
---|
| 1714 | /// dfs.processedMap(filler); |
---|
| 1715 | /// dfs.init(); |
---|
| 1716 | /// for (NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1717 | /// if (!dfs.reached(it)) { |
---|
| 1718 | /// dfs.addSource(it); |
---|
| 1719 | /// dfs.start(); |
---|
| 1720 | /// ++filler.fillValue(); |
---|
| 1721 | /// } |
---|
[25] | 1722 | /// } |
---|
[80] | 1723 | /// \endcode |
---|
[25] | 1724 | template <typename Map> |
---|
| 1725 | class FillBoolMap { |
---|
| 1726 | public: |
---|
| 1727 | typedef typename Map::Key Key; |
---|
| 1728 | typedef bool Value; |
---|
| 1729 | |
---|
| 1730 | /// Constructor |
---|
[80] | 1731 | FillBoolMap(Map& _map, const typename Map::Value& _fill) |
---|
[25] | 1732 | : map(_map), fill(_fill) {} |
---|
| 1733 | |
---|
| 1734 | /// Constructor |
---|
[80] | 1735 | FillBoolMap(Map& _map) |
---|
[25] | 1736 | : map(_map), fill() {} |
---|
| 1737 | |
---|
| 1738 | /// Gives back the current fill value |
---|
| 1739 | const typename Map::Value& fillValue() const { |
---|
| 1740 | return fill; |
---|
[80] | 1741 | } |
---|
[25] | 1742 | |
---|
| 1743 | /// Gives back the current fill value |
---|
| 1744 | typename Map::Value& fillValue() { |
---|
| 1745 | return fill; |
---|
[80] | 1746 | } |
---|
[25] | 1747 | |
---|
| 1748 | /// Sets the current fill value |
---|
| 1749 | void fillValue(const typename Map::Value& _fill) { |
---|
| 1750 | fill = _fill; |
---|
[80] | 1751 | } |
---|
[25] | 1752 | |
---|
[80] | 1753 | /// The set function of the map |
---|
[25] | 1754 | void set(const Key& key, Value value) { |
---|
| 1755 | if (value) { |
---|
| 1756 | map.set(key, fill); |
---|
| 1757 | } |
---|
| 1758 | } |
---|
[80] | 1759 | |
---|
[25] | 1760 | private: |
---|
| 1761 | Map& map; |
---|
| 1762 | typename Map::Value fill; |
---|
| 1763 | }; |
---|
| 1764 | |
---|
| 1765 | |
---|
[80] | 1766 | /// \brief Writable bool map for storing the sequence number of |
---|
| 1767 | /// \c true assignments. |
---|
| 1768 | /// |
---|
| 1769 | /// Writable bool map that stores for each \c true assigned elements |
---|
[26] | 1770 | /// the sequence number of this setting. |
---|
| 1771 | /// It makes it easy to calculate the leaving |
---|
[80] | 1772 | /// order of the nodes in the \ref Dfs algorithm. |
---|
[25] | 1773 | /// |
---|
[80] | 1774 | /// \code |
---|
| 1775 | /// typedef Digraph::NodeMap<int> OrderMap; |
---|
| 1776 | /// OrderMap order(digraph); |
---|
| 1777 | /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
---|
| 1778 | /// OrderSetterMap setter(order); |
---|
| 1779 | /// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph); |
---|
| 1780 | /// dfs.processedMap(setter); |
---|
| 1781 | /// dfs.init(); |
---|
| 1782 | /// for (NodeIt it(digraph); it != INVALID; ++it) { |
---|
| 1783 | /// if (!dfs.reached(it)) { |
---|
| 1784 | /// dfs.addSource(it); |
---|
| 1785 | /// dfs.start(); |
---|
| 1786 | /// } |
---|
[25] | 1787 | /// } |
---|
[80] | 1788 | /// \endcode |
---|
[25] | 1789 | /// |
---|
[26] | 1790 | /// The storing of the discovering order is more difficult because the |
---|
[25] | 1791 | /// ReachedMap should be readable in the dfs algorithm but the setting |
---|
[26] | 1792 | /// order map is not readable. Thus we must use the fork map: |
---|
[25] | 1793 | /// |
---|
[80] | 1794 | /// \code |
---|
| 1795 | /// typedef Digraph::NodeMap<int> OrderMap; |
---|
| 1796 | /// OrderMap order(digraph); |
---|
| 1797 | /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
---|
| 1798 | /// OrderSetterMap setter(order); |
---|
| 1799 | /// typedef Digraph::NodeMap<bool> StoreMap; |
---|
| 1800 | /// StoreMap store(digraph); |
---|
[25] | 1801 | /// |
---|
[80] | 1802 | /// typedef ForkMap<StoreMap, OrderSetterMap> ReachedMap; |
---|
| 1803 | /// ReachedMap reached(store, setter); |
---|
[25] | 1804 | /// |
---|
[80] | 1805 | /// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph); |
---|
| 1806 | /// dfs.reachedMap(reached); |
---|
| 1807 | /// dfs.init(); |
---|
| 1808 | /// for (NodeIt it(digraph); it != INVALID; ++it) { |
---|
| 1809 | /// if (!dfs.reached(it)) { |
---|
| 1810 | /// dfs.addSource(it); |
---|
| 1811 | /// dfs.start(); |
---|
| 1812 | /// } |
---|
[25] | 1813 | /// } |
---|
[80] | 1814 | /// \endcode |
---|
[25] | 1815 | template <typename Map> |
---|
| 1816 | class SettingOrderBoolMap { |
---|
| 1817 | public: |
---|
| 1818 | typedef typename Map::Key Key; |
---|
| 1819 | typedef bool Value; |
---|
| 1820 | |
---|
| 1821 | /// Constructor |
---|
[80] | 1822 | SettingOrderBoolMap(Map& _map) |
---|
[25] | 1823 | : map(_map), counter(0) {} |
---|
| 1824 | |
---|
| 1825 | /// Number of set operations. |
---|
| 1826 | int num() const { |
---|
| 1827 | return counter; |
---|
| 1828 | } |
---|
| 1829 | |
---|
[80] | 1830 | /// The set function of the map |
---|
[25] | 1831 | void set(const Key& key, Value value) { |
---|
| 1832 | if (value) { |
---|
| 1833 | map.set(key, counter++); |
---|
| 1834 | } |
---|
| 1835 | } |
---|
[80] | 1836 | |
---|
[25] | 1837 | private: |
---|
| 1838 | Map& map; |
---|
| 1839 | int counter; |
---|
| 1840 | }; |
---|
| 1841 | |
---|
| 1842 | /// @} |
---|
| 1843 | } |
---|
| 1844 | |
---|
| 1845 | #endif // LEMON_MAPS_H |
---|