graph_wrapper.h

Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * src/lemon/graph_wrapper.h - Part of LEMON, a generic C++ optimization library 00003 * 00004 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 00005 * (Egervary Combinatorial Optimization Research Group, EGRES). 00006 * 00007 * Permission to use, modify and distribute this software is granted 00008 * provided that this copyright notice appears in all copies. For 00009 * precise terms see the accompanying LICENSE file. 00010 * 00011 * This software is provided "AS IS" with no warranty of any kind, 00012 * express or implied, and with no claim as to its suitability for any 00013 * purpose. 00014 * 00015 */ 00016 00017 #ifndef LEMON_GRAPH_WRAPPER_H 00018 #define LEMON_GRAPH_WRAPPER_H 00019 00027 00028 #include <lemon/invalid.h> 00029 #include <lemon/maps.h> 00030 #include <lemon/map_defines.h> 00031 #include <iostream> 00032 00033 namespace lemon { 00034 00035 // Graph wrappers 00036 00093 00096 00098 00111 template<typename Graph> 00112 class GraphWrapper { 00113 protected: 00114 Graph* graph; 00115 GraphWrapper() : graph(0) { } 00116 void setGraph(Graph& _graph) { graph=&_graph; } 00117 00118 public: 00119 typedef Graph BaseGraph; 00120 typedef Graph ParentGraph; 00121 00122 GraphWrapper(Graph& _graph) : graph(&_graph) { } 00123 GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { } 00124 00125 typedef typename Graph::Node Node; 00126 class NodeIt : public Node { 00127 const GraphWrapper<Graph>* gw; 00128 friend class GraphWrapper<Graph>; 00129 public: 00130 NodeIt() { } 00131 NodeIt(Invalid i) : Node(i) { } 00132 NodeIt(const GraphWrapper<Graph>& _gw) : 00133 Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { } 00134 NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 00135 Node(n), gw(&_gw) { } 00136 NodeIt& operator++() { 00137 *(static_cast<Node*>(this))= 00138 ++(typename Graph::NodeIt(*(gw->graph), *this)); 00139 return *this; 00140 } 00141 }; 00142 typedef typename Graph::Edge Edge; 00143 class OutEdgeIt : public Edge { 00144 const GraphWrapper<Graph>* gw; 00145 friend class GraphWrapper<Graph>; 00146 public: 00147 OutEdgeIt() { } 00148 OutEdgeIt(Invalid i) : Edge(i) { } 00149 OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 00150 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { } 00151 OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 00152 Edge(e), gw(&_gw) { } 00153 OutEdgeIt& operator++() { 00154 *(static_cast<Edge*>(this))= 00155 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00156 return *this; 00157 } 00158 }; 00159 class InEdgeIt : public Edge { 00160 const GraphWrapper<Graph>* gw; 00161 friend class GraphWrapper<Graph>; 00162 public: 00163 InEdgeIt() { } 00164 InEdgeIt(Invalid i) : Edge(i) { } 00165 InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 00166 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { } 00167 InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 00168 Edge(e), gw(&_gw) { } 00169 InEdgeIt& operator++() { 00170 *(static_cast<Edge*>(this))= 00171 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00172 return *this; 00173 } 00174 }; 00175 class EdgeIt : public Edge { 00176 const GraphWrapper<Graph>* gw; 00177 friend class GraphWrapper<Graph>; 00178 public: 00179 EdgeIt() { } 00180 EdgeIt(Invalid i) : Edge(i) { } 00181 EdgeIt(const GraphWrapper<Graph>& _gw) : 00182 Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { } 00183 EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 00184 Edge(e), gw(&_gw) { } 00185 EdgeIt& operator++() { 00186 *(static_cast<Edge*>(this))= 00187 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00188 return *this; 00189 } 00190 }; 00191 00192 NodeIt& first(NodeIt& i) const { 00193 i=NodeIt(*this); return i; 00194 } 00195 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 00196 i=OutEdgeIt(*this, p); return i; 00197 } 00198 InEdgeIt& first(InEdgeIt& i, const Node& p) const { 00199 i=InEdgeIt(*this, p); return i; 00200 } 00201 EdgeIt& first(EdgeIt& i) const { 00202 i=EdgeIt(*this); return i; 00203 } 00204 00205 Node tail(const Edge& e) const { 00206 return Node(graph->tail(static_cast<typename Graph::Edge>(e))); } 00207 Node head(const Edge& e) const { 00208 return Node(graph->head(static_cast<typename Graph::Edge>(e))); } 00209 00210 int nodeNum() const { return graph->nodeNum(); } 00211 int edgeNum() const { return graph->edgeNum(); } 00212 00213 Node addNode() const { return Node(graph->addNode()); } 00214 Edge addEdge(const Node& tail, const Node& head) const { 00215 return Edge(graph->addEdge(tail, head)); } 00216 00217 void erase(const Node& i) const { graph->erase(i); } 00218 void erase(const Edge& i) const { graph->erase(i); } 00219 00220 void clear() const { graph->clear(); } 00221 00222 bool forward(const Edge& e) const { return graph->forward(e); } 00223 bool backward(const Edge& e) const { return graph->backward(e); } 00224 00225 int id(const Node& v) const { return graph->id(v); } 00226 int id(const Edge& e) const { return graph->id(e); } 00227 00228 Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); } 00229 00230 00231 IMPORT_NODE_MAP(Graph, *(gw.graph), GraphWrapper, gw); 00232 IMPORT_EDGE_MAP(Graph, *(gw.graph), GraphWrapper, gw); 00233 00234 00235 }; 00236 00237 00238 00240 00262 template<typename Graph> 00263 class RevGraphWrapper : public GraphWrapper<Graph> { 00264 public: 00265 typedef GraphWrapper<Graph> Parent; 00266 protected: 00267 RevGraphWrapper() : GraphWrapper<Graph>() { } 00268 public: 00269 RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { } 00270 RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { } 00271 00272 typedef typename GraphWrapper<Graph>::Node Node; 00273 typedef typename GraphWrapper<Graph>::Edge Edge; 00274 //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other 00275 //because this does not work is some of them are not defined in the 00276 //original graph. The problem with this is that typedef-ed stuff 00277 //are instantiated in c++. 00278 class OutEdgeIt : public Edge { 00279 const RevGraphWrapper<Graph>* gw; 00280 friend class GraphWrapper<Graph>; 00281 public: 00282 OutEdgeIt() { } 00283 OutEdgeIt(Invalid i) : Edge(i) { } 00284 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 00285 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { } 00286 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 00287 Edge(e), gw(&_gw) { } 00288 OutEdgeIt& operator++() { 00289 *(static_cast<Edge*>(this))= 00290 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00291 return *this; 00292 } 00293 }; 00294 class InEdgeIt : public Edge { 00295 const RevGraphWrapper<Graph>* gw; 00296 friend class GraphWrapper<Graph>; 00297 public: 00298 InEdgeIt() { } 00299 InEdgeIt(Invalid i) : Edge(i) { } 00300 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 00301 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { } 00302 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 00303 Edge(e), gw(&_gw) { } 00304 InEdgeIt& operator++() { 00305 *(static_cast<Edge*>(this))= 00306 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00307 return *this; 00308 } 00309 }; 00310 00311 using GraphWrapper<Graph>::first; 00312 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 00313 i=OutEdgeIt(*this, p); return i; 00314 } 00315 InEdgeIt& first(InEdgeIt& i, const Node& p) const { 00316 i=InEdgeIt(*this, p); return i; 00317 } 00318 00319 Node tail(const Edge& e) const { 00320 return GraphWrapper<Graph>::head(e); } 00321 Node head(const Edge& e) const { 00322 return GraphWrapper<Graph>::tail(e); } 00323 00324 // KEEP_MAPS(Parent, RevGraphWrapper); 00325 00326 }; 00327 00328 00329 00331 00372 template<typename Graph, typename NodeFilterMap, 00373 typename EdgeFilterMap> 00374 class SubGraphWrapper : public GraphWrapper<Graph> { 00375 public: 00376 typedef GraphWrapper<Graph> Parent; 00377 protected: 00378 NodeFilterMap* node_filter_map; 00379 EdgeFilterMap* edge_filter_map; 00380 00381 SubGraphWrapper() : GraphWrapper<Graph>(), 00382 node_filter_map(0), edge_filter_map(0) { } 00383 void setNodeFilterMap(NodeFilterMap& _node_filter_map) { 00384 node_filter_map=&_node_filter_map; 00385 } 00386 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) { 00387 edge_filter_map=&_edge_filter_map; 00388 } 00389 00390 public: 00391 SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map, 00392 EdgeFilterMap& _edge_filter_map) : 00393 GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map), 00394 edge_filter_map(&_edge_filter_map) { } 00395 00396 typedef typename GraphWrapper<Graph>::Node Node; 00397 class NodeIt : public Node { 00398 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw; 00399 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>; 00400 public: 00401 NodeIt() { } 00402 NodeIt(Invalid i) : Node(i) { } 00403 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 00404 Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { 00405 while (*static_cast<Node*>(this)!=INVALID && 00406 !(*(gw->node_filter_map))[*this]) 00407 *(static_cast<Node*>(this))= 00408 ++(typename Graph::NodeIt(*(gw->graph), *this)); 00409 } 00410 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 00411 const Node& n) : 00412 Node(n), gw(&_gw) { } 00413 NodeIt& operator++() { 00414 *(static_cast<Node*>(this))= 00415 ++(typename Graph::NodeIt(*(gw->graph), *this)); 00416 while (*static_cast<Node*>(this)!=INVALID && 00417 !(*(gw->node_filter_map))[*this]) 00418 *(static_cast<Node*>(this))= 00419 ++(typename Graph::NodeIt(*(gw->graph), *this)); 00420 return *this; 00421 } 00422 }; 00423 typedef typename GraphWrapper<Graph>::Edge Edge; 00424 class OutEdgeIt : public Edge { 00425 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw; 00426 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>; 00427 public: 00428 OutEdgeIt() { } 00429 OutEdgeIt(Invalid i) : Edge(i) { } 00430 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 00431 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { 00432 while (*static_cast<Edge*>(this)!=INVALID && 00433 !(*(gw->edge_filter_map))[*this]) 00434 *(static_cast<Edge*>(this))= 00435 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00436 } 00437 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 00438 const Edge& e) : 00439 Edge(e), gw(&_gw) { } 00440 OutEdgeIt& operator++() { 00441 *(static_cast<Edge*>(this))= 00442 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00443 while (*static_cast<Edge*>(this)!=INVALID && 00444 !(*(gw->edge_filter_map))[*this]) 00445 *(static_cast<Edge*>(this))= 00446 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00447 return *this; 00448 } 00449 }; 00450 class InEdgeIt : public Edge { 00451 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw; 00452 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>; 00453 public: 00454 InEdgeIt() { } 00455 // InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { } 00456 InEdgeIt(Invalid i) : Edge(i) { } 00457 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 00458 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { 00459 while (*static_cast<Edge*>(this)!=INVALID && 00460 !(*(gw->edge_filter_map))[*this]) 00461 *(static_cast<Edge*>(this))= 00462 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00463 } 00464 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 00465 const Edge& e) : 00466 Edge(e), gw(&_gw) { } 00467 InEdgeIt& operator++() { 00468 *(static_cast<Edge*>(this))= 00469 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00470 while (*static_cast<Edge*>(this)!=INVALID && 00471 !(*(gw->edge_filter_map))[*this]) 00472 *(static_cast<Edge*>(this))= 00473 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00474 return *this; 00475 } 00476 }; 00477 class EdgeIt : public Edge { 00478 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw; 00479 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>; 00480 public: 00481 EdgeIt() { } 00482 EdgeIt(Invalid i) : Edge(i) { } 00483 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 00484 Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { 00485 while (*static_cast<Edge*>(this)!=INVALID && 00486 !(*(gw->edge_filter_map))[*this]) 00487 *(static_cast<Edge*>(this))= 00488 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00489 } 00490 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 00491 const Edge& e) : 00492 Edge(e), gw(&_gw) { } 00493 EdgeIt& operator++() { 00494 *(static_cast<Edge*>(this))= 00495 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00496 while (*static_cast<Edge*>(this)!=INVALID && 00497 !(*(gw->edge_filter_map))[*this]) 00498 *(static_cast<Edge*>(this))= 00499 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00500 return *this; 00501 } 00502 }; 00503 00504 NodeIt& first(NodeIt& i) const { 00505 i=NodeIt(*this); return i; 00506 } 00507 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 00508 i=OutEdgeIt(*this, p); return i; 00509 } 00510 InEdgeIt& first(InEdgeIt& i, const Node& p) const { 00511 i=InEdgeIt(*this, p); return i; 00512 } 00513 EdgeIt& first(EdgeIt& i) const { 00514 i=EdgeIt(*this); return i; 00515 } 00516 00520 void hide(const Node& n) const { node_filter_map->set(n, false); } 00521 00525 void hide(const Edge& e) const { edge_filter_map->set(e, false); } 00526 00530 void unHide(const Node& n) const { node_filter_map->set(n, true); } 00531 00535 void unHide(const Edge& e) const { edge_filter_map->set(e, true); } 00536 00538 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; } 00539 00541 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; } 00542 00545 int nodeNum() const { 00546 int i=0; 00547 for (NodeIt n(*this); n!=INVALID; ++n) ++i; 00548 return i; 00549 } 00550 00553 int edgeNum() const { 00554 int i=0; 00555 for (EdgeIt e(*this); e!=INVALID; ++e) ++i; 00556 return i; 00557 } 00558 00559 // KEEP_MAPS(Parent, SubGraphWrapper); 00560 }; 00561 00562 00563 00564 template<typename Graph> 00565 class UndirGraphWrapper : public GraphWrapper<Graph> { 00566 public: 00567 typedef GraphWrapper<Graph> Parent; 00568 protected: 00569 UndirGraphWrapper() : GraphWrapper<Graph>() { } 00570 00571 public: 00572 typedef typename GraphWrapper<Graph>::Node Node; 00573 typedef typename GraphWrapper<Graph>::NodeIt NodeIt; 00574 typedef typename GraphWrapper<Graph>::Edge Edge; 00575 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt; 00576 00577 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { } 00578 00579 class OutEdgeIt { 00580 friend class UndirGraphWrapper<Graph>; 00581 bool out_or_in; //true iff out 00582 typename Graph::OutEdgeIt out; 00583 typename Graph::InEdgeIt in; 00584 public: 00585 OutEdgeIt() { } 00586 OutEdgeIt(const Invalid& i) : Edge(i) { } 00587 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) { 00588 out_or_in=true; _G.graph->first(out, _n); 00589 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); } 00590 } 00591 operator Edge() const { 00592 if (out_or_in) return Edge(out); else return Edge(in); 00593 } 00594 }; 00595 00596 typedef OutEdgeIt InEdgeIt; 00597 00598 using GraphWrapper<Graph>::first; 00599 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 00600 i=OutEdgeIt(*this, p); return i; 00601 } 00602 00603 using GraphWrapper<Graph>::next; 00604 00605 OutEdgeIt& next(OutEdgeIt& e) const { 00606 if (e.out_or_in) { 00607 typename Graph::Node n=this->graph->tail(e.out); 00608 this->graph->next(e.out); 00609 if (!this->graph->valid(e.out)) { 00610 e.out_or_in=false; this->graph->first(e.in, n); } 00611 } else { 00612 this->graph->next(e.in); 00613 } 00614 return e; 00615 } 00616 00617 Node aNode(const OutEdgeIt& e) const { 00618 if (e.out_or_in) return this->graph->tail(e); else 00619 return this->graph->head(e); } 00620 Node bNode(const OutEdgeIt& e) const { 00621 if (e.out_or_in) return this->graph->head(e); else 00622 return this->graph->tail(e); } 00623 00624 // KEEP_MAPS(Parent, UndirGraphWrapper); 00625 00626 }; 00627 00628 // /// \brief An undirected graph template. 00629 // /// 00630 // ///\warning Graph wrappers are in even more experimental state than the other 00631 // ///parts of the lib. Use them at your own risk. 00632 // /// 00633 // /// An undirected graph template. 00634 // /// This class works as an undirected graph and a directed graph of 00635 // /// class \c Graph is used for the physical storage. 00636 // /// \ingroup graphs 00637 template<typename Graph> 00638 class UndirGraph : public UndirGraphWrapper<Graph> { 00639 typedef UndirGraphWrapper<Graph> Parent; 00640 protected: 00641 Graph gr; 00642 public: 00643 UndirGraph() : UndirGraphWrapper<Graph>() { 00644 Parent::setGraph(gr); 00645 } 00646 00647 // KEEP_MAPS(Parent, UndirGraph); 00648 }; 00649 00650 00651 00689 template<typename Graph, 00690 typename ForwardFilterMap, typename BackwardFilterMap> 00691 class SubBidirGraphWrapper : public GraphWrapper<Graph> { 00692 public: 00693 typedef GraphWrapper<Graph> Parent; 00694 protected: 00695 ForwardFilterMap* forward_filter; 00696 BackwardFilterMap* backward_filter; 00697 00698 SubBidirGraphWrapper() : GraphWrapper<Graph>() { } 00699 void setForwardFilterMap(ForwardFilterMap& _forward_filter) { 00700 forward_filter=&_forward_filter; 00701 } 00702 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) { 00703 backward_filter=&_backward_filter; 00704 } 00705 00706 public: 00707 00708 SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter, 00709 BackwardFilterMap& _backward_filter) : 00710 GraphWrapper<Graph>(_graph), 00711 forward_filter(&_forward_filter), backward_filter(&_backward_filter) { } 00712 SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph, 00713 ForwardFilterMap, BackwardFilterMap>& gw) : 00714 Parent(gw), 00715 forward_filter(gw.forward_filter), 00716 backward_filter(gw.backward_filter) { } 00717 00718 class Edge; 00719 class OutEdgeIt; 00720 friend class Edge; 00721 friend class OutEdgeIt; 00722 00723 template<typename T> class EdgeMap; 00724 00725 typedef typename GraphWrapper<Graph>::Node Node; 00726 00727 typedef typename Graph::Edge GraphEdge; 00732 class Edge : public Graph::Edge { 00733 friend class SubBidirGraphWrapper<Graph, 00734 ForwardFilterMap, BackwardFilterMap>; 00735 template<typename T> friend class EdgeMap; 00736 protected: 00737 bool backward; //true, iff backward 00738 public: 00739 Edge() { } 00743 Edge(const typename Graph::Edge& e, bool _backward/*=false*/) : 00744 Graph::Edge(e), backward(_backward) { } 00745 Edge(Invalid i) : Graph::Edge(i), backward(true) { } 00746 bool operator==(const Edge& v) const { 00747 return (this->backward==v.backward && 00748 static_cast<typename Graph::Edge>(*this)== 00749 static_cast<typename Graph::Edge>(v)); 00750 } 00751 bool operator!=(const Edge& v) const { 00752 return (this->backward!=v.backward || 00753 static_cast<typename Graph::Edge>(*this)!= 00754 static_cast<typename Graph::Edge>(v)); 00755 } 00756 }; 00757 00758 class OutEdgeIt : public Edge { 00759 friend class SubBidirGraphWrapper<Graph, 00760 ForwardFilterMap, BackwardFilterMap>; 00761 protected: 00762 const SubBidirGraphWrapper<Graph, 00763 ForwardFilterMap, BackwardFilterMap>* gw; 00764 public: 00765 OutEdgeIt() { } 00766 OutEdgeIt(Invalid i) : Edge(i) { } 00767 OutEdgeIt(const SubBidirGraphWrapper<Graph, 00768 ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 00769 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 00770 while (*static_cast<GraphEdge*>(this)!=INVALID && 00771 !(*(gw->forward_filter))[*this]) 00772 *(static_cast<GraphEdge*>(this))= 00773 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00774 if (*static_cast<GraphEdge*>(this)==INVALID) { 00775 *static_cast<Edge*>(this)= 00776 Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true); 00777 while (*static_cast<GraphEdge*>(this)!=INVALID && 00778 !(*(gw->backward_filter))[*this]) 00779 *(static_cast<GraphEdge*>(this))= 00780 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00781 } 00782 } 00783 OutEdgeIt(const SubBidirGraphWrapper<Graph, 00784 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 00785 Edge(e), gw(&_gw) { } 00786 OutEdgeIt& operator++() { 00787 if (!this->backward) { 00788 Node n=gw->tail(*this); 00789 *(static_cast<GraphEdge*>(this))= 00790 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00791 while (*static_cast<GraphEdge*>(this)!=INVALID && 00792 !(*(gw->forward_filter))[*this]) 00793 *(static_cast<GraphEdge*>(this))= 00794 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00795 if (*static_cast<GraphEdge*>(this)==INVALID) { 00796 *static_cast<Edge*>(this)= 00797 Edge(typename Graph::InEdgeIt(*(gw->graph), n), true); 00798 while (*static_cast<GraphEdge*>(this)!=INVALID && 00799 !(*(gw->backward_filter))[*this]) 00800 *(static_cast<GraphEdge*>(this))= 00801 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00802 } 00803 } else { 00804 *(static_cast<GraphEdge*>(this))= 00805 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00806 while (*static_cast<GraphEdge*>(this)!=INVALID && 00807 !(*(gw->backward_filter))[*this]) 00808 *(static_cast<GraphEdge*>(this))= 00809 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00810 } 00811 return *this; 00812 } 00813 }; 00814 00815 class InEdgeIt : public Edge { 00816 friend class SubBidirGraphWrapper<Graph, 00817 ForwardFilterMap, BackwardFilterMap>; 00818 protected: 00819 const SubBidirGraphWrapper<Graph, 00820 ForwardFilterMap, BackwardFilterMap>* gw; 00821 public: 00822 InEdgeIt() { } 00823 InEdgeIt(Invalid i) : Edge(i) { } 00824 InEdgeIt(const SubBidirGraphWrapper<Graph, 00825 ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 00826 Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 00827 while (*static_cast<GraphEdge*>(this)!=INVALID && 00828 !(*(gw->forward_filter))[*this]) 00829 *(static_cast<GraphEdge*>(this))= 00830 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00831 if (*static_cast<GraphEdge*>(this)==INVALID) { 00832 *static_cast<Edge*>(this)= 00833 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true); 00834 while (*static_cast<GraphEdge*>(this)!=INVALID && 00835 !(*(gw->backward_filter))[*this]) 00836 *(static_cast<GraphEdge*>(this))= 00837 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00838 } 00839 } 00840 InEdgeIt(const SubBidirGraphWrapper<Graph, 00841 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 00842 Edge(e), gw(&_gw) { } 00843 InEdgeIt& operator++() { 00844 if (!this->backward) { 00845 Node n=gw->tail(*this); 00846 *(static_cast<GraphEdge*>(this))= 00847 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00848 while (*static_cast<GraphEdge*>(this)!=INVALID && 00849 !(*(gw->forward_filter))[*this]) 00850 *(static_cast<GraphEdge*>(this))= 00851 ++(typename Graph::InEdgeIt(*(gw->graph), *this)); 00852 if (*static_cast<GraphEdge*>(this)==INVALID) { 00853 *static_cast<Edge*>(this)= 00854 Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true); 00855 while (*static_cast<GraphEdge*>(this)!=INVALID && 00856 !(*(gw->backward_filter))[*this]) 00857 *(static_cast<GraphEdge*>(this))= 00858 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00859 } 00860 } else { 00861 *(static_cast<GraphEdge*>(this))= 00862 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00863 while (*static_cast<GraphEdge*>(this)!=INVALID && 00864 !(*(gw->backward_filter))[*this]) 00865 *(static_cast<GraphEdge*>(this))= 00866 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 00867 } 00868 return *this; 00869 } 00870 }; 00871 00872 class EdgeIt : public Edge { 00873 friend class SubBidirGraphWrapper<Graph, 00874 ForwardFilterMap, BackwardFilterMap>; 00875 protected: 00876 const SubBidirGraphWrapper<Graph, 00877 ForwardFilterMap, BackwardFilterMap>* gw; 00878 public: 00879 EdgeIt() { } 00880 EdgeIt(Invalid i) : Edge(i) { } 00881 EdgeIt(const SubBidirGraphWrapper<Graph, 00882 ForwardFilterMap, BackwardFilterMap>& _gw) : 00883 Edge(typename Graph::EdgeIt(*(_gw.graph)), false), gw(&_gw) { 00884 while (*static_cast<GraphEdge*>(this)!=INVALID && 00885 !(*(gw->forward_filter))[*this]) 00886 *(static_cast<GraphEdge*>(this))= 00887 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00888 if (*static_cast<GraphEdge*>(this)==INVALID) { 00889 *static_cast<Edge*>(this)= 00890 Edge(typename Graph::EdgeIt(*(_gw.graph)), true); 00891 while (*static_cast<GraphEdge*>(this)!=INVALID && 00892 !(*(gw->backward_filter))[*this]) 00893 *(static_cast<GraphEdge*>(this))= 00894 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00895 } 00896 } 00897 EdgeIt(const SubBidirGraphWrapper<Graph, 00898 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 00899 Edge(e), gw(&_gw) { } 00900 EdgeIt& operator++() { 00901 if (!this->backward) { 00902 *(static_cast<GraphEdge*>(this))= 00903 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00904 while (*static_cast<GraphEdge*>(this)!=INVALID && 00905 !(*(gw->forward_filter))[*this]) 00906 *(static_cast<GraphEdge*>(this))= 00907 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00908 if (*static_cast<GraphEdge*>(this)==INVALID) { 00909 *static_cast<Edge*>(this)= 00910 Edge(typename Graph::EdgeIt(*(gw->graph)), true); 00911 while (*static_cast<GraphEdge*>(this)!=INVALID && 00912 !(*(gw->backward_filter))[*this]) 00913 *(static_cast<GraphEdge*>(this))= 00914 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00915 } 00916 } else { 00917 *(static_cast<GraphEdge*>(this))= 00918 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00919 while (*static_cast<GraphEdge*>(this)!=INVALID && 00920 !(*(gw->backward_filter))[*this]) 00921 *(static_cast<GraphEdge*>(this))= 00922 ++(typename Graph::EdgeIt(*(gw->graph), *this)); 00923 } 00924 return *this; 00925 } 00926 }; 00927 00928 using GraphWrapper<Graph>::first; 00929 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 00930 i=OutEdgeIt(*this, p); return i; 00931 } 00932 InEdgeIt& first(InEdgeIt& i, const Node& p) const { 00933 i=InEdgeIt(*this, p); return i; 00934 } 00935 EdgeIt& first(EdgeIt& i) const { 00936 i=EdgeIt(*this); return i; 00937 } 00938 00939 00940 Node tail(Edge e) const { 00941 return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); } 00942 Node head(Edge e) const { 00943 return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); } 00944 00946 Edge opposite(const Edge& e) const { 00947 Edge f=e; 00948 f.backward=!f.backward; 00949 return f; 00950 } 00951 00954 int edgeNum() const { 00955 int i=0; 00956 for (EdgeIt e(*this); e!=INVALID; ++e) ++i; 00957 return i; 00958 } 00959 00960 bool forward(const Edge& e) const { return !e.backward; } 00961 bool backward(const Edge& e) const { return e.backward; } 00962 00963 00964 template <typename T> 00968 class EdgeMap { 00969 template <typename TT> friend class EdgeMap; 00970 typename Graph::template EdgeMap<T> forward_map, backward_map; 00971 public: 00972 typedef T ValueType; 00973 typedef Edge KeyType; 00974 00975 EdgeMap(const SubBidirGraphWrapper<Graph, 00976 ForwardFilterMap, BackwardFilterMap>& g) : 00977 forward_map(*(g.graph)), backward_map(*(g.graph)) { } 00978 00979 EdgeMap(const SubBidirGraphWrapper<Graph, 00980 ForwardFilterMap, BackwardFilterMap>& g, T a) : 00981 forward_map(*(g.graph), a), backward_map(*(g.graph), a) { } 00982 00983 template <typename TT> 00984 EdgeMap(const EdgeMap<TT>& copy) 00985 : forward_map(copy.forward_map), backward_map(copy.backward_map) {} 00986 00987 template <typename TT> 00988 EdgeMap& operator=(const EdgeMap<TT>& copy) { 00989 forward_map = copy.forward_map; 00990 backward_map = copy.backward_map; 00991 return *this; 00992 } 00993 00994 void set(Edge e, T a) { 00995 if (!e.backward) 00996 forward_map.set(e, a); 00997 else 00998 backward_map.set(e, a); 00999 } 01000 01001 typename Graph::template EdgeMap<T>::ConstReferenceType 01002 operator[](Edge e) const { 01003 if (!e.backward) 01004 return forward_map[e]; 01005 else 01006 return backward_map[e]; 01007 } 01008 01009 typename Graph::template EdgeMap<T>::ReferenceType 01010 operator[](Edge e) { 01011 if (!e.backward) 01012 return forward_map[e]; 01013 else 01014 return backward_map[e]; 01015 } 01016 01017 void update() { 01018 forward_map.update(); 01019 backward_map.update(); 01020 } 01021 }; 01022 01023 01024 // KEEP_NODE_MAP(Parent, SubBidirGraphWrapper); 01025 01026 }; 01027 01028 01038 template<typename Graph> 01039 class BidirGraphWrapper : 01040 public SubBidirGraphWrapper< 01041 Graph, 01042 ConstMap<typename Graph::Edge, bool>, 01043 ConstMap<typename Graph::Edge, bool> > { 01044 public: 01045 typedef SubBidirGraphWrapper< 01046 Graph, 01047 ConstMap<typename Graph::Edge, bool>, 01048 ConstMap<typename Graph::Edge, bool> > Parent; 01049 protected: 01050 ConstMap<typename Graph::Edge, bool> cm; 01051 01052 BidirGraphWrapper() : Parent(), cm(true) { 01053 Parent::setForwardFilterMap(cm); 01054 Parent::setBackwardFilterMap(cm); 01055 } 01056 public: 01057 BidirGraphWrapper(Graph& _graph) : Parent() { 01058 Parent::setGraph(_graph); 01059 Parent::setForwardFilterMap(cm); 01060 Parent::setBackwardFilterMap(cm); 01061 } 01062 01063 int edgeNum() const { 01064 return 2*this->graph->edgeNum(); 01065 } 01066 // KEEP_MAPS(Parent, BidirGraphWrapper); 01067 }; 01068 01069 01082 template<typename Graph> 01083 class BidirGraph : public BidirGraphWrapper<Graph> { 01084 public: 01085 typedef UndirGraphWrapper<Graph> Parent; 01086 protected: 01087 Graph gr; 01088 public: 01089 BidirGraph() : BidirGraphWrapper<Graph>() { 01090 Parent::setGraph(gr); 01091 } 01092 // KEEP_MAPS(Parent, BidirGraph); 01093 }; 01094 01095 01096 01097 template<typename Graph, typename Number, 01098 typename CapacityMap, typename FlowMap> 01099 class ResForwardFilter { 01100 // const Graph* graph; 01101 const CapacityMap* capacity; 01102 const FlowMap* flow; 01103 public: 01104 ResForwardFilter(/*const Graph& _graph, */ 01105 const CapacityMap& _capacity, const FlowMap& _flow) : 01106 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { } 01107 ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { } 01108 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; } 01109 void setFlow(const FlowMap& _flow) { flow=&_flow; } 01110 bool operator[](const typename Graph::Edge& e) const { 01111 return (Number((*flow)[e]) < Number((*capacity)[e])); 01112 } 01113 }; 01114 01115 template<typename Graph, typename Number, 01116 typename CapacityMap, typename FlowMap> 01117 class ResBackwardFilter { 01118 const CapacityMap* capacity; 01119 const FlowMap* flow; 01120 public: 01121 ResBackwardFilter(/*const Graph& _graph,*/ 01122 const CapacityMap& _capacity, const FlowMap& _flow) : 01123 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { } 01124 ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { } 01125 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; } 01126 void setFlow(const FlowMap& _flow) { flow=&_flow; } 01127 bool operator[](const typename Graph::Edge& e) const { 01128 return (Number(0) < Number((*flow)[e])); 01129 } 01130 }; 01131 01132 01134 01139 template<typename Graph, typename Number, 01140 typename CapacityMap, typename FlowMap> 01141 class ResGraphWrapper : 01142 public SubBidirGraphWrapper< 01143 Graph, 01144 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>, 01145 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > { 01146 public: 01147 typedef SubBidirGraphWrapper< 01148 Graph, 01149 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>, 01150 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent; 01151 protected: 01152 const CapacityMap* capacity; 01153 FlowMap* flow; 01154 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter; 01155 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter; 01156 ResGraphWrapper() : Parent(), 01157 capacity(0), flow(0) { } 01158 void setCapacityMap(const CapacityMap& _capacity) { 01159 capacity=&_capacity; 01160 forward_filter.setCapacity(_capacity); 01161 backward_filter.setCapacity(_capacity); 01162 } 01163 void setFlowMap(FlowMap& _flow) { 01164 flow=&_flow; 01165 forward_filter.setFlow(_flow); 01166 backward_filter.setFlow(_flow); 01167 } 01168 public: 01169 ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 01170 FlowMap& _flow) : 01171 Parent(), capacity(&_capacity), flow(&_flow), 01172 forward_filter(/*_graph,*/ _capacity, _flow), 01173 backward_filter(/*_graph,*/ _capacity, _flow) { 01174 Parent::setGraph(_graph); 01175 Parent::setForwardFilterMap(forward_filter); 01176 Parent::setBackwardFilterMap(backward_filter); 01177 } 01178 01179 typedef typename Parent::Edge Edge; 01180 01181 void augment(const Edge& e, Number a) const { 01182 if (Parent::forward(e)) 01183 flow->set(e, (*flow)[e]+a); 01184 else 01185 flow->set(e, (*flow)[e]-a); 01186 } 01187 01192 class ResCap { 01193 protected: 01194 const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph; 01195 public: 01196 typedef Number ValueType; 01197 typedef Edge KeyType; 01198 ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& 01199 _res_graph) : res_graph(&_res_graph) { } 01200 Number operator[](const Edge& e) const { 01201 if (res_graph->forward(e)) 01202 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 01203 else 01204 return (*(res_graph->flow))[e]; 01205 } 01206 }; 01207 01208 // KEEP_MAPS(Parent, ResGraphWrapper); 01209 }; 01210 01211 01213 01226 template<typename Graph, typename FirstOutEdgesMap> 01227 class ErasingFirstGraphWrapper : public GraphWrapper<Graph> { 01228 public: 01229 typedef GraphWrapper<Graph> Parent; 01230 protected: 01231 FirstOutEdgesMap* first_out_edges; 01232 public: 01233 ErasingFirstGraphWrapper(Graph& _graph, 01234 FirstOutEdgesMap& _first_out_edges) : 01235 GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { } 01236 01237 typedef typename GraphWrapper<Graph>::Node Node; 01238 typedef typename GraphWrapper<Graph>::Edge Edge; 01239 class OutEdgeIt : public Edge { 01240 friend class GraphWrapper<Graph>; 01241 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>; 01242 const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw; 01243 public: 01244 OutEdgeIt() { } 01245 OutEdgeIt(Invalid i) : Edge(i) { } 01246 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 01247 const Node& n) : 01248 Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { } 01249 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 01250 const Edge& e) : 01251 Edge(e), gw(&_gw) { } 01252 OutEdgeIt& operator++() { 01253 *(static_cast<Edge*>(this))= 01254 ++(typename Graph::OutEdgeIt(*(gw->graph), *this)); 01255 return *this; 01256 } 01257 }; 01258 01259 using GraphWrapper<Graph>::first; 01260 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 01261 i=OutEdgeIt(*this, p); return i; 01262 } 01263 void erase(const Edge& e) const { 01264 Node n=tail(e); 01265 typename Graph::OutEdgeIt f(*Parent::graph, n); 01266 ++f; 01267 first_out_edges->set(n, f); 01268 } 01269 01270 // KEEP_MAPS(Parent, ErasingFirstGraphWrapper); 01271 }; 01272 01274 01275 } //namespace lemon 01276 01277 #endif //LEMON_GRAPH_WRAPPER_H 01278

Generated on Thu Sep 30 12:18:33 2004 for LEMON by doxygen 1.3.8