src/hugo/graph_wrapper.h
author marci
Wed, 01 Sep 2004 09:04:07 +0000
changeset 779 83c49c272679
parent 775 e46a1f0623a0
child 792 147eb3a58706
permissions -rw-r--r--
correction
marci@556
     1
// -*- c++ -*-
marci@556
     2
#ifndef HUGO_GRAPH_WRAPPER_H
marci@556
     3
#define HUGO_GRAPH_WRAPPER_H
marci@556
     4
marci@556
     5
///\ingroup gwrappers
marci@556
     6
///\file
marci@556
     7
///\brief Several graph wrappers.
marci@556
     8
///
marci@556
     9
///This file contains several useful graph wrapper functions.
marci@556
    10
///
marci@556
    11
///\author Marton Makai
marci@556
    12
marci@556
    13
#include <hugo/invalid.h>
marci@650
    14
#include <hugo/maps.h>
alpar@774
    15
#include <iostream>
marci@556
    16
marci@556
    17
namespace hugo {
marci@556
    18
marci@556
    19
  // Graph wrappers
marci@556
    20
marci@556
    21
  /// \addtogroup gwrappers
marci@556
    22
  /// A main parts of HUGOlib are the different graph structures, 
marci@556
    23
  /// generic graph algorithms, graph concepts which couple these, and 
marci@556
    24
  /// graph wrappers. While the previous ones are more or less clear, the 
marci@556
    25
  /// latter notion needs further explanation.
marci@556
    26
  /// Graph wrappers are graph classes which serve for considering graph 
marci@556
    27
  /// structures in different ways. A short example makes the notion much 
marci@556
    28
  /// clearer. 
marci@556
    29
  /// Suppose that we have an instance \c g of a directed graph
marci@556
    30
  /// type say \c ListGraph and an algorithm 
marci@556
    31
  /// \code template<typename Graph> int algorithm(const Graph&); \endcode 
marci@556
    32
  /// is needed to run on the reversely oriented graph. 
marci@556
    33
  /// It may be expensive (in time or in memory usage) to copy 
marci@556
    34
  /// \c g with the reverse orientation. 
marci@556
    35
  /// Thus, a wrapper class
marci@556
    36
  /// \code template<typename Graph> class RevGraphWrapper; \endcode is used. 
marci@556
    37
  /// The code looks as follows
marci@556
    38
  /// \code
marci@556
    39
  /// ListGraph g;
marci@556
    40
  /// RevGraphWrapper<ListGraph> rgw(g);
marci@556
    41
  /// int result=algorithm(rgw);
marci@556
    42
  /// \endcode
marci@556
    43
  /// After running the algorithm, the original graph \c g 
marci@556
    44
  /// remains untouched. Thus the graph wrapper used above is to consider the 
marci@556
    45
  /// original graph with reverse orientation. 
marci@556
    46
  /// This techniques gives rise to an elegant code, and 
marci@556
    47
  /// based on stable graph wrappers, complex algorithms can be 
marci@556
    48
  /// implemented easily. 
marci@556
    49
  /// In flow, circulation and bipartite matching problems, the residual 
marci@556
    50
  /// graph is of particular importance. Combining a wrapper implementing 
marci@556
    51
  /// this, shortest path algorithms and minimum mean cycle algorithms, 
marci@556
    52
  /// a range of weighted and cardinality optimization algorithms can be 
marci@556
    53
  /// obtained. For lack of space, for other examples, 
marci@556
    54
  /// the interested user is referred to the detailed documentation of graph 
marci@556
    55
  /// wrappers. 
marci@556
    56
  /// The behavior of graph wrappers can be very different. Some of them keep 
marci@556
    57
  /// capabilities of the original graph while in other cases this would be 
marci@556
    58
  /// meaningless. This means that the concepts that they are a model of depend 
marci@556
    59
  /// on the graph wrapper, and the wrapped graph(s). 
marci@556
    60
  /// If an edge of \c rgw is deleted, this is carried out by 
marci@556
    61
  /// deleting the corresponding edge of \c g. But for a residual 
marci@556
    62
  /// graph, this operation has no sense. 
marci@556
    63
  /// Let we stand one more example here to simplify your work. 
marci@556
    64
  /// wrapper class
marci@556
    65
  /// \code template<typename Graph> class RevGraphWrapper; \endcode 
marci@556
    66
  /// has constructor 
marci@556
    67
  /// <tt> RevGraphWrapper(Graph& _g)</tt>. 
marci@556
    68
  /// This means that in a situation, 
marci@556
    69
  /// when a <tt> const ListGraph& </tt> reference to a graph is given, 
marci@556
    70
  /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
marci@556
    71
  /// \code
marci@556
    72
  /// int algorithm1(const ListGraph& g) {
marci@556
    73
  ///   RevGraphWrapper<const ListGraph> rgw(g);
marci@556
    74
  ///   return algorithm2(rgw);
marci@556
    75
  /// }
marci@556
    76
  /// \endcode
marci@556
    77
marci@556
    78
  /// \addtogroup gwrappers
marci@556
    79
  /// @{
marci@556
    80
marci@556
    81
  ///Base type for the Graph Wrappers
marci@556
    82
marci@556
    83
  ///This is the base type for the Graph Wrappers.
marci@556
    84
  ///\todo Some more docs... 
marci@556
    85
  ///
marci@612
    86
  ///\author Marton Makai 
marci@556
    87
  template<typename Graph>
marci@556
    88
  class GraphWrapper {
marci@556
    89
  protected:
marci@556
    90
    Graph* graph;
marci@556
    91
    GraphWrapper() : graph(0) { }
marci@556
    92
    void setGraph(Graph& _graph) { graph=&_graph; }
marci@556
    93
marci@556
    94
  public:
marci@556
    95
    typedef Graph BaseGraph;
marci@556
    96
    typedef Graph ParentGraph;
marci@556
    97
marci@556
    98
    GraphWrapper(Graph& _graph) : graph(&_graph) { }
alpar@774
    99
    GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { }
marci@556
   100
//     Graph& getGraph() const { return *graph; }
marci@556
   101
 
alpar@774
   102
    typedef typename Graph::Node Node;
alpar@774
   103
//     class Node : public Graph::Node {
alpar@774
   104
//       friend class GraphWrapper<Graph>;
alpar@774
   105
//     public:
alpar@774
   106
//       Node() { }
alpar@774
   107
//       Node(const typename Graph::Node& _n) : Graph::Node(_n) { }
alpar@774
   108
//       // /// \bug construction throughrthr multiple levels should be 
alpar@774
   109
//       // /// handled better
alpar@774
   110
//       // Node(const typename ParentGraph::ParentGraph::Node& _n) : 
alpar@774
   111
//       // Graph::Node(_n) { }
alpar@774
   112
//       Node(const Invalid& i) : Graph::Node(i) { }
alpar@774
   113
//     };
alpar@774
   114
    class NodeIt : public Node { 
alpar@774
   115
      const GraphWrapper<Graph>* gw;
marci@556
   116
      friend class GraphWrapper<Graph>;
marci@556
   117
     public:
marci@556
   118
      NodeIt() { }
alpar@774
   119
      //      NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
alpar@774
   120
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   121
      NodeIt(const GraphWrapper<Graph>& _gw) : 
alpar@774
   122
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
alpar@774
   123
      NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   124
	Node(n), gw(&_gw) { }
alpar@774
   125
      NodeIt& operator++() { 
alpar@774
   126
	*(static_cast<Node*>(this))=
alpar@774
   127
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
alpar@774
   128
	return *this; 
alpar@774
   129
      }
marci@556
   130
    };
alpar@774
   131
    typedef typename Graph::Edge Edge;
alpar@774
   132
//     class Edge : public Graph::Edge {
alpar@774
   133
//       friend class GraphWrapper<Graph>;
alpar@774
   134
//     public:
alpar@774
   135
//       Edge() { }
alpar@774
   136
//       Edge(const typename Graph::Edge& _e) : Graph::Edge(_e) { }
alpar@774
   137
//       Edge(const Invalid& i) : Graph::Edge(i) { }
alpar@774
   138
//     };
alpar@774
   139
    class OutEdgeIt : public Edge { 
alpar@774
   140
      const GraphWrapper<Graph>* gw;
marci@556
   141
      friend class GraphWrapper<Graph>;
alpar@774
   142
     public:
alpar@774
   143
      OutEdgeIt() { }
alpar@774
   144
      //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   145
      OutEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   146
      OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   147
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   148
      OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   149
	Edge(e), gw(&_gw) { }
alpar@774
   150
      OutEdgeIt& operator++() { 
alpar@774
   151
	*(static_cast<Edge*>(this))=
alpar@774
   152
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   153
	return *this; 
alpar@774
   154
      }
marci@556
   155
    };
alpar@774
   156
    class InEdgeIt : public Edge { 
alpar@774
   157
      const GraphWrapper<Graph>* gw;
marci@556
   158
      friend class GraphWrapper<Graph>;
alpar@774
   159
     public:
marci@556
   160
      InEdgeIt() { }
alpar@774
   161
      //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   162
      InEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   163
      InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   164
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   165
      InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   166
	Edge(e), gw(&_gw) { }
alpar@774
   167
      InEdgeIt& operator++() { 
alpar@774
   168
	*(static_cast<Edge*>(this))=
alpar@774
   169
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   170
	return *this; 
alpar@774
   171
      }
marci@556
   172
    };
marci@556
   173
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
alpar@774
   174
    class EdgeIt : public Edge { 
alpar@774
   175
      const GraphWrapper<Graph>* gw;
marci@556
   176
      friend class GraphWrapper<Graph>;
alpar@774
   177
     public:
marci@556
   178
      EdgeIt() { }
alpar@774
   179
      //EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   180
      EdgeIt(Invalid i) : Edge(i) { }
alpar@774
   181
      EdgeIt(const GraphWrapper<Graph>& _gw) : 
alpar@774
   182
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
alpar@774
   183
      EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
marci@777
   184
	Edge(e), gw(&_gw) { }
alpar@774
   185
      EdgeIt& operator++() { 
alpar@774
   186
	*(static_cast<Edge*>(this))=
alpar@774
   187
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   188
	return *this; 
alpar@774
   189
      }
marci@556
   190
    };
marci@556
   191
   
marci@556
   192
    NodeIt& first(NodeIt& i) const { 
marci@556
   193
      i=NodeIt(*this); return i;
marci@556
   194
    }
marci@556
   195
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   196
      i=OutEdgeIt(*this, p); return i;
marci@556
   197
    }
marci@556
   198
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   199
      i=InEdgeIt(*this, p); return i;
marci@556
   200
    }
marci@556
   201
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   202
      i=EdgeIt(*this); return i;
marci@556
   203
    }
marci@556
   204
alpar@774
   205
//     NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
alpar@774
   206
//     OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
alpar@774
   207
//     InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
alpar@774
   208
//     EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }    
marci@556
   209
marci@556
   210
    Node tail(const Edge& e) const { 
marci@556
   211
      return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
marci@556
   212
    Node head(const Edge& e) const { 
marci@556
   213
      return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
marci@556
   214
alpar@774
   215
//     bool valid(const Node& n) const { 
alpar@774
   216
//       return graph->valid(static_cast<typename Graph::Node>(n)); }
alpar@774
   217
//     bool valid(const Edge& e) const { 
alpar@774
   218
//       return graph->valid(static_cast<typename Graph::Edge>(e)); }
marci@556
   219
marci@556
   220
    int nodeNum() const { return graph->nodeNum(); }
marci@556
   221
    int edgeNum() const { return graph->edgeNum(); }
marci@556
   222
  
alpar@774
   223
//     Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
alpar@774
   224
//     Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
alpar@774
   225
//     Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
alpar@774
   226
//     Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
marci@556
   227
  
marci@556
   228
    Node addNode() const { return Node(graph->addNode()); }
marci@556
   229
    Edge addEdge(const Node& tail, const Node& head) const { 
marci@556
   230
      return Edge(graph->addEdge(tail, head)); }
marci@556
   231
marci@556
   232
    void erase(const Node& i) const { graph->erase(i); }
marci@556
   233
    void erase(const Edge& i) const { graph->erase(i); }
marci@556
   234
  
marci@556
   235
    void clear() const { graph->clear(); }
marci@556
   236
    
alpar@736
   237
    bool forward(const Edge& e) const { return graph->forward(e); }
alpar@736
   238
    bool backward(const Edge& e) const { return graph->backward(e); }
marci@739
   239
marci@739
   240
    int id(const Node& v) const { return graph->id(v); }
marci@739
   241
    int id(const Edge& e) const { return graph->id(e); }
marci@650
   242
    
marci@738
   243
    Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
marci@650
   244
marci@556
   245
    template<typename T> class NodeMap : public Graph::template NodeMap<T> { 
marci@556
   246
      typedef typename Graph::template NodeMap<T> Parent;
marci@556
   247
    public:
alpar@774
   248
      NodeMap(const GraphWrapper<Graph>& gw) :  Parent(*(gw.graph)) { }
alpar@774
   249
      NodeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
marci@556
   250
    };
marci@556
   251
marci@556
   252
    template<typename T> class EdgeMap : public Graph::template EdgeMap<T> { 
marci@556
   253
      typedef typename Graph::template EdgeMap<T> Parent;
marci@556
   254
    public:
alpar@774
   255
      EdgeMap(const GraphWrapper<Graph>& gw) : Parent(*(gw.graph)) { }
alpar@774
   256
      EdgeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
marci@556
   257
    };
marci@556
   258
  };
marci@556
   259
marci@569
   260
marci@569
   261
marci@556
   262
  /// A graph wrapper which reverses the orientation of the edges.
marci@556
   263
marci@556
   264
  /// A graph wrapper which reverses the orientation of the edges.
marci@612
   265
  /// Thus \c Graph have to be a directed graph type.
marci@556
   266
  ///
marci@556
   267
  ///\author Marton Makai
marci@556
   268
  template<typename Graph>
marci@556
   269
  class RevGraphWrapper : public GraphWrapper<Graph> {
marci@650
   270
  public:
marci@650
   271
    typedef GraphWrapper<Graph> Parent; 
marci@556
   272
  protected:
marci@612
   273
    RevGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   274
  public:
marci@556
   275
    RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
alpar@774
   276
    RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
marci@556
   277
marci@556
   278
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   279
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   280
    //If Graph::OutEdgeIt is not defined
marci@556
   281
    //and we do not want to use RevGraphWrapper::InEdgeIt,
marci@556
   282
    //the typdef techinque does not work.
marci@556
   283
    //Unfortunately all the typedefs are instantiated in templates.
marci@556
   284
    //typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
marci@556
   285
    //typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
marci@556
   286
alpar@774
   287
    class OutEdgeIt : public Edge { 
alpar@774
   288
      const RevGraphWrapper<Graph>* gw;
marci@556
   289
      friend class GraphWrapper<Graph>;
alpar@774
   290
     public:
marci@556
   291
      OutEdgeIt() { }
alpar@774
   292
      //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   293
      OutEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   294
      OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   295
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   296
      OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   297
	Edge(e), gw(&_gw) { }
alpar@774
   298
      OutEdgeIt& operator++() { 
alpar@774
   299
	*(static_cast<Edge*>(this))=
alpar@774
   300
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   301
	return *this; 
alpar@774
   302
      }
marci@556
   303
    };
alpar@774
   304
    class InEdgeIt : public Edge { 
alpar@774
   305
      const RevGraphWrapper<Graph>* gw;
marci@556
   306
      friend class GraphWrapper<Graph>;
alpar@774
   307
     public:
marci@556
   308
      InEdgeIt() { }
alpar@774
   309
      //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   310
      InEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   311
      InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   312
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   313
      InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   314
	Edge(e), gw(&_gw) { }
alpar@774
   315
      InEdgeIt& operator++() { 
alpar@774
   316
	*(static_cast<Edge*>(this))=
alpar@774
   317
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   318
	return *this; 
alpar@774
   319
      }
marci@556
   320
    };
marci@556
   321
marci@556
   322
    using GraphWrapper<Graph>::first;
marci@556
   323
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   324
      i=OutEdgeIt(*this, p); return i;
marci@556
   325
    }
marci@556
   326
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   327
      i=InEdgeIt(*this, p); return i;
marci@556
   328
    }
marci@556
   329
alpar@774
   330
//     using GraphWrapper<Graph>::next;
alpar@774
   331
//     OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
alpar@774
   332
//     InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@556
   333
alpar@774
   334
//     Node aNode(const OutEdgeIt& e) const { 
alpar@774
   335
//       return Node(this->graph->aNode(e.e)); }
alpar@774
   336
//     Node aNode(const InEdgeIt& e) const { 
alpar@774
   337
//       return Node(this->graph->aNode(e.e)); }
alpar@774
   338
//     Node bNode(const OutEdgeIt& e) const { 
alpar@774
   339
//       return Node(this->graph->bNode(e.e)); }
alpar@774
   340
//     Node bNode(const InEdgeIt& e) const { 
alpar@774
   341
//       return Node(this->graph->bNode(e.e)); }
marci@556
   342
marci@556
   343
    Node tail(const Edge& e) const { 
marci@556
   344
      return GraphWrapper<Graph>::head(e); }
marci@556
   345
    Node head(const Edge& e) const { 
marci@556
   346
      return GraphWrapper<Graph>::tail(e); }
marci@556
   347
marci@556
   348
  };
marci@556
   349
marci@775
   350
marci@775
   351
marci@612
   352
  /// A graph wrapper for hiding nodes and edges from a graph.
marci@556
   353
  
marci@556
   354
  /// This wrapper shows a graph with filtered node-set and 
marci@556
   355
  /// edge-set. The quick brown fox iterator jumps over 
marci@556
   356
  /// the lazy dog nodes or edges if the values for them are false 
marci@556
   357
  /// in the bool maps. 
marci@556
   358
  ///
marci@556
   359
  ///\author Marton Makai
marci@556
   360
  template<typename Graph, typename NodeFilterMap, 
marci@556
   361
	   typename EdgeFilterMap>
marci@556
   362
  class SubGraphWrapper : public GraphWrapper<Graph> {
marci@650
   363
  public:
marci@650
   364
    typedef GraphWrapper<Graph> Parent;
marci@556
   365
  protected:
marci@556
   366
    NodeFilterMap* node_filter_map;
marci@556
   367
    EdgeFilterMap* edge_filter_map;
marci@556
   368
marci@612
   369
    SubGraphWrapper() : GraphWrapper<Graph>(), 
marci@556
   370
			node_filter_map(0), edge_filter_map(0) { }
marci@556
   371
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@556
   372
      node_filter_map=&_node_filter_map;
marci@556
   373
    }
marci@556
   374
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@556
   375
      edge_filter_map=&_edge_filter_map;
marci@556
   376
    }
marci@556
   377
    
marci@556
   378
  public:
marci@556
   379
    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@556
   380
		    EdgeFilterMap& _edge_filter_map) : 
marci@556
   381
      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map), 
marci@556
   382
      edge_filter_map(&_edge_filter_map) { }  
marci@556
   383
marci@556
   384
    typedef typename GraphWrapper<Graph>::Node Node;
marci@775
   385
//     class NodeIt { 
marci@775
   386
//       friend class GraphWrapper<Graph>;
marci@775
   387
//       friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@775
   388
//       typename Graph::NodeIt n;
marci@775
   389
//      public:
marci@775
   390
//       NodeIt() { }
marci@775
   391
//       NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
marci@775
   392
//       NodeIt(const Invalid& i) : n(i) { }
marci@775
   393
//       NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) : 
marci@775
   394
// 	n(*(_G.graph)) { 
marci@775
   395
// 	while (_G.graph->valid(n) && !(*(_G.node_filter_map))[n]) 
marci@775
   396
// 	  _G.graph->next(n);
marci@775
   397
//       }
marci@775
   398
//       operator Node() const { return Node(typename Graph::Node(n)); }
marci@775
   399
//     };
marci@775
   400
    class NodeIt : public Node { 
marci@775
   401
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   402
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@775
   403
    public:
marci@556
   404
      NodeIt() { }
marci@775
   405
      //      NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
marci@775
   406
      NodeIt(Invalid i) : Node(i) { }
marci@775
   407
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@775
   408
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
marci@775
   409
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   410
	     const Node& n) : 
marci@775
   411
	Node(n), gw(&_gw) { }
marci@775
   412
      NodeIt& operator++() { 
marci@775
   413
	*(static_cast<Node*>(this))=
marci@775
   414
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   415
	while (*static_cast<Node*>(this)!=INVALID && 
marci@775
   416
	       !(*(gw->node_filter_map))[*this]) 
marci@775
   417
	  *(static_cast<Node*>(this))=
marci@775
   418
	    ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   419
	return *this; 
marci@556
   420
      }
marci@556
   421
    };
marci@556
   422
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@775
   423
    class OutEdgeIt : public Edge { 
marci@775
   424
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   425
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   426
    public:
marci@556
   427
      OutEdgeIt() { }
marci@775
   428
      //      OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   429
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@775
   430
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@777
   431
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
marci@775
   432
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   433
	     const Edge& e) : 
marci@775
   434
	Edge(e), gw(&_gw) { }
marci@775
   435
      OutEdgeIt& operator++() { 
marci@775
   436
	*(static_cast<Edge*>(this))=
marci@775
   437
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   438
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   439
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   440
	  *(static_cast<Edge*>(this))=
marci@775
   441
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   442
	return *this; 
marci@556
   443
      }
marci@556
   444
    };
marci@775
   445
    class InEdgeIt : public Edge { 
marci@775
   446
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   447
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   448
    public:
marci@556
   449
      InEdgeIt() { }
marci@775
   450
      //      InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   451
      InEdgeIt(Invalid i) : Edge(i) { }
marci@775
   452
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@777
   453
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
marci@775
   454
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   455
	     const Edge& e) : 
marci@775
   456
	Edge(e), gw(&_gw) { }
marci@775
   457
      InEdgeIt& operator++() { 
marci@775
   458
	*(static_cast<Edge*>(this))=
marci@775
   459
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   460
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   461
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   462
	  *(static_cast<Edge*>(this))=
marci@775
   463
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   464
	return *this; 
marci@556
   465
      }
marci@556
   466
    };
marci@775
   467
    class EdgeIt : public Edge { 
marci@775
   468
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   469
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   470
    public:
marci@556
   471
      EdgeIt() { }
marci@775
   472
      //      EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   473
      EdgeIt(Invalid i) : Edge(i) { }
marci@775
   474
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@775
   475
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
marci@775
   476
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   477
	     const Edge& e) : 
marci@775
   478
	Edge(e), gw(&_gw) { }
marci@775
   479
      EdgeIt& operator++() { 
marci@775
   480
	*(static_cast<Edge*>(this))=
marci@775
   481
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   482
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   483
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   484
	  *(static_cast<Edge*>(this))=
marci@775
   485
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   486
	return *this; 
marci@556
   487
      }
marci@556
   488
    };
marci@556
   489
marci@556
   490
    NodeIt& first(NodeIt& i) const { 
marci@556
   491
      i=NodeIt(*this); return i;
marci@556
   492
    }
marci@556
   493
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   494
      i=OutEdgeIt(*this, p); return i;
marci@556
   495
    }
marci@556
   496
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   497
      i=InEdgeIt(*this, p); return i;
marci@556
   498
    }
marci@556
   499
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   500
      i=EdgeIt(*this); return i;
marci@556
   501
    }
marci@556
   502
    
marci@775
   503
//     NodeIt& next(NodeIt& i) const {
marci@775
   504
//       this->graph->next(i.n); 
marci@775
   505
//       while (this->graph->valid(i) && !(*node_filter_map)[i.n]) { 
marci@775
   506
// 	this->graph->next(i.n); }
marci@775
   507
//       return i;
marci@775
   508
//     }
marci@775
   509
//     OutEdgeIt& next(OutEdgeIt& i) const {
marci@775
   510
//       this->graph->next(i.e); 
marci@775
   511
//       while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@775
   512
// 	this->graph->next(i.e); }
marci@775
   513
//       return i;
marci@775
   514
//     }
marci@775
   515
//     InEdgeIt& next(InEdgeIt& i) const {
marci@775
   516
//       this->graph->next(i.e); 
marci@775
   517
//       while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@775
   518
// 	this->graph->next(i.e); }
marci@775
   519
//       return i;
marci@775
   520
//     }
marci@775
   521
//     EdgeIt& next(EdgeIt& i) const {
marci@775
   522
//       this->graph->next(i.e); 
marci@775
   523
//       while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@775
   524
// 	this->graph->next(i.e); }
marci@775
   525
//       return i;
marci@775
   526
//     }
marci@556
   527
marci@775
   528
//     Node aNode(const OutEdgeIt& e) const { 
marci@775
   529
//       return Node(this->graph->aNode(e.e)); }
marci@775
   530
//     Node aNode(const InEdgeIt& e) const { 
marci@775
   531
//       return Node(this->graph->aNode(e.e)); }
marci@775
   532
//     Node bNode(const OutEdgeIt& e) const { 
marci@775
   533
//       return Node(this->graph->bNode(e.e)); }
marci@775
   534
//     Node bNode(const InEdgeIt& e) const { 
marci@775
   535
//       return Node(this->graph->bNode(e.e)); }
marci@556
   536
marci@561
   537
    /// This function hides \c n in the graph, i.e. the iteration 
marci@561
   538
    /// jumps over it. This is done by simply setting the value of \c n  
marci@561
   539
    /// to be false in the corresponding node-map.
marci@556
   540
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@561
   541
marci@561
   542
    /// This function hides \c e in the graph, i.e. the iteration 
marci@561
   543
    /// jumps over it. This is done by simply setting the value of \c e  
marci@561
   544
    /// to be false in the corresponding edge-map.
marci@556
   545
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@556
   546
marci@561
   547
    /// The value of \c n is set to be true in the node-map which stores 
marci@561
   548
    /// hide information. If \c n was hidden previuosly, then it is shown 
marci@561
   549
    /// again
marci@561
   550
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@561
   551
marci@561
   552
    /// The value of \c e is set to be true in the edge-map which stores 
marci@561
   553
    /// hide information. If \c e was hidden previuosly, then it is shown 
marci@561
   554
    /// again
marci@556
   555
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@556
   556
marci@561
   557
    /// Returns true if \c n is hidden.
marci@561
   558
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@561
   559
marci@561
   560
    /// Returns true if \c n is hidden.
marci@561
   561
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@593
   562
alpar@594
   563
    /// This is a linear time operation and works only if 
marci@593
   564
    /// NodeIt is defined.
marci@593
   565
    int nodeNum() const { 
marci@593
   566
      int i=0;
marci@593
   567
      NodeIt n;
marci@593
   568
      for (this->first(n); this->valid(n); this->next(n)) ++i;
marci@593
   569
      return i; 
marci@593
   570
    }
marci@593
   571
marci@593
   572
    /// This is a linear time operation and works only if 
marci@593
   573
    /// EdgeIt is defined.
marci@593
   574
    int edgeNum() const { 
marci@593
   575
      int i=0;
marci@593
   576
      EdgeIt e;
marci@593
   577
      for (this->first(e); this->valid(e); this->next(e)) ++i;
marci@593
   578
      return i; 
marci@593
   579
    }
marci@593
   580
marci@556
   581
  };
marci@556
   582
marci@569
   583
marci@569
   584
marci@612
   585
  /// \brief A wrapper for forgetting the orientation of a graph.
marci@612
   586
  ///
marci@556
   587
  /// A wrapper for getting an undirected graph by forgetting
marci@556
   588
  /// the orientation of a directed one.
alpar@589
   589
  ///
marci@612
   590
  /// \author Marton Makai
marci@556
   591
  template<typename Graph>
marci@556
   592
  class UndirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   593
  public:
marci@650
   594
    typedef GraphWrapper<Graph> Parent; 
marci@556
   595
  protected:
marci@556
   596
    UndirGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   597
    
marci@556
   598
  public:
marci@556
   599
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   600
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
   601
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   602
    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
marci@556
   603
marci@556
   604
    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
marci@556
   605
marci@556
   606
    class OutEdgeIt {
marci@556
   607
      friend class UndirGraphWrapper<Graph>;
marci@556
   608
      bool out_or_in; //true iff out
marci@556
   609
      typename Graph::OutEdgeIt out;
marci@556
   610
      typename Graph::InEdgeIt in;
marci@556
   611
    public:
marci@556
   612
      OutEdgeIt() { }
marci@556
   613
      OutEdgeIt(const Invalid& i) : Edge(i) { }
marci@556
   614
      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
marci@556
   615
	out_or_in=true; _G.graph->first(out, _n);
marci@556
   616
	if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);	}
marci@556
   617
      } 
marci@556
   618
      operator Edge() const { 
marci@556
   619
	if (out_or_in) return Edge(out); else return Edge(in); 
marci@556
   620
      }
marci@556
   621
    };
marci@556
   622
marci@556
   623
//FIXME InEdgeIt
marci@556
   624
    typedef OutEdgeIt InEdgeIt; 
marci@556
   625
marci@556
   626
    using GraphWrapper<Graph>::first;
marci@556
   627
//     NodeIt& first(NodeIt& i) const { 
marci@556
   628
//       i=NodeIt(*this); return i;
marci@556
   629
//     }
marci@556
   630
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   631
      i=OutEdgeIt(*this, p); return i;
marci@556
   632
    }
marci@556
   633
//FIXME
marci@556
   634
//     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   635
//       i=InEdgeIt(*this, p); return i;
marci@556
   636
//     }
marci@556
   637
//     EdgeIt& first(EdgeIt& i) const { 
marci@556
   638
//       i=EdgeIt(*this); return i;
marci@556
   639
//     }
marci@556
   640
marci@556
   641
    using GraphWrapper<Graph>::next;
marci@556
   642
//     NodeIt& next(NodeIt& n) const {
marci@556
   643
//       GraphWrapper<Graph>::next(n);
marci@556
   644
//       return n;
marci@556
   645
//     }
marci@556
   646
    OutEdgeIt& next(OutEdgeIt& e) const {
marci@556
   647
      if (e.out_or_in) {
marci@556
   648
	typename Graph::Node n=this->graph->tail(e.out);
marci@556
   649
	this->graph->next(e.out);
marci@556
   650
	if (!this->graph->valid(e.out)) { 
marci@556
   651
	  e.out_or_in=false; this->graph->first(e.in, n); }
marci@556
   652
      } else {
marci@556
   653
	this->graph->next(e.in);
marci@556
   654
      }
marci@556
   655
      return e;
marci@556
   656
    }
marci@556
   657
    //FIXME InEdgeIt
marci@556
   658
//     EdgeIt& next(EdgeIt& e) const {
marci@556
   659
//       GraphWrapper<Graph>::next(n);
marci@556
   660
// //      graph->next(e.e);
marci@556
   661
//       return e;
marci@556
   662
//     }
marci@556
   663
marci@556
   664
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   665
      if (e.out_or_in) return this->graph->tail(e); else 
marci@556
   666
	return this->graph->head(e); }
marci@556
   667
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   668
      if (e.out_or_in) return this->graph->head(e); else 
marci@556
   669
	return this->graph->tail(e); }
marci@556
   670
  };
marci@556
   671
  
marci@612
   672
  /// \brief An undirected graph template.
marci@612
   673
  ///
marci@612
   674
  /// An undirected graph template.
marci@612
   675
  /// This class works as an undirected graph and a directed graph of 
marci@612
   676
  /// class \c Graph is used for the physical storage.
marci@612
   677
  /// \ingroup graphs
marci@556
   678
  template<typename Graph>
marci@556
   679
  class UndirGraph : public UndirGraphWrapper<Graph> {
marci@556
   680
    typedef UndirGraphWrapper<Graph> Parent;
marci@556
   681
  protected:
marci@556
   682
    Graph gr;
marci@556
   683
  public:
marci@556
   684
    UndirGraph() : UndirGraphWrapper<Graph>() { 
marci@556
   685
      Parent::setGraph(gr); 
marci@556
   686
    }
marci@556
   687
  };
marci@556
   688
marci@569
   689
marci@650
   690
marci@650
   691
  ///\brief A wrapper for composing a subgraph of a 
marci@650
   692
  /// bidirected graph composed from a directed one. 
marci@612
   693
  /// experimental, for fezso's sake.
marci@612
   694
  ///
marci@650
   695
  /// A wrapper for composing a subgraps of a 
marci@650
   696
  /// bidirected graph composed from a directed one. 
marci@576
   697
  /// experimental, for fezso's sake.
marci@612
   698
  /// A bidirected graph is composed over the directed one without physical 
marci@612
   699
  /// storage. As the oppositely directed edges are logically different ones 
marci@612
   700
  /// the maps are able to attach different values for them.
marci@650
   701
  template<typename Graph, 
marci@650
   702
	   typename ForwardFilterMap, typename BackwardFilterMap>
marci@650
   703
  class SubBidirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   704
  public:
marci@650
   705
    typedef GraphWrapper<Graph> Parent; 
marci@569
   706
  protected:
marci@650
   707
    ForwardFilterMap* forward_filter;
marci@650
   708
    BackwardFilterMap* backward_filter;
marci@650
   709
marci@650
   710
    SubBidirGraphWrapper() : GraphWrapper<Graph>()/*, 
marci@569
   711
						 capacity(0), flow(0)*/ { }
marci@650
   712
    void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
marci@650
   713
      forward_filter=&_forward_filter;
marci@650
   714
    }
marci@650
   715
    void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
marci@650
   716
      backward_filter=&_backward_filter;
marci@650
   717
    }
marci@569
   718
marci@569
   719
  public:
marci@569
   720
marci@650
   721
    SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter, 
marci@650
   722
			 BackwardFilterMap& _backward_filter) : 
marci@650
   723
      GraphWrapper<Graph>(_graph), 
marci@650
   724
      forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
alpar@774
   725
    SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph, 
alpar@774
   726
			 ForwardFilterMap, BackwardFilterMap>& gw) : 
alpar@774
   727
      Parent(gw), 
alpar@774
   728
      forward_filter(gw.forward_filter), 
alpar@774
   729
      backward_filter(gw.backward_filter) { }
marci@569
   730
marci@569
   731
    class Edge; 
marci@569
   732
    class OutEdgeIt; 
marci@569
   733
    friend class Edge; 
marci@569
   734
    friend class OutEdgeIt; 
marci@569
   735
marci@621
   736
    //template<typename T> class NodeMap;    
marci@621
   737
    template<typename T> class EdgeMap;
marci@621
   738
marci@569
   739
    typedef typename GraphWrapper<Graph>::Node Node;
alpar@774
   740
    //typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@621
   741
alpar@774
   742
    typedef typename Graph::Edge GraphEdge;
marci@569
   743
    class Edge : public Graph::Edge {
marci@650
   744
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   745
					ForwardFilterMap, BackwardFilterMap>;
marci@621
   746
      ///\bug ez nem is kell
marci@621
   747
      //template<typename T> friend class NodeMap;
marci@621
   748
      template<typename T> friend class EdgeMap;
marci@569
   749
    protected:
marci@569
   750
      bool backward; //true, iff backward
marci@569
   751
//      typename Graph::Edge e;
marci@569
   752
    public:
marci@569
   753
      Edge() { }
marci@626
   754
      ///\bug =false kell-e? zsoltnak kell az addEdge miatt
alpar@774
   755
      Edge(const typename Graph::Edge& e, bool _backward/*=false*/) : 
alpar@774
   756
	Graph::Edge(e), backward(_backward) { }
alpar@774
   757
      Edge(Invalid i) : Graph::Edge(i), backward(true) { }
marci@569
   758
//the unique invalid iterator
alpar@774
   759
//       friend bool operator==(const Edge& u, const Edge& v) { 
alpar@774
   760
// 	return (u.backward==v.backward && 
alpar@774
   761
// 		static_cast<typename Graph::Edge>(u)==
alpar@774
   762
// 		static_cast<typename Graph::Edge>(v));
alpar@774
   763
//       } 
alpar@774
   764
//       friend bool operator!=(const Edge& u, const Edge& v) { 
alpar@774
   765
// 	return (u.backward!=v.backward || 
alpar@774
   766
// 		static_cast<typename Graph::Edge>(u)!=
alpar@774
   767
// 		static_cast<typename Graph::Edge>(v));
alpar@774
   768
//       }
alpar@774
   769
      bool operator==(const Edge& v) const { 
alpar@774
   770
	return (this->backward==v.backward && 
alpar@774
   771
		static_cast<typename Graph::Edge>(*this)==
marci@569
   772
		static_cast<typename Graph::Edge>(v));
marci@569
   773
      } 
alpar@774
   774
      bool operator!=(const Edge& v) const { 
alpar@774
   775
	return (this->backward!=v.backward || 
alpar@774
   776
		static_cast<typename Graph::Edge>(*this)!=
marci@569
   777
		static_cast<typename Graph::Edge>(v));
alpar@774
   778
      }
marci@569
   779
    };
marci@569
   780
alpar@774
   781
    class OutEdgeIt : public Edge {
marci@650
   782
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   783
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   784
    protected:
alpar@774
   785
      const SubBidirGraphWrapper<Graph, 
alpar@774
   786
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   787
    public:
marci@569
   788
      OutEdgeIt() { }
alpar@774
   789
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@569
   790
//the unique invalid iterator
marci@650
   791
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   792
		ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   793
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   794
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   795
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   796
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   797
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   798
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   799
	  *static_cast<Edge*>(this)=
alpar@774
   800
	    Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
marci@775
   801
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   802
		 !(*(gw->backward_filter))[*this]) 
marci@775
   803
	    *(static_cast<GraphEdge*>(this))=
marci@775
   804
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   805
	}
alpar@774
   806
      }
alpar@774
   807
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   808
		ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   809
	Edge(e), gw(&_gw) { }
alpar@774
   810
      OutEdgeIt& operator++() { 
alpar@774
   811
	if (!this->backward) {
alpar@774
   812
	  Node n=gw->tail(*this);
alpar@774
   813
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   814
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   815
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   816
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   817
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   818
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   819
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   820
	    *static_cast<Edge*>(this)=
alpar@774
   821
	      Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
marci@775
   822
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   823
		   !(*(gw->backward_filter))[*this]) 
marci@775
   824
	      *(static_cast<GraphEdge*>(this))=
marci@775
   825
		++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   826
	  }
alpar@774
   827
	} else {
alpar@774
   828
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   829
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   830
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   831
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   832
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   833
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@569
   834
	}
alpar@774
   835
	return *this;
marci@569
   836
      }
marci@569
   837
    };
marci@569
   838
alpar@774
   839
    class InEdgeIt : public Edge {
marci@650
   840
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   841
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   842
    protected:
alpar@774
   843
      const SubBidirGraphWrapper<Graph, 
alpar@774
   844
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   845
    public:
marci@569
   846
      InEdgeIt() { }
alpar@774
   847
      InEdgeIt(Invalid i) : Edge(i) { }
marci@569
   848
//the unique invalid iterator
marci@650
   849
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   850
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   851
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   852
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   853
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   854
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   855
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   856
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   857
	  *static_cast<Edge*>(this)=
alpar@774
   858
	    Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
marci@775
   859
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   860
		 !(*(gw->backward_filter))[*this]) 
marci@775
   861
	    *(static_cast<GraphEdge*>(this))=
marci@775
   862
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   863
	}
alpar@774
   864
      }
alpar@774
   865
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   866
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   867
	Edge(e), gw(&_gw) { }
alpar@774
   868
      InEdgeIt& operator++() { 
alpar@774
   869
	if (!this->backward) {
marci@775
   870
	  Node n=gw->tail(*this);
alpar@774
   871
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   872
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   873
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   874
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   875
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   876
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   877
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   878
	    *static_cast<Edge*>(this)=
alpar@774
   879
	      Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
marci@775
   880
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   881
		   !(*(gw->backward_filter))[*this]) 
marci@775
   882
	      *(static_cast<GraphEdge*>(this))=
marci@775
   883
		++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   884
	  }
alpar@774
   885
	} else {
alpar@774
   886
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   887
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   888
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   889
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   890
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   891
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@569
   892
	}
alpar@774
   893
	return *this;
marci@569
   894
      }
marci@569
   895
    };
marci@569
   896
alpar@774
   897
    class EdgeIt : public Edge {
marci@650
   898
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   899
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   900
    protected:
alpar@774
   901
      const SubBidirGraphWrapper<Graph, 
alpar@774
   902
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   903
    public:
marci@569
   904
      EdgeIt() { }
alpar@774
   905
      EdgeIt(Invalid i) : Edge(i) { }
alpar@774
   906
//the unique invalid iterator
marci@650
   907
      EdgeIt(const SubBidirGraphWrapper<Graph, 
marci@775
   908
	     ForwardFilterMap, BackwardFilterMap>& _gw) : 
marci@775
   909
	Edge(typename Graph::OutEdgeIt(*(_gw.graph)), false), gw(&_gw) { 
alpar@774
   910
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   911
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   912
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   913
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   914
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   915
	  *static_cast<Edge*>(this)=
alpar@774
   916
	    Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
marci@775
   917
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   918
		 !(*(gw->backward_filter))[*this]) 
marci@775
   919
	    *(static_cast<GraphEdge*>(this))=
marci@775
   920
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   921
	}
alpar@774
   922
      }
alpar@774
   923
      EdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   924
	     ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   925
	Edge(e), gw(&_gw) { }
alpar@774
   926
      EdgeIt& operator++() { 
alpar@774
   927
	if (!this->backward) {
alpar@774
   928
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   929
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   930
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   931
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   932
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   933
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   934
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   935
	    *static_cast<Edge*>(this)=
alpar@774
   936
	      Edge(typename Graph::EdgeIt(*(gw->graph)), true);
marci@775
   937
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   938
		   !(*(gw->backward_filter))[*this]) 
marci@775
   939
	      *(static_cast<GraphEdge*>(this))=
marci@775
   940
		++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   941
	  }
alpar@774
   942
	} else {
alpar@774
   943
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   944
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   945
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   946
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   947
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   948
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@569
   949
	}
alpar@774
   950
	return *this;
marci@569
   951
      }
marci@569
   952
    };
marci@569
   953
marci@569
   954
    using GraphWrapper<Graph>::first;
marci@569
   955
//     NodeIt& first(NodeIt& i) const { 
marci@569
   956
//       i=NodeIt(*this); return i;
marci@569
   957
//     }
marci@569
   958
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@569
   959
      i=OutEdgeIt(*this, p); return i;
marci@569
   960
    }
marci@569
   961
//    FIXME not tested
marci@569
   962
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@569
   963
      i=InEdgeIt(*this, p); return i;
marci@569
   964
    }
marci@569
   965
    EdgeIt& first(EdgeIt& i) const { 
marci@569
   966
      i=EdgeIt(*this); return i;
marci@569
   967
    }
marci@556
   968
  
alpar@774
   969
//     using GraphWrapper<Graph>::next;
alpar@774
   970
// //    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
alpar@774
   971
//     OutEdgeIt& next(OutEdgeIt& e) const { 
alpar@774
   972
//       if (!e.backward) {
alpar@774
   973
// 	Node v=this->graph->aNode(e.out);
alpar@774
   974
// 	this->graph->next(e.out);
alpar@774
   975
// 	while(this->graph->valid(e.out) && !(*forward_filter)[e]) { 
alpar@774
   976
// 	  this->graph->next(e.out); }
alpar@774
   977
// 	if (!this->graph->valid(e.out)) {
alpar@774
   978
// 	  e.backward=true;
alpar@774
   979
// 	  this->graph->first(e.in, v); 
alpar@774
   980
// 	  while(this->graph->valid(e.in) && !(*backward_filter)[e]) { 
alpar@774
   981
// 	    this->graph->next(e.in); }
alpar@774
   982
// 	}
alpar@774
   983
//       } else {
alpar@774
   984
// 	this->graph->next(e.in);
alpar@774
   985
// 	while(this->graph->valid(e.in) && !(*backward_filter)[e]) { 
alpar@774
   986
// 	  this->graph->next(e.in); } 
alpar@774
   987
//       }
alpar@774
   988
//       return e;
alpar@774
   989
//     }
alpar@774
   990
// //     FIXME Not tested
alpar@774
   991
//     InEdgeIt& next(InEdgeIt& e) const { 
alpar@774
   992
//       if (!e.backward) {
alpar@774
   993
// 	Node v=this->graph->aNode(e.in);
alpar@774
   994
// 	this->graph->next(e.in);
alpar@774
   995
// 	while(this->graph->valid(e.in) && !(*forward_filter)[e]) { 
alpar@774
   996
// 	  this->graph->next(e.in); }
alpar@774
   997
// 	if (!this->graph->valid(e.in)) {
alpar@774
   998
// 	  e.backward=true;
alpar@774
   999
// 	  this->graph->first(e.out, v); 
alpar@774
  1000
// 	  while(this->graph->valid(e.out) && !(*backward_filter)[e]) { 
alpar@774
  1001
// 	    this->graph->next(e.out); }
alpar@774
  1002
// 	}
alpar@774
  1003
//       } else {
alpar@774
  1004
// 	this->graph->next(e.out);
alpar@774
  1005
// 	while(this->graph->valid(e.out) && !(*backward_filter)[e]) { 
alpar@774
  1006
// 	  this->graph->next(e.out); } 
alpar@774
  1007
//       }
alpar@774
  1008
//       return e;
alpar@774
  1009
//     }
alpar@774
  1010
//     EdgeIt& next(EdgeIt& e) const {
alpar@774
  1011
//       if (!e.backward) {
alpar@774
  1012
// 	this->graph->next(e.e);
alpar@774
  1013
// 	while(this->graph->valid(e.e) && !(*forward_filter)[e]) { 
alpar@774
  1014
// 	  this->graph->next(e.e); }
alpar@774
  1015
// 	if (!this->graph->valid(e.e)) {
alpar@774
  1016
// 	  e.backward=true;
alpar@774
  1017
// 	  this->graph->first(e.e); 
alpar@774
  1018
// 	  while(this->graph->valid(e.e) && !(*backward_filter)[e]) { 
alpar@774
  1019
// 	    this->graph->next(e.e); }
alpar@774
  1020
// 	}
alpar@774
  1021
//       } else {
alpar@774
  1022
// 	this->graph->next(e.e);
alpar@774
  1023
// 	while(this->graph->valid(e.e) && !(*backward_filter)[e]) { 
alpar@774
  1024
// 	  this->graph->next(e.e); } 
alpar@774
  1025
//       }
alpar@774
  1026
//       return e;
alpar@774
  1027
//     }
marci@569
  1028
marci@569
  1029
    Node tail(Edge e) const { 
marci@569
  1030
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@569
  1031
    Node head(Edge e) const { 
marci@569
  1032
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@569
  1033
alpar@774
  1034
//     Node aNode(OutEdgeIt e) const { 
alpar@774
  1035
//       return ((!e.backward) ? this->graph->aNode(e.out) : 
alpar@774
  1036
// 	      this->graph->aNode(e.in)); }
alpar@774
  1037
//     Node bNode(OutEdgeIt e) const { 
alpar@774
  1038
//       return ((!e.backward) ? this->graph->bNode(e.out) : 
alpar@774
  1039
// 	      this->graph->bNode(e.in)); }
marci@569
  1040
alpar@774
  1041
//     Node aNode(InEdgeIt e) const { 
alpar@774
  1042
//       return ((!e.backward) ? this->graph->aNode(e.in) : 
alpar@774
  1043
// 	      this->graph->aNode(e.out)); }
alpar@774
  1044
//     Node bNode(InEdgeIt e) const { 
alpar@774
  1045
//       return ((!e.backward) ? this->graph->bNode(e.in) : 
alpar@774
  1046
// 	      this->graph->bNode(e.out)); }
marci@569
  1047
marci@572
  1048
    /// Gives back the opposite edge.
marci@572
  1049
    Edge opposite(const Edge& e) const { 
marci@572
  1050
      Edge f=e;
marci@572
  1051
      f.backward=!f.backward;
marci@572
  1052
      return f;
marci@572
  1053
    }
marci@572
  1054
marci@569
  1055
//    int nodeNum() const { return graph->nodeNum(); }
marci@569
  1056
    //FIXME
marci@569
  1057
    void edgeNum() const { }
marci@569
  1058
    //int edgeNum() const { return graph->edgeNum(); }
marci@569
  1059
marci@569
  1060
marci@569
  1061
//    int id(Node v) const { return graph->id(v); }
marci@569
  1062
alpar@774
  1063
//     bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
alpar@774
  1064
//     bool valid(Edge e) const { 
alpar@774
  1065
//       return this->graph->valid(e);
alpar@774
  1066
// 	//return e.forward ? graph->valid(e.out) : graph->valid(e.in); 
alpar@774
  1067
//     }
marci@569
  1068
marci@569
  1069
    bool forward(const Edge& e) const { return !e.backward; }
marci@569
  1070
    bool backward(const Edge& e) const { return e.backward; }
marci@569
  1071
marci@569
  1072
//     void augment(const Edge& e, Number a) const {
marci@569
  1073
//       if (!e.backward)  
marci@569
  1074
// // 	flow->set(e.out, flow->get(e.out)+a);
marci@569
  1075
// 	flow->set(e, (*flow)[e]+a);
marci@569
  1076
//       else  
marci@569
  1077
// // 	flow->set(e.in, flow->get(e.in)-a);
marci@569
  1078
// 	flow->set(e, (*flow)[e]-a);
marci@569
  1079
//     }
marci@569
  1080
marci@650
  1081
//     bool enabled(const Edge& e) const { 
marci@650
  1082
//       if (!e.backward) 
marci@650
  1083
// //	return (capacity->get(e.out)-flow->get(e.out)); 
marci@650
  1084
// 	//return ((*capacity)[e]-(*flow)[e]);
marci@650
  1085
// 	return true;
marci@650
  1086
//       else 
marci@650
  1087
// //	return (flow->get(e.in)); 
marci@650
  1088
// 	//return ((*flow)[e]); 
marci@650
  1089
// 	return true;
marci@650
  1090
//     }
marci@569
  1091
marci@569
  1092
//     Number enabled(typename Graph::OutEdgeIt out) const { 
marci@569
  1093
// //      return (capacity->get(out)-flow->get(out)); 
marci@569
  1094
//       return ((*capacity)[out]-(*flow)[out]); 
marci@569
  1095
//     }
marci@569
  1096
    
marci@569
  1097
//     Number enabled(typename Graph::InEdgeIt in) const { 
marci@569
  1098
// //      return (flow->get(in)); 
marci@569
  1099
//       return ((*flow)[in]); 
marci@569
  1100
//     }
marci@569
  1101
marci@569
  1102
    template <typename T>
marci@569
  1103
    class EdgeMap {
marci@569
  1104
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@569
  1105
    public:
marci@623
  1106
      typedef T ValueType;
marci@623
  1107
      typedef Edge KeyType;
marci@650
  1108
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1109
	      ForwardFilterMap, BackwardFilterMap>& g) : 
alpar@774
  1110
	forward_map(*(g.graph)), backward_map(*(g.graph)) { }
marci@650
  1111
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1112
	      ForwardFilterMap, BackwardFilterMap>& g, T a) : 
alpar@774
  1113
	forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
marci@569
  1114
      void set(Edge e, T a) { 
marci@569
  1115
	if (!e.backward) 
marci@622
  1116
	  forward_map.set(e/*.out*/, a); 
marci@569
  1117
	else 
marci@622
  1118
	  backward_map.set(e/*.in*/, a); 
marci@569
  1119
      }
marci@569
  1120
      T operator[](Edge e) const { 
marci@569
  1121
	if (!e.backward) 
marci@622
  1122
	  return forward_map[e/*.out*/]; 
marci@569
  1123
	else 
marci@622
  1124
	  return backward_map[e/*.in*/]; 
marci@569
  1125
      }
marci@625
  1126
      void update() { 
marci@625
  1127
	forward_map.update(); 
marci@625
  1128
	backward_map.update();
marci@625
  1129
      }
marci@569
  1130
//       T get(Edge e) const { 
marci@569
  1131
// 	if (e.out_or_in) 
marci@569
  1132
// 	  return forward_map.get(e.out); 
marci@569
  1133
// 	else 
marci@569
  1134
// 	  return backward_map.get(e.in); 
marci@569
  1135
//       }
marci@569
  1136
    };
marci@569
  1137
  };
marci@569
  1138
marci@650
  1139
marci@650
  1140
marci@650
  1141
  ///\brief A wrapper for composing bidirected graph from a directed one. 
marci@650
  1142
  /// experimental, for fezso's sake.
marci@650
  1143
  ///
marci@650
  1144
  /// A wrapper for composing bidirected graph from a directed one. 
marci@650
  1145
  /// experimental, for fezso's sake.
marci@650
  1146
  /// A bidirected graph is composed over the directed one without physical 
marci@650
  1147
  /// storage. As the oppositely directed edges are logically different ones 
marci@650
  1148
  /// the maps are able to attach different values for them.
marci@650
  1149
  template<typename Graph>
marci@650
  1150
  class BidirGraphWrapper : 
marci@650
  1151
    public SubBidirGraphWrapper<
marci@650
  1152
    Graph, 
marci@650
  1153
    ConstMap<typename Graph::Edge, bool>, 
marci@650
  1154
    ConstMap<typename Graph::Edge, bool> > {
marci@650
  1155
  public:
marci@650
  1156
    typedef  SubBidirGraphWrapper<
marci@650
  1157
      Graph, 
marci@650
  1158
      ConstMap<typename Graph::Edge, bool>, 
marci@650
  1159
      ConstMap<typename Graph::Edge, bool> > Parent; 
marci@650
  1160
  protected:
marci@650
  1161
    ConstMap<typename Graph::Edge, bool> cm;
marci@650
  1162
    //const CapacityMap* capacity;
marci@650
  1163
    //FlowMap* flow;
marci@650
  1164
marci@655
  1165
    BidirGraphWrapper() : Parent(), cm(true) { 
marci@655
  1166
      Parent::setForwardFilterMap(cm);
marci@655
  1167
      Parent::setBackwardFilterMap(cm);
marci@655
  1168
    }
marci@650
  1169
//     void setCapacityMap(const CapacityMap& _capacity) {
marci@650
  1170
//       capacity=&_capacity;
marci@650
  1171
//     }
marci@650
  1172
//     void setFlowMap(FlowMap& _flow) {
marci@650
  1173
//       flow=&_flow;
marci@650
  1174
//     }
marci@650
  1175
marci@650
  1176
  public:
marci@650
  1177
marci@650
  1178
    BidirGraphWrapper(Graph& _graph) : Parent() { 
marci@650
  1179
      Parent::setGraph(_graph);
marci@650
  1180
      Parent::setForwardFilterMap(cm);
marci@650
  1181
      Parent::setBackwardFilterMap(cm);
marci@650
  1182
    }
marci@738
  1183
marci@738
  1184
    int edgeNum() const { 
marci@738
  1185
      return 2*this->graph->edgeNum();
marci@738
  1186
    }
marci@650
  1187
  };
marci@650
  1188
marci@650
  1189
marci@650
  1190
marci@650
  1191
marci@653
  1192
  template<typename Graph>
marci@653
  1193
  class OldBidirGraphWrapper : public GraphWrapper<Graph> {
marci@653
  1194
  public:
marci@653
  1195
    typedef GraphWrapper<Graph> Parent; 
marci@653
  1196
  protected:
marci@653
  1197
    //const CapacityMap* capacity;
marci@653
  1198
    //FlowMap* flow;
marci@650
  1199
marci@653
  1200
    OldBidirGraphWrapper() : GraphWrapper<Graph>()/*, 
marci@653
  1201
						 capacity(0), flow(0)*/ { }
marci@653
  1202
//     void setCapacityMap(const CapacityMap& _capacity) {
marci@653
  1203
//       capacity=&_capacity;
marci@650
  1204
//     }
marci@653
  1205
//     void setFlowMap(FlowMap& _flow) {
marci@653
  1206
//       flow=&_flow;
marci@650
  1207
//     }
marci@650
  1208
marci@653
  1209
  public:
marci@650
  1210
marci@653
  1211
    OldBidirGraphWrapper(Graph& _graph/*, const CapacityMap& _capacity, 
marci@653
  1212
				     FlowMap& _flow*/) : 
marci@653
  1213
      GraphWrapper<Graph>(_graph)/*, capacity(&_capacity), flow(&_flow)*/ { }
marci@650
  1214
marci@653
  1215
    class Edge; 
marci@653
  1216
    class OutEdgeIt; 
marci@653
  1217
    friend class Edge; 
marci@653
  1218
    friend class OutEdgeIt; 
marci@650
  1219
marci@653
  1220
    //template<typename T> class NodeMap;    
marci@653
  1221
    template<typename T> class EdgeMap;
marci@653
  1222
marci@653
  1223
    typedef typename GraphWrapper<Graph>::Node Node;
marci@653
  1224
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@653
  1225
marci@653
  1226
    class Edge : public Graph::Edge {
marci@653
  1227
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1228
      ///\bug ez nem is kell
marci@653
  1229
      //template<typename T> friend class NodeMap;
marci@653
  1230
      template<typename T> friend class EdgeMap;
marci@653
  1231
    protected:
marci@653
  1232
      bool backward; //true, iff backward
marci@653
  1233
//      typename Graph::Edge e;
marci@653
  1234
    public:
marci@653
  1235
      Edge() { }
marci@653
  1236
      ///\bug =false kell-e? zsoltnak kell az addEdge miatt
marci@653
  1237
      Edge(const typename Graph::Edge& _e, bool _backward=false) : 
marci@653
  1238
	Graph::Edge(_e), backward(_backward) { }
marci@653
  1239
      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
marci@653
  1240
//the unique invalid iterator
marci@653
  1241
      friend bool operator==(const Edge& u, const Edge& v) { 
marci@653
  1242
	return (v.backward==u.backward && 
marci@653
  1243
		static_cast<typename Graph::Edge>(u)==
marci@653
  1244
		static_cast<typename Graph::Edge>(v));
marci@653
  1245
      } 
marci@653
  1246
      friend bool operator!=(const Edge& u, const Edge& v) { 
marci@653
  1247
	return (v.backward!=u.backward || 
marci@653
  1248
		static_cast<typename Graph::Edge>(u)!=
marci@653
  1249
		static_cast<typename Graph::Edge>(v));
marci@653
  1250
      } 
marci@653
  1251
    };
marci@653
  1252
marci@653
  1253
    class OutEdgeIt {
marci@653
  1254
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1255
    protected:
marci@653
  1256
      typename Graph::OutEdgeIt out;
marci@653
  1257
      typename Graph::InEdgeIt in;
marci@653
  1258
      bool backward;
marci@653
  1259
    public:
marci@653
  1260
      OutEdgeIt() { }
marci@653
  1261
      //FIXME
marci@653
  1262
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@653
  1263
      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@653
  1264
//the unique invalid iterator
marci@653
  1265
      OutEdgeIt(const OldBidirGraphWrapper<Graph>& _G, Node v) { 
marci@653
  1266
	backward=false;
marci@653
  1267
	_G.graph->first(out, v);
marci@653
  1268
	while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
marci@653
  1269
	if (!_G.graph->valid(out)) {
marci@653
  1270
	  backward=true;
marci@653
  1271
	  _G.graph->first(in, v);
marci@653
  1272
	  while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
marci@653
  1273
	}
marci@653
  1274
      }
marci@653
  1275
      operator Edge() const { 
marci@653
  1276
//	Edge e;
marci@653
  1277
//	e.forward=this->forward;
marci@653
  1278
//	if (this->forward) e=out; else e=in;
marci@653
  1279
//	return e;
marci@653
  1280
	if (this->backward) 
marci@653
  1281
	  return Edge(in, this->backward); 
marci@653
  1282
	else 
marci@653
  1283
	  return Edge(out, this->backward);
marci@653
  1284
      }
marci@653
  1285
    };
marci@653
  1286
marci@653
  1287
    class InEdgeIt {
marci@653
  1288
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1289
    protected:
marci@653
  1290
      typename Graph::OutEdgeIt out;
marci@653
  1291
      typename Graph::InEdgeIt in;
marci@653
  1292
      bool backward;
marci@653
  1293
    public:
marci@653
  1294
      InEdgeIt() { }
marci@653
  1295
      //FIXME
marci@653
  1296
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@653
  1297
      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@653
  1298
//the unique invalid iterator
marci@653
  1299
      InEdgeIt(const OldBidirGraphWrapper<Graph>& _G, Node v) { 
marci@653
  1300
	backward=false;
marci@653
  1301
	_G.graph->first(in, v);
marci@653
  1302
	while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
marci@653
  1303
	if (!_G.graph->valid(in)) {
marci@653
  1304
	  backward=true;
marci@653
  1305
	  _G.graph->first(out, v);
marci@653
  1306
	  while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
marci@653
  1307
	}
marci@653
  1308
      }
marci@653
  1309
      operator Edge() const { 
marci@653
  1310
//	Edge e;
marci@653
  1311
//	e.forward=this->forward;
marci@653
  1312
//	if (this->forward) e=out; else e=in;
marci@653
  1313
//	return e;
marci@653
  1314
	if (this->backward) 
marci@653
  1315
	  return Edge(out, this->backward); 
marci@653
  1316
	else 
marci@653
  1317
	  return Edge(in, this->backward);
marci@653
  1318
      }
marci@653
  1319
    };
marci@653
  1320
marci@653
  1321
    class EdgeIt {
marci@653
  1322
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1323
    protected:
marci@653
  1324
      typename Graph::EdgeIt e;
marci@653
  1325
      bool backward;
marci@653
  1326
    public:
marci@653
  1327
      EdgeIt() { }
marci@653
  1328
      EdgeIt(const Invalid& i) : e(i), backward(true) { }
marci@653
  1329
      EdgeIt(const OldBidirGraphWrapper<Graph>& _G) { 
marci@653
  1330
	backward=false;
marci@653
  1331
	_G.graph->first(e);
marci@653
  1332
	while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
marci@653
  1333
	if (!_G.graph->valid(e)) {
marci@653
  1334
	  backward=true;
marci@653
  1335
	  _G.graph->first(e);
marci@653
  1336
	  while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
marci@653
  1337
	}
marci@653
  1338
      }
marci@653
  1339
      operator Edge() const { 
marci@653
  1340
	return Edge(e, this->backward);
marci@653
  1341
      }
marci@653
  1342
    };
marci@653
  1343
marci@653
  1344
    using GraphWrapper<Graph>::first;
marci@653
  1345
//     NodeIt& first(NodeIt& i) const { 
marci@653
  1346
//       i=NodeIt(*this); return i;
marci@653
  1347
//     }
marci@653
  1348
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@653
  1349
      i=OutEdgeIt(*this, p); return i;
marci@653
  1350
    }
marci@653
  1351
//    FIXME not tested
marci@653
  1352
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@653
  1353
      i=InEdgeIt(*this, p); return i;
marci@653
  1354
    }
marci@653
  1355
    EdgeIt& first(EdgeIt& i) const { 
marci@653
  1356
      i=EdgeIt(*this); return i;
marci@653
  1357
    }
marci@653
  1358
  
marci@653
  1359
    using GraphWrapper<Graph>::next;
marci@653
  1360
//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
marci@653
  1361
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@653
  1362
      if (!e.backward) {
marci@653
  1363
	Node v=this->graph->aNode(e.out);
marci@653
  1364
	this->graph->next(e.out);
marci@653
  1365
	while(this->graph->valid(e.out) && !enabled(e)) { 
marci@653
  1366
	  this->graph->next(e.out); }
marci@653
  1367
	if (!this->graph->valid(e.out)) {
marci@653
  1368
	  e.backward=true;
marci@653
  1369
	  this->graph->first(e.in, v); 
marci@653
  1370
	  while(this->graph->valid(e.in) && !enabled(e)) { 
marci@653
  1371
	    this->graph->next(e.in); }
marci@653
  1372
	}
marci@653
  1373
      } else {
marci@653
  1374
	this->graph->next(e.in);
marci@653
  1375
	while(this->graph->valid(e.in) && !enabled(e)) { 
marci@653
  1376
	  this->graph->next(e.in); } 
marci@653
  1377
      }
marci@653
  1378
      return e;
marci@653
  1379
    }
marci@653
  1380
//     FIXME Not tested
marci@653
  1381
    InEdgeIt& next(InEdgeIt& e) const { 
marci@653
  1382
      if (!e.backward) {
marci@653
  1383
	Node v=this->graph->aNode(e.in);
marci@653
  1384
	this->graph->next(e.in);
marci@653
  1385
	while(this->graph->valid(e.in) && !enabled(e)) { 
marci@653
  1386
	  this->graph->next(e.in); }
marci@653
  1387
	if (!this->graph->valid(e.in)) {
marci@653
  1388
	  e.backward=true;
marci@653
  1389
	  this->graph->first(e.out, v); 
marci@653
  1390
	  while(this->graph->valid(e.out) && !enabled(e)) { 
marci@653
  1391
	    this->graph->next(e.out); }
marci@653
  1392
	}
marci@653
  1393
      } else {
marci@653
  1394
	this->graph->next(e.out);
marci@653
  1395
	while(this->graph->valid(e.out) && !enabled(e)) { 
marci@653
  1396
	  this->graph->next(e.out); } 
marci@653
  1397
      }
marci@653
  1398
      return e;
marci@653
  1399
    }
marci@653
  1400
    EdgeIt& next(EdgeIt& e) const {
marci@653
  1401
      if (!e.backward) {
marci@653
  1402
	this->graph->next(e.e);
marci@653
  1403
	while(this->graph->valid(e.e) && !enabled(e)) { 
marci@653
  1404
	  this->graph->next(e.e); }
marci@653
  1405
	if (!this->graph->valid(e.e)) {
marci@653
  1406
	  e.backward=true;
marci@653
  1407
	  this->graph->first(e.e); 
marci@653
  1408
	  while(this->graph->valid(e.e) && !enabled(e)) { 
marci@653
  1409
	    this->graph->next(e.e); }
marci@653
  1410
	}
marci@653
  1411
      } else {
marci@653
  1412
	this->graph->next(e.e);
marci@653
  1413
	while(this->graph->valid(e.e) && !enabled(e)) { 
marci@653
  1414
	  this->graph->next(e.e); } 
marci@653
  1415
      }
marci@653
  1416
      return e;
marci@653
  1417
    }
marci@653
  1418
marci@653
  1419
    Node tail(Edge e) const { 
marci@653
  1420
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@653
  1421
    Node head(Edge e) const { 
marci@653
  1422
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@653
  1423
marci@653
  1424
    Node aNode(OutEdgeIt e) const { 
marci@653
  1425
      return ((!e.backward) ? this->graph->aNode(e.out) : 
marci@653
  1426
	      this->graph->aNode(e.in)); }
marci@653
  1427
    Node bNode(OutEdgeIt e) const { 
marci@653
  1428
      return ((!e.backward) ? this->graph->bNode(e.out) : 
marci@653
  1429
	      this->graph->bNode(e.in)); }
marci@653
  1430
marci@653
  1431
    Node aNode(InEdgeIt e) const { 
marci@653
  1432
      return ((!e.backward) ? this->graph->aNode(e.in) : 
marci@653
  1433
	      this->graph->aNode(e.out)); }
marci@653
  1434
    Node bNode(InEdgeIt e) const { 
marci@653
  1435
      return ((!e.backward) ? this->graph->bNode(e.in) : 
marci@653
  1436
	      this->graph->bNode(e.out)); }
marci@653
  1437
marci@653
  1438
    /// Gives back the opposite edge.
marci@653
  1439
    Edge opposite(const Edge& e) const { 
marci@653
  1440
      Edge f=e;
marci@653
  1441
      f.backward=!f.backward;
marci@653
  1442
      return f;
marci@653
  1443
    }
marci@653
  1444
marci@653
  1445
//    int nodeNum() const { return graph->nodeNum(); }
marci@653
  1446
    //FIXME
marci@653
  1447
    void edgeNum() const { }
marci@653
  1448
    //int edgeNum() const { return graph->edgeNum(); }
marci@653
  1449
marci@653
  1450
marci@653
  1451
//    int id(Node v) const { return graph->id(v); }
marci@653
  1452
marci@653
  1453
    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
marci@653
  1454
    bool valid(Edge e) const { 
marci@653
  1455
      return this->graph->valid(e);
marci@653
  1456
	//return e.forward ? graph->valid(e.out) : graph->valid(e.in); 
marci@653
  1457
    }
marci@653
  1458
marci@653
  1459
    bool forward(const Edge& e) const { return !e.backward; }
marci@653
  1460
    bool backward(const Edge& e) const { return e.backward; }
marci@653
  1461
marci@653
  1462
//     void augment(const Edge& e, Number a) const {
marci@653
  1463
//       if (!e.backward)  
marci@653
  1464
// // 	flow->set(e.out, flow->get(e.out)+a);
marci@653
  1465
// 	flow->set(e, (*flow)[e]+a);
marci@653
  1466
//       else  
marci@653
  1467
// // 	flow->set(e.in, flow->get(e.in)-a);
marci@653
  1468
// 	flow->set(e, (*flow)[e]-a);
marci@650
  1469
//     }
marci@650
  1470
marci@653
  1471
    bool enabled(const Edge& e) const { 
marci@653
  1472
      if (!e.backward) 
marci@653
  1473
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@653
  1474
	//return ((*capacity)[e]-(*flow)[e]);
marci@653
  1475
	return true;
marci@653
  1476
      else 
marci@653
  1477
//	return (flow->get(e.in)); 
marci@653
  1478
	//return ((*flow)[e]); 
marci@653
  1479
	return true;
marci@653
  1480
    }
marci@650
  1481
marci@653
  1482
//     Number enabled(typename Graph::OutEdgeIt out) const { 
marci@653
  1483
// //      return (capacity->get(out)-flow->get(out)); 
marci@653
  1484
//       return ((*capacity)[out]-(*flow)[out]); 
marci@653
  1485
//     }
marci@653
  1486
    
marci@653
  1487
//     Number enabled(typename Graph::InEdgeIt in) const { 
marci@653
  1488
// //      return (flow->get(in)); 
marci@653
  1489
//       return ((*flow)[in]); 
marci@650
  1490
//     }
marci@650
  1491
marci@653
  1492
    template <typename T>
marci@653
  1493
    class EdgeMap {
marci@653
  1494
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@653
  1495
    public:
marci@653
  1496
      typedef T ValueType;
marci@653
  1497
      typedef Edge KeyType;
marci@653
  1498
      EdgeMap(const OldBidirGraphWrapper<Graph>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
marci@653
  1499
      EdgeMap(const OldBidirGraphWrapper<Graph>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
marci@653
  1500
      void set(Edge e, T a) { 
marci@653
  1501
	if (!e.backward) 
marci@653
  1502
	  forward_map.set(e/*.out*/, a); 
marci@653
  1503
	else 
marci@653
  1504
	  backward_map.set(e/*.in*/, a); 
marci@653
  1505
      }
marci@653
  1506
      T operator[](Edge e) const { 
marci@653
  1507
	if (!e.backward) 
marci@653
  1508
	  return forward_map[e/*.out*/]; 
marci@653
  1509
	else 
marci@653
  1510
	  return backward_map[e/*.in*/]; 
marci@653
  1511
      }
marci@653
  1512
      void update() { 
marci@653
  1513
	forward_map.update(); 
marci@653
  1514
	backward_map.update();
marci@653
  1515
      }
marci@653
  1516
//       T get(Edge e) const { 
marci@653
  1517
// 	if (e.out_or_in) 
marci@653
  1518
// 	  return forward_map.get(e.out); 
marci@653
  1519
// 	else 
marci@653
  1520
// 	  return backward_map.get(e.in); 
marci@653
  1521
//       }
marci@653
  1522
    };
marci@653
  1523
  };
marci@650
  1524
marci@650
  1525
marci@650
  1526
marci@612
  1527
  /// \brief A bidirected graph template.
marci@612
  1528
  ///
marci@612
  1529
  /// A bidirected graph template.
marci@612
  1530
  /// Such a bidirected graph stores each pair of oppositely directed edges 
marci@612
  1531
  /// ones in the memory, i.e. a directed graph of type 
marci@612
  1532
  /// \c Graph is used for that.
marci@612
  1533
  /// As the oppositely directed edges are logically different ones 
marci@612
  1534
  /// the maps are able to attach different values for them.
marci@612
  1535
  /// \ingroup graphs
marci@612
  1536
  template<typename Graph>
marci@612
  1537
  class BidirGraph : public BidirGraphWrapper<Graph> {
marci@650
  1538
  public:
marci@612
  1539
    typedef UndirGraphWrapper<Graph> Parent;
marci@612
  1540
  protected:
marci@612
  1541
    Graph gr;
marci@612
  1542
  public:
marci@612
  1543
    BidirGraph() : BidirGraphWrapper<Graph>() { 
marci@612
  1544
      Parent::setGraph(gr); 
marci@612
  1545
    }
marci@612
  1546
  };
marci@569
  1547
marci@556
  1548
marci@650
  1549
marci@650
  1550
  template<typename Graph, typename Number,
marci@650
  1551
	   typename CapacityMap, typename FlowMap>
marci@658
  1552
  class ResForwardFilter {
marci@658
  1553
    //    const Graph* graph;
marci@650
  1554
    const CapacityMap* capacity;
marci@650
  1555
    const FlowMap* flow;
marci@650
  1556
  public:
marci@658
  1557
    ResForwardFilter(/*const Graph& _graph, */
marci@658
  1558
		     const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1559
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1560
    ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@658
  1561
    //void setGraph(const Graph& _graph) { graph=&_graph; }
marci@656
  1562
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1563
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1564
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1565
      return (Number((*flow)[e]) < Number((*capacity)[e]));
marci@650
  1566
    }
marci@650
  1567
  };
marci@650
  1568
marci@650
  1569
  template<typename Graph, typename Number,
marci@650
  1570
	   typename CapacityMap, typename FlowMap>
marci@658
  1571
  class ResBackwardFilter {
marci@658
  1572
    //const Graph* graph;
marci@650
  1573
    const CapacityMap* capacity;
marci@650
  1574
    const FlowMap* flow;
marci@650
  1575
  public:
marci@658
  1576
    ResBackwardFilter(/*const Graph& _graph,*/ 
marci@658
  1577
		      const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1578
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1579
    ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@658
  1580
    //void setGraph(const Graph& _graph) { graph=&_graph; }
marci@656
  1581
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1582
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1583
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1584
      return (Number(0) < Number((*flow)[e]));
marci@650
  1585
    }
marci@650
  1586
  };
marci@650
  1587
marci@653
  1588
  
marci@653
  1589
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1590
marci@653
  1591
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1592
  template<typename Graph, typename Number, 
marci@650
  1593
	   typename CapacityMap, typename FlowMap>
marci@653
  1594
  class ResGraphWrapper : 
marci@650
  1595
    public SubBidirGraphWrapper< 
marci@650
  1596
    Graph, 
marci@658
  1597
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1598
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
marci@650
  1599
  public:
marci@650
  1600
    typedef SubBidirGraphWrapper< 
marci@650
  1601
      Graph, 
marci@658
  1602
      ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1603
      ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
marci@650
  1604
  protected:
marci@650
  1605
    const CapacityMap* capacity;
marci@650
  1606
    FlowMap* flow;
marci@658
  1607
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
marci@658
  1608
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
marci@658
  1609
    ResGraphWrapper() : Parent(), 
marci@658
  1610
 			capacity(0), flow(0) { }
marci@658
  1611
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1612
      capacity=&_capacity;
marci@658
  1613
      forward_filter.setCapacity(_capacity);
marci@658
  1614
      backward_filter.setCapacity(_capacity);
marci@658
  1615
    }
marci@658
  1616
    void setFlowMap(FlowMap& _flow) {
marci@658
  1617
      flow=&_flow;
marci@658
  1618
      forward_filter.setFlow(_flow);
marci@658
  1619
      backward_filter.setFlow(_flow);
marci@658
  1620
    }
marci@658
  1621
//     /// \bug does graph reference needed in filtermaps??
marci@658
  1622
//     void setGraph(const Graph& _graph) { 
marci@658
  1623
//       Parent::setGraph(_graph);
marci@658
  1624
//       forward_filter.setGraph(_graph);
marci@658
  1625
//       backward_filter.setGraph(_graph);
marci@656
  1626
//     }
marci@650
  1627
  public:
marci@653
  1628
    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@650
  1629
		       FlowMap& _flow) : 
marci@650
  1630
      Parent(), capacity(&_capacity), flow(&_flow), 
marci@658
  1631
      forward_filter(/*_graph,*/ _capacity, _flow), 
marci@658
  1632
      backward_filter(/*_graph,*/ _capacity, _flow) {
marci@650
  1633
      Parent::setGraph(_graph);
marci@650
  1634
      Parent::setForwardFilterMap(forward_filter);
marci@650
  1635
      Parent::setBackwardFilterMap(backward_filter);
marci@650
  1636
    }
marci@650
  1637
marci@660
  1638
    typedef typename Parent::Edge Edge;
marci@660
  1639
marci@650
  1640
    //    bool forward(const Parent::Edge& e) const { return Parent::forward(e); }
marci@650
  1641
    //bool backward(const Edge& e) const { return e.backward; }
marci@650
  1642
marci@660
  1643
    void augment(const Edge& e, Number a) const {
marci@650
  1644
      if (Parent::forward(e))  
marci@650
  1645
// 	flow->set(e.out, flow->get(e.out)+a);
marci@650
  1646
	flow->set(e, (*flow)[e]+a);
marci@650
  1647
      else  
marci@650
  1648
	//flow->set(e.in, flow->get(e.in)-a);
marci@650
  1649
	flow->set(e, (*flow)[e]-a);
marci@650
  1650
    }
marci@650
  1651
marci@660
  1652
    /// \deprecated
marci@660
  1653
    ///
marci@660
  1654
    Number resCap(const Edge& e) const { 
marci@650
  1655
      if (Parent::forward(e)) 
marci@650
  1656
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@650
  1657
	return ((*capacity)[e]-(*flow)[e]); 
marci@650
  1658
      else 
marci@650
  1659
//	return (flow->get(e.in)); 
marci@650
  1660
	return ((*flow)[e]); 
marci@650
  1661
    }
marci@650
  1662
marci@660
  1663
    /// \brief Residual capacity map.
marci@660
  1664
    ///
marci@660
  1665
    /// In generic residual graphs the residual capacity can be obtained as a map.
marci@660
  1666
    class ResCap {
marci@660
  1667
    protected:
marci@660
  1668
      const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
marci@660
  1669
    public:
marci@660
  1670
      typedef Number ValueType;
marci@660
  1671
      typedef Edge KeyType;
marci@660
  1672
      ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) : 
marci@660
  1673
	res_graph(&_res_graph) { }
marci@660
  1674
      Number operator[](const Edge& e) const { 
marci@660
  1675
	if (res_graph->forward(e)) 
marci@660
  1676
	  //	return (capacity->get(e.out)-flow->get(e.out)); 
marci@660
  1677
	  return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 
marci@660
  1678
	else 
marci@660
  1679
	  //	return (flow->get(e.in)); 
marci@660
  1680
	  return (*(res_graph->flow))[e]; 
marci@660
  1681
      }
marci@660
  1682
      /// \bug not needed with dynamic maps, or does it?
marci@660
  1683
      void update() { }
marci@660
  1684
    };
marci@660
  1685
marci@650
  1686
  };
marci@650
  1687
marci@650
  1688
marci@556
  1689
  template<typename Graph, typename Number, 
marci@556
  1690
	   typename CapacityMap, typename FlowMap>
marci@653
  1691
  class OldResGraphWrapper : public GraphWrapper<Graph> {
marci@650
  1692
  public:
marci@650
  1693
    typedef GraphWrapper<Graph> Parent; 
marci@556
  1694
  protected:
marci@556
  1695
    const CapacityMap* capacity;
marci@556
  1696
    FlowMap* flow;
marci@556
  1697
marci@653
  1698
    OldResGraphWrapper() : GraphWrapper<Graph>(0), 
marci@556
  1699
			capacity(0), flow(0) { }
marci@560
  1700
    void setCapacityMap(const CapacityMap& _capacity) {
marci@560
  1701
      capacity=&_capacity;
marci@556
  1702
    }
marci@556
  1703
    void setFlowMap(FlowMap& _flow) {
marci@556
  1704
      flow=&_flow;
marci@556
  1705
    }
marci@556
  1706
marci@556
  1707
  public:
marci@556
  1708
marci@653
  1709
    OldResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@556
  1710
		    FlowMap& _flow) : 
marci@556
  1711
      GraphWrapper<Graph>(_graph), capacity(&_capacity), flow(&_flow) { }
marci@556
  1712
marci@556
  1713
    class Edge; 
marci@556
  1714
    class OutEdgeIt; 
marci@556
  1715
    friend class Edge; 
marci@556
  1716
    friend class OutEdgeIt; 
marci@556
  1717
marci@556
  1718
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  1719
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
  1720
    class Edge : public Graph::Edge {
marci@653
  1721
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1722
    protected:
marci@565
  1723
      bool backward; //true, iff backward
marci@556
  1724
//      typename Graph::Edge e;
marci@556
  1725
    public:
marci@556
  1726
      Edge() { }
marci@565
  1727
      Edge(const typename Graph::Edge& _e, bool _backward) : 
marci@565
  1728
	Graph::Edge(_e), backward(_backward) { }
marci@565
  1729
      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
marci@556
  1730
//the unique invalid iterator
marci@556
  1731
      friend bool operator==(const Edge& u, const Edge& v) { 
marci@565
  1732
	return (v.backward==u.backward && 
marci@556
  1733
		static_cast<typename Graph::Edge>(u)==
marci@556
  1734
		static_cast<typename Graph::Edge>(v));
marci@556
  1735
      } 
marci@556
  1736
      friend bool operator!=(const Edge& u, const Edge& v) { 
marci@565
  1737
	return (v.backward!=u.backward || 
marci@556
  1738
		static_cast<typename Graph::Edge>(u)!=
marci@556
  1739
		static_cast<typename Graph::Edge>(v));
marci@556
  1740
      } 
marci@556
  1741
    };
marci@556
  1742
marci@556
  1743
    class OutEdgeIt {
marci@653
  1744
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1745
    protected:
marci@556
  1746
      typename Graph::OutEdgeIt out;
marci@556
  1747
      typename Graph::InEdgeIt in;
marci@565
  1748
      bool backward;
marci@556
  1749
    public:
marci@556
  1750
      OutEdgeIt() { }
marci@556
  1751
      //FIXME
marci@556
  1752
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@565
  1753
      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@556
  1754
//the unique invalid iterator
marci@653
  1755
      OutEdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) { 
marci@565
  1756
	backward=false;
marci@569
  1757
	_G.graph->first(out, v);
marci@569
  1758
	while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
marci@569
  1759
	if (!_G.graph->valid(out)) {
marci@565
  1760
	  backward=true;
marci@569
  1761
	  _G.graph->first(in, v);
marci@569
  1762
	  while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
marci@556
  1763
	}
marci@556
  1764
      }
marci@556
  1765
      operator Edge() const { 
marci@556
  1766
//	Edge e;
marci@556
  1767
//	e.forward=this->forward;
marci@556
  1768
//	if (this->forward) e=out; else e=in;
marci@556
  1769
//	return e;
marci@565
  1770
	if (this->backward) 
marci@565
  1771
	  return Edge(in, this->backward); 
marci@556
  1772
	else 
marci@565
  1773
	  return Edge(out, this->backward);
marci@556
  1774
      }
marci@556
  1775
    };
marci@556
  1776
marci@556
  1777
    class InEdgeIt {
marci@653
  1778
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1779
    protected:
marci@556
  1780
      typename Graph::OutEdgeIt out;
marci@556
  1781
      typename Graph::InEdgeIt in;
marci@565
  1782
      bool backward;
marci@556
  1783
    public:
marci@556
  1784
      InEdgeIt() { }
marci@556
  1785
      //FIXME
marci@556
  1786
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@565
  1787
      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@556
  1788
//the unique invalid iterator
marci@653
  1789
      InEdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) { 
marci@565
  1790
	backward=false;
marci@569
  1791
	_G.graph->first(in, v);
marci@569
  1792
	while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
marci@569
  1793
	if (!_G.graph->valid(in)) {
marci@565
  1794
	  backward=true;
marci@569
  1795
	  _G.graph->first(out, v);
marci@569
  1796
	  while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
marci@556
  1797
	}
marci@556
  1798
      }
marci@556
  1799
      operator Edge() const { 
marci@556
  1800
//	Edge e;
marci@556
  1801
//	e.forward=this->forward;
marci@556
  1802
//	if (this->forward) e=out; else e=in;
marci@556
  1803
//	return e;
marci@565
  1804
	if (this->backward) 
marci@565
  1805
	  return Edge(out, this->backward); 
marci@556
  1806
	else 
marci@565
  1807
	  return Edge(in, this->backward);
marci@556
  1808
      }
marci@556
  1809
    };
marci@556
  1810
marci@556
  1811
    class EdgeIt {
marci@653
  1812
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1813
    protected:
marci@556
  1814
      typename Graph::EdgeIt e;
marci@565
  1815
      bool backward;
marci@556
  1816
    public:
marci@556
  1817
      EdgeIt() { }
marci@565
  1818
      EdgeIt(const Invalid& i) : e(i), backward(true) { }
marci@653
  1819
      EdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) { 
marci@565
  1820
	backward=false;
marci@569
  1821
	_G.graph->first(e);
marci@569
  1822
	while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
marci@569
  1823
	if (!_G.graph->valid(e)) {
marci@565
  1824
	  backward=true;
marci@569
  1825
	  _G.graph->first(e);
marci@569
  1826
	  while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
marci@556
  1827
	}
marci@556
  1828
      }
marci@556
  1829
      operator Edge() const { 
marci@565
  1830
	return Edge(e, this->backward);
marci@556
  1831
      }
marci@556
  1832
    };
marci@556
  1833
marci@556
  1834
    using GraphWrapper<Graph>::first;
marci@556
  1835
//     NodeIt& first(NodeIt& i) const { 
marci@556
  1836
//       i=NodeIt(*this); return i;
marci@556
  1837
//     }
marci@556
  1838
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  1839
      i=OutEdgeIt(*this, p); return i;
marci@556
  1840
    }
marci@556
  1841
//    FIXME not tested
marci@556
  1842
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
  1843
      i=InEdgeIt(*this, p); return i;
marci@556
  1844
    }
marci@556
  1845
    EdgeIt& first(EdgeIt& i) const { 
marci@556
  1846
      i=EdgeIt(*this); return i;
marci@556
  1847
    }
marci@556
  1848
  
marci@556
  1849
    using GraphWrapper<Graph>::next;
marci@556
  1850
//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
marci@556
  1851
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@565
  1852
      if (!e.backward) {
marci@556
  1853
	Node v=this->graph->aNode(e.out);
marci@556
  1854
	this->graph->next(e.out);
marci@556
  1855
	while( this->graph->valid(e.out) && !(resCap(e)>0) ) { 
marci@556
  1856
	  this->graph->next(e.out); }
marci@556
  1857
	if (!this->graph->valid(e.out)) {
marci@565
  1858
	  e.backward=true;
marci@556
  1859
	  this->graph->first(e.in, v); 
marci@556
  1860
	  while( this->graph->valid(e.in) && !(resCap(e)>0) ) { 
marci@556
  1861
	    this->graph->next(e.in); }
marci@556
  1862
	}
marci@556
  1863
      } else {
marci@556
  1864
	this->graph->next(e.in);
marci@556
  1865
	while( this->graph->valid(e.in) && !(resCap(e)>0) ) { 
marci@556
  1866
	  this->graph->next(e.in); } 
marci@556
  1867
      }
marci@556
  1868
      return e;
marci@556
  1869
    }
marci@556
  1870
//     FIXME Not tested
marci@556
  1871
    InEdgeIt& next(InEdgeIt& e) const { 
marci@565
  1872
      if (!e.backward) {
marci@556
  1873
	Node v=this->graph->aNode(e.in);
marci@556
  1874
	this->graph->next(e.in);
marci@556
  1875
	while( this->graph->valid(e.in) && !(resCap(e)>0) ) { 
marci@556
  1876
	  this->graph->next(e.in); }
marci@556
  1877
	if (!this->graph->valid(e.in)) {
marci@565
  1878
	  e.backward=true;
marci@556
  1879
	  this->graph->first(e.out, v); 
marci@556
  1880
	  while( this->graph->valid(e.out) && !(resCap(e)>0) ) { 
marci@556
  1881
	    this->graph->next(e.out); }
marci@556
  1882
	}
marci@556
  1883
      } else {
marci@556
  1884
	this->graph->next(e.out);
marci@556
  1885
	while( this->graph->valid(e.out) && !(resCap(e)>0) ) { 
marci@556
  1886
	  this->graph->next(e.out); } 
marci@556
  1887
      }
marci@556
  1888
      return e;
marci@556
  1889
    }
marci@556
  1890
    EdgeIt& next(EdgeIt& e) const {
marci@565
  1891
      if (!e.backward) {
marci@556
  1892
	this->graph->next(e.e);
marci@556
  1893
	while( this->graph->valid(e.e) && !(resCap(e)>0) ) { 
marci@556
  1894
	  this->graph->next(e.e); }
marci@556
  1895
	if (!this->graph->valid(e.e)) {
marci@565
  1896
	  e.backward=true;
marci@556
  1897
	  this->graph->first(e.e); 
marci@556
  1898
	  while( this->graph->valid(e.e) && !(resCap(e)>0) ) { 
marci@556
  1899
	    this->graph->next(e.e); }
marci@556
  1900
	}
marci@556
  1901
      } else {
marci@556
  1902
	this->graph->next(e.e);
marci@556
  1903
	while( this->graph->valid(e.e) && !(resCap(e)>0) ) { 
marci@556
  1904
	  this->graph->next(e.e); } 
marci@556
  1905
      }
marci@556
  1906
      return e;
marci@556
  1907
    }
marci@556
  1908
marci@556
  1909
    Node tail(Edge e) const { 
marci@565
  1910
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@556
  1911
    Node head(Edge e) const { 
marci@565
  1912
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@556
  1913
marci@556
  1914
    Node aNode(OutEdgeIt e) const { 
marci@565
  1915
      return ((!e.backward) ? this->graph->aNode(e.out) : 
marci@556
  1916
	      this->graph->aNode(e.in)); }
marci@556
  1917
    Node bNode(OutEdgeIt e) const { 
marci@565
  1918
      return ((!e.backward) ? this->graph->bNode(e.out) : 
marci@556
  1919
	      this->graph->bNode(e.in)); }
marci@556
  1920
marci@556
  1921
    Node aNode(InEdgeIt e) const { 
marci@565
  1922
      return ((!e.backward) ? this->graph->aNode(e.in) : 
marci@556
  1923
	      this->graph->aNode(e.out)); }
marci@556
  1924
    Node bNode(InEdgeIt e) const { 
marci@565
  1925
      return ((!e.backward) ? this->graph->bNode(e.in) : 
marci@556
  1926
	      this->graph->bNode(e.out)); }
marci@556
  1927
marci@556
  1928
//    int nodeNum() const { return graph->nodeNum(); }
marci@556
  1929
    //FIXME
marci@556
  1930
    void edgeNum() const { }
marci@556
  1931
    //int edgeNum() const { return graph->edgeNum(); }
marci@556
  1932
marci@556
  1933
marci@556
  1934
//    int id(Node v) const { return graph->id(v); }
marci@556
  1935
marci@556
  1936
    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
marci@556
  1937
    bool valid(Edge e) const { 
marci@556
  1938
      return this->graph->valid(e);
marci@556
  1939
	//return e.forward ? graph->valid(e.out) : graph->valid(e.in); 
marci@556
  1940
    }
marci@556
  1941
marci@565
  1942
    bool forward(const Edge& e) const { return !e.backward; }
marci@565
  1943
    bool backward(const Edge& e) const { return e.backward; }
marci@556
  1944
marci@556
  1945
    void augment(const Edge& e, Number a) const {
marci@565
  1946
      if (!e.backward)  
marci@556
  1947
// 	flow->set(e.out, flow->get(e.out)+a);
marci@556
  1948
	flow->set(e, (*flow)[e]+a);
marci@556
  1949
      else  
marci@556
  1950
// 	flow->set(e.in, flow->get(e.in)-a);
marci@556
  1951
	flow->set(e, (*flow)[e]-a);
marci@556
  1952
    }
marci@556
  1953
marci@556
  1954
    Number resCap(const Edge& e) const { 
marci@565
  1955
      if (!e.backward) 
marci@556
  1956
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@556
  1957
	return ((*capacity)[e]-(*flow)[e]); 
marci@556
  1958
      else 
marci@556
  1959
//	return (flow->get(e.in)); 
marci@556
  1960
	return ((*flow)[e]); 
marci@556
  1961
    }
marci@556
  1962
marci@556
  1963
//     Number resCap(typename Graph::OutEdgeIt out) const { 
marci@556
  1964
// //      return (capacity->get(out)-flow->get(out)); 
marci@556
  1965
//       return ((*capacity)[out]-(*flow)[out]); 
marci@556
  1966
//     }
marci@556
  1967
    
marci@556
  1968
//     Number resCap(typename Graph::InEdgeIt in) const { 
marci@556
  1969
// //      return (flow->get(in)); 
marci@556
  1970
//       return ((*flow)[in]); 
marci@556
  1971
//     }
marci@556
  1972
marci@556
  1973
    template <typename T>
marci@556
  1974
    class EdgeMap {
marci@556
  1975
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@556
  1976
    public:
marci@624
  1977
      typedef T ValueType;
marci@624
  1978
      typedef Edge KeyType;
marci@653
  1979
      EdgeMap(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
marci@653
  1980
      EdgeMap(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
marci@556
  1981
      void set(Edge e, T a) { 
marci@565
  1982
	if (!e.backward) 
marci@624
  1983
	  forward_map.set(e/*.out*/, a); 
marci@556
  1984
	else 
marci@624
  1985
	  backward_map.set(e/*.in*/, a); 
marci@556
  1986
      }
marci@556
  1987
      T operator[](Edge e) const { 
marci@565
  1988
	if (!e.backward) 
marci@624
  1989
	  return forward_map[e/*.out*/]; 
marci@556
  1990
	else 
marci@624
  1991
	  return backward_map[e/*.in*/]; 
marci@556
  1992
      }
marci@625
  1993
      void update() { 
marci@625
  1994
	forward_map.update(); 
marci@625
  1995
	backward_map.update();
marci@625
  1996
      }
marci@556
  1997
//       T get(Edge e) const { 
marci@556
  1998
// 	if (e.out_or_in) 
marci@556
  1999
// 	  return forward_map.get(e.out); 
marci@556
  2000
// 	else 
marci@556
  2001
// 	  return backward_map.get(e.in); 
marci@556
  2002
//       }
marci@556
  2003
    };
marci@556
  2004
  };
marci@556
  2005
marci@569
  2006
marci@569
  2007
marci@612
  2008
  /// For blocking flows.
marci@556
  2009
marci@612
  2010
  /// This graph wrapper is used for Dinits blocking flow computations.
marci@612
  2011
  /// For each node, an out-edge is stored which is used when the 
marci@612
  2012
  /// \code 
marci@612
  2013
  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
marci@612
  2014
  /// \endcode
marci@612
  2015
  /// is called. 
marci@556
  2016
  ///
marci@556
  2017
  ///\author Marton Makai
marci@556
  2018
  template<typename Graph, typename FirstOutEdgesMap>
marci@556
  2019
  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
marci@650
  2020
  public:
marci@650
  2021
    typedef GraphWrapper<Graph> Parent; 
marci@556
  2022
  protected:
marci@556
  2023
    FirstOutEdgesMap* first_out_edges;
marci@556
  2024
  public:
marci@556
  2025
    ErasingFirstGraphWrapper(Graph& _graph, 
marci@556
  2026
			     FirstOutEdgesMap& _first_out_edges) : 
marci@556
  2027
      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }  
marci@556
  2028
marci@556
  2029
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  2030
//     class NodeIt { 
marci@556
  2031
//       friend class GraphWrapper<Graph>;
marci@556
  2032
//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@556
  2033
//       typename Graph::NodeIt n;
marci@556
  2034
//      public:
marci@556
  2035
//       NodeIt() { }
marci@556
  2036
//       NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
marci@556
  2037
//       NodeIt(const Invalid& i) : n(i) { }
marci@556
  2038
//       NodeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) : 
marci@556
  2039
// 	n(*(_G.graph)) { }
marci@556
  2040
//       operator Node() const { return Node(typename Graph::Node(n)); }
marci@556
  2041
//     };
marci@556
  2042
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@777
  2043
    class OutEdgeIt : public Edge { 
marci@556
  2044
      friend class GraphWrapper<Graph>;
marci@556
  2045
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  2046
      const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
marci@556
  2047
    public:
marci@556
  2048
      OutEdgeIt() { }
marci@777
  2049
      //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@777
  2050
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@777
  2051
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  2052
		const Node& n) : 
marci@777
  2053
	Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
marci@777
  2054
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  2055
		const Edge& e) : 
marci@777
  2056
	Edge(e), gw(&_gw) { }
marci@777
  2057
      OutEdgeIt& operator++() { 
marci@777
  2058
	*(static_cast<Edge*>(this))=
marci@777
  2059
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@777
  2060
	return *this; 
marci@777
  2061
      }
marci@556
  2062
    };
marci@777
  2063
//     class InEdgeIt { 
marci@777
  2064
//       friend class GraphWrapper<Graph>;
marci@777
  2065
//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  2066
// //      typedef typename Graph::InEdgeIt GraphInEdgeIt;
marci@777
  2067
//       typename Graph::InEdgeIt e;
marci@777
  2068
//     public:
marci@777
  2069
//       InEdgeIt() { }
marci@777
  2070
//       InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
marci@777
  2071
//       InEdgeIt(const Invalid& i) : e(i) { }
marci@777
  2072
//       InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G, 
marci@777
  2073
// 	       const Node& _n) : 
marci@777
  2074
// 	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@777
  2075
//       operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@777
  2076
//     };	
marci@556
  2077
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@777
  2078
//     class EdgeIt { 
marci@777
  2079
//       friend class GraphWrapper<Graph>;
marci@777
  2080
//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  2081
// //      typedef typename Graph::EdgeIt GraphEdgeIt;
marci@777
  2082
//       typename Graph::EdgeIt e;
marci@777
  2083
//     public:
marci@777
  2084
//       EdgeIt() { }
marci@777
  2085
//       EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
marci@777
  2086
//       EdgeIt(const Invalid& i) : e(i) { }
marci@777
  2087
//       EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) : 
marci@777
  2088
// 	e(*(_G.graph)) { }
marci@777
  2089
//       operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@777
  2090
//     };
marci@556
  2091
marci@556
  2092
    using GraphWrapper<Graph>::first;
marci@556
  2093
//     NodeIt& first(NodeIt& i) const { 
marci@556
  2094
//       i=NodeIt(*this); return i;
marci@556
  2095
//     }
marci@556
  2096
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  2097
      i=OutEdgeIt(*this, p); return i;
marci@556
  2098
    }
marci@777
  2099
//     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@777
  2100
//       i=InEdgeIt(*this, p); return i;
marci@777
  2101
//     }
marci@777
  2102
//     EdgeIt& first(EdgeIt& i) const { 
marci@777
  2103
//       i=EdgeIt(*this); return i;
marci@777
  2104
//     }
marci@556
  2105
marci@777
  2106
//     using GraphWrapper<Graph>::next;
marci@777
  2107
// //    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
marci@777
  2108
//     OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@777
  2109
//     InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@777
  2110
//     EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }    
marci@556
  2111
    
marci@777
  2112
//     Node aNode(const OutEdgeIt& e) const { 
marci@777
  2113
//       return Node(this->graph->aNode(e.e)); }
marci@777
  2114
//     Node aNode(const InEdgeIt& e) const { 
marci@777
  2115
//       return Node(this->graph->aNode(e.e)); }
marci@777
  2116
//     Node bNode(const OutEdgeIt& e) const { 
marci@777
  2117
//       return Node(this->graph->bNode(e.e)); }
marci@777
  2118
//     Node bNode(const InEdgeIt& e) const { 
marci@777
  2119
//       return Node(this->graph->bNode(e.e)); }
marci@556
  2120
marci@777
  2121
    void erase(const Edge& e) const {
marci@777
  2122
      Node n=tail(e);
marci@777
  2123
      typename Graph::OutEdgeIt f(*graph, n);
marci@777
  2124
      ++f;
marci@777
  2125
      first_out_edges->set(n, f);
marci@556
  2126
    }
marci@556
  2127
  };
marci@556
  2128
marci@556
  2129
  ///@}
marci@556
  2130
marci@556
  2131
} //namespace hugo
marci@556
  2132
marci@556
  2133
#endif //HUGO_GRAPH_WRAPPER_H
marci@556
  2134