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

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

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

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

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

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

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

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

	
19 19
///\ingroup graph_concepts
20 20
///\file
21 21
///\brief The concept of graph components.
22 22

	
23 23
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H
24 24
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H
25 25

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

	
29 29
#include <lemon/bits/alteration_notifier.h>
30 30

	
31 31
namespace lemon {
32 32
  namespace concepts {
33 33

	
34 34
    /// \brief Concept class for \c Node, \c Arc and \c Edge types.
35 35
    ///
36 36
    /// This class describes the concept of \c Node, \c Arc and \c Edge
37 37
    /// subtypes of digraph and graph types.
38 38
    ///
39 39
    /// \note This class is a template class so that we can use it to
40 40
    /// create graph skeleton classes. The reason for this is that \c Node
41 41
    /// and \c Arc (or \c Edge) types should \e not derive from the same 
42 42
    /// base class. For \c Node you should instantiate it with character
43 43
    /// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'.
44 44
#ifndef DOXYGEN
45 45
    template <char sel = '0'>
46 46
#endif
47 47
    class GraphItem {
48 48
    public:
49 49
      /// \brief Default constructor.
50 50
      ///
51 51
      /// Default constructor.
52 52
      /// \warning The default constructor is not required to set
53 53
      /// the item to some well-defined value. So you should consider it
54 54
      /// as uninitialized.
55 55
      GraphItem() {}
56 56

	
57 57
      /// \brief Copy constructor.
58 58
      ///
59 59
      /// Copy constructor.
60 60
      GraphItem(const GraphItem &) {}
61 61

	
62 62
      /// \brief Constructor for conversion from \c INVALID.
63 63
      ///
64 64
      /// Constructor for conversion from \c INVALID.
65 65
      /// It initializes the item to be invalid.
66 66
      /// \sa Invalid for more details.
67 67
      GraphItem(Invalid) {}
68 68

	
69 69
      /// \brief Assignment operator.
70 70
      ///
71 71
      /// Assignment operator for the item.
72 72
      GraphItem& operator=(const GraphItem&) { return *this; }
73 73

	
74 74
      /// \brief Equality operator.
75 75
      ///
76 76
      /// Equality operator.
77 77
      bool operator==(const GraphItem&) const { return false; }
78 78

	
79 79
      /// \brief Inequality operator.
80 80
      ///
81 81
      /// Inequality operator.
82 82
      bool operator!=(const GraphItem&) const { return false; }
83 83

	
84 84
      /// \brief Ordering operator.
85 85
      ///
86 86
      /// This operator defines an ordering of the items.
87 87
      /// It makes possible to use graph item types as key types in 
88 88
      /// associative containers (e.g. \c std::map).
89 89
      ///
90 90
      /// \note This operator only have to define some strict ordering of
91 91
      /// the items; this order has nothing to do with the iteration
92 92
      /// ordering of the items.
93 93
      bool operator<(const GraphItem&) const { return false; }
94 94

	
95 95
      template<typename _GraphItem>
96 96
      struct Constraints {
97 97
        void constraints() {
98 98
          _GraphItem i1;
99 99
          _GraphItem i2 = i1;
100 100
          _GraphItem i3 = INVALID;
101 101

	
102 102
          i1 = i2 = i3;
103 103

	
104 104
          bool b;
105 105
          b = (ia == ib) && (ia != ib);
106 106
          b = (ia == INVALID) && (ib != INVALID);
107 107
          b = (ia < ib);
108 108
        }
109 109

	
110 110
        const _GraphItem &ia;
111 111
        const _GraphItem &ib;
112 112
      };
113 113
    };
114 114

	
115 115
    /// \brief Base skeleton class for directed graphs.
116 116
    ///
117 117
    /// This class describes the base interface of directed graph types.
118 118
    /// All digraph %concepts have to conform to this class.
119 119
    /// It just provides types for nodes and arcs and functions 
120 120
    /// to get the source and the target nodes of arcs.
121 121
    class BaseDigraphComponent {
122 122
    public:
123 123

	
124 124
      typedef BaseDigraphComponent Digraph;
125 125

	
126 126
      /// \brief Node class of the digraph.
127 127
      ///
128 128
      /// This class represents the nodes of the digraph.
129 129
      typedef GraphItem<'n'> Node;
130 130

	
131 131
      /// \brief Arc class of the digraph.
132 132
      ///
133 133
      /// This class represents the arcs of the digraph.
134 134
      typedef GraphItem<'a'> Arc;
135 135

	
136 136
      /// \brief Return the source node of an arc.
137 137
      ///
138 138
      /// This function returns the source node of an arc.
139 139
      Node source(const Arc&) const { return INVALID; }
140 140

	
141 141
      /// \brief Return the target node of an arc.
142 142
      ///
143 143
      /// This function returns the target node of an arc.
144 144
      Node target(const Arc&) const { return INVALID; }
145 145

	
146 146
      /// \brief Return the opposite node on the given arc.
147 147
      ///
148 148
      /// This function returns the opposite node on the given arc.
149 149
      Node oppositeNode(const Node&, const Arc&) const {
150 150
        return INVALID;
151 151
      }
152 152

	
153 153
      template <typename _Digraph>
154 154
      struct Constraints {
155 155
        typedef typename _Digraph::Node Node;
156 156
        typedef typename _Digraph::Arc Arc;
157 157

	
158 158
        void constraints() {
159 159
          checkConcept<GraphItem<'n'>, Node>();
160 160
          checkConcept<GraphItem<'a'>, Arc>();
161 161
          {
162 162
            Node n;
163 163
            Arc e(INVALID);
164 164
            n = digraph.source(e);
165 165
            n = digraph.target(e);
166 166
            n = digraph.oppositeNode(n, e);
167 167
          }
168 168
        }
169 169

	
170 170
        const _Digraph& digraph;
171 171
      };
172 172
    };
173 173

	
174 174
    /// \brief Base skeleton class for undirected graphs.
175 175
    ///
176 176
    /// This class describes the base interface of undirected graph types.
177 177
    /// All graph %concepts have to conform to this class.
178 178
    /// It extends the interface of \ref BaseDigraphComponent with an
179 179
    /// \c Edge type and functions to get the end nodes of edges,
180 180
    /// to convert from arcs to edges and to get both direction of edges.
181 181
    class BaseGraphComponent : public BaseDigraphComponent {
182 182
    public:
183

	
184
      typedef BaseGraphComponent Graph;
185

	
183 186
      typedef BaseDigraphComponent::Node Node;
184 187
      typedef BaseDigraphComponent::Arc Arc;
185 188

	
186 189
      /// \brief Undirected edge class of the graph.
187 190
      ///
188 191
      /// This class represents the undirected edges of the graph.
189 192
      /// Undirected graphs can be used as directed graphs, each edge is
190 193
      /// represented by two opposite directed arcs.
191 194
      class Edge : public GraphItem<'e'> {
192
      public:
193 195
        typedef GraphItem<'e'> Parent;
194 196

	
197
      public:
195 198
        /// \brief Default constructor.
196 199
        ///
197 200
        /// Default constructor.
198 201
        /// \warning The default constructor is not required to set
199 202
        /// the item to some well-defined value. So you should consider it
200 203
        /// as uninitialized.
201 204
        Edge() {}
202 205

	
203 206
        /// \brief Copy constructor.
204 207
        ///
205 208
        /// Copy constructor.
206 209
        Edge(const Edge &) : Parent() {}
207 210

	
208 211
        /// \brief Constructor for conversion from \c INVALID.
209 212
        ///
210 213
        /// Constructor for conversion from \c INVALID.
211 214
        /// It initializes the item to be invalid.
212 215
        /// \sa Invalid for more details.
213 216
        Edge(Invalid) {}
214 217

	
215 218
        /// \brief Constructor for conversion from an arc.
216 219
        ///
217 220
        /// Constructor for conversion from an arc.
218 221
        /// Besides the core graph item functionality each arc should
219 222
        /// be convertible to the represented edge.
220 223
        Edge(const Arc&) {}
221 224

	
222 225
        /// \brief Assign an arc to an edge.
223 226
        ///
224 227
        /// This function assigns an arc to an edge.
225 228
        /// Besides the core graph item functionality each arc should
226 229
        /// be convertible to the represented edge.
227 230
        Edge& operator=(const Arc&) { return *this; }
228 231
      };
229 232

	
230 233
      /// \brief Return one end node of an edge.
231 234
      ///
232 235
      /// This function returns one end node of an edge.
233 236
      Node u(const Edge&) const { return INVALID; }
234 237

	
235 238
      /// \brief Return the other end node of an edge.
236 239
      ///
237 240
      /// This function returns the other end node of an edge.
238 241
      Node v(const Edge&) const { return INVALID; }
239 242

	
240 243
      /// \brief Return a directed arc related to an edge.
241 244
      ///
242 245
      /// This function returns a directed arc from its direction and the
243 246
      /// represented edge.
244 247
      Arc direct(const Edge&, bool) const { return INVALID; }
245 248

	
246 249
      /// \brief Return a directed arc related to an edge.
247 250
      ///
248 251
      /// This function returns a directed arc from its source node and the
249 252
      /// represented edge.
250 253
      Arc direct(const Edge&, const Node&) const { return INVALID; }
251 254

	
252 255
      /// \brief Return the direction of the arc.
253 256
      ///
254 257
      /// Returns the direction of the arc. Each arc represents an
255 258
      /// edge with a direction. It gives back the
256 259
      /// direction.
257 260
      bool direction(const Arc&) const { return true; }
258 261

	
259 262
      /// \brief Return the opposite arc.
260 263
      ///
261 264
      /// This function returns the opposite arc, i.e. the arc representing
262 265
      /// the same edge and has opposite direction.
263 266
      Arc oppositeArc(const Arc&) const { return INVALID; }
264 267

	
265 268
      template <typename _Graph>
266 269
      struct Constraints {
267 270
        typedef typename _Graph::Node Node;
268 271
        typedef typename _Graph::Arc Arc;
269 272
        typedef typename _Graph::Edge Edge;
270 273

	
271 274
        void constraints() {
272 275
          checkConcept<BaseDigraphComponent, _Graph>();
273 276
          checkConcept<GraphItem<'e'>, Edge>();
274 277
          {
275 278
            Node n;
276 279
            Edge ue(INVALID);
277 280
            Arc e;
278 281
            n = graph.u(ue);
279 282
            n = graph.v(ue);
280 283
            e = graph.direct(ue, true);
281 284
            e = graph.direct(ue, false);
282 285
            e = graph.direct(ue, n);
283 286
            e = graph.oppositeArc(e);
284 287
            ue = e;
285 288
            bool d = graph.direction(e);
286 289
            ignore_unused_variable_warning(d);
287 290
          }
288 291
        }
289 292

	
290 293
        const _Graph& graph;
291 294
      };
292 295

	
293 296
    };
294 297

	
295 298
    /// \brief Skeleton class for \e idable directed graphs.
296 299
    ///
297 300
    /// This class describes the interface of \e idable directed graphs.
298 301
    /// It extends \ref BaseDigraphComponent with the core ID functions.
299 302
    /// The ids of the items must be unique and immutable.
300 303
    /// This concept is part of the Digraph concept.
301 304
    template <typename BAS = BaseDigraphComponent>
302 305
    class IDableDigraphComponent : public BAS {
303 306
    public:
304 307

	
305 308
      typedef BAS Base;
306 309
      typedef typename Base::Node Node;
307 310
      typedef typename Base::Arc Arc;
308 311

	
309 312
      /// \brief Return a unique integer id for the given node.
310 313
      ///
311 314
      /// This function returns a unique integer id for the given node.
312 315
      int id(const Node&) const { return -1; }
313 316

	
314 317
      /// \brief Return the node by its unique id.
315 318
      ///
316 319
      /// This function returns the node by its unique id.
317 320
      /// If the digraph does not contain a node with the given id,
318 321
      /// then the result of the function is undefined.
319 322
      Node nodeFromId(int) const { return INVALID; }
320 323

	
321 324
      /// \brief Return a unique integer id for the given arc.
322 325
      ///
323 326
      /// This function returns a unique integer id for the given arc.
324 327
      int id(const Arc&) const { return -1; }
325 328

	
326 329
      /// \brief Return the arc by its unique id.
327 330
      ///
328 331
      /// This function returns the arc by its unique id.
329 332
      /// If the digraph does not contain an arc with the given id,
330 333
      /// then the result of the function is undefined.
331 334
      Arc arcFromId(int) const { return INVALID; }
332 335

	
333 336
      /// \brief Return an integer greater or equal to the maximum
334 337
      /// node id.
335 338
      ///
336 339
      /// This function returns an integer greater or equal to the
337 340
      /// maximum node id.
338 341
      int maxNodeId() const { return -1; }
339 342

	
340 343
      /// \brief Return an integer greater or equal to the maximum
341 344
      /// arc id.
342 345
      ///
343 346
      /// This function returns an integer greater or equal to the
344 347
      /// maximum arc id.
345 348
      int maxArcId() const { return -1; }
346 349

	
347 350
      template <typename _Digraph>
348 351
      struct Constraints {
349 352

	
350 353
        void constraints() {
351 354
          checkConcept<Base, _Digraph >();
352 355
          typename _Digraph::Node node;
353 356
          int nid = digraph.id(node);
354 357
          nid = digraph.id(node);
355 358
          node = digraph.nodeFromId(nid);
356 359
          typename _Digraph::Arc arc;
357 360
          int eid = digraph.id(arc);
358 361
          eid = digraph.id(arc);
359 362
          arc = digraph.arcFromId(eid);
360 363

	
361 364
          nid = digraph.maxNodeId();
362 365
          ignore_unused_variable_warning(nid);
363 366
          eid = digraph.maxArcId();
364 367
          ignore_unused_variable_warning(eid);
365 368
        }
366 369

	
367 370
        const _Digraph& digraph;
368 371
      };
369 372
    };
370 373

	
371 374
    /// \brief Skeleton class for \e idable undirected graphs.
372 375
    ///
373 376
    /// This class describes the interface of \e idable undirected
374 377
    /// graphs. It extends \ref IDableDigraphComponent with the core ID
375 378
    /// functions of undirected graphs.
376 379
    /// The ids of the items must be unique and immutable.
377 380
    /// This concept is part of the Graph concept.
378 381
    template <typename BAS = BaseGraphComponent>
379 382
    class IDableGraphComponent : public IDableDigraphComponent<BAS> {
380 383
    public:
381 384

	
382 385
      typedef BAS Base;
383 386
      typedef typename Base::Edge Edge;
384 387

	
385 388
      using IDableDigraphComponent<Base>::id;
386 389

	
387 390
      /// \brief Return a unique integer id for the given edge.
388 391
      ///
389 392
      /// This function returns a unique integer id for the given edge.
390 393
      int id(const Edge&) const { return -1; }
391 394

	
392 395
      /// \brief Return the edge by its unique id.
393 396
      ///
394 397
      /// This function returns the edge by its unique id.
395 398
      /// If the graph does not contain an edge with the given id,
396 399
      /// then the result of the function is undefined.
397 400
      Edge edgeFromId(int) const { return INVALID; }
398 401

	
399 402
      /// \brief Return an integer greater or equal to the maximum
400 403
      /// edge id.
401 404
      ///
402 405
      /// This function returns an integer greater or equal to the
403 406
      /// maximum edge id.
404 407
      int maxEdgeId() const { return -1; }
405 408

	
406 409
      template <typename _Graph>
407 410
      struct Constraints {
408 411

	
409 412
        void constraints() {
410 413
          checkConcept<IDableDigraphComponent<Base>, _Graph >();
411 414
          typename _Graph::Edge edge;
412 415
          int ueid = graph.id(edge);
413 416
          ueid = graph.id(edge);
414 417
          edge = graph.edgeFromId(ueid);
415 418
          ueid = graph.maxEdgeId();
416 419
          ignore_unused_variable_warning(ueid);
417 420
        }
418 421

	
419 422
        const _Graph& graph;
420 423
      };
421 424
    };
422 425

	
423 426
    /// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types.
424 427
    ///
425 428
    /// This class describes the concept of \c NodeIt, \c ArcIt and 
426 429
    /// \c EdgeIt subtypes of digraph and graph types.
427 430
    template <typename GR, typename Item>
428 431
    class GraphItemIt : public Item {
429 432
    public:
430 433
      /// \brief Default constructor.
431 434
      ///
432 435
      /// Default constructor.
433 436
      /// \warning The default constructor is not required to set
434 437
      /// the iterator to some well-defined value. So you should consider it
435 438
      /// as uninitialized.
436 439
      GraphItemIt() {}
437 440

	
438 441
      /// \brief Copy constructor.
439 442
      ///
440 443
      /// Copy constructor.
441 444
      GraphItemIt(const GraphItemIt& it) : Item(it) {}
442 445

	
443 446
      /// \brief Constructor that sets the iterator to the first item.
444 447
      ///
445 448
      /// Constructor that sets the iterator to the first item.
446 449
      explicit GraphItemIt(const GR&) {}
447 450

	
448 451
      /// \brief Constructor for conversion from \c INVALID.
449 452
      ///
450 453
      /// Constructor for conversion from \c INVALID.
451 454
      /// It initializes the iterator to be invalid.
452 455
      /// \sa Invalid for more details.
453 456
      GraphItemIt(Invalid) {}
454 457

	
455 458
      /// \brief Assignment operator.
456 459
      ///
457 460
      /// Assignment operator for the iterator.
458 461
      GraphItemIt& operator=(const GraphItemIt&) { return *this; }
459 462

	
460 463
      /// \brief Increment the iterator.
461 464
      ///
462 465
      /// This operator increments the iterator, i.e. assigns it to the
463 466
      /// next item.
464 467
      GraphItemIt& operator++() { return *this; }
465 468
 
466 469
      /// \brief Equality operator
467 470
      ///
468 471
      /// Equality operator.
469 472
      /// Two iterators are equal if and only if they point to the
470 473
      /// same object or both are invalid.
471 474
      bool operator==(const GraphItemIt&) const { return true;}
472 475

	
473 476
      /// \brief Inequality operator
474 477
      ///
475 478
      /// Inequality operator.
476 479
      /// Two iterators are equal if and only if they point to the
477 480
      /// same object or both are invalid.
478 481
      bool operator!=(const GraphItemIt&) const { return true;}
479 482

	
480 483
      template<typename _GraphItemIt>
481 484
      struct Constraints {
482 485
        void constraints() {
483 486
          checkConcept<GraphItem<>, _GraphItemIt>();
484 487
          _GraphItemIt it1(g);
485 488
          _GraphItemIt it2;
486 489
          _GraphItemIt it3 = it1;
487 490
          _GraphItemIt it4 = INVALID;
488 491

	
489 492
          it2 = ++it1;
490 493
          ++it2 = it1;
491 494
          ++(++it1);
492 495

	
493 496
          Item bi = it1;
494 497
          bi = it2;
495 498
        }
496 499
        const GR& g;
497 500
      };
498 501
    };
499 502

	
500 503
    /// \brief Concept class for \c InArcIt, \c OutArcIt and 
501 504
    /// \c IncEdgeIt types.
502 505
    ///
503 506
    /// This class describes the concept of \c InArcIt, \c OutArcIt 
504 507
    /// and \c IncEdgeIt subtypes of digraph and graph types.
505 508
    ///
506 509
    /// \note Since these iterator classes do not inherit from the same
507 510
    /// base class, there is an additional template parameter (selector)
508 511
    /// \c sel. For \c InArcIt you should instantiate it with character 
509 512
    /// \c 'i', for \c OutArcIt with \c 'o' and for \c IncEdgeIt with \c 'e'.
510 513
    template <typename GR,
511 514
              typename Item = typename GR::Arc,
512 515
              typename Base = typename GR::Node,
513 516
              char sel = '0'>
514 517
    class GraphIncIt : public Item {
515 518
    public:
516 519
      /// \brief Default constructor.
517 520
      ///
518 521
      /// Default constructor.
519 522
      /// \warning The default constructor is not required to set
520 523
      /// the iterator to some well-defined value. So you should consider it
521 524
      /// as uninitialized.
522 525
      GraphIncIt() {}
523 526

	
524 527
      /// \brief Copy constructor.
525 528
      ///
526 529
      /// Copy constructor.
527 530
      GraphIncIt(const GraphIncIt& it) : Item(it) {}
528 531

	
529 532
      /// \brief Constructor that sets the iterator to the first 
530 533
      /// incoming or outgoing arc.
531 534
      ///
532 535
      /// Constructor that sets the iterator to the first arc 
533 536
      /// incoming to or outgoing from the given node.
534 537
      explicit GraphIncIt(const GR&, const Base&) {}
535 538

	
536 539
      /// \brief Constructor for conversion from \c INVALID.
537 540
      ///
538 541
      /// Constructor for conversion from \c INVALID.
539 542
      /// It initializes the iterator to be invalid.
540 543
      /// \sa Invalid for more details.
541 544
      GraphIncIt(Invalid) {}
542 545

	
543 546
      /// \brief Assignment operator.
544 547
      ///
545 548
      /// Assignment operator for the iterator.
546 549
      GraphIncIt& operator=(const GraphIncIt&) { return *this; }
547 550

	
548 551
      /// \brief Increment the iterator.
549 552
      ///
550 553
      /// This operator increments the iterator, i.e. assigns it to the
551 554
      /// next arc incoming to or outgoing from the given node.
552 555
      GraphIncIt& operator++() { return *this; }
553 556

	
554 557
      /// \brief Equality operator
555 558
      ///
556 559
      /// Equality operator.
557 560
      /// Two iterators are equal if and only if they point to the
558 561
      /// same object or both are invalid.
559 562
      bool operator==(const GraphIncIt&) const { return true;}
560 563

	
561 564
      /// \brief Inequality operator
562 565
      ///
563 566
      /// Inequality operator.
564 567
      /// Two iterators are equal if and only if they point to the
565 568
      /// same object or both are invalid.
566 569
      bool operator!=(const GraphIncIt&) const { return true;}
567 570

	
568 571
      template <typename _GraphIncIt>
569 572
      struct Constraints {
570 573
        void constraints() {
571 574
          checkConcept<GraphItem<sel>, _GraphIncIt>();
572 575
          _GraphIncIt it1(graph, node);
573 576
          _GraphIncIt it2;
574 577
          _GraphIncIt it3 = it1;
575 578
          _GraphIncIt it4 = INVALID;
576 579

	
577 580
          it2 = ++it1;
578 581
          ++it2 = it1;
... ...
@@ -610,905 +613,903 @@
610 613

	
611 614
      /// \brief Return the first node.
612 615
      ///
613 616
      /// This function gives back the first node in the iteration order.
614 617
      void first(Node&) const {}
615 618

	
616 619
      /// \brief Return the next node.
617 620
      ///
618 621
      /// This function gives back the next node in the iteration order.
619 622
      void next(Node&) const {}
620 623

	
621 624
      /// \brief Return the first arc.
622 625
      ///
623 626
      /// This function gives back the first arc in the iteration order.
624 627
      void first(Arc&) const {}
625 628

	
626 629
      /// \brief Return the next arc.
627 630
      ///
628 631
      /// This function gives back the next arc in the iteration order.
629 632
      void next(Arc&) const {}
630 633

	
631 634
      /// \brief Return the first arc incomming to the given node.
632 635
      ///
633 636
      /// This function gives back the first arc incomming to the
634 637
      /// given node.
635 638
      void firstIn(Arc&, const Node&) const {}
636 639

	
637 640
      /// \brief Return the next arc incomming to the given node.
638 641
      ///
639 642
      /// This function gives back the next arc incomming to the
640 643
      /// given node.
641 644
      void nextIn(Arc&) const {}
642 645

	
643 646
      /// \brief Return the first arc outgoing form the given node.
644 647
      ///
645 648
      /// This function gives back the first arc outgoing form the
646 649
      /// given node.
647 650
      void firstOut(Arc&, const Node&) const {}
648 651

	
649 652
      /// \brief Return the next arc outgoing form the given node.
650 653
      ///
651 654
      /// This function gives back the next arc outgoing form the
652 655
      /// given node.
653 656
      void nextOut(Arc&) const {}
654 657

	
655 658
      /// @}
656 659

	
657 660
      /// \name Class Based Iteration
658 661
      ///
659 662
      /// This interface provides iterator classes for digraph items.
660 663
      ///
661 664
      /// @{
662 665

	
663 666
      /// \brief This iterator goes through each node.
664 667
      ///
665 668
      /// This iterator goes through each node.
666 669
      ///
667 670
      typedef GraphItemIt<Digraph, Node> NodeIt;
668 671

	
669 672
      /// \brief This iterator goes through each arc.
670 673
      ///
671 674
      /// This iterator goes through each arc.
672 675
      ///
673 676
      typedef GraphItemIt<Digraph, Arc> ArcIt;
674 677

	
675 678
      /// \brief This iterator goes trough the incoming arcs of a node.
676 679
      ///
677 680
      /// This iterator goes trough the \e incoming arcs of a certain node
678 681
      /// of a digraph.
679 682
      typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt;
680 683

	
681 684
      /// \brief This iterator goes trough the outgoing arcs of a node.
682 685
      ///
683 686
      /// This iterator goes trough the \e outgoing arcs of a certain node
684 687
      /// of a digraph.
685 688
      typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt;
686 689

	
687 690
      /// \brief The base node of the iterator.
688 691
      ///
689 692
      /// This function gives back the base node of the iterator.
690 693
      /// It is always the target node of the pointed arc.
691 694
      Node baseNode(const InArcIt&) const { return INVALID; }
692 695

	
693 696
      /// \brief The running node of the iterator.
694 697
      ///
695 698
      /// This function gives back the running node of the iterator.
696 699
      /// It is always the source node of the pointed arc.
697 700
      Node runningNode(const InArcIt&) const { return INVALID; }
698 701

	
699 702
      /// \brief The base node of the iterator.
700 703
      ///
701 704
      /// This function gives back the base node of the iterator.
702 705
      /// It is always the source node of the pointed arc.
703 706
      Node baseNode(const OutArcIt&) const { return INVALID; }
704 707

	
705 708
      /// \brief The running node of the iterator.
706 709
      ///
707 710
      /// This function gives back the running node of the iterator.
708 711
      /// It is always the target node of the pointed arc.
709 712
      Node runningNode(const OutArcIt&) const { return INVALID; }
710 713

	
711 714
      /// @}
712 715

	
713 716
      template <typename _Digraph>
714 717
      struct Constraints {
715 718
        void constraints() {
716 719
          checkConcept<Base, _Digraph>();
717 720

	
718 721
          {
719 722
            typename _Digraph::Node node(INVALID);
720 723
            typename _Digraph::Arc arc(INVALID);
721 724
            {
722 725
              digraph.first(node);
723 726
              digraph.next(node);
724 727
            }
725 728
            {
726 729
              digraph.first(arc);
727 730
              digraph.next(arc);
728 731
            }
729 732
            {
730 733
              digraph.firstIn(arc, node);
731 734
              digraph.nextIn(arc);
732 735
            }
733 736
            {
734 737
              digraph.firstOut(arc, node);
735 738
              digraph.nextOut(arc);
736 739
            }
737 740
          }
738 741

	
739 742
          {
740 743
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>,
741 744
              typename _Digraph::ArcIt >();
742 745
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>,
743 746
              typename _Digraph::NodeIt >();
744 747
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
745 748
              typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>();
746 749
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
747 750
              typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>();
748 751

	
749 752
            typename _Digraph::Node n;
750 753
            const typename _Digraph::InArcIt iait(INVALID);
751 754
            const typename _Digraph::OutArcIt oait(INVALID);
752 755
            n = digraph.baseNode(iait);
753 756
            n = digraph.runningNode(iait);
754 757
            n = digraph.baseNode(oait);
755 758
            n = digraph.runningNode(oait);
756 759
            ignore_unused_variable_warning(n);
757 760
          }
758 761
        }
759 762

	
760 763
        const _Digraph& digraph;
761 764
      };
762 765
    };
763 766

	
764 767
    /// \brief Skeleton class for iterable undirected graphs.
765 768
    ///
766 769
    /// This class describes the interface of iterable undirected
767 770
    /// graphs. It extends \ref IterableDigraphComponent with the core
768 771
    /// iterable interface of undirected graphs.
769 772
    /// This concept is part of the Graph concept.
770 773
    template <typename BAS = BaseGraphComponent>
771 774
    class IterableGraphComponent : public IterableDigraphComponent<BAS> {
772 775
    public:
773 776

	
774 777
      typedef BAS Base;
775 778
      typedef typename Base::Node Node;
776 779
      typedef typename Base::Arc Arc;
777 780
      typedef typename Base::Edge Edge;
778 781

	
779 782

	
780 783
      typedef IterableGraphComponent Graph;
781 784

	
782 785
      /// \name Base Iteration
783 786
      ///
784 787
      /// This interface provides functions for iteration on edges.
785 788
      ///
786 789
      /// @{
787 790

	
788 791
      using IterableDigraphComponent<Base>::first;
789 792
      using IterableDigraphComponent<Base>::next;
790 793

	
791 794
      /// \brief Return the first edge.
792 795
      ///
793 796
      /// This function gives back the first edge in the iteration order.
794 797
      void first(Edge&) const {}
795 798

	
796 799
      /// \brief Return the next edge.
797 800
      ///
798 801
      /// This function gives back the next edge in the iteration order.
799 802
      void next(Edge&) const {}
800 803

	
801 804
      /// \brief Return the first edge incident to the given node.
802 805
      ///
803 806
      /// This function gives back the first edge incident to the given 
804 807
      /// node. The bool parameter gives back the direction for which the
805 808
      /// source node of the directed arc representing the edge is the 
806 809
      /// given node.
807 810
      void firstInc(Edge&, bool&, const Node&) const {}
808 811

	
809 812
      /// \brief Gives back the next of the edges from the
810 813
      /// given node.
811 814
      ///
812 815
      /// This function gives back the next edge incident to the given 
813 816
      /// node. The bool parameter should be used as \c firstInc() use it.
814 817
      void nextInc(Edge&, bool&) const {}
815 818

	
816 819
      using IterableDigraphComponent<Base>::baseNode;
817 820
      using IterableDigraphComponent<Base>::runningNode;
818 821

	
819 822
      /// @}
820 823

	
821 824
      /// \name Class Based Iteration
822 825
      ///
823 826
      /// This interface provides iterator classes for edges.
824 827
      ///
825 828
      /// @{
826 829

	
827 830
      /// \brief This iterator goes through each edge.
828 831
      ///
829 832
      /// This iterator goes through each edge.
830 833
      typedef GraphItemIt<Graph, Edge> EdgeIt;
831 834

	
832 835
      /// \brief This iterator goes trough the incident edges of a
833 836
      /// node.
834 837
      ///
835 838
      /// This iterator goes trough the incident edges of a certain
836 839
      /// node of a graph.
837 840
      typedef GraphIncIt<Graph, Edge, Node, 'e'> IncEdgeIt;
838 841

	
839 842
      /// \brief The base node of the iterator.
840 843
      ///
841 844
      /// This function gives back the base node of the iterator.
842 845
      Node baseNode(const IncEdgeIt&) const { return INVALID; }
843 846

	
844 847
      /// \brief The running node of the iterator.
845 848
      ///
846 849
      /// This function gives back the running node of the iterator.
847 850
      Node runningNode(const IncEdgeIt&) const { return INVALID; }
848 851

	
849 852
      /// @}
850 853

	
851 854
      template <typename _Graph>
852 855
      struct Constraints {
853 856
        void constraints() {
854 857
          checkConcept<IterableDigraphComponent<Base>, _Graph>();
855 858

	
856 859
          {
857 860
            typename _Graph::Node node(INVALID);
858 861
            typename _Graph::Edge edge(INVALID);
859 862
            bool dir;
860 863
            {
861 864
              graph.first(edge);
862 865
              graph.next(edge);
863 866
            }
864 867
            {
865 868
              graph.firstInc(edge, dir, node);
866 869
              graph.nextInc(edge, dir);
867 870
            }
868 871

	
869 872
          }
870 873

	
871 874
          {
872 875
            checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
873 876
              typename _Graph::EdgeIt >();
874 877
            checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
875 878
              typename _Graph::Node, 'e'>, typename _Graph::IncEdgeIt>();
876 879

	
877 880
            typename _Graph::Node n;
878 881
            const typename _Graph::IncEdgeIt ieit(INVALID);
879 882
            n = graph.baseNode(ieit);
880 883
            n = graph.runningNode(ieit);
881 884
          }
882 885
        }
883 886

	
884 887
        const _Graph& graph;
885 888
      };
886 889
    };
887 890

	
888 891
    /// \brief Skeleton class for alterable directed graphs.
889 892
    ///
890 893
    /// This class describes the interface of alterable directed
891 894
    /// graphs. It extends \ref BaseDigraphComponent with the alteration
892 895
    /// notifier interface. It implements
893 896
    /// an observer-notifier pattern for each digraph item. More
894 897
    /// obsevers can be registered into the notifier and whenever an
895 898
    /// alteration occured in the digraph all the observers will be
896 899
    /// notified about it.
897 900
    template <typename BAS = BaseDigraphComponent>
898 901
    class AlterableDigraphComponent : public BAS {
899 902
    public:
900 903

	
901 904
      typedef BAS Base;
902 905
      typedef typename Base::Node Node;
903 906
      typedef typename Base::Arc Arc;
904 907

	
905 908

	
906 909
      /// Node alteration notifier class.
907 910
      typedef AlterationNotifier<AlterableDigraphComponent, Node>
908 911
      NodeNotifier;
909 912
      /// Arc alteration notifier class.
910 913
      typedef AlterationNotifier<AlterableDigraphComponent, Arc>
911 914
      ArcNotifier;
912 915

	
913 916
      /// \brief Return the node alteration notifier.
914 917
      ///
915 918
      /// This function gives back the node alteration notifier.
916 919
      NodeNotifier& notifier(Node) const {
917 920
         return NodeNotifier();
918 921
      }
919 922

	
920 923
      /// \brief Return the arc alteration notifier.
921 924
      ///
922 925
      /// This function gives back the arc alteration notifier.
923 926
      ArcNotifier& notifier(Arc) const {
924 927
        return ArcNotifier();
925 928
      }
926 929

	
927 930
      template <typename _Digraph>
928 931
      struct Constraints {
929 932
        void constraints() {
930 933
          checkConcept<Base, _Digraph>();
931 934
          typename _Digraph::NodeNotifier& nn
932 935
            = digraph.notifier(typename _Digraph::Node());
933 936

	
934 937
          typename _Digraph::ArcNotifier& en
935 938
            = digraph.notifier(typename _Digraph::Arc());
936 939

	
937 940
          ignore_unused_variable_warning(nn);
938 941
          ignore_unused_variable_warning(en);
939 942
        }
940 943

	
941 944
        const _Digraph& digraph;
942 945
      };
943 946
    };
944 947

	
945 948
    /// \brief Skeleton class for alterable undirected graphs.
946 949
    ///
947 950
    /// This class describes the interface of alterable undirected
948 951
    /// graphs. It extends \ref AlterableDigraphComponent with the alteration
949 952
    /// notifier interface of undirected graphs. It implements
950 953
    /// an observer-notifier pattern for the edges. More
951 954
    /// obsevers can be registered into the notifier and whenever an
952 955
    /// alteration occured in the graph all the observers will be
953 956
    /// notified about it.
954 957
    template <typename BAS = BaseGraphComponent>
955 958
    class AlterableGraphComponent : public AlterableDigraphComponent<BAS> {
956 959
    public:
957 960

	
958 961
      typedef BAS Base;
959 962
      typedef typename Base::Edge Edge;
960 963

	
961 964

	
962 965
      /// Edge alteration notifier class.
963 966
      typedef AlterationNotifier<AlterableGraphComponent, Edge>
964 967
      EdgeNotifier;
965 968

	
966 969
      /// \brief Return the edge alteration notifier.
967 970
      ///
968 971
      /// This function gives back the edge alteration notifier.
969 972
      EdgeNotifier& notifier(Edge) const {
970 973
        return EdgeNotifier();
971 974
      }
972 975

	
973 976
      template <typename _Graph>
974 977
      struct Constraints {
975 978
        void constraints() {
976 979
          checkConcept<AlterableDigraphComponent<Base>, _Graph>();
977 980
          typename _Graph::EdgeNotifier& uen
978 981
            = graph.notifier(typename _Graph::Edge());
979 982
          ignore_unused_variable_warning(uen);
980 983
        }
981 984

	
982 985
        const _Graph& graph;
983 986
      };
984 987
    };
985 988

	
986 989
    /// \brief Concept class for standard graph maps.
987 990
    ///
988 991
    /// This class describes the concept of standard graph maps, i.e.
989 992
    /// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and 
990 993
    /// graph types, which can be used for associating data to graph items.
991 994
    /// The standard graph maps must conform to the ReferenceMap concept.
992 995
    template <typename GR, typename K, typename V>
993 996
    class GraphMap : public ReferenceMap<K, V, V&, const V&> {
997
      typedef ReferenceMap<K, V, V&, const V&> Parent;
998

	
994 999
    public:
995 1000

	
996
      typedef ReadWriteMap<K, V> Parent;
997

	
998
      /// The graph type of the map.
999
      typedef GR Graph;
1000 1001
      /// The key type of the map.
1001 1002
      typedef K Key;
1002 1003
      /// The value type of the map.
1003 1004
      typedef V Value;
1004 1005
      /// The reference type of the map.
1005 1006
      typedef Value& Reference;
1006 1007
      /// The const reference type of the map.
1007 1008
      typedef const Value& ConstReference;
1008 1009

	
1009 1010
      // The reference map tag.
1010 1011
      typedef True ReferenceMapTag;
1011 1012

	
1012 1013
      /// \brief Construct a new map.
1013 1014
      ///
1014 1015
      /// Construct a new map for the graph.
1015
      explicit GraphMap(const Graph&) {}
1016
      explicit GraphMap(const GR&) {}
1016 1017
      /// \brief Construct a new map with default value.
1017 1018
      ///
1018 1019
      /// Construct a new map for the graph and initalize the values.
1019
      GraphMap(const Graph&, const Value&) {}
1020
      GraphMap(const GR&, const Value&) {}
1020 1021

	
1021 1022
    private:
1022 1023
      /// \brief Copy constructor.
1023 1024
      ///
1024 1025
      /// Copy Constructor.
1025 1026
      GraphMap(const GraphMap&) : Parent() {}
1026 1027

	
1027 1028
      /// \brief Assignment operator.
1028 1029
      ///
1029 1030
      /// Assignment operator. It does not mofify the underlying graph,
1030 1031
      /// it just iterates on the current item set and set the  map
1031 1032
      /// with the value returned by the assigned map.
1032 1033
      template <typename CMap>
1033 1034
      GraphMap& operator=(const CMap&) {
1034 1035
        checkConcept<ReadMap<Key, Value>, CMap>();
1035 1036
        return *this;
1036 1037
      }
1037 1038

	
1038 1039
    public:
1039 1040
      template<typename _Map>
1040 1041
      struct Constraints {
1041 1042
        void constraints() {
1042 1043
          checkConcept
1043 1044
            <ReferenceMap<Key, Value, Value&, const Value&>, _Map>();
1044 1045
          _Map m1(g);
1045 1046
          _Map m2(g,t);
1046 1047
          
1047 1048
          // Copy constructor
1048 1049
          // _Map m3(m);
1049 1050

	
1050 1051
          // Assignment operator
1051 1052
          // ReadMap<Key, Value> cmap;
1052 1053
          // m3 = cmap;
1053 1054

	
1054 1055
          ignore_unused_variable_warning(m1);
1055 1056
          ignore_unused_variable_warning(m2);
1056 1057
          // ignore_unused_variable_warning(m3);
1057 1058
        }
1058 1059

	
1059 1060
        const _Map &m;
1060
        const Graph &g;
1061
        const GR &g;
1061 1062
        const typename GraphMap::Value &t;
1062 1063
      };
1063 1064

	
1064 1065
    };
1065 1066

	
1066 1067
    /// \brief Skeleton class for mappable directed graphs.
1067 1068
    ///
1068 1069
    /// This class describes the interface of mappable directed graphs.
1069 1070
    /// It extends \ref BaseDigraphComponent with the standard digraph 
1070 1071
    /// map classes, namely \c NodeMap and \c ArcMap.
1071 1072
    /// This concept is part of the Digraph concept.
1072 1073
    template <typename BAS = BaseDigraphComponent>
1073 1074
    class MappableDigraphComponent : public BAS  {
1074 1075
    public:
1075 1076

	
1076 1077
      typedef BAS Base;
1077 1078
      typedef typename Base::Node Node;
1078 1079
      typedef typename Base::Arc Arc;
1079 1080

	
1080 1081
      typedef MappableDigraphComponent Digraph;
1081 1082

	
1082 1083
      /// \brief Standard graph map for the nodes.
1083 1084
      ///
1084 1085
      /// Standard graph map for the nodes.
1085 1086
      /// It conforms to the ReferenceMap concept.
1086 1087
      template <typename V>
1087 1088
      class NodeMap : public GraphMap<MappableDigraphComponent, Node, V> {
1088
      public:
1089 1089
        typedef GraphMap<MappableDigraphComponent, Node, V> Parent;
1090 1090

	
1091
      public:
1091 1092
        /// \brief Construct a new map.
1092 1093
        ///
1093 1094
        /// Construct a new map for the digraph.
1094 1095
        explicit NodeMap(const MappableDigraphComponent& digraph)
1095 1096
          : Parent(digraph) {}
1096 1097

	
1097 1098
        /// \brief Construct a new map with default value.
1098 1099
        ///
1099 1100
        /// Construct a new map for the digraph and initalize the values.
1100 1101
        NodeMap(const MappableDigraphComponent& digraph, const V& value)
1101 1102
          : Parent(digraph, value) {}
1102 1103

	
1103 1104
      private:
1104 1105
        /// \brief Copy constructor.
1105 1106
        ///
1106 1107
        /// Copy Constructor.
1107 1108
        NodeMap(const NodeMap& nm) : Parent(nm) {}
1108 1109

	
1109 1110
        /// \brief Assignment operator.
1110 1111
        ///
1111 1112
        /// Assignment operator.
1112 1113
        template <typename CMap>
1113 1114
        NodeMap& operator=(const CMap&) {
1114 1115
          checkConcept<ReadMap<Node, V>, CMap>();
1115 1116
          return *this;
1116 1117
        }
1117 1118

	
1118 1119
      };
1119 1120

	
1120 1121
      /// \brief Standard graph map for the arcs.
1121 1122
      ///
1122 1123
      /// Standard graph map for the arcs.
1123 1124
      /// It conforms to the ReferenceMap concept.
1124 1125
      template <typename V>
1125 1126
      class ArcMap : public GraphMap<MappableDigraphComponent, Arc, V> {
1126
      public:
1127 1127
        typedef GraphMap<MappableDigraphComponent, Arc, V> Parent;
1128 1128

	
1129
      public:
1129 1130
        /// \brief Construct a new map.
1130 1131
        ///
1131 1132
        /// Construct a new map for the digraph.
1132 1133
        explicit ArcMap(const MappableDigraphComponent& digraph)
1133 1134
          : Parent(digraph) {}
1134 1135

	
1135 1136
        /// \brief Construct a new map with default value.
1136 1137
        ///
1137 1138
        /// Construct a new map for the digraph and initalize the values.
1138 1139
        ArcMap(const MappableDigraphComponent& digraph, const V& value)
1139 1140
          : Parent(digraph, value) {}
1140 1141

	
1141 1142
      private:
1142 1143
        /// \brief Copy constructor.
1143 1144
        ///
1144 1145
        /// Copy Constructor.
1145 1146
        ArcMap(const ArcMap& nm) : Parent(nm) {}
1146 1147

	
1147 1148
        /// \brief Assignment operator.
1148 1149
        ///
1149 1150
        /// Assignment operator.
1150 1151
        template <typename CMap>
1151 1152
        ArcMap& operator=(const CMap&) {
1152 1153
          checkConcept<ReadMap<Arc, V>, CMap>();
1153 1154
          return *this;
1154 1155
        }
1155 1156

	
1156 1157
      };
1157 1158

	
1158 1159

	
1159 1160
      template <typename _Digraph>
1160 1161
      struct Constraints {
1161 1162

	
1162 1163
        struct Dummy {
1163 1164
          int value;
1164 1165
          Dummy() : value(0) {}
1165 1166
          Dummy(int _v) : value(_v) {}
1166 1167
        };
1167 1168

	
1168 1169
        void constraints() {
1169 1170
          checkConcept<Base, _Digraph>();
1170 1171
          { // int map test
1171 1172
            typedef typename _Digraph::template NodeMap<int> IntNodeMap;
1172 1173
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>,
1173 1174
              IntNodeMap >();
1174 1175
          } { // bool map test
1175 1176
            typedef typename _Digraph::template NodeMap<bool> BoolNodeMap;
1176 1177
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>,
1177 1178
              BoolNodeMap >();
1178 1179
          } { // Dummy map test
1179 1180
            typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap;
1180 1181
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>,
1181 1182
              DummyNodeMap >();
1182 1183
          }
1183 1184

	
1184 1185
          { // int map test
1185 1186
            typedef typename _Digraph::template ArcMap<int> IntArcMap;
1186 1187
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>,
1187 1188
              IntArcMap >();
1188 1189
          } { // bool map test
1189 1190
            typedef typename _Digraph::template ArcMap<bool> BoolArcMap;
1190 1191
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>,
1191 1192
              BoolArcMap >();
1192 1193
          } { // Dummy map test
1193 1194
            typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap;
1194 1195
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>,
1195 1196
              DummyArcMap >();
1196 1197
          }
1197 1198
        }
1198 1199

	
1199 1200
        const _Digraph& digraph;
1200 1201
      };
1201 1202
    };
1202 1203

	
1203 1204
    /// \brief Skeleton class for mappable undirected graphs.
1204 1205
    ///
1205 1206
    /// This class describes the interface of mappable undirected graphs.
1206 1207
    /// It extends \ref MappableDigraphComponent with the standard graph 
1207 1208
    /// map class for edges (\c EdgeMap).
1208 1209
    /// This concept is part of the Graph concept.
1209 1210
    template <typename BAS = BaseGraphComponent>
1210 1211
    class MappableGraphComponent : public MappableDigraphComponent<BAS>  {
1211 1212
    public:
1212 1213

	
1213 1214
      typedef BAS Base;
1214 1215
      typedef typename Base::Edge Edge;
1215 1216

	
1216 1217
      typedef MappableGraphComponent Graph;
1217 1218

	
1218 1219
      /// \brief Standard graph map for the edges.
1219 1220
      ///
1220 1221
      /// Standard graph map for the edges.
1221 1222
      /// It conforms to the ReferenceMap concept.
1222 1223
      template <typename V>
1223 1224
      class EdgeMap : public GraphMap<MappableGraphComponent, Edge, V> {
1224
      public:
1225 1225
        typedef GraphMap<MappableGraphComponent, Edge, V> Parent;
1226 1226

	
1227
      public:
1227 1228
        /// \brief Construct a new map.
1228 1229
        ///
1229 1230
        /// Construct a new map for the graph.
1230 1231
        explicit EdgeMap(const MappableGraphComponent& graph)
1231 1232
          : Parent(graph) {}
1232 1233

	
1233 1234
        /// \brief Construct a new map with default value.
1234 1235
        ///
1235 1236
        /// Construct a new map for the graph and initalize the values.
1236 1237
        EdgeMap(const MappableGraphComponent& graph, const V& value)
1237 1238
          : Parent(graph, value) {}
1238 1239

	
1239 1240
      private:
1240 1241
        /// \brief Copy constructor.
1241 1242
        ///
1242 1243
        /// Copy Constructor.
1243 1244
        EdgeMap(const EdgeMap& nm) : Parent(nm) {}
1244 1245

	
1245 1246
        /// \brief Assignment operator.
1246 1247
        ///
1247 1248
        /// Assignment operator.
1248 1249
        template <typename CMap>
1249 1250
        EdgeMap& operator=(const CMap&) {
1250 1251
          checkConcept<ReadMap<Edge, V>, CMap>();
1251 1252
          return *this;
1252 1253
        }
1253 1254

	
1254 1255
      };
1255 1256

	
1256 1257

	
1257 1258
      template <typename _Graph>
1258 1259
      struct Constraints {
1259 1260

	
1260 1261
        struct Dummy {
1261 1262
          int value;
1262 1263
          Dummy() : value(0) {}
1263 1264
          Dummy(int _v) : value(_v) {}
1264 1265
        };
1265 1266

	
1266 1267
        void constraints() {
1267 1268
          checkConcept<MappableDigraphComponent<Base>, _Graph>();
1268 1269

	
1269 1270
          { // int map test
1270 1271
            typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
1271 1272
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
1272 1273
              IntEdgeMap >();
1273 1274
          } { // bool map test
1274 1275
            typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
1275 1276
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
1276 1277
              BoolEdgeMap >();
1277 1278
          } { // Dummy map test
1278 1279
            typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
1279 1280
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>,
1280 1281
              DummyEdgeMap >();
1281 1282
          }
1282 1283
        }
1283 1284

	
1284 1285
        const _Graph& graph;
1285 1286
      };
1286 1287
    };
1287 1288

	
1288 1289
    /// \brief Skeleton class for extendable directed graphs.
1289 1290
    ///
1290 1291
    /// This class describes the interface of extendable directed graphs.
1291 1292
    /// It extends \ref BaseDigraphComponent with functions for adding 
1292 1293
    /// nodes and arcs to the digraph.
1293 1294
    /// This concept requires \ref AlterableDigraphComponent.
1294 1295
    template <typename BAS = BaseDigraphComponent>
1295 1296
    class ExtendableDigraphComponent : public BAS {
1296 1297
    public:
1297 1298
      typedef BAS Base;
1298 1299

	
1299 1300
      typedef typename Base::Node Node;
1300 1301
      typedef typename Base::Arc Arc;
1301 1302

	
1302 1303
      /// \brief Add a new node to the digraph.
1303 1304
      ///
1304 1305
      /// This function adds a new node to the digraph.
1305 1306
      Node addNode() {
1306 1307
        return INVALID;
1307 1308
      }
1308 1309

	
1309 1310
      /// \brief Add a new arc connecting the given two nodes.
1310 1311
      ///
1311 1312
      /// This function adds a new arc connecting the given two nodes
1312 1313
      /// of the digraph.
1313 1314
      Arc addArc(const Node&, const Node&) {
1314 1315
        return INVALID;
1315 1316
      }
1316 1317

	
1317 1318
      template <typename _Digraph>
1318 1319
      struct Constraints {
1319 1320
        void constraints() {
1320 1321
          checkConcept<Base, _Digraph>();
1321 1322
          typename _Digraph::Node node_a, node_b;
1322 1323
          node_a = digraph.addNode();
1323 1324
          node_b = digraph.addNode();
1324 1325
          typename _Digraph::Arc arc;
1325 1326
          arc = digraph.addArc(node_a, node_b);
1326 1327
        }
1327 1328

	
1328 1329
        _Digraph& digraph;
1329 1330
      };
1330 1331
    };
1331 1332

	
1332 1333
    /// \brief Skeleton class for extendable undirected graphs.
1333 1334
    ///
1334 1335
    /// This class describes the interface of extendable undirected graphs.
1335 1336
    /// It extends \ref BaseGraphComponent with functions for adding 
1336 1337
    /// nodes and edges to the graph.
1337 1338
    /// This concept requires \ref AlterableGraphComponent.
1338 1339
    template <typename BAS = BaseGraphComponent>
1339 1340
    class ExtendableGraphComponent : public BAS {
1340 1341
    public:
1341 1342

	
1342 1343
      typedef BAS Base;
1343 1344
      typedef typename Base::Node Node;
1344 1345
      typedef typename Base::Edge Edge;
1345 1346

	
1346 1347
      /// \brief Add a new node to the digraph.
1347 1348
      ///
1348 1349
      /// This function adds a new node to the digraph.
1349 1350
      Node addNode() {
1350 1351
        return INVALID;
1351 1352
      }
1352 1353

	
1353 1354
      /// \brief Add a new edge connecting the given two nodes.
1354 1355
      ///
1355 1356
      /// This function adds a new edge connecting the given two nodes
1356 1357
      /// of the graph.
1357 1358
      Edge addEdge(const Node&, const Node&) {
1358 1359
        return INVALID;
1359 1360
      }
1360 1361

	
1361 1362
      template <typename _Graph>
1362 1363
      struct Constraints {
1363 1364
        void constraints() {
1364 1365
          checkConcept<Base, _Graph>();
1365 1366
          typename _Graph::Node node_a, node_b;
1366 1367
          node_a = graph.addNode();
1367 1368
          node_b = graph.addNode();
1368 1369
          typename _Graph::Edge edge;
1369 1370
          edge = graph.addEdge(node_a, node_b);
1370 1371
        }
1371 1372

	
1372 1373
        _Graph& graph;
1373 1374
      };
1374 1375
    };
1375 1376

	
1376 1377
    /// \brief Skeleton class for erasable directed graphs.
1377 1378
    ///
1378 1379
    /// This class describes the interface of erasable directed graphs.
1379 1380
    /// It extends \ref BaseDigraphComponent with functions for removing 
1380 1381
    /// nodes and arcs from the digraph.
1381 1382
    /// This concept requires \ref AlterableDigraphComponent.
1382 1383
    template <typename BAS = BaseDigraphComponent>
1383 1384
    class ErasableDigraphComponent : public BAS {
1384 1385
    public:
1385 1386

	
1386 1387
      typedef BAS Base;
1387 1388
      typedef typename Base::Node Node;
1388 1389
      typedef typename Base::Arc Arc;
1389 1390

	
1390 1391
      /// \brief Erase a node from the digraph.
1391 1392
      ///
1392 1393
      /// This function erases the given node from the digraph and all arcs 
1393 1394
      /// connected to the node.
1394 1395
      void erase(const Node&) {}
1395 1396

	
1396 1397
      /// \brief Erase an arc from the digraph.
1397 1398
      ///
1398 1399
      /// This function erases the given arc from the digraph.
1399 1400
      void erase(const Arc&) {}
1400 1401

	
1401 1402
      template <typename _Digraph>
1402 1403
      struct Constraints {
1403 1404
        void constraints() {
1404 1405
          checkConcept<Base, _Digraph>();
1405 1406
          const typename _Digraph::Node node(INVALID);
1406 1407
          digraph.erase(node);
1407 1408
          const typename _Digraph::Arc arc(INVALID);
1408 1409
          digraph.erase(arc);
1409 1410
        }
1410 1411

	
1411 1412
        _Digraph& digraph;
1412 1413
      };
1413 1414
    };
1414 1415

	
1415 1416
    /// \brief Skeleton class for erasable undirected graphs.
1416 1417
    ///
1417 1418
    /// This class describes the interface of erasable undirected graphs.
1418 1419
    /// It extends \ref BaseGraphComponent with functions for removing 
1419 1420
    /// nodes and edges from the graph.
1420 1421
    /// This concept requires \ref AlterableGraphComponent.
1421 1422
    template <typename BAS = BaseGraphComponent>
1422 1423
    class ErasableGraphComponent : public BAS {
1423 1424
    public:
1424 1425

	
1425 1426
      typedef BAS Base;
1426 1427
      typedef typename Base::Node Node;
1427 1428
      typedef typename Base::Edge Edge;
1428 1429

	
1429 1430
      /// \brief Erase a node from the graph.
1430 1431
      ///
1431 1432
      /// This function erases the given node from the graph and all edges
1432 1433
      /// connected to the node.
1433 1434
      void erase(const Node&) {}
1434 1435

	
1435 1436
      /// \brief Erase an edge from the digraph.
1436 1437
      ///
1437 1438
      /// This function erases the given edge from the digraph.
1438 1439
      void erase(const Edge&) {}
1439 1440

	
1440 1441
      template <typename _Graph>
1441 1442
      struct Constraints {
1442 1443
        void constraints() {
1443 1444
          checkConcept<Base, _Graph>();
1444 1445
          const typename _Graph::Node node(INVALID);
1445 1446
          graph.erase(node);
1446 1447
          const typename _Graph::Edge edge(INVALID);
1447 1448
          graph.erase(edge);
1448 1449
        }
1449 1450

	
1450 1451
        _Graph& graph;
1451 1452
      };
1452 1453
    };
1453 1454

	
1454 1455
    /// \brief Skeleton class for clearable directed graphs.
1455 1456
    ///
1456 1457
    /// This class describes the interface of clearable directed graphs.
1457 1458
    /// It extends \ref BaseDigraphComponent with a function for clearing
1458 1459
    /// the digraph.
1459 1460
    /// This concept requires \ref AlterableDigraphComponent.
1460 1461
    template <typename BAS = BaseDigraphComponent>
1461 1462
    class ClearableDigraphComponent : public BAS {
1462 1463
    public:
1463 1464

	
1464 1465
      typedef BAS Base;
1465 1466

	
1466 1467
      /// \brief Erase all nodes and arcs from the digraph.
1467 1468
      ///
1468 1469
      /// This function erases all nodes and arcs from the digraph.
1469 1470
      void clear() {}
1470 1471

	
1471 1472
      template <typename _Digraph>
1472 1473
      struct Constraints {
1473 1474
        void constraints() {
1474 1475
          checkConcept<Base, _Digraph>();
1475 1476
          digraph.clear();
1476 1477
        }
1477 1478

	
1478 1479
        _Digraph& digraph;
1479 1480
      };
1480 1481
    };
1481 1482

	
1482 1483
    /// \brief Skeleton class for clearable undirected graphs.
1483 1484
    ///
1484 1485
    /// This class describes the interface of clearable undirected graphs.
1485 1486
    /// It extends \ref BaseGraphComponent with a function for clearing
1486 1487
    /// the graph.
1487 1488
    /// This concept requires \ref AlterableGraphComponent.
1488 1489
    template <typename BAS = BaseGraphComponent>
1489 1490
    class ClearableGraphComponent : public ClearableDigraphComponent<BAS> {
1490 1491
    public:
1491 1492

	
1492 1493
      typedef BAS Base;
1493 1494

	
1494 1495
      /// \brief Erase all nodes and edges from the graph.
1495 1496
      ///
1496 1497
      /// This function erases all nodes and edges from the graph.
1497 1498
      void clear() {}
1498 1499

	
1499 1500
      template <typename _Graph>
1500 1501
      struct Constraints {
1501 1502
        void constraints() {
1502 1503
          checkConcept<Base, _Graph>();
1503 1504
          graph.clear();
1504 1505
        }
1505 1506

	
1506 1507
        _Graph& graph;
1507 1508
      };
1508 1509
    };
1509 1510

	
1510 1511
  }
1511 1512

	
1512 1513
}
1513 1514

	
1514 1515
#endif
Ignore white space 6 line context
... ...
@@ -655,1192 +655,1201 @@
655 655
    return DigraphCopy<From, To>(from, to);
656 656
  }
657 657

	
658 658
  /// \brief Class to copy a graph.
659 659
  ///
660 660
  /// Class to copy a graph to another graph (duplicate a graph). The
661 661
  /// simplest way of using it is through the \c graphCopy() function.
662 662
  ///
663 663
  /// This class not only make a copy of a graph, but it can create
664 664
  /// references and cross references between the nodes, edges and arcs of
665 665
  /// the two graphs, and it can copy maps for using with the newly created
666 666
  /// graph.
667 667
  ///
668 668
  /// To make a copy from a graph, first an instance of GraphCopy
669 669
  /// should be created, then the data belongs to the graph should
670 670
  /// assigned to copy. In the end, the \c run() member should be
671 671
  /// called.
672 672
  ///
673 673
  /// The next code copies a graph with several data:
674 674
  ///\code
675 675
  ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
676 676
  ///  // Create references for the nodes
677 677
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
678 678
  ///  cg.nodeRef(nr);
679 679
  ///  // Create cross references (inverse) for the edges
680 680
  ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
681 681
  ///  cg.edgeCrossRef(ecr);
682 682
  ///  // Copy an edge map
683 683
  ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
684 684
  ///  NewGraph::EdgeMap<double> nemap(new_graph);
685 685
  ///  cg.edgeMap(oemap, nemap);
686 686
  ///  // Copy a node
687 687
  ///  OrigGraph::Node on;
688 688
  ///  NewGraph::Node nn;
689 689
  ///  cg.node(on, nn);
690 690
  ///  // Execute copying
691 691
  ///  cg.run();
692 692
  ///\endcode
693 693
  template <typename From, typename To>
694 694
  class GraphCopy {
695 695
  private:
696 696

	
697 697
    typedef typename From::Node Node;
698 698
    typedef typename From::NodeIt NodeIt;
699 699
    typedef typename From::Arc Arc;
700 700
    typedef typename From::ArcIt ArcIt;
701 701
    typedef typename From::Edge Edge;
702 702
    typedef typename From::EdgeIt EdgeIt;
703 703

	
704 704
    typedef typename To::Node TNode;
705 705
    typedef typename To::Arc TArc;
706 706
    typedef typename To::Edge TEdge;
707 707

	
708 708
    typedef typename From::template NodeMap<TNode> NodeRefMap;
709 709
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
710 710

	
711 711
    struct ArcRefMap {
712 712
      ArcRefMap(const From& from, const To& to,
713 713
                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
714 714
        : _from(from), _to(to),
715 715
          _edge_ref(edge_ref), _node_ref(node_ref) {}
716 716

	
717 717
      typedef typename From::Arc Key;
718 718
      typedef typename To::Arc Value;
719 719

	
720 720
      Value operator[](const Key& key) const {
721 721
        bool forward = _from.u(key) != _from.v(key) ?
722 722
          _node_ref[_from.source(key)] ==
723 723
          _to.source(_to.direct(_edge_ref[key], true)) :
724 724
          _from.direction(key);
725 725
        return _to.direct(_edge_ref[key], forward);
726 726
      }
727 727

	
728 728
      const From& _from;
729 729
      const To& _to;
730 730
      const EdgeRefMap& _edge_ref;
731 731
      const NodeRefMap& _node_ref;
732 732
    };
733 733

	
734 734
  public:
735 735

	
736 736
    /// \brief Constructor of GraphCopy.
737 737
    ///
738 738
    /// Constructor of GraphCopy for copying the content of the
739 739
    /// \c from graph into the \c to graph.
740 740
    GraphCopy(const From& from, To& to)
741 741
      : _from(from), _to(to) {}
742 742

	
743 743
    /// \brief Destructor of GraphCopy
744 744
    ///
745 745
    /// Destructor of GraphCopy.
746 746
    ~GraphCopy() {
747 747
      for (int i = 0; i < int(_node_maps.size()); ++i) {
748 748
        delete _node_maps[i];
749 749
      }
750 750
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
751 751
        delete _arc_maps[i];
752 752
      }
753 753
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
754 754
        delete _edge_maps[i];
755 755
      }
756 756
    }
757 757

	
758 758
    /// \brief Copy the node references into the given map.
759 759
    ///
760 760
    /// This function copies the node references into the given map.
761 761
    /// The parameter should be a map, whose key type is the Node type of
762 762
    /// the source graph, while the value type is the Node type of the
763 763
    /// destination graph.
764 764
    template <typename NodeRef>
765 765
    GraphCopy& nodeRef(NodeRef& map) {
766 766
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
767 767
                           NodeRefMap, NodeRef>(map));
768 768
      return *this;
769 769
    }
770 770

	
771 771
    /// \brief Copy the node cross references into the given map.
772 772
    ///
773 773
    /// This function copies the node cross references (reverse references)
774 774
    /// into the given map. The parameter should be a map, whose key type
775 775
    /// is the Node type of the destination graph, while the value type is
776 776
    /// the Node type of the source graph.
777 777
    template <typename NodeCrossRef>
778 778
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
779 779
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
780 780
                           NodeRefMap, NodeCrossRef>(map));
781 781
      return *this;
782 782
    }
783 783

	
784 784
    /// \brief Make a copy of the given node map.
785 785
    ///
786 786
    /// This function makes a copy of the given node map for the newly
787 787
    /// created graph.
788 788
    /// The key type of the new map \c tmap should be the Node type of the
789 789
    /// destination graph, and the key type of the original map \c map
790 790
    /// should be the Node type of the source graph.
791 791
    template <typename FromMap, typename ToMap>
792 792
    GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
793 793
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
794 794
                           NodeRefMap, FromMap, ToMap>(map, tmap));
795 795
      return *this;
796 796
    }
797 797

	
798 798
    /// \brief Make a copy of the given node.
799 799
    ///
800 800
    /// This function makes a copy of the given node.
801 801
    GraphCopy& node(const Node& node, TNode& tnode) {
802 802
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
803 803
                           NodeRefMap, TNode>(node, tnode));
804 804
      return *this;
805 805
    }
806 806

	
807 807
    /// \brief Copy the arc references into the given map.
808 808
    ///
809 809
    /// This function copies the arc references into the given map.
810 810
    /// The parameter should be a map, whose key type is the Arc type of
811 811
    /// the source graph, while the value type is the Arc type of the
812 812
    /// destination graph.
813 813
    template <typename ArcRef>
814 814
    GraphCopy& arcRef(ArcRef& map) {
815 815
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
816 816
                          ArcRefMap, ArcRef>(map));
817 817
      return *this;
818 818
    }
819 819

	
820 820
    /// \brief Copy the arc cross references into the given map.
821 821
    ///
822 822
    /// This function copies the arc cross references (reverse references)
823 823
    /// into the given map. The parameter should be a map, whose key type
824 824
    /// is the Arc type of the destination graph, while the value type is
825 825
    /// the Arc type of the source graph.
826 826
    template <typename ArcCrossRef>
827 827
    GraphCopy& arcCrossRef(ArcCrossRef& map) {
828 828
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
829 829
                          ArcRefMap, ArcCrossRef>(map));
830 830
      return *this;
831 831
    }
832 832

	
833 833
    /// \brief Make a copy of the given arc map.
834 834
    ///
835 835
    /// This function makes a copy of the given arc map for the newly
836 836
    /// created graph.
837 837
    /// The key type of the new map \c tmap should be the Arc type of the
838 838
    /// destination graph, and the key type of the original map \c map
839 839
    /// should be the Arc type of the source graph.
840 840
    template <typename FromMap, typename ToMap>
841 841
    GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
842 842
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
843 843
                          ArcRefMap, FromMap, ToMap>(map, tmap));
844 844
      return *this;
845 845
    }
846 846

	
847 847
    /// \brief Make a copy of the given arc.
848 848
    ///
849 849
    /// This function makes a copy of the given arc.
850 850
    GraphCopy& arc(const Arc& arc, TArc& tarc) {
851 851
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
852 852
                          ArcRefMap, TArc>(arc, tarc));
853 853
      return *this;
854 854
    }
855 855

	
856 856
    /// \brief Copy the edge references into the given map.
857 857
    ///
858 858
    /// This function copies the edge references into the given map.
859 859
    /// The parameter should be a map, whose key type is the Edge type of
860 860
    /// the source graph, while the value type is the Edge type of the
861 861
    /// destination graph.
862 862
    template <typename EdgeRef>
863 863
    GraphCopy& edgeRef(EdgeRef& map) {
864 864
      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
865 865
                           EdgeRefMap, EdgeRef>(map));
866 866
      return *this;
867 867
    }
868 868

	
869 869
    /// \brief Copy the edge cross references into the given map.
870 870
    ///
871 871
    /// This function copies the edge cross references (reverse references)
872 872
    /// into the given map. The parameter should be a map, whose key type
873 873
    /// is the Edge type of the destination graph, while the value type is
874 874
    /// the Edge type of the source graph.
875 875
    template <typename EdgeCrossRef>
876 876
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
877 877
      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
878 878
                           Edge, EdgeRefMap, EdgeCrossRef>(map));
879 879
      return *this;
880 880
    }
881 881

	
882 882
    /// \brief Make a copy of the given edge map.
883 883
    ///
884 884
    /// This function makes a copy of the given edge map for the newly
885 885
    /// created graph.
886 886
    /// The key type of the new map \c tmap should be the Edge type of the
887 887
    /// destination graph, and the key type of the original map \c map
888 888
    /// should be the Edge type of the source graph.
889 889
    template <typename FromMap, typename ToMap>
890 890
    GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
891 891
      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
892 892
                           EdgeRefMap, FromMap, ToMap>(map, tmap));
893 893
      return *this;
894 894
    }
895 895

	
896 896
    /// \brief Make a copy of the given edge.
897 897
    ///
898 898
    /// This function makes a copy of the given edge.
899 899
    GraphCopy& edge(const Edge& edge, TEdge& tedge) {
900 900
      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
901 901
                           EdgeRefMap, TEdge>(edge, tedge));
902 902
      return *this;
903 903
    }
904 904

	
905 905
    /// \brief Execute copying.
906 906
    ///
907 907
    /// This function executes the copying of the graph along with the
908 908
    /// copying of the assigned data.
909 909
    void run() {
910 910
      NodeRefMap nodeRefMap(_from);
911 911
      EdgeRefMap edgeRefMap(_from);
912 912
      ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
913 913
      _core_bits::GraphCopySelector<To>::
914 914
        copy(_from, _to, nodeRefMap, edgeRefMap);
915 915
      for (int i = 0; i < int(_node_maps.size()); ++i) {
916 916
        _node_maps[i]->copy(_from, nodeRefMap);
917 917
      }
918 918
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
919 919
        _edge_maps[i]->copy(_from, edgeRefMap);
920 920
      }
921 921
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
922 922
        _arc_maps[i]->copy(_from, arcRefMap);
923 923
      }
924 924
    }
925 925

	
926 926
  private:
927 927

	
928 928
    const From& _from;
929 929
    To& _to;
930 930

	
931 931
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
932 932
      _node_maps;
933 933

	
934 934
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
935 935
      _arc_maps;
936 936

	
937 937
    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
938 938
      _edge_maps;
939 939

	
940 940
  };
941 941

	
942 942
  /// \brief Copy a graph to another graph.
943 943
  ///
944 944
  /// This function copies a graph to another graph.
945 945
  /// The complete usage of it is detailed in the GraphCopy class,
946 946
  /// but a short example shows a basic work:
947 947
  ///\code
948 948
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
949 949
  ///\endcode
950 950
  ///
951 951
  /// After the copy the \c nr map will contain the mapping from the
952 952
  /// nodes of the \c from graph to the nodes of the \c to graph and
953 953
  /// \c ecr will contain the mapping from the edges of the \c to graph
954 954
  /// to the edges of the \c from graph.
955 955
  ///
956 956
  /// \see GraphCopy
957 957
  template <typename From, typename To>
958 958
  GraphCopy<From, To>
959 959
  graphCopy(const From& from, To& to) {
960 960
    return GraphCopy<From, To>(from, to);
961 961
  }
962 962

	
963 963
  namespace _core_bits {
964 964

	
965 965
    template <typename Graph, typename Enable = void>
966 966
    struct FindArcSelector {
967 967
      typedef typename Graph::Node Node;
968 968
      typedef typename Graph::Arc Arc;
969 969
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
970 970
        if (e == INVALID) {
971 971
          g.firstOut(e, u);
972 972
        } else {
973 973
          g.nextOut(e);
974 974
        }
975 975
        while (e != INVALID && g.target(e) != v) {
976 976
          g.nextOut(e);
977 977
        }
978 978
        return e;
979 979
      }
980 980
    };
981 981

	
982 982
    template <typename Graph>
983 983
    struct FindArcSelector<
984 984
      Graph,
985 985
      typename enable_if<typename Graph::FindArcTag, void>::type>
986 986
    {
987 987
      typedef typename Graph::Node Node;
988 988
      typedef typename Graph::Arc Arc;
989 989
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
990 990
        return g.findArc(u, v, prev);
991 991
      }
992 992
    };
993 993
  }
994 994

	
995 995
  /// \brief Find an arc between two nodes of a digraph.
996 996
  ///
997 997
  /// This function finds an arc from node \c u to node \c v in the
998 998
  /// digraph \c g.
999 999
  ///
1000 1000
  /// If \c prev is \ref INVALID (this is the default value), then
1001 1001
  /// it finds the first arc from \c u to \c v. Otherwise it looks for
1002 1002
  /// the next arc from \c u to \c v after \c prev.
1003 1003
  /// \return The found arc or \ref INVALID if there is no such an arc.
1004 1004
  ///
1005 1005
  /// Thus you can iterate through each arc from \c u to \c v as it follows.
1006 1006
  ///\code
1007 1007
  /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1008 1008
  ///   ...
1009 1009
  /// }
1010 1010
  ///\endcode
1011 1011
  ///
1012 1012
  /// \note \ref ConArcIt provides iterator interface for the same
1013 1013
  /// functionality.
1014 1014
  ///
1015 1015
  ///\sa ConArcIt
1016 1016
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1017 1017
  template <typename Graph>
1018 1018
  inline typename Graph::Arc
1019 1019
  findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1020 1020
          typename Graph::Arc prev = INVALID) {
1021 1021
    return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1022 1022
  }
1023 1023

	
1024 1024
  /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1025 1025
  ///
1026 1026
  /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1027 1027
  /// a higher level interface for the \ref findArc() function. You can
1028 1028
  /// use it the following way:
1029 1029
  ///\code
1030 1030
  /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1031 1031
  ///   ...
1032 1032
  /// }
1033 1033
  ///\endcode
1034 1034
  ///
1035 1035
  ///\sa findArc()
1036 1036
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1037 1037
  template <typename GR>
1038 1038
  class ConArcIt : public GR::Arc {
1039
    typedef typename GR::Arc Parent;
1040

	
1039 1041
  public:
1040 1042

	
1041
    typedef GR Graph;
1042
    typedef typename Graph::Arc Parent;
1043

	
1044
    typedef typename Graph::Arc Arc;
1045
    typedef typename Graph::Node Node;
1043
    typedef typename GR::Arc Arc;
1044
    typedef typename GR::Node Node;
1046 1045

	
1047 1046
    /// \brief Constructor.
1048 1047
    ///
1049 1048
    /// Construct a new ConArcIt iterating on the arcs that
1050 1049
    /// connects nodes \c u and \c v.
1051
    ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
1050
    ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1052 1051
      Parent::operator=(findArc(_graph, u, v));
1053 1052
    }
1054 1053

	
1055 1054
    /// \brief Constructor.
1056 1055
    ///
1057 1056
    /// Construct a new ConArcIt that continues the iterating from arc \c a.
1058
    ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
1057
    ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1059 1058

	
1060 1059
    /// \brief Increment operator.
1061 1060
    ///
1062 1061
    /// It increments the iterator and gives back the next arc.
1063 1062
    ConArcIt& operator++() {
1064 1063
      Parent::operator=(findArc(_graph, _graph.source(*this),
1065 1064
                                _graph.target(*this), *this));
1066 1065
      return *this;
1067 1066
    }
1068 1067
  private:
1069
    const Graph& _graph;
1068
    const GR& _graph;
1070 1069
  };
1071 1070

	
1072 1071
  namespace _core_bits {
1073 1072

	
1074 1073
    template <typename Graph, typename Enable = void>
1075 1074
    struct FindEdgeSelector {
1076 1075
      typedef typename Graph::Node Node;
1077 1076
      typedef typename Graph::Edge Edge;
1078 1077
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
1079 1078
        bool b;
1080 1079
        if (u != v) {
1081 1080
          if (e == INVALID) {
1082 1081
            g.firstInc(e, b, u);
1083 1082
          } else {
1084 1083
            b = g.u(e) == u;
1085 1084
            g.nextInc(e, b);
1086 1085
          }
1087 1086
          while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1088 1087
            g.nextInc(e, b);
1089 1088
          }
1090 1089
        } else {
1091 1090
          if (e == INVALID) {
1092 1091
            g.firstInc(e, b, u);
1093 1092
          } else {
1094 1093
            b = true;
1095 1094
            g.nextInc(e, b);
1096 1095
          }
1097 1096
          while (e != INVALID && (!b || g.v(e) != v)) {
1098 1097
            g.nextInc(e, b);
1099 1098
          }
1100 1099
        }
1101 1100
        return e;
1102 1101
      }
1103 1102
    };
1104 1103

	
1105 1104
    template <typename Graph>
1106 1105
    struct FindEdgeSelector<
1107 1106
      Graph,
1108 1107
      typename enable_if<typename Graph::FindEdgeTag, void>::type>
1109 1108
    {
1110 1109
      typedef typename Graph::Node Node;
1111 1110
      typedef typename Graph::Edge Edge;
1112 1111
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1113 1112
        return g.findEdge(u, v, prev);
1114 1113
      }
1115 1114
    };
1116 1115
  }
1117 1116

	
1118 1117
  /// \brief Find an edge between two nodes of a graph.
1119 1118
  ///
1120 1119
  /// This function finds an edge from node \c u to node \c v in graph \c g.
1121 1120
  /// If node \c u and node \c v is equal then each loop edge
1122 1121
  /// will be enumerated once.
1123 1122
  ///
1124 1123
  /// If \c prev is \ref INVALID (this is the default value), then
1125 1124
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
1126 1125
  /// the next edge from \c u to \c v after \c prev.
1127 1126
  /// \return The found edge or \ref INVALID if there is no such an edge.
1128 1127
  ///
1129 1128
  /// Thus you can iterate through each edge between \c u and \c v
1130 1129
  /// as it follows.
1131 1130
  ///\code
1132 1131
  /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1133 1132
  ///   ...
1134 1133
  /// }
1135 1134
  ///\endcode
1136 1135
  ///
1137 1136
  /// \note \ref ConEdgeIt provides iterator interface for the same
1138 1137
  /// functionality.
1139 1138
  ///
1140 1139
  ///\sa ConEdgeIt
1141 1140
  template <typename Graph>
1142 1141
  inline typename Graph::Edge
1143 1142
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1144 1143
            typename Graph::Edge p = INVALID) {
1145 1144
    return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1146 1145
  }
1147 1146

	
1148 1147
  /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1149 1148
  ///
1150 1149
  /// Iterator for iterating on parallel edges connecting the same nodes.
1151 1150
  /// It is a higher level interface for the findEdge() function. You can
1152 1151
  /// use it the following way:
1153 1152
  ///\code
1154 1153
  /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1155 1154
  ///   ...
1156 1155
  /// }
1157 1156
  ///\endcode
1158 1157
  ///
1159 1158
  ///\sa findEdge()
1160 1159
  template <typename GR>
1161 1160
  class ConEdgeIt : public GR::Edge {
1161
    typedef typename GR::Edge Parent;
1162

	
1162 1163
  public:
1163 1164

	
1164
    typedef GR Graph;
1165
    typedef typename Graph::Edge Parent;
1166

	
1167
    typedef typename Graph::Edge Edge;
1168
    typedef typename Graph::Node Node;
1165
    typedef typename GR::Edge Edge;
1166
    typedef typename GR::Node Node;
1169 1167

	
1170 1168
    /// \brief Constructor.
1171 1169
    ///
1172 1170
    /// Construct a new ConEdgeIt iterating on the edges that
1173 1171
    /// connects nodes \c u and \c v.
1174
    ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1172
    ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1175 1173
      Parent::operator=(findEdge(_graph, _u, _v));
1176 1174
    }
1177 1175

	
1178 1176
    /// \brief Constructor.
1179 1177
    ///
1180 1178
    /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1181
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
1179
    ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1182 1180

	
1183 1181
    /// \brief Increment operator.
1184 1182
    ///
1185 1183
    /// It increments the iterator and gives back the next edge.
1186 1184
    ConEdgeIt& operator++() {
1187 1185
      Parent::operator=(findEdge(_graph, _u, _v, *this));
1188 1186
      return *this;
1189 1187
    }
1190 1188
  private:
1191
    const Graph& _graph;
1189
    const GR& _graph;
1192 1190
    Node _u, _v;
1193 1191
  };
1194 1192

	
1195 1193

	
1196 1194
  ///Dynamic arc look-up between given endpoints.
1197 1195

	
1198 1196
  ///Using this class, you can find an arc in a digraph from a given
1199 1197
  ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1200 1198
  ///where <em>d</em> is the out-degree of the source node.
1201 1199
  ///
1202 1200
  ///It is possible to find \e all parallel arcs between two nodes with
1203 1201
  ///the \c operator() member.
1204 1202
  ///
1205 1203
  ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1206 1204
  ///\ref AllArcLookUp if your digraph is not changed so frequently.
1207 1205
  ///
1208 1206
  ///This class uses a self-adjusting binary search tree, the Splay tree
1209 1207
  ///of Sleator and Tarjan to guarantee the logarithmic amortized
1210 1208
  ///time bound for arc look-ups. This class also guarantees the
1211 1209
  ///optimal time bound in a constant factor for any distribution of
1212 1210
  ///queries.
1213 1211
  ///
1214 1212
  ///\tparam GR The type of the underlying digraph.
1215 1213
  ///
1216 1214
  ///\sa ArcLookUp
1217 1215
  ///\sa AllArcLookUp
1218 1216
  template <typename GR>
1219 1217
  class DynArcLookUp
1220 1218
    : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1221 1219
  {
1222
  public:
1223 1220
    typedef typename ItemSetTraits<GR, typename GR::Arc>
1224 1221
    ::ItemNotifier::ObserverBase Parent;
1225 1222

	
1226 1223
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1224

	
1225
  public:
1226

	
1227
    /// The Digraph type
1227 1228
    typedef GR Digraph;
1228 1229

	
1229 1230
  protected:
1230 1231

	
1231 1232
    class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type {
1233
      typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1234

	
1232 1235
    public:
1233 1236

	
1234
      typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1235

	
1236 1237
      AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1237 1238

	
1238 1239
      virtual void add(const Node& node) {
1239 1240
        Parent::add(node);
1240 1241
        Parent::set(node, INVALID);
1241 1242
      }
1242 1243

	
1243 1244
      virtual void add(const std::vector<Node>& nodes) {
1244 1245
        Parent::add(nodes);
1245 1246
        for (int i = 0; i < int(nodes.size()); ++i) {
1246 1247
          Parent::set(nodes[i], INVALID);
1247 1248
        }
1248 1249
      }
1249 1250

	
1250 1251
      virtual void build() {
1251 1252
        Parent::build();
1252 1253
        Node it;
1253 1254
        typename Parent::Notifier* nf = Parent::notifier();
1254 1255
        for (nf->first(it); it != INVALID; nf->next(it)) {
1255 1256
          Parent::set(it, INVALID);
1256 1257
        }
1257 1258
      }
1258 1259
    };
1259 1260

	
1260
    const Digraph &_g;
1261
    AutoNodeMap _head;
1262
    typename Digraph::template ArcMap<Arc> _parent;
1263
    typename Digraph::template ArcMap<Arc> _left;
1264
    typename Digraph::template ArcMap<Arc> _right;
1265

	
1266 1261
    class ArcLess {
1267 1262
      const Digraph &g;
1268 1263
    public:
1269 1264
      ArcLess(const Digraph &_g) : g(_g) {}
1270 1265
      bool operator()(Arc a,Arc b) const
1271 1266
      {
1272 1267
        return g.target(a)<g.target(b);
1273 1268
      }
1274 1269
    };
1275 1270

	
1271
  protected: 
1272

	
1273
    const Digraph &_g;
1274
    AutoNodeMap _head;
1275
    typename Digraph::template ArcMap<Arc> _parent;
1276
    typename Digraph::template ArcMap<Arc> _left;
1277
    typename Digraph::template ArcMap<Arc> _right;
1278

	
1276 1279
  public:
1277 1280

	
1278 1281
    ///Constructor
1279 1282

	
1280 1283
    ///Constructor.
1281 1284
    ///
1282 1285
    ///It builds up the search database.
1283 1286
    DynArcLookUp(const Digraph &g)
1284 1287
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
1285 1288
    {
1286 1289
      Parent::attach(_g.notifier(typename Digraph::Arc()));
1287 1290
      refresh();
1288 1291
    }
1289 1292

	
1290 1293
  protected:
1291 1294

	
1292 1295
    virtual void add(const Arc& arc) {
1293 1296
      insert(arc);
1294 1297
    }
1295 1298

	
1296 1299
    virtual void add(const std::vector<Arc>& arcs) {
1297 1300
      for (int i = 0; i < int(arcs.size()); ++i) {
1298 1301
        insert(arcs[i]);
1299 1302
      }
1300 1303
    }
1301 1304

	
1302 1305
    virtual void erase(const Arc& arc) {
1303 1306
      remove(arc);
1304 1307
    }
1305 1308

	
1306 1309
    virtual void erase(const std::vector<Arc>& arcs) {
1307 1310
      for (int i = 0; i < int(arcs.size()); ++i) {
1308 1311
        remove(arcs[i]);
1309 1312
      }
1310 1313
    }
1311 1314

	
1312 1315
    virtual void build() {
1313 1316
      refresh();
1314 1317
    }
1315 1318

	
1316 1319
    virtual void clear() {
1317 1320
      for(NodeIt n(_g);n!=INVALID;++n) {
1318 1321
        _head[n] = INVALID;
1319 1322
      }
1320 1323
    }
1321 1324

	
1322 1325
    void insert(Arc arc) {
1323 1326
      Node s = _g.source(arc);
1324 1327
      Node t = _g.target(arc);
1325 1328
      _left[arc] = INVALID;
1326 1329
      _right[arc] = INVALID;
1327 1330

	
1328 1331
      Arc e = _head[s];
1329 1332
      if (e == INVALID) {
1330 1333
        _head[s] = arc;
1331 1334
        _parent[arc] = INVALID;
1332 1335
        return;
1333 1336
      }
1334 1337
      while (true) {
1335 1338
        if (t < _g.target(e)) {
1336 1339
          if (_left[e] == INVALID) {
1337 1340
            _left[e] = arc;
1338 1341
            _parent[arc] = e;
1339 1342
            splay(arc);
1340 1343
            return;
1341 1344
          } else {
1342 1345
            e = _left[e];
1343 1346
          }
1344 1347
        } else {
1345 1348
          if (_right[e] == INVALID) {
1346 1349
            _right[e] = arc;
1347 1350
            _parent[arc] = e;
1348 1351
            splay(arc);
1349 1352
            return;
1350 1353
          } else {
1351 1354
            e = _right[e];
1352 1355
          }
1353 1356
        }
1354 1357
      }
1355 1358
    }
1356 1359

	
1357 1360
    void remove(Arc arc) {
1358 1361
      if (_left[arc] == INVALID) {
1359 1362
        if (_right[arc] != INVALID) {
1360 1363
          _parent[_right[arc]] = _parent[arc];
1361 1364
        }
1362 1365
        if (_parent[arc] != INVALID) {
1363 1366
          if (_left[_parent[arc]] == arc) {
1364 1367
            _left[_parent[arc]] = _right[arc];
1365 1368
          } else {
1366 1369
            _right[_parent[arc]] = _right[arc];
1367 1370
          }
1368 1371
        } else {
1369 1372
          _head[_g.source(arc)] = _right[arc];
1370 1373
        }
1371 1374
      } else if (_right[arc] == INVALID) {
1372 1375
        _parent[_left[arc]] = _parent[arc];
1373 1376
        if (_parent[arc] != INVALID) {
1374 1377
          if (_left[_parent[arc]] == arc) {
1375 1378
            _left[_parent[arc]] = _left[arc];
1376 1379
          } else {
1377 1380
            _right[_parent[arc]] = _left[arc];
1378 1381
          }
1379 1382
        } else {
1380 1383
          _head[_g.source(arc)] = _left[arc];
1381 1384
        }
1382 1385
      } else {
1383 1386
        Arc e = _left[arc];
1384 1387
        if (_right[e] != INVALID) {
1385 1388
          e = _right[e];
1386 1389
          while (_right[e] != INVALID) {
1387 1390
            e = _right[e];
1388 1391
          }
1389 1392
          Arc s = _parent[e];
1390 1393
          _right[_parent[e]] = _left[e];
1391 1394
          if (_left[e] != INVALID) {
1392 1395
            _parent[_left[e]] = _parent[e];
1393 1396
          }
1394 1397

	
1395 1398
          _left[e] = _left[arc];
1396 1399
          _parent[_left[arc]] = e;
1397 1400
          _right[e] = _right[arc];
1398 1401
          _parent[_right[arc]] = e;
1399 1402

	
1400 1403
          _parent[e] = _parent[arc];
1401 1404
          if (_parent[arc] != INVALID) {
1402 1405
            if (_left[_parent[arc]] == arc) {
1403 1406
              _left[_parent[arc]] = e;
1404 1407
            } else {
1405 1408
              _right[_parent[arc]] = e;
1406 1409
            }
1407 1410
          }
1408 1411
          splay(s);
1409 1412
        } else {
1410 1413
          _right[e] = _right[arc];
1411 1414
          _parent[_right[arc]] = e;
1412 1415
          _parent[e] = _parent[arc];
1413 1416

	
1414 1417
          if (_parent[arc] != INVALID) {
1415 1418
            if (_left[_parent[arc]] == arc) {
1416 1419
              _left[_parent[arc]] = e;
1417 1420
            } else {
1418 1421
              _right[_parent[arc]] = e;
1419 1422
            }
1420 1423
          } else {
1421 1424
            _head[_g.source(arc)] = e;
1422 1425
          }
1423 1426
        }
1424 1427
      }
1425 1428
    }
1426 1429

	
1427 1430
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1428 1431
    {
1429 1432
      int m=(a+b)/2;
1430 1433
      Arc me=v[m];
1431 1434
      if (a < m) {
1432 1435
        Arc left = refreshRec(v,a,m-1);
1433 1436
        _left[me] = left;
1434 1437
        _parent[left] = me;
1435 1438
      } else {
1436 1439
        _left[me] = INVALID;
1437 1440
      }
1438 1441
      if (m < b) {
1439 1442
        Arc right = refreshRec(v,m+1,b);
1440 1443
        _right[me] = right;
1441 1444
        _parent[right] = me;
1442 1445
      } else {
1443 1446
        _right[me] = INVALID;
1444 1447
      }
1445 1448
      return me;
1446 1449
    }
1447 1450

	
1448 1451
    void refresh() {
1449 1452
      for(NodeIt n(_g);n!=INVALID;++n) {
1450 1453
        std::vector<Arc> v;
1451 1454
        for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1452 1455
        if (!v.empty()) {
1453 1456
          std::sort(v.begin(),v.end(),ArcLess(_g));
1454 1457
          Arc head = refreshRec(v,0,v.size()-1);
1455 1458
          _head[n] = head;
1456 1459
          _parent[head] = INVALID;
1457 1460
        }
1458 1461
        else _head[n] = INVALID;
1459 1462
      }
1460 1463
    }
1461 1464

	
1462 1465
    void zig(Arc v) {
1463 1466
      Arc w = _parent[v];
1464 1467
      _parent[v] = _parent[w];
1465 1468
      _parent[w] = v;
1466 1469
      _left[w] = _right[v];
1467 1470
      _right[v] = w;
1468 1471
      if (_parent[v] != INVALID) {
1469 1472
        if (_right[_parent[v]] == w) {
1470 1473
          _right[_parent[v]] = v;
1471 1474
        } else {
1472 1475
          _left[_parent[v]] = v;
1473 1476
        }
1474 1477
      }
1475 1478
      if (_left[w] != INVALID){
1476 1479
        _parent[_left[w]] = w;
1477 1480
      }
1478 1481
    }
1479 1482

	
1480 1483
    void zag(Arc v) {
1481 1484
      Arc w = _parent[v];
1482 1485
      _parent[v] = _parent[w];
1483 1486
      _parent[w] = v;
1484 1487
      _right[w] = _left[v];
1485 1488
      _left[v] = w;
1486 1489
      if (_parent[v] != INVALID){
1487 1490
        if (_left[_parent[v]] == w) {
1488 1491
          _left[_parent[v]] = v;
1489 1492
        } else {
1490 1493
          _right[_parent[v]] = v;
1491 1494
        }
1492 1495
      }
1493 1496
      if (_right[w] != INVALID){
1494 1497
        _parent[_right[w]] = w;
1495 1498
      }
1496 1499
    }
1497 1500

	
1498 1501
    void splay(Arc v) {
1499 1502
      while (_parent[v] != INVALID) {
1500 1503
        if (v == _left[_parent[v]]) {
1501 1504
          if (_parent[_parent[v]] == INVALID) {
1502 1505
            zig(v);
1503 1506
          } else {
1504 1507
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1505 1508
              zig(_parent[v]);
1506 1509
              zig(v);
1507 1510
            } else {
1508 1511
              zig(v);
1509 1512
              zag(v);
1510 1513
            }
1511 1514
          }
1512 1515
        } else {
1513 1516
          if (_parent[_parent[v]] == INVALID) {
1514 1517
            zag(v);
1515 1518
          } else {
1516 1519
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1517 1520
              zag(v);
1518 1521
              zig(v);
1519 1522
            } else {
1520 1523
              zag(_parent[v]);
1521 1524
              zag(v);
1522 1525
            }
1523 1526
          }
1524 1527
        }
1525 1528
      }
1526 1529
      _head[_g.source(v)] = v;
1527 1530
    }
1528 1531

	
1529 1532

	
1530 1533
  public:
1531 1534

	
1532 1535
    ///Find an arc between two nodes.
1533 1536

	
1534 1537
    ///Find an arc between two nodes.
1535 1538
    ///\param s The source node.
1536 1539
    ///\param t The target node.
1537 1540
    ///\param p The previous arc between \c s and \c t. It it is INVALID or
1538 1541
    ///not given, the operator finds the first appropriate arc.
1539 1542
    ///\return An arc from \c s to \c t after \c p or
1540 1543
    ///\ref INVALID if there is no more.
1541 1544
    ///
1542 1545
    ///For example, you can count the number of arcs from \c u to \c v in the
1543 1546
    ///following way.
1544 1547
    ///\code
1545 1548
    ///DynArcLookUp<ListDigraph> ae(g);
1546 1549
    ///...
1547 1550
    ///int n = 0;
1548 1551
    ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1549 1552
    ///\endcode
1550 1553
    ///
1551 1554
    ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1552 1555
    ///amortized time, specifically, the time complexity of the lookups
1553 1556
    ///is equal to the optimal search tree implementation for the
1554 1557
    ///current query distribution in a constant factor.
1555 1558
    ///
1556 1559
    ///\note This is a dynamic data structure, therefore the data
1557 1560
    ///structure is updated after each graph alteration. Thus although
1558 1561
    ///this data structure is theoretically faster than \ref ArcLookUp
1559 1562
    ///and \ref AllArcLookUp, it often provides worse performance than
1560 1563
    ///them.
1561 1564
    Arc operator()(Node s, Node t, Arc p = INVALID) const  {
1562 1565
      if (p == INVALID) {
1563 1566
        Arc a = _head[s];
1564 1567
        if (a == INVALID) return INVALID;
1565 1568
        Arc r = INVALID;
1566 1569
        while (true) {
1567 1570
          if (_g.target(a) < t) {
1568 1571
            if (_right[a] == INVALID) {
1569 1572
              const_cast<DynArcLookUp&>(*this).splay(a);
1570 1573
              return r;
1571 1574
            } else {
1572 1575
              a = _right[a];
1573 1576
            }
1574 1577
          } else {
1575 1578
            if (_g.target(a) == t) {
1576 1579
              r = a;
1577 1580
            }
1578 1581
            if (_left[a] == INVALID) {
1579 1582
              const_cast<DynArcLookUp&>(*this).splay(a);
1580 1583
              return r;
1581 1584
            } else {
1582 1585
              a = _left[a];
1583 1586
            }
1584 1587
          }
1585 1588
        }
1586 1589
      } else {
1587 1590
        Arc a = p;
1588 1591
        if (_right[a] != INVALID) {
1589 1592
          a = _right[a];
1590 1593
          while (_left[a] != INVALID) {
1591 1594
            a = _left[a];
1592 1595
          }
1593 1596
          const_cast<DynArcLookUp&>(*this).splay(a);
1594 1597
        } else {
1595 1598
          while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
1596 1599
            a = _parent[a];
1597 1600
          }
1598 1601
          if (_parent[a] == INVALID) {
1599 1602
            return INVALID;
1600 1603
          } else {
1601 1604
            a = _parent[a];
1602 1605
            const_cast<DynArcLookUp&>(*this).splay(a);
1603 1606
          }
1604 1607
        }
1605 1608
        if (_g.target(a) == t) return a;
1606 1609
        else return INVALID;
1607 1610
      }
1608 1611
    }
1609 1612

	
1610 1613
  };
1611 1614

	
1612 1615
  ///Fast arc look-up between given endpoints.
1613 1616

	
1614 1617
  ///Using this class, you can find an arc in a digraph from a given
1615 1618
  ///source to a given target in time <em>O</em>(log<em>d</em>),
1616 1619
  ///where <em>d</em> is the out-degree of the source node.
1617 1620
  ///
1618 1621
  ///It is not possible to find \e all parallel arcs between two nodes.
1619 1622
  ///Use \ref AllArcLookUp for this purpose.
1620 1623
  ///
1621 1624
  ///\warning This class is static, so you should call refresh() (or at
1622 1625
  ///least refresh(Node)) to refresh this data structure whenever the
1623 1626
  ///digraph changes. This is a time consuming (superlinearly proportional
1624 1627
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1625 1628
  ///
1626 1629
  ///\tparam GR The type of the underlying digraph.
1627 1630
  ///
1628 1631
  ///\sa DynArcLookUp
1629 1632
  ///\sa AllArcLookUp
1630 1633
  template<class GR>
1631 1634
  class ArcLookUp
1632 1635
  {
1636
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1637

	
1633 1638
  public:
1634
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1639

	
1640
    /// The Digraph type
1635 1641
    typedef GR Digraph;
1636 1642

	
1637 1643
  protected:
1638 1644
    const Digraph &_g;
1639 1645
    typename Digraph::template NodeMap<Arc> _head;
1640 1646
    typename Digraph::template ArcMap<Arc> _left;
1641 1647
    typename Digraph::template ArcMap<Arc> _right;
1642 1648

	
1643 1649
    class ArcLess {
1644 1650
      const Digraph &g;
1645 1651
    public:
1646 1652
      ArcLess(const Digraph &_g) : g(_g) {}
1647 1653
      bool operator()(Arc a,Arc b) const
1648 1654
      {
1649 1655
        return g.target(a)<g.target(b);
1650 1656
      }
1651 1657
    };
1652 1658

	
1653 1659
  public:
1654 1660

	
1655 1661
    ///Constructor
1656 1662

	
1657 1663
    ///Constructor.
1658 1664
    ///
1659 1665
    ///It builds up the search database, which remains valid until the digraph
1660 1666
    ///changes.
1661 1667
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1662 1668

	
1663 1669
  private:
1664 1670
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1665 1671
    {
1666 1672
      int m=(a+b)/2;
1667 1673
      Arc me=v[m];
1668 1674
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1669 1675
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1670 1676
      return me;
1671 1677
    }
1672 1678
  public:
1673 1679
    ///Refresh the search data structure at a node.
1674 1680

	
1675 1681
    ///Build up the search database of node \c n.
1676 1682
    ///
1677 1683
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1678 1684
    ///is the number of the outgoing arcs of \c n.
1679 1685
    void refresh(Node n)
1680 1686
    {
1681 1687
      std::vector<Arc> v;
1682 1688
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1683 1689
      if(v.size()) {
1684 1690
        std::sort(v.begin(),v.end(),ArcLess(_g));
1685 1691
        _head[n]=refreshRec(v,0,v.size()-1);
1686 1692
      }
1687 1693
      else _head[n]=INVALID;
1688 1694
    }
1689 1695
    ///Refresh the full data structure.
1690 1696

	
1691 1697
    ///Build up the full search database. In fact, it simply calls
1692 1698
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1693 1699
    ///
1694 1700
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1695 1701
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1696 1702
    ///out-degree of the digraph.
1697 1703
    void refresh()
1698 1704
    {
1699 1705
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1700 1706
    }
1701 1707

	
1702 1708
    ///Find an arc between two nodes.
1703 1709

	
1704 1710
    ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1705 1711
    ///where <em>d</em> is the number of outgoing arcs of \c s.
1706 1712
    ///\param s The source node.
1707 1713
    ///\param t The target node.
1708 1714
    ///\return An arc from \c s to \c t if there exists,
1709 1715
    ///\ref INVALID otherwise.
1710 1716
    ///
1711 1717
    ///\warning If you change the digraph, refresh() must be called before using
1712 1718
    ///this operator. If you change the outgoing arcs of
1713 1719
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1714 1720
    Arc operator()(Node s, Node t) const
1715 1721
    {
1716 1722
      Arc e;
1717 1723
      for(e=_head[s];
1718 1724
          e!=INVALID&&_g.target(e)!=t;
1719 1725
          e = t < _g.target(e)?_left[e]:_right[e]) ;
1720 1726
      return e;
1721 1727
    }
1722 1728

	
1723 1729
  };
1724 1730

	
1725 1731
  ///Fast look-up of all arcs between given endpoints.
1726 1732

	
1727 1733
  ///This class is the same as \ref ArcLookUp, with the addition
1728 1734
  ///that it makes it possible to find all parallel arcs between given
1729 1735
  ///endpoints.
1730 1736
  ///
1731 1737
  ///\warning This class is static, so you should call refresh() (or at
1732 1738
  ///least refresh(Node)) to refresh this data structure whenever the
1733 1739
  ///digraph changes. This is a time consuming (superlinearly proportional
1734 1740
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1735 1741
  ///
1736 1742
  ///\tparam GR The type of the underlying digraph.
1737 1743
  ///
1738 1744
  ///\sa DynArcLookUp
1739 1745
  ///\sa ArcLookUp
1740 1746
  template<class GR>
1741 1747
  class AllArcLookUp : public ArcLookUp<GR>
1742 1748
  {
1743 1749
    using ArcLookUp<GR>::_g;
1744 1750
    using ArcLookUp<GR>::_right;
1745 1751
    using ArcLookUp<GR>::_left;
1746 1752
    using ArcLookUp<GR>::_head;
1747 1753

	
1748 1754
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1749
    typedef GR Digraph;
1750 1755

	
1751
    typename Digraph::template ArcMap<Arc> _next;
1756
    typename GR::template ArcMap<Arc> _next;
1752 1757

	
1753 1758
    Arc refreshNext(Arc head,Arc next=INVALID)
1754 1759
    {
1755 1760
      if(head==INVALID) return next;
1756 1761
      else {
1757 1762
        next=refreshNext(_right[head],next);
1758 1763
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1759 1764
          ? next : INVALID;
1760 1765
        return refreshNext(_left[head],head);
1761 1766
      }
1762 1767
    }
1763 1768

	
1764 1769
    void refreshNext()
1765 1770
    {
1766 1771
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1767 1772
    }
1768 1773

	
1769 1774
  public:
1775

	
1776
    /// The Digraph type
1777
    typedef GR Digraph;
1778

	
1770 1779
    ///Constructor
1771 1780

	
1772 1781
    ///Constructor.
1773 1782
    ///
1774 1783
    ///It builds up the search database, which remains valid until the digraph
1775 1784
    ///changes.
1776 1785
    AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
1777 1786

	
1778 1787
    ///Refresh the data structure at a node.
1779 1788

	
1780 1789
    ///Build up the search database of node \c n.
1781 1790
    ///
1782 1791
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1783 1792
    ///the number of the outgoing arcs of \c n.
1784 1793
    void refresh(Node n)
1785 1794
    {
1786 1795
      ArcLookUp<GR>::refresh(n);
1787 1796
      refreshNext(_head[n]);
1788 1797
    }
1789 1798

	
1790 1799
    ///Refresh the full data structure.
1791 1800

	
1792 1801
    ///Build up the full search database. In fact, it simply calls
1793 1802
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1794 1803
    ///
1795 1804
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1796 1805
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1797 1806
    ///out-degree of the digraph.
1798 1807
    void refresh()
1799 1808
    {
1800 1809
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1801 1810
    }
1802 1811

	
1803 1812
    ///Find an arc between two nodes.
1804 1813

	
1805 1814
    ///Find an arc between two nodes.
1806 1815
    ///\param s The source node.
1807 1816
    ///\param t The target node.
1808 1817
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1809 1818
    ///not given, the operator finds the first appropriate arc.
1810 1819
    ///\return An arc from \c s to \c t after \c prev or
1811 1820
    ///\ref INVALID if there is no more.
1812 1821
    ///
1813 1822
    ///For example, you can count the number of arcs from \c u to \c v in the
1814 1823
    ///following way.
1815 1824
    ///\code
1816 1825
    ///AllArcLookUp<ListDigraph> ae(g);
1817 1826
    ///...
1818 1827
    ///int n = 0;
1819 1828
    ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1820 1829
    ///\endcode
1821 1830
    ///
1822 1831
    ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1823 1832
    ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1824 1833
    ///consecutive arcs are found in constant time.
1825 1834
    ///
1826 1835
    ///\warning If you change the digraph, refresh() must be called before using
1827 1836
    ///this operator. If you change the outgoing arcs of
1828 1837
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1829 1838
    ///
1830 1839
#ifdef DOXYGEN
1831 1840
    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
1832 1841
#else
1833 1842
    using ArcLookUp<GR>::operator() ;
1834 1843
    Arc operator()(Node s, Node t, Arc prev) const
1835 1844
    {
1836 1845
      return prev==INVALID?(*this)(s,t):_next[prev];
1837 1846
    }
1838 1847
#endif
1839 1848

	
1840 1849
  };
1841 1850

	
1842 1851
  /// @}
1843 1852

	
1844 1853
} //namespace lemon
1845 1854

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

	
19 19
#ifndef LEMON_EDGE_SET_H
20 20
#define LEMON_EDGE_SET_H
21 21

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

	
25 25
/// \ingroup semi_adaptors
26 26
/// \file
27 27
/// \brief ArcSet and EdgeSet classes.
28 28
///
29 29
/// Graphs which use another graph's node-set as own.
30 30
namespace lemon {
31 31

	
32 32
  template <typename GR>
33 33
  class ListArcSetBase {
34 34
  public:
35 35

	
36
    typedef GR Graph;
37 36
    typedef typename GR::Node Node;
38 37
    typedef typename GR::NodeIt NodeIt;
39 38

	
40 39
  protected:
41 40

	
42 41
    struct NodeT {
43 42
      int first_out, first_in;
44 43
      NodeT() : first_out(-1), first_in(-1) {}
45 44
    };
46 45

	
47 46
    typedef typename ItemSetTraits<GR, Node>::
48 47
    template Map<NodeT>::Type NodesImplBase;
49 48

	
50 49
    NodesImplBase* _nodes;
51 50

	
52 51
    struct ArcT {
53 52
      Node source, target;
54 53
      int next_out, next_in;
55 54
      int prev_out, prev_in;
56 55
      ArcT() : prev_out(-1), prev_in(-1) {}
57 56
    };
58 57

	
59 58
    std::vector<ArcT> arcs;
60 59

	
61 60
    int first_arc;
62 61
    int first_free_arc;
63 62

	
64 63
    const GR* _graph;
65 64

	
66 65
    void initalize(const GR& graph, NodesImplBase& nodes) {
67 66
      _graph = &graph;
68 67
      _nodes = &nodes;
69 68
    }
70 69

	
71 70
  public:
72 71

	
73 72
    class Arc {
74 73
      friend class ListArcSetBase<GR>;
75 74
    protected:
76 75
      Arc(int _id) : id(_id) {}
77 76
      int id;
78 77
    public:
79 78
      Arc() {}
80 79
      Arc(Invalid) : id(-1) {}
81 80
      bool operator==(const Arc& arc) const { return id == arc.id; }
82 81
      bool operator!=(const Arc& arc) const { return id != arc.id; }
83 82
      bool operator<(const Arc& arc) const { return id < arc.id; }
84 83
    };
85 84

	
86 85
    ListArcSetBase() : first_arc(-1), first_free_arc(-1) {}
87 86

	
88 87
    Arc addArc(const Node& u, const Node& v) {
89 88
      int n;
90 89
      if (first_free_arc == -1) {
91 90
        n = arcs.size();
92 91
        arcs.push_back(ArcT());
93 92
      } else {
94 93
        n = first_free_arc;
95 94
        first_free_arc = arcs[first_free_arc].next_in;
96 95
      }
97 96
      arcs[n].next_in = (*_nodes)[v].first_in;
98 97
      if ((*_nodes)[v].first_in != -1) {
99 98
        arcs[(*_nodes)[v].first_in].prev_in = n;
100 99
      }
101 100
      (*_nodes)[v].first_in = n;
102 101
      arcs[n].next_out = (*_nodes)[u].first_out;
103 102
      if ((*_nodes)[u].first_out != -1) {
104 103
        arcs[(*_nodes)[u].first_out].prev_out = n;
105 104
      }
106 105
      (*_nodes)[u].first_out = n;
107 106
      arcs[n].source = u;
108 107
      arcs[n].target = v;
109 108
      return Arc(n);
110 109
    }
111 110

	
112 111
    void erase(const Arc& arc) {
113 112
      int n = arc.id;
114 113
      if (arcs[n].prev_in != -1) {
115 114
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
116 115
      } else {
117 116
        (*_nodes)[arcs[n].target].first_in = arcs[n].next_in;
118 117
      }
119 118
      if (arcs[n].next_in != -1) {
120 119
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
121 120
      }
122 121

	
123 122
      if (arcs[n].prev_out != -1) {
124 123
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
125 124
      } else {
126 125
        (*_nodes)[arcs[n].source].first_out = arcs[n].next_out;
127 126
      }
128 127
      if (arcs[n].next_out != -1) {
129 128
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
130 129
      }
131 130

	
132 131
    }
133 132

	
134 133
    void clear() {
135 134
      Node node;
136 135
      for (first(node); node != INVALID; next(node)) {
137 136
        (*_nodes)[node].first_in = -1;
138 137
        (*_nodes)[node].first_out = -1;
139 138
      }
140 139
      arcs.clear();
141 140
      first_arc = -1;
142 141
      first_free_arc = -1;
143 142
    }
144 143

	
145 144
    void first(Node& node) const {
146 145
      _graph->first(node);
147 146
    }
148 147

	
149 148
    void next(Node& node) const {
150 149
      _graph->next(node);
151 150
    }
152 151

	
153 152
    void first(Arc& arc) const {
154 153
      Node node;
155 154
      first(node);
156 155
      while (node != INVALID && (*_nodes)[node].first_in == -1) {
157 156
        next(node);
158 157
      }
159 158
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
160 159
    }
161 160

	
162 161
    void next(Arc& arc) const {
163 162
      if (arcs[arc.id].next_in != -1) {
164 163
        arc.id = arcs[arc.id].next_in;
165 164
      } else {
166 165
        Node node = arcs[arc.id].target;
167 166
        next(node);
168 167
        while (node != INVALID && (*_nodes)[node].first_in == -1) {
169 168
          next(node);
170 169
        }
171 170
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
172 171
      }
173 172
    }
174 173

	
175 174
    void firstOut(Arc& arc, const Node& node) const {
176 175
      arc.id = (*_nodes)[node].first_out;
177 176
    }
178 177

	
179 178
    void nextOut(Arc& arc) const {
180 179
      arc.id = arcs[arc.id].next_out;
181 180
    }
182 181

	
183 182
    void firstIn(Arc& arc, const Node& node) const {
184 183
      arc.id = (*_nodes)[node].first_in;
185 184
    }
186 185

	
187 186
    void nextIn(Arc& arc) const {
188 187
      arc.id = arcs[arc.id].next_in;
189 188
    }
190 189

	
191 190
    int id(const Node& node) const { return _graph->id(node); }
192 191
    int id(const Arc& arc) const { return arc.id; }
193 192

	
194 193
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
195 194
    Arc arcFromId(int ix) const { return Arc(ix); }
196 195

	
197 196
    int maxNodeId() const { return _graph->maxNodeId(); };
198 197
    int maxArcId() const { return arcs.size() - 1; }
199 198

	
200 199
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
201 200
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
202 201

	
203 202
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
204 203

	
205 204
    NodeNotifier& notifier(Node) const {
206 205
      return _graph->notifier(Node());
207 206
    }
208 207

	
209 208
    template <typename V>
210 209
    class NodeMap : public GR::template NodeMap<V> {
210
      typedef typename GR::template NodeMap<V> Parent;
211

	
211 212
    public:
212 213

	
213
      typedef typename GR::template NodeMap<V> Parent;
214

	
215 214
      explicit NodeMap(const ListArcSetBase<GR>& arcset)
216 215
        : Parent(*arcset._graph) {}
217 216

	
218 217
      NodeMap(const ListArcSetBase<GR>& arcset, const V& value)
219 218
        : Parent(*arcset._graph, value) {}
220 219

	
221 220
      NodeMap& operator=(const NodeMap& cmap) {
222 221
        return operator=<NodeMap>(cmap);
223 222
      }
224 223

	
225 224
      template <typename CMap>
226 225
      NodeMap& operator=(const CMap& cmap) {
227 226
        Parent::operator=(cmap);
228 227
        return *this;
229 228
      }
230 229
    };
231 230

	
232 231
  };
233 232

	
234 233
  /// \ingroup semi_adaptors
235 234
  ///
236 235
  /// \brief Digraph using a node set of another digraph or graph and
237 236
  /// an own arc set.
238 237
  ///
239 238
  /// This structure can be used to establish another directed graph
240 239
  /// over a node set of an existing one. This class uses the same
241 240
  /// Node type as the underlying graph, and each valid node of the
242 241
  /// original graph is valid in this arc set, therefore the node
243 242
  /// objects of the original graph can be used directly with this
244 243
  /// class. The node handling functions (id handling, observing, and
245 244
  /// iterators) works equivalently as in the original graph.
246 245
  ///
247 246
  /// This implementation is based on doubly-linked lists, from each
248 247
  /// node the outgoing and the incoming arcs make up lists, therefore
249 248
  /// one arc can be erased in constant time. It also makes possible,
250 249
  /// that node can be removed from the underlying graph, in this case
251 250
  /// all arcs incident to the given node is erased from the arc set.
252 251
  ///
253 252
  /// \param GR The type of the graph which shares its node set with
254 253
  /// this class. Its interface must conform to the
255 254
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
256 255
  /// concept.
257 256
  ///
258 257
  /// This class fully conforms to the \ref concepts::Digraph
259 258
  /// "Digraph" concept.
260 259
  template <typename GR>
261 260
  class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > {
261
    typedef ArcSetExtender<ListArcSetBase<GR> > Parent;
262 262

	
263 263
  public:
264 264

	
265
    typedef ArcSetExtender<ListArcSetBase<GR> > Parent;
266

	
267 265
    typedef typename Parent::Node Node;
268 266
    typedef typename Parent::Arc Arc;
269 267

	
270
    typedef GR Graph;
271

	
272

	
273 268
    typedef typename Parent::NodesImplBase NodesImplBase;
274 269

	
275 270
    void eraseNode(const Node& node) {
276 271
      Arc arc;
277 272
      Parent::firstOut(arc, node);
278 273
      while (arc != INVALID ) {
279 274
        erase(arc);
280 275
        Parent::firstOut(arc, node);
281 276
      }
282 277

	
283 278
      Parent::firstIn(arc, node);
284 279
      while (arc != INVALID ) {
285 280
        erase(arc);
286 281
        Parent::firstIn(arc, node);
287 282
      }
288 283
    }
289 284

	
290 285
    void clearNodes() {
291 286
      Parent::clear();
292 287
    }
293 288

	
294 289
    class NodesImpl : public NodesImplBase {
295
    public:
296 290
      typedef NodesImplBase Parent;
297 291

	
292
    public:
298 293
      NodesImpl(const GR& graph, ListArcSet& arcset)
299 294
        : Parent(graph), _arcset(arcset) {}
300 295

	
301 296
      virtual ~NodesImpl() {}
302 297

	
303 298
    protected:
304 299

	
305 300
      virtual void erase(const Node& node) {
306 301
        _arcset.eraseNode(node);
307 302
        Parent::erase(node);
308 303
      }
309 304
      virtual void erase(const std::vector<Node>& nodes) {
310 305
        for (int i = 0; i < int(nodes.size()); ++i) {
311 306
          _arcset.eraseNode(nodes[i]);
312 307
        }
313 308
        Parent::erase(nodes);
314 309
      }
315 310
      virtual void clear() {
316 311
        _arcset.clearNodes();
317 312
        Parent::clear();
318 313
      }
319 314

	
320 315
    private:
321 316
      ListArcSet& _arcset;
322 317
    };
323 318

	
324 319
    NodesImpl _nodes;
325 320

	
326 321
  public:
327 322

	
328 323
    /// \brief Constructor of the ArcSet.
329 324
    ///
330 325
    /// Constructor of the ArcSet.
331 326
    ListArcSet(const GR& graph) : _nodes(graph, *this) {
332 327
      Parent::initalize(graph, _nodes);
333 328
    }
334 329

	
335 330
    /// \brief Add a new arc to the digraph.
336 331
    ///
337 332
    /// Add a new arc to the digraph with source node \c s
338 333
    /// and target node \c t.
339 334
    /// \return The new arc.
340 335
    Arc addArc(const Node& s, const Node& t) {
341 336
      return Parent::addArc(s, t);
342 337
    }
343 338

	
344 339
    /// \brief Erase an arc from the digraph.
345 340
    ///
346 341
    /// Erase an arc \c a from the digraph.
347 342
    void erase(const Arc& a) {
348 343
      return Parent::erase(a);
349 344
    }
350 345

	
351 346
  };
352 347

	
353 348
  template <typename GR>
354 349
  class ListEdgeSetBase {
355 350
  public:
356 351

	
357
    typedef GR Graph;
358 352
    typedef typename GR::Node Node;
359 353
    typedef typename GR::NodeIt NodeIt;
360 354

	
361 355
  protected:
362 356

	
363 357
    struct NodeT {
364 358
      int first_out;
365 359
      NodeT() : first_out(-1) {}
366 360
    };
367 361

	
368 362
    typedef typename ItemSetTraits<GR, Node>::
369 363
    template Map<NodeT>::Type NodesImplBase;
370 364

	
371 365
    NodesImplBase* _nodes;
372 366

	
373 367
    struct ArcT {
374 368
      Node target;
375 369
      int prev_out, next_out;
376 370
      ArcT() : prev_out(-1), next_out(-1) {}
377 371
    };
378 372

	
379 373
    std::vector<ArcT> arcs;
380 374

	
381 375
    int first_arc;
382 376
    int first_free_arc;
383 377

	
384 378
    const GR* _graph;
385 379

	
386 380
    void initalize(const GR& graph, NodesImplBase& nodes) {
387 381
      _graph = &graph;
388 382
      _nodes = &nodes;
389 383
    }
390 384

	
391 385
  public:
392 386

	
393 387
    class Edge {
394 388
      friend class ListEdgeSetBase;
395 389
    protected:
396 390

	
397 391
      int id;
398 392
      explicit Edge(int _id) { id = _id;}
399 393

	
400 394
    public:
401 395
      Edge() {}
402 396
      Edge (Invalid) { id = -1; }
403 397
      bool operator==(const Edge& arc) const {return id == arc.id;}
404 398
      bool operator!=(const Edge& arc) const {return id != arc.id;}
405 399
      bool operator<(const Edge& arc) const {return id < arc.id;}
406 400
    };
407 401

	
408 402
    class Arc {
409 403
      friend class ListEdgeSetBase;
410 404
    protected:
411 405
      Arc(int _id) : id(_id) {}
412 406
      int id;
413 407
    public:
414 408
      operator Edge() const { return edgeFromId(id / 2); }
415 409

	
416 410
      Arc() {}
417 411
      Arc(Invalid) : id(-1) {}
418 412
      bool operator==(const Arc& arc) const { return id == arc.id; }
419 413
      bool operator!=(const Arc& arc) const { return id != arc.id; }
420 414
      bool operator<(const Arc& arc) const { return id < arc.id; }
421 415
    };
422 416

	
423 417
    ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {}
424 418

	
425 419
    Edge addEdge(const Node& u, const Node& v) {
426 420
      int n;
427 421

	
428 422
      if (first_free_arc == -1) {
429 423
        n = arcs.size();
430 424
        arcs.push_back(ArcT());
431 425
        arcs.push_back(ArcT());
432 426
      } else {
433 427
        n = first_free_arc;
434 428
        first_free_arc = arcs[n].next_out;
435 429
      }
436 430

	
437 431
      arcs[n].target = u;
438 432
      arcs[n | 1].target = v;
439 433

	
440 434
      arcs[n].next_out = (*_nodes)[v].first_out;
441 435
      if ((*_nodes)[v].first_out != -1) {
442 436
        arcs[(*_nodes)[v].first_out].prev_out = n;
443 437
      }
444 438
      (*_nodes)[v].first_out = n;
445 439
      arcs[n].prev_out = -1;
446 440

	
447 441
      if ((*_nodes)[u].first_out != -1) {
448 442
        arcs[(*_nodes)[u].first_out].prev_out = (n | 1);
449 443
      }
450 444
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
451 445
      (*_nodes)[u].first_out = (n | 1);
452 446
      arcs[n | 1].prev_out = -1;
453 447

	
454 448
      return Edge(n / 2);
455 449
    }
456 450

	
457 451
    void erase(const Edge& arc) {
458 452
      int n = arc.id * 2;
459 453

	
460 454
      if (arcs[n].next_out != -1) {
461 455
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
462 456
      }
463 457

	
464 458
      if (arcs[n].prev_out != -1) {
465 459
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
466 460
      } else {
467 461
        (*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out;
468 462
      }
469 463

	
470 464
      if (arcs[n | 1].next_out != -1) {
471 465
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
472 466
      }
473 467

	
474 468
      if (arcs[n | 1].prev_out != -1) {
475 469
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
476 470
      } else {
477 471
        (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
478 472
      }
479 473

	
480 474
      arcs[n].next_out = first_free_arc;
481 475
      first_free_arc = n;
482 476

	
483 477
    }
484 478

	
485 479
    void clear() {
486 480
      Node node;
487 481
      for (first(node); node != INVALID; next(node)) {
488 482
        (*_nodes)[node].first_out = -1;
489 483
      }
490 484
      arcs.clear();
491 485
      first_arc = -1;
492 486
      first_free_arc = -1;
493 487
    }
494 488

	
495 489
    void first(Node& node) const {
496 490
      _graph->first(node);
497 491
    }
498 492

	
499 493
    void next(Node& node) const {
500 494
      _graph->next(node);
501 495
    }
502 496

	
503 497
    void first(Arc& arc) const {
504 498
      Node node;
505 499
      first(node);
506 500
      while (node != INVALID && (*_nodes)[node].first_out == -1) {
507 501
        next(node);
508 502
      }
509 503
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
510 504
    }
511 505

	
512 506
    void next(Arc& arc) const {
513 507
      if (arcs[arc.id].next_out != -1) {
514 508
        arc.id = arcs[arc.id].next_out;
515 509
      } else {
516 510
        Node node = arcs[arc.id ^ 1].target;
517 511
        next(node);
518 512
        while(node != INVALID && (*_nodes)[node].first_out == -1) {
519 513
          next(node);
520 514
        }
521 515
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
522 516
      }
523 517
    }
524 518

	
525 519
    void first(Edge& edge) const {
526 520
      Node node;
527 521
      first(node);
528 522
      while (node != INVALID) {
529 523
        edge.id = (*_nodes)[node].first_out;
530 524
        while ((edge.id & 1) != 1) {
531 525
          edge.id = arcs[edge.id].next_out;
532 526
        }
533 527
        if (edge.id != -1) {
534 528
          edge.id /= 2;
535 529
          return;
536 530
        }
537 531
        next(node);
538 532
      }
539 533
      edge.id = -1;
540 534
    }
541 535

	
542 536
    void next(Edge& edge) const {
543 537
      Node node = arcs[edge.id * 2].target;
544 538
      edge.id = arcs[(edge.id * 2) | 1].next_out;
545 539
      while ((edge.id & 1) != 1) {
546 540
        edge.id = arcs[edge.id].next_out;
547 541
      }
548 542
      if (edge.id != -1) {
549 543
        edge.id /= 2;
550 544
        return;
551 545
      }
552 546
      next(node);
553 547
      while (node != INVALID) {
554 548
        edge.id = (*_nodes)[node].first_out;
555 549
        while ((edge.id & 1) != 1) {
556 550
          edge.id = arcs[edge.id].next_out;
557 551
        }
558 552
        if (edge.id != -1) {
559 553
          edge.id /= 2;
560 554
          return;
561 555
        }
562 556
        next(node);
563 557
      }
564 558
      edge.id = -1;
565 559
    }
566 560

	
567 561
    void firstOut(Arc& arc, const Node& node) const {
568 562
      arc.id = (*_nodes)[node].first_out;
569 563
    }
570 564

	
571 565
    void nextOut(Arc& arc) const {
572 566
      arc.id = arcs[arc.id].next_out;
573 567
    }
574 568

	
575 569
    void firstIn(Arc& arc, const Node& node) const {
576 570
      arc.id = (((*_nodes)[node].first_out) ^ 1);
577 571
      if (arc.id == -2) arc.id = -1;
578 572
    }
579 573

	
580 574
    void nextIn(Arc& arc) const {
581 575
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
582 576
      if (arc.id == -2) arc.id = -1;
583 577
    }
584 578

	
585 579
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
586 580
      int de = (*_nodes)[node].first_out;
587 581
      if (de != -1 ) {
588 582
        arc.id = de / 2;
589 583
        dir = ((de & 1) == 1);
590 584
      } else {
591 585
        arc.id = -1;
592 586
        dir = true;
593 587
      }
594 588
    }
595 589
    void nextInc(Edge &arc, bool& dir) const {
596 590
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
597 591
      if (de != -1 ) {
598 592
        arc.id = de / 2;
599 593
        dir = ((de & 1) == 1);
600 594
      } else {
601 595
        arc.id = -1;
602 596
        dir = true;
603 597
      }
604 598
    }
605 599

	
606 600
    static bool direction(Arc arc) {
607 601
      return (arc.id & 1) == 1;
608 602
    }
609 603

	
610 604
    static Arc direct(Edge edge, bool dir) {
611 605
      return Arc(edge.id * 2 + (dir ? 1 : 0));
612 606
    }
613 607

	
614 608
    int id(const Node& node) const { return _graph->id(node); }
615 609
    static int id(Arc e) { return e.id; }
616 610
    static int id(Edge e) { return e.id; }
617 611

	
618 612
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
619 613
    static Arc arcFromId(int id) { return Arc(id);}
620 614
    static Edge edgeFromId(int id) { return Edge(id);}
621 615

	
622 616
    int maxNodeId() const { return _graph->maxNodeId(); };
623 617
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
624 618
    int maxArcId() const { return arcs.size()-1; }
625 619

	
626 620
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
627 621
    Node target(Arc e) const { return arcs[e.id].target; }
628 622

	
629 623
    Node u(Edge e) const { return arcs[2 * e.id].target; }
630 624
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
631 625

	
632 626
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
633 627

	
634 628
    NodeNotifier& notifier(Node) const {
635 629
      return _graph->notifier(Node());
636 630
    }
637 631

	
638 632
    template <typename V>
639 633
    class NodeMap : public GR::template NodeMap<V> {
634
      typedef typename GR::template NodeMap<V> Parent;
635

	
640 636
    public:
641 637

	
642
      typedef typename GR::template NodeMap<V> Parent;
643

	
644 638
      explicit NodeMap(const ListEdgeSetBase<GR>& arcset)
645 639
        : Parent(*arcset._graph) {}
646 640

	
647 641
      NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value)
648 642
        : Parent(*arcset._graph, value) {}
649 643

	
650 644
      NodeMap& operator=(const NodeMap& cmap) {
651 645
        return operator=<NodeMap>(cmap);
652 646
      }
653 647

	
654 648
      template <typename CMap>
655 649
      NodeMap& operator=(const CMap& cmap) {
656 650
        Parent::operator=(cmap);
657 651
        return *this;
658 652
      }
659 653
    };
660 654

	
661 655
  };
662 656

	
663 657
  /// \ingroup semi_adaptors
664 658
  ///
665 659
  /// \brief Graph using a node set of another digraph or graph and an
666 660
  /// own edge set.
667 661
  ///
668 662
  /// This structure can be used to establish another graph over a
669 663
  /// node set of an existing one. This class uses the same Node type
670 664
  /// as the underlying graph, and each valid node of the original
671 665
  /// graph is valid in this arc set, therefore the node objects of
672 666
  /// the original graph can be used directly with this class. The
673 667
  /// node handling functions (id handling, observing, and iterators)
674 668
  /// works equivalently as in the original graph.
675 669
  ///
676 670
  /// This implementation is based on doubly-linked lists, from each
677 671
  /// node the incident edges make up lists, therefore one edge can be
678 672
  /// erased in constant time. It also makes possible, that node can
679 673
  /// be removed from the underlying graph, in this case all edges
680 674
  /// incident to the given node is erased from the arc set.
681 675
  ///
682 676
  /// \param GR The type of the graph which shares its node set
683 677
  /// with this class. Its interface must conform to the
684 678
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
685 679
  /// concept.
686 680
  ///
687 681
  /// This class fully conforms to the \ref concepts::Graph "Graph"
688 682
  /// concept.
689 683
  template <typename GR>
690 684
  class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > {
685
    typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent;
691 686

	
692 687
  public:
693 688

	
694
    typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent;
695

	
696 689
    typedef typename Parent::Node Node;
697 690
    typedef typename Parent::Arc Arc;
698 691
    typedef typename Parent::Edge Edge;
699 692

	
700
    typedef GR Graph;
701

	
702

	
703 693
    typedef typename Parent::NodesImplBase NodesImplBase;
704 694

	
705 695
    void eraseNode(const Node& node) {
706 696
      Arc arc;
707 697
      Parent::firstOut(arc, node);
708 698
      while (arc != INVALID ) {
709 699
        erase(arc);
710 700
        Parent::firstOut(arc, node);
711 701
      }
712 702

	
713 703
    }
714 704

	
715 705
    void clearNodes() {
716 706
      Parent::clear();
717 707
    }
718 708

	
719 709
    class NodesImpl : public NodesImplBase {
720
    public:
721 710
      typedef NodesImplBase Parent;
722 711

	
712
    public:
723 713
      NodesImpl(const GR& graph, ListEdgeSet& arcset)
724 714
        : Parent(graph), _arcset(arcset) {}
725 715

	
726 716
      virtual ~NodesImpl() {}
727 717

	
728 718
    protected:
729 719

	
730 720
      virtual void erase(const Node& node) {
731 721
        _arcset.eraseNode(node);
732 722
        Parent::erase(node);
733 723
      }
734 724
      virtual void erase(const std::vector<Node>& nodes) {
735 725
        for (int i = 0; i < int(nodes.size()); ++i) {
736 726
          _arcset.eraseNode(nodes[i]);
737 727
        }
738 728
        Parent::erase(nodes);
739 729
      }
740 730
      virtual void clear() {
741 731
        _arcset.clearNodes();
742 732
        Parent::clear();
743 733
      }
744 734

	
745 735
    private:
746 736
      ListEdgeSet& _arcset;
747 737
    };
748 738

	
749 739
    NodesImpl _nodes;
750 740

	
751 741
  public:
752 742

	
753 743
    /// \brief Constructor of the EdgeSet.
754 744
    ///
755 745
    /// Constructor of the EdgeSet.
756 746
    ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
757 747
      Parent::initalize(graph, _nodes);
758 748
    }
759 749

	
760 750
    /// \brief Add a new edge to the graph.
761 751
    ///
762 752
    /// Add a new edge to the graph with node \c u
763 753
    /// and node \c v endpoints.
764 754
    /// \return The new edge.
765 755
    Edge addEdge(const Node& u, const Node& v) {
766 756
      return Parent::addEdge(u, v);
767 757
    }
768 758

	
769 759
    /// \brief Erase an edge from the graph.
770 760
    ///
771 761
    /// Erase the edge \c e from the graph.
772 762
    void erase(const Edge& e) {
773 763
      return Parent::erase(e);
774 764
    }
775 765

	
776 766
  };
777 767

	
778 768
  template <typename GR>
779 769
  class SmartArcSetBase {
780 770
  public:
781 771

	
782
    typedef GR Graph;
783
    typedef typename Graph::Node Node;
784
    typedef typename Graph::NodeIt NodeIt;
772
    typedef typename GR::Node Node;
773
    typedef typename GR::NodeIt NodeIt;
785 774

	
786 775
  protected:
787 776

	
788 777
    struct NodeT {
789 778
      int first_out, first_in;
790 779
      NodeT() : first_out(-1), first_in(-1) {}
791 780
    };
792 781

	
793 782
    typedef typename ItemSetTraits<GR, Node>::
794 783
    template Map<NodeT>::Type NodesImplBase;
795 784

	
796 785
    NodesImplBase* _nodes;
797 786

	
798 787
    struct ArcT {
799 788
      Node source, target;
800 789
      int next_out, next_in;
801 790
      ArcT() {}
802 791
    };
803 792

	
804 793
    std::vector<ArcT> arcs;
805 794

	
806 795
    const GR* _graph;
807 796

	
808 797
    void initalize(const GR& graph, NodesImplBase& nodes) {
809 798
      _graph = &graph;
810 799
      _nodes = &nodes;
811 800
    }
812 801

	
813 802
  public:
814 803

	
815 804
    class Arc {
816 805
      friend class SmartArcSetBase<GR>;
817 806
    protected:
818 807
      Arc(int _id) : id(_id) {}
819 808
      int id;
820 809
    public:
821 810
      Arc() {}
822 811
      Arc(Invalid) : id(-1) {}
823 812
      bool operator==(const Arc& arc) const { return id == arc.id; }
824 813
      bool operator!=(const Arc& arc) const { return id != arc.id; }
825 814
      bool operator<(const Arc& arc) const { return id < arc.id; }
826 815
    };
827 816

	
828 817
    SmartArcSetBase() {}
829 818

	
830 819
    Arc addArc(const Node& u, const Node& v) {
831 820
      int n = arcs.size();
832 821
      arcs.push_back(ArcT());
833 822
      arcs[n].next_in = (*_nodes)[v].first_in;
834 823
      (*_nodes)[v].first_in = n;
835 824
      arcs[n].next_out = (*_nodes)[u].first_out;
836 825
      (*_nodes)[u].first_out = n;
837 826
      arcs[n].source = u;
838 827
      arcs[n].target = v;
839 828
      return Arc(n);
840 829
    }
841 830

	
842 831
    void clear() {
843 832
      Node node;
844 833
      for (first(node); node != INVALID; next(node)) {
845 834
        (*_nodes)[node].first_in = -1;
846 835
        (*_nodes)[node].first_out = -1;
847 836
      }
848 837
      arcs.clear();
849 838
    }
850 839

	
851 840
    void first(Node& node) const {
852 841
      _graph->first(node);
853 842
    }
854 843

	
855 844
    void next(Node& node) const {
856 845
      _graph->next(node);
857 846
    }
858 847

	
859 848
    void first(Arc& arc) const {
860 849
      arc.id = arcs.size() - 1;
861 850
    }
862 851

	
863 852
    void next(Arc& arc) const {
864 853
      --arc.id;
865 854
    }
866 855

	
867 856
    void firstOut(Arc& arc, const Node& node) const {
868 857
      arc.id = (*_nodes)[node].first_out;
869 858
    }
870 859

	
871 860
    void nextOut(Arc& arc) const {
872 861
      arc.id = arcs[arc.id].next_out;
873 862
    }
874 863

	
875 864
    void firstIn(Arc& arc, const Node& node) const {
876 865
      arc.id = (*_nodes)[node].first_in;
877 866
    }
878 867

	
879 868
    void nextIn(Arc& arc) const {
880 869
      arc.id = arcs[arc.id].next_in;
881 870
    }
882 871

	
883 872
    int id(const Node& node) const { return _graph->id(node); }
884 873
    int id(const Arc& arc) const { return arc.id; }
885 874

	
886 875
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
887 876
    Arc arcFromId(int ix) const { return Arc(ix); }
888 877

	
889 878
    int maxNodeId() const { return _graph->maxNodeId(); };
890 879
    int maxArcId() const { return arcs.size() - 1; }
891 880

	
892 881
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
893 882
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
894 883

	
895 884
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
896 885

	
897 886
    NodeNotifier& notifier(Node) const {
898 887
      return _graph->notifier(Node());
899 888
    }
900 889

	
901 890
    template <typename V>
902 891
    class NodeMap : public GR::template NodeMap<V> {
892
      typedef typename GR::template NodeMap<V> Parent;
893

	
903 894
    public:
904 895

	
905
      typedef typename GR::template NodeMap<V> Parent;
906

	
907 896
      explicit NodeMap(const SmartArcSetBase<GR>& arcset)
908 897
        : Parent(*arcset._graph) { }
909 898

	
910 899
      NodeMap(const SmartArcSetBase<GR>& arcset, const V& value)
911 900
        : Parent(*arcset._graph, value) { }
912 901

	
913 902
      NodeMap& operator=(const NodeMap& cmap) {
914 903
        return operator=<NodeMap>(cmap);
915 904
      }
916 905

	
917 906
      template <typename CMap>
918 907
      NodeMap& operator=(const CMap& cmap) {
919 908
        Parent::operator=(cmap);
920 909
        return *this;
921 910
      }
922 911
    };
923 912

	
924 913
  };
925 914

	
926 915

	
927 916
  /// \ingroup semi_adaptors
928 917
  ///
929 918
  /// \brief Digraph using a node set of another digraph or graph and
930 919
  /// an own arc set.
931 920
  ///
932 921
  /// This structure can be used to establish another directed graph
933 922
  /// over a node set of an existing one. This class uses the same
934 923
  /// Node type as the underlying graph, and each valid node of the
935 924
  /// original graph is valid in this arc set, therefore the node
936 925
  /// objects of the original graph can be used directly with this
937 926
  /// class. The node handling functions (id handling, observing, and
938 927
  /// iterators) works equivalently as in the original graph.
939 928
  ///
940 929
  /// \param GR The type of the graph which shares its node set with
941 930
  /// this class. Its interface must conform to the
942 931
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
943 932
  /// concept.
944 933
  ///
945 934
  /// This implementation is slightly faster than the \c ListArcSet,
946 935
  /// because it uses continuous storage for arcs and it uses just
947 936
  /// single-linked lists for enumerate outgoing and incoming
948 937
  /// arcs. Therefore the arcs cannot be erased from the arc sets.
949 938
  ///
950 939
  /// \warning If a node is erased from the underlying graph and this
951 940
  /// node is the source or target of one arc in the arc set, then
952 941
  /// the arc set is invalidated, and it cannot be used anymore. The
953 942
  /// validity can be checked with the \c valid() member function.
954 943
  ///
955 944
  /// This class fully conforms to the \ref concepts::Digraph
956 945
  /// "Digraph" concept.
957 946
  template <typename GR>
958 947
  class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > {
948
    typedef ArcSetExtender<SmartArcSetBase<GR> > Parent;
959 949

	
960 950
  public:
961 951

	
962
    typedef ArcSetExtender<SmartArcSetBase<GR> > Parent;
963

	
964 952
    typedef typename Parent::Node Node;
965 953
    typedef typename Parent::Arc Arc;
966 954

	
967
    typedef GR Graph;
968

	
969 955
  protected:
970 956

	
971 957
    typedef typename Parent::NodesImplBase NodesImplBase;
972 958

	
973 959
    void eraseNode(const Node& node) {
974 960
      if (typename Parent::InArcIt(*this, node) == INVALID &&
975 961
          typename Parent::OutArcIt(*this, node) == INVALID) {
976 962
        return;
977 963
      }
978 964
      throw typename NodesImplBase::Notifier::ImmediateDetach();
979 965
    }
980 966

	
981 967
    void clearNodes() {
982 968
      Parent::clear();
983 969
    }
984 970

	
985 971
    class NodesImpl : public NodesImplBase {
986
    public:
987 972
      typedef NodesImplBase Parent;
988 973

	
974
    public:
989 975
      NodesImpl(const GR& graph, SmartArcSet& arcset)
990 976
        : Parent(graph), _arcset(arcset) {}
991 977

	
992 978
      virtual ~NodesImpl() {}
993 979

	
994 980
      bool attached() const {
995 981
        return Parent::attached();
996 982
      }
997 983

	
998 984
    protected:
999 985

	
1000 986
      virtual void erase(const Node& node) {
1001 987
        try {
1002 988
          _arcset.eraseNode(node);
1003 989
          Parent::erase(node);
1004 990
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1005 991
          Parent::clear();
1006 992
          throw;
1007 993
        }
1008 994
      }
1009 995
      virtual void erase(const std::vector<Node>& nodes) {
1010 996
        try {
1011 997
          for (int i = 0; i < int(nodes.size()); ++i) {
1012 998
            _arcset.eraseNode(nodes[i]);
1013 999
          }
1014 1000
          Parent::erase(nodes);
1015 1001
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1016 1002
          Parent::clear();
1017 1003
          throw;
1018 1004
        }
1019 1005
      }
1020 1006
      virtual void clear() {
1021 1007
        _arcset.clearNodes();
1022 1008
        Parent::clear();
1023 1009
      }
1024 1010

	
1025 1011
    private:
1026 1012
      SmartArcSet& _arcset;
1027 1013
    };
1028 1014

	
1029 1015
    NodesImpl _nodes;
1030 1016

	
1031 1017
  public:
1032 1018

	
1033 1019
    /// \brief Constructor of the ArcSet.
1034 1020
    ///
1035 1021
    /// Constructor of the ArcSet.
1036 1022
    SmartArcSet(const GR& graph) : _nodes(graph, *this) {
1037 1023
      Parent::initalize(graph, _nodes);
1038 1024
    }
1039 1025

	
1040 1026
    /// \brief Add a new arc to the digraph.
1041 1027
    ///
1042 1028
    /// Add a new arc to the digraph with source node \c s
1043 1029
    /// and target node \c t.
1044 1030
    /// \return The new arc.
1045 1031
    Arc addArc(const Node& s, const Node& t) {
1046 1032
      return Parent::addArc(s, t);
1047 1033
    }
1048 1034

	
1049 1035
    /// \brief Validity check
1050 1036
    ///
1051 1037
    /// This functions gives back false if the ArcSet is
1052 1038
    /// invalidated. It occurs when a node in the underlying graph is
1053 1039
    /// erased and it is not isolated in the ArcSet.
1054 1040
    bool valid() const {
1055 1041
      return _nodes.attached();
1056 1042
    }
1057 1043

	
1058 1044
  };
1059 1045

	
1060 1046

	
1061 1047
  template <typename GR>
1062 1048
  class SmartEdgeSetBase {
1063 1049
  public:
1064 1050

	
1065
    typedef GR Graph;
1066 1051
    typedef typename GR::Node Node;
1067 1052
    typedef typename GR::NodeIt NodeIt;
1068 1053

	
1069 1054
  protected:
1070 1055

	
1071 1056
    struct NodeT {
1072 1057
      int first_out;
1073 1058
      NodeT() : first_out(-1) {}
1074 1059
    };
1075 1060

	
1076 1061
    typedef typename ItemSetTraits<GR, Node>::
1077 1062
    template Map<NodeT>::Type NodesImplBase;
1078 1063

	
1079 1064
    NodesImplBase* _nodes;
1080 1065

	
1081 1066
    struct ArcT {
1082 1067
      Node target;
1083 1068
      int next_out;
1084 1069
      ArcT() {}
1085 1070
    };
1086 1071

	
1087 1072
    std::vector<ArcT> arcs;
1088 1073

	
1089 1074
    const GR* _graph;
1090 1075

	
1091 1076
    void initalize(const GR& graph, NodesImplBase& nodes) {
1092 1077
      _graph = &graph;
1093 1078
      _nodes = &nodes;
1094 1079
    }
1095 1080

	
1096 1081
  public:
1097 1082

	
1098 1083
    class Edge {
1099 1084
      friend class SmartEdgeSetBase;
1100 1085
    protected:
1101 1086

	
1102 1087
      int id;
1103 1088
      explicit Edge(int _id) { id = _id;}
1104 1089

	
1105 1090
    public:
1106 1091
      Edge() {}
1107 1092
      Edge (Invalid) { id = -1; }
1108 1093
      bool operator==(const Edge& arc) const {return id == arc.id;}
1109 1094
      bool operator!=(const Edge& arc) const {return id != arc.id;}
1110 1095
      bool operator<(const Edge& arc) const {return id < arc.id;}
1111 1096
    };
1112 1097

	
1113 1098
    class Arc {
1114 1099
      friend class SmartEdgeSetBase;
1115 1100
    protected:
1116 1101
      Arc(int _id) : id(_id) {}
1117 1102
      int id;
1118 1103
    public:
1119 1104
      operator Edge() const { return edgeFromId(id / 2); }
1120 1105

	
1121 1106
      Arc() {}
1122 1107
      Arc(Invalid) : id(-1) {}
1123 1108
      bool operator==(const Arc& arc) const { return id == arc.id; }
1124 1109
      bool operator!=(const Arc& arc) const { return id != arc.id; }
1125 1110
      bool operator<(const Arc& arc) const { return id < arc.id; }
1126 1111
    };
1127 1112

	
1128 1113
    SmartEdgeSetBase() {}
1129 1114

	
1130 1115
    Edge addEdge(const Node& u, const Node& v) {
1131 1116
      int n = arcs.size();
1132 1117
      arcs.push_back(ArcT());
1133 1118
      arcs.push_back(ArcT());
1134 1119

	
1135 1120
      arcs[n].target = u;
1136 1121
      arcs[n | 1].target = v;
1137 1122

	
1138 1123
      arcs[n].next_out = (*_nodes)[v].first_out;
1139 1124
      (*_nodes)[v].first_out = n;
1140 1125

	
1141 1126
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
1142 1127
      (*_nodes)[u].first_out = (n | 1);
1143 1128

	
1144 1129
      return Edge(n / 2);
1145 1130
    }
1146 1131

	
1147 1132
    void clear() {
1148 1133
      Node node;
1149 1134
      for (first(node); node != INVALID; next(node)) {
1150 1135
        (*_nodes)[node].first_out = -1;
1151 1136
      }
1152 1137
      arcs.clear();
1153 1138
    }
1154 1139

	
1155 1140
    void first(Node& node) const {
1156 1141
      _graph->first(node);
1157 1142
    }
1158 1143

	
1159 1144
    void next(Node& node) const {
1160 1145
      _graph->next(node);
1161 1146
    }
1162 1147

	
1163 1148
    void first(Arc& arc) const {
1164 1149
      arc.id = arcs.size() - 1;
1165 1150
    }
1166 1151

	
1167 1152
    void next(Arc& arc) const {
1168 1153
      --arc.id;
1169 1154
    }
1170 1155

	
1171 1156
    void first(Edge& arc) const {
1172 1157
      arc.id = arcs.size() / 2 - 1;
1173 1158
    }
1174 1159

	
1175 1160
    void next(Edge& arc) const {
1176 1161
      --arc.id;
1177 1162
    }
1178 1163

	
1179 1164
    void firstOut(Arc& arc, const Node& node) const {
1180 1165
      arc.id = (*_nodes)[node].first_out;
1181 1166
    }
1182 1167

	
1183 1168
    void nextOut(Arc& arc) const {
1184 1169
      arc.id = arcs[arc.id].next_out;
1185 1170
    }
1186 1171

	
1187 1172
    void firstIn(Arc& arc, const Node& node) const {
1188 1173
      arc.id = (((*_nodes)[node].first_out) ^ 1);
1189 1174
      if (arc.id == -2) arc.id = -1;
1190 1175
    }
1191 1176

	
1192 1177
    void nextIn(Arc& arc) const {
1193 1178
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
1194 1179
      if (arc.id == -2) arc.id = -1;
1195 1180
    }
1196 1181

	
1197 1182
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
1198 1183
      int de = (*_nodes)[node].first_out;
1199 1184
      if (de != -1 ) {
1200 1185
        arc.id = de / 2;
1201 1186
        dir = ((de & 1) == 1);
1202 1187
      } else {
1203 1188
        arc.id = -1;
1204 1189
        dir = true;
1205 1190
      }
1206 1191
    }
1207 1192
    void nextInc(Edge &arc, bool& dir) const {
1208 1193
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
1209 1194
      if (de != -1 ) {
1210 1195
        arc.id = de / 2;
1211 1196
        dir = ((de & 1) == 1);
1212 1197
      } else {
1213 1198
        arc.id = -1;
1214 1199
        dir = true;
1215 1200
      }
1216 1201
    }
1217 1202

	
1218 1203
    static bool direction(Arc arc) {
1219 1204
      return (arc.id & 1) == 1;
1220 1205
    }
1221 1206

	
1222 1207
    static Arc direct(Edge edge, bool dir) {
1223 1208
      return Arc(edge.id * 2 + (dir ? 1 : 0));
1224 1209
    }
1225 1210

	
1226 1211
    int id(Node node) const { return _graph->id(node); }
1227 1212
    static int id(Arc arc) { return arc.id; }
1228 1213
    static int id(Edge arc) { return arc.id; }
1229 1214

	
1230 1215
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
1231 1216
    static Arc arcFromId(int id) { return Arc(id); }
1232 1217
    static Edge edgeFromId(int id) { return Edge(id);}
1233 1218

	
1234 1219
    int maxNodeId() const { return _graph->maxNodeId(); };
1235 1220
    int maxArcId() const { return arcs.size() - 1; }
1236 1221
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
1237 1222

	
1238 1223
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
1239 1224
    Node target(Arc e) const { return arcs[e.id].target; }
1240 1225

	
1241 1226
    Node u(Edge e) const { return arcs[2 * e.id].target; }
1242 1227
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
1243 1228

	
1244 1229
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
1245 1230

	
1246 1231
    NodeNotifier& notifier(Node) const {
1247 1232
      return _graph->notifier(Node());
1248 1233
    }
1249 1234

	
1250 1235
    template <typename V>
1251 1236
    class NodeMap : public GR::template NodeMap<V> {
1237
      typedef typename GR::template NodeMap<V> Parent;
1238

	
1252 1239
    public:
1253 1240

	
1254
      typedef typename GR::template NodeMap<V> Parent;
1255

	
1256 1241
      explicit NodeMap(const SmartEdgeSetBase<GR>& arcset)
1257 1242
        : Parent(*arcset._graph) { }
1258 1243

	
1259 1244
      NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value)
1260 1245
        : Parent(*arcset._graph, value) { }
1261 1246

	
1262 1247
      NodeMap& operator=(const NodeMap& cmap) {
1263 1248
        return operator=<NodeMap>(cmap);
1264 1249
      }
1265 1250

	
1266 1251
      template <typename CMap>
1267 1252
      NodeMap& operator=(const CMap& cmap) {
1268 1253
        Parent::operator=(cmap);
1269 1254
        return *this;
1270 1255
      }
1271 1256
    };
1272 1257

	
1273 1258
  };
1274 1259

	
1275 1260
  /// \ingroup semi_adaptors
1276 1261
  ///
1277 1262
  /// \brief Graph using a node set of another digraph or graph and an
1278 1263
  /// own edge set.
1279 1264
  ///
1280 1265
  /// This structure can be used to establish another graph over a
1281 1266
  /// node set of an existing one. This class uses the same Node type
1282 1267
  /// as the underlying graph, and each valid node of the original
1283 1268
  /// graph is valid in this arc set, therefore the node objects of
1284 1269
  /// the original graph can be used directly with this class. The
1285 1270
  /// node handling functions (id handling, observing, and iterators)
1286 1271
  /// works equivalently as in the original graph.
1287 1272
  ///
1288 1273
  /// \param GR The type of the graph which shares its node set
1289 1274
  /// with this class. Its interface must conform to the
1290 1275
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
1291 1276
  ///  concept.
1292 1277
  ///
1293 1278
  /// This implementation is slightly faster than the \c ListEdgeSet,
1294 1279
  /// because it uses continuous storage for edges and it uses just
1295 1280
  /// single-linked lists for enumerate incident edges. Therefore the
1296 1281
  /// edges cannot be erased from the edge sets.
1297 1282
  ///
1298 1283
  /// \warning If a node is erased from the underlying graph and this
1299 1284
  /// node is incident to one edge in the edge set, then the edge set
1300 1285
  /// is invalidated, and it cannot be used anymore. The validity can
1301 1286
  /// be checked with the \c valid() member function.
1302 1287
  ///
1303 1288
  /// This class fully conforms to the \ref concepts::Graph
1304 1289
  /// "Graph" concept.
1305 1290
  template <typename GR>
1306 1291
  class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > {
1292
    typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent;
1307 1293

	
1308 1294
  public:
1309 1295

	
1310
    typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent;
1311

	
1312 1296
    typedef typename Parent::Node Node;
1313 1297
    typedef typename Parent::Arc Arc;
1314 1298
    typedef typename Parent::Edge Edge;
1315 1299

	
1316
    typedef GR Graph;
1317

	
1318 1300
  protected:
1319 1301

	
1320 1302
    typedef typename Parent::NodesImplBase NodesImplBase;
1321 1303

	
1322 1304
    void eraseNode(const Node& node) {
1323 1305
      if (typename Parent::IncEdgeIt(*this, node) == INVALID) {
1324 1306
        return;
1325 1307
      }
1326 1308
      throw typename NodesImplBase::Notifier::ImmediateDetach();
1327 1309
    }
1328 1310

	
1329 1311
    void clearNodes() {
1330 1312
      Parent::clear();
1331 1313
    }
1332 1314

	
1333 1315
    class NodesImpl : public NodesImplBase {
1334
    public:
1335 1316
      typedef NodesImplBase Parent;
1336 1317

	
1318
    public:
1337 1319
      NodesImpl(const GR& graph, SmartEdgeSet& arcset)
1338 1320
        : Parent(graph), _arcset(arcset) {}
1339 1321

	
1340 1322
      virtual ~NodesImpl() {}
1341 1323

	
1342 1324
      bool attached() const {
1343 1325
        return Parent::attached();
1344 1326
      }
1345 1327

	
1346 1328
    protected:
1347 1329

	
1348 1330
      virtual void erase(const Node& node) {
1349 1331
        try {
1350 1332
          _arcset.eraseNode(node);
1351 1333
          Parent::erase(node);
1352 1334
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1353 1335
          Parent::clear();
1354 1336
          throw;
1355 1337
        }
1356 1338
      }
1357 1339
      virtual void erase(const std::vector<Node>& nodes) {
1358 1340
        try {
1359 1341
          for (int i = 0; i < int(nodes.size()); ++i) {
1360 1342
            _arcset.eraseNode(nodes[i]);
1361 1343
          }
1362 1344
          Parent::erase(nodes);
1363 1345
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1364 1346
          Parent::clear();
1365 1347
          throw;
1366 1348
        }
1367 1349
      }
1368 1350
      virtual void clear() {
1369 1351
        _arcset.clearNodes();
1370 1352
        Parent::clear();
1371 1353
      }
1372 1354

	
1373 1355
    private:
1374 1356
      SmartEdgeSet& _arcset;
1375 1357
    };
1376 1358

	
1377 1359
    NodesImpl _nodes;
1378 1360

	
1379 1361
  public:
1380 1362

	
1381 1363
    /// \brief Constructor of the EdgeSet.
1382 1364
    ///
1383 1365
    /// Constructor of the EdgeSet.
1384 1366
    SmartEdgeSet(const GR& graph) : _nodes(graph, *this) {
1385 1367
      Parent::initalize(graph, _nodes);
1386 1368
    }
1387 1369

	
1388 1370
    /// \brief Add a new edge to the graph.
1389 1371
    ///
1390 1372
    /// Add a new edge to the graph with node \c u
1391 1373
    /// and node \c v endpoints.
1392 1374
    /// \return The new edge.
1393 1375
    Edge addEdge(const Node& u, const Node& v) {
1394 1376
      return Parent::addEdge(u, v);
1395 1377
    }
1396 1378

	
1397 1379
    /// \brief Validity check
1398 1380
    ///
1399 1381
    /// This functions gives back false if the EdgeSet is
1400 1382
    /// invalidated. It occurs when a node in the underlying graph is
1401 1383
    /// erased and it is not isolated in the EdgeSet.
1402 1384
    bool valid() const {
1403 1385
      return _nodes.attached();
1404 1386
    }
1405 1387

	
1406 1388
  };
1407 1389

	
1408 1390
}
1409 1391

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

	
19 19
#ifndef LEMON_FULL_GRAPH_H
20 20
#define LEMON_FULL_GRAPH_H
21 21

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

	
25 25
///\ingroup graphs
26 26
///\file
27 27
///\brief FullGraph and FullDigraph classes.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  class FullDigraphBase {
32 32
  public:
33 33

	
34
    typedef FullDigraphBase Graph;
34
    typedef FullDigraphBase Digraph;
35 35

	
36 36
    class Node;
37 37
    class Arc;
38 38

	
39 39
  protected:
40 40

	
41 41
    int _node_num;
42 42
    int _arc_num;
43 43

	
44 44
    FullDigraphBase() {}
45 45

	
46 46
    void construct(int n) { _node_num = n; _arc_num = n * n; }
47 47

	
48 48
  public:
49 49

	
50 50
    typedef True NodeNumTag;
51 51
    typedef True ArcNumTag;
52 52

	
53 53
    Node operator()(int ix) const { return Node(ix); }
54 54
    int index(const Node& node) const { return node._id; }
55 55

	
56 56
    Arc arc(const Node& s, const Node& t) const {
57 57
      return Arc(s._id * _node_num + t._id);
58 58
    }
59 59

	
60 60
    int nodeNum() const { return _node_num; }
61 61
    int arcNum() const { return _arc_num; }
62 62

	
63 63
    int maxNodeId() const { return _node_num - 1; }
64 64
    int maxArcId() const { return _arc_num - 1; }
65 65

	
66 66
    Node source(Arc arc) const { return arc._id / _node_num; }
67 67
    Node target(Arc arc) const { return arc._id % _node_num; }
68 68

	
69 69
    static int id(Node node) { return node._id; }
70 70
    static int id(Arc arc) { return arc._id; }
71 71

	
72 72
    static Node nodeFromId(int id) { return Node(id);}
73 73
    static Arc arcFromId(int id) { return Arc(id);}
74 74

	
75 75
    typedef True FindArcTag;
76 76

	
77 77
    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
78 78
      return prev == INVALID ? arc(s, t) : INVALID;
79 79
    }
80 80

	
81 81
    class Node {
82 82
      friend class FullDigraphBase;
83 83

	
84 84
    protected:
85 85
      int _id;
86 86
      Node(int id) : _id(id) {}
87 87
    public:
88 88
      Node() {}
89 89
      Node (Invalid) : _id(-1) {}
90 90
      bool operator==(const Node node) const {return _id == node._id;}
91 91
      bool operator!=(const Node node) const {return _id != node._id;}
92 92
      bool operator<(const Node node) const {return _id < node._id;}
93 93
    };
94 94

	
95 95
    class Arc {
96 96
      friend class FullDigraphBase;
97 97

	
98 98
    protected:
99 99
      int _id;  // _node_num * source + target;
100 100

	
101 101
      Arc(int id) : _id(id) {}
102 102

	
103 103
    public:
104 104
      Arc() { }
105 105
      Arc (Invalid) { _id = -1; }
106 106
      bool operator==(const Arc arc) const {return _id == arc._id;}
107 107
      bool operator!=(const Arc arc) const {return _id != arc._id;}
108 108
      bool operator<(const Arc arc) const {return _id < arc._id;}
109 109
    };
110 110

	
111 111
    void first(Node& node) const {
112 112
      node._id = _node_num - 1;
113 113
    }
114 114

	
115 115
    static void next(Node& node) {
116 116
      --node._id;
117 117
    }
118 118

	
119 119
    void first(Arc& arc) const {
120 120
      arc._id = _arc_num - 1;
121 121
    }
122 122

	
123 123
    static void next(Arc& arc) {
124 124
      --arc._id;
125 125
    }
126 126

	
127 127
    void firstOut(Arc& arc, const Node& node) const {
128 128
      arc._id = (node._id + 1) * _node_num - 1;
129 129
    }
130 130

	
131 131
    void nextOut(Arc& arc) const {
132 132
      if (arc._id % _node_num == 0) arc._id = 0;
133 133
      --arc._id;
134 134
    }
135 135

	
136 136
    void firstIn(Arc& arc, const Node& node) const {
137 137
      arc._id = _arc_num + node._id - _node_num;
138 138
    }
139 139

	
140 140
    void nextIn(Arc& arc) const {
141 141
      arc._id -= _node_num;
142 142
      if (arc._id < 0) arc._id = -1;
143 143
    }
144 144

	
145 145
  };
146 146

	
147 147
  typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase;
148 148

	
149 149
  /// \ingroup graphs
150 150
  ///
151 151
  /// \brief A full digraph class.
152 152
  ///
153 153
  /// This is a simple and fast directed full graph implementation.
154 154
  /// From each node go arcs to each node (including the source node),
155 155
  /// therefore the number of the arcs in the digraph is the square of
156 156
  /// the node number. This digraph type is completely static, so you
157 157
  /// can neither add nor delete either arcs or nodes, and it needs
158 158
  /// constant space in memory.
159 159
  ///
160 160
  /// This class fully conforms to the \ref concepts::Digraph
161 161
  /// "Digraph concept".
162 162
  ///
163 163
  /// The \c FullDigraph and \c FullGraph classes are very similar,
164 164
  /// but there are two differences. While this class conforms only
165 165
  /// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph
166 166
  /// class conforms to the \ref concepts::Graph "Graph" concept,
167 167
  /// moreover \c FullGraph does not contain a loop arc for each
168 168
  /// node as \c FullDigraph does.
169 169
  ///
170 170
  /// \sa FullGraph
171 171
  class FullDigraph : public ExtendedFullDigraphBase {
172
    typedef ExtendedFullDigraphBase Parent;
173

	
172 174
  public:
173 175

	
174
    typedef ExtendedFullDigraphBase Parent;
175

	
176 176
    /// \brief Constructor
177 177
    FullDigraph() { construct(0); }
178 178

	
179 179
    /// \brief Constructor
180 180
    ///
181 181
    /// Constructor.
182 182
    /// \param n The number of the nodes.
183 183
    FullDigraph(int n) { construct(n); }
184 184

	
185 185
    /// \brief Resizes the digraph
186 186
    ///
187 187
    /// Resizes the digraph. The function will fully destroy and
188 188
    /// rebuild the digraph. This cause that the maps of the digraph will
189 189
    /// reallocated automatically and the previous values will be lost.
190 190
    void resize(int n) {
191 191
      Parent::notifier(Arc()).clear();
192 192
      Parent::notifier(Node()).clear();
193 193
      construct(n);
194 194
      Parent::notifier(Node()).build();
195 195
      Parent::notifier(Arc()).build();
196 196
    }
197 197

	
198 198
    /// \brief Returns the node with the given index.
199 199
    ///
200 200
    /// Returns the node with the given index. Since it is a static
201 201
    /// digraph its nodes can be indexed with integers from the range
202 202
    /// <tt>[0..nodeNum()-1]</tt>.
203 203
    /// \sa index()
204 204
    Node operator()(int ix) const { return Parent::operator()(ix); }
205 205

	
206 206
    /// \brief Returns the index of the given node.
207 207
    ///
208 208
    /// Returns the index of the given node. Since it is a static
209 209
    /// digraph its nodes can be indexed with integers from the range
210 210
    /// <tt>[0..nodeNum()-1]</tt>.
211 211
    /// \sa operator()
212 212
    int index(const Node& node) const { return Parent::index(node); }
213 213

	
214 214
    /// \brief Returns the arc connecting the given nodes.
215 215
    ///
216 216
    /// Returns the arc connecting the given nodes.
217 217
    Arc arc(const Node& u, const Node& v) const {
218 218
      return Parent::arc(u, v);
219 219
    }
220 220

	
221 221
    /// \brief Number of nodes.
222 222
    int nodeNum() const { return Parent::nodeNum(); }
223 223
    /// \brief Number of arcs.
224 224
    int arcNum() const { return Parent::arcNum(); }
225 225
  };
226 226

	
227 227

	
228 228
  class FullGraphBase {
229
    int _node_num;
230
    int _edge_num;
231 229
  public:
232 230

	
233 231
    typedef FullGraphBase Graph;
234 232

	
235 233
    class Node;
236 234
    class Arc;
237 235
    class Edge;
238 236

	
239 237
  protected:
240 238

	
239
    int _node_num;
240
    int _edge_num;
241

	
241 242
    FullGraphBase() {}
242 243

	
243 244
    void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
244 245

	
245 246
    int _uid(int e) const {
246 247
      int u = e / _node_num;
247 248
      int v = e % _node_num;
248 249
      return u < v ? u : _node_num - 2 - u;
249 250
    }
250 251

	
251 252
    int _vid(int e) const {
252 253
      int u = e / _node_num;
253 254
      int v = e % _node_num;
254 255
      return u < v ? v : _node_num - 1 - v;
255 256
    }
256 257

	
257 258
    void _uvid(int e, int& u, int& v) const {
258 259
      u = e / _node_num;
259 260
      v = e % _node_num;
260 261
      if  (u >= v) {
261 262
        u = _node_num - 2 - u;
262 263
        v = _node_num - 1 - v;
263 264
      }
264 265
    }
265 266

	
266 267
    void _stid(int a, int& s, int& t) const {
267 268
      if ((a & 1) == 1) {
268 269
        _uvid(a >> 1, s, t);
269 270
      } else {
270 271
        _uvid(a >> 1, t, s);
271 272
      }
272 273
    }
273 274

	
274 275
    int _eid(int u, int v) const {
275 276
      if (u < (_node_num - 1) / 2) {
276 277
        return u * _node_num + v;
277 278
      } else {
278 279
        return (_node_num - 1 - u) * _node_num - v - 1;
279 280
      }
280 281
    }
281 282

	
282 283
  public:
283 284

	
284 285
    Node operator()(int ix) const { return Node(ix); }
285 286
    int index(const Node& node) const { return node._id; }
286 287

	
287 288
    Edge edge(const Node& u, const Node& v) const {
288 289
      if (u._id < v._id) {
289 290
        return Edge(_eid(u._id, v._id));
290 291
      } else if (u._id != v._id) {
291 292
        return Edge(_eid(v._id, u._id));
292 293
      } else {
293 294
        return INVALID;
294 295
      }
295 296
    }
296 297

	
297 298
    Arc arc(const Node& s, const Node& t) const {
298 299
      if (s._id < t._id) {
299 300
        return Arc((_eid(s._id, t._id) << 1) | 1);
300 301
      } else if (s._id != t._id) {
301 302
        return Arc(_eid(t._id, s._id) << 1);
302 303
      } else {
303 304
        return INVALID;
304 305
      }
305 306
    }
306 307

	
307 308
    typedef True NodeNumTag;
308 309
    typedef True ArcNumTag;
309 310
    typedef True EdgeNumTag;
310 311

	
311 312
    int nodeNum() const { return _node_num; }
312 313
    int arcNum() const { return 2 * _edge_num; }
313 314
    int edgeNum() const { return _edge_num; }
314 315

	
315 316
    static int id(Node node) { return node._id; }
316 317
    static int id(Arc arc) { return arc._id; }
317 318
    static int id(Edge edge) { return edge._id; }
318 319

	
319 320
    int maxNodeId() const { return _node_num-1; }
320 321
    int maxArcId() const { return 2 * _edge_num-1; }
321 322
    int maxEdgeId() const { return _edge_num-1; }
322 323

	
323 324
    static Node nodeFromId(int id) { return Node(id);}
324 325
    static Arc arcFromId(int id) { return Arc(id);}
325 326
    static Edge edgeFromId(int id) { return Edge(id);}
326 327

	
327 328
    Node u(Edge edge) const {
328 329
      return Node(_uid(edge._id));
329 330
    }
330 331

	
331 332
    Node v(Edge edge) const {
332 333
      return Node(_vid(edge._id));
333 334
    }
334 335

	
335 336
    Node source(Arc arc) const {
336 337
      return Node((arc._id & 1) == 1 ?
337 338
                  _uid(arc._id >> 1) : _vid(arc._id >> 1));
338 339
    }
339 340

	
340 341
    Node target(Arc arc) const {
341 342
      return Node((arc._id & 1) == 1 ?
342 343
                  _vid(arc._id >> 1) : _uid(arc._id >> 1));
343 344
    }
344 345

	
345 346
    typedef True FindEdgeTag;
346 347
    typedef True FindArcTag;
347 348

	
348 349
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
349 350
      return prev != INVALID ? INVALID : edge(u, v);
350 351
    }
351 352

	
352 353
    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
353 354
      return prev != INVALID ? INVALID : arc(s, t);
354 355
    }
355 356

	
356 357
    class Node {
357 358
      friend class FullGraphBase;
358 359

	
359 360
    protected:
360 361
      int _id;
361 362
      Node(int id) : _id(id) {}
362 363
    public:
363 364
      Node() {}
364 365
      Node (Invalid) { _id = -1; }
365 366
      bool operator==(const Node node) const {return _id == node._id;}
366 367
      bool operator!=(const Node node) const {return _id != node._id;}
367 368
      bool operator<(const Node node) const {return _id < node._id;}
368 369
    };
369 370

	
370 371
    class Edge {
371 372
      friend class FullGraphBase;
372 373
      friend class Arc;
373 374

	
374 375
    protected:
375 376
      int _id;
376 377

	
377 378
      Edge(int id) : _id(id) {}
378 379

	
379 380
    public:
380 381
      Edge() { }
381 382
      Edge (Invalid) { _id = -1; }
382 383

	
383 384
      bool operator==(const Edge edge) const {return _id == edge._id;}
384 385
      bool operator!=(const Edge edge) const {return _id != edge._id;}
385 386
      bool operator<(const Edge edge) const {return _id < edge._id;}
386 387
    };
387 388

	
388 389
    class Arc {
389 390
      friend class FullGraphBase;
390 391

	
391 392
    protected:
392 393
      int _id;
393 394

	
394 395
      Arc(int id) : _id(id) {}
395 396

	
396 397
    public:
397 398
      Arc() { }
398 399
      Arc (Invalid) { _id = -1; }
399 400

	
400 401
      operator Edge() const { return Edge(_id != -1 ? (_id >> 1) : -1); }
401 402

	
402 403
      bool operator==(const Arc arc) const {return _id == arc._id;}
403 404
      bool operator!=(const Arc arc) const {return _id != arc._id;}
404 405
      bool operator<(const Arc arc) const {return _id < arc._id;}
405 406
    };
406 407

	
407 408
    static bool direction(Arc arc) {
408 409
      return (arc._id & 1) == 1;
409 410
    }
410 411

	
411 412
    static Arc direct(Edge edge, bool dir) {
412 413
      return Arc((edge._id << 1) | (dir ? 1 : 0));
413 414
    }
414 415

	
415 416
    void first(Node& node) const {
416 417
      node._id = _node_num - 1;
417 418
    }
418 419

	
419 420
    static void next(Node& node) {
420 421
      --node._id;
421 422
    }
422 423

	
423 424
    void first(Arc& arc) const {
424 425
      arc._id = (_edge_num << 1) - 1;
425 426
    }
426 427

	
427 428
    static void next(Arc& arc) {
428 429
      --arc._id;
429 430
    }
430 431

	
431 432
    void first(Edge& edge) const {
432 433
      edge._id = _edge_num - 1;
433 434
    }
434 435

	
435 436
    static void next(Edge& edge) {
436 437
      --edge._id;
437 438
    }
438 439

	
439 440
    void firstOut(Arc& arc, const Node& node) const {
440 441
      int s = node._id, t = _node_num - 1;
441 442
      if (s < t) {
442 443
        arc._id = (_eid(s, t) << 1) | 1;
443 444
      } else {
444 445
        --t;
445 446
        arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
446 447
      }
447 448
    }
448 449

	
449 450
    void nextOut(Arc& arc) const {
450 451
      int s, t;
451 452
      _stid(arc._id, s, t);
452 453
      --t;
453 454
      if (s < t) {
454 455
        arc._id = (_eid(s, t) << 1) | 1;
455 456
      } else {
456 457
        if (s == t) --t;
457 458
        arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
458 459
      }
459 460
    }
460 461

	
461 462
    void firstIn(Arc& arc, const Node& node) const {
462 463
      int s = _node_num - 1, t = node._id;
463 464
      if (s > t) {
464 465
        arc._id = (_eid(t, s) << 1);
465 466
      } else {
466 467
        --s;
467 468
        arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
468 469
      }
469 470
    }
470 471

	
471 472
    void nextIn(Arc& arc) const {
472 473
      int s, t;
473 474
      _stid(arc._id, s, t);
474 475
      --s;
475 476
      if (s > t) {
476 477
        arc._id = (_eid(t, s) << 1);
477 478
      } else {
478 479
        if (s == t) --s;
479 480
        arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
480 481
      }
481 482
    }
482 483

	
483 484
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
484 485
      int u = node._id, v = _node_num - 1;
485 486
      if (u < v) {
486 487
        edge._id = _eid(u, v);
487 488
        dir = true;
488 489
      } else {
489 490
        --v;
490 491
        edge._id = (v != -1 ? _eid(v, u) : -1);
491 492
        dir = false;
492 493
      }
493 494
    }
494 495

	
495 496
    void nextInc(Edge& edge, bool& dir) const {
496 497
      int u, v;
497 498
      if (dir) {
498 499
        _uvid(edge._id, u, v);
499 500
        --v;
500 501
        if (u < v) {
501 502
          edge._id = _eid(u, v);
502 503
        } else {
503 504
          --v;
504 505
          edge._id = (v != -1 ? _eid(v, u) : -1);
505 506
          dir = false;
506 507
        }
507 508
      } else {
508 509
        _uvid(edge._id, v, u);
509 510
        --v;
510 511
        edge._id = (v != -1 ? _eid(v, u) : -1);
511 512
      }
512 513
    }
513 514

	
514 515
  };
515 516

	
516 517
  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
517 518

	
518 519
  /// \ingroup graphs
519 520
  ///
520 521
  /// \brief An undirected full graph class.
521 522
  ///
522 523
  /// This is a simple and fast undirected full graph
523 524
  /// implementation. From each node go edge to each other node,
524 525
  /// therefore the number of edges in the graph is \f$n(n-1)/2\f$.
525 526
  /// This graph type is completely static, so you can neither
526 527
  /// add nor delete either edges or nodes, and it needs constant
527 528
  /// space in memory.
528 529
  ///
529 530
  /// This class fully conforms to the \ref concepts::Graph "Graph concept".
530 531
  ///
531 532
  /// The \c FullGraph and \c FullDigraph classes are very similar,
532 533
  /// but there are two differences. While the \c FullDigraph class
533 534
  /// conforms only to the \ref concepts::Digraph "Digraph" concept,
534 535
  /// this class conforms to the \ref concepts::Graph "Graph" concept,
535 536
  /// moreover \c FullGraph does not contain a loop arc for each
536 537
  /// node as \c FullDigraph does.
537 538
  ///
538 539
  /// \sa FullDigraph
539 540
  class FullGraph : public ExtendedFullGraphBase {
541
    typedef ExtendedFullGraphBase Parent;
542

	
540 543
  public:
541 544

	
542
    typedef ExtendedFullGraphBase Parent;
543

	
544 545
    /// \brief Constructor
545 546
    FullGraph() { construct(0); }
546 547

	
547 548
    /// \brief Constructor
548 549
    ///
549 550
    /// Constructor.
550 551
    /// \param n The number of the nodes.
551 552
    FullGraph(int n) { construct(n); }
552 553

	
553 554
    /// \brief Resizes the graph
554 555
    ///
555 556
    /// Resizes the graph. The function will fully destroy and
556 557
    /// rebuild the graph. This cause that the maps of the graph will
557 558
    /// reallocated automatically and the previous values will be lost.
558 559
    void resize(int n) {
559 560
      Parent::notifier(Arc()).clear();
560 561
      Parent::notifier(Edge()).clear();
561 562
      Parent::notifier(Node()).clear();
562 563
      construct(n);
563 564
      Parent::notifier(Node()).build();
564 565
      Parent::notifier(Edge()).build();
565 566
      Parent::notifier(Arc()).build();
566 567
    }
567 568

	
568 569
    /// \brief Returns the node with the given index.
569 570
    ///
570 571
    /// Returns the node with the given index. Since it is a static
571 572
    /// graph its nodes can be indexed with integers from the range
572 573
    /// <tt>[0..nodeNum()-1]</tt>.
573 574
    /// \sa index()
574 575
    Node operator()(int ix) const { return Parent::operator()(ix); }
575 576

	
576 577
    /// \brief Returns the index of the given node.
577 578
    ///
578 579
    /// Returns the index of the given node. Since it is a static
579 580
    /// graph its nodes can be indexed with integers from the range
580 581
    /// <tt>[0..nodeNum()-1]</tt>.
581 582
    /// \sa operator()
582 583
    int index(const Node& node) const { return Parent::index(node); }
583 584

	
584 585
    /// \brief Returns the arc connecting the given nodes.
585 586
    ///
586 587
    /// Returns the arc connecting the given nodes.
587 588
    Arc arc(const Node& s, const Node& t) const {
588 589
      return Parent::arc(s, t);
589 590
    }
590 591

	
591 592
    /// \brief Returns the edge connects the given nodes.
592 593
    ///
593 594
    /// Returns the edge connects the given nodes.
594 595
    Edge edge(const Node& u, const Node& v) const {
595 596
      return Parent::edge(u, v);
596 597
    }
597 598

	
598 599
    /// \brief Number of nodes.
599 600
    int nodeNum() const { return Parent::nodeNum(); }
600 601
    /// \brief Number of arcs.
601 602
    int arcNum() const { return Parent::arcNum(); }
602 603
    /// \brief Number of edges.
603 604
    int edgeNum() const { return Parent::edgeNum(); }
604 605

	
605 606
  };
606 607

	
607 608

	
608 609
} //namespace lemon
609 610

	
610 611

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

	
19 19
#ifndef LEMON_GRAPH_TO_EPS_H
20 20
#define LEMON_GRAPH_TO_EPS_H
21 21

	
22 22
#include<iostream>
23 23
#include<fstream>
24 24
#include<sstream>
25 25
#include<algorithm>
26 26
#include<vector>
27 27

	
28 28
#ifndef WIN32
29 29
#include<sys/time.h>
30 30
#include<ctime>
31 31
#else
32 32
#include<lemon/bits/windows.h>
33 33
#endif
34 34

	
35 35
#include<lemon/math.h>
36 36
#include<lemon/core.h>
37 37
#include<lemon/dim2.h>
38 38
#include<lemon/maps.h>
39 39
#include<lemon/color.h>
40 40
#include<lemon/bits/bezier.h>
41 41
#include<lemon/error.h>
42 42

	
43 43

	
44 44
///\ingroup eps_io
45 45
///\file
46 46
///\brief A well configurable tool for visualizing graphs
47 47

	
48 48
namespace lemon {
49 49

	
50 50
  namespace _graph_to_eps_bits {
51 51
    template<class MT>
52 52
    class _NegY {
53 53
    public:
54 54
      typedef typename MT::Key Key;
55 55
      typedef typename MT::Value Value;
56 56
      const MT &map;
57 57
      int yscale;
58 58
      _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
59 59
      Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
60 60
    };
61 61
  }
62 62

	
63 63
///Default traits class of GraphToEps
64 64

	
65 65
///Default traits class of \ref GraphToEps.
66 66
///
67 67
///\param GR is the type of the underlying graph.
68 68
template<class GR>
69 69
struct DefaultGraphToEpsTraits
70 70
{
71 71
  typedef GR Graph;
72
  typedef GR Digraph;
72 73
  typedef typename Graph::Node Node;
73 74
  typedef typename Graph::NodeIt NodeIt;
74 75
  typedef typename Graph::Arc Arc;
75 76
  typedef typename Graph::ArcIt ArcIt;
76 77
  typedef typename Graph::InArcIt InArcIt;
77 78
  typedef typename Graph::OutArcIt OutArcIt;
78 79

	
79 80

	
80 81
  const Graph &g;
81 82

	
82 83
  std::ostream& os;
83 84

	
84 85
  typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
85 86
  CoordsMapType _coords;
86 87
  ConstMap<typename Graph::Node,double > _nodeSizes;
87 88
  ConstMap<typename Graph::Node,int > _nodeShapes;
88 89

	
89 90
  ConstMap<typename Graph::Node,Color > _nodeColors;
90 91
  ConstMap<typename Graph::Arc,Color > _arcColors;
91 92

	
92 93
  ConstMap<typename Graph::Arc,double > _arcWidths;
93 94

	
94 95
  double _arcWidthScale;
95 96

	
96 97
  double _nodeScale;
97 98
  double _xBorder, _yBorder;
98 99
  double _scale;
99 100
  double _nodeBorderQuotient;
100 101

	
101 102
  bool _drawArrows;
102 103
  double _arrowLength, _arrowWidth;
103 104

	
104 105
  bool _showNodes, _showArcs;
105 106

	
106 107
  bool _enableParallel;
107 108
  double _parArcDist;
108 109

	
109 110
  bool _showNodeText;
110 111
  ConstMap<typename Graph::Node,bool > _nodeTexts;
111 112
  double _nodeTextSize;
112 113

	
113 114
  bool _showNodePsText;
114 115
  ConstMap<typename Graph::Node,bool > _nodePsTexts;
115 116
  char *_nodePsTextsPreamble;
116 117

	
117 118
  bool _undirected;
118 119

	
119 120
  bool _pleaseRemoveOsStream;
120 121

	
121 122
  bool _scaleToA4;
122 123

	
123 124
  std::string _title;
124 125
  std::string _copyright;
125 126

	
126 127
  enum NodeTextColorType
127 128
    { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
128 129
  ConstMap<typename Graph::Node,Color > _nodeTextColors;
129 130

	
130 131
  bool _autoNodeScale;
131 132
  bool _autoArcWidthScale;
132 133

	
133 134
  bool _absoluteNodeSizes;
134 135
  bool _absoluteArcWidths;
135 136

	
136 137
  bool _negY;
137 138

	
138 139
  bool _preScale;
139 140
  ///Constructor
140 141

	
141 142
  ///Constructor
142 143
  ///\param gr  Reference to the graph to be printed.
143 144
  ///\param ost Reference to the output stream.
144 145
  ///By default it is <tt>std::cout</tt>.
145 146
  ///\param pros If it is \c true, then the \c ostream referenced by \c os
146 147
  ///will be explicitly deallocated by the destructor.
147 148
  DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout,
148 149
                          bool pros = false) :
149 150
    g(gr), os(ost),
150 151
    _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
151 152
    _nodeColors(WHITE), _arcColors(BLACK),
152 153
    _arcWidths(1.0), _arcWidthScale(0.003),
153 154
    _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
154 155
    _nodeBorderQuotient(.1),
155 156
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
156 157
    _showNodes(true), _showArcs(true),
157 158
    _enableParallel(false), _parArcDist(1),
158 159
    _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
159 160
    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
160 161
    _undirected(lemon::UndirectedTagIndicator<GR>::value),
161 162
    _pleaseRemoveOsStream(pros), _scaleToA4(false),
162 163
    _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
163 164
    _autoNodeScale(false),
164 165
    _autoArcWidthScale(false),
165 166
    _absoluteNodeSizes(false),
166 167
    _absoluteArcWidths(false),
167 168
    _negY(false),
168 169
    _preScale(true)
169 170
  {}
170 171
};
171 172

	
172 173
///Auxiliary class to implement the named parameters of \ref graphToEps()
173 174

	
174 175
///Auxiliary class to implement the named parameters of \ref graphToEps().
175 176
///
176 177
///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
177 178
template<class T> class GraphToEps : public T
178 179
{
179 180
  // Can't believe it is required by the C++ standard
180 181
  using T::g;
181 182
  using T::os;
182 183

	
183 184
  using T::_coords;
184 185
  using T::_nodeSizes;
185 186
  using T::_nodeShapes;
186 187
  using T::_nodeColors;
187 188
  using T::_arcColors;
188 189
  using T::_arcWidths;
189 190

	
190 191
  using T::_arcWidthScale;
191 192
  using T::_nodeScale;
192 193
  using T::_xBorder;
193 194
  using T::_yBorder;
194 195
  using T::_scale;
195 196
  using T::_nodeBorderQuotient;
196 197

	
197 198
  using T::_drawArrows;
198 199
  using T::_arrowLength;
199 200
  using T::_arrowWidth;
200 201

	
201 202
  using T::_showNodes;
202 203
  using T::_showArcs;
203 204

	
204 205
  using T::_enableParallel;
205 206
  using T::_parArcDist;
206 207

	
207 208
  using T::_showNodeText;
208 209
  using T::_nodeTexts;
209 210
  using T::_nodeTextSize;
210 211

	
211 212
  using T::_showNodePsText;
212 213
  using T::_nodePsTexts;
213 214
  using T::_nodePsTextsPreamble;
214 215

	
215 216
  using T::_undirected;
216 217

	
217 218
  using T::_pleaseRemoveOsStream;
218 219

	
219 220
  using T::_scaleToA4;
220 221

	
221 222
  using T::_title;
222 223
  using T::_copyright;
223 224

	
224 225
  using T::NodeTextColorType;
225 226
  using T::CUST_COL;
226 227
  using T::DIST_COL;
227 228
  using T::DIST_BW;
228 229
  using T::_nodeTextColorType;
229 230
  using T::_nodeTextColors;
230 231

	
231 232
  using T::_autoNodeScale;
232 233
  using T::_autoArcWidthScale;
233 234

	
234 235
  using T::_absoluteNodeSizes;
235 236
  using T::_absoluteArcWidths;
236 237

	
237 238

	
238 239
  using T::_negY;
239 240
  using T::_preScale;
240 241

	
241 242
  // dradnats ++C eht yb deriuqer si ti eveileb t'naC
242 243

	
243 244
  typedef typename T::Graph Graph;
245
  typedef typename T::Digraph Digraph;
244 246
  typedef typename Graph::Node Node;
245 247
  typedef typename Graph::NodeIt NodeIt;
246 248
  typedef typename Graph::Arc Arc;
247 249
  typedef typename Graph::ArcIt ArcIt;
248 250
  typedef typename Graph::InArcIt InArcIt;
249 251
  typedef typename Graph::OutArcIt OutArcIt;
250 252

	
251 253
  static const int INTERPOL_PREC;
252 254
  static const double A4HEIGHT;
253 255
  static const double A4WIDTH;
254 256
  static const double A4BORDER;
255 257

	
256 258
  bool dontPrint;
257 259

	
258 260
public:
259 261
  ///Node shapes
260 262

	
261 263
  ///Node shapes.
262 264
  ///
263 265
  enum NodeShapes {
264 266
    /// = 0
265 267
    ///\image html nodeshape_0.png
266 268
    ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
267 269
    CIRCLE=0,
268 270
    /// = 1
269 271
    ///\image html nodeshape_1.png
270 272
    ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
271 273
    SQUARE=1,
272 274
    /// = 2
273 275
    ///\image html nodeshape_2.png
274 276
    ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
275 277
    DIAMOND=2,
276 278
    /// = 3
277 279
    ///\image html nodeshape_3.png
278 280
    ///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm
279 281
    MALE=3,
280 282
    /// = 4
281 283
    ///\image html nodeshape_4.png
282 284
    ///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm
283 285
    FEMALE=4
284 286
  };
285 287

	
286 288
private:
287 289
  class arcLess {
288 290
    const Graph &g;
289 291
  public:
290 292
    arcLess(const Graph &_g) : g(_g) {}
291 293
    bool operator()(Arc a,Arc b) const
292 294
    {
293 295
      Node ai=std::min(g.source(a),g.target(a));
294 296
      Node aa=std::max(g.source(a),g.target(a));
295 297
      Node bi=std::min(g.source(b),g.target(b));
296 298
      Node ba=std::max(g.source(b),g.target(b));
297 299
      return ai<bi ||
298 300
        (ai==bi && (aa < ba ||
299 301
                    (aa==ba && ai==g.source(a) && bi==g.target(b))));
300 302
    }
301 303
  };
302 304
  bool isParallel(Arc e,Arc f) const
303 305
  {
304 306
    return (g.source(e)==g.source(f)&&
305 307
            g.target(e)==g.target(f)) ||
306 308
      (g.source(e)==g.target(f)&&
307 309
       g.target(e)==g.source(f));
308 310
  }
309 311
  template<class TT>
310 312
  static std::string psOut(const dim2::Point<TT> &p)
311 313
    {
312 314
      std::ostringstream os;
313 315
      os << p.x << ' ' << p.y;
314 316
      return os.str();
315 317
    }
316 318
  static std::string psOut(const Color &c)
317 319
    {
318 320
      std::ostringstream os;
319 321
      os << c.red() << ' ' << c.green() << ' ' << c.blue();
320 322
      return os.str();
321 323
    }
322 324

	
323 325
public:
324 326
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
325 327

	
326 328
  template<class X> struct CoordsTraits : public T {
327 329
  typedef X CoordsMapType;
328 330
    const X &_coords;
329 331
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
330 332
  };
331 333
  ///Sets the map of the node coordinates
332 334

	
333 335
  ///Sets the map of the node coordinates.
334 336
  ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
335 337
  ///\ref dim2::Point "dim2::Point<int>" values.
336 338
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
337 339
    dontPrint=true;
338 340
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
339 341
  }
340 342
  template<class X> struct NodeSizesTraits : public T {
341 343
    const X &_nodeSizes;
342 344
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
343 345
  };
344 346
  ///Sets the map of the node sizes
345 347

	
346 348
  ///Sets the map of the node sizes.
347 349
  ///\param x must be a node map with \c double (or convertible) values.
348 350
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
349 351
  {
350 352
    dontPrint=true;
351 353
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
352 354
  }
353 355
  template<class X> struct NodeShapesTraits : public T {
354 356
    const X &_nodeShapes;
355 357
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
356 358
  };
357 359
  ///Sets the map of the node shapes
358 360

	
359 361
  ///Sets the map of the node shapes.
360 362
  ///The available shape values
361 363
  ///can be found in \ref NodeShapes "enum NodeShapes".
362 364
  ///\param x must be a node map with \c int (or convertible) values.
363 365
  ///\sa NodeShapes
364 366
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
365 367
  {
366 368
    dontPrint=true;
367 369
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
368 370
  }
369 371
  template<class X> struct NodeTextsTraits : public T {
370 372
    const X &_nodeTexts;
371 373
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
372 374
  };
373 375
  ///Sets the text printed on the nodes
374 376

	
375 377
  ///Sets the text printed on the nodes.
376 378
  ///\param x must be a node map with type that can be pushed to a standard
377 379
  ///\c ostream.
378 380
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
379 381
  {
380 382
    dontPrint=true;
381 383
    _showNodeText=true;
382 384
    return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
383 385
  }
384 386
  template<class X> struct NodePsTextsTraits : public T {
385 387
    const X &_nodePsTexts;
386 388
    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
387 389
  };
388 390
  ///Inserts a PostScript block to the nodes
389 391

	
390 392
  ///With this command it is possible to insert a verbatim PostScript
391 393
  ///block to the nodes.
392 394
  ///The PS current point will be moved to the center of the node before
393 395
  ///the PostScript block inserted.
394 396
  ///
395 397
  ///Before and after the block a newline character is inserted so you
396 398
  ///don't have to bother with the separators.
397 399
  ///
398 400
  ///\param x must be a node map with type that can be pushed to a standard
399 401
  ///\c ostream.
400 402
  ///
401 403
  ///\sa nodePsTextsPreamble()
402 404
  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
403 405
  {
404 406
    dontPrint=true;
405 407
    _showNodePsText=true;
406 408
    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
407 409
  }
408 410
  template<class X> struct ArcWidthsTraits : public T {
409 411
    const X &_arcWidths;
410 412
    ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
411 413
  };
412 414
  ///Sets the map of the arc widths
413 415

	
414 416
  ///Sets the map of the arc widths.
415 417
  ///\param x must be an arc map with \c double (or convertible) values.
416 418
  template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
417 419
  {
418 420
    dontPrint=true;
419 421
    return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
420 422
  }
421 423

	
422 424
  template<class X> struct NodeColorsTraits : public T {
423 425
    const X &_nodeColors;
424 426
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
425 427
  };
426 428
  ///Sets the map of the node colors
427 429

	
428 430
  ///Sets the map of the node colors.
429 431
  ///\param x must be a node map with \ref Color values.
430 432
  ///
431 433
  ///\sa Palette
432 434
  template<class X> GraphToEps<NodeColorsTraits<X> >
433 435
  nodeColors(const X &x)
434 436
  {
435 437
    dontPrint=true;
436 438
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
437 439
  }
438 440
  template<class X> struct NodeTextColorsTraits : public T {
439 441
    const X &_nodeTextColors;
440 442
    NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
441 443
  };
442 444
  ///Sets the map of the node text colors
443 445

	
444 446
  ///Sets the map of the node text colors.
445 447
  ///\param x must be a node map with \ref Color values.
446 448
  ///
447 449
  ///\sa Palette
448 450
  template<class X> GraphToEps<NodeTextColorsTraits<X> >
449 451
  nodeTextColors(const X &x)
450 452
  {
451 453
    dontPrint=true;
452 454
    _nodeTextColorType=CUST_COL;
453 455
    return GraphToEps<NodeTextColorsTraits<X> >
454 456
      (NodeTextColorsTraits<X>(*this,x));
455 457
  }
456 458
  template<class X> struct ArcColorsTraits : public T {
457 459
    const X &_arcColors;
458 460
    ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
459 461
  };
460 462
  ///Sets the map of the arc colors
461 463

	
462 464
  ///Sets the map of the arc colors.
463 465
  ///\param x must be an arc map with \ref Color values.
464 466
  ///
465 467
  ///\sa Palette
466 468
  template<class X> GraphToEps<ArcColorsTraits<X> >
467 469
  arcColors(const X &x)
468 470
  {
469 471
    dontPrint=true;
470 472
    return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
471 473
  }
472 474
  ///Sets a global scale factor for node sizes
473 475

	
474 476
  ///Sets a global scale factor for node sizes.
475 477
  ///
476 478
  /// If nodeSizes() is not given, this function simply sets the node
477 479
  /// sizes to \c d.  If nodeSizes() is given, but
478 480
  /// autoNodeScale() is not, then the node size given by
479 481
  /// nodeSizes() will be multiplied by the value \c d.
480 482
  /// If both nodeSizes() and autoNodeScale() are used, then the
481 483
  /// node sizes will be scaled in such a way that the greatest size will be
482 484
  /// equal to \c d.
483 485
  /// \sa nodeSizes()
484 486
  /// \sa autoNodeScale()
485 487
  GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
486 488
  ///Turns on/off the automatic node size scaling.
487 489

	
488 490
  ///Turns on/off the automatic node size scaling.
489 491
  ///
490 492
  ///\sa nodeScale()
491 493
  ///
492 494
  GraphToEps<T> &autoNodeScale(bool b=true) {
493 495
    _autoNodeScale=b;return *this;
494 496
  }
495 497

	
496 498
  ///Turns on/off the absolutematic node size scaling.
497 499

	
498 500
  ///Turns on/off the absolutematic node size scaling.
499 501
  ///
500 502
  ///\sa nodeScale()
501 503
  ///
502 504
  GraphToEps<T> &absoluteNodeSizes(bool b=true) {
503 505
    _absoluteNodeSizes=b;return *this;
504 506
  }
505 507

	
506 508
  ///Negates the Y coordinates.
507 509
  GraphToEps<T> &negateY(bool b=true) {
508 510
    _negY=b;return *this;
509 511
  }
510 512

	
511 513
  ///Turn on/off pre-scaling
512 514

	
513 515
  ///By default graphToEps() rescales the whole image in order to avoid
514 516
  ///very big or very small bounding boxes.
515 517
  ///
516 518
  ///This (p)rescaling can be turned off with this function.
517 519
  ///
518 520
  GraphToEps<T> &preScale(bool b=true) {
519 521
    _preScale=b;return *this;
520 522
  }
521 523

	
522 524
  ///Sets a global scale factor for arc widths
523 525

	
524 526
  /// Sets a global scale factor for arc widths.
525 527
  ///
526 528
  /// If arcWidths() is not given, this function simply sets the arc
527 529
  /// widths to \c d.  If arcWidths() is given, but
528 530
  /// autoArcWidthScale() is not, then the arc withs given by
529 531
  /// arcWidths() will be multiplied by the value \c d.
530 532
  /// If both arcWidths() and autoArcWidthScale() are used, then the
531 533
  /// arc withs will be scaled in such a way that the greatest width will be
532 534
  /// equal to \c d.
533 535
  GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
534 536
  ///Turns on/off the automatic arc width scaling.
535 537

	
536 538
  ///Turns on/off the automatic arc width scaling.
537 539
  ///
538 540
  ///\sa arcWidthScale()
539 541
  ///
540 542
  GraphToEps<T> &autoArcWidthScale(bool b=true) {
541 543
    _autoArcWidthScale=b;return *this;
542 544
  }
543 545
  ///Turns on/off the absolutematic arc width scaling.
544 546

	
545 547
  ///Turns on/off the absolutematic arc width scaling.
546 548
  ///
547 549
  ///\sa arcWidthScale()
548 550
  ///
549 551
  GraphToEps<T> &absoluteArcWidths(bool b=true) {
550 552
    _absoluteArcWidths=b;return *this;
551 553
  }
552 554
  ///Sets a global scale factor for the whole picture
553 555
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
554 556
  ///Sets the width of the border around the picture
555 557
  GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
556 558
  ///Sets the width of the border around the picture
557 559
  GraphToEps<T> &border(double x, double y) {
558 560
    _xBorder=x;_yBorder=y;return *this;
559 561
  }
560 562
  ///Sets whether to draw arrows
561 563
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
562 564
  ///Sets the length of the arrowheads
563 565
  GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
564 566
  ///Sets the width of the arrowheads
565 567
  GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
566 568

	
567 569
  ///Scales the drawing to fit to A4 page
568 570
  GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
569 571

	
570 572
  ///Enables parallel arcs
571 573
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
572 574

	
573 575
  ///Sets the distance between parallel arcs
574 576
  GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
575 577

	
576 578
  ///Hides the arcs
577 579
  GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
578 580
  ///Hides the nodes
579 581
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
580 582

	
581 583
  ///Sets the size of the node texts
582 584
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
583 585

	
584 586
  ///Sets the color of the node texts to be different from the node color
585 587

	
586 588
  ///Sets the color of the node texts to be as different from the node color
587 589
  ///as it is possible.
588 590
  GraphToEps<T> &distantColorNodeTexts()
589 591
  {_nodeTextColorType=DIST_COL;return *this;}
590 592
  ///Sets the color of the node texts to be black or white and always visible.
591 593

	
592 594
  ///Sets the color of the node texts to be black or white according to
593 595
  ///which is more different from the node color.
594 596
  GraphToEps<T> &distantBWNodeTexts()
595 597
  {_nodeTextColorType=DIST_BW;return *this;}
596 598

	
597 599
  ///Gives a preamble block for node Postscript block.
598 600

	
599 601
  ///Gives a preamble block for node Postscript block.
600 602
  ///
601 603
  ///\sa nodePsTexts()
602 604
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
603 605
    _nodePsTextsPreamble=str ;return *this;
604 606
  }
605 607
  ///Sets whether the graph is undirected
606 608

	
607 609
  ///Sets whether the graph is undirected.
608 610
  ///
609 611
  ///This setting is the default for undirected graphs.
610 612
  ///
611 613
  ///\sa directed()
612 614
   GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
613 615

	
614 616
  ///Sets whether the graph is directed
615 617

	
616 618
  ///Sets whether the graph is directed.
617 619
  ///Use it to show the edges as a pair of directed ones.
618 620
  ///
619 621
  ///This setting is the default for digraphs.
620 622
  ///
621 623
  ///\sa undirected()
622 624
  GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
623 625

	
624 626
  ///Sets the title.
625 627

	
626 628
  ///Sets the title of the generated image,
627 629
  ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
Ignore white space 6 line context
... ...
@@ -118,586 +118,586 @@
118 118
    static int id(Node node) { return node._id; }
119 119
    static int id(Edge edge) { return edge._id; }
120 120
    static int id(Arc arc) { return arc._id; }
121 121

	
122 122
    int maxNodeId() const { return _node_num - 1; }
123 123
    int maxEdgeId() const { return _edge_num - 1; }
124 124
    int maxArcId() const { return 2 * _edge_num - 1; }
125 125

	
126 126
    static Node nodeFromId(int id) { return Node(id);}
127 127
    static Edge edgeFromId(int id) { return Edge(id);}
128 128
    static Arc arcFromId(int id) { return Arc(id);}
129 129

	
130 130
    typedef True FindEdgeTag;
131 131
    typedef True FindArcTag;
132 132

	
133 133
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
134 134
      if (prev != INVALID) return INVALID;
135 135
      if (v._id > u._id) {
136 136
        if (v._id - u._id == _width)
137 137
          return Edge(u._id);
138 138
        if (v._id - u._id == 1 && u._id % _width < _width - 1) {
139 139
          return Edge(u._id / _width * (_width - 1) +
140 140
                      u._id % _width + _edge_limit);
141 141
        }
142 142
      } else {
143 143
        if (u._id - v._id == _width)
144 144
          return Edge(v._id);
145 145
        if (u._id - v._id == 1 && v._id % _width < _width - 1) {
146 146
          return Edge(v._id / _width * (_width - 1) +
147 147
                      v._id % _width + _edge_limit);
148 148
        }
149 149
      }
150 150
      return INVALID;
151 151
    }
152 152

	
153 153
    Arc findArc(Node u, Node v, Arc prev = INVALID) const {
154 154
      if (prev != INVALID) return INVALID;
155 155
      if (v._id > u._id) {
156 156
        if (v._id - u._id == _width)
157 157
          return Arc((u._id << 1) | 1);
158 158
        if (v._id - u._id == 1 && u._id % _width < _width - 1) {
159 159
          return Arc(((u._id / _width * (_width - 1) +
160 160
                       u._id % _width + _edge_limit) << 1) | 1);
161 161
        }
162 162
      } else {
163 163
        if (u._id - v._id == _width)
164 164
          return Arc(v._id << 1);
165 165
        if (u._id - v._id == 1 && v._id % _width < _width - 1) {
166 166
          return Arc((v._id / _width * (_width - 1) +
167 167
                       v._id % _width + _edge_limit) << 1);
168 168
        }
169 169
      }
170 170
      return INVALID;
171 171
    }
172 172

	
173 173
    class Node {
174 174
      friend class GridGraphBase;
175 175

	
176 176
    protected:
177 177
      int _id;
178 178
      Node(int id) : _id(id) {}
179 179
    public:
180 180
      Node() {}
181 181
      Node (Invalid) : _id(-1) {}
182 182
      bool operator==(const Node node) const {return _id == node._id;}
183 183
      bool operator!=(const Node node) const {return _id != node._id;}
184 184
      bool operator<(const Node node) const {return _id < node._id;}
185 185
    };
186 186

	
187 187
    class Edge {
188 188
      friend class GridGraphBase;
189 189
      friend class Arc;
190 190

	
191 191
    protected:
192 192
      int _id;
193 193

	
194 194
      Edge(int id) : _id(id) {}
195 195

	
196 196
    public:
197 197
      Edge() {}
198 198
      Edge (Invalid) : _id(-1) {}
199 199
      bool operator==(const Edge edge) const {return _id == edge._id;}
200 200
      bool operator!=(const Edge edge) const {return _id != edge._id;}
201 201
      bool operator<(const Edge edge) const {return _id < edge._id;}
202 202
    };
203 203

	
204 204
    class Arc {
205 205
      friend class GridGraphBase;
206 206

	
207 207
    protected:
208 208
      int _id;
209 209

	
210 210
      Arc(int id) : _id(id) {}
211 211

	
212 212
    public:
213 213
      Arc() {}
214 214
      Arc (Invalid) : _id(-1) {}
215 215
      operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
216 216
      bool operator==(const Arc arc) const {return _id == arc._id;}
217 217
      bool operator!=(const Arc arc) const {return _id != arc._id;}
218 218
      bool operator<(const Arc arc) const {return _id < arc._id;}
219 219
    };
220 220

	
221 221
    static bool direction(Arc arc) {
222 222
      return (arc._id & 1) == 1;
223 223
    }
224 224

	
225 225
    static Arc direct(Edge edge, bool dir) {
226 226
      return Arc((edge._id << 1) | (dir ? 1 : 0));
227 227
    }
228 228

	
229 229
    void first(Node& node) const {
230 230
      node._id = _node_num - 1;
231 231
    }
232 232

	
233 233
    static void next(Node& node) {
234 234
      --node._id;
235 235
    }
236 236

	
237 237
    void first(Edge& edge) const {
238 238
      edge._id = _edge_num - 1;
239 239
    }
240 240

	
241 241
    static void next(Edge& edge) {
242 242
      --edge._id;
243 243
    }
244 244

	
245 245
    void first(Arc& arc) const {
246 246
      arc._id = 2 * _edge_num - 1;
247 247
    }
248 248

	
249 249
    static void next(Arc& arc) {
250 250
      --arc._id;
251 251
    }
252 252

	
253 253
    void firstOut(Arc& arc, const Node& node) const {
254 254
      if (node._id % _width < _width - 1) {
255 255
        arc._id = (_edge_limit + node._id % _width +
256 256
                   (node._id / _width) * (_width - 1)) << 1 | 1;
257 257
        return;
258 258
      }
259 259
      if (node._id < _node_num - _width) {
260 260
        arc._id = node._id << 1 | 1;
261 261
        return;
262 262
      }
263 263
      if (node._id % _width > 0) {
264 264
        arc._id = (_edge_limit + node._id % _width +
265 265
                   (node._id / _width) * (_width - 1) - 1) << 1;
266 266
        return;
267 267
      }
268 268
      if (node._id >= _width) {
269 269
        arc._id = (node._id - _width) << 1;
270 270
        return;
271 271
      }
272 272
      arc._id = -1;
273 273
    }
274 274

	
275 275
    void nextOut(Arc& arc) const {
276 276
      int nid = arc._id >> 1;
277 277
      if ((arc._id & 1) == 1) {
278 278
        if (nid >= _edge_limit) {
279 279
          nid = (nid - _edge_limit) % (_width - 1) +
280 280
            (nid - _edge_limit) / (_width - 1) * _width;
281 281
          if (nid < _node_num - _width) {
282 282
            arc._id = nid << 1 | 1;
283 283
            return;
284 284
          }
285 285
        }
286 286
        if (nid % _width > 0) {
287 287
          arc._id = (_edge_limit + nid % _width +
288 288
                     (nid / _width) * (_width - 1) - 1) << 1;
289 289
          return;
290 290
        }
291 291
        if (nid >= _width) {
292 292
          arc._id = (nid - _width) << 1;
293 293
          return;
294 294
        }
295 295
      } else {
296 296
        if (nid >= _edge_limit) {
297 297
          nid = (nid - _edge_limit) % (_width - 1) +
298 298
            (nid - _edge_limit) / (_width - 1) * _width + 1;
299 299
          if (nid >= _width) {
300 300
            arc._id = (nid - _width) << 1;
301 301
            return;
302 302
          }
303 303
        }
304 304
      }
305 305
      arc._id = -1;
306 306
    }
307 307

	
308 308
    void firstIn(Arc& arc, const Node& node) const {
309 309
      if (node._id % _width < _width - 1) {
310 310
        arc._id = (_edge_limit + node._id % _width +
311 311
                   (node._id / _width) * (_width - 1)) << 1;
312 312
        return;
313 313
      }
314 314
      if (node._id < _node_num - _width) {
315 315
        arc._id = node._id << 1;
316 316
        return;
317 317
      }
318 318
      if (node._id % _width > 0) {
319 319
        arc._id = (_edge_limit + node._id % _width +
320 320
                   (node._id / _width) * (_width - 1) - 1) << 1 | 1;
321 321
        return;
322 322
      }
323 323
      if (node._id >= _width) {
324 324
        arc._id = (node._id - _width) << 1 | 1;
325 325
        return;
326 326
      }
327 327
      arc._id = -1;
328 328
    }
329 329

	
330 330
    void nextIn(Arc& arc) const {
331 331
      int nid = arc._id >> 1;
332 332
      if ((arc._id & 1) == 0) {
333 333
        if (nid >= _edge_limit) {
334 334
          nid = (nid - _edge_limit) % (_width - 1) +
335 335
            (nid - _edge_limit) / (_width - 1) * _width;
336 336
          if (nid < _node_num - _width) {
337 337
            arc._id = nid << 1;
338 338
            return;
339 339
          }
340 340
        }
341 341
        if (nid % _width > 0) {
342 342
          arc._id = (_edge_limit + nid % _width +
343 343
                     (nid / _width) * (_width - 1) - 1) << 1 | 1;
344 344
          return;
345 345
        }
346 346
        if (nid >= _width) {
347 347
          arc._id = (nid - _width) << 1 | 1;
348 348
          return;
349 349
        }
350 350
      } else {
351 351
        if (nid >= _edge_limit) {
352 352
          nid = (nid - _edge_limit) % (_width - 1) +
353 353
            (nid - _edge_limit) / (_width - 1) * _width + 1;
354 354
          if (nid >= _width) {
355 355
            arc._id = (nid - _width) << 1 | 1;
356 356
            return;
357 357
          }
358 358
        }
359 359
      }
360 360
      arc._id = -1;
361 361
    }
362 362

	
363 363
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
364 364
      if (node._id % _width < _width - 1) {
365 365
        edge._id = _edge_limit + node._id % _width +
366 366
          (node._id / _width) * (_width - 1);
367 367
        dir = true;
368 368
        return;
369 369
      }
370 370
      if (node._id < _node_num - _width) {
371 371
        edge._id = node._id;
372 372
        dir = true;
373 373
        return;
374 374
      }
375 375
      if (node._id % _width > 0) {
376 376
        edge._id = _edge_limit + node._id % _width +
377 377
          (node._id / _width) * (_width - 1) - 1;
378 378
        dir = false;
379 379
        return;
380 380
      }
381 381
      if (node._id >= _width) {
382 382
        edge._id = node._id - _width;
383 383
        dir = false;
384 384
        return;
385 385
      }
386 386
      edge._id = -1;
387 387
      dir = true;
388 388
    }
389 389

	
390 390
    void nextInc(Edge& edge, bool& dir) const {
391 391
      int nid = edge._id;
392 392
      if (dir) {
393 393
        if (nid >= _edge_limit) {
394 394
          nid = (nid - _edge_limit) % (_width - 1) +
395 395
            (nid - _edge_limit) / (_width - 1) * _width;
396 396
          if (nid < _node_num - _width) {
397 397
            edge._id = nid;
398 398
            return;
399 399
          }
400 400
        }
401 401
        if (nid % _width > 0) {
402 402
          edge._id = _edge_limit + nid % _width +
403 403
            (nid / _width) * (_width - 1) - 1;
404 404
          dir = false;
405 405
          return;
406 406
        }
407 407
        if (nid >= _width) {
408 408
          edge._id = nid - _width;
409 409
          dir = false;
410 410
          return;
411 411
        }
412 412
      } else {
413 413
        if (nid >= _edge_limit) {
414 414
          nid = (nid - _edge_limit) % (_width - 1) +
415 415
            (nid - _edge_limit) / (_width - 1) * _width + 1;
416 416
          if (nid >= _width) {
417 417
            edge._id = nid - _width;
418 418
            return;
419 419
          }
420 420
        }
421 421
      }
422 422
      edge._id = -1;
423 423
      dir = true;
424 424
    }
425 425

	
426 426
    Arc right(Node n) const {
427 427
      if (n._id % _width < _width - 1) {
428 428
        return Arc(((_edge_limit + n._id % _width +
429 429
                    (n._id / _width) * (_width - 1)) << 1) | 1);
430 430
      } else {
431 431
        return INVALID;
432 432
      }
433 433
    }
434 434

	
435 435
    Arc left(Node n) const {
436 436
      if (n._id % _width > 0) {
437 437
        return Arc((_edge_limit + n._id % _width +
438 438
                     (n._id / _width) * (_width - 1) - 1) << 1);
439 439
      } else {
440 440
        return INVALID;
441 441
      }
442 442
    }
443 443

	
444 444
    Arc up(Node n) const {
445 445
      if (n._id < _edge_limit) {
446 446
        return Arc((n._id << 1) | 1);
447 447
      } else {
448 448
        return INVALID;
449 449
      }
450 450
    }
451 451

	
452 452
    Arc down(Node n) const {
453 453
      if (n._id >= _width) {
454 454
        return Arc((n._id - _width) << 1);
455 455
      } else {
456 456
        return INVALID;
457 457
      }
458 458
    }
459 459

	
460 460
  private:
461 461
    int _width, _height;
462 462
    int _node_num, _edge_num;
463 463
    int _edge_limit;
464 464
  };
465 465

	
466 466

	
467 467
  typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase;
468 468

	
469 469
  /// \ingroup graphs
470 470
  ///
471 471
  /// \brief Grid graph class
472 472
  ///
473 473
  /// This class implements a special graph type. The nodes of the
474 474
  /// graph can be indexed by two integer \c (i,j) value where \c i is
475 475
  /// in the \c [0..width()-1] range and j is in the \c
476 476
  /// [0..height()-1] range.  Two nodes are connected in the graph if
477 477
  /// the indexes differ exactly on one position and exactly one is
478 478
  /// the difference. The nodes of the graph can be indexed by position
479 479
  /// with the \c operator()() function. The positions of the nodes can be
480 480
  /// get with \c pos(), \c col() and \c row() members. The outgoing
481 481
  /// arcs can be retrieved with the \c right(), \c up(), \c left()
482 482
  /// and \c down() functions, where the bottom-left corner is the
483 483
  /// origin.
484 484
  ///
485 485
  /// \image html grid_graph.png
486 486
  /// \image latex grid_graph.eps "Grid graph" width=\textwidth
487 487
  ///
488 488
  /// A short example about the basic usage:
489 489
  ///\code
490 490
  /// GridGraph graph(rows, cols);
491 491
  /// GridGraph::NodeMap<int> val(graph);
492 492
  /// for (int i = 0; i < graph.width(); ++i) {
493 493
  ///   for (int j = 0; j < graph.height(); ++j) {
494 494
  ///     val[graph(i, j)] = i + j;
495 495
  ///   }
496 496
  /// }
497 497
  ///\endcode
498 498
  ///
499 499
  /// This graph type fully conforms to the \ref concepts::Graph
500 500
  /// "Graph concept".
501 501
  class GridGraph : public ExtendedGridGraphBase {
502
    typedef ExtendedGridGraphBase Parent;
503

	
502 504
  public:
503 505

	
504
    typedef ExtendedGridGraphBase Parent;
505

	
506 506
    /// \brief Map to get the indices of the nodes as dim2::Point<int>.
507 507
    ///
508 508
    /// Map to get the indices of the nodes as dim2::Point<int>.
509 509
    class IndexMap {
510 510
    public:
511 511
      /// \brief The key type of the map
512 512
      typedef GridGraph::Node Key;
513 513
      /// \brief The value type of the map
514 514
      typedef dim2::Point<int> Value;
515 515

	
516 516
      /// \brief Constructor
517 517
      ///
518 518
      /// Constructor
519 519
      IndexMap(const GridGraph& graph) : _graph(graph) {}
520 520

	
521 521
      /// \brief The subscript operator
522 522
      ///
523 523
      /// The subscript operator.
524 524
      Value operator[](Key key) const {
525 525
        return _graph.pos(key);
526 526
      }
527 527

	
528 528
    private:
529 529
      const GridGraph& _graph;
530 530
    };
531 531

	
532 532
    /// \brief Map to get the column of the nodes.
533 533
    ///
534 534
    /// Map to get the column of the nodes.
535 535
    class ColMap {
536 536
    public:
537 537
      /// \brief The key type of the map
538 538
      typedef GridGraph::Node Key;
539 539
      /// \brief The value type of the map
540 540
      typedef int Value;
541 541

	
542 542
      /// \brief Constructor
543 543
      ///
544 544
      /// Constructor
545 545
      ColMap(const GridGraph& graph) : _graph(graph) {}
546 546

	
547 547
      /// \brief The subscript operator
548 548
      ///
549 549
      /// The subscript operator.
550 550
      Value operator[](Key key) const {
551 551
        return _graph.col(key);
552 552
      }
553 553

	
554 554
    private:
555 555
      const GridGraph& _graph;
556 556
    };
557 557

	
558 558
    /// \brief Map to get the row of the nodes.
559 559
    ///
560 560
    /// Map to get the row of the nodes.
561 561
    class RowMap {
562 562
    public:
563 563
      /// \brief The key type of the map
564 564
      typedef GridGraph::Node Key;
565 565
      /// \brief The value type of the map
566 566
      typedef int Value;
567 567

	
568 568
      /// \brief Constructor
569 569
      ///
570 570
      /// Constructor
571 571
      RowMap(const GridGraph& graph) : _graph(graph) {}
572 572

	
573 573
      /// \brief The subscript operator
574 574
      ///
575 575
      /// The subscript operator.
576 576
      Value operator[](Key key) const {
577 577
        return _graph.row(key);
578 578
      }
579 579

	
580 580
    private:
581 581
      const GridGraph& _graph;
582 582
    };
583 583

	
584 584
    /// \brief Constructor
585 585
    ///
586 586
    /// Construct a grid graph with given size.
587 587
    GridGraph(int width, int height) { construct(width, height); }
588 588

	
589 589
    /// \brief Resize the graph
590 590
    ///
591 591
    /// Resize the graph. The function will fully destroy and rebuild
592 592
    /// the graph.  This cause that the maps of the graph will
593 593
    /// reallocated automatically and the previous values will be
594 594
    /// lost.
595 595
    void resize(int width, int height) {
596 596
      Parent::notifier(Arc()).clear();
597 597
      Parent::notifier(Edge()).clear();
598 598
      Parent::notifier(Node()).clear();
599 599
      construct(width, height);
600 600
      Parent::notifier(Node()).build();
601 601
      Parent::notifier(Edge()).build();
602 602
      Parent::notifier(Arc()).build();
603 603
    }
604 604

	
605 605
    /// \brief The node on the given position.
606 606
    ///
607 607
    /// Gives back the node on the given position.
608 608
    Node operator()(int i, int j) const {
609 609
      return Parent::operator()(i, j);
610 610
    }
611 611

	
612 612
    /// \brief Gives back the column index of the node.
613 613
    ///
614 614
    /// Gives back the column index of the node.
615 615
    int col(Node n) const {
616 616
      return Parent::col(n);
617 617
    }
618 618

	
619 619
    /// \brief Gives back the row index of the node.
620 620
    ///
621 621
    /// Gives back the row index of the node.
622 622
    int row(Node n) const {
623 623
      return Parent::row(n);
624 624
    }
625 625

	
626 626
    /// \brief Gives back the position of the node.
627 627
    ///
628 628
    /// Gives back the position of the node, ie. the <tt>(col,row)</tt> pair.
629 629
    dim2::Point<int> pos(Node n) const {
630 630
      return Parent::pos(n);
631 631
    }
632 632

	
633 633
    /// \brief Gives back the number of the columns.
634 634
    ///
635 635
    /// Gives back the number of the columns.
636 636
    int width() const {
637 637
      return Parent::width();
638 638
    }
639 639

	
640 640
    /// \brief Gives back the number of the rows.
641 641
    ///
642 642
    /// Gives back the number of the rows.
643 643
    int height() const {
644 644
      return Parent::height();
645 645
    }
646 646

	
647 647
    /// \brief Gives back the arc goes right from the node.
648 648
    ///
649 649
    /// Gives back the arc goes right from the node. If there is not
650 650
    /// outgoing arc then it gives back INVALID.
651 651
    Arc right(Node n) const {
652 652
      return Parent::right(n);
653 653
    }
654 654

	
655 655
    /// \brief Gives back the arc goes left from the node.
656 656
    ///
657 657
    /// Gives back the arc goes left from the node. If there is not
658 658
    /// outgoing arc then it gives back INVALID.
659 659
    Arc left(Node n) const {
660 660
      return Parent::left(n);
661 661
    }
662 662

	
663 663
    /// \brief Gives back the arc goes up from the node.
664 664
    ///
665 665
    /// Gives back the arc goes up from the node. If there is not
666 666
    /// outgoing arc then it gives back INVALID.
667 667
    Arc up(Node n) const {
668 668
      return Parent::up(n);
669 669
    }
670 670

	
671 671
    /// \brief Gives back the arc goes down from the node.
672 672
    ///
673 673
    /// Gives back the arc goes down from the node. If there is not
674 674
    /// outgoing arc then it gives back INVALID.
675 675
    Arc down(Node n) const {
676 676
      return Parent::down(n);
677 677
    }
678 678

	
679 679
    /// \brief Index map of the grid graph
680 680
    ///
681 681
    /// Just returns an IndexMap for the grid graph.
682 682
    IndexMap indexMap() const {
683 683
      return IndexMap(*this);
684 684
    }
685 685

	
686 686
    /// \brief Row map of the grid graph
687 687
    ///
688 688
    /// Just returns a RowMap for the grid graph.
689 689
    RowMap rowMap() const {
690 690
      return RowMap(*this);
691 691
    }
692 692

	
693 693
    /// \brief Column map of the grid graph
694 694
    ///
695 695
    /// Just returns a ColMap for the grid graph.
696 696
    ColMap colMap() const {
697 697
      return ColMap(*this);
698 698
    }
699 699

	
700 700
  };
701 701

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

	
19 19
#ifndef HYPERCUBE_GRAPH_H
20 20
#define HYPERCUBE_GRAPH_H
21 21

	
22 22
#include <vector>
23 23
#include <lemon/core.h>
24 24
#include <lemon/assert.h>
25 25
#include <lemon/bits/graph_extender.h>
26 26

	
27 27
///\ingroup graphs
28 28
///\file
29 29
///\brief HypercubeGraph class.
30 30

	
31 31
namespace lemon {
32 32

	
33 33
  class HypercubeGraphBase {
34 34

	
35 35
  public:
36 36

	
37 37
    typedef HypercubeGraphBase Graph;
38 38

	
39 39
    class Node;
40 40
    class Edge;
41 41
    class Arc;
42 42

	
43 43
  public:
44 44

	
45 45
    HypercubeGraphBase() {}
46 46

	
47 47
  protected:
48 48

	
49 49
    void construct(int dim) {
50 50
      LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1.");
51 51
      _dim = dim;
52 52
      _node_num = 1 << dim;
53 53
      _edge_num = dim * (1 << (dim-1));
54 54
    }
55 55

	
56 56
  public:
57 57

	
58 58
    typedef True NodeNumTag;
59 59
    typedef True EdgeNumTag;
60 60
    typedef True ArcNumTag;
61 61

	
62 62
    int nodeNum() const { return _node_num; }
63 63
    int edgeNum() const { return _edge_num; }
64 64
    int arcNum() const { return 2 * _edge_num; }
65 65

	
66 66
    int maxNodeId() const { return _node_num - 1; }
67 67
    int maxEdgeId() const { return _edge_num - 1; }
68 68
    int maxArcId() const { return 2 * _edge_num - 1; }
69 69

	
70 70
    static Node nodeFromId(int id) { return Node(id); }
71 71
    static Edge edgeFromId(int id) { return Edge(id); }
72 72
    static Arc arcFromId(int id) { return Arc(id); }
73 73

	
74 74
    static int id(Node node) { return node._id; }
75 75
    static int id(Edge edge) { return edge._id; }
76 76
    static int id(Arc arc) { return arc._id; }
77 77

	
78 78
    Node u(Edge edge) const {
79 79
      int base = edge._id & ((1 << (_dim-1)) - 1);
80 80
      int k = edge._id >> (_dim-1);
81 81
      return ((base >> k) << (k+1)) | (base & ((1 << k) - 1));
82 82
    }
83 83

	
84 84
    Node v(Edge edge) const {
85 85
      int base = edge._id & ((1 << (_dim-1)) - 1);
86 86
      int k = edge._id >> (_dim-1);
87 87
      return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k);
88 88
    }
89 89

	
90 90
    Node source(Arc arc) const {
91 91
      return (arc._id & 1) == 1 ? u(arc) : v(arc);
92 92
    }
93 93

	
94 94
    Node target(Arc arc) const {
95 95
      return (arc._id & 1) == 1 ? v(arc) : u(arc);
96 96
    }
97 97

	
98 98
    typedef True FindEdgeTag;
99 99
    typedef True FindArcTag;
100 100

	
101 101
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
102 102
      if (prev != INVALID) return INVALID;
103 103
      int d = u._id ^ v._id;
104 104
      int k = 0;
105 105
      if (d == 0) return INVALID;
106 106
      for ( ; (d & 1) == 0; d >>= 1) ++k;
107 107
      if (d >> 1 != 0) return INVALID;
108 108
      return (k << (_dim-1)) | ((u._id >> (k+1)) << k) |
109 109
        (u._id & ((1 << k) - 1));
110 110
    }
111 111

	
112 112
    Arc findArc(Node u, Node v, Arc prev = INVALID) const {
113 113
      Edge edge = findEdge(u, v, prev);
114 114
      if (edge == INVALID) return INVALID;
115 115
      int k = edge._id >> (_dim-1);
116 116
      return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1;
117 117
    }
118 118

	
119 119
    class Node {
120 120
      friend class HypercubeGraphBase;
121 121

	
122 122
    protected:
123 123
      int _id;
124 124
      Node(int id) : _id(id) {}
125 125
    public:
126 126
      Node() {}
127 127
      Node (Invalid) : _id(-1) {}
128 128
      bool operator==(const Node node) const {return _id == node._id;}
129 129
      bool operator!=(const Node node) const {return _id != node._id;}
130 130
      bool operator<(const Node node) const {return _id < node._id;}
131 131
    };
132 132

	
133 133
    class Edge {
134 134
      friend class HypercubeGraphBase;
135 135
      friend class Arc;
136 136

	
137 137
    protected:
138 138
      int _id;
139 139

	
140 140
      Edge(int id) : _id(id) {}
141 141

	
142 142
    public:
143 143
      Edge() {}
144 144
      Edge (Invalid) : _id(-1) {}
145 145
      bool operator==(const Edge edge) const {return _id == edge._id;}
146 146
      bool operator!=(const Edge edge) const {return _id != edge._id;}
147 147
      bool operator<(const Edge edge) const {return _id < edge._id;}
148 148
    };
149 149

	
150 150
    class Arc {
151 151
      friend class HypercubeGraphBase;
152 152

	
153 153
    protected:
154 154
      int _id;
155 155

	
156 156
      Arc(int id) : _id(id) {}
157 157

	
158 158
    public:
159 159
      Arc() {}
160 160
      Arc (Invalid) : _id(-1) {}
161 161
      operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
162 162
      bool operator==(const Arc arc) const {return _id == arc._id;}
163 163
      bool operator!=(const Arc arc) const {return _id != arc._id;}
164 164
      bool operator<(const Arc arc) const {return _id < arc._id;}
165 165
    };
166 166

	
167 167
    void first(Node& node) const {
168 168
      node._id = _node_num - 1;
169 169
    }
170 170

	
171 171
    static void next(Node& node) {
172 172
      --node._id;
173 173
    }
174 174

	
175 175
    void first(Edge& edge) const {
176 176
      edge._id = _edge_num - 1;
177 177
    }
178 178

	
179 179
    static void next(Edge& edge) {
180 180
      --edge._id;
181 181
    }
182 182

	
183 183
    void first(Arc& arc) const {
184 184
      arc._id = 2 * _edge_num - 1;
185 185
    }
186 186

	
187 187
    static void next(Arc& arc) {
188 188
      --arc._id;
189 189
    }
190 190

	
191 191
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
192 192
      edge._id = node._id >> 1;
193 193
      dir = (node._id & 1) == 0;
194 194
    }
195 195

	
196 196
    void nextInc(Edge& edge, bool& dir) const {
197 197
      Node n = dir ? u(edge) : v(edge);
198 198
      int k = (edge._id >> (_dim-1)) + 1;
199 199
      if (k < _dim) {
200 200
        edge._id = (k << (_dim-1)) |
201 201
          ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
202 202
        dir = ((n._id >> k) & 1) == 0;
203 203
      } else {
204 204
        edge._id = -1;
205 205
        dir = true;
206 206
      }
207 207
    }
208 208

	
209 209
    void firstOut(Arc& arc, const Node& node) const {
210 210
      arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
211 211
    }
212 212

	
213 213
    void nextOut(Arc& arc) const {
214 214
      Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
215 215
      int k = (arc._id >> _dim) + 1;
216 216
      if (k < _dim) {
217 217
        arc._id = (k << (_dim-1)) |
218 218
          ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
219 219
        arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
220 220
      } else {
221 221
        arc._id = -1;
222 222
      }
223 223
    }
224 224

	
225 225
    void firstIn(Arc& arc, const Node& node) const {
226 226
      arc._id = ((node._id >> 1) << 1) | (node._id & 1);
227 227
    }
228 228

	
229 229
    void nextIn(Arc& arc) const {
230 230
      Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
231 231
      int k = (arc._id >> _dim) + 1;
232 232
      if (k < _dim) {
233 233
        arc._id = (k << (_dim-1)) |
234 234
          ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
235 235
        arc._id = (arc._id << 1) | ((n._id >> k) & 1);
236 236
      } else {
237 237
        arc._id = -1;
238 238
      }
239 239
    }
240 240

	
241 241
    static bool direction(Arc arc) {
242 242
      return (arc._id & 1) == 1;
243 243
    }
244 244

	
245 245
    static Arc direct(Edge edge, bool dir) {
246 246
      return Arc((edge._id << 1) | (dir ? 1 : 0));
247 247
    }
248 248

	
249 249
    int dimension() const {
250 250
      return _dim;
251 251
    }
252 252

	
253 253
    bool projection(Node node, int n) const {
254 254
      return static_cast<bool>(node._id & (1 << n));
255 255
    }
256 256

	
257 257
    int dimension(Edge edge) const {
258 258
      return edge._id >> (_dim-1);
259 259
    }
260 260

	
261 261
    int dimension(Arc arc) const {
262 262
      return arc._id >> _dim;
263 263
    }
264 264

	
265 265
    int index(Node node) const {
266 266
      return node._id;
267 267
    }
268 268

	
269 269
    Node operator()(int ix) const {
270 270
      return Node(ix);
271 271
    }
272 272

	
273 273
  private:
274 274
    int _dim;
275 275
    int _node_num, _edge_num;
276 276
  };
277 277

	
278 278

	
279 279
  typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
280 280

	
281 281
  /// \ingroup graphs
282 282
  ///
283 283
  /// \brief Hypercube graph class
284 284
  ///
285 285
  /// This class implements a special graph type. The nodes of the graph
286 286
  /// are indiced with integers with at most \c dim binary digits.
287 287
  /// Two nodes are connected in the graph if and only if their indices
288 288
  /// differ only on one position in the binary form.
289 289
  ///
290 290
  /// \note The type of the indices is chosen to \c int for efficiency
291 291
  /// reasons. Thus the maximum dimension of this implementation is 26
292 292
  /// (assuming that the size of \c int is 32 bit).
293 293
  ///
294 294
  /// This graph type fully conforms to the \ref concepts::Graph
295 295
  /// "Graph concept".
296 296
  class HypercubeGraph : public ExtendedHypercubeGraphBase {
297
    typedef ExtendedHypercubeGraphBase Parent;
298

	
297 299
  public:
298 300

	
299
    typedef ExtendedHypercubeGraphBase Parent;
300

	
301 301
    /// \brief Constructs a hypercube graph with \c dim dimensions.
302 302
    ///
303 303
    /// Constructs a hypercube graph with \c dim dimensions.
304 304
    HypercubeGraph(int dim) { construct(dim); }
305 305

	
306 306
    /// \brief The number of dimensions.
307 307
    ///
308 308
    /// Gives back the number of dimensions.
309 309
    int dimension() const {
310 310
      return Parent::dimension();
311 311
    }
312 312

	
313 313
    /// \brief Returns \c true if the n'th bit of the node is one.
314 314
    ///
315 315
    /// Returns \c true if the n'th bit of the node is one.
316 316
    bool projection(Node node, int n) const {
317 317
      return Parent::projection(node, n);
318 318
    }
319 319

	
320 320
    /// \brief The dimension id of an edge.
321 321
    ///
322 322
    /// Gives back the dimension id of the given edge.
323 323
    /// It is in the [0..dim-1] range.
324 324
    int dimension(Edge edge) const {
325 325
      return Parent::dimension(edge);
326 326
    }
327 327

	
328 328
    /// \brief The dimension id of an arc.
329 329
    ///
330 330
    /// Gives back the dimension id of the given arc.
331 331
    /// It is in the [0..dim-1] range.
332 332
    int dimension(Arc arc) const {
333 333
      return Parent::dimension(arc);
334 334
    }
335 335

	
336 336
    /// \brief The index of a node.
337 337
    ///
338 338
    /// Gives back the index of the given node.
339 339
    /// The lower bits of the integer describes the node.
340 340
    int index(Node node) const {
341 341
      return Parent::index(node);
342 342
    }
343 343

	
344 344
    /// \brief Gives back a node by its index.
345 345
    ///
346 346
    /// Gives back a node by its index.
347 347
    Node operator()(int ix) const {
348 348
      return Parent::operator()(ix);
349 349
    }
350 350

	
351 351
    /// \brief Number of nodes.
352 352
    int nodeNum() const { return Parent::nodeNum(); }
353 353
    /// \brief Number of edges.
354 354
    int edgeNum() const { return Parent::edgeNum(); }
355 355
    /// \brief Number of arcs.
356 356
    int arcNum() const { return Parent::arcNum(); }
357 357

	
358 358
    /// \brief Linear combination map.
359 359
    ///
360 360
    /// This map makes possible to give back a linear combination
361 361
    /// for each node. It works like the \c std::accumulate function,
362 362
    /// so it accumulates the \c bf binary function with the \c fv first
363 363
    /// value. The map accumulates only on that positions (dimensions)
364 364
    /// where the index of the node is one. The values that have to be
365 365
    /// accumulated should be given by the \c begin and \c end iterators
366 366
    /// and the length of this range should be equal to the dimension
367 367
    /// number of the graph.
368 368
    ///
369 369
    ///\code
370 370
    /// const int DIM = 3;
371 371
    /// HypercubeGraph graph(DIM);
372 372
    /// dim2::Point<double> base[DIM];
373 373
    /// for (int k = 0; k < DIM; ++k) {
374 374
    ///   base[k].x = rnd();
375 375
    ///   base[k].y = rnd();
376 376
    /// }
377 377
    /// HypercubeGraph::HyperMap<dim2::Point<double> >
378 378
    ///   pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
379 379
    ///\endcode
380 380
    ///
381 381
    /// \see HypercubeGraph
382 382
    template <typename T, typename BF = std::plus<T> >
383 383
    class HyperMap {
384 384
    public:
385 385

	
386 386
      /// \brief The key type of the map
387 387
      typedef Node Key;
388 388
      /// \brief The value type of the map
389 389
      typedef T Value;
390 390

	
391 391
      /// \brief Constructor for HyperMap.
392 392
      ///
393 393
      /// Construct a HyperMap for the given graph. The values that have
394 394
      /// to be accumulated should be given by the \c begin and \c end
395 395
      /// iterators and the length of this range should be equal to the
396 396
      /// dimension number of the graph.
397 397
      ///
398 398
      /// This map accumulates the \c bf binary function with the \c fv
399 399
      /// first value on that positions (dimensions) where the index of
400 400
      /// the node is one.
401 401
      template <typename It>
402 402
      HyperMap(const Graph& graph, It begin, It end,
403 403
               T fv = 0, const BF& bf = BF())
404 404
        : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf)
405 405
      {
406 406
        LEMON_ASSERT(_values.size() == graph.dimension(),
407 407
                     "Wrong size of range");
408 408
      }
409 409

	
410 410
      /// \brief The partial accumulated value.
411 411
      ///
412 412
      /// Gives back the partial accumulated value.
413 413
      Value operator[](const Key& k) const {
414 414
        Value val = _first_value;
415 415
        int id = _graph.index(k);
416 416
        int n = 0;
417 417
        while (id != 0) {
418 418
          if (id & 1) {
419 419
            val = _bin_func(val, _values[n]);
420 420
          }
421 421
          id >>= 1;
422 422
          ++n;
423 423
        }
424 424
        return val;
425 425
      }
426 426

	
427 427
    private:
428 428
      const Graph& _graph;
429 429
      std::vector<T> _values;
430 430
      T _first_value;
431 431
      BF _bin_func;
432 432
    };
433 433

	
434 434
  };
435 435

	
436 436
}
437 437

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

	
19 19
#ifndef LEMON_LIST_GRAPH_H
20 20
#define LEMON_LIST_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24 24
///\brief ListDigraph, ListGraph classes.
25 25

	
26 26
#include <lemon/core.h>
27 27
#include <lemon/error.h>
28 28
#include <lemon/bits/graph_extender.h>
29 29

	
30 30
#include <vector>
31 31
#include <list>
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  class ListDigraphBase {
36 36

	
37 37
  protected:
38 38
    struct NodeT {
39 39
      int first_in, first_out;
40 40
      int prev, next;
41 41
    };
42 42

	
43 43
    struct ArcT {
44 44
      int target, source;
45 45
      int prev_in, prev_out;
46 46
      int next_in, next_out;
47 47
    };
48 48

	
49 49
    std::vector<NodeT> nodes;
50 50

	
51 51
    int first_node;
52 52

	
53 53
    int first_free_node;
54 54

	
55 55
    std::vector<ArcT> arcs;
56 56

	
57 57
    int first_free_arc;
58 58

	
59 59
  public:
60 60

	
61 61
    typedef ListDigraphBase Digraph;
62 62

	
63 63
    class Node {
64 64
      friend class ListDigraphBase;
65 65
    protected:
66 66

	
67 67
      int id;
68 68
      explicit Node(int pid) { id = pid;}
69 69

	
70 70
    public:
71 71
      Node() {}
72 72
      Node (Invalid) { id = -1; }
73 73
      bool operator==(const Node& node) const {return id == node.id;}
74 74
      bool operator!=(const Node& node) const {return id != node.id;}
75 75
      bool operator<(const Node& node) const {return id < node.id;}
76 76
    };
77 77

	
78 78
    class Arc {
79 79
      friend class ListDigraphBase;
80 80
    protected:
81 81

	
82 82
      int id;
83 83
      explicit Arc(int pid) { id = pid;}
84 84

	
85 85
    public:
86 86
      Arc() {}
87 87
      Arc (Invalid) { id = -1; }
88 88
      bool operator==(const Arc& arc) const {return id == arc.id;}
89 89
      bool operator!=(const Arc& arc) const {return id != arc.id;}
90 90
      bool operator<(const Arc& arc) const {return id < arc.id;}
91 91
    };
92 92

	
93 93

	
94 94

	
95 95
    ListDigraphBase()
96 96
      : nodes(), first_node(-1),
97 97
        first_free_node(-1), arcs(), first_free_arc(-1) {}
98 98

	
99 99

	
100 100
    int maxNodeId() const { return nodes.size()-1; }
101 101
    int maxArcId() const { return arcs.size()-1; }
102 102

	
103 103
    Node source(Arc e) const { return Node(arcs[e.id].source); }
104 104
    Node target(Arc e) const { return Node(arcs[e.id].target); }
105 105

	
106 106

	
107 107
    void first(Node& node) const {
108 108
      node.id = first_node;
109 109
    }
110 110

	
111 111
    void next(Node& node) const {
112 112
      node.id = nodes[node.id].next;
113 113
    }
114 114

	
115 115

	
116 116
    void first(Arc& arc) const {
117 117
      int n;
118 118
      for(n = first_node;
119 119
          n!=-1 && nodes[n].first_in == -1;
120 120
          n = nodes[n].next) {}
121 121
      arc.id = (n == -1) ? -1 : nodes[n].first_in;
122 122
    }
123 123

	
124 124
    void next(Arc& arc) const {
125 125
      if (arcs[arc.id].next_in != -1) {
126 126
        arc.id = arcs[arc.id].next_in;
127 127
      } else {
128 128
        int n;
129 129
        for(n = nodes[arcs[arc.id].target].next;
130 130
            n!=-1 && nodes[n].first_in == -1;
131 131
            n = nodes[n].next) {}
132 132
        arc.id = (n == -1) ? -1 : nodes[n].first_in;
133 133
      }
134 134
    }
135 135

	
136 136
    void firstOut(Arc &e, const Node& v) const {
137 137
      e.id = nodes[v.id].first_out;
138 138
    }
139 139
    void nextOut(Arc &e) const {
140 140
      e.id=arcs[e.id].next_out;
141 141
    }
142 142

	
143 143
    void firstIn(Arc &e, const Node& v) const {
144 144
      e.id = nodes[v.id].first_in;
145 145
    }
146 146
    void nextIn(Arc &e) const {
147 147
      e.id=arcs[e.id].next_in;
148 148
    }
149 149

	
150 150

	
151 151
    static int id(Node v) { return v.id; }
152 152
    static int id(Arc e) { return e.id; }
153 153

	
154 154
    static Node nodeFromId(int id) { return Node(id);}
155 155
    static Arc arcFromId(int id) { return Arc(id);}
156 156

	
157 157
    bool valid(Node n) const {
158 158
      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
159 159
        nodes[n.id].prev != -2;
160 160
    }
161 161

	
162 162
    bool valid(Arc a) const {
163 163
      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
164 164
        arcs[a.id].prev_in != -2;
165 165
    }
166 166

	
167 167
    Node addNode() {
168 168
      int n;
169 169

	
170 170
      if(first_free_node==-1) {
171 171
        n = nodes.size();
172 172
        nodes.push_back(NodeT());
173 173
      } else {
174 174
        n = first_free_node;
175 175
        first_free_node = nodes[n].next;
176 176
      }
177 177

	
178 178
      nodes[n].next = first_node;
179 179
      if(first_node != -1) nodes[first_node].prev = n;
180 180
      first_node = n;
181 181
      nodes[n].prev = -1;
182 182

	
183 183
      nodes[n].first_in = nodes[n].first_out = -1;
184 184

	
185 185
      return Node(n);
186 186
    }
187 187

	
188 188
    Arc addArc(Node u, Node v) {
189 189
      int n;
190 190

	
191 191
      if (first_free_arc == -1) {
192 192
        n = arcs.size();
193 193
        arcs.push_back(ArcT());
194 194
      } else {
195 195
        n = first_free_arc;
196 196
        first_free_arc = arcs[n].next_in;
197 197
      }
198 198

	
199 199
      arcs[n].source = u.id;
200 200
      arcs[n].target = v.id;
201 201

	
202 202
      arcs[n].next_out = nodes[u.id].first_out;
203 203
      if(nodes[u.id].first_out != -1) {
204 204
        arcs[nodes[u.id].first_out].prev_out = n;
205 205
      }
206 206

	
207 207
      arcs[n].next_in = nodes[v.id].first_in;
208 208
      if(nodes[v.id].first_in != -1) {
209 209
        arcs[nodes[v.id].first_in].prev_in = n;
210 210
      }
211 211

	
212 212
      arcs[n].prev_in = arcs[n].prev_out = -1;
213 213

	
214 214
      nodes[u.id].first_out = nodes[v.id].first_in = n;
215 215

	
216 216
      return Arc(n);
217 217
    }
218 218

	
219 219
    void erase(const Node& node) {
220 220
      int n = node.id;
221 221

	
222 222
      if(nodes[n].next != -1) {
223 223
        nodes[nodes[n].next].prev = nodes[n].prev;
224 224
      }
225 225

	
226 226
      if(nodes[n].prev != -1) {
227 227
        nodes[nodes[n].prev].next = nodes[n].next;
228 228
      } else {
229 229
        first_node = nodes[n].next;
230 230
      }
231 231

	
232 232
      nodes[n].next = first_free_node;
233 233
      first_free_node = n;
234 234
      nodes[n].prev = -2;
235 235

	
236 236
    }
237 237

	
238 238
    void erase(const Arc& arc) {
239 239
      int n = arc.id;
240 240

	
241 241
      if(arcs[n].next_in!=-1) {
242 242
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
243 243
      }
244 244

	
245 245
      if(arcs[n].prev_in!=-1) {
246 246
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
247 247
      } else {
248 248
        nodes[arcs[n].target].first_in = arcs[n].next_in;
249 249
      }
250 250

	
251 251

	
252 252
      if(arcs[n].next_out!=-1) {
253 253
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
254 254
      }
255 255

	
256 256
      if(arcs[n].prev_out!=-1) {
257 257
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
258 258
      } else {
259 259
        nodes[arcs[n].source].first_out = arcs[n].next_out;
260 260
      }
261 261

	
262 262
      arcs[n].next_in = first_free_arc;
263 263
      first_free_arc = n;
264 264
      arcs[n].prev_in = -2;
265 265
    }
266 266

	
267 267
    void clear() {
268 268
      arcs.clear();
269 269
      nodes.clear();
270 270
      first_node = first_free_node = first_free_arc = -1;
271 271
    }
272 272

	
273 273
  protected:
274 274
    void changeTarget(Arc e, Node n)
275 275
    {
276 276
      if(arcs[e.id].next_in != -1)
277 277
        arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in;
278 278
      if(arcs[e.id].prev_in != -1)
279 279
        arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in;
280 280
      else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in;
281 281
      if (nodes[n.id].first_in != -1) {
282 282
        arcs[nodes[n.id].first_in].prev_in = e.id;
283 283
      }
284 284
      arcs[e.id].target = n.id;
285 285
      arcs[e.id].prev_in = -1;
286 286
      arcs[e.id].next_in = nodes[n.id].first_in;
287 287
      nodes[n.id].first_in = e.id;
288 288
    }
289 289
    void changeSource(Arc e, Node n)
290 290
    {
291 291
      if(arcs[e.id].next_out != -1)
292 292
        arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out;
293 293
      if(arcs[e.id].prev_out != -1)
294 294
        arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out;
295 295
      else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out;
296 296
      if (nodes[n.id].first_out != -1) {
297 297
        arcs[nodes[n.id].first_out].prev_out = e.id;
298 298
      }
299 299
      arcs[e.id].source = n.id;
300 300
      arcs[e.id].prev_out = -1;
301 301
      arcs[e.id].next_out = nodes[n.id].first_out;
302 302
      nodes[n.id].first_out = e.id;
303 303
    }
304 304

	
305 305
  };
306 306

	
307 307
  typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase;
308 308

	
309 309
  /// \addtogroup graphs
310 310
  /// @{
311 311

	
312 312
  ///A general directed graph structure.
313 313

	
314 314
  ///\ref ListDigraph is a simple and fast <em>directed graph</em>
315 315
  ///implementation based on static linked lists that are stored in
316 316
  ///\c std::vector structures.
317 317
  ///
318 318
  ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
319 319
  ///also provides several useful additional functionalities.
320 320
  ///Most of the member functions and nested classes are documented
321 321
  ///only in the concept class.
322 322
  ///
323 323
  ///\sa concepts::Digraph
324 324

	
325 325
  class ListDigraph : public ExtendedListDigraphBase {
326
    typedef ExtendedListDigraphBase Parent;
327

	
326 328
  private:
327 329
    ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
328 330

	
329 331
    ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
330 332
    ///
331 333
    ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
332 334
    ///\brief Assignment of ListDigraph to another one is \e not allowed.
333 335
    ///Use copyDigraph() instead.
334 336

	
335 337
    ///Assignment of ListDigraph to another one is \e not allowed.
336 338
    ///Use copyDigraph() instead.
337 339
    void operator=(const ListDigraph &) {}
338 340
  public:
339 341

	
340
    typedef ExtendedListDigraphBase Parent;
341

	
342 342
    /// Constructor
343 343

	
344 344
    /// Constructor.
345 345
    ///
346 346
    ListDigraph() {}
347 347

	
348 348
    ///Add a new node to the digraph.
349 349

	
350 350
    ///Add a new node to the digraph.
351 351
    ///\return The new node.
352 352
    Node addNode() { return Parent::addNode(); }
353 353

	
354 354
    ///Add a new arc to the digraph.
355 355

	
356 356
    ///Add a new arc to the digraph with source node \c s
357 357
    ///and target node \c t.
358 358
    ///\return The new arc.
359 359
    Arc addArc(const Node& s, const Node& t) {
360 360
      return Parent::addArc(s, t);
361 361
    }
362 362

	
363 363
    ///\brief Erase a node from the digraph.
364 364
    ///
365 365
    ///Erase a node from the digraph.
366 366
    ///
367 367
    void erase(const Node& n) { Parent::erase(n); }
368 368

	
369 369
    ///\brief Erase an arc from the digraph.
370 370
    ///
371 371
    ///Erase an arc from the digraph.
372 372
    ///
373 373
    void erase(const Arc& a) { Parent::erase(a); }
374 374

	
375 375
    /// Node validity check
376 376

	
377 377
    /// This function gives back true if the given node is valid,
378 378
    /// ie. it is a real node of the graph.
379 379
    ///
380 380
    /// \warning A Node pointing to a removed item
381 381
    /// could become valid again later if new nodes are
382 382
    /// added to the graph.
383 383
    bool valid(Node n) const { return Parent::valid(n); }
384 384

	
385 385
    /// Arc validity check
386 386

	
387 387
    /// This function gives back true if the given arc is valid,
388 388
    /// ie. it is a real arc of the graph.
389 389
    ///
390 390
    /// \warning An Arc pointing to a removed item
391 391
    /// could become valid again later if new nodes are
392 392
    /// added to the graph.
393 393
    bool valid(Arc a) const { return Parent::valid(a); }
394 394

	
395 395
    /// Change the target of \c a to \c n
396 396

	
397 397
    /// Change the target of \c a to \c n
398 398
    ///
399 399
    ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
400 400
    ///the changed arc remain valid. However <tt>InArcIt</tt>s are
401 401
    ///invalidated.
402 402
    ///
403 403
    ///\warning This functionality cannot be used together with the Snapshot
404 404
    ///feature.
405 405
    void changeTarget(Arc a, Node n) {
406 406
      Parent::changeTarget(a,n);
407 407
    }
408 408
    /// Change the source of \c a to \c n
409 409

	
410 410
    /// Change the source of \c a to \c n
411 411
    ///
412 412
    ///\note The <tt>InArcIt</tt>s referencing the changed arc remain
413 413
    ///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are
414 414
    ///invalidated.
415 415
    ///
416 416
    ///\warning This functionality cannot be used together with the Snapshot
417 417
    ///feature.
418 418
    void changeSource(Arc a, Node n) {
419 419
      Parent::changeSource(a,n);
420 420
    }
421 421

	
422 422
    /// Invert the direction of an arc.
423 423

	
424 424
    ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
425 425
    ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
426 426
    ///invalidated.
427 427
    ///
428 428
    ///\warning This functionality cannot be used together with the Snapshot
429 429
    ///feature.
430 430
    void reverseArc(Arc e) {
431 431
      Node t=target(e);
432 432
      changeTarget(e,source(e));
433 433
      changeSource(e,t);
434 434
    }
435 435

	
436 436
    /// Reserve memory for nodes.
437 437

	
438 438
    /// Using this function it is possible to avoid the superfluous memory
439 439
    /// allocation: if you know that the digraph you want to build will
440 440
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
441 441
    /// then it is worth reserving space for this amount before starting
442 442
    /// to build the digraph.
443 443
    /// \sa reserveArc
444 444
    void reserveNode(int n) { nodes.reserve(n); };
445 445

	
446 446
    /// Reserve memory for arcs.
447 447

	
448 448
    /// Using this function it is possible to avoid the superfluous memory
449 449
    /// allocation: if you know that the digraph you want to build will
450 450
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
451 451
    /// then it is worth reserving space for this amount before starting
452 452
    /// to build the digraph.
453 453
    /// \sa reserveNode
454 454
    void reserveArc(int m) { arcs.reserve(m); };
455 455

	
456 456
    ///Contract two nodes.
457 457

	
458 458
    ///This function contracts two nodes.
459 459
    ///Node \p b will be removed but instead of deleting
460 460
    ///incident arcs, they will be joined to \p a.
461 461
    ///The last parameter \p r controls whether to remove loops. \c true
462 462
    ///means that loops will be removed.
463 463
    ///
464 464
    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
465 465
    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
466 466
    ///may be invalidated.
467 467
    ///
468 468
    ///\warning This functionality cannot be used together with the Snapshot
469 469
    ///feature.
470 470
    void contract(Node a, Node b, bool r = true)
471 471
    {
472 472
      for(OutArcIt e(*this,b);e!=INVALID;) {
473 473
        OutArcIt f=e;
474 474
        ++f;
475 475
        if(r && target(e)==a) erase(e);
476 476
        else changeSource(e,a);
477 477
        e=f;
478 478
      }
479 479
      for(InArcIt e(*this,b);e!=INVALID;) {
480 480
        InArcIt f=e;
481 481
        ++f;
482 482
        if(r && source(e)==a) erase(e);
483 483
        else changeTarget(e,a);
484 484
        e=f;
485 485
      }
486 486
      erase(b);
487 487
    }
488 488

	
489 489
    ///Split a node.
490 490

	
491 491
    ///This function splits a node. First a new node is added to the digraph,
492 492
    ///then the source of each outgoing arc of \c n is moved to this new node.
493 493
    ///If \c connect is \c true (this is the default value), then a new arc
494 494
    ///from \c n to the newly created node is also added.
495 495
    ///\return The newly created node.
496 496
    ///
497 497
    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
498 498
    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
499 499
    ///be invalidated.
500 500
    ///
501 501
    ///\warning This functionality cannot be used in conjunction with the
502 502
    ///Snapshot feature.
503 503
    Node split(Node n, bool connect = true) {
504 504
      Node b = addNode();
505 505
      for(OutArcIt e(*this,n);e!=INVALID;) {
506 506
        OutArcIt f=e;
507 507
        ++f;
508 508
        changeSource(e,b);
509 509
        e=f;
510 510
      }
511 511
      if (connect) addArc(n,b);
512 512
      return b;
513 513
    }
514 514

	
515 515
    ///Split an arc.
516 516

	
517 517
    ///This function splits an arc. First a new node \c b is added to
518 518
    ///the digraph, then the original arc is re-targeted to \c
519 519
    ///b. Finally an arc from \c b to the original target is added.
520 520
    ///
521 521
    ///\return The newly created node.
522 522
    ///
523 523
    ///\warning This functionality cannot be used together with the
524 524
    ///Snapshot feature.
525 525
    Node split(Arc e) {
526 526
      Node b = addNode();
527 527
      addArc(b,target(e));
528 528
      changeTarget(e,b);
529 529
      return b;
530 530
    }
531 531

	
532 532
    /// \brief Class to make a snapshot of the digraph and restore
533 533
    /// it later.
534 534
    ///
535 535
    /// Class to make a snapshot of the digraph and restore it later.
536 536
    ///
537 537
    /// The newly added nodes and arcs can be removed using the
538 538
    /// restore() function.
539 539
    ///
540 540
    /// \warning Arc and node deletions and other modifications (e.g.
541 541
    /// contracting, splitting, reversing arcs or nodes) cannot be
542 542
    /// restored. These events invalidate the snapshot.
543 543
    class Snapshot {
544 544
    protected:
545 545

	
546 546
      typedef Parent::NodeNotifier NodeNotifier;
547 547

	
548 548
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
549 549
      public:
550 550

	
551 551
        NodeObserverProxy(Snapshot& _snapshot)
552 552
          : snapshot(_snapshot) {}
553 553

	
554 554
        using NodeNotifier::ObserverBase::attach;
555 555
        using NodeNotifier::ObserverBase::detach;
556 556
        using NodeNotifier::ObserverBase::attached;
557 557

	
558 558
      protected:
559 559

	
560 560
        virtual void add(const Node& node) {
561 561
          snapshot.addNode(node);
562 562
        }
563 563
        virtual void add(const std::vector<Node>& nodes) {
564 564
          for (int i = nodes.size() - 1; i >= 0; ++i) {
565 565
            snapshot.addNode(nodes[i]);
566 566
          }
567 567
        }
568 568
        virtual void erase(const Node& node) {
569 569
          snapshot.eraseNode(node);
570 570
        }
571 571
        virtual void erase(const std::vector<Node>& nodes) {
572 572
          for (int i = 0; i < int(nodes.size()); ++i) {
573 573
            snapshot.eraseNode(nodes[i]);
574 574
          }
575 575
        }
576 576
        virtual void build() {
577 577
          Node node;
578 578
          std::vector<Node> nodes;
579 579
          for (notifier()->first(node); node != INVALID;
580 580
               notifier()->next(node)) {
581 581
            nodes.push_back(node);
582 582
          }
583 583
          for (int i = nodes.size() - 1; i >= 0; --i) {
584 584
            snapshot.addNode(nodes[i]);
585 585
          }
586 586
        }
587 587
        virtual void clear() {
588 588
          Node node;
589 589
          for (notifier()->first(node); node != INVALID;
590 590
               notifier()->next(node)) {
591 591
            snapshot.eraseNode(node);
592 592
          }
593 593
        }
594 594

	
595 595
        Snapshot& snapshot;
596 596
      };
597 597

	
598 598
      class ArcObserverProxy : public ArcNotifier::ObserverBase {
599 599
      public:
600 600

	
601 601
        ArcObserverProxy(Snapshot& _snapshot)
602 602
          : snapshot(_snapshot) {}
603 603

	
604 604
        using ArcNotifier::ObserverBase::attach;
605 605
        using ArcNotifier::ObserverBase::detach;
606 606
        using ArcNotifier::ObserverBase::attached;
607 607

	
608 608
      protected:
609 609

	
610 610
        virtual void add(const Arc& arc) {
611 611
          snapshot.addArc(arc);
612 612
        }
613 613
        virtual void add(const std::vector<Arc>& arcs) {
614 614
          for (int i = arcs.size() - 1; i >= 0; ++i) {
615 615
            snapshot.addArc(arcs[i]);
616 616
          }
617 617
        }
618 618
        virtual void erase(const Arc& arc) {
619 619
          snapshot.eraseArc(arc);
620 620
        }
621 621
        virtual void erase(const std::vector<Arc>& arcs) {
622 622
          for (int i = 0; i < int(arcs.size()); ++i) {
623 623
            snapshot.eraseArc(arcs[i]);
624 624
          }
625 625
        }
626 626
        virtual void build() {
627 627
          Arc arc;
628 628
          std::vector<Arc> arcs;
629 629
          for (notifier()->first(arc); arc != INVALID;
630 630
               notifier()->next(arc)) {
631 631
            arcs.push_back(arc);
632 632
          }
633 633
          for (int i = arcs.size() - 1; i >= 0; --i) {
634 634
            snapshot.addArc(arcs[i]);
635 635
          }
636 636
        }
637 637
        virtual void clear() {
638 638
          Arc arc;
639 639
          for (notifier()->first(arc); arc != INVALID;
640 640
               notifier()->next(arc)) {
641 641
            snapshot.eraseArc(arc);
642 642
          }
643 643
        }
644 644

	
645 645
        Snapshot& snapshot;
646 646
      };
647 647

	
648 648
      ListDigraph *digraph;
649 649

	
650 650
      NodeObserverProxy node_observer_proxy;
651 651
      ArcObserverProxy arc_observer_proxy;
652 652

	
653 653
      std::list<Node> added_nodes;
654 654
      std::list<Arc> added_arcs;
655 655

	
656 656

	
657 657
      void addNode(const Node& node) {
658 658
        added_nodes.push_front(node);
659 659
      }
660 660
      void eraseNode(const Node& node) {
661 661
        std::list<Node>::iterator it =
662 662
          std::find(added_nodes.begin(), added_nodes.end(), node);
663 663
        if (it == added_nodes.end()) {
664 664
          clear();
665 665
          arc_observer_proxy.detach();
666 666
          throw NodeNotifier::ImmediateDetach();
667 667
        } else {
668 668
          added_nodes.erase(it);
669 669
        }
670 670
      }
671 671

	
672 672
      void addArc(const Arc& arc) {
673 673
        added_arcs.push_front(arc);
674 674
      }
675 675
      void eraseArc(const Arc& arc) {
676 676
        std::list<Arc>::iterator it =
677 677
          std::find(added_arcs.begin(), added_arcs.end(), arc);
678 678
        if (it == added_arcs.end()) {
679 679
          clear();
680 680
          node_observer_proxy.detach();
681 681
          throw ArcNotifier::ImmediateDetach();
682 682
        } else {
683 683
          added_arcs.erase(it);
684 684
        }
685 685
      }
686 686

	
687 687
      void attach(ListDigraph &_digraph) {
688 688
        digraph = &_digraph;
689 689
        node_observer_proxy.attach(digraph->notifier(Node()));
690 690
        arc_observer_proxy.attach(digraph->notifier(Arc()));
691 691
      }
692 692

	
693 693
      void detach() {
694 694
        node_observer_proxy.detach();
695 695
        arc_observer_proxy.detach();
696 696
      }
697 697

	
698 698
      bool attached() const {
699 699
        return node_observer_proxy.attached();
700 700
      }
701 701

	
702 702
      void clear() {
703 703
        added_nodes.clear();
704 704
        added_arcs.clear();
705 705
      }
706 706

	
707 707
    public:
708 708

	
709 709
      /// \brief Default constructor.
710 710
      ///
711 711
      /// Default constructor.
712 712
      /// To actually make a snapshot you must call save().
713 713
      Snapshot()
714 714
        : digraph(0), node_observer_proxy(*this),
715 715
          arc_observer_proxy(*this) {}
716 716

	
717 717
      /// \brief Constructor that immediately makes a snapshot.
718 718
      ///
719 719
      /// This constructor immediately makes a snapshot of the digraph.
720 720
      /// \param _digraph The digraph we make a snapshot of.
721 721
      Snapshot(ListDigraph &_digraph)
722 722
        : node_observer_proxy(*this),
723 723
          arc_observer_proxy(*this) {
724 724
        attach(_digraph);
725 725
      }
726 726

	
727 727
      /// \brief Make a snapshot.
728 728
      ///
729 729
      /// Make a snapshot of the digraph.
730 730
      ///
731 731
      /// This function can be called more than once. In case of a repeated
732 732
      /// call, the previous snapshot gets lost.
733 733
      /// \param _digraph The digraph we make the snapshot of.
734 734
      void save(ListDigraph &_digraph) {
735 735
        if (attached()) {
736 736
          detach();
737 737
          clear();
738 738
        }
739 739
        attach(_digraph);
740 740
      }
741 741

	
742 742
      /// \brief Undo the changes until the last snapshot.
743 743
      //
744 744
      /// Undo the changes until the last snapshot created by save().
745 745
      void restore() {
746 746
        detach();
747 747
        for(std::list<Arc>::iterator it = added_arcs.begin();
748 748
            it != added_arcs.end(); ++it) {
749 749
          digraph->erase(*it);
750 750
        }
751 751
        for(std::list<Node>::iterator it = added_nodes.begin();
752 752
            it != added_nodes.end(); ++it) {
753 753
          digraph->erase(*it);
754 754
        }
755 755
        clear();
756 756
      }
757 757

	
758 758
      /// \brief Gives back true when the snapshot is valid.
759 759
      ///
760 760
      /// Gives back true when the snapshot is valid.
761 761
      bool valid() const {
762 762
        return attached();
763 763
      }
764 764
    };
765 765

	
766 766
  };
767 767

	
768 768
  ///@}
769 769

	
770 770
  class ListGraphBase {
771 771

	
772 772
  protected:
773 773

	
774 774
    struct NodeT {
775 775
      int first_out;
776 776
      int prev, next;
777 777
    };
778 778

	
779 779
    struct ArcT {
780 780
      int target;
781 781
      int prev_out, next_out;
782 782
    };
783 783

	
784 784
    std::vector<NodeT> nodes;
785 785

	
786 786
    int first_node;
787 787

	
788 788
    int first_free_node;
789 789

	
790 790
    std::vector<ArcT> arcs;
791 791

	
792 792
    int first_free_arc;
793 793

	
794 794
  public:
795 795

	
796
    typedef ListGraphBase Digraph;
796
    typedef ListGraphBase Graph;
797 797

	
798 798
    class Node;
799 799
    class Arc;
800 800
    class Edge;
801 801

	
802 802
    class Node {
803 803
      friend class ListGraphBase;
804 804
    protected:
805 805

	
806 806
      int id;
807 807
      explicit Node(int pid) { id = pid;}
808 808

	
809 809
    public:
810 810
      Node() {}
811 811
      Node (Invalid) { id = -1; }
812 812
      bool operator==(const Node& node) const {return id == node.id;}
813 813
      bool operator!=(const Node& node) const {return id != node.id;}
814 814
      bool operator<(const Node& node) const {return id < node.id;}
815 815
    };
816 816

	
817 817
    class Edge {
818 818
      friend class ListGraphBase;
819 819
    protected:
820 820

	
821 821
      int id;
822 822
      explicit Edge(int pid) { id = pid;}
823 823

	
824 824
    public:
825 825
      Edge() {}
826 826
      Edge (Invalid) { id = -1; }
827 827
      bool operator==(const Edge& edge) const {return id == edge.id;}
828 828
      bool operator!=(const Edge& edge) const {return id != edge.id;}
829 829
      bool operator<(const Edge& edge) const {return id < edge.id;}
830 830
    };
831 831

	
832 832
    class Arc {
833 833
      friend class ListGraphBase;
834 834
    protected:
835 835

	
836 836
      int id;
837 837
      explicit Arc(int pid) { id = pid;}
838 838

	
839 839
    public:
840 840
      operator Edge() const {
841 841
        return id != -1 ? edgeFromId(id / 2) : INVALID;
842 842
      }
843 843

	
844 844
      Arc() {}
845 845
      Arc (Invalid) { id = -1; }
846 846
      bool operator==(const Arc& arc) const {return id == arc.id;}
847 847
      bool operator!=(const Arc& arc) const {return id != arc.id;}
848 848
      bool operator<(const Arc& arc) const {return id < arc.id;}
849 849
    };
850 850

	
851 851

	
852 852

	
853 853
    ListGraphBase()
854 854
      : nodes(), first_node(-1),
855 855
        first_free_node(-1), arcs(), first_free_arc(-1) {}
856 856

	
857 857

	
858 858
    int maxNodeId() const { return nodes.size()-1; }
859 859
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
860 860
    int maxArcId() const { return arcs.size()-1; }
861 861

	
862 862
    Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
863 863
    Node target(Arc e) const { return Node(arcs[e.id].target); }
864 864

	
865 865
    Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
866 866
    Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
867 867

	
868 868
    static bool direction(Arc e) {
869 869
      return (e.id & 1) == 1;
870 870
    }
871 871

	
872 872
    static Arc direct(Edge e, bool d) {
873 873
      return Arc(e.id * 2 + (d ? 1 : 0));
874 874
    }
875 875

	
876 876
    void first(Node& node) const {
877 877
      node.id = first_node;
878 878
    }
879 879

	
880 880
    void next(Node& node) const {
881 881
      node.id = nodes[node.id].next;
882 882
    }
883 883

	
884 884
    void first(Arc& e) const {
885 885
      int n = first_node;
886 886
      while (n != -1 && nodes[n].first_out == -1) {
887 887
        n = nodes[n].next;
888 888
      }
889 889
      e.id = (n == -1) ? -1 : nodes[n].first_out;
890 890
    }
891 891

	
892 892
    void next(Arc& e) const {
893 893
      if (arcs[e.id].next_out != -1) {
894 894
        e.id = arcs[e.id].next_out;
895 895
      } else {
896 896
        int n = nodes[arcs[e.id ^ 1].target].next;
897 897
        while(n != -1 && nodes[n].first_out == -1) {
898 898
          n = nodes[n].next;
899 899
        }
900 900
        e.id = (n == -1) ? -1 : nodes[n].first_out;
901 901
      }
902 902
    }
903 903

	
904 904
    void first(Edge& e) const {
905 905
      int n = first_node;
906 906
      while (n != -1) {
907 907
        e.id = nodes[n].first_out;
908 908
        while ((e.id & 1) != 1) {
909 909
          e.id = arcs[e.id].next_out;
910 910
        }
911 911
        if (e.id != -1) {
912 912
          e.id /= 2;
913 913
          return;
914 914
        }
915 915
        n = nodes[n].next;
916 916
      }
917 917
      e.id = -1;
918 918
    }
919 919

	
920 920
    void next(Edge& e) const {
921 921
      int n = arcs[e.id * 2].target;
922 922
      e.id = arcs[(e.id * 2) | 1].next_out;
923 923
      while ((e.id & 1) != 1) {
924 924
        e.id = arcs[e.id].next_out;
925 925
      }
926 926
      if (e.id != -1) {
927 927
        e.id /= 2;
928 928
        return;
929 929
      }
930 930
      n = nodes[n].next;
931 931
      while (n != -1) {
932 932
        e.id = nodes[n].first_out;
933 933
        while ((e.id & 1) != 1) {
934 934
          e.id = arcs[e.id].next_out;
935 935
        }
936 936
        if (e.id != -1) {
937 937
          e.id /= 2;
938 938
          return;
939 939
        }
940 940
        n = nodes[n].next;
941 941
      }
942 942
      e.id = -1;
943 943
    }
944 944

	
945 945
    void firstOut(Arc &e, const Node& v) const {
946 946
      e.id = nodes[v.id].first_out;
947 947
    }
948 948
    void nextOut(Arc &e) const {
949 949
      e.id = arcs[e.id].next_out;
950 950
    }
951 951

	
952 952
    void firstIn(Arc &e, const Node& v) const {
953 953
      e.id = ((nodes[v.id].first_out) ^ 1);
954 954
      if (e.id == -2) e.id = -1;
955 955
    }
956 956
    void nextIn(Arc &e) const {
957 957
      e.id = ((arcs[e.id ^ 1].next_out) ^ 1);
958 958
      if (e.id == -2) e.id = -1;
959 959
    }
960 960

	
961 961
    void firstInc(Edge &e, bool& d, const Node& v) const {
962 962
      int a = nodes[v.id].first_out;
963 963
      if (a != -1 ) {
964 964
        e.id = a / 2;
965 965
        d = ((a & 1) == 1);
966 966
      } else {
967 967
        e.id = -1;
968 968
        d = true;
969 969
      }
970 970
    }
971 971
    void nextInc(Edge &e, bool& d) const {
972 972
      int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out);
973 973
      if (a != -1 ) {
974 974
        e.id = a / 2;
975 975
        d = ((a & 1) == 1);
976 976
      } else {
977 977
        e.id = -1;
978 978
        d = true;
979 979
      }
980 980
    }
981 981

	
982 982
    static int id(Node v) { return v.id; }
983 983
    static int id(Arc e) { return e.id; }
984 984
    static int id(Edge e) { return e.id; }
985 985

	
986 986
    static Node nodeFromId(int id) { return Node(id);}
987 987
    static Arc arcFromId(int id) { return Arc(id);}
988 988
    static Edge edgeFromId(int id) { return Edge(id);}
989 989

	
990 990
    bool valid(Node n) const {
991 991
      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
992 992
        nodes[n.id].prev != -2;
993 993
    }
994 994

	
995 995
    bool valid(Arc a) const {
996 996
      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
997 997
        arcs[a.id].prev_out != -2;
998 998
    }
999 999

	
1000 1000
    bool valid(Edge e) const {
1001 1001
      return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) &&
1002 1002
        arcs[2 * e.id].prev_out != -2;
1003 1003
    }
1004 1004

	
1005 1005
    Node addNode() {
1006 1006
      int n;
1007 1007

	
1008 1008
      if(first_free_node==-1) {
1009 1009
        n = nodes.size();
1010 1010
        nodes.push_back(NodeT());
1011 1011
      } else {
1012 1012
        n = first_free_node;
1013 1013
        first_free_node = nodes[n].next;
1014 1014
      }
1015 1015

	
1016 1016
      nodes[n].next = first_node;
1017 1017
      if (first_node != -1) nodes[first_node].prev = n;
1018 1018
      first_node = n;
1019 1019
      nodes[n].prev = -1;
1020 1020

	
1021 1021
      nodes[n].first_out = -1;
1022 1022

	
1023 1023
      return Node(n);
1024 1024
    }
1025 1025

	
1026 1026
    Edge addEdge(Node u, Node v) {
1027 1027
      int n;
1028 1028

	
1029 1029
      if (first_free_arc == -1) {
1030 1030
        n = arcs.size();
1031 1031
        arcs.push_back(ArcT());
1032 1032
        arcs.push_back(ArcT());
1033 1033
      } else {
1034 1034
        n = first_free_arc;
1035 1035
        first_free_arc = arcs[n].next_out;
1036 1036
      }
1037 1037

	
1038 1038
      arcs[n].target = u.id;
1039 1039
      arcs[n | 1].target = v.id;
1040 1040

	
1041 1041
      arcs[n].next_out = nodes[v.id].first_out;
1042 1042
      if (nodes[v.id].first_out != -1) {
1043 1043
        arcs[nodes[v.id].first_out].prev_out = n;
1044 1044
      }
1045 1045
      arcs[n].prev_out = -1;
1046 1046
      nodes[v.id].first_out = n;
1047 1047

	
1048 1048
      arcs[n | 1].next_out = nodes[u.id].first_out;
1049 1049
      if (nodes[u.id].first_out != -1) {
1050 1050
        arcs[nodes[u.id].first_out].prev_out = (n | 1);
1051 1051
      }
1052 1052
      arcs[n | 1].prev_out = -1;
1053 1053
      nodes[u.id].first_out = (n | 1);
1054 1054

	
1055 1055
      return Edge(n / 2);
1056 1056
    }
1057 1057

	
1058 1058
    void erase(const Node& node) {
1059 1059
      int n = node.id;
1060 1060

	
1061 1061
      if(nodes[n].next != -1) {
1062 1062
        nodes[nodes[n].next].prev = nodes[n].prev;
1063 1063
      }
1064 1064

	
1065 1065
      if(nodes[n].prev != -1) {
1066 1066
        nodes[nodes[n].prev].next = nodes[n].next;
1067 1067
      } else {
1068 1068
        first_node = nodes[n].next;
1069 1069
      }
1070 1070

	
1071 1071
      nodes[n].next = first_free_node;
1072 1072
      first_free_node = n;
1073 1073
      nodes[n].prev = -2;
1074 1074
    }
1075 1075

	
1076 1076
    void erase(const Edge& edge) {
1077 1077
      int n = edge.id * 2;
1078 1078

	
1079 1079
      if (arcs[n].next_out != -1) {
1080 1080
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
1081 1081
      }
1082 1082

	
1083 1083
      if (arcs[n].prev_out != -1) {
1084 1084
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
1085 1085
      } else {
1086 1086
        nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
1087 1087
      }
1088 1088

	
1089 1089
      if (arcs[n | 1].next_out != -1) {
1090 1090
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
1091 1091
      }
1092 1092

	
1093 1093
      if (arcs[n | 1].prev_out != -1) {
1094 1094
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
1095 1095
      } else {
1096 1096
        nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
1097 1097
      }
1098 1098

	
1099 1099
      arcs[n].next_out = first_free_arc;
1100 1100
      first_free_arc = n;
1101 1101
      arcs[n].prev_out = -2;
1102 1102
      arcs[n | 1].prev_out = -2;
1103 1103

	
1104 1104
    }
1105 1105

	
1106 1106
    void clear() {
1107 1107
      arcs.clear();
1108 1108
      nodes.clear();
1109 1109
      first_node = first_free_node = first_free_arc = -1;
1110 1110
    }
1111 1111

	
1112 1112
  protected:
1113 1113

	
1114 1114
    void changeV(Edge e, Node n) {
1115 1115
      if(arcs[2 * e.id].next_out != -1) {
1116 1116
        arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out;
1117 1117
      }
1118 1118
      if(arcs[2 * e.id].prev_out != -1) {
1119 1119
        arcs[arcs[2 * e.id].prev_out].next_out =
1120 1120
          arcs[2 * e.id].next_out;
1121 1121
      } else {
1122 1122
        nodes[arcs[(2 * e.id) | 1].target].first_out =
1123 1123
          arcs[2 * e.id].next_out;
1124 1124
      }
1125 1125

	
1126 1126
      if (nodes[n.id].first_out != -1) {
1127 1127
        arcs[nodes[n.id].first_out].prev_out = 2 * e.id;
1128 1128
      }
1129 1129
      arcs[(2 * e.id) | 1].target = n.id;
1130 1130
      arcs[2 * e.id].prev_out = -1;
1131 1131
      arcs[2 * e.id].next_out = nodes[n.id].first_out;
1132 1132
      nodes[n.id].first_out = 2 * e.id;
1133 1133
    }
1134 1134

	
1135 1135
    void changeU(Edge e, Node n) {
1136 1136
      if(arcs[(2 * e.id) | 1].next_out != -1) {
1137 1137
        arcs[arcs[(2 * e.id) | 1].next_out].prev_out =
1138 1138
          arcs[(2 * e.id) | 1].prev_out;
1139 1139
      }
1140 1140
      if(arcs[(2 * e.id) | 1].prev_out != -1) {
1141 1141
        arcs[arcs[(2 * e.id) | 1].prev_out].next_out =
1142 1142
          arcs[(2 * e.id) | 1].next_out;
1143 1143
      } else {
1144 1144
        nodes[arcs[2 * e.id].target].first_out =
1145 1145
          arcs[(2 * e.id) | 1].next_out;
1146 1146
      }
1147 1147

	
1148 1148
      if (nodes[n.id].first_out != -1) {
1149 1149
        arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
1150 1150
      }
1151 1151
      arcs[2 * e.id].target = n.id;
1152 1152
      arcs[(2 * e.id) | 1].prev_out = -1;
1153 1153
      arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
1154 1154
      nodes[n.id].first_out = ((2 * e.id) | 1);
1155 1155
    }
1156 1156

	
1157 1157
  };
1158 1158

	
1159 1159
  typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
1160 1160

	
1161 1161

	
1162 1162
  /// \addtogroup graphs
1163 1163
  /// @{
1164 1164

	
1165 1165
  ///A general undirected graph structure.
1166 1166

	
1167 1167
  ///\ref ListGraph is a simple and fast <em>undirected graph</em>
1168 1168
  ///implementation based on static linked lists that are stored in
1169 1169
  ///\c std::vector structures.
1170 1170
  ///
1171 1171
  ///It conforms to the \ref concepts::Graph "Graph concept" and it
1172 1172
  ///also provides several useful additional functionalities.
1173 1173
  ///Most of the member functions and nested classes are documented
1174 1174
  ///only in the concept class.
1175 1175
  ///
1176 1176
  ///\sa concepts::Graph
1177 1177

	
1178 1178
  class ListGraph : public ExtendedListGraphBase {
1179
    typedef ExtendedListGraphBase Parent;
1180

	
1179 1181
  private:
1180 1182
    ///ListGraph is \e not copy constructible. Use copyGraph() instead.
1181 1183

	
1182 1184
    ///ListGraph is \e not copy constructible. Use copyGraph() instead.
1183 1185
    ///
1184 1186
    ListGraph(const ListGraph &) :ExtendedListGraphBase()  {};
1185 1187
    ///\brief Assignment of ListGraph to another one is \e not allowed.
1186 1188
    ///Use copyGraph() instead.
1187 1189

	
1188 1190
    ///Assignment of ListGraph to another one is \e not allowed.
1189 1191
    ///Use copyGraph() instead.
1190 1192
    void operator=(const ListGraph &) {}
1191 1193
  public:
1192 1194
    /// Constructor
1193 1195

	
1194 1196
    /// Constructor.
1195 1197
    ///
1196 1198
    ListGraph() {}
1197 1199

	
1198
    typedef ExtendedListGraphBase Parent;
1199

	
1200 1200
    typedef Parent::OutArcIt IncEdgeIt;
1201 1201

	
1202 1202
    /// \brief Add a new node to the graph.
1203 1203
    ///
1204 1204
    /// Add a new node to the graph.
1205 1205
    /// \return The new node.
1206 1206
    Node addNode() { return Parent::addNode(); }
1207 1207

	
1208 1208
    /// \brief Add a new edge to the graph.
1209 1209
    ///
1210 1210
    /// Add a new edge to the graph with source node \c s
1211 1211
    /// and target node \c t.
1212 1212
    /// \return The new edge.
1213 1213
    Edge addEdge(const Node& s, const Node& t) {
1214 1214
      return Parent::addEdge(s, t);
1215 1215
    }
1216 1216

	
1217 1217
    /// \brief Erase a node from the graph.
1218 1218
    ///
1219 1219
    /// Erase a node from the graph.
1220 1220
    ///
1221 1221
    void erase(const Node& n) { Parent::erase(n); }
1222 1222

	
1223 1223
    /// \brief Erase an edge from the graph.
1224 1224
    ///
1225 1225
    /// Erase an edge from the graph.
1226 1226
    ///
1227 1227
    void erase(const Edge& e) { Parent::erase(e); }
1228 1228
    /// Node validity check
1229 1229

	
1230 1230
    /// This function gives back true if the given node is valid,
1231 1231
    /// ie. it is a real node of the graph.
1232 1232
    ///
1233 1233
    /// \warning A Node pointing to a removed item
1234 1234
    /// could become valid again later if new nodes are
1235 1235
    /// added to the graph.
1236 1236
    bool valid(Node n) const { return Parent::valid(n); }
1237 1237
    /// Arc validity check
1238 1238

	
1239 1239
    /// This function gives back true if the given arc is valid,
1240 1240
    /// ie. it is a real arc of the graph.
1241 1241
    ///
1242 1242
    /// \warning An Arc pointing to a removed item
1243 1243
    /// could become valid again later if new edges are
1244 1244
    /// added to the graph.
1245 1245
    bool valid(Arc a) const { return Parent::valid(a); }
1246 1246
    /// Edge validity check
1247 1247

	
1248 1248
    /// This function gives back true if the given edge is valid,
1249 1249
    /// ie. it is a real arc of the graph.
1250 1250
    ///
1251 1251
    /// \warning A Edge pointing to a removed item
1252 1252
    /// could become valid again later if new edges are
1253 1253
    /// added to the graph.
1254 1254
    bool valid(Edge e) const { return Parent::valid(e); }
1255 1255
    /// \brief Change the end \c u of \c e to \c n
1256 1256
    ///
1257 1257
    /// This function changes the end \c u of \c e to node \c n.
1258 1258
    ///
1259 1259
    ///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the
1260 1260
    ///changed edge are invalidated and if the changed node is the
1261 1261
    ///base node of an iterator then this iterator is also
1262 1262
    ///invalidated.
1263 1263
    ///
1264 1264
    ///\warning This functionality cannot be used together with the
1265 1265
    ///Snapshot feature.
1266 1266
    void changeU(Edge e, Node n) {
1267 1267
      Parent::changeU(e,n);
1268 1268
    }
1269 1269
    /// \brief Change the end \c v of \c e to \c n
1270 1270
    ///
1271 1271
    /// This function changes the end \c v of \c e to \c n.
1272 1272
    ///
1273 1273
    ///\note The <tt>EdgeIt</tt>s referencing the changed edge remain
1274 1274
    ///valid, however <tt>ArcIt</tt>s and if the changed node is the
1275 1275
    ///base node of an iterator then this iterator is invalidated.
1276 1276
    ///
1277 1277
    ///\warning This functionality cannot be used together with the
1278 1278
    ///Snapshot feature.
1279 1279
    void changeV(Edge e, Node n) {
1280 1280
      Parent::changeV(e,n);
1281 1281
    }
1282 1282
    /// \brief Contract two nodes.
1283 1283
    ///
1284 1284
    /// This function contracts two nodes.
1285 1285
    /// Node \p b will be removed but instead of deleting
1286 1286
    /// its neighboring arcs, they will be joined to \p a.
1287 1287
    /// The last parameter \p r controls whether to remove loops. \c true
1288 1288
    /// means that loops will be removed.
1289 1289
    ///
1290 1290
    /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
1291 1291
    /// valid.
1292 1292
    ///
1293 1293
    ///\warning This functionality cannot be used together with the
1294 1294
    ///Snapshot feature.
1295 1295
    void contract(Node a, Node b, bool r = true) {
1296 1296
      for(IncEdgeIt e(*this, b); e!=INVALID;) {
1297 1297
        IncEdgeIt f = e; ++f;
1298 1298
        if (r && runningNode(e) == a) {
1299 1299
          erase(e);
1300 1300
        } else if (u(e) == b) {
1301 1301
          changeU(e, a);
1302 1302
        } else {
1303 1303
          changeV(e, a);
1304 1304
        }
1305 1305
        e = f;
1306 1306
      }
1307 1307
      erase(b);
1308 1308
    }
1309 1309

	
1310 1310

	
1311 1311
    /// \brief Class to make a snapshot of the graph and restore
1312 1312
    /// it later.
1313 1313
    ///
1314 1314
    /// Class to make a snapshot of the graph and restore it later.
1315 1315
    ///
1316 1316
    /// The newly added nodes and edges can be removed
1317 1317
    /// using the restore() function.
1318 1318
    ///
1319 1319
    /// \warning Edge and node deletions and other modifications
1320 1320
    /// (e.g. changing nodes of edges, contracting nodes) cannot be
1321 1321
    /// restored. These events invalidate the snapshot.
1322 1322
    class Snapshot {
1323 1323
    protected:
1324 1324

	
1325 1325
      typedef Parent::NodeNotifier NodeNotifier;
1326 1326

	
1327 1327
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
1328 1328
      public:
1329 1329

	
1330 1330
        NodeObserverProxy(Snapshot& _snapshot)
1331 1331
          : snapshot(_snapshot) {}
1332 1332

	
1333 1333
        using NodeNotifier::ObserverBase::attach;
1334 1334
        using NodeNotifier::ObserverBase::detach;
1335 1335
        using NodeNotifier::ObserverBase::attached;
1336 1336

	
1337 1337
      protected:
1338 1338

	
1339 1339
        virtual void add(const Node& node) {
1340 1340
          snapshot.addNode(node);
1341 1341
        }
1342 1342
        virtual void add(const std::vector<Node>& nodes) {
1343 1343
          for (int i = nodes.size() - 1; i >= 0; ++i) {
1344 1344
            snapshot.addNode(nodes[i]);
1345 1345
          }
1346 1346
        }
1347 1347
        virtual void erase(const Node& node) {
1348 1348
          snapshot.eraseNode(node);
1349 1349
        }
1350 1350
        virtual void erase(const std::vector<Node>& nodes) {
1351 1351
          for (int i = 0; i < int(nodes.size()); ++i) {
1352 1352
            snapshot.eraseNode(nodes[i]);
1353 1353
          }
1354 1354
        }
1355 1355
        virtual void build() {
1356 1356
          Node node;
1357 1357
          std::vector<Node> nodes;
1358 1358
          for (notifier()->first(node); node != INVALID;
1359 1359
               notifier()->next(node)) {
1360 1360
            nodes.push_back(node);
1361 1361
          }
1362 1362
          for (int i = nodes.size() - 1; i >= 0; --i) {
1363 1363
            snapshot.addNode(nodes[i]);
1364 1364
          }
1365 1365
        }
1366 1366
        virtual void clear() {
1367 1367
          Node node;
1368 1368
          for (notifier()->first(node); node != INVALID;
1369 1369
               notifier()->next(node)) {
1370 1370
            snapshot.eraseNode(node);
1371 1371
          }
1372 1372
        }
1373 1373

	
1374 1374
        Snapshot& snapshot;
1375 1375
      };
1376 1376

	
1377 1377
      class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
1378 1378
      public:
1379 1379

	
1380 1380
        EdgeObserverProxy(Snapshot& _snapshot)
1381 1381
          : snapshot(_snapshot) {}
1382 1382

	
1383 1383
        using EdgeNotifier::ObserverBase::attach;
1384 1384
        using EdgeNotifier::ObserverBase::detach;
1385 1385
        using EdgeNotifier::ObserverBase::attached;
1386 1386

	
1387 1387
      protected:
1388 1388

	
1389 1389
        virtual void add(const Edge& edge) {
1390 1390
          snapshot.addEdge(edge);
1391 1391
        }
1392 1392
        virtual void add(const std::vector<Edge>& edges) {
1393 1393
          for (int i = edges.size() - 1; i >= 0; ++i) {
1394 1394
            snapshot.addEdge(edges[i]);
1395 1395
          }
1396 1396
        }
1397 1397
        virtual void erase(const Edge& edge) {
1398 1398
          snapshot.eraseEdge(edge);
1399 1399
        }
1400 1400
        virtual void erase(const std::vector<Edge>& edges) {
1401 1401
          for (int i = 0; i < int(edges.size()); ++i) {
1402 1402
            snapshot.eraseEdge(edges[i]);
1403 1403
          }
1404 1404
        }
1405 1405
        virtual void build() {
1406 1406
          Edge edge;
1407 1407
          std::vector<Edge> edges;
1408 1408
          for (notifier()->first(edge); edge != INVALID;
1409 1409
               notifier()->next(edge)) {
1410 1410
            edges.push_back(edge);
1411 1411
          }
1412 1412
          for (int i = edges.size() - 1; i >= 0; --i) {
1413 1413
            snapshot.addEdge(edges[i]);
1414 1414
          }
1415 1415
        }
1416 1416
        virtual void clear() {
1417 1417
          Edge edge;
1418 1418
          for (notifier()->first(edge); edge != INVALID;
1419 1419
               notifier()->next(edge)) {
1420 1420
            snapshot.eraseEdge(edge);
1421 1421
          }
1422 1422
        }
1423 1423

	
1424 1424
        Snapshot& snapshot;
1425 1425
      };
1426 1426

	
1427 1427
      ListGraph *graph;
1428 1428

	
1429 1429
      NodeObserverProxy node_observer_proxy;
1430 1430
      EdgeObserverProxy edge_observer_proxy;
1431 1431

	
1432 1432
      std::list<Node> added_nodes;
1433 1433
      std::list<Edge> added_edges;
1434 1434

	
1435 1435

	
1436 1436
      void addNode(const Node& node) {
1437 1437
        added_nodes.push_front(node);
1438 1438
      }
1439 1439
      void eraseNode(const Node& node) {
1440 1440
        std::list<Node>::iterator it =
1441 1441
          std::find(added_nodes.begin(), added_nodes.end(), node);
1442 1442
        if (it == added_nodes.end()) {
1443 1443
          clear();
1444 1444
          edge_observer_proxy.detach();
1445 1445
          throw NodeNotifier::ImmediateDetach();
1446 1446
        } else {
1447 1447
          added_nodes.erase(it);
1448 1448
        }
1449 1449
      }
1450 1450

	
1451 1451
      void addEdge(const Edge& edge) {
1452 1452
        added_edges.push_front(edge);
1453 1453
      }
1454 1454
      void eraseEdge(const Edge& edge) {
1455 1455
        std::list<Edge>::iterator it =
1456 1456
          std::find(added_edges.begin(), added_edges.end(), edge);
1457 1457
        if (it == added_edges.end()) {
1458 1458
          clear();
1459 1459
          node_observer_proxy.detach();
1460 1460
          throw EdgeNotifier::ImmediateDetach();
1461 1461
        } else {
1462 1462
          added_edges.erase(it);
1463 1463
        }
1464 1464
      }
1465 1465

	
1466 1466
      void attach(ListGraph &_graph) {
1467 1467
        graph = &_graph;
1468 1468
        node_observer_proxy.attach(graph->notifier(Node()));
1469 1469
        edge_observer_proxy.attach(graph->notifier(Edge()));
1470 1470
      }
1471 1471

	
1472 1472
      void detach() {
1473 1473
        node_observer_proxy.detach();
1474 1474
        edge_observer_proxy.detach();
1475 1475
      }
1476 1476

	
1477 1477
      bool attached() const {
1478 1478
        return node_observer_proxy.attached();
1479 1479
      }
1480 1480

	
1481 1481
      void clear() {
1482 1482
        added_nodes.clear();
1483 1483
        added_edges.clear();
1484 1484
      }
1485 1485

	
1486 1486
    public:
1487 1487

	
1488 1488
      /// \brief Default constructor.
1489 1489
      ///
1490 1490
      /// Default constructor.
1491 1491
      /// To actually make a snapshot you must call save().
1492 1492
      Snapshot()
1493 1493
        : graph(0), node_observer_proxy(*this),
1494 1494
          edge_observer_proxy(*this) {}
1495 1495

	
1496 1496
      /// \brief Constructor that immediately makes a snapshot.
1497 1497
      ///
1498 1498
      /// This constructor immediately makes a snapshot of the graph.
1499 1499
      /// \param _graph The graph we make a snapshot of.
1500 1500
      Snapshot(ListGraph &_graph)
1501 1501
        : node_observer_proxy(*this),
1502 1502
          edge_observer_proxy(*this) {
1503 1503
        attach(_graph);
1504 1504
      }
1505 1505

	
1506 1506
      /// \brief Make a snapshot.
1507 1507
      ///
1508 1508
      /// Make a snapshot of the graph.
1509 1509
      ///
1510 1510
      /// This function can be called more than once. In case of a repeated
1511 1511
      /// call, the previous snapshot gets lost.
1512 1512
      /// \param _graph The graph we make the snapshot of.
1513 1513
      void save(ListGraph &_graph) {
1514 1514
        if (attached()) {
1515 1515
          detach();
1516 1516
          clear();
1517 1517
        }
1518 1518
        attach(_graph);
1519 1519
      }
1520 1520

	
1521 1521
      /// \brief Undo the changes until the last snapshot.
1522 1522
      //
1523 1523
      /// Undo the changes until the last snapshot created by save().
1524 1524
      void restore() {
1525 1525
        detach();
1526 1526
        for(std::list<Edge>::iterator it = added_edges.begin();
1527 1527
            it != added_edges.end(); ++it) {
1528 1528
          graph->erase(*it);
1529 1529
        }
1530 1530
        for(std::list<Node>::iterator it = added_nodes.begin();
1531 1531
            it != added_nodes.end(); ++it) {
1532 1532
          graph->erase(*it);
1533 1533
        }
1534 1534
        clear();
1535 1535
      }
1536 1536

	
1537 1537
      /// \brief Gives back true when the snapshot is valid.
1538 1538
      ///
1539 1539
      /// Gives back true when the snapshot is valid.
1540 1540
      bool valid() const {
1541 1541
        return attached();
1542 1542
      }
1543 1543
    };
1544 1544
  };
1545 1545

	
1546 1546
  /// @}
1547 1547
} //namespace lemon
1548 1548

	
1549 1549

	
1550 1550
#endif
Ignore white space 6 line context
... ...
@@ -1457,1326 +1457,1331 @@
1457 1457
  ///
1458 1458
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1459 1459
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1460 1460
  /// <tt>m1[x]&&m2[x]</tt>.
1461 1461
  ///
1462 1462
  /// \relates AndMap
1463 1463
  template<typename M1, typename M2>
1464 1464
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1465 1465
    return AndMap<M1, M2>(m1,m2);
1466 1466
  }
1467 1467

	
1468 1468

	
1469 1469
  /// Logical 'or' of two maps
1470 1470

	
1471 1471
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1472 1472
  /// 'or' of the values of the two given maps.
1473 1473
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1474 1474
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1475 1475
  ///
1476 1476
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1477 1477
  /// \code
1478 1478
  ///   OrMap<M1,M2> om(m1,m2);
1479 1479
  /// \endcode
1480 1480
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1481 1481
  ///
1482 1482
  /// The simplest way of using this map is through the orMap()
1483 1483
  /// function.
1484 1484
  ///
1485 1485
  /// \sa AndMap
1486 1486
  /// \sa NotMap, NotWriteMap
1487 1487
  template<typename M1, typename M2>
1488 1488
  class OrMap : public MapBase<typename M1::Key, bool> {
1489 1489
    const M1 &_m1;
1490 1490
    const M2 &_m2;
1491 1491
  public:
1492 1492
    ///\e
1493 1493
    typedef typename M1::Key Key;
1494 1494
    ///\e
1495 1495
    typedef bool Value;
1496 1496

	
1497 1497
    /// Constructor
1498 1498
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1499 1499
    ///\e
1500 1500
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1501 1501
  };
1502 1502

	
1503 1503
  /// Returns an \c OrMap class
1504 1504

	
1505 1505
  /// This function just returns an \c OrMap class.
1506 1506
  ///
1507 1507
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1508 1508
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1509 1509
  /// <tt>m1[x]||m2[x]</tt>.
1510 1510
  ///
1511 1511
  /// \relates OrMap
1512 1512
  template<typename M1, typename M2>
1513 1513
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1514 1514
    return OrMap<M1, M2>(m1,m2);
1515 1515
  }
1516 1516

	
1517 1517

	
1518 1518
  /// Logical 'not' of a map
1519 1519

	
1520 1520
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1521 1521
  /// negation of the values of the given map.
1522 1522
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1523 1523
  ///
1524 1524
  /// The simplest way of using this map is through the notMap()
1525 1525
  /// function.
1526 1526
  ///
1527 1527
  /// \sa NotWriteMap
1528 1528
  template <typename M>
1529 1529
  class NotMap : public MapBase<typename M::Key, bool> {
1530 1530
    const M &_m;
1531 1531
  public:
1532 1532
    ///\e
1533 1533
    typedef typename M::Key Key;
1534 1534
    ///\e
1535 1535
    typedef bool Value;
1536 1536

	
1537 1537
    /// Constructor
1538 1538
    NotMap(const M &m) : _m(m) {}
1539 1539
    ///\e
1540 1540
    Value operator[](const Key &k) const { return !_m[k]; }
1541 1541
  };
1542 1542

	
1543 1543
  /// Logical 'not' of a map (read-write version)
1544 1544

	
1545 1545
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1546 1546
  /// logical negation of the values of the given map.
1547 1547
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1548 1548
  /// It makes also possible to write the map. When a value is set,
1549 1549
  /// the opposite value is set to the original map.
1550 1550
  ///
1551 1551
  /// The simplest way of using this map is through the notWriteMap()
1552 1552
  /// function.
1553 1553
  ///
1554 1554
  /// \sa NotMap
1555 1555
  template <typename M>
1556 1556
  class NotWriteMap : public MapBase<typename M::Key, bool> {
1557 1557
    M &_m;
1558 1558
  public:
1559 1559
    ///\e
1560 1560
    typedef typename M::Key Key;
1561 1561
    ///\e
1562 1562
    typedef bool Value;
1563 1563

	
1564 1564
    /// Constructor
1565 1565
    NotWriteMap(M &m) : _m(m) {}
1566 1566
    ///\e
1567 1567
    Value operator[](const Key &k) const { return !_m[k]; }
1568 1568
    ///\e
1569 1569
    void set(const Key &k, bool v) { _m.set(k, !v); }
1570 1570
  };
1571 1571

	
1572 1572
  /// Returns a \c NotMap class
1573 1573

	
1574 1574
  /// This function just returns a \c NotMap class.
1575 1575
  ///
1576 1576
  /// For example, if \c m is a map with \c bool values, then
1577 1577
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1578 1578
  ///
1579 1579
  /// \relates NotMap
1580 1580
  template <typename M>
1581 1581
  inline NotMap<M> notMap(const M &m) {
1582 1582
    return NotMap<M>(m);
1583 1583
  }
1584 1584

	
1585 1585
  /// Returns a \c NotWriteMap class
1586 1586

	
1587 1587
  /// This function just returns a \c NotWriteMap class.
1588 1588
  ///
1589 1589
  /// For example, if \c m is a map with \c bool values, then
1590 1590
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1591 1591
  /// Moreover it makes also possible to write the map.
1592 1592
  ///
1593 1593
  /// \relates NotWriteMap
1594 1594
  template <typename M>
1595 1595
  inline NotWriteMap<M> notWriteMap(M &m) {
1596 1596
    return NotWriteMap<M>(m);
1597 1597
  }
1598 1598

	
1599 1599

	
1600 1600
  /// Combination of two maps using the \c == operator
1601 1601

	
1602 1602
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1603 1603
  /// the keys for which the corresponding values of the two maps are
1604 1604
  /// equal.
1605 1605
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1606 1606
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1607 1607
  ///
1608 1608
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1609 1609
  /// \code
1610 1610
  ///   EqualMap<M1,M2> em(m1,m2);
1611 1611
  /// \endcode
1612 1612
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1613 1613
  ///
1614 1614
  /// The simplest way of using this map is through the equalMap()
1615 1615
  /// function.
1616 1616
  ///
1617 1617
  /// \sa LessMap
1618 1618
  template<typename M1, typename M2>
1619 1619
  class EqualMap : public MapBase<typename M1::Key, bool> {
1620 1620
    const M1 &_m1;
1621 1621
    const M2 &_m2;
1622 1622
  public:
1623 1623
    ///\e
1624 1624
    typedef typename M1::Key Key;
1625 1625
    ///\e
1626 1626
    typedef bool Value;
1627 1627

	
1628 1628
    /// Constructor
1629 1629
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1630 1630
    ///\e
1631 1631
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1632 1632
  };
1633 1633

	
1634 1634
  /// Returns an \c EqualMap class
1635 1635

	
1636 1636
  /// This function just returns an \c EqualMap class.
1637 1637
  ///
1638 1638
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1639 1639
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1640 1640
  /// <tt>m1[x]==m2[x]</tt>.
1641 1641
  ///
1642 1642
  /// \relates EqualMap
1643 1643
  template<typename M1, typename M2>
1644 1644
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1645 1645
    return EqualMap<M1, M2>(m1,m2);
1646 1646
  }
1647 1647

	
1648 1648

	
1649 1649
  /// Combination of two maps using the \c < operator
1650 1650

	
1651 1651
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1652 1652
  /// the keys for which the corresponding value of the first map is
1653 1653
  /// less then the value of the second map.
1654 1654
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1655 1655
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1656 1656
  ///
1657 1657
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1658 1658
  /// \code
1659 1659
  ///   LessMap<M1,M2> lm(m1,m2);
1660 1660
  /// \endcode
1661 1661
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1662 1662
  ///
1663 1663
  /// The simplest way of using this map is through the lessMap()
1664 1664
  /// function.
1665 1665
  ///
1666 1666
  /// \sa EqualMap
1667 1667
  template<typename M1, typename M2>
1668 1668
  class LessMap : public MapBase<typename M1::Key, bool> {
1669 1669
    const M1 &_m1;
1670 1670
    const M2 &_m2;
1671 1671
  public:
1672 1672
    ///\e
1673 1673
    typedef typename M1::Key Key;
1674 1674
    ///\e
1675 1675
    typedef bool Value;
1676 1676

	
1677 1677
    /// Constructor
1678 1678
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1679 1679
    ///\e
1680 1680
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1681 1681
  };
1682 1682

	
1683 1683
  /// Returns an \c LessMap class
1684 1684

	
1685 1685
  /// This function just returns an \c LessMap class.
1686 1686
  ///
1687 1687
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1688 1688
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1689 1689
  /// <tt>m1[x]<m2[x]</tt>.
1690 1690
  ///
1691 1691
  /// \relates LessMap
1692 1692
  template<typename M1, typename M2>
1693 1693
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1694 1694
    return LessMap<M1, M2>(m1,m2);
1695 1695
  }
1696 1696

	
1697 1697
  namespace _maps_bits {
1698 1698

	
1699 1699
    template <typename _Iterator, typename Enable = void>
1700 1700
    struct IteratorTraits {
1701 1701
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1702 1702
    };
1703 1703

	
1704 1704
    template <typename _Iterator>
1705 1705
    struct IteratorTraits<_Iterator,
1706 1706
      typename exists<typename _Iterator::container_type>::type>
1707 1707
    {
1708 1708
      typedef typename _Iterator::container_type::value_type Value;
1709 1709
    };
1710 1710

	
1711 1711
  }
1712 1712

	
1713 1713
  /// @}
1714 1714

	
1715 1715
  /// \addtogroup maps
1716 1716
  /// @{
1717 1717

	
1718 1718
  /// \brief Writable bool map for logging each \c true assigned element
1719 1719
  ///
1720 1720
  /// A \ref concepts::WriteMap "writable" bool map for logging
1721 1721
  /// each \c true assigned element, i.e it copies subsequently each
1722 1722
  /// keys set to \c true to the given iterator.
1723 1723
  /// The most important usage of it is storing certain nodes or arcs
1724 1724
  /// that were marked \c true by an algorithm.
1725 1725
  ///
1726 1726
  /// There are several algorithms that provide solutions through bool
1727 1727
  /// maps and most of them assign \c true at most once for each key.
1728 1728
  /// In these cases it is a natural request to store each \c true
1729 1729
  /// assigned elements (in order of the assignment), which can be
1730 1730
  /// easily done with LoggerBoolMap.
1731 1731
  ///
1732 1732
  /// The simplest way of using this map is through the loggerBoolMap()
1733 1733
  /// function.
1734 1734
  ///
1735 1735
  /// \tparam IT The type of the iterator.
1736 1736
  /// \tparam KEY The key type of the map. The default value set
1737 1737
  /// according to the iterator type should work in most cases.
1738 1738
  ///
1739 1739
  /// \note The container of the iterator must contain enough space
1740 1740
  /// for the elements or the iterator should be an inserter iterator.
1741 1741
#ifdef DOXYGEN
1742 1742
  template <typename IT, typename KEY>
1743 1743
#else
1744 1744
  template <typename IT,
1745 1745
            typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1746 1746
#endif
1747 1747
  class LoggerBoolMap : public MapBase<KEY, bool> {
1748 1748
  public:
1749 1749

	
1750 1750
    ///\e
1751 1751
    typedef KEY Key;
1752 1752
    ///\e
1753 1753
    typedef bool Value;
1754 1754
    ///\e
1755 1755
    typedef IT Iterator;
1756 1756

	
1757 1757
    /// Constructor
1758 1758
    LoggerBoolMap(Iterator it)
1759 1759
      : _begin(it), _end(it) {}
1760 1760

	
1761 1761
    /// Gives back the given iterator set for the first key
1762 1762
    Iterator begin() const {
1763 1763
      return _begin;
1764 1764
    }
1765 1765

	
1766 1766
    /// Gives back the the 'after the last' iterator
1767 1767
    Iterator end() const {
1768 1768
      return _end;
1769 1769
    }
1770 1770

	
1771 1771
    /// The set function of the map
1772 1772
    void set(const Key& key, Value value) {
1773 1773
      if (value) {
1774 1774
        *_end++ = key;
1775 1775
      }
1776 1776
    }
1777 1777

	
1778 1778
  private:
1779 1779
    Iterator _begin;
1780 1780
    Iterator _end;
1781 1781
  };
1782 1782

	
1783 1783
  /// Returns a \c LoggerBoolMap class
1784 1784

	
1785 1785
  /// This function just returns a \c LoggerBoolMap class.
1786 1786
  ///
1787 1787
  /// The most important usage of it is storing certain nodes or arcs
1788 1788
  /// that were marked \c true by an algorithm.
1789 1789
  /// For example it makes easier to store the nodes in the processing
1790 1790
  /// order of Dfs algorithm, as the following examples show.
1791 1791
  /// \code
1792 1792
  ///   std::vector<Node> v;
1793 1793
  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1794 1794
  /// \endcode
1795 1795
  /// \code
1796 1796
  ///   std::vector<Node> v(countNodes(g));
1797 1797
  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1798 1798
  /// \endcode
1799 1799
  ///
1800 1800
  /// \note The container of the iterator must contain enough space
1801 1801
  /// for the elements or the iterator should be an inserter iterator.
1802 1802
  ///
1803 1803
  /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1804 1804
  /// it cannot be used when a readable map is needed, for example as
1805 1805
  /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1806 1806
  ///
1807 1807
  /// \relates LoggerBoolMap
1808 1808
  template<typename Iterator>
1809 1809
  inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1810 1810
    return LoggerBoolMap<Iterator>(it);
1811 1811
  }
1812 1812

	
1813 1813
  /// @}
1814 1814

	
1815 1815
  /// \addtogroup graph_maps
1816 1816
  /// @{
1817 1817

	
1818 1818
  /// \brief Provides an immutable and unique id for each item in a graph.
1819 1819
  ///
1820 1820
  /// IdMap provides a unique and immutable id for each item of the
1821 1821
  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is 
1822 1822
  ///  - \b unique: different items get different ids,
1823 1823
  ///  - \b immutable: the id of an item does not change (even if you
1824 1824
  ///    delete other nodes).
1825 1825
  ///
1826 1826
  /// Using this map you get access (i.e. can read) the inner id values of
1827 1827
  /// the items stored in the graph, which is returned by the \c id()
1828 1828
  /// function of the graph. This map can be inverted with its member
1829 1829
  /// class \c InverseMap or with the \c operator() member.
1830 1830
  ///
1831 1831
  /// \tparam GR The graph type.
1832 1832
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1833 1833
  /// \c GR::Edge).
1834 1834
  ///
1835 1835
  /// \see RangeIdMap
1836 1836
  template <typename GR, typename K>
1837 1837
  class IdMap : public MapBase<K, int> {
1838 1838
  public:
1839 1839
    /// The graph type of IdMap.
1840 1840
    typedef GR Graph;
1841
    typedef GR Digraph;
1841 1842
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1842 1843
    typedef K Item;
1843 1844
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1844 1845
    typedef K Key;
1845 1846
    /// The value type of IdMap.
1846 1847
    typedef int Value;
1847 1848

	
1848 1849
    /// \brief Constructor.
1849 1850
    ///
1850 1851
    /// Constructor of the map.
1851 1852
    explicit IdMap(const Graph& graph) : _graph(&graph) {}
1852 1853

	
1853 1854
    /// \brief Gives back the \e id of the item.
1854 1855
    ///
1855 1856
    /// Gives back the immutable and unique \e id of the item.
1856 1857
    int operator[](const Item& item) const { return _graph->id(item);}
1857 1858

	
1858 1859
    /// \brief Gives back the \e item by its id.
1859 1860
    ///
1860 1861
    /// Gives back the \e item by its id.
1861 1862
    Item operator()(int id) { return _graph->fromId(id, Item()); }
1862 1863

	
1863 1864
  private:
1864 1865
    const Graph* _graph;
1865 1866

	
1866 1867
  public:
1867 1868

	
1868 1869
    /// \brief This class represents the inverse of its owner (IdMap).
1869 1870
    ///
1870 1871
    /// This class represents the inverse of its owner (IdMap).
1871 1872
    /// \see inverse()
1872 1873
    class InverseMap {
1873 1874
    public:
1874 1875

	
1875 1876
      /// \brief Constructor.
1876 1877
      ///
1877 1878
      /// Constructor for creating an id-to-item map.
1878 1879
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1879 1880

	
1880 1881
      /// \brief Constructor.
1881 1882
      ///
1882 1883
      /// Constructor for creating an id-to-item map.
1883 1884
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1884 1885

	
1885 1886
      /// \brief Gives back the given item from its id.
1886 1887
      ///
1887 1888
      /// Gives back the given item from its id.
1888 1889
      Item operator[](int id) const { return _graph->fromId(id, Item());}
1889 1890

	
1890 1891
    private:
1891 1892
      const Graph* _graph;
1892 1893
    };
1893 1894

	
1894 1895
    /// \brief Gives back the inverse of the map.
1895 1896
    ///
1896 1897
    /// Gives back the inverse of the IdMap.
1897 1898
    InverseMap inverse() const { return InverseMap(*_graph);}
1898 1899
  };
1899 1900

	
1900 1901

	
1901 1902
  /// \brief General cross reference graph map type.
1902 1903

	
1903 1904
  /// This class provides simple invertable graph maps.
1904 1905
  /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
1905 1906
  /// and if a key is set to a new value then store it
1906 1907
  /// in the inverse map.
1907 1908
  ///
1908 1909
  /// The values of the map can be accessed
1909 1910
  /// with stl compatible forward iterator.
1910 1911
  ///
1911 1912
  /// \tparam GR The graph type.
1912 1913
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1913 1914
  /// \c GR::Edge).
1914 1915
  /// \tparam V The value type of the map.
1915 1916
  ///
1916 1917
  /// \see IterableValueMap
1917 1918
  template <typename GR, typename K, typename V>
1918 1919
  class CrossRefMap
1919 1920
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1920 1921
  private:
1921 1922

	
1922 1923
    typedef typename ItemSetTraits<GR, K>::
1923 1924
      template Map<V>::Type Map;
1924 1925

	
1925 1926
    typedef std::map<V, K> Container;
1926 1927
    Container _inv_map;
1927 1928

	
1928 1929
  public:
1929 1930

	
1930 1931
    /// The graph type of CrossRefMap.
1931 1932
    typedef GR Graph;
1933
    typedef GR Digraph;
1932 1934
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1933 1935
    typedef K Item;
1934 1936
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1935 1937
    typedef K Key;
1936 1938
    /// The value type of CrossRefMap.
1937 1939
    typedef V Value;
1938 1940

	
1939 1941
    /// \brief Constructor.
1940 1942
    ///
1941 1943
    /// Construct a new CrossRefMap for the given graph.
1942 1944
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1943 1945

	
1944 1946
    /// \brief Forward iterator for values.
1945 1947
    ///
1946 1948
    /// This iterator is an stl compatible forward
1947 1949
    /// iterator on the values of the map. The values can
1948 1950
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1949 1951
    class ValueIterator
1950 1952
      : public std::iterator<std::forward_iterator_tag, Value> {
1951 1953
      friend class CrossRefMap;
1952 1954
    private:
1953 1955
      ValueIterator(typename Container::const_iterator _it)
1954 1956
        : it(_it) {}
1955 1957
    public:
1956 1958

	
1957 1959
      ValueIterator() {}
1958 1960

	
1959 1961
      ValueIterator& operator++() { ++it; return *this; }
1960 1962
      ValueIterator operator++(int) {
1961 1963
        ValueIterator tmp(*this);
1962 1964
        operator++();
1963 1965
        return tmp;
1964 1966
      }
1965 1967

	
1966 1968
      const Value& operator*() const { return it->first; }
1967 1969
      const Value* operator->() const { return &(it->first); }
1968 1970

	
1969 1971
      bool operator==(ValueIterator jt) const { return it == jt.it; }
1970 1972
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
1971 1973

	
1972 1974
    private:
1973 1975
      typename Container::const_iterator it;
1974 1976
    };
1975 1977

	
1976 1978
    /// \brief Returns an iterator to the first value.
1977 1979
    ///
1978 1980
    /// Returns an stl compatible iterator to the
1979 1981
    /// first value of the map. The values of the
1980 1982
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1981 1983
    /// range.
1982 1984
    ValueIterator beginValue() const {
1983 1985
      return ValueIterator(_inv_map.begin());
1984 1986
    }
1985 1987

	
1986 1988
    /// \brief Returns an iterator after the last value.
1987 1989
    ///
1988 1990
    /// Returns an stl compatible iterator after the
1989 1991
    /// last value of the map. The values of the
1990 1992
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1991 1993
    /// range.
1992 1994
    ValueIterator endValue() const {
1993 1995
      return ValueIterator(_inv_map.end());
1994 1996
    }
1995 1997

	
1996 1998
    /// \brief Sets the value associated with the given key.
1997 1999
    ///
1998 2000
    /// Sets the value associated with the given key.
1999 2001
    void set(const Key& key, const Value& val) {
2000 2002
      Value oldval = Map::operator[](key);
2001 2003
      typename Container::iterator it = _inv_map.find(oldval);
2002 2004
      if (it != _inv_map.end() && it->second == key) {
2003 2005
        _inv_map.erase(it);
2004 2006
      }
2005 2007
      _inv_map.insert(make_pair(val, key));
2006 2008
      Map::set(key, val);
2007 2009
    }
2008 2010

	
2009 2011
    /// \brief Returns the value associated with the given key.
2010 2012
    ///
2011 2013
    /// Returns the value associated with the given key.
2012 2014
    typename MapTraits<Map>::ConstReturnValue
2013 2015
    operator[](const Key& key) const {
2014 2016
      return Map::operator[](key);
2015 2017
    }
2016 2018

	
2017 2019
    /// \brief Gives back the item by its value.
2018 2020
    ///
2019 2021
    /// Gives back the item by its value.
2020 2022
    Key operator()(const Value& key) const {
2021 2023
      typename Container::const_iterator it = _inv_map.find(key);
2022 2024
      return it != _inv_map.end() ? it->second : INVALID;
2023 2025
    }
2024 2026

	
2025 2027
  protected:
2026 2028

	
2027 2029
    /// \brief Erase the key from the map and the inverse map.
2028 2030
    ///
2029 2031
    /// Erase the key from the map and the inverse map. It is called by the
2030 2032
    /// \c AlterationNotifier.
2031 2033
    virtual void erase(const Key& key) {
2032 2034
      Value val = Map::operator[](key);
2033 2035
      typename Container::iterator it = _inv_map.find(val);
2034 2036
      if (it != _inv_map.end() && it->second == key) {
2035 2037
        _inv_map.erase(it);
2036 2038
      }
2037 2039
      Map::erase(key);
2038 2040
    }
2039 2041

	
2040 2042
    /// \brief Erase more keys from the map and the inverse map.
2041 2043
    ///
2042 2044
    /// Erase more keys from the map and the inverse map. It is called by the
2043 2045
    /// \c AlterationNotifier.
2044 2046
    virtual void erase(const std::vector<Key>& keys) {
2045 2047
      for (int i = 0; i < int(keys.size()); ++i) {
2046 2048
        Value val = Map::operator[](keys[i]);
2047 2049
        typename Container::iterator it = _inv_map.find(val);
2048 2050
        if (it != _inv_map.end() && it->second == keys[i]) {
2049 2051
          _inv_map.erase(it);
2050 2052
        }
2051 2053
      }
2052 2054
      Map::erase(keys);
2053 2055
    }
2054 2056

	
2055 2057
    /// \brief Clear the keys from the map and the inverse map.
2056 2058
    ///
2057 2059
    /// Clear the keys from the map and the inverse map. It is called by the
2058 2060
    /// \c AlterationNotifier.
2059 2061
    virtual void clear() {
2060 2062
      _inv_map.clear();
2061 2063
      Map::clear();
2062 2064
    }
2063 2065

	
2064 2066
  public:
2065 2067

	
2066 2068
    /// \brief The inverse map type.
2067 2069
    ///
2068 2070
    /// The inverse of this map. The subscript operator of the map
2069 2071
    /// gives back the item that was last assigned to the value.
2070 2072
    class InverseMap {
2071 2073
    public:
2072 2074
      /// \brief Constructor
2073 2075
      ///
2074 2076
      /// Constructor of the InverseMap.
2075 2077
      explicit InverseMap(const CrossRefMap& inverted)
2076 2078
        : _inverted(inverted) {}
2077 2079

	
2078 2080
      /// The value type of the InverseMap.
2079 2081
      typedef typename CrossRefMap::Key Value;
2080 2082
      /// The key type of the InverseMap.
2081 2083
      typedef typename CrossRefMap::Value Key;
2082 2084

	
2083 2085
      /// \brief Subscript operator.
2084 2086
      ///
2085 2087
      /// Subscript operator. It gives back the item
2086 2088
      /// that was last assigned to the given value.
2087 2089
      Value operator[](const Key& key) const {
2088 2090
        return _inverted(key);
2089 2091
      }
2090 2092

	
2091 2093
    private:
2092 2094
      const CrossRefMap& _inverted;
2093 2095
    };
2094 2096

	
2095 2097
    /// \brief It gives back the read-only inverse map.
2096 2098
    ///
2097 2099
    /// It gives back the read-only inverse map.
2098 2100
    InverseMap inverse() const {
2099 2101
      return InverseMap(*this);
2100 2102
    }
2101 2103

	
2102 2104
  };
2103 2105

	
2104 2106
  /// \brief Provides continuous and unique ID for the
2105 2107
  /// items of a graph.
2106 2108
  ///
2107 2109
  /// RangeIdMap provides a unique and continuous
2108 2110
  /// ID for each item of a given type (\c Node, \c Arc or
2109 2111
  /// \c Edge) in a graph. This id is
2110 2112
  ///  - \b unique: different items get different ids,
2111 2113
  ///  - \b continuous: the range of the ids is the set of integers
2112 2114
  ///    between 0 and \c n-1, where \c n is the number of the items of
2113 2115
  ///    this type (\c Node, \c Arc or \c Edge).
2114 2116
  ///  - So, the ids can change when deleting an item of the same type.
2115 2117
  ///
2116 2118
  /// Thus this id is not (necessarily) the same as what can get using
2117 2119
  /// the \c id() function of the graph or \ref IdMap.
2118 2120
  /// This map can be inverted with its member class \c InverseMap,
2119 2121
  /// or with the \c operator() member.
2120 2122
  ///
2121 2123
  /// \tparam GR The graph type.
2122 2124
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2123 2125
  /// \c GR::Edge).
2124 2126
  ///
2125 2127
  /// \see IdMap
2126 2128
  template <typename GR, typename K>
2127 2129
  class RangeIdMap
2128 2130
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2129 2131

	
2130 2132
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2131 2133

	
2132 2134
  public:
2133 2135
    /// The graph type of RangeIdMap.
2134 2136
    typedef GR Graph;
2137
    typedef GR Digraph;
2135 2138
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2136 2139
    typedef K Item;
2137 2140
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2138 2141
    typedef K Key;
2139 2142
    /// The value type of RangeIdMap.
2140 2143
    typedef int Value;
2141 2144

	
2142 2145
    /// \brief Constructor.
2143 2146
    ///
2144 2147
    /// Constructor.
2145 2148
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2146 2149
      Item it;
2147 2150
      const typename Map::Notifier* nf = Map::notifier();
2148 2151
      for (nf->first(it); it != INVALID; nf->next(it)) {
2149 2152
        Map::set(it, _inv_map.size());
2150 2153
        _inv_map.push_back(it);
2151 2154
      }
2152 2155
    }
2153 2156

	
2154 2157
  protected:
2155 2158

	
2156 2159
    /// \brief Adds a new key to the map.
2157 2160
    ///
2158 2161
    /// Add a new key to the map. It is called by the
2159 2162
    /// \c AlterationNotifier.
2160 2163
    virtual void add(const Item& item) {
2161 2164
      Map::add(item);
2162 2165
      Map::set(item, _inv_map.size());
2163 2166
      _inv_map.push_back(item);
2164 2167
    }
2165 2168

	
2166 2169
    /// \brief Add more new keys to the map.
2167 2170
    ///
2168 2171
    /// Add more new keys to the map. It is called by the
2169 2172
    /// \c AlterationNotifier.
2170 2173
    virtual void add(const std::vector<Item>& items) {
2171 2174
      Map::add(items);
2172 2175
      for (int i = 0; i < int(items.size()); ++i) {
2173 2176
        Map::set(items[i], _inv_map.size());
2174 2177
        _inv_map.push_back(items[i]);
2175 2178
      }
2176 2179
    }
2177 2180

	
2178 2181
    /// \brief Erase the key from the map.
2179 2182
    ///
2180 2183
    /// Erase the key from the map. It is called by the
2181 2184
    /// \c AlterationNotifier.
2182 2185
    virtual void erase(const Item& item) {
2183 2186
      Map::set(_inv_map.back(), Map::operator[](item));
2184 2187
      _inv_map[Map::operator[](item)] = _inv_map.back();
2185 2188
      _inv_map.pop_back();
2186 2189
      Map::erase(item);
2187 2190
    }
2188 2191

	
2189 2192
    /// \brief Erase more keys from the map.
2190 2193
    ///
2191 2194
    /// Erase more keys from the map. It is called by the
2192 2195
    /// \c AlterationNotifier.
2193 2196
    virtual void erase(const std::vector<Item>& items) {
2194 2197
      for (int i = 0; i < int(items.size()); ++i) {
2195 2198
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2196 2199
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2197 2200
        _inv_map.pop_back();
2198 2201
      }
2199 2202
      Map::erase(items);
2200 2203
    }
2201 2204

	
2202 2205
    /// \brief Build the unique map.
2203 2206
    ///
2204 2207
    /// Build the unique map. It is called by the
2205 2208
    /// \c AlterationNotifier.
2206 2209
    virtual void build() {
2207 2210
      Map::build();
2208 2211
      Item it;
2209 2212
      const typename Map::Notifier* nf = Map::notifier();
2210 2213
      for (nf->first(it); it != INVALID; nf->next(it)) {
2211 2214
        Map::set(it, _inv_map.size());
2212 2215
        _inv_map.push_back(it);
2213 2216
      }
2214 2217
    }
2215 2218

	
2216 2219
    /// \brief Clear the keys from the map.
2217 2220
    ///
2218 2221
    /// Clear the keys from the map. It is called by the
2219 2222
    /// \c AlterationNotifier.
2220 2223
    virtual void clear() {
2221 2224
      _inv_map.clear();
2222 2225
      Map::clear();
2223 2226
    }
2224 2227

	
2225 2228
  public:
2226 2229

	
2227 2230
    /// \brief Returns the maximal value plus one.
2228 2231
    ///
2229 2232
    /// Returns the maximal value plus one in the map.
2230 2233
    unsigned int size() const {
2231 2234
      return _inv_map.size();
2232 2235
    }
2233 2236

	
2234 2237
    /// \brief Swaps the position of the two items in the map.
2235 2238
    ///
2236 2239
    /// Swaps the position of the two items in the map.
2237 2240
    void swap(const Item& p, const Item& q) {
2238 2241
      int pi = Map::operator[](p);
2239 2242
      int qi = Map::operator[](q);
2240 2243
      Map::set(p, qi);
2241 2244
      _inv_map[qi] = p;
2242 2245
      Map::set(q, pi);
2243 2246
      _inv_map[pi] = q;
2244 2247
    }
2245 2248

	
2246 2249
    /// \brief Gives back the \e RangeId of the item
2247 2250
    ///
2248 2251
    /// Gives back the \e RangeId of the item.
2249 2252
    int operator[](const Item& item) const {
2250 2253
      return Map::operator[](item);
2251 2254
    }
2252 2255

	
2253 2256
    /// \brief Gives back the item belonging to a \e RangeId
2254 2257
    /// 
2255 2258
    /// Gives back the item belonging to a \e RangeId.
2256 2259
    Item operator()(int id) const {
2257 2260
      return _inv_map[id];
2258 2261
    }
2259 2262

	
2260 2263
  private:
2261 2264

	
2262 2265
    typedef std::vector<Item> Container;
2263 2266
    Container _inv_map;
2264 2267

	
2265 2268
  public:
2266 2269

	
2267 2270
    /// \brief The inverse map type of RangeIdMap.
2268 2271
    ///
2269 2272
    /// The inverse map type of RangeIdMap.
2270 2273
    class InverseMap {
2271 2274
    public:
2272 2275
      /// \brief Constructor
2273 2276
      ///
2274 2277
      /// Constructor of the InverseMap.
2275 2278
      explicit InverseMap(const RangeIdMap& inverted)
2276 2279
        : _inverted(inverted) {}
2277 2280

	
2278 2281

	
2279 2282
      /// The value type of the InverseMap.
2280 2283
      typedef typename RangeIdMap::Key Value;
2281 2284
      /// The key type of the InverseMap.
2282 2285
      typedef typename RangeIdMap::Value Key;
2283 2286

	
2284 2287
      /// \brief Subscript operator.
2285 2288
      ///
2286 2289
      /// Subscript operator. It gives back the item
2287 2290
      /// that the descriptor currently belongs to.
2288 2291
      Value operator[](const Key& key) const {
2289 2292
        return _inverted(key);
2290 2293
      }
2291 2294

	
2292 2295
      /// \brief Size of the map.
2293 2296
      ///
2294 2297
      /// Returns the size of the map.
2295 2298
      unsigned int size() const {
2296 2299
        return _inverted.size();
2297 2300
      }
2298 2301

	
2299 2302
    private:
2300 2303
      const RangeIdMap& _inverted;
2301 2304
    };
2302 2305

	
2303 2306
    /// \brief Gives back the inverse of the map.
2304 2307
    ///
2305 2308
    /// Gives back the inverse of the map.
2306 2309
    const InverseMap inverse() const {
2307 2310
      return InverseMap(*this);
2308 2311
    }
2309 2312
  };
2310 2313

	
2311 2314
  /// \brief Map of the source nodes of arcs in a digraph.
2312 2315
  ///
2313 2316
  /// SourceMap provides access for the source node of each arc in a digraph,
2314 2317
  /// which is returned by the \c source() function of the digraph.
2315 2318
  /// \tparam GR The digraph type.
2316 2319
  /// \see TargetMap
2317 2320
  template <typename GR>
2318 2321
  class SourceMap {
2319 2322
  public:
2320 2323

	
2321 2324
    ///\e
2322 2325
    typedef typename GR::Arc Key;
2323 2326
    ///\e
2324 2327
    typedef typename GR::Node Value;
2325 2328

	
2326 2329
    /// \brief Constructor
2327 2330
    ///
2328 2331
    /// Constructor.
2329 2332
    /// \param digraph The digraph that the map belongs to.
2330 2333
    explicit SourceMap(const GR& digraph) : _graph(digraph) {}
2331 2334

	
2332 2335
    /// \brief Returns the source node of the given arc.
2333 2336
    ///
2334 2337
    /// Returns the source node of the given arc.
2335 2338
    Value operator[](const Key& arc) const {
2336 2339
      return _graph.source(arc);
2337 2340
    }
2338 2341

	
2339 2342
  private:
2340 2343
    const GR& _graph;
2341 2344
  };
2342 2345

	
2343 2346
  /// \brief Returns a \c SourceMap class.
2344 2347
  ///
2345 2348
  /// This function just returns an \c SourceMap class.
2346 2349
  /// \relates SourceMap
2347 2350
  template <typename GR>
2348 2351
  inline SourceMap<GR> sourceMap(const GR& graph) {
2349 2352
    return SourceMap<GR>(graph);
2350 2353
  }
2351 2354

	
2352 2355
  /// \brief Map of the target nodes of arcs in a digraph.
2353 2356
  ///
2354 2357
  /// TargetMap provides access for the target node of each arc in a digraph,
2355 2358
  /// which is returned by the \c target() function of the digraph.
2356 2359
  /// \tparam GR The digraph type.
2357 2360
  /// \see SourceMap
2358 2361
  template <typename GR>
2359 2362
  class TargetMap {
2360 2363
  public:
2361 2364

	
2362 2365
    ///\e
2363 2366
    typedef typename GR::Arc Key;
2364 2367
    ///\e
2365 2368
    typedef typename GR::Node Value;
2366 2369

	
2367 2370
    /// \brief Constructor
2368 2371
    ///
2369 2372
    /// Constructor.
2370 2373
    /// \param digraph The digraph that the map belongs to.
2371 2374
    explicit TargetMap(const GR& digraph) : _graph(digraph) {}
2372 2375

	
2373 2376
    /// \brief Returns the target node of the given arc.
2374 2377
    ///
2375 2378
    /// Returns the target node of the given arc.
2376 2379
    Value operator[](const Key& e) const {
2377 2380
      return _graph.target(e);
2378 2381
    }
2379 2382

	
2380 2383
  private:
2381 2384
    const GR& _graph;
2382 2385
  };
2383 2386

	
2384 2387
  /// \brief Returns a \c TargetMap class.
2385 2388
  ///
2386 2389
  /// This function just returns a \c TargetMap class.
2387 2390
  /// \relates TargetMap
2388 2391
  template <typename GR>
2389 2392
  inline TargetMap<GR> targetMap(const GR& graph) {
2390 2393
    return TargetMap<GR>(graph);
2391 2394
  }
2392 2395

	
2393 2396
  /// \brief Map of the "forward" directed arc view of edges in a graph.
2394 2397
  ///
2395 2398
  /// ForwardMap provides access for the "forward" directed arc view of
2396 2399
  /// each edge in a graph, which is returned by the \c direct() function
2397 2400
  /// of the graph with \c true parameter.
2398 2401
  /// \tparam GR The graph type.
2399 2402
  /// \see BackwardMap
2400 2403
  template <typename GR>
2401 2404
  class ForwardMap {
2402 2405
  public:
2403 2406

	
2404 2407
    typedef typename GR::Arc Value;
2405 2408
    typedef typename GR::Edge Key;
2406 2409

	
2407 2410
    /// \brief Constructor
2408 2411
    ///
2409 2412
    /// Constructor.
2410 2413
    /// \param graph The graph that the map belongs to.
2411 2414
    explicit ForwardMap(const GR& graph) : _graph(graph) {}
2412 2415

	
2413 2416
    /// \brief Returns the "forward" directed arc view of the given edge.
2414 2417
    ///
2415 2418
    /// Returns the "forward" directed arc view of the given edge.
2416 2419
    Value operator[](const Key& key) const {
2417 2420
      return _graph.direct(key, true);
2418 2421
    }
2419 2422

	
2420 2423
  private:
2421 2424
    const GR& _graph;
2422 2425
  };
2423 2426

	
2424 2427
  /// \brief Returns a \c ForwardMap class.
2425 2428
  ///
2426 2429
  /// This function just returns an \c ForwardMap class.
2427 2430
  /// \relates ForwardMap
2428 2431
  template <typename GR>
2429 2432
  inline ForwardMap<GR> forwardMap(const GR& graph) {
2430 2433
    return ForwardMap<GR>(graph);
2431 2434
  }
2432 2435

	
2433 2436
  /// \brief Map of the "backward" directed arc view of edges in a graph.
2434 2437
  ///
2435 2438
  /// BackwardMap provides access for the "backward" directed arc view of
2436 2439
  /// each edge in a graph, which is returned by the \c direct() function
2437 2440
  /// of the graph with \c false parameter.
2438 2441
  /// \tparam GR The graph type.
2439 2442
  /// \see ForwardMap
2440 2443
  template <typename GR>
2441 2444
  class BackwardMap {
2442 2445
  public:
2443 2446

	
2444 2447
    typedef typename GR::Arc Value;
2445 2448
    typedef typename GR::Edge Key;
2446 2449

	
2447 2450
    /// \brief Constructor
2448 2451
    ///
2449 2452
    /// Constructor.
2450 2453
    /// \param graph The graph that the map belongs to.
2451 2454
    explicit BackwardMap(const GR& graph) : _graph(graph) {}
2452 2455

	
2453 2456
    /// \brief Returns the "backward" directed arc view of the given edge.
2454 2457
    ///
2455 2458
    /// Returns the "backward" directed arc view of the given edge.
2456 2459
    Value operator[](const Key& key) const {
2457 2460
      return _graph.direct(key, false);
2458 2461
    }
2459 2462

	
2460 2463
  private:
2461 2464
    const GR& _graph;
2462 2465
  };
2463 2466

	
2464 2467
  /// \brief Returns a \c BackwardMap class
2465 2468

	
2466 2469
  /// This function just returns a \c BackwardMap class.
2467 2470
  /// \relates BackwardMap
2468 2471
  template <typename GR>
2469 2472
  inline BackwardMap<GR> backwardMap(const GR& graph) {
2470 2473
    return BackwardMap<GR>(graph);
2471 2474
  }
2472 2475

	
2473 2476
  /// \brief Map of the in-degrees of nodes in a digraph.
2474 2477
  ///
2475 2478
  /// This map returns the in-degree of a node. Once it is constructed,
2476 2479
  /// the degrees are stored in a standard \c NodeMap, so each query is done
2477 2480
  /// in constant time. On the other hand, the values are updated automatically
2478 2481
  /// whenever the digraph changes.
2479 2482
  ///
2480 2483
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure 
2481 2484
  /// may provide alternative ways to modify the digraph.
2482 2485
  /// The correct behavior of InDegMap is not guarantied if these additional
2483 2486
  /// features are used. For example the functions
2484 2487
  /// \ref ListDigraph::changeSource() "changeSource()",
2485 2488
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2486 2489
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2487 2490
  /// of \ref ListDigraph will \e not update the degree values correctly.
2488 2491
  ///
2489 2492
  /// \sa OutDegMap
2490 2493
  template <typename GR>
2491 2494
  class InDegMap
2492 2495
    : protected ItemSetTraits<GR, typename GR::Arc>
2493 2496
      ::ItemNotifier::ObserverBase {
2494 2497

	
2495 2498
  public:
2496 2499
    
2497
    /// The digraph type
2500
    /// The graph type of InDegMap
2501
    typedef GR Graph;
2498 2502
    typedef GR Digraph;
2499 2503
    /// The key type
2500 2504
    typedef typename Digraph::Node Key;
2501 2505
    /// The value type
2502 2506
    typedef int Value;
2503 2507

	
2504 2508
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2505 2509
    ::ItemNotifier::ObserverBase Parent;
2506 2510

	
2507 2511
  private:
2508 2512

	
2509 2513
    class AutoNodeMap
2510 2514
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2511 2515
    public:
2512 2516

	
2513 2517
      typedef typename ItemSetTraits<Digraph, Key>::
2514 2518
      template Map<int>::Type Parent;
2515 2519

	
2516 2520
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2517 2521

	
2518 2522
      virtual void add(const Key& key) {
2519 2523
        Parent::add(key);
2520 2524
        Parent::set(key, 0);
2521 2525
      }
2522 2526

	
2523 2527
      virtual void add(const std::vector<Key>& keys) {
2524 2528
        Parent::add(keys);
2525 2529
        for (int i = 0; i < int(keys.size()); ++i) {
2526 2530
          Parent::set(keys[i], 0);
2527 2531
        }
2528 2532
      }
2529 2533

	
2530 2534
      virtual void build() {
2531 2535
        Parent::build();
2532 2536
        Key it;
2533 2537
        typename Parent::Notifier* nf = Parent::notifier();
2534 2538
        for (nf->first(it); it != INVALID; nf->next(it)) {
2535 2539
          Parent::set(it, 0);
2536 2540
        }
2537 2541
      }
2538 2542
    };
2539 2543

	
2540 2544
  public:
2541 2545

	
2542 2546
    /// \brief Constructor.
2543 2547
    ///
2544 2548
    /// Constructor for creating an in-degree map.
2545 2549
    explicit InDegMap(const Digraph& graph)
2546 2550
      : _digraph(graph), _deg(graph) {
2547 2551
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2548 2552

	
2549 2553
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2550 2554
        _deg[it] = countInArcs(_digraph, it);
2551 2555
      }
2552 2556
    }
2553 2557

	
2554 2558
    /// \brief Gives back the in-degree of a Node.
2555 2559
    ///
2556 2560
    /// Gives back the in-degree of a Node.
2557 2561
    int operator[](const Key& key) const {
2558 2562
      return _deg[key];
2559 2563
    }
2560 2564

	
2561 2565
  protected:
2562 2566

	
2563 2567
    typedef typename Digraph::Arc Arc;
2564 2568

	
2565 2569
    virtual void add(const Arc& arc) {
2566 2570
      ++_deg[_digraph.target(arc)];
2567 2571
    }
2568 2572

	
2569 2573
    virtual void add(const std::vector<Arc>& arcs) {
2570 2574
      for (int i = 0; i < int(arcs.size()); ++i) {
2571 2575
        ++_deg[_digraph.target(arcs[i])];
2572 2576
      }
2573 2577
    }
2574 2578

	
2575 2579
    virtual void erase(const Arc& arc) {
2576 2580
      --_deg[_digraph.target(arc)];
2577 2581
    }
2578 2582

	
2579 2583
    virtual void erase(const std::vector<Arc>& arcs) {
2580 2584
      for (int i = 0; i < int(arcs.size()); ++i) {
2581 2585
        --_deg[_digraph.target(arcs[i])];
2582 2586
      }
2583 2587
    }
2584 2588

	
2585 2589
    virtual void build() {
2586 2590
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2587 2591
        _deg[it] = countInArcs(_digraph, it);
2588 2592
      }
2589 2593
    }
2590 2594

	
2591 2595
    virtual void clear() {
2592 2596
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2593 2597
        _deg[it] = 0;
2594 2598
      }
2595 2599
    }
2596 2600
  private:
2597 2601

	
2598 2602
    const Digraph& _digraph;
2599 2603
    AutoNodeMap _deg;
2600 2604
  };
2601 2605

	
2602 2606
  /// \brief Map of the out-degrees of nodes in a digraph.
2603 2607
  ///
2604 2608
  /// This map returns the out-degree of a node. Once it is constructed,
2605 2609
  /// the degrees are stored in a standard \c NodeMap, so each query is done
2606 2610
  /// in constant time. On the other hand, the values are updated automatically
2607 2611
  /// whenever the digraph changes.
2608 2612
  ///
2609 2613
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure 
2610 2614
  /// may provide alternative ways to modify the digraph.
2611 2615
  /// The correct behavior of OutDegMap is not guarantied if these additional
2612 2616
  /// features are used. For example the functions
2613 2617
  /// \ref ListDigraph::changeSource() "changeSource()",
2614 2618
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2615 2619
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2616 2620
  /// of \ref ListDigraph will \e not update the degree values correctly.
2617 2621
  ///
2618 2622
  /// \sa InDegMap
2619 2623
  template <typename GR>
2620 2624
  class OutDegMap
2621 2625
    : protected ItemSetTraits<GR, typename GR::Arc>
2622 2626
      ::ItemNotifier::ObserverBase {
2623 2627

	
2624 2628
  public:
2625 2629

	
2626
    /// The digraph type
2630
    /// The graph type of OutDegMap
2631
    typedef GR Graph;
2627 2632
    typedef GR Digraph;
2628 2633
    /// The key type
2629 2634
    typedef typename Digraph::Node Key;
2630 2635
    /// The value type
2631 2636
    typedef int Value;
2632 2637

	
2633 2638
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2634 2639
    ::ItemNotifier::ObserverBase Parent;
2635 2640

	
2636 2641
  private:
2637 2642

	
2638 2643
    class AutoNodeMap
2639 2644
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2640 2645
    public:
2641 2646

	
2642 2647
      typedef typename ItemSetTraits<Digraph, Key>::
2643 2648
      template Map<int>::Type Parent;
2644 2649

	
2645 2650
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2646 2651

	
2647 2652
      virtual void add(const Key& key) {
2648 2653
        Parent::add(key);
2649 2654
        Parent::set(key, 0);
2650 2655
      }
2651 2656
      virtual void add(const std::vector<Key>& keys) {
2652 2657
        Parent::add(keys);
2653 2658
        for (int i = 0; i < int(keys.size()); ++i) {
2654 2659
          Parent::set(keys[i], 0);
2655 2660
        }
2656 2661
      }
2657 2662
      virtual void build() {
2658 2663
        Parent::build();
2659 2664
        Key it;
2660 2665
        typename Parent::Notifier* nf = Parent::notifier();
2661 2666
        for (nf->first(it); it != INVALID; nf->next(it)) {
2662 2667
          Parent::set(it, 0);
2663 2668
        }
2664 2669
      }
2665 2670
    };
2666 2671

	
2667 2672
  public:
2668 2673

	
2669 2674
    /// \brief Constructor.
2670 2675
    ///
2671 2676
    /// Constructor for creating an out-degree map.
2672 2677
    explicit OutDegMap(const Digraph& graph)
2673 2678
      : _digraph(graph), _deg(graph) {
2674 2679
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2675 2680

	
2676 2681
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2677 2682
        _deg[it] = countOutArcs(_digraph, it);
2678 2683
      }
2679 2684
    }
2680 2685

	
2681 2686
    /// \brief Gives back the out-degree of a Node.
2682 2687
    ///
2683 2688
    /// Gives back the out-degree of a Node.
2684 2689
    int operator[](const Key& key) const {
2685 2690
      return _deg[key];
2686 2691
    }
2687 2692

	
2688 2693
  protected:
2689 2694

	
2690 2695
    typedef typename Digraph::Arc Arc;
2691 2696

	
2692 2697
    virtual void add(const Arc& arc) {
2693 2698
      ++_deg[_digraph.source(arc)];
2694 2699
    }
2695 2700

	
2696 2701
    virtual void add(const std::vector<Arc>& arcs) {
2697 2702
      for (int i = 0; i < int(arcs.size()); ++i) {
2698 2703
        ++_deg[_digraph.source(arcs[i])];
2699 2704
      }
2700 2705
    }
2701 2706

	
2702 2707
    virtual void erase(const Arc& arc) {
2703 2708
      --_deg[_digraph.source(arc)];
2704 2709
    }
2705 2710

	
2706 2711
    virtual void erase(const std::vector<Arc>& arcs) {
2707 2712
      for (int i = 0; i < int(arcs.size()); ++i) {
2708 2713
        --_deg[_digraph.source(arcs[i])];
2709 2714
      }
2710 2715
    }
2711 2716

	
2712 2717
    virtual void build() {
2713 2718
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2714 2719
        _deg[it] = countOutArcs(_digraph, it);
2715 2720
      }
2716 2721
    }
2717 2722

	
2718 2723
    virtual void clear() {
2719 2724
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2720 2725
        _deg[it] = 0;
2721 2726
      }
2722 2727
    }
2723 2728
  private:
2724 2729

	
2725 2730
    const Digraph& _digraph;
2726 2731
    AutoNodeMap _deg;
2727 2732
  };
2728 2733

	
2729 2734
  /// \brief Potential difference map
2730 2735
  ///
2731 2736
  /// PotentialDifferenceMap returns the difference between the potentials of
2732 2737
  /// the source and target nodes of each arc in a digraph, i.e. it returns
2733 2738
  /// \code
2734 2739
  ///   potential[gr.target(arc)] - potential[gr.source(arc)].
2735 2740
  /// \endcode
2736 2741
  /// \tparam GR The digraph type.
2737 2742
  /// \tparam POT A node map storing the potentials.
2738 2743
  template <typename GR, typename POT>
2739 2744
  class PotentialDifferenceMap {
2740 2745
  public:
2741 2746
    /// Key type
2742 2747
    typedef typename GR::Arc Key;
2743 2748
    /// Value type
2744 2749
    typedef typename POT::Value Value;
2745 2750

	
2746 2751
    /// \brief Constructor
2747 2752
    ///
2748 2753
    /// Contructor of the map.
2749 2754
    explicit PotentialDifferenceMap(const GR& gr,
2750 2755
                                    const POT& potential)
2751 2756
      : _digraph(gr), _potential(potential) {}
2752 2757

	
2753 2758
    /// \brief Returns the potential difference for the given arc.
2754 2759
    ///
2755 2760
    /// Returns the potential difference for the given arc, i.e.
2756 2761
    /// \code
2757 2762
    ///   potential[gr.target(arc)] - potential[gr.source(arc)].
2758 2763
    /// \endcode
2759 2764
    Value operator[](const Key& arc) const {
2760 2765
      return _potential[_digraph.target(arc)] -
2761 2766
        _potential[_digraph.source(arc)];
2762 2767
    }
2763 2768

	
2764 2769
  private:
2765 2770
    const GR& _digraph;
2766 2771
    const POT& _potential;
2767 2772
  };
2768 2773

	
2769 2774
  /// \brief Returns a PotentialDifferenceMap.
2770 2775
  ///
2771 2776
  /// This function just returns a PotentialDifferenceMap.
2772 2777
  /// \relates PotentialDifferenceMap
2773 2778
  template <typename GR, typename POT>
2774 2779
  PotentialDifferenceMap<GR, POT>
2775 2780
  potentialDifferenceMap(const GR& gr, const POT& potential) {
2776 2781
    return PotentialDifferenceMap<GR, POT>(gr, potential);
2777 2782
  }
2778 2783

	
2779 2784
  /// @}
2780 2785
}
2781 2786

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

	
19 19
#ifndef LEMON_SMART_GRAPH_H
20 20
#define LEMON_SMART_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24 24
///\brief SmartDigraph and SmartGraph classes.
25 25

	
26 26
#include <vector>
27 27

	
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/bits/graph_extender.h>
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  class SmartDigraph;
35 35
  ///Base of SmartDigraph
36 36

	
37 37
  ///Base of SmartDigraph
38 38
  ///
39 39
  class SmartDigraphBase {
40 40
  protected:
41 41

	
42 42
    struct NodeT
43 43
    {
44 44
      int first_in, first_out;
45 45
      NodeT() {}
46 46
    };
47 47
    struct ArcT
48 48
    {
49 49
      int target, source, next_in, next_out;
50 50
      ArcT() {}
51 51
    };
52 52

	
53 53
    std::vector<NodeT> nodes;
54 54
    std::vector<ArcT> arcs;
55 55

	
56 56
  public:
57 57

	
58
    typedef SmartDigraphBase Graph;
58
    typedef SmartDigraphBase Digraph;
59 59

	
60 60
    class Node;
61 61
    class Arc;
62 62

	
63 63
  public:
64 64

	
65 65
    SmartDigraphBase() : nodes(), arcs() { }
66 66
    SmartDigraphBase(const SmartDigraphBase &_g)
67 67
      : nodes(_g.nodes), arcs(_g.arcs) { }
68 68

	
69 69
    typedef True NodeNumTag;
70 70
    typedef True ArcNumTag;
71 71

	
72 72
    int nodeNum() const { return nodes.size(); }
73 73
    int arcNum() const { return arcs.size(); }
74 74

	
75 75
    int maxNodeId() const { return nodes.size()-1; }
76 76
    int maxArcId() const { return arcs.size()-1; }
77 77

	
78 78
    Node addNode() {
79 79
      int n = nodes.size();
80 80
      nodes.push_back(NodeT());
81 81
      nodes[n].first_in = -1;
82 82
      nodes[n].first_out = -1;
83 83
      return Node(n);
84 84
    }
85 85

	
86 86
    Arc addArc(Node u, Node v) {
87 87
      int n = arcs.size();
88 88
      arcs.push_back(ArcT());
89 89
      arcs[n].source = u._id;
90 90
      arcs[n].target = v._id;
91 91
      arcs[n].next_out = nodes[u._id].first_out;
92 92
      arcs[n].next_in = nodes[v._id].first_in;
93 93
      nodes[u._id].first_out = nodes[v._id].first_in = n;
94 94

	
95 95
      return Arc(n);
96 96
    }
97 97

	
98 98
    void clear() {
99 99
      arcs.clear();
100 100
      nodes.clear();
101 101
    }
102 102

	
103 103
    Node source(Arc a) const { return Node(arcs[a._id].source); }
104 104
    Node target(Arc a) const { return Node(arcs[a._id].target); }
105 105

	
106 106
    static int id(Node v) { return v._id; }
107 107
    static int id(Arc a) { return a._id; }
108 108

	
109 109
    static Node nodeFromId(int id) { return Node(id);}
110 110
    static Arc arcFromId(int id) { return Arc(id);}
111 111

	
112 112
    bool valid(Node n) const {
113 113
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
114 114
    }
115 115
    bool valid(Arc a) const {
116 116
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
117 117
    }
118 118

	
119 119
    class Node {
120 120
      friend class SmartDigraphBase;
121 121
      friend class SmartDigraph;
122 122

	
123 123
    protected:
124 124
      int _id;
125 125
      explicit Node(int id) : _id(id) {}
126 126
    public:
127 127
      Node() {}
128 128
      Node (Invalid) : _id(-1) {}
129 129
      bool operator==(const Node i) const {return _id == i._id;}
130 130
      bool operator!=(const Node i) const {return _id != i._id;}
131 131
      bool operator<(const Node i) const {return _id < i._id;}
132 132
    };
133 133

	
134 134

	
135 135
    class Arc {
136 136
      friend class SmartDigraphBase;
137 137
      friend class SmartDigraph;
138 138

	
139 139
    protected:
140 140
      int _id;
141 141
      explicit Arc(int id) : _id(id) {}
142 142
    public:
143 143
      Arc() { }
144 144
      Arc (Invalid) : _id(-1) {}
145 145
      bool operator==(const Arc i) const {return _id == i._id;}
146 146
      bool operator!=(const Arc i) const {return _id != i._id;}
147 147
      bool operator<(const Arc i) const {return _id < i._id;}
148 148
    };
149 149

	
150 150
    void first(Node& node) const {
151 151
      node._id = nodes.size() - 1;
152 152
    }
153 153

	
154 154
    static void next(Node& node) {
155 155
      --node._id;
156 156
    }
157 157

	
158 158
    void first(Arc& arc) const {
159 159
      arc._id = arcs.size() - 1;
160 160
    }
161 161

	
162 162
    static void next(Arc& arc) {
163 163
      --arc._id;
164 164
    }
165 165

	
166 166
    void firstOut(Arc& arc, const Node& node) const {
167 167
      arc._id = nodes[node._id].first_out;
168 168
    }
169 169

	
170 170
    void nextOut(Arc& arc) const {
171 171
      arc._id = arcs[arc._id].next_out;
172 172
    }
173 173

	
174 174
    void firstIn(Arc& arc, const Node& node) const {
175 175
      arc._id = nodes[node._id].first_in;
176 176
    }
177 177

	
178 178
    void nextIn(Arc& arc) const {
179 179
      arc._id = arcs[arc._id].next_in;
180 180
    }
181 181

	
182 182
  };
183 183

	
184 184
  typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
185 185

	
186 186
  ///\ingroup graphs
187 187
  ///
188 188
  ///\brief A smart directed graph class.
189 189
  ///
190 190
  ///This is a simple and fast digraph implementation.
191 191
  ///It is also quite memory efficient, but at the price
192 192
  ///that <b> it does support only limited (only stack-like)
193 193
  ///node and arc deletions</b>.
194 194
  ///It fully conforms to the \ref concepts::Digraph "Digraph concept".
195 195
  ///
196 196
  ///\sa concepts::Digraph.
197 197
  class SmartDigraph : public ExtendedSmartDigraphBase {
198
  public:
199

	
200 198
    typedef ExtendedSmartDigraphBase Parent;
201 199

	
202 200
  private:
203 201

	
204 202
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
205 203

	
206 204
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
207 205
    ///
208 206
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
209 207
    ///\brief Assignment of SmartDigraph to another one is \e not allowed.
210 208
    ///Use DigraphCopy() instead.
211 209

	
212 210
    ///Assignment of SmartDigraph to another one is \e not allowed.
213 211
    ///Use DigraphCopy() instead.
214 212
    void operator=(const SmartDigraph &) {}
215 213

	
216 214
  public:
217 215

	
218 216
    /// Constructor
219 217

	
220 218
    /// Constructor.
221 219
    ///
222 220
    SmartDigraph() {};
223 221

	
224 222
    ///Add a new node to the digraph.
225 223

	
226 224
    /// Add a new node to the digraph.
227 225
    /// \return The new node.
228 226
    Node addNode() { return Parent::addNode(); }
229 227

	
230 228
    ///Add a new arc to the digraph.
231 229

	
232 230
    ///Add a new arc to the digraph with source node \c s
233 231
    ///and target node \c t.
234 232
    ///\return The new arc.
235 233
    Arc addArc(const Node& s, const Node& t) {
236 234
      return Parent::addArc(s, t);
237 235
    }
238 236

	
239 237
    /// \brief Using this it is possible to avoid the superfluous memory
240 238
    /// allocation.
241 239

	
242 240
    /// Using this it is possible to avoid the superfluous memory
243 241
    /// allocation: if you know that the digraph you want to build will
244 242
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
245 243
    /// then it is worth reserving space for this amount before starting
246 244
    /// to build the digraph.
247 245
    /// \sa reserveArc
248 246
    void reserveNode(int n) { nodes.reserve(n); };
249 247

	
250 248
    /// \brief Using this it is possible to avoid the superfluous memory
251 249
    /// allocation.
252 250

	
253 251
    /// Using this it is possible to avoid the superfluous memory
254 252
    /// allocation: if you know that the digraph you want to build will
255 253
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
256 254
    /// then it is worth reserving space for this amount before starting
257 255
    /// to build the digraph.
258 256
    /// \sa reserveNode
259 257
    void reserveArc(int m) { arcs.reserve(m); };
260 258

	
261 259
    /// \brief Node validity check
262 260
    ///
263 261
    /// This function gives back true if the given node is valid,
264 262
    /// ie. it is a real node of the graph.
265 263
    ///
266 264
    /// \warning A removed node (using Snapshot) could become valid again
267 265
    /// when new nodes are added to the graph.
268 266
    bool valid(Node n) const { return Parent::valid(n); }
269 267

	
270 268
    /// \brief Arc validity check
271 269
    ///
272 270
    /// This function gives back true if the given arc is valid,
273 271
    /// ie. it is a real arc of the graph.
274 272
    ///
275 273
    /// \warning A removed arc (using Snapshot) could become valid again
276 274
    /// when new arcs are added to the graph.
277 275
    bool valid(Arc a) const { return Parent::valid(a); }
278 276

	
279 277
    ///Clear the digraph.
280 278

	
281 279
    ///Erase all the nodes and arcs from the digraph.
282 280
    ///
283 281
    void clear() {
284 282
      Parent::clear();
285 283
    }
286 284

	
287 285
    ///Split a node.
288 286

	
289 287
    ///This function splits a node. First a new node is added to the digraph,
290 288
    ///then the source of each outgoing arc of \c n is moved to this new node.
291 289
    ///If \c connect is \c true (this is the default value), then a new arc
292 290
    ///from \c n to the newly created node is also added.
293 291
    ///\return The newly created node.
294 292
    ///
295 293
    ///\note The <tt>Arc</tt>s
296 294
    ///referencing a moved arc remain
297 295
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
298 296
    ///may be invalidated.
299 297
    ///\warning This functionality cannot be used together with the Snapshot
300 298
    ///feature.
301 299
    Node split(Node n, bool connect = true)
302 300
    {
303 301
      Node b = addNode();
304 302
      nodes[b._id].first_out=nodes[n._id].first_out;
305 303
      nodes[n._id].first_out=-1;
306 304
      for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
307 305
        arcs[i].source=b._id;
308 306
      }
309 307
      if(connect) addArc(n,b);
310 308
      return b;
311 309
    }
312 310

	
313 311
  public:
314 312

	
315 313
    class Snapshot;
316 314

	
317 315
  protected:
318 316

	
319 317
    void restoreSnapshot(const Snapshot &s)
320 318
    {
321 319
      while(s.arc_num<arcs.size()) {
322 320
        Arc arc = arcFromId(arcs.size()-1);
323 321
        Parent::notifier(Arc()).erase(arc);
324 322
        nodes[arcs.back().source].first_out=arcs.back().next_out;
325 323
        nodes[arcs.back().target].first_in=arcs.back().next_in;
326 324
        arcs.pop_back();
327 325
      }
328 326
      while(s.node_num<nodes.size()) {
329 327
        Node node = nodeFromId(nodes.size()-1);
330 328
        Parent::notifier(Node()).erase(node);
331 329
        nodes.pop_back();
332 330
      }
333 331
    }
334 332

	
335 333
  public:
336 334

	
337 335
    ///Class to make a snapshot of the digraph and to restrore to it later.
338 336

	
339 337
    ///Class to make a snapshot of the digraph and to restrore to it later.
340 338
    ///
341 339
    ///The newly added nodes and arcs can be removed using the
342 340
    ///restore() function.
343 341
    ///\note After you restore a state, you cannot restore
344 342
    ///a later state, in other word you cannot add again the arcs deleted
345 343
    ///by restore() using another one Snapshot instance.
346 344
    ///
347 345
    ///\warning If you do not use correctly the snapshot that can cause
348 346
    ///either broken program, invalid state of the digraph, valid but
349 347
    ///not the restored digraph or no change. Because the runtime performance
350 348
    ///the validity of the snapshot is not stored.
351 349
    class Snapshot
352 350
    {
353 351
      SmartDigraph *_graph;
354 352
    protected:
355 353
      friend class SmartDigraph;
356 354
      unsigned int node_num;
357 355
      unsigned int arc_num;
358 356
    public:
359 357
      ///Default constructor.
360 358

	
361 359
      ///Default constructor.
362 360
      ///To actually make a snapshot you must call save().
363 361
      ///
364 362
      Snapshot() : _graph(0) {}
365 363
      ///Constructor that immediately makes a snapshot
366 364

	
367 365
      ///This constructor immediately makes a snapshot of the digraph.
368 366
      ///\param graph The digraph we make a snapshot of.
369 367
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
370 368
        node_num=_graph->nodes.size();
371 369
        arc_num=_graph->arcs.size();
372 370
      }
373 371

	
374 372
      ///Make a snapshot.
375 373

	
376 374
      ///Make a snapshot of the digraph.
377 375
      ///
378 376
      ///This function can be called more than once. In case of a repeated
379 377
      ///call, the previous snapshot gets lost.
380 378
      ///\param graph The digraph we make the snapshot of.
381 379
      void save(SmartDigraph &graph)
382 380
      {
383 381
        _graph=&graph;
384 382
        node_num=_graph->nodes.size();
385 383
        arc_num=_graph->arcs.size();
386 384
      }
387 385

	
388 386
      ///Undo the changes until a snapshot.
389 387

	
390 388
      ///Undo the changes until a snapshot created by save().
391 389
      ///
392 390
      ///\note After you restored a state, you cannot restore
393 391
      ///a later state, in other word you cannot add again the arcs deleted
394 392
      ///by restore().
395 393
      void restore()
396 394
      {
397 395
        _graph->restoreSnapshot(*this);
398 396
      }
399 397
    };
400 398
  };
401 399

	
402 400

	
403 401
  class SmartGraphBase {
404 402

	
405 403
  protected:
406 404

	
407 405
    struct NodeT {
408 406
      int first_out;
409 407
    };
410 408

	
411 409
    struct ArcT {
412 410
      int target;
413 411
      int next_out;
414 412
    };
415 413

	
416 414
    std::vector<NodeT> nodes;
417 415
    std::vector<ArcT> arcs;
418 416

	
419 417
    int first_free_arc;
420 418

	
421 419
  public:
422 420

	
423
    typedef SmartGraphBase Digraph;
421
    typedef SmartGraphBase Graph;
424 422

	
425 423
    class Node;
426 424
    class Arc;
427 425
    class Edge;
428 426

	
429 427
    class Node {
430 428
      friend class SmartGraphBase;
431 429
    protected:
432 430

	
433 431
      int _id;
434 432
      explicit Node(int id) { _id = id;}
435 433

	
436 434
    public:
437 435
      Node() {}
438 436
      Node (Invalid) { _id = -1; }
439 437
      bool operator==(const Node& node) const {return _id == node._id;}
440 438
      bool operator!=(const Node& node) const {return _id != node._id;}
441 439
      bool operator<(const Node& node) const {return _id < node._id;}
442 440
    };
443 441

	
444 442
    class Edge {
445 443
      friend class SmartGraphBase;
446 444
    protected:
447 445

	
448 446
      int _id;
449 447
      explicit Edge(int id) { _id = id;}
450 448

	
451 449
    public:
452 450
      Edge() {}
453 451
      Edge (Invalid) { _id = -1; }
454 452
      bool operator==(const Edge& arc) const {return _id == arc._id;}
455 453
      bool operator!=(const Edge& arc) const {return _id != arc._id;}
456 454
      bool operator<(const Edge& arc) const {return _id < arc._id;}
457 455
    };
458 456

	
459 457
    class Arc {
460 458
      friend class SmartGraphBase;
461 459
    protected:
462 460

	
463 461
      int _id;
464 462
      explicit Arc(int id) { _id = id;}
465 463

	
466 464
    public:
467 465
      operator Edge() const {
468 466
        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
469 467
      }
470 468

	
471 469
      Arc() {}
472 470
      Arc (Invalid) { _id = -1; }
473 471
      bool operator==(const Arc& arc) const {return _id == arc._id;}
474 472
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
475 473
      bool operator<(const Arc& arc) const {return _id < arc._id;}
476 474
    };
477 475

	
478 476

	
479 477

	
480 478
    SmartGraphBase()
481 479
      : nodes(), arcs() {}
482 480

	
483 481
    typedef True NodeNumTag;
484 482
    typedef True EdgeNumTag;
485 483
    typedef True ArcNumTag;
486 484

	
487 485
    int nodeNum() const { return nodes.size(); }
488 486
    int edgeNum() const { return arcs.size() / 2; }
489 487
    int arcNum() const { return arcs.size(); }
490 488

	
491 489
    int maxNodeId() const { return nodes.size()-1; }
492 490
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
493 491
    int maxArcId() const { return arcs.size()-1; }
494 492

	
495 493
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
496 494
    Node target(Arc e) const { return Node(arcs[e._id].target); }
497 495

	
498 496
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
499 497
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
500 498

	
501 499
    static bool direction(Arc e) {
502 500
      return (e._id & 1) == 1;
503 501
    }
504 502

	
505 503
    static Arc direct(Edge e, bool d) {
506 504
      return Arc(e._id * 2 + (d ? 1 : 0));
507 505
    }
508 506

	
509 507
    void first(Node& node) const {
510 508
      node._id = nodes.size() - 1;
511 509
    }
512 510

	
513 511
    void next(Node& node) const {
514 512
      --node._id;
515 513
    }
516 514

	
517 515
    void first(Arc& arc) const {
518 516
      arc._id = arcs.size() - 1;
519 517
    }
520 518

	
521 519
    void next(Arc& arc) const {
522 520
      --arc._id;
523 521
    }
524 522

	
525 523
    void first(Edge& arc) const {
526 524
      arc._id = arcs.size() / 2 - 1;
527 525
    }
528 526

	
529 527
    void next(Edge& arc) const {
530 528
      --arc._id;
531 529
    }
532 530

	
533 531
    void firstOut(Arc &arc, const Node& v) const {
534 532
      arc._id = nodes[v._id].first_out;
535 533
    }
536 534
    void nextOut(Arc &arc) const {
537 535
      arc._id = arcs[arc._id].next_out;
538 536
    }
539 537

	
540 538
    void firstIn(Arc &arc, const Node& v) const {
541 539
      arc._id = ((nodes[v._id].first_out) ^ 1);
542 540
      if (arc._id == -2) arc._id = -1;
543 541
    }
544 542
    void nextIn(Arc &arc) const {
545 543
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
546 544
      if (arc._id == -2) arc._id = -1;
547 545
    }
548 546

	
549 547
    void firstInc(Edge &arc, bool& d, const Node& v) const {
550 548
      int de = nodes[v._id].first_out;
551 549
      if (de != -1) {
552 550
        arc._id = de / 2;
553 551
        d = ((de & 1) == 1);
554 552
      } else {
555 553
        arc._id = -1;
556 554
        d = true;
557 555
      }
558 556
    }
559 557
    void nextInc(Edge &arc, bool& d) const {
560 558
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
561 559
      if (de != -1) {
562 560
        arc._id = de / 2;
563 561
        d = ((de & 1) == 1);
564 562
      } else {
565 563
        arc._id = -1;
566 564
        d = true;
567 565
      }
568 566
    }
569 567

	
570 568
    static int id(Node v) { return v._id; }
571 569
    static int id(Arc e) { return e._id; }
572 570
    static int id(Edge e) { return e._id; }
573 571

	
574 572
    static Node nodeFromId(int id) { return Node(id);}
575 573
    static Arc arcFromId(int id) { return Arc(id);}
576 574
    static Edge edgeFromId(int id) { return Edge(id);}
577 575

	
578 576
    bool valid(Node n) const {
579 577
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
580 578
    }
581 579
    bool valid(Arc a) const {
582 580
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
583 581
    }
584 582
    bool valid(Edge e) const {
585 583
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
586 584
    }
587 585

	
588 586
    Node addNode() {
589 587
      int n = nodes.size();
590 588
      nodes.push_back(NodeT());
591 589
      nodes[n].first_out = -1;
592 590

	
593 591
      return Node(n);
594 592
    }
595 593

	
596 594
    Edge addEdge(Node u, Node v) {
597 595
      int n = arcs.size();
598 596
      arcs.push_back(ArcT());
599 597
      arcs.push_back(ArcT());
600 598

	
601 599
      arcs[n].target = u._id;
602 600
      arcs[n | 1].target = v._id;
603 601

	
604 602
      arcs[n].next_out = nodes[v._id].first_out;
605 603
      nodes[v._id].first_out = n;
606 604

	
607 605
      arcs[n | 1].next_out = nodes[u._id].first_out;
608 606
      nodes[u._id].first_out = (n | 1);
609 607

	
610 608
      return Edge(n / 2);
611 609
    }
612 610

	
613 611
    void clear() {
614 612
      arcs.clear();
615 613
      nodes.clear();
616 614
    }
617 615

	
618 616
  };
619 617

	
620 618
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
621 619

	
622 620
  /// \ingroup graphs
623 621
  ///
624 622
  /// \brief A smart undirected graph class.
625 623
  ///
626 624
  /// This is a simple and fast graph implementation.
627 625
  /// It is also quite memory efficient, but at the price
628 626
  /// that <b> it does support only limited (only stack-like)
629 627
  /// node and arc deletions</b>.
630 628
  /// It fully conforms to the \ref concepts::Graph "Graph concept".
631 629
  ///
632 630
  /// \sa concepts::Graph.
633 631
  class SmartGraph : public ExtendedSmartGraphBase {
632
    typedef ExtendedSmartGraphBase Parent;
633

	
634 634
  private:
635 635

	
636 636
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
637 637

	
638 638
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
639 639
    ///
640 640
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
641 641

	
642 642
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
643 643
    ///Use GraphCopy() instead.
644 644

	
645 645
    ///Assignment of SmartGraph to another one is \e not allowed.
646 646
    ///Use GraphCopy() instead.
647 647
    void operator=(const SmartGraph &) {}
648 648

	
649 649
  public:
650 650

	
651
    typedef ExtendedSmartGraphBase Parent;
652

	
653 651
    /// Constructor
654 652

	
655 653
    /// Constructor.
656 654
    ///
657 655
    SmartGraph() {}
658 656

	
659 657
    ///Add a new node to the graph.
660 658

	
661 659
    /// Add a new node to the graph.
662 660
    /// \return The new node.
663 661
    Node addNode() { return Parent::addNode(); }
664 662

	
665 663
    ///Add a new edge to the graph.
666 664

	
667 665
    ///Add a new edge to the graph with node \c s
668 666
    ///and \c t.
669 667
    ///\return The new edge.
670 668
    Edge addEdge(const Node& s, const Node& t) {
671 669
      return Parent::addEdge(s, t);
672 670
    }
673 671

	
674 672
    /// \brief Node validity check
675 673
    ///
676 674
    /// This function gives back true if the given node is valid,
677 675
    /// ie. it is a real node of the graph.
678 676
    ///
679 677
    /// \warning A removed node (using Snapshot) could become valid again
680 678
    /// when new nodes are added to the graph.
681 679
    bool valid(Node n) const { return Parent::valid(n); }
682 680

	
683 681
    /// \brief Arc validity check
684 682
    ///
685 683
    /// This function gives back true if the given arc is valid,
686 684
    /// ie. it is a real arc of the graph.
687 685
    ///
688 686
    /// \warning A removed arc (using Snapshot) could become valid again
689 687
    /// when new edges are added to the graph.
690 688
    bool valid(Arc a) const { return Parent::valid(a); }
691 689

	
692 690
    /// \brief Edge validity check
693 691
    ///
694 692
    /// This function gives back true if the given edge is valid,
695 693
    /// ie. it is a real edge of the graph.
696 694
    ///
697 695
    /// \warning A removed edge (using Snapshot) could become valid again
698 696
    /// when new edges are added to the graph.
699 697
    bool valid(Edge e) const { return Parent::valid(e); }
700 698

	
701 699
    ///Clear the graph.
702 700

	
703 701
    ///Erase all the nodes and edges from the graph.
704 702
    ///
705 703
    void clear() {
706 704
      Parent::clear();
707 705
    }
708 706

	
709 707
  public:
710 708

	
711 709
    class Snapshot;
712 710

	
713 711
  protected:
714 712

	
715 713
    void saveSnapshot(Snapshot &s)
716 714
    {
717 715
      s._graph = this;
718 716
      s.node_num = nodes.size();
719 717
      s.arc_num = arcs.size();
720 718
    }
721 719

	
722 720
    void restoreSnapshot(const Snapshot &s)
723 721
    {
724 722
      while(s.arc_num<arcs.size()) {
725 723
        int n=arcs.size()-1;
726 724
        Edge arc=edgeFromId(n/2);
727 725
        Parent::notifier(Edge()).erase(arc);
728 726
        std::vector<Arc> dir;
729 727
        dir.push_back(arcFromId(n));
730 728
        dir.push_back(arcFromId(n-1));
731 729
        Parent::notifier(Arc()).erase(dir);
732 730
        nodes[arcs[n-1].target].first_out=arcs[n].next_out;
733 731
        nodes[arcs[n].target].first_out=arcs[n-1].next_out;
734 732
        arcs.pop_back();
735 733
        arcs.pop_back();
736 734
      }
737 735
      while(s.node_num<nodes.size()) {
738 736
        int n=nodes.size()-1;
739 737
        Node node = nodeFromId(n);
740 738
        Parent::notifier(Node()).erase(node);
741 739
        nodes.pop_back();
742 740
      }
743 741
    }
744 742

	
745 743
  public:
746 744

	
747 745
    ///Class to make a snapshot of the digraph and to restrore to it later.
748 746

	
749 747
    ///Class to make a snapshot of the digraph and to restrore to it later.
750 748
    ///
751 749
    ///The newly added nodes and arcs can be removed using the
752 750
    ///restore() function.
753 751
    ///
754 752
    ///\note After you restore a state, you cannot restore
755 753
    ///a later state, in other word you cannot add again the arcs deleted
756 754
    ///by restore() using another one Snapshot instance.
757 755
    ///
758 756
    ///\warning If you do not use correctly the snapshot that can cause
759 757
    ///either broken program, invalid state of the digraph, valid but
760 758
    ///not the restored digraph or no change. Because the runtime performance
761 759
    ///the validity of the snapshot is not stored.
762 760
    class Snapshot
763 761
    {
764 762
      SmartGraph *_graph;
765 763
    protected:
766 764
      friend class SmartGraph;
767 765
      unsigned int node_num;
768 766
      unsigned int arc_num;
769 767
    public:
770 768
      ///Default constructor.
771 769

	
772 770
      ///Default constructor.
773 771
      ///To actually make a snapshot you must call save().
774 772
      ///
775 773
      Snapshot() : _graph(0) {}
776 774
      ///Constructor that immediately makes a snapshot
777 775

	
778 776
      ///This constructor immediately makes a snapshot of the digraph.
779 777
      ///\param graph The digraph we make a snapshot of.
780 778
      Snapshot(SmartGraph &graph) {
781 779
        graph.saveSnapshot(*this);
782 780
      }
783 781

	
784 782
      ///Make a snapshot.
785 783

	
786 784
      ///Make a snapshot of the graph.
787 785
      ///
788 786
      ///This function can be called more than once. In case of a repeated
789 787
      ///call, the previous snapshot gets lost.
790 788
      ///\param graph The digraph we make the snapshot of.
791 789
      void save(SmartGraph &graph)
792 790
      {
793 791
        graph.saveSnapshot(*this);
794 792
      }
795 793

	
796 794
      ///Undo the changes until a snapshot.
797 795

	
798 796
      ///Undo the changes until a snapshot created by save().
799 797
      ///
800 798
      ///\note After you restored a state, you cannot restore
801 799
      ///a later state, in other word you cannot add again the arcs deleted
802 800
      ///by restore().
803 801
      void restore()
804 802
      {
805 803
        _graph->restoreSnapshot(*this);
806 804
      }
807 805
    };
808 806
  };
809 807

	
810 808
} //namespace lemon
811 809

	
812 810

	
813 811
#endif //LEMON_SMART_GRAPH_H
0 comments (0 inline)