1.1 --- a/lemon/maps.h Mon Aug 31 07:22:26 2009 +0200
1.2 +++ b/lemon/maps.h Mon Aug 31 08:25:33 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 @@ -2273,7 +2272,7 @@
1.30 }
1.31
1.32 /// \brief Gives back the item belonging to a \e RangeId
1.33 - ///
1.34 + ///
1.35 /// Gives back the item belonging to a \e RangeId.
1.36 Item operator()(int id) const {
1.37 return _inv_map[id];
1.38 @@ -2330,6 +2329,903 @@
1.39 }
1.40 };
1.41
1.42 + /// \brief Dynamic iterable \c bool map.
1.43 + ///
1.44 + /// This class provides a special graph map type which can store a
1.45 + /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
1.46 + /// For both \c true and \c false values it is possible to iterate on
1.47 + /// the keys.
1.48 + ///
1.49 + /// This type is a reference map, so it can be modified with the
1.50 + /// subscript operator.
1.51 + ///
1.52 + /// \tparam GR The graph type.
1.53 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.54 + /// \c GR::Edge).
1.55 + ///
1.56 + /// \see IterableIntMap, IterableValueMap
1.57 + /// \see CrossRefMap
1.58 + template <typename GR, typename K>
1.59 + class IterableBoolMap
1.60 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
1.61 + private:
1.62 + typedef GR Graph;
1.63 +
1.64 + typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
1.65 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
1.66 +
1.67 + std::vector<K> _array;
1.68 + int _sep;
1.69 +
1.70 + public:
1.71 +
1.72 + /// Indicates that the map is reference map.
1.73 + typedef True ReferenceMapTag;
1.74 +
1.75 + /// The key type
1.76 + typedef K Key;
1.77 + /// The value type
1.78 + typedef bool Value;
1.79 + /// The const reference type.
1.80 + typedef const Value& ConstReference;
1.81 +
1.82 + private:
1.83 +
1.84 + int position(const Key& key) const {
1.85 + return Parent::operator[](key);
1.86 + }
1.87 +
1.88 + public:
1.89 +
1.90 + /// \brief Reference to the value of the map.
1.91 + ///
1.92 + /// This class is similar to the \c bool type. It can be converted to
1.93 + /// \c bool and it provides the same operators.
1.94 + class Reference {
1.95 + friend class IterableBoolMap;
1.96 + private:
1.97 + Reference(IterableBoolMap& map, const Key& key)
1.98 + : _key(key), _map(map) {}
1.99 + public:
1.100 +
1.101 + Reference& operator=(const Reference& value) {
1.102 + _map.set(_key, static_cast<bool>(value));
1.103 + return *this;
1.104 + }
1.105 +
1.106 + operator bool() const {
1.107 + return static_cast<const IterableBoolMap&>(_map)[_key];
1.108 + }
1.109 +
1.110 + Reference& operator=(bool value) {
1.111 + _map.set(_key, value);
1.112 + return *this;
1.113 + }
1.114 + Reference& operator&=(bool value) {
1.115 + _map.set(_key, _map[_key] & value);
1.116 + return *this;
1.117 + }
1.118 + Reference& operator|=(bool value) {
1.119 + _map.set(_key, _map[_key] | value);
1.120 + return *this;
1.121 + }
1.122 + Reference& operator^=(bool value) {
1.123 + _map.set(_key, _map[_key] ^ value);
1.124 + return *this;
1.125 + }
1.126 + private:
1.127 + Key _key;
1.128 + IterableBoolMap& _map;
1.129 + };
1.130 +
1.131 + /// \brief Constructor of the map with a default value.
1.132 + ///
1.133 + /// Constructor of the map with a default value.
1.134 + explicit IterableBoolMap(const Graph& graph, bool def = false)
1.135 + : Parent(graph) {
1.136 + typename Parent::Notifier* nf = Parent::notifier();
1.137 + Key it;
1.138 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.139 + Parent::set(it, _array.size());
1.140 + _array.push_back(it);
1.141 + }
1.142 + _sep = (def ? _array.size() : 0);
1.143 + }
1.144 +
1.145 + /// \brief Const subscript operator of the map.
1.146 + ///
1.147 + /// Const subscript operator of the map.
1.148 + bool operator[](const Key& key) const {
1.149 + return position(key) < _sep;
1.150 + }
1.151 +
1.152 + /// \brief Subscript operator of the map.
1.153 + ///
1.154 + /// Subscript operator of the map.
1.155 + Reference operator[](const Key& key) {
1.156 + return Reference(*this, key);
1.157 + }
1.158 +
1.159 + /// \brief Set operation of the map.
1.160 + ///
1.161 + /// Set operation of the map.
1.162 + void set(const Key& key, bool value) {
1.163 + int pos = position(key);
1.164 + if (value) {
1.165 + if (pos < _sep) return;
1.166 + Key tmp = _array[_sep];
1.167 + _array[_sep] = key;
1.168 + Parent::set(key, _sep);
1.169 + _array[pos] = tmp;
1.170 + Parent::set(tmp, pos);
1.171 + ++_sep;
1.172 + } else {
1.173 + if (pos >= _sep) return;
1.174 + --_sep;
1.175 + Key tmp = _array[_sep];
1.176 + _array[_sep] = key;
1.177 + Parent::set(key, _sep);
1.178 + _array[pos] = tmp;
1.179 + Parent::set(tmp, pos);
1.180 + }
1.181 + }
1.182 +
1.183 + /// \brief Set all items.
1.184 + ///
1.185 + /// Set all items in the map.
1.186 + /// \note Constant time operation.
1.187 + void setAll(bool value) {
1.188 + _sep = (value ? _array.size() : 0);
1.189 + }
1.190 +
1.191 + /// \brief Returns the number of the keys mapped to \c true.
1.192 + ///
1.193 + /// Returns the number of the keys mapped to \c true.
1.194 + int trueNum() const {
1.195 + return _sep;
1.196 + }
1.197 +
1.198 + /// \brief Returns the number of the keys mapped to \c false.
1.199 + ///
1.200 + /// Returns the number of the keys mapped to \c false.
1.201 + int falseNum() const {
1.202 + return _array.size() - _sep;
1.203 + }
1.204 +
1.205 + /// \brief Iterator for the keys mapped to \c true.
1.206 + ///
1.207 + /// Iterator for the keys mapped to \c true. It works
1.208 + /// like a graph item iterator, it can be converted to
1.209 + /// the key type of the map, incremented with \c ++ operator, and
1.210 + /// if the iterator leaves the last valid key, it will be equal to
1.211 + /// \c INVALID.
1.212 + class TrueIt : public Key {
1.213 + public:
1.214 + typedef Key Parent;
1.215 +
1.216 + /// \brief Creates an iterator.
1.217 + ///
1.218 + /// Creates an iterator. It iterates on the
1.219 + /// keys mapped to \c true.
1.220 + /// \param map The IterableBoolMap.
1.221 + explicit TrueIt(const IterableBoolMap& map)
1.222 + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
1.223 + _map(&map) {}
1.224 +
1.225 + /// \brief Invalid constructor \& conversion.
1.226 + ///
1.227 + /// This constructor initializes the iterator to be invalid.
1.228 + /// \sa Invalid for more details.
1.229 + TrueIt(Invalid) : Parent(INVALID), _map(0) {}
1.230 +
1.231 + /// \brief Increment operator.
1.232 + ///
1.233 + /// Increment operator.
1.234 + TrueIt& operator++() {
1.235 + int pos = _map->position(*this);
1.236 + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
1.237 + return *this;
1.238 + }
1.239 +
1.240 + private:
1.241 + const IterableBoolMap* _map;
1.242 + };
1.243 +
1.244 + /// \brief Iterator for the keys mapped to \c false.
1.245 + ///
1.246 + /// Iterator for the keys mapped to \c false. It works
1.247 + /// like a graph item iterator, it can be converted to
1.248 + /// the key type of the map, incremented with \c ++ operator, and
1.249 + /// if the iterator leaves the last valid key, it will be equal to
1.250 + /// \c INVALID.
1.251 + class FalseIt : public Key {
1.252 + public:
1.253 + typedef Key Parent;
1.254 +
1.255 + /// \brief Creates an iterator.
1.256 + ///
1.257 + /// Creates an iterator. It iterates on the
1.258 + /// keys mapped to \c false.
1.259 + /// \param map The IterableBoolMap.
1.260 + explicit FalseIt(const IterableBoolMap& map)
1.261 + : Parent(map._sep < int(map._array.size()) ?
1.262 + map._array.back() : INVALID), _map(&map) {}
1.263 +
1.264 + /// \brief Invalid constructor \& conversion.
1.265 + ///
1.266 + /// This constructor initializes the iterator to be invalid.
1.267 + /// \sa Invalid for more details.
1.268 + FalseIt(Invalid) : Parent(INVALID), _map(0) {}
1.269 +
1.270 + /// \brief Increment operator.
1.271 + ///
1.272 + /// Increment operator.
1.273 + FalseIt& operator++() {
1.274 + int pos = _map->position(*this);
1.275 + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
1.276 + return *this;
1.277 + }
1.278 +
1.279 + private:
1.280 + const IterableBoolMap* _map;
1.281 + };
1.282 +
1.283 + /// \brief Iterator for the keys mapped to a given value.
1.284 + ///
1.285 + /// Iterator for the keys mapped to a given value. It works
1.286 + /// like a graph item iterator, it can be converted to
1.287 + /// the key type of the map, incremented with \c ++ operator, and
1.288 + /// if the iterator leaves the last valid key, it will be equal to
1.289 + /// \c INVALID.
1.290 + class ItemIt : public Key {
1.291 + public:
1.292 + typedef Key Parent;
1.293 +
1.294 + /// \brief Creates an iterator with a value.
1.295 + ///
1.296 + /// Creates an iterator with a value. It iterates on the
1.297 + /// keys mapped to the given value.
1.298 + /// \param map The IterableBoolMap.
1.299 + /// \param value The value.
1.300 + ItemIt(const IterableBoolMap& map, bool value)
1.301 + : Parent(value ?
1.302 + (map._sep > 0 ?
1.303 + map._array[map._sep - 1] : INVALID) :
1.304 + (map._sep < int(map._array.size()) ?
1.305 + map._array.back() : INVALID)), _map(&map) {}
1.306 +
1.307 + /// \brief Invalid constructor \& conversion.
1.308 + ///
1.309 + /// This constructor initializes the iterator to be invalid.
1.310 + /// \sa Invalid for more details.
1.311 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.312 +
1.313 + /// \brief Increment operator.
1.314 + ///
1.315 + /// Increment operator.
1.316 + ItemIt& operator++() {
1.317 + int pos = _map->position(*this);
1.318 + int _sep = pos >= _map->_sep ? _map->_sep : 0;
1.319 + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
1.320 + return *this;
1.321 + }
1.322 +
1.323 + private:
1.324 + const IterableBoolMap* _map;
1.325 + };
1.326 +
1.327 + protected:
1.328 +
1.329 + virtual void add(const Key& key) {
1.330 + Parent::add(key);
1.331 + Parent::set(key, _array.size());
1.332 + _array.push_back(key);
1.333 + }
1.334 +
1.335 + virtual void add(const std::vector<Key>& keys) {
1.336 + Parent::add(keys);
1.337 + for (int i = 0; i < int(keys.size()); ++i) {
1.338 + Parent::set(keys[i], _array.size());
1.339 + _array.push_back(keys[i]);
1.340 + }
1.341 + }
1.342 +
1.343 + virtual void erase(const Key& key) {
1.344 + int pos = position(key);
1.345 + if (pos < _sep) {
1.346 + --_sep;
1.347 + Parent::set(_array[_sep], pos);
1.348 + _array[pos] = _array[_sep];
1.349 + Parent::set(_array.back(), _sep);
1.350 + _array[_sep] = _array.back();
1.351 + _array.pop_back();
1.352 + } else {
1.353 + Parent::set(_array.back(), pos);
1.354 + _array[pos] = _array.back();
1.355 + _array.pop_back();
1.356 + }
1.357 + Parent::erase(key);
1.358 + }
1.359 +
1.360 + virtual void erase(const std::vector<Key>& keys) {
1.361 + for (int i = 0; i < int(keys.size()); ++i) {
1.362 + int pos = position(keys[i]);
1.363 + if (pos < _sep) {
1.364 + --_sep;
1.365 + Parent::set(_array[_sep], pos);
1.366 + _array[pos] = _array[_sep];
1.367 + Parent::set(_array.back(), _sep);
1.368 + _array[_sep] = _array.back();
1.369 + _array.pop_back();
1.370 + } else {
1.371 + Parent::set(_array.back(), pos);
1.372 + _array[pos] = _array.back();
1.373 + _array.pop_back();
1.374 + }
1.375 + }
1.376 + Parent::erase(keys);
1.377 + }
1.378 +
1.379 + virtual void build() {
1.380 + Parent::build();
1.381 + typename Parent::Notifier* nf = Parent::notifier();
1.382 + Key it;
1.383 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.384 + Parent::set(it, _array.size());
1.385 + _array.push_back(it);
1.386 + }
1.387 + _sep = 0;
1.388 + }
1.389 +
1.390 + virtual void clear() {
1.391 + _array.clear();
1.392 + _sep = 0;
1.393 + Parent::clear();
1.394 + }
1.395 +
1.396 + };
1.397 +
1.398 +
1.399 + namespace _maps_bits {
1.400 + template <typename Item>
1.401 + struct IterableIntMapNode {
1.402 + IterableIntMapNode() : value(-1) {}
1.403 + IterableIntMapNode(int _value) : value(_value) {}
1.404 + Item prev, next;
1.405 + int value;
1.406 + };
1.407 + }
1.408 +
1.409 + /// \brief Dynamic iterable integer map.
1.410 + ///
1.411 + /// This class provides a special graph map type which can store an
1.412 + /// integer value for graph items (\c Node, \c Arc or \c Edge).
1.413 + /// For each non-negative value it is possible to iterate on the keys
1.414 + /// mapped to the value.
1.415 + ///
1.416 + /// This type is a reference map, so it can be modified with the
1.417 + /// subscript operator.
1.418 + ///
1.419 + /// \note The size of the data structure depends on the largest
1.420 + /// value in the map.
1.421 + ///
1.422 + /// \tparam GR The graph type.
1.423 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.424 + /// \c GR::Edge).
1.425 + ///
1.426 + /// \see IterableBoolMap, IterableValueMap
1.427 + /// \see CrossRefMap
1.428 + template <typename GR, typename K>
1.429 + class IterableIntMap
1.430 + : protected ItemSetTraits<GR, K>::
1.431 + template Map<_maps_bits::IterableIntMapNode<K> >::Type {
1.432 + public:
1.433 + typedef typename ItemSetTraits<GR, K>::
1.434 + template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
1.435 +
1.436 + /// The key type
1.437 + typedef K Key;
1.438 + /// The value type
1.439 + typedef int Value;
1.440 + /// The graph type
1.441 + typedef GR Graph;
1.442 +
1.443 + /// \brief Constructor of the map.
1.444 + ///
1.445 + /// Constructor of the map. It sets all values to -1.
1.446 + explicit IterableIntMap(const Graph& graph)
1.447 + : Parent(graph) {}
1.448 +
1.449 + /// \brief Constructor of the map with a given value.
1.450 + ///
1.451 + /// Constructor of the map with a given value.
1.452 + explicit IterableIntMap(const Graph& graph, int value)
1.453 + : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
1.454 + if (value >= 0) {
1.455 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.456 + lace(it);
1.457 + }
1.458 + }
1.459 + }
1.460 +
1.461 + private:
1.462 +
1.463 + void unlace(const Key& key) {
1.464 + typename Parent::Value& node = Parent::operator[](key);
1.465 + if (node.value < 0) return;
1.466 + if (node.prev != INVALID) {
1.467 + Parent::operator[](node.prev).next = node.next;
1.468 + } else {
1.469 + _first[node.value] = node.next;
1.470 + }
1.471 + if (node.next != INVALID) {
1.472 + Parent::operator[](node.next).prev = node.prev;
1.473 + }
1.474 + while (!_first.empty() && _first.back() == INVALID) {
1.475 + _first.pop_back();
1.476 + }
1.477 + }
1.478 +
1.479 + void lace(const Key& key) {
1.480 + typename Parent::Value& node = Parent::operator[](key);
1.481 + if (node.value < 0) return;
1.482 + if (node.value >= int(_first.size())) {
1.483 + _first.resize(node.value + 1, INVALID);
1.484 + }
1.485 + node.prev = INVALID;
1.486 + node.next = _first[node.value];
1.487 + if (node.next != INVALID) {
1.488 + Parent::operator[](node.next).prev = key;
1.489 + }
1.490 + _first[node.value] = key;
1.491 + }
1.492 +
1.493 + public:
1.494 +
1.495 + /// Indicates that the map is reference map.
1.496 + typedef True ReferenceMapTag;
1.497 +
1.498 + /// \brief Reference to the value of the map.
1.499 + ///
1.500 + /// This class is similar to the \c int type. It can
1.501 + /// be converted to \c int and it has the same operators.
1.502 + class Reference {
1.503 + friend class IterableIntMap;
1.504 + private:
1.505 + Reference(IterableIntMap& map, const Key& key)
1.506 + : _key(key), _map(map) {}
1.507 + public:
1.508 +
1.509 + Reference& operator=(const Reference& value) {
1.510 + _map.set(_key, static_cast<const int&>(value));
1.511 + return *this;
1.512 + }
1.513 +
1.514 + operator const int&() const {
1.515 + return static_cast<const IterableIntMap&>(_map)[_key];
1.516 + }
1.517 +
1.518 + Reference& operator=(int value) {
1.519 + _map.set(_key, value);
1.520 + return *this;
1.521 + }
1.522 + Reference& operator++() {
1.523 + _map.set(_key, _map[_key] + 1);
1.524 + return *this;
1.525 + }
1.526 + int operator++(int) {
1.527 + int value = _map[_key];
1.528 + _map.set(_key, value + 1);
1.529 + return value;
1.530 + }
1.531 + Reference& operator--() {
1.532 + _map.set(_key, _map[_key] - 1);
1.533 + return *this;
1.534 + }
1.535 + int operator--(int) {
1.536 + int value = _map[_key];
1.537 + _map.set(_key, value - 1);
1.538 + return value;
1.539 + }
1.540 + Reference& operator+=(int value) {
1.541 + _map.set(_key, _map[_key] + value);
1.542 + return *this;
1.543 + }
1.544 + Reference& operator-=(int value) {
1.545 + _map.set(_key, _map[_key] - value);
1.546 + return *this;
1.547 + }
1.548 + Reference& operator*=(int value) {
1.549 + _map.set(_key, _map[_key] * value);
1.550 + return *this;
1.551 + }
1.552 + Reference& operator/=(int value) {
1.553 + _map.set(_key, _map[_key] / value);
1.554 + return *this;
1.555 + }
1.556 + Reference& operator%=(int value) {
1.557 + _map.set(_key, _map[_key] % value);
1.558 + return *this;
1.559 + }
1.560 + Reference& operator&=(int value) {
1.561 + _map.set(_key, _map[_key] & value);
1.562 + return *this;
1.563 + }
1.564 + Reference& operator|=(int value) {
1.565 + _map.set(_key, _map[_key] | value);
1.566 + return *this;
1.567 + }
1.568 + Reference& operator^=(int value) {
1.569 + _map.set(_key, _map[_key] ^ value);
1.570 + return *this;
1.571 + }
1.572 + Reference& operator<<=(int value) {
1.573 + _map.set(_key, _map[_key] << value);
1.574 + return *this;
1.575 + }
1.576 + Reference& operator>>=(int value) {
1.577 + _map.set(_key, _map[_key] >> value);
1.578 + return *this;
1.579 + }
1.580 +
1.581 + private:
1.582 + Key _key;
1.583 + IterableIntMap& _map;
1.584 + };
1.585 +
1.586 + /// The const reference type.
1.587 + typedef const Value& ConstReference;
1.588 +
1.589 + /// \brief Gives back the maximal value plus one.
1.590 + ///
1.591 + /// Gives back the maximal value plus one.
1.592 + int size() const {
1.593 + return _first.size();
1.594 + }
1.595 +
1.596 + /// \brief Set operation of the map.
1.597 + ///
1.598 + /// Set operation of the map.
1.599 + void set(const Key& key, const Value& value) {
1.600 + unlace(key);
1.601 + Parent::operator[](key).value = value;
1.602 + lace(key);
1.603 + }
1.604 +
1.605 + /// \brief Const subscript operator of the map.
1.606 + ///
1.607 + /// Const subscript operator of the map.
1.608 + const Value& operator[](const Key& key) const {
1.609 + return Parent::operator[](key).value;
1.610 + }
1.611 +
1.612 + /// \brief Subscript operator of the map.
1.613 + ///
1.614 + /// Subscript operator of the map.
1.615 + Reference operator[](const Key& key) {
1.616 + return Reference(*this, key);
1.617 + }
1.618 +
1.619 + /// \brief Iterator for the keys with the same value.
1.620 + ///
1.621 + /// Iterator for the keys with the same value. It works
1.622 + /// like a graph item iterator, it can be converted to
1.623 + /// the item type of the map, incremented with \c ++ operator, and
1.624 + /// if the iterator leaves the last valid item, it will be equal to
1.625 + /// \c INVALID.
1.626 + class ItemIt : public Key {
1.627 + public:
1.628 + typedef Key Parent;
1.629 +
1.630 + /// \brief Invalid constructor \& conversion.
1.631 + ///
1.632 + /// This constructor initializes the iterator to be invalid.
1.633 + /// \sa Invalid for more details.
1.634 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.635 +
1.636 + /// \brief Creates an iterator with a value.
1.637 + ///
1.638 + /// Creates an iterator with a value. It iterates on the
1.639 + /// keys mapped to the given value.
1.640 + /// \param map The IterableIntMap.
1.641 + /// \param value The value.
1.642 + ItemIt(const IterableIntMap& map, int value) : _map(&map) {
1.643 + if (value < 0 || value >= int(_map->_first.size())) {
1.644 + Parent::operator=(INVALID);
1.645 + } else {
1.646 + Parent::operator=(_map->_first[value]);
1.647 + }
1.648 + }
1.649 +
1.650 + /// \brief Increment operator.
1.651 + ///
1.652 + /// Increment operator.
1.653 + ItemIt& operator++() {
1.654 + Parent::operator=(_map->IterableIntMap::Parent::
1.655 + operator[](static_cast<Parent&>(*this)).next);
1.656 + return *this;
1.657 + }
1.658 +
1.659 + private:
1.660 + const IterableIntMap* _map;
1.661 + };
1.662 +
1.663 + protected:
1.664 +
1.665 + virtual void erase(const Key& key) {
1.666 + unlace(key);
1.667 + Parent::erase(key);
1.668 + }
1.669 +
1.670 + virtual void erase(const std::vector<Key>& keys) {
1.671 + for (int i = 0; i < int(keys.size()); ++i) {
1.672 + unlace(keys[i]);
1.673 + }
1.674 + Parent::erase(keys);
1.675 + }
1.676 +
1.677 + virtual void clear() {
1.678 + _first.clear();
1.679 + Parent::clear();
1.680 + }
1.681 +
1.682 + private:
1.683 + std::vector<Key> _first;
1.684 + };
1.685 +
1.686 + namespace _maps_bits {
1.687 + template <typename Item, typename Value>
1.688 + struct IterableValueMapNode {
1.689 + IterableValueMapNode(Value _value = Value()) : value(_value) {}
1.690 + Item prev, next;
1.691 + Value value;
1.692 + };
1.693 + }
1.694 +
1.695 + /// \brief Dynamic iterable map for comparable values.
1.696 + ///
1.697 + /// This class provides a special graph map type which can store an
1.698 + /// comparable value for graph items (\c Node, \c Arc or \c Edge).
1.699 + /// For each value it is possible to iterate on the keys mapped to
1.700 + /// the value.
1.701 + ///
1.702 + /// The map stores for each value a linked list with
1.703 + /// the items which mapped to the value, and the values are stored
1.704 + /// in balanced binary tree. The values of the map can be accessed
1.705 + /// with stl compatible forward iterator.
1.706 + ///
1.707 + /// This type is not reference map, so it cannot be modified with
1.708 + /// the subscript operator.
1.709 + ///
1.710 + /// \tparam GR The graph type.
1.711 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.712 + /// \c GR::Edge).
1.713 + /// \tparam V The value type of the map. It can be any comparable
1.714 + /// value type.
1.715 + ///
1.716 + /// \see IterableBoolMap, IterableIntMap
1.717 + /// \see CrossRefMap
1.718 + template <typename GR, typename K, typename V>
1.719 + class IterableValueMap
1.720 + : protected ItemSetTraits<GR, K>::
1.721 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
1.722 + public:
1.723 + typedef typename ItemSetTraits<GR, K>::
1.724 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
1.725 +
1.726 + /// The key type
1.727 + typedef K Key;
1.728 + /// The value type
1.729 + typedef V Value;
1.730 + /// The graph type
1.731 + typedef GR Graph;
1.732 +
1.733 + public:
1.734 +
1.735 + /// \brief Constructor of the map with a given value.
1.736 + ///
1.737 + /// Constructor of the map with a given value.
1.738 + explicit IterableValueMap(const Graph& graph,
1.739 + const Value& value = Value())
1.740 + : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
1.741 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.742 + lace(it);
1.743 + }
1.744 + }
1.745 +
1.746 + protected:
1.747 +
1.748 + void unlace(const Key& key) {
1.749 + typename Parent::Value& node = Parent::operator[](key);
1.750 + if (node.prev != INVALID) {
1.751 + Parent::operator[](node.prev).next = node.next;
1.752 + } else {
1.753 + if (node.next != INVALID) {
1.754 + _first[node.value] = node.next;
1.755 + } else {
1.756 + _first.erase(node.value);
1.757 + }
1.758 + }
1.759 + if (node.next != INVALID) {
1.760 + Parent::operator[](node.next).prev = node.prev;
1.761 + }
1.762 + }
1.763 +
1.764 + void lace(const Key& key) {
1.765 + typename Parent::Value& node = Parent::operator[](key);
1.766 + typename std::map<Value, Key>::iterator it = _first.find(node.value);
1.767 + if (it == _first.end()) {
1.768 + node.prev = node.next = INVALID;
1.769 + _first.insert(std::make_pair(node.value, key));
1.770 + } else {
1.771 + node.prev = INVALID;
1.772 + node.next = it->second;
1.773 + if (node.next != INVALID) {
1.774 + Parent::operator[](node.next).prev = key;
1.775 + }
1.776 + it->second = key;
1.777 + }
1.778 + }
1.779 +
1.780 + public:
1.781 +
1.782 + /// \brief Forward iterator for values.
1.783 + ///
1.784 + /// This iterator is an stl compatible forward
1.785 + /// iterator on the values of the map. The values can
1.786 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.787 + class ValueIterator
1.788 + : public std::iterator<std::forward_iterator_tag, Value> {
1.789 + friend class IterableValueMap;
1.790 + private:
1.791 + ValueIterator(typename std::map<Value, Key>::const_iterator _it)
1.792 + : it(_it) {}
1.793 + public:
1.794 +
1.795 + ValueIterator() {}
1.796 +
1.797 + ValueIterator& operator++() { ++it; return *this; }
1.798 + ValueIterator operator++(int) {
1.799 + ValueIterator tmp(*this);
1.800 + operator++();
1.801 + return tmp;
1.802 + }
1.803 +
1.804 + const Value& operator*() const { return it->first; }
1.805 + const Value* operator->() const { return &(it->first); }
1.806 +
1.807 + bool operator==(ValueIterator jt) const { return it == jt.it; }
1.808 + bool operator!=(ValueIterator jt) const { return it != jt.it; }
1.809 +
1.810 + private:
1.811 + typename std::map<Value, Key>::const_iterator it;
1.812 + };
1.813 +
1.814 + /// \brief Returns an iterator to the first value.
1.815 + ///
1.816 + /// Returns an stl compatible iterator to the
1.817 + /// first value of the map. The values of the
1.818 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.819 + /// range.
1.820 + ValueIterator beginValue() const {
1.821 + return ValueIterator(_first.begin());
1.822 + }
1.823 +
1.824 + /// \brief Returns an iterator after the last value.
1.825 + ///
1.826 + /// Returns an stl compatible iterator after the
1.827 + /// last value of the map. The values of the
1.828 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.829 + /// range.
1.830 + ValueIterator endValue() const {
1.831 + return ValueIterator(_first.end());
1.832 + }
1.833 +
1.834 + /// \brief Set operation of the map.
1.835 + ///
1.836 + /// Set operation of the map.
1.837 + void set(const Key& key, const Value& value) {
1.838 + unlace(key);
1.839 + Parent::operator[](key).value = value;
1.840 + lace(key);
1.841 + }
1.842 +
1.843 + /// \brief Const subscript operator of the map.
1.844 + ///
1.845 + /// Const subscript operator of the map.
1.846 + const Value& operator[](const Key& key) const {
1.847 + return Parent::operator[](key).value;
1.848 + }
1.849 +
1.850 + /// \brief Iterator for the keys with the same value.
1.851 + ///
1.852 + /// Iterator for the keys with the same value. It works
1.853 + /// like a graph item iterator, it can be converted to
1.854 + /// the item type of the map, incremented with \c ++ operator, and
1.855 + /// if the iterator leaves the last valid item, it will be equal to
1.856 + /// \c INVALID.
1.857 + class ItemIt : public Key {
1.858 + public:
1.859 + typedef Key Parent;
1.860 +
1.861 + /// \brief Invalid constructor \& conversion.
1.862 + ///
1.863 + /// This constructor initializes the iterator to be invalid.
1.864 + /// \sa Invalid for more details.
1.865 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.866 +
1.867 + /// \brief Creates an iterator with a value.
1.868 + ///
1.869 + /// Creates an iterator with a value. It iterates on the
1.870 + /// keys which have the given value.
1.871 + /// \param map The IterableValueMap
1.872 + /// \param value The value
1.873 + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
1.874 + typename std::map<Value, Key>::const_iterator it =
1.875 + map._first.find(value);
1.876 + if (it == map._first.end()) {
1.877 + Parent::operator=(INVALID);
1.878 + } else {
1.879 + Parent::operator=(it->second);
1.880 + }
1.881 + }
1.882 +
1.883 + /// \brief Increment operator.
1.884 + ///
1.885 + /// Increment Operator.
1.886 + ItemIt& operator++() {
1.887 + Parent::operator=(_map->IterableValueMap::Parent::
1.888 + operator[](static_cast<Parent&>(*this)).next);
1.889 + return *this;
1.890 + }
1.891 +
1.892 +
1.893 + private:
1.894 + const IterableValueMap* _map;
1.895 + };
1.896 +
1.897 + protected:
1.898 +
1.899 + virtual void add(const Key& key) {
1.900 + Parent::add(key);
1.901 + unlace(key);
1.902 + }
1.903 +
1.904 + virtual void add(const std::vector<Key>& keys) {
1.905 + Parent::add(keys);
1.906 + for (int i = 0; i < int(keys.size()); ++i) {
1.907 + lace(keys[i]);
1.908 + }
1.909 + }
1.910 +
1.911 + virtual void erase(const Key& key) {
1.912 + unlace(key);
1.913 + Parent::erase(key);
1.914 + }
1.915 +
1.916 + virtual void erase(const std::vector<Key>& keys) {
1.917 + for (int i = 0; i < int(keys.size()); ++i) {
1.918 + unlace(keys[i]);
1.919 + }
1.920 + Parent::erase(keys);
1.921 + }
1.922 +
1.923 + virtual void build() {
1.924 + Parent::build();
1.925 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.926 + lace(it);
1.927 + }
1.928 + }
1.929 +
1.930 + virtual void clear() {
1.931 + _first.clear();
1.932 + Parent::clear();
1.933 + }
1.934 +
1.935 + private:
1.936 + std::map<Value, Key> _first;
1.937 + };
1.938 +
1.939 /// \brief Map of the source nodes of arcs in a digraph.
1.940 ///
1.941 /// SourceMap provides access for the source node of each arc in a digraph,
1.942 @@ -2499,7 +3395,7 @@
1.943 /// in constant time. On the other hand, the values are updated automatically
1.944 /// whenever the digraph changes.
1.945 ///
1.946 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.947 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.948 /// may provide alternative ways to modify the digraph.
1.949 /// The correct behavior of InDegMap is not guarantied if these additional
1.950 /// features are used. For example the functions
1.951 @@ -2515,7 +3411,7 @@
1.952 ::ItemNotifier::ObserverBase {
1.953
1.954 public:
1.955 -
1.956 +
1.957 /// The graph type of InDegMap
1.958 typedef GR Graph;
1.959 typedef GR Digraph;
1.960 @@ -2629,7 +3525,7 @@
1.961 /// in constant time. On the other hand, the values are updated automatically
1.962 /// whenever the digraph changes.
1.963 ///
1.964 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.965 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.966 /// may provide alternative ways to modify the digraph.
1.967 /// The correct behavior of OutDegMap is not guarantied if these additional
1.968 /// features are used. For example the functions
2.1 --- a/test/maps_test.cc Mon Aug 31 07:22:26 2009 +0200
2.2 +++ b/test/maps_test.cc Mon Aug 31 08:25:33 2009 +0200
2.3 @@ -22,7 +22,7 @@
2.4 #include <lemon/concept_check.h>
2.5 #include <lemon/concepts/maps.h>
2.6 #include <lemon/maps.h>
2.7 -#include <lemon/list_graph.h>
2.8 +#include <lemon/smart_graph.h>
2.9
2.10 #include "test_tools.h"
2.11
2.12 @@ -349,10 +349,10 @@
2.13 it != map2.end(); ++it )
2.14 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
2.15 }
2.16 -
2.17 +
2.18 // CrossRefMap
2.19 {
2.20 - typedef ListDigraph Graph;
2.21 + typedef SmartDigraph Graph;
2.22 DIGRAPH_TYPEDEFS(Graph);
2.23
2.24 checkConcept<ReadWriteMap<Node, int>,
2.25 @@ -383,6 +383,193 @@
2.26 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
2.27 it == map.endValue(), "Wrong value iterator");
2.28 }
2.29 +
2.30 + // Iterable bool map
2.31 + {
2.32 + typedef SmartGraph Graph;
2.33 + typedef SmartGraph::Node Item;
2.34
2.35 + typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
2.36 + checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
2.37 +
2.38 + const int num = 10;
2.39 + Graph g;
2.40 + std::vector<Item> items;
2.41 + for (int i = 0; i < num; ++i) {
2.42 + items.push_back(g.addNode());
2.43 + }
2.44 +
2.45 + Ibm map1(g, true);
2.46 + int n = 0;
2.47 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
2.48 + check(map1[static_cast<Item>(it)], "Wrong TrueIt");
2.49 + ++n;
2.50 + }
2.51 + check(n == num, "Wrong number");
2.52 +
2.53 + n = 0;
2.54 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
2.55 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
2.56 + ++n;
2.57 + }
2.58 + check(n == num, "Wrong number");
2.59 + check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
2.60 + check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
2.61 +
2.62 + map1[items[5]] = true;
2.63 +
2.64 + n = 0;
2.65 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
2.66 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
2.67 + ++n;
2.68 + }
2.69 + check(n == num, "Wrong number");
2.70 +
2.71 + map1[items[num / 2]] = false;
2.72 + check(map1[items[num / 2]] == false, "Wrong map value");
2.73 +
2.74 + n = 0;
2.75 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
2.76 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
2.77 + ++n;
2.78 + }
2.79 + check(n == num - 1, "Wrong number");
2.80 +
2.81 + n = 0;
2.82 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
2.83 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
2.84 + ++n;
2.85 + }
2.86 + check(n == 1, "Wrong number");
2.87 +
2.88 + map1[items[0]] = false;
2.89 + check(map1[items[0]] == false, "Wrong map value");
2.90 +
2.91 + map1[items[num - 1]] = false;
2.92 + check(map1[items[num - 1]] == false, "Wrong map value");
2.93 +
2.94 + n = 0;
2.95 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
2.96 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
2.97 + ++n;
2.98 + }
2.99 + check(n == num - 3, "Wrong number");
2.100 + check(map1.trueNum() == num - 3, "Wrong number");
2.101 +
2.102 + n = 0;
2.103 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
2.104 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
2.105 + ++n;
2.106 + }
2.107 + check(n == 3, "Wrong number");
2.108 + check(map1.falseNum() == 3, "Wrong number");
2.109 + }
2.110 +
2.111 + // Iterable int map
2.112 + {
2.113 + typedef SmartGraph Graph;
2.114 + typedef SmartGraph::Node Item;
2.115 + typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
2.116 +
2.117 + checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
2.118 +
2.119 + const int num = 10;
2.120 + Graph g;
2.121 + std::vector<Item> items;
2.122 + for (int i = 0; i < num; ++i) {
2.123 + items.push_back(g.addNode());
2.124 + }
2.125 +
2.126 + Iim map1(g);
2.127 + check(map1.size() == 0, "Wrong size");
2.128 +
2.129 + for (int i = 0; i < num; ++i) {
2.130 + map1[items[i]] = i;
2.131 + }
2.132 + check(map1.size() == num, "Wrong size");
2.133 +
2.134 + for (int i = 0; i < num; ++i) {
2.135 + Iim::ItemIt it(map1, i);
2.136 + check(static_cast<Item>(it) == items[i], "Wrong value");
2.137 + ++it;
2.138 + check(static_cast<Item>(it) == INVALID, "Wrong value");
2.139 + }
2.140 +
2.141 + for (int i = 0; i < num; ++i) {
2.142 + map1[items[i]] = i % 2;
2.143 + }
2.144 + check(map1.size() == 2, "Wrong size");
2.145 +
2.146 + int n = 0;
2.147 + for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
2.148 + check(map1[static_cast<Item>(it)] == 0, "Wrong value");
2.149 + ++n;
2.150 + }
2.151 + check(n == (num + 1) / 2, "Wrong number");
2.152 +
2.153 + for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
2.154 + check(map1[static_cast<Item>(it)] == 1, "Wrong value");
2.155 + ++n;
2.156 + }
2.157 + check(n == num, "Wrong number");
2.158 +
2.159 + }
2.160 +
2.161 + // Iterable value map
2.162 + {
2.163 + typedef SmartGraph Graph;
2.164 + typedef SmartGraph::Node Item;
2.165 + typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
2.166 +
2.167 + checkConcept<ReadWriteMap<Item, double>, Ivm>();
2.168 +
2.169 + const int num = 10;
2.170 + Graph g;
2.171 + std::vector<Item> items;
2.172 + for (int i = 0; i < num; ++i) {
2.173 + items.push_back(g.addNode());
2.174 + }
2.175 +
2.176 + Ivm map1(g, 0.0);
2.177 + check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
2.178 + check(*map1.beginValue() == 0.0, "Wrong value");
2.179 +
2.180 + for (int i = 0; i < num; ++i) {
2.181 + map1.set(items[i], static_cast<double>(i));
2.182 + }
2.183 + check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
2.184 +
2.185 + for (int i = 0; i < num; ++i) {
2.186 + Ivm::ItemIt it(map1, static_cast<double>(i));
2.187 + check(static_cast<Item>(it) == items[i], "Wrong value");
2.188 + ++it;
2.189 + check(static_cast<Item>(it) == INVALID, "Wrong value");
2.190 + }
2.191 +
2.192 + for (Ivm::ValueIterator vit = map1.beginValue();
2.193 + vit != map1.endValue(); ++vit) {
2.194 + check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
2.195 + "Wrong ValueIterator");
2.196 + }
2.197 +
2.198 + for (int i = 0; i < num; ++i) {
2.199 + map1.set(items[i], static_cast<double>(i % 2));
2.200 + }
2.201 + check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
2.202 +
2.203 + int n = 0;
2.204 + for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
2.205 + check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
2.206 + ++n;
2.207 + }
2.208 + check(n == (num + 1) / 2, "Wrong number");
2.209 +
2.210 + for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
2.211 + check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
2.212 + ++n;
2.213 + }
2.214 + check(n == num, "Wrong number");
2.215 +
2.216 + }
2.217 return 0;
2.218 }