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