3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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_GRAPH_EXTENDER_H
20 #define LEMON_GRAPH_EXTENDER_H
22 #include <lemon/invalid.h>
23 #include <lemon/error.h>
25 #include <lemon/bits/default_map.h>
29 template <typename Base>
30 class GraphExtender : public Base {
34 typedef GraphExtender Graph;
38 typedef typename Parent::Node Node;
39 typedef typename Parent::Edge Edge;
41 int maxId(Node) const {
42 return Parent::maxNodeId();
45 int maxId(Edge) const {
46 return Parent::maxEdgeId();
49 Node fromId(int id, Node) const {
50 return Parent::nodeFromId(id);
53 Edge fromId(int id, Edge) const {
54 return Parent::edgeFromId(id);
57 Node oppositeNode(const Node &n, const Edge &e) const {
58 if (n == Parent::source(e))
59 return Parent::target(e);
60 else if(n==Parent::target(e))
61 return Parent::source(e);
66 // Alterable extension
68 typedef AlterationNotifier<Node> NodeNotifier;
69 typedef AlterationNotifier<Edge> EdgeNotifier;
74 mutable NodeNotifier node_notifier;
75 mutable EdgeNotifier edge_notifier;
79 NodeNotifier& getNotifier(Node) const {
83 EdgeNotifier& getNotifier(Edge) const {
87 class NodeIt : public Node {
93 NodeIt(Invalid i) : Node(i) { }
95 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
96 _graph.first(*static_cast<Node*>(this));
99 NodeIt(const Graph& _graph, const Node& node)
100 : Node(node), graph(&_graph) {}
102 NodeIt& operator++() {
110 class EdgeIt : public Edge {
116 EdgeIt(Invalid i) : Edge(i) { }
118 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
119 _graph.first(*static_cast<Edge*>(this));
122 EdgeIt(const Graph& _graph, const Edge& e) :
123 Edge(e), graph(&_graph) { }
125 EdgeIt& operator++() {
133 class OutEdgeIt : public Edge {
139 OutEdgeIt(Invalid i) : Edge(i) { }
141 OutEdgeIt(const Graph& _graph, const Node& node)
143 _graph.firstOut(*this, node);
146 OutEdgeIt(const Graph& _graph, const Edge& edge)
147 : Edge(edge), graph(&_graph) {}
149 OutEdgeIt& operator++() {
150 graph->nextOut(*this);
157 class InEdgeIt : public Edge {
163 InEdgeIt(Invalid i) : Edge(i) { }
165 InEdgeIt(const Graph& _graph, const Node& node)
167 _graph.firstIn(*this, node);
170 InEdgeIt(const Graph& _graph, const Edge& edge) :
171 Edge(edge), graph(&_graph) {}
173 InEdgeIt& operator++() {
174 graph->nextIn(*this);
180 /// \brief Base node of the iterator
182 /// Returns the base node (ie. the source in this case) of the iterator
183 Node baseNode(const OutEdgeIt &e) const {
184 return Parent::source(e);
186 /// \brief Running node of the iterator
188 /// Returns the running node (ie. the target in this case) of the
190 Node runningNode(const OutEdgeIt &e) const {
191 return Parent::target(e);
194 /// \brief Base node of the iterator
196 /// Returns the base node (ie. the target in this case) of the iterator
197 Node baseNode(const InEdgeIt &e) const {
198 return Parent::target(e);
200 /// \brief Running node of the iterator
202 /// Returns the running node (ie. the source in this case) of the
204 Node runningNode(const InEdgeIt &e) const {
205 return Parent::source(e);
209 template <typename _Value>
211 : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
213 typedef GraphExtender Graph;
214 typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
216 NodeMap(const Graph& _g)
218 NodeMap(const Graph& _g, const _Value& _v)
221 NodeMap& operator=(const NodeMap& cmap) {
222 return operator=<NodeMap>(cmap);
226 /// \brief Template assign operator.
228 /// The given parameter should be conform to the ReadMap
229 /// concecpt and could be indiced by the current item set of
230 /// the NodeMap. In this case the value for each item
231 /// is assigned by the value of the given ReadMap.
232 template <typename CMap>
233 NodeMap& operator=(const CMap& cmap) {
234 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
235 const typename Parent::Graph* graph = Parent::getGraph();
237 for (graph->first(it); it != INVALID; graph->next(it)) {
238 Parent::set(it, cmap[it]);
245 template <typename _Value>
247 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
249 typedef GraphExtender Graph;
250 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
252 EdgeMap(const Graph& _g)
254 EdgeMap(const Graph& _g, const _Value& _v)
257 EdgeMap& operator=(const EdgeMap& cmap) {
258 return operator=<EdgeMap>(cmap);
261 template <typename CMap>
262 EdgeMap& operator=(const CMap& cmap) {
263 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
264 const typename Parent::Graph* graph = Parent::getGraph();
266 for (graph->first(it); it != INVALID; graph->next(it)) {
267 Parent::set(it, cmap[it]);
275 Node node = Parent::addNode();
276 getNotifier(Node()).add(node);
280 Edge addEdge(const Node& from, const Node& to) {
281 Edge edge = Parent::addEdge(from, to);
282 getNotifier(Edge()).add(edge);
287 getNotifier(Edge()).clear();
288 getNotifier(Node()).clear();
293 void erase(const Node& node) {
295 Parent::firstOut(edge, node);
296 while (edge != INVALID ) {
298 Parent::firstOut(edge, node);
301 Parent::firstIn(edge, node);
302 while (edge != INVALID ) {
304 Parent::firstIn(edge, node);
307 getNotifier(Node()).erase(node);
311 void erase(const Edge& edge) {
312 getNotifier(Edge()).erase(edge);
318 getNotifier(Edge()).clear();
319 getNotifier(Node()).clear();
323 template <typename Base>
324 class UGraphBaseExtender : public Base {
329 typedef typename Parent::Edge UEdge;
330 typedef typename Parent::Node Node;
332 typedef True UndirectedTag;
334 class Edge : public UEdge {
335 friend class UGraphBaseExtender;
340 Edge(const UEdge &ue, bool _forward) :
341 UEdge(ue), forward(_forward) {}
346 /// Invalid edge constructor
347 Edge(Invalid i) : UEdge(i), forward(true) {}
349 bool operator==(const Edge &that) const {
350 return forward==that.forward && UEdge(*this)==UEdge(that);
352 bool operator!=(const Edge &that) const {
353 return forward!=that.forward || UEdge(*this)!=UEdge(that);
355 bool operator<(const Edge &that) const {
356 return forward<that.forward ||
357 (!(that.forward<forward) && UEdge(*this)<UEdge(that));
363 using Parent::source;
365 /// Source of the given Edge.
366 Node source(const Edge &e) const {
367 return e.forward ? Parent::source(e) : Parent::target(e);
370 using Parent::target;
372 /// Target of the given Edge.
373 Node target(const Edge &e) const {
374 return e.forward ? Parent::target(e) : Parent::source(e);
377 /// \brief Directed edge from an undirected edge.
379 /// Returns a directed edge corresponding to the specified UEdge.
380 /// If the given bool is true the given undirected edge and the
381 /// returned edge have the same source node.
382 static Edge direct(const UEdge &ue, bool d) {
386 /// Returns whether the given directed edge is same orientation as the
387 /// corresponding undirected edge.
389 /// \todo reference to the corresponding point of the undirected graph
390 /// concept. "What does the direction of an undirected edge mean?"
391 static bool direction(const Edge &e) { return e.forward; }
397 void first(Edge &e) const {
402 void next(Edge &e) const {
412 void firstOut(Edge &e, const Node &n) const {
413 Parent::firstIn(e,n);
414 if( UEdge(e) != INVALID ) {
418 Parent::firstOut(e,n);
422 void nextOut(Edge &e) const {
424 Node n = Parent::target(e);
426 if( UEdge(e) == INVALID ) {
427 Parent::firstOut(e, n);
436 void firstIn(Edge &e, const Node &n) const {
437 Parent::firstOut(e,n);
438 if( UEdge(e) != INVALID ) {
442 Parent::firstIn(e,n);
446 void nextIn(Edge &e) const {
448 Node n = Parent::source(e);
450 if( UEdge(e) == INVALID ) {
451 Parent::firstIn(e, n);
460 void firstInc(UEdge &e, bool &d, const Node &n) const {
462 Parent::firstOut(e, n);
463 if (e != INVALID) return;
465 Parent::firstIn(e, n);
468 void nextInc(UEdge &e, bool &d) const {
470 Node s = Parent::source(e);
472 if (e != INVALID) return;
474 Parent::firstIn(e, s);
480 Node nodeFromId(int id) const {
481 return Parent::nodeFromId(id);
484 Edge edgeFromId(int id) const {
485 return direct(Parent::edgeFromId(id >> 1), bool(id & 1));
488 UEdge uEdgeFromId(int id) const {
489 return Parent::edgeFromId(id >> 1);
492 int id(const Node &n) const {
493 return Parent::id(n);
496 int id(const UEdge &e) const {
497 return Parent::id(e);
500 int id(const Edge &e) const {
501 return 2 * Parent::id(e) + int(e.forward);
504 int maxNodeId() const {
505 return Parent::maxNodeId();
508 int maxEdgeId() const {
509 return 2 * Parent::maxEdgeId() + 1;
512 int maxUEdgeId() const {
513 return Parent::maxEdgeId();
517 int edgeNum() const {
518 return 2 * Parent::edgeNum();
521 int uEdgeNum() const {
522 return Parent::edgeNum();
525 Edge findEdge(Node source, Node target, Edge prev) const {
526 if (prev == INVALID) {
527 UEdge edge = Parent::findEdge(source, target);
528 if (edge != INVALID) return direct(edge, true);
529 edge = Parent::findEdge(target, source);
530 if (edge != INVALID) return direct(edge, false);
531 } else if (direction(prev)) {
532 UEdge edge = Parent::findEdge(source, target, prev);
533 if (edge != INVALID) return direct(edge, true);
534 edge = Parent::findEdge(target, source);
535 if (edge != INVALID) return direct(edge, false);
537 UEdge edge = Parent::findEdge(target, source, prev);
538 if (edge != INVALID) return direct(edge, false);
543 UEdge findUEdge(Node source, Node target, UEdge prev) const {
544 if (prev == INVALID) {
545 UEdge edge = Parent::findEdge(source, target);
546 if (edge != INVALID) return edge;
547 edge = Parent::findEdge(target, source);
548 if (edge != INVALID) return edge;
549 } else if (Parent::source(prev) == source) {
550 UEdge edge = Parent::findEdge(source, target, prev);
551 if (edge != INVALID) return edge;
552 edge = Parent::findEdge(target, source);
553 if (edge != INVALID) return edge;
555 UEdge edge = Parent::findEdge(target, source, prev);
556 if (edge != INVALID) return edge;
563 template <typename Base>
564 class UGraphExtender : public Base {
568 typedef UGraphExtender Graph;
570 typedef typename Parent::Node Node;
571 typedef typename Parent::Edge Edge;
572 typedef typename Parent::UEdge UEdge;
576 int maxId(Node) const {
577 return Parent::maxNodeId();
580 int maxId(Edge) const {
581 return Parent::maxEdgeId();
584 int maxId(UEdge) const {
585 return Parent::maxUEdgeId();
588 Node fromId(int id, Node) const {
589 return Parent::nodeFromId(id);
592 Edge fromId(int id, Edge) const {
593 return Parent::edgeFromId(id);
596 UEdge fromId(int id, UEdge) const {
597 return Parent::uEdgeFromId(id);
600 Node oppositeNode(const Node &n, const UEdge &e) const {
601 if( n == Parent::source(e))
602 return Parent::target(e);
603 else if( n == Parent::target(e))
604 return Parent::source(e);
609 Edge oppositeEdge(const Edge &e) const {
610 return Parent::direct(e, !Parent::direction(e));
613 using Parent::direct;
614 Edge direct(const UEdge &ue, const Node &s) const {
615 return Parent::direct(ue, Parent::source(ue) == s);
618 // Alterable extension
620 typedef AlterationNotifier<Node> NodeNotifier;
621 typedef AlterationNotifier<Edge> EdgeNotifier;
622 typedef AlterationNotifier<UEdge> UEdgeNotifier;
627 mutable NodeNotifier node_notifier;
628 mutable EdgeNotifier edge_notifier;
629 mutable UEdgeNotifier uedge_notifier;
633 NodeNotifier& getNotifier(Node) const {
634 return node_notifier;
637 EdgeNotifier& getNotifier(Edge) const {
638 return edge_notifier;
641 UEdgeNotifier& getNotifier(UEdge) const {
642 return uedge_notifier;
647 class NodeIt : public Node {
653 NodeIt(Invalid i) : Node(i) { }
655 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
656 _graph.first(*static_cast<Node*>(this));
659 NodeIt(const Graph& _graph, const Node& node)
660 : Node(node), graph(&_graph) {}
662 NodeIt& operator++() {
670 class EdgeIt : public Edge {
676 EdgeIt(Invalid i) : Edge(i) { }
678 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
679 _graph.first(*static_cast<Edge*>(this));
682 EdgeIt(const Graph& _graph, const Edge& e) :
683 Edge(e), graph(&_graph) { }
685 EdgeIt& operator++() {
693 class OutEdgeIt : public Edge {
699 OutEdgeIt(Invalid i) : Edge(i) { }
701 OutEdgeIt(const Graph& _graph, const Node& node)
703 _graph.firstOut(*this, node);
706 OutEdgeIt(const Graph& _graph, const Edge& edge)
707 : Edge(edge), graph(&_graph) {}
709 OutEdgeIt& operator++() {
710 graph->nextOut(*this);
717 class InEdgeIt : public Edge {
723 InEdgeIt(Invalid i) : Edge(i) { }
725 InEdgeIt(const Graph& _graph, const Node& node)
727 _graph.firstIn(*this, node);
730 InEdgeIt(const Graph& _graph, const Edge& edge) :
731 Edge(edge), graph(&_graph) {}
733 InEdgeIt& operator++() {
734 graph->nextIn(*this);
741 class UEdgeIt : public Parent::UEdge {
747 UEdgeIt(Invalid i) : UEdge(i) { }
749 explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
750 _graph.first(*static_cast<UEdge*>(this));
753 UEdgeIt(const Graph& _graph, const UEdge& e) :
754 UEdge(e), graph(&_graph) { }
756 UEdgeIt& operator++() {
763 class IncEdgeIt : public Parent::UEdge {
764 friend class UGraphExtender;
771 IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
773 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
774 _graph.firstInc(*this, direction, n);
777 IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
778 : graph(&_graph), UEdge(ue) {
779 direction = (_graph.source(ue) == n);
782 IncEdgeIt& operator++() {
783 graph->nextInc(*this, direction);
788 /// \brief Base node of the iterator
790 /// Returns the base node (ie. the source in this case) of the iterator
791 Node baseNode(const OutEdgeIt &e) const {
792 return Parent::source((Edge)e);
794 /// \brief Running node of the iterator
796 /// Returns the running node (ie. the target in this case) of the
798 Node runningNode(const OutEdgeIt &e) const {
799 return Parent::target((Edge)e);
802 /// \brief Base node of the iterator
804 /// Returns the base node (ie. the target in this case) of the iterator
805 Node baseNode(const InEdgeIt &e) const {
806 return Parent::target((Edge)e);
808 /// \brief Running node of the iterator
810 /// Returns the running node (ie. the source in this case) of the
812 Node runningNode(const InEdgeIt &e) const {
813 return Parent::source((Edge)e);
816 /// Base node of the iterator
818 /// Returns the base node of the iterator
819 Node baseNode(const IncEdgeIt &e) const {
820 return e.direction ? source(e) : target(e);
822 /// Running node of the iterator
824 /// Returns the running node of the iterator
825 Node runningNode(const IncEdgeIt &e) const {
826 return e.direction ? target(e) : source(e);
829 // Mappable extension
831 template <typename _Value>
833 : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
835 typedef UGraphExtender Graph;
836 typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
838 NodeMap(const Graph& _g)
840 NodeMap(const Graph& _g, const _Value& _v)
843 NodeMap& operator=(const NodeMap& cmap) {
844 return operator=<NodeMap>(cmap);
848 /// \brief Template assign operator.
850 /// The given parameter should be conform to the ReadMap
851 /// concecpt and could be indiced by the current item set of
852 /// the NodeMap. In this case the value for each item
853 /// is assigned by the value of the given ReadMap.
854 template <typename CMap>
855 NodeMap& operator=(const CMap& cmap) {
856 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
857 const typename Parent::Graph* graph = Parent::getGraph();
859 for (graph->first(it); it != INVALID; graph->next(it)) {
860 Parent::set(it, cmap[it]);
867 template <typename _Value>
869 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
871 typedef UGraphExtender Graph;
872 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
874 EdgeMap(const Graph& _g)
876 EdgeMap(const Graph& _g, const _Value& _v)
879 EdgeMap& operator=(const EdgeMap& cmap) {
880 return operator=<EdgeMap>(cmap);
883 template <typename CMap>
884 EdgeMap& operator=(const CMap& cmap) {
885 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
886 const typename Parent::Graph* graph = Parent::getGraph();
888 for (graph->first(it); it != INVALID; graph->next(it)) {
889 Parent::set(it, cmap[it]);
896 template <typename _Value>
898 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
900 typedef UGraphExtender Graph;
901 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
903 UEdgeMap(const Graph& _g)
905 UEdgeMap(const Graph& _g, const _Value& _v)
908 UEdgeMap& operator=(const UEdgeMap& cmap) {
909 return operator=<UEdgeMap>(cmap);
912 template <typename CMap>
913 UEdgeMap& operator=(const CMap& cmap) {
914 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
915 const typename Parent::Graph* graph = Parent::getGraph();
917 for (graph->first(it); it != INVALID; graph->next(it)) {
918 Parent::set(it, cmap[it]);
924 // Alteration extension
927 Node node = Parent::addNode();
928 getNotifier(Node()).add(node);
932 UEdge addEdge(const Node& from, const Node& to) {
933 UEdge uedge = Parent::addEdge(from, to);
934 getNotifier(UEdge()).add(uedge);
935 getNotifier(Edge()).add(Parent::direct(uedge, true));
936 getNotifier(Edge()).add(Parent::direct(uedge, false));
941 getNotifier(Edge()).clear();
942 getNotifier(UEdge()).clear();
943 getNotifier(Node()).clear();
947 void erase(const Node& node) {
949 Parent::firstOut(edge, node);
950 while (edge != INVALID ) {
952 Parent::firstOut(edge, node);
955 Parent::firstIn(edge, node);
956 while (edge != INVALID ) {
958 Parent::firstIn(edge, node);
961 getNotifier(Node()).erase(node);
965 void erase(const UEdge& uedge) {
966 getNotifier(Edge()).erase(Parent::direct(uedge, true));
967 getNotifier(Edge()).erase(Parent::direct(uedge, false));
968 getNotifier(UEdge()).erase(uedge);
969 Parent::erase(uedge);
973 getNotifier(Edge()).clear();
974 getNotifier(UEdge()).clear();
975 getNotifier(Node()).clear();
981 template <typename Base>
982 class BpUGraphBaseExtender : public Base {
985 typedef BpUGraphBaseExtender Graph;
987 typedef typename Parent::Node Node;
988 typedef typename Parent::Edge UEdge;
996 class ANode : public Node {
997 friend class BpUGraphBaseExtender;
1000 ANode(const Node& node) : Node(node) {
1001 LEMON_ASSERT(Parent::aNode(node) || node == INVALID,
1002 typename Parent::NodeSetError());
1004 ANode(Invalid) : Node(INVALID) {}
1007 void first(ANode& node) const {
1008 Parent::firstANode(static_cast<Node&>(node));
1010 void next(ANode& node) const {
1011 Parent::nextANode(static_cast<Node&>(node));
1014 int id(const ANode& node) const {
1015 return Parent::aNodeId(node);
1018 class BNode : public Node {
1019 friend class BpUGraphBaseExtender;
1022 BNode(const Node& node) : Node(node) {
1023 LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
1024 typename Parent::NodeSetError());
1026 BNode(Invalid) : Node(INVALID) {}
1029 void first(BNode& node) const {
1030 Parent::firstBNode(static_cast<Node&>(node));
1032 void next(BNode& node) const {
1033 Parent::nextBNode(static_cast<Node&>(node));
1036 int id(const BNode& node) const {
1037 return Parent::aNodeId(node);
1040 Node source(const UEdge& edge) const {
1043 Node target(const UEdge& edge) const {
1047 void firstInc(UEdge& edge, bool& direction, const Node& node) const {
1048 if (Parent::aNode(node)) {
1049 Parent::firstOut(edge, node);
1052 Parent::firstIn(edge, node);
1053 direction = static_cast<UEdge&>(edge) == INVALID;
1056 void nextInc(UEdge& edge, bool& direction) const {
1058 Parent::nextOut(edge);
1060 Parent::nextIn(edge);
1061 if (edge == INVALID) direction = true;
1065 int maxUEdgeId() const {
1066 return Parent::maxEdgeId();
1069 UEdge uEdgeFromId(int id) const {
1070 return Parent::edgeFromId(id);
1073 class Edge : public UEdge {
1074 friend class BpUGraphBaseExtender;
1078 Edge(const UEdge& edge, bool _forward)
1079 : UEdge(edge), forward(_forward) {}
1083 Edge (Invalid) : UEdge(INVALID), forward(true) {}
1084 bool operator==(const Edge& i) const {
1085 return UEdge::operator==(i) && forward == i.forward;
1087 bool operator!=(const Edge& i) const {
1088 return UEdge::operator!=(i) || forward != i.forward;
1090 bool operator<(const Edge& i) const {
1091 return UEdge::operator<(i) ||
1092 (!(i.forward<forward) && UEdge(*this)<UEdge(i));
1096 void first(Edge& edge) const {
1097 Parent::first(static_cast<UEdge&>(edge));
1098 edge.forward = true;
1101 void next(Edge& edge) const {
1102 if (!edge.forward) {
1103 Parent::next(static_cast<UEdge&>(edge));
1105 edge.forward = !edge.forward;
1108 void firstOut(Edge& edge, const Node& node) const {
1109 if (Parent::aNode(node)) {
1110 Parent::firstOut(edge, node);
1111 edge.forward = true;
1113 Parent::firstIn(edge, node);
1114 edge.forward = static_cast<UEdge&>(edge) == INVALID;
1117 void nextOut(Edge& edge) const {
1119 Parent::nextOut(edge);
1121 Parent::nextIn(edge);
1122 edge.forward = static_cast<UEdge&>(edge) == INVALID;
1126 void firstIn(Edge& edge, const Node& node) const {
1127 if (Parent::bNode(node)) {
1128 Parent::firstIn(edge, node);
1129 edge.forward = true;
1131 Parent::firstOut(edge, node);
1132 edge.forward = static_cast<UEdge&>(edge) == INVALID;
1135 void nextIn(Edge& edge) const {
1137 Parent::nextIn(edge);
1139 Parent::nextOut(edge);
1140 edge.forward = static_cast<UEdge&>(edge) == INVALID;
1144 Node source(const Edge& edge) const {
1145 return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
1147 Node target(const Edge& edge) const {
1148 return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
1151 int id(const Edge& edge) const {
1152 return (Parent::id(edge) << 1) + (edge.forward ? 0 : 1);
1154 Edge edgeFromId(int id) const {
1155 return Edge(Parent::fromId(id >> 1, UEdge()), (id & 1) == 0);
1157 int maxEdgeId() const {
1158 return (Parent::maxId(UEdge()) << 1) + 1;
1161 bool direction(const Edge& edge) const {
1162 return edge.forward;
1165 Edge direct(const UEdge& edge, bool direction) const {
1166 return Edge(edge, direction);
1170 template <typename Base>
1171 class BpUGraphExtender : public Base {
1173 typedef Base Parent;
1174 typedef BpUGraphExtender Graph;
1176 typedef typename Parent::Node Node;
1177 typedef typename Parent::BNode BNode;
1178 typedef typename Parent::ANode ANode;
1179 typedef typename Parent::Edge Edge;
1180 typedef typename Parent::UEdge UEdge;
1182 Node oppositeNode(const UEdge& edge, const Node& node) const {
1183 return source(edge) == node ?
1184 target(edge) : source(edge);
1187 using Parent::direct;
1188 Edge direct(const UEdge& edge, const Node& node) const {
1189 return Edge(edge, node == Parent::source(edge));
1192 Edge oppositeEdge(const Edge& edge) const {
1193 return Parent::direct(edge, !Parent::direction(edge));
1197 int maxId(Node) const {
1198 return Parent::maxNodeId();
1200 int maxId(BNode) const {
1201 return Parent::maxBNodeId();
1203 int maxId(ANode) const {
1204 return Parent::maxANodeId();
1206 int maxId(Edge) const {
1207 return Parent::maxEdgeId();
1209 int maxId(UEdge) const {
1210 return Parent::maxUEdgeId();
1214 Node fromId(int id, Node) const {
1215 return Parent::nodeFromId(id);
1217 ANode fromId(int id, ANode) const {
1218 return Parent::fromANodeId(id);
1220 BNode fromId(int id, BNode) const {
1221 return Parent::fromBNodeId(id);
1223 Edge fromId(int id, Edge) const {
1224 return Parent::edgeFromId(id);
1226 UEdge fromId(int id, UEdge) const {
1227 return Parent::uEdgeFromId(id);
1230 typedef AlterationNotifier<Node> NodeNotifier;
1231 typedef AlterationNotifier<BNode> BNodeNotifier;
1232 typedef AlterationNotifier<ANode> ANodeNotifier;
1233 typedef AlterationNotifier<Edge> EdgeNotifier;
1234 typedef AlterationNotifier<UEdge> UEdgeNotifier;
1238 mutable NodeNotifier nodeNotifier;
1239 mutable BNodeNotifier bNodeNotifier;
1240 mutable ANodeNotifier aNodeNotifier;
1241 mutable EdgeNotifier edgeNotifier;
1242 mutable UEdgeNotifier uEdgeNotifier;
1246 NodeNotifier& getNotifier(Node) const {
1247 return nodeNotifier;
1250 BNodeNotifier& getNotifier(BNode) const {
1251 return bNodeNotifier;
1254 ANodeNotifier& getNotifier(ANode) const {
1255 return aNodeNotifier;
1258 EdgeNotifier& getNotifier(Edge) const {
1259 return edgeNotifier;
1262 UEdgeNotifier& getNotifier(UEdge) const {
1263 return uEdgeNotifier;
1266 ~BpUGraphExtender() {
1267 getNotifier(UEdge()).clear();
1268 getNotifier(Edge()).clear();
1269 getNotifier(ANode()).clear();
1270 getNotifier(BNode()).clear();
1271 getNotifier(Node()).clear();
1275 class NodeIt : public Node {
1281 NodeIt(Invalid i) : Node(INVALID) { }
1283 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
1284 graph->first(static_cast<Node&>(*this));
1287 NodeIt(const Graph& _graph, const Node& node)
1288 : Node(node), graph(&_graph) { }
1290 NodeIt& operator++() {
1297 class ANodeIt : public Node {
1298 friend class BpUGraphExtender;
1304 ANodeIt(Invalid i) : Node(INVALID) { }
1306 explicit ANodeIt(const Graph& _graph) : graph(&_graph) {
1307 graph->firstANode(static_cast<Node&>(*this));
1310 ANodeIt(const Graph& _graph, const Node& node)
1311 : Node(node), graph(&_graph) {}
1313 ANodeIt& operator++() {
1314 graph->nextANode(*this);
1319 class BNodeIt : public Node {
1320 friend class BpUGraphExtender;
1326 BNodeIt(Invalid i) : Node(INVALID) { }
1328 explicit BNodeIt(const Graph& _graph) : graph(&_graph) {
1329 graph->firstBNode(static_cast<Node&>(*this));
1332 BNodeIt(const Graph& _graph, const Node& node)
1333 : Node(node), graph(&_graph) {}
1335 BNodeIt& operator++() {
1336 graph->nextBNode(*this);
1341 class EdgeIt : public Edge {
1342 friend class BpUGraphExtender;
1348 EdgeIt(Invalid i) : Edge(INVALID) { }
1350 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
1351 graph->first(static_cast<Edge&>(*this));
1354 EdgeIt(const Graph& _graph, const Edge& edge)
1355 : Edge(edge), graph(&_graph) { }
1357 EdgeIt& operator++() {
1364 class UEdgeIt : public UEdge {
1365 friend class BpUGraphExtender;
1371 UEdgeIt(Invalid i) : UEdge(INVALID) { }
1373 explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
1374 graph->first(static_cast<UEdge&>(*this));
1377 UEdgeIt(const Graph& _graph, const UEdge& edge)
1378 : UEdge(edge), graph(&_graph) { }
1380 UEdgeIt& operator++() {
1386 class OutEdgeIt : public Edge {
1387 friend class BpUGraphExtender;
1393 OutEdgeIt(Invalid i) : Edge(i) { }
1395 OutEdgeIt(const Graph& _graph, const Node& node)
1397 graph->firstOut(*this, node);
1400 OutEdgeIt(const Graph& _graph, const Edge& edge)
1401 : Edge(edge), graph(&_graph) {}
1403 OutEdgeIt& operator++() {
1404 graph->nextOut(*this);
1411 class InEdgeIt : public Edge {
1412 friend class BpUGraphExtender;
1418 InEdgeIt(Invalid i) : Edge(i) { }
1420 InEdgeIt(const Graph& _graph, const Node& node)
1422 graph->firstIn(*this, node);
1425 InEdgeIt(const Graph& _graph, const Edge& edge) :
1426 Edge(edge), graph(&_graph) {}
1428 InEdgeIt& operator++() {
1429 graph->nextIn(*this);
1435 /// \brief Base node of the iterator
1437 /// Returns the base node (ie. the source in this case) of the iterator
1438 Node baseNode(const OutEdgeIt &e) const {
1439 return Parent::source((Edge&)e);
1441 /// \brief Running node of the iterator
1443 /// Returns the running node (ie. the target in this case) of the
1445 Node runningNode(const OutEdgeIt &e) const {
1446 return Parent::target((Edge&)e);
1449 /// \brief Base node of the iterator
1451 /// Returns the base node (ie. the target in this case) of the iterator
1452 Node baseNode(const InEdgeIt &e) const {
1453 return Parent::target((Edge&)e);
1455 /// \brief Running node of the iterator
1457 /// Returns the running node (ie. the source in this case) of the
1459 Node runningNode(const InEdgeIt &e) const {
1460 return Parent::source((Edge&)e);
1463 class IncEdgeIt : public Parent::UEdge {
1464 friend class BpUGraphExtender;
1471 IncEdgeIt(Invalid i) : UEdge(i), direction(true) { }
1473 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
1474 graph->firstInc(*this, direction, n);
1477 IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
1478 : graph(&_graph), UEdge(ue) {
1479 direction = (graph->source(ue) == n);
1482 IncEdgeIt& operator++() {
1483 graph->nextInc(*this, direction);
1489 /// Base node of the iterator
1491 /// Returns the base node of the iterator
1492 Node baseNode(const IncEdgeIt &e) const {
1493 return e.direction ? source(e) : target(e);
1496 /// Running node of the iterator
1498 /// Returns the running node of the iterator
1499 Node runningNode(const IncEdgeIt &e) const {
1500 return e.direction ? target(e) : source(e);
1503 template <typename _Value>
1505 : public IterableMapExtender<DefaultMap<Graph, ANode, _Value> > {
1507 typedef BpUGraphExtender Graph;
1508 typedef IterableMapExtender<DefaultMap<Graph, ANode, _Value> >
1511 ANodeMap(const Graph& _g)
1513 ANodeMap(const Graph& _g, const _Value& _v)
1516 ANodeMap& operator=(const ANodeMap& cmap) {
1517 return operator=<ANodeMap>(cmap);
1521 /// \brief Template assign operator.
1523 /// The given parameter should be conform to the ReadMap
1524 /// concept and could be indiced by the current item set of
1525 /// the ANodeMap. In this case the value for each item
1526 /// is assigned by the value of the given ReadMap.
1527 template <typename CMap>
1528 ANodeMap& operator=(const CMap& cmap) {
1529 checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
1530 const typename Parent::Graph* graph = Parent::getGraph();
1532 for (graph->first(it); it != INVALID; graph->next(it)) {
1533 Parent::set(it, cmap[it]);
1540 template <typename _Value>
1542 : public IterableMapExtender<DefaultMap<Graph, BNode, _Value> > {
1544 typedef BpUGraphExtender Graph;
1545 typedef IterableMapExtender<DefaultMap<Graph, BNode, _Value> >
1548 BNodeMap(const Graph& _g)
1550 BNodeMap(const Graph& _g, const _Value& _v)
1553 BNodeMap& operator=(const BNodeMap& cmap) {
1554 return operator=<BNodeMap>(cmap);
1558 /// \brief Template assign operator.
1560 /// The given parameter should be conform to the ReadMap
1561 /// concept and could be indiced by the current item set of
1562 /// the BNodeMap. In this case the value for each item
1563 /// is assigned by the value of the given ReadMap.
1564 template <typename CMap>
1565 BNodeMap& operator=(const CMap& cmap) {
1566 checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
1567 const typename Parent::Graph* graph = Parent::getGraph();
1569 for (graph->first(it); it != INVALID; graph->next(it)) {
1570 Parent::set(it, cmap[it]);
1579 template <typename _Value>
1580 class NodeMapBase : public NodeNotifier::ObserverBase {
1582 typedef BpUGraphExtender Graph;
1585 typedef _Value Value;
1587 /// The reference type of the map;
1588 typedef typename BNodeMap<_Value>::Reference Reference;
1589 /// The pointer type of the map;
1590 typedef typename BNodeMap<_Value>::Pointer Pointer;
1592 /// The const value type of the map.
1593 typedef const Value ConstValue;
1594 /// The const reference type of the map;
1595 typedef typename BNodeMap<_Value>::ConstReference ConstReference;
1596 /// The pointer type of the map;
1597 typedef typename BNodeMap<_Value>::ConstPointer ConstPointer;
1599 typedef True ReferenceMapTag;
1601 NodeMapBase(const Graph& _g)
1602 : graph(&_g), bNodeMap(_g), aNodeMap(_g) {
1603 NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
1605 NodeMapBase(const Graph& _g, const _Value& _v)
1606 : graph(&_g), bNodeMap(_g, _v),
1608 NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
1611 virtual ~NodeMapBase() {
1612 if (NodeNotifier::ObserverBase::attached()) {
1613 NodeNotifier::ObserverBase::detach();
1617 ConstReference operator[](const Key& node) const {
1618 if (Parent::aNode(node)) {
1619 return aNodeMap[node];
1621 return bNodeMap[node];
1625 Reference operator[](const Key& node) {
1626 if (Parent::aNode(node)) {
1627 return aNodeMap[node];
1629 return bNodeMap[node];
1633 void set(const Key& node, const Value& value) {
1634 if (Parent::aNode(node)) {
1635 aNodeMap.set(node, value);
1637 bNodeMap.set(node, value);
1643 virtual void add(const Node&) {}
1644 virtual void add(const std::vector<Node>&) {}
1645 virtual void erase(const Node&) {}
1646 virtual void erase(const std::vector<Node>&) {}
1647 virtual void clear() {}
1648 virtual void build() {}
1650 const Graph* getGraph() const { return graph; }
1654 BNodeMap<_Value> bNodeMap;
1655 ANodeMap<_Value> aNodeMap;
1660 template <typename _Value>
1662 : public IterableMapExtender<NodeMapBase<_Value> > {
1664 typedef BpUGraphExtender Graph;
1665 typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
1667 NodeMap(const Graph& _g)
1669 NodeMap(const Graph& _g, const _Value& _v)
1672 NodeMap& operator=(const NodeMap& cmap) {
1673 return operator=<NodeMap>(cmap);
1677 /// \brief Template assign operator.
1679 /// The given parameter should be conform to the ReadMap
1680 /// concept and could be indiced by the current item set of
1681 /// the NodeMap. In this case the value for each item
1682 /// is assigned by the value of the given ReadMap.
1683 template <typename CMap>
1684 NodeMap& operator=(const CMap& cmap) {
1685 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
1686 const typename Parent::Graph* graph = Parent::getGraph();
1688 for (graph->first(it); it != INVALID; graph->next(it)) {
1689 Parent::set(it, cmap[it]);
1698 template <typename _Value>
1700 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
1702 typedef BpUGraphExtender Graph;
1703 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
1705 EdgeMap(const Graph& _g)
1707 EdgeMap(const Graph& _g, const _Value& _v)
1710 EdgeMap& operator=(const EdgeMap& cmap) {
1711 return operator=<EdgeMap>(cmap);
1714 template <typename CMap>
1715 EdgeMap& operator=(const CMap& cmap) {
1716 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
1717 const typename Parent::Graph* graph = Parent::getGraph();
1719 for (graph->first(it); it != INVALID; graph->next(it)) {
1720 Parent::set(it, cmap[it]);
1726 template <typename _Value>
1728 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
1730 typedef BpUGraphExtender Graph;
1731 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> >
1734 UEdgeMap(const Graph& _g)
1736 UEdgeMap(const Graph& _g, const _Value& _v)
1739 UEdgeMap& operator=(const UEdgeMap& cmap) {
1740 return operator=<UEdgeMap>(cmap);
1743 template <typename CMap>
1744 UEdgeMap& operator=(const CMap& cmap) {
1745 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
1746 const typename Parent::Graph* graph = Parent::getGraph();
1748 for (graph->first(it); it != INVALID; graph->next(it)) {
1749 Parent::set(it, cmap[it]);
1757 Node node = Parent::addANode();
1758 getNotifier(ANode()).add(node);
1759 getNotifier(Node()).add(node);
1764 Node node = Parent::addBNode();
1765 getNotifier(BNode()).add(node);
1766 getNotifier(Node()).add(node);
1770 UEdge addEdge(const Node& source, const Node& target) {
1771 UEdge uedge = Parent::addEdge(source, target);
1772 getNotifier(UEdge()).add(uedge);
1774 std::vector<Edge> edges;
1775 edges.push_back(direct(uedge, true));
1776 edges.push_back(direct(uedge, false));
1777 getNotifier(Edge()).add(edges);
1783 getNotifier(Edge()).clear();
1784 getNotifier(UEdge()).clear();
1785 getNotifier(Node()).clear();
1786 getNotifier(BNode()).clear();
1787 getNotifier(ANode()).clear();
1791 void erase(const Node& node) {
1794 Parent::firstInc(uedge, dir, node);
1795 while (uedge != INVALID ) {
1797 Parent::firstInc(uedge, dir, node);
1800 getNotifier(Node()).erase(node);
1801 Parent::erase(node);
1804 void erase(const UEdge& uedge) {
1805 std::vector<Edge> edges;
1806 edges.push_back(direct(uedge, true));
1807 edges.push_back(direct(uedge, false));
1808 getNotifier(Edge()).erase(edges);
1809 getNotifier(UEdge()).erase(uedge);
1810 Parent::erase(uedge);
1814 ~BpUGraphExtender() {
1815 getNotifier(Edge()).clear();
1816 getNotifier(UEdge()).clear();
1817 getNotifier(Node()).clear();
1818 getNotifier(BNode()).clear();
1819 getNotifier(ANode()).clear();
1827 #endif // LEMON_UNDIR_GRAPH_EXTENDER_H