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