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