lemon/bits/graph_adaptor_extender.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 29 Sep 2009 12:03:02 +0200
changeset 777 5764dd9b6e18
parent 580 2313edd0db0b
child 882 ece1f8a3052d
permissions -rw-r--r--
Add a new build() function to StaticDigraph (#68)

This function builds the digraph from an arc list that
contains pairs of integer indices from the range [0..n-1].
It is useful in the cases when you would like to build a
StaticDigraph from scratch, i.e. you do not want to build
another digraph that can be copied using the other build()
function.
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
deba@414
   184
    typedef typename Parent::Node Node;
deba@414
   185
    typedef typename Parent::Arc Arc;
deba@414
   186
    typedef typename Parent::Edge Edge;
deba@414
   187
deba@416
   188
    // Graph extension
deba@414
   189
deba@414
   190
    int maxId(Node) const {
deba@414
   191
      return Parent::maxNodeId();
deba@414
   192
    }
deba@414
   193
deba@414
   194
    int maxId(Arc) const {
deba@414
   195
      return Parent::maxArcId();
deba@414
   196
    }
deba@414
   197
deba@414
   198
    int maxId(Edge) const {
deba@414
   199
      return Parent::maxEdgeId();
deba@414
   200
    }
deba@414
   201
deba@414
   202
    Node fromId(int id, Node) const {
deba@414
   203
      return Parent::nodeFromId(id);
deba@414
   204
    }
deba@414
   205
deba@414
   206
    Arc fromId(int id, Arc) const {
deba@414
   207
      return Parent::arcFromId(id);
deba@414
   208
    }
deba@414
   209
deba@414
   210
    Edge fromId(int id, Edge) const {
deba@414
   211
      return Parent::edgeFromId(id);
deba@414
   212
    }
deba@414
   213
deba@414
   214
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@414
   215
      if( n == Parent::u(e))
deba@416
   216
        return Parent::v(e);
deba@414
   217
      else if( n == Parent::v(e))
deba@416
   218
        return Parent::u(e);
deba@414
   219
      else
deba@416
   220
        return INVALID;
deba@414
   221
    }
deba@414
   222
deba@414
   223
    Arc oppositeArc(const Arc &a) const {
deba@414
   224
      return Parent::direct(a, !Parent::direction(a));
deba@414
   225
    }
deba@414
   226
deba@414
   227
    using Parent::direct;
deba@414
   228
    Arc direct(const Edge &e, const Node &s) const {
deba@414
   229
      return Parent::direct(e, Parent::u(e) == s);
deba@414
   230
    }
deba@414
   231
deba@414
   232
deba@416
   233
    class NodeIt : public Node {
deba@414
   234
      const Adaptor* _adaptor;
deba@414
   235
    public:
deba@414
   236
deba@414
   237
      NodeIt() {}
deba@414
   238
deba@414
   239
      NodeIt(Invalid i) : Node(i) { }
deba@414
   240
deba@414
   241
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
   242
        _adaptor->first(static_cast<Node&>(*this));
deba@414
   243
      }
deba@414
   244
deba@416
   245
      NodeIt(const Adaptor& adaptor, const Node& node)
deba@416
   246
        : Node(node), _adaptor(&adaptor) {}
deba@414
   247
deba@416
   248
      NodeIt& operator++() {
deba@416
   249
        _adaptor->next(*this);
deba@416
   250
        return *this;
deba@414
   251
      }
deba@414
   252
deba@414
   253
    };
deba@414
   254
deba@414
   255
deba@416
   256
    class ArcIt : public Arc {
deba@414
   257
      const Adaptor* _adaptor;
deba@414
   258
    public:
deba@414
   259
deba@414
   260
      ArcIt() { }
deba@414
   261
deba@414
   262
      ArcIt(Invalid i) : Arc(i) { }
deba@414
   263
deba@414
   264
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
   265
        _adaptor->first(static_cast<Arc&>(*this));
deba@414
   266
      }
deba@414
   267
deba@416
   268
      ArcIt(const Adaptor& adaptor, const Arc& e) :
deba@416
   269
        Arc(e), _adaptor(&adaptor) { }
deba@414
   270
deba@416
   271
      ArcIt& operator++() {
deba@416
   272
        _adaptor->next(*this);
deba@416
   273
        return *this;
deba@414
   274
      }
deba@414
   275
deba@414
   276
    };
deba@414
   277
deba@414
   278
deba@416
   279
    class OutArcIt : public Arc {
deba@414
   280
      const Adaptor* _adaptor;
deba@414
   281
    public:
deba@414
   282
deba@414
   283
      OutArcIt() { }
deba@414
   284
deba@414
   285
      OutArcIt(Invalid i) : Arc(i) { }
deba@414
   286
deba@416
   287
      OutArcIt(const Adaptor& adaptor, const Node& node)
deba@416
   288
        : _adaptor(&adaptor) {
deba@416
   289
        _adaptor->firstOut(*this, node);
deba@414
   290
      }
deba@414
   291
deba@416
   292
      OutArcIt(const Adaptor& adaptor, const Arc& arc)
deba@416
   293
        : Arc(arc), _adaptor(&adaptor) {}
deba@414
   294
deba@416
   295
      OutArcIt& operator++() {
deba@416
   296
        _adaptor->nextOut(*this);
deba@416
   297
        return *this;
deba@414
   298
      }
deba@414
   299
deba@414
   300
    };
deba@414
   301
deba@414
   302
deba@416
   303
    class InArcIt : public Arc {
deba@414
   304
      const Adaptor* _adaptor;
deba@414
   305
    public:
deba@414
   306
deba@414
   307
      InArcIt() { }
deba@414
   308
deba@414
   309
      InArcIt(Invalid i) : Arc(i) { }
deba@414
   310
deba@416
   311
      InArcIt(const Adaptor& adaptor, const Node& node)
deba@416
   312
        : _adaptor(&adaptor) {
deba@416
   313
        _adaptor->firstIn(*this, node);
deba@414
   314
      }
deba@414
   315
deba@416
   316
      InArcIt(const Adaptor& adaptor, const Arc& arc) :
deba@416
   317
        Arc(arc), _adaptor(&adaptor) {}
deba@414
   318
deba@416
   319
      InArcIt& operator++() {
deba@416
   320
        _adaptor->nextIn(*this);
deba@416
   321
        return *this;
deba@414
   322
      }
deba@414
   323
deba@414
   324
    };
deba@414
   325
deba@416
   326
    class EdgeIt : public Parent::Edge {
deba@414
   327
      const Adaptor* _adaptor;
deba@414
   328
    public:
deba@414
   329
deba@414
   330
      EdgeIt() { }
deba@414
   331
deba@414
   332
      EdgeIt(Invalid i) : Edge(i) { }
deba@414
   333
deba@414
   334
      explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
deba@416
   335
        _adaptor->first(static_cast<Edge&>(*this));
deba@414
   336
      }
deba@414
   337
deba@416
   338
      EdgeIt(const Adaptor& adaptor, const Edge& e) :
deba@416
   339
        Edge(e), _adaptor(&adaptor) { }
deba@414
   340
deba@416
   341
      EdgeIt& operator++() {
deba@416
   342
        _adaptor->next(*this);
deba@416
   343
        return *this;
deba@414
   344
      }
deba@414
   345
deba@414
   346
    };
deba@414
   347
deba@416
   348
    class IncEdgeIt : public Edge {
deba@414
   349
      friend class GraphAdaptorExtender;
deba@414
   350
      const Adaptor* _adaptor;
deba@414
   351
      bool direction;
deba@414
   352
    public:
deba@414
   353
deba@414
   354
      IncEdgeIt() { }
deba@414
   355
deba@414
   356
      IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
deba@414
   357
deba@414
   358
      IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
deba@416
   359
        _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
deba@414
   360
      }
deba@414
   361
deba@414
   362
      IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
deba@416
   363
        : _adaptor(&adaptor), Edge(e) {
deba@416
   364
        direction = (_adaptor->u(e) == n);
deba@414
   365
      }
deba@414
   366
deba@414
   367
      IncEdgeIt& operator++() {
deba@416
   368
        _adaptor->nextInc(*this, direction);
deba@416
   369
        return *this;
deba@414
   370
      }
deba@414
   371
    };
deba@414
   372
deba@414
   373
    Node baseNode(const OutArcIt &a) const {
deba@414
   374
      return Parent::source(a);
deba@414
   375
    }
deba@414
   376
    Node runningNode(const OutArcIt &a) const {
deba@414
   377
      return Parent::target(a);
deba@414
   378
    }
deba@414
   379
deba@414
   380
    Node baseNode(const InArcIt &a) const {
deba@414
   381
      return Parent::target(a);
deba@414
   382
    }
deba@414
   383
    Node runningNode(const InArcIt &a) const {
deba@414
   384
      return Parent::source(a);
deba@414
   385
    }
deba@414
   386
deba@414
   387
    Node baseNode(const IncEdgeIt &e) const {
deba@414
   388
      return e.direction ? Parent::u(e) : Parent::v(e);
deba@414
   389
    }
deba@414
   390
    Node runningNode(const IncEdgeIt &e) const {
deba@414
   391
      return e.direction ? Parent::v(e) : Parent::u(e);
deba@414
   392
    }
deba@414
   393
deba@414
   394
  };
deba@414
   395
deba@414
   396
}
deba@414
   397
deba@414
   398
deba@414
   399
#endif