1.1 --- a/lemon/maps.h Sun Aug 02 12:40:20 2009 +0200
1.2 +++ b/lemon/maps.h Fri Sep 25 09:13:03 2009 +0200
1.3 @@ -22,6 +22,7 @@
1.4 #include <iterator>
1.5 #include <functional>
1.6 #include <vector>
1.7 +#include <map>
1.8
1.9 #include <lemon/core.h>
1.10
1.11 @@ -29,8 +30,6 @@
1.12 ///\ingroup maps
1.13 ///\brief Miscellaneous property maps
1.14
1.15 -#include <map>
1.16 -
1.17 namespace lemon {
1.18
1.19 /// \addtogroup maps
1.20 @@ -1818,7 +1817,7 @@
1.21 /// \brief Provides an immutable and unique id for each item in a graph.
1.22 ///
1.23 /// IdMap provides a unique and immutable id for each item of the
1.24 - /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1.25 + /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1.26 /// - \b unique: different items get different ids,
1.27 /// - \b immutable: the id of an item does not change (even if you
1.28 /// delete other nodes).
1.29 @@ -1902,13 +1901,14 @@
1.30 /// \brief General cross reference graph map type.
1.31
1.32 /// This class provides simple invertable graph maps.
1.33 - /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
1.34 - /// and if a key is set to a new value then store it
1.35 - /// in the inverse map.
1.36 - ///
1.37 + /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1.38 + /// and if a key is set to a new value, then stores it in the inverse map.
1.39 /// The values of the map can be accessed
1.40 /// with stl compatible forward iterator.
1.41 ///
1.42 + /// This type is not reference map, so it cannot be modified with
1.43 + /// the subscript operator.
1.44 + ///
1.45 /// \tparam GR The graph type.
1.46 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.47 /// \c GR::Edge).
1.48 @@ -1923,7 +1923,7 @@
1.49 typedef typename ItemSetTraits<GR, K>::
1.50 template Map<V>::Type Map;
1.51
1.52 - typedef std::map<V, K> Container;
1.53 + typedef std::multimap<V, K> Container;
1.54 Container _inv_map;
1.55
1.56 public:
1.57 @@ -1948,6 +1948,8 @@
1.58 /// This iterator is an stl compatible forward
1.59 /// iterator on the values of the map. The values can
1.60 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.61 + /// They are considered with multiplicity, so each value is
1.62 + /// traversed for each item it is assigned to.
1.63 class ValueIterator
1.64 : public std::iterator<std::forward_iterator_tag, Value> {
1.65 friend class CrossRefMap;
1.66 @@ -2000,11 +2002,15 @@
1.67 /// Sets the value associated with the given key.
1.68 void set(const Key& key, const Value& val) {
1.69 Value oldval = Map::operator[](key);
1.70 - typename Container::iterator it = _inv_map.find(oldval);
1.71 - if (it != _inv_map.end() && it->second == key) {
1.72 - _inv_map.erase(it);
1.73 + typename Container::iterator it;
1.74 + for (it = _inv_map.equal_range(oldval).first;
1.75 + it != _inv_map.equal_range(oldval).second; ++it) {
1.76 + if (it->second == key) {
1.77 + _inv_map.erase(it);
1.78 + break;
1.79 + }
1.80 }
1.81 - _inv_map.insert(make_pair(val, key));
1.82 + _inv_map.insert(std::make_pair(val, key));
1.83 Map::set(key, val);
1.84 }
1.85
1.86 @@ -2016,11 +2022,14 @@
1.87 return Map::operator[](key);
1.88 }
1.89
1.90 - /// \brief Gives back the item by its value.
1.91 + /// \brief Gives back an item by its value.
1.92 ///
1.93 - /// Gives back the item by its value.
1.94 - Key operator()(const Value& key) const {
1.95 - typename Container::const_iterator it = _inv_map.find(key);
1.96 + /// This function gives back an item that is assigned to
1.97 + /// the given value or \c INVALID if no such item exists.
1.98 + /// If there are more items with the same associated value,
1.99 + /// only one of them is returned.
1.100 + Key operator()(const Value& val) const {
1.101 + typename Container::const_iterator it = _inv_map.find(val);
1.102 return it != _inv_map.end() ? it->second : INVALID;
1.103 }
1.104
1.105 @@ -2032,9 +2041,13 @@
1.106 /// \c AlterationNotifier.
1.107 virtual void erase(const Key& key) {
1.108 Value val = Map::operator[](key);
1.109 - typename Container::iterator it = _inv_map.find(val);
1.110 - if (it != _inv_map.end() && it->second == key) {
1.111 - _inv_map.erase(it);
1.112 + typename Container::iterator it;
1.113 + for (it = _inv_map.equal_range(val).first;
1.114 + it != _inv_map.equal_range(val).second; ++it) {
1.115 + if (it->second == key) {
1.116 + _inv_map.erase(it);
1.117 + break;
1.118 + }
1.119 }
1.120 Map::erase(key);
1.121 }
1.122 @@ -2046,9 +2059,13 @@
1.123 virtual void erase(const std::vector<Key>& keys) {
1.124 for (int i = 0; i < int(keys.size()); ++i) {
1.125 Value val = Map::operator[](keys[i]);
1.126 - typename Container::iterator it = _inv_map.find(val);
1.127 - if (it != _inv_map.end() && it->second == keys[i]) {
1.128 - _inv_map.erase(it);
1.129 + typename Container::iterator it;
1.130 + for (it = _inv_map.equal_range(val).first;
1.131 + it != _inv_map.equal_range(val).second; ++it) {
1.132 + if (it->second == keys[i]) {
1.133 + _inv_map.erase(it);
1.134 + break;
1.135 + }
1.136 }
1.137 }
1.138 Map::erase(keys);
1.139 @@ -2084,8 +2101,9 @@
1.140
1.141 /// \brief Subscript operator.
1.142 ///
1.143 - /// Subscript operator. It gives back the item
1.144 - /// that was last assigned to the given value.
1.145 + /// Subscript operator. It gives back an item
1.146 + /// that is assigned to the given value or \c INVALID
1.147 + /// if no such item exists.
1.148 Value operator[](const Key& key) const {
1.149 return _inverted(key);
1.150 }
1.151 @@ -2254,7 +2272,7 @@
1.152 }
1.153
1.154 /// \brief Gives back the item belonging to a \e RangeId
1.155 - ///
1.156 + ///
1.157 /// Gives back the item belonging to a \e RangeId.
1.158 Item operator()(int id) const {
1.159 return _inv_map[id];
1.160 @@ -2311,6 +2329,903 @@
1.161 }
1.162 };
1.163
1.164 + /// \brief Dynamic iterable \c bool map.
1.165 + ///
1.166 + /// This class provides a special graph map type which can store a
1.167 + /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
1.168 + /// For both \c true and \c false values it is possible to iterate on
1.169 + /// the keys.
1.170 + ///
1.171 + /// This type is a reference map, so it can be modified with the
1.172 + /// subscript operator.
1.173 + ///
1.174 + /// \tparam GR The graph type.
1.175 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.176 + /// \c GR::Edge).
1.177 + ///
1.178 + /// \see IterableIntMap, IterableValueMap
1.179 + /// \see CrossRefMap
1.180 + template <typename GR, typename K>
1.181 + class IterableBoolMap
1.182 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
1.183 + private:
1.184 + typedef GR Graph;
1.185 +
1.186 + typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
1.187 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
1.188 +
1.189 + std::vector<K> _array;
1.190 + int _sep;
1.191 +
1.192 + public:
1.193 +
1.194 + /// Indicates that the map is reference map.
1.195 + typedef True ReferenceMapTag;
1.196 +
1.197 + /// The key type
1.198 + typedef K Key;
1.199 + /// The value type
1.200 + typedef bool Value;
1.201 + /// The const reference type.
1.202 + typedef const Value& ConstReference;
1.203 +
1.204 + private:
1.205 +
1.206 + int position(const Key& key) const {
1.207 + return Parent::operator[](key);
1.208 + }
1.209 +
1.210 + public:
1.211 +
1.212 + /// \brief Reference to the value of the map.
1.213 + ///
1.214 + /// This class is similar to the \c bool type. It can be converted to
1.215 + /// \c bool and it provides the same operators.
1.216 + class Reference {
1.217 + friend class IterableBoolMap;
1.218 + private:
1.219 + Reference(IterableBoolMap& map, const Key& key)
1.220 + : _key(key), _map(map) {}
1.221 + public:
1.222 +
1.223 + Reference& operator=(const Reference& value) {
1.224 + _map.set(_key, static_cast<bool>(value));
1.225 + return *this;
1.226 + }
1.227 +
1.228 + operator bool() const {
1.229 + return static_cast<const IterableBoolMap&>(_map)[_key];
1.230 + }
1.231 +
1.232 + Reference& operator=(bool value) {
1.233 + _map.set(_key, value);
1.234 + return *this;
1.235 + }
1.236 + Reference& operator&=(bool value) {
1.237 + _map.set(_key, _map[_key] & value);
1.238 + return *this;
1.239 + }
1.240 + Reference& operator|=(bool value) {
1.241 + _map.set(_key, _map[_key] | value);
1.242 + return *this;
1.243 + }
1.244 + Reference& operator^=(bool value) {
1.245 + _map.set(_key, _map[_key] ^ value);
1.246 + return *this;
1.247 + }
1.248 + private:
1.249 + Key _key;
1.250 + IterableBoolMap& _map;
1.251 + };
1.252 +
1.253 + /// \brief Constructor of the map with a default value.
1.254 + ///
1.255 + /// Constructor of the map with a default value.
1.256 + explicit IterableBoolMap(const Graph& graph, bool def = false)
1.257 + : Parent(graph) {
1.258 + typename Parent::Notifier* nf = Parent::notifier();
1.259 + Key it;
1.260 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.261 + Parent::set(it, _array.size());
1.262 + _array.push_back(it);
1.263 + }
1.264 + _sep = (def ? _array.size() : 0);
1.265 + }
1.266 +
1.267 + /// \brief Const subscript operator of the map.
1.268 + ///
1.269 + /// Const subscript operator of the map.
1.270 + bool operator[](const Key& key) const {
1.271 + return position(key) < _sep;
1.272 + }
1.273 +
1.274 + /// \brief Subscript operator of the map.
1.275 + ///
1.276 + /// Subscript operator of the map.
1.277 + Reference operator[](const Key& key) {
1.278 + return Reference(*this, key);
1.279 + }
1.280 +
1.281 + /// \brief Set operation of the map.
1.282 + ///
1.283 + /// Set operation of the map.
1.284 + void set(const Key& key, bool value) {
1.285 + int pos = position(key);
1.286 + if (value) {
1.287 + if (pos < _sep) return;
1.288 + Key tmp = _array[_sep];
1.289 + _array[_sep] = key;
1.290 + Parent::set(key, _sep);
1.291 + _array[pos] = tmp;
1.292 + Parent::set(tmp, pos);
1.293 + ++_sep;
1.294 + } else {
1.295 + if (pos >= _sep) return;
1.296 + --_sep;
1.297 + Key tmp = _array[_sep];
1.298 + _array[_sep] = key;
1.299 + Parent::set(key, _sep);
1.300 + _array[pos] = tmp;
1.301 + Parent::set(tmp, pos);
1.302 + }
1.303 + }
1.304 +
1.305 + /// \brief Set all items.
1.306 + ///
1.307 + /// Set all items in the map.
1.308 + /// \note Constant time operation.
1.309 + void setAll(bool value) {
1.310 + _sep = (value ? _array.size() : 0);
1.311 + }
1.312 +
1.313 + /// \brief Returns the number of the keys mapped to \c true.
1.314 + ///
1.315 + /// Returns the number of the keys mapped to \c true.
1.316 + int trueNum() const {
1.317 + return _sep;
1.318 + }
1.319 +
1.320 + /// \brief Returns the number of the keys mapped to \c false.
1.321 + ///
1.322 + /// Returns the number of the keys mapped to \c false.
1.323 + int falseNum() const {
1.324 + return _array.size() - _sep;
1.325 + }
1.326 +
1.327 + /// \brief Iterator for the keys mapped to \c true.
1.328 + ///
1.329 + /// Iterator for the keys mapped to \c true. It works
1.330 + /// like a graph item iterator, it can be converted to
1.331 + /// the key type of the map, incremented with \c ++ operator, and
1.332 + /// if the iterator leaves the last valid key, it will be equal to
1.333 + /// \c INVALID.
1.334 + class TrueIt : public Key {
1.335 + public:
1.336 + typedef Key Parent;
1.337 +
1.338 + /// \brief Creates an iterator.
1.339 + ///
1.340 + /// Creates an iterator. It iterates on the
1.341 + /// keys mapped to \c true.
1.342 + /// \param map The IterableBoolMap.
1.343 + explicit TrueIt(const IterableBoolMap& map)
1.344 + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
1.345 + _map(&map) {}
1.346 +
1.347 + /// \brief Invalid constructor \& conversion.
1.348 + ///
1.349 + /// This constructor initializes the iterator to be invalid.
1.350 + /// \sa Invalid for more details.
1.351 + TrueIt(Invalid) : Parent(INVALID), _map(0) {}
1.352 +
1.353 + /// \brief Increment operator.
1.354 + ///
1.355 + /// Increment operator.
1.356 + TrueIt& operator++() {
1.357 + int pos = _map->position(*this);
1.358 + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
1.359 + return *this;
1.360 + }
1.361 +
1.362 + private:
1.363 + const IterableBoolMap* _map;
1.364 + };
1.365 +
1.366 + /// \brief Iterator for the keys mapped to \c false.
1.367 + ///
1.368 + /// Iterator for the keys mapped to \c false. It works
1.369 + /// like a graph item iterator, it can be converted to
1.370 + /// the key type of the map, incremented with \c ++ operator, and
1.371 + /// if the iterator leaves the last valid key, it will be equal to
1.372 + /// \c INVALID.
1.373 + class FalseIt : public Key {
1.374 + public:
1.375 + typedef Key Parent;
1.376 +
1.377 + /// \brief Creates an iterator.
1.378 + ///
1.379 + /// Creates an iterator. It iterates on the
1.380 + /// keys mapped to \c false.
1.381 + /// \param map The IterableBoolMap.
1.382 + explicit FalseIt(const IterableBoolMap& map)
1.383 + : Parent(map._sep < int(map._array.size()) ?
1.384 + map._array.back() : INVALID), _map(&map) {}
1.385 +
1.386 + /// \brief Invalid constructor \& conversion.
1.387 + ///
1.388 + /// This constructor initializes the iterator to be invalid.
1.389 + /// \sa Invalid for more details.
1.390 + FalseIt(Invalid) : Parent(INVALID), _map(0) {}
1.391 +
1.392 + /// \brief Increment operator.
1.393 + ///
1.394 + /// Increment operator.
1.395 + FalseIt& operator++() {
1.396 + int pos = _map->position(*this);
1.397 + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
1.398 + return *this;
1.399 + }
1.400 +
1.401 + private:
1.402 + const IterableBoolMap* _map;
1.403 + };
1.404 +
1.405 + /// \brief Iterator for the keys mapped to a given value.
1.406 + ///
1.407 + /// Iterator for the keys mapped to a given value. It works
1.408 + /// like a graph item iterator, it can be converted to
1.409 + /// the key type of the map, incremented with \c ++ operator, and
1.410 + /// if the iterator leaves the last valid key, it will be equal to
1.411 + /// \c INVALID.
1.412 + class ItemIt : public Key {
1.413 + public:
1.414 + typedef Key Parent;
1.415 +
1.416 + /// \brief Creates an iterator with a value.
1.417 + ///
1.418 + /// Creates an iterator with a value. It iterates on the
1.419 + /// keys mapped to the given value.
1.420 + /// \param map The IterableBoolMap.
1.421 + /// \param value The value.
1.422 + ItemIt(const IterableBoolMap& map, bool value)
1.423 + : Parent(value ?
1.424 + (map._sep > 0 ?
1.425 + map._array[map._sep - 1] : INVALID) :
1.426 + (map._sep < int(map._array.size()) ?
1.427 + map._array.back() : INVALID)), _map(&map) {}
1.428 +
1.429 + /// \brief Invalid constructor \& conversion.
1.430 + ///
1.431 + /// This constructor initializes the iterator to be invalid.
1.432 + /// \sa Invalid for more details.
1.433 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.434 +
1.435 + /// \brief Increment operator.
1.436 + ///
1.437 + /// Increment operator.
1.438 + ItemIt& operator++() {
1.439 + int pos = _map->position(*this);
1.440 + int _sep = pos >= _map->_sep ? _map->_sep : 0;
1.441 + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
1.442 + return *this;
1.443 + }
1.444 +
1.445 + private:
1.446 + const IterableBoolMap* _map;
1.447 + };
1.448 +
1.449 + protected:
1.450 +
1.451 + virtual void add(const Key& key) {
1.452 + Parent::add(key);
1.453 + Parent::set(key, _array.size());
1.454 + _array.push_back(key);
1.455 + }
1.456 +
1.457 + virtual void add(const std::vector<Key>& keys) {
1.458 + Parent::add(keys);
1.459 + for (int i = 0; i < int(keys.size()); ++i) {
1.460 + Parent::set(keys[i], _array.size());
1.461 + _array.push_back(keys[i]);
1.462 + }
1.463 + }
1.464 +
1.465 + virtual void erase(const Key& key) {
1.466 + int pos = position(key);
1.467 + if (pos < _sep) {
1.468 + --_sep;
1.469 + Parent::set(_array[_sep], pos);
1.470 + _array[pos] = _array[_sep];
1.471 + Parent::set(_array.back(), _sep);
1.472 + _array[_sep] = _array.back();
1.473 + _array.pop_back();
1.474 + } else {
1.475 + Parent::set(_array.back(), pos);
1.476 + _array[pos] = _array.back();
1.477 + _array.pop_back();
1.478 + }
1.479 + Parent::erase(key);
1.480 + }
1.481 +
1.482 + virtual void erase(const std::vector<Key>& keys) {
1.483 + for (int i = 0; i < int(keys.size()); ++i) {
1.484 + int pos = position(keys[i]);
1.485 + if (pos < _sep) {
1.486 + --_sep;
1.487 + Parent::set(_array[_sep], pos);
1.488 + _array[pos] = _array[_sep];
1.489 + Parent::set(_array.back(), _sep);
1.490 + _array[_sep] = _array.back();
1.491 + _array.pop_back();
1.492 + } else {
1.493 + Parent::set(_array.back(), pos);
1.494 + _array[pos] = _array.back();
1.495 + _array.pop_back();
1.496 + }
1.497 + }
1.498 + Parent::erase(keys);
1.499 + }
1.500 +
1.501 + virtual void build() {
1.502 + Parent::build();
1.503 + typename Parent::Notifier* nf = Parent::notifier();
1.504 + Key it;
1.505 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.506 + Parent::set(it, _array.size());
1.507 + _array.push_back(it);
1.508 + }
1.509 + _sep = 0;
1.510 + }
1.511 +
1.512 + virtual void clear() {
1.513 + _array.clear();
1.514 + _sep = 0;
1.515 + Parent::clear();
1.516 + }
1.517 +
1.518 + };
1.519 +
1.520 +
1.521 + namespace _maps_bits {
1.522 + template <typename Item>
1.523 + struct IterableIntMapNode {
1.524 + IterableIntMapNode() : value(-1) {}
1.525 + IterableIntMapNode(int _value) : value(_value) {}
1.526 + Item prev, next;
1.527 + int value;
1.528 + };
1.529 + }
1.530 +
1.531 + /// \brief Dynamic iterable integer map.
1.532 + ///
1.533 + /// This class provides a special graph map type which can store an
1.534 + /// integer value for graph items (\c Node, \c Arc or \c Edge).
1.535 + /// For each non-negative value it is possible to iterate on the keys
1.536 + /// mapped to the value.
1.537 + ///
1.538 + /// This type is a reference map, so it can be modified with the
1.539 + /// subscript operator.
1.540 + ///
1.541 + /// \note The size of the data structure depends on the largest
1.542 + /// value in the map.
1.543 + ///
1.544 + /// \tparam GR The graph type.
1.545 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.546 + /// \c GR::Edge).
1.547 + ///
1.548 + /// \see IterableBoolMap, IterableValueMap
1.549 + /// \see CrossRefMap
1.550 + template <typename GR, typename K>
1.551 + class IterableIntMap
1.552 + : protected ItemSetTraits<GR, K>::
1.553 + template Map<_maps_bits::IterableIntMapNode<K> >::Type {
1.554 + public:
1.555 + typedef typename ItemSetTraits<GR, K>::
1.556 + template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
1.557 +
1.558 + /// The key type
1.559 + typedef K Key;
1.560 + /// The value type
1.561 + typedef int Value;
1.562 + /// The graph type
1.563 + typedef GR Graph;
1.564 +
1.565 + /// \brief Constructor of the map.
1.566 + ///
1.567 + /// Constructor of the map. It sets all values to -1.
1.568 + explicit IterableIntMap(const Graph& graph)
1.569 + : Parent(graph) {}
1.570 +
1.571 + /// \brief Constructor of the map with a given value.
1.572 + ///
1.573 + /// Constructor of the map with a given value.
1.574 + explicit IterableIntMap(const Graph& graph, int value)
1.575 + : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
1.576 + if (value >= 0) {
1.577 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.578 + lace(it);
1.579 + }
1.580 + }
1.581 + }
1.582 +
1.583 + private:
1.584 +
1.585 + void unlace(const Key& key) {
1.586 + typename Parent::Value& node = Parent::operator[](key);
1.587 + if (node.value < 0) return;
1.588 + if (node.prev != INVALID) {
1.589 + Parent::operator[](node.prev).next = node.next;
1.590 + } else {
1.591 + _first[node.value] = node.next;
1.592 + }
1.593 + if (node.next != INVALID) {
1.594 + Parent::operator[](node.next).prev = node.prev;
1.595 + }
1.596 + while (!_first.empty() && _first.back() == INVALID) {
1.597 + _first.pop_back();
1.598 + }
1.599 + }
1.600 +
1.601 + void lace(const Key& key) {
1.602 + typename Parent::Value& node = Parent::operator[](key);
1.603 + if (node.value < 0) return;
1.604 + if (node.value >= int(_first.size())) {
1.605 + _first.resize(node.value + 1, INVALID);
1.606 + }
1.607 + node.prev = INVALID;
1.608 + node.next = _first[node.value];
1.609 + if (node.next != INVALID) {
1.610 + Parent::operator[](node.next).prev = key;
1.611 + }
1.612 + _first[node.value] = key;
1.613 + }
1.614 +
1.615 + public:
1.616 +
1.617 + /// Indicates that the map is reference map.
1.618 + typedef True ReferenceMapTag;
1.619 +
1.620 + /// \brief Reference to the value of the map.
1.621 + ///
1.622 + /// This class is similar to the \c int type. It can
1.623 + /// be converted to \c int and it has the same operators.
1.624 + class Reference {
1.625 + friend class IterableIntMap;
1.626 + private:
1.627 + Reference(IterableIntMap& map, const Key& key)
1.628 + : _key(key), _map(map) {}
1.629 + public:
1.630 +
1.631 + Reference& operator=(const Reference& value) {
1.632 + _map.set(_key, static_cast<const int&>(value));
1.633 + return *this;
1.634 + }
1.635 +
1.636 + operator const int&() const {
1.637 + return static_cast<const IterableIntMap&>(_map)[_key];
1.638 + }
1.639 +
1.640 + Reference& operator=(int value) {
1.641 + _map.set(_key, value);
1.642 + return *this;
1.643 + }
1.644 + Reference& operator++() {
1.645 + _map.set(_key, _map[_key] + 1);
1.646 + return *this;
1.647 + }
1.648 + int operator++(int) {
1.649 + int value = _map[_key];
1.650 + _map.set(_key, value + 1);
1.651 + return value;
1.652 + }
1.653 + Reference& operator--() {
1.654 + _map.set(_key, _map[_key] - 1);
1.655 + return *this;
1.656 + }
1.657 + int operator--(int) {
1.658 + int value = _map[_key];
1.659 + _map.set(_key, value - 1);
1.660 + return value;
1.661 + }
1.662 + Reference& operator+=(int value) {
1.663 + _map.set(_key, _map[_key] + value);
1.664 + return *this;
1.665 + }
1.666 + Reference& operator-=(int value) {
1.667 + _map.set(_key, _map[_key] - value);
1.668 + return *this;
1.669 + }
1.670 + Reference& operator*=(int value) {
1.671 + _map.set(_key, _map[_key] * value);
1.672 + return *this;
1.673 + }
1.674 + Reference& operator/=(int value) {
1.675 + _map.set(_key, _map[_key] / value);
1.676 + return *this;
1.677 + }
1.678 + Reference& operator%=(int value) {
1.679 + _map.set(_key, _map[_key] % value);
1.680 + return *this;
1.681 + }
1.682 + Reference& operator&=(int value) {
1.683 + _map.set(_key, _map[_key] & value);
1.684 + return *this;
1.685 + }
1.686 + Reference& operator|=(int value) {
1.687 + _map.set(_key, _map[_key] | value);
1.688 + return *this;
1.689 + }
1.690 + Reference& operator^=(int value) {
1.691 + _map.set(_key, _map[_key] ^ value);
1.692 + return *this;
1.693 + }
1.694 + Reference& operator<<=(int value) {
1.695 + _map.set(_key, _map[_key] << value);
1.696 + return *this;
1.697 + }
1.698 + Reference& operator>>=(int value) {
1.699 + _map.set(_key, _map[_key] >> value);
1.700 + return *this;
1.701 + }
1.702 +
1.703 + private:
1.704 + Key _key;
1.705 + IterableIntMap& _map;
1.706 + };
1.707 +
1.708 + /// The const reference type.
1.709 + typedef const Value& ConstReference;
1.710 +
1.711 + /// \brief Gives back the maximal value plus one.
1.712 + ///
1.713 + /// Gives back the maximal value plus one.
1.714 + int size() const {
1.715 + return _first.size();
1.716 + }
1.717 +
1.718 + /// \brief Set operation of the map.
1.719 + ///
1.720 + /// Set operation of the map.
1.721 + void set(const Key& key, const Value& value) {
1.722 + unlace(key);
1.723 + Parent::operator[](key).value = value;
1.724 + lace(key);
1.725 + }
1.726 +
1.727 + /// \brief Const subscript operator of the map.
1.728 + ///
1.729 + /// Const subscript operator of the map.
1.730 + const Value& operator[](const Key& key) const {
1.731 + return Parent::operator[](key).value;
1.732 + }
1.733 +
1.734 + /// \brief Subscript operator of the map.
1.735 + ///
1.736 + /// Subscript operator of the map.
1.737 + Reference operator[](const Key& key) {
1.738 + return Reference(*this, key);
1.739 + }
1.740 +
1.741 + /// \brief Iterator for the keys with the same value.
1.742 + ///
1.743 + /// Iterator for the keys with the same value. It works
1.744 + /// like a graph item iterator, it can be converted to
1.745 + /// the item type of the map, incremented with \c ++ operator, and
1.746 + /// if the iterator leaves the last valid item, it will be equal to
1.747 + /// \c INVALID.
1.748 + class ItemIt : public Key {
1.749 + public:
1.750 + typedef Key Parent;
1.751 +
1.752 + /// \brief Invalid constructor \& conversion.
1.753 + ///
1.754 + /// This constructor initializes the iterator to be invalid.
1.755 + /// \sa Invalid for more details.
1.756 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.757 +
1.758 + /// \brief Creates an iterator with a value.
1.759 + ///
1.760 + /// Creates an iterator with a value. It iterates on the
1.761 + /// keys mapped to the given value.
1.762 + /// \param map The IterableIntMap.
1.763 + /// \param value The value.
1.764 + ItemIt(const IterableIntMap& map, int value) : _map(&map) {
1.765 + if (value < 0 || value >= int(_map->_first.size())) {
1.766 + Parent::operator=(INVALID);
1.767 + } else {
1.768 + Parent::operator=(_map->_first[value]);
1.769 + }
1.770 + }
1.771 +
1.772 + /// \brief Increment operator.
1.773 + ///
1.774 + /// Increment operator.
1.775 + ItemIt& operator++() {
1.776 + Parent::operator=(_map->IterableIntMap::Parent::
1.777 + operator[](static_cast<Parent&>(*this)).next);
1.778 + return *this;
1.779 + }
1.780 +
1.781 + private:
1.782 + const IterableIntMap* _map;
1.783 + };
1.784 +
1.785 + protected:
1.786 +
1.787 + virtual void erase(const Key& key) {
1.788 + unlace(key);
1.789 + Parent::erase(key);
1.790 + }
1.791 +
1.792 + virtual void erase(const std::vector<Key>& keys) {
1.793 + for (int i = 0; i < int(keys.size()); ++i) {
1.794 + unlace(keys[i]);
1.795 + }
1.796 + Parent::erase(keys);
1.797 + }
1.798 +
1.799 + virtual void clear() {
1.800 + _first.clear();
1.801 + Parent::clear();
1.802 + }
1.803 +
1.804 + private:
1.805 + std::vector<Key> _first;
1.806 + };
1.807 +
1.808 + namespace _maps_bits {
1.809 + template <typename Item, typename Value>
1.810 + struct IterableValueMapNode {
1.811 + IterableValueMapNode(Value _value = Value()) : value(_value) {}
1.812 + Item prev, next;
1.813 + Value value;
1.814 + };
1.815 + }
1.816 +
1.817 + /// \brief Dynamic iterable map for comparable values.
1.818 + ///
1.819 + /// This class provides a special graph map type which can store an
1.820 + /// comparable value for graph items (\c Node, \c Arc or \c Edge).
1.821 + /// For each value it is possible to iterate on the keys mapped to
1.822 + /// the value.
1.823 + ///
1.824 + /// The map stores for each value a linked list with
1.825 + /// the items which mapped to the value, and the values are stored
1.826 + /// in balanced binary tree. The values of the map can be accessed
1.827 + /// with stl compatible forward iterator.
1.828 + ///
1.829 + /// This type is not reference map, so it cannot be modified with
1.830 + /// the subscript operator.
1.831 + ///
1.832 + /// \tparam GR The graph type.
1.833 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.834 + /// \c GR::Edge).
1.835 + /// \tparam V The value type of the map. It can be any comparable
1.836 + /// value type.
1.837 + ///
1.838 + /// \see IterableBoolMap, IterableIntMap
1.839 + /// \see CrossRefMap
1.840 + template <typename GR, typename K, typename V>
1.841 + class IterableValueMap
1.842 + : protected ItemSetTraits<GR, K>::
1.843 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
1.844 + public:
1.845 + typedef typename ItemSetTraits<GR, K>::
1.846 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
1.847 +
1.848 + /// The key type
1.849 + typedef K Key;
1.850 + /// The value type
1.851 + typedef V Value;
1.852 + /// The graph type
1.853 + typedef GR Graph;
1.854 +
1.855 + public:
1.856 +
1.857 + /// \brief Constructor of the map with a given value.
1.858 + ///
1.859 + /// Constructor of the map with a given value.
1.860 + explicit IterableValueMap(const Graph& graph,
1.861 + const Value& value = Value())
1.862 + : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
1.863 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.864 + lace(it);
1.865 + }
1.866 + }
1.867 +
1.868 + protected:
1.869 +
1.870 + void unlace(const Key& key) {
1.871 + typename Parent::Value& node = Parent::operator[](key);
1.872 + if (node.prev != INVALID) {
1.873 + Parent::operator[](node.prev).next = node.next;
1.874 + } else {
1.875 + if (node.next != INVALID) {
1.876 + _first[node.value] = node.next;
1.877 + } else {
1.878 + _first.erase(node.value);
1.879 + }
1.880 + }
1.881 + if (node.next != INVALID) {
1.882 + Parent::operator[](node.next).prev = node.prev;
1.883 + }
1.884 + }
1.885 +
1.886 + void lace(const Key& key) {
1.887 + typename Parent::Value& node = Parent::operator[](key);
1.888 + typename std::map<Value, Key>::iterator it = _first.find(node.value);
1.889 + if (it == _first.end()) {
1.890 + node.prev = node.next = INVALID;
1.891 + _first.insert(std::make_pair(node.value, key));
1.892 + } else {
1.893 + node.prev = INVALID;
1.894 + node.next = it->second;
1.895 + if (node.next != INVALID) {
1.896 + Parent::operator[](node.next).prev = key;
1.897 + }
1.898 + it->second = key;
1.899 + }
1.900 + }
1.901 +
1.902 + public:
1.903 +
1.904 + /// \brief Forward iterator for values.
1.905 + ///
1.906 + /// This iterator is an stl compatible forward
1.907 + /// iterator on the values of the map. The values can
1.908 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.909 + class ValueIterator
1.910 + : public std::iterator<std::forward_iterator_tag, Value> {
1.911 + friend class IterableValueMap;
1.912 + private:
1.913 + ValueIterator(typename std::map<Value, Key>::const_iterator _it)
1.914 + : it(_it) {}
1.915 + public:
1.916 +
1.917 + ValueIterator() {}
1.918 +
1.919 + ValueIterator& operator++() { ++it; return *this; }
1.920 + ValueIterator operator++(int) {
1.921 + ValueIterator tmp(*this);
1.922 + operator++();
1.923 + return tmp;
1.924 + }
1.925 +
1.926 + const Value& operator*() const { return it->first; }
1.927 + const Value* operator->() const { return &(it->first); }
1.928 +
1.929 + bool operator==(ValueIterator jt) const { return it == jt.it; }
1.930 + bool operator!=(ValueIterator jt) const { return it != jt.it; }
1.931 +
1.932 + private:
1.933 + typename std::map<Value, Key>::const_iterator it;
1.934 + };
1.935 +
1.936 + /// \brief Returns an iterator to the first value.
1.937 + ///
1.938 + /// Returns an stl compatible iterator to the
1.939 + /// first value of the map. The values of the
1.940 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.941 + /// range.
1.942 + ValueIterator beginValue() const {
1.943 + return ValueIterator(_first.begin());
1.944 + }
1.945 +
1.946 + /// \brief Returns an iterator after the last value.
1.947 + ///
1.948 + /// Returns an stl compatible iterator after the
1.949 + /// last value of the map. The values of the
1.950 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.951 + /// range.
1.952 + ValueIterator endValue() const {
1.953 + return ValueIterator(_first.end());
1.954 + }
1.955 +
1.956 + /// \brief Set operation of the map.
1.957 + ///
1.958 + /// Set operation of the map.
1.959 + void set(const Key& key, const Value& value) {
1.960 + unlace(key);
1.961 + Parent::operator[](key).value = value;
1.962 + lace(key);
1.963 + }
1.964 +
1.965 + /// \brief Const subscript operator of the map.
1.966 + ///
1.967 + /// Const subscript operator of the map.
1.968 + const Value& operator[](const Key& key) const {
1.969 + return Parent::operator[](key).value;
1.970 + }
1.971 +
1.972 + /// \brief Iterator for the keys with the same value.
1.973 + ///
1.974 + /// Iterator for the keys with the same value. It works
1.975 + /// like a graph item iterator, it can be converted to
1.976 + /// the item type of the map, incremented with \c ++ operator, and
1.977 + /// if the iterator leaves the last valid item, it will be equal to
1.978 + /// \c INVALID.
1.979 + class ItemIt : public Key {
1.980 + public:
1.981 + typedef Key Parent;
1.982 +
1.983 + /// \brief Invalid constructor \& conversion.
1.984 + ///
1.985 + /// This constructor initializes the iterator to be invalid.
1.986 + /// \sa Invalid for more details.
1.987 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.988 +
1.989 + /// \brief Creates an iterator with a value.
1.990 + ///
1.991 + /// Creates an iterator with a value. It iterates on the
1.992 + /// keys which have the given value.
1.993 + /// \param map The IterableValueMap
1.994 + /// \param value The value
1.995 + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
1.996 + typename std::map<Value, Key>::const_iterator it =
1.997 + map._first.find(value);
1.998 + if (it == map._first.end()) {
1.999 + Parent::operator=(INVALID);
1.1000 + } else {
1.1001 + Parent::operator=(it->second);
1.1002 + }
1.1003 + }
1.1004 +
1.1005 + /// \brief Increment operator.
1.1006 + ///
1.1007 + /// Increment Operator.
1.1008 + ItemIt& operator++() {
1.1009 + Parent::operator=(_map->IterableValueMap::Parent::
1.1010 + operator[](static_cast<Parent&>(*this)).next);
1.1011 + return *this;
1.1012 + }
1.1013 +
1.1014 +
1.1015 + private:
1.1016 + const IterableValueMap* _map;
1.1017 + };
1.1018 +
1.1019 + protected:
1.1020 +
1.1021 + virtual void add(const Key& key) {
1.1022 + Parent::add(key);
1.1023 + unlace(key);
1.1024 + }
1.1025 +
1.1026 + virtual void add(const std::vector<Key>& keys) {
1.1027 + Parent::add(keys);
1.1028 + for (int i = 0; i < int(keys.size()); ++i) {
1.1029 + lace(keys[i]);
1.1030 + }
1.1031 + }
1.1032 +
1.1033 + virtual void erase(const Key& key) {
1.1034 + unlace(key);
1.1035 + Parent::erase(key);
1.1036 + }
1.1037 +
1.1038 + virtual void erase(const std::vector<Key>& keys) {
1.1039 + for (int i = 0; i < int(keys.size()); ++i) {
1.1040 + unlace(keys[i]);
1.1041 + }
1.1042 + Parent::erase(keys);
1.1043 + }
1.1044 +
1.1045 + virtual void build() {
1.1046 + Parent::build();
1.1047 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.1048 + lace(it);
1.1049 + }
1.1050 + }
1.1051 +
1.1052 + virtual void clear() {
1.1053 + _first.clear();
1.1054 + Parent::clear();
1.1055 + }
1.1056 +
1.1057 + private:
1.1058 + std::map<Value, Key> _first;
1.1059 + };
1.1060 +
1.1061 /// \brief Map of the source nodes of arcs in a digraph.
1.1062 ///
1.1063 /// SourceMap provides access for the source node of each arc in a digraph,
1.1064 @@ -2480,7 +3395,7 @@
1.1065 /// in constant time. On the other hand, the values are updated automatically
1.1066 /// whenever the digraph changes.
1.1067 ///
1.1068 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1069 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1070 /// may provide alternative ways to modify the digraph.
1.1071 /// The correct behavior of InDegMap is not guarantied if these additional
1.1072 /// features are used. For example the functions
1.1073 @@ -2496,7 +3411,7 @@
1.1074 ::ItemNotifier::ObserverBase {
1.1075
1.1076 public:
1.1077 -
1.1078 +
1.1079 /// The graph type of InDegMap
1.1080 typedef GR Graph;
1.1081 typedef GR Digraph;
1.1082 @@ -2610,7 +3525,7 @@
1.1083 /// in constant time. On the other hand, the values are updated automatically
1.1084 /// whenever the digraph changes.
1.1085 ///
1.1086 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1087 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1088 /// may provide alternative ways to modify the digraph.
1.1089 /// The correct behavior of OutDegMap is not guarantied if these additional
1.1090 /// features are used. For example the functions