1.1 --- a/lemon/maps.h Fri Aug 09 11:07:27 2013 +0200
1.2 +++ b/lemon/maps.h Sun Aug 11 15:28:12 2013 +0200
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-2009
1.8 + * Copyright (C) 2003-2010
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,7 +56,7 @@
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 @@ -90,7 +89,7 @@
1.39 /// value to each key.
1.40 ///
1.41 /// In other aspects it is equivalent to \c NullMap.
1.42 - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
1.43 + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
1.44 /// concept, but it absorbs the data written to it.
1.45 ///
1.46 /// The simplest way of using this map is through the constMap()
1.47 @@ -159,7 +158,7 @@
1.48 /// value to each key.
1.49 ///
1.50 /// In other aspects it is equivalent to \c NullMap.
1.51 - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
1.52 + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
1.53 /// concept, but it absorbs the data written to it.
1.54 ///
1.55 /// The simplest way of using this map is through the constMap()
1.56 @@ -231,9 +230,9 @@
1.57 ///
1.58 /// This map is essentially a wrapper for \c std::vector. It assigns
1.59 /// values to integer keys from the range <tt>[0..size-1]</tt>.
1.60 - /// It can be used with some data structures, for example
1.61 - /// \c UnionFind, \c BinHeap, when the used items are small
1.62 - /// integers. This map conforms the \ref concepts::ReferenceMap
1.63 + /// It can be used together with some data structures, e.g.
1.64 + /// heap types and \c UnionFind, when the used items are small
1.65 + /// integers. This map conforms to the \ref concepts::ReferenceMap
1.66 /// "ReferenceMap" concept.
1.67 ///
1.68 /// The simplest way of using this map is through the rangeMap()
1.69 @@ -341,7 +340,7 @@
1.70 /// that you can specify a default value for the keys that are not
1.71 /// stored actually. This value can be different from the default
1.72 /// contructed value (i.e. \c %Value()).
1.73 - /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
1.74 + /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
1.75 /// concept.
1.76 ///
1.77 /// This map is useful if a default value should be assigned to most of
1.78 @@ -349,9 +348,9 @@
1.79 /// keys (i.e. the map is "sparse").
1.80 /// The name of this type also refers to this important usage.
1.81 ///
1.82 - /// Apart form that this map can be used in many other cases since it
1.83 + /// Apart form that, this map can be used in many other cases since it
1.84 /// is based on \c std::map, which is a general associative container.
1.85 - /// However keep in mind that it is usually not as efficient as other
1.86 + /// However, keep in mind that it is usually not as efficient as other
1.87 /// maps.
1.88 ///
1.89 /// The simplest way of using this map is through the sparseMap()
1.90 @@ -707,7 +706,7 @@
1.91 /// "readable map" to another type using the default conversion.
1.92 /// The \c Key type of it is inherited from \c M and the \c Value
1.93 /// type is \c V.
1.94 - /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
1.95 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.96 ///
1.97 /// The simplest way of using this map is through the convertMap()
1.98 /// function.
1.99 @@ -1786,22 +1785,22 @@
1.100 ///
1.101 /// The most important usage of it is storing certain nodes or arcs
1.102 /// that were marked \c true by an algorithm.
1.103 - /// For example it makes easier to store the nodes in the processing
1.104 + /// For example, it makes easier to store the nodes in the processing
1.105 /// order of Dfs algorithm, as the following examples show.
1.106 /// \code
1.107 /// std::vector<Node> v;
1.108 - /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1.109 + /// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
1.110 /// \endcode
1.111 /// \code
1.112 /// std::vector<Node> v(countNodes(g));
1.113 - /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1.114 + /// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
1.115 /// \endcode
1.116 ///
1.117 /// \note The container of the iterator must contain enough space
1.118 /// for the elements or the iterator should be an inserter iterator.
1.119 ///
1.120 /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1.121 - /// it cannot be used when a readable map is needed, for example as
1.122 + /// it cannot be used when a readable map is needed, for example, as
1.123 /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1.124 ///
1.125 /// \relates LoggerBoolMap
1.126 @@ -1818,7 +1817,7 @@
1.127 /// \brief Provides an immutable and unique id for each item in a graph.
1.128 ///
1.129 /// IdMap provides a unique and immutable id for each item of the
1.130 - /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1.131 + /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1.132 /// - \b unique: different items get different ids,
1.133 /// - \b immutable: the id of an item does not change (even if you
1.134 /// delete other nodes).
1.135 @@ -1826,7 +1825,7 @@
1.136 /// Using this map you get access (i.e. can read) the inner id values of
1.137 /// the items stored in the graph, which is returned by the \c id()
1.138 /// function of the graph. This map can be inverted with its member
1.139 - /// class \c InverseMap or with the \c operator() member.
1.140 + /// class \c InverseMap or with the \c operator()() member.
1.141 ///
1.142 /// \tparam GR The graph type.
1.143 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.144 @@ -1866,9 +1865,11 @@
1.145
1.146 public:
1.147
1.148 - /// \brief This class represents the inverse of its owner (IdMap).
1.149 + /// \brief The inverse map type of IdMap.
1.150 ///
1.151 - /// This class represents the inverse of its owner (IdMap).
1.152 + /// The inverse map type of IdMap. The subscript operator gives back
1.153 + /// an item by its id.
1.154 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.155 /// \see inverse()
1.156 class InverseMap {
1.157 public:
1.158 @@ -1883,9 +1884,9 @@
1.159 /// Constructor for creating an id-to-item map.
1.160 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1.161
1.162 - /// \brief Gives back the given item from its id.
1.163 + /// \brief Gives back an item by its id.
1.164 ///
1.165 - /// Gives back the given item from its id.
1.166 + /// Gives back an item by its id.
1.167 Item operator[](int id) const { return _graph->fromId(id, Item());}
1.168
1.169 private:
1.170 @@ -1898,14 +1899,31 @@
1.171 InverseMap inverse() const { return InverseMap(*_graph);}
1.172 };
1.173
1.174 + /// \brief Returns an \c IdMap class.
1.175 + ///
1.176 + /// This function just returns an \c IdMap class.
1.177 + /// \relates IdMap
1.178 + template <typename K, typename GR>
1.179 + inline IdMap<GR, K> idMap(const GR& graph) {
1.180 + return IdMap<GR, K>(graph);
1.181 + }
1.182
1.183 /// \brief General cross reference graph map type.
1.184
1.185 /// This class provides simple invertable graph maps.
1.186 /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1.187 /// and if a key is set to a new value, then stores it in the inverse map.
1.188 - /// The values of the map can be accessed
1.189 - /// with stl compatible forward iterator.
1.190 + /// The graph items can be accessed by their values either using
1.191 + /// \c InverseMap or \c operator()(), and the values of the map can be
1.192 + /// accessed with an STL compatible forward iterator (\c ValueIt).
1.193 + ///
1.194 + /// This map is intended to be used when all associated values are
1.195 + /// different (the map is actually invertable) or there are only a few
1.196 + /// items with the same value.
1.197 + /// Otherwise consider to use \c IterableValueMap, which is more
1.198 + /// suitable and more efficient for such cases. It provides iterators
1.199 + /// to traverse the items with the same associated value, but
1.200 + /// it does not have \c InverseMap.
1.201 ///
1.202 /// This type is not reference map, so it cannot be modified with
1.203 /// the subscript operator.
1.204 @@ -1946,56 +1964,66 @@
1.205
1.206 /// \brief Forward iterator for values.
1.207 ///
1.208 - /// This iterator is an stl compatible forward
1.209 + /// This iterator is an STL compatible forward
1.210 /// iterator on the values of the map. The values can
1.211 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.212 /// They are considered with multiplicity, so each value is
1.213 /// traversed for each item it is assigned to.
1.214 - class ValueIterator
1.215 + class ValueIt
1.216 : public std::iterator<std::forward_iterator_tag, Value> {
1.217 friend class CrossRefMap;
1.218 private:
1.219 - ValueIterator(typename Container::const_iterator _it)
1.220 + ValueIt(typename Container::const_iterator _it)
1.221 : it(_it) {}
1.222 public:
1.223
1.224 - ValueIterator() {}
1.225 -
1.226 - ValueIterator& operator++() { ++it; return *this; }
1.227 - ValueIterator operator++(int) {
1.228 - ValueIterator tmp(*this);
1.229 + /// Constructor
1.230 + ValueIt() {}
1.231 +
1.232 + /// \e
1.233 + ValueIt& operator++() { ++it; return *this; }
1.234 + /// \e
1.235 + ValueIt operator++(int) {
1.236 + ValueIt tmp(*this);
1.237 operator++();
1.238 return tmp;
1.239 }
1.240
1.241 + /// \e
1.242 const Value& operator*() const { return it->first; }
1.243 + /// \e
1.244 const Value* operator->() const { return &(it->first); }
1.245
1.246 - bool operator==(ValueIterator jt) const { return it == jt.it; }
1.247 - bool operator!=(ValueIterator jt) const { return it != jt.it; }
1.248 + /// \e
1.249 + bool operator==(ValueIt jt) const { return it == jt.it; }
1.250 + /// \e
1.251 + bool operator!=(ValueIt jt) const { return it != jt.it; }
1.252
1.253 private:
1.254 typename Container::const_iterator it;
1.255 };
1.256
1.257 + /// Alias for \c ValueIt
1.258 + typedef ValueIt ValueIterator;
1.259 +
1.260 /// \brief Returns an iterator to the first value.
1.261 ///
1.262 - /// Returns an stl compatible iterator to the
1.263 + /// Returns an STL compatible iterator to the
1.264 /// first value of the map. The values of the
1.265 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.266 /// range.
1.267 - ValueIterator beginValue() const {
1.268 - return ValueIterator(_inv_map.begin());
1.269 + ValueIt beginValue() const {
1.270 + return ValueIt(_inv_map.begin());
1.271 }
1.272
1.273 /// \brief Returns an iterator after the last value.
1.274 ///
1.275 - /// Returns an stl compatible iterator after the
1.276 + /// Returns an STL compatible iterator after the
1.277 /// last value of the map. The values of the
1.278 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.279 /// range.
1.280 - ValueIterator endValue() const {
1.281 - return ValueIterator(_inv_map.end());
1.282 + ValueIt endValue() const {
1.283 + return ValueIt(_inv_map.end());
1.284 }
1.285
1.286 /// \brief Sets the value associated with the given key.
1.287 @@ -2034,6 +2062,14 @@
1.288 return it != _inv_map.end() ? it->second : INVALID;
1.289 }
1.290
1.291 + /// \brief Returns the number of items with the given value.
1.292 + ///
1.293 + /// This function returns the number of items with the given value
1.294 + /// associated with it.
1.295 + int count(const Value &val) const {
1.296 + return _inv_map.count(val);
1.297 + }
1.298 +
1.299 protected:
1.300
1.301 /// \brief Erase the key from the map and the inverse map.
1.302 @@ -2083,10 +2119,12 @@
1.303
1.304 public:
1.305
1.306 - /// \brief The inverse map type.
1.307 + /// \brief The inverse map type of CrossRefMap.
1.308 ///
1.309 - /// The inverse of this map. The subscript operator of the map
1.310 - /// gives back the item that was last assigned to the value.
1.311 + /// The inverse map type of CrossRefMap. The subscript operator gives
1.312 + /// back an item by its value.
1.313 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.314 + /// \see inverse()
1.315 class InverseMap {
1.316 public:
1.317 /// \brief Constructor
1.318 @@ -2113,20 +2151,20 @@
1.319 const CrossRefMap& _inverted;
1.320 };
1.321
1.322 - /// \brief It gives back the read-only inverse map.
1.323 + /// \brief Gives back the inverse of the map.
1.324 ///
1.325 - /// It gives back the read-only inverse map.
1.326 + /// Gives back the inverse of the CrossRefMap.
1.327 InverseMap inverse() const {
1.328 return InverseMap(*this);
1.329 }
1.330
1.331 };
1.332
1.333 - /// \brief Provides continuous and unique ID for the
1.334 + /// \brief Provides continuous and unique id for the
1.335 /// items of a graph.
1.336 ///
1.337 /// RangeIdMap provides a unique and continuous
1.338 - /// ID for each item of a given type (\c Node, \c Arc or
1.339 + /// id for each item of a given type (\c Node, \c Arc or
1.340 /// \c Edge) in a graph. This id is
1.341 /// - \b unique: different items get different ids,
1.342 /// - \b continuous: the range of the ids is the set of integers
1.343 @@ -2137,7 +2175,7 @@
1.344 /// Thus this id is not (necessarily) the same as what can get using
1.345 /// the \c id() function of the graph or \ref IdMap.
1.346 /// This map can be inverted with its member class \c InverseMap,
1.347 - /// or with the \c operator() member.
1.348 + /// or with the \c operator()() member.
1.349 ///
1.350 /// \tparam GR The graph type.
1.351 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.352 @@ -2265,16 +2303,16 @@
1.353 _inv_map[pi] = q;
1.354 }
1.355
1.356 - /// \brief Gives back the \e RangeId of the item
1.357 + /// \brief Gives back the \e range \e id of the item
1.358 ///
1.359 - /// Gives back the \e RangeId of the item.
1.360 + /// Gives back the \e range \e id of the item.
1.361 int operator[](const Item& item) const {
1.362 return Map::operator[](item);
1.363 }
1.364
1.365 - /// \brief Gives back the item belonging to a \e RangeId
1.366 - ///
1.367 - /// Gives back the item belonging to a \e RangeId.
1.368 + /// \brief Gives back the item belonging to a \e range \e id
1.369 + ///
1.370 + /// Gives back the item belonging to the given \e range \e id.
1.371 Item operator()(int id) const {
1.372 return _inv_map[id];
1.373 }
1.374 @@ -2288,7 +2326,9 @@
1.375
1.376 /// \brief The inverse map type of RangeIdMap.
1.377 ///
1.378 - /// The inverse map type of RangeIdMap.
1.379 + /// The inverse map type of RangeIdMap. The subscript operator gives
1.380 + /// back an item by its \e range \e id.
1.381 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1.382 class InverseMap {
1.383 public:
1.384 /// \brief Constructor
1.385 @@ -2306,7 +2346,7 @@
1.386 /// \brief Subscript operator.
1.387 ///
1.388 /// Subscript operator. It gives back the item
1.389 - /// that the descriptor currently belongs to.
1.390 + /// that the given \e range \e id currently belongs to.
1.391 Value operator[](const Key& key) const {
1.392 return _inverted(key);
1.393 }
1.394 @@ -2324,12 +2364,932 @@
1.395
1.396 /// \brief Gives back the inverse of the map.
1.397 ///
1.398 - /// Gives back the inverse of the map.
1.399 + /// Gives back the inverse of the RangeIdMap.
1.400 const InverseMap inverse() const {
1.401 return InverseMap(*this);
1.402 }
1.403 };
1.404
1.405 + /// \brief Returns a \c RangeIdMap class.
1.406 + ///
1.407 + /// This function just returns an \c RangeIdMap class.
1.408 + /// \relates RangeIdMap
1.409 + template <typename K, typename GR>
1.410 + inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
1.411 + return RangeIdMap<GR, K>(graph);
1.412 + }
1.413 +
1.414 + /// \brief Dynamic iterable \c bool map.
1.415 + ///
1.416 + /// This class provides a special graph map type which can store a
1.417 + /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
1.418 + /// For both \c true and \c false values it is possible to iterate on
1.419 + /// the keys mapped to the value.
1.420 + ///
1.421 + /// This type is a reference map, so it can be modified with the
1.422 + /// subscript operator.
1.423 + ///
1.424 + /// \tparam GR The graph type.
1.425 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.426 + /// \c GR::Edge).
1.427 + ///
1.428 + /// \see IterableIntMap, IterableValueMap
1.429 + /// \see CrossRefMap
1.430 + template <typename GR, typename K>
1.431 + class IterableBoolMap
1.432 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
1.433 + private:
1.434 + typedef GR Graph;
1.435 +
1.436 + typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
1.437 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
1.438 +
1.439 + std::vector<K> _array;
1.440 + int _sep;
1.441 +
1.442 + public:
1.443 +
1.444 + /// Indicates that the map is reference map.
1.445 + typedef True ReferenceMapTag;
1.446 +
1.447 + /// The key type
1.448 + typedef K Key;
1.449 + /// The value type
1.450 + typedef bool Value;
1.451 + /// The const reference type.
1.452 + typedef const Value& ConstReference;
1.453 +
1.454 + private:
1.455 +
1.456 + int position(const Key& key) const {
1.457 + return Parent::operator[](key);
1.458 + }
1.459 +
1.460 + public:
1.461 +
1.462 + /// \brief Reference to the value of the map.
1.463 + ///
1.464 + /// This class is similar to the \c bool type. It can be converted to
1.465 + /// \c bool and it provides the same operators.
1.466 + class Reference {
1.467 + friend class IterableBoolMap;
1.468 + private:
1.469 + Reference(IterableBoolMap& map, const Key& key)
1.470 + : _key(key), _map(map) {}
1.471 + public:
1.472 +
1.473 + Reference& operator=(const Reference& value) {
1.474 + _map.set(_key, static_cast<bool>(value));
1.475 + return *this;
1.476 + }
1.477 +
1.478 + operator bool() const {
1.479 + return static_cast<const IterableBoolMap&>(_map)[_key];
1.480 + }
1.481 +
1.482 + Reference& operator=(bool value) {
1.483 + _map.set(_key, value);
1.484 + return *this;
1.485 + }
1.486 + Reference& operator&=(bool value) {
1.487 + _map.set(_key, _map[_key] & value);
1.488 + return *this;
1.489 + }
1.490 + Reference& operator|=(bool value) {
1.491 + _map.set(_key, _map[_key] | value);
1.492 + return *this;
1.493 + }
1.494 + Reference& operator^=(bool value) {
1.495 + _map.set(_key, _map[_key] ^ value);
1.496 + return *this;
1.497 + }
1.498 + private:
1.499 + Key _key;
1.500 + IterableBoolMap& _map;
1.501 + };
1.502 +
1.503 + /// \brief Constructor of the map with a default value.
1.504 + ///
1.505 + /// Constructor of the map with a default value.
1.506 + explicit IterableBoolMap(const Graph& graph, bool def = false)
1.507 + : Parent(graph) {
1.508 + typename Parent::Notifier* nf = Parent::notifier();
1.509 + Key it;
1.510 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.511 + Parent::set(it, _array.size());
1.512 + _array.push_back(it);
1.513 + }
1.514 + _sep = (def ? _array.size() : 0);
1.515 + }
1.516 +
1.517 + /// \brief Const subscript operator of the map.
1.518 + ///
1.519 + /// Const subscript operator of the map.
1.520 + bool operator[](const Key& key) const {
1.521 + return position(key) < _sep;
1.522 + }
1.523 +
1.524 + /// \brief Subscript operator of the map.
1.525 + ///
1.526 + /// Subscript operator of the map.
1.527 + Reference operator[](const Key& key) {
1.528 + return Reference(*this, key);
1.529 + }
1.530 +
1.531 + /// \brief Set operation of the map.
1.532 + ///
1.533 + /// Set operation of the map.
1.534 + void set(const Key& key, bool value) {
1.535 + int pos = position(key);
1.536 + if (value) {
1.537 + if (pos < _sep) return;
1.538 + Key tmp = _array[_sep];
1.539 + _array[_sep] = key;
1.540 + Parent::set(key, _sep);
1.541 + _array[pos] = tmp;
1.542 + Parent::set(tmp, pos);
1.543 + ++_sep;
1.544 + } else {
1.545 + if (pos >= _sep) return;
1.546 + --_sep;
1.547 + Key tmp = _array[_sep];
1.548 + _array[_sep] = key;
1.549 + Parent::set(key, _sep);
1.550 + _array[pos] = tmp;
1.551 + Parent::set(tmp, pos);
1.552 + }
1.553 + }
1.554 +
1.555 + /// \brief Set all items.
1.556 + ///
1.557 + /// Set all items in the map.
1.558 + /// \note Constant time operation.
1.559 + void setAll(bool value) {
1.560 + _sep = (value ? _array.size() : 0);
1.561 + }
1.562 +
1.563 + /// \brief Returns the number of the keys mapped to \c true.
1.564 + ///
1.565 + /// Returns the number of the keys mapped to \c true.
1.566 + int trueNum() const {
1.567 + return _sep;
1.568 + }
1.569 +
1.570 + /// \brief Returns the number of the keys mapped to \c false.
1.571 + ///
1.572 + /// Returns the number of the keys mapped to \c false.
1.573 + int falseNum() const {
1.574 + return _array.size() - _sep;
1.575 + }
1.576 +
1.577 + /// \brief Iterator for the keys mapped to \c true.
1.578 + ///
1.579 + /// Iterator for the keys mapped to \c true. It works
1.580 + /// like a graph item iterator, it can be converted to
1.581 + /// the key type of the map, incremented with \c ++ operator, and
1.582 + /// if the iterator leaves the last valid key, it will be equal to
1.583 + /// \c INVALID.
1.584 + class TrueIt : public Key {
1.585 + public:
1.586 + typedef Key Parent;
1.587 +
1.588 + /// \brief Creates an iterator.
1.589 + ///
1.590 + /// Creates an iterator. It iterates on the
1.591 + /// keys mapped to \c true.
1.592 + /// \param map The IterableBoolMap.
1.593 + explicit TrueIt(const IterableBoolMap& map)
1.594 + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
1.595 + _map(&map) {}
1.596 +
1.597 + /// \brief Invalid constructor \& conversion.
1.598 + ///
1.599 + /// This constructor initializes the iterator to be invalid.
1.600 + /// \sa Invalid for more details.
1.601 + TrueIt(Invalid) : Parent(INVALID), _map(0) {}
1.602 +
1.603 + /// \brief Increment operator.
1.604 + ///
1.605 + /// Increment operator.
1.606 + TrueIt& operator++() {
1.607 + int pos = _map->position(*this);
1.608 + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
1.609 + return *this;
1.610 + }
1.611 +
1.612 + private:
1.613 + const IterableBoolMap* _map;
1.614 + };
1.615 +
1.616 + /// \brief Iterator for the keys mapped to \c false.
1.617 + ///
1.618 + /// Iterator for the keys mapped to \c false. It works
1.619 + /// like a graph item iterator, it can be converted to
1.620 + /// the key type of the map, incremented with \c ++ operator, and
1.621 + /// if the iterator leaves the last valid key, it will be equal to
1.622 + /// \c INVALID.
1.623 + class FalseIt : public Key {
1.624 + public:
1.625 + typedef Key Parent;
1.626 +
1.627 + /// \brief Creates an iterator.
1.628 + ///
1.629 + /// Creates an iterator. It iterates on the
1.630 + /// keys mapped to \c false.
1.631 + /// \param map The IterableBoolMap.
1.632 + explicit FalseIt(const IterableBoolMap& map)
1.633 + : Parent(map._sep < int(map._array.size()) ?
1.634 + map._array.back() : INVALID), _map(&map) {}
1.635 +
1.636 + /// \brief Invalid constructor \& conversion.
1.637 + ///
1.638 + /// This constructor initializes the iterator to be invalid.
1.639 + /// \sa Invalid for more details.
1.640 + FalseIt(Invalid) : Parent(INVALID), _map(0) {}
1.641 +
1.642 + /// \brief Increment operator.
1.643 + ///
1.644 + /// Increment operator.
1.645 + FalseIt& operator++() {
1.646 + int pos = _map->position(*this);
1.647 + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
1.648 + return *this;
1.649 + }
1.650 +
1.651 + private:
1.652 + const IterableBoolMap* _map;
1.653 + };
1.654 +
1.655 + /// \brief Iterator for the keys mapped to a given value.
1.656 + ///
1.657 + /// Iterator for the keys mapped to a given value. It works
1.658 + /// like a graph item iterator, it can be converted to
1.659 + /// the key type of the map, incremented with \c ++ operator, and
1.660 + /// if the iterator leaves the last valid key, it will be equal to
1.661 + /// \c INVALID.
1.662 + class ItemIt : public Key {
1.663 + public:
1.664 + typedef Key Parent;
1.665 +
1.666 + /// \brief Creates an iterator with a value.
1.667 + ///
1.668 + /// Creates an iterator with a value. It iterates on the
1.669 + /// keys mapped to the given value.
1.670 + /// \param map The IterableBoolMap.
1.671 + /// \param value The value.
1.672 + ItemIt(const IterableBoolMap& map, bool value)
1.673 + : Parent(value ?
1.674 + (map._sep > 0 ?
1.675 + map._array[map._sep - 1] : INVALID) :
1.676 + (map._sep < int(map._array.size()) ?
1.677 + map._array.back() : INVALID)), _map(&map) {}
1.678 +
1.679 + /// \brief Invalid constructor \& conversion.
1.680 + ///
1.681 + /// This constructor initializes the iterator to be invalid.
1.682 + /// \sa Invalid for more details.
1.683 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.684 +
1.685 + /// \brief Increment operator.
1.686 + ///
1.687 + /// Increment operator.
1.688 + ItemIt& operator++() {
1.689 + int pos = _map->position(*this);
1.690 + int _sep = pos >= _map->_sep ? _map->_sep : 0;
1.691 + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
1.692 + return *this;
1.693 + }
1.694 +
1.695 + private:
1.696 + const IterableBoolMap* _map;
1.697 + };
1.698 +
1.699 + protected:
1.700 +
1.701 + virtual void add(const Key& key) {
1.702 + Parent::add(key);
1.703 + Parent::set(key, _array.size());
1.704 + _array.push_back(key);
1.705 + }
1.706 +
1.707 + virtual void add(const std::vector<Key>& keys) {
1.708 + Parent::add(keys);
1.709 + for (int i = 0; i < int(keys.size()); ++i) {
1.710 + Parent::set(keys[i], _array.size());
1.711 + _array.push_back(keys[i]);
1.712 + }
1.713 + }
1.714 +
1.715 + virtual void erase(const Key& key) {
1.716 + int pos = position(key);
1.717 + if (pos < _sep) {
1.718 + --_sep;
1.719 + Parent::set(_array[_sep], pos);
1.720 + _array[pos] = _array[_sep];
1.721 + Parent::set(_array.back(), _sep);
1.722 + _array[_sep] = _array.back();
1.723 + _array.pop_back();
1.724 + } else {
1.725 + Parent::set(_array.back(), pos);
1.726 + _array[pos] = _array.back();
1.727 + _array.pop_back();
1.728 + }
1.729 + Parent::erase(key);
1.730 + }
1.731 +
1.732 + virtual void erase(const std::vector<Key>& keys) {
1.733 + for (int i = 0; i < int(keys.size()); ++i) {
1.734 + int pos = position(keys[i]);
1.735 + if (pos < _sep) {
1.736 + --_sep;
1.737 + Parent::set(_array[_sep], pos);
1.738 + _array[pos] = _array[_sep];
1.739 + Parent::set(_array.back(), _sep);
1.740 + _array[_sep] = _array.back();
1.741 + _array.pop_back();
1.742 + } else {
1.743 + Parent::set(_array.back(), pos);
1.744 + _array[pos] = _array.back();
1.745 + _array.pop_back();
1.746 + }
1.747 + }
1.748 + Parent::erase(keys);
1.749 + }
1.750 +
1.751 + virtual void build() {
1.752 + Parent::build();
1.753 + typename Parent::Notifier* nf = Parent::notifier();
1.754 + Key it;
1.755 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.756 + Parent::set(it, _array.size());
1.757 + _array.push_back(it);
1.758 + }
1.759 + _sep = 0;
1.760 + }
1.761 +
1.762 + virtual void clear() {
1.763 + _array.clear();
1.764 + _sep = 0;
1.765 + Parent::clear();
1.766 + }
1.767 +
1.768 + };
1.769 +
1.770 +
1.771 + namespace _maps_bits {
1.772 + template <typename Item>
1.773 + struct IterableIntMapNode {
1.774 + IterableIntMapNode() : value(-1) {}
1.775 + IterableIntMapNode(int _value) : value(_value) {}
1.776 + Item prev, next;
1.777 + int value;
1.778 + };
1.779 + }
1.780 +
1.781 + /// \brief Dynamic iterable integer map.
1.782 + ///
1.783 + /// This class provides a special graph map type which can store an
1.784 + /// integer value for graph items (\c Node, \c Arc or \c Edge).
1.785 + /// For each non-negative value it is possible to iterate on the keys
1.786 + /// mapped to the value.
1.787 + ///
1.788 + /// This map is intended to be used with small integer values, for which
1.789 + /// it is efficient, and supports iteration only for non-negative values.
1.790 + /// If you need large values and/or iteration for negative integers,
1.791 + /// consider to use \ref IterableValueMap instead.
1.792 + ///
1.793 + /// This type is a reference map, so it can be modified with the
1.794 + /// subscript operator.
1.795 + ///
1.796 + /// \note The size of the data structure depends on the largest
1.797 + /// value in the map.
1.798 + ///
1.799 + /// \tparam GR The graph type.
1.800 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.801 + /// \c GR::Edge).
1.802 + ///
1.803 + /// \see IterableBoolMap, IterableValueMap
1.804 + /// \see CrossRefMap
1.805 + template <typename GR, typename K>
1.806 + class IterableIntMap
1.807 + : protected ItemSetTraits<GR, K>::
1.808 + template Map<_maps_bits::IterableIntMapNode<K> >::Type {
1.809 + public:
1.810 + typedef typename ItemSetTraits<GR, K>::
1.811 + template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
1.812 +
1.813 + /// The key type
1.814 + typedef K Key;
1.815 + /// The value type
1.816 + typedef int Value;
1.817 + /// The graph type
1.818 + typedef GR Graph;
1.819 +
1.820 + /// \brief Constructor of the map.
1.821 + ///
1.822 + /// Constructor of the map. It sets all values to -1.
1.823 + explicit IterableIntMap(const Graph& graph)
1.824 + : Parent(graph) {}
1.825 +
1.826 + /// \brief Constructor of the map with a given value.
1.827 + ///
1.828 + /// Constructor of the map with a given value.
1.829 + explicit IterableIntMap(const Graph& graph, int value)
1.830 + : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
1.831 + if (value >= 0) {
1.832 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.833 + lace(it);
1.834 + }
1.835 + }
1.836 + }
1.837 +
1.838 + private:
1.839 +
1.840 + void unlace(const Key& key) {
1.841 + typename Parent::Value& node = Parent::operator[](key);
1.842 + if (node.value < 0) return;
1.843 + if (node.prev != INVALID) {
1.844 + Parent::operator[](node.prev).next = node.next;
1.845 + } else {
1.846 + _first[node.value] = node.next;
1.847 + }
1.848 + if (node.next != INVALID) {
1.849 + Parent::operator[](node.next).prev = node.prev;
1.850 + }
1.851 + while (!_first.empty() && _first.back() == INVALID) {
1.852 + _first.pop_back();
1.853 + }
1.854 + }
1.855 +
1.856 + void lace(const Key& key) {
1.857 + typename Parent::Value& node = Parent::operator[](key);
1.858 + if (node.value < 0) return;
1.859 + if (node.value >= int(_first.size())) {
1.860 + _first.resize(node.value + 1, INVALID);
1.861 + }
1.862 + node.prev = INVALID;
1.863 + node.next = _first[node.value];
1.864 + if (node.next != INVALID) {
1.865 + Parent::operator[](node.next).prev = key;
1.866 + }
1.867 + _first[node.value] = key;
1.868 + }
1.869 +
1.870 + public:
1.871 +
1.872 + /// Indicates that the map is reference map.
1.873 + typedef True ReferenceMapTag;
1.874 +
1.875 + /// \brief Reference to the value of the map.
1.876 + ///
1.877 + /// This class is similar to the \c int type. It can
1.878 + /// be converted to \c int and it has the same operators.
1.879 + class Reference {
1.880 + friend class IterableIntMap;
1.881 + private:
1.882 + Reference(IterableIntMap& map, const Key& key)
1.883 + : _key(key), _map(map) {}
1.884 + public:
1.885 +
1.886 + Reference& operator=(const Reference& value) {
1.887 + _map.set(_key, static_cast<const int&>(value));
1.888 + return *this;
1.889 + }
1.890 +
1.891 + operator const int&() const {
1.892 + return static_cast<const IterableIntMap&>(_map)[_key];
1.893 + }
1.894 +
1.895 + Reference& operator=(int value) {
1.896 + _map.set(_key, value);
1.897 + return *this;
1.898 + }
1.899 + Reference& operator++() {
1.900 + _map.set(_key, _map[_key] + 1);
1.901 + return *this;
1.902 + }
1.903 + int operator++(int) {
1.904 + int value = _map[_key];
1.905 + _map.set(_key, value + 1);
1.906 + return value;
1.907 + }
1.908 + Reference& operator--() {
1.909 + _map.set(_key, _map[_key] - 1);
1.910 + return *this;
1.911 + }
1.912 + int operator--(int) {
1.913 + int value = _map[_key];
1.914 + _map.set(_key, value - 1);
1.915 + return value;
1.916 + }
1.917 + Reference& operator+=(int value) {
1.918 + _map.set(_key, _map[_key] + value);
1.919 + return *this;
1.920 + }
1.921 + Reference& operator-=(int value) {
1.922 + _map.set(_key, _map[_key] - value);
1.923 + return *this;
1.924 + }
1.925 + Reference& operator*=(int value) {
1.926 + _map.set(_key, _map[_key] * value);
1.927 + return *this;
1.928 + }
1.929 + Reference& operator/=(int value) {
1.930 + _map.set(_key, _map[_key] / value);
1.931 + return *this;
1.932 + }
1.933 + Reference& operator%=(int value) {
1.934 + _map.set(_key, _map[_key] % value);
1.935 + return *this;
1.936 + }
1.937 + Reference& operator&=(int value) {
1.938 + _map.set(_key, _map[_key] & value);
1.939 + return *this;
1.940 + }
1.941 + Reference& operator|=(int value) {
1.942 + _map.set(_key, _map[_key] | value);
1.943 + return *this;
1.944 + }
1.945 + Reference& operator^=(int value) {
1.946 + _map.set(_key, _map[_key] ^ value);
1.947 + return *this;
1.948 + }
1.949 + Reference& operator<<=(int value) {
1.950 + _map.set(_key, _map[_key] << value);
1.951 + return *this;
1.952 + }
1.953 + Reference& operator>>=(int value) {
1.954 + _map.set(_key, _map[_key] >> value);
1.955 + return *this;
1.956 + }
1.957 +
1.958 + private:
1.959 + Key _key;
1.960 + IterableIntMap& _map;
1.961 + };
1.962 +
1.963 + /// The const reference type.
1.964 + typedef const Value& ConstReference;
1.965 +
1.966 + /// \brief Gives back the maximal value plus one.
1.967 + ///
1.968 + /// Gives back the maximal value plus one.
1.969 + int size() const {
1.970 + return _first.size();
1.971 + }
1.972 +
1.973 + /// \brief Set operation of the map.
1.974 + ///
1.975 + /// Set operation of the map.
1.976 + void set(const Key& key, const Value& value) {
1.977 + unlace(key);
1.978 + Parent::operator[](key).value = value;
1.979 + lace(key);
1.980 + }
1.981 +
1.982 + /// \brief Const subscript operator of the map.
1.983 + ///
1.984 + /// Const subscript operator of the map.
1.985 + const Value& operator[](const Key& key) const {
1.986 + return Parent::operator[](key).value;
1.987 + }
1.988 +
1.989 + /// \brief Subscript operator of the map.
1.990 + ///
1.991 + /// Subscript operator of the map.
1.992 + Reference operator[](const Key& key) {
1.993 + return Reference(*this, key);
1.994 + }
1.995 +
1.996 + /// \brief Iterator for the keys with the same value.
1.997 + ///
1.998 + /// Iterator for the keys with the same value. It works
1.999 + /// like a graph item iterator, it can be converted to
1.1000 + /// the item type of the map, incremented with \c ++ operator, and
1.1001 + /// if the iterator leaves the last valid item, it will be equal to
1.1002 + /// \c INVALID.
1.1003 + class ItemIt : public Key {
1.1004 + public:
1.1005 + typedef Key Parent;
1.1006 +
1.1007 + /// \brief Invalid constructor \& conversion.
1.1008 + ///
1.1009 + /// This constructor initializes the iterator to be invalid.
1.1010 + /// \sa Invalid for more details.
1.1011 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.1012 +
1.1013 + /// \brief Creates an iterator with a value.
1.1014 + ///
1.1015 + /// Creates an iterator with a value. It iterates on the
1.1016 + /// keys mapped to the given value.
1.1017 + /// \param map The IterableIntMap.
1.1018 + /// \param value The value.
1.1019 + ItemIt(const IterableIntMap& map, int value) : _map(&map) {
1.1020 + if (value < 0 || value >= int(_map->_first.size())) {
1.1021 + Parent::operator=(INVALID);
1.1022 + } else {
1.1023 + Parent::operator=(_map->_first[value]);
1.1024 + }
1.1025 + }
1.1026 +
1.1027 + /// \brief Increment operator.
1.1028 + ///
1.1029 + /// Increment operator.
1.1030 + ItemIt& operator++() {
1.1031 + Parent::operator=(_map->IterableIntMap::Parent::
1.1032 + operator[](static_cast<Parent&>(*this)).next);
1.1033 + return *this;
1.1034 + }
1.1035 +
1.1036 + private:
1.1037 + const IterableIntMap* _map;
1.1038 + };
1.1039 +
1.1040 + protected:
1.1041 +
1.1042 + virtual void erase(const Key& key) {
1.1043 + unlace(key);
1.1044 + Parent::erase(key);
1.1045 + }
1.1046 +
1.1047 + virtual void erase(const std::vector<Key>& keys) {
1.1048 + for (int i = 0; i < int(keys.size()); ++i) {
1.1049 + unlace(keys[i]);
1.1050 + }
1.1051 + Parent::erase(keys);
1.1052 + }
1.1053 +
1.1054 + virtual void clear() {
1.1055 + _first.clear();
1.1056 + Parent::clear();
1.1057 + }
1.1058 +
1.1059 + private:
1.1060 + std::vector<Key> _first;
1.1061 + };
1.1062 +
1.1063 + namespace _maps_bits {
1.1064 + template <typename Item, typename Value>
1.1065 + struct IterableValueMapNode {
1.1066 + IterableValueMapNode(Value _value = Value()) : value(_value) {}
1.1067 + Item prev, next;
1.1068 + Value value;
1.1069 + };
1.1070 + }
1.1071 +
1.1072 + /// \brief Dynamic iterable map for comparable values.
1.1073 + ///
1.1074 + /// This class provides a special graph map type which can store a
1.1075 + /// comparable value for graph items (\c Node, \c Arc or \c Edge).
1.1076 + /// For each value it is possible to iterate on the keys mapped to
1.1077 + /// the value (\c ItemIt), and the values of the map can be accessed
1.1078 + /// with an STL compatible forward iterator (\c ValueIt).
1.1079 + /// The map stores a linked list for each value, which contains
1.1080 + /// the items mapped to the value, and the used values are stored
1.1081 + /// in balanced binary tree (\c std::map).
1.1082 + ///
1.1083 + /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
1.1084 + /// specialized for \c bool and \c int values, respectively.
1.1085 + ///
1.1086 + /// This type is not reference map, so it cannot be modified with
1.1087 + /// the subscript operator.
1.1088 + ///
1.1089 + /// \tparam GR The graph type.
1.1090 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1.1091 + /// \c GR::Edge).
1.1092 + /// \tparam V The value type of the map. It can be any comparable
1.1093 + /// value type.
1.1094 + ///
1.1095 + /// \see IterableBoolMap, IterableIntMap
1.1096 + /// \see CrossRefMap
1.1097 + template <typename GR, typename K, typename V>
1.1098 + class IterableValueMap
1.1099 + : protected ItemSetTraits<GR, K>::
1.1100 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
1.1101 + public:
1.1102 + typedef typename ItemSetTraits<GR, K>::
1.1103 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
1.1104 +
1.1105 + /// The key type
1.1106 + typedef K Key;
1.1107 + /// The value type
1.1108 + typedef V Value;
1.1109 + /// The graph type
1.1110 + typedef GR Graph;
1.1111 +
1.1112 + public:
1.1113 +
1.1114 + /// \brief Constructor of the map with a given value.
1.1115 + ///
1.1116 + /// Constructor of the map with a given value.
1.1117 + explicit IterableValueMap(const Graph& graph,
1.1118 + const Value& value = Value())
1.1119 + : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
1.1120 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.1121 + lace(it);
1.1122 + }
1.1123 + }
1.1124 +
1.1125 + protected:
1.1126 +
1.1127 + void unlace(const Key& key) {
1.1128 + typename Parent::Value& node = Parent::operator[](key);
1.1129 + if (node.prev != INVALID) {
1.1130 + Parent::operator[](node.prev).next = node.next;
1.1131 + } else {
1.1132 + if (node.next != INVALID) {
1.1133 + _first[node.value] = node.next;
1.1134 + } else {
1.1135 + _first.erase(node.value);
1.1136 + }
1.1137 + }
1.1138 + if (node.next != INVALID) {
1.1139 + Parent::operator[](node.next).prev = node.prev;
1.1140 + }
1.1141 + }
1.1142 +
1.1143 + void lace(const Key& key) {
1.1144 + typename Parent::Value& node = Parent::operator[](key);
1.1145 + typename std::map<Value, Key>::iterator it = _first.find(node.value);
1.1146 + if (it == _first.end()) {
1.1147 + node.prev = node.next = INVALID;
1.1148 + _first.insert(std::make_pair(node.value, key));
1.1149 + } else {
1.1150 + node.prev = INVALID;
1.1151 + node.next = it->second;
1.1152 + if (node.next != INVALID) {
1.1153 + Parent::operator[](node.next).prev = key;
1.1154 + }
1.1155 + it->second = key;
1.1156 + }
1.1157 + }
1.1158 +
1.1159 + public:
1.1160 +
1.1161 + /// \brief Forward iterator for values.
1.1162 + ///
1.1163 + /// This iterator is an STL compatible forward
1.1164 + /// iterator on the values of the map. The values can
1.1165 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1.1166 + class ValueIt
1.1167 + : public std::iterator<std::forward_iterator_tag, Value> {
1.1168 + friend class IterableValueMap;
1.1169 + private:
1.1170 + ValueIt(typename std::map<Value, Key>::const_iterator _it)
1.1171 + : it(_it) {}
1.1172 + public:
1.1173 +
1.1174 + /// Constructor
1.1175 + ValueIt() {}
1.1176 +
1.1177 + /// \e
1.1178 + ValueIt& operator++() { ++it; return *this; }
1.1179 + /// \e
1.1180 + ValueIt operator++(int) {
1.1181 + ValueIt tmp(*this);
1.1182 + operator++();
1.1183 + return tmp;
1.1184 + }
1.1185 +
1.1186 + /// \e
1.1187 + const Value& operator*() const { return it->first; }
1.1188 + /// \e
1.1189 + const Value* operator->() const { return &(it->first); }
1.1190 +
1.1191 + /// \e
1.1192 + bool operator==(ValueIt jt) const { return it == jt.it; }
1.1193 + /// \e
1.1194 + bool operator!=(ValueIt jt) const { return it != jt.it; }
1.1195 +
1.1196 + private:
1.1197 + typename std::map<Value, Key>::const_iterator it;
1.1198 + };
1.1199 +
1.1200 + /// \brief Returns an iterator to the first value.
1.1201 + ///
1.1202 + /// Returns an STL compatible iterator to the
1.1203 + /// first value of the map. The values of the
1.1204 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.1205 + /// range.
1.1206 + ValueIt beginValue() const {
1.1207 + return ValueIt(_first.begin());
1.1208 + }
1.1209 +
1.1210 + /// \brief Returns an iterator after the last value.
1.1211 + ///
1.1212 + /// Returns an STL compatible iterator after the
1.1213 + /// last value of the map. The values of the
1.1214 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1.1215 + /// range.
1.1216 + ValueIt endValue() const {
1.1217 + return ValueIt(_first.end());
1.1218 + }
1.1219 +
1.1220 + /// \brief Set operation of the map.
1.1221 + ///
1.1222 + /// Set operation of the map.
1.1223 + void set(const Key& key, const Value& value) {
1.1224 + unlace(key);
1.1225 + Parent::operator[](key).value = value;
1.1226 + lace(key);
1.1227 + }
1.1228 +
1.1229 + /// \brief Const subscript operator of the map.
1.1230 + ///
1.1231 + /// Const subscript operator of the map.
1.1232 + const Value& operator[](const Key& key) const {
1.1233 + return Parent::operator[](key).value;
1.1234 + }
1.1235 +
1.1236 + /// \brief Iterator for the keys with the same value.
1.1237 + ///
1.1238 + /// Iterator for the keys with the same value. It works
1.1239 + /// like a graph item iterator, it can be converted to
1.1240 + /// the item type of the map, incremented with \c ++ operator, and
1.1241 + /// if the iterator leaves the last valid item, it will be equal to
1.1242 + /// \c INVALID.
1.1243 + class ItemIt : public Key {
1.1244 + public:
1.1245 + typedef Key Parent;
1.1246 +
1.1247 + /// \brief Invalid constructor \& conversion.
1.1248 + ///
1.1249 + /// This constructor initializes the iterator to be invalid.
1.1250 + /// \sa Invalid for more details.
1.1251 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
1.1252 +
1.1253 + /// \brief Creates an iterator with a value.
1.1254 + ///
1.1255 + /// Creates an iterator with a value. It iterates on the
1.1256 + /// keys which have the given value.
1.1257 + /// \param map The IterableValueMap
1.1258 + /// \param value The value
1.1259 + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
1.1260 + typename std::map<Value, Key>::const_iterator it =
1.1261 + map._first.find(value);
1.1262 + if (it == map._first.end()) {
1.1263 + Parent::operator=(INVALID);
1.1264 + } else {
1.1265 + Parent::operator=(it->second);
1.1266 + }
1.1267 + }
1.1268 +
1.1269 + /// \brief Increment operator.
1.1270 + ///
1.1271 + /// Increment Operator.
1.1272 + ItemIt& operator++() {
1.1273 + Parent::operator=(_map->IterableValueMap::Parent::
1.1274 + operator[](static_cast<Parent&>(*this)).next);
1.1275 + return *this;
1.1276 + }
1.1277 +
1.1278 +
1.1279 + private:
1.1280 + const IterableValueMap* _map;
1.1281 + };
1.1282 +
1.1283 + protected:
1.1284 +
1.1285 + virtual void add(const Key& key) {
1.1286 + Parent::add(key);
1.1287 + lace(key);
1.1288 + }
1.1289 +
1.1290 + virtual void add(const std::vector<Key>& keys) {
1.1291 + Parent::add(keys);
1.1292 + for (int i = 0; i < int(keys.size()); ++i) {
1.1293 + lace(keys[i]);
1.1294 + }
1.1295 + }
1.1296 +
1.1297 + virtual void erase(const Key& key) {
1.1298 + unlace(key);
1.1299 + Parent::erase(key);
1.1300 + }
1.1301 +
1.1302 + virtual void erase(const std::vector<Key>& keys) {
1.1303 + for (int i = 0; i < int(keys.size()); ++i) {
1.1304 + unlace(keys[i]);
1.1305 + }
1.1306 + Parent::erase(keys);
1.1307 + }
1.1308 +
1.1309 + virtual void build() {
1.1310 + Parent::build();
1.1311 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
1.1312 + lace(it);
1.1313 + }
1.1314 + }
1.1315 +
1.1316 + virtual void clear() {
1.1317 + _first.clear();
1.1318 + Parent::clear();
1.1319 + }
1.1320 +
1.1321 + private:
1.1322 + std::map<Value, Key> _first;
1.1323 + };
1.1324 +
1.1325 /// \brief Map of the source nodes of arcs in a digraph.
1.1326 ///
1.1327 /// SourceMap provides access for the source node of each arc in a digraph,
1.1328 @@ -2340,9 +3300,9 @@
1.1329 class SourceMap {
1.1330 public:
1.1331
1.1332 - ///\e
1.1333 + /// The key type (the \c Arc type of the digraph).
1.1334 typedef typename GR::Arc Key;
1.1335 - ///\e
1.1336 + /// The value type (the \c Node type of the digraph).
1.1337 typedef typename GR::Node Value;
1.1338
1.1339 /// \brief Constructor
1.1340 @@ -2381,9 +3341,9 @@
1.1341 class TargetMap {
1.1342 public:
1.1343
1.1344 - ///\e
1.1345 + /// The key type (the \c Arc type of the digraph).
1.1346 typedef typename GR::Arc Key;
1.1347 - ///\e
1.1348 + /// The value type (the \c Node type of the digraph).
1.1349 typedef typename GR::Node Value;
1.1350
1.1351 /// \brief Constructor
1.1352 @@ -2423,8 +3383,10 @@
1.1353 class ForwardMap {
1.1354 public:
1.1355
1.1356 + /// The key type (the \c Edge type of the digraph).
1.1357 + typedef typename GR::Edge Key;
1.1358 + /// The value type (the \c Arc type of the digraph).
1.1359 typedef typename GR::Arc Value;
1.1360 - typedef typename GR::Edge Key;
1.1361
1.1362 /// \brief Constructor
1.1363 ///
1.1364 @@ -2463,8 +3425,10 @@
1.1365 class BackwardMap {
1.1366 public:
1.1367
1.1368 + /// The key type (the \c Edge type of the digraph).
1.1369 + typedef typename GR::Edge Key;
1.1370 + /// The value type (the \c Arc type of the digraph).
1.1371 typedef typename GR::Arc Value;
1.1372 - typedef typename GR::Edge Key;
1.1373
1.1374 /// \brief Constructor
1.1375 ///
1.1376 @@ -2499,10 +3463,10 @@
1.1377 /// in constant time. On the other hand, the values are updated automatically
1.1378 /// whenever the digraph changes.
1.1379 ///
1.1380 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1381 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1382 /// may provide alternative ways to modify the digraph.
1.1383 /// The correct behavior of InDegMap is not guarantied if these additional
1.1384 - /// features are used. For example the functions
1.1385 + /// features are used. For example, the functions
1.1386 /// \ref ListDigraph::changeSource() "changeSource()",
1.1387 /// \ref ListDigraph::changeTarget() "changeTarget()" and
1.1388 /// \ref ListDigraph::reverseArc() "reverseArc()"
1.1389 @@ -2515,7 +3479,7 @@
1.1390 ::ItemNotifier::ObserverBase {
1.1391
1.1392 public:
1.1393 -
1.1394 +
1.1395 /// The graph type of InDegMap
1.1396 typedef GR Graph;
1.1397 typedef GR Digraph;
1.1398 @@ -2629,10 +3593,10 @@
1.1399 /// in constant time. On the other hand, the values are updated automatically
1.1400 /// whenever the digraph changes.
1.1401 ///
1.1402 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1403 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
1.1404 /// may provide alternative ways to modify the digraph.
1.1405 /// The correct behavior of OutDegMap is not guarantied if these additional
1.1406 - /// features are used. For example the functions
1.1407 + /// features are used. For example, the functions
1.1408 /// \ref ListDigraph::changeSource() "changeSource()",
1.1409 /// \ref ListDigraph::changeTarget() "changeTarget()" and
1.1410 /// \ref ListDigraph::reverseArc() "reverseArc()"
1.1411 @@ -2800,6 +3764,293 @@
1.1412 return PotentialDifferenceMap<GR, POT>(gr, potential);
1.1413 }
1.1414
1.1415 +
1.1416 + /// \brief Copy the values of a graph map to another map.
1.1417 + ///
1.1418 + /// This function copies the values of a graph map to another graph map.
1.1419 + /// \c To::Key must be equal or convertible to \c From::Key and
1.1420 + /// \c From::Value must be equal or convertible to \c To::Value.
1.1421 + ///
1.1422 + /// For example, an edge map of \c int value type can be copied to
1.1423 + /// an arc map of \c double value type in an undirected graph, but
1.1424 + /// an arc map cannot be copied to an edge map.
1.1425 + /// Note that even a \ref ConstMap can be copied to a standard graph map,
1.1426 + /// but \ref mapFill() can also be used for this purpose.
1.1427 + ///
1.1428 + /// \param gr The graph for which the maps are defined.
1.1429 + /// \param from The map from which the values have to be copied.
1.1430 + /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
1.1431 + /// \param to The map to which the values have to be copied.
1.1432 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
1.1433 + template <typename GR, typename From, typename To>
1.1434 + void mapCopy(const GR& gr, const From& from, To& to) {
1.1435 + typedef typename To::Key Item;
1.1436 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1437 +
1.1438 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1439 + to.set(it, from[it]);
1.1440 + }
1.1441 + }
1.1442 +
1.1443 + /// \brief Compare two graph maps.
1.1444 + ///
1.1445 + /// This function compares the values of two graph maps. It returns
1.1446 + /// \c true if the maps assign the same value for all items in the graph.
1.1447 + /// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
1.1448 + /// and their \c Value types must be comparable using \c %operator==().
1.1449 + ///
1.1450 + /// \param gr The graph for which the maps are defined.
1.1451 + /// \param map1 The first map.
1.1452 + /// \param map2 The second map.
1.1453 + template <typename GR, typename Map1, typename Map2>
1.1454 + bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
1.1455 + typedef typename Map2::Key Item;
1.1456 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1457 +
1.1458 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1459 + if (!(map1[it] == map2[it])) return false;
1.1460 + }
1.1461 + return true;
1.1462 + }
1.1463 +
1.1464 + /// \brief Return an item having minimum value of a graph map.
1.1465 + ///
1.1466 + /// This function returns an item (\c Node, \c Arc or \c Edge) having
1.1467 + /// minimum value of the given graph map.
1.1468 + /// If the item set is empty, it returns \c INVALID.
1.1469 + ///
1.1470 + /// \param gr The graph for which the map is defined.
1.1471 + /// \param map The graph map.
1.1472 + template <typename GR, typename Map>
1.1473 + typename Map::Key mapMin(const GR& gr, const Map& map) {
1.1474 + return mapMin(gr, map, std::less<typename Map::Value>());
1.1475 + }
1.1476 +
1.1477 + /// \brief Return an item having minimum value of a graph map.
1.1478 + ///
1.1479 + /// This function returns an item (\c Node, \c Arc or \c Edge) having
1.1480 + /// minimum value of the given graph map.
1.1481 + /// If the item set is empty, it returns \c INVALID.
1.1482 + ///
1.1483 + /// \param gr The graph for which the map is defined.
1.1484 + /// \param map The graph map.
1.1485 + /// \param comp Comparison function object.
1.1486 + template <typename GR, typename Map, typename Comp>
1.1487 + typename Map::Key mapMin(const GR& gr, const Map& map, const Comp& comp) {
1.1488 + typedef typename Map::Key Item;
1.1489 + typedef typename Map::Value Value;
1.1490 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1491 +
1.1492 + ItemIt min_item(gr);
1.1493 + if (min_item == INVALID) return INVALID;
1.1494 + Value min = map[min_item];
1.1495 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1496 + if (comp(map[it], min)) {
1.1497 + min = map[it];
1.1498 + min_item = it;
1.1499 + }
1.1500 + }
1.1501 + return min_item;
1.1502 + }
1.1503 +
1.1504 + /// \brief Return an item having maximum value of a graph map.
1.1505 + ///
1.1506 + /// This function returns an item (\c Node, \c Arc or \c Edge) having
1.1507 + /// maximum value of the given graph map.
1.1508 + /// If the item set is empty, it returns \c INVALID.
1.1509 + ///
1.1510 + /// \param gr The graph for which the map is defined.
1.1511 + /// \param map The graph map.
1.1512 + template <typename GR, typename Map>
1.1513 + typename Map::Key mapMax(const GR& gr, const Map& map) {
1.1514 + return mapMax(gr, map, std::less<typename Map::Value>());
1.1515 + }
1.1516 +
1.1517 + /// \brief Return an item having maximum value of a graph map.
1.1518 + ///
1.1519 + /// This function returns an item (\c Node, \c Arc or \c Edge) having
1.1520 + /// maximum value of the given graph map.
1.1521 + /// If the item set is empty, it returns \c INVALID.
1.1522 + ///
1.1523 + /// \param gr The graph for which the map is defined.
1.1524 + /// \param map The graph map.
1.1525 + /// \param comp Comparison function object.
1.1526 + template <typename GR, typename Map, typename Comp>
1.1527 + typename Map::Key mapMax(const GR& gr, const Map& map, const Comp& comp) {
1.1528 + typedef typename Map::Key Item;
1.1529 + typedef typename Map::Value Value;
1.1530 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1531 +
1.1532 + ItemIt max_item(gr);
1.1533 + if (max_item == INVALID) return INVALID;
1.1534 + Value max = map[max_item];
1.1535 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1536 + if (comp(max, map[it])) {
1.1537 + max = map[it];
1.1538 + max_item = it;
1.1539 + }
1.1540 + }
1.1541 + return max_item;
1.1542 + }
1.1543 +
1.1544 + /// \brief Return the minimum value of a graph map.
1.1545 + ///
1.1546 + /// This function returns the minimum value of the given graph map.
1.1547 + /// The corresponding item set of the graph must not be empty.
1.1548 + ///
1.1549 + /// \param gr The graph for which the map is defined.
1.1550 + /// \param map The graph map.
1.1551 + template <typename GR, typename Map>
1.1552 + typename Map::Value mapMinValue(const GR& gr, const Map& map) {
1.1553 + return map[mapMin(gr, map, std::less<typename Map::Value>())];
1.1554 + }
1.1555 +
1.1556 + /// \brief Return the minimum value of a graph map.
1.1557 + ///
1.1558 + /// This function returns the minimum value of the given graph map.
1.1559 + /// The corresponding item set of the graph must not be empty.
1.1560 + ///
1.1561 + /// \param gr The graph for which the map is defined.
1.1562 + /// \param map The graph map.
1.1563 + /// \param comp Comparison function object.
1.1564 + template <typename GR, typename Map, typename Comp>
1.1565 + typename Map::Value
1.1566 + mapMinValue(const GR& gr, const Map& map, const Comp& comp) {
1.1567 + return map[mapMin(gr, map, comp)];
1.1568 + }
1.1569 +
1.1570 + /// \brief Return the maximum value of a graph map.
1.1571 + ///
1.1572 + /// This function returns the maximum value of the given graph map.
1.1573 + /// The corresponding item set of the graph must not be empty.
1.1574 + ///
1.1575 + /// \param gr The graph for which the map is defined.
1.1576 + /// \param map The graph map.
1.1577 + template <typename GR, typename Map>
1.1578 + typename Map::Value mapMaxValue(const GR& gr, const Map& map) {
1.1579 + return map[mapMax(gr, map, std::less<typename Map::Value>())];
1.1580 + }
1.1581 +
1.1582 + /// \brief Return the maximum value of a graph map.
1.1583 + ///
1.1584 + /// This function returns the maximum value of the given graph map.
1.1585 + /// The corresponding item set of the graph must not be empty.
1.1586 + ///
1.1587 + /// \param gr The graph for which the map is defined.
1.1588 + /// \param map The graph map.
1.1589 + /// \param comp Comparison function object.
1.1590 + template <typename GR, typename Map, typename Comp>
1.1591 + typename Map::Value
1.1592 + mapMaxValue(const GR& gr, const Map& map, const Comp& comp) {
1.1593 + return map[mapMax(gr, map, comp)];
1.1594 + }
1.1595 +
1.1596 + /// \brief Return an item having a specified value in a graph map.
1.1597 + ///
1.1598 + /// This function returns an item (\c Node, \c Arc or \c Edge) having
1.1599 + /// the specified assigned value in the given graph map.
1.1600 + /// If no such item exists, it returns \c INVALID.
1.1601 + ///
1.1602 + /// \param gr The graph for which the map is defined.
1.1603 + /// \param map The graph map.
1.1604 + /// \param val The value that have to be found.
1.1605 + template <typename GR, typename Map>
1.1606 + typename Map::Key
1.1607 + mapFind(const GR& gr, const Map& map, const typename Map::Value& val) {
1.1608 + typedef typename Map::Key Item;
1.1609 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1610 +
1.1611 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1612 + if (map[it] == val) return it;
1.1613 + }
1.1614 + return INVALID;
1.1615 + }
1.1616 +
1.1617 + /// \brief Return an item having value for which a certain predicate is
1.1618 + /// true in a graph map.
1.1619 + ///
1.1620 + /// This function returns an item (\c Node, \c Arc or \c Edge) having
1.1621 + /// such assigned value for which the specified predicate is true
1.1622 + /// in the given graph map.
1.1623 + /// If no such item exists, it returns \c INVALID.
1.1624 + ///
1.1625 + /// \param gr The graph for which the map is defined.
1.1626 + /// \param map The graph map.
1.1627 + /// \param pred The predicate function object.
1.1628 + template <typename GR, typename Map, typename Pred>
1.1629 + typename Map::Key
1.1630 + mapFindIf(const GR& gr, const Map& map, const Pred& pred) {
1.1631 + typedef typename Map::Key Item;
1.1632 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1633 +
1.1634 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1635 + if (pred(map[it])) return it;
1.1636 + }
1.1637 + return INVALID;
1.1638 + }
1.1639 +
1.1640 + /// \brief Return the number of items having a specified value in a
1.1641 + /// graph map.
1.1642 + ///
1.1643 + /// This function returns the number of items (\c Node, \c Arc or \c Edge)
1.1644 + /// having the specified assigned value in the given graph map.
1.1645 + ///
1.1646 + /// \param gr The graph for which the map is defined.
1.1647 + /// \param map The graph map.
1.1648 + /// \param val The value that have to be counted.
1.1649 + template <typename GR, typename Map>
1.1650 + int mapCount(const GR& gr, const Map& map, const typename Map::Value& val) {
1.1651 + typedef typename Map::Key Item;
1.1652 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1653 +
1.1654 + int cnt = 0;
1.1655 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1656 + if (map[it] == val) ++cnt;
1.1657 + }
1.1658 + return cnt;
1.1659 + }
1.1660 +
1.1661 + /// \brief Return the number of items having values for which a certain
1.1662 + /// predicate is true in a graph map.
1.1663 + ///
1.1664 + /// This function returns the number of items (\c Node, \c Arc or \c Edge)
1.1665 + /// having such assigned values for which the specified predicate is true
1.1666 + /// in the given graph map.
1.1667 + ///
1.1668 + /// \param gr The graph for which the map is defined.
1.1669 + /// \param map The graph map.
1.1670 + /// \param pred The predicate function object.
1.1671 + template <typename GR, typename Map, typename Pred>
1.1672 + int mapCountIf(const GR& gr, const Map& map, const Pred& pred) {
1.1673 + typedef typename Map::Key Item;
1.1674 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1675 +
1.1676 + int cnt = 0;
1.1677 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1678 + if (pred(map[it])) ++cnt;
1.1679 + }
1.1680 + return cnt;
1.1681 + }
1.1682 +
1.1683 + /// \brief Fill a graph map with a certain value.
1.1684 + ///
1.1685 + /// This function sets the specified value for all items (\c Node,
1.1686 + /// \c Arc or \c Edge) in the given graph map.
1.1687 + ///
1.1688 + /// \param gr The graph for which the map is defined.
1.1689 + /// \param map The graph map. It must conform to the
1.1690 + /// \ref concepts::WriteMap "WriteMap" concept.
1.1691 + /// \param val The value.
1.1692 + template <typename GR, typename Map>
1.1693 + void mapFill(const GR& gr, Map& map, const typename Map::Value& val) {
1.1694 + typedef typename Map::Key Item;
1.1695 + typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
1.1696 +
1.1697 + for (ItemIt it(gr); it != INVALID; ++it) {
1.1698 + map.set(it, val);
1.1699 + }
1.1700 + }
1.1701 +
1.1702 /// @}
1.1703 }
1.1704