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>
28 ///\brief Maps that makes it possible to iterate through the keys having
36 ///\ingroup graph_maps
38 /// \brief Dynamic iterable bool map.
40 /// This class provides a special graph map type which can store
41 /// for each graph item(node, edge, etc.) a bool value. For both
42 /// the true and the false it is possible to iterate on the keys which
43 /// mapped to the given value.
45 /// \param _Graph The graph type.
46 /// \param _Item One of the graph's item type, the key of the map.
47 template <typename _Graph, typename _Item>
49 : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Parent {
53 typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
54 typedef typename ItemSetTraits<Graph, _Item>
55 ::template Map<int>::Parent Parent;
57 std::vector<_Item> array;
64 /// Indicates that the map if reference map.
65 typedef True ReferenceMapTag;
71 /// The const reference type.
72 typedef const Value& ConstReference;
76 int position(const Key& key) const {
77 return Parent::operator[](key);
82 /// \brief Refernce to the value of the map.
84 /// This class is near to similar to the bool type. It can
85 /// be converted to bool and it has the same operators.
87 friend class IterableBoolMap;
89 Reference(IterableBoolMap& map, const Key& key)
90 : _key(key), _map(map) {}
93 Reference& operator=(const Reference& value) {
94 _map.set(_key, (bool)value);
98 operator bool() const {
99 return static_cast<const IterableBoolMap&>(_map)[_key];
102 Reference& operator=(bool value) {
103 _map.set(_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);
114 Reference& operator^=(bool value) {
115 _map.set(_key, _map[_key] ^ value);
120 IterableBoolMap& _map;
123 /// \brief Constructor of the Map with a default value.
125 /// Constructor of the Map with a default value.
126 IterableBoolMap(const Graph& _graph, bool def = false)
127 : Parent(_graph), graph(_graph) {
128 for (KeyIt it(graph); it != INVALID; ++it) {
129 Parent::set(it, array.size());
132 sep = (def ? array.size() : 0);
135 /// \brief Const subscript operator of the map.
137 /// Const subscript operator of the map.
138 bool operator[](const Key& key) const {
139 return position(key) < sep;
142 /// \brief Subscript operator of the map.
144 /// Subscript operator of the map.
145 Reference operator[](const Key& key) {
146 return Reference(*this, key);
149 /// \brief Set operation of the map.
151 /// Set operation of the map.
152 void set(const Key& key, bool value) {
153 int pos = position(key);
155 if (pos < sep) return;
156 Key tmp = array[sep];
158 Parent::set(key, sep);
160 Parent::set(tmp, pos);
163 if (pos >= sep) return;
165 Key tmp = array[sep];
167 Parent::set(key, sep);
169 Parent::set(tmp, pos);
173 /// \brief Returns the number of the keys mapped to true.
175 /// Returns the number of the keys mapped to true.
176 int trueNum() const {
180 /// \brief Returns the number of the keys mapped to false.
182 /// Returns the number of the keys mapped to false.
183 int falseNum() const {
184 return array.size() - sep;
187 /// \brief Iterator for the keys mapped to true.
189 /// Iterator for the keys mapped to true. It works
190 /// like a graph item iterator in the map, it can be converted
191 /// the key type of the map, incremented with \c ++ operator, and
192 /// if the iterator leave the last valid key it will be equal to
194 class TrueIt : public Key {
198 /// \brief Creates an iterator.
200 /// Creates an iterator. It iterates on the
201 /// keys which mapped to true.
202 /// \param map The IterableIntMap
203 TrueIt(const IterableBoolMap& _map)
204 : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID),
207 /// \brief Invalid constructor \& conversion.
209 /// This constructor initializes the key to be invalid.
210 /// \sa Invalid for more details.
211 TrueIt(Invalid) : Parent(INVALID), map(0) {}
213 /// \brief Increment operator.
215 /// Increment Operator.
216 TrueIt& operator++() {
217 int pos = map->position(*this);
218 Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
224 const IterableBoolMap* map;
227 /// \brief Iterator for the keys mapped to false.
229 /// Iterator for the keys mapped to false. It works
230 /// like a graph item iterator in the map, it can be converted
231 /// the key type of the map, incremented with \c ++ operator, and
232 /// if the iterator leave the last valid key it will be equal to
234 class FalseIt : public Key {
238 /// \brief Creates an iterator.
240 /// Creates an iterator. It iterates on the
241 /// keys which mapped to false.
242 /// \param map The IterableIntMap
243 FalseIt(const IterableBoolMap& _map)
244 : Parent(_map.sep < (int)_map.array.size() ?
245 _map.array.back() : INVALID), map(&_map) {}
247 /// \brief Invalid constructor \& conversion.
249 /// This constructor initializes the key to be invalid.
250 /// \sa Invalid for more details.
251 FalseIt(Invalid) : Parent(INVALID), map(0) {}
253 /// \brief Increment operator.
255 /// Increment Operator.
256 FalseIt& operator++() {
257 int pos = map->position(*this);
258 Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
263 const IterableBoolMap* map;
266 /// \brief Iterator for the keys mapped to a given value.
268 /// Iterator for the keys mapped to a given value. It works
269 /// like a graph item iterator in the map, it can be converted
270 /// the key type of the map, incremented with \c ++ operator, and
271 /// if the iterator leave the last valid key it will be equal to
273 class ItemIt : public Key {
277 /// \brief Creates an iterator.
279 /// Creates an iterator. It iterates on the
280 /// keys which mapped to false.
281 /// \param map The IterableIntMap
282 /// \param value Which elements should be iterated.
283 ItemIt(const IterableBoolMap& _map, bool value)
284 : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) :
285 (_map.sep < (int)_map.array.size() ?
286 _map.array.back() : INVALID)), map(&_map) {}
288 /// \brief Invalid constructor \& conversion.
290 /// This constructor initializes the key to be invalid.
291 /// \sa Invalid for more details.
292 ItemIt(Invalid) : Parent(INVALID), map(0) {}
294 /// \brief Increment operator.
296 /// Increment Operator.
297 ItemIt& operator++() {
298 int pos = map->position(*this);
299 int sep = pos >= map->sep ? map->sep : 0;
300 Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID);
305 const IterableBoolMap* map;
310 virtual void add(const Key& key) {
312 Parent::set(key, array.size());
313 array.push_back(key);
316 virtual void add(const std::vector<Key>& keys) {
318 for (int i = 0; i < (int)keys.size(); ++i) {
319 Parent::set(keys[i], array.size());
320 array.push_back(keys[i]);
324 virtual void erase(const Key& key) {
325 int pos = position(key);
328 Parent::set(array[sep], pos);
329 array[pos] = array[sep];
330 Parent::set(array.back(), sep);
331 array[sep] = array.back();
334 Parent::set(array.back(), pos);
335 array[pos] = array.back();
341 virtual void erase(const std::vector<Key>& keys) {
342 for (int i = 0; i < (int)keys.size(); ++i) {
343 int pos = position(keys[i]);
346 Parent::set(array[sep], pos);
347 array[pos] = array[sep];
348 Parent::set(array.back(), sep);
349 array[sep] = array.back();
352 Parent::set(array.back(), pos);
353 array[pos] = array.back();
360 virtual void build() {
362 for (KeyIt it(graph); it != INVALID; ++it) {
363 Parent::set(it, array.size());
369 virtual void clear() {
378 namespace _iterable_maps_bits {
379 template <typename Item>
380 struct IterableIntMapNode {
381 IterableIntMapNode() : value(-1) {}
382 IterableIntMapNode(int _value) : value(_value) {}
388 ///\ingroup graph_maps
390 /// \brief Dynamic iterable integer map.
392 /// This class provides a special graph map type which can store
393 /// for each graph item(node, edge, etc.) an integer value. For each
394 /// non negative value it is possible to iterate on the keys which
395 /// mapped to the given value.
397 /// \param _Graph The graph type.
398 /// \param _Item One of the graph's item type, the key of the map.
399 template <typename _Graph, typename _Item>
400 class IterableIntMap : protected ItemSetTraits<_Graph, _Item>
401 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
403 typedef typename ItemSetTraits<_Graph, _Item>
404 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
412 typedef _Graph Graph;
414 /// \brief Constructor of the Map.
416 /// Constructor of the Map. It set all values -1.
417 explicit IterableIntMap(const Graph& graph)
420 /// \brief Constructor of the Map with a given value.
422 /// Constructor of the Map with a given value.
423 explicit IterableIntMap(const Graph& graph, int value)
424 : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
426 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
434 void unlace(const Key& key) {
435 typename Parent::Value& node = Parent::operator[](key);
436 if (node.value < 0) return;
437 if (node.prev != INVALID) {
438 Parent::operator[](node.prev).next = node.next;
440 first[node.value] = node.next;
442 if (node.next != INVALID) {
443 Parent::operator[](node.next).prev = node.prev;
445 while (!first.empty() && first.back() == INVALID) {
450 void lace(const Key& key) {
451 typename Parent::Value& node = Parent::operator[](key);
452 if (node.value < 0) return;
453 if (node.value >= (int)first.size()) {
454 first.resize(node.value + 1, INVALID);
457 node.next = first[node.value];
458 if (node.next != INVALID) {
459 Parent::operator[](node.next).prev = key;
461 first[node.value] = key;
466 /// Indicates that the map if reference map.
467 typedef True ReferenceMapTag;
469 /// \brief Refernce to the value of the map.
471 /// This class is near to similar to the int type. It can
472 /// be converted to int and it has the same operators.
474 friend class IterableIntMap;
476 Reference(IterableIntMap& map, const Key& key)
477 : _key(key), _map(map) {}
480 Reference& operator=(const Reference& value) {
481 _map.set(_key, (const int&)value);
485 operator const int&() const {
486 return static_cast<const IterableIntMap&>(_map)[_key];
489 Reference& operator=(int value) {
490 _map.set(_key, value);
493 Reference& operator++() {
494 _map.set(_key, _map[_key] + 1);
497 int operator++(int) {
498 int value = _map[_key];
499 _map.set(_key, value + 1);
502 Reference& operator--() {
503 _map.set(_key, _map[_key] - 1);
506 int operator--(int) {
507 int value = _map[_key];
508 _map.set(_key, value - 1);
511 Reference& operator+=(int value) {
512 _map.set(_key, _map[_key] + value);
515 Reference& operator-=(int value) {
516 _map.set(_key, _map[_key] - value);
519 Reference& operator*=(int value) {
520 _map.set(_key, _map[_key] * value);
523 Reference& operator/=(int value) {
524 _map.set(_key, _map[_key] / value);
527 Reference& operator%=(int value) {
528 _map.set(_key, _map[_key] % value);
531 Reference& operator&=(int value) {
532 _map.set(_key, _map[_key] & value);
535 Reference& operator|=(int value) {
536 _map.set(_key, _map[_key] | value);
539 Reference& operator^=(int value) {
540 _map.set(_key, _map[_key] ^ value);
543 Reference& operator<<=(int value) {
544 _map.set(_key, _map[_key] << value);
547 Reference& operator>>=(int value) {
548 _map.set(_key, _map[_key] >> value);
554 IterableIntMap& _map;
557 /// The const reference type.
558 typedef const Value& ConstReference;
560 /// \brief Gives back the maximal value plus one.
562 /// Gives back the maximal value plus one.
563 unsigned int size() const {
567 /// \brief Set operation of the map.
569 /// Set operation of the map.
570 void set(const Key& key, const Value& value) {
572 Parent::operator[](key).value = value;
576 /// \brief Const subscript operator of the map.
578 /// Const subscript operator of the map.
579 const Value& operator[](const Key& key) const {
580 return Parent::operator[](key).value;
583 /// \brief Subscript operator of the map.
585 /// Subscript operator of the map.
586 Reference operator[](const Key& key) {
587 return Reference(*this, key);
590 /// \brief Iterator for the keys with the same value.
592 /// Iterator for the keys with the same value. It works
593 /// like a graph item iterator in the map, it can be converted
594 /// the item type of the map, incremented with \c ++ operator, and
595 /// if the iterator leave the last valid item it will be equal to
597 class ItemIt : public _Item {
599 typedef _Item Parent;
601 /// \brief Invalid constructor \& conversion.
603 /// This constructor initializes the item to be invalid.
604 /// \sa Invalid for more details.
605 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
607 /// \brief Creates an iterator with a value.
609 /// Creates an iterator with a value. It iterates on the
610 /// keys which have the given value.
611 /// \param map The IterableIntMap
612 /// \param value The value
613 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
614 if (value < 0 || value >= (int)_map->first.size()) {
615 Parent::operator=(INVALID);
617 Parent::operator=(_map->first[value]);
621 /// \brief Increment operator.
623 /// Increment Operator.
624 ItemIt& operator++() {
625 Parent::operator=(_map->IterableIntMap::Parent::
626 operator[](static_cast<Parent&>(*this)).next);
632 const IterableIntMap* _map;
637 virtual void erase(const Key& key) {
642 virtual void erase(const std::vector<Key>& keys) {
643 for (int i = 0; i < (int)keys.size(); ++i) {
649 virtual void clear() {
655 std::vector<_Item> first;
658 namespace _iterable_maps_bits {
659 template <typename Item, typename Value>
660 struct IterableValueMapNode {
661 IterableValueMapNode(Value _value = Value()) : value(_value) {}
667 ///\ingroup graph_maps
669 /// \brief Dynamic iterable map for comparable values.
671 /// This class provides a special graph map type which can store
672 /// for each graph item(node, edge, etc.) a value. For each
673 /// value it is possible to iterate on the keys which mapped to the
674 /// given value. The type stores for each value a linked list with
675 /// the items which mapped to the value, and the values are stored
676 /// in balanced binary tree. The values of the map can be accessed
677 /// with stl compatible forward iterator.
679 /// This type is not reference map so it cannot be modified with
680 /// the subscription operator.
682 /// \see InvertableMap
684 /// \param _Graph The graph type.
685 /// \param _Item One of the graph's item type, the key of the map.
686 /// \param _Value Any comparable value type.
687 template <typename _Graph, typename _Item, typename _Value>
688 class IterableValueMap : protected ItemSetTraits<_Graph, _Item>
689 ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> >
692 typedef typename ItemSetTraits<_Graph, _Item>
693 ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> >
699 typedef _Value Value;
701 typedef _Graph Graph;
703 /// \brief Constructor of the Map with a given value.
705 /// Constructor of the Map with a given value.
706 explicit IterableValueMap(const Graph& graph,
707 const Value& value = Value())
708 : Parent(graph, _iterable_maps_bits::IterableValueMapNode<_Item,
710 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
717 void unlace(const Key& key) {
718 typename Parent::Value& node = Parent::operator[](key);
719 if (node.prev != INVALID) {
720 Parent::operator[](node.prev).next = node.next;
722 if (node.next != INVALID) {
723 first[node.value] = node.next;
725 first.erase(node.value);
728 if (node.next != INVALID) {
729 Parent::operator[](node.next).prev = node.prev;
733 void lace(const Key& key) {
734 typename Parent::Value& node = Parent::operator[](key);
735 typename std::map<Value, Key>::iterator it = first.find(node.value);
736 if (it == first.end()) {
737 node.prev = node.next = INVALID;
738 if (node.next != INVALID) {
739 Parent::operator[](node.next).prev = key;
741 first.insert(make_pair(node.value, key));
744 node.next = it->second;
745 if (node.next != INVALID) {
746 Parent::operator[](node.next).prev = key;
754 /// \brief Forward iterator for values.
756 /// This iterator is an stl compatible forward
757 /// iterator on the values of the map. The values can
758 /// be accessed in the [beginValue, endValue) range.
761 : public std::iterator<std::forward_iterator_tag, Value> {
762 friend class IterableValueMap;
764 ValueIterator(typename std::map<Value, Key>::const_iterator _it)
770 ValueIterator& operator++() { ++it; return *this; }
771 ValueIterator operator++(int) {
772 ValueIterator tmp(*this);
777 const Value& operator*() const { return it->first; }
778 const Value* operator->() const { return &(it->first); }
780 bool operator==(ValueIterator jt) const { return it == jt.it; }
781 bool operator!=(ValueIterator jt) const { return it != jt.it; }
784 typename std::map<Value, Key>::const_iterator it;
787 /// \brief Returns an iterator to the first value.
789 /// Returns an stl compatible iterator to the
790 /// first value of the map. The values of the
791 /// map can be accessed in the [beginValue, endValue)
793 ValueIterator beginValue() const {
794 return ValueIterator(first.begin());
797 /// \brief Returns an iterator after the last value.
799 /// Returns an stl compatible iterator after the
800 /// last value of the map. The values of the
801 /// map can be accessed in the [beginValue, endValue)
803 ValueIterator endValue() const {
804 return ValueIterator(first.end());
807 /// \brief Set operation of the map.
809 /// Set operation of the map.
810 void set(const Key& key, const Value& value) {
812 Parent::operator[](key).value = value;
816 /// \brief Const subscript operator of the map.
818 /// Const subscript operator of the map.
819 const Value& operator[](const Key& key) const {
820 return Parent::operator[](key).value;
823 /// \brief Iterator for the keys with the same value.
825 /// Iterator for the keys with the same value. It works
826 /// like a graph item iterator in the map, it can be converted
827 /// the item type of the map, incremented with \c ++ operator, and
828 /// if the iterator leave the last valid item it will be equal to
830 class ItemIt : public _Item {
832 typedef _Item Parent;
834 /// \brief Invalid constructor \& conversion.
836 /// This constructor initializes the item to be invalid.
837 /// \sa Invalid for more details.
838 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
840 /// \brief Creates an iterator with a value.
842 /// Creates an iterator with a value. It iterates on the
843 /// keys which have the given value.
844 /// \param map The IterableValueMap
845 /// \param value The value
846 ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
847 typename std::map<Value, Key>::const_iterator it =
848 map.first.find(value);
849 if (it == map.first.end()) {
850 Parent::operator=(INVALID);
852 Parent::operator=(it->second);
856 /// \brief Increment operator.
858 /// Increment Operator.
859 ItemIt& operator++() {
860 Parent::operator=(_map->IterableValueMap::Parent::
861 operator[](static_cast<Parent&>(*this)).next);
867 const IterableValueMap* _map;
872 virtual void erase(const Key& key) {
877 virtual void erase(const std::vector<Key>& keys) {
878 for (int i = 0; i < (int)keys.size(); ++i) {
884 virtual void clear() {
890 std::map<Value, Key> first;