Location: LEMON/LEMON-main/lemon/bits/graph_extender.h

Load file history
gravatar
alpar (Alpar Juttner)
Further extension of the migration guide
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
#define LEMON_BITS_GRAPH_EXTENDER_H
#include <lemon/core.h>
#include <lemon/bits/map_extender.h>
#include <lemon/bits/default_map.h>
#include <lemon/concept_check.h>
#include <lemon/concepts/maps.h>
///\ingroup graphbits
///\file
///\brief Extenders for the digraph types
namespace lemon {
/// \ingroup graphbits
///
/// \brief Extender for the Digraphs
template <typename Base>
class DigraphExtender : public Base {
public:
typedef Base Parent;
typedef DigraphExtender Digraph;
// Base extensions
typedef typename Parent::Node Node;
typedef typename Parent::Arc Arc;
int maxId(Node) const {
return Parent::maxNodeId();
}
int maxId(Arc) const {
return Parent::maxArcId();
}
Node fromId(int id, Node) const {
return Parent::nodeFromId(id);
}
Arc fromId(int id, Arc) const {
return Parent::arcFromId(id);
}
Node oppositeNode(const Node &node, const Arc &arc) const {
if (node == Parent::source(arc))
return Parent::target(arc);
else if(node == Parent::target(arc))
return Parent::source(arc);
else
return INVALID;
}
// Alterable extension
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
protected:
mutable NodeNotifier node_notifier;
mutable ArcNotifier arc_notifier;
public:
NodeNotifier& notifier(Node) const {
return node_notifier;
}
ArcNotifier& notifier(Arc) const {
return arc_notifier;
}
class NodeIt : public Node {
const Digraph* _digraph;
public:
NodeIt() {}
NodeIt(Invalid i) : Node(i) { }
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
_digraph->first(static_cast<Node&>(*this));
}
NodeIt(const Digraph& digraph, const Node& node)
: Node(node), _digraph(&digraph) {}
NodeIt& operator++() {
_digraph->next(*this);
return *this;
}
};
class ArcIt : public Arc {
const Digraph* _digraph;
public:
ArcIt() { }
ArcIt(Invalid i) : Arc(i) { }
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
_digraph->first(static_cast<Arc&>(*this));
}
ArcIt(const Digraph& digraph, const Arc& arc) :
Arc(arc), _digraph(&digraph) { }
ArcIt& operator++() {
_digraph->next(*this);
return *this;
}
};
class OutArcIt : public Arc {
const Digraph* _digraph;
public:
OutArcIt() { }
OutArcIt(Invalid i) : Arc(i) { }
OutArcIt(const Digraph& digraph, const Node& node)
: _digraph(&digraph) {
_digraph->firstOut(*this, node);
}
OutArcIt(const Digraph& digraph, const Arc& arc)
: Arc(arc), _digraph(&digraph) {}
OutArcIt& operator++() {
_digraph->nextOut(*this);
return *this;
}
};
class InArcIt : public Arc {
const Digraph* _digraph;
public:
InArcIt() { }
InArcIt(Invalid i) : Arc(i) { }
InArcIt(const Digraph& digraph, const Node& node)
: _digraph(&digraph) {
_digraph->firstIn(*this, node);
}
InArcIt(const Digraph& digraph, const Arc& arc) :
Arc(arc), _digraph(&digraph) {}
InArcIt& operator++() {
_digraph->nextIn(*this);
return *this;
}
};
/// \brief Base node of the iterator
///
/// Returns the base node (i.e. the source in this case) of the iterator
Node baseNode(const OutArcIt &arc) const {
return Parent::source(arc);
}
/// \brief Running node of the iterator
///
/// Returns the running node (i.e. the target in this case) of the
/// iterator
Node runningNode(const OutArcIt &arc) const {
return Parent::target(arc);
}
/// \brief Base node of the iterator
///
/// Returns the base node (i.e. the target in this case) of the iterator
Node baseNode(const InArcIt &arc) const {
return Parent::target(arc);
}
/// \brief Running node of the iterator
///
/// Returns the running node (i.e. the source in this case) of the
/// iterator
Node runningNode(const InArcIt &arc) const {
return Parent::source(arc);
}
template <typename _Value>
class NodeMap
: public MapExtender<DefaultMap<Digraph, Node, _Value> > {
public:
typedef DigraphExtender Digraph;
typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
explicit NodeMap(const Digraph& digraph)
: Parent(digraph) {}
NodeMap(const Digraph& digraph, const _Value& value)
: Parent(digraph, value) {}
private:
NodeMap& operator=(const NodeMap& cmap) {
return operator=<NodeMap>(cmap);
}
template <typename CMap>
NodeMap& operator=(const CMap& cmap) {
Parent::operator=(cmap);
return *this;
}
};
template <typename _Value>
class ArcMap
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
public:
typedef DigraphExtender Digraph;
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
explicit ArcMap(const Digraph& digraph)
: Parent(digraph) {}
ArcMap(const Digraph& digraph, const _Value& value)
: Parent(digraph, value) {}
private:
ArcMap& operator=(const ArcMap& cmap) {
return operator=<ArcMap>(cmap);
}
template <typename CMap>
ArcMap& operator=(const CMap& cmap) {
Parent::operator=(cmap);
return *this;
}
};
Node addNode() {
Node node = Parent::addNode();
notifier(Node()).add(node);
return node;
}
Arc addArc(const Node& from, const Node& to) {
Arc arc = Parent::addArc(from, to);
notifier(Arc()).add(arc);
return arc;
}
void clear() {
notifier(Arc()).clear();
notifier(Node()).clear();
Parent::clear();
}
template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
Parent::build(digraph, nodeRef, arcRef);
notifier(Node()).build();
notifier(Arc()).build();
}
void erase(const Node& node) {
Arc arc;
Parent::firstOut(arc, node);
while (arc != INVALID ) {
erase(arc);
Parent::firstOut(arc, node);
}
Parent::firstIn(arc, node);
while (arc != INVALID ) {
erase(arc);
Parent::firstIn(arc, node);
}
notifier(Node()).erase(node);
Parent::erase(node);
}
void erase(const Arc& arc) {
notifier(Arc()).erase(arc);
Parent::erase(arc);
}
DigraphExtender() {
node_notifier.setContainer(*this);
arc_notifier.setContainer(*this);
}
~DigraphExtender() {
arc_notifier.clear();
node_notifier.clear();
}
};
/// \ingroup _graphbits
///
/// \brief Extender for the Graphs
template <typename Base>
class GraphExtender : public Base {
public:
typedef Base Parent;
typedef GraphExtender Graph;
typedef True UndirectedTag;
typedef typename Parent::Node Node;
typedef typename Parent::Arc Arc;
typedef typename Parent::Edge Edge;
// Graph extension
int maxId(Node) const {
return Parent::maxNodeId();
}
int maxId(Arc) const {
return Parent::maxArcId();
}
int maxId(Edge) const {
return Parent::maxEdgeId();
}
Node fromId(int id, Node) const {
return Parent::nodeFromId(id);
}
Arc fromId(int id, Arc) const {
return Parent::arcFromId(id);
}
Edge fromId(int id, Edge) const {
return Parent::edgeFromId(id);
}
Node oppositeNode(const Node &n, const Edge &e) const {
if( n == Parent::u(e))
return Parent::v(e);
else if( n == Parent::v(e))
return Parent::u(e);
else
return INVALID;
}
Arc oppositeArc(const Arc &arc) const {
return Parent::direct(arc, !Parent::direction(arc));
}
using Parent::direct;
Arc direct(const Edge &edge, const Node &node) const {
return Parent::direct(edge, Parent::u(edge) == node);
}
// Alterable extension
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
protected:
mutable NodeNotifier node_notifier;
mutable ArcNotifier arc_notifier;
mutable EdgeNotifier edge_notifier;
public:
NodeNotifier& notifier(Node) const {
return node_notifier;
}
ArcNotifier& notifier(Arc) const {
return arc_notifier;
}
EdgeNotifier& notifier(Edge) const {
return edge_notifier;
}
class NodeIt : public Node {
const Graph* _graph;
public:
NodeIt() {}
NodeIt(Invalid i) : Node(i) { }
explicit NodeIt(const Graph& graph) : _graph(&graph) {
_graph->first(static_cast<Node&>(*this));
}
NodeIt(const Graph& graph, const Node& node)
: Node(node), _graph(&graph) {}
NodeIt& operator++() {
_graph->next(*this);
return *this;
}
};
class ArcIt : public Arc {
const Graph* _graph;
public:
ArcIt() { }
ArcIt(Invalid i) : Arc(i) { }
explicit ArcIt(const Graph& graph) : _graph(&graph) {
_graph->first(static_cast<Arc&>(*this));
}
ArcIt(const Graph& graph, const Arc& arc) :
Arc(arc), _graph(&graph) { }
ArcIt& operator++() {
_graph->next(*this);
return *this;
}
};
class OutArcIt : public Arc {
const Graph* _graph;
public:
OutArcIt() { }
OutArcIt(Invalid i) : Arc(i) { }
OutArcIt(const Graph& graph, const Node& node)
: _graph(&graph) {
_graph->firstOut(*this, node);
}
OutArcIt(const Graph& graph, const Arc& arc)
: Arc(arc), _graph(&graph) {}
OutArcIt& operator++() {
_graph->nextOut(*this);
return *this;
}
};
class InArcIt : public Arc {
const Graph* _graph;
public:
InArcIt() { }
InArcIt(Invalid i) : Arc(i) { }
InArcIt(const Graph& graph, const Node& node)
: _graph(&graph) {
_graph->firstIn(*this, node);
}
InArcIt(const Graph& graph, const Arc& arc) :
Arc(arc), _graph(&graph) {}
InArcIt& operator++() {
_graph->nextIn(*this);
return *this;
}
};
class EdgeIt : public Parent::Edge {
const Graph* _graph;
public:
EdgeIt() { }
EdgeIt(Invalid i) : Edge(i) { }
explicit EdgeIt(const Graph& graph) : _graph(&graph) {
_graph->first(static_cast<Edge&>(*this));
}
EdgeIt(const Graph& graph, const Edge& edge) :
Edge(edge), _graph(&graph) { }
EdgeIt& operator++() {
_graph->next(*this);
return *this;
}
};
class IncEdgeIt : public Parent::Edge {
friend class GraphExtender;
const Graph* _graph;
bool _direction;
public:
IncEdgeIt() { }
IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
_graph->firstInc(*this, _direction, node);
}
IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
: _graph(&graph), Edge(edge) {
_direction = (_graph->source(edge) == node);
}
IncEdgeIt& operator++() {
_graph->nextInc(*this, _direction);
return *this;
}
};
/// \brief Base node of the iterator
///
/// Returns the base node (ie. the source in this case) of the iterator
Node baseNode(const OutArcIt &arc) const {
return Parent::source(static_cast<const Arc&>(arc));
}
/// \brief Running node of the iterator
///
/// Returns the running node (ie. the target in this case) of the
/// iterator
Node runningNode(const OutArcIt &arc) const {
return Parent::target(static_cast<const Arc&>(arc));
}
/// \brief Base node of the iterator
///
/// Returns the base node (ie. the target in this case) of the iterator
Node baseNode(const InArcIt &arc) const {
return Parent::target(static_cast<const Arc&>(arc));
}
/// \brief Running node of the iterator
///
/// Returns the running node (ie. the source in this case) of the
/// iterator
Node runningNode(const InArcIt &arc) const {
return Parent::source(static_cast<const Arc&>(arc));
}
/// Base node of the iterator
///
/// Returns the base node of the iterator
Node baseNode(const IncEdgeIt &edge) const {
return edge._direction ? u(edge) : v(edge);
}
/// Running node of the iterator
///
/// Returns the running node of the iterator
Node runningNode(const IncEdgeIt &edge) const {
return edge._direction ? v(edge) : u(edge);
}
// Mappable extension
template <typename _Value>
class NodeMap
: public MapExtender<DefaultMap<Graph, Node, _Value> > {
public:
typedef GraphExtender Graph;
typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
NodeMap(const Graph& graph)
: Parent(graph) {}
NodeMap(const Graph& graph, const _Value& value)
: Parent(graph, value) {}
private:
NodeMap& operator=(const NodeMap& cmap) {
return operator=<NodeMap>(cmap);
}
template <typename CMap>
NodeMap& operator=(const CMap& cmap) {
Parent::operator=(cmap);
return *this;
}
};
template <typename _Value>
class ArcMap
: public MapExtender<DefaultMap<Graph, Arc, _Value> > {
public:
typedef GraphExtender Graph;
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
ArcMap(const Graph& graph)
: Parent(graph) {}
ArcMap(const Graph& graph, const _Value& value)
: Parent(graph, value) {}
private:
ArcMap& operator=(const ArcMap& cmap) {
return operator=<ArcMap>(cmap);
}
template <typename CMap>
ArcMap& operator=(const CMap& cmap) {
Parent::operator=(cmap);
return *this;
}
};
template <typename _Value>
class EdgeMap
: public MapExtender<DefaultMap<Graph, Edge, _Value> > {
public:
typedef GraphExtender Graph;
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
EdgeMap(const Graph& graph)
: Parent(graph) {}
EdgeMap(const Graph& graph, const _Value& value)
: Parent(graph, value) {}
private:
EdgeMap& operator=(const EdgeMap& cmap) {
return operator=<EdgeMap>(cmap);
}
template <typename CMap>
EdgeMap& operator=(const CMap& cmap) {
Parent::operator=(cmap);
return *this;
}
};
// Alteration extension
Node addNode() {
Node node = Parent::addNode();
notifier(Node()).add(node);
return node;
}
Edge addEdge(const Node& from, const Node& to) {
Edge edge = Parent::addEdge(from, to);
notifier(Edge()).add(edge);
std::vector<Arc> ev;
ev.push_back(Parent::direct(edge, true));
ev.push_back(Parent::direct(edge, false));
notifier(Arc()).add(ev);
return edge;
}
void clear() {
notifier(Arc()).clear();
notifier(Edge()).clear();
notifier(Node()).clear();
Parent::clear();
}
template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
void build(const Graph& graph, NodeRefMap& nodeRef,
EdgeRefMap& edgeRef) {
Parent::build(graph, nodeRef, edgeRef);
notifier(Node()).build();
notifier(Edge()).build();
notifier(Arc()).build();
}
void erase(const Node& node) {
Arc arc;
Parent::firstOut(arc, node);
while (arc != INVALID ) {
erase(arc);
Parent::firstOut(arc, node);
}
Parent::firstIn(arc, node);
while (arc != INVALID ) {
erase(arc);
Parent::firstIn(arc, node);
}
notifier(Node()).erase(node);
Parent::erase(node);
}
void erase(const Edge& edge) {
std::vector<Arc> av;
av.push_back(Parent::direct(edge, true));
av.push_back(Parent::direct(edge, false));
notifier(Arc()).erase(av);
notifier(Edge()).erase(edge);
Parent::erase(edge);
}
GraphExtender() {
node_notifier.setContainer(*this);
arc_notifier.setContainer(*this);
edge_notifier.setContainer(*this);
}
~GraphExtender() {
edge_notifier.clear();
arc_notifier.clear();
node_notifier.clear();
}
};
}
#endif