lemon/bits/graph_adaptor_extender.h
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 06 Aug 2013 12:28:37 +0200
changeset 1079 5958cc5c0a98
parent 617 4137ef9aacc6
child 1092 dceba191c00d
permissions -rw-r--r--
Merge further fixes #470
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 {
kpeter@617
    29
    typedef _Digraph Parent;
kpeter@617
    30
deba@414
    31
  public:
deba@414
    32
deba@414
    33
    typedef _Digraph Digraph;
deba@414
    34
    typedef DigraphAdaptorExtender Adaptor;
deba@414
    35
deba@414
    36
    // Base extensions
deba@414
    37
deba@414
    38
    typedef typename Parent::Node Node;
deba@414
    39
    typedef typename Parent::Arc Arc;
deba@414
    40
deba@414
    41
    int maxId(Node) const {
deba@414
    42
      return Parent::maxNodeId();
deba@414
    43
    }
deba@414
    44
deba@414
    45
    int maxId(Arc) const {
deba@414
    46
      return Parent::maxArcId();
deba@414
    47
    }
deba@414
    48
deba@414
    49
    Node fromId(int id, Node) const {
deba@414
    50
      return Parent::nodeFromId(id);
deba@414
    51
    }
deba@414
    52
deba@414
    53
    Arc fromId(int id, Arc) const {
deba@414
    54
      return Parent::arcFromId(id);
deba@414
    55
    }
deba@414
    56
deba@414
    57
    Node oppositeNode(const Node &n, const Arc &e) const {
deba@414
    58
      if (n == Parent::source(e))
deba@416
    59
        return Parent::target(e);
deba@414
    60
      else if(n==Parent::target(e))
deba@416
    61
        return Parent::source(e);
deba@414
    62
      else
deba@416
    63
        return INVALID;
deba@414
    64
    }
deba@414
    65
deba@416
    66
    class NodeIt : public Node {
deba@414
    67
      const Adaptor* _adaptor;
deba@414
    68
    public:
deba@414
    69
deba@414
    70
      NodeIt() {}
deba@414
    71
deba@414
    72
      NodeIt(Invalid i) : Node(i) { }
deba@414
    73
deba@414
    74
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
    75
        _adaptor->first(static_cast<Node&>(*this));
deba@414
    76
      }
deba@414
    77
deba@416
    78
      NodeIt(const Adaptor& adaptor, const Node& node)
deba@416
    79
        : Node(node), _adaptor(&adaptor) {}
deba@414
    80
deba@416
    81
      NodeIt& operator++() {
deba@416
    82
        _adaptor->next(*this);
deba@416
    83
        return *this;
deba@414
    84
      }
deba@414
    85
deba@414
    86
    };
deba@414
    87
deba@414
    88
deba@416
    89
    class ArcIt : public Arc {
deba@414
    90
      const Adaptor* _adaptor;
deba@414
    91
    public:
deba@414
    92
deba@414
    93
      ArcIt() { }
deba@414
    94
deba@414
    95
      ArcIt(Invalid i) : Arc(i) { }
deba@414
    96
deba@414
    97
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
    98
        _adaptor->first(static_cast<Arc&>(*this));
deba@414
    99
      }
deba@414
   100
deba@416
   101
      ArcIt(const Adaptor& adaptor, const Arc& e) :
deba@416
   102
        Arc(e), _adaptor(&adaptor) { }
deba@414
   103
deba@416
   104
      ArcIt& operator++() {
deba@416
   105
        _adaptor->next(*this);
deba@416
   106
        return *this;
deba@414
   107
      }
deba@414
   108
deba@414
   109
    };
deba@414
   110
deba@414
   111
deba@416
   112
    class OutArcIt : public Arc {
deba@414
   113
      const Adaptor* _adaptor;
deba@414
   114
    public:
deba@414
   115
deba@414
   116
      OutArcIt() { }
deba@414
   117
deba@414
   118
      OutArcIt(Invalid i) : Arc(i) { }
deba@414
   119
deba@416
   120
      OutArcIt(const Adaptor& adaptor, const Node& node)
deba@416
   121
        : _adaptor(&adaptor) {
deba@416
   122
        _adaptor->firstOut(*this, node);
deba@414
   123
      }
deba@414
   124
deba@416
   125
      OutArcIt(const Adaptor& adaptor, const Arc& arc)
deba@416
   126
        : Arc(arc), _adaptor(&adaptor) {}
deba@414
   127
deba@416
   128
      OutArcIt& operator++() {
deba@416
   129
        _adaptor->nextOut(*this);
deba@416
   130
        return *this;
deba@414
   131
      }
deba@414
   132
deba@414
   133
    };
deba@414
   134
deba@414
   135
deba@416
   136
    class InArcIt : public Arc {
deba@414
   137
      const Adaptor* _adaptor;
deba@414
   138
    public:
deba@414
   139
deba@414
   140
      InArcIt() { }
deba@414
   141
deba@414
   142
      InArcIt(Invalid i) : Arc(i) { }
deba@414
   143
deba@416
   144
      InArcIt(const Adaptor& adaptor, const Node& node)
deba@416
   145
        : _adaptor(&adaptor) {
deba@416
   146
        _adaptor->firstIn(*this, node);
deba@414
   147
      }
deba@414
   148
deba@416
   149
      InArcIt(const Adaptor& adaptor, const Arc& arc) :
deba@416
   150
        Arc(arc), _adaptor(&adaptor) {}
deba@414
   151
deba@416
   152
      InArcIt& operator++() {
deba@416
   153
        _adaptor->nextIn(*this);
deba@416
   154
        return *this;
deba@414
   155
      }
deba@414
   156
deba@414
   157
    };
deba@414
   158
deba@414
   159
    Node baseNode(const OutArcIt &e) const {
deba@414
   160
      return Parent::source(e);
deba@414
   161
    }
deba@414
   162
    Node runningNode(const OutArcIt &e) const {
deba@414
   163
      return Parent::target(e);
deba@414
   164
    }
deba@414
   165
deba@414
   166
    Node baseNode(const InArcIt &e) const {
deba@414
   167
      return Parent::target(e);
deba@414
   168
    }
deba@414
   169
    Node runningNode(const InArcIt &e) const {
deba@414
   170
      return Parent::source(e);
deba@414
   171
    }
deba@414
   172
deba@414
   173
  };
deba@414
   174
deba@416
   175
  template <typename _Graph>
deba@414
   176
  class GraphAdaptorExtender : public _Graph {
kpeter@617
   177
    typedef _Graph Parent;
kpeter@617
   178
deba@414
   179
  public:
deba@416
   180
deba@414
   181
    typedef _Graph Graph;
deba@414
   182
    typedef GraphAdaptorExtender Adaptor;
deba@414
   183
kpeter@882
   184
    typedef True UndirectedTag;
kpeter@882
   185
deba@414
   186
    typedef typename Parent::Node Node;
deba@414
   187
    typedef typename Parent::Arc Arc;
deba@414
   188
    typedef typename Parent::Edge Edge;
deba@414
   189
deba@416
   190
    // Graph extension
deba@414
   191
deba@414
   192
    int maxId(Node) const {
deba@414
   193
      return Parent::maxNodeId();
deba@414
   194
    }
deba@414
   195
deba@414
   196
    int maxId(Arc) const {
deba@414
   197
      return Parent::maxArcId();
deba@414
   198
    }
deba@414
   199
deba@414
   200
    int maxId(Edge) const {
deba@414
   201
      return Parent::maxEdgeId();
deba@414
   202
    }
deba@414
   203
deba@414
   204
    Node fromId(int id, Node) const {
deba@414
   205
      return Parent::nodeFromId(id);
deba@414
   206
    }
deba@414
   207
deba@414
   208
    Arc fromId(int id, Arc) const {
deba@414
   209
      return Parent::arcFromId(id);
deba@414
   210
    }
deba@414
   211
deba@414
   212
    Edge fromId(int id, Edge) const {
deba@414
   213
      return Parent::edgeFromId(id);
deba@414
   214
    }
deba@414
   215
deba@414
   216
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@414
   217
      if( n == Parent::u(e))
deba@416
   218
        return Parent::v(e);
deba@414
   219
      else if( n == Parent::v(e))
deba@416
   220
        return Parent::u(e);
deba@414
   221
      else
deba@416
   222
        return INVALID;
deba@414
   223
    }
deba@414
   224
deba@414
   225
    Arc oppositeArc(const Arc &a) const {
deba@414
   226
      return Parent::direct(a, !Parent::direction(a));
deba@414
   227
    }
deba@414
   228
deba@414
   229
    using Parent::direct;
deba@414
   230
    Arc direct(const Edge &e, const Node &s) const {
deba@414
   231
      return Parent::direct(e, Parent::u(e) == s);
deba@414
   232
    }
deba@414
   233
deba@414
   234
deba@416
   235
    class NodeIt : public Node {
deba@414
   236
      const Adaptor* _adaptor;
deba@414
   237
    public:
deba@414
   238
deba@414
   239
      NodeIt() {}
deba@414
   240
deba@414
   241
      NodeIt(Invalid i) : Node(i) { }
deba@414
   242
deba@414
   243
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
   244
        _adaptor->first(static_cast<Node&>(*this));
deba@414
   245
      }
deba@414
   246
deba@416
   247
      NodeIt(const Adaptor& adaptor, const Node& node)
deba@416
   248
        : Node(node), _adaptor(&adaptor) {}
deba@414
   249
deba@416
   250
      NodeIt& operator++() {
deba@416
   251
        _adaptor->next(*this);
deba@416
   252
        return *this;
deba@414
   253
      }
deba@414
   254
deba@414
   255
    };
deba@414
   256
deba@414
   257
deba@416
   258
    class ArcIt : public Arc {
deba@414
   259
      const Adaptor* _adaptor;
deba@414
   260
    public:
deba@414
   261
deba@414
   262
      ArcIt() { }
deba@414
   263
deba@414
   264
      ArcIt(Invalid i) : Arc(i) { }
deba@414
   265
deba@414
   266
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
   267
        _adaptor->first(static_cast<Arc&>(*this));
deba@414
   268
      }
deba@414
   269
deba@416
   270
      ArcIt(const Adaptor& adaptor, const Arc& e) :
deba@416
   271
        Arc(e), _adaptor(&adaptor) { }
deba@414
   272
deba@416
   273
      ArcIt& operator++() {
deba@416
   274
        _adaptor->next(*this);
deba@416
   275
        return *this;
deba@414
   276
      }
deba@414
   277
deba@414
   278
    };
deba@414
   279
deba@414
   280
deba@416
   281
    class OutArcIt : public Arc {
deba@414
   282
      const Adaptor* _adaptor;
deba@414
   283
    public:
deba@414
   284
deba@414
   285
      OutArcIt() { }
deba@414
   286
deba@414
   287
      OutArcIt(Invalid i) : Arc(i) { }
deba@414
   288
deba@416
   289
      OutArcIt(const Adaptor& adaptor, const Node& node)
deba@416
   290
        : _adaptor(&adaptor) {
deba@416
   291
        _adaptor->firstOut(*this, node);
deba@414
   292
      }
deba@414
   293
deba@416
   294
      OutArcIt(const Adaptor& adaptor, const Arc& arc)
deba@416
   295
        : Arc(arc), _adaptor(&adaptor) {}
deba@414
   296
deba@416
   297
      OutArcIt& operator++() {
deba@416
   298
        _adaptor->nextOut(*this);
deba@416
   299
        return *this;
deba@414
   300
      }
deba@414
   301
deba@414
   302
    };
deba@414
   303
deba@414
   304
deba@416
   305
    class InArcIt : public Arc {
deba@414
   306
      const Adaptor* _adaptor;
deba@414
   307
    public:
deba@414
   308
deba@414
   309
      InArcIt() { }
deba@414
   310
deba@414
   311
      InArcIt(Invalid i) : Arc(i) { }
deba@414
   312
deba@416
   313
      InArcIt(const Adaptor& adaptor, const Node& node)
deba@416
   314
        : _adaptor(&adaptor) {
deba@416
   315
        _adaptor->firstIn(*this, node);
deba@414
   316
      }
deba@414
   317
deba@416
   318
      InArcIt(const Adaptor& adaptor, const Arc& arc) :
deba@416
   319
        Arc(arc), _adaptor(&adaptor) {}
deba@414
   320
deba@416
   321
      InArcIt& operator++() {
deba@416
   322
        _adaptor->nextIn(*this);
deba@416
   323
        return *this;
deba@414
   324
      }
deba@414
   325
deba@414
   326
    };
deba@414
   327
deba@416
   328
    class EdgeIt : public Parent::Edge {
deba@414
   329
      const Adaptor* _adaptor;
deba@414
   330
    public:
deba@414
   331
deba@414
   332
      EdgeIt() { }
deba@414
   333
deba@414
   334
      EdgeIt(Invalid i) : Edge(i) { }
deba@414
   335
deba@414
   336
      explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
   337
        _adaptor->first(static_cast<Edge&>(*this));
deba@414
   338
      }
deba@414
   339
deba@416
   340
      EdgeIt(const Adaptor& adaptor, const Edge& e) :
deba@416
   341
        Edge(e), _adaptor(&adaptor) { }
deba@414
   342
deba@416
   343
      EdgeIt& operator++() {
deba@416
   344
        _adaptor->next(*this);
deba@416
   345
        return *this;
deba@414
   346
      }
deba@414
   347
deba@414
   348
    };
deba@414
   349
deba@416
   350
    class IncEdgeIt : public Edge {
deba@414
   351
      friend class GraphAdaptorExtender;
deba@414
   352
      const Adaptor* _adaptor;
deba@414
   353
      bool direction;
deba@414
   354
    public:
deba@414
   355
deba@414
   356
      IncEdgeIt() { }
deba@414
   357
deba@414
   358
      IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
deba@414
   359
deba@414
   360
      IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
deba@416
   361
        _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
deba@414
   362
      }
deba@414
   363
deba@414
   364
      IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
deba@416
   365
        : _adaptor(&adaptor), Edge(e) {
deba@416
   366
        direction = (_adaptor->u(e) == n);
deba@414
   367
      }
deba@414
   368
deba@414
   369
      IncEdgeIt& operator++() {
deba@416
   370
        _adaptor->nextInc(*this, direction);
deba@416
   371
        return *this;
deba@414
   372
      }
deba@414
   373
    };
deba@414
   374
deba@414
   375
    Node baseNode(const OutArcIt &a) const {
deba@414
   376
      return Parent::source(a);
deba@414
   377
    }
deba@414
   378
    Node runningNode(const OutArcIt &a) const {
deba@414
   379
      return Parent::target(a);
deba@414
   380
    }
deba@414
   381
deba@414
   382
    Node baseNode(const InArcIt &a) const {
deba@414
   383
      return Parent::target(a);
deba@414
   384
    }
deba@414
   385
    Node runningNode(const InArcIt &a) const {
deba@414
   386
      return Parent::source(a);
deba@414
   387
    }
deba@414
   388
deba@414
   389
    Node baseNode(const IncEdgeIt &e) const {
deba@414
   390
      return e.direction ? Parent::u(e) : Parent::v(e);
deba@414
   391
    }
deba@414
   392
    Node runningNode(const IncEdgeIt &e) const {
deba@414
   393
      return e.direction ? Parent::v(e) : Parent::u(e);
deba@414
   394
    }
deba@414
   395
deba@414
   396
  };
deba@414
   397
deba@414
   398
}
deba@414
   399
deba@414
   400
deba@414
   401
#endif