3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_BITS_GRAPH_EXTENDER_H
20 #define LEMON_BITS_GRAPH_EXTENDER_H
22 #include <lemon/bits/invalid.h>
23 #include <lemon/bits/utility.h>
25 #include <lemon/bits/map_extender.h>
26 #include <lemon/bits/default_map.h>
28 #include <lemon/concept_check.h>
29 #include <lemon/concepts/maps.h>
33 ///\brief Extenders for the digraph types
36 /// \ingroup graphbits
38 /// \brief Extender for the Digraphs
39 template <typename Base>
40 class DigraphExtender : public Base {
44 typedef DigraphExtender Digraph;
48 typedef typename Parent::Node Node;
49 typedef typename Parent::Arc Arc;
51 int maxId(Node) const {
52 return Parent::maxNodeId();
55 int maxId(Arc) const {
56 return Parent::maxArcId();
59 Node fromId(int id, Node) const {
60 return Parent::nodeFromId(id);
63 Arc fromId(int id, Arc) const {
64 return Parent::arcFromId(id);
67 Node oppositeNode(const Node &n, const Arc &e) const {
68 if (n == Parent::source(e))
69 return Parent::target(e);
70 else if(n==Parent::target(e))
71 return Parent::source(e);
76 // Alterable extension
78 typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
79 typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
84 mutable NodeNotifier node_notifier;
85 mutable ArcNotifier arc_notifier;
89 NodeNotifier& notifier(Node) const {
93 ArcNotifier& notifier(Arc) const {
97 class NodeIt : public Node {
98 const Digraph* digraph;
103 NodeIt(Invalid i) : Node(i) { }
105 explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) {
106 _digraph.first(static_cast<Node&>(*this));
109 NodeIt(const Digraph& _digraph, const Node& node)
110 : Node(node), digraph(&_digraph) {}
112 NodeIt& operator++() {
113 digraph->next(*this);
120 class ArcIt : public Arc {
121 const Digraph* digraph;
126 ArcIt(Invalid i) : Arc(i) { }
128 explicit ArcIt(const Digraph& _digraph) : digraph(&_digraph) {
129 _digraph.first(static_cast<Arc&>(*this));
132 ArcIt(const Digraph& _digraph, const Arc& e) :
133 Arc(e), digraph(&_digraph) { }
135 ArcIt& operator++() {
136 digraph->next(*this);
143 class OutArcIt : public Arc {
144 const Digraph* digraph;
149 OutArcIt(Invalid i) : Arc(i) { }
151 OutArcIt(const Digraph& _digraph, const Node& node)
152 : digraph(&_digraph) {
153 _digraph.firstOut(*this, node);
156 OutArcIt(const Digraph& _digraph, const Arc& arc)
157 : Arc(arc), digraph(&_digraph) {}
159 OutArcIt& operator++() {
160 digraph->nextOut(*this);
167 class InArcIt : public Arc {
168 const Digraph* digraph;
173 InArcIt(Invalid i) : Arc(i) { }
175 InArcIt(const Digraph& _digraph, const Node& node)
176 : digraph(&_digraph) {
177 _digraph.firstIn(*this, node);
180 InArcIt(const Digraph& _digraph, const Arc& arc) :
181 Arc(arc), digraph(&_digraph) {}
183 InArcIt& operator++() {
184 digraph->nextIn(*this);
190 /// \brief Base node of the iterator
192 /// Returns the base node (i.e. the source in this case) of the iterator
193 Node baseNode(const OutArcIt &e) const {
194 return Parent::source(e);
196 /// \brief Running node of the iterator
198 /// Returns the running node (i.e. the target in this case) of the
200 Node runningNode(const OutArcIt &e) const {
201 return Parent::target(e);
204 /// \brief Base node of the iterator
206 /// Returns the base node (i.e. the target in this case) of the iterator
207 Node baseNode(const InArcIt &e) const {
208 return Parent::target(e);
210 /// \brief Running node of the iterator
212 /// Returns the running node (i.e. the source in this case) of the
214 Node runningNode(const InArcIt &e) const {
215 return Parent::source(e);
219 template <typename _Value>
221 : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
223 typedef DigraphExtender Digraph;
224 typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
226 explicit NodeMap(const Digraph& digraph)
228 NodeMap(const Digraph& digraph, const _Value& value)
229 : Parent(digraph, value) {}
231 NodeMap& operator=(const NodeMap& cmap) {
232 return operator=<NodeMap>(cmap);
235 template <typename CMap>
236 NodeMap& operator=(const CMap& cmap) {
237 Parent::operator=(cmap);
243 template <typename _Value>
245 : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
247 typedef DigraphExtender Digraph;
248 typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
250 explicit ArcMap(const Digraph& digraph)
252 ArcMap(const Digraph& digraph, const _Value& value)
253 : Parent(digraph, value) {}
255 ArcMap& operator=(const ArcMap& cmap) {
256 return operator=<ArcMap>(cmap);
259 template <typename CMap>
260 ArcMap& operator=(const CMap& cmap) {
261 Parent::operator=(cmap);
268 Node node = Parent::addNode();
269 notifier(Node()).add(node);
273 Arc addArc(const Node& from, const Node& to) {
274 Arc arc = Parent::addArc(from, to);
275 notifier(Arc()).add(arc);
280 notifier(Arc()).clear();
281 notifier(Node()).clear();
285 template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
286 void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
287 Parent::build(digraph, nodeRef, arcRef);
288 notifier(Node()).build();
289 notifier(Arc()).build();
292 void erase(const Node& node) {
294 Parent::firstOut(arc, node);
295 while (arc != INVALID ) {
297 Parent::firstOut(arc, node);
300 Parent::firstIn(arc, node);
301 while (arc != INVALID ) {
303 Parent::firstIn(arc, node);
306 notifier(Node()).erase(node);
310 void erase(const Arc& arc) {
311 notifier(Arc()).erase(arc);
316 node_notifier.setContainer(*this);
317 arc_notifier.setContainer(*this);
322 arc_notifier.clear();
323 node_notifier.clear();
327 /// \ingroup graphbits
329 /// \brief Extender for the Graphs
330 template <typename Base>
331 class GraphExtender : public Base {
335 typedef GraphExtender Digraph;
337 typedef True UndirectedTag;
339 typedef typename Parent::Node Node;
340 typedef typename Parent::Arc Arc;
341 typedef typename Parent::Edge Edge;
345 int maxId(Node) const {
346 return Parent::maxNodeId();
349 int maxId(Arc) const {
350 return Parent::maxArcId();
353 int maxId(Edge) const {
354 return Parent::maxEdgeId();
357 Node fromId(int id, Node) const {
358 return Parent::nodeFromId(id);
361 Arc fromId(int id, Arc) const {
362 return Parent::arcFromId(id);
365 Edge fromId(int id, Edge) const {
366 return Parent::edgeFromId(id);
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);
378 Arc oppositeArc(const Arc &e) const {
379 return Parent::direct(e, !Parent::direction(e));
382 using Parent::direct;
383 Arc direct(const Edge &ue, const Node &s) const {
384 return Parent::direct(ue, Parent::source(ue) == s);
387 // Alterable extension
389 typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
390 typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
391 typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
396 mutable NodeNotifier node_notifier;
397 mutable ArcNotifier arc_notifier;
398 mutable EdgeNotifier edge_notifier;
402 NodeNotifier& notifier(Node) const {
403 return node_notifier;
406 ArcNotifier& notifier(Arc) const {
410 EdgeNotifier& notifier(Edge) const {
411 return edge_notifier;
416 class NodeIt : public Node {
417 const Digraph* digraph;
422 NodeIt(Invalid i) : Node(i) { }
424 explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) {
425 _digraph.first(static_cast<Node&>(*this));
428 NodeIt(const Digraph& _digraph, const Node& node)
429 : Node(node), digraph(&_digraph) {}
431 NodeIt& operator++() {
432 digraph->next(*this);
439 class ArcIt : public Arc {
440 const Digraph* digraph;
445 ArcIt(Invalid i) : Arc(i) { }
447 explicit ArcIt(const Digraph& _digraph) : digraph(&_digraph) {
448 _digraph.first(static_cast<Arc&>(*this));
451 ArcIt(const Digraph& _digraph, const Arc& e) :
452 Arc(e), digraph(&_digraph) { }
454 ArcIt& operator++() {
455 digraph->next(*this);
462 class OutArcIt : public Arc {
463 const Digraph* digraph;
468 OutArcIt(Invalid i) : Arc(i) { }
470 OutArcIt(const Digraph& _digraph, const Node& node)
471 : digraph(&_digraph) {
472 _digraph.firstOut(*this, node);
475 OutArcIt(const Digraph& _digraph, const Arc& arc)
476 : Arc(arc), digraph(&_digraph) {}
478 OutArcIt& operator++() {
479 digraph->nextOut(*this);
486 class InArcIt : public Arc {
487 const Digraph* digraph;
492 InArcIt(Invalid i) : Arc(i) { }
494 InArcIt(const Digraph& _digraph, const Node& node)
495 : digraph(&_digraph) {
496 _digraph.firstIn(*this, node);
499 InArcIt(const Digraph& _digraph, const Arc& arc) :
500 Arc(arc), digraph(&_digraph) {}
502 InArcIt& operator++() {
503 digraph->nextIn(*this);
510 class EdgeIt : public Parent::Edge {
511 const Digraph* digraph;
516 EdgeIt(Invalid i) : Edge(i) { }
518 explicit EdgeIt(const Digraph& _digraph) : digraph(&_digraph) {
519 _digraph.first(static_cast<Edge&>(*this));
522 EdgeIt(const Digraph& _digraph, const Edge& e) :
523 Edge(e), digraph(&_digraph) { }
525 EdgeIt& operator++() {
526 digraph->next(*this);
532 class IncArcIt : public Parent::Edge {
533 friend class GraphExtender;
534 const Digraph* digraph;
540 IncArcIt(Invalid i) : Edge(i), direction(false) { }
542 IncArcIt(const Digraph& _digraph, const Node &n) : digraph(&_digraph) {
543 _digraph.firstInc(*this, direction, n);
546 IncArcIt(const Digraph& _digraph, const Edge &ue, const Node &n)
547 : digraph(&_digraph), Edge(ue) {
548 direction = (_digraph.source(ue) == n);
551 IncArcIt& operator++() {
552 digraph->nextInc(*this, direction);
557 /// \brief Base node of the iterator
559 /// Returns the base node (ie. the source in this case) of the iterator
560 Node baseNode(const OutArcIt &e) const {
561 return Parent::source(static_cast<const Arc&>(e));
563 /// \brief Running node of the iterator
565 /// Returns the running node (ie. the target in this case) of the
567 Node runningNode(const OutArcIt &e) const {
568 return Parent::target(static_cast<const Arc&>(e));
571 /// \brief Base node of the iterator
573 /// Returns the base node (ie. the target in this case) of the iterator
574 Node baseNode(const InArcIt &e) const {
575 return Parent::target(static_cast<const Arc&>(e));
577 /// \brief Running node of the iterator
579 /// Returns the running node (ie. the source in this case) of the
581 Node runningNode(const InArcIt &e) const {
582 return Parent::source(static_cast<const Arc&>(e));
585 /// Base node of the iterator
587 /// Returns the base node of the iterator
588 Node baseNode(const IncArcIt &e) const {
589 return e.direction ? source(e) : target(e);
591 /// Running node of the iterator
593 /// Returns the running node of the iterator
594 Node runningNode(const IncArcIt &e) const {
595 return e.direction ? target(e) : source(e);
598 // Mappable extension
600 template <typename _Value>
602 : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
604 typedef GraphExtender Digraph;
605 typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
607 NodeMap(const Digraph& digraph)
609 NodeMap(const Digraph& digraph, const _Value& value)
610 : Parent(digraph, value) {}
612 NodeMap& operator=(const NodeMap& cmap) {
613 return operator=<NodeMap>(cmap);
616 template <typename CMap>
617 NodeMap& operator=(const CMap& cmap) {
618 Parent::operator=(cmap);
624 template <typename _Value>
626 : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
628 typedef GraphExtender Digraph;
629 typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
631 ArcMap(const Digraph& digraph)
633 ArcMap(const Digraph& digraph, const _Value& value)
634 : Parent(digraph, value) {}
636 ArcMap& operator=(const ArcMap& cmap) {
637 return operator=<ArcMap>(cmap);
640 template <typename CMap>
641 ArcMap& operator=(const CMap& cmap) {
642 Parent::operator=(cmap);
648 template <typename _Value>
650 : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
652 typedef GraphExtender Digraph;
653 typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
655 EdgeMap(const Digraph& digraph)
658 EdgeMap(const Digraph& digraph, const _Value& value)
659 : Parent(digraph, value) {}
661 EdgeMap& operator=(const EdgeMap& cmap) {
662 return operator=<EdgeMap>(cmap);
665 template <typename CMap>
666 EdgeMap& operator=(const CMap& cmap) {
667 Parent::operator=(cmap);
673 // Alteration extension
676 Node node = Parent::addNode();
677 notifier(Node()).add(node);
681 Edge addEdge(const Node& from, const Node& to) {
682 Edge edge = Parent::addEdge(from, to);
683 notifier(Edge()).add(edge);
685 ev.push_back(Parent::direct(edge, true));
686 ev.push_back(Parent::direct(edge, false));
687 notifier(Arc()).add(ev);
692 notifier(Arc()).clear();
693 notifier(Edge()).clear();
694 notifier(Node()).clear();
698 template <typename Digraph, typename NodeRefMap, typename EdgeRefMap>
699 void build(const Digraph& digraph, NodeRefMap& nodeRef,
700 EdgeRefMap& edgeRef) {
701 Parent::build(digraph, nodeRef, edgeRef);
702 notifier(Node()).build();
703 notifier(Edge()).build();
704 notifier(Arc()).build();
707 void erase(const Node& node) {
709 Parent::firstOut(arc, node);
710 while (arc != INVALID ) {
712 Parent::firstOut(arc, node);
715 Parent::firstIn(arc, node);
716 while (arc != INVALID ) {
718 Parent::firstIn(arc, node);
721 notifier(Node()).erase(node);
725 void erase(const Edge& edge) {
727 ev.push_back(Parent::direct(edge, true));
728 ev.push_back(Parent::direct(edge, false));
729 notifier(Arc()).erase(ev);
730 notifier(Edge()).erase(edge);
735 node_notifier.setContainer(*this);
736 arc_notifier.setContainer(*this);
737 edge_notifier.setContainer(*this);
741 edge_notifier.clear();
742 arc_notifier.clear();
743 node_notifier.clear();