lemon/bits/graph_adaptor_extender.h
author deba
Fri, 24 Feb 2006 11:13:09 +0000
changeset 1984 d4cbd10e1256
child 1996 5dc13b93f8b4
permissions -rw-r--r--
Bug fix
deba@1979
     1
/* -*- C++ -*-
deba@1979
     2
 *
deba@1979
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@1979
     4
 *
deba@1979
     5
 * Copyright (C) 2003-2006
deba@1979
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1979
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1979
     8
 *
deba@1979
     9
 * Permission to use, modify and distribute this software is granted
deba@1979
    10
 * provided that this copyright notice appears in all copies. For
deba@1979
    11
 * precise terms see the accompanying LICENSE file.
deba@1979
    12
 *
deba@1979
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1979
    14
 * express or implied, and with no claim as to its suitability for any
deba@1979
    15
 * purpose.
deba@1979
    16
 *
deba@1979
    17
 */
deba@1979
    18
deba@1979
    19
#ifndef LEMON_GRAPH_ADAPTOR_EXTENDER_H
deba@1979
    20
#define LEMON_GRAPH_ADAPTOR_EXTENDER_H
deba@1979
    21
deba@1979
    22
deba@1979
    23
namespace lemon {
deba@1979
    24
deba@1979
    25
  template <typename Base>
deba@1979
    26
  class GraphAdaptorExtender : public Base {
deba@1979
    27
  public:
deba@1979
    28
deba@1979
    29
    typedef Base Parent;
deba@1979
    30
    typedef GraphAdaptorExtender Graph;
deba@1979
    31
deba@1979
    32
    // Base extensions
deba@1979
    33
deba@1979
    34
    typedef typename Parent::Node Node;
deba@1979
    35
    typedef typename Parent::Edge Edge;
deba@1979
    36
deba@1979
    37
    int maxId(Node) const {
deba@1979
    38
      return Parent::maxNodeId();
deba@1979
    39
    }
deba@1979
    40
deba@1979
    41
    int maxId(Edge) const {
deba@1979
    42
      return Parent::maxEdgeId();
deba@1979
    43
    }
deba@1979
    44
deba@1979
    45
    Node fromId(int id, Node) const {
deba@1979
    46
      return Parent::nodeFromId(id);
deba@1979
    47
    }
deba@1979
    48
deba@1979
    49
    Edge fromId(int id, Edge) const {
deba@1979
    50
      return Parent::edgeFromId(id);
deba@1979
    51
    }
deba@1979
    52
deba@1979
    53
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@1979
    54
      if (n == Parent::source(e))
deba@1979
    55
	return Parent::target(e);
deba@1979
    56
      else if(n==Parent::target(e))
deba@1979
    57
	return Parent::source(e);
deba@1979
    58
      else
deba@1979
    59
	return INVALID;
deba@1979
    60
    }
deba@1979
    61
deba@1979
    62
    class NodeIt : public Node { 
deba@1979
    63
      const Graph* graph;
deba@1979
    64
    public:
deba@1979
    65
deba@1979
    66
      NodeIt() {}
deba@1979
    67
deba@1979
    68
      NodeIt(Invalid i) : Node(i) { }
deba@1979
    69
deba@1979
    70
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
    71
	_graph.first(*static_cast<Node*>(this));
deba@1979
    72
      }
deba@1979
    73
deba@1979
    74
      NodeIt(const Graph& _graph, const Node& node) 
deba@1979
    75
	: Node(node), graph(&_graph) {}
deba@1979
    76
deba@1979
    77
      NodeIt& operator++() { 
deba@1979
    78
	graph->next(*this);
deba@1979
    79
	return *this; 
deba@1979
    80
      }
deba@1979
    81
deba@1979
    82
    };
deba@1979
    83
deba@1979
    84
deba@1979
    85
    class EdgeIt : public Edge { 
deba@1979
    86
      const Graph* graph;
deba@1979
    87
    public:
deba@1979
    88
deba@1979
    89
      EdgeIt() { }
deba@1979
    90
deba@1979
    91
      EdgeIt(Invalid i) : Edge(i) { }
deba@1979
    92
deba@1979
    93
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
    94
	_graph.first(*static_cast<Edge*>(this));
deba@1979
    95
      }
deba@1979
    96
deba@1979
    97
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@1979
    98
	Edge(e), graph(&_graph) { }
deba@1979
    99
deba@1979
   100
      EdgeIt& operator++() { 
deba@1979
   101
	graph->next(*this);
deba@1979
   102
	return *this; 
deba@1979
   103
      }
deba@1979
   104
deba@1979
   105
    };
deba@1979
   106
deba@1979
   107
deba@1979
   108
    class OutEdgeIt : public Edge { 
deba@1979
   109
      const Graph* graph;
deba@1979
   110
    public:
deba@1979
   111
deba@1979
   112
      OutEdgeIt() { }
deba@1979
   113
deba@1979
   114
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   115
deba@1979
   116
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   117
	: graph(&_graph) {
deba@1979
   118
	_graph.firstOut(*this, node);
deba@1979
   119
      }
deba@1979
   120
deba@1979
   121
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@1979
   122
	: Edge(edge), graph(&_graph) {}
deba@1979
   123
deba@1979
   124
      OutEdgeIt& operator++() { 
deba@1979
   125
	graph->nextOut(*this);
deba@1979
   126
	return *this; 
deba@1979
   127
      }
deba@1979
   128
deba@1979
   129
    };
deba@1979
   130
deba@1979
   131
deba@1979
   132
    class InEdgeIt : public Edge { 
deba@1979
   133
      const Graph* graph;
deba@1979
   134
    public:
deba@1979
   135
deba@1979
   136
      InEdgeIt() { }
deba@1979
   137
deba@1979
   138
      InEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   139
deba@1979
   140
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   141
	: graph(&_graph) {
deba@1979
   142
	_graph.firstIn(*this, node);
deba@1979
   143
      }
deba@1979
   144
deba@1979
   145
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@1979
   146
	Edge(edge), graph(&_graph) {}
deba@1979
   147
deba@1979
   148
      InEdgeIt& operator++() { 
deba@1979
   149
	graph->nextIn(*this);
deba@1979
   150
	return *this; 
deba@1979
   151
      }
deba@1979
   152
deba@1979
   153
    };
deba@1979
   154
deba@1979
   155
    /// \brief Base node of the iterator
deba@1979
   156
    ///
deba@1979
   157
    /// Returns the base node (ie. the source in this case) of the iterator
deba@1979
   158
    Node baseNode(const OutEdgeIt &e) const {
deba@1979
   159
      return Parent::source(e);
deba@1979
   160
    }
deba@1979
   161
    /// \brief Running node of the iterator
deba@1979
   162
    ///
deba@1979
   163
    /// Returns the running node (ie. the target in this case) of the
deba@1979
   164
    /// iterator
deba@1979
   165
    Node runningNode(const OutEdgeIt &e) const {
deba@1979
   166
      return Parent::target(e);
deba@1979
   167
    }
deba@1979
   168
deba@1979
   169
    /// \brief Base node of the iterator
deba@1979
   170
    ///
deba@1979
   171
    /// Returns the base node (ie. the target in this case) of the iterator
deba@1979
   172
    Node baseNode(const InEdgeIt &e) const {
deba@1979
   173
      return Parent::target(e);
deba@1979
   174
    }
deba@1979
   175
    /// \brief Running node of the iterator
deba@1979
   176
    ///
deba@1979
   177
    /// Returns the running node (ie. the source in this case) of the
deba@1979
   178
    /// iterator
deba@1979
   179
    Node runningNode(const InEdgeIt &e) const {
deba@1979
   180
      return Parent::source(e);
deba@1979
   181
    }
deba@1979
   182
deba@1979
   183
  };
deba@1979
   184
deba@1979
   185
deba@1979
   186
  template <typename Base> 
deba@1979
   187
  class UGraphAdaptorExtender : public Base {
deba@1979
   188
  public:
deba@1979
   189
    
deba@1979
   190
    typedef Base Parent;
deba@1979
   191
    typedef UGraphAdaptorExtender Graph;
deba@1979
   192
deba@1979
   193
    typedef typename Parent::Node Node;
deba@1979
   194
    typedef typename Parent::Edge Edge;
deba@1979
   195
    typedef typename Parent::UEdge UEdge;
deba@1979
   196
deba@1979
   197
    // UGraph extension    
deba@1979
   198
deba@1979
   199
    int maxId(Node) const {
deba@1979
   200
      return Parent::maxNodeId();
deba@1979
   201
    }
deba@1979
   202
deba@1979
   203
    int maxId(Edge) const {
deba@1979
   204
      return Parent::maxEdgeId();
deba@1979
   205
    }
deba@1979
   206
deba@1979
   207
    int maxId(UEdge) const {
deba@1979
   208
      return Parent::maxUEdgeId();
deba@1979
   209
    }
deba@1979
   210
deba@1979
   211
    Node fromId(int id, Node) const {
deba@1979
   212
      return Parent::nodeFromId(id);
deba@1979
   213
    }
deba@1979
   214
deba@1979
   215
    Edge fromId(int id, Edge) const {
deba@1979
   216
      return Parent::edgeFromId(id);
deba@1979
   217
    }
deba@1979
   218
deba@1979
   219
    UEdge fromId(int id, UEdge) const {
deba@1979
   220
      return Parent::uEdgeFromId(id);
deba@1979
   221
    }
deba@1979
   222
deba@1979
   223
    Node oppositeNode(const Node &n, const UEdge &e) const {
deba@1979
   224
      if( n == Parent::source(e))
deba@1979
   225
	return Parent::target(e);
deba@1979
   226
      else if( n == Parent::target(e))
deba@1979
   227
	return Parent::source(e);
deba@1979
   228
      else
deba@1979
   229
	return INVALID;
deba@1979
   230
    }
deba@1979
   231
deba@1979
   232
    Edge oppositeEdge(const Edge &e) const {
deba@1979
   233
      return Parent::direct(e, !Parent::direction(e));
deba@1979
   234
    }
deba@1979
   235
deba@1979
   236
    using Parent::direct;
deba@1979
   237
    Edge direct(const UEdge &ue, const Node &s) const {
deba@1979
   238
      return Parent::direct(ue, Parent::source(ue) == s);
deba@1979
   239
    }
deba@1979
   240
deba@1979
   241
deba@1979
   242
    class NodeIt : public Node { 
deba@1979
   243
      const Graph* graph;
deba@1979
   244
    public:
deba@1979
   245
deba@1979
   246
      NodeIt() {}
deba@1979
   247
deba@1979
   248
      NodeIt(Invalid i) : Node(i) { }
deba@1979
   249
deba@1979
   250
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   251
	_graph.first(*static_cast<Node*>(this));
deba@1979
   252
      }
deba@1979
   253
deba@1979
   254
      NodeIt(const Graph& _graph, const Node& node) 
deba@1979
   255
	: Node(node), graph(&_graph) {}
deba@1979
   256
deba@1979
   257
      NodeIt& operator++() { 
deba@1979
   258
	graph->next(*this);
deba@1979
   259
	return *this; 
deba@1979
   260
      }
deba@1979
   261
deba@1979
   262
    };
deba@1979
   263
deba@1979
   264
deba@1979
   265
    class EdgeIt : public Edge { 
deba@1979
   266
      const Graph* graph;
deba@1979
   267
    public:
deba@1979
   268
deba@1979
   269
      EdgeIt() { }
deba@1979
   270
deba@1979
   271
      EdgeIt(Invalid i) : Edge(i) { }
deba@1979
   272
deba@1979
   273
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   274
	_graph.first(*static_cast<Edge*>(this));
deba@1979
   275
      }
deba@1979
   276
deba@1979
   277
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@1979
   278
	Edge(e), graph(&_graph) { }
deba@1979
   279
deba@1979
   280
      EdgeIt& operator++() { 
deba@1979
   281
	graph->next(*this);
deba@1979
   282
	return *this; 
deba@1979
   283
      }
deba@1979
   284
deba@1979
   285
    };
deba@1979
   286
deba@1979
   287
deba@1979
   288
    class OutEdgeIt : public Edge { 
deba@1979
   289
      const Graph* graph;
deba@1979
   290
    public:
deba@1979
   291
deba@1979
   292
      OutEdgeIt() { }
deba@1979
   293
deba@1979
   294
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   295
deba@1979
   296
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   297
	: graph(&_graph) {
deba@1979
   298
	_graph.firstOut(*this, node);
deba@1979
   299
      }
deba@1979
   300
deba@1979
   301
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@1979
   302
	: Edge(edge), graph(&_graph) {}
deba@1979
   303
deba@1979
   304
      OutEdgeIt& operator++() { 
deba@1979
   305
	graph->nextOut(*this);
deba@1979
   306
	return *this; 
deba@1979
   307
      }
deba@1979
   308
deba@1979
   309
    };
deba@1979
   310
deba@1979
   311
deba@1979
   312
    class InEdgeIt : public Edge { 
deba@1979
   313
      const Graph* graph;
deba@1979
   314
    public:
deba@1979
   315
deba@1979
   316
      InEdgeIt() { }
deba@1979
   317
deba@1979
   318
      InEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   319
deba@1979
   320
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   321
	: graph(&_graph) {
deba@1979
   322
	_graph.firstIn(*this, node);
deba@1979
   323
      }
deba@1979
   324
deba@1979
   325
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@1979
   326
	Edge(edge), graph(&_graph) {}
deba@1979
   327
deba@1979
   328
      InEdgeIt& operator++() { 
deba@1979
   329
	graph->nextIn(*this);
deba@1979
   330
	return *this; 
deba@1979
   331
      }
deba@1979
   332
deba@1979
   333
    };
deba@1979
   334
deba@1979
   335
    class UEdgeIt : public Parent::UEdge { 
deba@1979
   336
      const Graph* graph;
deba@1979
   337
    public:
deba@1979
   338
deba@1979
   339
      UEdgeIt() { }
deba@1979
   340
deba@1979
   341
      UEdgeIt(Invalid i) : UEdge(i) { }
deba@1979
   342
deba@1979
   343
      explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   344
	_graph.first(*static_cast<UEdge*>(this));
deba@1979
   345
      }
deba@1979
   346
deba@1979
   347
      UEdgeIt(const Graph& _graph, const UEdge& e) : 
deba@1979
   348
	UEdge(e), graph(&_graph) { }
deba@1979
   349
deba@1979
   350
      UEdgeIt& operator++() { 
deba@1979
   351
	graph->next(*this);
deba@1979
   352
	return *this; 
deba@1979
   353
      }
deba@1979
   354
deba@1979
   355
    };
deba@1979
   356
deba@1979
   357
    class IncEdgeIt : public Parent::UEdge { 
deba@1979
   358
      friend class UGraphAdaptorExtender;
deba@1979
   359
      const Graph* graph;
deba@1979
   360
      bool direction;
deba@1979
   361
    public:
deba@1979
   362
deba@1979
   363
      IncEdgeIt() { }
deba@1979
   364
deba@1979
   365
      IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
deba@1979
   366
deba@1979
   367
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@1979
   368
	_graph.firstInc(static_cast<UEdge&>(*this), direction, n);
deba@1979
   369
      }
deba@1979
   370
deba@1979
   371
      IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
deba@1979
   372
	: graph(&_graph), UEdge(ue) {
deba@1979
   373
	direction = (_graph.source(ue) == n);
deba@1979
   374
      }
deba@1979
   375
deba@1979
   376
      IncEdgeIt& operator++() {
deba@1979
   377
	graph->nextInc(*this, direction);
deba@1979
   378
	return *this; 
deba@1979
   379
      }
deba@1979
   380
    };
deba@1979
   381
deba@1979
   382
    /// \brief Base node of the iterator
deba@1979
   383
    ///
deba@1979
   384
    /// Returns the base node (ie. the source in this case) of the iterator
deba@1979
   385
    Node baseNode(const OutEdgeIt &e) const {
deba@1979
   386
      return Parent::source((Edge)e);
deba@1979
   387
    }
deba@1979
   388
    /// \brief Running node of the iterator
deba@1979
   389
    ///
deba@1979
   390
    /// Returns the running node (ie. the target in this case) of the
deba@1979
   391
    /// iterator
deba@1979
   392
    Node runningNode(const OutEdgeIt &e) const {
deba@1979
   393
      return Parent::target((Edge)e);
deba@1979
   394
    }
deba@1979
   395
deba@1979
   396
    /// \brief Base node of the iterator
deba@1979
   397
    ///
deba@1979
   398
    /// Returns the base node (ie. the target in this case) of the iterator
deba@1979
   399
    Node baseNode(const InEdgeIt &e) const {
deba@1979
   400
      return Parent::target((Edge)e);
deba@1979
   401
    }
deba@1979
   402
    /// \brief Running node of the iterator
deba@1979
   403
    ///
deba@1979
   404
    /// Returns the running node (ie. the source in this case) of the
deba@1979
   405
    /// iterator
deba@1979
   406
    Node runningNode(const InEdgeIt &e) const {
deba@1979
   407
      return Parent::source((Edge)e);
deba@1979
   408
    }
deba@1979
   409
deba@1979
   410
    /// Base node of the iterator
deba@1979
   411
    ///
deba@1979
   412
    /// Returns the base node of the iterator
deba@1979
   413
    Node baseNode(const IncEdgeIt &e) const {
deba@1979
   414
      return e.direction ? source(e) : target(e);
deba@1979
   415
    }
deba@1979
   416
    /// Running node of the iterator
deba@1979
   417
    ///
deba@1979
   418
    /// Returns the running node of the iterator
deba@1979
   419
    Node runningNode(const IncEdgeIt &e) const {
deba@1979
   420
      return e.direction ? target(e) : source(e);
deba@1979
   421
    }
deba@1979
   422
deba@1979
   423
  };
deba@1979
   424
deba@1979
   425
deba@1979
   426
}
deba@1979
   427
deba@1979
   428
deba@1979
   429
#endif