2 #ifndef GRAPH_WRAPPER_H
3 #define GRAPH_WRAPPER_H
9 template<typename Graph>
10 class TrivGraphWrapper {
15 typedef Graph BaseGraph;
17 typedef typename Graph::Node Node;
18 typedef typename Graph::NodeIt NodeIt;
20 typedef typename Graph::Edge Edge;
21 typedef typename Graph::OutEdgeIt OutEdgeIt;
22 typedef typename Graph::InEdgeIt InEdgeIt;
23 //typedef typename Graph::SymEdgeIt SymEdgeIt;
24 typedef typename Graph::EdgeIt EdgeIt;
26 //TrivGraphWrapper() : graph(0) { }
27 TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
29 void setGraph(Graph& _graph) { graph = &_graph; }
30 Graph& getGraph() const { return (*graph); }
32 template<typename I> I& first(I& i) const { return graph->first(i); }
33 template<typename I, typename P> I& first(I& i, const P& p) const {
34 return graph->first(i, p); }
36 template<typename I> I getNext(const I& i) const {
37 return graph->getNext(i); }
38 template<typename I> I& next(I &i) const { return graph->next(i); }
40 template< typename It > It first() const {
41 It e; first(e); return e; }
43 template< typename It > It first(const Node& v) const {
44 It e; first(e, v); return e; }
46 Node head(const Edge& e) const { return graph->head(e); }
47 Node tail(const Edge& e) const { return graph->tail(e); }
49 template<typename I> bool valid(const I& i) const
50 { return graph->valid(i); }
52 //template<typename I> void setInvalid(const I &i);
53 //{ return graph->setInvalid(i); }
55 int nodeNum() const { return graph->nodeNum(); }
56 int edgeNum() const { return graph->edgeNum(); }
58 template<typename I> Node aNode(const I& e) const {
59 return graph->aNode(e); }
60 template<typename I> Node bNode(const I& e) const {
61 return graph->bNode(e); }
63 Node addNode() const { return graph->addNode(); }
64 Edge addEdge(const Node& tail, const Node& head) const {
65 return graph->addEdge(tail, head); }
67 template<typename I> void erase(const I& i) const { graph->erase(i); }
69 void clear() const { graph->clear(); }
71 template<typename T> class NodeMap : public Graph::NodeMap<T> {
73 NodeMap(const TrivGraphWrapper<Graph>& _G) :
74 Graph::NodeMap<T>(_G.getGraph()) { }
75 NodeMap(const TrivGraphWrapper<Graph>& _G, T a) :
76 Graph::NodeMap<T>(_G.getGraph(), a) { }
79 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
81 EdgeMap(const TrivGraphWrapper<Graph>& _G) :
82 Graph::EdgeMap<T>(_G.getGraph()) { }
83 EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) :
84 Graph::EdgeMap<T>(_G.getGraph(), a) { }
88 template<typename GraphWrapper>
89 class GraphWrapperSkeleton {
94 typedef typename GraphWrapper::BaseGraph BaseGraph;
96 typedef typename GraphWrapper::Node Node;
97 typedef typename GraphWrapper::NodeIt NodeIt;
99 typedef typename GraphWrapper::Edge Edge;
100 typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
101 typedef typename GraphWrapper::InEdgeIt InEdgeIt;
102 //typedef typename GraphWrapper::SymEdgeIt SymEdgeIt;
103 typedef typename GraphWrapper::EdgeIt EdgeIt;
105 //GraphWrapperSkeleton() : gw() { }
106 GraphWrapperSkeleton(GraphWrapper& _gw) : gw(_gw) { }
108 void setGraph(BaseGraph& _graph) { gw.setGraph(_graph); }
109 BaseGraph& getGraph() const { return gw.getGraph(); }
111 template<typename I> I& first(I& i) const { return gw.first(i); }
112 template<typename I, typename P> I& first(I& i, const P& p) const {
113 return gw.first(i, p); }
115 template<typename I> I getNext(const I& i) const { return gw.getNext(i); }
116 template<typename I> I& next(I &i) const { return gw.next(i); }
118 template< typename It > It first() const {
119 It e; this->first(e); return e; }
121 template< typename It > It first(const Node& v) const {
122 It e; this->first(e, v); return e; }
124 Node head(const Edge& e) const { return gw.head(e); }
125 Node tail(const Edge& e) const { return gw.tail(e); }
127 template<typename I> bool valid(const I& i) const { return gw.valid(i); }
129 //template<typename I> void setInvalid(const I &i);
130 //{ return graph->setInvalid(i); }
132 int nodeNum() const { return gw.nodeNum(); }
133 int edgeNum() const { return gw.edgeNum(); }
135 template<typename I> Node aNode(const I& e) const { return gw.aNode(e); }
136 template<typename I> Node bNode(const I& e) const { return gw.bNode(e); }
138 Node addNode() const { return gw.addNode(); }
139 Edge addEdge(const Node& tail, const Node& head) const {
140 return gw.addEdge(tail, head); }
142 template<typename I> void erase(const I& i) const { gw.erase(i); }
144 void clear() const { gw.clear(); }
146 template<typename T> class NodeMap : public GraphWrapper::NodeMap<T> {
148 NodeMap(const GraphWrapperSkeleton<GraphWrapper>& _G) :
149 GraphWrapper::NodeMap<T>(_G.gw) { }
150 NodeMap(const GraphWrapperSkeleton<GraphWrapper>& _G, T a) :
151 GraphWrapper::NodeMap<T>(_G.gw, a) { }
154 template<typename T> class EdgeMap : public GraphWrapper::EdgeMap<T> {
156 EdgeMap(const GraphWrapperSkeleton<GraphWrapper>& _G) :
157 GraphWrapper::EdgeMap<T>(_G.gw) { }
158 EdgeMap(const GraphWrapperSkeleton<GraphWrapper>& _G, T a) :
159 GraphWrapper::EdgeMap<T>(_G.gw, a) { }
163 template<typename Graph>
164 class RevGraphWrapper
170 typedef Graph BaseGraph;
172 typedef typename Graph::Node Node;
173 typedef typename Graph::NodeIt NodeIt;
175 typedef typename Graph::Edge Edge;
176 typedef typename Graph::OutEdgeIt InEdgeIt;
177 typedef typename Graph::InEdgeIt OutEdgeIt;
178 //typedef typename Graph::SymEdgeIt SymEdgeIt;
179 typedef typename Graph::EdgeIt EdgeIt;
181 //RevGraphWrapper() : graph(0) { }
182 RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
184 void setGraph(Graph& _graph) { graph = &_graph; }
185 Graph& getGraph() const { return (*graph); }
187 template<typename I> I& first(I& i) const { return graph->first(i); }
188 template<typename I, typename P> I& first(I& i, const P& p) const {
189 return graph->first(i, p); }
191 template<typename I> I getNext(const I& i) const {
192 return graph->getNext(i); }
193 template<typename I> I& next(I &i) const { return graph->next(i); }
195 template< typename It > It first() const {
196 It e; first(e); return e; }
198 template< typename It > It first(const Node& v) const {
199 It e; first(e, v); return e; }
201 Node head(const Edge& e) const { return graph->tail(e); }
202 Node tail(const Edge& e) const { return graph->head(e); }
204 template<typename I> bool valid(const I& i) const
205 { return graph->valid(i); }
207 //template<typename I> void setInvalid(const I &i);
208 //{ return graph->setInvalid(i); }
210 template<typename I> Node aNode(const I& e) const {
211 return graph->aNode(e); }
212 template<typename I> Node bNode(const I& e) const {
213 return graph->bNode(e); }
215 Node addNode() const { return graph->addNode(); }
216 Edge addEdge(const Node& tail, const Node& head) const {
217 return graph->addEdge(tail, head); }
219 int nodeNum() const { return graph->nodeNum(); }
220 int edgeNum() const { return graph->edgeNum(); }
222 template<typename I> void erase(const I& i) const { graph->erase(i); }
224 void clear() const { graph->clear(); }
226 template<typename T> class NodeMap : public Graph::NodeMap<T> {
228 NodeMap(const RevGraphWrapper<Graph>& _G) :
229 Graph::NodeMap<T>(_G.getGraph()) { }
230 NodeMap(const RevGraphWrapper<Graph>& _G, T a) :
231 Graph::NodeMap<T>(_G.getGraph(), a) { }
234 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
236 EdgeMap(const RevGraphWrapper<Graph>& _G) :
237 Graph::EdgeMap<T>(_G.getGraph()) { }
238 EdgeMap(const RevGraphWrapper<Graph>& _G, T a) :
239 Graph::EdgeMap<T>(_G.getGraph(), a) { }
244 template<typename Graph>
245 class UndirGraphWrapper {
250 typedef Graph BaseGraph;
252 typedef typename Graph::Node Node;
253 typedef typename Graph::NodeIt NodeIt;
255 //typedef typename Graph::Edge Edge;
256 //typedef typename Graph::OutEdgeIt OutEdgeIt;
257 //typedef typename Graph::InEdgeIt InEdgeIt;
258 //typedef typename Graph::SymEdgeIt SymEdgeIt;
259 //typedef typename Graph::EdgeIt EdgeIt;
262 typedef typename Graph::Edge GraphEdge;
263 typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
264 typedef typename Graph::InEdgeIt GraphInEdgeIt;
267 //UndirGraphWrapper() : graph(0) { }
268 UndirGraphWrapper(Graph& _graph) : graph(&_graph) { }
270 void setGraph(Graph& _graph) { graph = &_graph; }
271 Graph& getGraph() const { return (*graph); }
274 friend class UndirGraphWrapper<Graph>;
275 bool out_or_in; //true iff out
279 Edge() : out_or_in(), out(), in() { }
280 Edge(const Invalid& i) : out_or_in(false), out(), in(i) { }
281 operator GraphEdge() const {
282 if (out_or_in) return(out); else return(in);
284 friend bool operator==(const Edge& u, const Edge& v) {
286 return (u.out_or_in && u.out==v.out);
288 return (!u.out_or_in && u.in==v.in);
290 friend bool operator!=(const Edge& u, const Edge& v) {
292 return (!u.out_or_in || u.out!=v.out);
294 return (u.out_or_in || u.in!=v.in);
298 class OutEdgeIt : public Edge {
299 friend class UndirGraphWrapper<Graph>;
301 OutEdgeIt() : Edge() { }
302 OutEdgeIt(const Invalid& i) : Edge(i) { }
303 OutEdgeIt(const UndirGraphWrapper& _G, const Node& n) : Edge() {
305 _G.graph->first(out, n);
306 if (!(_G.graph->valid(out))) {
308 _G.graph->first(in, n);
313 OutEdgeIt& first(OutEdgeIt& e, const Node& n) const {
315 graph->first(e.out, n);
316 if (!(graph->valid(e.out))) {
318 graph->first(e.in, n);
323 OutEdgeIt& next(OutEdgeIt& e) const {
325 Node n=graph->tail(e.out);
327 if (!graph->valid(e.out)) {
329 graph->first(e.in, n);
337 Node aNode(const OutEdgeIt& e) const {
338 if (e.out_or_in) return graph->tail(e); else return graph->head(e); }
339 Node bNode(const OutEdgeIt& e) const {
340 if (e.out_or_in) return graph->head(e); else return graph->tail(e); }
342 typedef OutEdgeIt InEdgeIt;
344 template<typename I> I& first(I& i) const { return graph->first(i); }
345 // template<typename I, typename P> I& first(I& i, const P& p) const {
346 // return graph->first(i, p); }
348 template<typename I> I getNext(const I& i) const {
349 return graph->getNext(i); }
350 template<typename I> I& next(I &i) const { return graph->next(i); }
352 template< typename It > It first() const {
353 It e; first(e); return e; }
355 template< typename It > It first(const Node& v) const {
356 It e; first(e, v); return e; }
358 Node head(const Edge& e) const { return graph->head(e); }
359 Node tail(const Edge& e) const { return graph->tail(e); }
361 template<typename I> bool valid(const I& i) const
362 { return graph->valid(i); }
364 //template<typename I> void setInvalid(const I &i);
365 //{ return graph->setInvalid(i); }
367 int nodeNum() const { return graph->nodeNum(); }
368 int edgeNum() const { return graph->edgeNum(); }
370 // template<typename I> Node aNode(const I& e) const {
371 // return graph->aNode(e); }
372 // template<typename I> Node bNode(const I& e) const {
373 // return graph->bNode(e); }
375 Node addNode() const { return graph->addNode(); }
376 Edge addEdge(const Node& tail, const Node& head) const {
377 return graph->addEdge(tail, head); }
379 template<typename I> void erase(const I& i) const { graph->erase(i); }
381 void clear() const { graph->clear(); }
383 template<typename T> class NodeMap : public Graph::NodeMap<T> {
385 NodeMap(const UndirGraphWrapper<Graph>& _G) :
386 Graph::NodeMap<T>(_G.getGraph()) { }
387 NodeMap(const UndirGraphWrapper<Graph>& _G, T a) :
388 Graph::NodeMap<T>(_G.getGraph(), a) { }
391 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
393 EdgeMap(const UndirGraphWrapper<Graph>& _G) :
394 Graph::EdgeMap<T>(_G.getGraph()) { }
395 EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) :
396 Graph::EdgeMap<T>(_G.getGraph(), a) { }
402 // template<typename Graph>
403 // class SymGraphWrapper
408 // typedef Graph BaseGraph;
410 // typedef typename Graph::Node Node;
411 // typedef typename Graph::Edge Edge;
413 // typedef typename Graph::NodeIt NodeIt;
415 // //FIXME tag-ekkel megcsinalni, hogy abbol csinaljon
416 // //iranyitatlant, ami van
417 // //mert csak 1 dolgot lehet be typedef-elni
418 // typedef typename Graph::OutEdgeIt SymEdgeIt;
419 // //typedef typename Graph::InEdgeIt SymEdgeIt;
420 // //typedef typename Graph::SymEdgeIt SymEdgeIt;
421 // typedef typename Graph::EdgeIt EdgeIt;
423 // int nodeNum() const { return graph->nodeNum(); }
424 // int edgeNum() const { return graph->edgeNum(); }
426 // template<typename I> I& first(I& i) const { return graph->first(i); }
427 // template<typename I, typename P> I& first(I& i, const P& p) const {
428 // return graph->first(i, p); }
429 // //template<typename I> I next(const I i); { return graph->goNext(i); }
430 // //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
432 // template< typename It > It first() const {
433 // It e; first(e); return e; }
435 // template< typename It > It first(Node v) const {
436 // It e; first(e, v); return e; }
438 // Node head(const Edge& e) const { return graph->head(e); }
439 // Node tail(const Edge& e) const { return graph->tail(e); }
441 // template<typename I> Node aNode(const I& e) const {
442 // return graph->aNode(e); }
443 // template<typename I> Node bNode(const I& e) const {
444 // return graph->bNode(e); }
446 // //template<typename I> bool valid(const I i);
447 // //{ return graph->valid(i); }
449 // //template<typename I> void setInvalid(const I &i);
450 // //{ return graph->setInvalid(i); }
452 // Node addNode() { return graph->addNode(); }
453 // Edge addEdge(const Node& tail, const Node& head) {
454 // return graph->addEdge(tail, head); }
456 // template<typename I> void erase(const I& i) { graph->erase(i); }
458 // void clear() { graph->clear(); }
460 // template<typename T> class NodeMap : public Graph::NodeMap<T> { };
461 // template<typename T> class EdgeMap : public Graph::EdgeMap<T> { };
463 // void setGraph(Graph& _graph) { graph = &_graph; }
464 // Graph& getGraph() { return (*graph); }
466 // //SymGraphWrapper() : graph(0) { }
467 // SymGraphWrapper(Graph& _graph) : graph(&_graph) { }
471 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
472 class ResGraphWrapper {
474 typedef Graph BaseGraph;
475 typedef typename Graph::Node Node;
476 typedef typename Graph::NodeIt NodeIt;
478 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
479 typedef typename Graph::InEdgeIt OldInEdgeIt;
483 const CapacityMap* capacity;
486 ResGraphWrapper(const Graph& _G, FlowMap& _flow,
487 const CapacityMap& _capacity) :
488 graph(&_G), flow(&_flow), capacity(&_capacity) { }
490 void setGraph(const Graph& _graph) { graph = &_graph; }
491 const Graph& getGraph() const { return (*graph); }
496 friend class OutEdgeIt;
499 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
501 bool out_or_in; //true, iff out
505 Edge() : out_or_in(true) { }
506 Edge(const Invalid& i) : out_or_in(false), out(), in(i) { }
507 // bool valid() const {
508 // return out_or_in && out.valid() || in.valid(); }
509 friend bool operator==(const Edge& u, const Edge& v) {
511 return (u.out_or_in && u.out==v.out);
513 return (!u.out_or_in && u.in==v.in);
515 friend bool operator!=(const Edge& u, const Edge& v) {
517 return (!u.out_or_in || u.out!=v.out);
519 return (u.out_or_in || u.in!=v.in);
524 class OutEdgeIt : public Edge {
525 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
529 OutEdgeIt(const Edge& e) : Edge(e) { }
530 OutEdgeIt(const Invalid& i) : Edge(i) { }
532 OutEdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG, Node v) : Edge() {
533 resG.graph->first(out, v);
534 while( resG.graph->valid(out) && !(resG.free(out)>0) ) { resG.graph->next(out); }
535 if (!resG.graph->valid(out)) {
537 resG.graph->first(in, v);
538 while( resG.graph->valid(in) && !(resG.free(in)>0) ) { resG.graph->next(in); }
542 // OutEdgeIt& operator++() {
544 // Node v=/*resG->*/G->aNode(out);
546 // while( out.valid() && !(Edge::free()>0) ) { ++out; }
547 // if (!out.valid()) {
550 // while( in.valid() && !(Edge::free()>0) ) { ++in; }
554 // while( in.valid() && !(Edge::free()>0) ) { ++in; }
560 class EdgeIt : public Edge {
561 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
562 typename Graph::NodeIt v;
565 //EdgeIt(const EdgeIt& e) : Edge(e), v(e.v) { }
566 EdgeIt(const Invalid& i) : Edge(i) { }
567 EdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG) : Edge() {
568 resG.graph->first(v);
569 if (resG.graph->valid(v)) resG.graph->first(out, v); else out=OldOutEdgeIt(INVALID);
570 while (resG.graph->valid(out) && !(resG.free(out)>0) ) { resG.graph->next(out); }
571 while (resG.graph->valid(v) && !resG.graph->valid(out)) {
573 if (resG.graph->valid(v)) resG.graph->first(out, v);
574 while (resG.graph->valid(out) && !(resG.free(out)>0) ) { resG.graph->next(out); }
576 if (!resG.graph->valid(out)) {
578 resG.graph->first(v);
579 if (resG.graph->valid(v)) resG.graph->first(in, v); else in=OldInEdgeIt(INVALID);
580 while (resG.graph->valid(in) && !(resG.free(in)>0) ) { resG.graph->next(in); }
581 while (resG.graph->valid(v) && !resG.graph->valid(in)) {
583 if (resG.graph->valid(v)) resG.graph->first(in, v);
584 while (resG.graph->valid(in) && !(resG.free(in)>0) ) { resG.graph->next(in); }
588 // EdgeIt& operator++() {
591 // while (out.valid() && !(Edge::free()>0) ) { ++out; }
592 // while (v.valid() && !out.valid()) {
594 // if (v.valid()) G->first(out, v);
595 // while (out.valid() && !(Edge::free()>0) ) { ++out; }
597 // if (!out.valid()) {
600 // if (v.valid()) G->first(in, v); else in=OldInEdgeIt();
601 // while (in.valid() && !(Edge::free()>0) ) { ++in; }
602 // while (v.valid() && !in.valid()) {
604 // if (v.valid()) G->first(in, v);
605 // while (in.valid() && !(Edge::free()>0) ) { ++in; }
610 // while (in.valid() && !(Edge::free()>0) ) { ++in; }
611 // while (v.valid() && !in.valid()) {
613 // if (v.valid()) G->first(in, v);
614 // while (in.valid() && !(Edge::free()>0) ) { ++in; }
621 NodeIt& first(NodeIt& v) const { return graph->first(v); }
622 OutEdgeIt& first(OutEdgeIt& e, Node v) const {
623 e=OutEdgeIt(*this, v);
626 EdgeIt& first(EdgeIt& e) const {
631 NodeIt& next(NodeIt& n) const { return graph->next(n); }
633 OutEdgeIt& next(OutEdgeIt& e) const {
635 Node v=graph->aNode(e.out);
637 while( graph->valid(e.out) && !(free(e.out)>0) ) { graph->next(e.out); }
638 if (!graph->valid(e.out)) {
640 graph->first(e.in, v);
641 while( graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); }
645 while( graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); }
650 EdgeIt& next(EdgeIt& e) const {
653 while (graph->valid(e.out) && !(free(e.out)>0) ) { graph->next(e.out); }
654 while (graph->valid(e.v) && !graph->valid(e.out)) {
656 if (graph->valid(e.v)) graph->first(e.out, e.v);
657 while (graph->valid(e.out) && !(free(e.out)>0) ) { graph->next(e.out); }
659 if (!graph->valid(e.out)) {
662 if (graph->valid(e.v)) graph->first(e.in, e.v); else e.in=OldInEdgeIt(INVALID);
663 while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); }
664 while (graph->valid(e.v) && !graph->valid(e.in)) {
666 if (graph->valid(e.v)) graph->first(e.in, e.v);
667 while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); }
672 while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); }
673 while (graph->valid(e.v) && !graph->valid(e.in)) {
675 if (graph->valid(e.v)) graph->first(e.in, e.v);
676 while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); }
683 template< typename It >
690 template< typename It >
691 It first(Node v) const {
697 Node tail(Edge e) const {
698 return ((e.out_or_in) ? graph->aNode(e.out) : graph->aNode(e.in)); }
699 Node head(Edge e) const {
700 return ((e.out_or_in) ? graph->bNode(e.out) : graph->bNode(e.in)); }
702 Node aNode(OutEdgeIt e) const {
703 return ((e.out_or_in) ? graph->aNode(e.out) : graph->aNode(e.in)); }
704 Node bNode(OutEdgeIt e) const {
705 return ((e.out_or_in) ? graph->bNode(e.out) : graph->bNode(e.in)); }
707 int id(Node v) const { return graph->id(v); }
709 bool valid(Node n) const { return graph->valid(n); }
710 bool valid(Edge e) const {
711 return e.out_or_in ? graph->valid(e.out) : graph->valid(e.in); }
713 void augment(const Edge& e, Number a) const {
715 flow->set(e.out, flow->get(e.out)+a);
717 flow->set(e.in, flow->get(e.in)-a);
720 Number free(const Edge& e) const {
722 return (capacity->get(e.out)-flow->get(e.out));
724 return (flow->get(e.in));
727 Number free(OldOutEdgeIt out) const {
728 return (capacity->get(out)-flow->get(out));
731 Number free(OldInEdgeIt in) const {
732 return (flow->get(in));
735 template<typename T> class NodeMap : public Graph::NodeMap<T> {
737 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G)
738 : Graph::NodeMap<T>(_G.getGraph()) { }
739 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G,
740 T a) : Graph::NodeMap<T>(_G.getGraph(), a) { }
743 // template <typename T>
745 // typename Graph::NodeMap<T> node_map;
747 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.graph)) { }
748 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.graph), a) { }
749 // void set(Node nit, T a) { node_map.set(nit, a); }
750 // T get(Node nit) const { return node_map.get(nit); }
753 template <typename T>
755 typename Graph::EdgeMap<T> forward_map, backward_map;
757 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(_G.getGraph()), backward_map(_G.getGraph()) { }
758 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(_G.getGraph(), a), backward_map(_G.getGraph(), a) { }
759 void set(Edge e, T a) {
761 forward_map.set(e.out, a);
763 backward_map.set(e.in, a);
767 return forward_map.get(e.out);
769 return backward_map.get(e.in);
774 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
775 class ErasingResGraphWrapper : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap> {
777 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt> first_out_edges;
778 //ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<int> dist;
780 ErasingResGraphWrapper(const Graph& _G, FlowMap& _flow,
781 const CapacityMap& _capacity) :
782 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>(_G, _flow, _capacity),
783 first_out_edges(*this) /*, dist(*this)*/ {
784 for(NodeIt n=this->template first<NodeIt>(); this->valid(n); this->next(n)) {
786 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(e, n);
787 first_out_edges.set(n, e);
791 //void setGraph(Graph& _graph) { graph = &_graph; }
792 //Graph& getGraph() const { return (*graph); }
794 //TrivGraphWrapper() : graph(0) { }
795 //ErasingResGraphWrapper(Graph& _graph) : graph(&_graph) { }
797 //typedef Graph BaseGraph;
799 //typedef typename Graph::Node Node;
800 //typedef typename Graph::NodeIt NodeIt;
802 //typedef typename Graph::Edge Edge;
803 //typedef typename Graph::OutEdgeIt OutEdgeIt;
804 //typedef typename Graph::InEdgeIt InEdgeIt;
805 //typedef typename Graph::SymEdgeIt SymEdgeIt;
806 //typedef typename Graph::EdgeIt EdgeIt;
808 typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Node Node;
809 typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeIt NodeIt;
811 typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Edge Edge;
812 typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt OutEdgeIt;
813 //typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::InEdgeIt InEdgeIt;
814 //typedef typename Graph::SymEdgeIt SymEdgeIt;
815 //typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeIt EdgeIt;
817 NodeIt& first(NodeIt& n) const {
818 return ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(n);
821 OutEdgeIt& first(OutEdgeIt& e, const Node& n) const {
822 e=first_out_edges.get(n);
826 //ROSSZ template<typename I> I& first(I& i) const { return first(i); }
827 //ROSSZ template<typename I, typename P> I& first(I& i, const P& p) const {
828 // return first(i, p); }
830 //template<typename I> I getNext(const I& i) const {
831 // return graph->getNext(i); }
832 //template<typename I> I& next(I &i) const { return graph->next(i); }
834 template< typename It > It first() const {
835 It e; first(e); return e; }
837 template< typename It > It first(const Node& v) const {
838 It e; first(e, v); return e; }
840 //Node head(const Edge& e) const { return graph->head(e); }
841 //Node tail(const Edge& e) const { return graph->tail(e); }
843 //template<typename I> bool valid(const I& i) const
844 // { return graph->valid(i); }
846 //int nodeNum() const { return graph->nodeNum(); }
847 //int edgeNum() const { return graph->edgeNum(); }
849 //template<typename I> Node aNode(const I& e) const {
850 // return graph->aNode(e); }
851 //template<typename I> Node bNode(const I& e) const {
852 // return graph->bNode(e); }
854 //Node addNode() const { return graph->addNode(); }
855 //Edge addEdge(const Node& tail, const Node& head) const {
856 // return graph->addEdge(tail, head); }
858 //void erase(const OutEdgeIt& e) {
859 // first_out_edge(this->tail(e))=e;
861 void erase(const Edge& e) {
864 first_out_edges.set(this->tail(e), f);
866 //template<typename I> void erase(const I& i) const { graph->erase(i); }
868 //void clear() const { graph->clear(); }
870 template<typename T> class NodeMap : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T> {
872 NodeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) :
873 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/) { }
874 NodeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) :
875 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/, a) { }
878 template<typename T> class EdgeMap : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T> {
880 EdgeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) :
881 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/) { }
882 EdgeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) :
883 ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/, a) { }
887 template<typename GraphWrapper>
888 class FilterGraphWrapper {
891 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
892 class FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> {
897 //typedef Graph BaseGraph;
899 typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Node Node;
900 typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeIt NodeIt;
902 typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Edge Edge;
903 typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt OutEdgeIt;
904 //typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::InEdgeIt InEdgeIt;
905 //typedef typename Graph::SymEdgeIt SymEdgeIt;
906 typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeIt EdgeIt;
908 //FilterGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt> first_out_edges;
911 FilterGraphWrapper(const Graph& _G, FlowMap& _flow,
912 const CapacityMap& _capacity) :
913 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>(_G, _flow, _capacity), dist(*this, graph->nodeNum()) {
916 OutEdgeIt& first(OutEdgeIt& e, const Node& n) const {
917 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(e, n);
918 while (valid(e) && (dist.get(tail(e))/*+1!=*/>=dist.get(head(e))))
919 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
923 NodeIt& next(NodeIt& e) const {
924 return ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
927 OutEdgeIt& next(OutEdgeIt& e) const {
928 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
929 while (valid(e) && (dist.get(tail(e))/*+1!*/>=dist.get(head(e))))
930 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e);
934 NodeIt& first(NodeIt& n) const {
935 return ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(n);
938 void erase(const Edge& e) {
940 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(f);
941 while (valid(f) && (dist.get(tail(f))/*+1!=*/>=dist.get(head(f))))
942 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(f);
943 first_out_edges.set(this->tail(e), f);
946 //TrivGraphWrapper() : graph(0) { }
947 //TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
949 //void setGraph(Graph& _graph) { graph = &_graph; }
950 //Graph& getGraph() const { return (*graph); }
952 //template<typename I> I& first(I& i) const { return graph->first(i); }
953 //template<typename I, typename P> I& first(I& i, const P& p) const {
954 // return graph->first(i, p); }
956 //template<typename I> I getNext(const I& i) const {
957 // return graph->getNext(i); }
958 //template<typename I> I& next(I &i) const { return graph->next(i); }
960 template< typename It > It first() const {
961 It e; first(e); return e; }
963 template< typename It > It first(const Node& v) const {
964 It e; first(e, v); return e; }
966 //Node head(const Edge& e) const { return graph->head(e); }
967 //Node tail(const Edge& e) const { return graph->tail(e); }
969 //template<typename I> bool valid(const I& i) const
970 // { return graph->valid(i); }
972 //template<typename I> void setInvalid(const I &i);
973 //{ return graph->setInvalid(i); }
975 //int nodeNum() const { return graph->nodeNum(); }
976 //int edgeNum() const { return graph->edgeNum(); }
978 //template<typename I> Node aNode(const I& e) const {
979 // return graph->aNode(e); }
980 //template<typename I> Node bNode(const I& e) const {
981 // return graph->bNode(e); }
983 //Node addNode() const { return graph->addNode(); }
984 //Edge addEdge(const Node& tail, const Node& head) const {
985 // return graph->addEdge(tail, head); }
987 //template<typename I> void erase(const I& i) const { graph->erase(i); }
989 //void clear() const { graph->clear(); }
991 template<typename T> class NodeMap : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T> {
993 NodeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G) :
994 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/) { }
995 NodeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G, T a) :
996 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/, a) { }
999 template<typename T> class EdgeMap : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T> {
1001 EdgeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G) :
1002 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/) { }
1003 EdgeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G, T a) :
1004 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/, a) { }
1008 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<int> dist;
1014 // // FIXME: comparison should be made better!!!
1015 // template<typename Graph, typename T, typename LowerMap, typename FlowMap, typename UpperMap>
1016 // class ResGraphWrapper
1021 // typedef Graph BaseGraph;
1023 // typedef typename Graph::Node Node;
1024 // typedef typename Graph::Edge Edge;
1026 // typedef typename Graph::NodeIt NodeIt;
1028 // class OutEdgeIt {
1032 // typename Graph::OutEdgeIt o;
1033 // typename Graph::InEdgeIt i;
1039 // typename Graph::OutEdgeIt o;
1040 // typename Graph::InEdgeIt i;
1042 // typedef typename Graph::SymEdgeIt SymEdgeIt;
1043 // typedef typename Graph::EdgeIt EdgeIt;
1045 // int nodeNum() const { return graph->nodeNum(); }
1046 // int edgeNum() const { return graph->edgeNum(); }
1048 // Node& first(Node& n) const { return graph->first(n); }
1050 // // Edge and SymEdge is missing!!!!
1051 // // Edge <-> In/OutEdgeIt conversion is missing!!!!
1054 // OutEdgeIt& first(OutEdgeIt& e, const Node& n) const
1057 // graph->first(e.o,n);
1058 // while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
1059 // graph->goNext(e.o);
1060 // if(!graph->valid(e.o)) {
1061 // graph->first(e.i,n);
1062 // while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
1063 // graph->goNext(e.i);
1068 // OutEdgeIt &goNext(OutEdgeIt &e)
1070 // if(graph->valid(e.o)) {
1071 // while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
1072 // graph->goNext(e.o);
1073 // if(graph->valid(e.o)) return e;
1074 // else graph->first(e.i,e.n);
1077 // while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
1078 // graph->goNext(e.i);
1082 // OutEdgeIt Next(const OutEdgeIt &e) {OutEdgeIt t(e); return goNext(t);}
1084 // //bool valid(const OutEdgeIt e) { return graph->valid(e.o)||graph->valid(e.i);}
1087 // InEdgeIt& first(InEdgeIt& e, const Node& n) const
1090 // graph->first(e.i,n);
1091 // while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
1092 // graph->goNext(e.i);
1093 // if(!graph->valid(e.i)) {
1094 // graph->first(e.o,n);
1095 // while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
1096 // graph->goNext(e.o);
1101 // InEdgeIt &goNext(InEdgeIt &e)
1103 // if(graph->valid(e.i)) {
1104 // while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
1105 // graph->goNext(e.i);
1106 // if(graph->valid(e.i)) return e;
1107 // else graph->first(e.o,e.n);
1110 // while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
1111 // graph->goNext(e.o);
1115 // InEdgeIt Next(const InEdgeIt &e) {InEdgeIt t(e); return goNext(t);}
1117 // //bool valid(const InEdgeIt e) { return graph->valid(e.i)||graph->valid(e.o);}
1119 // //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
1120 // //template<typename I> I next(const I i); { return graph->goNext(i); }
1122 // template< typename It > It first() const {
1123 // It e; first(e); return e; }
1125 // template< typename It > It first(Node v) const {
1126 // It e; first(e, v); return e; }
1128 // Node head(const Edge& e) const { return graph->head(e); }
1129 // Node tail(const Edge& e) const { return graph->tail(e); }
1131 // template<typename I> Node aNode(const I& e) const {
1132 // return graph->aNode(e); }
1133 // template<typename I> Node bNode(const I& e) const {
1134 // return graph->bNode(e); }
1136 // //template<typename I> bool valid(const I i);
1137 // //{ return graph->valid(i); }
1139 // //template<typename I> void setInvalid(const I &i);
1140 // //{ return graph->setInvalid(i); }
1142 // Node addNode() { return graph->addNode(); }
1143 // Edge addEdge(const Node& tail, const Node& head) {
1144 // return graph->addEdge(tail, head); }
1146 // template<typename I> void erase(const I& i) { graph->erase(i); }
1148 // void clear() { graph->clear(); }
1150 // template<typename S> class NodeMap : public Graph::NodeMap<S> { };
1151 // template<typename S> class EdgeMap : public Graph::EdgeMap<S> { };
1153 // void setGraph(Graph& _graph) { graph = &_graph; }
1154 // Graph& getGraph() { return (*graph); }
1156 // //ResGraphWrapper() : graph(0) { }
1157 // ResGraphWrapper(Graph& _graph) : graph(&_graph) { }
1162 #endif //GRAPH_WRAPPER_H