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

	
19 19
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
20 20
#define LEMON_BITS_GRAPH_EXTENDER_H
21 21

	
22 22
#include <lemon/bits/invalid.h>
23 23
#include <lemon/bits/utility.h>
24 24

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

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

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

	
36 36
  /// \ingroup graphbits
37 37
  ///
38 38
  /// \brief Extender for the Digraphs
39 39
  template <typename Base>
40 40
  class DigraphExtender : public Base {
41 41
  public:
42 42

	
43 43
    typedef Base Parent;
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 59
    Node fromId(int id, Node) const {
60 60
      return Parent::nodeFromId(id);
61 61
    }
62 62

	
63 63
    Arc fromId(int id, Arc) const {
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
    public:
223 223
      typedef DigraphExtender Digraph;
224 224
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
225 225

	
226 226
      explicit NodeMap(const Digraph& digraph) 
227 227
	: Parent(digraph) {}
228 228
      NodeMap(const Digraph& digraph, const _Value& value) 
229 229
	: Parent(digraph, value) {}
230 230

	
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
    public:
247 247
      typedef DigraphExtender Digraph;
248 248
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
249 249

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

	
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
  public:
333 333
    
334 334
    typedef Base Parent;
335 335
    typedef GraphExtender Graph;
336 336

	
337 337
    typedef True UndirectedTag;
338 338

	
339 339
    typedef typename Parent::Node Node;
340 340
    typedef typename Parent::Arc Arc;
341 341
    typedef typename Parent::Edge Edge;
342 342

	
343 343
    // Graph extension    
344 344

	
345 345
    int maxId(Node) const {
346 346
      return Parent::maxNodeId();
347 347
    }
348 348

	
349 349
    int maxId(Arc) const {
350 350
      return Parent::maxArcId();
351 351
    }
352 352

	
353 353
    int maxId(Edge) const {
354 354
      return Parent::maxEdgeId();
355 355
    }
356 356

	
357 357
    Node fromId(int id, Node) const {
358 358
      return Parent::nodeFromId(id);
359 359
    }
360 360

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

	
365 365
    Edge fromId(int id, Edge) const {
366 366
      return Parent::edgeFromId(id);
367 367
    }
368 368

	
369 369
    Node oppositeNode(const Node &n, const Edge &e) const {
370
      if( n == Parent::source(e))
371
	return Parent::target(e);
372
      else if( n == Parent::target(e))
373
	return Parent::source(e);
370
      if( n == Parent::u(e))
371
	return Parent::v(e);
372
      else if( n == Parent::v(e))
373
	return Parent::u(e);
374 374
      else
375 375
	return INVALID;
376 376
    }
377 377

	
378 378
    Arc oppositeArc(const Arc &arc) const {
379 379
      return Parent::direct(arc, !Parent::direction(arc));
380 380
    }
381 381

	
382 382
    using Parent::direct;
383 383
    Arc direct(const Edge &edge, const Node &node) const {
384
      return Parent::direct(edge, Parent::source(edge) == node);
384
      return Parent::direct(edge, Parent::u(edge) == node);
385 385
    }
386 386

	
387 387
    // Alterable extension
388 388

	
389 389
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
390 390
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
391 391
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
392 392

	
393 393

	
394 394
  protected:
395 395

	
396 396
    mutable NodeNotifier node_notifier;
397 397
    mutable ArcNotifier arc_notifier;
398 398
    mutable EdgeNotifier edge_notifier;
399 399

	
400 400
  public:
401 401

	
402 402
    NodeNotifier& notifier(Node) const {
403 403
      return node_notifier;
404 404
    }
405 405
    
406 406
    ArcNotifier& notifier(Arc) const {
407 407
      return arc_notifier;
408 408
    }
409 409

	
410 410
    EdgeNotifier& notifier(Edge) const {
411 411
      return edge_notifier;
412 412
    }
413 413

	
414 414

	
415 415

	
416 416
    class NodeIt : public Node { 
417 417
      const Graph* _graph;
418 418
    public:
419 419

	
420 420
      NodeIt() {}
421 421

	
422 422
      NodeIt(Invalid i) : Node(i) { }
423 423

	
424 424
      explicit NodeIt(const Graph& graph) : _graph(&graph) {
425 425
	_graph->first(static_cast<Node&>(*this));
426 426
      }
427 427

	
428 428
      NodeIt(const Graph& graph, const Node& node) 
429 429
	: Node(node), _graph(&graph) {}
430 430

	
431 431
      NodeIt& operator++() { 
432 432
	_graph->next(*this);
433 433
	return *this; 
434 434
      }
435 435

	
436 436
    };
437 437

	
438 438

	
439 439
    class ArcIt : public Arc { 
440 440
      const Graph* _graph;
441 441
    public:
442 442

	
443 443
      ArcIt() { }
444 444

	
445 445
      ArcIt(Invalid i) : Arc(i) { }
446 446

	
447 447
      explicit ArcIt(const Graph& graph) : _graph(&graph) {
448 448
	_graph->first(static_cast<Arc&>(*this));
449 449
      }
450 450

	
451 451
      ArcIt(const Graph& graph, const Arc& arc) : 
452 452
	Arc(arc), _graph(&graph) { }
453 453

	
454 454
      ArcIt& operator++() { 
455 455
	_graph->next(*this);
456 456
	return *this; 
457 457
      }
458 458

	
459 459
    };
460 460

	
461 461

	
462 462
    class OutArcIt : public Arc { 
463 463
      const Graph* _graph;
464 464
    public:
465 465

	
466 466
      OutArcIt() { }
467 467

	
468 468
      OutArcIt(Invalid i) : Arc(i) { }
469 469

	
470 470
      OutArcIt(const Graph& graph, const Node& node) 
471 471
	: _graph(&graph) {
472 472
	_graph->firstOut(*this, node);
473 473
      }
474 474

	
475 475
      OutArcIt(const Graph& graph, const Arc& arc) 
476 476
	: Arc(arc), _graph(&graph) {}
477 477

	
478 478
      OutArcIt& operator++() { 
479 479
	_graph->nextOut(*this);
480 480
	return *this; 
481 481
      }
482 482

	
483 483
    };
484 484

	
485 485

	
486 486
    class InArcIt : public Arc { 
487 487
      const Graph* _graph;
488 488
    public:
489 489

	
490 490
      InArcIt() { }
491 491

	
492 492
      InArcIt(Invalid i) : Arc(i) { }
493 493

	
494 494
      InArcIt(const Graph& graph, const Node& node) 
495 495
	: _graph(&graph) {
496 496
	_graph->firstIn(*this, node);
497 497
      }
498 498

	
499 499
      InArcIt(const Graph& graph, const Arc& arc) : 
500 500
	Arc(arc), _graph(&graph) {}
501 501

	
502 502
      InArcIt& operator++() { 
503 503
	_graph->nextIn(*this);
504 504
	return *this; 
505 505
      }
506 506

	
507 507
    };
508 508

	
509 509

	
510 510
    class EdgeIt : public Parent::Edge { 
511 511
      const Graph* _graph;
512 512
    public:
513 513

	
514 514
      EdgeIt() { }
515 515

	
516 516
      EdgeIt(Invalid i) : Edge(i) { }
517 517

	
518 518
      explicit EdgeIt(const Graph& graph) : _graph(&graph) {
519 519
	_graph->first(static_cast<Edge&>(*this));
520 520
      }
521 521

	
522 522
      EdgeIt(const Graph& graph, const Edge& edge) : 
523 523
	Edge(edge), _graph(&graph) { }
524 524

	
525 525
      EdgeIt& operator++() { 
526 526
	_graph->next(*this);
527 527
	return *this; 
528 528
      }
529 529

	
530 530
    };
531 531

	
532 532
    class IncEdgeIt : public Parent::Edge {
533 533
      friend class GraphExtender;
534 534
      const Graph* _graph;
535 535
      bool _direction;
536 536
    public:
537 537

	
538 538
      IncEdgeIt() { }
539 539

	
540 540
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
541 541

	
542 542
      IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
543 543
	_graph->firstInc(*this, _direction, node);
544 544
      }
545 545

	
546 546
      IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
547 547
	: _graph(&graph), Edge(edge) {
548 548
	_direction = (_graph->source(edge) == node);
549 549
      }
550 550

	
551 551
      IncEdgeIt& operator++() {
552 552
	_graph->nextInc(*this, _direction);
553 553
	return *this; 
554 554
      }
555 555
    };
556 556

	
557 557
    /// \brief Base node of the iterator
558 558
    ///
559 559
    /// Returns the base node (ie. the source in this case) of the iterator
560 560
    Node baseNode(const OutArcIt &arc) const {
561 561
      return Parent::source(static_cast<const Arc&>(arc));
562 562
    }
563 563
    /// \brief Running node of the iterator
564 564
    ///
565 565
    /// Returns the running node (ie. the target in this case) of the
566 566
    /// iterator
567 567
    Node runningNode(const OutArcIt &arc) const {
568 568
      return Parent::target(static_cast<const Arc&>(arc));
569 569
    }
570 570

	
571 571
    /// \brief Base node of the iterator
572 572
    ///
573 573
    /// Returns the base node (ie. the target in this case) of the iterator
574 574
    Node baseNode(const InArcIt &arc) const {
575 575
      return Parent::target(static_cast<const Arc&>(arc));
576 576
    }
577 577
    /// \brief Running node of the iterator
578 578
    ///
579 579
    /// Returns the running node (ie. the source in this case) of the
580 580
    /// iterator
581 581
    Node runningNode(const InArcIt &arc) const {
582 582
      return Parent::source(static_cast<const Arc&>(arc));
583 583
    }
584 584

	
585 585
    /// Base node of the iterator
586 586
    ///
587 587
    /// Returns the base node of the iterator
588 588
    Node baseNode(const IncEdgeIt &edge) const {
589
      return edge._direction ? source(edge) : target(edge);
589
      return edge._direction ? u(edge) : v(edge);
590 590
    }
591 591
    /// Running node of the iterator
592 592
    ///
593 593
    /// Returns the running node of the iterator
594 594
    Node runningNode(const IncEdgeIt &edge) const {
595
      return edge._direction ? target(edge) : source(edge);
595
      return edge._direction ? v(edge) : u(edge);
596 596
    }
597 597

	
598 598
    // Mappable extension
599 599

	
600 600
    template <typename _Value>
601 601
    class NodeMap 
602 602
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
603 603
    public:
604 604
      typedef GraphExtender Graph;
605 605
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
606 606

	
607 607
      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
      NodeMap& operator=(const NodeMap& cmap) {
613 613
	return operator=<NodeMap>(cmap);
614 614
      }
615 615

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

	
622 622
    };
623 623

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

	
631 631
      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
      ArcMap& operator=(const ArcMap& cmap) {
637 637
	return operator=<ArcMap>(cmap);
638 638
      }
639 639

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

	
647 647

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

	
655 655
      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
      EdgeMap& operator=(const EdgeMap& cmap) {
662 662
	return operator=<EdgeMap>(cmap);
663 663
      }
664 664

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

	
671 671
    };
672 672

	
673 673
    // Alteration extension
674 674

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

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

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

	
707 707
    void erase(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
      Parent::firstIn(arc, node);
716 716
      while (arc != INVALID ) {
717 717
	erase(arc);
718 718
	Parent::firstIn(arc, node);
719 719
      }
720 720

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

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

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

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

	
746 746
  };
747 747

	
748 748
}
749 749

	
750 750
#endif
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_CONCEPT_DIGRAPH_H
20 20
#define LEMON_CONCEPT_DIGRAPH_H
21 21

	
22 22
///\ingroup graph_concepts
23 23
///\file
24 24
///\brief The concept of directed graphs.
25 25

	
26 26
#include <lemon/bits/invalid.h>
27 27
#include <lemon/bits/utility.h>
28 28
#include <lemon/concepts/maps.h>
29 29
#include <lemon/concept_check.h>
30 30
#include <lemon/concepts/graph_components.h>
31 31

	
32 32
namespace lemon {
33 33
  namespace concepts {
34 34

	
35 35
    /// \ingroup graph_concepts
36 36
    ///
37 37
    /// \brief Class describing the concept of directed graphs.
38 38
    ///
39 39
    /// This class describes the \ref concept "concept" of the
40 40
    /// immutable directed digraphs.
41 41
    ///
42 42
    /// Note that actual digraph implementation like @ref ListDigraph or
43 43
    /// @ref SmartDigraph may have several additional functionality.
44 44
    ///
45 45
    /// \sa concept
46 46
    class Digraph {
47 47
    private:
48 48
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
49 49
      
50 50
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
51 51
      ///
52 52
      Digraph(const Digraph &) {};
53 53
      ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
54 54
      ///\e not allowed. Use DigraphCopy() instead.
55 55
      
56 56
      ///Assignment of \ref Digraph "Digraph"s to another ones are
57 57
      ///\e not allowed.  Use DigraphCopy() instead.
58 58

	
59 59
      void operator=(const Digraph &) {}
60 60
    public:
61 61
      ///\e
62 62

	
63 63
      /// Defalult constructor.
64 64

	
65 65
      /// Defalult constructor.
66 66
      ///
67 67
      Digraph() { }
68 68
      /// Class for identifying a node of the digraph
69 69

	
70 70
      /// This class identifies a node of the digraph. It also serves
71 71
      /// as a base class of the node iterators,
72 72
      /// thus they will convert to this type.
73 73
      class Node {
74 74
      public:
75 75
        /// Default constructor
76 76

	
77 77
        /// @warning The default constructor sets the iterator
78 78
        /// to an undefined value.
79 79
        Node() { }
80 80
        /// Copy constructor.
81 81

	
82 82
        /// Copy constructor.
83 83
        ///
84 84
        Node(const Node&) { }
85 85

	
86 86
        /// Invalid constructor \& conversion.
87 87

	
88 88
        /// This constructor initializes the iterator to be invalid.
89 89
        /// \sa Invalid for more details.
90 90
        Node(Invalid) { }
91 91
        /// Equality operator
92 92

	
93 93
        /// Two iterators are equal if and only if they point to the
94 94
        /// same object or both are invalid.
95 95
        bool operator==(Node) const { return true; }
96 96

	
97 97
        /// Inequality operator
98 98
        
99 99
        /// \sa operator==(Node n)
100 100
        ///
101 101
        bool operator!=(Node) const { return true; }
102 102

	
103 103
	/// Artificial ordering operator.
104 104
	
105 105
	/// To allow the use of digraph descriptors as key type in std::map or
106 106
	/// similar associative container we require this.
107 107
	///
108 108
	/// \note This operator only have to define some strict ordering of
109 109
	/// the items; this order has nothing to do with the iteration
110 110
	/// ordering of the items.
111 111
	bool operator<(Node) const { return false; }
112 112

	
113 113
      };
114 114
    
115 115
      /// This iterator goes through each node.
116 116

	
117 117
      /// This iterator goes through each node.
118 118
      /// Its usage is quite simple, for example you can count the number
119 119
      /// of nodes in digraph \c g of type \c Digraph like this:
120 120
      ///\code
121 121
      /// int count=0;
122 122
      /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
123 123
      ///\endcode
124 124
      class NodeIt : public Node {
125 125
      public:
126 126
        /// Default constructor
127 127

	
128 128
        /// @warning The default constructor sets the iterator
129 129
        /// to an undefined value.
130 130
        NodeIt() { }
131 131
        /// Copy constructor.
132 132
        
133 133
        /// Copy constructor.
134 134
        ///
135 135
        NodeIt(const NodeIt& n) : Node(n) { }
136 136
        /// Invalid constructor \& conversion.
137 137

	
138 138
        /// Initialize the iterator to be invalid.
139 139
        /// \sa Invalid for more details.
140 140
        NodeIt(Invalid) { }
141 141
        /// Sets the iterator to the first node.
142 142

	
143 143
        /// Sets the iterator to the first node of \c g.
144 144
        ///
145 145
        NodeIt(const Digraph&) { }
146 146
        /// Node -> NodeIt conversion.
147 147

	
148 148
        /// Sets the iterator to the node of \c the digraph pointed by 
149 149
	/// the trivial iterator.
150 150
        /// This feature necessitates that each time we 
151 151
        /// iterate the arc-set, the iteration order is the same.
152 152
        NodeIt(const Digraph&, const Node&) { }
153 153
        /// Next node.
154 154

	
155 155
        /// Assign the iterator to the next node.
156 156
        ///
157 157
        NodeIt& operator++() { return *this; }
158 158
      };
159 159
    
160 160
    
161 161
      /// Class for identifying an arc of the digraph
162 162

	
163 163
      /// This class identifies an arc of the digraph. It also serves
164 164
      /// as a base class of the arc iterators,
165 165
      /// thus they will convert to this type.
166 166
      class Arc {
167 167
      public:
168 168
        /// Default constructor
169 169

	
170 170
        /// @warning The default constructor sets the iterator
171 171
        /// to an undefined value.
172 172
        Arc() { }
173 173
        /// Copy constructor.
174 174

	
175 175
        /// Copy constructor.
176 176
        ///
177 177
        Arc(const Arc&) { }
178 178
        /// Initialize the iterator to be invalid.
179 179

	
180 180
        /// Initialize the iterator to be invalid.
181 181
        ///
182 182
        Arc(Invalid) { }
183 183
        /// Equality operator
184 184

	
185 185
        /// Two iterators are equal if and only if they point to the
186 186
        /// same object or both are invalid.
187 187
        bool operator==(Arc) const { return true; }
188 188
        /// Inequality operator
189 189

	
190 190
        /// \sa operator==(Arc n)
191 191
        ///
192 192
        bool operator!=(Arc) const { return true; }
193 193

	
194 194
	/// Artificial ordering operator.
195 195
	
196 196
	/// To allow the use of digraph descriptors as key type in std::map or
197 197
	/// similar associative container we require this.
198 198
	///
199 199
	/// \note This operator only have to define some strict ordering of
200 200
	/// the items; this order has nothing to do with the iteration
201 201
	/// ordering of the items.
202 202
	bool operator<(Arc) const { return false; }
203 203
      };
204 204
    
205 205
      /// This iterator goes trough the outgoing arcs of a node.
206 206

	
207 207
      /// This iterator goes trough the \e outgoing arcs of a certain node
208 208
      /// of a digraph.
209 209
      /// Its usage is quite simple, for example you can count the number
210 210
      /// of outgoing arcs of a node \c n
211 211
      /// in digraph \c g of type \c Digraph as follows.
212 212
      ///\code
213 213
      /// int count=0;
214 214
      /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
215 215
      ///\endcode
216 216
    
217 217
      class OutArcIt : public Arc {
218 218
      public:
219 219
        /// Default constructor
220 220

	
221 221
        /// @warning The default constructor sets the iterator
222 222
        /// to an undefined value.
223 223
        OutArcIt() { }
224 224
        /// Copy constructor.
225 225

	
226 226
        /// Copy constructor.
227 227
        ///
228 228
        OutArcIt(const OutArcIt& e) : Arc(e) { }
229 229
        /// Initialize the iterator to be invalid.
230 230

	
231 231
        /// Initialize the iterator to be invalid.
232 232
        ///
233 233
        OutArcIt(Invalid) { }
234 234
        /// This constructor sets the iterator to the first outgoing arc.
235 235
    
236 236
        /// This constructor sets the iterator to the first outgoing arc of
237 237
        /// the node.
238 238
        OutArcIt(const Digraph&, const Node&) { }
239 239
        /// Arc -> OutArcIt conversion
240 240

	
241 241
        /// Sets the iterator to the value of the trivial iterator.
242 242
	/// This feature necessitates that each time we 
243 243
        /// iterate the arc-set, the iteration order is the same.
244 244
        OutArcIt(const Digraph&, const Arc&) { }
245 245
        ///Next outgoing arc
246 246
        
247 247
        /// Assign the iterator to the next 
248 248
        /// outgoing arc of the corresponding node.
249 249
        OutArcIt& operator++() { return *this; }
250 250
      };
251 251

	
252 252
      /// This iterator goes trough the incoming arcs of a node.
253 253

	
254 254
      /// This iterator goes trough the \e incoming arcs of a certain node
255 255
      /// of a digraph.
256 256
      /// Its usage is quite simple, for example you can count the number
257 257
      /// of outgoing arcs of a node \c n
258 258
      /// in digraph \c g of type \c Digraph as follows.
259 259
      ///\code
260 260
      /// int count=0;
261 261
      /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
262 262
      ///\endcode
263 263

	
264 264
      class InArcIt : public Arc {
265 265
      public:
266 266
        /// Default constructor
267 267

	
268 268
        /// @warning The default constructor sets the iterator
269 269
        /// to an undefined value.
270 270
        InArcIt() { }
271 271
        /// Copy constructor.
272 272

	
273 273
        /// Copy constructor.
274 274
        ///
275 275
        InArcIt(const InArcIt& e) : Arc(e) { }
276 276
        /// Initialize the iterator to be invalid.
277 277

	
278 278
        /// Initialize the iterator to be invalid.
279 279
        ///
280 280
        InArcIt(Invalid) { }
281 281
        /// This constructor sets the iterator to first incoming arc.
282 282
    
283 283
        /// This constructor set the iterator to the first incoming arc of
284 284
        /// the node.
285 285
        InArcIt(const Digraph&, const Node&) { }
286 286
        /// Arc -> InArcIt conversion
287 287

	
288 288
        /// Sets the iterator to the value of the trivial iterator \c e.
289 289
        /// This feature necessitates that each time we 
290 290
        /// iterate the arc-set, the iteration order is the same.
291 291
        InArcIt(const Digraph&, const Arc&) { }
292 292
        /// Next incoming arc
293 293

	
294 294
        /// Assign the iterator to the next inarc of the corresponding node.
295 295
        ///
296 296
        InArcIt& operator++() { return *this; }
297 297
      };
298 298
      /// This iterator goes through each arc.
299 299

	
300 300
      /// This iterator goes through each arc of a digraph.
301 301
      /// Its usage is quite simple, for example you can count the number
302 302
      /// of arcs in a digraph \c g of type \c Digraph as follows:
303 303
      ///\code
304 304
      /// int count=0;
305 305
      /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
306 306
      ///\endcode
307 307
      class ArcIt : public Arc {
308 308
      public:
309 309
        /// Default constructor
310 310

	
311 311
        /// @warning The default constructor sets the iterator
312 312
        /// to an undefined value.
313 313
        ArcIt() { }
314 314
        /// Copy constructor.
315 315

	
316 316
        /// Copy constructor.
317 317
        ///
318 318
        ArcIt(const ArcIt& e) : Arc(e) { }
319 319
        /// Initialize the iterator to be invalid.
320 320

	
321 321
        /// Initialize the iterator to be invalid.
322 322
        ///
323 323
        ArcIt(Invalid) { }
324 324
        /// This constructor sets the iterator to the first arc.
325 325
    
326 326
        /// This constructor sets the iterator to the first arc of \c g.
327 327
        ///@param g the digraph
328 328
        ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
329 329
        /// Arc -> ArcIt conversion
330 330

	
331 331
        /// Sets the iterator to the value of the trivial iterator \c e.
332 332
        /// This feature necessitates that each time we 
333 333
        /// iterate the arc-set, the iteration order is the same.
334 334
        ArcIt(const Digraph&, const Arc&) { } 
335 335
        ///Next arc
336 336
        
337 337
        /// Assign the iterator to the next arc.
338 338
        ArcIt& operator++() { return *this; }
339 339
      };
340 340
      ///Gives back the target node of an arc.
341 341

	
342 342
      ///Gives back the target node of an arc.
343 343
      ///
344 344
      Node target(Arc) const { return INVALID; }
345 345
      ///Gives back the source node of an arc.
346 346

	
347 347
      ///Gives back the source node of an arc.
348 348
      ///
349 349
      Node source(Arc) const { return INVALID; }
350 350

	
351 351
      /// \brief Returns the ID of the node.
352 352
      int id(Node) const { return -1; } 
353 353

	
354 354
      /// \brief Returns the ID of the arc.
355 355
      int id(Arc) const { return -1; } 
356 356

	
357 357
      /// \brief Returns the node with the given ID.
358 358
      ///
359 359
      /// \pre The argument should be a valid node ID in the graph.
360 360
      Node nodeFromId(int) const { return INVALID; } 
361 361

	
362 362
      /// \brief Returns the arc with the given ID.
363 363
      ///
364 364
      /// \pre The argument should be a valid arc ID in the graph.
365 365
      Arc arcFromId(int) const { return INVALID; } 
366 366

	
367 367
      /// \brief Returns an upper bound on the node IDs.
368 368
      int maxNodeId() const { return -1; } 
369 369

	
370 370
      /// \brief Returns an upper bound on the arc IDs.
371 371
      int maxArcId() const { return -1; } 
372 372

	
373 373
      void first(Node&) const {}
374 374
      void next(Node&) const {}
375 375

	
376 376
      void first(Arc&) const {}
377 377
      void next(Arc&) const {}
378 378

	
379 379

	
380 380
      void firstIn(Arc&, const Node&) const {}
381 381
      void nextIn(Arc&) const {}
382 382

	
383 383
      void firstOut(Arc&, const Node&) const {}
384 384
      void nextOut(Arc&) const {}
385 385

	
386 386
      // The second parameter is dummy.
387 387
      Node fromId(int, Node) const { return INVALID; }
388 388
      // The second parameter is dummy.
389 389
      Arc fromId(int, Arc) const { return INVALID; }
390 390

	
391 391
      // Dummy parameter.
392 392
      int maxId(Node) const { return -1; } 
393 393
      // Dummy parameter.
394 394
      int maxId(Arc) const { return -1; } 
395 395

	
396 396
      /// \brief The base node of the iterator.
397 397
      ///
398 398
      /// Gives back the base node of the iterator.
399 399
      /// It is always the target of the pointed arc.
400 400
      Node baseNode(const InArcIt&) const { return INVALID; }
401 401

	
402 402
      /// \brief The running node of the iterator.
403 403
      ///
404 404
      /// Gives back the running node of the iterator.
405 405
      /// It is always the source of the pointed arc.
406 406
      Node runningNode(const InArcIt&) const { return INVALID; }
407 407

	
408 408
      /// \brief The base node of the iterator.
409 409
      ///
410 410
      /// Gives back the base node of the iterator.
411 411
      /// It is always the source of the pointed arc.
412 412
      Node baseNode(const OutArcIt&) const { return INVALID; }
413 413

	
414 414
      /// \brief The running node of the iterator.
415 415
      ///
416 416
      /// Gives back the running node of the iterator.
417 417
      /// It is always the target of the pointed arc.
418 418
      Node runningNode(const OutArcIt&) const { return INVALID; }
419 419

	
420 420
      /// \brief The opposite node on the given arc.
421 421
      ///
422 422
      /// Gives back the opposite node on the given arc.
423 423
      Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
424 424

	
425 425
      /// \brief Read write map of the nodes to type \c T.
426 426
      /// 
427 427
      /// ReadWrite map of the nodes to type \c T.
428 428
      /// \sa Reference
429 429
      template<class T> 
430 430
      class NodeMap : public ReadWriteMap< Node, T > {
431 431
      public:
432 432

	
433 433
        ///\e
434 434
        NodeMap(const Digraph&) { }
435 435
        ///\e
436 436
        NodeMap(const Digraph&, T) { }
437 437

	
438 438
        ///Copy constructor
439 439
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
440 440
        ///Assignment operator
441 441
        template <typename CMap>
442 442
        NodeMap& operator=(const CMap&) { 
443 443
          checkConcept<ReadMap<Node, T>, CMap>();
444 444
          return *this; 
445 445
        }
446 446
      };
447 447

	
448 448
      /// \brief Read write map of the arcs to type \c T.
449 449
      ///
450 450
      /// Reference map of the arcs to type \c T.
451 451
      /// \sa Reference
452 452
      template<class T> 
453 453
      class ArcMap : public ReadWriteMap<Arc,T> {
454 454
      public:
455 455

	
456 456
        ///\e
457 457
        ArcMap(const Digraph&) { }
458 458
        ///\e
459 459
        ArcMap(const Digraph&, T) { }
460 460
        ///Copy constructor
461 461
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
462 462
        ///Assignment operator
463 463
        template <typename CMap>
464 464
        ArcMap& operator=(const CMap&) { 
465 465
          checkConcept<ReadMap<Arc, T>, CMap>();
466 466
          return *this; 
467 467
        }
468 468
      };
469 469

	
470
      template <typename RDigraph>
470
      template <typename _Digraph>
471 471
      struct Constraints {
472 472
        void constraints() {
473
          checkConcept<IterableDigraphComponent<>, Digraph>();
474
	  checkConcept<IDableDigraphComponent<>, Digraph>();
475
          checkConcept<MappableDigraphComponent<>, Digraph>();
473
          checkConcept<IterableDigraphComponent<>, _Digraph>();
474
	  checkConcept<IDableDigraphComponent<>, _Digraph>();
475
          checkConcept<MappableDigraphComponent<>, _Digraph>();
476 476
        }
477 477
      };
478 478

	
479 479
    };
480 480
    
481 481
  } //namespace concepts  
482 482
} //namespace lemon
483 483

	
484 484

	
485 485

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

	
19 19
///\ingroup graph_concepts
20 20
///\file
21 21
///\brief The concept of Undirected Graphs.
22 22

	
23 23
#ifndef LEMON_CONCEPT_GRAPH_H
24 24
#define LEMON_CONCEPT_GRAPH_H
25 25

	
26 26
#include <lemon/concepts/graph_components.h>
27 27
#include <lemon/concepts/graph.h>
28 28
#include <lemon/bits/utility.h>
29 29

	
30 30
namespace lemon {
31 31
  namespace concepts {
32 32

	
33 33
    /// \ingroup graph_concepts
34 34
    ///
35 35
    /// \brief Class describing the concept of Undirected Graphs.
36 36
    ///
37 37
    /// This class describes the common interface of all Undirected
38 38
    /// Graphs.
39 39
    ///
40 40
    /// As all concept describing classes it provides only interface
41 41
    /// without any sensible implementation. So any algorithm for
42 42
    /// undirected graph should compile with this class, but it will not
43 43
    /// run properly, of course.
44 44
    ///
45 45
    /// The LEMON undirected graphs also fulfill the concept of
46 46
    /// directed graphs (\ref lemon::concepts::Digraph "Digraph
47 47
    /// Concept"). Each edges can be seen as two opposite
48 48
    /// directed arc and consequently the undirected graph can be
49 49
    /// seen as the direceted graph of these directed arcs. The
50 50
    /// Graph has the Edge inner class for the edges and
51 51
    /// the Arc type for the directed arcs. The Arc type is
52 52
    /// convertible to Edge or inherited from it so from a directed
53 53
    /// arc we can get the represented edge.
54 54
    ///
55 55
    /// In the sense of the LEMON each edge has a default
56 56
    /// direction (it should be in every computer implementation,
57 57
    /// because the order of edge's nodes defines an
58 58
    /// orientation). With the default orientation we can define that
59 59
    /// the directed arc is forward or backward directed. With the \c
60 60
    /// direction() and \c direct() function we can get the direction
61 61
    /// of the directed arc and we can direct an edge.
62 62
    ///
63 63
    /// The EdgeIt is an iterator for the edges. We can use
64 64
    /// the EdgeMap to map values for the edges. The InArcIt and
65 65
    /// OutArcIt iterates on the same edges but with opposite
66 66
    /// direction. The IncEdgeIt iterates also on the same edges
67 67
    /// as the OutArcIt and InArcIt but it is not convertible to Arc just
68 68
    /// to Edge.  
69 69
    class Graph {
70 70
    public:
71 71
      /// \brief The undirected graph should be tagged by the
72 72
      /// UndirectedTag.
73 73
      ///
74 74
      /// The undirected graph should be tagged by the UndirectedTag. This
75 75
      /// tag helps the enable_if technics to make compile time 
76 76
      /// specializations for undirected graphs.  
77 77
      typedef True UndirectedTag;
78 78

	
79 79
      /// \brief The base type of node iterators, 
80 80
      /// or in other words, the trivial node iterator.
81 81
      ///
82 82
      /// This is the base type of each node iterator,
83 83
      /// thus each kind of node iterator converts to this.
84 84
      /// More precisely each kind of node iterator should be inherited 
85 85
      /// from the trivial node iterator.
86 86
      class Node {
87 87
      public:
88 88
        /// Default constructor
89 89

	
90 90
        /// @warning The default constructor sets the iterator
91 91
        /// to an undefined value.
92 92
        Node() { }
93 93
        /// Copy constructor.
94 94

	
95 95
        /// Copy constructor.
96 96
        ///
97 97
        Node(const Node&) { }
98 98

	
99 99
        /// Invalid constructor \& conversion.
100 100

	
101 101
        /// This constructor initializes the iterator to be invalid.
102 102
        /// \sa Invalid for more details.
103 103
        Node(Invalid) { }
104 104
        /// Equality operator
105 105

	
106 106
        /// Two iterators are equal if and only if they point to the
107 107
        /// same object or both are invalid.
108 108
        bool operator==(Node) const { return true; }
109 109

	
110 110
        /// Inequality operator
111 111
        
112 112
        /// \sa operator==(Node n)
113 113
        ///
114 114
        bool operator!=(Node) const { return true; }
115 115

	
116 116
	/// Artificial ordering operator.
117 117
	
118 118
	/// To allow the use of graph descriptors as key type in std::map or
119 119
	/// similar associative container we require this.
120 120
	///
121 121
	/// \note This operator only have to define some strict ordering of
122 122
	/// the items; this order has nothing to do with the iteration
123 123
	/// ordering of the items.
124 124
	bool operator<(Node) const { return false; }
125 125

	
126 126
      };
127 127
    
128 128
      /// This iterator goes through each node.
129 129

	
130 130
      /// This iterator goes through each node.
131 131
      /// Its usage is quite simple, for example you can count the number
132 132
      /// of nodes in graph \c g of type \c Graph like this:
133 133
      ///\code
134 134
      /// int count=0;
135 135
      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
136 136
      ///\endcode
137 137
      class NodeIt : public Node {
138 138
      public:
139 139
        /// Default constructor
140 140

	
141 141
        /// @warning The default constructor sets the iterator
142 142
        /// to an undefined value.
143 143
        NodeIt() { }
144 144
        /// Copy constructor.
145 145
        
146 146
        /// Copy constructor.
147 147
        ///
148 148
        NodeIt(const NodeIt& n) : Node(n) { }
149 149
        /// Invalid constructor \& conversion.
150 150

	
151 151
        /// Initialize the iterator to be invalid.
152 152
        /// \sa Invalid for more details.
153 153
        NodeIt(Invalid) { }
154 154
        /// Sets the iterator to the first node.
155 155

	
156 156
        /// Sets the iterator to the first node of \c g.
157 157
        ///
158 158
        NodeIt(const Graph&) { }
159 159
        /// Node -> NodeIt conversion.
160 160

	
161 161
        /// Sets the iterator to the node of \c the graph pointed by 
162 162
	/// the trivial iterator.
163 163
        /// This feature necessitates that each time we 
164 164
        /// iterate the arc-set, the iteration order is the same.
165 165
        NodeIt(const Graph&, const Node&) { }
166 166
        /// Next node.
167 167

	
168 168
        /// Assign the iterator to the next node.
169 169
        ///
170 170
        NodeIt& operator++() { return *this; }
171 171
      };
172 172
    
173 173
    
174 174
      /// The base type of the edge iterators.
175 175

	
176 176
      /// The base type of the edge iterators.
177 177
      ///
178 178
      class Edge {
179 179
      public:
180 180
        /// Default constructor
181 181

	
182 182
        /// @warning The default constructor sets the iterator
183 183
        /// to an undefined value.
184 184
        Edge() { }
185 185
        /// Copy constructor.
186 186

	
187 187
        /// Copy constructor.
188 188
        ///
189 189
        Edge(const Edge&) { }
190 190
        /// Initialize the iterator to be invalid.
191 191

	
192 192
        /// Initialize the iterator to be invalid.
193 193
        ///
194 194
        Edge(Invalid) { }
195 195
        /// Equality operator
196 196

	
197 197
        /// Two iterators are equal if and only if they point to the
198 198
        /// same object or both are invalid.
199 199
        bool operator==(Edge) const { return true; }
200 200
        /// Inequality operator
201 201

	
202 202
        /// \sa operator==(Edge n)
203 203
        ///
204 204
        bool operator!=(Edge) const { return true; }
205 205

	
206 206
	/// Artificial ordering operator.
207 207
	
208 208
	/// To allow the use of graph descriptors as key type in std::map or
209 209
	/// similar associative container we require this.
210 210
	///
211 211
	/// \note This operator only have to define some strict ordering of
212 212
	/// the items; this order has nothing to do with the iteration
213 213
	/// ordering of the items.
214 214
	bool operator<(Edge) const { return false; }
215 215
      };
216 216

	
217 217
      /// This iterator goes through each edge.
218 218

	
219 219
      /// This iterator goes through each edge of a graph.
220 220
      /// Its usage is quite simple, for example you can count the number
221 221
      /// of edges in a graph \c g of type \c Graph as follows:
222 222
      ///\code
223 223
      /// int count=0;
224 224
      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
225 225
      ///\endcode
226 226
      class EdgeIt : public Edge {
227 227
      public:
228 228
        /// Default constructor
229 229

	
230 230
        /// @warning The default constructor sets the iterator
231 231
        /// to an undefined value.
232 232
        EdgeIt() { }
233 233
        /// Copy constructor.
234 234

	
235 235
        /// Copy constructor.
236 236
        ///
237 237
        EdgeIt(const EdgeIt& e) : Edge(e) { }
238 238
        /// Initialize the iterator to be invalid.
239 239

	
240 240
        /// Initialize the iterator to be invalid.
241 241
        ///
242 242
        EdgeIt(Invalid) { }
243 243
        /// This constructor sets the iterator to the first edge.
244 244
    
245 245
        /// This constructor sets the iterator to the first edge.
246 246
        EdgeIt(const Graph&) { }
247 247
        /// Edge -> EdgeIt conversion
248 248

	
249 249
        /// Sets the iterator to the value of the trivial iterator.
250 250
        /// This feature necessitates that each time we
251 251
        /// iterate the edge-set, the iteration order is the 
252 252
	/// same.
253 253
        EdgeIt(const Graph&, const Edge&) { } 
254 254
        /// Next edge
255 255
        
256 256
        /// Assign the iterator to the next edge.
257 257
        EdgeIt& operator++() { return *this; }
258 258
      };
259 259

	
260 260
      /// \brief This iterator goes trough the incident undirected 
261 261
      /// arcs of a node.
262 262
      ///
263 263
      /// This iterator goes trough the incident edges
264 264
      /// of a certain node of a graph. You should assume that the 
265 265
      /// loop arcs will be iterated twice.
266 266
      /// 
267 267
      /// Its usage is quite simple, for example you can compute the
268 268
      /// degree (i.e. count the number of incident arcs of a node \c n
269 269
      /// in graph \c g of type \c Graph as follows. 
270 270
      ///
271 271
      ///\code
272 272
      /// int count=0;
273 273
      /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
274 274
      ///\endcode
275 275
      class IncEdgeIt : public Edge {
276 276
      public:
277 277
        /// Default constructor
278 278

	
279 279
        /// @warning The default constructor sets the iterator
280 280
        /// to an undefined value.
281 281
        IncEdgeIt() { }
282 282
        /// Copy constructor.
283 283

	
284 284
        /// Copy constructor.
285 285
        ///
286 286
        IncEdgeIt(const IncEdgeIt& e) : Edge(e) { }
287 287
        /// Initialize the iterator to be invalid.
288 288

	
289 289
        /// Initialize the iterator to be invalid.
290 290
        ///
291 291
        IncEdgeIt(Invalid) { }
292 292
        /// This constructor sets the iterator to first incident arc.
293 293
    
294 294
        /// This constructor set the iterator to the first incident arc of
295 295
        /// the node.
296 296
        IncEdgeIt(const Graph&, const Node&) { }
297 297
        /// Edge -> IncEdgeIt conversion
298 298

	
299 299
        /// Sets the iterator to the value of the trivial iterator \c e.
300 300
        /// This feature necessitates that each time we 
301 301
        /// iterate the arc-set, the iteration order is the same.
302 302
        IncEdgeIt(const Graph&, const Edge&) { }
303 303
        /// Next incident arc
304 304

	
305 305
        /// Assign the iterator to the next incident arc
306 306
	/// of the corresponding node.
307 307
        IncEdgeIt& operator++() { return *this; }
308 308
      };
309 309

	
310 310
      /// The directed arc type.
311 311

	
312 312
      /// The directed arc type. It can be converted to the
313 313
      /// edge or it should be inherited from the undirected
314 314
      /// arc.
315 315
      class Arc : public Edge {
316 316
      public:
317 317
        /// Default constructor
318 318

	
319 319
        /// @warning The default constructor sets the iterator
320 320
        /// to an undefined value.
321 321
        Arc() { }
322 322
        /// Copy constructor.
323 323

	
324 324
        /// Copy constructor.
325 325
        ///
326 326
        Arc(const Arc& e) : Edge(e) { }
327 327
        /// Initialize the iterator to be invalid.
328 328

	
329 329
        /// Initialize the iterator to be invalid.
330 330
        ///
331 331
        Arc(Invalid) { }
332 332
        /// Equality operator
333 333

	
334 334
        /// Two iterators are equal if and only if they point to the
335 335
        /// same object or both are invalid.
336 336
        bool operator==(Arc) const { return true; }
337 337
        /// Inequality operator
338 338

	
339 339
        /// \sa operator==(Arc n)
340 340
        ///
341 341
        bool operator!=(Arc) const { return true; }
342 342

	
343 343
	/// Artificial ordering operator.
344 344
	
345 345
	/// To allow the use of graph descriptors as key type in std::map or
346 346
	/// similar associative container we require this.
347 347
	///
348 348
	/// \note This operator only have to define some strict ordering of
349 349
	/// the items; this order has nothing to do with the iteration
350 350
	/// ordering of the items.
351 351
	bool operator<(Arc) const { return false; }
352 352
	
353 353
      }; 
354 354
      /// This iterator goes through each directed arc.
355 355

	
356 356
      /// This iterator goes through each arc of a graph.
357 357
      /// Its usage is quite simple, for example you can count the number
358 358
      /// of arcs in a graph \c g of type \c Graph as follows:
359 359
      ///\code
360 360
      /// int count=0;
361 361
      /// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count;
362 362
      ///\endcode
363 363
      class ArcIt : public Arc {
364 364
      public:
365 365
        /// Default constructor
366 366

	
367 367
        /// @warning The default constructor sets the iterator
368 368
        /// to an undefined value.
369 369
        ArcIt() { }
370 370
        /// Copy constructor.
371 371

	
372 372
        /// Copy constructor.
373 373
        ///
374 374
        ArcIt(const ArcIt& e) : Arc(e) { }
375 375
        /// Initialize the iterator to be invalid.
376 376

	
377 377
        /// Initialize the iterator to be invalid.
378 378
        ///
379 379
        ArcIt(Invalid) { }
380 380
        /// This constructor sets the iterator to the first arc.
381 381
    
382 382
        /// This constructor sets the iterator to the first arc of \c g.
383 383
        ///@param g the graph
384 384
        ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
385 385
        /// Arc -> ArcIt conversion
386 386

	
387 387
        /// Sets the iterator to the value of the trivial iterator \c e.
388 388
        /// This feature necessitates that each time we 
389 389
        /// iterate the arc-set, the iteration order is the same.
390 390
        ArcIt(const Graph&, const Arc&) { } 
391 391
        ///Next arc
392 392
        
393 393
        /// Assign the iterator to the next arc.
394 394
        ArcIt& operator++() { return *this; }
395 395
      };
396 396
   
397 397
      /// This iterator goes trough the outgoing directed arcs of a node.
398 398

	
399 399
      /// This iterator goes trough the \e outgoing arcs of a certain node
400 400
      /// of a graph.
401 401
      /// Its usage is quite simple, for example you can count the number
402 402
      /// of outgoing arcs of a node \c n
403 403
      /// in graph \c g of type \c Graph as follows.
404 404
      ///\code
405 405
      /// int count=0;
406 406
      /// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
407 407
      ///\endcode
408 408
    
409 409
      class OutArcIt : public Arc {
410 410
      public:
411 411
        /// Default constructor
412 412

	
413 413
        /// @warning The default constructor sets the iterator
414 414
        /// to an undefined value.
415 415
        OutArcIt() { }
416 416
        /// Copy constructor.
417 417

	
418 418
        /// Copy constructor.
419 419
        ///
420 420
        OutArcIt(const OutArcIt& e) : Arc(e) { }
421 421
        /// Initialize the iterator to be invalid.
422 422

	
423 423
        /// Initialize the iterator to be invalid.
424 424
        ///
425 425
        OutArcIt(Invalid) { }
426 426
        /// This constructor sets the iterator to the first outgoing arc.
427 427
    
428 428
        /// This constructor sets the iterator to the first outgoing arc of
429 429
        /// the node.
430 430
        ///@param n the node
431 431
        ///@param g the graph
432 432
        OutArcIt(const Graph& n, const Node& g) {
433 433
	  ignore_unused_variable_warning(n);
434 434
	  ignore_unused_variable_warning(g);
435 435
	}
436 436
        /// Arc -> OutArcIt conversion
437 437

	
438 438
        /// Sets the iterator to the value of the trivial iterator.
439 439
	/// This feature necessitates that each time we 
440 440
        /// iterate the arc-set, the iteration order is the same.
441 441
        OutArcIt(const Graph&, const Arc&) { }
442 442
        ///Next outgoing arc
443 443
        
444 444
        /// Assign the iterator to the next 
445 445
        /// outgoing arc of the corresponding node.
446 446
        OutArcIt& operator++() { return *this; }
447 447
      };
448 448

	
449 449
      /// This iterator goes trough the incoming directed arcs of a node.
450 450

	
451 451
      /// This iterator goes trough the \e incoming arcs of a certain node
452 452
      /// of a graph.
453 453
      /// Its usage is quite simple, for example you can count the number
454 454
      /// of outgoing arcs of a node \c n
455 455
      /// in graph \c g of type \c Graph as follows.
456 456
      ///\code
457 457
      /// int count=0;
458 458
      /// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
459 459
      ///\endcode
460 460

	
461 461
      class InArcIt : public Arc {
462 462
      public:
463 463
        /// Default constructor
464 464

	
465 465
        /// @warning The default constructor sets the iterator
466 466
        /// to an undefined value.
467 467
        InArcIt() { }
468 468
        /// Copy constructor.
469 469

	
470 470
        /// Copy constructor.
471 471
        ///
472 472
        InArcIt(const InArcIt& e) : Arc(e) { }
473 473
        /// Initialize the iterator to be invalid.
474 474

	
475 475
        /// Initialize the iterator to be invalid.
476 476
        ///
477 477
        InArcIt(Invalid) { }
478 478
        /// This constructor sets the iterator to first incoming arc.
479 479
    
480 480
        /// This constructor set the iterator to the first incoming arc of
481 481
        /// the node.
482 482
        ///@param n the node
483 483
        ///@param g the graph
484 484
        InArcIt(const Graph& g, const Node& n) { 
485 485
	  ignore_unused_variable_warning(n);
486 486
	  ignore_unused_variable_warning(g);
487 487
	}
488 488
        /// Arc -> InArcIt conversion
489 489

	
490 490
        /// Sets the iterator to the value of the trivial iterator \c e.
491 491
        /// This feature necessitates that each time we 
492 492
        /// iterate the arc-set, the iteration order is the same.
493 493
        InArcIt(const Graph&, const Arc&) { }
494 494
        /// Next incoming arc
495 495

	
496 496
        /// Assign the iterator to the next inarc of the corresponding node.
497 497
        ///
498 498
        InArcIt& operator++() { return *this; }
499 499
      };
500 500

	
501 501
      /// \brief Read write map of the nodes to type \c T.
502 502
      /// 
503 503
      /// ReadWrite map of the nodes to type \c T.
504 504
      /// \sa Reference
505 505
      template<class T> 
506 506
      class NodeMap : public ReadWriteMap< Node, T >
507 507
      {
508 508
      public:
509 509

	
510 510
        ///\e
511 511
        NodeMap(const Graph&) { }
512 512
        ///\e
513 513
        NodeMap(const Graph&, T) { }
514 514

	
515 515
        ///Copy constructor
516 516
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
517 517
        ///Assignment operator
518 518
        template <typename CMap>
519 519
        NodeMap& operator=(const CMap&) { 
520 520
          checkConcept<ReadMap<Node, T>, CMap>();
521 521
          return *this; 
522 522
        }
523 523
      };
524 524

	
525 525
      /// \brief Read write map of the directed arcs to type \c T.
526 526
      ///
527 527
      /// Reference map of the directed arcs to type \c T.
528 528
      /// \sa Reference
529 529
      template<class T> 
530 530
      class ArcMap : public ReadWriteMap<Arc,T>
531 531
      {
532 532
      public:
533 533

	
534 534
        ///\e
535 535
        ArcMap(const Graph&) { }
536 536
        ///\e
537 537
        ArcMap(const Graph&, T) { }
538 538
        ///Copy constructor
539 539
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
540 540
        ///Assignment operator
541 541
        template <typename CMap>
542 542
        ArcMap& operator=(const CMap&) { 
543 543
          checkConcept<ReadMap<Arc, T>, CMap>();
544 544
          return *this; 
545 545
        }
546 546
      };
547 547

	
548 548
      /// Read write map of the edges to type \c T.
549 549

	
550 550
      /// Reference map of the arcs to type \c T.
551 551
      /// \sa Reference
552 552
      template<class T> 
553 553
      class EdgeMap : public ReadWriteMap<Edge,T>
554 554
      {
555 555
      public:
556 556

	
557 557
        ///\e
558 558
        EdgeMap(const Graph&) { }
559 559
        ///\e
560 560
        EdgeMap(const Graph&, T) { }
561 561
        ///Copy constructor
562 562
        EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {}
563 563
        ///Assignment operator
564 564
        template <typename CMap>
565 565
        EdgeMap& operator=(const CMap&) { 
566 566
          checkConcept<ReadMap<Edge, T>, CMap>();
567 567
          return *this; 
568 568
        }
569 569
      };
570 570

	
571 571
      /// \brief Direct the given edge.
572 572
      ///
573 573
      /// Direct the given edge. The returned arc source
574 574
      /// will be the given node.
575 575
      Arc direct(const Edge&, const Node&) const {
576 576
	return INVALID;
577 577
      }
578 578

	
579 579
      /// \brief Direct the given edge.
580 580
      ///
581 581
      /// Direct the given edge. The returned arc
582 582
      /// represents the given edge and the direction comes
583 583
      /// from the bool parameter. The source of the edge and
584 584
      /// the directed arc is the same when the given bool is true.
585 585
      Arc direct(const Edge&, bool) const {
586 586
	return INVALID;
587 587
      }
588 588

	
589 589
      /// \brief Returns true if the arc has default orientation.
590 590
      ///
591 591
      /// Returns whether the given directed arc is same orientation as
592 592
      /// the corresponding edge's default orientation.
593 593
      bool direction(Arc) const { return true; }
594 594

	
595 595
      /// \brief Returns the opposite directed arc.
596 596
      ///
597 597
      /// Returns the opposite directed arc.
598 598
      Arc oppositeArc(Arc) const { return INVALID; }
599 599

	
600 600
      /// \brief Opposite node on an arc
601 601
      ///
602 602
      /// \return the opposite of the given Node on the given Edge
603 603
      Node oppositeNode(Node, Edge) const { return INVALID; }
604 604

	
605 605
      /// \brief First node of the edge.
606 606
      ///
607 607
      /// \return the first node of the given Edge.
608 608
      ///
609 609
      /// Naturally edges don't have direction and thus
610 610
      /// don't have source and target node. But we use these two methods
611 611
      /// to query the two nodes of the arc. The direction of the arc
612 612
      /// which arises this way is called the inherent direction of the
613 613
      /// edge, and is used to define the "default" direction
614 614
      /// of the directed versions of the arcs.
615 615
      /// \sa direction
616 616
      Node u(Edge) const { return INVALID; }
617 617

	
618 618
      /// \brief Second node of the edge.
619 619
      Node v(Edge) const { return INVALID; }
620 620

	
621 621
      /// \brief Source node of the directed arc.
622 622
      Node source(Arc) const { return INVALID; }
623 623

	
624 624
      /// \brief Target node of the directed arc.
625 625
      Node target(Arc) const { return INVALID; }
626 626

	
627 627
      /// \brief Returns the id of the node.
628 628
      int id(Node) const { return -1; } 
629 629

	
630 630
      /// \brief Returns the id of the edge.
631 631
      int id(Edge) const { return -1; } 
632 632

	
633 633
      /// \brief Returns the id of the arc.
634 634
      int id(Arc) const { return -1; } 
635 635

	
636 636
      /// \brief Returns the node with the given id.
637 637
      ///
638 638
      /// \pre The argument should be a valid node id in the graph.
639 639
      Node nodeFromId(int) const { return INVALID; } 
640 640

	
641 641
      /// \brief Returns the edge with the given id.
642 642
      ///
643 643
      /// \pre The argument should be a valid edge id in the graph.
644 644
      Edge edgeFromId(int) const { return INVALID; } 
645 645

	
646 646
      /// \brief Returns the arc with the given id.
647 647
      ///
648 648
      /// \pre The argument should be a valid arc id in the graph.
649 649
      Arc arcFromId(int) const { return INVALID; } 
650 650

	
651 651
      /// \brief Returns an upper bound on the node IDs.
652 652
      int maxNodeId() const { return -1; } 
653 653

	
654 654
      /// \brief Returns an upper bound on the edge IDs.
655 655
      int maxEdgeId() const { return -1; } 
656 656

	
657 657
      /// \brief Returns an upper bound on the arc IDs.
658 658
      int maxArcId() const { return -1; } 
659 659

	
660 660
      void first(Node&) const {}
661 661
      void next(Node&) const {}
662 662

	
663 663
      void first(Edge&) const {}
664 664
      void next(Edge&) const {}
665 665

	
666 666
      void first(Arc&) const {}
667 667
      void next(Arc&) const {}
668 668

	
669 669
      void firstOut(Arc&, Node) const {}
670 670
      void nextOut(Arc&) const {}
671 671

	
672 672
      void firstIn(Arc&, Node) const {}
673 673
      void nextIn(Arc&) const {}
674 674

	
675 675
      void firstInc(Edge &, bool &, const Node &) const {}
676 676
      void nextInc(Edge &, bool &) const {}
677 677

	
678 678
      // The second parameter is dummy.
679 679
      Node fromId(int, Node) const { return INVALID; }
680 680
      // The second parameter is dummy.
681 681
      Edge fromId(int, Edge) const { return INVALID; }
682 682
      // The second parameter is dummy.
683 683
      Arc fromId(int, Arc) const { return INVALID; }
684 684

	
685 685
      // Dummy parameter.
686 686
      int maxId(Node) const { return -1; } 
687 687
      // Dummy parameter.
688 688
      int maxId(Edge) const { return -1; } 
689 689
      // Dummy parameter.
690 690
      int maxId(Arc) const { return -1; } 
691 691

	
692 692
      /// \brief Base node of the iterator
693 693
      ///
694 694
      /// Returns the base node (the source in this case) of the iterator
695 695
      Node baseNode(OutArcIt e) const {
696 696
	return source(e);
697 697
      }
698 698
      /// \brief Running node of the iterator
699 699
      ///
700 700
      /// Returns the running node (the target in this case) of the
701 701
      /// iterator
702 702
      Node runningNode(OutArcIt e) const {
703 703
	return target(e);
704 704
      }
705 705

	
706 706
      /// \brief Base node of the iterator
707 707
      ///
708 708
      /// Returns the base node (the target in this case) of the iterator
709 709
      Node baseNode(InArcIt e) const {
710 710
	return target(e);
711 711
      }
712 712
      /// \brief Running node of the iterator
713 713
      ///
714 714
      /// Returns the running node (the source in this case) of the
715 715
      /// iterator
716 716
      Node runningNode(InArcIt e) const {
717 717
	return source(e);
718 718
      }
719 719

	
720 720
      /// \brief Base node of the iterator
721 721
      ///
722 722
      /// Returns the base node of the iterator
723 723
      Node baseNode(IncEdgeIt) const {
724 724
	return INVALID;
725 725
      }
726 726
      
727 727
      /// \brief Running node of the iterator
728 728
      ///
729 729
      /// Returns the running node of the iterator
730 730
      Node runningNode(IncEdgeIt) const {
731 731
	return INVALID;
732 732
      }
733 733

	
734
      template <typename Graph>
734
      template <typename _Graph>
735 735
      struct Constraints {
736 736
	void constraints() {
737
	  checkConcept<IterableGraphComponent<>, Graph>();
738
	  checkConcept<IDableGraphComponent<>, Graph>();
739
	  checkConcept<MappableGraphComponent<>, Graph>();
737
	  checkConcept<IterableGraphComponent<>, _Graph>();
738
	  checkConcept<IDableGraphComponent<>, _Graph>();
739
	  checkConcept<MappableGraphComponent<>, _Graph>();
740 740
	}
741 741
      };
742 742

	
743 743
    };
744 744

	
745 745
  }
746 746

	
747 747
}
748 748

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

	
19 19
#ifndef LEMON_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/bits/invalid.h>
29 29

	
30 30
#include <lemon/bits/base_extender.h>
31 31
#include <lemon/bits/graph_extender.h>
32 32

	
33 33
#include <lemon/bits/utility.h>
34 34
#include <lemon/error.h>
35 35

	
36 36
#include <lemon/bits/graph_extender.h>
37 37

	
38 38
namespace lemon {
39 39

	
40 40
  class SmartDigraph;
41 41
  ///Base of SmartDigraph
42 42

	
43 43
  ///Base of SmartDigraph
44 44
  ///
45 45
  class SmartDigraphBase {
46 46
  protected:
47 47

	
48 48
    struct NodeT 
49 49
    {
50 50
      int first_in, first_out;      
51 51
      NodeT() {}
52 52
    };
53 53
    struct ArcT 
54 54
    {
55 55
      int target, source, next_in, next_out;      
56 56
      ArcT() {}  
57 57
    };
58 58

	
59 59
    std::vector<NodeT> nodes;
60 60
    std::vector<ArcT> arcs;
61 61
        
62 62
  public:
63 63

	
64 64
    typedef SmartDigraphBase Graph;
65 65

	
66 66
    class Node;
67 67
    class Arc;
68 68

	
69 69
  public:
70 70

	
71 71
    SmartDigraphBase() : nodes(), arcs() { }
72 72
    SmartDigraphBase(const SmartDigraphBase &_g) 
73 73
      : nodes(_g.nodes), arcs(_g.arcs) { }
74 74
    
75 75
    typedef True NodeNumTag;
76 76
    typedef True ArcNumTag;
77 77

	
78 78
    int nodeNum() const { return nodes.size(); }
79 79
    int arcNum() const { return arcs.size(); }
80 80

	
81 81
    int maxNodeId() const { return nodes.size()-1; }
82 82
    int maxArcId() const { return arcs.size()-1; }
83 83

	
84 84
    Node addNode() {
85 85
      int n = nodes.size();     
86 86
      nodes.push_back(NodeT());
87 87
      nodes[n].first_in = -1;
88 88
      nodes[n].first_out = -1;
89 89
      return Node(n);
90 90
    }
91 91
    
92 92
    Arc addArc(Node u, Node v) {
93 93
      int n = arcs.size(); 
94 94
      arcs.push_back(ArcT());
95 95
      arcs[n].source = u._id; 
96 96
      arcs[n].target = v._id;
97 97
      arcs[n].next_out = nodes[u._id].first_out;
98 98
      arcs[n].next_in = nodes[v._id].first_in;
99 99
      nodes[u._id].first_out = nodes[v._id].first_in = n;
100 100

	
101 101
      return Arc(n);
102 102
    }
103 103

	
104 104
    void clear() {
105 105
      arcs.clear();
106 106
      nodes.clear();
107 107
    }
108 108

	
109 109
    Node source(Arc a) const { return Node(arcs[a._id].source); }
110 110
    Node target(Arc a) const { return Node(arcs[a._id].target); }
111 111

	
112 112
    static int id(Node v) { return v._id; }
113 113
    static int id(Arc a) { return a._id; }
114 114

	
115 115
    static Node nodeFromId(int id) { return Node(id);}
116 116
    static Arc arcFromId(int id) { return Arc(id);}
117 117

	
118 118
    class Node {
119 119
      friend class SmartDigraphBase;
120 120
      friend class SmartDigraph;
121 121

	
122 122
    protected:
123 123
      int _id;
124 124
      explicit Node(int id) : _id(id) {}
125 125
    public:
126 126
      Node() {}
127 127
      Node (Invalid) : _id(-1) {}
128 128
      bool operator==(const Node i) const {return _id == i._id;}
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
    };
132 132
    
133 133

	
134 134
    class Arc {
135 135
      friend class SmartDigraphBase;
136 136
      friend class SmartDigraph;
137 137

	
138 138
    protected:
139 139
      int _id;
140 140
      explicit Arc(int id) : _id(id) {}
141 141
    public:
142 142
      Arc() { }
143 143
      Arc (Invalid) : _id(-1) {}
144 144
      bool operator==(const Arc i) const {return _id == i._id;}
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
    };
148 148

	
149 149
    void first(Node& node) const {
150 150
      node._id = nodes.size() - 1;
151 151
    }
152 152

	
153 153
    static void next(Node& node) {
154 154
      --node._id;
155 155
    }
156 156

	
157 157
    void first(Arc& arc) const {
158 158
      arc._id = arcs.size() - 1;
159 159
    }
160 160

	
161 161
    static void next(Arc& arc) {
162 162
      --arc._id;
163 163
    }
164 164

	
165 165
    void firstOut(Arc& arc, const Node& node) const {
166 166
      arc._id = nodes[node._id].first_out;
167 167
    }
168 168

	
169 169
    void nextOut(Arc& arc) const {
170 170
      arc._id = arcs[arc._id].next_out;
171 171
    }
172 172

	
173 173
    void firstIn(Arc& arc, const Node& node) const {
174 174
      arc._id = nodes[node._id].first_in;
175 175
    }
176 176
    
177 177
    void nextIn(Arc& arc) const {
178 178
      arc._id = arcs[arc._id].next_in;
179 179
    }
180 180

	
181 181
  };
182 182

	
183 183
  typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
184 184

	
185 185
  ///\ingroup graphs
186 186
  ///
187 187
  ///\brief A smart directed graph class.
188 188
  ///
189 189
  ///This is a simple and fast digraph implementation.
190 190
  ///It is also quite memory efficient, but at the price
191 191
  ///that <b> it does support only limited (only stack-like)
192 192
  ///node and arc deletions</b>.
193 193
  ///It conforms to the \ref concepts::Digraph "Digraph concept" with
194 194
  ///an important extra feature that its maps are real \ref
195 195
  ///concepts::ReferenceMap "reference map"s.
196 196
  ///
197 197
  ///\sa concepts::Digraph.
198 198
  ///
199 199
  ///\author Alpar Juttner
200 200
  class SmartDigraph : public ExtendedSmartDigraphBase {
201 201
  public:
202 202

	
203 203
    typedef ExtendedSmartDigraphBase Parent;
204 204

	
205 205
  private:
206 206

	
207 207
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
208 208

	
209 209
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
210 210
    ///
211 211
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
212 212
    ///\brief Assignment of SmartDigraph to another one is \e not allowed.
213 213
    ///Use DigraphCopy() instead.
214 214

	
215 215
    ///Assignment of SmartDigraph to another one is \e not allowed.
216 216
    ///Use DigraphCopy() instead.
217 217
    void operator=(const SmartDigraph &) {}
218 218

	
219 219
  public:
220 220
    
221 221
    /// Constructor
222 222
    
223 223
    /// Constructor.
224 224
    ///
225 225
    SmartDigraph() {};
226 226
    
227 227
    ///Add a new node to the digraph.
228 228
    
229 229
    /// \return the new node.
230 230
    ///
231 231
    Node addNode() { return Parent::addNode(); }
232 232
    
233 233
    ///Add a new arc to the digraph.
234 234
    
235 235
    ///Add a new arc to the digraph with source node \c s
236 236
    ///and target node \c t.
237 237
    ///\return the new arc.
238 238
    Arc addArc(const Node& s, const Node& t) { 
239 239
      return Parent::addArc(s, t); 
240 240
    }
241 241

	
242 242
    /// \brief Using this it is possible to avoid the superfluous memory
243 243
    /// allocation.
244 244

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

	
253 253
    /// \brief Using this it is possible to avoid the superfluous memory
254 254
    /// allocation.
255 255

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

	
264 264
    ///Clear the digraph.
265 265
    
266 266
    ///Erase all the nodes and arcs from the digraph.
267 267
    ///
268 268
    void clear() {
269 269
      Parent::clear();
270 270
    }
271 271

	
272 272
    ///Split a node.
273 273
    
274 274
    ///This function splits a node. First a new node is added to the digraph,
275 275
    ///then the source of each outgoing arc of \c n is moved to this new node.
276 276
    ///If \c connect is \c true (this is the default value), then a new arc
277 277
    ///from \c n to the newly created node is also added.
278 278
    ///\return The newly created node.
279 279
    ///
280 280
    ///\note The <tt>Arc</tt>s
281 281
    ///referencing a moved arc remain
282 282
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
283 283
    ///may be invalidated.
284 284
    ///\warning This functionality cannot be used together with the Snapshot
285 285
    ///feature.
286 286
    ///\todo It could be implemented in a bit faster way.
287 287
    Node split(Node n, bool connect = true)
288 288
    {
289 289
      Node b = addNode();
290 290
      nodes[b._id].first_out=nodes[n._id].first_out;
291 291
      nodes[n._id].first_out=-1;
292 292
      for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id;
293 293
      if(connect) addArc(n,b);
294 294
      return b;
295 295
    }
296 296

	
297 297
  public:
298 298
    
299 299
    class Snapshot;
300 300

	
301 301
  protected:
302 302

	
303 303
    void restoreSnapshot(const Snapshot &s)
304 304
    {
305 305
      while(s.arc_num<arcs.size()) {
306 306
        Arc arc = arcFromId(arcs.size()-1);
307 307
	Parent::notifier(Arc()).erase(arc);
308 308
	nodes[arcs.back().source].first_out=arcs.back().next_out;
309 309
	nodes[arcs.back().target].first_in=arcs.back().next_in;
310 310
	arcs.pop_back();
311 311
      }
312 312
      while(s.node_num<nodes.size()) {
313 313
        Node node = nodeFromId(nodes.size()-1);
314 314
	Parent::notifier(Node()).erase(node);
315 315
	nodes.pop_back();
316 316
      }
317 317
    }    
318 318

	
319 319
  public:
320 320

	
321 321
    ///Class to make a snapshot of the digraph and to restrore to it later.
322 322

	
323 323
    ///Class to make a snapshot of the digraph and to restrore to it later.
324 324
    ///
325 325
    ///The newly added nodes and arcs can be removed using the
326 326
    ///restore() function.
327 327
    ///\note After you restore a state, you cannot restore
328 328
    ///a later state, in other word you cannot add again the arcs deleted
329 329
    ///by restore() using another one Snapshot instance.
330 330
    ///
331 331
    ///\warning If you do not use correctly the snapshot that can cause
332 332
    ///either broken program, invalid state of the digraph, valid but
333 333
    ///not the restored digraph or no change. Because the runtime performance
334 334
    ///the validity of the snapshot is not stored.
335 335
    class Snapshot 
336 336
    {
337 337
      SmartDigraph *_graph;
338 338
    protected:
339 339
      friend class SmartDigraph;
340 340
      unsigned int node_num;
341 341
      unsigned int arc_num;
342 342
    public:
343 343
      ///Default constructor.
344 344
      
345 345
      ///Default constructor.
346 346
      ///To actually make a snapshot you must call save().
347 347
      ///
348 348
      Snapshot() : _graph(0) {}
349 349
      ///Constructor that immediately makes a snapshot
350 350
      
351 351
      ///This constructor immediately makes a snapshot of the digraph.
352 352
      ///\param _g The digraph we make a snapshot of.
353 353
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
354 354
	node_num=_graph->nodes.size();
355 355
	arc_num=_graph->arcs.size();
356 356
      }
357 357

	
358 358
      ///Make a snapshot.
359 359

	
360 360
      ///Make a snapshot of the digraph.
361 361
      ///
362 362
      ///This function can be called more than once. In case of a repeated
363 363
      ///call, the previous snapshot gets lost.
364 364
      ///\param _g The digraph we make the snapshot of.
365 365
      void save(SmartDigraph &graph) 
366 366
      {
367 367
	_graph=&graph;
368 368
	node_num=_graph->nodes.size();
369 369
	arc_num=_graph->arcs.size();
370 370
      }
371 371

	
372 372
      ///Undo the changes until a snapshot.
373 373
      
374 374
      ///Undo the changes until a snapshot created by save().
375 375
      ///
376 376
      ///\note After you restored a state, you cannot restore
377 377
      ///a later state, in other word you cannot add again the arcs deleted
378 378
      ///by restore().
379 379
      void restore()
380 380
      {
381 381
	_graph->restoreSnapshot(*this);
382 382
      }
383 383
    };
384 384
  };
385 385

	
386 386

	
387 387
  class SmartGraphBase {
388 388

	
389 389
  protected:
390 390

	
391 391
    struct NodeT {
392 392
      int first_out;
393 393
    };
394 394
 
395 395
    struct ArcT {
396 396
      int target;
397 397
      int next_out;
398 398
    };
399 399

	
400 400
    std::vector<NodeT> nodes;
401 401
    std::vector<ArcT> arcs;
402 402

	
403 403
    int first_free_arc;
404 404
    
405 405
  public:
406 406
    
407 407
    typedef SmartGraphBase Digraph;
408 408

	
409 409
    class Node;
410 410
    class Arc;
411 411
    class Edge;
412 412
    
413 413
    class Node {
414 414
      friend class SmartGraphBase;
415 415
    protected:
416 416

	
417 417
      int _id;
418 418
      explicit Node(int id) { _id = id;}
419 419

	
420 420
    public:
421 421
      Node() {}
422 422
      Node (Invalid) { _id = -1; }
423 423
      bool operator==(const Node& node) const {return _id == node._id;}
424 424
      bool operator!=(const Node& node) const {return _id != node._id;}
425 425
      bool operator<(const Node& node) const {return _id < node._id;}
426 426
    };
427 427

	
428 428
    class Edge {
429 429
      friend class SmartGraphBase;
430 430
    protected:
431 431

	
432 432
      int _id;
433 433
      explicit Edge(int id) { _id = id;}
434 434

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

	
443 443
    class Arc {
444 444
      friend class SmartGraphBase;
445 445
    protected:
446 446

	
447 447
      int _id;
448 448
      explicit Arc(int id) { _id = id;}
449 449

	
450 450
    public:
451 451
      operator Edge() const { return edgeFromId(_id / 2); }
452 452

	
453 453
      Arc() {}
454 454
      Arc (Invalid) { _id = -1; }
455 455
      bool operator==(const Arc& arc) const {return _id == arc._id;}
456 456
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
457 457
      bool operator<(const Arc& arc) const {return _id < arc._id;}
458 458
    };
459 459

	
460 460

	
461 461

	
462 462
    SmartGraphBase()
463 463
      : nodes(), arcs() {}
464 464

	
465 465
    
466 466
    int maxNodeId() const { return nodes.size()-1; } 
467 467
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
468 468
    int maxArcId() const { return arcs.size()-1; }
469 469

	
470 470
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
471 471
    Node target(Arc e) const { return Node(arcs[e._id].target); }
472 472

	
473
    Node source(Edge e) const { return Node(arcs[2 * e._id].target); }
474
    Node target(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
473
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
474
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
475 475

	
476 476
    static bool direction(Arc e) {
477 477
      return (e._id & 1) == 1;
478 478
    }
479 479

	
480 480
    static Arc direct(Edge e, bool d) {
481 481
      return Arc(e._id * 2 + (d ? 1 : 0));
482 482
    }
483 483

	
484 484
    void first(Node& node) const { 
485 485
      node._id = nodes.size() - 1;
486 486
    }
487 487

	
488 488
    void next(Node& node) const {
489 489
      --node._id;
490 490
    }
491 491

	
492 492
    void first(Arc& arc) const { 
493 493
      arc._id = arcs.size() - 1;
494 494
    }
495 495

	
496 496
    void next(Arc& arc) const {
497 497
      --arc._id;
498 498
    }
499 499

	
500 500
    void first(Edge& arc) const { 
501 501
      arc._id = arcs.size() / 2 - 1;
502 502
    }
503 503

	
504 504
    void next(Edge& arc) const {
505 505
      --arc._id;
506 506
    }
507 507

	
508 508
    void firstOut(Arc &arc, const Node& v) const {
509 509
      arc._id = nodes[v._id].first_out;
510 510
    }
511 511
    void nextOut(Arc &arc) const {
512 512
      arc._id = arcs[arc._id].next_out;
513 513
    }
514 514

	
515 515
    void firstIn(Arc &arc, const Node& v) const {
516 516
      arc._id = ((nodes[v._id].first_out) ^ 1);
517 517
      if (arc._id == -2) arc._id = -1;
518 518
    }
519 519
    void nextIn(Arc &arc) const {
520 520
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
521 521
      if (arc._id == -2) arc._id = -1;
522 522
    }
523 523

	
524 524
    void firstInc(Edge &arc, bool& d, const Node& v) const {
525 525
      int de = nodes[v._id].first_out;
526 526
      if (de != -1) {
527 527
        arc._id = de / 2;
528 528
        d = ((de & 1) == 1);
529 529
      } else {
530 530
        arc._id = -1;
531 531
        d = true;
532 532
      }
533 533
    }
534 534
    void nextInc(Edge &arc, bool& d) const {
535 535
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
536 536
      if (de != -1) {
537 537
        arc._id = de / 2;
538 538
        d = ((de & 1) == 1);
539 539
      } else {
540 540
        arc._id = -1;
541 541
        d = true;      
542 542
      }
543 543
    }
544 544
    
545 545
    static int id(Node v) { return v._id; }
546 546
    static int id(Arc e) { return e._id; }
547 547
    static int id(Edge e) { return e._id; }
548 548

	
549 549
    static Node nodeFromId(int id) { return Node(id);}
550 550
    static Arc arcFromId(int id) { return Arc(id);}
551 551
    static Edge edgeFromId(int id) { return Edge(id);}
552 552

	
553 553
    Node addNode() {     
554 554
      int n = nodes.size();
555 555
      nodes.push_back(NodeT());
556 556
      nodes[n].first_out = -1;
557 557
      
558 558
      return Node(n);
559 559
    }
560 560
    
561 561
    Edge addArc(Node u, Node v) {
562 562
      int n = arcs.size();
563 563
      arcs.push_back(ArcT());
564 564
      arcs.push_back(ArcT());
565 565
      
566 566
      arcs[n].target = u._id;
567 567
      arcs[n | 1].target = v._id;
568 568

	
569 569
      arcs[n].next_out = nodes[v._id].first_out;
570 570
      nodes[v._id].first_out = n;
571 571

	
572 572
      arcs[n | 1].next_out = nodes[u._id].first_out;	
573 573
      nodes[u._id].first_out = (n | 1);
574 574

	
575 575
      return Edge(n / 2);
576 576
    }
577 577
    
578 578
    void clear() {
579 579
      arcs.clear();
580 580
      nodes.clear();
581 581
    }
582 582

	
583 583
  };
584 584

	
585 585
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
586 586

	
587 587
  /// \ingroup graphs
588 588
  ///
589 589
  /// \brief A smart undirected graph class.
590 590
  ///
591 591
  /// This is a simple and fast graph implementation.
592 592
  /// It is also quite memory efficient, but at the price
593 593
  /// that <b> it does support only limited (only stack-like)
594 594
  /// node and arc deletions</b>.
595 595
  /// Except from this it conforms to 
596 596
  /// the \ref concepts::Graph "Graph concept".
597 597
  ///
598 598
  /// It also has an
599 599
  /// important extra feature that
600 600
  /// its maps are real \ref concepts::ReferenceMap "reference map"s.
601 601
  ///
602 602
  /// \sa concepts::Graph.
603 603
  ///
604 604
  class SmartGraph : public ExtendedSmartGraphBase {
605 605
  private:
606 606

	
607 607
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
608 608

	
609 609
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
610 610
    ///
611 611
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
612 612

	
613 613
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
614 614
    ///Use GraphCopy() instead.
615 615

	
616 616
    ///Assignment of SmartGraph to another one is \e not allowed.
617 617
    ///Use GraphCopy() instead.
618 618
    void operator=(const SmartGraph &) {}
619 619

	
620 620
  public:
621 621

	
622 622
    typedef ExtendedSmartGraphBase Parent;
623 623

	
624 624
    /// Constructor
625 625
    
626 626
    /// Constructor.
627 627
    ///
628 628
    SmartGraph() {}
629 629

	
630 630
    ///Add a new node to the graph.
631 631
    
632 632
    /// \return the new node.
633 633
    ///
634 634
    Node addNode() { return Parent::addNode(); }
635 635
    
636 636
    ///Add a new edge to the graph.
637 637
    
638 638
    ///Add a new edge to the graph with node \c s
639 639
    ///and \c t.
640 640
    ///\return the new edge.
641 641
    Edge addEdge(const Node& s, const Node& t) { 
642 642
      return Parent::addArc(s, t); 
643 643
    }
644 644

	
645 645
    ///Clear the graph.
646 646
    
647 647
    ///Erase all the nodes and edges from the graph.
648 648
    ///
649 649
    void clear() {
650 650
      Parent::clear();
651 651
    }
652 652

	
653 653
  public:
654 654
    
655 655
    class Snapshot;
656 656

	
657 657
  protected:
658 658

	
659 659
    void saveSnapshot(Snapshot &s)
660 660
    {
661 661
      s._graph = this;
662 662
      s.node_num = nodes.size();
663 663
      s.arc_num = arcs.size();
664 664
    }
665 665

	
666 666
    void restoreSnapshot(const Snapshot &s)
667 667
    {
668 668
      while(s.arc_num<arcs.size()) {
669 669
        int n=arcs.size()-1;
670 670
        Edge arc=edgeFromId(n/2);
671 671
	Parent::notifier(Edge()).erase(arc);
672 672
        std::vector<Arc> dir;
673 673
        dir.push_back(arcFromId(n));
674 674
        dir.push_back(arcFromId(n-1));
675 675
	Parent::notifier(Arc()).erase(dir);
676 676
	nodes[arcs[n].target].first_out=arcs[n].next_out;
677 677
	nodes[arcs[n-1].target].first_out=arcs[n-1].next_out;
678 678
	arcs.pop_back();
679 679
	arcs.pop_back();
680 680
      }
681 681
      while(s.node_num<nodes.size()) {
682 682
        int n=nodes.size()-1;
683 683
        Node node = nodeFromId(n);
684 684
	Parent::notifier(Node()).erase(node);
685 685
	nodes.pop_back();
686 686
      }
687 687
    }    
688 688

	
689 689
  public:
690 690

	
691 691
    ///Class to make a snapshot of the digraph and to restrore to it later.
692 692

	
693 693
    ///Class to make a snapshot of the digraph and to restrore to it later.
694 694
    ///
695 695
    ///The newly added nodes and arcs can be removed using the
696 696
    ///restore() function.
697 697
    ///
698 698
    ///\note After you restore a state, you cannot restore
699 699
    ///a later state, in other word you cannot add again the arcs deleted
700 700
    ///by restore() using another one Snapshot instance.
701 701
    ///
702 702
    ///\warning If you do not use correctly the snapshot that can cause
703 703
    ///either broken program, invalid state of the digraph, valid but
704 704
    ///not the restored digraph or no change. Because the runtime performance
705 705
    ///the validity of the snapshot is not stored.
706 706
    class Snapshot 
707 707
    {
708 708
      SmartGraph *_graph;
709 709
    protected:
710 710
      friend class SmartGraph;
711 711
      unsigned int node_num;
712 712
      unsigned int arc_num;
713 713
    public:
714 714
      ///Default constructor.
715 715
      
716 716
      ///Default constructor.
717 717
      ///To actually make a snapshot you must call save().
718 718
      ///
719 719
      Snapshot() : _graph(0) {}
720 720
      ///Constructor that immediately makes a snapshot
721 721
      
722 722
      ///This constructor immediately makes a snapshot of the digraph.
723 723
      ///\param g The digraph we make a snapshot of.
724 724
      Snapshot(SmartGraph &graph) {
725 725
        graph.saveSnapshot(*this);
726 726
      }
727 727

	
728 728
      ///Make a snapshot.
729 729

	
730 730
      ///Make a snapshot of the graph.
731 731
      ///
732 732
      ///This function can be called more than once. In case of a repeated
733 733
      ///call, the previous snapshot gets lost.
734 734
      ///\param g The digraph we make the snapshot of.
735 735
      void save(SmartGraph &graph) 
736 736
      {
737 737
        graph.saveSnapshot(*this);
738 738
      }
739 739

	
740 740
      ///Undo the changes until a snapshot.
741 741
      
742 742
      ///Undo the changes until a snapshot created by save().
743 743
      ///
744 744
      ///\note After you restored a state, you cannot restore
745 745
      ///a later state, in other word you cannot add again the arcs deleted
746 746
      ///by restore().
747 747
      void restore()
748 748
      {
749 749
        _graph->restoreSnapshot(*this);
750 750
      }
751 751
    };
752 752
  };
753 753
  
754 754
} //namespace lemon
755 755

	
756 756

	
757 757
#endif //LEMON_SMART_GRAPH_H
0 comments (0 inline)