lemon/adaptors.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 09 Jan 2009 14:03:25 +0100
changeset 452 aea2dc0518ce
parent 451 fbd6e04acf44
child 453 c246659c8b19
permissions -rw-r--r--
Rename convenience functions in subgraph adaptors (#67)

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