2 * lemon/iterable_maps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #include <lemon/traits.h>
18 #include <lemon/invalid.h>
24 ///\brief Maps that makes it possible to iterate through the keys having
34 /// \brief Dynamic iterable bool map.
36 /// This class provides a special graph map type which can store
37 /// for each graph item(node, edge, etc.) a bool value. For both
38 /// the true and the false it is possible to iterate on the keys which
39 /// mapped to the given value.
41 /// \param _Graph The graph type.
42 /// \param _Item One of the graph's item type, the key of the map.
43 template <typename _Graph, typename _Item>
45 : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Parent {
50 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
51 typedef typename ItemSetTraits<Graph, Item>
52 ::template Map<int>::Parent Parent;
54 std::vector<Item> array;
61 int position(const Item& item) const {
62 return Parent::operator[](item);
67 /// Indicates that the map if reference map.
68 typedef True ReferenceMapTag;
74 /// The const reference type.
75 typedef const Value& ConstReference;
78 /// \brief Refernce to the value of the map.
80 /// This class is near to similar to the bool type. It can
81 /// be converted to bool and it has the same operators.
83 friend class IterableBoolMap;
85 Reference(IterableBoolMap& map, const Key& key)
86 : _key(key), _map(map) {}
89 Reference& operator=(const Reference& value) {
90 _map.set(_key, (bool)value);
94 operator bool() const {
95 return static_cast<const IterableBoolMap&>(_map)[_key];
98 Reference& operator=(bool value) {
99 _map.set(_key, value);
102 Reference& operator&=(bool value) {
103 _map.set(_key, _map[_key] & value);
106 Reference& operator|=(bool value) {
107 _map.set(_key, _map[_key] | value);
110 Reference& operator^=(bool value) {
111 _map.set(_key, _map[_key] ^ value);
116 IterableBoolMap& _map;
119 /// \brief Constructor of the Map with a default value.
121 /// Constructor of the Map with a default value.
122 IterableBoolMap(const Graph& _graph, bool def = false)
123 : Parent(_graph), graph(_graph) {
124 for (ItemIt it(graph); it != INVALID; ++it) {
125 Parent::set(it, array.size());
128 sep = (def ? array.size() : 0);
131 /// \brief Const subscript operator of the map.
133 /// Const subscript operator of the map.
134 bool operator[](const Item& item) const {
135 return position(item) < sep;
138 /// \brief Subscript operator of the map.
140 /// Subscript operator of the map.
141 Reference operator[](const Item& item) {
142 return Reference(*this, item);
145 /// \brief Set operation of the map.
147 /// Set operation of the map.
148 void set(const Item& item, bool value) {
149 int pos = position(item);
151 if (pos < sep) return;
152 Item tmp = array[sep];
154 Parent::set(item, sep);
156 Parent::set(tmp, pos);
159 if (pos >= sep) return;
161 Item tmp = array[sep];
163 Parent::set(item, sep);
165 Parent::set(tmp, pos);
169 /// \brief Returns the number of the items mapped to true.
171 /// Returns the number of the items mapped to true.
172 int trueNum() const {
176 /// \brief Returns the number of the items mapped to false.
178 /// Returns the number of the items mapped to false.
179 int falseNum() const {
180 return array.size() - sep;
183 /// \brief Iterator for the keys mapped to true.
185 /// Iterator for the keys mapped to true. It works
186 /// like a graph item iterator in the map, it can be converted
187 /// the item type of the map, incremented with \c ++ operator, and
188 /// if the iterator leave the last valid item it will be equal to
190 class TrueIt : public Item {
194 /// \brief Creates an iterator.
196 /// Creates an iterator. It iterates on the
197 /// keys which mapped to true.
198 /// \param map The IterableIntMap
199 TrueIt(const IterableBoolMap& _map)
200 : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID),
203 /// \brief Invalid constructor \& conversion.
205 /// This constructor initializes the item to be invalid.
206 /// \sa Invalid for more details.
207 TrueIt(Invalid) : Parent(INVALID), map(0) {}
209 /// \brief Increment operator.
211 /// Increment Operator.
212 TrueIt& operator++() {
213 int pos = map->position(*this);
214 Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
220 const IterableBoolMap* map;
223 /// \brief Iterator for the keys mapped to false.
225 /// Iterator for the keys mapped to false. It works
226 /// like a graph item iterator in the map, it can be converted
227 /// the item type of the map, incremented with \c ++ operator, and
228 /// if the iterator leave the last valid item it will be equal to
230 class FalseIt : public Item {
234 /// \brief Creates an iterator.
236 /// Creates an iterator. It iterates on the
237 /// keys which mapped to false.
238 /// \param map The IterableIntMap
239 FalseIt(const IterableBoolMap& _map)
240 : Parent(_map.sep < _map.array.size() ? _map.array.back() : INVALID),
243 /// \brief Invalid constructor \& conversion.
245 /// This constructor initializes the item to be invalid.
246 /// \sa Invalid for more details.
247 FalseIt(Invalid) : Parent(INVALID), map(0) {}
249 /// \brief Increment operator.
251 /// Increment Operator.
252 FalseIt& operator++() {
253 int pos = map->position(*this);
254 Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
259 const IterableBoolMap* map;
264 virtual void add(const Item& item) {
266 Parent::set(item, array.size());
267 array.push_back(item);
270 virtual void add(const std::vector<Item>& items) {
272 for (int i = 0; i < (int)items.size(); ++i) {
273 Parent::set(items[i], array.size());
274 array.push_back(items[i]);
278 virtual void erase(const Item& item) {
279 int pos = position(item);
282 Parent::set(array[sep], pos);
283 array[pos] = array[sep];
284 Parent::set(array.back(), sep);
285 array[sep] = array.back();
288 Parent::set(array.back(), pos);
289 array[pos] = array.back();
295 virtual void erase(const std::vector<Item>& items) {
296 for (int i = 0; i < (int)items.size(); ++i) {
297 int pos = position(items[i]);
300 Parent::set(array[sep], pos);
301 array[pos] = array[sep];
302 Parent::set(array.back(), sep);
303 array[sep] = array.back();
306 Parent::set(array.back(), pos);
307 array[pos] = array.back();
311 Parent::erase(items);
314 virtual void build() {
316 for (ItemIt it(graph); it != INVALID; ++it) {
317 Parent::set(it, array.size());
323 virtual void clear() {
332 namespace _iterable_maps_bits {
333 template <typename Item>
334 struct IterableIntMapNode {
335 IterableIntMapNode() : value(-1) {}
336 IterableIntMapNode(int _value) : value(_value) {}
344 /// \brief Dynamic iterable integer map.
346 /// This class provides a special graph map type which can store
347 /// for each graph item(node, edge, etc.) an integer value. For each
348 /// non negative value it is possible to iterate on the keys which
349 /// mapped to the given value.
351 /// \param _Graph The graph type.
352 /// \param _Item One of the graph's item type, the key of the map.
353 template <typename _Graph, typename _Item>
354 class IterableIntMap : protected ItemSetTraits<_Graph, _Item>
355 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
357 typedef typename ItemSetTraits<_Graph, _Item>
358 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
366 typedef _Graph Graph;
368 /// \brief Constructor of the Map.
370 /// Constructor of the Map. It set all values -1.
371 explicit IterableIntMap(const Graph& graph)
374 /// \brief Constructor of the Map with a given value.
376 /// Constructor of the Map with a given value.
377 explicit IterableIntMap(const Graph& graph, int value)
378 : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
380 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
388 void unlace(const Key& key) {
389 typename Parent::Value& node = Parent::operator[](key);
390 if (node.value < 0) return;
391 if (node.prev != INVALID) {
392 Parent::operator[](node.prev).next = node.next;
394 first[node.value] = node.next;
396 if (node.next != INVALID) {
397 Parent::operator[](node.next).prev = node.prev;
399 while (!first.empty() && first.back() == INVALID) {
404 void lace(const Key& key) {
405 typename Parent::Value& node = Parent::operator[](key);
406 if (node.value < 0) return;
407 if (node.value >= (int)first.size()) {
408 first.resize(node.value + 1, INVALID);
411 node.next = first[node.value];
412 if (node.next != INVALID) {
413 Parent::operator[](node.next).prev = key;
415 first[node.value] = key;
420 /// Indicates that the map if reference map.
421 typedef True ReferenceMapTag;
423 /// \brief Refernce to the value of the map.
425 /// This class is near to similar to the int type. It can
426 /// be converted to int and it has the same operators.
428 friend class IterableIntMap;
430 Reference(IterableIntMap& map, const Key& key)
431 : _key(key), _map(map) {}
434 Reference& operator=(const Reference& value) {
435 _map.set(_key, (const int&)value);
439 operator const int&() const {
440 return static_cast<const IterableIntMap&>(_map)[_key];
443 Reference& operator=(int value) {
444 _map.set(_key, value);
447 Reference& operator++() {
448 _map.set(_key, _map[_key] + 1);
451 int operator++(int) {
452 int value = _map[_key];
453 _map.set(_key, value + 1);
456 Reference& operator--() {
457 _map.set(_key, _map[_key] - 1);
460 int operator--(int) {
461 int value = _map[_key];
462 _map.set(_key, value - 1);
465 Reference& operator+=(int value) {
466 _map.set(_key, _map[_key] + value);
469 Reference& operator-=(int value) {
470 _map.set(_key, _map[_key] - value);
473 Reference& operator*=(int value) {
474 _map.set(_key, _map[_key] * value);
477 Reference& operator/=(int value) {
478 _map.set(_key, _map[_key] / value);
481 Reference& operator%=(int value) {
482 _map.set(_key, _map[_key] % value);
485 Reference& operator&=(int value) {
486 _map.set(_key, _map[_key] & value);
489 Reference& operator|=(int value) {
490 _map.set(_key, _map[_key] | value);
493 Reference& operator^=(int value) {
494 _map.set(_key, _map[_key] ^ value);
497 Reference& operator<<=(int value) {
498 _map.set(_key, _map[_key] << value);
501 Reference& operator>>=(int value) {
502 _map.set(_key, _map[_key] >> value);
508 IterableIntMap& _map;
511 /// The const reference type.
512 typedef const Value& ConstReference;
514 /// \brief Gives back the maximal value plus one.
516 /// Gives back the maximal value plus one.
518 return (int)first.size();
521 /// \brief Set operation of the map.
523 /// Set operation of the map.
524 void set(const Key& key, const Value& value) {
526 Parent::operator[](key).value = value;
530 /// \brief Const subscript operator of the map.
532 /// Const subscript operator of the map.
533 const Value& operator[](const Key& key) const {
534 return Parent::operator[](key).value;
537 /// \brief Subscript operator of the map.
539 /// Subscript operator of the map.
540 Reference operator[](const Key& key) {
541 return Reference(*this, key);
544 /// \brief Iterator for the keys with the same value.
546 /// Iterator for the keys with the same value. It works
547 /// like a graph item iterator in the map, it can be converted
548 /// the item type of the map, incremented with \c ++ operator, and
549 /// if the iterator leave the last valid item it will be equal to
551 class ItemIt : public _Item {
553 typedef _Item Parent;
555 /// \brief Invalid constructor \& conversion.
557 /// This constructor initializes the item to be invalid.
558 /// \sa Invalid for more details.
559 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
561 /// \brief Creates an iterator with a value.
563 /// Creates an iterator with a value. It iterates on the
564 /// keys which have the given value.
565 /// \param map The IterableIntMap
566 /// \param value The value
567 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
568 if (value < 0 || value >= (int)_map->first.size()) {
569 Parent::operator=(INVALID);
571 Parent::operator=(_map->first[value]);
575 /// \brief Increment operator.
577 /// Increment Operator.
578 ItemIt& operator++() {
579 Parent::operator=(_map->IterableIntMap::Parent::
580 operator[](static_cast<Parent&>(*this)).next);
586 const IterableIntMap* _map;
591 virtual void erase(const Key& key) {
596 virtual void erase(const std::vector<Key>& keys) {
597 for (int i = 0; i < keys.size(); ++i) {
603 virtual void clear() {
609 std::vector<_Item> first;