One more step toward the standars interface.
2 #ifndef GRAPH_WRAPPER_H
3 #define GRAPH_WRAPPER_H
7 template<typename Graph>
8 class TrivGraphWrapper {
12 typedef Graph BaseGraph;
14 typedef typename Graph::NodeIt NodeIt;
15 typedef typename Graph::EachNodeIt EachNodeIt;
17 typedef typename Graph::EdgeIt EdgeIt;
18 typedef typename Graph::OutEdgeIt OutEdgeIt;
19 typedef typename Graph::InEdgeIt InEdgeIt;
20 //typedef typename Graph::SymEdgeIt SymEdgeIt;
21 typedef typename Graph::EachEdgeIt EachEdgeIt;
23 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 {
25 return graph->getFirst(i, p); }
27 template<typename I> I getNext(const I& i) const {
28 return graph->getNext(i); }
29 template<typename I> I& next(I &i) const { return graph->next(i); }
31 template< typename It > It first() const {
32 It e; getFirst(e); return e; }
34 template< typename It > It first(const NodeIt& v) const {
35 It e; getFirst(e, v); return e; }
37 NodeIt head(const EdgeIt& e) const { return graph->head(e); }
38 NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
40 template<typename I> bool valid(const I& i) const
41 { return graph->valid(i); }
43 //template<typename I> void setInvalid(const I &i);
44 //{ return graph->setInvalid(i); }
46 int nodeNum() const { return graph->nodeNum(); }
47 int edgeNum() const { return graph->edgeNum(); }
49 template<typename I> NodeIt aNode(const I& e) const {
50 return graph->aNode(e); }
51 template<typename I> NodeIt bNode(const I& e) const {
52 return graph->bNode(e); }
54 NodeIt addNode() const { return graph->addNode(); }
55 EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const {
56 return graph->addEdge(tail, head); }
58 template<typename I> void erase(const I& i) const { graph->erase(i); }
60 void clear() const { graph->clear(); }
62 template<typename T> class NodeMap : public Graph::NodeMap<T> {
64 NodeMap(const TrivGraphWrapper<Graph>& _G) :
65 Graph::NodeMap<T>(*(_G.G)) { }
66 NodeMap(const TrivGraphWrapper<Graph>& _G, T a) :
67 Graph::NodeMap<T>(*(_G.G), a) { }
69 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
71 EdgeMap(const TrivGraphWrapper<Graph>& _G) :
72 Graph::EdgeMap<T>(*(_G.G)) { }
73 EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) :
74 Graph::EdgeMap<T>(*(_G.G), a) { }
77 void setGraph(Graph& _graph) { graph = &_graph; }
78 Graph& getGraph() const { return (*graph); }
80 //TrivGraphWrapper() : graph(0) { }
81 TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
84 template<typename Graph>
90 typedef Graph BaseGraph;
92 typedef typename Graph::NodeIt NodeIt;
93 typedef typename Graph::EachNodeIt EachNodeIt;
95 typedef typename Graph::EdgeIt EdgeIt;
96 typedef typename Graph::OutEdgeIt InEdgeIt;
97 typedef typename Graph::InEdgeIt OutEdgeIt;
98 //typedef typename Graph::SymEdgeIt SymEdgeIt;
99 typedef typename Graph::EachEdgeIt EachEdgeIt;
101 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 {
103 return graph->getFirst(i, p); }
105 template<typename I> I getNext(const I& i) const {
106 return graph->getNext(i); }
107 template<typename I> I& next(I &i) const { return graph->next(i); }
109 template< typename It > It first() const {
110 It e; getFirst(e); return e; }
112 template< typename It > It first(const NodeIt& v) const {
113 It e; getFirst(e, v); return e; }
115 NodeIt head(const EdgeIt& e) const { return graph->tail(e); }
116 NodeIt tail(const EdgeIt& e) const { return graph->head(e); }
118 template<typename I> bool valid(const I& i) const
119 { return graph->valid(i); }
121 //template<typename I> void setInvalid(const I &i);
122 //{ return graph->setInvalid(i); }
124 template<typename I> NodeIt aNode(const I& e) const {
125 return graph->aNode(e); }
126 template<typename I> NodeIt bNode(const I& e) const {
127 return graph->bNode(e); }
129 NodeIt addNode() const { return graph->addNode(); }
130 EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const {
131 return graph->addEdge(tail, head); }
133 int nodeNum() const { return graph->nodeNum(); }
134 int edgeNum() const { return graph->edgeNum(); }
136 template<typename I> void erase(const I& i) const { graph->erase(i); }
138 void clear() const { graph->clear(); }
140 template<typename T> class NodeMap : public Graph::NodeMap<T> {
142 NodeMap(const RevGraphWrapper<Graph>& _G) :
143 Graph::NodeMap<T>(*(_G.G)) { }
144 NodeMap(const RevGraphWrapper<Graph>& _G, T a) :
145 Graph::NodeMap<T>(*(_G.G), a) { }
147 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
149 EdgeMap(const RevGraphWrapper<Graph>& _G) :
150 Graph::EdgeMap<T>(*(_G.G)) { }
151 EdgeMap(const RevGraphWrapper<Graph>& _G, T a) :
152 Graph::EdgeMap<T>(*(_G.G), a) { }
155 void setGraph(Graph& _graph) { graph = &_graph; }
156 Graph& getGraph() const { return (*graph); }
158 //RevGraphWrapper() : graph(0) { }
159 RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
163 // template<typename Graph>
164 // class SymGraphWrapper
169 // typedef Graph BaseGraph;
171 // typedef typename Graph::NodeIt NodeIt;
172 // typedef typename Graph::EdgeIt EdgeIt;
174 // typedef typename Graph::EachNodeIt EachNodeIt;
176 // //FIXME tag-ekkel megcsinalni, hogy abbol csinaljon
177 // //iranyitatlant, ami van
178 // //mert csak 1 dolgot lehet be typedef-elni
179 // typedef typename Graph::OutEdgeIt SymEdgeIt;
180 // //typedef typename Graph::InEdgeIt SymEdgeIt;
181 // //typedef typename Graph::SymEdgeIt SymEdgeIt;
182 // typedef typename Graph::EachEdgeIt EachEdgeIt;
184 // int nodeNum() const { return graph->nodeNum(); }
185 // int edgeNum() const { return graph->edgeNum(); }
187 // template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
188 // template<typename I, typename P> I& getFirst(I& i, const P& p) const {
189 // return graph->getFirst(i, p); }
190 // //template<typename I> I next(const I i); { return graph->goNext(i); }
191 // //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
193 // template< typename It > It first() const {
194 // It e; getFirst(e); return e; }
196 // template< typename It > It first(NodeIt v) const {
197 // It e; getFirst(e, v); return e; }
199 // NodeIt head(const EdgeIt& e) const { return graph->head(e); }
200 // NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
202 // template<typename I> NodeIt aNode(const I& e) const {
203 // return graph->aNode(e); }
204 // template<typename I> NodeIt bNode(const I& e) const {
205 // return graph->bNode(e); }
207 // //template<typename I> bool valid(const I i);
208 // //{ return graph->valid(i); }
210 // //template<typename I> void setInvalid(const I &i);
211 // //{ return graph->setInvalid(i); }
213 // NodeIt addNode() { return graph->addNode(); }
214 // EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) {
215 // return graph->addEdge(tail, head); }
217 // template<typename I> void erase(const I& i) { graph->erase(i); }
219 // void clear() { graph->clear(); }
221 // template<typename T> class NodeMap : public Graph::NodeMap<T> { };
222 // template<typename T> class EdgeMap : public Graph::EdgeMap<T> { };
224 // void setGraph(Graph& _graph) { graph = &_graph; }
225 // Graph& getGraph() { return (*graph); }
227 // //SymGraphWrapper() : graph(0) { }
228 // SymGraphWrapper(Graph& _graph) : graph(&_graph) { }
232 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
233 class ResGraphWrapper {
235 typedef Graph BaseGraph;
236 typedef typename Graph::NodeIt NodeIt;
237 typedef typename Graph::EachNodeIt EachNodeIt;
239 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
240 typedef typename Graph::InEdgeIt OldInEdgeIt;
243 const CapacityMap* capacity;
245 ResGraphWrapper(const Graph& _G, FlowMap& _flow,
246 const CapacityMap& _capacity) :
247 G(&_G), flow(&_flow), capacity(&_capacity) { }
248 // ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) :
249 // G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
253 friend class OutEdgeIt;
256 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
258 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
261 const CapacityMap* capacity;
265 bool out_or_in; //true, iff out
267 EdgeIt() : out_or_in(true) { }
268 EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) :
269 G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { }
270 //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) { }
271 Number free() const {
273 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
275 return (/*resG->*/flow->get(in));
279 return out_or_in && out.valid() || in.valid(); }
280 void augment(Number a) const {
282 /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
284 /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
291 std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out));
293 std::cout << "invalid";
298 std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in));
300 std::cout << "invalid";
302 std::cout << std::endl;
306 Number free(OldOutEdgeIt out) const {
307 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
309 Number free(OldInEdgeIt in) const {
310 return (/*resG->*/flow->get(in));
313 class OutEdgeIt : public EdgeIt {
314 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
318 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) {
319 //out=/*resG->*/G->template first<OldOutEdgeIt>(v);
321 while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
324 //in=/*resG->*/G->template first<OldInEdgeIt>(v);
326 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
330 OutEdgeIt& operator++() {
332 NodeIt v=/*resG->*/G->aNode(out);
334 while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
337 G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
338 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
342 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
348 class EachEdgeIt : public EdgeIt {
349 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
350 typename Graph::EachNodeIt v;
353 //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
354 EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) {
357 if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt();
358 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
359 while (v.valid() && !out.valid()) {
361 if (v.valid()) G->getFirst(out, v);
362 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
367 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
368 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
369 while (v.valid() && !in.valid()) {
371 if (v.valid()) G->getFirst(in, v);
372 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
376 EachEdgeIt& operator++() {
379 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
380 while (v.valid() && !out.valid()) {
382 if (v.valid()) G->getFirst(out, v);
383 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
388 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
389 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
390 while (v.valid() && !in.valid()) {
392 if (v.valid()) G->getFirst(in, v);
393 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
398 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
399 while (v.valid() && !in.valid()) {
401 if (v.valid()) G->getFirst(in, v);
402 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
409 void getFirst(EachNodeIt& v) const { G->getFirst(v); }
410 void getFirst(OutEdgeIt& e, NodeIt v) const {
411 e=OutEdgeIt(*G, v, *flow, *capacity);
413 void getFirst(EachEdgeIt& e) const {
414 e=EachEdgeIt(*G, *flow, *capacity);
417 EachNodeIt& next(EachNodeIt& n) const { return G->next(n); }
419 OutEdgeIt& next(OutEdgeIt& e) const {
421 NodeIt v=G->aNode(e.out);
423 while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
424 if (!G->valid(e.out)) {
426 G->getFirst(e.in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
427 while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
431 while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
436 EachEdgeIt& next(EachEdgeIt& e) const {
439 while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
440 while (G->valid(e.v) && !G->valid(e.out)) {
442 if (G->valid(e.v)) G->getFirst(e.out, e.v);
443 while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
445 if (!G->valid(e.out)) {
448 if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt();
449 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
450 while (G->valid(e.v) && !G->valid(e.in)) {
452 if (G->valid(e.v)) G->getFirst(e.in, e.v);
453 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
458 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
459 while (G->valid(e.v) && !G->valid(e.in)) {
461 if (G->valid(e.v)) G->getFirst(e.in, e.v);
462 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
469 template< typename It >
476 template< typename It >
477 It first(NodeIt v) const {
483 NodeIt tail(EdgeIt e) const {
484 return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
485 NodeIt head(EdgeIt e) const {
486 return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
488 NodeIt aNode(OutEdgeIt e) const {
489 return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
490 NodeIt bNode(OutEdgeIt e) const {
491 return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
493 int id(NodeIt v) const { return G->id(v); }
495 bool valid(NodeIt n) const { return G->valid(n); }
496 bool valid(EdgeIt e) const {
497 return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
499 template<typename T> class NodeMap : public Graph::NodeMap<T> {
501 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G)
502 : Graph::NodeMap<T>(*(_G.G)) { }
503 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G,
504 T a) : Graph::NodeMap<T>(*(_G.G), a) { }
507 // template <typename T>
509 // typename Graph::NodeMap<T> node_map;
511 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.G)) { }
512 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.G), a) { }
513 // void set(NodeIt nit, T a) { node_map.set(nit, a); }
514 // T get(NodeIt nit) const { return node_map.get(nit); }
517 template <typename T>
519 typename Graph::EdgeMap<T> forward_map, backward_map;
521 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(*(_G.G)), backward_map(*(_G.G)) { }
522 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(*(_G.G), a), backward_map(*(_G.G), a) { }
523 void set(EdgeIt e, T a) {
525 forward_map.set(e.out, a);
527 backward_map.set(e.in, a);
531 return forward_map.get(e.out);
533 return backward_map.get(e.in);
541 // // FIXME: comparison should be made better!!!
542 // template<typename Graph, typename T, typename LowerMap, typename FlowMap, typename UpperMap>
543 // class ResGraphWrapper
548 // typedef Graph BaseGraph;
550 // typedef typename Graph::NodeIt NodeIt;
551 // typedef typename Graph::EdgeIt EdgeIt;
553 // typedef typename Graph::EachNodeIt EachNodeIt;
557 // //Graph::NodeIt n;
559 // typename Graph::OutEdgeIt o;
560 // typename Graph::InEdgeIt i;
564 // //Graph::NodeIt n;
566 // typename Graph::OutEdgeIt o;
567 // typename Graph::InEdgeIt i;
569 // typedef typename Graph::SymEdgeIt SymEdgeIt;
570 // typedef typename Graph::EachEdgeIt EachEdgeIt;
572 // int nodeNum() const { return graph->nodeNum(); }
573 // int edgeNum() const { return graph->edgeNum(); }
575 // NodeIt& getFirst(NodeIt& n) const { return graph->getFirst(n); }
577 // // EachEdge and SymEdge is missing!!!!
578 // // EdgeIt <-> In/OutEdgeIt conversion is missing!!!!
581 // OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const
584 // graph->getFirst(e.o,n);
585 // while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
586 // graph->goNext(e.o);
587 // if(!graph->valid(e.o)) {
588 // graph->getFirst(e.i,n);
589 // while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
590 // graph->goNext(e.i);
595 // OutEdgeIt &goNext(OutEdgeIt &e)
597 // if(graph->valid(e.o)) {
598 // while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
599 // graph->goNext(e.o);
600 // if(graph->valid(e.o)) return e;
601 // else graph->getFirst(e.i,e.n);
604 // while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
605 // graph->goNext(e.i);
609 // OutEdgeIt Next(const OutEdgeIt &e) {OutEdgeIt t(e); return goNext(t);}
611 // //bool valid(const OutEdgeIt e) { return graph->valid(e.o)||graph->valid(e.i);}
614 // InEdgeIt& getFirst(InEdgeIt& e, const NodeIt& n) const
617 // graph->getFirst(e.i,n);
618 // while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
619 // graph->goNext(e.i);
620 // if(!graph->valid(e.i)) {
621 // graph->getFirst(e.o,n);
622 // while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
623 // graph->goNext(e.o);
628 // InEdgeIt &goNext(InEdgeIt &e)
630 // if(graph->valid(e.i)) {
631 // while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
632 // graph->goNext(e.i);
633 // if(graph->valid(e.i)) return e;
634 // else graph->getFirst(e.o,e.n);
637 // while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
638 // graph->goNext(e.o);
642 // InEdgeIt Next(const InEdgeIt &e) {InEdgeIt t(e); return goNext(t);}
644 // //bool valid(const InEdgeIt e) { return graph->valid(e.i)||graph->valid(e.o);}
646 // //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
647 // //template<typename I> I next(const I i); { return graph->goNext(i); }
649 // template< typename It > It first() const {
650 // It e; getFirst(e); return e; }
652 // template< typename It > It first(NodeIt v) const {
653 // It e; getFirst(e, v); return e; }
655 // NodeIt head(const EdgeIt& e) const { return graph->head(e); }
656 // NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
658 // template<typename I> NodeIt aNode(const I& e) const {
659 // return graph->aNode(e); }
660 // template<typename I> NodeIt bNode(const I& e) const {
661 // return graph->bNode(e); }
663 // //template<typename I> bool valid(const I i);
664 // //{ return graph->valid(i); }
666 // //template<typename I> void setInvalid(const I &i);
667 // //{ return graph->setInvalid(i); }
669 // NodeIt addNode() { return graph->addNode(); }
670 // EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) {
671 // return graph->addEdge(tail, head); }
673 // template<typename I> void erase(const I& i) { graph->erase(i); }
675 // void clear() { graph->clear(); }
677 // template<typename S> class NodeMap : public Graph::NodeMap<S> { };
678 // template<typename S> class EdgeMap : public Graph::EdgeMap<S> { };
680 // void setGraph(Graph& _graph) { graph = &_graph; }
681 // Graph& getGraph() { return (*graph); }
683 // //ResGraphWrapper() : graph(0) { }
684 // ResGraphWrapper(Graph& _graph) : graph(&_graph) { }
689 #endif //GRAPH_WRAPPER_H