COIN-OR::LEMON - Graph Library

source: lemon/lemon/adaptors.h @ 559:9b9ffe7d9b75

Last change on this file since 559:9b9ffe7d9b75 was 559:9b9ffe7d9b75, checked in by Balazs Dezso <deba@…>, 15 years ago

Fixes for MSVC 2008 in grap_adaptors.h and edge_set.h (#194)

Several renamings and changes in adaptors and edge sets

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