Adding GraphEdgeSet and GraphNodeSet classes to graph_utils.h.
2 #ifndef LEMON_GRAPH_WRAPPER_H
3 #define LEMON_GRAPH_WRAPPER_H
9 template<typename Graph>
10 class TrivGraphWrapper {
15 typedef Graph BaseGraph;
17 // TrivGraphWrapper() : graph(0) { }
18 TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
19 // void setGraph(Graph& _graph) { graph = &_graph; }
20 // Graph& getGraph() const { return *graph; }
22 typedef typename Graph::Node Node;
23 class NodeIt : public Graph::NodeIt {
26 NodeIt(const typename Graph::NodeIt& n) : Graph::NodeIt(n) { }
27 NodeIt(const Invalid& i) : Graph::NodeIt(i) { }
28 NodeIt(const TrivGraphWrapper<Graph>& _G) :
29 Graph::NodeIt(*(_G.graph)) { }
31 typedef typename Graph::Edge Edge;
32 class OutEdgeIt : public Graph::OutEdgeIt {
35 OutEdgeIt(const typename Graph::OutEdgeIt& e) : Graph::OutEdgeIt(e) { }
36 OutEdgeIt(const Invalid& i) : Graph::OutEdgeIt(i) { }
37 OutEdgeIt(const TrivGraphWrapper<Graph>& _G, const Node& n) :
38 Graph::OutEdgeIt(*(_G.graph), n) { }
40 class InEdgeIt : public Graph::InEdgeIt {
43 InEdgeIt(const typename Graph::InEdgeIt& e) : Graph::InEdgeIt(e) { }
44 InEdgeIt(const Invalid& i) : Graph::InEdgeIt(i) { }
45 InEdgeIt(const TrivGraphWrapper<Graph>& _G, const Node& n) :
46 Graph::InEdgeIt(*(_G.graph), n) { }
48 //typedef typename Graph::SymEdgeIt SymEdgeIt;
49 class EdgeIt : public Graph::EdgeIt {
52 EdgeIt(const typename Graph::EdgeIt& e) : Graph::EdgeIt(e) { }
53 EdgeIt(const Invalid& i) : Graph::EdgeIt(i) { }
54 EdgeIt(const TrivGraphWrapper<Graph>& _G) :
55 Graph::EdgeIt(*(_G.graph)) { }
58 NodeIt& first(NodeIt& i) const {
62 EdgeIt& first(EdgeIt& i) const {
66 // template<typename I> I& first(I& i) const {
70 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
71 i=OutEdgeIt(*this, p);
74 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
78 // template<typename I, typename P> I& first(I& i, const P& p) const {
83 // template<typename I> I getNext(const I& i) const {
84 // return graph->getNext(i); }
85 template<typename I> I& next(I &i) const { graph->next(i); return i; }
87 template< typename It > It first() const {
88 It e; this->first(e); return e; }
90 template< typename It > It first(const Node& v) const {
91 It e; this->first(e, v); return e; }
93 Node target(const Edge& e) const { return graph->target(e); }
94 Node source(const Edge& e) const { return graph->source(e); }
96 template<typename I> bool valid(const I& i) const {
97 return graph->valid(i); }
99 //template<typename I> void setInvalid(const I &i);
100 //{ return graph->setInvalid(i); }
102 int nodeNum() const { return graph->nodeNum(); }
103 int edgeNum() const { return graph->edgeNum(); }
105 template<typename I> Node aNode(const I& e) const {
106 return graph->aNode(e); }
107 template<typename I> Node bNode(const I& e) const {
108 return graph->bNode(e); }
110 Node addNode() const { return graph->addNode(); }
111 Edge addEdge(const Node& source, const Node& target) const {
112 return graph->addEdge(source, target); }
114 template<typename I> void erase(const I& i) const { graph->erase(i); }
116 void clear() const { graph->clear(); }
118 template<typename T> class NodeMap : public Graph::NodeMap<T> {
120 NodeMap(const TrivGraphWrapper<Graph>& _G) :
121 Graph::NodeMap<T>(*(_G.graph)) { }
122 NodeMap(const TrivGraphWrapper<Graph>& _G, T a) :
123 Graph::NodeMap<T>(*(_G.graph), a) { }
126 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
128 EdgeMap(const TrivGraphWrapper<Graph>& _G) :
129 Graph::EdgeMap<T>(*(_G.graph)) { }
130 EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) :
131 Graph::EdgeMap<T>(*(_G.graph), a) { }
134 template<typename Map, typename T> class NodeMapWrapper {
138 NodeMapWrapper(Map& _map) : map(&_map) { }
139 void set(Node n, T a) { map->set(n, a); }
140 T get(Node n) const { return map->get(n); }
143 template<typename Map, typename T> class EdgeMapWrapper {
147 EdgeMapWrapper(Map& _map) : map(&_map) { }
148 void set(Edge n, T a) { map->set(n, a); }
149 T get(Edge n) const { return map->get(n); }
154 template<typename Graph>
160 typedef Graph BaseGraph;
162 // GraphWrapper() : graph(0) { }
163 GraphWrapper(Graph& _graph) : graph(&_graph) { }
164 // void setGraph(Graph& _graph) { graph=&_graph; }
165 // Graph& getGraph() const { return *graph; }
167 typedef typename Graph::Node Node;
168 class NodeIt : public Graph::NodeIt {
171 NodeIt(const typename Graph::NodeIt& n) : Graph::NodeIt(n) { }
172 NodeIt(const Invalid& i) : Graph::NodeIt(i) { }
173 NodeIt(const GraphWrapper<Graph>& _G) :
174 Graph::NodeIt(*(_G.graph)) { }
176 typedef typename Graph::Edge Edge;
177 class OutEdgeIt : public Graph::OutEdgeIt {
180 OutEdgeIt(const typename Graph::OutEdgeIt& e) : Graph::OutEdgeIt(e) { }
181 OutEdgeIt(const Invalid& i) : Graph::OutEdgeIt(i) { }
182 OutEdgeIt(const GraphWrapper<Graph>& _G, const Node& n) :
183 Graph::OutEdgeIt(*(_G.graph), n) { }
185 class InEdgeIt : public Graph::InEdgeIt {
188 InEdgeIt(const typename Graph::InEdgeIt& e) : Graph::InEdgeIt(e) { }
189 InEdgeIt(const Invalid& i) : Graph::InEdgeIt(i) { }
190 InEdgeIt(const GraphWrapper<Graph>& _G, const Node& n) :
191 Graph::InEdgeIt(*(_G.graph), n) { }
193 //typedef typename Graph::SymEdgeIt SymEdgeIt;
194 class EdgeIt : public Graph::EdgeIt {
197 EdgeIt(const typename Graph::EdgeIt& e) : Graph::EdgeIt(e) { }
198 EdgeIt(const Invalid& i) : Graph::EdgeIt(i) { }
199 EdgeIt(const GraphWrapper<Graph>& _G) :
200 Graph::EdgeIt(*(_G.graph)) { }
203 NodeIt& first(NodeIt& i) const {
207 EdgeIt& first(EdgeIt& i) const {
211 // template<typename I> I& first(I& i) const {
215 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
216 i=OutEdgeIt(*this, p);
219 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
220 i=InEdgeIt(*this, p);
223 // template<typename I, typename P> I& first(I& i, const P& p) const {
228 // template<typename I> I getNext(const I& i) const {
229 // return gw.getNext(i); }
230 template<typename I> I& next(I &i) const { graph->next(i); return i; }
232 template< typename It > It first() const {
233 It e; this->first(e); return e; }
235 template< typename It > It first(const Node& v) const {
236 It e; this->first(e, v); return e; }
238 Node target(const Edge& e) const { return graph->target(e); }
239 Node source(const Edge& e) const { return graph->source(e); }
241 template<typename I> bool valid(const I& i) const {
242 return graph->valid(i); }
244 //template<typename I> void setInvalid(const I &i);
245 //{ return graph->setInvalid(i); }
247 int nodeNum() const { return graph->nodeNum(); }
248 int edgeNum() const { return graph->edgeNum(); }
250 template<typename I> Node aNode(const I& e) const {
251 return graph->aNode(e); }
252 template<typename I> Node bNode(const I& e) const {
253 return graph->bNode(e); }
255 Node addNode() const { return graph->addNode(); }
256 Edge addEdge(const Node& source, const Node& target) const {
257 return graph->addEdge(source, target); }
259 template<typename I> void erase(const I& i) const { graph->erase(i); }
261 void clear() const { graph->clear(); }
263 template<typename T> class NodeMap : public Graph::NodeMap<T> {
265 NodeMap(const GraphWrapper<Graph>& _G) :
266 Graph::NodeMap<T>(*(_G.graph)) { }
267 NodeMap(const GraphWrapper<Graph>& _G, T a) :
268 Graph::NodeMap<T>(*(_G.graph), a) { }
271 template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
273 EdgeMap(const GraphWrapper<Graph>& _G) :
274 Graph::EdgeMap<T>(*(_G.graph)) { }
275 EdgeMap(const GraphWrapper<Graph>& _G, T a) :
276 Graph::EdgeMap<T>(*(_G.graph), a) { }
281 // template<typename Graph>
282 // class RevGraphWrapper
288 // typedef Graph BaseGraph;
290 // typedef typename Graph::Node Node;
291 // typedef typename Graph::NodeIt NodeIt;
293 // typedef typename Graph::Edge Edge;
294 // typedef typename Graph::OutEdgeIt InEdgeIt;
295 // typedef typename Graph::InEdgeIt OutEdgeIt;
296 // //typedef typename Graph::SymEdgeIt SymEdgeIt;
297 // typedef typename Graph::EdgeIt EdgeIt;
299 // //RevGraphWrapper() : graph(0) { }
300 // RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
302 // void setGraph(Graph& _graph) { graph = &_graph; }
303 // Graph& getGraph() const { return (*graph); }
305 // template<typename I> I& first(I& i) const { return graph->first(i); }
306 // template<typename I, typename P> I& first(I& i, const P& p) const {
307 // return graph->first(i, p); }
309 // template<typename I> I getNext(const I& i) const {
310 // return graph->getNext(i); }
311 // template<typename I> I& next(I &i) const { return graph->next(i); }
313 // template< typename It > It first() const {
314 // It e; first(e); return e; }
316 // template< typename It > It first(const Node& v) const {
317 // It e; first(e, v); return e; }
319 // Node target(const Edge& e) const { return graph->source(e); }
320 // Node source(const Edge& e) const { return graph->target(e); }
322 // template<typename I> bool valid(const I& i) const
323 // { return graph->valid(i); }
325 // //template<typename I> void setInvalid(const I &i);
326 // //{ return graph->setInvalid(i); }
328 // template<typename I> Node aNode(const I& e) const {
329 // return graph->aNode(e); }
330 // template<typename I> Node bNode(const I& e) const {
331 // return graph->bNode(e); }
333 // Node addNode() const { return graph->addNode(); }
334 // Edge addEdge(const Node& source, const Node& target) const {
335 // return graph->addEdge(source, target); }
337 // int nodeNum() const { return graph->nodeNum(); }
338 // int edgeNum() const { return graph->edgeNum(); }
340 // template<typename I> void erase(const I& i) const { graph->erase(i); }
342 // void clear() const { graph->clear(); }
344 // template<typename T> class NodeMap : public Graph::NodeMap<T> {
346 // NodeMap(const RevGraphWrapper<Graph>& _G) :
347 // Graph::NodeMap<T>(_G.getGraph()) { }
348 // NodeMap(const RevGraphWrapper<Graph>& _G, T a) :
349 // Graph::NodeMap<T>(_G.getGraph(), a) { }
352 // template<typename T> class EdgeMap : public Graph::EdgeMap<T> {
354 // EdgeMap(const RevGraphWrapper<Graph>& _G) :
355 // Graph::EdgeMap<T>(_G.getGraph()) { }
356 // EdgeMap(const RevGraphWrapper<Graph>& _G, T a) :
357 // Graph::EdgeMap<T>(_G.getGraph(), a) { }
362 template<typename Graph>
363 class RevGraphWrapper : public GraphWrapper<Graph> {
365 typedef typename GraphWrapper<Graph>::Node Node;
366 typedef typename GraphWrapper<Graph>::Edge Edge;
368 //If Graph::OutEdgeIt is not defined
369 //and we do not want to use RevGraphWrapper::InEdgeIt,
370 //this won't work, because of typedef
372 //graphs have to define their non-existing iterators to void
373 //Unfortunately all the typedefs are instantiated in templates,
375 typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
376 typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
378 // RevGraphWrapper() : GraphWrapper<Graph>() { }
379 RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
381 Node target(const Edge& e) const
382 { return GraphWrapper<Graph>::source(e); }
383 Node source(const Edge& e) const
384 { return GraphWrapper<Graph>::target(e); }
387 //Subgraph on the same node-set and partial edge-set
388 template<typename Graph, typename EdgeFilterMap>
389 class SubGraphWrapper : public GraphWrapper<Graph> {
391 EdgeFilterMap* filter_map;
393 typedef typename GraphWrapper<Graph>::Node Node;
394 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
395 typedef typename GraphWrapper<Graph>::Edge Edge;
396 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
397 typedef typename GraphWrapper<Graph>::InEdgeIt InEdgeIt;
398 typedef typename GraphWrapper<Graph>::OutEdgeIt OutEdgeIt;
400 // SubGraphWrapper() : GraphWrapper<Graph>(), filter_map(0) { }
401 SubGraphWrapper(Graph& _graph, EdgeFilterMap& _filter_map) :
402 GraphWrapper<Graph>(_graph), filter_map(&_filter_map) { }
404 template<typename I> I& first(I& i) const {
406 while (graph->valid(i) && !filter_map->get(i)) { graph->next(i); }
409 template<typename I, typename P> I& first(I& i, const P& p) const {
411 while (graph->valid(i) && !filter_map->get(i)) { graph->next(i); }
415 //template<typename I> I getNext(const I& i) const {
416 // return gw.getNext(i);
418 template<typename I> I& next(I &i) const {
420 while (graph->valid(i) && !filter_map->get(i)) { graph->next(i); }
424 template< typename It > It first() const {
425 It e; this->first(e); return e; }
427 template< typename It > It first(const Node& v) const {
428 It e; this->first(e, v); return e; }
431 // template<typename GraphWrapper>
432 // class UndirGraphWrapper {
438 // typedef GraphWrapper BaseGraph;
440 // typedef typename GraphWrapper::Node Node;
441 // typedef typename GraphWrapper::NodeIt NodeIt;
443 // //typedef typename Graph::Edge Edge;
444 // //typedef typename Graph::OutEdgeIt OutEdgeIt;
445 // //typedef typename Graph::InEdgeIt InEdgeIt;
446 // //typedef typename Graph::SymEdgeIt SymEdgeIt;
447 // //typedef typename Graph::EdgeIt EdgeIt;
450 // typedef typename GraphWrapper::Edge GraphEdge;
451 // typedef typename GraphWrapper::OutEdgeIt GraphOutEdgeIt;
452 // typedef typename GraphWrapper::InEdgeIt GraphInEdgeIt;
455 // //UndirGraphWrapper() : graph(0) { }
456 // UndirGraphWrapper(GraphWrapper _gw) : gw(_gw) { }
458 // //void setGraph(Graph& _graph) { graph = &_graph; }
459 // //Graph& getGraph() const { return (*graph); }
462 // friend class UndirGraphWrapper<GraphWrapper>;
463 // bool out_or_in; //true iff out
464 // GraphOutEdgeIt out;
467 // Edge() : out_or_in(), out(), in() { }
468 // Edge(const Invalid& i) : out_or_in(false), out(), in(i) { }
469 // operator GraphEdge() const {
470 // if (out_or_in) return(out); else return(in);
472 // friend bool operator==(const Edge& u, const Edge& v) {
474 // return (u.out_or_in && u.out==v.out);
476 // return (!u.out_or_in && u.in==v.in);
478 // friend bool operator!=(const Edge& u, const Edge& v) {
480 // return (!u.out_or_in || u.out!=v.out);
482 // return (u.out_or_in || u.in!=v.in);
486 // class OutEdgeIt : public Edge {
487 // friend class UndirGraphWrapper<GraphWrapper>;
489 // OutEdgeIt() : Edge() { }
490 // OutEdgeIt(const Invalid& i) : Edge(i) { }
491 // OutEdgeIt(const UndirGraphWrapper<GraphWrapper>& _G, const Node& n)
494 // _G.gw.first(out, n);
495 // if (!(_G.gw.valid(out))) {
497 // _G.gw.first(in, n);
502 // OutEdgeIt& first(OutEdgeIt& e, const Node& n) const {
504 // gw.first(e.out, n);
505 // if (!(gw.valid(e.out))) {
506 // e.out_or_in=false;
507 // gw.first(e.in, n);
512 // OutEdgeIt& next(OutEdgeIt& e) const {
513 // if (e.out_or_in) {
514 // Node n=gw.source(e.out);
516 // if (!gw.valid(e.out)) {
517 // e.out_or_in=false;
518 // gw.first(e.in, n);
526 // Node aNode(const OutEdgeIt& e) const {
527 // if (e.out_or_in) return gw.source(e); else return gw.target(e); }
528 // Node bNode(const OutEdgeIt& e) const {
529 // if (e.out_or_in) return gw.target(e); else return gw.source(e); }
531 // typedef OutEdgeIt InEdgeIt;
533 // template<typename I> I& first(I& i) const { return gw.first(i); }
534 // // template<typename I, typename P> I& first(I& i, const P& p) const {
535 // // return graph->first(i, p); }
537 // template<typename I> I getNext(const I& i) const {
538 // return gw.getNext(i); }
539 // template<typename I> I& next(I &i) const { return gw.next(i); }
541 // template< typename It > It first() const {
542 // It e; first(e); return e; }
544 // template< typename It > It first(const Node& v) const {
545 // It e; first(e, v); return e; }
547 // Node target(const Edge& e) const { return gw.target(e); }
548 // Node source(const Edge& e) const { return gw.source(e); }
550 // template<typename I> bool valid(const I& i) const
551 // { return gw.valid(i); }
553 // //template<typename I> void setInvalid(const I &i);
554 // //{ return graph->setInvalid(i); }
556 // int nodeNum() const { return gw.nodeNum(); }
557 // int edgeNum() const { return gw.edgeNum(); }
559 // // template<typename I> Node aNode(const I& e) const {
560 // // return graph->aNode(e); }
561 // // template<typename I> Node bNode(const I& e) const {
562 // // return graph->bNode(e); }
564 // Node addNode() const { return gw.addNode(); }
565 // // FIXME: ez igy nem jo, mert nem
566 // // Edge addEdge(const Node& source, const Node& target) const {
567 // // return graph->addEdge(source, target); }
569 // template<typename I> void erase(const I& i) const { gw.erase(i); }
571 // void clear() const { gw.clear(); }
573 // template<typename T> class NodeMap : public GraphWrapper::NodeMap<T> {
575 // NodeMap(const UndirGraphWrapper<GraphWrapper>& _G) :
576 // GraphWrapper::NodeMap<T>(_G.gw) { }
577 // NodeMap(const UndirGraphWrapper<GraphWrapper>& _G, T a) :
578 // GraphWrapper::NodeMap<T>(_G.gw, a) { }
581 // template<typename T> class EdgeMap : public GraphWrapper::EdgeMap<T> {
583 // EdgeMap(const UndirGraphWrapper<GraphWrapper>& _G) :
584 // GraphWrapper::EdgeMap<T>(_G.gw) { }
585 // EdgeMap(const UndirGraphWrapper<GraphWrapper>& _G, T a) :
586 // GraphWrapper::EdgeMap<T>(_G.gw, a) { }
591 template<typename Graph>
592 class UndirGraphWrapper : public GraphWrapper<Graph> {
594 typedef typename Graph::Edge GraphEdge;
595 typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
596 typedef typename Graph::InEdgeIt GraphInEdgeIt;
598 typedef typename GraphWrapper<Graph>::Node Node;
599 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
601 // UndirGraphWrapper() : GraphWrapper<Graph>() { }
602 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
605 friend class UndirGraphWrapper<Graph>;
607 bool out_or_in; //true iff out
611 Edge() : out_or_in(), out(), in() { }
612 Edge(const Invalid& i) : out_or_in(false), out(), in(i) { }
613 operator GraphEdge() const {
614 if (out_or_in) return(out); else return(in);
617 //2 edges are equal if they "refer" to the same physical edge
619 friend bool operator==(const Edge& u, const Edge& v) {
621 if (u.out_or_in) return (u.out==v.out); else return (u.out==v.in);
622 //return (u.out_or_in && u.out==v.out);
624 if (u.out_or_in) return (u.out==v.in); else return (u.in==v.in);
625 //return (!u.out_or_in && u.in==v.in);
627 friend bool operator!=(const Edge& u, const Edge& v) {
629 if (u.out_or_in) return (u.out!=v.out); else return (u.out!=v.in);
630 //return (!u.out_or_in || u.out!=v.out);
632 if (u.out_or_in) return (u.out!=v.in); else return (u.in!=v.in);
633 //return (u.out_or_in || u.in!=v.in);
637 class OutEdgeIt : public Edge {
638 friend class UndirGraphWrapper<Graph>;
640 OutEdgeIt() : Edge() { }
641 OutEdgeIt(const Invalid& i) : Edge(i) { }
642 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& n)
644 out_or_in=true; _G.graph->first(out, n);
645 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, n); }
649 typedef OutEdgeIt InEdgeIt;
651 class EdgeIt : public Edge {
652 friend class UndirGraphWrapper<Graph>;
656 EdgeIt() : Edge() { }
657 EdgeIt(const Invalid& i) : Edge(i) { }
658 EdgeIt(const UndirGraphWrapper<Graph>& _G)
663 if (_G.valid(v)) _G.graph->first(out); else out=INVALID;
664 while (_G.valid(v) && !_G.graph->valid(out)) {
666 if (_G.valid(v)) _G.graph->first(out);
671 OutEdgeIt& first(OutEdgeIt& e, const Node& n) const {
672 e.out_or_in=true; graph->first(e.out, n);
673 if (!(graph->valid(e.out))) { e.out_or_in=false; graph->first(e.in, n); }
677 EdgeIt& first(EdgeIt& e) const {
681 if (valid(e.v)) graph->first(e.out, e.v); else e.out=INVALID;
682 while (valid(e.v) && !graph->valid(e.out)) {
684 if (valid(e.v)) graph->first(e.out, e.v);
689 template<typename I> I& first(I& i) const { graph->first(i); return i; }
690 template<typename I, typename P> I& first(I& i, const P& p) const {
691 graph->first(i, p); return i; }
693 OutEdgeIt& next(OutEdgeIt& e) const {
695 Node n=graph->source(e.out);
697 if (!graph->valid(e.out)) { e.out_or_in=false; graph->first(e.in, n); }
704 EdgeIt& next(EdgeIt& e) const {
705 //NodeIt v=source(e);
707 while (valid(e.v) && !graph->valid(e.out)) {
709 if (valid(e.v)) graph->first(e.out, e.v);
714 template<typename I> I& next(I &i) const { return graph->next(i); }
715 // template<typename I> I getNext(const I& i) const { return gw.getNext(i); }
717 template< typename It > It first() const {
718 It e; this->first(e); return e; }
720 template< typename It > It first(const Node& v) const {
721 It e; this->first(e, v); return e; }
723 // Node target(const Edge& e) const { return gw.target(e); }
724 // Node source(const Edge& e) const { return gw.source(e); }
726 // template<typename I> bool valid(const I& i) const
727 // { return gw.valid(i); }
729 // int nodeNum() const { return gw.nodeNum(); }
730 // int edgeNum() const { return gw.edgeNum(); }
732 // template<typename I> Node aNode(const I& e) const {
733 // return graph->aNode(e); }
734 // template<typename I> Node bNode(const I& e) const {
735 // return graph->bNode(e); }
737 Node aNode(const OutEdgeIt& e) const {
738 if (e.out_or_in) return graph->source(e); else return graph->target(e); }
739 Node bNode(const OutEdgeIt& e) const {
740 if (e.out_or_in) return graph->target(e); else return graph->source(e); }
742 // Node addNode() const { return gw.addNode(); }
744 // FIXME: ez igy nem jo, mert nem
745 // Edge addEdge(const Node& source, const Node& target) const {
746 // return graph->addEdge(source, target); }
748 // template<typename I> void erase(const I& i) const { gw.erase(i); }
750 // void clear() const { gw.clear(); }
752 // template<typename T> class NodeMap : public Graph::NodeMap<T> {
754 // NodeMap(const UndirGraphWrapper<Graph>& _G) :
755 // Graph::NodeMap<T>(_G.gw) { }
756 // NodeMap(const UndirGraphWrapper<Graph>& _G, T a) :
757 // Graph::NodeMap<T>(_G.gw, a) { }
760 // template<typename T> class EdgeMap :
761 // public GraphWrapper<Graph>::EdgeMap<T> {
763 // EdgeMap(const UndirGraphWrapper<Graph>& _G) :
764 // GraphWrapper<Graph>::EdgeMap<T>(_G.gw) { }
765 // EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) :
766 // Graph::EdgeMap<T>(_G.gw, a) { }
774 // template<typename Graph>
775 // class SymGraphWrapper
780 // typedef Graph BaseGraph;
782 // typedef typename Graph::Node Node;
783 // typedef typename Graph::Edge Edge;
785 // typedef typename Graph::NodeIt NodeIt;
787 // //FIXME tag-ekkel megcsinalni, hogy abbol csinaljon
788 // //iranyitatlant, ami van
789 // //mert csak 1 dolgot lehet be typedef-elni
790 // typedef typename Graph::OutEdgeIt SymEdgeIt;
791 // //typedef typename Graph::InEdgeIt SymEdgeIt;
792 // //typedef typename Graph::SymEdgeIt SymEdgeIt;
793 // typedef typename Graph::EdgeIt EdgeIt;
795 // int nodeNum() const { return graph->nodeNum(); }
796 // int edgeNum() const { return graph->edgeNum(); }
798 // template<typename I> I& first(I& i) const { return graph->first(i); }
799 // template<typename I, typename P> I& first(I& i, const P& p) const {
800 // return graph->first(i, p); }
801 // //template<typename I> I next(const I i); { return graph->goNext(i); }
802 // //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
804 // template< typename It > It first() const {
805 // It e; first(e); return e; }
807 // template< typename It > It first(Node v) const {
808 // It e; first(e, v); return e; }
810 // Node target(const Edge& e) const { return graph->target(e); }
811 // Node source(const Edge& e) const { return graph->source(e); }
813 // template<typename I> Node aNode(const I& e) const {
814 // return graph->aNode(e); }
815 // template<typename I> Node bNode(const I& e) const {
816 // return graph->bNode(e); }
818 // //template<typename I> bool valid(const I i);
819 // //{ return graph->valid(i); }
821 // //template<typename I> void setInvalid(const I &i);
822 // //{ return graph->setInvalid(i); }
824 // Node addNode() { return graph->addNode(); }
825 // Edge addEdge(const Node& source, const Node& target) {
826 // return graph->addEdge(source, target); }
828 // template<typename I> void erase(const I& i) { graph->erase(i); }
830 // void clear() { graph->clear(); }
832 // template<typename T> class NodeMap : public Graph::NodeMap<T> { };
833 // template<typename T> class EdgeMap : public Graph::EdgeMap<T> { };
835 // void setGraph(Graph& _graph) { graph = &_graph; }
836 // Graph& getGraph() { return (*graph); }
838 // //SymGraphWrapper() : graph(0) { }
839 // SymGraphWrapper(Graph& _graph) : graph(&_graph) { }
843 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
844 class ResGraphWrapper : public GraphWrapper<Graph>{
846 typedef typename GraphWrapper<Graph>::Node Node;
847 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
849 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
850 typedef typename Graph::InEdgeIt OldInEdgeIt;
852 const CapacityMap* capacity;
855 ResGraphWrapper(Graph& _graph, FlowMap& _flow,
856 const CapacityMap& _capacity) :
857 GraphWrapper<Graph>(_graph), flow(&_flow), capacity(&_capacity) { }
862 friend class OutEdgeIt;
865 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
867 bool out_or_in; //true, iff out
871 Edge() : out_or_in(true) { }
872 Edge(const Invalid& i) : out_or_in(false), out(), in(i) { }
873 // bool valid() const {
874 // return out_or_in && out.valid() || in.valid(); }
875 friend bool operator==(const Edge& u, const Edge& v) {
877 return (u.out_or_in && u.out==v.out);
879 return (!u.out_or_in && u.in==v.in);
881 friend bool operator!=(const Edge& u, const Edge& v) {
883 return (!u.out_or_in || u.out!=v.out);
885 return (u.out_or_in || u.in!=v.in);
890 class OutEdgeIt : public Edge {
891 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
895 OutEdgeIt(const Edge& e) : Edge(e) { }
896 OutEdgeIt(const Invalid& i) : Edge(i) { }
898 OutEdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG, Node v) : Edge() {
899 resG.graph->first(out, v);
900 while( resG.graph->valid(out) && !(resG.resCap(out)>0) ) { resG.graph->next(out); }
901 if (!resG.graph->valid(out)) {
903 resG.graph->first(in, v);
904 while( resG.graph->valid(in) && !(resG.resCap(in)>0) ) { resG.graph->next(in); }
908 // OutEdgeIt& operator++() {
910 // Node v=/*resG->*/G->aNode(out);
912 // while( out.valid() && !(Edge::resCap()>0) ) { ++out; }
913 // if (!out.valid()) {
916 // while( in.valid() && !(Edge::resCap()>0) ) { ++in; }
920 // while( in.valid() && !(Edge::resCap()>0) ) { ++in; }
926 //FIXME This is just for having InEdgeIt
927 typedef void InEdgeIt;
929 class EdgeIt : public Edge {
930 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
934 //EdgeIt(const EdgeIt& e) : Edge(e), v(e.v) { }
935 EdgeIt(const Invalid& i) : Edge(i) { }
936 EdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG) : Edge() {
937 resG.graph->first(v);
938 if (resG.graph->valid(v)) resG.graph->first(out, v); else out=INVALID;
939 while (resG.graph->valid(out) && !(resG.resCap(out)>0) ) { resG.graph->next(out); }
940 while (resG.graph->valid(v) && !resG.graph->valid(out)) {
942 if (resG.graph->valid(v)) resG.graph->first(out, v);
943 while (resG.graph->valid(out) && !(resG.resCap(out)>0) ) { resG.graph->next(out); }
945 if (!resG.graph->valid(out)) {
947 resG.graph->first(v);
948 if (resG.graph->valid(v)) resG.graph->first(in, v); else in=INVALID;
949 while (resG.graph->valid(in) && !(resG.resCap(in)>0) ) { resG.graph->next(in); }
950 while (resG.graph->valid(v) && !resG.graph->valid(in)) {
952 if (resG.graph->valid(v)) resG.graph->first(in, v);
953 while (resG.graph->valid(in) && !(resG.resCap(in)>0) ) { resG.graph->next(in); }
957 // EdgeIt& operator++() {
960 // while (out.valid() && !(Edge::resCap()>0) ) { ++out; }
961 // while (v.valid() && !out.valid()) {
963 // if (v.valid()) G->first(out, v);
964 // while (out.valid() && !(Edge::resCap()>0) ) { ++out; }
966 // if (!out.valid()) {
969 // if (v.valid()) G->first(in, v); else in=OldInEdgeIt();
970 // while (in.valid() && !(Edge::resCap()>0) ) { ++in; }
971 // while (v.valid() && !in.valid()) {
973 // if (v.valid()) G->first(in, v);
974 // while (in.valid() && !(Edge::resCap()>0) ) { ++in; }
979 // while (in.valid() && !(Edge::resCap()>0) ) { ++in; }
980 // while (v.valid() && !in.valid()) {
982 // if (v.valid()) G->first(in, v);
983 // while (in.valid() && !(Edge::resCap()>0) ) { ++in; }
990 NodeIt& first(NodeIt& v) const { graph->first(v); return v; }
991 OutEdgeIt& first(OutEdgeIt& e, Node v) const {
992 e=OutEdgeIt(*this, v);
995 EdgeIt& first(EdgeIt& e) const {
1000 NodeIt& next(NodeIt& n) const { return graph->next(n); }
1002 OutEdgeIt& next(OutEdgeIt& e) const {
1004 Node v=graph->aNode(e.out);
1006 while( graph->valid(e.out) && !(resCap(e.out)>0) ) { graph->next(e.out); }
1007 if (!graph->valid(e.out)) {
1009 graph->first(e.in, v);
1010 while( graph->valid(e.in) && !(resCap(e.in)>0) ) { graph->next(e.in); }
1014 while( graph->valid(e.in) && !(resCap(e.in)>0) ) { graph->next(e.in); }
1019 EdgeIt& next(EdgeIt& e) const {
1022 while (graph->valid(e.out) && !(resCap(e.out)>0) ) { graph->next(e.out); }
1023 while (graph->valid(e.v) && !graph->valid(e.out)) {
1025 if (graph->valid(e.v)) graph->first(e.out, e.v);
1026 while (graph->valid(e.out) && !(resCap(e.out)>0) ) { graph->next(e.out); }
1028 if (!graph->valid(e.out)) {
1031 if (graph->valid(e.v)) graph->first(e.in, e.v); else e.in=INVALID;
1032 while (graph->valid(e.in) && !(resCap(e.in)>0) ) { graph->next(e.in); }
1033 while (graph->valid(e.v) && !graph->valid(e.in)) {
1035 if (graph->valid(e.v)) graph->first(e.in, e.v);
1036 while (graph->valid(e.in) && !(resCap(e.in)>0) ) { graph->next(e.in); }
1041 while (graph->valid(e.in) && !(resCap(e.in)>0) ) { graph->next(e.in); }
1042 while (graph->valid(e.v) && !graph->valid(e.in)) {
1044 if (graph->valid(e.v)) graph->first(e.in, e.v);
1045 while (graph->valid(e.in) && !(resCap(e.in)>0) ) { graph->next(e.in); }
1052 template< typename It >
1059 template< typename It >
1060 It first(Node v) const {
1066 Node source(Edge e) const {
1067 return ((e.out_or_in) ? graph->aNode(e.out) : graph->aNode(e.in)); }
1068 Node target(Edge e) const {
1069 return ((e.out_or_in) ? graph->bNode(e.out) : graph->bNode(e.in)); }
1071 Node aNode(OutEdgeIt e) const {
1072 return ((e.out_or_in) ? graph->aNode(e.out) : graph->aNode(e.in)); }
1073 Node bNode(OutEdgeIt e) const {
1074 return ((e.out_or_in) ? graph->bNode(e.out) : graph->bNode(e.in)); }
1076 int nodeNum() const { return graph->nodeNum(); }
1078 //int edgeNum() const { return graph->edgeNum(); }
1081 int id(Node v) const { return graph->id(v); }
1083 bool valid(Node n) const { return graph->valid(n); }
1084 bool valid(Edge e) const {
1085 return e.out_or_in ? graph->valid(e.out) : graph->valid(e.in); }
1087 void augment(const Edge& e, Number a) const {
1089 flow->set(e.out, flow->get(e.out)+a);
1091 flow->set(e.in, flow->get(e.in)-a);
1094 Number resCap(const Edge& e) const {
1096 return (capacity->get(e.out)-flow->get(e.out));
1098 return (flow->get(e.in));
1101 Number resCap(OldOutEdgeIt out) const {
1102 return (capacity->get(out)-flow->get(out));
1105 Number resCap(OldInEdgeIt in) const {
1106 return (flow->get(in));
1109 // template<typename T> class NodeMap : public Graph::NodeMap<T> {
1111 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G)
1112 // : Graph::NodeMap<T>(_G.gw) { }
1113 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G,
1114 // T a) : Graph::NodeMap<T>(_G.gw, a) { }
1117 // template <typename T>
1119 // typename Graph::NodeMap<T> node_map;
1121 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.graph)) { }
1122 // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.graph), a) { }
1123 // void set(Node nit, T a) { node_map.set(nit, a); }
1124 // T get(Node nit) const { return node_map.get(nit); }
1127 template <typename T>
1129 typename Graph::EdgeMap<T> forward_map, backward_map;
1131 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
1132 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
1133 void set(Edge e, T a) {
1135 forward_map.set(e.out, a);
1137 backward_map.set(e.in, a);
1141 return forward_map.get(e.out);
1143 return backward_map.get(e.in);
1148 //ErasingFirstGraphWrapper for blocking flows
1149 template<typename Graph, typename FirstOutEdgesMap>
1150 class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1152 FirstOutEdgesMap* first_out_edges;
1154 typedef typename GraphWrapper<Graph>::Node Node;
1155 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
1156 typedef typename GraphWrapper<Graph>::Edge Edge;
1157 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
1158 typedef typename GraphWrapper<Graph>::InEdgeIt InEdgeIt;
1159 typedef typename GraphWrapper<Graph>::OutEdgeIt OutEdgeIt;
1161 ErasingFirstGraphWrapper(Graph& _graph,
1162 FirstOutEdgesMap& _first_out_edges) :
1163 GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
1165 template<typename I> I& first(I& i) const {
1169 OutEdgeIt& first(OutEdgeIt& e, const Node& n) const {
1170 e=first_out_edges->get(n);
1173 template<typename I, typename P> I& first(I& i, const P& p) const {
1178 //template<typename I> I getNext(const I& i) const {
1179 // return gw.getNext(i);
1181 template<typename I> I& next(I &i) const {
1186 template< typename It > It first() const {
1187 It e; this->first(e); return e; }
1189 template< typename It > It first(const Node& v) const {
1190 It e; this->first(e, v); return e; }
1192 void erase(const OutEdgeIt& e) const {
1195 first_out_edges->set(this->source(e), f);
1199 // // FIXME: comparison should be made better!!!
1200 // template<typename Graph, typename T, typename LowerMap, typename FlowMap, typename UpperMap>
1201 // class ResGraphWrapper
1206 // typedef Graph BaseGraph;
1208 // typedef typename Graph::Node Node;
1209 // typedef typename Graph::Edge Edge;
1211 // typedef typename Graph::NodeIt NodeIt;
1213 // class OutEdgeIt {
1217 // typename Graph::OutEdgeIt o;
1218 // typename Graph::InEdgeIt i;
1224 // typename Graph::OutEdgeIt o;
1225 // typename Graph::InEdgeIt i;
1227 // typedef typename Graph::SymEdgeIt SymEdgeIt;
1228 // typedef typename Graph::EdgeIt EdgeIt;
1230 // int nodeNum() const { return gw.nodeNum(); }
1231 // int edgeNum() const { return gw.edgeNum(); }
1233 // Node& first(Node& n) const { return gw.first(n); }
1235 // // Edge and SymEdge is missing!!!!
1236 // // Edge <-> In/OutEdgeIt conversion is missing!!!!
1239 // OutEdgeIt& first(OutEdgeIt& e, const Node& n) const
1243 // while(gw.valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
1245 // if(!gw.valid(e.o)) {
1247 // while(gw.valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
1253 // OutEdgeIt &goNext(OutEdgeIt &e)
1255 // if(gw.valid(e.o)) {
1256 // while(gw.valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
1258 // if(gw.valid(e.o)) return e;
1259 // else gw.first(e.i,e.n);
1262 // while(gw.valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
1267 // OutEdgeIt Next(const OutEdgeIt &e) {OutEdgeIt t(e); return goNext(t);}
1269 // //bool valid(const OutEdgeIt e) { return gw.valid(e.o)||gw.valid(e.i);}
1272 // InEdgeIt& first(InEdgeIt& e, const Node& n) const
1276 // while(gw.valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
1278 // if(!gw.valid(e.i)) {
1280 // while(gw.valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
1286 // InEdgeIt &goNext(InEdgeIt &e)
1288 // if(gw.valid(e.i)) {
1289 // while(gw.valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
1291 // if(gw.valid(e.i)) return e;
1292 // else gw.first(e.o,e.n);
1295 // while(gw.valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
1300 // InEdgeIt Next(const InEdgeIt &e) {InEdgeIt t(e); return goNext(t);}
1302 // //bool valid(const InEdgeIt e) { return gw.valid(e.i)||gw.valid(e.o);}
1304 // //template<typename I> I &goNext(I &i); { return gw.goNext(i); }
1305 // //template<typename I> I next(const I i); { return gw.goNext(i); }
1307 // template< typename It > It first() const {
1308 // It e; first(e); return e; }
1310 // template< typename It > It first(Node v) const {
1311 // It e; first(e, v); return e; }
1313 // Node target(const Edge& e) const { return gw.target(e); }
1314 // Node source(const Edge& e) const { return gw.source(e); }
1316 // template<typename I> Node aNode(const I& e) const {
1317 // return gw.aNode(e); }
1318 // template<typename I> Node bNode(const I& e) const {
1319 // return gw.bNode(e); }
1321 // //template<typename I> bool valid(const I i);
1322 // //{ return gw.valid(i); }
1324 // //template<typename I> void setInvalid(const I &i);
1325 // //{ return gw.setInvalid(i); }
1327 // Node addNode() { return gw.addNode(); }
1328 // Edge addEdge(const Node& source, const Node& target) {
1329 // return gw.addEdge(source, target); }
1331 // template<typename I> void erase(const I& i) { gw.erase(i); }
1333 // void clear() { gw.clear(); }
1335 // template<typename S> class NodeMap : public Graph::NodeMap<S> { };
1336 // template<typename S> class EdgeMap : public Graph::EdgeMap<S> { };
1338 // void setGraph(Graph& _graph) { graph = &_graph; }
1339 // Graph& getGraph() { return (*graph); }
1341 // //ResGraphWrapper() : graph(0) { }
1342 // ResGraphWrapper(Graph& _graph) : graph(&_graph) { }
1347 #endif //LEMON_GRAPH_WRAPPER_H