src/lemon/graph_wrapper.h
changeset 978 175cf8c3a994
parent 933 1b7c88fbb950
child 986 e997802b855c
equal deleted inserted replaced
4:2dc4ea021230 5:d4ed5d23fca9
   106   /// considered which differs from the wrapped graph only in some of its 
   106   /// considered which differs from the wrapped graph only in some of its 
   107   /// functions or types, then it can be derived from GraphWrapper, and only the 
   107   /// functions or types, then it can be derived from GraphWrapper, and only the 
   108   /// differences should be implemented.
   108   /// differences should be implemented.
   109   ///
   109   ///
   110   ///\author Marton Makai 
   110   ///\author Marton Makai 
   111   template<typename Graph>
   111   template<typename _Graph>
   112   class GraphWrapper {
   112   class GraphWrapperBase {
       
   113   public:
       
   114     typedef _Graph Graph;
       
   115     /// \todo Is it needed?
       
   116     typedef Graph BaseGraph;
       
   117     typedef Graph ParentGraph;
       
   118 
   113   protected:
   119   protected:
   114     Graph* graph;
   120     Graph* graph;
   115     GraphWrapper() : graph(0) { }
   121     GraphWrapperBase() : graph(0) { }
   116     void setGraph(Graph& _graph) { graph=&_graph; }
   122     void setGraph(Graph& _graph) { graph=&_graph; }
   117 
   123 
   118   public:
   124   public:
   119     typedef Graph BaseGraph;
   125     GraphWrapperBase(Graph& _graph) : graph(&_graph) { }
   120     typedef Graph ParentGraph;
   126     GraphWrapperBase(const GraphWrapperBase<_Graph>& gw) : graph(gw.graph) { }
   121 
       
   122     GraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   123     GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { }
       
   124  
   127  
   125     typedef typename Graph::Node Node;
   128     typedef typename Graph::Node Node;
   126     class NodeIt : public Node { 
       
   127       const GraphWrapper<Graph>* gw;
       
   128       friend class GraphWrapper<Graph>;
       
   129      public:
       
   130       NodeIt() { }
       
   131       NodeIt(Invalid i) : Node(i) { }
       
   132       NodeIt(const GraphWrapper<Graph>& _gw) : 
       
   133 	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
       
   134       NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
       
   135 	Node(n), gw(&_gw) { }
       
   136       NodeIt& operator++() { 
       
   137 	*(static_cast<Node*>(this))=
       
   138 	  ++(typename Graph::NodeIt(*(gw->graph), *this));
       
   139 	return *this; 
       
   140       }
       
   141     };
       
   142     typedef typename Graph::Edge Edge;
   129     typedef typename Graph::Edge Edge;
   143     class OutEdgeIt : public Edge { 
       
   144       const GraphWrapper<Graph>* gw;
       
   145       friend class GraphWrapper<Graph>;
       
   146      public:
       
   147       OutEdgeIt() { }
       
   148       OutEdgeIt(Invalid i) : Edge(i) { }
       
   149       OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
       
   150 	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
       
   151       OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
       
   152 	Edge(e), gw(&_gw) { }
       
   153       OutEdgeIt& operator++() { 
       
   154 	*(static_cast<Edge*>(this))=
       
   155 	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
       
   156 	return *this; 
       
   157       }
       
   158     };
       
   159     class InEdgeIt : public Edge { 
       
   160       const GraphWrapper<Graph>* gw;
       
   161       friend class GraphWrapper<Graph>;
       
   162      public:
       
   163       InEdgeIt() { }
       
   164       InEdgeIt(Invalid i) : Edge(i) { }
       
   165       InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
       
   166 	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
       
   167       InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
       
   168 	Edge(e), gw(&_gw) { }
       
   169       InEdgeIt& operator++() { 
       
   170 	*(static_cast<Edge*>(this))=
       
   171 	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
       
   172 	return *this; 
       
   173       }
       
   174     };
       
   175     class EdgeIt : public Edge { 
       
   176       const GraphWrapper<Graph>* gw;
       
   177       friend class GraphWrapper<Graph>;
       
   178      public:
       
   179       EdgeIt() { }
       
   180       EdgeIt(Invalid i) : Edge(i) { }
       
   181       EdgeIt(const GraphWrapper<Graph>& _gw) : 
       
   182 	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
       
   183       EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
       
   184 	Edge(e), gw(&_gw) { }
       
   185       EdgeIt& operator++() { 
       
   186 	*(static_cast<Edge*>(this))=
       
   187 	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
       
   188 	return *this; 
       
   189       }
       
   190     };
       
   191    
   130    
   192     NodeIt& first(NodeIt& i) const { 
   131     void first(Node& i) const { graph->first(i); }
   193       i=NodeIt(*this); return i;
   132     void first(Edge& i) const { graph->first(i); }
   194     }
   133     void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
   195     OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
   134     void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
   196       i=OutEdgeIt(*this, p); return i;
   135 //     NodeIt& first(NodeIt& i) const { 
   197     }
   136 //       i=NodeIt(*this); return i;
   198     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
   137 //     }
   199       i=InEdgeIt(*this, p); return i;
   138 //     OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
   200     }
   139 //       i=OutEdgeIt(*this, p); return i;
   201     EdgeIt& first(EdgeIt& i) const { 
   140 //     }
   202       i=EdgeIt(*this); return i;
   141 //     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
   203     }
   142 //       i=InEdgeIt(*this, p); return i;
   204 
   143 //     }
   205     Node tail(const Edge& e) const { 
   144 //     EdgeIt& first(EdgeIt& i) const { 
   206       return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
   145 //       i=EdgeIt(*this); return i;
   207     Node head(const Edge& e) const { 
   146 //     }
   208       return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
   147 
       
   148     void next(Node& i) const { graph->next(i); }
       
   149     void next(Edge& i) const { graph->next(i); }
       
   150     void nextIn(Edge& i) const { graph->nextIn(i); }
       
   151     void nextOut(Edge& i) const { graph->nextOut(i); }
       
   152 
       
   153     Node tail(const Edge& e) const { return graph->tail(e); }
       
   154     Node head(const Edge& e) const { return graph->head(e); }
       
   155 //     Node tail(const Edge& e) const { 
       
   156 //       return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
       
   157 //     Node head(const Edge& e) const { 
       
   158 //       return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
   209 
   159 
   210     int nodeNum() const { return graph->nodeNum(); }
   160     int nodeNum() const { return graph->nodeNum(); }
   211     int edgeNum() const { return graph->edgeNum(); }
   161     int edgeNum() const { return graph->edgeNum(); }
   212   
   162   
   213     Node addNode() const { return Node(graph->addNode()); }
   163     Node addNode() const { return Node(graph->addNode()); }
   225     int id(const Node& v) const { return graph->id(v); }
   175     int id(const Node& v) const { return graph->id(v); }
   226     int id(const Edge& e) const { return graph->id(e); }
   176     int id(const Edge& e) const { return graph->id(e); }
   227     
   177     
   228     Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
   178     Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
   229 
   179 
   230 
   180     template <typename _Value>
   231     IMPORT_NODE_MAP(Graph, *(gw.graph), GraphWrapper, gw);    
   181     class NodeMap : public _Graph::template NodeMap<_Value> {
   232     IMPORT_EDGE_MAP(Graph, *(gw.graph), GraphWrapper, gw);
   182     public:
   233     
   183       typedef typename _Graph::template NodeMap<_Value> Parent;
       
   184       NodeMap(const GraphWrapperBase<_Graph>& gw) : Parent(*gw.graph) { }
       
   185       NodeMap(const GraphWrapperBase<_Graph>& gw, const _Value& value)
       
   186       : Parent(*gw.graph, value) { }
       
   187     };
       
   188 
       
   189     template <typename _Value>
       
   190     class EdgeMap : public _Graph::template EdgeMap<_Value> {
       
   191     public:
       
   192       typedef typename _Graph::template EdgeMap<_Value> Parent;
       
   193       EdgeMap(const GraphWrapperBase<_Graph>& gw) : Parent(*gw.graph) { }
       
   194       EdgeMap(const GraphWrapperBase<_Graph>& gw, const _Value& value)
       
   195       : Parent(*gw.graph, value) { }
       
   196     };
   234 
   197 
   235   };
   198   };
   236 
   199 
   237 
   200   template <typename _Graph>
       
   201   class GraphWrapper :
       
   202     public IterableGraphExtender<GraphWrapperBase<_Graph> > { 
       
   203   public:
       
   204     typedef _Graph Graph;
       
   205     typedef IterableGraphExtender<GraphWrapperBase<_Graph> > Parent;
       
   206   protected:
       
   207     GraphWrapper() : Parent() { }
       
   208 
       
   209   public:
       
   210     GraphWrapper(Graph& _graph) { setGraph(_graph); }
       
   211   };
   238 
   212 
   239   /// A graph wrapper which reverses the orientation of the edges.
   213   /// A graph wrapper which reverses the orientation of the edges.
   240 
   214 
   241   ///\warning Graph wrappers are in even more experimental state than the other
   215   ///\warning Graph wrappers are in even more experimental state than the other
   242   ///parts of the lib. Use them at you own risk.
   216   ///parts of the lib. Use them at you own risk.
  1101 	}
  1075 	}
  1102 	return *this;
  1076 	return *this;
  1103       }
  1077       }
  1104     };
  1078     };
  1105 
  1079 
  1106     using GraphWrapper<Graph>::first;
  1080 //     using GraphWrapper<Graph>::first;
  1107     OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
  1081 //     OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
  1108       i=OutEdgeIt(*this, p); return i;
  1082 //       i=OutEdgeIt(*this, p); return i;
  1109     }
  1083 //     }
  1110     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
  1084 //     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
  1111       i=InEdgeIt(*this, p); return i;
  1085 //       i=InEdgeIt(*this, p); return i;
  1112     }
  1086 //     }
  1113     EdgeIt& first(EdgeIt& i) const { 
  1087 //     EdgeIt& first(EdgeIt& i) const { 
  1114       i=EdgeIt(*this); return i;
  1088 //       i=EdgeIt(*this); return i;
  1115     }
  1089 //     }
  1116   
  1090   
  1117 
  1091 
  1118     Node tail(Edge e) const { 
  1092     Node tail(Edge e) const { 
  1119       return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
  1093       return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
  1120     Node head(Edge e) const { 
  1094     Node head(Edge e) const { 
  1432 	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
  1406 	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
  1433 	return *this; 
  1407 	return *this; 
  1434       }
  1408       }
  1435     };
  1409     };
  1436 
  1410 
  1437     using GraphWrapper<Graph>::first;
  1411 //     using GraphWrapper<Graph>::first;
  1438     OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
  1412 //     OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
  1439       i=OutEdgeIt(*this, p); return i;
  1413 //       i=OutEdgeIt(*this, p); return i;
  1440     }
  1414 //     }
  1441     void erase(const Edge& e) const {
  1415     void erase(const Edge& e) const {
  1442       Node n=tail(e);
  1416       Node n=tail(e);
  1443       typename Graph::OutEdgeIt f(*Parent::graph, n);
  1417       typename Graph::OutEdgeIt f(*Parent::graph, n);
  1444       ++f;
  1418       ++f;
  1445       first_out_edges->set(n, f);
  1419       first_out_edges->set(n, f);