3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #include <lemon/traits.h>
20 #include <lemon/invalid.h>
30 ///\brief Maps that makes it possible to iterate through the keys having
38 ///\ingroup graph_maps
40 /// \brief Dynamic iterable bool map.
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.
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>
51 : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Parent {
55 typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
56 typedef typename ItemSetTraits<Graph, _Item>
57 ::template Map<int>::Parent Parent;
59 std::vector<_Item> array;
66 /// Indicates that the map if reference map.
67 typedef True ReferenceMapTag;
73 /// The const reference type.
74 typedef const Value& ConstReference;
78 int position(const Key& key) const {
79 return Parent::operator[](key);
84 /// \brief Refernce to the value of the map.
86 /// This class is near to similar to the bool type. It can
87 /// be converted to bool and it has the same operators.
89 friend class IterableBoolMap;
91 Reference(IterableBoolMap& map, const Key& key)
92 : _key(key), _map(map) {}
95 Reference& operator=(const Reference& value) {
96 _map.set(_key, (bool)value);
100 operator bool() const {
101 return static_cast<const IterableBoolMap&>(_map)[_key];
104 Reference& operator=(bool value) {
105 _map.set(_key, value);
108 Reference& operator&=(bool value) {
109 _map.set(_key, _map[_key] & value);
112 Reference& operator|=(bool value) {
113 _map.set(_key, _map[_key] | value);
116 Reference& operator^=(bool value) {
117 _map.set(_key, _map[_key] ^ value);
122 IterableBoolMap& _map;
125 /// \brief Constructor of the Map with a default value.
127 /// Constructor of the Map with a default value.
128 IterableBoolMap(const Graph& _graph, bool def = false)
129 : Parent(_graph), graph(_graph) {
130 for (KeyIt it(graph); it != INVALID; ++it) {
131 Parent::set(it, array.size());
134 sep = (def ? array.size() : 0);
137 /// \brief Const subscript operator of the map.
139 /// Const subscript operator of the map.
140 bool operator[](const Key& key) const {
141 return position(key) < sep;
144 /// \brief Subscript operator of the map.
146 /// Subscript operator of the map.
147 Reference operator[](const Key& key) {
148 return Reference(*this, key);
151 /// \brief Set operation of the map.
153 /// Set operation of the map.
154 void set(const Key& key, bool value) {
155 int pos = position(key);
157 if (pos < sep) return;
158 Key tmp = array[sep];
160 Parent::set(key, sep);
162 Parent::set(tmp, pos);
165 if (pos >= sep) return;
167 Key tmp = array[sep];
169 Parent::set(key, sep);
171 Parent::set(tmp, pos);
175 /// \brief Returns the number of the keys mapped to true.
177 /// Returns the number of the keys mapped to true.
178 int trueNum() const {
182 /// \brief Returns the number of the keys mapped to false.
184 /// Returns the number of the keys mapped to false.
185 int falseNum() const {
186 return array.size() - sep;
189 /// \brief Iterator for the keys mapped to true.
191 /// Iterator for the keys mapped to true. It works
192 /// like a graph item iterator in the map, it can be converted
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
196 class TrueIt : public Key {
200 /// \brief Creates an iterator.
202 /// Creates an iterator. It iterates on the
203 /// keys which mapped to true.
204 /// \param _map The IterableIntMap
205 TrueIt(const IterableBoolMap& _map)
206 : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID),
209 /// \brief Invalid constructor \& conversion.
211 /// This constructor initializes the key to be invalid.
212 /// \sa Invalid for more details.
213 TrueIt(Invalid) : Parent(INVALID), map(0) {}
215 /// \brief Increment operator.
217 /// Increment Operator.
218 TrueIt& operator++() {
219 int pos = map->position(*this);
220 Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
226 const IterableBoolMap* map;
229 /// \brief Iterator for the keys mapped to false.
231 /// Iterator for the keys mapped to false. It works
232 /// like a graph item iterator in the map, it can be converted
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
236 class FalseIt : public Key {
240 /// \brief Creates an iterator.
242 /// Creates an iterator. It iterates on the
243 /// keys which mapped to false.
244 /// \param _map The IterableIntMap
245 FalseIt(const IterableBoolMap& _map)
246 : Parent(_map.sep < (int)_map.array.size() ?
247 _map.array.back() : INVALID), map(&_map) {}
249 /// \brief Invalid constructor \& conversion.
251 /// This constructor initializes the key to be invalid.
252 /// \sa Invalid for more details.
253 FalseIt(Invalid) : Parent(INVALID), map(0) {}
255 /// \brief Increment operator.
257 /// Increment Operator.
258 FalseIt& operator++() {
259 int pos = map->position(*this);
260 Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
265 const IterableBoolMap* map;
268 /// \brief Iterator for the keys mapped to a given value.
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
275 class ItemIt : public Key {
279 /// \brief Creates an iterator.
281 /// Creates an iterator. It iterates on the
282 /// keys which mapped to false.
283 /// \param _map The IterableIntMap
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) {}
290 /// \brief Invalid constructor \& conversion.
292 /// This constructor initializes the key to be invalid.
293 /// \sa Invalid for more details.
294 ItemIt(Invalid) : Parent(INVALID), map(0) {}
296 /// \brief Increment operator.
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);
307 const IterableBoolMap* map;
312 virtual void add(const Key& key) {
314 Parent::set(key, array.size());
315 array.push_back(key);
318 virtual void add(const std::vector<Key>& keys) {
320 for (int i = 0; i < (int)keys.size(); ++i) {
321 Parent::set(keys[i], array.size());
322 array.push_back(keys[i]);
326 virtual void erase(const Key& key) {
327 int pos = position(key);
330 Parent::set(array[sep], pos);
331 array[pos] = array[sep];
332 Parent::set(array.back(), sep);
333 array[sep] = array.back();
336 Parent::set(array.back(), pos);
337 array[pos] = array.back();
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]);
348 Parent::set(array[sep], pos);
349 array[pos] = array[sep];
350 Parent::set(array.back(), sep);
351 array[sep] = array.back();
354 Parent::set(array.back(), pos);
355 array[pos] = array.back();
362 virtual void build() {
364 for (KeyIt it(graph); it != INVALID; ++it) {
365 Parent::set(it, array.size());
371 virtual void clear() {
380 namespace _iterable_maps_bits {
381 template <typename Item>
382 struct IterableIntMapNode {
383 IterableIntMapNode() : value(-1) {}
384 IterableIntMapNode(int _value) : value(_value) {}
390 ///\ingroup graph_maps
392 /// \brief Dynamic iterable integer map.
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.
399 /// \param _Graph The graph type.
400 /// \param _Item One of the graph's item type, the key of the map.
401 template <typename _Graph, typename _Item>
402 class IterableIntMap : protected ItemSetTraits<_Graph, _Item>
403 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
405 typedef typename ItemSetTraits<_Graph, _Item>
406 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
414 typedef _Graph Graph;
416 /// \brief Constructor of the Map.
418 /// Constructor of the Map. It set all values -1.
419 explicit IterableIntMap(const Graph& graph)
422 /// \brief Constructor of the Map with a given value.
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)) {
428 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
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;
442 first[node.value] = node.next;
444 if (node.next != INVALID) {
445 Parent::operator[](node.next).prev = node.prev;
447 while (!first.empty() && first.back() == INVALID) {
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);
459 node.next = first[node.value];
460 if (node.next != INVALID) {
461 Parent::operator[](node.next).prev = key;
463 first[node.value] = key;
468 /// Indicates that the map if reference map.
469 typedef True ReferenceMapTag;
471 /// \brief Refernce to the value of the map.
473 /// This class is near to similar to the int type. It can
474 /// be converted to int and it has the same operators.
476 friend class IterableIntMap;
478 Reference(IterableIntMap& map, const Key& key)
479 : _key(key), _map(map) {}
482 Reference& operator=(const Reference& value) {
483 _map.set(_key, (const int&)value);
487 operator const int&() const {
488 return static_cast<const IterableIntMap&>(_map)[_key];
491 Reference& operator=(int value) {
492 _map.set(_key, value);
495 Reference& operator++() {
496 _map.set(_key, _map[_key] + 1);
499 int operator++(int) {
500 int value = _map[_key];
501 _map.set(_key, value + 1);
504 Reference& operator--() {
505 _map.set(_key, _map[_key] - 1);
508 int operator--(int) {
509 int value = _map[_key];
510 _map.set(_key, value - 1);
513 Reference& operator+=(int value) {
514 _map.set(_key, _map[_key] + value);
517 Reference& operator-=(int value) {
518 _map.set(_key, _map[_key] - value);
521 Reference& operator*=(int value) {
522 _map.set(_key, _map[_key] * value);
525 Reference& operator/=(int value) {
526 _map.set(_key, _map[_key] / value);
529 Reference& operator%=(int value) {
530 _map.set(_key, _map[_key] % value);
533 Reference& operator&=(int value) {
534 _map.set(_key, _map[_key] & value);
537 Reference& operator|=(int value) {
538 _map.set(_key, _map[_key] | value);
541 Reference& operator^=(int value) {
542 _map.set(_key, _map[_key] ^ value);
545 Reference& operator<<=(int value) {
546 _map.set(_key, _map[_key] << value);
549 Reference& operator>>=(int value) {
550 _map.set(_key, _map[_key] >> value);
556 IterableIntMap& _map;
559 /// The const reference type.
560 typedef const Value& ConstReference;
562 /// \brief Gives back the maximal value plus one.
564 /// Gives back the maximal value plus one.
565 unsigned int size() const {
569 /// \brief Set operation of the map.
571 /// Set operation of the map.
572 void set(const Key& key, const Value& value) {
574 Parent::operator[](key).value = value;
578 /// \brief Const subscript operator of the map.
580 /// Const subscript operator of the map.
581 const Value& operator[](const Key& key) const {
582 return Parent::operator[](key).value;
585 /// \brief Subscript operator of the map.
587 /// Subscript operator of the map.
588 Reference operator[](const Key& key) {
589 return Reference(*this, key);
592 /// \brief Iterator for the keys with the same value.
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
599 class ItemIt : public _Item {
601 typedef _Item Parent;
603 /// \brief Invalid constructor \& conversion.
605 /// This constructor initializes the item to be invalid.
606 /// \sa Invalid for more details.
607 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
609 /// \brief Creates an iterator with a value.
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
615 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
616 if (value < 0 || value >= (int)_map->first.size()) {
617 Parent::operator=(INVALID);
619 Parent::operator=(_map->first[value]);
623 /// \brief Increment operator.
625 /// Increment Operator.
626 ItemIt& operator++() {
627 Parent::operator=(_map->IterableIntMap::Parent::
628 operator[](static_cast<Parent&>(*this)).next);
634 const IterableIntMap* _map;
639 virtual void erase(const Key& key) {
644 virtual void erase(const std::vector<Key>& keys) {
645 for (int i = 0; i < (int)keys.size(); ++i) {
651 virtual void clear() {
657 std::vector<_Item> first;
660 namespace _iterable_maps_bits {
661 template <typename Item, typename Value>
662 struct IterableValueMapNode {
663 IterableValueMapNode(Value _value = Value()) : value(_value) {}
669 ///\ingroup graph_maps
671 /// \brief Dynamic iterable map for comparable values.
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.
681 /// This type is not reference map so it cannot be modified with
682 /// the subscription operator.
684 /// \see InvertableMap
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> >
694 typedef typename ItemSetTraits<_Graph, _Item>
695 ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> >
701 typedef _Value Value;
703 typedef _Graph Graph;
705 /// \brief Constructor of the Map with a given value.
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,
712 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
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;
724 if (node.next != INVALID) {
725 first[node.value] = node.next;
727 first.erase(node.value);
730 if (node.next != INVALID) {
731 Parent::operator[](node.next).prev = node.prev;
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;
743 first.insert(make_pair(node.value, key));
746 node.next = it->second;
747 if (node.next != INVALID) {
748 Parent::operator[](node.next).prev = key;
756 /// \brief Forward iterator for values.
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.
763 : public std::iterator<std::forward_iterator_tag, Value> {
764 friend class IterableValueMap;
766 ValueIterator(typename std::map<Value, Key>::const_iterator _it)
772 ValueIterator& operator++() { ++it; return *this; }
773 ValueIterator operator++(int) {
774 ValueIterator tmp(*this);
779 const Value& operator*() const { return it->first; }
780 const Value* operator->() const { return &(it->first); }
782 bool operator==(ValueIterator jt) const { return it == jt.it; }
783 bool operator!=(ValueIterator jt) const { return it != jt.it; }
786 typename std::map<Value, Key>::const_iterator it;
789 /// \brief Returns an iterator to the first value.
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)
795 ValueIterator beginValue() const {
796 return ValueIterator(first.begin());
799 /// \brief Returns an iterator after the last value.
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)
805 ValueIterator endValue() const {
806 return ValueIterator(first.end());
809 /// \brief Set operation of the map.
811 /// Set operation of the map.
812 void set(const Key& key, const Value& value) {
814 Parent::operator[](key).value = value;
818 /// \brief Const subscript operator of the map.
820 /// Const subscript operator of the map.
821 const Value& operator[](const Key& key) const {
822 return Parent::operator[](key).value;
825 /// \brief Iterator for the keys with the same value.
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
832 class ItemIt : public _Item {
834 typedef _Item Parent;
836 /// \brief Invalid constructor \& conversion.
838 /// This constructor initializes the item to be invalid.
839 /// \sa Invalid for more details.
840 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
842 /// \brief Creates an iterator with a value.
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);
854 Parent::operator=(it->second);
858 /// \brief Increment operator.
860 /// Increment Operator.
861 ItemIt& operator++() {
862 Parent::operator=(_map->IterableValueMap::Parent::
863 operator[](static_cast<Parent&>(*this)).next);
869 const IterableValueMap* _map;
874 virtual void erase(const Key& key) {
879 virtual void erase(const std::vector<Key>& keys) {
880 for (int i = 0; i < (int)keys.size(); ++i) {
886 virtual void clear() {
892 std::map<Value, Key> first;