gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix and uniform the usage of Graph and Parent typedefs (#268) - Rename Graph typedefs to GraphType in the implementation of graph maps and MapExtender to prevent conflicts (especially using VS). They are not public. - Make Parent typedefs private in all classes. - Replace Digraph with Graph in some places (fix faulty renamings of the script). - Use Graph and Digraph typedefs (more) consequently.
0 19 0
default
19 files changed:
↑ Collapse diff ↑
Ignore white space 131072 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 33
#include <lemon/bits/map_extender.h>
34 34
#include <lemon/tolerance.h>
35 35

	
36 36
#include <algorithm>
37 37

	
38 38
namespace lemon {
39 39

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
110 110
    template <typename V>
111 111
    class NodeMap : public DGR::template NodeMap<V> {
112
      typedef typename DGR::template NodeMap<V> Parent;
113

	
112 114
    public:
113

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

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

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

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

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

	
133 131
    };
134 132

	
135 133
    template <typename V>
136 134
    class ArcMap : public DGR::template ArcMap<V> {
135
      typedef typename DGR::template ArcMap<V> Parent;
136

	
137 137
    public:
138

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

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

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

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

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

	
158 154
    };
159 155

	
160 156
  };
161 157

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

	
167 163
  protected:
168 164
    GR* _graph;
169 165

	
170 166
    GraphAdaptorBase() : _graph(0) {}
171 167

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

	
174 170
  public:
175 171
    GraphAdaptorBase(GR& graph) : _graph(&graph) {}
176 172

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
256 252
    template <typename V>
257 253
    class NodeMap : public GR::template NodeMap<V> {
254
      typedef typename GR::template NodeMap<V> Parent;
255

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

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

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

	
276 273
    };
277 274

	
278 275
    template <typename V>
279 276
    class ArcMap : public GR::template ArcMap<V> {
277
      typedef typename GR::template ArcMap<V> Parent;
278

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

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

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

	
299 297
    template <typename V>
300 298
    class EdgeMap : public GR::template EdgeMap<V> {
299
      typedef typename GR::template EdgeMap<V> Parent;
300

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

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

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

	
320 319
  };
321 320

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

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

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

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

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

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

	
350 349
  };
351 350

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

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

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

	
403 402

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

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

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

	
425 424
  public:
426 425

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

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

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

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

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

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

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

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

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

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

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

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

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

	
506 505
  public:
507 506

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

	
512 514
    public:
513 515
      typedef V Value;
514
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
515
	    LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
516 516

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

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

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

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

	
538 541
    public:
539 542
      typedef V Value;
540
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
541
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
542 543

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

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

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

	
560 561
  };
561 562

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

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

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

	
584 585
  public:
585 586

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

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

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

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

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

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

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

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

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

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

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

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

	
653 657
    public:
654 658
      typedef V Value;
655
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, 
656
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
657 659

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

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

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

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

	
679 684
    public:
680 685
      typedef V Value;
681
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
682
          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
683 686

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

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

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

	
701 704
  };
702 705

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

	
754 757
    typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> >
755 758
      Parent;
756 759

	
757 760
    typedef typename Parent::Node Node;
758 761
    typedef typename Parent::Arc Arc;
759 762

	
760 763
  protected:
761 764
    SubDigraph() { }
762 765
  public:
763 766

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

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

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

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

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

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

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

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

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

	
824 827
  };
825 828

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

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

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

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

	
863 866

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

	
871 875
    typedef SubGraphBase Adaptor;
872
    typedef GraphAdaptorBase<GR> Parent;
873 876
  protected:
874 877

	
875 878
    NF* _node_filter;
876 879
    EF* _edge_filter;
877 880

	
878 881
    SubGraphBase()
879 882
      : Parent(), _node_filter(0), _edge_filter(0) { }
880 883

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

	
887 890
  public:
888 891

	
889 892
    typedef typename Parent::Node Node;
890 893
    typedef typename Parent::Arc Arc;
891 894
    typedef typename Parent::Edge Edge;
892 895

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
985 988
    typedef False NodeNumTag;
986 989
    typedef False ArcNumTag;
987 990
    typedef False EdgeNumTag;
988 991

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

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

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

	
1019 1025
    public:
1020 1026
      typedef V Value;
1021
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1022
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1023 1027

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

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

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

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

	
1045 1052
    public:
1046 1053
      typedef V Value;
1047
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1048
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1049 1054

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

	
1055 1060
    private:
1056 1061
      ArcMap& operator=(const ArcMap& cmap) {
1057 1062
        return operator=<ArcMap>(cmap);
1058 1063
      }
1059 1064

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

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

	
1071 1079
    public:
1072 1080
      typedef V Value;
1073
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1074
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1075 1081

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

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

	
1082 1088
    private:
1083 1089
      EdgeMap& operator=(const EdgeMap& cmap) {
1084 1090
        return operator=<EdgeMap>(cmap);
1085 1091
      }
1086 1092

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

	
1094 1100
  };
1095 1101

	
1096 1102
  template <typename GR, typename NF, typename EF>
1097 1103
  class SubGraphBase<GR, NF, EF, false>
1098 1104
    : public GraphAdaptorBase<GR> {
1105
    typedef GraphAdaptorBase<GR> Parent;
1099 1106
  public:
1100 1107
    typedef GR Graph;
1101 1108
    typedef NF NodeFilterMap;
1102 1109
    typedef EF EdgeFilterMap;
1103 1110

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

	
1112 1118
    void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
1113 1119
      Parent::initialize(graph);
1114 1120
      _node_filter = &node_filter;
1115 1121
      _edge_filter = &edge_filter;
1116 1122
    }
1117 1123

	
1118 1124
  public:
1119 1125

	
1120 1126
    typedef typename Parent::Node Node;
1121 1127
    typedef typename Parent::Arc Arc;
1122 1128
    typedef typename Parent::Edge Edge;
1123 1129

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

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

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

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

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

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

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

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

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

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

	
1186 1192
    typedef False NodeNumTag;
1187 1193
    typedef False ArcNumTag;
1188 1194
    typedef False EdgeNumTag;
1189 1195

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

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

	
1210 1216
    template <typename V>
1211 1217
    class NodeMap 
1212 1218
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1213 1219
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1220
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1221
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1222

	
1214 1223
    public:
1215 1224
      typedef V Value;
1216
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1217
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1218 1225

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

	
1224 1231
    private:
1225 1232
      NodeMap& operator=(const NodeMap& cmap) {
1226 1233
        return operator=<NodeMap>(cmap);
1227 1234
      }
1228 1235

	
1229 1236
      template <typename CMap>
1230 1237
      NodeMap& operator=(const CMap& cmap) {
1231 1238
        Parent::operator=(cmap);
1232 1239
        return *this;
1233 1240
      }
1234 1241
    };
1235 1242

	
1236 1243
    template <typename V>
1237 1244
    class ArcMap 
1238 1245
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1239 1246
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1247
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1248
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1249

	
1240 1250
    public:
1241 1251
      typedef V Value;
1242
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1243
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1244 1252

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

	
1250 1258
    private:
1251 1259
      ArcMap& operator=(const ArcMap& cmap) {
1252 1260
        return operator=<ArcMap>(cmap);
1253 1261
      }
1254 1262

	
1255 1263
      template <typename CMap>
1256 1264
      ArcMap& operator=(const CMap& cmap) {
1257 1265
        Parent::operator=(cmap);
1258 1266
        return *this;
1259 1267
      }
1260 1268
    };
1261 1269

	
1262 1270
    template <typename V>
1263 1271
    class EdgeMap 
1264 1272
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1265 1273
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1274
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1275
	LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1276

	
1266 1277
    public:
1267 1278
      typedef V Value;
1268
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1269
		  LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1270 1279

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

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

	
1277 1286
    private:
1278 1287
      EdgeMap& operator=(const EdgeMap& cmap) {
1279 1288
        return operator=<EdgeMap>(cmap);
1280 1289
      }
1281 1290

	
1282 1291
      template <typename CMap>
1283 1292
      EdgeMap& operator=(const CMap& cmap) {
1284 1293
        Parent::operator=(cmap);
1285 1294
        return *this;
1286 1295
      }
1287 1296
    };
1288 1297

	
1289 1298
  };
1290 1299

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

	
1343 1352
    typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> >
1344 1353
      Parent;
1345 1354

	
1346 1355
    typedef typename Parent::Node Node;
1347 1356
    typedef typename Parent::Edge Edge;
1348 1357

	
1349 1358
  protected:
1350 1359
    SubGraph() { }
1351 1360
  public:
1352 1361

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

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

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

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

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

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

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

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

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

	
1413 1422
  };
1414 1423

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

	
1427 1436
  template<typename GR, typename NF, typename EF>
1428 1437
  SubGraph<const GR, const NF, EF>
1429 1438
  subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) {
1430 1439
    return SubGraph<const GR, const NF, EF>
1431 1440
      (graph, node_filter, edge_filter);
1432 1441
  }
1433 1442

	
1434 1443
  template<typename GR, typename NF, typename EF>
1435 1444
  SubGraph<const GR, NF, const EF>
1436 1445
  subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) {
1437 1446
    return SubGraph<const GR, NF, const EF>
1438 1447
      (graph, node_filter, edge_filter);
1439 1448
  }
1440 1449

	
1441 1450
  template<typename GR, typename NF, typename EF>
1442 1451
  SubGraph<const GR, const NF, const EF>
1443 1452
  subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) {
1444 1453
    return SubGraph<const GR, const NF, const EF>
1445 1454
      (graph, node_filter, edge_filter);
1446 1455
  }
1447 1456

	
1448 1457

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

	
1490
    typedef GR Digraph;
1491
    typedef NF NodeFilterMap;
1492

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

	
1501
  public:
1502

	
1503
    typedef GR Digraph;
1504
    typedef NF NodeFilterMap;
1505

	
1497 1506
    typedef typename Parent::Node Node;
1498 1507

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

	
1502 1511
    FilterNodes() : const_true_map() {}
1503 1512

	
1504 1513
  public:
1505 1514

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

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

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

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

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

	
1542 1551
  };
1543 1552

	
1544 1553
  template<typename GR, typename NF>
1545 1554
  class FilterNodes<GR, NF,
1546 1555
                    typename enable_if<UndirectedTagIndicator<GR> >::type> :
1547 1556
    public GraphAdaptorExtender<
1548 1557
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, 
1549 1558
                   true> > {
1550 1559

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

	
1564
  public:
1565

	
1566
    typedef GR Graph;
1567
    typedef NF NodeFilterMap;
1568

	
1558 1569
    typedef typename Parent::Node Node;
1570

	
1559 1571
  protected:
1560 1572
    ConstMap<typename GR::Edge, Const<bool, true> > const_true_map;
1561 1573

	
1562 1574
    FilterNodes() : const_true_map() {}
1563 1575

	
1564 1576
  public:
1565 1577

	
1566 1578
    FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1567 1579
      Parent(), const_true_map() {
1568 1580
      Parent::initialize(graph, node_filter, const_true_map);
1569 1581
    }
1570 1582

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

	
1576 1588
  };
1577 1589

	
1578 1590

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

	
1590 1602
  template<typename GR, typename NF>
1591 1603
  FilterNodes<const GR, const NF>
1592 1604
  filterNodes(const GR& graph, const NF& node_filter) {
1593 1605
    return FilterNodes<const GR, const NF>(graph, node_filter);
1594 1606
  }
1595 1607

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

	
1632 1648
  public:
1649

	
1633 1650
    /// The type of the adapted digraph.
1634 1651
    typedef DGR Digraph;
1635 1652
    /// The type of the arc filter map.
1636 1653
    typedef AF ArcFilterMap;
1637 1654

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

	
1642 1655
    typedef typename Parent::Arc Arc;
1643 1656

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

	
1647 1660
    FilterArcs() : const_true_map() {}
1648 1661

	
1649 1662
  public:
1650 1663

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

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

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

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

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

	
1686 1699
  };
1687 1700

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

	
1699 1712
  template<typename DGR, typename AF>
1700 1713
  FilterArcs<const DGR, const AF>
1701 1714
  filterArcs(const DGR& digraph, const AF& arc_filter) {
1702 1715
    return FilterArcs<const DGR, const AF>(digraph, arc_filter);
1703 1716
  }
1704 1717

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

	
1741 1758
  public:
1759

	
1742 1760
    /// The type of the adapted graph.
1743 1761
    typedef GR Graph;
1744 1762
    /// The type of the edge filter map.
1745 1763
    typedef EF EdgeFilterMap;
1746 1764

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

	
1751 1765
    typedef typename Parent::Edge Edge;
1752 1766

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

	
1756 1770
    FilterEdges() : const_true_map(true) {
1757 1771
      Parent::setNodeFilterMap(const_true_map);
1758 1772
    }
1759 1773

	
1760 1774
  public:
1761 1775

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

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

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

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

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

	
1797 1811
  };
1798 1812

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

	
1810 1824
  template<typename GR, typename EF>
1811 1825
  FilterEdges<const GR, const EF>
1812 1826
  filterEdges(const GR& graph, const EF& edge_filter) {
1813 1827
    return FilterEdges<const GR, const EF>(graph, edge_filter);
1814 1828
  }
1815 1829

	
1816 1830

	
1817 1831
  template <typename DGR>
1818 1832
  class UndirectorBase {
1819 1833
  public:
1820 1834
    typedef DGR Digraph;
1821 1835
    typedef UndirectorBase Adaptor;
1822 1836

	
1823 1837
    typedef True UndirectedTag;
1824 1838

	
1825 1839
    typedef typename Digraph::Arc Edge;
1826 1840
    typedef typename Digraph::Node Node;
1827 1841

	
1828 1842
    class Arc : public Edge {
1829 1843
      friend class UndirectorBase;
1830 1844
    protected:
1831 1845
      bool _forward;
1832 1846

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

	
1836 1850
    public:
1837 1851
      Arc() {}
1838 1852

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

	
1841 1855
      bool operator==(const Arc &other) const {
1842 1856
        return _forward == other._forward &&
1843 1857
          static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1844 1858
      }
1845 1859
      bool operator!=(const Arc &other) const {
1846 1860
        return _forward != other._forward ||
1847 1861
          static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1848 1862
      }
1849 1863
      bool operator<(const Arc &other) const {
1850 1864
        return _forward < other._forward ||
1851 1865
          (_forward == other._forward &&
1852 1866
           static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1853 1867
      }
1854 1868
    };
1855 1869

	
1856 1870
    void first(Node& n) const {
1857 1871
      _digraph->first(n);
1858 1872
    }
1859 1873

	
1860 1874
    void next(Node& n) const {
1861 1875
      _digraph->next(n);
1862 1876
    }
1863 1877

	
1864 1878
    void first(Arc& a) const {
1865 1879
      _digraph->first(a);
1866 1880
      a._forward = true;
1867 1881
    }
1868 1882

	
1869 1883
    void next(Arc& a) const {
1870 1884
      if (a._forward) {
1871 1885
        a._forward = false;
1872 1886
      } else {
1873 1887
        _digraph->next(a);
1874 1888
        a._forward = true;
1875 1889
      }
1876 1890
    }
1877 1891

	
1878 1892
    void first(Edge& e) const {
1879 1893
      _digraph->first(e);
1880 1894
    }
1881 1895

	
1882 1896
    void next(Edge& e) const {
1883 1897
      _digraph->next(e);
1884 1898
    }
1885 1899

	
1886 1900
    void firstOut(Arc& a, const Node& n) const {
1887 1901
      _digraph->firstIn(a, n);
1888 1902
      if( static_cast<const Edge&>(a) != INVALID ) {
1889 1903
        a._forward = false;
1890 1904
      } else {
1891 1905
        _digraph->firstOut(a, n);
1892 1906
        a._forward = true;
1893 1907
      }
1894 1908
    }
1895 1909
    void nextOut(Arc &a) const {
1896 1910
      if (!a._forward) {
1897 1911
        Node n = _digraph->target(a);
1898 1912
        _digraph->nextIn(a);
1899 1913
        if (static_cast<const Edge&>(a) == INVALID ) {
1900 1914
          _digraph->firstOut(a, n);
1901 1915
          a._forward = true;
1902 1916
        }
1903 1917
      }
1904 1918
      else {
1905 1919
        _digraph->nextOut(a);
1906 1920
      }
1907 1921
    }
1908 1922

	
1909 1923
    void firstIn(Arc &a, const Node &n) const {
1910 1924
      _digraph->firstOut(a, n);
1911 1925
      if (static_cast<const Edge&>(a) != INVALID ) {
1912 1926
        a._forward = false;
1913 1927
      } else {
1914 1928
        _digraph->firstIn(a, n);
1915 1929
        a._forward = true;
1916 1930
      }
1917 1931
    }
1918 1932
    void nextIn(Arc &a) const {
1919 1933
      if (!a._forward) {
1920 1934
        Node n = _digraph->source(a);
1921 1935
        _digraph->nextOut(a);
1922 1936
        if( static_cast<const Edge&>(a) == INVALID ) {
1923 1937
          _digraph->firstIn(a, n);
1924 1938
          a._forward = true;
1925 1939
        }
1926 1940
      }
1927 1941
      else {
1928 1942
        _digraph->nextIn(a);
1929 1943
      }
1930 1944
    }
1931 1945

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

	
1940 1954
    void nextInc(Edge &e, bool &d) const {
1941 1955
      if (d) {
1942 1956
        Node s = _digraph->source(e);
1943 1957
        _digraph->nextOut(e);
1944 1958
        if (e != INVALID) return;
1945 1959
        d = false;
1946 1960
        _digraph->firstIn(e, s);
1947 1961
      } else {
1948 1962
        _digraph->nextIn(e);
1949 1963
      }
1950 1964
    }
1951 1965

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

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

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

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

	
1968 1982
    static Arc direct(const Edge &e, bool d) {
1969 1983
      return Arc(e, d);
1970 1984
    }
1971 1985
    Arc direct(const Edge &e, const Node& n) const {
1972 1986
      return Arc(e, _digraph->source(e) == n);
1973 1987
    }
1974 1988

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

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

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

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

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

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

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

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

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

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

	
2012 2026
    typedef FindArcTagIndicator<Digraph> FindArcTag;
2013 2027
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
2014 2028
      if (p == INVALID) {
2015 2029
        Edge arc = _digraph->findArc(s, t);
2016 2030
        if (arc != INVALID) return direct(arc, true);
2017 2031
        arc = _digraph->findArc(t, s);
2018 2032
        if (arc != INVALID) return direct(arc, false);
2019 2033
      } else if (direction(p)) {
2020 2034
        Edge arc = _digraph->findArc(s, t, p);
2021 2035
        if (arc != INVALID) return direct(arc, true);
2022 2036
        arc = _digraph->findArc(t, s);
2023 2037
        if (arc != INVALID) return direct(arc, false);
2024 2038
      } else {
2025 2039
        Edge arc = _digraph->findArc(t, s, p);
2026 2040
        if (arc != INVALID) return direct(arc, false);
2027 2041
      }
2028 2042
      return INVALID;
2029 2043
    }
2030 2044

	
2031 2045
    typedef FindArcTag FindEdgeTag;
2032 2046
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
2033 2047
      if (s != t) {
2034 2048
        if (p == INVALID) {
2035 2049
          Edge arc = _digraph->findArc(s, t);
2036 2050
          if (arc != INVALID) return arc;
2037 2051
          arc = _digraph->findArc(t, s);
2038 2052
          if (arc != INVALID) return arc;
2039 2053
        } else if (_digraph->source(p) == s) {
2040 2054
          Edge arc = _digraph->findArc(s, t, p);
2041 2055
          if (arc != INVALID) return arc;
2042 2056
          arc = _digraph->findArc(t, s);
2043 2057
          if (arc != INVALID) return arc;
2044 2058
        } else {
2045 2059
          Edge arc = _digraph->findArc(t, s, p);
2046 2060
          if (arc != INVALID) return arc;
2047 2061
        }
2048 2062
      } else {
2049 2063
        return _digraph->findArc(s, t, p);
2050 2064
      }
2051 2065
      return INVALID;
2052 2066
    }
2053 2067

	
2054 2068
  private:
2055 2069

	
2056 2070
    template <typename V>
2057 2071
    class ArcMapBase {
2058 2072
    private:
2059 2073

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

	
2062 2076
    public:
2063 2077

	
2064 2078
      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
2065 2079

	
2066 2080
      typedef V Value;
2067 2081
      typedef Arc Key;
2068 2082
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
2069 2083
      typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
2070 2084
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
2071 2085
      typedef typename MapTraits<MapImpl>::ReturnValue Reference;
2072 2086

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

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

	
2080 2094
      void set(const Arc& a, const V& value) {
2081 2095
        if (direction(a)) {
2082 2096
          _forward.set(a, value);
2083 2097
        } else {
2084 2098
          _backward.set(a, value);
2085 2099
        }
2086 2100
      }
2087 2101

	
2088 2102
      ConstReturnValue operator[](const Arc& a) const {
2089 2103
        if (direction(a)) {
2090 2104
          return _forward[a];
2091 2105
        } else {
2092 2106
          return _backward[a];
2093 2107
        }
2094 2108
      }
2095 2109

	
2096 2110
      ReturnValue operator[](const Arc& a) {
2097 2111
        if (direction(a)) {
2098 2112
          return _forward[a];
2099 2113
        } else {
2100 2114
          return _backward[a];
2101 2115
        }
2102 2116
      }
2103 2117

	
2104 2118
    protected:
2105 2119

	
2106 2120
      MapImpl _forward, _backward;
2107 2121

	
2108 2122
    };
2109 2123

	
2110 2124
  public:
2111 2125

	
2112 2126
    template <typename V>
2113 2127
    class NodeMap : public DGR::template NodeMap<V> {
2128
      typedef typename DGR::template NodeMap<V> Parent;
2129

	
2114 2130
    public:
2115

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

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

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

	
2125 2139
    private:
2126 2140
      NodeMap& operator=(const NodeMap& cmap) {
2127 2141
        return operator=<NodeMap>(cmap);
2128 2142
      }
2129 2143

	
2130 2144
      template <typename CMap>
2131 2145
      NodeMap& operator=(const CMap& cmap) {
2132 2146
        Parent::operator=(cmap);
2133 2147
        return *this;
2134 2148
      }
2135 2149

	
2136 2150
    };
2137 2151

	
2138 2152
    template <typename V>
2139 2153
    class ArcMap
2140
      : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> >
2141
    {
2154
      : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > {
2155
      typedef SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > Parent;
2156

	
2142 2157
    public:
2143 2158
      typedef V Value;
2144
      typedef SubMapExtender<Adaptor, ArcMapBase<V> > Parent;
2145 2159

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

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

	
2152 2166
    private:
2153 2167
      ArcMap& operator=(const ArcMap& cmap) {
2154 2168
        return operator=<ArcMap>(cmap);
2155 2169
      }
2156 2170

	
2157 2171
      template <typename CMap>
2158 2172
      ArcMap& operator=(const CMap& cmap) {
2159 2173
        Parent::operator=(cmap);
2160 2174
        return *this;
2161 2175
      }
2162 2176
    };
2163 2177

	
2164 2178
    template <typename V>
2165 2179
    class EdgeMap : public Digraph::template ArcMap<V> {
2180
      typedef typename Digraph::template ArcMap<V> Parent;
2181

	
2166 2182
    public:
2167

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

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

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

	
2177 2191
    private:
2178 2192
      EdgeMap& operator=(const EdgeMap& cmap) {
2179 2193
        return operator=<EdgeMap>(cmap);
2180 2194
      }
2181 2195

	
2182 2196
      template <typename CMap>
2183 2197
      EdgeMap& operator=(const CMap& cmap) {
2184 2198
        Parent::operator=(cmap);
2185 2199
        return *this;
2186 2200
      }
2187 2201

	
2188 2202
    };
2189 2203

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

	
2193 2207
    typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier;
2194 2208
    EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2195 2209
    
2196 2210
    typedef EdgeNotifier ArcNotifier;
2197 2211
    ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); }
2198 2212

	
2199 2213
  protected:
2200 2214

	
2201 2215
    UndirectorBase() : _digraph(0) {}
2202 2216

	
2203 2217
    DGR* _digraph;
2204 2218

	
2205 2219
    void initialize(DGR& digraph) {
2206 2220
      _digraph = &digraph;
2207 2221
    }
2208 2222

	
2209 2223
  };
2210 2224

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

	
2249 2263
    /// \brief Constructor
2250 2264
    ///
2251 2265
    /// Creates an undirected graph from the given digraph.
2252 2266
    Undirector(DGR& digraph) {
2253 2267
      initialize(digraph);
2254 2268
    }
2255 2269

	
2256 2270
    /// \brief Arc map combined from two original arc maps
2257 2271
    ///
2258 2272
    /// This map adaptor class adapts two arc maps of the underlying
2259 2273
    /// digraph to get an arc map of the undirected graph.
2260 2274
    /// Its value type is inherited from the first arc map type (\c FW).
2261 2275
    /// \tparam FW The type of the "foward" arc map.
2262 2276
    /// \tparam BK The type of the "backward" arc map.
2263 2277
    template <typename FW, typename BK>
2264 2278
    class CombinedArcMap {
2265 2279
    public:
2266 2280

	
2267 2281
      /// The key type of the map
2268 2282
      typedef typename Parent::Arc Key;
2269 2283
      /// The value type of the map
2270 2284
      typedef typename FW::Value Value;
2271 2285

	
2272 2286
      typedef typename MapTraits<FW>::ReferenceMapTag ReferenceMapTag;
2273 2287

	
2274 2288
      typedef typename MapTraits<FW>::ReturnValue ReturnValue;
2275 2289
      typedef typename MapTraits<FW>::ConstReturnValue ConstReturnValue;
2276 2290
      typedef typename MapTraits<FW>::ReturnValue Reference;
2277 2291
      typedef typename MapTraits<FW>::ConstReturnValue ConstReference;
2278 2292

	
2279 2293
      /// Constructor
2280 2294
      CombinedArcMap(FW& forward, BK& backward)
2281 2295
        : _forward(&forward), _backward(&backward) {}
2282 2296

	
2283 2297
      /// Sets the value associated with the given key.
2284 2298
      void set(const Key& e, const Value& a) {
2285 2299
        if (Parent::direction(e)) {
2286 2300
          _forward->set(e, a);
2287 2301
        } else {
2288 2302
          _backward->set(e, a);
2289 2303
        }
2290 2304
      }
2291 2305

	
2292 2306
      /// Returns the value associated with the given key.
2293 2307
      ConstReturnValue operator[](const Key& e) const {
2294 2308
        if (Parent::direction(e)) {
2295 2309
          return (*_forward)[e];
2296 2310
        } else {
2297 2311
          return (*_backward)[e];
2298 2312
        }
2299 2313
      }
2300 2314

	
2301 2315
      /// Returns a reference to the value associated with the given key.
2302 2316
      ReturnValue operator[](const Key& e) {
2303 2317
        if (Parent::direction(e)) {
2304 2318
          return (*_forward)[e];
2305 2319
        } else {
2306 2320
          return (*_backward)[e];
2307 2321
        }
2308 2322
      }
2309 2323

	
2310 2324
    protected:
2311 2325

	
2312 2326
      FW* _forward;
2313 2327
      BK* _backward;
2314 2328

	
2315 2329
    };
2316 2330

	
2317 2331
    /// \brief Returns a combined arc map
2318 2332
    ///
2319 2333
    /// This function just returns a combined arc map.
2320 2334
    template <typename FW, typename BK>
2321 2335
    static CombinedArcMap<FW, BK>
2322 2336
    combinedArcMap(FW& forward, BK& backward) {
2323 2337
      return CombinedArcMap<FW, BK>(forward, backward);
2324 2338
    }
2325 2339

	
2326 2340
    template <typename FW, typename BK>
2327 2341
    static CombinedArcMap<const FW, BK>
2328 2342
    combinedArcMap(const FW& forward, BK& backward) {
2329 2343
      return CombinedArcMap<const FW, BK>(forward, backward);
2330 2344
    }
2331 2345

	
2332 2346
    template <typename FW, typename BK>
2333 2347
    static CombinedArcMap<FW, const BK>
2334 2348
    combinedArcMap(FW& forward, const BK& backward) {
2335 2349
      return CombinedArcMap<FW, const BK>(forward, backward);
2336 2350
    }
2337 2351

	
2338 2352
    template <typename FW, typename BK>
2339 2353
    static CombinedArcMap<const FW, const BK>
2340 2354
    combinedArcMap(const FW& forward, const BK& backward) {
2341 2355
      return CombinedArcMap<const FW, const BK>(forward, backward);
2342 2356
    }
2343 2357

	
2344 2358
  };
2345 2359

	
2346 2360
  /// \brief Returns a read-only Undirector adaptor
2347 2361
  ///
2348 2362
  /// This function just returns a read-only \ref Undirector adaptor.
2349 2363
  /// \ingroup graph_adaptors
2350 2364
  /// \relates Undirector
2351 2365
  template<typename DGR>
2352 2366
  Undirector<const DGR> undirector(const DGR& digraph) {
2353 2367
    return Undirector<const DGR>(digraph);
2354 2368
  }
2355 2369

	
2356 2370

	
2357 2371
  template <typename GR, typename DM>
2358 2372
  class OrienterBase {
2359 2373
  public:
2360 2374

	
2361 2375
    typedef GR Graph;
2362 2376
    typedef DM DirectionMap;
2363 2377

	
2364 2378
    typedef typename GR::Node Node;
2365 2379
    typedef typename GR::Edge Arc;
2366 2380

	
2367 2381
    void reverseArc(const Arc& arc) {
2368 2382
      _direction->set(arc, !(*_direction)[arc]);
2369 2383
    }
2370 2384

	
2371 2385
    void first(Node& i) const { _graph->first(i); }
2372 2386
    void first(Arc& i) const { _graph->first(i); }
2373 2387
    void firstIn(Arc& i, const Node& n) const {
2374 2388
      bool d = true;
2375 2389
      _graph->firstInc(i, d, n);
2376 2390
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2377 2391
    }
2378 2392
    void firstOut(Arc& i, const Node& n ) const {
2379 2393
      bool d = true;
2380 2394
      _graph->firstInc(i, d, n);
2381 2395
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2382 2396
    }
2383 2397

	
2384 2398
    void next(Node& i) const { _graph->next(i); }
2385 2399
    void next(Arc& i) const { _graph->next(i); }
2386 2400
    void nextIn(Arc& i) const {
2387 2401
      bool d = !(*_direction)[i];
2388 2402
      _graph->nextInc(i, d);
2389 2403
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2390 2404
    }
2391 2405
    void nextOut(Arc& i) const {
2392 2406
      bool d = (*_direction)[i];
2393 2407
      _graph->nextInc(i, d);
2394 2408
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2395 2409
    }
2396 2410

	
2397 2411
    Node source(const Arc& e) const {
2398 2412
      return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2399 2413
    }
2400 2414
    Node target(const Arc& e) const {
2401 2415
      return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2402 2416
    }
2403 2417

	
2404 2418
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
2405 2419
    int nodeNum() const { return _graph->nodeNum(); }
2406 2420

	
2407 2421
    typedef EdgeNumTagIndicator<Graph> ArcNumTag;
2408 2422
    int arcNum() const { return _graph->edgeNum(); }
2409 2423

	
2410 2424
    typedef FindEdgeTagIndicator<Graph> FindArcTag;
2411 2425
    Arc findArc(const Node& u, const Node& v,
2412 2426
                const Arc& prev = INVALID) const {
2413 2427
      Arc arc = _graph->findEdge(u, v, prev);
2414 2428
      while (arc != INVALID && source(arc) != u) {
2415 2429
        arc = _graph->findEdge(u, v, arc);
2416 2430
      }
2417 2431
      return arc;
2418 2432
    }
2419 2433

	
2420 2434
    Node addNode() {
2421 2435
      return Node(_graph->addNode());
2422 2436
    }
2423 2437

	
2424 2438
    Arc addArc(const Node& u, const Node& v) {
2425 2439
      Arc arc = _graph->addEdge(u, v);
2426 2440
      _direction->set(arc, _graph->u(arc) == u);
2427 2441
      return arc;
2428 2442
    }
2429 2443

	
2430 2444
    void erase(const Node& i) { _graph->erase(i); }
2431 2445
    void erase(const Arc& i) { _graph->erase(i); }
2432 2446

	
2433 2447
    void clear() { _graph->clear(); }
2434 2448

	
2435 2449
    int id(const Node& v) const { return _graph->id(v); }
2436 2450
    int id(const Arc& e) const { return _graph->id(e); }
2437 2451

	
2438 2452
    Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2439 2453
    Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2440 2454

	
2441 2455
    int maxNodeId() const { return _graph->maxNodeId(); }
2442 2456
    int maxArcId() const { return _graph->maxEdgeId(); }
2443 2457

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

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

	
2450 2464
    template <typename V>
2451 2465
    class NodeMap : public GR::template NodeMap<V> {
2466
      typedef typename GR::template NodeMap<V> Parent;
2467

	
2452 2468
    public:
2453 2469

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

	
2456 2470
      explicit NodeMap(const OrienterBase<GR, DM>& adapter)
2457 2471
        : Parent(*adapter._graph) {}
2458 2472

	
2459 2473
      NodeMap(const OrienterBase<GR, DM>& adapter, const V& value)
2460 2474
        : Parent(*adapter._graph, value) {}
2461 2475

	
2462 2476
    private:
2463 2477
      NodeMap& operator=(const NodeMap& cmap) {
2464 2478
        return operator=<NodeMap>(cmap);
2465 2479
      }
2466 2480

	
2467 2481
      template <typename CMap>
2468 2482
      NodeMap& operator=(const CMap& cmap) {
2469 2483
        Parent::operator=(cmap);
2470 2484
        return *this;
2471 2485
      }
2472 2486

	
2473 2487
    };
2474 2488

	
2475 2489
    template <typename V>
2476 2490
    class ArcMap : public GR::template EdgeMap<V> {
2491
      typedef typename Graph::template EdgeMap<V> Parent;
2492

	
2477 2493
    public:
2478 2494

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

	
2481 2495
      explicit ArcMap(const OrienterBase<GR, DM>& adapter)
2482 2496
        : Parent(*adapter._graph) { }
2483 2497

	
2484 2498
      ArcMap(const OrienterBase<GR, DM>& adapter, const V& value)
2485 2499
        : Parent(*adapter._graph, value) { }
2486 2500

	
2487 2501
    private:
2488 2502
      ArcMap& operator=(const ArcMap& cmap) {
2489 2503
        return operator=<ArcMap>(cmap);
2490 2504
      }
2491 2505

	
2492 2506
      template <typename CMap>
2493 2507
      ArcMap& operator=(const CMap& cmap) {
2494 2508
        Parent::operator=(cmap);
2495 2509
        return *this;
2496 2510
      }
2497 2511
    };
2498 2512

	
2499 2513

	
2500 2514

	
2501 2515
  protected:
2502 2516
    Graph* _graph;
2503 2517
    DM* _direction;
2504 2518

	
2505 2519
    void initialize(GR& graph, DM& direction) {
2506 2520
      _graph = &graph;
2507 2521
      _direction = &direction;
2508 2522
    }
2509 2523

	
2510 2524
  };
2511 2525

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

	
2551 2566
    /// The type of the adapted graph.
2552 2567
    typedef GR Graph;
2553 2568
    /// The type of the direction edge map.
2554 2569
    typedef DM DirectionMap;
2555 2570

	
2556
    typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
2557 2571
    typedef typename Parent::Arc Arc;
2572

	
2558 2573
  protected:
2559 2574
    Orienter() { }
2575

	
2560 2576
  public:
2561 2577

	
2562 2578
    /// \brief Constructor
2563 2579
    ///
2564 2580
    /// Constructor of the adaptor.
2565 2581
    Orienter(GR& graph, DM& direction) {
2566 2582
      Parent::initialize(graph, direction);
2567 2583
    }
2568 2584

	
2569 2585
    /// \brief Reverses the given arc
2570 2586
    ///
2571 2587
    /// This function reverses the given arc.
2572 2588
    /// It is done by simply negate the assigned value of \c a
2573 2589
    /// in the direction map.
2574 2590
    void reverseArc(const Arc& a) {
2575 2591
      Parent::reverseArc(a);
2576 2592
    }
2577 2593
  };
2578 2594

	
2579 2595
  /// \brief Returns a read-only Orienter adaptor
2580 2596
  ///
2581 2597
  /// This function just returns a read-only \ref Orienter adaptor.
2582 2598
  /// \ingroup graph_adaptors
2583 2599
  /// \relates Orienter
2584 2600
  template<typename GR, typename DM>
2585 2601
  Orienter<const GR, DM>
2586 2602
  orienter(const GR& graph, DM& direction) {
2587 2603
    return Orienter<const GR, DM>(graph, direction);
2588 2604
  }
2589 2605

	
2590 2606
  template<typename GR, typename DM>
2591 2607
  Orienter<const GR, const DM>
2592 2608
  orienter(const GR& graph, const DM& direction) {
2593 2609
    return Orienter<const GR, const DM>(graph, direction);
2594 2610
  }
2595 2611

	
2596 2612
  namespace _adaptor_bits {
2597 2613

	
2598 2614
    template <typename DGR, typename CM, typename FM, typename TL>
2599 2615
    class ResForwardFilter {
2600 2616
    public:
2601 2617

	
2602 2618
      typedef typename DGR::Arc Key;
2603 2619
      typedef bool Value;
2604 2620

	
2605 2621
    private:
2606 2622

	
2607 2623
      const CM* _capacity;
2608 2624
      const FM* _flow;
2609 2625
      TL _tolerance;
2610 2626

	
2611 2627
    public:
2612 2628

	
2613 2629
      ResForwardFilter(const CM& capacity, const FM& flow,
2614 2630
                       const TL& tolerance = TL())
2615 2631
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2616 2632

	
2617 2633
      bool operator[](const typename DGR::Arc& a) const {
2618 2634
        return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2619 2635
      }
2620 2636
    };
2621 2637

	
2622 2638
    template<typename DGR,typename CM, typename FM, typename TL>
2623 2639
    class ResBackwardFilter {
2624 2640
    public:
2625 2641

	
2626 2642
      typedef typename DGR::Arc Key;
2627 2643
      typedef bool Value;
2628 2644

	
2629 2645
    private:
2630 2646

	
2631 2647
      const CM* _capacity;
2632 2648
      const FM* _flow;
2633 2649
      TL _tolerance;
2634 2650

	
2635 2651
    public:
2636 2652

	
2637 2653
      ResBackwardFilter(const CM& capacity, const FM& flow,
2638 2654
                        const TL& tolerance = TL())
2639 2655
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2640 2656

	
2641 2657
      bool operator[](const typename DGR::Arc& a) const {
2642 2658
        return _tolerance.positive((*_flow)[a]);
2643 2659
      }
2644 2660
    };
2645 2661

	
2646 2662
  }
2647 2663

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

	
2708 2724
    /// The type of the underlying digraph.
2709 2725
    typedef DGR Digraph;
2710 2726
    /// The type of the capacity map.
2711 2727
    typedef CM CapacityMap;
2712 2728
    /// The type of the flow map.
2713 2729
    typedef FM FlowMap;
2714 2730
    /// The tolerance type.
2715 2731
    typedef TL Tolerance;
2716 2732

	
2717 2733
    typedef typename CapacityMap::Value Value;
2718 2734
    typedef ResidualDigraph Adaptor;
2719 2735

	
2720 2736
  protected:
2721 2737

	
2722 2738
    typedef Undirector<const Digraph> Undirected;
2723 2739

	
2724 2740
    typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter;
2725 2741

	
2726 2742
    typedef _adaptor_bits::ResForwardFilter<const DGR, CM,
2727 2743
                                            FM, TL> ForwardFilter;
2728 2744

	
2729 2745
    typedef _adaptor_bits::ResBackwardFilter<const DGR, CM,
2730 2746
                                             FM, TL> BackwardFilter;
2731 2747

	
2732 2748
    typedef typename Undirected::
2733 2749
      template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2734 2750

	
2735 2751
    typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent;
2736 2752

	
2737 2753
    const CapacityMap* _capacity;
2738 2754
    FlowMap* _flow;
2739 2755

	
2740 2756
    Undirected _graph;
2741 2757
    NodeFilter _node_filter;
2742 2758
    ForwardFilter _forward_filter;
2743 2759
    BackwardFilter _backward_filter;
2744 2760
    ArcFilter _arc_filter;
2745 2761

	
2746 2762
  public:
2747 2763

	
2748 2764
    /// \brief Constructor
2749 2765
    ///
2750 2766
    /// Constructor of the residual digraph adaptor. The parameters are the
2751 2767
    /// digraph, the capacity map, the flow map, and a tolerance object.
2752 2768
    ResidualDigraph(const DGR& digraph, const CM& capacity,
2753 2769
                    FM& flow, const TL& tolerance = Tolerance())
2754 2770
      : Parent(), _capacity(&capacity), _flow(&flow), 
2755 2771
        _graph(digraph), _node_filter(),
2756 2772
        _forward_filter(capacity, flow, tolerance),
2757 2773
        _backward_filter(capacity, flow, tolerance),
2758 2774
        _arc_filter(_forward_filter, _backward_filter)
2759 2775
    {
2760 2776
      Parent::initialize(_graph, _node_filter, _arc_filter);
2761 2777
    }
2762 2778

	
2763 2779
    typedef typename Parent::Arc Arc;
2764 2780

	
2765 2781
    /// \brief Returns the residual capacity of the given arc.
2766 2782
    ///
2767 2783
    /// Returns the residual capacity of the given arc.
2768 2784
    Value residualCapacity(const Arc& a) const {
2769 2785
      if (Undirected::direction(a)) {
2770 2786
        return (*_capacity)[a] - (*_flow)[a];
2771 2787
      } else {
2772 2788
        return (*_flow)[a];
2773 2789
      }
2774 2790
    }
2775 2791

	
2776 2792
    /// \brief Augments on the given arc in the residual digraph.
2777 2793
    ///
2778 2794
    /// Augments on the given arc in the residual digraph. It increases
2779 2795
    /// or decreases the flow value on the original arc according to the
2780 2796
    /// direction of the residual arc.
2781 2797
    void augment(const Arc& a, const Value& v) const {
2782 2798
      if (Undirected::direction(a)) {
2783 2799
        _flow->set(a, (*_flow)[a] + v);
2784 2800
      } else {
2785 2801
        _flow->set(a, (*_flow)[a] - v);
2786 2802
      }
2787 2803
    }
2788 2804

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

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

	
2805 2821
    /// \brief Returns the forward oriented residual arc.
2806 2822
    ///
2807 2823
    /// Returns the forward oriented residual arc related to the given
2808 2824
    /// arc of the underlying digraph.
2809 2825
    static Arc forward(const typename Digraph::Arc& a) {
2810 2826
      return Undirected::direct(a, true);
2811 2827
    }
2812 2828

	
2813 2829
    /// \brief Returns the backward oriented residual arc.
2814 2830
    ///
2815 2831
    /// Returns the backward oriented residual arc related to the given
2816 2832
    /// arc of the underlying digraph.
2817 2833
    static Arc backward(const typename Digraph::Arc& a) {
2818 2834
      return Undirected::direct(a, false);
2819 2835
    }
2820 2836

	
2821 2837
    /// \brief Residual capacity map.
2822 2838
    ///
2823 2839
    /// This map adaptor class can be used for obtaining the residual
2824 2840
    /// capacities as an arc map of the residual digraph.
2825 2841
    /// Its value type is inherited from the capacity map.
2826 2842
    class ResidualCapacity {
2827 2843
    protected:
2828 2844
      const Adaptor* _adaptor;
2829 2845
    public:
2830 2846
      /// The key type of the map
2831 2847
      typedef Arc Key;
2832 2848
      /// The value type of the map
2833 2849
      typedef typename CapacityMap::Value Value;
2834 2850

	
2835 2851
      /// Constructor
2836 2852
      ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) 
2837 2853
        : _adaptor(&adaptor) {}
2838 2854

	
2839 2855
      /// Returns the value associated with the given residual arc
2840 2856
      Value operator[](const Arc& a) const {
2841 2857
        return _adaptor->residualCapacity(a);
2842 2858
      }
2843 2859

	
2844 2860
    };
2845 2861

	
2846 2862
    /// \brief Returns a residual capacity map
2847 2863
    ///
2848 2864
    /// This function just returns a residual capacity map.
2849 2865
    ResidualCapacity residualCapacity() const {
2850 2866
      return ResidualCapacity(*this);
2851 2867
    }
2852 2868

	
2853 2869
  };
2854 2870

	
2855 2871
  /// \brief Returns a (read-only) Residual adaptor
2856 2872
  ///
2857 2873
  /// This function just returns a (read-only) \ref ResidualDigraph adaptor.
2858 2874
  /// \ingroup graph_adaptors
2859 2875
  /// \relates ResidualDigraph
2860 2876
    template<typename DGR, typename CM, typename FM>
2861 2877
  ResidualDigraph<DGR, CM, FM>
2862 2878
  residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) {
2863 2879
    return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map);
2864 2880
  }
2865 2881

	
2866 2882

	
2867 2883
  template <typename DGR>
2868 2884
  class SplitNodesBase {
2885
    typedef DigraphAdaptorBase<const DGR> Parent;
2886

	
2869 2887
  public:
2870 2888

	
2871 2889
    typedef DGR Digraph;
2872
    typedef DigraphAdaptorBase<const DGR> Parent;
2873 2890
    typedef SplitNodesBase Adaptor;
2874 2891

	
2875 2892
    typedef typename DGR::Node DigraphNode;
2876 2893
    typedef typename DGR::Arc DigraphArc;
2877 2894

	
2878 2895
    class Node;
2879 2896
    class Arc;
2880 2897

	
2881 2898
  private:
2882 2899

	
2883 2900
    template <typename T> class NodeMapBase;
2884 2901
    template <typename T> class ArcMapBase;
2885 2902

	
2886 2903
  public:
2887 2904

	
2888 2905
    class Node : public DigraphNode {
2889 2906
      friend class SplitNodesBase;
2890 2907
      template <typename T> friend class NodeMapBase;
2891 2908
    private:
2892 2909

	
2893 2910
      bool _in;
2894 2911
      Node(DigraphNode node, bool in)
2895 2912
        : DigraphNode(node), _in(in) {}
2896 2913

	
2897 2914
    public:
2898 2915

	
2899 2916
      Node() {}
2900 2917
      Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2901 2918

	
2902 2919
      bool operator==(const Node& node) const {
2903 2920
        return DigraphNode::operator==(node) && _in == node._in;
2904 2921
      }
2905 2922

	
2906 2923
      bool operator!=(const Node& node) const {
2907 2924
        return !(*this == node);
2908 2925
      }
2909 2926

	
2910 2927
      bool operator<(const Node& node) const {
2911 2928
        return DigraphNode::operator<(node) ||
2912 2929
          (DigraphNode::operator==(node) && _in < node._in);
2913 2930
      }
2914 2931
    };
2915 2932

	
2916 2933
    class Arc {
2917 2934
      friend class SplitNodesBase;
2918 2935
      template <typename T> friend class ArcMapBase;
2919 2936
    private:
2920 2937
      typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2921 2938

	
2922 2939
      explicit Arc(const DigraphArc& arc) : _item(arc) {}
2923 2940
      explicit Arc(const DigraphNode& node) : _item(node) {}
2924 2941

	
2925 2942
      ArcImpl _item;
2926 2943

	
2927 2944
    public:
2928 2945
      Arc() {}
2929 2946
      Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2930 2947

	
2931 2948
      bool operator==(const Arc& arc) const {
2932 2949
        if (_item.firstState()) {
2933 2950
          if (arc._item.firstState()) {
2934 2951
            return _item.first() == arc._item.first();
2935 2952
          }
2936 2953
        } else {
2937 2954
          if (arc._item.secondState()) {
2938 2955
            return _item.second() == arc._item.second();
2939 2956
          }
2940 2957
        }
2941 2958
        return false;
2942 2959
      }
2943 2960

	
2944 2961
      bool operator!=(const Arc& arc) const {
2945 2962
        return !(*this == arc);
2946 2963
      }
2947 2964

	
2948 2965
      bool operator<(const Arc& arc) const {
2949 2966
        if (_item.firstState()) {
2950 2967
          if (arc._item.firstState()) {
2951 2968
            return _item.first() < arc._item.first();
2952 2969
          }
2953 2970
          return false;
2954 2971
        } else {
2955 2972
          if (arc._item.secondState()) {
2956 2973
            return _item.second() < arc._item.second();
2957 2974
          }
2958 2975
          return true;
2959 2976
        }
2960 2977
      }
2961 2978

	
2962 2979
      operator DigraphArc() const { return _item.first(); }
2963 2980
      operator DigraphNode() const { return _item.second(); }
2964 2981

	
2965 2982
    };
2966 2983

	
2967 2984
    void first(Node& n) const {
2968 2985
      _digraph->first(n);
2969 2986
      n._in = true;
2970 2987
    }
2971 2988

	
2972 2989
    void next(Node& n) const {
2973 2990
      if (n._in) {
2974 2991
        n._in = false;
2975 2992
      } else {
2976 2993
        n._in = true;
2977 2994
        _digraph->next(n);
2978 2995
      }
2979 2996
    }
2980 2997

	
2981 2998
    void first(Arc& e) const {
2982 2999
      e._item.setSecond();
2983 3000
      _digraph->first(e._item.second());
2984 3001
      if (e._item.second() == INVALID) {
2985 3002
        e._item.setFirst();
2986 3003
        _digraph->first(e._item.first());
2987 3004
      }
2988 3005
    }
2989 3006

	
2990 3007
    void next(Arc& e) const {
2991 3008
      if (e._item.secondState()) {
2992 3009
        _digraph->next(e._item.second());
2993 3010
        if (e._item.second() == INVALID) {
2994 3011
          e._item.setFirst();
2995 3012
          _digraph->first(e._item.first());
2996 3013
        }
2997 3014
      } else {
2998 3015
        _digraph->next(e._item.first());
2999 3016
      }
3000 3017
    }
3001 3018

	
3002 3019
    void firstOut(Arc& e, const Node& n) const {
3003 3020
      if (n._in) {
3004 3021
        e._item.setSecond(n);
3005 3022
      } else {
3006 3023
        e._item.setFirst();
3007 3024
        _digraph->firstOut(e._item.first(), n);
3008 3025
      }
3009 3026
    }
3010 3027

	
3011 3028
    void nextOut(Arc& e) const {
3012 3029
      if (!e._item.firstState()) {
3013 3030
        e._item.setFirst(INVALID);
3014 3031
      } else {
3015 3032
        _digraph->nextOut(e._item.first());
3016 3033
      }
3017 3034
    }
3018 3035

	
3019 3036
    void firstIn(Arc& e, const Node& n) const {
3020 3037
      if (!n._in) {
3021 3038
        e._item.setSecond(n);
3022 3039
      } else {
3023 3040
        e._item.setFirst();
3024 3041
        _digraph->firstIn(e._item.first(), n);
3025 3042
      }
3026 3043
    }
3027 3044

	
3028 3045
    void nextIn(Arc& e) const {
3029 3046
      if (!e._item.firstState()) {
3030 3047
        e._item.setFirst(INVALID);
3031 3048
      } else {
3032 3049
        _digraph->nextIn(e._item.first());
3033 3050
      }
3034 3051
    }
3035 3052

	
3036 3053
    Node source(const Arc& e) const {
3037 3054
      if (e._item.firstState()) {
3038 3055
        return Node(_digraph->source(e._item.first()), false);
3039 3056
      } else {
3040 3057
        return Node(e._item.second(), true);
3041 3058
      }
3042 3059
    }
3043 3060

	
3044 3061
    Node target(const Arc& e) const {
3045 3062
      if (e._item.firstState()) {
3046 3063
        return Node(_digraph->target(e._item.first()), true);
3047 3064
      } else {
3048 3065
        return Node(e._item.second(), false);
3049 3066
      }
3050 3067
    }
3051 3068

	
3052 3069
    int id(const Node& n) const {
3053 3070
      return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
3054 3071
    }
3055 3072
    Node nodeFromId(int ix) const {
3056 3073
      return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
3057 3074
    }
3058 3075
    int maxNodeId() const {
3059 3076
      return 2 * _digraph->maxNodeId() + 1;
3060 3077
    }
3061 3078

	
3062 3079
    int id(const Arc& e) const {
3063 3080
      if (e._item.firstState()) {
3064 3081
        return _digraph->id(e._item.first()) << 1;
3065 3082
      } else {
3066 3083
        return (_digraph->id(e._item.second()) << 1) | 1;
3067 3084
      }
3068 3085
    }
3069 3086
    Arc arcFromId(int ix) const {
3070 3087
      if ((ix & 1) == 0) {
3071 3088
        return Arc(_digraph->arcFromId(ix >> 1));
3072 3089
      } else {
3073 3090
        return Arc(_digraph->nodeFromId(ix >> 1));
3074 3091
      }
3075 3092
    }
3076 3093
    int maxArcId() const {
3077 3094
      return std::max(_digraph->maxNodeId() << 1,
3078 3095
                      (_digraph->maxArcId() << 1) | 1);
3079 3096
    }
3080 3097

	
3081 3098
    static bool inNode(const Node& n) {
3082 3099
      return n._in;
3083 3100
    }
3084 3101

	
3085 3102
    static bool outNode(const Node& n) {
3086 3103
      return !n._in;
3087 3104
    }
3088 3105

	
3089 3106
    static bool origArc(const Arc& e) {
3090 3107
      return e._item.firstState();
3091 3108
    }
3092 3109

	
3093 3110
    static bool bindArc(const Arc& e) {
3094 3111
      return e._item.secondState();
3095 3112
    }
3096 3113

	
3097 3114
    static Node inNode(const DigraphNode& n) {
3098 3115
      return Node(n, true);
3099 3116
    }
3100 3117

	
3101 3118
    static Node outNode(const DigraphNode& n) {
3102 3119
      return Node(n, false);
3103 3120
    }
3104 3121

	
3105 3122
    static Arc arc(const DigraphNode& n) {
3106 3123
      return Arc(n);
3107 3124
    }
3108 3125

	
3109 3126
    static Arc arc(const DigraphArc& e) {
3110 3127
      return Arc(e);
3111 3128
    }
3112 3129

	
3113 3130
    typedef True NodeNumTag;
3114 3131
    int nodeNum() const {
3115 3132
      return  2 * countNodes(*_digraph);
3116 3133
    }
3117 3134

	
3118 3135
    typedef True ArcNumTag;
3119 3136
    int arcNum() const {
3120 3137
      return countArcs(*_digraph) + countNodes(*_digraph);
3121 3138
    }
3122 3139

	
3123 3140
    typedef True FindArcTag;
3124 3141
    Arc findArc(const Node& u, const Node& v,
3125 3142
                const Arc& prev = INVALID) const {
3126 3143
      if (inNode(u) && outNode(v)) {
3127 3144
        if (static_cast<const DigraphNode&>(u) ==
3128 3145
            static_cast<const DigraphNode&>(v) && prev == INVALID) {
3129 3146
          return Arc(u);
3130 3147
        }
3131 3148
      }
3132 3149
      else if (outNode(u) && inNode(v)) {
3133 3150
        return Arc(::lemon::findArc(*_digraph, u, v, prev));
3134 3151
      }
3135 3152
      return INVALID;
3136 3153
    }
3137 3154

	
3138 3155
  private:
3139 3156

	
3140 3157
    template <typename V>
3141 3158
    class NodeMapBase
3142 3159
      : public MapTraits<typename Parent::template NodeMap<V> > {
3143 3160
      typedef typename Parent::template NodeMap<V> NodeImpl;
3144 3161
    public:
3145 3162
      typedef Node Key;
3146 3163
      typedef V Value;
3147 3164
      typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
3148 3165
      typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
3149 3166
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
3150 3167
      typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
3151 3168
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
3152 3169

	
3153 3170
      NodeMapBase(const SplitNodesBase<DGR>& adaptor)
3154 3171
        : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
3155 3172
      NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3156 3173
        : _in_map(*adaptor._digraph, value),
3157 3174
          _out_map(*adaptor._digraph, value) {}
3158 3175

	
3159 3176
      void set(const Node& key, const V& val) {
3160 3177
        if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); }
3161 3178
        else {_out_map.set(key, val); }
3162 3179
      }
3163 3180

	
3164 3181
      ReturnValue operator[](const Node& key) {
3165 3182
        if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; }
3166 3183
        else { return _out_map[key]; }
3167 3184
      }
3168 3185

	
3169 3186
      ConstReturnValue operator[](const Node& key) const {
3170 3187
        if (Adaptor::inNode(key)) { return _in_map[key]; }
3171 3188
        else { return _out_map[key]; }
3172 3189
      }
3173 3190

	
3174 3191
    private:
3175 3192
      NodeImpl _in_map, _out_map;
3176 3193
    };
3177 3194

	
3178 3195
    template <typename V>
3179 3196
    class ArcMapBase
3180 3197
      : public MapTraits<typename Parent::template ArcMap<V> > {
3181 3198
      typedef typename Parent::template ArcMap<V> ArcImpl;
3182 3199
      typedef typename Parent::template NodeMap<V> NodeImpl;
3183 3200
    public:
3184 3201
      typedef Arc Key;
3185 3202
      typedef V Value;
3186 3203
      typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
3187 3204
      typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
3188 3205
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
3189 3206
      typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
3190 3207
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
3191 3208

	
3192 3209
      ArcMapBase(const SplitNodesBase<DGR>& adaptor)
3193 3210
        : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
3194 3211
      ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3195 3212
        : _arc_map(*adaptor._digraph, value),
3196 3213
          _node_map(*adaptor._digraph, value) {}
3197 3214

	
3198 3215
      void set(const Arc& key, const V& val) {
3199 3216
        if (SplitNodesBase<DGR>::origArc(key)) {
3200 3217
          _arc_map.set(static_cast<const DigraphArc&>(key), val);
3201 3218
        } else {
3202 3219
          _node_map.set(static_cast<const DigraphNode&>(key), val);
3203 3220
        }
3204 3221
      }
3205 3222

	
3206 3223
      ReturnValue operator[](const Arc& key) {
3207 3224
        if (SplitNodesBase<DGR>::origArc(key)) {
3208 3225
          return _arc_map[static_cast<const DigraphArc&>(key)];
3209 3226
        } else {
3210 3227
          return _node_map[static_cast<const DigraphNode&>(key)];
3211 3228
        }
3212 3229
      }
3213 3230

	
3214 3231
      ConstReturnValue operator[](const Arc& key) const {
3215 3232
        if (SplitNodesBase<DGR>::origArc(key)) {
3216 3233
          return _arc_map[static_cast<const DigraphArc&>(key)];
3217 3234
        } else {
3218 3235
          return _node_map[static_cast<const DigraphNode&>(key)];
3219 3236
        }
3220 3237
      }
3221 3238

	
3222 3239
    private:
3223 3240
      ArcImpl _arc_map;
3224 3241
      NodeImpl _node_map;
3225 3242
    };
3226 3243

	
3227 3244
  public:
3228 3245

	
3229 3246
    template <typename V>
3230 3247
    class NodeMap
3231
      : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> >
3232
    {
3248
      : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > {
3249
      typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > Parent;
3250

	
3233 3251
    public:
3234 3252
      typedef V Value;
3235
      typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<Value> > Parent;
3236 3253

	
3237 3254
      NodeMap(const SplitNodesBase<DGR>& adaptor)
3238 3255
        : Parent(adaptor) {}
3239 3256

	
3240 3257
      NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3241 3258
        : Parent(adaptor, value) {}
3242 3259

	
3243 3260
    private:
3244 3261
      NodeMap& operator=(const NodeMap& cmap) {
3245 3262
        return operator=<NodeMap>(cmap);
3246 3263
      }
3247 3264

	
3248 3265
      template <typename CMap>
3249 3266
      NodeMap& operator=(const CMap& cmap) {
3250 3267
        Parent::operator=(cmap);
3251 3268
        return *this;
3252 3269
      }
3253 3270
    };
3254 3271

	
3255 3272
    template <typename V>
3256 3273
    class ArcMap
3257
      : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> >
3258
    {
3274
      : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > {
3275
      typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > Parent;
3276

	
3259 3277
    public:
3260 3278
      typedef V Value;
3261
      typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<Value> > Parent;
3262 3279

	
3263 3280
      ArcMap(const SplitNodesBase<DGR>& adaptor)
3264 3281
        : Parent(adaptor) {}
3265 3282

	
3266 3283
      ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3267 3284
        : Parent(adaptor, value) {}
3268 3285

	
3269 3286
    private:
3270 3287
      ArcMap& operator=(const ArcMap& cmap) {
3271 3288
        return operator=<ArcMap>(cmap);
3272 3289
      }
3273 3290

	
3274 3291
      template <typename CMap>
3275 3292
      ArcMap& operator=(const CMap& cmap) {
3276 3293
        Parent::operator=(cmap);
3277 3294
        return *this;
3278 3295
      }
3279 3296
    };
3280 3297

	
3281 3298
  protected:
3282 3299

	
3283 3300
    SplitNodesBase() : _digraph(0) {}
3284 3301

	
3285 3302
    DGR* _digraph;
3286 3303

	
3287 3304
    void initialize(Digraph& digraph) {
3288 3305
      _digraph = &digraph;
3289 3306
    }
3290 3307

	
3291 3308
  };
3292 3309

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

	
3327 3346
  public:
3328 3347
    typedef DGR Digraph;
3329
    typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent;
3330 3348

	
3331 3349
    typedef typename DGR::Node DigraphNode;
3332 3350
    typedef typename DGR::Arc DigraphArc;
3333 3351

	
3334 3352
    typedef typename Parent::Node Node;
3335 3353
    typedef typename Parent::Arc Arc;
3336 3354

	
3337 3355
    /// \brief Constructor
3338 3356
    ///
3339 3357
    /// Constructor of the adaptor.
3340 3358
    SplitNodes(const DGR& g) {
3341 3359
      Parent::initialize(g);
3342 3360
    }
3343 3361

	
3344 3362
    /// \brief Returns \c true if the given node is an in-node.
3345 3363
    ///
3346 3364
    /// Returns \c true if the given node is an in-node.
3347 3365
    static bool inNode(const Node& n) {
3348 3366
      return Parent::inNode(n);
3349 3367
    }
3350 3368

	
3351 3369
    /// \brief Returns \c true if the given node is an out-node.
3352 3370
    ///
3353 3371
    /// Returns \c true if the given node is an out-node.
3354 3372
    static bool outNode(const Node& n) {
3355 3373
      return Parent::outNode(n);
3356 3374
    }
3357 3375

	
3358 3376
    /// \brief Returns \c true if the given arc is an original arc.
3359 3377
    ///
3360 3378
    /// Returns \c true if the given arc is one of the arcs in the
3361 3379
    /// original digraph.
3362 3380
    static bool origArc(const Arc& a) {
3363 3381
      return Parent::origArc(a);
3364 3382
    }
3365 3383

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

	
3374 3392
    /// \brief Returns the in-node created from the given original node.
3375 3393
    ///
3376 3394
    /// Returns the in-node created from the given original node.
3377 3395
    static Node inNode(const DigraphNode& n) {
3378 3396
      return Parent::inNode(n);
3379 3397
    }
3380 3398

	
3381 3399
    /// \brief Returns the out-node created from the given original node.
3382 3400
    ///
3383 3401
    /// Returns the out-node created from the given original node.
3384 3402
    static Node outNode(const DigraphNode& n) {
3385 3403
      return Parent::outNode(n);
3386 3404
    }
3387 3405

	
3388 3406
    /// \brief Returns the bind arc that corresponds to the given
3389 3407
    /// original node.
3390 3408
    ///
3391 3409
    /// Returns the bind arc in the adaptor that corresponds to the given
3392 3410
    /// original node, i.e. the arc connecting the in-node and out-node
3393 3411
    /// of \c n.
3394 3412
    static Arc arc(const DigraphNode& n) {
3395 3413
      return Parent::arc(n);
3396 3414
    }
3397 3415

	
3398 3416
    /// \brief Returns the arc that corresponds to the given original arc.
3399 3417
    ///
3400 3418
    /// Returns the arc in the adaptor that corresponds to the given
3401 3419
    /// original arc.
3402 3420
    static Arc arc(const DigraphArc& a) {
3403 3421
      return Parent::arc(a);
3404 3422
    }
3405 3423

	
3406 3424
    /// \brief Node map combined from two original node maps
3407 3425
    ///
3408 3426
    /// This map adaptor class adapts two node maps of the original digraph
3409 3427
    /// to get a node map of the split digraph.
3410 3428
    /// Its value type is inherited from the first node map type (\c IN).
3411 3429
    /// \tparam IN The type of the node map for the in-nodes. 
3412 3430
    /// \tparam OUT The type of the node map for the out-nodes.
3413 3431
    template <typename IN, typename OUT>
3414 3432
    class CombinedNodeMap {
3415 3433
    public:
3416 3434

	
3417 3435
      /// The key type of the map
3418 3436
      typedef Node Key;
3419 3437
      /// The value type of the map
3420 3438
      typedef typename IN::Value Value;
3421 3439

	
3422 3440
      typedef typename MapTraits<IN>::ReferenceMapTag ReferenceMapTag;
3423 3441
      typedef typename MapTraits<IN>::ReturnValue ReturnValue;
3424 3442
      typedef typename MapTraits<IN>::ConstReturnValue ConstReturnValue;
3425 3443
      typedef typename MapTraits<IN>::ReturnValue Reference;
3426 3444
      typedef typename MapTraits<IN>::ConstReturnValue ConstReference;
3427 3445

	
3428 3446
      /// Constructor
3429 3447
      CombinedNodeMap(IN& in_map, OUT& out_map)
3430 3448
        : _in_map(in_map), _out_map(out_map) {}
3431 3449

	
3432 3450
      /// Returns the value associated with the given key.
3433 3451
      Value operator[](const Key& key) const {
3434 3452
        if (SplitNodesBase<const DGR>::inNode(key)) {
3435 3453
          return _in_map[key];
3436 3454
        } else {
3437 3455
          return _out_map[key];
3438 3456
        }
3439 3457
      }
3440 3458

	
3441 3459
      /// Returns a reference to the value associated with the given key.
3442 3460
      Value& operator[](const Key& key) {
3443 3461
        if (SplitNodesBase<const DGR>::inNode(key)) {
3444 3462
          return _in_map[key];
3445 3463
        } else {
3446 3464
          return _out_map[key];
3447 3465
        }
3448 3466
      }
3449 3467

	
3450 3468
      /// Sets the value associated with the given key.
3451 3469
      void set(const Key& key, const Value& value) {
3452 3470
        if (SplitNodesBase<const DGR>::inNode(key)) {
3453 3471
          _in_map.set(key, value);
3454 3472
        } else {
3455 3473
          _out_map.set(key, value);
3456 3474
        }
3457 3475
      }
3458 3476

	
3459 3477
    private:
3460 3478

	
3461 3479
      IN& _in_map;
3462 3480
      OUT& _out_map;
3463 3481

	
3464 3482
    };
3465 3483

	
3466 3484

	
3467 3485
    /// \brief Returns a combined node map
3468 3486
    ///
3469 3487
    /// This function just returns a combined node map.
3470 3488
    template <typename IN, typename OUT>
3471 3489
    static CombinedNodeMap<IN, OUT>
3472 3490
    combinedNodeMap(IN& in_map, OUT& out_map) {
3473 3491
      return CombinedNodeMap<IN, OUT>(in_map, out_map);
3474 3492
    }
3475 3493

	
3476 3494
    template <typename IN, typename OUT>
3477 3495
    static CombinedNodeMap<const IN, OUT>
3478 3496
    combinedNodeMap(const IN& in_map, OUT& out_map) {
3479 3497
      return CombinedNodeMap<const IN, OUT>(in_map, out_map);
3480 3498
    }
3481 3499

	
3482 3500
    template <typename IN, typename OUT>
3483 3501
    static CombinedNodeMap<IN, const OUT>
3484 3502
    combinedNodeMap(IN& in_map, const OUT& out_map) {
3485 3503
      return CombinedNodeMap<IN, const OUT>(in_map, out_map);
3486 3504
    }
3487 3505

	
3488 3506
    template <typename IN, typename OUT>
3489 3507
    static CombinedNodeMap<const IN, const OUT>
3490 3508
    combinedNodeMap(const IN& in_map, const OUT& out_map) {
3491 3509
      return CombinedNodeMap<const IN, const OUT>(in_map, out_map);
3492 3510
    }
3493 3511

	
3494 3512
    /// \brief Arc map combined from an arc map and a node map of the
3495 3513
    /// original digraph.
3496 3514
    ///
3497 3515
    /// This map adaptor class adapts an arc map and a node map of the
3498 3516
    /// original digraph to get an arc map of the split digraph.
3499 3517
    /// Its value type is inherited from the original arc map type (\c AM).
3500 3518
    /// \tparam AM The type of the arc map.
3501 3519
    /// \tparam NM the type of the node map.
3502 3520
    template <typename AM, typename NM>
3503 3521
    class CombinedArcMap {
3504 3522
    public:
3505 3523

	
3506 3524
      /// The key type of the map
3507 3525
      typedef Arc Key;
3508 3526
      /// The value type of the map
3509 3527
      typedef typename AM::Value Value;
3510 3528

	
3511 3529
      typedef typename MapTraits<AM>::ReferenceMapTag ReferenceMapTag;
3512 3530
      typedef typename MapTraits<AM>::ReturnValue ReturnValue;
3513 3531
      typedef typename MapTraits<AM>::ConstReturnValue ConstReturnValue;
3514 3532
      typedef typename MapTraits<AM>::ReturnValue Reference;
3515 3533
      typedef typename MapTraits<AM>::ConstReturnValue ConstReference;
3516 3534

	
3517 3535
      /// Constructor
3518 3536
      CombinedArcMap(AM& arc_map, NM& node_map)
3519 3537
        : _arc_map(arc_map), _node_map(node_map) {}
3520 3538

	
3521 3539
      /// Returns the value associated with the given key.
3522 3540
      Value operator[](const Key& arc) const {
3523 3541
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3524 3542
          return _arc_map[arc];
3525 3543
        } else {
3526 3544
          return _node_map[arc];
3527 3545
        }
3528 3546
      }
3529 3547

	
3530 3548
      /// Returns a reference to the value associated with the given key.
3531 3549
      Value& operator[](const Key& arc) {
3532 3550
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3533 3551
          return _arc_map[arc];
3534 3552
        } else {
3535 3553
          return _node_map[arc];
3536 3554
        }
3537 3555
      }
3538 3556

	
3539 3557
      /// Sets the value associated with the given key.
3540 3558
      void set(const Arc& arc, const Value& val) {
3541 3559
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3542 3560
          _arc_map.set(arc, val);
3543 3561
        } else {
3544 3562
          _node_map.set(arc, val);
3545 3563
        }
3546 3564
      }
3547 3565

	
3548 3566
    private:
3549 3567

	
3550 3568
      AM& _arc_map;
3551 3569
      NM& _node_map;
3552 3570

	
3553 3571
    };
3554 3572

	
3555 3573
    /// \brief Returns a combined arc map
3556 3574
    ///
3557 3575
    /// This function just returns a combined arc map.
3558 3576
    template <typename ArcMap, typename NodeMap>
3559 3577
    static CombinedArcMap<ArcMap, NodeMap>
3560 3578
    combinedArcMap(ArcMap& arc_map, NodeMap& node_map) {
3561 3579
      return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map);
3562 3580
    }
3563 3581

	
3564 3582
    template <typename ArcMap, typename NodeMap>
3565 3583
    static CombinedArcMap<const ArcMap, NodeMap>
3566 3584
    combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) {
3567 3585
      return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map);
3568 3586
    }
3569 3587

	
3570 3588
    template <typename ArcMap, typename NodeMap>
3571 3589
    static CombinedArcMap<ArcMap, const NodeMap>
3572 3590
    combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) {
3573 3591
      return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map);
3574 3592
    }
3575 3593

	
3576 3594
    template <typename ArcMap, typename NodeMap>
3577 3595
    static CombinedArcMap<const ArcMap, const NodeMap>
3578 3596
    combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) {
3579 3597
      return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map);
3580 3598
    }
3581 3599

	
3582 3600
  };
3583 3601

	
3584 3602
  /// \brief Returns a (read-only) SplitNodes adaptor
3585 3603
  ///
3586 3604
  /// This function just returns a (read-only) \ref SplitNodes adaptor.
3587 3605
  /// \ingroup graph_adaptors
3588 3606
  /// \relates SplitNodes
3589 3607
  template<typename DGR>
3590 3608
  SplitNodes<DGR>
3591 3609
  splitNodes(const DGR& digraph) {
3592 3610
    return SplitNodes<DGR>(digraph);
3593 3611
  }
3594 3612

	
3595 3613
#undef LEMON_SCOPE_FIX
3596 3614

	
3597 3615
} //namespace lemon
3598 3616

	
3599 3617
#endif //LEMON_ADAPTORS_H
Ignore white space 131072 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_ARRAY_MAP_H
20 20
#define LEMON_BITS_ARRAY_MAP_H
21 21

	
22 22
#include <memory>
23 23

	
24 24
#include <lemon/bits/traits.h>
25 25
#include <lemon/bits/alteration_notifier.h>
26 26
#include <lemon/concept_check.h>
27 27
#include <lemon/concepts/maps.h>
28 28

	
29 29
// \ingroup graphbits
30 30
// \file
31 31
// \brief Graph map based on the array storage.
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  // \ingroup graphbits
36 36
  //
37 37
  // \brief Graph map based on the array storage.
38 38
  //
39 39
  // The ArrayMap template class is graph map structure that automatically
40 40
  // updates the map when a key is added to or erased from the graph.
41 41
  // This map uses the allocators to implement the container functionality.
42 42
  //
43 43
  // The template parameters are the Graph, the current Item type and
44 44
  // the Value type of the map.
45 45
  template <typename _Graph, typename _Item, typename _Value>
46 46
  class ArrayMap
47 47
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
48 48
  public:
49 49
    // The graph type.
50
    typedef _Graph Graph;
50
    typedef _Graph GraphType;
51 51
    // The item type.
52 52
    typedef _Item Item;
53 53
    // The reference map tag.
54 54
    typedef True ReferenceMapTag;
55 55

	
56 56
    // The key type of the map.
57 57
    typedef _Item Key;
58 58
    // The value type of the map.
59 59
    typedef _Value Value;
60 60

	
61 61
    // The const reference type of the map.
62 62
    typedef const _Value& ConstReference;
63 63
    // The reference type of the map.
64 64
    typedef _Value& Reference;
65 65

	
66
    // The map type.
67
    typedef ArrayMap Map;
68

	
66 69
    // The notifier type.
67 70
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
68 71

	
72
  private:
73
  
69 74
    // The MapBase of the Map which imlements the core regisitry function.
70 75
    typedef typename Notifier::ObserverBase Parent;
71 76

	
72
  private:
73 77
    typedef std::allocator<Value> Allocator;
74 78

	
75 79
  public:
76 80

	
77 81
    // \brief Graph initialized map constructor.
78 82
    //
79 83
    // Graph initialized map constructor.
80
    explicit ArrayMap(const Graph& graph) {
84
    explicit ArrayMap(const GraphType& graph) {
81 85
      Parent::attach(graph.notifier(Item()));
82 86
      allocate_memory();
83 87
      Notifier* nf = Parent::notifier();
84 88
      Item it;
85 89
      for (nf->first(it); it != INVALID; nf->next(it)) {
86 90
        int id = nf->id(it);;
87 91
        allocator.construct(&(values[id]), Value());
88 92
      }
89 93
    }
90 94

	
91 95
    // \brief Constructor to use default value to initialize the map.
92 96
    //
93 97
    // It constructs a map and initialize all of the the map.
94
    ArrayMap(const Graph& graph, const Value& value) {
98
    ArrayMap(const GraphType& graph, const Value& value) {
95 99
      Parent::attach(graph.notifier(Item()));
96 100
      allocate_memory();
97 101
      Notifier* nf = Parent::notifier();
98 102
      Item it;
99 103
      for (nf->first(it); it != INVALID; nf->next(it)) {
100 104
        int id = nf->id(it);;
101 105
        allocator.construct(&(values[id]), value);
102 106
      }
103 107
    }
104 108

	
105 109
  private:
106 110
    // \brief Constructor to copy a map of the same map type.
107 111
    //
108 112
    // Constructor to copy a map of the same map type.
109 113
    ArrayMap(const ArrayMap& copy) : Parent() {
110 114
      if (copy.attached()) {
111 115
        attach(*copy.notifier());
112 116
      }
113 117
      capacity = copy.capacity;
114 118
      if (capacity == 0) return;
115 119
      values = allocator.allocate(capacity);
116 120
      Notifier* nf = Parent::notifier();
117 121
      Item it;
118 122
      for (nf->first(it); it != INVALID; nf->next(it)) {
119 123
        int id = nf->id(it);;
120 124
        allocator.construct(&(values[id]), copy.values[id]);
121 125
      }
122 126
    }
123 127

	
124 128
    // \brief Assign operator.
125 129
    //
126 130
    // This operator assigns for each item in the map the
127 131
    // value mapped to the same item in the copied map.
128 132
    // The parameter map should be indiced with the same
129 133
    // itemset because this assign operator does not change
130 134
    // the container of the map.
131 135
    ArrayMap& operator=(const ArrayMap& cmap) {
132 136
      return operator=<ArrayMap>(cmap);
133 137
    }
134 138

	
135 139

	
136 140
    // \brief Template assign operator.
137 141
    //
138 142
    // The given parameter should conform to the ReadMap
139 143
    // concecpt and could be indiced by the current item set of
140 144
    // the NodeMap. In this case the value for each item
141 145
    // is assigned by the value of the given ReadMap.
142 146
    template <typename CMap>
143 147
    ArrayMap& operator=(const CMap& cmap) {
144 148
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
145 149
      const typename Parent::Notifier* nf = Parent::notifier();
146 150
      Item it;
147 151
      for (nf->first(it); it != INVALID; nf->next(it)) {
148 152
        set(it, cmap[it]);
149 153
      }
150 154
      return *this;
151 155
    }
152 156

	
153 157
  public:
154 158
    // \brief The destructor of the map.
155 159
    //
156 160
    // The destructor of the map.
157 161
    virtual ~ArrayMap() {
158 162
      if (attached()) {
159 163
        clear();
160 164
        detach();
161 165
      }
162 166
    }
163 167

	
164 168
  protected:
165 169

	
166 170
    using Parent::attach;
167 171
    using Parent::detach;
168 172
    using Parent::attached;
169 173

	
170 174
  public:
171 175

	
172 176
    // \brief The subscript operator.
173 177
    //
174 178
    // The subscript operator. The map can be subscripted by the
175 179
    // actual keys of the graph.
176 180
    Value& operator[](const Key& key) {
177 181
      int id = Parent::notifier()->id(key);
178 182
      return values[id];
179 183
    }
180 184

	
181 185
    // \brief The const subscript operator.
182 186
    //
183 187
    // The const subscript operator. The map can be subscripted by the
184 188
    // actual keys of the graph.
185 189
    const Value& operator[](const Key& key) const {
186 190
      int id = Parent::notifier()->id(key);
187 191
      return values[id];
188 192
    }
189 193

	
190 194
    // \brief Setter function of the map.
191 195
    //
192 196
    // Setter function of the map. Equivalent with map[key] = val.
193 197
    // This is a compatibility feature with the not dereferable maps.
194 198
    void set(const Key& key, const Value& val) {
195 199
      (*this)[key] = val;
196 200
    }
197 201

	
198 202
  protected:
199 203

	
200 204
    // \brief Adds a new key to the map.
201 205
    //
202 206
    // It adds a new key to the map. It is called by the observer notifier
203 207
    // and it overrides the add() member function of the observer base.
204 208
    virtual void add(const Key& key) {
205 209
      Notifier* nf = Parent::notifier();
206 210
      int id = nf->id(key);
207 211
      if (id >= capacity) {
208 212
        int new_capacity = (capacity == 0 ? 1 : capacity);
209 213
        while (new_capacity <= id) {
210 214
          new_capacity <<= 1;
211 215
        }
212 216
        Value* new_values = allocator.allocate(new_capacity);
213 217
        Item it;
214 218
        for (nf->first(it); it != INVALID; nf->next(it)) {
215 219
          int jd = nf->id(it);;
216 220
          if (id != jd) {
217 221
            allocator.construct(&(new_values[jd]), values[jd]);
218 222
            allocator.destroy(&(values[jd]));
219 223
          }
220 224
        }
221 225
        if (capacity != 0) allocator.deallocate(values, capacity);
222 226
        values = new_values;
223 227
        capacity = new_capacity;
224 228
      }
225 229
      allocator.construct(&(values[id]), Value());
226 230
    }
227 231

	
228 232
    // \brief Adds more new keys to the map.
229 233
    //
230 234
    // It adds more new keys to the map. It is called by the observer notifier
231 235
    // and it overrides the add() member function of the observer base.
232 236
    virtual void add(const std::vector<Key>& keys) {
233 237
      Notifier* nf = Parent::notifier();
234 238
      int max_id = -1;
235 239
      for (int i = 0; i < int(keys.size()); ++i) {
236 240
        int id = nf->id(keys[i]);
237 241
        if (id > max_id) {
238 242
          max_id = id;
239 243
        }
240 244
      }
241 245
      if (max_id >= capacity) {
242 246
        int new_capacity = (capacity == 0 ? 1 : capacity);
243 247
        while (new_capacity <= max_id) {
244 248
          new_capacity <<= 1;
245 249
        }
246 250
        Value* new_values = allocator.allocate(new_capacity);
247 251
        Item it;
248 252
        for (nf->first(it); it != INVALID; nf->next(it)) {
249 253
          int id = nf->id(it);
250 254
          bool found = false;
251 255
          for (int i = 0; i < int(keys.size()); ++i) {
252 256
            int jd = nf->id(keys[i]);
253 257
            if (id == jd) {
254 258
              found = true;
255 259
              break;
256 260
            }
257 261
          }
258 262
          if (found) continue;
259 263
          allocator.construct(&(new_values[id]), values[id]);
260 264
          allocator.destroy(&(values[id]));
261 265
        }
262 266
        if (capacity != 0) allocator.deallocate(values, capacity);
263 267
        values = new_values;
264 268
        capacity = new_capacity;
265 269
      }
266 270
      for (int i = 0; i < int(keys.size()); ++i) {
267 271
        int id = nf->id(keys[i]);
268 272
        allocator.construct(&(values[id]), Value());
269 273
      }
270 274
    }
271 275

	
272 276
    // \brief Erase a key from the map.
273 277
    //
274 278
    // Erase a key from the map. It is called by the observer notifier
275 279
    // and it overrides the erase() member function of the observer base.
276 280
    virtual void erase(const Key& key) {
277 281
      int id = Parent::notifier()->id(key);
278 282
      allocator.destroy(&(values[id]));
279 283
    }
280 284

	
281 285
    // \brief Erase more keys from the map.
282 286
    //
283 287
    // Erase more keys from the map. It is called by the observer notifier
284 288
    // and it overrides the erase() member function of the observer base.
285 289
    virtual void erase(const std::vector<Key>& keys) {
286 290
      for (int i = 0; i < int(keys.size()); ++i) {
287 291
        int id = Parent::notifier()->id(keys[i]);
288 292
        allocator.destroy(&(values[id]));
289 293
      }
290 294
    }
291 295

	
292 296
    // \brief Builds the map.
293 297
    //
294 298
    // It builds the map. It is called by the observer notifier
295 299
    // and it overrides the build() member function of the observer base.
296 300
    virtual void build() {
297 301
      Notifier* nf = Parent::notifier();
298 302
      allocate_memory();
299 303
      Item it;
300 304
      for (nf->first(it); it != INVALID; nf->next(it)) {
301 305
        int id = nf->id(it);;
302 306
        allocator.construct(&(values[id]), Value());
303 307
      }
304 308
    }
305 309

	
306 310
    // \brief Clear the map.
307 311
    //
308 312
    // It erase all items from the map. It is called by the observer notifier
309 313
    // and it overrides the clear() member function of the observer base.
310 314
    virtual void clear() {
311 315
      Notifier* nf = Parent::notifier();
312 316
      if (capacity != 0) {
313 317
        Item it;
314 318
        for (nf->first(it); it != INVALID; nf->next(it)) {
315 319
          int id = nf->id(it);
316 320
          allocator.destroy(&(values[id]));
317 321
        }
318 322
        allocator.deallocate(values, capacity);
319 323
        capacity = 0;
320 324
      }
321 325
    }
322 326

	
323 327
  private:
324 328

	
325 329
    void allocate_memory() {
326 330
      int max_id = Parent::notifier()->maxId();
327 331
      if (max_id == -1) {
328 332
        capacity = 0;
329 333
        values = 0;
330 334
        return;
331 335
      }
332 336
      capacity = 1;
333 337
      while (capacity <= max_id) {
334 338
        capacity <<= 1;
335 339
      }
336 340
      values = allocator.allocate(capacity);
337 341
    }
338 342

	
339 343
    int capacity;
340 344
    Value* values;
341 345
    Allocator allocator;
342 346

	
343 347
  };
344 348

	
345 349
}
346 350

	
347 351
#endif
Ignore white space 131072 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_BASE_EXTENDER_H
20 20
#define LEMON_BITS_BASE_EXTENDER_H
21 21

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

	
25 25
#include <lemon/bits/map_extender.h>
26 26
#include <lemon/bits/default_map.h>
27 27

	
28 28
#include <lemon/concept_check.h>
29 29
#include <lemon/concepts/maps.h>
30 30

	
31 31
//\ingroup digraphbits
32 32
//\file
33 33
//\brief Extenders for the graph types
34 34
namespace lemon {
35 35

	
36 36
  // \ingroup digraphbits
37 37
  //
38 38
  // \brief BaseDigraph to BaseGraph extender
39 39
  template <typename Base>
40 40
  class UndirDigraphExtender : public Base {
41
    typedef Base Parent;
41 42

	
42 43
  public:
43 44

	
44
    typedef Base Parent;
45 45
    typedef typename Parent::Arc Edge;
46 46
    typedef typename Parent::Node Node;
47 47

	
48 48
    typedef True UndirectedTag;
49 49

	
50 50
    class Arc : public Edge {
51 51
      friend class UndirDigraphExtender;
52 52

	
53 53
    protected:
54 54
      bool forward;
55 55

	
56 56
      Arc(const Edge &ue, bool _forward) :
57 57
        Edge(ue), forward(_forward) {}
58 58

	
59 59
    public:
60 60
      Arc() {}
61 61

	
62 62
      // Invalid arc constructor
63 63
      Arc(Invalid i) : Edge(i), forward(true) {}
64 64

	
65 65
      bool operator==(const Arc &that) const {
66 66
        return forward==that.forward && Edge(*this)==Edge(that);
67 67
      }
68 68
      bool operator!=(const Arc &that) const {
69 69
        return forward!=that.forward || Edge(*this)!=Edge(that);
70 70
      }
71 71
      bool operator<(const Arc &that) const {
72 72
        return forward<that.forward ||
73 73
          (!(that.forward<forward) && Edge(*this)<Edge(that));
74 74
      }
75 75
    };
76 76

	
77 77
    // First node of the edge
78 78
    Node u(const Edge &e) const {
79 79
      return Parent::source(e);
80 80
    }
81 81

	
82 82
    // Source of the given arc
83 83
    Node source(const Arc &e) const {
84 84
      return e.forward ? Parent::source(e) : Parent::target(e);
85 85
    }
86 86

	
87 87
    // Second node of the edge
88 88
    Node v(const Edge &e) const {
89 89
      return Parent::target(e);
90 90
    }
91 91

	
92 92
    // Target of the given arc
93 93
    Node target(const Arc &e) const {
94 94
      return e.forward ? Parent::target(e) : Parent::source(e);
95 95
    }
96 96

	
97 97
    // \brief Directed arc from an edge.
98 98
    //
99 99
    // Returns a directed arc corresponding to the specified edge.
100 100
    // If the given bool is true, the first node of the given edge and
101 101
    // the source node of the returned arc are the same.
102 102
    static Arc direct(const Edge &e, bool d) {
103 103
      return Arc(e, d);
104 104
    }
105 105

	
106 106
    // Returns whether the given directed arc has the same orientation
107 107
    // as the corresponding edge.
108 108
    static bool direction(const Arc &a) { return a.forward; }
109 109

	
110 110
    using Parent::first;
111 111
    using Parent::next;
112 112

	
113 113
    void first(Arc &e) const {
114 114
      Parent::first(e);
115 115
      e.forward=true;
116 116
    }
117 117

	
118 118
    void next(Arc &e) const {
119 119
      if( e.forward ) {
120 120
        e.forward = false;
121 121
      }
122 122
      else {
123 123
        Parent::next(e);
124 124
        e.forward = true;
125 125
      }
126 126
    }
127 127

	
128 128
    void firstOut(Arc &e, const Node &n) const {
129 129
      Parent::firstIn(e,n);
130 130
      if( Edge(e) != INVALID ) {
131 131
        e.forward = false;
132 132
      }
133 133
      else {
134 134
        Parent::firstOut(e,n);
135 135
        e.forward = true;
136 136
      }
137 137
    }
138 138
    void nextOut(Arc &e) const {
139 139
      if( ! e.forward ) {
140 140
        Node n = Parent::target(e);
141 141
        Parent::nextIn(e);
142 142
        if( Edge(e) == INVALID ) {
143 143
          Parent::firstOut(e, n);
144 144
          e.forward = true;
145 145
        }
146 146
      }
147 147
      else {
148 148
        Parent::nextOut(e);
149 149
      }
150 150
    }
151 151

	
152 152
    void firstIn(Arc &e, const Node &n) const {
153 153
      Parent::firstOut(e,n);
154 154
      if( Edge(e) != INVALID ) {
155 155
        e.forward = false;
156 156
      }
157 157
      else {
158 158
        Parent::firstIn(e,n);
159 159
        e.forward = true;
160 160
      }
161 161
    }
162 162
    void nextIn(Arc &e) const {
163 163
      if( ! e.forward ) {
164 164
        Node n = Parent::source(e);
165 165
        Parent::nextOut(e);
166 166
        if( Edge(e) == INVALID ) {
167 167
          Parent::firstIn(e, n);
168 168
          e.forward = true;
169 169
        }
170 170
      }
171 171
      else {
172 172
        Parent::nextIn(e);
173 173
      }
174 174
    }
175 175

	
176 176
    void firstInc(Edge &e, bool &d, const Node &n) const {
177 177
      d = true;
178 178
      Parent::firstOut(e, n);
179 179
      if (e != INVALID) return;
180 180
      d = false;
181 181
      Parent::firstIn(e, n);
182 182
    }
183 183

	
184 184
    void nextInc(Edge &e, bool &d) const {
185 185
      if (d) {
186 186
        Node s = Parent::source(e);
187 187
        Parent::nextOut(e);
188 188
        if (e != INVALID) return;
189 189
        d = false;
190 190
        Parent::firstIn(e, s);
191 191
      } else {
192 192
        Parent::nextIn(e);
193 193
      }
194 194
    }
195 195

	
196 196
    Node nodeFromId(int ix) const {
197 197
      return Parent::nodeFromId(ix);
198 198
    }
199 199

	
200 200
    Arc arcFromId(int ix) const {
201 201
      return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
202 202
    }
203 203

	
204 204
    Edge edgeFromId(int ix) const {
205 205
      return Parent::arcFromId(ix);
206 206
    }
207 207

	
208 208
    int id(const Node &n) const {
209 209
      return Parent::id(n);
210 210
    }
211 211

	
212 212
    int id(const Edge &e) const {
213 213
      return Parent::id(e);
214 214
    }
215 215

	
216 216
    int id(const Arc &e) const {
217 217
      return 2 * Parent::id(e) + int(e.forward);
218 218
    }
219 219

	
220 220
    int maxNodeId() const {
221 221
      return Parent::maxNodeId();
222 222
    }
223 223

	
224 224
    int maxArcId() const {
225 225
      return 2 * Parent::maxArcId() + 1;
226 226
    }
227 227

	
228 228
    int maxEdgeId() const {
229 229
      return Parent::maxArcId();
230 230
    }
231 231

	
232 232
    int arcNum() const {
233 233
      return 2 * Parent::arcNum();
234 234
    }
235 235

	
236 236
    int edgeNum() const {
237 237
      return Parent::arcNum();
238 238
    }
239 239

	
240 240
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
241 241
      if (p == INVALID) {
242 242
        Edge arc = Parent::findArc(s, t);
243 243
        if (arc != INVALID) return direct(arc, true);
244 244
        arc = Parent::findArc(t, s);
245 245
        if (arc != INVALID) return direct(arc, false);
246 246
      } else if (direction(p)) {
247 247
        Edge arc = Parent::findArc(s, t, p);
248 248
        if (arc != INVALID) return direct(arc, true);
249 249
        arc = Parent::findArc(t, s);
250 250
        if (arc != INVALID) return direct(arc, false);
251 251
      } else {
252 252
        Edge arc = Parent::findArc(t, s, p);
253 253
        if (arc != INVALID) return direct(arc, false);
254 254
      }
255 255
      return INVALID;
256 256
    }
257 257

	
258 258
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
259 259
      if (s != t) {
260 260
        if (p == INVALID) {
261 261
          Edge arc = Parent::findArc(s, t);
262 262
          if (arc != INVALID) return arc;
263 263
          arc = Parent::findArc(t, s);
264 264
          if (arc != INVALID) return arc;
265 265
        } else if (Parent::s(p) == s) {
266 266
          Edge arc = Parent::findArc(s, t, p);
267 267
          if (arc != INVALID) return arc;
268 268
          arc = Parent::findArc(t, s);
269 269
          if (arc != INVALID) return arc;
270 270
        } else {
271 271
          Edge arc = Parent::findArc(t, s, p);
272 272
          if (arc != INVALID) return arc;
273 273
        }
274 274
      } else {
275 275
        return Parent::findArc(s, t, p);
276 276
      }
277 277
      return INVALID;
278 278
    }
279 279
  };
280 280

	
281 281
  template <typename Base>
282 282
  class BidirBpGraphExtender : public Base {
283
    typedef Base Parent;
284

	
283 285
  public:
284
    typedef Base Parent;
285 286
    typedef BidirBpGraphExtender Digraph;
286 287

	
287 288
    typedef typename Parent::Node Node;
288 289
    typedef typename Parent::Edge Edge;
289 290

	
290 291

	
291 292
    using Parent::first;
292 293
    using Parent::next;
293 294

	
294 295
    using Parent::id;
295 296

	
296 297
    class Red : public Node {
297 298
      friend class BidirBpGraphExtender;
298 299
    public:
299 300
      Red() {}
300 301
      Red(const Node& node) : Node(node) {
301 302
        LEMON_DEBUG(Parent::red(node) || node == INVALID,
302 303
                    typename Parent::NodeSetError());
303 304
      }
304 305
      Red& operator=(const Node& node) {
305 306
        LEMON_DEBUG(Parent::red(node) || node == INVALID,
306 307
                    typename Parent::NodeSetError());
307 308
        Node::operator=(node);
308 309
        return *this;
309 310
      }
310 311
      Red(Invalid) : Node(INVALID) {}
311 312
      Red& operator=(Invalid) {
312 313
        Node::operator=(INVALID);
313 314
        return *this;
314 315
      }
315 316
    };
316 317

	
317 318
    void first(Red& node) const {
318 319
      Parent::firstRed(static_cast<Node&>(node));
319 320
    }
320 321
    void next(Red& node) const {
321 322
      Parent::nextRed(static_cast<Node&>(node));
322 323
    }
323 324

	
324 325
    int id(const Red& node) const {
325 326
      return Parent::redId(node);
326 327
    }
327 328

	
328 329
    class Blue : public Node {
329 330
      friend class BidirBpGraphExtender;
330 331
    public:
331 332
      Blue() {}
332 333
      Blue(const Node& node) : Node(node) {
333 334
        LEMON_DEBUG(Parent::blue(node) || node == INVALID,
334 335
                    typename Parent::NodeSetError());
335 336
      }
336 337
      Blue& operator=(const Node& node) {
337 338
        LEMON_DEBUG(Parent::blue(node) || node == INVALID,
338 339
                    typename Parent::NodeSetError());
339 340
        Node::operator=(node);
340 341
        return *this;
341 342
      }
342 343
      Blue(Invalid) : Node(INVALID) {}
343 344
      Blue& operator=(Invalid) {
344 345
        Node::operator=(INVALID);
345 346
        return *this;
346 347
      }
347 348
    };
348 349

	
349 350
    void first(Blue& node) const {
350 351
      Parent::firstBlue(static_cast<Node&>(node));
351 352
    }
352 353
    void next(Blue& node) const {
353 354
      Parent::nextBlue(static_cast<Node&>(node));
354 355
    }
355 356

	
356 357
    int id(const Blue& node) const {
357 358
      return Parent::redId(node);
358 359
    }
359 360

	
360 361
    Node source(const Edge& arc) const {
361 362
      return red(arc);
362 363
    }
363 364
    Node target(const Edge& arc) const {
364 365
      return blue(arc);
365 366
    }
366 367

	
367 368
    void firstInc(Edge& arc, bool& dir, const Node& node) const {
368 369
      if (Parent::red(node)) {
369 370
        Parent::firstFromRed(arc, node);
370 371
        dir = true;
371 372
      } else {
372 373
        Parent::firstFromBlue(arc, node);
373 374
        dir = static_cast<Edge&>(arc) == INVALID;
374 375
      }
375 376
    }
376 377
    void nextInc(Edge& arc, bool& dir) const {
377 378
      if (dir) {
378 379
        Parent::nextFromRed(arc);
379 380
      } else {
380 381
        Parent::nextFromBlue(arc);
381 382
        if (arc == INVALID) dir = true;
382 383
      }
383 384
    }
384 385

	
385 386
    class Arc : public Edge {
386 387
      friend class BidirBpGraphExtender;
387 388
    protected:
388 389
      bool forward;
389 390

	
390 391
      Arc(const Edge& arc, bool _forward)
391 392
        : Edge(arc), forward(_forward) {}
392 393

	
393 394
    public:
394 395
      Arc() {}
395 396
      Arc (Invalid) : Edge(INVALID), forward(true) {}
396 397
      bool operator==(const Arc& i) const {
397 398
        return Edge::operator==(i) && forward == i.forward;
398 399
      }
399 400
      bool operator!=(const Arc& i) const {
400 401
        return Edge::operator!=(i) || forward != i.forward;
401 402
      }
402 403
      bool operator<(const Arc& i) const {
403 404
        return Edge::operator<(i) ||
404 405
          (!(i.forward<forward) && Edge(*this)<Edge(i));
405 406
      }
406 407
    };
407 408

	
408 409
    void first(Arc& arc) const {
409 410
      Parent::first(static_cast<Edge&>(arc));
410 411
      arc.forward = true;
411 412
    }
412 413

	
413 414
    void next(Arc& arc) const {
414 415
      if (!arc.forward) {
415 416
        Parent::next(static_cast<Edge&>(arc));
416 417
      }
417 418
      arc.forward = !arc.forward;
418 419
    }
419 420

	
420 421
    void firstOut(Arc& arc, const Node& node) const {
421 422
      if (Parent::red(node)) {
422 423
        Parent::firstFromRed(arc, node);
423 424
        arc.forward = true;
424 425
      } else {
425 426
        Parent::firstFromBlue(arc, node);
426 427
        arc.forward = static_cast<Edge&>(arc) == INVALID;
427 428
      }
428 429
    }
429 430
    void nextOut(Arc& arc) const {
430 431
      if (arc.forward) {
431 432
        Parent::nextFromRed(arc);
432 433
      } else {
433 434
        Parent::nextFromBlue(arc);
434 435
        arc.forward = static_cast<Edge&>(arc) == INVALID;
435 436
      }
436 437
    }
437 438

	
438 439
    void firstIn(Arc& arc, const Node& node) const {
439 440
      if (Parent::blue(node)) {
440 441
        Parent::firstFromBlue(arc, node);
441 442
        arc.forward = true;
442 443
      } else {
443 444
        Parent::firstFromRed(arc, node);
444 445
        arc.forward = static_cast<Edge&>(arc) == INVALID;
445 446
      }
446 447
    }
447 448
    void nextIn(Arc& arc) const {
448 449
      if (arc.forward) {
449 450
        Parent::nextFromBlue(arc);
450 451
      } else {
451 452
        Parent::nextFromRed(arc);
452 453
        arc.forward = static_cast<Edge&>(arc) == INVALID;
453 454
      }
454 455
    }
455 456

	
456 457
    Node source(const Arc& arc) const {
457 458
      return arc.forward ? Parent::red(arc) : Parent::blue(arc);
458 459
    }
459 460
    Node target(const Arc& arc) const {
460 461
      return arc.forward ? Parent::blue(arc) : Parent::red(arc);
461 462
    }
462 463

	
463 464
    int id(const Arc& arc) const {
464 465
      return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
465 466
        (arc.forward ? 0 : 1);
466 467
    }
467 468
    Arc arcFromId(int ix) const {
468 469
      return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
469 470
    }
470 471
    int maxArcId() const {
471 472
      return (Parent::maxEdgeId() << 1) + 1;
472 473
    }
473 474

	
474 475
    bool direction(const Arc& arc) const {
475 476
      return arc.forward;
476 477
    }
477 478

	
478 479
    Arc direct(const Edge& arc, bool dir) const {
479 480
      return Arc(arc, dir);
480 481
    }
481 482

	
482 483
    int arcNum() const {
483 484
      return 2 * Parent::edgeNum();
484 485
    }
485 486

	
486 487
    int edgeNum() const {
487 488
      return Parent::edgeNum();
488 489
    }
489 490

	
490 491

	
491 492
  };
492 493
}
493 494

	
494 495
#endif
Ignore white space 131072 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_DEFAULT_MAP_H
20 20
#define LEMON_BITS_DEFAULT_MAP_H
21 21

	
22 22
#include <lemon/config.h>
23 23
#include <lemon/bits/array_map.h>
24 24
#include <lemon/bits/vector_map.h>
25 25
//#include <lemon/bits/debug_map.h>
26 26

	
27 27
//\ingroup graphbits
28 28
//\file
29 29
//\brief Graph maps that construct and destruct their elements dynamically.
30 30

	
31 31
namespace lemon {
32 32

	
33 33

	
34 34
  //#ifndef LEMON_USE_DEBUG_MAP
35 35

	
36 36
  template <typename _Graph, typename _Item, typename _Value>
37 37
  struct DefaultMapSelector {
38 38
    typedef ArrayMap<_Graph, _Item, _Value> Map;
39 39
  };
40 40

	
41 41
  // bool
42 42
  template <typename _Graph, typename _Item>
43 43
  struct DefaultMapSelector<_Graph, _Item, bool> {
44 44
    typedef VectorMap<_Graph, _Item, bool> Map;
45 45
  };
46 46

	
47 47
  // char
48 48
  template <typename _Graph, typename _Item>
49 49
  struct DefaultMapSelector<_Graph, _Item, char> {
50 50
    typedef VectorMap<_Graph, _Item, char> Map;
51 51
  };
52 52

	
53 53
  template <typename _Graph, typename _Item>
54 54
  struct DefaultMapSelector<_Graph, _Item, signed char> {
55 55
    typedef VectorMap<_Graph, _Item, signed char> Map;
56 56
  };
57 57

	
58 58
  template <typename _Graph, typename _Item>
59 59
  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
60 60
    typedef VectorMap<_Graph, _Item, unsigned char> Map;
61 61
  };
62 62

	
63 63

	
64 64
  // int
65 65
  template <typename _Graph, typename _Item>
66 66
  struct DefaultMapSelector<_Graph, _Item, signed int> {
67 67
    typedef VectorMap<_Graph, _Item, signed int> Map;
68 68
  };
69 69

	
70 70
  template <typename _Graph, typename _Item>
71 71
  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
72 72
    typedef VectorMap<_Graph, _Item, unsigned int> Map;
73 73
  };
74 74

	
75 75

	
76 76
  // short
77 77
  template <typename _Graph, typename _Item>
78 78
  struct DefaultMapSelector<_Graph, _Item, signed short> {
79 79
    typedef VectorMap<_Graph, _Item, signed short> Map;
80 80
  };
81 81

	
82 82
  template <typename _Graph, typename _Item>
83 83
  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
84 84
    typedef VectorMap<_Graph, _Item, unsigned short> Map;
85 85
  };
86 86

	
87 87

	
88 88
  // long
89 89
  template <typename _Graph, typename _Item>
90 90
  struct DefaultMapSelector<_Graph, _Item, signed long> {
91 91
    typedef VectorMap<_Graph, _Item, signed long> Map;
92 92
  };
93 93

	
94 94
  template <typename _Graph, typename _Item>
95 95
  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
96 96
    typedef VectorMap<_Graph, _Item, unsigned long> Map;
97 97
  };
98 98

	
99 99

	
100 100
#if defined HAVE_LONG_LONG
101 101

	
102 102
  // long long
103 103
  template <typename _Graph, typename _Item>
104 104
  struct DefaultMapSelector<_Graph, _Item, signed long long> {
105 105
    typedef VectorMap<_Graph, _Item, signed long long> Map;
106 106
  };
107 107

	
108 108
  template <typename _Graph, typename _Item>
109 109
  struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
110 110
    typedef VectorMap<_Graph, _Item, unsigned long long> Map;
111 111
  };
112 112

	
113 113
#endif
114 114

	
115 115

	
116 116
  // float
117 117
  template <typename _Graph, typename _Item>
118 118
  struct DefaultMapSelector<_Graph, _Item, float> {
119 119
    typedef VectorMap<_Graph, _Item, float> Map;
120 120
  };
121 121

	
122 122

	
123 123
  // double
124 124
  template <typename _Graph, typename _Item>
125 125
  struct DefaultMapSelector<_Graph, _Item, double> {
126 126
    typedef VectorMap<_Graph, _Item,  double> Map;
127 127
  };
128 128

	
129 129

	
130 130
  // long double
131 131
  template <typename _Graph, typename _Item>
132 132
  struct DefaultMapSelector<_Graph, _Item, long double> {
133 133
    typedef VectorMap<_Graph, _Item, long double> Map;
134 134
  };
135 135

	
136 136

	
137 137
  // pointer
138 138
  template <typename _Graph, typename _Item, typename _Ptr>
139 139
  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
140 140
    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
141 141
  };
142 142

	
143 143
// #else
144 144

	
145 145
//   template <typename _Graph, typename _Item, typename _Value>
146 146
//   struct DefaultMapSelector {
147 147
//     typedef DebugMap<_Graph, _Item, _Value> Map;
148 148
//   };
149 149

	
150 150
// #endif
151 151

	
152 152
  // DefaultMap class
153 153
  template <typename _Graph, typename _Item, typename _Value>
154 154
  class DefaultMap
155 155
    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
156
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
157

	
156 158
  public:
157
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
158 159
    typedef DefaultMap<_Graph, _Item, _Value> Map;
159

	
160
    typedef typename Parent::Graph Graph;
160
    
161
    typedef typename Parent::GraphType GraphType;
161 162
    typedef typename Parent::Value Value;
162 163

	
163
    explicit DefaultMap(const Graph& graph) : Parent(graph) {}
164
    DefaultMap(const Graph& graph, const Value& value)
164
    explicit DefaultMap(const GraphType& graph) : Parent(graph) {}
165
    DefaultMap(const GraphType& graph, const Value& value)
165 166
      : Parent(graph, value) {}
166 167

	
167 168
    DefaultMap& operator=(const DefaultMap& cmap) {
168 169
      return operator=<DefaultMap>(cmap);
169 170
    }
170 171

	
171 172
    template <typename CMap>
172 173
    DefaultMap& operator=(const CMap& cmap) {
173 174
      Parent::operator=(cmap);
174 175
      return *this;
175 176
    }
176 177

	
177 178
  };
178 179

	
179 180
}
180 181

	
181 182
#endif
Ignore white space 131072 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 22
#include <lemon/core.h>
23 23
#include <lemon/error.h>
24 24
#include <lemon/bits/default_map.h>
25 25
#include <lemon/bits/map_extender.h>
26 26

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

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

	
37 39
  public:
38 40

	
39
    typedef Base Parent;
40 41
    typedef ArcSetExtender Digraph;
41 42

	
42 43
    // Base extensions
43 44

	
44 45
    typedef typename Parent::Node Node;
45 46
    typedef typename Parent::Arc Arc;
46 47

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

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

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

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

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

	
72 73

	
73 74
    // Alteration notifier extensions
74 75

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

	
78 79
  protected:
79 80

	
80 81
    mutable ArcNotifier arc_notifier;
81 82

	
82 83
  public:
83 84

	
84 85
    using Parent::notifier;
85 86

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

	
91 92
    // Iterable extensions
92 93

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

	
97 98
      NodeIt() {}
98 99

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

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

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

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

	
113 114
    };
114 115

	
115 116

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

	
120 121
      ArcIt() { }
121 122

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

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

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

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

	
136 137
    };
137 138

	
138 139

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

	
143 144
      OutArcIt() { }
144 145

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

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

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

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

	
160 161
    };
161 162

	
162 163

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

	
167 168
      InArcIt() { }
168 169

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

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

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

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

	
184 185
    };
185 186

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

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

	
214 215
    using Parent::first;
215 216

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

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

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

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

	
240 240
    };
241 241

	
242 242

	
243 243
    // Alteration extension
244 244

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

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

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

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

	
269 269
  };
270 270

	
271 271

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

	
278 279
  public:
279 280

	
280
    typedef Base Parent;
281
    typedef EdgeSetExtender Digraph;
281
    typedef EdgeSetExtender Graph;
282 282

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

	
287

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

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

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

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

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

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

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

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

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

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

	
333 332

	
334 333
  protected:
335 334

	
336 335
    mutable ArcNotifier arc_notifier;
337 336
    mutable EdgeNotifier edge_notifier;
338 337

	
339 338
  public:
340 339

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

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

	
351 350

	
352 351
    class NodeIt : public Node { 
353
      const Digraph* digraph;
352
      const Graph* graph;
354 353
    public:
355 354

	
356 355
      NodeIt() {}
357 356

	
358 357
      NodeIt(Invalid i) : Node(i) { }
359 358

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

	
364
      NodeIt(const Digraph& _graph, const Node& node) 
365
	: Node(node), digraph(&_graph) {}
363
      NodeIt(const Graph& _graph, const Node& node) 
364
	: Node(node), graph(&_graph) {}
366 365

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

	
372 371
    };
373 372

	
374 373

	
375 374
    class ArcIt : public Arc { 
376
      const Digraph* digraph;
375
      const Graph* graph;
377 376
    public:
378 377

	
379 378
      ArcIt() { }
380 379

	
381 380
      ArcIt(Invalid i) : Arc(i) { }
382 381

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

	
387
      ArcIt(const Digraph& _graph, const Arc& e) : 
388
	Arc(e), digraph(&_graph) { }
386
      ArcIt(const Graph& _graph, const Arc& e) : 
387
	Arc(e), graph(&_graph) { }
389 388

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

	
395 394
    };
396 395

	
397 396

	
398 397
    class OutArcIt : public Arc { 
399
      const Digraph* digraph;
398
      const Graph* graph;
400 399
    public:
401 400

	
402 401
      OutArcIt() { }
403 402

	
404 403
      OutArcIt(Invalid i) : Arc(i) { }
405 404

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

	
411
      OutArcIt(const Digraph& _graph, const Arc& arc) 
412
	: Arc(arc), digraph(&_graph) {}
410
      OutArcIt(const Graph& _graph, const Arc& arc) 
411
	: Arc(arc), graph(&_graph) {}
413 412

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

	
419 418
    };
420 419

	
421 420

	
422 421
    class InArcIt : public Arc { 
423
      const Digraph* digraph;
422
      const Graph* graph;
424 423
    public:
425 424

	
426 425
      InArcIt() { }
427 426

	
428 427
      InArcIt(Invalid i) : Arc(i) { }
429 428

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

	
435
      InArcIt(const Digraph& _graph, const Arc& arc) : 
436
	Arc(arc), digraph(&_graph) {}
434
      InArcIt(const Graph& _graph, const Arc& arc) : 
435
	Arc(arc), graph(&_graph) {}
437 436

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

	
443 442
    };
444 443

	
445 444

	
446 445
    class EdgeIt : public Parent::Edge { 
447
      const Digraph* digraph;
446
      const Graph* graph;
448 447
    public:
449 448

	
450 449
      EdgeIt() { }
451 450

	
452 451
      EdgeIt(Invalid i) : Edge(i) { }
453 452

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

	
458
      EdgeIt(const Digraph& _graph, const Edge& e) : 
459
	Edge(e), digraph(&_graph) { }
457
      EdgeIt(const Graph& _graph, const Edge& e) : 
458
	Edge(e), graph(&_graph) { }
460 459

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

	
466 465
    };
467 466

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

	
474 473
      IncEdgeIt() { }
475 474

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

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

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

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

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

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

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

	
534 533

	
535 534
    template <typename _Value>
536 535
    class ArcMap 
537
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
536
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
537
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
538

	
538 539
    public:
539
      typedef EdgeSetExtender Digraph;
540
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
541

	
542
      ArcMap(const Digraph& _g) 
540
      ArcMap(const Graph& _g) 
543 541
	: Parent(_g) {}
544
      ArcMap(const Digraph& _g, const _Value& _v) 
542
      ArcMap(const Graph& _g, const _Value& _v) 
545 543
	: Parent(_g, _v) {}
546 544

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

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

	
557 555
    };
558 556

	
559 557

	
560 558
    template <typename _Value>
561 559
    class EdgeMap 
562
      : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
560
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
561
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
562

	
563 563
    public:
564
      typedef EdgeSetExtender Digraph;
565
      typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
566

	
567
      EdgeMap(const Digraph& _g) 
564
      EdgeMap(const Graph& _g) 
568 565
	: Parent(_g) {}
569 566

	
570
      EdgeMap(const Digraph& _g, const _Value& _v) 
567
      EdgeMap(const Graph& _g, const _Value& _v) 
571 568
	: Parent(_g, _v) {}
572 569

	
573 570
      EdgeMap& operator=(const EdgeMap& cmap) {
574 571
	return operator=<EdgeMap>(cmap);
575 572
      }
576 573

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

	
583 580
    };
584 581

	
585 582

	
586 583
    // Alteration extension
587 584

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

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

	
613 610

	
614 611
    EdgeSetExtender() {
615 612
      arc_notifier.setContainer(*this);
616 613
      edge_notifier.setContainer(*this);
617 614
    }
618 615

	
619 616
    ~EdgeSetExtender() {
620 617
      edge_notifier.clear();
621 618
      arc_notifier.clear();
622 619
    }
623 620
    
624 621
  };
625 622

	
626 623
}
627 624

	
628 625
#endif
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_GRAPH_ADAPTOR_EXTENDER_H
20 20
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
21 21

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

	
25 25
namespace lemon {
26 26

	
27 27
  template <typename _Digraph>
28 28
  class DigraphAdaptorExtender : public _Digraph {
29
    typedef _Digraph Parent;
30

	
29 31
  public:
30 32

	
31
    typedef _Digraph Parent;
32 33
    typedef _Digraph Digraph;
33 34
    typedef DigraphAdaptorExtender Adaptor;
34 35

	
35 36
    // Base extensions
36 37

	
37 38
    typedef typename Parent::Node Node;
38 39
    typedef typename Parent::Arc Arc;
39 40

	
40 41
    int maxId(Node) const {
41 42
      return Parent::maxNodeId();
42 43
    }
43 44

	
44 45
    int maxId(Arc) const {
45 46
      return Parent::maxArcId();
46 47
    }
47 48

	
48 49
    Node fromId(int id, Node) const {
49 50
      return Parent::nodeFromId(id);
50 51
    }
51 52

	
52 53
    Arc fromId(int id, Arc) const {
53 54
      return Parent::arcFromId(id);
54 55
    }
55 56

	
56 57
    Node oppositeNode(const Node &n, const Arc &e) const {
57 58
      if (n == Parent::source(e))
58 59
        return Parent::target(e);
59 60
      else if(n==Parent::target(e))
60 61
        return Parent::source(e);
61 62
      else
62 63
        return INVALID;
63 64
    }
64 65

	
65 66
    class NodeIt : public Node {
66 67
      const Adaptor* _adaptor;
67 68
    public:
68 69

	
69 70
      NodeIt() {}
70 71

	
71 72
      NodeIt(Invalid i) : Node(i) { }
72 73

	
73 74
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
74 75
        _adaptor->first(static_cast<Node&>(*this));
75 76
      }
76 77

	
77 78
      NodeIt(const Adaptor& adaptor, const Node& node)
78 79
        : Node(node), _adaptor(&adaptor) {}
79 80

	
80 81
      NodeIt& operator++() {
81 82
        _adaptor->next(*this);
82 83
        return *this;
83 84
      }
84 85

	
85 86
    };
86 87

	
87 88

	
88 89
    class ArcIt : public Arc {
89 90
      const Adaptor* _adaptor;
90 91
    public:
91 92

	
92 93
      ArcIt() { }
93 94

	
94 95
      ArcIt(Invalid i) : Arc(i) { }
95 96

	
96 97
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
97 98
        _adaptor->first(static_cast<Arc&>(*this));
98 99
      }
99 100

	
100 101
      ArcIt(const Adaptor& adaptor, const Arc& e) :
101 102
        Arc(e), _adaptor(&adaptor) { }
102 103

	
103 104
      ArcIt& operator++() {
104 105
        _adaptor->next(*this);
105 106
        return *this;
106 107
      }
107 108

	
108 109
    };
109 110

	
110 111

	
111 112
    class OutArcIt : public Arc {
112 113
      const Adaptor* _adaptor;
113 114
    public:
114 115

	
115 116
      OutArcIt() { }
116 117

	
117 118
      OutArcIt(Invalid i) : Arc(i) { }
118 119

	
119 120
      OutArcIt(const Adaptor& adaptor, const Node& node)
120 121
        : _adaptor(&adaptor) {
121 122
        _adaptor->firstOut(*this, node);
122 123
      }
123 124

	
124 125
      OutArcIt(const Adaptor& adaptor, const Arc& arc)
125 126
        : Arc(arc), _adaptor(&adaptor) {}
126 127

	
127 128
      OutArcIt& operator++() {
128 129
        _adaptor->nextOut(*this);
129 130
        return *this;
130 131
      }
131 132

	
132 133
    };
133 134

	
134 135

	
135 136
    class InArcIt : public Arc {
136 137
      const Adaptor* _adaptor;
137 138
    public:
138 139

	
139 140
      InArcIt() { }
140 141

	
141 142
      InArcIt(Invalid i) : Arc(i) { }
142 143

	
143 144
      InArcIt(const Adaptor& adaptor, const Node& node)
144 145
        : _adaptor(&adaptor) {
145 146
        _adaptor->firstIn(*this, node);
146 147
      }
147 148

	
148 149
      InArcIt(const Adaptor& adaptor, const Arc& arc) :
149 150
        Arc(arc), _adaptor(&adaptor) {}
150 151

	
151 152
      InArcIt& operator++() {
152 153
        _adaptor->nextIn(*this);
153 154
        return *this;
154 155
      }
155 156

	
156 157
    };
157 158

	
158 159
    Node baseNode(const OutArcIt &e) const {
159 160
      return Parent::source(e);
160 161
    }
161 162
    Node runningNode(const OutArcIt &e) const {
162 163
      return Parent::target(e);
163 164
    }
164 165

	
165 166
    Node baseNode(const InArcIt &e) const {
166 167
      return Parent::target(e);
167 168
    }
168 169
    Node runningNode(const InArcIt &e) const {
169 170
      return Parent::source(e);
170 171
    }
171 172

	
172 173
  };
173 174

	
174 175
  template <typename _Graph>
175 176
  class GraphAdaptorExtender : public _Graph {
177
    typedef _Graph Parent;
178

	
176 179
  public:
177 180

	
178
    typedef _Graph Parent;
179 181
    typedef _Graph Graph;
180 182
    typedef GraphAdaptorExtender Adaptor;
181 183

	
182 184
    typedef typename Parent::Node Node;
183 185
    typedef typename Parent::Arc Arc;
184 186
    typedef typename Parent::Edge Edge;
185 187

	
186 188
    // Graph extension
187 189

	
188 190
    int maxId(Node) const {
189 191
      return Parent::maxNodeId();
190 192
    }
191 193

	
192 194
    int maxId(Arc) const {
193 195
      return Parent::maxArcId();
194 196
    }
195 197

	
196 198
    int maxId(Edge) const {
197 199
      return Parent::maxEdgeId();
198 200
    }
199 201

	
200 202
    Node fromId(int id, Node) const {
201 203
      return Parent::nodeFromId(id);
202 204
    }
203 205

	
204 206
    Arc fromId(int id, Arc) const {
205 207
      return Parent::arcFromId(id);
206 208
    }
207 209

	
208 210
    Edge fromId(int id, Edge) const {
209 211
      return Parent::edgeFromId(id);
210 212
    }
211 213

	
212 214
    Node oppositeNode(const Node &n, const Edge &e) const {
213 215
      if( n == Parent::u(e))
214 216
        return Parent::v(e);
215 217
      else if( n == Parent::v(e))
216 218
        return Parent::u(e);
217 219
      else
218 220
        return INVALID;
219 221
    }
220 222

	
221 223
    Arc oppositeArc(const Arc &a) const {
222 224
      return Parent::direct(a, !Parent::direction(a));
223 225
    }
224 226

	
225 227
    using Parent::direct;
226 228
    Arc direct(const Edge &e, const Node &s) const {
227 229
      return Parent::direct(e, Parent::u(e) == s);
228 230
    }
229 231

	
230 232

	
231 233
    class NodeIt : public Node {
232 234
      const Adaptor* _adaptor;
233 235
    public:
234 236

	
235 237
      NodeIt() {}
236 238

	
237 239
      NodeIt(Invalid i) : Node(i) { }
238 240

	
239 241
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
240 242
        _adaptor->first(static_cast<Node&>(*this));
241 243
      }
242 244

	
243 245
      NodeIt(const Adaptor& adaptor, const Node& node)
244 246
        : Node(node), _adaptor(&adaptor) {}
245 247

	
246 248
      NodeIt& operator++() {
247 249
        _adaptor->next(*this);
248 250
        return *this;
249 251
      }
250 252

	
251 253
    };
252 254

	
253 255

	
254 256
    class ArcIt : public Arc {
255 257
      const Adaptor* _adaptor;
256 258
    public:
257 259

	
258 260
      ArcIt() { }
259 261

	
260 262
      ArcIt(Invalid i) : Arc(i) { }
261 263

	
262 264
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
263 265
        _adaptor->first(static_cast<Arc&>(*this));
264 266
      }
265 267

	
266 268
      ArcIt(const Adaptor& adaptor, const Arc& e) :
267 269
        Arc(e), _adaptor(&adaptor) { }
268 270

	
269 271
      ArcIt& operator++() {
270 272
        _adaptor->next(*this);
271 273
        return *this;
272 274
      }
273 275

	
274 276
    };
275 277

	
276 278

	
277 279
    class OutArcIt : public Arc {
278 280
      const Adaptor* _adaptor;
279 281
    public:
280 282

	
281 283
      OutArcIt() { }
282 284

	
283 285
      OutArcIt(Invalid i) : Arc(i) { }
284 286

	
285 287
      OutArcIt(const Adaptor& adaptor, const Node& node)
286 288
        : _adaptor(&adaptor) {
287 289
        _adaptor->firstOut(*this, node);
288 290
      }
289 291

	
290 292
      OutArcIt(const Adaptor& adaptor, const Arc& arc)
291 293
        : Arc(arc), _adaptor(&adaptor) {}
292 294

	
293 295
      OutArcIt& operator++() {
294 296
        _adaptor->nextOut(*this);
295 297
        return *this;
296 298
      }
297 299

	
298 300
    };
299 301

	
300 302

	
301 303
    class InArcIt : public Arc {
302 304
      const Adaptor* _adaptor;
303 305
    public:
304 306

	
305 307
      InArcIt() { }
306 308

	
307 309
      InArcIt(Invalid i) : Arc(i) { }
308 310

	
309 311
      InArcIt(const Adaptor& adaptor, const Node& node)
310 312
        : _adaptor(&adaptor) {
311 313
        _adaptor->firstIn(*this, node);
312 314
      }
313 315

	
314 316
      InArcIt(const Adaptor& adaptor, const Arc& arc) :
315 317
        Arc(arc), _adaptor(&adaptor) {}
316 318

	
317 319
      InArcIt& operator++() {
318 320
        _adaptor->nextIn(*this);
319 321
        return *this;
320 322
      }
321 323

	
322 324
    };
323 325

	
324 326
    class EdgeIt : public Parent::Edge {
325 327
      const Adaptor* _adaptor;
326 328
    public:
327 329

	
328 330
      EdgeIt() { }
329 331

	
330 332
      EdgeIt(Invalid i) : Edge(i) { }
331 333

	
332 334
      explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
333 335
        _adaptor->first(static_cast<Edge&>(*this));
334 336
      }
335 337

	
336 338
      EdgeIt(const Adaptor& adaptor, const Edge& e) :
337 339
        Edge(e), _adaptor(&adaptor) { }
338 340

	
339 341
      EdgeIt& operator++() {
340 342
        _adaptor->next(*this);
341 343
        return *this;
342 344
      }
343 345

	
344 346
    };
345 347

	
346 348
    class IncEdgeIt : public Edge {
347 349
      friend class GraphAdaptorExtender;
348 350
      const Adaptor* _adaptor;
349 351
      bool direction;
350 352
    public:
351 353

	
352 354
      IncEdgeIt() { }
353 355

	
354 356
      IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
355 357

	
356 358
      IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
357 359
        _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
358 360
      }
359 361

	
360 362
      IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
361 363
        : _adaptor(&adaptor), Edge(e) {
362 364
        direction = (_adaptor->u(e) == n);
363 365
      }
364 366

	
365 367
      IncEdgeIt& operator++() {
366 368
        _adaptor->nextInc(*this, direction);
367 369
        return *this;
368 370
      }
369 371
    };
370 372

	
371 373
    Node baseNode(const OutArcIt &a) const {
372 374
      return Parent::source(a);
373 375
    }
374 376
    Node runningNode(const OutArcIt &a) const {
375 377
      return Parent::target(a);
376 378
    }
377 379

	
378 380
    Node baseNode(const InArcIt &a) const {
379 381
      return Parent::target(a);
380 382
    }
381 383
    Node runningNode(const InArcIt &a) const {
382 384
      return Parent::source(a);
383 385
    }
384 386

	
385 387
    Node baseNode(const IncEdgeIt &e) const {
386 388
      return e.direction ? Parent::u(e) : Parent::v(e);
387 389
    }
388 390
    Node runningNode(const IncEdgeIt &e) const {
389 391
      return e.direction ? Parent::v(e) : Parent::u(e);
390 392
    }
391 393

	
392 394
  };
393 395

	
394 396
}
395 397

	
396 398

	
397 399
#endif
Ignore white space 131072 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_GRAPH_EXTENDER_H
20 20
#define LEMON_BITS_GRAPH_EXTENDER_H
21 21

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

	
24 24
#include <lemon/bits/map_extender.h>
25 25
#include <lemon/bits/default_map.h>
26 26

	
27 27
#include <lemon/concept_check.h>
28 28
#include <lemon/concepts/maps.h>
29 29

	
30 30
//\ingroup graphbits
31 31
//\file
32 32
//\brief Extenders for the graph types
33 33
namespace lemon {
34 34

	
35 35
  // \ingroup graphbits
36 36
  //
37 37
  // \brief Extender for the digraph implementations
38 38
  template <typename Base>
39 39
  class DigraphExtender : public Base {
40
    typedef Base Parent;
41

	
40 42
  public:
41 43

	
42
    typedef Base Parent;
43 44
    typedef DigraphExtender Digraph;
44 45

	
45 46
    // Base extensions
46 47

	
47 48
    typedef typename Parent::Node Node;
48 49
    typedef typename Parent::Arc Arc;
49 50

	
50 51
    int maxId(Node) const {
51 52
      return Parent::maxNodeId();
52 53
    }
53 54

	
54 55
    int maxId(Arc) const {
55 56
      return Parent::maxArcId();
56 57
    }
57 58

	
58 59
    Node fromId(int id, Node) const {
59 60
      return Parent::nodeFromId(id);
60 61
    }
61 62

	
62 63
    Arc fromId(int id, Arc) const {
63 64
      return Parent::arcFromId(id);
64 65
    }
65 66

	
66 67
    Node oppositeNode(const Node &node, const Arc &arc) const {
67 68
      if (node == Parent::source(arc))
68 69
        return Parent::target(arc);
69 70
      else if(node == Parent::target(arc))
70 71
        return Parent::source(arc);
71 72
      else
72 73
        return INVALID;
73 74
    }
74 75

	
75 76
    // Alterable extension
76 77

	
77 78
    typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
78 79
    typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
79 80

	
80 81

	
81 82
  protected:
82 83

	
83 84
    mutable NodeNotifier node_notifier;
84 85
    mutable ArcNotifier arc_notifier;
85 86

	
86 87
  public:
87 88

	
88 89
    NodeNotifier& notifier(Node) const {
89 90
      return node_notifier;
90 91
    }
91 92

	
92 93
    ArcNotifier& notifier(Arc) const {
93 94
      return arc_notifier;
94 95
    }
95 96

	
96 97
    class NodeIt : public Node {
97 98
      const Digraph* _digraph;
98 99
    public:
99 100

	
100 101
      NodeIt() {}
101 102

	
102 103
      NodeIt(Invalid i) : Node(i) { }
103 104

	
104 105
      explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
105 106
        _digraph->first(static_cast<Node&>(*this));
106 107
      }
107 108

	
108 109
      NodeIt(const Digraph& digraph, const Node& node)
109 110
        : Node(node), _digraph(&digraph) {}
110 111

	
111 112
      NodeIt& operator++() {
112 113
        _digraph->next(*this);
113 114
        return *this;
114 115
      }
115 116

	
116 117
    };
117 118

	
118 119

	
119 120
    class ArcIt : public Arc {
120 121
      const Digraph* _digraph;
121 122
    public:
122 123

	
123 124
      ArcIt() { }
124 125

	
125 126
      ArcIt(Invalid i) : Arc(i) { }
126 127

	
127 128
      explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
128 129
        _digraph->first(static_cast<Arc&>(*this));
129 130
      }
130 131

	
131 132
      ArcIt(const Digraph& digraph, const Arc& arc) :
132 133
        Arc(arc), _digraph(&digraph) { }
133 134

	
134 135
      ArcIt& operator++() {
135 136
        _digraph->next(*this);
136 137
        return *this;
137 138
      }
138 139

	
139 140
    };
140 141

	
141 142

	
142 143
    class OutArcIt : public Arc {
143 144
      const Digraph* _digraph;
144 145
    public:
145 146

	
146 147
      OutArcIt() { }
147 148

	
148 149
      OutArcIt(Invalid i) : Arc(i) { }
149 150

	
150 151
      OutArcIt(const Digraph& digraph, const Node& node)
151 152
        : _digraph(&digraph) {
152 153
        _digraph->firstOut(*this, node);
153 154
      }
154 155

	
155 156
      OutArcIt(const Digraph& digraph, const Arc& arc)
156 157
        : Arc(arc), _digraph(&digraph) {}
157 158

	
158 159
      OutArcIt& operator++() {
159 160
        _digraph->nextOut(*this);
160 161
        return *this;
161 162
      }
162 163

	
163 164
    };
164 165

	
165 166

	
166 167
    class InArcIt : public Arc {
167 168
      const Digraph* _digraph;
168 169
    public:
169 170

	
170 171
      InArcIt() { }
171 172

	
172 173
      InArcIt(Invalid i) : Arc(i) { }
173 174

	
174 175
      InArcIt(const Digraph& digraph, const Node& node)
175 176
        : _digraph(&digraph) {
176 177
        _digraph->firstIn(*this, node);
177 178
      }
178 179

	
179 180
      InArcIt(const Digraph& digraph, const Arc& arc) :
180 181
        Arc(arc), _digraph(&digraph) {}
181 182

	
182 183
      InArcIt& operator++() {
183 184
        _digraph->nextIn(*this);
184 185
        return *this;
185 186
      }
186 187

	
187 188
    };
188 189

	
189 190
    // \brief Base node of the iterator
190 191
    //
191 192
    // Returns the base node (i.e. the source in this case) of the iterator
192 193
    Node baseNode(const OutArcIt &arc) const {
193 194
      return Parent::source(arc);
194 195
    }
195 196
    // \brief Running node of the iterator
196 197
    //
197 198
    // Returns the running node (i.e. the target in this case) of the
198 199
    // iterator
199 200
    Node runningNode(const OutArcIt &arc) const {
200 201
      return Parent::target(arc);
201 202
    }
202 203

	
203 204
    // \brief Base node of the iterator
204 205
    //
205 206
    // Returns the base node (i.e. the target in this case) of the iterator
206 207
    Node baseNode(const InArcIt &arc) const {
207 208
      return Parent::target(arc);
208 209
    }
209 210
    // \brief Running node of the iterator
210 211
    //
211 212
    // Returns the running node (i.e. the source in this case) of the
212 213
    // iterator
213 214
    Node runningNode(const InArcIt &arc) const {
214 215
      return Parent::source(arc);
215 216
    }
216 217

	
217 218

	
218 219
    template <typename _Value>
219 220
    class NodeMap
220 221
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
221
    public:
222
      typedef DigraphExtender Digraph;
223 222
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
224 223

	
224
    public:
225 225
      explicit NodeMap(const Digraph& digraph)
226 226
        : Parent(digraph) {}
227 227
      NodeMap(const Digraph& digraph, const _Value& value)
228 228
        : Parent(digraph, value) {}
229 229

	
230 230
    private:
231 231
      NodeMap& operator=(const NodeMap& cmap) {
232 232
        return operator=<NodeMap>(cmap);
233 233
      }
234 234

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

	
241 241
    };
242 242

	
243 243
    template <typename _Value>
244 244
    class ArcMap
245 245
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
246
    public:
247
      typedef DigraphExtender Digraph;
248 246
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
249 247

	
248
    public:
250 249
      explicit ArcMap(const Digraph& digraph)
251 250
        : Parent(digraph) {}
252 251
      ArcMap(const Digraph& digraph, const _Value& value)
253 252
        : Parent(digraph, value) {}
254 253

	
255 254
    private:
256 255
      ArcMap& operator=(const ArcMap& cmap) {
257 256
        return operator=<ArcMap>(cmap);
258 257
      }
259 258

	
260 259
      template <typename CMap>
261 260
      ArcMap& operator=(const CMap& cmap) {
262 261
        Parent::operator=(cmap);
263 262
        return *this;
264 263
      }
265 264
    };
266 265

	
267 266

	
268 267
    Node addNode() {
269 268
      Node node = Parent::addNode();
270 269
      notifier(Node()).add(node);
271 270
      return node;
272 271
    }
273 272

	
274 273
    Arc addArc(const Node& from, const Node& to) {
275 274
      Arc arc = Parent::addArc(from, to);
276 275
      notifier(Arc()).add(arc);
277 276
      return arc;
278 277
    }
279 278

	
280 279
    void clear() {
281 280
      notifier(Arc()).clear();
282 281
      notifier(Node()).clear();
283 282
      Parent::clear();
284 283
    }
285 284

	
286 285
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
287 286
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
288 287
      Parent::build(digraph, nodeRef, arcRef);
289 288
      notifier(Node()).build();
290 289
      notifier(Arc()).build();
291 290
    }
292 291

	
293 292
    void erase(const Node& node) {
294 293
      Arc arc;
295 294
      Parent::firstOut(arc, node);
296 295
      while (arc != INVALID ) {
297 296
        erase(arc);
298 297
        Parent::firstOut(arc, node);
299 298
      }
300 299

	
301 300
      Parent::firstIn(arc, node);
302 301
      while (arc != INVALID ) {
303 302
        erase(arc);
304 303
        Parent::firstIn(arc, node);
305 304
      }
306 305

	
307 306
      notifier(Node()).erase(node);
308 307
      Parent::erase(node);
309 308
    }
310 309

	
311 310
    void erase(const Arc& arc) {
312 311
      notifier(Arc()).erase(arc);
313 312
      Parent::erase(arc);
314 313
    }
315 314

	
316 315
    DigraphExtender() {
317 316
      node_notifier.setContainer(*this);
318 317
      arc_notifier.setContainer(*this);
319 318
    }
320 319

	
321 320

	
322 321
    ~DigraphExtender() {
323 322
      arc_notifier.clear();
324 323
      node_notifier.clear();
325 324
    }
326 325
  };
327 326

	
328 327
  // \ingroup _graphbits
329 328
  //
330 329
  // \brief Extender for the Graphs
331 330
  template <typename Base>
332 331
  class GraphExtender : public Base {
332
    typedef Base Parent;
333

	
333 334
  public:
334 335

	
335
    typedef Base Parent;
336 336
    typedef GraphExtender Graph;
337 337

	
338 338
    typedef True UndirectedTag;
339 339

	
340 340
    typedef typename Parent::Node Node;
341 341
    typedef typename Parent::Arc Arc;
342 342
    typedef typename Parent::Edge Edge;
343 343

	
344 344
    // Graph extension
345 345

	
346 346
    int maxId(Node) const {
347 347
      return Parent::maxNodeId();
348 348
    }
349 349

	
350 350
    int maxId(Arc) const {
351 351
      return Parent::maxArcId();
352 352
    }
353 353

	
354 354
    int maxId(Edge) const {
355 355
      return Parent::maxEdgeId();
356 356
    }
357 357

	
358 358
    Node fromId(int id, Node) const {
359 359
      return Parent::nodeFromId(id);
360 360
    }
361 361

	
362 362
    Arc fromId(int id, Arc) const {
363 363
      return Parent::arcFromId(id);
364 364
    }
365 365

	
366 366
    Edge fromId(int id, Edge) const {
367 367
      return Parent::edgeFromId(id);
368 368
    }
369 369

	
370 370
    Node oppositeNode(const Node &n, const Edge &e) const {
371 371
      if( n == Parent::u(e))
372 372
        return Parent::v(e);
373 373
      else if( n == Parent::v(e))
374 374
        return Parent::u(e);
375 375
      else
376 376
        return INVALID;
377 377
    }
378 378

	
379 379
    Arc oppositeArc(const Arc &arc) const {
380 380
      return Parent::direct(arc, !Parent::direction(arc));
381 381
    }
382 382

	
383 383
    using Parent::direct;
384 384
    Arc direct(const Edge &edge, const Node &node) const {
385 385
      return Parent::direct(edge, Parent::u(edge) == node);
386 386
    }
387 387

	
388 388
    // Alterable extension
389 389

	
390 390
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
391 391
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
392 392
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
393 393

	
394 394

	
395 395
  protected:
396 396

	
397 397
    mutable NodeNotifier node_notifier;
398 398
    mutable ArcNotifier arc_notifier;
399 399
    mutable EdgeNotifier edge_notifier;
400 400

	
401 401
  public:
402 402

	
403 403
    NodeNotifier& notifier(Node) const {
404 404
      return node_notifier;
405 405
    }
406 406

	
407 407
    ArcNotifier& notifier(Arc) const {
408 408
      return arc_notifier;
409 409
    }
410 410

	
411 411
    EdgeNotifier& notifier(Edge) const {
412 412
      return edge_notifier;
413 413
    }
414 414

	
415 415

	
416 416

	
417 417
    class NodeIt : public Node {
418 418
      const Graph* _graph;
419 419
    public:
420 420

	
421 421
      NodeIt() {}
422 422

	
423 423
      NodeIt(Invalid i) : Node(i) { }
424 424

	
425 425
      explicit NodeIt(const Graph& graph) : _graph(&graph) {
426 426
        _graph->first(static_cast<Node&>(*this));
427 427
      }
428 428

	
429 429
      NodeIt(const Graph& graph, const Node& node)
430 430
        : Node(node), _graph(&graph) {}
431 431

	
432 432
      NodeIt& operator++() {
433 433
        _graph->next(*this);
434 434
        return *this;
435 435
      }
436 436

	
437 437
    };
438 438

	
439 439

	
440 440
    class ArcIt : public Arc {
441 441
      const Graph* _graph;
442 442
    public:
443 443

	
444 444
      ArcIt() { }
445 445

	
446 446
      ArcIt(Invalid i) : Arc(i) { }
447 447

	
448 448
      explicit ArcIt(const Graph& graph) : _graph(&graph) {
449 449
        _graph->first(static_cast<Arc&>(*this));
450 450
      }
451 451

	
452 452
      ArcIt(const Graph& graph, const Arc& arc) :
453 453
        Arc(arc), _graph(&graph) { }
454 454

	
455 455
      ArcIt& operator++() {
456 456
        _graph->next(*this);
457 457
        return *this;
458 458
      }
459 459

	
460 460
    };
461 461

	
462 462

	
463 463
    class OutArcIt : public Arc {
464 464
      const Graph* _graph;
465 465
    public:
466 466

	
467 467
      OutArcIt() { }
468 468

	
469 469
      OutArcIt(Invalid i) : Arc(i) { }
470 470

	
471 471
      OutArcIt(const Graph& graph, const Node& node)
472 472
        : _graph(&graph) {
473 473
        _graph->firstOut(*this, node);
474 474
      }
475 475

	
476 476
      OutArcIt(const Graph& graph, const Arc& arc)
477 477
        : Arc(arc), _graph(&graph) {}
478 478

	
479 479
      OutArcIt& operator++() {
480 480
        _graph->nextOut(*this);
481 481
        return *this;
482 482
      }
483 483

	
484 484
    };
485 485

	
486 486

	
487 487
    class InArcIt : public Arc {
488 488
      const Graph* _graph;
489 489
    public:
490 490

	
491 491
      InArcIt() { }
492 492

	
493 493
      InArcIt(Invalid i) : Arc(i) { }
494 494

	
495 495
      InArcIt(const Graph& graph, const Node& node)
496 496
        : _graph(&graph) {
497 497
        _graph->firstIn(*this, node);
498 498
      }
499 499

	
500 500
      InArcIt(const Graph& graph, const Arc& arc) :
501 501
        Arc(arc), _graph(&graph) {}
502 502

	
503 503
      InArcIt& operator++() {
504 504
        _graph->nextIn(*this);
505 505
        return *this;
506 506
      }
507 507

	
508 508
    };
509 509

	
510 510

	
511 511
    class EdgeIt : public Parent::Edge {
512 512
      const Graph* _graph;
513 513
    public:
514 514

	
515 515
      EdgeIt() { }
516 516

	
517 517
      EdgeIt(Invalid i) : Edge(i) { }
518 518

	
519 519
      explicit EdgeIt(const Graph& graph) : _graph(&graph) {
520 520
        _graph->first(static_cast<Edge&>(*this));
521 521
      }
522 522

	
523 523
      EdgeIt(const Graph& graph, const Edge& edge) :
524 524
        Edge(edge), _graph(&graph) { }
525 525

	
526 526
      EdgeIt& operator++() {
527 527
        _graph->next(*this);
528 528
        return *this;
529 529
      }
530 530

	
531 531
    };
532 532

	
533 533
    class IncEdgeIt : public Parent::Edge {
534 534
      friend class GraphExtender;
535 535
      const Graph* _graph;
536 536
      bool _direction;
537 537
    public:
538 538

	
539 539
      IncEdgeIt() { }
540 540

	
541 541
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
542 542

	
543 543
      IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
544 544
        _graph->firstInc(*this, _direction, node);
545 545
      }
546 546

	
547 547
      IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
548 548
        : _graph(&graph), Edge(edge) {
549 549
        _direction = (_graph->source(edge) == node);
550 550
      }
551 551

	
552 552
      IncEdgeIt& operator++() {
553 553
        _graph->nextInc(*this, _direction);
554 554
        return *this;
555 555
      }
556 556
    };
557 557

	
558 558
    // \brief Base node of the iterator
559 559
    //
560 560
    // Returns the base node (ie. the source in this case) of the iterator
561 561
    Node baseNode(const OutArcIt &arc) const {
562 562
      return Parent::source(static_cast<const Arc&>(arc));
563 563
    }
564 564
    // \brief Running node of the iterator
565 565
    //
566 566
    // Returns the running node (ie. the target in this case) of the
567 567
    // iterator
568 568
    Node runningNode(const OutArcIt &arc) const {
569 569
      return Parent::target(static_cast<const Arc&>(arc));
570 570
    }
571 571

	
572 572
    // \brief Base node of the iterator
573 573
    //
574 574
    // Returns the base node (ie. the target in this case) of the iterator
575 575
    Node baseNode(const InArcIt &arc) const {
576 576
      return Parent::target(static_cast<const Arc&>(arc));
577 577
    }
578 578
    // \brief Running node of the iterator
579 579
    //
580 580
    // Returns the running node (ie. the source in this case) of the
581 581
    // iterator
582 582
    Node runningNode(const InArcIt &arc) const {
583 583
      return Parent::source(static_cast<const Arc&>(arc));
584 584
    }
585 585

	
586 586
    // Base node of the iterator
587 587
    //
588 588
    // Returns the base node of the iterator
589 589
    Node baseNode(const IncEdgeIt &edge) const {
590 590
      return edge._direction ? u(edge) : v(edge);
591 591
    }
592 592
    // Running node of the iterator
593 593
    //
594 594
    // Returns the running node of the iterator
595 595
    Node runningNode(const IncEdgeIt &edge) const {
596 596
      return edge._direction ? v(edge) : u(edge);
597 597
    }
598 598

	
599 599
    // Mappable extension
600 600

	
601 601
    template <typename _Value>
602 602
    class NodeMap
603 603
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
604
    public:
605
      typedef GraphExtender Graph;
606 604
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
607 605

	
606
    public:
608 607
      NodeMap(const Graph& graph)
609 608
        : Parent(graph) {}
610 609
      NodeMap(const Graph& graph, const _Value& value)
611 610
        : Parent(graph, value) {}
612 611

	
613 612
    private:
614 613
      NodeMap& operator=(const NodeMap& cmap) {
615 614
        return operator=<NodeMap>(cmap);
616 615
      }
617 616

	
618 617
      template <typename CMap>
619 618
      NodeMap& operator=(const CMap& cmap) {
620 619
        Parent::operator=(cmap);
621 620
        return *this;
622 621
      }
623 622

	
624 623
    };
625 624

	
626 625
    template <typename _Value>
627 626
    class ArcMap
628 627
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
629
    public:
630
      typedef GraphExtender Graph;
631 628
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
632 629

	
630
    public:
633 631
      ArcMap(const Graph& graph)
634 632
        : Parent(graph) {}
635 633
      ArcMap(const Graph& graph, const _Value& value)
636 634
        : Parent(graph, value) {}
637 635

	
638 636
    private:
639 637
      ArcMap& operator=(const ArcMap& cmap) {
640 638
        return operator=<ArcMap>(cmap);
641 639
      }
642 640

	
643 641
      template <typename CMap>
644 642
      ArcMap& operator=(const CMap& cmap) {
645 643
        Parent::operator=(cmap);
646 644
        return *this;
647 645
      }
648 646
    };
649 647

	
650 648

	
651 649
    template <typename _Value>
652 650
    class EdgeMap
653 651
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
654
    public:
655
      typedef GraphExtender Graph;
656 652
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
657 653

	
654
    public:
658 655
      EdgeMap(const Graph& graph)
659 656
        : Parent(graph) {}
660 657

	
661 658
      EdgeMap(const Graph& graph, const _Value& value)
662 659
        : Parent(graph, value) {}
663 660

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

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

	
675 672
    };
676 673

	
677 674
    // Alteration extension
678 675

	
679 676
    Node addNode() {
680 677
      Node node = Parent::addNode();
681 678
      notifier(Node()).add(node);
682 679
      return node;
683 680
    }
684 681

	
685 682
    Edge addEdge(const Node& from, const Node& to) {
686 683
      Edge edge = Parent::addEdge(from, to);
687 684
      notifier(Edge()).add(edge);
688 685
      std::vector<Arc> ev;
689 686
      ev.push_back(Parent::direct(edge, true));
690 687
      ev.push_back(Parent::direct(edge, false));
691 688
      notifier(Arc()).add(ev);
692 689
      return edge;
693 690
    }
694 691

	
695 692
    void clear() {
696 693
      notifier(Arc()).clear();
697 694
      notifier(Edge()).clear();
698 695
      notifier(Node()).clear();
699 696
      Parent::clear();
700 697
    }
701 698

	
702 699
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
703 700
    void build(const Graph& graph, NodeRefMap& nodeRef,
704 701
               EdgeRefMap& edgeRef) {
705 702
      Parent::build(graph, nodeRef, edgeRef);
706 703
      notifier(Node()).build();
707 704
      notifier(Edge()).build();
708 705
      notifier(Arc()).build();
709 706
    }
710 707

	
711 708
    void erase(const Node& node) {
712 709
      Arc arc;
713 710
      Parent::firstOut(arc, node);
714 711
      while (arc != INVALID ) {
715 712
        erase(arc);
716 713
        Parent::firstOut(arc, node);
717 714
      }
718 715

	
719 716
      Parent::firstIn(arc, node);
720 717
      while (arc != INVALID ) {
721 718
        erase(arc);
722 719
        Parent::firstIn(arc, node);
723 720
      }
724 721

	
725 722
      notifier(Node()).erase(node);
726 723
      Parent::erase(node);
727 724
    }
728 725

	
729 726
    void erase(const Edge& edge) {
730 727
      std::vector<Arc> av;
731 728
      av.push_back(Parent::direct(edge, true));
732 729
      av.push_back(Parent::direct(edge, false));
733 730
      notifier(Arc()).erase(av);
734 731
      notifier(Edge()).erase(edge);
735 732
      Parent::erase(edge);
736 733
    }
737 734

	
738 735
    GraphExtender() {
739 736
      node_notifier.setContainer(*this);
740 737
      arc_notifier.setContainer(*this);
741 738
      edge_notifier.setContainer(*this);
742 739
    }
743 740

	
744 741
    ~GraphExtender() {
745 742
      edge_notifier.clear();
746 743
      arc_notifier.clear();
747 744
      node_notifier.clear();
748 745
    }
749 746

	
750 747
  };
751 748

	
752 749
}
753 750

	
754 751
#endif
Ignore white space 131072 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_MAP_EXTENDER_H
20 20
#define LEMON_BITS_MAP_EXTENDER_H
21 21

	
22 22
#include <iterator>
23 23

	
24 24
#include <lemon/bits/traits.h>
25 25

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

	
29 29
//\file
30 30
//\brief Extenders for iterable maps.
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  // \ingroup graphbits
35 35
  //
36 36
  // \brief Extender for maps
37 37
  template <typename _Map>
38 38
  class MapExtender : public _Map {
39
    typedef _Map Parent;
40
    typedef typename Parent::GraphType GraphType;
41

	
39 42
  public:
40 43

	
41
    typedef _Map Parent;
42 44
    typedef MapExtender Map;
43

	
44

	
45
    typedef typename Parent::Graph Graph;
46 45
    typedef typename Parent::Key Item;
47 46

	
48 47
    typedef typename Parent::Key Key;
49 48
    typedef typename Parent::Value Value;
50 49
    typedef typename Parent::Reference Reference;
51 50
    typedef typename Parent::ConstReference ConstReference;
52 51

	
53 52
    class MapIt;
54 53
    class ConstMapIt;
55 54

	
56 55
    friend class MapIt;
57 56
    friend class ConstMapIt;
58 57

	
59 58
  public:
60 59

	
61
    MapExtender(const Graph& graph)
60
    MapExtender(const GraphType& graph)
62 61
      : Parent(graph) {}
63 62

	
64
    MapExtender(const Graph& graph, const Value& value)
63
    MapExtender(const GraphType& graph, const Value& value)
65 64
      : Parent(graph, value) {}
66 65

	
67 66
  private:
68 67
    MapExtender& operator=(const MapExtender& cmap) {
69 68
      return operator=<MapExtender>(cmap);
70 69
    }
71 70

	
72 71
    template <typename CMap>
73 72
    MapExtender& operator=(const CMap& cmap) {
74 73
      Parent::operator=(cmap);
75 74
      return *this;
76 75
    }
77 76

	
78 77
  public:
79 78
    class MapIt : public Item {
79
      typedef Item Parent;
80

	
80 81
    public:
81 82

	
82
      typedef Item Parent;
83 83
      typedef typename Map::Value Value;
84 84

	
85 85
      MapIt() {}
86 86

	
87 87
      MapIt(Invalid i) : Parent(i) { }
88 88

	
89 89
      explicit MapIt(Map& _map) : map(_map) {
90 90
        map.notifier()->first(*this);
91 91
      }
92 92

	
93 93
      MapIt(const Map& _map, const Item& item)
94 94
        : Parent(item), map(_map) {}
95 95

	
96 96
      MapIt& operator++() {
97 97
        map.notifier()->next(*this);
98 98
        return *this;
99 99
      }
100 100

	
101 101
      typename MapTraits<Map>::ConstReturnValue operator*() const {
102 102
        return map[*this];
103 103
      }
104 104

	
105 105
      typename MapTraits<Map>::ReturnValue operator*() {
106 106
        return map[*this];
107 107
      }
108 108

	
109 109
      void set(const Value& value) {
110 110
        map.set(*this, value);
111 111
      }
112 112

	
113 113
    protected:
114 114
      Map& map;
115 115

	
116 116
    };
117 117

	
118 118
    class ConstMapIt : public Item {
119
      typedef Item Parent;
120

	
119 121
    public:
120 122

	
121
      typedef Item Parent;
122

	
123 123
      typedef typename Map::Value Value;
124 124

	
125 125
      ConstMapIt() {}
126 126

	
127 127
      ConstMapIt(Invalid i) : Parent(i) { }
128 128

	
129 129
      explicit ConstMapIt(Map& _map) : map(_map) {
130 130
        map.notifier()->first(*this);
131 131
      }
132 132

	
133 133
      ConstMapIt(const Map& _map, const Item& item)
134 134
        : Parent(item), map(_map) {}
135 135

	
136 136
      ConstMapIt& operator++() {
137 137
        map.notifier()->next(*this);
138 138
        return *this;
139 139
      }
140 140

	
141 141
      typename MapTraits<Map>::ConstReturnValue operator*() const {
142 142
        return map[*this];
143 143
      }
144 144

	
145 145
    protected:
146 146
      const Map& map;
147 147
    };
148 148

	
149 149
    class ItemIt : public Item {
150
      typedef Item Parent;
151

	
150 152
    public:
151 153

	
152
      typedef Item Parent;
153

	
154 154
      ItemIt() {}
155 155

	
156 156
      ItemIt(Invalid i) : Parent(i) { }
157 157

	
158 158
      explicit ItemIt(Map& _map) : map(_map) {
159 159
        map.notifier()->first(*this);
160 160
      }
161 161

	
162 162
      ItemIt(const Map& _map, const Item& item)
163 163
        : Parent(item), map(_map) {}
164 164

	
165 165
      ItemIt& operator++() {
166 166
        map.notifier()->next(*this);
167 167
        return *this;
168 168
      }
169 169

	
170 170
    protected:
171 171
      const Map& map;
172 172

	
173 173
    };
174 174
  };
175 175

	
176 176
  // \ingroup graphbits
177 177
  //
178 178
  // \brief Extender for maps which use a subset of the items.
179 179
  template <typename _Graph, typename _Map>
180 180
  class SubMapExtender : public _Map {
181
    typedef _Map Parent;
182
    typedef _Graph GraphType;
183

	
181 184
  public:
182 185

	
183
    typedef _Map Parent;
184 186
    typedef SubMapExtender Map;
185

	
186
    typedef _Graph Graph;
187

	
188 187
    typedef typename Parent::Key Item;
189 188

	
190 189
    typedef typename Parent::Key Key;
191 190
    typedef typename Parent::Value Value;
192 191
    typedef typename Parent::Reference Reference;
193 192
    typedef typename Parent::ConstReference ConstReference;
194 193

	
195 194
    class MapIt;
196 195
    class ConstMapIt;
197 196

	
198 197
    friend class MapIt;
199 198
    friend class ConstMapIt;
200 199

	
201 200
  public:
202 201

	
203
    SubMapExtender(const Graph& _graph)
202
    SubMapExtender(const GraphType& _graph)
204 203
      : Parent(_graph), graph(_graph) {}
205 204

	
206
    SubMapExtender(const Graph& _graph, const Value& _value)
205
    SubMapExtender(const GraphType& _graph, const Value& _value)
207 206
      : Parent(_graph, _value), graph(_graph) {}
208 207

	
209 208
  private:
210 209
    SubMapExtender& operator=(const SubMapExtender& cmap) {
211 210
      return operator=<MapExtender>(cmap);
212 211
    }
213 212

	
214 213
    template <typename CMap>
215 214
    SubMapExtender& operator=(const CMap& cmap) {
216 215
      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
217 216
      Item it;
218 217
      for (graph.first(it); it != INVALID; graph.next(it)) {
219 218
        Parent::set(it, cmap[it]);
220 219
      }
221 220
      return *this;
222 221
    }
223 222

	
224 223
  public:
225 224
    class MapIt : public Item {
225
      typedef Item Parent;
226

	
226 227
    public:
227

	
228
      typedef Item Parent;
229 228
      typedef typename Map::Value Value;
230 229

	
231 230
      MapIt() {}
232 231

	
233 232
      MapIt(Invalid i) : Parent(i) { }
234 233

	
235 234
      explicit MapIt(Map& _map) : map(_map) {
236 235
        map.graph.first(*this);
237 236
      }
238 237

	
239 238
      MapIt(const Map& _map, const Item& item)
240 239
        : Parent(item), map(_map) {}
241 240

	
242 241
      MapIt& operator++() {
243 242
        map.graph.next(*this);
244 243
        return *this;
245 244
      }
246 245

	
247 246
      typename MapTraits<Map>::ConstReturnValue operator*() const {
248 247
        return map[*this];
249 248
      }
250 249

	
251 250
      typename MapTraits<Map>::ReturnValue operator*() {
252 251
        return map[*this];
253 252
      }
254 253

	
255 254
      void set(const Value& value) {
256 255
        map.set(*this, value);
257 256
      }
258 257

	
259 258
    protected:
260 259
      Map& map;
261 260

	
262 261
    };
263 262

	
264 263
    class ConstMapIt : public Item {
264
      typedef Item Parent;
265

	
265 266
    public:
266 267

	
267
      typedef Item Parent;
268

	
269 268
      typedef typename Map::Value Value;
270 269

	
271 270
      ConstMapIt() {}
272 271

	
273 272
      ConstMapIt(Invalid i) : Parent(i) { }
274 273

	
275 274
      explicit ConstMapIt(Map& _map) : map(_map) {
276 275
        map.graph.first(*this);
277 276
      }
278 277

	
279 278
      ConstMapIt(const Map& _map, const Item& item)
280 279
        : Parent(item), map(_map) {}
281 280

	
282 281
      ConstMapIt& operator++() {
283 282
        map.graph.next(*this);
284 283
        return *this;
285 284
      }
286 285

	
287 286
      typename MapTraits<Map>::ConstReturnValue operator*() const {
288 287
        return map[*this];
289 288
      }
290 289

	
291 290
    protected:
292 291
      const Map& map;
293 292
    };
294 293

	
295 294
    class ItemIt : public Item {
295
      typedef Item Parent;
296

	
296 297
    public:
297 298

	
298
      typedef Item Parent;
299

	
300 299
      ItemIt() {}
301 300

	
302 301
      ItemIt(Invalid i) : Parent(i) { }
303 302

	
304 303
      explicit ItemIt(Map& _map) : map(_map) {
305 304
        map.graph.first(*this);
306 305
      }
307 306

	
308 307
      ItemIt(const Map& _map, const Item& item)
309 308
        : Parent(item), map(_map) {}
310 309

	
311 310
      ItemIt& operator++() {
312 311
        map.graph.next(*this);
313 312
        return *this;
314 313
      }
315 314

	
316 315
    protected:
317 316
      const Map& map;
318 317

	
319 318
    };
320 319

	
321 320
  private:
322 321

	
323
    const Graph& graph;
322
    const GraphType& graph;
324 323

	
325 324
  };
326 325

	
327 326
}
328 327

	
329 328
#endif
Ignore white space 131072 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_VECTOR_MAP_H
20 20
#define LEMON_BITS_VECTOR_MAP_H
21 21

	
22 22
#include <vector>
23 23
#include <algorithm>
24 24

	
25 25
#include <lemon/core.h>
26 26
#include <lemon/bits/alteration_notifier.h>
27 27

	
28 28
#include <lemon/concept_check.h>
29 29
#include <lemon/concepts/maps.h>
30 30

	
31 31
//\ingroup graphbits
32 32
//
33 33
//\file
34 34
//\brief Vector based graph maps.
35 35
namespace lemon {
36 36

	
37 37
  // \ingroup graphbits
38 38
  //
39 39
  // \brief Graph map based on the std::vector storage.
40 40
  //
41 41
  // The VectorMap template class is graph map structure that automatically
42 42
  // updates the map when a key is added to or erased from the graph.
43 43
  // This map type uses std::vector to store the values.
44 44
  //
45 45
  // \tparam _Graph The graph this map is attached to.
46 46
  // \tparam _Item The item type of the graph items.
47 47
  // \tparam _Value The value type of the map.
48 48
  template <typename _Graph, typename _Item, typename _Value>
49 49
  class VectorMap
50 50
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
51 51
  private:
52 52

	
53 53
    // The container type of the map.
54 54
    typedef std::vector<_Value> Container;
55 55

	
56 56
  public:
57 57

	
58 58
    // The graph type of the map.
59
    typedef _Graph Graph;
59
    typedef _Graph GraphType;
60 60
    // The item type of the map.
61 61
    typedef _Item Item;
62 62
    // The reference map tag.
63 63
    typedef True ReferenceMapTag;
64 64

	
65 65
    // The key type of the map.
66 66
    typedef _Item Key;
67 67
    // The value type of the map.
68 68
    typedef _Value Value;
69 69

	
70 70
    // The notifier type.
71 71
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
72 72

	
73 73
    // The map type.
74 74
    typedef VectorMap Map;
75
    // The base class of the map.
76
    typedef typename Notifier::ObserverBase Parent;
77 75

	
78 76
    // The reference type of the map;
79 77
    typedef typename Container::reference Reference;
80 78
    // The const reference type of the map;
81 79
    typedef typename Container::const_reference ConstReference;
82 80

	
81
  private:
82

	
83
    // The base class of the map.
84
    typedef typename Notifier::ObserverBase Parent;
85

	
86
  public:
83 87

	
84 88
    // \brief Constructor to attach the new map into the notifier.
85 89
    //
86 90
    // It constructs a map and attachs it into the notifier.
87 91
    // It adds all the items of the graph to the map.
88
    VectorMap(const Graph& graph) {
92
    VectorMap(const GraphType& graph) {
89 93
      Parent::attach(graph.notifier(Item()));
90 94
      container.resize(Parent::notifier()->maxId() + 1);
91 95
    }
92 96

	
93 97
    // \brief Constructor uses given value to initialize the map.
94 98
    //
95 99
    // It constructs a map uses a given value to initialize the map.
96 100
    // It adds all the items of the graph to the map.
97
    VectorMap(const Graph& graph, const Value& value) {
101
    VectorMap(const GraphType& graph, const Value& value) {
98 102
      Parent::attach(graph.notifier(Item()));
99 103
      container.resize(Parent::notifier()->maxId() + 1, value);
100 104
    }
101 105

	
102 106
  private:
103 107
    // \brief Copy constructor
104 108
    //
105 109
    // Copy constructor.
106 110
    VectorMap(const VectorMap& _copy) : Parent() {
107 111
      if (_copy.attached()) {
108 112
        Parent::attach(*_copy.notifier());
109 113
        container = _copy.container;
110 114
      }
111 115
    }
112 116

	
113 117
    // \brief Assign operator.
114 118
    //
115 119
    // This operator assigns for each item in the map the
116 120
    // value mapped to the same item in the copied map.
117 121
    // The parameter map should be indiced with the same
118 122
    // itemset because this assign operator does not change
119 123
    // the container of the map.
120 124
    VectorMap& operator=(const VectorMap& cmap) {
121 125
      return operator=<VectorMap>(cmap);
122 126
    }
123 127

	
124 128

	
125 129
    // \brief Template assign operator.
126 130
    //
127 131
    // The given parameter should conform to the ReadMap
128 132
    // concecpt and could be indiced by the current item set of
129 133
    // the NodeMap. In this case the value for each item
130 134
    // is assigned by the value of the given ReadMap.
131 135
    template <typename CMap>
132 136
    VectorMap& operator=(const CMap& cmap) {
133 137
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
134 138
      const typename Parent::Notifier* nf = Parent::notifier();
135 139
      Item it;
136 140
      for (nf->first(it); it != INVALID; nf->next(it)) {
137 141
        set(it, cmap[it]);
138 142
      }
139 143
      return *this;
140 144
    }
141 145

	
142 146
  public:
143 147

	
144 148
    // \brief The subcript operator.
145 149
    //
146 150
    // The subscript operator. The map can be subscripted by the
147 151
    // actual items of the graph.
148 152
    Reference operator[](const Key& key) {
149 153
      return container[Parent::notifier()->id(key)];
150 154
    }
151 155

	
152 156
    // \brief The const subcript operator.
153 157
    //
154 158
    // The const subscript operator. The map can be subscripted by the
155 159
    // actual items of the graph.
156 160
    ConstReference operator[](const Key& key) const {
157 161
      return container[Parent::notifier()->id(key)];
158 162
    }
159 163

	
160 164

	
161 165
    // \brief The setter function of the map.
162 166
    //
163 167
    // It the same as operator[](key) = value expression.
164 168
    void set(const Key& key, const Value& value) {
165 169
      (*this)[key] = value;
166 170
    }
167 171

	
168 172
  protected:
169 173

	
170 174
    // \brief Adds a new key to the map.
171 175
    //
172 176
    // It adds a new key to the map. It is called by the observer notifier
173 177
    // and it overrides the add() member function of the observer base.
174 178
    virtual void add(const Key& key) {
175 179
      int id = Parent::notifier()->id(key);
176 180
      if (id >= int(container.size())) {
177 181
        container.resize(id + 1);
178 182
      }
179 183
    }
180 184

	
181 185
    // \brief Adds more new keys to the map.
182 186
    //
183 187
    // It adds more new keys to the map. It is called by the observer notifier
184 188
    // and it overrides the add() member function of the observer base.
185 189
    virtual void add(const std::vector<Key>& keys) {
186 190
      int max = container.size() - 1;
187 191
      for (int i = 0; i < int(keys.size()); ++i) {
188 192
        int id = Parent::notifier()->id(keys[i]);
189 193
        if (id >= max) {
190 194
          max = id;
191 195
        }
192 196
      }
193 197
      container.resize(max + 1);
194 198
    }
195 199

	
196 200
    // \brief Erase a key from the map.
197 201
    //
198 202
    // Erase a key from the map. It is called by the observer notifier
199 203
    // and it overrides the erase() member function of the observer base.
200 204
    virtual void erase(const Key& key) {
201 205
      container[Parent::notifier()->id(key)] = Value();
202 206
    }
203 207

	
204 208
    // \brief Erase more keys from the map.
205 209
    //
206 210
    // It erases more keys from the map. It is called by the observer notifier
207 211
    // and it overrides the erase() member function of the observer base.
208 212
    virtual void erase(const std::vector<Key>& keys) {
209 213
      for (int i = 0; i < int(keys.size()); ++i) {
210 214
        container[Parent::notifier()->id(keys[i])] = Value();
211 215
      }
212 216
    }
213 217

	
214 218
    // \brief Build the map.
215 219
    //
216 220
    // It builds the map. It is called by the observer notifier
217 221
    // and it overrides the build() member function of the observer base.
218 222
    virtual void build() {
219 223
      int size = Parent::notifier()->maxId() + 1;
220 224
      container.reserve(size);
221 225
      container.resize(size);
222 226
    }
223 227

	
224 228
    // \brief Clear the map.
225 229
    //
226 230
    // It erases all items from the map. It is called by the observer notifier
227 231
    // and it overrides the clear() member function of the observer base.
228 232
    virtual void clear() {
229 233
      container.clear();
230 234
    }
231 235

	
232 236
  private:
233 237

	
234 238
    Container container;
235 239

	
236 240
  };
237 241

	
238 242
}
239 243

	
240 244
#endif

Changeset was too big and was cut off... Show full diff

0 comments (0 inline)