lemon/bits/graph_extender.h
author deba
Fri, 27 Jan 2006 08:18:47 +0000
changeset 1913 49fe71fce7fb
parent 1909 2d806130e700
child 1956 a055123339d5
permissions -rw-r--r--
Making iterable bool map dynamic
Changed interface
deba@1791
     1
/* -*- C++ -*-
deba@1791
     2
 * lemon/graph_extender.h - Part of LEMON, a generic C++ optimization library
deba@1791
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi
deba@1910
     5
 * Kutatocsoport (Egervary Research Groin on Combinatorial Optimization,
deba@1791
     6
 * EGRES).
deba@1791
     7
 *
deba@1791
     8
 * Permission to use, modify and distribute this software is granted
deba@1791
     9
 * provided that this copyright notice appears in all copies. For
deba@1791
    10
 * precise terms see the accompanying LICENSE file.
deba@1791
    11
 *
deba@1791
    12
 * This software is provided "AS IS" with no warranty of any kind,
deba@1791
    13
 * express or implied, and with no claim as to its suitability for any
deba@1791
    14
 * purpose.
deba@1791
    15
 *
deba@1791
    16
 */
deba@1791
    17
deba@1791
    18
#ifndef LEMON_GRAPH_EXTENDER_H
deba@1791
    19
#define LEMON_GRAPH_EXTENDER_H
deba@1791
    20
deba@1791
    21
#include <lemon/invalid.h>
deba@1820
    22
#include <lemon/error.h>
deba@1791
    23
deba@1791
    24
namespace lemon {
deba@1791
    25
deba@1791
    26
  template <typename _Base>
deba@1791
    27
  class GraphExtender : public _Base {
deba@1791
    28
  public:
deba@1791
    29
deba@1791
    30
    typedef _Base Parent;
deba@1791
    31
    typedef GraphExtender Graph;
deba@1791
    32
deba@1791
    33
    typedef typename Parent::Node Node;
deba@1791
    34
    typedef typename Parent::Edge Edge;
deba@1791
    35
deba@1791
    36
    int maxId(Node) const {
deba@1791
    37
      return Parent::maxNodeId();
deba@1791
    38
    }
deba@1791
    39
deba@1791
    40
    int maxId(Edge) const {
deba@1791
    41
      return Parent::maxEdgeId();
deba@1791
    42
    }
deba@1791
    43
deba@1791
    44
    Node fromId(int id, Node) const {
deba@1791
    45
      return Parent::nodeFromId(id);
deba@1791
    46
    }
deba@1791
    47
deba@1791
    48
    Edge fromId(int id, Edge) const {
deba@1791
    49
      return Parent::edgeFromId(id);
deba@1791
    50
    }
deba@1791
    51
deba@1791
    52
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@1791
    53
      if (n == Parent::source(e))
deba@1791
    54
	return Parent::target(e);
deba@1791
    55
      else if(n==Parent::target(e))
deba@1791
    56
	return Parent::source(e);
deba@1791
    57
      else
deba@1791
    58
	return INVALID;
deba@1791
    59
    }
deba@1791
    60
deba@1791
    61
  };
deba@1791
    62
deba@1791
    63
  template <typename _Base>
klao@1909
    64
  class UGraphExtender : public _Base {
deba@1791
    65
    typedef _Base Parent;
klao@1909
    66
    typedef UGraphExtender Graph;
deba@1791
    67
deba@1791
    68
  public:
deba@1791
    69
klao@1909
    70
    typedef typename Parent::Edge UEdge;
deba@1791
    71
    typedef typename Parent::Node Node;
deba@1791
    72
klao@1909
    73
    class Edge : public UEdge {
klao@1909
    74
      friend class UGraphExtender;
deba@1791
    75
deba@1791
    76
    protected:
deba@1791
    77
      // FIXME: Marci use opposite logic in his graph adaptors. It would
deba@1791
    78
      // be reasonable to syncronize...
deba@1791
    79
      bool forward;
deba@1791
    80
klao@1909
    81
      Edge(const UEdge &ue, bool _forward) :
klao@1909
    82
        UEdge(ue), forward(_forward) {}
deba@1791
    83
deba@1791
    84
    public:
deba@1791
    85
      Edge() {}
deba@1791
    86
deba@1791
    87
      /// Invalid edge constructor
klao@1909
    88
      Edge(Invalid i) : UEdge(i), forward(true) {}
deba@1791
    89
deba@1791
    90
      bool operator==(const Edge &that) const {
klao@1909
    91
	return forward==that.forward && UEdge(*this)==UEdge(that);
deba@1791
    92
      }
deba@1791
    93
      bool operator!=(const Edge &that) const {
klao@1909
    94
	return forward!=that.forward || UEdge(*this)!=UEdge(that);
deba@1791
    95
      }
deba@1791
    96
      bool operator<(const Edge &that) const {
deba@1791
    97
	return forward<that.forward ||
klao@1909
    98
	  (!(that.forward<forward) && UEdge(*this)<UEdge(that));
deba@1791
    99
      }
deba@1791
   100
    };
deba@1791
   101
deba@1791
   102
deba@1791
   103
    /// \brief Edge of opposite direction.
deba@1791
   104
    ///
deba@1791
   105
    /// Returns the Edge of opposite direction.
deba@1791
   106
    Edge oppositeEdge(const Edge &e) const {
deba@1791
   107
      return Edge(e,!e.forward);
deba@1791
   108
    }
deba@1791
   109
deba@1791
   110
  public:
deba@1791
   111
    /// \todo Shouldn't the "source" of an undirected edge be called "aNode"
deba@1791
   112
    /// or something???
deba@1791
   113
    using Parent::source;
deba@1791
   114
deba@1791
   115
    /// Source of the given Edge.
deba@1791
   116
    Node source(const Edge &e) const {
deba@1791
   117
      return e.forward ? Parent::source(e) : Parent::target(e);
deba@1791
   118
    }
deba@1791
   119
deba@1791
   120
    /// \todo Shouldn't the "target" of an undirected edge be called "bNode"
deba@1791
   121
    /// or something???
deba@1791
   122
    using Parent::target;
deba@1791
   123
deba@1791
   124
    /// Target of the given Edge.
deba@1791
   125
    Node target(const Edge &e) const {
deba@1791
   126
      return e.forward ? Parent::target(e) : Parent::source(e);
deba@1791
   127
    }
deba@1791
   128
klao@1909
   129
    Node oppositeNode(const Node &n, const UEdge &e) const {
deba@1791
   130
      if( n == Parent::source(e))
deba@1791
   131
	return Parent::target(e);
deba@1791
   132
      else if( n == Parent::target(e))
deba@1791
   133
	return Parent::source(e);
deba@1791
   134
      else
deba@1791
   135
	return INVALID;
deba@1791
   136
    }
deba@1791
   137
deba@1791
   138
    /// \brief Directed edge from an undirected edge and a source node.
deba@1791
   139
    ///
klao@1909
   140
    /// Returns a (directed) Edge corresponding to the specified UEdge
deba@1791
   141
    /// and source Node.
deba@1791
   142
    ///
klao@1909
   143
    Edge direct(const UEdge &ue, const Node &s) const {
deba@1791
   144
      return Edge(ue, s == source(ue));
deba@1791
   145
    }
deba@1791
   146
deba@1791
   147
    /// \brief Directed edge from an undirected edge.
deba@1791
   148
    ///
klao@1909
   149
    /// Returns a directed edge corresponding to the specified UEdge.
deba@1791
   150
    /// If the given bool is true the given undirected edge and the
deba@1791
   151
    /// returned edge have the same source node.
klao@1909
   152
    Edge direct(const UEdge &ue, bool d) const {
deba@1791
   153
      return Edge(ue, d);
deba@1791
   154
    }
deba@1791
   155
deba@1791
   156
    /// Returns whether the given directed edge is same orientation as the
deba@1791
   157
    /// corresponding undirected edge.
deba@1791
   158
    ///
deba@1791
   159
    /// \todo reference to the corresponding point of the undirected graph
deba@1791
   160
    /// concept. "What does the direction of an undirected edge mean?"
deba@1791
   161
    bool direction(const Edge &e) const { return e.forward; }
deba@1791
   162
deba@1791
   163
deba@1791
   164
    using Parent::first;
deba@1791
   165
    void first(Edge &e) const {
deba@1791
   166
      Parent::first(e);
deba@1791
   167
      e.forward=true;
deba@1791
   168
    }
deba@1791
   169
deba@1791
   170
    using Parent::next;
deba@1791
   171
    void next(Edge &e) const {
deba@1791
   172
      if( e.forward ) {
deba@1791
   173
	e.forward = false;
deba@1791
   174
      }
deba@1791
   175
      else {
deba@1791
   176
	Parent::next(e);
deba@1791
   177
	e.forward = true;
deba@1791
   178
      }
deba@1791
   179
    }
deba@1791
   180
deba@1791
   181
  public:
deba@1791
   182
deba@1791
   183
    void firstOut(Edge &e, const Node &n) const {
deba@1791
   184
      Parent::firstIn(e,n);
klao@1909
   185
      if( UEdge(e) != INVALID ) {
deba@1791
   186
	e.forward = false;
deba@1791
   187
      }
deba@1791
   188
      else {
deba@1791
   189
	Parent::firstOut(e,n);
deba@1791
   190
	e.forward = true;
deba@1791
   191
      }
deba@1791
   192
    }
deba@1791
   193
    void nextOut(Edge &e) const {
deba@1791
   194
      if( ! e.forward ) {
deba@1791
   195
	Node n = Parent::target(e);
deba@1791
   196
	Parent::nextIn(e);
klao@1909
   197
	if( UEdge(e) == INVALID ) {
deba@1791
   198
	  Parent::firstOut(e, n);
deba@1791
   199
	  e.forward = true;
deba@1791
   200
	}
deba@1791
   201
      }
deba@1791
   202
      else {
deba@1791
   203
	Parent::nextOut(e);
deba@1791
   204
      }
deba@1791
   205
    }
deba@1791
   206
deba@1791
   207
    void firstIn(Edge &e, const Node &n) const {
deba@1791
   208
      Parent::firstOut(e,n);
klao@1909
   209
      if( UEdge(e) != INVALID ) {
deba@1791
   210
	e.forward = false;
deba@1791
   211
      }
deba@1791
   212
      else {
deba@1791
   213
	Parent::firstIn(e,n);
deba@1791
   214
	e.forward = true;
deba@1791
   215
      }
deba@1791
   216
    }
deba@1791
   217
    void nextIn(Edge &e) const {
deba@1791
   218
      if( ! e.forward ) {
deba@1791
   219
	Node n = Parent::source(e);
deba@1791
   220
	Parent::nextOut(e);
klao@1909
   221
	if( UEdge(e) == INVALID ) {
deba@1791
   222
	  Parent::firstIn(e, n);
deba@1791
   223
	  e.forward = true;
deba@1791
   224
	}
deba@1791
   225
      }
deba@1791
   226
      else {
deba@1791
   227
	Parent::nextIn(e);
deba@1791
   228
      }
deba@1791
   229
    }
deba@1791
   230
klao@1909
   231
    void firstInc(UEdge &e, const Node &n) const {
deba@1791
   232
      Parent::firstOut(e, n);
deba@1791
   233
      if (e != INVALID) return;
deba@1791
   234
      Parent::firstIn(e, n);
deba@1791
   235
    }
klao@1909
   236
    void nextInc(UEdge &e, const Node &n) const {
deba@1791
   237
      if (Parent::source(e) == n) {
deba@1791
   238
	Parent::nextOut(e);
deba@1791
   239
	if (e != INVALID) return;
deba@1791
   240
	Parent::firstIn(e, n);
deba@1791
   241
      } else {
deba@1791
   242
	Parent::nextIn(e);
deba@1791
   243
      }
deba@1791
   244
    }
deba@1791
   245
klao@1909
   246
    void firstInc(UEdge &e, bool &d, const Node &n) const {
deba@1791
   247
      d = true;
deba@1791
   248
      Parent::firstOut(e, n);
deba@1791
   249
      if (e != INVALID) return;
deba@1791
   250
      d = false;
deba@1791
   251
      Parent::firstIn(e, n);
deba@1791
   252
    }
klao@1909
   253
    void nextInc(UEdge &e, bool &d) const {
deba@1791
   254
      if (d) {
deba@1791
   255
	Node s = Parent::source(e);
deba@1791
   256
	Parent::nextOut(e);
deba@1791
   257
	if (e != INVALID) return;
deba@1791
   258
	d = false;
deba@1791
   259
	Parent::firstIn(e, s);
deba@1791
   260
      } else {
deba@1791
   261
	Parent::nextIn(e);
deba@1791
   262
      }
deba@1791
   263
    }
deba@1791
   264
deba@1791
   265
    // Miscellaneous stuff:
deba@1791
   266
deba@1791
   267
    /// \todo these methods (id, maxEdgeId) should be moved into separate
deba@1791
   268
    /// Extender
deba@1791
   269
deba@1791
   270
    // using Parent::id;
deba@1791
   271
    // Using "using" is not a good idea, cause it could be that there is
deba@1791
   272
    // no "id" in Parent...
deba@1791
   273
deba@1791
   274
    int id(const Node &n) const {
deba@1791
   275
      return Parent::id(n);
deba@1791
   276
    }
deba@1791
   277
klao@1909
   278
    int id(const UEdge &e) const {
deba@1791
   279
      return Parent::id(e);
deba@1791
   280
    }
deba@1791
   281
deba@1791
   282
    int id(const Edge &e) const {
deba@1791
   283
      return 2 * Parent::id(e) + int(e.forward);
deba@1791
   284
    }
deba@1791
   285
deba@1791
   286
    int maxNodeId() const {
deba@1791
   287
      return Parent::maxNodeId();
deba@1791
   288
    }
deba@1791
   289
deba@1791
   290
    int maxEdgeId() const {
deba@1791
   291
      return 2 * Parent::maxEdgeId() + 1;
deba@1791
   292
    }
deba@1791
   293
klao@1909
   294
    int maxUEdgeId() const {
deba@1791
   295
      return Parent::maxEdgeId();
deba@1791
   296
    }
deba@1791
   297
deba@1791
   298
    int maxId(Node) const {
deba@1791
   299
      return maxNodeId();
deba@1791
   300
    }
deba@1791
   301
deba@1791
   302
    int maxId(Edge) const {
deba@1791
   303
      return maxEdgeId();
deba@1791
   304
    }
klao@1909
   305
    int maxId(UEdge) const {
klao@1909
   306
      return maxUEdgeId();
deba@1791
   307
    }
deba@1791
   308
deba@1791
   309
    int edgeNum() const {
deba@1791
   310
      return 2 * Parent::edgeNum();
deba@1791
   311
    }
deba@1791
   312
klao@1909
   313
    int uEdgeNum() const {
deba@1791
   314
      return Parent::edgeNum();
deba@1791
   315
    }
deba@1791
   316
deba@1791
   317
    Node nodeFromId(int id) const {
deba@1791
   318
      return Parent::nodeFromId(id);
deba@1791
   319
    }
deba@1791
   320
deba@1791
   321
    Edge edgeFromId(int id) const {
deba@1791
   322
      return direct(Parent::edgeFromId(id >> 1), bool(id & 1));
deba@1791
   323
    }
deba@1791
   324
klao@1909
   325
    UEdge uEdgeFromId(int id) const {
deba@1791
   326
      return Parent::edgeFromId(id >> 1);
deba@1791
   327
    }
deba@1791
   328
deba@1791
   329
    Node fromId(int id, Node) const {
deba@1791
   330
      return nodeFromId(id);
deba@1791
   331
    }
deba@1791
   332
deba@1791
   333
    Edge fromId(int id, Edge) const {
deba@1791
   334
      return edgeFromId(id);
deba@1791
   335
    }
deba@1791
   336
klao@1909
   337
    UEdge fromId(int id, UEdge) const {
klao@1909
   338
      return uEdgeFromId(id);
deba@1791
   339
    }
deba@1791
   340
deba@1791
   341
deba@1791
   342
    Edge findEdge(Node source, Node target, Edge prev) const {
deba@1791
   343
      if (prev == INVALID) {
klao@1909
   344
	UEdge edge = Parent::findEdge(source, target);
deba@1791
   345
	if (edge != INVALID) return direct(edge, true);
deba@1791
   346
	edge = Parent::findEdge(target, source);
deba@1791
   347
	if (edge != INVALID) return direct(edge, false);
deba@1791
   348
      } else if (direction(prev)) {
klao@1909
   349
	UEdge edge = Parent::findEdge(source, target, prev);
deba@1791
   350
	if (edge != INVALID) return direct(edge, true);
deba@1791
   351
	edge = Parent::findEdge(target, source);
deba@1791
   352
	if (edge != INVALID) return direct(edge, false);	
deba@1791
   353
      } else {
klao@1909
   354
	UEdge edge = Parent::findEdge(target, source, prev);
deba@1791
   355
	if (edge != INVALID) return direct(edge, false);	      
deba@1791
   356
      }
deba@1791
   357
      return INVALID;
deba@1791
   358
    }
deba@1791
   359
klao@1909
   360
    UEdge findUEdge(Node source, Node target, UEdge prev) const {
deba@1791
   361
      if (prev == INVALID) {
klao@1909
   362
	UEdge edge = Parent::findEdge(source, target);
deba@1791
   363
	if (edge != INVALID) return edge;
deba@1791
   364
	edge = Parent::findEdge(target, source);
deba@1791
   365
	if (edge != INVALID) return edge;
deba@1791
   366
      } else if (Parent::source(prev) == source) {
klao@1909
   367
	UEdge edge = Parent::findEdge(source, target, prev);
deba@1791
   368
	if (edge != INVALID) return edge;
deba@1791
   369
	edge = Parent::findEdge(target, source);
deba@1791
   370
	if (edge != INVALID) return edge;	
deba@1791
   371
      } else {
klao@1909
   372
	UEdge edge = Parent::findEdge(target, source, prev);
deba@1791
   373
	if (edge != INVALID) return edge;	      
deba@1791
   374
      }
deba@1791
   375
      return INVALID;
deba@1791
   376
    }
deba@1791
   377
deba@1791
   378
  };
deba@1791
   379
deba@1820
   380
deba@1820
   381
  template <typename _Base>
deba@1910
   382
  class BpUGraphExtender : public _Base {
deba@1820
   383
  public:
deba@1820
   384
    typedef _Base Parent;
deba@1910
   385
    typedef BpUGraphExtender Graph;
deba@1820
   386
deba@1820
   387
    typedef typename Parent::Node Node;
klao@1909
   388
    typedef typename Parent::Edge UEdge;
deba@1820
   389
deba@1820
   390
    using Parent::first;
deba@1820
   391
    using Parent::next;
deba@1820
   392
deba@1820
   393
    using Parent::id;
deba@1820
   394
klao@1909
   395
    Node source(const UEdge& edge) const {
deba@1910
   396
      return aNode(edge);
deba@1820
   397
    }
klao@1909
   398
    Node target(const UEdge& edge) const {
deba@1910
   399
      return bNode(edge);
deba@1820
   400
    }
deba@1820
   401
klao@1909
   402
    void firstInc(UEdge& edge, bool& direction, const Node& node) const {
deba@1910
   403
      if (Parent::aNode(node)) {
deba@1910
   404
	Parent::firstOut(edge, node);
deba@1820
   405
	direction = true;
deba@1820
   406
      } else {
deba@1910
   407
	Parent::firstIn(edge, node);
klao@1909
   408
	direction = static_cast<UEdge&>(edge) == INVALID;
deba@1820
   409
      }
deba@1820
   410
    }
klao@1909
   411
    void nextInc(UEdge& edge, bool& direction) const {
deba@1820
   412
      if (direction) {
deba@1910
   413
	Parent::nextOut(edge);
deba@1820
   414
      } else {
deba@1910
   415
	Parent::nextIn(edge);
deba@1820
   416
	if (edge == INVALID) direction = true;
deba@1820
   417
      }
deba@1820
   418
    }
deba@1820
   419
klao@1909
   420
    int maxUEdgeId() const {
deba@1820
   421
      return Parent::maxEdgeId();
deba@1820
   422
    }
deba@1820
   423
klao@1909
   424
    UEdge uEdgeFromId(int id) const {
deba@1820
   425
      return Parent::edgeFromId(id);
deba@1820
   426
    }
deba@1820
   427
klao@1909
   428
    class Edge : public UEdge {
deba@1910
   429
      friend class BpUGraphExtender;
deba@1820
   430
    protected:
deba@1820
   431
      bool forward;
deba@1820
   432
klao@1909
   433
      Edge(const UEdge& edge, bool _forward)
klao@1909
   434
	: UEdge(edge), forward(_forward) {}
deba@1820
   435
deba@1820
   436
    public:
deba@1820
   437
      Edge() {}
klao@1909
   438
      Edge (Invalid) : UEdge(INVALID), forward(true) {}
deba@1820
   439
      bool operator==(const Edge& i) const {
klao@1909
   440
	return UEdge::operator==(i) && forward == i.forward;
deba@1820
   441
      }
deba@1820
   442
      bool operator!=(const Edge& i) const {
klao@1909
   443
	return UEdge::operator!=(i) || forward != i.forward;
deba@1820
   444
      }
deba@1820
   445
      bool operator<(const Edge& i) const {
klao@1909
   446
	return UEdge::operator<(i) || 
klao@1909
   447
	  (!(i.forward<forward) && UEdge(*this)<UEdge(i));
deba@1820
   448
      }
deba@1820
   449
    };
deba@1820
   450
deba@1820
   451
    void first(Edge& edge) const {
klao@1909
   452
      Parent::first(static_cast<UEdge&>(edge));
deba@1820
   453
      edge.forward = true;
deba@1820
   454
    }
deba@1820
   455
deba@1820
   456
    void next(Edge& edge) const {
deba@1820
   457
      if (!edge.forward) {
klao@1909
   458
	Parent::next(static_cast<UEdge&>(edge));
deba@1820
   459
      }
deba@1820
   460
      edge.forward = !edge.forward;
deba@1820
   461
    }
deba@1820
   462
deba@1820
   463
    void firstOut(Edge& edge, const Node& node) const {
deba@1910
   464
      if (Parent::aNode(node)) {
deba@1910
   465
	Parent::firstOut(edge, node);
deba@1820
   466
	edge.forward = true;
deba@1820
   467
      } else {
deba@1910
   468
	Parent::firstIn(edge, node);
klao@1909
   469
	edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@1820
   470
      }
deba@1820
   471
    }
deba@1820
   472
    void nextOut(Edge& edge) const {
deba@1820
   473
      if (edge.forward) {
deba@1910
   474
	Parent::nextOut(edge);
deba@1820
   475
      } else {
deba@1910
   476
	Parent::nextIn(edge);
klao@1909
   477
        edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@1820
   478
      }
deba@1820
   479
    }
deba@1820
   480
deba@1820
   481
    void firstIn(Edge& edge, const Node& node) const {
deba@1910
   482
      if (Parent::bNode(node)) {
deba@1910
   483
	Parent::firstIn(edge, node);
deba@1820
   484
	edge.forward = true;	
deba@1820
   485
      } else {
deba@1910
   486
	Parent::firstOut(edge, node);
klao@1909
   487
	edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@1820
   488
      }
deba@1820
   489
    }
deba@1820
   490
    void nextIn(Edge& edge) const {
deba@1820
   491
      if (edge.forward) {
deba@1910
   492
	Parent::nextIn(edge);
deba@1820
   493
      } else {
deba@1910
   494
	Parent::nextOut(edge);
klao@1909
   495
	edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@1820
   496
      }
deba@1820
   497
    }
deba@1820
   498
deba@1820
   499
    Node source(const Edge& edge) const {
deba@1910
   500
      return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
deba@1820
   501
    }
deba@1820
   502
    Node target(const Edge& edge) const {
deba@1910
   503
      return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
deba@1820
   504
    }
deba@1820
   505
deba@1820
   506
    bool direction(const Edge& edge) const {
deba@1820
   507
      return edge.forward;
deba@1820
   508
    }
deba@1820
   509
klao@1909
   510
    Edge direct(const UEdge& edge, const Node& node) const {
deba@1820
   511
      return Edge(edge, node == Parent::source(edge));
deba@1820
   512
    }
deba@1820
   513
klao@1909
   514
    Edge direct(const UEdge& edge, bool direction) const {
deba@1820
   515
      return Edge(edge, direction);
deba@1820
   516
    }
deba@1820
   517
klao@1909
   518
    Node oppositeNode(const UEdge& edge, const Node& node) const {
deba@1820
   519
      return source(edge) == node ? 
deba@1820
   520
	target(edge) : source(edge);
deba@1820
   521
    }
deba@1820
   522
deba@1820
   523
    Edge oppositeEdge(const Edge& edge) const {
deba@1820
   524
      return Edge(edge, !edge.forward);
deba@1820
   525
    }
deba@1820
   526
deba@1820
   527
    int id(const Edge& edge) const {
deba@1820
   528
      return (Parent::id(edge) << 1) + (edge.forward ? 0 : 1);
deba@1820
   529
    }
deba@1820
   530
    Edge edgeFromId(int id) const {
klao@1909
   531
      return Edge(Parent::fromId(id >> 1, UEdge()), (id & 1) == 0);
deba@1820
   532
    }
deba@1820
   533
    int maxEdgeId() const {
klao@1909
   534
      return (Parent::maxId(UEdge()) << 1) + 1;
deba@1820
   535
    }
deba@1820
   536
deba@1910
   537
    class ANode : public Node {
deba@1910
   538
      friend class BpUGraphExtender;
deba@1820
   539
    public:
deba@1910
   540
      ANode() {}
deba@1910
   541
      ANode(const Node& node) : Node(node) {
deba@1910
   542
	LEMON_ASSERT(Parent::aNode(node) || node == INVALID, 
deba@1820
   543
		     typename Parent::NodeSetError());
deba@1820
   544
      }
deba@1910
   545
      ANode(Invalid) : Node(INVALID) {}
deba@1820
   546
    };
deba@1820
   547
deba@1910
   548
    void first(ANode& node) const {
deba@1910
   549
      Parent::firstANode(static_cast<Node&>(node));
deba@1820
   550
    }
deba@1910
   551
    void next(ANode& node) const {
deba@1910
   552
      Parent::nextANode(static_cast<Node&>(node));
deba@1820
   553
    }
deba@1820
   554
deba@1910
   555
    int id(const ANode& node) const {
deba@1910
   556
      return Parent::aNodeId(node);
deba@1820
   557
    }
deba@1820
   558
deba@1910
   559
    class BNode : public Node {
deba@1910
   560
      friend class BpUGraphExtender;
deba@1820
   561
    public:
deba@1910
   562
      BNode() {}
deba@1910
   563
      BNode(const Node& node) : Node(node) {
deba@1910
   564
	LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
deba@1820
   565
		     typename Parent::NodeSetError());
deba@1820
   566
      }
deba@1910
   567
      BNode(Invalid) : Node(INVALID) {}
deba@1820
   568
    };
deba@1820
   569
deba@1910
   570
    void first(BNode& node) const {
deba@1910
   571
      Parent::firstBNode(static_cast<Node&>(node));
deba@1820
   572
    }
deba@1910
   573
    void next(BNode& node) const {
deba@1910
   574
      Parent::nextBNode(static_cast<Node&>(node));
deba@1820
   575
    }
deba@1820
   576
  
deba@1910
   577
    int id(const BNode& node) const {
deba@1910
   578
      return Parent::aNodeId(node);
deba@1820
   579
    }
deba@1820
   580
deba@1820
   581
deba@1820
   582
deba@1820
   583
    int maxId(Node) const {
deba@1820
   584
      return Parent::maxNodeId();
deba@1820
   585
    }
deba@1910
   586
    int maxId(BNode) const {
deba@1910
   587
      return Parent::maxBNodeId();
deba@1820
   588
    }
deba@1910
   589
    int maxId(ANode) const {
deba@1910
   590
      return Parent::maxANodeId();
deba@1820
   591
    }
deba@1820
   592
    int maxId(Edge) const {
deba@1820
   593
      return maxEdgeId();
deba@1820
   594
    }
klao@1909
   595
    int maxId(UEdge) const {
klao@1909
   596
      return maxUEdgeId();
deba@1820
   597
    }
deba@1820
   598
deba@1820
   599
deba@1820
   600
    Node fromId(int id, Node) const {
deba@1820
   601
      return Parent::nodeFromId(id);
deba@1820
   602
    }
deba@1910
   603
    ANode fromId(int id, ANode) const {
deba@1910
   604
      return Parent::fromANodeId(id);
deba@1820
   605
    }
deba@1910
   606
    BNode fromId(int id, BNode) const {
deba@1910
   607
      return Parent::fromBNodeId(id);
deba@1820
   608
    }
deba@1820
   609
    Edge fromId(int id, Edge) const {
deba@1820
   610
      return edgeFromId(id);
deba@1820
   611
    }
klao@1909
   612
    UEdge fromId(int id, UEdge) const {
klao@1909
   613
      return uEdgeFromId(id);
deba@1820
   614
    }
deba@1820
   615
deba@1820
   616
  };
deba@1820
   617
deba@1791
   618
}
deba@1791
   619
deba@1791
   620
#endif // LEMON_UNDIR_GRAPH_EXTENDER_H