1.1 --- a/lemon/maps.h Fri Oct 16 10:21:37 2009 +0200
1.2 +++ b/lemon/maps.h Thu Nov 05 15:50:01 2009 +0100
1.3 @@ -2,7 +2,7 @@
1.4 *
1.5 * This file is a part of LEMON, a generic C++ optimization library.
1.6 *
1.7 - * Copyright (C) 2003-2008
1.8 + * Copyright (C) 2003-2009
1.9 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 *
1.12 @@ -22,6 +22,7 @@
1.13 #include <iterator>
1.14 #include <functional>
1.15 #include <vector>
1.16 +#include <map>
1.17
1.18 #include <lemon/core.h>
1.19
1.20 @@ -29,8 +30,6 @@
1.21 ///\ingroup maps
1.22 ///\brief Miscellaneous property maps
1.23
1.24 -#include <map>
1.25 -
1.26 namespace lemon {
1.27
1.28 /// \addtogroup maps
1.29 @@ -57,15 +56,16 @@
1.30 /// its type definitions, or if you have to provide a writable map,
1.31 /// but data written to it is not required (i.e. it will be sent to
1.32 /// <tt>/dev/null</tt>).
1.33 - /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1.34 + /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1.35 ///
1.36 /// \sa ConstMap
1.37 template<typename K, typename V>
1.38 class NullMap : public MapBase<K, V> {
1.39 public:
1.40 - typedef MapBase<K, V> Parent;
1.41 - typedef typename Parent::Key Key;
1.42 - typedef typename Parent::Value Value;
1.43 + ///\e
1.44 + typedef K Key;
1.45 + ///\e
1.46 + typedef V Value;
1.47
1.48 /// Gives back a default constructed element.
1.49 Value operator[](const Key&) const { return Value(); }
1.50 @@ -89,7 +89,7 @@
1.51 /// value to each key.
1.52 ///
1.53 /// In other aspects it is equivalent to \c NullMap.
1.54 - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
1.55 + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
1.56 /// concept, but it absorbs the data written to it.
1.57 ///
1.58 /// The simplest way of using this map is through the constMap()
1.59 @@ -102,9 +102,10 @@
1.60 private:
1.61 V _value;
1.62 public:
1.63 - typedef MapBase<K, V> Parent;
1.64 - typedef typename Parent::Key Key;
1.65 - typedef typename Parent::Value Value;
1.66 + ///\e
1.67 + typedef K Key;
1.68 + ///\e
1.69 + typedef V Value;
1.70
1.71 /// Default constructor
1.72
1.73 @@ -157,7 +158,7 @@
1.74 /// value to each key.
1.75 ///
1.76 /// In other aspects it is equivalent to \c NullMap.
1.77 - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
1.78 + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
1.79 /// concept, but it absorbs the data written to it.
1.80 ///
1.81 /// The simplest way of using this map is through the constMap()
1.82 @@ -168,9 +169,10 @@
1.83 template<typename K, typename V, V v>
1.84 class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
1.85 public:
1.86 - typedef MapBase<K, V> Parent;
1.87 - typedef typename Parent::Key Key;
1.88 - typedef typename Parent::Value Value;
1.89 + ///\e
1.90 + typedef K Key;
1.91 + ///\e
1.92 + typedef V Value;
1.93
1.94 /// Constructor.
1.95 ConstMap() {}
1.96 @@ -202,9 +204,10 @@
1.97 template <typename T>
1.98 class IdentityMap : public MapBase<T, T> {
1.99 public:
1.100 - typedef MapBase<T, T> Parent;
1.101 - typedef typename Parent::Key Key;
1.102 - typedef typename Parent::Value Value;
1.103 + ///\e
1.104 + typedef T Key;
1.105 + ///\e
1.106 + typedef T Value;
1.107
1.108 /// Gives back the given value without any modification.
1.109 Value operator[](const Key &k) const {
1.110 @@ -229,7 +232,7 @@
1.111 /// values to integer keys from the range <tt>[0..size-1]</tt>.
1.112 /// It can be used with some data structures, for example
1.113 /// \c UnionFind, \c BinHeap, when the used items are small
1.114 - /// integers. This map conforms the \ref concepts::ReferenceMap
1.115 + /// integers. This map conforms to the \ref concepts::ReferenceMap
1.116 /// "ReferenceMap" concept.
1.117 ///
1.118 /// The simplest way of using this map is through the rangeMap()
1.119 @@ -245,11 +248,10 @@
1.120
1.121 public:
1.122
1.123 - typedef MapBase<int, V> Parent;
1.124 /// Key type
1.125 - typedef typename Parent::Key Key;
1.126 + typedef int Key;
1.127 /// Value type
1.128 - typedef typename Parent::Value Value;
1.129 + typedef V Value;
1.130 /// Reference type
1.131 typedef typename Vector::reference Reference;
1.132 /// Const reference type
1.133 @@ -338,7 +340,7 @@
1.134 /// that you can specify a default value for the keys that are not
1.135 /// stored actually. This value can be different from the default
1.136 /// contructed value (i.e. \c %Value()).
1.137 - /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
1.138 + /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
1.139 /// concept.
1.140 ///
1.141 /// This map is useful if a default value should be assigned to most of
1.142 @@ -353,17 +355,16 @@
1.143 ///
1.144 /// The simplest way of using this map is through the sparseMap()
1.145 /// function.
1.146 - template <typename K, typename V, typename Compare = std::less<K> >
1.147 + template <typename K, typename V, typename Comp = std::less<K> >
1.148 class SparseMap : public MapBase<K, V> {
1.149 template <typename K1, typename V1, typename C1>
1.150 friend class SparseMap;
1.151 public:
1.152
1.153 - typedef MapBase<K, V> Parent;
1.154 /// Key type
1.155 - typedef typename Parent::Key Key;
1.156 + typedef K Key;
1.157 /// Value type
1.158 - typedef typename Parent::Value Value;
1.159 + typedef V Value;
1.160 /// Reference type
1.161 typedef Value& Reference;
1.162 /// Const reference type
1.163 @@ -373,7 +374,7 @@
1.164
1.165 private:
1.166
1.167 - typedef std::map<K, V, Compare> Map;
1.168 + typedef std::map<K, V, Comp> Map;
1.169 Map _map;
1.170 Value _value;
1.171
1.172 @@ -489,14 +490,15 @@
1.173 const M1 &_m1;
1.174 const M2 &_m2;
1.175 public:
1.176 - typedef MapBase<typename M2::Key, typename M1::Value> Parent;
1.177 - typedef typename Parent::Key Key;
1.178 - typedef typename Parent::Value Value;
1.179 + ///\e
1.180 + typedef typename M2::Key Key;
1.181 + ///\e
1.182 + typedef typename M1::Value Value;
1.183
1.184 /// Constructor
1.185 ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.186
1.187 - /// \e
1.188 + ///\e
1.189 typename MapTraits<M1>::ConstReturnValue
1.190 operator[](const Key &k) const { return _m1[_m2[k]]; }
1.191 };
1.192 @@ -545,14 +547,15 @@
1.193 const M2 &_m2;
1.194 F _f;
1.195 public:
1.196 - typedef MapBase<typename M1::Key, V> Parent;
1.197 - typedef typename Parent::Key Key;
1.198 - typedef typename Parent::Value Value;
1.199 + ///\e
1.200 + typedef typename M1::Key Key;
1.201 + ///\e
1.202 + typedef V Value;
1.203
1.204 /// Constructor
1.205 CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
1.206 : _m1(m1), _m2(m2), _f(f) {}
1.207 - /// \e
1.208 + ///\e
1.209 Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
1.210 };
1.211
1.212 @@ -615,13 +618,14 @@
1.213 class FunctorToMap : public MapBase<K, V> {
1.214 F _f;
1.215 public:
1.216 - typedef MapBase<K, V> Parent;
1.217 - typedef typename Parent::Key Key;
1.218 - typedef typename Parent::Value Value;
1.219 + ///\e
1.220 + typedef K Key;
1.221 + ///\e
1.222 + typedef V Value;
1.223
1.224 /// Constructor
1.225 FunctorToMap(const F &f = F()) : _f(f) {}
1.226 - /// \e
1.227 + ///\e
1.228 Value operator[](const Key &k) const { return _f(k); }
1.229 };
1.230
1.231 @@ -669,18 +673,19 @@
1.232 class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
1.233 const M &_m;
1.234 public:
1.235 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.236 - typedef typename Parent::Key Key;
1.237 - typedef typename Parent::Value Value;
1.238 -
1.239 - typedef typename Parent::Key argument_type;
1.240 - typedef typename Parent::Value result_type;
1.241 + ///\e
1.242 + typedef typename M::Key Key;
1.243 + ///\e
1.244 + typedef typename M::Value Value;
1.245 +
1.246 + typedef typename M::Key argument_type;
1.247 + typedef typename M::Value result_type;
1.248
1.249 /// Constructor
1.250 MapToFunctor(const M &m) : _m(m) {}
1.251 - /// \e
1.252 + ///\e
1.253 Value operator()(const Key &k) const { return _m[k]; }
1.254 - /// \e
1.255 + ///\e
1.256 Value operator[](const Key &k) const { return _m[k]; }
1.257 };
1.258
1.259 @@ -701,7 +706,7 @@
1.260 /// "readable map" to another type using the default conversion.
1.261 /// The \c Key type of it is inherited from \c M and the \c Value
1.262 /// type is \c V.
1.263 - /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
1.264 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.265 ///
1.266 /// The simplest way of using this map is through the convertMap()
1.267 /// function.
1.268 @@ -709,9 +714,10 @@
1.269 class ConvertMap : public MapBase<typename M::Key, V> {
1.270 const M &_m;
1.271 public:
1.272 - typedef MapBase<typename M::Key, V> Parent;
1.273 - typedef typename Parent::Key Key;
1.274 - typedef typename Parent::Value Value;
1.275 + ///\e
1.276 + typedef typename M::Key Key;
1.277 + ///\e
1.278 + typedef V Value;
1.279
1.280 /// Constructor
1.281
1.282 @@ -719,7 +725,7 @@
1.283 /// \param m The underlying map.
1.284 ConvertMap(const M &m) : _m(m) {}
1.285
1.286 - /// \e
1.287 + ///\e
1.288 Value operator[](const Key &k) const { return _m[k]; }
1.289 };
1.290
1.291 @@ -751,9 +757,10 @@
1.292 M1 &_m1;
1.293 M2 &_m2;
1.294 public:
1.295 - typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1.296 - typedef typename Parent::Key Key;
1.297 - typedef typename Parent::Value Value;
1.298 + ///\e
1.299 + typedef typename M1::Key Key;
1.300 + ///\e
1.301 + typedef typename M1::Value Value;
1.302
1.303 /// Constructor
1.304 ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
1.305 @@ -797,13 +804,14 @@
1.306 const M1 &_m1;
1.307 const M2 &_m2;
1.308 public:
1.309 - typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1.310 - typedef typename Parent::Key Key;
1.311 - typedef typename Parent::Value Value;
1.312 + ///\e
1.313 + typedef typename M1::Key Key;
1.314 + ///\e
1.315 + typedef typename M1::Value Value;
1.316
1.317 /// Constructor
1.318 AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.319 - /// \e
1.320 + ///\e
1.321 Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
1.322 };
1.323
1.324 @@ -845,13 +853,14 @@
1.325 const M1 &_m1;
1.326 const M2 &_m2;
1.327 public:
1.328 - typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1.329 - typedef typename Parent::Key Key;
1.330 - typedef typename Parent::Value Value;
1.331 + ///\e
1.332 + typedef typename M1::Key Key;
1.333 + ///\e
1.334 + typedef typename M1::Value Value;
1.335
1.336 /// Constructor
1.337 SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.338 - /// \e
1.339 + ///\e
1.340 Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
1.341 };
1.342
1.343 @@ -894,13 +903,14 @@
1.344 const M1 &_m1;
1.345 const M2 &_m2;
1.346 public:
1.347 - typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1.348 - typedef typename Parent::Key Key;
1.349 - typedef typename Parent::Value Value;
1.350 + ///\e
1.351 + typedef typename M1::Key Key;
1.352 + ///\e
1.353 + typedef typename M1::Value Value;
1.354
1.355 /// Constructor
1.356 MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
1.357 - /// \e
1.358 + ///\e
1.359 Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
1.360 };
1.361
1.362 @@ -942,13 +952,14 @@
1.363 const M1 &_m1;
1.364 const M2 &_m2;
1.365 public:
1.366 - typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1.367 - typedef typename Parent::Key Key;
1.368 - typedef typename Parent::Value Value;
1.369 + ///\e
1.370 + typedef typename M1::Key Key;
1.371 + ///\e
1.372 + typedef typename M1::Value Value;
1.373
1.374 /// Constructor
1.375 DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
1.376 - /// \e
1.377 + ///\e
1.378 Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
1.379 };
1.380
1.381 @@ -992,9 +1003,10 @@
1.382 const M &_m;
1.383 C _v;
1.384 public:
1.385 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.386 - typedef typename Parent::Key Key;
1.387 - typedef typename Parent::Value Value;
1.388 + ///\e
1.389 + typedef typename M::Key Key;
1.390 + ///\e
1.391 + typedef typename M::Value Value;
1.392
1.393 /// Constructor
1.394
1.395 @@ -1002,7 +1014,7 @@
1.396 /// \param m The undelying map.
1.397 /// \param v The constant value.
1.398 ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1.399 - /// \e
1.400 + ///\e
1.401 Value operator[](const Key &k) const { return _m[k]+_v; }
1.402 };
1.403
1.404 @@ -1022,9 +1034,10 @@
1.405 M &_m;
1.406 C _v;
1.407 public:
1.408 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.409 - typedef typename Parent::Key Key;
1.410 - typedef typename Parent::Value Value;
1.411 + ///\e
1.412 + typedef typename M::Key Key;
1.413 + ///\e
1.414 + typedef typename M::Value Value;
1.415
1.416 /// Constructor
1.417
1.418 @@ -1032,9 +1045,9 @@
1.419 /// \param m The undelying map.
1.420 /// \param v The constant value.
1.421 ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1.422 - /// \e
1.423 + ///\e
1.424 Value operator[](const Key &k) const { return _m[k]+_v; }
1.425 - /// \e
1.426 + ///\e
1.427 void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1.428 };
1.429
1.430 @@ -1093,9 +1106,10 @@
1.431 const M &_m;
1.432 C _v;
1.433 public:
1.434 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.435 - typedef typename Parent::Key Key;
1.436 - typedef typename Parent::Value Value;
1.437 + ///\e
1.438 + typedef typename M::Key Key;
1.439 + ///\e
1.440 + typedef typename M::Value Value;
1.441
1.442 /// Constructor
1.443
1.444 @@ -1103,7 +1117,7 @@
1.445 /// \param m The undelying map.
1.446 /// \param v The constant value.
1.447 ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1.448 - /// \e
1.449 + ///\e
1.450 Value operator[](const Key &k) const { return _v*_m[k]; }
1.451 };
1.452
1.453 @@ -1124,9 +1138,10 @@
1.454 M &_m;
1.455 C _v;
1.456 public:
1.457 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.458 - typedef typename Parent::Key Key;
1.459 - typedef typename Parent::Value Value;
1.460 + ///\e
1.461 + typedef typename M::Key Key;
1.462 + ///\e
1.463 + typedef typename M::Value Value;
1.464
1.465 /// Constructor
1.466
1.467 @@ -1134,9 +1149,9 @@
1.468 /// \param m The undelying map.
1.469 /// \param v The constant value.
1.470 ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1.471 - /// \e
1.472 + ///\e
1.473 Value operator[](const Key &k) const { return _v*_m[k]; }
1.474 - /// \e
1.475 + ///\e
1.476 void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1.477 };
1.478
1.479 @@ -1193,13 +1208,14 @@
1.480 class NegMap : public MapBase<typename M::Key, typename M::Value> {
1.481 const M& _m;
1.482 public:
1.483 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.484 - typedef typename Parent::Key Key;
1.485 - typedef typename Parent::Value Value;
1.486 + ///\e
1.487 + typedef typename M::Key Key;
1.488 + ///\e
1.489 + typedef typename M::Value Value;
1.490
1.491 /// Constructor
1.492 NegMap(const M &m) : _m(m) {}
1.493 - /// \e
1.494 + ///\e
1.495 Value operator[](const Key &k) const { return -_m[k]; }
1.496 };
1.497
1.498 @@ -1228,15 +1244,16 @@
1.499 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1.500 M &_m;
1.501 public:
1.502 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.503 - typedef typename Parent::Key Key;
1.504 - typedef typename Parent::Value Value;
1.505 + ///\e
1.506 + typedef typename M::Key Key;
1.507 + ///\e
1.508 + typedef typename M::Value Value;
1.509
1.510 /// Constructor
1.511 NegWriteMap(M &m) : _m(m) {}
1.512 - /// \e
1.513 + ///\e
1.514 Value operator[](const Key &k) const { return -_m[k]; }
1.515 - /// \e
1.516 + ///\e
1.517 void set(const Key &k, const Value &v) { _m.set(k, -v); }
1.518 };
1.519
1.520 @@ -1282,13 +1299,14 @@
1.521 class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1.522 const M &_m;
1.523 public:
1.524 - typedef MapBase<typename M::Key, typename M::Value> Parent;
1.525 - typedef typename Parent::Key Key;
1.526 - typedef typename Parent::Value Value;
1.527 + ///\e
1.528 + typedef typename M::Key Key;
1.529 + ///\e
1.530 + typedef typename M::Value Value;
1.531
1.532 /// Constructor
1.533 AbsMap(const M &m) : _m(m) {}
1.534 - /// \e
1.535 + ///\e
1.536 Value operator[](const Key &k) const {
1.537 Value tmp = _m[k];
1.538 return tmp >= 0 ? tmp : -tmp;
1.539 @@ -1337,9 +1355,10 @@
1.540 template <typename K>
1.541 class TrueMap : public MapBase<K, bool> {
1.542 public:
1.543 - typedef MapBase<K, bool> Parent;
1.544 - typedef typename Parent::Key Key;
1.545 - typedef typename Parent::Value Value;
1.546 + ///\e
1.547 + typedef K Key;
1.548 + ///\e
1.549 + typedef bool Value;
1.550
1.551 /// Gives back \c true.
1.552 Value operator[](const Key&) const { return true; }
1.553 @@ -1374,9 +1393,10 @@
1.554 template <typename K>
1.555 class FalseMap : public MapBase<K, bool> {
1.556 public:
1.557 - typedef MapBase<K, bool> Parent;
1.558 - typedef typename Parent::Key Key;
1.559 - typedef typename Parent::Value Value;
1.560 + ///\e
1.561 + typedef K Key;
1.562 + ///\e
1.563 + typedef bool Value;
1.564
1.565 /// Gives back \c false.
1.566 Value operator[](const Key&) const { return false; }
1.567 @@ -1419,13 +1439,14 @@
1.568 const M1 &_m1;
1.569 const M2 &_m2;
1.570 public:
1.571 - typedef MapBase<typename M1::Key, bool> Parent;
1.572 - typedef typename Parent::Key Key;
1.573 - typedef typename Parent::Value Value;
1.574 + ///\e
1.575 + typedef typename M1::Key Key;
1.576 + ///\e
1.577 + typedef bool Value;
1.578
1.579 /// Constructor
1.580 AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.581 - /// \e
1.582 + ///\e
1.583 Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1.584 };
1.585
1.586 @@ -1467,13 +1488,14 @@
1.587 const M1 &_m1;
1.588 const M2 &_m2;
1.589 public:
1.590 - typedef MapBase<typename M1::Key, bool> Parent;
1.591 - typedef typename Parent::Key Key;
1.592 - typedef typename Parent::Value Value;
1.593 + ///\e
1.594 + typedef typename M1::Key Key;
1.595 + ///\e
1.596 + typedef bool Value;
1.597
1.598 /// Constructor
1.599 OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.600 - /// \e
1.601 + ///\e
1.602 Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1.603 };
1.604
1.605 @@ -1506,13 +1528,14 @@
1.606 class NotMap : public MapBase<typename M::Key, bool> {
1.607 const M &_m;
1.608 public:
1.609 - typedef MapBase<typename M::Key, bool> Parent;
1.610 - typedef typename Parent::Key Key;
1.611 - typedef typename Parent::Value Value;
1.612 + ///\e
1.613 + typedef typename M::Key Key;
1.614 + ///\e
1.615 + typedef bool Value;
1.616
1.617 /// Constructor
1.618 NotMap(const M &m) : _m(m) {}
1.619 - /// \e
1.620 + ///\e
1.621 Value operator[](const Key &k) const { return !_m[k]; }
1.622 };
1.623
1.624 @@ -1532,15 +1555,16 @@
1.625 class NotWriteMap : public MapBase<typename M::Key, bool> {
1.626 M &_m;
1.627 public:
1.628 - typedef MapBase<typename M::Key, bool> Parent;
1.629 - typedef typename Parent::Key Key;
1.630 - typedef typename Parent::Value Value;
1.631 + ///\e
1.632 + typedef typename M::Key Key;
1.633 + ///\e
1.634 + typedef bool Value;
1.635
1.636 /// Constructor
1.637 NotWriteMap(M &m) : _m(m) {}
1.638 - /// \e
1.639 + ///\e
1.640 Value operator[](const Key &k) const { return !_m[k]; }
1.641 - /// \e
1.642 + ///\e
1.643 void set(const Key &k, bool v) { _m.set(k, !v); }
1.644 };
1.645
1.646 @@ -1595,13 +1619,14 @@
1.647 const M1 &_m1;
1.648 const M2 &_m2;
1.649 public:
1.650 - typedef MapBase<typename M1::Key, bool> Parent;
1.651 - typedef typename Parent::Key Key;
1.652 - typedef typename Parent::Value Value;
1.653 + ///\e
1.654 + typedef typename M1::Key Key;
1.655 + ///\e
1.656 + typedef bool Value;
1.657
1.658 /// Constructor
1.659 EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.660 - /// \e
1.661 + ///\e
1.662 Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1.663 };
1.664
1.665 @@ -1643,13 +1668,14 @@
1.666 const M1 &_m1;
1.667 const M2 &_m2;
1.668 public:
1.669 - typedef MapBase<typename M1::Key, bool> Parent;
1.670 - typedef typename Parent::Key Key;
1.671 - typedef typename Parent::Value Value;
1.672 + ///\e
1.673 + typedef typename M1::Key Key;
1.674 + ///\e
1.675 + typedef bool Value;
1.676
1.677 /// Constructor
1.678 LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1.679 - /// \e
1.680 + ///\e
1.681 Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1.682 };
1.683
1.684 @@ -1705,24 +1731,27 @@
1.685 /// The simplest way of using this map is through the loggerBoolMap()
1.686 /// function.
1.687 ///
1.688 - /// \tparam It The type of the iterator.
1.689 - /// \tparam Ke The key type of the map. The default value set
1.690 + /// \tparam IT The type of the iterator.
1.691 + /// \tparam KEY The key type of the map. The default value set
1.692 /// according to the iterator type should work in most cases.
1.693 ///
1.694 /// \note The container of the iterator must contain enough space
1.695 /// for the elements or the iterator should be an inserter iterator.
1.696 #ifdef DOXYGEN
1.697 - template <typename It, typename Ke>
1.698 + template <typename IT, typename KEY>
1.699 #else
1.700 - template <typename It,
1.701 - typename Ke=typename _maps_bits::IteratorTraits<It>::Value>
1.702 + template <typename IT,
1.703 + typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1.704 #endif
1.705 - class LoggerBoolMap {
1.706 + class LoggerBoolMap : public MapBase<KEY, bool> {
1.707 public:
1.708 - typedef It Iterator;
1.709 -
1.710 - typedef Ke Key;
1.711 +
1.712 + ///\e
1.713 + typedef KEY Key;
1.714 + ///\e
1.715 typedef bool Value;
1.716 + ///\e
1.717 + typedef IT Iterator;
1.718
1.719 /// Constructor
1.720 LoggerBoolMap(Iterator it)
1.721 @@ -1760,11 +1789,11 @@
1.722 /// order of Dfs algorithm, as the following examples show.
1.723 /// \code
1.724 /// std::vector<Node> v;
1.725 - /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1.726 + /// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
1.727 /// \endcode
1.728 /// \code
1.729 /// std::vector<Node> v(countNodes(g));
1.730 - /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1.731 + /// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
1.732 /// \endcode
1.733 ///
1.734 /// \note The container of the iterator must contain enough space
1.735 @@ -1785,23 +1814,36 @@
1.736 /// \addtogroup graph_maps
1.737 /// @{
1.738
1.739 - /// Provides an immutable and unique id for each item in the graph.
1.740 -
1.741 - /// The IdMap class provides a unique and immutable id for each item of the
1.742 - /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
1.743 - /// different items (nodes) get different ids <li>\b immutable: the id of an
1.744 - /// item (node) does not change (even if you delete other nodes). </ul>
1.745 - /// Through this map you get access (i.e. can read) the inner id values of
1.746 - /// the items stored in the graph. This map can be inverted with its member
1.747 - /// class \c InverseMap or with the \c operator() member.
1.748 + /// \brief Provides an immutable and unique id for each item in a graph.
1.749 ///
1.750 - template <typename _Graph, typename _Item>
1.751 - class IdMap {
1.752 + /// IdMap provides a unique and immutable id for each item of the
1.753 + /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1.754 + /// - \b unique: different items get different ids,
1.755 + /// - \b immutable: the id of an item does not change (even if you
1.756 + /// delete other nodes).
1.757 + ///
1.758 + /// Using this map you get access (i.e. can read) the inner id values of
1.759 + /// the items stored in the graph, which is returned by the \c id()
1.760 + /// function of the graph. This map can be inverted with its member
1.761 + /// class \c InverseMap or with the \c operator()() member.
1.762 + ///
1.763 + /// \tparam GR The graph type.
1.764 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.765 + /// \c GR::Edge).
1.766 + ///
1.767 + /// \see RangeIdMap
1.768 + template <typename GR, typename K>
1.769 + class IdMap : public MapBase<K, int> {
1.770 public:
1.771 - typedef _Graph Graph;
1.772 + /// The graph type of IdMap.
1.773 + typedef GR Graph;
1.774 + typedef GR Digraph;
1.775 + /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1.776 + typedef K Item;
1.777 + /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1.778 + typedef K Key;
1.779 + /// The value type of IdMap.
1.780 typedef int Value;
1.781 - typedef _Item Item;
1.782 - typedef _Item Key;
1.783
1.784 /// \brief Constructor.
1.785 ///
1.786 @@ -1813,9 +1855,9 @@
1.787 /// Gives back the immutable and unique \e id of the item.
1.788 int operator[](const Item& item) const { return _graph->id(item);}
1.789
1.790 - /// \brief Gives back the item by its id.
1.791 + /// \brief Gives back the \e item by its id.
1.792 ///
1.793 - /// Gives back the item by its id.
1.794 + /// Gives back the \e item by its id.
1.795 Item operator()(int id) { return _graph->fromId(id, Item()); }
1.796
1.797 private:
1.798 @@ -1823,9 +1865,11 @@
1.799
1.800 public:
1.801
1.802 - /// \brief The class represents the inverse of its owner (IdMap).
1.803 + /// \brief The inverse map type of IdMap.
1.804 ///
1.805 - /// The class represents the inverse of its owner (IdMap).
1.806 + /// The inverse map type of IdMap. The subscript operator gives back
1.807 + /// an item by its id.
1.808 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.809 /// \see inverse()
1.810 class InverseMap {
1.811 public:
1.812 @@ -1840,10 +1884,9 @@
1.813 /// Constructor for creating an id-to-item map.
1.814 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1.815
1.816 - /// \brief Gives back the given item from its id.
1.817 + /// \brief Gives back an item by its id.
1.818 ///
1.819 - /// Gives back the given item from its id.
1.820 - ///
1.821 + /// Gives back an item by its id.
1.822 Item operator[](int id) const { return _graph->fromId(id, Item());}
1.823
1.824 private:
1.825 @@ -1854,165 +1897,220 @@
1.826 ///
1.827 /// Gives back the inverse of the IdMap.
1.828 InverseMap inverse() const { return InverseMap(*_graph);}
1.829 -
1.830 };
1.831
1.832 -
1.833 - /// \brief General invertable graph-map type.
1.834 -
1.835 - /// This type provides simple invertable graph-maps.
1.836 - /// The InvertableMap wraps an arbitrary ReadWriteMap
1.837 - /// and if a key is set to a new value then store it
1.838 - /// in the inverse map.
1.839 + /// \brief Returns an \c IdMap class.
1.840 ///
1.841 - /// The values of the map can be accessed
1.842 - /// with stl compatible forward iterator.
1.843 + /// This function just returns an \c IdMap class.
1.844 + /// \relates IdMap
1.845 + template <typename K, typename GR>
1.846 + inline IdMap<GR, K> idMap(const GR& graph) {
1.847 + return IdMap<GR, K>(graph);
1.848 + }
1.849 +
1.850 + /// \brief General cross reference graph map type.
1.851 +
1.852 + /// This class provides simple invertable graph maps.
1.853 + /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1.854 + /// and if a key is set to a new value, then stores it in the inverse map.
1.855 + /// The graph items can be accessed by their values either using
1.856 + /// \c InverseMap or \c operator()(), and the values of the map can be
1.857 + /// accessed with an STL compatible forward iterator (\c ValueIt).
1.858 + ///
1.859 + /// This map is intended to be used when all associated values are
1.860 + /// different (the map is actually invertable) or there are only a few
1.861 + /// items with the same value.
1.862 + /// Otherwise consider to use \c IterableValueMap, which is more
1.863 + /// suitable and more efficient for such cases. It provides iterators
1.864 + /// to traverse the items with the same associated value, however
1.865 + /// it does not have \c InverseMap.
1.866 ///
1.867 - /// \tparam _Graph The graph type.
1.868 - /// \tparam _Item The item type of the graph.
1.869 - /// \tparam _Value The value type of the map.
1.870 + /// This type is not reference map, so it cannot be modified with
1.871 + /// the subscript operator.
1.872 + ///
1.873 + /// \tparam GR The graph type.
1.874 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.875 + /// \c GR::Edge).
1.876 + /// \tparam V The value type of the map.
1.877 ///
1.878 /// \see IterableValueMap
1.879 - template <typename _Graph, typename _Item, typename _Value>
1.880 - class InvertableMap
1.881 - : protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type {
1.882 + template <typename GR, typename K, typename V>
1.883 + class CrossRefMap
1.884 + : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1.885 private:
1.886
1.887 - typedef typename ItemSetTraits<_Graph, _Item>::
1.888 - template Map<_Value>::Type Map;
1.889 - typedef _Graph Graph;
1.890 -
1.891 - typedef std::map<_Value, _Item> Container;
1.892 + typedef typename ItemSetTraits<GR, K>::
1.893 + template Map<V>::Type Map;
1.894 +
1.895 + typedef std::multimap<V, K> Container;
1.896 Container _inv_map;
1.897
1.898 public:
1.899
1.900 - /// The key type of InvertableMap (Node, Arc, Edge).
1.901 - typedef typename Map::Key Key;
1.902 - /// The value type of the InvertableMap.
1.903 - typedef typename Map::Value Value;
1.904 + /// The graph type of CrossRefMap.
1.905 + typedef GR Graph;
1.906 + typedef GR Digraph;
1.907 + /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1.908 + typedef K Item;
1.909 + /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1.910 + typedef K Key;
1.911 + /// The value type of CrossRefMap.
1.912 + typedef V Value;
1.913
1.914 /// \brief Constructor.
1.915 ///
1.916 - /// Construct a new InvertableMap for the graph.
1.917 - ///
1.918 - explicit InvertableMap(const Graph& graph) : Map(graph) {}
1.919 + /// Construct a new CrossRefMap for the given graph.
1.920 + explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1.921
1.922 /// \brief Forward iterator for values.
1.923 ///
1.924 - /// This iterator is an stl compatible forward
1.925 + /// This iterator is an STL compatible forward
1.926 /// iterator on the values of the map. The values can
1.927 - /// be accessed in the [beginValue, endValue) range.
1.928 - ///
1.929 - class ValueIterator
1.930 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.931 + /// They are considered with multiplicity, so each value is
1.932 + /// traversed for each item it is assigned to.
1.933 + class ValueIt
1.934 : public std::iterator<std::forward_iterator_tag, Value> {
1.935 - friend class InvertableMap;
1.936 + friend class CrossRefMap;
1.937 private:
1.938 - ValueIterator(typename Container::const_iterator _it)
1.939 + ValueIt(typename Container::const_iterator _it)
1.940 : it(_it) {}
1.941 public:
1.942
1.943 - ValueIterator() {}
1.944 -
1.945 - ValueIterator& operator++() { ++it; return *this; }
1.946 - ValueIterator operator++(int) {
1.947 - ValueIterator tmp(*this);
1.948 + /// Constructor
1.949 + ValueIt() {}
1.950 +
1.951 + /// \e
1.952 + ValueIt& operator++() { ++it; return *this; }
1.953 + /// \e
1.954 + ValueIt operator++(int) {
1.955 + ValueIt tmp(*this);
1.956 operator++();
1.957 return tmp;
1.958 }
1.959
1.960 + /// \e
1.961 const Value& operator*() const { return it->first; }
1.962 + /// \e
1.963 const Value* operator->() const { return &(it->first); }
1.964
1.965 - bool operator==(ValueIterator jt) const { return it == jt.it; }
1.966 - bool operator!=(ValueIterator jt) const { return it != jt.it; }
1.967 + /// \e
1.968 + bool operator==(ValueIt jt) const { return it == jt.it; }
1.969 + /// \e
1.970 + bool operator!=(ValueIt jt) const { return it != jt.it; }
1.971
1.972 private:
1.973 typename Container::const_iterator it;
1.974 };
1.975 +
1.976 + /// Alias for \c ValueIt
1.977 + typedef ValueIt ValueIterator;
1.978
1.979 /// \brief Returns an iterator to the first value.
1.980 ///
1.981 - /// Returns an stl compatible iterator to the
1.982 + /// Returns an STL compatible iterator to the
1.983 /// first value of the map. The values of the
1.984 - /// map can be accessed in the [beginValue, endValue)
1.985 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.986 /// range.
1.987 - ValueIterator beginValue() const {
1.988 - return ValueIterator(_inv_map.begin());
1.989 + ValueIt beginValue() const {
1.990 + return ValueIt(_inv_map.begin());
1.991 }
1.992
1.993 /// \brief Returns an iterator after the last value.
1.994 ///
1.995 - /// Returns an stl compatible iterator after the
1.996 + /// Returns an STL compatible iterator after the
1.997 /// last value of the map. The values of the
1.998 - /// map can be accessed in the [beginValue, endValue)
1.999 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.1000 /// range.
1.1001 - ValueIterator endValue() const {
1.1002 - return ValueIterator(_inv_map.end());
1.1003 + ValueIt endValue() const {
1.1004 + return ValueIt(_inv_map.end());
1.1005 }
1.1006
1.1007 - /// \brief The setter function of the map.
1.1008 + /// \brief Sets the value associated with the given key.
1.1009 ///
1.1010 - /// Sets the mapped value.
1.1011 + /// Sets the value associated with the given key.
1.1012 void set(const Key& key, const Value& val) {
1.1013 Value oldval = Map::operator[](key);
1.1014 - typename Container::iterator it = _inv_map.find(oldval);
1.1015 - if (it != _inv_map.end() && it->second == key) {
1.1016 - _inv_map.erase(it);
1.1017 + typename Container::iterator it;
1.1018 + for (it = _inv_map.equal_range(oldval).first;
1.1019 + it != _inv_map.equal_range(oldval).second; ++it) {
1.1020 + if (it->second == key) {
1.1021 + _inv_map.erase(it);
1.1022 + break;
1.1023 + }
1.1024 }
1.1025 - _inv_map.insert(make_pair(val, key));
1.1026 + _inv_map.insert(std::make_pair(val, key));
1.1027 Map::set(key, val);
1.1028 }
1.1029
1.1030 - /// \brief The getter function of the map.
1.1031 + /// \brief Returns the value associated with the given key.
1.1032 ///
1.1033 - /// It gives back the value associated with the key.
1.1034 + /// Returns the value associated with the given key.
1.1035 typename MapTraits<Map>::ConstReturnValue
1.1036 operator[](const Key& key) const {
1.1037 return Map::operator[](key);
1.1038 }
1.1039
1.1040 - /// \brief Gives back the item by its value.
1.1041 + /// \brief Gives back an item by its value.
1.1042 ///
1.1043 - /// Gives back the item by its value.
1.1044 - Key operator()(const Value& key) const {
1.1045 - typename Container::const_iterator it = _inv_map.find(key);
1.1046 + /// This function gives back an item that is assigned to
1.1047 + /// the given value or \c INVALID if no such item exists.
1.1048 + /// If there are more items with the same associated value,
1.1049 + /// only one of them is returned.
1.1050 + Key operator()(const Value& val) const {
1.1051 + typename Container::const_iterator it = _inv_map.find(val);
1.1052 return it != _inv_map.end() ? it->second : INVALID;
1.1053 }
1.1054 +
1.1055 + /// \brief Returns the number of items with the given value.
1.1056 + ///
1.1057 + /// This function returns the number of items with the given value
1.1058 + /// associated with it.
1.1059 + int count(const Value &val) const {
1.1060 + return _inv_map.count(val);
1.1061 + }
1.1062
1.1063 protected:
1.1064
1.1065 - /// \brief Erase the key from the map.
1.1066 + /// \brief Erase the key from the map and the inverse map.
1.1067 ///
1.1068 - /// Erase the key to the map. It is called by the
1.1069 + /// Erase the key from the map and the inverse map. It is called by the
1.1070 /// \c AlterationNotifier.
1.1071 virtual void erase(const Key& key) {
1.1072 Value val = Map::operator[](key);
1.1073 - typename Container::iterator it = _inv_map.find(val);
1.1074 - if (it != _inv_map.end() && it->second == key) {
1.1075 - _inv_map.erase(it);
1.1076 + typename Container::iterator it;
1.1077 + for (it = _inv_map.equal_range(val).first;
1.1078 + it != _inv_map.equal_range(val).second; ++it) {
1.1079 + if (it->second == key) {
1.1080 + _inv_map.erase(it);
1.1081 + break;
1.1082 + }
1.1083 }
1.1084 Map::erase(key);
1.1085 }
1.1086
1.1087 - /// \brief Erase more keys from the map.
1.1088 + /// \brief Erase more keys from the map and the inverse map.
1.1089 ///
1.1090 - /// Erase more keys from the map. It is called by the
1.1091 + /// Erase more keys from the map and the inverse map. It is called by the
1.1092 /// \c AlterationNotifier.
1.1093 virtual void erase(const std::vector<Key>& keys) {
1.1094 for (int i = 0; i < int(keys.size()); ++i) {
1.1095 Value val = Map::operator[](keys[i]);
1.1096 - typename Container::iterator it = _inv_map.find(val);
1.1097 - if (it != _inv_map.end() && it->second == keys[i]) {
1.1098 - _inv_map.erase(it);
1.1099 + typename Container::iterator it;
1.1100 + for (it = _inv_map.equal_range(val).first;
1.1101 + it != _inv_map.equal_range(val).second; ++it) {
1.1102 + if (it->second == keys[i]) {
1.1103 + _inv_map.erase(it);
1.1104 + break;
1.1105 + }
1.1106 }
1.1107 }
1.1108 Map::erase(keys);
1.1109 }
1.1110
1.1111 - /// \brief Clear the keys from the map and inverse map.
1.1112 + /// \brief Clear the keys from the map and the inverse map.
1.1113 ///
1.1114 - /// Clear the keys from the map and inverse map. It is called by the
1.1115 + /// Clear the keys from the map and the inverse map. It is called by the
1.1116 /// \c AlterationNotifier.
1.1117 virtual void clear() {
1.1118 _inv_map.clear();
1.1119 @@ -2021,79 +2119,90 @@
1.1120
1.1121 public:
1.1122
1.1123 - /// \brief The inverse map type.
1.1124 + /// \brief The inverse map type of CrossRefMap.
1.1125 ///
1.1126 - /// The inverse of this map. The subscript operator of the map
1.1127 - /// gives back always the item what was last assigned to the value.
1.1128 + /// The inverse map type of CrossRefMap. The subscript operator gives
1.1129 + /// back an item by its value.
1.1130 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.1131 + /// \see inverse()
1.1132 class InverseMap {
1.1133 public:
1.1134 - /// \brief Constructor of the InverseMap.
1.1135 + /// \brief Constructor
1.1136 ///
1.1137 /// Constructor of the InverseMap.
1.1138 - explicit InverseMap(const InvertableMap& inverted)
1.1139 + explicit InverseMap(const CrossRefMap& inverted)
1.1140 : _inverted(inverted) {}
1.1141
1.1142 /// The value type of the InverseMap.
1.1143 - typedef typename InvertableMap::Key Value;
1.1144 + typedef typename CrossRefMap::Key Value;
1.1145 /// The key type of the InverseMap.
1.1146 - typedef typename InvertableMap::Value Key;
1.1147 + typedef typename CrossRefMap::Value Key;
1.1148
1.1149 /// \brief Subscript operator.
1.1150 ///
1.1151 - /// Subscript operator. It gives back always the item
1.1152 - /// what was last assigned to the value.
1.1153 + /// Subscript operator. It gives back an item
1.1154 + /// that is assigned to the given value or \c INVALID
1.1155 + /// if no such item exists.
1.1156 Value operator[](const Key& key) const {
1.1157 return _inverted(key);
1.1158 }
1.1159
1.1160 private:
1.1161 - const InvertableMap& _inverted;
1.1162 + const CrossRefMap& _inverted;
1.1163 };
1.1164
1.1165 - /// \brief It gives back the just readable inverse map.
1.1166 + /// \brief Gives back the inverse of the map.
1.1167 ///
1.1168 - /// It gives back the just readable inverse map.
1.1169 + /// Gives back the inverse of the CrossRefMap.
1.1170 InverseMap inverse() const {
1.1171 return InverseMap(*this);
1.1172 }
1.1173
1.1174 };
1.1175
1.1176 - /// \brief Provides a mutable, continuous and unique descriptor for each
1.1177 - /// item in the graph.
1.1178 + /// \brief Provides continuous and unique id for the
1.1179 + /// items of a graph.
1.1180 ///
1.1181 - /// The DescriptorMap class provides a unique and continuous (but mutable)
1.1182 - /// descriptor (id) for each item of the same type (e.g. node) in the
1.1183 - /// graph. This id is <ul><li>\b unique: different items (nodes) get
1.1184 - /// different ids <li>\b continuous: the range of the ids is the set of
1.1185 - /// integers between 0 and \c n-1, where \c n is the number of the items of
1.1186 - /// this type (e.g. nodes) (so the id of a node can change if you delete an
1.1187 - /// other node, i.e. this id is mutable). </ul> This map can be inverted
1.1188 - /// with its member class \c InverseMap, or with the \c operator() member.
1.1189 + /// RangeIdMap provides a unique and continuous
1.1190 + /// id for each item of a given type (\c Node, \c Arc or
1.1191 + /// \c Edge) in a graph. This id is
1.1192 + /// - \b unique: different items get different ids,
1.1193 + /// - \b continuous: the range of the ids is the set of integers
1.1194 + /// between 0 and \c n-1, where \c n is the number of the items of
1.1195 + /// this type (\c Node, \c Arc or \c Edge).
1.1196 + /// - So, the ids can change when deleting an item of the same type.
1.1197 ///
1.1198 - /// \tparam _Graph The graph class the \c DescriptorMap belongs to.
1.1199 - /// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or
1.1200 - /// Edge.
1.1201 - template <typename _Graph, typename _Item>
1.1202 - class DescriptorMap
1.1203 - : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type {
1.1204 -
1.1205 - typedef _Item Item;
1.1206 - typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map;
1.1207 + /// Thus this id is not (necessarily) the same as what can get using
1.1208 + /// the \c id() function of the graph or \ref IdMap.
1.1209 + /// This map can be inverted with its member class \c InverseMap,
1.1210 + /// or with the \c operator()() member.
1.1211 + ///
1.1212 + /// \tparam GR The graph type.
1.1213 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.1214 + /// \c GR::Edge).
1.1215 + ///
1.1216 + /// \see IdMap
1.1217 + template <typename GR, typename K>
1.1218 + class RangeIdMap
1.1219 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
1.1220 +
1.1221 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
1.1222
1.1223 public:
1.1224 - /// The graph class of DescriptorMap.
1.1225 - typedef _Graph Graph;
1.1226 -
1.1227 - /// The key type of DescriptorMap (Node, Arc, Edge).
1.1228 - typedef typename Map::Key Key;
1.1229 - /// The value type of DescriptorMap.
1.1230 - typedef typename Map::Value Value;
1.1231 + /// The graph type of RangeIdMap.
1.1232 + typedef GR Graph;
1.1233 + typedef GR Digraph;
1.1234 + /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
1.1235 + typedef K Item;
1.1236 + /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
1.1237 + typedef K Key;
1.1238 + /// The value type of RangeIdMap.
1.1239 + typedef int Value;
1.1240
1.1241 /// \brief Constructor.
1.1242 ///
1.1243 - /// Constructor for descriptor map.
1.1244 - explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
1.1245 + /// Constructor.
1.1246 + explicit RangeIdMap(const Graph& gr) : Map(gr) {
1.1247 Item it;
1.1248 const typename Map::Notifier* nf = Map::notifier();
1.1249 for (nf->first(it); it != INVALID; nf->next(it)) {
1.1250 @@ -2104,7 +2213,7 @@
1.1251
1.1252 protected:
1.1253
1.1254 - /// \brief Add a new key to the map.
1.1255 + /// \brief Adds a new key to the map.
1.1256 ///
1.1257 /// Add a new key to the map. It is called by the
1.1258 /// \c AlterationNotifier.
1.1259 @@ -2194,16 +2303,16 @@
1.1260 _inv_map[pi] = q;
1.1261 }
1.1262
1.1263 - /// \brief Gives back the \e descriptor of the item.
1.1264 + /// \brief Gives back the \e range \e id of the item
1.1265 ///
1.1266 - /// Gives back the mutable and unique \e descriptor of the map.
1.1267 + /// Gives back the \e range \e id of the item.
1.1268 int operator[](const Item& item) const {
1.1269 return Map::operator[](item);
1.1270 }
1.1271
1.1272 - /// \brief Gives back the item by its descriptor.
1.1273 + /// \brief Gives back the item belonging to a \e range \e id
1.1274 ///
1.1275 - /// Gives back th item by its descriptor.
1.1276 + /// Gives back the item belonging to the given \e range \e id.
1.1277 Item operator()(int id) const {
1.1278 return _inv_map[id];
1.1279 }
1.1280 @@ -2214,27 +2323,30 @@
1.1281 Container _inv_map;
1.1282
1.1283 public:
1.1284 - /// \brief The inverse map type of DescriptorMap.
1.1285 +
1.1286 + /// \brief The inverse map type of RangeIdMap.
1.1287 ///
1.1288 - /// The inverse map type of DescriptorMap.
1.1289 + /// The inverse map type of RangeIdMap. The subscript operator gives
1.1290 + /// back an item by its \e range \e id.
1.1291 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.1292 class InverseMap {
1.1293 public:
1.1294 - /// \brief Constructor of the InverseMap.
1.1295 + /// \brief Constructor
1.1296 ///
1.1297 /// Constructor of the InverseMap.
1.1298 - explicit InverseMap(const DescriptorMap& inverted)
1.1299 + explicit InverseMap(const RangeIdMap& inverted)
1.1300 : _inverted(inverted) {}
1.1301
1.1302
1.1303 /// The value type of the InverseMap.
1.1304 - typedef typename DescriptorMap::Key Value;
1.1305 + typedef typename RangeIdMap::Key Value;
1.1306 /// The key type of the InverseMap.
1.1307 - typedef typename DescriptorMap::Value Key;
1.1308 + typedef typename RangeIdMap::Value Key;
1.1309
1.1310 /// \brief Subscript operator.
1.1311 ///
1.1312 /// Subscript operator. It gives back the item
1.1313 - /// that the descriptor belongs to currently.
1.1314 + /// that the given \e range \e id currently belongs to.
1.1315 Value operator[](const Key& key) const {
1.1316 return _inverted(key);
1.1317 }
1.1318 @@ -2247,241 +2359,1134 @@
1.1319 }
1.1320
1.1321 private:
1.1322 - const DescriptorMap& _inverted;
1.1323 + const RangeIdMap& _inverted;
1.1324 };
1.1325
1.1326 /// \brief Gives back the inverse of the map.
1.1327 ///
1.1328 - /// Gives back the inverse of the map.
1.1329 + /// Gives back the inverse of the RangeIdMap.
1.1330 const InverseMap inverse() const {
1.1331 return InverseMap(*this);
1.1332 }
1.1333 };
1.1334
1.1335 - /// \brief Returns the source of the given arc.
1.1336 + /// \brief Returns a \c RangeIdMap class.
1.1337 ///
1.1338 - /// The SourceMap gives back the source Node of the given arc.
1.1339 + /// This function just returns an \c RangeIdMap class.
1.1340 + /// \relates RangeIdMap
1.1341 + template <typename K, typename GR>
1.1342 + inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
1.1343 + return RangeIdMap<GR, K>(graph);
1.1344 + }
1.1345 +
1.1346 + /// \brief Dynamic iterable \c bool map.
1.1347 + ///
1.1348 + /// This class provides a special graph map type which can store a
1.1349 + /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
1.1350 + /// For both \c true and \c false values it is possible to iterate on
1.1351 + /// the keys mapped to the value.
1.1352 + ///
1.1353 + /// This type is a reference map, so it can be modified with the
1.1354 + /// subscript operator.
1.1355 + ///
1.1356 + /// \tparam GR The graph type.
1.1357 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.1358 + /// \c GR::Edge).
1.1359 + ///
1.1360 + /// \see IterableIntMap, IterableValueMap
1.1361 + /// \see CrossRefMap
1.1362 + template <typename GR, typename K>
1.1363 + class IterableBoolMap
1.1364 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
1.1365 + private:
1.1366 + typedef GR Graph;
1.1367 +
1.1368 + typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
1.1369 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
1.1370 +
1.1371 + std::vector<K> _array;
1.1372 + int _sep;
1.1373 +
1.1374 + public:
1.1375 +
1.1376 + /// Indicates that the map is reference map.
1.1377 + typedef True ReferenceMapTag;
1.1378 +
1.1379 + /// The key type
1.1380 + typedef K Key;
1.1381 + /// The value type
1.1382 + typedef bool Value;
1.1383 + /// The const reference type.
1.1384 + typedef const Value& ConstReference;
1.1385 +
1.1386 + private:
1.1387 +
1.1388 + int position(const Key& key) const {
1.1389 + return Parent::operator[](key);
1.1390 + }
1.1391 +
1.1392 + public:
1.1393 +
1.1394 + /// \brief Reference to the value of the map.
1.1395 + ///
1.1396 + /// This class is similar to the \c bool type. It can be converted to
1.1397 + /// \c bool and it provides the same operators.
1.1398 + class Reference {
1.1399 + friend class IterableBoolMap;
1.1400 + private:
1.1401 + Reference(IterableBoolMap& map, const Key& key)
1.1402 + : _key(key), _map(map) {}
1.1403 + public:
1.1404 +
1.1405 + Reference& operator=(const Reference& value) {
1.1406 + _map.set(_key, static_cast<bool>(value));
1.1407 + return *this;
1.1408 + }
1.1409 +
1.1410 + operator bool() const {
1.1411 + return static_cast<const IterableBoolMap&>(_map)[_key];
1.1412 + }
1.1413 +
1.1414 + Reference& operator=(bool value) {
1.1415 + _map.set(_key, value);
1.1416 + return *this;
1.1417 + }
1.1418 + Reference& operator&=(bool value) {
1.1419 + _map.set(_key, _map[_key] & value);
1.1420 + return *this;
1.1421 + }
1.1422 + Reference& operator|=(bool value) {
1.1423 + _map.set(_key, _map[_key] | value);
1.1424 + return *this;
1.1425 + }
1.1426 + Reference& operator^=(bool value) {
1.1427 + _map.set(_key, _map[_key] ^ value);
1.1428 + return *this;
1.1429 + }
1.1430 + private:
1.1431 + Key _key;
1.1432 + IterableBoolMap& _map;
1.1433 + };
1.1434 +
1.1435 + /// \brief Constructor of the map with a default value.
1.1436 + ///
1.1437 + /// Constructor of the map with a default value.
1.1438 + explicit IterableBoolMap(const Graph& graph, bool def = false)
1.1439 + : Parent(graph) {
1.1440 + typename Parent::Notifier* nf = Parent::notifier();
1.1441 + Key it;
1.1442 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.1443 + Parent::set(it, _array.size());
1.1444 + _array.push_back(it);
1.1445 + }
1.1446 + _sep = (def ? _array.size() : 0);
1.1447 + }
1.1448 +
1.1449 + /// \brief Const subscript operator of the map.
1.1450 + ///
1.1451 + /// Const subscript operator of the map.
1.1452 + bool operator[](const Key& key) const {
1.1453 + return position(key) < _sep;
1.1454 + }
1.1455 +
1.1456 + /// \brief Subscript operator of the map.
1.1457 + ///
1.1458 + /// Subscript operator of the map.
1.1459 + Reference operator[](const Key& key) {
1.1460 + return Reference(*this, key);
1.1461 + }
1.1462 +
1.1463 + /// \brief Set operation of the map.
1.1464 + ///
1.1465 + /// Set operation of the map.
1.1466 + void set(const Key& key, bool value) {
1.1467 + int pos = position(key);
1.1468 + if (value) {
1.1469 + if (pos < _sep) return;
1.1470 + Key tmp = _array[_sep];
1.1471 + _array[_sep] = key;
1.1472 + Parent::set(key, _sep);
1.1473 + _array[pos] = tmp;
1.1474 + Parent::set(tmp, pos);
1.1475 + ++_sep;
1.1476 + } else {
1.1477 + if (pos >= _sep) return;
1.1478 + --_sep;
1.1479 + Key tmp = _array[_sep];
1.1480 + _array[_sep] = key;
1.1481 + Parent::set(key, _sep);
1.1482 + _array[pos] = tmp;
1.1483 + Parent::set(tmp, pos);
1.1484 + }
1.1485 + }
1.1486 +
1.1487 + /// \brief Set all items.
1.1488 + ///
1.1489 + /// Set all items in the map.
1.1490 + /// \note Constant time operation.
1.1491 + void setAll(bool value) {
1.1492 + _sep = (value ? _array.size() : 0);
1.1493 + }
1.1494 +
1.1495 + /// \brief Returns the number of the keys mapped to \c true.
1.1496 + ///
1.1497 + /// Returns the number of the keys mapped to \c true.
1.1498 + int trueNum() const {
1.1499 + return _sep;
1.1500 + }
1.1501 +
1.1502 + /// \brief Returns the number of the keys mapped to \c false.
1.1503 + ///
1.1504 + /// Returns the number of the keys mapped to \c false.
1.1505 + int falseNum() const {
1.1506 + return _array.size() - _sep;
1.1507 + }
1.1508 +
1.1509 + /// \brief Iterator for the keys mapped to \c true.
1.1510 + ///
1.1511 + /// Iterator for the keys mapped to \c true. It works
1.1512 + /// like a graph item iterator, it can be converted to
1.1513 + /// the key type of the map, incremented with \c ++ operator, and
1.1514 + /// if the iterator leaves the last valid key, it will be equal to
1.1515 + /// \c INVALID.
1.1516 + class TrueIt : public Key {
1.1517 + public:
1.1518 + typedef Key Parent;
1.1519 +
1.1520 + /// \brief Creates an iterator.
1.1521 + ///
1.1522 + /// Creates an iterator. It iterates on the
1.1523 + /// keys mapped to \c true.
1.1524 + /// \param map The IterableBoolMap.
1.1525 + explicit TrueIt(const IterableBoolMap& map)
1.1526 + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
1.1527 + _map(&map) {}
1.1528 +
1.1529 + /// \brief Invalid constructor \& conversion.
1.1530 + ///
1.1531 + /// This constructor initializes the iterator to be invalid.
1.1532 + /// \sa Invalid for more details.
1.1533 + TrueIt(Invalid) : Parent(INVALID), _map(0) {}
1.1534 +
1.1535 + /// \brief Increment operator.
1.1536 + ///
1.1537 + /// Increment operator.
1.1538 + TrueIt& operator++() {
1.1539 + int pos = _map->position(*this);
1.1540 + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
1.1541 + return *this;
1.1542 + }
1.1543 +
1.1544 + private:
1.1545 + const IterableBoolMap* _map;
1.1546 + };
1.1547 +
1.1548 + /// \brief Iterator for the keys mapped to \c false.
1.1549 + ///
1.1550 + /// Iterator for the keys mapped to \c false. It works
1.1551 + /// like a graph item iterator, it can be converted to
1.1552 + /// the key type of the map, incremented with \c ++ operator, and
1.1553 + /// if the iterator leaves the last valid key, it will be equal to
1.1554 + /// \c INVALID.
1.1555 + class FalseIt : public Key {
1.1556 + public:
1.1557 + typedef Key Parent;
1.1558 +
1.1559 + /// \brief Creates an iterator.
1.1560 + ///
1.1561 + /// Creates an iterator. It iterates on the
1.1562 + /// keys mapped to \c false.
1.1563 + /// \param map The IterableBoolMap.
1.1564 + explicit FalseIt(const IterableBoolMap& map)
1.1565 + : Parent(map._sep < int(map._array.size()) ?
1.1566 + map._array.back() : INVALID), _map(&map) {}
1.1567 +
1.1568 + /// \brief Invalid constructor \& conversion.
1.1569 + ///
1.1570 + /// This constructor initializes the iterator to be invalid.
1.1571 + /// \sa Invalid for more details.
1.1572 + FalseIt(Invalid) : Parent(INVALID), _map(0) {}
1.1573 +
1.1574 + /// \brief Increment operator.
1.1575 + ///
1.1576 + /// Increment operator.
1.1577 + FalseIt& operator++() {
1.1578 + int pos = _map->position(*this);
1.1579 + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
1.1580 + return *this;
1.1581 + }
1.1582 +
1.1583 + private:
1.1584 + const IterableBoolMap* _map;
1.1585 + };
1.1586 +
1.1587 + /// \brief Iterator for the keys mapped to a given value.
1.1588 + ///
1.1589 + /// Iterator for the keys mapped to a given value. It works
1.1590 + /// like a graph item iterator, it can be converted to
1.1591 + /// the key type of the map, incremented with \c ++ operator, and
1.1592 + /// if the iterator leaves the last valid key, it will be equal to
1.1593 + /// \c INVALID.
1.1594 + class ItemIt : public Key {
1.1595 + public:
1.1596 + typedef Key Parent;
1.1597 +
1.1598 + /// \brief Creates an iterator with a value.
1.1599 + ///
1.1600 + /// Creates an iterator with a value. It iterates on the
1.1601 + /// keys mapped to the given value.
1.1602 + /// \param map The IterableBoolMap.
1.1603 + /// \param value The value.
1.1604 + ItemIt(const IterableBoolMap& map, bool value)
1.1605 + : Parent(value ?
1.1606 + (map._sep > 0 ?
1.1607 + map._array[map._sep - 1] : INVALID) :
1.1608 + (map._sep < int(map._array.size()) ?
1.1609 + map._array.back() : INVALID)), _map(&map) {}
1.1610 +
1.1611 + /// \brief Invalid constructor \& conversion.
1.1612 + ///
1.1613 + /// This constructor initializes the iterator to be invalid.
1.1614 + /// \sa Invalid for more details.
1.1615 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.1616 +
1.1617 + /// \brief Increment operator.
1.1618 + ///
1.1619 + /// Increment operator.
1.1620 + ItemIt& operator++() {
1.1621 + int pos = _map->position(*this);
1.1622 + int _sep = pos >= _map->_sep ? _map->_sep : 0;
1.1623 + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
1.1624 + return *this;
1.1625 + }
1.1626 +
1.1627 + private:
1.1628 + const IterableBoolMap* _map;
1.1629 + };
1.1630 +
1.1631 + protected:
1.1632 +
1.1633 + virtual void add(const Key& key) {
1.1634 + Parent::add(key);
1.1635 + Parent::set(key, _array.size());
1.1636 + _array.push_back(key);
1.1637 + }
1.1638 +
1.1639 + virtual void add(const std::vector<Key>& keys) {
1.1640 + Parent::add(keys);
1.1641 + for (int i = 0; i < int(keys.size()); ++i) {
1.1642 + Parent::set(keys[i], _array.size());
1.1643 + _array.push_back(keys[i]);
1.1644 + }
1.1645 + }
1.1646 +
1.1647 + virtual void erase(const Key& key) {
1.1648 + int pos = position(key);
1.1649 + if (pos < _sep) {
1.1650 + --_sep;
1.1651 + Parent::set(_array[_sep], pos);
1.1652 + _array[pos] = _array[_sep];
1.1653 + Parent::set(_array.back(), _sep);
1.1654 + _array[_sep] = _array.back();
1.1655 + _array.pop_back();
1.1656 + } else {
1.1657 + Parent::set(_array.back(), pos);
1.1658 + _array[pos] = _array.back();
1.1659 + _array.pop_back();
1.1660 + }
1.1661 + Parent::erase(key);
1.1662 + }
1.1663 +
1.1664 + virtual void erase(const std::vector<Key>& keys) {
1.1665 + for (int i = 0; i < int(keys.size()); ++i) {
1.1666 + int pos = position(keys[i]);
1.1667 + if (pos < _sep) {
1.1668 + --_sep;
1.1669 + Parent::set(_array[_sep], pos);
1.1670 + _array[pos] = _array[_sep];
1.1671 + Parent::set(_array.back(), _sep);
1.1672 + _array[_sep] = _array.back();
1.1673 + _array.pop_back();
1.1674 + } else {
1.1675 + Parent::set(_array.back(), pos);
1.1676 + _array[pos] = _array.back();
1.1677 + _array.pop_back();
1.1678 + }
1.1679 + }
1.1680 + Parent::erase(keys);
1.1681 + }
1.1682 +
1.1683 + virtual void build() {
1.1684 + Parent::build();
1.1685 + typename Parent::Notifier* nf = Parent::notifier();
1.1686 + Key it;
1.1687 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.1688 + Parent::set(it, _array.size());
1.1689 + _array.push_back(it);
1.1690 + }
1.1691 + _sep = 0;
1.1692 + }
1.1693 +
1.1694 + virtual void clear() {
1.1695 + _array.clear();
1.1696 + _sep = 0;
1.1697 + Parent::clear();
1.1698 + }
1.1699 +
1.1700 + };
1.1701 +
1.1702 +
1.1703 + namespace _maps_bits {
1.1704 + template <typename Item>
1.1705 + struct IterableIntMapNode {
1.1706 + IterableIntMapNode() : value(-1) {}
1.1707 + IterableIntMapNode(int _value) : value(_value) {}
1.1708 + Item prev, next;
1.1709 + int value;
1.1710 + };
1.1711 + }
1.1712 +
1.1713 + /// \brief Dynamic iterable integer map.
1.1714 + ///
1.1715 + /// This class provides a special graph map type which can store an
1.1716 + /// integer value for graph items (\c Node, \c Arc or \c Edge).
1.1717 + /// For each non-negative value it is possible to iterate on the keys
1.1718 + /// mapped to the value.
1.1719 + ///
1.1720 + /// This map is intended to be used with small integer values, for which
1.1721 + /// it is efficient, and supports iteration only for non-negative values.
1.1722 + /// If you need large values and/or iteration for negative integers,
1.1723 + /// consider to use \ref IterableValueMap instead.
1.1724 + ///
1.1725 + /// This type is a reference map, so it can be modified with the
1.1726 + /// subscript operator.
1.1727 + ///
1.1728 + /// \note The size of the data structure depends on the largest
1.1729 + /// value in the map.
1.1730 + ///
1.1731 + /// \tparam GR The graph type.
1.1732 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.1733 + /// \c GR::Edge).
1.1734 + ///
1.1735 + /// \see IterableBoolMap, IterableValueMap
1.1736 + /// \see CrossRefMap
1.1737 + template <typename GR, typename K>
1.1738 + class IterableIntMap
1.1739 + : protected ItemSetTraits<GR, K>::
1.1740 + template Map<_maps_bits::IterableIntMapNode<K> >::Type {
1.1741 + public:
1.1742 + typedef typename ItemSetTraits<GR, K>::
1.1743 + template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
1.1744 +
1.1745 + /// The key type
1.1746 + typedef K Key;
1.1747 + /// The value type
1.1748 + typedef int Value;
1.1749 + /// The graph type
1.1750 + typedef GR Graph;
1.1751 +
1.1752 + /// \brief Constructor of the map.
1.1753 + ///
1.1754 + /// Constructor of the map. It sets all values to -1.
1.1755 + explicit IterableIntMap(const Graph& graph)
1.1756 + : Parent(graph) {}
1.1757 +
1.1758 + /// \brief Constructor of the map with a given value.
1.1759 + ///
1.1760 + /// Constructor of the map with a given value.
1.1761 + explicit IterableIntMap(const Graph& graph, int value)
1.1762 + : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
1.1763 + if (value >= 0) {
1.1764 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.1765 + lace(it);
1.1766 + }
1.1767 + }
1.1768 + }
1.1769 +
1.1770 + private:
1.1771 +
1.1772 + void unlace(const Key& key) {
1.1773 + typename Parent::Value& node = Parent::operator[](key);
1.1774 + if (node.value < 0) return;
1.1775 + if (node.prev != INVALID) {
1.1776 + Parent::operator[](node.prev).next = node.next;
1.1777 + } else {
1.1778 + _first[node.value] = node.next;
1.1779 + }
1.1780 + if (node.next != INVALID) {
1.1781 + Parent::operator[](node.next).prev = node.prev;
1.1782 + }
1.1783 + while (!_first.empty() && _first.back() == INVALID) {
1.1784 + _first.pop_back();
1.1785 + }
1.1786 + }
1.1787 +
1.1788 + void lace(const Key& key) {
1.1789 + typename Parent::Value& node = Parent::operator[](key);
1.1790 + if (node.value < 0) return;
1.1791 + if (node.value >= int(_first.size())) {
1.1792 + _first.resize(node.value + 1, INVALID);
1.1793 + }
1.1794 + node.prev = INVALID;
1.1795 + node.next = _first[node.value];
1.1796 + if (node.next != INVALID) {
1.1797 + Parent::operator[](node.next).prev = key;
1.1798 + }
1.1799 + _first[node.value] = key;
1.1800 + }
1.1801 +
1.1802 + public:
1.1803 +
1.1804 + /// Indicates that the map is reference map.
1.1805 + typedef True ReferenceMapTag;
1.1806 +
1.1807 + /// \brief Reference to the value of the map.
1.1808 + ///
1.1809 + /// This class is similar to the \c int type. It can
1.1810 + /// be converted to \c int and it has the same operators.
1.1811 + class Reference {
1.1812 + friend class IterableIntMap;
1.1813 + private:
1.1814 + Reference(IterableIntMap& map, const Key& key)
1.1815 + : _key(key), _map(map) {}
1.1816 + public:
1.1817 +
1.1818 + Reference& operator=(const Reference& value) {
1.1819 + _map.set(_key, static_cast<const int&>(value));
1.1820 + return *this;
1.1821 + }
1.1822 +
1.1823 + operator const int&() const {
1.1824 + return static_cast<const IterableIntMap&>(_map)[_key];
1.1825 + }
1.1826 +
1.1827 + Reference& operator=(int value) {
1.1828 + _map.set(_key, value);
1.1829 + return *this;
1.1830 + }
1.1831 + Reference& operator++() {
1.1832 + _map.set(_key, _map[_key] + 1);
1.1833 + return *this;
1.1834 + }
1.1835 + int operator++(int) {
1.1836 + int value = _map[_key];
1.1837 + _map.set(_key, value + 1);
1.1838 + return value;
1.1839 + }
1.1840 + Reference& operator--() {
1.1841 + _map.set(_key, _map[_key] - 1);
1.1842 + return *this;
1.1843 + }
1.1844 + int operator--(int) {
1.1845 + int value = _map[_key];
1.1846 + _map.set(_key, value - 1);
1.1847 + return value;
1.1848 + }
1.1849 + Reference& operator+=(int value) {
1.1850 + _map.set(_key, _map[_key] + value);
1.1851 + return *this;
1.1852 + }
1.1853 + Reference& operator-=(int value) {
1.1854 + _map.set(_key, _map[_key] - value);
1.1855 + return *this;
1.1856 + }
1.1857 + Reference& operator*=(int value) {
1.1858 + _map.set(_key, _map[_key] * value);
1.1859 + return *this;
1.1860 + }
1.1861 + Reference& operator/=(int value) {
1.1862 + _map.set(_key, _map[_key] / value);
1.1863 + return *this;
1.1864 + }
1.1865 + Reference& operator%=(int value) {
1.1866 + _map.set(_key, _map[_key] % value);
1.1867 + return *this;
1.1868 + }
1.1869 + Reference& operator&=(int value) {
1.1870 + _map.set(_key, _map[_key] & value);
1.1871 + return *this;
1.1872 + }
1.1873 + Reference& operator|=(int value) {
1.1874 + _map.set(_key, _map[_key] | value);
1.1875 + return *this;
1.1876 + }
1.1877 + Reference& operator^=(int value) {
1.1878 + _map.set(_key, _map[_key] ^ value);
1.1879 + return *this;
1.1880 + }
1.1881 + Reference& operator<<=(int value) {
1.1882 + _map.set(_key, _map[_key] << value);
1.1883 + return *this;
1.1884 + }
1.1885 + Reference& operator>>=(int value) {
1.1886 + _map.set(_key, _map[_key] >> value);
1.1887 + return *this;
1.1888 + }
1.1889 +
1.1890 + private:
1.1891 + Key _key;
1.1892 + IterableIntMap& _map;
1.1893 + };
1.1894 +
1.1895 + /// The const reference type.
1.1896 + typedef const Value& ConstReference;
1.1897 +
1.1898 + /// \brief Gives back the maximal value plus one.
1.1899 + ///
1.1900 + /// Gives back the maximal value plus one.
1.1901 + int size() const {
1.1902 + return _first.size();
1.1903 + }
1.1904 +
1.1905 + /// \brief Set operation of the map.
1.1906 + ///
1.1907 + /// Set operation of the map.
1.1908 + void set(const Key& key, const Value& value) {
1.1909 + unlace(key);
1.1910 + Parent::operator[](key).value = value;
1.1911 + lace(key);
1.1912 + }
1.1913 +
1.1914 + /// \brief Const subscript operator of the map.
1.1915 + ///
1.1916 + /// Const subscript operator of the map.
1.1917 + const Value& operator[](const Key& key) const {
1.1918 + return Parent::operator[](key).value;
1.1919 + }
1.1920 +
1.1921 + /// \brief Subscript operator of the map.
1.1922 + ///
1.1923 + /// Subscript operator of the map.
1.1924 + Reference operator[](const Key& key) {
1.1925 + return Reference(*this, key);
1.1926 + }
1.1927 +
1.1928 + /// \brief Iterator for the keys with the same value.
1.1929 + ///
1.1930 + /// Iterator for the keys with the same value. It works
1.1931 + /// like a graph item iterator, it can be converted to
1.1932 + /// the item type of the map, incremented with \c ++ operator, and
1.1933 + /// if the iterator leaves the last valid item, it will be equal to
1.1934 + /// \c INVALID.
1.1935 + class ItemIt : public Key {
1.1936 + public:
1.1937 + typedef Key Parent;
1.1938 +
1.1939 + /// \brief Invalid constructor \& conversion.
1.1940 + ///
1.1941 + /// This constructor initializes the iterator to be invalid.
1.1942 + /// \sa Invalid for more details.
1.1943 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.1944 +
1.1945 + /// \brief Creates an iterator with a value.
1.1946 + ///
1.1947 + /// Creates an iterator with a value. It iterates on the
1.1948 + /// keys mapped to the given value.
1.1949 + /// \param map The IterableIntMap.
1.1950 + /// \param value The value.
1.1951 + ItemIt(const IterableIntMap& map, int value) : _map(&map) {
1.1952 + if (value < 0 || value >= int(_map->_first.size())) {
1.1953 + Parent::operator=(INVALID);
1.1954 + } else {
1.1955 + Parent::operator=(_map->_first[value]);
1.1956 + }
1.1957 + }
1.1958 +
1.1959 + /// \brief Increment operator.
1.1960 + ///
1.1961 + /// Increment operator.
1.1962 + ItemIt& operator++() {
1.1963 + Parent::operator=(_map->IterableIntMap::Parent::
1.1964 + operator[](static_cast<Parent&>(*this)).next);
1.1965 + return *this;
1.1966 + }
1.1967 +
1.1968 + private:
1.1969 + const IterableIntMap* _map;
1.1970 + };
1.1971 +
1.1972 + protected:
1.1973 +
1.1974 + virtual void erase(const Key& key) {
1.1975 + unlace(key);
1.1976 + Parent::erase(key);
1.1977 + }
1.1978 +
1.1979 + virtual void erase(const std::vector<Key>& keys) {
1.1980 + for (int i = 0; i < int(keys.size()); ++i) {
1.1981 + unlace(keys[i]);
1.1982 + }
1.1983 + Parent::erase(keys);
1.1984 + }
1.1985 +
1.1986 + virtual void clear() {
1.1987 + _first.clear();
1.1988 + Parent::clear();
1.1989 + }
1.1990 +
1.1991 + private:
1.1992 + std::vector<Key> _first;
1.1993 + };
1.1994 +
1.1995 + namespace _maps_bits {
1.1996 + template <typename Item, typename Value>
1.1997 + struct IterableValueMapNode {
1.1998 + IterableValueMapNode(Value _value = Value()) : value(_value) {}
1.1999 + Item prev, next;
1.2000 + Value value;
1.2001 + };
1.2002 + }
1.2003 +
1.2004 + /// \brief Dynamic iterable map for comparable values.
1.2005 + ///
1.2006 + /// This class provides a special graph map type which can store a
1.2007 + /// comparable value for graph items (\c Node, \c Arc or \c Edge).
1.2008 + /// For each value it is possible to iterate on the keys mapped to
1.2009 + /// the value (\c ItemIt), and the values of the map can be accessed
1.2010 + /// with an STL compatible forward iterator (\c ValueIt).
1.2011 + /// The map stores a linked list for each value, which contains
1.2012 + /// the items mapped to the value, and the used values are stored
1.2013 + /// in balanced binary tree (\c std::map).
1.2014 + ///
1.2015 + /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
1.2016 + /// specialized for \c bool and \c int values, respectively.
1.2017 + ///
1.2018 + /// This type is not reference map, so it cannot be modified with
1.2019 + /// the subscript operator.
1.2020 + ///
1.2021 + /// \tparam GR The graph type.
1.2022 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.2023 + /// \c GR::Edge).
1.2024 + /// \tparam V The value type of the map. It can be any comparable
1.2025 + /// value type.
1.2026 + ///
1.2027 + /// \see IterableBoolMap, IterableIntMap
1.2028 + /// \see CrossRefMap
1.2029 + template <typename GR, typename K, typename V>
1.2030 + class IterableValueMap
1.2031 + : protected ItemSetTraits<GR, K>::
1.2032 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
1.2033 + public:
1.2034 + typedef typename ItemSetTraits<GR, K>::
1.2035 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
1.2036 +
1.2037 + /// The key type
1.2038 + typedef K Key;
1.2039 + /// The value type
1.2040 + typedef V Value;
1.2041 + /// The graph type
1.2042 + typedef GR Graph;
1.2043 +
1.2044 + public:
1.2045 +
1.2046 + /// \brief Constructor of the map with a given value.
1.2047 + ///
1.2048 + /// Constructor of the map with a given value.
1.2049 + explicit IterableValueMap(const Graph& graph,
1.2050 + const Value& value = Value())
1.2051 + : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
1.2052 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.2053 + lace(it);
1.2054 + }
1.2055 + }
1.2056 +
1.2057 + protected:
1.2058 +
1.2059 + void unlace(const Key& key) {
1.2060 + typename Parent::Value& node = Parent::operator[](key);
1.2061 + if (node.prev != INVALID) {
1.2062 + Parent::operator[](node.prev).next = node.next;
1.2063 + } else {
1.2064 + if (node.next != INVALID) {
1.2065 + _first[node.value] = node.next;
1.2066 + } else {
1.2067 + _first.erase(node.value);
1.2068 + }
1.2069 + }
1.2070 + if (node.next != INVALID) {
1.2071 + Parent::operator[](node.next).prev = node.prev;
1.2072 + }
1.2073 + }
1.2074 +
1.2075 + void lace(const Key& key) {
1.2076 + typename Parent::Value& node = Parent::operator[](key);
1.2077 + typename std::map<Value, Key>::iterator it = _first.find(node.value);
1.2078 + if (it == _first.end()) {
1.2079 + node.prev = node.next = INVALID;
1.2080 + _first.insert(std::make_pair(node.value, key));
1.2081 + } else {
1.2082 + node.prev = INVALID;
1.2083 + node.next = it->second;
1.2084 + if (node.next != INVALID) {
1.2085 + Parent::operator[](node.next).prev = key;
1.2086 + }
1.2087 + it->second = key;
1.2088 + }
1.2089 + }
1.2090 +
1.2091 + public:
1.2092 +
1.2093 + /// \brief Forward iterator for values.
1.2094 + ///
1.2095 + /// This iterator is an STL compatible forward
1.2096 + /// iterator on the values of the map. The values can
1.2097 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.2098 + class ValueIt
1.2099 + : public std::iterator<std::forward_iterator_tag, Value> {
1.2100 + friend class IterableValueMap;
1.2101 + private:
1.2102 + ValueIt(typename std::map<Value, Key>::const_iterator _it)
1.2103 + : it(_it) {}
1.2104 + public:
1.2105 +
1.2106 + /// Constructor
1.2107 + ValueIt() {}
1.2108 +
1.2109 + /// \e
1.2110 + ValueIt& operator++() { ++it; return *this; }
1.2111 + /// \e
1.2112 + ValueIt operator++(int) {
1.2113 + ValueIt tmp(*this);
1.2114 + operator++();
1.2115 + return tmp;
1.2116 + }
1.2117 +
1.2118 + /// \e
1.2119 + const Value& operator*() const { return it->first; }
1.2120 + /// \e
1.2121 + const Value* operator->() const { return &(it->first); }
1.2122 +
1.2123 + /// \e
1.2124 + bool operator==(ValueIt jt) const { return it == jt.it; }
1.2125 + /// \e
1.2126 + bool operator!=(ValueIt jt) const { return it != jt.it; }
1.2127 +
1.2128 + private:
1.2129 + typename std::map<Value, Key>::const_iterator it;
1.2130 + };
1.2131 +
1.2132 + /// \brief Returns an iterator to the first value.
1.2133 + ///
1.2134 + /// Returns an STL compatible iterator to the
1.2135 + /// first value of the map. The values of the
1.2136 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.2137 + /// range.
1.2138 + ValueIt beginValue() const {
1.2139 + return ValueIt(_first.begin());
1.2140 + }
1.2141 +
1.2142 + /// \brief Returns an iterator after the last value.
1.2143 + ///
1.2144 + /// Returns an STL compatible iterator after the
1.2145 + /// last value of the map. The values of the
1.2146 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.2147 + /// range.
1.2148 + ValueIt endValue() const {
1.2149 + return ValueIt(_first.end());
1.2150 + }
1.2151 +
1.2152 + /// \brief Set operation of the map.
1.2153 + ///
1.2154 + /// Set operation of the map.
1.2155 + void set(const Key& key, const Value& value) {
1.2156 + unlace(key);
1.2157 + Parent::operator[](key).value = value;
1.2158 + lace(key);
1.2159 + }
1.2160 +
1.2161 + /// \brief Const subscript operator of the map.
1.2162 + ///
1.2163 + /// Const subscript operator of the map.
1.2164 + const Value& operator[](const Key& key) const {
1.2165 + return Parent::operator[](key).value;
1.2166 + }
1.2167 +
1.2168 + /// \brief Iterator for the keys with the same value.
1.2169 + ///
1.2170 + /// Iterator for the keys with the same value. It works
1.2171 + /// like a graph item iterator, it can be converted to
1.2172 + /// the item type of the map, incremented with \c ++ operator, and
1.2173 + /// if the iterator leaves the last valid item, it will be equal to
1.2174 + /// \c INVALID.
1.2175 + class ItemIt : public Key {
1.2176 + public:
1.2177 + typedef Key Parent;
1.2178 +
1.2179 + /// \brief Invalid constructor \& conversion.
1.2180 + ///
1.2181 + /// This constructor initializes the iterator to be invalid.
1.2182 + /// \sa Invalid for more details.
1.2183 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.2184 +
1.2185 + /// \brief Creates an iterator with a value.
1.2186 + ///
1.2187 + /// Creates an iterator with a value. It iterates on the
1.2188 + /// keys which have the given value.
1.2189 + /// \param map The IterableValueMap
1.2190 + /// \param value The value
1.2191 + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
1.2192 + typename std::map<Value, Key>::const_iterator it =
1.2193 + map._first.find(value);
1.2194 + if (it == map._first.end()) {
1.2195 + Parent::operator=(INVALID);
1.2196 + } else {
1.2197 + Parent::operator=(it->second);
1.2198 + }
1.2199 + }
1.2200 +
1.2201 + /// \brief Increment operator.
1.2202 + ///
1.2203 + /// Increment Operator.
1.2204 + ItemIt& operator++() {
1.2205 + Parent::operator=(_map->IterableValueMap::Parent::
1.2206 + operator[](static_cast<Parent&>(*this)).next);
1.2207 + return *this;
1.2208 + }
1.2209 +
1.2210 +
1.2211 + private:
1.2212 + const IterableValueMap* _map;
1.2213 + };
1.2214 +
1.2215 + protected:
1.2216 +
1.2217 + virtual void add(const Key& key) {
1.2218 + Parent::add(key);
1.2219 + unlace(key);
1.2220 + }
1.2221 +
1.2222 + virtual void add(const std::vector<Key>& keys) {
1.2223 + Parent::add(keys);
1.2224 + for (int i = 0; i < int(keys.size()); ++i) {
1.2225 + lace(keys[i]);
1.2226 + }
1.2227 + }
1.2228 +
1.2229 + virtual void erase(const Key& key) {
1.2230 + unlace(key);
1.2231 + Parent::erase(key);
1.2232 + }
1.2233 +
1.2234 + virtual void erase(const std::vector<Key>& keys) {
1.2235 + for (int i = 0; i < int(keys.size()); ++i) {
1.2236 + unlace(keys[i]);
1.2237 + }
1.2238 + Parent::erase(keys);
1.2239 + }
1.2240 +
1.2241 + virtual void build() {
1.2242 + Parent::build();
1.2243 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.2244 + lace(it);
1.2245 + }
1.2246 + }
1.2247 +
1.2248 + virtual void clear() {
1.2249 + _first.clear();
1.2250 + Parent::clear();
1.2251 + }
1.2252 +
1.2253 + private:
1.2254 + std::map<Value, Key> _first;
1.2255 + };
1.2256 +
1.2257 + /// \brief Map of the source nodes of arcs in a digraph.
1.2258 + ///
1.2259 + /// SourceMap provides access for the source node of each arc in a digraph,
1.2260 + /// which is returned by the \c source() function of the digraph.
1.2261 + /// \tparam GR The digraph type.
1.2262 /// \see TargetMap
1.2263 - template <typename Digraph>
1.2264 + template <typename GR>
1.2265 class SourceMap {
1.2266 public:
1.2267
1.2268 - typedef typename Digraph::Node Value;
1.2269 - typedef typename Digraph::Arc Key;
1.2270 + /// The key type (the \c Arc type of the digraph).
1.2271 + typedef typename GR::Arc Key;
1.2272 + /// The value type (the \c Node type of the digraph).
1.2273 + typedef typename GR::Node Value;
1.2274
1.2275 /// \brief Constructor
1.2276 ///
1.2277 - /// Constructor
1.2278 + /// Constructor.
1.2279 /// \param digraph The digraph that the map belongs to.
1.2280 - explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
1.2281 -
1.2282 - /// \brief The subscript operator.
1.2283 + explicit SourceMap(const GR& digraph) : _graph(digraph) {}
1.2284 +
1.2285 + /// \brief Returns the source node of the given arc.
1.2286 ///
1.2287 - /// The subscript operator.
1.2288 - /// \param arc The arc
1.2289 - /// \return The source of the arc
1.2290 + /// Returns the source node of the given arc.
1.2291 Value operator[](const Key& arc) const {
1.2292 - return _digraph.source(arc);
1.2293 + return _graph.source(arc);
1.2294 }
1.2295
1.2296 private:
1.2297 - const Digraph& _digraph;
1.2298 + const GR& _graph;
1.2299 };
1.2300
1.2301 /// \brief Returns a \c SourceMap class.
1.2302 ///
1.2303 /// This function just returns an \c SourceMap class.
1.2304 /// \relates SourceMap
1.2305 - template <typename Digraph>
1.2306 - inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
1.2307 - return SourceMap<Digraph>(digraph);
1.2308 + template <typename GR>
1.2309 + inline SourceMap<GR> sourceMap(const GR& graph) {
1.2310 + return SourceMap<GR>(graph);
1.2311 }
1.2312
1.2313 - /// \brief Returns the target of the given arc.
1.2314 + /// \brief Map of the target nodes of arcs in a digraph.
1.2315 ///
1.2316 - /// The TargetMap gives back the target Node of the given arc.
1.2317 + /// TargetMap provides access for the target node of each arc in a digraph,
1.2318 + /// which is returned by the \c target() function of the digraph.
1.2319 + /// \tparam GR The digraph type.
1.2320 /// \see SourceMap
1.2321 - template <typename Digraph>
1.2322 + template <typename GR>
1.2323 class TargetMap {
1.2324 public:
1.2325
1.2326 - typedef typename Digraph::Node Value;
1.2327 - typedef typename Digraph::Arc Key;
1.2328 + /// The key type (the \c Arc type of the digraph).
1.2329 + typedef typename GR::Arc Key;
1.2330 + /// The value type (the \c Node type of the digraph).
1.2331 + typedef typename GR::Node Value;
1.2332
1.2333 /// \brief Constructor
1.2334 ///
1.2335 - /// Constructor
1.2336 + /// Constructor.
1.2337 /// \param digraph The digraph that the map belongs to.
1.2338 - explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
1.2339 -
1.2340 - /// \brief The subscript operator.
1.2341 + explicit TargetMap(const GR& digraph) : _graph(digraph) {}
1.2342 +
1.2343 + /// \brief Returns the target node of the given arc.
1.2344 ///
1.2345 - /// The subscript operator.
1.2346 - /// \param e The arc
1.2347 - /// \return The target of the arc
1.2348 + /// Returns the target node of the given arc.
1.2349 Value operator[](const Key& e) const {
1.2350 - return _digraph.target(e);
1.2351 + return _graph.target(e);
1.2352 }
1.2353
1.2354 private:
1.2355 - const Digraph& _digraph;
1.2356 + const GR& _graph;
1.2357 };
1.2358
1.2359 /// \brief Returns a \c TargetMap class.
1.2360 ///
1.2361 /// This function just returns a \c TargetMap class.
1.2362 /// \relates TargetMap
1.2363 - template <typename Digraph>
1.2364 - inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
1.2365 - return TargetMap<Digraph>(digraph);
1.2366 + template <typename GR>
1.2367 + inline TargetMap<GR> targetMap(const GR& graph) {
1.2368 + return TargetMap<GR>(graph);
1.2369 }
1.2370
1.2371 - /// \brief Returns the "forward" directed arc view of an edge.
1.2372 + /// \brief Map of the "forward" directed arc view of edges in a graph.
1.2373 ///
1.2374 - /// Returns the "forward" directed arc view of an edge.
1.2375 + /// ForwardMap provides access for the "forward" directed arc view of
1.2376 + /// each edge in a graph, which is returned by the \c direct() function
1.2377 + /// of the graph with \c true parameter.
1.2378 + /// \tparam GR The graph type.
1.2379 /// \see BackwardMap
1.2380 - template <typename Graph>
1.2381 + template <typename GR>
1.2382 class ForwardMap {
1.2383 public:
1.2384
1.2385 - typedef typename Graph::Arc Value;
1.2386 - typedef typename Graph::Edge Key;
1.2387 + /// The key type (the \c Edge type of the digraph).
1.2388 + typedef typename GR::Edge Key;
1.2389 + /// The value type (the \c Arc type of the digraph).
1.2390 + typedef typename GR::Arc Value;
1.2391
1.2392 /// \brief Constructor
1.2393 ///
1.2394 - /// Constructor
1.2395 + /// Constructor.
1.2396 /// \param graph The graph that the map belongs to.
1.2397 - explicit ForwardMap(const Graph& graph) : _graph(graph) {}
1.2398 -
1.2399 - /// \brief The subscript operator.
1.2400 + explicit ForwardMap(const GR& graph) : _graph(graph) {}
1.2401 +
1.2402 + /// \brief Returns the "forward" directed arc view of the given edge.
1.2403 ///
1.2404 - /// The subscript operator.
1.2405 - /// \param key An edge
1.2406 - /// \return The "forward" directed arc view of edge
1.2407 + /// Returns the "forward" directed arc view of the given edge.
1.2408 Value operator[](const Key& key) const {
1.2409 return _graph.direct(key, true);
1.2410 }
1.2411
1.2412 private:
1.2413 - const Graph& _graph;
1.2414 + const GR& _graph;
1.2415 };
1.2416
1.2417 /// \brief Returns a \c ForwardMap class.
1.2418 ///
1.2419 /// This function just returns an \c ForwardMap class.
1.2420 /// \relates ForwardMap
1.2421 - template <typename Graph>
1.2422 - inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1.2423 - return ForwardMap<Graph>(graph);
1.2424 + template <typename GR>
1.2425 + inline ForwardMap<GR> forwardMap(const GR& graph) {
1.2426 + return ForwardMap<GR>(graph);
1.2427 }
1.2428
1.2429 - /// \brief Returns the "backward" directed arc view of an edge.
1.2430 + /// \brief Map of the "backward" directed arc view of edges in a graph.
1.2431 ///
1.2432 - /// Returns the "backward" directed arc view of an edge.
1.2433 + /// BackwardMap provides access for the "backward" directed arc view of
1.2434 + /// each edge in a graph, which is returned by the \c direct() function
1.2435 + /// of the graph with \c false parameter.
1.2436 + /// \tparam GR The graph type.
1.2437 /// \see ForwardMap
1.2438 - template <typename Graph>
1.2439 + template <typename GR>
1.2440 class BackwardMap {
1.2441 public:
1.2442
1.2443 - typedef typename Graph::Arc Value;
1.2444 - typedef typename Graph::Edge Key;
1.2445 + /// The key type (the \c Edge type of the digraph).
1.2446 + typedef typename GR::Edge Key;
1.2447 + /// The value type (the \c Arc type of the digraph).
1.2448 + typedef typename GR::Arc Value;
1.2449
1.2450 /// \brief Constructor
1.2451 ///
1.2452 - /// Constructor
1.2453 + /// Constructor.
1.2454 /// \param graph The graph that the map belongs to.
1.2455 - explicit BackwardMap(const Graph& graph) : _graph(graph) {}
1.2456 -
1.2457 - /// \brief The subscript operator.
1.2458 + explicit BackwardMap(const GR& graph) : _graph(graph) {}
1.2459 +
1.2460 + /// \brief Returns the "backward" directed arc view of the given edge.
1.2461 ///
1.2462 - /// The subscript operator.
1.2463 - /// \param key An edge
1.2464 - /// \return The "backward" directed arc view of edge
1.2465 + /// Returns the "backward" directed arc view of the given edge.
1.2466 Value operator[](const Key& key) const {
1.2467 return _graph.direct(key, false);
1.2468 }
1.2469
1.2470 private:
1.2471 - const Graph& _graph;
1.2472 + const GR& _graph;
1.2473 };
1.2474
1.2475 /// \brief Returns a \c BackwardMap class
1.2476
1.2477 /// This function just returns a \c BackwardMap class.
1.2478 /// \relates BackwardMap
1.2479 - template <typename Graph>
1.2480 - inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1.2481 - return BackwardMap<Graph>(graph);
1.2482 + template <typename GR>
1.2483 + inline BackwardMap<GR> backwardMap(const GR& graph) {
1.2484 + return BackwardMap<GR>(graph);
1.2485 }
1.2486
1.2487 - /// \brief Potential difference map
1.2488 - ///
1.2489 - /// If there is an potential map on the nodes then we
1.2490 - /// can get an arc map as we get the substraction of the
1.2491 - /// values of the target and source.
1.2492 - template <typename Digraph, typename NodeMap>
1.2493 - class PotentialDifferenceMap {
1.2494 - public:
1.2495 - typedef typename Digraph::Arc Key;
1.2496 - typedef typename NodeMap::Value Value;
1.2497 -
1.2498 - /// \brief Constructor
1.2499 - ///
1.2500 - /// Contructor of the map
1.2501 - explicit PotentialDifferenceMap(const Digraph& digraph,
1.2502 - const NodeMap& potential)
1.2503 - : _digraph(digraph), _potential(potential) {}
1.2504 -
1.2505 - /// \brief Const subscription operator
1.2506 - ///
1.2507 - /// Const subscription operator
1.2508 - Value operator[](const Key& arc) const {
1.2509 - return _potential[_digraph.target(arc)] -
1.2510 - _potential[_digraph.source(arc)];
1.2511 - }
1.2512 -
1.2513 - private:
1.2514 - const Digraph& _digraph;
1.2515 - const NodeMap& _potential;
1.2516 - };
1.2517 -
1.2518 - /// \brief Returns a PotentialDifferenceMap.
1.2519 - ///
1.2520 - /// This function just returns a PotentialDifferenceMap.
1.2521 - /// \relates PotentialDifferenceMap
1.2522 - template <typename Digraph, typename NodeMap>
1.2523 - PotentialDifferenceMap<Digraph, NodeMap>
1.2524 - potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
1.2525 - return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
1.2526 - }
1.2527 -
1.2528 - /// \brief Map of the node in-degrees.
1.2529 + /// \brief Map of the in-degrees of nodes in a digraph.
1.2530 ///
1.2531 /// This map returns the in-degree of a node. Once it is constructed,
1.2532 - /// the degrees are stored in a standard NodeMap, so each query is done
1.2533 + /// the degrees are stored in a standard \c NodeMap, so each query is done
1.2534 /// in constant time. On the other hand, the values are updated automatically
1.2535 /// whenever the digraph changes.
1.2536 ///
1.2537 - /// \warning Besides addNode() and addArc(), a digraph structure may provide
1.2538 - /// alternative ways to modify the digraph. The correct behavior of InDegMap
1.2539 - /// is not guarantied if these additional features are used. For example
1.2540 - /// the functions \ref ListDigraph::changeSource() "changeSource()",
1.2541 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.2542 + /// may provide alternative ways to modify the digraph.
1.2543 + /// The correct behavior of InDegMap is not guarantied if these additional
1.2544 + /// features are used. For example the functions
1.2545 + /// \ref ListDigraph::changeSource() "changeSource()",
1.2546 /// \ref ListDigraph::changeTarget() "changeTarget()" and
1.2547 /// \ref ListDigraph::reverseArc() "reverseArc()"
1.2548 /// of \ref ListDigraph will \e not update the degree values correctly.
1.2549 ///
1.2550 /// \sa OutDegMap
1.2551 -
1.2552 - template <typename _Digraph>
1.2553 + template <typename GR>
1.2554 class InDegMap
1.2555 - : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
1.2556 + : protected ItemSetTraits<GR, typename GR::Arc>
1.2557 ::ItemNotifier::ObserverBase {
1.2558
1.2559 public:
1.2560
1.2561 - typedef _Digraph Digraph;
1.2562 + /// The graph type of InDegMap
1.2563 + typedef GR Graph;
1.2564 + typedef GR Digraph;
1.2565 + /// The key type
1.2566 + typedef typename Digraph::Node Key;
1.2567 + /// The value type
1.2568 typedef int Value;
1.2569 - typedef typename Digraph::Node Key;
1.2570
1.2571 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
1.2572 ::ItemNotifier::ObserverBase Parent;
1.2573 @@ -2523,9 +3528,9 @@
1.2574
1.2575 /// \brief Constructor.
1.2576 ///
1.2577 - /// Constructor for creating in-degree map.
1.2578 - explicit InDegMap(const Digraph& digraph)
1.2579 - : _digraph(digraph), _deg(digraph) {
1.2580 + /// Constructor for creating an in-degree map.
1.2581 + explicit InDegMap(const Digraph& graph)
1.2582 + : _digraph(graph), _deg(graph) {
1.2583 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
1.2584
1.2585 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1.2586 @@ -2533,6 +3538,8 @@
1.2587 }
1.2588 }
1.2589
1.2590 + /// \brief Gives back the in-degree of a Node.
1.2591 + ///
1.2592 /// Gives back the in-degree of a Node.
1.2593 int operator[](const Key& key) const {
1.2594 return _deg[key];
1.2595 @@ -2579,33 +3586,37 @@
1.2596 AutoNodeMap _deg;
1.2597 };
1.2598
1.2599 - /// \brief Map of the node out-degrees.
1.2600 + /// \brief Map of the out-degrees of nodes in a digraph.
1.2601 ///
1.2602 /// This map returns the out-degree of a node. Once it is constructed,
1.2603 - /// the degrees are stored in a standard NodeMap, so each query is done
1.2604 + /// the degrees are stored in a standard \c NodeMap, so each query is done
1.2605 /// in constant time. On the other hand, the values are updated automatically
1.2606 /// whenever the digraph changes.
1.2607 ///
1.2608 - /// \warning Besides addNode() and addArc(), a digraph structure may provide
1.2609 - /// alternative ways to modify the digraph. The correct behavior of OutDegMap
1.2610 - /// is not guarantied if these additional features are used. For example
1.2611 - /// the functions \ref ListDigraph::changeSource() "changeSource()",
1.2612 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.2613 + /// may provide alternative ways to modify the digraph.
1.2614 + /// The correct behavior of OutDegMap is not guarantied if these additional
1.2615 + /// features are used. For example the functions
1.2616 + /// \ref ListDigraph::changeSource() "changeSource()",
1.2617 /// \ref ListDigraph::changeTarget() "changeTarget()" and
1.2618 /// \ref ListDigraph::reverseArc() "reverseArc()"
1.2619 /// of \ref ListDigraph will \e not update the degree values correctly.
1.2620 ///
1.2621 /// \sa InDegMap
1.2622 -
1.2623 - template <typename _Digraph>
1.2624 + template <typename GR>
1.2625 class OutDegMap
1.2626 - : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
1.2627 + : protected ItemSetTraits<GR, typename GR::Arc>
1.2628 ::ItemNotifier::ObserverBase {
1.2629
1.2630 public:
1.2631
1.2632 - typedef _Digraph Digraph;
1.2633 + /// The graph type of OutDegMap
1.2634 + typedef GR Graph;
1.2635 + typedef GR Digraph;
1.2636 + /// The key type
1.2637 + typedef typename Digraph::Node Key;
1.2638 + /// The value type
1.2639 typedef int Value;
1.2640 - typedef typename Digraph::Node Key;
1.2641
1.2642 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
1.2643 ::ItemNotifier::ObserverBase Parent;
1.2644 @@ -2645,9 +3656,9 @@
1.2645
1.2646 /// \brief Constructor.
1.2647 ///
1.2648 - /// Constructor for creating out-degree map.
1.2649 - explicit OutDegMap(const Digraph& digraph)
1.2650 - : _digraph(digraph), _deg(digraph) {
1.2651 + /// Constructor for creating an out-degree map.
1.2652 + explicit OutDegMap(const Digraph& graph)
1.2653 + : _digraph(graph), _deg(graph) {
1.2654 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
1.2655
1.2656 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1.2657 @@ -2655,6 +3666,8 @@
1.2658 }
1.2659 }
1.2660
1.2661 + /// \brief Gives back the out-degree of a Node.
1.2662 + ///
1.2663 /// Gives back the out-degree of a Node.
1.2664 int operator[](const Key& key) const {
1.2665 return _deg[key];
1.2666 @@ -2701,6 +3714,56 @@
1.2667 AutoNodeMap _deg;
1.2668 };
1.2669
1.2670 + /// \brief Potential difference map
1.2671 + ///
1.2672 + /// PotentialDifferenceMap returns the difference between the potentials of
1.2673 + /// the source and target nodes of each arc in a digraph, i.e. it returns
1.2674 + /// \code
1.2675 + /// potential[gr.target(arc)] - potential[gr.source(arc)].
1.2676 + /// \endcode
1.2677 + /// \tparam GR The digraph type.
1.2678 + /// \tparam POT A node map storing the potentials.
1.2679 + template <typename GR, typename POT>
1.2680 + class PotentialDifferenceMap {
1.2681 + public:
1.2682 + /// Key type
1.2683 + typedef typename GR::Arc Key;
1.2684 + /// Value type
1.2685 + typedef typename POT::Value Value;
1.2686 +
1.2687 + /// \brief Constructor
1.2688 + ///
1.2689 + /// Contructor of the map.
1.2690 + explicit PotentialDifferenceMap(const GR& gr,
1.2691 + const POT& potential)
1.2692 + : _digraph(gr), _potential(potential) {}
1.2693 +
1.2694 + /// \brief Returns the potential difference for the given arc.
1.2695 + ///
1.2696 + /// Returns the potential difference for the given arc, i.e.
1.2697 + /// \code
1.2698 + /// potential[gr.target(arc)] - potential[gr.source(arc)].
1.2699 + /// \endcode
1.2700 + Value operator[](const Key& arc) const {
1.2701 + return _potential[_digraph.target(arc)] -
1.2702 + _potential[_digraph.source(arc)];
1.2703 + }
1.2704 +
1.2705 + private:
1.2706 + const GR& _digraph;
1.2707 + const POT& _potential;
1.2708 + };
1.2709 +
1.2710 + /// \brief Returns a PotentialDifferenceMap.
1.2711 + ///
1.2712 + /// This function just returns a PotentialDifferenceMap.
1.2713 + /// \relates PotentialDifferenceMap
1.2714 + template <typename GR, typename POT>
1.2715 + PotentialDifferenceMap<GR, POT>
1.2716 + potentialDifferenceMap(const GR& gr, const POT& potential) {
1.2717 + return PotentialDifferenceMap<GR, POT>(gr, potential);
1.2718 + }
1.2719 +
1.2720 /// @}
1.2721 }
1.2722