Some bugfixes.
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
22 ///\brief Maps that makes it possible to iterate through the keys having
30 ///\todo This is only a static map!
31 ///\param BaseMap is an interger map.
32 template<class BaseMap>
37 typedef typename BaseMap::Key Key;
46 std::vector<Key> vals;
47 int sep; //map[e] is true <=> cref[e]>=sep
49 bool isTrue(Key k) {return cref[k]>=sep;}
50 void swap(Key k, int s)
55 cref[tk]=ti; vals[ti]=tk;
58 void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
59 void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
63 void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
68 const IterableBoolMap &M;
71 explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
73 : M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
74 FalseIt &operator++() { ++i; return *this;}
75 operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
76 bool operator !=(Invalid) const { return i<M.sep; }
77 bool operator ==(Invalid) const { return i>=M.sep; }
82 const IterableBoolMap &M;
85 explicit TrueIt(const IterableBoolMap &_M)
86 : M(_M), i(M.vals.size()-1) { }
88 : M(*((IterableBoolMap*)(0))), i(-1) { }
89 TrueIt &operator++() { --i; return *this;}
90 operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
91 bool operator !=(Invalid) const { return i>=M.sep; }
92 bool operator ==(Invalid) const { return i<M.sep; }
101 RefType(IterableBoolMap &_M,Key _k) : M(_M), k(_k) { }
103 operator Value() const
107 Value operator = (Value v) const { M.set(k,v); return v; }
111 explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
114 for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
115 i!=cref.mapSet().end();
118 vals.push_back(i->first);
123 RefType operator[] (Key k) { return RefType(*this,k);}
124 Value operator[] (Key k) const { return isTrue(k);}
130 /// \addtogroup graph_maps
133 /// Iterable bool NodeMap
135 /// This map can be used in the same way
136 /// as the standard NodeMap<bool> of the
137 /// given graph \c Graph.
138 /// In addition, this class provides two iterators called \ref TrueIt
139 /// and \ref FalseIt to iterate through the "true" and "false" nodes.
140 template <class Graph>
141 class IterableBoolNodeMap
143 typename Graph::NodeMap<int> cmap;
147 typedef IterableBoolMap<typename Graph::NodeMap<int> > BimType;
151 typedef typename BimType::RefType RefType;
152 typedef typename Graph::Node Key;
155 friend class FalseIt;
159 IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
163 void set(Key k, bool v) { imap.set(k,v);}
166 bool &operator[](Key k) { return imap[k];}
168 const bool &operator[](Key k) const { return imap[k];}
170 Value operator[](Key k) const { return imap[k];}
171 RefType operator[](Key k) { return imap[k];}
173 ///Iterator for the "false" nodes
174 class FalseIt : public BimType::FalseIt
177 explicit FalseIt(const IterableBoolNodeMap &m)
178 : BimType::FalseIt(m.imap) { }
179 FalseIt(Invalid i) : BimType::FalseIt(i) { }
181 ///Iterator for the "true" nodes
182 class TrueIt : public BimType::TrueIt
185 explicit TrueIt(const IterableBoolNodeMap &m)
186 : BimType::TrueIt(m.imap) { }
187 TrueIt(Invalid i) : BimType::TrueIt(i) { }
191 /// Iterable bool EdgeMap
193 /// This map can be used in the same way
194 /// as the standard EdgeMap<bool> of the
195 /// given graph \c Graph.
196 /// In addition, this class provides two iterators called \ref TrueIt
197 /// and \ref FalseIt to iterate through the "true" and "false" edges.
198 template <class Graph>
199 class IterableBoolEdgeMap
201 typename Graph::EdgeMap<int> cmap;
205 typedef IterableBoolMap<typename Graph::EdgeMap<int> > BimType;
209 typedef typename BimType::RefType RefType;
210 typedef typename Graph::Edge Key;
213 friend class FalseIt;
217 IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
221 void set(Key k, bool v) { imap.set(k,v);}
224 bool &operator[](Key k) { return imap[k];}
226 const bool &operator[](Key k) const { return imap[k];}
228 Value operator[](Key k) const { return imap[k];}
229 RefType operator[](Key k) { return imap[k];}
231 ///Iterator for the "false" edges
232 class FalseIt : public BimType::FalseIt
235 explicit FalseIt(const IterableBoolEdgeMap &m)
236 : BimType::FalseIt(m.imap) { }
237 FalseIt(Invalid i) : BimType::FalseIt(i) { }
239 ///Iterator for the "true" edges
240 class TrueIt : public BimType::TrueIt
243 explicit TrueIt(const IterableBoolEdgeMap &m)
244 : BimType::TrueIt(m.imap) { }
245 TrueIt(Invalid i) : BimType::TrueIt(i) { }