00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef LEMON_GRAPH_ADAPTOR_H
00018 #define LEMON_GRAPH_ADAPTOR_H
00019
00027
00028 #include <lemon/invalid.h>
00029 #include <lemon/maps.h>
00030 #include <lemon/bits/erasable_graph_extender.h>
00031 #include <lemon/bits/clearable_graph_extender.h>
00032 #include <lemon/bits/extendable_graph_extender.h>
00033 #include <lemon/bits/iterable_graph_extender.h>
00034 #include <lemon/bits/alteration_notifier.h>
00035 #include <lemon/bits/default_map.h>
00036 #include <lemon/bits/undir_graph_extender.h>
00037 #include <iostream>
00038
00039 namespace lemon {
00040
00041
00042
00064 template<typename _Graph>
00065 class GraphAdaptorBase {
00066 public:
00067 typedef _Graph Graph;
00069 typedef Graph BaseGraph;
00070 typedef Graph ParentGraph;
00071
00072 protected:
00073 Graph* graph;
00074 GraphAdaptorBase() : graph(0) { }
00075 void setGraph(Graph& _graph) { graph=&_graph; }
00076
00077 public:
00078 GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
00079
00080 typedef typename Graph::Node Node;
00081 typedef typename Graph::Edge Edge;
00082
00083 void first(Node& i) const { graph->first(i); }
00084 void first(Edge& i) const { graph->first(i); }
00085 void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
00086 void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
00087
00088 void next(Node& i) const { graph->next(i); }
00089 void next(Edge& i) const { graph->next(i); }
00090 void nextIn(Edge& i) const { graph->nextIn(i); }
00091 void nextOut(Edge& i) const { graph->nextOut(i); }
00092
00093 Node source(const Edge& e) const { return graph->source(e); }
00094 Node target(const Edge& e) const { return graph->target(e); }
00095
00096 int nodeNum() const { return graph->nodeNum(); }
00097 int edgeNum() const { return graph->edgeNum(); }
00098
00099 Node addNode() const { return Node(graph->addNode()); }
00100 Edge addEdge(const Node& source, const Node& target) const {
00101 return Edge(graph->addEdge(source, target)); }
00102
00103 void erase(const Node& i) const { graph->erase(i); }
00104 void erase(const Edge& i) const { graph->erase(i); }
00105
00106 void clear() const { graph->clear(); }
00107
00108 int id(const Node& v) const { return graph->id(v); }
00109 int id(const Edge& e) const { return graph->id(e); }
00110
00111 Edge oppositeNode(const Edge& e) const {
00112 return Edge(graph->opposite(e));
00113 }
00114
00115 template <typename _Value>
00116 class NodeMap : public _Graph::template NodeMap<_Value> {
00117 public:
00118 typedef typename _Graph::template NodeMap<_Value> Parent;
00119 NodeMap(const GraphAdaptorBase<_Graph>& gw) : Parent(*gw.graph) { }
00120 NodeMap(const GraphAdaptorBase<_Graph>& gw, const _Value& value)
00121 : Parent(*gw.graph, value) { }
00122 };
00123
00124 template <typename _Value>
00125 class EdgeMap : public _Graph::template EdgeMap<_Value> {
00126 public:
00127 typedef typename _Graph::template EdgeMap<_Value> Parent;
00128 EdgeMap(const GraphAdaptorBase<_Graph>& gw) : Parent(*gw.graph) { }
00129 EdgeMap(const GraphAdaptorBase<_Graph>& gw, const _Value& value)
00130 : Parent(*gw.graph, value) { }
00131 };
00132
00133 };
00134
00135 template <typename _Graph>
00136 class GraphAdaptor :
00137 public IterableGraphExtender<GraphAdaptorBase<_Graph> > {
00138 public:
00139 typedef _Graph Graph;
00140 typedef IterableGraphExtender<GraphAdaptorBase<_Graph> > Parent;
00141 protected:
00142 GraphAdaptor() : Parent() { }
00143
00144 public:
00145 GraphAdaptor(Graph& _graph) { setGraph(_graph); }
00146 };
00147
00148 template <typename _Graph>
00149 class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
00150 public:
00151 typedef _Graph Graph;
00152 typedef GraphAdaptorBase<_Graph> Parent;
00153 protected:
00154 RevGraphAdaptorBase() : Parent() { }
00155 public:
00156 typedef typename Parent::Node Node;
00157 typedef typename Parent::Edge Edge;
00158
00159
00160 void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
00161 void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
00162
00163
00164 void nextIn(Edge& i) const { Parent::nextOut(i); }
00165 void nextOut(Edge& i) const { Parent::nextIn(i); }
00166
00167 Node source(const Edge& e) const { return Parent::target(e); }
00168 Node target(const Edge& e) const { return Parent::source(e); }
00169 };
00170
00171
00173
00195 template<typename _Graph>
00196 class RevGraphAdaptor :
00197 public IterableGraphExtender<RevGraphAdaptorBase<_Graph> > {
00198 public:
00199 typedef _Graph Graph;
00200 typedef IterableGraphExtender<
00201 RevGraphAdaptorBase<_Graph> > Parent;
00202 protected:
00203 RevGraphAdaptor() { }
00204 public:
00205 RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
00206 };
00207
00208
00209 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
00210 class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
00211 public:
00212 typedef _Graph Graph;
00213 typedef GraphAdaptorBase<_Graph> Parent;
00214 protected:
00215 NodeFilterMap* node_filter_map;
00216 EdgeFilterMap* edge_filter_map;
00217 SubGraphAdaptorBase() : Parent(),
00218 node_filter_map(0), edge_filter_map(0) { }
00219
00220 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
00221 node_filter_map=&_node_filter_map;
00222 }
00223 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
00224 edge_filter_map=&_edge_filter_map;
00225 }
00226
00227 public:
00228
00229
00230
00231
00232
00233
00234
00235 typedef typename Parent::Node Node;
00236 typedef typename Parent::Edge Edge;
00237
00238 void first(Node& i) const {
00239 Parent::first(i);
00240 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
00241 }
00242 void first(Edge& i) const {
00243 Parent::first(i);
00244 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
00245 }
00246 void firstIn(Edge& i, const Node& n) const {
00247 Parent::firstIn(i, n);
00248 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
00249 }
00250 void firstOut(Edge& i, const Node& n) const {
00251 Parent::firstOut(i, n);
00252 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
00253 }
00254
00255 void next(Node& i) const {
00256 Parent::next(i);
00257 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
00258 }
00259 void next(Edge& i) const {
00260 Parent::next(i);
00261 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
00262 }
00263 void nextIn(Edge& i) const {
00264 Parent::nextIn(i);
00265 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
00266 }
00267 void nextOut(Edge& i) const {
00268 Parent::nextOut(i);
00269 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
00270 }
00271
00275 void hide(const Node& n) const { node_filter_map->set(n, false); }
00276
00280 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
00281
00285 void unHide(const Node& n) const { node_filter_map->set(n, true); }
00286
00290 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
00291
00293 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
00294
00296 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
00297
00301 int nodeNum() const {
00302 int i=0;
00303 Node n;
00304 for (first(n); n!=INVALID; next(n)) ++i;
00305 return i;
00306 }
00307
00311 int edgeNum() const {
00312 int i=0;
00313 Edge e;
00314 for (first(e); e!=INVALID; next(e)) ++i;
00315 return i;
00316 }
00317
00318
00319 };
00320
00377 template<typename _Graph, typename NodeFilterMap,
00378 typename EdgeFilterMap>
00379 class SubGraphAdaptor :
00380 public IterableGraphExtender<
00381 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > {
00382 public:
00383 typedef _Graph Graph;
00384 typedef IterableGraphExtender<
00385 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
00386 protected:
00387 SubGraphAdaptor() { }
00388 public:
00389 SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map,
00390 EdgeFilterMap& _edge_filter_map) {
00391 setGraph(_graph);
00392 setNodeFilterMap(_node_filter_map);
00393 setEdgeFilterMap(_edge_filter_map);
00394 }
00395 };
00396
00397
00398
00410 template<typename Graph, typename NodeFilterMap>
00411 class NodeSubGraphAdaptor :
00412 public SubGraphAdaptor<Graph, NodeFilterMap,
00413 ConstMap<typename Graph::Edge,bool> > {
00414 public:
00415 typedef SubGraphAdaptor<Graph, NodeFilterMap,
00416 ConstMap<typename Graph::Edge,bool> > Parent;
00417 protected:
00418 ConstMap<typename Graph::Edge, bool> const_true_map;
00419 public:
00420 NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) :
00421 Parent(), const_true_map(true) {
00422 Parent::setGraph(_graph);
00423 Parent::setNodeFilterMap(_node_filter_map);
00424 Parent::setEdgeFilterMap(const_true_map);
00425 }
00426 };
00427
00428
00560 template<typename Graph, typename EdgeFilterMap>
00561 class EdgeSubGraphAdaptor :
00562 public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
00563 EdgeFilterMap> {
00564 public:
00565 typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
00566 EdgeFilterMap> Parent;
00567 protected:
00568 ConstMap<typename Graph::Node, bool> const_true_map;
00569 public:
00570 EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
00571 Parent(), const_true_map(true) {
00572 Parent::setGraph(_graph);
00573 Parent::setNodeFilterMap(const_true_map);
00574 Parent::setEdgeFilterMap(_edge_filter_map);
00575 }
00576 };
00577
00578 template <typename _Graph>
00579 class UndirGraphAdaptorBase :
00580 public UndirGraphExtender<GraphAdaptorBase<_Graph> > {
00581 public:
00582 typedef _Graph Graph;
00583 typedef UndirGraphExtender<GraphAdaptorBase<_Graph> > Parent;
00584 protected:
00585 UndirGraphAdaptorBase() : Parent() { }
00586 public:
00587 typedef typename Parent::UndirEdge UndirEdge;
00588 typedef typename Parent::Edge Edge;
00589
00593 template <typename T>
00594 class EdgeMap {
00595 protected:
00596 const UndirGraphAdaptorBase<_Graph>* g;
00597 template <typename TT> friend class EdgeMap;
00598 typename _Graph::template EdgeMap<T> forward_map, backward_map;
00599 public:
00600 typedef T Value;
00601 typedef Edge Key;
00602
00603 EdgeMap(const UndirGraphAdaptorBase<_Graph>& _g) : g(&_g),
00604 forward_map(*(g->graph)), backward_map(*(g->graph)) { }
00605
00606 EdgeMap(const UndirGraphAdaptorBase<_Graph>& _g, T a) : g(&_g),
00607 forward_map(*(g->graph), a), backward_map(*(g->graph), a) { }
00608
00609 void set(Edge e, T a) {
00610 if (g->direction(e))
00611 forward_map.set(e, a);
00612 else
00613 backward_map.set(e, a);
00614 }
00615
00616 T operator[](Edge e) const {
00617 if (g->direction(e))
00618 return forward_map[e];
00619 else
00620 return backward_map[e];
00621 }
00622 };
00623
00624 template <typename T>
00625 class UndirEdgeMap {
00626 template <typename TT> friend class UndirEdgeMap;
00627 typename _Graph::template EdgeMap<T> map;
00628 public:
00629 typedef T Value;
00630 typedef UndirEdge Key;
00631
00632 UndirEdgeMap(const UndirGraphAdaptorBase<_Graph>& g) :
00633 map(*(g.graph)) { }
00634
00635 UndirEdgeMap(const UndirGraphAdaptorBase<_Graph>& g, T a) :
00636 map(*(g.graph), a) { }
00637
00638 void set(UndirEdge e, T a) {
00639 map.set(e, a);
00640 }
00641
00642 T operator[](UndirEdge e) const {
00643 return map[e];
00644 }
00645 };
00646
00647 };
00648
00655 template<typename _Graph>
00656 class UndirGraphAdaptor :
00657 public IterableUndirGraphExtender<
00658 UndirGraphAdaptorBase<_Graph> > {
00659 public:
00660 typedef _Graph Graph;
00661 typedef IterableUndirGraphExtender<
00662 UndirGraphAdaptorBase<_Graph> > Parent;
00663 protected:
00664 UndirGraphAdaptor() { }
00665 public:
00666 UndirGraphAdaptor(_Graph& _graph) {
00667 setGraph(_graph);
00668 }
00669 };
00670
00671
00672 template <typename _Graph,
00673 typename ForwardFilterMap, typename BackwardFilterMap>
00674 class SubBidirGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
00675 public:
00676 typedef _Graph Graph;
00677 typedef GraphAdaptorBase<_Graph> Parent;
00678 protected:
00679 ForwardFilterMap* forward_filter;
00680 BackwardFilterMap* backward_filter;
00681 SubBidirGraphAdaptorBase() : Parent(),
00682 forward_filter(0), backward_filter(0) { }
00683
00684 void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
00685 forward_filter=&_forward_filter;
00686 }
00687 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
00688 backward_filter=&_backward_filter;
00689 }
00690
00691 public:
00692
00693
00694
00695
00696
00697
00698
00699 typedef typename Parent::Node Node;
00700 typedef typename _Graph::Edge GraphEdge;
00701 template <typename T> class EdgeMap;
00706 class Edge : public _Graph::Edge {
00707 friend class SubBidirGraphAdaptorBase<
00708 Graph, ForwardFilterMap, BackwardFilterMap>;
00709 template<typename T> friend class EdgeMap;
00710 protected:
00711 bool backward;
00712 public:
00713 Edge() { }
00717 Edge(const typename _Graph::Edge& e, bool _backward) :
00718 _Graph::Edge(e), backward(_backward) { }
00719 Edge(Invalid i) : _Graph::Edge(i), backward(true) { }
00720 bool operator==(const Edge& v) const {
00721 return (this->backward==v.backward &&
00722 static_cast<typename _Graph::Edge>(*this)==
00723 static_cast<typename _Graph::Edge>(v));
00724 }
00725 bool operator!=(const Edge& v) const {
00726 return (this->backward!=v.backward ||
00727 static_cast<typename _Graph::Edge>(*this)!=
00728 static_cast<typename _Graph::Edge>(v));
00729 }
00730 };
00731
00732 void first(Node& i) const {
00733 Parent::first(i);
00734 }
00735
00736 void first(Edge& i) const {
00737 Parent::first(i);
00738 i.backward=false;
00739 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00740 !(*forward_filter)[i]) Parent::next(i);
00741 if (*static_cast<GraphEdge*>(&i)==INVALID) {
00742 Parent::first(i);
00743 i.backward=true;
00744 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00745 !(*backward_filter)[i]) Parent::next(i);
00746 }
00747 }
00748
00749 void firstIn(Edge& i, const Node& n) const {
00750 Parent::firstIn(i, n);
00751 i.backward=false;
00752 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00753 !(*forward_filter)[i]) Parent::nextIn(i);
00754 if (*static_cast<GraphEdge*>(&i)==INVALID) {
00755 Parent::firstOut(i, n);
00756 i.backward=true;
00757 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00758 !(*backward_filter)[i]) Parent::nextOut(i);
00759 }
00760 }
00761
00762 void firstOut(Edge& i, const Node& n) const {
00763 Parent::firstOut(i, n);
00764 i.backward=false;
00765 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00766 !(*forward_filter)[i]) Parent::nextOut(i);
00767 if (*static_cast<GraphEdge*>(&i)==INVALID) {
00768 Parent::firstIn(i, n);
00769 i.backward=true;
00770 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00771 !(*backward_filter)[i]) Parent::nextIn(i);
00772 }
00773 }
00774
00775 void next(Node& i) const {
00776 Parent::next(i);
00777 }
00778
00779 void next(Edge& i) const {
00780 if (!(i.backward)) {
00781 Parent::next(i);
00782 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00783 !(*forward_filter)[i]) Parent::next(i);
00784 if (*static_cast<GraphEdge*>(&i)==INVALID) {
00785 Parent::first(i);
00786 i.backward=true;
00787 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00788 !(*backward_filter)[i]) Parent::next(i);
00789 }
00790 } else {
00791 Parent::next(i);
00792 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00793 !(*backward_filter)[i]) Parent::next(i);
00794 }
00795 }
00796
00797 void nextIn(Edge& i) const {
00798 if (!(i.backward)) {
00799 Node n=Parent::target(i);
00800 Parent::nextIn(i);
00801 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00802 !(*forward_filter)[i]) Parent::nextIn(i);
00803 if (*static_cast<GraphEdge*>(&i)==INVALID) {
00804 Parent::firstOut(i, n);
00805 i.backward=true;
00806 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00807 !(*backward_filter)[i]) Parent::nextOut(i);
00808 }
00809 } else {
00810 Parent::nextOut(i);
00811 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00812 !(*backward_filter)[i]) Parent::nextOut(i);
00813 }
00814 }
00815
00816 void nextOut(Edge& i) const {
00817 if (!(i.backward)) {
00818 Node n=Parent::source(i);
00819 Parent::nextOut(i);
00820 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00821 !(*forward_filter)[i]) Parent::nextOut(i);
00822 if (*static_cast<GraphEdge*>(&i)==INVALID) {
00823 Parent::firstIn(i, n);
00824 i.backward=true;
00825 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00826 !(*backward_filter)[i]) Parent::nextIn(i);
00827 }
00828 } else {
00829 Parent::nextIn(i);
00830 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
00831 !(*backward_filter)[i]) Parent::nextIn(i);
00832 }
00833 }
00834
00835 Node source(Edge e) const {
00836 return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
00837 Node target(Edge e) const {
00838 return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
00839
00841 Edge opposite(const Edge& e) const {
00842 Edge f=e;
00843 f.backward=!f.backward;
00844 return f;
00845 }
00846
00850 int edgeNum() const {
00851 int i=0;
00852 Edge e;
00853 for (first(e); e!=INVALID; next(e)) ++i;
00854 return i;
00855 }
00856
00857 bool forward(const Edge& e) const { return !e.backward; }
00858 bool backward(const Edge& e) const { return e.backward; }
00859
00860 template <typename T>
00864 class EdgeMap {
00865 template <typename TT> friend class EdgeMap;
00866 typename _Graph::template EdgeMap<T> forward_map, backward_map;
00867 public:
00868 typedef T Value;
00869 typedef Edge Key;
00870
00871 EdgeMap(const SubBidirGraphAdaptorBase<_Graph,
00872 ForwardFilterMap, BackwardFilterMap>& g) :
00873 forward_map(*(g.graph)), backward_map(*(g.graph)) { }
00874
00875 EdgeMap(const SubBidirGraphAdaptorBase<_Graph,
00876 ForwardFilterMap, BackwardFilterMap>& g, T a) :
00877 forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
00878
00879 void set(Edge e, T a) {
00880 if (!e.backward)
00881 forward_map.set(e, a);
00882 else
00883 backward_map.set(e, a);
00884 }
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895 T operator[](Edge e) const {
00896 if (!e.backward)
00897 return forward_map[e];
00898 else
00899 return backward_map[e];
00900 }
00901
00902 void update() {
00903 forward_map.update();
00904 backward_map.update();
00905 }
00906 };
00907
00908 };
00909
00910
00949 template<typename _Graph,
00950 typename ForwardFilterMap, typename BackwardFilterMap>
00951 class SubBidirGraphAdaptor :
00952 public IterableGraphExtender<
00953 SubBidirGraphAdaptorBase<_Graph, ForwardFilterMap, BackwardFilterMap> > {
00954 public:
00955 typedef _Graph Graph;
00956 typedef IterableGraphExtender<
00957 SubBidirGraphAdaptorBase<
00958 _Graph, ForwardFilterMap, BackwardFilterMap> > Parent;
00959 protected:
00960 SubBidirGraphAdaptor() { }
00961 public:
00962 SubBidirGraphAdaptor(_Graph& _graph, ForwardFilterMap& _forward_filter,
00963 BackwardFilterMap& _backward_filter) {
00964 setGraph(_graph);
00965 setForwardFilterMap(_forward_filter);
00966 setBackwardFilterMap(_backward_filter);
00967 }
00968 };
00969
00970
00971
00981 template<typename Graph>
00982 class BidirGraphAdaptor :
00983 public SubBidirGraphAdaptor<
00984 Graph,
00985 ConstMap<typename Graph::Edge, bool>,
00986 ConstMap<typename Graph::Edge, bool> > {
00987 public:
00988 typedef SubBidirGraphAdaptor<
00989 Graph,
00990 ConstMap<typename Graph::Edge, bool>,
00991 ConstMap<typename Graph::Edge, bool> > Parent;
00992 protected:
00993 ConstMap<typename Graph::Edge, bool> cm;
00994
00995 BidirGraphAdaptor() : Parent(), cm(true) {
00996 Parent::setForwardFilterMap(cm);
00997 Parent::setBackwardFilterMap(cm);
00998 }
00999 public:
01000 BidirGraphAdaptor(Graph& _graph) : Parent(), cm(true) {
01001 Parent::setGraph(_graph);
01002 Parent::setForwardFilterMap(cm);
01003 Parent::setBackwardFilterMap(cm);
01004 }
01005
01006 int edgeNum() const {
01007 return 2*this->graph->edgeNum();
01008 }
01009
01010 };
01011
01012
01013 template<typename Graph, typename Number,
01014 typename CapacityMap, typename FlowMap>
01015 class ResForwardFilter {
01016
01017 const CapacityMap* capacity;
01018 const FlowMap* flow;
01019 public:
01020 ResForwardFilter(
01021 const CapacityMap& _capacity, const FlowMap& _flow) :
01022 capacity(&_capacity), flow(&_flow) { }
01023 ResForwardFilter() : capacity(0), flow(0) { }
01024 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
01025 void setFlow(const FlowMap& _flow) { flow=&_flow; }
01026 bool operator[](const typename Graph::Edge& e) const {
01027 return (Number((*flow)[e]) < Number((*capacity)[e]));
01028 }
01029 };
01030
01031 template<typename Graph, typename Number,
01032 typename CapacityMap, typename FlowMap>
01033 class ResBackwardFilter {
01034 const CapacityMap* capacity;
01035 const FlowMap* flow;
01036 public:
01037 ResBackwardFilter(
01038 const CapacityMap& _capacity, const FlowMap& _flow) :
01039 capacity(&_capacity), flow(&_flow) { }
01040 ResBackwardFilter() : capacity(0), flow(0) { }
01041 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
01042 void setFlow(const FlowMap& _flow) { flow=&_flow; }
01043 bool operator[](const typename Graph::Edge& e) const {
01044 return (Number(0) < Number((*flow)[e]));
01045 }
01046 };
01047
01048
01081 template<typename Graph, typename Number,
01082 typename CapacityMap, typename FlowMap>
01083 class ResGraphAdaptor :
01084 public SubBidirGraphAdaptor<
01085 Graph,
01086 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
01087 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
01088 public:
01089 typedef SubBidirGraphAdaptor<
01090 Graph,
01091 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
01092 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
01093 protected:
01094 const CapacityMap* capacity;
01095 FlowMap* flow;
01096 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
01097 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
01098 ResGraphAdaptor() : Parent(),
01099 capacity(0), flow(0) { }
01100 void setCapacityMap(const CapacityMap& _capacity) {
01101 capacity=&_capacity;
01102 forward_filter.setCapacity(_capacity);
01103 backward_filter.setCapacity(_capacity);
01104 }
01105 void setFlowMap(FlowMap& _flow) {
01106 flow=&_flow;
01107 forward_filter.setFlow(_flow);
01108 backward_filter.setFlow(_flow);
01109 }
01110 public:
01111 ResGraphAdaptor(Graph& _graph, const CapacityMap& _capacity,
01112 FlowMap& _flow) :
01113 Parent(), capacity(&_capacity), flow(&_flow),
01114 forward_filter( _capacity, _flow),
01115 backward_filter( _capacity, _flow) {
01116 Parent::setGraph(_graph);
01117 Parent::setForwardFilterMap(forward_filter);
01118 Parent::setBackwardFilterMap(backward_filter);
01119 }
01120
01121 typedef typename Parent::Edge Edge;
01122
01123 void augment(const Edge& e, Number a) const {
01124 if (Parent::forward(e))
01125 flow->set(e, (*flow)[e]+a);
01126 else
01127 flow->set(e, (*flow)[e]-a);
01128 }
01129
01134 class ResCap {
01135 protected:
01136 const ResGraphAdaptor<Graph, Number, CapacityMap, FlowMap>* res_graph;
01137 public:
01138 typedef Number Value;
01139 typedef Edge Key;
01140 ResCap(const ResGraphAdaptor<Graph, Number, CapacityMap, FlowMap>&
01141 _res_graph) : res_graph(&_res_graph) { }
01142 Number operator[](const Edge& e) const {
01143 if (res_graph->forward(e))
01144 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
01145 else
01146 return (*(res_graph->flow))[e];
01147 }
01148 };
01149
01150
01151 };
01152
01153
01154
01155 template <typename _Graph, typename FirstOutEdgesMap>
01156 class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
01157 public:
01158 typedef _Graph Graph;
01159 typedef GraphAdaptorBase<_Graph> Parent;
01160 protected:
01161 FirstOutEdgesMap* first_out_edges;
01162 ErasingFirstGraphAdaptorBase() : Parent(),
01163 first_out_edges(0) { }
01164
01165 void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
01166 first_out_edges=&_first_out_edges;
01167 }
01168
01169 public:
01170
01171 typedef typename Parent::Node Node;
01172 typedef typename Parent::Edge Edge;
01173
01174 void firstOut(Edge& i, const Node& n) const {
01175 i=(*first_out_edges)[n];
01176 }
01177
01178 void erase(const Edge& e) const {
01179 Node n=source(e);
01180 Edge f=e;
01181 Parent::nextOut(f);
01182 first_out_edges->set(n, f);
01183 }
01184 };
01185
01186
01188
01201 template <typename _Graph, typename FirstOutEdgesMap>
01202 class ErasingFirstGraphAdaptor :
01203 public IterableGraphExtender<
01204 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
01205 public:
01206 typedef _Graph Graph;
01207 typedef IterableGraphExtender<
01208 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
01209 ErasingFirstGraphAdaptor(Graph& _graph,
01210 FirstOutEdgesMap& _first_out_edges) {
01211 setGraph(_graph);
01212 setFirstOutEdgesMap(_first_out_edges);
01213 }
01214
01215 };
01216
01217 template <typename _Graph>
01218 class NewEdgeSetAdaptorBase {
01219 public:
01220
01221 typedef _Graph Graph;
01222 typedef typename Graph::Node Node;
01223 typedef typename Graph::NodeIt NodeIt;
01224
01225 protected:
01226
01227 struct NodeT {
01228 int first_out, first_in;
01229 NodeT() : first_out(-1), first_in(-1) {}
01230 };
01231
01232 class NodesImpl : protected Graph::template NodeMap<NodeT> {
01233
01234 typedef typename Graph::template NodeMap<NodeT> Parent;
01235 typedef NewEdgeSetAdaptorBase<Graph> Adaptor;
01236
01237 Adaptor& adaptor;
01238
01239 public:
01240
01241 NodesImpl(Adaptor& _adaptor, const Graph& _graph)
01242 : Parent(_graph), adaptor(_adaptor) {}
01243
01244 virtual ~NodesImpl() {}
01245
01246 virtual void build() {
01247 Parent::build();
01248 }
01249
01250 virtual void clear() {
01251 adaptor._clear();
01252 Parent::clear();
01253 }
01254
01255 virtual void add(const Node& node) {
01256 Parent::add(node);
01257 adaptor._add(node);
01258 }
01259
01260 virtual void erase(const Node& node) {
01261 adaptor._erase(node);
01262 Parent::erase(node);
01263 }
01264
01265 NodeT& operator[](const Node& node) {
01266 return Parent::operator[](node);
01267 }
01268
01269 const NodeT& operator[](const Node& node) const {
01270 return Parent::operator[](node);
01271 }
01272
01273 };
01274
01275 NodesImpl* nodes;
01276
01277 struct EdgeT {
01278 Node source, target;
01279 int next_out, next_in;
01280 int prev_out, prev_in;
01281 EdgeT() : prev_out(-1), prev_in(-1) {}
01282 };
01283
01284 std::vector<EdgeT> edges;
01285
01286 int first_edge;
01287 int first_free_edge;
01288
01289 virtual void _clear() = 0;
01290 virtual void _add(const Node& node) = 0;
01291 virtual void _erase(const Node& node) = 0;
01292
01293 const Graph* graph;
01294
01295 void initalize(const Graph& _graph, NodesImpl& _nodes) {
01296 graph = &_graph;
01297 nodes = &_nodes;
01298 }
01299
01300 public:
01301
01302 class Edge {
01303 friend class NewEdgeSetAdaptorBase<Graph>;
01304 protected:
01305 Edge(int _id) : id(_id) {}
01306 int id;
01307 public:
01308 Edge() {}
01309 Edge(Invalid) : id(-1) {}
01310 bool operator==(const Edge& edge) const { return id == edge.id; }
01311 bool operator!=(const Edge& edge) const { return id != edge.id; }
01312 bool operator<(const Edge& edge) const { return id < edge.id; }
01313 };
01314
01315 NewEdgeSetAdaptorBase() : first_edge(-1), first_free_edge(-1) {}
01316 virtual ~NewEdgeSetAdaptorBase() {}
01317
01318 Edge addEdge(const Node& source, const Node& target) {
01319 int n;
01320 if (first_free_edge == -1) {
01321 n = edges.size();
01322 edges.push_back(EdgeT());
01323 } else {
01324 n = first_free_edge;
01325 first_free_edge = edges[first_free_edge].next_in;
01326 }
01327 edges[n].next_in = (*nodes)[target].first_in;
01328 (*nodes)[target].first_in = n;
01329 edges[n].next_out = (*nodes)[source].first_out;
01330 (*nodes)[source].first_out = n;
01331 edges[n].source = source;
01332 edges[n].target = target;
01333 return Edge(n);
01334 }
01335
01336 void erase(const Edge& edge) {
01337 int n = edge.id;
01338 if (edges[n].prev_in != -1) {
01339 edges[edges[n].prev_in].next_in = edges[n].next_in;
01340 } else {
01341 (*nodes)[edges[n].target].first_in = edges[n].next_in;
01342 }
01343 if (edges[n].next_in != -1) {
01344 edges[edges[n].next_in].prev_in = edges[n].prev_in;
01345 }
01346
01347 if (edges[n].prev_out != -1) {
01348 edges[edges[n].prev_out].next_out = edges[n].next_out;
01349 } else {
01350 (*nodes)[edges[n].source].first_out = edges[n].next_out;
01351 }
01352 if (edges[n].next_out != -1) {
01353 edges[edges[n].next_out].prev_out = edges[n].prev_out;
01354 }
01355
01356 }
01357
01358 void first(Node& node) const {
01359 graph->first(node);
01360 }
01361
01362 void next(Node& node) const {
01363 graph->next(node);
01364 }
01365
01366 void first(Edge& edge) const {
01367 Node node;
01368 for (first(node); node != INVALID && (*nodes)[node].first_in == -1;
01369 next(node));
01370 edge.id = (node == INVALID) ? -1 : (*nodes)[node].first_in;
01371 }
01372
01373 void next(Edge& edge) const {
01374 if (edges[edge.id].next_in != -1) {
01375 edge.id = edges[edge.id].next_in;
01376 } else {
01377 Node node = edges[edge.id].target;
01378 for (next(node); node != INVALID && (*nodes)[node].first_in == -1;
01379 next(node));
01380 edge.id = (node == INVALID) ? -1 : (*nodes)[node].first_in;
01381 }
01382 }
01383
01384 void firstOut(Edge& edge, const Node& node) const {
01385 edge.id = (*nodes)[node].first_out;
01386 }
01387
01388 void nextOut(Edge& edge) const {
01389 edge.id = edges[edge.id].next_out;
01390 }
01391
01392 void firstIn(Edge& edge, const Node& node) const {
01393 edge.id = (*nodes)[node].first_in;
01394 }
01395
01396 void nextIn(Edge& edge) const {
01397 edge.id = edges[edge.id].next_in;
01398 }
01399
01400 int id(const Node& node) const { return graph->id(node); }
01401 int id(const Edge& edge) const { return edge.id; }
01402
01403 Node fromId(int id, Node) const { return graph->fromId(id, Node()); }
01404 Edge fromId(int id, Edge) const { return Edge(id); }
01405
01406 int maxId(Node) const { return graph->maxId(Node()); };
01407 int maxId(Edge) const { return edges.size() - 1; }
01408
01409 Node source(const Edge& edge) const { return edges[edge.id].source;}
01410 Node target(const Edge& edge) const { return edges[edge.id].target;}
01411
01412 };
01413
01414
01428 template <typename _Graph>
01429 class NewEdgeSetAdaptor :
01430 public ErasableGraphExtender<
01431 ClearableGraphExtender<
01432 ExtendableGraphExtender<
01433 DefaultMappableGraphExtender<
01434 IterableGraphExtender<
01435 AlterableGraphExtender<
01436 NewEdgeSetAdaptorBase<_Graph> > > > > > > {
01437
01438 public:
01439
01440 typedef ErasableGraphExtender<
01441 ClearableGraphExtender<
01442 ExtendableGraphExtender<
01443 DefaultMappableGraphExtender<
01444 IterableGraphExtender<
01445 AlterableGraphExtender<
01446 NewEdgeSetAdaptorBase<_Graph> > > > > > > Parent;
01447
01448
01449 typedef typename Parent::Node Node;
01450 typedef typename Parent::Edge Edge;
01451
01452 private:
01453
01454 virtual void _clear() {
01455 Parent::edges.clear();
01456 Parent::first_edge = -1;
01457 Parent::first_free_edge = -1;
01458 Parent::getNotifier(Edge()).clear();
01459 Parent::getNotifier(Node()).clear();
01460 }
01461
01462 virtual void _add(const Node& node) {
01463 Parent::getNotifier(Node()).add(node);
01464 }
01465
01466 virtual void _erase(const Node& node) {
01467 Edge edge;
01468 Parent::firstOut(edge, node);
01469 while (edge != INVALID) {
01470 Parent::erase(edge);
01471 Parent::firstOut(edge, node);
01472 }
01473
01474 Parent::firstIn(edge, node);
01475 while (edge != INVALID) {
01476 Parent::erase(edge);
01477 Parent::firstIn(edge, node);
01478 }
01479
01480 Parent::getNotifier(Node()).erase(node);
01481 }
01482
01483
01484 typedef typename Parent::NodesImpl NodesImpl;
01485
01486 NodesImpl nodes;
01487
01488 public:
01489
01493 NewEdgeSetAdaptor(const _Graph& _graph) : nodes(*this, _graph) {
01494 Parent::initalize(_graph, nodes);
01495 }
01496
01497 void clear() {
01498 Parent::getNotifier(Edge()).clear();
01499
01500 Parent::edges.clear();
01501 Parent::first_edge = -1;
01502 Parent::first_free_edge = -1;
01503 }
01504
01505 };
01506
01520 template <typename _Graph>
01521 class NewUndirEdgeSetAdaptor :
01522 public ErasableUndirGraphExtender<
01523 ClearableUndirGraphExtender<
01524 ExtendableUndirGraphExtender<
01525 MappableUndirGraphExtender<
01526 IterableUndirGraphExtender<
01527 AlterableUndirGraphExtender<
01528 UndirGraphExtender<
01529 NewEdgeSetAdaptorBase<_Graph> > > > > > > > {
01530
01531 public:
01532
01533 typedef ErasableUndirGraphExtender<
01534 ClearableUndirGraphExtender<
01535 ExtendableUndirGraphExtender<
01536 MappableUndirGraphExtender<
01537 IterableUndirGraphExtender<
01538 AlterableUndirGraphExtender<
01539 UndirGraphExtender<
01540 NewEdgeSetAdaptorBase<_Graph> > > > > > > > Parent;
01541
01542
01543 typedef typename Parent::Node Node;
01544 typedef typename Parent::Edge Edge;
01545 typedef typename Parent::UndirEdge UndirEdge;
01546
01547 private:
01548
01549 virtual void _clear() {
01550 Parent::edges.clear();
01551 Parent::first_edge = -1;
01552 Parent::first_free_edge = -1;
01553 Parent::getNotifier(Edge()).clear();
01554 Parent::getNotifier(Node()).clear();
01555 }
01556
01557 virtual void _add(const Node& node) {
01558 Parent::getNotifier(Node()).add(node);
01559 }
01560
01561 virtual void _erase(const Node& node) {
01562 Edge edge;
01563 Parent::firstOut(edge, node);
01564 while (edge != INVALID) {
01565 Parent::erase(edge);
01566 Parent::firstOut(edge, node);
01567 }
01568
01569 Parent::firstIn(edge, node);
01570 while (edge != INVALID) {
01571 Parent::erase(edge);
01572 Parent::firstIn(edge, node);
01573 }
01574
01575 Parent::getNotifier(Node()).erase(node);
01576 }
01577
01578 typedef typename Parent::NodesImpl NodesImpl;
01579
01580 NodesImpl nodes;
01581
01582 public:
01583
01584
01588 NewUndirEdgeSetAdaptor(const _Graph& _graph) : nodes(*this, _graph) {
01589 Parent::initalize(_graph, nodes);
01590 }
01591
01592 void clear() {
01593 Parent::getNotifier(Edge()).clear();
01594 Parent::getNotifier(UndirEdge()).clear();
01595
01596 Parent::edges.clear();
01597 Parent::first_edge = -1;
01598 Parent::first_free_edge = -1;
01599 }
01600
01601 };
01602
01604
01605 }
01606
01607 #endif //LEMON_GRAPH_ADAPTOR_H
01608