lemon/edge_set.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 29 Apr 2009 15:52:08 +0100
changeset 632 0286cb1ac78d
parent 559 c5fd2d996909
child 660 d9cf3b5858ae
permissions -rw-r--r--
Merge
deba@468
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@468
     2
 *
deba@468
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@468
     4
 *
deba@468
     5
 * Copyright (C) 2003-2008
deba@468
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@468
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@468
     8
 *
deba@468
     9
 * Permission to use, modify and distribute this software is granted
deba@468
    10
 * provided that this copyright notice appears in all copies. For
deba@468
    11
 * precise terms see the accompanying LICENSE file.
deba@468
    12
 *
deba@468
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@468
    14
 * express or implied, and with no claim as to its suitability for any
deba@468
    15
 * purpose.
deba@468
    16
 *
deba@468
    17
 */
deba@468
    18
deba@468
    19
#ifndef LEMON_EDGE_SET_H
deba@468
    20
#define LEMON_EDGE_SET_H
deba@468
    21
deba@468
    22
#include <lemon/core.h>
deba@468
    23
#include <lemon/bits/edge_set_extender.h>
deba@468
    24
deba@468
    25
/// \ingroup semi_adaptors
deba@468
    26
/// \file
deba@468
    27
/// \brief ArcSet and EdgeSet classes.
deba@468
    28
///
deba@468
    29
/// Graphs which use another graph's node-set as own.
deba@468
    30
namespace lemon {
deba@468
    31
deba@488
    32
  template <typename GR>
deba@468
    33
  class ListArcSetBase {
deba@468
    34
  public:
deba@468
    35
deba@488
    36
    typedef typename GR::Node Node;
deba@488
    37
    typedef typename GR::NodeIt NodeIt;
deba@468
    38
deba@468
    39
  protected:
deba@468
    40
deba@468
    41
    struct NodeT {
deba@468
    42
      int first_out, first_in;
deba@468
    43
      NodeT() : first_out(-1), first_in(-1) {}
deba@468
    44
    };
deba@468
    45
deba@488
    46
    typedef typename ItemSetTraits<GR, Node>::
deba@468
    47
    template Map<NodeT>::Type NodesImplBase;
deba@468
    48
deba@488
    49
    NodesImplBase* _nodes;
deba@468
    50
deba@468
    51
    struct ArcT {
deba@468
    52
      Node source, target;
deba@468
    53
      int next_out, next_in;
deba@468
    54
      int prev_out, prev_in;
deba@468
    55
      ArcT() : prev_out(-1), prev_in(-1) {}
deba@468
    56
    };
deba@468
    57
deba@468
    58
    std::vector<ArcT> arcs;
deba@468
    59
deba@468
    60
    int first_arc;
deba@468
    61
    int first_free_arc;
deba@468
    62
deba@488
    63
    const GR* _graph;
deba@468
    64
deba@488
    65
    void initalize(const GR& graph, NodesImplBase& nodes) {
deba@488
    66
      _graph = &graph;
deba@488
    67
      _nodes = &nodes;
deba@468
    68
    }
deba@468
    69
deba@468
    70
  public:
deba@468
    71
deba@468
    72
    class Arc {
deba@488
    73
      friend class ListArcSetBase<GR>;
deba@468
    74
    protected:
deba@468
    75
      Arc(int _id) : id(_id) {}
deba@468
    76
      int id;
deba@468
    77
    public:
deba@468
    78
      Arc() {}
deba@468
    79
      Arc(Invalid) : id(-1) {}
deba@468
    80
      bool operator==(const Arc& arc) const { return id == arc.id; }
deba@468
    81
      bool operator!=(const Arc& arc) const { return id != arc.id; }
deba@468
    82
      bool operator<(const Arc& arc) const { return id < arc.id; }
deba@468
    83
    };
deba@468
    84
deba@468
    85
    ListArcSetBase() : first_arc(-1), first_free_arc(-1) {}
deba@468
    86
deba@468
    87
    Arc addArc(const Node& u, const Node& v) {
deba@468
    88
      int n;
deba@468
    89
      if (first_free_arc == -1) {
deba@468
    90
        n = arcs.size();
deba@468
    91
        arcs.push_back(ArcT());
deba@468
    92
      } else {
deba@468
    93
        n = first_free_arc;
deba@468
    94
        first_free_arc = arcs[first_free_arc].next_in;
deba@468
    95
      }
deba@488
    96
      arcs[n].next_in = (*_nodes)[v].first_in;
deba@488
    97
      if ((*_nodes)[v].first_in != -1) {
deba@488
    98
        arcs[(*_nodes)[v].first_in].prev_in = n;
deba@468
    99
      }
deba@488
   100
      (*_nodes)[v].first_in = n;
deba@488
   101
      arcs[n].next_out = (*_nodes)[u].first_out;
deba@488
   102
      if ((*_nodes)[u].first_out != -1) {
deba@488
   103
        arcs[(*_nodes)[u].first_out].prev_out = n;
deba@468
   104
      }
deba@488
   105
      (*_nodes)[u].first_out = n;
deba@468
   106
      arcs[n].source = u;
deba@468
   107
      arcs[n].target = v;
deba@468
   108
      return Arc(n);
deba@468
   109
    }
deba@468
   110
deba@468
   111
    void erase(const Arc& arc) {
deba@468
   112
      int n = arc.id;
deba@468
   113
      if (arcs[n].prev_in != -1) {
deba@468
   114
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
deba@468
   115
      } else {
deba@488
   116
        (*_nodes)[arcs[n].target].first_in = arcs[n].next_in;
deba@468
   117
      }
deba@468
   118
      if (arcs[n].next_in != -1) {
deba@468
   119
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
deba@468
   120
      }
deba@468
   121
deba@468
   122
      if (arcs[n].prev_out != -1) {
deba@468
   123
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
deba@468
   124
      } else {
deba@488
   125
        (*_nodes)[arcs[n].source].first_out = arcs[n].next_out;
deba@468
   126
      }
deba@468
   127
      if (arcs[n].next_out != -1) {
deba@468
   128
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
deba@468
   129
      }
deba@468
   130
deba@468
   131
    }
deba@468
   132
deba@468
   133
    void clear() {
deba@468
   134
      Node node;
deba@468
   135
      for (first(node); node != INVALID; next(node)) {
deba@488
   136
        (*_nodes)[node].first_in = -1;
deba@488
   137
        (*_nodes)[node].first_out = -1;
deba@468
   138
      }
deba@468
   139
      arcs.clear();
deba@468
   140
      first_arc = -1;
deba@468
   141
      first_free_arc = -1;
deba@468
   142
    }
deba@468
   143
deba@468
   144
    void first(Node& node) const {
deba@488
   145
      _graph->first(node);
deba@468
   146
    }
deba@468
   147
deba@468
   148
    void next(Node& node) const {
deba@488
   149
      _graph->next(node);
deba@468
   150
    }
deba@468
   151
deba@468
   152
    void first(Arc& arc) const {
deba@468
   153
      Node node;
deba@468
   154
      first(node);
deba@488
   155
      while (node != INVALID && (*_nodes)[node].first_in == -1) {
deba@468
   156
        next(node);
deba@468
   157
      }
deba@488
   158
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
deba@468
   159
    }
deba@468
   160
deba@468
   161
    void next(Arc& arc) const {
deba@468
   162
      if (arcs[arc.id].next_in != -1) {
deba@468
   163
        arc.id = arcs[arc.id].next_in;
deba@468
   164
      } else {
deba@468
   165
        Node node = arcs[arc.id].target;
deba@468
   166
        next(node);
deba@488
   167
        while (node != INVALID && (*_nodes)[node].first_in == -1) {
deba@468
   168
          next(node);
deba@468
   169
        }
deba@488
   170
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
deba@468
   171
      }
deba@468
   172
    }
deba@468
   173
deba@468
   174
    void firstOut(Arc& arc, const Node& node) const {
deba@488
   175
      arc.id = (*_nodes)[node].first_out;
deba@468
   176
    }
deba@468
   177
deba@468
   178
    void nextOut(Arc& arc) const {
deba@468
   179
      arc.id = arcs[arc.id].next_out;
deba@468
   180
    }
deba@468
   181
deba@468
   182
    void firstIn(Arc& arc, const Node& node) const {
deba@488
   183
      arc.id = (*_nodes)[node].first_in;
deba@468
   184
    }
deba@468
   185
deba@468
   186
    void nextIn(Arc& arc) const {
deba@468
   187
      arc.id = arcs[arc.id].next_in;
deba@468
   188
    }
deba@468
   189
deba@488
   190
    int id(const Node& node) const { return _graph->id(node); }
deba@468
   191
    int id(const Arc& arc) const { return arc.id; }
deba@468
   192
deba@488
   193
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
deba@468
   194
    Arc arcFromId(int ix) const { return Arc(ix); }
deba@468
   195
deba@488
   196
    int maxNodeId() const { return _graph->maxNodeId(); };
deba@468
   197
    int maxArcId() const { return arcs.size() - 1; }
deba@468
   198
deba@468
   199
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
deba@468
   200
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
deba@468
   201
deba@488
   202
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
deba@468
   203
deba@468
   204
    NodeNotifier& notifier(Node) const {
deba@488
   205
      return _graph->notifier(Node());
deba@468
   206
    }
deba@468
   207
deba@488
   208
    template <typename V>
deba@488
   209
    class NodeMap : public GR::template NodeMap<V> {
kpeter@617
   210
      typedef typename GR::template NodeMap<V> Parent;
kpeter@617
   211
deba@468
   212
    public:
deba@468
   213
deba@488
   214
      explicit NodeMap(const ListArcSetBase<GR>& arcset)
deba@488
   215
        : Parent(*arcset._graph) {}
deba@468
   216
deba@488
   217
      NodeMap(const ListArcSetBase<GR>& arcset, const V& value)
deba@488
   218
        : Parent(*arcset._graph, value) {}
deba@468
   219
deba@468
   220
      NodeMap& operator=(const NodeMap& cmap) {
deba@468
   221
        return operator=<NodeMap>(cmap);
deba@468
   222
      }
deba@468
   223
deba@468
   224
      template <typename CMap>
deba@468
   225
      NodeMap& operator=(const CMap& cmap) {
deba@468
   226
        Parent::operator=(cmap);
deba@468
   227
        return *this;
deba@468
   228
      }
deba@468
   229
    };
deba@468
   230
deba@468
   231
  };
deba@468
   232
deba@468
   233
  /// \ingroup semi_adaptors
deba@468
   234
  ///
deba@468
   235
  /// \brief Digraph using a node set of another digraph or graph and
deba@468
   236
  /// an own arc set.
deba@468
   237
  ///
deba@468
   238
  /// This structure can be used to establish another directed graph
deba@468
   239
  /// over a node set of an existing one. This class uses the same
deba@468
   240
  /// Node type as the underlying graph, and each valid node of the
deba@468
   241
  /// original graph is valid in this arc set, therefore the node
deba@468
   242
  /// objects of the original graph can be used directly with this
deba@468
   243
  /// class. The node handling functions (id handling, observing, and
deba@468
   244
  /// iterators) works equivalently as in the original graph.
deba@468
   245
  ///
deba@468
   246
  /// This implementation is based on doubly-linked lists, from each
deba@468
   247
  /// node the outgoing and the incoming arcs make up lists, therefore
deba@468
   248
  /// one arc can be erased in constant time. It also makes possible,
deba@468
   249
  /// that node can be removed from the underlying graph, in this case
deba@468
   250
  /// all arcs incident to the given node is erased from the arc set.
deba@468
   251
  ///
deba@488
   252
  /// \param GR The type of the graph which shares its node set with
deba@468
   253
  /// this class. Its interface must conform to the
deba@468
   254
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
deba@468
   255
  /// concept.
deba@468
   256
  ///
kpeter@559
   257
  /// This class fully conforms to the \ref concepts::Digraph
deba@468
   258
  /// "Digraph" concept.
deba@488
   259
  template <typename GR>
deba@488
   260
  class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > {
kpeter@617
   261
    typedef ArcSetExtender<ListArcSetBase<GR> > Parent;
deba@468
   262
deba@468
   263
  public:
deba@468
   264
deba@468
   265
    typedef typename Parent::Node Node;
deba@468
   266
    typedef typename Parent::Arc Arc;
deba@468
   267
deba@468
   268
    typedef typename Parent::NodesImplBase NodesImplBase;
deba@468
   269
deba@468
   270
    void eraseNode(const Node& node) {
deba@468
   271
      Arc arc;
deba@468
   272
      Parent::firstOut(arc, node);
deba@468
   273
      while (arc != INVALID ) {
deba@468
   274
        erase(arc);
deba@468
   275
        Parent::firstOut(arc, node);
deba@468
   276
      }
deba@468
   277
deba@468
   278
      Parent::firstIn(arc, node);
deba@468
   279
      while (arc != INVALID ) {
deba@468
   280
        erase(arc);
deba@468
   281
        Parent::firstIn(arc, node);
deba@468
   282
      }
deba@468
   283
    }
deba@468
   284
deba@468
   285
    void clearNodes() {
deba@468
   286
      Parent::clear();
deba@468
   287
    }
deba@468
   288
deba@468
   289
    class NodesImpl : public NodesImplBase {
deba@468
   290
      typedef NodesImplBase Parent;
deba@468
   291
kpeter@617
   292
    public:
deba@488
   293
      NodesImpl(const GR& graph, ListArcSet& arcset)
deba@468
   294
        : Parent(graph), _arcset(arcset) {}
deba@468
   295
deba@468
   296
      virtual ~NodesImpl() {}
deba@468
   297
deba@468
   298
    protected:
deba@468
   299
deba@468
   300
      virtual void erase(const Node& node) {
deba@468
   301
        _arcset.eraseNode(node);
deba@468
   302
        Parent::erase(node);
deba@468
   303
      }
deba@468
   304
      virtual void erase(const std::vector<Node>& nodes) {
deba@468
   305
        for (int i = 0; i < int(nodes.size()); ++i) {
deba@468
   306
          _arcset.eraseNode(nodes[i]);
deba@468
   307
        }
deba@468
   308
        Parent::erase(nodes);
deba@468
   309
      }
deba@468
   310
      virtual void clear() {
deba@468
   311
        _arcset.clearNodes();
deba@468
   312
        Parent::clear();
deba@468
   313
      }
deba@468
   314
deba@468
   315
    private:
deba@468
   316
      ListArcSet& _arcset;
deba@468
   317
    };
deba@468
   318
deba@488
   319
    NodesImpl _nodes;
deba@468
   320
deba@468
   321
  public:
deba@468
   322
deba@468
   323
    /// \brief Constructor of the ArcSet.
deba@468
   324
    ///
deba@468
   325
    /// Constructor of the ArcSet.
deba@488
   326
    ListArcSet(const GR& graph) : _nodes(graph, *this) {
deba@488
   327
      Parent::initalize(graph, _nodes);
deba@468
   328
    }
deba@468
   329
deba@468
   330
    /// \brief Add a new arc to the digraph.
deba@468
   331
    ///
deba@468
   332
    /// Add a new arc to the digraph with source node \c s
deba@468
   333
    /// and target node \c t.
kpeter@559
   334
    /// \return The new arc.
deba@468
   335
    Arc addArc(const Node& s, const Node& t) {
deba@468
   336
      return Parent::addArc(s, t);
deba@468
   337
    }
deba@468
   338
deba@468
   339
    /// \brief Erase an arc from the digraph.
deba@468
   340
    ///
deba@468
   341
    /// Erase an arc \c a from the digraph.
deba@468
   342
    void erase(const Arc& a) {
deba@468
   343
      return Parent::erase(a);
deba@468
   344
    }
deba@468
   345
deba@468
   346
  };
deba@468
   347
deba@488
   348
  template <typename GR>
deba@468
   349
  class ListEdgeSetBase {
deba@468
   350
  public:
deba@468
   351
deba@488
   352
    typedef typename GR::Node Node;
deba@488
   353
    typedef typename GR::NodeIt NodeIt;
deba@468
   354
deba@468
   355
  protected:
deba@468
   356
deba@468
   357
    struct NodeT {
deba@468
   358
      int first_out;
deba@468
   359
      NodeT() : first_out(-1) {}
deba@468
   360
    };
deba@468
   361
deba@488
   362
    typedef typename ItemSetTraits<GR, Node>::
deba@468
   363
    template Map<NodeT>::Type NodesImplBase;
deba@468
   364
deba@488
   365
    NodesImplBase* _nodes;
deba@468
   366
deba@468
   367
    struct ArcT {
deba@468
   368
      Node target;
deba@468
   369
      int prev_out, next_out;
deba@468
   370
      ArcT() : prev_out(-1), next_out(-1) {}
deba@468
   371
    };
deba@468
   372
deba@468
   373
    std::vector<ArcT> arcs;
deba@468
   374
deba@468
   375
    int first_arc;
deba@468
   376
    int first_free_arc;
deba@468
   377
deba@488
   378
    const GR* _graph;
deba@468
   379
deba@488
   380
    void initalize(const GR& graph, NodesImplBase& nodes) {
deba@488
   381
      _graph = &graph;
deba@488
   382
      _nodes = &nodes;
deba@468
   383
    }
deba@468
   384
deba@468
   385
  public:
deba@468
   386
deba@468
   387
    class Edge {
deba@468
   388
      friend class ListEdgeSetBase;
deba@468
   389
    protected:
deba@468
   390
deba@468
   391
      int id;
deba@468
   392
      explicit Edge(int _id) { id = _id;}
deba@468
   393
deba@468
   394
    public:
deba@468
   395
      Edge() {}
deba@468
   396
      Edge (Invalid) { id = -1; }
deba@468
   397
      bool operator==(const Edge& arc) const {return id == arc.id;}
deba@468
   398
      bool operator!=(const Edge& arc) const {return id != arc.id;}
deba@468
   399
      bool operator<(const Edge& arc) const {return id < arc.id;}
deba@468
   400
    };
deba@468
   401
deba@468
   402
    class Arc {
deba@468
   403
      friend class ListEdgeSetBase;
deba@468
   404
    protected:
deba@468
   405
      Arc(int _id) : id(_id) {}
deba@468
   406
      int id;
deba@468
   407
    public:
deba@468
   408
      operator Edge() const { return edgeFromId(id / 2); }
deba@468
   409
deba@468
   410
      Arc() {}
deba@468
   411
      Arc(Invalid) : id(-1) {}
deba@468
   412
      bool operator==(const Arc& arc) const { return id == arc.id; }
deba@468
   413
      bool operator!=(const Arc& arc) const { return id != arc.id; }
deba@468
   414
      bool operator<(const Arc& arc) const { return id < arc.id; }
deba@468
   415
    };
deba@468
   416
deba@468
   417
    ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {}
deba@468
   418
deba@468
   419
    Edge addEdge(const Node& u, const Node& v) {
deba@468
   420
      int n;
deba@468
   421
deba@468
   422
      if (first_free_arc == -1) {
deba@468
   423
        n = arcs.size();
deba@468
   424
        arcs.push_back(ArcT());
deba@468
   425
        arcs.push_back(ArcT());
deba@468
   426
      } else {
deba@468
   427
        n = first_free_arc;
deba@468
   428
        first_free_arc = arcs[n].next_out;
deba@468
   429
      }
deba@468
   430
deba@468
   431
      arcs[n].target = u;
deba@468
   432
      arcs[n | 1].target = v;
deba@468
   433
deba@488
   434
      arcs[n].next_out = (*_nodes)[v].first_out;
deba@488
   435
      if ((*_nodes)[v].first_out != -1) {
deba@488
   436
        arcs[(*_nodes)[v].first_out].prev_out = n;
deba@468
   437
      }
deba@488
   438
      (*_nodes)[v].first_out = n;
deba@468
   439
      arcs[n].prev_out = -1;
deba@468
   440
deba@488
   441
      if ((*_nodes)[u].first_out != -1) {
deba@488
   442
        arcs[(*_nodes)[u].first_out].prev_out = (n | 1);
deba@468
   443
      }
deba@488
   444
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
deba@488
   445
      (*_nodes)[u].first_out = (n | 1);
deba@468
   446
      arcs[n | 1].prev_out = -1;
deba@468
   447
deba@468
   448
      return Edge(n / 2);
deba@468
   449
    }
deba@468
   450
deba@468
   451
    void erase(const Edge& arc) {
deba@468
   452
      int n = arc.id * 2;
deba@468
   453
deba@468
   454
      if (arcs[n].next_out != -1) {
deba@468
   455
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
deba@468
   456
      }
deba@468
   457
deba@468
   458
      if (arcs[n].prev_out != -1) {
deba@468
   459
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
deba@468
   460
      } else {
deba@488
   461
        (*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out;
deba@468
   462
      }
deba@468
   463
deba@468
   464
      if (arcs[n | 1].next_out != -1) {
deba@468
   465
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
deba@468
   466
      }
deba@468
   467
deba@468
   468
      if (arcs[n | 1].prev_out != -1) {
deba@468
   469
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
deba@468
   470
      } else {
deba@488
   471
        (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
deba@468
   472
      }
deba@468
   473
deba@468
   474
      arcs[n].next_out = first_free_arc;
deba@468
   475
      first_free_arc = n;
deba@468
   476
deba@468
   477
    }
deba@468
   478
deba@468
   479
    void clear() {
deba@468
   480
      Node node;
deba@468
   481
      for (first(node); node != INVALID; next(node)) {
deba@488
   482
        (*_nodes)[node].first_out = -1;
deba@468
   483
      }
deba@468
   484
      arcs.clear();
deba@468
   485
      first_arc = -1;
deba@468
   486
      first_free_arc = -1;
deba@468
   487
    }
deba@468
   488
deba@468
   489
    void first(Node& node) const {
deba@488
   490
      _graph->first(node);
deba@468
   491
    }
deba@468
   492
deba@468
   493
    void next(Node& node) const {
deba@488
   494
      _graph->next(node);
deba@468
   495
    }
deba@468
   496
deba@468
   497
    void first(Arc& arc) const {
deba@468
   498
      Node node;
deba@468
   499
      first(node);
deba@488
   500
      while (node != INVALID && (*_nodes)[node].first_out == -1) {
deba@468
   501
        next(node);
deba@468
   502
      }
deba@488
   503
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
deba@468
   504
    }
deba@468
   505
deba@468
   506
    void next(Arc& arc) const {
deba@468
   507
      if (arcs[arc.id].next_out != -1) {
deba@468
   508
        arc.id = arcs[arc.id].next_out;
deba@468
   509
      } else {
deba@468
   510
        Node node = arcs[arc.id ^ 1].target;
deba@468
   511
        next(node);
deba@488
   512
        while(node != INVALID && (*_nodes)[node].first_out == -1) {
deba@468
   513
          next(node);
deba@468
   514
        }
deba@488
   515
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
deba@468
   516
      }
deba@468
   517
    }
deba@468
   518
deba@468
   519
    void first(Edge& edge) const {
deba@468
   520
      Node node;
deba@468
   521
      first(node);
deba@468
   522
      while (node != INVALID) {
deba@488
   523
        edge.id = (*_nodes)[node].first_out;
deba@468
   524
        while ((edge.id & 1) != 1) {
deba@468
   525
          edge.id = arcs[edge.id].next_out;
deba@468
   526
        }
deba@468
   527
        if (edge.id != -1) {
deba@468
   528
          edge.id /= 2;
deba@468
   529
          return;
deba@468
   530
        }
deba@468
   531
        next(node);
deba@468
   532
      }
deba@468
   533
      edge.id = -1;
deba@468
   534
    }
deba@468
   535
deba@468
   536
    void next(Edge& edge) const {
deba@468
   537
      Node node = arcs[edge.id * 2].target;
deba@468
   538
      edge.id = arcs[(edge.id * 2) | 1].next_out;
deba@468
   539
      while ((edge.id & 1) != 1) {
deba@468
   540
        edge.id = arcs[edge.id].next_out;
deba@468
   541
      }
deba@468
   542
      if (edge.id != -1) {
deba@468
   543
        edge.id /= 2;
deba@468
   544
        return;
deba@468
   545
      }
deba@468
   546
      next(node);
deba@468
   547
      while (node != INVALID) {
deba@488
   548
        edge.id = (*_nodes)[node].first_out;
deba@468
   549
        while ((edge.id & 1) != 1) {
deba@468
   550
          edge.id = arcs[edge.id].next_out;
deba@468
   551
        }
deba@468
   552
        if (edge.id != -1) {
deba@468
   553
          edge.id /= 2;
deba@468
   554
          return;
deba@468
   555
        }
deba@468
   556
        next(node);
deba@468
   557
      }
deba@468
   558
      edge.id = -1;
deba@468
   559
    }
deba@468
   560
deba@468
   561
    void firstOut(Arc& arc, const Node& node) const {
deba@488
   562
      arc.id = (*_nodes)[node].first_out;
deba@468
   563
    }
deba@468
   564
deba@468
   565
    void nextOut(Arc& arc) const {
deba@468
   566
      arc.id = arcs[arc.id].next_out;
deba@468
   567
    }
deba@468
   568
deba@468
   569
    void firstIn(Arc& arc, const Node& node) const {
deba@488
   570
      arc.id = (((*_nodes)[node].first_out) ^ 1);
deba@468
   571
      if (arc.id == -2) arc.id = -1;
deba@468
   572
    }
deba@468
   573
deba@468
   574
    void nextIn(Arc& arc) const {
deba@468
   575
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
deba@468
   576
      if (arc.id == -2) arc.id = -1;
deba@468
   577
    }
deba@468
   578
deba@468
   579
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
deba@488
   580
      int de = (*_nodes)[node].first_out;
deba@468
   581
      if (de != -1 ) {
deba@468
   582
        arc.id = de / 2;
deba@468
   583
        dir = ((de & 1) == 1);
deba@468
   584
      } else {
deba@468
   585
        arc.id = -1;
deba@468
   586
        dir = true;
deba@468
   587
      }
deba@468
   588
    }
deba@468
   589
    void nextInc(Edge &arc, bool& dir) const {
deba@468
   590
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
deba@468
   591
      if (de != -1 ) {
deba@468
   592
        arc.id = de / 2;
deba@468
   593
        dir = ((de & 1) == 1);
deba@468
   594
      } else {
deba@468
   595
        arc.id = -1;
deba@468
   596
        dir = true;
deba@468
   597
      }
deba@468
   598
    }
deba@468
   599
deba@468
   600
    static bool direction(Arc arc) {
deba@468
   601
      return (arc.id & 1) == 1;
deba@468
   602
    }
deba@468
   603
deba@468
   604
    static Arc direct(Edge edge, bool dir) {
deba@468
   605
      return Arc(edge.id * 2 + (dir ? 1 : 0));
deba@468
   606
    }
deba@468
   607
deba@488
   608
    int id(const Node& node) const { return _graph->id(node); }
deba@468
   609
    static int id(Arc e) { return e.id; }
deba@468
   610
    static int id(Edge e) { return e.id; }
deba@468
   611
deba@488
   612
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
deba@468
   613
    static Arc arcFromId(int id) { return Arc(id);}
deba@468
   614
    static Edge edgeFromId(int id) { return Edge(id);}
deba@468
   615
deba@488
   616
    int maxNodeId() const { return _graph->maxNodeId(); };
deba@468
   617
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
deba@468
   618
    int maxArcId() const { return arcs.size()-1; }
deba@468
   619
deba@468
   620
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
deba@468
   621
    Node target(Arc e) const { return arcs[e.id].target; }
deba@468
   622
deba@468
   623
    Node u(Edge e) const { return arcs[2 * e.id].target; }
deba@468
   624
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
deba@468
   625
deba@488
   626
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
deba@468
   627
deba@468
   628
    NodeNotifier& notifier(Node) const {
deba@488
   629
      return _graph->notifier(Node());
deba@468
   630
    }
deba@468
   631
deba@488
   632
    template <typename V>
deba@488
   633
    class NodeMap : public GR::template NodeMap<V> {
kpeter@617
   634
      typedef typename GR::template NodeMap<V> Parent;
kpeter@617
   635
deba@468
   636
    public:
deba@468
   637
deba@488
   638
      explicit NodeMap(const ListEdgeSetBase<GR>& arcset)
deba@488
   639
        : Parent(*arcset._graph) {}
deba@468
   640
deba@488
   641
      NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value)
deba@488
   642
        : Parent(*arcset._graph, value) {}
deba@468
   643
deba@468
   644
      NodeMap& operator=(const NodeMap& cmap) {
deba@468
   645
        return operator=<NodeMap>(cmap);
deba@468
   646
      }
deba@468
   647
deba@468
   648
      template <typename CMap>
deba@468
   649
      NodeMap& operator=(const CMap& cmap) {
deba@468
   650
        Parent::operator=(cmap);
deba@468
   651
        return *this;
deba@468
   652
      }
deba@468
   653
    };
deba@468
   654
deba@468
   655
  };
deba@468
   656
deba@468
   657
  /// \ingroup semi_adaptors
deba@468
   658
  ///
deba@468
   659
  /// \brief Graph using a node set of another digraph or graph and an
deba@468
   660
  /// own edge set.
deba@468
   661
  ///
deba@468
   662
  /// This structure can be used to establish another graph over a
deba@468
   663
  /// node set of an existing one. This class uses the same Node type
deba@468
   664
  /// as the underlying graph, and each valid node of the original
deba@468
   665
  /// graph is valid in this arc set, therefore the node objects of
deba@468
   666
  /// the original graph can be used directly with this class. The
deba@468
   667
  /// node handling functions (id handling, observing, and iterators)
deba@468
   668
  /// works equivalently as in the original graph.
deba@468
   669
  ///
deba@468
   670
  /// This implementation is based on doubly-linked lists, from each
deba@468
   671
  /// node the incident edges make up lists, therefore one edge can be
deba@468
   672
  /// erased in constant time. It also makes possible, that node can
deba@468
   673
  /// be removed from the underlying graph, in this case all edges
deba@468
   674
  /// incident to the given node is erased from the arc set.
deba@468
   675
  ///
deba@488
   676
  /// \param GR The type of the graph which shares its node set
deba@468
   677
  /// with this class. Its interface must conform to the
deba@468
   678
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
deba@468
   679
  /// concept.
deba@468
   680
  ///
kpeter@559
   681
  /// This class fully conforms to the \ref concepts::Graph "Graph"
deba@468
   682
  /// concept.
deba@488
   683
  template <typename GR>
deba@488
   684
  class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > {
kpeter@617
   685
    typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent;
deba@468
   686
deba@468
   687
  public:
deba@468
   688
deba@468
   689
    typedef typename Parent::Node Node;
deba@468
   690
    typedef typename Parent::Arc Arc;
deba@468
   691
    typedef typename Parent::Edge Edge;
deba@468
   692
deba@468
   693
    typedef typename Parent::NodesImplBase NodesImplBase;
deba@468
   694
deba@468
   695
    void eraseNode(const Node& node) {
deba@468
   696
      Arc arc;
deba@468
   697
      Parent::firstOut(arc, node);
deba@468
   698
      while (arc != INVALID ) {
deba@468
   699
        erase(arc);
deba@468
   700
        Parent::firstOut(arc, node);
deba@468
   701
      }
deba@468
   702
deba@468
   703
    }
deba@468
   704
deba@468
   705
    void clearNodes() {
deba@468
   706
      Parent::clear();
deba@468
   707
    }
deba@468
   708
deba@468
   709
    class NodesImpl : public NodesImplBase {
deba@468
   710
      typedef NodesImplBase Parent;
deba@468
   711
kpeter@617
   712
    public:
deba@488
   713
      NodesImpl(const GR& graph, ListEdgeSet& arcset)
deba@468
   714
        : Parent(graph), _arcset(arcset) {}
deba@468
   715
deba@468
   716
      virtual ~NodesImpl() {}
deba@468
   717
deba@468
   718
    protected:
deba@468
   719
deba@468
   720
      virtual void erase(const Node& node) {
deba@468
   721
        _arcset.eraseNode(node);
deba@468
   722
        Parent::erase(node);
deba@468
   723
      }
deba@468
   724
      virtual void erase(const std::vector<Node>& nodes) {
deba@468
   725
        for (int i = 0; i < int(nodes.size()); ++i) {
deba@468
   726
          _arcset.eraseNode(nodes[i]);
deba@468
   727
        }
deba@468
   728
        Parent::erase(nodes);
deba@468
   729
      }
deba@468
   730
      virtual void clear() {
deba@468
   731
        _arcset.clearNodes();
deba@468
   732
        Parent::clear();
deba@468
   733
      }
deba@468
   734
deba@468
   735
    private:
deba@468
   736
      ListEdgeSet& _arcset;
deba@468
   737
    };
deba@468
   738
deba@488
   739
    NodesImpl _nodes;
deba@468
   740
deba@468
   741
  public:
deba@468
   742
deba@468
   743
    /// \brief Constructor of the EdgeSet.
deba@468
   744
    ///
deba@468
   745
    /// Constructor of the EdgeSet.
deba@488
   746
    ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
deba@488
   747
      Parent::initalize(graph, _nodes);
deba@468
   748
    }
deba@468
   749
deba@468
   750
    /// \brief Add a new edge to the graph.
deba@468
   751
    ///
deba@468
   752
    /// Add a new edge to the graph with node \c u
deba@468
   753
    /// and node \c v endpoints.
kpeter@559
   754
    /// \return The new edge.
deba@468
   755
    Edge addEdge(const Node& u, const Node& v) {
deba@468
   756
      return Parent::addEdge(u, v);
deba@468
   757
    }
deba@468
   758
deba@468
   759
    /// \brief Erase an edge from the graph.
deba@468
   760
    ///
deba@468
   761
    /// Erase the edge \c e from the graph.
deba@468
   762
    void erase(const Edge& e) {
deba@468
   763
      return Parent::erase(e);
deba@468
   764
    }
deba@468
   765
deba@468
   766
  };
deba@468
   767
deba@488
   768
  template <typename GR>
deba@468
   769
  class SmartArcSetBase {
deba@468
   770
  public:
deba@468
   771
kpeter@617
   772
    typedef typename GR::Node Node;
kpeter@617
   773
    typedef typename GR::NodeIt NodeIt;
deba@468
   774
deba@468
   775
  protected:
deba@468
   776
deba@468
   777
    struct NodeT {
deba@468
   778
      int first_out, first_in;
deba@468
   779
      NodeT() : first_out(-1), first_in(-1) {}
deba@468
   780
    };
deba@468
   781
deba@488
   782
    typedef typename ItemSetTraits<GR, Node>::
deba@468
   783
    template Map<NodeT>::Type NodesImplBase;
deba@468
   784
deba@488
   785
    NodesImplBase* _nodes;
deba@468
   786
deba@468
   787
    struct ArcT {
deba@468
   788
      Node source, target;
deba@468
   789
      int next_out, next_in;
deba@468
   790
      ArcT() {}
deba@468
   791
    };
deba@468
   792
deba@468
   793
    std::vector<ArcT> arcs;
deba@468
   794
deba@488
   795
    const GR* _graph;
deba@468
   796
deba@488
   797
    void initalize(const GR& graph, NodesImplBase& nodes) {
deba@488
   798
      _graph = &graph;
deba@488
   799
      _nodes = &nodes;
deba@468
   800
    }
deba@468
   801
deba@468
   802
  public:
deba@468
   803
deba@468
   804
    class Arc {
deba@488
   805
      friend class SmartArcSetBase<GR>;
deba@468
   806
    protected:
deba@468
   807
      Arc(int _id) : id(_id) {}
deba@468
   808
      int id;
deba@468
   809
    public:
deba@468
   810
      Arc() {}
deba@468
   811
      Arc(Invalid) : id(-1) {}
deba@468
   812
      bool operator==(const Arc& arc) const { return id == arc.id; }
deba@468
   813
      bool operator!=(const Arc& arc) const { return id != arc.id; }
deba@468
   814
      bool operator<(const Arc& arc) const { return id < arc.id; }
deba@468
   815
    };
deba@468
   816
deba@468
   817
    SmartArcSetBase() {}
deba@468
   818
deba@468
   819
    Arc addArc(const Node& u, const Node& v) {
deba@468
   820
      int n = arcs.size();
deba@468
   821
      arcs.push_back(ArcT());
deba@488
   822
      arcs[n].next_in = (*_nodes)[v].first_in;
deba@488
   823
      (*_nodes)[v].first_in = n;
deba@488
   824
      arcs[n].next_out = (*_nodes)[u].first_out;
deba@488
   825
      (*_nodes)[u].first_out = n;
deba@468
   826
      arcs[n].source = u;
deba@468
   827
      arcs[n].target = v;
deba@468
   828
      return Arc(n);
deba@468
   829
    }
deba@468
   830
deba@468
   831
    void clear() {
deba@468
   832
      Node node;
deba@468
   833
      for (first(node); node != INVALID; next(node)) {
deba@488
   834
        (*_nodes)[node].first_in = -1;
deba@488
   835
        (*_nodes)[node].first_out = -1;
deba@468
   836
      }
deba@468
   837
      arcs.clear();
deba@468
   838
    }
deba@468
   839
deba@468
   840
    void first(Node& node) const {
deba@488
   841
      _graph->first(node);
deba@468
   842
    }
deba@468
   843
deba@468
   844
    void next(Node& node) const {
deba@488
   845
      _graph->next(node);
deba@468
   846
    }
deba@468
   847
deba@468
   848
    void first(Arc& arc) const {
deba@468
   849
      arc.id = arcs.size() - 1;
deba@468
   850
    }
deba@468
   851
deba@468
   852
    void next(Arc& arc) const {
deba@468
   853
      --arc.id;
deba@468
   854
    }
deba@468
   855
deba@468
   856
    void firstOut(Arc& arc, const Node& node) const {
deba@488
   857
      arc.id = (*_nodes)[node].first_out;
deba@468
   858
    }
deba@468
   859
deba@468
   860
    void nextOut(Arc& arc) const {
deba@468
   861
      arc.id = arcs[arc.id].next_out;
deba@468
   862
    }
deba@468
   863
deba@468
   864
    void firstIn(Arc& arc, const Node& node) const {
deba@488
   865
      arc.id = (*_nodes)[node].first_in;
deba@468
   866
    }
deba@468
   867
deba@468
   868
    void nextIn(Arc& arc) const {
deba@468
   869
      arc.id = arcs[arc.id].next_in;
deba@468
   870
    }
deba@468
   871
deba@488
   872
    int id(const Node& node) const { return _graph->id(node); }
deba@468
   873
    int id(const Arc& arc) const { return arc.id; }
deba@468
   874
deba@488
   875
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
deba@468
   876
    Arc arcFromId(int ix) const { return Arc(ix); }
deba@468
   877
deba@488
   878
    int maxNodeId() const { return _graph->maxNodeId(); };
deba@468
   879
    int maxArcId() const { return arcs.size() - 1; }
deba@468
   880
deba@468
   881
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
deba@468
   882
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
deba@468
   883
deba@488
   884
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
deba@468
   885
deba@468
   886
    NodeNotifier& notifier(Node) const {
deba@488
   887
      return _graph->notifier(Node());
deba@468
   888
    }
deba@468
   889
deba@488
   890
    template <typename V>
deba@488
   891
    class NodeMap : public GR::template NodeMap<V> {
kpeter@617
   892
      typedef typename GR::template NodeMap<V> Parent;
kpeter@617
   893
deba@468
   894
    public:
deba@468
   895
deba@488
   896
      explicit NodeMap(const SmartArcSetBase<GR>& arcset)
deba@488
   897
        : Parent(*arcset._graph) { }
deba@468
   898
deba@488
   899
      NodeMap(const SmartArcSetBase<GR>& arcset, const V& value)
deba@488
   900
        : Parent(*arcset._graph, value) { }
deba@468
   901
deba@468
   902
      NodeMap& operator=(const NodeMap& cmap) {
deba@468
   903
        return operator=<NodeMap>(cmap);
deba@468
   904
      }
deba@468
   905
deba@468
   906
      template <typename CMap>
deba@468
   907
      NodeMap& operator=(const CMap& cmap) {
deba@468
   908
        Parent::operator=(cmap);
deba@468
   909
        return *this;
deba@468
   910
      }
deba@468
   911
    };
deba@468
   912
deba@468
   913
  };
deba@468
   914
deba@468
   915
deba@468
   916
  /// \ingroup semi_adaptors
deba@468
   917
  ///
deba@468
   918
  /// \brief Digraph using a node set of another digraph or graph and
deba@468
   919
  /// an own arc set.
deba@468
   920
  ///
deba@468
   921
  /// This structure can be used to establish another directed graph
deba@468
   922
  /// over a node set of an existing one. This class uses the same
deba@468
   923
  /// Node type as the underlying graph, and each valid node of the
deba@468
   924
  /// original graph is valid in this arc set, therefore the node
deba@468
   925
  /// objects of the original graph can be used directly with this
deba@468
   926
  /// class. The node handling functions (id handling, observing, and
deba@468
   927
  /// iterators) works equivalently as in the original graph.
deba@468
   928
  ///
deba@488
   929
  /// \param GR The type of the graph which shares its node set with
deba@468
   930
  /// this class. Its interface must conform to the
deba@468
   931
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
deba@468
   932
  /// concept.
deba@468
   933
  ///
deba@468
   934
  /// This implementation is slightly faster than the \c ListArcSet,
deba@468
   935
  /// because it uses continuous storage for arcs and it uses just
deba@468
   936
  /// single-linked lists for enumerate outgoing and incoming
deba@468
   937
  /// arcs. Therefore the arcs cannot be erased from the arc sets.
deba@468
   938
  ///
deba@468
   939
  /// \warning If a node is erased from the underlying graph and this
deba@468
   940
  /// node is the source or target of one arc in the arc set, then
deba@468
   941
  /// the arc set is invalidated, and it cannot be used anymore. The
deba@468
   942
  /// validity can be checked with the \c valid() member function.
deba@468
   943
  ///
kpeter@559
   944
  /// This class fully conforms to the \ref concepts::Digraph
deba@468
   945
  /// "Digraph" concept.
deba@488
   946
  template <typename GR>
deba@488
   947
  class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > {
kpeter@617
   948
    typedef ArcSetExtender<SmartArcSetBase<GR> > Parent;
deba@468
   949
deba@468
   950
  public:
deba@468
   951
deba@468
   952
    typedef typename Parent::Node Node;
deba@468
   953
    typedef typename Parent::Arc Arc;
deba@468
   954
deba@468
   955
  protected:
deba@468
   956
deba@468
   957
    typedef typename Parent::NodesImplBase NodesImplBase;
deba@468
   958
deba@468
   959
    void eraseNode(const Node& node) {
deba@468
   960
      if (typename Parent::InArcIt(*this, node) == INVALID &&
deba@468
   961
          typename Parent::OutArcIt(*this, node) == INVALID) {
deba@468
   962
        return;
deba@468
   963
      }
deba@468
   964
      throw typename NodesImplBase::Notifier::ImmediateDetach();
deba@468
   965
    }
deba@468
   966
deba@468
   967
    void clearNodes() {
deba@468
   968
      Parent::clear();
deba@468
   969
    }
deba@468
   970
deba@468
   971
    class NodesImpl : public NodesImplBase {
deba@468
   972
      typedef NodesImplBase Parent;
deba@468
   973
kpeter@617
   974
    public:
deba@488
   975
      NodesImpl(const GR& graph, SmartArcSet& arcset)
deba@468
   976
        : Parent(graph), _arcset(arcset) {}
deba@468
   977
deba@468
   978
      virtual ~NodesImpl() {}
deba@468
   979
deba@468
   980
      bool attached() const {
deba@468
   981
        return Parent::attached();
deba@468
   982
      }
deba@468
   983
deba@468
   984
    protected:
deba@468
   985
deba@468
   986
      virtual void erase(const Node& node) {
deba@468
   987
        try {
deba@468
   988
          _arcset.eraseNode(node);
deba@468
   989
          Parent::erase(node);
deba@468
   990
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
deba@468
   991
          Parent::clear();
deba@468
   992
          throw;
deba@468
   993
        }
deba@468
   994
      }
deba@468
   995
      virtual void erase(const std::vector<Node>& nodes) {
deba@468
   996
        try {
deba@468
   997
          for (int i = 0; i < int(nodes.size()); ++i) {
deba@468
   998
            _arcset.eraseNode(nodes[i]);
deba@468
   999
          }
deba@468
  1000
          Parent::erase(nodes);
deba@468
  1001
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
deba@468
  1002
          Parent::clear();
deba@468
  1003
          throw;
deba@468
  1004
        }
deba@468
  1005
      }
deba@468
  1006
      virtual void clear() {
deba@468
  1007
        _arcset.clearNodes();
deba@468
  1008
        Parent::clear();
deba@468
  1009
      }
deba@468
  1010
deba@468
  1011
    private:
deba@468
  1012
      SmartArcSet& _arcset;
deba@468
  1013
    };
deba@468
  1014
deba@488
  1015
    NodesImpl _nodes;
deba@468
  1016
deba@468
  1017
  public:
deba@468
  1018
deba@468
  1019
    /// \brief Constructor of the ArcSet.
deba@468
  1020
    ///
deba@468
  1021
    /// Constructor of the ArcSet.
deba@488
  1022
    SmartArcSet(const GR& graph) : _nodes(graph, *this) {
deba@488
  1023
      Parent::initalize(graph, _nodes);
deba@468
  1024
    }
deba@468
  1025
deba@468
  1026
    /// \brief Add a new arc to the digraph.
deba@468
  1027
    ///
deba@468
  1028
    /// Add a new arc to the digraph with source node \c s
deba@468
  1029
    /// and target node \c t.
kpeter@559
  1030
    /// \return The new arc.
deba@468
  1031
    Arc addArc(const Node& s, const Node& t) {
deba@468
  1032
      return Parent::addArc(s, t);
deba@468
  1033
    }
deba@468
  1034
deba@468
  1035
    /// \brief Validity check
deba@468
  1036
    ///
deba@468
  1037
    /// This functions gives back false if the ArcSet is
deba@468
  1038
    /// invalidated. It occurs when a node in the underlying graph is
deba@468
  1039
    /// erased and it is not isolated in the ArcSet.
deba@468
  1040
    bool valid() const {
deba@488
  1041
      return _nodes.attached();
deba@468
  1042
    }
deba@468
  1043
deba@468
  1044
  };
deba@468
  1045
deba@468
  1046
deba@488
  1047
  template <typename GR>
deba@468
  1048
  class SmartEdgeSetBase {
deba@468
  1049
  public:
deba@468
  1050
deba@488
  1051
    typedef typename GR::Node Node;
deba@488
  1052
    typedef typename GR::NodeIt NodeIt;
deba@468
  1053
deba@468
  1054
  protected:
deba@468
  1055
deba@468
  1056
    struct NodeT {
deba@468
  1057
      int first_out;
deba@468
  1058
      NodeT() : first_out(-1) {}
deba@468
  1059
    };
deba@468
  1060
deba@488
  1061
    typedef typename ItemSetTraits<GR, Node>::
deba@468
  1062
    template Map<NodeT>::Type NodesImplBase;
deba@468
  1063
deba@488
  1064
    NodesImplBase* _nodes;
deba@468
  1065
deba@468
  1066
    struct ArcT {
deba@468
  1067
      Node target;
deba@468
  1068
      int next_out;
deba@468
  1069
      ArcT() {}
deba@468
  1070
    };
deba@468
  1071
deba@468
  1072
    std::vector<ArcT> arcs;
deba@468
  1073
deba@488
  1074
    const GR* _graph;
deba@468
  1075
deba@488
  1076
    void initalize(const GR& graph, NodesImplBase& nodes) {
deba@488
  1077
      _graph = &graph;
deba@488
  1078
      _nodes = &nodes;
deba@468
  1079
    }
deba@468
  1080
deba@468
  1081
  public:
deba@468
  1082
deba@468
  1083
    class Edge {
deba@468
  1084
      friend class SmartEdgeSetBase;
deba@468
  1085
    protected:
deba@468
  1086
deba@468
  1087
      int id;
deba@468
  1088
      explicit Edge(int _id) { id = _id;}
deba@468
  1089
deba@468
  1090
    public:
deba@468
  1091
      Edge() {}
deba@468
  1092
      Edge (Invalid) { id = -1; }
deba@468
  1093
      bool operator==(const Edge& arc) const {return id == arc.id;}
deba@468
  1094
      bool operator!=(const Edge& arc) const {return id != arc.id;}
deba@468
  1095
      bool operator<(const Edge& arc) const {return id < arc.id;}
deba@468
  1096
    };
deba@468
  1097
deba@468
  1098
    class Arc {
deba@468
  1099
      friend class SmartEdgeSetBase;
deba@468
  1100
    protected:
deba@468
  1101
      Arc(int _id) : id(_id) {}
deba@468
  1102
      int id;
deba@468
  1103
    public:
deba@468
  1104
      operator Edge() const { return edgeFromId(id / 2); }
deba@468
  1105
deba@468
  1106
      Arc() {}
deba@468
  1107
      Arc(Invalid) : id(-1) {}
deba@468
  1108
      bool operator==(const Arc& arc) const { return id == arc.id; }
deba@468
  1109
      bool operator!=(const Arc& arc) const { return id != arc.id; }
deba@468
  1110
      bool operator<(const Arc& arc) const { return id < arc.id; }
deba@468
  1111
    };
deba@468
  1112
deba@468
  1113
    SmartEdgeSetBase() {}
deba@468
  1114
deba@468
  1115
    Edge addEdge(const Node& u, const Node& v) {
deba@468
  1116
      int n = arcs.size();
deba@468
  1117
      arcs.push_back(ArcT());
deba@468
  1118
      arcs.push_back(ArcT());
deba@468
  1119
deba@468
  1120
      arcs[n].target = u;
deba@468
  1121
      arcs[n | 1].target = v;
deba@468
  1122
deba@488
  1123
      arcs[n].next_out = (*_nodes)[v].first_out;
deba@488
  1124
      (*_nodes)[v].first_out = n;
deba@468
  1125
deba@488
  1126
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
deba@488
  1127
      (*_nodes)[u].first_out = (n | 1);
deba@468
  1128
deba@468
  1129
      return Edge(n / 2);
deba@468
  1130
    }
deba@468
  1131
deba@468
  1132
    void clear() {
deba@468
  1133
      Node node;
deba@468
  1134
      for (first(node); node != INVALID; next(node)) {
deba@488
  1135
        (*_nodes)[node].first_out = -1;
deba@468
  1136
      }
deba@468
  1137
      arcs.clear();
deba@468
  1138
    }
deba@468
  1139
deba@468
  1140
    void first(Node& node) const {
deba@488
  1141
      _graph->first(node);
deba@468
  1142
    }
deba@468
  1143
deba@468
  1144
    void next(Node& node) const {
deba@488
  1145
      _graph->next(node);
deba@468
  1146
    }
deba@468
  1147
deba@468
  1148
    void first(Arc& arc) const {
deba@468
  1149
      arc.id = arcs.size() - 1;
deba@468
  1150
    }
deba@468
  1151
deba@468
  1152
    void next(Arc& arc) const {
deba@468
  1153
      --arc.id;
deba@468
  1154
    }
deba@468
  1155
deba@468
  1156
    void first(Edge& arc) const {
deba@468
  1157
      arc.id = arcs.size() / 2 - 1;
deba@468
  1158
    }
deba@468
  1159
deba@468
  1160
    void next(Edge& arc) const {
deba@468
  1161
      --arc.id;
deba@468
  1162
    }
deba@468
  1163
deba@468
  1164
    void firstOut(Arc& arc, const Node& node) const {
deba@488
  1165
      arc.id = (*_nodes)[node].first_out;
deba@468
  1166
    }
deba@468
  1167
deba@468
  1168
    void nextOut(Arc& arc) const {
deba@468
  1169
      arc.id = arcs[arc.id].next_out;
deba@468
  1170
    }
deba@468
  1171
deba@468
  1172
    void firstIn(Arc& arc, const Node& node) const {
deba@488
  1173
      arc.id = (((*_nodes)[node].first_out) ^ 1);
deba@468
  1174
      if (arc.id == -2) arc.id = -1;
deba@468
  1175
    }
deba@468
  1176
deba@468
  1177
    void nextIn(Arc& arc) const {
deba@468
  1178
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
deba@468
  1179
      if (arc.id == -2) arc.id = -1;
deba@468
  1180
    }
deba@468
  1181
deba@468
  1182
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
deba@488
  1183
      int de = (*_nodes)[node].first_out;
deba@468
  1184
      if (de != -1 ) {
deba@468
  1185
        arc.id = de / 2;
deba@468
  1186
        dir = ((de & 1) == 1);
deba@468
  1187
      } else {
deba@468
  1188
        arc.id = -1;
deba@468
  1189
        dir = true;
deba@468
  1190
      }
deba@468
  1191
    }
deba@468
  1192
    void nextInc(Edge &arc, bool& dir) const {
deba@468
  1193
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
deba@468
  1194
      if (de != -1 ) {
deba@468
  1195
        arc.id = de / 2;
deba@468
  1196
        dir = ((de & 1) == 1);
deba@468
  1197
      } else {
deba@468
  1198
        arc.id = -1;
deba@468
  1199
        dir = true;
deba@468
  1200
      }
deba@468
  1201
    }
deba@468
  1202
deba@468
  1203
    static bool direction(Arc arc) {
deba@468
  1204
      return (arc.id & 1) == 1;
deba@468
  1205
    }
deba@468
  1206
deba@468
  1207
    static Arc direct(Edge edge, bool dir) {
deba@468
  1208
      return Arc(edge.id * 2 + (dir ? 1 : 0));
deba@468
  1209
    }
deba@468
  1210
deba@488
  1211
    int id(Node node) const { return _graph->id(node); }
deba@468
  1212
    static int id(Arc arc) { return arc.id; }
deba@468
  1213
    static int id(Edge arc) { return arc.id; }
deba@468
  1214
deba@488
  1215
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
deba@468
  1216
    static Arc arcFromId(int id) { return Arc(id); }
deba@468
  1217
    static Edge edgeFromId(int id) { return Edge(id);}
deba@468
  1218
deba@488
  1219
    int maxNodeId() const { return _graph->maxNodeId(); };
deba@468
  1220
    int maxArcId() const { return arcs.size() - 1; }
deba@468
  1221
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
deba@468
  1222
deba@468
  1223
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
deba@468
  1224
    Node target(Arc e) const { return arcs[e.id].target; }
deba@468
  1225
deba@468
  1226
    Node u(Edge e) const { return arcs[2 * e.id].target; }
deba@468
  1227
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
deba@468
  1228
deba@488
  1229
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
deba@468
  1230
deba@468
  1231
    NodeNotifier& notifier(Node) const {
deba@488
  1232
      return _graph->notifier(Node());
deba@468
  1233
    }
deba@468
  1234
deba@488
  1235
    template <typename V>
deba@488
  1236
    class NodeMap : public GR::template NodeMap<V> {
kpeter@617
  1237
      typedef typename GR::template NodeMap<V> Parent;
kpeter@617
  1238
deba@468
  1239
    public:
deba@468
  1240
deba@488
  1241
      explicit NodeMap(const SmartEdgeSetBase<GR>& arcset)
deba@488
  1242
        : Parent(*arcset._graph) { }
deba@468
  1243
deba@488
  1244
      NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value)
deba@488
  1245
        : Parent(*arcset._graph, value) { }
deba@468
  1246
deba@468
  1247
      NodeMap& operator=(const NodeMap& cmap) {
deba@468
  1248
        return operator=<NodeMap>(cmap);
deba@468
  1249
      }
deba@468
  1250
deba@468
  1251
      template <typename CMap>
deba@468
  1252
      NodeMap& operator=(const CMap& cmap) {
deba@468
  1253
        Parent::operator=(cmap);
deba@468
  1254
        return *this;
deba@468
  1255
      }
deba@468
  1256
    };
deba@468
  1257
deba@468
  1258
  };
deba@468
  1259
deba@468
  1260
  /// \ingroup semi_adaptors
deba@468
  1261
  ///
deba@468
  1262
  /// \brief Graph using a node set of another digraph or graph and an
deba@468
  1263
  /// own edge set.
deba@468
  1264
  ///
deba@468
  1265
  /// This structure can be used to establish another graph over a
deba@468
  1266
  /// node set of an existing one. This class uses the same Node type
deba@468
  1267
  /// as the underlying graph, and each valid node of the original
deba@468
  1268
  /// graph is valid in this arc set, therefore the node objects of
deba@468
  1269
  /// the original graph can be used directly with this class. The
deba@468
  1270
  /// node handling functions (id handling, observing, and iterators)
deba@468
  1271
  /// works equivalently as in the original graph.
deba@468
  1272
  ///
deba@488
  1273
  /// \param GR The type of the graph which shares its node set
deba@468
  1274
  /// with this class. Its interface must conform to the
deba@468
  1275
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
deba@468
  1276
  ///  concept.
deba@468
  1277
  ///
deba@468
  1278
  /// This implementation is slightly faster than the \c ListEdgeSet,
deba@468
  1279
  /// because it uses continuous storage for edges and it uses just
deba@468
  1280
  /// single-linked lists for enumerate incident edges. Therefore the
deba@468
  1281
  /// edges cannot be erased from the edge sets.
deba@468
  1282
  ///
deba@468
  1283
  /// \warning If a node is erased from the underlying graph and this
deba@468
  1284
  /// node is incident to one edge in the edge set, then the edge set
deba@468
  1285
  /// is invalidated, and it cannot be used anymore. The validity can
deba@468
  1286
  /// be checked with the \c valid() member function.
deba@468
  1287
  ///
kpeter@559
  1288
  /// This class fully conforms to the \ref concepts::Graph
deba@468
  1289
  /// "Graph" concept.
deba@488
  1290
  template <typename GR>
deba@488
  1291
  class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > {
kpeter@617
  1292
    typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent;
deba@468
  1293
deba@468
  1294
  public:
deba@468
  1295
deba@468
  1296
    typedef typename Parent::Node Node;
deba@468
  1297
    typedef typename Parent::Arc Arc;
deba@468
  1298
    typedef typename Parent::Edge Edge;
deba@468
  1299
deba@468
  1300
  protected:
deba@468
  1301
deba@468
  1302
    typedef typename Parent::NodesImplBase NodesImplBase;
deba@468
  1303
deba@468
  1304
    void eraseNode(const Node& node) {
deba@468
  1305
      if (typename Parent::IncEdgeIt(*this, node) == INVALID) {
deba@468
  1306
        return;
deba@468
  1307
      }
deba@468
  1308
      throw typename NodesImplBase::Notifier::ImmediateDetach();
deba@468
  1309
    }
deba@468
  1310
deba@468
  1311
    void clearNodes() {
deba@468
  1312
      Parent::clear();
deba@468
  1313
    }
deba@468
  1314
deba@468
  1315
    class NodesImpl : public NodesImplBase {
deba@468
  1316
      typedef NodesImplBase Parent;
deba@468
  1317
kpeter@617
  1318
    public:
deba@488
  1319
      NodesImpl(const GR& graph, SmartEdgeSet& arcset)
deba@468
  1320
        : Parent(graph), _arcset(arcset) {}
deba@468
  1321
deba@468
  1322
      virtual ~NodesImpl() {}
deba@468
  1323
deba@468
  1324
      bool attached() const {
deba@468
  1325
        return Parent::attached();
deba@468
  1326
      }
deba@468
  1327
deba@468
  1328
    protected:
deba@468
  1329
deba@468
  1330
      virtual void erase(const Node& node) {
deba@468
  1331
        try {
deba@468
  1332
          _arcset.eraseNode(node);
deba@468
  1333
          Parent::erase(node);
deba@468
  1334
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
deba@468
  1335
          Parent::clear();
deba@468
  1336
          throw;
deba@468
  1337
        }
deba@468
  1338
      }
deba@468
  1339
      virtual void erase(const std::vector<Node>& nodes) {
deba@468
  1340
        try {
deba@468
  1341
          for (int i = 0; i < int(nodes.size()); ++i) {
deba@468
  1342
            _arcset.eraseNode(nodes[i]);
deba@468
  1343
          }
deba@468
  1344
          Parent::erase(nodes);
deba@468
  1345
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
deba@468
  1346
          Parent::clear();
deba@468
  1347
          throw;
deba@468
  1348
        }
deba@468
  1349
      }
deba@468
  1350
      virtual void clear() {
deba@468
  1351
        _arcset.clearNodes();
deba@468
  1352
        Parent::clear();
deba@468
  1353
      }
deba@468
  1354
deba@468
  1355
    private:
deba@468
  1356
      SmartEdgeSet& _arcset;
deba@468
  1357
    };
deba@468
  1358
deba@488
  1359
    NodesImpl _nodes;
deba@468
  1360
deba@468
  1361
  public:
deba@468
  1362
deba@468
  1363
    /// \brief Constructor of the EdgeSet.
deba@468
  1364
    ///
deba@468
  1365
    /// Constructor of the EdgeSet.
deba@488
  1366
    SmartEdgeSet(const GR& graph) : _nodes(graph, *this) {
deba@488
  1367
      Parent::initalize(graph, _nodes);
deba@468
  1368
    }
deba@468
  1369
deba@468
  1370
    /// \brief Add a new edge to the graph.
deba@468
  1371
    ///
deba@468
  1372
    /// Add a new edge to the graph with node \c u
deba@468
  1373
    /// and node \c v endpoints.
kpeter@559
  1374
    /// \return The new edge.
deba@468
  1375
    Edge addEdge(const Node& u, const Node& v) {
deba@468
  1376
      return Parent::addEdge(u, v);
deba@468
  1377
    }
deba@468
  1378
deba@468
  1379
    /// \brief Validity check
deba@468
  1380
    ///
deba@468
  1381
    /// This functions gives back false if the EdgeSet is
deba@468
  1382
    /// invalidated. It occurs when a node in the underlying graph is
deba@468
  1383
    /// erased and it is not isolated in the EdgeSet.
deba@468
  1384
    bool valid() const {
deba@488
  1385
      return _nodes.attached();
deba@468
  1386
    }
deba@468
  1387
deba@468
  1388
  };
deba@468
  1389
deba@468
  1390
}
deba@468
  1391
deba@468
  1392
#endif