1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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/core.h>
24 #include <lemon/bits/map_extender.h>
25 #include <lemon/bits/default_map.h>
27 #include <lemon/concept_check.h>
28 #include <lemon/concepts/maps.h>
30 #include <lemon/bits/stl_iterators.h>
34 //\brief Extenders for the graph types
39 // \brief Extender for the digraph implementations
40 template <typename Base>
41 class DigraphExtender : public Base {
46 typedef DigraphExtender Digraph;
50 typedef typename Parent::Node Node;
51 typedef typename Parent::Arc Arc;
53 int maxId(Node) const {
54 return Parent::maxNodeId();
57 int maxId(Arc) const {
58 return Parent::maxArcId();
61 static Node fromId(int id, Node) {
62 return Parent::nodeFromId(id);
65 static Arc fromId(int id, Arc) {
66 return Parent::arcFromId(id);
69 Node oppositeNode(const Node &node, const Arc &arc) const {
70 if (node == Parent::source(arc))
71 return Parent::target(arc);
72 else if(node == Parent::target(arc))
73 return Parent::source(arc);
78 // Alterable extension
80 typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
81 typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
86 mutable NodeNotifier node_notifier;
87 mutable ArcNotifier arc_notifier;
91 NodeNotifier& notifier(Node) const {
95 ArcNotifier& notifier(Arc) const {
99 class NodeIt : public Node {
100 const Digraph* _digraph;
105 NodeIt(Invalid i) : Node(i) { }
107 explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
108 _digraph->first(static_cast<Node&>(*this));
111 NodeIt(const Digraph& digraph, const Node& node)
112 : Node(node), _digraph(&digraph) {}
114 NodeIt& operator++() {
115 _digraph->next(*this);
121 LemonRangeWrapper1<NodeIt, Digraph> nodes() const {
122 return LemonRangeWrapper1<NodeIt, Digraph>(*this);
126 class ArcIt : public Arc {
127 const Digraph* _digraph;
132 ArcIt(Invalid i) : Arc(i) { }
134 explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
135 _digraph->first(static_cast<Arc&>(*this));
138 ArcIt(const Digraph& digraph, const Arc& arc) :
139 Arc(arc), _digraph(&digraph) { }
141 ArcIt& operator++() {
142 _digraph->next(*this);
148 LemonRangeWrapper1<ArcIt, Digraph> arcs() const {
149 return LemonRangeWrapper1<ArcIt, Digraph>(*this);
153 class OutArcIt : public Arc {
154 const Digraph* _digraph;
159 OutArcIt(Invalid i) : Arc(i) { }
161 OutArcIt(const Digraph& digraph, const Node& node)
162 : _digraph(&digraph) {
163 _digraph->firstOut(*this, node);
166 OutArcIt(const Digraph& digraph, const Arc& arc)
167 : Arc(arc), _digraph(&digraph) {}
169 OutArcIt& operator++() {
170 _digraph->nextOut(*this);
176 LemonRangeWrapper2<OutArcIt, Digraph, Node> outArcs(const Node& u) const {
177 return LemonRangeWrapper2<OutArcIt, Digraph, Node>(*this, u);
181 class InArcIt : public Arc {
182 const Digraph* _digraph;
187 InArcIt(Invalid i) : Arc(i) { }
189 InArcIt(const Digraph& digraph, const Node& node)
190 : _digraph(&digraph) {
191 _digraph->firstIn(*this, node);
194 InArcIt(const Digraph& digraph, const Arc& arc) :
195 Arc(arc), _digraph(&digraph) {}
197 InArcIt& operator++() {
198 _digraph->nextIn(*this);
204 LemonRangeWrapper2<InArcIt, Digraph, Node> inArcs(const Node& u) const {
205 return LemonRangeWrapper2<InArcIt, Digraph, Node>(*this, u);
208 // \brief Base node of the iterator
210 // Returns the base node (i.e. the source in this case) of the iterator
211 Node baseNode(const OutArcIt &arc) const {
212 return Parent::source(arc);
214 // \brief Running node of the iterator
216 // Returns the running node (i.e. the target in this case) of the
218 Node runningNode(const OutArcIt &arc) const {
219 return Parent::target(arc);
222 // \brief Base node of the iterator
224 // Returns the base node (i.e. the target in this case) of the iterator
225 Node baseNode(const InArcIt &arc) const {
226 return Parent::target(arc);
228 // \brief Running node of the iterator
230 // Returns the running node (i.e. the source in this case) of the
232 Node runningNode(const InArcIt &arc) const {
233 return Parent::source(arc);
237 template <typename _Value>
239 : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
240 typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
243 explicit NodeMap(const Digraph& digraph)
245 NodeMap(const Digraph& digraph, const _Value& value)
246 : Parent(digraph, value) {}
249 NodeMap& operator=(const NodeMap& cmap) {
250 return operator=<NodeMap>(cmap);
253 template <typename CMap>
254 NodeMap& operator=(const CMap& cmap) {
255 Parent::operator=(cmap);
261 template <typename _Value>
263 : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
264 typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
267 explicit ArcMap(const Digraph& digraph)
269 ArcMap(const Digraph& digraph, const _Value& value)
270 : Parent(digraph, value) {}
273 ArcMap& operator=(const ArcMap& cmap) {
274 return operator=<ArcMap>(cmap);
277 template <typename CMap>
278 ArcMap& operator=(const CMap& cmap) {
279 Parent::operator=(cmap);
286 Node node = Parent::addNode();
287 notifier(Node()).add(node);
291 Arc addArc(const Node& from, const Node& to) {
292 Arc arc = Parent::addArc(from, to);
293 notifier(Arc()).add(arc);
298 notifier(Arc()).clear();
299 notifier(Node()).clear();
303 template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
304 void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
305 Parent::build(digraph, nodeRef, arcRef);
306 notifier(Node()).build();
307 notifier(Arc()).build();
310 void erase(const Node& node) {
312 Parent::firstOut(arc, node);
313 while (arc != INVALID ) {
315 Parent::firstOut(arc, node);
318 Parent::firstIn(arc, node);
319 while (arc != INVALID ) {
321 Parent::firstIn(arc, node);
324 notifier(Node()).erase(node);
328 void erase(const Arc& arc) {
329 notifier(Arc()).erase(arc);
334 node_notifier.setContainer(*this);
335 arc_notifier.setContainer(*this);
340 arc_notifier.clear();
341 node_notifier.clear();
345 // \ingroup _graphbits
347 // \brief Extender for the Graphs
348 template <typename Base>
349 class GraphExtender : public Base {
354 typedef GraphExtender Graph;
356 typedef True UndirectedTag;
358 typedef typename Parent::Node Node;
359 typedef typename Parent::Arc Arc;
360 typedef typename Parent::Edge Edge;
364 int maxId(Node) const {
365 return Parent::maxNodeId();
368 int maxId(Arc) const {
369 return Parent::maxArcId();
372 int maxId(Edge) const {
373 return Parent::maxEdgeId();
376 static Node fromId(int id, Node) {
377 return Parent::nodeFromId(id);
380 static Arc fromId(int id, Arc) {
381 return Parent::arcFromId(id);
384 static Edge fromId(int id, Edge) {
385 return Parent::edgeFromId(id);
388 Node oppositeNode(const Node &n, const Edge &e) const {
389 if( n == Parent::u(e))
391 else if( n == Parent::v(e))
397 Arc oppositeArc(const Arc &arc) const {
398 return Parent::direct(arc, !Parent::direction(arc));
401 using Parent::direct;
402 Arc direct(const Edge &edge, const Node &node) const {
403 return Parent::direct(edge, Parent::u(edge) == node);
406 // Alterable extension
408 typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
409 typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
410 typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
415 mutable NodeNotifier node_notifier;
416 mutable ArcNotifier arc_notifier;
417 mutable EdgeNotifier edge_notifier;
421 NodeNotifier& notifier(Node) const {
422 return node_notifier;
425 ArcNotifier& notifier(Arc) const {
429 EdgeNotifier& notifier(Edge) const {
430 return edge_notifier;
435 class NodeIt : public Node {
441 NodeIt(Invalid i) : Node(i) { }
443 explicit NodeIt(const Graph& graph) : _graph(&graph) {
444 _graph->first(static_cast<Node&>(*this));
447 NodeIt(const Graph& graph, const Node& node)
448 : Node(node), _graph(&graph) {}
450 NodeIt& operator++() {
457 LemonRangeWrapper1<NodeIt, Graph> nodes() const {
458 return LemonRangeWrapper1<NodeIt, Graph>(*this);
462 class ArcIt : public Arc {
468 ArcIt(Invalid i) : Arc(i) { }
470 explicit ArcIt(const Graph& graph) : _graph(&graph) {
471 _graph->first(static_cast<Arc&>(*this));
474 ArcIt(const Graph& graph, const Arc& arc) :
475 Arc(arc), _graph(&graph) { }
477 ArcIt& operator++() {
484 LemonRangeWrapper1<ArcIt, Graph> arcs() const {
485 return LemonRangeWrapper1<ArcIt, Graph>(*this);
489 class OutArcIt : public Arc {
495 OutArcIt(Invalid i) : Arc(i) { }
497 OutArcIt(const Graph& graph, const Node& node)
499 _graph->firstOut(*this, node);
502 OutArcIt(const Graph& graph, const Arc& arc)
503 : Arc(arc), _graph(&graph) {}
505 OutArcIt& operator++() {
506 _graph->nextOut(*this);
512 LemonRangeWrapper2<OutArcIt, Graph, Node> outArcs(const Node& u) const {
513 return LemonRangeWrapper2<OutArcIt, Graph, Node>(*this, u);
517 class InArcIt : public Arc {
523 InArcIt(Invalid i) : Arc(i) { }
525 InArcIt(const Graph& graph, const Node& node)
527 _graph->firstIn(*this, node);
530 InArcIt(const Graph& graph, const Arc& arc) :
531 Arc(arc), _graph(&graph) {}
533 InArcIt& operator++() {
534 _graph->nextIn(*this);
540 LemonRangeWrapper2<InArcIt, Graph, Node> inArcs(const Node& u) const {
541 return LemonRangeWrapper2<InArcIt, Graph, Node>(*this, u);
545 class EdgeIt : public Parent::Edge {
551 EdgeIt(Invalid i) : Edge(i) { }
553 explicit EdgeIt(const Graph& graph) : _graph(&graph) {
554 _graph->first(static_cast<Edge&>(*this));
557 EdgeIt(const Graph& graph, const Edge& edge) :
558 Edge(edge), _graph(&graph) { }
560 EdgeIt& operator++() {
567 LemonRangeWrapper1<EdgeIt, Graph> edges() const {
568 return LemonRangeWrapper1<EdgeIt, Graph>(*this);
572 class IncEdgeIt : public Parent::Edge {
573 friend class GraphExtender;
580 IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
582 IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
583 _graph->firstInc(*this, _direction, node);
586 IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
587 : _graph(&graph), Edge(edge) {
588 _direction = (_graph->source(edge) == node);
591 IncEdgeIt& operator++() {
592 _graph->nextInc(*this, _direction);
597 LemonRangeWrapper2<IncEdgeIt, Graph, Node> incEdges(const Node& u) const {
598 return LemonRangeWrapper2<IncEdgeIt, Graph, Node>(*this, u);
602 // \brief Base node of the iterator
604 // Returns the base node (ie. the source in this case) of the iterator
605 Node baseNode(const OutArcIt &arc) const {
606 return Parent::source(static_cast<const Arc&>(arc));
608 // \brief Running node of the iterator
610 // Returns the running node (ie. the target in this case) of the
612 Node runningNode(const OutArcIt &arc) const {
613 return Parent::target(static_cast<const Arc&>(arc));
616 // \brief Base node of the iterator
618 // Returns the base node (ie. the target in this case) of the iterator
619 Node baseNode(const InArcIt &arc) const {
620 return Parent::target(static_cast<const Arc&>(arc));
622 // \brief Running node of the iterator
624 // Returns the running node (ie. the source in this case) of the
626 Node runningNode(const InArcIt &arc) const {
627 return Parent::source(static_cast<const Arc&>(arc));
630 // Base node of the iterator
632 // Returns the base node of the iterator
633 Node baseNode(const IncEdgeIt &edge) const {
634 return edge._direction ? this->u(edge) : this->v(edge);
636 // Running node of the iterator
638 // Returns the running node of the iterator
639 Node runningNode(const IncEdgeIt &edge) const {
640 return edge._direction ? this->v(edge) : this->u(edge);
643 // Mappable extension
645 template <typename _Value>
647 : public MapExtender<DefaultMap<Graph, Node, _Value> > {
648 typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
651 explicit NodeMap(const Graph& graph)
653 NodeMap(const Graph& graph, const _Value& value)
654 : Parent(graph, value) {}
657 NodeMap& operator=(const NodeMap& cmap) {
658 return operator=<NodeMap>(cmap);
661 template <typename CMap>
662 NodeMap& operator=(const CMap& cmap) {
663 Parent::operator=(cmap);
669 template <typename _Value>
671 : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
672 typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
675 explicit ArcMap(const Graph& graph)
677 ArcMap(const Graph& graph, const _Value& value)
678 : Parent(graph, value) {}
681 ArcMap& operator=(const ArcMap& cmap) {
682 return operator=<ArcMap>(cmap);
685 template <typename CMap>
686 ArcMap& operator=(const CMap& cmap) {
687 Parent::operator=(cmap);
693 template <typename _Value>
695 : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
696 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
699 explicit EdgeMap(const Graph& graph)
702 EdgeMap(const Graph& graph, const _Value& value)
703 : Parent(graph, value) {}
706 EdgeMap& operator=(const EdgeMap& cmap) {
707 return operator=<EdgeMap>(cmap);
710 template <typename CMap>
711 EdgeMap& operator=(const CMap& cmap) {
712 Parent::operator=(cmap);
718 // Alteration extension
721 Node node = Parent::addNode();
722 notifier(Node()).add(node);
726 Edge addEdge(const Node& from, const Node& to) {
727 Edge edge = Parent::addEdge(from, to);
728 notifier(Edge()).add(edge);
730 ev.push_back(Parent::direct(edge, true));
731 ev.push_back(Parent::direct(edge, false));
732 notifier(Arc()).add(ev);
737 notifier(Arc()).clear();
738 notifier(Edge()).clear();
739 notifier(Node()).clear();
743 template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
744 void build(const Graph& graph, NodeRefMap& nodeRef,
745 EdgeRefMap& edgeRef) {
746 Parent::build(graph, nodeRef, edgeRef);
747 notifier(Node()).build();
748 notifier(Edge()).build();
749 notifier(Arc()).build();
752 void erase(const Node& node) {
754 Parent::firstOut(arc, node);
755 while (arc != INVALID ) {
757 Parent::firstOut(arc, node);
760 Parent::firstIn(arc, node);
761 while (arc != INVALID ) {
763 Parent::firstIn(arc, node);
766 notifier(Node()).erase(node);
770 void erase(const Edge& edge) {
772 av.push_back(Parent::direct(edge, true));
773 av.push_back(Parent::direct(edge, false));
774 notifier(Arc()).erase(av);
775 notifier(Edge()).erase(edge);
780 node_notifier.setContainer(*this);
781 arc_notifier.setContainer(*this);
782 edge_notifier.setContainer(*this);
786 edge_notifier.clear();
787 arc_notifier.clear();
788 node_notifier.clear();
793 // \ingroup _graphbits
795 // \brief Extender for the BpGraphs
796 template <typename Base>
797 class BpGraphExtender : public Base {
802 typedef BpGraphExtender BpGraph;
804 typedef True UndirectedTag;
806 typedef typename Parent::Node Node;
807 typedef typename Parent::RedNode RedNode;
808 typedef typename Parent::BlueNode BlueNode;
809 typedef typename Parent::Arc Arc;
810 typedef typename Parent::Edge Edge;
818 int maxId(Node) const {
819 return Parent::maxNodeId();
822 int maxId(RedNode) const {
823 return Parent::maxRedId();
826 int maxId(BlueNode) const {
827 return Parent::maxBlueId();
830 int maxId(Arc) const {
831 return Parent::maxArcId();
834 int maxId(Edge) const {
835 return Parent::maxEdgeId();
838 static Node fromId(int id, Node) {
839 return Parent::nodeFromId(id);
842 static Arc fromId(int id, Arc) {
843 return Parent::arcFromId(id);
846 static Edge fromId(int id, Edge) {
847 return Parent::edgeFromId(id);
850 Node u(Edge e) const { return this->redNode(e); }
851 Node v(Edge e) const { return this->blueNode(e); }
853 Node oppositeNode(const Node &n, const Edge &e) const {
862 Arc oppositeArc(const Arc &arc) const {
863 return Parent::direct(arc, !Parent::direction(arc));
866 using Parent::direct;
867 Arc direct(const Edge &edge, const Node &node) const {
868 return Parent::direct(edge, Parent::redNode(edge) == node);
871 RedNode asRedNode(const Node& node) const {
872 if (node == INVALID || Parent::blue(node)) {
875 return Parent::asRedNodeUnsafe(node);
879 BlueNode asBlueNode(const Node& node) const {
880 if (node == INVALID || Parent::red(node)) {
883 return Parent::asBlueNodeUnsafe(node);
887 // Alterable extension
889 typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier;
890 typedef AlterationNotifier<BpGraphExtender, RedNode> RedNodeNotifier;
891 typedef AlterationNotifier<BpGraphExtender, BlueNode> BlueNodeNotifier;
892 typedef AlterationNotifier<BpGraphExtender, Arc> ArcNotifier;
893 typedef AlterationNotifier<BpGraphExtender, Edge> EdgeNotifier;
898 mutable NodeNotifier node_notifier;
899 mutable RedNodeNotifier red_node_notifier;
900 mutable BlueNodeNotifier blue_node_notifier;
901 mutable ArcNotifier arc_notifier;
902 mutable EdgeNotifier edge_notifier;
906 NodeNotifier& notifier(Node) const {
907 return node_notifier;
910 RedNodeNotifier& notifier(RedNode) const {
911 return red_node_notifier;
914 BlueNodeNotifier& notifier(BlueNode) const {
915 return blue_node_notifier;
918 ArcNotifier& notifier(Arc) const {
922 EdgeNotifier& notifier(Edge) const {
923 return edge_notifier;
928 class NodeIt : public Node {
929 const BpGraph* _graph;
934 NodeIt(Invalid i) : Node(i) { }
936 explicit NodeIt(const BpGraph& graph) : _graph(&graph) {
937 _graph->first(static_cast<Node&>(*this));
940 NodeIt(const BpGraph& graph, const Node& node)
941 : Node(node), _graph(&graph) {}
943 NodeIt& operator++() {
950 LemonRangeWrapper1<NodeIt, BpGraph> nodes() const {
951 return LemonRangeWrapper1<NodeIt, BpGraph>(*this);
955 class RedNodeIt : public RedNode {
956 const BpGraph* _graph;
961 RedNodeIt(Invalid i) : RedNode(i) { }
963 explicit RedNodeIt(const BpGraph& graph) : _graph(&graph) {
964 _graph->first(static_cast<RedNode&>(*this));
967 RedNodeIt(const BpGraph& graph, const RedNode& node)
968 : RedNode(node), _graph(&graph) {}
970 RedNodeIt& operator++() {
971 _graph->next(static_cast<RedNode&>(*this));
977 LemonRangeWrapper1<RedNodeIt, BpGraph> redNodes() const {
978 return LemonRangeWrapper1<RedNodeIt, BpGraph>(*this);
982 class BlueNodeIt : public BlueNode {
983 const BpGraph* _graph;
988 BlueNodeIt(Invalid i) : BlueNode(i) { }
990 explicit BlueNodeIt(const BpGraph& graph) : _graph(&graph) {
991 _graph->first(static_cast<BlueNode&>(*this));
994 BlueNodeIt(const BpGraph& graph, const BlueNode& node)
995 : BlueNode(node), _graph(&graph) {}
997 BlueNodeIt& operator++() {
998 _graph->next(static_cast<BlueNode&>(*this));
1004 LemonRangeWrapper1<BlueNodeIt, BpGraph> blueNodes() const {
1005 return LemonRangeWrapper1<BlueNodeIt, BpGraph>(*this);
1010 class ArcIt : public Arc {
1011 const BpGraph* _graph;
1016 ArcIt(Invalid i) : Arc(i) { }
1018 explicit ArcIt(const BpGraph& graph) : _graph(&graph) {
1019 _graph->first(static_cast<Arc&>(*this));
1022 ArcIt(const BpGraph& graph, const Arc& arc) :
1023 Arc(arc), _graph(&graph) { }
1025 ArcIt& operator++() {
1026 _graph->next(*this);
1032 LemonRangeWrapper1<ArcIt, BpGraph> arcs() const {
1033 return LemonRangeWrapper1<ArcIt, BpGraph>(*this);
1037 class OutArcIt : public Arc {
1038 const BpGraph* _graph;
1043 OutArcIt(Invalid i) : Arc(i) { }
1045 OutArcIt(const BpGraph& graph, const Node& node)
1047 _graph->firstOut(*this, node);
1050 OutArcIt(const BpGraph& graph, const Arc& arc)
1051 : Arc(arc), _graph(&graph) {}
1053 OutArcIt& operator++() {
1054 _graph->nextOut(*this);
1060 LemonRangeWrapper2<OutArcIt, BpGraph, Node> outArcs(const Node& u) const {
1061 return LemonRangeWrapper2<OutArcIt, BpGraph, Node>(*this, u);
1065 class InArcIt : public Arc {
1066 const BpGraph* _graph;
1071 InArcIt(Invalid i) : Arc(i) { }
1073 InArcIt(const BpGraph& graph, const Node& node)
1075 _graph->firstIn(*this, node);
1078 InArcIt(const BpGraph& graph, const Arc& arc) :
1079 Arc(arc), _graph(&graph) {}
1081 InArcIt& operator++() {
1082 _graph->nextIn(*this);
1088 LemonRangeWrapper2<InArcIt, BpGraph, Node> inArcs(const Node& u) const {
1089 return LemonRangeWrapper2<InArcIt, BpGraph, Node>(*this, u);
1093 class EdgeIt : public Parent::Edge {
1094 const BpGraph* _graph;
1099 EdgeIt(Invalid i) : Edge(i) { }
1101 explicit EdgeIt(const BpGraph& graph) : _graph(&graph) {
1102 _graph->first(static_cast<Edge&>(*this));
1105 EdgeIt(const BpGraph& graph, const Edge& edge) :
1106 Edge(edge), _graph(&graph) { }
1108 EdgeIt& operator++() {
1109 _graph->next(*this);
1115 LemonRangeWrapper1<EdgeIt, BpGraph> edges() const {
1116 return LemonRangeWrapper1<EdgeIt, BpGraph>(*this);
1120 class IncEdgeIt : public Parent::Edge {
1121 friend class BpGraphExtender;
1122 const BpGraph* _graph;
1128 IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
1130 IncEdgeIt(const BpGraph& graph, const Node &node) : _graph(&graph) {
1131 _graph->firstInc(*this, _direction, node);
1134 IncEdgeIt(const BpGraph& graph, const Edge &edge, const Node &node)
1135 : _graph(&graph), Edge(edge) {
1136 _direction = (_graph->source(edge) == node);
1139 IncEdgeIt& operator++() {
1140 _graph->nextInc(*this, _direction);
1145 LemonRangeWrapper2<IncEdgeIt, BpGraph, Node> incEdges(const Node& u) const {
1146 return LemonRangeWrapper2<IncEdgeIt, BpGraph, Node>(*this, u);
1150 // \brief Base node of the iterator
1152 // Returns the base node (ie. the source in this case) of the iterator
1153 Node baseNode(const OutArcIt &arc) const {
1154 return Parent::source(static_cast<const Arc&>(arc));
1156 // \brief Running node of the iterator
1158 // Returns the running node (ie. the target in this case) of the
1160 Node runningNode(const OutArcIt &arc) const {
1161 return Parent::target(static_cast<const Arc&>(arc));
1164 // \brief Base node of the iterator
1166 // Returns the base node (ie. the target in this case) of the iterator
1167 Node baseNode(const InArcIt &arc) const {
1168 return Parent::target(static_cast<const Arc&>(arc));
1170 // \brief Running node of the iterator
1172 // Returns the running node (ie. the source in this case) of the
1174 Node runningNode(const InArcIt &arc) const {
1175 return Parent::source(static_cast<const Arc&>(arc));
1178 // Base node of the iterator
1180 // Returns the base node of the iterator
1181 Node baseNode(const IncEdgeIt &edge) const {
1182 return edge._direction ? this->u(edge) : this->v(edge);
1184 // Running node of the iterator
1186 // Returns the running node of the iterator
1187 Node runningNode(const IncEdgeIt &edge) const {
1188 return edge._direction ? this->v(edge) : this->u(edge);
1191 // Mappable extension
1193 template <typename _Value>
1195 : public MapExtender<DefaultMap<BpGraph, Node, _Value> > {
1196 typedef MapExtender<DefaultMap<BpGraph, Node, _Value> > Parent;
1199 explicit NodeMap(const BpGraph& bpgraph)
1200 : Parent(bpgraph) {}
1201 NodeMap(const BpGraph& bpgraph, const _Value& value)
1202 : Parent(bpgraph, value) {}
1205 NodeMap& operator=(const NodeMap& cmap) {
1206 return operator=<NodeMap>(cmap);
1209 template <typename CMap>
1210 NodeMap& operator=(const CMap& cmap) {
1211 Parent::operator=(cmap);
1217 template <typename _Value>
1219 : public MapExtender<DefaultMap<BpGraph, RedNode, _Value> > {
1220 typedef MapExtender<DefaultMap<BpGraph, RedNode, _Value> > Parent;
1223 explicit RedNodeMap(const BpGraph& bpgraph)
1224 : Parent(bpgraph) {}
1225 RedNodeMap(const BpGraph& bpgraph, const _Value& value)
1226 : Parent(bpgraph, value) {}
1229 RedNodeMap& operator=(const RedNodeMap& cmap) {
1230 return operator=<RedNodeMap>(cmap);
1233 template <typename CMap>
1234 RedNodeMap& operator=(const CMap& cmap) {
1235 Parent::operator=(cmap);
1241 template <typename _Value>
1243 : public MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > {
1244 typedef MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > Parent;
1247 explicit BlueNodeMap(const BpGraph& bpgraph)
1248 : Parent(bpgraph) {}
1249 BlueNodeMap(const BpGraph& bpgraph, const _Value& value)
1250 : Parent(bpgraph, value) {}
1253 BlueNodeMap& operator=(const BlueNodeMap& cmap) {
1254 return operator=<BlueNodeMap>(cmap);
1257 template <typename CMap>
1258 BlueNodeMap& operator=(const CMap& cmap) {
1259 Parent::operator=(cmap);
1265 template <typename _Value>
1267 : public MapExtender<DefaultMap<BpGraph, Arc, _Value> > {
1268 typedef MapExtender<DefaultMap<BpGraph, Arc, _Value> > Parent;
1271 explicit ArcMap(const BpGraph& graph)
1273 ArcMap(const BpGraph& graph, const _Value& value)
1274 : Parent(graph, value) {}
1277 ArcMap& operator=(const ArcMap& cmap) {
1278 return operator=<ArcMap>(cmap);
1281 template <typename CMap>
1282 ArcMap& operator=(const CMap& cmap) {
1283 Parent::operator=(cmap);
1289 template <typename _Value>
1291 : public MapExtender<DefaultMap<BpGraph, Edge, _Value> > {
1292 typedef MapExtender<DefaultMap<BpGraph, Edge, _Value> > Parent;
1295 explicit EdgeMap(const BpGraph& graph)
1298 EdgeMap(const BpGraph& graph, const _Value& value)
1299 : Parent(graph, value) {}
1302 EdgeMap& operator=(const EdgeMap& cmap) {
1303 return operator=<EdgeMap>(cmap);
1306 template <typename CMap>
1307 EdgeMap& operator=(const CMap& cmap) {
1308 Parent::operator=(cmap);
1314 // Alteration extension
1316 RedNode addRedNode() {
1317 RedNode node = Parent::addRedNode();
1318 notifier(RedNode()).add(node);
1319 notifier(Node()).add(node);
1323 BlueNode addBlueNode() {
1324 BlueNode node = Parent::addBlueNode();
1325 notifier(BlueNode()).add(node);
1326 notifier(Node()).add(node);
1330 Edge addEdge(const RedNode& from, const BlueNode& to) {
1331 Edge edge = Parent::addEdge(from, to);
1332 notifier(Edge()).add(edge);
1333 std::vector<Arc> av;
1334 av.push_back(Parent::direct(edge, true));
1335 av.push_back(Parent::direct(edge, false));
1336 notifier(Arc()).add(av);
1341 notifier(Arc()).clear();
1342 notifier(Edge()).clear();
1343 notifier(Node()).clear();
1344 notifier(BlueNode()).clear();
1345 notifier(RedNode()).clear();
1349 template <typename BpGraph, typename NodeRefMap, typename EdgeRefMap>
1350 void build(const BpGraph& graph, NodeRefMap& nodeRef,
1351 EdgeRefMap& edgeRef) {
1352 Parent::build(graph, nodeRef, edgeRef);
1353 notifier(RedNode()).build();
1354 notifier(BlueNode()).build();
1355 notifier(Node()).build();
1356 notifier(Edge()).build();
1357 notifier(Arc()).build();
1360 void erase(const Node& node) {
1362 Parent::firstOut(arc, node);
1363 while (arc != INVALID ) {
1365 Parent::firstOut(arc, node);
1368 Parent::firstIn(arc, node);
1369 while (arc != INVALID ) {
1371 Parent::firstIn(arc, node);
1374 if (Parent::red(node)) {
1375 notifier(RedNode()).erase(this->asRedNodeUnsafe(node));
1377 notifier(BlueNode()).erase(this->asBlueNodeUnsafe(node));
1380 notifier(Node()).erase(node);
1381 Parent::erase(node);
1384 void erase(const Edge& edge) {
1385 std::vector<Arc> av;
1386 av.push_back(Parent::direct(edge, true));
1387 av.push_back(Parent::direct(edge, false));
1388 notifier(Arc()).erase(av);
1389 notifier(Edge()).erase(edge);
1390 Parent::erase(edge);
1394 red_node_notifier.setContainer(*this);
1395 blue_node_notifier.setContainer(*this);
1396 node_notifier.setContainer(*this);
1397 arc_notifier.setContainer(*this);
1398 edge_notifier.setContainer(*this);
1401 ~BpGraphExtender() {
1402 edge_notifier.clear();
1403 arc_notifier.clear();
1404 node_notifier.clear();
1405 blue_node_notifier.clear();
1406 red_node_notifier.clear();