gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Fix several missing includes (#232)
0 7 0
default
7 files changed with 12 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 524288 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_ADAPTORS_H
20 20
#define LEMON_ADAPTORS_H
21 21

	
22 22
/// \ingroup graph_adaptors
23 23
/// \file
24 24
/// \brief Adaptor classes for digraphs and graphs
25 25
///
26 26
/// This file contains several useful adaptors for digraphs and graphs.
27 27

	
28 28
#include <lemon/core.h>
29 29
#include <lemon/maps.h>
30 30
#include <lemon/bits/variant.h>
31 31

	
32 32
#include <lemon/bits/graph_adaptor_extender.h>
33
#include <lemon/bits/map_extender.h>
33 34
#include <lemon/tolerance.h>
34 35

	
35 36
#include <algorithm>
36 37

	
37 38
namespace lemon {
38 39

	
39 40
#ifdef _MSC_VER
40 41
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED
41 42
#else
42 43
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED
43 44
#endif
44 45

	
45 46
  template<typename DGR>
46 47
  class DigraphAdaptorBase {
47 48
  public:
48 49
    typedef DGR Digraph;
49 50
    typedef DigraphAdaptorBase Adaptor;
50 51

	
51 52
  protected:
52 53
    DGR* _digraph;
53 54
    DigraphAdaptorBase() : _digraph(0) { }
54 55
    void initialize(DGR& digraph) { _digraph = &digraph; }
55 56

	
56 57
  public:
57 58
    DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { }
58 59

	
59 60
    typedef typename DGR::Node Node;
60 61
    typedef typename DGR::Arc Arc;
61 62

	
62 63
    void first(Node& i) const { _digraph->first(i); }
63 64
    void first(Arc& i) const { _digraph->first(i); }
64 65
    void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
65 66
    void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
66 67

	
67 68
    void next(Node& i) const { _digraph->next(i); }
68 69
    void next(Arc& i) const { _digraph->next(i); }
69 70
    void nextIn(Arc& i) const { _digraph->nextIn(i); }
70 71
    void nextOut(Arc& i) const { _digraph->nextOut(i); }
71 72

	
72 73
    Node source(const Arc& a) const { return _digraph->source(a); }
73 74
    Node target(const Arc& a) const { return _digraph->target(a); }
74 75

	
75 76
    typedef NodeNumTagIndicator<DGR> NodeNumTag;
76 77
    int nodeNum() const { return _digraph->nodeNum(); }
77 78

	
78 79
    typedef ArcNumTagIndicator<DGR> ArcNumTag;
79 80
    int arcNum() const { return _digraph->arcNum(); }
80 81

	
81 82
    typedef FindArcTagIndicator<DGR> FindArcTag;
82 83
    Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
83 84
      return _digraph->findArc(u, v, prev);
84 85
    }
85 86

	
86 87
    Node addNode() { return _digraph->addNode(); }
87 88
    Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
88 89

	
89 90
    void erase(const Node& n) { _digraph->erase(n); }
90 91
    void erase(const Arc& a) { _digraph->erase(a); }
91 92

	
92 93
    void clear() { _digraph->clear(); }
93 94

	
94 95
    int id(const Node& n) const { return _digraph->id(n); }
95 96
    int id(const Arc& a) const { return _digraph->id(a); }
96 97

	
97 98
    Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
98 99
    Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
99 100

	
100 101
    int maxNodeId() const { return _digraph->maxNodeId(); }
101 102
    int maxArcId() const { return _digraph->maxArcId(); }
102 103

	
103 104
    typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
104 105
    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
105 106

	
106 107
    typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier;
107 108
    ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
108 109

	
109 110
    template <typename V>
110 111
    class NodeMap : public DGR::template NodeMap<V> {
111 112
    public:
112 113

	
113 114
      typedef typename DGR::template NodeMap<V> Parent;
114 115

	
115 116
      explicit NodeMap(const Adaptor& adaptor)
116 117
        : Parent(*adaptor._digraph) {}
117 118

	
118 119
      NodeMap(const Adaptor& adaptor, const V& value)
119 120
        : Parent(*adaptor._digraph, value) { }
120 121

	
121 122
    private:
122 123
      NodeMap& operator=(const NodeMap& cmap) {
123 124
        return operator=<NodeMap>(cmap);
124 125
      }
125 126

	
126 127
      template <typename CMap>
127 128
      NodeMap& operator=(const CMap& cmap) {
128 129
        Parent::operator=(cmap);
129 130
        return *this;
130 131
      }
131 132

	
132 133
    };
133 134

	
134 135
    template <typename V>
135 136
    class ArcMap : public DGR::template ArcMap<V> {
136 137
    public:
137 138

	
138 139
      typedef typename DGR::template ArcMap<V> Parent;
139 140

	
140 141
      explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor)
141 142
        : Parent(*adaptor._digraph) {}
142 143

	
143 144
      ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value)
144 145
        : Parent(*adaptor._digraph, value) {}
145 146

	
146 147
    private:
147 148
      ArcMap& operator=(const ArcMap& cmap) {
148 149
        return operator=<ArcMap>(cmap);
149 150
      }
150 151

	
151 152
      template <typename CMap>
152 153
      ArcMap& operator=(const CMap& cmap) {
153 154
        Parent::operator=(cmap);
154 155
        return *this;
155 156
      }
156 157

	
157 158
    };
158 159

	
159 160
  };
160 161

	
161 162
  template<typename GR>
162 163
  class GraphAdaptorBase {
163 164
  public:
164 165
    typedef GR Graph;
165 166

	
166 167
  protected:
167 168
    GR* _graph;
168 169

	
169 170
    GraphAdaptorBase() : _graph(0) {}
170 171

	
171 172
    void initialize(GR& graph) { _graph = &graph; }
172 173

	
173 174
  public:
174 175
    GraphAdaptorBase(GR& graph) : _graph(&graph) {}
175 176

	
176 177
    typedef typename GR::Node Node;
177 178
    typedef typename GR::Arc Arc;
178 179
    typedef typename GR::Edge Edge;
179 180

	
180 181
    void first(Node& i) const { _graph->first(i); }
181 182
    void first(Arc& i) const { _graph->first(i); }
182 183
    void first(Edge& i) const { _graph->first(i); }
183 184
    void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
184 185
    void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
185 186
    void firstInc(Edge &i, bool &d, const Node &n) const {
186 187
      _graph->firstInc(i, d, n);
187 188
    }
188 189

	
189 190
    void next(Node& i) const { _graph->next(i); }
190 191
    void next(Arc& i) const { _graph->next(i); }
191 192
    void next(Edge& i) const { _graph->next(i); }
192 193
    void nextIn(Arc& i) const { _graph->nextIn(i); }
193 194
    void nextOut(Arc& i) const { _graph->nextOut(i); }
194 195
    void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
195 196

	
196 197
    Node u(const Edge& e) const { return _graph->u(e); }
197 198
    Node v(const Edge& e) const { return _graph->v(e); }
198 199

	
199 200
    Node source(const Arc& a) const { return _graph->source(a); }
200 201
    Node target(const Arc& a) const { return _graph->target(a); }
201 202

	
202 203
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
203 204
    int nodeNum() const { return _graph->nodeNum(); }
204 205

	
205 206
    typedef ArcNumTagIndicator<Graph> ArcNumTag;
206 207
    int arcNum() const { return _graph->arcNum(); }
207 208

	
208 209
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
209 210
    int edgeNum() const { return _graph->edgeNum(); }
210 211

	
211 212
    typedef FindArcTagIndicator<Graph> FindArcTag;
212 213
    Arc findArc(const Node& u, const Node& v,
213 214
                const Arc& prev = INVALID) const {
214 215
      return _graph->findArc(u, v, prev);
215 216
    }
216 217

	
217 218
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
218 219
    Edge findEdge(const Node& u, const Node& v,
219 220
                  const Edge& prev = INVALID) const {
220 221
      return _graph->findEdge(u, v, prev);
221 222
    }
222 223

	
223 224
    Node addNode() { return _graph->addNode(); }
224 225
    Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
225 226

	
226 227
    void erase(const Node& i) { _graph->erase(i); }
227 228
    void erase(const Edge& i) { _graph->erase(i); }
228 229

	
229 230
    void clear() { _graph->clear(); }
230 231

	
231 232
    bool direction(const Arc& a) const { return _graph->direction(a); }
232 233
    Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
233 234

	
234 235
    int id(const Node& v) const { return _graph->id(v); }
235 236
    int id(const Arc& a) const { return _graph->id(a); }
236 237
    int id(const Edge& e) const { return _graph->id(e); }
237 238

	
238 239
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
239 240
    Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
240 241
    Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
241 242

	
242 243
    int maxNodeId() const { return _graph->maxNodeId(); }
243 244
    int maxArcId() const { return _graph->maxArcId(); }
244 245
    int maxEdgeId() const { return _graph->maxEdgeId(); }
245 246

	
246 247
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
247 248
    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
248 249

	
249 250
    typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
250 251
    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
251 252

	
252 253
    typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier;
253 254
    EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
254 255

	
255 256
    template <typename V>
256 257
    class NodeMap : public GR::template NodeMap<V> {
257 258
    public:
258 259
      typedef typename GR::template NodeMap<V> Parent;
259 260
      explicit NodeMap(const GraphAdaptorBase<GR>& adapter)
260 261
        : Parent(*adapter._graph) {}
261 262
      NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
262 263
        : Parent(*adapter._graph, value) {}
263 264

	
264 265
    private:
265 266
      NodeMap& operator=(const NodeMap& cmap) {
266 267
        return operator=<NodeMap>(cmap);
267 268
      }
268 269

	
269 270
      template <typename CMap>
270 271
      NodeMap& operator=(const CMap& cmap) {
271 272
        Parent::operator=(cmap);
272 273
        return *this;
273 274
      }
274 275

	
275 276
    };
276 277

	
277 278
    template <typename V>
278 279
    class ArcMap : public GR::template ArcMap<V> {
279 280
    public:
280 281
      typedef typename GR::template ArcMap<V> Parent;
281 282
      explicit ArcMap(const GraphAdaptorBase<GR>& adapter)
282 283
        : Parent(*adapter._graph) {}
283 284
      ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value)
284 285
        : Parent(*adapter._graph, value) {}
285 286

	
286 287
    private:
287 288
      ArcMap& operator=(const ArcMap& cmap) {
288 289
        return operator=<ArcMap>(cmap);
289 290
      }
290 291

	
291 292
      template <typename CMap>
292 293
      ArcMap& operator=(const CMap& cmap) {
293 294
        Parent::operator=(cmap);
294 295
        return *this;
295 296
      }
296 297
    };
297 298

	
298 299
    template <typename V>
299 300
    class EdgeMap : public GR::template EdgeMap<V> {
300 301
    public:
301 302
      typedef typename GR::template EdgeMap<V> Parent;
302 303
      explicit EdgeMap(const GraphAdaptorBase<GR>& adapter)
303 304
        : Parent(*adapter._graph) {}
304 305
      EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
305 306
        : Parent(*adapter._graph, value) {}
306 307

	
307 308
    private:
308 309
      EdgeMap& operator=(const EdgeMap& cmap) {
309 310
        return operator=<EdgeMap>(cmap);
310 311
      }
311 312

	
312 313
      template <typename CMap>
313 314
      EdgeMap& operator=(const CMap& cmap) {
314 315
        Parent::operator=(cmap);
315 316
        return *this;
316 317
      }
317 318
    };
318 319

	
319 320
  };
320 321

	
321 322
  template <typename DGR>
322 323
  class ReverseDigraphBase : public DigraphAdaptorBase<DGR> {
323 324
  public:
324 325
    typedef DGR Digraph;
325 326
    typedef DigraphAdaptorBase<DGR> Parent;
326 327
  protected:
327 328
    ReverseDigraphBase() : Parent() { }
328 329
  public:
329 330
    typedef typename Parent::Node Node;
330 331
    typedef typename Parent::Arc Arc;
331 332

	
332 333
    void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
333 334
    void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
334 335

	
335 336
    void nextIn(Arc& a) const { Parent::nextOut(a); }
336 337
    void nextOut(Arc& a) const { Parent::nextIn(a); }
337 338

	
338 339
    Node source(const Arc& a) const { return Parent::target(a); }
339 340
    Node target(const Arc& a) const { return Parent::source(a); }
340 341

	
341 342
    Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
342 343

	
343 344
    typedef FindArcTagIndicator<DGR> FindArcTag;
344 345
    Arc findArc(const Node& u, const Node& v,
345 346
                const Arc& prev = INVALID) const {
346 347
      return Parent::findArc(v, u, prev);
347 348
    }
348 349

	
349 350
  };
350 351

	
351 352
  /// \ingroup graph_adaptors
352 353
  ///
353 354
  /// \brief Adaptor class for reversing the orientation of the arcs in
354 355
  /// a digraph.
355 356
  ///
356 357
  /// ReverseDigraph can be used for reversing the arcs in a digraph.
357 358
  /// It conforms to the \ref concepts::Digraph "Digraph" concept.
358 359
  ///
359 360
  /// The adapted digraph can also be modified through this adaptor
360 361
  /// by adding or removing nodes or arcs, unless the \c GR template
361 362
  /// parameter is set to be \c const.
362 363
  ///
363 364
  /// \tparam DGR The type of the adapted digraph.
364 365
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
365 366
  /// It can also be specified to be \c const.
366 367
  ///
367 368
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
368 369
  /// digraph are convertible to each other.
369 370
  template<typename DGR>
370 371
#ifdef DOXYGEN
371 372
  class ReverseDigraph {
372 373
#else
373 374
  class ReverseDigraph :
374 375
    public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > {
375 376
#endif
376 377
  public:
377 378
    /// The type of the adapted digraph.
378 379
    typedef DGR Digraph;
379 380
    typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent;
380 381
  protected:
381 382
    ReverseDigraph() { }
382 383
  public:
383 384

	
384 385
    /// \brief Constructor
385 386
    ///
386 387
    /// Creates a reverse digraph adaptor for the given digraph.
387 388
    explicit ReverseDigraph(DGR& digraph) {
388 389
      Parent::initialize(digraph);
389 390
    }
390 391
  };
391 392

	
392 393
  /// \brief Returns a read-only ReverseDigraph adaptor
393 394
  ///
394 395
  /// This function just returns a read-only \ref ReverseDigraph adaptor.
395 396
  /// \ingroup graph_adaptors
396 397
  /// \relates ReverseDigraph
397 398
  template<typename DGR>
398 399
  ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
399 400
    return ReverseDigraph<const DGR>(digraph);
400 401
  }
401 402

	
402 403

	
403 404
  template <typename DGR, typename NF, typename AF, bool ch = true>
404 405
  class SubDigraphBase : public DigraphAdaptorBase<DGR> {
405 406
  public:
406 407
    typedef DGR Digraph;
407 408
    typedef NF NodeFilterMap;
408 409
    typedef AF ArcFilterMap;
409 410

	
410 411
    typedef SubDigraphBase Adaptor;
411 412
    typedef DigraphAdaptorBase<DGR> Parent;
412 413
  protected:
413 414
    NF* _node_filter;
414 415
    AF* _arc_filter;
415 416
    SubDigraphBase()
416 417
      : Parent(), _node_filter(0), _arc_filter(0) { }
417 418

	
418 419
    void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
419 420
      Parent::initialize(digraph);
420 421
      _node_filter = &node_filter;
421 422
      _arc_filter = &arc_filter;      
422 423
    }
423 424

	
424 425
  public:
425 426

	
426 427
    typedef typename Parent::Node Node;
427 428
    typedef typename Parent::Arc Arc;
428 429

	
429 430
    void first(Node& i) const {
430 431
      Parent::first(i);
431 432
      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
432 433
    }
433 434

	
434 435
    void first(Arc& i) const {
435 436
      Parent::first(i);
436 437
      while (i != INVALID && (!(*_arc_filter)[i]
437 438
                              || !(*_node_filter)[Parent::source(i)]
438 439
                              || !(*_node_filter)[Parent::target(i)]))
439 440
        Parent::next(i);
440 441
    }
441 442

	
442 443
    void firstIn(Arc& i, const Node& n) const {
443 444
      Parent::firstIn(i, n);
444 445
      while (i != INVALID && (!(*_arc_filter)[i]
445 446
                              || !(*_node_filter)[Parent::source(i)]))
446 447
        Parent::nextIn(i);
447 448
    }
448 449

	
449 450
    void firstOut(Arc& i, const Node& n) const {
450 451
      Parent::firstOut(i, n);
451 452
      while (i != INVALID && (!(*_arc_filter)[i]
452 453
                              || !(*_node_filter)[Parent::target(i)]))
453 454
        Parent::nextOut(i);
454 455
    }
455 456

	
456 457
    void next(Node& i) const {
457 458
      Parent::next(i);
458 459
      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
459 460
    }
460 461

	
461 462
    void next(Arc& i) const {
462 463
      Parent::next(i);
463 464
      while (i != INVALID && (!(*_arc_filter)[i]
464 465
                              || !(*_node_filter)[Parent::source(i)]
465 466
                              || !(*_node_filter)[Parent::target(i)]))
466 467
        Parent::next(i);
467 468
    }
468 469

	
469 470
    void nextIn(Arc& i) const {
470 471
      Parent::nextIn(i);
471 472
      while (i != INVALID && (!(*_arc_filter)[i]
472 473
                              || !(*_node_filter)[Parent::source(i)]))
473 474
        Parent::nextIn(i);
474 475
    }
475 476

	
476 477
    void nextOut(Arc& i) const {
477 478
      Parent::nextOut(i);
478 479
      while (i != INVALID && (!(*_arc_filter)[i]
479 480
                              || !(*_node_filter)[Parent::target(i)]))
480 481
        Parent::nextOut(i);
481 482
    }
482 483

	
483 484
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
484 485
    void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
485 486

	
486 487
    bool status(const Node& n) const { return (*_node_filter)[n]; }
487 488
    bool status(const Arc& a) const { return (*_arc_filter)[a]; }
488 489

	
489 490
    typedef False NodeNumTag;
490 491
    typedef False ArcNumTag;
491 492

	
492 493
    typedef FindArcTagIndicator<DGR> FindArcTag;
493 494
    Arc findArc(const Node& source, const Node& target,
494 495
                const Arc& prev = INVALID) const {
495 496
      if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
496 497
        return INVALID;
497 498
      }
498 499
      Arc arc = Parent::findArc(source, target, prev);
499 500
      while (arc != INVALID && !(*_arc_filter)[arc]) {
500 501
        arc = Parent::findArc(source, target, arc);
501 502
      }
502 503
      return arc;
503 504
    }
504 505

	
505 506
  public:
506 507

	
507 508
    template <typename V>
508 509
    class NodeMap 
509 510
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, 
510 511
	      LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
511 512
    public:
512 513
      typedef V Value;
513 514
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
514 515
	    LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
515 516

	
516 517
      NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor)
517 518
        : Parent(adaptor) {}
518 519
      NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value)
519 520
        : Parent(adaptor, value) {}
520 521

	
521 522
    private:
522 523
      NodeMap& operator=(const NodeMap& cmap) {
523 524
        return operator=<NodeMap>(cmap);
524 525
      }
525 526

	
526 527
      template <typename CMap>
527 528
      NodeMap& operator=(const CMap& cmap) {
528 529
        Parent::operator=(cmap);
529 530
        return *this;
530 531
      }
531 532
    };
532 533

	
533 534
    template <typename V>
534 535
    class ArcMap 
535 536
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
536 537
	      LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
537 538
    public:
538 539
      typedef V Value;
539 540
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
540 541
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
541 542

	
542 543
      ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor)
543 544
        : Parent(adaptor) {}
544 545
      ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value)
545 546
        : Parent(adaptor, value) {}
546 547

	
547 548
    private:
548 549
      ArcMap& operator=(const ArcMap& cmap) {
549 550
        return operator=<ArcMap>(cmap);
550 551
      }
551 552

	
552 553
      template <typename CMap>
553 554
      ArcMap& operator=(const CMap& cmap) {
554 555
        Parent::operator=(cmap);
555 556
        return *this;
556 557
      }
557 558
    };
558 559

	
559 560
  };
560 561

	
561 562
  template <typename DGR, typename NF, typename AF>
562 563
  class SubDigraphBase<DGR, NF, AF, false>
563 564
    : public DigraphAdaptorBase<DGR> {
564 565
  public:
565 566
    typedef DGR Digraph;
566 567
    typedef NF NodeFilterMap;
567 568
    typedef AF ArcFilterMap;
568 569

	
569 570
    typedef SubDigraphBase Adaptor;
570 571
    typedef DigraphAdaptorBase<Digraph> Parent;
571 572
  protected:
572 573
    NF* _node_filter;
573 574
    AF* _arc_filter;
574 575
    SubDigraphBase()
575 576
      : Parent(), _node_filter(0), _arc_filter(0) { }
576 577

	
577 578
    void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
578 579
      Parent::initialize(digraph);
579 580
      _node_filter = &node_filter;
580 581
      _arc_filter = &arc_filter;      
581 582
    }
582 583

	
583 584
  public:
584 585

	
585 586
    typedef typename Parent::Node Node;
586 587
    typedef typename Parent::Arc Arc;
587 588

	
588 589
    void first(Node& i) const {
589 590
      Parent::first(i);
590 591
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
591 592
    }
592 593

	
593 594
    void first(Arc& i) const {
594 595
      Parent::first(i);
595 596
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
596 597
    }
597 598

	
598 599
    void firstIn(Arc& i, const Node& n) const {
599 600
      Parent::firstIn(i, n);
600 601
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
601 602
    }
602 603

	
603 604
    void firstOut(Arc& i, const Node& n) const {
604 605
      Parent::firstOut(i, n);
605 606
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
606 607
    }
607 608

	
608 609
    void next(Node& i) const {
609 610
      Parent::next(i);
610 611
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
611 612
    }
612 613
    void next(Arc& i) const {
613 614
      Parent::next(i);
614 615
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
615 616
    }
616 617
    void nextIn(Arc& i) const {
617 618
      Parent::nextIn(i);
618 619
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
619 620
    }
620 621

	
621 622
    void nextOut(Arc& i) const {
622 623
      Parent::nextOut(i);
623 624
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
624 625
    }
625 626

	
626 627
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
627 628
    void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
628 629

	
629 630
    bool status(const Node& n) const { return (*_node_filter)[n]; }
630 631
    bool status(const Arc& a) const { return (*_arc_filter)[a]; }
631 632

	
632 633
    typedef False NodeNumTag;
633 634
    typedef False ArcNumTag;
634 635

	
635 636
    typedef FindArcTagIndicator<DGR> FindArcTag;
636 637
    Arc findArc(const Node& source, const Node& target,
637 638
                const Arc& prev = INVALID) const {
638 639
      if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
639 640
        return INVALID;
640 641
      }
641 642
      Arc arc = Parent::findArc(source, target, prev);
642 643
      while (arc != INVALID && !(*_arc_filter)[arc]) {
643 644
        arc = Parent::findArc(source, target, arc);
644 645
      }
645 646
      return arc;
646 647
    }
647 648

	
648 649
    template <typename V>
649 650
    class NodeMap 
650 651
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
651 652
          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
652 653
    public:
653 654
      typedef V Value;
654 655
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, 
655 656
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
656 657

	
657 658
      NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor)
658 659
        : Parent(adaptor) {}
659 660
      NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value)
660 661
        : Parent(adaptor, value) {}
661 662

	
662 663
    private:
663 664
      NodeMap& operator=(const NodeMap& cmap) {
664 665
        return operator=<NodeMap>(cmap);
665 666
      }
666 667

	
667 668
      template <typename CMap>
668 669
      NodeMap& operator=(const CMap& cmap) {
669 670
        Parent::operator=(cmap);
670 671
        return *this;
671 672
      }
672 673
    };
673 674

	
674 675
    template <typename V>
675 676
    class ArcMap 
676 677
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
677 678
          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
678 679
    public:
679 680
      typedef V Value;
680 681
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
681 682
          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
682 683

	
683 684
      ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor)
684 685
        : Parent(adaptor) {}
685 686
      ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value)
686 687
        : Parent(adaptor, value) {}
687 688

	
688 689
    private:
689 690
      ArcMap& operator=(const ArcMap& cmap) {
690 691
        return operator=<ArcMap>(cmap);
691 692
      }
692 693

	
693 694
      template <typename CMap>
694 695
      ArcMap& operator=(const CMap& cmap) {
695 696
        Parent::operator=(cmap);
696 697
        return *this;
697 698
      }
698 699
    };
699 700

	
700 701
  };
701 702

	
702 703
  /// \ingroup graph_adaptors
703 704
  ///
704 705
  /// \brief Adaptor class for hiding nodes and arcs in a digraph
705 706
  ///
706 707
  /// SubDigraph can be used for hiding nodes and arcs in a digraph.
707 708
  /// A \c bool node map and a \c bool arc map must be specified, which
708 709
  /// define the filters for nodes and arcs.
709 710
  /// Only the nodes and arcs with \c true filter value are
710 711
  /// shown in the subdigraph. The arcs that are incident to hidden
711 712
  /// nodes are also filtered out.
712 713
  /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept.
713 714
  ///
714 715
  /// The adapted digraph can also be modified through this adaptor
715 716
  /// by adding or removing nodes or arcs, unless the \c GR template
716 717
  /// parameter is set to be \c const.
717 718
  ///
718 719
  /// \tparam DGR The type of the adapted digraph.
719 720
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
720 721
  /// It can also be specified to be \c const.
721 722
  /// \tparam NF The type of the node filter map.
722 723
  /// It must be a \c bool (or convertible) node map of the
723 724
  /// adapted digraph. The default type is
724 725
  /// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>".
725 726
  /// \tparam AF The type of the arc filter map.
726 727
  /// It must be \c bool (or convertible) arc map of the
727 728
  /// adapted digraph. The default type is
728 729
  /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
729 730
  ///
730 731
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
731 732
  /// digraph are convertible to each other.
732 733
  ///
733 734
  /// \see FilterNodes
734 735
  /// \see FilterArcs
735 736
#ifdef DOXYGEN
736 737
  template<typename DGR, typename NF, typename AF>
737 738
  class SubDigraph {
738 739
#else
739 740
  template<typename DGR,
740 741
           typename NF = typename DGR::template NodeMap<bool>,
741 742
           typename AF = typename DGR::template ArcMap<bool> >
742 743
  class SubDigraph :
743 744
    public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > {
744 745
#endif
745 746
  public:
746 747
    /// The type of the adapted digraph.
747 748
    typedef DGR Digraph;
748 749
    /// The type of the node filter map.
749 750
    typedef NF NodeFilterMap;
750 751
    /// The type of the arc filter map.
751 752
    typedef AF ArcFilterMap;
752 753

	
753 754
    typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> >
754 755
      Parent;
755 756

	
756 757
    typedef typename Parent::Node Node;
757 758
    typedef typename Parent::Arc Arc;
758 759

	
759 760
  protected:
760 761
    SubDigraph() { }
761 762
  public:
762 763

	
763 764
    /// \brief Constructor
764 765
    ///
765 766
    /// Creates a subdigraph for the given digraph with the
766 767
    /// given node and arc filter maps.
767 768
    SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) {
768 769
      Parent::initialize(digraph, node_filter, arc_filter);
769 770
    }
770 771

	
771 772
    /// \brief Sets the status of the given node
772 773
    ///
773 774
    /// This function sets the status of the given node.
774 775
    /// It is done by simply setting the assigned value of \c n
775 776
    /// to \c v in the node filter map.
776 777
    void status(const Node& n, bool v) const { Parent::status(n, v); }
777 778

	
778 779
    /// \brief Sets the status of the given arc
779 780
    ///
780 781
    /// This function sets the status of the given arc.
781 782
    /// It is done by simply setting the assigned value of \c a
782 783
    /// to \c v in the arc filter map.
783 784
    void status(const Arc& a, bool v) const { Parent::status(a, v); }
784 785

	
785 786
    /// \brief Returns the status of the given node
786 787
    ///
787 788
    /// This function returns the status of the given node.
788 789
    /// It is \c true if the given node is enabled (i.e. not hidden).
789 790
    bool status(const Node& n) const { return Parent::status(n); }
790 791

	
791 792
    /// \brief Returns the status of the given arc
792 793
    ///
793 794
    /// This function returns the status of the given arc.
794 795
    /// It is \c true if the given arc is enabled (i.e. not hidden).
795 796
    bool status(const Arc& a) const { return Parent::status(a); }
796 797

	
797 798
    /// \brief Disables the given node
798 799
    ///
799 800
    /// This function disables the given node in the subdigraph,
800 801
    /// so the iteration jumps over it.
801 802
    /// It is the same as \ref status() "status(n, false)".
802 803
    void disable(const Node& n) const { Parent::status(n, false); }
803 804

	
804 805
    /// \brief Disables the given arc
805 806
    ///
806 807
    /// This function disables the given arc in the subdigraph,
807 808
    /// so the iteration jumps over it.
808 809
    /// It is the same as \ref status() "status(a, false)".
809 810
    void disable(const Arc& a) const { Parent::status(a, false); }
810 811

	
811 812
    /// \brief Enables the given node
812 813
    ///
813 814
    /// This function enables the given node in the subdigraph.
814 815
    /// It is the same as \ref status() "status(n, true)".
815 816
    void enable(const Node& n) const { Parent::status(n, true); }
816 817

	
817 818
    /// \brief Enables the given arc
818 819
    ///
819 820
    /// This function enables the given arc in the subdigraph.
820 821
    /// It is the same as \ref status() "status(a, true)".
821 822
    void enable(const Arc& a) const { Parent::status(a, true); }
822 823

	
823 824
  };
824 825

	
825 826
  /// \brief Returns a read-only SubDigraph adaptor
826 827
  ///
827 828
  /// This function just returns a read-only \ref SubDigraph adaptor.
828 829
  /// \ingroup graph_adaptors
829 830
  /// \relates SubDigraph
830 831
  template<typename DGR, typename NF, typename AF>
831 832
  SubDigraph<const DGR, NF, AF>
832 833
  subDigraph(const DGR& digraph,
833 834
             NF& node_filter, AF& arc_filter) {
834 835
    return SubDigraph<const DGR, NF, AF>
835 836
      (digraph, node_filter, arc_filter);
836 837
  }
837 838

	
838 839
  template<typename DGR, typename NF, typename AF>
839 840
  SubDigraph<const DGR, const NF, AF>
840 841
  subDigraph(const DGR& digraph,
841 842
             const NF& node_filter, AF& arc_filter) {
842 843
    return SubDigraph<const DGR, const NF, AF>
843 844
      (digraph, node_filter, arc_filter);
844 845
  }
845 846

	
846 847
  template<typename DGR, typename NF, typename AF>
847 848
  SubDigraph<const DGR, NF, const AF>
848 849
  subDigraph(const DGR& digraph,
849 850
             NF& node_filter, const AF& arc_filter) {
850 851
    return SubDigraph<const DGR, NF, const AF>
851 852
      (digraph, node_filter, arc_filter);
852 853
  }
853 854

	
854 855
  template<typename DGR, typename NF, typename AF>
855 856
  SubDigraph<const DGR, const NF, const AF>
856 857
  subDigraph(const DGR& digraph,
857 858
             const NF& node_filter, const AF& arc_filter) {
858 859
    return SubDigraph<const DGR, const NF, const AF>
859 860
      (digraph, node_filter, arc_filter);
860 861
  }
861 862

	
862 863

	
863 864
  template <typename GR, typename NF, typename EF, bool ch = true>
864 865
  class SubGraphBase : public GraphAdaptorBase<GR> {
865 866
  public:
866 867
    typedef GR Graph;
867 868
    typedef NF NodeFilterMap;
868 869
    typedef EF EdgeFilterMap;
869 870

	
870 871
    typedef SubGraphBase Adaptor;
871 872
    typedef GraphAdaptorBase<GR> Parent;
872 873
  protected:
873 874

	
874 875
    NF* _node_filter;
875 876
    EF* _edge_filter;
876 877

	
877 878
    SubGraphBase()
878 879
      : Parent(), _node_filter(0), _edge_filter(0) { }
879 880

	
880 881
    void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
881 882
      Parent::initialize(graph);
882 883
      _node_filter = &node_filter;
883 884
      _edge_filter = &edge_filter;
884 885
    }
885 886

	
886 887
  public:
887 888

	
888 889
    typedef typename Parent::Node Node;
889 890
    typedef typename Parent::Arc Arc;
890 891
    typedef typename Parent::Edge Edge;
891 892

	
892 893
    void first(Node& i) const {
893 894
      Parent::first(i);
894 895
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
895 896
    }
896 897

	
897 898
    void first(Arc& i) const {
898 899
      Parent::first(i);
899 900
      while (i!=INVALID && (!(*_edge_filter)[i]
900 901
                            || !(*_node_filter)[Parent::source(i)]
901 902
                            || !(*_node_filter)[Parent::target(i)]))
902 903
        Parent::next(i);
903 904
    }
904 905

	
905 906
    void first(Edge& i) const {
906 907
      Parent::first(i);
907 908
      while (i!=INVALID && (!(*_edge_filter)[i]
908 909
                            || !(*_node_filter)[Parent::u(i)]
909 910
                            || !(*_node_filter)[Parent::v(i)]))
910 911
        Parent::next(i);
911 912
    }
912 913

	
913 914
    void firstIn(Arc& i, const Node& n) const {
914 915
      Parent::firstIn(i, n);
915 916
      while (i!=INVALID && (!(*_edge_filter)[i]
916 917
                            || !(*_node_filter)[Parent::source(i)]))
917 918
        Parent::nextIn(i);
918 919
    }
919 920

	
920 921
    void firstOut(Arc& i, const Node& n) const {
921 922
      Parent::firstOut(i, n);
922 923
      while (i!=INVALID && (!(*_edge_filter)[i]
923 924
                            || !(*_node_filter)[Parent::target(i)]))
924 925
        Parent::nextOut(i);
925 926
    }
926 927

	
927 928
    void firstInc(Edge& i, bool& d, const Node& n) const {
928 929
      Parent::firstInc(i, d, n);
929 930
      while (i!=INVALID && (!(*_edge_filter)[i]
930 931
                            || !(*_node_filter)[Parent::u(i)]
931 932
                            || !(*_node_filter)[Parent::v(i)]))
932 933
        Parent::nextInc(i, d);
933 934
    }
934 935

	
935 936
    void next(Node& i) const {
936 937
      Parent::next(i);
937 938
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
938 939
    }
939 940

	
940 941
    void next(Arc& i) const {
941 942
      Parent::next(i);
942 943
      while (i!=INVALID && (!(*_edge_filter)[i]
943 944
                            || !(*_node_filter)[Parent::source(i)]
944 945
                            || !(*_node_filter)[Parent::target(i)]))
945 946
        Parent::next(i);
946 947
    }
947 948

	
948 949
    void next(Edge& i) const {
949 950
      Parent::next(i);
950 951
      while (i!=INVALID && (!(*_edge_filter)[i]
951 952
                            || !(*_node_filter)[Parent::u(i)]
952 953
                            || !(*_node_filter)[Parent::v(i)]))
953 954
        Parent::next(i);
954 955
    }
955 956

	
956 957
    void nextIn(Arc& i) const {
957 958
      Parent::nextIn(i);
958 959
      while (i!=INVALID && (!(*_edge_filter)[i]
959 960
                            || !(*_node_filter)[Parent::source(i)]))
960 961
        Parent::nextIn(i);
961 962
    }
962 963

	
963 964
    void nextOut(Arc& i) const {
964 965
      Parent::nextOut(i);
965 966
      while (i!=INVALID && (!(*_edge_filter)[i]
966 967
                            || !(*_node_filter)[Parent::target(i)]))
967 968
        Parent::nextOut(i);
968 969
    }
969 970

	
970 971
    void nextInc(Edge& i, bool& d) const {
971 972
      Parent::nextInc(i, d);
972 973
      while (i!=INVALID && (!(*_edge_filter)[i]
973 974
                            || !(*_node_filter)[Parent::u(i)]
974 975
                            || !(*_node_filter)[Parent::v(i)]))
975 976
        Parent::nextInc(i, d);
976 977
    }
977 978

	
978 979
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
979 980
    void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
980 981

	
981 982
    bool status(const Node& n) const { return (*_node_filter)[n]; }
982 983
    bool status(const Edge& e) const { return (*_edge_filter)[e]; }
983 984

	
984 985
    typedef False NodeNumTag;
985 986
    typedef False ArcNumTag;
986 987
    typedef False EdgeNumTag;
987 988

	
988 989
    typedef FindArcTagIndicator<Graph> FindArcTag;
989 990
    Arc findArc(const Node& u, const Node& v,
990 991
                const Arc& prev = INVALID) const {
991 992
      if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
992 993
        return INVALID;
993 994
      }
994 995
      Arc arc = Parent::findArc(u, v, prev);
995 996
      while (arc != INVALID && !(*_edge_filter)[arc]) {
996 997
        arc = Parent::findArc(u, v, arc);
997 998
      }
998 999
      return arc;
999 1000
    }
1000 1001

	
1001 1002
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1002 1003
    Edge findEdge(const Node& u, const Node& v,
1003 1004
                  const Edge& prev = INVALID) const {
1004 1005
      if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
1005 1006
        return INVALID;
1006 1007
      }
1007 1008
      Edge edge = Parent::findEdge(u, v, prev);
1008 1009
      while (edge != INVALID && !(*_edge_filter)[edge]) {
1009 1010
        edge = Parent::findEdge(u, v, edge);
1010 1011
      }
1011 1012
      return edge;
1012 1013
    }
1013 1014

	
1014 1015
    template <typename V>
1015 1016
    class NodeMap 
1016 1017
      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1017 1018
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1018 1019
    public:
1019 1020
      typedef V Value;
1020 1021
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1021 1022
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1022 1023

	
1023 1024
      NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1024 1025
        : Parent(adaptor) {}
1025 1026
      NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1026 1027
        : Parent(adaptor, value) {}
1027 1028

	
1028 1029
    private:
1029 1030
      NodeMap& operator=(const NodeMap& cmap) {
1030 1031
        return operator=<NodeMap>(cmap);
1031 1032
      }
1032 1033

	
1033 1034
      template <typename CMap>
1034 1035
      NodeMap& operator=(const CMap& cmap) {
1035 1036
        Parent::operator=(cmap);
1036 1037
        return *this;
1037 1038
      }
1038 1039
    };
1039 1040

	
1040 1041
    template <typename V>
1041 1042
    class ArcMap 
1042 1043
      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1043 1044
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1044 1045
    public:
1045 1046
      typedef V Value;
1046 1047
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1047 1048
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1048 1049

	
1049 1050
      ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1050 1051
        : Parent(adaptor) {}
1051 1052
      ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1052 1053
        : Parent(adaptor, value) {}
1053 1054

	
1054 1055
    private:
1055 1056
      ArcMap& operator=(const ArcMap& cmap) {
1056 1057
        return operator=<ArcMap>(cmap);
1057 1058
      }
1058 1059

	
1059 1060
      template <typename CMap>
1060 1061
      ArcMap& operator=(const CMap& cmap) {
1061 1062
        Parent::operator=(cmap);
1062 1063
        return *this;
1063 1064
      }
1064 1065
    };
1065 1066

	
1066 1067
    template <typename V>
1067 1068
    class EdgeMap 
1068 1069
      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1069 1070
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1070 1071
    public:
1071 1072
      typedef V Value;
1072 1073
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1073 1074
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1074 1075

	
1075 1076
      EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1076 1077
        : Parent(adaptor) {}
1077 1078

	
1078 1079
      EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1079 1080
        : Parent(adaptor, value) {}
1080 1081

	
1081 1082
    private:
1082 1083
      EdgeMap& operator=(const EdgeMap& cmap) {
1083 1084
        return operator=<EdgeMap>(cmap);
1084 1085
      }
1085 1086

	
1086 1087
      template <typename CMap>
1087 1088
      EdgeMap& operator=(const CMap& cmap) {
1088 1089
        Parent::operator=(cmap);
1089 1090
        return *this;
1090 1091
      }
1091 1092
    };
1092 1093

	
1093 1094
  };
1094 1095

	
1095 1096
  template <typename GR, typename NF, typename EF>
1096 1097
  class SubGraphBase<GR, NF, EF, false>
1097 1098
    : public GraphAdaptorBase<GR> {
1098 1099
  public:
1099 1100
    typedef GR Graph;
1100 1101
    typedef NF NodeFilterMap;
1101 1102
    typedef EF EdgeFilterMap;
1102 1103

	
1103 1104
    typedef SubGraphBase Adaptor;
1104 1105
    typedef GraphAdaptorBase<GR> Parent;
1105 1106
  protected:
1106 1107
    NF* _node_filter;
1107 1108
    EF* _edge_filter;
1108 1109
    SubGraphBase() 
1109 1110
	  : Parent(), _node_filter(0), _edge_filter(0) { }
1110 1111

	
1111 1112
    void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
1112 1113
      Parent::initialize(graph);
1113 1114
      _node_filter = &node_filter;
1114 1115
      _edge_filter = &edge_filter;
1115 1116
    }
1116 1117

	
1117 1118
  public:
1118 1119

	
1119 1120
    typedef typename Parent::Node Node;
1120 1121
    typedef typename Parent::Arc Arc;
1121 1122
    typedef typename Parent::Edge Edge;
1122 1123

	
1123 1124
    void first(Node& i) const {
1124 1125
      Parent::first(i);
1125 1126
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
1126 1127
    }
1127 1128

	
1128 1129
    void first(Arc& i) const {
1129 1130
      Parent::first(i);
1130 1131
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1131 1132
    }
1132 1133

	
1133 1134
    void first(Edge& i) const {
1134 1135
      Parent::first(i);
1135 1136
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1136 1137
    }
1137 1138

	
1138 1139
    void firstIn(Arc& i, const Node& n) const {
1139 1140
      Parent::firstIn(i, n);
1140 1141
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
1141 1142
    }
1142 1143

	
1143 1144
    void firstOut(Arc& i, const Node& n) const {
1144 1145
      Parent::firstOut(i, n);
1145 1146
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
1146 1147
    }
1147 1148

	
1148 1149
    void firstInc(Edge& i, bool& d, const Node& n) const {
1149 1150
      Parent::firstInc(i, d, n);
1150 1151
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
1151 1152
    }
1152 1153

	
1153 1154
    void next(Node& i) const {
1154 1155
      Parent::next(i);
1155 1156
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
1156 1157
    }
1157 1158
    void next(Arc& i) const {
1158 1159
      Parent::next(i);
1159 1160
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1160 1161
    }
1161 1162
    void next(Edge& i) const {
1162 1163
      Parent::next(i);
1163 1164
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1164 1165
    }
1165 1166
    void nextIn(Arc& i) const {
1166 1167
      Parent::nextIn(i);
1167 1168
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
1168 1169
    }
1169 1170

	
1170 1171
    void nextOut(Arc& i) const {
1171 1172
      Parent::nextOut(i);
1172 1173
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
1173 1174
    }
1174 1175
    void nextInc(Edge& i, bool& d) const {
1175 1176
      Parent::nextInc(i, d);
1176 1177
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
1177 1178
    }
1178 1179

	
1179 1180
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
1180 1181
    void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
1181 1182

	
1182 1183
    bool status(const Node& n) const { return (*_node_filter)[n]; }
1183 1184
    bool status(const Edge& e) const { return (*_edge_filter)[e]; }
1184 1185

	
1185 1186
    typedef False NodeNumTag;
1186 1187
    typedef False ArcNumTag;
1187 1188
    typedef False EdgeNumTag;
1188 1189

	
1189 1190
    typedef FindArcTagIndicator<Graph> FindArcTag;
1190 1191
    Arc findArc(const Node& u, const Node& v,
1191 1192
                const Arc& prev = INVALID) const {
1192 1193
      Arc arc = Parent::findArc(u, v, prev);
1193 1194
      while (arc != INVALID && !(*_edge_filter)[arc]) {
1194 1195
        arc = Parent::findArc(u, v, arc);
1195 1196
      }
1196 1197
      return arc;
1197 1198
    }
1198 1199

	
1199 1200
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1200 1201
    Edge findEdge(const Node& u, const Node& v,
1201 1202
                  const Edge& prev = INVALID) const {
1202 1203
      Edge edge = Parent::findEdge(u, v, prev);
1203 1204
      while (edge != INVALID && !(*_edge_filter)[edge]) {
1204 1205
        edge = Parent::findEdge(u, v, edge);
1205 1206
      }
1206 1207
      return edge;
1207 1208
    }
1208 1209

	
1209 1210
    template <typename V>
1210 1211
    class NodeMap 
1211 1212
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1212 1213
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1213 1214
    public:
1214 1215
      typedef V Value;
1215 1216
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1216 1217
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1217 1218

	
1218 1219
      NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1219 1220
        : Parent(adaptor) {}
1220 1221
      NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1221 1222
        : Parent(adaptor, value) {}
1222 1223

	
1223 1224
    private:
1224 1225
      NodeMap& operator=(const NodeMap& cmap) {
1225 1226
        return operator=<NodeMap>(cmap);
1226 1227
      }
1227 1228

	
1228 1229
      template <typename CMap>
1229 1230
      NodeMap& operator=(const CMap& cmap) {
1230 1231
        Parent::operator=(cmap);
1231 1232
        return *this;
1232 1233
      }
1233 1234
    };
1234 1235

	
1235 1236
    template <typename V>
1236 1237
    class ArcMap 
1237 1238
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1238 1239
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1239 1240
    public:
1240 1241
      typedef V Value;
1241 1242
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1242 1243
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1243 1244

	
1244 1245
      ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1245 1246
        : Parent(adaptor) {}
1246 1247
      ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1247 1248
        : Parent(adaptor, value) {}
1248 1249

	
1249 1250
    private:
1250 1251
      ArcMap& operator=(const ArcMap& cmap) {
1251 1252
        return operator=<ArcMap>(cmap);
1252 1253
      }
1253 1254

	
1254 1255
      template <typename CMap>
1255 1256
      ArcMap& operator=(const CMap& cmap) {
1256 1257
        Parent::operator=(cmap);
1257 1258
        return *this;
1258 1259
      }
1259 1260
    };
1260 1261

	
1261 1262
    template <typename V>
1262 1263
    class EdgeMap 
1263 1264
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1264 1265
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1265 1266
    public:
1266 1267
      typedef V Value;
1267 1268
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1268 1269
		  LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1269 1270

	
1270 1271
      EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1271 1272
        : Parent(adaptor) {}
1272 1273

	
1273 1274
      EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1274 1275
        : Parent(adaptor, value) {}
1275 1276

	
1276 1277
    private:
1277 1278
      EdgeMap& operator=(const EdgeMap& cmap) {
1278 1279
        return operator=<EdgeMap>(cmap);
1279 1280
      }
1280 1281

	
1281 1282
      template <typename CMap>
1282 1283
      EdgeMap& operator=(const CMap& cmap) {
1283 1284
        Parent::operator=(cmap);
1284 1285
        return *this;
1285 1286
      }
1286 1287
    };
1287 1288

	
1288 1289
  };
1289 1290

	
1290 1291
  /// \ingroup graph_adaptors
1291 1292
  ///
1292 1293
  /// \brief Adaptor class for hiding nodes and edges in an undirected
1293 1294
  /// graph.
1294 1295
  ///
1295 1296
  /// SubGraph can be used for hiding nodes and edges in a graph.
1296 1297
  /// A \c bool node map and a \c bool edge map must be specified, which
1297 1298
  /// define the filters for nodes and edges.
1298 1299
  /// Only the nodes and edges with \c true filter value are
1299 1300
  /// shown in the subgraph. The edges that are incident to hidden
1300 1301
  /// nodes are also filtered out.
1301 1302
  /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
1302 1303
  ///
1303 1304
  /// The adapted graph can also be modified through this adaptor
1304 1305
  /// by adding or removing nodes or edges, unless the \c GR template
1305 1306
  /// parameter is set to be \c const.
1306 1307
  ///
1307 1308
  /// \tparam GR The type of the adapted graph.
1308 1309
  /// It must conform to the \ref concepts::Graph "Graph" concept.
1309 1310
  /// It can also be specified to be \c const.
1310 1311
  /// \tparam NF The type of the node filter map.
1311 1312
  /// It must be a \c bool (or convertible) node map of the
1312 1313
  /// adapted graph. The default type is
1313 1314
  /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1314 1315
  /// \tparam EF The type of the edge filter map.
1315 1316
  /// It must be a \c bool (or convertible) edge map of the
1316 1317
  /// adapted graph. The default type is
1317 1318
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1318 1319
  ///
1319 1320
  /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1320 1321
  /// adapted graph are convertible to each other.
1321 1322
  ///
1322 1323
  /// \see FilterNodes
1323 1324
  /// \see FilterEdges
1324 1325
#ifdef DOXYGEN
1325 1326
  template<typename GR, typename NF, typename EF>
1326 1327
  class SubGraph {
1327 1328
#else
1328 1329
  template<typename GR,
1329 1330
           typename NF = typename GR::template NodeMap<bool>,
1330 1331
           typename EF = typename GR::template EdgeMap<bool> >
1331 1332
  class SubGraph :
1332 1333
    public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > {
1333 1334
#endif
1334 1335
  public:
1335 1336
    /// The type of the adapted graph.
1336 1337
    typedef GR Graph;
1337 1338
    /// The type of the node filter map.
1338 1339
    typedef NF NodeFilterMap;
1339 1340
    /// The type of the edge filter map.
1340 1341
    typedef EF EdgeFilterMap;
1341 1342

	
1342 1343
    typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> >
1343 1344
      Parent;
1344 1345

	
1345 1346
    typedef typename Parent::Node Node;
1346 1347
    typedef typename Parent::Edge Edge;
1347 1348

	
1348 1349
  protected:
1349 1350
    SubGraph() { }
1350 1351
  public:
1351 1352

	
1352 1353
    /// \brief Constructor
1353 1354
    ///
1354 1355
    /// Creates a subgraph for the given graph with the given node
1355 1356
    /// and edge filter maps.
1356 1357
    SubGraph(GR& graph, NF& node_filter, EF& edge_filter) {
1357 1358
      initialize(graph, node_filter, edge_filter);
1358 1359
    }
1359 1360

	
1360 1361
    /// \brief Sets the status of the given node
1361 1362
    ///
1362 1363
    /// This function sets the status of the given node.
1363 1364
    /// It is done by simply setting the assigned value of \c n
1364 1365
    /// to \c v in the node filter map.
1365 1366
    void status(const Node& n, bool v) const { Parent::status(n, v); }
1366 1367

	
1367 1368
    /// \brief Sets the status of the given edge
1368 1369
    ///
1369 1370
    /// This function sets the status of the given edge.
1370 1371
    /// It is done by simply setting the assigned value of \c e
1371 1372
    /// to \c v in the edge filter map.
1372 1373
    void status(const Edge& e, bool v) const { Parent::status(e, v); }
1373 1374

	
1374 1375
    /// \brief Returns the status of the given node
1375 1376
    ///
1376 1377
    /// This function returns the status of the given node.
1377 1378
    /// It is \c true if the given node is enabled (i.e. not hidden).
1378 1379
    bool status(const Node& n) const { return Parent::status(n); }
1379 1380

	
1380 1381
    /// \brief Returns the status of the given edge
1381 1382
    ///
1382 1383
    /// This function returns the status of the given edge.
1383 1384
    /// It is \c true if the given edge is enabled (i.e. not hidden).
1384 1385
    bool status(const Edge& e) const { return Parent::status(e); }
1385 1386

	
1386 1387
    /// \brief Disables the given node
1387 1388
    ///
1388 1389
    /// This function disables the given node in the subdigraph,
1389 1390
    /// so the iteration jumps over it.
1390 1391
    /// It is the same as \ref status() "status(n, false)".
1391 1392
    void disable(const Node& n) const { Parent::status(n, false); }
1392 1393

	
1393 1394
    /// \brief Disables the given edge
1394 1395
    ///
1395 1396
    /// This function disables the given edge in the subgraph,
1396 1397
    /// so the iteration jumps over it.
1397 1398
    /// It is the same as \ref status() "status(e, false)".
1398 1399
    void disable(const Edge& e) const { Parent::status(e, false); }
1399 1400

	
1400 1401
    /// \brief Enables the given node
1401 1402
    ///
1402 1403
    /// This function enables the given node in the subdigraph.
1403 1404
    /// It is the same as \ref status() "status(n, true)".
1404 1405
    void enable(const Node& n) const { Parent::status(n, true); }
1405 1406

	
1406 1407
    /// \brief Enables the given edge
1407 1408
    ///
1408 1409
    /// This function enables the given edge in the subgraph.
1409 1410
    /// It is the same as \ref status() "status(e, true)".
1410 1411
    void enable(const Edge& e) const { Parent::status(e, true); }
1411 1412

	
1412 1413
  };
1413 1414

	
1414 1415
  /// \brief Returns a read-only SubGraph adaptor
1415 1416
  ///
1416 1417
  /// This function just returns a read-only \ref SubGraph adaptor.
1417 1418
  /// \ingroup graph_adaptors
1418 1419
  /// \relates SubGraph
1419 1420
  template<typename GR, typename NF, typename EF>
1420 1421
  SubGraph<const GR, NF, EF>
1421 1422
  subGraph(const GR& graph, NF& node_filter, EF& edge_filter) {
1422 1423
    return SubGraph<const GR, NF, EF>
1423 1424
      (graph, node_filter, edge_filter);
1424 1425
  }
1425 1426

	
1426 1427
  template<typename GR, typename NF, typename EF>
1427 1428
  SubGraph<const GR, const NF, EF>
1428 1429
  subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) {
1429 1430
    return SubGraph<const GR, const NF, EF>
1430 1431
      (graph, node_filter, edge_filter);
1431 1432
  }
1432 1433

	
1433 1434
  template<typename GR, typename NF, typename EF>
1434 1435
  SubGraph<const GR, NF, const EF>
1435 1436
  subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) {
1436 1437
    return SubGraph<const GR, NF, const EF>
1437 1438
      (graph, node_filter, edge_filter);
1438 1439
  }
1439 1440

	
1440 1441
  template<typename GR, typename NF, typename EF>
1441 1442
  SubGraph<const GR, const NF, const EF>
1442 1443
  subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) {
1443 1444
    return SubGraph<const GR, const NF, const EF>
1444 1445
      (graph, node_filter, edge_filter);
1445 1446
  }
1446 1447

	
1447 1448

	
1448 1449
  /// \ingroup graph_adaptors
1449 1450
  ///
1450 1451
  /// \brief Adaptor class for hiding nodes in a digraph or a graph.
1451 1452
  ///
1452 1453
  /// FilterNodes adaptor can be used for hiding nodes in a digraph or a
1453 1454
  /// graph. A \c bool node map must be specified, which defines the filter
1454 1455
  /// for the nodes. Only the nodes with \c true filter value and the
1455 1456
  /// arcs/edges incident to nodes both with \c true filter value are shown
1456 1457
  /// in the subgraph. This adaptor conforms to the \ref concepts::Digraph
1457 1458
  /// "Digraph" concept or the \ref concepts::Graph "Graph" concept
1458 1459
  /// depending on the \c GR template parameter.
1459 1460
  ///
1460 1461
  /// The adapted (di)graph can also be modified through this adaptor
1461 1462
  /// by adding or removing nodes or arcs/edges, unless the \c GR template
1462 1463
  /// parameter is set to be \c const.
1463 1464
  ///
1464 1465
  /// \tparam GR The type of the adapted digraph or graph.
1465 1466
  /// It must conform to the \ref concepts::Digraph "Digraph" concept
1466 1467
  /// or the \ref concepts::Graph "Graph" concept.
1467 1468
  /// It can also be specified to be \c const.
1468 1469
  /// \tparam NF The type of the node filter map.
1469 1470
  /// It must be a \c bool (or convertible) node map of the
1470 1471
  /// adapted (di)graph. The default type is
1471 1472
  /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1472 1473
  ///
1473 1474
  /// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the
1474 1475
  /// adapted (di)graph are convertible to each other.
1475 1476
#ifdef DOXYGEN
1476 1477
  template<typename GR, typename NF>
1477 1478
  class FilterNodes {
1478 1479
#else
1479 1480
  template<typename GR,
1480 1481
           typename NF = typename GR::template NodeMap<bool>,
1481 1482
           typename Enable = void>
1482 1483
  class FilterNodes :
1483 1484
    public DigraphAdaptorExtender<
1484 1485
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >,
1485 1486
                     true> > {
1486 1487
#endif
1487 1488
  public:
1488 1489

	
1489 1490
    typedef GR Digraph;
1490 1491
    typedef NF NodeFilterMap;
1491 1492

	
1492 1493
    typedef DigraphAdaptorExtender<
1493 1494
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, 
1494 1495
                     true> > Parent;
1495 1496

	
1496 1497
    typedef typename Parent::Node Node;
1497 1498

	
1498 1499
  protected:
1499 1500
    ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map;
1500 1501

	
1501 1502
    FilterNodes() : const_true_map() {}
1502 1503

	
1503 1504
  public:
1504 1505

	
1505 1506
    /// \brief Constructor
1506 1507
    ///
1507 1508
    /// Creates a subgraph for the given digraph or graph with the
1508 1509
    /// given node filter map.
1509 1510
    FilterNodes(GR& graph, NF& node_filter) 
1510 1511
      : Parent(), const_true_map()
1511 1512
    {
1512 1513
      Parent::initialize(graph, node_filter, const_true_map);
1513 1514
    }
1514 1515

	
1515 1516
    /// \brief Sets the status of the given node
1516 1517
    ///
1517 1518
    /// This function sets the status of the given node.
1518 1519
    /// It is done by simply setting the assigned value of \c n
1519 1520
    /// to \c v in the node filter map.
1520 1521
    void status(const Node& n, bool v) const { Parent::status(n, v); }
1521 1522

	
1522 1523
    /// \brief Returns the status of the given node
1523 1524
    ///
1524 1525
    /// This function returns the status of the given node.
1525 1526
    /// It is \c true if the given node is enabled (i.e. not hidden).
1526 1527
    bool status(const Node& n) const { return Parent::status(n); }
1527 1528

	
1528 1529
    /// \brief Disables the given node
1529 1530
    ///
1530 1531
    /// This function disables the given node, so the iteration
1531 1532
    /// jumps over it.
1532 1533
    /// It is the same as \ref status() "status(n, false)".
1533 1534
    void disable(const Node& n) const { Parent::status(n, false); }
1534 1535

	
1535 1536
    /// \brief Enables the given node
1536 1537
    ///
1537 1538
    /// This function enables the given node.
1538 1539
    /// It is the same as \ref status() "status(n, true)".
1539 1540
    void enable(const Node& n) const { Parent::status(n, true); }
1540 1541

	
1541 1542
  };
1542 1543

	
1543 1544
  template<typename GR, typename NF>
1544 1545
  class FilterNodes<GR, NF,
1545 1546
                    typename enable_if<UndirectedTagIndicator<GR> >::type> :
1546 1547
    public GraphAdaptorExtender<
1547 1548
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, 
1548 1549
                   true> > {
1549 1550

	
1550 1551
  public:
1551 1552
    typedef GR Graph;
1552 1553
    typedef NF NodeFilterMap;
1553 1554
    typedef GraphAdaptorExtender<
1554 1555
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, 
1555 1556
                   true> > Parent;
1556 1557

	
1557 1558
    typedef typename Parent::Node Node;
1558 1559
  protected:
1559 1560
    ConstMap<typename GR::Edge, Const<bool, true> > const_true_map;
1560 1561

	
1561 1562
    FilterNodes() : const_true_map() {}
1562 1563

	
1563 1564
  public:
1564 1565

	
1565 1566
    FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1566 1567
      Parent(), const_true_map() {
1567 1568
      Parent::initialize(graph, node_filter, const_true_map);
1568 1569
    }
1569 1570

	
1570 1571
    void status(const Node& n, bool v) const { Parent::status(n, v); }
1571 1572
    bool status(const Node& n) const { return Parent::status(n); }
1572 1573
    void disable(const Node& n) const { Parent::status(n, false); }
1573 1574
    void enable(const Node& n) const { Parent::status(n, true); }
1574 1575

	
1575 1576
  };
1576 1577

	
1577 1578

	
1578 1579
  /// \brief Returns a read-only FilterNodes adaptor
1579 1580
  ///
1580 1581
  /// This function just returns a read-only \ref FilterNodes adaptor.
1581 1582
  /// \ingroup graph_adaptors
1582 1583
  /// \relates FilterNodes
1583 1584
  template<typename GR, typename NF>
1584 1585
  FilterNodes<const GR, NF>
1585 1586
  filterNodes(const GR& graph, NF& node_filter) {
1586 1587
    return FilterNodes<const GR, NF>(graph, node_filter);
1587 1588
  }
1588 1589

	
1589 1590
  template<typename GR, typename NF>
1590 1591
  FilterNodes<const GR, const NF>
1591 1592
  filterNodes(const GR& graph, const NF& node_filter) {
1592 1593
    return FilterNodes<const GR, const NF>(graph, node_filter);
1593 1594
  }
1594 1595

	
1595 1596
  /// \ingroup graph_adaptors
1596 1597
  ///
1597 1598
  /// \brief Adaptor class for hiding arcs in a digraph.
1598 1599
  ///
1599 1600
  /// FilterArcs adaptor can be used for hiding arcs in a digraph.
1600 1601
  /// A \c bool arc map must be specified, which defines the filter for
1601 1602
  /// the arcs. Only the arcs with \c true filter value are shown in the
1602 1603
  /// subdigraph. This adaptor conforms to the \ref concepts::Digraph
1603 1604
  /// "Digraph" concept.
1604 1605
  ///
1605 1606
  /// The adapted digraph can also be modified through this adaptor
1606 1607
  /// by adding or removing nodes or arcs, unless the \c GR template
1607 1608
  /// parameter is set to be \c const.
1608 1609
  ///
1609 1610
  /// \tparam DGR The type of the adapted digraph.
1610 1611
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
1611 1612
  /// It can also be specified to be \c const.
1612 1613
  /// \tparam AF The type of the arc filter map.
1613 1614
  /// It must be a \c bool (or convertible) arc map of the
1614 1615
  /// adapted digraph. The default type is
1615 1616
  /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
1616 1617
  ///
1617 1618
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
1618 1619
  /// digraph are convertible to each other.
1619 1620
#ifdef DOXYGEN
1620 1621
  template<typename DGR,
1621 1622
           typename AF>
1622 1623
  class FilterArcs {
1623 1624
#else
1624 1625
  template<typename DGR,
1625 1626
           typename AF = typename DGR::template ArcMap<bool> >
1626 1627
  class FilterArcs :
1627 1628
    public DigraphAdaptorExtender<
1628 1629
      SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >,
1629 1630
                     AF, false> > {
1630 1631
#endif
1631 1632
  public:
1632 1633
    /// The type of the adapted digraph.
1633 1634
    typedef DGR Digraph;
1634 1635
    /// The type of the arc filter map.
1635 1636
    typedef AF ArcFilterMap;
1636 1637

	
1637 1638
    typedef DigraphAdaptorExtender<
1638 1639
      SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, 
1639 1640
                     AF, false> > Parent;
1640 1641

	
1641 1642
    typedef typename Parent::Arc Arc;
1642 1643

	
1643 1644
  protected:
1644 1645
    ConstMap<typename DGR::Node, Const<bool, true> > const_true_map;
1645 1646

	
1646 1647
    FilterArcs() : const_true_map() {}
1647 1648

	
1648 1649
  public:
1649 1650

	
1650 1651
    /// \brief Constructor
1651 1652
    ///
1652 1653
    /// Creates a subdigraph for the given digraph with the given arc
1653 1654
    /// filter map.
1654 1655
    FilterArcs(DGR& digraph, ArcFilterMap& arc_filter)
1655 1656
      : Parent(), const_true_map() {
1656 1657
      Parent::initialize(digraph, const_true_map, arc_filter);
1657 1658
    }
1658 1659

	
1659 1660
    /// \brief Sets the status of the given arc
1660 1661
    ///
1661 1662
    /// This function sets the status of the given arc.
1662 1663
    /// It is done by simply setting the assigned value of \c a
1663 1664
    /// to \c v in the arc filter map.
1664 1665
    void status(const Arc& a, bool v) const { Parent::status(a, v); }
1665 1666

	
1666 1667
    /// \brief Returns the status of the given arc
1667 1668
    ///
1668 1669
    /// This function returns the status of the given arc.
1669 1670
    /// It is \c true if the given arc is enabled (i.e. not hidden).
1670 1671
    bool status(const Arc& a) const { return Parent::status(a); }
1671 1672

	
1672 1673
    /// \brief Disables the given arc
1673 1674
    ///
1674 1675
    /// This function disables the given arc in the subdigraph,
1675 1676
    /// so the iteration jumps over it.
1676 1677
    /// It is the same as \ref status() "status(a, false)".
1677 1678
    void disable(const Arc& a) const { Parent::status(a, false); }
1678 1679

	
1679 1680
    /// \brief Enables the given arc
1680 1681
    ///
1681 1682
    /// This function enables the given arc in the subdigraph.
1682 1683
    /// It is the same as \ref status() "status(a, true)".
1683 1684
    void enable(const Arc& a) const { Parent::status(a, true); }
1684 1685

	
1685 1686
  };
1686 1687

	
1687 1688
  /// \brief Returns a read-only FilterArcs adaptor
1688 1689
  ///
1689 1690
  /// This function just returns a read-only \ref FilterArcs adaptor.
1690 1691
  /// \ingroup graph_adaptors
1691 1692
  /// \relates FilterArcs
1692 1693
  template<typename DGR, typename AF>
1693 1694
  FilterArcs<const DGR, AF>
1694 1695
  filterArcs(const DGR& digraph, AF& arc_filter) {
1695 1696
    return FilterArcs<const DGR, AF>(digraph, arc_filter);
1696 1697
  }
1697 1698

	
1698 1699
  template<typename DGR, typename AF>
1699 1700
  FilterArcs<const DGR, const AF>
1700 1701
  filterArcs(const DGR& digraph, const AF& arc_filter) {
1701 1702
    return FilterArcs<const DGR, const AF>(digraph, arc_filter);
1702 1703
  }
1703 1704

	
1704 1705
  /// \ingroup graph_adaptors
1705 1706
  ///
1706 1707
  /// \brief Adaptor class for hiding edges in a graph.
1707 1708
  ///
1708 1709
  /// FilterEdges adaptor can be used for hiding edges in a graph.
1709 1710
  /// A \c bool edge map must be specified, which defines the filter for
1710 1711
  /// the edges. Only the edges with \c true filter value are shown in the
1711 1712
  /// subgraph. This adaptor conforms to the \ref concepts::Graph
1712 1713
  /// "Graph" concept.
1713 1714
  ///
1714 1715
  /// The adapted graph can also be modified through this adaptor
1715 1716
  /// by adding or removing nodes or edges, unless the \c GR template
1716 1717
  /// parameter is set to be \c const.
1717 1718
  ///
1718 1719
  /// \tparam GR The type of the adapted graph.
1719 1720
  /// It must conform to the \ref concepts::Graph "Graph" concept.
1720 1721
  /// It can also be specified to be \c const.
1721 1722
  /// \tparam EF The type of the edge filter map.
1722 1723
  /// It must be a \c bool (or convertible) edge map of the
1723 1724
  /// adapted graph. The default type is
1724 1725
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1725 1726
  ///
1726 1727
  /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1727 1728
  /// adapted graph are convertible to each other.
1728 1729
#ifdef DOXYGEN
1729 1730
  template<typename GR,
1730 1731
           typename EF>
1731 1732
  class FilterEdges {
1732 1733
#else
1733 1734
  template<typename GR,
1734 1735
           typename EF = typename GR::template EdgeMap<bool> >
1735 1736
  class FilterEdges :
1736 1737
    public GraphAdaptorExtender<
1737 1738
      SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, 
1738 1739
                   EF, false> > {
1739 1740
#endif
1740 1741
  public:
1741 1742
    /// The type of the adapted graph.
1742 1743
    typedef GR Graph;
1743 1744
    /// The type of the edge filter map.
1744 1745
    typedef EF EdgeFilterMap;
1745 1746

	
1746 1747
    typedef GraphAdaptorExtender<
1747 1748
      SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, 
1748 1749
                   EF, false> > Parent;
1749 1750

	
1750 1751
    typedef typename Parent::Edge Edge;
1751 1752

	
1752 1753
  protected:
1753 1754
    ConstMap<typename GR::Node, Const<bool, true> > const_true_map;
1754 1755

	
1755 1756
    FilterEdges() : const_true_map(true) {
1756 1757
      Parent::setNodeFilterMap(const_true_map);
1757 1758
    }
1758 1759

	
1759 1760
  public:
1760 1761

	
1761 1762
    /// \brief Constructor
1762 1763
    ///
1763 1764
    /// Creates a subgraph for the given graph with the given edge
1764 1765
    /// filter map.
1765 1766
    FilterEdges(GR& graph, EF& edge_filter) 
1766 1767
      : Parent(), const_true_map() {
1767 1768
      Parent::initialize(graph, const_true_map, edge_filter);
1768 1769
    }
1769 1770

	
1770 1771
    /// \brief Sets the status of the given edge
1771 1772
    ///
1772 1773
    /// This function sets the status of the given edge.
1773 1774
    /// It is done by simply setting the assigned value of \c e
1774 1775
    /// to \c v in the edge filter map.
1775 1776
    void status(const Edge& e, bool v) const { Parent::status(e, v); }
1776 1777

	
1777 1778
    /// \brief Returns the status of the given edge
1778 1779
    ///
1779 1780
    /// This function returns the status of the given edge.
1780 1781
    /// It is \c true if the given edge is enabled (i.e. not hidden).
1781 1782
    bool status(const Edge& e) const { return Parent::status(e); }
1782 1783

	
1783 1784
    /// \brief Disables the given edge
1784 1785
    ///
1785 1786
    /// This function disables the given edge in the subgraph,
1786 1787
    /// so the iteration jumps over it.
1787 1788
    /// It is the same as \ref status() "status(e, false)".
1788 1789
    void disable(const Edge& e) const { Parent::status(e, false); }
1789 1790

	
1790 1791
    /// \brief Enables the given edge
1791 1792
    ///
1792 1793
    /// This function enables the given edge in the subgraph.
1793 1794
    /// It is the same as \ref status() "status(e, true)".
1794 1795
    void enable(const Edge& e) const { Parent::status(e, true); }
1795 1796

	
1796 1797
  };
1797 1798

	
1798 1799
  /// \brief Returns a read-only FilterEdges adaptor
1799 1800
  ///
1800 1801
  /// This function just returns a read-only \ref FilterEdges adaptor.
1801 1802
  /// \ingroup graph_adaptors
1802 1803
  /// \relates FilterEdges
1803 1804
  template<typename GR, typename EF>
1804 1805
  FilterEdges<const GR, EF>
1805 1806
  filterEdges(const GR& graph, EF& edge_filter) {
1806 1807
    return FilterEdges<const GR, EF>(graph, edge_filter);
1807 1808
  }
1808 1809

	
1809 1810
  template<typename GR, typename EF>
1810 1811
  FilterEdges<const GR, const EF>
1811 1812
  filterEdges(const GR& graph, const EF& edge_filter) {
1812 1813
    return FilterEdges<const GR, const EF>(graph, edge_filter);
1813 1814
  }
1814 1815

	
1815 1816

	
1816 1817
  template <typename DGR>
1817 1818
  class UndirectorBase {
1818 1819
  public:
1819 1820
    typedef DGR Digraph;
1820 1821
    typedef UndirectorBase Adaptor;
1821 1822

	
1822 1823
    typedef True UndirectedTag;
1823 1824

	
1824 1825
    typedef typename Digraph::Arc Edge;
1825 1826
    typedef typename Digraph::Node Node;
1826 1827

	
1827 1828
    class Arc : public Edge {
1828 1829
      friend class UndirectorBase;
1829 1830
    protected:
1830 1831
      bool _forward;
1831 1832

	
1832 1833
      Arc(const Edge& edge, bool forward) :
1833 1834
        Edge(edge), _forward(forward) {}
1834 1835

	
1835 1836
    public:
1836 1837
      Arc() {}
1837 1838

	
1838 1839
      Arc(Invalid) : Edge(INVALID), _forward(true) {}
1839 1840

	
1840 1841
      bool operator==(const Arc &other) const {
1841 1842
        return _forward == other._forward &&
1842 1843
          static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1843 1844
      }
1844 1845
      bool operator!=(const Arc &other) const {
1845 1846
        return _forward != other._forward ||
1846 1847
          static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1847 1848
      }
1848 1849
      bool operator<(const Arc &other) const {
1849 1850
        return _forward < other._forward ||
1850 1851
          (_forward == other._forward &&
1851 1852
           static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1852 1853
      }
1853 1854
    };
1854 1855

	
1855 1856
    void first(Node& n) const {
1856 1857
      _digraph->first(n);
1857 1858
    }
1858 1859

	
1859 1860
    void next(Node& n) const {
1860 1861
      _digraph->next(n);
1861 1862
    }
1862 1863

	
1863 1864
    void first(Arc& a) const {
1864 1865
      _digraph->first(a);
1865 1866
      a._forward = true;
1866 1867
    }
1867 1868

	
1868 1869
    void next(Arc& a) const {
1869 1870
      if (a._forward) {
1870 1871
        a._forward = false;
1871 1872
      } else {
1872 1873
        _digraph->next(a);
1873 1874
        a._forward = true;
1874 1875
      }
1875 1876
    }
1876 1877

	
1877 1878
    void first(Edge& e) const {
1878 1879
      _digraph->first(e);
1879 1880
    }
1880 1881

	
1881 1882
    void next(Edge& e) const {
1882 1883
      _digraph->next(e);
1883 1884
    }
1884 1885

	
1885 1886
    void firstOut(Arc& a, const Node& n) const {
1886 1887
      _digraph->firstIn(a, n);
1887 1888
      if( static_cast<const Edge&>(a) != INVALID ) {
1888 1889
        a._forward = false;
1889 1890
      } else {
1890 1891
        _digraph->firstOut(a, n);
1891 1892
        a._forward = true;
1892 1893
      }
1893 1894
    }
1894 1895
    void nextOut(Arc &a) const {
1895 1896
      if (!a._forward) {
1896 1897
        Node n = _digraph->target(a);
1897 1898
        _digraph->nextIn(a);
1898 1899
        if (static_cast<const Edge&>(a) == INVALID ) {
1899 1900
          _digraph->firstOut(a, n);
1900 1901
          a._forward = true;
1901 1902
        }
1902 1903
      }
1903 1904
      else {
1904 1905
        _digraph->nextOut(a);
1905 1906
      }
1906 1907
    }
1907 1908

	
1908 1909
    void firstIn(Arc &a, const Node &n) const {
1909 1910
      _digraph->firstOut(a, n);
1910 1911
      if (static_cast<const Edge&>(a) != INVALID ) {
1911 1912
        a._forward = false;
1912 1913
      } else {
1913 1914
        _digraph->firstIn(a, n);
1914 1915
        a._forward = true;
1915 1916
      }
1916 1917
    }
1917 1918
    void nextIn(Arc &a) const {
1918 1919
      if (!a._forward) {
1919 1920
        Node n = _digraph->source(a);
1920 1921
        _digraph->nextOut(a);
1921 1922
        if( static_cast<const Edge&>(a) == INVALID ) {
1922 1923
          _digraph->firstIn(a, n);
1923 1924
          a._forward = true;
1924 1925
        }
1925 1926
      }
1926 1927
      else {
1927 1928
        _digraph->nextIn(a);
1928 1929
      }
1929 1930
    }
1930 1931

	
1931 1932
    void firstInc(Edge &e, bool &d, const Node &n) const {
1932 1933
      d = true;
1933 1934
      _digraph->firstOut(e, n);
1934 1935
      if (e != INVALID) return;
1935 1936
      d = false;
1936 1937
      _digraph->firstIn(e, n);
1937 1938
    }
1938 1939

	
1939 1940
    void nextInc(Edge &e, bool &d) const {
1940 1941
      if (d) {
1941 1942
        Node s = _digraph->source(e);
1942 1943
        _digraph->nextOut(e);
1943 1944
        if (e != INVALID) return;
1944 1945
        d = false;
1945 1946
        _digraph->firstIn(e, s);
1946 1947
      } else {
1947 1948
        _digraph->nextIn(e);
1948 1949
      }
1949 1950
    }
1950 1951

	
1951 1952
    Node u(const Edge& e) const {
1952 1953
      return _digraph->source(e);
1953 1954
    }
1954 1955

	
1955 1956
    Node v(const Edge& e) const {
1956 1957
      return _digraph->target(e);
1957 1958
    }
1958 1959

	
1959 1960
    Node source(const Arc &a) const {
1960 1961
      return a._forward ? _digraph->source(a) : _digraph->target(a);
1961 1962
    }
1962 1963

	
1963 1964
    Node target(const Arc &a) const {
1964 1965
      return a._forward ? _digraph->target(a) : _digraph->source(a);
1965 1966
    }
1966 1967

	
1967 1968
    static Arc direct(const Edge &e, bool d) {
1968 1969
      return Arc(e, d);
1969 1970
    }
1970 1971
    Arc direct(const Edge &e, const Node& n) const {
1971 1972
      return Arc(e, _digraph->source(e) == n);
1972 1973
    }
1973 1974

	
1974 1975
    static bool direction(const Arc &a) { return a._forward; }
1975 1976

	
1976 1977
    Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
1977 1978
    Arc arcFromId(int ix) const {
1978 1979
      return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
1979 1980
    }
1980 1981
    Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
1981 1982

	
1982 1983
    int id(const Node &n) const { return _digraph->id(n); }
1983 1984
    int id(const Arc &a) const {
1984 1985
      return  (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
1985 1986
    }
1986 1987
    int id(const Edge &e) const { return _digraph->id(e); }
1987 1988

	
1988 1989
    int maxNodeId() const { return _digraph->maxNodeId(); }
1989 1990
    int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
1990 1991
    int maxEdgeId() const { return _digraph->maxArcId(); }
1991 1992

	
1992 1993
    Node addNode() { return _digraph->addNode(); }
1993 1994
    Edge addEdge(const Node& u, const Node& v) {
1994 1995
      return _digraph->addArc(u, v);
1995 1996
    }
1996 1997

	
1997 1998
    void erase(const Node& i) { _digraph->erase(i); }
1998 1999
    void erase(const Edge& i) { _digraph->erase(i); }
1999 2000

	
2000 2001
    void clear() { _digraph->clear(); }
2001 2002

	
2002 2003
    typedef NodeNumTagIndicator<Digraph> NodeNumTag;
2003 2004
    int nodeNum() const { return _digraph->nodeNum(); }
2004 2005

	
2005 2006
    typedef ArcNumTagIndicator<Digraph> ArcNumTag;
2006 2007
    int arcNum() const { return 2 * _digraph->arcNum(); }
2007 2008

	
2008 2009
    typedef ArcNumTag EdgeNumTag;
2009 2010
    int edgeNum() const { return _digraph->arcNum(); }
2010 2011

	
2011 2012
    typedef FindArcTagIndicator<Digraph> FindArcTag;
2012 2013
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
2013 2014
      if (p == INVALID) {
2014 2015
        Edge arc = _digraph->findArc(s, t);
2015 2016
        if (arc != INVALID) return direct(arc, true);
2016 2017
        arc = _digraph->findArc(t, s);
2017 2018
        if (arc != INVALID) return direct(arc, false);
2018 2019
      } else if (direction(p)) {
2019 2020
        Edge arc = _digraph->findArc(s, t, p);
2020 2021
        if (arc != INVALID) return direct(arc, true);
2021 2022
        arc = _digraph->findArc(t, s);
2022 2023
        if (arc != INVALID) return direct(arc, false);
2023 2024
      } else {
2024 2025
        Edge arc = _digraph->findArc(t, s, p);
2025 2026
        if (arc != INVALID) return direct(arc, false);
2026 2027
      }
2027 2028
      return INVALID;
2028 2029
    }
2029 2030

	
2030 2031
    typedef FindArcTag FindEdgeTag;
2031 2032
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
2032 2033
      if (s != t) {
2033 2034
        if (p == INVALID) {
2034 2035
          Edge arc = _digraph->findArc(s, t);
2035 2036
          if (arc != INVALID) return arc;
2036 2037
          arc = _digraph->findArc(t, s);
2037 2038
          if (arc != INVALID) return arc;
2038 2039
        } else if (_digraph->source(p) == s) {
2039 2040
          Edge arc = _digraph->findArc(s, t, p);
2040 2041
          if (arc != INVALID) return arc;
2041 2042
          arc = _digraph->findArc(t, s);
2042 2043
          if (arc != INVALID) return arc;
2043 2044
        } else {
2044 2045
          Edge arc = _digraph->findArc(t, s, p);
2045 2046
          if (arc != INVALID) return arc;
2046 2047
        }
2047 2048
      } else {
2048 2049
        return _digraph->findArc(s, t, p);
2049 2050
      }
2050 2051
      return INVALID;
2051 2052
    }
2052 2053

	
2053 2054
  private:
2054 2055

	
2055 2056
    template <typename V>
2056 2057
    class ArcMapBase {
2057 2058
    private:
2058 2059

	
2059 2060
      typedef typename DGR::template ArcMap<V> MapImpl;
2060 2061

	
2061 2062
    public:
2062 2063

	
2063 2064
      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
2064 2065

	
2065 2066
      typedef V Value;
2066 2067
      typedef Arc Key;
2067 2068
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
2068 2069
      typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
2069 2070
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
2070 2071
      typedef typename MapTraits<MapImpl>::ReturnValue Reference;
2071 2072

	
2072 2073
      ArcMapBase(const UndirectorBase<DGR>& adaptor) :
2073 2074
        _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
2074 2075

	
2075 2076
      ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value)
2076 2077
        : _forward(*adaptor._digraph, value), 
2077 2078
          _backward(*adaptor._digraph, value) {}
2078 2079

	
2079 2080
      void set(const Arc& a, const V& value) {
2080 2081
        if (direction(a)) {
2081 2082
          _forward.set(a, value);
2082 2083
        } else {
2083 2084
          _backward.set(a, value);
2084 2085
        }
2085 2086
      }
2086 2087

	
2087 2088
      ConstReturnValue operator[](const Arc& a) const {
2088 2089
        if (direction(a)) {
2089 2090
          return _forward[a];
2090 2091
        } else {
2091 2092
          return _backward[a];
2092 2093
        }
2093 2094
      }
2094 2095

	
2095 2096
      ReturnValue operator[](const Arc& a) {
2096 2097
        if (direction(a)) {
2097 2098
          return _forward[a];
2098 2099
        } else {
2099 2100
          return _backward[a];
2100 2101
        }
2101 2102
      }
2102 2103

	
2103 2104
    protected:
2104 2105

	
2105 2106
      MapImpl _forward, _backward;
2106 2107

	
2107 2108
    };
2108 2109

	
2109 2110
  public:
2110 2111

	
2111 2112
    template <typename V>
2112 2113
    class NodeMap : public DGR::template NodeMap<V> {
2113 2114
    public:
2114 2115

	
2115 2116
      typedef V Value;
2116 2117
      typedef typename DGR::template NodeMap<Value> Parent;
2117 2118

	
2118 2119
      explicit NodeMap(const UndirectorBase<DGR>& adaptor)
2119 2120
        : Parent(*adaptor._digraph) {}
2120 2121

	
2121 2122
      NodeMap(const UndirectorBase<DGR>& adaptor, const V& value)
2122 2123
        : Parent(*adaptor._digraph, value) { }
2123 2124

	
2124 2125
    private:
2125 2126
      NodeMap& operator=(const NodeMap& cmap) {
2126 2127
        return operator=<NodeMap>(cmap);
2127 2128
      }
2128 2129

	
2129 2130
      template <typename CMap>
2130 2131
      NodeMap& operator=(const CMap& cmap) {
2131 2132
        Parent::operator=(cmap);
2132 2133
        return *this;
2133 2134
      }
2134 2135

	
2135 2136
    };
2136 2137

	
2137 2138
    template <typename V>
2138 2139
    class ArcMap
2139 2140
      : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> >
2140 2141
    {
2141 2142
    public:
2142 2143
      typedef V Value;
2143 2144
      typedef SubMapExtender<Adaptor, ArcMapBase<V> > Parent;
2144 2145

	
2145 2146
      explicit ArcMap(const UndirectorBase<DGR>& adaptor)
2146 2147
        : Parent(adaptor) {}
2147 2148

	
2148 2149
      ArcMap(const UndirectorBase<DGR>& adaptor, const V& value)
2149 2150
        : Parent(adaptor, value) {}
2150 2151

	
2151 2152
    private:
2152 2153
      ArcMap& operator=(const ArcMap& cmap) {
2153 2154
        return operator=<ArcMap>(cmap);
2154 2155
      }
2155 2156

	
2156 2157
      template <typename CMap>
2157 2158
      ArcMap& operator=(const CMap& cmap) {
2158 2159
        Parent::operator=(cmap);
2159 2160
        return *this;
2160 2161
      }
2161 2162
    };
2162 2163

	
2163 2164
    template <typename V>
2164 2165
    class EdgeMap : public Digraph::template ArcMap<V> {
2165 2166
    public:
2166 2167

	
2167 2168
      typedef V Value;
2168 2169
      typedef typename Digraph::template ArcMap<V> Parent;
2169 2170

	
2170 2171
      explicit EdgeMap(const UndirectorBase<DGR>& adaptor)
2171 2172
        : Parent(*adaptor._digraph) {}
2172 2173

	
2173 2174
      EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value)
2174 2175
        : Parent(*adaptor._digraph, value) {}
2175 2176

	
2176 2177
    private:
2177 2178
      EdgeMap& operator=(const EdgeMap& cmap) {
2178 2179
        return operator=<EdgeMap>(cmap);
2179 2180
      }
2180 2181

	
2181 2182
      template <typename CMap>
2182 2183
      EdgeMap& operator=(const CMap& cmap) {
2183 2184
        Parent::operator=(cmap);
2184 2185
        return *this;
2185 2186
      }
2186 2187

	
2187 2188
    };
2188 2189

	
2189 2190
    typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
2190 2191
    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
2191 2192

	
2192 2193
    typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier;
2193 2194
    EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2194 2195

	
2195 2196
  protected:
2196 2197

	
2197 2198
    UndirectorBase() : _digraph(0) {}
2198 2199

	
2199 2200
    DGR* _digraph;
2200 2201

	
2201 2202
    void initialize(DGR& digraph) {
2202 2203
      _digraph = &digraph;
2203 2204
    }
2204 2205

	
2205 2206
  };
2206 2207

	
2207 2208
  /// \ingroup graph_adaptors
2208 2209
  ///
2209 2210
  /// \brief Adaptor class for viewing a digraph as an undirected graph.
2210 2211
  ///
2211 2212
  /// Undirector adaptor can be used for viewing a digraph as an undirected
2212 2213
  /// graph. All arcs of the underlying digraph are showed in the
2213 2214
  /// adaptor as an edge (and also as a pair of arcs, of course).
2214 2215
  /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
2215 2216
  ///
2216 2217
  /// The adapted digraph can also be modified through this adaptor
2217 2218
  /// by adding or removing nodes or edges, unless the \c GR template
2218 2219
  /// parameter is set to be \c const.
2219 2220
  ///
2220 2221
  /// \tparam DGR The type of the adapted digraph.
2221 2222
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2222 2223
  /// It can also be specified to be \c const.
2223 2224
  ///
2224 2225
  /// \note The \c Node type of this adaptor and the adapted digraph are
2225 2226
  /// convertible to each other, moreover the \c Edge type of the adaptor
2226 2227
  /// and the \c Arc type of the adapted digraph are also convertible to
2227 2228
  /// each other.
2228 2229
  /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type
2229 2230
  /// of the adapted digraph.)
2230 2231
  template<typename DGR>
2231 2232
#ifdef DOXYGEN
2232 2233
  class Undirector {
2233 2234
#else
2234 2235
  class Undirector :
2235 2236
    public GraphAdaptorExtender<UndirectorBase<DGR> > {
2236 2237
#endif
2237 2238
  public:
2238 2239
    /// The type of the adapted digraph.
2239 2240
    typedef DGR Digraph;
2240 2241
    typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent;
2241 2242
  protected:
2242 2243
    Undirector() { }
2243 2244
  public:
2244 2245

	
2245 2246
    /// \brief Constructor
2246 2247
    ///
2247 2248
    /// Creates an undirected graph from the given digraph.
2248 2249
    Undirector(DGR& digraph) {
2249 2250
      initialize(digraph);
2250 2251
    }
2251 2252

	
2252 2253
    /// \brief Arc map combined from two original arc maps
2253 2254
    ///
2254 2255
    /// This map adaptor class adapts two arc maps of the underlying
2255 2256
    /// digraph to get an arc map of the undirected graph.
2256 2257
    /// Its value type is inherited from the first arc map type
2257 2258
    /// (\c %ForwardMap).
2258 2259
    template <typename ForwardMap, typename BackwardMap>
2259 2260
    class CombinedArcMap {
2260 2261
    public:
2261 2262

	
2262 2263
      /// The key type of the map
2263 2264
      typedef typename Parent::Arc Key;
2264 2265
      /// The value type of the map
2265 2266
      typedef typename ForwardMap::Value Value;
2266 2267

	
2267 2268
      typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2268 2269

	
2269 2270
      typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue;
2270 2271
      typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue;
2271 2272
      typedef typename MapTraits<ForwardMap>::ReturnValue Reference;
2272 2273
      typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference;
2273 2274

	
2274 2275
      /// Constructor
2275 2276
      CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
2276 2277
        : _forward(&forward), _backward(&backward) {}
2277 2278

	
2278 2279
      /// Sets the value associated with the given key.
2279 2280
      void set(const Key& e, const Value& a) {
2280 2281
        if (Parent::direction(e)) {
2281 2282
          _forward->set(e, a);
2282 2283
        } else {
2283 2284
          _backward->set(e, a);
2284 2285
        }
2285 2286
      }
2286 2287

	
2287 2288
      /// Returns the value associated with the given key.
2288 2289
      ConstReturnValue operator[](const Key& e) const {
2289 2290
        if (Parent::direction(e)) {
2290 2291
          return (*_forward)[e];
2291 2292
        } else {
2292 2293
          return (*_backward)[e];
2293 2294
        }
2294 2295
      }
2295 2296

	
2296 2297
      /// Returns a reference to the value associated with the given key.
2297 2298
      ReturnValue operator[](const Key& e) {
2298 2299
        if (Parent::direction(e)) {
2299 2300
          return (*_forward)[e];
2300 2301
        } else {
2301 2302
          return (*_backward)[e];
2302 2303
        }
2303 2304
      }
2304 2305

	
2305 2306
    protected:
2306 2307

	
2307 2308
      ForwardMap* _forward;
2308 2309
      BackwardMap* _backward;
2309 2310

	
2310 2311
    };
2311 2312

	
2312 2313
    /// \brief Returns a combined arc map
2313 2314
    ///
2314 2315
    /// This function just returns a combined arc map.
2315 2316
    template <typename ForwardMap, typename BackwardMap>
2316 2317
    static CombinedArcMap<ForwardMap, BackwardMap>
2317 2318
    combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
2318 2319
      return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
2319 2320
    }
2320 2321

	
2321 2322
    template <typename ForwardMap, typename BackwardMap>
2322 2323
    static CombinedArcMap<const ForwardMap, BackwardMap>
2323 2324
    combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
2324 2325
      return CombinedArcMap<const ForwardMap,
2325 2326
        BackwardMap>(forward, backward);
2326 2327
    }
2327 2328

	
2328 2329
    template <typename ForwardMap, typename BackwardMap>
2329 2330
    static CombinedArcMap<ForwardMap, const BackwardMap>
2330 2331
    combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
2331 2332
      return CombinedArcMap<ForwardMap,
2332 2333
        const BackwardMap>(forward, backward);
2333 2334
    }
2334 2335

	
2335 2336
    template <typename ForwardMap, typename BackwardMap>
2336 2337
    static CombinedArcMap<const ForwardMap, const BackwardMap>
2337 2338
    combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
2338 2339
      return CombinedArcMap<const ForwardMap,
2339 2340
        const BackwardMap>(forward, backward);
2340 2341
    }
2341 2342

	
2342 2343
  };
2343 2344

	
2344 2345
  /// \brief Returns a read-only Undirector adaptor
2345 2346
  ///
2346 2347
  /// This function just returns a read-only \ref Undirector adaptor.
2347 2348
  /// \ingroup graph_adaptors
2348 2349
  /// \relates Undirector
2349 2350
  template<typename DGR>
2350 2351
  Undirector<const DGR> undirector(const DGR& digraph) {
2351 2352
    return Undirector<const DGR>(digraph);
2352 2353
  }
2353 2354

	
2354 2355

	
2355 2356
  template <typename GR, typename DM>
2356 2357
  class OrienterBase {
2357 2358
  public:
2358 2359

	
2359 2360
    typedef GR Graph;
2360 2361
    typedef DM DirectionMap;
2361 2362

	
2362 2363
    typedef typename GR::Node Node;
2363 2364
    typedef typename GR::Edge Arc;
2364 2365

	
2365 2366
    void reverseArc(const Arc& arc) {
2366 2367
      _direction->set(arc, !(*_direction)[arc]);
2367 2368
    }
2368 2369

	
2369 2370
    void first(Node& i) const { _graph->first(i); }
2370 2371
    void first(Arc& i) const { _graph->first(i); }
2371 2372
    void firstIn(Arc& i, const Node& n) const {
2372 2373
      bool d = true;
2373 2374
      _graph->firstInc(i, d, n);
2374 2375
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2375 2376
    }
2376 2377
    void firstOut(Arc& i, const Node& n ) const {
2377 2378
      bool d = true;
2378 2379
      _graph->firstInc(i, d, n);
2379 2380
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2380 2381
    }
2381 2382

	
2382 2383
    void next(Node& i) const { _graph->next(i); }
2383 2384
    void next(Arc& i) const { _graph->next(i); }
2384 2385
    void nextIn(Arc& i) const {
2385 2386
      bool d = !(*_direction)[i];
2386 2387
      _graph->nextInc(i, d);
2387 2388
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2388 2389
    }
2389 2390
    void nextOut(Arc& i) const {
2390 2391
      bool d = (*_direction)[i];
2391 2392
      _graph->nextInc(i, d);
2392 2393
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2393 2394
    }
2394 2395

	
2395 2396
    Node source(const Arc& e) const {
2396 2397
      return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2397 2398
    }
2398 2399
    Node target(const Arc& e) const {
2399 2400
      return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2400 2401
    }
2401 2402

	
2402 2403
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
2403 2404
    int nodeNum() const { return _graph->nodeNum(); }
2404 2405

	
2405 2406
    typedef EdgeNumTagIndicator<Graph> ArcNumTag;
2406 2407
    int arcNum() const { return _graph->edgeNum(); }
2407 2408

	
2408 2409
    typedef FindEdgeTagIndicator<Graph> FindArcTag;
2409 2410
    Arc findArc(const Node& u, const Node& v,
2410 2411
                const Arc& prev = INVALID) const {
2411 2412
      Arc arc = _graph->findEdge(u, v, prev);
2412 2413
      while (arc != INVALID && source(arc) != u) {
2413 2414
        arc = _graph->findEdge(u, v, arc);
2414 2415
      }
2415 2416
      return arc;
2416 2417
    }
2417 2418

	
2418 2419
    Node addNode() {
2419 2420
      return Node(_graph->addNode());
2420 2421
    }
2421 2422

	
2422 2423
    Arc addArc(const Node& u, const Node& v) {
2423 2424
      Arc arc = _graph->addEdge(u, v);
2424 2425
      _direction->set(arc, _graph->u(arc) == u);
2425 2426
      return arc;
2426 2427
    }
2427 2428

	
2428 2429
    void erase(const Node& i) { _graph->erase(i); }
2429 2430
    void erase(const Arc& i) { _graph->erase(i); }
2430 2431

	
2431 2432
    void clear() { _graph->clear(); }
2432 2433

	
2433 2434
    int id(const Node& v) const { return _graph->id(v); }
2434 2435
    int id(const Arc& e) const { return _graph->id(e); }
2435 2436

	
2436 2437
    Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2437 2438
    Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2438 2439

	
2439 2440
    int maxNodeId() const { return _graph->maxNodeId(); }
2440 2441
    int maxArcId() const { return _graph->maxEdgeId(); }
2441 2442

	
2442 2443
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
2443 2444
    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2444 2445

	
2445 2446
    typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
2446 2447
    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2447 2448

	
2448 2449
    template <typename V>
2449 2450
    class NodeMap : public GR::template NodeMap<V> {
2450 2451
    public:
2451 2452

	
2452 2453
      typedef typename GR::template NodeMap<V> Parent;
2453 2454

	
2454 2455
      explicit NodeMap(const OrienterBase<GR, DM>& adapter)
2455 2456
        : Parent(*adapter._graph) {}
2456 2457

	
2457 2458
      NodeMap(const OrienterBase<GR, DM>& adapter, const V& value)
2458 2459
        : Parent(*adapter._graph, value) {}
2459 2460

	
2460 2461
    private:
2461 2462
      NodeMap& operator=(const NodeMap& cmap) {
2462 2463
        return operator=<NodeMap>(cmap);
2463 2464
      }
2464 2465

	
2465 2466
      template <typename CMap>
2466 2467
      NodeMap& operator=(const CMap& cmap) {
2467 2468
        Parent::operator=(cmap);
2468 2469
        return *this;
2469 2470
      }
2470 2471

	
2471 2472
    };
2472 2473

	
2473 2474
    template <typename V>
2474 2475
    class ArcMap : public GR::template EdgeMap<V> {
2475 2476
    public:
2476 2477

	
2477 2478
      typedef typename Graph::template EdgeMap<V> Parent;
2478 2479

	
2479 2480
      explicit ArcMap(const OrienterBase<GR, DM>& adapter)
2480 2481
        : Parent(*adapter._graph) { }
2481 2482

	
2482 2483
      ArcMap(const OrienterBase<GR, DM>& adapter, const V& value)
2483 2484
        : Parent(*adapter._graph, value) { }
2484 2485

	
2485 2486
    private:
2486 2487
      ArcMap& operator=(const ArcMap& cmap) {
2487 2488
        return operator=<ArcMap>(cmap);
2488 2489
      }
2489 2490

	
2490 2491
      template <typename CMap>
2491 2492
      ArcMap& operator=(const CMap& cmap) {
2492 2493
        Parent::operator=(cmap);
2493 2494
        return *this;
2494 2495
      }
2495 2496
    };
2496 2497

	
2497 2498

	
2498 2499

	
2499 2500
  protected:
2500 2501
    Graph* _graph;
2501 2502
    DM* _direction;
2502 2503

	
2503 2504
    void initialize(GR& graph, DM& direction) {
2504 2505
      _graph = &graph;
2505 2506
      _direction = &direction;
2506 2507
    }
2507 2508

	
2508 2509
  };
2509 2510

	
2510 2511
  /// \ingroup graph_adaptors
2511 2512
  ///
2512 2513
  /// \brief Adaptor class for orienting the edges of a graph to get a digraph
2513 2514
  ///
2514 2515
  /// Orienter adaptor can be used for orienting the edges of a graph to
2515 2516
  /// get a digraph. A \c bool edge map of the underlying graph must be
2516 2517
  /// specified, which define the direction of the arcs in the adaptor.
2517 2518
  /// The arcs can be easily reversed by the \c reverseArc() member function
2518 2519
  /// of the adaptor.
2519 2520
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2520 2521
  ///
2521 2522
  /// The adapted graph can also be modified through this adaptor
2522 2523
  /// by adding or removing nodes or arcs, unless the \c GR template
2523 2524
  /// parameter is set to be \c const.
2524 2525
  ///
2525 2526
  /// \tparam GR The type of the adapted graph.
2526 2527
  /// It must conform to the \ref concepts::Graph "Graph" concept.
2527 2528
  /// It can also be specified to be \c const.
2528 2529
  /// \tparam DM The type of the direction map.
2529 2530
  /// It must be a \c bool (or convertible) edge map of the
2530 2531
  /// adapted graph. The default type is
2531 2532
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
2532 2533
  ///
2533 2534
  /// \note The \c Node type of this adaptor and the adapted graph are
2534 2535
  /// convertible to each other, moreover the \c Arc type of the adaptor
2535 2536
  /// and the \c Edge type of the adapted graph are also convertible to
2536 2537
  /// each other.
2537 2538
#ifdef DOXYGEN
2538 2539
  template<typename GR,
2539 2540
           typename DM>
2540 2541
  class Orienter {
2541 2542
#else
2542 2543
  template<typename GR,
2543 2544
           typename DM = typename GR::template EdgeMap<bool> >
2544 2545
  class Orienter :
2545 2546
    public DigraphAdaptorExtender<OrienterBase<GR, DM> > {
2546 2547
#endif
2547 2548
  public:
2548 2549

	
2549 2550
    /// The type of the adapted graph.
2550 2551
    typedef GR Graph;
2551 2552
    /// The type of the direction edge map.
2552 2553
    typedef DM DirectionMap;
2553 2554

	
2554 2555
    typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
2555 2556
    typedef typename Parent::Arc Arc;
2556 2557
  protected:
2557 2558
    Orienter() { }
2558 2559
  public:
2559 2560

	
2560 2561
    /// \brief Constructor
2561 2562
    ///
2562 2563
    /// Constructor of the adaptor.
2563 2564
    Orienter(GR& graph, DM& direction) {
2564 2565
      Parent::initialize(graph, direction);
2565 2566
    }
2566 2567

	
2567 2568
    /// \brief Reverses the given arc
2568 2569
    ///
2569 2570
    /// This function reverses the given arc.
2570 2571
    /// It is done by simply negate the assigned value of \c a
2571 2572
    /// in the direction map.
2572 2573
    void reverseArc(const Arc& a) {
2573 2574
      Parent::reverseArc(a);
2574 2575
    }
2575 2576
  };
2576 2577

	
2577 2578
  /// \brief Returns a read-only Orienter adaptor
2578 2579
  ///
2579 2580
  /// This function just returns a read-only \ref Orienter adaptor.
2580 2581
  /// \ingroup graph_adaptors
2581 2582
  /// \relates Orienter
2582 2583
  template<typename GR, typename DM>
2583 2584
  Orienter<const GR, DM>
2584 2585
  orienter(const GR& graph, DM& direction) {
2585 2586
    return Orienter<const GR, DM>(graph, direction);
2586 2587
  }
2587 2588

	
2588 2589
  template<typename GR, typename DM>
2589 2590
  Orienter<const GR, const DM>
2590 2591
  orienter(const GR& graph, const DM& direction) {
2591 2592
    return Orienter<const GR, const DM>(graph, direction);
2592 2593
  }
2593 2594

	
2594 2595
  namespace _adaptor_bits {
2595 2596

	
2596 2597
    template <typename DGR, typename CM, typename FM, typename TL>
2597 2598
    class ResForwardFilter {
2598 2599
    public:
2599 2600

	
2600 2601
      typedef typename DGR::Arc Key;
2601 2602
      typedef bool Value;
2602 2603

	
2603 2604
    private:
2604 2605

	
2605 2606
      const CM* _capacity;
2606 2607
      const FM* _flow;
2607 2608
      TL _tolerance;
2608 2609

	
2609 2610
    public:
2610 2611

	
2611 2612
      ResForwardFilter(const CM& capacity, const FM& flow,
2612 2613
                       const TL& tolerance = TL())
2613 2614
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2614 2615

	
2615 2616
      bool operator[](const typename DGR::Arc& a) const {
2616 2617
        return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2617 2618
      }
2618 2619
    };
2619 2620

	
2620 2621
    template<typename DGR,typename CM, typename FM, typename TL>
2621 2622
    class ResBackwardFilter {
2622 2623
    public:
2623 2624

	
2624 2625
      typedef typename DGR::Arc Key;
2625 2626
      typedef bool Value;
2626 2627

	
2627 2628
    private:
2628 2629

	
2629 2630
      const CM* _capacity;
2630 2631
      const FM* _flow;
2631 2632
      TL _tolerance;
2632 2633

	
2633 2634
    public:
2634 2635

	
2635 2636
      ResBackwardFilter(const CM& capacity, const FM& flow,
2636 2637
                        const TL& tolerance = TL())
2637 2638
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2638 2639

	
2639 2640
      bool operator[](const typename DGR::Arc& a) const {
2640 2641
        return _tolerance.positive((*_flow)[a]);
2641 2642
      }
2642 2643
    };
2643 2644

	
2644 2645
  }
2645 2646

	
2646 2647
  /// \ingroup graph_adaptors
2647 2648
  ///
2648 2649
  /// \brief Adaptor class for composing the residual digraph for directed
2649 2650
  /// flow and circulation problems.
2650 2651
  ///
2651 2652
  /// ResidualDigraph can be used for composing the \e residual digraph
2652 2653
  /// for directed flow and circulation problems. Let \f$ G=(V, A) \f$
2653 2654
  /// be a directed graph and let \f$ F \f$ be a number type.
2654 2655
  /// Let \f$ flow, cap: A\to F \f$ be functions on the arcs.
2655 2656
  /// This adaptor implements a digraph structure with node set \f$ V \f$
2656 2657
  /// and arc set \f$ A_{forward}\cup A_{backward} \f$,
2657 2658
  /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and
2658 2659
  /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so
2659 2660
  /// called residual digraph.
2660 2661
  /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken,
2661 2662
  /// multiplicities are counted, i.e. the adaptor has exactly
2662 2663
  /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel
2663 2664
  /// arcs).
2664 2665
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2665 2666
  ///
2666 2667
  /// \tparam DGR The type of the adapted digraph.
2667 2668
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2668 2669
  /// It is implicitly \c const.
2669 2670
  /// \tparam CM The type of the capacity map.
2670 2671
  /// It must be an arc map of some numerical type, which defines
2671 2672
  /// the capacities in the flow problem. It is implicitly \c const.
2672 2673
  /// The default type is
2673 2674
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
2674 2675
  /// \tparam FM The type of the flow map.
2675 2676
  /// It must be an arc map of some numerical type, which defines
2676 2677
  /// the flow values in the flow problem. The default type is \c CM.
2677 2678
  /// \tparam TL The tolerance type for handling inexact computation.
2678 2679
  /// The default tolerance type depends on the value type of the
2679 2680
  /// capacity map.
2680 2681
  ///
2681 2682
  /// \note This adaptor is implemented using Undirector and FilterArcs
2682 2683
  /// adaptors.
2683 2684
  ///
2684 2685
  /// \note The \c Node type of this adaptor and the adapted digraph are
2685 2686
  /// convertible to each other, moreover the \c Arc type of the adaptor
2686 2687
  /// is convertible to the \c Arc type of the adapted digraph.
2687 2688
#ifdef DOXYGEN
2688 2689
  template<typename DGR, typename CM, typename FM, typename TL>
2689 2690
  class ResidualDigraph
2690 2691
#else
2691 2692
  template<typename DGR,
2692 2693
           typename CM = typename DGR::template ArcMap<int>,
2693 2694
           typename FM = CM,
2694 2695
           typename TL = Tolerance<typename CM::Value> >
2695 2696
  class ResidualDigraph 
2696 2697
    : public SubDigraph<
2697 2698
        Undirector<const DGR>,
2698 2699
        ConstMap<typename DGR::Node, Const<bool, true> >,
2699 2700
        typename Undirector<const DGR>::template CombinedArcMap<
2700 2701
          _adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>,
2701 2702
          _adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > >
2702 2703
#endif
2703 2704
  {
2704 2705
  public:
2705 2706

	
2706 2707
    /// The type of the underlying digraph.
2707 2708
    typedef DGR Digraph;
2708 2709
    /// The type of the capacity map.
2709 2710
    typedef CM CapacityMap;
2710 2711
    /// The type of the flow map.
2711 2712
    typedef FM FlowMap;
2712 2713
    /// The tolerance type.
2713 2714
    typedef TL Tolerance;
2714 2715

	
2715 2716
    typedef typename CapacityMap::Value Value;
2716 2717
    typedef ResidualDigraph Adaptor;
2717 2718

	
2718 2719
  protected:
2719 2720

	
2720 2721
    typedef Undirector<const Digraph> Undirected;
2721 2722

	
2722 2723
    typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter;
2723 2724

	
2724 2725
    typedef _adaptor_bits::ResForwardFilter<const DGR, CM,
2725 2726
                                            FM, TL> ForwardFilter;
2726 2727

	
2727 2728
    typedef _adaptor_bits::ResBackwardFilter<const DGR, CM,
2728 2729
                                             FM, TL> BackwardFilter;
2729 2730

	
2730 2731
    typedef typename Undirected::
2731 2732
      template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2732 2733

	
2733 2734
    typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent;
2734 2735

	
2735 2736
    const CapacityMap* _capacity;
2736 2737
    FlowMap* _flow;
2737 2738

	
2738 2739
    Undirected _graph;
2739 2740
    NodeFilter _node_filter;
2740 2741
    ForwardFilter _forward_filter;
2741 2742
    BackwardFilter _backward_filter;
2742 2743
    ArcFilter _arc_filter;
2743 2744

	
2744 2745
  public:
2745 2746

	
2746 2747
    /// \brief Constructor
2747 2748
    ///
2748 2749
    /// Constructor of the residual digraph adaptor. The parameters are the
2749 2750
    /// digraph, the capacity map, the flow map, and a tolerance object.
2750 2751
    ResidualDigraph(const DGR& digraph, const CM& capacity,
2751 2752
                    FM& flow, const TL& tolerance = Tolerance())
2752 2753
      : Parent(), _capacity(&capacity), _flow(&flow), 
2753 2754
        _graph(digraph), _node_filter(),
2754 2755
        _forward_filter(capacity, flow, tolerance),
2755 2756
        _backward_filter(capacity, flow, tolerance),
2756 2757
        _arc_filter(_forward_filter, _backward_filter)
2757 2758
    {
2758 2759
      Parent::initialize(_graph, _node_filter, _arc_filter);
2759 2760
    }
2760 2761

	
2761 2762
    typedef typename Parent::Arc Arc;
2762 2763

	
2763 2764
    /// \brief Returns the residual capacity of the given arc.
2764 2765
    ///
2765 2766
    /// Returns the residual capacity of the given arc.
2766 2767
    Value residualCapacity(const Arc& a) const {
2767 2768
      if (Undirected::direction(a)) {
2768 2769
        return (*_capacity)[a] - (*_flow)[a];
2769 2770
      } else {
2770 2771
        return (*_flow)[a];
2771 2772
      }
2772 2773
    }
2773 2774

	
2774 2775
    /// \brief Augments on the given arc in the residual digraph.
2775 2776
    ///
2776 2777
    /// Augments on the given arc in the residual digraph. It increases
2777 2778
    /// or decreases the flow value on the original arc according to the
2778 2779
    /// direction of the residual arc.
2779 2780
    void augment(const Arc& a, const Value& v) const {
2780 2781
      if (Undirected::direction(a)) {
2781 2782
        _flow->set(a, (*_flow)[a] + v);
2782 2783
      } else {
2783 2784
        _flow->set(a, (*_flow)[a] - v);
2784 2785
      }
2785 2786
    }
2786 2787

	
2787 2788
    /// \brief Returns \c true if the given residual arc is a forward arc.
2788 2789
    ///
2789 2790
    /// Returns \c true if the given residual arc has the same orientation
2790 2791
    /// as the original arc, i.e. it is a so called forward arc.
2791 2792
    static bool forward(const Arc& a) {
2792 2793
      return Undirected::direction(a);
2793 2794
    }
2794 2795

	
2795 2796
    /// \brief Returns \c true if the given residual arc is a backward arc.
2796 2797
    ///
2797 2798
    /// Returns \c true if the given residual arc has the opposite orientation
2798 2799
    /// than the original arc, i.e. it is a so called backward arc.
2799 2800
    static bool backward(const Arc& a) {
2800 2801
      return !Undirected::direction(a);
2801 2802
    }
2802 2803

	
2803 2804
    /// \brief Returns the forward oriented residual arc.
2804 2805
    ///
2805 2806
    /// Returns the forward oriented residual arc related to the given
2806 2807
    /// arc of the underlying digraph.
2807 2808
    static Arc forward(const typename Digraph::Arc& a) {
2808 2809
      return Undirected::direct(a, true);
2809 2810
    }
2810 2811

	
2811 2812
    /// \brief Returns the backward oriented residual arc.
2812 2813
    ///
2813 2814
    /// Returns the backward oriented residual arc related to the given
2814 2815
    /// arc of the underlying digraph.
2815 2816
    static Arc backward(const typename Digraph::Arc& a) {
2816 2817
      return Undirected::direct(a, false);
2817 2818
    }
2818 2819

	
2819 2820
    /// \brief Residual capacity map.
2820 2821
    ///
2821 2822
    /// This map adaptor class can be used for obtaining the residual
2822 2823
    /// capacities as an arc map of the residual digraph.
2823 2824
    /// Its value type is inherited from the capacity map.
2824 2825
    class ResidualCapacity {
2825 2826
    protected:
2826 2827
      const Adaptor* _adaptor;
2827 2828
    public:
2828 2829
      /// The key type of the map
2829 2830
      typedef Arc Key;
2830 2831
      /// The value type of the map
2831 2832
      typedef typename CapacityMap::Value Value;
2832 2833

	
2833 2834
      /// Constructor
2834 2835
      ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) 
2835 2836
        : _adaptor(&adaptor) {}
2836 2837

	
2837 2838
      /// Returns the value associated with the given residual arc
2838 2839
      Value operator[](const Arc& a) const {
2839 2840
        return _adaptor->residualCapacity(a);
2840 2841
      }
2841 2842

	
2842 2843
    };
2843 2844

	
2844 2845
    /// \brief Returns a residual capacity map
2845 2846
    ///
2846 2847
    /// This function just returns a residual capacity map.
2847 2848
    ResidualCapacity residualCapacity() const {
2848 2849
      return ResidualCapacity(*this);
2849 2850
    }
2850 2851

	
2851 2852
  };
2852 2853

	
2853 2854
  /// \brief Returns a (read-only) Residual adaptor
2854 2855
  ///
2855 2856
  /// This function just returns a (read-only) \ref ResidualDigraph adaptor.
2856 2857
  /// \ingroup graph_adaptors
2857 2858
  /// \relates ResidualDigraph
2858 2859
    template<typename DGR, typename CM, typename FM>
2859 2860
  ResidualDigraph<DGR, CM, FM>
2860 2861
  residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) {
2861 2862
    return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map);
2862 2863
  }
2863 2864

	
2864 2865

	
2865 2866
  template <typename DGR>
2866 2867
  class SplitNodesBase {
2867 2868
  public:
2868 2869

	
2869 2870
    typedef DGR Digraph;
2870 2871
    typedef DigraphAdaptorBase<const DGR> Parent;
2871 2872
    typedef SplitNodesBase Adaptor;
2872 2873

	
2873 2874
    typedef typename DGR::Node DigraphNode;
2874 2875
    typedef typename DGR::Arc DigraphArc;
2875 2876

	
2876 2877
    class Node;
2877 2878
    class Arc;
2878 2879

	
2879 2880
  private:
2880 2881

	
2881 2882
    template <typename T> class NodeMapBase;
2882 2883
    template <typename T> class ArcMapBase;
2883 2884

	
2884 2885
  public:
2885 2886

	
2886 2887
    class Node : public DigraphNode {
2887 2888
      friend class SplitNodesBase;
2888 2889
      template <typename T> friend class NodeMapBase;
2889 2890
    private:
2890 2891

	
2891 2892
      bool _in;
2892 2893
      Node(DigraphNode node, bool in)
2893 2894
        : DigraphNode(node), _in(in) {}
2894 2895

	
2895 2896
    public:
2896 2897

	
2897 2898
      Node() {}
2898 2899
      Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2899 2900

	
2900 2901
      bool operator==(const Node& node) const {
2901 2902
        return DigraphNode::operator==(node) && _in == node._in;
2902 2903
      }
2903 2904

	
2904 2905
      bool operator!=(const Node& node) const {
2905 2906
        return !(*this == node);
2906 2907
      }
2907 2908

	
2908 2909
      bool operator<(const Node& node) const {
2909 2910
        return DigraphNode::operator<(node) ||
2910 2911
          (DigraphNode::operator==(node) && _in < node._in);
2911 2912
      }
2912 2913
    };
2913 2914

	
2914 2915
    class Arc {
2915 2916
      friend class SplitNodesBase;
2916 2917
      template <typename T> friend class ArcMapBase;
2917 2918
    private:
2918 2919
      typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2919 2920

	
2920 2921
      explicit Arc(const DigraphArc& arc) : _item(arc) {}
2921 2922
      explicit Arc(const DigraphNode& node) : _item(node) {}
2922 2923

	
2923 2924
      ArcImpl _item;
2924 2925

	
2925 2926
    public:
2926 2927
      Arc() {}
2927 2928
      Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2928 2929

	
2929 2930
      bool operator==(const Arc& arc) const {
2930 2931
        if (_item.firstState()) {
2931 2932
          if (arc._item.firstState()) {
2932 2933
            return _item.first() == arc._item.first();
2933 2934
          }
2934 2935
        } else {
2935 2936
          if (arc._item.secondState()) {
2936 2937
            return _item.second() == arc._item.second();
2937 2938
          }
2938 2939
        }
2939 2940
        return false;
2940 2941
      }
2941 2942

	
2942 2943
      bool operator!=(const Arc& arc) const {
2943 2944
        return !(*this == arc);
2944 2945
      }
2945 2946

	
2946 2947
      bool operator<(const Arc& arc) const {
2947 2948
        if (_item.firstState()) {
2948 2949
          if (arc._item.firstState()) {
2949 2950
            return _item.first() < arc._item.first();
2950 2951
          }
2951 2952
          return false;
2952 2953
        } else {
2953 2954
          if (arc._item.secondState()) {
2954 2955
            return _item.second() < arc._item.second();
2955 2956
          }
2956 2957
          return true;
2957 2958
        }
2958 2959
      }
2959 2960

	
2960 2961
      operator DigraphArc() const { return _item.first(); }
2961 2962
      operator DigraphNode() const { return _item.second(); }
2962 2963

	
2963 2964
    };
2964 2965

	
2965 2966
    void first(Node& n) const {
2966 2967
      _digraph->first(n);
2967 2968
      n._in = true;
2968 2969
    }
2969 2970

	
2970 2971
    void next(Node& n) const {
2971 2972
      if (n._in) {
2972 2973
        n._in = false;
2973 2974
      } else {
2974 2975
        n._in = true;
2975 2976
        _digraph->next(n);
2976 2977
      }
2977 2978
    }
2978 2979

	
2979 2980
    void first(Arc& e) const {
2980 2981
      e._item.setSecond();
2981 2982
      _digraph->first(e._item.second());
2982 2983
      if (e._item.second() == INVALID) {
2983 2984
        e._item.setFirst();
2984 2985
        _digraph->first(e._item.first());
2985 2986
      }
2986 2987
    }
2987 2988

	
2988 2989
    void next(Arc& e) const {
2989 2990
      if (e._item.secondState()) {
2990 2991
        _digraph->next(e._item.second());
2991 2992
        if (e._item.second() == INVALID) {
2992 2993
          e._item.setFirst();
2993 2994
          _digraph->first(e._item.first());
2994 2995
        }
2995 2996
      } else {
2996 2997
        _digraph->next(e._item.first());
2997 2998
      }
2998 2999
    }
2999 3000

	
3000 3001
    void firstOut(Arc& e, const Node& n) const {
3001 3002
      if (n._in) {
3002 3003
        e._item.setSecond(n);
3003 3004
      } else {
3004 3005
        e._item.setFirst();
3005 3006
        _digraph->firstOut(e._item.first(), n);
3006 3007
      }
3007 3008
    }
3008 3009

	
3009 3010
    void nextOut(Arc& e) const {
3010 3011
      if (!e._item.firstState()) {
3011 3012
        e._item.setFirst(INVALID);
3012 3013
      } else {
3013 3014
        _digraph->nextOut(e._item.first());
3014 3015
      }
3015 3016
    }
3016 3017

	
3017 3018
    void firstIn(Arc& e, const Node& n) const {
3018 3019
      if (!n._in) {
3019 3020
        e._item.setSecond(n);
3020 3021
      } else {
3021 3022
        e._item.setFirst();
3022 3023
        _digraph->firstIn(e._item.first(), n);
3023 3024
      }
3024 3025
    }
3025 3026

	
3026 3027
    void nextIn(Arc& e) const {
3027 3028
      if (!e._item.firstState()) {
3028 3029
        e._item.setFirst(INVALID);
3029 3030
      } else {
3030 3031
        _digraph->nextIn(e._item.first());
3031 3032
      }
3032 3033
    }
3033 3034

	
3034 3035
    Node source(const Arc& e) const {
3035 3036
      if (e._item.firstState()) {
3036 3037
        return Node(_digraph->source(e._item.first()), false);
3037 3038
      } else {
3038 3039
        return Node(e._item.second(), true);
3039 3040
      }
3040 3041
    }
3041 3042

	
3042 3043
    Node target(const Arc& e) const {
3043 3044
      if (e._item.firstState()) {
3044 3045
        return Node(_digraph->target(e._item.first()), true);
3045 3046
      } else {
3046 3047
        return Node(e._item.second(), false);
3047 3048
      }
3048 3049
    }
3049 3050

	
3050 3051
    int id(const Node& n) const {
3051 3052
      return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
3052 3053
    }
3053 3054
    Node nodeFromId(int ix) const {
3054 3055
      return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
3055 3056
    }
3056 3057
    int maxNodeId() const {
3057 3058
      return 2 * _digraph->maxNodeId() + 1;
3058 3059
    }
3059 3060

	
3060 3061
    int id(const Arc& e) const {
3061 3062
      if (e._item.firstState()) {
3062 3063
        return _digraph->id(e._item.first()) << 1;
3063 3064
      } else {
3064 3065
        return (_digraph->id(e._item.second()) << 1) | 1;
3065 3066
      }
3066 3067
    }
3067 3068
    Arc arcFromId(int ix) const {
3068 3069
      if ((ix & 1) == 0) {
3069 3070
        return Arc(_digraph->arcFromId(ix >> 1));
3070 3071
      } else {
3071 3072
        return Arc(_digraph->nodeFromId(ix >> 1));
3072 3073
      }
3073 3074
    }
3074 3075
    int maxArcId() const {
3075 3076
      return std::max(_digraph->maxNodeId() << 1,
3076 3077
                      (_digraph->maxArcId() << 1) | 1);
3077 3078
    }
3078 3079

	
3079 3080
    static bool inNode(const Node& n) {
3080 3081
      return n._in;
3081 3082
    }
3082 3083

	
3083 3084
    static bool outNode(const Node& n) {
3084 3085
      return !n._in;
3085 3086
    }
3086 3087

	
3087 3088
    static bool origArc(const Arc& e) {
3088 3089
      return e._item.firstState();
3089 3090
    }
3090 3091

	
3091 3092
    static bool bindArc(const Arc& e) {
3092 3093
      return e._item.secondState();
3093 3094
    }
3094 3095

	
3095 3096
    static Node inNode(const DigraphNode& n) {
3096 3097
      return Node(n, true);
3097 3098
    }
3098 3099

	
3099 3100
    static Node outNode(const DigraphNode& n) {
3100 3101
      return Node(n, false);
3101 3102
    }
3102 3103

	
3103 3104
    static Arc arc(const DigraphNode& n) {
3104 3105
      return Arc(n);
3105 3106
    }
3106 3107

	
3107 3108
    static Arc arc(const DigraphArc& e) {
3108 3109
      return Arc(e);
3109 3110
    }
3110 3111

	
3111 3112
    typedef True NodeNumTag;
3112 3113
    int nodeNum() const {
3113 3114
      return  2 * countNodes(*_digraph);
3114 3115
    }
3115 3116

	
3116 3117
    typedef True ArcNumTag;
3117 3118
    int arcNum() const {
3118 3119
      return countArcs(*_digraph) + countNodes(*_digraph);
3119 3120
    }
3120 3121

	
3121 3122
    typedef True FindArcTag;
3122 3123
    Arc findArc(const Node& u, const Node& v,
3123 3124
                const Arc& prev = INVALID) const {
3124 3125
      if (inNode(u) && outNode(v)) {
3125 3126
        if (static_cast<const DigraphNode&>(u) ==
3126 3127
            static_cast<const DigraphNode&>(v) && prev == INVALID) {
3127 3128
          return Arc(u);
3128 3129
        }
3129 3130
      }
3130 3131
      else if (outNode(u) && inNode(v)) {
3131 3132
        return Arc(::lemon::findArc(*_digraph, u, v, prev));
3132 3133
      }
3133 3134
      return INVALID;
3134 3135
    }
3135 3136

	
3136 3137
  private:
3137 3138

	
3138 3139
    template <typename V>
3139 3140
    class NodeMapBase
3140 3141
      : public MapTraits<typename Parent::template NodeMap<V> > {
3141 3142
      typedef typename Parent::template NodeMap<V> NodeImpl;
3142 3143
    public:
3143 3144
      typedef Node Key;
3144 3145
      typedef V Value;
3145 3146
      typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
3146 3147
      typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
3147 3148
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
3148 3149
      typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
3149 3150
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
3150 3151

	
3151 3152
      NodeMapBase(const SplitNodesBase<DGR>& adaptor)
3152 3153
        : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
3153 3154
      NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3154 3155
        : _in_map(*adaptor._digraph, value),
3155 3156
          _out_map(*adaptor._digraph, value) {}
3156 3157

	
3157 3158
      void set(const Node& key, const V& val) {
3158 3159
        if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); }
3159 3160
        else {_out_map.set(key, val); }
3160 3161
      }
3161 3162

	
3162 3163
      ReturnValue operator[](const Node& key) {
3163 3164
        if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; }
3164 3165
        else { return _out_map[key]; }
3165 3166
      }
3166 3167

	
3167 3168
      ConstReturnValue operator[](const Node& key) const {
3168 3169
        if (Adaptor::inNode(key)) { return _in_map[key]; }
3169 3170
        else { return _out_map[key]; }
3170 3171
      }
3171 3172

	
3172 3173
    private:
3173 3174
      NodeImpl _in_map, _out_map;
3174 3175
    };
3175 3176

	
3176 3177
    template <typename V>
3177 3178
    class ArcMapBase
3178 3179
      : public MapTraits<typename Parent::template ArcMap<V> > {
3179 3180
      typedef typename Parent::template ArcMap<V> ArcImpl;
3180 3181
      typedef typename Parent::template NodeMap<V> NodeImpl;
3181 3182
    public:
3182 3183
      typedef Arc Key;
3183 3184
      typedef V Value;
3184 3185
      typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
3185 3186
      typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
3186 3187
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
3187 3188
      typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
3188 3189
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
3189 3190

	
3190 3191
      ArcMapBase(const SplitNodesBase<DGR>& adaptor)
3191 3192
        : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
3192 3193
      ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3193 3194
        : _arc_map(*adaptor._digraph, value),
3194 3195
          _node_map(*adaptor._digraph, value) {}
3195 3196

	
3196 3197
      void set(const Arc& key, const V& val) {
3197 3198
        if (SplitNodesBase<DGR>::origArc(key)) {
3198 3199
          _arc_map.set(static_cast<const DigraphArc&>(key), val);
3199 3200
        } else {
3200 3201
          _node_map.set(static_cast<const DigraphNode&>(key), val);
3201 3202
        }
3202 3203
      }
3203 3204

	
3204 3205
      ReturnValue operator[](const Arc& key) {
3205 3206
        if (SplitNodesBase<DGR>::origArc(key)) {
3206 3207
          return _arc_map[static_cast<const DigraphArc&>(key)];
3207 3208
        } else {
3208 3209
          return _node_map[static_cast<const DigraphNode&>(key)];
3209 3210
        }
3210 3211
      }
3211 3212

	
3212 3213
      ConstReturnValue operator[](const Arc& key) const {
3213 3214
        if (SplitNodesBase<DGR>::origArc(key)) {
3214 3215
          return _arc_map[static_cast<const DigraphArc&>(key)];
3215 3216
        } else {
3216 3217
          return _node_map[static_cast<const DigraphNode&>(key)];
3217 3218
        }
3218 3219
      }
3219 3220

	
3220 3221
    private:
3221 3222
      ArcImpl _arc_map;
3222 3223
      NodeImpl _node_map;
3223 3224
    };
3224 3225

	
3225 3226
  public:
3226 3227

	
3227 3228
    template <typename V>
3228 3229
    class NodeMap
3229 3230
      : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> >
3230 3231
    {
3231 3232
    public:
3232 3233
      typedef V Value;
3233 3234
      typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<Value> > Parent;
3234 3235

	
3235 3236
      NodeMap(const SplitNodesBase<DGR>& adaptor)
3236 3237
        : Parent(adaptor) {}
3237 3238

	
3238 3239
      NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3239 3240
        : Parent(adaptor, value) {}
3240 3241

	
3241 3242
    private:
3242 3243
      NodeMap& operator=(const NodeMap& cmap) {
3243 3244
        return operator=<NodeMap>(cmap);
3244 3245
      }
3245 3246

	
3246 3247
      template <typename CMap>
3247 3248
      NodeMap& operator=(const CMap& cmap) {
3248 3249
        Parent::operator=(cmap);
3249 3250
        return *this;
3250 3251
      }
3251 3252
    };
3252 3253

	
3253 3254
    template <typename V>
3254 3255
    class ArcMap
3255 3256
      : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> >
3256 3257
    {
3257 3258
    public:
3258 3259
      typedef V Value;
3259 3260
      typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<Value> > Parent;
3260 3261

	
3261 3262
      ArcMap(const SplitNodesBase<DGR>& adaptor)
3262 3263
        : Parent(adaptor) {}
3263 3264

	
3264 3265
      ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3265 3266
        : Parent(adaptor, value) {}
3266 3267

	
3267 3268
    private:
3268 3269
      ArcMap& operator=(const ArcMap& cmap) {
3269 3270
        return operator=<ArcMap>(cmap);
3270 3271
      }
3271 3272

	
3272 3273
      template <typename CMap>
3273 3274
      ArcMap& operator=(const CMap& cmap) {
3274 3275
        Parent::operator=(cmap);
3275 3276
        return *this;
3276 3277
      }
3277 3278
    };
3278 3279

	
3279 3280
  protected:
3280 3281

	
3281 3282
    SplitNodesBase() : _digraph(0) {}
3282 3283

	
3283 3284
    DGR* _digraph;
3284 3285

	
3285 3286
    void initialize(Digraph& digraph) {
3286 3287
      _digraph = &digraph;
3287 3288
    }
3288 3289

	
3289 3290
  };
3290 3291

	
3291 3292
  /// \ingroup graph_adaptors
3292 3293
  ///
3293 3294
  /// \brief Adaptor class for splitting the nodes of a digraph.
3294 3295
  ///
3295 3296
  /// SplitNodes adaptor can be used for splitting each node into an
3296 3297
  /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor
3297 3298
  /// replaces each node \f$ u \f$ in the digraph with two nodes,
3298 3299
  /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$.
3299 3300
  /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the
3300 3301
  /// new target of the arc will be \f$ u_{in} \f$ and similarly the
3301 3302
  /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$.
3302 3303
  /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$
3303 3304
  /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph.
3304 3305
  ///
3305 3306
  /// The aim of this class is running an algorithm with respect to node
3306 3307
  /// costs or capacities if the algorithm considers only arc costs or
3307 3308
  /// capacities directly.
3308 3309
  /// In this case you can use \c SplitNodes adaptor, and set the node
3309 3310
  /// costs/capacities of the original digraph to the \e bind \e arcs
3310 3311
  /// in the adaptor.
3311 3312
  ///
3312 3313
  /// \tparam DGR The type of the adapted digraph.
3313 3314
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
3314 3315
  /// It is implicitly \c const.
3315 3316
  ///
3316 3317
  /// \note The \c Node type of this adaptor is converible to the \c Node
3317 3318
  /// type of the adapted digraph.
3318 3319
  template <typename DGR>
3319 3320
#ifdef DOXYGEN
3320 3321
  class SplitNodes {
3321 3322
#else
3322 3323
  class SplitNodes
3323 3324
    : public DigraphAdaptorExtender<SplitNodesBase<const DGR> > {
3324 3325
#endif
3325 3326
  public:
3326 3327
    typedef DGR Digraph;
3327 3328
    typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent;
3328 3329

	
3329 3330
    typedef typename DGR::Node DigraphNode;
3330 3331
    typedef typename DGR::Arc DigraphArc;
3331 3332

	
3332 3333
    typedef typename Parent::Node Node;
3333 3334
    typedef typename Parent::Arc Arc;
3334 3335

	
3335 3336
    /// \brief Constructor
3336 3337
    ///
3337 3338
    /// Constructor of the adaptor.
3338 3339
    SplitNodes(const DGR& g) {
3339 3340
      Parent::initialize(g);
3340 3341
    }
3341 3342

	
3342 3343
    /// \brief Returns \c true if the given node is an in-node.
3343 3344
    ///
3344 3345
    /// Returns \c true if the given node is an in-node.
3345 3346
    static bool inNode(const Node& n) {
3346 3347
      return Parent::inNode(n);
3347 3348
    }
3348 3349

	
3349 3350
    /// \brief Returns \c true if the given node is an out-node.
3350 3351
    ///
3351 3352
    /// Returns \c true if the given node is an out-node.
3352 3353
    static bool outNode(const Node& n) {
3353 3354
      return Parent::outNode(n);
3354 3355
    }
3355 3356

	
3356 3357
    /// \brief Returns \c true if the given arc is an original arc.
3357 3358
    ///
3358 3359
    /// Returns \c true if the given arc is one of the arcs in the
3359 3360
    /// original digraph.
3360 3361
    static bool origArc(const Arc& a) {
3361 3362
      return Parent::origArc(a);
3362 3363
    }
3363 3364

	
3364 3365
    /// \brief Returns \c true if the given arc is a bind arc.
3365 3366
    ///
3366 3367
    /// Returns \c true if the given arc is a bind arc, i.e. it connects
3367 3368
    /// an in-node and an out-node.
3368 3369
    static bool bindArc(const Arc& a) {
3369 3370
      return Parent::bindArc(a);
3370 3371
    }
3371 3372

	
3372 3373
    /// \brief Returns the in-node created from the given original node.
3373 3374
    ///
3374 3375
    /// Returns the in-node created from the given original node.
3375 3376
    static Node inNode(const DigraphNode& n) {
3376 3377
      return Parent::inNode(n);
3377 3378
    }
3378 3379

	
3379 3380
    /// \brief Returns the out-node created from the given original node.
3380 3381
    ///
3381 3382
    /// Returns the out-node created from the given original node.
3382 3383
    static Node outNode(const DigraphNode& n) {
3383 3384
      return Parent::outNode(n);
3384 3385
    }
3385 3386

	
3386 3387
    /// \brief Returns the bind arc that corresponds to the given
3387 3388
    /// original node.
3388 3389
    ///
3389 3390
    /// Returns the bind arc in the adaptor that corresponds to the given
3390 3391
    /// original node, i.e. the arc connecting the in-node and out-node
3391 3392
    /// of \c n.
3392 3393
    static Arc arc(const DigraphNode& n) {
3393 3394
      return Parent::arc(n);
3394 3395
    }
3395 3396

	
3396 3397
    /// \brief Returns the arc that corresponds to the given original arc.
3397 3398
    ///
3398 3399
    /// Returns the arc in the adaptor that corresponds to the given
3399 3400
    /// original arc.
3400 3401
    static Arc arc(const DigraphArc& a) {
3401 3402
      return Parent::arc(a);
3402 3403
    }
3403 3404

	
3404 3405
    /// \brief Node map combined from two original node maps
3405 3406
    ///
3406 3407
    /// This map adaptor class adapts two node maps of the original digraph
3407 3408
    /// to get a node map of the split digraph.
3408 3409
    /// Its value type is inherited from the first node map type
3409 3410
    /// (\c InNodeMap).
3410 3411
    template <typename InNodeMap, typename OutNodeMap>
3411 3412
    class CombinedNodeMap {
3412 3413
    public:
3413 3414

	
3414 3415
      /// The key type of the map
3415 3416
      typedef Node Key;
3416 3417
      /// The value type of the map
3417 3418
      typedef typename InNodeMap::Value Value;
3418 3419

	
3419 3420
      typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag;
3420 3421
      typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue;
3421 3422
      typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue;
3422 3423
      typedef typename MapTraits<InNodeMap>::ReturnValue Reference;
3423 3424
      typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference;
3424 3425

	
3425 3426
      /// Constructor
3426 3427
      CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3427 3428
        : _in_map(in_map), _out_map(out_map) {}
3428 3429

	
3429 3430
      /// Returns the value associated with the given key.
3430 3431
      Value operator[](const Key& key) const {
3431 3432
        if (SplitNodesBase<const DGR>::inNode(key)) {
3432 3433
          return _in_map[key];
3433 3434
        } else {
3434 3435
          return _out_map[key];
3435 3436
        }
3436 3437
      }
3437 3438

	
3438 3439
      /// Returns a reference to the value associated with the given key.
3439 3440
      Value& operator[](const Key& key) {
3440 3441
        if (SplitNodesBase<const DGR>::inNode(key)) {
3441 3442
          return _in_map[key];
3442 3443
        } else {
3443 3444
          return _out_map[key];
3444 3445
        }
3445 3446
      }
3446 3447

	
3447 3448
      /// Sets the value associated with the given key.
3448 3449
      void set(const Key& key, const Value& value) {
3449 3450
        if (SplitNodesBase<const DGR>::inNode(key)) {
3450 3451
          _in_map.set(key, value);
3451 3452
        } else {
3452 3453
          _out_map.set(key, value);
3453 3454
        }
3454 3455
      }
3455 3456

	
3456 3457
    private:
3457 3458

	
3458 3459
      InNodeMap& _in_map;
3459 3460
      OutNodeMap& _out_map;
3460 3461

	
3461 3462
    };
3462 3463

	
3463 3464

	
3464 3465
    /// \brief Returns a combined node map
3465 3466
    ///
3466 3467
    /// This function just returns a combined node map.
3467 3468
    template <typename InNodeMap, typename OutNodeMap>
3468 3469
    static CombinedNodeMap<InNodeMap, OutNodeMap>
3469 3470
    combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
3470 3471
      return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
3471 3472
    }
3472 3473

	
3473 3474
    template <typename InNodeMap, typename OutNodeMap>
3474 3475
    static CombinedNodeMap<const InNodeMap, OutNodeMap>
3475 3476
    combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
3476 3477
      return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
3477 3478
    }
3478 3479

	
3479 3480
    template <typename InNodeMap, typename OutNodeMap>
3480 3481
    static CombinedNodeMap<InNodeMap, const OutNodeMap>
3481 3482
    combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
3482 3483
      return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
3483 3484
    }
3484 3485

	
3485 3486
    template <typename InNodeMap, typename OutNodeMap>
3486 3487
    static CombinedNodeMap<const InNodeMap, const OutNodeMap>
3487 3488
    combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
3488 3489
      return CombinedNodeMap<const InNodeMap,
3489 3490
        const OutNodeMap>(in_map, out_map);
3490 3491
    }
3491 3492

	
3492 3493
    /// \brief Arc map combined from an arc map and a node map of the
3493 3494
    /// original digraph.
3494 3495
    ///
3495 3496
    /// This map adaptor class adapts an arc map and a node map of the
3496 3497
    /// original digraph to get an arc map of the split digraph.
3497 3498
    /// Its value type is inherited from the original arc map type
3498 3499
    /// (\c ArcMap).
3499 3500
    template <typename ArcMap, typename NodeMap>
3500 3501
    class CombinedArcMap {
3501 3502
    public:
3502 3503

	
3503 3504
      /// The key type of the map
3504 3505
      typedef Arc Key;
3505 3506
      /// The value type of the map
3506 3507
      typedef typename ArcMap::Value Value;
3507 3508

	
3508 3509
      typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag;
3509 3510
      typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue;
3510 3511
      typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue;
3511 3512
      typedef typename MapTraits<ArcMap>::ReturnValue Reference;
3512 3513
      typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReference;
3513 3514

	
3514 3515
      /// Constructor
3515 3516
      CombinedArcMap(ArcMap& arc_map, NodeMap& node_map)
3516 3517
        : _arc_map(arc_map), _node_map(node_map) {}
3517 3518

	
3518 3519
      /// Returns the value associated with the given key.
3519 3520
      Value operator[](const Key& arc) const {
3520 3521
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3521 3522
          return _arc_map[arc];
3522 3523
        } else {
3523 3524
          return _node_map[arc];
3524 3525
        }
3525 3526
      }
3526 3527

	
3527 3528
      /// Returns a reference to the value associated with the given key.
3528 3529
      Value& operator[](const Key& arc) {
3529 3530
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3530 3531
          return _arc_map[arc];
3531 3532
        } else {
3532 3533
          return _node_map[arc];
3533 3534
        }
3534 3535
      }
3535 3536

	
3536 3537
      /// Sets the value associated with the given key.
3537 3538
      void set(const Arc& arc, const Value& val) {
3538 3539
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3539 3540
          _arc_map.set(arc, val);
3540 3541
        } else {
3541 3542
          _node_map.set(arc, val);
3542 3543
        }
3543 3544
      }
3544 3545

	
3545 3546
    private:
3546 3547
      ArcMap& _arc_map;
3547 3548
      NodeMap& _node_map;
3548 3549
    };
3549 3550

	
3550 3551
    /// \brief Returns a combined arc map
3551 3552
    ///
3552 3553
    /// This function just returns a combined arc map.
3553 3554
    template <typename ArcMap, typename NodeMap>
3554 3555
    static CombinedArcMap<ArcMap, NodeMap>
3555 3556
    combinedArcMap(ArcMap& arc_map, NodeMap& node_map) {
3556 3557
      return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map);
3557 3558
    }
3558 3559

	
3559 3560
    template <typename ArcMap, typename NodeMap>
3560 3561
    static CombinedArcMap<const ArcMap, NodeMap>
3561 3562
    combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) {
3562 3563
      return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map);
3563 3564
    }
3564 3565

	
3565 3566
    template <typename ArcMap, typename NodeMap>
3566 3567
    static CombinedArcMap<ArcMap, const NodeMap>
3567 3568
    combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) {
3568 3569
      return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map);
3569 3570
    }
3570 3571

	
3571 3572
    template <typename ArcMap, typename NodeMap>
3572 3573
    static CombinedArcMap<const ArcMap, const NodeMap>
3573 3574
    combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) {
3574 3575
      return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map);
3575 3576
    }
3576 3577

	
3577 3578
  };
3578 3579

	
3579 3580
  /// \brief Returns a (read-only) SplitNodes adaptor
3580 3581
  ///
3581 3582
  /// This function just returns a (read-only) \ref SplitNodes adaptor.
3582 3583
  /// \ingroup graph_adaptors
3583 3584
  /// \relates SplitNodes
3584 3585
  template<typename DGR>
3585 3586
  SplitNodes<DGR>
3586 3587
  splitNodes(const DGR& digraph) {
3587 3588
    return SplitNodes<DGR>(digraph);
3588 3589
  }
3589 3590

	
3590 3591
#undef LEMON_SCOPE_FIX
3591 3592

	
3592 3593
} //namespace lemon
3593 3594

	
3594 3595
#endif //LEMON_ADAPTORS_H
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H
20 20
#define LEMON_BITS_EDGE_SET_EXTENDER_H
21 21

	
22
#include <lemon/core.h>
22 23
#include <lemon/error.h>
23 24
#include <lemon/bits/default_map.h>
25
#include <lemon/bits/map_extender.h>
24 26

	
25 27
///\ingroup digraphbits
26 28
///\file
27 29
///\brief Extenders for the arc set types
28 30
namespace lemon {
29 31

	
30 32
  /// \ingroup digraphbits
31 33
  ///
32 34
  /// \brief Extender for the ArcSets
33 35
  template <typename Base>
34 36
  class ArcSetExtender : public Base {
35 37
  public:
36 38

	
37 39
    typedef Base Parent;
38 40
    typedef ArcSetExtender Digraph;
39 41

	
40 42
    // Base extensions
41 43

	
42 44
    typedef typename Parent::Node Node;
43 45
    typedef typename Parent::Arc Arc;
44 46

	
45 47
    int maxId(Node) const {
46 48
      return Parent::maxNodeId();
47 49
    }
48 50

	
49 51
    int maxId(Arc) const {
50 52
      return Parent::maxArcId();
51 53
    }
52 54

	
53 55
    Node fromId(int id, Node) const {
54 56
      return Parent::nodeFromId(id);
55 57
    }
56 58

	
57 59
    Arc fromId(int id, Arc) const {
58 60
      return Parent::arcFromId(id);
59 61
    }
60 62

	
61 63
    Node oppositeNode(const Node &n, const Arc &e) const {
62 64
      if (n == Parent::source(e))
63 65
	return Parent::target(e);
64 66
      else if(n==Parent::target(e))
65 67
	return Parent::source(e);
66 68
      else
67 69
	return INVALID;
68 70
    }
69 71

	
70 72

	
71 73
    // Alteration notifier extensions
72 74

	
73 75
    /// The arc observer registry.
74 76
    typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier;
75 77

	
76 78
  protected:
77 79

	
78 80
    mutable ArcNotifier arc_notifier;
79 81

	
80 82
  public:
81 83

	
82 84
    using Parent::notifier;
83 85

	
84 86
    /// \brief Gives back the arc alteration notifier.
85 87
    ///
86 88
    /// Gives back the arc alteration notifier.
87 89
    ArcNotifier& notifier(Arc) const {
88 90
      return arc_notifier;
89 91
    }
90 92

	
91 93
    // Iterable extensions
92 94

	
93 95
    class NodeIt : public Node { 
94 96
      const Digraph* digraph;
95 97
    public:
96 98

	
97 99
      NodeIt() {}
98 100

	
99 101
      NodeIt(Invalid i) : Node(i) { }
100 102

	
101 103
      explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
102 104
	_graph.first(static_cast<Node&>(*this));
103 105
      }
104 106

	
105 107
      NodeIt(const Digraph& _graph, const Node& node) 
106 108
	: Node(node), digraph(&_graph) {}
107 109

	
108 110
      NodeIt& operator++() { 
109 111
	digraph->next(*this);
110 112
	return *this; 
111 113
      }
112 114

	
113 115
    };
114 116

	
115 117

	
116 118
    class ArcIt : public Arc { 
117 119
      const Digraph* digraph;
118 120
    public:
119 121

	
120 122
      ArcIt() { }
121 123

	
122 124
      ArcIt(Invalid i) : Arc(i) { }
123 125

	
124 126
      explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
125 127
	_graph.first(static_cast<Arc&>(*this));
126 128
      }
127 129

	
128 130
      ArcIt(const Digraph& _graph, const Arc& e) : 
129 131
	Arc(e), digraph(&_graph) { }
130 132

	
131 133
      ArcIt& operator++() { 
132 134
	digraph->next(*this);
133 135
	return *this; 
134 136
      }
135 137

	
136 138
    };
137 139

	
138 140

	
139 141
    class OutArcIt : public Arc { 
140 142
      const Digraph* digraph;
141 143
    public:
142 144

	
143 145
      OutArcIt() { }
144 146

	
145 147
      OutArcIt(Invalid i) : Arc(i) { }
146 148

	
147 149
      OutArcIt(const Digraph& _graph, const Node& node) 
148 150
	: digraph(&_graph) {
149 151
	_graph.firstOut(*this, node);
150 152
      }
151 153

	
152 154
      OutArcIt(const Digraph& _graph, const Arc& arc) 
153 155
	: Arc(arc), digraph(&_graph) {}
154 156

	
155 157
      OutArcIt& operator++() { 
156 158
	digraph->nextOut(*this);
157 159
	return *this; 
158 160
      }
159 161

	
160 162
    };
161 163

	
162 164

	
163 165
    class InArcIt : public Arc { 
164 166
      const Digraph* digraph;
165 167
    public:
166 168

	
167 169
      InArcIt() { }
168 170

	
169 171
      InArcIt(Invalid i) : Arc(i) { }
170 172

	
171 173
      InArcIt(const Digraph& _graph, const Node& node) 
172 174
	: digraph(&_graph) {
173 175
	_graph.firstIn(*this, node);
174 176
      }
175 177

	
176 178
      InArcIt(const Digraph& _graph, const Arc& arc) : 
177 179
	Arc(arc), digraph(&_graph) {}
178 180

	
179 181
      InArcIt& operator++() { 
180 182
	digraph->nextIn(*this);
181 183
	return *this; 
182 184
      }
183 185

	
184 186
    };
185 187

	
186 188
    /// \brief Base node of the iterator
187 189
    ///
188 190
    /// Returns the base node (ie. the source in this case) of the iterator
189 191
    Node baseNode(const OutArcIt &e) const {
190 192
      return Parent::source(static_cast<const Arc&>(e));
191 193
    }
192 194
    /// \brief Running node of the iterator
193 195
    ///
194 196
    /// Returns the running node (ie. the target in this case) of the
195 197
    /// iterator
196 198
    Node runningNode(const OutArcIt &e) const {
197 199
      return Parent::target(static_cast<const Arc&>(e));
198 200
    }
199 201

	
200 202
    /// \brief Base node of the iterator
201 203
    ///
202 204
    /// Returns the base node (ie. the target in this case) of the iterator
203 205
    Node baseNode(const InArcIt &e) const {
204 206
      return Parent::target(static_cast<const Arc&>(e));
205 207
    }
206 208
    /// \brief Running node of the iterator
207 209
    ///
208 210
    /// Returns the running node (ie. the source in this case) of the
209 211
    /// iterator
210 212
    Node runningNode(const InArcIt &e) const {
211 213
      return Parent::source(static_cast<const Arc&>(e));
212 214
    }
213 215

	
214 216
    using Parent::first;
215 217

	
216 218
    // Mappable extension
217 219
    
218 220
    template <typename _Value>
219 221
    class ArcMap 
220 222
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
221 223
    public:
222 224
      typedef ArcSetExtender Digraph;
223 225
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
224 226

	
225 227
      explicit ArcMap(const Digraph& _g) 
226 228
	: Parent(_g) {}
227 229
      ArcMap(const Digraph& _g, const _Value& _v) 
228 230
	: Parent(_g, _v) {}
229 231

	
230 232
      ArcMap& operator=(const ArcMap& cmap) {
231 233
	return operator=<ArcMap>(cmap);
232 234
      }
233 235

	
234 236
      template <typename CMap>
235 237
      ArcMap& operator=(const CMap& cmap) {
236 238
        Parent::operator=(cmap);
237 239
	return *this;
238 240
      }
239 241

	
240 242
    };
241 243

	
242 244

	
243 245
    // Alteration extension
244 246

	
245 247
    Arc addArc(const Node& from, const Node& to) {
246 248
      Arc arc = Parent::addArc(from, to);
247 249
      notifier(Arc()).add(arc);
248 250
      return arc;
249 251
    }
250 252
    
251 253
    void clear() {
252 254
      notifier(Arc()).clear();
253 255
      Parent::clear();
254 256
    }
255 257

	
256 258
    void erase(const Arc& arc) {
257 259
      notifier(Arc()).erase(arc);
258 260
      Parent::erase(arc);
259 261
    }
260 262

	
261 263
    ArcSetExtender() {
262 264
      arc_notifier.setContainer(*this);
263 265
    }
264 266

	
265 267
    ~ArcSetExtender() {
266 268
      arc_notifier.clear();
267 269
    }
268 270

	
269 271
  };
270 272

	
271 273

	
272 274
  /// \ingroup digraphbits
273 275
  ///
274 276
  /// \brief Extender for the EdgeSets
275 277
  template <typename Base>
276 278
  class EdgeSetExtender : public Base {
277 279

	
278 280
  public:
279 281

	
280 282
    typedef Base Parent;
281 283
    typedef EdgeSetExtender Digraph;
282 284

	
283 285
    typedef typename Parent::Node Node;
284 286
    typedef typename Parent::Arc Arc;
285 287
    typedef typename Parent::Edge Edge;
286 288

	
287 289

	
288 290
    int maxId(Node) const {
289 291
      return Parent::maxNodeId();
290 292
    }
291 293

	
292 294
    int maxId(Arc) const {
293 295
      return Parent::maxArcId();
294 296
    }
295 297

	
296 298
    int maxId(Edge) const {
297 299
      return Parent::maxEdgeId();
298 300
    }
299 301

	
300 302
    Node fromId(int id, Node) const {
301 303
      return Parent::nodeFromId(id);
302 304
    }
303 305

	
304 306
    Arc fromId(int id, Arc) const {
305 307
      return Parent::arcFromId(id);
306 308
    }
307 309

	
308 310
    Edge fromId(int id, Edge) const {
309 311
      return Parent::edgeFromId(id);
310 312
    }
311 313

	
312 314
    Node oppositeNode(const Node &n, const Edge &e) const {
313 315
      if( n == Parent::u(e))
314 316
	return Parent::v(e);
315 317
      else if( n == Parent::v(e))
316 318
	return Parent::u(e);
317 319
      else
318 320
	return INVALID;
319 321
    }
320 322

	
321 323
    Arc oppositeArc(const Arc &e) const {
322 324
      return Parent::direct(e, !Parent::direction(e));
323 325
    }
324 326

	
325 327
    using Parent::direct;
326 328
    Arc direct(const Edge &e, const Node &s) const {
327 329
      return Parent::direct(e, Parent::u(e) == s);
328 330
    }
329 331

	
330 332
    typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
331 333
    typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
332 334

	
333 335

	
334 336
  protected:
335 337

	
336 338
    mutable ArcNotifier arc_notifier;
337 339
    mutable EdgeNotifier edge_notifier;
338 340

	
339 341
  public:
340 342

	
341 343
    using Parent::notifier;
342 344
    
343 345
    ArcNotifier& notifier(Arc) const {
344 346
      return arc_notifier;
345 347
    }
346 348

	
347 349
    EdgeNotifier& notifier(Edge) const {
348 350
      return edge_notifier;
349 351
    }
350 352

	
351 353

	
352 354
    class NodeIt : public Node { 
353 355
      const Digraph* digraph;
354 356
    public:
355 357

	
356 358
      NodeIt() {}
357 359

	
358 360
      NodeIt(Invalid i) : Node(i) { }
359 361

	
360 362
      explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
361 363
	_graph.first(static_cast<Node&>(*this));
362 364
      }
363 365

	
364 366
      NodeIt(const Digraph& _graph, const Node& node) 
365 367
	: Node(node), digraph(&_graph) {}
366 368

	
367 369
      NodeIt& operator++() { 
368 370
	digraph->next(*this);
369 371
	return *this; 
370 372
      }
371 373

	
372 374
    };
373 375

	
374 376

	
375 377
    class ArcIt : public Arc { 
376 378
      const Digraph* digraph;
377 379
    public:
378 380

	
379 381
      ArcIt() { }
380 382

	
381 383
      ArcIt(Invalid i) : Arc(i) { }
382 384

	
383 385
      explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
384 386
	_graph.first(static_cast<Arc&>(*this));
385 387
      }
386 388

	
387 389
      ArcIt(const Digraph& _graph, const Arc& e) : 
388 390
	Arc(e), digraph(&_graph) { }
389 391

	
390 392
      ArcIt& operator++() { 
391 393
	digraph->next(*this);
392 394
	return *this; 
393 395
      }
394 396

	
395 397
    };
396 398

	
397 399

	
398 400
    class OutArcIt : public Arc { 
399 401
      const Digraph* digraph;
400 402
    public:
401 403

	
402 404
      OutArcIt() { }
403 405

	
404 406
      OutArcIt(Invalid i) : Arc(i) { }
405 407

	
406 408
      OutArcIt(const Digraph& _graph, const Node& node) 
407 409
	: digraph(&_graph) {
408 410
	_graph.firstOut(*this, node);
409 411
      }
410 412

	
411 413
      OutArcIt(const Digraph& _graph, const Arc& arc) 
412 414
	: Arc(arc), digraph(&_graph) {}
413 415

	
414 416
      OutArcIt& operator++() { 
415 417
	digraph->nextOut(*this);
416 418
	return *this; 
417 419
      }
418 420

	
419 421
    };
420 422

	
421 423

	
422 424
    class InArcIt : public Arc { 
423 425
      const Digraph* digraph;
424 426
    public:
425 427

	
426 428
      InArcIt() { }
427 429

	
428 430
      InArcIt(Invalid i) : Arc(i) { }
429 431

	
430 432
      InArcIt(const Digraph& _graph, const Node& node) 
431 433
	: digraph(&_graph) {
432 434
	_graph.firstIn(*this, node);
433 435
      }
434 436

	
435 437
      InArcIt(const Digraph& _graph, const Arc& arc) : 
436 438
	Arc(arc), digraph(&_graph) {}
437 439

	
438 440
      InArcIt& operator++() { 
439 441
	digraph->nextIn(*this);
440 442
	return *this; 
441 443
      }
442 444

	
443 445
    };
444 446

	
445 447

	
446 448
    class EdgeIt : public Parent::Edge { 
447 449
      const Digraph* digraph;
448 450
    public:
449 451

	
450 452
      EdgeIt() { }
451 453

	
452 454
      EdgeIt(Invalid i) : Edge(i) { }
453 455

	
454 456
      explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) {
455 457
	_graph.first(static_cast<Edge&>(*this));
456 458
      }
457 459

	
458 460
      EdgeIt(const Digraph& _graph, const Edge& e) : 
459 461
	Edge(e), digraph(&_graph) { }
460 462

	
461 463
      EdgeIt& operator++() { 
462 464
	digraph->next(*this);
463 465
	return *this; 
464 466
      }
465 467

	
466 468
    };
467 469

	
468 470
    class IncEdgeIt : public Parent::Edge {
469 471
      friend class EdgeSetExtender;
470 472
      const Digraph* digraph;
471 473
      bool direction;
472 474
    public:
473 475

	
474 476
      IncEdgeIt() { }
475 477

	
476 478
      IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
477 479

	
478 480
      IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) {
479 481
	_graph.firstInc(*this, direction, n);
480 482
      }
481 483

	
482 484
      IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n)
483 485
	: digraph(&_graph), Edge(ue) {
484 486
	direction = (_graph.source(ue) == n);
485 487
      }
486 488

	
487 489
      IncEdgeIt& operator++() {
488 490
	digraph->nextInc(*this, direction);
489 491
	return *this; 
490 492
      }
491 493
    };
492 494

	
493 495
    /// \brief Base node of the iterator
494 496
    ///
495 497
    /// Returns the base node (ie. the source in this case) of the iterator
496 498
    Node baseNode(const OutArcIt &e) const {
497 499
      return Parent::source(static_cast<const Arc&>(e));
498 500
    }
499 501
    /// \brief Running node of the iterator
500 502
    ///
501 503
    /// Returns the running node (ie. the target in this case) of the
502 504
    /// iterator
503 505
    Node runningNode(const OutArcIt &e) const {
504 506
      return Parent::target(static_cast<const Arc&>(e));
505 507
    }
506 508

	
507 509
    /// \brief Base node of the iterator
508 510
    ///
509 511
    /// Returns the base node (ie. the target in this case) of the iterator
510 512
    Node baseNode(const InArcIt &e) const {
511 513
      return Parent::target(static_cast<const Arc&>(e));
512 514
    }
513 515
    /// \brief Running node of the iterator
514 516
    ///
515 517
    /// Returns the running node (ie. the source in this case) of the
516 518
    /// iterator
517 519
    Node runningNode(const InArcIt &e) const {
518 520
      return Parent::source(static_cast<const Arc&>(e));
519 521
    }
520 522

	
521 523
    /// Base node of the iterator
522 524
    ///
523 525
    /// Returns the base node of the iterator
524 526
    Node baseNode(const IncEdgeIt &e) const {
525 527
      return e.direction ? u(e) : v(e);
526 528
    }
527 529
    /// Running node of the iterator
528 530
    ///
529 531
    /// Returns the running node of the iterator
530 532
    Node runningNode(const IncEdgeIt &e) const {
531 533
      return e.direction ? v(e) : u(e);
532 534
    }
533 535

	
534 536

	
535 537
    template <typename _Value>
536 538
    class ArcMap 
537 539
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
538 540
    public:
539 541
      typedef EdgeSetExtender Digraph;
540 542
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
541 543

	
542 544
      ArcMap(const Digraph& _g) 
543 545
	: Parent(_g) {}
544 546
      ArcMap(const Digraph& _g, const _Value& _v) 
545 547
	: Parent(_g, _v) {}
546 548

	
547 549
      ArcMap& operator=(const ArcMap& cmap) {
548 550
	return operator=<ArcMap>(cmap);
549 551
      }
550 552

	
551 553
      template <typename CMap>
552 554
      ArcMap& operator=(const CMap& cmap) {
553 555
        Parent::operator=(cmap);
554 556
	return *this;
555 557
      }
556 558

	
557 559
    };
558 560

	
559 561

	
560 562
    template <typename _Value>
561 563
    class EdgeMap 
562 564
      : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
563 565
    public:
564 566
      typedef EdgeSetExtender Digraph;
565 567
      typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
566 568

	
567 569
      EdgeMap(const Digraph& _g) 
568 570
	: Parent(_g) {}
569 571

	
570 572
      EdgeMap(const Digraph& _g, const _Value& _v) 
571 573
	: Parent(_g, _v) {}
572 574

	
573 575
      EdgeMap& operator=(const EdgeMap& cmap) {
574 576
	return operator=<EdgeMap>(cmap);
575 577
      }
576 578

	
577 579
      template <typename CMap>
578 580
      EdgeMap& operator=(const CMap& cmap) {
579 581
        Parent::operator=(cmap);
580 582
	return *this;
581 583
      }
582 584

	
583 585
    };
584 586

	
585 587

	
586 588
    // Alteration extension
587 589

	
588 590
    Edge addEdge(const Node& from, const Node& to) {
589 591
      Edge edge = Parent::addEdge(from, to);
590 592
      notifier(Edge()).add(edge);
591 593
      std::vector<Arc> arcs;
592 594
      arcs.push_back(Parent::direct(edge, true));
593 595
      arcs.push_back(Parent::direct(edge, false));
594 596
      notifier(Arc()).add(arcs);
595 597
      return edge;
596 598
    }
597 599
    
598 600
    void clear() {
599 601
      notifier(Arc()).clear();
600 602
      notifier(Edge()).clear();
601 603
      Parent::clear();
602 604
    }
603 605

	
604 606
    void erase(const Edge& edge) {
605 607
      std::vector<Arc> arcs;
606 608
      arcs.push_back(Parent::direct(edge, true));
607 609
      arcs.push_back(Parent::direct(edge, false));
608 610
      notifier(Arc()).erase(arcs);
609 611
      notifier(Edge()).erase(edge);
610 612
      Parent::erase(edge);
611 613
    }
612 614

	
613 615

	
614 616
    EdgeSetExtender() {
615 617
      arc_notifier.setContainer(*this);
616 618
      edge_notifier.setContainer(*this);
617 619
    }
618 620

	
619 621
    ~EdgeSetExtender() {
620 622
      edge_notifier.clear();
621 623
      arc_notifier.clear();
622 624
    }
623 625
    
624 626
  };
625 627

	
626 628
}
627 629

	
628 630
#endif
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_PRED_MAP_PATH_H
20 20
#define LEMON_BITS_PRED_MAP_PATH_H
21 21

	
22
#include <lemon/core.h>
23
#include <lemon/concept_check.h>
24

	
22 25
namespace lemon {
23 26

	
24 27
  template <typename _Digraph, typename _PredMap>
25 28
  class PredMapPath {
26 29
  public:
27 30
    typedef True RevPathTag;
28 31

	
29 32
    typedef _Digraph Digraph;
30 33
    typedef typename Digraph::Arc Arc;
31 34
    typedef _PredMap PredMap;
32 35

	
33 36
    PredMapPath(const Digraph& _digraph, const PredMap& _predMap,
34 37
                typename Digraph::Node _target)
35 38
      : digraph(_digraph), predMap(_predMap), target(_target) {}
36 39

	
37 40
    int length() const {
38 41
      int len = 0;
39 42
      typename Digraph::Node node = target;
40 43
      typename Digraph::Arc arc;
41 44
      while ((arc = predMap[node]) != INVALID) {
42 45
        node = digraph.source(arc);
43 46
        ++len;
44 47
      }
45 48
      return len;
46 49
    }
47 50

	
48 51
    bool empty() const {
49 52
      return predMap[target] != INVALID;
50 53
    }
51 54

	
52 55
    class RevArcIt {
53 56
    public:
54 57
      RevArcIt() {}
55 58
      RevArcIt(Invalid) : path(0), current(INVALID) {}
56 59
      RevArcIt(const PredMapPath& _path)
57 60
        : path(&_path), current(_path.target) {
58 61
        if (path->predMap[current] == INVALID) current = INVALID;
59 62
      }
60 63

	
61 64
      operator const typename Digraph::Arc() const {
62 65
        return path->predMap[current];
63 66
      }
64 67

	
65 68
      RevArcIt& operator++() {
66 69
        current = path->digraph.source(path->predMap[current]);
67 70
        if (path->predMap[current] == INVALID) current = INVALID;
68 71
        return *this;
69 72
      }
70 73

	
71 74
      bool operator==(const RevArcIt& e) const {
72 75
        return current == e.current;
73 76
      }
74 77

	
75 78
      bool operator!=(const RevArcIt& e) const {
76 79
        return current != e.current;
77 80
      }
78 81

	
79 82
      bool operator<(const RevArcIt& e) const {
80 83
        return current < e.current;
81 84
      }
82 85

	
83 86
    private:
84 87
      const PredMapPath* path;
85 88
      typename Digraph::Node current;
86 89
    };
87 90

	
88 91
  private:
89 92
    const Digraph& digraph;
90 93
    const PredMap& predMap;
91 94
    typename Digraph::Node target;
92 95
  };
93 96

	
94 97

	
95 98
  template <typename _Digraph, typename _PredMatrixMap>
96 99
  class PredMatrixMapPath {
97 100
  public:
98 101
    typedef True RevPathTag;
99 102

	
100 103
    typedef _Digraph Digraph;
101 104
    typedef typename Digraph::Arc Arc;
102 105
    typedef _PredMatrixMap PredMatrixMap;
103 106

	
104 107
    PredMatrixMapPath(const Digraph& _digraph,
105 108
                      const PredMatrixMap& _predMatrixMap,
106 109
                      typename Digraph::Node _source,
107 110
                      typename Digraph::Node _target)
108 111
      : digraph(_digraph), predMatrixMap(_predMatrixMap),
109 112
        source(_source), target(_target) {}
110 113

	
111 114
    int length() const {
112 115
      int len = 0;
113 116
      typename Digraph::Node node = target;
114 117
      typename Digraph::Arc arc;
115 118
      while ((arc = predMatrixMap(source, node)) != INVALID) {
116 119
        node = digraph.source(arc);
117 120
        ++len;
118 121
      }
119 122
      return len;
120 123
    }
121 124

	
122 125
    bool empty() const {
123 126
      return source != target;
124 127
    }
125 128

	
126 129
    class RevArcIt {
127 130
    public:
128 131
      RevArcIt() {}
129 132
      RevArcIt(Invalid) : path(0), current(INVALID) {}
130 133
      RevArcIt(const PredMatrixMapPath& _path)
131 134
        : path(&_path), current(_path.target) {
132 135
        if (path->predMatrixMap(path->source, current) == INVALID)
133 136
          current = INVALID;
134 137
      }
135 138

	
136 139
      operator const typename Digraph::Arc() const {
137 140
        return path->predMatrixMap(path->source, current);
138 141
      }
139 142

	
140 143
      RevArcIt& operator++() {
141 144
        current =
142 145
          path->digraph.source(path->predMatrixMap(path->source, current));
143 146
        if (path->predMatrixMap(path->source, current) == INVALID)
144 147
          current = INVALID;
145 148
        return *this;
146 149
      }
147 150

	
148 151
      bool operator==(const RevArcIt& e) const {
149 152
        return current == e.current;
150 153
      }
151 154

	
152 155
      bool operator!=(const RevArcIt& e) const {
153 156
        return current != e.current;
154 157
      }
155 158

	
156 159
      bool operator<(const RevArcIt& e) const {
157 160
        return current < e.current;
158 161
      }
159 162

	
160 163
    private:
161 164
      const PredMatrixMapPath* path;
162 165
      typename Digraph::Node current;
163 166
    };
164 167

	
165 168
  private:
166 169
    const Digraph& digraph;
167 170
    const PredMatrixMap& predMatrixMap;
168 171
    typename Digraph::Node source;
169 172
    typename Digraph::Node target;
170 173
  };
171 174

	
172 175
}
173 176

	
174 177
#endif
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_SOLVER_BITS_H
20 20
#define LEMON_BITS_SOLVER_BITS_H
21 21

	
22
#include <vector>
23

	
22 24
namespace lemon {
23 25

	
24 26
  namespace _solver_bits {
25 27

	
26 28
    class VarIndex {
27 29
    private:
28 30
      struct ItemT {
29 31
        int prev, next;
30 32
        int index;
31 33
      };
32 34
      std::vector<ItemT> items;
33 35
      int first_item, last_item, first_free_item;
34 36

	
35 37
      std::vector<int> cross;
36 38

	
37 39
    public:
38 40

	
39 41
      VarIndex()
40 42
        : first_item(-1), last_item(-1), first_free_item(-1) {
41 43
      }
42 44

	
43 45
      void clear() {
44 46
        first_item = -1;
45 47
        first_free_item = -1;
46 48
        items.clear();
47 49
        cross.clear();
48 50
      }
49 51

	
50 52
      int addIndex(int idx) {
51 53
        int n;
52 54
        if (first_free_item == -1) {
53 55
          n = items.size();
54 56
          items.push_back(ItemT());
55 57
        } else {
56 58
          n = first_free_item;
57 59
          first_free_item = items[n].next;
58 60
          if (first_free_item != -1) {
59 61
            items[first_free_item].prev = -1;
60 62
          }
61 63
        }
62 64
        items[n].index = idx;
63 65
        if (static_cast<int>(cross.size()) <= idx) {
64 66
          cross.resize(idx + 1, -1);
65 67
        }
66 68
        cross[idx] = n;
67 69

	
68 70
        items[n].prev = last_item;
69 71
        items[n].next = -1;
70 72
        if (last_item != -1) {
71 73
          items[last_item].next = n;
72 74
        } else {
73 75
          first_item = n;
74 76
        }
75 77
        last_item = n;
76 78

	
77 79
        return n;
78 80
      }
79 81

	
80 82
      int addIndex(int idx, int n) {
81 83
        while (n >= static_cast<int>(items.size())) {
82 84
          items.push_back(ItemT());
83 85
          items.back().prev = -1;
84 86
          items.back().next = first_free_item;
85 87
          if (first_free_item != -1) {
86 88
            items[first_free_item].prev = items.size() - 1;
87 89
          }
88 90
          first_free_item = items.size() - 1;
89 91
        }
90 92
        if (items[n].next != -1) {
91 93
          items[items[n].next].prev = items[n].prev;
92 94
        }
93 95
        if (items[n].prev != -1) {
94 96
          items[items[n].prev].next = items[n].next;
95 97
        } else {
96 98
          first_free_item = items[n].next;
97 99
        }
98 100

	
99 101
        items[n].index = idx;
100 102
        if (static_cast<int>(cross.size()) <= idx) {
101 103
          cross.resize(idx + 1, -1);
102 104
        }
103 105
        cross[idx] = n;
104 106

	
105 107
        items[n].prev = last_item;
106 108
        items[n].next = -1;
107 109
        if (last_item != -1) {
108 110
          items[last_item].next = n;
109 111
        } else {
110 112
          first_item = n;
111 113
        }
112 114
        last_item = n;
113 115

	
114 116
        return n;
115 117
      }
116 118

	
117 119
      void eraseIndex(int idx) {
118 120
        int n = cross[idx];
119 121

	
120 122
        if (items[n].prev != -1) {
121 123
          items[items[n].prev].next = items[n].next;
122 124
        } else {
123 125
          first_item = items[n].next;
124 126
        }
125 127
        if (items[n].next != -1) {
126 128
          items[items[n].next].prev = items[n].prev;
127 129
        } else {
128 130
          last_item = items[n].prev;
129 131
        }
130 132

	
131 133
        if (first_free_item != -1) {
132 134
          items[first_free_item].prev = n;
133 135
        }
134 136
        items[n].next = first_free_item;
135 137
        items[n].prev = -1;
136 138
        first_free_item = n;
137 139

	
138 140
        while (!cross.empty() && cross.back() == -1) {
139 141
          cross.pop_back();
140 142
        }
141 143
      }
142 144

	
143 145
      int maxIndex() const {
144 146
        return cross.size() - 1;
145 147
      }
146 148

	
147 149
      void shiftIndices(int idx) {
148 150
        for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
149 151
          cross[i - 1] = cross[i];
150 152
          if (cross[i] != -1) {
151 153
            --items[cross[i]].index;
152 154
          }
153 155
        }
154 156
        cross.back() = -1;
155 157
        cross.pop_back();
156 158
        while (!cross.empty() && cross.back() == -1) {
157 159
          cross.pop_back();
158 160
        }
159 161
      }
160 162

	
161 163
      void relocateIndex(int idx, int jdx) {
162 164
        cross[idx] = cross[jdx];
163 165
        items[cross[jdx]].index = idx;
164 166
        cross[jdx] = -1;
165 167

	
166 168
        while (!cross.empty() && cross.back() == -1) {
167 169
          cross.pop_back();
168 170
        }
169 171
      }
170 172

	
171 173
      int operator[](int idx) const {
172 174
        return cross[idx];
173 175
      }
174 176

	
175 177
      int operator()(int fdx) const {
176 178
        return items[fdx].index;
177 179
      }
178 180

	
179 181
      void firstItem(int& fdx) const {
180 182
        fdx = first_item;
181 183
      }
182 184

	
183 185
      void nextItem(int& fdx) const {
184 186
        fdx = items[fdx].next;
185 187
      }
186 188

	
187 189
    };
188 190
  }
189 191
}
190 192

	
191 193
#endif
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup concept
20 20
///\file
21 21
///\brief The concept of heaps.
22 22

	
23 23
#ifndef LEMON_CONCEPT_HEAP_H
24 24
#define LEMON_CONCEPT_HEAP_H
25 25

	
26 26
#include <lemon/core.h>
27
#include <lemon/concept_check.h>
27 28

	
28 29
namespace lemon {
29 30

	
30 31
  namespace concepts {
31 32

	
32 33
    /// \addtogroup concept
33 34
    /// @{
34 35

	
35 36
    /// \brief The heap concept.
36 37
    ///
37 38
    /// Concept class describing the main interface of heaps.
38 39
    template <typename Priority, typename ItemIntMap>
39 40
    class Heap {
40 41
    public:
41 42

	
42 43
      /// Type of the items stored in the heap.
43 44
      typedef typename ItemIntMap::Key Item;
44 45

	
45 46
      /// Type of the priorities.
46 47
      typedef Priority Prio;
47 48

	
48 49
      /// \brief Type to represent the states of the items.
49 50
      ///
50 51
      /// Each item has a state associated to it. It can be "in heap",
51 52
      /// "pre heap" or "post heap". The later two are indifferent
52 53
      /// from the point of view of the heap, but may be useful for
53 54
      /// the user.
54 55
      ///
55 56
      /// The \c ItemIntMap must be initialized in such a way, that it
56 57
      /// assigns \c PRE_HEAP (<tt>-1</tt>) to every item.
57 58
      enum State {
58 59
        IN_HEAP = 0,
59 60
        PRE_HEAP = -1,
60 61
        POST_HEAP = -2
61 62
      };
62 63

	
63 64
      /// \brief The constructor.
64 65
      ///
65 66
      /// The constructor.
66 67
      /// \param map A map that assigns \c int values to keys of type
67 68
      /// \c Item. It is used internally by the heap implementations to
68 69
      /// handle the cross references. The assigned value must be
69 70
      /// \c PRE_HEAP (<tt>-1</tt>) for every item.
70 71
      explicit Heap(ItemIntMap &map) {}
71 72

	
72 73
      /// \brief The number of items stored in the heap.
73 74
      ///
74 75
      /// Returns the number of items stored in the heap.
75 76
      int size() const { return 0; }
76 77

	
77 78
      /// \brief Checks if the heap is empty.
78 79
      ///
79 80
      /// Returns \c true if the heap is empty.
80 81
      bool empty() const { return false; }
81 82

	
82 83
      /// \brief Makes the heap empty.
83 84
      ///
84 85
      /// Makes the heap empty.
85 86
      void clear();
86 87

	
87 88
      /// \brief Inserts an item into the heap with the given priority.
88 89
      ///
89 90
      /// Inserts the given item into the heap with the given priority.
90 91
      /// \param i The item to insert.
91 92
      /// \param p The priority of the item.
92 93
      void push(const Item &i, const Prio &p) {}
93 94

	
94 95
      /// \brief Returns the item having minimum priority.
95 96
      ///
96 97
      /// Returns the item having minimum priority.
97 98
      /// \pre The heap must be non-empty.
98 99
      Item top() const {}
99 100

	
100 101
      /// \brief The minimum priority.
101 102
      ///
102 103
      /// Returns the minimum priority.
103 104
      /// \pre The heap must be non-empty.
104 105
      Prio prio() const {}
105 106

	
106 107
      /// \brief Removes the item having minimum priority.
107 108
      ///
108 109
      /// Removes the item having minimum priority.
109 110
      /// \pre The heap must be non-empty.
110 111
      void pop() {}
111 112

	
112 113
      /// \brief Removes an item from the heap.
113 114
      ///
114 115
      /// Removes the given item from the heap if it is already stored.
115 116
      /// \param i The item to delete.
116 117
      void erase(const Item &i) {}
117 118

	
118 119
      /// \brief The priority of an item.
119 120
      ///
120 121
      /// Returns the priority of the given item.
121 122
      /// \pre \c i must be in the heap.
122 123
      /// \param i The item.
123 124
      Prio operator[](const Item &i) const {}
124 125

	
125 126
      /// \brief Sets the priority of an item or inserts it, if it is
126 127
      /// not stored in the heap.
127 128
      ///
128 129
      /// This method sets the priority of the given item if it is
129 130
      /// already stored in the heap.
130 131
      /// Otherwise it inserts the given item with the given priority.
131 132
      ///
132 133
      /// \param i The item.
133 134
      /// \param p The priority.
134 135
      void set(const Item &i, const Prio &p) {}
135 136

	
136 137
      /// \brief Decreases the priority of an item to the given value.
137 138
      ///
138 139
      /// Decreases the priority of an item to the given value.
139 140
      /// \pre \c i must be stored in the heap with priority at least \c p.
140 141
      /// \param i The item.
141 142
      /// \param p The priority.
142 143
      void decrease(const Item &i, const Prio &p) {}
143 144

	
144 145
      /// \brief Increases the priority of an item to the given value.
145 146
      ///
146 147
      /// Increases the priority of an item to the given value.
147 148
      /// \pre \c i must be stored in the heap with priority at most \c p.
148 149
      /// \param i The item.
149 150
      /// \param p The priority.
150 151
      void increase(const Item &i, const Prio &p) {}
151 152

	
152 153
      /// \brief Returns if an item is in, has already been in, or has
153 154
      /// never been in the heap.
154 155
      ///
155 156
      /// This method returns \c PRE_HEAP if the given item has never
156 157
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
157 158
      /// and \c POST_HEAP otherwise.
158 159
      /// In the latter case it is possible that the item will get back
159 160
      /// to the heap again.
160 161
      /// \param i The item.
161 162
      State state(const Item &i) const {}
162 163

	
163 164
      /// \brief Sets the state of an item in the heap.
164 165
      ///
165 166
      /// Sets the state of the given item in the heap. It can be used
166 167
      /// to manually clear the heap when it is important to achive the
167 168
      /// better time complexity.
168 169
      /// \param i The item.
169 170
      /// \param st The state. It should not be \c IN_HEAP.
170 171
      void state(const Item& i, State st) {}
171 172

	
172 173

	
173 174
      template <typename _Heap>
174 175
      struct Constraints {
175 176
      public:
176 177
        void constraints() {
177 178
          typedef typename _Heap::Item OwnItem;
178 179
          typedef typename _Heap::Prio OwnPrio;
179 180
          typedef typename _Heap::State OwnState;
180 181

	
181 182
          Item item;
182 183
          Prio prio;
183 184
          item=Item();
184 185
          prio=Prio();
185 186
          ignore_unused_variable_warning(item);
186 187
          ignore_unused_variable_warning(prio);
187 188

	
188 189
          OwnItem own_item;
189 190
          OwnPrio own_prio;
190 191
          OwnState own_state;
191 192
          own_item=Item();
192 193
          own_prio=Prio();
193 194
          ignore_unused_variable_warning(own_item);
194 195
          ignore_unused_variable_warning(own_prio);
195 196
          ignore_unused_variable_warning(own_state);
196 197

	
197 198
          _Heap heap1(map);
198 199
          _Heap heap2 = heap1;
199 200
          ignore_unused_variable_warning(heap1);
200 201
          ignore_unused_variable_warning(heap2);
201 202

	
202 203
          int s = heap.size();
203 204
          ignore_unused_variable_warning(s);
204 205
          bool e = heap.empty();
205 206
          ignore_unused_variable_warning(e);
206 207

	
207 208
          prio = heap.prio();
208 209
          item = heap.top();
209 210
          prio = heap[item];
210 211
          own_prio = heap.prio();
211 212
          own_item = heap.top();
212 213
          own_prio = heap[own_item];
213 214

	
214 215
          heap.push(item, prio);
215 216
          heap.push(own_item, own_prio);
216 217
          heap.pop();
217 218

	
218 219
          heap.set(item, prio);
219 220
          heap.decrease(item, prio);
220 221
          heap.increase(item, prio);
221 222
          heap.set(own_item, own_prio);
222 223
          heap.decrease(own_item, own_prio);
223 224
          heap.increase(own_item, own_prio);
224 225

	
225 226
          heap.erase(item);
226 227
          heap.erase(own_item);
227 228
          heap.clear();
228 229

	
229 230
          own_state = heap.state(own_item);
230 231
          heap.state(own_item, own_state);
231 232

	
232 233
          own_state = _Heap::PRE_HEAP;
233 234
          own_state = _Heap::IN_HEAP;
234 235
          own_state = _Heap::POST_HEAP;
235 236
        }
236 237

	
237 238
        _Heap& heap;
238 239
        ItemIntMap& map;
239 240
      };
240 241
    };
241 242

	
242 243
    /// @}
243 244
  } // namespace lemon
244 245
}
245 246
#endif // LEMON_CONCEPT_PATH_H
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_ELEVATOR_H
20 20
#define LEMON_ELEVATOR_H
21 21

	
22 22
///\ingroup auxdat
23 23
///\file
24 24
///\brief Elevator class
25 25
///
26 26
///Elevator class implements an efficient data structure
27 27
///for labeling items in push-relabel type algorithms.
28 28
///
29 29

	
30
#include <lemon/core.h>
30 31
#include <lemon/bits/traits.h>
31 32

	
32 33
namespace lemon {
33 34

	
34 35
  ///Class for handling "labels" in push-relabel type algorithms.
35 36

	
36 37
  ///A class for handling "labels" in push-relabel type algorithms.
37 38
  ///
38 39
  ///\ingroup auxdat
39 40
  ///Using this class you can assign "labels" (nonnegative integer numbers)
40 41
  ///to the edges or nodes of a graph, manipulate and query them through
41 42
  ///operations typically arising in "push-relabel" type algorithms.
42 43
  ///
43 44
  ///Each item is either \em active or not, and you can also choose a
44 45
  ///highest level active item.
45 46
  ///
46 47
  ///\sa LinkedElevator
47 48
  ///
48 49
  ///\param Graph Type of the underlying graph.
49 50
  ///\param Item Type of the items the data is assigned to (Graph::Node,
50 51
  ///Graph::Arc, Graph::Edge).
51 52
  template<class Graph, class Item>
52 53
  class Elevator
53 54
  {
54 55
  public:
55 56

	
56 57
    typedef Item Key;
57 58
    typedef int Value;
58 59

	
59 60
  private:
60 61

	
61 62
    typedef Item *Vit;
62 63
    typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap;
63 64
    typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap;
64 65

	
65 66
    const Graph &_g;
66 67
    int _max_level;
67 68
    int _item_num;
68 69
    VitMap _where;
69 70
    IntMap _level;
70 71
    std::vector<Item> _items;
71 72
    std::vector<Vit> _first;
72 73
    std::vector<Vit> _last_active;
73 74

	
74 75
    int _highest_active;
75 76

	
76 77
    void copy(Item i, Vit p)
77 78
    {
78 79
      _where.set(*p=i,p);
79 80
    }
80 81
    void copy(Vit s, Vit p)
81 82
    {
82 83
      if(s!=p)
83 84
        {
84 85
          Item i=*s;
85 86
          *p=i;
86 87
          _where.set(i,p);
87 88
        }
88 89
    }
89 90
    void swap(Vit i, Vit j)
90 91
    {
91 92
      Item ti=*i;
92 93
      Vit ct = _where[ti];
93 94
      _where.set(ti,_where[*i=*j]);
94 95
      _where.set(*j,ct);
95 96
      *j=ti;
96 97
    }
97 98

	
98 99
  public:
99 100

	
100 101
    ///Constructor with given maximum level.
101 102

	
102 103
    ///Constructor with given maximum level.
103 104
    ///
104 105
    ///\param graph The underlying graph.
105 106
    ///\param max_level The maximum allowed level.
106 107
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>.
107 108
    Elevator(const Graph &graph,int max_level) :
108 109
      _g(graph),
109 110
      _max_level(max_level),
110 111
      _item_num(_max_level),
111 112
      _where(graph),
112 113
      _level(graph,0),
113 114
      _items(_max_level),
114 115
      _first(_max_level+2),
115 116
      _last_active(_max_level+2),
116 117
      _highest_active(-1) {}
117 118
    ///Constructor.
118 119

	
119 120
    ///Constructor.
120 121
    ///
121 122
    ///\param graph The underlying graph.
122 123
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>,
123 124
    ///where \c max_level is equal to the number of labeled items in the graph.
124 125
    Elevator(const Graph &graph) :
125 126
      _g(graph),
126 127
      _max_level(countItems<Graph, Item>(graph)),
127 128
      _item_num(_max_level),
128 129
      _where(graph),
129 130
      _level(graph,0),
130 131
      _items(_max_level),
131 132
      _first(_max_level+2),
132 133
      _last_active(_max_level+2),
133 134
      _highest_active(-1)
134 135
    {
135 136
    }
136 137

	
137 138
    ///Activate item \c i.
138 139

	
139 140
    ///Activate item \c i.
140 141
    ///\pre Item \c i shouldn't be active before.
141 142
    void activate(Item i)
142 143
    {
143 144
      const int l=_level[i];
144 145
      swap(_where[i],++_last_active[l]);
145 146
      if(l>_highest_active) _highest_active=l;
146 147
    }
147 148

	
148 149
    ///Deactivate item \c i.
149 150

	
150 151
    ///Deactivate item \c i.
151 152
    ///\pre Item \c i must be active before.
152 153
    void deactivate(Item i)
153 154
    {
154 155
      swap(_where[i],_last_active[_level[i]]--);
155 156
      while(_highest_active>=0 &&
156 157
            _last_active[_highest_active]<_first[_highest_active])
157 158
        _highest_active--;
158 159
    }
159 160

	
160 161
    ///Query whether item \c i is active
161 162
    bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
162 163

	
163 164
    ///Return the level of item \c i.
164 165
    int operator[](Item i) const { return _level[i]; }
165 166

	
166 167
    ///Return the number of items on level \c l.
167 168
    int onLevel(int l) const
168 169
    {
169 170
      return _first[l+1]-_first[l];
170 171
    }
171 172
    ///Return true if level \c l is empty.
172 173
    bool emptyLevel(int l) const
173 174
    {
174 175
      return _first[l+1]-_first[l]==0;
175 176
    }
176 177
    ///Return the number of items above level \c l.
177 178
    int aboveLevel(int l) const
178 179
    {
179 180
      return _first[_max_level+1]-_first[l+1];
180 181
    }
181 182
    ///Return the number of active items on level \c l.
182 183
    int activesOnLevel(int l) const
183 184
    {
184 185
      return _last_active[l]-_first[l]+1;
185 186
    }
186 187
    ///Return true if there is no active item on level \c l.
187 188
    bool activeFree(int l) const
188 189
    {
189 190
      return _last_active[l]<_first[l];
190 191
    }
191 192
    ///Return the maximum allowed level.
192 193
    int maxLevel() const
193 194
    {
194 195
      return _max_level;
195 196
    }
196 197

	
197 198
    ///\name Highest Active Item
198 199
    ///Functions for working with the highest level
199 200
    ///active item.
200 201

	
201 202
    ///@{
202 203

	
203 204
    ///Return a highest level active item.
204 205

	
205 206
    ///Return a highest level active item or INVALID if there is no active
206 207
    ///item.
207 208
    Item highestActive() const
208 209
    {
209 210
      return _highest_active>=0?*_last_active[_highest_active]:INVALID;
210 211
    }
211 212

	
212 213
    ///Return the highest active level.
213 214

	
214 215
    ///Return the level of the highest active item or -1 if there is no active
215 216
    ///item.
216 217
    int highestActiveLevel() const
217 218
    {
218 219
      return _highest_active;
219 220
    }
220 221

	
221 222
    ///Lift the highest active item by one.
222 223

	
223 224
    ///Lift the item returned by highestActive() by one.
224 225
    ///
225 226
    void liftHighestActive()
226 227
    {
227 228
      Item it = *_last_active[_highest_active];
228 229
      _level.set(it,_level[it]+1);
229 230
      swap(_last_active[_highest_active]--,_last_active[_highest_active+1]);
230 231
      --_first[++_highest_active];
231 232
    }
232 233

	
233 234
    ///Lift the highest active item to the given level.
234 235

	
235 236
    ///Lift the item returned by highestActive() to level \c new_level.
236 237
    ///
237 238
    ///\warning \c new_level must be strictly higher
238 239
    ///than the current level.
239 240
    ///
240 241
    void liftHighestActive(int new_level)
241 242
    {
242 243
      const Item li = *_last_active[_highest_active];
243 244

	
244 245
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
245 246
      for(int l=_highest_active+1;l<new_level;l++)
246 247
        {
247 248
          copy(--_first[l+1],_first[l]);
248 249
          --_last_active[l];
249 250
        }
250 251
      copy(li,_first[new_level]);
251 252
      _level.set(li,new_level);
252 253
      _highest_active=new_level;
253 254
    }
254 255

	
255 256
    ///Lift the highest active item to the top level.
256 257

	
257 258
    ///Lift the item returned by highestActive() to the top level and
258 259
    ///deactivate it.
259 260
    void liftHighestActiveToTop()
260 261
    {
261 262
      const Item li = *_last_active[_highest_active];
262 263

	
263 264
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
264 265
      for(int l=_highest_active+1;l<_max_level;l++)
265 266
        {
266 267
          copy(--_first[l+1],_first[l]);
267 268
          --_last_active[l];
268 269
        }
269 270
      copy(li,_first[_max_level]);
270 271
      --_last_active[_max_level];
271 272
      _level.set(li,_max_level);
272 273

	
273 274
      while(_highest_active>=0 &&
274 275
            _last_active[_highest_active]<_first[_highest_active])
275 276
        _highest_active--;
276 277
    }
277 278

	
278 279
    ///@}
279 280

	
280 281
    ///\name Active Item on Certain Level
281 282
    ///Functions for working with the active items.
282 283

	
283 284
    ///@{
284 285

	
285 286
    ///Return an active item on level \c l.
286 287

	
287 288
    ///Return an active item on level \c l or \ref INVALID if there is no such
288 289
    ///an item. (\c l must be from the range [0...\c max_level].
289 290
    Item activeOn(int l) const
290 291
    {
291 292
      return _last_active[l]>=_first[l]?*_last_active[l]:INVALID;
292 293
    }
293 294

	
294 295
    ///Lift the active item returned by \c activeOn(level) by one.
295 296

	
296 297
    ///Lift the active item returned by \ref activeOn() "activeOn(level)"
297 298
    ///by one.
298 299
    Item liftActiveOn(int level)
299 300
    {
300 301
      Item it =*_last_active[level];
301 302
      _level.set(it,_level[it]+1);
302 303
      swap(_last_active[level]--, --_first[level+1]);
303 304
      if (level+1>_highest_active) ++_highest_active;
304 305
    }
305 306

	
306 307
    ///Lift the active item returned by \c activeOn(level) to the given level.
307 308

	
308 309
    ///Lift the active item returned by \ref activeOn() "activeOn(level)"
309 310
    ///to the given level.
310 311
    void liftActiveOn(int level, int new_level)
311 312
    {
312 313
      const Item ai = *_last_active[level];
313 314

	
314 315
      copy(--_first[level+1], _last_active[level]--);
315 316
      for(int l=level+1;l<new_level;l++)
316 317
        {
317 318
          copy(_last_active[l],_first[l]);
318 319
          copy(--_first[l+1], _last_active[l]--);
319 320
        }
320 321
      copy(ai,_first[new_level]);
321 322
      _level.set(ai,new_level);
322 323
      if (new_level>_highest_active) _highest_active=new_level;
323 324
    }
324 325

	
325 326
    ///Lift the active item returned by \c activeOn(level) to the top level.
326 327

	
327 328
    ///Lift the active item returned by \ref activeOn() "activeOn(level)"
328 329
    ///to the top level and deactivate it.
329 330
    void liftActiveToTop(int level)
330 331
    {
331 332
      const Item ai = *_last_active[level];
332 333

	
333 334
      copy(--_first[level+1],_last_active[level]--);
334 335
      for(int l=level+1;l<_max_level;l++)
335 336
        {
336 337
          copy(_last_active[l],_first[l]);
337 338
          copy(--_first[l+1], _last_active[l]--);
338 339
        }
339 340
      copy(ai,_first[_max_level]);
340 341
      --_last_active[_max_level];
341 342
      _level.set(ai,_max_level);
342 343

	
343 344
      if (_highest_active==level) {
344 345
        while(_highest_active>=0 &&
345 346
              _last_active[_highest_active]<_first[_highest_active])
346 347
          _highest_active--;
347 348
      }
348 349
    }
349 350

	
350 351
    ///@}
351 352

	
352 353
    ///Lift an active item to a higher level.
353 354

	
354 355
    ///Lift an active item to a higher level.
355 356
    ///\param i The item to be lifted. It must be active.
356 357
    ///\param new_level The new level of \c i. It must be strictly higher
357 358
    ///than the current level.
358 359
    ///
359 360
    void lift(Item i, int new_level)
360 361
    {
361 362
      const int lo = _level[i];
362 363
      const Vit w = _where[i];
363 364

	
364 365
      copy(_last_active[lo],w);
365 366
      copy(--_first[lo+1],_last_active[lo]--);
366 367
      for(int l=lo+1;l<new_level;l++)
367 368
        {
368 369
          copy(_last_active[l],_first[l]);
369 370
          copy(--_first[l+1],_last_active[l]--);
370 371
        }
371 372
      copy(i,_first[new_level]);
372 373
      _level.set(i,new_level);
373 374
      if(new_level>_highest_active) _highest_active=new_level;
374 375
    }
375 376

	
376 377
    ///Move an inactive item to the top but one level (in a dirty way).
377 378

	
378 379
    ///This function moves an inactive item from the top level to the top
379 380
    ///but one level (in a dirty way).
380 381
    ///\warning It makes the underlying datastructure corrupt, so use it
381 382
    ///only if you really know what it is for.
382 383
    ///\pre The item is on the top level.
383 384
    void dirtyTopButOne(Item i) {
384 385
      _level.set(i,_max_level - 1);
385 386
    }
386 387

	
387 388
    ///Lift all items on and above the given level to the top level.
388 389

	
389 390
    ///This function lifts all items on and above level \c l to the top
390 391
    ///level and deactivates them.
391 392
    void liftToTop(int l)
392 393
    {
393 394
      const Vit f=_first[l];
394 395
      const Vit tl=_first[_max_level];
395 396
      for(Vit i=f;i!=tl;++i)
396 397
        _level.set(*i,_max_level);
397 398
      for(int i=l;i<=_max_level;i++)
398 399
        {
399 400
          _first[i]=f;
400 401
          _last_active[i]=f-1;
401 402
        }
402 403
      for(_highest_active=l-1;
403 404
          _highest_active>=0 &&
404 405
            _last_active[_highest_active]<_first[_highest_active];
405 406
          _highest_active--) ;
406 407
    }
407 408

	
408 409
  private:
409 410
    int _init_lev;
410 411
    Vit _init_num;
411 412

	
412 413
  public:
413 414

	
414 415
    ///\name Initialization
415 416
    ///Using these functions you can initialize the levels of the items.
416 417
    ///\n
417 418
    ///The initialization must be started with calling \c initStart().
418 419
    ///Then the items should be listed level by level starting with the
419 420
    ///lowest one (level 0) using \c initAddItem() and \c initNewLevel().
420 421
    ///Finally \c initFinish() must be called.
421 422
    ///The items not listed are put on the highest level.
422 423
    ///@{
423 424

	
424 425
    ///Start the initialization process.
425 426
    void initStart()
426 427
    {
427 428
      _init_lev=0;
428 429
      _init_num=&_items[0];
429 430
      _first[0]=&_items[0];
430 431
      _last_active[0]=&_items[0]-1;
431 432
      Vit n=&_items[0];
432 433
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
433 434
        {
434 435
          *n=i;
435 436
          _where.set(i,n);
436 437
          _level.set(i,_max_level);
437 438
          ++n;
438 439
        }
439 440
    }
440 441

	
441 442
    ///Add an item to the current level.
442 443
    void initAddItem(Item i)
443 444
    {
444 445
      swap(_where[i],_init_num);
445 446
      _level.set(i,_init_lev);
446 447
      ++_init_num;
447 448
    }
448 449

	
449 450
    ///Start a new level.
450 451

	
451 452
    ///Start a new level.
452 453
    ///It shouldn't be used before the items on level 0 are listed.
453 454
    void initNewLevel()
454 455
    {
455 456
      _init_lev++;
456 457
      _first[_init_lev]=_init_num;
457 458
      _last_active[_init_lev]=_init_num-1;
458 459
    }
459 460

	
460 461
    ///Finalize the initialization process.
461 462
    void initFinish()
462 463
    {
463 464
      for(_init_lev++;_init_lev<=_max_level;_init_lev++)
464 465
        {
465 466
          _first[_init_lev]=_init_num;
466 467
          _last_active[_init_lev]=_init_num-1;
467 468
        }
468 469
      _first[_max_level+1]=&_items[0]+_item_num;
469 470
      _last_active[_max_level+1]=&_items[0]+_item_num-1;
470 471
      _highest_active = -1;
471 472
    }
472 473

	
473 474
    ///@}
474 475

	
475 476
  };
476 477

	
477 478
  ///Class for handling "labels" in push-relabel type algorithms.
478 479

	
479 480
  ///A class for handling "labels" in push-relabel type algorithms.
480 481
  ///
481 482
  ///\ingroup auxdat
482 483
  ///Using this class you can assign "labels" (nonnegative integer numbers)
483 484
  ///to the edges or nodes of a graph, manipulate and query them through
484 485
  ///operations typically arising in "push-relabel" type algorithms.
485 486
  ///
486 487
  ///Each item is either \em active or not, and you can also choose a
487 488
  ///highest level active item.
488 489
  ///
489 490
  ///\sa Elevator
490 491
  ///
491 492
  ///\param Graph Type of the underlying graph.
492 493
  ///\param Item Type of the items the data is assigned to (Graph::Node,
493 494
  ///Graph::Arc, Graph::Edge).
494 495
  template <class Graph, class Item>
495 496
  class LinkedElevator {
496 497
  public:
497 498

	
498 499
    typedef Item Key;
499 500
    typedef int Value;
500 501

	
501 502
  private:
502 503

	
503 504
    typedef typename ItemSetTraits<Graph,Item>::
504 505
    template Map<Item>::Type ItemMap;
505 506
    typedef typename ItemSetTraits<Graph,Item>::
506 507
    template Map<int>::Type IntMap;
507 508
    typedef typename ItemSetTraits<Graph,Item>::
508 509
    template Map<bool>::Type BoolMap;
509 510

	
510 511
    const Graph &_graph;
511 512
    int _max_level;
512 513
    int _item_num;
513 514
    std::vector<Item> _first, _last;
514 515
    ItemMap _prev, _next;
515 516
    int _highest_active;
516 517
    IntMap _level;
517 518
    BoolMap _active;
518 519

	
519 520
  public:
520 521
    ///Constructor with given maximum level.
521 522

	
522 523
    ///Constructor with given maximum level.
523 524
    ///
524 525
    ///\param graph The underlying graph.
525 526
    ///\param max_level The maximum allowed level.
526 527
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>.
527 528
    LinkedElevator(const Graph& graph, int max_level)
528 529
      : _graph(graph), _max_level(max_level), _item_num(_max_level),
529 530
        _first(_max_level + 1), _last(_max_level + 1),
530 531
        _prev(graph), _next(graph),
531 532
        _highest_active(-1), _level(graph), _active(graph) {}
532 533

	
533 534
    ///Constructor.
534 535

	
535 536
    ///Constructor.
536 537
    ///
537 538
    ///\param graph The underlying graph.
538 539
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>,
539 540
    ///where \c max_level is equal to the number of labeled items in the graph.
540 541
    LinkedElevator(const Graph& graph)
541 542
      : _graph(graph), _max_level(countItems<Graph, Item>(graph)),
542 543
        _item_num(_max_level),
543 544
        _first(_max_level + 1), _last(_max_level + 1),
544 545
        _prev(graph, INVALID), _next(graph, INVALID),
545 546
        _highest_active(-1), _level(graph), _active(graph) {}
546 547

	
547 548

	
548 549
    ///Activate item \c i.
549 550

	
550 551
    ///Activate item \c i.
551 552
    ///\pre Item \c i shouldn't be active before.
552 553
    void activate(Item i) {
553 554
      _active.set(i, true);
554 555

	
555 556
      int level = _level[i];
556 557
      if (level > _highest_active) {
557 558
        _highest_active = level;
558 559
      }
559 560

	
560 561
      if (_prev[i] == INVALID || _active[_prev[i]]) return;
561 562
      //unlace
562 563
      _next.set(_prev[i], _next[i]);
563 564
      if (_next[i] != INVALID) {
564 565
        _prev.set(_next[i], _prev[i]);
565 566
      } else {
566 567
        _last[level] = _prev[i];
567 568
      }
568 569
      //lace
569 570
      _next.set(i, _first[level]);
570 571
      _prev.set(_first[level], i);
571 572
      _prev.set(i, INVALID);
572 573
      _first[level] = i;
573 574

	
574 575
    }
575 576

	
576 577
    ///Deactivate item \c i.
577 578

	
578 579
    ///Deactivate item \c i.
579 580
    ///\pre Item \c i must be active before.
580 581
    void deactivate(Item i) {
581 582
      _active.set(i, false);
582 583
      int level = _level[i];
583 584

	
584 585
      if (_next[i] == INVALID || !_active[_next[i]])
585 586
        goto find_highest_level;
586 587

	
587 588
      //unlace
588 589
      _prev.set(_next[i], _prev[i]);
589 590
      if (_prev[i] != INVALID) {
590 591
        _next.set(_prev[i], _next[i]);
591 592
      } else {
592 593
        _first[_level[i]] = _next[i];
593 594
      }
594 595
      //lace
595 596
      _prev.set(i, _last[level]);
596 597
      _next.set(_last[level], i);
597 598
      _next.set(i, INVALID);
598 599
      _last[level] = i;
599 600

	
600 601
    find_highest_level:
601 602
      if (level == _highest_active) {
602 603
        while (_highest_active >= 0 && activeFree(_highest_active))
603 604
          --_highest_active;
604 605
      }
605 606
    }
606 607

	
607 608
    ///Query whether item \c i is active
608 609
    bool active(Item i) const { return _active[i]; }
609 610

	
610 611
    ///Return the level of item \c i.
611 612
    int operator[](Item i) const { return _level[i]; }
612 613

	
613 614
    ///Return the number of items on level \c l.
614 615
    int onLevel(int l) const {
615 616
      int num = 0;
616 617
      Item n = _first[l];
617 618
      while (n != INVALID) {
618 619
        ++num;
619 620
        n = _next[n];
620 621
      }
621 622
      return num;
622 623
    }
623 624

	
624 625
    ///Return true if the level is empty.
625 626
    bool emptyLevel(int l) const {
626 627
      return _first[l] == INVALID;
627 628
    }
628 629

	
629 630
    ///Return the number of items above level \c l.
630 631
    int aboveLevel(int l) const {
631 632
      int num = 0;
632 633
      for (int level = l + 1; level < _max_level; ++level)
633 634
        num += onLevel(level);
634 635
      return num;
635 636
    }
636 637

	
637 638
    ///Return the number of active items on level \c l.
638 639
    int activesOnLevel(int l) const {
639 640
      int num = 0;
640 641
      Item n = _first[l];
641 642
      while (n != INVALID && _active[n]) {
642 643
        ++num;
643 644
        n = _next[n];
644 645
      }
645 646
      return num;
646 647
    }
647 648

	
648 649
    ///Return true if there is no active item on level \c l.
649 650
    bool activeFree(int l) const {
650 651
      return _first[l] == INVALID || !_active[_first[l]];
651 652
    }
652 653

	
653 654
    ///Return the maximum allowed level.
654 655
    int maxLevel() const {
655 656
      return _max_level;
656 657
    }
657 658

	
658 659
    ///\name Highest Active Item
659 660
    ///Functions for working with the highest level
660 661
    ///active item.
661 662

	
662 663
    ///@{
663 664

	
664 665
    ///Return a highest level active item.
665 666

	
666 667
    ///Return a highest level active item or INVALID if there is no active
667 668
    ///item.
668 669
    Item highestActive() const {
669 670
      return _highest_active >= 0 ? _first[_highest_active] : INVALID;
670 671
    }
671 672

	
672 673
    ///Return the highest active level.
673 674

	
674 675
    ///Return the level of the highest active item or -1 if there is no active
675 676
    ///item.
676 677
    int highestActiveLevel() const {
677 678
      return _highest_active;
678 679
    }
679 680

	
680 681
    ///Lift the highest active item by one.
681 682

	
682 683
    ///Lift the item returned by highestActive() by one.
683 684
    ///
684 685
    void liftHighestActive() {
685 686
      Item i = _first[_highest_active];
686 687
      if (_next[i] != INVALID) {
687 688
        _prev.set(_next[i], INVALID);
688 689
        _first[_highest_active] = _next[i];
689 690
      } else {
690 691
        _first[_highest_active] = INVALID;
691 692
        _last[_highest_active] = INVALID;
692 693
      }
693 694
      _level.set(i, ++_highest_active);
694 695
      if (_first[_highest_active] == INVALID) {
695 696
        _first[_highest_active] = i;
696 697
        _last[_highest_active] = i;
697 698
        _prev.set(i, INVALID);
698 699
        _next.set(i, INVALID);
699 700
      } else {
700 701
        _prev.set(_first[_highest_active], i);
701 702
        _next.set(i, _first[_highest_active]);
702 703
        _first[_highest_active] = i;
703 704
      }
704 705
    }
705 706

	
706 707
    ///Lift the highest active item to the given level.
707 708

	
708 709
    ///Lift the item returned by highestActive() to level \c new_level.
709 710
    ///
710 711
    ///\warning \c new_level must be strictly higher
711 712
    ///than the current level.
712 713
    ///
713 714
    void liftHighestActive(int new_level) {
714 715
      Item i = _first[_highest_active];
715 716
      if (_next[i] != INVALID) {
716 717
        _prev.set(_next[i], INVALID);
717 718
        _first[_highest_active] = _next[i];
718 719
      } else {
719 720
        _first[_highest_active] = INVALID;
720 721
        _last[_highest_active] = INVALID;
721 722
      }
722 723
      _level.set(i, _highest_active = new_level);
723 724
      if (_first[_highest_active] == INVALID) {
724 725
        _first[_highest_active] = _last[_highest_active] = i;
725 726
        _prev.set(i, INVALID);
726 727
        _next.set(i, INVALID);
727 728
      } else {
728 729
        _prev.set(_first[_highest_active], i);
729 730
        _next.set(i, _first[_highest_active]);
730 731
        _first[_highest_active] = i;
731 732
      }
732 733
    }
733 734

	
734 735
    ///Lift the highest active item to the top level.
735 736

	
736 737
    ///Lift the item returned by highestActive() to the top level and
737 738
    ///deactivate it.
738 739
    void liftHighestActiveToTop() {
739 740
      Item i = _first[_highest_active];
740 741
      _level.set(i, _max_level);
741 742
      if (_next[i] != INVALID) {
742 743
        _prev.set(_next[i], INVALID);
743 744
        _first[_highest_active] = _next[i];
744 745
      } else {
745 746
        _first[_highest_active] = INVALID;
746 747
        _last[_highest_active] = INVALID;
747 748
      }
748 749
      while (_highest_active >= 0 && activeFree(_highest_active))
749 750
        --_highest_active;
750 751
    }
751 752

	
752 753
    ///@}
753 754

	
754 755
    ///\name Active Item on Certain Level
755 756
    ///Functions for working with the active items.
756 757

	
757 758
    ///@{
758 759

	
759 760
    ///Return an active item on level \c l.
760 761

	
761 762
    ///Return an active item on level \c l or \ref INVALID if there is no such
762 763
    ///an item. (\c l must be from the range [0...\c max_level].
763 764
    Item activeOn(int l) const
764 765
    {
765 766
      return _active[_first[l]] ? _first[l] : INVALID;
766 767
    }
767 768

	
768 769
    ///Lift the active item returned by \c activeOn(l) by one.
769 770

	
770 771
    ///Lift the active item returned by \ref activeOn() "activeOn(l)"
771 772
    ///by one.
772 773
    Item liftActiveOn(int l)
773 774
    {
774 775
      Item i = _first[l];
775 776
      if (_next[i] != INVALID) {
776 777
        _prev.set(_next[i], INVALID);
777 778
        _first[l] = _next[i];
778 779
      } else {
779 780
        _first[l] = INVALID;
780 781
        _last[l] = INVALID;
781 782
      }
782 783
      _level.set(i, ++l);
783 784
      if (_first[l] == INVALID) {
784 785
        _first[l] = _last[l] = i;
785 786
        _prev.set(i, INVALID);
786 787
        _next.set(i, INVALID);
787 788
      } else {
788 789
        _prev.set(_first[l], i);
789 790
        _next.set(i, _first[l]);
790 791
        _first[l] = i;
791 792
      }
792 793
      if (_highest_active < l) {
793 794
        _highest_active = l;
794 795
      }
795 796
    }
796 797

	
797 798
    ///Lift the active item returned by \c activeOn(l) to the given level.
798 799

	
799 800
    ///Lift the active item returned by \ref activeOn() "activeOn(l)"
800 801
    ///to the given level.
801 802
    void liftActiveOn(int l, int new_level)
802 803
    {
803 804
      Item i = _first[l];
804 805
      if (_next[i] != INVALID) {
805 806
        _prev.set(_next[i], INVALID);
806 807
        _first[l] = _next[i];
807 808
      } else {
808 809
        _first[l] = INVALID;
809 810
        _last[l] = INVALID;
810 811
      }
811 812
      _level.set(i, l = new_level);
812 813
      if (_first[l] == INVALID) {
813 814
        _first[l] = _last[l] = i;
814 815
        _prev.set(i, INVALID);
815 816
        _next.set(i, INVALID);
816 817
      } else {
817 818
        _prev.set(_first[l], i);
818 819
        _next.set(i, _first[l]);
819 820
        _first[l] = i;
820 821
      }
821 822
      if (_highest_active < l) {
822 823
        _highest_active = l;
823 824
      }
824 825
    }
825 826

	
826 827
    ///Lift the active item returned by \c activeOn(l) to the top level.
827 828

	
828 829
    ///Lift the active item returned by \ref activeOn() "activeOn(l)"
829 830
    ///to the top level and deactivate it.
830 831
    void liftActiveToTop(int l)
831 832
    {
832 833
      Item i = _first[l];
833 834
      if (_next[i] != INVALID) {
834 835
        _prev.set(_next[i], INVALID);
835 836
        _first[l] = _next[i];
836 837
      } else {
837 838
        _first[l] = INVALID;
838 839
        _last[l] = INVALID;
839 840
      }
840 841
      _level.set(i, _max_level);
841 842
      if (l == _highest_active) {
842 843
        while (_highest_active >= 0 && activeFree(_highest_active))
843 844
          --_highest_active;
844 845
      }
845 846
    }
846 847

	
847 848
    ///@}
848 849

	
849 850
    /// \brief Lift an active item to a higher level.
850 851
    ///
851 852
    /// Lift an active item to a higher level.
852 853
    /// \param i The item to be lifted. It must be active.
853 854
    /// \param new_level The new level of \c i. It must be strictly higher
854 855
    /// than the current level.
855 856
    ///
856 857
    void lift(Item i, int new_level) {
857 858
      if (_next[i] != INVALID) {
858 859
        _prev.set(_next[i], _prev[i]);
859 860
      } else {
860 861
        _last[new_level] = _prev[i];
861 862
      }
862 863
      if (_prev[i] != INVALID) {
863 864
        _next.set(_prev[i], _next[i]);
864 865
      } else {
865 866
        _first[new_level] = _next[i];
866 867
      }
867 868
      _level.set(i, new_level);
868 869
      if (_first[new_level] == INVALID) {
869 870
        _first[new_level] = _last[new_level] = i;
870 871
        _prev.set(i, INVALID);
871 872
        _next.set(i, INVALID);
872 873
      } else {
873 874
        _prev.set(_first[new_level], i);
874 875
        _next.set(i, _first[new_level]);
875 876
        _first[new_level] = i;
876 877
      }
877 878
      if (_highest_active < new_level) {
878 879
        _highest_active = new_level;
879 880
      }
880 881
    }
881 882

	
882 883
    ///Move an inactive item to the top but one level (in a dirty way).
883 884

	
884 885
    ///This function moves an inactive item from the top level to the top
885 886
    ///but one level (in a dirty way).
886 887
    ///\warning It makes the underlying datastructure corrupt, so use it
887 888
    ///only if you really know what it is for.
888 889
    ///\pre The item is on the top level.
889 890
    void dirtyTopButOne(Item i) {
890 891
      _level.set(i, _max_level - 1);
891 892
    }
892 893

	
893 894
    ///Lift all items on and above the given level to the top level.
894 895

	
895 896
    ///This function lifts all items on and above level \c l to the top
896 897
    ///level and deactivates them.
897 898
    void liftToTop(int l)  {
898 899
      for (int i = l + 1; _first[i] != INVALID; ++i) {
899 900
        Item n = _first[i];
900 901
        while (n != INVALID) {
901 902
          _level.set(n, _max_level);
902 903
          n = _next[n];
903 904
        }
904 905
        _first[i] = INVALID;
905 906
        _last[i] = INVALID;
906 907
      }
907 908
      if (_highest_active > l - 1) {
908 909
        _highest_active = l - 1;
909 910
        while (_highest_active >= 0 && activeFree(_highest_active))
910 911
          --_highest_active;
911 912
      }
912 913
    }
913 914

	
914 915
  private:
915 916

	
916 917
    int _init_level;
917 918

	
918 919
  public:
919 920

	
920 921
    ///\name Initialization
921 922
    ///Using these functions you can initialize the levels of the items.
922 923
    ///\n
923 924
    ///The initialization must be started with calling \c initStart().
924 925
    ///Then the items should be listed level by level starting with the
925 926
    ///lowest one (level 0) using \c initAddItem() and \c initNewLevel().
926 927
    ///Finally \c initFinish() must be called.
927 928
    ///The items not listed are put on the highest level.
928 929
    ///@{
929 930

	
930 931
    ///Start the initialization process.
931 932
    void initStart() {
932 933

	
933 934
      for (int i = 0; i <= _max_level; ++i) {
934 935
        _first[i] = _last[i] = INVALID;
935 936
      }
936 937
      _init_level = 0;
937 938
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph);
938 939
          i != INVALID; ++i) {
939 940
        _level.set(i, _max_level);
940 941
        _active.set(i, false);
941 942
      }
942 943
    }
943 944

	
944 945
    ///Add an item to the current level.
945 946
    void initAddItem(Item i) {
946 947
      _level.set(i, _init_level);
947 948
      if (_last[_init_level] == INVALID) {
948 949
        _first[_init_level] = i;
949 950
        _last[_init_level] = i;
950 951
        _prev.set(i, INVALID);
951 952
        _next.set(i, INVALID);
952 953
      } else {
953 954
        _prev.set(i, _last[_init_level]);
954 955
        _next.set(i, INVALID);
955 956
        _next.set(_last[_init_level], i);
956 957
        _last[_init_level] = i;
957 958
      }
958 959
    }
959 960

	
960 961
    ///Start a new level.
961 962

	
962 963
    ///Start a new level.
963 964
    ///It shouldn't be used before the items on level 0 are listed.
964 965
    void initNewLevel() {
965 966
      ++_init_level;
966 967
    }
967 968

	
968 969
    ///Finalize the initialization process.
969 970
    void initFinish() {
970 971
      _highest_active = -1;
971 972
    }
972 973

	
973 974
    ///@}
974 975

	
975 976
  };
976 977

	
977 978

	
978 979
} //END OF NAMESPACE LEMON
979 980

	
980 981
#endif
981 982

	
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_SUURBALLE_H
20 20
#define LEMON_SUURBALLE_H
21 21

	
22 22
///\ingroup shortest_path
23 23
///\file
24 24
///\brief An algorithm for finding arc-disjoint paths between two
25 25
/// nodes having minimum total length.
26 26

	
27 27
#include <vector>
28 28
#include <lemon/bin_heap.h>
29 29
#include <lemon/path.h>
30
#include <lemon/list_graph.h>
31
#include <lemon/maps.h>
30 32

	
31 33
namespace lemon {
32 34

	
33 35
  /// \addtogroup shortest_path
34 36
  /// @{
35 37

	
36 38
  /// \brief Algorithm for finding arc-disjoint paths between two nodes
37 39
  /// having minimum total length.
38 40
  ///
39 41
  /// \ref lemon::Suurballe "Suurballe" implements an algorithm for
40 42
  /// finding arc-disjoint paths having minimum total length (cost)
41 43
  /// from a given source node to a given target node in a digraph.
42 44
  ///
43 45
  /// In fact, this implementation is the specialization of the
44 46
  /// \ref CapacityScaling "successive shortest path" algorithm.
45 47
  ///
46 48
  /// \tparam Digraph The digraph type the algorithm runs on.
47 49
  /// The default value is \c ListDigraph.
48 50
  /// \tparam LengthMap The type of the length (cost) map.
49 51
  /// The default value is <tt>Digraph::ArcMap<int></tt>.
50 52
  ///
51 53
  /// \warning Length values should be \e non-negative \e integers.
52 54
  ///
53 55
  /// \note For finding node-disjoint paths this algorithm can be used
54 56
  /// with \ref SplitNodes.
55 57
#ifdef DOXYGEN
56 58
  template <typename Digraph, typename LengthMap>
57 59
#else
58 60
  template < typename Digraph = ListDigraph,
59 61
             typename LengthMap = typename Digraph::template ArcMap<int> >
60 62
#endif
61 63
  class Suurballe
62 64
  {
63 65
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
64 66

	
65 67
    typedef typename LengthMap::Value Length;
66 68
    typedef ConstMap<Arc, int> ConstArcMap;
67 69
    typedef typename Digraph::template NodeMap<Arc> PredMap;
68 70

	
69 71
  public:
70 72

	
71 73
    /// The type of the flow map.
72 74
    typedef typename Digraph::template ArcMap<int> FlowMap;
73 75
    /// The type of the potential map.
74 76
    typedef typename Digraph::template NodeMap<Length> PotentialMap;
75 77
    /// The type of the path structures.
76 78
    typedef SimplePath<Digraph> Path;
77 79

	
78 80
  private:
79 81

	
80 82
    /// \brief Special implementation of the Dijkstra algorithm
81 83
    /// for finding shortest paths in the residual network.
82 84
    ///
83 85
    /// \ref ResidualDijkstra is a special implementation of the
84 86
    /// \ref Dijkstra algorithm for finding shortest paths in the
85 87
    /// residual network of the digraph with respect to the reduced arc
86 88
    /// lengths and modifying the node potentials according to the
87 89
    /// distance of the nodes.
88 90
    class ResidualDijkstra
89 91
    {
90 92
      typedef typename Digraph::template NodeMap<int> HeapCrossRef;
91 93
      typedef BinHeap<Length, HeapCrossRef> Heap;
92 94

	
93 95
    private:
94 96

	
95 97
      // The digraph the algorithm runs on
96 98
      const Digraph &_graph;
97 99

	
98 100
      // The main maps
99 101
      const FlowMap &_flow;
100 102
      const LengthMap &_length;
101 103
      PotentialMap &_potential;
102 104

	
103 105
      // The distance map
104 106
      PotentialMap _dist;
105 107
      // The pred arc map
106 108
      PredMap &_pred;
107 109
      // The processed (i.e. permanently labeled) nodes
108 110
      std::vector<Node> _proc_nodes;
109 111

	
110 112
      Node _s;
111 113
      Node _t;
112 114

	
113 115
    public:
114 116

	
115 117
      /// Constructor.
116 118
      ResidualDijkstra( const Digraph &digraph,
117 119
                        const FlowMap &flow,
118 120
                        const LengthMap &length,
119 121
                        PotentialMap &potential,
120 122
                        PredMap &pred,
121 123
                        Node s, Node t ) :
122 124
        _graph(digraph), _flow(flow), _length(length), _potential(potential),
123 125
        _dist(digraph), _pred(pred), _s(s), _t(t) {}
124 126

	
125 127
      /// \brief Run the algorithm. It returns \c true if a path is found
126 128
      /// from the source node to the target node.
127 129
      bool run() {
128 130
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
129 131
        Heap heap(heap_cross_ref);
130 132
        heap.push(_s, 0);
131 133
        _pred[_s] = INVALID;
132 134
        _proc_nodes.clear();
133 135

	
134 136
        // Process nodes
135 137
        while (!heap.empty() && heap.top() != _t) {
136 138
          Node u = heap.top(), v;
137 139
          Length d = heap.prio() + _potential[u], nd;
138 140
          _dist[u] = heap.prio();
139 141
          heap.pop();
140 142
          _proc_nodes.push_back(u);
141 143

	
142 144
          // Traverse outgoing arcs
143 145
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
144 146
            if (_flow[e] == 0) {
145 147
              v = _graph.target(e);
146 148
              switch(heap.state(v)) {
147 149
              case Heap::PRE_HEAP:
148 150
                heap.push(v, d + _length[e] - _potential[v]);
149 151
                _pred[v] = e;
150 152
                break;
151 153
              case Heap::IN_HEAP:
152 154
                nd = d + _length[e] - _potential[v];
153 155
                if (nd < heap[v]) {
154 156
                  heap.decrease(v, nd);
155 157
                  _pred[v] = e;
156 158
                }
157 159
                break;
158 160
              case Heap::POST_HEAP:
159 161
                break;
160 162
              }
161 163
            }
162 164
          }
163 165

	
164 166
          // Traverse incoming arcs
165 167
          for (InArcIt e(_graph, u); e != INVALID; ++e) {
166 168
            if (_flow[e] == 1) {
167 169
              v = _graph.source(e);
168 170
              switch(heap.state(v)) {
169 171
              case Heap::PRE_HEAP:
170 172
                heap.push(v, d - _length[e] - _potential[v]);
171 173
                _pred[v] = e;
172 174
                break;
173 175
              case Heap::IN_HEAP:
174 176
                nd = d - _length[e] - _potential[v];
175 177
                if (nd < heap[v]) {
176 178
                  heap.decrease(v, nd);
177 179
                  _pred[v] = e;
178 180
                }
179 181
                break;
180 182
              case Heap::POST_HEAP:
181 183
                break;
182 184
              }
183 185
            }
184 186
          }
185 187
        }
186 188
        if (heap.empty()) return false;
187 189

	
188 190
        // Update potentials of processed nodes
189 191
        Length t_dist = heap.prio();
190 192
        for (int i = 0; i < int(_proc_nodes.size()); ++i)
191 193
          _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
192 194
        return true;
193 195
      }
194 196

	
195 197
    }; //class ResidualDijkstra
196 198

	
197 199
  private:
198 200

	
199 201
    // The digraph the algorithm runs on
200 202
    const Digraph &_graph;
201 203
    // The length map
202 204
    const LengthMap &_length;
203 205

	
204 206
    // Arc map of the current flow
205 207
    FlowMap *_flow;
206 208
    bool _local_flow;
207 209
    // Node map of the current potentials
208 210
    PotentialMap *_potential;
209 211
    bool _local_potential;
210 212

	
211 213
    // The source node
212 214
    Node _source;
213 215
    // The target node
214 216
    Node _target;
215 217

	
216 218
    // Container to store the found paths
217 219
    std::vector< SimplePath<Digraph> > paths;
218 220
    int _path_num;
219 221

	
220 222
    // The pred arc map
221 223
    PredMap _pred;
222 224
    // Implementation of the Dijkstra algorithm for finding augmenting
223 225
    // shortest paths in the residual network
224 226
    ResidualDijkstra *_dijkstra;
225 227

	
226 228
  public:
227 229

	
228 230
    /// \brief Constructor.
229 231
    ///
230 232
    /// Constructor.
231 233
    ///
232 234
    /// \param digraph The digraph the algorithm runs on.
233 235
    /// \param length The length (cost) values of the arcs.
234 236
    /// \param s The source node.
235 237
    /// \param t The target node.
236 238
    Suurballe( const Digraph &digraph,
237 239
               const LengthMap &length,
238 240
               Node s, Node t ) :
239 241
      _graph(digraph), _length(length), _flow(0), _local_flow(false),
240 242
      _potential(0), _local_potential(false), _source(s), _target(t),
241 243
      _pred(digraph) {}
242 244

	
243 245
    /// Destructor.
244 246
    ~Suurballe() {
245 247
      if (_local_flow) delete _flow;
246 248
      if (_local_potential) delete _potential;
247 249
      delete _dijkstra;
248 250
    }
249 251

	
250 252
    /// \brief Set the flow map.
251 253
    ///
252 254
    /// This function sets the flow map.
253 255
    ///
254 256
    /// The found flow contains only 0 and 1 values. It is the union of
255 257
    /// the found arc-disjoint paths.
256 258
    ///
257 259
    /// \return \c (*this)
258 260
    Suurballe& flowMap(FlowMap &map) {
259 261
      if (_local_flow) {
260 262
        delete _flow;
261 263
        _local_flow = false;
262 264
      }
263 265
      _flow = &map;
264 266
      return *this;
265 267
    }
266 268

	
267 269
    /// \brief Set the potential map.
268 270
    ///
269 271
    /// This function sets the potential map.
270 272
    ///
271 273
    /// The potentials provide the dual solution of the underlying
272 274
    /// minimum cost flow problem.
273 275
    ///
274 276
    /// \return \c (*this)
275 277
    Suurballe& potentialMap(PotentialMap &map) {
276 278
      if (_local_potential) {
277 279
        delete _potential;
278 280
        _local_potential = false;
279 281
      }
280 282
      _potential = &map;
281 283
      return *this;
282 284
    }
283 285

	
284 286
    /// \name Execution control
285 287
    /// The simplest way to execute the algorithm is to call the run()
286 288
    /// function.
287 289
    /// \n
288 290
    /// If you only need the flow that is the union of the found
289 291
    /// arc-disjoint paths, you may call init() and findFlow().
290 292

	
291 293
    /// @{
292 294

	
293 295
    /// \brief Run the algorithm.
294 296
    ///
295 297
    /// This function runs the algorithm.
296 298
    ///
297 299
    /// \param k The number of paths to be found.
298 300
    ///
299 301
    /// \return \c k if there are at least \c k arc-disjoint paths from
300 302
    /// \c s to \c t in the digraph. Otherwise it returns the number of
301 303
    /// arc-disjoint paths found.
302 304
    ///
303 305
    /// \note Apart from the return value, <tt>s.run(k)</tt> is just a
304 306
    /// shortcut of the following code.
305 307
    /// \code
306 308
    ///   s.init();
307 309
    ///   s.findFlow(k);
308 310
    ///   s.findPaths();
309 311
    /// \endcode
310 312
    int run(int k = 2) {
311 313
      init();
312 314
      findFlow(k);
313 315
      findPaths();
314 316
      return _path_num;
315 317
    }
316 318

	
317 319
    /// \brief Initialize the algorithm.
318 320
    ///
319 321
    /// This function initializes the algorithm.
320 322
    void init() {
321 323
      // Initialize maps
322 324
      if (!_flow) {
323 325
        _flow = new FlowMap(_graph);
324 326
        _local_flow = true;
325 327
      }
326 328
      if (!_potential) {
327 329
        _potential = new PotentialMap(_graph);
328 330
        _local_potential = true;
329 331
      }
330 332
      for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
331 333
      for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
332 334

	
333 335
      _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
334 336
                                        *_potential, _pred,
335 337
                                        _source, _target );
336 338
    }
337 339

	
338 340
    /// \brief Execute the successive shortest path algorithm to find
339 341
    /// an optimal flow.
340 342
    ///
341 343
    /// This function executes the successive shortest path algorithm to
342 344
    /// find a minimum cost flow, which is the union of \c k or less
343 345
    /// arc-disjoint paths.
344 346
    ///
345 347
    /// \return \c k if there are at least \c k arc-disjoint paths from
346 348
    /// \c s to \c t in the digraph. Otherwise it returns the number of
347 349
    /// arc-disjoint paths found.
348 350
    ///
349 351
    /// \pre \ref init() must be called before using this function.
350 352
    int findFlow(int k = 2) {
351 353
      // Find shortest paths
352 354
      _path_num = 0;
353 355
      while (_path_num < k) {
354 356
        // Run Dijkstra
355 357
        if (!_dijkstra->run()) break;
356 358
        ++_path_num;
357 359

	
358 360
        // Set the flow along the found shortest path
359 361
        Node u = _target;
360 362
        Arc e;
361 363
        while ((e = _pred[u]) != INVALID) {
362 364
          if (u == _graph.target(e)) {
363 365
            (*_flow)[e] = 1;
364 366
            u = _graph.source(e);
365 367
          } else {
366 368
            (*_flow)[e] = 0;
367 369
            u = _graph.target(e);
368 370
          }
369 371
        }
370 372
      }
371 373
      return _path_num;
372 374
    }
373 375

	
374 376
    /// \brief Compute the paths from the flow.
375 377
    ///
376 378
    /// This function computes the paths from the flow.
377 379
    ///
378 380
    /// \pre \ref init() and \ref findFlow() must be called before using
379 381
    /// this function.
380 382
    void findPaths() {
381 383
      // Create the residual flow map (the union of the paths not found
382 384
      // so far)
383 385
      FlowMap res_flow(_graph);
384 386
      for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
385 387

	
386 388
      paths.clear();
387 389
      paths.resize(_path_num);
388 390
      for (int i = 0; i < _path_num; ++i) {
389 391
        Node n = _source;
390 392
        while (n != _target) {
391 393
          OutArcIt e(_graph, n);
392 394
          for ( ; res_flow[e] == 0; ++e) ;
393 395
          n = _graph.target(e);
394 396
          paths[i].addBack(e);
395 397
          res_flow[e] = 0;
396 398
        }
397 399
      }
398 400
    }
399 401

	
400 402
    /// @}
401 403

	
402 404
    /// \name Query Functions
403 405
    /// The results of the algorithm can be obtained using these
404 406
    /// functions.
405 407
    /// \n The algorithm should be executed before using them.
406 408

	
407 409
    /// @{
408 410

	
409 411
    /// \brief Return a const reference to the arc map storing the
410 412
    /// found flow.
411 413
    ///
412 414
    /// This function returns a const reference to the arc map storing
413 415
    /// the flow that is the union of the found arc-disjoint paths.
414 416
    ///
415 417
    /// \pre \ref run() or \ref findFlow() must be called before using
416 418
    /// this function.
417 419
    const FlowMap& flowMap() const {
418 420
      return *_flow;
419 421
    }
420 422

	
421 423
    /// \brief Return a const reference to the node map storing the
422 424
    /// found potentials (the dual solution).
423 425
    ///
424 426
    /// This function returns a const reference to the node map storing
425 427
    /// the found potentials that provide the dual solution of the
426 428
    /// underlying minimum cost flow problem.
427 429
    ///
428 430
    /// \pre \ref run() or \ref findFlow() must be called before using
429 431
    /// this function.
430 432
    const PotentialMap& potentialMap() const {
431 433
      return *_potential;
432 434
    }
433 435

	
434 436
    /// \brief Return the flow on the given arc.
435 437
    ///
436 438
    /// This function returns the flow on the given arc.
437 439
    /// It is \c 1 if the arc is involved in one of the found paths,
438 440
    /// otherwise it is \c 0.
439 441
    ///
440 442
    /// \pre \ref run() or \ref findFlow() must be called before using
441 443
    /// this function.
442 444
    int flow(const Arc& arc) const {
443 445
      return (*_flow)[arc];
444 446
    }
445 447

	
446 448
    /// \brief Return the potential of the given node.
447 449
    ///
448 450
    /// This function returns the potential of the given node.
449 451
    ///
450 452
    /// \pre \ref run() or \ref findFlow() must be called before using
451 453
    /// this function.
452 454
    Length potential(const Node& node) const {
453 455
      return (*_potential)[node];
454 456
    }
455 457

	
456 458
    /// \brief Return the total length (cost) of the found paths (flow).
457 459
    ///
458 460
    /// This function returns the total length (cost) of the found paths
459 461
    /// (flow). The complexity of the function is \f$ O(e) \f$.
460 462
    ///
461 463
    /// \pre \ref run() or \ref findFlow() must be called before using
462 464
    /// this function.
463 465
    Length totalLength() const {
464 466
      Length c = 0;
465 467
      for (ArcIt e(_graph); e != INVALID; ++e)
466 468
        c += (*_flow)[e] * _length[e];
467 469
      return c;
468 470
    }
469 471

	
470 472
    /// \brief Return the number of the found paths.
471 473
    ///
472 474
    /// This function returns the number of the found paths.
473 475
    ///
474 476
    /// \pre \ref run() or \ref findFlow() must be called before using
475 477
    /// this function.
476 478
    int pathNum() const {
477 479
      return _path_num;
478 480
    }
479 481

	
480 482
    /// \brief Return a const reference to the specified path.
481 483
    ///
482 484
    /// This function returns a const reference to the specified path.
483 485
    ///
484 486
    /// \param i The function returns the \c i-th path.
485 487
    /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
486 488
    ///
487 489
    /// \pre \ref run() or \ref findPaths() must be called before using
488 490
    /// this function.
489 491
    Path path(int i) const {
490 492
      return paths[i];
491 493
    }
492 494

	
493 495
    /// @}
494 496

	
495 497
  }; //class Suurballe
496 498

	
497 499
  ///@}
498 500

	
499 501
} //namespace lemon
500 502

	
501 503
#endif //LEMON_SUURBALLE_H
0 comments (0 inline)