Bug fix.
2 * lemon/iterable_maps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 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
32 ///\todo This is only a static map!
33 ///\param BaseMap is an interger map.
34 template<class BaseMap>
39 typedef typename BaseMap::Key Key;
48 std::vector<Key> vals;
49 int sep; //map[e] is true <=> cref[e]>=sep
51 bool isTrue(Key k) {return cref[k]>=sep;}
52 void swap(Key k, int s)
57 cref[tk]=ti; vals[ti]=tk;
60 void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
61 void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
65 void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
70 const IterableBoolMap &M;
73 explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
75 : M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
76 FalseIt &operator++() { ++i; return *this;}
77 operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
78 bool operator !=(Invalid) const { return i<M.sep; }
79 bool operator ==(Invalid) const { return i>=M.sep; }
84 const IterableBoolMap &M;
87 explicit TrueIt(const IterableBoolMap &_M)
88 : M(_M), i(M.vals.size()-1) { }
90 : M(*((IterableBoolMap*)(0))), i(-1) { }
91 TrueIt &operator++() { --i; return *this;}
92 operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
93 bool operator !=(Invalid) const { return i>=M.sep; }
94 bool operator ==(Invalid) const { return i<M.sep; }
103 RefType(IterableBoolMap &_M,Key _k) : M(_M), k(_k) { }
105 operator Value() const
109 Value operator = (Value v) const { M.set(k,v); return v; }
113 explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
116 for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
117 i!=cref.mapSet().end();
120 vals.push_back(i->first);
125 RefType operator[] (Key k) { return RefType(*this,k);}
126 Value operator[] (Key k) const { return isTrue(k);}
132 /// \addtogroup graph_maps
135 /// Iterable bool NodeMap
137 /// This map can be used in the same way
138 /// as the standard NodeMap<bool> of the
139 /// given graph \c Graph.
140 /// In addition, this class provides two iterators called \ref TrueIt
141 /// and \ref FalseIt to iterate through the "true" and "false" nodes.
142 template <class Graph>
143 class IterableBoolNodeMap
145 typename Graph::template NodeMap<int> cmap;
149 typedef IterableBoolMap<typename Graph::template NodeMap<int> > BimType;
153 typedef typename BimType::RefType RefType;
154 typedef typename Graph::Node Key;
157 friend class FalseIt;
161 IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
165 void set(Key k, bool v) { imap.set(k,v);}
168 bool &operator[](Key k) { return imap[k];}
170 const bool &operator[](Key k) const { return imap[k];}
172 Value operator[](Key k) const { return imap[k];}
173 RefType operator[](Key k) { return imap[k];}
175 ///Iterator for the "false" nodes
176 class FalseIt : public BimType::FalseIt
179 explicit FalseIt(const IterableBoolNodeMap &m)
180 : BimType::FalseIt(m.imap) { }
181 FalseIt(Invalid i) : BimType::FalseIt(i) { }
183 ///Iterator for the "true" nodes
184 class TrueIt : public BimType::TrueIt
187 explicit TrueIt(const IterableBoolNodeMap &m)
188 : BimType::TrueIt(m.imap) { }
189 TrueIt(Invalid i) : BimType::TrueIt(i) { }
193 /// Iterable bool EdgeMap
195 /// This map can be used in the same way
196 /// as the standard EdgeMap<bool> of the
197 /// given graph \c Graph.
198 /// In addition, this class provides two iterators called \ref TrueIt
199 /// and \ref FalseIt to iterate through the "true" and "false" edges.
200 template <class Graph>
201 class IterableBoolEdgeMap
203 typename Graph::template EdgeMap<int> cmap;
207 typedef IterableBoolMap<typename Graph::template EdgeMap<int> > BimType;
211 typedef typename BimType::RefType RefType;
212 typedef typename Graph::Edge Key;
215 friend class FalseIt;
219 IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
223 void set(Key k, bool v) { imap.set(k,v);}
226 bool &operator[](Key k) { return imap[k];}
228 const bool &operator[](Key k) const { return imap[k];}
230 Value operator[](Key k) const { return imap[k];}
231 RefType operator[](Key k) { return imap[k];}
233 ///Iterator for the "false" edges
234 class FalseIt : public BimType::FalseIt
237 explicit FalseIt(const IterableBoolEdgeMap &m)
238 : BimType::FalseIt(m.imap) { }
239 FalseIt(Invalid i) : BimType::FalseIt(i) { }
241 ///Iterator for the "true" edges
242 class TrueIt : public BimType::TrueIt
245 explicit TrueIt(const IterableBoolEdgeMap &m)
246 : BimType::TrueIt(m.imap) { }
247 TrueIt(Invalid i) : BimType::TrueIt(i) { }
252 namespace _iterable_maps_bits {
253 template <typename Item>
254 struct IterableIntMapNode {
255 IterableIntMapNode() : value(-1) {}
263 /// \brief Dynamic iterable integer map.
265 /// \todo Document please
266 template <typename _Graph, typename _Item>
267 class IterableIntMap : protected ItemSetTraits<_Graph, _Item>
268 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
270 typedef typename ItemSetTraits<_Graph, _Item>
271 ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
276 typedef _Graph Graph;
278 explicit IterableIntMap(const Graph& graph) : Parent(graph) {}
282 void unlace(const Key& key) {
283 typename Parent::Value& node = Parent::operator[](key);
284 if (node.value < 0) return;
285 if (node.prev != INVALID) {
286 Parent::operator[](node.prev).next = node.next;
288 first[node.value] = node.next;
290 if (node.next != INVALID) {
291 Parent::operator[](node.next).prev = node.prev;
293 while (!first.empty() && first.back() == INVALID) {
298 void lace(const Key& key) {
299 typename Parent::Value& node = Parent::operator[](key);
300 if (node.value < 0) return;
301 if (node.value >= (int)first.size()) {
302 first.resize(node.value + 1, INVALID);
305 node.next = first[node.value];
306 if (node.next != INVALID) {
307 Parent::operator[](node.next).prev = key;
309 first[node.value] = key;
314 typedef True ReferenceMapTag;
317 friend class IterableIntMap;
319 Reference(IterableIntMap& map, const Key& key)
320 : _key(key), _map(map) {}
323 Reference& operator=(const Reference& value) {
324 _map.set(_key, (const int&)value);
328 operator const int&() const {
329 return static_cast<const IterableIntMap&>(_map)[_key];
332 Reference& operator=(int value) {
333 _map.set(_key, value);
336 Reference& operator++() {
337 _map.set(_key, _map[_key] + 1);
340 int operator++(int) {
341 int value = _map[_key];
342 _map.set(_key, value + 1);
345 Reference& operator--() {
346 _map.set(_key, _map[_key] - 1);
349 int operator--(int) {
350 int value = _map[_key];
351 _map.set(_key, value - 1);
354 Reference& operator+=(int value) {
355 _map.set(_key, _map[_key] + value);
358 Reference& operator-=(int value) {
359 _map.set(_key, _map[_key] - value);
362 Reference& operator*=(int value) {
363 _map.set(_key, _map[_key] * value);
366 Reference& operator/=(int value) {
367 _map.set(_key, _map[_key] / value);
370 Reference& operator%=(int value) {
371 _map.set(_key, _map[_key] % value);
374 Reference& operator&=(int value) {
375 _map.set(_key, _map[_key] & value);
378 Reference& operator|=(int value) {
379 _map.set(_key, _map[_key] | value);
382 Reference& operator^=(int value) {
383 _map.set(_key, _map[_key] ^ value);
386 Reference& operator<<=(int value) {
387 _map.set(_key, _map[_key] << value);
390 Reference& operator>>=(int value) {
391 _map.set(_key, _map[_key] >> value);
397 IterableIntMap& _map;
400 typedef const Value& ConstReference;
403 return (int)first.size();
406 void set(const Key& key, const Value& value) {
408 Parent::operator[](key).value = value;
412 const Value& operator[](const Key& key) const {
413 return Parent::operator[](key).value;
416 Reference operator[](const Key& key) {
417 return Reference(*this, key);
420 class ItemIt : public _Item {
422 typedef _Item Parent;
424 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
426 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
427 if (value < 0 || value >= (int)_map->first.size()) {
428 Parent::operator=(INVALID);
430 Parent::operator=(_map->first[value]);
434 ItemIt& operator++() {
435 Parent::operator=(_map->IterableIntMap::Parent::
436 operator[](static_cast<Parent&>(*this)).next);
442 const IterableIntMap* _map;
447 virtual void erase(const Key& key) {
452 virtual void clear() {
458 std::vector<_Item> first;