lemon/adaptors.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 11 Jan 2009 15:09:53 +0000
changeset 478 5a1e9fdcfd3a
parent 463 88ed40ad0d4f
parent 477 f599fa651202
child 487 acfb0f24d178
permissions -rw-r--r--
Merge
deba@432
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@430
     2
 *
deba@432
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@430
     4
 *
alpar@477
     5
 * Copyright (C) 2003-2009
deba@430
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@430
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@430
     8
 *
deba@430
     9
 * Permission to use, modify and distribute this software is granted
deba@430
    10
 * provided that this copyright notice appears in all copies. For
deba@430
    11
 * precise terms see the accompanying LICENSE file.
deba@430
    12
 *
deba@430
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@430
    14
 * express or implied, and with no claim as to its suitability for any
deba@430
    15
 * purpose.
deba@430
    16
 *
deba@430
    17
 */
deba@430
    18
deba@432
    19
#ifndef LEMON_ADAPTORS_H
deba@432
    20
#define LEMON_ADAPTORS_H
deba@432
    21
deba@432
    22
/// \ingroup graph_adaptors
deba@432
    23
/// \file
kpeter@474
    24
/// \brief Adaptor classes for digraphs and graphs
deba@430
    25
///
deba@432
    26
/// This file contains several useful adaptors for digraphs and graphs.
deba@430
    27
deba@430
    28
#include <lemon/core.h>
deba@430
    29
#include <lemon/maps.h>
deba@430
    30
#include <lemon/bits/variant.h>
deba@430
    31
deba@430
    32
#include <lemon/bits/graph_adaptor_extender.h>
deba@430
    33
#include <lemon/tolerance.h>
deba@430
    34
deba@430
    35
#include <algorithm>
deba@430
    36
deba@430
    37
namespace lemon {
deba@430
    38
deba@430
    39
  template<typename _Digraph>
deba@430
    40
  class DigraphAdaptorBase {
deba@430
    41
  public:
deba@430
    42
    typedef _Digraph Digraph;
deba@430
    43
    typedef DigraphAdaptorBase Adaptor;
deba@430
    44
    typedef Digraph ParentDigraph;
deba@430
    45
deba@430
    46
  protected:
deba@430
    47
    Digraph* _digraph;
deba@430
    48
    DigraphAdaptorBase() : _digraph(0) { }
deba@430
    49
    void setDigraph(Digraph& digraph) { _digraph = &digraph; }
deba@430
    50
deba@430
    51
  public:
deba@430
    52
    DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { }
deba@430
    53
deba@430
    54
    typedef typename Digraph::Node Node;
deba@430
    55
    typedef typename Digraph::Arc Arc;
deba@432
    56
deba@430
    57
    void first(Node& i) const { _digraph->first(i); }
deba@430
    58
    void first(Arc& i) const { _digraph->first(i); }
deba@430
    59
    void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
deba@430
    60
    void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
deba@430
    61
deba@430
    62
    void next(Node& i) const { _digraph->next(i); }
deba@430
    63
    void next(Arc& i) const { _digraph->next(i); }
deba@430
    64
    void nextIn(Arc& i) const { _digraph->nextIn(i); }
deba@430
    65
    void nextOut(Arc& i) const { _digraph->nextOut(i); }
deba@430
    66
deba@430
    67
    Node source(const Arc& a) const { return _digraph->source(a); }
deba@430
    68
    Node target(const Arc& a) const { return _digraph->target(a); }
deba@430
    69
deba@430
    70
    typedef NodeNumTagIndicator<Digraph> NodeNumTag;
deba@430
    71
    int nodeNum() const { return _digraph->nodeNum(); }
deba@432
    72
kpeter@469
    73
    typedef ArcNumTagIndicator<Digraph> ArcNumTag;
deba@430
    74
    int arcNum() const { return _digraph->arcNum(); }
deba@430
    75
kpeter@469
    76
    typedef FindArcTagIndicator<Digraph> FindArcTag;
kpeter@471
    77
    Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
deba@430
    78
      return _digraph->findArc(u, v, prev);
deba@430
    79
    }
deba@432
    80
deba@430
    81
    Node addNode() { return _digraph->addNode(); }
deba@430
    82
    Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
deba@430
    83
kpeter@471
    84
    void erase(const Node& n) { _digraph->erase(n); }
kpeter@471
    85
    void erase(const Arc& a) { _digraph->erase(a); }
kpeter@471
    86
kpeter@471
    87
    void clear() { _digraph->clear(); }
deba@432
    88
deba@430
    89
    int id(const Node& n) const { return _digraph->id(n); }
deba@430
    90
    int id(const Arc& a) const { return _digraph->id(a); }
deba@430
    91
deba@430
    92
    Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
deba@430
    93
    Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
deba@430
    94
deba@430
    95
    int maxNodeId() const { return _digraph->maxNodeId(); }
deba@430
    96
    int maxArcId() const { return _digraph->maxArcId(); }
deba@430
    97
deba@430
    98
    typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
deba@432
    99
    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
deba@430
   100
deba@430
   101
    typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier;
deba@432
   102
    ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
deba@432
   103
deba@430
   104
    template <typename _Value>
deba@430
   105
    class NodeMap : public Digraph::template NodeMap<_Value> {
deba@430
   106
    public:
deba@430
   107
deba@430
   108
      typedef typename Digraph::template NodeMap<_Value> Parent;
deba@430
   109
deba@432
   110
      explicit NodeMap(const Adaptor& adaptor)
deba@432
   111
        : Parent(*adaptor._digraph) {}
deba@430
   112
deba@430
   113
      NodeMap(const Adaptor& adaptor, const _Value& value)
deba@432
   114
        : Parent(*adaptor._digraph, value) { }
deba@430
   115
deba@430
   116
    private:
deba@430
   117
      NodeMap& operator=(const NodeMap& cmap) {
deba@430
   118
        return operator=<NodeMap>(cmap);
deba@430
   119
      }
deba@430
   120
deba@430
   121
      template <typename CMap>
deba@430
   122
      NodeMap& operator=(const CMap& cmap) {
deba@430
   123
        Parent::operator=(cmap);
deba@430
   124
        return *this;
deba@430
   125
      }
deba@432
   126
deba@430
   127
    };
deba@430
   128
deba@430
   129
    template <typename _Value>
deba@430
   130
    class ArcMap : public Digraph::template ArcMap<_Value> {
deba@430
   131
    public:
deba@432
   132
deba@430
   133
      typedef typename Digraph::template ArcMap<_Value> Parent;
deba@432
   134
deba@432
   135
      explicit ArcMap(const Adaptor& adaptor)
deba@432
   136
        : Parent(*adaptor._digraph) {}
deba@430
   137
deba@430
   138
      ArcMap(const Adaptor& adaptor, const _Value& value)
deba@432
   139
        : Parent(*adaptor._digraph, value) {}
deba@430
   140
deba@430
   141
    private:
deba@430
   142
      ArcMap& operator=(const ArcMap& cmap) {
deba@430
   143
        return operator=<ArcMap>(cmap);
deba@430
   144
      }
deba@430
   145
deba@430
   146
      template <typename CMap>
deba@430
   147
      ArcMap& operator=(const CMap& cmap) {
deba@430
   148
        Parent::operator=(cmap);
deba@430
   149
        return *this;
deba@430
   150
      }
deba@430
   151
deba@430
   152
    };
deba@430
   153
deba@430
   154
  };
deba@430
   155
deba@432
   156
  template<typename _Graph>
deba@432
   157
  class GraphAdaptorBase {
deba@432
   158
  public:
deba@432
   159
    typedef _Graph Graph;
deba@432
   160
    typedef Graph ParentGraph;
deba@432
   161
deba@432
   162
  protected:
deba@432
   163
    Graph* _graph;
deba@432
   164
deba@432
   165
    GraphAdaptorBase() : _graph(0) {}
deba@432
   166
deba@432
   167
    void setGraph(Graph& graph) { _graph = &graph; }
deba@432
   168
deba@432
   169
  public:
deba@432
   170
    GraphAdaptorBase(Graph& graph) : _graph(&graph) {}
deba@432
   171
deba@432
   172
    typedef typename Graph::Node Node;
deba@432
   173
    typedef typename Graph::Arc Arc;
deba@432
   174
    typedef typename Graph::Edge Edge;
deba@432
   175
deba@432
   176
    void first(Node& i) const { _graph->first(i); }
deba@432
   177
    void first(Arc& i) const { _graph->first(i); }
deba@432
   178
    void first(Edge& i) const { _graph->first(i); }
deba@432
   179
    void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
deba@432
   180
    void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
deba@432
   181
    void firstInc(Edge &i, bool &d, const Node &n) const {
deba@432
   182
      _graph->firstInc(i, d, n);
deba@432
   183
    }
deba@432
   184
deba@432
   185
    void next(Node& i) const { _graph->next(i); }
deba@432
   186
    void next(Arc& i) const { _graph->next(i); }
deba@432
   187
    void next(Edge& i) const { _graph->next(i); }
deba@432
   188
    void nextIn(Arc& i) const { _graph->nextIn(i); }
deba@432
   189
    void nextOut(Arc& i) const { _graph->nextOut(i); }
deba@432
   190
    void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
deba@432
   191
deba@432
   192
    Node u(const Edge& e) const { return _graph->u(e); }
deba@432
   193
    Node v(const Edge& e) const { return _graph->v(e); }
deba@432
   194
deba@432
   195
    Node source(const Arc& a) const { return _graph->source(a); }
deba@432
   196
    Node target(const Arc& a) const { return _graph->target(a); }
deba@432
   197
deba@432
   198
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
deba@432
   199
    int nodeNum() const { return _graph->nodeNum(); }
deba@432
   200
kpeter@469
   201
    typedef ArcNumTagIndicator<Graph> ArcNumTag;
kpeter@469
   202
    int arcNum() const { return _graph->arcNum(); }
kpeter@469
   203
deba@432
   204
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
deba@432
   205
    int edgeNum() const { return _graph->edgeNum(); }
deba@432
   206
kpeter@469
   207
    typedef FindArcTagIndicator<Graph> FindArcTag;
kpeter@471
   208
    Arc findArc(const Node& u, const Node& v,
kpeter@471
   209
                const Arc& prev = INVALID) const {
deba@432
   210
      return _graph->findArc(u, v, prev);
deba@432
   211
    }
kpeter@469
   212
kpeter@469
   213
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
kpeter@471
   214
    Edge findEdge(const Node& u, const Node& v,
kpeter@471
   215
                  const Edge& prev = INVALID) const {
deba@432
   216
      return _graph->findEdge(u, v, prev);
deba@432
   217
    }
deba@432
   218
deba@432
   219
    Node addNode() { return _graph->addNode(); }
deba@432
   220
    Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
deba@432
   221
deba@432
   222
    void erase(const Node& i) { _graph->erase(i); }
deba@432
   223
    void erase(const Edge& i) { _graph->erase(i); }
deba@432
   224
deba@432
   225
    void clear() { _graph->clear(); }
deba@432
   226
deba@432
   227
    bool direction(const Arc& a) const { return _graph->direction(a); }
deba@432
   228
    Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
deba@432
   229
deba@432
   230
    int id(const Node& v) const { return _graph->id(v); }
deba@432
   231
    int id(const Arc& a) const { return _graph->id(a); }
deba@432
   232
    int id(const Edge& e) const { return _graph->id(e); }
deba@432
   233
deba@432
   234
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
deba@432
   235
    Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
deba@432
   236
    Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
deba@432
   237
deba@432
   238
    int maxNodeId() const { return _graph->maxNodeId(); }
deba@432
   239
    int maxArcId() const { return _graph->maxArcId(); }
deba@432
   240
    int maxEdgeId() const { return _graph->maxEdgeId(); }
deba@432
   241
deba@432
   242
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
deba@432
   243
    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
deba@432
   244
deba@432
   245
    typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
deba@432
   246
    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
deba@432
   247
deba@432
   248
    typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
deba@432
   249
    EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
deba@432
   250
deba@432
   251
    template <typename _Value>
deba@432
   252
    class NodeMap : public Graph::template NodeMap<_Value> {
deba@432
   253
    public:
deba@432
   254
      typedef typename Graph::template NodeMap<_Value> Parent;
deba@432
   255
      explicit NodeMap(const GraphAdaptorBase<Graph>& adapter)
deba@432
   256
        : Parent(*adapter._graph) {}
deba@432
   257
      NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
deba@432
   258
        : Parent(*adapter._graph, value) {}
deba@432
   259
deba@432
   260
    private:
deba@432
   261
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
   262
        return operator=<NodeMap>(cmap);
deba@432
   263
      }
deba@432
   264
deba@432
   265
      template <typename CMap>
deba@432
   266
      NodeMap& operator=(const CMap& cmap) {
deba@432
   267
        Parent::operator=(cmap);
deba@432
   268
        return *this;
deba@432
   269
      }
deba@432
   270
deba@432
   271
    };
deba@432
   272
deba@432
   273
    template <typename _Value>
deba@432
   274
    class ArcMap : public Graph::template ArcMap<_Value> {
deba@432
   275
    public:
deba@432
   276
      typedef typename Graph::template ArcMap<_Value> Parent;
deba@432
   277
      explicit ArcMap(const GraphAdaptorBase<Graph>& adapter)
deba@432
   278
        : Parent(*adapter._graph) {}
deba@432
   279
      ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
deba@432
   280
        : Parent(*adapter._graph, value) {}
deba@432
   281
deba@432
   282
    private:
deba@432
   283
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
   284
        return operator=<ArcMap>(cmap);
deba@432
   285
      }
deba@432
   286
deba@432
   287
      template <typename CMap>
deba@432
   288
      ArcMap& operator=(const CMap& cmap) {
deba@432
   289
        Parent::operator=(cmap);
deba@432
   290
        return *this;
deba@432
   291
      }
deba@432
   292
    };
deba@432
   293
deba@432
   294
    template <typename _Value>
deba@432
   295
    class EdgeMap : public Graph::template EdgeMap<_Value> {
deba@432
   296
    public:
deba@432
   297
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@432
   298
      explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter)
deba@432
   299
        : Parent(*adapter._graph) {}
deba@432
   300
      EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
deba@432
   301
        : Parent(*adapter._graph, value) {}
deba@432
   302
deba@432
   303
    private:
deba@432
   304
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@432
   305
        return operator=<EdgeMap>(cmap);
deba@432
   306
      }
deba@432
   307
deba@432
   308
      template <typename CMap>
deba@432
   309
      EdgeMap& operator=(const CMap& cmap) {
deba@432
   310
        Parent::operator=(cmap);
deba@432
   311
        return *this;
deba@432
   312
      }
deba@432
   313
    };
deba@432
   314
deba@432
   315
  };
deba@430
   316
deba@430
   317
  template <typename _Digraph>
deba@432
   318
  class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> {
deba@430
   319
  public:
deba@430
   320
    typedef _Digraph Digraph;
deba@430
   321
    typedef DigraphAdaptorBase<_Digraph> Parent;
deba@430
   322
  protected:
deba@432
   323
    ReverseDigraphBase() : Parent() { }
deba@430
   324
  public:
deba@430
   325
    typedef typename Parent::Node Node;
deba@430
   326
    typedef typename Parent::Arc Arc;
deba@430
   327
deba@430
   328
    void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
deba@430
   329
    void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
deba@430
   330
deba@430
   331
    void nextIn(Arc& a) const { Parent::nextOut(a); }
deba@430
   332
    void nextOut(Arc& a) const { Parent::nextIn(a); }
deba@430
   333
deba@430
   334
    Node source(const Arc& a) const { return Parent::target(a); }
deba@430
   335
    Node target(const Arc& a) const { return Parent::source(a); }
deba@430
   336
deba@432
   337
    Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
deba@432
   338
kpeter@469
   339
    typedef FindArcTagIndicator<Digraph> FindArcTag;
deba@432
   340
    Arc findArc(const Node& u, const Node& v,
kpeter@471
   341
                const Arc& prev = INVALID) const {
deba@430
   342
      return Parent::findArc(v, u, prev);
deba@430
   343
    }
deba@430
   344
deba@430
   345
  };
deba@432
   346
deba@432
   347
  /// \ingroup graph_adaptors
deba@430
   348
  ///
kpeter@474
   349
  /// \brief Adaptor class for reversing the orientation of the arcs in
kpeter@474
   350
  /// a digraph.
deba@430
   351
  ///
kpeter@474
   352
  /// ReverseDigraph can be used for reversing the arcs in a digraph.
kpeter@474
   353
  /// It conforms to the \ref concepts::Digraph "Digraph" concept.
deba@430
   354
  ///
kpeter@474
   355
  /// The adapted digraph can also be modified through this adaptor
kpeter@476
   356
  /// by adding or removing nodes or arcs, unless the \c GR template
kpeter@474
   357
  /// parameter is set to be \c const.
kpeter@474
   358
  ///
kpeter@476
   359
  /// \tparam GR The type of the adapted digraph.
kpeter@474
   360
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
kpeter@474
   361
  /// It can also be specified to be \c const.
kpeter@474
   362
  ///
kpeter@474
   363
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
kpeter@474
   364
  /// digraph are convertible to each other.
kpeter@476
   365
  template<typename GR>
kpeter@476
   366
#ifdef DOXYGEN
kpeter@476
   367
  class ReverseDigraph {
kpeter@476
   368
#else
deba@432
   369
  class ReverseDigraph :
kpeter@476
   370
    public DigraphAdaptorExtender<ReverseDigraphBase<GR> > {
kpeter@476
   371
#endif
deba@430
   372
  public:
kpeter@476
   373
    /// The type of the adapted digraph.
kpeter@476
   374
    typedef GR Digraph;
kpeter@476
   375
    typedef DigraphAdaptorExtender<ReverseDigraphBase<GR> > Parent;
deba@430
   376
  protected:
deba@432
   377
    ReverseDigraph() { }
deba@430
   378
  public:
deba@431
   379
deba@431
   380
    /// \brief Constructor
deba@431
   381
    ///
kpeter@474
   382
    /// Creates a reverse digraph adaptor for the given digraph.
deba@432
   383
    explicit ReverseDigraph(Digraph& digraph) {
deba@432
   384
      Parent::setDigraph(digraph);
deba@430
   385
    }
deba@430
   386
  };
deba@430
   387
kpeter@474
   388
  /// \brief Returns a read-only ReverseDigraph adaptor
deba@430
   389
  ///
kpeter@474
   390
  /// This function just returns a read-only \ref ReverseDigraph adaptor.
kpeter@474
   391
  /// \ingroup graph_adaptors
kpeter@474
   392
  /// \relates ReverseDigraph
kpeter@476
   393
  template<typename GR>
kpeter@476
   394
  ReverseDigraph<const GR> reverseDigraph(const GR& digraph) {
kpeter@476
   395
    return ReverseDigraph<const GR>(digraph);
deba@430
   396
  }
deba@430
   397
kpeter@474
   398
deba@432
   399
  template <typename _Digraph, typename _NodeFilterMap,
deba@432
   400
            typename _ArcFilterMap, bool _checked = true>
deba@432
   401
  class SubDigraphBase : public DigraphAdaptorBase<_Digraph> {
deba@430
   402
  public:
deba@430
   403
    typedef _Digraph Digraph;
deba@430
   404
    typedef _NodeFilterMap NodeFilterMap;
deba@430
   405
    typedef _ArcFilterMap ArcFilterMap;
deba@430
   406
deba@432
   407
    typedef SubDigraphBase Adaptor;
deba@430
   408
    typedef DigraphAdaptorBase<_Digraph> Parent;
deba@430
   409
  protected:
deba@430
   410
    NodeFilterMap* _node_filter;
deba@430
   411
    ArcFilterMap* _arc_filter;
deba@432
   412
    SubDigraphBase()
deba@430
   413
      : Parent(), _node_filter(0), _arc_filter(0) { }
deba@430
   414
deba@430
   415
    void setNodeFilterMap(NodeFilterMap& node_filter) {
deba@430
   416
      _node_filter = &node_filter;
deba@430
   417
    }
deba@430
   418
    void setArcFilterMap(ArcFilterMap& arc_filter) {
deba@430
   419
      _arc_filter = &arc_filter;
deba@430
   420
    }
deba@430
   421
deba@430
   422
  public:
deba@430
   423
deba@430
   424
    typedef typename Parent::Node Node;
deba@430
   425
    typedef typename Parent::Arc Arc;
deba@430
   426
deba@432
   427
    void first(Node& i) const {
deba@432
   428
      Parent::first(i);
deba@432
   429
      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
deba@430
   430
    }
deba@430
   431
deba@432
   432
    void first(Arc& i) const {
deba@432
   433
      Parent::first(i);
deba@432
   434
      while (i != INVALID && (!(*_arc_filter)[i]
deba@432
   435
                              || !(*_node_filter)[Parent::source(i)]
deba@432
   436
                              || !(*_node_filter)[Parent::target(i)]))
deba@432
   437
        Parent::next(i);
deba@430
   438
    }
deba@430
   439
deba@432
   440
    void firstIn(Arc& i, const Node& n) const {
deba@432
   441
      Parent::firstIn(i, n);
deba@432
   442
      while (i != INVALID && (!(*_arc_filter)[i]
deba@432
   443
                              || !(*_node_filter)[Parent::source(i)]))
deba@432
   444
        Parent::nextIn(i);
deba@430
   445
    }
deba@430
   446
deba@432
   447
    void firstOut(Arc& i, const Node& n) const {
deba@432
   448
      Parent::firstOut(i, n);
deba@432
   449
      while (i != INVALID && (!(*_arc_filter)[i]
deba@432
   450
                              || !(*_node_filter)[Parent::target(i)]))
deba@432
   451
        Parent::nextOut(i);
deba@430
   452
    }
deba@430
   453
deba@432
   454
    void next(Node& i) const {
deba@432
   455
      Parent::next(i);
deba@432
   456
      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
deba@430
   457
    }
deba@430
   458
deba@432
   459
    void next(Arc& i) const {
deba@432
   460
      Parent::next(i);
deba@432
   461
      while (i != INVALID && (!(*_arc_filter)[i]
deba@432
   462
                              || !(*_node_filter)[Parent::source(i)]
deba@432
   463
                              || !(*_node_filter)[Parent::target(i)]))
deba@432
   464
        Parent::next(i);
deba@430
   465
    }
deba@430
   466
deba@432
   467
    void nextIn(Arc& i) const {
deba@432
   468
      Parent::nextIn(i);
deba@432
   469
      while (i != INVALID && (!(*_arc_filter)[i]
deba@432
   470
                              || !(*_node_filter)[Parent::source(i)]))
deba@432
   471
        Parent::nextIn(i);
deba@430
   472
    }
deba@430
   473
deba@432
   474
    void nextOut(Arc& i) const {
deba@432
   475
      Parent::nextOut(i);
deba@432
   476
      while (i != INVALID && (!(*_arc_filter)[i]
deba@432
   477
                              || !(*_node_filter)[Parent::target(i)]))
deba@432
   478
        Parent::nextOut(i);
deba@430
   479
    }
deba@430
   480
kpeter@475
   481
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
kpeter@475
   482
    void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
kpeter@475
   483
kpeter@475
   484
    bool status(const Node& n) const { return (*_node_filter)[n]; }
kpeter@475
   485
    bool status(const Arc& a) const { return (*_arc_filter)[a]; }
deba@430
   486
deba@430
   487
    typedef False NodeNumTag;
kpeter@469
   488
    typedef False ArcNumTag;
kpeter@469
   489
kpeter@469
   490
    typedef FindArcTagIndicator<Digraph> FindArcTag;
deba@432
   491
    Arc findArc(const Node& source, const Node& target,
kpeter@471
   492
                const Arc& prev = INVALID) const {
deba@430
   493
      if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
deba@430
   494
        return INVALID;
deba@430
   495
      }
deba@430
   496
      Arc arc = Parent::findArc(source, target, prev);
deba@430
   497
      while (arc != INVALID && !(*_arc_filter)[arc]) {
deba@430
   498
        arc = Parent::findArc(source, target, arc);
deba@430
   499
      }
deba@430
   500
      return arc;
deba@430
   501
    }
deba@430
   502
deba@430
   503
    template <typename _Value>
deba@432
   504
    class NodeMap : public SubMapExtender<Adaptor,
deba@432
   505
      typename Parent::template NodeMap<_Value> > {
deba@430
   506
    public:
deba@430
   507
      typedef _Value Value;
deba@430
   508
      typedef SubMapExtender<Adaptor, typename Parent::
deba@430
   509
                             template NodeMap<Value> > MapParent;
deba@432
   510
deba@432
   511
      NodeMap(const Adaptor& adaptor)
deba@432
   512
        : MapParent(adaptor) {}
deba@432
   513
      NodeMap(const Adaptor& adaptor, const Value& value)
deba@432
   514
        : MapParent(adaptor, value) {}
deba@432
   515
deba@430
   516
    private:
deba@430
   517
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
   518
        return operator=<NodeMap>(cmap);
deba@430
   519
      }
deba@432
   520
deba@430
   521
      template <typename CMap>
deba@430
   522
      NodeMap& operator=(const CMap& cmap) {
deba@430
   523
        MapParent::operator=(cmap);
deba@432
   524
        return *this;
deba@430
   525
      }
deba@430
   526
    };
deba@430
   527
deba@430
   528
    template <typename _Value>
deba@432
   529
    class ArcMap : public SubMapExtender<Adaptor,
deba@432
   530
      typename Parent::template ArcMap<_Value> > {
deba@430
   531
    public:
deba@430
   532
      typedef _Value Value;
deba@430
   533
      typedef SubMapExtender<Adaptor, typename Parent::
deba@430
   534
                             template ArcMap<Value> > MapParent;
deba@432
   535
deba@432
   536
      ArcMap(const Adaptor& adaptor)
deba@432
   537
        : MapParent(adaptor) {}
deba@432
   538
      ArcMap(const Adaptor& adaptor, const Value& value)
deba@432
   539
        : MapParent(adaptor, value) {}
deba@432
   540
deba@430
   541
    private:
deba@430
   542
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
   543
        return operator=<ArcMap>(cmap);
deba@430
   544
      }
deba@432
   545
deba@430
   546
      template <typename CMap>
deba@430
   547
      ArcMap& operator=(const CMap& cmap) {
deba@430
   548
        MapParent::operator=(cmap);
deba@432
   549
        return *this;
deba@430
   550
      }
deba@430
   551
    };
deba@430
   552
deba@430
   553
  };
deba@430
   554
deba@430
   555
  template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap>
deba@432
   556
  class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false>
deba@430
   557
    : public DigraphAdaptorBase<_Digraph> {
deba@430
   558
  public:
deba@430
   559
    typedef _Digraph Digraph;
deba@430
   560
    typedef _NodeFilterMap NodeFilterMap;
deba@430
   561
    typedef _ArcFilterMap ArcFilterMap;
deba@430
   562
deba@432
   563
    typedef SubDigraphBase Adaptor;
deba@430
   564
    typedef DigraphAdaptorBase<Digraph> Parent;
deba@430
   565
  protected:
deba@430
   566
    NodeFilterMap* _node_filter;
deba@430
   567
    ArcFilterMap* _arc_filter;
deba@432
   568
    SubDigraphBase()
deba@430
   569
      : Parent(), _node_filter(0), _arc_filter(0) { }
deba@430
   570
deba@430
   571
    void setNodeFilterMap(NodeFilterMap& node_filter) {
deba@430
   572
      _node_filter = &node_filter;
deba@430
   573
    }
deba@430
   574
    void setArcFilterMap(ArcFilterMap& arc_filter) {
deba@430
   575
      _arc_filter = &arc_filter;
deba@430
   576
    }
deba@430
   577
deba@430
   578
  public:
deba@430
   579
deba@430
   580
    typedef typename Parent::Node Node;
deba@430
   581
    typedef typename Parent::Arc Arc;
deba@430
   582
deba@432
   583
    void first(Node& i) const {
deba@432
   584
      Parent::first(i);
deba@432
   585
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
deba@430
   586
    }
deba@430
   587
deba@432
   588
    void first(Arc& i) const {
deba@432
   589
      Parent::first(i);
deba@432
   590
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
deba@430
   591
    }
deba@430
   592
deba@432
   593
    void firstIn(Arc& i, const Node& n) const {
deba@432
   594
      Parent::firstIn(i, n);
deba@432
   595
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
deba@430
   596
    }
deba@430
   597
deba@432
   598
    void firstOut(Arc& i, const Node& n) const {
deba@432
   599
      Parent::firstOut(i, n);
deba@432
   600
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
deba@430
   601
    }
deba@430
   602
deba@432
   603
    void next(Node& i) const {
deba@432
   604
      Parent::next(i);
deba@432
   605
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
deba@430
   606
    }
deba@432
   607
    void next(Arc& i) const {
deba@432
   608
      Parent::next(i);
deba@432
   609
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
deba@430
   610
    }
deba@432
   611
    void nextIn(Arc& i) const {
deba@432
   612
      Parent::nextIn(i);
deba@432
   613
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
deba@430
   614
    }
deba@430
   615
deba@432
   616
    void nextOut(Arc& i) const {
deba@432
   617
      Parent::nextOut(i);
deba@432
   618
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
deba@430
   619
    }
deba@430
   620
kpeter@475
   621
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
kpeter@475
   622
    void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
kpeter@475
   623
kpeter@475
   624
    bool status(const Node& n) const { return (*_node_filter)[n]; }
kpeter@475
   625
    bool status(const Arc& a) const { return (*_arc_filter)[a]; }
deba@430
   626
deba@430
   627
    typedef False NodeNumTag;
kpeter@469
   628
    typedef False ArcNumTag;
kpeter@469
   629
kpeter@469
   630
    typedef FindArcTagIndicator<Digraph> FindArcTag;
deba@432
   631
    Arc findArc(const Node& source, const Node& target,
kpeter@471
   632
                const Arc& prev = INVALID) const {
deba@430
   633
      if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
deba@430
   634
        return INVALID;
deba@430
   635
      }
deba@430
   636
      Arc arc = Parent::findArc(source, target, prev);
deba@430
   637
      while (arc != INVALID && !(*_arc_filter)[arc]) {
deba@430
   638
        arc = Parent::findArc(source, target, arc);
deba@430
   639
      }
deba@430
   640
      return arc;
deba@430
   641
    }
deba@430
   642
deba@430
   643
    template <typename _Value>
deba@432
   644
    class NodeMap : public SubMapExtender<Adaptor,
deba@432
   645
      typename Parent::template NodeMap<_Value> > {
deba@430
   646
    public:
deba@430
   647
      typedef _Value Value;
deba@430
   648
      typedef SubMapExtender<Adaptor, typename Parent::
deba@430
   649
                             template NodeMap<Value> > MapParent;
deba@432
   650
deba@432
   651
      NodeMap(const Adaptor& adaptor)
deba@432
   652
        : MapParent(adaptor) {}
deba@432
   653
      NodeMap(const Adaptor& adaptor, const Value& value)
deba@432
   654
        : MapParent(adaptor, value) {}
deba@432
   655
deba@430
   656
    private:
deba@430
   657
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
   658
        return operator=<NodeMap>(cmap);
deba@430
   659
      }
deba@432
   660
deba@430
   661
      template <typename CMap>
deba@430
   662
      NodeMap& operator=(const CMap& cmap) {
deba@430
   663
        MapParent::operator=(cmap);
deba@432
   664
        return *this;
deba@430
   665
      }
deba@430
   666
    };
deba@430
   667
deba@430
   668
    template <typename _Value>
deba@432
   669
    class ArcMap : public SubMapExtender<Adaptor,
deba@432
   670
      typename Parent::template ArcMap<_Value> > {
deba@430
   671
    public:
deba@430
   672
      typedef _Value Value;
deba@430
   673
      typedef SubMapExtender<Adaptor, typename Parent::
deba@430
   674
                             template ArcMap<Value> > MapParent;
deba@432
   675
deba@432
   676
      ArcMap(const Adaptor& adaptor)
deba@432
   677
        : MapParent(adaptor) {}
deba@432
   678
      ArcMap(const Adaptor& adaptor, const Value& value)
deba@432
   679
        : MapParent(adaptor, value) {}
deba@432
   680
deba@430
   681
    private:
deba@430
   682
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
   683
        return operator=<ArcMap>(cmap);
deba@430
   684
      }
deba@432
   685
deba@430
   686
      template <typename CMap>
deba@430
   687
      ArcMap& operator=(const CMap& cmap) {
deba@430
   688
        MapParent::operator=(cmap);
deba@432
   689
        return *this;
deba@430
   690
      }
deba@430
   691
    };
deba@430
   692
deba@430
   693
  };
deba@430
   694
deba@430
   695
  /// \ingroup graph_adaptors
deba@430
   696
  ///
kpeter@474
   697
  /// \brief Adaptor class for hiding nodes and arcs in a digraph
deba@432
   698
  ///
kpeter@474
   699
  /// SubDigraph can be used for hiding nodes and arcs in a digraph.
kpeter@474
   700
  /// A \c bool node map and a \c bool arc map must be specified, which
kpeter@474
   701
  /// define the filters for nodes and arcs.
kpeter@474
   702
  /// Only the nodes and arcs with \c true filter value are
kpeter@476
   703
  /// shown in the subdigraph. The arcs that are incident to hidden
kpeter@476
   704
  /// nodes are also filtered out.
kpeter@476
   705
  /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept.
deba@432
   706
  ///
kpeter@474
   707
  /// The adapted digraph can also be modified through this adaptor
kpeter@476
   708
  /// by adding or removing nodes or arcs, unless the \c GR template
kpeter@474
   709
  /// parameter is set to be \c const.
kpeter@474
   710
  ///
kpeter@476
   711
  /// \tparam GR The type of the adapted digraph.
kpeter@474
   712
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
kpeter@474
   713
  /// It can also be specified to be \c const.
kpeter@476
   714
  /// \tparam NF The type of the node filter map.
kpeter@476
   715
  /// It must be a \c bool (or convertible) node map of the
kpeter@476
   716
  /// adapted digraph. The default type is
kpeter@476
   717
  /// \ref concepts::Digraph::NodeMap "GR::NodeMap<bool>".
kpeter@476
   718
  /// \tparam AF The type of the arc filter map.
kpeter@476
   719
  /// It must be \c bool (or convertible) arc map of the
kpeter@476
   720
  /// adapted digraph. The default type is
kpeter@476
   721
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<bool>".
kpeter@474
   722
  ///
kpeter@474
   723
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
kpeter@474
   724
  /// digraph are convertible to each other.
deba@432
   725
  ///
deba@432
   726
  /// \see FilterNodes
deba@432
   727
  /// \see FilterArcs
kpeter@474
   728
#ifdef DOXYGEN
kpeter@476
   729
  template<typename GR, typename NF, typename AF>
kpeter@476
   730
  class SubDigraph {
kpeter@474
   731
#else
kpeter@476
   732
  template<typename GR,
kpeter@476
   733
           typename NF = typename GR::template NodeMap<bool>,
kpeter@476
   734
           typename AF = typename GR::template ArcMap<bool> >
kpeter@476
   735
  class SubDigraph :
kpeter@476
   736
    public DigraphAdaptorExtender<SubDigraphBase<GR, NF, AF, true> > {
kpeter@474
   737
#endif
deba@430
   738
  public:
kpeter@474
   739
    /// The type of the adapted digraph.
kpeter@476
   740
    typedef GR Digraph;
kpeter@474
   741
    /// The type of the node filter map.
kpeter@476
   742
    typedef NF NodeFilterMap;
kpeter@474
   743
    /// The type of the arc filter map.
kpeter@476
   744
    typedef AF ArcFilterMap;
kpeter@476
   745
kpeter@476
   746
    typedef DigraphAdaptorExtender<SubDigraphBase<GR, NF, AF, true> >
kpeter@476
   747
      Parent;
deba@430
   748
deba@431
   749
    typedef typename Parent::Node Node;
deba@431
   750
    typedef typename Parent::Arc Arc;
deba@431
   751
deba@430
   752
  protected:
deba@432
   753
    SubDigraph() { }
deba@430
   754
  public:
deba@430
   755
deba@431
   756
    /// \brief Constructor
deba@431
   757
    ///
kpeter@474
   758
    /// Creates a subdigraph for the given digraph with the
kpeter@474
   759
    /// given node and arc filter maps.
deba@432
   760
    SubDigraph(Digraph& digraph, NodeFilterMap& node_filter,
deba@432
   761
               ArcFilterMap& arc_filter) {
deba@430
   762
      setDigraph(digraph);
deba@430
   763
      setNodeFilterMap(node_filter);
deba@430
   764
      setArcFilterMap(arc_filter);
deba@430
   765
    }
deba@430
   766
kpeter@475
   767
    /// \brief Sets the status of the given node
deba@431
   768
    ///
kpeter@475
   769
    /// This function sets the status of the given node.
kpeter@474
   770
    /// It is done by simply setting the assigned value of \c n
kpeter@475
   771
    /// to \c v in the node filter map.
kpeter@475
   772
    void status(const Node& n, bool v) const { Parent::status(n, v); }
kpeter@475
   773
kpeter@475
   774
    /// \brief Sets the status of the given arc
deba@431
   775
    ///
kpeter@475
   776
    /// This function sets the status of the given arc.
kpeter@474
   777
    /// It is done by simply setting the assigned value of \c a
kpeter@475
   778
    /// to \c v in the arc filter map.
kpeter@475
   779
    void status(const Arc& a, bool v) const { Parent::status(a, v); }
kpeter@475
   780
kpeter@475
   781
    /// \brief Returns the status of the given node
deba@431
   782
    ///
kpeter@475
   783
    /// This function returns the status of the given node.
kpeter@475
   784
    /// It is \c true if the given node is enabled (i.e. not hidden).
kpeter@475
   785
    bool status(const Node& n) const { return Parent::status(n); }
kpeter@475
   786
kpeter@475
   787
    /// \brief Returns the status of the given arc
deba@431
   788
    ///
kpeter@475
   789
    /// This function returns the status of the given arc.
kpeter@475
   790
    /// It is \c true if the given arc is enabled (i.e. not hidden).
kpeter@475
   791
    bool status(const Arc& a) const { return Parent::status(a); }
kpeter@475
   792
kpeter@475
   793
    /// \brief Disables the given node
deba@431
   794
    ///
kpeter@475
   795
    /// This function disables the given node in the subdigraph,
kpeter@475
   796
    /// so the iteration jumps over it.
kpeter@475
   797
    /// It is the same as \ref status() "status(n, false)".
kpeter@475
   798
    void disable(const Node& n) const { Parent::status(n, false); }
kpeter@475
   799
kpeter@475
   800
    /// \brief Disables the given arc
deba@431
   801
    ///
kpeter@475
   802
    /// This function disables the given arc in the subdigraph,
kpeter@475
   803
    /// so the iteration jumps over it.
kpeter@475
   804
    /// It is the same as \ref status() "status(a, false)".
kpeter@475
   805
    void disable(const Arc& a) const { Parent::status(a, false); }
kpeter@475
   806
kpeter@475
   807
    /// \brief Enables the given node
kpeter@475
   808
    ///
kpeter@475
   809
    /// This function enables the given node in the subdigraph.
kpeter@475
   810
    /// It is the same as \ref status() "status(n, true)".
kpeter@475
   811
    void enable(const Node& n) const { Parent::status(n, true); }
kpeter@475
   812
kpeter@475
   813
    /// \brief Enables the given arc
kpeter@475
   814
    ///
kpeter@475
   815
    /// This function enables the given arc in the subdigraph.
kpeter@475
   816
    /// It is the same as \ref status() "status(a, true)".
kpeter@475
   817
    void enable(const Arc& a) const { Parent::status(a, true); }
deba@431
   818
deba@430
   819
  };
deba@430
   820
kpeter@474
   821
  /// \brief Returns a read-only SubDigraph adaptor
deba@430
   822
  ///
kpeter@474
   823
  /// This function just returns a read-only \ref SubDigraph adaptor.
kpeter@474
   824
  /// \ingroup graph_adaptors
kpeter@474
   825
  /// \relates SubDigraph
kpeter@476
   826
  template<typename GR, typename NF, typename AF>
kpeter@476
   827
  SubDigraph<const GR, NF, AF>
kpeter@476
   828
  subDigraph(const GR& digraph,
kpeter@476
   829
             NF& node_filter_map, AF& arc_filter_map) {
kpeter@476
   830
    return SubDigraph<const GR, NF, AF>
kpeter@476
   831
      (digraph, node_filter_map, arc_filter_map);
deba@430
   832
  }
deba@430
   833
kpeter@476
   834
  template<typename GR, typename NF, typename AF>
kpeter@476
   835
  SubDigraph<const GR, const NF, AF>
kpeter@476
   836
  subDigraph(const GR& digraph,
kpeter@476
   837
             const NF& node_filter_map, AF& arc_filter_map) {
kpeter@476
   838
    return SubDigraph<const GR, const NF, AF>
kpeter@476
   839
      (digraph, node_filter_map, arc_filter_map);
deba@430
   840
  }
deba@430
   841
kpeter@476
   842
  template<typename GR, typename NF, typename AF>
kpeter@476
   843
  SubDigraph<const GR, NF, const AF>
kpeter@476
   844
  subDigraph(const GR& digraph,
kpeter@476
   845
             NF& node_filter_map, const AF& arc_filter_map) {
kpeter@476
   846
    return SubDigraph<const GR, NF, const AF>
kpeter@476
   847
      (digraph, node_filter_map, arc_filter_map);
deba@430
   848
  }
deba@430
   849
kpeter@476
   850
  template<typename GR, typename NF, typename AF>
kpeter@476
   851
  SubDigraph<const GR, const NF, const AF>
kpeter@476
   852
  subDigraph(const GR& digraph,
kpeter@476
   853
             const NF& node_filter_map, const AF& arc_filter_map) {
kpeter@476
   854
    return SubDigraph<const GR, const NF, const AF>
kpeter@476
   855
      (digraph, node_filter_map, arc_filter_map);
deba@430
   856
  }
deba@430
   857
deba@430
   858
kpeter@472
   859
  template <typename _Graph, typename _NodeFilterMap,
kpeter@472
   860
            typename _EdgeFilterMap, bool _checked = true>
deba@432
   861
  class SubGraphBase : public GraphAdaptorBase<_Graph> {
deba@432
   862
  public:
deba@432
   863
    typedef _Graph Graph;
kpeter@472
   864
    typedef _NodeFilterMap NodeFilterMap;
kpeter@472
   865
    typedef _EdgeFilterMap EdgeFilterMap;
kpeter@472
   866
deba@432
   867
    typedef SubGraphBase Adaptor;
deba@432
   868
    typedef GraphAdaptorBase<_Graph> Parent;
deba@432
   869
  protected:
deba@432
   870
deba@432
   871
    NodeFilterMap* _node_filter_map;
deba@432
   872
    EdgeFilterMap* _edge_filter_map;
deba@432
   873
deba@432
   874
    SubGraphBase()
deba@432
   875
      : Parent(), _node_filter_map(0), _edge_filter_map(0) { }
deba@432
   876
deba@432
   877
    void setNodeFilterMap(NodeFilterMap& node_filter_map) {
deba@432
   878
      _node_filter_map=&node_filter_map;
deba@432
   879
    }
deba@432
   880
    void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
deba@432
   881
      _edge_filter_map=&edge_filter_map;
deba@432
   882
    }
deba@432
   883
deba@432
   884
  public:
deba@432
   885
deba@432
   886
    typedef typename Parent::Node Node;
deba@432
   887
    typedef typename Parent::Arc Arc;
deba@432
   888
    typedef typename Parent::Edge Edge;
deba@432
   889
deba@432
   890
    void first(Node& i) const {
deba@432
   891
      Parent::first(i);
deba@432
   892
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
deba@432
   893
    }
deba@432
   894
deba@432
   895
    void first(Arc& i) const {
deba@432
   896
      Parent::first(i);
deba@432
   897
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   898
                            || !(*_node_filter_map)[Parent::source(i)]
deba@432
   899
                            || !(*_node_filter_map)[Parent::target(i)]))
deba@432
   900
        Parent::next(i);
deba@432
   901
    }
deba@432
   902
deba@432
   903
    void first(Edge& i) const {
deba@432
   904
      Parent::first(i);
deba@432
   905
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   906
                            || !(*_node_filter_map)[Parent::u(i)]
deba@432
   907
                            || !(*_node_filter_map)[Parent::v(i)]))
deba@432
   908
        Parent::next(i);
deba@432
   909
    }
deba@432
   910
deba@432
   911
    void firstIn(Arc& i, const Node& n) const {
deba@432
   912
      Parent::firstIn(i, n);
deba@432
   913
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   914
                            || !(*_node_filter_map)[Parent::source(i)]))
deba@432
   915
        Parent::nextIn(i);
deba@432
   916
    }
deba@432
   917
deba@432
   918
    void firstOut(Arc& i, const Node& n) const {
deba@432
   919
      Parent::firstOut(i, n);
deba@432
   920
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   921
                            || !(*_node_filter_map)[Parent::target(i)]))
deba@432
   922
        Parent::nextOut(i);
deba@432
   923
    }
deba@432
   924
deba@432
   925
    void firstInc(Edge& i, bool& d, const Node& n) const {
deba@432
   926
      Parent::firstInc(i, d, n);
deba@432
   927
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   928
                            || !(*_node_filter_map)[Parent::u(i)]
deba@432
   929
                            || !(*_node_filter_map)[Parent::v(i)]))
deba@432
   930
        Parent::nextInc(i, d);
deba@432
   931
    }
deba@432
   932
deba@432
   933
    void next(Node& i) const {
deba@432
   934
      Parent::next(i);
deba@432
   935
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
deba@432
   936
    }
deba@432
   937
deba@432
   938
    void next(Arc& i) const {
deba@432
   939
      Parent::next(i);
deba@432
   940
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   941
                            || !(*_node_filter_map)[Parent::source(i)]
deba@432
   942
                            || !(*_node_filter_map)[Parent::target(i)]))
deba@432
   943
        Parent::next(i);
deba@432
   944
    }
deba@432
   945
deba@432
   946
    void next(Edge& i) const {
deba@432
   947
      Parent::next(i);
deba@432
   948
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   949
                            || !(*_node_filter_map)[Parent::u(i)]
deba@432
   950
                            || !(*_node_filter_map)[Parent::v(i)]))
deba@432
   951
        Parent::next(i);
deba@432
   952
    }
deba@432
   953
deba@432
   954
    void nextIn(Arc& i) const {
deba@432
   955
      Parent::nextIn(i);
deba@432
   956
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   957
                            || !(*_node_filter_map)[Parent::source(i)]))
deba@432
   958
        Parent::nextIn(i);
deba@432
   959
    }
deba@432
   960
deba@432
   961
    void nextOut(Arc& i) const {
deba@432
   962
      Parent::nextOut(i);
deba@432
   963
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   964
                            || !(*_node_filter_map)[Parent::target(i)]))
deba@432
   965
        Parent::nextOut(i);
deba@432
   966
    }
deba@432
   967
deba@432
   968
    void nextInc(Edge& i, bool& d) const {
deba@432
   969
      Parent::nextInc(i, d);
deba@432
   970
      while (i!=INVALID && (!(*_edge_filter_map)[i]
deba@432
   971
                            || !(*_node_filter_map)[Parent::u(i)]
deba@432
   972
                            || !(*_node_filter_map)[Parent::v(i)]))
deba@432
   973
        Parent::nextInc(i, d);
deba@432
   974
    }
deba@432
   975
kpeter@475
   976
    void status(const Node& n, bool v) const { _node_filter_map->set(n, v); }
kpeter@475
   977
    void status(const Edge& e, bool v) const { _edge_filter_map->set(e, v); }
kpeter@475
   978
kpeter@475
   979
    bool status(const Node& n) const { return (*_node_filter_map)[n]; }
kpeter@475
   980
    bool status(const Edge& e) const { return (*_edge_filter_map)[e]; }
deba@432
   981
deba@432
   982
    typedef False NodeNumTag;
kpeter@469
   983
    typedef False ArcNumTag;
deba@432
   984
    typedef False EdgeNumTag;
deba@432
   985
kpeter@469
   986
    typedef FindArcTagIndicator<Graph> FindArcTag;
deba@432
   987
    Arc findArc(const Node& u, const Node& v,
kpeter@471
   988
                const Arc& prev = INVALID) const {
deba@432
   989
      if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
deba@432
   990
        return INVALID;
deba@432
   991
      }
deba@432
   992
      Arc arc = Parent::findArc(u, v, prev);
deba@432
   993
      while (arc != INVALID && !(*_edge_filter_map)[arc]) {
deba@432
   994
        arc = Parent::findArc(u, v, arc);
deba@432
   995
      }
deba@432
   996
      return arc;
deba@432
   997
    }
kpeter@469
   998
kpeter@469
   999
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@432
  1000
    Edge findEdge(const Node& u, const Node& v,
kpeter@471
  1001
                  const Edge& prev = INVALID) const {
deba@432
  1002
      if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
deba@432
  1003
        return INVALID;
deba@432
  1004
      }
deba@432
  1005
      Edge edge = Parent::findEdge(u, v, prev);
deba@432
  1006
      while (edge != INVALID && !(*_edge_filter_map)[edge]) {
deba@432
  1007
        edge = Parent::findEdge(u, v, edge);
deba@432
  1008
      }
deba@432
  1009
      return edge;
deba@432
  1010
    }
deba@432
  1011
deba@432
  1012
    template <typename _Value>
deba@432
  1013
    class NodeMap : public SubMapExtender<Adaptor,
deba@432
  1014
      typename Parent::template NodeMap<_Value> > {
deba@432
  1015
    public:
deba@432
  1016
      typedef _Value Value;
deba@432
  1017
      typedef SubMapExtender<Adaptor, typename Parent::
deba@432
  1018
                             template NodeMap<Value> > MapParent;
deba@432
  1019
deba@432
  1020
      NodeMap(const Adaptor& adaptor)
deba@432
  1021
        : MapParent(adaptor) {}
deba@432
  1022
      NodeMap(const Adaptor& adaptor, const Value& value)
deba@432
  1023
        : MapParent(adaptor, value) {}
deba@432
  1024
deba@432
  1025
    private:
deba@432
  1026
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
  1027
        return operator=<NodeMap>(cmap);
deba@432
  1028
      }
deba@432
  1029
deba@432
  1030
      template <typename CMap>
deba@432
  1031
      NodeMap& operator=(const CMap& cmap) {
deba@432
  1032
        MapParent::operator=(cmap);
deba@432
  1033
        return *this;
deba@432
  1034
      }
deba@432
  1035
    };
deba@432
  1036
deba@432
  1037
    template <typename _Value>
deba@432
  1038
    class ArcMap : public SubMapExtender<Adaptor,
deba@432
  1039
      typename Parent::template ArcMap<_Value> > {
deba@432
  1040
    public:
deba@432
  1041
      typedef _Value Value;
deba@432
  1042
      typedef SubMapExtender<Adaptor, typename Parent::
deba@432
  1043
                             template ArcMap<Value> > MapParent;
deba@432
  1044
deba@432
  1045
      ArcMap(const Adaptor& adaptor)
deba@432
  1046
        : MapParent(adaptor) {}
deba@432
  1047
      ArcMap(const Adaptor& adaptor, const Value& value)
deba@432
  1048
        : MapParent(adaptor, value) {}
deba@432
  1049
deba@432
  1050
    private:
deba@432
  1051
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
  1052
        return operator=<ArcMap>(cmap);
deba@432
  1053
      }
deba@432
  1054
deba@432
  1055
      template <typename CMap>
deba@432
  1056
      ArcMap& operator=(const CMap& cmap) {
deba@432
  1057
        MapParent::operator=(cmap);
deba@432
  1058
        return *this;
deba@432
  1059
      }
deba@432
  1060
    };
deba@432
  1061
deba@432
  1062
    template <typename _Value>
deba@432
  1063
    class EdgeMap : public SubMapExtender<Adaptor,
deba@432
  1064
      typename Parent::template EdgeMap<_Value> > {
deba@432
  1065
    public:
deba@432
  1066
      typedef _Value Value;
deba@432
  1067
      typedef SubMapExtender<Adaptor, typename Parent::
deba@432
  1068
                             template EdgeMap<Value> > MapParent;
deba@432
  1069
deba@432
  1070
      EdgeMap(const Adaptor& adaptor)
deba@432
  1071
        : MapParent(adaptor) {}
deba@432
  1072
deba@432
  1073
      EdgeMap(const Adaptor& adaptor, const Value& value)
deba@432
  1074
        : MapParent(adaptor, value) {}
deba@432
  1075
deba@432
  1076
    private:
deba@432
  1077
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@432
  1078
        return operator=<EdgeMap>(cmap);
deba@432
  1079
      }
deba@432
  1080
deba@432
  1081
      template <typename CMap>
deba@432
  1082
      EdgeMap& operator=(const CMap& cmap) {
deba@432
  1083
        MapParent::operator=(cmap);
deba@432
  1084
        return *this;
deba@432
  1085
      }
deba@432
  1086
    };
deba@432
  1087
deba@432
  1088
  };
deba@432
  1089
kpeter@472
  1090
  template <typename _Graph, typename _NodeFilterMap, typename _EdgeFilterMap>
kpeter@472
  1091
  class SubGraphBase<_Graph, _NodeFilterMap, _EdgeFilterMap, false>
deba@432
  1092
    : public GraphAdaptorBase<_Graph> {
deba@432
  1093
  public:
deba@432
  1094
    typedef _Graph Graph;
kpeter@472
  1095
    typedef _NodeFilterMap NodeFilterMap;
kpeter@472
  1096
    typedef _EdgeFilterMap EdgeFilterMap;
kpeter@472
  1097
deba@432
  1098
    typedef SubGraphBase Adaptor;
deba@432
  1099
    typedef GraphAdaptorBase<_Graph> Parent;
deba@432
  1100
  protected:
deba@432
  1101
    NodeFilterMap* _node_filter_map;
deba@432
  1102
    EdgeFilterMap* _edge_filter_map;
deba@432
  1103
    SubGraphBase() : Parent(),
deba@432
  1104
                     _node_filter_map(0), _edge_filter_map(0) { }
deba@432
  1105
deba@432
  1106
    void setNodeFilterMap(NodeFilterMap& node_filter_map) {
deba@432
  1107
      _node_filter_map=&node_filter_map;
deba@432
  1108
    }
deba@432
  1109
    void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
deba@432
  1110
      _edge_filter_map=&edge_filter_map;
deba@432
  1111
    }
deba@432
  1112
deba@432
  1113
  public:
deba@432
  1114
deba@432
  1115
    typedef typename Parent::Node Node;
deba@432
  1116
    typedef typename Parent::Arc Arc;
deba@432
  1117
    typedef typename Parent::Edge Edge;
deba@432
  1118
deba@432
  1119
    void first(Node& i) const {
deba@432
  1120
      Parent::first(i);
deba@432
  1121
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
deba@432
  1122
    }
deba@432
  1123
deba@432
  1124
    void first(Arc& i) const {
deba@432
  1125
      Parent::first(i);
deba@432
  1126
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
deba@432
  1127
    }
deba@432
  1128
deba@432
  1129
    void first(Edge& i) const {
deba@432
  1130
      Parent::first(i);
deba@432
  1131
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
deba@432
  1132
    }
deba@432
  1133
deba@432
  1134
    void firstIn(Arc& i, const Node& n) const {
deba@432
  1135
      Parent::firstIn(i, n);
deba@432
  1136
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
deba@432
  1137
    }
deba@432
  1138
deba@432
  1139
    void firstOut(Arc& i, const Node& n) const {
deba@432
  1140
      Parent::firstOut(i, n);
deba@432
  1141
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
deba@432
  1142
    }
deba@432
  1143
deba@432
  1144
    void firstInc(Edge& i, bool& d, const Node& n) const {
deba@432
  1145
      Parent::firstInc(i, d, n);
deba@432
  1146
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
deba@432
  1147
    }
deba@432
  1148
deba@432
  1149
    void next(Node& i) const {
deba@432
  1150
      Parent::next(i);
deba@432
  1151
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
deba@432
  1152
    }
deba@432
  1153
    void next(Arc& i) const {
deba@432
  1154
      Parent::next(i);
deba@432
  1155
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
deba@432
  1156
    }
deba@432
  1157
    void next(Edge& i) const {
deba@432
  1158
      Parent::next(i);
deba@432
  1159
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
deba@432
  1160
    }
deba@432
  1161
    void nextIn(Arc& i) const {
deba@432
  1162
      Parent::nextIn(i);
deba@432
  1163
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
deba@432
  1164
    }
deba@432
  1165
deba@432
  1166
    void nextOut(Arc& i) const {
deba@432
  1167
      Parent::nextOut(i);
deba@432
  1168
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
deba@432
  1169
    }
deba@432
  1170
    void nextInc(Edge& i, bool& d) const {
deba@432
  1171
      Parent::nextInc(i, d);
deba@432
  1172
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
deba@432
  1173
    }
deba@432
  1174
kpeter@475
  1175
    void status(const Node& n, bool v) const { _node_filter_map->set(n, v); }
kpeter@475
  1176
    void status(const Edge& e, bool v) const { _edge_filter_map->set(e, v); }
kpeter@475
  1177
kpeter@475
  1178
    bool status(const Node& n) const { return (*_node_filter_map)[n]; }
kpeter@475
  1179
    bool status(const Edge& e) const { return (*_edge_filter_map)[e]; }
deba@432
  1180
deba@432
  1181
    typedef False NodeNumTag;
kpeter@469
  1182
    typedef False ArcNumTag;
deba@432
  1183
    typedef False EdgeNumTag;
deba@432
  1184
kpeter@469
  1185
    typedef FindArcTagIndicator<Graph> FindArcTag;
deba@432
  1186
    Arc findArc(const Node& u, const Node& v,
kpeter@471
  1187
                const Arc& prev = INVALID) const {
deba@432
  1188
      Arc arc = Parent::findArc(u, v, prev);
deba@432
  1189
      while (arc != INVALID && !(*_edge_filter_map)[arc]) {
deba@432
  1190
        arc = Parent::findArc(u, v, arc);
deba@432
  1191
      }
deba@432
  1192
      return arc;
deba@432
  1193
    }
kpeter@469
  1194
kpeter@469
  1195
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@432
  1196
    Edge findEdge(const Node& u, const Node& v,
kpeter@471
  1197
                  const Edge& prev = INVALID) const {
deba@432
  1198
      Edge edge = Parent::findEdge(u, v, prev);
deba@432
  1199
      while (edge != INVALID && !(*_edge_filter_map)[edge]) {
deba@432
  1200
        edge = Parent::findEdge(u, v, edge);
deba@432
  1201
      }
deba@432
  1202
      return edge;
deba@432
  1203
    }
deba@432
  1204
deba@432
  1205
    template <typename _Value>
deba@432
  1206
    class NodeMap : public SubMapExtender<Adaptor,
deba@432
  1207
      typename Parent::template NodeMap<_Value> > {
deba@432
  1208
    public:
deba@432
  1209
      typedef _Value Value;
deba@432
  1210
      typedef SubMapExtender<Adaptor, typename Parent::
deba@432
  1211
                             template NodeMap<Value> > MapParent;
deba@432
  1212
deba@432
  1213
      NodeMap(const Adaptor& adaptor)
deba@432
  1214
        : MapParent(adaptor) {}
deba@432
  1215
      NodeMap(const Adaptor& adaptor, const Value& value)
deba@432
  1216
        : MapParent(adaptor, value) {}
deba@432
  1217
deba@432
  1218
    private:
deba@432
  1219
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
  1220
        return operator=<NodeMap>(cmap);
deba@432
  1221
      }
deba@432
  1222
deba@432
  1223
      template <typename CMap>
deba@432
  1224
      NodeMap& operator=(const CMap& cmap) {
deba@432
  1225
        MapParent::operator=(cmap);
deba@432
  1226
        return *this;
deba@432
  1227
      }
deba@432
  1228
    };
deba@432
  1229
deba@432
  1230
    template <typename _Value>
deba@432
  1231
    class ArcMap : public SubMapExtender<Adaptor,
deba@432
  1232
      typename Parent::template ArcMap<_Value> > {
deba@432
  1233
    public:
deba@432
  1234
      typedef _Value Value;
deba@432
  1235
      typedef SubMapExtender<Adaptor, typename Parent::
deba@432
  1236
                             template ArcMap<Value> > MapParent;
deba@432
  1237
deba@432
  1238
      ArcMap(const Adaptor& adaptor)
deba@432
  1239
        : MapParent(adaptor) {}
deba@432
  1240
      ArcMap(const Adaptor& adaptor, const Value& value)
deba@432
  1241
        : MapParent(adaptor, value) {}
deba@432
  1242
deba@432
  1243
    private:
deba@432
  1244
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
  1245
        return operator=<ArcMap>(cmap);
deba@432
  1246
      }
deba@432
  1247
deba@432
  1248
      template <typename CMap>
deba@432
  1249
      ArcMap& operator=(const CMap& cmap) {
deba@432
  1250
        MapParent::operator=(cmap);
deba@432
  1251
        return *this;
deba@432
  1252
      }
deba@432
  1253
    };
deba@432
  1254
deba@432
  1255
    template <typename _Value>
deba@432
  1256
    class EdgeMap : public SubMapExtender<Adaptor,
deba@432
  1257
      typename Parent::template EdgeMap<_Value> > {
deba@432
  1258
    public:
deba@432
  1259
      typedef _Value Value;
deba@432
  1260
      typedef SubMapExtender<Adaptor, typename Parent::
deba@432
  1261
                             template EdgeMap<Value> > MapParent;
deba@432
  1262
deba@432
  1263
      EdgeMap(const Adaptor& adaptor)
deba@432
  1264
        : MapParent(adaptor) {}
deba@432
  1265
deba@432
  1266
      EdgeMap(const Adaptor& adaptor, const _Value& value)
deba@432
  1267
        : MapParent(adaptor, value) {}
deba@432
  1268
deba@432
  1269
    private:
deba@432
  1270
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@432
  1271
        return operator=<EdgeMap>(cmap);
deba@432
  1272
      }
deba@432
  1273
deba@432
  1274
      template <typename CMap>
deba@432
  1275
      EdgeMap& operator=(const CMap& cmap) {
deba@432
  1276
        MapParent::operator=(cmap);
deba@432
  1277
        return *this;
deba@432
  1278
      }
deba@432
  1279
    };
deba@432
  1280
deba@432
  1281
  };
deba@432
  1282
deba@432
  1283
  /// \ingroup graph_adaptors
deba@430
  1284
  ///
kpeter@474
  1285
  /// \brief Adaptor class for hiding nodes and edges in an undirected
kpeter@474
  1286
  /// graph.
deba@430
  1287
  ///
kpeter@474
  1288
  /// SubGraph can be used for hiding nodes and edges in a graph.
kpeter@474
  1289
  /// A \c bool node map and a \c bool edge map must be specified, which
kpeter@474
  1290
  /// define the filters for nodes and edges.
kpeter@474
  1291
  /// Only the nodes and edges with \c true filter value are
kpeter@476
  1292
  /// shown in the subgraph. The edges that are incident to hidden
kpeter@476
  1293
  /// nodes are also filtered out.
kpeter@476
  1294
  /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
deba@432
  1295
  ///
kpeter@474
  1296
  /// The adapted graph can also be modified through this adaptor
kpeter@476
  1297
  /// by adding or removing nodes or edges, unless the \c GR template
kpeter@474
  1298
  /// parameter is set to be \c const.
kpeter@474
  1299
  ///
kpeter@476
  1300
  /// \tparam GR The type of the adapted graph.
kpeter@474
  1301
  /// It must conform to the \ref concepts::Graph "Graph" concept.
kpeter@474
  1302
  /// It can also be specified to be \c const.
kpeter@476
  1303
  /// \tparam NF The type of the node filter map.
kpeter@476
  1304
  /// It must be a \c bool (or convertible) node map of the
kpeter@476
  1305
  /// adapted graph. The default type is
kpeter@476
  1306
  /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
kpeter@476
  1307
  /// \tparam EF The type of the edge filter map.
kpeter@476
  1308
  /// It must be a \c bool (or convertible) edge map of the
kpeter@476
  1309
  /// adapted graph. The default type is
kpeter@476
  1310
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
kpeter@474
  1311
  ///
kpeter@474
  1312
  /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
kpeter@474
  1313
  /// adapted graph are convertible to each other.
deba@432
  1314
  ///
deba@432
  1315
  /// \see FilterNodes
deba@432
  1316
  /// \see FilterEdges
kpeter@474
  1317
#ifdef DOXYGEN
kpeter@476
  1318
  template<typename GR, typename NF, typename EF>
kpeter@476
  1319
  class SubGraph {
kpeter@474
  1320
#else
kpeter@476
  1321
  template<typename GR,
kpeter@476
  1322
           typename NF = typename GR::template NodeMap<bool>,
kpeter@476
  1323
           typename EF = typename GR::template EdgeMap<bool> >
kpeter@476
  1324
  class SubGraph :
kpeter@476
  1325
    public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > {
kpeter@474
  1326
#endif
deba@430
  1327
  public:
kpeter@474
  1328
    /// The type of the adapted graph.
kpeter@476
  1329
    typedef GR Graph;
kpeter@474
  1330
    /// The type of the node filter map.
kpeter@476
  1331
    typedef NF NodeFilterMap;
kpeter@474
  1332
    /// The type of the edge filter map.
kpeter@476
  1333
    typedef EF EdgeFilterMap;
kpeter@476
  1334
kpeter@476
  1335
    typedef GraphAdaptorExtender< SubGraphBase<GR, NF, EF, true> >
kpeter@476
  1336
      Parent;
deba@430
  1337
deba@431
  1338
    typedef typename Parent::Node Node;
deba@432
  1339
    typedef typename Parent::Edge Edge;
deba@431
  1340
deba@430
  1341
  protected:
deba@432
  1342
    SubGraph() { }
deba@430
  1343
  public:
deba@430
  1344
deba@431
  1345
    /// \brief Constructor
deba@431
  1346
    ///
kpeter@474
  1347
    /// Creates a subgraph for the given graph with the given node
kpeter@474
  1348
    /// and edge filter maps.
kpeter@474
  1349
    SubGraph(Graph& graph, NodeFilterMap& node_filter_map,
deba@432
  1350
             EdgeFilterMap& edge_filter_map) {
kpeter@474
  1351
      setGraph(graph);
deba@432
  1352
      setNodeFilterMap(node_filter_map);
deba@432
  1353
      setEdgeFilterMap(edge_filter_map);
deba@430
  1354
    }
deba@430
  1355
kpeter@475
  1356
    /// \brief Sets the status of the given node
deba@431
  1357
    ///
kpeter@475
  1358
    /// This function sets the status of the given node.
kpeter@474
  1359
    /// It is done by simply setting the assigned value of \c n
kpeter@475
  1360
    /// to \c v in the node filter map.
kpeter@475
  1361
    void status(const Node& n, bool v) const { Parent::status(n, v); }
kpeter@475
  1362
kpeter@475
  1363
    /// \brief Sets the status of the given edge
deba@432
  1364
    ///
kpeter@475
  1365
    /// This function sets the status of the given edge.
kpeter@474
  1366
    /// It is done by simply setting the assigned value of \c e
kpeter@475
  1367
    /// to \c v in the edge filter map.
kpeter@475
  1368
    void status(const Edge& e, bool v) const { Parent::status(e, v); }
kpeter@475
  1369
kpeter@475
  1370
    /// \brief Returns the status of the given node
deba@431
  1371
    ///
kpeter@475
  1372
    /// This function returns the status of the given node.
kpeter@475
  1373
    /// It is \c true if the given node is enabled (i.e. not hidden).
kpeter@475
  1374
    bool status(const Node& n) const { return Parent::status(n); }
kpeter@475
  1375
kpeter@475
  1376
    /// \brief Returns the status of the given edge
deba@432
  1377
    ///
kpeter@475
  1378
    /// This function returns the status of the given edge.
kpeter@475
  1379
    /// It is \c true if the given edge is enabled (i.e. not hidden).
kpeter@475
  1380
    bool status(const Edge& e) const { return Parent::status(e); }
kpeter@475
  1381
kpeter@475
  1382
    /// \brief Disables the given node
deba@431
  1383
    ///
kpeter@475
  1384
    /// This function disables the given node in the subdigraph,
kpeter@475
  1385
    /// so the iteration jumps over it.
kpeter@475
  1386
    /// It is the same as \ref status() "status(n, false)".
kpeter@475
  1387
    void disable(const Node& n) const { Parent::status(n, false); }
kpeter@475
  1388
kpeter@475
  1389
    /// \brief Disables the given edge
deba@431
  1390
    ///
kpeter@475
  1391
    /// This function disables the given edge in the subgraph,
kpeter@475
  1392
    /// so the iteration jumps over it.
kpeter@475
  1393
    /// It is the same as \ref status() "status(e, false)".
kpeter@475
  1394
    void disable(const Edge& e) const { Parent::status(e, false); }
kpeter@475
  1395
kpeter@475
  1396
    /// \brief Enables the given node
kpeter@475
  1397
    ///
kpeter@475
  1398
    /// This function enables the given node in the subdigraph.
kpeter@475
  1399
    /// It is the same as \ref status() "status(n, true)".
kpeter@475
  1400
    void enable(const Node& n) const { Parent::status(n, true); }
kpeter@475
  1401
kpeter@475
  1402
    /// \brief Enables the given edge
kpeter@475
  1403
    ///
kpeter@475
  1404
    /// This function enables the given edge in the subgraph.
kpeter@475
  1405
    /// It is the same as \ref status() "status(e, true)".
kpeter@475
  1406
    void enable(const Edge& e) const { Parent::status(e, true); }
kpeter@475
  1407
deba@430
  1408
  };
deba@430
  1409
kpeter@474
  1410
  /// \brief Returns a read-only SubGraph adaptor
deba@430
  1411
  ///
kpeter@474
  1412
  /// This function just returns a read-only \ref SubGraph adaptor.
kpeter@474
  1413
  /// \ingroup graph_adaptors
kpeter@474
  1414
  /// \relates SubGraph
kpeter@476
  1415
  template<typename GR, typename NF, typename EF>
kpeter@476
  1416
  SubGraph<const GR, NF, EF>
kpeter@476
  1417
  subGraph(const GR& graph,
kpeter@476
  1418
           NF& node_filter_map, EF& edge_filter_map) {
kpeter@476
  1419
    return SubGraph<const GR, NF, EF>
kpeter@476
  1420
      (graph, node_filter_map, edge_filter_map);
deba@432
  1421
  }
deba@432
  1422
kpeter@476
  1423
  template<typename GR, typename NF, typename EF>
kpeter@476
  1424
  SubGraph<const GR, const NF, EF>
kpeter@476
  1425
  subGraph(const GR& graph,
kpeter@476
  1426
           const NF& node_filter_map, EF& edge_filter_map) {
kpeter@476
  1427
    return SubGraph<const GR, const NF, EF>
kpeter@476
  1428
      (graph, node_filter_map, edge_filter_map);
deba@432
  1429
  }
deba@432
  1430
kpeter@476
  1431
  template<typename GR, typename NF, typename EF>
kpeter@476
  1432
  SubGraph<const GR, NF, const EF>
kpeter@476
  1433
  subGraph(const GR& graph,
kpeter@476
  1434
           NF& node_filter_map, const EF& edge_filter_map) {
kpeter@476
  1435
    return SubGraph<const GR, NF, const EF>
kpeter@476
  1436
      (graph, node_filter_map, edge_filter_map);
deba@432
  1437
  }
deba@432
  1438
kpeter@476
  1439
  template<typename GR, typename NF, typename EF>
kpeter@476
  1440
  SubGraph<const GR, const NF, const EF>
kpeter@476
  1441
  subGraph(const GR& graph,
kpeter@476
  1442
           const NF& node_filter_map, const EF& edge_filter_map) {
kpeter@476
  1443
    return SubGraph<const GR, const NF, const EF>
kpeter@476
  1444
      (graph, node_filter_map, edge_filter_map);
deba@432
  1445
  }
deba@432
  1446
kpeter@474
  1447
deba@432
  1448
  /// \ingroup graph_adaptors
deba@432
  1449
  ///
kpeter@474
  1450
  /// \brief Adaptor class for hiding nodes in a digraph or a graph.
deba@432
  1451
  ///
kpeter@474
  1452
  /// FilterNodes adaptor can be used for hiding nodes in a digraph or a
kpeter@474
  1453
  /// graph. A \c bool node map must be specified, which defines the filter
kpeter@474
  1454
  /// for the nodes. Only the nodes with \c true filter value and the
kpeter@474
  1455
  /// arcs/edges incident to nodes both with \c true filter value are shown
kpeter@474
  1456
  /// in the subgraph. This adaptor conforms to the \ref concepts::Digraph
kpeter@474
  1457
  /// "Digraph" concept or the \ref concepts::Graph "Graph" concept
kpeter@476
  1458
  /// depending on the \c GR template parameter.
deba@432
  1459
  ///
kpeter@474
  1460
  /// The adapted (di)graph can also be modified through this adaptor
kpeter@476
  1461
  /// by adding or removing nodes or arcs/edges, unless the \c GR template
kpeter@474
  1462
  /// parameter is set to be \c const.
kpeter@474
  1463
  ///
kpeter@476
  1464
  /// \tparam GR The type of the adapted digraph or graph.
kpeter@474
  1465
  /// It must conform to the \ref concepts::Digraph "Digraph" concept
kpeter@474
  1466
  /// or the \ref concepts::Graph "Graph" concept.
kpeter@474
  1467
  /// It can also be specified to be \c const.
kpeter@476
  1468
  /// \tparam NF The type of the node filter map.
kpeter@476
  1469
  /// It must be a \c bool (or convertible) node map of the
kpeter@476
  1470
  /// adapted (di)graph. The default type is
kpeter@476
  1471
  /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
kpeter@474
  1472
  ///
kpeter@474
  1473
  /// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the
kpeter@474
  1474
  /// adapted (di)graph are convertible to each other.
deba@432
  1475
#ifdef DOXYGEN
kpeter@476
  1476
  template<typename GR, typename NF>
kpeter@476
  1477
  class FilterNodes {
deba@432
  1478
#else
kpeter@476
  1479
  template<typename GR,
kpeter@476
  1480
           typename NF = typename GR::template NodeMap<bool>,
deba@432
  1481
           typename Enable = void>
kpeter@476
  1482
  class FilterNodes :
kpeter@476
  1483
    public DigraphAdaptorExtender<
kpeter@476
  1484
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, bool>, true> > {
deba@432
  1485
#endif
deba@432
  1486
  public:
deba@432
  1487
kpeter@476
  1488
    typedef GR Digraph;
kpeter@476
  1489
    typedef NF NodeFilterMap;
kpeter@476
  1490
kpeter@476
  1491
    typedef DigraphAdaptorExtender<
kpeter@476
  1492
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, bool>, true> >
kpeter@476
  1493
      Parent;
deba@432
  1494
deba@432
  1495
    typedef typename Parent::Node Node;
deba@432
  1496
deba@432
  1497
  protected:
deba@432
  1498
    ConstMap<typename Digraph::Arc, bool> const_true_map;
deba@432
  1499
deba@432
  1500
    FilterNodes() : const_true_map(true) {
deba@432
  1501
      Parent::setArcFilterMap(const_true_map);
deba@432
  1502
    }
deba@432
  1503
deba@432
  1504
  public:
deba@432
  1505
deba@432
  1506
    /// \brief Constructor
deba@432
  1507
    ///
kpeter@474
  1508
    /// Creates a subgraph for the given digraph or graph with the
deba@432
  1509
    /// given node filter map.
kpeter@476
  1510
    FilterNodes(GR& graph, NodeFilterMap& node_filter) :
kpeter@476
  1511
      Parent(), const_true_map(true)
kpeter@476
  1512
    {
kpeter@474
  1513
      Parent::setDigraph(graph);
deba@432
  1514
      Parent::setNodeFilterMap(node_filter);
deba@432
  1515
      Parent::setArcFilterMap(const_true_map);
deba@432
  1516
    }
deba@432
  1517
kpeter@475
  1518
    /// \brief Sets the status of the given node
deba@432
  1519
    ///
kpeter@475
  1520
    /// This function sets the status of the given node.
kpeter@474
  1521
    /// It is done by simply setting the assigned value of \c n
kpeter@475
  1522
    /// to \c v in the node filter map.
kpeter@475
  1523
    void status(const Node& n, bool v) const { Parent::status(n, v); }
kpeter@475
  1524
kpeter@475
  1525
    /// \brief Returns the status of the given node
deba@432
  1526
    ///
kpeter@475
  1527
    /// This function returns the status of the given node.
kpeter@475
  1528
    /// It is \c true if the given node is enabled (i.e. not hidden).
kpeter@475
  1529
    bool status(const Node& n) const { return Parent::status(n); }
kpeter@475
  1530
kpeter@475
  1531
    /// \brief Disables the given node
deba@432
  1532
    ///
kpeter@475
  1533
    /// This function disables the given node, so the iteration
kpeter@475
  1534
    /// jumps over it.
kpeter@475
  1535
    /// It is the same as \ref status() "status(n, false)".
kpeter@475
  1536
    void disable(const Node& n) const { Parent::status(n, false); }
kpeter@475
  1537
kpeter@475
  1538
    /// \brief Enables the given node
kpeter@475
  1539
    ///
kpeter@475
  1540
    /// This function enables the given node.
kpeter@475
  1541
    /// It is the same as \ref status() "status(n, true)".
kpeter@475
  1542
    void enable(const Node& n) const { Parent::status(n, true); }
deba@432
  1543
deba@432
  1544
  };
deba@432
  1545
kpeter@476
  1546
  template<typename GR, typename NF>
kpeter@476
  1547
  class FilterNodes<GR, NF,
kpeter@476
  1548
                    typename enable_if<UndirectedTagIndicator<GR> >::type> :
kpeter@476
  1549
    public GraphAdaptorExtender<
kpeter@476
  1550
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, bool>, true> > {
kpeter@476
  1551
deba@432
  1552
  public:
kpeter@476
  1553
    typedef GR Graph;
kpeter@476
  1554
    typedef NF NodeFilterMap;
kpeter@476
  1555
    typedef GraphAdaptorExtender<
kpeter@476
  1556
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, bool>, true> >
kpeter@476
  1557
      Parent;
deba@432
  1558
deba@432
  1559
    typedef typename Parent::Node Node;
deba@432
  1560
  protected:
deba@432
  1561
    ConstMap<typename Graph::Edge, bool> const_true_map;
deba@432
  1562
deba@432
  1563
    FilterNodes() : const_true_map(true) {
deba@432
  1564
      Parent::setEdgeFilterMap(const_true_map);
deba@432
  1565
    }
deba@432
  1566
deba@432
  1567
  public:
deba@432
  1568
deba@432
  1569
    FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) :
deba@432
  1570
      Parent(), const_true_map(true) {
deba@432
  1571
      Parent::setGraph(_graph);
deba@432
  1572
      Parent::setNodeFilterMap(node_filter_map);
deba@432
  1573
      Parent::setEdgeFilterMap(const_true_map);
deba@432
  1574
    }
deba@432
  1575
kpeter@475
  1576
    void status(const Node& n, bool v) const { Parent::status(n, v); }
kpeter@475
  1577
    bool status(const Node& n) const { return Parent::status(n); }
kpeter@475
  1578
    void disable(const Node& n) const { Parent::status(n, false); }
kpeter@475
  1579
    void enable(const Node& n) const { Parent::status(n, true); }
deba@432
  1580
deba@432
  1581
  };
deba@432
  1582
deba@432
  1583
kpeter@474
  1584
  /// \brief Returns a read-only FilterNodes adaptor
deba@432
  1585
  ///
kpeter@474
  1586
  /// This function just returns a read-only \ref FilterNodes adaptor.
kpeter@474
  1587
  /// \ingroup graph_adaptors
kpeter@474
  1588
  /// \relates FilterNodes
kpeter@476
  1589
  template<typename GR, typename NF>
kpeter@476
  1590
  FilterNodes<const GR, NF>
kpeter@476
  1591
  filterNodes(const GR& graph, NF& node_filter_map) {
kpeter@476
  1592
    return FilterNodes<const GR, NF>(graph, node_filter_map);
deba@430
  1593
  }
deba@430
  1594
kpeter@476
  1595
  template<typename GR, typename NF>
kpeter@476
  1596
  FilterNodes<const GR, const NF>
kpeter@476
  1597
  filterNodes(const GR& graph, const NF& node_filter_map) {
kpeter@476
  1598
    return FilterNodes<const GR, const NF>(graph, node_filter_map);
deba@430
  1599
  }
deba@430
  1600
deba@432
  1601
  /// \ingroup graph_adaptors
deba@430
  1602
  ///
kpeter@474
  1603
  /// \brief Adaptor class for hiding arcs in a digraph.
deba@430
  1604
  ///
kpeter@474
  1605
  /// FilterArcs adaptor can be used for hiding arcs in a digraph.
kpeter@474
  1606
  /// A \c bool arc map must be specified, which defines the filter for
kpeter@474
  1607
  /// the arcs. Only the arcs with \c true filter value are shown in the
kpeter@474
  1608
  /// subdigraph. This adaptor conforms to the \ref concepts::Digraph
kpeter@474
  1609
  /// "Digraph" concept.
deba@430
  1610
  ///
kpeter@474
  1611
  /// The adapted digraph can also be modified through this adaptor
kpeter@476
  1612
  /// by adding or removing nodes or arcs, unless the \c GR template
kpeter@474
  1613
  /// parameter is set to be \c const.
kpeter@474
  1614
  ///
kpeter@476
  1615
  /// \tparam GR The type of the adapted digraph.
kpeter@474
  1616
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
kpeter@474
  1617
  /// It can also be specified to be \c const.
kpeter@476
  1618
  /// \tparam AF The type of the arc filter map.
kpeter@476
  1619
  /// It must be a \c bool (or convertible) arc map of the
kpeter@476
  1620
  /// adapted digraph. The default type is
kpeter@476
  1621
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<bool>".
kpeter@474
  1622
  ///
kpeter@474
  1623
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
kpeter@474
  1624
  /// digraph are convertible to each other.
kpeter@474
  1625
#ifdef DOXYGEN
kpeter@476
  1626
  template<typename GR,
kpeter@476
  1627
           typename AF>
kpeter@476
  1628
  class FilterArcs {
kpeter@474
  1629
#else
kpeter@476
  1630
  template<typename GR,
kpeter@476
  1631
           typename AF = typename GR::template ArcMap<bool> >
kpeter@476
  1632
  class FilterArcs :
kpeter@476
  1633
    public DigraphAdaptorExtender<
kpeter@476
  1634
      SubDigraphBase<GR, ConstMap<typename GR::Node, bool>, AF, false> > {
kpeter@474
  1635
#endif
deba@430
  1636
  public:
kpeter@476
  1637
    /// The type of the adapted digraph.
kpeter@476
  1638
    typedef GR Digraph;
kpeter@476
  1639
    /// The type of the arc filter map.
kpeter@476
  1640
    typedef AF ArcFilterMap;
kpeter@476
  1641
kpeter@476
  1642
    typedef DigraphAdaptorExtender<
kpeter@476
  1643
      SubDigraphBase<GR, ConstMap<typename GR::Node, bool>, AF, false> >
kpeter@476
  1644
      Parent;
deba@431
  1645
deba@431
  1646
    typedef typename Parent::Arc Arc;
deba@431
  1647
deba@430
  1648
  protected:
deba@430
  1649
    ConstMap<typename Digraph::Node, bool> const_true_map;
deba@430
  1650
deba@432
  1651
    FilterArcs() : const_true_map(true) {
deba@430
  1652
      Parent::setNodeFilterMap(const_true_map);
deba@430
  1653
    }
deba@430
  1654
deba@430
  1655
  public:
deba@430
  1656
deba@431
  1657
    /// \brief Constructor
deba@431
  1658
    ///
kpeter@474
  1659
    /// Creates a subdigraph for the given digraph with the given arc
kpeter@474
  1660
    /// filter map.
deba@432
  1661
    FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter)
deba@432
  1662
      : Parent(), const_true_map(true) {
deba@430
  1663
      Parent::setDigraph(digraph);
deba@430
  1664
      Parent::setNodeFilterMap(const_true_map);
deba@430
  1665
      Parent::setArcFilterMap(arc_filter);
deba@430
  1666
    }
deba@430
  1667
kpeter@475
  1668
    /// \brief Sets the status of the given arc
deba@431
  1669
    ///
kpeter@475
  1670
    /// This function sets the status of the given arc.
kpeter@474
  1671
    /// It is done by simply setting the assigned value of \c a
kpeter@475
  1672
    /// to \c v in the arc filter map.
kpeter@475
  1673
    void status(const Arc& a, bool v) const { Parent::status(a, v); }
kpeter@475
  1674
kpeter@475
  1675
    /// \brief Returns the status of the given arc
deba@431
  1676
    ///
kpeter@475
  1677
    /// This function returns the status of the given arc.
kpeter@475
  1678
    /// It is \c true if the given arc is enabled (i.e. not hidden).
kpeter@475
  1679
    bool status(const Arc& a) const { return Parent::status(a); }
kpeter@475
  1680
kpeter@475
  1681
    /// \brief Disables the given arc
deba@431
  1682
    ///
kpeter@475
  1683
    /// This function disables the given arc in the subdigraph,
kpeter@475
  1684
    /// so the iteration jumps over it.
kpeter@475
  1685
    /// It is the same as \ref status() "status(a, false)".
kpeter@475
  1686
    void disable(const Arc& a) const { Parent::status(a, false); }
kpeter@475
  1687
kpeter@475
  1688
    /// \brief Enables the given arc
kpeter@475
  1689
    ///
kpeter@475
  1690
    /// This function enables the given arc in the subdigraph.
kpeter@475
  1691
    /// It is the same as \ref status() "status(a, true)".
kpeter@475
  1692
    void enable(const Arc& a) const { Parent::status(a, true); }
deba@431
  1693
deba@430
  1694
  };
deba@430
  1695
kpeter@474
  1696
  /// \brief Returns a read-only FilterArcs adaptor
deba@430
  1697
  ///
kpeter@474
  1698
  /// This function just returns a read-only \ref FilterArcs adaptor.
kpeter@474
  1699
  /// \ingroup graph_adaptors
kpeter@474
  1700
  /// \relates FilterArcs
kpeter@476
  1701
  template<typename GR, typename AF>
kpeter@476
  1702
  FilterArcs<const GR, AF>
kpeter@476
  1703
  filterArcs(const GR& digraph, AF& arc_filter_map) {
kpeter@476
  1704
    return FilterArcs<const GR, AF>(digraph, arc_filter_map);
deba@430
  1705
  }
deba@430
  1706
kpeter@476
  1707
  template<typename GR, typename AF>
kpeter@476
  1708
  FilterArcs<const GR, const AF>
kpeter@476
  1709
  filterArcs(const GR& digraph, const AF& arc_filter_map) {
kpeter@476
  1710
    return FilterArcs<const GR, const AF>(digraph, arc_filter_map);
deba@430
  1711
  }
deba@430
  1712
deba@432
  1713
  /// \ingroup graph_adaptors
deba@432
  1714
  ///
kpeter@474
  1715
  /// \brief Adaptor class for hiding edges in a graph.
deba@432
  1716
  ///
kpeter@474
  1717
  /// FilterEdges adaptor can be used for hiding edges in a graph.
kpeter@474
  1718
  /// A \c bool edge map must be specified, which defines the filter for
kpeter@474
  1719
  /// the edges. Only the edges with \c true filter value are shown in the
kpeter@474
  1720
  /// subgraph. This adaptor conforms to the \ref concepts::Graph
kpeter@474
  1721
  /// "Graph" concept.
deba@432
  1722
  ///
kpeter@474
  1723
  /// The adapted graph can also be modified through this adaptor
kpeter@476
  1724
  /// by adding or removing nodes or edges, unless the \c GR template
kpeter@474
  1725
  /// parameter is set to be \c const.
kpeter@474
  1726
  ///
kpeter@476
  1727
  /// \tparam GR The type of the adapted graph.
kpeter@474
  1728
  /// It must conform to the \ref concepts::Graph "Graph" concept.
kpeter@474
  1729
  /// It can also be specified to be \c const.
kpeter@476
  1730
  /// \tparam EF The type of the edge filter map.
kpeter@476
  1731
  /// It must be a \c bool (or convertible) edge map of the
kpeter@476
  1732
  /// adapted graph. The default type is
kpeter@476
  1733
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
kpeter@474
  1734
  ///
kpeter@474
  1735
  /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
kpeter@474
  1736
  /// adapted graph are convertible to each other.
kpeter@474
  1737
#ifdef DOXYGEN
kpeter@476
  1738
  template<typename GR,
kpeter@476
  1739
           typename EF>
kpeter@476
  1740
  class FilterEdges {
kpeter@474
  1741
#else
kpeter@476
  1742
  template<typename GR,
kpeter@476
  1743
           typename EF = typename GR::template EdgeMap<bool> >
kpeter@476
  1744
  class FilterEdges :
kpeter@476
  1745
    public GraphAdaptorExtender<
kpeter@476
  1746
      SubGraphBase<GR, ConstMap<typename GR::Node,bool>, EF, false> > {
kpeter@474
  1747
#endif
deba@432
  1748
  public:
kpeter@476
  1749
    /// The type of the adapted graph.
kpeter@476
  1750
    typedef GR Graph;
kpeter@476
  1751
    /// The type of the edge filter map.
kpeter@476
  1752
    typedef EF EdgeFilterMap;
kpeter@476
  1753
kpeter@476
  1754
    typedef GraphAdaptorExtender<
kpeter@476
  1755
      SubGraphBase<GR, ConstMap<typename GR::Node,bool>, EF, false> >
kpeter@476
  1756
      Parent;
kpeter@476
  1757
deba@432
  1758
    typedef typename Parent::Edge Edge;
kpeter@476
  1759
deba@432
  1760
  protected:
deba@432
  1761
    ConstMap<typename Graph::Node, bool> const_true_map;
deba@432
  1762
deba@432
  1763
    FilterEdges() : const_true_map(true) {
deba@432
  1764
      Parent::setNodeFilterMap(const_true_map);
deba@432
  1765
    }
deba@432
  1766
deba@432
  1767
  public:
deba@432
  1768
deba@432
  1769
    /// \brief Constructor
deba@432
  1770
    ///
kpeter@474
  1771
    /// Creates a subgraph for the given graph with the given edge
kpeter@474
  1772
    /// filter map.
kpeter@474
  1773
    FilterEdges(Graph& graph, EdgeFilterMap& edge_filter_map) :
deba@432
  1774
      Parent(), const_true_map(true) {
kpeter@474
  1775
      Parent::setGraph(graph);
deba@432
  1776
      Parent::setNodeFilterMap(const_true_map);
deba@432
  1777
      Parent::setEdgeFilterMap(edge_filter_map);
deba@432
  1778
    }
deba@432
  1779
kpeter@475
  1780
    /// \brief Sets the status of the given edge
deba@432
  1781
    ///
kpeter@475
  1782
    /// This function sets the status of the given edge.
kpeter@474
  1783
    /// It is done by simply setting the assigned value of \c e
kpeter@475
  1784
    /// to \c v in the edge filter map.
kpeter@475
  1785
    void status(const Edge& e, bool v) const { Parent::status(e, v); }
kpeter@475
  1786
kpeter@475
  1787
    /// \brief Returns the status of the given edge
deba@432
  1788
    ///
kpeter@475
  1789
    /// This function returns the status of the given edge.
kpeter@475
  1790
    /// It is \c true if the given edge is enabled (i.e. not hidden).
kpeter@475
  1791
    bool status(const Edge& e) const { return Parent::status(e); }
kpeter@475
  1792
kpeter@475
  1793
    /// \brief Disables the given edge
deba@432
  1794
    ///
kpeter@475
  1795
    /// This function disables the given edge in the subgraph,
kpeter@475
  1796
    /// so the iteration jumps over it.
kpeter@475
  1797
    /// It is the same as \ref status() "status(e, false)".
kpeter@475
  1798
    void disable(const Edge& e) const { Parent::status(e, false); }
kpeter@475
  1799
kpeter@475
  1800
    /// \brief Enables the given edge
kpeter@475
  1801
    ///
kpeter@475
  1802
    /// This function enables the given edge in the subgraph.
kpeter@475
  1803
    /// It is the same as \ref status() "status(e, true)".
kpeter@475
  1804
    void enable(const Edge& e) const { Parent::status(e, true); }
deba@432
  1805
deba@432
  1806
  };
deba@432
  1807
kpeter@474
  1808
  /// \brief Returns a read-only FilterEdges adaptor
deba@432
  1809
  ///
kpeter@474
  1810
  /// This function just returns a read-only \ref FilterEdges adaptor.
kpeter@474
  1811
  /// \ingroup graph_adaptors
kpeter@474
  1812
  /// \relates FilterEdges
kpeter@476
  1813
  template<typename GR, typename EF>
kpeter@476
  1814
  FilterEdges<const GR, EF>
kpeter@476
  1815
  filterEdges(const GR& graph, EF& edge_filter_map) {
kpeter@476
  1816
    return FilterEdges<const GR, EF>(graph, edge_filter_map);
deba@432
  1817
  }
deba@432
  1818
kpeter@476
  1819
  template<typename GR, typename EF>
kpeter@476
  1820
  FilterEdges<const GR, const EF>
kpeter@476
  1821
  filterEdges(const GR& graph, const EF& edge_filter_map) {
kpeter@476
  1822
    return FilterEdges<const GR, const EF>(graph, edge_filter_map);
deba@432
  1823
  }
deba@432
  1824
kpeter@474
  1825
deba@430
  1826
  template <typename _Digraph>
deba@432
  1827
  class UndirectorBase {
deba@430
  1828
  public:
deba@430
  1829
    typedef _Digraph Digraph;
deba@432
  1830
    typedef UndirectorBase Adaptor;
deba@430
  1831
deba@430
  1832
    typedef True UndirectedTag;
deba@430
  1833
deba@430
  1834
    typedef typename Digraph::Arc Edge;
deba@430
  1835
    typedef typename Digraph::Node Node;
deba@430
  1836
deba@430
  1837
    class Arc : public Edge {
deba@432
  1838
      friend class UndirectorBase;
deba@430
  1839
    protected:
deba@430
  1840
      bool _forward;
deba@430
  1841
deba@430
  1842
      Arc(const Edge& edge, bool forward) :
deba@430
  1843
        Edge(edge), _forward(forward) {}
deba@430
  1844
deba@430
  1845
    public:
deba@430
  1846
      Arc() {}
deba@430
  1847
deba@430
  1848
      Arc(Invalid) : Edge(INVALID), _forward(true) {}
deba@430
  1849
deba@430
  1850
      bool operator==(const Arc &other) const {
deba@432
  1851
        return _forward == other._forward &&
deba@432
  1852
          static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
deba@430
  1853
      }
deba@430
  1854
      bool operator!=(const Arc &other) const {
deba@432
  1855
        return _forward != other._forward ||
deba@432
  1856
          static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
deba@430
  1857
      }
deba@430
  1858
      bool operator<(const Arc &other) const {
deba@432
  1859
        return _forward < other._forward ||
deba@432
  1860
          (_forward == other._forward &&
deba@432
  1861
           static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
deba@430
  1862
      }
deba@430
  1863
    };
deba@430
  1864
deba@430
  1865
    void first(Node& n) const {
deba@430
  1866
      _digraph->first(n);
deba@430
  1867
    }
deba@430
  1868
deba@430
  1869
    void next(Node& n) const {
deba@430
  1870
      _digraph->next(n);
deba@430
  1871
    }
deba@430
  1872
deba@430
  1873
    void first(Arc& a) const {
deba@430
  1874
      _digraph->first(a);
deba@430
  1875
      a._forward = true;
deba@430
  1876
    }
deba@430
  1877
deba@430
  1878
    void next(Arc& a) const {
deba@430
  1879
      if (a._forward) {
deba@432
  1880
        a._forward = false;
deba@430
  1881
      } else {
deba@432
  1882
        _digraph->next(a);
deba@432
  1883
        a._forward = true;
deba@430
  1884
      }
deba@430
  1885
    }
deba@430
  1886
deba@430
  1887
    void first(Edge& e) const {
deba@430
  1888
      _digraph->first(e);
deba@430
  1889
    }
deba@430
  1890
deba@430
  1891
    void next(Edge& e) const {
deba@430
  1892
      _digraph->next(e);
deba@430
  1893
    }
deba@430
  1894
deba@430
  1895
    void firstOut(Arc& a, const Node& n) const {
deba@430
  1896
      _digraph->firstIn(a, n);
deba@430
  1897
      if( static_cast<const Edge&>(a) != INVALID ) {
deba@432
  1898
        a._forward = false;
deba@430
  1899
      } else {
deba@432
  1900
        _digraph->firstOut(a, n);
deba@432
  1901
        a._forward = true;
deba@430
  1902
      }
deba@430
  1903
    }
deba@430
  1904
    void nextOut(Arc &a) const {
deba@430
  1905
      if (!a._forward) {
deba@432
  1906
        Node n = _digraph->target(a);
deba@432
  1907
        _digraph->nextIn(a);
deba@432
  1908
        if (static_cast<const Edge&>(a) == INVALID ) {
deba@432
  1909
          _digraph->firstOut(a, n);
deba@432
  1910
          a._forward = true;
deba@432
  1911
        }
deba@430
  1912
      }
deba@430
  1913
      else {
deba@432
  1914
        _digraph->nextOut(a);
deba@430
  1915
      }
deba@430
  1916
    }
deba@430
  1917
deba@430
  1918
    void firstIn(Arc &a, const Node &n) const {
deba@430
  1919
      _digraph->firstOut(a, n);
deba@430
  1920
      if (static_cast<const Edge&>(a) != INVALID ) {
deba@432
  1921
        a._forward = false;
deba@430
  1922
      } else {
deba@432
  1923
        _digraph->firstIn(a, n);
deba@432
  1924
        a._forward = true;
deba@430
  1925
      }
deba@430
  1926
    }
deba@430
  1927
    void nextIn(Arc &a) const {
deba@430
  1928
      if (!a._forward) {
deba@432
  1929
        Node n = _digraph->source(a);
deba@432
  1930
        _digraph->nextOut(a);
deba@432
  1931
        if( static_cast<const Edge&>(a) == INVALID ) {
deba@432
  1932
          _digraph->firstIn(a, n);
deba@432
  1933
          a._forward = true;
deba@432
  1934
        }
deba@430
  1935
      }
deba@430
  1936
      else {
deba@432
  1937
        _digraph->nextIn(a);
deba@430
  1938
      }
deba@430
  1939
    }
deba@430
  1940
deba@430
  1941
    void firstInc(Edge &e, bool &d, const Node &n) const {
deba@430
  1942
      d = true;
deba@430
  1943
      _digraph->firstOut(e, n);
deba@430
  1944
      if (e != INVALID) return;
deba@430
  1945
      d = false;
deba@430
  1946
      _digraph->firstIn(e, n);
deba@430
  1947
    }
deba@430
  1948
deba@430
  1949
    void nextInc(Edge &e, bool &d) const {
deba@430
  1950
      if (d) {
deba@432
  1951
        Node s = _digraph->source(e);
deba@432
  1952
        _digraph->nextOut(e);
deba@432
  1953
        if (e != INVALID) return;
deba@432
  1954
        d = false;
deba@432
  1955
        _digraph->firstIn(e, s);
deba@430
  1956
      } else {
deba@432
  1957
        _digraph->nextIn(e);
deba@430
  1958
      }
deba@430
  1959
    }
deba@430
  1960
deba@430
  1961
    Node u(const Edge& e) const {
deba@430
  1962
      return _digraph->source(e);
deba@430
  1963
    }
deba@430
  1964
deba@430
  1965
    Node v(const Edge& e) const {
deba@430
  1966
      return _digraph->target(e);
deba@430
  1967
    }
deba@430
  1968
deba@430
  1969
    Node source(const Arc &a) const {
deba@430
  1970
      return a._forward ? _digraph->source(a) : _digraph->target(a);
deba@430
  1971
    }
deba@430
  1972
deba@430
  1973
    Node target(const Arc &a) const {
deba@430
  1974
      return a._forward ? _digraph->target(a) : _digraph->source(a);
deba@430
  1975
    }
deba@430
  1976
deba@430
  1977
    static Arc direct(const Edge &e, bool d) {
deba@430
  1978
      return Arc(e, d);
deba@430
  1979
    }
deba@430
  1980
    Arc direct(const Edge &e, const Node& n) const {
deba@430
  1981
      return Arc(e, _digraph->source(e) == n);
deba@430
  1982
    }
deba@430
  1983
deba@430
  1984
    static bool direction(const Arc &a) { return a._forward; }
deba@430
  1985
deba@430
  1986
    Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
deba@430
  1987
    Arc arcFromId(int ix) const {
deba@430
  1988
      return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
deba@430
  1989
    }
deba@430
  1990
    Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
deba@430
  1991
deba@430
  1992
    int id(const Node &n) const { return _digraph->id(n); }
deba@430
  1993
    int id(const Arc &a) const {
deba@430
  1994
      return  (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
deba@430
  1995
    }
deba@430
  1996
    int id(const Edge &e) const { return _digraph->id(e); }
deba@430
  1997
deba@430
  1998
    int maxNodeId() const { return _digraph->maxNodeId(); }
deba@430
  1999
    int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
deba@430
  2000
    int maxEdgeId() const { return _digraph->maxArcId(); }
deba@430
  2001
deba@430
  2002
    Node addNode() { return _digraph->addNode(); }
deba@432
  2003
    Edge addEdge(const Node& u, const Node& v) {
deba@432
  2004
      return _digraph->addArc(u, v);
deba@430
  2005
    }
deba@430
  2006
deba@430
  2007
    void erase(const Node& i) { _digraph->erase(i); }
deba@430
  2008
    void erase(const Edge& i) { _digraph->erase(i); }
deba@432
  2009
deba@430
  2010
    void clear() { _digraph->clear(); }
deba@430
  2011
deba@430
  2012
    typedef NodeNumTagIndicator<Digraph> NodeNumTag;
kpeter@472
  2013
    int nodeNum() const { return _digraph->nodeNum(); }
kpeter@469
  2014
kpeter@469
  2015
    typedef ArcNumTagIndicator<Digraph> ArcNumTag;
deba@430
  2016
    int arcNum() const { return 2 * _digraph->arcNum(); }
kpeter@469
  2017
kpeter@469
  2018
    typedef ArcNumTag EdgeNumTag;
deba@430
  2019
    int edgeNum() const { return _digraph->arcNum(); }
deba@430
  2020
kpeter@469
  2021
    typedef FindArcTagIndicator<Digraph> FindArcTag;
deba@430
  2022
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
deba@430
  2023
      if (p == INVALID) {
deba@432
  2024
        Edge arc = _digraph->findArc(s, t);
deba@432
  2025
        if (arc != INVALID) return direct(arc, true);
deba@432
  2026
        arc = _digraph->findArc(t, s);
deba@432
  2027
        if (arc != INVALID) return direct(arc, false);
deba@430
  2028
      } else if (direction(p)) {
deba@432
  2029
        Edge arc = _digraph->findArc(s, t, p);
deba@432
  2030
        if (arc != INVALID) return direct(arc, true);
deba@432
  2031
        arc = _digraph->findArc(t, s);
deba@432
  2032
        if (arc != INVALID) return direct(arc, false);
deba@430
  2033
      } else {
deba@432
  2034
        Edge arc = _digraph->findArc(t, s, p);
deba@432
  2035
        if (arc != INVALID) return direct(arc, false);
deba@430
  2036
      }
deba@430
  2037
      return INVALID;
deba@430
  2038
    }
deba@430
  2039
kpeter@469
  2040
    typedef FindArcTag FindEdgeTag;
deba@430
  2041
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
deba@430
  2042
      if (s != t) {
deba@430
  2043
        if (p == INVALID) {
deba@430
  2044
          Edge arc = _digraph->findArc(s, t);
deba@430
  2045
          if (arc != INVALID) return arc;
deba@430
  2046
          arc = _digraph->findArc(t, s);
deba@430
  2047
          if (arc != INVALID) return arc;
kpeter@472
  2048
        } else if (_digraph->source(p) == s) {
deba@430
  2049
          Edge arc = _digraph->findArc(s, t, p);
deba@430
  2050
          if (arc != INVALID) return arc;
deba@430
  2051
          arc = _digraph->findArc(t, s);
deba@432
  2052
          if (arc != INVALID) return arc;
deba@430
  2053
        } else {
deba@430
  2054
          Edge arc = _digraph->findArc(t, s, p);
deba@432
  2055
          if (arc != INVALID) return arc;
deba@430
  2056
        }
deba@430
  2057
      } else {
deba@430
  2058
        return _digraph->findArc(s, t, p);
deba@430
  2059
      }
deba@430
  2060
      return INVALID;
deba@430
  2061
    }
deba@430
  2062
deba@430
  2063
  private:
deba@432
  2064
deba@430
  2065
    template <typename _Value>
deba@430
  2066
    class ArcMapBase {
deba@430
  2067
    private:
deba@432
  2068
deba@430
  2069
      typedef typename Digraph::template ArcMap<_Value> MapImpl;
deba@432
  2070
deba@430
  2071
    public:
deba@430
  2072
deba@430
  2073
      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
deba@430
  2074
deba@430
  2075
      typedef _Value Value;
deba@430
  2076
      typedef Arc Key;
kpeter@472
  2077
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
kpeter@472
  2078
      typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
kpeter@472
  2079
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
kpeter@472
  2080
      typedef typename MapTraits<MapImpl>::ReturnValue Reference;
deba@432
  2081
deba@430
  2082
      ArcMapBase(const Adaptor& adaptor) :
deba@432
  2083
        _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
deba@432
  2084
deba@432
  2085
      ArcMapBase(const Adaptor& adaptor, const Value& v)
deba@430
  2086
        : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {}
deba@432
  2087
deba@432
  2088
      void set(const Arc& a, const Value& v) {
deba@432
  2089
        if (direction(a)) {
deba@432
  2090
          _forward.set(a, v);
deba@432
  2091
        } else {
deba@432
  2092
          _backward.set(a, v);
deba@430
  2093
        }
deba@430
  2094
      }
deba@430
  2095
kpeter@472
  2096
      ConstReturnValue operator[](const Arc& a) const {
deba@432
  2097
        if (direction(a)) {
deba@432
  2098
          return _forward[a];
deba@432
  2099
        } else {
deba@432
  2100
          return _backward[a];
deba@430
  2101
        }
deba@430
  2102
      }
deba@430
  2103
kpeter@472
  2104
      ReturnValue operator[](const Arc& a) {
deba@432
  2105
        if (direction(a)) {
deba@432
  2106
          return _forward[a];
deba@432
  2107
        } else {
deba@432
  2108
          return _backward[a];
deba@432
  2109
        }
deba@432
  2110
      }
deba@432
  2111
deba@430
  2112
    protected:
deba@430
  2113
deba@432
  2114
      MapImpl _forward, _backward;
deba@430
  2115
deba@430
  2116
    };
deba@430
  2117
deba@430
  2118
  public:
deba@430
  2119
deba@430
  2120
    template <typename _Value>
deba@430
  2121
    class NodeMap : public Digraph::template NodeMap<_Value> {
deba@430
  2122
    public:
deba@430
  2123
deba@430
  2124
      typedef _Value Value;
deba@430
  2125
      typedef typename Digraph::template NodeMap<Value> Parent;
deba@430
  2126
deba@432
  2127
      explicit NodeMap(const Adaptor& adaptor)
deba@432
  2128
        : Parent(*adaptor._digraph) {}
deba@430
  2129
deba@430
  2130
      NodeMap(const Adaptor& adaptor, const _Value& value)
deba@432
  2131
        : Parent(*adaptor._digraph, value) { }
deba@430
  2132
deba@430
  2133
    private:
deba@430
  2134
      NodeMap& operator=(const NodeMap& cmap) {
deba@430
  2135
        return operator=<NodeMap>(cmap);
deba@430
  2136
      }
deba@430
  2137
deba@430
  2138
      template <typename CMap>
deba@430
  2139
      NodeMap& operator=(const CMap& cmap) {
deba@430
  2140
        Parent::operator=(cmap);
deba@430
  2141
        return *this;
deba@430
  2142
      }
deba@432
  2143
deba@430
  2144
    };
deba@430
  2145
deba@430
  2146
    template <typename _Value>
deba@432
  2147
    class ArcMap
deba@432
  2148
      : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
deba@430
  2149
    {
deba@430
  2150
    public:
deba@430
  2151
      typedef _Value Value;
deba@430
  2152
      typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
deba@432
  2153
kpeter@472
  2154
      explicit ArcMap(const Adaptor& adaptor)
deba@432
  2155
        : Parent(adaptor) {}
deba@432
  2156
deba@432
  2157
      ArcMap(const Adaptor& adaptor, const Value& value)
deba@432
  2158
        : Parent(adaptor, value) {}
deba@432
  2159
deba@430
  2160
    private:
deba@430
  2161
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
  2162
        return operator=<ArcMap>(cmap);
deba@430
  2163
      }
deba@432
  2164
deba@430
  2165
      template <typename CMap>
deba@430
  2166
      ArcMap& operator=(const CMap& cmap) {
deba@430
  2167
        Parent::operator=(cmap);
deba@432
  2168
        return *this;
deba@430
  2169
      }
deba@430
  2170
    };
deba@432
  2171
deba@430
  2172
    template <typename _Value>
deba@430
  2173
    class EdgeMap : public Digraph::template ArcMap<_Value> {
deba@430
  2174
    public:
deba@432
  2175
deba@430
  2176
      typedef _Value Value;
deba@430
  2177
      typedef typename Digraph::template ArcMap<Value> Parent;
deba@432
  2178
deba@432
  2179
      explicit EdgeMap(const Adaptor& adaptor)
deba@432
  2180
        : Parent(*adaptor._digraph) {}
deba@430
  2181
deba@430
  2182
      EdgeMap(const Adaptor& adaptor, const Value& value)
deba@432
  2183
        : Parent(*adaptor._digraph, value) {}
deba@430
  2184
deba@430
  2185
    private:
deba@430
  2186
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@430
  2187
        return operator=<EdgeMap>(cmap);
deba@430
  2188
      }
deba@430
  2189
deba@430
  2190
      template <typename CMap>
deba@430
  2191
      EdgeMap& operator=(const CMap& cmap) {
deba@430
  2192
        Parent::operator=(cmap);
deba@430
  2193
        return *this;
deba@430
  2194
      }
deba@430
  2195
deba@430
  2196
    };
deba@430
  2197
deba@430
  2198
    typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
deba@432
  2199
    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
deba@430
  2200
kpeter@472
  2201
    typedef typename ItemSetTraits<Digraph, Edge>::ItemNotifier EdgeNotifier;
kpeter@472
  2202
    EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
kpeter@472
  2203
deba@430
  2204
  protected:
deba@430
  2205
deba@432
  2206
    UndirectorBase() : _digraph(0) {}
deba@430
  2207
deba@430
  2208
    Digraph* _digraph;
deba@430
  2209
deba@430
  2210
    void setDigraph(Digraph& digraph) {
deba@430
  2211
      _digraph = &digraph;
deba@430
  2212
    }
deba@432
  2213
deba@430
  2214
  };
deba@430
  2215
deba@432
  2216
  /// \ingroup graph_adaptors
deba@430
  2217
  ///
kpeter@474
  2218
  /// \brief Adaptor class for viewing a digraph as an undirected graph.
deba@430
  2219
  ///
kpeter@474
  2220
  /// Undirector adaptor can be used for viewing a digraph as an undirected
kpeter@474
  2221
  /// graph. All arcs of the underlying digraph are showed in the
kpeter@474
  2222
  /// adaptor as an edge (and also as a pair of arcs, of course).
kpeter@474
  2223
  /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
deba@430
  2224
  ///
kpeter@474
  2225
  /// The adapted digraph can also be modified through this adaptor
kpeter@476
  2226
  /// by adding or removing nodes or edges, unless the \c GR template
kpeter@474
  2227
  /// parameter is set to be \c const.
kpeter@474
  2228
  ///
kpeter@476
  2229
  /// \tparam GR The type of the adapted digraph.
kpeter@474
  2230
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
kpeter@474
  2231
  /// It can also be specified to be \c const.
kpeter@474
  2232
  ///
kpeter@474
  2233
  /// \note The \c Node type of this adaptor and the adapted digraph are
kpeter@474
  2234
  /// convertible to each other, moreover the \c Edge type of the adaptor
kpeter@474
  2235
  /// and the \c Arc type of the adapted digraph are also convertible to
kpeter@474
  2236
  /// each other.
kpeter@474
  2237
  /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type
kpeter@474
  2238
  /// of the adapted digraph.)
kpeter@476
  2239
  template<typename GR>
kpeter@476
  2240
#ifdef DOXYGEN
kpeter@476
  2241
  class Undirector {
kpeter@476
  2242
#else
kpeter@476
  2243
  class Undirector :
kpeter@476
  2244
    public GraphAdaptorExtender<UndirectorBase<GR> > {
kpeter@476
  2245
#endif
deba@430
  2246
  public:
kpeter@476
  2247
    /// The type of the adapted digraph.
kpeter@476
  2248
    typedef GR Digraph;
kpeter@476
  2249
    typedef GraphAdaptorExtender<UndirectorBase<GR> > Parent;
deba@430
  2250
  protected:
deba@432
  2251
    Undirector() { }
deba@430
  2252
  public:
deba@430
  2253
deba@430
  2254
    /// \brief Constructor
deba@430
  2255
    ///
kpeter@474
  2256
    /// Creates an undirected graph from the given digraph.
kpeter@476
  2257
    Undirector(Digraph& digraph) {
deba@432
  2258
      setDigraph(digraph);
deba@430
  2259
    }
deba@430
  2260
kpeter@474
  2261
    /// \brief Arc map combined from two original arc maps
deba@430
  2262
    ///
kpeter@474
  2263
    /// This map adaptor class adapts two arc maps of the underlying
kpeter@474
  2264
    /// digraph to get an arc map of the undirected graph.
kpeter@474
  2265
    /// Its value type is inherited from the first arc map type
kpeter@474
  2266
    /// (\c %ForwardMap).
kpeter@476
  2267
    template <typename ForwardMap, typename BackwardMap>
deba@430
  2268
    class CombinedArcMap {
deba@430
  2269
    public:
deba@432
  2270
kpeter@474
  2271
      /// The key type of the map
kpeter@474
  2272
      typedef typename Parent::Arc Key;
kpeter@474
  2273
      /// The value type of the map
deba@430
  2274
      typedef typename ForwardMap::Value Value;
deba@430
  2275
kpeter@476
  2276
      typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
kpeter@476
  2277
kpeter@472
  2278
      typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue;
kpeter@472
  2279
      typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue;
kpeter@472
  2280
      typedef typename MapTraits<ForwardMap>::ReturnValue Reference;
kpeter@472
  2281
      typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference;
kpeter@472
  2282
deba@432
  2283
      /// Constructor
deba@432
  2284
      CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
deba@430
  2285
        : _forward(&forward), _backward(&backward) {}
deba@432
  2286
kpeter@474
  2287
      /// Sets the value associated with the given key.
deba@432
  2288
      void set(const Key& e, const Value& a) {
deba@432
  2289
        if (Parent::direction(e)) {
deba@432
  2290
          _forward->set(e, a);
deba@432
  2291
        } else {
deba@432
  2292
          _backward->set(e, a);
deba@432
  2293
        }
deba@430
  2294
      }
deba@430
  2295
kpeter@474
  2296
      /// Returns the value associated with the given key.
kpeter@472
  2297
      ConstReturnValue operator[](const Key& e) const {
deba@432
  2298
        if (Parent::direction(e)) {
deba@432
  2299
          return (*_forward)[e];
deba@432
  2300
        } else {
deba@432
  2301
          return (*_backward)[e];
deba@430
  2302
        }
deba@430
  2303
      }
deba@430
  2304
kpeter@474
  2305
      /// Returns a reference to the value associated with the given key.
kpeter@472
  2306
      ReturnValue operator[](const Key& e) {
deba@432
  2307
        if (Parent::direction(e)) {
deba@432
  2308
          return (*_forward)[e];
deba@432
  2309
        } else {
deba@432
  2310
          return (*_backward)[e];
deba@430
  2311
        }
deba@430
  2312
      }
deba@430
  2313
deba@432
  2314
    protected:
deba@432
  2315
deba@432
  2316
      ForwardMap* _forward;
deba@432
  2317
      BackwardMap* _backward;
deba@432
  2318
deba@432
  2319
    };
deba@432
  2320
kpeter@474
  2321
    /// \brief Returns a combined arc map
deba@432
  2322
    ///
kpeter@474
  2323
    /// This function just returns a combined arc map.
deba@432
  2324
    template <typename ForwardMap, typename BackwardMap>
deba@432
  2325
    static CombinedArcMap<ForwardMap, BackwardMap>
deba@432
  2326
    combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
deba@432
  2327
      return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
deba@432
  2328
    }
deba@432
  2329
deba@432
  2330
    template <typename ForwardMap, typename BackwardMap>
deba@432
  2331
    static CombinedArcMap<const ForwardMap, BackwardMap>
deba@432
  2332
    combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
deba@432
  2333
      return CombinedArcMap<const ForwardMap,
deba@432
  2334
        BackwardMap>(forward, backward);
deba@432
  2335
    }
deba@432
  2336
deba@432
  2337
    template <typename ForwardMap, typename BackwardMap>
deba@432
  2338
    static CombinedArcMap<ForwardMap, const BackwardMap>
deba@432
  2339
    combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
deba@432
  2340
      return CombinedArcMap<ForwardMap,
deba@432
  2341
        const BackwardMap>(forward, backward);
deba@432
  2342
    }
deba@432
  2343
deba@432
  2344
    template <typename ForwardMap, typename BackwardMap>
deba@432
  2345
    static CombinedArcMap<const ForwardMap, const BackwardMap>
deba@432
  2346
    combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
deba@432
  2347
      return CombinedArcMap<const ForwardMap,
deba@432
  2348
        const BackwardMap>(forward, backward);
deba@432
  2349
    }
deba@432
  2350
deba@432
  2351
  };
deba@432
  2352
kpeter@474
  2353
  /// \brief Returns a read-only Undirector adaptor
deba@432
  2354
  ///
kpeter@474
  2355
  /// This function just returns a read-only \ref Undirector adaptor.
kpeter@474
  2356
  /// \ingroup graph_adaptors
kpeter@474
  2357
  /// \relates Undirector
kpeter@476
  2358
  template<typename GR>
kpeter@476
  2359
  Undirector<const GR> undirector(const GR& digraph) {
kpeter@476
  2360
    return Undirector<const GR>(digraph);
deba@432
  2361
  }
deba@432
  2362
kpeter@474
  2363
deba@432
  2364
  template <typename _Graph, typename _DirectionMap>
deba@432
  2365
  class OrienterBase {
deba@432
  2366
  public:
deba@432
  2367
deba@432
  2368
    typedef _Graph Graph;
deba@432
  2369
    typedef _DirectionMap DirectionMap;
deba@432
  2370
deba@432
  2371
    typedef typename Graph::Node Node;
deba@432
  2372
    typedef typename Graph::Edge Arc;
deba@432
  2373
deba@432
  2374
    void reverseArc(const Arc& arc) {
deba@432
  2375
      _direction->set(arc, !(*_direction)[arc]);
deba@432
  2376
    }
deba@432
  2377
deba@432
  2378
    void first(Node& i) const { _graph->first(i); }
deba@432
  2379
    void first(Arc& i) const { _graph->first(i); }
deba@432
  2380
    void firstIn(Arc& i, const Node& n) const {
kpeter@470
  2381
      bool d = true;
deba@432
  2382
      _graph->firstInc(i, d, n);
deba@432
  2383
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
deba@432
  2384
    }
deba@432
  2385
    void firstOut(Arc& i, const Node& n ) const {
kpeter@470
  2386
      bool d = true;
deba@432
  2387
      _graph->firstInc(i, d, n);
deba@432
  2388
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
deba@432
  2389
    }
deba@432
  2390
deba@432
  2391
    void next(Node& i) const { _graph->next(i); }
deba@432
  2392
    void next(Arc& i) const { _graph->next(i); }
deba@432
  2393
    void nextIn(Arc& i) const {
deba@432
  2394
      bool d = !(*_direction)[i];
deba@432
  2395
      _graph->nextInc(i, d);
deba@432
  2396
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
deba@432
  2397
    }
deba@432
  2398
    void nextOut(Arc& i) const {
deba@432
  2399
      bool d = (*_direction)[i];
deba@432
  2400
      _graph->nextInc(i, d);
deba@432
  2401
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
deba@432
  2402
    }
deba@432
  2403
deba@432
  2404
    Node source(const Arc& e) const {
deba@432
  2405
      return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
deba@432
  2406
    }
deba@432
  2407
    Node target(const Arc& e) const {
deba@432
  2408
      return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
deba@432
  2409
    }
deba@432
  2410
deba@432
  2411
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
deba@432
  2412
    int nodeNum() const { return _graph->nodeNum(); }
deba@432
  2413
kpeter@469
  2414
    typedef EdgeNumTagIndicator<Graph> ArcNumTag;
deba@432
  2415
    int arcNum() const { return _graph->edgeNum(); }
deba@432
  2416
kpeter@469
  2417
    typedef FindEdgeTagIndicator<Graph> FindArcTag;
deba@432
  2418
    Arc findArc(const Node& u, const Node& v,
kpeter@471
  2419
                const Arc& prev = INVALID) const {
kpeter@472
  2420
      Arc arc = _graph->findEdge(u, v, prev);
kpeter@472
  2421
      while (arc != INVALID && source(arc) != u) {
deba@432
  2422
        arc = _graph->findEdge(u, v, arc);
deba@430
  2423
      }
deba@432
  2424
      return arc;
deba@432
  2425
    }
deba@432
  2426
deba@432
  2427
    Node addNode() {
deba@432
  2428
      return Node(_graph->addNode());
deba@432
  2429
    }
deba@432
  2430
deba@432
  2431
    Arc addArc(const Node& u, const Node& v) {
kpeter@472
  2432
      Arc arc = _graph->addEdge(u, v);
kpeter@472
  2433
      _direction->set(arc, _graph->u(arc) == u);
deba@432
  2434
      return arc;
deba@432
  2435
    }
deba@432
  2436
deba@432
  2437
    void erase(const Node& i) { _graph->erase(i); }
deba@432
  2438
    void erase(const Arc& i) { _graph->erase(i); }
deba@432
  2439
deba@432
  2440
    void clear() { _graph->clear(); }
deba@432
  2441
deba@432
  2442
    int id(const Node& v) const { return _graph->id(v); }
deba@432
  2443
    int id(const Arc& e) const { return _graph->id(e); }
deba@432
  2444
deba@432
  2445
    Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
deba@432
  2446
    Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
deba@432
  2447
deba@432
  2448
    int maxNodeId() const { return _graph->maxNodeId(); }
deba@432
  2449
    int maxArcId() const { return _graph->maxEdgeId(); }
deba@432
  2450
deba@432
  2451
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
deba@432
  2452
    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
deba@432
  2453
deba@432
  2454
    typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
deba@432
  2455
    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
deba@432
  2456
deba@432
  2457
    template <typename _Value>
deba@432
  2458
    class NodeMap : public _Graph::template NodeMap<_Value> {
deba@432
  2459
    public:
deba@432
  2460
deba@432
  2461
      typedef typename _Graph::template NodeMap<_Value> Parent;
deba@432
  2462
deba@432
  2463
      explicit NodeMap(const OrienterBase& adapter)
deba@432
  2464
        : Parent(*adapter._graph) {}
deba@432
  2465
deba@432
  2466
      NodeMap(const OrienterBase& adapter, const _Value& value)
deba@432
  2467
        : Parent(*adapter._graph, value) {}
deba@432
  2468
deba@432
  2469
    private:
deba@432
  2470
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
  2471
        return operator=<NodeMap>(cmap);
deba@432
  2472
      }
deba@432
  2473
deba@432
  2474
      template <typename CMap>
deba@432
  2475
      NodeMap& operator=(const CMap& cmap) {
deba@432
  2476
        Parent::operator=(cmap);
deba@432
  2477
        return *this;
deba@432
  2478
      }
deba@430
  2479
deba@430
  2480
    };
deba@430
  2481
deba@432
  2482
    template <typename _Value>
deba@432
  2483
    class ArcMap : public _Graph::template EdgeMap<_Value> {
deba@432
  2484
    public:
deba@432
  2485
deba@432
  2486
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@432
  2487
deba@432
  2488
      explicit ArcMap(const OrienterBase& adapter)
deba@432
  2489
        : Parent(*adapter._graph) { }
deba@432
  2490
deba@432
  2491
      ArcMap(const OrienterBase& adapter, const _Value& value)
deba@432
  2492
        : Parent(*adapter._graph, value) { }
deba@432
  2493
deba@432
  2494
    private:
deba@432
  2495
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
  2496
        return operator=<ArcMap>(cmap);
deba@432
  2497
      }
deba@432
  2498
deba@432
  2499
      template <typename CMap>
deba@432
  2500
      ArcMap& operator=(const CMap& cmap) {
deba@432
  2501
        Parent::operator=(cmap);
deba@432
  2502
        return *this;
deba@432
  2503
      }
deba@432
  2504
    };
deba@432
  2505
deba@432
  2506
deba@432
  2507
deba@432
  2508
  protected:
deba@432
  2509
    Graph* _graph;
deba@432
  2510
    DirectionMap* _direction;
deba@432
  2511
deba@432
  2512
    void setDirectionMap(DirectionMap& direction) {
deba@432
  2513
      _direction = &direction;
deba@432
  2514
    }
deba@432
  2515
deba@432
  2516
    void setGraph(Graph& graph) {
deba@432
  2517
      _graph = &graph;
deba@432
  2518
    }
deba@432
  2519
deba@430
  2520
  };
deba@430
  2521
deba@432
  2522
  /// \ingroup graph_adaptors
deba@430
  2523
  ///
kpeter@474
  2524
  /// \brief Adaptor class for orienting the edges of a graph to get a digraph
deba@432
  2525
  ///
kpeter@474
  2526
  /// Orienter adaptor can be used for orienting the edges of a graph to
kpeter@474
  2527
  /// get a digraph. A \c bool edge map of the underlying graph must be
kpeter@474
  2528
  /// specified, which define the direction of the arcs in the adaptor.
kpeter@474
  2529
  /// The arcs can be easily reversed by the \c reverseArc() member function
kpeter@474
  2530
  /// of the adaptor.
kpeter@474
  2531
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
deba@432
  2532
  ///
kpeter@474
  2533
  /// The adapted graph can also be modified through this adaptor
kpeter@476
  2534
  /// by adding or removing nodes or arcs, unless the \c GR template
kpeter@474
  2535
  /// parameter is set to be \c const.
deba@432
  2536
  ///
kpeter@476
  2537
  /// \tparam GR The type of the adapted graph.
kpeter@474
  2538
  /// It must conform to the \ref concepts::Graph "Graph" concept.
kpeter@474
  2539
  /// It can also be specified to be \c const.
kpeter@476
  2540
  /// \tparam DM The type of the direction map.
kpeter@476
  2541
  /// It must be a \c bool (or convertible) edge map of the
kpeter@476
  2542
  /// adapted graph. The default type is
kpeter@476
  2543
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
kpeter@474
  2544
  ///
kpeter@474
  2545
  /// \note The \c Node type of this adaptor and the adapted graph are
kpeter@474
  2546
  /// convertible to each other, moreover the \c Arc type of the adaptor
kpeter@474
  2547
  /// and the \c Edge type of the adapted graph are also convertible to
kpeter@474
  2548
  /// each other.
kpeter@474
  2549
#ifdef DOXYGEN
kpeter@476
  2550
  template<typename GR,
kpeter@476
  2551
           typename DM>
kpeter@476
  2552
  class Orienter {
kpeter@474
  2553
#else
kpeter@476
  2554
  template<typename GR,
kpeter@476
  2555
           typename DM = typename GR::template EdgeMap<bool> >
kpeter@476
  2556
  class Orienter :
kpeter@476
  2557
    public DigraphAdaptorExtender<OrienterBase<GR, DM> > {
kpeter@474
  2558
#endif
deba@432
  2559
  public:
kpeter@474
  2560
kpeter@474
  2561
    /// The type of the adapted graph.
kpeter@476
  2562
    typedef GR Graph;
kpeter@474
  2563
    /// The type of the direction edge map.
kpeter@476
  2564
    typedef DM DirectionMap;
kpeter@476
  2565
kpeter@476
  2566
    typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
deba@432
  2567
    typedef typename Parent::Arc Arc;
deba@432
  2568
  protected:
deba@432
  2569
    Orienter() { }
deba@432
  2570
  public:
deba@432
  2571
kpeter@474
  2572
    /// \brief Constructor
deba@432
  2573
    ///
kpeter@474
  2574
    /// Constructor of the adaptor.
deba@432
  2575
    Orienter(Graph& graph, DirectionMap& direction) {
deba@432
  2576
      setGraph(graph);
deba@432
  2577
      setDirectionMap(direction);
deba@432
  2578
    }
deba@432
  2579
kpeter@474
  2580
    /// \brief Reverses the given arc
deba@432
  2581
    ///
kpeter@474
  2582
    /// This function reverses the given arc.
kpeter@474
  2583
    /// It is done by simply negate the assigned value of \c a
kpeter@474
  2584
    /// in the direction map.
deba@432
  2585
    void reverseArc(const Arc& a) {
deba@432
  2586
      Parent::reverseArc(a);
deba@432
  2587
    }
deba@432
  2588
  };
deba@432
  2589
kpeter@474
  2590
  /// \brief Returns a read-only Orienter adaptor
deba@432
  2591
  ///
kpeter@474
  2592
  /// This function just returns a read-only \ref Orienter adaptor.
kpeter@474
  2593
  /// \ingroup graph_adaptors
kpeter@474
  2594
  /// \relates Orienter
kpeter@476
  2595
  template<typename GR, typename DM>
kpeter@476
  2596
  Orienter<const GR, DM>
kpeter@476
  2597
  orienter(const GR& graph, DM& direction_map) {
kpeter@476
  2598
    return Orienter<const GR, DM>(graph, direction_map);
deba@430
  2599
  }
deba@430
  2600
kpeter@476
  2601
  template<typename GR, typename DM>
kpeter@476
  2602
  Orienter<const GR, const DM>
kpeter@476
  2603
  orienter(const GR& graph, const DM& direction_map) {
kpeter@476
  2604
    return Orienter<const GR, const DM>(graph, direction_map);
deba@432
  2605
  }
deba@432
  2606
deba@432
  2607
  namespace _adaptor_bits {
deba@432
  2608
kpeter@476
  2609
    template<typename Digraph,
kpeter@476
  2610
             typename CapacityMap,
kpeter@476
  2611
             typename FlowMap,
kpeter@476
  2612
             typename Tolerance>
deba@432
  2613
    class ResForwardFilter {
deba@432
  2614
    public:
deba@432
  2615
deba@432
  2616
      typedef typename Digraph::Arc Key;
deba@432
  2617
      typedef bool Value;
deba@432
  2618
deba@432
  2619
    private:
deba@432
  2620
deba@432
  2621
      const CapacityMap* _capacity;
deba@432
  2622
      const FlowMap* _flow;
deba@432
  2623
      Tolerance _tolerance;
deba@432
  2624
    public:
deba@432
  2625
deba@432
  2626
      ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow,
deba@432
  2627
                       const Tolerance& tolerance = Tolerance())
deba@432
  2628
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
deba@432
  2629
deba@432
  2630
      bool operator[](const typename Digraph::Arc& a) const {
deba@432
  2631
        return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
deba@432
  2632
      }
deba@432
  2633
    };
deba@432
  2634
kpeter@476
  2635
    template<typename Digraph,
kpeter@476
  2636
             typename CapacityMap,
kpeter@476
  2637
             typename FlowMap,
kpeter@476
  2638
             typename Tolerance>
deba@432
  2639
    class ResBackwardFilter {
deba@432
  2640
    public:
deba@432
  2641
deba@432
  2642
      typedef typename Digraph::Arc Key;
deba@432
  2643
      typedef bool Value;
deba@432
  2644
deba@432
  2645
    private:
deba@432
  2646
deba@432
  2647
      const CapacityMap* _capacity;
deba@432
  2648
      const FlowMap* _flow;
deba@432
  2649
      Tolerance _tolerance;
deba@432
  2650
deba@432
  2651
    public:
deba@432
  2652
deba@432
  2653
      ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow,
deba@432
  2654
                        const Tolerance& tolerance = Tolerance())
deba@432
  2655
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
deba@432
  2656
deba@432
  2657
      bool operator[](const typename Digraph::Arc& a) const {
deba@432
  2658
        return _tolerance.positive((*_flow)[a]);
deba@432
  2659
      }
deba@432
  2660
    };
deba@432
  2661
deba@432
  2662
  }
deba@432
  2663
deba@432
  2664
  /// \ingroup graph_adaptors
deba@432
  2665
  ///
kpeter@474
  2666
  /// \brief Adaptor class for composing the residual digraph for directed
deba@432
  2667
  /// flow and circulation problems.
deba@432
  2668
  ///
kpeter@474
  2669
  /// Residual can be used for composing the \e residual digraph for directed
kpeter@474
  2670
  /// flow and circulation problems. Let \f$ G=(V, A) \f$ be a directed graph
kpeter@474
  2671
  /// and let \f$ F \f$ be a number type. Let \f$ flow, cap: A\to F \f$ be
kpeter@474
  2672
  /// functions on the arcs.
kpeter@474
  2673
  /// This adaptor implements a digraph structure with node set \f$ V \f$
kpeter@474
  2674
  /// and arc set \f$ A_{forward}\cup A_{backward} \f$,
kpeter@474
  2675
  /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and
kpeter@474
  2676
  /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so
kpeter@474
  2677
  /// called residual digraph.
kpeter@474
  2678
  /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken,
kpeter@474
  2679
  /// multiplicities are counted, i.e. the adaptor has exactly
kpeter@474
  2680
  /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel
kpeter@474
  2681
  /// arcs).
kpeter@474
  2682
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
deba@432
  2683
  ///
kpeter@476
  2684
  /// \tparam GR The type of the adapted digraph.
kpeter@474
  2685
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
kpeter@474
  2686
  /// It is implicitly \c const.
kpeter@476
  2687
  /// \tparam CM The type of the capacity map.
kpeter@476
  2688
  /// It must be an arc map of some numerical type, which defines
kpeter@474
  2689
  /// the capacities in the flow problem. It is implicitly \c const.
kpeter@476
  2690
  /// The default type is
kpeter@476
  2691
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
kpeter@476
  2692
  /// \tparam FM The type of the flow map.
kpeter@476
  2693
  /// It must be an arc map of some numerical type, which defines
kpeter@476
  2694
  /// the flow values in the flow problem. The default type is \c CM.
kpeter@476
  2695
  /// \tparam TL The tolerance type for handling inexact computation.
kpeter@474
  2696
  /// The default tolerance type depends on the value type of the
kpeter@474
  2697
  /// capacity map.
deba@432
  2698
  ///
kpeter@474
  2699
  /// \note This adaptor is implemented using Undirector and FilterArcs
kpeter@474
  2700
  /// adaptors.
kpeter@474
  2701
  ///
kpeter@474
  2702
  /// \note The \c Node type of this adaptor and the adapted digraph are
kpeter@474
  2703
  /// convertible to each other, moreover the \c Arc type of the adaptor
kpeter@474
  2704
  /// is convertible to the \c Arc type of the adapted digraph.
kpeter@474
  2705
#ifdef DOXYGEN
kpeter@476
  2706
  template<typename GR, typename CM, typename FM, typename TL>
kpeter@474
  2707
  class Residual
kpeter@474
  2708
#else
kpeter@476
  2709
  template<typename GR,
kpeter@476
  2710
           typename CM = typename GR::template ArcMap<int>,
kpeter@476
  2711
           typename FM = CM,
kpeter@476
  2712
           typename TL = Tolerance<typename CM::Value> >
deba@432
  2713
  class Residual :
deba@432
  2714
    public FilterArcs<
kpeter@476
  2715
      Undirector<const GR>,
kpeter@476
  2716
      typename Undirector<const GR>::template CombinedArcMap<
kpeter@476
  2717
        _adaptor_bits::ResForwardFilter<const GR, CM, FM, TL>,
kpeter@476
  2718
        _adaptor_bits::ResBackwardFilter<const GR, CM, FM, TL> > >
kpeter@474
  2719
#endif
deba@432
  2720
  {
deba@430
  2721
  public:
deba@430
  2722
kpeter@474
  2723
    /// The type of the underlying digraph.
kpeter@476
  2724
    typedef GR Digraph;
kpeter@474
  2725
    /// The type of the capacity map.
kpeter@476
  2726
    typedef CM CapacityMap;
kpeter@474
  2727
    /// The type of the flow map.
kpeter@476
  2728
    typedef FM FlowMap;
kpeter@476
  2729
    /// The tolerance type.
kpeter@476
  2730
    typedef TL Tolerance;
deba@430
  2731
deba@430
  2732
    typedef typename CapacityMap::Value Value;
deba@432
  2733
    typedef Residual Adaptor;
deba@430
  2734
deba@430
  2735
  protected:
deba@430
  2736
deba@432
  2737
    typedef Undirector<const Digraph> Undirected;
deba@432
  2738
deba@432
  2739
    typedef _adaptor_bits::ResForwardFilter<const Digraph, CapacityMap,
deba@432
  2740
                                            FlowMap, Tolerance> ForwardFilter;
deba@432
  2741
deba@432
  2742
    typedef _adaptor_bits::ResBackwardFilter<const Digraph, CapacityMap,
deba@432
  2743
                                             FlowMap, Tolerance> BackwardFilter;
deba@432
  2744
deba@432
  2745
    typedef typename Undirected::
kpeter@476
  2746
      template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
deba@430
  2747
deba@432
  2748
    typedef FilterArcs<Undirected, ArcFilter> Parent;
deba@430
  2749
deba@430
  2750
    const CapacityMap* _capacity;
deba@430
  2751
    FlowMap* _flow;
deba@430
  2752
deba@432
  2753
    Undirected _graph;
deba@430
  2754
    ForwardFilter _forward_filter;
deba@430
  2755
    BackwardFilter _backward_filter;
deba@430
  2756
    ArcFilter _arc_filter;
deba@430
  2757
deba@430
  2758
  public:
deba@430
  2759
kpeter@474
  2760
    /// \brief Constructor
deba@430
  2761
    ///
kpeter@474
  2762
    /// Constructor of the residual digraph adaptor. The parameters are the
kpeter@474
  2763
    /// digraph, the capacity map, the flow map, and a tolerance object.
deba@432
  2764
    Residual(const Digraph& digraph, const CapacityMap& capacity,
deba@432
  2765
             FlowMap& flow, const Tolerance& tolerance = Tolerance())
deba@430
  2766
      : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph),
deba@432
  2767
        _forward_filter(capacity, flow, tolerance),
deba@430
  2768
        _backward_filter(capacity, flow, tolerance),
deba@430
  2769
        _arc_filter(_forward_filter, _backward_filter)
deba@430
  2770
    {
deba@430
  2771
      Parent::setDigraph(_graph);
deba@430
  2772
      Parent::setArcFilterMap(_arc_filter);
deba@430
  2773
    }
deba@430
  2774
deba@430
  2775
    typedef typename Parent::Arc Arc;
deba@430
  2776
kpeter@474
  2777
    /// \brief Returns the residual capacity of the given arc.
deba@430
  2778
    ///
kpeter@474
  2779
    /// Returns the residual capacity of the given arc.
deba@432
  2780
    Value residualCapacity(const Arc& a) const {
deba@432
  2781
      if (Undirected::direction(a)) {
deba@432
  2782
        return (*_capacity)[a] - (*_flow)[a];
deba@430
  2783
      } else {
deba@432
  2784
        return (*_flow)[a];
deba@430
  2785
      }
deba@432
  2786
    }
deba@432
  2787
kpeter@475
  2788
    /// \brief Augments on the given arc in the residual digraph.
deba@430
  2789
    ///
kpeter@475
  2790
    /// Augments on the given arc in the residual digraph. It increases
kpeter@474
  2791
    /// or decreases the flow value on the original arc according to the
kpeter@474
  2792
    /// direction of the residual arc.
deba@432
  2793
    void augment(const Arc& a, const Value& v) const {
deba@432
  2794
      if (Undirected::direction(a)) {
deba@432
  2795
        _flow->set(a, (*_flow)[a] + v);
deba@432
  2796
      } else {
deba@432
  2797
        _flow->set(a, (*_flow)[a] - v);
deba@430
  2798
      }
deba@430
  2799
    }
deba@430
  2800
kpeter@474
  2801
    /// \brief Returns \c true if the given residual arc is a forward arc.
deba@430
  2802
    ///
kpeter@474
  2803
    /// Returns \c true if the given residual arc has the same orientation
kpeter@474
  2804
    /// as the original arc, i.e. it is a so called forward arc.
deba@432
  2805
    static bool forward(const Arc& a) {
deba@432
  2806
      return Undirected::direction(a);
deba@430
  2807
    }
deba@430
  2808
kpeter@474
  2809
    /// \brief Returns \c true if the given residual arc is a backward arc.
deba@430
  2810
    ///
kpeter@474
  2811
    /// Returns \c true if the given residual arc has the opposite orientation
kpeter@474
  2812
    /// than the original arc, i.e. it is a so called backward arc.
deba@432
  2813
    static bool backward(const Arc& a) {
deba@432
  2814
      return !Undirected::direction(a);
deba@430
  2815
    }
deba@430
  2816
kpeter@474
  2817
    /// \brief Returns the forward oriented residual arc.
deba@430
  2818
    ///
kpeter@474
  2819
    /// Returns the forward oriented residual arc related to the given
kpeter@474
  2820
    /// arc of the underlying digraph.
deba@432
  2821
    static Arc forward(const typename Digraph::Arc& a) {
deba@432
  2822
      return Undirected::direct(a, true);
deba@430
  2823
    }
deba@430
  2824
kpeter@474
  2825
    /// \brief Returns the backward oriented residual arc.
deba@430
  2826
    ///
kpeter@474
  2827
    /// Returns the backward oriented residual arc related to the given
kpeter@474
  2828
    /// arc of the underlying digraph.
deba@432
  2829
    static Arc backward(const typename Digraph::Arc& a) {
deba@432
  2830
      return Undirected::direct(a, false);
deba@430
  2831
    }
deba@430
  2832
deba@430
  2833
    /// \brief Residual capacity map.
deba@430
  2834
    ///
kpeter@474
  2835
    /// This map adaptor class can be used for obtaining the residual
kpeter@474
  2836
    /// capacities as an arc map of the residual digraph.
kpeter@474
  2837
    /// Its value type is inherited from the capacity map.
deba@432
  2838
    class ResidualCapacity {
deba@430
  2839
    protected:
deba@430
  2840
      const Adaptor* _adaptor;
deba@430
  2841
    public:
kpeter@474
  2842
      /// The key type of the map
deba@430
  2843
      typedef Arc Key;
kpeter@474
  2844
      /// The value type of the map
kpeter@476
  2845
      typedef typename CapacityMap::Value Value;
deba@430
  2846
deba@432
  2847
      /// Constructor
deba@432
  2848
      ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {}
deba@432
  2849
kpeter@474
  2850
      /// Returns the value associated with the given residual arc
deba@432
  2851
      Value operator[](const Arc& a) const {
deba@432
  2852
        return _adaptor->residualCapacity(a);
deba@430
  2853
      }
deba@432
  2854
deba@430
  2855
    };
deba@430
  2856
kpeter@473
  2857
    /// \brief Returns a residual capacity map
kpeter@473
  2858
    ///
kpeter@473
  2859
    /// This function just returns a residual capacity map.
kpeter@473
  2860
    ResidualCapacity residualCapacity() const {
kpeter@473
  2861
      return ResidualCapacity(*this);
kpeter@473
  2862
    }
kpeter@473
  2863
deba@430
  2864
  };
deba@430
  2865
kpeter@473
  2866
  /// \brief Returns a (read-only) Residual adaptor
kpeter@473
  2867
  ///
kpeter@473
  2868
  /// This function just returns a (read-only) \ref Residual adaptor.
kpeter@473
  2869
  /// \ingroup graph_adaptors
kpeter@473
  2870
  /// \relates Residual
kpeter@476
  2871
  template<typename GR, typename CM, typename FM>
kpeter@476
  2872
  Residual<GR, CM, FM> residual(const GR& digraph,
kpeter@476
  2873
                                const CM& capacity_map,
kpeter@476
  2874
                                FM& flow_map) {
kpeter@476
  2875
    return Residual<GR, CM, FM> (digraph, capacity_map, flow_map);
kpeter@473
  2876
  }
kpeter@473
  2877
kpeter@473
  2878
deba@430
  2879
  template <typename _Digraph>
deba@432
  2880
  class SplitNodesBase {
deba@430
  2881
  public:
deba@430
  2882
deba@430
  2883
    typedef _Digraph Digraph;
deba@430
  2884
    typedef DigraphAdaptorBase<const _Digraph> Parent;
deba@432
  2885
    typedef SplitNodesBase Adaptor;
deba@430
  2886
deba@430
  2887
    typedef typename Digraph::Node DigraphNode;
deba@430
  2888
    typedef typename Digraph::Arc DigraphArc;
deba@430
  2889
deba@430
  2890
    class Node;
deba@430
  2891
    class Arc;
deba@430
  2892
deba@430
  2893
  private:
deba@430
  2894
deba@430
  2895
    template <typename T> class NodeMapBase;
deba@430
  2896
    template <typename T> class ArcMapBase;
deba@430
  2897
deba@430
  2898
  public:
deba@432
  2899
deba@430
  2900
    class Node : public DigraphNode {
deba@432
  2901
      friend class SplitNodesBase;
deba@430
  2902
      template <typename T> friend class NodeMapBase;
deba@430
  2903
    private:
deba@430
  2904
deba@430
  2905
      bool _in;
deba@430
  2906
      Node(DigraphNode node, bool in)
deba@432
  2907
        : DigraphNode(node), _in(in) {}
deba@432
  2908
deba@430
  2909
    public:
deba@430
  2910
deba@430
  2911
      Node() {}
deba@430
  2912
      Node(Invalid) : DigraphNode(INVALID), _in(true) {}
deba@430
  2913
deba@430
  2914
      bool operator==(const Node& node) const {
deba@432
  2915
        return DigraphNode::operator==(node) && _in == node._in;
deba@430
  2916
      }
deba@432
  2917
deba@430
  2918
      bool operator!=(const Node& node) const {
deba@432
  2919
        return !(*this == node);
deba@430
  2920
      }
deba@432
  2921
deba@430
  2922
      bool operator<(const Node& node) const {
deba@432
  2923
        return DigraphNode::operator<(node) ||
deba@432
  2924
          (DigraphNode::operator==(node) && _in < node._in);
deba@430
  2925
      }
deba@430
  2926
    };
deba@430
  2927
deba@430
  2928
    class Arc {
deba@432
  2929
      friend class SplitNodesBase;
deba@430
  2930
      template <typename T> friend class ArcMapBase;
deba@430
  2931
    private:
deba@430
  2932
      typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
deba@430
  2933
deba@430
  2934
      explicit Arc(const DigraphArc& arc) : _item(arc) {}
deba@430
  2935
      explicit Arc(const DigraphNode& node) : _item(node) {}
deba@432
  2936
deba@430
  2937
      ArcImpl _item;
deba@430
  2938
deba@430
  2939
    public:
deba@430
  2940
      Arc() {}
deba@430
  2941
      Arc(Invalid) : _item(DigraphArc(INVALID)) {}
deba@430
  2942
deba@430
  2943
      bool operator==(const Arc& arc) const {
deba@430
  2944
        if (_item.firstState()) {
deba@430
  2945
          if (arc._item.firstState()) {
deba@430
  2946
            return _item.first() == arc._item.first();
deba@430
  2947
          }
deba@430
  2948
        } else {
deba@430
  2949
          if (arc._item.secondState()) {
deba@430
  2950
            return _item.second() == arc._item.second();
deba@430
  2951
          }
deba@430
  2952
        }
deba@430
  2953
        return false;
deba@430
  2954
      }
deba@432
  2955
deba@430
  2956
      bool operator!=(const Arc& arc) const {
deba@432
  2957
        return !(*this == arc);
deba@430
  2958
      }
deba@432
  2959
deba@430
  2960
      bool operator<(const Arc& arc) const {
deba@430
  2961
        if (_item.firstState()) {
deba@430
  2962
          if (arc._item.firstState()) {
deba@430
  2963
            return _item.first() < arc._item.first();
deba@430
  2964
          }
deba@430
  2965
          return false;
deba@430
  2966
        } else {
deba@430
  2967
          if (arc._item.secondState()) {
deba@430
  2968
            return _item.second() < arc._item.second();
deba@430
  2969
          }
deba@430
  2970
          return true;
deba@430
  2971
        }
deba@430
  2972
      }
deba@430
  2973
deba@430
  2974
      operator DigraphArc() const { return _item.first(); }
deba@430
  2975
      operator DigraphNode() const { return _item.second(); }
deba@430
  2976
deba@430
  2977
    };
deba@430
  2978
deba@430
  2979
    void first(Node& n) const {
deba@430
  2980
      _digraph->first(n);
deba@430
  2981
      n._in = true;
deba@430
  2982
    }
deba@430
  2983
deba@430
  2984
    void next(Node& n) const {
deba@430
  2985
      if (n._in) {
deba@432
  2986
        n._in = false;
deba@430
  2987
      } else {
deba@432
  2988
        n._in = true;
deba@432
  2989
        _digraph->next(n);
deba@430
  2990
      }
deba@430
  2991
    }
deba@430
  2992
deba@430
  2993
    void first(Arc& e) const {
deba@430
  2994
      e._item.setSecond();
deba@430
  2995
      _digraph->first(e._item.second());
deba@430
  2996
      if (e._item.second() == INVALID) {
deba@430
  2997
        e._item.setFirst();
deba@432
  2998
        _digraph->first(e._item.first());
deba@430
  2999
      }
deba@430
  3000
    }
deba@430
  3001
deba@430
  3002
    void next(Arc& e) const {
deba@430
  3003
      if (e._item.secondState()) {
deba@432
  3004
        _digraph->next(e._item.second());
deba@430
  3005
        if (e._item.second() == INVALID) {
deba@430
  3006
          e._item.setFirst();
deba@430
  3007
          _digraph->first(e._item.first());
deba@430
  3008
        }
deba@430
  3009
      } else {
deba@432
  3010
        _digraph->next(e._item.first());
deba@432
  3011
      }
deba@430
  3012
    }
deba@430
  3013
deba@430
  3014
    void firstOut(Arc& e, const Node& n) const {
deba@430
  3015
      if (n._in) {
deba@430
  3016
        e._item.setSecond(n);
deba@430
  3017
      } else {
deba@430
  3018
        e._item.setFirst();
deba@432
  3019
        _digraph->firstOut(e._item.first(), n);
deba@430
  3020
      }
deba@430
  3021
    }
deba@430
  3022
deba@430
  3023
    void nextOut(Arc& e) const {
deba@430
  3024
      if (!e._item.firstState()) {
deba@432
  3025
        e._item.setFirst(INVALID);
deba@430
  3026
      } else {
deba@432
  3027
        _digraph->nextOut(e._item.first());
deba@432
  3028
      }
deba@430
  3029
    }
deba@430
  3030
deba@430
  3031
    void firstIn(Arc& e, const Node& n) const {
deba@430
  3032
      if (!n._in) {
deba@432
  3033
        e._item.setSecond(n);
deba@430
  3034
      } else {
deba@430
  3035
        e._item.setFirst();
deba@432
  3036
        _digraph->firstIn(e._item.first(), n);
deba@430
  3037
      }
deba@430
  3038
    }
deba@430
  3039
deba@430
  3040
    void nextIn(Arc& e) const {
deba@430
  3041
      if (!e._item.firstState()) {
deba@432
  3042
        e._item.setFirst(INVALID);
deba@430
  3043
      } else {
deba@432
  3044
        _digraph->nextIn(e._item.first());
deba@430
  3045
      }
deba@430
  3046
    }
deba@430
  3047
deba@430
  3048
    Node source(const Arc& e) const {
deba@430
  3049
      if (e._item.firstState()) {
deba@432
  3050
        return Node(_digraph->source(e._item.first()), false);
deba@430
  3051
      } else {
deba@432
  3052
        return Node(e._item.second(), true);
deba@430
  3053
      }
deba@430
  3054
    }
deba@430
  3055
deba@430
  3056
    Node target(const Arc& e) const {
deba@430
  3057
      if (e._item.firstState()) {
deba@432
  3058
        return Node(_digraph->target(e._item.first()), true);
deba@430
  3059
      } else {
deba@432
  3060
        return Node(e._item.second(), false);
deba@430
  3061
      }
deba@430
  3062
    }
deba@430
  3063
deba@430
  3064
    int id(const Node& n) const {
deba@430
  3065
      return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
deba@430
  3066
    }
deba@430
  3067
    Node nodeFromId(int ix) const {
deba@430
  3068
      return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
deba@430
  3069
    }
deba@430
  3070
    int maxNodeId() const {
deba@430
  3071
      return 2 * _digraph->maxNodeId() + 1;
deba@430
  3072
    }
deba@430
  3073
deba@430
  3074
    int id(const Arc& e) const {
deba@430
  3075
      if (e._item.firstState()) {
deba@430
  3076
        return _digraph->id(e._item.first()) << 1;
deba@430
  3077
      } else {
deba@430
  3078
        return (_digraph->id(e._item.second()) << 1) | 1;
deba@430
  3079
      }
deba@430
  3080
    }
deba@430
  3081
    Arc arcFromId(int ix) const {
deba@430
  3082
      if ((ix & 1) == 0) {
deba@430
  3083
        return Arc(_digraph->arcFromId(ix >> 1));
deba@430
  3084
      } else {
deba@430
  3085
        return Arc(_digraph->nodeFromId(ix >> 1));
deba@430
  3086
      }
deba@430
  3087
    }
deba@430
  3088
    int maxArcId() const {
deba@432
  3089
      return std::max(_digraph->maxNodeId() << 1,
deba@430
  3090
                      (_digraph->maxArcId() << 1) | 1);
deba@430
  3091
    }
deba@430
  3092
deba@430
  3093
    static bool inNode(const Node& n) {
deba@430
  3094
      return n._in;
deba@430
  3095
    }
deba@430
  3096
deba@430
  3097
    static bool outNode(const Node& n) {
deba@430
  3098
      return !n._in;
deba@430
  3099
    }
deba@430
  3100
deba@430
  3101
    static bool origArc(const Arc& e) {
deba@430
  3102
      return e._item.firstState();
deba@430
  3103
    }
deba@430
  3104
deba@430
  3105
    static bool bindArc(const Arc& e) {
deba@430
  3106
      return e._item.secondState();
deba@430
  3107
    }
deba@430
  3108
deba@430
  3109
    static Node inNode(const DigraphNode& n) {
deba@430
  3110
      return Node(n, true);
deba@430
  3111
    }
deba@430
  3112
deba@430
  3113
    static Node outNode(const DigraphNode& n) {
deba@430
  3114
      return Node(n, false);
deba@430
  3115
    }
deba@430
  3116
deba@430
  3117
    static Arc arc(const DigraphNode& n) {
deba@430
  3118
      return Arc(n);
deba@430
  3119
    }
deba@430
  3120
deba@430
  3121
    static Arc arc(const DigraphArc& e) {
deba@430
  3122
      return Arc(e);
deba@430
  3123
    }
deba@430
  3124
deba@430
  3125
    typedef True NodeNumTag;
deba@430
  3126
    int nodeNum() const {
deba@430
  3127
      return  2 * countNodes(*_digraph);
deba@430
  3128
    }
deba@430
  3129
kpeter@469
  3130
    typedef True ArcNumTag;
deba@430
  3131
    int arcNum() const {
deba@430
  3132
      return countArcs(*_digraph) + countNodes(*_digraph);
deba@430
  3133
    }
deba@430
  3134
kpeter@469
  3135
    typedef True FindArcTag;
deba@432
  3136
    Arc findArc(const Node& u, const Node& v,
deba@432
  3137
                const Arc& prev = INVALID) const {
kpeter@472
  3138
      if (inNode(u) && outNode(v)) {
kpeter@472
  3139
        if (static_cast<const DigraphNode&>(u) ==
kpeter@472
  3140
            static_cast<const DigraphNode&>(v) && prev == INVALID) {
kpeter@472
  3141
          return Arc(u);
deba@430
  3142
        }
kpeter@472
  3143
      }
kpeter@472
  3144
      else if (outNode(u) && inNode(v)) {
kpeter@472
  3145
        return Arc(::lemon::findArc(*_digraph, u, v, prev));
deba@430
  3146
      }
deba@430
  3147
      return INVALID;
deba@430
  3148
    }
deba@430
  3149
deba@430
  3150
  private:
deba@432
  3151
deba@430
  3152
    template <typename _Value>
deba@432
  3153
    class NodeMapBase
deba@430
  3154
      : public MapTraits<typename Parent::template NodeMap<_Value> > {
deba@430
  3155
      typedef typename Parent::template NodeMap<_Value> NodeImpl;
deba@430
  3156
    public:
deba@430
  3157
      typedef Node Key;
deba@430
  3158
      typedef _Value Value;
kpeter@472
  3159
      typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
kpeter@472
  3160
      typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
kpeter@472
  3161
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
kpeter@472
  3162
      typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
kpeter@472
  3163
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
deba@432
  3164
deba@432
  3165
      NodeMapBase(const Adaptor& adaptor)
deba@432
  3166
        : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
deba@432
  3167
      NodeMapBase(const Adaptor& adaptor, const Value& value)
deba@432
  3168
        : _in_map(*adaptor._digraph, value),
deba@432
  3169
          _out_map(*adaptor._digraph, value) {}
deba@430
  3170
deba@430
  3171
      void set(const Node& key, const Value& val) {
deba@432
  3172
        if (Adaptor::inNode(key)) { _in_map.set(key, val); }
deba@432
  3173
        else {_out_map.set(key, val); }
deba@430
  3174
      }
deba@432
  3175
kpeter@472
  3176
      ReturnValue operator[](const Node& key) {
deba@432
  3177
        if (Adaptor::inNode(key)) { return _in_map[key]; }
deba@432
  3178
        else { return _out_map[key]; }
deba@430
  3179
      }
deba@430
  3180
kpeter@472
  3181
      ConstReturnValue operator[](const Node& key) const {
deba@432
  3182
        if (Adaptor::inNode(key)) { return _in_map[key]; }
deba@432
  3183
        else { return _out_map[key]; }
deba@430
  3184
      }
deba@430
  3185
deba@430
  3186
    private:
deba@430
  3187
      NodeImpl _in_map, _out_map;
deba@430
  3188
    };
deba@430
  3189
deba@430
  3190
    template <typename _Value>
deba@432
  3191
    class ArcMapBase
deba@430
  3192
      : public MapTraits<typename Parent::template ArcMap<_Value> > {
deba@430
  3193
      typedef typename Parent::template ArcMap<_Value> ArcImpl;
deba@430
  3194
      typedef typename Parent::template NodeMap<_Value> NodeImpl;
deba@430
  3195
    public:
deba@430
  3196
      typedef Arc Key;
deba@430
  3197
      typedef _Value Value;
kpeter@472
  3198
      typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
kpeter@472
  3199
      typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
kpeter@472
  3200
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
kpeter@472
  3201
      typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
kpeter@472
  3202
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
deba@430
  3203
deba@432
  3204
      ArcMapBase(const Adaptor& adaptor)
deba@432
  3205
        : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
deba@432
  3206
      ArcMapBase(const Adaptor& adaptor, const Value& value)
deba@432
  3207
        : _arc_map(*adaptor._digraph, value),
deba@432
  3208
          _node_map(*adaptor._digraph, value) {}
deba@430
  3209
deba@430
  3210
      void set(const Arc& key, const Value& val) {
deba@432
  3211
        if (Adaptor::origArc(key)) {
deba@432
  3212
          _arc_map.set(key._item.first(), val);
deba@430
  3213
        } else {
deba@432
  3214
          _node_map.set(key._item.second(), val);
deba@430
  3215
        }
deba@430
  3216
      }
deba@432
  3217
kpeter@472
  3218
      ReturnValue operator[](const Arc& key) {
deba@432
  3219
        if (Adaptor::origArc(key)) {
deba@430
  3220
          return _arc_map[key._item.first()];
deba@430
  3221
        } else {
deba@430
  3222
          return _node_map[key._item.second()];
deba@430
  3223
        }
deba@430
  3224
      }
deba@430
  3225
kpeter@472
  3226
      ConstReturnValue operator[](const Arc& key) const {
deba@432
  3227
        if (Adaptor::origArc(key)) {
deba@430
  3228
          return _arc_map[key._item.first()];
deba@430
  3229
        } else {
deba@430
  3230
          return _node_map[key._item.second()];
deba@430
  3231
        }
deba@430
  3232
      }
deba@430
  3233
deba@430
  3234
    private:
deba@430
  3235
      ArcImpl _arc_map;
deba@430
  3236
      NodeImpl _node_map;
deba@430
  3237
    };
deba@430
  3238
deba@430
  3239
  public:
deba@430
  3240
deba@430
  3241
    template <typename _Value>
deba@432
  3242
    class NodeMap
deba@432
  3243
      : public SubMapExtender<Adaptor, NodeMapBase<_Value> >
deba@430
  3244
    {
deba@430
  3245
    public:
deba@430
  3246
      typedef _Value Value;
deba@430
  3247
      typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent;
deba@432
  3248
deba@432
  3249
      NodeMap(const Adaptor& adaptor)
deba@432
  3250
        : Parent(adaptor) {}
deba@432
  3251
deba@432
  3252
      NodeMap(const Adaptor& adaptor, const Value& value)
deba@432
  3253
        : Parent(adaptor, value) {}
deba@432
  3254
deba@430
  3255
    private:
deba@430
  3256
      NodeMap& operator=(const NodeMap& cmap) {
deba@432
  3257
        return operator=<NodeMap>(cmap);
deba@430
  3258
      }
deba@432
  3259
deba@430
  3260
      template <typename CMap>
deba@430
  3261
      NodeMap& operator=(const CMap& cmap) {
deba@430
  3262
        Parent::operator=(cmap);
deba@432
  3263
        return *this;
deba@430
  3264
      }
deba@430
  3265
    };
deba@430
  3266
deba@430
  3267
    template <typename _Value>
deba@432
  3268
    class ArcMap
deba@432
  3269
      : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
deba@430
  3270
    {
deba@430
  3271
    public:
deba@430
  3272
      typedef _Value Value;
deba@430
  3273
      typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
deba@432
  3274
deba@432
  3275
      ArcMap(const Adaptor& adaptor)
deba@432
  3276
        : Parent(adaptor) {}
deba@432
  3277
deba@432
  3278
      ArcMap(const Adaptor& adaptor, const Value& value)
deba@432
  3279
        : Parent(adaptor, value) {}
deba@432
  3280
deba@430
  3281
    private:
deba@430
  3282
      ArcMap& operator=(const ArcMap& cmap) {
deba@432
  3283
        return operator=<ArcMap>(cmap);
deba@430
  3284
      }
deba@432
  3285
deba@430
  3286
      template <typename CMap>
deba@430
  3287
      ArcMap& operator=(const CMap& cmap) {
deba@430
  3288
        Parent::operator=(cmap);
deba@432
  3289
        return *this;
deba@430
  3290
      }
deba@430
  3291
    };
deba@430
  3292
deba@430
  3293
  protected:
deba@430
  3294
deba@432
  3295
    SplitNodesBase() : _digraph(0) {}
deba@430
  3296
deba@430
  3297
    Digraph* _digraph;
deba@430
  3298
deba@430
  3299
    void setDigraph(Digraph& digraph) {
deba@430
  3300
      _digraph = &digraph;
deba@430
  3301
    }
deba@432
  3302
deba@430
  3303
  };
deba@430
  3304
deba@430
  3305
  /// \ingroup graph_adaptors
deba@430
  3306
  ///
kpeter@474
  3307
  /// \brief Adaptor class for splitting the nodes of a digraph.
deba@432
  3308
  ///
kpeter@474
  3309
  /// SplitNodes adaptor can be used for splitting each node into an
kpeter@474
  3310
  /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor
kpeter@474
  3311
  /// replaces each node \f$ u \f$ in the digraph with two nodes,
kpeter@474
  3312
  /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$.
kpeter@474
  3313
  /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the
kpeter@474
  3314
  /// new target of the arc will be \f$ u_{in} \f$ and similarly the
kpeter@474
  3315
  /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$.
kpeter@474
  3316
  /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$
kpeter@474
  3317
  /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph.
deba@430
  3318
  ///
kpeter@474
  3319
  /// The aim of this class is running an algorithm with respect to node
kpeter@474
  3320
  /// costs or capacities if the algorithm considers only arc costs or
kpeter@474
  3321
  /// capacities directly.
kpeter@474
  3322
  /// In this case you can use \c SplitNodes adaptor, and set the node
kpeter@474
  3323
  /// costs/capacities of the original digraph to the \e bind \e arcs
kpeter@474
  3324
  /// in the adaptor.
deba@430
  3325
  ///
kpeter@476
  3326
  /// \tparam GR The type of the adapted digraph.
kpeter@474
  3327
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
kpeter@474
  3328
  /// It is implicitly \c const.
kpeter@474
  3329
  ///
kpeter@474
  3330
  /// \note The \c Node type of this adaptor is converible to the \c Node
kpeter@474
  3331
  /// type of the adapted digraph.
kpeter@476
  3332
  template <typename GR>
kpeter@476
  3333
#ifdef DOXYGEN
kpeter@476
  3334
  class SplitNodes {
kpeter@476
  3335
#else
deba@432
  3336
  class SplitNodes
kpeter@476
  3337
    : public DigraphAdaptorExtender<SplitNodesBase<const GR> > {
kpeter@476
  3338
#endif
deba@430
  3339
  public:
kpeter@476
  3340
    typedef GR Digraph;
kpeter@476
  3341
    typedef DigraphAdaptorExtender<SplitNodesBase<const GR> > Parent;
deba@430
  3342
deba@431
  3343
    typedef typename Digraph::Node DigraphNode;
deba@431
  3344
    typedef typename Digraph::Arc DigraphArc;
deba@431
  3345
deba@430
  3346
    typedef typename Parent::Node Node;
deba@430
  3347
    typedef typename Parent::Arc Arc;
deba@430
  3348
kpeter@474
  3349
    /// \brief Constructor
deba@430
  3350
    ///
deba@430
  3351
    /// Constructor of the adaptor.
kpeter@471
  3352
    SplitNodes(const Digraph& g) {
deba@430
  3353
      Parent::setDigraph(g);
deba@430
  3354
    }
deba@430
  3355
kpeter@474
  3356
    /// \brief Returns \c true if the given node is an in-node.
deba@431
  3357
    ///
kpeter@474
  3358
    /// Returns \c true if the given node is an in-node.
deba@431
  3359
    static bool inNode(const Node& n) {
deba@431
  3360
      return Parent::inNode(n);
deba@431
  3361
    }
deba@431
  3362
kpeter@474
  3363
    /// \brief Returns \c true if the given node is an out-node.
deba@431
  3364
    ///
kpeter@474
  3365
    /// Returns \c true if the given node is an out-node.
deba@431
  3366
    static bool outNode(const Node& n) {
deba@431
  3367
      return Parent::outNode(n);
deba@431
  3368
    }
deba@431
  3369
kpeter@474
  3370
    /// \brief Returns \c true if the given arc is an original arc.
deba@431
  3371
    ///
kpeter@474
  3372
    /// Returns \c true if the given arc is one of the arcs in the
kpeter@474
  3373
    /// original digraph.
deba@431
  3374
    static bool origArc(const Arc& a) {
deba@431
  3375
      return Parent::origArc(a);
deba@431
  3376
    }
deba@431
  3377
kpeter@474
  3378
    /// \brief Returns \c true if the given arc is a bind arc.
deba@431
  3379
    ///
kpeter@474
  3380
    /// Returns \c true if the given arc is a bind arc, i.e. it connects
kpeter@474
  3381
    /// an in-node and an out-node.
deba@431
  3382
    static bool bindArc(const Arc& a) {
deba@431
  3383
      return Parent::bindArc(a);
deba@431
  3384
    }
deba@431
  3385
kpeter@474
  3386
    /// \brief Returns the in-node created from the given original node.
deba@431
  3387
    ///
kpeter@474
  3388
    /// Returns the in-node created from the given original node.
deba@431
  3389
    static Node inNode(const DigraphNode& n) {
deba@431
  3390
      return Parent::inNode(n);
deba@431
  3391
    }
deba@431
  3392
kpeter@474
  3393
    /// \brief Returns the out-node created from the given original node.
deba@431
  3394
    ///
kpeter@474
  3395
    /// Returns the out-node created from the given original node.
deba@431
  3396
    static Node outNode(const DigraphNode& n) {
deba@431
  3397
      return Parent::outNode(n);
deba@431
  3398
    }
deba@431
  3399
kpeter@474
  3400
    /// \brief Returns the bind arc that corresponds to the given
kpeter@474
  3401
    /// original node.
deba@432
  3402
    ///
kpeter@474
  3403
    /// Returns the bind arc in the adaptor that corresponds to the given
kpeter@474
  3404
    /// original node, i.e. the arc connecting the in-node and out-node
kpeter@474
  3405
    /// of \c n.
deba@431
  3406
    static Arc arc(const DigraphNode& n) {
deba@431
  3407
      return Parent::arc(n);
deba@431
  3408
    }
deba@431
  3409
kpeter@474
  3410
    /// \brief Returns the arc that corresponds to the given original arc.
deba@432
  3411
    ///
kpeter@474
  3412
    /// Returns the arc in the adaptor that corresponds to the given
kpeter@474
  3413
    /// original arc.
deba@431
  3414
    static Arc arc(const DigraphArc& a) {
deba@431
  3415
      return Parent::arc(a);
deba@431
  3416
    }
deba@431
  3417
kpeter@474
  3418
    /// \brief Node map combined from two original node maps
deba@430
  3419
    ///
kpeter@474
  3420
    /// This map adaptor class adapts two node maps of the original digraph
kpeter@474
  3421
    /// to get a node map of the split digraph.
kpeter@474
  3422
    /// Its value type is inherited from the first node map type
kpeter@474
  3423
    /// (\c InNodeMap).
deba@430
  3424
    template <typename InNodeMap, typename OutNodeMap>
deba@430
  3425
    class CombinedNodeMap {
deba@430
  3426
    public:
deba@430
  3427
kpeter@474
  3428
      /// The key type of the map
deba@430
  3429
      typedef Node Key;
kpeter@474
  3430
      /// The value type of the map
deba@430
  3431
      typedef typename InNodeMap::Value Value;
deba@430
  3432
kpeter@472
  3433
      typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag;
kpeter@472
  3434
      typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue;
kpeter@472
  3435
      typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue;
kpeter@472
  3436
      typedef typename MapTraits<InNodeMap>::ReturnValue Reference;
kpeter@472
  3437
      typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference;
kpeter@472
  3438
kpeter@474
  3439
      /// Constructor
deba@432
  3440
      CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
deba@432
  3441
        : _in_map(in_map), _out_map(out_map) {}
deba@430
  3442
kpeter@474
  3443
      /// Returns the value associated with the given key.
kpeter@474
  3444
      Value operator[](const Key& key) const {
kpeter@474
  3445
        if (Parent::inNode(key)) {
kpeter@474
  3446
          return _in_map[key];
kpeter@474
  3447
        } else {
kpeter@474
  3448
          return _out_map[key];
kpeter@474
  3449
        }
kpeter@474
  3450
      }
kpeter@474
  3451
kpeter@474
  3452
      /// Returns a reference to the value associated with the given key.
deba@430
  3453
      Value& operator[](const Key& key) {
deba@432
  3454
        if (Parent::inNode(key)) {
deba@432
  3455
          return _in_map[key];
deba@432
  3456
        } else {
deba@432
  3457
          return _out_map[key];
deba@432
  3458
        }
deba@430
  3459
      }
deba@430
  3460
kpeter@474
  3461
      /// Sets the value associated with the given key.
deba@430
  3462
      void set(const Key& key, const Value& value) {
deba@432
  3463
        if (Parent::inNode(key)) {
deba@432
  3464
          _in_map.set(key, value);
deba@432
  3465
        } else {
deba@432
  3466
          _out_map.set(key, value);
deba@432
  3467
        }
deba@430
  3468
      }
deba@432
  3469
deba@430
  3470
    private:
deba@432
  3471
deba@430
  3472
      InNodeMap& _in_map;
deba@430
  3473
      OutNodeMap& _out_map;
deba@432
  3474
deba@430
  3475
    };
deba@430
  3476
deba@430
  3477
kpeter@474
  3478
    /// \brief Returns a combined node map
deba@432
  3479
    ///
kpeter@474
  3480
    /// This function just returns a combined node map.
deba@430
  3481
    template <typename InNodeMap, typename OutNodeMap>
deba@432
  3482
    static CombinedNodeMap<InNodeMap, OutNodeMap>
deba@430
  3483
    combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
deba@430
  3484
      return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
deba@430
  3485
    }
deba@430
  3486
deba@430
  3487
    template <typename InNodeMap, typename OutNodeMap>
deba@432
  3488
    static CombinedNodeMap<const InNodeMap, OutNodeMap>
deba@430
  3489
    combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
deba@430
  3490
      return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
deba@430
  3491
    }
deba@430
  3492
deba@430
  3493
    template <typename InNodeMap, typename OutNodeMap>
deba@432
  3494
    static CombinedNodeMap<InNodeMap, const OutNodeMap>
deba@430
  3495
    combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
deba@430
  3496
      return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
deba@430
  3497
    }
deba@430
  3498
deba@430
  3499
    template <typename InNodeMap, typename OutNodeMap>
deba@432
  3500
    static CombinedNodeMap<const InNodeMap, const OutNodeMap>
deba@430
  3501
    combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
deba@432
  3502
      return CombinedNodeMap<const InNodeMap,
deba@430
  3503
        const OutNodeMap>(in_map, out_map);
deba@430
  3504
    }
deba@430
  3505
kpeter@474
  3506
    /// \brief Arc map combined from an arc map and a node map of the
kpeter@474
  3507
    /// original digraph.
deba@430
  3508
    ///
kpeter@474
  3509
    /// This map adaptor class adapts an arc map and a node map of the
kpeter@474
  3510
    /// original digraph to get an arc map of the split digraph.
kpeter@474
  3511
    /// Its value type is inherited from the original arc map type
kpeter@476
  3512
    /// (\c ArcMap).
kpeter@476
  3513
    template <typename ArcMap, typename NodeMap>
deba@430
  3514
    class CombinedArcMap {
deba@430
  3515
    public:
deba@432
  3516
kpeter@474
  3517
      /// The key type of the map
deba@430
  3518
      typedef Arc Key;
kpeter@474
  3519
      /// The value type of the map
kpeter@476
  3520
      typedef typename ArcMap::Value Value;
kpeter@476
  3521
kpeter@476
  3522
      typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag;
kpeter@476
  3523
      typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue;
kpeter@476
  3524
      typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue;
kpeter@476
  3525
      typedef typename MapTraits<ArcMap>::ReturnValue Reference;
kpeter@476
  3526
      typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReference;
kpeter@472
  3527
kpeter@474
  3528
      /// Constructor
kpeter@476
  3529
      CombinedArcMap(ArcMap& arc_map, NodeMap& node_map)
deba@432
  3530
        : _arc_map(arc_map), _node_map(node_map) {}
deba@430
  3531
kpeter@474
  3532
      /// Returns the value associated with the given key.
kpeter@474
  3533
      Value operator[](const Key& arc) const {
kpeter@474
  3534
        if (Parent::origArc(arc)) {
kpeter@474
  3535
          return _arc_map[arc];
kpeter@474
  3536
        } else {
kpeter@474
  3537
          return _node_map[arc];
kpeter@474
  3538
        }
kpeter@474
  3539
      }
kpeter@474
  3540
kpeter@474
  3541
      /// Returns a reference to the value associated with the given key.
kpeter@474
  3542
      Value& operator[](const Key& arc) {
kpeter@474
  3543
        if (Parent::origArc(arc)) {
kpeter@474
  3544
          return _arc_map[arc];
kpeter@474
  3545
        } else {
kpeter@474
  3546
          return _node_map[arc];
kpeter@474
  3547
        }
kpeter@474
  3548
      }
kpeter@474
  3549
kpeter@474
  3550
      /// Sets the value associated with the given key.
deba@430
  3551
      void set(const Arc& arc, const Value& val) {
deba@432
  3552
        if (Parent::origArc(arc)) {
deba@432
  3553
          _arc_map.set(arc, val);
deba@432
  3554
        } else {
deba@432
  3555
          _node_map.set(arc, val);
deba@432
  3556
        }
deba@430
  3557
      }
deba@430
  3558
deba@430
  3559
    private:
kpeter@476
  3560
      ArcMap& _arc_map;
kpeter@476
  3561
      NodeMap& _node_map;
deba@430
  3562
    };
deba@432
  3563
kpeter@474
  3564
    /// \brief Returns a combined arc map
deba@432
  3565
    ///
kpeter@474
  3566
    /// This function just returns a combined arc map.
kpeter@476
  3567
    template <typename ArcMap, typename NodeMap>
kpeter@476
  3568
    static CombinedArcMap<ArcMap, NodeMap>
kpeter@476
  3569
    combinedArcMap(ArcMap& arc_map, NodeMap& node_map) {
kpeter@476
  3570
      return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map);
deba@430
  3571
    }
deba@430
  3572
kpeter@476
  3573
    template <typename ArcMap, typename NodeMap>
kpeter@476
  3574
    static CombinedArcMap<const ArcMap, NodeMap>
kpeter@476
  3575
    combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) {
kpeter@476
  3576
      return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map);
deba@430
  3577
    }
deba@430
  3578
kpeter@476
  3579
    template <typename ArcMap, typename NodeMap>
kpeter@476
  3580
    static CombinedArcMap<ArcMap, const NodeMap>
kpeter@476
  3581
    combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) {
kpeter@476
  3582
      return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map);
deba@430
  3583
    }
deba@430
  3584
kpeter@476
  3585
    template <typename ArcMap, typename NodeMap>
kpeter@476
  3586
    static CombinedArcMap<const ArcMap, const NodeMap>
kpeter@476
  3587
    combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) {
kpeter@476
  3588
      return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map);
deba@430
  3589
    }
deba@430
  3590
deba@430
  3591
  };
deba@430
  3592
kpeter@474
  3593
  /// \brief Returns a (read-only) SplitNodes adaptor
deba@430
  3594
  ///
kpeter@474
  3595
  /// This function just returns a (read-only) \ref SplitNodes adaptor.
kpeter@474
  3596
  /// \ingroup graph_adaptors
kpeter@474
  3597
  /// \relates SplitNodes
kpeter@476
  3598
  template<typename GR>
kpeter@476
  3599
  SplitNodes<GR>
kpeter@476
  3600
  splitNodes(const GR& digraph) {
kpeter@476
  3601
    return SplitNodes<GR>(digraph);
deba@430
  3602
  }
deba@430
  3603
deba@430
  3604
} //namespace lemon
deba@430
  3605
deba@432
  3606
#endif //LEMON_ADAPTORS_H