COIN-OR::LEMON - Graph Library

source: lemon/lemon/adaptors.h @ 834:c2230649a493

Last change on this file since 834:c2230649a493 was 834:c2230649a493, checked in by Peter Kovacs <kpeter@…>, 14 years ago

Various doc improvements (#331)

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