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