0
2
5
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
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. |
|
12 |
* |
|
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 |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
|
20 |
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
|
21 |
|
|
22 |
#include <lemon/core.h> |
|
23 |
#include <lemon/error.h> |
|
24 |
|
|
25 |
#include <lemon/bits/default_map.h> |
|
26 |
|
|
27 |
|
|
28 |
///\ingroup digraphbits |
|
29 |
///\file |
|
30 |
///\brief Extenders for the digraph adaptor types |
|
31 |
namespace lemon { |
|
32 |
|
|
33 |
/// \ingroup digraphbits |
|
34 |
/// |
|
35 |
/// \brief Extender for the DigraphAdaptors |
|
36 |
template <typename _Digraph> |
|
37 |
class DigraphAdaptorExtender : public _Digraph { |
|
38 |
public: |
|
39 |
|
|
40 |
typedef _Digraph Parent; |
|
41 |
typedef _Digraph Digraph; |
|
42 |
typedef DigraphAdaptorExtender Adaptor; |
|
43 |
|
|
44 |
// Base extensions |
|
45 |
|
|
46 |
typedef typename Parent::Node Node; |
|
47 |
typedef typename Parent::Arc Arc; |
|
48 |
|
|
49 |
int maxId(Node) const { |
|
50 |
return Parent::maxNodeId(); |
|
51 |
} |
|
52 |
|
|
53 |
int maxId(Arc) const { |
|
54 |
return Parent::maxArcId(); |
|
55 |
} |
|
56 |
|
|
57 |
Node fromId(int id, Node) const { |
|
58 |
return Parent::nodeFromId(id); |
|
59 |
} |
|
60 |
|
|
61 |
Arc fromId(int id, Arc) const { |
|
62 |
return Parent::arcFromId(id); |
|
63 |
} |
|
64 |
|
|
65 |
Node oppositeNode(const Node &n, const Arc &e) const { |
|
66 |
if (n == Parent::source(e)) |
|
67 |
return Parent::target(e); |
|
68 |
else if(n==Parent::target(e)) |
|
69 |
return Parent::source(e); |
|
70 |
else |
|
71 |
return INVALID; |
|
72 |
} |
|
73 |
|
|
74 |
class NodeIt : public Node { |
|
75 |
const Adaptor* _adaptor; |
|
76 |
public: |
|
77 |
|
|
78 |
NodeIt() {} |
|
79 |
|
|
80 |
NodeIt(Invalid i) : Node(i) { } |
|
81 |
|
|
82 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
83 |
_adaptor->first(static_cast<Node&>(*this)); |
|
84 |
} |
|
85 |
|
|
86 |
NodeIt(const Adaptor& adaptor, const Node& node) |
|
87 |
: Node(node), _adaptor(&adaptor) {} |
|
88 |
|
|
89 |
NodeIt& operator++() { |
|
90 |
_adaptor->next(*this); |
|
91 |
return *this; |
|
92 |
} |
|
93 |
|
|
94 |
}; |
|
95 |
|
|
96 |
|
|
97 |
class ArcIt : public Arc { |
|
98 |
const Adaptor* _adaptor; |
|
99 |
public: |
|
100 |
|
|
101 |
ArcIt() { } |
|
102 |
|
|
103 |
ArcIt(Invalid i) : Arc(i) { } |
|
104 |
|
|
105 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
106 |
_adaptor->first(static_cast<Arc&>(*this)); |
|
107 |
} |
|
108 |
|
|
109 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
|
110 |
Arc(e), _adaptor(&adaptor) { } |
|
111 |
|
|
112 |
ArcIt& operator++() { |
|
113 |
_adaptor->next(*this); |
|
114 |
return *this; |
|
115 |
} |
|
116 |
|
|
117 |
}; |
|
118 |
|
|
119 |
|
|
120 |
class OutArcIt : public Arc { |
|
121 |
const Adaptor* _adaptor; |
|
122 |
public: |
|
123 |
|
|
124 |
OutArcIt() { } |
|
125 |
|
|
126 |
OutArcIt(Invalid i) : Arc(i) { } |
|
127 |
|
|
128 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
|
129 |
: _adaptor(&adaptor) { |
|
130 |
_adaptor->firstOut(*this, node); |
|
131 |
} |
|
132 |
|
|
133 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
|
134 |
: Arc(arc), _adaptor(&adaptor) {} |
|
135 |
|
|
136 |
OutArcIt& operator++() { |
|
137 |
_adaptor->nextOut(*this); |
|
138 |
return *this; |
|
139 |
} |
|
140 |
|
|
141 |
}; |
|
142 |
|
|
143 |
|
|
144 |
class InArcIt : public Arc { |
|
145 |
const Adaptor* _adaptor; |
|
146 |
public: |
|
147 |
|
|
148 |
InArcIt() { } |
|
149 |
|
|
150 |
InArcIt(Invalid i) : Arc(i) { } |
|
151 |
|
|
152 |
InArcIt(const Adaptor& adaptor, const Node& node) |
|
153 |
: _adaptor(&adaptor) { |
|
154 |
_adaptor->firstIn(*this, node); |
|
155 |
} |
|
156 |
|
|
157 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
|
158 |
Arc(arc), _adaptor(&adaptor) {} |
|
159 |
|
|
160 |
InArcIt& operator++() { |
|
161 |
_adaptor->nextIn(*this); |
|
162 |
return *this; |
|
163 |
} |
|
164 |
|
|
165 |
}; |
|
166 |
|
|
167 |
/// \brief Base node of the iterator |
|
168 |
/// |
|
169 |
/// Returns the base node (ie. the source in this case) of the iterator |
|
170 |
Node baseNode(const OutArcIt &e) const { |
|
171 |
return Parent::source(e); |
|
172 |
} |
|
173 |
/// \brief Running node of the iterator |
|
174 |
/// |
|
175 |
/// Returns the running node (ie. the target in this case) of the |
|
176 |
/// iterator |
|
177 |
Node runningNode(const OutArcIt &e) const { |
|
178 |
return Parent::target(e); |
|
179 |
} |
|
180 |
|
|
181 |
/// \brief Base node of the iterator |
|
182 |
/// |
|
183 |
/// Returns the base node (ie. the target in this case) of the iterator |
|
184 |
Node baseNode(const InArcIt &e) const { |
|
185 |
return Parent::target(e); |
|
186 |
} |
|
187 |
/// \brief Running node of the iterator |
|
188 |
/// |
|
189 |
/// Returns the running node (ie. the source in this case) of the |
|
190 |
/// iterator |
|
191 |
Node runningNode(const InArcIt &e) const { |
|
192 |
return Parent::source(e); |
|
193 |
} |
|
194 |
|
|
195 |
}; |
|
196 |
|
|
197 |
|
|
198 |
/// \ingroup digraphbits |
|
199 |
/// |
|
200 |
/// \brief Extender for the GraphAdaptors |
|
201 |
template <typename _Graph> |
|
202 |
class GraphAdaptorExtender : public _Graph { |
|
203 |
public: |
|
204 |
|
|
205 |
typedef _Graph Parent; |
|
206 |
typedef _Graph Graph; |
|
207 |
typedef GraphAdaptorExtender Adaptor; |
|
208 |
|
|
209 |
typedef typename Parent::Node Node; |
|
210 |
typedef typename Parent::Arc Arc; |
|
211 |
typedef typename Parent::Edge Edge; |
|
212 |
|
|
213 |
// Graph extension |
|
214 |
|
|
215 |
int maxId(Node) const { |
|
216 |
return Parent::maxNodeId(); |
|
217 |
} |
|
218 |
|
|
219 |
int maxId(Arc) const { |
|
220 |
return Parent::maxArcId(); |
|
221 |
} |
|
222 |
|
|
223 |
int maxId(Edge) const { |
|
224 |
return Parent::maxEdgeId(); |
|
225 |
} |
|
226 |
|
|
227 |
Node fromId(int id, Node) const { |
|
228 |
return Parent::nodeFromId(id); |
|
229 |
} |
|
230 |
|
|
231 |
Arc fromId(int id, Arc) const { |
|
232 |
return Parent::arcFromId(id); |
|
233 |
} |
|
234 |
|
|
235 |
Edge fromId(int id, Edge) const { |
|
236 |
return Parent::edgeFromId(id); |
|
237 |
} |
|
238 |
|
|
239 |
Node oppositeNode(const Node &n, const Edge &e) const { |
|
240 |
if( n == Parent::u(e)) |
|
241 |
return Parent::v(e); |
|
242 |
else if( n == Parent::v(e)) |
|
243 |
return Parent::u(e); |
|
244 |
else |
|
245 |
return INVALID; |
|
246 |
} |
|
247 |
|
|
248 |
Arc oppositeArc(const Arc &a) const { |
|
249 |
return Parent::direct(a, !Parent::direction(a)); |
|
250 |
} |
|
251 |
|
|
252 |
using Parent::direct; |
|
253 |
Arc direct(const Edge &e, const Node &s) const { |
|
254 |
return Parent::direct(e, Parent::u(e) == s); |
|
255 |
} |
|
256 |
|
|
257 |
|
|
258 |
class NodeIt : public Node { |
|
259 |
const Adaptor* _adaptor; |
|
260 |
public: |
|
261 |
|
|
262 |
NodeIt() {} |
|
263 |
|
|
264 |
NodeIt(Invalid i) : Node(i) { } |
|
265 |
|
|
266 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
267 |
_adaptor->first(static_cast<Node&>(*this)); |
|
268 |
} |
|
269 |
|
|
270 |
NodeIt(const Adaptor& adaptor, const Node& node) |
|
271 |
: Node(node), _adaptor(&adaptor) {} |
|
272 |
|
|
273 |
NodeIt& operator++() { |
|
274 |
_adaptor->next(*this); |
|
275 |
return *this; |
|
276 |
} |
|
277 |
|
|
278 |
}; |
|
279 |
|
|
280 |
|
|
281 |
class ArcIt : public Arc { |
|
282 |
const Adaptor* _adaptor; |
|
283 |
public: |
|
284 |
|
|
285 |
ArcIt() { } |
|
286 |
|
|
287 |
ArcIt(Invalid i) : Arc(i) { } |
|
288 |
|
|
289 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
290 |
_adaptor->first(static_cast<Arc&>(*this)); |
|
291 |
} |
|
292 |
|
|
293 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
|
294 |
Arc(e), _adaptor(&adaptor) { } |
|
295 |
|
|
296 |
ArcIt& operator++() { |
|
297 |
_adaptor->next(*this); |
|
298 |
return *this; |
|
299 |
} |
|
300 |
|
|
301 |
}; |
|
302 |
|
|
303 |
|
|
304 |
class OutArcIt : public Arc { |
|
305 |
const Adaptor* _adaptor; |
|
306 |
public: |
|
307 |
|
|
308 |
OutArcIt() { } |
|
309 |
|
|
310 |
OutArcIt(Invalid i) : Arc(i) { } |
|
311 |
|
|
312 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
|
313 |
: _adaptor(&adaptor) { |
|
314 |
_adaptor->firstOut(*this, node); |
|
315 |
} |
|
316 |
|
|
317 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
|
318 |
: Arc(arc), _adaptor(&adaptor) {} |
|
319 |
|
|
320 |
OutArcIt& operator++() { |
|
321 |
_adaptor->nextOut(*this); |
|
322 |
return *this; |
|
323 |
} |
|
324 |
|
|
325 |
}; |
|
326 |
|
|
327 |
|
|
328 |
class InArcIt : public Arc { |
|
329 |
const Adaptor* _adaptor; |
|
330 |
public: |
|
331 |
|
|
332 |
InArcIt() { } |
|
333 |
|
|
334 |
InArcIt(Invalid i) : Arc(i) { } |
|
335 |
|
|
336 |
InArcIt(const Adaptor& adaptor, const Node& node) |
|
337 |
: _adaptor(&adaptor) { |
|
338 |
_adaptor->firstIn(*this, node); |
|
339 |
} |
|
340 |
|
|
341 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
|
342 |
Arc(arc), _adaptor(&adaptor) {} |
|
343 |
|
|
344 |
InArcIt& operator++() { |
|
345 |
_adaptor->nextIn(*this); |
|
346 |
return *this; |
|
347 |
} |
|
348 |
|
|
349 |
}; |
|
350 |
|
|
351 |
class EdgeIt : public Parent::Edge { |
|
352 |
const Adaptor* _adaptor; |
|
353 |
public: |
|
354 |
|
|
355 |
EdgeIt() { } |
|
356 |
|
|
357 |
EdgeIt(Invalid i) : Edge(i) { } |
|
358 |
|
|
359 |
explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
360 |
_adaptor->first(static_cast<Edge&>(*this)); |
|
361 |
} |
|
362 |
|
|
363 |
EdgeIt(const Adaptor& adaptor, const Edge& e) : |
|
364 |
Edge(e), _adaptor(&adaptor) { } |
|
365 |
|
|
366 |
EdgeIt& operator++() { |
|
367 |
_adaptor->next(*this); |
|
368 |
return *this; |
|
369 |
} |
|
370 |
|
|
371 |
}; |
|
372 |
|
|
373 |
class IncEdgeIt : public Edge { |
|
374 |
friend class GraphAdaptorExtender; |
|
375 |
const Adaptor* _adaptor; |
|
376 |
bool direction; |
|
377 |
public: |
|
378 |
|
|
379 |
IncEdgeIt() { } |
|
380 |
|
|
381 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
|
382 |
|
|
383 |
IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) { |
|
384 |
_adaptor->firstInc(static_cast<Edge&>(*this), direction, n); |
|
385 |
} |
|
386 |
|
|
387 |
IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n) |
|
388 |
: _adaptor(&adaptor), Edge(e) { |
|
389 |
direction = (_adaptor->u(e) == n); |
|
390 |
} |
|
391 |
|
|
392 |
IncEdgeIt& operator++() { |
|
393 |
_adaptor->nextInc(*this, direction); |
|
394 |
return *this; |
|
395 |
} |
|
396 |
}; |
|
397 |
|
|
398 |
/// \brief Base node of the iterator |
|
399 |
/// |
|
400 |
/// Returns the base node (ie. the source in this case) of the iterator |
|
401 |
Node baseNode(const OutArcIt &a) const { |
|
402 |
return Parent::source(a); |
|
403 |
} |
|
404 |
/// \brief Running node of the iterator |
|
405 |
/// |
|
406 |
/// Returns the running node (ie. the target in this case) of the |
|
407 |
/// iterator |
|
408 |
Node runningNode(const OutArcIt &a) const { |
|
409 |
return Parent::target(a); |
|
410 |
} |
|
411 |
|
|
412 |
/// \brief Base node of the iterator |
|
413 |
/// |
|
414 |
/// Returns the base node (ie. the target in this case) of the iterator |
|
415 |
Node baseNode(const InArcIt &a) const { |
|
416 |
return Parent::target(a); |
|
417 |
} |
|
418 |
/// \brief Running node of the iterator |
|
419 |
/// |
|
420 |
/// Returns the running node (ie. the source in this case) of the |
|
421 |
/// iterator |
|
422 |
Node runningNode(const InArcIt &a) const { |
|
423 |
return Parent::source(a); |
|
424 |
} |
|
425 |
|
|
426 |
/// Base node of the iterator |
|
427 |
/// |
|
428 |
/// Returns the base node of the iterator |
|
429 |
Node baseNode(const IncEdgeIt &e) const { |
|
430 |
return e.direction ? Parent::u(e) : Parent::v(e); |
|
431 |
} |
|
432 |
/// Running node of the iterator |
|
433 |
/// |
|
434 |
/// Returns the running node of the iterator |
|
435 |
Node runningNode(const IncEdgeIt &e) const { |
|
436 |
return e.direction ? Parent::v(e) : Parent::u(e); |
|
437 |
} |
|
438 |
|
|
439 |
}; |
|
440 |
|
|
441 |
} |
|
442 |
|
|
443 |
|
|
444 |
#endif |
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
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. |
|
12 |
* |
|
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 |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_VARIANT_H |
|
20 |
#define LEMON_BITS_VARIANT_H |
|
21 |
|
|
22 |
#include <lemon/assert.h> |
|
23 |
|
|
24 |
/// \file |
|
25 |
/// \brief Variant types |
|
26 |
|
|
27 |
namespace lemon { |
|
28 |
|
|
29 |
namespace _variant_bits { |
|
30 |
|
|
31 |
template <int left, int right> |
|
32 |
struct CTMax { |
|
33 |
static const int value = left < right ? right : left; |
|
34 |
}; |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
|
|
39 |
/// \brief Simple Variant type for two types |
|
40 |
/// |
|
41 |
/// Simple Variant type for two types. The Variant type is a type |
|
42 |
/// safe union. The C++ has strong limitations for using unions, by |
|
43 |
/// example we can not store type with non default constructor or |
|
44 |
/// destructor in an union. This class always knowns the current |
|
45 |
/// state of the variant and it cares for the proper construction |
|
46 |
/// and destruction. |
|
47 |
template <typename _First, typename _Second> |
|
48 |
class BiVariant { |
|
49 |
public: |
|
50 |
|
|
51 |
/// \brief The \c First type. |
|
52 |
typedef _First First; |
|
53 |
/// \brief The \c Second type. |
|
54 |
typedef _Second Second; |
|
55 |
|
|
56 |
/// \brief Constructor |
|
57 |
/// |
|
58 |
/// This constructor initalizes to the default value of the \c First |
|
59 |
/// type. |
|
60 |
BiVariant() { |
|
61 |
flag = true; |
|
62 |
new(reinterpret_cast<First*>(data)) First(); |
|
63 |
} |
|
64 |
|
|
65 |
/// \brief Constructor |
|
66 |
/// |
|
67 |
/// This constructor initalizes to the given value of the \c First |
|
68 |
/// type. |
|
69 |
BiVariant(const First& f) { |
|
70 |
flag = true; |
|
71 |
new(reinterpret_cast<First*>(data)) First(f); |
|
72 |
} |
|
73 |
|
|
74 |
/// \brief Constructor |
|
75 |
/// |
|
76 |
/// This constructor initalizes to the given value of the \c |
|
77 |
/// Second type. |
|
78 |
BiVariant(const Second& s) { |
|
79 |
flag = false; |
|
80 |
new(reinterpret_cast<Second*>(data)) Second(s); |
|
81 |
} |
|
82 |
|
|
83 |
/// \brief Copy constructor |
|
84 |
/// |
|
85 |
/// Copy constructor |
|
86 |
BiVariant(const BiVariant& bivariant) { |
|
87 |
flag = bivariant.flag; |
|
88 |
if (flag) { |
|
89 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
|
90 |
} else { |
|
91 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/// \brief Destrcutor |
|
96 |
/// |
|
97 |
/// Destructor |
|
98 |
~BiVariant() { |
|
99 |
destroy(); |
|
100 |
} |
|
101 |
|
|
102 |
/// \brief Set to the default value of the \c First type. |
|
103 |
/// |
|
104 |
/// This function sets the variant to the default value of the \c |
|
105 |
/// First type. |
|
106 |
BiVariant& setFirst() { |
|
107 |
destroy(); |
|
108 |
flag = true; |
|
109 |
new(reinterpret_cast<First*>(data)) First(); |
|
110 |
return *this; |
|
111 |
} |
|
112 |
|
|
113 |
/// \brief Set to the given value of the \c First type. |
|
114 |
/// |
|
115 |
/// This function sets the variant to the given value of the \c |
|
116 |
/// First type. |
|
117 |
BiVariant& setFirst(const First& f) { |
|
118 |
destroy(); |
|
119 |
flag = true; |
|
120 |
new(reinterpret_cast<First*>(data)) First(f); |
|
121 |
return *this; |
|
122 |
} |
|
123 |
|
|
124 |
/// \brief Set to the default value of the \c Second type. |
|
125 |
/// |
|
126 |
/// This function sets the variant to the default value of the \c |
|
127 |
/// Second type. |
|
128 |
BiVariant& setSecond() { |
|
129 |
destroy(); |
|
130 |
flag = false; |
|
131 |
new(reinterpret_cast<Second*>(data)) Second(); |
|
132 |
return *this; |
|
133 |
} |
|
134 |
|
|
135 |
/// \brief Set to the given value of the \c Second type. |
|
136 |
/// |
|
137 |
/// This function sets the variant to the given value of the \c |
|
138 |
/// Second type. |
|
139 |
BiVariant& setSecond(const Second& s) { |
|
140 |
destroy(); |
|
141 |
flag = false; |
|
142 |
new(reinterpret_cast<Second*>(data)) Second(s); |
|
143 |
return *this; |
|
144 |
} |
|
145 |
|
|
146 |
/// \brief Operator form of the \c setFirst() |
|
147 |
BiVariant& operator=(const First& f) { |
|
148 |
return setFirst(f); |
|
149 |
} |
|
150 |
|
|
151 |
/// \brief Operator form of the \c setSecond() |
|
152 |
BiVariant& operator=(const Second& s) { |
|
153 |
return setSecond(s); |
|
154 |
} |
|
155 |
|
|
156 |
/// \brief Assign operator |
|
157 |
BiVariant& operator=(const BiVariant& bivariant) { |
|
158 |
if (this == &bivariant) return *this; |
|
159 |
destroy(); |
|
160 |
flag = bivariant.flag; |
|
161 |
if (flag) { |
|
162 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
|
163 |
} else { |
|
164 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
|
165 |
} |
|
166 |
return *this; |
|
167 |
} |
|
168 |
|
|
169 |
/// \brief Reference to the value |
|
170 |
/// |
|
171 |
/// Reference to the value of the \c First type. |
|
172 |
/// \pre The BiVariant should store value of \c First type. |
|
173 |
First& first() { |
|
174 |
LEMON_DEBUG(flag, "Variant wrong state"); |
|
175 |
return *reinterpret_cast<First*>(data); |
|
176 |
} |
|
177 |
|
|
178 |
/// \brief Const reference to the value |
|
179 |
/// |
|
180 |
/// Const reference to the value of the \c First type. |
|
181 |
/// \pre The BiVariant should store value of \c First type. |
|
182 |
const First& first() const { |
|
183 |
LEMON_DEBUG(flag, "Variant wrong state"); |
|
184 |
return *reinterpret_cast<const First*>(data); |
|
185 |
} |
|
186 |
|
|
187 |
/// \brief Operator form of the \c first() |
|
188 |
operator First&() { return first(); } |
|
189 |
/// \brief Operator form of the const \c first() |
|
190 |
operator const First&() const { return first(); } |
|
191 |
|
|
192 |
/// \brief Reference to the value |
|
193 |
/// |
|
194 |
/// Reference to the value of the \c Second type. |
|
195 |
/// \pre The BiVariant should store value of \c Second type. |
|
196 |
Second& second() { |
|
197 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
|
198 |
return *reinterpret_cast<Second*>(data); |
|
199 |
} |
|
200 |
|
|
201 |
/// \brief Const reference to the value |
|
202 |
/// |
|
203 |
/// Const reference to the value of the \c Second type. |
|
204 |
/// \pre The BiVariant should store value of \c Second type. |
|
205 |
const Second& second() const { |
|
206 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
|
207 |
return *reinterpret_cast<const Second*>(data); |
|
208 |
} |
|
209 |
|
|
210 |
/// \brief Operator form of the \c second() |
|
211 |
operator Second&() { return second(); } |
|
212 |
/// \brief Operator form of the const \c second() |
|
213 |
operator const Second&() const { return second(); } |
|
214 |
|
|
215 |
/// \brief %True when the variant is in the first state |
|
216 |
/// |
|
217 |
/// %True when the variant stores value of the \c First type. |
|
218 |
bool firstState() const { return flag; } |
|
219 |
|
|
220 |
/// \brief %True when the variant is in the second state |
|
221 |
/// |
|
222 |
/// %True when the variant stores value of the \c Second type. |
|
223 |
bool secondState() const { return !flag; } |
|
224 |
|
|
225 |
private: |
|
226 |
|
|
227 |
void destroy() { |
|
228 |
if (flag) { |
|
229 |
reinterpret_cast<First*>(data)->~First(); |
|
230 |
} else { |
|
231 |
reinterpret_cast<Second*>(data)->~Second(); |
|
232 |
} |
|
233 |
} |
|
234 |
|
|
235 |
char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value]; |
|
236 |
bool flag; |
|
237 |
}; |
|
238 |
|
|
239 |
namespace _variant_bits { |
|
240 |
|
|
241 |
template <int _idx, typename _TypeMap> |
|
242 |
struct Memory { |
|
243 |
|
|
244 |
typedef typename _TypeMap::template Map<_idx>::Type Current; |
|
245 |
|
|
246 |
static void destroy(int index, char* place) { |
|
247 |
if (index == _idx) { |
|
248 |
reinterpret_cast<Current*>(place)->~Current(); |
|
249 |
} else { |
|
250 |
Memory<_idx - 1, _TypeMap>::destroy(index, place); |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
static void copy(int index, char* to, const char* from) { |
|
255 |
if (index == _idx) { |
|
256 |
new (reinterpret_cast<Current*>(to)) |
|
257 |
Current(reinterpret_cast<const Current*>(from)); |
|
258 |
} else { |
|
259 |
Memory<_idx - 1, _TypeMap>::copy(index, to, from); |
|
260 |
} |
|
261 |
} |
|
262 |
|
|
263 |
}; |
|
264 |
|
|
265 |
template <typename _TypeMap> |
|
266 |
struct Memory<-1, _TypeMap> { |
|
267 |
|
|
268 |
static void destroy(int, char*) { |
|
269 |
LEMON_DEBUG(false, "Variant wrong index."); |
|
270 |
} |
|
271 |
|
|
272 |
static void copy(int, char*, const char*) { |
|
273 |
LEMON_DEBUG(false, "Variant wrong index."); |
|
274 |
} |
|
275 |
}; |
|
276 |
|
|
277 |
template <int _idx, typename _TypeMap> |
|
278 |
struct Size { |
|
279 |
static const int value = |
|
280 |
CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type), |
|
281 |
Size<_idx - 1, _TypeMap>::value>::value; |
|
282 |
}; |
|
283 |
|
|
284 |
template <typename _TypeMap> |
|
285 |
struct Size<0, _TypeMap> { |
|
286 |
static const int value = |
|
287 |
sizeof(typename _TypeMap::template Map<0>::Type); |
|
288 |
}; |
|
289 |
|
|
290 |
} |
|
291 |
|
|
292 |
/// \brief Variant type |
|
293 |
/// |
|
294 |
/// Simple Variant type. The Variant type is a type safe union. The |
|
295 |
/// C++ has strong limitations for using unions, for example we |
|
296 |
/// cannot store type with non default constructor or destructor in |
|
297 |
/// a union. This class always knowns the current state of the |
|
298 |
/// variant and it cares for the proper construction and |
|
299 |
/// destruction. |
|
300 |
/// |
|
301 |
/// \param _num The number of the types which can be stored in the |
|
302 |
/// variant type. |
|
303 |
/// \param _TypeMap This class describes the types of the Variant. The |
|
304 |
/// _TypeMap::Map<index>::Type should be a valid type for each index |
|
305 |
/// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper |
|
306 |
/// class to define such type mappings up to 10 types. |
|
307 |
/// |
|
308 |
/// And the usage of the class: |
|
309 |
///\code |
|
310 |
/// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant; |
|
311 |
/// MyVariant var; |
|
312 |
/// var.set<0>(12); |
|
313 |
/// std::cout << var.get<0>() << std::endl; |
|
314 |
/// var.set<1>("alpha"); |
|
315 |
/// std::cout << var.get<1>() << std::endl; |
|
316 |
/// var.set<2>(0.75); |
|
317 |
/// std::cout << var.get<2>() << std::endl; |
|
318 |
///\endcode |
|
319 |
/// |
|
320 |
/// The result of course: |
|
321 |
///\code |
|
322 |
/// 12 |
|
323 |
/// alpha |
|
324 |
/// 0.75 |
|
325 |
///\endcode |
|
326 |
template <int _num, typename _TypeMap> |
|
327 |
class Variant { |
|
328 |
public: |
|
329 |
|
|
330 |
static const int num = _num; |
|
331 |
|
|
332 |
typedef _TypeMap TypeMap; |
|
333 |
|
|
334 |
/// \brief Constructor |
|
335 |
/// |
|
336 |
/// This constructor initalizes to the default value of the \c type |
|
337 |
/// with 0 index. |
|
338 |
Variant() { |
|
339 |
flag = 0; |
|
340 |
new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data)) |
|
341 |
typename TypeMap::template Map<0>::Type(); |
|
342 |
} |
|
343 |
|
|
344 |
|
|
345 |
/// \brief Copy constructor |
|
346 |
/// |
|
347 |
/// Copy constructor |
|
348 |
Variant(const Variant& variant) { |
|
349 |
flag = variant.flag; |
|
350 |
_variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data); |
|
351 |
} |
|
352 |
|
|
353 |
/// \brief Assign operator |
|
354 |
/// |
|
355 |
/// Assign operator |
|
356 |
Variant& operator=(const Variant& variant) { |
|
357 |
if (this == &variant) return *this; |
|
358 |
_variant_bits::Memory<num - 1, TypeMap>:: |
|
359 |
destroy(flag, data); |
|
360 |
flag = variant.flag; |
|
361 |
_variant_bits::Memory<num - 1, TypeMap>:: |
|
362 |
copy(flag, data, variant.data); |
|
363 |
return *this; |
|
364 |
} |
|
365 |
|
|
366 |
/// \brief Destrcutor |
|
367 |
/// |
|
368 |
/// Destructor |
|
369 |
~Variant() { |
|
370 |
_variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); |
|
371 |
} |
|
372 |
|
|
373 |
/// \brief Set to the default value of the type with \c _idx index. |
|
374 |
/// |
|
375 |
/// This function sets the variant to the default value of the |
|
376 |
/// type with \c _idx index. |
|
377 |
template <int _idx> |
|
378 |
Variant& set() { |
|
379 |
_variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); |
|
380 |
flag = _idx; |
|
381 |
new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) |
|
382 |
typename TypeMap::template Map<_idx>::Type(); |
|
383 |
return *this; |
|
384 |
} |
|
385 |
|
|
386 |
/// \brief Set to the given value of the type with \c _idx index. |
|
387 |
/// |
|
388 |
/// This function sets the variant to the given value of the type |
|
389 |
/// with \c _idx index. |
|
390 |
template <int _idx> |
|
391 |
Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) { |
|
392 |
_variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); |
|
393 |
flag = _idx; |
|
394 |
new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) |
|
395 |
typename TypeMap::template Map<_idx>::Type(init); |
|
396 |
return *this; |
|
397 |
} |
|
398 |
|
|
399 |
/// \brief Gets the current value of the type with \c _idx index. |
|
400 |
/// |
|
401 |
/// Gets the current value of the type with \c _idx index. |
|
402 |
template <int _idx> |
|
403 |
const typename TypeMap::template Map<_idx>::Type& get() const { |
|
404 |
LEMON_DEBUG(_idx == flag, "Variant wrong index"); |
|
405 |
return *reinterpret_cast<const typename TypeMap:: |
|
406 |
template Map<_idx>::Type*>(data); |
|
407 |
} |
|
408 |
|
|
409 |
/// \brief Gets the current value of the type with \c _idx index. |
|
410 |
/// |
|
411 |
/// Gets the current value of the type with \c _idx index. |
|
412 |
template <int _idx> |
|
413 |
typename _TypeMap::template Map<_idx>::Type& get() { |
|
414 |
LEMON_DEBUG(_idx == flag, "Variant wrong index"); |
|
415 |
return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*> |
|
416 |
(data); |
|
417 |
} |
|
418 |
|
|
419 |
/// \brief Returns the current state of the variant. |
|
420 |
/// |
|
421 |
/// Returns the current state of the variant. |
|
422 |
int state() const { |
|
423 |
return flag; |
|
424 |
} |
|
425 |
|
|
426 |
private: |
|
427 |
|
|
428 |
char data[_variant_bits::Size<num - 1, TypeMap>::value]; |
|
429 |
int flag; |
|
430 |
}; |
|
431 |
|
|
432 |
namespace _variant_bits { |
|
433 |
|
|
434 |
template <int _index, typename _List> |
|
435 |
struct Get { |
|
436 |
typedef typename Get<_index - 1, typename _List::Next>::Type Type; |
|
437 |
}; |
|
438 |
|
|
439 |
template <typename _List> |
|
440 |
struct Get<0, _List> { |
|
441 |
typedef typename _List::Type Type; |
|
442 |
}; |
|
443 |
|
|
444 |
struct List {}; |
|
445 |
|
|
446 |
template <typename _Type, typename _List> |
|
447 |
struct Insert { |
|
448 |
typedef _List Next; |
|
449 |
typedef _Type Type; |
|
450 |
}; |
|
451 |
|
|
452 |
template <int _idx, typename _T0, typename _T1, typename _T2, |
|
453 |
typename _T3, typename _T5, typename _T4, typename _T6, |
|
454 |
typename _T7, typename _T8, typename _T9> |
|
455 |
struct Mapper { |
|
456 |
typedef List L10; |
|
457 |
typedef Insert<_T9, L10> L9; |
|
458 |
typedef Insert<_T8, L9> L8; |
|
459 |
typedef Insert<_T7, L8> L7; |
|
460 |
typedef Insert<_T6, L7> L6; |
|
461 |
typedef Insert<_T5, L6> L5; |
|
462 |
typedef Insert<_T4, L5> L4; |
|
463 |
typedef Insert<_T3, L4> L3; |
|
464 |
typedef Insert<_T2, L3> L2; |
|
465 |
typedef Insert<_T1, L2> L1; |
|
466 |
typedef Insert<_T0, L1> L0; |
|
467 |
typedef typename Get<_idx, L0>::Type Type; |
|
468 |
}; |
|
469 |
|
|
470 |
} |
|
471 |
|
|
472 |
/// \brief Helper class for Variant |
|
473 |
/// |
|
474 |
/// Helper class to define type mappings for Variant. This class |
|
475 |
/// converts the template parameters to be mappable by integer. |
|
476 |
/// \see Variant |
|
477 |
template < |
|
478 |
typename _T0, |
|
479 |
typename _T1 = void, typename _T2 = void, typename _T3 = void, |
|
480 |
typename _T5 = void, typename _T4 = void, typename _T6 = void, |
|
481 |
typename _T7 = void, typename _T8 = void, typename _T9 = void> |
|
482 |
struct VariantTypeMap { |
|
483 |
template <int _idx> |
|
484 |
struct Map { |
|
485 |
typedef typename _variant_bits:: |
|
486 |
Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type |
|
487 |
Type; |
|
488 |
}; |
|
489 |
}; |
|
490 |
|
|
491 |
} |
|
492 |
|
|
493 |
|
|
494 |
#endif |
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
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. |
|
12 |
* |
|
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 |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_DIGRAPH_ADAPTOR_H |
|
20 |
#define LEMON_DIGRAPH_ADAPTOR_H |
|
21 |
|
|
22 |
///\ingroup graph_adaptors |
|
23 |
///\file |
|
24 |
///\brief Several digraph adaptors. |
|
25 |
/// |
|
26 |
///This file contains several useful digraph adaptor functions. |
|
27 |
|
|
28 |
#include <lemon/core.h> |
|
29 |
#include <lemon/maps.h> |
|
30 |
#include <lemon/bits/variant.h> |
|
31 |
|
|
32 |
#include <lemon/bits/base_extender.h> |
|
33 |
#include <lemon/bits/graph_adaptor_extender.h> |
|
34 |
#include <lemon/bits/graph_extender.h> |
|
35 |
#include <lemon/tolerance.h> |
|
36 |
|
|
37 |
#include <algorithm> |
|
38 |
|
|
39 |
namespace lemon { |
|
40 |
|
|
41 |
///\brief Base type for the Digraph Adaptors |
|
42 |
/// |
|
43 |
///Base type for the Digraph Adaptors |
|
44 |
/// |
|
45 |
///This is the base type for most of LEMON digraph adaptors. This |
|
46 |
///class implements a trivial digraph adaptor i.e. it only wraps the |
|
47 |
///functions and types of the digraph. The purpose of this class is |
|
48 |
///to make easier implementing digraph adaptors. E.g. if an adaptor |
|
49 |
///is considered which differs from the wrapped digraph only in some |
|
50 |
///of its functions or types, then it can be derived from |
|
51 |
///DigraphAdaptor, and only the differences should be implemented. |
|
52 |
template<typename _Digraph> |
|
53 |
class DigraphAdaptorBase { |
|
54 |
public: |
|
55 |
typedef _Digraph Digraph; |
|
56 |
typedef DigraphAdaptorBase Adaptor; |
|
57 |
typedef Digraph ParentDigraph; |
|
58 |
|
|
59 |
protected: |
|
60 |
Digraph* _digraph; |
|
61 |
DigraphAdaptorBase() : _digraph(0) { } |
|
62 |
void setDigraph(Digraph& digraph) { _digraph = &digraph; } |
|
63 |
|
|
64 |
public: |
|
65 |
DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { } |
|
66 |
|
|
67 |
typedef typename Digraph::Node Node; |
|
68 |
typedef typename Digraph::Arc Arc; |
|
69 |
|
|
70 |
void first(Node& i) const { _digraph->first(i); } |
|
71 |
void first(Arc& i) const { _digraph->first(i); } |
|
72 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
|
73 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
|
74 |
|
|
75 |
void next(Node& i) const { _digraph->next(i); } |
|
76 |
void next(Arc& i) const { _digraph->next(i); } |
|
77 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
|
78 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
|
79 |
|
|
80 |
Node source(const Arc& a) const { return _digraph->source(a); } |
|
81 |
Node target(const Arc& a) const { return _digraph->target(a); } |
|
82 |
|
|
83 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
|
84 |
int nodeNum() const { return _digraph->nodeNum(); } |
|
85 |
|
|
86 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
|
87 |
int arcNum() const { return _digraph->arcNum(); } |
|
88 |
|
|
89 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
90 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { |
|
91 |
return _digraph->findArc(u, v, prev); |
|
92 |
} |
|
93 |
|
|
94 |
Node addNode() { return _digraph->addNode(); } |
|
95 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
|
96 |
|
|
97 |
void erase(const Node& n) const { _digraph->erase(n); } |
|
98 |
void erase(const Arc& a) const { _digraph->erase(a); } |
|
99 |
|
|
100 |
void clear() const { _digraph->clear(); } |
|
101 |
|
|
102 |
int id(const Node& n) const { return _digraph->id(n); } |
|
103 |
int id(const Arc& a) const { return _digraph->id(a); } |
|
104 |
|
|
105 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
|
106 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
|
107 |
|
|
108 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
|
109 |
int maxArcId() const { return _digraph->maxArcId(); } |
|
110 |
|
|
111 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
|
112 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
|
113 |
|
|
114 |
typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier; |
|
115 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
|
116 |
|
|
117 |
template <typename _Value> |
|
118 |
class NodeMap : public Digraph::template NodeMap<_Value> { |
|
119 |
public: |
|
120 |
|
|
121 |
typedef typename Digraph::template NodeMap<_Value> Parent; |
|
122 |
|
|
123 |
explicit NodeMap(const Adaptor& adaptor) |
|
124 |
: Parent(*adaptor._digraph) {} |
|
125 |
|
|
126 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
|
127 |
: Parent(*adaptor._digraph, value) { } |
|
128 |
|
|
129 |
private: |
|
130 |
NodeMap& operator=(const NodeMap& cmap) { |
|
131 |
return operator=<NodeMap>(cmap); |
|
132 |
} |
|
133 |
|
|
134 |
template <typename CMap> |
|
135 |
NodeMap& operator=(const CMap& cmap) { |
|
136 |
Parent::operator=(cmap); |
|
137 |
return *this; |
|
138 |
} |
|
139 |
|
|
140 |
}; |
|
141 |
|
|
142 |
template <typename _Value> |
|
143 |
class ArcMap : public Digraph::template ArcMap<_Value> { |
|
144 |
public: |
|
145 |
|
|
146 |
typedef typename Digraph::template ArcMap<_Value> Parent; |
|
147 |
|
|
148 |
explicit ArcMap(const Adaptor& adaptor) |
|
149 |
: Parent(*adaptor._digraph) {} |
|
150 |
|
|
151 |
ArcMap(const Adaptor& adaptor, const _Value& value) |
|
152 |
: Parent(*adaptor._digraph, value) {} |
|
153 |
|
|
154 |
private: |
|
155 |
ArcMap& operator=(const ArcMap& cmap) { |
|
156 |
return operator=<ArcMap>(cmap); |
|
157 |
} |
|
158 |
|
|
159 |
template <typename CMap> |
|
160 |
ArcMap& operator=(const CMap& cmap) { |
|
161 |
Parent::operator=(cmap); |
|
162 |
return *this; |
|
163 |
} |
|
164 |
|
|
165 |
}; |
|
166 |
|
|
167 |
}; |
|
168 |
|
|
169 |
///\ingroup graph_adaptors |
|
170 |
/// |
|
171 |
///\brief Trivial Digraph Adaptor |
|
172 |
/// |
|
173 |
/// This class is an adaptor which does not change the adapted |
|
174 |
/// digraph. It can be used only to test the digraph adaptors. |
|
175 |
template <typename _Digraph> |
|
176 |
class DigraphAdaptor : |
|
177 |
public DigraphAdaptorExtender<DigraphAdaptorBase<_Digraph> > { |
|
178 |
public: |
|
179 |
typedef _Digraph Digraph; |
|
180 |
typedef DigraphAdaptorExtender<DigraphAdaptorBase<_Digraph> > Parent; |
|
181 |
protected: |
|
182 |
DigraphAdaptor() : Parent() { } |
|
183 |
|
|
184 |
public: |
|
185 |
explicit DigraphAdaptor(Digraph& digraph) { setDigraph(digraph); } |
|
186 |
}; |
|
187 |
|
|
188 |
/// \brief Just gives back a digraph adaptor |
|
189 |
/// |
|
190 |
/// Just gives back a digraph adaptor which |
|
191 |
/// should be provide original digraph |
|
192 |
template<typename Digraph> |
|
193 |
DigraphAdaptor<const Digraph> |
|
194 |
digraphAdaptor(const Digraph& digraph) { |
|
195 |
return DigraphAdaptor<const Digraph>(digraph); |
|
196 |
} |
|
197 |
|
|
198 |
|
|
199 |
template <typename _Digraph> |
|
200 |
class RevDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { |
|
201 |
public: |
|
202 |
typedef _Digraph Digraph; |
|
203 |
typedef DigraphAdaptorBase<_Digraph> Parent; |
|
204 |
protected: |
|
205 |
RevDigraphAdaptorBase() : Parent() { } |
|
206 |
public: |
|
207 |
typedef typename Parent::Node Node; |
|
208 |
typedef typename Parent::Arc Arc; |
|
209 |
|
|
210 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } |
|
211 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } |
|
212 |
|
|
213 |
void nextIn(Arc& a) const { Parent::nextOut(a); } |
|
214 |
void nextOut(Arc& a) const { Parent::nextIn(a); } |
|
215 |
|
|
216 |
Node source(const Arc& a) const { return Parent::target(a); } |
|
217 |
Node target(const Arc& a) const { return Parent::source(a); } |
|
218 |
|
|
219 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
220 |
Arc findArc(const Node& u, const Node& v, |
|
221 |
const Arc& prev = INVALID) { |
|
222 |
return Parent::findArc(v, u, prev); |
|
223 |
} |
|
224 |
|
|
225 |
}; |
|
226 |
|
|
227 |
|
|
228 |
///\ingroup graph_adaptors |
|
229 |
/// |
|
230 |
///\brief A digraph adaptor which reverses the orientation of the arcs. |
|
231 |
/// |
|
232 |
/// If \c g is defined as |
|
233 |
///\code |
|
234 |
/// ListDigraph g; |
|
235 |
///\endcode |
|
236 |
/// then |
|
237 |
///\code |
|
238 |
/// RevDigraphAdaptor<ListDigraph> ga(g); |
|
239 |
///\endcode |
|
240 |
/// implements the digraph obtained from \c g by |
|
241 |
/// reversing the orientation of its arcs. |
|
242 |
/// |
|
243 |
/// A good example of using RevDigraphAdaptor is to decide that the |
|
244 |
/// directed graph is wheter strongly connected or not. If from one |
|
245 |
/// node each node is reachable and from each node is reachable this |
|
246 |
/// node then and just then the digraph is strongly |
|
247 |
/// connected. Instead of this condition we use a little bit |
|
248 |
/// different. From one node each node ahould be reachable in the |
|
249 |
/// digraph and in the reversed digraph. Now this condition can be |
|
250 |
/// checked with the Dfs algorithm class and the RevDigraphAdaptor |
|
251 |
/// algorithm class. |
|
252 |
/// |
|
253 |
/// And look at the code: |
|
254 |
/// |
|
255 |
///\code |
|
256 |
/// bool stronglyConnected(const Digraph& digraph) { |
|
257 |
/// if (NodeIt(digraph) == INVALID) return true; |
|
258 |
/// Dfs<Digraph> dfs(digraph); |
|
259 |
/// dfs.run(NodeIt(digraph)); |
|
260 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
|
261 |
/// if (!dfs.reached(it)) { |
|
262 |
/// return false; |
|
263 |
/// } |
|
264 |
/// } |
|
265 |
/// typedef RevDigraphAdaptor<const Digraph> RDigraph; |
|
266 |
/// RDigraph rdigraph(digraph); |
|
267 |
/// DfsVisit<RDigraph> rdfs(rdigraph); |
|
268 |
/// rdfs.run(NodeIt(digraph)); |
|
269 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
|
270 |
/// if (!rdfs.reached(it)) { |
|
271 |
/// return false; |
|
272 |
/// } |
|
273 |
/// } |
|
274 |
/// return true; |
|
275 |
/// } |
|
276 |
///\endcode |
|
277 |
template<typename _Digraph> |
|
278 |
class RevDigraphAdaptor : |
|
279 |
public DigraphAdaptorExtender<RevDigraphAdaptorBase<_Digraph> > { |
|
280 |
public: |
|
281 |
typedef _Digraph Digraph; |
|
282 |
typedef DigraphAdaptorExtender< |
|
283 |
RevDigraphAdaptorBase<_Digraph> > Parent; |
|
284 |
protected: |
|
285 |
RevDigraphAdaptor() { } |
|
286 |
public: |
|
287 |
explicit RevDigraphAdaptor(Digraph& digraph) { |
|
288 |
Parent::setDigraph(digraph); |
|
289 |
} |
|
290 |
}; |
|
291 |
|
|
292 |
/// \brief Just gives back a reverse digraph adaptor |
|
293 |
/// |
|
294 |
/// Just gives back a reverse digraph adaptor |
|
295 |
template<typename Digraph> |
|
296 |
RevDigraphAdaptor<const Digraph> |
|
297 |
revDigraphAdaptor(const Digraph& digraph) { |
|
298 |
return RevDigraphAdaptor<const Digraph>(digraph); |
|
299 |
} |
|
300 |
|
|
301 |
template <typename _Digraph, typename _NodeFilterMap, |
|
302 |
typename _ArcFilterMap, bool checked = true> |
|
303 |
class SubDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { |
|
304 |
public: |
|
305 |
typedef _Digraph Digraph; |
|
306 |
typedef _NodeFilterMap NodeFilterMap; |
|
307 |
typedef _ArcFilterMap ArcFilterMap; |
|
308 |
|
|
309 |
typedef SubDigraphAdaptorBase Adaptor; |
|
310 |
typedef DigraphAdaptorBase<_Digraph> Parent; |
|
311 |
protected: |
|
312 |
NodeFilterMap* _node_filter; |
|
313 |
ArcFilterMap* _arc_filter; |
|
314 |
SubDigraphAdaptorBase() |
|
315 |
: Parent(), _node_filter(0), _arc_filter(0) { } |
|
316 |
|
|
317 |
void setNodeFilterMap(NodeFilterMap& node_filter) { |
|
318 |
_node_filter = &node_filter; |
|
319 |
} |
|
320 |
void setArcFilterMap(ArcFilterMap& arc_filter) { |
|
321 |
_arc_filter = &arc_filter; |
|
322 |
} |
|
323 |
|
|
324 |
public: |
|
325 |
|
|
326 |
typedef typename Parent::Node Node; |
|
327 |
typedef typename Parent::Arc Arc; |
|
328 |
|
|
329 |
void first(Node& i) const { |
|
330 |
Parent::first(i); |
|
331 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
332 |
} |
|
333 |
|
|
334 |
void first(Arc& i) const { |
|
335 |
Parent::first(i); |
|
336 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
337 |
|| !(*_node_filter)[Parent::source(i)] |
|
338 |
|| !(*_node_filter)[Parent::target(i)])) Parent::next(i); |
|
339 |
} |
|
340 |
|
|
341 |
void firstIn(Arc& i, const Node& n) const { |
|
342 |
Parent::firstIn(i, n); |
|
343 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
344 |
|| !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); |
|
345 |
} |
|
346 |
|
|
347 |
void firstOut(Arc& i, const Node& n) const { |
|
348 |
Parent::firstOut(i, n); |
|
349 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
350 |
|| !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); |
|
351 |
} |
|
352 |
|
|
353 |
void next(Node& i) const { |
|
354 |
Parent::next(i); |
|
355 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
356 |
} |
|
357 |
|
|
358 |
void next(Arc& i) const { |
|
359 |
Parent::next(i); |
|
360 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
361 |
|| !(*_node_filter)[Parent::source(i)] |
|
362 |
|| !(*_node_filter)[Parent::target(i)])) Parent::next(i); |
|
363 |
} |
|
364 |
|
|
365 |
void nextIn(Arc& i) const { |
|
366 |
Parent::nextIn(i); |
|
367 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
368 |
|| !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); |
|
369 |
} |
|
370 |
|
|
371 |
void nextOut(Arc& i) const { |
|
372 |
Parent::nextOut(i); |
|
373 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
374 |
|| !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); |
|
375 |
} |
|
376 |
|
|
377 |
///\e |
|
378 |
|
|
379 |
/// This function hides \c n in the digraph, i.e. the iteration |
|
380 |
/// jumps over it. This is done by simply setting the value of \c n |
|
381 |
/// to be false in the corresponding node-map. |
|
382 |
void hide(const Node& n) const { _node_filter->set(n, false); } |
|
383 |
|
|
384 |
///\e |
|
385 |
|
|
386 |
/// This function hides \c a in the digraph, i.e. the iteration |
|
387 |
/// jumps over it. This is done by simply setting the value of \c a |
|
388 |
/// to be false in the corresponding arc-map. |
|
389 |
void hide(const Arc& a) const { _arc_filter->set(a, false); } |
|
390 |
|
|
391 |
///\e |
|
392 |
|
|
393 |
/// The value of \c n is set to be true in the node-map which stores |
|
394 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
395 |
/// again |
|
396 |
void unHide(const Node& n) const { _node_filter->set(n, true); } |
|
397 |
|
|
398 |
///\e |
|
399 |
|
|
400 |
/// The value of \c a is set to be true in the arc-map which stores |
|
401 |
/// hide information. If \c a was hidden previuosly, then it is shown |
|
402 |
/// again |
|
403 |
void unHide(const Arc& a) const { _arc_filter->set(a, true); } |
|
404 |
|
|
405 |
/// Returns true if \c n is hidden. |
|
406 |
|
|
407 |
///\e |
|
408 |
/// |
|
409 |
bool hidden(const Node& n) const { return !(*_node_filter)[n]; } |
|
410 |
|
|
411 |
/// Returns true if \c a is hidden. |
|
412 |
|
|
413 |
///\e |
|
414 |
/// |
|
415 |
bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; } |
|
416 |
|
|
417 |
typedef False NodeNumTag; |
|
418 |
typedef False EdgeNumTag; |
|
419 |
|
|
420 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
421 |
Arc findArc(const Node& source, const Node& target, |
|
422 |
const Arc& prev = INVALID) { |
|
423 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
|
424 |
return INVALID; |
|
425 |
} |
|
426 |
Arc arc = Parent::findArc(source, target, prev); |
|
427 |
while (arc != INVALID && !(*_arc_filter)[arc]) { |
|
428 |
arc = Parent::findArc(source, target, arc); |
|
429 |
} |
|
430 |
return arc; |
|
431 |
} |
|
432 |
|
|
433 |
template <typename _Value> |
|
434 |
class NodeMap : public SubMapExtender<Adaptor, |
|
435 |
typename Parent::template NodeMap<_Value> > { |
|
436 |
public: |
|
437 |
typedef _Value Value; |
|
438 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
439 |
template NodeMap<Value> > MapParent; |
|
440 |
|
|
441 |
NodeMap(const Adaptor& adaptor) |
|
442 |
: MapParent(adaptor) {} |
|
443 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
444 |
: MapParent(adaptor, value) {} |
|
445 |
|
|
446 |
private: |
|
447 |
NodeMap& operator=(const NodeMap& cmap) { |
|
448 |
return operator=<NodeMap>(cmap); |
|
449 |
} |
|
450 |
|
|
451 |
template <typename CMap> |
|
452 |
NodeMap& operator=(const CMap& cmap) { |
|
453 |
MapParent::operator=(cmap); |
|
454 |
return *this; |
|
455 |
} |
|
456 |
}; |
|
457 |
|
|
458 |
template <typename _Value> |
|
459 |
class ArcMap : public SubMapExtender<Adaptor, |
|
460 |
typename Parent::template ArcMap<_Value> > { |
|
461 |
public: |
|
462 |
typedef _Value Value; |
|
463 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
464 |
template ArcMap<Value> > MapParent; |
|
465 |
|
|
466 |
ArcMap(const Adaptor& adaptor) |
|
467 |
: MapParent(adaptor) {} |
|
468 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
469 |
: MapParent(adaptor, value) {} |
|
470 |
|
|
471 |
private: |
|
472 |
ArcMap& operator=(const ArcMap& cmap) { |
|
473 |
return operator=<ArcMap>(cmap); |
|
474 |
} |
|
475 |
|
|
476 |
template <typename CMap> |
|
477 |
ArcMap& operator=(const CMap& cmap) { |
|
478 |
MapParent::operator=(cmap); |
|
479 |
return *this; |
|
480 |
} |
|
481 |
}; |
|
482 |
|
|
483 |
}; |
|
484 |
|
|
485 |
template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap> |
|
486 |
class SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> |
|
487 |
: public DigraphAdaptorBase<_Digraph> { |
|
488 |
public: |
|
489 |
typedef _Digraph Digraph; |
|
490 |
typedef _NodeFilterMap NodeFilterMap; |
|
491 |
typedef _ArcFilterMap ArcFilterMap; |
|
492 |
|
|
493 |
typedef SubDigraphAdaptorBase Adaptor; |
|
494 |
typedef DigraphAdaptorBase<Digraph> Parent; |
|
495 |
protected: |
|
496 |
NodeFilterMap* _node_filter; |
|
497 |
ArcFilterMap* _arc_filter; |
|
498 |
SubDigraphAdaptorBase() |
|
499 |
: Parent(), _node_filter(0), _arc_filter(0) { } |
|
500 |
|
|
501 |
void setNodeFilterMap(NodeFilterMap& node_filter) { |
|
502 |
_node_filter = &node_filter; |
|
503 |
} |
|
504 |
void setArcFilterMap(ArcFilterMap& arc_filter) { |
|
505 |
_arc_filter = &arc_filter; |
|
506 |
} |
|
507 |
|
|
508 |
public: |
|
509 |
|
|
510 |
typedef typename Parent::Node Node; |
|
511 |
typedef typename Parent::Arc Arc; |
|
512 |
|
|
513 |
void first(Node& i) const { |
|
514 |
Parent::first(i); |
|
515 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
516 |
} |
|
517 |
|
|
518 |
void first(Arc& i) const { |
|
519 |
Parent::first(i); |
|
520 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
|
521 |
} |
|
522 |
|
|
523 |
void firstIn(Arc& i, const Node& n) const { |
|
524 |
Parent::firstIn(i, n); |
|
525 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
|
526 |
} |
|
527 |
|
|
528 |
void firstOut(Arc& i, const Node& n) const { |
|
529 |
Parent::firstOut(i, n); |
|
530 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
|
531 |
} |
|
532 |
|
|
533 |
void next(Node& i) const { |
|
534 |
Parent::next(i); |
|
535 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
536 |
} |
|
537 |
void next(Arc& i) const { |
|
538 |
Parent::next(i); |
|
539 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
|
540 |
} |
|
541 |
void nextIn(Arc& i) const { |
|
542 |
Parent::nextIn(i); |
|
543 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
|
544 |
} |
|
545 |
|
|
546 |
void nextOut(Arc& i) const { |
|
547 |
Parent::nextOut(i); |
|
548 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
|
549 |
} |
|
550 |
|
|
551 |
///\e |
|
552 |
|
|
553 |
/// This function hides \c n in the digraph, i.e. the iteration |
|
554 |
/// jumps over it. This is done by simply setting the value of \c n |
|
555 |
/// to be false in the corresponding node-map. |
|
556 |
void hide(const Node& n) const { _node_filter->set(n, false); } |
|
557 |
|
|
558 |
///\e |
|
559 |
|
|
560 |
/// This function hides \c e in the digraph, i.e. the iteration |
|
561 |
/// jumps over it. This is done by simply setting the value of \c e |
|
562 |
/// to be false in the corresponding arc-map. |
|
563 |
void hide(const Arc& e) const { _arc_filter->set(e, false); } |
|
564 |
|
|
565 |
///\e |
|
566 |
|
|
567 |
/// The value of \c n is set to be true in the node-map which stores |
|
568 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
569 |
/// again |
|
570 |
void unHide(const Node& n) const { _node_filter->set(n, true); } |
|
571 |
|
|
572 |
///\e |
|
573 |
|
|
574 |
/// The value of \c e is set to be true in the arc-map which stores |
|
575 |
/// hide information. If \c e was hidden previuosly, then it is shown |
|
576 |
/// again |
|
577 |
void unHide(const Arc& e) const { _arc_filter->set(e, true); } |
|
578 |
|
|
579 |
/// Returns true if \c n is hidden. |
|
580 |
|
|
581 |
///\e |
|
582 |
/// |
|
583 |
bool hidden(const Node& n) const { return !(*_node_filter)[n]; } |
|
584 |
|
|
585 |
/// Returns true if \c n is hidden. |
|
586 |
|
|
587 |
///\e |
|
588 |
/// |
|
589 |
bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; } |
|
590 |
|
|
591 |
typedef False NodeNumTag; |
|
592 |
typedef False EdgeNumTag; |
|
593 |
|
|
594 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
595 |
Arc findArc(const Node& source, const Node& target, |
|
596 |
const Arc& prev = INVALID) { |
|
597 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
|
598 |
return INVALID; |
|
599 |
} |
|
600 |
Arc arc = Parent::findArc(source, target, prev); |
|
601 |
while (arc != INVALID && !(*_arc_filter)[arc]) { |
|
602 |
arc = Parent::findArc(source, target, arc); |
|
603 |
} |
|
604 |
return arc; |
|
605 |
} |
|
606 |
|
|
607 |
template <typename _Value> |
|
608 |
class NodeMap : public SubMapExtender<Adaptor, |
|
609 |
typename Parent::template NodeMap<_Value> > { |
|
610 |
public: |
|
611 |
typedef _Value Value; |
|
612 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
613 |
template NodeMap<Value> > MapParent; |
|
614 |
|
|
615 |
NodeMap(const Adaptor& adaptor) |
|
616 |
: MapParent(adaptor) {} |
|
617 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
618 |
: MapParent(adaptor, value) {} |
|
619 |
|
|
620 |
private: |
|
621 |
NodeMap& operator=(const NodeMap& cmap) { |
|
622 |
return operator=<NodeMap>(cmap); |
|
623 |
} |
|
624 |
|
|
625 |
template <typename CMap> |
|
626 |
NodeMap& operator=(const CMap& cmap) { |
|
627 |
MapParent::operator=(cmap); |
|
628 |
return *this; |
|
629 |
} |
|
630 |
}; |
|
631 |
|
|
632 |
template <typename _Value> |
|
633 |
class ArcMap : public SubMapExtender<Adaptor, |
|
634 |
typename Parent::template ArcMap<_Value> > { |
|
635 |
public: |
|
636 |
typedef _Value Value; |
|
637 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
638 |
template ArcMap<Value> > MapParent; |
|
639 |
|
|
640 |
ArcMap(const Adaptor& adaptor) |
|
641 |
: MapParent(adaptor) {} |
|
642 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
643 |
: MapParent(adaptor, value) {} |
|
644 |
|
|
645 |
private: |
|
646 |
ArcMap& operator=(const ArcMap& cmap) { |
|
647 |
return operator=<ArcMap>(cmap); |
|
648 |
} |
|
649 |
|
|
650 |
template <typename CMap> |
|
651 |
ArcMap& operator=(const CMap& cmap) { |
|
652 |
MapParent::operator=(cmap); |
|
653 |
return *this; |
|
654 |
} |
|
655 |
}; |
|
656 |
|
|
657 |
}; |
|
658 |
|
|
659 |
/// \ingroup graph_adaptors |
|
660 |
/// |
|
661 |
/// \brief A digraph adaptor for hiding nodes and arcs from a digraph. |
|
662 |
/// |
|
663 |
/// SubDigraphAdaptor shows the digraph with filtered node-set and |
|
664 |
/// arc-set. If the \c checked parameter is true then it filters the arcset |
|
665 |
/// to do not get invalid arcs without source or target. |
|
666 |
/// Let \f$ G=(V, A) \f$ be a directed digraph |
|
667 |
/// and suppose that the digraph instance \c g of type ListDigraph |
|
668 |
/// implements \f$ G \f$. |
|
669 |
/// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp. |
|
670 |
/// on the node-set and arc-set. |
|
671 |
/// SubDigraphAdaptor<...>::NodeIt iterates |
|
672 |
/// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and |
|
673 |
/// SubDigraphAdaptor<...>::ArcIt iterates |
|
674 |
/// on the arc-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly, |
|
675 |
/// SubDigraphAdaptor<...>::OutArcIt and |
|
676 |
/// SubDigraphAdaptor<...>::InArcIt iterates |
|
677 |
/// only on arcs leaving and entering a specific node which have true value. |
|
678 |
/// |
|
679 |
/// If the \c checked template parameter is false then we have to |
|
680 |
/// note that the node-iterator cares only the filter on the |
|
681 |
/// node-set, and the arc-iterator cares only the filter on the |
|
682 |
/// arc-set. This way the arc-map should filter all arcs which's |
|
683 |
/// source or target is filtered by the node-filter. |
|
684 |
///\code |
|
685 |
/// typedef ListDigraph Digraph; |
|
686 |
/// DIGRAPH_TYPEDEFS(Digraph); |
|
687 |
/// Digraph g; |
|
688 |
/// Node u=g.addNode(); //node of id 0 |
|
689 |
/// Node v=g.addNode(); //node of id 1 |
|
690 |
/// Arc a=g.addArc(u, v); //arc of id 0 |
|
691 |
/// Arc f=g.addArc(v, u); //arc of id 1 |
|
692 |
/// BoolNodeMap nm(g, true); |
|
693 |
/// nm.set(u, false); |
|
694 |
/// BoolArcMap am(g, true); |
|
695 |
/// am.set(a, false); |
|
696 |
/// typedef SubDigraphAdaptor<Digraph, BoolNodeMap, BoolArcMap> SubGA; |
|
697 |
/// SubGA ga(g, nm, am); |
|
698 |
/// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) |
|
699 |
/// std::cout << g.id(n) << std::endl; |
|
700 |
/// std::cout << ":-)" << std::endl; |
|
701 |
/// for (SubGA::ArcIt a(ga); a!=INVALID; ++a) |
|
702 |
/// std::cout << g.id(a) << std::endl; |
|
703 |
///\endcode |
|
704 |
/// The output of the above code is the following. |
|
705 |
///\code |
|
706 |
/// 1 |
|
707 |
/// :-) |
|
708 |
/// 1 |
|
709 |
///\endcode |
|
710 |
/// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to |
|
711 |
/// \c Digraph::Node that is why \c g.id(n) can be applied. |
|
712 |
/// |
|
713 |
/// For other examples see also the documentation of |
|
714 |
/// NodeSubDigraphAdaptor and ArcSubDigraphAdaptor. |
|
715 |
template<typename _Digraph, |
|
716 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
|
717 |
typename _ArcFilterMap = typename _Digraph::template ArcMap<bool>, |
|
718 |
bool checked = true> |
|
719 |
class SubDigraphAdaptor : |
|
720 |
public DigraphAdaptorExtender< |
|
721 |
SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, checked> > { |
|
722 |
public: |
|
723 |
typedef _Digraph Digraph; |
|
724 |
typedef _NodeFilterMap NodeFilterMap; |
|
725 |
typedef _ArcFilterMap ArcFilterMap; |
|
726 |
|
|
727 |
typedef DigraphAdaptorExtender< |
|
728 |
SubDigraphAdaptorBase<Digraph, NodeFilterMap, ArcFilterMap, checked> > |
|
729 |
Parent; |
|
730 |
|
|
731 |
protected: |
|
732 |
SubDigraphAdaptor() { } |
|
733 |
public: |
|
734 |
|
|
735 |
SubDigraphAdaptor(Digraph& digraph, NodeFilterMap& node_filter, |
|
736 |
ArcFilterMap& arc_filter) { |
|
737 |
setDigraph(digraph); |
|
738 |
setNodeFilterMap(node_filter); |
|
739 |
setArcFilterMap(arc_filter); |
|
740 |
} |
|
741 |
|
|
742 |
}; |
|
743 |
|
|
744 |
/// \brief Just gives back a sub digraph adaptor |
|
745 |
/// |
|
746 |
/// Just gives back a sub digraph adaptor |
|
747 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
748 |
SubDigraphAdaptor<const Digraph, NodeFilterMap, ArcFilterMap> |
|
749 |
subDigraphAdaptor(const Digraph& digraph, |
|
750 |
NodeFilterMap& nfm, ArcFilterMap& afm) { |
|
751 |
return SubDigraphAdaptor<const Digraph, NodeFilterMap, ArcFilterMap> |
|
752 |
(digraph, nfm, afm); |
|
753 |
} |
|
754 |
|
|
755 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
756 |
SubDigraphAdaptor<const Digraph, const NodeFilterMap, ArcFilterMap> |
|
757 |
subDigraphAdaptor(const Digraph& digraph, |
|
758 |
NodeFilterMap& nfm, ArcFilterMap& afm) { |
|
759 |
return SubDigraphAdaptor<const Digraph, const NodeFilterMap, ArcFilterMap> |
|
760 |
(digraph, nfm, afm); |
|
761 |
} |
|
762 |
|
|
763 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
764 |
SubDigraphAdaptor<const Digraph, NodeFilterMap, const ArcFilterMap> |
|
765 |
subDigraphAdaptor(const Digraph& digraph, |
|
766 |
NodeFilterMap& nfm, ArcFilterMap& afm) { |
|
767 |
return SubDigraphAdaptor<const Digraph, NodeFilterMap, const ArcFilterMap> |
|
768 |
(digraph, nfm, afm); |
|
769 |
} |
|
770 |
|
|
771 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
772 |
SubDigraphAdaptor<const Digraph, const NodeFilterMap, const ArcFilterMap> |
|
773 |
subDigraphAdaptor(const Digraph& digraph, |
|
774 |
NodeFilterMap& nfm, ArcFilterMap& afm) { |
|
775 |
return SubDigraphAdaptor<const Digraph, const NodeFilterMap, |
|
776 |
const ArcFilterMap>(digraph, nfm, afm); |
|
777 |
} |
|
778 |
|
|
779 |
|
|
780 |
|
|
781 |
///\ingroup graph_adaptors |
|
782 |
/// |
|
783 |
///\brief An adaptor for hiding nodes from a digraph. |
|
784 |
/// |
|
785 |
///An adaptor for hiding nodes from a digraph. This adaptor |
|
786 |
///specializes SubDigraphAdaptor in the way that only the node-set |
|
787 |
///can be filtered. In usual case the checked parameter is true, we |
|
788 |
///get the induced subgraph. But if the checked parameter is false |
|
789 |
///then we can filter only isolated nodes. |
|
790 |
template<typename _Digraph, |
|
791 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
|
792 |
bool checked = true> |
|
793 |
class NodeSubDigraphAdaptor : |
|
794 |
public SubDigraphAdaptor<_Digraph, _NodeFilterMap, |
|
795 |
ConstMap<typename _Digraph::Arc, bool>, checked> { |
|
796 |
public: |
|
797 |
|
|
798 |
typedef _Digraph Digraph; |
|
799 |
typedef _NodeFilterMap NodeFilterMap; |
|
800 |
|
|
801 |
typedef SubDigraphAdaptor<Digraph, NodeFilterMap, |
|
802 |
ConstMap<typename Digraph::Arc, bool>, checked> |
|
803 |
Parent; |
|
804 |
|
|
805 |
protected: |
|
806 |
ConstMap<typename Digraph::Arc, bool> const_true_map; |
|
807 |
|
|
808 |
NodeSubDigraphAdaptor() : const_true_map(true) { |
|
809 |
Parent::setArcFilterMap(const_true_map); |
|
810 |
} |
|
811 |
|
|
812 |
public: |
|
813 |
|
|
814 |
NodeSubDigraphAdaptor(Digraph& _digraph, NodeFilterMap& node_filter) : |
|
815 |
Parent(), const_true_map(true) { |
|
816 |
Parent::setDigraph(_digraph); |
|
817 |
Parent::setNodeFilterMap(node_filter); |
|
818 |
Parent::setArcFilterMap(const_true_map); |
|
819 |
} |
|
820 |
|
|
821 |
}; |
|
822 |
|
|
823 |
|
|
824 |
/// \brief Just gives back a \c NodeSubDigraphAdaptor |
|
825 |
/// |
|
826 |
/// Just gives back a \c NodeSubDigraphAdaptor |
|
827 |
template<typename Digraph, typename NodeFilterMap> |
|
828 |
NodeSubDigraphAdaptor<const Digraph, NodeFilterMap> |
|
829 |
nodeSubDigraphAdaptor(const Digraph& digraph, NodeFilterMap& nfm) { |
|
830 |
return NodeSubDigraphAdaptor<const Digraph, NodeFilterMap>(digraph, nfm); |
|
831 |
} |
|
832 |
|
|
833 |
template<typename Digraph, typename NodeFilterMap> |
|
834 |
NodeSubDigraphAdaptor<const Digraph, const NodeFilterMap> |
|
835 |
nodeSubDigraphAdaptor(const Digraph& digraph, const NodeFilterMap& nfm) { |
|
836 |
return NodeSubDigraphAdaptor<const Digraph, const NodeFilterMap> |
|
837 |
(digraph, nfm); |
|
838 |
} |
|
839 |
|
|
840 |
///\ingroup graph_adaptors |
|
841 |
/// |
|
842 |
///\brief An adaptor for hiding arcs from a digraph. |
|
843 |
/// |
|
844 |
///An adaptor for hiding arcs from a digraph. This adaptor |
|
845 |
///specializes SubDigraphAdaptor in the way that only the arc-set |
|
846 |
///can be filtered. The usefulness of this adaptor is demonstrated |
|
847 |
///in the problem of searching a maximum number of arc-disjoint |
|
848 |
///shortest paths between two nodes \c s and \c t. Shortest here |
|
849 |
///means being shortest w.r.t. non-negative arc-lengths. Note that |
|
850 |
///the comprehension of the presented solution need's some |
|
851 |
///elementary knowlarc from combinatorial optimization. |
|
852 |
/// |
|
853 |
///If a single shortest path is to be searched between \c s and \c |
|
854 |
///t, then this can be done easily by applying the Dijkstra |
|
855 |
///algorithm. What happens, if a maximum number of arc-disjoint |
|
856 |
///shortest paths is to be computed. It can be proved that an arc |
|
857 |
///can be in a shortest path if and only if it is tight with respect |
|
858 |
///to the potential function computed by Dijkstra. Moreover, any |
|
859 |
///path containing only such arcs is a shortest one. Thus we have |
|
860 |
///to compute a maximum number of arc-disjoint paths between \c s |
|
861 |
///and \c t in the digraph which has arc-set all the tight arcs. The |
|
862 |
///computation will be demonstrated on the following digraph, which |
|
863 |
///is read from the dimacs file \c sub_digraph_adaptor_demo.dim. |
|
864 |
///The full source code is available in \ref |
|
865 |
///sub_digraph_adaptor_demo.cc. If you are interested in more demo |
|
866 |
///programs, you can use \ref dim_to_dot.cc to generate .dot files |
|
867 |
///from dimacs files. The .dot file of the following figure was |
|
868 |
///generated by the demo program \ref dim_to_dot.cc. |
|
869 |
/// |
|
870 |
///\dot |
|
871 |
///didigraph lemon_dot_example { |
|
872 |
///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; |
|
873 |
///n0 [ label="0 (s)" ]; |
|
874 |
///n1 [ label="1" ]; |
|
875 |
///n2 [ label="2" ]; |
|
876 |
///n3 [ label="3" ]; |
|
877 |
///n4 [ label="4" ]; |
|
878 |
///n5 [ label="5" ]; |
|
879 |
///n6 [ label="6 (t)" ]; |
|
880 |
///arc [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; |
|
881 |
///n5 -> n6 [ label="9, length:4" ]; |
|
882 |
///n4 -> n6 [ label="8, length:2" ]; |
|
883 |
///n3 -> n5 [ label="7, length:1" ]; |
|
884 |
///n2 -> n5 [ label="6, length:3" ]; |
|
885 |
///n2 -> n6 [ label="5, length:5" ]; |
|
886 |
///n2 -> n4 [ label="4, length:2" ]; |
|
887 |
///n1 -> n4 [ label="3, length:3" ]; |
|
888 |
///n0 -> n3 [ label="2, length:1" ]; |
|
889 |
///n0 -> n2 [ label="1, length:2" ]; |
|
890 |
///n0 -> n1 [ label="0, length:3" ]; |
|
891 |
///} |
|
892 |
///\enddot |
|
893 |
/// |
|
894 |
///\code |
|
895 |
///Digraph g; |
|
896 |
///Node s, t; |
|
897 |
///LengthMap length(g); |
|
898 |
/// |
|
899 |
///readDimacs(std::cin, g, length, s, t); |
|
900 |
/// |
|
901 |
///cout << "arcs with lengths (of form id, source--length->target): " << endl; |
|
902 |
///for(ArcIt e(g); e!=INVALID; ++e) |
|
903 |
/// cout << g.id(e) << ", " << g.id(g.source(e)) << "--" |
|
904 |
/// << length[e] << "->" << g.id(g.target(e)) << endl; |
|
905 |
/// |
|
906 |
///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl; |
|
907 |
///\endcode |
|
908 |
///Next, the potential function is computed with Dijkstra. |
|
909 |
///\code |
|
910 |
///typedef Dijkstra<Digraph, LengthMap> Dijkstra; |
|
911 |
///Dijkstra dijkstra(g, length); |
|
912 |
///dijkstra.run(s); |
|
913 |
///\endcode |
|
914 |
///Next, we consrtruct a map which filters the arc-set to the tight arcs. |
|
915 |
///\code |
|
916 |
///typedef TightArcFilterMap<Digraph, const Dijkstra::DistMap, LengthMap> |
|
917 |
/// TightArcFilter; |
|
918 |
///TightArcFilter tight_arc_filter(g, dijkstra.distMap(), length); |
|
919 |
/// |
|
920 |
///typedef ArcSubDigraphAdaptor<Digraph, TightArcFilter> SubGA; |
|
921 |
///SubGA ga(g, tight_arc_filter); |
|
922 |
///\endcode |
|
923 |
///Then, the maximum nimber of arc-disjoint \c s-\c t paths are computed |
|
924 |
///with a max flow algorithm Preflow. |
|
925 |
///\code |
|
926 |
///ConstMap<Arc, int> const_1_map(1); |
|
927 |
///Digraph::ArcMap<int> flow(g, 0); |
|
928 |
/// |
|
929 |
///Preflow<SubGA, ConstMap<Arc, int>, Digraph::ArcMap<int> > |
|
930 |
/// preflow(ga, const_1_map, s, t); |
|
931 |
///preflow.run(); |
|
932 |
///\endcode |
|
933 |
///Last, the output is: |
|
934 |
///\code |
|
935 |
///cout << "maximum number of arc-disjoint shortest path: " |
|
936 |
/// << preflow.flowValue() << endl; |
|
937 |
///cout << "arcs of the maximum number of arc-disjoint shortest s-t paths: " |
|
938 |
/// << endl; |
|
939 |
///for(ArcIt e(g); e!=INVALID; ++e) |
|
940 |
/// if (preflow.flow(e)) |
|
941 |
/// cout << " " << g.id(g.source(e)) << "--" |
|
942 |
/// << length[e] << "->" << g.id(g.target(e)) << endl; |
|
943 |
///\endcode |
|
944 |
///The program has the following (expected :-)) output: |
|
945 |
///\code |
|
946 |
///arcs with lengths (of form id, source--length->target): |
|
947 |
/// 9, 5--4->6 |
|
948 |
/// 8, 4--2->6 |
|
949 |
/// 7, 3--1->5 |
|
950 |
/// 6, 2--3->5 |
|
951 |
/// 5, 2--5->6 |
|
952 |
/// 4, 2--2->4 |
|
953 |
/// 3, 1--3->4 |
|
954 |
/// 2, 0--1->3 |
|
955 |
/// 1, 0--2->2 |
|
956 |
/// 0, 0--3->1 |
|
957 |
///s: 0 t: 6 |
|
958 |
///maximum number of arc-disjoint shortest path: 2 |
|
959 |
///arcs of the maximum number of arc-disjoint shortest s-t paths: |
|
960 |
/// 9, 5--4->6 |
|
961 |
/// 8, 4--2->6 |
|
962 |
/// 7, 3--1->5 |
|
963 |
/// 4, 2--2->4 |
|
964 |
/// 2, 0--1->3 |
|
965 |
/// 1, 0--2->2 |
|
966 |
///\endcode |
|
967 |
template<typename _Digraph, typename _ArcFilterMap> |
|
968 |
class ArcSubDigraphAdaptor : |
|
969 |
public SubDigraphAdaptor<_Digraph, ConstMap<typename _Digraph::Node, bool>, |
|
970 |
_ArcFilterMap, false> { |
|
971 |
public: |
|
972 |
typedef _Digraph Digraph; |
|
973 |
typedef _ArcFilterMap ArcFilterMap; |
|
974 |
|
|
975 |
typedef SubDigraphAdaptor<Digraph, ConstMap<typename Digraph::Node, bool>, |
|
976 |
ArcFilterMap, false> Parent; |
|
977 |
protected: |
|
978 |
ConstMap<typename Digraph::Node, bool> const_true_map; |
|
979 |
|
|
980 |
ArcSubDigraphAdaptor() : const_true_map(true) { |
|
981 |
Parent::setNodeFilterMap(const_true_map); |
|
982 |
} |
|
983 |
|
|
984 |
public: |
|
985 |
|
|
986 |
ArcSubDigraphAdaptor(Digraph& digraph, ArcFilterMap& arc_filter) |
|
987 |
: Parent(), const_true_map(true) { |
|
988 |
Parent::setDigraph(digraph); |
|
989 |
Parent::setNodeFilterMap(const_true_map); |
|
990 |
Parent::setArcFilterMap(arc_filter); |
|
991 |
} |
|
992 |
|
|
993 |
}; |
|
994 |
|
|
995 |
/// \brief Just gives back an arc sub digraph adaptor |
|
996 |
/// |
|
997 |
/// Just gives back an arc sub digraph adaptor |
|
998 |
template<typename Digraph, typename ArcFilterMap> |
|
999 |
ArcSubDigraphAdaptor<const Digraph, ArcFilterMap> |
|
1000 |
arcSubDigraphAdaptor(const Digraph& digraph, ArcFilterMap& afm) { |
|
1001 |
return ArcSubDigraphAdaptor<const Digraph, ArcFilterMap>(digraph, afm); |
|
1002 |
} |
|
1003 |
|
|
1004 |
template<typename Digraph, typename ArcFilterMap> |
|
1005 |
ArcSubDigraphAdaptor<const Digraph, const ArcFilterMap> |
|
1006 |
arcSubDigraphAdaptor(const Digraph& digraph, const ArcFilterMap& afm) { |
|
1007 |
return ArcSubDigraphAdaptor<const Digraph, const ArcFilterMap> |
|
1008 |
(digraph, afm); |
|
1009 |
} |
|
1010 |
|
|
1011 |
template <typename _Digraph> |
|
1012 |
class UndirDigraphAdaptorBase { |
|
1013 |
public: |
|
1014 |
typedef _Digraph Digraph; |
|
1015 |
typedef UndirDigraphAdaptorBase Adaptor; |
|
1016 |
|
|
1017 |
typedef True UndirectedTag; |
|
1018 |
|
|
1019 |
typedef typename Digraph::Arc Edge; |
|
1020 |
typedef typename Digraph::Node Node; |
|
1021 |
|
|
1022 |
class Arc : public Edge { |
|
1023 |
friend class UndirDigraphAdaptorBase; |
|
1024 |
protected: |
|
1025 |
bool _forward; |
|
1026 |
|
|
1027 |
Arc(const Edge& edge, bool forward) : |
|
1028 |
Edge(edge), _forward(forward) {} |
|
1029 |
|
|
1030 |
public: |
|
1031 |
Arc() {} |
|
1032 |
|
|
1033 |
Arc(Invalid) : Edge(INVALID), _forward(true) {} |
|
1034 |
|
|
1035 |
bool operator==(const Arc &other) const { |
|
1036 |
return _forward == other._forward && |
|
1037 |
static_cast<const Edge&>(*this) == static_cast<const Edge&>(other); |
|
1038 |
} |
|
1039 |
bool operator!=(const Arc &other) const { |
|
1040 |
return _forward != other._forward || |
|
1041 |
static_cast<const Edge&>(*this) != static_cast<const Edge&>(other); |
|
1042 |
} |
|
1043 |
bool operator<(const Arc &other) const { |
|
1044 |
return _forward < other._forward || |
|
1045 |
(_forward == other._forward && |
|
1046 |
static_cast<const Edge&>(*this) < static_cast<const Edge&>(other)); |
|
1047 |
} |
|
1048 |
}; |
|
1049 |
|
|
1050 |
|
|
1051 |
|
|
1052 |
void first(Node& n) const { |
|
1053 |
_digraph->first(n); |
|
1054 |
} |
|
1055 |
|
|
1056 |
void next(Node& n) const { |
|
1057 |
_digraph->next(n); |
|
1058 |
} |
|
1059 |
|
|
1060 |
void first(Arc& a) const { |
|
1061 |
_digraph->first(a); |
|
1062 |
a._forward = true; |
|
1063 |
} |
|
1064 |
|
|
1065 |
void next(Arc& a) const { |
|
1066 |
if (a._forward) { |
|
1067 |
a._forward = false; |
|
1068 |
} else { |
|
1069 |
_digraph->next(a); |
|
1070 |
a._forward = true; |
|
1071 |
} |
|
1072 |
} |
|
1073 |
|
|
1074 |
void first(Edge& e) const { |
|
1075 |
_digraph->first(e); |
|
1076 |
} |
|
1077 |
|
|
1078 |
void next(Edge& e) const { |
|
1079 |
_digraph->next(e); |
|
1080 |
} |
|
1081 |
|
|
1082 |
void firstOut(Arc& a, const Node& n) const { |
|
1083 |
_digraph->firstIn(a, n); |
|
1084 |
if( static_cast<const Edge&>(a) != INVALID ) { |
|
1085 |
a._forward = false; |
|
1086 |
} else { |
|
1087 |
_digraph->firstOut(a, n); |
|
1088 |
a._forward = true; |
|
1089 |
} |
|
1090 |
} |
|
1091 |
void nextOut(Arc &a) const { |
|
1092 |
if (!a._forward) { |
|
1093 |
Node n = _digraph->target(a); |
|
1094 |
_digraph->nextIn(a); |
|
1095 |
if (static_cast<const Edge&>(a) == INVALID ) { |
|
1096 |
_digraph->firstOut(a, n); |
|
1097 |
a._forward = true; |
|
1098 |
} |
|
1099 |
} |
|
1100 |
else { |
|
1101 |
_digraph->nextOut(a); |
|
1102 |
} |
|
1103 |
} |
|
1104 |
|
|
1105 |
void firstIn(Arc &a, const Node &n) const { |
|
1106 |
_digraph->firstOut(a, n); |
|
1107 |
if (static_cast<const Edge&>(a) != INVALID ) { |
|
1108 |
a._forward = false; |
|
1109 |
} else { |
|
1110 |
_digraph->firstIn(a, n); |
|
1111 |
a._forward = true; |
|
1112 |
} |
|
1113 |
} |
|
1114 |
void nextIn(Arc &a) const { |
|
1115 |
if (!a._forward) { |
|
1116 |
Node n = _digraph->source(a); |
|
1117 |
_digraph->nextOut(a); |
|
1118 |
if( static_cast<const Edge&>(a) == INVALID ) { |
|
1119 |
_digraph->firstIn(a, n); |
|
1120 |
a._forward = true; |
|
1121 |
} |
|
1122 |
} |
|
1123 |
else { |
|
1124 |
_digraph->nextIn(a); |
|
1125 |
} |
|
1126 |
} |
|
1127 |
|
|
1128 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
|
1129 |
d = true; |
|
1130 |
_digraph->firstOut(e, n); |
|
1131 |
if (e != INVALID) return; |
|
1132 |
d = false; |
|
1133 |
_digraph->firstIn(e, n); |
|
1134 |
} |
|
1135 |
|
|
1136 |
void nextInc(Edge &e, bool &d) const { |
|
1137 |
if (d) { |
|
1138 |
Node s = _digraph->source(e); |
|
1139 |
_digraph->nextOut(e); |
|
1140 |
if (e != INVALID) return; |
|
1141 |
d = false; |
|
1142 |
_digraph->firstIn(e, s); |
|
1143 |
} else { |
|
1144 |
_digraph->nextIn(e); |
|
1145 |
} |
|
1146 |
} |
|
1147 |
|
|
1148 |
Node u(const Edge& e) const { |
|
1149 |
return _digraph->source(e); |
|
1150 |
} |
|
1151 |
|
|
1152 |
Node v(const Edge& e) const { |
|
1153 |
return _digraph->target(e); |
|
1154 |
} |
|
1155 |
|
|
1156 |
Node source(const Arc &a) const { |
|
1157 |
return a._forward ? _digraph->source(a) : _digraph->target(a); |
|
1158 |
} |
|
1159 |
|
|
1160 |
Node target(const Arc &a) const { |
|
1161 |
return a._forward ? _digraph->target(a) : _digraph->source(a); |
|
1162 |
} |
|
1163 |
|
|
1164 |
static Arc direct(const Edge &e, bool d) { |
|
1165 |
return Arc(e, d); |
|
1166 |
} |
|
1167 |
Arc direct(const Edge &e, const Node& n) const { |
|
1168 |
return Arc(e, _digraph->source(e) == n); |
|
1169 |
} |
|
1170 |
|
|
1171 |
static bool direction(const Arc &a) { return a._forward; } |
|
1172 |
|
|
1173 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
|
1174 |
Arc arcFromId(int ix) const { |
|
1175 |
return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); |
|
1176 |
} |
|
1177 |
Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } |
|
1178 |
|
|
1179 |
int id(const Node &n) const { return _digraph->id(n); } |
|
1180 |
int id(const Arc &a) const { |
|
1181 |
return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); |
|
1182 |
} |
|
1183 |
int id(const Edge &e) const { return _digraph->id(e); } |
|
1184 |
|
|
1185 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
|
1186 |
int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } |
|
1187 |
int maxEdgeId() const { return _digraph->maxArcId(); } |
|
1188 |
|
|
1189 |
Node addNode() { return _digraph->addNode(); } |
|
1190 |
Edge addEdge(const Node& u, const Node& v) { |
|
1191 |
return _digraph->addArc(u, v); |
|
1192 |
} |
|
1193 |
|
|
1194 |
void erase(const Node& i) { _digraph->erase(i); } |
|
1195 |
void erase(const Edge& i) { _digraph->erase(i); } |
|
1196 |
|
|
1197 |
void clear() { _digraph->clear(); } |
|
1198 |
|
|
1199 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
|
1200 |
int nodeNum() const { return 2 * _digraph->arcNum(); } |
|
1201 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
|
1202 |
int arcNum() const { return 2 * _digraph->arcNum(); } |
|
1203 |
int edgeNum() const { return _digraph->arcNum(); } |
|
1204 |
|
|
1205 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
1206 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
|
1207 |
if (p == INVALID) { |
|
1208 |
Edge arc = _digraph->findArc(s, t); |
|
1209 |
if (arc != INVALID) return direct(arc, true); |
|
1210 |
arc = _digraph->findArc(t, s); |
|
1211 |
if (arc != INVALID) return direct(arc, false); |
|
1212 |
} else if (direction(p)) { |
|
1213 |
Edge arc = _digraph->findArc(s, t, p); |
|
1214 |
if (arc != INVALID) return direct(arc, true); |
|
1215 |
arc = _digraph->findArc(t, s); |
|
1216 |
if (arc != INVALID) return direct(arc, false); |
|
1217 |
} else { |
|
1218 |
Edge arc = _digraph->findArc(t, s, p); |
|
1219 |
if (arc != INVALID) return direct(arc, false); |
|
1220 |
} |
|
1221 |
return INVALID; |
|
1222 |
} |
|
1223 |
|
|
1224 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
|
1225 |
if (s != t) { |
|
1226 |
if (p == INVALID) { |
|
1227 |
Edge arc = _digraph->findArc(s, t); |
|
1228 |
if (arc != INVALID) return arc; |
|
1229 |
arc = _digraph->findArc(t, s); |
|
1230 |
if (arc != INVALID) return arc; |
|
1231 |
} else if (_digraph->s(p) == s) { |
|
1232 |
Edge arc = _digraph->findArc(s, t, p); |
|
1233 |
if (arc != INVALID) return arc; |
|
1234 |
arc = _digraph->findArc(t, s); |
|
1235 |
if (arc != INVALID) return arc; |
|
1236 |
} else { |
|
1237 |
Edge arc = _digraph->findArc(t, s, p); |
|
1238 |
if (arc != INVALID) return arc; |
|
1239 |
} |
|
1240 |
} else { |
|
1241 |
return _digraph->findArc(s, t, p); |
|
1242 |
} |
|
1243 |
return INVALID; |
|
1244 |
} |
|
1245 |
|
|
1246 |
private: |
|
1247 |
|
|
1248 |
template <typename _Value> |
|
1249 |
class ArcMapBase { |
|
1250 |
private: |
|
1251 |
|
|
1252 |
typedef typename Digraph::template ArcMap<_Value> MapImpl; |
|
1253 |
|
|
1254 |
public: |
|
1255 |
|
|
1256 |
typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
|
1257 |
|
|
1258 |
typedef _Value Value; |
|
1259 |
typedef Arc Key; |
|
1260 |
|
|
1261 |
ArcMapBase(const Adaptor& adaptor) : |
|
1262 |
_forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
|
1263 |
|
|
1264 |
ArcMapBase(const Adaptor& adaptor, const Value& v) |
|
1265 |
: _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {} |
|
1266 |
|
|
1267 |
void set(const Arc& a, const Value& v) { |
|
1268 |
if (direction(a)) { |
|
1269 |
_forward.set(a, v); |
|
1270 |
} else { |
|
1271 |
_backward.set(a, v); |
|
1272 |
} |
|
1273 |
} |
|
1274 |
|
|
1275 |
typename MapTraits<MapImpl>::ConstReturnValue |
|
1276 |
operator[](const Arc& a) const { |
|
1277 |
if (direction(a)) { |
|
1278 |
return _forward[a]; |
|
1279 |
} else { |
|
1280 |
return _backward[a]; |
|
1281 |
} |
|
1282 |
} |
|
1283 |
|
|
1284 |
typename MapTraits<MapImpl>::ReturnValue |
|
1285 |
operator[](const Arc& a) { |
|
1286 |
if (direction(a)) { |
|
1287 |
return _forward[a]; |
|
1288 |
} else { |
|
1289 |
return _backward[a]; |
|
1290 |
} |
|
1291 |
} |
|
1292 |
|
|
1293 |
protected: |
|
1294 |
|
|
1295 |
MapImpl _forward, _backward; |
|
1296 |
|
|
1297 |
}; |
|
1298 |
|
|
1299 |
public: |
|
1300 |
|
|
1301 |
template <typename _Value> |
|
1302 |
class NodeMap : public Digraph::template NodeMap<_Value> { |
|
1303 |
public: |
|
1304 |
|
|
1305 |
typedef _Value Value; |
|
1306 |
typedef typename Digraph::template NodeMap<Value> Parent; |
|
1307 |
|
|
1308 |
explicit NodeMap(const Adaptor& adaptor) |
|
1309 |
: Parent(*adaptor._digraph) {} |
|
1310 |
|
|
1311 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
|
1312 |
: Parent(*adaptor._digraph, value) { } |
|
1313 |
|
|
1314 |
private: |
|
1315 |
NodeMap& operator=(const NodeMap& cmap) { |
|
1316 |
return operator=<NodeMap>(cmap); |
|
1317 |
} |
|
1318 |
|
|
1319 |
template <typename CMap> |
|
1320 |
NodeMap& operator=(const CMap& cmap) { |
|
1321 |
Parent::operator=(cmap); |
|
1322 |
return *this; |
|
1323 |
} |
|
1324 |
|
|
1325 |
}; |
|
1326 |
|
|
1327 |
template <typename _Value> |
|
1328 |
class ArcMap |
|
1329 |
: public SubMapExtender<Adaptor, ArcMapBase<_Value> > |
|
1330 |
{ |
|
1331 |
public: |
|
1332 |
typedef _Value Value; |
|
1333 |
typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent; |
|
1334 |
|
|
1335 |
ArcMap(const Adaptor& adaptor) |
|
1336 |
: Parent(adaptor) {} |
|
1337 |
|
|
1338 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
1339 |
: Parent(adaptor, value) {} |
|
1340 |
|
|
1341 |
private: |
|
1342 |
ArcMap& operator=(const ArcMap& cmap) { |
|
1343 |
return operator=<ArcMap>(cmap); |
|
1344 |
} |
|
1345 |
|
|
1346 |
template <typename CMap> |
|
1347 |
ArcMap& operator=(const CMap& cmap) { |
|
1348 |
Parent::operator=(cmap); |
|
1349 |
return *this; |
|
1350 |
} |
|
1351 |
}; |
|
1352 |
|
|
1353 |
template <typename _Value> |
|
1354 |
class EdgeMap : public Digraph::template ArcMap<_Value> { |
|
1355 |
public: |
|
1356 |
|
|
1357 |
typedef _Value Value; |
|
1358 |
typedef typename Digraph::template ArcMap<Value> Parent; |
|
1359 |
|
|
1360 |
explicit EdgeMap(const Adaptor& adaptor) |
|
1361 |
: Parent(*adaptor._digraph) {} |
|
1362 |
|
|
1363 |
EdgeMap(const Adaptor& adaptor, const Value& value) |
|
1364 |
: Parent(*adaptor._digraph, value) {} |
|
1365 |
|
|
1366 |
private: |
|
1367 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
1368 |
return operator=<EdgeMap>(cmap); |
|
1369 |
} |
|
1370 |
|
|
1371 |
template <typename CMap> |
|
1372 |
EdgeMap& operator=(const CMap& cmap) { |
|
1373 |
Parent::operator=(cmap); |
|
1374 |
return *this; |
|
1375 |
} |
|
1376 |
|
|
1377 |
}; |
|
1378 |
|
|
1379 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
|
1380 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
|
1381 |
|
|
1382 |
protected: |
|
1383 |
|
|
1384 |
UndirDigraphAdaptorBase() : _digraph(0) {} |
|
1385 |
|
|
1386 |
Digraph* _digraph; |
|
1387 |
|
|
1388 |
void setDigraph(Digraph& digraph) { |
|
1389 |
_digraph = &digraph; |
|
1390 |
} |
|
1391 |
|
|
1392 |
}; |
|
1393 |
|
|
1394 |
///\ingroup graph_adaptors |
|
1395 |
/// |
|
1396 |
/// \brief An graph is made from a directed digraph by an adaptor |
|
1397 |
/// |
|
1398 |
/// This adaptor makes an undirected graph from a directed |
|
1399 |
/// digraph. All arc of the underlying will be showed in the adaptor |
|
1400 |
/// as an edge. Let's see an informal example about using |
|
1401 |
/// this adaptor: |
|
1402 |
/// |
|
1403 |
/// There is a network of the streets of a town. Of course there are |
|
1404 |
/// some one-way street in the town hence the network is a directed |
|
1405 |
/// one. There is a crazy driver who go oppositely in the one-way |
|
1406 |
/// street without moral sense. Of course he can pass this streets |
|
1407 |
/// slower than the regular way, in fact his speed is half of the |
|
1408 |
/// normal speed. How long should he drive to get from a source |
|
1409 |
/// point to the target? Let see the example code which calculate it: |
|
1410 |
/// |
|
1411 |
/// \todo BadCode, SimpleMap does no exists |
|
1412 |
///\code |
|
1413 |
/// typedef UndirDigraphAdaptor<Digraph> Graph; |
|
1414 |
/// Graph graph(digraph); |
|
1415 |
/// |
|
1416 |
/// typedef SimpleMap<LengthMap> FLengthMap; |
|
1417 |
/// FLengthMap flength(length); |
|
1418 |
/// |
|
1419 |
/// typedef ScaleMap<LengthMap> RLengthMap; |
|
1420 |
/// RLengthMap rlength(length, 2.0); |
|
1421 |
/// |
|
1422 |
/// typedef Graph::CombinedArcMap<FLengthMap, RLengthMap > ULengthMap; |
|
1423 |
/// ULengthMap ulength(flength, rlength); |
|
1424 |
/// |
|
1425 |
/// Dijkstra<Graph, ULengthMap> dijkstra(graph, ulength); |
|
1426 |
/// std::cout << "Driving time : " << dijkstra.run(src, trg) << std::endl; |
|
1427 |
///\endcode |
|
1428 |
/// |
|
1429 |
/// The combined arc map makes the length map for the undirected |
|
1430 |
/// graph. It is created from a forward and reverse map. The forward |
|
1431 |
/// map is created from the original length map with a SimpleMap |
|
1432 |
/// adaptor which just makes a read-write map from the reference map |
|
1433 |
/// i.e. it forgets that it can be return reference to values. The |
|
1434 |
/// reverse map is just the scaled original map with the ScaleMap |
|
1435 |
/// adaptor. The combination solves that passing the reverse way |
|
1436 |
/// takes double time than the original. To get the driving time we |
|
1437 |
/// run the dijkstra algorithm on the graph. |
|
1438 |
template<typename _Digraph> |
|
1439 |
class UndirDigraphAdaptor |
|
1440 |
: public GraphAdaptorExtender<UndirDigraphAdaptorBase<_Digraph> > { |
|
1441 |
public: |
|
1442 |
typedef _Digraph Digraph; |
|
1443 |
typedef GraphAdaptorExtender<UndirDigraphAdaptorBase<Digraph> > Parent; |
|
1444 |
protected: |
|
1445 |
UndirDigraphAdaptor() { } |
|
1446 |
public: |
|
1447 |
|
|
1448 |
/// \brief Constructor |
|
1449 |
/// |
|
1450 |
/// Constructor |
|
1451 |
UndirDigraphAdaptor(_Digraph& _digraph) { |
|
1452 |
setDigraph(_digraph); |
|
1453 |
} |
|
1454 |
|
|
1455 |
/// \brief ArcMap combined from two original ArcMap |
|
1456 |
/// |
|
1457 |
/// This class adapts two original digraph ArcMap to |
|
1458 |
/// get an arc map on the adaptor. |
|
1459 |
template <typename _ForwardMap, typename _BackwardMap> |
|
1460 |
class CombinedArcMap { |
|
1461 |
public: |
|
1462 |
|
|
1463 |
typedef _ForwardMap ForwardMap; |
|
1464 |
typedef _BackwardMap BackwardMap; |
|
1465 |
|
|
1466 |
typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag; |
|
1467 |
|
|
1468 |
typedef typename ForwardMap::Value Value; |
|
1469 |
typedef typename Parent::Arc Key; |
|
1470 |
|
|
1471 |
/// \brief Constructor |
|
1472 |
/// |
|
1473 |
/// Constructor |
|
1474 |
CombinedArcMap() : _forward(0), _backward(0) {} |
|
1475 |
|
|
1476 |
/// \brief Constructor |
|
1477 |
/// |
|
1478 |
/// Constructor |
|
1479 |
CombinedArcMap(ForwardMap& forward, BackwardMap& backward) |
|
1480 |
: _forward(&forward), _backward(&backward) {} |
|
1481 |
|
|
1482 |
|
|
1483 |
/// \brief Sets the value associated with a key. |
|
1484 |
/// |
|
1485 |
/// Sets the value associated with a key. |
|
1486 |
void set(const Key& e, const Value& a) { |
|
1487 |
if (Parent::direction(e)) { |
|
1488 |
_forward->set(e, a); |
|
1489 |
} else { |
|
1490 |
_backward->set(e, a); |
|
1491 |
} |
|
1492 |
} |
|
1493 |
|
|
1494 |
/// \brief Returns the value associated with a key. |
|
1495 |
/// |
|
1496 |
/// Returns the value associated with a key. |
|
1497 |
typename MapTraits<ForwardMap>::ConstReturnValue |
|
1498 |
operator[](const Key& e) const { |
|
1499 |
if (Parent::direction(e)) { |
|
1500 |
return (*_forward)[e]; |
|
1501 |
} else { |
|
1502 |
return (*_backward)[e]; |
|
1503 |
} |
|
1504 |
} |
|
1505 |
|
|
1506 |
/// \brief Returns the value associated with a key. |
|
1507 |
/// |
|
1508 |
/// Returns the value associated with a key. |
|
1509 |
typename MapTraits<ForwardMap>::ReturnValue |
|
1510 |
operator[](const Key& e) { |
|
1511 |
if (Parent::direction(e)) { |
|
1512 |
return (*_forward)[e]; |
|
1513 |
} else { |
|
1514 |
return (*_backward)[e]; |
|
1515 |
} |
|
1516 |
} |
|
1517 |
|
|
1518 |
/// \brief Sets the forward map |
|
1519 |
/// |
|
1520 |
/// Sets the forward map |
|
1521 |
void setForwardMap(ForwardMap& forward) { |
|
1522 |
_forward = &forward; |
|
1523 |
} |
|
1524 |
|
|
1525 |
/// \brief Sets the backward map |
|
1526 |
/// |
|
1527 |
/// Sets the backward map |
|
1528 |
void setBackwardMap(BackwardMap& backward) { |
|
1529 |
_backward = &backward; |
|
1530 |
} |
|
1531 |
|
|
1532 |
protected: |
|
1533 |
|
|
1534 |
ForwardMap* _forward; |
|
1535 |
BackwardMap* _backward; |
|
1536 |
|
|
1537 |
}; |
|
1538 |
|
|
1539 |
}; |
|
1540 |
|
|
1541 |
/// \brief Just gives back an undir digraph adaptor |
|
1542 |
/// |
|
1543 |
/// Just gives back an undir digraph adaptor |
|
1544 |
template<typename Digraph> |
|
1545 |
UndirDigraphAdaptor<const Digraph> |
|
1546 |
undirDigraphAdaptor(const Digraph& digraph) { |
|
1547 |
return UndirDigraphAdaptor<const Digraph>(digraph); |
|
1548 |
} |
|
1549 |
|
|
1550 |
template<typename _Digraph, |
|
1551 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
1552 |
typename _FlowMap = _CapacityMap, |
|
1553 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
|
1554 |
class ResForwardFilter { |
|
1555 |
public: |
|
1556 |
|
|
1557 |
typedef _Digraph Digraph; |
|
1558 |
typedef _CapacityMap CapacityMap; |
|
1559 |
typedef _FlowMap FlowMap; |
|
1560 |
typedef _Tolerance Tolerance; |
|
1561 |
|
|
1562 |
typedef typename Digraph::Arc Key; |
|
1563 |
typedef bool Value; |
|
1564 |
|
|
1565 |
private: |
|
1566 |
|
|
1567 |
const CapacityMap* _capacity; |
|
1568 |
const FlowMap* _flow; |
|
1569 |
Tolerance _tolerance; |
|
1570 |
public: |
|
1571 |
|
|
1572 |
ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow, |
|
1573 |
const Tolerance& tolerance = Tolerance()) |
|
1574 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
|
1575 |
|
|
1576 |
ResForwardFilter(const Tolerance& tolerance = Tolerance()) |
|
1577 |
: _capacity(0), _flow(0), _tolerance(tolerance) { } |
|
1578 |
|
|
1579 |
void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } |
|
1580 |
void setFlow(const FlowMap& flow) { _flow = &flow; } |
|
1581 |
|
|
1582 |
bool operator[](const typename Digraph::Arc& a) const { |
|
1583 |
return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); |
|
1584 |
} |
|
1585 |
}; |
|
1586 |
|
|
1587 |
template<typename _Digraph, |
|
1588 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
1589 |
typename _FlowMap = _CapacityMap, |
|
1590 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
|
1591 |
class ResBackwardFilter { |
|
1592 |
public: |
|
1593 |
|
|
1594 |
typedef _Digraph Digraph; |
|
1595 |
typedef _CapacityMap CapacityMap; |
|
1596 |
typedef _FlowMap FlowMap; |
|
1597 |
typedef _Tolerance Tolerance; |
|
1598 |
|
|
1599 |
typedef typename Digraph::Arc Key; |
|
1600 |
typedef bool Value; |
|
1601 |
|
|
1602 |
private: |
|
1603 |
|
|
1604 |
const CapacityMap* _capacity; |
|
1605 |
const FlowMap* _flow; |
|
1606 |
Tolerance _tolerance; |
|
1607 |
|
|
1608 |
public: |
|
1609 |
|
|
1610 |
ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow, |
|
1611 |
const Tolerance& tolerance = Tolerance()) |
|
1612 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
|
1613 |
ResBackwardFilter(const Tolerance& tolerance = Tolerance()) |
|
1614 |
: _capacity(0), _flow(0), _tolerance(tolerance) { } |
|
1615 |
|
|
1616 |
void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } |
|
1617 |
void setFlow(const FlowMap& flow) { _flow = &flow; } |
|
1618 |
|
|
1619 |
bool operator[](const typename Digraph::Arc& a) const { |
|
1620 |
return _tolerance.positive((*_flow)[a]); |
|
1621 |
} |
|
1622 |
}; |
|
1623 |
|
|
1624 |
|
|
1625 |
///\ingroup graph_adaptors |
|
1626 |
/// |
|
1627 |
///\brief An adaptor for composing the residual graph for directed |
|
1628 |
///flow and circulation problems. |
|
1629 |
/// |
|
1630 |
///An adaptor for composing the residual graph for directed flow and |
|
1631 |
///circulation problems. Let \f$ G=(V, A) \f$ be a directed digraph |
|
1632 |
///and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F |
|
1633 |
///\f$, be functions on the arc-set. |
|
1634 |
/// |
|
1635 |
///In the appications of ResDigraphAdaptor, \f$ f \f$ usually stands |
|
1636 |
///for a flow and \f$ c \f$ for a capacity function. Suppose that a |
|
1637 |
///graph instance \c g of type \c ListDigraph implements \f$ G \f$. |
|
1638 |
/// |
|
1639 |
///\code |
|
1640 |
/// ListDigraph g; |
|
1641 |
///\endcode |
|
1642 |
/// |
|
1643 |
///Then ResDigraphAdaptor implements the digraph structure with |
|
1644 |
/// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} |
|
1645 |
/// \f$, where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and |
|
1646 |
/// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so |
|
1647 |
/// called residual graph. When we take the union \f$ |
|
1648 |
/// A_{forward}\cup A_{backward} \f$, multilicities are counted, |
|
1649 |
/// i.e. if an arc is in both \f$ A_{forward} \f$ and \f$ |
|
1650 |
/// A_{backward} \f$, then in the adaptor it appears twice. The |
|
1651 |
/// following code shows how such an instance can be constructed. |
|
1652 |
/// |
|
1653 |
///\code |
|
1654 |
/// typedef ListDigraph Digraph; |
|
1655 |
/// IntArcMap f(g), c(g); |
|
1656 |
/// ResDigraphAdaptor<Digraph, int, IntArcMap, IntArcMap> ga(g); |
|
1657 |
///\endcode |
|
1658 |
template<typename _Digraph, |
|
1659 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
1660 |
typename _FlowMap = _CapacityMap, |
|
1661 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
|
1662 |
class ResDigraphAdaptor : |
|
1663 |
public ArcSubDigraphAdaptor< |
|
1664 |
UndirDigraphAdaptor<const _Digraph>, |
|
1665 |
typename UndirDigraphAdaptor<const _Digraph>::template CombinedArcMap< |
|
1666 |
ResForwardFilter<const _Digraph, _CapacityMap, _FlowMap>, |
|
1667 |
ResBackwardFilter<const _Digraph, _CapacityMap, _FlowMap> > > { |
|
1668 |
public: |
|
1669 |
|
|
1670 |
typedef _Digraph Digraph; |
|
1671 |
typedef _CapacityMap CapacityMap; |
|
1672 |
typedef _FlowMap FlowMap; |
|
1673 |
typedef _Tolerance Tolerance; |
|
1674 |
|
|
1675 |
typedef typename CapacityMap::Value Value; |
|
1676 |
typedef ResDigraphAdaptor Adaptor; |
|
1677 |
|
|
1678 |
protected: |
|
1679 |
|
|
1680 |
typedef UndirDigraphAdaptor<const Digraph> UndirDigraph; |
|
1681 |
|
|
1682 |
typedef ResForwardFilter<const Digraph, CapacityMap, FlowMap> |
|
1683 |
ForwardFilter; |
|
1684 |
|
|
1685 |
typedef ResBackwardFilter<const Digraph, CapacityMap, FlowMap> |
|
1686 |
BackwardFilter; |
|
1687 |
|
|
1688 |
typedef typename UndirDigraph:: |
|
1689 |
template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
|
1690 |
|
|
1691 |
typedef ArcSubDigraphAdaptor<UndirDigraph, ArcFilter> Parent; |
|
1692 |
|
|
1693 |
const CapacityMap* _capacity; |
|
1694 |
FlowMap* _flow; |
|
1695 |
|
|
1696 |
UndirDigraph _graph; |
|
1697 |
ForwardFilter _forward_filter; |
|
1698 |
BackwardFilter _backward_filter; |
|
1699 |
ArcFilter _arc_filter; |
|
1700 |
|
|
1701 |
void setCapacityMap(const CapacityMap& capacity) { |
|
1702 |
_capacity = &capacity; |
|
1703 |
_forward_filter.setCapacity(capacity); |
|
1704 |
_backward_filter.setCapacity(capacity); |
|
1705 |
} |
|
1706 |
|
|
1707 |
void setFlowMap(FlowMap& flow) { |
|
1708 |
_flow = &flow; |
|
1709 |
_forward_filter.setFlow(flow); |
|
1710 |
_backward_filter.setFlow(flow); |
|
1711 |
} |
|
1712 |
|
|
1713 |
public: |
|
1714 |
|
|
1715 |
/// \brief Constructor of the residual digraph. |
|
1716 |
/// |
|
1717 |
/// Constructor of the residual graph. The parameters are the digraph type, |
|
1718 |
/// the flow map, the capacity map and a tolerance object. |
|
1719 |
ResDigraphAdaptor(const Digraph& digraph, const CapacityMap& capacity, |
|
1720 |
FlowMap& flow, const Tolerance& tolerance = Tolerance()) |
|
1721 |
: Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph), |
|
1722 |
_forward_filter(capacity, flow, tolerance), |
|
1723 |
_backward_filter(capacity, flow, tolerance), |
|
1724 |
_arc_filter(_forward_filter, _backward_filter) |
|
1725 |
{ |
|
1726 |
Parent::setDigraph(_graph); |
|
1727 |
Parent::setArcFilterMap(_arc_filter); |
|
1728 |
} |
|
1729 |
|
|
1730 |
typedef typename Parent::Arc Arc; |
|
1731 |
|
|
1732 |
/// \brief Gives back the residual capacity of the arc. |
|
1733 |
/// |
|
1734 |
/// Gives back the residual capacity of the arc. |
|
1735 |
Value rescap(const Arc& arc) const { |
|
1736 |
if (UndirDigraph::direction(arc)) { |
|
1737 |
return (*_capacity)[arc] - (*_flow)[arc]; |
|
1738 |
} else { |
|
1739 |
return (*_flow)[arc]; |
|
1740 |
} |
|
1741 |
} |
|
1742 |
|
|
1743 |
/// \brief Augment on the given arc in the residual digraph. |
|
1744 |
/// |
|
1745 |
/// Augment on the given arc in the residual digraph. It increase |
|
1746 |
/// or decrease the flow on the original arc depend on the direction |
|
1747 |
/// of the residual arc. |
|
1748 |
void augment(const Arc& e, const Value& a) const { |
|
1749 |
if (UndirDigraph::direction(e)) { |
|
1750 |
_flow->set(e, (*_flow)[e] + a); |
|
1751 |
} else { |
|
1752 |
_flow->set(e, (*_flow)[e] - a); |
|
1753 |
} |
|
1754 |
} |
|
1755 |
|
|
1756 |
/// \brief Returns the direction of the arc. |
|
1757 |
/// |
|
1758 |
/// Returns true when the arc is same oriented as the original arc. |
|
1759 |
static bool forward(const Arc& e) { |
|
1760 |
return UndirDigraph::direction(e); |
|
1761 |
} |
|
1762 |
|
|
1763 |
/// \brief Returns the direction of the arc. |
|
1764 |
/// |
|
1765 |
/// Returns true when the arc is opposite oriented as the original arc. |
|
1766 |
static bool backward(const Arc& e) { |
|
1767 |
return !UndirDigraph::direction(e); |
|
1768 |
} |
|
1769 |
|
|
1770 |
/// \brief Gives back the forward oriented residual arc. |
|
1771 |
/// |
|
1772 |
/// Gives back the forward oriented residual arc. |
|
1773 |
static Arc forward(const typename Digraph::Arc& e) { |
|
1774 |
return UndirDigraph::direct(e, true); |
|
1775 |
} |
|
1776 |
|
|
1777 |
/// \brief Gives back the backward oriented residual arc. |
|
1778 |
/// |
|
1779 |
/// Gives back the backward oriented residual arc. |
|
1780 |
static Arc backward(const typename Digraph::Arc& e) { |
|
1781 |
return UndirDigraph::direct(e, false); |
|
1782 |
} |
|
1783 |
|
|
1784 |
/// \brief Residual capacity map. |
|
1785 |
/// |
|
1786 |
/// In generic residual digraphs the residual capacity can be obtained |
|
1787 |
/// as a map. |
|
1788 |
class ResCap { |
|
1789 |
protected: |
|
1790 |
const Adaptor* _adaptor; |
|
1791 |
public: |
|
1792 |
typedef Arc Key; |
|
1793 |
typedef typename _CapacityMap::Value Value; |
|
1794 |
|
|
1795 |
ResCap(const Adaptor& adaptor) : _adaptor(&adaptor) {} |
|
1796 |
|
|
1797 |
Value operator[](const Arc& e) const { |
|
1798 |
return _adaptor->rescap(e); |
|
1799 |
} |
|
1800 |
|
|
1801 |
}; |
|
1802 |
|
|
1803 |
}; |
|
1804 |
|
|
1805 |
/// \brief Base class for split digraph adaptor |
|
1806 |
/// |
|
1807 |
/// Base class of split digraph adaptor. In most case you do not need to |
|
1808 |
/// use it directly but the documented member functions of this class can |
|
1809 |
/// be used with the SplitDigraphAdaptor class. |
|
1810 |
/// \sa SplitDigraphAdaptor |
|
1811 |
template <typename _Digraph> |
|
1812 |
class SplitDigraphAdaptorBase { |
|
1813 |
public: |
|
1814 |
|
|
1815 |
typedef _Digraph Digraph; |
|
1816 |
typedef DigraphAdaptorBase<const _Digraph> Parent; |
|
1817 |
typedef SplitDigraphAdaptorBase Adaptor; |
|
1818 |
|
|
1819 |
typedef typename Digraph::Node DigraphNode; |
|
1820 |
typedef typename Digraph::Arc DigraphArc; |
|
1821 |
|
|
1822 |
class Node; |
|
1823 |
class Arc; |
|
1824 |
|
|
1825 |
private: |
|
1826 |
|
|
1827 |
template <typename T> class NodeMapBase; |
|
1828 |
template <typename T> class ArcMapBase; |
|
1829 |
|
|
1830 |
public: |
|
1831 |
|
|
1832 |
class Node : public DigraphNode { |
|
1833 |
friend class SplitDigraphAdaptorBase; |
|
1834 |
template <typename T> friend class NodeMapBase; |
|
1835 |
private: |
|
1836 |
|
|
1837 |
bool _in; |
|
1838 |
Node(DigraphNode node, bool in) |
|
1839 |
: DigraphNode(node), _in(in) {} |
|
1840 |
|
|
1841 |
public: |
|
1842 |
|
|
1843 |
Node() {} |
|
1844 |
Node(Invalid) : DigraphNode(INVALID), _in(true) {} |
|
1845 |
|
|
1846 |
bool operator==(const Node& node) const { |
|
1847 |
return DigraphNode::operator==(node) && _in == node._in; |
|
1848 |
} |
|
1849 |
|
|
1850 |
bool operator!=(const Node& node) const { |
|
1851 |
return !(*this == node); |
|
1852 |
} |
|
1853 |
|
|
1854 |
bool operator<(const Node& node) const { |
|
1855 |
return DigraphNode::operator<(node) || |
|
1856 |
(DigraphNode::operator==(node) && _in < node._in); |
|
1857 |
} |
|
1858 |
}; |
|
1859 |
|
|
1860 |
class Arc { |
|
1861 |
friend class SplitDigraphAdaptorBase; |
|
1862 |
template <typename T> friend class ArcMapBase; |
|
1863 |
private: |
|
1864 |
typedef BiVariant<DigraphArc, DigraphNode> ArcImpl; |
|
1865 |
|
|
1866 |
explicit Arc(const DigraphArc& arc) : _item(arc) {} |
|
1867 |
explicit Arc(const DigraphNode& node) : _item(node) {} |
|
1868 |
|
|
1869 |
ArcImpl _item; |
|
1870 |
|
|
1871 |
public: |
|
1872 |
Arc() {} |
|
1873 |
Arc(Invalid) : _item(DigraphArc(INVALID)) {} |
|
1874 |
|
|
1875 |
bool operator==(const Arc& arc) const { |
|
1876 |
if (_item.firstState()) { |
|
1877 |
if (arc._item.firstState()) { |
|
1878 |
return _item.first() == arc._item.first(); |
|
1879 |
} |
|
1880 |
} else { |
|
1881 |
if (arc._item.secondState()) { |
|
1882 |
return _item.second() == arc._item.second(); |
|
1883 |
} |
|
1884 |
} |
|
1885 |
return false; |
|
1886 |
} |
|
1887 |
|
|
1888 |
bool operator!=(const Arc& arc) const { |
|
1889 |
return !(*this == arc); |
|
1890 |
} |
|
1891 |
|
|
1892 |
bool operator<(const Arc& arc) const { |
|
1893 |
if (_item.firstState()) { |
|
1894 |
if (arc._item.firstState()) { |
|
1895 |
return _item.first() < arc._item.first(); |
|
1896 |
} |
|
1897 |
return false; |
|
1898 |
} else { |
|
1899 |
if (arc._item.secondState()) { |
|
1900 |
return _item.second() < arc._item.second(); |
|
1901 |
} |
|
1902 |
return true; |
|
1903 |
} |
|
1904 |
} |
|
1905 |
|
|
1906 |
operator DigraphArc() const { return _item.first(); } |
|
1907 |
operator DigraphNode() const { return _item.second(); } |
|
1908 |
|
|
1909 |
}; |
|
1910 |
|
|
1911 |
void first(Node& n) const { |
|
1912 |
_digraph->first(n); |
|
1913 |
n._in = true; |
|
1914 |
} |
|
1915 |
|
|
1916 |
void next(Node& n) const { |
|
1917 |
if (n._in) { |
|
1918 |
n._in = false; |
|
1919 |
} else { |
|
1920 |
n._in = true; |
|
1921 |
_digraph->next(n); |
|
1922 |
} |
|
1923 |
} |
|
1924 |
|
|
1925 |
void first(Arc& e) const { |
|
1926 |
e._item.setSecond(); |
|
1927 |
_digraph->first(e._item.second()); |
|
1928 |
if (e._item.second() == INVALID) { |
|
1929 |
e._item.setFirst(); |
|
1930 |
_digraph->first(e._item.first()); |
|
1931 |
} |
|
1932 |
} |
|
1933 |
|
|
1934 |
void next(Arc& e) const { |
|
1935 |
if (e._item.secondState()) { |
|
1936 |
_digraph->next(e._item.second()); |
|
1937 |
if (e._item.second() == INVALID) { |
|
1938 |
e._item.setFirst(); |
|
1939 |
_digraph->first(e._item.first()); |
|
1940 |
} |
|
1941 |
} else { |
|
1942 |
_digraph->next(e._item.first()); |
|
1943 |
} |
|
1944 |
} |
|
1945 |
|
|
1946 |
void firstOut(Arc& e, const Node& n) const { |
|
1947 |
if (n._in) { |
|
1948 |
e._item.setSecond(n); |
|
1949 |
} else { |
|
1950 |
e._item.setFirst(); |
|
1951 |
_digraph->firstOut(e._item.first(), n); |
|
1952 |
} |
|
1953 |
} |
|
1954 |
|
|
1955 |
void nextOut(Arc& e) const { |
|
1956 |
if (!e._item.firstState()) { |
|
1957 |
e._item.setFirst(INVALID); |
|
1958 |
} else { |
|
1959 |
_digraph->nextOut(e._item.first()); |
|
1960 |
} |
|
1961 |
} |
|
1962 |
|
|
1963 |
void firstIn(Arc& e, const Node& n) const { |
|
1964 |
if (!n._in) { |
|
1965 |
e._item.setSecond(n); |
|
1966 |
} else { |
|
1967 |
e._item.setFirst(); |
|
1968 |
_digraph->firstIn(e._item.first(), n); |
|
1969 |
} |
|
1970 |
} |
|
1971 |
|
|
1972 |
void nextIn(Arc& e) const { |
|
1973 |
if (!e._item.firstState()) { |
|
1974 |
e._item.setFirst(INVALID); |
|
1975 |
} else { |
|
1976 |
_digraph->nextIn(e._item.first()); |
|
1977 |
} |
|
1978 |
} |
|
1979 |
|
|
1980 |
Node source(const Arc& e) const { |
|
1981 |
if (e._item.firstState()) { |
|
1982 |
return Node(_digraph->source(e._item.first()), false); |
|
1983 |
} else { |
|
1984 |
return Node(e._item.second(), true); |
|
1985 |
} |
|
1986 |
} |
|
1987 |
|
|
1988 |
Node target(const Arc& e) const { |
|
1989 |
if (e._item.firstState()) { |
|
1990 |
return Node(_digraph->target(e._item.first()), true); |
|
1991 |
} else { |
|
1992 |
return Node(e._item.second(), false); |
|
1993 |
} |
|
1994 |
} |
|
1995 |
|
|
1996 |
int id(const Node& n) const { |
|
1997 |
return (_digraph->id(n) << 1) | (n._in ? 0 : 1); |
|
1998 |
} |
|
1999 |
Node nodeFromId(int ix) const { |
|
2000 |
return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); |
|
2001 |
} |
|
2002 |
int maxNodeId() const { |
|
2003 |
return 2 * _digraph->maxNodeId() + 1; |
|
2004 |
} |
|
2005 |
|
|
2006 |
int id(const Arc& e) const { |
|
2007 |
if (e._item.firstState()) { |
|
2008 |
return _digraph->id(e._item.first()) << 1; |
|
2009 |
} else { |
|
2010 |
return (_digraph->id(e._item.second()) << 1) | 1; |
|
2011 |
} |
|
2012 |
} |
|
2013 |
Arc arcFromId(int ix) const { |
|
2014 |
if ((ix & 1) == 0) { |
|
2015 |
return Arc(_digraph->arcFromId(ix >> 1)); |
|
2016 |
} else { |
|
2017 |
return Arc(_digraph->nodeFromId(ix >> 1)); |
|
2018 |
} |
|
2019 |
} |
|
2020 |
int maxArcId() const { |
|
2021 |
return std::max(_digraph->maxNodeId() << 1, |
|
2022 |
(_digraph->maxArcId() << 1) | 1); |
|
2023 |
} |
|
2024 |
|
|
2025 |
/// \brief Returns true when the node is in-node. |
|
2026 |
/// |
|
2027 |
/// Returns true when the node is in-node. |
|
2028 |
static bool inNode(const Node& n) { |
|
2029 |
return n._in; |
|
2030 |
} |
|
2031 |
|
|
2032 |
/// \brief Returns true when the node is out-node. |
|
2033 |
/// |
|
2034 |
/// Returns true when the node is out-node. |
|
2035 |
static bool outNode(const Node& n) { |
|
2036 |
return !n._in; |
|
2037 |
} |
|
2038 |
|
|
2039 |
/// \brief Returns true when the arc is arc in the original digraph. |
|
2040 |
/// |
|
2041 |
/// Returns true when the arc is arc in the original digraph. |
|
2042 |
static bool origArc(const Arc& e) { |
|
2043 |
return e._item.firstState(); |
|
2044 |
} |
|
2045 |
|
|
2046 |
/// \brief Returns true when the arc binds an in-node and an out-node. |
|
2047 |
/// |
|
2048 |
/// Returns true when the arc binds an in-node and an out-node. |
|
2049 |
static bool bindArc(const Arc& e) { |
|
2050 |
return e._item.secondState(); |
|
2051 |
} |
|
2052 |
|
|
2053 |
/// \brief Gives back the in-node created from the \c node. |
|
2054 |
/// |
|
2055 |
/// Gives back the in-node created from the \c node. |
|
2056 |
static Node inNode(const DigraphNode& n) { |
|
2057 |
return Node(n, true); |
|
2058 |
} |
|
2059 |
|
|
2060 |
/// \brief Gives back the out-node created from the \c node. |
|
2061 |
/// |
|
2062 |
/// Gives back the out-node created from the \c node. |
|
2063 |
static Node outNode(const DigraphNode& n) { |
|
2064 |
return Node(n, false); |
|
2065 |
} |
|
2066 |
|
|
2067 |
/// \brief Gives back the arc binds the two part of the node. |
|
2068 |
/// |
|
2069 |
/// Gives back the arc binds the two part of the node. |
|
2070 |
static Arc arc(const DigraphNode& n) { |
|
2071 |
return Arc(n); |
|
2072 |
} |
|
2073 |
|
|
2074 |
/// \brief Gives back the arc of the original arc. |
|
2075 |
/// |
|
2076 |
/// Gives back the arc of the original arc. |
|
2077 |
static Arc arc(const DigraphArc& e) { |
|
2078 |
return Arc(e); |
|
2079 |
} |
|
2080 |
|
|
2081 |
typedef True NodeNumTag; |
|
2082 |
|
|
2083 |
int nodeNum() const { |
|
2084 |
return 2 * countNodes(*_digraph); |
|
2085 |
} |
|
2086 |
|
|
2087 |
typedef True EdgeNumTag; |
|
2088 |
int arcNum() const { |
|
2089 |
return countArcs(*_digraph) + countNodes(*_digraph); |
|
2090 |
} |
|
2091 |
|
|
2092 |
typedef True FindEdgeTag; |
|
2093 |
Arc findArc(const Node& u, const Node& v, |
|
2094 |
const Arc& prev = INVALID) const { |
|
2095 |
if (inNode(u)) { |
|
2096 |
if (outNode(v)) { |
|
2097 |
if (static_cast<const DigraphNode&>(u) == |
|
2098 |
static_cast<const DigraphNode&>(v) && prev == INVALID) { |
|
2099 |
return Arc(u); |
|
2100 |
} |
|
2101 |
} |
|
2102 |
} else { |
|
2103 |
if (inNode(v)) { |
|
2104 |
return Arc(::lemon::findArc(*_digraph, u, v, prev)); |
|
2105 |
} |
|
2106 |
} |
|
2107 |
return INVALID; |
|
2108 |
} |
|
2109 |
|
|
2110 |
private: |
|
2111 |
|
|
2112 |
template <typename _Value> |
|
2113 |
class NodeMapBase |
|
2114 |
: public MapTraits<typename Parent::template NodeMap<_Value> > { |
|
2115 |
typedef typename Parent::template NodeMap<_Value> NodeImpl; |
|
2116 |
public: |
|
2117 |
typedef Node Key; |
|
2118 |
typedef _Value Value; |
|
2119 |
|
|
2120 |
NodeMapBase(const Adaptor& adaptor) |
|
2121 |
: _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} |
|
2122 |
NodeMapBase(const Adaptor& adaptor, const Value& value) |
|
2123 |
: _in_map(*adaptor._digraph, value), |
|
2124 |
_out_map(*adaptor._digraph, value) {} |
|
2125 |
|
|
2126 |
void set(const Node& key, const Value& val) { |
|
2127 |
if (Adaptor::inNode(key)) { _in_map.set(key, val); } |
|
2128 |
else {_out_map.set(key, val); } |
|
2129 |
} |
|
2130 |
|
|
2131 |
typename MapTraits<NodeImpl>::ReturnValue |
|
2132 |
operator[](const Node& key) { |
|
2133 |
if (Adaptor::inNode(key)) { return _in_map[key]; } |
|
2134 |
else { return _out_map[key]; } |
|
2135 |
} |
|
2136 |
|
|
2137 |
typename MapTraits<NodeImpl>::ConstReturnValue |
|
2138 |
operator[](const Node& key) const { |
|
2139 |
if (Adaptor::inNode(key)) { return _in_map[key]; } |
|
2140 |
else { return _out_map[key]; } |
|
2141 |
} |
|
2142 |
|
|
2143 |
private: |
|
2144 |
NodeImpl _in_map, _out_map; |
|
2145 |
}; |
|
2146 |
|
|
2147 |
template <typename _Value> |
|
2148 |
class ArcMapBase |
|
2149 |
: public MapTraits<typename Parent::template ArcMap<_Value> > { |
|
2150 |
typedef typename Parent::template ArcMap<_Value> ArcImpl; |
|
2151 |
typedef typename Parent::template NodeMap<_Value> NodeImpl; |
|
2152 |
public: |
|
2153 |
typedef Arc Key; |
|
2154 |
typedef _Value Value; |
|
2155 |
|
|
2156 |
ArcMapBase(const Adaptor& adaptor) |
|
2157 |
: _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} |
|
2158 |
ArcMapBase(const Adaptor& adaptor, const Value& value) |
|
2159 |
: _arc_map(*adaptor._digraph, value), |
|
2160 |
_node_map(*adaptor._digraph, value) {} |
|
2161 |
|
|
2162 |
void set(const Arc& key, const Value& val) { |
|
2163 |
if (Adaptor::origArc(key)) { |
|
2164 |
_arc_map.set(key._item.first(), val); |
|
2165 |
} else { |
|
2166 |
_node_map.set(key._item.second(), val); |
|
2167 |
} |
|
2168 |
} |
|
2169 |
|
|
2170 |
typename MapTraits<ArcImpl>::ReturnValue |
|
2171 |
operator[](const Arc& key) { |
|
2172 |
if (Adaptor::origArc(key)) { |
|
2173 |
return _arc_map[key._item.first()]; |
|
2174 |
} else { |
|
2175 |
return _node_map[key._item.second()]; |
|
2176 |
} |
|
2177 |
} |
|
2178 |
|
|
2179 |
typename MapTraits<ArcImpl>::ConstReturnValue |
|
2180 |
operator[](const Arc& key) const { |
|
2181 |
if (Adaptor::origArc(key)) { |
|
2182 |
return _arc_map[key._item.first()]; |
|
2183 |
} else { |
|
2184 |
return _node_map[key._item.second()]; |
|
2185 |
} |
|
2186 |
} |
|
2187 |
|
|
2188 |
private: |
|
2189 |
ArcImpl _arc_map; |
|
2190 |
NodeImpl _node_map; |
|
2191 |
}; |
|
2192 |
|
|
2193 |
public: |
|
2194 |
|
|
2195 |
template <typename _Value> |
|
2196 |
class NodeMap |
|
2197 |
: public SubMapExtender<Adaptor, NodeMapBase<_Value> > |
|
2198 |
{ |
|
2199 |
public: |
|
2200 |
typedef _Value Value; |
|
2201 |
typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent; |
|
2202 |
|
|
2203 |
NodeMap(const Adaptor& adaptor) |
|
2204 |
: Parent(adaptor) {} |
|
2205 |
|
|
2206 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
2207 |
: Parent(adaptor, value) {} |
|
2208 |
|
|
2209 |
private: |
|
2210 |
NodeMap& operator=(const NodeMap& cmap) { |
|
2211 |
return operator=<NodeMap>(cmap); |
|
2212 |
} |
|
2213 |
|
|
2214 |
template <typename CMap> |
|
2215 |
NodeMap& operator=(const CMap& cmap) { |
|
2216 |
Parent::operator=(cmap); |
|
2217 |
return *this; |
|
2218 |
} |
|
2219 |
}; |
|
2220 |
|
|
2221 |
template <typename _Value> |
|
2222 |
class ArcMap |
|
2223 |
: public SubMapExtender<Adaptor, ArcMapBase<_Value> > |
|
2224 |
{ |
|
2225 |
public: |
|
2226 |
typedef _Value Value; |
|
2227 |
typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent; |
|
2228 |
|
|
2229 |
ArcMap(const Adaptor& adaptor) |
|
2230 |
: Parent(adaptor) {} |
|
2231 |
|
|
2232 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
2233 |
: Parent(adaptor, value) {} |
|
2234 |
|
|
2235 |
private: |
|
2236 |
ArcMap& operator=(const ArcMap& cmap) { |
|
2237 |
return operator=<ArcMap>(cmap); |
|
2238 |
} |
|
2239 |
|
|
2240 |
template <typename CMap> |
|
2241 |
ArcMap& operator=(const CMap& cmap) { |
|
2242 |
Parent::operator=(cmap); |
|
2243 |
return *this; |
|
2244 |
} |
|
2245 |
}; |
|
2246 |
|
|
2247 |
protected: |
|
2248 |
|
|
2249 |
SplitDigraphAdaptorBase() : _digraph(0) {} |
|
2250 |
|
|
2251 |
Digraph* _digraph; |
|
2252 |
|
|
2253 |
void setDigraph(Digraph& digraph) { |
|
2254 |
_digraph = &digraph; |
|
2255 |
} |
|
2256 |
|
|
2257 |
}; |
|
2258 |
|
|
2259 |
/// \ingroup graph_adaptors |
|
2260 |
/// |
|
2261 |
/// \brief Split digraph adaptor class |
|
2262 |
/// |
|
2263 |
/// This is an digraph adaptor which splits all node into an in-node |
|
2264 |
/// and an out-node. Formaly, the adaptor replaces each \f$ u \f$ |
|
2265 |
/// node in the digraph with two node, \f$ u_{in} \f$ node and |
|
2266 |
/// \f$ u_{out} \f$ node. If there is an \f$ (v, u) \f$ arc in the |
|
2267 |
/// original digraph the new target of the arc will be \f$ u_{in} \f$ and |
|
2268 |
/// similarly the source of the original \f$ (u, v) \f$ arc will be |
|
2269 |
/// \f$ u_{out} \f$. The adaptor will add for each node in the |
|
2270 |
/// original digraph an additional arc which will connect |
|
2271 |
/// \f$ (u_{in}, u_{out}) \f$. |
|
2272 |
/// |
|
2273 |
/// The aim of this class is to run algorithm with node costs if the |
|
2274 |
/// algorithm can use directly just arc costs. In this case we should use |
|
2275 |
/// a \c SplitDigraphAdaptor and set the node cost of the digraph to the |
|
2276 |
/// bind arc in the adapted digraph. |
|
2277 |
/// |
|
2278 |
/// By example a maximum flow algoritm can compute how many arc |
|
2279 |
/// disjoint paths are in the digraph. But we would like to know how |
|
2280 |
/// many node disjoint paths are in the digraph. First we have to |
|
2281 |
/// adapt the digraph with the \c SplitDigraphAdaptor. Then run the flow |
|
2282 |
/// algorithm on the adapted digraph. The bottleneck of the flow will |
|
2283 |
/// be the bind arcs which bounds the flow with the count of the |
|
2284 |
/// node disjoint paths. |
|
2285 |
/// |
|
2286 |
///\code |
|
2287 |
/// |
|
2288 |
/// typedef SplitDigraphAdaptor<SmartDigraph> SDigraph; |
|
2289 |
/// |
|
2290 |
/// SDigraph sdigraph(digraph); |
|
2291 |
/// |
|
2292 |
/// typedef ConstMap<SDigraph::Arc, int> SCapacity; |
|
2293 |
/// SCapacity scapacity(1); |
|
2294 |
/// |
|
2295 |
/// SDigraph::ArcMap<int> sflow(sdigraph); |
|
2296 |
/// |
|
2297 |
/// Preflow<SDigraph, SCapacity> |
|
2298 |
/// spreflow(sdigraph, scapacity, |
|
2299 |
/// SDigraph::outNode(source), SDigraph::inNode(target)); |
|
2300 |
/// |
|
2301 |
/// spreflow.run(); |
|
2302 |
/// |
|
2303 |
///\endcode |
|
2304 |
/// |
|
2305 |
/// The result of the mamixum flow on the original digraph |
|
2306 |
/// shows the next figure: |
|
2307 |
/// |
|
2308 |
/// \image html arc_disjoint.png |
|
2309 |
/// \image latex arc_disjoint.eps "Arc disjoint paths" width=\textwidth |
|
2310 |
/// |
|
2311 |
/// And the maximum flow on the adapted digraph: |
|
2312 |
/// |
|
2313 |
/// \image html node_disjoint.png |
|
2314 |
/// \image latex node_disjoint.eps "Node disjoint paths" width=\textwidth |
|
2315 |
/// |
|
2316 |
/// The second solution contains just 3 disjoint paths while the first 4. |
|
2317 |
/// The full code can be found in the \ref disjoint_paths_demo.cc demo file. |
|
2318 |
/// |
|
2319 |
/// This digraph adaptor is fully conform to the |
|
2320 |
/// \ref concepts::Digraph "Digraph" concept and |
|
2321 |
/// contains some additional member functions and types. The |
|
2322 |
/// documentation of some member functions may be found just in the |
|
2323 |
/// SplitDigraphAdaptorBase class. |
|
2324 |
/// |
|
2325 |
/// \sa SplitDigraphAdaptorBase |
|
2326 |
template <typename _Digraph> |
|
2327 |
class SplitDigraphAdaptor |
|
2328 |
: public DigraphAdaptorExtender<SplitDigraphAdaptorBase<_Digraph> > { |
|
2329 |
public: |
|
2330 |
typedef _Digraph Digraph; |
|
2331 |
typedef DigraphAdaptorExtender<SplitDigraphAdaptorBase<Digraph> > Parent; |
|
2332 |
|
|
2333 |
typedef typename Parent::Node Node; |
|
2334 |
typedef typename Parent::Arc Arc; |
|
2335 |
|
|
2336 |
/// \brief Constructor of the adaptor. |
|
2337 |
/// |
|
2338 |
/// Constructor of the adaptor. |
|
2339 |
SplitDigraphAdaptor(Digraph& g) { |
|
2340 |
Parent::setDigraph(g); |
|
2341 |
} |
|
2342 |
|
|
2343 |
/// \brief NodeMap combined from two original NodeMap |
|
2344 |
/// |
|
2345 |
/// This class adapt two of the original digraph NodeMap to |
|
2346 |
/// get a node map on the adapted digraph. |
|
2347 |
template <typename InNodeMap, typename OutNodeMap> |
|
2348 |
class CombinedNodeMap { |
|
2349 |
public: |
|
2350 |
|
|
2351 |
typedef Node Key; |
|
2352 |
typedef typename InNodeMap::Value Value; |
|
2353 |
|
|
2354 |
/// \brief Constructor |
|
2355 |
/// |
|
2356 |
/// Constructor. |
|
2357 |
CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) |
|
2358 |
: _in_map(in_map), _out_map(out_map) {} |
|
2359 |
|
|
2360 |
/// \brief The subscript operator. |
|
2361 |
/// |
|
2362 |
/// The subscript operator. |
|
2363 |
Value& operator[](const Key& key) { |
|
2364 |
if (Parent::inNode(key)) { |
|
2365 |
return _in_map[key]; |
|
2366 |
} else { |
|
2367 |
return _out_map[key]; |
|
2368 |
} |
|
2369 |
} |
|
2370 |
|
|
2371 |
/// \brief The const subscript operator. |
|
2372 |
/// |
|
2373 |
/// The const subscript operator. |
|
2374 |
Value operator[](const Key& key) const { |
|
2375 |
if (Parent::inNode(key)) { |
|
2376 |
return _in_map[key]; |
|
2377 |
} else { |
|
2378 |
return _out_map[key]; |
|
2379 |
} |
|
2380 |
} |
|
2381 |
|
|
2382 |
/// \brief The setter function of the map. |
|
2383 |
/// |
|
2384 |
/// The setter function of the map. |
|
2385 |
void set(const Key& key, const Value& value) { |
|
2386 |
if (Parent::inNode(key)) { |
|
2387 |
_in_map.set(key, value); |
|
2388 |
} else { |
|
2389 |
_out_map.set(key, value); |
|
2390 |
} |
|
2391 |
} |
|
2392 |
|
|
2393 |
private: |
|
2394 |
|
|
2395 |
InNodeMap& _in_map; |
|
2396 |
OutNodeMap& _out_map; |
|
2397 |
|
|
2398 |
}; |
|
2399 |
|
|
2400 |
|
|
2401 |
/// \brief Just gives back a combined node map. |
|
2402 |
/// |
|
2403 |
/// Just gives back a combined node map. |
|
2404 |
template <typename InNodeMap, typename OutNodeMap> |
|
2405 |
static CombinedNodeMap<InNodeMap, OutNodeMap> |
|
2406 |
combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { |
|
2407 |
return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map); |
|
2408 |
} |
|
2409 |
|
|
2410 |
template <typename InNodeMap, typename OutNodeMap> |
|
2411 |
static CombinedNodeMap<const InNodeMap, OutNodeMap> |
|
2412 |
combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { |
|
2413 |
return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map); |
|
2414 |
} |
|
2415 |
|
|
2416 |
template <typename InNodeMap, typename OutNodeMap> |
|
2417 |
static CombinedNodeMap<InNodeMap, const OutNodeMap> |
|
2418 |
combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { |
|
2419 |
return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map); |
|
2420 |
} |
|
2421 |
|
|
2422 |
template <typename InNodeMap, typename OutNodeMap> |
|
2423 |
static CombinedNodeMap<const InNodeMap, const OutNodeMap> |
|
2424 |
combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { |
|
2425 |
return CombinedNodeMap<const InNodeMap, |
|
2426 |
const OutNodeMap>(in_map, out_map); |
|
2427 |
} |
|
2428 |
|
|
2429 |
/// \brief ArcMap combined from an original ArcMap and NodeMap |
|
2430 |
/// |
|
2431 |
/// This class adapt an original digraph ArcMap and NodeMap to |
|
2432 |
/// get an arc map on the adapted digraph. |
|
2433 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
2434 |
class CombinedArcMap { |
|
2435 |
public: |
|
2436 |
|
|
2437 |
typedef Arc Key; |
|
2438 |
typedef typename DigraphArcMap::Value Value; |
|
2439 |
|
|
2440 |
/// \brief Constructor |
|
2441 |
/// |
|
2442 |
/// Constructor. |
|
2443 |
CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) |
|
2444 |
: _arc_map(arc_map), _node_map(node_map) {} |
|
2445 |
|
|
2446 |
/// \brief The subscript operator. |
|
2447 |
/// |
|
2448 |
/// The subscript operator. |
|
2449 |
void set(const Arc& arc, const Value& val) { |
|
2450 |
if (Parent::origArc(arc)) { |
|
2451 |
_arc_map.set(arc, val); |
|
2452 |
} else { |
|
2453 |
_node_map.set(arc, val); |
|
2454 |
} |
|
2455 |
} |
|
2456 |
|
|
2457 |
/// \brief The const subscript operator. |
|
2458 |
/// |
|
2459 |
/// The const subscript operator. |
|
2460 |
Value operator[](const Key& arc) const { |
|
2461 |
if (Parent::origArc(arc)) { |
|
2462 |
return _arc_map[arc]; |
|
2463 |
} else { |
|
2464 |
return _node_map[arc]; |
|
2465 |
} |
|
2466 |
} |
|
2467 |
|
|
2468 |
/// \brief The const subscript operator. |
|
2469 |
/// |
|
2470 |
/// The const subscript operator. |
|
2471 |
Value& operator[](const Key& arc) { |
|
2472 |
if (Parent::origArc(arc)) { |
|
2473 |
return _arc_map[arc]; |
|
2474 |
} else { |
|
2475 |
return _node_map[arc]; |
|
2476 |
} |
|
2477 |
} |
|
2478 |
|
|
2479 |
private: |
|
2480 |
DigraphArcMap& _arc_map; |
|
2481 |
DigraphNodeMap& _node_map; |
|
2482 |
}; |
|
2483 |
|
|
2484 |
/// \brief Just gives back a combined arc map. |
|
2485 |
/// |
|
2486 |
/// Just gives back a combined arc map. |
|
2487 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
2488 |
static CombinedArcMap<DigraphArcMap, DigraphNodeMap> |
|
2489 |
combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) { |
|
2490 |
return CombinedArcMap<DigraphArcMap, DigraphNodeMap>(arc_map, node_map); |
|
2491 |
} |
|
2492 |
|
|
2493 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
2494 |
static CombinedArcMap<const DigraphArcMap, DigraphNodeMap> |
|
2495 |
combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) { |
|
2496 |
return CombinedArcMap<const DigraphArcMap, |
|
2497 |
DigraphNodeMap>(arc_map, node_map); |
|
2498 |
} |
|
2499 |
|
|
2500 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
2501 |
static CombinedArcMap<DigraphArcMap, const DigraphNodeMap> |
|
2502 |
combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) { |
|
2503 |
return CombinedArcMap<DigraphArcMap, |
|
2504 |
const DigraphNodeMap>(arc_map, node_map); |
|
2505 |
} |
|
2506 |
|
|
2507 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
2508 |
static CombinedArcMap<const DigraphArcMap, const DigraphNodeMap> |
|
2509 |
combinedArcMap(const DigraphArcMap& arc_map, |
|
2510 |
const DigraphNodeMap& node_map) { |
|
2511 |
return CombinedArcMap<const DigraphArcMap, |
|
2512 |
const DigraphNodeMap>(arc_map, node_map); |
|
2513 |
} |
|
2514 |
|
|
2515 |
}; |
|
2516 |
|
|
2517 |
/// \brief Just gives back a split digraph adaptor |
|
2518 |
/// |
|
2519 |
/// Just gives back a split digraph adaptor |
|
2520 |
template<typename Digraph> |
|
2521 |
SplitDigraphAdaptor<Digraph> |
|
2522 |
splitDigraphAdaptor(const Digraph& digraph) { |
|
2523 |
return SplitDigraphAdaptor<Digraph>(digraph); |
|
2524 |
} |
|
2525 |
|
|
2526 |
|
|
2527 |
} //namespace lemon |
|
2528 |
|
|
2529 |
#endif //LEMON_DIGRAPH_ADAPTOR_H |
|
2530 |
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
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. |
|
12 |
* |
|
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 |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_GRAPH_ADAPTOR_H |
|
20 |
#define LEMON_GRAPH_ADAPTOR_H |
|
21 |
|
|
22 |
///\ingroup graph_adaptors |
|
23 |
///\file |
|
24 |
///\brief Several graph adaptors. |
|
25 |
/// |
|
26 |
///This file contains several useful undirected graph adaptor classes. |
|
27 |
|
|
28 |
#include <lemon/core.h> |
|
29 |
#include <lemon/maps.h> |
|
30 |
#include <lemon/bits/graph_adaptor_extender.h> |
|
31 |
|
|
32 |
namespace lemon { |
|
33 |
|
|
34 |
/// \brief Base type for the Graph Adaptors |
|
35 |
/// |
|
36 |
/// This is the base type for most of LEMON graph adaptors. |
|
37 |
/// This class implements a trivial graph adaptor i.e. it only wraps the |
|
38 |
/// functions and types of the graph. The purpose of this class is to |
|
39 |
/// make easier implementing graph adaptors. E.g. if an adaptor is |
|
40 |
/// considered which differs from the wrapped graph only in some of its |
|
41 |
/// functions or types, then it can be derived from GraphAdaptor, and only |
|
42 |
/// the differences should be implemented. |
|
43 |
template<typename _Graph> |
|
44 |
class GraphAdaptorBase { |
|
45 |
public: |
|
46 |
typedef _Graph Graph; |
|
47 |
typedef Graph ParentGraph; |
|
48 |
|
|
49 |
protected: |
|
50 |
Graph* _graph; |
|
51 |
|
|
52 |
GraphAdaptorBase() : _graph(0) {} |
|
53 |
|
|
54 |
void setGraph(Graph& graph) { _graph = &graph; } |
|
55 |
|
|
56 |
public: |
|
57 |
GraphAdaptorBase(Graph& graph) : _graph(&graph) {} |
|
58 |
|
|
59 |
typedef typename Graph::Node Node; |
|
60 |
typedef typename Graph::Arc Arc; |
|
61 |
typedef typename Graph::Edge Edge; |
|
62 |
|
|
63 |
void first(Node& i) const { _graph->first(i); } |
|
64 |
void first(Arc& i) const { _graph->first(i); } |
|
65 |
void first(Edge& i) const { _graph->first(i); } |
|
66 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
|
67 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
|
68 |
void firstInc(Edge &i, bool &d, const Node &n) const { |
|
69 |
_graph->firstInc(i, d, n); |
|
70 |
} |
|
71 |
|
|
72 |
void next(Node& i) const { _graph->next(i); } |
|
73 |
void next(Arc& i) const { _graph->next(i); } |
|
74 |
void next(Edge& i) const { _graph->next(i); } |
|
75 |
void nextIn(Arc& i) const { _graph->nextIn(i); } |
|
76 |
void nextOut(Arc& i) const { _graph->nextOut(i); } |
|
77 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
|
78 |
|
|
79 |
Node u(const Edge& e) const { return _graph->u(e); } |
|
80 |
Node v(const Edge& e) const { return _graph->v(e); } |
|
81 |
|
|
82 |
Node source(const Arc& a) const { return _graph->source(a); } |
|
83 |
Node target(const Arc& a) const { return _graph->target(a); } |
|
84 |
|
|
85 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
|
86 |
int nodeNum() const { return _graph->nodeNum(); } |
|
87 |
|
|
88 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
|
89 |
int arcNum() const { return _graph->arcNum(); } |
|
90 |
int edgeNum() const { return _graph->edgeNum(); } |
|
91 |
|
|
92 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
93 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { |
|
94 |
return _graph->findArc(u, v, prev); |
|
95 |
} |
|
96 |
Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) { |
|
97 |
return _graph->findEdge(u, v, prev); |
|
98 |
} |
|
99 |
|
|
100 |
Node addNode() { return _graph->addNode(); } |
|
101 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
|
102 |
|
|
103 |
void erase(const Node& i) { _graph->erase(i); } |
|
104 |
void erase(const Edge& i) { _graph->erase(i); } |
|
105 |
|
|
106 |
void clear() { _graph->clear(); } |
|
107 |
|
|
108 |
bool direction(const Arc& a) const { return _graph->direction(a); } |
|
109 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
|
110 |
|
|
111 |
int id(const Node& v) const { return _graph->id(v); } |
|
112 |
int id(const Arc& a) const { return _graph->id(a); } |
|
113 |
int id(const Edge& e) const { return _graph->id(e); } |
|
114 |
|
|
115 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
|
116 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
|
117 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
|
118 |
|
|
119 |
int maxNodeId() const { return _graph->maxNodeId(); } |
|
120 |
int maxArcId() const { return _graph->maxArcId(); } |
|
121 |
int maxEdgeId() const { return _graph->maxEdgeId(); } |
|
122 |
|
|
123 |
typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; |
|
124 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
|
125 |
|
|
126 |
typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; |
|
127 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
|
128 |
|
|
129 |
typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier; |
|
130 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
|
131 |
|
|
132 |
template <typename _Value> |
|
133 |
class NodeMap : public Graph::template NodeMap<_Value> { |
|
134 |
public: |
|
135 |
typedef typename Graph::template NodeMap<_Value> Parent; |
|
136 |
explicit NodeMap(const GraphAdaptorBase<Graph>& adapter) |
|
137 |
: Parent(*adapter._graph) {} |
|
138 |
NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
|
139 |
: Parent(*adapter._graph, value) {} |
|
140 |
|
|
141 |
private: |
|
142 |
NodeMap& operator=(const NodeMap& cmap) { |
|
143 |
return operator=<NodeMap>(cmap); |
|
144 |
} |
|
145 |
|
|
146 |
template <typename CMap> |
|
147 |
NodeMap& operator=(const CMap& cmap) { |
|
148 |
Parent::operator=(cmap); |
|
149 |
return *this; |
|
150 |
} |
|
151 |
|
|
152 |
}; |
|
153 |
|
|
154 |
template <typename _Value> |
|
155 |
class ArcMap : public Graph::template ArcMap<_Value> { |
|
156 |
public: |
|
157 |
typedef typename Graph::template ArcMap<_Value> Parent; |
|
158 |
explicit ArcMap(const GraphAdaptorBase<Graph>& adapter) |
|
159 |
: Parent(*adapter._graph) {} |
|
160 |
ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
|
161 |
: Parent(*adapter._graph, value) {} |
|
162 |
|
|
163 |
private: |
|
164 |
ArcMap& operator=(const ArcMap& cmap) { |
|
165 |
return operator=<ArcMap>(cmap); |
|
166 |
} |
|
167 |
|
|
168 |
template <typename CMap> |
|
169 |
ArcMap& operator=(const CMap& cmap) { |
|
170 |
Parent::operator=(cmap); |
|
171 |
return *this; |
|
172 |
} |
|
173 |
}; |
|
174 |
|
|
175 |
template <typename _Value> |
|
176 |
class EdgeMap : public Graph::template EdgeMap<_Value> { |
|
177 |
public: |
|
178 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
|
179 |
explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter) |
|
180 |
: Parent(*adapter._graph) {} |
|
181 |
EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
|
182 |
: Parent(*adapter._graph, value) {} |
|
183 |
|
|
184 |
private: |
|
185 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
186 |
return operator=<EdgeMap>(cmap); |
|
187 |
} |
|
188 |
|
|
189 |
template <typename CMap> |
|
190 |
EdgeMap& operator=(const CMap& cmap) { |
|
191 |
Parent::operator=(cmap); |
|
192 |
return *this; |
|
193 |
} |
|
194 |
}; |
|
195 |
|
|
196 |
}; |
|
197 |
|
|
198 |
/// \ingroup graph_adaptors |
|
199 |
/// |
|
200 |
/// \brief Trivial graph adaptor |
|
201 |
/// |
|
202 |
/// This class is an adaptor which does not change the adapted undirected |
|
203 |
/// graph. It can be used only to test the graph adaptors. |
|
204 |
template <typename _Graph> |
|
205 |
class GraphAdaptor |
|
206 |
: public GraphAdaptorExtender< GraphAdaptorBase<_Graph> > { |
|
207 |
public: |
|
208 |
typedef _Graph Graph; |
|
209 |
typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent; |
|
210 |
protected: |
|
211 |
GraphAdaptor() : Parent() {} |
|
212 |
|
|
213 |
public: |
|
214 |
explicit GraphAdaptor(Graph& graph) { setGraph(graph); } |
|
215 |
}; |
|
216 |
|
|
217 |
template <typename _Graph, typename NodeFilterMap, |
|
218 |
typename EdgeFilterMap, bool checked = true> |
|
219 |
class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> { |
|
220 |
public: |
|
221 |
typedef _Graph Graph; |
|
222 |
typedef SubGraphAdaptorBase Adaptor; |
|
223 |
typedef GraphAdaptorBase<_Graph> Parent; |
|
224 |
protected: |
|
225 |
|
|
226 |
NodeFilterMap* _node_filter_map; |
|
227 |
EdgeFilterMap* _edge_filter_map; |
|
228 |
|
|
229 |
SubGraphAdaptorBase() |
|
230 |
: Parent(), _node_filter_map(0), _edge_filter_map(0) { } |
|
231 |
|
|
232 |
void setNodeFilterMap(NodeFilterMap& node_filter_map) { |
|
233 |
_node_filter_map=&node_filter_map; |
|
234 |
} |
|
235 |
void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { |
|
236 |
_edge_filter_map=&edge_filter_map; |
|
237 |
} |
|
238 |
|
|
239 |
public: |
|
240 |
|
|
241 |
typedef typename Parent::Node Node; |
|
242 |
typedef typename Parent::Arc Arc; |
|
243 |
typedef typename Parent::Edge Edge; |
|
244 |
|
|
245 |
void first(Node& i) const { |
|
246 |
Parent::first(i); |
|
247 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
248 |
} |
|
249 |
|
|
250 |
void first(Arc& i) const { |
|
251 |
Parent::first(i); |
|
252 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
253 |
|| !(*_node_filter_map)[Parent::source(i)] |
|
254 |
|| !(*_node_filter_map)[Parent::target(i)])) Parent::next(i); |
|
255 |
} |
|
256 |
|
|
257 |
void first(Edge& i) const { |
|
258 |
Parent::first(i); |
|
259 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
260 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
261 |
|| !(*_node_filter_map)[Parent::v(i)])) Parent::next(i); |
|
262 |
} |
|
263 |
|
|
264 |
void firstIn(Arc& i, const Node& n) const { |
|
265 |
Parent::firstIn(i, n); |
|
266 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
267 |
|| !(*_node_filter_map)[Parent::source(i)])) Parent::nextIn(i); |
|
268 |
} |
|
269 |
|
|
270 |
void firstOut(Arc& i, const Node& n) const { |
|
271 |
Parent::firstOut(i, n); |
|
272 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
273 |
|| !(*_node_filter_map)[Parent::target(i)])) Parent::nextOut(i); |
|
274 |
} |
|
275 |
|
|
276 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
|
277 |
Parent::firstInc(i, d, n); |
|
278 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
279 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
280 |
|| !(*_node_filter_map)[Parent::v(i)])) Parent::nextInc(i, d); |
|
281 |
} |
|
282 |
|
|
283 |
void next(Node& i) const { |
|
284 |
Parent::next(i); |
|
285 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
286 |
} |
|
287 |
|
|
288 |
void next(Arc& i) const { |
|
289 |
Parent::next(i); |
|
290 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
291 |
|| !(*_node_filter_map)[Parent::source(i)] |
|
292 |
|| !(*_node_filter_map)[Parent::target(i)])) Parent::next(i); |
|
293 |
} |
|
294 |
|
|
295 |
void next(Edge& i) const { |
|
296 |
Parent::next(i); |
|
297 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
298 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
299 |
|| !(*_node_filter_map)[Parent::v(i)])) Parent::next(i); |
|
300 |
} |
|
301 |
|
|
302 |
void nextIn(Arc& i) const { |
|
303 |
Parent::nextIn(i); |
|
304 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
305 |
|| !(*_node_filter_map)[Parent::source(i)])) Parent::nextIn(i); |
|
306 |
} |
|
307 |
|
|
308 |
void nextOut(Arc& i) const { |
|
309 |
Parent::nextOut(i); |
|
310 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
311 |
|| !(*_node_filter_map)[Parent::target(i)])) Parent::nextOut(i); |
|
312 |
} |
|
313 |
|
|
314 |
void nextInc(Edge& i, bool& d) const { |
|
315 |
Parent::nextInc(i, d); |
|
316 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
317 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
318 |
|| !(*_node_filter_map)[Parent::v(i)])) Parent::nextInc(i, d); |
|
319 |
} |
|
320 |
|
|
321 |
/// \brief Hide the given node in the graph. |
|
322 |
/// |
|
323 |
/// This function hides \c n in the graph, i.e. the iteration |
|
324 |
/// jumps over it. This is done by simply setting the value of \c n |
|
325 |
/// to be false in the corresponding node-map. |
|
326 |
void hide(const Node& n) const { _node_filter_map->set(n, false); } |
|
327 |
|
|
328 |
/// \brief Hide the given edge in the graph. |
|
329 |
/// |
|
330 |
/// This function hides \c e in the graph, i.e. the iteration |
|
331 |
/// jumps over it. This is done by simply setting the value of \c e |
|
332 |
/// to be false in the corresponding edge-map. |
|
333 |
void hide(const Edge& e) const { _edge_filter_map->set(e, false); } |
|
334 |
|
|
335 |
/// \brief Unhide the given node in the graph. |
|
336 |
/// |
|
337 |
/// The value of \c n is set to be true in the node-map which stores |
|
338 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
339 |
/// again |
|
340 |
void unHide(const Node& n) const { _node_filter_map->set(n, true); } |
|
341 |
|
|
342 |
/// \brief Hide the given edge in the graph. |
|
343 |
/// |
|
344 |
/// The value of \c e is set to be true in the edge-map which stores |
|
345 |
/// hide information. If \c e was hidden previuosly, then it is shown |
|
346 |
/// again |
|
347 |
void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } |
|
348 |
|
|
349 |
/// \brief Returns true if \c n is hidden. |
|
350 |
/// |
|
351 |
/// Returns true if \c n is hidden. |
|
352 |
bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } |
|
353 |
|
|
354 |
/// \brief Returns true if \c e is hidden. |
|
355 |
/// |
|
356 |
/// Returns true if \c e is hidden. |
|
357 |
bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } |
|
358 |
|
|
359 |
typedef False NodeNumTag; |
|
360 |
typedef False EdgeNumTag; |
|
361 |
|
|
362 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
363 |
Arc findArc(const Node& u, const Node& v, |
|
364 |
const Arc& prev = INVALID) { |
|
365 |
if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { |
|
366 |
return INVALID; |
|
367 |
} |
|
368 |
Arc arc = Parent::findArc(u, v, prev); |
|
369 |
while (arc != INVALID && !(*_edge_filter_map)[arc]) { |
|
370 |
arc = Parent::findArc(u, v, arc); |
|
371 |
} |
|
372 |
return arc; |
|
373 |
} |
|
374 |
Edge findEdge(const Node& u, const Node& v, |
|
375 |
const Edge& prev = INVALID) { |
|
376 |
if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { |
|
377 |
return INVALID; |
|
378 |
} |
|
379 |
Edge edge = Parent::findEdge(u, v, prev); |
|
380 |
while (edge != INVALID && !(*_edge_filter_map)[edge]) { |
|
381 |
edge = Parent::findEdge(u, v, edge); |
|
382 |
} |
|
383 |
return edge; |
|
384 |
} |
|
385 |
|
|
386 |
template <typename _Value> |
|
387 |
class NodeMap : public SubMapExtender<Adaptor, |
|
388 |
typename Parent::template NodeMap<_Value> > { |
|
389 |
public: |
|
390 |
typedef _Value Value; |
|
391 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
392 |
template NodeMap<Value> > MapParent; |
|
393 |
|
|
394 |
NodeMap(const Adaptor& adaptor) |
|
395 |
: MapParent(adaptor) {} |
|
396 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
397 |
: MapParent(adaptor, value) {} |
|
398 |
|
|
399 |
private: |
|
400 |
NodeMap& operator=(const NodeMap& cmap) { |
|
401 |
return operator=<NodeMap>(cmap); |
|
402 |
} |
|
403 |
|
|
404 |
template <typename CMap> |
|
405 |
NodeMap& operator=(const CMap& cmap) { |
|
406 |
MapParent::operator=(cmap); |
|
407 |
return *this; |
|
408 |
} |
|
409 |
}; |
|
410 |
|
|
411 |
template <typename _Value> |
|
412 |
class ArcMap : public SubMapExtender<Adaptor, |
|
413 |
typename Parent::template ArcMap<_Value> > { |
|
414 |
public: |
|
415 |
typedef _Value Value; |
|
416 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
417 |
template ArcMap<Value> > MapParent; |
|
418 |
|
|
419 |
ArcMap(const Adaptor& adaptor) |
|
420 |
: MapParent(adaptor) {} |
|
421 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
422 |
: MapParent(adaptor, value) {} |
|
423 |
|
|
424 |
private: |
|
425 |
ArcMap& operator=(const ArcMap& cmap) { |
|
426 |
return operator=<ArcMap>(cmap); |
|
427 |
} |
|
428 |
|
|
429 |
template <typename CMap> |
|
430 |
ArcMap& operator=(const CMap& cmap) { |
|
431 |
MapParent::operator=(cmap); |
|
432 |
return *this; |
|
433 |
} |
|
434 |
}; |
|
435 |
|
|
436 |
template <typename _Value> |
|
437 |
class EdgeMap : public SubMapExtender<Adaptor, |
|
438 |
typename Parent::template EdgeMap<_Value> > { |
|
439 |
public: |
|
440 |
typedef _Value Value; |
|
441 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
442 |
template EdgeMap<Value> > MapParent; |
|
443 |
|
|
444 |
EdgeMap(const Adaptor& adaptor) |
|
445 |
: MapParent(adaptor) {} |
|
446 |
|
|
447 |
EdgeMap(const Adaptor& adaptor, const Value& value) |
|
448 |
: MapParent(adaptor, value) {} |
|
449 |
|
|
450 |
private: |
|
451 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
452 |
return operator=<EdgeMap>(cmap); |
|
453 |
} |
|
454 |
|
|
455 |
template <typename CMap> |
|
456 |
EdgeMap& operator=(const CMap& cmap) { |
|
457 |
MapParent::operator=(cmap); |
|
458 |
return *this; |
|
459 |
} |
|
460 |
}; |
|
461 |
|
|
462 |
}; |
|
463 |
|
|
464 |
template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap> |
|
465 |
class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false> |
|
466 |
: public GraphAdaptorBase<_Graph> { |
|
467 |
public: |
|
468 |
typedef _Graph Graph; |
|
469 |
typedef SubGraphAdaptorBase Adaptor; |
|
470 |
typedef GraphAdaptorBase<_Graph> Parent; |
|
471 |
protected: |
|
472 |
NodeFilterMap* _node_filter_map; |
|
473 |
EdgeFilterMap* _edge_filter_map; |
|
474 |
SubGraphAdaptorBase() : Parent(), |
|
475 |
_node_filter_map(0), _edge_filter_map(0) { } |
|
476 |
|
|
477 |
void setNodeFilterMap(NodeFilterMap& node_filter_map) { |
|
478 |
_node_filter_map=&node_filter_map; |
|
479 |
} |
|
480 |
void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { |
|
481 |
_edge_filter_map=&edge_filter_map; |
|
482 |
} |
|
483 |
|
|
484 |
public: |
|
485 |
|
|
486 |
typedef typename Parent::Node Node; |
|
487 |
typedef typename Parent::Arc Arc; |
|
488 |
typedef typename Parent::Edge Edge; |
|
489 |
|
|
490 |
void first(Node& i) const { |
|
491 |
Parent::first(i); |
|
492 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
493 |
} |
|
494 |
|
|
495 |
void first(Arc& i) const { |
|
496 |
Parent::first(i); |
|
497 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
498 |
} |
|
499 |
|
|
500 |
void first(Edge& i) const { |
|
501 |
Parent::first(i); |
|
502 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
503 |
} |
|
504 |
|
|
505 |
void firstIn(Arc& i, const Node& n) const { |
|
506 |
Parent::firstIn(i, n); |
|
507 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); |
|
508 |
} |
|
509 |
|
|
510 |
void firstOut(Arc& i, const Node& n) const { |
|
511 |
Parent::firstOut(i, n); |
|
512 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); |
|
513 |
} |
|
514 |
|
|
515 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
|
516 |
Parent::firstInc(i, d, n); |
|
517 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); |
|
518 |
} |
|
519 |
|
|
520 |
void next(Node& i) const { |
|
521 |
Parent::next(i); |
|
522 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
523 |
} |
|
524 |
void next(Arc& i) const { |
|
525 |
Parent::next(i); |
|
526 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
527 |
} |
|
528 |
void next(Edge& i) const { |
|
529 |
Parent::next(i); |
|
530 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
531 |
} |
|
532 |
void nextIn(Arc& i) const { |
|
533 |
Parent::nextIn(i); |
|
534 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); |
|
535 |
} |
|
536 |
|
|
537 |
void nextOut(Arc& i) const { |
|
538 |
Parent::nextOut(i); |
|
539 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); |
|
540 |
} |
|
541 |
void nextInc(Edge& i, bool& d) const { |
|
542 |
Parent::nextInc(i, d); |
|
543 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); |
|
544 |
} |
|
545 |
|
|
546 |
/// \brief Hide the given node in the graph. |
|
547 |
/// |
|
548 |
/// This function hides \c n in the graph, i.e. the iteration |
|
549 |
/// jumps over it. This is done by simply setting the value of \c n |
|
550 |
/// to be false in the corresponding node-map. |
|
551 |
void hide(const Node& n) const { _node_filter_map->set(n, false); } |
|
552 |
|
|
553 |
/// \brief Hide the given edge in the graph. |
|
554 |
/// |
|
555 |
/// This function hides \c e in the graph, i.e. the iteration |
|
556 |
/// jumps over it. This is done by simply setting the value of \c e |
|
557 |
/// to be false in the corresponding edge-map. |
|
558 |
void hide(const Edge& e) const { _edge_filter_map->set(e, false); } |
|
559 |
|
|
560 |
/// \brief Unhide the given node in the graph. |
|
561 |
/// |
|
562 |
/// The value of \c n is set to be true in the node-map which stores |
|
563 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
564 |
/// again |
|
565 |
void unHide(const Node& n) const { _node_filter_map->set(n, true); } |
|
566 |
|
|
567 |
/// \brief Hide the given edge in the graph. |
|
568 |
/// |
|
569 |
/// The value of \c e is set to be true in the edge-map which stores |
|
570 |
/// hide information. If \c e was hidden previuosly, then it is shown |
|
571 |
/// again |
|
572 |
void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } |
|
573 |
|
|
574 |
/// \brief Returns true if \c n is hidden. |
|
575 |
/// |
|
576 |
/// Returns true if \c n is hidden. |
|
577 |
bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } |
|
578 |
|
|
579 |
/// \brief Returns true if \c e is hidden. |
|
580 |
/// |
|
581 |
/// Returns true if \c e is hidden. |
|
582 |
bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } |
|
583 |
|
|
584 |
typedef False NodeNumTag; |
|
585 |
typedef False EdgeNumTag; |
|
586 |
|
|
587 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
588 |
Arc findArc(const Node& u, const Node& v, |
|
589 |
const Arc& prev = INVALID) { |
|
590 |
Arc arc = Parent::findArc(u, v, prev); |
|
591 |
while (arc != INVALID && !(*_edge_filter_map)[arc]) { |
|
592 |
arc = Parent::findArc(u, v, arc); |
|
593 |
} |
|
594 |
return arc; |
|
595 |
} |
|
596 |
Edge findEdge(const Node& u, const Node& v, |
|
597 |
const Edge& prev = INVALID) { |
|
598 |
Edge edge = Parent::findEdge(u, v, prev); |
|
599 |
while (edge != INVALID && !(*_edge_filter_map)[edge]) { |
|
600 |
edge = Parent::findEdge(u, v, edge); |
|
601 |
} |
|
602 |
return edge; |
|
603 |
} |
|
604 |
|
|
605 |
template <typename _Value> |
|
606 |
class NodeMap : public SubMapExtender<Adaptor, |
|
607 |
typename Parent::template NodeMap<_Value> > { |
|
608 |
public: |
|
609 |
typedef _Value Value; |
|
610 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
611 |
template NodeMap<Value> > MapParent; |
|
612 |
|
|
613 |
NodeMap(const Adaptor& adaptor) |
|
614 |
: MapParent(adaptor) {} |
|
615 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
616 |
: MapParent(adaptor, value) {} |
|
617 |
|
|
618 |
private: |
|
619 |
NodeMap& operator=(const NodeMap& cmap) { |
|
620 |
return operator=<NodeMap>(cmap); |
|
621 |
} |
|
622 |
|
|
623 |
template <typename CMap> |
|
624 |
NodeMap& operator=(const CMap& cmap) { |
|
625 |
MapParent::operator=(cmap); |
|
626 |
return *this; |
|
627 |
} |
|
628 |
}; |
|
629 |
|
|
630 |
template <typename _Value> |
|
631 |
class ArcMap : public SubMapExtender<Adaptor, |
|
632 |
typename Parent::template ArcMap<_Value> > { |
|
633 |
public: |
|
634 |
typedef _Value Value; |
|
635 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
636 |
template ArcMap<Value> > MapParent; |
|
637 |
|
|
638 |
ArcMap(const Adaptor& adaptor) |
|
639 |
: MapParent(adaptor) {} |
|
640 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
641 |
: MapParent(adaptor, value) {} |
|
642 |
|
|
643 |
private: |
|
644 |
ArcMap& operator=(const ArcMap& cmap) { |
|
645 |
return operator=<ArcMap>(cmap); |
|
646 |
} |
|
647 |
|
|
648 |
template <typename CMap> |
|
649 |
ArcMap& operator=(const CMap& cmap) { |
|
650 |
MapParent::operator=(cmap); |
|
651 |
return *this; |
|
652 |
} |
|
653 |
}; |
|
654 |
|
|
655 |
template <typename _Value> |
|
656 |
class EdgeMap : public SubMapExtender<Adaptor, |
|
657 |
typename Parent::template EdgeMap<_Value> > { |
|
658 |
public: |
|
659 |
typedef _Value Value; |
|
660 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
661 |
template EdgeMap<Value> > MapParent; |
|
662 |
|
|
663 |
EdgeMap(const Adaptor& adaptor) |
|
664 |
: MapParent(adaptor) {} |
|
665 |
|
|
666 |
EdgeMap(const Adaptor& adaptor, const _Value& value) |
|
667 |
: MapParent(adaptor, value) {} |
|
668 |
|
|
669 |
private: |
|
670 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
671 |
return operator=<EdgeMap>(cmap); |
|
672 |
} |
|
673 |
|
|
674 |
template <typename CMap> |
|
675 |
EdgeMap& operator=(const CMap& cmap) { |
|
676 |
MapParent::operator=(cmap); |
|
677 |
return *this; |
|
678 |
} |
|
679 |
}; |
|
680 |
|
|
681 |
}; |
|
682 |
|
|
683 |
/// \ingroup graph_adaptors |
|
684 |
/// |
|
685 |
/// \brief A graph adaptor for hiding nodes and arcs from an |
|
686 |
/// undirected graph. |
|
687 |
/// |
|
688 |
/// SubGraphAdaptor shows the graph with filtered node-set and |
|
689 |
/// edge-set. If the \c checked parameter is true then it filters |
|
690 |
/// the edge-set to do not get invalid edges which incident node is |
|
691 |
/// filtered. |
|
692 |
/// |
|
693 |
/// If the \c checked template parameter is false then we have to |
|
694 |
/// note that the node-iterator cares only the filter on the |
|
695 |
/// node-set, and the edge-iterator cares only the filter on the |
|
696 |
/// edge-set. This way the edge-map should filter all arcs which |
|
697 |
/// has filtered end node. |
|
698 |
template<typename _Graph, typename NodeFilterMap, |
|
699 |
typename EdgeFilterMap, bool checked = true> |
|
700 |
class SubGraphAdaptor : |
|
701 |
public GraphAdaptorExtender< |
|
702 |
SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > { |
|
703 |
public: |
|
704 |
typedef _Graph Graph; |
|
705 |
typedef GraphAdaptorExtender< |
|
706 |
SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent; |
|
707 |
protected: |
|
708 |
SubGraphAdaptor() { } |
|
709 |
public: |
|
710 |
SubGraphAdaptor(Graph& _graph, NodeFilterMap& node_filter_map, |
|
711 |
EdgeFilterMap& edge_filter_map) { |
|
712 |
setGraph(_graph); |
|
713 |
setNodeFilterMap(node_filter_map); |
|
714 |
setEdgeFilterMap(edge_filter_map); |
|
715 |
} |
|
716 |
}; |
|
717 |
|
|
718 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
719 |
SubGraphAdaptor<const Graph, NodeFilterMap, ArcFilterMap> |
|
720 |
subGraphAdaptor(const Graph& graph, |
|
721 |
NodeFilterMap& nfm, ArcFilterMap& efm) { |
|
722 |
return SubGraphAdaptor<const Graph, NodeFilterMap, ArcFilterMap> |
|
723 |
(graph, nfm, efm); |
|
724 |
} |
|
725 |
|
|
726 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
727 |
SubGraphAdaptor<const Graph, const NodeFilterMap, ArcFilterMap> |
|
728 |
subGraphAdaptor(const Graph& graph, |
|
729 |
NodeFilterMap& nfm, ArcFilterMap& efm) { |
|
730 |
return SubGraphAdaptor<const Graph, const NodeFilterMap, ArcFilterMap> |
|
731 |
(graph, nfm, efm); |
|
732 |
} |
|
733 |
|
|
734 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
735 |
SubGraphAdaptor<const Graph, NodeFilterMap, const ArcFilterMap> |
|
736 |
subGraphAdaptor(const Graph& graph, |
|
737 |
NodeFilterMap& nfm, ArcFilterMap& efm) { |
|
738 |
return SubGraphAdaptor<const Graph, NodeFilterMap, const ArcFilterMap> |
|
739 |
(graph, nfm, efm); |
|
740 |
} |
|
741 |
|
|
742 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
743 |
SubGraphAdaptor<const Graph, const NodeFilterMap, const ArcFilterMap> |
|
744 |
subGraphAdaptor(const Graph& graph, |
|
745 |
NodeFilterMap& nfm, ArcFilterMap& efm) { |
|
746 |
return SubGraphAdaptor<const Graph, const NodeFilterMap, |
|
747 |
const ArcFilterMap>(graph, nfm, efm); |
|
748 |
} |
|
749 |
|
|
750 |
/// \ingroup graph_adaptors |
|
751 |
/// |
|
752 |
/// \brief An adaptor for hiding nodes from an graph. |
|
753 |
/// |
|
754 |
/// An adaptor for hiding nodes from an graph. This |
|
755 |
/// adaptor specializes SubGraphAdaptor in the way that only the |
|
756 |
/// node-set can be filtered. In usual case the checked parameter is |
|
757 |
/// true, we get the induced subgraph. But if the checked parameter |
|
758 |
/// is false then we can filter only isolated nodes. |
|
759 |
template<typename _Graph, typename _NodeFilterMap, bool checked = true> |
|
760 |
class NodeSubGraphAdaptor : |
|
761 |
public SubGraphAdaptor<_Graph, _NodeFilterMap, |
|
762 |
ConstMap<typename _Graph::Edge, bool>, checked> { |
|
763 |
public: |
|
764 |
typedef _Graph Graph; |
|
765 |
typedef _NodeFilterMap NodeFilterMap; |
|
766 |
typedef SubGraphAdaptor<Graph, NodeFilterMap, |
|
767 |
ConstMap<typename Graph::Edge, bool> > Parent; |
|
768 |
protected: |
|
769 |
ConstMap<typename Graph::Edge, bool> const_true_map; |
|
770 |
|
|
771 |
NodeSubGraphAdaptor() : const_true_map(true) { |
|
772 |
Parent::setEdgeFilterMap(const_true_map); |
|
773 |
} |
|
774 |
|
|
775 |
public: |
|
776 |
NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& node_filter_map) : |
|
777 |
Parent(), const_true_map(true) { |
|
778 |
Parent::setGraph(_graph); |
|
779 |
Parent::setNodeFilterMap(node_filter_map); |
|
780 |
Parent::setEdgeFilterMap(const_true_map); |
|
781 |
} |
|
782 |
}; |
|
783 |
|
|
784 |
template<typename Graph, typename NodeFilterMap> |
|
785 |
NodeSubGraphAdaptor<const Graph, NodeFilterMap> |
|
786 |
nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) { |
|
787 |
return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm); |
|
788 |
} |
|
789 |
|
|
790 |
template<typename Graph, typename NodeFilterMap> |
|
791 |
NodeSubGraphAdaptor<const Graph, const NodeFilterMap> |
|
792 |
nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) { |
|
793 |
return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm); |
|
794 |
} |
|
795 |
|
|
796 |
/// \ingroup graph_adaptors |
|
797 |
/// |
|
798 |
/// \brief An adaptor for hiding edges from an graph. |
|
799 |
/// |
|
800 |
/// \warning Graph adaptors are in even more experimental state |
|
801 |
/// than the other parts of the lib. Use them at you own risk. |
|
802 |
/// |
|
803 |
/// An adaptor for hiding edges from an graph. |
|
804 |
/// This adaptor specializes SubGraphAdaptor in the way that |
|
805 |
/// only the arc-set |
|
806 |
/// can be filtered. |
|
807 |
template<typename _Graph, typename _EdgeFilterMap> |
|
808 |
class EdgeSubGraphAdaptor : |
|
809 |
public SubGraphAdaptor<_Graph, ConstMap<typename _Graph::Node,bool>, |
|
810 |
_EdgeFilterMap, false> { |
|
811 |
public: |
|
812 |
typedef _Graph Graph; |
|
813 |
typedef _EdgeFilterMap EdgeFilterMap; |
|
814 |
typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>, |
|
815 |
EdgeFilterMap, false> Parent; |
|
816 |
protected: |
|
817 |
ConstMap<typename Graph::Node, bool> const_true_map; |
|
818 |
|
|
819 |
EdgeSubGraphAdaptor() : const_true_map(true) { |
|
820 |
Parent::setNodeFilterMap(const_true_map); |
|
821 |
} |
|
822 |
|
|
823 |
public: |
|
824 |
|
|
825 |
EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& edge_filter_map) : |
|
826 |
Parent(), const_true_map(true) { |
|
827 |
Parent::setGraph(_graph); |
|
828 |
Parent::setNodeFilterMap(const_true_map); |
|
829 |
Parent::setEdgeFilterMap(edge_filter_map); |
|
830 |
} |
|
831 |
|
|
832 |
}; |
|
833 |
|
|
834 |
template<typename Graph, typename EdgeFilterMap> |
|
835 |
EdgeSubGraphAdaptor<const Graph, EdgeFilterMap> |
|
836 |
edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) { |
|
837 |
return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm); |
|
838 |
} |
|
839 |
|
|
840 |
template<typename Graph, typename EdgeFilterMap> |
|
841 |
EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap> |
|
842 |
edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) { |
|
843 |
return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm); |
|
844 |
} |
|
845 |
|
|
846 |
/// \brief Base of direct graph adaptor |
|
847 |
/// |
|
848 |
/// Base class of the direct graph adaptor. All public member |
|
849 |
/// of this class can be used with the DirGraphAdaptor too. |
|
850 |
/// \sa DirGraphAdaptor |
|
851 |
template <typename _Graph, typename _DirectionMap> |
|
852 |
class DirGraphAdaptorBase { |
|
853 |
public: |
|
854 |
|
|
855 |
typedef _Graph Graph; |
|
856 |
typedef _DirectionMap DirectionMap; |
|
857 |
|
|
858 |
typedef typename Graph::Node Node; |
|
859 |
typedef typename Graph::Edge Arc; |
|
860 |
|
|
861 |
/// \brief Reverse arc |
|
862 |
/// |
|
863 |
/// It reverse the given arc. It simply negate the direction in the map. |
|
864 |
void reverseArc(const Arc& arc) { |
|
865 |
_direction->set(arc, !(*_direction)[arc]); |
|
866 |
} |
|
867 |
|
|
868 |
void first(Node& i) const { _graph->first(i); } |
|
869 |
void first(Arc& i) const { _graph->first(i); } |
|
870 |
void firstIn(Arc& i, const Node& n) const { |
|
871 |
bool d; |
|
872 |
_graph->firstInc(i, d, n); |
|
873 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
|
874 |
} |
|
875 |
void firstOut(Arc& i, const Node& n ) const { |
|
876 |
bool d; |
|
877 |
_graph->firstInc(i, d, n); |
|
878 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
|
879 |
} |
|
880 |
|
|
881 |
void next(Node& i) const { _graph->next(i); } |
|
882 |
void next(Arc& i) const { _graph->next(i); } |
|
883 |
void nextIn(Arc& i) const { |
|
884 |
bool d = !(*_direction)[i]; |
|
885 |
_graph->nextInc(i, d); |
|
886 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
|
887 |
} |
|
888 |
void nextOut(Arc& i) const { |
|
889 |
bool d = (*_direction)[i]; |
|
890 |
_graph->nextInc(i, d); |
|
891 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
|
892 |
} |
|
893 |
|
|
894 |
Node source(const Arc& e) const { |
|
895 |
return (*_direction)[e] ? _graph->u(e) : _graph->v(e); |
|
896 |
} |
|
897 |
Node target(const Arc& e) const { |
|
898 |
return (*_direction)[e] ? _graph->v(e) : _graph->u(e); |
|
899 |
} |
|
900 |
|
|
901 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
|
902 |
int nodeNum() const { return _graph->nodeNum(); } |
|
903 |
|
|
904 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
|
905 |
int arcNum() const { return _graph->edgeNum(); } |
|
906 |
|
|
907 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
908 |
Arc findArc(const Node& u, const Node& v, |
|
909 |
const Arc& prev = INVALID) { |
|
910 |
Arc arc = prev; |
|
911 |
bool d = arc == INVALID ? true : (*_direction)[arc]; |
|
912 |
if (d) { |
|
913 |
arc = _graph->findEdge(u, v, arc); |
|
914 |
while (arc != INVALID && !(*_direction)[arc]) { |
|
915 |
_graph->findEdge(u, v, arc); |
|
916 |
} |
|
917 |
if (arc != INVALID) return arc; |
|
918 |
} |
|
919 |
_graph->findEdge(v, u, arc); |
|
920 |
while (arc != INVALID && (*_direction)[arc]) { |
|
921 |
_graph->findEdge(u, v, arc); |
|
922 |
} |
|
923 |
return arc; |
|
924 |
} |
|
925 |
|
|
926 |
Node addNode() { |
|
927 |
return Node(_graph->addNode()); |
|
928 |
} |
|
929 |
|
|
930 |
Arc addArc(const Node& u, const Node& v) { |
|
931 |
Arc arc = _graph->addArc(u, v); |
|
932 |
_direction->set(arc, _graph->source(arc) == u); |
|
933 |
return arc; |
|
934 |
} |
|
935 |
|
|
936 |
void erase(const Node& i) { _graph->erase(i); } |
|
937 |
void erase(const Arc& i) { _graph->erase(i); } |
|
938 |
|
|
939 |
void clear() { _graph->clear(); } |
|
940 |
|
|
941 |
int id(const Node& v) const { return _graph->id(v); } |
|
942 |
int id(const Arc& e) const { return _graph->id(e); } |
|
943 |
|
|
944 |
Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } |
|
945 |
Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } |
|
946 |
|
|
947 |
int maxNodeId() const { return _graph->maxNodeId(); } |
|
948 |
int maxArcId() const { return _graph->maxEdgeId(); } |
|
949 |
|
|
950 |
typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; |
|
951 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
|
952 |
|
|
953 |
typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; |
|
954 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
|
955 |
|
|
956 |
template <typename _Value> |
|
957 |
class NodeMap : public _Graph::template NodeMap<_Value> { |
|
958 |
public: |
|
959 |
|
|
960 |
typedef typename _Graph::template NodeMap<_Value> Parent; |
|
961 |
|
|
962 |
explicit NodeMap(const DirGraphAdaptorBase& adapter) |
|
963 |
: Parent(*adapter._graph) {} |
|
964 |
|
|
965 |
NodeMap(const DirGraphAdaptorBase& adapter, const _Value& value) |
|
966 |
: Parent(*adapter._graph, value) {} |
|
967 |
|
|
968 |
private: |
|
969 |
NodeMap& operator=(const NodeMap& cmap) { |
|
970 |
return operator=<NodeMap>(cmap); |
|
971 |
} |
|
972 |
|
|
973 |
template <typename CMap> |
|
974 |
NodeMap& operator=(const CMap& cmap) { |
|
975 |
Parent::operator=(cmap); |
|
976 |
return *this; |
|
977 |
} |
|
978 |
|
|
979 |
}; |
|
980 |
|
|
981 |
template <typename _Value> |
|
982 |
class ArcMap : public _Graph::template EdgeMap<_Value> { |
|
983 |
public: |
|
984 |
|
|
985 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
|
986 |
|
|
987 |
explicit ArcMap(const DirGraphAdaptorBase& adapter) |
|
988 |
: Parent(*adapter._graph) { } |
|
989 |
|
|
990 |
ArcMap(const DirGraphAdaptorBase& adapter, const _Value& value) |
|
991 |
: Parent(*adapter._graph, value) { } |
|
992 |
|
|
993 |
private: |
|
994 |
ArcMap& operator=(const ArcMap& cmap) { |
|
995 |
return operator=<ArcMap>(cmap); |
|
996 |
} |
|
997 |
|
|
998 |
template <typename CMap> |
|
999 |
ArcMap& operator=(const CMap& cmap) { |
|
1000 |
Parent::operator=(cmap); |
|
1001 |
return *this; |
|
1002 |
} |
|
1003 |
}; |
|
1004 |
|
|
1005 |
|
|
1006 |
|
|
1007 |
protected: |
|
1008 |
Graph* _graph; |
|
1009 |
DirectionMap* _direction; |
|
1010 |
|
|
1011 |
void setDirectionMap(DirectionMap& direction) { |
|
1012 |
_direction = &direction; |
|
1013 |
} |
|
1014 |
|
|
1015 |
void setGraph(Graph& graph) { |
|
1016 |
_graph = &graph; |
|
1017 |
} |
|
1018 |
|
|
1019 |
}; |
|
1020 |
|
|
1021 |
|
|
1022 |
/// \ingroup graph_adaptors |
|
1023 |
/// |
|
1024 |
/// \brief A directed graph is made from an graph by an adaptor |
|
1025 |
/// |
|
1026 |
/// This adaptor gives a direction for each edge in the undirected |
|
1027 |
/// graph. The direction of the arcs stored in the |
|
1028 |
/// DirectionMap. This map is a bool map on the edges. If |
|
1029 |
/// the edge is mapped to true then the direction of the directed |
|
1030 |
/// arc will be the same as the default direction of the edge. The |
|
1031 |
/// arcs can be easily reverted by the \ref |
|
1032 |
/// DirGraphAdaptorBase::reverseArc "reverseArc()" member in the |
|
1033 |
/// adaptor. |
|
1034 |
/// |
|
1035 |
/// It can be used to solve orientation problems on directed graphs. |
|
1036 |
/// For example how can we orient an graph to get the minimum |
|
1037 |
/// number of strongly connected components. If we orient the arcs with |
|
1038 |
/// the dfs algorithm out from the source then we will get such an |
|
1039 |
/// orientation. |
|
1040 |
/// |
|
1041 |
/// We use the \ref DfsVisitor "visitor" interface of the |
|
1042 |
/// \ref DfsVisit "dfs" algorithm: |
|
1043 |
///\code |
|
1044 |
/// template <typename DirMap> |
|
1045 |
/// class OrientVisitor : public DfsVisitor<Graph> { |
|
1046 |
/// public: |
|
1047 |
/// |
|
1048 |
/// OrientVisitor(const Graph& graph, DirMap& dirMap) |
|
1049 |
/// : _graph(graph), _dirMap(dirMap), _processed(graph, false) {} |
|
1050 |
/// |
|
1051 |
/// void discover(const Arc& arc) { |
|
1052 |
/// _processed.set(arc, true); |
|
1053 |
/// _dirMap.set(arc, _graph.direction(arc)); |
|
1054 |
/// } |
|
1055 |
/// |
|
1056 |
/// void examine(const Arc& arc) { |
|
1057 |
/// if (_processed[arc]) return; |
|
1058 |
/// _processed.set(arc, true); |
|
1059 |
/// _dirMap.set(arc, _graph.direction(arc)); |
|
1060 |
/// } |
|
1061 |
/// |
|
1062 |
/// private: |
|
1063 |
/// const Graph& _graph; |
|
1064 |
/// DirMap& _dirMap; |
|
1065 |
/// Graph::EdgeMap<bool> _processed; |
|
1066 |
/// }; |
|
1067 |
///\endcode |
|
1068 |
/// |
|
1069 |
/// And now we can use the orientation: |
|
1070 |
///\code |
|
1071 |
/// Graph::EdgeMap<bool> dmap(graph); |
|
1072 |
/// |
|
1073 |
/// typedef OrientVisitor<Graph::EdgeMap<bool> > Visitor; |
|
1074 |
/// Visitor visitor(graph, dmap); |
|
1075 |
/// |
|
1076 |
/// DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
1077 |
/// |
|
1078 |
/// dfs.run(); |
|
1079 |
/// |
|
1080 |
/// typedef DirGraphAdaptor<Graph> DGraph; |
|
1081 |
/// DGraph dgraph(graph, dmap); |
|
1082 |
/// |
|
1083 |
/// LEMON_ASSERT(countStronglyConnectedComponents(dgraph) == |
|
1084 |
/// countBiArcConnectedComponents(graph), "Wrong Orientation"); |
|
1085 |
///\endcode |
|
1086 |
/// |
|
1087 |
/// The number of the bi-connected components is a lower bound for |
|
1088 |
/// the number of the strongly connected components in the directed |
|
1089 |
/// graph because if we contract the bi-connected components to |
|
1090 |
/// nodes we will get a tree therefore we cannot orient arcs in |
|
1091 |
/// both direction between bi-connected components. In the other way |
|
1092 |
/// the algorithm will orient one component to be strongly |
|
1093 |
/// connected. The two relations proof that the assertion will |
|
1094 |
/// be always true and the found solution is optimal. |
|
1095 |
/// |
|
1096 |
/// \sa DirGraphAdaptorBase |
|
1097 |
/// \sa dirGraphAdaptor |
|
1098 |
template<typename _Graph, |
|
1099 |
typename DirectionMap = typename _Graph::template EdgeMap<bool> > |
|
1100 |
class DirGraphAdaptor : |
|
1101 |
public DigraphAdaptorExtender<DirGraphAdaptorBase<_Graph, DirectionMap> > { |
|
1102 |
public: |
|
1103 |
typedef _Graph Graph; |
|
1104 |
typedef DigraphAdaptorExtender< |
|
1105 |
DirGraphAdaptorBase<_Graph, DirectionMap> > Parent; |
|
1106 |
protected: |
|
1107 |
DirGraphAdaptor() { } |
|
1108 |
public: |
|
1109 |
|
|
1110 |
/// \brief Constructor of the adaptor |
|
1111 |
/// |
|
1112 |
/// Constructor of the adaptor |
|
1113 |
DirGraphAdaptor(Graph& graph, DirectionMap& direction) { |
|
1114 |
setGraph(graph); |
|
1115 |
setDirectionMap(direction); |
|
1116 |
} |
|
1117 |
}; |
|
1118 |
|
|
1119 |
/// \brief Just gives back a DirGraphAdaptor |
|
1120 |
/// |
|
1121 |
/// Just gives back a DirGraphAdaptor |
|
1122 |
template<typename Graph, typename DirectionMap> |
|
1123 |
DirGraphAdaptor<const Graph, DirectionMap> |
|
1124 |
dirGraphAdaptor(const Graph& graph, DirectionMap& dm) { |
|
1125 |
return DirGraphAdaptor<const Graph, DirectionMap>(graph, dm); |
|
1126 |
} |
|
1127 |
|
|
1128 |
template<typename Graph, typename DirectionMap> |
|
1129 |
DirGraphAdaptor<const Graph, const DirectionMap> |
|
1130 |
dirGraphAdaptor(const Graph& graph, const DirectionMap& dm) { |
|
1131 |
return DirGraphAdaptor<const Graph, const DirectionMap>(graph, dm); |
|
1132 |
} |
|
1133 |
|
|
1134 |
} |
|
1135 |
|
|
1136 |
#endif |
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
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. |
|
12 |
* |
|
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 |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#include<iostream> |
|
20 |
#include<lemon/concept_check.h> |
|
21 |
|
|
22 |
#include<lemon/list_graph.h> |
|
23 |
#include<lemon/smart_graph.h> |
|
24 |
|
|
25 |
#include<lemon/concepts/digraph.h> |
|
26 |
#include<lemon/concepts/graph.h> |
|
27 |
|
|
28 |
#include<lemon/digraph_adaptor.h> |
|
29 |
#include<lemon/graph_adaptor.h> |
|
30 |
|
|
31 |
#include <limits> |
|
32 |
#include <lemon/bfs.h> |
|
33 |
#include <lemon/path.h> |
|
34 |
|
|
35 |
#include"test/test_tools.h" |
|
36 |
#include"test/graph_test.h" |
|
37 |
|
|
38 |
using namespace lemon; |
|
39 |
|
|
40 |
void checkDigraphAdaptor() { |
|
41 |
checkConcept<concepts::Digraph, DigraphAdaptor<concepts::Digraph> >(); |
|
42 |
|
|
43 |
typedef ListDigraph Digraph; |
|
44 |
typedef DigraphAdaptor<Digraph> Adaptor; |
|
45 |
|
|
46 |
Digraph digraph; |
|
47 |
Adaptor adaptor(digraph); |
|
48 |
|
|
49 |
Digraph::Node n1 = digraph.addNode(); |
|
50 |
Digraph::Node n2 = digraph.addNode(); |
|
51 |
Digraph::Node n3 = digraph.addNode(); |
|
52 |
|
|
53 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
54 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
55 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
56 |
|
|
57 |
checkGraphNodeList(adaptor, 3); |
|
58 |
checkGraphArcList(adaptor, 3); |
|
59 |
checkGraphConArcList(adaptor, 3); |
|
60 |
|
|
61 |
checkGraphOutArcList(adaptor, n1, 2); |
|
62 |
checkGraphOutArcList(adaptor, n2, 1); |
|
63 |
checkGraphOutArcList(adaptor, n3, 0); |
|
64 |
|
|
65 |
checkGraphInArcList(adaptor, n1, 0); |
|
66 |
checkGraphInArcList(adaptor, n2, 1); |
|
67 |
checkGraphInArcList(adaptor, n3, 2); |
|
68 |
|
|
69 |
checkNodeIds(adaptor); |
|
70 |
checkArcIds(adaptor); |
|
71 |
|
|
72 |
checkGraphNodeMap(adaptor); |
|
73 |
checkGraphArcMap(adaptor); |
|
74 |
} |
|
75 |
|
|
76 |
void checkRevDigraphAdaptor() { |
|
77 |
checkConcept<concepts::Digraph, RevDigraphAdaptor<concepts::Digraph> >(); |
|
78 |
|
|
79 |
typedef ListDigraph Digraph; |
|
80 |
typedef RevDigraphAdaptor<Digraph> Adaptor; |
|
81 |
|
|
82 |
Digraph digraph; |
|
83 |
Adaptor adaptor(digraph); |
|
84 |
|
|
85 |
Digraph::Node n1 = digraph.addNode(); |
|
86 |
Digraph::Node n2 = digraph.addNode(); |
|
87 |
Digraph::Node n3 = digraph.addNode(); |
|
88 |
|
|
89 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
90 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
91 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
92 |
|
|
93 |
checkGraphNodeList(adaptor, 3); |
|
94 |
checkGraphArcList(adaptor, 3); |
|
95 |
checkGraphConArcList(adaptor, 3); |
|
96 |
|
|
97 |
checkGraphOutArcList(adaptor, n1, 0); |
|
98 |
checkGraphOutArcList(adaptor, n2, 1); |
|
99 |
checkGraphOutArcList(adaptor, n3, 2); |
|
100 |
|
|
101 |
checkGraphInArcList(adaptor, n1, 2); |
|
102 |
checkGraphInArcList(adaptor, n2, 1); |
|
103 |
checkGraphInArcList(adaptor, n3, 0); |
|
104 |
|
|
105 |
checkNodeIds(adaptor); |
|
106 |
checkArcIds(adaptor); |
|
107 |
|
|
108 |
checkGraphNodeMap(adaptor); |
|
109 |
checkGraphArcMap(adaptor); |
|
110 |
|
|
111 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
112 |
check(adaptor.source(a) == digraph.target(a), "Wrong reverse"); |
|
113 |
check(adaptor.target(a) == digraph.source(a), "Wrong reverse"); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
void checkSubDigraphAdaptor() { |
|
118 |
checkConcept<concepts::Digraph, |
|
119 |
SubDigraphAdaptor<concepts::Digraph, |
|
120 |
concepts::Digraph::NodeMap<bool>, |
|
121 |
concepts::Digraph::ArcMap<bool> > >(); |
|
122 |
|
|
123 |
typedef ListDigraph Digraph; |
|
124 |
typedef Digraph::NodeMap<bool> NodeFilter; |
|
125 |
typedef Digraph::ArcMap<bool> ArcFilter; |
|
126 |
typedef SubDigraphAdaptor<Digraph, NodeFilter, ArcFilter> Adaptor; |
|
127 |
|
|
128 |
Digraph digraph; |
|
129 |
NodeFilter node_filter(digraph); |
|
130 |
ArcFilter arc_filter(digraph); |
|
131 |
Adaptor adaptor(digraph, node_filter, arc_filter); |
|
132 |
|
|
133 |
Digraph::Node n1 = digraph.addNode(); |
|
134 |
Digraph::Node n2 = digraph.addNode(); |
|
135 |
Digraph::Node n3 = digraph.addNode(); |
|
136 |
|
|
137 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
138 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
139 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
140 |
|
|
141 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = true; |
|
142 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = true; |
|
143 |
|
|
144 |
checkGraphNodeList(adaptor, 3); |
|
145 |
checkGraphArcList(adaptor, 3); |
|
146 |
checkGraphConArcList(adaptor, 3); |
|
147 |
|
|
148 |
checkGraphOutArcList(adaptor, n1, 2); |
|
149 |
checkGraphOutArcList(adaptor, n2, 1); |
|
150 |
checkGraphOutArcList(adaptor, n3, 0); |
|
151 |
|
|
152 |
checkGraphInArcList(adaptor, n1, 0); |
|
153 |
checkGraphInArcList(adaptor, n2, 1); |
|
154 |
checkGraphInArcList(adaptor, n3, 2); |
|
155 |
|
|
156 |
checkNodeIds(adaptor); |
|
157 |
checkArcIds(adaptor); |
|
158 |
|
|
159 |
checkGraphNodeMap(adaptor); |
|
160 |
checkGraphArcMap(adaptor); |
|
161 |
|
|
162 |
arc_filter[a2] = false; |
|
163 |
|
|
164 |
checkGraphNodeList(adaptor, 3); |
|
165 |
checkGraphArcList(adaptor, 2); |
|
166 |
checkGraphConArcList(adaptor, 2); |
|
167 |
|
|
168 |
checkGraphOutArcList(adaptor, n1, 1); |
|
169 |
checkGraphOutArcList(adaptor, n2, 1); |
|
170 |
checkGraphOutArcList(adaptor, n3, 0); |
|
171 |
|
|
172 |
checkGraphInArcList(adaptor, n1, 0); |
|
173 |
checkGraphInArcList(adaptor, n2, 1); |
|
174 |
checkGraphInArcList(adaptor, n3, 1); |
|
175 |
|
|
176 |
checkNodeIds(adaptor); |
|
177 |
checkArcIds(adaptor); |
|
178 |
|
|
179 |
checkGraphNodeMap(adaptor); |
|
180 |
checkGraphArcMap(adaptor); |
|
181 |
|
|
182 |
node_filter[n1] = false; |
|
183 |
|
|
184 |
checkGraphNodeList(adaptor, 2); |
|
185 |
checkGraphArcList(adaptor, 1); |
|
186 |
checkGraphConArcList(adaptor, 1); |
|
187 |
|
|
188 |
checkGraphOutArcList(adaptor, n2, 1); |
|
189 |
checkGraphOutArcList(adaptor, n3, 0); |
|
190 |
|
|
191 |
checkGraphInArcList(adaptor, n2, 0); |
|
192 |
checkGraphInArcList(adaptor, n3, 1); |
|
193 |
|
|
194 |
checkNodeIds(adaptor); |
|
195 |
checkArcIds(adaptor); |
|
196 |
|
|
197 |
checkGraphNodeMap(adaptor); |
|
198 |
checkGraphArcMap(adaptor); |
|
199 |
|
|
200 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = false; |
|
201 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = false; |
|
202 |
|
|
203 |
checkGraphNodeList(adaptor, 0); |
|
204 |
checkGraphArcList(adaptor, 0); |
|
205 |
checkGraphConArcList(adaptor, 0); |
|
206 |
|
|
207 |
checkNodeIds(adaptor); |
|
208 |
checkArcIds(adaptor); |
|
209 |
|
|
210 |
checkGraphNodeMap(adaptor); |
|
211 |
checkGraphArcMap(adaptor); |
|
212 |
} |
|
213 |
|
|
214 |
void checkNodeSubDigraphAdaptor() { |
|
215 |
checkConcept<concepts::Digraph, |
|
216 |
NodeSubDigraphAdaptor<concepts::Digraph, |
|
217 |
concepts::Digraph::NodeMap<bool> > >(); |
|
218 |
|
|
219 |
typedef ListDigraph Digraph; |
|
220 |
typedef Digraph::NodeMap<bool> NodeFilter; |
|
221 |
typedef NodeSubDigraphAdaptor<Digraph, NodeFilter> Adaptor; |
|
222 |
|
|
223 |
Digraph digraph; |
|
224 |
NodeFilter node_filter(digraph); |
|
225 |
Adaptor adaptor(digraph, node_filter); |
|
226 |
|
|
227 |
Digraph::Node n1 = digraph.addNode(); |
|
228 |
Digraph::Node n2 = digraph.addNode(); |
|
229 |
Digraph::Node n3 = digraph.addNode(); |
|
230 |
|
|
231 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
232 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
233 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
234 |
|
|
235 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = true; |
|
236 |
|
|
237 |
checkGraphNodeList(adaptor, 3); |
|
238 |
checkGraphArcList(adaptor, 3); |
|
239 |
checkGraphConArcList(adaptor, 3); |
|
240 |
|
|
241 |
checkGraphOutArcList(adaptor, n1, 2); |
|
242 |
checkGraphOutArcList(adaptor, n2, 1); |
|
243 |
checkGraphOutArcList(adaptor, n3, 0); |
|
244 |
|
|
245 |
checkGraphInArcList(adaptor, n1, 0); |
|
246 |
checkGraphInArcList(adaptor, n2, 1); |
|
247 |
checkGraphInArcList(adaptor, n3, 2); |
|
248 |
|
|
249 |
checkNodeIds(adaptor); |
|
250 |
checkArcIds(adaptor); |
|
251 |
|
|
252 |
checkGraphNodeMap(adaptor); |
|
253 |
checkGraphArcMap(adaptor); |
|
254 |
|
|
255 |
node_filter[n1] = false; |
|
256 |
|
|
257 |
checkGraphNodeList(adaptor, 2); |
|
258 |
checkGraphArcList(adaptor, 1); |
|
259 |
checkGraphConArcList(adaptor, 1); |
|
260 |
|
|
261 |
checkGraphOutArcList(adaptor, n2, 1); |
|
262 |
checkGraphOutArcList(adaptor, n3, 0); |
|
263 |
|
|
264 |
checkGraphInArcList(adaptor, n2, 0); |
|
265 |
checkGraphInArcList(adaptor, n3, 1); |
|
266 |
|
|
267 |
checkNodeIds(adaptor); |
|
268 |
checkArcIds(adaptor); |
|
269 |
|
|
270 |
checkGraphNodeMap(adaptor); |
|
271 |
checkGraphArcMap(adaptor); |
|
272 |
|
|
273 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = false; |
|
274 |
|
|
275 |
checkGraphNodeList(adaptor, 0); |
|
276 |
checkGraphArcList(adaptor, 0); |
|
277 |
checkGraphConArcList(adaptor, 0); |
|
278 |
|
|
279 |
checkNodeIds(adaptor); |
|
280 |
checkArcIds(adaptor); |
|
281 |
|
|
282 |
checkGraphNodeMap(adaptor); |
|
283 |
checkGraphArcMap(adaptor); |
|
284 |
} |
|
285 |
|
|
286 |
void checkArcSubDigraphAdaptor() { |
|
287 |
checkConcept<concepts::Digraph, |
|
288 |
ArcSubDigraphAdaptor<concepts::Digraph, |
|
289 |
concepts::Digraph::ArcMap<bool> > >(); |
|
290 |
|
|
291 |
typedef ListDigraph Digraph; |
|
292 |
typedef Digraph::ArcMap<bool> ArcFilter; |
|
293 |
typedef ArcSubDigraphAdaptor<Digraph, ArcFilter> Adaptor; |
|
294 |
|
|
295 |
Digraph digraph; |
|
296 |
ArcFilter arc_filter(digraph); |
|
297 |
Adaptor adaptor(digraph, arc_filter); |
|
298 |
|
|
299 |
Digraph::Node n1 = digraph.addNode(); |
|
300 |
Digraph::Node n2 = digraph.addNode(); |
|
301 |
Digraph::Node n3 = digraph.addNode(); |
|
302 |
|
|
303 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
304 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
305 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
306 |
|
|
307 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = true; |
|
308 |
|
|
309 |
checkGraphNodeList(adaptor, 3); |
|
310 |
checkGraphArcList(adaptor, 3); |
|
311 |
checkGraphConArcList(adaptor, 3); |
|
312 |
|
|
313 |
checkGraphOutArcList(adaptor, n1, 2); |
|
314 |
checkGraphOutArcList(adaptor, n2, 1); |
|
315 |
checkGraphOutArcList(adaptor, n3, 0); |
|
316 |
|
|
317 |
checkGraphInArcList(adaptor, n1, 0); |
|
318 |
checkGraphInArcList(adaptor, n2, 1); |
|
319 |
checkGraphInArcList(adaptor, n3, 2); |
|
320 |
|
|
321 |
checkNodeIds(adaptor); |
|
322 |
checkArcIds(adaptor); |
|
323 |
|
|
324 |
checkGraphNodeMap(adaptor); |
|
325 |
checkGraphArcMap(adaptor); |
|
326 |
|
|
327 |
arc_filter[a2] = false; |
|
328 |
|
|
329 |
checkGraphNodeList(adaptor, 3); |
|
330 |
checkGraphArcList(adaptor, 2); |
|
331 |
checkGraphConArcList(adaptor, 2); |
|
332 |
|
|
333 |
checkGraphOutArcList(adaptor, n1, 1); |
|
334 |
checkGraphOutArcList(adaptor, n2, 1); |
|
335 |
checkGraphOutArcList(adaptor, n3, 0); |
|
336 |
|
|
337 |
checkGraphInArcList(adaptor, n1, 0); |
|
338 |
checkGraphInArcList(adaptor, n2, 1); |
|
339 |
checkGraphInArcList(adaptor, n3, 1); |
|
340 |
|
|
341 |
checkNodeIds(adaptor); |
|
342 |
checkArcIds(adaptor); |
|
343 |
|
|
344 |
checkGraphNodeMap(adaptor); |
|
345 |
checkGraphArcMap(adaptor); |
|
346 |
|
|
347 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = false; |
|
348 |
|
|
349 |
checkGraphNodeList(adaptor, 3); |
|
350 |
checkGraphArcList(adaptor, 0); |
|
351 |
checkGraphConArcList(adaptor, 0); |
|
352 |
|
|
353 |
checkNodeIds(adaptor); |
|
354 |
checkArcIds(adaptor); |
|
355 |
|
|
356 |
checkGraphNodeMap(adaptor); |
|
357 |
checkGraphArcMap(adaptor); |
|
358 |
} |
|
359 |
|
|
360 |
void checkUndirDigraphAdaptor() { |
|
361 |
checkConcept<concepts::Graph, UndirDigraphAdaptor<concepts::Digraph> >(); |
|
362 |
|
|
363 |
typedef ListDigraph Digraph; |
|
364 |
typedef UndirDigraphAdaptor<Digraph> Adaptor; |
|
365 |
|
|
366 |
Digraph digraph; |
|
367 |
Adaptor adaptor(digraph); |
|
368 |
|
|
369 |
Digraph::Node n1 = digraph.addNode(); |
|
370 |
Digraph::Node n2 = digraph.addNode(); |
|
371 |
Digraph::Node n3 = digraph.addNode(); |
|
372 |
|
|
373 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
374 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
375 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
376 |
|
|
377 |
checkGraphNodeList(adaptor, 3); |
|
378 |
checkGraphArcList(adaptor, 6); |
|
379 |
checkGraphEdgeList(adaptor, 3); |
|
380 |
checkGraphConArcList(adaptor, 6); |
|
381 |
checkGraphConEdgeList(adaptor, 3); |
|
382 |
|
|
383 |
checkGraphOutArcList(adaptor, n1, 2); |
|
384 |
checkGraphOutArcList(adaptor, n2, 2); |
|
385 |
checkGraphOutArcList(adaptor, n3, 2); |
|
386 |
|
|
387 |
checkGraphInArcList(adaptor, n1, 2); |
|
388 |
checkGraphInArcList(adaptor, n2, 2); |
|
389 |
checkGraphInArcList(adaptor, n3, 2); |
|
390 |
|
|
391 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
392 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
393 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
394 |
|
|
395 |
checkNodeIds(adaptor); |
|
396 |
checkArcIds(adaptor); |
|
397 |
checkEdgeIds(adaptor); |
|
398 |
|
|
399 |
checkGraphNodeMap(adaptor); |
|
400 |
checkGraphArcMap(adaptor); |
|
401 |
checkGraphEdgeMap(adaptor); |
|
402 |
|
|
403 |
for (Adaptor::EdgeIt e(adaptor); e != INVALID; ++e) { |
|
404 |
check(adaptor.u(e) == digraph.source(e), "Wrong undir"); |
|
405 |
check(adaptor.v(e) == digraph.target(e), "Wrong undir"); |
|
406 |
} |
|
407 |
|
|
408 |
} |
|
409 |
|
|
410 |
void checkResDigraphAdaptor() { |
|
411 |
checkConcept<concepts::Digraph, |
|
412 |
ResDigraphAdaptor<concepts::Digraph, |
|
413 |
concepts::Digraph::ArcMap<int>, |
|
414 |
concepts::Digraph::ArcMap<int> > >(); |
|
415 |
|
|
416 |
typedef ListDigraph Digraph; |
|
417 |
typedef Digraph::ArcMap<int> IntArcMap; |
|
418 |
typedef ResDigraphAdaptor<Digraph, IntArcMap> Adaptor; |
|
419 |
|
|
420 |
Digraph digraph; |
|
421 |
IntArcMap capacity(digraph), flow(digraph); |
|
422 |
Adaptor adaptor(digraph, capacity, flow); |
|
423 |
|
|
424 |
Digraph::Node n1 = digraph.addNode(); |
|
425 |
Digraph::Node n2 = digraph.addNode(); |
|
426 |
Digraph::Node n3 = digraph.addNode(); |
|
427 |
Digraph::Node n4 = digraph.addNode(); |
|
428 |
|
|
429 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
430 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
431 |
Digraph::Arc a3 = digraph.addArc(n1, n4); |
|
432 |
Digraph::Arc a4 = digraph.addArc(n2, n3); |
|
433 |
Digraph::Arc a5 = digraph.addArc(n2, n4); |
|
434 |
Digraph::Arc a6 = digraph.addArc(n3, n4); |
|
435 |
|
|
436 |
capacity[a1] = 8; |
|
437 |
capacity[a2] = 6; |
|
438 |
capacity[a3] = 4; |
|
439 |
capacity[a4] = 4; |
|
440 |
capacity[a5] = 6; |
|
441 |
capacity[a6] = 10; |
|
442 |
|
|
443 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
444 |
flow[a] = 0; |
|
445 |
} |
|
446 |
|
|
447 |
checkGraphNodeList(adaptor, 4); |
|
448 |
checkGraphArcList(adaptor, 6); |
|
449 |
checkGraphConArcList(adaptor, 6); |
|
450 |
|
|
451 |
checkGraphOutArcList(adaptor, n1, 3); |
|
452 |
checkGraphOutArcList(adaptor, n2, 2); |
|
453 |
checkGraphOutArcList(adaptor, n3, 1); |
|
454 |
checkGraphOutArcList(adaptor, n4, 0); |
|
455 |
|
|
456 |
checkGraphInArcList(adaptor, n1, 0); |
|
457 |
checkGraphInArcList(adaptor, n2, 1); |
|
458 |
checkGraphInArcList(adaptor, n3, 2); |
|
459 |
checkGraphInArcList(adaptor, n4, 3); |
|
460 |
|
|
461 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
462 |
flow[a] = capacity[a] / 2; |
|
463 |
} |
|
464 |
|
|
465 |
checkGraphNodeList(adaptor, 4); |
|
466 |
checkGraphArcList(adaptor, 12); |
|
467 |
checkGraphConArcList(adaptor, 12); |
|
468 |
|
|
469 |
checkGraphOutArcList(adaptor, n1, 3); |
|
470 |
checkGraphOutArcList(adaptor, n2, 3); |
|
471 |
checkGraphOutArcList(adaptor, n3, 3); |
|
472 |
checkGraphOutArcList(adaptor, n4, 3); |
|
473 |
|
|
474 |
checkGraphInArcList(adaptor, n1, 3); |
|
475 |
checkGraphInArcList(adaptor, n2, 3); |
|
476 |
checkGraphInArcList(adaptor, n3, 3); |
|
477 |
checkGraphInArcList(adaptor, n4, 3); |
|
478 |
|
|
479 |
checkNodeIds(adaptor); |
|
480 |
checkArcIds(adaptor); |
|
481 |
|
|
482 |
checkGraphNodeMap(adaptor); |
|
483 |
checkGraphArcMap(adaptor); |
|
484 |
|
|
485 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
486 |
flow[a] = capacity[a]; |
|
487 |
} |
|
488 |
|
|
489 |
checkGraphNodeList(adaptor, 4); |
|
490 |
checkGraphArcList(adaptor, 6); |
|
491 |
checkGraphConArcList(adaptor, 6); |
|
492 |
|
|
493 |
checkGraphOutArcList(adaptor, n1, 0); |
|
494 |
checkGraphOutArcList(adaptor, n2, 1); |
|
495 |
checkGraphOutArcList(adaptor, n3, 2); |
|
496 |
checkGraphOutArcList(adaptor, n4, 3); |
|
497 |
|
|
498 |
checkGraphInArcList(adaptor, n1, 3); |
|
499 |
checkGraphInArcList(adaptor, n2, 2); |
|
500 |
checkGraphInArcList(adaptor, n3, 1); |
|
501 |
checkGraphInArcList(adaptor, n4, 0); |
|
502 |
|
|
503 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
504 |
flow[a] = 0; |
|
505 |
} |
|
506 |
|
|
507 |
int flow_value = 0; |
|
508 |
while (true) { |
|
509 |
|
|
510 |
Bfs<Adaptor> bfs(adaptor); |
|
511 |
bfs.run(n1, n4); |
|
512 |
|
|
513 |
if (!bfs.reached(n4)) break; |
|
514 |
|
|
515 |
Path<Adaptor> p = bfs.path(n4); |
|
516 |
|
|
517 |
int min = std::numeric_limits<int>::max(); |
|
518 |
for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) { |
|
519 |
if (adaptor.rescap(a) < min) min = adaptor.rescap(a); |
|
520 |
} |
|
521 |
|
|
522 |
for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) { |
|
523 |
adaptor.augment(a, min); |
|
524 |
} |
|
525 |
flow_value += min; |
|
526 |
} |
|
527 |
|
|
528 |
check(flow_value == 18, "Wrong flow with res graph adaptor"); |
|
529 |
|
|
530 |
} |
|
531 |
|
|
532 |
void checkSplitDigraphAdaptor() { |
|
533 |
checkConcept<concepts::Digraph, SplitDigraphAdaptor<concepts::Digraph> >(); |
|
534 |
|
|
535 |
typedef ListDigraph Digraph; |
|
536 |
typedef SplitDigraphAdaptor<Digraph> Adaptor; |
|
537 |
|
|
538 |
Digraph digraph; |
|
539 |
Adaptor adaptor(digraph); |
|
540 |
|
|
541 |
Digraph::Node n1 = digraph.addNode(); |
|
542 |
Digraph::Node n2 = digraph.addNode(); |
|
543 |
Digraph::Node n3 = digraph.addNode(); |
|
544 |
|
|
545 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
546 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
547 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
548 |
|
|
549 |
checkGraphNodeList(adaptor, 6); |
|
550 |
checkGraphArcList(adaptor, 6); |
|
551 |
checkGraphConArcList(adaptor, 6); |
|
552 |
|
|
553 |
checkGraphOutArcList(adaptor, adaptor.inNode(n1), 1); |
|
554 |
checkGraphOutArcList(adaptor, adaptor.outNode(n1), 2); |
|
555 |
checkGraphOutArcList(adaptor, adaptor.inNode(n2), 1); |
|
556 |
checkGraphOutArcList(adaptor, adaptor.outNode(n2), 1); |
|
557 |
checkGraphOutArcList(adaptor, adaptor.inNode(n3), 1); |
|
558 |
checkGraphOutArcList(adaptor, adaptor.outNode(n3), 0); |
|
559 |
|
|
560 |
checkGraphInArcList(adaptor, adaptor.inNode(n1), 0); |
|
561 |
checkGraphInArcList(adaptor, adaptor.outNode(n1), 1); |
|
562 |
checkGraphInArcList(adaptor, adaptor.inNode(n2), 1); |
|
563 |
checkGraphInArcList(adaptor, adaptor.outNode(n2), 1); |
|
564 |
checkGraphInArcList(adaptor, adaptor.inNode(n3), 2); |
|
565 |
checkGraphInArcList(adaptor, adaptor.outNode(n3), 1); |
|
566 |
|
|
567 |
checkNodeIds(adaptor); |
|
568 |
checkArcIds(adaptor); |
|
569 |
|
|
570 |
checkGraphNodeMap(adaptor); |
|
571 |
checkGraphArcMap(adaptor); |
|
572 |
|
|
573 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
574 |
if (adaptor.origArc(a)) { |
|
575 |
Digraph::Arc oa = a; |
|
576 |
check(adaptor.source(a) == adaptor.outNode(digraph.source(oa)), |
|
577 |
"Wrong split"); |
|
578 |
check(adaptor.target(a) == adaptor.inNode(digraph.target(oa)), |
|
579 |
"Wrong split"); |
|
580 |
} else { |
|
581 |
Digraph::Node on = a; |
|
582 |
check(adaptor.source(a) == adaptor.inNode(on), "Wrong split"); |
|
583 |
check(adaptor.target(a) == adaptor.outNode(on), "Wrong split"); |
|
584 |
} |
|
585 |
} |
|
586 |
} |
|
587 |
|
|
588 |
void checkGraphAdaptor() { |
|
589 |
checkConcept<concepts::Graph, GraphAdaptor<concepts::Graph> >(); |
|
590 |
|
|
591 |
typedef ListGraph Graph; |
|
592 |
typedef GraphAdaptor<Graph> Adaptor; |
|
593 |
|
|
594 |
Graph graph; |
|
595 |
Adaptor adaptor(graph); |
|
596 |
|
|
597 |
Graph::Node n1 = graph.addNode(); |
|
598 |
Graph::Node n2 = graph.addNode(); |
|
599 |
Graph::Node n3 = graph.addNode(); |
|
600 |
Graph::Node n4 = graph.addNode(); |
|
601 |
|
|
602 |
Graph::Edge a1 = graph.addEdge(n1, n2); |
|
603 |
Graph::Edge a2 = graph.addEdge(n1, n3); |
|
604 |
Graph::Edge a3 = graph.addEdge(n2, n3); |
|
605 |
Graph::Edge a4 = graph.addEdge(n3, n4); |
|
606 |
|
|
607 |
checkGraphNodeList(adaptor, 4); |
|
608 |
checkGraphArcList(adaptor, 8); |
|
609 |
checkGraphEdgeList(adaptor, 4); |
|
610 |
checkGraphConArcList(adaptor, 8); |
|
611 |
checkGraphConEdgeList(adaptor, 4); |
|
612 |
|
|
613 |
checkGraphOutArcList(adaptor, n1, 2); |
|
614 |
checkGraphOutArcList(adaptor, n2, 2); |
|
615 |
checkGraphOutArcList(adaptor, n3, 3); |
|
616 |
checkGraphOutArcList(adaptor, n4, 1); |
|
617 |
|
|
618 |
checkGraphInArcList(adaptor, n1, 2); |
|
619 |
checkGraphInArcList(adaptor, n2, 2); |
|
620 |
checkGraphInArcList(adaptor, n3, 3); |
|
621 |
checkGraphInArcList(adaptor, n4, 1); |
|
622 |
|
|
623 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
624 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
625 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
626 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
627 |
|
|
628 |
|
|
629 |
checkNodeIds(adaptor); |
|
630 |
checkArcIds(adaptor); |
|
631 |
checkEdgeIds(adaptor); |
|
632 |
|
|
633 |
checkGraphNodeMap(adaptor); |
|
634 |
checkGraphArcMap(adaptor); |
|
635 |
checkGraphEdgeMap(adaptor); |
|
636 |
} |
|
637 |
|
|
638 |
void checkSubGraphAdaptor() { |
|
639 |
checkConcept<concepts::Graph, |
|
640 |
SubGraphAdaptor<concepts::Graph, |
|
641 |
concepts::Graph::NodeMap<bool>, |
|
642 |
concepts::Graph::EdgeMap<bool> > >(); |
|
643 |
|
|
644 |
typedef ListGraph Graph; |
|
645 |
typedef Graph::NodeMap<bool> NodeFilter; |
|
646 |
typedef Graph::EdgeMap<bool> EdgeFilter; |
|
647 |
typedef SubGraphAdaptor<Graph, NodeFilter, EdgeFilter> Adaptor; |
|
648 |
|
|
649 |
Graph graph; |
|
650 |
NodeFilter node_filter(graph); |
|
651 |
EdgeFilter edge_filter(graph); |
|
652 |
Adaptor adaptor(graph, node_filter, edge_filter); |
|
653 |
|
|
654 |
Graph::Node n1 = graph.addNode(); |
|
655 |
Graph::Node n2 = graph.addNode(); |
|
656 |
Graph::Node n3 = graph.addNode(); |
|
657 |
Graph::Node n4 = graph.addNode(); |
|
658 |
|
|
659 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
660 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
661 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
662 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
|
663 |
|
|
664 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = true; |
|
665 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = true; |
|
666 |
|
|
667 |
checkGraphNodeList(adaptor, 4); |
|
668 |
checkGraphArcList(adaptor, 8); |
|
669 |
checkGraphEdgeList(adaptor, 4); |
|
670 |
checkGraphConArcList(adaptor, 8); |
|
671 |
checkGraphConEdgeList(adaptor, 4); |
|
672 |
|
|
673 |
checkGraphOutArcList(adaptor, n1, 2); |
|
674 |
checkGraphOutArcList(adaptor, n2, 2); |
|
675 |
checkGraphOutArcList(adaptor, n3, 3); |
|
676 |
checkGraphOutArcList(adaptor, n4, 1); |
|
677 |
|
|
678 |
checkGraphInArcList(adaptor, n1, 2); |
|
679 |
checkGraphInArcList(adaptor, n2, 2); |
|
680 |
checkGraphInArcList(adaptor, n3, 3); |
|
681 |
checkGraphInArcList(adaptor, n4, 1); |
|
682 |
|
|
683 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
684 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
685 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
686 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
687 |
|
|
688 |
checkNodeIds(adaptor); |
|
689 |
checkArcIds(adaptor); |
|
690 |
checkEdgeIds(adaptor); |
|
691 |
|
|
692 |
checkGraphNodeMap(adaptor); |
|
693 |
checkGraphArcMap(adaptor); |
|
694 |
checkGraphEdgeMap(adaptor); |
|
695 |
|
|
696 |
edge_filter[e2] = false; |
|
697 |
|
|
698 |
checkGraphNodeList(adaptor, 4); |
|
699 |
checkGraphArcList(adaptor, 6); |
|
700 |
checkGraphEdgeList(adaptor, 3); |
|
701 |
checkGraphConArcList(adaptor, 6); |
|
702 |
checkGraphConEdgeList(adaptor, 3); |
|
703 |
|
|
704 |
checkGraphOutArcList(adaptor, n1, 1); |
|
705 |
checkGraphOutArcList(adaptor, n2, 2); |
|
706 |
checkGraphOutArcList(adaptor, n3, 2); |
|
707 |
checkGraphOutArcList(adaptor, n4, 1); |
|
708 |
|
|
709 |
checkGraphInArcList(adaptor, n1, 1); |
|
710 |
checkGraphInArcList(adaptor, n2, 2); |
|
711 |
checkGraphInArcList(adaptor, n3, 2); |
|
712 |
checkGraphInArcList(adaptor, n4, 1); |
|
713 |
|
|
714 |
checkGraphIncEdgeList(adaptor, n1, 1); |
|
715 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
716 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
717 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
718 |
|
|
719 |
checkNodeIds(adaptor); |
|
720 |
checkArcIds(adaptor); |
|
721 |
checkEdgeIds(adaptor); |
|
722 |
|
|
723 |
checkGraphNodeMap(adaptor); |
|
724 |
checkGraphArcMap(adaptor); |
|
725 |
checkGraphEdgeMap(adaptor); |
|
726 |
|
|
727 |
node_filter[n1] = false; |
|
728 |
|
|
729 |
checkGraphNodeList(adaptor, 3); |
|
730 |
checkGraphArcList(adaptor, 4); |
|
731 |
checkGraphEdgeList(adaptor, 2); |
|
732 |
checkGraphConArcList(adaptor, 4); |
|
733 |
checkGraphConEdgeList(adaptor, 2); |
|
734 |
|
|
735 |
checkGraphOutArcList(adaptor, n2, 1); |
|
736 |
checkGraphOutArcList(adaptor, n3, 2); |
|
737 |
checkGraphOutArcList(adaptor, n4, 1); |
|
738 |
|
|
739 |
checkGraphInArcList(adaptor, n2, 1); |
|
740 |
checkGraphInArcList(adaptor, n3, 2); |
|
741 |
checkGraphInArcList(adaptor, n4, 1); |
|
742 |
|
|
743 |
checkGraphIncEdgeList(adaptor, n2, 1); |
|
744 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
745 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
746 |
|
|
747 |
checkNodeIds(adaptor); |
|
748 |
checkArcIds(adaptor); |
|
749 |
checkEdgeIds(adaptor); |
|
750 |
|
|
751 |
checkGraphNodeMap(adaptor); |
|
752 |
checkGraphArcMap(adaptor); |
|
753 |
checkGraphEdgeMap(adaptor); |
|
754 |
|
|
755 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = false; |
|
756 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = false; |
|
757 |
|
|
758 |
checkGraphNodeList(adaptor, 0); |
|
759 |
checkGraphArcList(adaptor, 0); |
|
760 |
checkGraphEdgeList(adaptor, 0); |
|
761 |
checkGraphConArcList(adaptor, 0); |
|
762 |
checkGraphConEdgeList(adaptor, 0); |
|
763 |
|
|
764 |
checkNodeIds(adaptor); |
|
765 |
checkArcIds(adaptor); |
|
766 |
checkEdgeIds(adaptor); |
|
767 |
|
|
768 |
checkGraphNodeMap(adaptor); |
|
769 |
checkGraphArcMap(adaptor); |
|
770 |
checkGraphEdgeMap(adaptor); |
|
771 |
} |
|
772 |
|
|
773 |
void checkNodeSubGraphAdaptor() { |
|
774 |
checkConcept<concepts::Graph, |
|
775 |
NodeSubGraphAdaptor<concepts::Graph, |
|
776 |
concepts::Graph::NodeMap<bool> > >(); |
|
777 |
|
|
778 |
typedef ListGraph Graph; |
|
779 |
typedef Graph::NodeMap<bool> NodeFilter; |
|
780 |
typedef NodeSubGraphAdaptor<Graph, NodeFilter> Adaptor; |
|
781 |
|
|
782 |
Graph graph; |
|
783 |
NodeFilter node_filter(graph); |
|
784 |
Adaptor adaptor(graph, node_filter); |
|
785 |
|
|
786 |
Graph::Node n1 = graph.addNode(); |
|
787 |
Graph::Node n2 = graph.addNode(); |
|
788 |
Graph::Node n3 = graph.addNode(); |
|
789 |
Graph::Node n4 = graph.addNode(); |
|
790 |
|
|
791 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
792 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
793 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
794 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
|
795 |
|
|
796 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = true; |
|
797 |
|
|
798 |
checkGraphNodeList(adaptor, 4); |
|
799 |
checkGraphArcList(adaptor, 8); |
|
800 |
checkGraphEdgeList(adaptor, 4); |
|
801 |
checkGraphConArcList(adaptor, 8); |
|
802 |
checkGraphConEdgeList(adaptor, 4); |
|
803 |
|
|
804 |
checkGraphOutArcList(adaptor, n1, 2); |
|
805 |
checkGraphOutArcList(adaptor, n2, 2); |
|
806 |
checkGraphOutArcList(adaptor, n3, 3); |
|
807 |
checkGraphOutArcList(adaptor, n4, 1); |
|
808 |
|
|
809 |
checkGraphInArcList(adaptor, n1, 2); |
|
810 |
checkGraphInArcList(adaptor, n2, 2); |
|
811 |
checkGraphInArcList(adaptor, n3, 3); |
|
812 |
checkGraphInArcList(adaptor, n4, 1); |
|
813 |
|
|
814 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
815 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
816 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
817 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
818 |
|
|
819 |
checkNodeIds(adaptor); |
|
820 |
checkArcIds(adaptor); |
|
821 |
checkEdgeIds(adaptor); |
|
822 |
|
|
823 |
checkGraphNodeMap(adaptor); |
|
824 |
checkGraphArcMap(adaptor); |
|
825 |
checkGraphEdgeMap(adaptor); |
|
826 |
|
|
827 |
node_filter[n1] = false; |
|
828 |
|
|
829 |
checkGraphNodeList(adaptor, 3); |
|
830 |
checkGraphArcList(adaptor, 4); |
|
831 |
checkGraphEdgeList(adaptor, 2); |
|
832 |
checkGraphConArcList(adaptor, 4); |
|
833 |
checkGraphConEdgeList(adaptor, 2); |
|
834 |
|
|
835 |
checkGraphOutArcList(adaptor, n2, 1); |
|
836 |
checkGraphOutArcList(adaptor, n3, 2); |
|
837 |
checkGraphOutArcList(adaptor, n4, 1); |
|
838 |
|
|
839 |
checkGraphInArcList(adaptor, n2, 1); |
|
840 |
checkGraphInArcList(adaptor, n3, 2); |
|
841 |
checkGraphInArcList(adaptor, n4, 1); |
|
842 |
|
|
843 |
checkGraphIncEdgeList(adaptor, n2, 1); |
|
844 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
845 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
846 |
|
|
847 |
checkNodeIds(adaptor); |
|
848 |
checkArcIds(adaptor); |
|
849 |
checkEdgeIds(adaptor); |
|
850 |
|
|
851 |
checkGraphNodeMap(adaptor); |
|
852 |
checkGraphArcMap(adaptor); |
|
853 |
checkGraphEdgeMap(adaptor); |
|
854 |
|
|
855 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = false; |
|
856 |
|
|
857 |
checkGraphNodeList(adaptor, 0); |
|
858 |
checkGraphArcList(adaptor, 0); |
|
859 |
checkGraphEdgeList(adaptor, 0); |
|
860 |
checkGraphConArcList(adaptor, 0); |
|
861 |
checkGraphConEdgeList(adaptor, 0); |
|
862 |
|
|
863 |
checkNodeIds(adaptor); |
|
864 |
checkArcIds(adaptor); |
|
865 |
checkEdgeIds(adaptor); |
|
866 |
|
|
867 |
checkGraphNodeMap(adaptor); |
|
868 |
checkGraphArcMap(adaptor); |
|
869 |
checkGraphEdgeMap(adaptor); |
|
870 |
} |
|
871 |
|
|
872 |
void checkEdgeSubGraphAdaptor() { |
|
873 |
checkConcept<concepts::Graph, |
|
874 |
EdgeSubGraphAdaptor<concepts::Graph, |
|
875 |
concepts::Graph::EdgeMap<bool> > >(); |
|
876 |
|
|
877 |
typedef ListGraph Graph; |
|
878 |
typedef Graph::EdgeMap<bool> EdgeFilter; |
|
879 |
typedef EdgeSubGraphAdaptor<Graph, EdgeFilter> Adaptor; |
|
880 |
|
|
881 |
Graph graph; |
|
882 |
EdgeFilter edge_filter(graph); |
|
883 |
Adaptor adaptor(graph, edge_filter); |
|
884 |
|
|
885 |
Graph::Node n1 = graph.addNode(); |
|
886 |
Graph::Node n2 = graph.addNode(); |
|
887 |
Graph::Node n3 = graph.addNode(); |
|
888 |
Graph::Node n4 = graph.addNode(); |
|
889 |
|
|
890 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
891 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
892 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
893 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
|
894 |
|
|
895 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = true; |
|
896 |
|
|
897 |
checkGraphNodeList(adaptor, 4); |
|
898 |
checkGraphArcList(adaptor, 8); |
|
899 |
checkGraphEdgeList(adaptor, 4); |
|
900 |
checkGraphConArcList(adaptor, 8); |
|
901 |
checkGraphConEdgeList(adaptor, 4); |
|
902 |
|
|
903 |
checkGraphOutArcList(adaptor, n1, 2); |
|
904 |
checkGraphOutArcList(adaptor, n2, 2); |
|
905 |
checkGraphOutArcList(adaptor, n3, 3); |
|
906 |
checkGraphOutArcList(adaptor, n4, 1); |
|
907 |
|
|
908 |
checkGraphInArcList(adaptor, n1, 2); |
|
909 |
checkGraphInArcList(adaptor, n2, 2); |
|
910 |
checkGraphInArcList(adaptor, n3, 3); |
|
911 |
checkGraphInArcList(adaptor, n4, 1); |
|
912 |
|
|
913 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
914 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
915 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
916 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
917 |
|
|
918 |
checkNodeIds(adaptor); |
|
919 |
checkArcIds(adaptor); |
|
920 |
checkEdgeIds(adaptor); |
|
921 |
|
|
922 |
checkGraphNodeMap(adaptor); |
|
923 |
checkGraphArcMap(adaptor); |
|
924 |
checkGraphEdgeMap(adaptor); |
|
925 |
|
|
926 |
edge_filter[e2] = false; |
|
927 |
|
|
928 |
checkGraphNodeList(adaptor, 4); |
|
929 |
checkGraphArcList(adaptor, 6); |
|
930 |
checkGraphEdgeList(adaptor, 3); |
|
931 |
checkGraphConArcList(adaptor, 6); |
|
932 |
checkGraphConEdgeList(adaptor, 3); |
|
933 |
|
|
934 |
checkGraphOutArcList(adaptor, n1, 1); |
|
935 |
checkGraphOutArcList(adaptor, n2, 2); |
|
936 |
checkGraphOutArcList(adaptor, n3, 2); |
|
937 |
checkGraphOutArcList(adaptor, n4, 1); |
|
938 |
|
|
939 |
checkGraphInArcList(adaptor, n1, 1); |
|
940 |
checkGraphInArcList(adaptor, n2, 2); |
|
941 |
checkGraphInArcList(adaptor, n3, 2); |
|
942 |
checkGraphInArcList(adaptor, n4, 1); |
|
943 |
|
|
944 |
checkGraphIncEdgeList(adaptor, n1, 1); |
|
945 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
946 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
947 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
948 |
|
|
949 |
checkNodeIds(adaptor); |
|
950 |
checkArcIds(adaptor); |
|
951 |
checkEdgeIds(adaptor); |
|
952 |
|
|
953 |
checkGraphNodeMap(adaptor); |
|
954 |
checkGraphArcMap(adaptor); |
|
955 |
checkGraphEdgeMap(adaptor); |
|
956 |
|
|
957 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = false; |
|
958 |
|
|
959 |
checkGraphNodeList(adaptor, 4); |
|
960 |
checkGraphArcList(adaptor, 0); |
|
961 |
checkGraphEdgeList(adaptor, 0); |
|
962 |
checkGraphConArcList(adaptor, 0); |
|
963 |
checkGraphConEdgeList(adaptor, 0); |
|
964 |
|
|
965 |
checkNodeIds(adaptor); |
|
966 |
checkArcIds(adaptor); |
|
967 |
checkEdgeIds(adaptor); |
|
968 |
|
|
969 |
checkGraphNodeMap(adaptor); |
|
970 |
checkGraphArcMap(adaptor); |
|
971 |
checkGraphEdgeMap(adaptor); |
|
972 |
} |
|
973 |
|
|
974 |
void checkDirGraphAdaptor() { |
|
975 |
checkConcept<concepts::Digraph, |
|
976 |
DirGraphAdaptor<concepts::Graph, concepts::Graph::EdgeMap<bool> > >(); |
|
977 |
|
|
978 |
typedef ListGraph Graph; |
|
979 |
typedef ListGraph::EdgeMap<bool> DirMap; |
|
980 |
typedef DirGraphAdaptor<Graph> Adaptor; |
|
981 |
|
|
982 |
Graph graph; |
|
983 |
DirMap dir(graph, true); |
|
984 |
Adaptor adaptor(graph, dir); |
|
985 |
|
|
986 |
Graph::Node n1 = graph.addNode(); |
|
987 |
Graph::Node n2 = graph.addNode(); |
|
988 |
Graph::Node n3 = graph.addNode(); |
|
989 |
|
|
990 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
991 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
992 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
993 |
|
|
994 |
checkGraphNodeList(adaptor, 3); |
|
995 |
checkGraphArcList(adaptor, 3); |
|
996 |
checkGraphConArcList(adaptor, 3); |
|
997 |
|
|
998 |
{ |
|
999 |
dir[e1] = true; |
|
1000 |
Adaptor::Node u = adaptor.source(e1); |
|
1001 |
Adaptor::Node v = adaptor.target(e1); |
|
1002 |
|
|
1003 |
dir[e1] = false; |
|
1004 |
check (u == adaptor.target(e1), "Wrong dir"); |
|
1005 |
check (v == adaptor.source(e1), "Wrong dir"); |
|
1006 |
|
|
1007 |
check ((u == n1 && v == n2) || (u == n2 && v == n1), "Wrong dir"); |
|
1008 |
dir[e1] = n1 == u; |
|
1009 |
} |
|
1010 |
|
|
1011 |
{ |
|
1012 |
dir[e2] = true; |
|
1013 |
Adaptor::Node u = adaptor.source(e2); |
|
1014 |
Adaptor::Node v = adaptor.target(e2); |
|
1015 |
|
|
1016 |
dir[e2] = false; |
|
1017 |
check (u == adaptor.target(e2), "Wrong dir"); |
|
1018 |
check (v == adaptor.source(e2), "Wrong dir"); |
|
1019 |
|
|
1020 |
check ((u == n1 && v == n3) || (u == n3 && v == n1), "Wrong dir"); |
|
1021 |
dir[e2] = n3 == u; |
|
1022 |
} |
|
1023 |
|
|
1024 |
{ |
|
1025 |
dir[e3] = true; |
|
1026 |
Adaptor::Node u = adaptor.source(e3); |
|
1027 |
Adaptor::Node v = adaptor.target(e3); |
|
1028 |
|
|
1029 |
dir[e3] = false; |
|
1030 |
check (u == adaptor.target(e3), "Wrong dir"); |
|
1031 |
check (v == adaptor.source(e3), "Wrong dir"); |
|
1032 |
|
|
1033 |
check ((u == n2 && v == n3) || (u == n3 && v == n2), "Wrong dir"); |
|
1034 |
dir[e3] = n2 == u; |
|
1035 |
} |
|
1036 |
|
|
1037 |
checkGraphOutArcList(adaptor, n1, 1); |
|
1038 |
checkGraphOutArcList(adaptor, n2, 1); |
|
1039 |
checkGraphOutArcList(adaptor, n3, 1); |
|
1040 |
|
|
1041 |
checkGraphInArcList(adaptor, n1, 1); |
|
1042 |
checkGraphInArcList(adaptor, n2, 1); |
|
1043 |
checkGraphInArcList(adaptor, n3, 1); |
|
1044 |
|
|
1045 |
checkNodeIds(adaptor); |
|
1046 |
checkArcIds(adaptor); |
|
1047 |
|
|
1048 |
checkGraphNodeMap(adaptor); |
|
1049 |
checkGraphArcMap(adaptor); |
|
1050 |
|
|
1051 |
} |
|
1052 |
|
|
1053 |
|
|
1054 |
int main(int, const char **) { |
|
1055 |
|
|
1056 |
checkDigraphAdaptor(); |
|
1057 |
checkRevDigraphAdaptor(); |
|
1058 |
checkSubDigraphAdaptor(); |
|
1059 |
checkNodeSubDigraphAdaptor(); |
|
1060 |
checkArcSubDigraphAdaptor(); |
|
1061 |
checkUndirDigraphAdaptor(); |
|
1062 |
checkResDigraphAdaptor(); |
|
1063 |
checkSplitDigraphAdaptor(); |
|
1064 |
|
|
1065 |
checkGraphAdaptor(); |
|
1066 |
checkSubGraphAdaptor(); |
|
1067 |
checkNodeSubGraphAdaptor(); |
|
1068 |
checkEdgeSubGraphAdaptor(); |
|
1069 |
checkDirGraphAdaptor(); |
|
1070 |
|
|
1071 |
return 0; |
|
1072 |
} |
... | ... |
@@ -58,10 +58,12 @@ |
58 | 58 |
lemon/bits/bezier.h \ |
59 | 59 |
lemon/bits/default_map.h \ |
60 | 60 |
lemon/bits/enable_if.h \ |
61 |
lemon/bits/graph_adaptor_extender.h \ |
|
61 | 62 |
lemon/bits/graph_extender.h \ |
62 | 63 |
lemon/bits/map_extender.h \ |
63 | 64 |
lemon/bits/path_dump.h \ |
64 | 65 |
lemon/bits/traits.h \ |
66 |
lemon/bits/variant.h \ |
|
65 | 67 |
lemon/bits/vector_map.h |
66 | 68 |
|
67 | 69 |
concept_HEADERS += \ |
... | ... |
@@ -15,6 +15,7 @@ |
15 | 15 |
test/dijkstra_test \ |
16 | 16 |
test/dim_test \ |
17 | 17 |
test/error_test \ |
18 |
test/graph_adaptor_test \ |
|
18 | 19 |
test/graph_copy_test \ |
19 | 20 |
test/graph_test \ |
20 | 21 |
test/graph_utils_test \ |
... | ... |
@@ -41,6 +42,7 @@ |
41 | 42 |
test_dijkstra_test_SOURCES = test/dijkstra_test.cc |
42 | 43 |
test_dim_test_SOURCES = test/dim_test.cc |
43 | 44 |
test_error_test_SOURCES = test/error_test.cc |
45 |
test_graph_adaptor_test_SOURCES = test/graph_adaptor_test.cc |
|
44 | 46 |
test_graph_copy_test_SOURCES = test/graph_copy_test.cc |
45 | 47 |
test_graph_test_SOURCES = test/graph_test.cc |
46 | 48 |
test_graph_utils_test_SOURCES = test/graph_utils_test.cc |
0 comments (0 inline)