lemon/bits/base_extender.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 21 Sep 2008 08:48:08 +0100
changeset 273 5d12d5c80ac9
parent 220 a5d8c039f218
child 280 e7f8647ce760
permissions -rw-r--r--
Better versioning (#144)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@57
     4
 *
alpar@107
     5
 * Copyright (C) 2003-2008
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_BITS_BASE_EXTENDER_H
deba@57
    20
#define LEMON_BITS_BASE_EXTENDER_H
deba@57
    21
deba@220
    22
#include <lemon/core.h>
deba@57
    23
#include <lemon/error.h>
deba@57
    24
deba@57
    25
#include <lemon/bits/map_extender.h>
deba@57
    26
#include <lemon/bits/default_map.h>
deba@57
    27
deba@57
    28
#include <lemon/concept_check.h>
deba@57
    29
#include <lemon/concepts/maps.h>
deba@57
    30
deba@57
    31
///\ingroup digraphbits
deba@57
    32
///\file
deba@57
    33
///\brief Extenders for the digraph types
deba@57
    34
namespace lemon {
deba@57
    35
deba@57
    36
  /// \ingroup digraphbits
deba@57
    37
  ///
deba@57
    38
  /// \brief BaseDigraph to BaseGraph extender
deba@57
    39
  template <typename Base>
deba@57
    40
  class UndirDigraphExtender : public Base {
deba@57
    41
deba@57
    42
  public:
deba@57
    43
deba@57
    44
    typedef Base Parent;
deba@57
    45
    typedef typename Parent::Arc Edge;
deba@57
    46
    typedef typename Parent::Node Node;
deba@57
    47
deba@57
    48
    typedef True UndirectedTag;
deba@57
    49
deba@57
    50
    class Arc : public Edge {
deba@57
    51
      friend class UndirDigraphExtender;
deba@57
    52
deba@57
    53
    protected:
deba@57
    54
      bool forward;
deba@57
    55
deba@57
    56
      Arc(const Edge &ue, bool _forward) :
deba@57
    57
        Edge(ue), forward(_forward) {}
deba@57
    58
deba@57
    59
    public:
deba@57
    60
      Arc() {}
deba@57
    61
kpeter@256
    62
      // Invalid arc constructor
deba@57
    63
      Arc(Invalid i) : Edge(i), forward(true) {}
deba@57
    64
deba@57
    65
      bool operator==(const Arc &that) const {
alpar@209
    66
        return forward==that.forward && Edge(*this)==Edge(that);
deba@57
    67
      }
deba@57
    68
      bool operator!=(const Arc &that) const {
alpar@209
    69
        return forward!=that.forward || Edge(*this)!=Edge(that);
deba@57
    70
      }
deba@57
    71
      bool operator<(const Arc &that) const {
alpar@209
    72
        return forward<that.forward ||
alpar@209
    73
          (!(that.forward<forward) && Edge(*this)<Edge(that));
deba@57
    74
      }
deba@57
    75
    };
deba@57
    76
kpeter@256
    77
    /// First node of the edge
kpeter@256
    78
    Node u(const Edge &e) const {
kpeter@256
    79
      return Parent::source(e);
kpeter@256
    80
    }
deba@57
    81
kpeter@256
    82
    /// Source of the given arc
deba@57
    83
    Node source(const Arc &e) const {
deba@57
    84
      return e.forward ? Parent::source(e) : Parent::target(e);
deba@57
    85
    }
deba@57
    86
kpeter@256
    87
    /// Second node of the edge
kpeter@256
    88
    Node v(const Edge &e) const {
kpeter@256
    89
      return Parent::target(e);
kpeter@256
    90
    }
deba@57
    91
kpeter@256
    92
    /// Target of the given arc
deba@57
    93
    Node target(const Arc &e) const {
deba@57
    94
      return e.forward ? Parent::target(e) : Parent::source(e);
deba@57
    95
    }
deba@57
    96
deba@57
    97
    /// \brief Directed arc from an edge.
deba@57
    98
    ///
kpeter@256
    99
    /// Returns a directed arc corresponding to the specified edge.
kpeter@256
   100
    /// If the given bool is true, the first node of the given edge and
kpeter@256
   101
    /// the source node of the returned arc are the same.
kpeter@256
   102
    static Arc direct(const Edge &e, bool d) {
kpeter@256
   103
      return Arc(e, d);
deba@57
   104
    }
deba@57
   105
kpeter@256
   106
    /// Returns whether the given directed arc has the same orientation
kpeter@256
   107
    /// as the corresponding edge.
deba@57
   108
    ///
deba@57
   109
    /// \todo reference to the corresponding point of the undirected digraph
deba@57
   110
    /// concept. "What does the direction of an edge mean?"
kpeter@256
   111
    static bool direction(const Arc &a) { return a.forward; }
deba@57
   112
deba@57
   113
    using Parent::first;
deba@57
   114
    using Parent::next;
deba@57
   115
deba@57
   116
    void first(Arc &e) const {
deba@57
   117
      Parent::first(e);
deba@57
   118
      e.forward=true;
deba@57
   119
    }
deba@57
   120
deba@57
   121
    void next(Arc &e) const {
deba@57
   122
      if( e.forward ) {
alpar@209
   123
        e.forward = false;
deba@57
   124
      }
deba@57
   125
      else {
alpar@209
   126
        Parent::next(e);
alpar@209
   127
        e.forward = true;
deba@57
   128
      }
deba@57
   129
    }
deba@57
   130
deba@57
   131
    void firstOut(Arc &e, const Node &n) const {
deba@57
   132
      Parent::firstIn(e,n);
deba@57
   133
      if( Edge(e) != INVALID ) {
alpar@209
   134
        e.forward = false;
deba@57
   135
      }
deba@57
   136
      else {
alpar@209
   137
        Parent::firstOut(e,n);
alpar@209
   138
        e.forward = true;
deba@57
   139
      }
deba@57
   140
    }
deba@57
   141
    void nextOut(Arc &e) const {
deba@57
   142
      if( ! e.forward ) {
alpar@209
   143
        Node n = Parent::target(e);
alpar@209
   144
        Parent::nextIn(e);
alpar@209
   145
        if( Edge(e) == INVALID ) {
alpar@209
   146
          Parent::firstOut(e, n);
alpar@209
   147
          e.forward = true;
alpar@209
   148
        }
deba@57
   149
      }
deba@57
   150
      else {
alpar@209
   151
        Parent::nextOut(e);
deba@57
   152
      }
deba@57
   153
    }
deba@57
   154
deba@57
   155
    void firstIn(Arc &e, const Node &n) const {
deba@57
   156
      Parent::firstOut(e,n);
deba@57
   157
      if( Edge(e) != INVALID ) {
alpar@209
   158
        e.forward = false;
deba@57
   159
      }
deba@57
   160
      else {
alpar@209
   161
        Parent::firstIn(e,n);
alpar@209
   162
        e.forward = true;
deba@57
   163
      }
deba@57
   164
    }
deba@57
   165
    void nextIn(Arc &e) const {
deba@57
   166
      if( ! e.forward ) {
alpar@209
   167
        Node n = Parent::source(e);
alpar@209
   168
        Parent::nextOut(e);
alpar@209
   169
        if( Edge(e) == INVALID ) {
alpar@209
   170
          Parent::firstIn(e, n);
alpar@209
   171
          e.forward = true;
alpar@209
   172
        }
deba@57
   173
      }
deba@57
   174
      else {
alpar@209
   175
        Parent::nextIn(e);
deba@57
   176
      }
deba@57
   177
    }
deba@57
   178
deba@57
   179
    void firstInc(Edge &e, bool &d, const Node &n) const {
deba@57
   180
      d = true;
deba@57
   181
      Parent::firstOut(e, n);
deba@57
   182
      if (e != INVALID) return;
deba@57
   183
      d = false;
deba@57
   184
      Parent::firstIn(e, n);
deba@57
   185
    }
deba@57
   186
deba@57
   187
    void nextInc(Edge &e, bool &d) const {
deba@57
   188
      if (d) {
alpar@209
   189
        Node s = Parent::source(e);
alpar@209
   190
        Parent::nextOut(e);
alpar@209
   191
        if (e != INVALID) return;
alpar@209
   192
        d = false;
alpar@209
   193
        Parent::firstIn(e, s);
deba@57
   194
      } else {
alpar@209
   195
        Parent::nextIn(e);
deba@57
   196
      }
deba@57
   197
    }
deba@57
   198
deba@57
   199
    Node nodeFromId(int ix) const {
deba@57
   200
      return Parent::nodeFromId(ix);
deba@57
   201
    }
deba@57
   202
deba@57
   203
    Arc arcFromId(int ix) const {
deba@57
   204
      return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
deba@57
   205
    }
deba@57
   206
deba@57
   207
    Edge edgeFromId(int ix) const {
deba@57
   208
      return Parent::arcFromId(ix);
deba@57
   209
    }
deba@57
   210
deba@57
   211
    int id(const Node &n) const {
deba@57
   212
      return Parent::id(n);
deba@57
   213
    }
deba@57
   214
deba@57
   215
    int id(const Edge &e) const {
deba@57
   216
      return Parent::id(e);
deba@57
   217
    }
deba@57
   218
deba@57
   219
    int id(const Arc &e) const {
deba@57
   220
      return 2 * Parent::id(e) + int(e.forward);
deba@57
   221
    }
deba@57
   222
deba@57
   223
    int maxNodeId() const {
deba@57
   224
      return Parent::maxNodeId();
deba@57
   225
    }
deba@57
   226
deba@57
   227
    int maxArcId() const {
deba@57
   228
      return 2 * Parent::maxArcId() + 1;
deba@57
   229
    }
deba@57
   230
deba@57
   231
    int maxEdgeId() const {
deba@57
   232
      return Parent::maxArcId();
deba@57
   233
    }
deba@57
   234
deba@57
   235
    int arcNum() const {
deba@57
   236
      return 2 * Parent::arcNum();
deba@57
   237
    }
deba@57
   238
deba@57
   239
    int edgeNum() const {
deba@57
   240
      return Parent::arcNum();
deba@57
   241
    }
deba@57
   242
deba@57
   243
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
deba@57
   244
      if (p == INVALID) {
alpar@209
   245
        Edge arc = Parent::findArc(s, t);
alpar@209
   246
        if (arc != INVALID) return direct(arc, true);
alpar@209
   247
        arc = Parent::findArc(t, s);
alpar@209
   248
        if (arc != INVALID) return direct(arc, false);
deba@57
   249
      } else if (direction(p)) {
alpar@209
   250
        Edge arc = Parent::findArc(s, t, p);
alpar@209
   251
        if (arc != INVALID) return direct(arc, true);
alpar@209
   252
        arc = Parent::findArc(t, s);
alpar@209
   253
        if (arc != INVALID) return direct(arc, false);
deba@57
   254
      } else {
alpar@209
   255
        Edge arc = Parent::findArc(t, s, p);
alpar@209
   256
        if (arc != INVALID) return direct(arc, false);
deba@57
   257
      }
deba@57
   258
      return INVALID;
deba@57
   259
    }
deba@57
   260
deba@57
   261
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
deba@57
   262
      if (s != t) {
deba@57
   263
        if (p == INVALID) {
deba@57
   264
          Edge arc = Parent::findArc(s, t);
deba@57
   265
          if (arc != INVALID) return arc;
deba@57
   266
          arc = Parent::findArc(t, s);
deba@57
   267
          if (arc != INVALID) return arc;
deba@57
   268
        } else if (Parent::s(p) == s) {
deba@57
   269
          Edge arc = Parent::findArc(s, t, p);
deba@57
   270
          if (arc != INVALID) return arc;
deba@57
   271
          arc = Parent::findArc(t, s);
alpar@209
   272
          if (arc != INVALID) return arc;
deba@57
   273
        } else {
deba@57
   274
          Edge arc = Parent::findArc(t, s, p);
alpar@209
   275
          if (arc != INVALID) return arc;
deba@57
   276
        }
deba@57
   277
      } else {
deba@57
   278
        return Parent::findArc(s, t, p);
deba@57
   279
      }
deba@57
   280
      return INVALID;
deba@57
   281
    }
deba@57
   282
  };
deba@57
   283
deba@57
   284
  template <typename Base>
deba@57
   285
  class BidirBpGraphExtender : public Base {
deba@57
   286
  public:
deba@57
   287
    typedef Base Parent;
deba@57
   288
    typedef BidirBpGraphExtender Digraph;
deba@57
   289
deba@57
   290
    typedef typename Parent::Node Node;
deba@57
   291
    typedef typename Parent::Edge Edge;
deba@57
   292
deba@57
   293
deba@57
   294
    using Parent::first;
deba@57
   295
    using Parent::next;
deba@57
   296
deba@57
   297
    using Parent::id;
deba@57
   298
deba@57
   299
    class Red : public Node {
deba@57
   300
      friend class BidirBpGraphExtender;
deba@57
   301
    public:
deba@57
   302
      Red() {}
deba@57
   303
      Red(const Node& node) : Node(node) {
alpar@209
   304
        LEMON_ASSERT(Parent::red(node) || node == INVALID,
alpar@209
   305
                     typename Parent::NodeSetError());
deba@57
   306
      }
deba@57
   307
      Red& operator=(const Node& node) {
alpar@209
   308
        LEMON_ASSERT(Parent::red(node) || node == INVALID,
alpar@209
   309
                     typename Parent::NodeSetError());
deba@57
   310
        Node::operator=(node);
deba@57
   311
        return *this;
deba@57
   312
      }
deba@57
   313
      Red(Invalid) : Node(INVALID) {}
deba@57
   314
      Red& operator=(Invalid) {
deba@57
   315
        Node::operator=(INVALID);
deba@57
   316
        return *this;
deba@57
   317
      }
deba@57
   318
    };
deba@57
   319
deba@57
   320
    void first(Red& node) const {
deba@57
   321
      Parent::firstRed(static_cast<Node&>(node));
deba@57
   322
    }
deba@57
   323
    void next(Red& node) const {
deba@57
   324
      Parent::nextRed(static_cast<Node&>(node));
deba@57
   325
    }
deba@57
   326
deba@57
   327
    int id(const Red& node) const {
deba@57
   328
      return Parent::redId(node);
deba@57
   329
    }
deba@57
   330
deba@57
   331
    class Blue : public Node {
deba@57
   332
      friend class BidirBpGraphExtender;
deba@57
   333
    public:
deba@57
   334
      Blue() {}
deba@57
   335
      Blue(const Node& node) : Node(node) {
alpar@209
   336
        LEMON_ASSERT(Parent::blue(node) || node == INVALID,
alpar@209
   337
                     typename Parent::NodeSetError());
deba@57
   338
      }
deba@57
   339
      Blue& operator=(const Node& node) {
alpar@209
   340
        LEMON_ASSERT(Parent::blue(node) || node == INVALID,
alpar@209
   341
                     typename Parent::NodeSetError());
deba@57
   342
        Node::operator=(node);
deba@57
   343
        return *this;
deba@57
   344
      }
deba@57
   345
      Blue(Invalid) : Node(INVALID) {}
deba@57
   346
      Blue& operator=(Invalid) {
deba@57
   347
        Node::operator=(INVALID);
deba@57
   348
        return *this;
deba@57
   349
      }
deba@57
   350
    };
deba@57
   351
deba@57
   352
    void first(Blue& node) const {
deba@57
   353
      Parent::firstBlue(static_cast<Node&>(node));
deba@57
   354
    }
deba@57
   355
    void next(Blue& node) const {
deba@57
   356
      Parent::nextBlue(static_cast<Node&>(node));
deba@57
   357
    }
alpar@209
   358
deba@57
   359
    int id(const Blue& node) const {
deba@57
   360
      return Parent::redId(node);
deba@57
   361
    }
deba@57
   362
deba@57
   363
    Node source(const Edge& arc) const {
deba@57
   364
      return red(arc);
deba@57
   365
    }
deba@57
   366
    Node target(const Edge& arc) const {
deba@57
   367
      return blue(arc);
deba@57
   368
    }
deba@57
   369
deba@57
   370
    void firstInc(Edge& arc, bool& dir, const Node& node) const {
deba@57
   371
      if (Parent::red(node)) {
alpar@209
   372
        Parent::firstFromRed(arc, node);
alpar@209
   373
        dir = true;
deba@57
   374
      } else {
alpar@209
   375
        Parent::firstFromBlue(arc, node);
alpar@209
   376
        dir = static_cast<Edge&>(arc) == INVALID;
deba@57
   377
      }
deba@57
   378
    }
deba@57
   379
    void nextInc(Edge& arc, bool& dir) const {
deba@57
   380
      if (dir) {
alpar@209
   381
        Parent::nextFromRed(arc);
deba@57
   382
      } else {
alpar@209
   383
        Parent::nextFromBlue(arc);
alpar@209
   384
        if (arc == INVALID) dir = true;
deba@57
   385
      }
deba@57
   386
    }
deba@57
   387
deba@57
   388
    class Arc : public Edge {
deba@57
   389
      friend class BidirBpGraphExtender;
deba@57
   390
    protected:
deba@57
   391
      bool forward;
deba@57
   392
deba@57
   393
      Arc(const Edge& arc, bool _forward)
alpar@209
   394
        : Edge(arc), forward(_forward) {}
deba@57
   395
deba@57
   396
    public:
deba@57
   397
      Arc() {}
deba@57
   398
      Arc (Invalid) : Edge(INVALID), forward(true) {}
deba@57
   399
      bool operator==(const Arc& i) const {
alpar@209
   400
        return Edge::operator==(i) && forward == i.forward;
deba@57
   401
      }
deba@57
   402
      bool operator!=(const Arc& i) const {
alpar@209
   403
        return Edge::operator!=(i) || forward != i.forward;
deba@57
   404
      }
deba@57
   405
      bool operator<(const Arc& i) const {
alpar@209
   406
        return Edge::operator<(i) ||
alpar@209
   407
          (!(i.forward<forward) && Edge(*this)<Edge(i));
deba@57
   408
      }
deba@57
   409
    };
deba@57
   410
deba@57
   411
    void first(Arc& arc) const {
deba@57
   412
      Parent::first(static_cast<Edge&>(arc));
deba@57
   413
      arc.forward = true;
deba@57
   414
    }
deba@57
   415
deba@57
   416
    void next(Arc& arc) const {
deba@57
   417
      if (!arc.forward) {
alpar@209
   418
        Parent::next(static_cast<Edge&>(arc));
deba@57
   419
      }
deba@57
   420
      arc.forward = !arc.forward;
deba@57
   421
    }
deba@57
   422
deba@57
   423
    void firstOut(Arc& arc, const Node& node) const {
deba@57
   424
      if (Parent::red(node)) {
alpar@209
   425
        Parent::firstFromRed(arc, node);
alpar@209
   426
        arc.forward = true;
deba@57
   427
      } else {
alpar@209
   428
        Parent::firstFromBlue(arc, node);
alpar@209
   429
        arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57
   430
      }
deba@57
   431
    }
deba@57
   432
    void nextOut(Arc& arc) const {
deba@57
   433
      if (arc.forward) {
alpar@209
   434
        Parent::nextFromRed(arc);
deba@57
   435
      } else {
alpar@209
   436
        Parent::nextFromBlue(arc);
deba@57
   437
        arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57
   438
      }
deba@57
   439
    }
deba@57
   440
deba@57
   441
    void firstIn(Arc& arc, const Node& node) const {
deba@57
   442
      if (Parent::blue(node)) {
alpar@209
   443
        Parent::firstFromBlue(arc, node);
alpar@209
   444
        arc.forward = true;
deba@57
   445
      } else {
alpar@209
   446
        Parent::firstFromRed(arc, node);
alpar@209
   447
        arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57
   448
      }
deba@57
   449
    }
deba@57
   450
    void nextIn(Arc& arc) const {
deba@57
   451
      if (arc.forward) {
alpar@209
   452
        Parent::nextFromBlue(arc);
deba@57
   453
      } else {
alpar@209
   454
        Parent::nextFromRed(arc);
alpar@209
   455
        arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57
   456
      }
deba@57
   457
    }
deba@57
   458
deba@57
   459
    Node source(const Arc& arc) const {
deba@57
   460
      return arc.forward ? Parent::red(arc) : Parent::blue(arc);
deba@57
   461
    }
deba@57
   462
    Node target(const Arc& arc) const {
deba@57
   463
      return arc.forward ? Parent::blue(arc) : Parent::red(arc);
deba@57
   464
    }
deba@57
   465
deba@57
   466
    int id(const Arc& arc) const {
alpar@209
   467
      return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
deba@57
   468
        (arc.forward ? 0 : 1);
deba@57
   469
    }
deba@57
   470
    Arc arcFromId(int ix) const {
deba@57
   471
      return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
deba@57
   472
    }
deba@57
   473
    int maxArcId() const {
deba@57
   474
      return (Parent::maxEdgeId() << 1) + 1;
deba@57
   475
    }
deba@57
   476
deba@57
   477
    bool direction(const Arc& arc) const {
deba@57
   478
      return arc.forward;
deba@57
   479
    }
deba@57
   480
deba@57
   481
    Arc direct(const Edge& arc, bool dir) const {
deba@57
   482
      return Arc(arc, dir);
deba@57
   483
    }
deba@57
   484
deba@57
   485
    int arcNum() const {
deba@57
   486
      return 2 * Parent::edgeNum();
deba@57
   487
    }
deba@57
   488
deba@57
   489
    int edgeNum() const {
deba@57
   490
      return Parent::edgeNum();
deba@57
   491
    }
deba@57
   492
deba@57
   493
deba@57
   494
  };
deba@57
   495
}
deba@57
   496
deba@57
   497
#endif