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