[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[25] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[25] | 4 | * |
---|
[463] | 5 | * Copyright (C) 2003-2009 |
---|
[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> |
---|
[741] | 25 | #include <map> |
---|
[25] | 26 | |
---|
[220] | 27 | #include <lemon/core.h> |
---|
[25] | 28 | |
---|
| 29 | ///\file |
---|
| 30 | ///\ingroup maps |
---|
| 31 | ///\brief Miscellaneous property maps |
---|
[80] | 32 | |
---|
[25] | 33 | namespace lemon { |
---|
| 34 | |
---|
| 35 | /// \addtogroup maps |
---|
| 36 | /// @{ |
---|
| 37 | |
---|
| 38 | /// Base class of maps. |
---|
| 39 | |
---|
[80] | 40 | /// Base class of maps. It provides the necessary type definitions |
---|
| 41 | /// required by the map %concepts. |
---|
| 42 | template<typename K, typename V> |
---|
[25] | 43 | class MapBase { |
---|
| 44 | public: |
---|
[313] | 45 | /// \brief The key type of the map. |
---|
[25] | 46 | typedef K Key; |
---|
[80] | 47 | /// \brief The value type of the map. |
---|
| 48 | /// (The type of objects associated with the keys). |
---|
| 49 | typedef V Value; |
---|
[25] | 50 | }; |
---|
| 51 | |
---|
[80] | 52 | |
---|
[25] | 53 | /// Null map. (a.k.a. DoNothingMap) |
---|
| 54 | |
---|
[29] | 55 | /// This map can be used if you have to provide a map only for |
---|
[80] | 56 | /// its type definitions, or if you have to provide a writable map, |
---|
| 57 | /// but data written to it is not required (i.e. it will be sent to |
---|
[29] | 58 | /// <tt>/dev/null</tt>). |
---|
[80] | 59 | /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
---|
| 60 | /// |
---|
| 61 | /// \sa ConstMap |
---|
| 62 | template<typename K, typename V> |
---|
| 63 | class NullMap : public MapBase<K, V> { |
---|
[25] | 64 | public: |
---|
[606] | 65 | ///\e |
---|
| 66 | typedef K Key; |
---|
| 67 | ///\e |
---|
| 68 | typedef V Value; |
---|
[80] | 69 | |
---|
[25] | 70 | /// Gives back a default constructed element. |
---|
[80] | 71 | Value operator[](const Key&) const { return Value(); } |
---|
[25] | 72 | /// Absorbs the value. |
---|
[80] | 73 | void set(const Key&, const Value&) {} |
---|
[25] | 74 | }; |
---|
| 75 | |
---|
[301] | 76 | /// Returns a \c NullMap class |
---|
| 77 | |
---|
| 78 | /// This function just returns a \c NullMap class. |
---|
[80] | 79 | /// \relates NullMap |
---|
| 80 | template <typename K, typename V> |
---|
[25] | 81 | NullMap<K, V> nullMap() { |
---|
| 82 | return NullMap<K, V>(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | /// Constant map. |
---|
| 87 | |
---|
[82] | 88 | /// This \ref concepts::ReadMap "readable map" assigns a specified |
---|
| 89 | /// value to each key. |
---|
[80] | 90 | /// |
---|
[301] | 91 | /// In other aspects it is equivalent to \c NullMap. |
---|
[80] | 92 | /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
---|
| 93 | /// concept, but it absorbs the data written to it. |
---|
| 94 | /// |
---|
| 95 | /// The simplest way of using this map is through the constMap() |
---|
| 96 | /// function. |
---|
| 97 | /// |
---|
| 98 | /// \sa NullMap |
---|
| 99 | /// \sa IdentityMap |
---|
| 100 | template<typename K, typename V> |
---|
| 101 | class ConstMap : public MapBase<K, V> { |
---|
[25] | 102 | private: |
---|
[80] | 103 | V _value; |
---|
[25] | 104 | public: |
---|
[606] | 105 | ///\e |
---|
| 106 | typedef K Key; |
---|
| 107 | ///\e |
---|
| 108 | typedef V Value; |
---|
[25] | 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. |
---|
[123] | 119 | /// \param v The initial value of the map. |
---|
[80] | 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 | |
---|
[301] | 137 | /// Returns a \c ConstMap class |
---|
| 138 | |
---|
| 139 | /// This function just returns a \c ConstMap class. |
---|
[80] | 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 | |
---|
[123] | 146 | template<typename K, typename V> |
---|
| 147 | inline ConstMap<K, V> constMap() { |
---|
| 148 | return ConstMap<K, V>(); |
---|
| 149 | } |
---|
| 150 | |
---|
[25] | 151 | |
---|
| 152 | template<typename T, T v> |
---|
[80] | 153 | struct Const {}; |
---|
[25] | 154 | |
---|
| 155 | /// Constant map with inlined constant value. |
---|
| 156 | |
---|
[82] | 157 | /// This \ref concepts::ReadMap "readable map" assigns a specified |
---|
| 158 | /// value to each key. |
---|
[80] | 159 | /// |
---|
[301] | 160 | /// In other aspects it is equivalent to \c NullMap. |
---|
[80] | 161 | /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
---|
| 162 | /// concept, but it absorbs the data written to it. |
---|
| 163 | /// |
---|
| 164 | /// The simplest way of using this map is through the constMap() |
---|
| 165 | /// function. |
---|
| 166 | /// |
---|
| 167 | /// \sa NullMap |
---|
| 168 | /// \sa IdentityMap |
---|
[25] | 169 | template<typename K, typename V, V v> |
---|
| 170 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
---|
| 171 | public: |
---|
[606] | 172 | ///\e |
---|
| 173 | typedef K Key; |
---|
| 174 | ///\e |
---|
| 175 | typedef V Value; |
---|
[25] | 176 | |
---|
[80] | 177 | /// Constructor. |
---|
| 178 | ConstMap() {} |
---|
| 179 | |
---|
| 180 | /// Gives back the specified value. |
---|
| 181 | Value operator[](const Key&) const { return v; } |
---|
| 182 | |
---|
| 183 | /// Absorbs the value. |
---|
| 184 | void set(const Key&, const Value&) {} |
---|
[25] | 185 | }; |
---|
| 186 | |
---|
[301] | 187 | /// Returns a \c ConstMap class with inlined constant value |
---|
| 188 | |
---|
| 189 | /// This function just returns a \c ConstMap class with inlined |
---|
[80] | 190 | /// constant value. |
---|
| 191 | /// \relates ConstMap |
---|
| 192 | template<typename K, typename V, V v> |
---|
[25] | 193 | inline ConstMap<K, Const<V, v> > constMap() { |
---|
| 194 | return ConstMap<K, Const<V, v> >(); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
[82] | 198 | /// Identity map. |
---|
| 199 | |
---|
| 200 | /// This \ref concepts::ReadMap "read-only map" gives back the given |
---|
| 201 | /// key as value without any modification. |
---|
[80] | 202 | /// |
---|
| 203 | /// \sa ConstMap |
---|
| 204 | template <typename T> |
---|
| 205 | class IdentityMap : public MapBase<T, T> { |
---|
| 206 | public: |
---|
[606] | 207 | ///\e |
---|
| 208 | typedef T Key; |
---|
| 209 | ///\e |
---|
| 210 | typedef T Value; |
---|
[80] | 211 | |
---|
| 212 | /// Gives back the given value without any modification. |
---|
[82] | 213 | Value operator[](const Key &k) const { |
---|
| 214 | return k; |
---|
[80] | 215 | } |
---|
| 216 | }; |
---|
| 217 | |
---|
[301] | 218 | /// Returns an \c IdentityMap class |
---|
| 219 | |
---|
| 220 | /// This function just returns an \c IdentityMap class. |
---|
[80] | 221 | /// \relates IdentityMap |
---|
| 222 | template<typename T> |
---|
| 223 | inline IdentityMap<T> identityMap() { |
---|
| 224 | return IdentityMap<T>(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | /// \brief Map for storing values for integer keys from the range |
---|
| 229 | /// <tt>[0..size-1]</tt>. |
---|
| 230 | /// |
---|
| 231 | /// This map is essentially a wrapper for \c std::vector. It assigns |
---|
| 232 | /// values to integer keys from the range <tt>[0..size-1]</tt>. |
---|
| 233 | /// It can be used with some data structures, for example |
---|
[301] | 234 | /// \c UnionFind, \c BinHeap, when the used items are small |
---|
[80] | 235 | /// integers. This map conforms the \ref concepts::ReferenceMap |
---|
| 236 | /// "ReferenceMap" concept. |
---|
| 237 | /// |
---|
| 238 | /// The simplest way of using this map is through the rangeMap() |
---|
| 239 | /// function. |
---|
| 240 | template <typename V> |
---|
| 241 | class RangeMap : public MapBase<int, V> { |
---|
| 242 | template <typename V1> |
---|
| 243 | friend class RangeMap; |
---|
| 244 | private: |
---|
| 245 | |
---|
| 246 | typedef std::vector<V> Vector; |
---|
| 247 | Vector _vector; |
---|
| 248 | |
---|
[25] | 249 | public: |
---|
| 250 | |
---|
[80] | 251 | /// Key type |
---|
[606] | 252 | typedef int Key; |
---|
[80] | 253 | /// Value type |
---|
[606] | 254 | typedef V Value; |
---|
[80] | 255 | /// Reference type |
---|
| 256 | typedef typename Vector::reference Reference; |
---|
| 257 | /// Const reference type |
---|
| 258 | typedef typename Vector::const_reference ConstReference; |
---|
| 259 | |
---|
| 260 | typedef True ReferenceMapTag; |
---|
| 261 | |
---|
| 262 | public: |
---|
| 263 | |
---|
| 264 | /// Constructor with specified default value. |
---|
| 265 | RangeMap(int size = 0, const Value &value = Value()) |
---|
| 266 | : _vector(size, value) {} |
---|
| 267 | |
---|
| 268 | /// Constructs the map from an appropriate \c std::vector. |
---|
| 269 | template <typename V1> |
---|
| 270 | RangeMap(const std::vector<V1>& vector) |
---|
| 271 | : _vector(vector.begin(), vector.end()) {} |
---|
| 272 | |
---|
[301] | 273 | /// Constructs the map from another \c RangeMap. |
---|
[80] | 274 | template <typename V1> |
---|
| 275 | RangeMap(const RangeMap<V1> &c) |
---|
| 276 | : _vector(c._vector.begin(), c._vector.end()) {} |
---|
| 277 | |
---|
| 278 | /// Returns the size of the map. |
---|
| 279 | int size() { |
---|
| 280 | return _vector.size(); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | /// Resizes the map. |
---|
| 284 | |
---|
| 285 | /// Resizes the underlying \c std::vector container, so changes the |
---|
| 286 | /// keyset of the map. |
---|
| 287 | /// \param size The new size of the map. The new keyset will be the |
---|
| 288 | /// range <tt>[0..size-1]</tt>. |
---|
| 289 | /// \param value The default value to assign to the new keys. |
---|
| 290 | void resize(int size, const Value &value = Value()) { |
---|
| 291 | _vector.resize(size, value); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | private: |
---|
| 295 | |
---|
| 296 | RangeMap& operator=(const RangeMap&); |
---|
| 297 | |
---|
| 298 | public: |
---|
| 299 | |
---|
| 300 | ///\e |
---|
| 301 | Reference operator[](const Key &k) { |
---|
| 302 | return _vector[k]; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | ///\e |
---|
| 306 | ConstReference operator[](const Key &k) const { |
---|
| 307 | return _vector[k]; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | ///\e |
---|
| 311 | void set(const Key &k, const Value &v) { |
---|
| 312 | _vector[k] = v; |
---|
| 313 | } |
---|
| 314 | }; |
---|
| 315 | |
---|
[301] | 316 | /// Returns a \c RangeMap class |
---|
| 317 | |
---|
| 318 | /// This function just returns a \c RangeMap class. |
---|
[80] | 319 | /// \relates RangeMap |
---|
| 320 | template<typename V> |
---|
| 321 | inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) { |
---|
| 322 | return RangeMap<V>(size, value); |
---|
| 323 | } |
---|
| 324 | |
---|
[301] | 325 | /// \brief Returns a \c RangeMap class created from an appropriate |
---|
[80] | 326 | /// \c std::vector |
---|
| 327 | |
---|
[301] | 328 | /// This function just returns a \c RangeMap class created from an |
---|
[80] | 329 | /// appropriate \c std::vector. |
---|
| 330 | /// \relates RangeMap |
---|
| 331 | template<typename V> |
---|
| 332 | inline RangeMap<V> rangeMap(const std::vector<V> &vector) { |
---|
| 333 | return RangeMap<V>(vector); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | |
---|
| 337 | /// Map type based on \c std::map |
---|
| 338 | |
---|
| 339 | /// This map is essentially a wrapper for \c std::map with addition |
---|
| 340 | /// that you can specify a default value for the keys that are not |
---|
| 341 | /// stored actually. This value can be different from the default |
---|
| 342 | /// contructed value (i.e. \c %Value()). |
---|
| 343 | /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap" |
---|
| 344 | /// concept. |
---|
| 345 | /// |
---|
| 346 | /// This map is useful if a default value should be assigned to most of |
---|
| 347 | /// the keys and different values should be assigned only to a few |
---|
| 348 | /// keys (i.e. the map is "sparse"). |
---|
| 349 | /// The name of this type also refers to this important usage. |
---|
| 350 | /// |
---|
| 351 | /// Apart form that this map can be used in many other cases since it |
---|
| 352 | /// is based on \c std::map, which is a general associative container. |
---|
| 353 | /// However keep in mind that it is usually not as efficient as other |
---|
| 354 | /// maps. |
---|
| 355 | /// |
---|
| 356 | /// The simplest way of using this map is through the sparseMap() |
---|
| 357 | /// function. |
---|
[606] | 358 | template <typename K, typename V, typename Comp = std::less<K> > |
---|
[80] | 359 | class SparseMap : public MapBase<K, V> { |
---|
| 360 | template <typename K1, typename V1, typename C1> |
---|
| 361 | friend class SparseMap; |
---|
| 362 | public: |
---|
| 363 | |
---|
| 364 | /// Key type |
---|
[606] | 365 | typedef K Key; |
---|
[80] | 366 | /// Value type |
---|
[606] | 367 | typedef V Value; |
---|
[80] | 368 | /// Reference type |
---|
| 369 | typedef Value& Reference; |
---|
| 370 | /// Const reference type |
---|
| 371 | typedef const Value& ConstReference; |
---|
[25] | 372 | |
---|
[45] | 373 | typedef True ReferenceMapTag; |
---|
| 374 | |
---|
[25] | 375 | private: |
---|
[80] | 376 | |
---|
[606] | 377 | typedef std::map<K, V, Comp> Map; |
---|
[80] | 378 | Map _map; |
---|
[25] | 379 | Value _value; |
---|
| 380 | |
---|
| 381 | public: |
---|
| 382 | |
---|
[80] | 383 | /// \brief Constructor with specified default value. |
---|
| 384 | SparseMap(const Value &value = Value()) : _value(value) {} |
---|
| 385 | /// \brief Constructs the map from an appropriate \c std::map, and |
---|
[47] | 386 | /// explicitly specifies a default value. |
---|
[80] | 387 | template <typename V1, typename Comp1> |
---|
| 388 | SparseMap(const std::map<Key, V1, Comp1> &map, |
---|
| 389 | const Value &value = Value()) |
---|
[25] | 390 | : _map(map.begin(), map.end()), _value(value) {} |
---|
[80] | 391 | |
---|
[301] | 392 | /// \brief Constructs the map from another \c SparseMap. |
---|
[80] | 393 | template<typename V1, typename Comp1> |
---|
| 394 | SparseMap(const SparseMap<Key, V1, Comp1> &c) |
---|
[25] | 395 | : _map(c._map.begin(), c._map.end()), _value(c._value) {} |
---|
| 396 | |
---|
| 397 | private: |
---|
| 398 | |
---|
[80] | 399 | SparseMap& operator=(const SparseMap&); |
---|
[25] | 400 | |
---|
| 401 | public: |
---|
| 402 | |
---|
| 403 | ///\e |
---|
| 404 | Reference operator[](const Key &k) { |
---|
| 405 | typename Map::iterator it = _map.lower_bound(k); |
---|
| 406 | if (it != _map.end() && !_map.key_comp()(k, it->first)) |
---|
[209] | 407 | return it->second; |
---|
[25] | 408 | else |
---|
[209] | 409 | return _map.insert(it, std::make_pair(k, _value))->second; |
---|
[25] | 410 | } |
---|
| 411 | |
---|
[80] | 412 | ///\e |
---|
[25] | 413 | ConstReference operator[](const Key &k) const { |
---|
| 414 | typename Map::const_iterator it = _map.find(k); |
---|
| 415 | if (it != _map.end()) |
---|
[209] | 416 | return it->second; |
---|
[25] | 417 | else |
---|
[209] | 418 | return _value; |
---|
[25] | 419 | } |
---|
| 420 | |
---|
[80] | 421 | ///\e |
---|
| 422 | void set(const Key &k, const Value &v) { |
---|
[25] | 423 | typename Map::iterator it = _map.lower_bound(k); |
---|
| 424 | if (it != _map.end() && !_map.key_comp()(k, it->first)) |
---|
[209] | 425 | it->second = v; |
---|
[25] | 426 | else |
---|
[209] | 427 | _map.insert(it, std::make_pair(k, v)); |
---|
[25] | 428 | } |
---|
| 429 | |
---|
[80] | 430 | ///\e |
---|
| 431 | void setAll(const Value &v) { |
---|
| 432 | _value = v; |
---|
[25] | 433 | _map.clear(); |
---|
[80] | 434 | } |
---|
| 435 | }; |
---|
[25] | 436 | |
---|
[301] | 437 | /// Returns a \c SparseMap class |
---|
| 438 | |
---|
| 439 | /// This function just returns a \c SparseMap class with specified |
---|
[80] | 440 | /// default value. |
---|
| 441 | /// \relates SparseMap |
---|
| 442 | template<typename K, typename V, typename Compare> |
---|
| 443 | inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) { |
---|
| 444 | return SparseMap<K, V, Compare>(value); |
---|
[54] | 445 | } |
---|
[45] | 446 | |
---|
[80] | 447 | template<typename K, typename V> |
---|
| 448 | inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) { |
---|
| 449 | return SparseMap<K, V, std::less<K> >(value); |
---|
[45] | 450 | } |
---|
[25] | 451 | |
---|
[301] | 452 | /// \brief Returns a \c SparseMap class created from an appropriate |
---|
[80] | 453 | /// \c std::map |
---|
[25] | 454 | |
---|
[301] | 455 | /// This function just returns a \c SparseMap class created from an |
---|
[80] | 456 | /// appropriate \c std::map. |
---|
| 457 | /// \relates SparseMap |
---|
| 458 | template<typename K, typename V, typename Compare> |
---|
| 459 | inline SparseMap<K, V, Compare> |
---|
| 460 | sparseMap(const std::map<K, V, Compare> &map, const V& value = V()) |
---|
| 461 | { |
---|
| 462 | return SparseMap<K, V, Compare>(map, value); |
---|
[45] | 463 | } |
---|
[25] | 464 | |
---|
| 465 | /// @} |
---|
| 466 | |
---|
| 467 | /// \addtogroup map_adaptors |
---|
| 468 | /// @{ |
---|
| 469 | |
---|
[80] | 470 | /// Composition of two maps |
---|
| 471 | |
---|
[82] | 472 | /// This \ref concepts::ReadMap "read-only map" returns the |
---|
[80] | 473 | /// composition of two given maps. That is to say, if \c m1 is of |
---|
| 474 | /// type \c M1 and \c m2 is of \c M2, then for |
---|
| 475 | /// \code |
---|
| 476 | /// ComposeMap<M1, M2> cm(m1,m2); |
---|
| 477 | /// \endcode |
---|
| 478 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
---|
[25] | 479 | /// |
---|
[80] | 480 | /// The \c Key type of the map is inherited from \c M2 and the |
---|
| 481 | /// \c Value type is from \c M1. |
---|
| 482 | /// \c M2::Value must be convertible to \c M1::Key. |
---|
| 483 | /// |
---|
| 484 | /// The simplest way of using this map is through the composeMap() |
---|
| 485 | /// function. |
---|
| 486 | /// |
---|
| 487 | /// \sa CombineMap |
---|
| 488 | template <typename M1, typename M2> |
---|
| 489 | class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
---|
| 490 | const M1 &_m1; |
---|
| 491 | const M2 &_m2; |
---|
[25] | 492 | public: |
---|
[606] | 493 | ///\e |
---|
| 494 | typedef typename M2::Key Key; |
---|
| 495 | ///\e |
---|
| 496 | typedef typename M1::Value Value; |
---|
[25] | 497 | |
---|
[80] | 498 | /// Constructor |
---|
| 499 | ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 500 | |
---|
[606] | 501 | ///\e |
---|
[80] | 502 | typename MapTraits<M1>::ConstReturnValue |
---|
| 503 | operator[](const Key &k) const { return _m1[_m2[k]]; } |
---|
[25] | 504 | }; |
---|
| 505 | |
---|
[301] | 506 | /// Returns a \c ComposeMap class |
---|
| 507 | |
---|
| 508 | /// This function just returns a \c ComposeMap class. |
---|
[80] | 509 | /// |
---|
| 510 | /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is |
---|
| 511 | /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt> |
---|
| 512 | /// will be equal to <tt>m1[m2[x]]</tt>. |
---|
| 513 | /// |
---|
| 514 | /// \relates ComposeMap |
---|
| 515 | template <typename M1, typename M2> |
---|
| 516 | inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) { |
---|
| 517 | return ComposeMap<M1, M2>(m1, m2); |
---|
[25] | 518 | } |
---|
| 519 | |
---|
[80] | 520 | |
---|
| 521 | /// Combination of two maps using an STL (binary) functor. |
---|
| 522 | |
---|
[82] | 523 | /// This \ref concepts::ReadMap "read-only map" takes two maps and a |
---|
[80] | 524 | /// binary functor and returns the combination of the two given maps |
---|
| 525 | /// using the functor. |
---|
| 526 | /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 |
---|
| 527 | /// and \c f is of \c F, then for |
---|
| 528 | /// \code |
---|
| 529 | /// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
---|
| 530 | /// \endcode |
---|
| 531 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>. |
---|
[26] | 532 | /// |
---|
[80] | 533 | /// The \c Key type of the map is inherited from \c M1 (\c M1::Key |
---|
| 534 | /// must be convertible to \c M2::Key) and the \c Value type is \c V. |
---|
| 535 | /// \c M2::Value and \c M1::Value must be convertible to the |
---|
| 536 | /// corresponding input parameter of \c F and the return type of \c F |
---|
| 537 | /// must be convertible to \c V. |
---|
| 538 | /// |
---|
| 539 | /// The simplest way of using this map is through the combineMap() |
---|
| 540 | /// function. |
---|
| 541 | /// |
---|
| 542 | /// \sa ComposeMap |
---|
| 543 | template<typename M1, typename M2, typename F, |
---|
[209] | 544 | typename V = typename F::result_type> |
---|
[80] | 545 | class CombineMap : public MapBase<typename M1::Key, V> { |
---|
| 546 | const M1 &_m1; |
---|
| 547 | const M2 &_m2; |
---|
| 548 | F _f; |
---|
[25] | 549 | public: |
---|
[606] | 550 | ///\e |
---|
| 551 | typedef typename M1::Key Key; |
---|
| 552 | ///\e |
---|
| 553 | typedef V Value; |
---|
[25] | 554 | |
---|
[80] | 555 | /// Constructor |
---|
| 556 | CombineMap(const M1 &m1, const M2 &m2, const F &f = F()) |
---|
| 557 | : _m1(m1), _m2(m2), _f(f) {} |
---|
[606] | 558 | ///\e |
---|
[80] | 559 | Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); } |
---|
| 560 | }; |
---|
[25] | 561 | |
---|
[301] | 562 | /// Returns a \c CombineMap class |
---|
| 563 | |
---|
| 564 | /// This function just returns a \c CombineMap class. |
---|
[80] | 565 | /// |
---|
| 566 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 567 | /// values, then |
---|
| 568 | /// \code |
---|
| 569 | /// combineMap(m1,m2,std::plus<double>()) |
---|
| 570 | /// \endcode |
---|
| 571 | /// is equivalent to |
---|
| 572 | /// \code |
---|
| 573 | /// addMap(m1,m2) |
---|
| 574 | /// \endcode |
---|
| 575 | /// |
---|
| 576 | /// This function is specialized for adaptable binary function |
---|
| 577 | /// classes and C++ functions. |
---|
| 578 | /// |
---|
| 579 | /// \relates CombineMap |
---|
| 580 | template<typename M1, typename M2, typename F, typename V> |
---|
| 581 | inline CombineMap<M1, M2, F, V> |
---|
| 582 | combineMap(const M1 &m1, const M2 &m2, const F &f) { |
---|
| 583 | return CombineMap<M1, M2, F, V>(m1,m2,f); |
---|
[25] | 584 | } |
---|
| 585 | |
---|
[80] | 586 | template<typename M1, typename M2, typename F> |
---|
| 587 | inline CombineMap<M1, M2, F, typename F::result_type> |
---|
| 588 | combineMap(const M1 &m1, const M2 &m2, const F &f) { |
---|
| 589 | return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
---|
| 590 | } |
---|
[25] | 591 | |
---|
[80] | 592 | template<typename M1, typename M2, typename K1, typename K2, typename V> |
---|
| 593 | inline CombineMap<M1, M2, V (*)(K1, K2), V> |
---|
| 594 | combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
---|
| 595 | return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | |
---|
| 599 | /// Converts an STL style (unary) functor to a map |
---|
| 600 | |
---|
[82] | 601 | /// This \ref concepts::ReadMap "read-only map" returns the value |
---|
[80] | 602 | /// of a given functor. Actually, it just wraps the functor and |
---|
| 603 | /// provides the \c Key and \c Value typedefs. |
---|
[26] | 604 | /// |
---|
[80] | 605 | /// Template parameters \c K and \c V will become its \c Key and |
---|
| 606 | /// \c Value. In most cases they have to be given explicitly because |
---|
| 607 | /// a functor typically does not provide \c argument_type and |
---|
| 608 | /// \c result_type typedefs. |
---|
| 609 | /// Parameter \c F is the type of the used functor. |
---|
[29] | 610 | /// |
---|
[80] | 611 | /// The simplest way of using this map is through the functorToMap() |
---|
| 612 | /// function. |
---|
| 613 | /// |
---|
| 614 | /// \sa MapToFunctor |
---|
| 615 | template<typename F, |
---|
[209] | 616 | typename K = typename F::argument_type, |
---|
| 617 | typename V = typename F::result_type> |
---|
[80] | 618 | class FunctorToMap : public MapBase<K, V> { |
---|
[123] | 619 | F _f; |
---|
[80] | 620 | public: |
---|
[606] | 621 | ///\e |
---|
| 622 | typedef K Key; |
---|
| 623 | ///\e |
---|
| 624 | typedef V Value; |
---|
[25] | 625 | |
---|
[80] | 626 | /// Constructor |
---|
| 627 | FunctorToMap(const F &f = F()) : _f(f) {} |
---|
[606] | 628 | ///\e |
---|
[80] | 629 | Value operator[](const Key &k) const { return _f(k); } |
---|
| 630 | }; |
---|
| 631 | |
---|
[301] | 632 | /// Returns a \c FunctorToMap class |
---|
| 633 | |
---|
| 634 | /// This function just returns a \c FunctorToMap class. |
---|
[80] | 635 | /// |
---|
| 636 | /// This function is specialized for adaptable binary function |
---|
| 637 | /// classes and C++ functions. |
---|
| 638 | /// |
---|
| 639 | /// \relates FunctorToMap |
---|
| 640 | template<typename K, typename V, typename F> |
---|
| 641 | inline FunctorToMap<F, K, V> functorToMap(const F &f) { |
---|
| 642 | return FunctorToMap<F, K, V>(f); |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | template <typename F> |
---|
| 646 | inline FunctorToMap<F, typename F::argument_type, typename F::result_type> |
---|
| 647 | functorToMap(const F &f) |
---|
| 648 | { |
---|
| 649 | return FunctorToMap<F, typename F::argument_type, |
---|
| 650 | typename F::result_type>(f); |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | template <typename K, typename V> |
---|
| 654 | inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) { |
---|
| 655 | return FunctorToMap<V (*)(K), K, V>(f); |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | |
---|
| 659 | /// Converts a map to an STL style (unary) functor |
---|
| 660 | |
---|
| 661 | /// This class converts a map to an STL style (unary) functor. |
---|
| 662 | /// That is it provides an <tt>operator()</tt> to read its values. |
---|
| 663 | /// |
---|
| 664 | /// For the sake of convenience it also works as a usual |
---|
| 665 | /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt> |
---|
| 666 | /// and the \c Key and \c Value typedefs also exist. |
---|
| 667 | /// |
---|
| 668 | /// The simplest way of using this map is through the mapToFunctor() |
---|
| 669 | /// function. |
---|
| 670 | /// |
---|
| 671 | ///\sa FunctorToMap |
---|
| 672 | template <typename M> |
---|
| 673 | class MapToFunctor : public MapBase<typename M::Key, typename M::Value> { |
---|
| 674 | const M &_m; |
---|
[25] | 675 | public: |
---|
[606] | 676 | ///\e |
---|
| 677 | typedef typename M::Key Key; |
---|
| 678 | ///\e |
---|
| 679 | typedef typename M::Value Value; |
---|
| 680 | |
---|
| 681 | typedef typename M::Key argument_type; |
---|
| 682 | typedef typename M::Value result_type; |
---|
[80] | 683 | |
---|
| 684 | /// Constructor |
---|
| 685 | MapToFunctor(const M &m) : _m(m) {} |
---|
[606] | 686 | ///\e |
---|
[80] | 687 | Value operator()(const Key &k) const { return _m[k]; } |
---|
[606] | 688 | ///\e |
---|
[80] | 689 | Value operator[](const Key &k) const { return _m[k]; } |
---|
[25] | 690 | }; |
---|
[45] | 691 | |
---|
[301] | 692 | /// Returns a \c MapToFunctor class |
---|
| 693 | |
---|
| 694 | /// This function just returns a \c MapToFunctor class. |
---|
[80] | 695 | /// \relates MapToFunctor |
---|
[45] | 696 | template<typename M> |
---|
[80] | 697 | inline MapToFunctor<M> mapToFunctor(const M &m) { |
---|
| 698 | return MapToFunctor<M>(m); |
---|
[45] | 699 | } |
---|
[25] | 700 | |
---|
| 701 | |
---|
[80] | 702 | /// \brief Map adaptor to convert the \c Value type of a map to |
---|
| 703 | /// another type using the default conversion. |
---|
| 704 | |
---|
| 705 | /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap |
---|
| 706 | /// "readable map" to another type using the default conversion. |
---|
| 707 | /// The \c Key type of it is inherited from \c M and the \c Value |
---|
| 708 | /// type is \c V. |
---|
| 709 | /// This type conforms the \ref concepts::ReadMap "ReadMap" concept. |
---|
[26] | 710 | /// |
---|
[80] | 711 | /// The simplest way of using this map is through the convertMap() |
---|
| 712 | /// function. |
---|
| 713 | template <typename M, typename V> |
---|
| 714 | class ConvertMap : public MapBase<typename M::Key, V> { |
---|
| 715 | const M &_m; |
---|
| 716 | public: |
---|
[606] | 717 | ///\e |
---|
| 718 | typedef typename M::Key Key; |
---|
| 719 | ///\e |
---|
| 720 | typedef V Value; |
---|
[80] | 721 | |
---|
| 722 | /// Constructor |
---|
| 723 | |
---|
| 724 | /// Constructor. |
---|
| 725 | /// \param m The underlying map. |
---|
| 726 | ConvertMap(const M &m) : _m(m) {} |
---|
| 727 | |
---|
[606] | 728 | ///\e |
---|
[80] | 729 | Value operator[](const Key &k) const { return _m[k]; } |
---|
| 730 | }; |
---|
| 731 | |
---|
[301] | 732 | /// Returns a \c ConvertMap class |
---|
| 733 | |
---|
| 734 | /// This function just returns a \c ConvertMap class. |
---|
[80] | 735 | /// \relates ConvertMap |
---|
| 736 | template<typename V, typename M> |
---|
| 737 | inline ConvertMap<M, V> convertMap(const M &map) { |
---|
| 738 | return ConvertMap<M, V>(map); |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | |
---|
| 742 | /// Applies all map setting operations to two maps |
---|
| 743 | |
---|
| 744 | /// This map has two \ref concepts::WriteMap "writable map" parameters |
---|
| 745 | /// and each write request will be passed to both of them. |
---|
| 746 | /// If \c M1 is also \ref concepts::ReadMap "readable", then the read |
---|
| 747 | /// operations will return the corresponding values of \c M1. |
---|
[29] | 748 | /// |
---|
[80] | 749 | /// The \c Key and \c Value types are inherited from \c M1. |
---|
| 750 | /// The \c Key and \c Value of \c M2 must be convertible from those |
---|
| 751 | /// of \c M1. |
---|
| 752 | /// |
---|
| 753 | /// The simplest way of using this map is through the forkMap() |
---|
| 754 | /// function. |
---|
| 755 | template<typename M1, typename M2> |
---|
| 756 | class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 757 | M1 &_m1; |
---|
| 758 | M2 &_m2; |
---|
| 759 | public: |
---|
[606] | 760 | ///\e |
---|
| 761 | typedef typename M1::Key Key; |
---|
| 762 | ///\e |
---|
| 763 | typedef typename M1::Value Value; |
---|
[25] | 764 | |
---|
[80] | 765 | /// Constructor |
---|
| 766 | ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {} |
---|
| 767 | /// Returns the value associated with the given key in the first map. |
---|
| 768 | Value operator[](const Key &k) const { return _m1[k]; } |
---|
| 769 | /// Sets the value associated with the given key in both maps. |
---|
| 770 | void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); } |
---|
| 771 | }; |
---|
| 772 | |
---|
[301] | 773 | /// Returns a \c ForkMap class |
---|
| 774 | |
---|
| 775 | /// This function just returns a \c ForkMap class. |
---|
[80] | 776 | /// \relates ForkMap |
---|
| 777 | template <typename M1, typename M2> |
---|
| 778 | inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) { |
---|
| 779 | return ForkMap<M1,M2>(m1,m2); |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | |
---|
| 783 | /// Sum of two maps |
---|
| 784 | |
---|
[82] | 785 | /// This \ref concepts::ReadMap "read-only map" returns the sum |
---|
[80] | 786 | /// of the values of the two given maps. |
---|
| 787 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 788 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 789 | /// \c M1. |
---|
| 790 | /// |
---|
| 791 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 792 | /// \code |
---|
| 793 | /// AddMap<M1,M2> am(m1,m2); |
---|
| 794 | /// \endcode |
---|
| 795 | /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>. |
---|
| 796 | /// |
---|
| 797 | /// The simplest way of using this map is through the addMap() |
---|
| 798 | /// function. |
---|
| 799 | /// |
---|
| 800 | /// \sa SubMap, MulMap, DivMap |
---|
| 801 | /// \sa ShiftMap, ShiftWriteMap |
---|
| 802 | template<typename M1, typename M2> |
---|
[25] | 803 | class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
[80] | 804 | const M1 &_m1; |
---|
| 805 | const M2 &_m2; |
---|
[25] | 806 | public: |
---|
[606] | 807 | ///\e |
---|
| 808 | typedef typename M1::Key Key; |
---|
| 809 | ///\e |
---|
| 810 | typedef typename M1::Value Value; |
---|
[25] | 811 | |
---|
[80] | 812 | /// Constructor |
---|
| 813 | AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 814 | ///\e |
---|
[80] | 815 | Value operator[](const Key &k) const { return _m1[k]+_m2[k]; } |
---|
[25] | 816 | }; |
---|
| 817 | |
---|
[301] | 818 | /// Returns an \c AddMap class |
---|
| 819 | |
---|
| 820 | /// This function just returns an \c AddMap class. |
---|
[25] | 821 | /// |
---|
[80] | 822 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 823 | /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to |
---|
| 824 | /// <tt>m1[x]+m2[x]</tt>. |
---|
| 825 | /// |
---|
| 826 | /// \relates AddMap |
---|
| 827 | template<typename M1, typename M2> |
---|
| 828 | inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) { |
---|
[25] | 829 | return AddMap<M1, M2>(m1,m2); |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | |
---|
[80] | 833 | /// Difference of two maps |
---|
| 834 | |
---|
[82] | 835 | /// This \ref concepts::ReadMap "read-only map" returns the difference |
---|
[80] | 836 | /// of the values of the two given maps. |
---|
| 837 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 838 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 839 | /// \c M1. |
---|
[25] | 840 | /// |
---|
[80] | 841 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 842 | /// \code |
---|
| 843 | /// SubMap<M1,M2> sm(m1,m2); |
---|
| 844 | /// \endcode |
---|
| 845 | /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>. |
---|
[29] | 846 | /// |
---|
[80] | 847 | /// The simplest way of using this map is through the subMap() |
---|
| 848 | /// function. |
---|
| 849 | /// |
---|
| 850 | /// \sa AddMap, MulMap, DivMap |
---|
| 851 | template<typename M1, typename M2> |
---|
| 852 | class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 853 | const M1 &_m1; |
---|
| 854 | const M2 &_m2; |
---|
| 855 | public: |
---|
[606] | 856 | ///\e |
---|
| 857 | typedef typename M1::Key Key; |
---|
| 858 | ///\e |
---|
| 859 | typedef typename M1::Value Value; |
---|
[80] | 860 | |
---|
| 861 | /// Constructor |
---|
| 862 | SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 863 | ///\e |
---|
[80] | 864 | Value operator[](const Key &k) const { return _m1[k]-_m2[k]; } |
---|
| 865 | }; |
---|
| 866 | |
---|
[301] | 867 | /// Returns a \c SubMap class |
---|
| 868 | |
---|
| 869 | /// This function just returns a \c SubMap class. |
---|
[80] | 870 | /// |
---|
| 871 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 872 | /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to |
---|
| 873 | /// <tt>m1[x]-m2[x]</tt>. |
---|
| 874 | /// |
---|
| 875 | /// \relates SubMap |
---|
| 876 | template<typename M1, typename M2> |
---|
| 877 | inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
---|
| 878 | return SubMap<M1, M2>(m1,m2); |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | |
---|
| 882 | /// Product of two maps |
---|
| 883 | |
---|
[82] | 884 | /// This \ref concepts::ReadMap "read-only map" returns the product |
---|
[80] | 885 | /// of the values of the two given maps. |
---|
| 886 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 887 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 888 | /// \c M1. |
---|
| 889 | /// |
---|
| 890 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 891 | /// \code |
---|
| 892 | /// MulMap<M1,M2> mm(m1,m2); |
---|
| 893 | /// \endcode |
---|
| 894 | /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>. |
---|
| 895 | /// |
---|
| 896 | /// The simplest way of using this map is through the mulMap() |
---|
| 897 | /// function. |
---|
| 898 | /// |
---|
| 899 | /// \sa AddMap, SubMap, DivMap |
---|
| 900 | /// \sa ScaleMap, ScaleWriteMap |
---|
| 901 | template<typename M1, typename M2> |
---|
| 902 | class MulMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 903 | const M1 &_m1; |
---|
| 904 | const M2 &_m2; |
---|
| 905 | public: |
---|
[606] | 906 | ///\e |
---|
| 907 | typedef typename M1::Key Key; |
---|
| 908 | ///\e |
---|
| 909 | typedef typename M1::Value Value; |
---|
[80] | 910 | |
---|
| 911 | /// Constructor |
---|
| 912 | MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 913 | ///\e |
---|
[80] | 914 | Value operator[](const Key &k) const { return _m1[k]*_m2[k]; } |
---|
| 915 | }; |
---|
| 916 | |
---|
[301] | 917 | /// Returns a \c MulMap class |
---|
| 918 | |
---|
| 919 | /// This function just returns a \c MulMap class. |
---|
[80] | 920 | /// |
---|
| 921 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 922 | /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to |
---|
| 923 | /// <tt>m1[x]*m2[x]</tt>. |
---|
| 924 | /// |
---|
| 925 | /// \relates MulMap |
---|
| 926 | template<typename M1, typename M2> |
---|
| 927 | inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
---|
| 928 | return MulMap<M1, M2>(m1,m2); |
---|
| 929 | } |
---|
| 930 | |
---|
| 931 | |
---|
| 932 | /// Quotient of two maps |
---|
| 933 | |
---|
[82] | 934 | /// This \ref concepts::ReadMap "read-only map" returns the quotient |
---|
[80] | 935 | /// of the values of the two given maps. |
---|
| 936 | /// Its \c Key and \c Value types are inherited from \c M1. |
---|
| 937 | /// The \c Key and \c Value of \c M2 must be convertible to those of |
---|
| 938 | /// \c M1. |
---|
| 939 | /// |
---|
| 940 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 941 | /// \code |
---|
| 942 | /// DivMap<M1,M2> dm(m1,m2); |
---|
| 943 | /// \endcode |
---|
| 944 | /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>. |
---|
| 945 | /// |
---|
| 946 | /// The simplest way of using this map is through the divMap() |
---|
| 947 | /// function. |
---|
| 948 | /// |
---|
| 949 | /// \sa AddMap, SubMap, MulMap |
---|
| 950 | template<typename M1, typename M2> |
---|
| 951 | class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
---|
| 952 | const M1 &_m1; |
---|
| 953 | const M2 &_m2; |
---|
| 954 | public: |
---|
[606] | 955 | ///\e |
---|
| 956 | typedef typename M1::Key Key; |
---|
| 957 | ///\e |
---|
| 958 | typedef typename M1::Value Value; |
---|
[80] | 959 | |
---|
| 960 | /// Constructor |
---|
| 961 | DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 962 | ///\e |
---|
[80] | 963 | Value operator[](const Key &k) const { return _m1[k]/_m2[k]; } |
---|
| 964 | }; |
---|
| 965 | |
---|
[301] | 966 | /// Returns a \c DivMap class |
---|
| 967 | |
---|
| 968 | /// This function just returns a \c DivMap class. |
---|
[80] | 969 | /// |
---|
| 970 | /// For example, if \c m1 and \c m2 are both maps with \c double |
---|
| 971 | /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to |
---|
| 972 | /// <tt>m1[x]/m2[x]</tt>. |
---|
| 973 | /// |
---|
| 974 | /// \relates DivMap |
---|
| 975 | template<typename M1, typename M2> |
---|
| 976 | inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) { |
---|
| 977 | return DivMap<M1, M2>(m1,m2); |
---|
| 978 | } |
---|
| 979 | |
---|
| 980 | |
---|
| 981 | /// Shifts a map with a constant. |
---|
| 982 | |
---|
[82] | 983 | /// This \ref concepts::ReadMap "read-only map" returns the sum of |
---|
[80] | 984 | /// the given map and a constant value (i.e. it shifts the map with |
---|
| 985 | /// the constant). Its \c Key and \c Value are inherited from \c M. |
---|
| 986 | /// |
---|
| 987 | /// Actually, |
---|
| 988 | /// \code |
---|
| 989 | /// ShiftMap<M> sh(m,v); |
---|
| 990 | /// \endcode |
---|
| 991 | /// is equivalent to |
---|
| 992 | /// \code |
---|
| 993 | /// ConstMap<M::Key, M::Value> cm(v); |
---|
| 994 | /// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm); |
---|
| 995 | /// \endcode |
---|
| 996 | /// |
---|
| 997 | /// The simplest way of using this map is through the shiftMap() |
---|
| 998 | /// function. |
---|
| 999 | /// |
---|
| 1000 | /// \sa ShiftWriteMap |
---|
| 1001 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1002 | class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1003 | const M &_m; |
---|
| 1004 | C _v; |
---|
[25] | 1005 | public: |
---|
[606] | 1006 | ///\e |
---|
| 1007 | typedef typename M::Key Key; |
---|
| 1008 | ///\e |
---|
| 1009 | typedef typename M::Value Value; |
---|
[25] | 1010 | |
---|
[80] | 1011 | /// Constructor |
---|
[25] | 1012 | |
---|
[80] | 1013 | /// Constructor. |
---|
| 1014 | /// \param m The undelying map. |
---|
| 1015 | /// \param v The constant value. |
---|
| 1016 | ShiftMap(const M &m, const C &v) : _m(m), _v(v) {} |
---|
[606] | 1017 | ///\e |
---|
[80] | 1018 | Value operator[](const Key &k) const { return _m[k]+_v; } |
---|
[25] | 1019 | }; |
---|
| 1020 | |
---|
[80] | 1021 | /// Shifts a map with a constant (read-write version). |
---|
[25] | 1022 | |
---|
[80] | 1023 | /// This \ref concepts::ReadWriteMap "read-write map" returns the sum |
---|
| 1024 | /// of the given map and a constant value (i.e. it shifts the map with |
---|
| 1025 | /// the constant). Its \c Key and \c Value are inherited from \c M. |
---|
| 1026 | /// It makes also possible to write the map. |
---|
[25] | 1027 | /// |
---|
[80] | 1028 | /// The simplest way of using this map is through the shiftWriteMap() |
---|
| 1029 | /// function. |
---|
| 1030 | /// |
---|
| 1031 | /// \sa ShiftMap |
---|
| 1032 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1033 | class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1034 | M &_m; |
---|
| 1035 | C _v; |
---|
[25] | 1036 | public: |
---|
[606] | 1037 | ///\e |
---|
| 1038 | typedef typename M::Key Key; |
---|
| 1039 | ///\e |
---|
| 1040 | typedef typename M::Value Value; |
---|
[25] | 1041 | |
---|
[80] | 1042 | /// Constructor |
---|
[25] | 1043 | |
---|
[80] | 1044 | /// Constructor. |
---|
| 1045 | /// \param m The undelying map. |
---|
| 1046 | /// \param v The constant value. |
---|
| 1047 | ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {} |
---|
[606] | 1048 | ///\e |
---|
[80] | 1049 | Value operator[](const Key &k) const { return _m[k]+_v; } |
---|
[606] | 1050 | ///\e |
---|
[80] | 1051 | void set(const Key &k, const Value &v) { _m.set(k, v-_v); } |
---|
[25] | 1052 | }; |
---|
| 1053 | |
---|
[301] | 1054 | /// Returns a \c ShiftMap class |
---|
| 1055 | |
---|
| 1056 | /// This function just returns a \c ShiftMap class. |
---|
[80] | 1057 | /// |
---|
| 1058 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1059 | /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to |
---|
| 1060 | /// <tt>m[x]+v</tt>. |
---|
| 1061 | /// |
---|
| 1062 | /// \relates ShiftMap |
---|
| 1063 | template<typename M, typename C> |
---|
| 1064 | inline ShiftMap<M, C> shiftMap(const M &m, const C &v) { |
---|
[25] | 1065 | return ShiftMap<M, C>(m,v); |
---|
| 1066 | } |
---|
| 1067 | |
---|
[301] | 1068 | /// Returns a \c ShiftWriteMap class |
---|
| 1069 | |
---|
| 1070 | /// This function just returns a \c ShiftWriteMap class. |
---|
[80] | 1071 | /// |
---|
| 1072 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1073 | /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to |
---|
| 1074 | /// <tt>m[x]+v</tt>. |
---|
| 1075 | /// Moreover it makes also possible to write the map. |
---|
| 1076 | /// |
---|
| 1077 | /// \relates ShiftWriteMap |
---|
| 1078 | template<typename M, typename C> |
---|
| 1079 | inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) { |
---|
[25] | 1080 | return ShiftWriteMap<M, C>(m,v); |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | |
---|
[80] | 1084 | /// Scales a map with a constant. |
---|
| 1085 | |
---|
[82] | 1086 | /// This \ref concepts::ReadMap "read-only map" returns the value of |
---|
[80] | 1087 | /// the given map multiplied from the left side with a constant value. |
---|
| 1088 | /// Its \c Key and \c Value are inherited from \c M. |
---|
[26] | 1089 | /// |
---|
[80] | 1090 | /// Actually, |
---|
| 1091 | /// \code |
---|
| 1092 | /// ScaleMap<M> sc(m,v); |
---|
| 1093 | /// \endcode |
---|
| 1094 | /// is equivalent to |
---|
| 1095 | /// \code |
---|
| 1096 | /// ConstMap<M::Key, M::Value> cm(v); |
---|
| 1097 | /// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m); |
---|
| 1098 | /// \endcode |
---|
[25] | 1099 | /// |
---|
[80] | 1100 | /// The simplest way of using this map is through the scaleMap() |
---|
| 1101 | /// function. |
---|
[25] | 1102 | /// |
---|
[80] | 1103 | /// \sa ScaleWriteMap |
---|
| 1104 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1105 | class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1106 | const M &_m; |
---|
| 1107 | C _v; |
---|
[25] | 1108 | public: |
---|
[606] | 1109 | ///\e |
---|
| 1110 | typedef typename M::Key Key; |
---|
| 1111 | ///\e |
---|
| 1112 | typedef typename M::Value Value; |
---|
[25] | 1113 | |
---|
[80] | 1114 | /// Constructor |
---|
[25] | 1115 | |
---|
[80] | 1116 | /// Constructor. |
---|
| 1117 | /// \param m The undelying map. |
---|
| 1118 | /// \param v The constant value. |
---|
| 1119 | ScaleMap(const M &m, const C &v) : _m(m), _v(v) {} |
---|
[606] | 1120 | ///\e |
---|
[80] | 1121 | Value operator[](const Key &k) const { return _v*_m[k]; } |
---|
[25] | 1122 | }; |
---|
| 1123 | |
---|
[80] | 1124 | /// Scales a map with a constant (read-write version). |
---|
[25] | 1125 | |
---|
[80] | 1126 | /// This \ref concepts::ReadWriteMap "read-write map" returns the value of |
---|
| 1127 | /// the given map multiplied from the left side with a constant value. |
---|
| 1128 | /// Its \c Key and \c Value are inherited from \c M. |
---|
| 1129 | /// It can also be used as write map if the \c / operator is defined |
---|
| 1130 | /// between \c Value and \c C and the given multiplier is not zero. |
---|
[29] | 1131 | /// |
---|
[80] | 1132 | /// The simplest way of using this map is through the scaleWriteMap() |
---|
| 1133 | /// function. |
---|
| 1134 | /// |
---|
| 1135 | /// \sa ScaleMap |
---|
| 1136 | template<typename M, typename C = typename M::Value> |
---|
[25] | 1137 | class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1138 | M &_m; |
---|
| 1139 | C _v; |
---|
[25] | 1140 | public: |
---|
[606] | 1141 | ///\e |
---|
| 1142 | typedef typename M::Key Key; |
---|
| 1143 | ///\e |
---|
| 1144 | typedef typename M::Value Value; |
---|
[25] | 1145 | |
---|
[80] | 1146 | /// Constructor |
---|
[25] | 1147 | |
---|
[80] | 1148 | /// Constructor. |
---|
| 1149 | /// \param m The undelying map. |
---|
| 1150 | /// \param v The constant value. |
---|
| 1151 | ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {} |
---|
[606] | 1152 | ///\e |
---|
[80] | 1153 | Value operator[](const Key &k) const { return _v*_m[k]; } |
---|
[606] | 1154 | ///\e |
---|
[80] | 1155 | void set(const Key &k, const Value &v) { _m.set(k, v/_v); } |
---|
[25] | 1156 | }; |
---|
| 1157 | |
---|
[301] | 1158 | /// Returns a \c ScaleMap class |
---|
| 1159 | |
---|
| 1160 | /// This function just returns a \c ScaleMap class. |
---|
[80] | 1161 | /// |
---|
| 1162 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1163 | /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to |
---|
| 1164 | /// <tt>v*m[x]</tt>. |
---|
| 1165 | /// |
---|
| 1166 | /// \relates ScaleMap |
---|
| 1167 | template<typename M, typename C> |
---|
| 1168 | inline ScaleMap<M, C> scaleMap(const M &m, const C &v) { |
---|
[25] | 1169 | return ScaleMap<M, C>(m,v); |
---|
| 1170 | } |
---|
| 1171 | |
---|
[301] | 1172 | /// Returns a \c ScaleWriteMap class |
---|
| 1173 | |
---|
| 1174 | /// This function just returns a \c ScaleWriteMap class. |
---|
[80] | 1175 | /// |
---|
| 1176 | /// For example, if \c m is a map with \c double values and \c v is |
---|
| 1177 | /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to |
---|
| 1178 | /// <tt>v*m[x]</tt>. |
---|
| 1179 | /// Moreover it makes also possible to write the map. |
---|
| 1180 | /// |
---|
| 1181 | /// \relates ScaleWriteMap |
---|
| 1182 | template<typename M, typename C> |
---|
| 1183 | inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) { |
---|
[25] | 1184 | return ScaleWriteMap<M, C>(m,v); |
---|
| 1185 | } |
---|
| 1186 | |
---|
| 1187 | |
---|
[80] | 1188 | /// Negative of a map |
---|
[25] | 1189 | |
---|
[82] | 1190 | /// This \ref concepts::ReadMap "read-only map" returns the negative |
---|
[80] | 1191 | /// of the values of the given map (using the unary \c - operator). |
---|
| 1192 | /// Its \c Key and \c Value are inherited from \c M. |
---|
[25] | 1193 | /// |
---|
[80] | 1194 | /// If M::Value is \c int, \c double etc., then |
---|
| 1195 | /// \code |
---|
| 1196 | /// NegMap<M> neg(m); |
---|
| 1197 | /// \endcode |
---|
| 1198 | /// is equivalent to |
---|
| 1199 | /// \code |
---|
| 1200 | /// ScaleMap<M> neg(m,-1); |
---|
| 1201 | /// \endcode |
---|
[29] | 1202 | /// |
---|
[80] | 1203 | /// The simplest way of using this map is through the negMap() |
---|
| 1204 | /// function. |
---|
[29] | 1205 | /// |
---|
[80] | 1206 | /// \sa NegWriteMap |
---|
| 1207 | template<typename M> |
---|
[25] | 1208 | class NegMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1209 | const M& _m; |
---|
[25] | 1210 | public: |
---|
[606] | 1211 | ///\e |
---|
| 1212 | typedef typename M::Key Key; |
---|
| 1213 | ///\e |
---|
| 1214 | typedef typename M::Value Value; |
---|
[25] | 1215 | |
---|
[80] | 1216 | /// Constructor |
---|
| 1217 | NegMap(const M &m) : _m(m) {} |
---|
[606] | 1218 | ///\e |
---|
[80] | 1219 | Value operator[](const Key &k) const { return -_m[k]; } |
---|
[25] | 1220 | }; |
---|
| 1221 | |
---|
[80] | 1222 | /// Negative of a map (read-write version) |
---|
| 1223 | |
---|
| 1224 | /// This \ref concepts::ReadWriteMap "read-write map" returns the |
---|
| 1225 | /// negative of the values of the given map (using the unary \c - |
---|
| 1226 | /// operator). |
---|
| 1227 | /// Its \c Key and \c Value are inherited from \c M. |
---|
| 1228 | /// It makes also possible to write the map. |
---|
| 1229 | /// |
---|
| 1230 | /// If M::Value is \c int, \c double etc., then |
---|
| 1231 | /// \code |
---|
| 1232 | /// NegWriteMap<M> neg(m); |
---|
| 1233 | /// \endcode |
---|
| 1234 | /// is equivalent to |
---|
| 1235 | /// \code |
---|
| 1236 | /// ScaleWriteMap<M> neg(m,-1); |
---|
| 1237 | /// \endcode |
---|
| 1238 | /// |
---|
| 1239 | /// The simplest way of using this map is through the negWriteMap() |
---|
| 1240 | /// function. |
---|
[29] | 1241 | /// |
---|
| 1242 | /// \sa NegMap |
---|
[80] | 1243 | template<typename M> |
---|
[25] | 1244 | class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1245 | M &_m; |
---|
[25] | 1246 | public: |
---|
[606] | 1247 | ///\e |
---|
| 1248 | typedef typename M::Key Key; |
---|
| 1249 | ///\e |
---|
| 1250 | typedef typename M::Value Value; |
---|
[25] | 1251 | |
---|
[80] | 1252 | /// Constructor |
---|
| 1253 | NegWriteMap(M &m) : _m(m) {} |
---|
[606] | 1254 | ///\e |
---|
[80] | 1255 | Value operator[](const Key &k) const { return -_m[k]; } |
---|
[606] | 1256 | ///\e |
---|
[80] | 1257 | void set(const Key &k, const Value &v) { _m.set(k, -v); } |
---|
[25] | 1258 | }; |
---|
| 1259 | |
---|
[301] | 1260 | /// Returns a \c NegMap class |
---|
| 1261 | |
---|
| 1262 | /// This function just returns a \c NegMap class. |
---|
[80] | 1263 | /// |
---|
| 1264 | /// For example, if \c m is a map with \c double values, then |
---|
| 1265 | /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
---|
| 1266 | /// |
---|
| 1267 | /// \relates NegMap |
---|
| 1268 | template <typename M> |
---|
[25] | 1269 | inline NegMap<M> negMap(const M &m) { |
---|
| 1270 | return NegMap<M>(m); |
---|
| 1271 | } |
---|
| 1272 | |
---|
[301] | 1273 | /// Returns a \c NegWriteMap class |
---|
| 1274 | |
---|
| 1275 | /// This function just returns a \c NegWriteMap class. |
---|
[80] | 1276 | /// |
---|
| 1277 | /// For example, if \c m is a map with \c double values, then |
---|
| 1278 | /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
---|
| 1279 | /// Moreover it makes also possible to write the map. |
---|
| 1280 | /// |
---|
| 1281 | /// \relates NegWriteMap |
---|
| 1282 | template <typename M> |
---|
| 1283 | inline NegWriteMap<M> negWriteMap(M &m) { |
---|
[25] | 1284 | return NegWriteMap<M>(m); |
---|
| 1285 | } |
---|
| 1286 | |
---|
| 1287 | |
---|
[80] | 1288 | /// Absolute value of a map |
---|
| 1289 | |
---|
[82] | 1290 | /// This \ref concepts::ReadMap "read-only map" returns the absolute |
---|
[80] | 1291 | /// value of the values of the given map. |
---|
| 1292 | /// Its \c Key and \c Value are inherited from \c M. |
---|
| 1293 | /// \c Value must be comparable to \c 0 and the unary \c - |
---|
| 1294 | /// operator must be defined for it, of course. |
---|
| 1295 | /// |
---|
| 1296 | /// The simplest way of using this map is through the absMap() |
---|
| 1297 | /// function. |
---|
| 1298 | template<typename M> |
---|
[25] | 1299 | class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
---|
[80] | 1300 | const M &_m; |
---|
[25] | 1301 | public: |
---|
[606] | 1302 | ///\e |
---|
| 1303 | typedef typename M::Key Key; |
---|
| 1304 | ///\e |
---|
| 1305 | typedef typename M::Value Value; |
---|
[25] | 1306 | |
---|
[80] | 1307 | /// Constructor |
---|
| 1308 | AbsMap(const M &m) : _m(m) {} |
---|
[606] | 1309 | ///\e |
---|
[80] | 1310 | Value operator[](const Key &k) const { |
---|
| 1311 | Value tmp = _m[k]; |
---|
[25] | 1312 | return tmp >= 0 ? tmp : -tmp; |
---|
| 1313 | } |
---|
| 1314 | |
---|
| 1315 | }; |
---|
| 1316 | |
---|
[301] | 1317 | /// Returns an \c AbsMap class |
---|
| 1318 | |
---|
| 1319 | /// This function just returns an \c AbsMap class. |
---|
[80] | 1320 | /// |
---|
| 1321 | /// For example, if \c m is a map with \c double values, then |
---|
| 1322 | /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if |
---|
| 1323 | /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is |
---|
| 1324 | /// negative. |
---|
| 1325 | /// |
---|
| 1326 | /// \relates AbsMap |
---|
| 1327 | template<typename M> |
---|
[25] | 1328 | inline AbsMap<M> absMap(const M &m) { |
---|
| 1329 | return AbsMap<M>(m); |
---|
| 1330 | } |
---|
| 1331 | |
---|
[82] | 1332 | /// @} |
---|
[209] | 1333 | |
---|
[82] | 1334 | // Logical maps and map adaptors: |
---|
| 1335 | |
---|
| 1336 | /// \addtogroup maps |
---|
| 1337 | /// @{ |
---|
| 1338 | |
---|
| 1339 | /// Constant \c true map. |
---|
| 1340 | |
---|
| 1341 | /// This \ref concepts::ReadMap "read-only map" assigns \c true to |
---|
| 1342 | /// each key. |
---|
| 1343 | /// |
---|
| 1344 | /// Note that |
---|
| 1345 | /// \code |
---|
| 1346 | /// TrueMap<K> tm; |
---|
| 1347 | /// \endcode |
---|
| 1348 | /// is equivalent to |
---|
| 1349 | /// \code |
---|
| 1350 | /// ConstMap<K,bool> tm(true); |
---|
| 1351 | /// \endcode |
---|
| 1352 | /// |
---|
| 1353 | /// \sa FalseMap |
---|
| 1354 | /// \sa ConstMap |
---|
| 1355 | template <typename K> |
---|
| 1356 | class TrueMap : public MapBase<K, bool> { |
---|
| 1357 | public: |
---|
[606] | 1358 | ///\e |
---|
| 1359 | typedef K Key; |
---|
| 1360 | ///\e |
---|
| 1361 | typedef bool Value; |
---|
[82] | 1362 | |
---|
| 1363 | /// Gives back \c true. |
---|
| 1364 | Value operator[](const Key&) const { return true; } |
---|
| 1365 | }; |
---|
| 1366 | |
---|
[301] | 1367 | /// Returns a \c TrueMap class |
---|
| 1368 | |
---|
| 1369 | /// This function just returns a \c TrueMap class. |
---|
[82] | 1370 | /// \relates TrueMap |
---|
| 1371 | template<typename K> |
---|
| 1372 | inline TrueMap<K> trueMap() { |
---|
| 1373 | return TrueMap<K>(); |
---|
| 1374 | } |
---|
| 1375 | |
---|
| 1376 | |
---|
| 1377 | /// Constant \c false map. |
---|
| 1378 | |
---|
| 1379 | /// This \ref concepts::ReadMap "read-only map" assigns \c false to |
---|
| 1380 | /// each key. |
---|
| 1381 | /// |
---|
| 1382 | /// Note that |
---|
| 1383 | /// \code |
---|
| 1384 | /// FalseMap<K> fm; |
---|
| 1385 | /// \endcode |
---|
| 1386 | /// is equivalent to |
---|
| 1387 | /// \code |
---|
| 1388 | /// ConstMap<K,bool> fm(false); |
---|
| 1389 | /// \endcode |
---|
| 1390 | /// |
---|
| 1391 | /// \sa TrueMap |
---|
| 1392 | /// \sa ConstMap |
---|
| 1393 | template <typename K> |
---|
| 1394 | class FalseMap : public MapBase<K, bool> { |
---|
| 1395 | public: |
---|
[606] | 1396 | ///\e |
---|
| 1397 | typedef K Key; |
---|
| 1398 | ///\e |
---|
| 1399 | typedef bool Value; |
---|
[82] | 1400 | |
---|
| 1401 | /// Gives back \c false. |
---|
| 1402 | Value operator[](const Key&) const { return false; } |
---|
| 1403 | }; |
---|
| 1404 | |
---|
[301] | 1405 | /// Returns a \c FalseMap class |
---|
| 1406 | |
---|
| 1407 | /// This function just returns a \c FalseMap class. |
---|
[82] | 1408 | /// \relates FalseMap |
---|
| 1409 | template<typename K> |
---|
| 1410 | inline FalseMap<K> falseMap() { |
---|
| 1411 | return FalseMap<K>(); |
---|
| 1412 | } |
---|
| 1413 | |
---|
| 1414 | /// @} |
---|
| 1415 | |
---|
| 1416 | /// \addtogroup map_adaptors |
---|
| 1417 | /// @{ |
---|
| 1418 | |
---|
| 1419 | /// Logical 'and' of two maps |
---|
| 1420 | |
---|
| 1421 | /// This \ref concepts::ReadMap "read-only map" returns the logical |
---|
| 1422 | /// 'and' of the values of the two given maps. |
---|
| 1423 | /// Its \c Key type is inherited from \c M1 and its \c Value type is |
---|
| 1424 | /// \c bool. \c M2::Key must be convertible to \c M1::Key. |
---|
| 1425 | /// |
---|
| 1426 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 1427 | /// \code |
---|
| 1428 | /// AndMap<M1,M2> am(m1,m2); |
---|
| 1429 | /// \endcode |
---|
| 1430 | /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>. |
---|
| 1431 | /// |
---|
| 1432 | /// The simplest way of using this map is through the andMap() |
---|
| 1433 | /// function. |
---|
| 1434 | /// |
---|
| 1435 | /// \sa OrMap |
---|
| 1436 | /// \sa NotMap, NotWriteMap |
---|
| 1437 | template<typename M1, typename M2> |
---|
| 1438 | class AndMap : public MapBase<typename M1::Key, bool> { |
---|
| 1439 | const M1 &_m1; |
---|
| 1440 | const M2 &_m2; |
---|
| 1441 | public: |
---|
[606] | 1442 | ///\e |
---|
| 1443 | typedef typename M1::Key Key; |
---|
| 1444 | ///\e |
---|
| 1445 | typedef bool Value; |
---|
[82] | 1446 | |
---|
| 1447 | /// Constructor |
---|
| 1448 | AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 1449 | ///\e |
---|
[82] | 1450 | Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; } |
---|
| 1451 | }; |
---|
| 1452 | |
---|
[301] | 1453 | /// Returns an \c AndMap class |
---|
| 1454 | |
---|
| 1455 | /// This function just returns an \c AndMap class. |
---|
[82] | 1456 | /// |
---|
| 1457 | /// For example, if \c m1 and \c m2 are both maps with \c bool values, |
---|
| 1458 | /// then <tt>andMap(m1,m2)[x]</tt> will be equal to |
---|
| 1459 | /// <tt>m1[x]&&m2[x]</tt>. |
---|
| 1460 | /// |
---|
| 1461 | /// \relates AndMap |
---|
| 1462 | template<typename M1, typename M2> |
---|
| 1463 | inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) { |
---|
| 1464 | return AndMap<M1, M2>(m1,m2); |
---|
| 1465 | } |
---|
| 1466 | |
---|
| 1467 | |
---|
| 1468 | /// Logical 'or' of two maps |
---|
| 1469 | |
---|
| 1470 | /// This \ref concepts::ReadMap "read-only map" returns the logical |
---|
| 1471 | /// 'or' of the values of the two given maps. |
---|
| 1472 | /// Its \c Key type is inherited from \c M1 and its \c Value type is |
---|
| 1473 | /// \c bool. \c M2::Key must be convertible to \c M1::Key. |
---|
| 1474 | /// |
---|
| 1475 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 1476 | /// \code |
---|
| 1477 | /// OrMap<M1,M2> om(m1,m2); |
---|
| 1478 | /// \endcode |
---|
| 1479 | /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>. |
---|
| 1480 | /// |
---|
| 1481 | /// The simplest way of using this map is through the orMap() |
---|
| 1482 | /// function. |
---|
| 1483 | /// |
---|
| 1484 | /// \sa AndMap |
---|
| 1485 | /// \sa NotMap, NotWriteMap |
---|
| 1486 | template<typename M1, typename M2> |
---|
| 1487 | class OrMap : public MapBase<typename M1::Key, bool> { |
---|
| 1488 | const M1 &_m1; |
---|
| 1489 | const M2 &_m2; |
---|
| 1490 | public: |
---|
[606] | 1491 | ///\e |
---|
| 1492 | typedef typename M1::Key Key; |
---|
| 1493 | ///\e |
---|
| 1494 | typedef bool Value; |
---|
[82] | 1495 | |
---|
| 1496 | /// Constructor |
---|
| 1497 | OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 1498 | ///\e |
---|
[82] | 1499 | Value operator[](const Key &k) const { return _m1[k]||_m2[k]; } |
---|
| 1500 | }; |
---|
| 1501 | |
---|
[301] | 1502 | /// Returns an \c OrMap class |
---|
| 1503 | |
---|
| 1504 | /// This function just returns an \c OrMap class. |
---|
[82] | 1505 | /// |
---|
| 1506 | /// For example, if \c m1 and \c m2 are both maps with \c bool values, |
---|
| 1507 | /// then <tt>orMap(m1,m2)[x]</tt> will be equal to |
---|
| 1508 | /// <tt>m1[x]||m2[x]</tt>. |
---|
| 1509 | /// |
---|
| 1510 | /// \relates OrMap |
---|
| 1511 | template<typename M1, typename M2> |
---|
| 1512 | inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) { |
---|
| 1513 | return OrMap<M1, M2>(m1,m2); |
---|
| 1514 | } |
---|
| 1515 | |
---|
[25] | 1516 | |
---|
[80] | 1517 | /// Logical 'not' of a map |
---|
| 1518 | |
---|
[82] | 1519 | /// This \ref concepts::ReadMap "read-only map" returns the logical |
---|
[80] | 1520 | /// negation of the values of the given map. |
---|
| 1521 | /// Its \c Key is inherited from \c M and its \c Value is \c bool. |
---|
[25] | 1522 | /// |
---|
[80] | 1523 | /// The simplest way of using this map is through the notMap() |
---|
| 1524 | /// function. |
---|
[25] | 1525 | /// |
---|
[80] | 1526 | /// \sa NotWriteMap |
---|
| 1527 | template <typename M> |
---|
[25] | 1528 | class NotMap : public MapBase<typename M::Key, bool> { |
---|
[80] | 1529 | const M &_m; |
---|
[25] | 1530 | public: |
---|
[606] | 1531 | ///\e |
---|
| 1532 | typedef typename M::Key Key; |
---|
| 1533 | ///\e |
---|
| 1534 | typedef bool Value; |
---|
[25] | 1535 | |
---|
| 1536 | /// Constructor |
---|
[80] | 1537 | NotMap(const M &m) : _m(m) {} |
---|
[606] | 1538 | ///\e |
---|
[80] | 1539 | Value operator[](const Key &k) const { return !_m[k]; } |
---|
[25] | 1540 | }; |
---|
| 1541 | |
---|
[80] | 1542 | /// Logical 'not' of a map (read-write version) |
---|
| 1543 | |
---|
| 1544 | /// This \ref concepts::ReadWriteMap "read-write map" returns the |
---|
| 1545 | /// logical negation of the values of the given map. |
---|
| 1546 | /// Its \c Key is inherited from \c M and its \c Value is \c bool. |
---|
| 1547 | /// It makes also possible to write the map. When a value is set, |
---|
| 1548 | /// the opposite value is set to the original map. |
---|
[29] | 1549 | /// |
---|
[80] | 1550 | /// The simplest way of using this map is through the notWriteMap() |
---|
| 1551 | /// function. |
---|
| 1552 | /// |
---|
| 1553 | /// \sa NotMap |
---|
| 1554 | template <typename M> |
---|
[25] | 1555 | class NotWriteMap : public MapBase<typename M::Key, bool> { |
---|
[80] | 1556 | M &_m; |
---|
[25] | 1557 | public: |
---|
[606] | 1558 | ///\e |
---|
| 1559 | typedef typename M::Key Key; |
---|
| 1560 | ///\e |
---|
| 1561 | typedef bool Value; |
---|
[25] | 1562 | |
---|
| 1563 | /// Constructor |
---|
[80] | 1564 | NotWriteMap(M &m) : _m(m) {} |
---|
[606] | 1565 | ///\e |
---|
[80] | 1566 | Value operator[](const Key &k) const { return !_m[k]; } |
---|
[606] | 1567 | ///\e |
---|
[80] | 1568 | void set(const Key &k, bool v) { _m.set(k, !v); } |
---|
[25] | 1569 | }; |
---|
[80] | 1570 | |
---|
[301] | 1571 | /// Returns a \c NotMap class |
---|
| 1572 | |
---|
| 1573 | /// This function just returns a \c NotMap class. |
---|
[80] | 1574 | /// |
---|
| 1575 | /// For example, if \c m is a map with \c bool values, then |
---|
| 1576 | /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
---|
| 1577 | /// |
---|
| 1578 | /// \relates NotMap |
---|
| 1579 | template <typename M> |
---|
[25] | 1580 | inline NotMap<M> notMap(const M &m) { |
---|
| 1581 | return NotMap<M>(m); |
---|
| 1582 | } |
---|
[80] | 1583 | |
---|
[301] | 1584 | /// Returns a \c NotWriteMap class |
---|
| 1585 | |
---|
| 1586 | /// This function just returns a \c NotWriteMap class. |
---|
[80] | 1587 | /// |
---|
| 1588 | /// For example, if \c m is a map with \c bool values, then |
---|
| 1589 | /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
---|
| 1590 | /// Moreover it makes also possible to write the map. |
---|
| 1591 | /// |
---|
| 1592 | /// \relates NotWriteMap |
---|
| 1593 | template <typename M> |
---|
| 1594 | inline NotWriteMap<M> notWriteMap(M &m) { |
---|
[25] | 1595 | return NotWriteMap<M>(m); |
---|
| 1596 | } |
---|
| 1597 | |
---|
[82] | 1598 | |
---|
| 1599 | /// Combination of two maps using the \c == operator |
---|
| 1600 | |
---|
| 1601 | /// This \ref concepts::ReadMap "read-only map" assigns \c true to |
---|
| 1602 | /// the keys for which the corresponding values of the two maps are |
---|
| 1603 | /// equal. |
---|
| 1604 | /// Its \c Key type is inherited from \c M1 and its \c Value type is |
---|
| 1605 | /// \c bool. \c M2::Key must be convertible to \c M1::Key. |
---|
| 1606 | /// |
---|
| 1607 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 1608 | /// \code |
---|
| 1609 | /// EqualMap<M1,M2> em(m1,m2); |
---|
| 1610 | /// \endcode |
---|
| 1611 | /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>. |
---|
| 1612 | /// |
---|
| 1613 | /// The simplest way of using this map is through the equalMap() |
---|
| 1614 | /// function. |
---|
| 1615 | /// |
---|
| 1616 | /// \sa LessMap |
---|
| 1617 | template<typename M1, typename M2> |
---|
| 1618 | class EqualMap : public MapBase<typename M1::Key, bool> { |
---|
| 1619 | const M1 &_m1; |
---|
| 1620 | const M2 &_m2; |
---|
| 1621 | public: |
---|
[606] | 1622 | ///\e |
---|
| 1623 | typedef typename M1::Key Key; |
---|
| 1624 | ///\e |
---|
| 1625 | typedef bool Value; |
---|
[82] | 1626 | |
---|
| 1627 | /// Constructor |
---|
| 1628 | EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 1629 | ///\e |
---|
[82] | 1630 | Value operator[](const Key &k) const { return _m1[k]==_m2[k]; } |
---|
| 1631 | }; |
---|
| 1632 | |
---|
[301] | 1633 | /// Returns an \c EqualMap class |
---|
| 1634 | |
---|
| 1635 | /// This function just returns an \c EqualMap class. |
---|
[82] | 1636 | /// |
---|
| 1637 | /// For example, if \c m1 and \c m2 are maps with keys and values of |
---|
| 1638 | /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to |
---|
| 1639 | /// <tt>m1[x]==m2[x]</tt>. |
---|
| 1640 | /// |
---|
| 1641 | /// \relates EqualMap |
---|
| 1642 | template<typename M1, typename M2> |
---|
| 1643 | inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) { |
---|
| 1644 | return EqualMap<M1, M2>(m1,m2); |
---|
| 1645 | } |
---|
| 1646 | |
---|
| 1647 | |
---|
| 1648 | /// Combination of two maps using the \c < operator |
---|
| 1649 | |
---|
| 1650 | /// This \ref concepts::ReadMap "read-only map" assigns \c true to |
---|
| 1651 | /// the keys for which the corresponding value of the first map is |
---|
| 1652 | /// less then the value of the second map. |
---|
| 1653 | /// Its \c Key type is inherited from \c M1 and its \c Value type is |
---|
| 1654 | /// \c bool. \c M2::Key must be convertible to \c M1::Key. |
---|
| 1655 | /// |
---|
| 1656 | /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
---|
| 1657 | /// \code |
---|
| 1658 | /// LessMap<M1,M2> lm(m1,m2); |
---|
| 1659 | /// \endcode |
---|
| 1660 | /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>. |
---|
| 1661 | /// |
---|
| 1662 | /// The simplest way of using this map is through the lessMap() |
---|
| 1663 | /// function. |
---|
| 1664 | /// |
---|
| 1665 | /// \sa EqualMap |
---|
| 1666 | template<typename M1, typename M2> |
---|
| 1667 | class LessMap : public MapBase<typename M1::Key, bool> { |
---|
| 1668 | const M1 &_m1; |
---|
| 1669 | const M2 &_m2; |
---|
| 1670 | public: |
---|
[606] | 1671 | ///\e |
---|
| 1672 | typedef typename M1::Key Key; |
---|
| 1673 | ///\e |
---|
| 1674 | typedef bool Value; |
---|
[82] | 1675 | |
---|
| 1676 | /// Constructor |
---|
| 1677 | LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
---|
[606] | 1678 | ///\e |
---|
[82] | 1679 | Value operator[](const Key &k) const { return _m1[k]<_m2[k]; } |
---|
| 1680 | }; |
---|
| 1681 | |
---|
[301] | 1682 | /// Returns an \c LessMap class |
---|
| 1683 | |
---|
| 1684 | /// This function just returns an \c LessMap class. |
---|
[82] | 1685 | /// |
---|
| 1686 | /// For example, if \c m1 and \c m2 are maps with keys and values of |
---|
| 1687 | /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to |
---|
| 1688 | /// <tt>m1[x]<m2[x]</tt>. |
---|
| 1689 | /// |
---|
| 1690 | /// \relates LessMap |
---|
| 1691 | template<typename M1, typename M2> |
---|
| 1692 | inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) { |
---|
| 1693 | return LessMap<M1, M2>(m1,m2); |
---|
| 1694 | } |
---|
| 1695 | |
---|
[104] | 1696 | namespace _maps_bits { |
---|
| 1697 | |
---|
| 1698 | template <typename _Iterator, typename Enable = void> |
---|
| 1699 | struct IteratorTraits { |
---|
| 1700 | typedef typename std::iterator_traits<_Iterator>::value_type Value; |
---|
| 1701 | }; |
---|
| 1702 | |
---|
| 1703 | template <typename _Iterator> |
---|
| 1704 | struct IteratorTraits<_Iterator, |
---|
| 1705 | typename exists<typename _Iterator::container_type>::type> |
---|
| 1706 | { |
---|
| 1707 | typedef typename _Iterator::container_type::value_type Value; |
---|
| 1708 | }; |
---|
| 1709 | |
---|
| 1710 | } |
---|
| 1711 | |
---|
[314] | 1712 | /// @} |
---|
| 1713 | |
---|
| 1714 | /// \addtogroup maps |
---|
| 1715 | /// @{ |
---|
| 1716 | |
---|
[104] | 1717 | /// \brief Writable bool map for logging each \c true assigned element |
---|
| 1718 | /// |
---|
[159] | 1719 | /// A \ref concepts::WriteMap "writable" bool map for logging |
---|
[104] | 1720 | /// each \c true assigned element, i.e it copies subsequently each |
---|
| 1721 | /// keys set to \c true to the given iterator. |
---|
[159] | 1722 | /// The most important usage of it is storing certain nodes or arcs |
---|
| 1723 | /// that were marked \c true by an algorithm. |
---|
[104] | 1724 | /// |
---|
[159] | 1725 | /// There are several algorithms that provide solutions through bool |
---|
| 1726 | /// maps and most of them assign \c true at most once for each key. |
---|
| 1727 | /// In these cases it is a natural request to store each \c true |
---|
| 1728 | /// assigned elements (in order of the assignment), which can be |
---|
[167] | 1729 | /// easily done with LoggerBoolMap. |
---|
[159] | 1730 | /// |
---|
[167] | 1731 | /// The simplest way of using this map is through the loggerBoolMap() |
---|
[159] | 1732 | /// function. |
---|
| 1733 | /// |
---|
[606] | 1734 | /// \tparam IT The type of the iterator. |
---|
| 1735 | /// \tparam KEY The key type of the map. The default value set |
---|
[159] | 1736 | /// according to the iterator type should work in most cases. |
---|
[104] | 1737 | /// |
---|
| 1738 | /// \note The container of the iterator must contain enough space |
---|
[159] | 1739 | /// for the elements or the iterator should be an inserter iterator. |
---|
| 1740 | #ifdef DOXYGEN |
---|
[606] | 1741 | template <typename IT, typename KEY> |
---|
[159] | 1742 | #else |
---|
[606] | 1743 | template <typename IT, |
---|
| 1744 | typename KEY = typename _maps_bits::IteratorTraits<IT>::Value> |
---|
[159] | 1745 | #endif |
---|
[606] | 1746 | class LoggerBoolMap : public MapBase<KEY, bool> { |
---|
[104] | 1747 | public: |
---|
[606] | 1748 | |
---|
| 1749 | ///\e |
---|
| 1750 | typedef KEY Key; |
---|
| 1751 | ///\e |
---|
[104] | 1752 | typedef bool Value; |
---|
[606] | 1753 | ///\e |
---|
| 1754 | typedef IT Iterator; |
---|
[104] | 1755 | |
---|
| 1756 | /// Constructor |
---|
[167] | 1757 | LoggerBoolMap(Iterator it) |
---|
[104] | 1758 | : _begin(it), _end(it) {} |
---|
| 1759 | |
---|
| 1760 | /// Gives back the given iterator set for the first key |
---|
| 1761 | Iterator begin() const { |
---|
| 1762 | return _begin; |
---|
| 1763 | } |
---|
| 1764 | |
---|
| 1765 | /// Gives back the the 'after the last' iterator |
---|
| 1766 | Iterator end() const { |
---|
| 1767 | return _end; |
---|
| 1768 | } |
---|
| 1769 | |
---|
| 1770 | /// The set function of the map |
---|
[159] | 1771 | void set(const Key& key, Value value) { |
---|
[104] | 1772 | if (value) { |
---|
[209] | 1773 | *_end++ = key; |
---|
[104] | 1774 | } |
---|
| 1775 | } |
---|
| 1776 | |
---|
| 1777 | private: |
---|
| 1778 | Iterator _begin; |
---|
[159] | 1779 | Iterator _end; |
---|
[104] | 1780 | }; |
---|
[209] | 1781 | |
---|
[301] | 1782 | /// Returns a \c LoggerBoolMap class |
---|
| 1783 | |
---|
| 1784 | /// This function just returns a \c LoggerBoolMap class. |
---|
[159] | 1785 | /// |
---|
| 1786 | /// The most important usage of it is storing certain nodes or arcs |
---|
| 1787 | /// that were marked \c true by an algorithm. |
---|
| 1788 | /// For example it makes easier to store the nodes in the processing |
---|
| 1789 | /// order of Dfs algorithm, as the following examples show. |
---|
| 1790 | /// \code |
---|
| 1791 | /// std::vector<Node> v; |
---|
[167] | 1792 | /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
---|
[159] | 1793 | /// \endcode |
---|
| 1794 | /// \code |
---|
| 1795 | /// std::vector<Node> v(countNodes(g)); |
---|
[167] | 1796 | /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); |
---|
[159] | 1797 | /// \endcode |
---|
| 1798 | /// |
---|
| 1799 | /// \note The container of the iterator must contain enough space |
---|
| 1800 | /// for the elements or the iterator should be an inserter iterator. |
---|
| 1801 | /// |
---|
[167] | 1802 | /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
---|
[159] | 1803 | /// it cannot be used when a readable map is needed, for example as |
---|
[301] | 1804 | /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms. |
---|
[159] | 1805 | /// |
---|
[167] | 1806 | /// \relates LoggerBoolMap |
---|
[159] | 1807 | template<typename Iterator> |
---|
[167] | 1808 | inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) { |
---|
| 1809 | return LoggerBoolMap<Iterator>(it); |
---|
[159] | 1810 | } |
---|
[104] | 1811 | |
---|
[314] | 1812 | /// @} |
---|
| 1813 | |
---|
| 1814 | /// \addtogroup graph_maps |
---|
| 1815 | /// @{ |
---|
| 1816 | |
---|
[606] | 1817 | /// \brief Provides an immutable and unique id for each item in a graph. |
---|
| 1818 | /// |
---|
| 1819 | /// IdMap provides a unique and immutable id for each item of the |
---|
[740] | 1820 | /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is |
---|
[606] | 1821 | /// - \b unique: different items get different ids, |
---|
| 1822 | /// - \b immutable: the id of an item does not change (even if you |
---|
| 1823 | /// delete other nodes). |
---|
| 1824 | /// |
---|
| 1825 | /// Using this map you get access (i.e. can read) the inner id values of |
---|
| 1826 | /// the items stored in the graph, which is returned by the \c id() |
---|
| 1827 | /// function of the graph. This map can be inverted with its member |
---|
[220] | 1828 | /// class \c InverseMap or with the \c operator() member. |
---|
| 1829 | /// |
---|
[606] | 1830 | /// \tparam GR The graph type. |
---|
| 1831 | /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
---|
| 1832 | /// \c GR::Edge). |
---|
| 1833 | /// |
---|
[619] | 1834 | /// \see RangeIdMap |
---|
[606] | 1835 | template <typename GR, typename K> |
---|
| 1836 | class IdMap : public MapBase<K, int> { |
---|
[220] | 1837 | public: |
---|
[606] | 1838 | /// The graph type of IdMap. |
---|
| 1839 | typedef GR Graph; |
---|
[664] | 1840 | typedef GR Digraph; |
---|
[606] | 1841 | /// The key type of IdMap (\c Node, \c Arc or \c Edge). |
---|
| 1842 | typedef K Item; |
---|
| 1843 | /// The key type of IdMap (\c Node, \c Arc or \c Edge). |
---|
| 1844 | typedef K Key; |
---|
| 1845 | /// The value type of IdMap. |
---|
[220] | 1846 | typedef int Value; |
---|
| 1847 | |
---|
| 1848 | /// \brief Constructor. |
---|
| 1849 | /// |
---|
| 1850 | /// Constructor of the map. |
---|
| 1851 | explicit IdMap(const Graph& graph) : _graph(&graph) {} |
---|
| 1852 | |
---|
| 1853 | /// \brief Gives back the \e id of the item. |
---|
| 1854 | /// |
---|
| 1855 | /// Gives back the immutable and unique \e id of the item. |
---|
| 1856 | int operator[](const Item& item) const { return _graph->id(item);} |
---|
| 1857 | |
---|
[606] | 1858 | /// \brief Gives back the \e item by its id. |
---|
[220] | 1859 | /// |
---|
[606] | 1860 | /// Gives back the \e item by its id. |
---|
[220] | 1861 | Item operator()(int id) { return _graph->fromId(id, Item()); } |
---|
| 1862 | |
---|
| 1863 | private: |
---|
| 1864 | const Graph* _graph; |
---|
| 1865 | |
---|
| 1866 | public: |
---|
| 1867 | |
---|
[606] | 1868 | /// \brief This class represents the inverse of its owner (IdMap). |
---|
[220] | 1869 | /// |
---|
[606] | 1870 | /// This class represents the inverse of its owner (IdMap). |
---|
[220] | 1871 | /// \see inverse() |
---|
| 1872 | class InverseMap { |
---|
| 1873 | public: |
---|
| 1874 | |
---|
| 1875 | /// \brief Constructor. |
---|
| 1876 | /// |
---|
| 1877 | /// Constructor for creating an id-to-item map. |
---|
| 1878 | explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
---|
| 1879 | |
---|
| 1880 | /// \brief Constructor. |
---|
| 1881 | /// |
---|
| 1882 | /// Constructor for creating an id-to-item map. |
---|
| 1883 | explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
---|
| 1884 | |
---|
| 1885 | /// \brief Gives back the given item from its id. |
---|
| 1886 | /// |
---|
| 1887 | /// Gives back the given item from its id. |
---|
| 1888 | Item operator[](int id) const { return _graph->fromId(id, Item());} |
---|
| 1889 | |
---|
| 1890 | private: |
---|
| 1891 | const Graph* _graph; |
---|
| 1892 | }; |
---|
| 1893 | |
---|
| 1894 | /// \brief Gives back the inverse of the map. |
---|
| 1895 | /// |
---|
| 1896 | /// Gives back the inverse of the IdMap. |
---|
| 1897 | InverseMap inverse() const { return InverseMap(*_graph);} |
---|
| 1898 | }; |
---|
| 1899 | |
---|
| 1900 | |
---|
[619] | 1901 | /// \brief General cross reference graph map type. |
---|
[606] | 1902 | |
---|
| 1903 | /// This class provides simple invertable graph maps. |
---|
| 1904 | /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap" |
---|
[220] | 1905 | /// and if a key is set to a new value then store it |
---|
| 1906 | /// in the inverse map. |
---|
| 1907 | /// The values of the map can be accessed |
---|
| 1908 | /// with stl compatible forward iterator. |
---|
| 1909 | /// |
---|
[741] | 1910 | /// This type is not reference map, so it cannot be modified with |
---|
| 1911 | /// the subscription operator. |
---|
| 1912 | /// |
---|
[606] | 1913 | /// \tparam GR The graph type. |
---|
| 1914 | /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
---|
| 1915 | /// \c GR::Edge). |
---|
| 1916 | /// \tparam V The value type of the map. |
---|
[220] | 1917 | /// |
---|
| 1918 | /// \see IterableValueMap |
---|
[606] | 1919 | template <typename GR, typename K, typename V> |
---|
[619] | 1920 | class CrossRefMap |
---|
[606] | 1921 | : protected ItemSetTraits<GR, K>::template Map<V>::Type { |
---|
[220] | 1922 | private: |
---|
| 1923 | |
---|
[606] | 1924 | typedef typename ItemSetTraits<GR, K>:: |
---|
| 1925 | template Map<V>::Type Map; |
---|
| 1926 | |
---|
| 1927 | typedef std::map<V, K> Container; |
---|
[220] | 1928 | Container _inv_map; |
---|
| 1929 | |
---|
| 1930 | public: |
---|
| 1931 | |
---|
[619] | 1932 | /// The graph type of CrossRefMap. |
---|
[606] | 1933 | typedef GR Graph; |
---|
[664] | 1934 | typedef GR Digraph; |
---|
[619] | 1935 | /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
---|
[606] | 1936 | typedef K Item; |
---|
[619] | 1937 | /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
---|
[606] | 1938 | typedef K Key; |
---|
[619] | 1939 | /// The value type of CrossRefMap. |
---|
[606] | 1940 | typedef V Value; |
---|
[220] | 1941 | |
---|
| 1942 | /// \brief Constructor. |
---|
| 1943 | /// |
---|
[619] | 1944 | /// Construct a new CrossRefMap for the given graph. |
---|
| 1945 | explicit CrossRefMap(const Graph& graph) : Map(graph) {} |
---|
[220] | 1946 | |
---|
| 1947 | /// \brief Forward iterator for values. |
---|
| 1948 | /// |
---|
| 1949 | /// This iterator is an stl compatible forward |
---|
| 1950 | /// iterator on the values of the map. The values can |
---|
[606] | 1951 | /// be accessed in the <tt>[beginValue, endValue)</tt> range. |
---|
[220] | 1952 | class ValueIterator |
---|
| 1953 | : public std::iterator<std::forward_iterator_tag, Value> { |
---|
[619] | 1954 | friend class CrossRefMap; |
---|
[220] | 1955 | private: |
---|
| 1956 | ValueIterator(typename Container::const_iterator _it) |
---|
| 1957 | : it(_it) {} |
---|
| 1958 | public: |
---|
| 1959 | |
---|
| 1960 | ValueIterator() {} |
---|
| 1961 | |
---|
| 1962 | ValueIterator& operator++() { ++it; return *this; } |
---|
| 1963 | ValueIterator operator++(int) { |
---|
| 1964 | ValueIterator tmp(*this); |
---|
| 1965 | operator++(); |
---|
| 1966 | return tmp; |
---|
| 1967 | } |
---|
| 1968 | |
---|
| 1969 | const Value& operator*() const { return it->first; } |
---|
| 1970 | const Value* operator->() const { return &(it->first); } |
---|
| 1971 | |
---|
| 1972 | bool operator==(ValueIterator jt) const { return it == jt.it; } |
---|
| 1973 | bool operator!=(ValueIterator jt) const { return it != jt.it; } |
---|
| 1974 | |
---|
| 1975 | private: |
---|
| 1976 | typename Container::const_iterator it; |
---|
| 1977 | }; |
---|
| 1978 | |
---|
| 1979 | /// \brief Returns an iterator to the first value. |
---|
| 1980 | /// |
---|
| 1981 | /// Returns an stl compatible iterator to the |
---|
| 1982 | /// first value of the map. The values of the |
---|
[606] | 1983 | /// map can be accessed in the <tt>[beginValue, endValue)</tt> |
---|
[220] | 1984 | /// range. |
---|
| 1985 | ValueIterator beginValue() const { |
---|
| 1986 | return ValueIterator(_inv_map.begin()); |
---|
| 1987 | } |
---|
| 1988 | |
---|
| 1989 | /// \brief Returns an iterator after the last value. |
---|
| 1990 | /// |
---|
| 1991 | /// Returns an stl compatible iterator after the |
---|
| 1992 | /// last value of the map. The values of the |
---|
[606] | 1993 | /// map can be accessed in the <tt>[beginValue, endValue)</tt> |
---|
[220] | 1994 | /// range. |
---|
| 1995 | ValueIterator endValue() const { |
---|
| 1996 | return ValueIterator(_inv_map.end()); |
---|
| 1997 | } |
---|
| 1998 | |
---|
[606] | 1999 | /// \brief Sets the value associated with the given key. |
---|
[220] | 2000 | /// |
---|
[606] | 2001 | /// Sets the value associated with the given key. |
---|
[220] | 2002 | void set(const Key& key, const Value& val) { |
---|
| 2003 | Value oldval = Map::operator[](key); |
---|
| 2004 | typename Container::iterator it = _inv_map.find(oldval); |
---|
| 2005 | if (it != _inv_map.end() && it->second == key) { |
---|
| 2006 | _inv_map.erase(it); |
---|
| 2007 | } |
---|
[740] | 2008 | _inv_map.insert(std::make_pair(val, key)); |
---|
[220] | 2009 | Map::set(key, val); |
---|
| 2010 | } |
---|
| 2011 | |
---|
[606] | 2012 | /// \brief Returns the value associated with the given key. |
---|
[220] | 2013 | /// |
---|
[606] | 2014 | /// Returns the value associated with the given key. |
---|
[220] | 2015 | typename MapTraits<Map>::ConstReturnValue |
---|
| 2016 | operator[](const Key& key) const { |
---|
| 2017 | return Map::operator[](key); |
---|
| 2018 | } |
---|
| 2019 | |
---|
| 2020 | /// \brief Gives back the item by its value. |
---|
| 2021 | /// |
---|
| 2022 | /// Gives back the item by its value. |
---|
| 2023 | Key operator()(const Value& key) const { |
---|
| 2024 | typename Container::const_iterator it = _inv_map.find(key); |
---|
| 2025 | return it != _inv_map.end() ? it->second : INVALID; |
---|
| 2026 | } |
---|
| 2027 | |
---|
| 2028 | protected: |
---|
| 2029 | |
---|
[606] | 2030 | /// \brief Erase the key from the map and the inverse map. |
---|
[220] | 2031 | /// |
---|
[606] | 2032 | /// Erase the key from the map and the inverse map. It is called by the |
---|
[220] | 2033 | /// \c AlterationNotifier. |
---|
| 2034 | virtual void erase(const Key& key) { |
---|
| 2035 | Value val = Map::operator[](key); |
---|
| 2036 | typename Container::iterator it = _inv_map.find(val); |
---|
| 2037 | if (it != _inv_map.end() && it->second == key) { |
---|
| 2038 | _inv_map.erase(it); |
---|
| 2039 | } |
---|
| 2040 | Map::erase(key); |
---|
| 2041 | } |
---|
| 2042 | |
---|
[606] | 2043 | /// \brief Erase more keys from the map and the inverse map. |
---|
[220] | 2044 | /// |
---|
[606] | 2045 | /// Erase more keys from the map and the inverse map. It is called by the |
---|
[220] | 2046 | /// \c AlterationNotifier. |
---|
| 2047 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 2048 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 2049 | Value val = Map::operator[](keys[i]); |
---|
| 2050 | typename Container::iterator it = _inv_map.find(val); |
---|
| 2051 | if (it != _inv_map.end() && it->second == keys[i]) { |
---|
| 2052 | _inv_map.erase(it); |
---|
| 2053 | } |
---|
| 2054 | } |
---|
| 2055 | Map::erase(keys); |
---|
| 2056 | } |
---|
| 2057 | |
---|
[606] | 2058 | /// \brief Clear the keys from the map and the inverse map. |
---|
[220] | 2059 | /// |
---|
[606] | 2060 | /// Clear the keys from the map and the inverse map. It is called by the |
---|
[220] | 2061 | /// \c AlterationNotifier. |
---|
| 2062 | virtual void clear() { |
---|
| 2063 | _inv_map.clear(); |
---|
| 2064 | Map::clear(); |
---|
| 2065 | } |
---|
| 2066 | |
---|
| 2067 | public: |
---|
| 2068 | |
---|
| 2069 | /// \brief The inverse map type. |
---|
| 2070 | /// |
---|
| 2071 | /// The inverse of this map. The subscript operator of the map |
---|
[606] | 2072 | /// gives back the item that was last assigned to the value. |
---|
[220] | 2073 | class InverseMap { |
---|
| 2074 | public: |
---|
[606] | 2075 | /// \brief Constructor |
---|
[220] | 2076 | /// |
---|
| 2077 | /// Constructor of the InverseMap. |
---|
[619] | 2078 | explicit InverseMap(const CrossRefMap& inverted) |
---|
[220] | 2079 | : _inverted(inverted) {} |
---|
| 2080 | |
---|
| 2081 | /// The value type of the InverseMap. |
---|
[619] | 2082 | typedef typename CrossRefMap::Key Value; |
---|
[220] | 2083 | /// The key type of the InverseMap. |
---|
[619] | 2084 | typedef typename CrossRefMap::Value Key; |
---|
[220] | 2085 | |
---|
| 2086 | /// \brief Subscript operator. |
---|
| 2087 | /// |
---|
[606] | 2088 | /// Subscript operator. It gives back the item |
---|
| 2089 | /// that was last assigned to the given value. |
---|
[220] | 2090 | Value operator[](const Key& key) const { |
---|
| 2091 | return _inverted(key); |
---|
| 2092 | } |
---|
| 2093 | |
---|
| 2094 | private: |
---|
[619] | 2095 | const CrossRefMap& _inverted; |
---|
[220] | 2096 | }; |
---|
| 2097 | |
---|
[606] | 2098 | /// \brief It gives back the read-only inverse map. |
---|
[220] | 2099 | /// |
---|
[606] | 2100 | /// It gives back the read-only inverse map. |
---|
[220] | 2101 | InverseMap inverse() const { |
---|
| 2102 | return InverseMap(*this); |
---|
| 2103 | } |
---|
| 2104 | |
---|
| 2105 | }; |
---|
| 2106 | |
---|
[619] | 2107 | /// \brief Provides continuous and unique ID for the |
---|
| 2108 | /// items of a graph. |
---|
[220] | 2109 | /// |
---|
[619] | 2110 | /// RangeIdMap provides a unique and continuous |
---|
| 2111 | /// ID for each item of a given type (\c Node, \c Arc or |
---|
[606] | 2112 | /// \c Edge) in a graph. This id is |
---|
| 2113 | /// - \b unique: different items get different ids, |
---|
| 2114 | /// - \b continuous: the range of the ids is the set of integers |
---|
| 2115 | /// between 0 and \c n-1, where \c n is the number of the items of |
---|
[619] | 2116 | /// this type (\c Node, \c Arc or \c Edge). |
---|
| 2117 | /// - So, the ids can change when deleting an item of the same type. |
---|
[220] | 2118 | /// |
---|
[606] | 2119 | /// Thus this id is not (necessarily) the same as what can get using |
---|
| 2120 | /// the \c id() function of the graph or \ref IdMap. |
---|
| 2121 | /// This map can be inverted with its member class \c InverseMap, |
---|
| 2122 | /// or with the \c operator() member. |
---|
| 2123 | /// |
---|
| 2124 | /// \tparam GR The graph type. |
---|
| 2125 | /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
---|
| 2126 | /// \c GR::Edge). |
---|
| 2127 | /// |
---|
| 2128 | /// \see IdMap |
---|
| 2129 | template <typename GR, typename K> |
---|
[619] | 2130 | class RangeIdMap |
---|
[606] | 2131 | : protected ItemSetTraits<GR, K>::template Map<int>::Type { |
---|
| 2132 | |
---|
| 2133 | typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map; |
---|
[220] | 2134 | |
---|
| 2135 | public: |
---|
[619] | 2136 | /// The graph type of RangeIdMap. |
---|
[606] | 2137 | typedef GR Graph; |
---|
[664] | 2138 | typedef GR Digraph; |
---|
[619] | 2139 | /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge). |
---|
[606] | 2140 | typedef K Item; |
---|
[619] | 2141 | /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge). |
---|
[606] | 2142 | typedef K Key; |
---|
[619] | 2143 | /// The value type of RangeIdMap. |
---|
[606] | 2144 | typedef int Value; |
---|
[220] | 2145 | |
---|
| 2146 | /// \brief Constructor. |
---|
| 2147 | /// |
---|
[619] | 2148 | /// Constructor. |
---|
| 2149 | explicit RangeIdMap(const Graph& gr) : Map(gr) { |
---|
[220] | 2150 | Item it; |
---|
| 2151 | const typename Map::Notifier* nf = Map::notifier(); |
---|
| 2152 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 2153 | Map::set(it, _inv_map.size()); |
---|
| 2154 | _inv_map.push_back(it); |
---|
| 2155 | } |
---|
| 2156 | } |
---|
| 2157 | |
---|
| 2158 | protected: |
---|
| 2159 | |
---|
[606] | 2160 | /// \brief Adds a new key to the map. |
---|
[220] | 2161 | /// |
---|
| 2162 | /// Add a new key to the map. It is called by the |
---|
| 2163 | /// \c AlterationNotifier. |
---|
| 2164 | virtual void add(const Item& item) { |
---|
| 2165 | Map::add(item); |
---|
| 2166 | Map::set(item, _inv_map.size()); |
---|
| 2167 | _inv_map.push_back(item); |
---|
| 2168 | } |
---|
| 2169 | |
---|
| 2170 | /// \brief Add more new keys to the map. |
---|
| 2171 | /// |
---|
| 2172 | /// Add more new keys to the map. It is called by the |
---|
| 2173 | /// \c AlterationNotifier. |
---|
| 2174 | virtual void add(const std::vector<Item>& items) { |
---|
| 2175 | Map::add(items); |
---|
| 2176 | for (int i = 0; i < int(items.size()); ++i) { |
---|
| 2177 | Map::set(items[i], _inv_map.size()); |
---|
| 2178 | _inv_map.push_back(items[i]); |
---|
| 2179 | } |
---|
| 2180 | } |
---|
| 2181 | |
---|
| 2182 | /// \brief Erase the key from the map. |
---|
| 2183 | /// |
---|
| 2184 | /// Erase the key from the map. It is called by the |
---|
| 2185 | /// \c AlterationNotifier. |
---|
| 2186 | virtual void erase(const Item& item) { |
---|
| 2187 | Map::set(_inv_map.back(), Map::operator[](item)); |
---|
| 2188 | _inv_map[Map::operator[](item)] = _inv_map.back(); |
---|
| 2189 | _inv_map.pop_back(); |
---|
| 2190 | Map::erase(item); |
---|
| 2191 | } |
---|
| 2192 | |
---|
| 2193 | /// \brief Erase more keys from the map. |
---|
| 2194 | /// |
---|
| 2195 | /// Erase more keys from the map. It is called by the |
---|
| 2196 | /// \c AlterationNotifier. |
---|
| 2197 | virtual void erase(const std::vector<Item>& items) { |
---|
| 2198 | for (int i = 0; i < int(items.size()); ++i) { |
---|
| 2199 | Map::set(_inv_map.back(), Map::operator[](items[i])); |
---|
| 2200 | _inv_map[Map::operator[](items[i])] = _inv_map.back(); |
---|
| 2201 | _inv_map.pop_back(); |
---|
| 2202 | } |
---|
| 2203 | Map::erase(items); |
---|
| 2204 | } |
---|
| 2205 | |
---|
| 2206 | /// \brief Build the unique map. |
---|
| 2207 | /// |
---|
| 2208 | /// Build the unique map. It is called by the |
---|
| 2209 | /// \c AlterationNotifier. |
---|
| 2210 | virtual void build() { |
---|
| 2211 | Map::build(); |
---|
| 2212 | Item it; |
---|
| 2213 | const typename Map::Notifier* nf = Map::notifier(); |
---|
| 2214 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 2215 | Map::set(it, _inv_map.size()); |
---|
| 2216 | _inv_map.push_back(it); |
---|
| 2217 | } |
---|
| 2218 | } |
---|
| 2219 | |
---|
| 2220 | /// \brief Clear the keys from the map. |
---|
| 2221 | /// |
---|
| 2222 | /// Clear the keys from the map. It is called by the |
---|
| 2223 | /// \c AlterationNotifier. |
---|
| 2224 | virtual void clear() { |
---|
| 2225 | _inv_map.clear(); |
---|
| 2226 | Map::clear(); |
---|
| 2227 | } |
---|
| 2228 | |
---|
| 2229 | public: |
---|
| 2230 | |
---|
| 2231 | /// \brief Returns the maximal value plus one. |
---|
| 2232 | /// |
---|
| 2233 | /// Returns the maximal value plus one in the map. |
---|
| 2234 | unsigned int size() const { |
---|
| 2235 | return _inv_map.size(); |
---|
| 2236 | } |
---|
| 2237 | |
---|
| 2238 | /// \brief Swaps the position of the two items in the map. |
---|
| 2239 | /// |
---|
| 2240 | /// Swaps the position of the two items in the map. |
---|
| 2241 | void swap(const Item& p, const Item& q) { |
---|
| 2242 | int pi = Map::operator[](p); |
---|
| 2243 | int qi = Map::operator[](q); |
---|
| 2244 | Map::set(p, qi); |
---|
| 2245 | _inv_map[qi] = p; |
---|
| 2246 | Map::set(q, pi); |
---|
| 2247 | _inv_map[pi] = q; |
---|
| 2248 | } |
---|
| 2249 | |
---|
[619] | 2250 | /// \brief Gives back the \e RangeId of the item |
---|
[220] | 2251 | /// |
---|
[619] | 2252 | /// Gives back the \e RangeId of the item. |
---|
[220] | 2253 | int operator[](const Item& item) const { |
---|
| 2254 | return Map::operator[](item); |
---|
| 2255 | } |
---|
| 2256 | |
---|
[619] | 2257 | /// \brief Gives back the item belonging to a \e RangeId |
---|
[740] | 2258 | /// |
---|
[619] | 2259 | /// Gives back the item belonging to a \e RangeId. |
---|
[220] | 2260 | Item operator()(int id) const { |
---|
| 2261 | return _inv_map[id]; |
---|
| 2262 | } |
---|
| 2263 | |
---|
| 2264 | private: |
---|
| 2265 | |
---|
| 2266 | typedef std::vector<Item> Container; |
---|
| 2267 | Container _inv_map; |
---|
| 2268 | |
---|
| 2269 | public: |
---|
[606] | 2270 | |
---|
[619] | 2271 | /// \brief The inverse map type of RangeIdMap. |
---|
[220] | 2272 | /// |
---|
[619] | 2273 | /// The inverse map type of RangeIdMap. |
---|
[220] | 2274 | class InverseMap { |
---|
| 2275 | public: |
---|
[606] | 2276 | /// \brief Constructor |
---|
[220] | 2277 | /// |
---|
| 2278 | /// Constructor of the InverseMap. |
---|
[619] | 2279 | explicit InverseMap(const RangeIdMap& inverted) |
---|
[220] | 2280 | : _inverted(inverted) {} |
---|
| 2281 | |
---|
| 2282 | |
---|
| 2283 | /// The value type of the InverseMap. |
---|
[619] | 2284 | typedef typename RangeIdMap::Key Value; |
---|
[220] | 2285 | /// The key type of the InverseMap. |
---|
[619] | 2286 | typedef typename RangeIdMap::Value Key; |
---|
[220] | 2287 | |
---|
| 2288 | /// \brief Subscript operator. |
---|
| 2289 | /// |
---|
| 2290 | /// Subscript operator. It gives back the item |
---|
[606] | 2291 | /// that the descriptor currently belongs to. |
---|
[220] | 2292 | Value operator[](const Key& key) const { |
---|
| 2293 | return _inverted(key); |
---|
| 2294 | } |
---|
| 2295 | |
---|
| 2296 | /// \brief Size of the map. |
---|
| 2297 | /// |
---|
| 2298 | /// Returns the size of the map. |
---|
| 2299 | unsigned int size() const { |
---|
| 2300 | return _inverted.size(); |
---|
| 2301 | } |
---|
| 2302 | |
---|
| 2303 | private: |
---|
[619] | 2304 | const RangeIdMap& _inverted; |
---|
[220] | 2305 | }; |
---|
| 2306 | |
---|
| 2307 | /// \brief Gives back the inverse of the map. |
---|
| 2308 | /// |
---|
| 2309 | /// Gives back the inverse of the map. |
---|
| 2310 | const InverseMap inverse() const { |
---|
| 2311 | return InverseMap(*this); |
---|
| 2312 | } |
---|
| 2313 | }; |
---|
| 2314 | |
---|
[741] | 2315 | /// \brief Dynamic iterable \c bool map. |
---|
[740] | 2316 | /// |
---|
[741] | 2317 | /// This class provides a special graph map type which can store a |
---|
| 2318 | /// \c bool value for graph items (\c Node, \c Arc or \c Edge). |
---|
| 2319 | /// For both \c true and \c false values it is possible to iterate on |
---|
| 2320 | /// the keys. |
---|
[740] | 2321 | /// |
---|
[741] | 2322 | /// This type is a reference map, so it can be modified with the |
---|
| 2323 | /// subscription operator. |
---|
| 2324 | /// |
---|
| 2325 | /// \tparam GR The graph type. |
---|
| 2326 | /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
---|
| 2327 | /// \c GR::Edge). |
---|
| 2328 | /// |
---|
| 2329 | /// \see IterableIntMap, IterableValueMap |
---|
| 2330 | /// \see CrossRefMap |
---|
| 2331 | template <typename GR, typename K> |
---|
[740] | 2332 | class IterableBoolMap |
---|
[741] | 2333 | : protected ItemSetTraits<GR, K>::template Map<int>::Type { |
---|
[740] | 2334 | private: |
---|
| 2335 | typedef GR Graph; |
---|
| 2336 | |
---|
[741] | 2337 | typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt; |
---|
| 2338 | typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent; |
---|
| 2339 | |
---|
| 2340 | std::vector<K> _array; |
---|
[740] | 2341 | int _sep; |
---|
| 2342 | |
---|
| 2343 | public: |
---|
| 2344 | |
---|
[741] | 2345 | /// Indicates that the map is reference map. |
---|
[740] | 2346 | typedef True ReferenceMapTag; |
---|
| 2347 | |
---|
| 2348 | /// The key type |
---|
[741] | 2349 | typedef K Key; |
---|
[740] | 2350 | /// The value type |
---|
| 2351 | typedef bool Value; |
---|
| 2352 | /// The const reference type. |
---|
| 2353 | typedef const Value& ConstReference; |
---|
| 2354 | |
---|
| 2355 | private: |
---|
| 2356 | |
---|
| 2357 | int position(const Key& key) const { |
---|
| 2358 | return Parent::operator[](key); |
---|
| 2359 | } |
---|
| 2360 | |
---|
| 2361 | public: |
---|
| 2362 | |
---|
[741] | 2363 | /// \brief Reference to the value of the map. |
---|
[740] | 2364 | /// |
---|
[741] | 2365 | /// This class is similar to the \c bool type. It can be converted to |
---|
| 2366 | /// \c bool and it provides the same operators. |
---|
[740] | 2367 | class Reference { |
---|
| 2368 | friend class IterableBoolMap; |
---|
| 2369 | private: |
---|
| 2370 | Reference(IterableBoolMap& map, const Key& key) |
---|
| 2371 | : _key(key), _map(map) {} |
---|
| 2372 | public: |
---|
| 2373 | |
---|
| 2374 | Reference& operator=(const Reference& value) { |
---|
| 2375 | _map.set(_key, static_cast<bool>(value)); |
---|
| 2376 | return *this; |
---|
| 2377 | } |
---|
| 2378 | |
---|
| 2379 | operator bool() const { |
---|
| 2380 | return static_cast<const IterableBoolMap&>(_map)[_key]; |
---|
| 2381 | } |
---|
| 2382 | |
---|
| 2383 | Reference& operator=(bool value) { |
---|
| 2384 | _map.set(_key, value); |
---|
| 2385 | return *this; |
---|
| 2386 | } |
---|
| 2387 | Reference& operator&=(bool value) { |
---|
| 2388 | _map.set(_key, _map[_key] & value); |
---|
| 2389 | return *this; |
---|
| 2390 | } |
---|
| 2391 | Reference& operator|=(bool value) { |
---|
| 2392 | _map.set(_key, _map[_key] | value); |
---|
| 2393 | return *this; |
---|
| 2394 | } |
---|
| 2395 | Reference& operator^=(bool value) { |
---|
| 2396 | _map.set(_key, _map[_key] ^ value); |
---|
| 2397 | return *this; |
---|
| 2398 | } |
---|
| 2399 | private: |
---|
| 2400 | Key _key; |
---|
| 2401 | IterableBoolMap& _map; |
---|
| 2402 | }; |
---|
| 2403 | |
---|
| 2404 | /// \brief Constructor of the map with a default value. |
---|
| 2405 | /// |
---|
| 2406 | /// Constructor of the map with a default value. |
---|
| 2407 | explicit IterableBoolMap(const Graph& graph, bool def = false) |
---|
| 2408 | : Parent(graph) { |
---|
| 2409 | typename Parent::Notifier* nf = Parent::notifier(); |
---|
| 2410 | Key it; |
---|
| 2411 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 2412 | Parent::set(it, _array.size()); |
---|
| 2413 | _array.push_back(it); |
---|
| 2414 | } |
---|
| 2415 | _sep = (def ? _array.size() : 0); |
---|
| 2416 | } |
---|
| 2417 | |
---|
| 2418 | /// \brief Const subscript operator of the map. |
---|
| 2419 | /// |
---|
| 2420 | /// Const subscript operator of the map. |
---|
| 2421 | bool operator[](const Key& key) const { |
---|
| 2422 | return position(key) < _sep; |
---|
| 2423 | } |
---|
| 2424 | |
---|
| 2425 | /// \brief Subscript operator of the map. |
---|
| 2426 | /// |
---|
| 2427 | /// Subscript operator of the map. |
---|
| 2428 | Reference operator[](const Key& key) { |
---|
| 2429 | return Reference(*this, key); |
---|
| 2430 | } |
---|
| 2431 | |
---|
| 2432 | /// \brief Set operation of the map. |
---|
| 2433 | /// |
---|
| 2434 | /// Set operation of the map. |
---|
| 2435 | void set(const Key& key, bool value) { |
---|
| 2436 | int pos = position(key); |
---|
| 2437 | if (value) { |
---|
| 2438 | if (pos < _sep) return; |
---|
| 2439 | Key tmp = _array[_sep]; |
---|
| 2440 | _array[_sep] = key; |
---|
| 2441 | Parent::set(key, _sep); |
---|
| 2442 | _array[pos] = tmp; |
---|
| 2443 | Parent::set(tmp, pos); |
---|
| 2444 | ++_sep; |
---|
| 2445 | } else { |
---|
| 2446 | if (pos >= _sep) return; |
---|
| 2447 | --_sep; |
---|
| 2448 | Key tmp = _array[_sep]; |
---|
| 2449 | _array[_sep] = key; |
---|
| 2450 | Parent::set(key, _sep); |
---|
| 2451 | _array[pos] = tmp; |
---|
| 2452 | Parent::set(tmp, pos); |
---|
| 2453 | } |
---|
| 2454 | } |
---|
| 2455 | |
---|
| 2456 | /// \brief Set all items. |
---|
| 2457 | /// |
---|
| 2458 | /// Set all items in the map. |
---|
| 2459 | /// \note Constant time operation. |
---|
| 2460 | void setAll(bool value) { |
---|
| 2461 | _sep = (value ? _array.size() : 0); |
---|
| 2462 | } |
---|
| 2463 | |
---|
[741] | 2464 | /// \brief Returns the number of the keys mapped to \c true. |
---|
[740] | 2465 | /// |
---|
[741] | 2466 | /// Returns the number of the keys mapped to \c true. |
---|
[740] | 2467 | int trueNum() const { |
---|
| 2468 | return _sep; |
---|
| 2469 | } |
---|
| 2470 | |
---|
[741] | 2471 | /// \brief Returns the number of the keys mapped to \c false. |
---|
[740] | 2472 | /// |
---|
[741] | 2473 | /// Returns the number of the keys mapped to \c false. |
---|
[740] | 2474 | int falseNum() const { |
---|
| 2475 | return _array.size() - _sep; |
---|
| 2476 | } |
---|
| 2477 | |
---|
[741] | 2478 | /// \brief Iterator for the keys mapped to \c true. |
---|
[740] | 2479 | /// |
---|
[741] | 2480 | /// Iterator for the keys mapped to \c true. It works |
---|
| 2481 | /// like a graph item iterator, it can be converted to |
---|
[740] | 2482 | /// the key type of the map, incremented with \c ++ operator, and |
---|
[741] | 2483 | /// if the iterator leaves the last valid key, it will be equal to |
---|
[740] | 2484 | /// \c INVALID. |
---|
| 2485 | class TrueIt : public Key { |
---|
| 2486 | public: |
---|
| 2487 | typedef Key Parent; |
---|
| 2488 | |
---|
| 2489 | /// \brief Creates an iterator. |
---|
| 2490 | /// |
---|
| 2491 | /// Creates an iterator. It iterates on the |
---|
[741] | 2492 | /// keys mapped to \c true. |
---|
| 2493 | /// \param map The IterableBoolMap. |
---|
[740] | 2494 | explicit TrueIt(const IterableBoolMap& map) |
---|
| 2495 | : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID), |
---|
| 2496 | _map(&map) {} |
---|
| 2497 | |
---|
| 2498 | /// \brief Invalid constructor \& conversion. |
---|
| 2499 | /// |
---|
[741] | 2500 | /// This constructor initializes the iterator to be invalid. |
---|
[740] | 2501 | /// \sa Invalid for more details. |
---|
| 2502 | TrueIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 2503 | |
---|
| 2504 | /// \brief Increment operator. |
---|
| 2505 | /// |
---|
[741] | 2506 | /// Increment operator. |
---|
[740] | 2507 | TrueIt& operator++() { |
---|
| 2508 | int pos = _map->position(*this); |
---|
| 2509 | Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID); |
---|
| 2510 | return *this; |
---|
| 2511 | } |
---|
| 2512 | |
---|
| 2513 | private: |
---|
| 2514 | const IterableBoolMap* _map; |
---|
| 2515 | }; |
---|
| 2516 | |
---|
[741] | 2517 | /// \brief Iterator for the keys mapped to \c false. |
---|
[740] | 2518 | /// |
---|
[741] | 2519 | /// Iterator for the keys mapped to \c false. It works |
---|
| 2520 | /// like a graph item iterator, it can be converted to |
---|
[740] | 2521 | /// the key type of the map, incremented with \c ++ operator, and |
---|
[741] | 2522 | /// if the iterator leaves the last valid key, it will be equal to |
---|
[740] | 2523 | /// \c INVALID. |
---|
| 2524 | class FalseIt : public Key { |
---|
| 2525 | public: |
---|
| 2526 | typedef Key Parent; |
---|
| 2527 | |
---|
| 2528 | /// \brief Creates an iterator. |
---|
| 2529 | /// |
---|
| 2530 | /// Creates an iterator. It iterates on the |
---|
[741] | 2531 | /// keys mapped to \c false. |
---|
| 2532 | /// \param map The IterableBoolMap. |
---|
[740] | 2533 | explicit FalseIt(const IterableBoolMap& map) |
---|
| 2534 | : Parent(map._sep < int(map._array.size()) ? |
---|
| 2535 | map._array.back() : INVALID), _map(&map) {} |
---|
| 2536 | |
---|
| 2537 | /// \brief Invalid constructor \& conversion. |
---|
| 2538 | /// |
---|
[741] | 2539 | /// This constructor initializes the iterator to be invalid. |
---|
[740] | 2540 | /// \sa Invalid for more details. |
---|
| 2541 | FalseIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 2542 | |
---|
| 2543 | /// \brief Increment operator. |
---|
| 2544 | /// |
---|
[741] | 2545 | /// Increment operator. |
---|
[740] | 2546 | FalseIt& operator++() { |
---|
| 2547 | int pos = _map->position(*this); |
---|
| 2548 | Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID); |
---|
| 2549 | return *this; |
---|
| 2550 | } |
---|
| 2551 | |
---|
| 2552 | private: |
---|
| 2553 | const IterableBoolMap* _map; |
---|
| 2554 | }; |
---|
| 2555 | |
---|
| 2556 | /// \brief Iterator for the keys mapped to a given value. |
---|
| 2557 | /// |
---|
| 2558 | /// Iterator for the keys mapped to a given value. It works |
---|
[741] | 2559 | /// like a graph item iterator, it can be converted to |
---|
[740] | 2560 | /// the key type of the map, incremented with \c ++ operator, and |
---|
[741] | 2561 | /// if the iterator leaves the last valid key, it will be equal to |
---|
[740] | 2562 | /// \c INVALID. |
---|
| 2563 | class ItemIt : public Key { |
---|
| 2564 | public: |
---|
| 2565 | typedef Key Parent; |
---|
| 2566 | |
---|
[741] | 2567 | /// \brief Creates an iterator with a value. |
---|
[740] | 2568 | /// |
---|
[741] | 2569 | /// Creates an iterator with a value. It iterates on the |
---|
| 2570 | /// keys mapped to the given value. |
---|
| 2571 | /// \param map The IterableBoolMap. |
---|
| 2572 | /// \param value The value. |
---|
[740] | 2573 | ItemIt(const IterableBoolMap& map, bool value) |
---|
| 2574 | : Parent(value ? |
---|
| 2575 | (map._sep > 0 ? |
---|
| 2576 | map._array[map._sep - 1] : INVALID) : |
---|
| 2577 | (map._sep < int(map._array.size()) ? |
---|
| 2578 | map._array.back() : INVALID)), _map(&map) {} |
---|
| 2579 | |
---|
| 2580 | /// \brief Invalid constructor \& conversion. |
---|
| 2581 | /// |
---|
[741] | 2582 | /// This constructor initializes the iterator to be invalid. |
---|
[740] | 2583 | /// \sa Invalid for more details. |
---|
| 2584 | ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 2585 | |
---|
| 2586 | /// \brief Increment operator. |
---|
| 2587 | /// |
---|
[741] | 2588 | /// Increment operator. |
---|
[740] | 2589 | ItemIt& operator++() { |
---|
| 2590 | int pos = _map->position(*this); |
---|
| 2591 | int _sep = pos >= _map->_sep ? _map->_sep : 0; |
---|
| 2592 | Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID); |
---|
| 2593 | return *this; |
---|
| 2594 | } |
---|
| 2595 | |
---|
| 2596 | private: |
---|
| 2597 | const IterableBoolMap* _map; |
---|
| 2598 | }; |
---|
| 2599 | |
---|
| 2600 | protected: |
---|
| 2601 | |
---|
| 2602 | virtual void add(const Key& key) { |
---|
| 2603 | Parent::add(key); |
---|
| 2604 | Parent::set(key, _array.size()); |
---|
| 2605 | _array.push_back(key); |
---|
| 2606 | } |
---|
| 2607 | |
---|
| 2608 | virtual void add(const std::vector<Key>& keys) { |
---|
| 2609 | Parent::add(keys); |
---|
| 2610 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 2611 | Parent::set(keys[i], _array.size()); |
---|
| 2612 | _array.push_back(keys[i]); |
---|
| 2613 | } |
---|
| 2614 | } |
---|
| 2615 | |
---|
| 2616 | virtual void erase(const Key& key) { |
---|
| 2617 | int pos = position(key); |
---|
| 2618 | if (pos < _sep) { |
---|
| 2619 | --_sep; |
---|
| 2620 | Parent::set(_array[_sep], pos); |
---|
| 2621 | _array[pos] = _array[_sep]; |
---|
| 2622 | Parent::set(_array.back(), _sep); |
---|
| 2623 | _array[_sep] = _array.back(); |
---|
| 2624 | _array.pop_back(); |
---|
| 2625 | } else { |
---|
| 2626 | Parent::set(_array.back(), pos); |
---|
| 2627 | _array[pos] = _array.back(); |
---|
| 2628 | _array.pop_back(); |
---|
| 2629 | } |
---|
| 2630 | Parent::erase(key); |
---|
| 2631 | } |
---|
| 2632 | |
---|
| 2633 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 2634 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 2635 | int pos = position(keys[i]); |
---|
| 2636 | if (pos < _sep) { |
---|
| 2637 | --_sep; |
---|
| 2638 | Parent::set(_array[_sep], pos); |
---|
| 2639 | _array[pos] = _array[_sep]; |
---|
| 2640 | Parent::set(_array.back(), _sep); |
---|
| 2641 | _array[_sep] = _array.back(); |
---|
| 2642 | _array.pop_back(); |
---|
| 2643 | } else { |
---|
| 2644 | Parent::set(_array.back(), pos); |
---|
| 2645 | _array[pos] = _array.back(); |
---|
| 2646 | _array.pop_back(); |
---|
| 2647 | } |
---|
| 2648 | } |
---|
| 2649 | Parent::erase(keys); |
---|
| 2650 | } |
---|
| 2651 | |
---|
| 2652 | virtual void build() { |
---|
| 2653 | Parent::build(); |
---|
| 2654 | typename Parent::Notifier* nf = Parent::notifier(); |
---|
| 2655 | Key it; |
---|
| 2656 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 2657 | Parent::set(it, _array.size()); |
---|
| 2658 | _array.push_back(it); |
---|
| 2659 | } |
---|
| 2660 | _sep = 0; |
---|
| 2661 | } |
---|
| 2662 | |
---|
| 2663 | virtual void clear() { |
---|
| 2664 | _array.clear(); |
---|
| 2665 | _sep = 0; |
---|
| 2666 | Parent::clear(); |
---|
| 2667 | } |
---|
| 2668 | |
---|
| 2669 | }; |
---|
| 2670 | |
---|
| 2671 | |
---|
| 2672 | namespace _maps_bits { |
---|
| 2673 | template <typename Item> |
---|
| 2674 | struct IterableIntMapNode { |
---|
| 2675 | IterableIntMapNode() : value(-1) {} |
---|
| 2676 | IterableIntMapNode(int _value) : value(_value) {} |
---|
| 2677 | Item prev, next; |
---|
| 2678 | int value; |
---|
| 2679 | }; |
---|
| 2680 | } |
---|
| 2681 | |
---|
| 2682 | /// \brief Dynamic iterable integer map. |
---|
| 2683 | /// |
---|
[741] | 2684 | /// This class provides a special graph map type which can store an |
---|
| 2685 | /// integer value for graph items (\c Node, \c Arc or \c Edge). |
---|
| 2686 | /// For each non-negative value it is possible to iterate on the keys |
---|
| 2687 | /// mapped to the value. |
---|
[740] | 2688 | /// |
---|
[741] | 2689 | /// This type is a reference map, so it can be modified with the |
---|
| 2690 | /// subscription operator. |
---|
| 2691 | /// |
---|
| 2692 | /// \note The size of the data structure depends on the largest |
---|
[740] | 2693 | /// value in the map. |
---|
| 2694 | /// |
---|
[741] | 2695 | /// \tparam GR The graph type. |
---|
| 2696 | /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
---|
| 2697 | /// \c GR::Edge). |
---|
| 2698 | /// |
---|
| 2699 | /// \see IterableBoolMap, IterableValueMap |
---|
| 2700 | /// \see CrossRefMap |
---|
| 2701 | template <typename GR, typename K> |
---|
[740] | 2702 | class IterableIntMap |
---|
[741] | 2703 | : protected ItemSetTraits<GR, K>:: |
---|
| 2704 | template Map<_maps_bits::IterableIntMapNode<K> >::Type { |
---|
[740] | 2705 | public: |
---|
[741] | 2706 | typedef typename ItemSetTraits<GR, K>:: |
---|
| 2707 | template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent; |
---|
[740] | 2708 | |
---|
| 2709 | /// The key type |
---|
[741] | 2710 | typedef K Key; |
---|
[740] | 2711 | /// The value type |
---|
| 2712 | typedef int Value; |
---|
| 2713 | /// The graph type |
---|
| 2714 | typedef GR Graph; |
---|
| 2715 | |
---|
| 2716 | /// \brief Constructor of the map. |
---|
| 2717 | /// |
---|
[741] | 2718 | /// Constructor of the map. It sets all values to -1. |
---|
[740] | 2719 | explicit IterableIntMap(const Graph& graph) |
---|
| 2720 | : Parent(graph) {} |
---|
| 2721 | |
---|
| 2722 | /// \brief Constructor of the map with a given value. |
---|
| 2723 | /// |
---|
| 2724 | /// Constructor of the map with a given value. |
---|
| 2725 | explicit IterableIntMap(const Graph& graph, int value) |
---|
[741] | 2726 | : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) { |
---|
[740] | 2727 | if (value >= 0) { |
---|
| 2728 | for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
---|
| 2729 | lace(it); |
---|
| 2730 | } |
---|
| 2731 | } |
---|
| 2732 | } |
---|
| 2733 | |
---|
| 2734 | private: |
---|
| 2735 | |
---|
| 2736 | void unlace(const Key& key) { |
---|
| 2737 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 2738 | if (node.value < 0) return; |
---|
| 2739 | if (node.prev != INVALID) { |
---|
| 2740 | Parent::operator[](node.prev).next = node.next; |
---|
| 2741 | } else { |
---|
| 2742 | _first[node.value] = node.next; |
---|
| 2743 | } |
---|
| 2744 | if (node.next != INVALID) { |
---|
| 2745 | Parent::operator[](node.next).prev = node.prev; |
---|
| 2746 | } |
---|
| 2747 | while (!_first.empty() && _first.back() == INVALID) { |
---|
| 2748 | _first.pop_back(); |
---|
| 2749 | } |
---|
| 2750 | } |
---|
| 2751 | |
---|
| 2752 | void lace(const Key& key) { |
---|
| 2753 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 2754 | if (node.value < 0) return; |
---|
| 2755 | if (node.value >= int(_first.size())) { |
---|
| 2756 | _first.resize(node.value + 1, INVALID); |
---|
| 2757 | } |
---|
| 2758 | node.prev = INVALID; |
---|
| 2759 | node.next = _first[node.value]; |
---|
| 2760 | if (node.next != INVALID) { |
---|
| 2761 | Parent::operator[](node.next).prev = key; |
---|
| 2762 | } |
---|
| 2763 | _first[node.value] = key; |
---|
| 2764 | } |
---|
| 2765 | |
---|
| 2766 | public: |
---|
| 2767 | |
---|
[741] | 2768 | /// Indicates that the map is reference map. |
---|
[740] | 2769 | typedef True ReferenceMapTag; |
---|
| 2770 | |
---|
[741] | 2771 | /// \brief Reference to the value of the map. |
---|
[740] | 2772 | /// |
---|
[741] | 2773 | /// This class is similar to the \c int type. It can |
---|
| 2774 | /// be converted to \c int and it has the same operators. |
---|
[740] | 2775 | class Reference { |
---|
| 2776 | friend class IterableIntMap; |
---|
| 2777 | private: |
---|
| 2778 | Reference(IterableIntMap& map, const Key& key) |
---|
| 2779 | : _key(key), _map(map) {} |
---|
| 2780 | public: |
---|
| 2781 | |
---|
| 2782 | Reference& operator=(const Reference& value) { |
---|
| 2783 | _map.set(_key, static_cast<const int&>(value)); |
---|
| 2784 | return *this; |
---|
| 2785 | } |
---|
| 2786 | |
---|
| 2787 | operator const int&() const { |
---|
| 2788 | return static_cast<const IterableIntMap&>(_map)[_key]; |
---|
| 2789 | } |
---|
| 2790 | |
---|
| 2791 | Reference& operator=(int value) { |
---|
| 2792 | _map.set(_key, value); |
---|
| 2793 | return *this; |
---|
| 2794 | } |
---|
| 2795 | Reference& operator++() { |
---|
| 2796 | _map.set(_key, _map[_key] + 1); |
---|
| 2797 | return *this; |
---|
| 2798 | } |
---|
| 2799 | int operator++(int) { |
---|
| 2800 | int value = _map[_key]; |
---|
| 2801 | _map.set(_key, value + 1); |
---|
| 2802 | return value; |
---|
| 2803 | } |
---|
| 2804 | Reference& operator--() { |
---|
| 2805 | _map.set(_key, _map[_key] - 1); |
---|
| 2806 | return *this; |
---|
| 2807 | } |
---|
| 2808 | int operator--(int) { |
---|
| 2809 | int value = _map[_key]; |
---|
| 2810 | _map.set(_key, value - 1); |
---|
| 2811 | return value; |
---|
| 2812 | } |
---|
| 2813 | Reference& operator+=(int value) { |
---|
| 2814 | _map.set(_key, _map[_key] + value); |
---|
| 2815 | return *this; |
---|
| 2816 | } |
---|
| 2817 | Reference& operator-=(int value) { |
---|
| 2818 | _map.set(_key, _map[_key] - value); |
---|
| 2819 | return *this; |
---|
| 2820 | } |
---|
| 2821 | Reference& operator*=(int value) { |
---|
| 2822 | _map.set(_key, _map[_key] * value); |
---|
| 2823 | return *this; |
---|
| 2824 | } |
---|
| 2825 | Reference& operator/=(int value) { |
---|
| 2826 | _map.set(_key, _map[_key] / value); |
---|
| 2827 | return *this; |
---|
| 2828 | } |
---|
| 2829 | Reference& operator%=(int value) { |
---|
| 2830 | _map.set(_key, _map[_key] % value); |
---|
| 2831 | return *this; |
---|
| 2832 | } |
---|
| 2833 | Reference& operator&=(int value) { |
---|
| 2834 | _map.set(_key, _map[_key] & value); |
---|
| 2835 | return *this; |
---|
| 2836 | } |
---|
| 2837 | Reference& operator|=(int value) { |
---|
| 2838 | _map.set(_key, _map[_key] | value); |
---|
| 2839 | return *this; |
---|
| 2840 | } |
---|
| 2841 | Reference& operator^=(int value) { |
---|
| 2842 | _map.set(_key, _map[_key] ^ value); |
---|
| 2843 | return *this; |
---|
| 2844 | } |
---|
| 2845 | Reference& operator<<=(int value) { |
---|
| 2846 | _map.set(_key, _map[_key] << value); |
---|
| 2847 | return *this; |
---|
| 2848 | } |
---|
| 2849 | Reference& operator>>=(int value) { |
---|
| 2850 | _map.set(_key, _map[_key] >> value); |
---|
| 2851 | return *this; |
---|
| 2852 | } |
---|
| 2853 | |
---|
| 2854 | private: |
---|
| 2855 | Key _key; |
---|
| 2856 | IterableIntMap& _map; |
---|
| 2857 | }; |
---|
| 2858 | |
---|
| 2859 | /// The const reference type. |
---|
| 2860 | typedef const Value& ConstReference; |
---|
| 2861 | |
---|
| 2862 | /// \brief Gives back the maximal value plus one. |
---|
| 2863 | /// |
---|
| 2864 | /// Gives back the maximal value plus one. |
---|
| 2865 | int size() const { |
---|
| 2866 | return _first.size(); |
---|
| 2867 | } |
---|
| 2868 | |
---|
| 2869 | /// \brief Set operation of the map. |
---|
| 2870 | /// |
---|
| 2871 | /// Set operation of the map. |
---|
| 2872 | void set(const Key& key, const Value& value) { |
---|
| 2873 | unlace(key); |
---|
| 2874 | Parent::operator[](key).value = value; |
---|
| 2875 | lace(key); |
---|
| 2876 | } |
---|
| 2877 | |
---|
| 2878 | /// \brief Const subscript operator of the map. |
---|
| 2879 | /// |
---|
| 2880 | /// Const subscript operator of the map. |
---|
| 2881 | const Value& operator[](const Key& key) const { |
---|
| 2882 | return Parent::operator[](key).value; |
---|
| 2883 | } |
---|
| 2884 | |
---|
| 2885 | /// \brief Subscript operator of the map. |
---|
| 2886 | /// |
---|
| 2887 | /// Subscript operator of the map. |
---|
| 2888 | Reference operator[](const Key& key) { |
---|
| 2889 | return Reference(*this, key); |
---|
| 2890 | } |
---|
| 2891 | |
---|
| 2892 | /// \brief Iterator for the keys with the same value. |
---|
| 2893 | /// |
---|
| 2894 | /// Iterator for the keys with the same value. It works |
---|
[741] | 2895 | /// like a graph item iterator, it can be converted to |
---|
[740] | 2896 | /// the item type of the map, incremented with \c ++ operator, and |
---|
[741] | 2897 | /// if the iterator leaves the last valid item, it will be equal to |
---|
[740] | 2898 | /// \c INVALID. |
---|
[741] | 2899 | class ItemIt : public Key { |
---|
[740] | 2900 | public: |
---|
[741] | 2901 | typedef Key Parent; |
---|
[740] | 2902 | |
---|
| 2903 | /// \brief Invalid constructor \& conversion. |
---|
| 2904 | /// |
---|
[741] | 2905 | /// This constructor initializes the iterator to be invalid. |
---|
[740] | 2906 | /// \sa Invalid for more details. |
---|
| 2907 | ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 2908 | |
---|
| 2909 | /// \brief Creates an iterator with a value. |
---|
| 2910 | /// |
---|
| 2911 | /// Creates an iterator with a value. It iterates on the |
---|
[741] | 2912 | /// keys mapped to the given value. |
---|
| 2913 | /// \param map The IterableIntMap. |
---|
| 2914 | /// \param value The value. |
---|
[740] | 2915 | ItemIt(const IterableIntMap& map, int value) : _map(&map) { |
---|
| 2916 | if (value < 0 || value >= int(_map->_first.size())) { |
---|
| 2917 | Parent::operator=(INVALID); |
---|
| 2918 | } else { |
---|
| 2919 | Parent::operator=(_map->_first[value]); |
---|
| 2920 | } |
---|
| 2921 | } |
---|
| 2922 | |
---|
| 2923 | /// \brief Increment operator. |
---|
| 2924 | /// |
---|
[741] | 2925 | /// Increment operator. |
---|
[740] | 2926 | ItemIt& operator++() { |
---|
| 2927 | Parent::operator=(_map->IterableIntMap::Parent:: |
---|
| 2928 | operator[](static_cast<Parent&>(*this)).next); |
---|
| 2929 | return *this; |
---|
| 2930 | } |
---|
| 2931 | |
---|
| 2932 | private: |
---|
| 2933 | const IterableIntMap* _map; |
---|
| 2934 | }; |
---|
| 2935 | |
---|
| 2936 | protected: |
---|
| 2937 | |
---|
| 2938 | virtual void erase(const Key& key) { |
---|
| 2939 | unlace(key); |
---|
| 2940 | Parent::erase(key); |
---|
| 2941 | } |
---|
| 2942 | |
---|
| 2943 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 2944 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 2945 | unlace(keys[i]); |
---|
| 2946 | } |
---|
| 2947 | Parent::erase(keys); |
---|
| 2948 | } |
---|
| 2949 | |
---|
| 2950 | virtual void clear() { |
---|
| 2951 | _first.clear(); |
---|
| 2952 | Parent::clear(); |
---|
| 2953 | } |
---|
| 2954 | |
---|
| 2955 | private: |
---|
[741] | 2956 | std::vector<Key> _first; |
---|
[740] | 2957 | }; |
---|
| 2958 | |
---|
| 2959 | namespace _maps_bits { |
---|
| 2960 | template <typename Item, typename Value> |
---|
| 2961 | struct IterableValueMapNode { |
---|
| 2962 | IterableValueMapNode(Value _value = Value()) : value(_value) {} |
---|
| 2963 | Item prev, next; |
---|
| 2964 | Value value; |
---|
| 2965 | }; |
---|
| 2966 | } |
---|
| 2967 | |
---|
| 2968 | /// \brief Dynamic iterable map for comparable values. |
---|
| 2969 | /// |
---|
[741] | 2970 | /// This class provides a special graph map type which can store an |
---|
| 2971 | /// comparable value for graph items (\c Node, \c Arc or \c Edge). |
---|
| 2972 | /// For each value it is possible to iterate on the keys mapped to |
---|
| 2973 | /// the value. |
---|
| 2974 | /// |
---|
| 2975 | /// The map stores for each value a linked list with |
---|
[740] | 2976 | /// the items which mapped to the value, and the values are stored |
---|
| 2977 | /// in balanced binary tree. The values of the map can be accessed |
---|
| 2978 | /// with stl compatible forward iterator. |
---|
| 2979 | /// |
---|
[741] | 2980 | /// This type is not reference map, so it cannot be modified with |
---|
[740] | 2981 | /// the subscription operator. |
---|
| 2982 | /// |
---|
[741] | 2983 | /// \tparam GR The graph type. |
---|
| 2984 | /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
---|
| 2985 | /// \c GR::Edge). |
---|
| 2986 | /// \tparam V The value type of the map. It can be any comparable |
---|
| 2987 | /// value type. |
---|
[740] | 2988 | /// |
---|
[741] | 2989 | /// \see IterableBoolMap, IterableIntMap |
---|
| 2990 | /// \see CrossRefMap |
---|
| 2991 | template <typename GR, typename K, typename V> |
---|
[740] | 2992 | class IterableValueMap |
---|
[741] | 2993 | : protected ItemSetTraits<GR, K>:: |
---|
| 2994 | template Map<_maps_bits::IterableValueMapNode<K, V> >::Type { |
---|
[740] | 2995 | public: |
---|
[741] | 2996 | typedef typename ItemSetTraits<GR, K>:: |
---|
| 2997 | template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent; |
---|
[740] | 2998 | |
---|
| 2999 | /// The key type |
---|
[741] | 3000 | typedef K Key; |
---|
[740] | 3001 | /// The value type |
---|
[741] | 3002 | typedef V Value; |
---|
[740] | 3003 | /// The graph type |
---|
| 3004 | typedef GR Graph; |
---|
| 3005 | |
---|
| 3006 | public: |
---|
| 3007 | |
---|
[741] | 3008 | /// \brief Constructor of the map with a given value. |
---|
[740] | 3009 | /// |
---|
[741] | 3010 | /// Constructor of the map with a given value. |
---|
[740] | 3011 | explicit IterableValueMap(const Graph& graph, |
---|
| 3012 | const Value& value = Value()) |
---|
[741] | 3013 | : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) { |
---|
[740] | 3014 | for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
---|
| 3015 | lace(it); |
---|
| 3016 | } |
---|
| 3017 | } |
---|
| 3018 | |
---|
| 3019 | protected: |
---|
| 3020 | |
---|
| 3021 | void unlace(const Key& key) { |
---|
| 3022 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 3023 | if (node.prev != INVALID) { |
---|
| 3024 | Parent::operator[](node.prev).next = node.next; |
---|
| 3025 | } else { |
---|
| 3026 | if (node.next != INVALID) { |
---|
| 3027 | _first[node.value] = node.next; |
---|
| 3028 | } else { |
---|
| 3029 | _first.erase(node.value); |
---|
| 3030 | } |
---|
| 3031 | } |
---|
| 3032 | if (node.next != INVALID) { |
---|
| 3033 | Parent::operator[](node.next).prev = node.prev; |
---|
| 3034 | } |
---|
| 3035 | } |
---|
| 3036 | |
---|
| 3037 | void lace(const Key& key) { |
---|
| 3038 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 3039 | typename std::map<Value, Key>::iterator it = _first.find(node.value); |
---|
| 3040 | if (it == _first.end()) { |
---|
| 3041 | node.prev = node.next = INVALID; |
---|
| 3042 | _first.insert(std::make_pair(node.value, key)); |
---|
| 3043 | } else { |
---|
| 3044 | node.prev = INVALID; |
---|
| 3045 | node.next = it->second; |
---|
| 3046 | if (node.next != INVALID) { |
---|
| 3047 | Parent::operator[](node.next).prev = key; |
---|
| 3048 | } |
---|
| 3049 | it->second = key; |
---|
| 3050 | } |
---|
| 3051 | } |
---|
| 3052 | |
---|
| 3053 | public: |
---|
| 3054 | |
---|
| 3055 | /// \brief Forward iterator for values. |
---|
| 3056 | /// |
---|
| 3057 | /// This iterator is an stl compatible forward |
---|
| 3058 | /// iterator on the values of the map. The values can |
---|
[741] | 3059 | /// be accessed in the <tt>[beginValue, endValue)</tt> range. |
---|
[740] | 3060 | class ValueIterator |
---|
| 3061 | : public std::iterator<std::forward_iterator_tag, Value> { |
---|
| 3062 | friend class IterableValueMap; |
---|
| 3063 | private: |
---|
| 3064 | ValueIterator(typename std::map<Value, Key>::const_iterator _it) |
---|
| 3065 | : it(_it) {} |
---|
| 3066 | public: |
---|
| 3067 | |
---|
| 3068 | ValueIterator() {} |
---|
| 3069 | |
---|
| 3070 | ValueIterator& operator++() { ++it; return *this; } |
---|
| 3071 | ValueIterator operator++(int) { |
---|
| 3072 | ValueIterator tmp(*this); |
---|
| 3073 | operator++(); |
---|
| 3074 | return tmp; |
---|
| 3075 | } |
---|
| 3076 | |
---|
| 3077 | const Value& operator*() const { return it->first; } |
---|
| 3078 | const Value* operator->() const { return &(it->first); } |
---|
| 3079 | |
---|
| 3080 | bool operator==(ValueIterator jt) const { return it == jt.it; } |
---|
| 3081 | bool operator!=(ValueIterator jt) const { return it != jt.it; } |
---|
| 3082 | |
---|
| 3083 | private: |
---|
| 3084 | typename std::map<Value, Key>::const_iterator it; |
---|
| 3085 | }; |
---|
| 3086 | |
---|
| 3087 | /// \brief Returns an iterator to the first value. |
---|
| 3088 | /// |
---|
| 3089 | /// Returns an stl compatible iterator to the |
---|
| 3090 | /// first value of the map. The values of the |
---|
[741] | 3091 | /// map can be accessed in the <tt>[beginValue, endValue)</tt> |
---|
[740] | 3092 | /// range. |
---|
| 3093 | ValueIterator beginValue() const { |
---|
| 3094 | return ValueIterator(_first.begin()); |
---|
| 3095 | } |
---|
| 3096 | |
---|
| 3097 | /// \brief Returns an iterator after the last value. |
---|
| 3098 | /// |
---|
| 3099 | /// Returns an stl compatible iterator after the |
---|
| 3100 | /// last value of the map. The values of the |
---|
[741] | 3101 | /// map can be accessed in the <tt>[beginValue, endValue)</tt> |
---|
[740] | 3102 | /// range. |
---|
| 3103 | ValueIterator endValue() const { |
---|
| 3104 | return ValueIterator(_first.end()); |
---|
| 3105 | } |
---|
| 3106 | |
---|
| 3107 | /// \brief Set operation of the map. |
---|
| 3108 | /// |
---|
| 3109 | /// Set operation of the map. |
---|
| 3110 | void set(const Key& key, const Value& value) { |
---|
| 3111 | unlace(key); |
---|
| 3112 | Parent::operator[](key).value = value; |
---|
| 3113 | lace(key); |
---|
| 3114 | } |
---|
| 3115 | |
---|
| 3116 | /// \brief Const subscript operator of the map. |
---|
| 3117 | /// |
---|
| 3118 | /// Const subscript operator of the map. |
---|
| 3119 | const Value& operator[](const Key& key) const { |
---|
| 3120 | return Parent::operator[](key).value; |
---|
| 3121 | } |
---|
| 3122 | |
---|
| 3123 | /// \brief Iterator for the keys with the same value. |
---|
| 3124 | /// |
---|
| 3125 | /// Iterator for the keys with the same value. It works |
---|
[741] | 3126 | /// like a graph item iterator, it can be converted to |
---|
[740] | 3127 | /// the item type of the map, incremented with \c ++ operator, and |
---|
[741] | 3128 | /// if the iterator leaves the last valid item, it will be equal to |
---|
[740] | 3129 | /// \c INVALID. |
---|
[741] | 3130 | class ItemIt : public Key { |
---|
[740] | 3131 | public: |
---|
[741] | 3132 | typedef Key Parent; |
---|
[740] | 3133 | |
---|
| 3134 | /// \brief Invalid constructor \& conversion. |
---|
| 3135 | /// |
---|
[741] | 3136 | /// This constructor initializes the iterator to be invalid. |
---|
[740] | 3137 | /// \sa Invalid for more details. |
---|
| 3138 | ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 3139 | |
---|
| 3140 | /// \brief Creates an iterator with a value. |
---|
| 3141 | /// |
---|
| 3142 | /// Creates an iterator with a value. It iterates on the |
---|
| 3143 | /// keys which have the given value. |
---|
| 3144 | /// \param map The IterableValueMap |
---|
| 3145 | /// \param value The value |
---|
| 3146 | ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) { |
---|
| 3147 | typename std::map<Value, Key>::const_iterator it = |
---|
| 3148 | map._first.find(value); |
---|
| 3149 | if (it == map._first.end()) { |
---|
| 3150 | Parent::operator=(INVALID); |
---|
| 3151 | } else { |
---|
| 3152 | Parent::operator=(it->second); |
---|
| 3153 | } |
---|
| 3154 | } |
---|
| 3155 | |
---|
| 3156 | /// \brief Increment operator. |
---|
| 3157 | /// |
---|
| 3158 | /// Increment Operator. |
---|
| 3159 | ItemIt& operator++() { |
---|
| 3160 | Parent::operator=(_map->IterableValueMap::Parent:: |
---|
| 3161 | operator[](static_cast<Parent&>(*this)).next); |
---|
| 3162 | return *this; |
---|
| 3163 | } |
---|
| 3164 | |
---|
| 3165 | |
---|
| 3166 | private: |
---|
| 3167 | const IterableValueMap* _map; |
---|
| 3168 | }; |
---|
| 3169 | |
---|
| 3170 | protected: |
---|
| 3171 | |
---|
| 3172 | virtual void add(const Key& key) { |
---|
| 3173 | Parent::add(key); |
---|
| 3174 | unlace(key); |
---|
| 3175 | } |
---|
| 3176 | |
---|
| 3177 | virtual void add(const std::vector<Key>& keys) { |
---|
| 3178 | Parent::add(keys); |
---|
| 3179 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 3180 | lace(keys[i]); |
---|
| 3181 | } |
---|
| 3182 | } |
---|
| 3183 | |
---|
| 3184 | virtual void erase(const Key& key) { |
---|
| 3185 | unlace(key); |
---|
| 3186 | Parent::erase(key); |
---|
| 3187 | } |
---|
| 3188 | |
---|
| 3189 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 3190 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 3191 | unlace(keys[i]); |
---|
| 3192 | } |
---|
| 3193 | Parent::erase(keys); |
---|
| 3194 | } |
---|
| 3195 | |
---|
| 3196 | virtual void build() { |
---|
| 3197 | Parent::build(); |
---|
| 3198 | for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
---|
| 3199 | lace(it); |
---|
| 3200 | } |
---|
| 3201 | } |
---|
| 3202 | |
---|
| 3203 | virtual void clear() { |
---|
| 3204 | _first.clear(); |
---|
| 3205 | Parent::clear(); |
---|
| 3206 | } |
---|
| 3207 | |
---|
| 3208 | private: |
---|
| 3209 | std::map<Value, Key> _first; |
---|
| 3210 | }; |
---|
| 3211 | |
---|
[606] | 3212 | /// \brief Map of the source nodes of arcs in a digraph. |
---|
[220] | 3213 | /// |
---|
[606] | 3214 | /// SourceMap provides access for the source node of each arc in a digraph, |
---|
| 3215 | /// which is returned by the \c source() function of the digraph. |
---|
| 3216 | /// \tparam GR The digraph type. |
---|
[220] | 3217 | /// \see TargetMap |
---|
[606] | 3218 | template <typename GR> |
---|
[220] | 3219 | class SourceMap { |
---|
| 3220 | public: |
---|
| 3221 | |
---|
[606] | 3222 | ///\e |
---|
| 3223 | typedef typename GR::Arc Key; |
---|
| 3224 | ///\e |
---|
| 3225 | typedef typename GR::Node Value; |
---|
[220] | 3226 | |
---|
| 3227 | /// \brief Constructor |
---|
| 3228 | /// |
---|
[606] | 3229 | /// Constructor. |
---|
[313] | 3230 | /// \param digraph The digraph that the map belongs to. |
---|
[606] | 3231 | explicit SourceMap(const GR& digraph) : _graph(digraph) {} |
---|
| 3232 | |
---|
| 3233 | /// \brief Returns the source node of the given arc. |
---|
[220] | 3234 | /// |
---|
[606] | 3235 | /// Returns the source node of the given arc. |
---|
[220] | 3236 | Value operator[](const Key& arc) const { |
---|
[606] | 3237 | return _graph.source(arc); |
---|
[220] | 3238 | } |
---|
| 3239 | |
---|
| 3240 | private: |
---|
[606] | 3241 | const GR& _graph; |
---|
[220] | 3242 | }; |
---|
| 3243 | |
---|
[301] | 3244 | /// \brief Returns a \c SourceMap class. |
---|
[220] | 3245 | /// |
---|
[301] | 3246 | /// This function just returns an \c SourceMap class. |
---|
[220] | 3247 | /// \relates SourceMap |
---|
[606] | 3248 | template <typename GR> |
---|
| 3249 | inline SourceMap<GR> sourceMap(const GR& graph) { |
---|
| 3250 | return SourceMap<GR>(graph); |
---|
[220] | 3251 | } |
---|
| 3252 | |
---|
[606] | 3253 | /// \brief Map of the target nodes of arcs in a digraph. |
---|
[220] | 3254 | /// |
---|
[606] | 3255 | /// TargetMap provides access for the target node of each arc in a digraph, |
---|
| 3256 | /// which is returned by the \c target() function of the digraph. |
---|
| 3257 | /// \tparam GR The digraph type. |
---|
[220] | 3258 | /// \see SourceMap |
---|
[606] | 3259 | template <typename GR> |
---|
[220] | 3260 | class TargetMap { |
---|
| 3261 | public: |
---|
| 3262 | |
---|
[606] | 3263 | ///\e |
---|
| 3264 | typedef typename GR::Arc Key; |
---|
| 3265 | ///\e |
---|
| 3266 | typedef typename GR::Node Value; |
---|
[220] | 3267 | |
---|
| 3268 | /// \brief Constructor |
---|
| 3269 | /// |
---|
[606] | 3270 | /// Constructor. |
---|
[313] | 3271 | /// \param digraph The digraph that the map belongs to. |
---|
[606] | 3272 | explicit TargetMap(const GR& digraph) : _graph(digraph) {} |
---|
| 3273 | |
---|
| 3274 | /// \brief Returns the target node of the given arc. |
---|
[220] | 3275 | /// |
---|
[606] | 3276 | /// Returns the target node of the given arc. |
---|
[220] | 3277 | Value operator[](const Key& e) const { |
---|
[606] | 3278 | return _graph.target(e); |
---|
[220] | 3279 | } |
---|
| 3280 | |
---|
| 3281 | private: |
---|
[606] | 3282 | const GR& _graph; |
---|
[220] | 3283 | }; |
---|
| 3284 | |
---|
[301] | 3285 | /// \brief Returns a \c TargetMap class. |
---|
[220] | 3286 | /// |
---|
[301] | 3287 | /// This function just returns a \c TargetMap class. |
---|
[220] | 3288 | /// \relates TargetMap |
---|
[606] | 3289 | template <typename GR> |
---|
| 3290 | inline TargetMap<GR> targetMap(const GR& graph) { |
---|
| 3291 | return TargetMap<GR>(graph); |
---|
[220] | 3292 | } |
---|
| 3293 | |
---|
[606] | 3294 | /// \brief Map of the "forward" directed arc view of edges in a graph. |
---|
[220] | 3295 | /// |
---|
[606] | 3296 | /// ForwardMap provides access for the "forward" directed arc view of |
---|
| 3297 | /// each edge in a graph, which is returned by the \c direct() function |
---|
| 3298 | /// of the graph with \c true parameter. |
---|
| 3299 | /// \tparam GR The graph type. |
---|
[220] | 3300 | /// \see BackwardMap |
---|
[606] | 3301 | template <typename GR> |
---|
[220] | 3302 | class ForwardMap { |
---|
| 3303 | public: |
---|
| 3304 | |
---|
[606] | 3305 | typedef typename GR::Arc Value; |
---|
| 3306 | typedef typename GR::Edge Key; |
---|
[220] | 3307 | |
---|
| 3308 | /// \brief Constructor |
---|
| 3309 | /// |
---|
[606] | 3310 | /// Constructor. |
---|
[313] | 3311 | /// \param graph The graph that the map belongs to. |
---|
[606] | 3312 | explicit ForwardMap(const GR& graph) : _graph(graph) {} |
---|
| 3313 | |
---|
| 3314 | /// \brief Returns the "forward" directed arc view of the given edge. |
---|
[220] | 3315 | /// |
---|
[606] | 3316 | /// Returns the "forward" directed arc view of the given edge. |
---|
[220] | 3317 | Value operator[](const Key& key) const { |
---|
| 3318 | return _graph.direct(key, true); |
---|
| 3319 | } |
---|
| 3320 | |
---|
| 3321 | private: |
---|
[606] | 3322 | const GR& _graph; |
---|
[220] | 3323 | }; |
---|
| 3324 | |
---|
[301] | 3325 | /// \brief Returns a \c ForwardMap class. |
---|
[220] | 3326 | /// |
---|
[301] | 3327 | /// This function just returns an \c ForwardMap class. |
---|
[220] | 3328 | /// \relates ForwardMap |
---|
[606] | 3329 | template <typename GR> |
---|
| 3330 | inline ForwardMap<GR> forwardMap(const GR& graph) { |
---|
| 3331 | return ForwardMap<GR>(graph); |
---|
[220] | 3332 | } |
---|
| 3333 | |
---|
[606] | 3334 | /// \brief Map of the "backward" directed arc view of edges in a graph. |
---|
[220] | 3335 | /// |
---|
[606] | 3336 | /// BackwardMap provides access for the "backward" directed arc view of |
---|
| 3337 | /// each edge in a graph, which is returned by the \c direct() function |
---|
| 3338 | /// of the graph with \c false parameter. |
---|
| 3339 | /// \tparam GR The graph type. |
---|
[220] | 3340 | /// \see ForwardMap |
---|
[606] | 3341 | template <typename GR> |
---|
[220] | 3342 | class BackwardMap { |
---|
| 3343 | public: |
---|
| 3344 | |
---|
[606] | 3345 | typedef typename GR::Arc Value; |
---|
| 3346 | typedef typename GR::Edge Key; |
---|
[220] | 3347 | |
---|
| 3348 | /// \brief Constructor |
---|
| 3349 | /// |
---|
[606] | 3350 | /// Constructor. |
---|
[313] | 3351 | /// \param graph The graph that the map belongs to. |
---|
[606] | 3352 | explicit BackwardMap(const GR& graph) : _graph(graph) {} |
---|
| 3353 | |
---|
| 3354 | /// \brief Returns the "backward" directed arc view of the given edge. |
---|
[220] | 3355 | /// |
---|
[606] | 3356 | /// Returns the "backward" directed arc view of the given edge. |
---|
[220] | 3357 | Value operator[](const Key& key) const { |
---|
| 3358 | return _graph.direct(key, false); |
---|
| 3359 | } |
---|
| 3360 | |
---|
| 3361 | private: |
---|
[606] | 3362 | const GR& _graph; |
---|
[220] | 3363 | }; |
---|
| 3364 | |
---|
[301] | 3365 | /// \brief Returns a \c BackwardMap class |
---|
| 3366 | |
---|
| 3367 | /// This function just returns a \c BackwardMap class. |
---|
[220] | 3368 | /// \relates BackwardMap |
---|
[606] | 3369 | template <typename GR> |
---|
| 3370 | inline BackwardMap<GR> backwardMap(const GR& graph) { |
---|
| 3371 | return BackwardMap<GR>(graph); |
---|
[220] | 3372 | } |
---|
| 3373 | |
---|
[606] | 3374 | /// \brief Map of the in-degrees of nodes in a digraph. |
---|
[220] | 3375 | /// |
---|
| 3376 | /// This map returns the in-degree of a node. Once it is constructed, |
---|
[606] | 3377 | /// the degrees are stored in a standard \c NodeMap, so each query is done |
---|
[220] | 3378 | /// in constant time. On the other hand, the values are updated automatically |
---|
| 3379 | /// whenever the digraph changes. |
---|
| 3380 | /// |
---|
[740] | 3381 | /// \warning Besides \c addNode() and \c addArc(), a digraph structure |
---|
[606] | 3382 | /// may provide alternative ways to modify the digraph. |
---|
| 3383 | /// The correct behavior of InDegMap is not guarantied if these additional |
---|
| 3384 | /// features are used. For example the functions |
---|
| 3385 | /// \ref ListDigraph::changeSource() "changeSource()", |
---|
[220] | 3386 | /// \ref ListDigraph::changeTarget() "changeTarget()" and |
---|
| 3387 | /// \ref ListDigraph::reverseArc() "reverseArc()" |
---|
| 3388 | /// of \ref ListDigraph will \e not update the degree values correctly. |
---|
| 3389 | /// |
---|
| 3390 | /// \sa OutDegMap |
---|
[606] | 3391 | template <typename GR> |
---|
[220] | 3392 | class InDegMap |
---|
[606] | 3393 | : protected ItemSetTraits<GR, typename GR::Arc> |
---|
[220] | 3394 | ::ItemNotifier::ObserverBase { |
---|
| 3395 | |
---|
| 3396 | public: |
---|
[740] | 3397 | |
---|
[664] | 3398 | /// The graph type of InDegMap |
---|
| 3399 | typedef GR Graph; |
---|
[606] | 3400 | typedef GR Digraph; |
---|
| 3401 | /// The key type |
---|
| 3402 | typedef typename Digraph::Node Key; |
---|
| 3403 | /// The value type |
---|
[220] | 3404 | typedef int Value; |
---|
| 3405 | |
---|
| 3406 | typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
---|
| 3407 | ::ItemNotifier::ObserverBase Parent; |
---|
| 3408 | |
---|
| 3409 | private: |
---|
| 3410 | |
---|
| 3411 | class AutoNodeMap |
---|
| 3412 | : public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
---|
| 3413 | public: |
---|
| 3414 | |
---|
| 3415 | typedef typename ItemSetTraits<Digraph, Key>:: |
---|
| 3416 | template Map<int>::Type Parent; |
---|
| 3417 | |
---|
| 3418 | AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
---|
| 3419 | |
---|
| 3420 | virtual void add(const Key& key) { |
---|
| 3421 | Parent::add(key); |
---|
| 3422 | Parent::set(key, 0); |
---|
| 3423 | } |
---|
| 3424 | |
---|
| 3425 | virtual void add(const std::vector<Key>& keys) { |
---|
| 3426 | Parent::add(keys); |
---|
| 3427 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 3428 | Parent::set(keys[i], 0); |
---|
| 3429 | } |
---|
| 3430 | } |
---|
| 3431 | |
---|
| 3432 | virtual void build() { |
---|
| 3433 | Parent::build(); |
---|
| 3434 | Key it; |
---|
| 3435 | typename Parent::Notifier* nf = Parent::notifier(); |
---|
| 3436 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 3437 | Parent::set(it, 0); |
---|
| 3438 | } |
---|
| 3439 | } |
---|
| 3440 | }; |
---|
| 3441 | |
---|
| 3442 | public: |
---|
| 3443 | |
---|
| 3444 | /// \brief Constructor. |
---|
| 3445 | /// |
---|
[606] | 3446 | /// Constructor for creating an in-degree map. |
---|
| 3447 | explicit InDegMap(const Digraph& graph) |
---|
| 3448 | : _digraph(graph), _deg(graph) { |
---|
[220] | 3449 | Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
---|
| 3450 | |
---|
| 3451 | for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
---|
| 3452 | _deg[it] = countInArcs(_digraph, it); |
---|
| 3453 | } |
---|
| 3454 | } |
---|
| 3455 | |
---|
[606] | 3456 | /// \brief Gives back the in-degree of a Node. |
---|
| 3457 | /// |
---|
[220] | 3458 | /// Gives back the in-degree of a Node. |
---|
| 3459 | int operator[](const Key& key) const { |
---|
| 3460 | return _deg[key]; |
---|
| 3461 | } |
---|
| 3462 | |
---|
| 3463 | protected: |
---|
| 3464 | |
---|
| 3465 | typedef typename Digraph::Arc Arc; |
---|
| 3466 | |
---|
| 3467 | virtual void add(const Arc& arc) { |
---|
| 3468 | ++_deg[_digraph.target(arc)]; |
---|
| 3469 | } |
---|
| 3470 | |
---|
| 3471 | virtual void add(const std::vector<Arc>& arcs) { |
---|
| 3472 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 3473 | ++_deg[_digraph.target(arcs[i])]; |
---|
| 3474 | } |
---|
| 3475 | } |
---|
| 3476 | |
---|
| 3477 | virtual void erase(const Arc& arc) { |
---|
| 3478 | --_deg[_digraph.target(arc)]; |
---|
| 3479 | } |
---|
| 3480 | |
---|
| 3481 | virtual void erase(const std::vector<Arc>& arcs) { |
---|
| 3482 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 3483 | --_deg[_digraph.target(arcs[i])]; |
---|
| 3484 | } |
---|
| 3485 | } |
---|
| 3486 | |
---|
| 3487 | virtual void build() { |
---|
| 3488 | for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
---|
| 3489 | _deg[it] = countInArcs(_digraph, it); |
---|
| 3490 | } |
---|
| 3491 | } |
---|
| 3492 | |
---|
| 3493 | virtual void clear() { |
---|
| 3494 | for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
---|
| 3495 | _deg[it] = 0; |
---|
| 3496 | } |
---|
| 3497 | } |
---|
| 3498 | private: |
---|
| 3499 | |
---|
| 3500 | const Digraph& _digraph; |
---|
| 3501 | AutoNodeMap _deg; |
---|
| 3502 | }; |
---|
| 3503 | |
---|
[606] | 3504 | /// \brief Map of the out-degrees of nodes in a digraph. |
---|
[220] | 3505 | /// |
---|
| 3506 | /// This map returns the out-degree of a node. Once it is constructed, |
---|
[606] | 3507 | /// the degrees are stored in a standard \c NodeMap, so each query is done |
---|
[220] | 3508 | /// in constant time. On the other hand, the values are updated automatically |
---|
| 3509 | /// whenever the digraph changes. |
---|
| 3510 | /// |
---|
[740] | 3511 | /// \warning Besides \c addNode() and \c addArc(), a digraph structure |
---|
[606] | 3512 | /// may provide alternative ways to modify the digraph. |
---|
| 3513 | /// The correct behavior of OutDegMap is not guarantied if these additional |
---|
| 3514 | /// features are used. For example the functions |
---|
| 3515 | /// \ref ListDigraph::changeSource() "changeSource()", |
---|
[220] | 3516 | /// \ref ListDigraph::changeTarget() "changeTarget()" and |
---|
| 3517 | /// \ref ListDigraph::reverseArc() "reverseArc()" |
---|
| 3518 | /// of \ref ListDigraph will \e not update the degree values correctly. |
---|
| 3519 | /// |
---|
| 3520 | /// \sa InDegMap |
---|
[606] | 3521 | template <typename GR> |
---|
[220] | 3522 | class OutDegMap |
---|
[606] | 3523 | : protected ItemSetTraits<GR, typename GR::Arc> |
---|
[220] | 3524 | ::ItemNotifier::ObserverBase { |
---|
| 3525 | |
---|
| 3526 | public: |
---|
| 3527 | |
---|
[664] | 3528 | /// The graph type of OutDegMap |
---|
| 3529 | typedef GR Graph; |
---|
[606] | 3530 | typedef GR Digraph; |
---|
| 3531 | /// The key type |
---|
| 3532 | typedef typename Digraph::Node Key; |
---|
| 3533 | /// The value type |
---|
[220] | 3534 | typedef int Value; |
---|
| 3535 | |
---|
| 3536 | typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
---|
| 3537 | ::ItemNotifier::ObserverBase Parent; |
---|
| 3538 | |
---|
| 3539 | private: |
---|
| 3540 | |
---|
| 3541 | class AutoNodeMap |
---|
| 3542 | : public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
---|
| 3543 | public: |
---|
| 3544 | |
---|
| 3545 | typedef typename ItemSetTraits<Digraph, Key>:: |
---|
| 3546 | template Map<int>::Type Parent; |
---|
| 3547 | |
---|
| 3548 | AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
---|
| 3549 | |
---|
| 3550 | virtual void add(const Key& key) { |
---|
| 3551 | Parent::add(key); |
---|
| 3552 | Parent::set(key, 0); |
---|
| 3553 | } |
---|
| 3554 | virtual void add(const std::vector<Key>& keys) { |
---|
| 3555 | Parent::add(keys); |
---|
| 3556 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
| 3557 | Parent::set(keys[i], 0); |
---|
| 3558 | } |
---|
| 3559 | } |
---|
| 3560 | virtual void build() { |
---|
| 3561 | Parent::build(); |
---|
| 3562 | Key it; |
---|
| 3563 | typename Parent::Notifier* nf = Parent::notifier(); |
---|
| 3564 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 3565 | Parent::set(it, 0); |
---|
| 3566 | } |
---|
| 3567 | } |
---|
| 3568 | }; |
---|
| 3569 | |
---|
| 3570 | public: |
---|
| 3571 | |
---|
| 3572 | /// \brief Constructor. |
---|
| 3573 | /// |
---|
[606] | 3574 | /// Constructor for creating an out-degree map. |
---|
| 3575 | explicit OutDegMap(const Digraph& graph) |
---|
| 3576 | : _digraph(graph), _deg(graph) { |
---|
[220] | 3577 | Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
---|
| 3578 | |
---|
| 3579 | for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
---|
| 3580 | _deg[it] = countOutArcs(_digraph, it); |
---|
| 3581 | } |
---|
| 3582 | } |
---|
| 3583 | |
---|
[606] | 3584 | /// \brief Gives back the out-degree of a Node. |
---|
| 3585 | /// |
---|
[220] | 3586 | /// Gives back the out-degree of a Node. |
---|
| 3587 | int operator[](const Key& key) const { |
---|
| 3588 | return _deg[key]; |
---|
| 3589 | } |
---|
| 3590 | |
---|
| 3591 | protected: |
---|
| 3592 | |
---|
| 3593 | typedef typename Digraph::Arc Arc; |
---|
| 3594 | |
---|
| 3595 | virtual void add(const Arc& arc) { |
---|
| 3596 | ++_deg[_digraph.source(arc)]; |
---|
| 3597 | } |
---|
| 3598 | |
---|
| 3599 | virtual void add(const std::vector<Arc>& arcs) { |
---|
| 3600 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 3601 | ++_deg[_digraph.source(arcs[i])]; |
---|
| 3602 | } |
---|
| 3603 | } |
---|
| 3604 | |
---|
| 3605 | virtual void erase(const Arc& arc) { |
---|
| 3606 | --_deg[_digraph.source(arc)]; |
---|
| 3607 | } |
---|
| 3608 | |
---|
| 3609 | virtual void erase(const std::vector<Arc>& arcs) { |
---|
| 3610 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 3611 | --_deg[_digraph.source(arcs[i])]; |
---|
| 3612 | } |
---|
| 3613 | } |
---|
| 3614 | |
---|
| 3615 | virtual void build() { |
---|
| 3616 | for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
---|
| 3617 | _deg[it] = countOutArcs(_digraph, it); |
---|
| 3618 | } |
---|
| 3619 | } |
---|
| 3620 | |
---|
| 3621 | virtual void clear() { |
---|
| 3622 | for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
---|
| 3623 | _deg[it] = 0; |
---|
| 3624 | } |
---|
| 3625 | } |
---|
| 3626 | private: |
---|
| 3627 | |
---|
| 3628 | const Digraph& _digraph; |
---|
| 3629 | AutoNodeMap _deg; |
---|
| 3630 | }; |
---|
| 3631 | |
---|
[606] | 3632 | /// \brief Potential difference map |
---|
| 3633 | /// |
---|
[631] | 3634 | /// PotentialDifferenceMap returns the difference between the potentials of |
---|
| 3635 | /// the source and target nodes of each arc in a digraph, i.e. it returns |
---|
[606] | 3636 | /// \code |
---|
| 3637 | /// potential[gr.target(arc)] - potential[gr.source(arc)]. |
---|
| 3638 | /// \endcode |
---|
| 3639 | /// \tparam GR The digraph type. |
---|
| 3640 | /// \tparam POT A node map storing the potentials. |
---|
| 3641 | template <typename GR, typename POT> |
---|
| 3642 | class PotentialDifferenceMap { |
---|
| 3643 | public: |
---|
| 3644 | /// Key type |
---|
| 3645 | typedef typename GR::Arc Key; |
---|
| 3646 | /// Value type |
---|
| 3647 | typedef typename POT::Value Value; |
---|
| 3648 | |
---|
| 3649 | /// \brief Constructor |
---|
| 3650 | /// |
---|
| 3651 | /// Contructor of the map. |
---|
| 3652 | explicit PotentialDifferenceMap(const GR& gr, |
---|
| 3653 | const POT& potential) |
---|
| 3654 | : _digraph(gr), _potential(potential) {} |
---|
| 3655 | |
---|
| 3656 | /// \brief Returns the potential difference for the given arc. |
---|
| 3657 | /// |
---|
| 3658 | /// Returns the potential difference for the given arc, i.e. |
---|
| 3659 | /// \code |
---|
| 3660 | /// potential[gr.target(arc)] - potential[gr.source(arc)]. |
---|
| 3661 | /// \endcode |
---|
| 3662 | Value operator[](const Key& arc) const { |
---|
| 3663 | return _potential[_digraph.target(arc)] - |
---|
| 3664 | _potential[_digraph.source(arc)]; |
---|
| 3665 | } |
---|
| 3666 | |
---|
| 3667 | private: |
---|
| 3668 | const GR& _digraph; |
---|
| 3669 | const POT& _potential; |
---|
| 3670 | }; |
---|
| 3671 | |
---|
| 3672 | /// \brief Returns a PotentialDifferenceMap. |
---|
| 3673 | /// |
---|
| 3674 | /// This function just returns a PotentialDifferenceMap. |
---|
| 3675 | /// \relates PotentialDifferenceMap |
---|
| 3676 | template <typename GR, typename POT> |
---|
| 3677 | PotentialDifferenceMap<GR, POT> |
---|
| 3678 | potentialDifferenceMap(const GR& gr, const POT& potential) { |
---|
| 3679 | return PotentialDifferenceMap<GR, POT>(gr, potential); |
---|
| 3680 | } |
---|
| 3681 | |
---|
[25] | 3682 | /// @} |
---|
| 3683 | } |
---|
| 3684 | |
---|
| 3685 | #endif // LEMON_MAPS_H |
---|