gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Make some graph member functions static (#311, #68)
0 6 0
default
6 files changed with 25 insertions and 25 deletions:
↑ Collapse diff ↑
Ignore white space 1536 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 40
    typedef Base Parent;
41 41

	
42 42
  public:
43 43

	
44 44
    typedef DigraphExtender Digraph;
45 45

	
46 46
    // Base extensions
47 47

	
48 48
    typedef typename Parent::Node Node;
49 49
    typedef typename Parent::Arc Arc;
50 50

	
51 51
    int maxId(Node) const {
52 52
      return Parent::maxNodeId();
53 53
    }
54 54

	
55 55
    int maxId(Arc) const {
56 56
      return Parent::maxArcId();
57 57
    }
58 58

	
59
    Node fromId(int id, Node) const {
59
    static Node fromId(int id, Node) {
60 60
      return Parent::nodeFromId(id);
61 61
    }
62 62

	
63
    Arc fromId(int id, Arc) const {
63
    static Arc fromId(int id, Arc) {
64 64
      return Parent::arcFromId(id);
65 65
    }
66 66

	
67 67
    Node oppositeNode(const Node &node, const Arc &arc) const {
68 68
      if (node == Parent::source(arc))
69 69
        return Parent::target(arc);
70 70
      else if(node == Parent::target(arc))
71 71
        return Parent::source(arc);
72 72
      else
73 73
        return INVALID;
74 74
    }
75 75

	
76 76
    // Alterable extension
77 77

	
78 78
    typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
79 79
    typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
80 80

	
81 81

	
82 82
  protected:
83 83

	
84 84
    mutable NodeNotifier node_notifier;
85 85
    mutable ArcNotifier arc_notifier;
86 86

	
87 87
  public:
88 88

	
89 89
    NodeNotifier& notifier(Node) const {
90 90
      return node_notifier;
91 91
    }
92 92

	
93 93
    ArcNotifier& notifier(Arc) const {
94 94
      return arc_notifier;
95 95
    }
96 96

	
97 97
    class NodeIt : public Node {
98 98
      const Digraph* _digraph;
99 99
    public:
100 100

	
101 101
      NodeIt() {}
102 102

	
103 103
      NodeIt(Invalid i) : Node(i) { }
104 104

	
105 105
      explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
106 106
        _digraph->first(static_cast<Node&>(*this));
107 107
      }
108 108

	
109 109
      NodeIt(const Digraph& digraph, const Node& node)
110 110
        : Node(node), _digraph(&digraph) {}
111 111

	
112 112
      NodeIt& operator++() {
113 113
        _digraph->next(*this);
114 114
        return *this;
115 115
      }
116 116

	
117 117
    };
118 118

	
119 119

	
120 120
    class ArcIt : public Arc {
121 121
      const Digraph* _digraph;
122 122
    public:
123 123

	
124 124
      ArcIt() { }
125 125

	
126 126
      ArcIt(Invalid i) : Arc(i) { }
127 127

	
128 128
      explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
129 129
        _digraph->first(static_cast<Arc&>(*this));
130 130
      }
131 131

	
132 132
      ArcIt(const Digraph& digraph, const Arc& arc) :
133 133
        Arc(arc), _digraph(&digraph) { }
134 134

	
135 135
      ArcIt& operator++() {
136 136
        _digraph->next(*this);
137 137
        return *this;
138 138
      }
139 139

	
140 140
    };
141 141

	
142 142

	
143 143
    class OutArcIt : public Arc {
144 144
      const Digraph* _digraph;
145 145
    public:
146 146

	
147 147
      OutArcIt() { }
148 148

	
149 149
      OutArcIt(Invalid i) : Arc(i) { }
150 150

	
151 151
      OutArcIt(const Digraph& digraph, const Node& node)
152 152
        : _digraph(&digraph) {
153 153
        _digraph->firstOut(*this, node);
154 154
      }
155 155

	
156 156
      OutArcIt(const Digraph& digraph, const Arc& arc)
157 157
        : Arc(arc), _digraph(&digraph) {}
158 158

	
159 159
      OutArcIt& operator++() {
160 160
        _digraph->nextOut(*this);
161 161
        return *this;
162 162
      }
163 163

	
164 164
    };
165 165

	
166 166

	
167 167
    class InArcIt : public Arc {
168 168
      const Digraph* _digraph;
169 169
    public:
170 170

	
171 171
      InArcIt() { }
172 172

	
173 173
      InArcIt(Invalid i) : Arc(i) { }
174 174

	
175 175
      InArcIt(const Digraph& digraph, const Node& node)
176 176
        : _digraph(&digraph) {
177 177
        _digraph->firstIn(*this, node);
178 178
      }
179 179

	
180 180
      InArcIt(const Digraph& digraph, const Arc& arc) :
181 181
        Arc(arc), _digraph(&digraph) {}
182 182

	
183 183
      InArcIt& operator++() {
184 184
        _digraph->nextIn(*this);
185 185
        return *this;
186 186
      }
187 187

	
188 188
    };
189 189

	
190 190
    // \brief Base node of the iterator
191 191
    //
192 192
    // Returns the base node (i.e. the source in this case) of the iterator
193 193
    Node baseNode(const OutArcIt &arc) const {
194 194
      return Parent::source(arc);
195 195
    }
196 196
    // \brief Running node of the iterator
197 197
    //
198 198
    // Returns the running node (i.e. the target in this case) of the
199 199
    // iterator
200 200
    Node runningNode(const OutArcIt &arc) const {
201 201
      return Parent::target(arc);
202 202
    }
203 203

	
204 204
    // \brief Base node of the iterator
205 205
    //
206 206
    // Returns the base node (i.e. the target in this case) of the iterator
207 207
    Node baseNode(const InArcIt &arc) const {
208 208
      return Parent::target(arc);
209 209
    }
210 210
    // \brief Running node of the iterator
211 211
    //
212 212
    // Returns the running node (i.e. the source in this case) of the
213 213
    // iterator
214 214
    Node runningNode(const InArcIt &arc) const {
215 215
      return Parent::source(arc);
216 216
    }
217 217

	
218 218

	
219 219
    template <typename _Value>
220 220
    class NodeMap
221 221
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
222 222
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
223 223

	
224 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 246
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
247 247

	
248 248
    public:
249 249
      explicit ArcMap(const Digraph& digraph)
250 250
        : Parent(digraph) {}
251 251
      ArcMap(const Digraph& digraph, const _Value& value)
252 252
        : Parent(digraph, value) {}
253 253

	
254 254
    private:
255 255
      ArcMap& operator=(const ArcMap& cmap) {
256 256
        return operator=<ArcMap>(cmap);
257 257
      }
258 258

	
259 259
      template <typename CMap>
260 260
      ArcMap& operator=(const CMap& cmap) {
261 261
        Parent::operator=(cmap);
262 262
        return *this;
263 263
      }
264 264
    };
265 265

	
266 266

	
267 267
    Node addNode() {
268 268
      Node node = Parent::addNode();
269 269
      notifier(Node()).add(node);
270 270
      return node;
271 271
    }
272 272

	
273 273
    Arc addArc(const Node& from, const Node& to) {
274 274
      Arc arc = Parent::addArc(from, to);
275 275
      notifier(Arc()).add(arc);
276 276
      return arc;
277 277
    }
278 278

	
279 279
    void clear() {
280 280
      notifier(Arc()).clear();
281 281
      notifier(Node()).clear();
282 282
      Parent::clear();
283 283
    }
284 284

	
285 285
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
286 286
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
287 287
      Parent::build(digraph, nodeRef, arcRef);
288 288
      notifier(Node()).build();
289 289
      notifier(Arc()).build();
290 290
    }
291 291

	
292 292
    void erase(const Node& node) {
293 293
      Arc arc;
294 294
      Parent::firstOut(arc, node);
295 295
      while (arc != INVALID ) {
296 296
        erase(arc);
297 297
        Parent::firstOut(arc, node);
298 298
      }
299 299

	
300 300
      Parent::firstIn(arc, node);
301 301
      while (arc != INVALID ) {
302 302
        erase(arc);
303 303
        Parent::firstIn(arc, node);
304 304
      }
305 305

	
306 306
      notifier(Node()).erase(node);
307 307
      Parent::erase(node);
308 308
    }
309 309

	
310 310
    void erase(const Arc& arc) {
311 311
      notifier(Arc()).erase(arc);
312 312
      Parent::erase(arc);
313 313
    }
314 314

	
315 315
    DigraphExtender() {
316 316
      node_notifier.setContainer(*this);
317 317
      arc_notifier.setContainer(*this);
318 318
    }
319 319

	
320 320

	
321 321
    ~DigraphExtender() {
322 322
      arc_notifier.clear();
323 323
      node_notifier.clear();
324 324
    }
325 325
  };
326 326

	
327 327
  // \ingroup _graphbits
328 328
  //
329 329
  // \brief Extender for the Graphs
330 330
  template <typename Base>
331 331
  class GraphExtender : public Base {
332 332
    typedef Base Parent;
333 333

	
334 334
  public:
335 335

	
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
    Node fromId(int id, Node) const {
358
    static Node fromId(int id, Node) {
359 359
      return Parent::nodeFromId(id);
360 360
    }
361 361

	
362
    Arc fromId(int id, Arc) const {
362
    static Arc fromId(int id, Arc) {
363 363
      return Parent::arcFromId(id);
364 364
    }
365 365

	
366
    Edge fromId(int id, Edge) const {
366
    static Edge fromId(int id, Edge) {
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 604
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
605 605

	
606 606
    public:
607 607
      explicit NodeMap(const Graph& graph)
608 608
        : Parent(graph) {}
609 609
      NodeMap(const Graph& graph, const _Value& value)
610 610
        : Parent(graph, value) {}
611 611

	
612 612
    private:
613 613
      NodeMap& operator=(const NodeMap& cmap) {
614 614
        return operator=<NodeMap>(cmap);
615 615
      }
616 616

	
617 617
      template <typename CMap>
618 618
      NodeMap& operator=(const CMap& cmap) {
619 619
        Parent::operator=(cmap);
620 620
        return *this;
621 621
      }
622 622

	
623 623
    };
624 624

	
625 625
    template <typename _Value>
626 626
    class ArcMap
627 627
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
628 628
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
629 629

	
630 630
    public:
631 631
      explicit ArcMap(const Graph& graph)
632 632
        : Parent(graph) {}
633 633
      ArcMap(const Graph& graph, const _Value& value)
634 634
        : Parent(graph, value) {}
635 635

	
636 636
    private:
637 637
      ArcMap& operator=(const ArcMap& cmap) {
638 638
        return operator=<ArcMap>(cmap);
639 639
      }
640 640

	
641 641
      template <typename CMap>
642 642
      ArcMap& operator=(const CMap& cmap) {
643 643
        Parent::operator=(cmap);
644 644
        return *this;
645 645
      }
646 646
    };
647 647

	
648 648

	
649 649
    template <typename _Value>
650 650
    class EdgeMap
651 651
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
652 652
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
653 653

	
654 654
    public:
655 655
      explicit EdgeMap(const Graph& graph)
656 656
        : Parent(graph) {}
657 657

	
658 658
      EdgeMap(const Graph& graph, const _Value& value)
659 659
        : Parent(graph, value) {}
660 660

	
661 661
    private:
662 662
      EdgeMap& operator=(const EdgeMap& cmap) {
663 663
        return operator=<EdgeMap>(cmap);
664 664
      }
665 665

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

	
672 672
    };
673 673

	
674 674
    // Alteration extension
675 675

	
676 676
    Node addNode() {
677 677
      Node node = Parent::addNode();
678 678
      notifier(Node()).add(node);
679 679
      return node;
680 680
    }
681 681

	
682 682
    Edge addEdge(const Node& from, const Node& to) {
683 683
      Edge edge = Parent::addEdge(from, to);
684 684
      notifier(Edge()).add(edge);
685 685
      std::vector<Arc> ev;
686 686
      ev.push_back(Parent::direct(edge, true));
687 687
      ev.push_back(Parent::direct(edge, false));
688 688
      notifier(Arc()).add(ev);
689 689
      return edge;
690 690
    }
691 691

	
692 692
    void clear() {
693 693
      notifier(Arc()).clear();
694 694
      notifier(Edge()).clear();
695 695
      notifier(Node()).clear();
696 696
      Parent::clear();
697 697
    }
698 698

	
699 699
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
700 700
    void build(const Graph& graph, NodeRefMap& nodeRef,
701 701
               EdgeRefMap& edgeRef) {
702 702
      Parent::build(graph, nodeRef, edgeRef);
703 703
      notifier(Node()).build();
704 704
      notifier(Edge()).build();
705 705
      notifier(Arc()).build();
706 706
    }
707 707

	
708 708
    void erase(const Node& node) {
709 709
      Arc arc;
710 710
      Parent::firstOut(arc, node);
711 711
      while (arc != INVALID ) {
712 712
        erase(arc);
713 713
        Parent::firstOut(arc, node);
714 714
      }
715 715

	
716 716
      Parent::firstIn(arc, node);
717 717
      while (arc != INVALID ) {
718 718
        erase(arc);
719 719
        Parent::firstIn(arc, node);
720 720
      }
721 721

	
722 722
      notifier(Node()).erase(node);
723 723
      Parent::erase(node);
724 724
    }
725 725

	
726 726
    void erase(const Edge& edge) {
727 727
      std::vector<Arc> av;
728 728
      av.push_back(Parent::direct(edge, true));
729 729
      av.push_back(Parent::direct(edge, false));
730 730
      notifier(Arc()).erase(av);
731 731
      notifier(Edge()).erase(edge);
732 732
      Parent::erase(edge);
733 733
    }
734 734

	
735 735
    GraphExtender() {
736 736
      node_notifier.setContainer(*this);
737 737
      arc_notifier.setContainer(*this);
738 738
      edge_notifier.setContainer(*this);
739 739
    }
740 740

	
741 741
    ~GraphExtender() {
742 742
      edge_notifier.clear();
743 743
      arc_notifier.clear();
744 744
      node_notifier.clear();
745 745
    }
746 746

	
747 747
  };
748 748

	
749 749
}
750 750

	
751 751
#endif
Ignore white space 1536 line context
... ...
@@ -102,1315 +102,1315 @@
102 102
      arcs[n].next_in = (*_nodes)[v].first_in;
103 103
      if ((*_nodes)[v].first_in != -1) {
104 104
        arcs[(*_nodes)[v].first_in].prev_in = n;
105 105
      }
106 106
      (*_nodes)[v].first_in = n;
107 107
      arcs[n].next_out = (*_nodes)[u].first_out;
108 108
      if ((*_nodes)[u].first_out != -1) {
109 109
        arcs[(*_nodes)[u].first_out].prev_out = n;
110 110
      }
111 111
      (*_nodes)[u].first_out = n;
112 112
      arcs[n].source = u;
113 113
      arcs[n].target = v;
114 114
      return Arc(n);
115 115
    }
116 116

	
117 117
    void erase(const Arc& arc) {
118 118
      int n = arc.id;
119 119
      if (arcs[n].prev_in != -1) {
120 120
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
121 121
      } else {
122 122
        (*_nodes)[arcs[n].target].first_in = arcs[n].next_in;
123 123
      }
124 124
      if (arcs[n].next_in != -1) {
125 125
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
126 126
      }
127 127

	
128 128
      if (arcs[n].prev_out != -1) {
129 129
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
130 130
      } else {
131 131
        (*_nodes)[arcs[n].source].first_out = arcs[n].next_out;
132 132
      }
133 133
      if (arcs[n].next_out != -1) {
134 134
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
135 135
      }
136 136

	
137 137
    }
138 138

	
139 139
    void clear() {
140 140
      Node node;
141 141
      for (first(node); node != INVALID; next(node)) {
142 142
        (*_nodes)[node].first_in = -1;
143 143
        (*_nodes)[node].first_out = -1;
144 144
      }
145 145
      arcs.clear();
146 146
      first_arc = -1;
147 147
      first_free_arc = -1;
148 148
    }
149 149

	
150 150
    void first(Node& node) const {
151 151
      _graph->first(node);
152 152
    }
153 153

	
154 154
    void next(Node& node) const {
155 155
      _graph->next(node);
156 156
    }
157 157

	
158 158
    void first(Arc& arc) const {
159 159
      Node node;
160 160
      first(node);
161 161
      while (node != INVALID && (*_nodes)[node].first_in == -1) {
162 162
        next(node);
163 163
      }
164 164
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
165 165
    }
166 166

	
167 167
    void next(Arc& arc) const {
168 168
      if (arcs[arc.id].next_in != -1) {
169 169
        arc.id = arcs[arc.id].next_in;
170 170
      } else {
171 171
        Node node = arcs[arc.id].target;
172 172
        next(node);
173 173
        while (node != INVALID && (*_nodes)[node].first_in == -1) {
174 174
          next(node);
175 175
        }
176 176
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
177 177
      }
178 178
    }
179 179

	
180 180
    void firstOut(Arc& arc, const Node& node) const {
181 181
      arc.id = (*_nodes)[node].first_out;
182 182
    }
183 183

	
184 184
    void nextOut(Arc& arc) const {
185 185
      arc.id = arcs[arc.id].next_out;
186 186
    }
187 187

	
188 188
    void firstIn(Arc& arc, const Node& node) const {
189 189
      arc.id = (*_nodes)[node].first_in;
190 190
    }
191 191

	
192 192
    void nextIn(Arc& arc) const {
193 193
      arc.id = arcs[arc.id].next_in;
194 194
    }
195 195

	
196 196
    int id(const Node& node) const { return _graph->id(node); }
197 197
    int id(const Arc& arc) const { return arc.id; }
198 198

	
199 199
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
200 200
    Arc arcFromId(int ix) const { return Arc(ix); }
201 201

	
202 202
    int maxNodeId() const { return _graph->maxNodeId(); };
203 203
    int maxArcId() const { return arcs.size() - 1; }
204 204

	
205 205
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
206 206
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
207 207

	
208 208
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
209 209

	
210 210
    NodeNotifier& notifier(Node) const {
211 211
      return _graph->notifier(Node());
212 212
    }
213 213

	
214 214
    template <typename V>
215 215
    class NodeMap : public GR::template NodeMap<V> {
216 216
      typedef typename GR::template NodeMap<V> Parent;
217 217

	
218 218
    public:
219 219

	
220 220
      explicit NodeMap(const ListArcSetBase<GR>& arcset)
221 221
        : Parent(*arcset._graph) {}
222 222

	
223 223
      NodeMap(const ListArcSetBase<GR>& arcset, const V& value)
224 224
        : Parent(*arcset._graph, value) {}
225 225

	
226 226
      NodeMap& operator=(const NodeMap& cmap) {
227 227
        return operator=<NodeMap>(cmap);
228 228
      }
229 229

	
230 230
      template <typename CMap>
231 231
      NodeMap& operator=(const CMap& cmap) {
232 232
        Parent::operator=(cmap);
233 233
        return *this;
234 234
      }
235 235
    };
236 236

	
237 237
  };
238 238

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

	
269 269
  public:
270 270

	
271 271
    typedef typename Parent::Node Node;
272 272
    typedef typename Parent::Arc Arc;
273 273

	
274 274
    typedef typename Parent::NodesImplBase NodesImplBase;
275 275

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

	
284 284
      Parent::firstIn(arc, node);
285 285
      while (arc != INVALID ) {
286 286
        erase(arc);
287 287
        Parent::firstIn(arc, node);
288 288
      }
289 289
    }
290 290

	
291 291
    void clearNodes() {
292 292
      Parent::clear();
293 293
    }
294 294

	
295 295
    class NodesImpl : public NodesImplBase {
296 296
      typedef NodesImplBase Parent;
297 297

	
298 298
    public:
299 299
      NodesImpl(const GR& graph, ListArcSet& arcset)
300 300
        : Parent(graph), _arcset(arcset) {}
301 301

	
302 302
      virtual ~NodesImpl() {}
303 303

	
304 304
    protected:
305 305

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

	
321 321
    private:
322 322
      ListArcSet& _arcset;
323 323
    };
324 324

	
325 325
    NodesImpl _nodes;
326 326

	
327 327
  public:
328 328

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

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

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

	
352 352
  };
353 353

	
354 354
  template <typename GR>
355 355
  class ListEdgeSetBase {
356 356
  public:
357 357

	
358 358
    typedef typename GR::Node Node;
359 359
    typedef typename GR::NodeIt NodeIt;
360 360

	
361 361
  protected:
362 362

	
363 363
    struct NodeT {
364 364
      int first_out;
365 365
      NodeT() : first_out(-1) {}
366 366
    };
367 367

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

	
371 371
    NodesImplBase* _nodes;
372 372

	
373 373
    struct ArcT {
374 374
      Node target;
375 375
      int prev_out, next_out;
376 376
      ArcT() : prev_out(-1), next_out(-1) {}
377 377
    };
378 378

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

	
381 381
    int first_arc;
382 382
    int first_free_arc;
383 383

	
384 384
    const GR* _graph;
385 385

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

	
391 391
  public:
392 392

	
393 393
    class Edge {
394 394
      friend class ListEdgeSetBase;
395 395
    protected:
396 396

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

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

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

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

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

	
425 425
    Node addNode() {
426 426
      LEMON_ASSERT(false,
427 427
        "This graph structure does not support node insertion");
428 428
      return INVALID; // avoid warning
429 429
    }
430 430

	
431 431
    Edge addEdge(const Node& u, const Node& v) {
432 432
      int n;
433 433

	
434 434
      if (first_free_arc == -1) {
435 435
        n = arcs.size();
436 436
        arcs.push_back(ArcT());
437 437
        arcs.push_back(ArcT());
438 438
      } else {
439 439
        n = first_free_arc;
440 440
        first_free_arc = arcs[n].next_out;
441 441
      }
442 442

	
443 443
      arcs[n].target = u;
444 444
      arcs[n | 1].target = v;
445 445

	
446 446
      arcs[n].next_out = (*_nodes)[v].first_out;
447 447
      if ((*_nodes)[v].first_out != -1) {
448 448
        arcs[(*_nodes)[v].first_out].prev_out = n;
449 449
      }
450 450
      (*_nodes)[v].first_out = n;
451 451
      arcs[n].prev_out = -1;
452 452

	
453 453
      if ((*_nodes)[u].first_out != -1) {
454 454
        arcs[(*_nodes)[u].first_out].prev_out = (n | 1);
455 455
      }
456 456
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
457 457
      (*_nodes)[u].first_out = (n | 1);
458 458
      arcs[n | 1].prev_out = -1;
459 459

	
460 460
      return Edge(n / 2);
461 461
    }
462 462

	
463 463
    void erase(const Edge& arc) {
464 464
      int n = arc.id * 2;
465 465

	
466 466
      if (arcs[n].next_out != -1) {
467 467
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
468 468
      }
469 469

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

	
476 476
      if (arcs[n | 1].next_out != -1) {
477 477
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
478 478
      }
479 479

	
480 480
      if (arcs[n | 1].prev_out != -1) {
481 481
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
482 482
      } else {
483 483
        (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
484 484
      }
485 485

	
486 486
      arcs[n].next_out = first_free_arc;
487 487
      first_free_arc = n;
488 488

	
489 489
    }
490 490

	
491 491
    void clear() {
492 492
      Node node;
493 493
      for (first(node); node != INVALID; next(node)) {
494 494
        (*_nodes)[node].first_out = -1;
495 495
      }
496 496
      arcs.clear();
497 497
      first_arc = -1;
498 498
      first_free_arc = -1;
499 499
    }
500 500

	
501 501
    void first(Node& node) const {
502 502
      _graph->first(node);
503 503
    }
504 504

	
505 505
    void next(Node& node) const {
506 506
      _graph->next(node);
507 507
    }
508 508

	
509 509
    void first(Arc& arc) const {
510 510
      Node node;
511 511
      first(node);
512 512
      while (node != INVALID && (*_nodes)[node].first_out == -1) {
513 513
        next(node);
514 514
      }
515 515
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
516 516
    }
517 517

	
518 518
    void next(Arc& arc) const {
519 519
      if (arcs[arc.id].next_out != -1) {
520 520
        arc.id = arcs[arc.id].next_out;
521 521
      } else {
522 522
        Node node = arcs[arc.id ^ 1].target;
523 523
        next(node);
524 524
        while(node != INVALID && (*_nodes)[node].first_out == -1) {
525 525
          next(node);
526 526
        }
527 527
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
528 528
      }
529 529
    }
530 530

	
531 531
    void first(Edge& edge) const {
532 532
      Node node;
533 533
      first(node);
534 534
      while (node != INVALID) {
535 535
        edge.id = (*_nodes)[node].first_out;
536 536
        while ((edge.id & 1) != 1) {
537 537
          edge.id = arcs[edge.id].next_out;
538 538
        }
539 539
        if (edge.id != -1) {
540 540
          edge.id /= 2;
541 541
          return;
542 542
        }
543 543
        next(node);
544 544
      }
545 545
      edge.id = -1;
546 546
    }
547 547

	
548 548
    void next(Edge& edge) const {
549 549
      Node node = arcs[edge.id * 2].target;
550 550
      edge.id = arcs[(edge.id * 2) | 1].next_out;
551 551
      while ((edge.id & 1) != 1) {
552 552
        edge.id = arcs[edge.id].next_out;
553 553
      }
554 554
      if (edge.id != -1) {
555 555
        edge.id /= 2;
556 556
        return;
557 557
      }
558 558
      next(node);
559 559
      while (node != INVALID) {
560 560
        edge.id = (*_nodes)[node].first_out;
561 561
        while ((edge.id & 1) != 1) {
562 562
          edge.id = arcs[edge.id].next_out;
563 563
        }
564 564
        if (edge.id != -1) {
565 565
          edge.id /= 2;
566 566
          return;
567 567
        }
568 568
        next(node);
569 569
      }
570 570
      edge.id = -1;
571 571
    }
572 572

	
573 573
    void firstOut(Arc& arc, const Node& node) const {
574 574
      arc.id = (*_nodes)[node].first_out;
575 575
    }
576 576

	
577 577
    void nextOut(Arc& arc) const {
578 578
      arc.id = arcs[arc.id].next_out;
579 579
    }
580 580

	
581 581
    void firstIn(Arc& arc, const Node& node) const {
582 582
      arc.id = (((*_nodes)[node].first_out) ^ 1);
583 583
      if (arc.id == -2) arc.id = -1;
584 584
    }
585 585

	
586 586
    void nextIn(Arc& arc) const {
587 587
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
588 588
      if (arc.id == -2) arc.id = -1;
589 589
    }
590 590

	
591 591
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
592 592
      int de = (*_nodes)[node].first_out;
593 593
      if (de != -1 ) {
594 594
        arc.id = de / 2;
595 595
        dir = ((de & 1) == 1);
596 596
      } else {
597 597
        arc.id = -1;
598 598
        dir = true;
599 599
      }
600 600
    }
601 601
    void nextInc(Edge &arc, bool& dir) const {
602 602
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
603 603
      if (de != -1 ) {
604 604
        arc.id = de / 2;
605 605
        dir = ((de & 1) == 1);
606 606
      } else {
607 607
        arc.id = -1;
608 608
        dir = true;
609 609
      }
610 610
    }
611 611

	
612 612
    static bool direction(Arc arc) {
613 613
      return (arc.id & 1) == 1;
614 614
    }
615 615

	
616 616
    static Arc direct(Edge edge, bool dir) {
617 617
      return Arc(edge.id * 2 + (dir ? 1 : 0));
618 618
    }
619 619

	
620 620
    int id(const Node& node) const { return _graph->id(node); }
621 621
    static int id(Arc e) { return e.id; }
622 622
    static int id(Edge e) { return e.id; }
623 623

	
624 624
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
625 625
    static Arc arcFromId(int id) { return Arc(id);}
626 626
    static Edge edgeFromId(int id) { return Edge(id);}
627 627

	
628 628
    int maxNodeId() const { return _graph->maxNodeId(); };
629 629
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
630 630
    int maxArcId() const { return arcs.size()-1; }
631 631

	
632 632
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
633 633
    Node target(Arc e) const { return arcs[e.id].target; }
634 634

	
635 635
    Node u(Edge e) const { return arcs[2 * e.id].target; }
636 636
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
637 637

	
638 638
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
639 639

	
640 640
    NodeNotifier& notifier(Node) const {
641 641
      return _graph->notifier(Node());
642 642
    }
643 643

	
644 644
    template <typename V>
645 645
    class NodeMap : public GR::template NodeMap<V> {
646 646
      typedef typename GR::template NodeMap<V> Parent;
647 647

	
648 648
    public:
649 649

	
650 650
      explicit NodeMap(const ListEdgeSetBase<GR>& arcset)
651 651
        : Parent(*arcset._graph) {}
652 652

	
653 653
      NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value)
654 654
        : Parent(*arcset._graph, value) {}
655 655

	
656 656
      NodeMap& operator=(const NodeMap& cmap) {
657 657
        return operator=<NodeMap>(cmap);
658 658
      }
659 659

	
660 660
      template <typename CMap>
661 661
      NodeMap& operator=(const CMap& cmap) {
662 662
        Parent::operator=(cmap);
663 663
        return *this;
664 664
      }
665 665
    };
666 666

	
667 667
  };
668 668

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

	
699 699
  public:
700 700

	
701 701
    typedef typename Parent::Node Node;
702 702
    typedef typename Parent::Arc Arc;
703 703
    typedef typename Parent::Edge Edge;
704 704

	
705 705
    typedef typename Parent::NodesImplBase NodesImplBase;
706 706

	
707 707
    void eraseNode(const Node& node) {
708 708
      Arc arc;
709 709
      Parent::firstOut(arc, node);
710 710
      while (arc != INVALID ) {
711 711
        erase(arc);
712 712
        Parent::firstOut(arc, node);
713 713
      }
714 714

	
715 715
    }
716 716

	
717 717
    void clearNodes() {
718 718
      Parent::clear();
719 719
    }
720 720

	
721 721
    class NodesImpl : public NodesImplBase {
722 722
      typedef NodesImplBase Parent;
723 723

	
724 724
    public:
725 725
      NodesImpl(const GR& graph, ListEdgeSet& arcset)
726 726
        : Parent(graph), _arcset(arcset) {}
727 727

	
728 728
      virtual ~NodesImpl() {}
729 729

	
730 730
    protected:
731 731

	
732 732
      virtual void erase(const Node& node) {
733 733
        _arcset.eraseNode(node);
734 734
        Parent::erase(node);
735 735
      }
736 736
      virtual void erase(const std::vector<Node>& nodes) {
737 737
        for (int i = 0; i < int(nodes.size()); ++i) {
738 738
          _arcset.eraseNode(nodes[i]);
739 739
        }
740 740
        Parent::erase(nodes);
741 741
      }
742 742
      virtual void clear() {
743 743
        _arcset.clearNodes();
744 744
        Parent::clear();
745 745
      }
746 746

	
747 747
    private:
748 748
      ListEdgeSet& _arcset;
749 749
    };
750 750

	
751 751
    NodesImpl _nodes;
752 752

	
753 753
  public:
754 754

	
755 755
    /// \brief Constructor of the EdgeSet.
756 756
    ///
757 757
    /// Constructor of the EdgeSet.
758 758
    ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
759 759
      Parent::initalize(graph, _nodes);
760 760
    }
761 761

	
762 762
    /// \brief Add a new edge to the graph.
763 763
    ///
764 764
    /// Add a new edge to the graph with node \c u
765 765
    /// and node \c v endpoints.
766 766
    /// \return The new edge.
767 767
    Edge addEdge(const Node& u, const Node& v) {
768 768
      return Parent::addEdge(u, v);
769 769
    }
770 770

	
771 771
    /// \brief Erase an edge from the graph.
772 772
    ///
773 773
    /// Erase the edge \c e from the graph.
774 774
    void erase(const Edge& e) {
775 775
      return Parent::erase(e);
776 776
    }
777 777

	
778 778
  };
779 779

	
780 780
  template <typename GR>
781 781
  class SmartArcSetBase {
782 782
  public:
783 783

	
784 784
    typedef typename GR::Node Node;
785 785
    typedef typename GR::NodeIt NodeIt;
786 786

	
787 787
  protected:
788 788

	
789 789
    struct NodeT {
790 790
      int first_out, first_in;
791 791
      NodeT() : first_out(-1), first_in(-1) {}
792 792
    };
793 793

	
794 794
    typedef typename ItemSetTraits<GR, Node>::
795 795
    template Map<NodeT>::Type NodesImplBase;
796 796

	
797 797
    NodesImplBase* _nodes;
798 798

	
799 799
    struct ArcT {
800 800
      Node source, target;
801 801
      int next_out, next_in;
802 802
      ArcT() {}
803 803
    };
804 804

	
805 805
    std::vector<ArcT> arcs;
806 806

	
807 807
    const GR* _graph;
808 808

	
809 809
    void initalize(const GR& graph, NodesImplBase& nodes) {
810 810
      _graph = &graph;
811 811
      _nodes = &nodes;
812 812
    }
813 813

	
814 814
  public:
815 815

	
816 816
    class Arc {
817 817
      friend class SmartArcSetBase<GR>;
818 818
    protected:
819 819
      Arc(int _id) : id(_id) {}
820 820
      int id;
821 821
    public:
822 822
      Arc() {}
823 823
      Arc(Invalid) : id(-1) {}
824 824
      bool operator==(const Arc& arc) const { return id == arc.id; }
825 825
      bool operator!=(const Arc& arc) const { return id != arc.id; }
826 826
      bool operator<(const Arc& arc) const { return id < arc.id; }
827 827
    };
828 828

	
829 829
    SmartArcSetBase() {}
830 830

	
831 831
    Node addNode() {
832 832
      LEMON_ASSERT(false,
833 833
        "This graph structure does not support node insertion");
834 834
      return INVALID; // avoid warning
835 835
    }
836 836

	
837 837
    Arc addArc(const Node& u, const Node& v) {
838 838
      int n = arcs.size();
839 839
      arcs.push_back(ArcT());
840 840
      arcs[n].next_in = (*_nodes)[v].first_in;
841 841
      (*_nodes)[v].first_in = n;
842 842
      arcs[n].next_out = (*_nodes)[u].first_out;
843 843
      (*_nodes)[u].first_out = n;
844 844
      arcs[n].source = u;
845 845
      arcs[n].target = v;
846 846
      return Arc(n);
847 847
    }
848 848

	
849 849
    void clear() {
850 850
      Node node;
851 851
      for (first(node); node != INVALID; next(node)) {
852 852
        (*_nodes)[node].first_in = -1;
853 853
        (*_nodes)[node].first_out = -1;
854 854
      }
855 855
      arcs.clear();
856 856
    }
857 857

	
858 858
    void first(Node& node) const {
859 859
      _graph->first(node);
860 860
    }
861 861

	
862 862
    void next(Node& node) const {
863 863
      _graph->next(node);
864 864
    }
865 865

	
866 866
    void first(Arc& arc) const {
867 867
      arc.id = arcs.size() - 1;
868 868
    }
869 869

	
870
    void next(Arc& arc) const {
870
    static void next(Arc& arc) {
871 871
      --arc.id;
872 872
    }
873 873

	
874 874
    void firstOut(Arc& arc, const Node& node) const {
875 875
      arc.id = (*_nodes)[node].first_out;
876 876
    }
877 877

	
878 878
    void nextOut(Arc& arc) const {
879 879
      arc.id = arcs[arc.id].next_out;
880 880
    }
881 881

	
882 882
    void firstIn(Arc& arc, const Node& node) const {
883 883
      arc.id = (*_nodes)[node].first_in;
884 884
    }
885 885

	
886 886
    void nextIn(Arc& arc) const {
887 887
      arc.id = arcs[arc.id].next_in;
888 888
    }
889 889

	
890 890
    int id(const Node& node) const { return _graph->id(node); }
891 891
    int id(const Arc& arc) const { return arc.id; }
892 892

	
893 893
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
894 894
    Arc arcFromId(int ix) const { return Arc(ix); }
895 895

	
896 896
    int maxNodeId() const { return _graph->maxNodeId(); };
897 897
    int maxArcId() const { return arcs.size() - 1; }
898 898

	
899 899
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
900 900
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
901 901

	
902 902
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
903 903

	
904 904
    NodeNotifier& notifier(Node) const {
905 905
      return _graph->notifier(Node());
906 906
    }
907 907

	
908 908
    template <typename V>
909 909
    class NodeMap : public GR::template NodeMap<V> {
910 910
      typedef typename GR::template NodeMap<V> Parent;
911 911

	
912 912
    public:
913 913

	
914 914
      explicit NodeMap(const SmartArcSetBase<GR>& arcset)
915 915
        : Parent(*arcset._graph) { }
916 916

	
917 917
      NodeMap(const SmartArcSetBase<GR>& arcset, const V& value)
918 918
        : Parent(*arcset._graph, value) { }
919 919

	
920 920
      NodeMap& operator=(const NodeMap& cmap) {
921 921
        return operator=<NodeMap>(cmap);
922 922
      }
923 923

	
924 924
      template <typename CMap>
925 925
      NodeMap& operator=(const CMap& cmap) {
926 926
        Parent::operator=(cmap);
927 927
        return *this;
928 928
      }
929 929
    };
930 930

	
931 931
  };
932 932

	
933 933

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

	
968 968
  public:
969 969

	
970 970
    typedef typename Parent::Node Node;
971 971
    typedef typename Parent::Arc Arc;
972 972

	
973 973
  protected:
974 974

	
975 975
    typedef typename Parent::NodesImplBase NodesImplBase;
976 976

	
977 977
    void eraseNode(const Node& node) {
978 978
      if (typename Parent::InArcIt(*this, node) == INVALID &&
979 979
          typename Parent::OutArcIt(*this, node) == INVALID) {
980 980
        return;
981 981
      }
982 982
      throw typename NodesImplBase::Notifier::ImmediateDetach();
983 983
    }
984 984

	
985 985
    void clearNodes() {
986 986
      Parent::clear();
987 987
    }
988 988

	
989 989
    class NodesImpl : public NodesImplBase {
990 990
      typedef NodesImplBase Parent;
991 991

	
992 992
    public:
993 993
      NodesImpl(const GR& graph, SmartArcSet& arcset)
994 994
        : Parent(graph), _arcset(arcset) {}
995 995

	
996 996
      virtual ~NodesImpl() {}
997 997

	
998 998
      bool attached() const {
999 999
        return Parent::attached();
1000 1000
      }
1001 1001

	
1002 1002
    protected:
1003 1003

	
1004 1004
      virtual void erase(const Node& node) {
1005 1005
        try {
1006 1006
          _arcset.eraseNode(node);
1007 1007
          Parent::erase(node);
1008 1008
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1009 1009
          Parent::clear();
1010 1010
          throw;
1011 1011
        }
1012 1012
      }
1013 1013
      virtual void erase(const std::vector<Node>& nodes) {
1014 1014
        try {
1015 1015
          for (int i = 0; i < int(nodes.size()); ++i) {
1016 1016
            _arcset.eraseNode(nodes[i]);
1017 1017
          }
1018 1018
          Parent::erase(nodes);
1019 1019
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1020 1020
          Parent::clear();
1021 1021
          throw;
1022 1022
        }
1023 1023
      }
1024 1024
      virtual void clear() {
1025 1025
        _arcset.clearNodes();
1026 1026
        Parent::clear();
1027 1027
      }
1028 1028

	
1029 1029
    private:
1030 1030
      SmartArcSet& _arcset;
1031 1031
    };
1032 1032

	
1033 1033
    NodesImpl _nodes;
1034 1034

	
1035 1035
  public:
1036 1036

	
1037 1037
    /// \brief Constructor of the ArcSet.
1038 1038
    ///
1039 1039
    /// Constructor of the ArcSet.
1040 1040
    SmartArcSet(const GR& graph) : _nodes(graph, *this) {
1041 1041
      Parent::initalize(graph, _nodes);
1042 1042
    }
1043 1043

	
1044 1044
    /// \brief Add a new arc to the digraph.
1045 1045
    ///
1046 1046
    /// Add a new arc to the digraph with source node \c s
1047 1047
    /// and target node \c t.
1048 1048
    /// \return The new arc.
1049 1049
    Arc addArc(const Node& s, const Node& t) {
1050 1050
      return Parent::addArc(s, t);
1051 1051
    }
1052 1052

	
1053 1053
    /// \brief Validity check
1054 1054
    ///
1055 1055
    /// This functions gives back false if the ArcSet is
1056 1056
    /// invalidated. It occurs when a node in the underlying graph is
1057 1057
    /// erased and it is not isolated in the ArcSet.
1058 1058
    bool valid() const {
1059 1059
      return _nodes.attached();
1060 1060
    }
1061 1061

	
1062 1062
  };
1063 1063

	
1064 1064

	
1065 1065
  template <typename GR>
1066 1066
  class SmartEdgeSetBase {
1067 1067
  public:
1068 1068

	
1069 1069
    typedef typename GR::Node Node;
1070 1070
    typedef typename GR::NodeIt NodeIt;
1071 1071

	
1072 1072
  protected:
1073 1073

	
1074 1074
    struct NodeT {
1075 1075
      int first_out;
1076 1076
      NodeT() : first_out(-1) {}
1077 1077
    };
1078 1078

	
1079 1079
    typedef typename ItemSetTraits<GR, Node>::
1080 1080
    template Map<NodeT>::Type NodesImplBase;
1081 1081

	
1082 1082
    NodesImplBase* _nodes;
1083 1083

	
1084 1084
    struct ArcT {
1085 1085
      Node target;
1086 1086
      int next_out;
1087 1087
      ArcT() {}
1088 1088
    };
1089 1089

	
1090 1090
    std::vector<ArcT> arcs;
1091 1091

	
1092 1092
    const GR* _graph;
1093 1093

	
1094 1094
    void initalize(const GR& graph, NodesImplBase& nodes) {
1095 1095
      _graph = &graph;
1096 1096
      _nodes = &nodes;
1097 1097
    }
1098 1098

	
1099 1099
  public:
1100 1100

	
1101 1101
    class Edge {
1102 1102
      friend class SmartEdgeSetBase;
1103 1103
    protected:
1104 1104

	
1105 1105
      int id;
1106 1106
      explicit Edge(int _id) { id = _id;}
1107 1107

	
1108 1108
    public:
1109 1109
      Edge() {}
1110 1110
      Edge (Invalid) { id = -1; }
1111 1111
      bool operator==(const Edge& arc) const {return id == arc.id;}
1112 1112
      bool operator!=(const Edge& arc) const {return id != arc.id;}
1113 1113
      bool operator<(const Edge& arc) const {return id < arc.id;}
1114 1114
    };
1115 1115

	
1116 1116
    class Arc {
1117 1117
      friend class SmartEdgeSetBase;
1118 1118
    protected:
1119 1119
      Arc(int _id) : id(_id) {}
1120 1120
      int id;
1121 1121
    public:
1122 1122
      operator Edge() const { return edgeFromId(id / 2); }
1123 1123

	
1124 1124
      Arc() {}
1125 1125
      Arc(Invalid) : id(-1) {}
1126 1126
      bool operator==(const Arc& arc) const { return id == arc.id; }
1127 1127
      bool operator!=(const Arc& arc) const { return id != arc.id; }
1128 1128
      bool operator<(const Arc& arc) const { return id < arc.id; }
1129 1129
    };
1130 1130

	
1131 1131
    SmartEdgeSetBase() {}
1132 1132

	
1133 1133
    Node addNode() {
1134 1134
      LEMON_ASSERT(false,
1135 1135
        "This graph structure does not support node insertion");
1136 1136
      return INVALID; // avoid warning
1137 1137
    }
1138 1138

	
1139 1139
    Edge addEdge(const Node& u, const Node& v) {
1140 1140
      int n = arcs.size();
1141 1141
      arcs.push_back(ArcT());
1142 1142
      arcs.push_back(ArcT());
1143 1143

	
1144 1144
      arcs[n].target = u;
1145 1145
      arcs[n | 1].target = v;
1146 1146

	
1147 1147
      arcs[n].next_out = (*_nodes)[v].first_out;
1148 1148
      (*_nodes)[v].first_out = n;
1149 1149

	
1150 1150
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
1151 1151
      (*_nodes)[u].first_out = (n | 1);
1152 1152

	
1153 1153
      return Edge(n / 2);
1154 1154
    }
1155 1155

	
1156 1156
    void clear() {
1157 1157
      Node node;
1158 1158
      for (first(node); node != INVALID; next(node)) {
1159 1159
        (*_nodes)[node].first_out = -1;
1160 1160
      }
1161 1161
      arcs.clear();
1162 1162
    }
1163 1163

	
1164 1164
    void first(Node& node) const {
1165 1165
      _graph->first(node);
1166 1166
    }
1167 1167

	
1168 1168
    void next(Node& node) const {
1169 1169
      _graph->next(node);
1170 1170
    }
1171 1171

	
1172 1172
    void first(Arc& arc) const {
1173 1173
      arc.id = arcs.size() - 1;
1174 1174
    }
1175 1175

	
1176
    void next(Arc& arc) const {
1176
    static void next(Arc& arc) {
1177 1177
      --arc.id;
1178 1178
    }
1179 1179

	
1180 1180
    void first(Edge& arc) const {
1181 1181
      arc.id = arcs.size() / 2 - 1;
1182 1182
    }
1183 1183

	
1184
    void next(Edge& arc) const {
1184
    static void next(Edge& arc) {
1185 1185
      --arc.id;
1186 1186
    }
1187 1187

	
1188 1188
    void firstOut(Arc& arc, const Node& node) const {
1189 1189
      arc.id = (*_nodes)[node].first_out;
1190 1190
    }
1191 1191

	
1192 1192
    void nextOut(Arc& arc) const {
1193 1193
      arc.id = arcs[arc.id].next_out;
1194 1194
    }
1195 1195

	
1196 1196
    void firstIn(Arc& arc, const Node& node) const {
1197 1197
      arc.id = (((*_nodes)[node].first_out) ^ 1);
1198 1198
      if (arc.id == -2) arc.id = -1;
1199 1199
    }
1200 1200

	
1201 1201
    void nextIn(Arc& arc) const {
1202 1202
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
1203 1203
      if (arc.id == -2) arc.id = -1;
1204 1204
    }
1205 1205

	
1206 1206
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
1207 1207
      int de = (*_nodes)[node].first_out;
1208 1208
      if (de != -1 ) {
1209 1209
        arc.id = de / 2;
1210 1210
        dir = ((de & 1) == 1);
1211 1211
      } else {
1212 1212
        arc.id = -1;
1213 1213
        dir = true;
1214 1214
      }
1215 1215
    }
1216 1216
    void nextInc(Edge &arc, bool& dir) const {
1217 1217
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
1218 1218
      if (de != -1 ) {
1219 1219
        arc.id = de / 2;
1220 1220
        dir = ((de & 1) == 1);
1221 1221
      } else {
1222 1222
        arc.id = -1;
1223 1223
        dir = true;
1224 1224
      }
1225 1225
    }
1226 1226

	
1227 1227
    static bool direction(Arc arc) {
1228 1228
      return (arc.id & 1) == 1;
1229 1229
    }
1230 1230

	
1231 1231
    static Arc direct(Edge edge, bool dir) {
1232 1232
      return Arc(edge.id * 2 + (dir ? 1 : 0));
1233 1233
    }
1234 1234

	
1235 1235
    int id(Node node) const { return _graph->id(node); }
1236 1236
    static int id(Arc arc) { return arc.id; }
1237 1237
    static int id(Edge arc) { return arc.id; }
1238 1238

	
1239 1239
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
1240 1240
    static Arc arcFromId(int id) { return Arc(id); }
1241 1241
    static Edge edgeFromId(int id) { return Edge(id);}
1242 1242

	
1243 1243
    int maxNodeId() const { return _graph->maxNodeId(); };
1244 1244
    int maxArcId() const { return arcs.size() - 1; }
1245 1245
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
1246 1246

	
1247 1247
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
1248 1248
    Node target(Arc e) const { return arcs[e.id].target; }
1249 1249

	
1250 1250
    Node u(Edge e) const { return arcs[2 * e.id].target; }
1251 1251
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
1252 1252

	
1253 1253
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
1254 1254

	
1255 1255
    NodeNotifier& notifier(Node) const {
1256 1256
      return _graph->notifier(Node());
1257 1257
    }
1258 1258

	
1259 1259
    template <typename V>
1260 1260
    class NodeMap : public GR::template NodeMap<V> {
1261 1261
      typedef typename GR::template NodeMap<V> Parent;
1262 1262

	
1263 1263
    public:
1264 1264

	
1265 1265
      explicit NodeMap(const SmartEdgeSetBase<GR>& arcset)
1266 1266
        : Parent(*arcset._graph) { }
1267 1267

	
1268 1268
      NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value)
1269 1269
        : Parent(*arcset._graph, value) { }
1270 1270

	
1271 1271
      NodeMap& operator=(const NodeMap& cmap) {
1272 1272
        return operator=<NodeMap>(cmap);
1273 1273
      }
1274 1274

	
1275 1275
      template <typename CMap>
1276 1276
      NodeMap& operator=(const CMap& cmap) {
1277 1277
        Parent::operator=(cmap);
1278 1278
        return *this;
1279 1279
      }
1280 1280
    };
1281 1281

	
1282 1282
  };
1283 1283

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

	
1318 1318
  public:
1319 1319

	
1320 1320
    typedef typename Parent::Node Node;
1321 1321
    typedef typename Parent::Arc Arc;
1322 1322
    typedef typename Parent::Edge Edge;
1323 1323

	
1324 1324
  protected:
1325 1325

	
1326 1326
    typedef typename Parent::NodesImplBase NodesImplBase;
1327 1327

	
1328 1328
    void eraseNode(const Node& node) {
1329 1329
      if (typename Parent::IncEdgeIt(*this, node) == INVALID) {
1330 1330
        return;
1331 1331
      }
1332 1332
      throw typename NodesImplBase::Notifier::ImmediateDetach();
1333 1333
    }
1334 1334

	
1335 1335
    void clearNodes() {
1336 1336
      Parent::clear();
1337 1337
    }
1338 1338

	
1339 1339
    class NodesImpl : public NodesImplBase {
1340 1340
      typedef NodesImplBase Parent;
1341 1341

	
1342 1342
    public:
1343 1343
      NodesImpl(const GR& graph, SmartEdgeSet& arcset)
1344 1344
        : Parent(graph), _arcset(arcset) {}
1345 1345

	
1346 1346
      virtual ~NodesImpl() {}
1347 1347

	
1348 1348
      bool attached() const {
1349 1349
        return Parent::attached();
1350 1350
      }
1351 1351

	
1352 1352
    protected:
1353 1353

	
1354 1354
      virtual void erase(const Node& node) {
1355 1355
        try {
1356 1356
          _arcset.eraseNode(node);
1357 1357
          Parent::erase(node);
1358 1358
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1359 1359
          Parent::clear();
1360 1360
          throw;
1361 1361
        }
1362 1362
      }
1363 1363
      virtual void erase(const std::vector<Node>& nodes) {
1364 1364
        try {
1365 1365
          for (int i = 0; i < int(nodes.size()); ++i) {
1366 1366
            _arcset.eraseNode(nodes[i]);
1367 1367
          }
1368 1368
          Parent::erase(nodes);
1369 1369
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1370 1370
          Parent::clear();
1371 1371
          throw;
1372 1372
        }
1373 1373
      }
1374 1374
      virtual void clear() {
1375 1375
        _arcset.clearNodes();
1376 1376
        Parent::clear();
1377 1377
      }
1378 1378

	
1379 1379
    private:
1380 1380
      SmartEdgeSet& _arcset;
1381 1381
    };
1382 1382

	
1383 1383
    NodesImpl _nodes;
1384 1384

	
1385 1385
  public:
1386 1386

	
1387 1387
    /// \brief Constructor of the EdgeSet.
1388 1388
    ///
1389 1389
    /// Constructor of the EdgeSet.
1390 1390
    SmartEdgeSet(const GR& graph) : _nodes(graph, *this) {
1391 1391
      Parent::initalize(graph, _nodes);
1392 1392
    }
1393 1393

	
1394 1394
    /// \brief Add a new edge to the graph.
1395 1395
    ///
1396 1396
    /// Add a new edge to the graph with node \c u
1397 1397
    /// and node \c v endpoints.
1398 1398
    /// \return The new edge.
1399 1399
    Edge addEdge(const Node& u, const Node& v) {
1400 1400
      return Parent::addEdge(u, v);
1401 1401
    }
1402 1402

	
1403 1403
    /// \brief Validity check
1404 1404
    ///
1405 1405
    /// This functions gives back false if the EdgeSet is
1406 1406
    /// invalidated. It occurs when a node in the underlying graph is
1407 1407
    /// erased and it is not isolated in the EdgeSet.
1408 1408
    bool valid() const {
1409 1409
      return _nodes.attached();
1410 1410
    }
1411 1411

	
1412 1412
  };
1413 1413

	
1414 1414
}
1415 1415

	
1416 1416
#endif
Ignore white space 1536 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 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
    int index(const Node& node) const { return node._id; }
54
    static int index(const Node& node) { 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 172
    typedef ExtendedFullDigraphBase Parent;
173 173

	
174 174
  public:
175 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
    int index(const Node& node) const { return Parent::index(node); }
212
    static int index(const Node& node) { 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 229
  public:
230 230

	
231 231
    typedef FullGraphBase Graph;
232 232

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

	
237 237
  protected:
238 238

	
239 239
    int _node_num;
240 240
    int _edge_num;
241 241

	
242 242
    FullGraphBase() {}
243 243

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

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

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

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

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

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

	
283 283
  public:
284 284

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
375 375
    protected:
376 376
      int _id;
377 377

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

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

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

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

	
392 392
    protected:
393 393
      int _id;
394 394

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
515 515
  };
516 516

	
517 517
  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
518 518

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

	
543 543
  public:
544 544

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

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

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

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

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

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

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

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

	
606 606
  };
607 607

	
608 608

	
609 609
} //namespace lemon
610 610

	
611 611

	
612 612
#endif //LEMON_FULL_GRAPH_H
Ignore white space 1536 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
    int index(Node node) const {
265
    static int index(Node node) {
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 297
    typedef ExtendedHypercubeGraphBase Parent;
298 298

	
299 299
  public:
300 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
    int index(Node node) const {
340
    static int index(Node node) {
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 1536 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 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 198
    typedef ExtendedSmartDigraphBase Parent;
199 199

	
200 200
  private:
201 201

	
202 202
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
203 203

	
204 204
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
205 205
    ///
206 206
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
207 207
    ///\brief Assignment of SmartDigraph to another one is \e not allowed.
208 208
    ///Use DigraphCopy() instead.
209 209

	
210 210
    ///Assignment of SmartDigraph to another one is \e not allowed.
211 211
    ///Use DigraphCopy() instead.
212 212
    void operator=(const SmartDigraph &) {}
213 213

	
214 214
  public:
215 215

	
216 216
    /// Constructor
217 217

	
218 218
    /// Constructor.
219 219
    ///
220 220
    SmartDigraph() {};
221 221

	
222 222
    ///Add a new node to the digraph.
223 223

	
224 224
    /// Add a new node to the digraph.
225 225
    /// \return The new node.
226 226
    Node addNode() { return Parent::addNode(); }
227 227

	
228 228
    ///Add a new arc to the digraph.
229 229

	
230 230
    ///Add a new arc to the digraph with source node \c s
231 231
    ///and target node \c t.
232 232
    ///\return The new arc.
233 233
    Arc addArc(const Node& s, const Node& t) {
234 234
      return Parent::addArc(s, t);
235 235
    }
236 236

	
237 237
    /// \brief Using this it is possible to avoid the superfluous memory
238 238
    /// allocation.
239 239

	
240 240
    /// Using this it is possible to avoid the superfluous memory
241 241
    /// allocation: if you know that the digraph you want to build will
242 242
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
243 243
    /// then it is worth reserving space for this amount before starting
244 244
    /// to build the digraph.
245 245
    /// \sa reserveArc
246 246
    void reserveNode(int n) { nodes.reserve(n); };
247 247

	
248 248
    /// \brief Using this it is possible to avoid the superfluous memory
249 249
    /// allocation.
250 250

	
251 251
    /// Using this it is possible to avoid the superfluous memory
252 252
    /// allocation: if you know that the digraph you want to build will
253 253
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
254 254
    /// then it is worth reserving space for this amount before starting
255 255
    /// to build the digraph.
256 256
    /// \sa reserveNode
257 257
    void reserveArc(int m) { arcs.reserve(m); };
258 258

	
259 259
    /// \brief Node validity check
260 260
    ///
261 261
    /// This function gives back true if the given node is valid,
262 262
    /// ie. it is a real node of the graph.
263 263
    ///
264 264
    /// \warning A removed node (using Snapshot) could become valid again
265 265
    /// when new nodes are added to the graph.
266 266
    bool valid(Node n) const { return Parent::valid(n); }
267 267

	
268 268
    /// \brief Arc validity check
269 269
    ///
270 270
    /// This function gives back true if the given arc is valid,
271 271
    /// ie. it is a real arc of the graph.
272 272
    ///
273 273
    /// \warning A removed arc (using Snapshot) could become valid again
274 274
    /// when new arcs are added to the graph.
275 275
    bool valid(Arc a) const { return Parent::valid(a); }
276 276

	
277 277
    ///Clear the digraph.
278 278

	
279 279
    ///Erase all the nodes and arcs from the digraph.
280 280
    ///
281 281
    void clear() {
282 282
      Parent::clear();
283 283
    }
284 284

	
285 285
    ///Split a node.
286 286

	
287 287
    ///This function splits a node. First a new node is added to the digraph,
288 288
    ///then the source of each outgoing arc of \c n is moved to this new node.
289 289
    ///If \c connect is \c true (this is the default value), then a new arc
290 290
    ///from \c n to the newly created node is also added.
291 291
    ///\return The newly created node.
292 292
    ///
293 293
    ///\note The <tt>Arc</tt>s
294 294
    ///referencing a moved arc remain
295 295
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
296 296
    ///may be invalidated.
297 297
    ///\warning This functionality cannot be used together with the Snapshot
298 298
    ///feature.
299 299
    Node split(Node n, bool connect = true)
300 300
    {
301 301
      Node b = addNode();
302 302
      nodes[b._id].first_out=nodes[n._id].first_out;
303 303
      nodes[n._id].first_out=-1;
304 304
      for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
305 305
        arcs[i].source=b._id;
306 306
      }
307 307
      if(connect) addArc(n,b);
308 308
      return b;
309 309
    }
310 310

	
311 311
  public:
312 312

	
313 313
    class Snapshot;
314 314

	
315 315
  protected:
316 316

	
317 317
    void restoreSnapshot(const Snapshot &s)
318 318
    {
319 319
      while(s.arc_num<arcs.size()) {
320 320
        Arc arc = arcFromId(arcs.size()-1);
321 321
        Parent::notifier(Arc()).erase(arc);
322 322
        nodes[arcs.back().source].first_out=arcs.back().next_out;
323 323
        nodes[arcs.back().target].first_in=arcs.back().next_in;
324 324
        arcs.pop_back();
325 325
      }
326 326
      while(s.node_num<nodes.size()) {
327 327
        Node node = nodeFromId(nodes.size()-1);
328 328
        Parent::notifier(Node()).erase(node);
329 329
        nodes.pop_back();
330 330
      }
331 331
    }
332 332

	
333 333
  public:
334 334

	
335 335
    ///Class to make a snapshot of the digraph and to restrore to it later.
336 336

	
337 337
    ///Class to make a snapshot of the digraph and to restrore to it later.
338 338
    ///
339 339
    ///The newly added nodes and arcs can be removed using the
340 340
    ///restore() function.
341 341
    ///\note After you restore a state, you cannot restore
342 342
    ///a later state, in other word you cannot add again the arcs deleted
343 343
    ///by restore() using another one Snapshot instance.
344 344
    ///
345 345
    ///\warning If you do not use correctly the snapshot that can cause
346 346
    ///either broken program, invalid state of the digraph, valid but
347 347
    ///not the restored digraph or no change. Because the runtime performance
348 348
    ///the validity of the snapshot is not stored.
349 349
    class Snapshot
350 350
    {
351 351
      SmartDigraph *_graph;
352 352
    protected:
353 353
      friend class SmartDigraph;
354 354
      unsigned int node_num;
355 355
      unsigned int arc_num;
356 356
    public:
357 357
      ///Default constructor.
358 358

	
359 359
      ///Default constructor.
360 360
      ///To actually make a snapshot you must call save().
361 361
      ///
362 362
      Snapshot() : _graph(0) {}
363 363
      ///Constructor that immediately makes a snapshot
364 364

	
365 365
      ///This constructor immediately makes a snapshot of the digraph.
366 366
      ///\param graph The digraph we make a snapshot of.
367 367
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
368 368
        node_num=_graph->nodes.size();
369 369
        arc_num=_graph->arcs.size();
370 370
      }
371 371

	
372 372
      ///Make a snapshot.
373 373

	
374 374
      ///Make a snapshot of the digraph.
375 375
      ///
376 376
      ///This function can be called more than once. In case of a repeated
377 377
      ///call, the previous snapshot gets lost.
378 378
      ///\param graph The digraph we make the snapshot of.
379 379
      void save(SmartDigraph &graph)
380 380
      {
381 381
        _graph=&graph;
382 382
        node_num=_graph->nodes.size();
383 383
        arc_num=_graph->arcs.size();
384 384
      }
385 385

	
386 386
      ///Undo the changes until a snapshot.
387 387

	
388 388
      ///Undo the changes until a snapshot created by save().
389 389
      ///
390 390
      ///\note After you restored a state, you cannot restore
391 391
      ///a later state, in other word you cannot add again the arcs deleted
392 392
      ///by restore().
393 393
      void restore()
394 394
      {
395 395
        _graph->restoreSnapshot(*this);
396 396
      }
397 397
    };
398 398
  };
399 399

	
400 400

	
401 401
  class SmartGraphBase {
402 402

	
403 403
  protected:
404 404

	
405 405
    struct NodeT {
406 406
      int first_out;
407 407
    };
408 408

	
409 409
    struct ArcT {
410 410
      int target;
411 411
      int next_out;
412 412
    };
413 413

	
414 414
    std::vector<NodeT> nodes;
415 415
    std::vector<ArcT> arcs;
416 416

	
417 417
    int first_free_arc;
418 418

	
419 419
  public:
420 420

	
421 421
    typedef SmartGraphBase Graph;
422 422

	
423 423
    class Node;
424 424
    class Arc;
425 425
    class Edge;
426 426

	
427 427
    class Node {
428 428
      friend class SmartGraphBase;
429 429
    protected:
430 430

	
431 431
      int _id;
432 432
      explicit Node(int id) { _id = id;}
433 433

	
434 434
    public:
435 435
      Node() {}
436 436
      Node (Invalid) { _id = -1; }
437 437
      bool operator==(const Node& node) const {return _id == node._id;}
438 438
      bool operator!=(const Node& node) const {return _id != node._id;}
439 439
      bool operator<(const Node& node) const {return _id < node._id;}
440 440
    };
441 441

	
442 442
    class Edge {
443 443
      friend class SmartGraphBase;
444 444
    protected:
445 445

	
446 446
      int _id;
447 447
      explicit Edge(int id) { _id = id;}
448 448

	
449 449
    public:
450 450
      Edge() {}
451 451
      Edge (Invalid) { _id = -1; }
452 452
      bool operator==(const Edge& arc) const {return _id == arc._id;}
453 453
      bool operator!=(const Edge& arc) const {return _id != arc._id;}
454 454
      bool operator<(const Edge& arc) const {return _id < arc._id;}
455 455
    };
456 456

	
457 457
    class Arc {
458 458
      friend class SmartGraphBase;
459 459
    protected:
460 460

	
461 461
      int _id;
462 462
      explicit Arc(int id) { _id = id;}
463 463

	
464 464
    public:
465 465
      operator Edge() const {
466 466
        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
467 467
      }
468 468

	
469 469
      Arc() {}
470 470
      Arc (Invalid) { _id = -1; }
471 471
      bool operator==(const Arc& arc) const {return _id == arc._id;}
472 472
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
473 473
      bool operator<(const Arc& arc) const {return _id < arc._id;}
474 474
    };
475 475

	
476 476

	
477 477

	
478 478
    SmartGraphBase()
479 479
      : nodes(), arcs() {}
480 480

	
481 481
    typedef True NodeNumTag;
482 482
    typedef True EdgeNumTag;
483 483
    typedef True ArcNumTag;
484 484

	
485 485
    int nodeNum() const { return nodes.size(); }
486 486
    int edgeNum() const { return arcs.size() / 2; }
487 487
    int arcNum() const { return arcs.size(); }
488 488

	
489 489
    int maxNodeId() const { return nodes.size()-1; }
490 490
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
491 491
    int maxArcId() const { return arcs.size()-1; }
492 492

	
493 493
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
494 494
    Node target(Arc e) const { return Node(arcs[e._id].target); }
495 495

	
496 496
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
497 497
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
498 498

	
499 499
    static bool direction(Arc e) {
500 500
      return (e._id & 1) == 1;
501 501
    }
502 502

	
503 503
    static Arc direct(Edge e, bool d) {
504 504
      return Arc(e._id * 2 + (d ? 1 : 0));
505 505
    }
506 506

	
507 507
    void first(Node& node) const {
508 508
      node._id = nodes.size() - 1;
509 509
    }
510 510

	
511
    void next(Node& node) const {
511
    static void next(Node& node) {
512 512
      --node._id;
513 513
    }
514 514

	
515 515
    void first(Arc& arc) const {
516 516
      arc._id = arcs.size() - 1;
517 517
    }
518 518

	
519
    void next(Arc& arc) const {
519
    static void next(Arc& arc) {
520 520
      --arc._id;
521 521
    }
522 522

	
523 523
    void first(Edge& arc) const {
524 524
      arc._id = arcs.size() / 2 - 1;
525 525
    }
526 526

	
527
    void next(Edge& arc) const {
527
    static void next(Edge& arc) {
528 528
      --arc._id;
529 529
    }
530 530

	
531 531
    void firstOut(Arc &arc, const Node& v) const {
532 532
      arc._id = nodes[v._id].first_out;
533 533
    }
534 534
    void nextOut(Arc &arc) const {
535 535
      arc._id = arcs[arc._id].next_out;
536 536
    }
537 537

	
538 538
    void firstIn(Arc &arc, const Node& v) const {
539 539
      arc._id = ((nodes[v._id].first_out) ^ 1);
540 540
      if (arc._id == -2) arc._id = -1;
541 541
    }
542 542
    void nextIn(Arc &arc) const {
543 543
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
544 544
      if (arc._id == -2) arc._id = -1;
545 545
    }
546 546

	
547 547
    void firstInc(Edge &arc, bool& d, const Node& v) const {
548 548
      int de = nodes[v._id].first_out;
549 549
      if (de != -1) {
550 550
        arc._id = de / 2;
551 551
        d = ((de & 1) == 1);
552 552
      } else {
553 553
        arc._id = -1;
554 554
        d = true;
555 555
      }
556 556
    }
557 557
    void nextInc(Edge &arc, bool& d) const {
558 558
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
559 559
      if (de != -1) {
560 560
        arc._id = de / 2;
561 561
        d = ((de & 1) == 1);
562 562
      } else {
563 563
        arc._id = -1;
564 564
        d = true;
565 565
      }
566 566
    }
567 567

	
568 568
    static int id(Node v) { return v._id; }
569 569
    static int id(Arc e) { return e._id; }
570 570
    static int id(Edge e) { return e._id; }
571 571

	
572 572
    static Node nodeFromId(int id) { return Node(id);}
573 573
    static Arc arcFromId(int id) { return Arc(id);}
574 574
    static Edge edgeFromId(int id) { return Edge(id);}
575 575

	
576 576
    bool valid(Node n) const {
577 577
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
578 578
    }
579 579
    bool valid(Arc a) const {
580 580
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
581 581
    }
582 582
    bool valid(Edge e) const {
583 583
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
584 584
    }
585 585

	
586 586
    Node addNode() {
587 587
      int n = nodes.size();
588 588
      nodes.push_back(NodeT());
589 589
      nodes[n].first_out = -1;
590 590

	
591 591
      return Node(n);
592 592
    }
593 593

	
594 594
    Edge addEdge(Node u, Node v) {
595 595
      int n = arcs.size();
596 596
      arcs.push_back(ArcT());
597 597
      arcs.push_back(ArcT());
598 598

	
599 599
      arcs[n].target = u._id;
600 600
      arcs[n | 1].target = v._id;
601 601

	
602 602
      arcs[n].next_out = nodes[v._id].first_out;
603 603
      nodes[v._id].first_out = n;
604 604

	
605 605
      arcs[n | 1].next_out = nodes[u._id].first_out;
606 606
      nodes[u._id].first_out = (n | 1);
607 607

	
608 608
      return Edge(n / 2);
609 609
    }
610 610

	
611 611
    void clear() {
612 612
      arcs.clear();
613 613
      nodes.clear();
614 614
    }
615 615

	
616 616
  };
617 617

	
618 618
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
619 619

	
620 620
  /// \ingroup graphs
621 621
  ///
622 622
  /// \brief A smart undirected graph class.
623 623
  ///
624 624
  /// This is a simple and fast graph implementation.
625 625
  /// It is also quite memory efficient, but at the price
626 626
  /// that <b> it does support only limited (only stack-like)
627 627
  /// node and arc deletions</b>.
628 628
  /// It fully conforms to the \ref concepts::Graph "Graph concept".
629 629
  ///
630 630
  /// \sa concepts::Graph.
631 631
  class SmartGraph : public ExtendedSmartGraphBase {
632 632
    typedef ExtendedSmartGraphBase Parent;
633 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 651
    /// Constructor
652 652

	
653 653
    /// Constructor.
654 654
    ///
655 655
    SmartGraph() {}
656 656

	
657 657
    ///Add a new node to the graph.
658 658

	
659 659
    /// Add a new node to the graph.
660 660
    /// \return The new node.
661 661
    Node addNode() { return Parent::addNode(); }
662 662

	
663 663
    ///Add a new edge to the graph.
664 664

	
665 665
    ///Add a new edge to the graph with node \c s
666 666
    ///and \c t.
667 667
    ///\return The new edge.
668 668
    Edge addEdge(const Node& s, const Node& t) {
669 669
      return Parent::addEdge(s, t);
670 670
    }
671 671

	
672 672
    /// \brief Node validity check
673 673
    ///
674 674
    /// This function gives back true if the given node is valid,
675 675
    /// ie. it is a real node of the graph.
676 676
    ///
677 677
    /// \warning A removed node (using Snapshot) could become valid again
678 678
    /// when new nodes are added to the graph.
679 679
    bool valid(Node n) const { return Parent::valid(n); }
680 680

	
681 681
    /// \brief Arc validity check
682 682
    ///
683 683
    /// This function gives back true if the given arc is valid,
684 684
    /// ie. it is a real arc of the graph.
685 685
    ///
686 686
    /// \warning A removed arc (using Snapshot) could become valid again
687 687
    /// when new edges are added to the graph.
688 688
    bool valid(Arc a) const { return Parent::valid(a); }
689 689

	
690 690
    /// \brief Edge validity check
691 691
    ///
692 692
    /// This function gives back true if the given edge is valid,
693 693
    /// ie. it is a real edge of the graph.
694 694
    ///
695 695
    /// \warning A removed edge (using Snapshot) could become valid again
696 696
    /// when new edges are added to the graph.
697 697
    bool valid(Edge e) const { return Parent::valid(e); }
698 698

	
699 699
    ///Clear the graph.
700 700

	
701 701
    ///Erase all the nodes and edges from the graph.
702 702
    ///
703 703
    void clear() {
704 704
      Parent::clear();
705 705
    }
706 706

	
707 707
  public:
708 708

	
709 709
    class Snapshot;
710 710

	
711 711
  protected:
712 712

	
713 713
    void saveSnapshot(Snapshot &s)
714 714
    {
715 715
      s._graph = this;
716 716
      s.node_num = nodes.size();
717 717
      s.arc_num = arcs.size();
718 718
    }
719 719

	
720 720
    void restoreSnapshot(const Snapshot &s)
721 721
    {
722 722
      while(s.arc_num<arcs.size()) {
723 723
        int n=arcs.size()-1;
724 724
        Edge arc=edgeFromId(n/2);
725 725
        Parent::notifier(Edge()).erase(arc);
726 726
        std::vector<Arc> dir;
727 727
        dir.push_back(arcFromId(n));
728 728
        dir.push_back(arcFromId(n-1));
729 729
        Parent::notifier(Arc()).erase(dir);
730 730
        nodes[arcs[n-1].target].first_out=arcs[n].next_out;
731 731
        nodes[arcs[n].target].first_out=arcs[n-1].next_out;
732 732
        arcs.pop_back();
733 733
        arcs.pop_back();
734 734
      }
735 735
      while(s.node_num<nodes.size()) {
736 736
        int n=nodes.size()-1;
737 737
        Node node = nodeFromId(n);
738 738
        Parent::notifier(Node()).erase(node);
739 739
        nodes.pop_back();
740 740
      }
741 741
    }
742 742

	
743 743
  public:
744 744

	
745 745
    ///Class to make a snapshot of the digraph and to restrore to it later.
746 746

	
747 747
    ///Class to make a snapshot of the digraph and to restrore to it later.
748 748
    ///
749 749
    ///The newly added nodes and arcs can be removed using the
750 750
    ///restore() function.
751 751
    ///
752 752
    ///\note After you restore a state, you cannot restore
753 753
    ///a later state, in other word you cannot add again the arcs deleted
754 754
    ///by restore() using another one Snapshot instance.
755 755
    ///
756 756
    ///\warning If you do not use correctly the snapshot that can cause
757 757
    ///either broken program, invalid state of the digraph, valid but
758 758
    ///not the restored digraph or no change. Because the runtime performance
759 759
    ///the validity of the snapshot is not stored.
760 760
    class Snapshot
761 761
    {
762 762
      SmartGraph *_graph;
763 763
    protected:
764 764
      friend class SmartGraph;
765 765
      unsigned int node_num;
766 766
      unsigned int arc_num;
767 767
    public:
768 768
      ///Default constructor.
769 769

	
770 770
      ///Default constructor.
771 771
      ///To actually make a snapshot you must call save().
772 772
      ///
773 773
      Snapshot() : _graph(0) {}
774 774
      ///Constructor that immediately makes a snapshot
775 775

	
776 776
      ///This constructor immediately makes a snapshot of the digraph.
777 777
      ///\param graph The digraph we make a snapshot of.
778 778
      Snapshot(SmartGraph &graph) {
779 779
        graph.saveSnapshot(*this);
780 780
      }
781 781

	
782 782
      ///Make a snapshot.
783 783

	
784 784
      ///Make a snapshot of the graph.
785 785
      ///
786 786
      ///This function can be called more than once. In case of a repeated
787 787
      ///call, the previous snapshot gets lost.
788 788
      ///\param graph The digraph we make the snapshot of.
789 789
      void save(SmartGraph &graph)
790 790
      {
791 791
        graph.saveSnapshot(*this);
792 792
      }
793 793

	
794 794
      ///Undo the changes until a snapshot.
795 795

	
796 796
      ///Undo the changes until a snapshot created by save().
797 797
      ///
798 798
      ///\note After you restored a state, you cannot restore
799 799
      ///a later state, in other word you cannot add again the arcs deleted
800 800
      ///by restore().
801 801
      void restore()
802 802
      {
803 803
        _graph->restoreSnapshot(*this);
804 804
      }
805 805
    };
806 806
  };
807 807

	
808 808
} //namespace lemon
809 809

	
810 810

	
811 811
#endif //LEMON_SMART_GRAPH_H
Ignore white space 1536 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_STATIC_GRAPH_H
20 20
#define LEMON_STATIC_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24 24
///\brief StaticDigraph class.
25 25

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

	
29 29
namespace lemon {
30 30

	
31 31
  class StaticDigraphBase {
32 32
  public:
33 33

	
34 34
    StaticDigraphBase() 
35 35
      : built(false), node_num(0), arc_num(0), 
36 36
        node_first_out(NULL), node_first_in(NULL),
37 37
        arc_source(NULL), arc_target(NULL), 
38 38
        arc_next_in(NULL), arc_next_out(NULL) {}
39 39
    
40 40
    ~StaticDigraphBase() {
41 41
      if (built) {
42 42
        delete[] node_first_out;
43 43
        delete[] node_first_in;
44 44
        delete[] arc_source;
45 45
        delete[] arc_target;
46 46
        delete[] arc_next_out;
47 47
        delete[] arc_next_in;
48 48
      }
49 49
    }
50 50

	
51 51
    class Node {
52 52
      friend class StaticDigraphBase;
53 53
    protected:
54 54
      int id;
55 55
      Node(int _id) : id(_id) {}
56 56
    public:
57 57
      Node() {}
58 58
      Node (Invalid) : id(-1) {}
59 59
      bool operator==(const Node& node) const { return id == node.id; }
60 60
      bool operator!=(const Node& node) const { return id != node.id; }
61 61
      bool operator<(const Node& node) const { return id < node.id; }
62 62
    };
63 63

	
64 64
    class Arc {
65 65
      friend class StaticDigraphBase;      
66 66
    protected:
67 67
      int id;
68 68
      Arc(int _id) : id(_id) {}
69 69
    public:
70 70
      Arc() { }
71 71
      Arc (Invalid) : id(-1) {}
72 72
      bool operator==(const Arc& arc) const { return id == arc.id; }
73 73
      bool operator!=(const Arc& arc) const { return id != arc.id; }
74 74
      bool operator<(const Arc& arc) const { return id < arc.id; }
75 75
    };
76 76

	
77 77
    Node source(const Arc& e) const { return Node(arc_source[e.id]); }
78 78
    Node target(const Arc& e) const { return Node(arc_target[e.id]); }
79 79

	
80 80
    void first(Node& n) const { n.id = node_num - 1; }
81 81
    static void next(Node& n) { --n.id; }
82 82

	
83 83
    void first(Arc& e) const { e.id = arc_num - 1; }
84 84
    static void next(Arc& e) { --e.id; }
85 85

	
86 86
    void firstOut(Arc& e, const Node& n) const { 
87 87
      e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? 
88 88
        node_first_out[n.id] : -1;
89 89
    }
90 90
    void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
91 91

	
92 92
    void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
93 93
    void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
94 94

	
95
    int id(const Node& n) const { return n.id; }
96
    Node nodeFromId(int id) const { return Node(id); }
95
    static int id(const Node& n) { return n.id; }
96
    static Node nodeFromId(int id) { return Node(id); }
97 97
    int maxNodeId() const { return node_num - 1; }
98 98

	
99
    int id(const Arc& e) const { return e.id; }
100
    Arc arcFromId(int id) const { return Arc(id); }
99
    static int id(const Arc& e) { return e.id; }
100
    static Arc arcFromId(int id) { return Arc(id); }
101 101
    int maxArcId() const { return arc_num - 1; }
102 102

	
103 103
    typedef True NodeNumTag;
104 104
    typedef True ArcNumTag;
105 105

	
106 106
    int nodeNum() const { return node_num; }
107 107
    int arcNum() const { return arc_num; }
108 108

	
109 109
  private:
110 110

	
111 111
    template <typename Digraph, typename NodeRefMap>
112 112
    class ArcLess {
113 113
    public:
114 114
      typedef typename Digraph::Arc Arc;
115 115

	
116 116
      ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) 
117 117
        : digraph(_graph), nodeRef(_nodeRef) {}
118 118
      
119 119
      bool operator()(const Arc& left, const Arc& right) const {
120 120
	return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)];
121 121
      }
122 122
    private:
123 123
      const Digraph& digraph;
124 124
      const NodeRefMap& nodeRef;
125 125
    };
126 126
    
127 127
  public:
128 128

	
129 129
    typedef True BuildTag;
130 130
    
131 131
    void clear() {
132 132
      if (built) {
133 133
        delete[] node_first_out;
134 134
        delete[] node_first_in;
135 135
        delete[] arc_source;
136 136
        delete[] arc_target;
137 137
        delete[] arc_next_out;
138 138
        delete[] arc_next_in;
139 139
      }
140 140
      built = false;
141 141
      node_num = 0;
142 142
      arc_num = 0;
143 143
    }
144 144
    
145 145
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
146 146
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
147 147
      typedef typename Digraph::Node GNode;
148 148
      typedef typename Digraph::Arc GArc;
149 149

	
150 150
      built = true;
151 151

	
152 152
      node_num = countNodes(digraph);
153 153
      arc_num = countArcs(digraph);
154 154

	
155 155
      node_first_out = new int[node_num + 1];
156 156
      node_first_in = new int[node_num];
157 157

	
158 158
      arc_source = new int[arc_num];
159 159
      arc_target = new int[arc_num];
160 160
      arc_next_out = new int[arc_num];
161 161
      arc_next_in = new int[arc_num];
162 162

	
163 163
      int node_index = 0;
164 164
      for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
165 165
        nodeRef[n] = Node(node_index);
166 166
        node_first_in[node_index] = -1;
167 167
        ++node_index;
168 168
      }
169 169

	
170 170
      ArcLess<Digraph, NodeRefMap> arcLess(digraph, nodeRef);
171 171

	
172 172
      int arc_index = 0;
173 173
      for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
174 174
        int source = nodeRef[n].id;
175 175
        std::vector<GArc> arcs;
176 176
        for (typename Digraph::OutArcIt e(digraph, n); e != INVALID; ++e) {
177 177
          arcs.push_back(e);
178 178
        }
179 179
        if (!arcs.empty()) {
180 180
          node_first_out[source] = arc_index;
181 181
          std::sort(arcs.begin(), arcs.end(), arcLess);
182 182
          for (typename std::vector<GArc>::iterator it = arcs.begin();
183 183
               it != arcs.end(); ++it) {
184 184
            int target = nodeRef[digraph.target(*it)].id;
185 185
            arcRef[*it] = Arc(arc_index);
186 186
            arc_source[arc_index] = source; 
187 187
            arc_target[arc_index] = target;
188 188
            arc_next_in[arc_index] = node_first_in[target];
189 189
            node_first_in[target] = arc_index;
190 190
            arc_next_out[arc_index] = arc_index + 1;
191 191
            ++arc_index;
192 192
          }
193 193
          arc_next_out[arc_index - 1] = -1;
194 194
        } else {
195 195
          node_first_out[source] = arc_index;
196 196
        }
197 197
      }
198 198
      node_first_out[node_num] = arc_num;
199 199
    }
200 200

	
201 201
  protected:
202 202

	
203 203
    void fastFirstOut(Arc& e, const Node& n) const {
204 204
      e.id = node_first_out[n.id];
205 205
    }
206 206

	
207 207
    static void fastNextOut(Arc& e) {
208 208
      ++e.id;
209 209
    }
210 210
    void fastLastOut(Arc& e, const Node& n) const {
211 211
      e.id = node_first_out[n.id + 1];
212 212
    }
213 213

	
214 214
  protected:
215 215
    bool built;
216 216
    int node_num;
217 217
    int arc_num;
218 218
    int *node_first_out;
219 219
    int *node_first_in;
220 220
    int *arc_source;
221 221
    int *arc_target;
222 222
    int *arc_next_in;
223 223
    int *arc_next_out;
224 224
  };
225 225

	
226 226
  typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;
227 227

	
228 228

	
229 229
  /// \ingroup graphs
230 230
  ///
231 231
  /// \brief A static directed graph class.
232 232
  ///
233 233
  /// \ref StaticDigraph is a highly efficient digraph implementation,
234 234
  /// but it is fully static.
235 235
  /// It stores only two \c int values for each node and only four \c int
236 236
  /// values for each arc. Moreover it provides faster item iteration than
237 237
  /// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt
238 238
  /// iterators, since its arcs are stored in an appropriate order.
239 239
  /// However it only provides build() and clear() functions and does not
240 240
  /// support any other modification of the digraph.
241 241
  ///
242 242
  /// Since this digraph structure is completely static, its nodes and arcs
243 243
  /// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt>
244 244
  /// and <tt>[0..arcNum()-1]</tt>, respectively. 
245 245
  /// The index of an item is the same as its ID, it can be obtained
246 246
  /// using the corresponding \ref index() or \ref concepts::Digraph::id()
247 247
  /// "id()" function. A node or arc with a certain index can be obtained
248 248
  /// using node() or arc().
249 249
  ///
250 250
  /// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
251 251
  /// Most of its member functions and nested classes are documented
252 252
  /// only in the concept class.
253 253
  ///
254 254
  /// \sa concepts::Digraph
255 255
  class StaticDigraph : public ExtendedStaticDigraphBase {
256 256
  public:
257 257

	
258 258
    typedef ExtendedStaticDigraphBase Parent;
259 259
  
260 260
  public:
261 261
  
262 262
    /// \brief Constructor
263 263
    ///
264 264
    /// Default constructor.
265 265
    StaticDigraph() : Parent() {}
266 266

	
267 267
    /// \brief The node with the given index.
268 268
    ///
269 269
    /// This function returns the node with the given index.
270 270
    /// \sa index()
271
    Node node(int ix) const { return Parent::nodeFromId(ix); }
271
    static Node node(int ix) { return Parent::nodeFromId(ix); }
272 272

	
273 273
    /// \brief The arc with the given index.
274 274
    ///
275 275
    /// This function returns the arc with the given index.
276 276
    /// \sa index()
277
    Arc arc(int ix) const { return Parent::arcFromId(ix); }
277
    static Arc arc(int ix) { return Parent::arcFromId(ix); }
278 278

	
279 279
    /// \brief The index of the given node.
280 280
    ///
281 281
    /// This function returns the index of the the given node.
282 282
    /// \sa node()
283
    int index(Node node) const { return Parent::id(node); }
283
    static int index(Node node) { return Parent::id(node); }
284 284

	
285 285
    /// \brief The index of the given arc.
286 286
    ///
287 287
    /// This function returns the index of the the given arc.
288 288
    /// \sa arc()
289
    int index(Arc arc) const { return Parent::id(arc); }
289
    static int index(Arc arc) { return Parent::id(arc); }
290 290

	
291 291
    /// \brief Number of nodes.
292 292
    ///
293 293
    /// This function returns the number of nodes.
294 294
    int nodeNum() const { return node_num; }
295 295

	
296 296
    /// \brief Number of arcs.
297 297
    ///
298 298
    /// This function returns the number of arcs.
299 299
    int arcNum() const { return arc_num; }
300 300

	
301 301
    /// \brief Build the digraph copying another digraph.
302 302
    ///
303 303
    /// This function builds the digraph copying another digraph of any
304 304
    /// kind. It can be called more than once, but in such case, the whole
305 305
    /// structure and all maps will be cleared and rebuilt.
306 306
    ///
307 307
    /// This method also makes possible to copy a digraph to a StaticDigraph
308 308
    /// structure using \ref DigraphCopy.
309 309
    /// 
310 310
    /// \param digraph An existing digraph to be copied.
311 311
    /// \param nodeRef The node references will be copied into this map.
312 312
    /// Its key type must be \c Digraph::Node and its value type must be
313 313
    /// \c StaticDigraph::Node.
314 314
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
315 315
    /// concept.
316 316
    /// \param arcRef The arc references will be copied into this map.
317 317
    /// Its key type must be \c Digraph::Arc and its value type must be
318 318
    /// \c StaticDigraph::Arc.
319 319
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
320 320
    ///
321 321
    /// \note If you do not need the arc references, then you could use
322 322
    /// \ref NullMap for the last parameter. However the node references
323 323
    /// are required by the function itself, thus they must be readable
324 324
    /// from the map.
325 325
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
326 326
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
327 327
      if (built) Parent::clear();
328 328
      Parent::build(digraph, nodeRef, arcRef);
329 329
    }
330 330
  
331 331
    /// \brief Clear the digraph.
332 332
    ///
333 333
    /// This function erases all nodes and arcs from the digraph.
334 334
    void clear() {
335 335
      Parent::clear();
336 336
    }
337 337

	
338 338
  protected:
339 339

	
340 340
    using Parent::fastFirstOut;
341 341
    using Parent::fastNextOut;
342 342
    using Parent::fastLastOut;
343 343
    
344 344
  public:
345 345

	
346 346
    class OutArcIt : public Arc {
347 347
    public:
348 348

	
349 349
      OutArcIt() { }
350 350

	
351 351
      OutArcIt(Invalid i) : Arc(i) { }
352 352

	
353 353
      OutArcIt(const StaticDigraph& digraph, const Node& node) {
354 354
	digraph.fastFirstOut(*this, node);
355 355
	digraph.fastLastOut(last, node);
356 356
        if (last == *this) *this = INVALID;
357 357
      }
358 358

	
359 359
      OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
360 360
        if (arc != INVALID) {
361 361
          digraph.fastLastOut(last, digraph.source(arc));
362 362
        }
363 363
      }
364 364

	
365 365
      OutArcIt& operator++() { 
366 366
        StaticDigraph::fastNextOut(*this);
367 367
        if (last == *this) *this = INVALID;
368 368
        return *this; 
369 369
      }
370 370

	
371 371
    private:
372 372
      Arc last;
373 373
    };
374 374

	
375 375
    Node baseNode(const OutArcIt &arc) const {
376 376
      return Parent::source(static_cast<const Arc&>(arc));
377 377
    }
378 378

	
379 379
    Node runningNode(const OutArcIt &arc) const {
380 380
      return Parent::target(static_cast<const Arc&>(arc));
381 381
    }
382 382

	
383 383
    Node baseNode(const InArcIt &arc) const {
384 384
      return Parent::target(static_cast<const Arc&>(arc));
385 385
    }
386 386

	
387 387
    Node runningNode(const InArcIt &arc) const {
388 388
      return Parent::source(static_cast<const Arc&>(arc));
389 389
    }
390 390

	
391 391
  };
392 392

	
393 393
}
394 394

	
395 395
#endif
0 comments (0 inline)