src/work/marci/graph_wrapper.h
changeset 171 ec3d3596e3c9
parent 158 4f54d89fa9d2
child 174 44700ed9ffaa
equal deleted inserted replaced
5:eb45adef7d8d 6:519477d6709c
    17     typedef typename Graph::EdgeIt EdgeIt;
    17     typedef typename Graph::EdgeIt EdgeIt;
    18     typedef typename Graph::OutEdgeIt OutEdgeIt;
    18     typedef typename Graph::OutEdgeIt OutEdgeIt;
    19     typedef typename Graph::InEdgeIt InEdgeIt;
    19     typedef typename Graph::InEdgeIt InEdgeIt;
    20     //typedef typename Graph::SymEdgeIt SymEdgeIt;
    20     //typedef typename Graph::SymEdgeIt SymEdgeIt;
    21     typedef typename Graph::EachEdgeIt EachEdgeIt;
    21     typedef typename Graph::EachEdgeIt EachEdgeIt;
       
    22 
       
    23     //TrivGraphWrapper() : graph(0) { }
       
    24     TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
    25 
       
    26     void setGraph(Graph& _graph) { graph = &_graph; }
       
    27     Graph& getGraph() const { return (*graph); }
    22     
    28     
    23     template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
    29     template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
    24     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
    30     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
    25       return graph->getFirst(i, p); }
    31       return graph->getFirst(i, p); }
    26     
    32     
    64       NodeMap(const TrivGraphWrapper<Graph>& _G) : 
    70       NodeMap(const TrivGraphWrapper<Graph>& _G) : 
    65 	Graph::NodeMap<T>(_G.getGraph()) { }
    71 	Graph::NodeMap<T>(_G.getGraph()) { }
    66       NodeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    72       NodeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    67 	Graph::NodeMap<T>(_G.getGraph(), a) { }
    73 	Graph::NodeMap<T>(_G.getGraph(), a) { }
    68     };
    74     };
       
    75 
    69     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
    76     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
    70     public:
    77     public:
    71       EdgeMap(const TrivGraphWrapper<Graph>& _G) : 
    78       EdgeMap(const TrivGraphWrapper<Graph>& _G) : 
    72 	Graph::EdgeMap<T>(_G.getGraph()) { }
    79 	Graph::EdgeMap<T>(_G.getGraph()) { }
    73       EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    80       EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
    74 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
    81 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
    75     };
    82     };
    76     
       
    77     void setGraph(Graph& _graph) { graph = &_graph; }
       
    78     Graph& getGraph() const { return (*graph); }
       
    79   
       
    80     //TrivGraphWrapper() : graph(0) { }
       
    81     TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
    82   };
    83   };
    83 
    84 
    84   template<typename Graph>
    85   template<typename Graph>
    85   class RevGraphWrapper
    86   class RevGraphWrapper
    86   {
    87   {
    95     typedef typename Graph::EdgeIt EdgeIt;
    96     typedef typename Graph::EdgeIt EdgeIt;
    96     typedef typename Graph::OutEdgeIt InEdgeIt;
    97     typedef typename Graph::OutEdgeIt InEdgeIt;
    97     typedef typename Graph::InEdgeIt OutEdgeIt;
    98     typedef typename Graph::InEdgeIt OutEdgeIt;
    98     //typedef typename Graph::SymEdgeIt SymEdgeIt;
    99     //typedef typename Graph::SymEdgeIt SymEdgeIt;
    99     typedef typename Graph::EachEdgeIt EachEdgeIt;
   100     typedef typename Graph::EachEdgeIt EachEdgeIt;
       
   101 
       
   102     //RevGraphWrapper() : graph(0) { }
       
   103     RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   104 
       
   105     void setGraph(Graph& _graph) { graph = &_graph; }
       
   106     Graph& getGraph() const { return (*graph); }
   100     
   107     
   101     template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
   108     template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
   102     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
   109     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
   103       return graph->getFirst(i, p); }
   110       return graph->getFirst(i, p); }
   104 
   111 
   142       NodeMap(const RevGraphWrapper<Graph>& _G) : 
   149       NodeMap(const RevGraphWrapper<Graph>& _G) : 
   143 	Graph::NodeMap<T>(_G.getGraph()) { }
   150 	Graph::NodeMap<T>(_G.getGraph()) { }
   144       NodeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   151       NodeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   145 	Graph::NodeMap<T>(_G.getGraph(), a) { }
   152 	Graph::NodeMap<T>(_G.getGraph(), a) { }
   146     };
   153     };
       
   154 
   147     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
   155     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
   148     public:
   156     public:
   149       EdgeMap(const RevGraphWrapper<Graph>& _G) : 
   157       EdgeMap(const RevGraphWrapper<Graph>& _G) : 
   150 	Graph::EdgeMap<T>(_G.getGraph()) { }
   158 	Graph::EdgeMap<T>(_G.getGraph()) { }
   151       EdgeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   159       EdgeMap(const RevGraphWrapper<Graph>& _G, T a) : 
   152 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
   160 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
   153     };
   161     };
   154 
       
   155     void setGraph(Graph& _graph) { graph = &_graph; }
       
   156     Graph& getGraph() const { return (*graph); }
       
   157 
       
   158     //RevGraphWrapper() : graph(0) { }
       
   159     RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   160   };
   162   };
   161 
   163 
   162 
   164 
   163   template<typename Graph>
   165   template<typename Graph>
   164   class UndirGraphWrapper {
   166   class UndirGraphWrapper {
   180     typedef typename Graph::EdgeIt GraphEdgeIt;
   182     typedef typename Graph::EdgeIt GraphEdgeIt;
   181     typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
   183     typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
   182     typedef typename Graph::InEdgeIt GraphInEdgeIt;
   184     typedef typename Graph::InEdgeIt GraphInEdgeIt;
   183     //public:
   185     //public:
   184 
   186 
       
   187     //UndirGraphWrapper() : graph(0) { }
       
   188     UndirGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   189 
       
   190     void setGraph(Graph& _graph) { graph = &_graph; }
       
   191     Graph& getGraph() const { return (*graph); }
       
   192   
   185     class EdgeIt {
   193     class EdgeIt {
   186       friend class UndirGraphWrapper<Graph>;
   194       friend class UndirGraphWrapper<Graph>;
   187       bool out_or_in; //true iff out
   195       bool out_or_in; //true iff out
   188       GraphOutEdgeIt out;
   196       GraphOutEdgeIt out;
   189       GraphInEdgeIt in;
   197       GraphInEdgeIt in;
   194       }
   202       }
   195     };
   203     };
   196 
   204 
   197     class OutEdgeIt : public EdgeIt {
   205     class OutEdgeIt : public EdgeIt {
   198       friend class UndirGraphWrapper<Graph>;
   206       friend class UndirGraphWrapper<Graph>;
   199       //bool out_or_in; //true iff out
       
   200       //GraphOutEdgeIt out;
       
   201       //GraphInEdgeIt in;
       
   202     public:
   207     public:
   203       OutEdgeIt() : EdgeIt() { }
   208       OutEdgeIt() : EdgeIt() { }
   204       OutEdgeIt(const UndirGraphWrapper& _G, const NodeIt& n) : EdgeIt() { 
   209       OutEdgeIt(const UndirGraphWrapper& _G, const NodeIt& n) : EdgeIt() { 
   205 	_G.graph->getFirst(out, n);
   210 	_G.graph->getFirst(out, n);
   206 	if (!(_G.graph->valid(out))) {
   211 	if (!(_G.graph->valid(out))) {
   285       NodeMap(const UndirGraphWrapper<Graph>& _G) : 
   290       NodeMap(const UndirGraphWrapper<Graph>& _G) : 
   286 	Graph::NodeMap<T>(_G.getGraph()) { }
   291 	Graph::NodeMap<T>(_G.getGraph()) { }
   287       NodeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
   292       NodeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
   288 	Graph::NodeMap<T>(_G.getGraph(), a) { }
   293 	Graph::NodeMap<T>(_G.getGraph(), a) { }
   289     };
   294     };
       
   295 
   290     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
   296     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
   291     public:
   297     public:
   292       EdgeMap(const UndirGraphWrapper<Graph>& _G) : 
   298       EdgeMap(const UndirGraphWrapper<Graph>& _G) : 
   293 	Graph::EdgeMap<T>(_G.getGraph()) { }
   299 	Graph::EdgeMap<T>(_G.getGraph()) { }
   294       EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
   300       EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
   295 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
   301 	Graph::EdgeMap<T>(_G.getGraph(), a) { }
   296     };
   302     };
   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   };
   303   };
   304 
   304 
   305 
   305 
   306 
   306 
   307 //   template<typename Graph>
   307 //   template<typename Graph>
   384     typedef typename Graph::InEdgeIt OldInEdgeIt;
   384     typedef typename Graph::InEdgeIt OldInEdgeIt;
   385     const Graph* G;
   385     const Graph* G;
   386     FlowMap* flow;
   386     FlowMap* flow;
   387     const CapacityMap* capacity;
   387     const CapacityMap* capacity;
   388   public:
   388   public:
       
   389 
   389     ResGraphWrapper(const Graph& _G, FlowMap& _flow, 
   390     ResGraphWrapper(const Graph& _G, FlowMap& _flow, 
   390 	     const CapacityMap& _capacity) : 
   391 	     const CapacityMap& _capacity) : 
   391       G(&_G), flow(&_flow), capacity(&_capacity) { }
   392       G(&_G), flow(&_flow), capacity(&_capacity) { }
   392 //     ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : 
   393 //     ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : 
   393 //       G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
   394 //       G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
   394     void setGraph(Graph& _graph) { graph = &_graph; }
   395 
   395     Graph& getGraph() const { return (*graph); }
   396     void setGraph(const Graph& _graph) { graph = &_graph; }
       
   397     const Graph& getGraph() const { return (*G); }
       
   398 
   396     class EdgeIt; 
   399     class EdgeIt; 
   397     class OutEdgeIt; 
   400     class OutEdgeIt; 
   398     friend class EdgeIt; 
   401     friend class EdgeIt; 
   399     friend class OutEdgeIt; 
   402     friend class OutEdgeIt; 
   400 
   403 
   401     class EdgeIt {
   404     class EdgeIt {
   402       friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
   405       friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
   403     protected:
   406     protected:
   404       //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
   407       bool out_or_in; //true, iff out
   405       const Graph* G;
       
   406       FlowMap* flow;
       
   407       const CapacityMap* capacity;
       
   408       //OldSymEdgeIt sym;
       
   409       OldOutEdgeIt out;
   408       OldOutEdgeIt out;
   410       OldInEdgeIt in;
   409       OldInEdgeIt in;
   411       bool out_or_in; //true, iff out
       
   412     public:
   410     public:
   413       EdgeIt() : out_or_in(true) { } 
   411       EdgeIt() : out_or_in(true) { } 
   414       EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : 
   412 //       bool valid() const { 
   415 	G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { }
   413 // 	return out_or_in && out.valid() || in.valid(); }
   416       //EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { }
   414     };
   417       Number free() const { 
   415 
   418 	if (out_or_in) { 
   416 
   419 	  return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
   417     class OutEdgeIt : public EdgeIt {
   420 	} else { 
   418       friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
   421 	  return (/*resG->*/flow->get(in)); 
   419     public:
       
   420       OutEdgeIt() { }
       
   421       //FIXME
       
   422       OutEdgeIt(const EdgeIt& e) : EdgeIt(e) { }
       
   423     private:
       
   424       OutEdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG, NodeIt v) : EdgeIt() { 
       
   425 	resG.G->getFirst(out, v);
       
   426 	while( out.valid() && !(resG.free(out)>0) ) { ++out; }
       
   427 	if (!out.valid()) {
       
   428 	  out_or_in=0;
       
   429 	  resG.G->getFirst(in, v);
       
   430 	  while( in.valid() && !(resG.free(in)>0) ) { ++in; }
   422 	}
   431 	}
   423       }
   432       }
   424       bool valid() const { 
   433 //     public:
   425 	return out_or_in && out.valid() || in.valid(); }
   434 //       OutEdgeIt& operator++() { 
   426       void augment(Number a) const {
   435 // 	if (out_or_in) {
   427 	if (out_or_in) { 
   436 // 	  NodeIt v=/*resG->*/G->aNode(out);
   428 	  /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
   437 // 	  ++out;
   429 	} else { 
   438 // 	  while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
   430 	  /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
   439 // 	  if (!out.valid()) {
   431 	}
   440 // 	    out_or_in=0;
   432       }
   441 // 	    G->getFirst(in, v); 
   433       void print() { 
   442 // 	    while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   434 	if (out_or_in) {
   443 // 	  }
   435 	  std::cout << "out "; 
   444 // 	} else {
   436 	  if (out.valid()) 
   445 // 	  ++in;
   437 	    std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out)); 
   446 // 	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } 
   438 	  else 
   447 // 	}
   439 	    std::cout << "invalid"; 
   448 // 	return *this; 
   440 	}
   449 //       }
   441 	else {
       
   442 	  std::cout << "in "; 
       
   443 	  if (in.valid()) 
       
   444 	    std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in)); 
       
   445 	  else 
       
   446 	    std::cout << "invalid"; 
       
   447 	}
       
   448 	std::cout << std::endl;
       
   449       }
       
   450     };
       
   451 
       
   452     Number free(OldOutEdgeIt out) const { 
       
   453       return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
       
   454     }
       
   455     Number free(OldInEdgeIt in) const { 
       
   456       return (/*resG->*/flow->get(in)); 
       
   457     }
       
   458 
       
   459     class OutEdgeIt : public EdgeIt {
       
   460       friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
       
   461     public:
       
   462       OutEdgeIt() { }
       
   463     private:
       
   464       OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { 
       
   465 	//out=/*resG->*/G->template first<OldOutEdgeIt>(v);
       
   466 	G->getFirst(out, v);
       
   467 	while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
       
   468 	if (!out.valid()) {
       
   469 	  out_or_in=0;
       
   470 	  //in=/*resG->*/G->template first<OldInEdgeIt>(v);
       
   471 	  G->getFirst(in, v);
       
   472 	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
       
   473 	}
       
   474       }
       
   475     public:
       
   476       OutEdgeIt& operator++() { 
       
   477 	if (out_or_in) {
       
   478 	  NodeIt v=/*resG->*/G->aNode(out);
       
   479 	  ++out;
       
   480 	  while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
       
   481 	  if (!out.valid()) {
       
   482 	    out_or_in=0;
       
   483 	    G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
       
   484 	    while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
       
   485 	  }
       
   486 	} else {
       
   487 	  ++in;
       
   488 	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } 
       
   489 	}
       
   490 	return *this; 
       
   491       }
       
   492     };
   450     };
   493 
   451 
   494     class EachEdgeIt : public EdgeIt {
   452     class EachEdgeIt : public EdgeIt {
   495       friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
   453       friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
   496       typename Graph::EachNodeIt v;
   454       typename Graph::EachNodeIt v;
   497     public:
   455     public:
   498       EachEdgeIt() { }
   456       EachEdgeIt() { }
   499       //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
   457       //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
   500       EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { 
   458       EachEdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG) : EdgeIt() { 
   501 	out_or_in=true;
   459 	resG.G->getFirst(v);
   502 	G->getFirst(v);
   460 	if (v.valid()) resG.G->getFirst(out, v); else out=OldOutEdgeIt();
   503 	if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt();
   461 	while (out.valid() && !(resG.free(out)>0) ) { ++out; }
   504 	while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
       
   505 	while (v.valid() && !out.valid()) { 
   462 	while (v.valid() && !out.valid()) { 
   506 	  ++v; 
   463 	  ++v; 
   507 	  if (v.valid()) G->getFirst(out, v); 
   464 	  if (v.valid()) resG.G->getFirst(out, v); 
   508 	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
   465 	  while (out.valid() && !(resG.free(out)>0) ) { ++out; }
   509 	}
   466 	}
   510 	if (!out.valid()) {
   467 	if (!out.valid()) {
   511 	  out_or_in=0;
   468 	  out_or_in=0;
   512 	  G->getFirst(v);
   469 	  resG.G->getFirst(v);
   513 	  if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
   470 	  if (v.valid()) resG.G->getFirst(in, v); else in=OldInEdgeIt();
   514 	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   471 	  while (in.valid() && !(resG.free(in)>0) ) { ++in; }
   515 	  while (v.valid() && !in.valid()) { 
   472 	  while (v.valid() && !in.valid()) { 
   516 	    ++v; 
   473 	    ++v; 
   517 	    if (v.valid()) G->getFirst(in, v); 
   474 	    if (v.valid()) resG.G->getFirst(in, v); 
   518 	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   475 	    while (in.valid() && !(resG.free(in)>0) ) { ++in; }
   519 	  }
   476 	  }
   520 	}
   477 	}
   521       }
   478       }
   522       EachEdgeIt& operator++() { 
   479 //       EachEdgeIt& operator++() { 
   523 	if (out_or_in) {
   480 // 	if (out_or_in) {
   524 	  ++out;
   481 // 	  ++out;
   525 	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
   482 // 	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
   526 	  while (v.valid() && !out.valid()) { 
   483 // 	  while (v.valid() && !out.valid()) { 
   527 	    ++v; 
   484 // 	    ++v; 
   528 	    if (v.valid()) G->getFirst(out, v); 
   485 // 	    if (v.valid()) G->getFirst(out, v); 
   529 	    while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
   486 // 	    while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
   530 	  }
   487 // 	  }
   531 	  if (!out.valid()) {
   488 // 	  if (!out.valid()) {
   532 	    out_or_in=0;
   489 // 	    out_or_in=0;
   533 	    G->getFirst(v);
   490 // 	    G->getFirst(v);
   534 	    if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
   491 // 	    if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
   535 	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   492 // 	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   536 	    while (v.valid() && !in.valid()) { 
   493 // 	    while (v.valid() && !in.valid()) { 
   537 	      ++v; 
   494 // 	      ++v; 
   538 	      if (v.valid()) G->getFirst(in, v); 
   495 // 	      if (v.valid()) G->getFirst(in, v); 
   539 	      while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   496 // 	      while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   540 	    }  
   497 // 	    }  
   541 	  }
   498 // 	  }
   542 	} else {
   499 // 	} else {
   543 	  ++in;
   500 // 	  ++in;
   544 	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   501 // 	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   545 	  while (v.valid() && !in.valid()) { 
   502 // 	  while (v.valid() && !in.valid()) { 
   546 	    ++v; 
   503 // 	    ++v; 
   547 	    if (v.valid()) G->getFirst(in, v); 
   504 // 	    if (v.valid()) G->getFirst(in, v); 
   548 	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   505 // 	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
   549 	  }
   506 // 	  }
   550 	}
   507 // 	}
   551 	return *this;
   508 // 	return *this;
   552       }
   509 //       }
   553     };
   510     };
   554 
   511 
   555     void getFirst(EachNodeIt& v) const { G->getFirst(v); }
   512     EachNodeIt& getFirst(EachNodeIt& v) const { G->getFirst(v); }
   556     void getFirst(OutEdgeIt& e, NodeIt v) const { 
   513     OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const { 
   557       e=OutEdgeIt(*G, v, *flow, *capacity); 
   514       e=OutEdgeIt(*this, v); 
   558     }
   515     }
   559     void getFirst(EachEdgeIt& e) const { 
   516     EachEdgeIt& getFirst(EachEdgeIt& e) const { 
   560       e=EachEdgeIt(*G, *flow, *capacity); 
   517       e=EachEdgeIt(*this); 
   561     }
   518     }
   562    
   519    
   563     EachNodeIt& next(EachNodeIt& n) const { return G->next(n); }
   520     EachNodeIt& next(EachNodeIt& n) const { return G->next(n); }
   564 
   521 
   565     OutEdgeIt& next(OutEdgeIt& e) const { 
   522     OutEdgeIt& next(OutEdgeIt& e) const { 
   566       if (e.out_or_in) {
   523       if (e.out_or_in) {
   567 	NodeIt v=G->aNode(e.out);
   524 	NodeIt v=G->aNode(e.out);
   568 	++(e.out);
   525 	++(e.out);
   569 	while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
   526 	while( G->valid(e.out) && !(free(e.out)>0) ) { ++(e.out); }
   570 	if (!G->valid(e.out)) {
   527 	if (!G->valid(e.out)) {
   571 	  e.out_or_in=0;
   528 	  e.out_or_in=0;
   572 	  G->getFirst(e.in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
   529 	  G->getFirst(e.in, v); 
   573 	  while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
   530 	  while( G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); }
   574 	}
   531 	}
   575       } else {
   532       } else {
   576 	++(e.in);
   533 	++(e.in);
   577 	while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } 
   534 	while( G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } 
   578       }
   535       }
   579       return e;
   536       return e;
   580     }
   537     }
   581 
   538 
   582     EachEdgeIt& next(EachEdgeIt& e) const { 
   539     EachEdgeIt& next(EachEdgeIt& e) const { 
   583       if (e.out_or_in) {
   540       if (e.out_or_in) {
   584 	++(e.out);
   541 	++(e.out);
   585 	while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
   542 	while (G->valid(e.out) && !(free(e.out)>0) ) { ++(e.out); }
   586 	  while (G->valid(e.v) && !G->valid(e.out)) { 
   543 	  while (G->valid(e.v) && !G->valid(e.out)) { 
   587 	    ++(e.v); 
   544 	    ++(e.v); 
   588 	    if (G->valid(e.v)) G->getFirst(e.out, e.v); 
   545 	    if (G->valid(e.v)) G->getFirst(e.out, e.v); 
   589 	    while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
   546 	    while (G->valid(e.out) && !(free(e.out)>0) ) { ++(e.out); }
   590 	  }
   547 	  }
   591 	  if (!G->valid(e.out)) {
   548 	  if (!G->valid(e.out)) {
   592 	    e.out_or_in=0;
   549 	    e.out_or_in=0;
   593 	    G->getFirst(e.v);
   550 	    G->getFirst(e.v);
   594 	    if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt();
   551 	    if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt();
   595 	    while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
   552 	    while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); }
   596 	    while (G->valid(e.v) && !G->valid(e.in)) { 
   553 	    while (G->valid(e.v) && !G->valid(e.in)) { 
   597 	      ++(e.v); 
   554 	      ++(e.v); 
   598 	      if (G->valid(e.v)) G->getFirst(e.in, e.v); 
   555 	      if (G->valid(e.v)) G->getFirst(e.in, e.v); 
   599 	      while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
   556 	      while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); }
   600 	    }  
   557 	    }  
   601 	  }
   558 	  }
   602 	} else {
   559 	} else {
   603 	  ++(e.in);
   560 	  ++(e.in);
   604 	  while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
   561 	  while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); }
   605 	  while (G->valid(e.v) && !G->valid(e.in)) { 
   562 	  while (G->valid(e.v) && !G->valid(e.in)) { 
   606 	    ++(e.v); 
   563 	    ++(e.v); 
   607 	    if (G->valid(e.v)) G->getFirst(e.in, e.v); 
   564 	    if (G->valid(e.v)) G->getFirst(e.in, e.v); 
   608 	    while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
   565 	    while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); }
   609 	  }
   566 	  }
   610 	}
   567 	}
   611 	return e;
   568 	return e;
   612       }
   569       }
   613     
   570     
   639     int id(NodeIt v) const { return G->id(v); }
   596     int id(NodeIt v) const { return G->id(v); }
   640 
   597 
   641     bool valid(NodeIt n) const { return G->valid(n); }
   598     bool valid(NodeIt n) const { return G->valid(n); }
   642     bool valid(EdgeIt e) const { 
   599     bool valid(EdgeIt e) const { 
   643       return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
   600       return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
       
   601 
       
   602     void augment(const EdgeIt& e, Number a) const {
       
   603       if (e.out_or_in)  
       
   604 	flow->set(e.out, flow->get(e.out)+a);
       
   605       else  
       
   606 	flow->set(e.in, flow->get(e.in)-a);
       
   607     }
       
   608 
       
   609     Number free(const EdgeIt& e) const { 
       
   610       if (e.out_or_in) 
       
   611 	return (capacity->get(e.out)-flow->get(e.out)); 
       
   612       else 
       
   613 	return (flow->get(e.in)); 
       
   614     }
       
   615 
       
   616     Number free(OldOutEdgeIt out) const { 
       
   617       return (capacity->get(out)-flow->get(out)); 
       
   618     }
       
   619     
       
   620     Number free(OldInEdgeIt in) const { 
       
   621       return (flow->get(in)); 
       
   622     }
   644 
   623 
   645     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
   624     template<typename T> class NodeMap : public Graph::NodeMap<T> { 
   646     public:
   625     public:
   647       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) 
   626       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) 
   648 	: Graph::NodeMap<T>(_G.getGraph()) { }
   627 	: Graph::NodeMap<T>(_G.getGraph()) { }
   677 	  return forward_map.get(e.out); 
   656 	  return forward_map.get(e.out); 
   678 	else 
   657 	else 
   679 	  return backward_map.get(e.in); 
   658 	  return backward_map.get(e.in); 
   680       }
   659       }
   681     };
   660     };
       
   661   };
       
   662 
       
   663   template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
       
   664   class ErasingResGraphWrapper : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap> {
       
   665   protected:
       
   666     ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt> first_out_edges;
       
   667     //ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<int> dist;
       
   668   public:
       
   669     ErasingResGraphWrapper(const Graph& _G, FlowMap& _flow, 
       
   670 			   const CapacityMap& _capacity) : 
       
   671       ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>(_G, _flow, _capacity), 
       
   672       first_out_edges(*this) /*, dist(*this)*/ { 
       
   673       for(EachNodeIt n=this->template first<EachNodeIt>(); this->valid(n); this->next(n)) {
       
   674 	OutEdgeIt e;
       
   675 	ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::getFirst(e, n);
       
   676 	first_out_edges.set(n, e);
       
   677       }
       
   678     }
       
   679 
       
   680     //void setGraph(Graph& _graph) { graph = &_graph; }
       
   681     //Graph& getGraph() const { return (*graph); }
       
   682   
       
   683     //TrivGraphWrapper() : graph(0) { }
       
   684     //ErasingResGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   685 
       
   686     //typedef Graph BaseGraph;
       
   687 
       
   688     //typedef typename Graph::NodeIt NodeIt;
       
   689     //typedef typename Graph::EachNodeIt EachNodeIt;
       
   690 
       
   691     //typedef typename Graph::EdgeIt EdgeIt;
       
   692     //typedef typename Graph::OutEdgeIt OutEdgeIt;
       
   693     //typedef typename Graph::InEdgeIt InEdgeIt;
       
   694     //typedef typename Graph::SymEdgeIt SymEdgeIt;
       
   695     //typedef typename Graph::EachEdgeIt EachEdgeIt;
       
   696 
       
   697     typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeIt NodeIt;
       
   698     typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EachNodeIt EachNodeIt;
       
   699 
       
   700     typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeIt EdgeIt;
       
   701     typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt OutEdgeIt;
       
   702     //typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::InEdgeIt InEdgeIt;
       
   703     //typedef typename Graph::SymEdgeIt SymEdgeIt;
       
   704     //typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EachEdgeIt EachEdgeIt;
       
   705 
       
   706     EachNodeIt& getFirst(EachNodeIt& n) const { 
       
   707       return ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::getFirst(n);
       
   708     }
       
   709 
       
   710     OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const { 
       
   711       e=first_out_edges.get(n);
       
   712       return e;
       
   713     }
       
   714     
       
   715     //ROSSZ template<typename I> I& getFirst(I& i) const { return getFirst(i); }
       
   716     //ROSSZ template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
       
   717     //  return getFirst(i, p); }
       
   718     
       
   719     //template<typename I> I getNext(const I& i) const { 
       
   720     //  return graph->getNext(i); }
       
   721     //template<typename I> I& next(I &i) const { return graph->next(i); }    
       
   722 
       
   723     template< typename It > It first() const { 
       
   724       It e; getFirst(e); return e; }
       
   725 
       
   726     template< typename It > It first(const NodeIt& v) const { 
       
   727       It e; getFirst(e, v); return e; }
       
   728 
       
   729     //NodeIt head(const EdgeIt& e) const { return graph->head(e); }
       
   730     //NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
       
   731 
       
   732     //template<typename I> bool valid(const I& i) const 
       
   733     //  { return graph->valid(i); }
       
   734   
       
   735     //int nodeNum() const { return graph->nodeNum(); }
       
   736     //int edgeNum() const { return graph->edgeNum(); }
       
   737   
       
   738     //template<typename I> NodeIt aNode(const I& e) const { 
       
   739     //  return graph->aNode(e); }
       
   740     //template<typename I> NodeIt bNode(const I& e) const { 
       
   741     //  return graph->bNode(e); }
       
   742   
       
   743     //NodeIt addNode() const { return graph->addNode(); }
       
   744     //EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { 
       
   745     //  return graph->addEdge(tail, head); }
       
   746   
       
   747     //void erase(const OutEdgeIt& e) {
       
   748     //  first_out_edge(this->tail(e))=e;
       
   749     //}
       
   750     void erase(const EdgeIt& e) {
       
   751       OutEdgeIt f(e);
       
   752       next(f);
       
   753       first_out_edges.set(this->tail(e), f);
       
   754     }
       
   755     //template<typename I> void erase(const I& i) const { graph->erase(i); }
       
   756   
       
   757     //void clear() const { graph->clear(); }
       
   758     
       
   759     template<typename T> class NodeMap : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T> { 
       
   760     public:
       
   761       NodeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : 
       
   762 	ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/) { }
       
   763       NodeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : 
       
   764 	ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/, a) { }
       
   765     };
       
   766 
       
   767     template<typename T> class EdgeMap : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T> { 
       
   768     public:
       
   769       EdgeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : 
       
   770 	ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/) { }
       
   771       EdgeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : 
       
   772 	ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/, a) { }
       
   773     };
       
   774   };
       
   775 
       
   776   template<typename GraphWrapper> 
       
   777   class FilterGraphWrapper {
       
   778   };
       
   779 
       
   780   template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
       
   781   class FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> {
       
   782 
       
   783     //Graph* graph;
       
   784   
       
   785   public:
       
   786     //typedef Graph BaseGraph;
       
   787 
       
   788     typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeIt NodeIt;
       
   789     typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EachNodeIt EachNodeIt;
       
   790 
       
   791     typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeIt EdgeIt;
       
   792     typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt OutEdgeIt;
       
   793     //typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::InEdgeIt InEdgeIt;
       
   794     //typedef typename Graph::SymEdgeIt SymEdgeIt;
       
   795     typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EachEdgeIt EachEdgeIt;
       
   796 
       
   797     //FilterGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt> first_out_edges;
       
   798     
       
   799   public:
       
   800     FilterGraphWrapper(const Graph& _G, FlowMap& _flow, 
       
   801 			   const CapacityMap& _capacity) : 
       
   802       ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>(_G, _flow, _capacity), dist(*this) { 
       
   803     }
       
   804 
       
   805     OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const {
       
   806       ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::getFirst(e, n);
       
   807       while (valid(e) && (dist.get(tail(e))+1!=dist.get(head(e)))) 
       
   808 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
       
   809       return e;
       
   810     }
       
   811 
       
   812     EachNodeIt& next(EachNodeIt& e) const {
       
   813       return ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
       
   814     }
       
   815 
       
   816     OutEdgeIt& next(OutEdgeIt& e) const {
       
   817       ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
       
   818       while (valid(e) && (dist.get(tail(e))+1!=dist.get(head(e)))) 
       
   819 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
       
   820       return e;
       
   821     }
       
   822 
       
   823     EachNodeIt& getFirst(EachNodeIt& n) const {
       
   824       return ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::getFirst(n);
       
   825     }
       
   826 
       
   827     void erase(const EdgeIt& e) {
       
   828       OutEdgeIt f(e);
       
   829       ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(f);
       
   830       while (valid(f) && (dist.get(tail(f))+1!=dist.get(head(f)))) 
       
   831 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(f);
       
   832       first_out_edges.set(this->tail(e), f);
       
   833     }
       
   834 
       
   835     //TrivGraphWrapper() : graph(0) { }
       
   836     //TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
       
   837 
       
   838     //void setGraph(Graph& _graph) { graph = &_graph; }
       
   839     //Graph& getGraph() const { return (*graph); }
       
   840     
       
   841     //template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
       
   842     //template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
       
   843     //  return graph->getFirst(i, p); }
       
   844     
       
   845     //template<typename I> I getNext(const I& i) const { 
       
   846     //  return graph->getNext(i); }
       
   847     //template<typename I> I& next(I &i) const { return graph->next(i); }    
       
   848 
       
   849     template< typename It > It first() const { 
       
   850       It e; getFirst(e); return e; }
       
   851 
       
   852     template< typename It > It first(const NodeIt& v) const { 
       
   853       It e; getFirst(e, v); return e; }
       
   854 
       
   855     //NodeIt head(const EdgeIt& e) const { return graph->head(e); }
       
   856     //NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
       
   857 
       
   858     //template<typename I> bool valid(const I& i) const 
       
   859     //  { return graph->valid(i); }
       
   860   
       
   861     //template<typename I> void setInvalid(const I &i);
       
   862     //{ return graph->setInvalid(i); }
       
   863 
       
   864     //int nodeNum() const { return graph->nodeNum(); }
       
   865     //int edgeNum() const { return graph->edgeNum(); }
       
   866   
       
   867     //template<typename I> NodeIt aNode(const I& e) const { 
       
   868     //  return graph->aNode(e); }
       
   869     //template<typename I> NodeIt bNode(const I& e) const { 
       
   870     //  return graph->bNode(e); }
       
   871   
       
   872     //NodeIt addNode() const { return graph->addNode(); }
       
   873     //EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { 
       
   874     //  return graph->addEdge(tail, head); }
       
   875   
       
   876     //template<typename I> void erase(const I& i) const { graph->erase(i); }
       
   877   
       
   878     //void clear() const { graph->clear(); }
       
   879     
       
   880     template<typename T> class NodeMap : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T> { 
       
   881     public:
       
   882       NodeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G) : 
       
   883 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/) { }
       
   884       NodeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G, T a) : 
       
   885 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/, a) { }
       
   886     };
       
   887 
       
   888     template<typename T> class EdgeMap : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T> { 
       
   889     public:
       
   890       EdgeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G) : 
       
   891 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/) { }
       
   892       EdgeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G, T a) : 
       
   893 	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/, a) { }
       
   894     };
       
   895 
       
   896   public:
       
   897     ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<int> dist;
   682 
   898 
   683   };
   899   };
   684 
   900 
   685 
   901 
   686 
   902