[1677] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1677] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1752] | 19 | #include <lemon/traits.h> |
---|
| 20 | #include <lemon/invalid.h> |
---|
[1931] | 21 | |
---|
[1677] | 22 | #include <vector> |
---|
[1931] | 23 | #include <map> |
---|
| 24 | |
---|
| 25 | #include <iterator> |
---|
[1677] | 26 | #include <limits> |
---|
| 27 | |
---|
| 28 | ///\ingroup maps |
---|
| 29 | ///\file |
---|
| 30 | ///\brief Maps that makes it possible to iterate through the keys having |
---|
| 31 | ///a certain value |
---|
| 32 | /// |
---|
| 33 | /// |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | namespace lemon { |
---|
[1913] | 37 | |
---|
[1931] | 38 | ///\ingroup graph_maps |
---|
[1913] | 39 | /// |
---|
| 40 | /// \brief Dynamic iterable bool map. |
---|
| 41 | /// |
---|
| 42 | /// This class provides a special graph map type which can store |
---|
| 43 | /// for each graph item(node, edge, etc.) a bool value. For both |
---|
| 44 | /// the true and the false it is possible to iterate on the keys which |
---|
| 45 | /// mapped to the given value. |
---|
| 46 | /// |
---|
| 47 | /// \param _Graph The graph type. |
---|
| 48 | /// \param _Item One of the graph's item type, the key of the map. |
---|
| 49 | template <typename _Graph, typename _Item> |
---|
| 50 | class IterableBoolMap |
---|
| 51 | : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Parent { |
---|
| 52 | private: |
---|
| 53 | typedef _Graph Graph; |
---|
| 54 | |
---|
[1931] | 55 | typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt; |
---|
| 56 | typedef typename ItemSetTraits<Graph, _Item> |
---|
[1913] | 57 | ::template Map<int>::Parent Parent; |
---|
| 58 | |
---|
[1931] | 59 | std::vector<_Item> array; |
---|
[1913] | 60 | int sep; |
---|
| 61 | |
---|
| 62 | const Graph& graph; |
---|
[1677] | 63 | |
---|
| 64 | public: |
---|
[1805] | 65 | |
---|
[1913] | 66 | /// Indicates that the map if reference map. |
---|
| 67 | typedef True ReferenceMapTag; |
---|
[1805] | 68 | |
---|
[1913] | 69 | /// The key type |
---|
[1931] | 70 | typedef _Item Key; |
---|
[1913] | 71 | /// The value type |
---|
| 72 | typedef bool Value; |
---|
| 73 | /// The const reference type. |
---|
| 74 | typedef const Value& ConstReference; |
---|
| 75 | |
---|
[1931] | 76 | private: |
---|
| 77 | |
---|
| 78 | int position(const Key& key) const { |
---|
| 79 | return Parent::operator[](key); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public: |
---|
[1913] | 83 | |
---|
| 84 | /// \brief Refernce to the value of the map. |
---|
| 85 | /// |
---|
| 86 | /// This class is near to similar to the bool type. It can |
---|
| 87 | /// be converted to bool and it has the same operators. |
---|
| 88 | class Reference { |
---|
| 89 | friend class IterableBoolMap; |
---|
| 90 | private: |
---|
| 91 | Reference(IterableBoolMap& map, const Key& key) |
---|
| 92 | : _key(key), _map(map) {} |
---|
[1677] | 93 | public: |
---|
[1913] | 94 | |
---|
| 95 | Reference& operator=(const Reference& value) { |
---|
| 96 | _map.set(_key, (bool)value); |
---|
| 97 | return *this; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | operator bool() const { |
---|
| 101 | return static_cast<const IterableBoolMap&>(_map)[_key]; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | Reference& operator=(bool value) { |
---|
| 105 | _map.set(_key, value); |
---|
| 106 | return *this; |
---|
| 107 | } |
---|
| 108 | Reference& operator&=(bool value) { |
---|
| 109 | _map.set(_key, _map[_key] & value); |
---|
| 110 | return *this; |
---|
| 111 | } |
---|
| 112 | Reference& operator|=(bool value) { |
---|
| 113 | _map.set(_key, _map[_key] | value); |
---|
| 114 | return *this; |
---|
| 115 | } |
---|
| 116 | Reference& operator^=(bool value) { |
---|
| 117 | _map.set(_key, _map[_key] ^ value); |
---|
| 118 | return *this; |
---|
| 119 | } |
---|
| 120 | private: |
---|
| 121 | Key _key; |
---|
| 122 | IterableBoolMap& _map; |
---|
[1677] | 123 | }; |
---|
[1913] | 124 | |
---|
| 125 | /// \brief Constructor of the Map with a default value. |
---|
| 126 | /// |
---|
| 127 | /// Constructor of the Map with a default value. |
---|
| 128 | IterableBoolMap(const Graph& _graph, bool def = false) |
---|
| 129 | : Parent(_graph), graph(_graph) { |
---|
[1931] | 130 | for (KeyIt it(graph); it != INVALID; ++it) { |
---|
[1913] | 131 | Parent::set(it, array.size()); |
---|
| 132 | array.push_back(it); |
---|
| 133 | } |
---|
| 134 | sep = (def ? array.size() : 0); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /// \brief Const subscript operator of the map. |
---|
| 138 | /// |
---|
| 139 | /// Const subscript operator of the map. |
---|
[1931] | 140 | bool operator[](const Key& key) const { |
---|
| 141 | return position(key) < sep; |
---|
[1913] | 142 | } |
---|
| 143 | |
---|
| 144 | /// \brief Subscript operator of the map. |
---|
| 145 | /// |
---|
| 146 | /// Subscript operator of the map. |
---|
[1931] | 147 | Reference operator[](const Key& key) { |
---|
| 148 | return Reference(*this, key); |
---|
[1913] | 149 | } |
---|
| 150 | |
---|
| 151 | /// \brief Set operation of the map. |
---|
| 152 | /// |
---|
| 153 | /// Set operation of the map. |
---|
[1931] | 154 | void set(const Key& key, bool value) { |
---|
| 155 | int pos = position(key); |
---|
[1913] | 156 | if (value) { |
---|
| 157 | if (pos < sep) return; |
---|
[1931] | 158 | Key tmp = array[sep]; |
---|
| 159 | array[sep] = key; |
---|
| 160 | Parent::set(key, sep); |
---|
[1913] | 161 | array[pos] = tmp; |
---|
| 162 | Parent::set(tmp, pos); |
---|
| 163 | ++sep; |
---|
| 164 | } else { |
---|
| 165 | if (pos >= sep) return; |
---|
| 166 | --sep; |
---|
[1931] | 167 | Key tmp = array[sep]; |
---|
| 168 | array[sep] = key; |
---|
| 169 | Parent::set(key, sep); |
---|
[1913] | 170 | array[pos] = tmp; |
---|
| 171 | Parent::set(tmp, pos); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
[1931] | 175 | /// \brief Returns the number of the keys mapped to true. |
---|
[1913] | 176 | /// |
---|
[1931] | 177 | /// Returns the number of the keys mapped to true. |
---|
[1913] | 178 | int trueNum() const { |
---|
| 179 | return sep; |
---|
| 180 | } |
---|
| 181 | |
---|
[1931] | 182 | /// \brief Returns the number of the keys mapped to false. |
---|
[1913] | 183 | /// |
---|
[1931] | 184 | /// Returns the number of the keys mapped to false. |
---|
[1913] | 185 | int falseNum() const { |
---|
| 186 | return array.size() - sep; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /// \brief Iterator for the keys mapped to true. |
---|
| 190 | /// |
---|
| 191 | /// Iterator for the keys mapped to true. It works |
---|
| 192 | /// like a graph item iterator in the map, it can be converted |
---|
[1931] | 193 | /// the key type of the map, incremented with \c ++ operator, and |
---|
| 194 | /// if the iterator leave the last valid key it will be equal to |
---|
[1913] | 195 | /// \c INVALID. |
---|
[1931] | 196 | class TrueIt : public Key { |
---|
[1677] | 197 | public: |
---|
[1931] | 198 | typedef Key Parent; |
---|
[1913] | 199 | |
---|
| 200 | /// \brief Creates an iterator. |
---|
| 201 | /// |
---|
| 202 | /// Creates an iterator. It iterates on the |
---|
| 203 | /// keys which mapped to true. |
---|
[1953] | 204 | /// \param _map The IterableIntMap |
---|
[1913] | 205 | TrueIt(const IterableBoolMap& _map) |
---|
| 206 | : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), |
---|
| 207 | map(&_map) {} |
---|
| 208 | |
---|
| 209 | /// \brief Invalid constructor \& conversion. |
---|
| 210 | /// |
---|
[1931] | 211 | /// This constructor initializes the key to be invalid. |
---|
[1913] | 212 | /// \sa Invalid for more details. |
---|
| 213 | TrueIt(Invalid) : Parent(INVALID), map(0) {} |
---|
| 214 | |
---|
| 215 | /// \brief Increment operator. |
---|
| 216 | /// |
---|
| 217 | /// Increment Operator. |
---|
| 218 | TrueIt& operator++() { |
---|
| 219 | int pos = map->position(*this); |
---|
| 220 | Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID); |
---|
| 221 | return *this; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | private: |
---|
| 226 | const IterableBoolMap* map; |
---|
| 227 | }; |
---|
| 228 | |
---|
| 229 | /// \brief Iterator for the keys mapped to false. |
---|
| 230 | /// |
---|
| 231 | /// Iterator for the keys mapped to false. It works |
---|
| 232 | /// like a graph item iterator in the map, it can be converted |
---|
[1931] | 233 | /// the key type of the map, incremented with \c ++ operator, and |
---|
| 234 | /// if the iterator leave the last valid key it will be equal to |
---|
[1913] | 235 | /// \c INVALID. |
---|
[1931] | 236 | class FalseIt : public Key { |
---|
[1913] | 237 | public: |
---|
[1931] | 238 | typedef Key Parent; |
---|
[1913] | 239 | |
---|
| 240 | /// \brief Creates an iterator. |
---|
| 241 | /// |
---|
| 242 | /// Creates an iterator. It iterates on the |
---|
| 243 | /// keys which mapped to false. |
---|
[1953] | 244 | /// \param _map The IterableIntMap |
---|
[1913] | 245 | FalseIt(const IterableBoolMap& _map) |
---|
[1931] | 246 | : Parent(_map.sep < (int)_map.array.size() ? |
---|
| 247 | _map.array.back() : INVALID), map(&_map) {} |
---|
[1913] | 248 | |
---|
| 249 | /// \brief Invalid constructor \& conversion. |
---|
| 250 | /// |
---|
[1931] | 251 | /// This constructor initializes the key to be invalid. |
---|
[1913] | 252 | /// \sa Invalid for more details. |
---|
| 253 | FalseIt(Invalid) : Parent(INVALID), map(0) {} |
---|
| 254 | |
---|
| 255 | /// \brief Increment operator. |
---|
| 256 | /// |
---|
| 257 | /// Increment Operator. |
---|
| 258 | FalseIt& operator++() { |
---|
| 259 | int pos = map->position(*this); |
---|
| 260 | Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID); |
---|
| 261 | return *this; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | private: |
---|
| 265 | const IterableBoolMap* map; |
---|
| 266 | }; |
---|
| 267 | |
---|
[1931] | 268 | /// \brief Iterator for the keys mapped to a given value. |
---|
| 269 | /// |
---|
| 270 | /// Iterator for the keys mapped to a given value. It works |
---|
| 271 | /// like a graph item iterator in the map, it can be converted |
---|
| 272 | /// the key type of the map, incremented with \c ++ operator, and |
---|
| 273 | /// if the iterator leave the last valid key it will be equal to |
---|
| 274 | /// \c INVALID. |
---|
| 275 | class ItemIt : public Key { |
---|
| 276 | public: |
---|
| 277 | typedef Key Parent; |
---|
| 278 | |
---|
| 279 | /// \brief Creates an iterator. |
---|
| 280 | /// |
---|
| 281 | /// Creates an iterator. It iterates on the |
---|
| 282 | /// keys which mapped to false. |
---|
[1953] | 283 | /// \param _map The IterableIntMap |
---|
[1931] | 284 | /// \param value Which elements should be iterated. |
---|
| 285 | ItemIt(const IterableBoolMap& _map, bool value) |
---|
| 286 | : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) : |
---|
| 287 | (_map.sep < (int)_map.array.size() ? |
---|
| 288 | _map.array.back() : INVALID)), map(&_map) {} |
---|
| 289 | |
---|
| 290 | /// \brief Invalid constructor \& conversion. |
---|
| 291 | /// |
---|
| 292 | /// This constructor initializes the key to be invalid. |
---|
| 293 | /// \sa Invalid for more details. |
---|
| 294 | ItemIt(Invalid) : Parent(INVALID), map(0) {} |
---|
| 295 | |
---|
| 296 | /// \brief Increment operator. |
---|
| 297 | /// |
---|
| 298 | /// Increment Operator. |
---|
| 299 | ItemIt& operator++() { |
---|
| 300 | int pos = map->position(*this); |
---|
| 301 | int sep = pos >= map->sep ? map->sep : 0; |
---|
| 302 | Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID); |
---|
| 303 | return *this; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | private: |
---|
| 307 | const IterableBoolMap* map; |
---|
| 308 | }; |
---|
| 309 | |
---|
[1913] | 310 | protected: |
---|
| 311 | |
---|
[1931] | 312 | virtual void add(const Key& key) { |
---|
| 313 | Parent::add(key); |
---|
| 314 | Parent::set(key, array.size()); |
---|
| 315 | array.push_back(key); |
---|
[1913] | 316 | } |
---|
| 317 | |
---|
[1931] | 318 | virtual void add(const std::vector<Key>& keys) { |
---|
| 319 | Parent::add(keys); |
---|
| 320 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 321 | Parent::set(keys[i], array.size()); |
---|
| 322 | array.push_back(keys[i]); |
---|
[1913] | 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
[1931] | 326 | virtual void erase(const Key& key) { |
---|
| 327 | int pos = position(key); |
---|
[1913] | 328 | if (pos < sep) { |
---|
| 329 | --sep; |
---|
| 330 | Parent::set(array[sep], pos); |
---|
| 331 | array[pos] = array[sep]; |
---|
| 332 | Parent::set(array.back(), sep); |
---|
| 333 | array[sep] = array.back(); |
---|
| 334 | array.pop_back(); |
---|
| 335 | } else { |
---|
| 336 | Parent::set(array.back(), pos); |
---|
| 337 | array[pos] = array.back(); |
---|
| 338 | array.pop_back(); |
---|
| 339 | } |
---|
[1931] | 340 | Parent::erase(key); |
---|
[1913] | 341 | } |
---|
| 342 | |
---|
[1931] | 343 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 344 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 345 | int pos = position(keys[i]); |
---|
[1913] | 346 | if (pos < sep) { |
---|
| 347 | --sep; |
---|
| 348 | Parent::set(array[sep], pos); |
---|
| 349 | array[pos] = array[sep]; |
---|
| 350 | Parent::set(array.back(), sep); |
---|
| 351 | array[sep] = array.back(); |
---|
| 352 | array.pop_back(); |
---|
| 353 | } else { |
---|
| 354 | Parent::set(array.back(), pos); |
---|
| 355 | array[pos] = array.back(); |
---|
| 356 | array.pop_back(); |
---|
| 357 | } |
---|
| 358 | } |
---|
[1931] | 359 | Parent::erase(keys); |
---|
[1913] | 360 | } |
---|
| 361 | |
---|
| 362 | virtual void build() { |
---|
| 363 | Parent::build(); |
---|
[1931] | 364 | for (KeyIt it(graph); it != INVALID; ++it) { |
---|
[1913] | 365 | Parent::set(it, array.size()); |
---|
| 366 | array.push_back(it); |
---|
| 367 | } |
---|
| 368 | sep = 0; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | virtual void clear() { |
---|
| 372 | array.clear(); |
---|
| 373 | sep = 0; |
---|
| 374 | Parent::clear(); |
---|
| 375 | } |
---|
| 376 | |
---|
[1677] | 377 | }; |
---|
[1873] | 378 | |
---|
[1752] | 379 | |
---|
| 380 | namespace _iterable_maps_bits { |
---|
| 381 | template <typename Item> |
---|
| 382 | struct IterableIntMapNode { |
---|
[1913] | 383 | IterableIntMapNode() : value(-1) {} |
---|
[1810] | 384 | IterableIntMapNode(int _value) : value(_value) {} |
---|
[1752] | 385 | Item prev, next; |
---|
| 386 | int value; |
---|
| 387 | }; |
---|
| 388 | } |
---|
| 389 | |
---|
[1931] | 390 | ///\ingroup graph_maps |
---|
[1752] | 391 | /// |
---|
| 392 | /// \brief Dynamic iterable integer map. |
---|
| 393 | /// |
---|
[1810] | 394 | /// This class provides a special graph map type which can store |
---|
| 395 | /// for each graph item(node, edge, etc.) an integer value. For each |
---|
| 396 | /// non negative value it is possible to iterate on the keys which |
---|
| 397 | /// mapped to the given value. |
---|
| 398 | /// |
---|
| 399 | /// \param _Graph The graph type. |
---|
| 400 | /// \param _Item One of the graph's item type, the key of the map. |
---|
[1752] | 401 | template <typename _Graph, typename _Item> |
---|
| 402 | class IterableIntMap : protected ItemSetTraits<_Graph, _Item> |
---|
| 403 | ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent { |
---|
| 404 | public: |
---|
| 405 | typedef typename ItemSetTraits<_Graph, _Item> |
---|
| 406 | ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> > |
---|
| 407 | ::Parent Parent; |
---|
| 408 | |
---|
[1810] | 409 | /// The key type |
---|
[1752] | 410 | typedef _Item Key; |
---|
[1810] | 411 | /// The value type |
---|
[1752] | 412 | typedef int Value; |
---|
[1810] | 413 | /// The graph type |
---|
[1752] | 414 | typedef _Graph Graph; |
---|
| 415 | |
---|
[1810] | 416 | /// \brief Constructor of the Map. |
---|
| 417 | /// |
---|
| 418 | /// Constructor of the Map. It set all values -1. |
---|
| 419 | explicit IterableIntMap(const Graph& graph) |
---|
[1913] | 420 | : Parent(graph) {} |
---|
[1810] | 421 | |
---|
| 422 | /// \brief Constructor of the Map with a given value. |
---|
| 423 | /// |
---|
| 424 | /// Constructor of the Map with a given value. |
---|
| 425 | explicit IterableIntMap(const Graph& graph, int value) |
---|
| 426 | : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) { |
---|
| 427 | if (value >= 0) { |
---|
| 428 | for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
---|
| 429 | lace(it); |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | } |
---|
[1752] | 433 | |
---|
| 434 | private: |
---|
| 435 | |
---|
| 436 | void unlace(const Key& key) { |
---|
| 437 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 438 | if (node.value < 0) return; |
---|
| 439 | if (node.prev != INVALID) { |
---|
| 440 | Parent::operator[](node.prev).next = node.next; |
---|
| 441 | } else { |
---|
| 442 | first[node.value] = node.next; |
---|
| 443 | } |
---|
| 444 | if (node.next != INVALID) { |
---|
| 445 | Parent::operator[](node.next).prev = node.prev; |
---|
| 446 | } |
---|
| 447 | while (!first.empty() && first.back() == INVALID) { |
---|
| 448 | first.pop_back(); |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | void lace(const Key& key) { |
---|
| 453 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 454 | if (node.value < 0) return; |
---|
| 455 | if (node.value >= (int)first.size()) { |
---|
| 456 | first.resize(node.value + 1, INVALID); |
---|
| 457 | } |
---|
| 458 | node.prev = INVALID; |
---|
| 459 | node.next = first[node.value]; |
---|
| 460 | if (node.next != INVALID) { |
---|
| 461 | Parent::operator[](node.next).prev = key; |
---|
| 462 | } |
---|
| 463 | first[node.value] = key; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | public: |
---|
| 467 | |
---|
[1810] | 468 | /// Indicates that the map if reference map. |
---|
[1752] | 469 | typedef True ReferenceMapTag; |
---|
| 470 | |
---|
[1810] | 471 | /// \brief Refernce to the value of the map. |
---|
| 472 | /// |
---|
| 473 | /// This class is near to similar to the int type. It can |
---|
| 474 | /// be converted to int and it has the same operators. |
---|
[1752] | 475 | class Reference { |
---|
| 476 | friend class IterableIntMap; |
---|
| 477 | private: |
---|
| 478 | Reference(IterableIntMap& map, const Key& key) |
---|
| 479 | : _key(key), _map(map) {} |
---|
| 480 | public: |
---|
| 481 | |
---|
| 482 | Reference& operator=(const Reference& value) { |
---|
| 483 | _map.set(_key, (const int&)value); |
---|
| 484 | return *this; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | operator const int&() const { |
---|
| 488 | return static_cast<const IterableIntMap&>(_map)[_key]; |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | Reference& operator=(int value) { |
---|
| 492 | _map.set(_key, value); |
---|
| 493 | return *this; |
---|
| 494 | } |
---|
[1759] | 495 | Reference& operator++() { |
---|
| 496 | _map.set(_key, _map[_key] + 1); |
---|
| 497 | return *this; |
---|
| 498 | } |
---|
| 499 | int operator++(int) { |
---|
| 500 | int value = _map[_key]; |
---|
| 501 | _map.set(_key, value + 1); |
---|
| 502 | return value; |
---|
| 503 | } |
---|
| 504 | Reference& operator--() { |
---|
| 505 | _map.set(_key, _map[_key] - 1); |
---|
| 506 | return *this; |
---|
| 507 | } |
---|
| 508 | int operator--(int) { |
---|
| 509 | int value = _map[_key]; |
---|
| 510 | _map.set(_key, value - 1); |
---|
| 511 | return value; |
---|
| 512 | } |
---|
[1752] | 513 | Reference& operator+=(int value) { |
---|
| 514 | _map.set(_key, _map[_key] + value); |
---|
| 515 | return *this; |
---|
| 516 | } |
---|
| 517 | Reference& operator-=(int value) { |
---|
| 518 | _map.set(_key, _map[_key] - value); |
---|
| 519 | return *this; |
---|
| 520 | } |
---|
| 521 | Reference& operator*=(int value) { |
---|
| 522 | _map.set(_key, _map[_key] * value); |
---|
| 523 | return *this; |
---|
| 524 | } |
---|
| 525 | Reference& operator/=(int value) { |
---|
| 526 | _map.set(_key, _map[_key] / value); |
---|
| 527 | return *this; |
---|
| 528 | } |
---|
| 529 | Reference& operator%=(int value) { |
---|
| 530 | _map.set(_key, _map[_key] % value); |
---|
| 531 | return *this; |
---|
| 532 | } |
---|
| 533 | Reference& operator&=(int value) { |
---|
| 534 | _map.set(_key, _map[_key] & value); |
---|
| 535 | return *this; |
---|
| 536 | } |
---|
| 537 | Reference& operator|=(int value) { |
---|
| 538 | _map.set(_key, _map[_key] | value); |
---|
| 539 | return *this; |
---|
| 540 | } |
---|
| 541 | Reference& operator^=(int value) { |
---|
| 542 | _map.set(_key, _map[_key] ^ value); |
---|
| 543 | return *this; |
---|
| 544 | } |
---|
| 545 | Reference& operator<<=(int value) { |
---|
| 546 | _map.set(_key, _map[_key] << value); |
---|
| 547 | return *this; |
---|
| 548 | } |
---|
| 549 | Reference& operator>>=(int value) { |
---|
| 550 | _map.set(_key, _map[_key] >> value); |
---|
| 551 | return *this; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | private: |
---|
| 555 | Key _key; |
---|
| 556 | IterableIntMap& _map; |
---|
| 557 | }; |
---|
[1810] | 558 | |
---|
| 559 | /// The const reference type. |
---|
[1752] | 560 | typedef const Value& ConstReference; |
---|
| 561 | |
---|
[1810] | 562 | /// \brief Gives back the maximal value plus one. |
---|
| 563 | /// |
---|
| 564 | /// Gives back the maximal value plus one. |
---|
[1931] | 565 | unsigned int size() const { |
---|
| 566 | return first.size(); |
---|
[1752] | 567 | } |
---|
| 568 | |
---|
[1810] | 569 | /// \brief Set operation of the map. |
---|
| 570 | /// |
---|
| 571 | /// Set operation of the map. |
---|
[1752] | 572 | void set(const Key& key, const Value& value) { |
---|
| 573 | unlace(key); |
---|
| 574 | Parent::operator[](key).value = value; |
---|
| 575 | lace(key); |
---|
| 576 | } |
---|
| 577 | |
---|
[1810] | 578 | /// \brief Const subscript operator of the map. |
---|
| 579 | /// |
---|
| 580 | /// Const subscript operator of the map. |
---|
[1752] | 581 | const Value& operator[](const Key& key) const { |
---|
| 582 | return Parent::operator[](key).value; |
---|
| 583 | } |
---|
| 584 | |
---|
[1810] | 585 | /// \brief Subscript operator of the map. |
---|
| 586 | /// |
---|
| 587 | /// Subscript operator of the map. |
---|
[1752] | 588 | Reference operator[](const Key& key) { |
---|
| 589 | return Reference(*this, key); |
---|
| 590 | } |
---|
| 591 | |
---|
[1810] | 592 | /// \brief Iterator for the keys with the same value. |
---|
| 593 | /// |
---|
| 594 | /// Iterator for the keys with the same value. It works |
---|
| 595 | /// like a graph item iterator in the map, it can be converted |
---|
| 596 | /// the item type of the map, incremented with \c ++ operator, and |
---|
| 597 | /// if the iterator leave the last valid item it will be equal to |
---|
| 598 | /// \c INVALID. |
---|
[1752] | 599 | class ItemIt : public _Item { |
---|
| 600 | public: |
---|
| 601 | typedef _Item Parent; |
---|
| 602 | |
---|
[1810] | 603 | /// \brief Invalid constructor \& conversion. |
---|
| 604 | /// |
---|
| 605 | /// This constructor initializes the item to be invalid. |
---|
| 606 | /// \sa Invalid for more details. |
---|
[1752] | 607 | ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 608 | |
---|
[1810] | 609 | /// \brief Creates an iterator with a value. |
---|
| 610 | /// |
---|
| 611 | /// Creates an iterator with a value. It iterates on the |
---|
| 612 | /// keys which have the given value. |
---|
| 613 | /// \param map The IterableIntMap |
---|
| 614 | /// \param value The value |
---|
[1752] | 615 | ItemIt(const IterableIntMap& map, int value) : _map(&map) { |
---|
| 616 | if (value < 0 || value >= (int)_map->first.size()) { |
---|
| 617 | Parent::operator=(INVALID); |
---|
| 618 | } else { |
---|
| 619 | Parent::operator=(_map->first[value]); |
---|
| 620 | } |
---|
| 621 | } |
---|
| 622 | |
---|
[1810] | 623 | /// \brief Increment operator. |
---|
| 624 | /// |
---|
| 625 | /// Increment Operator. |
---|
[1752] | 626 | ItemIt& operator++() { |
---|
| 627 | Parent::operator=(_map->IterableIntMap::Parent:: |
---|
| 628 | operator[](static_cast<Parent&>(*this)).next); |
---|
| 629 | return *this; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | |
---|
| 633 | private: |
---|
| 634 | const IterableIntMap* _map; |
---|
| 635 | }; |
---|
| 636 | |
---|
| 637 | protected: |
---|
| 638 | |
---|
| 639 | virtual void erase(const Key& key) { |
---|
| 640 | unlace(key); |
---|
| 641 | Parent::erase(key); |
---|
| 642 | } |
---|
| 643 | |
---|
[1913] | 644 | virtual void erase(const std::vector<Key>& keys) { |
---|
[1931] | 645 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
[1913] | 646 | unlace(keys[i]); |
---|
| 647 | } |
---|
| 648 | Parent::erase(keys); |
---|
| 649 | } |
---|
| 650 | |
---|
[1752] | 651 | virtual void clear() { |
---|
| 652 | first.clear(); |
---|
| 653 | Parent::clear(); |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | private: |
---|
| 657 | std::vector<_Item> first; |
---|
| 658 | }; |
---|
| 659 | |
---|
[1931] | 660 | namespace _iterable_maps_bits { |
---|
| 661 | template <typename Item, typename Value> |
---|
| 662 | struct IterableValueMapNode { |
---|
| 663 | IterableValueMapNode(Value _value = Value()) : value(_value) {} |
---|
| 664 | Item prev, next; |
---|
| 665 | Value value; |
---|
| 666 | }; |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | ///\ingroup graph_maps |
---|
| 670 | /// |
---|
| 671 | /// \brief Dynamic iterable map for comparable values. |
---|
| 672 | /// |
---|
| 673 | /// This class provides a special graph map type which can store |
---|
| 674 | /// for each graph item(node, edge, etc.) a value. For each |
---|
| 675 | /// value it is possible to iterate on the keys which mapped to the |
---|
| 676 | /// given value. The type stores for each value a linked list with |
---|
| 677 | /// the items which mapped to the value, and the values are stored |
---|
| 678 | /// in balanced binary tree. The values of the map can be accessed |
---|
| 679 | /// with stl compatible forward iterator. |
---|
| 680 | /// |
---|
| 681 | /// This type is not reference map so it cannot be modified with |
---|
| 682 | /// the subscription operator. |
---|
| 683 | /// |
---|
| 684 | /// \see InvertableMap |
---|
| 685 | /// |
---|
| 686 | /// \param _Graph The graph type. |
---|
| 687 | /// \param _Item One of the graph's item type, the key of the map. |
---|
| 688 | /// \param _Value Any comparable value type. |
---|
| 689 | template <typename _Graph, typename _Item, typename _Value> |
---|
| 690 | class IterableValueMap : protected ItemSetTraits<_Graph, _Item> |
---|
| 691 | ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> > |
---|
| 692 | ::Parent { |
---|
| 693 | public: |
---|
| 694 | typedef typename ItemSetTraits<_Graph, _Item> |
---|
| 695 | ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> > |
---|
| 696 | ::Parent Parent; |
---|
| 697 | |
---|
| 698 | /// The key type |
---|
| 699 | typedef _Item Key; |
---|
| 700 | /// The value type |
---|
| 701 | typedef _Value Value; |
---|
| 702 | /// The graph type |
---|
| 703 | typedef _Graph Graph; |
---|
| 704 | |
---|
| 705 | /// \brief Constructor of the Map with a given value. |
---|
| 706 | /// |
---|
| 707 | /// Constructor of the Map with a given value. |
---|
| 708 | explicit IterableValueMap(const Graph& graph, |
---|
| 709 | const Value& value = Value()) |
---|
| 710 | : Parent(graph, _iterable_maps_bits::IterableValueMapNode<_Item, |
---|
| 711 | _Value>(value)) { |
---|
| 712 | for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
---|
| 713 | lace(it); |
---|
| 714 | } |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | private: |
---|
| 718 | |
---|
| 719 | void unlace(const Key& key) { |
---|
| 720 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 721 | if (node.prev != INVALID) { |
---|
| 722 | Parent::operator[](node.prev).next = node.next; |
---|
| 723 | } else { |
---|
| 724 | if (node.next != INVALID) { |
---|
| 725 | first[node.value] = node.next; |
---|
| 726 | } else { |
---|
| 727 | first.erase(node.value); |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | if (node.next != INVALID) { |
---|
| 731 | Parent::operator[](node.next).prev = node.prev; |
---|
| 732 | } |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | void lace(const Key& key) { |
---|
| 736 | typename Parent::Value& node = Parent::operator[](key); |
---|
| 737 | typename std::map<Value, Key>::iterator it = first.find(node.value); |
---|
| 738 | if (it == first.end()) { |
---|
| 739 | node.prev = node.next = INVALID; |
---|
| 740 | if (node.next != INVALID) { |
---|
| 741 | Parent::operator[](node.next).prev = key; |
---|
| 742 | } |
---|
| 743 | first.insert(make_pair(node.value, key)); |
---|
| 744 | } else { |
---|
| 745 | node.prev = INVALID; |
---|
| 746 | node.next = it->second; |
---|
| 747 | if (node.next != INVALID) { |
---|
| 748 | Parent::operator[](node.next).prev = key; |
---|
| 749 | } |
---|
| 750 | it->second = key; |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | public: |
---|
| 755 | |
---|
| 756 | /// \brief Forward iterator for values. |
---|
| 757 | /// |
---|
| 758 | /// This iterator is an stl compatible forward |
---|
| 759 | /// iterator on the values of the map. The values can |
---|
| 760 | /// be accessed in the [beginValue, endValue) range. |
---|
| 761 | /// |
---|
| 762 | class ValueIterator |
---|
| 763 | : public std::iterator<std::forward_iterator_tag, Value> { |
---|
| 764 | friend class IterableValueMap; |
---|
| 765 | private: |
---|
| 766 | ValueIterator(typename std::map<Value, Key>::const_iterator _it) |
---|
| 767 | : it(_it) {} |
---|
| 768 | public: |
---|
| 769 | |
---|
| 770 | ValueIterator() {} |
---|
| 771 | |
---|
| 772 | ValueIterator& operator++() { ++it; return *this; } |
---|
| 773 | ValueIterator operator++(int) { |
---|
| 774 | ValueIterator tmp(*this); |
---|
| 775 | operator++(); |
---|
| 776 | return tmp; |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | const Value& operator*() const { return it->first; } |
---|
| 780 | const Value* operator->() const { return &(it->first); } |
---|
| 781 | |
---|
| 782 | bool operator==(ValueIterator jt) const { return it == jt.it; } |
---|
| 783 | bool operator!=(ValueIterator jt) const { return it != jt.it; } |
---|
| 784 | |
---|
| 785 | private: |
---|
| 786 | typename std::map<Value, Key>::const_iterator it; |
---|
| 787 | }; |
---|
| 788 | |
---|
| 789 | /// \brief Returns an iterator to the first value. |
---|
| 790 | /// |
---|
| 791 | /// Returns an stl compatible iterator to the |
---|
| 792 | /// first value of the map. The values of the |
---|
| 793 | /// map can be accessed in the [beginValue, endValue) |
---|
| 794 | /// range. |
---|
| 795 | ValueIterator beginValue() const { |
---|
| 796 | return ValueIterator(first.begin()); |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | /// \brief Returns an iterator after the last value. |
---|
| 800 | /// |
---|
| 801 | /// Returns an stl compatible iterator after the |
---|
| 802 | /// last value of the map. The values of the |
---|
| 803 | /// map can be accessed in the [beginValue, endValue) |
---|
| 804 | /// range. |
---|
| 805 | ValueIterator endValue() const { |
---|
| 806 | return ValueIterator(first.end()); |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | /// \brief Set operation of the map. |
---|
| 810 | /// |
---|
| 811 | /// Set operation of the map. |
---|
| 812 | void set(const Key& key, const Value& value) { |
---|
| 813 | unlace(key); |
---|
| 814 | Parent::operator[](key).value = value; |
---|
| 815 | lace(key); |
---|
| 816 | } |
---|
| 817 | |
---|
| 818 | /// \brief Const subscript operator of the map. |
---|
| 819 | /// |
---|
| 820 | /// Const subscript operator of the map. |
---|
| 821 | const Value& operator[](const Key& key) const { |
---|
| 822 | return Parent::operator[](key).value; |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | /// \brief Iterator for the keys with the same value. |
---|
| 826 | /// |
---|
| 827 | /// Iterator for the keys with the same value. It works |
---|
| 828 | /// like a graph item iterator in the map, it can be converted |
---|
| 829 | /// the item type of the map, incremented with \c ++ operator, and |
---|
| 830 | /// if the iterator leave the last valid item it will be equal to |
---|
| 831 | /// \c INVALID. |
---|
| 832 | class ItemIt : public _Item { |
---|
| 833 | public: |
---|
| 834 | typedef _Item Parent; |
---|
| 835 | |
---|
| 836 | /// \brief Invalid constructor \& conversion. |
---|
| 837 | /// |
---|
| 838 | /// This constructor initializes the item to be invalid. |
---|
| 839 | /// \sa Invalid for more details. |
---|
| 840 | ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
---|
| 841 | |
---|
| 842 | /// \brief Creates an iterator with a value. |
---|
| 843 | /// |
---|
| 844 | /// Creates an iterator with a value. It iterates on the |
---|
| 845 | /// keys which have the given value. |
---|
| 846 | /// \param map The IterableValueMap |
---|
| 847 | /// \param value The value |
---|
| 848 | ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) { |
---|
| 849 | typename std::map<Value, Key>::const_iterator it = |
---|
| 850 | map.first.find(value); |
---|
| 851 | if (it == map.first.end()) { |
---|
| 852 | Parent::operator=(INVALID); |
---|
| 853 | } else { |
---|
| 854 | Parent::operator=(it->second); |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | /// \brief Increment operator. |
---|
| 859 | /// |
---|
| 860 | /// Increment Operator. |
---|
| 861 | ItemIt& operator++() { |
---|
| 862 | Parent::operator=(_map->IterableValueMap::Parent:: |
---|
| 863 | operator[](static_cast<Parent&>(*this)).next); |
---|
| 864 | return *this; |
---|
| 865 | } |
---|
| 866 | |
---|
| 867 | |
---|
| 868 | private: |
---|
| 869 | const IterableValueMap* _map; |
---|
| 870 | }; |
---|
| 871 | |
---|
| 872 | protected: |
---|
| 873 | |
---|
| 874 | virtual void erase(const Key& key) { |
---|
| 875 | unlace(key); |
---|
| 876 | Parent::erase(key); |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 880 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 881 | unlace(keys[i]); |
---|
| 882 | } |
---|
| 883 | Parent::erase(keys); |
---|
| 884 | } |
---|
| 885 | |
---|
| 886 | virtual void clear() { |
---|
| 887 | first.clear(); |
---|
| 888 | Parent::clear(); |
---|
| 889 | } |
---|
| 890 | |
---|
| 891 | private: |
---|
| 892 | std::map<Value, Key> first; |
---|
| 893 | }; |
---|
| 894 | |
---|
[1677] | 895 | /// @} |
---|
| 896 | } |
---|