src/work/marci/graph_wrapper.h
changeset 164 970b265696b0
parent 156 a34e5a909e97
child 168 27fbd1559fb7
equal deleted inserted replaced
4:681d0eb95155 5:eb45adef7d8d
    60     void clear() const { graph->clear(); }
    60     void clear() const { graph->clear(); }
    61     
    61     
    62     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
    62     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
    63     public:
    63     public:
    64       NodeMap(const TrivGraphWrapper<Graph>& _G) : 
    64       NodeMap(const TrivGraphWrapper<Graph>& _G) : 
    65 	Graph::NodeMap<T>(*(_G.G)) { }
    65 	Graph::NodeMap<T>(_G.getGraph()) { }
    66       NodeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    66       NodeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    67 	Graph::NodeMap<T>(*(_G.G), a) { }
    67 	Graph::NodeMap<T>(_G.getGraph(), a) { }
    68     };
    68     };
    69     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
    69     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
    70     public:
    70     public:
    71       EdgeMap(const TrivGraphWrapper<Graph>& _G) : 
    71       EdgeMap(const TrivGraphWrapper<Graph>& _G) : 
    72 	Graph::EdgeMap<T>(*(_G.G)) { }
    72 	Graph::EdgeMap<T>(_G.getGraph()) { }
    73       EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    73       EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    74 	Graph::EdgeMap<T>(*(_G.G), a) { }
    74 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
    75     };
    75     };
    76     
    76     
    77     void setGraph(Graph& _graph) { graph = &_graph; }
    77     void setGraph(Graph& _graph) { graph = &_graph; }
    78     Graph& getGraph() const { return (*graph); }
    78     Graph& getGraph() const { return (*graph); }
    79   
    79   
   138     void clear() const { graph->clear(); }
   138     void clear() const { graph->clear(); }
   139 
   139 
   140     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
   140     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
   141     public:
   141     public:
   142       NodeMap(const RevGraphWrapper<Graph>& _G) : 
   142       NodeMap(const RevGraphWrapper<Graph>& _G) : 
   143 	Graph::NodeMap<T>(*(_G.G)) { }
   143 	Graph::NodeMap<T>(_G.getGraph()) { }
   144       NodeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   144       NodeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   145 	Graph::NodeMap<T>(*(_G.G), a) { }
   145 	Graph::NodeMap<T>(_G.getGraph(), a) { }
   146     };
   146     };
   147     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
   147     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
   148     public:
   148     public:
   149       EdgeMap(const RevGraphWrapper<Graph>& _G) : 
   149       EdgeMap(const RevGraphWrapper<Graph>& _G) : 
   150 	Graph::EdgeMap<T>(*(_G.G)) { }
   150 	Graph::EdgeMap<T>(_G.getGraph()) { }
   151       EdgeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   151       EdgeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   152 	Graph::EdgeMap<T>(*(_G.G), a) { }
   152 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
   153     };
   153     };
   154 
   154 
   155     void setGraph(Graph& _graph) { graph = &_graph; }
   155     void setGraph(Graph& _graph) { graph = &_graph; }
   156     Graph& getGraph() const { return (*graph); }
   156     Graph& getGraph() const { return (*graph); }
   157 
   157 
   158     //RevGraphWrapper() : graph(0) { }
   158     //RevGraphWrapper() : graph(0) { }
   159     RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
   159     RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
   160   };
   160   };
       
   161 
       
   162 
       
   163   template<typename Graph>
       
   164   class UndirGraphWrapper {
       
   165     Graph* graph;
       
   166   
       
   167   public:
       
   168     typedef Graph BaseGraph;
       
   169 
       
   170     typedef typename Graph::NodeIt NodeIt;
       
   171     typedef typename Graph::EachNodeIt EachNodeIt;
       
   172 
       
   173     //typedef typename Graph::EdgeIt EdgeIt;
       
   174     //typedef typename Graph::OutEdgeIt OutEdgeIt;
       
   175     //typedef typename Graph::InEdgeIt InEdgeIt;
       
   176     //typedef typename Graph::SymEdgeIt SymEdgeIt;
       
   177     //typedef typename Graph::EachEdgeIt EachEdgeIt;
       
   178 
       
   179     //private:
       
   180     typedef typename Graph::EdgeIt GraphEdgeIt;
       
   181     typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
       
   182     typedef typename Graph::InEdgeIt GraphInEdgeIt;
       
   183     //public:
       
   184 
       
   185     class EdgeIt {
       
   186       friend class UndirGraphWrapper<Graph>;
       
   187       bool out_or_in; //true iff out
       
   188       GraphOutEdgeIt out;
       
   189       GraphInEdgeIt in;
       
   190     public:
       
   191       EdgeIt() : out_or_in(true), out(), in() { }
       
   192       operator GraphEdgeIt() const {
       
   193 	if (out_or_in) return(out); else return(in);
       
   194       }
       
   195     };
       
   196 
       
   197     class OutEdgeIt : public EdgeIt {
       
   198       friend class UndirGraphWrapper<Graph>;
       
   199       //bool out_or_in; //true iff out
       
   200       //GraphOutEdgeIt out;
       
   201       //GraphInEdgeIt in;
       
   202     public:
       
   203       OutEdgeIt() : EdgeIt() { }
       
   204       OutEdgeIt(const UndirGraphWrapper& _G, const NodeIt& n) : EdgeIt() { 
       
   205 	_G.graph->getFirst(out, n);
       
   206 	if (!(_G.graph->valid(out))) {
       
   207 	  out_or_in=false;
       
   208 	  _G.graph->getFirst(in, n);
       
   209 	}
       
   210       }
       
   211     };
       
   212 
       
   213     OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const {
       
   214       e.out_or_in=true;
       
   215       graph->getFirst(e.out, n);
       
   216       if (!(graph->valid(e.out))) {
       
   217 	e.out_or_in=false;
       
   218 	graph->getFirst(e.in, n);
       
   219       }
       
   220       return e;
       
   221     }
       
   222 
       
   223     OutEdgeIt& next(OutEdgeIt& e) const {
       
   224       if (e.out_or_in) {
       
   225 	NodeIt n=graph->tail(e.out);
       
   226 	graph->next(e.out);
       
   227 	if (!graph->valid(e.out)) {
       
   228 	  e.out_or_in=false;
       
   229 	  graph->getFirst(e.in, n);
       
   230 	}
       
   231       } else {
       
   232 	graph->next(e.in);
       
   233       }
       
   234       return e;
       
   235     }
       
   236 
       
   237     NodeIt aNode(const OutEdgeIt& e) const { 
       
   238       if (e.out_or_in) return graph->tail(e); else return graph->head(e); }
       
   239     NodeIt bNode(const OutEdgeIt& e) const { 
       
   240       if (e.out_or_in) return graph->head(e); else return graph->tail(e); }
       
   241 
       
   242     typedef OutEdgeIt InEdgeIt; 
       
   243 
       
   244     template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
       
   245 //     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
       
   246 //       return graph->getFirst(i, p); }
       
   247     
       
   248     template<typename I> I getNext(const I& i) const { 
       
   249       return graph->getNext(i); }
       
   250     template<typename I> I& next(I &i) const { return graph->next(i); }    
       
   251 
       
   252     template< typename It > It first() const { 
       
   253       It e; getFirst(e); return e; }
       
   254 
       
   255     template< typename It > It first(const NodeIt& v) const { 
       
   256       It e; getFirst(e, v); return e; }
       
   257 
       
   258     NodeIt head(const EdgeIt& e) const { return graph->head(e); }
       
   259     NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
       
   260 
       
   261     template<typename I> bool valid(const I& i) const 
       
   262       { return graph->valid(i); }
       
   263   
       
   264     //template<typename I> void setInvalid(const I &i);
       
   265     //{ return graph->setInvalid(i); }
       
   266 
       
   267     int nodeNum() const { return graph->nodeNum(); }
       
   268     int edgeNum() const { return graph->edgeNum(); }
       
   269   
       
   270 //     template<typename I> NodeIt aNode(const I& e) const { 
       
   271 //       return graph->aNode(e); }
       
   272 //     template<typename I> NodeIt bNode(const I& e) const { 
       
   273 //       return graph->bNode(e); }
       
   274   
       
   275     NodeIt addNode() const { return graph->addNode(); }
       
   276     EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { 
       
   277       return graph->addEdge(tail, head); }
       
   278   
       
   279     template<typename I> void erase(const I& i) const { graph->erase(i); }
       
   280   
       
   281     void clear() const { graph->clear(); }
       
   282     
       
   283     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
       
   284     public:
       
   285       NodeMap(const UndirGraphWrapper<Graph>& _G) : 
       
   286 	Graph::NodeMap<T>(_G.getGraph()) { }
       
   287       NodeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
       
   288 	Graph::NodeMap<T>(_G.getGraph(), a) { }
       
   289     };
       
   290     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
       
   291     public:
       
   292       EdgeMap(const UndirGraphWrapper<Graph>& _G) : 
       
   293 	Graph::EdgeMap<T>(_G.getGraph()) { }
       
   294       EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
       
   295 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
       
   296     };
       
   297     
       
   298     void setGraph(Graph& _graph) { graph = &_graph; }
       
   299     Graph& getGraph() const { return (*graph); }
       
   300   
       
   301     //TrivGraphWrapper() : graph(0) { }
       
   302     UndirGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   303   };
       
   304 
   161 
   305 
   162 
   306 
   163 //   template<typename Graph>
   307 //   template<typename Graph>
   164 //   class SymGraphWrapper
   308 //   class SymGraphWrapper
   165 //   {
   309 //   {
   245     ResGraphWrapper(const Graph& _G, FlowMap& _flow, 
   389     ResGraphWrapper(const Graph& _G, FlowMap& _flow, 
   246 	     const CapacityMap& _capacity) : 
   390 	     const CapacityMap& _capacity) : 
   247       G(&_G), flow(&_flow), capacity(&_capacity) { }
   391       G(&_G), flow(&_flow), capacity(&_capacity) { }
   248 //     ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : 
   392 //     ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : 
   249 //       G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
   393 //       G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
       
   394     void setGraph(Graph& _graph) { graph = &_graph; }
       
   395     Graph& getGraph() const { return (*graph); }
   250     class EdgeIt; 
   396     class EdgeIt; 
   251     class OutEdgeIt; 
   397     class OutEdgeIt; 
   252     friend class EdgeIt; 
   398     friend class EdgeIt; 
   253     friend class OutEdgeIt; 
   399     friend class OutEdgeIt; 
   254 
   400 
   497       return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
   643       return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
   498 
   644 
   499     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
   645     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
   500     public:
   646     public:
   501       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) 
   647       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) 
   502 	: Graph::NodeMap<T>(*(_G.G)) { }
   648 	: Graph::NodeMap<T>(_G.getGraph()) { }
   503       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, 
   649       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, 
   504 	      T a) : Graph::NodeMap<T>(*(_G.G), a) { }
   650 	      T a) : Graph::NodeMap<T>(_G.getGraph(), a) { }
   505     };
   651     };
   506 
   652 
   507 //     template <typename T>
   653 //     template <typename T>
   508 //     class NodeMap {
   654 //     class NodeMap {
   509 //       typename Graph::NodeMap<T> node_map; 
   655 //       typename Graph::NodeMap<T> node_map; 
   516 
   662 
   517     template <typename T>
   663     template <typename T>
   518     class EdgeMap {
   664     class EdgeMap {
   519       typename Graph::EdgeMap<T> forward_map, backward_map; 
   665       typename Graph::EdgeMap<T> forward_map, backward_map; 
   520     public:
   666     public:
   521       EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(*(_G.G)), backward_map(*(_G.G)) { }
   667       EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(_G.getGraph()), backward_map(_G.getGraph()) { }
   522       EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(*(_G.G), a), backward_map(*(_G.G), a) { }
   668       EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(_G.getGraph(), a), backward_map(_G.getGraph(), a) { }
   523       void set(EdgeIt e, T a) { 
   669       void set(EdgeIt e, T a) { 
   524 	if (e.out_or_in) 
   670 	if (e.out_or_in) 
   525 	  forward_map.set(e.out, a); 
   671 	  forward_map.set(e.out, a); 
   526 	else 
   672 	else 
   527 	  backward_map.set(e.in, a); 
   673 	  backward_map.set(e.in, a);