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