COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/graph_wrapper.h @ 457:8fbd472b1a22

Last change on this file since 457:8fbd472b1a22 was 457:8fbd472b1a22, checked in by Alpar Juttner, 20 years ago

\author's added

File size: 52.2 KB
RevLine 
[174]1// -*- c++ -*-
[259]2#ifndef HUGO_GRAPH_WRAPPER_H
3#define HUGO_GRAPH_WRAPPER_H
[76]4
[457]5///ingroup gwrappers
6///\file
7///\brief Several graph wrappers.
8///
9///This file contains several useful graph wrapper functions.
10///
11///\author Marton Makai
12
[174]13#include <invalid.h>
[368]14#include <iter_map.h>
[174]15
[105]16namespace hugo {
[76]17
[438]18  // Graph wrappers
19
[406]20  /// \addtogroup gwrappers
[344]21  /// A main parts of HUGOlib are the different graph structures,
[335]22  /// generic graph algorithms, graph concepts which couple these, and
23  /// graph wrappers. While the previous ones are more or less clear, the
24  /// latter notion needs further explanation.
25  /// Graph wrappers are graph classes which serve for considering graph
[344]26  /// structures in different ways. A short example makes the notion much
27  /// clearer.
28  /// Suppose that we have an instance \c g of a directed graph
29  /// type say \c ListGraph and an algorithm
[335]30  /// \code template<typename Graph> int algorithm(const Graph&); \endcode
[344]31  /// is needed to run on the reversely oriented graph.
32  /// It may be expensive (in time or in memory usage) to copy
33  /// \c g with the reverse orientation.
[335]34  /// Thus, a wrapper class
35  /// \code template<typename Graph> class RevGraphWrapper; \endcode is used.
36  /// The code looks as follows
37  /// \code
38  /// ListGraph g;
39  /// RevGraphWrapper<ListGraph> rgw(g);
40  /// int result=algorithm(rgw);
41  /// \endcode
[344]42  /// After running the algorithm, the original graph \c g
43  /// remains untouched. Thus the graph wrapper used above is to consider the
44  /// original graph with reverse orientation.
[335]45  /// This techniques gives rise to an elegant code, and
46  /// based on stable graph wrappers, complex algorithms can be
47  /// implemented easily.
48  /// In flow, circulation and bipartite matching problems, the residual
[344]49  /// graph is of particular importance. Combining a wrapper implementing
50  /// this, shortest path algorithms and minimum mean cycle algorithms,
[335]51  /// a range of weighted and cardinality optimization algorithms can be
52  /// obtained. For lack of space, for other examples,
[344]53  /// the interested user is referred to the detailed documentation of graph
[335]54  /// wrappers.
[344]55  /// The behavior of graph wrappers can be very different. Some of them keep
[335]56  /// capabilities of the original graph while in other cases this would be
[344]57  /// meaningless. This means that the concepts that they are a model of depend
[335]58  /// on the graph wrapper, and the wrapped graph(s).
[344]59  /// If an edge of \c rgw is deleted, this is carried out by
60  /// deleting the corresponding edge of \c g. But for a residual
[335]61  /// graph, this operation has no sense.
62  /// Let we stand one more example here to simplify your work.
63  /// wrapper class
64  /// \code template<typename Graph> class RevGraphWrapper; \endcode
65  /// has constructor
[344]66  /// <tt> RevGraphWrapper(Graph& _g)</tt>.
[335]67  /// This means that in a situation,
[344]68  /// when a <tt> const ListGraph& </tt> reference to a graph is given,
69  /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
[335]70  /// \code
71  /// int algorithm1(const ListGraph& g) {
72  ///   RevGraphWrapper<const ListGraph> rgw(g);
73  ///   return algorithm2(rgw);
74  /// }
75  /// \endcode
[438]76
77  /// \addtogroup gwrappers
78  /// @{
79
80  ///Base type for the Graph Wrappers
81
82  ///This is the base type for the Graph Wrappers.
[457]83  ///\todo Some more docs...
84  ///
85  ///\author Marton Makai
86 
[303]87  template<typename Graph>
88  class GraphWrapper {
[212]89  protected:
[303]90    Graph* graph;
[212]91 
92  public:
[311]93    typedef Graph BaseGraph;
[303]94    typedef Graph ParentGraph;
[212]95
[303]96//     GraphWrapper() : graph(0) { }
97    GraphWrapper(Graph& _graph) : graph(&_graph) { }
98//     void setGraph(Graph& _graph) { graph=&_graph; }
99//     Graph& getGraph() const { return *graph; }
100 
[317]101//    typedef typename Graph::Node Node;
102    class Node : public Graph::Node {
103      friend class GraphWrapper<Graph>;
[265]104    public:
[317]105      Node() { }
106      Node(const typename Graph::Node& _n) : Graph::Node(_n) { }
107      Node(const Invalid& i) : Graph::Node(i) { }
108    };
109    class NodeIt {
110      friend class GraphWrapper<Graph>;
111      typename Graph::NodeIt n;
112     public:
[265]113      NodeIt() { }
[317]114      NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
115      NodeIt(const Invalid& i) : n(i) { }
116      NodeIt(const GraphWrapper<Graph>& _G) : n(*(_G.graph)) { }
117      operator Node() const { return Node(typename Graph::Node(n)); }
[265]118    };
[317]119//    typedef typename Graph::Edge Edge;
120    class Edge : public Graph::Edge {
121      friend class GraphWrapper<Graph>;
122    public:
123      Edge() { }
124      Edge(const typename Graph::Edge& _e) : Graph::Edge(_e) { }
125      Edge(const Invalid& i) : Graph::Edge(i) { }
126    };
127    class OutEdgeIt {
128      friend class GraphWrapper<Graph>;
129      typename Graph::OutEdgeIt e;
[265]130    public:
131      OutEdgeIt() { }
[317]132      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
133      OutEdgeIt(const Invalid& i) : e(i) { }
134      OutEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
135        e(*(_G.graph), typename Graph::Node(_n)) { }
136      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[265]137    };
[317]138    class InEdgeIt {
139      friend class GraphWrapper<Graph>;
140      typename Graph::InEdgeIt e;
[265]141    public:
142      InEdgeIt() { }
[317]143      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
144      InEdgeIt(const Invalid& i) : e(i) { }
145      InEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
146        e(*(_G.graph), typename Graph::Node(_n)) { }
147      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[265]148    };
[303]149    //typedef typename Graph::SymEdgeIt SymEdgeIt;
[317]150    class EdgeIt {
151      friend class GraphWrapper<Graph>;
152      typename Graph::EdgeIt e;
[265]153    public:
154      EdgeIt() { }
[317]155      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
156      EdgeIt(const Invalid& i) : e(i) { }
157      EdgeIt(const GraphWrapper<Graph>& _G) : e(*(_G.graph)) { }
158      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[265]159    };
[303]160   
161    NodeIt& first(NodeIt& i) const {
[317]162      i=NodeIt(*this); return i;
[265]163    }
[303]164    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
[317]165      i=OutEdgeIt(*this, p); return i;
[303]166    }
167    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
[317]168      i=InEdgeIt(*this, p); return i;
[303]169    }
[311]170    EdgeIt& first(EdgeIt& i) const {
[317]171      i=EdgeIt(*this); return i;
[311]172    }
[338]173
[317]174    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
175    OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
176    InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
177    EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }   
[212]178
[379]179    Node tail(const Edge& e) const {
180      return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
[317]181    Node head(const Edge& e) const {
182      return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
[212]183
[317]184    bool valid(const Node& n) const {
185      return graph->valid(static_cast<typename Graph::Node>(n)); }
186    bool valid(const Edge& e) const {
187      return graph->valid(static_cast<typename Graph::Edge>(e)); }
[212]188
[303]189    int nodeNum() const { return graph->nodeNum(); }
190    int edgeNum() const { return graph->edgeNum(); }
[212]191 
[317]192    Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
193    Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
194    Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
195    Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
[212]196 
[317]197    Node addNode() const { return Node(graph->addNode()); }
[212]198    Edge addEdge(const Node& tail, const Node& head) const {
[317]199      return Edge(graph->addEdge(tail, head)); }
200
201    void erase(const Node& i) const { graph->erase(i); }
202    void erase(const Edge& i) const { graph->erase(i); }
[212]203 
[303]204    void clear() const { graph->clear(); }
[212]205   
[389]206    template<typename T> class NodeMap : public Graph::template NodeMap<T> {
207      typedef typename Graph::template NodeMap<T> Parent;
[212]208    public:
[389]209      NodeMap(const GraphWrapper<Graph>& _G) :  Parent(*(_G.graph)) { }
210      NodeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
[212]211    };
212
[389]213    template<typename T> class EdgeMap : public Graph::template EdgeMap<T> {
214      typedef typename Graph::template EdgeMap<T> Parent;
[212]215    public:
[389]216      EdgeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
217      EdgeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
[212]218    };
219  };
220
[338]221  /// A graph wrapper which reverses the orientation of the edges.
[303]222
[338]223  /// A graph wrapper which reverses the orientation of the edges.
[457]224  ///
225  ///\author Marton Makai
[303]226  template<typename Graph>
227  class RevGraphWrapper : public GraphWrapper<Graph> {
[230]228  public:
[338]229
230    RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { } 
231
[303]232    typedef typename GraphWrapper<Graph>::Node Node;
233    typedef typename GraphWrapper<Graph>::Edge Edge;
234    //If Graph::OutEdgeIt is not defined
[279]235    //and we do not want to use RevGraphWrapper::InEdgeIt,
[338]236    //the typdef techinque does not work.
237    //Unfortunately all the typedefs are instantiated in templates.
238    //typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
239    //typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
[237]240
[338]241    class OutEdgeIt {
242      friend class GraphWrapper<Graph>;
243      friend class RevGraphWrapper<Graph>;
[379]244      typename Graph::InEdgeIt e;
[338]245    public:
246      OutEdgeIt() { }
[379]247      OutEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
[338]248      OutEdgeIt(const Invalid& i) : e(i) { }
249      OutEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
250        e(*(_G.graph), typename Graph::Node(_n)) { }
251      operator Edge() const { return Edge(typename Graph::Edge(e)); }
252    };
253    class InEdgeIt {
254      friend class GraphWrapper<Graph>;
255      friend class RevGraphWrapper<Graph>;
[379]256      typename Graph::OutEdgeIt e;
[338]257    public:
258      InEdgeIt() { }
[379]259      InEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
[338]260      InEdgeIt(const Invalid& i) : e(i) { }
261      InEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
262        e(*(_G.graph), typename Graph::Node(_n)) { }
263      operator Edge() const { return Edge(typename Graph::Edge(e)); }
264    };
[238]265
[338]266    using GraphWrapper<Graph>::first;
267    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
268      i=OutEdgeIt(*this, p); return i;
269    }
270    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
271      i=InEdgeIt(*this, p); return i;
272    }
273
274    using GraphWrapper<Graph>::next;
[389]275    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
276    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
[338]277
[389]278    Node aNode(const OutEdgeIt& e) const {
279      return Node(this->graph->aNode(e.e)); }
280    Node aNode(const InEdgeIt& e) const {
281      return Node(this->graph->aNode(e.e)); }
282    Node bNode(const OutEdgeIt& e) const {
283      return Node(this->graph->bNode(e.e)); }
284    Node bNode(const InEdgeIt& e) const {
285      return Node(this->graph->bNode(e.e)); }
[379]286
287    Node tail(const Edge& e) const {
288      return GraphWrapper<Graph>::head(e); }
289    Node head(const Edge& e) const {
290      return GraphWrapper<Graph>::tail(e); }
291
[76]292  };
293
[335]294  /// Wrapper for hiding nodes and edges from a graph.
295 
296  /// This wrapper shows a graph with filtered node-set and
[363]297  /// edge-set. The quick brown fox iterator jumps over
[335]298  /// the lazy dog nodes or edges if the values for them are false
299  /// in the bool maps.
[457]300  ///
301  ///\author Marton Makai
[311]302  template<typename Graph, typename NodeFilterMap,
303           typename EdgeFilterMap>
[303]304  class SubGraphWrapper : public GraphWrapper<Graph> {
[263]305  protected:
[311]306    NodeFilterMap* node_filter_map;
307    EdgeFilterMap* edge_filter_map;
[263]308  public:
[338]309
[311]310    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
311                    EdgeFilterMap& _edge_filter_map) :
312      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
313      edge_filter_map(&_edge_filter_map) { } 
[263]314
[317]315    typedef typename GraphWrapper<Graph>::Node Node;
316    class NodeIt {
317      friend class GraphWrapper<Graph>;
318      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
319      typename Graph::NodeIt n;
320     public:
[311]321      NodeIt() { }
[317]322      NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
323      NodeIt(const Invalid& i) : n(i) { }
[311]324      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
[317]325        n(*(_G.graph)) {
326        while (_G.graph->valid(n) && !(*(_G.node_filter_map))[n])
327          _G.graph->next(n);
[311]328      }
[317]329      operator Node() const { return Node(typename Graph::Node(n)); }
[311]330    };
[317]331    typedef typename GraphWrapper<Graph>::Edge Edge;
332    class OutEdgeIt {
333      friend class GraphWrapper<Graph>;
334      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
335      typename Graph::OutEdgeIt e;
[311]336    public:
337      OutEdgeIt() { }
[317]338      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
339      OutEdgeIt(const Invalid& i) : e(i) { }
340      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
341                const Node& _n) :
342        e(*(_G.graph), typename Graph::Node(_n)) {
343        while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
344          _G.graph->next(e);
[311]345      }
[317]346      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[311]347    };
[317]348    class InEdgeIt {
349      friend class GraphWrapper<Graph>;
350      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
351      typename Graph::InEdgeIt e;
[311]352    public:
353      InEdgeIt() { }
[317]354      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
355      InEdgeIt(const Invalid& i) : e(i) { }
[311]356      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
[317]357               const Node& _n) :
358        e(*(_G.graph), typename Graph::Node(_n)) {
359        while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
360          _G.graph->next(e);
[311]361      }
[317]362      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[311]363    };
[317]364    //typedef typename Graph::SymEdgeIt SymEdgeIt;
365    class EdgeIt {
366      friend class GraphWrapper<Graph>;
367      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
368      typename Graph::EdgeIt e;
[311]369    public:
370      EdgeIt() { }
[317]371      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
372      EdgeIt(const Invalid& i) : e(i) { }
[311]373      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
[317]374        e(*(_G.graph)) {
375        while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
376          _G.graph->next(e);
[311]377      }
[317]378      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[311]379    };
[317]380
381    NodeIt& first(NodeIt& i) const {
382      i=NodeIt(*this); return i;
[263]383    }
[317]384    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
385      i=OutEdgeIt(*this, p); return i;
[311]386    }
[317]387    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
388      i=InEdgeIt(*this, p); return i;
[311]389    }
[317]390    EdgeIt& first(EdgeIt& i) const {
391      i=EdgeIt(*this); return i;
[263]392    }
393   
[311]394    NodeIt& next(NodeIt& i) const {
[389]395      this->graph->next(i.n);
396      while (this->graph->valid(i) && !(*node_filter_map)[i.n]) {
397        this->graph->next(i.n); }
[311]398      return i;
399    }
400    OutEdgeIt& next(OutEdgeIt& i) const {
[389]401      this->graph->next(i.e);
402      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
403        this->graph->next(i.e); }
[311]404      return i;
405    }
406    InEdgeIt& next(InEdgeIt& i) const {
[389]407      this->graph->next(i.e);
408      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
409        this->graph->next(i.e); }
[311]410      return i;
411    }
412    EdgeIt& next(EdgeIt& i) const {
[389]413      this->graph->next(i.e);
414      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
415        this->graph->next(i.e); }
[311]416      return i;
417    }
418
[389]419    Node aNode(const OutEdgeIt& e) const {
420      return Node(this->graph->aNode(e.e)); }
421    Node aNode(const InEdgeIt& e) const {
422      return Node(this->graph->aNode(e.e)); }
423    Node bNode(const OutEdgeIt& e) const {
424      return Node(this->graph->bNode(e.e)); }
425    Node bNode(const InEdgeIt& e) const {
426      return Node(this->graph->bNode(e.e)); }
[323]427
[357]428    ///\todo
429    ///Some doki, please.
[323]430    void hide(const Node& n) const { node_filter_map->set(n, false); }
[357]431    ///\todo
432    ///Some doki, please.
[323]433    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
434
[357]435    ///\todo
436    ///Some doki, please.
[323]437    void unHide(const Node& n) const { node_filter_map->set(n, true); }
[357]438    ///\todo
439    ///Some doki, please.
[323]440    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
441
[357]442    ///\todo
443    ///Some doki, please.
[323]444    bool hidden(const Node& n) const { return (*node_filter_map)[n]; }
[357]445    ///\todo
446    ///Some doki, please.
[323]447    bool hidden(const Edge& e) const { return (*edge_filter_map)[e]; }
[263]448  };
[155]449
[356]450  /// A wrapper for forgetting the orientation of a graph.
[317]451
[356]452  /// A wrapper for getting an undirected graph by forgetting
453  /// the orientation of a directed one.
[303]454  template<typename Graph>
455  class UndirGraphWrapper : public GraphWrapper<Graph> {
456  public:
457    typedef typename GraphWrapper<Graph>::Node Node;
458    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
[317]459    typedef typename GraphWrapper<Graph>::Edge Edge;
460    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
[236]461
[303]462    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { } 
[158]463
[317]464    class OutEdgeIt {
[303]465      friend class UndirGraphWrapper<Graph>;
[158]466      bool out_or_in; //true iff out
[317]467      typename Graph::OutEdgeIt out;
468      typename Graph::InEdgeIt in;
[158]469    public:
[317]470      OutEdgeIt() { }
471      OutEdgeIt(const Invalid& i) : Edge(i) { }
472      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
473        out_or_in=true; _G.graph->first(out, _n);
474        if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);        }
[174]475      }
[317]476      operator Edge() const {
477        if (out_or_in) return Edge(out); else return Edge(in);
[158]478      }
479    };
480
[317]481//FIXME InEdgeIt
[238]482    typedef OutEdgeIt InEdgeIt;
483
[338]484    using GraphWrapper<Graph>::first;
485//     NodeIt& first(NodeIt& i) const {
486//       i=NodeIt(*this); return i;
487//     }
[317]488    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
489      i=OutEdgeIt(*this, p); return i;
490    }
491//FIXME
492//     InEdgeIt& first(InEdgeIt& i, const Node& p) const {
493//       i=InEdgeIt(*this, p); return i;
494//     }
[338]495//     EdgeIt& first(EdgeIt& i) const {
496//       i=EdgeIt(*this); return i;
497//     }
[238]498
[338]499    using GraphWrapper<Graph>::next;
500//     NodeIt& next(NodeIt& n) const {
501//       GraphWrapper<Graph>::next(n);
502//       return n;
503//     }
[158]504    OutEdgeIt& next(OutEdgeIt& e) const {
505      if (e.out_or_in) {
[389]506        typename Graph::Node n=this->graph->tail(e.out);
507        this->graph->next(e.out);
508        if (!this->graph->valid(e.out)) {
509          e.out_or_in=false; this->graph->first(e.in, n); }
[158]510      } else {
[389]511        this->graph->next(e.in);
[158]512      }
513      return e;
514    }
[317]515    //FIXME InEdgeIt
[338]516//     EdgeIt& next(EdgeIt& e) const {
517//       GraphWrapper<Graph>::next(n);
518// //      graph->next(e.e);
519//       return e;
520//     }
[238]521
522    Node aNode(const OutEdgeIt& e) const {
[389]523      if (e.out_or_in) return this->graph->tail(e); else
524        return this->graph->head(e); }
[238]525    Node bNode(const OutEdgeIt& e) const {
[389]526      if (e.out_or_in) return this->graph->head(e); else
527        return this->graph->tail(e); }
[338]528  };
[158]529 
[338]530  /// A wrapper for composing the residual graph for directed flow and circulation problems.
[238]531
[338]532  /// A wrapper for composing the residual graph for directed flow and circulation problems.
[330]533  template<typename Graph, typename Number,
534           typename CapacityMap, typename FlowMap>
[311]535  class ResGraphWrapper : public GraphWrapper<Graph> {
[199]536  protected:
[330]537    const CapacityMap* capacity;
[155]538    FlowMap* flow;
539  public:
[168]540
[330]541    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
542                    FlowMap& _flow) :
543      GraphWrapper<Graph>(_graph), capacity(&_capacity), flow(&_flow) { }
[168]544
[174]545    class Edge;
[155]546    class OutEdgeIt;
[174]547    friend class Edge;
[155]548    friend class OutEdgeIt;
[76]549
[311]550    typedef typename GraphWrapper<Graph>::Node Node;
551    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
[317]552    class Edge : public Graph::Edge {
[330]553      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
[155]554    protected:
[317]555      bool forward; //true, iff forward
556//      typename Graph::Edge e;
[155]557    public:
[317]558      Edge() { }
559      Edge(const typename Graph::Edge& _e, bool _forward) :
560        Graph::Edge(_e), forward(_forward) { }
561      Edge(const Invalid& i) : Graph::Edge(i), forward(false) { }
562//the unique invalid iterator
[174]563      friend bool operator==(const Edge& u, const Edge& v) {
[317]564        return (v.forward==u.forward &&
565                static_cast<typename Graph::Edge>(u)==
566                static_cast<typename Graph::Edge>(v));
[174]567      }
568      friend bool operator!=(const Edge& u, const Edge& v) {
[317]569        return (v.forward!=u.forward ||
570                static_cast<typename Graph::Edge>(u)!=
571                static_cast<typename Graph::Edge>(v));
[174]572      }
[155]573    };
[338]574
[317]575    class OutEdgeIt {
[330]576      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
[317]577    protected:
578      typename Graph::OutEdgeIt out;
579      typename Graph::InEdgeIt in;
580      bool forward;
[155]581    public:
582      OutEdgeIt() { }
[168]583      //FIXME
[317]584//      OutEdgeIt(const Edge& e) : Edge(e) { }
585      OutEdgeIt(const Invalid& i) : out(i), in(i), forward(false) { }
586//the unique invalid iterator
[330]587      OutEdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& resG, Node v) {
[317]588        forward=true;
[303]589        resG.graph->first(out, v);
[317]590        while( resG.graph->valid(out) && !(resG.resCap(*this)>0) ) { resG.graph->next(out); }
[303]591        if (!resG.graph->valid(out)) {
[317]592          forward=false;
[303]593          resG.graph->first(in, v);
[317]594          while( resG.graph->valid(in) && !(resG.resCap(*this)>0) ) { resG.graph->next(in); }
[155]595        }
596      }
[317]597      operator Edge() const {
598//      Edge e;
599//      e.forward=this->forward;
600//      if (this->forward) e=out; else e=in;
601//      return e;
602        if (this->forward)
603          return Edge(out, this->forward);
604        else
605          return Edge(in, this->forward);
606      }
607    };
[263]608
[317]609    class InEdgeIt {
[330]610      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
[317]611    protected:
612      typename Graph::OutEdgeIt out;
613      typename Graph::InEdgeIt in;
614      bool forward;
615    public:
616      InEdgeIt() { }
617      //FIXME
618//      OutEdgeIt(const Edge& e) : Edge(e) { }
619      InEdgeIt(const Invalid& i) : out(i), in(i), forward(false) { }
620//the unique invalid iterator
[330]621      InEdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& resG, Node v) {
[317]622        forward=true;
623        resG.graph->first(in, v);
624        while( resG.graph->valid(in) && !(resG.resCap(*this)>0) ) { resG.graph->next(in); }
625        if (!resG.graph->valid(in)) {
626          forward=false;
627          resG.graph->first(out, v);
628          while( resG.graph->valid(out) && !(resG.resCap(*this)>0) ) { resG.graph->next(out); }
629        }
630      }
631      operator Edge() const {
632//      Edge e;
633//      e.forward=this->forward;
634//      if (this->forward) e=out; else e=in;
635//      return e;
636        if (this->forward)
637          return Edge(in, this->forward);
638        else
639          return Edge(out, this->forward);
640      }
641    };
642
643    class EdgeIt {
[330]644      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
[317]645    protected:
646      typename Graph::EdgeIt e;
647      bool forward;
[155]648    public:
[174]649      EdgeIt() { }
[317]650      EdgeIt(const Invalid& i) : e(i), forward(false) { }
[330]651      EdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& resG) {
[317]652        forward=true;
653        resG.graph->first(e);
654        while (resG.graph->valid(e) && !(resG.resCap(*this)>0)) resG.graph->next(e);
655        if (!resG.graph->valid(e)) {
656          forward=false;
657          resG.graph->first(e);
658          while (resG.graph->valid(e) && !(resG.resCap(*this)>0)) resG.graph->next(e);
[155]659        }
660      }
[317]661      operator Edge() const {
662        return Edge(e, this->forward);
663      }
664    };
[155]665
[338]666    using GraphWrapper<Graph>::first;
667//     NodeIt& first(NodeIt& i) const {
668//       i=NodeIt(*this); return i;
669//     }
[311]670    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
[317]671      i=OutEdgeIt(*this, p); return i;
[311]672    }
[317]673//    FIXME not tested
674    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
675      i=InEdgeIt(*this, p); return i;
676    }
677    EdgeIt& first(EdgeIt& i) const {
678      i=EdgeIt(*this); return i;
[155]679    }
[338]680 
681    using GraphWrapper<Graph>::next;
682//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
[155]683    OutEdgeIt& next(OutEdgeIt& e) const {
[317]684      if (e.forward) {
[389]685        Node v=this->graph->aNode(e.out);
686        this->graph->next(e.out);
687        while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
688          this->graph->next(e.out); }
689        if (!this->graph->valid(e.out)) {
[317]690          e.forward=false;
[389]691          this->graph->first(e.in, v);
692          while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
693            this->graph->next(e.in); }
[155]694        }
695      } else {
[389]696        this->graph->next(e.in);
697        while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
698          this->graph->next(e.in); }
[155]699      }
700      return e;
701    }
[317]702//     FIXME Not tested
703    InEdgeIt& next(InEdgeIt& e) const {
704      if (e.forward) {
[389]705        Node v=this->graph->aNode(e.in);
706        this->graph->next(e.in);
707        while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
708          this->graph->next(e.in); }
709        if (!this->graph->valid(e.in)) {
[317]710          e.forward=false;
[389]711          this->graph->first(e.out, v);
712          while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
713            this->graph->next(e.out); }
[317]714        }
715      } else {
[389]716        this->graph->next(e.out);
717        while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
718          this->graph->next(e.out); }
[317]719      }
720      return e;
721    }
722    EdgeIt& next(EdgeIt& e) const {
723      if (e.forward) {
[389]724        this->graph->next(e.e);
725        while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
726          this->graph->next(e.e); }
727        if (!this->graph->valid(e.e)) {
[317]728          e.forward=false;
[389]729          this->graph->first(e.e);
730          while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
731            this->graph->next(e.e); }
[155]732        }
[317]733      } else {
[389]734        this->graph->next(e.e);
735        while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
736          this->graph->next(e.e); }
[155]737      }
[317]738      return e;
739    }
[76]740
[174]741    Node tail(Edge e) const {
[389]742      return ((e.forward) ? this->graph->tail(e) : this->graph->head(e)); }
[174]743    Node head(Edge e) const {
[389]744      return ((e.forward) ? this->graph->head(e) : this->graph->tail(e)); }
[76]745
[174]746    Node aNode(OutEdgeIt e) const {
[389]747      return ((e.forward) ? this->graph->aNode(e.out) :
748              this->graph->aNode(e.in)); }
[174]749    Node bNode(OutEdgeIt e) const {
[389]750      return ((e.forward) ? this->graph->bNode(e.out) :
751              this->graph->bNode(e.in)); }
[76]752
[376]753    Node aNode(InEdgeIt e) const {
[389]754      return ((e.forward) ? this->graph->aNode(e.in) :
755              this->graph->aNode(e.out)); }
[376]756    Node bNode(InEdgeIt e) const {
[389]757      return ((e.forward) ? this->graph->bNode(e.in) :
758              this->graph->bNode(e.out)); }
[376]759
[338]760//    int nodeNum() const { return graph->nodeNum(); }
[263]761    //FIXME
[338]762    void edgeNum() const { }
[303]763    //int edgeNum() const { return graph->edgeNum(); }
[263]764
765
[317]766//    int id(Node v) const { return graph->id(v); }
[155]767
[317]768    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
[174]769    bool valid(Edge e) const {
[389]770      return this->graph->valid(e);
[317]771        //return e.forward ? graph->valid(e.out) : graph->valid(e.in);
772    }
[155]773
[174]774    void augment(const Edge& e, Number a) const {
[317]775      if (e.forward) 
[303]776//      flow->set(e.out, flow->get(e.out)+a);
[317]777        flow->set(e, (*flow)[e]+a);
[168]778      else 
[303]779//      flow->set(e.in, flow->get(e.in)-a);
[317]780        flow->set(e, (*flow)[e]-a);
[168]781    }
782
[269]783    Number resCap(const Edge& e) const {
[317]784      if (e.forward)
[303]785//      return (capacity->get(e.out)-flow->get(e.out));
[317]786        return ((*capacity)[e]-(*flow)[e]);
[168]787      else
[303]788//      return (flow->get(e.in));
[317]789        return ((*flow)[e]);
[168]790    }
791
[317]792//     Number resCap(typename Graph::OutEdgeIt out) const {
793// //      return (capacity->get(out)-flow->get(out));
794//       return ((*capacity)[out]-(*flow)[out]);
795//     }
[168]796   
[317]797//     Number resCap(typename Graph::InEdgeIt in) const {
798// //      return (flow->get(in));
799//       return ((*flow)[in]);
800//     }
[168]801
[155]802    template <typename T>
803    class EdgeMap {
[389]804      typename Graph::template EdgeMap<T> forward_map, backward_map;
[155]805    public:
[330]806      EdgeMap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
807      EdgeMap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
[174]808      void set(Edge e, T a) {
[317]809        if (e.forward)
[155]810          forward_map.set(e.out, a);
811        else
812          backward_map.set(e.in, a);
813      }
[303]814      T operator[](Edge e) const {
[317]815        if (e.forward)
[303]816          return forward_map[e.out];
[155]817        else
[303]818          return backward_map[e.in];
[155]819      }
[303]820//       T get(Edge e) const {
821//      if (e.out_or_in)
822//        return forward_map.get(e.out);
823//      else
824//        return backward_map.get(e.in);
825//       }
[155]826    };
[168]827  };
828
[338]829  /// ErasingFirstGraphWrapper for blocking flows.
[317]830
[338]831  /// ErasingFirstGraphWrapper for blocking flows.
[457]832  ///
833  ///\author Marton Makai
[303]834  template<typename Graph, typename FirstOutEdgesMap>
835  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
[269]836  protected:
837    FirstOutEdgesMap* first_out_edges;
838  public:
[303]839    ErasingFirstGraphWrapper(Graph& _graph,
840                             FirstOutEdgesMap& _first_out_edges) :
841      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { } 
[269]842
[317]843    typedef typename GraphWrapper<Graph>::Node Node;
[338]844//     class NodeIt {
845//       friend class GraphWrapper<Graph>;
846//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
847//       typename Graph::NodeIt n;
848//      public:
849//       NodeIt() { }
850//       NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
851//       NodeIt(const Invalid& i) : n(i) { }
852//       NodeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
853//      n(*(_G.graph)) { }
854//       operator Node() const { return Node(typename Graph::Node(n)); }
855//     };
[317]856    typedef typename GraphWrapper<Graph>::Edge Edge;
857    class OutEdgeIt {
858      friend class GraphWrapper<Graph>;
859      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
860//      typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
861      typename Graph::OutEdgeIt e;
[311]862    public:
863      OutEdgeIt() { }
[317]864      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
865      OutEdgeIt(const Invalid& i) : e(i) { }
[311]866      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
[317]867                const Node& _n) :
868        e((*_G.first_out_edges)[_n]) { }
869      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[311]870    };
[317]871    class InEdgeIt {
872      friend class GraphWrapper<Graph>;
873      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
874//      typedef typename Graph::InEdgeIt GraphInEdgeIt;
875      typename Graph::InEdgeIt e;
[311]876    public:
877      InEdgeIt() { }
[317]878      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
879      InEdgeIt(const Invalid& i) : e(i) { }
[311]880      InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
[317]881               const Node& _n) :
882        e(*(_G.graph), typename Graph::Node(_n)) { }
883      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[311]884    };
885    //typedef typename Graph::SymEdgeIt SymEdgeIt;
[317]886    class EdgeIt {
887      friend class GraphWrapper<Graph>;
888      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
889//      typedef typename Graph::EdgeIt GraphEdgeIt;
890      typename Graph::EdgeIt e;
[311]891    public:
892      EdgeIt() { }
[317]893      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
894      EdgeIt(const Invalid& i) : e(i) { }
[311]895      EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
[317]896        e(*(_G.graph)) { }
897      operator Edge() const { return Edge(typename Graph::Edge(e)); }
[311]898    };
899
[338]900    using GraphWrapper<Graph>::first;
901//     NodeIt& first(NodeIt& i) const {
902//       i=NodeIt(*this); return i;
903//     }
[317]904    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
905      i=OutEdgeIt(*this, p); return i;
[269]906    }
[317]907    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
908      i=InEdgeIt(*this, p); return i;
[311]909    }
[317]910    EdgeIt& first(EdgeIt& i) const {
911      i=EdgeIt(*this); return i;
[311]912    }
913
[338]914    using GraphWrapper<Graph>::next;
915//    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
[389]916    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
917    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
918    EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }   
[317]919   
[389]920    Node aNode(const OutEdgeIt& e) const {
921      return Node(this->graph->aNode(e.e)); }
922    Node aNode(const InEdgeIt& e) const {
923      return Node(this->graph->aNode(e.e)); }
924    Node bNode(const OutEdgeIt& e) const {
925      return Node(this->graph->bNode(e.e)); }
926    Node bNode(const InEdgeIt& e) const {
927      return Node(this->graph->bNode(e.e)); }
[311]928
[269]929    void erase(const OutEdgeIt& e) const {
930      OutEdgeIt f=e;
931      this->next(f);
[317]932      first_out_edges->set(this->tail(e), f.e);
[269]933    }
934  };
935
[368]936  /// A wrapper for composing a bipartite graph.
[371]937  /// \c _graph have to be a reference to a graph of type \c Graph
938  /// and \c _s_false_t_true_map is an \c IterableBoolMap
[368]939  /// reference containing the elements for the
[371]940  /// color classes S and T. \c _graph is to be referred to an undirected
941  /// graph or a directed graph with edges oriented from S to T.
[457]942  ///
943  ///\author Marton Makai
[368]944  template<typename Graph>
945  class BipartiteGraphWrapper : public GraphWrapper<Graph> {
[389]946    typedef IterableBoolMap< typename Graph::template NodeMap<int> >
947    SFalseTTrueMap;
[368]948    SFalseTTrueMap* s_false_t_true_map;
[393]949
[368]950  public:
[409]951    //marci
952    //FIXME vhogy igy kellene, csak az en forditom nem eszi meg
953    //static const bool S_CLASS=false;
954    //static const bool T_CLASS=true;
[379]955   
[409]956    bool S_CLASS;
957    bool T_CLASS;
958
[368]959    BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
[409]960      : GraphWrapper<Graph>(_graph), s_false_t_true_map(&_s_false_t_true_map),
961      S_CLASS(false), T_CLASS(true) { }
[368]962    typedef typename GraphWrapper<Graph>::Node Node;
963    //using GraphWrapper<Graph>::NodeIt;
964    typedef typename GraphWrapper<Graph>::Edge Edge;
965    //using GraphWrapper<Graph>::EdgeIt;
[393]966    class ClassNodeIt;
967    friend class ClassNodeIt;
968    class OutEdgeIt;
969    friend class OutEdgeIt;
970    class InEdgeIt;
971    friend class InEdgeIt;
[379]972    class ClassNodeIt {
[393]973      friend class BipartiteGraphWrapper<Graph>;
974    protected:
[368]975      Node n;
976    public:
[379]977      ClassNodeIt() { }
978      ClassNodeIt(const Invalid& i) : n(i) { }
979      ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) {
980        _G.s_false_t_true_map->first(n, _class);
[368]981      }
[381]982      //FIXME needed in new concept, important here
983      ClassNodeIt(const Node& _n) : n(_n) { }
[368]984      operator Node() const { return n; }
985    };
[379]986//     class SNodeIt {
987//       Node n;
988//     public:
989//       SNodeIt() { }
990//       SNodeIt(const Invalid& i) : n(i) { }
991//       SNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
992//      _G.s_false_t_true_map->first(n, false);
993//       }
994//       operator Node() const { return n; }
995//     };
996//     class TNodeIt {
997//       Node n;
998//     public:
999//       TNodeIt() { }
1000//       TNodeIt(const Invalid& i) : n(i) { }
1001//       TNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
1002//      _G.s_false_t_true_map->first(n, true);
1003//       }
1004//       operator Node() const { return n; }
1005//     };
[368]1006    class OutEdgeIt {
[393]1007      friend class BipartiteGraphWrapper<Graph>;
1008    protected:
[368]1009      typename Graph::OutEdgeIt e;
1010    public:
1011      OutEdgeIt() { }
1012      OutEdgeIt(const Invalid& i) : e(i) { }
1013      OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
1014        if (!(*(_G.s_false_t_true_map))[_n])
[379]1015          e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
[368]1016        else
1017          e=INVALID;
1018      }
1019      operator Edge() const { return Edge(typename Graph::Edge(e)); }
1020    };
1021    class InEdgeIt {
[393]1022      friend class BipartiteGraphWrapper<Graph>;
1023    protected:
[368]1024      typename Graph::InEdgeIt e;
1025    public:
1026      InEdgeIt() { }
1027      InEdgeIt(const Invalid& i) : e(i) { }
1028      InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
1029        if ((*(_G.s_false_t_true_map))[_n])
[379]1030          e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
[368]1031        else
1032          e=INVALID;
1033      }
1034      operator Edge() const { return Edge(typename Graph::Edge(e)); }
1035    };
1036
1037    using GraphWrapper<Graph>::first;
[379]1038    ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
[393]1039      n=ClassNodeIt(*this, _class) ; return n; }
[379]1040//    SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; }
1041//    TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; }
1042    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1043      i=OutEdgeIt(*this, p); return i;
1044    }
1045    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1046      i=InEdgeIt(*this, p); return i;
1047    }
[368]1048
1049    using GraphWrapper<Graph>::next;
[379]1050    ClassNodeIt& next(ClassNodeIt& n) const {
[393]1051      this->s_false_t_true_map->next(n.n); return n;
[368]1052    }
[379]1053//     SNodeIt& next(SNodeIt& n) const {
1054//       this->s_false_t_true_map->next(n); return n;
1055//     }
1056//     TNodeIt& next(TNodeIt& n) const {
1057//       this->s_false_t_true_map->next(n); return n;
1058//     }
[389]1059    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
1060    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
[368]1061
1062    Node tail(const Edge& e) {
1063      if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
1064        return Node(this->graph->tail(e));
1065      else
1066        return Node(this->graph->head(e));     
1067    }
1068    Node head(const Edge& e) {
1069      if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
1070        return Node(this->graph->head(e));
1071      else
1072        return Node(this->graph->tail(e));     
1073    }
1074
1075    Node aNode(const OutEdgeIt& e) const {
1076      return Node(this->graph->aNode(e.e));
1077    }
1078    Node aNode(const InEdgeIt& e) const {
1079      return Node(this->graph->aNode(e.e));
1080    }
1081    Node bNode(const OutEdgeIt& e) const {
1082      return Node(this->graph->bNode(e.e));
1083    }
1084    Node bNode(const InEdgeIt& e) const {
1085      return Node(this->graph->bNode(e.e));
1086    }
[379]1087
1088    bool inSClass(const Node& n) const {
[381]1089      return !(*(this->s_false_t_true_map))[n];
[379]1090    }
1091    bool inTClass(const Node& n) const {
[381]1092      return (*(this->s_false_t_true_map))[n];
[379]1093    }
[368]1094  };
1095
[435]1096  template<typename Graph>
1097  class stGraphWrapper;
1098
1099//   template<typename Graph>
1100//   std::ostream&
1101//   operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) {
1102//     os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
1103//     return os;
1104//   }
1105//   template<typename Graph>
1106//   std::ostream&
1107//   operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) {
1108//     os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
1109//       " node: " << i.n << ")";
1110//     return os;
1111//   }
[341]1112
[379]1113  /// experimentral, do not try it.
1114  /// It eats a bipartite graph, oriented from S to T.
1115  /// Such one can be made e.g. by the above wrapper.
[457]1116  ///
1117  ///\author Marton Makai
[379]1118  template<typename Graph>
1119  class stGraphWrapper : public GraphWrapper<Graph> {
1120  public:
1121    class Node;
[381]1122    friend class Node;
[379]1123//GN, int
1124//0 normalis, 1 s, 2, true, ez az iteralasi sorrend,
1125//es a vege a false azaz (invalid, 3)   
1126    class NodeIt;
[381]1127    friend class NodeIt;
[379]1128//GNI, int
1129    class Edge;
[381]1130    friend class Edge;
[379]1131//GE, int, GN
1132//0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
1133//invalid: (invalid, 3, invalid)
1134    class OutEdgeIt;
[381]1135    friend class OutEdgeIt;
[379]1136//GO, int, GNI
1137//normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid)
1138//s-bol (invalid, 1, first), ... (invalid, 3, invalid)
1139//t-bol (invalid, 3, invalid)
1140    class InEdgeIt;
[381]1141    friend class InEdgeIt;
[379]1142//GI, int, GNI
1143//normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid)
1144//s-be (invalid, 3, invalid)
1145//t-be (invalid, 2, first), ... (invalid, 3, invalid)
1146    class EdgeIt;
[381]1147    friend class EdgeIt;
[379]1148//(first, 0, invalid) ...
1149//(invalid, 1, vmi)
1150//(invalid, 2, vmi)
1151//invalid, 3, invalid)
1152    template <typename T> class NodeMap;
[409]1153    template <typename T, typename Parent> class EdgeMap;
[341]1154
[379]1155//    template <typename T> friend class NodeMap;
1156//    template <typename T> friend class EdgeMap;
[341]1157
[379]1158    const Node S_NODE;
1159    const Node T_NODE;
[341]1160
[379]1161    static const bool S_CLASS=false;
1162    static const bool T_CLASS=true;
[341]1163
[379]1164    stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
1165                                    S_NODE(INVALID, 1),
1166                                    T_NODE(INVALID, 2) { }
[341]1167
[435]1168   
1169//    std::ostream&
1170//    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
1171//    friend std::ostream&
1172//    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
1173//    friend std::ostream&
1174//    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i);
1175
[379]1176    class Node : public Graph::Node {
1177    protected:
1178      friend class GraphWrapper<Graph>;
1179      friend class stGraphWrapper<Graph>;
[389]1180      template <typename T> friend class NodeMap;
[380]1181      friend class Edge;
1182      friend class OutEdgeIt;
1183      friend class InEdgeIt;
1184      friend class EdgeIt;
[379]1185      int spec;
1186    public:
1187      Node() { }
1188      Node(const typename Graph::Node& _n, int _spec=0) :
1189        Graph::Node(_n), spec(_spec) { }
1190      Node(const Invalid& i) : Graph::Node(i), spec(3) { }
1191      friend bool operator==(const Node& u, const Node& v) {
1192        return (u.spec==v.spec &&
1193                static_cast<typename Graph::Node>(u)==
1194                static_cast<typename Graph::Node>(v));
1195      }
1196      friend bool operator!=(const Node& u, const Node& v) {
1197        return (v.spec!=u.spec ||
1198                static_cast<typename Graph::Node>(u)!=
1199                static_cast<typename Graph::Node>(v));
[409]1200      }
[435]1201//      template<typename G>
1202//      friend std::ostream&
1203//      operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i);
1204      friend std::ostream& operator<< (std::ostream& os, const Node& i);
[379]1205      int getSpec() const { return spec; }
1206    };
[409]1207
[379]1208    class NodeIt {
1209      friend class GraphWrapper<Graph>;
1210      friend class stGraphWrapper<Graph>;
1211      typename Graph::NodeIt n;
1212      int spec;
1213     public:
1214      NodeIt() { }
1215      NodeIt(const typename Graph::NodeIt& _n, int _spec) :
1216        n(_n), spec(_spec) { }
1217      NodeIt(const Invalid& i) : n(i), spec(3) { }
[381]1218      NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
[409]1219        if (!_G.graph->valid(n)) spec=1;
[379]1220      }
1221      operator Node() const { return Node(n, spec); }
1222    };
[409]1223
[379]1224    class Edge : public Graph::Edge {
1225      friend class GraphWrapper<Graph>;
1226      friend class stGraphWrapper<Graph>;
[409]1227      template <typename T, typename Parent> friend class EdgeMap;
[379]1228      int spec;
1229      typename Graph::Node n;
1230    public:
1231      Edge() { }
1232      Edge(const typename Graph::Edge& _e, int _spec,
1233           const typename Graph::Node& _n) :
1234        Graph::Edge(_e), spec(_spec), n(_n) {
1235      }
1236      Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { }
1237      friend bool operator==(const Edge& u, const Edge& v) {
1238        return (u.spec==v.spec &&
1239                static_cast<typename Graph::Edge>(u)==
1240                static_cast<typename Graph::Edge>(v) &&
1241                u.n==v.n);
1242      }
1243      friend bool operator!=(const Edge& u, const Edge& v) {
1244        return (v.spec!=u.spec ||
1245                static_cast<typename Graph::Edge>(u)!=
1246                static_cast<typename Graph::Edge>(v) ||
1247                u.n!=v.n);
1248      }
[435]1249//      template<typename G>
1250//      friend std::ostream&
1251//      operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
1252      friend std::ostream& operator<< (std::ostream& os, const Edge& i);
[379]1253      int getSpec() const { return spec; }
1254    };
[409]1255
[379]1256    class OutEdgeIt {
1257      friend class GraphWrapper<Graph>;
1258      friend class stGraphWrapper<Graph>;
1259      typename Graph::OutEdgeIt e;
1260      int spec;
1261      typename Graph::ClassNodeIt n;
1262    public:
1263      OutEdgeIt() { }
1264      OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
1265                const typename Graph::ClassNodeIt& _n) :
1266        e(_e), spec(_spec), n(_n) {
1267      }
1268      OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
[381]1269      OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
[379]1270        switch (_n.spec) {
1271          case 0 :
[381]1272            if (_G.graph->inSClass(_n)) { //S, van normalis kiel
[379]1273              e=typename Graph::OutEdgeIt(*(_G.graph),
1274                                          typename Graph::Node(_n));
1275              spec=0;
1276              n=INVALID;
1277              if (!_G.graph->valid(e)) spec=3;
1278            } else { //T, specko kiel van
1279              e=INVALID;
1280              spec=2;
1281              n=_n;
1282            }
1283            break;
1284          case 1:
1285            e=INVALID;
1286            spec=1;
1287            _G.graph->first(n, S_CLASS); //s->vmi;
1288            if (!_G.graph->valid(n)) spec=3; //Ha S ures
1289            break;
1290          case 2:
1291            e=INVALID;
1292            spec=3;
1293            n=INVALID;
1294            break;
1295        }
1296      }
1297      operator Edge() const { return Edge(e, spec, n); }
1298    };
[409]1299
[379]1300    class InEdgeIt {
1301      friend class GraphWrapper<Graph>;
1302      friend class stGraphWrapper<Graph>;
1303      typename Graph::InEdgeIt e;
1304      int spec;
1305      typename Graph::ClassNodeIt n;
1306    public:
1307      InEdgeIt() { }
1308      InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
1309               const typename Graph::ClassNodeIt& _n) :
1310        e(_e), spec(_spec), n(_n) {
1311      }
1312      InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
[381]1313      InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
[379]1314        switch (_n.spec) {
1315          case 0 :
[381]1316            if (_G.graph->inTClass(_n)) { //T, van normalis beel
[379]1317              e=typename Graph::InEdgeIt(*(_G.graph),
1318                                         typename Graph::Node(_n));
1319              spec=0;
1320              n=INVALID;
1321              if (!_G.graph->valid(e)) spec=3;
1322            } else { //S, specko beel van
1323              e=INVALID;
1324              spec=1;
1325              n=_n;
1326            }
1327            break;
1328          case 1:
1329            e=INVALID;
1330            spec=3;
1331            n=INVALID;
[409]1332            break;
[379]1333          case 2:
1334            e=INVALID;
[409]1335            spec=2;
[379]1336            _G.graph->first(n, T_CLASS); //vmi->t;
1337            if (!_G.graph->valid(n)) spec=3; //Ha T ures
1338            break;
1339        }
1340      }
1341      operator Edge() const { return Edge(e, spec, n); }
1342    };
[409]1343
[379]1344    class EdgeIt {
1345      friend class GraphWrapper<Graph>;
1346      friend class stGraphWrapper<Graph>;
1347      typename Graph::EdgeIt e;
1348      int spec;
1349      typename Graph::ClassNodeIt n;
1350    public:
1351      EdgeIt() { }
1352      EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
1353             const typename Graph::ClassNodeIt& _n) :
1354        e(_e), spec(_spec), n(_n) { }
1355      EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
[381]1356      EdgeIt(const stGraphWrapper<Graph>& _G) :
[379]1357        e(*(_G.graph)), spec(0), n(INVALID) {
1358        if (!_G.graph->valid(e)) {
1359          spec=1;
1360          _G.graph->first(n, S_CLASS);
1361          if (!_G.graph->valid(n)) { //Ha S ures
1362            spec=2;
1363            _G.graph->first(n, T_CLASS);
1364            if (!_G.graph->valid(n)) { //Ha T ures
1365              spec=3;
1366            }
1367          }
1368        }
1369      }
1370      operator Edge() const { return Edge(e, spec, n); }
1371    };
[341]1372   
[379]1373    NodeIt& first(NodeIt& i) const {
1374      i=NodeIt(*this); return i;
1375    }
1376    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1377      i=OutEdgeIt(*this, p); return i;
1378    }
1379    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1380      i=InEdgeIt(*this, p); return i;
1381    }
1382    EdgeIt& first(EdgeIt& i) const {
1383      i=EdgeIt(*this); return i;
1384    }
[341]1385
[379]1386    NodeIt& next(NodeIt& i) const {
1387      switch (i.spec) {
1388        case 0:
[389]1389          this->graph->next(i.n);
1390          if (!this->graph->valid(i.n)) {
[379]1391            i.spec=1;
1392          }
1393          break;
1394        case 1:
1395          i.spec=2;
1396          break;
1397        case 2:
1398          i.spec=3;
1399          break;
1400      }
1401      return i;
1402    }
1403    OutEdgeIt& next(OutEdgeIt& i) const {
[393]1404      typename Graph::Node v;
[379]1405      switch (i.spec) {
1406        case 0: //normal edge
[409]1407          v=this->graph->aNode(i.e);
[389]1408          this->graph->next(i.e);
1409          if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
1410            if (this->graph->inSClass(v)) { //S, nincs kiel
[379]1411              i.spec=3;
1412              i.n=INVALID;
1413            } else { //T, van kiel
1414              i.spec=2;
1415              i.n=v;
1416            }
1417          }
1418          break;
1419        case 1: //s->vmi
[389]1420          this->graph->next(i.n);
1421          if (!this->graph->valid(i.n)) i.spec=3;
[379]1422          break;
1423        case 2: //vmi->t
1424          i.spec=3;
1425          i.n=INVALID;
1426          break;
1427      }
1428      return i;
1429    }
1430    InEdgeIt& next(InEdgeIt& i) const {
[393]1431      typename Graph::Node v;
[379]1432      switch (i.spec) {
1433        case 0: //normal edge
[393]1434          v=this->graph->aNode(i.e);
[389]1435          this->graph->next(i.e);
1436          if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
1437            if (this->graph->inTClass(v)) { //S, nincs beel
[379]1438              i.spec=3;
1439              i.n=INVALID;
1440            } else { //S, van beel
1441              i.spec=1;
1442              i.n=v;
1443            }
1444          }
1445          break;
1446        case 1: //s->vmi
1447          i.spec=3;
1448          i.n=INVALID;
1449          break;
1450        case 2: //vmi->t
[389]1451          this->graph->next(i.n);
1452          if (!this->graph->valid(i.n)) i.spec=3;
[379]1453          break;
1454      }
1455      return i;
1456    }
[341]1457
[379]1458    EdgeIt& next(EdgeIt& i) const {
1459      switch (i.spec) {
1460        case 0:
[389]1461          this->graph->next(i.e);
1462          if (!this->graph->valid(i.e)) {
[379]1463            i.spec=1;
[389]1464            this->graph->first(i.n, S_CLASS);
1465            if (!this->graph->valid(i.n)) {
[379]1466              i.spec=2;
[389]1467              this->graph->first(i.n, T_CLASS);
1468              if (!this->graph->valid(i.n)) i.spec=3;
[379]1469            }
1470          }
1471          break;
1472        case 1:
[389]1473          this->graph->next(i.n);
1474          if (!this->graph->valid(i.n)) {
[379]1475            i.spec=2;
[389]1476            this->graph->first(i.n, T_CLASS);
1477            if (!this->graph->valid(i.n)) i.spec=3;
[379]1478          }
1479          break;
1480        case 2:
[389]1481          this->graph->next(i.n);
1482          if (!this->graph->valid(i.n)) i.spec=3;
[379]1483          break;
1484      }
1485      return i;
1486    }   
[341]1487
[379]1488    Node tail(const Edge& e) const {
1489      switch (e.spec) {
[393]1490      case 0:
1491        return Node(this->graph->tail(e));
1492        break;
1493      case 1:
1494        return S_NODE;
1495        break;
1496      case 2:
1497      default:
1498        return Node(e.n);
1499        break;
[379]1500      }
1501    }
1502    Node head(const Edge& e) const {
1503      switch (e.spec) {
[393]1504      case 0:
1505        return Node(this->graph->head(e));
1506        break;
1507      case 1:
1508        return Node(e.n);
1509        break;
1510      case 2:
1511      default:
1512        return T_NODE;
1513        break;
[379]1514      }
1515    }
[341]1516
[379]1517    bool valid(const Node& n) const { return (n.spec<3); }
1518    bool valid(const Edge& e) const { return (e.spec<3); }
1519
[409]1520    int nodeNum() const { return this->graph->nodeNum()+2; }
1521    int edgeNum() const {
1522      return this->graph->edgeNum()+this->graph->nodeNum();
1523    }
[341]1524 
[379]1525    Node aNode(const OutEdgeIt& e) const { return tail(e); }
1526    Node aNode(const InEdgeIt& e) const { return head(e); }
1527    Node bNode(const OutEdgeIt& e) const { return head(e); }
1528    Node bNode(const InEdgeIt& e) const { return tail(e); }
[409]1529
1530    void addNode() const { }
1531    void addEdge() const { }
1532   
[389]1533//    Node addNode() const { return Node(this->graph->addNode()); }
[379]1534//    Edge addEdge(const Node& tail, const Node& head) const {
[389]1535//      return Edge(this->graph->addEdge(tail, head)); }
[341]1536
[389]1537//    void erase(const Node& i) const { this->graph->erase(i); }
1538//    void erase(const Edge& i) const { this->graph->erase(i); }
[341]1539 
[389]1540//    void clear() const { this->graph->clear(); }
[341]1541   
[389]1542    template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
1543      typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
[379]1544      T s_value, t_value;
1545    public:
[409]1546      NodeMap(const stGraphWrapper<Graph>& _G) :  Parent(_G),
1547                                                  s_value(),
1548                                                  t_value() { }
[389]1549      NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
1550                                                      s_value(a),
1551                                                      t_value(a) { }
[379]1552      T operator[](const Node& n) const {
1553        switch (n.spec) {
[393]1554        case 0:
1555          return Parent::operator[](n);
1556          break;
1557        case 1:
1558          return s_value;
1559          break;
1560        case 2:
1561        default:
1562          return t_value;
1563          break;
[379]1564        }
1565      }
1566      void set(const Node& n, T t) {
1567        switch (n.spec) {
[393]1568        case 0:
1569          GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
1570          break;
1571        case 1:
1572          s_value=t;
1573          break;
1574        case 2:
1575        default:
1576          t_value=t;
1577          break;
[379]1578        }
1579      }
1580    };
[341]1581
[409]1582    template<typename T,
1583             typename Parent=
1584             typename GraphWrapper<Graph>::template EdgeMap<T> >
1585    class EdgeMap : public Parent {
1586      //typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
[389]1587      typename GraphWrapper<Graph>::template NodeMap<T> node_value;
[379]1588    public:
[393]1589      EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
1590                                                 node_value(_G) { }
[389]1591      EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
1592                                                      node_value(_G, a) { }
[379]1593      T operator[](const Edge& e) const {
1594        switch (e.spec) {
[393]1595        case 0:
1596          return Parent::operator[](e);
1597          break;
1598        case 1:
1599          return node_value[e.n];
1600          break;
1601        case 2:
1602        default:
1603          return node_value[e.n];
1604          break;
[379]1605        }
1606      }
1607      void set(const Edge& e, T t) {
1608        switch (e.spec) {
[393]1609        case 0:
[409]1610          Parent::set(e, t);
[393]1611          break;
1612        case 1:
1613          node_value.set(e.n, t);
1614          break;
1615        case 2:
1616        default:
1617          node_value.set(e.n, t);
1618          break;
[379]1619        }
1620      }
1621    };
[409]1622
1623//     template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
1624//       typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
1625//       typename GraphWrapper<Graph>::template NodeMap<T> node_value;
1626//     public:
1627//       EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
1628//                                               node_value(_G) { }
1629//       EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
1630//                                                    node_value(_G, a) { }
1631//       T operator[](const Edge& e) const {
1632//      switch (e.spec) {
1633//      case 0:
1634//        return Parent::operator[](e);
1635//        break;
1636//      case 1:
1637//        return node_value[e.n];
1638//        break;
1639//      case 2:
1640//      default:
1641//        return node_value[e.n];
1642//        break;
1643//      }
1644//       }
1645//       void set(const Edge& e, T t) {
1646//      switch (e.spec) {
1647//      case 0:
1648//        GraphWrapper<Graph>::template EdgeMap<T>::set(e, t);
1649//        break;
1650//      case 1:
1651//        node_value.set(e.n, t);
1652//        break;
1653//      case 2:
1654//      default:
1655//        node_value.set(e.n, t);
1656//        break;
1657//      }
1658//       }
1659//     };
1660
[435]1661//  template<typename G>
1662    friend std::ostream&
1663    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
[409]1664      os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
1665      return os;
1666    }
[435]1667//  template<typename G>
1668    friend std::ostream&
1669    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
[409]1670      os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
1671        " node: " << i.n << ")";
1672      return os;
1673    }
1674
[379]1675  };
1676
[406]1677  ///@}
[341]1678
[105]1679} //namespace hugo
[76]1680
[406]1681
[259]1682#endif //HUGO_GRAPH_WRAPPER_H
[76]1683
Note: See TracBrowser for help on using the repository browser.