lemon/bits/graph_adaptor_extender.h
author klao
Fri, 03 Mar 2006 21:49:39 +0000
changeset 1997 b7a70cdb5520
parent 1979 c2992fd74dad
child 2031 080d51024ac5
permissions -rw-r--r--
Bugfix: an ugly artefact of the 'id' -> 'label' renaming
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@1996
    19
#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
deba@1996
    20
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
deba@1979
    21
deba@1996
    22
#include <lemon/bits/invalid.h>
deba@1996
    23
#include <lemon/error.h>
deba@1979
    24
deba@1996
    25
#include <lemon/bits/default_map.h>
deba@1996
    26
deba@1996
    27
deba@1996
    28
///\ingroup graphbits
deba@1996
    29
///\file
deba@1996
    30
///\brief Extenders for the graph adaptor types
deba@1979
    31
namespace lemon {
deba@1979
    32
deba@1996
    33
  /// \ingroup graphbits
deba@1996
    34
  ///
deba@1996
    35
  /// \brief Extender for the GraphAdaptors
deba@1979
    36
  template <typename Base>
deba@1979
    37
  class GraphAdaptorExtender : public Base {
deba@1979
    38
  public:
deba@1979
    39
deba@1979
    40
    typedef Base Parent;
deba@1979
    41
    typedef GraphAdaptorExtender Graph;
deba@1979
    42
deba@1979
    43
    // Base extensions
deba@1979
    44
deba@1979
    45
    typedef typename Parent::Node Node;
deba@1979
    46
    typedef typename Parent::Edge Edge;
deba@1979
    47
deba@1979
    48
    int maxId(Node) const {
deba@1979
    49
      return Parent::maxNodeId();
deba@1979
    50
    }
deba@1979
    51
deba@1979
    52
    int maxId(Edge) const {
deba@1979
    53
      return Parent::maxEdgeId();
deba@1979
    54
    }
deba@1979
    55
deba@1979
    56
    Node fromId(int id, Node) const {
deba@1979
    57
      return Parent::nodeFromId(id);
deba@1979
    58
    }
deba@1979
    59
deba@1979
    60
    Edge fromId(int id, Edge) const {
deba@1979
    61
      return Parent::edgeFromId(id);
deba@1979
    62
    }
deba@1979
    63
deba@1979
    64
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@1979
    65
      if (n == Parent::source(e))
deba@1979
    66
	return Parent::target(e);
deba@1979
    67
      else if(n==Parent::target(e))
deba@1979
    68
	return Parent::source(e);
deba@1979
    69
      else
deba@1979
    70
	return INVALID;
deba@1979
    71
    }
deba@1979
    72
deba@1979
    73
    class NodeIt : public Node { 
deba@1979
    74
      const Graph* graph;
deba@1979
    75
    public:
deba@1979
    76
deba@1979
    77
      NodeIt() {}
deba@1979
    78
deba@1979
    79
      NodeIt(Invalid i) : Node(i) { }
deba@1979
    80
deba@1979
    81
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
    82
	_graph.first(*static_cast<Node*>(this));
deba@1979
    83
      }
deba@1979
    84
deba@1979
    85
      NodeIt(const Graph& _graph, const Node& node) 
deba@1979
    86
	: Node(node), graph(&_graph) {}
deba@1979
    87
deba@1979
    88
      NodeIt& operator++() { 
deba@1979
    89
	graph->next(*this);
deba@1979
    90
	return *this; 
deba@1979
    91
      }
deba@1979
    92
deba@1979
    93
    };
deba@1979
    94
deba@1979
    95
deba@1979
    96
    class EdgeIt : public Edge { 
deba@1979
    97
      const Graph* graph;
deba@1979
    98
    public:
deba@1979
    99
deba@1979
   100
      EdgeIt() { }
deba@1979
   101
deba@1979
   102
      EdgeIt(Invalid i) : Edge(i) { }
deba@1979
   103
deba@1979
   104
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   105
	_graph.first(*static_cast<Edge*>(this));
deba@1979
   106
      }
deba@1979
   107
deba@1979
   108
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@1979
   109
	Edge(e), graph(&_graph) { }
deba@1979
   110
deba@1979
   111
      EdgeIt& operator++() { 
deba@1979
   112
	graph->next(*this);
deba@1979
   113
	return *this; 
deba@1979
   114
      }
deba@1979
   115
deba@1979
   116
    };
deba@1979
   117
deba@1979
   118
deba@1979
   119
    class OutEdgeIt : public Edge { 
deba@1979
   120
      const Graph* graph;
deba@1979
   121
    public:
deba@1979
   122
deba@1979
   123
      OutEdgeIt() { }
deba@1979
   124
deba@1979
   125
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   126
deba@1979
   127
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   128
	: graph(&_graph) {
deba@1979
   129
	_graph.firstOut(*this, node);
deba@1979
   130
      }
deba@1979
   131
deba@1979
   132
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@1979
   133
	: Edge(edge), graph(&_graph) {}
deba@1979
   134
deba@1979
   135
      OutEdgeIt& operator++() { 
deba@1979
   136
	graph->nextOut(*this);
deba@1979
   137
	return *this; 
deba@1979
   138
      }
deba@1979
   139
deba@1979
   140
    };
deba@1979
   141
deba@1979
   142
deba@1979
   143
    class InEdgeIt : public Edge { 
deba@1979
   144
      const Graph* graph;
deba@1979
   145
    public:
deba@1979
   146
deba@1979
   147
      InEdgeIt() { }
deba@1979
   148
deba@1979
   149
      InEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   150
deba@1979
   151
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   152
	: graph(&_graph) {
deba@1979
   153
	_graph.firstIn(*this, node);
deba@1979
   154
      }
deba@1979
   155
deba@1979
   156
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@1979
   157
	Edge(edge), graph(&_graph) {}
deba@1979
   158
deba@1979
   159
      InEdgeIt& operator++() { 
deba@1979
   160
	graph->nextIn(*this);
deba@1979
   161
	return *this; 
deba@1979
   162
      }
deba@1979
   163
deba@1979
   164
    };
deba@1979
   165
deba@1979
   166
    /// \brief Base node of the iterator
deba@1979
   167
    ///
deba@1979
   168
    /// Returns the base node (ie. the source in this case) of the iterator
deba@1979
   169
    Node baseNode(const OutEdgeIt &e) const {
deba@1979
   170
      return Parent::source(e);
deba@1979
   171
    }
deba@1979
   172
    /// \brief Running node of the iterator
deba@1979
   173
    ///
deba@1979
   174
    /// Returns the running node (ie. the target in this case) of the
deba@1979
   175
    /// iterator
deba@1979
   176
    Node runningNode(const OutEdgeIt &e) const {
deba@1979
   177
      return Parent::target(e);
deba@1979
   178
    }
deba@1979
   179
deba@1979
   180
    /// \brief Base node of the iterator
deba@1979
   181
    ///
deba@1979
   182
    /// Returns the base node (ie. the target in this case) of the iterator
deba@1979
   183
    Node baseNode(const InEdgeIt &e) const {
deba@1979
   184
      return Parent::target(e);
deba@1979
   185
    }
deba@1979
   186
    /// \brief Running node of the iterator
deba@1979
   187
    ///
deba@1979
   188
    /// Returns the running node (ie. the source in this case) of the
deba@1979
   189
    /// iterator
deba@1979
   190
    Node runningNode(const InEdgeIt &e) const {
deba@1979
   191
      return Parent::source(e);
deba@1979
   192
    }
deba@1979
   193
deba@1979
   194
  };
deba@1979
   195
deba@1979
   196
deba@1996
   197
  /// \ingroup graphbits
deba@1996
   198
  ///
deba@1996
   199
  /// \brief Extender for the UGraphAdaptors
deba@1979
   200
  template <typename Base> 
deba@1979
   201
  class UGraphAdaptorExtender : public Base {
deba@1979
   202
  public:
deba@1979
   203
    
deba@1979
   204
    typedef Base Parent;
deba@1979
   205
    typedef UGraphAdaptorExtender Graph;
deba@1979
   206
deba@1979
   207
    typedef typename Parent::Node Node;
deba@1979
   208
    typedef typename Parent::Edge Edge;
deba@1979
   209
    typedef typename Parent::UEdge UEdge;
deba@1979
   210
deba@1979
   211
    // UGraph extension    
deba@1979
   212
deba@1979
   213
    int maxId(Node) const {
deba@1979
   214
      return Parent::maxNodeId();
deba@1979
   215
    }
deba@1979
   216
deba@1979
   217
    int maxId(Edge) const {
deba@1979
   218
      return Parent::maxEdgeId();
deba@1979
   219
    }
deba@1979
   220
deba@1979
   221
    int maxId(UEdge) const {
deba@1979
   222
      return Parent::maxUEdgeId();
deba@1979
   223
    }
deba@1979
   224
deba@1979
   225
    Node fromId(int id, Node) const {
deba@1979
   226
      return Parent::nodeFromId(id);
deba@1979
   227
    }
deba@1979
   228
deba@1979
   229
    Edge fromId(int id, Edge) const {
deba@1979
   230
      return Parent::edgeFromId(id);
deba@1979
   231
    }
deba@1979
   232
deba@1979
   233
    UEdge fromId(int id, UEdge) const {
deba@1979
   234
      return Parent::uEdgeFromId(id);
deba@1979
   235
    }
deba@1979
   236
deba@1979
   237
    Node oppositeNode(const Node &n, const UEdge &e) const {
deba@1979
   238
      if( n == Parent::source(e))
deba@1979
   239
	return Parent::target(e);
deba@1979
   240
      else if( n == Parent::target(e))
deba@1979
   241
	return Parent::source(e);
deba@1979
   242
      else
deba@1979
   243
	return INVALID;
deba@1979
   244
    }
deba@1979
   245
deba@1979
   246
    Edge oppositeEdge(const Edge &e) const {
deba@1979
   247
      return Parent::direct(e, !Parent::direction(e));
deba@1979
   248
    }
deba@1979
   249
deba@1979
   250
    using Parent::direct;
deba@1979
   251
    Edge direct(const UEdge &ue, const Node &s) const {
deba@1979
   252
      return Parent::direct(ue, Parent::source(ue) == s);
deba@1979
   253
    }
deba@1979
   254
deba@1979
   255
deba@1979
   256
    class NodeIt : public Node { 
deba@1979
   257
      const Graph* graph;
deba@1979
   258
    public:
deba@1979
   259
deba@1979
   260
      NodeIt() {}
deba@1979
   261
deba@1979
   262
      NodeIt(Invalid i) : Node(i) { }
deba@1979
   263
deba@1979
   264
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   265
	_graph.first(*static_cast<Node*>(this));
deba@1979
   266
      }
deba@1979
   267
deba@1979
   268
      NodeIt(const Graph& _graph, const Node& node) 
deba@1979
   269
	: Node(node), graph(&_graph) {}
deba@1979
   270
deba@1979
   271
      NodeIt& operator++() { 
deba@1979
   272
	graph->next(*this);
deba@1979
   273
	return *this; 
deba@1979
   274
      }
deba@1979
   275
deba@1979
   276
    };
deba@1979
   277
deba@1979
   278
deba@1979
   279
    class EdgeIt : public Edge { 
deba@1979
   280
      const Graph* graph;
deba@1979
   281
    public:
deba@1979
   282
deba@1979
   283
      EdgeIt() { }
deba@1979
   284
deba@1979
   285
      EdgeIt(Invalid i) : Edge(i) { }
deba@1979
   286
deba@1979
   287
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   288
	_graph.first(*static_cast<Edge*>(this));
deba@1979
   289
      }
deba@1979
   290
deba@1979
   291
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@1979
   292
	Edge(e), graph(&_graph) { }
deba@1979
   293
deba@1979
   294
      EdgeIt& operator++() { 
deba@1979
   295
	graph->next(*this);
deba@1979
   296
	return *this; 
deba@1979
   297
      }
deba@1979
   298
deba@1979
   299
    };
deba@1979
   300
deba@1979
   301
deba@1979
   302
    class OutEdgeIt : public Edge { 
deba@1979
   303
      const Graph* graph;
deba@1979
   304
    public:
deba@1979
   305
deba@1979
   306
      OutEdgeIt() { }
deba@1979
   307
deba@1979
   308
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   309
deba@1979
   310
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   311
	: graph(&_graph) {
deba@1979
   312
	_graph.firstOut(*this, node);
deba@1979
   313
      }
deba@1979
   314
deba@1979
   315
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@1979
   316
	: Edge(edge), graph(&_graph) {}
deba@1979
   317
deba@1979
   318
      OutEdgeIt& operator++() { 
deba@1979
   319
	graph->nextOut(*this);
deba@1979
   320
	return *this; 
deba@1979
   321
      }
deba@1979
   322
deba@1979
   323
    };
deba@1979
   324
deba@1979
   325
deba@1979
   326
    class InEdgeIt : public Edge { 
deba@1979
   327
      const Graph* graph;
deba@1979
   328
    public:
deba@1979
   329
deba@1979
   330
      InEdgeIt() { }
deba@1979
   331
deba@1979
   332
      InEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   333
deba@1979
   334
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   335
	: graph(&_graph) {
deba@1979
   336
	_graph.firstIn(*this, node);
deba@1979
   337
      }
deba@1979
   338
deba@1979
   339
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@1979
   340
	Edge(edge), graph(&_graph) {}
deba@1979
   341
deba@1979
   342
      InEdgeIt& operator++() { 
deba@1979
   343
	graph->nextIn(*this);
deba@1979
   344
	return *this; 
deba@1979
   345
      }
deba@1979
   346
deba@1979
   347
    };
deba@1979
   348
deba@1979
   349
    class UEdgeIt : public Parent::UEdge { 
deba@1979
   350
      const Graph* graph;
deba@1979
   351
    public:
deba@1979
   352
deba@1979
   353
      UEdgeIt() { }
deba@1979
   354
deba@1979
   355
      UEdgeIt(Invalid i) : UEdge(i) { }
deba@1979
   356
deba@1979
   357
      explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
deba@1979
   358
	_graph.first(*static_cast<UEdge*>(this));
deba@1979
   359
      }
deba@1979
   360
deba@1979
   361
      UEdgeIt(const Graph& _graph, const UEdge& e) : 
deba@1979
   362
	UEdge(e), graph(&_graph) { }
deba@1979
   363
deba@1979
   364
      UEdgeIt& operator++() { 
deba@1979
   365
	graph->next(*this);
deba@1979
   366
	return *this; 
deba@1979
   367
      }
deba@1979
   368
deba@1979
   369
    };
deba@1979
   370
deba@1979
   371
    class IncEdgeIt : public Parent::UEdge { 
deba@1979
   372
      friend class UGraphAdaptorExtender;
deba@1979
   373
      const Graph* graph;
deba@1979
   374
      bool direction;
deba@1979
   375
    public:
deba@1979
   376
deba@1979
   377
      IncEdgeIt() { }
deba@1979
   378
deba@1979
   379
      IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
deba@1979
   380
deba@1979
   381
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@1979
   382
	_graph.firstInc(static_cast<UEdge&>(*this), direction, n);
deba@1979
   383
      }
deba@1979
   384
deba@1979
   385
      IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
deba@1979
   386
	: graph(&_graph), UEdge(ue) {
deba@1979
   387
	direction = (_graph.source(ue) == n);
deba@1979
   388
      }
deba@1979
   389
deba@1979
   390
      IncEdgeIt& operator++() {
deba@1979
   391
	graph->nextInc(*this, direction);
deba@1979
   392
	return *this; 
deba@1979
   393
      }
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 source in this case) of the iterator
deba@1979
   399
    Node baseNode(const OutEdgeIt &e) const {
deba@1979
   400
      return Parent::source((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 target in this case) of the
deba@1979
   405
    /// iterator
deba@1979
   406
    Node runningNode(const OutEdgeIt &e) const {
deba@1979
   407
      return Parent::target((Edge)e);
deba@1979
   408
    }
deba@1979
   409
deba@1979
   410
    /// \brief Base node of the iterator
deba@1979
   411
    ///
deba@1979
   412
    /// Returns the base node (ie. the target in this case) of the iterator
deba@1979
   413
    Node baseNode(const InEdgeIt &e) const {
deba@1979
   414
      return Parent::target((Edge)e);
deba@1979
   415
    }
deba@1979
   416
    /// \brief Running node of the iterator
deba@1979
   417
    ///
deba@1979
   418
    /// Returns the running node (ie. the source in this case) of the
deba@1979
   419
    /// iterator
deba@1979
   420
    Node runningNode(const InEdgeIt &e) const {
deba@1979
   421
      return Parent::source((Edge)e);
deba@1979
   422
    }
deba@1979
   423
deba@1979
   424
    /// Base node of the iterator
deba@1979
   425
    ///
deba@1979
   426
    /// Returns the base node of the iterator
deba@1979
   427
    Node baseNode(const IncEdgeIt &e) const {
deba@1979
   428
      return e.direction ? source(e) : target(e);
deba@1979
   429
    }
deba@1979
   430
    /// Running node of the iterator
deba@1979
   431
    ///
deba@1979
   432
    /// Returns the running node of the iterator
deba@1979
   433
    Node runningNode(const IncEdgeIt &e) const {
deba@1979
   434
      return e.direction ? target(e) : source(e);
deba@1979
   435
    }
deba@1979
   436
deba@1979
   437
  };
deba@1979
   438
deba@1979
   439
deba@1979
   440
}
deba@1979
   441
deba@1979
   442
deba@1979
   443
#endif