alpar@1677
|
1 |
/* -*- C++ -*-
|
alpar@1677
|
2 |
* lemon/iterable_maps.h - Part of LEMON, a generic C++ optimization library
|
alpar@1677
|
3 |
*
|
alpar@1875
|
4 |
* Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1677
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@1677
|
6 |
*
|
alpar@1677
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@1677
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@1677
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@1677
|
10 |
*
|
alpar@1677
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@1677
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@1677
|
13 |
* purpose.
|
alpar@1677
|
14 |
*
|
alpar@1677
|
15 |
*/
|
alpar@1677
|
16 |
|
deba@1752
|
17 |
#include <lemon/traits.h>
|
deba@1752
|
18 |
#include <lemon/invalid.h>
|
alpar@1677
|
19 |
#include <vector>
|
alpar@1677
|
20 |
#include <limits>
|
alpar@1677
|
21 |
|
alpar@1677
|
22 |
///\ingroup maps
|
alpar@1677
|
23 |
///\file
|
alpar@1677
|
24 |
///\brief Maps that makes it possible to iterate through the keys having
|
alpar@1677
|
25 |
///a certain value
|
alpar@1677
|
26 |
///
|
alpar@1677
|
27 |
///
|
alpar@1677
|
28 |
|
alpar@1677
|
29 |
|
alpar@1677
|
30 |
namespace lemon {
|
alpar@1677
|
31 |
|
alpar@1677
|
32 |
///\todo This is only a static map!
|
alpar@1805
|
33 |
///\todo Undocumented.
|
alpar@1677
|
34 |
///\param BaseMap is an interger map.
|
alpar@1677
|
35 |
template<class BaseMap>
|
alpar@1677
|
36 |
class IterableBoolMap
|
alpar@1677
|
37 |
{
|
alpar@1677
|
38 |
public:
|
alpar@1677
|
39 |
|
alpar@1677
|
40 |
typedef typename BaseMap::Key Key;
|
alpar@1677
|
41 |
typedef bool Value;
|
alpar@1677
|
42 |
|
alpar@1677
|
43 |
friend class RefType;
|
alpar@1677
|
44 |
friend class FalseIt;
|
alpar@1677
|
45 |
friend class TrueIt;
|
alpar@1677
|
46 |
|
alpar@1677
|
47 |
private:
|
alpar@1677
|
48 |
BaseMap &cref;
|
alpar@1677
|
49 |
std::vector<Key> vals;
|
alpar@1677
|
50 |
int sep; //map[e] is true <=> cref[e]>=sep
|
alpar@1677
|
51 |
|
alpar@1677
|
52 |
bool isTrue(Key k) {return cref[k]>=sep;}
|
alpar@1677
|
53 |
void swap(Key k, int s)
|
alpar@1677
|
54 |
{
|
alpar@1677
|
55 |
int ti=cref[k];
|
alpar@1677
|
56 |
Key tk=vals[s];
|
alpar@1677
|
57 |
cref[k]=s; vals[s]=k;
|
alpar@1677
|
58 |
cref[tk]=ti; vals[ti]=tk;
|
alpar@1677
|
59 |
}
|
alpar@1677
|
60 |
|
alpar@1677
|
61 |
void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
|
alpar@1677
|
62 |
void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
|
alpar@1677
|
63 |
|
alpar@1677
|
64 |
public:
|
alpar@1677
|
65 |
///\e
|
alpar@1677
|
66 |
void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
|
alpar@1805
|
67 |
///Number of \c true items in the map
|
alpar@1805
|
68 |
|
alpar@1805
|
69 |
///Returns the number of \c true values in the map.
|
alpar@1805
|
70 |
///This is a constant time operation.
|
alpar@1805
|
71 |
int countTrue() { return vals.size()-sep; }
|
alpar@1805
|
72 |
///Number of \c false items in the map
|
alpar@1805
|
73 |
|
alpar@1805
|
74 |
///Returns the number of \c false values in the map.
|
alpar@1805
|
75 |
///This is a constant time operation.
|
alpar@1805
|
76 |
int countFalse() { return sep; }
|
alpar@1677
|
77 |
|
alpar@1677
|
78 |
///\e
|
alpar@1677
|
79 |
class FalseIt
|
alpar@1677
|
80 |
{
|
alpar@1677
|
81 |
const IterableBoolMap &M;
|
alpar@1677
|
82 |
int i;
|
alpar@1677
|
83 |
public:
|
alpar@1805
|
84 |
///\e
|
alpar@1677
|
85 |
explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
|
alpar@1805
|
86 |
///\e
|
alpar@1677
|
87 |
FalseIt(Invalid)
|
alpar@1677
|
88 |
: M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
|
alpar@1805
|
89 |
///\e
|
alpar@1677
|
90 |
FalseIt &operator++() { ++i; return *this;}
|
alpar@1805
|
91 |
///\e
|
alpar@1677
|
92 |
operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
|
alpar@1805
|
93 |
///\e
|
alpar@1677
|
94 |
bool operator !=(Invalid) const { return i<M.sep; }
|
alpar@1805
|
95 |
///\e
|
alpar@1677
|
96 |
bool operator ==(Invalid) const { return i>=M.sep; }
|
alpar@1677
|
97 |
};
|
alpar@1677
|
98 |
///\e
|
alpar@1677
|
99 |
class TrueIt
|
alpar@1677
|
100 |
{
|
alpar@1677
|
101 |
const IterableBoolMap &M;
|
alpar@1677
|
102 |
int i;
|
alpar@1677
|
103 |
public:
|
alpar@1805
|
104 |
///\e
|
alpar@1677
|
105 |
explicit TrueIt(const IterableBoolMap &_M)
|
alpar@1677
|
106 |
: M(_M), i(M.vals.size()-1) { }
|
alpar@1805
|
107 |
///\e
|
alpar@1677
|
108 |
TrueIt(Invalid)
|
alpar@1677
|
109 |
: M(*((IterableBoolMap*)(0))), i(-1) { }
|
alpar@1805
|
110 |
///\e
|
alpar@1677
|
111 |
TrueIt &operator++() { --i; return *this;}
|
alpar@1805
|
112 |
///\e
|
alpar@1677
|
113 |
operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
|
alpar@1805
|
114 |
///\e
|
alpar@1677
|
115 |
bool operator !=(Invalid) const { return i>=M.sep; }
|
alpar@1805
|
116 |
///\e
|
alpar@1677
|
117 |
bool operator ==(Invalid) const { return i<M.sep; }
|
alpar@1677
|
118 |
};
|
alpar@1677
|
119 |
|
alpar@1677
|
120 |
///\e
|
alpar@1677
|
121 |
class RefType
|
alpar@1677
|
122 |
{
|
alpar@1677
|
123 |
IterableBoolMap &M;
|
alpar@1677
|
124 |
Key k;
|
alpar@1677
|
125 |
public:
|
alpar@1677
|
126 |
RefType(IterableBoolMap &_M,Key _k) : M(_M), k(_k) { }
|
alpar@1677
|
127 |
|
alpar@1677
|
128 |
operator Value() const
|
alpar@1677
|
129 |
{
|
alpar@1677
|
130 |
return M.isTrue(k);
|
alpar@1677
|
131 |
}
|
alpar@1677
|
132 |
Value operator = (Value v) const { M.set(k,v); return v; }
|
alpar@1677
|
133 |
};
|
alpar@1677
|
134 |
|
alpar@1677
|
135 |
public:
|
alpar@1677
|
136 |
explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
|
alpar@1677
|
137 |
{
|
alpar@1677
|
138 |
sep=0;
|
deba@1810
|
139 |
for(typename BaseMap::MapIt i(cref);i!=INVALID; ++i) {
|
deba@1810
|
140 |
i.set(sep);
|
deba@1810
|
141 |
vals.push_back(i);
|
alpar@1677
|
142 |
sep++;
|
alpar@1677
|
143 |
}
|
alpar@1677
|
144 |
if(init) sep=0;
|
alpar@1677
|
145 |
}
|
alpar@1805
|
146 |
///\e
|
alpar@1677
|
147 |
RefType operator[] (Key k) { return RefType(*this,k);}
|
alpar@1805
|
148 |
///\e
|
alpar@1677
|
149 |
Value operator[] (Key k) const { return isTrue(k);}
|
alpar@1677
|
150 |
};
|
alpar@1677
|
151 |
|
alpar@1677
|
152 |
|
alpar@1677
|
153 |
|
alpar@1677
|
154 |
|
alpar@1677
|
155 |
/// \addtogroup graph_maps
|
alpar@1677
|
156 |
/// @{
|
alpar@1677
|
157 |
|
alpar@1677
|
158 |
/// Iterable bool NodeMap
|
alpar@1677
|
159 |
|
alpar@1677
|
160 |
/// This map can be used in the same way
|
alpar@1677
|
161 |
/// as the standard NodeMap<bool> of the
|
alpar@1677
|
162 |
/// given graph \c Graph.
|
alpar@1677
|
163 |
/// In addition, this class provides two iterators called \ref TrueIt
|
alpar@1677
|
164 |
/// and \ref FalseIt to iterate through the "true" and "false" nodes.
|
alpar@1677
|
165 |
template <class Graph>
|
alpar@1677
|
166 |
class IterableBoolNodeMap
|
alpar@1677
|
167 |
{
|
deba@1685
|
168 |
typename Graph::template NodeMap<int> cmap;
|
alpar@1677
|
169 |
|
alpar@1677
|
170 |
public:
|
alpar@1677
|
171 |
|
deba@1685
|
172 |
typedef IterableBoolMap<typename Graph::template NodeMap<int> > BimType;
|
alpar@1677
|
173 |
BimType imap;
|
alpar@1677
|
174 |
|
alpar@1677
|
175 |
|
alpar@1677
|
176 |
typedef typename BimType::RefType RefType;
|
alpar@1677
|
177 |
typedef typename Graph::Node Key;
|
alpar@1677
|
178 |
typedef bool Value;
|
alpar@1677
|
179 |
|
alpar@1677
|
180 |
friend class FalseIt;
|
alpar@1677
|
181 |
friend class TrueIt;
|
alpar@1677
|
182 |
|
alpar@1677
|
183 |
///\e
|
alpar@1677
|
184 |
IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
|
alpar@1677
|
185 |
|
alpar@1677
|
186 |
public:
|
alpar@1677
|
187 |
///\e
|
alpar@1677
|
188 |
void set(Key k, bool v) { imap.set(k,v);}
|
alpar@1805
|
189 |
///Number of \c true items in the map
|
alpar@1805
|
190 |
|
alpar@1805
|
191 |
///Returns the number of \c true values in the map.
|
alpar@1805
|
192 |
///This is a constant time operation.
|
alpar@1805
|
193 |
int countTrue() { return imap.countTrue(); }
|
alpar@1805
|
194 |
///Number of \c false items in the map
|
alpar@1805
|
195 |
|
alpar@1805
|
196 |
///Returns the number of \c false values in the map.
|
alpar@1805
|
197 |
///This is a constant time operation.
|
alpar@1805
|
198 |
int countFalse() { return imap.countFalse(); }
|
alpar@1677
|
199 |
#ifdef DOXYGEN
|
alpar@1677
|
200 |
///\e
|
alpar@1677
|
201 |
bool &operator[](Key k) { return imap[k];}
|
alpar@1677
|
202 |
///\e
|
alpar@1677
|
203 |
const bool &operator[](Key k) const { return imap[k];}
|
alpar@1677
|
204 |
#else
|
alpar@1677
|
205 |
Value operator[](Key k) const { return imap[k];}
|
alpar@1677
|
206 |
RefType operator[](Key k) { return imap[k];}
|
alpar@1677
|
207 |
#endif
|
alpar@1677
|
208 |
///Iterator for the "false" nodes
|
alpar@1677
|
209 |
class FalseIt : public BimType::FalseIt
|
alpar@1677
|
210 |
{
|
alpar@1677
|
211 |
public:
|
alpar@1805
|
212 |
///\e
|
alpar@1677
|
213 |
explicit FalseIt(const IterableBoolNodeMap &m)
|
alpar@1677
|
214 |
: BimType::FalseIt(m.imap) { }
|
alpar@1805
|
215 |
///\e
|
alpar@1677
|
216 |
FalseIt(Invalid i) : BimType::FalseIt(i) { }
|
alpar@1677
|
217 |
};
|
alpar@1677
|
218 |
///Iterator for the "true" nodes
|
alpar@1677
|
219 |
class TrueIt : public BimType::TrueIt
|
alpar@1677
|
220 |
{
|
alpar@1677
|
221 |
public:
|
alpar@1805
|
222 |
///\e
|
alpar@1677
|
223 |
explicit TrueIt(const IterableBoolNodeMap &m)
|
alpar@1677
|
224 |
: BimType::TrueIt(m.imap) { }
|
alpar@1805
|
225 |
///\e
|
alpar@1677
|
226 |
TrueIt(Invalid i) : BimType::TrueIt(i) { }
|
alpar@1677
|
227 |
};
|
alpar@1677
|
228 |
};
|
alpar@1677
|
229 |
|
alpar@1873
|
230 |
/// Iterable bool UpperNodeMap
|
alpar@1873
|
231 |
|
alpar@1873
|
232 |
/// This map can be used in the same way
|
alpar@1873
|
233 |
/// as the standard UpperNodeMap<bool> of the
|
alpar@1873
|
234 |
/// given graph \c Graph.
|
alpar@1873
|
235 |
/// In addition, this class provides two iterators called \ref TrueIt
|
alpar@1873
|
236 |
/// and \ref FalseIt to iterate through the "true" and "false" nodes.
|
alpar@1873
|
237 |
template <class Graph>
|
alpar@1873
|
238 |
class IterableBoolUpperNodeMap
|
alpar@1873
|
239 |
{
|
alpar@1873
|
240 |
typename Graph::template UpperNodeMap<int> cmap;
|
alpar@1873
|
241 |
|
alpar@1873
|
242 |
public:
|
alpar@1873
|
243 |
|
alpar@1873
|
244 |
typedef IterableBoolMap<typename Graph::template UpperNodeMap<int> > BimType;
|
alpar@1873
|
245 |
BimType imap;
|
alpar@1873
|
246 |
|
alpar@1873
|
247 |
|
alpar@1873
|
248 |
typedef typename BimType::RefType RefType;
|
alpar@1873
|
249 |
typedef typename Graph::Node Key;
|
alpar@1873
|
250 |
typedef bool Value;
|
alpar@1873
|
251 |
|
alpar@1873
|
252 |
friend class FalseIt;
|
alpar@1873
|
253 |
friend class TrueIt;
|
alpar@1873
|
254 |
|
alpar@1873
|
255 |
///\e
|
alpar@1873
|
256 |
IterableBoolUpperNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
|
alpar@1873
|
257 |
|
alpar@1873
|
258 |
public:
|
alpar@1873
|
259 |
///\e
|
alpar@1873
|
260 |
void set(Key k, bool v) { imap.set(k,v);}
|
alpar@1873
|
261 |
///Number of \c true items in the map
|
alpar@1873
|
262 |
|
alpar@1873
|
263 |
///Returns the number of \c true values in the map.
|
alpar@1873
|
264 |
///This is a constant time operation.
|
alpar@1873
|
265 |
int countTrue() { return imap.countTrue(); }
|
alpar@1873
|
266 |
///Number of \c false items in the map
|
alpar@1873
|
267 |
|
alpar@1873
|
268 |
///Returns the number of \c false values in the map.
|
alpar@1873
|
269 |
///This is a constant time operation.
|
alpar@1873
|
270 |
int countFalse() { return imap.countFalse(); }
|
alpar@1873
|
271 |
#ifdef DOXYGEN
|
alpar@1873
|
272 |
///\e
|
alpar@1873
|
273 |
bool &operator[](Key k) { return imap[k];}
|
alpar@1873
|
274 |
///\e
|
alpar@1873
|
275 |
const bool &operator[](Key k) const { return imap[k];}
|
alpar@1873
|
276 |
#else
|
alpar@1873
|
277 |
Value operator[](Key k) const { return imap[k];}
|
alpar@1873
|
278 |
RefType operator[](Key k) { return imap[k];}
|
alpar@1873
|
279 |
#endif
|
alpar@1873
|
280 |
///Iterator for the "false" nodes
|
alpar@1873
|
281 |
class FalseIt : public BimType::FalseIt
|
alpar@1873
|
282 |
{
|
alpar@1873
|
283 |
public:
|
alpar@1873
|
284 |
///\e
|
alpar@1873
|
285 |
explicit FalseIt(const IterableBoolUpperNodeMap &m)
|
alpar@1873
|
286 |
: BimType::FalseIt(m.imap) { }
|
alpar@1873
|
287 |
///\e
|
alpar@1873
|
288 |
FalseIt(Invalid i) : BimType::FalseIt(i) { }
|
alpar@1873
|
289 |
};
|
alpar@1873
|
290 |
///Iterator for the "true" nodes
|
alpar@1873
|
291 |
class TrueIt : public BimType::TrueIt
|
alpar@1873
|
292 |
{
|
alpar@1873
|
293 |
public:
|
alpar@1873
|
294 |
///\e
|
alpar@1873
|
295 |
explicit TrueIt(const IterableBoolUpperNodeMap &m)
|
alpar@1873
|
296 |
: BimType::TrueIt(m.imap) { }
|
alpar@1873
|
297 |
///\e
|
alpar@1873
|
298 |
TrueIt(Invalid i) : BimType::TrueIt(i) { }
|
alpar@1873
|
299 |
};
|
alpar@1873
|
300 |
};
|
alpar@1873
|
301 |
|
alpar@1873
|
302 |
/// Iterable bool LowerNodeMap
|
alpar@1873
|
303 |
|
alpar@1873
|
304 |
/// This map can be used in the same way
|
alpar@1873
|
305 |
/// as the standard LowerNodeMap<bool> of the
|
alpar@1873
|
306 |
/// given graph \c Graph.
|
alpar@1873
|
307 |
/// In addition, this class provides two iterators called \ref TrueIt
|
alpar@1873
|
308 |
/// and \ref FalseIt to iterate through the "true" and "false" nodes.
|
alpar@1873
|
309 |
template <class Graph>
|
alpar@1873
|
310 |
class IterableBoolLowerNodeMap
|
alpar@1873
|
311 |
{
|
alpar@1873
|
312 |
typename Graph::template LowerNodeMap<int> cmap;
|
alpar@1873
|
313 |
|
alpar@1873
|
314 |
public:
|
alpar@1873
|
315 |
|
alpar@1873
|
316 |
typedef IterableBoolMap<typename Graph::template LowerNodeMap<int> > BimType;
|
alpar@1873
|
317 |
BimType imap;
|
alpar@1873
|
318 |
|
alpar@1873
|
319 |
|
alpar@1873
|
320 |
typedef typename BimType::RefType RefType;
|
alpar@1873
|
321 |
typedef typename Graph::Node Key;
|
alpar@1873
|
322 |
typedef bool Value;
|
alpar@1873
|
323 |
|
alpar@1873
|
324 |
friend class FalseIt;
|
alpar@1873
|
325 |
friend class TrueIt;
|
alpar@1873
|
326 |
|
alpar@1873
|
327 |
///\e
|
alpar@1873
|
328 |
IterableBoolLowerNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
|
alpar@1873
|
329 |
|
alpar@1873
|
330 |
public:
|
alpar@1873
|
331 |
///\e
|
alpar@1873
|
332 |
void set(Key k, bool v) { imap.set(k,v);}
|
alpar@1873
|
333 |
///Number of \c true items in the map
|
alpar@1873
|
334 |
|
alpar@1873
|
335 |
///Returns the number of \c true values in the map.
|
alpar@1873
|
336 |
///This is a constant time operation.
|
alpar@1873
|
337 |
int countTrue() { return imap.countTrue(); }
|
alpar@1873
|
338 |
///Number of \c false items in the map
|
alpar@1873
|
339 |
|
alpar@1873
|
340 |
///Returns the number of \c false values in the map.
|
alpar@1873
|
341 |
///This is a constant time operation.
|
alpar@1873
|
342 |
int countFalse() { return imap.countFalse(); }
|
alpar@1873
|
343 |
#ifdef DOXYGEN
|
alpar@1873
|
344 |
///\e
|
alpar@1873
|
345 |
bool &operator[](Key k) { return imap[k];}
|
alpar@1873
|
346 |
///\e
|
alpar@1873
|
347 |
const bool &operator[](Key k) const { return imap[k];}
|
alpar@1873
|
348 |
#else
|
alpar@1873
|
349 |
Value operator[](Key k) const { return imap[k];}
|
alpar@1873
|
350 |
RefType operator[](Key k) { return imap[k];}
|
alpar@1873
|
351 |
#endif
|
alpar@1873
|
352 |
///Iterator for the "false" nodes
|
alpar@1873
|
353 |
class FalseIt : public BimType::FalseIt
|
alpar@1873
|
354 |
{
|
alpar@1873
|
355 |
public:
|
alpar@1873
|
356 |
///\e
|
alpar@1873
|
357 |
explicit FalseIt(const IterableBoolLowerNodeMap &m)
|
alpar@1873
|
358 |
: BimType::FalseIt(m.imap) { }
|
alpar@1873
|
359 |
///\e
|
alpar@1873
|
360 |
FalseIt(Invalid i) : BimType::FalseIt(i) { }
|
alpar@1873
|
361 |
};
|
alpar@1873
|
362 |
///Iterator for the "true" nodes
|
alpar@1873
|
363 |
class TrueIt : public BimType::TrueIt
|
alpar@1873
|
364 |
{
|
alpar@1873
|
365 |
public:
|
alpar@1873
|
366 |
///\e
|
alpar@1873
|
367 |
explicit TrueIt(const IterableBoolLowerNodeMap &m)
|
alpar@1873
|
368 |
: BimType::TrueIt(m.imap) { }
|
alpar@1873
|
369 |
///\e
|
alpar@1873
|
370 |
TrueIt(Invalid i) : BimType::TrueIt(i) { }
|
alpar@1873
|
371 |
};
|
alpar@1873
|
372 |
};
|
alpar@1873
|
373 |
|
alpar@1677
|
374 |
/// Iterable bool EdgeMap
|
alpar@1677
|
375 |
|
alpar@1677
|
376 |
/// This map can be used in the same way
|
alpar@1677
|
377 |
/// as the standard EdgeMap<bool> of the
|
alpar@1677
|
378 |
/// given graph \c Graph.
|
alpar@1677
|
379 |
/// In addition, this class provides two iterators called \ref TrueIt
|
alpar@1677
|
380 |
/// and \ref FalseIt to iterate through the "true" and "false" edges.
|
alpar@1677
|
381 |
template <class Graph>
|
alpar@1677
|
382 |
class IterableBoolEdgeMap
|
alpar@1677
|
383 |
{
|
deba@1685
|
384 |
typename Graph::template EdgeMap<int> cmap;
|
alpar@1677
|
385 |
|
alpar@1677
|
386 |
public:
|
alpar@1677
|
387 |
|
deba@1685
|
388 |
typedef IterableBoolMap<typename Graph::template EdgeMap<int> > BimType;
|
alpar@1677
|
389 |
BimType imap;
|
alpar@1677
|
390 |
|
alpar@1677
|
391 |
|
alpar@1677
|
392 |
typedef typename BimType::RefType RefType;
|
alpar@1677
|
393 |
typedef typename Graph::Edge Key;
|
alpar@1677
|
394 |
typedef bool Value;
|
alpar@1677
|
395 |
|
alpar@1677
|
396 |
friend class FalseIt;
|
alpar@1677
|
397 |
friend class TrueIt;
|
alpar@1677
|
398 |
|
alpar@1677
|
399 |
///\e
|
alpar@1677
|
400 |
IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
|
alpar@1677
|
401 |
|
alpar@1677
|
402 |
public:
|
alpar@1677
|
403 |
///\e
|
alpar@1677
|
404 |
void set(Key k, bool v) { imap.set(k,v);}
|
alpar@1805
|
405 |
///Returns the number of \c true values in the map.
|
alpar@1805
|
406 |
///This is a constant time operation.
|
alpar@1805
|
407 |
int countTrue() { return imap.countTrue(); }
|
alpar@1805
|
408 |
///Number of \c false items in the map
|
alpar@1805
|
409 |
|
alpar@1805
|
410 |
///Returns the number of \c false values in the map.
|
alpar@1805
|
411 |
///This is a constant time operation.
|
alpar@1805
|
412 |
int countFalse() { return imap.countFalse(); }
|
alpar@1677
|
413 |
#ifdef DOXYGEN
|
alpar@1677
|
414 |
///\e
|
alpar@1677
|
415 |
bool &operator[](Key k) { return imap[k];}
|
alpar@1677
|
416 |
///\e
|
alpar@1677
|
417 |
const bool &operator[](Key k) const { return imap[k];}
|
alpar@1677
|
418 |
#else
|
alpar@1677
|
419 |
Value operator[](Key k) const { return imap[k];}
|
alpar@1677
|
420 |
RefType operator[](Key k) { return imap[k];}
|
alpar@1677
|
421 |
#endif
|
alpar@1677
|
422 |
///Iterator for the "false" edges
|
alpar@1677
|
423 |
class FalseIt : public BimType::FalseIt
|
alpar@1677
|
424 |
{
|
alpar@1677
|
425 |
public:
|
alpar@1805
|
426 |
///\e
|
alpar@1677
|
427 |
explicit FalseIt(const IterableBoolEdgeMap &m)
|
alpar@1677
|
428 |
: BimType::FalseIt(m.imap) { }
|
alpar@1805
|
429 |
///\e
|
alpar@1677
|
430 |
FalseIt(Invalid i) : BimType::FalseIt(i) { }
|
alpar@1677
|
431 |
};
|
alpar@1677
|
432 |
///Iterator for the "true" edges
|
alpar@1677
|
433 |
class TrueIt : public BimType::TrueIt
|
alpar@1677
|
434 |
{
|
alpar@1677
|
435 |
public:
|
alpar@1805
|
436 |
///\e
|
alpar@1677
|
437 |
explicit TrueIt(const IterableBoolEdgeMap &m)
|
alpar@1677
|
438 |
: BimType::TrueIt(m.imap) { }
|
alpar@1805
|
439 |
///\e
|
alpar@1677
|
440 |
TrueIt(Invalid i) : BimType::TrueIt(i) { }
|
alpar@1677
|
441 |
};
|
alpar@1677
|
442 |
};
|
alpar@1677
|
443 |
|
deba@1752
|
444 |
|
deba@1752
|
445 |
namespace _iterable_maps_bits {
|
deba@1752
|
446 |
template <typename Item>
|
deba@1752
|
447 |
struct IterableIntMapNode {
|
deba@1810
|
448 |
IterableIntMapNode() {}
|
deba@1810
|
449 |
IterableIntMapNode(int _value) : value(_value) {}
|
deba@1752
|
450 |
Item prev, next;
|
deba@1752
|
451 |
int value;
|
deba@1752
|
452 |
};
|
deba@1752
|
453 |
}
|
deba@1752
|
454 |
|
deba@1752
|
455 |
///\ingroup maps
|
deba@1752
|
456 |
///
|
deba@1752
|
457 |
/// \brief Dynamic iterable integer map.
|
deba@1752
|
458 |
///
|
deba@1810
|
459 |
/// This class provides a special graph map type which can store
|
deba@1810
|
460 |
/// for each graph item(node, edge, etc.) an integer value. For each
|
deba@1810
|
461 |
/// non negative value it is possible to iterate on the keys which
|
deba@1810
|
462 |
/// mapped to the given value.
|
deba@1810
|
463 |
///
|
deba@1810
|
464 |
/// \param _Graph The graph type.
|
deba@1810
|
465 |
/// \param _Item One of the graph's item type, the key of the map.
|
deba@1752
|
466 |
template <typename _Graph, typename _Item>
|
deba@1752
|
467 |
class IterableIntMap : protected ItemSetTraits<_Graph, _Item>
|
deba@1752
|
468 |
::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
|
deba@1752
|
469 |
public:
|
deba@1752
|
470 |
typedef typename ItemSetTraits<_Graph, _Item>
|
deba@1752
|
471 |
::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
|
deba@1752
|
472 |
::Parent Parent;
|
deba@1752
|
473 |
|
deba@1810
|
474 |
/// The key type
|
deba@1752
|
475 |
typedef _Item Key;
|
deba@1810
|
476 |
/// The value type
|
deba@1752
|
477 |
typedef int Value;
|
deba@1810
|
478 |
/// The graph type
|
deba@1752
|
479 |
typedef _Graph Graph;
|
deba@1752
|
480 |
|
deba@1810
|
481 |
/// \brief Constructor of the Map.
|
deba@1810
|
482 |
///
|
deba@1810
|
483 |
/// Constructor of the Map. It set all values -1.
|
deba@1810
|
484 |
explicit IterableIntMap(const Graph& graph)
|
deba@1810
|
485 |
: Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(-1)) {}
|
deba@1810
|
486 |
|
deba@1810
|
487 |
/// \brief Constructor of the Map with a given value.
|
deba@1810
|
488 |
///
|
deba@1810
|
489 |
/// Constructor of the Map with a given value.
|
deba@1810
|
490 |
explicit IterableIntMap(const Graph& graph, int value)
|
deba@1810
|
491 |
: Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
|
deba@1810
|
492 |
if (value >= 0) {
|
deba@1810
|
493 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
deba@1810
|
494 |
lace(it);
|
deba@1810
|
495 |
}
|
deba@1810
|
496 |
}
|
deba@1810
|
497 |
}
|
deba@1752
|
498 |
|
deba@1752
|
499 |
private:
|
deba@1752
|
500 |
|
deba@1752
|
501 |
void unlace(const Key& key) {
|
deba@1752
|
502 |
typename Parent::Value& node = Parent::operator[](key);
|
deba@1752
|
503 |
if (node.value < 0) return;
|
deba@1752
|
504 |
if (node.prev != INVALID) {
|
deba@1752
|
505 |
Parent::operator[](node.prev).next = node.next;
|
deba@1752
|
506 |
} else {
|
deba@1752
|
507 |
first[node.value] = node.next;
|
deba@1752
|
508 |
}
|
deba@1752
|
509 |
if (node.next != INVALID) {
|
deba@1752
|
510 |
Parent::operator[](node.next).prev = node.prev;
|
deba@1752
|
511 |
}
|
deba@1752
|
512 |
while (!first.empty() && first.back() == INVALID) {
|
deba@1752
|
513 |
first.pop_back();
|
deba@1752
|
514 |
}
|
deba@1752
|
515 |
}
|
deba@1752
|
516 |
|
deba@1752
|
517 |
void lace(const Key& key) {
|
deba@1752
|
518 |
typename Parent::Value& node = Parent::operator[](key);
|
deba@1752
|
519 |
if (node.value < 0) return;
|
deba@1752
|
520 |
if (node.value >= (int)first.size()) {
|
deba@1752
|
521 |
first.resize(node.value + 1, INVALID);
|
deba@1752
|
522 |
}
|
deba@1752
|
523 |
node.prev = INVALID;
|
deba@1752
|
524 |
node.next = first[node.value];
|
deba@1752
|
525 |
if (node.next != INVALID) {
|
deba@1752
|
526 |
Parent::operator[](node.next).prev = key;
|
deba@1752
|
527 |
}
|
deba@1752
|
528 |
first[node.value] = key;
|
deba@1752
|
529 |
}
|
deba@1752
|
530 |
|
deba@1752
|
531 |
public:
|
deba@1752
|
532 |
|
deba@1810
|
533 |
/// Indicates that the map if reference map.
|
deba@1752
|
534 |
typedef True ReferenceMapTag;
|
deba@1752
|
535 |
|
deba@1810
|
536 |
/// \brief Refernce to the value of the map.
|
deba@1810
|
537 |
///
|
deba@1810
|
538 |
/// This class is near to similar to the int type. It can
|
deba@1810
|
539 |
/// be converted to int and it has the same operators.
|
deba@1752
|
540 |
class Reference {
|
deba@1752
|
541 |
friend class IterableIntMap;
|
deba@1752
|
542 |
private:
|
deba@1752
|
543 |
Reference(IterableIntMap& map, const Key& key)
|
deba@1752
|
544 |
: _key(key), _map(map) {}
|
deba@1752
|
545 |
public:
|
deba@1752
|
546 |
|
deba@1752
|
547 |
Reference& operator=(const Reference& value) {
|
deba@1752
|
548 |
_map.set(_key, (const int&)value);
|
deba@1752
|
549 |
return *this;
|
deba@1752
|
550 |
}
|
deba@1752
|
551 |
|
deba@1752
|
552 |
operator const int&() const {
|
deba@1752
|
553 |
return static_cast<const IterableIntMap&>(_map)[_key];
|
deba@1752
|
554 |
}
|
deba@1752
|
555 |
|
deba@1752
|
556 |
Reference& operator=(int value) {
|
deba@1752
|
557 |
_map.set(_key, value);
|
deba@1752
|
558 |
return *this;
|
deba@1752
|
559 |
}
|
deba@1759
|
560 |
Reference& operator++() {
|
deba@1759
|
561 |
_map.set(_key, _map[_key] + 1);
|
deba@1759
|
562 |
return *this;
|
deba@1759
|
563 |
}
|
deba@1759
|
564 |
int operator++(int) {
|
deba@1759
|
565 |
int value = _map[_key];
|
deba@1759
|
566 |
_map.set(_key, value + 1);
|
deba@1759
|
567 |
return value;
|
deba@1759
|
568 |
}
|
deba@1759
|
569 |
Reference& operator--() {
|
deba@1759
|
570 |
_map.set(_key, _map[_key] - 1);
|
deba@1759
|
571 |
return *this;
|
deba@1759
|
572 |
}
|
deba@1759
|
573 |
int operator--(int) {
|
deba@1759
|
574 |
int value = _map[_key];
|
deba@1759
|
575 |
_map.set(_key, value - 1);
|
deba@1759
|
576 |
return value;
|
deba@1759
|
577 |
}
|
deba@1752
|
578 |
Reference& operator+=(int value) {
|
deba@1752
|
579 |
_map.set(_key, _map[_key] + value);
|
deba@1752
|
580 |
return *this;
|
deba@1752
|
581 |
}
|
deba@1752
|
582 |
Reference& operator-=(int value) {
|
deba@1752
|
583 |
_map.set(_key, _map[_key] - value);
|
deba@1752
|
584 |
return *this;
|
deba@1752
|
585 |
}
|
deba@1752
|
586 |
Reference& operator*=(int value) {
|
deba@1752
|
587 |
_map.set(_key, _map[_key] * value);
|
deba@1752
|
588 |
return *this;
|
deba@1752
|
589 |
}
|
deba@1752
|
590 |
Reference& operator/=(int value) {
|
deba@1752
|
591 |
_map.set(_key, _map[_key] / value);
|
deba@1752
|
592 |
return *this;
|
deba@1752
|
593 |
}
|
deba@1752
|
594 |
Reference& operator%=(int value) {
|
deba@1752
|
595 |
_map.set(_key, _map[_key] % value);
|
deba@1752
|
596 |
return *this;
|
deba@1752
|
597 |
}
|
deba@1752
|
598 |
Reference& operator&=(int value) {
|
deba@1752
|
599 |
_map.set(_key, _map[_key] & value);
|
deba@1752
|
600 |
return *this;
|
deba@1752
|
601 |
}
|
deba@1752
|
602 |
Reference& operator|=(int value) {
|
deba@1752
|
603 |
_map.set(_key, _map[_key] | value);
|
deba@1752
|
604 |
return *this;
|
deba@1752
|
605 |
}
|
deba@1752
|
606 |
Reference& operator^=(int value) {
|
deba@1752
|
607 |
_map.set(_key, _map[_key] ^ value);
|
deba@1752
|
608 |
return *this;
|
deba@1752
|
609 |
}
|
deba@1752
|
610 |
Reference& operator<<=(int value) {
|
deba@1752
|
611 |
_map.set(_key, _map[_key] << value);
|
deba@1752
|
612 |
return *this;
|
deba@1752
|
613 |
}
|
deba@1752
|
614 |
Reference& operator>>=(int value) {
|
deba@1752
|
615 |
_map.set(_key, _map[_key] >> value);
|
deba@1752
|
616 |
return *this;
|
deba@1752
|
617 |
}
|
deba@1752
|
618 |
|
deba@1752
|
619 |
private:
|
deba@1752
|
620 |
Key _key;
|
deba@1752
|
621 |
IterableIntMap& _map;
|
deba@1752
|
622 |
};
|
deba@1810
|
623 |
|
deba@1810
|
624 |
/// The const reference type.
|
deba@1752
|
625 |
typedef const Value& ConstReference;
|
deba@1752
|
626 |
|
deba@1810
|
627 |
/// \brief Gives back the maximal value plus one.
|
deba@1810
|
628 |
///
|
deba@1810
|
629 |
/// Gives back the maximal value plus one.
|
deba@1752
|
630 |
int size() const {
|
deba@1752
|
631 |
return (int)first.size();
|
deba@1752
|
632 |
}
|
deba@1752
|
633 |
|
deba@1810
|
634 |
/// \brief Set operation of the map.
|
deba@1810
|
635 |
///
|
deba@1810
|
636 |
/// Set operation of the map.
|
deba@1752
|
637 |
void set(const Key& key, const Value& value) {
|
deba@1752
|
638 |
unlace(key);
|
deba@1752
|
639 |
Parent::operator[](key).value = value;
|
deba@1752
|
640 |
lace(key);
|
deba@1752
|
641 |
}
|
deba@1752
|
642 |
|
deba@1810
|
643 |
/// \brief Const subscript operator of the map.
|
deba@1810
|
644 |
///
|
deba@1810
|
645 |
/// Const subscript operator of the map.
|
deba@1752
|
646 |
const Value& operator[](const Key& key) const {
|
deba@1752
|
647 |
return Parent::operator[](key).value;
|
deba@1752
|
648 |
}
|
deba@1752
|
649 |
|
deba@1810
|
650 |
/// \brief Subscript operator of the map.
|
deba@1810
|
651 |
///
|
deba@1810
|
652 |
/// Subscript operator of the map.
|
deba@1752
|
653 |
Reference operator[](const Key& key) {
|
deba@1752
|
654 |
return Reference(*this, key);
|
deba@1752
|
655 |
}
|
deba@1752
|
656 |
|
deba@1810
|
657 |
/// \brief Iterator for the keys with the same value.
|
deba@1810
|
658 |
///
|
deba@1810
|
659 |
/// Iterator for the keys with the same value. It works
|
deba@1810
|
660 |
/// like a graph item iterator in the map, it can be converted
|
deba@1810
|
661 |
/// the item type of the map, incremented with \c ++ operator, and
|
deba@1810
|
662 |
/// if the iterator leave the last valid item it will be equal to
|
deba@1810
|
663 |
/// \c INVALID.
|
deba@1752
|
664 |
class ItemIt : public _Item {
|
deba@1752
|
665 |
public:
|
deba@1752
|
666 |
typedef _Item Parent;
|
deba@1752
|
667 |
|
deba@1810
|
668 |
/// \brief Invalid constructor \& conversion.
|
deba@1810
|
669 |
///
|
deba@1810
|
670 |
/// This constructor initializes the item to be invalid.
|
deba@1810
|
671 |
/// \sa Invalid for more details.
|
deba@1752
|
672 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
deba@1752
|
673 |
|
deba@1810
|
674 |
/// \brief Creates an iterator with a value.
|
deba@1810
|
675 |
///
|
deba@1810
|
676 |
/// Creates an iterator with a value. It iterates on the
|
deba@1810
|
677 |
/// keys which have the given value.
|
deba@1810
|
678 |
/// \param map The IterableIntMap
|
deba@1810
|
679 |
/// \param value The value
|
deba@1752
|
680 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
deba@1752
|
681 |
if (value < 0 || value >= (int)_map->first.size()) {
|
deba@1752
|
682 |
Parent::operator=(INVALID);
|
deba@1752
|
683 |
} else {
|
deba@1752
|
684 |
Parent::operator=(_map->first[value]);
|
deba@1752
|
685 |
}
|
deba@1752
|
686 |
}
|
deba@1752
|
687 |
|
deba@1810
|
688 |
/// \brief Increment operator.
|
deba@1810
|
689 |
///
|
deba@1810
|
690 |
/// Increment Operator.
|
deba@1752
|
691 |
ItemIt& operator++() {
|
deba@1752
|
692 |
Parent::operator=(_map->IterableIntMap::Parent::
|
deba@1752
|
693 |
operator[](static_cast<Parent&>(*this)).next);
|
deba@1752
|
694 |
return *this;
|
deba@1752
|
695 |
}
|
deba@1752
|
696 |
|
deba@1752
|
697 |
|
deba@1752
|
698 |
private:
|
deba@1752
|
699 |
const IterableIntMap* _map;
|
deba@1752
|
700 |
};
|
deba@1752
|
701 |
|
deba@1752
|
702 |
protected:
|
deba@1752
|
703 |
|
deba@1752
|
704 |
virtual void erase(const Key& key) {
|
deba@1752
|
705 |
unlace(key);
|
deba@1752
|
706 |
Parent::erase(key);
|
deba@1752
|
707 |
}
|
deba@1752
|
708 |
|
deba@1752
|
709 |
virtual void clear() {
|
deba@1752
|
710 |
first.clear();
|
deba@1752
|
711 |
Parent::clear();
|
deba@1752
|
712 |
}
|
deba@1752
|
713 |
|
deba@1752
|
714 |
private:
|
deba@1752
|
715 |
std::vector<_Item> first;
|
deba@1752
|
716 |
};
|
deba@1752
|
717 |
|
alpar@1677
|
718 |
/// @}
|
alpar@1677
|
719 |
}
|