src/work/marci/graph_wrapper.h
author alpar
Wed, 10 Mar 2004 17:47:54 +0000
changeset 164 970b265696b0
parent 156 a34e5a909e97
child 168 27fbd1559fb7
permissions -rw-r--r--
New graph interface
marci@76
     1
// -*-mode: c++; -*-
marci@76
     2
#ifndef GRAPH_WRAPPER_H
marci@76
     3
#define GRAPH_WRAPPER_H
marci@76
     4
alpar@105
     5
namespace hugo {
marci@76
     6
marci@76
     7
  template<typename Graph>
marci@76
     8
  class TrivGraphWrapper {
marci@76
     9
    Graph* graph;
marci@76
    10
  
marci@76
    11
  public:
marci@76
    12
    typedef Graph BaseGraph;
marci@76
    13
marci@76
    14
    typedef typename Graph::NodeIt NodeIt;
marci@76
    15
    typedef typename Graph::EachNodeIt EachNodeIt;
marci@76
    16
marci@155
    17
    typedef typename Graph::EdgeIt EdgeIt;
marci@76
    18
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@76
    19
    typedef typename Graph::InEdgeIt InEdgeIt;
marci@155
    20
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@76
    21
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@76
    22
    
marci@76
    23
    template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
marci@76
    24
    template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
marci@76
    25
      return graph->getFirst(i, p); }
marci@155
    26
    
marci@155
    27
    template<typename I> I getNext(const I& i) const { 
marci@155
    28
      return graph->getNext(i); }
marci@155
    29
    template<typename I> I& next(I &i) const { return graph->next(i); }    
marci@76
    30
marci@76
    31
    template< typename It > It first() const { 
marci@76
    32
      It e; getFirst(e); return e; }
marci@76
    33
marci@155
    34
    template< typename It > It first(const NodeIt& v) const { 
marci@76
    35
      It e; getFirst(e, v); return e; }
marci@76
    36
marci@76
    37
    NodeIt head(const EdgeIt& e) const { return graph->head(e); }
marci@76
    38
    NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
marci@155
    39
marci@155
    40
    template<typename I> bool valid(const I& i) const 
marci@155
    41
      { return graph->valid(i); }
marci@155
    42
  
marci@155
    43
    //template<typename I> void setInvalid(const I &i);
marci@155
    44
    //{ return graph->setInvalid(i); }
marci@155
    45
marci@155
    46
    int nodeNum() const { return graph->nodeNum(); }
marci@155
    47
    int edgeNum() const { return graph->edgeNum(); }
marci@76
    48
  
marci@76
    49
    template<typename I> NodeIt aNode(const I& e) const { 
marci@76
    50
      return graph->aNode(e); }
marci@76
    51
    template<typename I> NodeIt bNode(const I& e) const { 
marci@76
    52
      return graph->bNode(e); }
marci@76
    53
  
marci@76
    54
    NodeIt addNode() const { return graph->addNode(); }
marci@76
    55
    EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { 
marci@76
    56
      return graph->addEdge(tail, head); }
marci@76
    57
  
marci@76
    58
    template<typename I> void erase(const I& i) const { graph->erase(i); }
marci@76
    59
  
marci@76
    60
    void clear() const { graph->clear(); }
marci@155
    61
    
marci@76
    62
    template<typename T> class NodeMap : public Graph::NodeMap<T> { 
marci@76
    63
    public:
marci@155
    64
      NodeMap(const TrivGraphWrapper<Graph>& _G) : 
marci@158
    65
	Graph::NodeMap<T>(_G.getGraph()) { }
marci@155
    66
      NodeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
marci@158
    67
	Graph::NodeMap<T>(_G.getGraph(), a) { }
marci@76
    68
    };
marci@155
    69
    template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
marci@155
    70
    public:
marci@155
    71
      EdgeMap(const TrivGraphWrapper<Graph>& _G) : 
marci@158
    72
	Graph::EdgeMap<T>(_G.getGraph()) { }
marci@155
    73
      EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) : 
marci@158
    74
	Graph::EdgeMap<T>(_G.getGraph(), a) { }
marci@155
    75
    };
marci@155
    76
    
marci@76
    77
    void setGraph(Graph& _graph) { graph = &_graph; }
marci@155
    78
    Graph& getGraph() const { return (*graph); }
marci@76
    79
  
marci@76
    80
    //TrivGraphWrapper() : graph(0) { }
marci@76
    81
    TrivGraphWrapper(Graph& _graph) : graph(&_graph) { }
marci@76
    82
  };
marci@76
    83
marci@76
    84
  template<typename Graph>
marci@76
    85
  class RevGraphWrapper
marci@76
    86
  {
marci@76
    87
    Graph* graph;
marci@76
    88
  
marci@76
    89
  public:
marci@76
    90
    typedef Graph BaseGraph;
marci@76
    91
marci@155
    92
    typedef typename Graph::NodeIt NodeIt;    
marci@76
    93
    typedef typename Graph::EachNodeIt EachNodeIt;
marci@76
    94
  
marci@155
    95
    typedef typename Graph::EdgeIt EdgeIt;
marci@76
    96
    typedef typename Graph::OutEdgeIt InEdgeIt;
marci@76
    97
    typedef typename Graph::InEdgeIt OutEdgeIt;
marci@155
    98
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@76
    99
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@76
   100
    
marci@76
   101
    template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
marci@76
   102
    template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
marci@76
   103
      return graph->getFirst(i, p); }
marci@155
   104
marci@155
   105
    template<typename I> I getNext(const I& i) const { 
marci@155
   106
      return graph->getNext(i); }
marci@155
   107
    template<typename I> I& next(I &i) const { return graph->next(i); }    
marci@76
   108
marci@76
   109
    template< typename It > It first() const { 
marci@76
   110
      It e; getFirst(e); return e; }
marci@76
   111
marci@155
   112
    template< typename It > It first(const NodeIt& v) const { 
marci@76
   113
      It e; getFirst(e, v); return e; }
marci@76
   114
marci@76
   115
    NodeIt head(const EdgeIt& e) const { return graph->tail(e); }
marci@76
   116
    NodeIt tail(const EdgeIt& e) const { return graph->head(e); }
marci@76
   117
  
marci@155
   118
    template<typename I> bool valid(const I& i) const 
marci@155
   119
      { return graph->valid(i); }
marci@155
   120
  
marci@155
   121
    //template<typename I> void setInvalid(const I &i);
marci@155
   122
    //{ return graph->setInvalid(i); }
marci@155
   123
  
marci@76
   124
    template<typename I> NodeIt aNode(const I& e) const { 
marci@76
   125
      return graph->aNode(e); }
marci@76
   126
    template<typename I> NodeIt bNode(const I& e) const { 
marci@76
   127
      return graph->bNode(e); }
marci@155
   128
marci@155
   129
    NodeIt addNode() const { return graph->addNode(); }
marci@155
   130
    EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { 
marci@76
   131
      return graph->addEdge(tail, head); }
marci@76
   132
  
marci@155
   133
    int nodeNum() const { return graph->nodeNum(); }
marci@155
   134
    int edgeNum() const { return graph->edgeNum(); }
marci@76
   135
  
marci@155
   136
    template<typename I> void erase(const I& i) const { graph->erase(i); }
marci@76
   137
  
marci@155
   138
    void clear() const { graph->clear(); }
marci@155
   139
marci@155
   140
    template<typename T> class NodeMap : public Graph::NodeMap<T> { 
marci@155
   141
    public:
marci@155
   142
      NodeMap(const RevGraphWrapper<Graph>& _G) : 
marci@158
   143
	Graph::NodeMap<T>(_G.getGraph()) { }
marci@155
   144
      NodeMap(const RevGraphWrapper<Graph>& _G, T a) : 
marci@158
   145
	Graph::NodeMap<T>(_G.getGraph(), a) { }
marci@155
   146
    };
marci@155
   147
    template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
marci@155
   148
    public:
marci@155
   149
      EdgeMap(const RevGraphWrapper<Graph>& _G) : 
marci@158
   150
	Graph::EdgeMap<T>(_G.getGraph()) { }
marci@155
   151
      EdgeMap(const RevGraphWrapper<Graph>& _G, T a) : 
marci@158
   152
	Graph::EdgeMap<T>(_G.getGraph(), a) { }
marci@155
   153
    };
marci@155
   154
marci@76
   155
    void setGraph(Graph& _graph) { graph = &_graph; }
marci@155
   156
    Graph& getGraph() const { return (*graph); }
marci@76
   157
marci@76
   158
    //RevGraphWrapper() : graph(0) { }
marci@76
   159
    RevGraphWrapper(Graph& _graph) : graph(&_graph) { }
marci@76
   160
  };
marci@76
   161
marci@155
   162
marci@158
   163
  template<typename Graph>
marci@158
   164
  class UndirGraphWrapper {
marci@158
   165
    Graph* graph;
marci@158
   166
  
marci@158
   167
  public:
marci@158
   168
    typedef Graph BaseGraph;
marci@158
   169
marci@158
   170
    typedef typename Graph::NodeIt NodeIt;
marci@158
   171
    typedef typename Graph::EachNodeIt EachNodeIt;
marci@158
   172
marci@158
   173
    //typedef typename Graph::EdgeIt EdgeIt;
marci@158
   174
    //typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@158
   175
    //typedef typename Graph::InEdgeIt InEdgeIt;
marci@158
   176
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@158
   177
    //typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@158
   178
marci@158
   179
    //private:
marci@158
   180
    typedef typename Graph::EdgeIt GraphEdgeIt;
marci@158
   181
    typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
marci@158
   182
    typedef typename Graph::InEdgeIt GraphInEdgeIt;
marci@158
   183
    //public:
marci@158
   184
marci@158
   185
    class EdgeIt {
marci@158
   186
      friend class UndirGraphWrapper<Graph>;
marci@158
   187
      bool out_or_in; //true iff out
marci@158
   188
      GraphOutEdgeIt out;
marci@158
   189
      GraphInEdgeIt in;
marci@158
   190
    public:
marci@158
   191
      EdgeIt() : out_or_in(true), out(), in() { }
marci@158
   192
      operator GraphEdgeIt() const {
marci@158
   193
	if (out_or_in) return(out); else return(in);
marci@158
   194
      }
marci@158
   195
    };
marci@158
   196
marci@158
   197
    class OutEdgeIt : public EdgeIt {
marci@158
   198
      friend class UndirGraphWrapper<Graph>;
marci@158
   199
      //bool out_or_in; //true iff out
marci@158
   200
      //GraphOutEdgeIt out;
marci@158
   201
      //GraphInEdgeIt in;
marci@158
   202
    public:
marci@158
   203
      OutEdgeIt() : EdgeIt() { }
marci@158
   204
      OutEdgeIt(const UndirGraphWrapper& _G, const NodeIt& n) : EdgeIt() { 
marci@158
   205
	_G.graph->getFirst(out, n);
marci@158
   206
	if (!(_G.graph->valid(out))) {
marci@158
   207
	  out_or_in=false;
marci@158
   208
	  _G.graph->getFirst(in, n);
marci@158
   209
	}
marci@158
   210
      }
marci@158
   211
    };
marci@158
   212
marci@158
   213
    OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const {
marci@158
   214
      e.out_or_in=true;
marci@158
   215
      graph->getFirst(e.out, n);
marci@158
   216
      if (!(graph->valid(e.out))) {
marci@158
   217
	e.out_or_in=false;
marci@158
   218
	graph->getFirst(e.in, n);
marci@158
   219
      }
marci@158
   220
      return e;
marci@158
   221
    }
marci@158
   222
marci@158
   223
    OutEdgeIt& next(OutEdgeIt& e) const {
marci@158
   224
      if (e.out_or_in) {
marci@158
   225
	NodeIt n=graph->tail(e.out);
marci@158
   226
	graph->next(e.out);
marci@158
   227
	if (!graph->valid(e.out)) {
marci@158
   228
	  e.out_or_in=false;
marci@158
   229
	  graph->getFirst(e.in, n);
marci@158
   230
	}
marci@158
   231
      } else {
marci@158
   232
	graph->next(e.in);
marci@158
   233
      }
marci@158
   234
      return e;
marci@158
   235
    }
marci@158
   236
marci@158
   237
    NodeIt aNode(const OutEdgeIt& e) const { 
marci@158
   238
      if (e.out_or_in) return graph->tail(e); else return graph->head(e); }
marci@158
   239
    NodeIt bNode(const OutEdgeIt& e) const { 
marci@158
   240
      if (e.out_or_in) return graph->head(e); else return graph->tail(e); }
marci@158
   241
marci@158
   242
    typedef OutEdgeIt InEdgeIt; 
marci@158
   243
marci@158
   244
    template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
marci@158
   245
//     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
marci@158
   246
//       return graph->getFirst(i, p); }
marci@158
   247
    
marci@158
   248
    template<typename I> I getNext(const I& i) const { 
marci@158
   249
      return graph->getNext(i); }
marci@158
   250
    template<typename I> I& next(I &i) const { return graph->next(i); }    
marci@158
   251
marci@158
   252
    template< typename It > It first() const { 
marci@158
   253
      It e; getFirst(e); return e; }
marci@158
   254
marci@158
   255
    template< typename It > It first(const NodeIt& v) const { 
marci@158
   256
      It e; getFirst(e, v); return e; }
marci@158
   257
marci@158
   258
    NodeIt head(const EdgeIt& e) const { return graph->head(e); }
marci@158
   259
    NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
marci@158
   260
marci@158
   261
    template<typename I> bool valid(const I& i) const 
marci@158
   262
      { return graph->valid(i); }
marci@158
   263
  
marci@158
   264
    //template<typename I> void setInvalid(const I &i);
marci@158
   265
    //{ return graph->setInvalid(i); }
marci@158
   266
marci@158
   267
    int nodeNum() const { return graph->nodeNum(); }
marci@158
   268
    int edgeNum() const { return graph->edgeNum(); }
marci@158
   269
  
marci@158
   270
//     template<typename I> NodeIt aNode(const I& e) const { 
marci@158
   271
//       return graph->aNode(e); }
marci@158
   272
//     template<typename I> NodeIt bNode(const I& e) const { 
marci@158
   273
//       return graph->bNode(e); }
marci@158
   274
  
marci@158
   275
    NodeIt addNode() const { return graph->addNode(); }
marci@158
   276
    EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { 
marci@158
   277
      return graph->addEdge(tail, head); }
marci@158
   278
  
marci@158
   279
    template<typename I> void erase(const I& i) const { graph->erase(i); }
marci@158
   280
  
marci@158
   281
    void clear() const { graph->clear(); }
marci@158
   282
    
marci@158
   283
    template<typename T> class NodeMap : public Graph::NodeMap<T> { 
marci@158
   284
    public:
marci@158
   285
      NodeMap(const UndirGraphWrapper<Graph>& _G) : 
marci@158
   286
	Graph::NodeMap<T>(_G.getGraph()) { }
marci@158
   287
      NodeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
marci@158
   288
	Graph::NodeMap<T>(_G.getGraph(), a) { }
marci@158
   289
    };
marci@158
   290
    template<typename T> class EdgeMap : public Graph::EdgeMap<T> { 
marci@158
   291
    public:
marci@158
   292
      EdgeMap(const UndirGraphWrapper<Graph>& _G) : 
marci@158
   293
	Graph::EdgeMap<T>(_G.getGraph()) { }
marci@158
   294
      EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) : 
marci@158
   295
	Graph::EdgeMap<T>(_G.getGraph(), a) { }
marci@158
   296
    };
marci@158
   297
    
marci@158
   298
    void setGraph(Graph& _graph) { graph = &_graph; }
marci@158
   299
    Graph& getGraph() const { return (*graph); }
marci@158
   300
  
marci@158
   301
    //TrivGraphWrapper() : graph(0) { }
marci@158
   302
    UndirGraphWrapper(Graph& _graph) : graph(&_graph) { }
marci@158
   303
  };
marci@158
   304
marci@158
   305
marci@158
   306
marci@155
   307
//   template<typename Graph>
marci@155
   308
//   class SymGraphWrapper
marci@155
   309
//   {
marci@155
   310
//     Graph* graph;
marci@76
   311
  
marci@155
   312
//   public:
marci@155
   313
//     typedef Graph BaseGraph;
marci@155
   314
marci@155
   315
//     typedef typename Graph::NodeIt NodeIt;
marci@155
   316
//     typedef typename Graph::EdgeIt EdgeIt;
marci@155
   317
  
marci@155
   318
//     typedef typename Graph::EachNodeIt EachNodeIt;
marci@155
   319
    
marci@155
   320
//     //FIXME tag-ekkel megcsinalni, hogy abbol csinaljon
marci@155
   321
//     //iranyitatlant, ami van
marci@155
   322
//     //mert csak 1 dolgot lehet be typedef-elni
marci@155
   323
//     typedef typename Graph::OutEdgeIt SymEdgeIt;
marci@155
   324
//     //typedef typename Graph::InEdgeIt SymEdgeIt;
marci@155
   325
//     //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@155
   326
//     typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@155
   327
marci@155
   328
//     int nodeNum() const { return graph->nodeNum(); }
marci@155
   329
//     int edgeNum() const { return graph->edgeNum(); }
marci@155
   330
    
marci@155
   331
//     template<typename I> I& getFirst(I& i) const { return graph->getFirst(i); }
marci@155
   332
//     template<typename I, typename P> I& getFirst(I& i, const P& p) const { 
marci@155
   333
//       return graph->getFirst(i, p); }
marci@155
   334
//     //template<typename I> I next(const I i); { return graph->goNext(i); }
marci@155
   335
//     //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
marci@155
   336
marci@155
   337
//     template< typename It > It first() const { 
marci@155
   338
//       It e; getFirst(e); return e; }
marci@155
   339
marci@155
   340
//     template< typename It > It first(NodeIt v) const { 
marci@155
   341
//       It e; getFirst(e, v); return e; }
marci@155
   342
marci@155
   343
//     NodeIt head(const EdgeIt& e) const { return graph->head(e); }
marci@155
   344
//     NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
marci@155
   345
  
marci@155
   346
//     template<typename I> NodeIt aNode(const I& e) const { 
marci@155
   347
//       return graph->aNode(e); }
marci@155
   348
//     template<typename I> NodeIt bNode(const I& e) const { 
marci@155
   349
//       return graph->bNode(e); }
marci@155
   350
  
marci@155
   351
//     //template<typename I> bool valid(const I i);
marci@155
   352
//     //{ return graph->valid(i); }
marci@155
   353
  
marci@155
   354
//     //template<typename I> void setInvalid(const I &i);
marci@155
   355
//     //{ return graph->setInvalid(i); }
marci@155
   356
  
marci@155
   357
//     NodeIt addNode() { return graph->addNode(); }
marci@155
   358
//     EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) { 
marci@155
   359
//       return graph->addEdge(tail, head); }
marci@155
   360
  
marci@155
   361
//     template<typename I> void erase(const I& i) { graph->erase(i); }
marci@155
   362
  
marci@155
   363
//     void clear() { graph->clear(); }
marci@155
   364
  
marci@155
   365
//     template<typename T> class NodeMap : public Graph::NodeMap<T> { };
marci@155
   366
//     template<typename T> class EdgeMap : public Graph::EdgeMap<T> { };
marci@155
   367
  
marci@155
   368
//     void setGraph(Graph& _graph) { graph = &_graph; }
marci@155
   369
//     Graph& getGraph() { return (*graph); }
marci@155
   370
marci@155
   371
//     //SymGraphWrapper() : graph(0) { }
marci@155
   372
//     SymGraphWrapper(Graph& _graph) : graph(&_graph) { }
marci@155
   373
//   };
marci@155
   374
marci@155
   375
marci@155
   376
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@155
   377
  class ResGraphWrapper {
marci@76
   378
  public:
marci@76
   379
    typedef Graph BaseGraph;
marci@155
   380
    typedef typename Graph::NodeIt NodeIt;
marci@155
   381
    typedef typename Graph::EachNodeIt EachNodeIt;
marci@155
   382
  private:
marci@155
   383
    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
marci@155
   384
    typedef typename Graph::InEdgeIt OldInEdgeIt;
marci@155
   385
    const Graph* G;
marci@155
   386
    FlowMap* flow;
marci@155
   387
    const CapacityMap* capacity;
marci@155
   388
  public:
marci@155
   389
    ResGraphWrapper(const Graph& _G, FlowMap& _flow, 
marci@155
   390
	     const CapacityMap& _capacity) : 
marci@155
   391
      G(&_G), flow(&_flow), capacity(&_capacity) { }
marci@155
   392
//     ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : 
marci@155
   393
//       G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
marci@158
   394
    void setGraph(Graph& _graph) { graph = &_graph; }
marci@158
   395
    Graph& getGraph() const { return (*graph); }
marci@155
   396
    class EdgeIt; 
marci@155
   397
    class OutEdgeIt; 
marci@155
   398
    friend class EdgeIt; 
marci@155
   399
    friend class OutEdgeIt; 
marci@76
   400
marci@155
   401
    class EdgeIt {
marci@155
   402
      friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
marci@155
   403
    protected:
marci@155
   404
      //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
marci@155
   405
      const Graph* G;
marci@155
   406
      FlowMap* flow;
marci@155
   407
      const CapacityMap* capacity;
marci@155
   408
      //OldSymEdgeIt sym;
marci@155
   409
      OldOutEdgeIt out;
marci@155
   410
      OldInEdgeIt in;
marci@155
   411
      bool out_or_in; //true, iff out
marci@155
   412
    public:
marci@155
   413
      EdgeIt() : out_or_in(true) { } 
marci@155
   414
      EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@155
   415
	G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { }
marci@155
   416
      //EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { }
marci@155
   417
      Number free() const { 
marci@155
   418
	if (out_or_in) { 
marci@155
   419
	  return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
marci@155
   420
	} else { 
marci@155
   421
	  return (/*resG->*/flow->get(in)); 
marci@155
   422
	}
marci@155
   423
      }
marci@155
   424
      bool valid() const { 
marci@155
   425
	return out_or_in && out.valid() || in.valid(); }
marci@155
   426
      void augment(Number a) const {
marci@155
   427
	if (out_or_in) { 
marci@155
   428
	  /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
marci@155
   429
	} else { 
marci@155
   430
	  /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
marci@155
   431
	}
marci@155
   432
      }
marci@155
   433
      void print() { 
marci@155
   434
	if (out_or_in) {
marci@155
   435
	  std::cout << "out "; 
marci@155
   436
	  if (out.valid()) 
marci@155
   437
	    std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out)); 
marci@155
   438
	  else 
marci@155
   439
	    std::cout << "invalid"; 
marci@155
   440
	}
marci@155
   441
	else {
marci@155
   442
	  std::cout << "in "; 
marci@155
   443
	  if (in.valid()) 
marci@155
   444
	    std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in)); 
marci@155
   445
	  else 
marci@155
   446
	    std::cout << "invalid"; 
marci@155
   447
	}
marci@155
   448
	std::cout << std::endl;
marci@155
   449
      }
marci@155
   450
    };
marci@155
   451
marci@155
   452
    Number free(OldOutEdgeIt out) const { 
marci@155
   453
      return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
marci@155
   454
    }
marci@155
   455
    Number free(OldInEdgeIt in) const { 
marci@155
   456
      return (/*resG->*/flow->get(in)); 
marci@155
   457
    }
marci@155
   458
marci@155
   459
    class OutEdgeIt : public EdgeIt {
marci@155
   460
      friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
marci@155
   461
    public:
marci@155
   462
      OutEdgeIt() { }
marci@155
   463
    private:
marci@155
   464
      OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { 
marci@155
   465
	//out=/*resG->*/G->template first<OldOutEdgeIt>(v);
marci@155
   466
	G->getFirst(out, v);
marci@155
   467
	while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@155
   468
	if (!out.valid()) {
marci@155
   469
	  out_or_in=0;
marci@155
   470
	  //in=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@155
   471
	  G->getFirst(in, v);
marci@155
   472
	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   473
	}
marci@155
   474
      }
marci@155
   475
    public:
marci@155
   476
      OutEdgeIt& operator++() { 
marci@155
   477
	if (out_or_in) {
marci@155
   478
	  NodeIt v=/*resG->*/G->aNode(out);
marci@155
   479
	  ++out;
marci@155
   480
	  while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@155
   481
	  if (!out.valid()) {
marci@155
   482
	    out_or_in=0;
marci@155
   483
	    G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@155
   484
	    while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   485
	  }
marci@155
   486
	} else {
marci@155
   487
	  ++in;
marci@155
   488
	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } 
marci@155
   489
	}
marci@155
   490
	return *this; 
marci@155
   491
      }
marci@155
   492
    };
marci@155
   493
marci@155
   494
    class EachEdgeIt : public EdgeIt {
marci@155
   495
      friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
marci@155
   496
      typename Graph::EachNodeIt v;
marci@155
   497
    public:
marci@155
   498
      EachEdgeIt() { }
marci@155
   499
      //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
marci@155
   500
      EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { 
marci@155
   501
	out_or_in=true;
marci@155
   502
	G->getFirst(v);
marci@155
   503
	if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt();
marci@155
   504
	while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@155
   505
	while (v.valid() && !out.valid()) { 
marci@155
   506
	  ++v; 
marci@155
   507
	  if (v.valid()) G->getFirst(out, v); 
marci@155
   508
	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@155
   509
	}
marci@155
   510
	if (!out.valid()) {
marci@155
   511
	  out_or_in=0;
marci@155
   512
	  G->getFirst(v);
marci@155
   513
	  if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
marci@155
   514
	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   515
	  while (v.valid() && !in.valid()) { 
marci@155
   516
	    ++v; 
marci@155
   517
	    if (v.valid()) G->getFirst(in, v); 
marci@155
   518
	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   519
	  }
marci@155
   520
	}
marci@155
   521
      }
marci@155
   522
      EachEdgeIt& operator++() { 
marci@155
   523
	if (out_or_in) {
marci@155
   524
	  ++out;
marci@155
   525
	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@155
   526
	  while (v.valid() && !out.valid()) { 
marci@155
   527
	    ++v; 
marci@155
   528
	    if (v.valid()) G->getFirst(out, v); 
marci@155
   529
	    while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@155
   530
	  }
marci@155
   531
	  if (!out.valid()) {
marci@155
   532
	    out_or_in=0;
marci@155
   533
	    G->getFirst(v);
marci@155
   534
	    if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
marci@155
   535
	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   536
	    while (v.valid() && !in.valid()) { 
marci@155
   537
	      ++v; 
marci@155
   538
	      if (v.valid()) G->getFirst(in, v); 
marci@155
   539
	      while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   540
	    }  
marci@155
   541
	  }
marci@155
   542
	} else {
marci@155
   543
	  ++in;
marci@155
   544
	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   545
	  while (v.valid() && !in.valid()) { 
marci@155
   546
	    ++v; 
marci@155
   547
	    if (v.valid()) G->getFirst(in, v); 
marci@155
   548
	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@155
   549
	  }
marci@155
   550
	}
marci@155
   551
	return *this;
marci@155
   552
      }
marci@155
   553
    };
marci@155
   554
marci@155
   555
    void getFirst(EachNodeIt& v) const { G->getFirst(v); }
marci@155
   556
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@155
   557
      e=OutEdgeIt(*G, v, *flow, *capacity); 
marci@155
   558
    }
marci@155
   559
    void getFirst(EachEdgeIt& e) const { 
marci@155
   560
      e=EachEdgeIt(*G, *flow, *capacity); 
marci@155
   561
    }
marci@155
   562
   
marci@155
   563
    EachNodeIt& next(EachNodeIt& n) const { return G->next(n); }
marci@155
   564
marci@155
   565
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@155
   566
      if (e.out_or_in) {
marci@155
   567
	NodeIt v=G->aNode(e.out);
marci@155
   568
	++(e.out);
marci@155
   569
	while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
marci@155
   570
	if (!G->valid(e.out)) {
marci@155
   571
	  e.out_or_in=0;
marci@155
   572
	  G->getFirst(e.in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@155
   573
	  while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@155
   574
	}
marci@155
   575
      } else {
marci@155
   576
	++(e.in);
marci@155
   577
	while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } 
marci@155
   578
      }
marci@155
   579
      return e;
marci@155
   580
    }
marci@155
   581
marci@155
   582
    EachEdgeIt& next(EachEdgeIt& e) const { 
marci@155
   583
      if (e.out_or_in) {
marci@155
   584
	++(e.out);
marci@155
   585
	while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
marci@155
   586
	  while (G->valid(e.v) && !G->valid(e.out)) { 
marci@155
   587
	    ++(e.v); 
marci@155
   588
	    if (G->valid(e.v)) G->getFirst(e.out, e.v); 
marci@155
   589
	    while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
marci@155
   590
	  }
marci@155
   591
	  if (!G->valid(e.out)) {
marci@155
   592
	    e.out_or_in=0;
marci@155
   593
	    G->getFirst(e.v);
marci@155
   594
	    if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt();
marci@155
   595
	    while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@155
   596
	    while (G->valid(e.v) && !G->valid(e.in)) { 
marci@155
   597
	      ++(e.v); 
marci@155
   598
	      if (G->valid(e.v)) G->getFirst(e.in, e.v); 
marci@155
   599
	      while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@155
   600
	    }  
marci@155
   601
	  }
marci@155
   602
	} else {
marci@155
   603
	  ++(e.in);
marci@155
   604
	  while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@155
   605
	  while (G->valid(e.v) && !G->valid(e.in)) { 
marci@155
   606
	    ++(e.v); 
marci@155
   607
	    if (G->valid(e.v)) G->getFirst(e.in, e.v); 
marci@155
   608
	    while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@155
   609
	  }
marci@155
   610
	}
marci@155
   611
	return e;
marci@155
   612
      }
marci@76
   613
    
marci@76
   614
marci@155
   615
    template< typename It >
marci@155
   616
    It first() const { 
marci@155
   617
      It e;
marci@155
   618
      getFirst(e);
marci@155
   619
      return e; 
marci@155
   620
    }
marci@76
   621
marci@155
   622
    template< typename It >
marci@155
   623
    It first(NodeIt v) const { 
marci@155
   624
      It e;
marci@155
   625
      getFirst(e, v);
marci@155
   626
      return e; 
marci@155
   627
    }
marci@76
   628
marci@155
   629
    NodeIt tail(EdgeIt e) const { 
marci@155
   630
      return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
marci@155
   631
    NodeIt head(EdgeIt e) const { 
marci@155
   632
      return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
marci@76
   633
marci@155
   634
    NodeIt aNode(OutEdgeIt e) const { 
marci@155
   635
      return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
marci@155
   636
    NodeIt bNode(OutEdgeIt e) const { 
marci@155
   637
      return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
marci@76
   638
marci@155
   639
    int id(NodeIt v) const { return G->id(v); }
marci@155
   640
marci@155
   641
    bool valid(NodeIt n) const { return G->valid(n); }
marci@155
   642
    bool valid(EdgeIt e) const { 
marci@155
   643
      return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
marci@155
   644
marci@155
   645
    template<typename T> class NodeMap : public Graph::NodeMap<T> { 
marci@155
   646
    public:
marci@155
   647
      NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) 
marci@158
   648
	: Graph::NodeMap<T>(_G.getGraph()) { }
marci@155
   649
      NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, 
marci@158
   650
	      T a) : Graph::NodeMap<T>(_G.getGraph(), a) { }
marci@155
   651
    };
marci@155
   652
marci@155
   653
//     template <typename T>
marci@155
   654
//     class NodeMap {
marci@155
   655
//       typename Graph::NodeMap<T> node_map; 
marci@155
   656
//     public:
marci@155
   657
//       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.G)) { }
marci@155
   658
//       NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.G), a) { }
marci@155
   659
//       void set(NodeIt nit, T a) { node_map.set(nit, a); }
marci@155
   660
//       T get(NodeIt nit) const { return node_map.get(nit); }
marci@155
   661
//     };
marci@155
   662
marci@155
   663
    template <typename T>
marci@155
   664
    class EdgeMap {
marci@155
   665
      typename Graph::EdgeMap<T> forward_map, backward_map; 
marci@155
   666
    public:
marci@158
   667
      EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(_G.getGraph()), backward_map(_G.getGraph()) { }
marci@158
   668
      EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(_G.getGraph(), a), backward_map(_G.getGraph(), a) { }
marci@155
   669
      void set(EdgeIt e, T a) { 
marci@155
   670
	if (e.out_or_in) 
marci@155
   671
	  forward_map.set(e.out, a); 
marci@155
   672
	else 
marci@155
   673
	  backward_map.set(e.in, a); 
marci@155
   674
      }
marci@155
   675
      T get(EdgeIt e) { 
marci@155
   676
	if (e.out_or_in) 
marci@155
   677
	  return forward_map.get(e.out); 
marci@155
   678
	else 
marci@155
   679
	  return backward_map.get(e.in); 
marci@155
   680
      }
marci@155
   681
    };
marci@155
   682
marci@76
   683
  };
marci@76
   684
marci@76
   685
marci@148
   686
marci@148
   687
// // FIXME: comparison should be made better!!!
marci@148
   688
//   template<typename Graph, typename T, typename LowerMap, typename FlowMap, typename UpperMap>
marci@148
   689
//   class ResGraphWrapper
marci@148
   690
//   {
marci@148
   691
//     Graph* graph;
marci@76
   692
  
marci@148
   693
//   public:
marci@148
   694
//     typedef Graph BaseGraph;
marci@76
   695
marci@148
   696
//     typedef typename Graph::NodeIt NodeIt;
marci@148
   697
//     typedef typename Graph::EdgeIt EdgeIt;
marci@76
   698
  
marci@148
   699
//     typedef typename Graph::EachNodeIt EachNodeIt;
marci@76
   700
   
marci@148
   701
//     class OutEdgeIt {
marci@148
   702
//     public:
marci@148
   703
//       //Graph::NodeIt n;
marci@148
   704
//       bool out_or_in;
marci@148
   705
//       typename Graph::OutEdgeIt o;
marci@148
   706
//       typename Graph::InEdgeIt i;   
marci@148
   707
//     };
marci@148
   708
//     class InEdgeIt {
marci@148
   709
//     public:
marci@148
   710
//       //Graph::NodeIt n;
marci@148
   711
//       bool out_or_in;
marci@148
   712
//       typename Graph::OutEdgeIt o;
marci@148
   713
//       typename Graph::InEdgeIt i;   
marci@148
   714
//     };
marci@148
   715
//     typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@148
   716
//     typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@76
   717
marci@148
   718
//     int nodeNum() const { return graph->nodeNum(); }
marci@148
   719
//     int edgeNum() const { return graph->edgeNum(); }
marci@76
   720
marci@148
   721
//     NodeIt& getFirst(NodeIt& n) const { return graph->getFirst(n); }
marci@76
   722
marci@148
   723
//     // EachEdge and SymEdge  is missing!!!!
marci@148
   724
//     // EdgeIt <-> In/OutEdgeIt conversion is missing!!!!
marci@76
   725
marci@148
   726
//     //FIXME
marci@148
   727
//     OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const 
marci@148
   728
//       {
marci@148
   729
// 	e.n=n;
marci@148
   730
// 	graph->getFirst(e.o,n);
marci@148
   731
// 	while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
marci@148
   732
// 	  graph->goNext(e.o);
marci@148
   733
// 	if(!graph->valid(e.o)) {
marci@148
   734
// 	  graph->getFirst(e.i,n);
marci@148
   735
// 	  while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
marci@148
   736
// 	    graph->goNext(e.i);
marci@148
   737
// 	}
marci@148
   738
// 	return e;
marci@148
   739
//       }
marci@148
   740
// /*
marci@148
   741
//   OutEdgeIt &goNext(OutEdgeIt &e)
marci@148
   742
//   {
marci@148
   743
//   if(graph->valid(e.o)) {
marci@148
   744
//   while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o))
marci@148
   745
//   graph->goNext(e.o);
marci@148
   746
//   if(graph->valid(e.o)) return e;
marci@148
   747
//   else graph->getFirst(e.i,e.n);
marci@148
   748
//   }
marci@148
   749
//   else {
marci@148
   750
//   while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i))
marci@148
   751
//   graph->goNext(e.i);
marci@148
   752
//   return e;
marci@148
   753
//   }
marci@148
   754
//   }
marci@148
   755
//   OutEdgeIt Next(const OutEdgeIt &e) {OutEdgeIt t(e); return goNext(t);}
marci@148
   756
// */
marci@148
   757
//     //bool valid(const OutEdgeIt e) { return graph->valid(e.o)||graph->valid(e.i);}
marci@76
   758
marci@148
   759
//     //FIXME
marci@148
   760
//     InEdgeIt& getFirst(InEdgeIt& e, const NodeIt& n) const 
marci@148
   761
//       {
marci@148
   762
// 	e.n=n;
marci@148
   763
// 	graph->getFirst(e.i,n);
marci@148
   764
// 	while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
marci@148
   765
// 	  graph->goNext(e.i);
marci@148
   766
// 	if(!graph->valid(e.i)) {
marci@148
   767
// 	  graph->getFirst(e.o,n);
marci@148
   768
// 	  while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
marci@148
   769
// 	    graph->goNext(e.o);
marci@148
   770
// 	}
marci@148
   771
// 	return e;
marci@148
   772
//       }
marci@148
   773
// /*
marci@148
   774
//   InEdgeIt &goNext(InEdgeIt &e)
marci@148
   775
//   {
marci@148
   776
//   if(graph->valid(e.i)) {
marci@148
   777
//   while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i))
marci@148
   778
//   graph->goNext(e.i);
marci@148
   779
//   if(graph->valid(e.i)) return e;
marci@148
   780
//   else graph->getFirst(e.o,e.n);
marci@148
   781
//   }
marci@148
   782
//   else {
marci@148
   783
//   while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o))
marci@148
   784
//   graph->goNext(e.o);
marci@148
   785
//   return e;
marci@148
   786
//   }
marci@148
   787
//   }
marci@148
   788
//   InEdgeIt Next(const InEdgeIt &e) {InEdgeIt t(e); return goNext(t);}
marci@148
   789
// */
marci@148
   790
//     //bool valid(const InEdgeIt e) { return graph->valid(e.i)||graph->valid(e.o);}
marci@76
   791
marci@148
   792
//     //template<typename I> I &goNext(I &i); { return graph->goNext(i); }
marci@148
   793
//     //template<typename I> I next(const I i); { return graph->goNext(i); }
marci@76
   794
marci@148
   795
//     template< typename It > It first() const { 
marci@148
   796
//       It e; getFirst(e); return e; }
marci@76
   797
marci@148
   798
//     template< typename It > It first(NodeIt v) const { 
marci@148
   799
//       It e; getFirst(e, v); return e; }
marci@76
   800
marci@148
   801
//     NodeIt head(const EdgeIt& e) const { return graph->head(e); }
marci@148
   802
//     NodeIt tail(const EdgeIt& e) const { return graph->tail(e); }
marci@76
   803
  
marci@148
   804
//     template<typename I> NodeIt aNode(const I& e) const { 
marci@148
   805
//       return graph->aNode(e); }
marci@148
   806
//     template<typename I> NodeIt bNode(const I& e) const { 
marci@148
   807
//       return graph->bNode(e); }
marci@76
   808
  
marci@148
   809
//     //template<typename I> bool valid(const I i);
marci@148
   810
//     //{ return graph->valid(i); }
marci@76
   811
  
marci@148
   812
//     //template<typename I> void setInvalid(const I &i);
marci@148
   813
//     //{ return graph->setInvalid(i); }
marci@76
   814
  
marci@148
   815
//     NodeIt addNode() { return graph->addNode(); }
marci@148
   816
//     EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) { 
marci@148
   817
//       return graph->addEdge(tail, head); }
marci@76
   818
  
marci@148
   819
//     template<typename I> void erase(const I& i) { graph->erase(i); }
marci@76
   820
  
marci@148
   821
//     void clear() { graph->clear(); }
marci@76
   822
  
marci@148
   823
//     template<typename S> class NodeMap : public Graph::NodeMap<S> { };
marci@148
   824
//     template<typename S> class EdgeMap : public Graph::EdgeMap<S> { };
marci@76
   825
  
marci@148
   826
//     void setGraph(Graph& _graph) { graph = &_graph; }
marci@148
   827
//     Graph& getGraph() { return (*graph); }
marci@76
   828
marci@148
   829
//     //ResGraphWrapper() : graph(0) { }
marci@148
   830
//     ResGraphWrapper(Graph& _graph) : graph(&_graph) { }
marci@148
   831
//   };
marci@76
   832
alpar@105
   833
} //namespace hugo
marci@76
   834
marci@76
   835
#endif //GRAPH_WRAPPER_H
marci@76
   836