1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2009 |
---|
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_ADAPTORS_H |
---|
20 | #define LEMON_ADAPTORS_H |
---|
21 | |
---|
22 | /// \ingroup graph_adaptors |
---|
23 | /// \file |
---|
24 | /// \brief Adaptor classes for digraphs and graphs |
---|
25 | /// |
---|
26 | /// This file contains several useful adaptors for digraphs and graphs. |
---|
27 | |
---|
28 | #include <lemon/core.h> |
---|
29 | #include <lemon/maps.h> |
---|
30 | #include <lemon/bits/variant.h> |
---|
31 | |
---|
32 | #include <lemon/bits/graph_adaptor_extender.h> |
---|
33 | #include <lemon/tolerance.h> |
---|
34 | |
---|
35 | #include <algorithm> |
---|
36 | |
---|
37 | namespace lemon { |
---|
38 | |
---|
39 | #ifdef _MSC_VER |
---|
40 | #define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
---|
41 | #else |
---|
42 | #define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
---|
43 | #endif |
---|
44 | |
---|
45 | template<typename DGR> |
---|
46 | class DigraphAdaptorBase { |
---|
47 | public: |
---|
48 | typedef DGR Digraph; |
---|
49 | typedef DigraphAdaptorBase Adaptor; |
---|
50 | |
---|
51 | protected: |
---|
52 | DGR* _digraph; |
---|
53 | DigraphAdaptorBase() : _digraph(0) { } |
---|
54 | void initialize(DGR& digraph) { _digraph = &digraph; } |
---|
55 | |
---|
56 | public: |
---|
57 | DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { } |
---|
58 | |
---|
59 | typedef typename DGR::Node Node; |
---|
60 | typedef typename DGR::Arc Arc; |
---|
61 | |
---|
62 | void first(Node& i) const { _digraph->first(i); } |
---|
63 | void first(Arc& i) const { _digraph->first(i); } |
---|
64 | void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
---|
65 | void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
---|
66 | |
---|
67 | void next(Node& i) const { _digraph->next(i); } |
---|
68 | void next(Arc& i) const { _digraph->next(i); } |
---|
69 | void nextIn(Arc& i) const { _digraph->nextIn(i); } |
---|
70 | void nextOut(Arc& i) const { _digraph->nextOut(i); } |
---|
71 | |
---|
72 | Node source(const Arc& a) const { return _digraph->source(a); } |
---|
73 | Node target(const Arc& a) const { return _digraph->target(a); } |
---|
74 | |
---|
75 | typedef NodeNumTagIndicator<DGR> NodeNumTag; |
---|
76 | int nodeNum() const { return _digraph->nodeNum(); } |
---|
77 | |
---|
78 | typedef ArcNumTagIndicator<DGR> ArcNumTag; |
---|
79 | int arcNum() const { return _digraph->arcNum(); } |
---|
80 | |
---|
81 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
82 | Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const { |
---|
83 | return _digraph->findArc(u, v, prev); |
---|
84 | } |
---|
85 | |
---|
86 | Node addNode() { return _digraph->addNode(); } |
---|
87 | Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
---|
88 | |
---|
89 | void erase(const Node& n) { _digraph->erase(n); } |
---|
90 | void erase(const Arc& a) { _digraph->erase(a); } |
---|
91 | |
---|
92 | void clear() { _digraph->clear(); } |
---|
93 | |
---|
94 | int id(const Node& n) const { return _digraph->id(n); } |
---|
95 | int id(const Arc& a) const { return _digraph->id(a); } |
---|
96 | |
---|
97 | Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
---|
98 | Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
---|
99 | |
---|
100 | int maxNodeId() const { return _digraph->maxNodeId(); } |
---|
101 | int maxArcId() const { return _digraph->maxArcId(); } |
---|
102 | |
---|
103 | typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
---|
104 | NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
---|
105 | |
---|
106 | typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
---|
107 | ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
---|
108 | |
---|
109 | template <typename V> |
---|
110 | class NodeMap : public DGR::template NodeMap<V> { |
---|
111 | public: |
---|
112 | |
---|
113 | typedef typename DGR::template NodeMap<V> Parent; |
---|
114 | |
---|
115 | explicit NodeMap(const Adaptor& adaptor) |
---|
116 | : Parent(*adaptor._digraph) {} |
---|
117 | |
---|
118 | NodeMap(const Adaptor& adaptor, const V& value) |
---|
119 | : Parent(*adaptor._digraph, value) { } |
---|
120 | |
---|
121 | private: |
---|
122 | NodeMap& operator=(const NodeMap& cmap) { |
---|
123 | return operator=<NodeMap>(cmap); |
---|
124 | } |
---|
125 | |
---|
126 | template <typename CMap> |
---|
127 | NodeMap& operator=(const CMap& cmap) { |
---|
128 | Parent::operator=(cmap); |
---|
129 | return *this; |
---|
130 | } |
---|
131 | |
---|
132 | }; |
---|
133 | |
---|
134 | template <typename V> |
---|
135 | class ArcMap : public DGR::template ArcMap<V> { |
---|
136 | public: |
---|
137 | |
---|
138 | typedef typename DGR::template ArcMap<V> Parent; |
---|
139 | |
---|
140 | explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
---|
141 | : Parent(*adaptor._digraph) {} |
---|
142 | |
---|
143 | ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
---|
144 | : Parent(*adaptor._digraph, value) {} |
---|
145 | |
---|
146 | private: |
---|
147 | ArcMap& operator=(const ArcMap& cmap) { |
---|
148 | return operator=<ArcMap>(cmap); |
---|
149 | } |
---|
150 | |
---|
151 | template <typename CMap> |
---|
152 | ArcMap& operator=(const CMap& cmap) { |
---|
153 | Parent::operator=(cmap); |
---|
154 | return *this; |
---|
155 | } |
---|
156 | |
---|
157 | }; |
---|
158 | |
---|
159 | }; |
---|
160 | |
---|
161 | template<typename GR> |
---|
162 | class GraphAdaptorBase { |
---|
163 | public: |
---|
164 | typedef GR Graph; |
---|
165 | |
---|
166 | protected: |
---|
167 | GR* _graph; |
---|
168 | |
---|
169 | GraphAdaptorBase() : _graph(0) {} |
---|
170 | |
---|
171 | void initialize(GR& graph) { _graph = &graph; } |
---|
172 | |
---|
173 | public: |
---|
174 | GraphAdaptorBase(GR& graph) : _graph(&graph) {} |
---|
175 | |
---|
176 | typedef typename GR::Node Node; |
---|
177 | typedef typename GR::Arc Arc; |
---|
178 | typedef typename GR::Edge Edge; |
---|
179 | |
---|
180 | void first(Node& i) const { _graph->first(i); } |
---|
181 | void first(Arc& i) const { _graph->first(i); } |
---|
182 | void first(Edge& i) const { _graph->first(i); } |
---|
183 | void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
---|
184 | void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
---|
185 | void firstInc(Edge &i, bool &d, const Node &n) const { |
---|
186 | _graph->firstInc(i, d, n); |
---|
187 | } |
---|
188 | |
---|
189 | void next(Node& i) const { _graph->next(i); } |
---|
190 | void next(Arc& i) const { _graph->next(i); } |
---|
191 | void next(Edge& i) const { _graph->next(i); } |
---|
192 | void nextIn(Arc& i) const { _graph->nextIn(i); } |
---|
193 | void nextOut(Arc& i) const { _graph->nextOut(i); } |
---|
194 | void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
---|
195 | |
---|
196 | Node u(const Edge& e) const { return _graph->u(e); } |
---|
197 | Node v(const Edge& e) const { return _graph->v(e); } |
---|
198 | |
---|
199 | Node source(const Arc& a) const { return _graph->source(a); } |
---|
200 | Node target(const Arc& a) const { return _graph->target(a); } |
---|
201 | |
---|
202 | typedef NodeNumTagIndicator<Graph> NodeNumTag; |
---|
203 | int nodeNum() const { return _graph->nodeNum(); } |
---|
204 | |
---|
205 | typedef ArcNumTagIndicator<Graph> ArcNumTag; |
---|
206 | int arcNum() const { return _graph->arcNum(); } |
---|
207 | |
---|
208 | typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
---|
209 | int edgeNum() const { return _graph->edgeNum(); } |
---|
210 | |
---|
211 | typedef FindArcTagIndicator<Graph> FindArcTag; |
---|
212 | Arc findArc(const Node& u, const Node& v, |
---|
213 | const Arc& prev = INVALID) const { |
---|
214 | return _graph->findArc(u, v, prev); |
---|
215 | } |
---|
216 | |
---|
217 | typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
---|
218 | Edge findEdge(const Node& u, const Node& v, |
---|
219 | const Edge& prev = INVALID) const { |
---|
220 | return _graph->findEdge(u, v, prev); |
---|
221 | } |
---|
222 | |
---|
223 | Node addNode() { return _graph->addNode(); } |
---|
224 | Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
---|
225 | |
---|
226 | void erase(const Node& i) { _graph->erase(i); } |
---|
227 | void erase(const Edge& i) { _graph->erase(i); } |
---|
228 | |
---|
229 | void clear() { _graph->clear(); } |
---|
230 | |
---|
231 | bool direction(const Arc& a) const { return _graph->direction(a); } |
---|
232 | Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
---|
233 | |
---|
234 | int id(const Node& v) const { return _graph->id(v); } |
---|
235 | int id(const Arc& a) const { return _graph->id(a); } |
---|
236 | int id(const Edge& e) const { return _graph->id(e); } |
---|
237 | |
---|
238 | Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
---|
239 | Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
---|
240 | Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
---|
241 | |
---|
242 | int maxNodeId() const { return _graph->maxNodeId(); } |
---|
243 | int maxArcId() const { return _graph->maxArcId(); } |
---|
244 | int maxEdgeId() const { return _graph->maxEdgeId(); } |
---|
245 | |
---|
246 | typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
---|
247 | NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
---|
248 | |
---|
249 | typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
---|
250 | ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
---|
251 | |
---|
252 | typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier; |
---|
253 | EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
---|
254 | |
---|
255 | template <typename V> |
---|
256 | class NodeMap : public GR::template NodeMap<V> { |
---|
257 | public: |
---|
258 | typedef typename GR::template NodeMap<V> Parent; |
---|
259 | explicit NodeMap(const GraphAdaptorBase<GR>& adapter) |
---|
260 | : Parent(*adapter._graph) {} |
---|
261 | NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
---|
262 | : Parent(*adapter._graph, value) {} |
---|
263 | |
---|
264 | private: |
---|
265 | NodeMap& operator=(const NodeMap& cmap) { |
---|
266 | return operator=<NodeMap>(cmap); |
---|
267 | } |
---|
268 | |
---|
269 | template <typename CMap> |
---|
270 | NodeMap& operator=(const CMap& cmap) { |
---|
271 | Parent::operator=(cmap); |
---|
272 | return *this; |
---|
273 | } |
---|
274 | |
---|
275 | }; |
---|
276 | |
---|
277 | template <typename V> |
---|
278 | class ArcMap : public GR::template ArcMap<V> { |
---|
279 | public: |
---|
280 | typedef typename GR::template ArcMap<V> Parent; |
---|
281 | explicit ArcMap(const GraphAdaptorBase<GR>& adapter) |
---|
282 | : Parent(*adapter._graph) {} |
---|
283 | ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
---|
284 | : Parent(*adapter._graph, value) {} |
---|
285 | |
---|
286 | private: |
---|
287 | ArcMap& operator=(const ArcMap& cmap) { |
---|
288 | return operator=<ArcMap>(cmap); |
---|
289 | } |
---|
290 | |
---|
291 | template <typename CMap> |
---|
292 | ArcMap& operator=(const CMap& cmap) { |
---|
293 | Parent::operator=(cmap); |
---|
294 | return *this; |
---|
295 | } |
---|
296 | }; |
---|
297 | |
---|
298 | template <typename V> |
---|
299 | class EdgeMap : public GR::template EdgeMap<V> { |
---|
300 | public: |
---|
301 | typedef typename GR::template EdgeMap<V> Parent; |
---|
302 | explicit EdgeMap(const GraphAdaptorBase<GR>& adapter) |
---|
303 | : Parent(*adapter._graph) {} |
---|
304 | EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
---|
305 | : Parent(*adapter._graph, value) {} |
---|
306 | |
---|
307 | private: |
---|
308 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
309 | return operator=<EdgeMap>(cmap); |
---|
310 | } |
---|
311 | |
---|
312 | template <typename CMap> |
---|
313 | EdgeMap& operator=(const CMap& cmap) { |
---|
314 | Parent::operator=(cmap); |
---|
315 | return *this; |
---|
316 | } |
---|
317 | }; |
---|
318 | |
---|
319 | }; |
---|
320 | |
---|
321 | template <typename DGR> |
---|
322 | class ReverseDigraphBase : public DigraphAdaptorBase<DGR> { |
---|
323 | public: |
---|
324 | typedef DGR Digraph; |
---|
325 | typedef DigraphAdaptorBase<DGR> Parent; |
---|
326 | protected: |
---|
327 | ReverseDigraphBase() : Parent() { } |
---|
328 | public: |
---|
329 | typedef typename Parent::Node Node; |
---|
330 | typedef typename Parent::Arc Arc; |
---|
331 | |
---|
332 | void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } |
---|
333 | void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } |
---|
334 | |
---|
335 | void nextIn(Arc& a) const { Parent::nextOut(a); } |
---|
336 | void nextOut(Arc& a) const { Parent::nextIn(a); } |
---|
337 | |
---|
338 | Node source(const Arc& a) const { return Parent::target(a); } |
---|
339 | Node target(const Arc& a) const { return Parent::source(a); } |
---|
340 | |
---|
341 | Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); } |
---|
342 | |
---|
343 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
344 | Arc findArc(const Node& u, const Node& v, |
---|
345 | const Arc& prev = INVALID) const { |
---|
346 | return Parent::findArc(v, u, prev); |
---|
347 | } |
---|
348 | |
---|
349 | }; |
---|
350 | |
---|
351 | /// \ingroup graph_adaptors |
---|
352 | /// |
---|
353 | /// \brief Adaptor class for reversing the orientation of the arcs in |
---|
354 | /// a digraph. |
---|
355 | /// |
---|
356 | /// ReverseDigraph can be used for reversing the arcs in a digraph. |
---|
357 | /// It conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
358 | /// |
---|
359 | /// The adapted digraph can also be modified through this adaptor |
---|
360 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
361 | /// parameter is set to be \c const. |
---|
362 | /// |
---|
363 | /// \tparam DGR The type of the adapted digraph. |
---|
364 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
365 | /// It can also be specified to be \c const. |
---|
366 | /// |
---|
367 | /// \note The \c Node and \c Arc types of this adaptor and the adapted |
---|
368 | /// digraph are convertible to each other. |
---|
369 | template<typename DGR> |
---|
370 | #ifdef DOXYGEN |
---|
371 | class ReverseDigraph { |
---|
372 | #else |
---|
373 | class ReverseDigraph : |
---|
374 | public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > { |
---|
375 | #endif |
---|
376 | public: |
---|
377 | /// The type of the adapted digraph. |
---|
378 | typedef DGR Digraph; |
---|
379 | typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent; |
---|
380 | protected: |
---|
381 | ReverseDigraph() { } |
---|
382 | public: |
---|
383 | |
---|
384 | /// \brief Constructor |
---|
385 | /// |
---|
386 | /// Creates a reverse digraph adaptor for the given digraph. |
---|
387 | explicit ReverseDigraph(DGR& digraph) { |
---|
388 | Parent::initialize(digraph); |
---|
389 | } |
---|
390 | }; |
---|
391 | |
---|
392 | /// \brief Returns a read-only ReverseDigraph adaptor |
---|
393 | /// |
---|
394 | /// This function just returns a read-only \ref ReverseDigraph adaptor. |
---|
395 | /// \ingroup graph_adaptors |
---|
396 | /// \relates ReverseDigraph |
---|
397 | template<typename DGR> |
---|
398 | ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) { |
---|
399 | return ReverseDigraph<const DGR>(digraph); |
---|
400 | } |
---|
401 | |
---|
402 | |
---|
403 | template <typename DGR, typename NF, typename AF, bool ch = true> |
---|
404 | class SubDigraphBase : public DigraphAdaptorBase<DGR> { |
---|
405 | public: |
---|
406 | typedef DGR Digraph; |
---|
407 | typedef NF NodeFilterMap; |
---|
408 | typedef AF ArcFilterMap; |
---|
409 | |
---|
410 | typedef SubDigraphBase Adaptor; |
---|
411 | typedef DigraphAdaptorBase<DGR> Parent; |
---|
412 | protected: |
---|
413 | NF* _node_filter; |
---|
414 | AF* _arc_filter; |
---|
415 | SubDigraphBase() |
---|
416 | : Parent(), _node_filter(0), _arc_filter(0) { } |
---|
417 | |
---|
418 | void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) { |
---|
419 | Parent::initialize(digraph); |
---|
420 | _node_filter = &node_filter; |
---|
421 | _arc_filter = &arc_filter; |
---|
422 | } |
---|
423 | |
---|
424 | public: |
---|
425 | |
---|
426 | typedef typename Parent::Node Node; |
---|
427 | typedef typename Parent::Arc Arc; |
---|
428 | |
---|
429 | void first(Node& i) const { |
---|
430 | Parent::first(i); |
---|
431 | while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
432 | } |
---|
433 | |
---|
434 | void first(Arc& i) const { |
---|
435 | Parent::first(i); |
---|
436 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
437 | || !(*_node_filter)[Parent::source(i)] |
---|
438 | || !(*_node_filter)[Parent::target(i)])) |
---|
439 | Parent::next(i); |
---|
440 | } |
---|
441 | |
---|
442 | void firstIn(Arc& i, const Node& n) const { |
---|
443 | Parent::firstIn(i, n); |
---|
444 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
445 | || !(*_node_filter)[Parent::source(i)])) |
---|
446 | Parent::nextIn(i); |
---|
447 | } |
---|
448 | |
---|
449 | void firstOut(Arc& i, const Node& n) const { |
---|
450 | Parent::firstOut(i, n); |
---|
451 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
452 | || !(*_node_filter)[Parent::target(i)])) |
---|
453 | Parent::nextOut(i); |
---|
454 | } |
---|
455 | |
---|
456 | void next(Node& i) const { |
---|
457 | Parent::next(i); |
---|
458 | while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
459 | } |
---|
460 | |
---|
461 | void next(Arc& i) const { |
---|
462 | Parent::next(i); |
---|
463 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
464 | || !(*_node_filter)[Parent::source(i)] |
---|
465 | || !(*_node_filter)[Parent::target(i)])) |
---|
466 | Parent::next(i); |
---|
467 | } |
---|
468 | |
---|
469 | void nextIn(Arc& i) const { |
---|
470 | Parent::nextIn(i); |
---|
471 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
472 | || !(*_node_filter)[Parent::source(i)])) |
---|
473 | Parent::nextIn(i); |
---|
474 | } |
---|
475 | |
---|
476 | void nextOut(Arc& i) const { |
---|
477 | Parent::nextOut(i); |
---|
478 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
479 | || !(*_node_filter)[Parent::target(i)])) |
---|
480 | Parent::nextOut(i); |
---|
481 | } |
---|
482 | |
---|
483 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
484 | void status(const Arc& a, bool v) const { _arc_filter->set(a, v); } |
---|
485 | |
---|
486 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
487 | bool status(const Arc& a) const { return (*_arc_filter)[a]; } |
---|
488 | |
---|
489 | typedef False NodeNumTag; |
---|
490 | typedef False ArcNumTag; |
---|
491 | |
---|
492 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
493 | Arc findArc(const Node& source, const Node& target, |
---|
494 | const Arc& prev = INVALID) const { |
---|
495 | if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
---|
496 | return INVALID; |
---|
497 | } |
---|
498 | Arc arc = Parent::findArc(source, target, prev); |
---|
499 | while (arc != INVALID && !(*_arc_filter)[arc]) { |
---|
500 | arc = Parent::findArc(source, target, arc); |
---|
501 | } |
---|
502 | return arc; |
---|
503 | } |
---|
504 | |
---|
505 | public: |
---|
506 | |
---|
507 | template <typename V> |
---|
508 | class NodeMap |
---|
509 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
510 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
---|
511 | public: |
---|
512 | typedef V Value; |
---|
513 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
514 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
---|
515 | |
---|
516 | NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
---|
517 | : Parent(adaptor) {} |
---|
518 | NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
---|
519 | : Parent(adaptor, value) {} |
---|
520 | |
---|
521 | private: |
---|
522 | NodeMap& operator=(const NodeMap& cmap) { |
---|
523 | return operator=<NodeMap>(cmap); |
---|
524 | } |
---|
525 | |
---|
526 | template <typename CMap> |
---|
527 | NodeMap& operator=(const CMap& cmap) { |
---|
528 | Parent::operator=(cmap); |
---|
529 | return *this; |
---|
530 | } |
---|
531 | }; |
---|
532 | |
---|
533 | template <typename V> |
---|
534 | class ArcMap |
---|
535 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
536 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
---|
537 | public: |
---|
538 | typedef V Value; |
---|
539 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
540 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
---|
541 | |
---|
542 | ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
---|
543 | : Parent(adaptor) {} |
---|
544 | ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
---|
545 | : Parent(adaptor, value) {} |
---|
546 | |
---|
547 | private: |
---|
548 | ArcMap& operator=(const ArcMap& cmap) { |
---|
549 | return operator=<ArcMap>(cmap); |
---|
550 | } |
---|
551 | |
---|
552 | template <typename CMap> |
---|
553 | ArcMap& operator=(const CMap& cmap) { |
---|
554 | Parent::operator=(cmap); |
---|
555 | return *this; |
---|
556 | } |
---|
557 | }; |
---|
558 | |
---|
559 | }; |
---|
560 | |
---|
561 | template <typename DGR, typename NF, typename AF> |
---|
562 | class SubDigraphBase<DGR, NF, AF, false> |
---|
563 | : public DigraphAdaptorBase<DGR> { |
---|
564 | public: |
---|
565 | typedef DGR Digraph; |
---|
566 | typedef NF NodeFilterMap; |
---|
567 | typedef AF ArcFilterMap; |
---|
568 | |
---|
569 | typedef SubDigraphBase Adaptor; |
---|
570 | typedef DigraphAdaptorBase<Digraph> Parent; |
---|
571 | protected: |
---|
572 | NF* _node_filter; |
---|
573 | AF* _arc_filter; |
---|
574 | SubDigraphBase() |
---|
575 | : Parent(), _node_filter(0), _arc_filter(0) { } |
---|
576 | |
---|
577 | void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) { |
---|
578 | Parent::initialize(digraph); |
---|
579 | _node_filter = &node_filter; |
---|
580 | _arc_filter = &arc_filter; |
---|
581 | } |
---|
582 | |
---|
583 | public: |
---|
584 | |
---|
585 | typedef typename Parent::Node Node; |
---|
586 | typedef typename Parent::Arc Arc; |
---|
587 | |
---|
588 | void first(Node& i) const { |
---|
589 | Parent::first(i); |
---|
590 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
591 | } |
---|
592 | |
---|
593 | void first(Arc& i) const { |
---|
594 | Parent::first(i); |
---|
595 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
---|
596 | } |
---|
597 | |
---|
598 | void firstIn(Arc& i, const Node& n) const { |
---|
599 | Parent::firstIn(i, n); |
---|
600 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
---|
601 | } |
---|
602 | |
---|
603 | void firstOut(Arc& i, const Node& n) const { |
---|
604 | Parent::firstOut(i, n); |
---|
605 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
---|
606 | } |
---|
607 | |
---|
608 | void next(Node& i) const { |
---|
609 | Parent::next(i); |
---|
610 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
611 | } |
---|
612 | void next(Arc& i) const { |
---|
613 | Parent::next(i); |
---|
614 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
---|
615 | } |
---|
616 | void nextIn(Arc& i) const { |
---|
617 | Parent::nextIn(i); |
---|
618 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
---|
619 | } |
---|
620 | |
---|
621 | void nextOut(Arc& i) const { |
---|
622 | Parent::nextOut(i); |
---|
623 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
---|
624 | } |
---|
625 | |
---|
626 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
627 | void status(const Arc& a, bool v) const { _arc_filter->set(a, v); } |
---|
628 | |
---|
629 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
630 | bool status(const Arc& a) const { return (*_arc_filter)[a]; } |
---|
631 | |
---|
632 | typedef False NodeNumTag; |
---|
633 | typedef False ArcNumTag; |
---|
634 | |
---|
635 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
636 | Arc findArc(const Node& source, const Node& target, |
---|
637 | const Arc& prev = INVALID) const { |
---|
638 | if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
---|
639 | return INVALID; |
---|
640 | } |
---|
641 | Arc arc = Parent::findArc(source, target, prev); |
---|
642 | while (arc != INVALID && !(*_arc_filter)[arc]) { |
---|
643 | arc = Parent::findArc(source, target, arc); |
---|
644 | } |
---|
645 | return arc; |
---|
646 | } |
---|
647 | |
---|
648 | template <typename V> |
---|
649 | class NodeMap |
---|
650 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
651 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
---|
652 | public: |
---|
653 | typedef V Value; |
---|
654 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
655 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
---|
656 | |
---|
657 | NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
---|
658 | : Parent(adaptor) {} |
---|
659 | NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
---|
660 | : Parent(adaptor, value) {} |
---|
661 | |
---|
662 | private: |
---|
663 | NodeMap& operator=(const NodeMap& cmap) { |
---|
664 | return operator=<NodeMap>(cmap); |
---|
665 | } |
---|
666 | |
---|
667 | template <typename CMap> |
---|
668 | NodeMap& operator=(const CMap& cmap) { |
---|
669 | Parent::operator=(cmap); |
---|
670 | return *this; |
---|
671 | } |
---|
672 | }; |
---|
673 | |
---|
674 | template <typename V> |
---|
675 | class ArcMap |
---|
676 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
677 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
---|
678 | public: |
---|
679 | typedef V Value; |
---|
680 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
681 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
---|
682 | |
---|
683 | ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
---|
684 | : Parent(adaptor) {} |
---|
685 | ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
---|
686 | : Parent(adaptor, value) {} |
---|
687 | |
---|
688 | private: |
---|
689 | ArcMap& operator=(const ArcMap& cmap) { |
---|
690 | return operator=<ArcMap>(cmap); |
---|
691 | } |
---|
692 | |
---|
693 | template <typename CMap> |
---|
694 | ArcMap& operator=(const CMap& cmap) { |
---|
695 | Parent::operator=(cmap); |
---|
696 | return *this; |
---|
697 | } |
---|
698 | }; |
---|
699 | |
---|
700 | }; |
---|
701 | |
---|
702 | /// \ingroup graph_adaptors |
---|
703 | /// |
---|
704 | /// \brief Adaptor class for hiding nodes and arcs in a digraph |
---|
705 | /// |
---|
706 | /// SubDigraph can be used for hiding nodes and arcs in a digraph. |
---|
707 | /// A \c bool node map and a \c bool arc map must be specified, which |
---|
708 | /// define the filters for nodes and arcs. |
---|
709 | /// Only the nodes and arcs with \c true filter value are |
---|
710 | /// shown in the subdigraph. The arcs that are incident to hidden |
---|
711 | /// nodes are also filtered out. |
---|
712 | /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
713 | /// |
---|
714 | /// The adapted digraph can also be modified through this adaptor |
---|
715 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
716 | /// parameter is set to be \c const. |
---|
717 | /// |
---|
718 | /// \tparam DGR The type of the adapted digraph. |
---|
719 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
720 | /// It can also be specified to be \c const. |
---|
721 | /// \tparam NF The type of the node filter map. |
---|
722 | /// It must be a \c bool (or convertible) node map of the |
---|
723 | /// adapted digraph. The default type is |
---|
724 | /// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
---|
725 | /// \tparam AF The type of the arc filter map. |
---|
726 | /// It must be \c bool (or convertible) arc map of the |
---|
727 | /// adapted digraph. The default type is |
---|
728 | /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
---|
729 | /// |
---|
730 | /// \note The \c Node and \c Arc types of this adaptor and the adapted |
---|
731 | /// digraph are convertible to each other. |
---|
732 | /// |
---|
733 | /// \see FilterNodes |
---|
734 | /// \see FilterArcs |
---|
735 | #ifdef DOXYGEN |
---|
736 | template<typename DGR, typename NF, typename AF> |
---|
737 | class SubDigraph { |
---|
738 | #else |
---|
739 | template<typename DGR, |
---|
740 | typename NF = typename DGR::template NodeMap<bool>, |
---|
741 | typename AF = typename DGR::template ArcMap<bool> > |
---|
742 | class SubDigraph : |
---|
743 | public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > { |
---|
744 | #endif |
---|
745 | public: |
---|
746 | /// The type of the adapted digraph. |
---|
747 | typedef DGR Digraph; |
---|
748 | /// The type of the node filter map. |
---|
749 | typedef NF NodeFilterMap; |
---|
750 | /// The type of the arc filter map. |
---|
751 | typedef AF ArcFilterMap; |
---|
752 | |
---|
753 | typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
---|
754 | Parent; |
---|
755 | |
---|
756 | typedef typename Parent::Node Node; |
---|
757 | typedef typename Parent::Arc Arc; |
---|
758 | |
---|
759 | protected: |
---|
760 | SubDigraph() { } |
---|
761 | public: |
---|
762 | |
---|
763 | /// \brief Constructor |
---|
764 | /// |
---|
765 | /// Creates a subdigraph for the given digraph with the |
---|
766 | /// given node and arc filter maps. |
---|
767 | SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) { |
---|
768 | Parent::initialize(digraph, node_filter, arc_filter); |
---|
769 | } |
---|
770 | |
---|
771 | /// \brief Sets the status of the given node |
---|
772 | /// |
---|
773 | /// This function sets the status of the given node. |
---|
774 | /// It is done by simply setting the assigned value of \c n |
---|
775 | /// to \c v in the node filter map. |
---|
776 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
777 | |
---|
778 | /// \brief Sets the status of the given arc |
---|
779 | /// |
---|
780 | /// This function sets the status of the given arc. |
---|
781 | /// It is done by simply setting the assigned value of \c a |
---|
782 | /// to \c v in the arc filter map. |
---|
783 | void status(const Arc& a, bool v) const { Parent::status(a, v); } |
---|
784 | |
---|
785 | /// \brief Returns the status of the given node |
---|
786 | /// |
---|
787 | /// This function returns the status of the given node. |
---|
788 | /// It is \c true if the given node is enabled (i.e. not hidden). |
---|
789 | bool status(const Node& n) const { return Parent::status(n); } |
---|
790 | |
---|
791 | /// \brief Returns the status of the given arc |
---|
792 | /// |
---|
793 | /// This function returns the status of the given arc. |
---|
794 | /// It is \c true if the given arc is enabled (i.e. not hidden). |
---|
795 | bool status(const Arc& a) const { return Parent::status(a); } |
---|
796 | |
---|
797 | /// \brief Disables the given node |
---|
798 | /// |
---|
799 | /// This function disables the given node in the subdigraph, |
---|
800 | /// so the iteration jumps over it. |
---|
801 | /// It is the same as \ref status() "status(n, false)". |
---|
802 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
803 | |
---|
804 | /// \brief Disables the given arc |
---|
805 | /// |
---|
806 | /// This function disables the given arc in the subdigraph, |
---|
807 | /// so the iteration jumps over it. |
---|
808 | /// It is the same as \ref status() "status(a, false)". |
---|
809 | void disable(const Arc& a) const { Parent::status(a, false); } |
---|
810 | |
---|
811 | /// \brief Enables the given node |
---|
812 | /// |
---|
813 | /// This function enables the given node in the subdigraph. |
---|
814 | /// It is the same as \ref status() "status(n, true)". |
---|
815 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
816 | |
---|
817 | /// \brief Enables the given arc |
---|
818 | /// |
---|
819 | /// This function enables the given arc in the subdigraph. |
---|
820 | /// It is the same as \ref status() "status(a, true)". |
---|
821 | void enable(const Arc& a) const { Parent::status(a, true); } |
---|
822 | |
---|
823 | }; |
---|
824 | |
---|
825 | /// \brief Returns a read-only SubDigraph adaptor |
---|
826 | /// |
---|
827 | /// This function just returns a read-only \ref SubDigraph adaptor. |
---|
828 | /// \ingroup graph_adaptors |
---|
829 | /// \relates SubDigraph |
---|
830 | template<typename DGR, typename NF, typename AF> |
---|
831 | SubDigraph<const DGR, NF, AF> |
---|
832 | subDigraph(const DGR& digraph, |
---|
833 | NF& node_filter, AF& arc_filter) { |
---|
834 | return SubDigraph<const DGR, NF, AF> |
---|
835 | (digraph, node_filter, arc_filter); |
---|
836 | } |
---|
837 | |
---|
838 | template<typename DGR, typename NF, typename AF> |
---|
839 | SubDigraph<const DGR, const NF, AF> |
---|
840 | subDigraph(const DGR& digraph, |
---|
841 | const NF& node_filter, AF& arc_filter) { |
---|
842 | return SubDigraph<const DGR, const NF, AF> |
---|
843 | (digraph, node_filter, arc_filter); |
---|
844 | } |
---|
845 | |
---|
846 | template<typename DGR, typename NF, typename AF> |
---|
847 | SubDigraph<const DGR, NF, const AF> |
---|
848 | subDigraph(const DGR& digraph, |
---|
849 | NF& node_filter, const AF& arc_filter) { |
---|
850 | return SubDigraph<const DGR, NF, const AF> |
---|
851 | (digraph, node_filter, arc_filter); |
---|
852 | } |
---|
853 | |
---|
854 | template<typename DGR, typename NF, typename AF> |
---|
855 | SubDigraph<const DGR, const NF, const AF> |
---|
856 | subDigraph(const DGR& digraph, |
---|
857 | const NF& node_filter, const AF& arc_filter) { |
---|
858 | return SubDigraph<const DGR, const NF, const AF> |
---|
859 | (digraph, node_filter, arc_filter); |
---|
860 | } |
---|
861 | |
---|
862 | |
---|
863 | template <typename GR, typename NF, typename EF, bool ch = true> |
---|
864 | class SubGraphBase : public GraphAdaptorBase<GR> { |
---|
865 | public: |
---|
866 | typedef GR Graph; |
---|
867 | typedef NF NodeFilterMap; |
---|
868 | typedef EF EdgeFilterMap; |
---|
869 | |
---|
870 | typedef SubGraphBase Adaptor; |
---|
871 | typedef GraphAdaptorBase<GR> Parent; |
---|
872 | protected: |
---|
873 | |
---|
874 | NF* _node_filter; |
---|
875 | EF* _edge_filter; |
---|
876 | |
---|
877 | SubGraphBase() |
---|
878 | : Parent(), _node_filter(0), _edge_filter(0) { } |
---|
879 | |
---|
880 | void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
---|
881 | Parent::initialize(graph); |
---|
882 | _node_filter = &node_filter; |
---|
883 | _edge_filter = &edge_filter; |
---|
884 | } |
---|
885 | |
---|
886 | public: |
---|
887 | |
---|
888 | typedef typename Parent::Node Node; |
---|
889 | typedef typename Parent::Arc Arc; |
---|
890 | typedef typename Parent::Edge Edge; |
---|
891 | |
---|
892 | void first(Node& i) const { |
---|
893 | Parent::first(i); |
---|
894 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
895 | } |
---|
896 | |
---|
897 | void first(Arc& i) const { |
---|
898 | Parent::first(i); |
---|
899 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
900 | || !(*_node_filter)[Parent::source(i)] |
---|
901 | || !(*_node_filter)[Parent::target(i)])) |
---|
902 | Parent::next(i); |
---|
903 | } |
---|
904 | |
---|
905 | void first(Edge& i) const { |
---|
906 | Parent::first(i); |
---|
907 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
908 | || !(*_node_filter)[Parent::u(i)] |
---|
909 | || !(*_node_filter)[Parent::v(i)])) |
---|
910 | Parent::next(i); |
---|
911 | } |
---|
912 | |
---|
913 | void firstIn(Arc& i, const Node& n) const { |
---|
914 | Parent::firstIn(i, n); |
---|
915 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
916 | || !(*_node_filter)[Parent::source(i)])) |
---|
917 | Parent::nextIn(i); |
---|
918 | } |
---|
919 | |
---|
920 | void firstOut(Arc& i, const Node& n) const { |
---|
921 | Parent::firstOut(i, n); |
---|
922 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
923 | || !(*_node_filter)[Parent::target(i)])) |
---|
924 | Parent::nextOut(i); |
---|
925 | } |
---|
926 | |
---|
927 | void firstInc(Edge& i, bool& d, const Node& n) const { |
---|
928 | Parent::firstInc(i, d, n); |
---|
929 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
930 | || !(*_node_filter)[Parent::u(i)] |
---|
931 | || !(*_node_filter)[Parent::v(i)])) |
---|
932 | Parent::nextInc(i, d); |
---|
933 | } |
---|
934 | |
---|
935 | void next(Node& i) const { |
---|
936 | Parent::next(i); |
---|
937 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
938 | } |
---|
939 | |
---|
940 | void next(Arc& i) const { |
---|
941 | Parent::next(i); |
---|
942 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
943 | || !(*_node_filter)[Parent::source(i)] |
---|
944 | || !(*_node_filter)[Parent::target(i)])) |
---|
945 | Parent::next(i); |
---|
946 | } |
---|
947 | |
---|
948 | void next(Edge& i) const { |
---|
949 | Parent::next(i); |
---|
950 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
951 | || !(*_node_filter)[Parent::u(i)] |
---|
952 | || !(*_node_filter)[Parent::v(i)])) |
---|
953 | Parent::next(i); |
---|
954 | } |
---|
955 | |
---|
956 | void nextIn(Arc& i) const { |
---|
957 | Parent::nextIn(i); |
---|
958 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
959 | || !(*_node_filter)[Parent::source(i)])) |
---|
960 | Parent::nextIn(i); |
---|
961 | } |
---|
962 | |
---|
963 | void nextOut(Arc& i) const { |
---|
964 | Parent::nextOut(i); |
---|
965 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
966 | || !(*_node_filter)[Parent::target(i)])) |
---|
967 | Parent::nextOut(i); |
---|
968 | } |
---|
969 | |
---|
970 | void nextInc(Edge& i, bool& d) const { |
---|
971 | Parent::nextInc(i, d); |
---|
972 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
973 | || !(*_node_filter)[Parent::u(i)] |
---|
974 | || !(*_node_filter)[Parent::v(i)])) |
---|
975 | Parent::nextInc(i, d); |
---|
976 | } |
---|
977 | |
---|
978 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
979 | void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
---|
980 | |
---|
981 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
982 | bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
---|
983 | |
---|
984 | typedef False NodeNumTag; |
---|
985 | typedef False ArcNumTag; |
---|
986 | typedef False EdgeNumTag; |
---|
987 | |
---|
988 | typedef FindArcTagIndicator<Graph> FindArcTag; |
---|
989 | Arc findArc(const Node& u, const Node& v, |
---|
990 | const Arc& prev = INVALID) const { |
---|
991 | if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
---|
992 | return INVALID; |
---|
993 | } |
---|
994 | Arc arc = Parent::findArc(u, v, prev); |
---|
995 | while (arc != INVALID && !(*_edge_filter)[arc]) { |
---|
996 | arc = Parent::findArc(u, v, arc); |
---|
997 | } |
---|
998 | return arc; |
---|
999 | } |
---|
1000 | |
---|
1001 | typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
---|
1002 | Edge findEdge(const Node& u, const Node& v, |
---|
1003 | const Edge& prev = INVALID) const { |
---|
1004 | if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
---|
1005 | return INVALID; |
---|
1006 | } |
---|
1007 | Edge edge = Parent::findEdge(u, v, prev); |
---|
1008 | while (edge != INVALID && !(*_edge_filter)[edge]) { |
---|
1009 | edge = Parent::findEdge(u, v, edge); |
---|
1010 | } |
---|
1011 | return edge; |
---|
1012 | } |
---|
1013 | |
---|
1014 | template <typename V> |
---|
1015 | class NodeMap |
---|
1016 | : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
1017 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
---|
1018 | public: |
---|
1019 | typedef V Value; |
---|
1020 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
1021 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
---|
1022 | |
---|
1023 | NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
---|
1024 | : Parent(adaptor) {} |
---|
1025 | NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
---|
1026 | : Parent(adaptor, value) {} |
---|
1027 | |
---|
1028 | private: |
---|
1029 | NodeMap& operator=(const NodeMap& cmap) { |
---|
1030 | return operator=<NodeMap>(cmap); |
---|
1031 | } |
---|
1032 | |
---|
1033 | template <typename CMap> |
---|
1034 | NodeMap& operator=(const CMap& cmap) { |
---|
1035 | Parent::operator=(cmap); |
---|
1036 | return *this; |
---|
1037 | } |
---|
1038 | }; |
---|
1039 | |
---|
1040 | template <typename V> |
---|
1041 | class ArcMap |
---|
1042 | : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
1043 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
---|
1044 | public: |
---|
1045 | typedef V Value; |
---|
1046 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
1047 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
---|
1048 | |
---|
1049 | ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
---|
1050 | : Parent(adaptor) {} |
---|
1051 | ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
---|
1052 | : Parent(adaptor, value) {} |
---|
1053 | |
---|
1054 | private: |
---|
1055 | ArcMap& operator=(const ArcMap& cmap) { |
---|
1056 | return operator=<ArcMap>(cmap); |
---|
1057 | } |
---|
1058 | |
---|
1059 | template <typename CMap> |
---|
1060 | ArcMap& operator=(const CMap& cmap) { |
---|
1061 | Parent::operator=(cmap); |
---|
1062 | return *this; |
---|
1063 | } |
---|
1064 | }; |
---|
1065 | |
---|
1066 | template <typename V> |
---|
1067 | class EdgeMap |
---|
1068 | : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
1069 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
---|
1070 | public: |
---|
1071 | typedef V Value; |
---|
1072 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
1073 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
---|
1074 | |
---|
1075 | EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
---|
1076 | : Parent(adaptor) {} |
---|
1077 | |
---|
1078 | EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
---|
1079 | : Parent(adaptor, value) {} |
---|
1080 | |
---|
1081 | private: |
---|
1082 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
1083 | return operator=<EdgeMap>(cmap); |
---|
1084 | } |
---|
1085 | |
---|
1086 | template <typename CMap> |
---|
1087 | EdgeMap& operator=(const CMap& cmap) { |
---|
1088 | Parent::operator=(cmap); |
---|
1089 | return *this; |
---|
1090 | } |
---|
1091 | }; |
---|
1092 | |
---|
1093 | }; |
---|
1094 | |
---|
1095 | template <typename GR, typename NF, typename EF> |
---|
1096 | class SubGraphBase<GR, NF, EF, false> |
---|
1097 | : public GraphAdaptorBase<GR> { |
---|
1098 | public: |
---|
1099 | typedef GR Graph; |
---|
1100 | typedef NF NodeFilterMap; |
---|
1101 | typedef EF EdgeFilterMap; |
---|
1102 | |
---|
1103 | typedef SubGraphBase Adaptor; |
---|
1104 | typedef GraphAdaptorBase<GR> Parent; |
---|
1105 | protected: |
---|
1106 | NF* _node_filter; |
---|
1107 | EF* _edge_filter; |
---|
1108 | SubGraphBase() |
---|
1109 | : Parent(), _node_filter(0), _edge_filter(0) { } |
---|
1110 | |
---|
1111 | void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
---|
1112 | Parent::initialize(graph); |
---|
1113 | _node_filter = &node_filter; |
---|
1114 | _edge_filter = &edge_filter; |
---|
1115 | } |
---|
1116 | |
---|
1117 | public: |
---|
1118 | |
---|
1119 | typedef typename Parent::Node Node; |
---|
1120 | typedef typename Parent::Arc Arc; |
---|
1121 | typedef typename Parent::Edge Edge; |
---|
1122 | |
---|
1123 | void first(Node& i) const { |
---|
1124 | Parent::first(i); |
---|
1125 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
1126 | } |
---|
1127 | |
---|
1128 | void first(Arc& i) const { |
---|
1129 | Parent::first(i); |
---|
1130 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
1131 | } |
---|
1132 | |
---|
1133 | void first(Edge& i) const { |
---|
1134 | Parent::first(i); |
---|
1135 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
1136 | } |
---|
1137 | |
---|
1138 | void firstIn(Arc& i, const Node& n) const { |
---|
1139 | Parent::firstIn(i, n); |
---|
1140 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
---|
1141 | } |
---|
1142 | |
---|
1143 | void firstOut(Arc& i, const Node& n) const { |
---|
1144 | Parent::firstOut(i, n); |
---|
1145 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
---|
1146 | } |
---|
1147 | |
---|
1148 | void firstInc(Edge& i, bool& d, const Node& n) const { |
---|
1149 | Parent::firstInc(i, d, n); |
---|
1150 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
---|
1151 | } |
---|
1152 | |
---|
1153 | void next(Node& i) const { |
---|
1154 | Parent::next(i); |
---|
1155 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
1156 | } |
---|
1157 | void next(Arc& i) const { |
---|
1158 | Parent::next(i); |
---|
1159 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
1160 | } |
---|
1161 | void next(Edge& i) const { |
---|
1162 | Parent::next(i); |
---|
1163 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
1164 | } |
---|
1165 | void nextIn(Arc& i) const { |
---|
1166 | Parent::nextIn(i); |
---|
1167 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
---|
1168 | } |
---|
1169 | |
---|
1170 | void nextOut(Arc& i) const { |
---|
1171 | Parent::nextOut(i); |
---|
1172 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
---|
1173 | } |
---|
1174 | void nextInc(Edge& i, bool& d) const { |
---|
1175 | Parent::nextInc(i, d); |
---|
1176 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
---|
1177 | } |
---|
1178 | |
---|
1179 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
1180 | void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
---|
1181 | |
---|
1182 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
1183 | bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
---|
1184 | |
---|
1185 | typedef False NodeNumTag; |
---|
1186 | typedef False ArcNumTag; |
---|
1187 | typedef False EdgeNumTag; |
---|
1188 | |
---|
1189 | typedef FindArcTagIndicator<Graph> FindArcTag; |
---|
1190 | Arc findArc(const Node& u, const Node& v, |
---|
1191 | const Arc& prev = INVALID) const { |
---|
1192 | Arc arc = Parent::findArc(u, v, prev); |
---|
1193 | while (arc != INVALID && !(*_edge_filter)[arc]) { |
---|
1194 | arc = Parent::findArc(u, v, arc); |
---|
1195 | } |
---|
1196 | return arc; |
---|
1197 | } |
---|
1198 | |
---|
1199 | typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
---|
1200 | Edge findEdge(const Node& u, const Node& v, |
---|
1201 | const Edge& prev = INVALID) const { |
---|
1202 | Edge edge = Parent::findEdge(u, v, prev); |
---|
1203 | while (edge != INVALID && !(*_edge_filter)[edge]) { |
---|
1204 | edge = Parent::findEdge(u, v, edge); |
---|
1205 | } |
---|
1206 | return edge; |
---|
1207 | } |
---|
1208 | |
---|
1209 | template <typename V> |
---|
1210 | class NodeMap |
---|
1211 | : public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
1212 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
---|
1213 | public: |
---|
1214 | typedef V Value; |
---|
1215 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
1216 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
---|
1217 | |
---|
1218 | NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
---|
1219 | : Parent(adaptor) {} |
---|
1220 | NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
---|
1221 | : Parent(adaptor, value) {} |
---|
1222 | |
---|
1223 | private: |
---|
1224 | NodeMap& operator=(const NodeMap& cmap) { |
---|
1225 | return operator=<NodeMap>(cmap); |
---|
1226 | } |
---|
1227 | |
---|
1228 | template <typename CMap> |
---|
1229 | NodeMap& operator=(const CMap& cmap) { |
---|
1230 | Parent::operator=(cmap); |
---|
1231 | return *this; |
---|
1232 | } |
---|
1233 | }; |
---|
1234 | |
---|
1235 | template <typename V> |
---|
1236 | class ArcMap |
---|
1237 | : public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
1238 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
---|
1239 | public: |
---|
1240 | typedef V Value; |
---|
1241 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
1242 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
---|
1243 | |
---|
1244 | ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
---|
1245 | : Parent(adaptor) {} |
---|
1246 | ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
---|
1247 | : Parent(adaptor, value) {} |
---|
1248 | |
---|
1249 | private: |
---|
1250 | ArcMap& operator=(const ArcMap& cmap) { |
---|
1251 | return operator=<ArcMap>(cmap); |
---|
1252 | } |
---|
1253 | |
---|
1254 | template <typename CMap> |
---|
1255 | ArcMap& operator=(const CMap& cmap) { |
---|
1256 | Parent::operator=(cmap); |
---|
1257 | return *this; |
---|
1258 | } |
---|
1259 | }; |
---|
1260 | |
---|
1261 | template <typename V> |
---|
1262 | class EdgeMap |
---|
1263 | : public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
1264 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
---|
1265 | public: |
---|
1266 | typedef V Value; |
---|
1267 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
1268 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
---|
1269 | |
---|
1270 | EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
---|
1271 | : Parent(adaptor) {} |
---|
1272 | |
---|
1273 | EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
---|
1274 | : Parent(adaptor, value) {} |
---|
1275 | |
---|
1276 | private: |
---|
1277 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
1278 | return operator=<EdgeMap>(cmap); |
---|
1279 | } |
---|
1280 | |
---|
1281 | template <typename CMap> |
---|
1282 | EdgeMap& operator=(const CMap& cmap) { |
---|
1283 | Parent::operator=(cmap); |
---|
1284 | return *this; |
---|
1285 | } |
---|
1286 | }; |
---|
1287 | |
---|
1288 | }; |
---|
1289 | |
---|
1290 | /// \ingroup graph_adaptors |
---|
1291 | /// |
---|
1292 | /// \brief Adaptor class for hiding nodes and edges in an undirected |
---|
1293 | /// graph. |
---|
1294 | /// |
---|
1295 | /// SubGraph can be used for hiding nodes and edges in a graph. |
---|
1296 | /// A \c bool node map and a \c bool edge map must be specified, which |
---|
1297 | /// define the filters for nodes and edges. |
---|
1298 | /// Only the nodes and edges with \c true filter value are |
---|
1299 | /// shown in the subgraph. The edges that are incident to hidden |
---|
1300 | /// nodes are also filtered out. |
---|
1301 | /// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
---|
1302 | /// |
---|
1303 | /// The adapted graph can also be modified through this adaptor |
---|
1304 | /// by adding or removing nodes or edges, unless the \c GR template |
---|
1305 | /// parameter is set to be \c const. |
---|
1306 | /// |
---|
1307 | /// \tparam GR The type of the adapted graph. |
---|
1308 | /// It must conform to the \ref concepts::Graph "Graph" concept. |
---|
1309 | /// It can also be specified to be \c const. |
---|
1310 | /// \tparam NF The type of the node filter map. |
---|
1311 | /// It must be a \c bool (or convertible) node map of the |
---|
1312 | /// adapted graph. The default type is |
---|
1313 | /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
---|
1314 | /// \tparam EF The type of the edge filter map. |
---|
1315 | /// It must be a \c bool (or convertible) edge map of the |
---|
1316 | /// adapted graph. The default type is |
---|
1317 | /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
---|
1318 | /// |
---|
1319 | /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
---|
1320 | /// adapted graph are convertible to each other. |
---|
1321 | /// |
---|
1322 | /// \see FilterNodes |
---|
1323 | /// \see FilterEdges |
---|
1324 | #ifdef DOXYGEN |
---|
1325 | template<typename GR, typename NF, typename EF> |
---|
1326 | class SubGraph { |
---|
1327 | #else |
---|
1328 | template<typename GR, |
---|
1329 | typename NF = typename GR::template NodeMap<bool>, |
---|
1330 | typename EF = typename GR::template EdgeMap<bool> > |
---|
1331 | class SubGraph : |
---|
1332 | public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > { |
---|
1333 | #endif |
---|
1334 | public: |
---|
1335 | /// The type of the adapted graph. |
---|
1336 | typedef GR Graph; |
---|
1337 | /// The type of the node filter map. |
---|
1338 | typedef NF NodeFilterMap; |
---|
1339 | /// The type of the edge filter map. |
---|
1340 | typedef EF EdgeFilterMap; |
---|
1341 | |
---|
1342 | typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > |
---|
1343 | Parent; |
---|
1344 | |
---|
1345 | typedef typename Parent::Node Node; |
---|
1346 | typedef typename Parent::Edge Edge; |
---|
1347 | |
---|
1348 | protected: |
---|
1349 | SubGraph() { } |
---|
1350 | public: |
---|
1351 | |
---|
1352 | /// \brief Constructor |
---|
1353 | /// |
---|
1354 | /// Creates a subgraph for the given graph with the given node |
---|
1355 | /// and edge filter maps. |
---|
1356 | SubGraph(GR& graph, NF& node_filter, EF& edge_filter) { |
---|
1357 | initialize(graph, node_filter, edge_filter); |
---|
1358 | } |
---|
1359 | |
---|
1360 | /// \brief Sets the status of the given node |
---|
1361 | /// |
---|
1362 | /// This function sets the status of the given node. |
---|
1363 | /// It is done by simply setting the assigned value of \c n |
---|
1364 | /// to \c v in the node filter map. |
---|
1365 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
1366 | |
---|
1367 | /// \brief Sets the status of the given edge |
---|
1368 | /// |
---|
1369 | /// This function sets the status of the given edge. |
---|
1370 | /// It is done by simply setting the assigned value of \c e |
---|
1371 | /// to \c v in the edge filter map. |
---|
1372 | void status(const Edge& e, bool v) const { Parent::status(e, v); } |
---|
1373 | |
---|
1374 | /// \brief Returns the status of the given node |
---|
1375 | /// |
---|
1376 | /// This function returns the status of the given node. |
---|
1377 | /// It is \c true if the given node is enabled (i.e. not hidden). |
---|
1378 | bool status(const Node& n) const { return Parent::status(n); } |
---|
1379 | |
---|
1380 | /// \brief Returns the status of the given edge |
---|
1381 | /// |
---|
1382 | /// This function returns the status of the given edge. |
---|
1383 | /// It is \c true if the given edge is enabled (i.e. not hidden). |
---|
1384 | bool status(const Edge& e) const { return Parent::status(e); } |
---|
1385 | |
---|
1386 | /// \brief Disables the given node |
---|
1387 | /// |
---|
1388 | /// This function disables the given node in the subdigraph, |
---|
1389 | /// so the iteration jumps over it. |
---|
1390 | /// It is the same as \ref status() "status(n, false)". |
---|
1391 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
1392 | |
---|
1393 | /// \brief Disables the given edge |
---|
1394 | /// |
---|
1395 | /// This function disables the given edge in the subgraph, |
---|
1396 | /// so the iteration jumps over it. |
---|
1397 | /// It is the same as \ref status() "status(e, false)". |
---|
1398 | void disable(const Edge& e) const { Parent::status(e, false); } |
---|
1399 | |
---|
1400 | /// \brief Enables the given node |
---|
1401 | /// |
---|
1402 | /// This function enables the given node in the subdigraph. |
---|
1403 | /// It is the same as \ref status() "status(n, true)". |
---|
1404 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
1405 | |
---|
1406 | /// \brief Enables the given edge |
---|
1407 | /// |
---|
1408 | /// This function enables the given edge in the subgraph. |
---|
1409 | /// It is the same as \ref status() "status(e, true)". |
---|
1410 | void enable(const Edge& e) const { Parent::status(e, true); } |
---|
1411 | |
---|
1412 | }; |
---|
1413 | |
---|
1414 | /// \brief Returns a read-only SubGraph adaptor |
---|
1415 | /// |
---|
1416 | /// This function just returns a read-only \ref SubGraph adaptor. |
---|
1417 | /// \ingroup graph_adaptors |
---|
1418 | /// \relates SubGraph |
---|
1419 | template<typename GR, typename NF, typename EF> |
---|
1420 | SubGraph<const GR, NF, EF> |
---|
1421 | subGraph(const GR& graph, NF& node_filter, EF& edge_filter) { |
---|
1422 | return SubGraph<const GR, NF, EF> |
---|
1423 | (graph, node_filter, edge_filter); |
---|
1424 | } |
---|
1425 | |
---|
1426 | template<typename GR, typename NF, typename EF> |
---|
1427 | SubGraph<const GR, const NF, EF> |
---|
1428 | subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) { |
---|
1429 | return SubGraph<const GR, const NF, EF> |
---|
1430 | (graph, node_filter, edge_filter); |
---|
1431 | } |
---|
1432 | |
---|
1433 | template<typename GR, typename NF, typename EF> |
---|
1434 | SubGraph<const GR, NF, const EF> |
---|
1435 | subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) { |
---|
1436 | return SubGraph<const GR, NF, const EF> |
---|
1437 | (graph, node_filter, edge_filter); |
---|
1438 | } |
---|
1439 | |
---|
1440 | template<typename GR, typename NF, typename EF> |
---|
1441 | SubGraph<const GR, const NF, const EF> |
---|
1442 | subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) { |
---|
1443 | return SubGraph<const GR, const NF, const EF> |
---|
1444 | (graph, node_filter, edge_filter); |
---|
1445 | } |
---|
1446 | |
---|
1447 | |
---|
1448 | /// \ingroup graph_adaptors |
---|
1449 | /// |
---|
1450 | /// \brief Adaptor class for hiding nodes in a digraph or a graph. |
---|
1451 | /// |
---|
1452 | /// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
---|
1453 | /// graph. A \c bool node map must be specified, which defines the filter |
---|
1454 | /// for the nodes. Only the nodes with \c true filter value and the |
---|
1455 | /// arcs/edges incident to nodes both with \c true filter value are shown |
---|
1456 | /// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
---|
1457 | /// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
---|
1458 | /// depending on the \c GR template parameter. |
---|
1459 | /// |
---|
1460 | /// The adapted (di)graph can also be modified through this adaptor |
---|
1461 | /// by adding or removing nodes or arcs/edges, unless the \c GR template |
---|
1462 | /// parameter is set to be \c const. |
---|
1463 | /// |
---|
1464 | /// \tparam GR The type of the adapted digraph or graph. |
---|
1465 | /// It must conform to the \ref concepts::Digraph "Digraph" concept |
---|
1466 | /// or the \ref concepts::Graph "Graph" concept. |
---|
1467 | /// It can also be specified to be \c const. |
---|
1468 | /// \tparam NF The type of the node filter map. |
---|
1469 | /// It must be a \c bool (or convertible) node map of the |
---|
1470 | /// adapted (di)graph. The default type is |
---|
1471 | /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
---|
1472 | /// |
---|
1473 | /// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
---|
1474 | /// adapted (di)graph are convertible to each other. |
---|
1475 | #ifdef DOXYGEN |
---|
1476 | template<typename GR, typename NF> |
---|
1477 | class FilterNodes { |
---|
1478 | #else |
---|
1479 | template<typename GR, |
---|
1480 | typename NF = typename GR::template NodeMap<bool>, |
---|
1481 | typename Enable = void> |
---|
1482 | class FilterNodes : |
---|
1483 | public DigraphAdaptorExtender< |
---|
1484 | SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
---|
1485 | true> > { |
---|
1486 | #endif |
---|
1487 | public: |
---|
1488 | |
---|
1489 | typedef GR Digraph; |
---|
1490 | typedef NF NodeFilterMap; |
---|
1491 | |
---|
1492 | typedef DigraphAdaptorExtender< |
---|
1493 | SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
---|
1494 | true> > Parent; |
---|
1495 | |
---|
1496 | typedef typename Parent::Node Node; |
---|
1497 | |
---|
1498 | protected: |
---|
1499 | ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
---|
1500 | |
---|
1501 | FilterNodes() : const_true_map() {} |
---|
1502 | |
---|
1503 | public: |
---|
1504 | |
---|
1505 | /// \brief Constructor |
---|
1506 | /// |
---|
1507 | /// Creates a subgraph for the given digraph or graph with the |
---|
1508 | /// given node filter map. |
---|
1509 | FilterNodes(GR& graph, NF& node_filter) |
---|
1510 | : Parent(), const_true_map() |
---|
1511 | { |
---|
1512 | Parent::initialize(graph, node_filter, const_true_map); |
---|
1513 | } |
---|
1514 | |
---|
1515 | /// \brief Sets the status of the given node |
---|
1516 | /// |
---|
1517 | /// This function sets the status of the given node. |
---|
1518 | /// It is done by simply setting the assigned value of \c n |
---|
1519 | /// to \c v in the node filter map. |
---|
1520 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
1521 | |
---|
1522 | /// \brief Returns the status of the given node |
---|
1523 | /// |
---|
1524 | /// This function returns the status of the given node. |
---|
1525 | /// It is \c true if the given node is enabled (i.e. not hidden). |
---|
1526 | bool status(const Node& n) const { return Parent::status(n); } |
---|
1527 | |
---|
1528 | /// \brief Disables the given node |
---|
1529 | /// |
---|
1530 | /// This function disables the given node, so the iteration |
---|
1531 | /// jumps over it. |
---|
1532 | /// It is the same as \ref status() "status(n, false)". |
---|
1533 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
1534 | |
---|
1535 | /// \brief Enables the given node |
---|
1536 | /// |
---|
1537 | /// This function enables the given node. |
---|
1538 | /// It is the same as \ref status() "status(n, true)". |
---|
1539 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
1540 | |
---|
1541 | }; |
---|
1542 | |
---|
1543 | template<typename GR, typename NF> |
---|
1544 | class FilterNodes<GR, NF, |
---|
1545 | typename enable_if<UndirectedTagIndicator<GR> >::type> : |
---|
1546 | public GraphAdaptorExtender< |
---|
1547 | SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
---|
1548 | true> > { |
---|
1549 | |
---|
1550 | public: |
---|
1551 | typedef GR Graph; |
---|
1552 | typedef NF NodeFilterMap; |
---|
1553 | typedef GraphAdaptorExtender< |
---|
1554 | SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
---|
1555 | true> > Parent; |
---|
1556 | |
---|
1557 | typedef typename Parent::Node Node; |
---|
1558 | protected: |
---|
1559 | ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
---|
1560 | |
---|
1561 | FilterNodes() : const_true_map() {} |
---|
1562 | |
---|
1563 | public: |
---|
1564 | |
---|
1565 | FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
---|
1566 | Parent(), const_true_map() { |
---|
1567 | Parent::initialize(graph, node_filter, const_true_map); |
---|
1568 | } |
---|
1569 | |
---|
1570 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
1571 | bool status(const Node& n) const { return Parent::status(n); } |
---|
1572 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
1573 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
1574 | |
---|
1575 | }; |
---|
1576 | |
---|
1577 | |
---|
1578 | /// \brief Returns a read-only FilterNodes adaptor |
---|
1579 | /// |
---|
1580 | /// This function just returns a read-only \ref FilterNodes adaptor. |
---|
1581 | /// \ingroup graph_adaptors |
---|
1582 | /// \relates FilterNodes |
---|
1583 | template<typename GR, typename NF> |
---|
1584 | FilterNodes<const GR, NF> |
---|
1585 | filterNodes(const GR& graph, NF& node_filter) { |
---|
1586 | return FilterNodes<const GR, NF>(graph, node_filter); |
---|
1587 | } |
---|
1588 | |
---|
1589 | template<typename GR, typename NF> |
---|
1590 | FilterNodes<const GR, const NF> |
---|
1591 | filterNodes(const GR& graph, const NF& node_filter) { |
---|
1592 | return FilterNodes<const GR, const NF>(graph, node_filter); |
---|
1593 | } |
---|
1594 | |
---|
1595 | /// \ingroup graph_adaptors |
---|
1596 | /// |
---|
1597 | /// \brief Adaptor class for hiding arcs in a digraph. |
---|
1598 | /// |
---|
1599 | /// FilterArcs adaptor can be used for hiding arcs in a digraph. |
---|
1600 | /// A \c bool arc map must be specified, which defines the filter for |
---|
1601 | /// the arcs. Only the arcs with \c true filter value are shown in the |
---|
1602 | /// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
---|
1603 | /// "Digraph" concept. |
---|
1604 | /// |
---|
1605 | /// The adapted digraph can also be modified through this adaptor |
---|
1606 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
1607 | /// parameter is set to be \c const. |
---|
1608 | /// |
---|
1609 | /// \tparam DGR The type of the adapted digraph. |
---|
1610 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
1611 | /// It can also be specified to be \c const. |
---|
1612 | /// \tparam AF The type of the arc filter map. |
---|
1613 | /// It must be a \c bool (or convertible) arc map of the |
---|
1614 | /// adapted digraph. The default type is |
---|
1615 | /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
---|
1616 | /// |
---|
1617 | /// \note The \c Node and \c Arc types of this adaptor and the adapted |
---|
1618 | /// digraph are convertible to each other. |
---|
1619 | #ifdef DOXYGEN |
---|
1620 | template<typename DGR, |
---|
1621 | typename AF> |
---|
1622 | class FilterArcs { |
---|
1623 | #else |
---|
1624 | template<typename DGR, |
---|
1625 | typename AF = typename DGR::template ArcMap<bool> > |
---|
1626 | class FilterArcs : |
---|
1627 | public DigraphAdaptorExtender< |
---|
1628 | SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
---|
1629 | AF, false> > { |
---|
1630 | #endif |
---|
1631 | public: |
---|
1632 | /// The type of the adapted digraph. |
---|
1633 | typedef DGR Digraph; |
---|
1634 | /// The type of the arc filter map. |
---|
1635 | typedef AF ArcFilterMap; |
---|
1636 | |
---|
1637 | typedef DigraphAdaptorExtender< |
---|
1638 | SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
---|
1639 | AF, false> > Parent; |
---|
1640 | |
---|
1641 | typedef typename Parent::Arc Arc; |
---|
1642 | |
---|
1643 | protected: |
---|
1644 | ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
---|
1645 | |
---|
1646 | FilterArcs() : const_true_map() {} |
---|
1647 | |
---|
1648 | public: |
---|
1649 | |
---|
1650 | /// \brief Constructor |
---|
1651 | /// |
---|
1652 | /// Creates a subdigraph for the given digraph with the given arc |
---|
1653 | /// filter map. |
---|
1654 | FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
---|
1655 | : Parent(), const_true_map() { |
---|
1656 | Parent::initialize(digraph, const_true_map, arc_filter); |
---|
1657 | } |
---|
1658 | |
---|
1659 | /// \brief Sets the status of the given arc |
---|
1660 | /// |
---|
1661 | /// This function sets the status of the given arc. |
---|
1662 | /// It is done by simply setting the assigned value of \c a |
---|
1663 | /// to \c v in the arc filter map. |
---|
1664 | void status(const Arc& a, bool v) const { Parent::status(a, v); } |
---|
1665 | |
---|
1666 | /// \brief Returns the status of the given arc |
---|
1667 | /// |
---|
1668 | /// This function returns the status of the given arc. |
---|
1669 | /// It is \c true if the given arc is enabled (i.e. not hidden). |
---|
1670 | bool status(const Arc& a) const { return Parent::status(a); } |
---|
1671 | |
---|
1672 | /// \brief Disables the given arc |
---|
1673 | /// |
---|
1674 | /// This function disables the given arc in the subdigraph, |
---|
1675 | /// so the iteration jumps over it. |
---|
1676 | /// It is the same as \ref status() "status(a, false)". |
---|
1677 | void disable(const Arc& a) const { Parent::status(a, false); } |
---|
1678 | |
---|
1679 | /// \brief Enables the given arc |
---|
1680 | /// |
---|
1681 | /// This function enables the given arc in the subdigraph. |
---|
1682 | /// It is the same as \ref status() "status(a, true)". |
---|
1683 | void enable(const Arc& a) const { Parent::status(a, true); } |
---|
1684 | |
---|
1685 | }; |
---|
1686 | |
---|
1687 | /// \brief Returns a read-only FilterArcs adaptor |
---|
1688 | /// |
---|
1689 | /// This function just returns a read-only \ref FilterArcs adaptor. |
---|
1690 | /// \ingroup graph_adaptors |
---|
1691 | /// \relates FilterArcs |
---|
1692 | template<typename DGR, typename AF> |
---|
1693 | FilterArcs<const DGR, AF> |
---|
1694 | filterArcs(const DGR& digraph, AF& arc_filter) { |
---|
1695 | return FilterArcs<const DGR, AF>(digraph, arc_filter); |
---|
1696 | } |
---|
1697 | |
---|
1698 | template<typename DGR, typename AF> |
---|
1699 | FilterArcs<const DGR, const AF> |
---|
1700 | filterArcs(const DGR& digraph, const AF& arc_filter) { |
---|
1701 | return FilterArcs<const DGR, const AF>(digraph, arc_filter); |
---|
1702 | } |
---|
1703 | |
---|
1704 | /// \ingroup graph_adaptors |
---|
1705 | /// |
---|
1706 | /// \brief Adaptor class for hiding edges in a graph. |
---|
1707 | /// |
---|
1708 | /// FilterEdges adaptor can be used for hiding edges in a graph. |
---|
1709 | /// A \c bool edge map must be specified, which defines the filter for |
---|
1710 | /// the edges. Only the edges with \c true filter value are shown in the |
---|
1711 | /// subgraph. This adaptor conforms to the \ref concepts::Graph |
---|
1712 | /// "Graph" concept. |
---|
1713 | /// |
---|
1714 | /// The adapted graph can also be modified through this adaptor |
---|
1715 | /// by adding or removing nodes or edges, unless the \c GR template |
---|
1716 | /// parameter is set to be \c const. |
---|
1717 | /// |
---|
1718 | /// \tparam GR The type of the adapted graph. |
---|
1719 | /// It must conform to the \ref concepts::Graph "Graph" concept. |
---|
1720 | /// It can also be specified to be \c const. |
---|
1721 | /// \tparam EF The type of the edge filter map. |
---|
1722 | /// It must be a \c bool (or convertible) edge map of the |
---|
1723 | /// adapted graph. The default type is |
---|
1724 | /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
---|
1725 | /// |
---|
1726 | /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
---|
1727 | /// adapted graph are convertible to each other. |
---|
1728 | #ifdef DOXYGEN |
---|
1729 | template<typename GR, |
---|
1730 | typename EF> |
---|
1731 | class FilterEdges { |
---|
1732 | #else |
---|
1733 | template<typename GR, |
---|
1734 | typename EF = typename GR::template EdgeMap<bool> > |
---|
1735 | class FilterEdges : |
---|
1736 | public GraphAdaptorExtender< |
---|
1737 | SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
---|
1738 | EF, false> > { |
---|
1739 | #endif |
---|
1740 | public: |
---|
1741 | /// The type of the adapted graph. |
---|
1742 | typedef GR Graph; |
---|
1743 | /// The type of the edge filter map. |
---|
1744 | typedef EF EdgeFilterMap; |
---|
1745 | |
---|
1746 | typedef GraphAdaptorExtender< |
---|
1747 | SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
---|
1748 | EF, false> > Parent; |
---|
1749 | |
---|
1750 | typedef typename Parent::Edge Edge; |
---|
1751 | |
---|
1752 | protected: |
---|
1753 | ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
---|
1754 | |
---|
1755 | FilterEdges() : const_true_map(true) { |
---|
1756 | Parent::setNodeFilterMap(const_true_map); |
---|
1757 | } |
---|
1758 | |
---|
1759 | public: |
---|
1760 | |
---|
1761 | /// \brief Constructor |
---|
1762 | /// |
---|
1763 | /// Creates a subgraph for the given graph with the given edge |
---|
1764 | /// filter map. |
---|
1765 | FilterEdges(GR& graph, EF& edge_filter) |
---|
1766 | : Parent(), const_true_map() { |
---|
1767 | Parent::initialize(graph, const_true_map, edge_filter); |
---|
1768 | } |
---|
1769 | |
---|
1770 | /// \brief Sets the status of the given edge |
---|
1771 | /// |
---|
1772 | /// This function sets the status of the given edge. |
---|
1773 | /// It is done by simply setting the assigned value of \c e |
---|
1774 | /// to \c v in the edge filter map. |
---|
1775 | void status(const Edge& e, bool v) const { Parent::status(e, v); } |
---|
1776 | |
---|
1777 | /// \brief Returns the status of the given edge |
---|
1778 | /// |
---|
1779 | /// This function returns the status of the given edge. |
---|
1780 | /// It is \c true if the given edge is enabled (i.e. not hidden). |
---|
1781 | bool status(const Edge& e) const { return Parent::status(e); } |
---|
1782 | |
---|
1783 | /// \brief Disables the given edge |
---|
1784 | /// |
---|
1785 | /// This function disables the given edge in the subgraph, |
---|
1786 | /// so the iteration jumps over it. |
---|
1787 | /// It is the same as \ref status() "status(e, false)". |
---|
1788 | void disable(const Edge& e) const { Parent::status(e, false); } |
---|
1789 | |
---|
1790 | /// \brief Enables the given edge |
---|
1791 | /// |
---|
1792 | /// This function enables the given edge in the subgraph. |
---|
1793 | /// It is the same as \ref status() "status(e, true)". |
---|
1794 | void enable(const Edge& e) const { Parent::status(e, true); } |
---|
1795 | |
---|
1796 | }; |
---|
1797 | |
---|
1798 | /// \brief Returns a read-only FilterEdges adaptor |
---|
1799 | /// |
---|
1800 | /// This function just returns a read-only \ref FilterEdges adaptor. |
---|
1801 | /// \ingroup graph_adaptors |
---|
1802 | /// \relates FilterEdges |
---|
1803 | template<typename GR, typename EF> |
---|
1804 | FilterEdges<const GR, EF> |
---|
1805 | filterEdges(const GR& graph, EF& edge_filter) { |
---|
1806 | return FilterEdges<const GR, EF>(graph, edge_filter); |
---|
1807 | } |
---|
1808 | |
---|
1809 | template<typename GR, typename EF> |
---|
1810 | FilterEdges<const GR, const EF> |
---|
1811 | filterEdges(const GR& graph, const EF& edge_filter) { |
---|
1812 | return FilterEdges<const GR, const EF>(graph, edge_filter); |
---|
1813 | } |
---|
1814 | |
---|
1815 | |
---|
1816 | template <typename DGR> |
---|
1817 | class UndirectorBase { |
---|
1818 | public: |
---|
1819 | typedef DGR Digraph; |
---|
1820 | typedef UndirectorBase Adaptor; |
---|
1821 | |
---|
1822 | typedef True UndirectedTag; |
---|
1823 | |
---|
1824 | typedef typename Digraph::Arc Edge; |
---|
1825 | typedef typename Digraph::Node Node; |
---|
1826 | |
---|
1827 | class Arc : public Edge { |
---|
1828 | friend class UndirectorBase; |
---|
1829 | protected: |
---|
1830 | bool _forward; |
---|
1831 | |
---|
1832 | Arc(const Edge& edge, bool forward) : |
---|
1833 | Edge(edge), _forward(forward) {} |
---|
1834 | |
---|
1835 | public: |
---|
1836 | Arc() {} |
---|
1837 | |
---|
1838 | Arc(Invalid) : Edge(INVALID), _forward(true) {} |
---|
1839 | |
---|
1840 | bool operator==(const Arc &other) const { |
---|
1841 | return _forward == other._forward && |
---|
1842 | static_cast<const Edge&>(*this) == static_cast<const Edge&>(other); |
---|
1843 | } |
---|
1844 | bool operator!=(const Arc &other) const { |
---|
1845 | return _forward != other._forward || |
---|
1846 | static_cast<const Edge&>(*this) != static_cast<const Edge&>(other); |
---|
1847 | } |
---|
1848 | bool operator<(const Arc &other) const { |
---|
1849 | return _forward < other._forward || |
---|
1850 | (_forward == other._forward && |
---|
1851 | static_cast<const Edge&>(*this) < static_cast<const Edge&>(other)); |
---|
1852 | } |
---|
1853 | }; |
---|
1854 | |
---|
1855 | void first(Node& n) const { |
---|
1856 | _digraph->first(n); |
---|
1857 | } |
---|
1858 | |
---|
1859 | void next(Node& n) const { |
---|
1860 | _digraph->next(n); |
---|
1861 | } |
---|
1862 | |
---|
1863 | void first(Arc& a) const { |
---|
1864 | _digraph->first(a); |
---|
1865 | a._forward = true; |
---|
1866 | } |
---|
1867 | |
---|
1868 | void next(Arc& a) const { |
---|
1869 | if (a._forward) { |
---|
1870 | a._forward = false; |
---|
1871 | } else { |
---|
1872 | _digraph->next(a); |
---|
1873 | a._forward = true; |
---|
1874 | } |
---|
1875 | } |
---|
1876 | |
---|
1877 | void first(Edge& e) const { |
---|
1878 | _digraph->first(e); |
---|
1879 | } |
---|
1880 | |
---|
1881 | void next(Edge& e) const { |
---|
1882 | _digraph->next(e); |
---|
1883 | } |
---|
1884 | |
---|
1885 | void firstOut(Arc& a, const Node& n) const { |
---|
1886 | _digraph->firstIn(a, n); |
---|
1887 | if( static_cast<const Edge&>(a) != INVALID ) { |
---|
1888 | a._forward = false; |
---|
1889 | } else { |
---|
1890 | _digraph->firstOut(a, n); |
---|
1891 | a._forward = true; |
---|
1892 | } |
---|
1893 | } |
---|
1894 | void nextOut(Arc &a) const { |
---|
1895 | if (!a._forward) { |
---|
1896 | Node n = _digraph->target(a); |
---|
1897 | _digraph->nextIn(a); |
---|
1898 | if (static_cast<const Edge&>(a) == INVALID ) { |
---|
1899 | _digraph->firstOut(a, n); |
---|
1900 | a._forward = true; |
---|
1901 | } |
---|
1902 | } |
---|
1903 | else { |
---|
1904 | _digraph->nextOut(a); |
---|
1905 | } |
---|
1906 | } |
---|
1907 | |
---|
1908 | void firstIn(Arc &a, const Node &n) const { |
---|
1909 | _digraph->firstOut(a, n); |
---|
1910 | if (static_cast<const Edge&>(a) != INVALID ) { |
---|
1911 | a._forward = false; |
---|
1912 | } else { |
---|
1913 | _digraph->firstIn(a, n); |
---|
1914 | a._forward = true; |
---|
1915 | } |
---|
1916 | } |
---|
1917 | void nextIn(Arc &a) const { |
---|
1918 | if (!a._forward) { |
---|
1919 | Node n = _digraph->source(a); |
---|
1920 | _digraph->nextOut(a); |
---|
1921 | if( static_cast<const Edge&>(a) == INVALID ) { |
---|
1922 | _digraph->firstIn(a, n); |
---|
1923 | a._forward = true; |
---|
1924 | } |
---|
1925 | } |
---|
1926 | else { |
---|
1927 | _digraph->nextIn(a); |
---|
1928 | } |
---|
1929 | } |
---|
1930 | |
---|
1931 | void firstInc(Edge &e, bool &d, const Node &n) const { |
---|
1932 | d = true; |
---|
1933 | _digraph->firstOut(e, n); |
---|
1934 | if (e != INVALID) return; |
---|
1935 | d = false; |
---|
1936 | _digraph->firstIn(e, n); |
---|
1937 | } |
---|
1938 | |
---|
1939 | void nextInc(Edge &e, bool &d) const { |
---|
1940 | if (d) { |
---|
1941 | Node s = _digraph->source(e); |
---|
1942 | _digraph->nextOut(e); |
---|
1943 | if (e != INVALID) return; |
---|
1944 | d = false; |
---|
1945 | _digraph->firstIn(e, s); |
---|
1946 | } else { |
---|
1947 | _digraph->nextIn(e); |
---|
1948 | } |
---|
1949 | } |
---|
1950 | |
---|
1951 | Node u(const Edge& e) const { |
---|
1952 | return _digraph->source(e); |
---|
1953 | } |
---|
1954 | |
---|
1955 | Node v(const Edge& e) const { |
---|
1956 | return _digraph->target(e); |
---|
1957 | } |
---|
1958 | |
---|
1959 | Node source(const Arc &a) const { |
---|
1960 | return a._forward ? _digraph->source(a) : _digraph->target(a); |
---|
1961 | } |
---|
1962 | |
---|
1963 | Node target(const Arc &a) const { |
---|
1964 | return a._forward ? _digraph->target(a) : _digraph->source(a); |
---|
1965 | } |
---|
1966 | |
---|
1967 | static Arc direct(const Edge &e, bool d) { |
---|
1968 | return Arc(e, d); |
---|
1969 | } |
---|
1970 | Arc direct(const Edge &e, const Node& n) const { |
---|
1971 | return Arc(e, _digraph->source(e) == n); |
---|
1972 | } |
---|
1973 | |
---|
1974 | static bool direction(const Arc &a) { return a._forward; } |
---|
1975 | |
---|
1976 | Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
---|
1977 | Arc arcFromId(int ix) const { |
---|
1978 | return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); |
---|
1979 | } |
---|
1980 | Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } |
---|
1981 | |
---|
1982 | int id(const Node &n) const { return _digraph->id(n); } |
---|
1983 | int id(const Arc &a) const { |
---|
1984 | return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); |
---|
1985 | } |
---|
1986 | int id(const Edge &e) const { return _digraph->id(e); } |
---|
1987 | |
---|
1988 | int maxNodeId() const { return _digraph->maxNodeId(); } |
---|
1989 | int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } |
---|
1990 | int maxEdgeId() const { return _digraph->maxArcId(); } |
---|
1991 | |
---|
1992 | Node addNode() { return _digraph->addNode(); } |
---|
1993 | Edge addEdge(const Node& u, const Node& v) { |
---|
1994 | return _digraph->addArc(u, v); |
---|
1995 | } |
---|
1996 | |
---|
1997 | void erase(const Node& i) { _digraph->erase(i); } |
---|
1998 | void erase(const Edge& i) { _digraph->erase(i); } |
---|
1999 | |
---|
2000 | void clear() { _digraph->clear(); } |
---|
2001 | |
---|
2002 | typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
---|
2003 | int nodeNum() const { return _digraph->nodeNum(); } |
---|
2004 | |
---|
2005 | typedef ArcNumTagIndicator<Digraph> ArcNumTag; |
---|
2006 | int arcNum() const { return 2 * _digraph->arcNum(); } |
---|
2007 | |
---|
2008 | typedef ArcNumTag EdgeNumTag; |
---|
2009 | int edgeNum() const { return _digraph->arcNum(); } |
---|
2010 | |
---|
2011 | typedef FindArcTagIndicator<Digraph> FindArcTag; |
---|
2012 | Arc findArc(Node s, Node t, Arc p = INVALID) const { |
---|
2013 | if (p == INVALID) { |
---|
2014 | Edge arc = _digraph->findArc(s, t); |
---|
2015 | if (arc != INVALID) return direct(arc, true); |
---|
2016 | arc = _digraph->findArc(t, s); |
---|
2017 | if (arc != INVALID) return direct(arc, false); |
---|
2018 | } else if (direction(p)) { |
---|
2019 | Edge arc = _digraph->findArc(s, t, p); |
---|
2020 | if (arc != INVALID) return direct(arc, true); |
---|
2021 | arc = _digraph->findArc(t, s); |
---|
2022 | if (arc != INVALID) return direct(arc, false); |
---|
2023 | } else { |
---|
2024 | Edge arc = _digraph->findArc(t, s, p); |
---|
2025 | if (arc != INVALID) return direct(arc, false); |
---|
2026 | } |
---|
2027 | return INVALID; |
---|
2028 | } |
---|
2029 | |
---|
2030 | typedef FindArcTag FindEdgeTag; |
---|
2031 | Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
---|
2032 | if (s != t) { |
---|
2033 | if (p == INVALID) { |
---|
2034 | Edge arc = _digraph->findArc(s, t); |
---|
2035 | if (arc != INVALID) return arc; |
---|
2036 | arc = _digraph->findArc(t, s); |
---|
2037 | if (arc != INVALID) return arc; |
---|
2038 | } else if (_digraph->source(p) == s) { |
---|
2039 | Edge arc = _digraph->findArc(s, t, p); |
---|
2040 | if (arc != INVALID) return arc; |
---|
2041 | arc = _digraph->findArc(t, s); |
---|
2042 | if (arc != INVALID) return arc; |
---|
2043 | } else { |
---|
2044 | Edge arc = _digraph->findArc(t, s, p); |
---|
2045 | if (arc != INVALID) return arc; |
---|
2046 | } |
---|
2047 | } else { |
---|
2048 | return _digraph->findArc(s, t, p); |
---|
2049 | } |
---|
2050 | return INVALID; |
---|
2051 | } |
---|
2052 | |
---|
2053 | private: |
---|
2054 | |
---|
2055 | template <typename V> |
---|
2056 | class ArcMapBase { |
---|
2057 | private: |
---|
2058 | |
---|
2059 | typedef typename DGR::template ArcMap<V> MapImpl; |
---|
2060 | |
---|
2061 | public: |
---|
2062 | |
---|
2063 | typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
---|
2064 | |
---|
2065 | typedef V Value; |
---|
2066 | typedef Arc Key; |
---|
2067 | typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue; |
---|
2068 | typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue; |
---|
2069 | typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference; |
---|
2070 | typedef typename MapTraits<MapImpl>::ReturnValue Reference; |
---|
2071 | |
---|
2072 | ArcMapBase(const UndirectorBase<DGR>& adaptor) : |
---|
2073 | _forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
---|
2074 | |
---|
2075 | ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
2076 | : _forward(*adaptor._digraph, value), |
---|
2077 | _backward(*adaptor._digraph, value) {} |
---|
2078 | |
---|
2079 | void set(const Arc& a, const V& value) { |
---|
2080 | if (direction(a)) { |
---|
2081 | _forward.set(a, value); |
---|
2082 | } else { |
---|
2083 | _backward.set(a, value); |
---|
2084 | } |
---|
2085 | } |
---|
2086 | |
---|
2087 | ConstReturnValue operator[](const Arc& a) const { |
---|
2088 | if (direction(a)) { |
---|
2089 | return _forward[a]; |
---|
2090 | } else { |
---|
2091 | return _backward[a]; |
---|
2092 | } |
---|
2093 | } |
---|
2094 | |
---|
2095 | ReturnValue operator[](const Arc& a) { |
---|
2096 | if (direction(a)) { |
---|
2097 | return _forward[a]; |
---|
2098 | } else { |
---|
2099 | return _backward[a]; |
---|
2100 | } |
---|
2101 | } |
---|
2102 | |
---|
2103 | protected: |
---|
2104 | |
---|
2105 | MapImpl _forward, _backward; |
---|
2106 | |
---|
2107 | }; |
---|
2108 | |
---|
2109 | public: |
---|
2110 | |
---|
2111 | template <typename V> |
---|
2112 | class NodeMap : public DGR::template NodeMap<V> { |
---|
2113 | public: |
---|
2114 | |
---|
2115 | typedef V Value; |
---|
2116 | typedef typename DGR::template NodeMap<Value> Parent; |
---|
2117 | |
---|
2118 | explicit NodeMap(const UndirectorBase<DGR>& adaptor) |
---|
2119 | : Parent(*adaptor._digraph) {} |
---|
2120 | |
---|
2121 | NodeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
2122 | : Parent(*adaptor._digraph, value) { } |
---|
2123 | |
---|
2124 | private: |
---|
2125 | NodeMap& operator=(const NodeMap& cmap) { |
---|
2126 | return operator=<NodeMap>(cmap); |
---|
2127 | } |
---|
2128 | |
---|
2129 | template <typename CMap> |
---|
2130 | NodeMap& operator=(const CMap& cmap) { |
---|
2131 | Parent::operator=(cmap); |
---|
2132 | return *this; |
---|
2133 | } |
---|
2134 | |
---|
2135 | }; |
---|
2136 | |
---|
2137 | template <typename V> |
---|
2138 | class ArcMap |
---|
2139 | : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > |
---|
2140 | { |
---|
2141 | public: |
---|
2142 | typedef V Value; |
---|
2143 | typedef SubMapExtender<Adaptor, ArcMapBase<V> > Parent; |
---|
2144 | |
---|
2145 | explicit ArcMap(const UndirectorBase<DGR>& adaptor) |
---|
2146 | : Parent(adaptor) {} |
---|
2147 | |
---|
2148 | ArcMap(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
2149 | : Parent(adaptor, value) {} |
---|
2150 | |
---|
2151 | private: |
---|
2152 | ArcMap& operator=(const ArcMap& cmap) { |
---|
2153 | return operator=<ArcMap>(cmap); |
---|
2154 | } |
---|
2155 | |
---|
2156 | template <typename CMap> |
---|
2157 | ArcMap& operator=(const CMap& cmap) { |
---|
2158 | Parent::operator=(cmap); |
---|
2159 | return *this; |
---|
2160 | } |
---|
2161 | }; |
---|
2162 | |
---|
2163 | template <typename V> |
---|
2164 | class EdgeMap : public Digraph::template ArcMap<V> { |
---|
2165 | public: |
---|
2166 | |
---|
2167 | typedef V Value; |
---|
2168 | typedef typename Digraph::template ArcMap<V> Parent; |
---|
2169 | |
---|
2170 | explicit EdgeMap(const UndirectorBase<DGR>& adaptor) |
---|
2171 | : Parent(*adaptor._digraph) {} |
---|
2172 | |
---|
2173 | EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
2174 | : Parent(*adaptor._digraph, value) {} |
---|
2175 | |
---|
2176 | private: |
---|
2177 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
2178 | return operator=<EdgeMap>(cmap); |
---|
2179 | } |
---|
2180 | |
---|
2181 | template <typename CMap> |
---|
2182 | EdgeMap& operator=(const CMap& cmap) { |
---|
2183 | Parent::operator=(cmap); |
---|
2184 | return *this; |
---|
2185 | } |
---|
2186 | |
---|
2187 | }; |
---|
2188 | |
---|
2189 | typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
---|
2190 | NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
---|
2191 | |
---|
2192 | typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
---|
2193 | EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); } |
---|
2194 | |
---|
2195 | protected: |
---|
2196 | |
---|
2197 | UndirectorBase() : _digraph(0) {} |
---|
2198 | |
---|
2199 | DGR* _digraph; |
---|
2200 | |
---|
2201 | void initialize(DGR& digraph) { |
---|
2202 | _digraph = &digraph; |
---|
2203 | } |
---|
2204 | |
---|
2205 | }; |
---|
2206 | |
---|
2207 | /// \ingroup graph_adaptors |
---|
2208 | /// |
---|
2209 | /// \brief Adaptor class for viewing a digraph as an undirected graph. |
---|
2210 | /// |
---|
2211 | /// Undirector adaptor can be used for viewing a digraph as an undirected |
---|
2212 | /// graph. All arcs of the underlying digraph are showed in the |
---|
2213 | /// adaptor as an edge (and also as a pair of arcs, of course). |
---|
2214 | /// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
---|
2215 | /// |
---|
2216 | /// The adapted digraph can also be modified through this adaptor |
---|
2217 | /// by adding or removing nodes or edges, unless the \c GR template |
---|
2218 | /// parameter is set to be \c const. |
---|
2219 | /// |
---|
2220 | /// \tparam DGR The type of the adapted digraph. |
---|
2221 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
2222 | /// It can also be specified to be \c const. |
---|
2223 | /// |
---|
2224 | /// \note The \c Node type of this adaptor and the adapted digraph are |
---|
2225 | /// convertible to each other, moreover the \c Edge type of the adaptor |
---|
2226 | /// and the \c Arc type of the adapted digraph are also convertible to |
---|
2227 | /// each other. |
---|
2228 | /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
---|
2229 | /// of the adapted digraph.) |
---|
2230 | template<typename DGR> |
---|
2231 | #ifdef DOXYGEN |
---|
2232 | class Undirector { |
---|
2233 | #else |
---|
2234 | class Undirector : |
---|
2235 | public GraphAdaptorExtender<UndirectorBase<DGR> > { |
---|
2236 | #endif |
---|
2237 | public: |
---|
2238 | /// The type of the adapted digraph. |
---|
2239 | typedef DGR Digraph; |
---|
2240 | typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent; |
---|
2241 | protected: |
---|
2242 | Undirector() { } |
---|
2243 | public: |
---|
2244 | |
---|
2245 | /// \brief Constructor |
---|
2246 | /// |
---|
2247 | /// Creates an undirected graph from the given digraph. |
---|
2248 | Undirector(DGR& digraph) { |
---|
2249 | initialize(digraph); |
---|
2250 | } |
---|
2251 | |
---|
2252 | /// \brief Arc map combined from two original arc maps |
---|
2253 | /// |
---|
2254 | /// This map adaptor class adapts two arc maps of the underlying |
---|
2255 | /// digraph to get an arc map of the undirected graph. |
---|
2256 | /// Its value type is inherited from the first arc map type |
---|
2257 | /// (\c %ForwardMap). |
---|
2258 | template <typename ForwardMap, typename BackwardMap> |
---|
2259 | class CombinedArcMap { |
---|
2260 | public: |
---|
2261 | |
---|
2262 | /// The key type of the map |
---|
2263 | typedef typename Parent::Arc Key; |
---|
2264 | /// The value type of the map |
---|
2265 | typedef typename ForwardMap::Value Value; |
---|
2266 | |
---|
2267 | typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag; |
---|
2268 | |
---|
2269 | typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue; |
---|
2270 | typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue; |
---|
2271 | typedef typename MapTraits<ForwardMap>::ReturnValue Reference; |
---|
2272 | typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference; |
---|
2273 | |
---|
2274 | /// Constructor |
---|
2275 | CombinedArcMap(ForwardMap& forward, BackwardMap& backward) |
---|
2276 | : _forward(&forward), _backward(&backward) {} |
---|
2277 | |
---|
2278 | /// Sets the value associated with the given key. |
---|
2279 | void set(const Key& e, const Value& a) { |
---|
2280 | if (Parent::direction(e)) { |
---|
2281 | _forward->set(e, a); |
---|
2282 | } else { |
---|
2283 | _backward->set(e, a); |
---|
2284 | } |
---|
2285 | } |
---|
2286 | |
---|
2287 | /// Returns the value associated with the given key. |
---|
2288 | ConstReturnValue operator[](const Key& e) const { |
---|
2289 | if (Parent::direction(e)) { |
---|
2290 | return (*_forward)[e]; |
---|
2291 | } else { |
---|
2292 | return (*_backward)[e]; |
---|
2293 | } |
---|
2294 | } |
---|
2295 | |
---|
2296 | /// Returns a reference to the value associated with the given key. |
---|
2297 | ReturnValue operator[](const Key& e) { |
---|
2298 | if (Parent::direction(e)) { |
---|
2299 | return (*_forward)[e]; |
---|
2300 | } else { |
---|
2301 | return (*_backward)[e]; |
---|
2302 | } |
---|
2303 | } |
---|
2304 | |
---|
2305 | protected: |
---|
2306 | |
---|
2307 | ForwardMap* _forward; |
---|
2308 | BackwardMap* _backward; |
---|
2309 | |
---|
2310 | }; |
---|
2311 | |
---|
2312 | /// \brief Returns a combined arc map |
---|
2313 | /// |
---|
2314 | /// This function just returns a combined arc map. |
---|
2315 | template <typename ForwardMap, typename BackwardMap> |
---|
2316 | static CombinedArcMap<ForwardMap, BackwardMap> |
---|
2317 | combinedArcMap(ForwardMap& forward, BackwardMap& backward) { |
---|
2318 | return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward); |
---|
2319 | } |
---|
2320 | |
---|
2321 | template <typename ForwardMap, typename BackwardMap> |
---|
2322 | static CombinedArcMap<const ForwardMap, BackwardMap> |
---|
2323 | combinedArcMap(const ForwardMap& forward, BackwardMap& backward) { |
---|
2324 | return CombinedArcMap<const ForwardMap, |
---|
2325 | BackwardMap>(forward, backward); |
---|
2326 | } |
---|
2327 | |
---|
2328 | template <typename ForwardMap, typename BackwardMap> |
---|
2329 | static CombinedArcMap<ForwardMap, const BackwardMap> |
---|
2330 | combinedArcMap(ForwardMap& forward, const BackwardMap& backward) { |
---|
2331 | return CombinedArcMap<ForwardMap, |
---|
2332 | const BackwardMap>(forward, backward); |
---|
2333 | } |
---|
2334 | |
---|
2335 | template <typename ForwardMap, typename BackwardMap> |
---|
2336 | static CombinedArcMap<const ForwardMap, const BackwardMap> |
---|
2337 | combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) { |
---|
2338 | return CombinedArcMap<const ForwardMap, |
---|
2339 | const BackwardMap>(forward, backward); |
---|
2340 | } |
---|
2341 | |
---|
2342 | }; |
---|
2343 | |
---|
2344 | /// \brief Returns a read-only Undirector adaptor |
---|
2345 | /// |
---|
2346 | /// This function just returns a read-only \ref Undirector adaptor. |
---|
2347 | /// \ingroup graph_adaptors |
---|
2348 | /// \relates Undirector |
---|
2349 | template<typename DGR> |
---|
2350 | Undirector<const DGR> undirector(const DGR& digraph) { |
---|
2351 | return Undirector<const DGR>(digraph); |
---|
2352 | } |
---|
2353 | |
---|
2354 | |
---|
2355 | template <typename GR, typename DM> |
---|
2356 | class OrienterBase { |
---|
2357 | public: |
---|
2358 | |
---|
2359 | typedef GR Graph; |
---|
2360 | typedef DM DirectionMap; |
---|
2361 | |
---|
2362 | typedef typename GR::Node Node; |
---|
2363 | typedef typename GR::Edge Arc; |
---|
2364 | |
---|
2365 | void reverseArc(const Arc& arc) { |
---|
2366 | _direction->set(arc, !(*_direction)[arc]); |
---|
2367 | } |
---|
2368 | |
---|
2369 | void first(Node& i) const { _graph->first(i); } |
---|
2370 | void first(Arc& i) const { _graph->first(i); } |
---|
2371 | void firstIn(Arc& i, const Node& n) const { |
---|
2372 | bool d = true; |
---|
2373 | _graph->firstInc(i, d, n); |
---|
2374 | while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
---|
2375 | } |
---|
2376 | void firstOut(Arc& i, const Node& n ) const { |
---|
2377 | bool d = true; |
---|
2378 | _graph->firstInc(i, d, n); |
---|
2379 | while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
---|
2380 | } |
---|
2381 | |
---|
2382 | void next(Node& i) const { _graph->next(i); } |
---|
2383 | void next(Arc& i) const { _graph->next(i); } |
---|
2384 | void nextIn(Arc& i) const { |
---|
2385 | bool d = !(*_direction)[i]; |
---|
2386 | _graph->nextInc(i, d); |
---|
2387 | while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
---|
2388 | } |
---|
2389 | void nextOut(Arc& i) const { |
---|
2390 | bool d = (*_direction)[i]; |
---|
2391 | _graph->nextInc(i, d); |
---|
2392 | while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
---|
2393 | } |
---|
2394 | |
---|
2395 | Node source(const Arc& e) const { |
---|
2396 | return (*_direction)[e] ? _graph->u(e) : _graph->v(e); |
---|
2397 | } |
---|
2398 | Node target(const Arc& e) const { |
---|
2399 | return (*_direction)[e] ? _graph->v(e) : _graph->u(e); |
---|
2400 | } |
---|
2401 | |
---|
2402 | typedef NodeNumTagIndicator<Graph> NodeNumTag; |
---|
2403 | int nodeNum() const { return _graph->nodeNum(); } |
---|
2404 | |
---|
2405 | typedef EdgeNumTagIndicator<Graph> ArcNumTag; |
---|
2406 | int arcNum() const { return _graph->edgeNum(); } |
---|
2407 | |
---|
2408 | typedef FindEdgeTagIndicator<Graph> FindArcTag; |
---|
2409 | Arc findArc(const Node& u, const Node& v, |
---|
2410 | const Arc& prev = INVALID) const { |
---|
2411 | Arc arc = _graph->findEdge(u, v, prev); |
---|
2412 | while (arc != INVALID && source(arc) != u) { |
---|
2413 | arc = _graph->findEdge(u, v, arc); |
---|
2414 | } |
---|
2415 | return arc; |
---|
2416 | } |
---|
2417 | |
---|
2418 | Node addNode() { |
---|
2419 | return Node(_graph->addNode()); |
---|
2420 | } |
---|
2421 | |
---|
2422 | Arc addArc(const Node& u, const Node& v) { |
---|
2423 | Arc arc = _graph->addEdge(u, v); |
---|
2424 | _direction->set(arc, _graph->u(arc) == u); |
---|
2425 | return arc; |
---|
2426 | } |
---|
2427 | |
---|
2428 | void erase(const Node& i) { _graph->erase(i); } |
---|
2429 | void erase(const Arc& i) { _graph->erase(i); } |
---|
2430 | |
---|
2431 | void clear() { _graph->clear(); } |
---|
2432 | |
---|
2433 | int id(const Node& v) const { return _graph->id(v); } |
---|
2434 | int id(const Arc& e) const { return _graph->id(e); } |
---|
2435 | |
---|
2436 | Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } |
---|
2437 | Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } |
---|
2438 | |
---|
2439 | int maxNodeId() const { return _graph->maxNodeId(); } |
---|
2440 | int maxArcId() const { return _graph->maxEdgeId(); } |
---|
2441 | |
---|
2442 | typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
---|
2443 | NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
---|
2444 | |
---|
2445 | typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
---|
2446 | ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
---|
2447 | |
---|
2448 | template <typename V> |
---|
2449 | class NodeMap : public GR::template NodeMap<V> { |
---|
2450 | public: |
---|
2451 | |
---|
2452 | typedef typename GR::template NodeMap<V> Parent; |
---|
2453 | |
---|
2454 | explicit NodeMap(const OrienterBase<GR, DM>& adapter) |
---|
2455 | : Parent(*adapter._graph) {} |
---|
2456 | |
---|
2457 | NodeMap(const OrienterBase<GR, DM>& adapter, const V& value) |
---|
2458 | : Parent(*adapter._graph, value) {} |
---|
2459 | |
---|
2460 | private: |
---|
2461 | NodeMap& operator=(const NodeMap& cmap) { |
---|
2462 | return operator=<NodeMap>(cmap); |
---|
2463 | } |
---|
2464 | |
---|
2465 | template <typename CMap> |
---|
2466 | NodeMap& operator=(const CMap& cmap) { |
---|
2467 | Parent::operator=(cmap); |
---|
2468 | return *this; |
---|
2469 | } |
---|
2470 | |
---|
2471 | }; |
---|
2472 | |
---|
2473 | template <typename V> |
---|
2474 | class ArcMap : public GR::template EdgeMap<V> { |
---|
2475 | public: |
---|
2476 | |
---|
2477 | typedef typename Graph::template EdgeMap<V> Parent; |
---|
2478 | |
---|
2479 | explicit ArcMap(const OrienterBase<GR, DM>& adapter) |
---|
2480 | : Parent(*adapter._graph) { } |
---|
2481 | |
---|
2482 | ArcMap(const OrienterBase<GR, DM>& adapter, const V& value) |
---|
2483 | : Parent(*adapter._graph, value) { } |
---|
2484 | |
---|
2485 | private: |
---|
2486 | ArcMap& operator=(const ArcMap& cmap) { |
---|
2487 | return operator=<ArcMap>(cmap); |
---|
2488 | } |
---|
2489 | |
---|
2490 | template <typename CMap> |
---|
2491 | ArcMap& operator=(const CMap& cmap) { |
---|
2492 | Parent::operator=(cmap); |
---|
2493 | return *this; |
---|
2494 | } |
---|
2495 | }; |
---|
2496 | |
---|
2497 | |
---|
2498 | |
---|
2499 | protected: |
---|
2500 | Graph* _graph; |
---|
2501 | DM* _direction; |
---|
2502 | |
---|
2503 | void initialize(GR& graph, DM& direction) { |
---|
2504 | _graph = &graph; |
---|
2505 | _direction = &direction; |
---|
2506 | } |
---|
2507 | |
---|
2508 | }; |
---|
2509 | |
---|
2510 | /// \ingroup graph_adaptors |
---|
2511 | /// |
---|
2512 | /// \brief Adaptor class for orienting the edges of a graph to get a digraph |
---|
2513 | /// |
---|
2514 | /// Orienter adaptor can be used for orienting the edges of a graph to |
---|
2515 | /// get a digraph. A \c bool edge map of the underlying graph must be |
---|
2516 | /// specified, which define the direction of the arcs in the adaptor. |
---|
2517 | /// The arcs can be easily reversed by the \c reverseArc() member function |
---|
2518 | /// of the adaptor. |
---|
2519 | /// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
2520 | /// |
---|
2521 | /// The adapted graph can also be modified through this adaptor |
---|
2522 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
2523 | /// parameter is set to be \c const. |
---|
2524 | /// |
---|
2525 | /// \tparam GR The type of the adapted graph. |
---|
2526 | /// It must conform to the \ref concepts::Graph "Graph" concept. |
---|
2527 | /// It can also be specified to be \c const. |
---|
2528 | /// \tparam DM The type of the direction map. |
---|
2529 | /// It must be a \c bool (or convertible) edge map of the |
---|
2530 | /// adapted graph. The default type is |
---|
2531 | /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
---|
2532 | /// |
---|
2533 | /// \note The \c Node type of this adaptor and the adapted graph are |
---|
2534 | /// convertible to each other, moreover the \c Arc type of the adaptor |
---|
2535 | /// and the \c Edge type of the adapted graph are also convertible to |
---|
2536 | /// each other. |
---|
2537 | #ifdef DOXYGEN |
---|
2538 | template<typename GR, |
---|
2539 | typename DM> |
---|
2540 | class Orienter { |
---|
2541 | #else |
---|
2542 | template<typename GR, |
---|
2543 | typename DM = typename GR::template EdgeMap<bool> > |
---|
2544 | class Orienter : |
---|
2545 | public DigraphAdaptorExtender<OrienterBase<GR, DM> > { |
---|
2546 | #endif |
---|
2547 | public: |
---|
2548 | |
---|
2549 | /// The type of the adapted graph. |
---|
2550 | typedef GR Graph; |
---|
2551 | /// The type of the direction edge map. |
---|
2552 | typedef DM DirectionMap; |
---|
2553 | |
---|
2554 | typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent; |
---|
2555 | typedef typename Parent::Arc Arc; |
---|
2556 | protected: |
---|
2557 | Orienter() { } |
---|
2558 | public: |
---|
2559 | |
---|
2560 | /// \brief Constructor |
---|
2561 | /// |
---|
2562 | /// Constructor of the adaptor. |
---|
2563 | Orienter(GR& graph, DM& direction) { |
---|
2564 | Parent::initialize(graph, direction); |
---|
2565 | } |
---|
2566 | |
---|
2567 | /// \brief Reverses the given arc |
---|
2568 | /// |
---|
2569 | /// This function reverses the given arc. |
---|
2570 | /// It is done by simply negate the assigned value of \c a |
---|
2571 | /// in the direction map. |
---|
2572 | void reverseArc(const Arc& a) { |
---|
2573 | Parent::reverseArc(a); |
---|
2574 | } |
---|
2575 | }; |
---|
2576 | |
---|
2577 | /// \brief Returns a read-only Orienter adaptor |
---|
2578 | /// |
---|
2579 | /// This function just returns a read-only \ref Orienter adaptor. |
---|
2580 | /// \ingroup graph_adaptors |
---|
2581 | /// \relates Orienter |
---|
2582 | template<typename GR, typename DM> |
---|
2583 | Orienter<const GR, DM> |
---|
2584 | orienter(const GR& graph, DM& direction) { |
---|
2585 | return Orienter<const GR, DM>(graph, direction); |
---|
2586 | } |
---|
2587 | |
---|
2588 | template<typename GR, typename DM> |
---|
2589 | Orienter<const GR, const DM> |
---|
2590 | orienter(const GR& graph, const DM& direction) { |
---|
2591 | return Orienter<const GR, const DM>(graph, direction); |
---|
2592 | } |
---|
2593 | |
---|
2594 | namespace _adaptor_bits { |
---|
2595 | |
---|
2596 | template <typename DGR, typename CM, typename FM, typename TL> |
---|
2597 | class ResForwardFilter { |
---|
2598 | public: |
---|
2599 | |
---|
2600 | typedef typename DGR::Arc Key; |
---|
2601 | typedef bool Value; |
---|
2602 | |
---|
2603 | private: |
---|
2604 | |
---|
2605 | const CM* _capacity; |
---|
2606 | const FM* _flow; |
---|
2607 | TL _tolerance; |
---|
2608 | |
---|
2609 | public: |
---|
2610 | |
---|
2611 | ResForwardFilter(const CM& capacity, const FM& flow, |
---|
2612 | const TL& tolerance = TL()) |
---|
2613 | : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
---|
2614 | |
---|
2615 | bool operator[](const typename DGR::Arc& a) const { |
---|
2616 | return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); |
---|
2617 | } |
---|
2618 | }; |
---|
2619 | |
---|
2620 | template<typename DGR,typename CM, typename FM, typename TL> |
---|
2621 | class ResBackwardFilter { |
---|
2622 | public: |
---|
2623 | |
---|
2624 | typedef typename DGR::Arc Key; |
---|
2625 | typedef bool Value; |
---|
2626 | |
---|
2627 | private: |
---|
2628 | |
---|
2629 | const CM* _capacity; |
---|
2630 | const FM* _flow; |
---|
2631 | TL _tolerance; |
---|
2632 | |
---|
2633 | public: |
---|
2634 | |
---|
2635 | ResBackwardFilter(const CM& capacity, const FM& flow, |
---|
2636 | const TL& tolerance = TL()) |
---|
2637 | : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
---|
2638 | |
---|
2639 | bool operator[](const typename DGR::Arc& a) const { |
---|
2640 | return _tolerance.positive((*_flow)[a]); |
---|
2641 | } |
---|
2642 | }; |
---|
2643 | |
---|
2644 | } |
---|
2645 | |
---|
2646 | /// \ingroup graph_adaptors |
---|
2647 | /// |
---|
2648 | /// \brief Adaptor class for composing the residual digraph for directed |
---|
2649 | /// flow and circulation problems. |
---|
2650 | /// |
---|
2651 | /// ResidualDigraph can be used for composing the \e residual digraph |
---|
2652 | /// for directed flow and circulation problems. Let \f$ G=(V, A) \f$ |
---|
2653 | /// be a directed graph and let \f$ F \f$ be a number type. |
---|
2654 | /// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
---|
2655 | /// This adaptor implements a digraph structure with node set \f$ V \f$ |
---|
2656 | /// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
---|
2657 | /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
---|
2658 | /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
---|
2659 | /// called residual digraph. |
---|
2660 | /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
---|
2661 | /// multiplicities are counted, i.e. the adaptor has exactly |
---|
2662 | /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
---|
2663 | /// arcs). |
---|
2664 | /// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
2665 | /// |
---|
2666 | /// \tparam DGR The type of the adapted digraph. |
---|
2667 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
2668 | /// It is implicitly \c const. |
---|
2669 | /// \tparam CM The type of the capacity map. |
---|
2670 | /// It must be an arc map of some numerical type, which defines |
---|
2671 | /// the capacities in the flow problem. It is implicitly \c const. |
---|
2672 | /// The default type is |
---|
2673 | /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
---|
2674 | /// \tparam FM The type of the flow map. |
---|
2675 | /// It must be an arc map of some numerical type, which defines |
---|
2676 | /// the flow values in the flow problem. The default type is \c CM. |
---|
2677 | /// \tparam TL The tolerance type for handling inexact computation. |
---|
2678 | /// The default tolerance type depends on the value type of the |
---|
2679 | /// capacity map. |
---|
2680 | /// |
---|
2681 | /// \note This adaptor is implemented using Undirector and FilterArcs |
---|
2682 | /// adaptors. |
---|
2683 | /// |
---|
2684 | /// \note The \c Node type of this adaptor and the adapted digraph are |
---|
2685 | /// convertible to each other, moreover the \c Arc type of the adaptor |
---|
2686 | /// is convertible to the \c Arc type of the adapted digraph. |
---|
2687 | #ifdef DOXYGEN |
---|
2688 | template<typename DGR, typename CM, typename FM, typename TL> |
---|
2689 | class ResidualDigraph |
---|
2690 | #else |
---|
2691 | template<typename DGR, |
---|
2692 | typename CM = typename DGR::template ArcMap<int>, |
---|
2693 | typename FM = CM, |
---|
2694 | typename TL = Tolerance<typename CM::Value> > |
---|
2695 | class ResidualDigraph |
---|
2696 | : public SubDigraph< |
---|
2697 | Undirector<const DGR>, |
---|
2698 | ConstMap<typename DGR::Node, Const<bool, true> >, |
---|
2699 | typename Undirector<const DGR>::template CombinedArcMap< |
---|
2700 | _adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
---|
2701 | _adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
---|
2702 | #endif |
---|
2703 | { |
---|
2704 | public: |
---|
2705 | |
---|
2706 | /// The type of the underlying digraph. |
---|
2707 | typedef DGR Digraph; |
---|
2708 | /// The type of the capacity map. |
---|
2709 | typedef CM CapacityMap; |
---|
2710 | /// The type of the flow map. |
---|
2711 | typedef FM FlowMap; |
---|
2712 | /// The tolerance type. |
---|
2713 | typedef TL Tolerance; |
---|
2714 | |
---|
2715 | typedef typename CapacityMap::Value Value; |
---|
2716 | typedef ResidualDigraph Adaptor; |
---|
2717 | |
---|
2718 | protected: |
---|
2719 | |
---|
2720 | typedef Undirector<const Digraph> Undirected; |
---|
2721 | |
---|
2722 | typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter; |
---|
2723 | |
---|
2724 | typedef _adaptor_bits::ResForwardFilter<const DGR, CM, |
---|
2725 | FM, TL> ForwardFilter; |
---|
2726 | |
---|
2727 | typedef _adaptor_bits::ResBackwardFilter<const DGR, CM, |
---|
2728 | FM, TL> BackwardFilter; |
---|
2729 | |
---|
2730 | typedef typename Undirected:: |
---|
2731 | template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
---|
2732 | |
---|
2733 | typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent; |
---|
2734 | |
---|
2735 | const CapacityMap* _capacity; |
---|
2736 | FlowMap* _flow; |
---|
2737 | |
---|
2738 | Undirected _graph; |
---|
2739 | NodeFilter _node_filter; |
---|
2740 | ForwardFilter _forward_filter; |
---|
2741 | BackwardFilter _backward_filter; |
---|
2742 | ArcFilter _arc_filter; |
---|
2743 | |
---|
2744 | public: |
---|
2745 | |
---|
2746 | /// \brief Constructor |
---|
2747 | /// |
---|
2748 | /// Constructor of the residual digraph adaptor. The parameters are the |
---|
2749 | /// digraph, the capacity map, the flow map, and a tolerance object. |
---|
2750 | ResidualDigraph(const DGR& digraph, const CM& capacity, |
---|
2751 | FM& flow, const TL& tolerance = Tolerance()) |
---|
2752 | : Parent(), _capacity(&capacity), _flow(&flow), |
---|
2753 | _graph(digraph), _node_filter(), |
---|
2754 | _forward_filter(capacity, flow, tolerance), |
---|
2755 | _backward_filter(capacity, flow, tolerance), |
---|
2756 | _arc_filter(_forward_filter, _backward_filter) |
---|
2757 | { |
---|
2758 | Parent::initialize(_graph, _node_filter, _arc_filter); |
---|
2759 | } |
---|
2760 | |
---|
2761 | typedef typename Parent::Arc Arc; |
---|
2762 | |
---|
2763 | /// \brief Returns the residual capacity of the given arc. |
---|
2764 | /// |
---|
2765 | /// Returns the residual capacity of the given arc. |
---|
2766 | Value residualCapacity(const Arc& a) const { |
---|
2767 | if (Undirected::direction(a)) { |
---|
2768 | return (*_capacity)[a] - (*_flow)[a]; |
---|
2769 | } else { |
---|
2770 | return (*_flow)[a]; |
---|
2771 | } |
---|
2772 | } |
---|
2773 | |
---|
2774 | /// \brief Augments on the given arc in the residual digraph. |
---|
2775 | /// |
---|
2776 | /// Augments on the given arc in the residual digraph. It increases |
---|
2777 | /// or decreases the flow value on the original arc according to the |
---|
2778 | /// direction of the residual arc. |
---|
2779 | void augment(const Arc& a, const Value& v) const { |
---|
2780 | if (Undirected::direction(a)) { |
---|
2781 | _flow->set(a, (*_flow)[a] + v); |
---|
2782 | } else { |
---|
2783 | _flow->set(a, (*_flow)[a] - v); |
---|
2784 | } |
---|
2785 | } |
---|
2786 | |
---|
2787 | /// \brief Returns \c true if the given residual arc is a forward arc. |
---|
2788 | /// |
---|
2789 | /// Returns \c true if the given residual arc has the same orientation |
---|
2790 | /// as the original arc, i.e. it is a so called forward arc. |
---|
2791 | static bool forward(const Arc& a) { |
---|
2792 | return Undirected::direction(a); |
---|
2793 | } |
---|
2794 | |
---|
2795 | /// \brief Returns \c true if the given residual arc is a backward arc. |
---|
2796 | /// |
---|
2797 | /// Returns \c true if the given residual arc has the opposite orientation |
---|
2798 | /// than the original arc, i.e. it is a so called backward arc. |
---|
2799 | static bool backward(const Arc& a) { |
---|
2800 | return !Undirected::direction(a); |
---|
2801 | } |
---|
2802 | |
---|
2803 | /// \brief Returns the forward oriented residual arc. |
---|
2804 | /// |
---|
2805 | /// Returns the forward oriented residual arc related to the given |
---|
2806 | /// arc of the underlying digraph. |
---|
2807 | static Arc forward(const typename Digraph::Arc& a) { |
---|
2808 | return Undirected::direct(a, true); |
---|
2809 | } |
---|
2810 | |
---|
2811 | /// \brief Returns the backward oriented residual arc. |
---|
2812 | /// |
---|
2813 | /// Returns the backward oriented residual arc related to the given |
---|
2814 | /// arc of the underlying digraph. |
---|
2815 | static Arc backward(const typename Digraph::Arc& a) { |
---|
2816 | return Undirected::direct(a, false); |
---|
2817 | } |
---|
2818 | |
---|
2819 | /// \brief Residual capacity map. |
---|
2820 | /// |
---|
2821 | /// This map adaptor class can be used for obtaining the residual |
---|
2822 | /// capacities as an arc map of the residual digraph. |
---|
2823 | /// Its value type is inherited from the capacity map. |
---|
2824 | class ResidualCapacity { |
---|
2825 | protected: |
---|
2826 | const Adaptor* _adaptor; |
---|
2827 | public: |
---|
2828 | /// The key type of the map |
---|
2829 | typedef Arc Key; |
---|
2830 | /// The value type of the map |
---|
2831 | typedef typename CapacityMap::Value Value; |
---|
2832 | |
---|
2833 | /// Constructor |
---|
2834 | ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
---|
2835 | : _adaptor(&adaptor) {} |
---|
2836 | |
---|
2837 | /// Returns the value associated with the given residual arc |
---|
2838 | Value operator[](const Arc& a) const { |
---|
2839 | return _adaptor->residualCapacity(a); |
---|
2840 | } |
---|
2841 | |
---|
2842 | }; |
---|
2843 | |
---|
2844 | /// \brief Returns a residual capacity map |
---|
2845 | /// |
---|
2846 | /// This function just returns a residual capacity map. |
---|
2847 | ResidualCapacity residualCapacity() const { |
---|
2848 | return ResidualCapacity(*this); |
---|
2849 | } |
---|
2850 | |
---|
2851 | }; |
---|
2852 | |
---|
2853 | /// \brief Returns a (read-only) Residual adaptor |
---|
2854 | /// |
---|
2855 | /// This function just returns a (read-only) \ref ResidualDigraph adaptor. |
---|
2856 | /// \ingroup graph_adaptors |
---|
2857 | /// \relates ResidualDigraph |
---|
2858 | template<typename DGR, typename CM, typename FM> |
---|
2859 | ResidualDigraph<DGR, CM, FM> |
---|
2860 | residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) { |
---|
2861 | return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map); |
---|
2862 | } |
---|
2863 | |
---|
2864 | |
---|
2865 | template <typename DGR> |
---|
2866 | class SplitNodesBase { |
---|
2867 | public: |
---|
2868 | |
---|
2869 | typedef DGR Digraph; |
---|
2870 | typedef DigraphAdaptorBase<const DGR> Parent; |
---|
2871 | typedef SplitNodesBase Adaptor; |
---|
2872 | |
---|
2873 | typedef typename DGR::Node DigraphNode; |
---|
2874 | typedef typename DGR::Arc DigraphArc; |
---|
2875 | |
---|
2876 | class Node; |
---|
2877 | class Arc; |
---|
2878 | |
---|
2879 | private: |
---|
2880 | |
---|
2881 | template <typename T> class NodeMapBase; |
---|
2882 | template <typename T> class ArcMapBase; |
---|
2883 | |
---|
2884 | public: |
---|
2885 | |
---|
2886 | class Node : public DigraphNode { |
---|
2887 | friend class SplitNodesBase; |
---|
2888 | template <typename T> friend class NodeMapBase; |
---|
2889 | private: |
---|
2890 | |
---|
2891 | bool _in; |
---|
2892 | Node(DigraphNode node, bool in) |
---|
2893 | : DigraphNode(node), _in(in) {} |
---|
2894 | |
---|
2895 | public: |
---|
2896 | |
---|
2897 | Node() {} |
---|
2898 | Node(Invalid) : DigraphNode(INVALID), _in(true) {} |
---|
2899 | |
---|
2900 | bool operator==(const Node& node) const { |
---|
2901 | return DigraphNode::operator==(node) && _in == node._in; |
---|
2902 | } |
---|
2903 | |
---|
2904 | bool operator!=(const Node& node) const { |
---|
2905 | return !(*this == node); |
---|
2906 | } |
---|
2907 | |
---|
2908 | bool operator<(const Node& node) const { |
---|
2909 | return DigraphNode::operator<(node) || |
---|
2910 | (DigraphNode::operator==(node) && _in < node._in); |
---|
2911 | } |
---|
2912 | }; |
---|
2913 | |
---|
2914 | class Arc { |
---|
2915 | friend class SplitNodesBase; |
---|
2916 | template <typename T> friend class ArcMapBase; |
---|
2917 | private: |
---|
2918 | typedef BiVariant<DigraphArc, DigraphNode> ArcImpl; |
---|
2919 | |
---|
2920 | explicit Arc(const DigraphArc& arc) : _item(arc) {} |
---|
2921 | explicit Arc(const DigraphNode& node) : _item(node) {} |
---|
2922 | |
---|
2923 | ArcImpl _item; |
---|
2924 | |
---|
2925 | public: |
---|
2926 | Arc() {} |
---|
2927 | Arc(Invalid) : _item(DigraphArc(INVALID)) {} |
---|
2928 | |
---|
2929 | bool operator==(const Arc& arc) const { |
---|
2930 | if (_item.firstState()) { |
---|
2931 | if (arc._item.firstState()) { |
---|
2932 | return _item.first() == arc._item.first(); |
---|
2933 | } |
---|
2934 | } else { |
---|
2935 | if (arc._item.secondState()) { |
---|
2936 | return _item.second() == arc._item.second(); |
---|
2937 | } |
---|
2938 | } |
---|
2939 | return false; |
---|
2940 | } |
---|
2941 | |
---|
2942 | bool operator!=(const Arc& arc) const { |
---|
2943 | return !(*this == arc); |
---|
2944 | } |
---|
2945 | |
---|
2946 | bool operator<(const Arc& arc) const { |
---|
2947 | if (_item.firstState()) { |
---|
2948 | if (arc._item.firstState()) { |
---|
2949 | return _item.first() < arc._item.first(); |
---|
2950 | } |
---|
2951 | return false; |
---|
2952 | } else { |
---|
2953 | if (arc._item.secondState()) { |
---|
2954 | return _item.second() < arc._item.second(); |
---|
2955 | } |
---|
2956 | return true; |
---|
2957 | } |
---|
2958 | } |
---|
2959 | |
---|
2960 | operator DigraphArc() const { return _item.first(); } |
---|
2961 | operator DigraphNode() const { return _item.second(); } |
---|
2962 | |
---|
2963 | }; |
---|
2964 | |
---|
2965 | void first(Node& n) const { |
---|
2966 | _digraph->first(n); |
---|
2967 | n._in = true; |
---|
2968 | } |
---|
2969 | |
---|
2970 | void next(Node& n) const { |
---|
2971 | if (n._in) { |
---|
2972 | n._in = false; |
---|
2973 | } else { |
---|
2974 | n._in = true; |
---|
2975 | _digraph->next(n); |
---|
2976 | } |
---|
2977 | } |
---|
2978 | |
---|
2979 | void first(Arc& e) const { |
---|
2980 | e._item.setSecond(); |
---|
2981 | _digraph->first(e._item.second()); |
---|
2982 | if (e._item.second() == INVALID) { |
---|
2983 | e._item.setFirst(); |
---|
2984 | _digraph->first(e._item.first()); |
---|
2985 | } |
---|
2986 | } |
---|
2987 | |
---|
2988 | void next(Arc& e) const { |
---|
2989 | if (e._item.secondState()) { |
---|
2990 | _digraph->next(e._item.second()); |
---|
2991 | if (e._item.second() == INVALID) { |
---|
2992 | e._item.setFirst(); |
---|
2993 | _digraph->first(e._item.first()); |
---|
2994 | } |
---|
2995 | } else { |
---|
2996 | _digraph->next(e._item.first()); |
---|
2997 | } |
---|
2998 | } |
---|
2999 | |
---|
3000 | void firstOut(Arc& e, const Node& n) const { |
---|
3001 | if (n._in) { |
---|
3002 | e._item.setSecond(n); |
---|
3003 | } else { |
---|
3004 | e._item.setFirst(); |
---|
3005 | _digraph->firstOut(e._item.first(), n); |
---|
3006 | } |
---|
3007 | } |
---|
3008 | |
---|
3009 | void nextOut(Arc& e) const { |
---|
3010 | if (!e._item.firstState()) { |
---|
3011 | e._item.setFirst(INVALID); |
---|
3012 | } else { |
---|
3013 | _digraph->nextOut(e._item.first()); |
---|
3014 | } |
---|
3015 | } |
---|
3016 | |
---|
3017 | void firstIn(Arc& e, const Node& n) const { |
---|
3018 | if (!n._in) { |
---|
3019 | e._item.setSecond(n); |
---|
3020 | } else { |
---|
3021 | e._item.setFirst(); |
---|
3022 | _digraph->firstIn(e._item.first(), n); |
---|
3023 | } |
---|
3024 | } |
---|
3025 | |
---|
3026 | void nextIn(Arc& e) const { |
---|
3027 | if (!e._item.firstState()) { |
---|
3028 | e._item.setFirst(INVALID); |
---|
3029 | } else { |
---|
3030 | _digraph->nextIn(e._item.first()); |
---|
3031 | } |
---|
3032 | } |
---|
3033 | |
---|
3034 | Node source(const Arc& e) const { |
---|
3035 | if (e._item.firstState()) { |
---|
3036 | return Node(_digraph->source(e._item.first()), false); |
---|
3037 | } else { |
---|
3038 | return Node(e._item.second(), true); |
---|
3039 | } |
---|
3040 | } |
---|
3041 | |
---|
3042 | Node target(const Arc& e) const { |
---|
3043 | if (e._item.firstState()) { |
---|
3044 | return Node(_digraph->target(e._item.first()), true); |
---|
3045 | } else { |
---|
3046 | return Node(e._item.second(), false); |
---|
3047 | } |
---|
3048 | } |
---|
3049 | |
---|
3050 | int id(const Node& n) const { |
---|
3051 | return (_digraph->id(n) << 1) | (n._in ? 0 : 1); |
---|
3052 | } |
---|
3053 | Node nodeFromId(int ix) const { |
---|
3054 | return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); |
---|
3055 | } |
---|
3056 | int maxNodeId() const { |
---|
3057 | return 2 * _digraph->maxNodeId() + 1; |
---|
3058 | } |
---|
3059 | |
---|
3060 | int id(const Arc& e) const { |
---|
3061 | if (e._item.firstState()) { |
---|
3062 | return _digraph->id(e._item.first()) << 1; |
---|
3063 | } else { |
---|
3064 | return (_digraph->id(e._item.second()) << 1) | 1; |
---|
3065 | } |
---|
3066 | } |
---|
3067 | Arc arcFromId(int ix) const { |
---|
3068 | if ((ix & 1) == 0) { |
---|
3069 | return Arc(_digraph->arcFromId(ix >> 1)); |
---|
3070 | } else { |
---|
3071 | return Arc(_digraph->nodeFromId(ix >> 1)); |
---|
3072 | } |
---|
3073 | } |
---|
3074 | int maxArcId() const { |
---|
3075 | return std::max(_digraph->maxNodeId() << 1, |
---|
3076 | (_digraph->maxArcId() << 1) | 1); |
---|
3077 | } |
---|
3078 | |
---|
3079 | static bool inNode(const Node& n) { |
---|
3080 | return n._in; |
---|
3081 | } |
---|
3082 | |
---|
3083 | static bool outNode(const Node& n) { |
---|
3084 | return !n._in; |
---|
3085 | } |
---|
3086 | |
---|
3087 | static bool origArc(const Arc& e) { |
---|
3088 | return e._item.firstState(); |
---|
3089 | } |
---|
3090 | |
---|
3091 | static bool bindArc(const Arc& e) { |
---|
3092 | return e._item.secondState(); |
---|
3093 | } |
---|
3094 | |
---|
3095 | static Node inNode(const DigraphNode& n) { |
---|
3096 | return Node(n, true); |
---|
3097 | } |
---|
3098 | |
---|
3099 | static Node outNode(const DigraphNode& n) { |
---|
3100 | return Node(n, false); |
---|
3101 | } |
---|
3102 | |
---|
3103 | static Arc arc(const DigraphNode& n) { |
---|
3104 | return Arc(n); |
---|
3105 | } |
---|
3106 | |
---|
3107 | static Arc arc(const DigraphArc& e) { |
---|
3108 | return Arc(e); |
---|
3109 | } |
---|
3110 | |
---|
3111 | typedef True NodeNumTag; |
---|
3112 | int nodeNum() const { |
---|
3113 | return 2 * countNodes(*_digraph); |
---|
3114 | } |
---|
3115 | |
---|
3116 | typedef True ArcNumTag; |
---|
3117 | int arcNum() const { |
---|
3118 | return countArcs(*_digraph) + countNodes(*_digraph); |
---|
3119 | } |
---|
3120 | |
---|
3121 | typedef True FindArcTag; |
---|
3122 | Arc findArc(const Node& u, const Node& v, |
---|
3123 | const Arc& prev = INVALID) const { |
---|
3124 | if (inNode(u) && outNode(v)) { |
---|
3125 | if (static_cast<const DigraphNode&>(u) == |
---|
3126 | static_cast<const DigraphNode&>(v) && prev == INVALID) { |
---|
3127 | return Arc(u); |
---|
3128 | } |
---|
3129 | } |
---|
3130 | else if (outNode(u) && inNode(v)) { |
---|
3131 | return Arc(::lemon::findArc(*_digraph, u, v, prev)); |
---|
3132 | } |
---|
3133 | return INVALID; |
---|
3134 | } |
---|
3135 | |
---|
3136 | private: |
---|
3137 | |
---|
3138 | template <typename V> |
---|
3139 | class NodeMapBase |
---|
3140 | : public MapTraits<typename Parent::template NodeMap<V> > { |
---|
3141 | typedef typename Parent::template NodeMap<V> NodeImpl; |
---|
3142 | public: |
---|
3143 | typedef Node Key; |
---|
3144 | typedef V Value; |
---|
3145 | typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag; |
---|
3146 | typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue; |
---|
3147 | typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue; |
---|
3148 | typedef typename MapTraits<NodeImpl>::ReturnValue Reference; |
---|
3149 | typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference; |
---|
3150 | |
---|
3151 | NodeMapBase(const SplitNodesBase<DGR>& adaptor) |
---|
3152 | : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} |
---|
3153 | NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
3154 | : _in_map(*adaptor._digraph, value), |
---|
3155 | _out_map(*adaptor._digraph, value) {} |
---|
3156 | |
---|
3157 | void set(const Node& key, const V& val) { |
---|
3158 | if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); } |
---|
3159 | else {_out_map.set(key, val); } |
---|
3160 | } |
---|
3161 | |
---|
3162 | ReturnValue operator[](const Node& key) { |
---|
3163 | if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; } |
---|
3164 | else { return _out_map[key]; } |
---|
3165 | } |
---|
3166 | |
---|
3167 | ConstReturnValue operator[](const Node& key) const { |
---|
3168 | if (Adaptor::inNode(key)) { return _in_map[key]; } |
---|
3169 | else { return _out_map[key]; } |
---|
3170 | } |
---|
3171 | |
---|
3172 | private: |
---|
3173 | NodeImpl _in_map, _out_map; |
---|
3174 | }; |
---|
3175 | |
---|
3176 | template <typename V> |
---|
3177 | class ArcMapBase |
---|
3178 | : public MapTraits<typename Parent::template ArcMap<V> > { |
---|
3179 | typedef typename Parent::template ArcMap<V> ArcImpl; |
---|
3180 | typedef typename Parent::template NodeMap<V> NodeImpl; |
---|
3181 | public: |
---|
3182 | typedef Arc Key; |
---|
3183 | typedef V Value; |
---|
3184 | typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag; |
---|
3185 | typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue; |
---|
3186 | typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue; |
---|
3187 | typedef typename MapTraits<ArcImpl>::ReturnValue Reference; |
---|
3188 | typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference; |
---|
3189 | |
---|
3190 | ArcMapBase(const SplitNodesBase<DGR>& adaptor) |
---|
3191 | : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} |
---|
3192 | ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
3193 | : _arc_map(*adaptor._digraph, value), |
---|
3194 | _node_map(*adaptor._digraph, value) {} |
---|
3195 | |
---|
3196 | void set(const Arc& key, const V& val) { |
---|
3197 | if (SplitNodesBase<DGR>::origArc(key)) { |
---|
3198 | _arc_map.set(static_cast<const DigraphArc&>(key), val); |
---|
3199 | } else { |
---|
3200 | _node_map.set(static_cast<const DigraphNode&>(key), val); |
---|
3201 | } |
---|
3202 | } |
---|
3203 | |
---|
3204 | ReturnValue operator[](const Arc& key) { |
---|
3205 | if (SplitNodesBase<DGR>::origArc(key)) { |
---|
3206 | return _arc_map[static_cast<const DigraphArc&>(key)]; |
---|
3207 | } else { |
---|
3208 | return _node_map[static_cast<const DigraphNode&>(key)]; |
---|
3209 | } |
---|
3210 | } |
---|
3211 | |
---|
3212 | ConstReturnValue operator[](const Arc& key) const { |
---|
3213 | if (SplitNodesBase<DGR>::origArc(key)) { |
---|
3214 | return _arc_map[static_cast<const DigraphArc&>(key)]; |
---|
3215 | } else { |
---|
3216 | return _node_map[static_cast<const DigraphNode&>(key)]; |
---|
3217 | } |
---|
3218 | } |
---|
3219 | |
---|
3220 | private: |
---|
3221 | ArcImpl _arc_map; |
---|
3222 | NodeImpl _node_map; |
---|
3223 | }; |
---|
3224 | |
---|
3225 | public: |
---|
3226 | |
---|
3227 | template <typename V> |
---|
3228 | class NodeMap |
---|
3229 | : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > |
---|
3230 | { |
---|
3231 | public: |
---|
3232 | typedef V Value; |
---|
3233 | typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<Value> > Parent; |
---|
3234 | |
---|
3235 | NodeMap(const SplitNodesBase<DGR>& adaptor) |
---|
3236 | : Parent(adaptor) {} |
---|
3237 | |
---|
3238 | NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
3239 | : Parent(adaptor, value) {} |
---|
3240 | |
---|
3241 | private: |
---|
3242 | NodeMap& operator=(const NodeMap& cmap) { |
---|
3243 | return operator=<NodeMap>(cmap); |
---|
3244 | } |
---|
3245 | |
---|
3246 | template <typename CMap> |
---|
3247 | NodeMap& operator=(const CMap& cmap) { |
---|
3248 | Parent::operator=(cmap); |
---|
3249 | return *this; |
---|
3250 | } |
---|
3251 | }; |
---|
3252 | |
---|
3253 | template <typename V> |
---|
3254 | class ArcMap |
---|
3255 | : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > |
---|
3256 | { |
---|
3257 | public: |
---|
3258 | typedef V Value; |
---|
3259 | typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<Value> > Parent; |
---|
3260 | |
---|
3261 | ArcMap(const SplitNodesBase<DGR>& adaptor) |
---|
3262 | : Parent(adaptor) {} |
---|
3263 | |
---|
3264 | ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
3265 | : Parent(adaptor, value) {} |
---|
3266 | |
---|
3267 | private: |
---|
3268 | ArcMap& operator=(const ArcMap& cmap) { |
---|
3269 | return operator=<ArcMap>(cmap); |
---|
3270 | } |
---|
3271 | |
---|
3272 | template <typename CMap> |
---|
3273 | ArcMap& operator=(const CMap& cmap) { |
---|
3274 | Parent::operator=(cmap); |
---|
3275 | return *this; |
---|
3276 | } |
---|
3277 | }; |
---|
3278 | |
---|
3279 | protected: |
---|
3280 | |
---|
3281 | SplitNodesBase() : _digraph(0) {} |
---|
3282 | |
---|
3283 | DGR* _digraph; |
---|
3284 | |
---|
3285 | void initialize(Digraph& digraph) { |
---|
3286 | _digraph = &digraph; |
---|
3287 | } |
---|
3288 | |
---|
3289 | }; |
---|
3290 | |
---|
3291 | /// \ingroup graph_adaptors |
---|
3292 | /// |
---|
3293 | /// \brief Adaptor class for splitting the nodes of a digraph. |
---|
3294 | /// |
---|
3295 | /// SplitNodes adaptor can be used for splitting each node into an |
---|
3296 | /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor |
---|
3297 | /// replaces each node \f$ u \f$ in the digraph with two nodes, |
---|
3298 | /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$. |
---|
3299 | /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the |
---|
3300 | /// new target of the arc will be \f$ u_{in} \f$ and similarly the |
---|
3301 | /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$. |
---|
3302 | /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$ |
---|
3303 | /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph. |
---|
3304 | /// |
---|
3305 | /// The aim of this class is running an algorithm with respect to node |
---|
3306 | /// costs or capacities if the algorithm considers only arc costs or |
---|
3307 | /// capacities directly. |
---|
3308 | /// In this case you can use \c SplitNodes adaptor, and set the node |
---|
3309 | /// costs/capacities of the original digraph to the \e bind \e arcs |
---|
3310 | /// in the adaptor. |
---|
3311 | /// |
---|
3312 | /// \tparam DGR The type of the adapted digraph. |
---|
3313 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
3314 | /// It is implicitly \c const. |
---|
3315 | /// |
---|
3316 | /// \note The \c Node type of this adaptor is converible to the \c Node |
---|
3317 | /// type of the adapted digraph. |
---|
3318 | template <typename DGR> |
---|
3319 | #ifdef DOXYGEN |
---|
3320 | class SplitNodes { |
---|
3321 | #else |
---|
3322 | class SplitNodes |
---|
3323 | : public DigraphAdaptorExtender<SplitNodesBase<const DGR> > { |
---|
3324 | #endif |
---|
3325 | public: |
---|
3326 | typedef DGR Digraph; |
---|
3327 | typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent; |
---|
3328 | |
---|
3329 | typedef typename DGR::Node DigraphNode; |
---|
3330 | typedef typename DGR::Arc DigraphArc; |
---|
3331 | |
---|
3332 | typedef typename Parent::Node Node; |
---|
3333 | typedef typename Parent::Arc Arc; |
---|
3334 | |
---|
3335 | /// \brief Constructor |
---|
3336 | /// |
---|
3337 | /// Constructor of the adaptor. |
---|
3338 | SplitNodes(const DGR& g) { |
---|
3339 | Parent::initialize(g); |
---|
3340 | } |
---|
3341 | |
---|
3342 | /// \brief Returns \c true if the given node is an in-node. |
---|
3343 | /// |
---|
3344 | /// Returns \c true if the given node is an in-node. |
---|
3345 | static bool inNode(const Node& n) { |
---|
3346 | return Parent::inNode(n); |
---|
3347 | } |
---|
3348 | |
---|
3349 | /// \brief Returns \c true if the given node is an out-node. |
---|
3350 | /// |
---|
3351 | /// Returns \c true if the given node is an out-node. |
---|
3352 | static bool outNode(const Node& n) { |
---|
3353 | return Parent::outNode(n); |
---|
3354 | } |
---|
3355 | |
---|
3356 | /// \brief Returns \c true if the given arc is an original arc. |
---|
3357 | /// |
---|
3358 | /// Returns \c true if the given arc is one of the arcs in the |
---|
3359 | /// original digraph. |
---|
3360 | static bool origArc(const Arc& a) { |
---|
3361 | return Parent::origArc(a); |
---|
3362 | } |
---|
3363 | |
---|
3364 | /// \brief Returns \c true if the given arc is a bind arc. |
---|
3365 | /// |
---|
3366 | /// Returns \c true if the given arc is a bind arc, i.e. it connects |
---|
3367 | /// an in-node and an out-node. |
---|
3368 | static bool bindArc(const Arc& a) { |
---|
3369 | return Parent::bindArc(a); |
---|
3370 | } |
---|
3371 | |
---|
3372 | /// \brief Returns the in-node created from the given original node. |
---|
3373 | /// |
---|
3374 | /// Returns the in-node created from the given original node. |
---|
3375 | static Node inNode(const DigraphNode& n) { |
---|
3376 | return Parent::inNode(n); |
---|
3377 | } |
---|
3378 | |
---|
3379 | /// \brief Returns the out-node created from the given original node. |
---|
3380 | /// |
---|
3381 | /// Returns the out-node created from the given original node. |
---|
3382 | static Node outNode(const DigraphNode& n) { |
---|
3383 | return Parent::outNode(n); |
---|
3384 | } |
---|
3385 | |
---|
3386 | /// \brief Returns the bind arc that corresponds to the given |
---|
3387 | /// original node. |
---|
3388 | /// |
---|
3389 | /// Returns the bind arc in the adaptor that corresponds to the given |
---|
3390 | /// original node, i.e. the arc connecting the in-node and out-node |
---|
3391 | /// of \c n. |
---|
3392 | static Arc arc(const DigraphNode& n) { |
---|
3393 | return Parent::arc(n); |
---|
3394 | } |
---|
3395 | |
---|
3396 | /// \brief Returns the arc that corresponds to the given original arc. |
---|
3397 | /// |
---|
3398 | /// Returns the arc in the adaptor that corresponds to the given |
---|
3399 | /// original arc. |
---|
3400 | static Arc arc(const DigraphArc& a) { |
---|
3401 | return Parent::arc(a); |
---|
3402 | } |
---|
3403 | |
---|
3404 | /// \brief Node map combined from two original node maps |
---|
3405 | /// |
---|
3406 | /// This map adaptor class adapts two node maps of the original digraph |
---|
3407 | /// to get a node map of the split digraph. |
---|
3408 | /// Its value type is inherited from the first node map type |
---|
3409 | /// (\c InNodeMap). |
---|
3410 | template <typename InNodeMap, typename OutNodeMap> |
---|
3411 | class CombinedNodeMap { |
---|
3412 | public: |
---|
3413 | |
---|
3414 | /// The key type of the map |
---|
3415 | typedef Node Key; |
---|
3416 | /// The value type of the map |
---|
3417 | typedef typename InNodeMap::Value Value; |
---|
3418 | |
---|
3419 | typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag; |
---|
3420 | typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue; |
---|
3421 | typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue; |
---|
3422 | typedef typename MapTraits<InNodeMap>::ReturnValue Reference; |
---|
3423 | typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference; |
---|
3424 | |
---|
3425 | /// Constructor |
---|
3426 | CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) |
---|
3427 | : _in_map(in_map), _out_map(out_map) {} |
---|
3428 | |
---|
3429 | /// Returns the value associated with the given key. |
---|
3430 | Value operator[](const Key& key) const { |
---|
3431 | if (SplitNodesBase<const DGR>::inNode(key)) { |
---|
3432 | return _in_map[key]; |
---|
3433 | } else { |
---|
3434 | return _out_map[key]; |
---|
3435 | } |
---|
3436 | } |
---|
3437 | |
---|
3438 | /// Returns a reference to the value associated with the given key. |
---|
3439 | Value& operator[](const Key& key) { |
---|
3440 | if (SplitNodesBase<const DGR>::inNode(key)) { |
---|
3441 | return _in_map[key]; |
---|
3442 | } else { |
---|
3443 | return _out_map[key]; |
---|
3444 | } |
---|
3445 | } |
---|
3446 | |
---|
3447 | /// Sets the value associated with the given key. |
---|
3448 | void set(const Key& key, const Value& value) { |
---|
3449 | if (SplitNodesBase<const DGR>::inNode(key)) { |
---|
3450 | _in_map.set(key, value); |
---|
3451 | } else { |
---|
3452 | _out_map.set(key, value); |
---|
3453 | } |
---|
3454 | } |
---|
3455 | |
---|
3456 | private: |
---|
3457 | |
---|
3458 | InNodeMap& _in_map; |
---|
3459 | OutNodeMap& _out_map; |
---|
3460 | |
---|
3461 | }; |
---|
3462 | |
---|
3463 | |
---|
3464 | /// \brief Returns a combined node map |
---|
3465 | /// |
---|
3466 | /// This function just returns a combined node map. |
---|
3467 | template <typename InNodeMap, typename OutNodeMap> |
---|
3468 | static CombinedNodeMap<InNodeMap, OutNodeMap> |
---|
3469 | combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { |
---|
3470 | return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map); |
---|
3471 | } |
---|
3472 | |
---|
3473 | template <typename InNodeMap, typename OutNodeMap> |
---|
3474 | static CombinedNodeMap<const InNodeMap, OutNodeMap> |
---|
3475 | combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { |
---|
3476 | return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map); |
---|
3477 | } |
---|
3478 | |
---|
3479 | template <typename InNodeMap, typename OutNodeMap> |
---|
3480 | static CombinedNodeMap<InNodeMap, const OutNodeMap> |
---|
3481 | combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { |
---|
3482 | return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map); |
---|
3483 | } |
---|
3484 | |
---|
3485 | template <typename InNodeMap, typename OutNodeMap> |
---|
3486 | static CombinedNodeMap<const InNodeMap, const OutNodeMap> |
---|
3487 | combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { |
---|
3488 | return CombinedNodeMap<const InNodeMap, |
---|
3489 | const OutNodeMap>(in_map, out_map); |
---|
3490 | } |
---|
3491 | |
---|
3492 | /// \brief Arc map combined from an arc map and a node map of the |
---|
3493 | /// original digraph. |
---|
3494 | /// |
---|
3495 | /// This map adaptor class adapts an arc map and a node map of the |
---|
3496 | /// original digraph to get an arc map of the split digraph. |
---|
3497 | /// Its value type is inherited from the original arc map type |
---|
3498 | /// (\c ArcMap). |
---|
3499 | template <typename ArcMap, typename NodeMap> |
---|
3500 | class CombinedArcMap { |
---|
3501 | public: |
---|
3502 | |
---|
3503 | /// The key type of the map |
---|
3504 | typedef Arc Key; |
---|
3505 | /// The value type of the map |
---|
3506 | typedef typename ArcMap::Value Value; |
---|
3507 | |
---|
3508 | typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag; |
---|
3509 | typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue; |
---|
3510 | typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue; |
---|
3511 | typedef typename MapTraits<ArcMap>::ReturnValue Reference; |
---|
3512 | typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReference; |
---|
3513 | |
---|
3514 | /// Constructor |
---|
3515 | CombinedArcMap(ArcMap& arc_map, NodeMap& node_map) |
---|
3516 | : _arc_map(arc_map), _node_map(node_map) {} |
---|
3517 | |
---|
3518 | /// Returns the value associated with the given key. |
---|
3519 | Value operator[](const Key& arc) const { |
---|
3520 | if (SplitNodesBase<const DGR>::origArc(arc)) { |
---|
3521 | return _arc_map[arc]; |
---|
3522 | } else { |
---|
3523 | return _node_map[arc]; |
---|
3524 | } |
---|
3525 | } |
---|
3526 | |
---|
3527 | /// Returns a reference to the value associated with the given key. |
---|
3528 | Value& operator[](const Key& arc) { |
---|
3529 | if (SplitNodesBase<const DGR>::origArc(arc)) { |
---|
3530 | return _arc_map[arc]; |
---|
3531 | } else { |
---|
3532 | return _node_map[arc]; |
---|
3533 | } |
---|
3534 | } |
---|
3535 | |
---|
3536 | /// Sets the value associated with the given key. |
---|
3537 | void set(const Arc& arc, const Value& val) { |
---|
3538 | if (SplitNodesBase<const DGR>::origArc(arc)) { |
---|
3539 | _arc_map.set(arc, val); |
---|
3540 | } else { |
---|
3541 | _node_map.set(arc, val); |
---|
3542 | } |
---|
3543 | } |
---|
3544 | |
---|
3545 | private: |
---|
3546 | ArcMap& _arc_map; |
---|
3547 | NodeMap& _node_map; |
---|
3548 | }; |
---|
3549 | |
---|
3550 | /// \brief Returns a combined arc map |
---|
3551 | /// |
---|
3552 | /// This function just returns a combined arc map. |
---|
3553 | template <typename ArcMap, typename NodeMap> |
---|
3554 | static CombinedArcMap<ArcMap, NodeMap> |
---|
3555 | combinedArcMap(ArcMap& arc_map, NodeMap& node_map) { |
---|
3556 | return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map); |
---|
3557 | } |
---|
3558 | |
---|
3559 | template <typename ArcMap, typename NodeMap> |
---|
3560 | static CombinedArcMap<const ArcMap, NodeMap> |
---|
3561 | combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) { |
---|
3562 | return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map); |
---|
3563 | } |
---|
3564 | |
---|
3565 | template <typename ArcMap, typename NodeMap> |
---|
3566 | static CombinedArcMap<ArcMap, const NodeMap> |
---|
3567 | combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) { |
---|
3568 | return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map); |
---|
3569 | } |
---|
3570 | |
---|
3571 | template <typename ArcMap, typename NodeMap> |
---|
3572 | static CombinedArcMap<const ArcMap, const NodeMap> |
---|
3573 | combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) { |
---|
3574 | return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map); |
---|
3575 | } |
---|
3576 | |
---|
3577 | }; |
---|
3578 | |
---|
3579 | /// \brief Returns a (read-only) SplitNodes adaptor |
---|
3580 | /// |
---|
3581 | /// This function just returns a (read-only) \ref SplitNodes adaptor. |
---|
3582 | /// \ingroup graph_adaptors |
---|
3583 | /// \relates SplitNodes |
---|
3584 | template<typename DGR> |
---|
3585 | SplitNodes<DGR> |
---|
3586 | splitNodes(const DGR& digraph) { |
---|
3587 | return SplitNodes<DGR>(digraph); |
---|
3588 | } |
---|
3589 | |
---|
3590 | #undef LEMON_SCOPE_FIX |
---|
3591 | |
---|
3592 | } //namespace lemon |
---|
3593 | |
---|
3594 | #endif //LEMON_ADAPTORS_H |
---|