COIN-OR::LEMON - Graph Library

source: lemon-main/lemon/adaptors.h @ 495:dab9e610e37d

Last change on this file since 495:dab9e610e37d was 495:dab9e610e37d, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Fixes in adaptors.h to compile on AIX

File size: 110.0 KB
RevLine 
[416]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[414]2 *
[416]3 * This file is a part of LEMON, a generic C++ optimization library.
[414]4 *
[454]5 * Copyright (C) 2003-2009
[414]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
[416]19#ifndef LEMON_ADAPTORS_H
20#define LEMON_ADAPTORS_H
21
22/// \ingroup graph_adaptors
23/// \file
[451]24/// \brief Adaptor classes for digraphs and graphs
[414]25///
[416]26/// This file contains several useful adaptors for digraphs and graphs.
[414]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
[488]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>
[414]46  class DigraphAdaptorBase {
47  public:
[488]48    typedef DGR Digraph;
[414]49    typedef DigraphAdaptorBase Adaptor;
50
51  protected:
[488]52    DGR* _digraph;
[414]53    DigraphAdaptorBase() : _digraph(0) { }
[488]54    void initialize(DGR& digraph) { _digraph = &digraph; }
[414]55
56  public:
[488]57    DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { }
58
59    typedef typename DGR::Node Node;
60    typedef typename DGR::Arc Arc;
[416]61
[414]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
[488]75    typedef NodeNumTagIndicator<DGR> NodeNumTag;
[414]76    int nodeNum() const { return _digraph->nodeNum(); }
[416]77
[488]78    typedef ArcNumTagIndicator<DGR> ArcNumTag;
[414]79    int arcNum() const { return _digraph->arcNum(); }
80
[488]81    typedef FindArcTagIndicator<DGR> FindArcTag;
[448]82    Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
[414]83      return _digraph->findArc(u, v, prev);
84    }
[416]85
[414]86    Node addNode() { return _digraph->addNode(); }
87    Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
88
[448]89    void erase(const Node& n) { _digraph->erase(n); }
90    void erase(const Arc& a) { _digraph->erase(a); }
91
92    void clear() { _digraph->clear(); }
[416]93
[414]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
[488]103    typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
[416]104    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
[414]105
[488]106    typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier;
[416]107    ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
108
[488]109    template <typename V>
110    class NodeMap : public DGR::template NodeMap<V> {
[414]111    public:
112
[488]113      typedef typename DGR::template NodeMap<V> Parent;
[414]114
[416]115      explicit NodeMap(const Adaptor& adaptor)
116        : Parent(*adaptor._digraph) {}
[414]117
[488]118      NodeMap(const Adaptor& adaptor, const V& value)
[416]119        : Parent(*adaptor._digraph, value) { }
[414]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      }
[416]131
[414]132    };
133
[488]134    template <typename V>
135    class ArcMap : public DGR::template ArcMap<V> {
[414]136    public:
[416]137
[488]138      typedef typename DGR::template ArcMap<V> Parent;
139
140      explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor)
[416]141        : Parent(*adaptor._digraph) {}
[414]142
[488]143      ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value)
[416]144        : Parent(*adaptor._digraph, value) {}
[414]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
[488]161  template<typename GR>
[416]162  class GraphAdaptorBase {
163  public:
[488]164    typedef GR Graph;
[416]165
166  protected:
[488]167    GR* _graph;
[416]168
169    GraphAdaptorBase() : _graph(0) {}
170
[488]171    void initialize(GR& graph) { _graph = &graph; }
[416]172
173  public:
[488]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;
[416]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
[446]205    typedef ArcNumTagIndicator<Graph> ArcNumTag;
206    int arcNum() const { return _graph->arcNum(); }
207
[416]208    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
209    int edgeNum() const { return _graph->edgeNum(); }
210
[446]211    typedef FindArcTagIndicator<Graph> FindArcTag;
[448]212    Arc findArc(const Node& u, const Node& v,
213                const Arc& prev = INVALID) const {
[416]214      return _graph->findArc(u, v, prev);
215    }
[446]216
217    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
[448]218    Edge findEdge(const Node& u, const Node& v,
219                  const Edge& prev = INVALID) const {
[416]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
[488]246    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
[416]247    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
248
[488]249    typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
[416]250    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
251
[488]252    typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier;
[416]253    EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
254
[488]255    template <typename V>
256    class NodeMap : public GR::template NodeMap<V> {
[416]257    public:
[488]258      typedef typename GR::template NodeMap<V> Parent;
259      explicit NodeMap(const GraphAdaptorBase<GR>& adapter)
[416]260        : Parent(*adapter._graph) {}
[488]261      NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
[416]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
[488]277    template <typename V>
278    class ArcMap : public GR::template ArcMap<V> {
[416]279    public:
[488]280      typedef typename GR::template ArcMap<V> Parent;
281      explicit ArcMap(const GraphAdaptorBase<GR>& adapter)
[416]282        : Parent(*adapter._graph) {}
[488]283      ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value)
[416]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
[488]298    template <typename V>
299    class EdgeMap : public GR::template EdgeMap<V> {
[416]300    public:
[488]301      typedef typename GR::template EdgeMap<V> Parent;
302      explicit EdgeMap(const GraphAdaptorBase<GR>& adapter)
[416]303        : Parent(*adapter._graph) {}
[488]304      EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
[416]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  };
[414]320
[488]321  template <typename DGR>
322  class ReverseDigraphBase : public DigraphAdaptorBase<DGR> {
[414]323  public:
[488]324    typedef DGR Digraph;
325    typedef DigraphAdaptorBase<DGR> Parent;
[414]326  protected:
[416]327    ReverseDigraphBase() : Parent() { }
[414]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
[416]341    Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
342
[488]343    typedef FindArcTagIndicator<DGR> FindArcTag;
[416]344    Arc findArc(const Node& u, const Node& v,
[448]345                const Arc& prev = INVALID) const {
[414]346      return Parent::findArc(v, u, prev);
347    }
348
349  };
[416]350
351  /// \ingroup graph_adaptors
[414]352  ///
[451]353  /// \brief Adaptor class for reversing the orientation of the arcs in
354  /// a digraph.
[414]355  ///
[451]356  /// ReverseDigraph can be used for reversing the arcs in a digraph.
357  /// It conforms to the \ref concepts::Digraph "Digraph" concept.
[414]358  ///
[451]359  /// The adapted digraph can also be modified through this adaptor
[453]360  /// by adding or removing nodes or arcs, unless the \c GR template
[451]361  /// parameter is set to be \c const.
362  ///
[488]363  /// \tparam DGR The type of the adapted digraph.
[451]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.
[488]369  template<typename DGR>
[453]370#ifdef DOXYGEN
371  class ReverseDigraph {
372#else
[416]373  class ReverseDigraph :
[488]374    public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > {
[453]375#endif
[414]376  public:
[453]377    /// The type of the adapted digraph.
[488]378    typedef DGR Digraph;
379    typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent;
[414]380  protected:
[416]381    ReverseDigraph() { }
[414]382  public:
[415]383
384    /// \brief Constructor
385    ///
[451]386    /// Creates a reverse digraph adaptor for the given digraph.
[488]387    explicit ReverseDigraph(DGR& digraph) {
388      Parent::initialize(digraph);
[414]389    }
390  };
391
[451]392  /// \brief Returns a read-only ReverseDigraph adaptor
[414]393  ///
[451]394  /// This function just returns a read-only \ref ReverseDigraph adaptor.
395  /// \ingroup graph_adaptors
396  /// \relates ReverseDigraph
[488]397  template<typename DGR>
398  ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
399    return ReverseDigraph<const DGR>(digraph);
[414]400  }
401
[451]402
[488]403  template <typename DGR, typename NF, typename AF, bool ch = true>
404  class SubDigraphBase : public DigraphAdaptorBase<DGR> {
[414]405  public:
[488]406    typedef DGR Digraph;
407    typedef NF NodeFilterMap;
408    typedef AF ArcFilterMap;
[414]409
[416]410    typedef SubDigraphBase Adaptor;
[488]411    typedef DigraphAdaptorBase<DGR> Parent;
[414]412  protected:
[488]413    NF* _node_filter;
414    AF* _arc_filter;
[416]415    SubDigraphBase()
[414]416      : Parent(), _node_filter(0), _arc_filter(0) { }
417
[488]418    void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
419      Parent::initialize(digraph);
[414]420      _node_filter = &node_filter;
[488]421      _arc_filter = &arc_filter;     
[414]422    }
423
424  public:
425
426    typedef typename Parent::Node Node;
427    typedef typename Parent::Arc Arc;
428
[416]429    void first(Node& i) const {
430      Parent::first(i);
431      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
[414]432    }
433
[416]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);
[414]440    }
441
[416]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);
[414]447    }
448
[416]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);
[414]454    }
455
[416]456    void next(Node& i) const {
457      Parent::next(i);
458      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
[414]459    }
460
[416]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);
[414]467    }
468
[416]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);
[414]474    }
475
[416]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);
[414]481    }
482
[452]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]; }
[414]488
489    typedef False NodeNumTag;
[446]490    typedef False ArcNumTag;
491
[488]492    typedef FindArcTagIndicator<DGR> FindArcTag;
[416]493    Arc findArc(const Node& source, const Node& target,
[448]494                const Arc& prev = INVALID) const {
[414]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
[488]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>)> {
[414]511    public:
[488]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) {}
[416]520
[414]521    private:
522      NodeMap& operator=(const NodeMap& cmap) {
[416]523        return operator=<NodeMap>(cmap);
[414]524      }
[416]525
[414]526      template <typename CMap>
527      NodeMap& operator=(const CMap& cmap) {
[488]528        Parent::operator=(cmap);
[416]529        return *this;
[414]530      }
531    };
532
[488]533    template <typename V>
534    class ArcMap
535      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
536              LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
[414]537    public:
[488]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) {}
[416]546
[414]547    private:
548      ArcMap& operator=(const ArcMap& cmap) {
[416]549        return operator=<ArcMap>(cmap);
[414]550      }
[416]551
[414]552      template <typename CMap>
553      ArcMap& operator=(const CMap& cmap) {
[488]554        Parent::operator=(cmap);
[416]555        return *this;
[414]556      }
557    };
558
559  };
560
[488]561  template <typename DGR, typename NF, typename AF>
562  class SubDigraphBase<DGR, NF, AF, false>
563    : public DigraphAdaptorBase<DGR> {
[414]564  public:
[488]565    typedef DGR Digraph;
566    typedef NF NodeFilterMap;
567    typedef AF ArcFilterMap;
[414]568
[416]569    typedef SubDigraphBase Adaptor;
[414]570    typedef DigraphAdaptorBase<Digraph> Parent;
571  protected:
[488]572    NF* _node_filter;
573    AF* _arc_filter;
[416]574    SubDigraphBase()
[414]575      : Parent(), _node_filter(0), _arc_filter(0) { }
576
[488]577    void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
578      Parent::initialize(digraph);
[414]579      _node_filter = &node_filter;
[488]580      _arc_filter = &arc_filter;     
[414]581    }
582
583  public:
584
585    typedef typename Parent::Node Node;
586    typedef typename Parent::Arc Arc;
587
[416]588    void first(Node& i) const {
589      Parent::first(i);
590      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
[414]591    }
592
[416]593    void first(Arc& i) const {
594      Parent::first(i);
595      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
[414]596    }
597
[416]598    void firstIn(Arc& i, const Node& n) const {
599      Parent::firstIn(i, n);
600      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
[414]601    }
602
[416]603    void firstOut(Arc& i, const Node& n) const {
604      Parent::firstOut(i, n);
605      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
[414]606    }
607
[416]608    void next(Node& i) const {
609      Parent::next(i);
610      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
[414]611    }
[416]612    void next(Arc& i) const {
613      Parent::next(i);
614      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
[414]615    }
[416]616    void nextIn(Arc& i) const {
617      Parent::nextIn(i);
618      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
[414]619    }
620
[416]621    void nextOut(Arc& i) const {
622      Parent::nextOut(i);
623      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
[414]624    }
625
[452]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]; }
[414]631
632    typedef False NodeNumTag;
[446]633    typedef False ArcNumTag;
634
[488]635    typedef FindArcTagIndicator<DGR> FindArcTag;
[416]636    Arc findArc(const Node& source, const Node& target,
[448]637                const Arc& prev = INVALID) const {
[414]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
[488]648    template <typename V>
649    class NodeMap
650      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
651          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
[414]652    public:
[488]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) {}
[416]661
[414]662    private:
663      NodeMap& operator=(const NodeMap& cmap) {
[416]664        return operator=<NodeMap>(cmap);
[414]665      }
[416]666
[414]667      template <typename CMap>
668      NodeMap& operator=(const CMap& cmap) {
[488]669        Parent::operator=(cmap);
[416]670        return *this;
[414]671      }
672    };
673
[488]674    template <typename V>
675    class ArcMap
676      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
677          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
[414]678    public:
[488]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) {}
[416]687
[414]688    private:
689      ArcMap& operator=(const ArcMap& cmap) {
[416]690        return operator=<ArcMap>(cmap);
[414]691      }
[416]692
[414]693      template <typename CMap>
694      ArcMap& operator=(const CMap& cmap) {
[488]695        Parent::operator=(cmap);
[416]696        return *this;
[414]697      }
698    };
699
700  };
701
702  /// \ingroup graph_adaptors
703  ///
[451]704  /// \brief Adaptor class for hiding nodes and arcs in a digraph
[416]705  ///
[451]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
[453]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.
[416]713  ///
[451]714  /// The adapted digraph can also be modified through this adaptor
[453]715  /// by adding or removing nodes or arcs, unless the \c GR template
[451]716  /// parameter is set to be \c const.
717  ///
[488]718  /// \tparam DGR The type of the adapted digraph.
[451]719  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
720  /// It can also be specified to be \c const.
[453]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
[488]724  /// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>".
[453]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
[488]728  /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
[451]729  ///
730  /// \note The \c Node and \c Arc types of this adaptor and the adapted
731  /// digraph are convertible to each other.
[416]732  ///
733  /// \see FilterNodes
734  /// \see FilterArcs
[451]735#ifdef DOXYGEN
[488]736  template<typename DGR, typename NF, typename AF>
[453]737  class SubDigraph {
[451]738#else
[488]739  template<typename DGR,
740           typename NF = typename DGR::template NodeMap<bool>,
741           typename AF = typename DGR::template ArcMap<bool> >
[453]742  class SubDigraph :
[488]743    public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > {
[451]744#endif
[414]745  public:
[451]746    /// The type of the adapted digraph.
[488]747    typedef DGR Digraph;
[451]748    /// The type of the node filter map.
[453]749    typedef NF NodeFilterMap;
[451]750    /// The type of the arc filter map.
[453]751    typedef AF ArcFilterMap;
752
[488]753    typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> >
[453]754      Parent;
[414]755
[415]756    typedef typename Parent::Node Node;
757    typedef typename Parent::Arc Arc;
758
[414]759  protected:
[416]760    SubDigraph() { }
[414]761  public:
762
[415]763    /// \brief Constructor
764    ///
[451]765    /// Creates a subdigraph for the given digraph with the
766    /// given node and arc filter maps.
[488]767    SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) {
768      Parent::initialize(digraph, node_filter, arc_filter);
[414]769    }
770
[452]771    /// \brief Sets the status of the given node
[415]772    ///
[452]773    /// This function sets the status of the given node.
[451]774    /// It is done by simply setting the assigned value of \c n
[452]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
[415]779    ///
[452]780    /// This function sets the status of the given arc.
[451]781    /// It is done by simply setting the assigned value of \c a
[452]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
[415]786    ///
[452]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
[415]792    ///
[452]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
[415]798    ///
[452]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
[415]805    ///
[452]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); }
[415]822
[414]823  };
824
[451]825  /// \brief Returns a read-only SubDigraph adaptor
[414]826  ///
[451]827  /// This function just returns a read-only \ref SubDigraph adaptor.
828  /// \ingroup graph_adaptors
829  /// \relates SubDigraph
[488]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);
[414]836  }
837
[488]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);
[414]844  }
845
[488]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);
[414]852  }
853
[488]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);
[414]860  }
861
862
[488]863  template <typename GR, typename NF, typename EF, bool ch = true>
864  class SubGraphBase : public GraphAdaptorBase<GR> {
[416]865  public:
[488]866    typedef GR Graph;
867    typedef NF NodeFilterMap;
868    typedef EF EdgeFilterMap;
[449]869
[416]870    typedef SubGraphBase Adaptor;
[488]871    typedef GraphAdaptorBase<GR> Parent;
[416]872  protected:
873
[488]874    NF* _node_filter;
875    EF* _edge_filter;
[416]876
877    SubGraphBase()
[488]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;
[416]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);
[488]894      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
[416]895    }
896
897    void first(Arc& i) const {
898      Parent::first(i);
[488]899      while (i!=INVALID && (!(*_edge_filter)[i]
900                            || !(*_node_filter)[Parent::source(i)]
901                            || !(*_node_filter)[Parent::target(i)]))
[416]902        Parent::next(i);
903    }
904
905    void first(Edge& i) const {
906      Parent::first(i);
[488]907      while (i!=INVALID && (!(*_edge_filter)[i]
908                            || !(*_node_filter)[Parent::u(i)]
909                            || !(*_node_filter)[Parent::v(i)]))
[416]910        Parent::next(i);
911    }
912
913    void firstIn(Arc& i, const Node& n) const {
914      Parent::firstIn(i, n);
[488]915      while (i!=INVALID && (!(*_edge_filter)[i]
916                            || !(*_node_filter)[Parent::source(i)]))
[416]917        Parent::nextIn(i);
918    }
919
920    void firstOut(Arc& i, const Node& n) const {
921      Parent::firstOut(i, n);
[488]922      while (i!=INVALID && (!(*_edge_filter)[i]
923                            || !(*_node_filter)[Parent::target(i)]))
[416]924        Parent::nextOut(i);
925    }
926
927    void firstInc(Edge& i, bool& d, const Node& n) const {
928      Parent::firstInc(i, d, n);
[488]929      while (i!=INVALID && (!(*_edge_filter)[i]
930                            || !(*_node_filter)[Parent::u(i)]
931                            || !(*_node_filter)[Parent::v(i)]))
[416]932        Parent::nextInc(i, d);
933    }
934
935    void next(Node& i) const {
936      Parent::next(i);
[488]937      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
[416]938    }
939
940    void next(Arc& i) const {
941      Parent::next(i);
[488]942      while (i!=INVALID && (!(*_edge_filter)[i]
943                            || !(*_node_filter)[Parent::source(i)]
944                            || !(*_node_filter)[Parent::target(i)]))
[416]945        Parent::next(i);
946    }
947
948    void next(Edge& i) const {
949      Parent::next(i);
[488]950      while (i!=INVALID && (!(*_edge_filter)[i]
951                            || !(*_node_filter)[Parent::u(i)]
952                            || !(*_node_filter)[Parent::v(i)]))
[416]953        Parent::next(i);
954    }
955
956    void nextIn(Arc& i) const {
957      Parent::nextIn(i);
[488]958      while (i!=INVALID && (!(*_edge_filter)[i]
959                            || !(*_node_filter)[Parent::source(i)]))
[416]960        Parent::nextIn(i);
961    }
962
963    void nextOut(Arc& i) const {
964      Parent::nextOut(i);
[488]965      while (i!=INVALID && (!(*_edge_filter)[i]
966                            || !(*_node_filter)[Parent::target(i)]))
[416]967        Parent::nextOut(i);
968    }
969
970    void nextInc(Edge& i, bool& d) const {
971      Parent::nextInc(i, d);
[488]972      while (i!=INVALID && (!(*_edge_filter)[i]
973                            || !(*_node_filter)[Parent::u(i)]
974                            || !(*_node_filter)[Parent::v(i)]))
[416]975        Parent::nextInc(i, d);
976    }
977
[488]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]; }
[416]983
984    typedef False NodeNumTag;
[446]985    typedef False ArcNumTag;
[416]986    typedef False EdgeNumTag;
987
[446]988    typedef FindArcTagIndicator<Graph> FindArcTag;
[416]989    Arc findArc(const Node& u, const Node& v,
[448]990                const Arc& prev = INVALID) const {
[488]991      if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
[416]992        return INVALID;
993      }
994      Arc arc = Parent::findArc(u, v, prev);
[488]995      while (arc != INVALID && !(*_edge_filter)[arc]) {
[416]996        arc = Parent::findArc(u, v, arc);
997      }
998      return arc;
999    }
[446]1000
1001    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
[416]1002    Edge findEdge(const Node& u, const Node& v,
[448]1003                  const Edge& prev = INVALID) const {
[488]1004      if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
[416]1005        return INVALID;
1006      }
1007      Edge edge = Parent::findEdge(u, v, prev);
[488]1008      while (edge != INVALID && !(*_edge_filter)[edge]) {
[416]1009        edge = Parent::findEdge(u, v, edge);
1010      }
1011      return edge;
1012    }
1013
[488]1014    template <typename V>
1015    class NodeMap
1016      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1017          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
[416]1018    public:
[488]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) {}
[416]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) {
[488]1035        Parent::operator=(cmap);
[416]1036        return *this;
1037      }
1038    };
1039
[488]1040    template <typename V>
1041    class ArcMap
1042      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1043          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
[416]1044    public:
[488]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) {}
[416]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) {
[488]1061        Parent::operator=(cmap);
[416]1062        return *this;
1063      }
1064    };
1065
[488]1066    template <typename V>
1067    class EdgeMap
1068      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1069        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
[416]1070    public:
[488]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) {}
[416]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) {
[488]1088        Parent::operator=(cmap);
[416]1089        return *this;
1090      }
1091    };
1092
1093  };
1094
[488]1095  template <typename GR, typename NF, typename EF>
1096  class SubGraphBase<GR, NF, EF, false>
1097    : public GraphAdaptorBase<GR> {
[416]1098  public:
[488]1099    typedef GR Graph;
1100    typedef NF NodeFilterMap;
1101    typedef EF EdgeFilterMap;
[449]1102
[416]1103    typedef SubGraphBase Adaptor;
[488]1104    typedef GraphAdaptorBase<GR> Parent;
[416]1105  protected:
[488]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;
[416]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);
[488]1125      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
[416]1126    }
1127
1128    void first(Arc& i) const {
1129      Parent::first(i);
[488]1130      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
[416]1131    }
1132
1133    void first(Edge& i) const {
1134      Parent::first(i);
[488]1135      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
[416]1136    }
1137
1138    void firstIn(Arc& i, const Node& n) const {
1139      Parent::firstIn(i, n);
[488]1140      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
[416]1141    }
1142
1143    void firstOut(Arc& i, const Node& n) const {
1144      Parent::firstOut(i, n);
[488]1145      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
[416]1146    }
1147
1148    void firstInc(Edge& i, bool& d, const Node& n) const {
1149      Parent::firstInc(i, d, n);
[488]1150      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
[416]1151    }
1152
1153    void next(Node& i) const {
1154      Parent::next(i);
[488]1155      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
[416]1156    }
1157    void next(Arc& i) const {
1158      Parent::next(i);
[488]1159      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
[416]1160    }
1161    void next(Edge& i) const {
1162      Parent::next(i);
[488]1163      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
[416]1164    }
1165    void nextIn(Arc& i) const {
1166      Parent::nextIn(i);
[488]1167      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
[416]1168    }
1169
1170    void nextOut(Arc& i) const {
1171      Parent::nextOut(i);
[488]1172      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
[416]1173    }
1174    void nextInc(Edge& i, bool& d) const {
1175      Parent::nextInc(i, d);
[488]1176      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
[416]1177    }
1178
[488]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]; }
[416]1184
1185    typedef False NodeNumTag;
[446]1186    typedef False ArcNumTag;
[416]1187    typedef False EdgeNumTag;
1188
[446]1189    typedef FindArcTagIndicator<Graph> FindArcTag;
[416]1190    Arc findArc(const Node& u, const Node& v,
[448]1191                const Arc& prev = INVALID) const {
[416]1192      Arc arc = Parent::findArc(u, v, prev);
[488]1193      while (arc != INVALID && !(*_edge_filter)[arc]) {
[416]1194        arc = Parent::findArc(u, v, arc);
1195      }
1196      return arc;
1197    }
[446]1198
1199    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
[416]1200    Edge findEdge(const Node& u, const Node& v,
[448]1201                  const Edge& prev = INVALID) const {
[416]1202      Edge edge = Parent::findEdge(u, v, prev);
[488]1203      while (edge != INVALID && !(*_edge_filter)[edge]) {
[416]1204        edge = Parent::findEdge(u, v, edge);
1205      }
1206      return edge;
1207    }
1208
[488]1209    template <typename V>
1210    class NodeMap
1211      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1212          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
[416]1213    public:
[488]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) {}
[416]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) {
[488]1230        Parent::operator=(cmap);
[416]1231        return *this;
1232      }
1233    };
1234
[488]1235    template <typename V>
1236    class ArcMap
1237      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1238          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
[416]1239    public:
[488]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) {}
[416]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) {
[488]1256        Parent::operator=(cmap);
[416]1257        return *this;
1258      }
1259    };
1260
[488]1261    template <typename V>
1262    class EdgeMap
1263      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1264        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
[416]1265    public:
[488]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) {}
[416]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) {
[488]1283        Parent::operator=(cmap);
[416]1284        return *this;
1285      }
1286    };
1287
1288  };
1289
1290  /// \ingroup graph_adaptors
[414]1291  ///
[451]1292  /// \brief Adaptor class for hiding nodes and edges in an undirected
1293  /// graph.
[414]1294  ///
[451]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
[453]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.
[416]1302  ///
[451]1303  /// The adapted graph can also be modified through this adaptor
[453]1304  /// by adding or removing nodes or edges, unless the \c GR template
[451]1305  /// parameter is set to be \c const.
1306  ///
[453]1307  /// \tparam GR The type of the adapted graph.
[451]1308  /// It must conform to the \ref concepts::Graph "Graph" concept.
1309  /// It can also be specified to be \c const.
[453]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>".
[451]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.
[416]1321  ///
1322  /// \see FilterNodes
1323  /// \see FilterEdges
[451]1324#ifdef DOXYGEN
[453]1325  template<typename GR, typename NF, typename EF>
1326  class SubGraph {
[451]1327#else
[453]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> > {
[451]1333#endif
[414]1334  public:
[451]1335    /// The type of the adapted graph.
[453]1336    typedef GR Graph;
[451]1337    /// The type of the node filter map.
[453]1338    typedef NF NodeFilterMap;
[451]1339    /// The type of the edge filter map.
[453]1340    typedef EF EdgeFilterMap;
1341
[488]1342    typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> >
[453]1343      Parent;
[414]1344
[415]1345    typedef typename Parent::Node Node;
[416]1346    typedef typename Parent::Edge Edge;
[415]1347
[414]1348  protected:
[416]1349    SubGraph() { }
[414]1350  public:
1351
[415]1352    /// \brief Constructor
1353    ///
[451]1354    /// Creates a subgraph for the given graph with the given node
1355    /// and edge filter maps.
[488]1356    SubGraph(GR& graph, NF& node_filter, EF& edge_filter) {
1357      initialize(graph, node_filter, edge_filter);
[414]1358    }
1359
[452]1360    /// \brief Sets the status of the given node
[415]1361    ///
[452]1362    /// This function sets the status of the given node.
[451]1363    /// It is done by simply setting the assigned value of \c n
[452]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
[416]1368    ///
[452]1369    /// This function sets the status of the given edge.
[451]1370    /// It is done by simply setting the assigned value of \c e
[452]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
[415]1375    ///
[452]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
[416]1381    ///
[452]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
[415]1387    ///
[452]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
[415]1394    ///
[452]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
[414]1412  };
1413
[451]1414  /// \brief Returns a read-only SubGraph adaptor
[414]1415  ///
[451]1416  /// This function just returns a read-only \ref SubGraph adaptor.
1417  /// \ingroup graph_adaptors
1418  /// \relates SubGraph
[453]1419  template<typename GR, typename NF, typename EF>
1420  SubGraph<const GR, NF, EF>
[488]1421  subGraph(const GR& graph, NF& node_filter, EF& edge_filter) {
[453]1422    return SubGraph<const GR, NF, EF>
[488]1423      (graph, node_filter, edge_filter);
[416]1424  }
1425
[453]1426  template<typename GR, typename NF, typename EF>
1427  SubGraph<const GR, const NF, EF>
[488]1428  subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) {
[453]1429    return SubGraph<const GR, const NF, EF>
[488]1430      (graph, node_filter, edge_filter);
[416]1431  }
1432
[453]1433  template<typename GR, typename NF, typename EF>
1434  SubGraph<const GR, NF, const EF>
[488]1435  subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) {
[453]1436    return SubGraph<const GR, NF, const EF>
[488]1437      (graph, node_filter, edge_filter);
[416]1438  }
1439
[453]1440  template<typename GR, typename NF, typename EF>
1441  SubGraph<const GR, const NF, const EF>
[488]1442  subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) {
[453]1443    return SubGraph<const GR, const NF, const EF>
[488]1444      (graph, node_filter, edge_filter);
[416]1445  }
1446
[451]1447
[416]1448  /// \ingroup graph_adaptors
1449  ///
[451]1450  /// \brief Adaptor class for hiding nodes in a digraph or a graph.
[416]1451  ///
[451]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
[453]1458  /// depending on the \c GR template parameter.
[416]1459  ///
[451]1460  /// The adapted (di)graph can also be modified through this adaptor
[453]1461  /// by adding or removing nodes or arcs/edges, unless the \c GR template
[451]1462  /// parameter is set to be \c const.
1463  ///
[453]1464  /// \tparam GR The type of the adapted digraph or graph.
[451]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.
[453]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>".
[451]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.
[416]1475#ifdef DOXYGEN
[453]1476  template<typename GR, typename NF>
1477  class FilterNodes {
[416]1478#else
[453]1479  template<typename GR,
1480           typename NF = typename GR::template NodeMap<bool>,
[416]1481           typename Enable = void>
[453]1482  class FilterNodes :
1483    public DigraphAdaptorExtender<
[488]1484      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >,
1485                     true> > {
[416]1486#endif
1487  public:
1488
[453]1489    typedef GR Digraph;
1490    typedef NF NodeFilterMap;
1491
1492    typedef DigraphAdaptorExtender<
[488]1493      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >,
1494                     true> > Parent;
[416]1495
1496    typedef typename Parent::Node Node;
1497
1498  protected:
[488]1499    ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map;
1500
1501    FilterNodes() : const_true_map() {}
[416]1502
1503  public:
1504
1505    /// \brief Constructor
1506    ///
[451]1507    /// Creates a subgraph for the given digraph or graph with the
[416]1508    /// given node filter map.
[488]1509    FilterNodes(GR& graph, NF& node_filter)
1510      : Parent(), const_true_map()
[453]1511    {
[488]1512      Parent::initialize(graph, node_filter, const_true_map);
[416]1513    }
1514
[452]1515    /// \brief Sets the status of the given node
[416]1516    ///
[452]1517    /// This function sets the status of the given node.
[451]1518    /// It is done by simply setting the assigned value of \c n
[452]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
[416]1523    ///
[452]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
[416]1529    ///
[452]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); }
[416]1540
1541  };
1542
[453]1543  template<typename GR, typename NF>
1544  class FilterNodes<GR, NF,
1545                    typename enable_if<UndirectedTagIndicator<GR> >::type> :
1546    public GraphAdaptorExtender<
[488]1547      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >,
1548                   true> > {
[453]1549
[416]1550  public:
[453]1551    typedef GR Graph;
1552    typedef NF NodeFilterMap;
1553    typedef GraphAdaptorExtender<
[488]1554      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >,
1555                   true> > Parent;
[416]1556
1557    typedef typename Parent::Node Node;
1558  protected:
[488]1559    ConstMap<typename GR::Edge, Const<bool, true> > const_true_map;
1560
1561    FilterNodes() : const_true_map() {}
[416]1562
1563  public:
1564
[488]1565    FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1566      Parent(), const_true_map() {
1567      Parent::initialize(graph, node_filter, const_true_map);
[416]1568    }
1569
[452]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); }
[416]1574
1575  };
1576
1577
[451]1578  /// \brief Returns a read-only FilterNodes adaptor
[416]1579  ///
[451]1580  /// This function just returns a read-only \ref FilterNodes adaptor.
1581  /// \ingroup graph_adaptors
1582  /// \relates FilterNodes
[453]1583  template<typename GR, typename NF>
1584  FilterNodes<const GR, NF>
[488]1585  filterNodes(const GR& graph, NF& node_filter) {
1586    return FilterNodes<const GR, NF>(graph, node_filter);
[414]1587  }
1588
[453]1589  template<typename GR, typename NF>
1590  FilterNodes<const GR, const NF>
[488]1591  filterNodes(const GR& graph, const NF& node_filter) {
1592    return FilterNodes<const GR, const NF>(graph, node_filter);
[414]1593  }
1594
[416]1595  /// \ingroup graph_adaptors
[414]1596  ///
[451]1597  /// \brief Adaptor class for hiding arcs in a digraph.
[414]1598  ///
[451]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.
[414]1604  ///
[451]1605  /// The adapted digraph can also be modified through this adaptor
[453]1606  /// by adding or removing nodes or arcs, unless the \c GR template
[451]1607  /// parameter is set to be \c const.
1608  ///
[488]1609  /// \tparam DGR The type of the adapted digraph.
[451]1610  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
1611  /// It can also be specified to be \c const.
[453]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
[488]1615  /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
[451]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
[488]1620  template<typename DGR,
[453]1621           typename AF>
1622  class FilterArcs {
[451]1623#else
[488]1624  template<typename DGR,
1625           typename AF = typename DGR::template ArcMap<bool> >
[453]1626  class FilterArcs :
1627    public DigraphAdaptorExtender<
[488]1628      SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >,
1629                     AF, false> > {
[451]1630#endif
[414]1631  public:
[453]1632    /// The type of the adapted digraph.
[488]1633    typedef DGR Digraph;
[453]1634    /// The type of the arc filter map.
1635    typedef AF ArcFilterMap;
1636
1637    typedef DigraphAdaptorExtender<
[488]1638      SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >,
1639                     AF, false> > Parent;
[415]1640
1641    typedef typename Parent::Arc Arc;
1642
[414]1643  protected:
[488]1644    ConstMap<typename DGR::Node, Const<bool, true> > const_true_map;
1645
1646    FilterArcs() : const_true_map() {}
[414]1647
1648  public:
1649
[415]1650    /// \brief Constructor
1651    ///
[451]1652    /// Creates a subdigraph for the given digraph with the given arc
1653    /// filter map.
[488]1654    FilterArcs(DGR& digraph, ArcFilterMap& arc_filter)
1655      : Parent(), const_true_map() {
1656      Parent::initialize(digraph, const_true_map, arc_filter);
[414]1657    }
1658
[452]1659    /// \brief Sets the status of the given arc
[415]1660    ///
[452]1661    /// This function sets the status of the given arc.
[451]1662    /// It is done by simply setting the assigned value of \c a
[452]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
[415]1667    ///
[452]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
[415]1673    ///
[452]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); }
[415]1684
[414]1685  };
1686
[451]1687  /// \brief Returns a read-only FilterArcs adaptor
[414]1688  ///
[451]1689  /// This function just returns a read-only \ref FilterArcs adaptor.
1690  /// \ingroup graph_adaptors
1691  /// \relates FilterArcs
[488]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);
[414]1696  }
1697
[488]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);
[414]1702  }
1703
[416]1704  /// \ingroup graph_adaptors
1705  ///
[451]1706  /// \brief Adaptor class for hiding edges in a graph.
[416]1707  ///
[451]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.
[416]1713  ///
[451]1714  /// The adapted graph can also be modified through this adaptor
[453]1715  /// by adding or removing nodes or edges, unless the \c GR template
[451]1716  /// parameter is set to be \c const.
1717  ///
[453]1718  /// \tparam GR The type of the adapted graph.
[451]1719  /// It must conform to the \ref concepts::Graph "Graph" concept.
1720  /// It can also be specified to be \c const.
[453]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>".
[451]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
[453]1729  template<typename GR,
1730           typename EF>
1731  class FilterEdges {
[451]1732#else
[453]1733  template<typename GR,
1734           typename EF = typename GR::template EdgeMap<bool> >
1735  class FilterEdges :
1736    public GraphAdaptorExtender<
[488]1737      SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >,
1738                   EF, false> > {
[451]1739#endif
[416]1740  public:
[453]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<
[488]1747      SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >,
1748                   EF, false> > Parent;
[453]1749
[416]1750    typedef typename Parent::Edge Edge;
[453]1751
[416]1752  protected:
[488]1753    ConstMap<typename GR::Node, Const<bool, true> > const_true_map;
[416]1754
1755    FilterEdges() : const_true_map(true) {
1756      Parent::setNodeFilterMap(const_true_map);
1757    }
1758
1759  public:
1760
1761    /// \brief Constructor
1762    ///
[451]1763    /// Creates a subgraph for the given graph with the given edge
1764    /// filter map.
[488]1765    FilterEdges(GR& graph, EF& edge_filter)
1766      : Parent(), const_true_map() {
1767      Parent::initialize(graph, const_true_map, edge_filter);
[416]1768    }
1769
[452]1770    /// \brief Sets the status of the given edge
[416]1771    ///
[452]1772    /// This function sets the status of the given edge.
[451]1773    /// It is done by simply setting the assigned value of \c e
[452]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
[416]1778    ///
[452]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
[416]1784    ///
[452]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); }
[416]1795
1796  };
1797
[451]1798  /// \brief Returns a read-only FilterEdges adaptor
[416]1799  ///
[451]1800  /// This function just returns a read-only \ref FilterEdges adaptor.
1801  /// \ingroup graph_adaptors
1802  /// \relates FilterEdges
[453]1803  template<typename GR, typename EF>
1804  FilterEdges<const GR, EF>
[488]1805  filterEdges(const GR& graph, EF& edge_filter) {
1806    return FilterEdges<const GR, EF>(graph, edge_filter);
[416]1807  }
1808
[453]1809  template<typename GR, typename EF>
1810  FilterEdges<const GR, const EF>
[488]1811  filterEdges(const GR& graph, const EF& edge_filter) {
1812    return FilterEdges<const GR, const EF>(graph, edge_filter);
[416]1813  }
1814
[451]1815
[488]1816  template <typename DGR>
[416]1817  class UndirectorBase {
[414]1818  public:
[488]1819    typedef DGR Digraph;
[416]1820    typedef UndirectorBase Adaptor;
[414]1821
1822    typedef True UndirectedTag;
1823
1824    typedef typename Digraph::Arc Edge;
1825    typedef typename Digraph::Node Node;
1826
1827    class Arc : public Edge {
[416]1828      friend class UndirectorBase;
[414]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 {
[416]1841        return _forward == other._forward &&
1842          static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
[414]1843      }
1844      bool operator!=(const Arc &other) const {
[416]1845        return _forward != other._forward ||
1846          static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
[414]1847      }
1848      bool operator<(const Arc &other) const {
[416]1849        return _forward < other._forward ||
1850          (_forward == other._forward &&
1851           static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
[414]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) {
[416]1870        a._forward = false;
[414]1871      } else {
[416]1872        _digraph->next(a);
1873        a._forward = true;
[414]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 ) {
[416]1888        a._forward = false;
[414]1889      } else {
[416]1890        _digraph->firstOut(a, n);
1891        a._forward = true;
[414]1892      }
1893    }
1894    void nextOut(Arc &a) const {
1895      if (!a._forward) {
[416]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        }
[414]1902      }
1903      else {
[416]1904        _digraph->nextOut(a);
[414]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 ) {
[416]1911        a._forward = false;
[414]1912      } else {
[416]1913        _digraph->firstIn(a, n);
1914        a._forward = true;
[414]1915      }
1916    }
1917    void nextIn(Arc &a) const {
1918      if (!a._forward) {
[416]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        }
[414]1925      }
1926      else {
[416]1927        _digraph->nextIn(a);
[414]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) {
[416]1941        Node s = _digraph->source(e);
1942        _digraph->nextOut(e);
1943        if (e != INVALID) return;
1944        d = false;
1945        _digraph->firstIn(e, s);
[414]1946      } else {
[416]1947        _digraph->nextIn(e);
[414]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(); }
[416]1993    Edge addEdge(const Node& u, const Node& v) {
1994      return _digraph->addArc(u, v);
[414]1995    }
1996
1997    void erase(const Node& i) { _digraph->erase(i); }
1998    void erase(const Edge& i) { _digraph->erase(i); }
[416]1999
[414]2000    void clear() { _digraph->clear(); }
2001
2002    typedef NodeNumTagIndicator<Digraph> NodeNumTag;
[449]2003    int nodeNum() const { return _digraph->nodeNum(); }
[446]2004
2005    typedef ArcNumTagIndicator<Digraph> ArcNumTag;
[414]2006    int arcNum() const { return 2 * _digraph->arcNum(); }
[446]2007
2008    typedef ArcNumTag EdgeNumTag;
[414]2009    int edgeNum() const { return _digraph->arcNum(); }
2010
[446]2011    typedef FindArcTagIndicator<Digraph> FindArcTag;
[414]2012    Arc findArc(Node s, Node t, Arc p = INVALID) const {
2013      if (p == INVALID) {
[416]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);
[414]2018      } else if (direction(p)) {
[416]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);
[414]2023      } else {
[416]2024        Edge arc = _digraph->findArc(t, s, p);
2025        if (arc != INVALID) return direct(arc, false);
[414]2026      }
2027      return INVALID;
2028    }
2029
[446]2030    typedef FindArcTag FindEdgeTag;
[414]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;
[449]2038        } else if (_digraph->source(p) == s) {
[414]2039          Edge arc = _digraph->findArc(s, t, p);
2040          if (arc != INVALID) return arc;
2041          arc = _digraph->findArc(t, s);
[416]2042          if (arc != INVALID) return arc;
[414]2043        } else {
2044          Edge arc = _digraph->findArc(t, s, p);
[416]2045          if (arc != INVALID) return arc;
[414]2046        }
2047      } else {
2048        return _digraph->findArc(s, t, p);
2049      }
2050      return INVALID;
2051    }
2052
2053  private:
[416]2054
[488]2055    template <typename V>
[414]2056    class ArcMapBase {
2057    private:
[416]2058
[488]2059      typedef typename DGR::template ArcMap<V> MapImpl;
[416]2060
[414]2061    public:
2062
2063      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
2064
[488]2065      typedef V Value;
[414]2066      typedef Arc Key;
[449]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;
[416]2071
[488]2072      ArcMapBase(const UndirectorBase<DGR>& adaptor) :
[416]2073        _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
2074
[488]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) {
[416]2080        if (direction(a)) {
[488]2081          _forward.set(a, value);
[416]2082        } else {
[488]2083          _backward.set(a, value);
[414]2084        }
2085      }
2086
[449]2087      ConstReturnValue operator[](const Arc& a) const {
[416]2088        if (direction(a)) {
2089          return _forward[a];
2090        } else {
2091          return _backward[a];
[414]2092        }
2093      }
2094
[449]2095      ReturnValue operator[](const Arc& a) {
[416]2096        if (direction(a)) {
2097          return _forward[a];
2098        } else {
2099          return _backward[a];
2100        }
2101      }
2102
[414]2103    protected:
2104
[416]2105      MapImpl _forward, _backward;
[414]2106
2107    };
2108
2109  public:
2110
[488]2111    template <typename V>
2112    class NodeMap : public DGR::template NodeMap<V> {
[414]2113    public:
2114
[488]2115      typedef V Value;
2116      typedef typename DGR::template NodeMap<Value> Parent;
2117
2118      explicit NodeMap(const UndirectorBase<DGR>& adaptor)
[416]2119        : Parent(*adaptor._digraph) {}
[414]2120
[488]2121      NodeMap(const UndirectorBase<DGR>& adaptor, const V& value)
[416]2122        : Parent(*adaptor._digraph, value) { }
[414]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      }
[416]2134
[414]2135    };
2136
[488]2137    template <typename V>
[416]2138    class ArcMap
[488]2139      : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> >
[414]2140    {
2141    public:
[488]2142      typedef V Value;
2143      typedef SubMapExtender<Adaptor, ArcMapBase<V> > Parent;
2144
2145      explicit ArcMap(const UndirectorBase<DGR>& adaptor)
[416]2146        : Parent(adaptor) {}
2147
[488]2148      ArcMap(const UndirectorBase<DGR>& adaptor, const V& value)
[416]2149        : Parent(adaptor, value) {}
2150
[414]2151    private:
2152      ArcMap& operator=(const ArcMap& cmap) {
[416]2153        return operator=<ArcMap>(cmap);
[414]2154      }
[416]2155
[414]2156      template <typename CMap>
2157      ArcMap& operator=(const CMap& cmap) {
2158        Parent::operator=(cmap);
[416]2159        return *this;
[414]2160      }
2161    };
[416]2162
[488]2163    template <typename V>
2164    class EdgeMap : public Digraph::template ArcMap<V> {
[414]2165    public:
[416]2166
[488]2167      typedef V Value;
2168      typedef typename Digraph::template ArcMap<V> Parent;
2169
2170      explicit EdgeMap(const UndirectorBase<DGR>& adaptor)
[416]2171        : Parent(*adaptor._digraph) {}
[414]2172
[488]2173      EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value)
[416]2174        : Parent(*adaptor._digraph, value) {}
[414]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
[488]2189    typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
[416]2190    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
[414]2191
[488]2192    typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier;
[449]2193    EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2194
[414]2195  protected:
2196
[416]2197    UndirectorBase() : _digraph(0) {}
[414]2198
[488]2199    DGR* _digraph;
2200
2201    void initialize(DGR& digraph) {
[414]2202      _digraph = &digraph;
2203    }
[416]2204
[414]2205  };
2206
[416]2207  /// \ingroup graph_adaptors
[414]2208  ///
[451]2209  /// \brief Adaptor class for viewing a digraph as an undirected graph.
[414]2210  ///
[451]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.
[414]2215  ///
[451]2216  /// The adapted digraph can also be modified through this adaptor
[453]2217  /// by adding or removing nodes or edges, unless the \c GR template
[451]2218  /// parameter is set to be \c const.
2219  ///
[488]2220  /// \tparam DGR The type of the adapted digraph.
[451]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.)
[488]2230  template<typename DGR>
[453]2231#ifdef DOXYGEN
2232  class Undirector {
2233#else
2234  class Undirector :
[488]2235    public GraphAdaptorExtender<UndirectorBase<DGR> > {
[453]2236#endif
[414]2237  public:
[453]2238    /// The type of the adapted digraph.
[488]2239    typedef DGR Digraph;
2240    typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent;
[414]2241  protected:
[416]2242    Undirector() { }
[414]2243  public:
2244
2245    /// \brief Constructor
2246    ///
[451]2247    /// Creates an undirected graph from the given digraph.
[488]2248    Undirector(DGR& digraph) {
2249      initialize(digraph);
[414]2250    }
2251
[451]2252    /// \brief Arc map combined from two original arc maps
[414]2253    ///
[451]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).
[453]2258    template <typename ForwardMap, typename BackwardMap>
[414]2259    class CombinedArcMap {
2260    public:
[416]2261
[451]2262      /// The key type of the map
2263      typedef typename Parent::Arc Key;
2264      /// The value type of the map
[414]2265      typedef typename ForwardMap::Value Value;
2266
[453]2267      typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2268
[449]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
[416]2274      /// Constructor
2275      CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
[414]2276        : _forward(&forward), _backward(&backward) {}
[416]2277
[451]2278      /// Sets the value associated with the given key.
[416]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        }
[414]2285      }
2286
[451]2287      /// Returns the value associated with the given key.
[449]2288      ConstReturnValue operator[](const Key& e) const {
[416]2289        if (Parent::direction(e)) {
2290          return (*_forward)[e];
2291        } else {
2292          return (*_backward)[e];
[414]2293        }
2294      }
2295
[451]2296      /// Returns a reference to the value associated with the given key.
[449]2297      ReturnValue operator[](const Key& e) {
[416]2298        if (Parent::direction(e)) {
2299          return (*_forward)[e];
2300        } else {
2301          return (*_backward)[e];
[414]2302        }
2303      }
2304
[416]2305    protected:
2306
2307      ForwardMap* _forward;
2308      BackwardMap* _backward;
2309
2310    };
2311
[451]2312    /// \brief Returns a combined arc map
[416]2313    ///
[451]2314    /// This function just returns a combined arc map.
[416]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
[451]2344  /// \brief Returns a read-only Undirector adaptor
[416]2345  ///
[451]2346  /// This function just returns a read-only \ref Undirector adaptor.
2347  /// \ingroup graph_adaptors
2348  /// \relates Undirector
[488]2349  template<typename DGR>
2350  Undirector<const DGR> undirector(const DGR& digraph) {
2351    return Undirector<const DGR>(digraph);
[416]2352  }
2353
[451]2354
[488]2355  template <typename GR, typename DM>
[416]2356  class OrienterBase {
2357  public:
2358
[488]2359    typedef GR Graph;
2360    typedef DM DirectionMap;
2361
2362    typedef typename GR::Node Node;
2363    typedef typename GR::Edge Arc;
[416]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 {
[447]2372      bool d = true;
[416]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 {
[447]2377      bool d = true;
[416]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
[446]2405    typedef EdgeNumTagIndicator<Graph> ArcNumTag;
[416]2406    int arcNum() const { return _graph->edgeNum(); }
2407
[446]2408    typedef FindEdgeTagIndicator<Graph> FindArcTag;
[416]2409    Arc findArc(const Node& u, const Node& v,
[448]2410                const Arc& prev = INVALID) const {
[449]2411      Arc arc = _graph->findEdge(u, v, prev);
2412      while (arc != INVALID && source(arc) != u) {
[416]2413        arc = _graph->findEdge(u, v, arc);
[414]2414      }
[416]2415      return arc;
2416    }
2417
2418    Node addNode() {
2419      return Node(_graph->addNode());
2420    }
2421
2422    Arc addArc(const Node& u, const Node& v) {
[449]2423      Arc arc = _graph->addEdge(u, v);
2424      _direction->set(arc, _graph->u(arc) == u);
[416]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
[488]2442    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
[416]2443    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2444
[488]2445    typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
[416]2446    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2447
[488]2448    template <typename V>
2449    class NodeMap : public GR::template NodeMap<V> {
[416]2450    public:
2451
[488]2452      typedef typename GR::template NodeMap<V> Parent;
2453
2454      explicit NodeMap(const OrienterBase<GR, DM>& adapter)
[416]2455        : Parent(*adapter._graph) {}
2456
[488]2457      NodeMap(const OrienterBase<GR, DM>& adapter, const V& value)
[416]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      }
[414]2470
2471    };
2472
[488]2473    template <typename V>
2474    class ArcMap : public GR::template EdgeMap<V> {
[416]2475    public:
2476
[488]2477      typedef typename Graph::template EdgeMap<V> Parent;
2478
2479      explicit ArcMap(const OrienterBase<GR, DM>& adapter)
[416]2480        : Parent(*adapter._graph) { }
2481
[488]2482      ArcMap(const OrienterBase<GR, DM>& adapter, const V& value)
[416]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;
[488]2501    DM* _direction;
2502
2503    void initialize(GR& graph, DM& direction) {
2504      _graph = &graph;
[416]2505      _direction = &direction;
2506    }
2507
[414]2508  };
2509
[416]2510  /// \ingroup graph_adaptors
[414]2511  ///
[451]2512  /// \brief Adaptor class for orienting the edges of a graph to get a digraph
[416]2513  ///
[451]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.
[416]2520  ///
[451]2521  /// The adapted graph can also be modified through this adaptor
[453]2522  /// by adding or removing nodes or arcs, unless the \c GR template
[451]2523  /// parameter is set to be \c const.
[416]2524  ///
[453]2525  /// \tparam GR The type of the adapted graph.
[451]2526  /// It must conform to the \ref concepts::Graph "Graph" concept.
2527  /// It can also be specified to be \c const.
[453]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>".
[451]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
[453]2538  template<typename GR,
2539           typename DM>
2540  class Orienter {
[451]2541#else
[453]2542  template<typename GR,
2543           typename DM = typename GR::template EdgeMap<bool> >
2544  class Orienter :
2545    public DigraphAdaptorExtender<OrienterBase<GR, DM> > {
[451]2546#endif
[416]2547  public:
[451]2548
2549    /// The type of the adapted graph.
[453]2550    typedef GR Graph;
[451]2551    /// The type of the direction edge map.
[453]2552    typedef DM DirectionMap;
2553
2554    typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
[416]2555    typedef typename Parent::Arc Arc;
2556  protected:
2557    Orienter() { }
2558  public:
2559
[451]2560    /// \brief Constructor
[416]2561    ///
[451]2562    /// Constructor of the adaptor.
[488]2563    Orienter(GR& graph, DM& direction) {
2564      Parent::initialize(graph, direction);
[416]2565    }
2566
[451]2567    /// \brief Reverses the given arc
[416]2568    ///
[451]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.
[416]2572    void reverseArc(const Arc& a) {
2573      Parent::reverseArc(a);
2574    }
2575  };
2576
[451]2577  /// \brief Returns a read-only Orienter adaptor
[416]2578  ///
[451]2579  /// This function just returns a read-only \ref Orienter adaptor.
2580  /// \ingroup graph_adaptors
2581  /// \relates Orienter
[453]2582  template<typename GR, typename DM>
2583  Orienter<const GR, DM>
[488]2584  orienter(const GR& graph, DM& direction) {
2585    return Orienter<const GR, DM>(graph, direction);
[414]2586  }
2587
[453]2588  template<typename GR, typename DM>
2589  Orienter<const GR, const DM>
[488]2590  orienter(const GR& graph, const DM& direction) {
2591    return Orienter<const GR, const DM>(graph, direction);
[416]2592  }
2593
2594  namespace _adaptor_bits {
2595
[488]2596    template <typename DGR, typename CM, typename FM, typename TL>
[416]2597    class ResForwardFilter {
2598    public:
2599
[488]2600      typedef typename DGR::Arc Key;
[416]2601      typedef bool Value;
2602
2603    private:
2604
[488]2605      const CM* _capacity;
2606      const FM* _flow;
2607      TL _tolerance;
2608
[416]2609    public:
2610
[488]2611      ResForwardFilter(const CM& capacity, const FM& flow,
2612                       const TL& tolerance = TL())
[416]2613        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2614
[488]2615      bool operator[](const typename DGR::Arc& a) const {
[416]2616        return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2617      }
2618    };
2619
[488]2620    template<typename DGR,typename CM, typename FM, typename TL>
[416]2621    class ResBackwardFilter {
2622    public:
2623
[488]2624      typedef typename DGR::Arc Key;
[416]2625      typedef bool Value;
2626
2627    private:
2628
[488]2629      const CM* _capacity;
2630      const FM* _flow;
2631      TL _tolerance;
[416]2632
2633    public:
2634
[488]2635      ResBackwardFilter(const CM& capacity, const FM& flow,
2636                        const TL& tolerance = TL())
[416]2637        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2638
[488]2639      bool operator[](const typename DGR::Arc& a) const {
[416]2640        return _tolerance.positive((*_flow)[a]);
2641      }
2642    };
2643
2644  }
2645
2646  /// \ingroup graph_adaptors
2647  ///
[451]2648  /// \brief Adaptor class for composing the residual digraph for directed
[416]2649  /// flow and circulation problems.
2650  ///
[464]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.
[451]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.
[416]2665  ///
[488]2666  /// \tparam DGR The type of the adapted digraph.
[451]2667  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2668  /// It is implicitly \c const.
[453]2669  /// \tparam CM The type of the capacity map.
2670  /// It must be an arc map of some numerical type, which defines
[451]2671  /// the capacities in the flow problem. It is implicitly \c const.
[453]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.
[451]2678  /// The default tolerance type depends on the value type of the
2679  /// capacity map.
[416]2680  ///
[451]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
[488]2688  template<typename DGR, typename CM, typename FM, typename TL>
[464]2689  class ResidualDigraph
[451]2690#else
[488]2691  template<typename DGR,
2692           typename CM = typename DGR::template ArcMap<int>,
[453]2693           typename FM = CM,
2694           typename TL = Tolerance<typename CM::Value> >
[488]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> > >
[451]2702#endif
[416]2703  {
[414]2704  public:
2705
[451]2706    /// The type of the underlying digraph.
[488]2707    typedef DGR Digraph;
[451]2708    /// The type of the capacity map.
[453]2709    typedef CM CapacityMap;
[451]2710    /// The type of the flow map.
[453]2711    typedef FM FlowMap;
2712    /// The tolerance type.
2713    typedef TL Tolerance;
[414]2714
2715    typedef typename CapacityMap::Value Value;
[464]2716    typedef ResidualDigraph Adaptor;
[414]2717
2718  protected:
2719
[416]2720    typedef Undirector<const Digraph> Undirected;
2721
[488]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;
[416]2729
2730    typedef typename Undirected::
[453]2731      template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
[414]2732
[488]2733    typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent;
[414]2734
2735    const CapacityMap* _capacity;
2736    FlowMap* _flow;
2737
[416]2738    Undirected _graph;
[488]2739    NodeFilter _node_filter;
[414]2740    ForwardFilter _forward_filter;
2741    BackwardFilter _backward_filter;
2742    ArcFilter _arc_filter;
2743
2744  public:
2745
[451]2746    /// \brief Constructor
[414]2747    ///
[451]2748    /// Constructor of the residual digraph adaptor. The parameters are the
2749    /// digraph, the capacity map, the flow map, and a tolerance object.
[488]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(),
[416]2754        _forward_filter(capacity, flow, tolerance),
[414]2755        _backward_filter(capacity, flow, tolerance),
2756        _arc_filter(_forward_filter, _backward_filter)
2757    {
[488]2758      Parent::initialize(_graph, _node_filter, _arc_filter);
[414]2759    }
2760
2761    typedef typename Parent::Arc Arc;
2762
[451]2763    /// \brief Returns the residual capacity of the given arc.
[414]2764    ///
[451]2765    /// Returns the residual capacity of the given arc.
[416]2766    Value residualCapacity(const Arc& a) const {
2767      if (Undirected::direction(a)) {
2768        return (*_capacity)[a] - (*_flow)[a];
[414]2769      } else {
[416]2770        return (*_flow)[a];
[414]2771      }
[416]2772    }
2773
[452]2774    /// \brief Augments on the given arc in the residual digraph.
[414]2775    ///
[452]2776    /// Augments on the given arc in the residual digraph. It increases
[451]2777    /// or decreases the flow value on the original arc according to the
2778    /// direction of the residual arc.
[416]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);
[414]2784      }
2785    }
2786
[451]2787    /// \brief Returns \c true if the given residual arc is a forward arc.
[414]2788    ///
[451]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.
[416]2791    static bool forward(const Arc& a) {
2792      return Undirected::direction(a);
[414]2793    }
2794
[451]2795    /// \brief Returns \c true if the given residual arc is a backward arc.
[414]2796    ///
[451]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.
[416]2799    static bool backward(const Arc& a) {
2800      return !Undirected::direction(a);
[414]2801    }
2802
[451]2803    /// \brief Returns the forward oriented residual arc.
[414]2804    ///
[451]2805    /// Returns the forward oriented residual arc related to the given
2806    /// arc of the underlying digraph.
[416]2807    static Arc forward(const typename Digraph::Arc& a) {
2808      return Undirected::direct(a, true);
[414]2809    }
2810
[451]2811    /// \brief Returns the backward oriented residual arc.
[414]2812    ///
[451]2813    /// Returns the backward oriented residual arc related to the given
2814    /// arc of the underlying digraph.
[416]2815    static Arc backward(const typename Digraph::Arc& a) {
2816      return Undirected::direct(a, false);
[414]2817    }
2818
2819    /// \brief Residual capacity map.
2820    ///
[451]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.
[416]2824    class ResidualCapacity {
[414]2825    protected:
2826      const Adaptor* _adaptor;
2827    public:
[451]2828      /// The key type of the map
[414]2829      typedef Arc Key;
[451]2830      /// The value type of the map
[453]2831      typedef typename CapacityMap::Value Value;
[414]2832
[416]2833      /// Constructor
[488]2834      ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor)
2835        : _adaptor(&adaptor) {}
[416]2836
[451]2837      /// Returns the value associated with the given residual arc
[416]2838      Value operator[](const Arc& a) const {
2839        return _adaptor->residualCapacity(a);
[414]2840      }
[416]2841
[414]2842    };
2843
[450]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
[414]2851  };
2852
[450]2853  /// \brief Returns a (read-only) Residual adaptor
2854  ///
[488]2855  /// This function just returns a (read-only) \ref ResidualDigraph adaptor.
[450]2856  /// \ingroup graph_adaptors
[488]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);
[450]2862  }
2863
2864
[488]2865  template <typename DGR>
[416]2866  class SplitNodesBase {
[414]2867  public:
2868
[488]2869    typedef DGR Digraph;
2870    typedef DigraphAdaptorBase<const DGR> Parent;
[416]2871    typedef SplitNodesBase Adaptor;
[414]2872
[488]2873    typedef typename DGR::Node DigraphNode;
2874    typedef typename DGR::Arc DigraphArc;
[414]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:
[416]2885
[414]2886    class Node : public DigraphNode {
[416]2887      friend class SplitNodesBase;
[414]2888      template <typename T> friend class NodeMapBase;
2889    private:
2890
2891      bool _in;
2892      Node(DigraphNode node, bool in)
[416]2893        : DigraphNode(node), _in(in) {}
2894
[414]2895    public:
2896
2897      Node() {}
2898      Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2899
2900      bool operator==(const Node& node) const {
[416]2901        return DigraphNode::operator==(node) && _in == node._in;
[414]2902      }
[416]2903
[414]2904      bool operator!=(const Node& node) const {
[416]2905        return !(*this == node);
[414]2906      }
[416]2907
[414]2908      bool operator<(const Node& node) const {
[416]2909        return DigraphNode::operator<(node) ||
2910          (DigraphNode::operator==(node) && _in < node._in);
[414]2911      }
2912    };
2913
2914    class Arc {
[416]2915      friend class SplitNodesBase;
[414]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) {}
[416]2922
[414]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      }
[416]2941
[414]2942      bool operator!=(const Arc& arc) const {
[416]2943        return !(*this == arc);
[414]2944      }
[416]2945
[414]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) {
[416]2972        n._in = false;
[414]2973      } else {
[416]2974        n._in = true;
2975        _digraph->next(n);
[414]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();
[416]2984        _digraph->first(e._item.first());
[414]2985      }
2986    }
2987
2988    void next(Arc& e) const {
2989      if (e._item.secondState()) {
[416]2990        _digraph->next(e._item.second());
[414]2991        if (e._item.second() == INVALID) {
2992          e._item.setFirst();
2993          _digraph->first(e._item.first());
2994        }
2995      } else {
[416]2996        _digraph->next(e._item.first());
2997      }
[414]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();
[416]3005        _digraph->firstOut(e._item.first(), n);
[414]3006      }
3007    }
3008
3009    void nextOut(Arc& e) const {
3010      if (!e._item.firstState()) {
[416]3011        e._item.setFirst(INVALID);
[414]3012      } else {
[416]3013        _digraph->nextOut(e._item.first());
3014      }
[414]3015    }
3016
3017    void firstIn(Arc& e, const Node& n) const {
3018      if (!n._in) {
[416]3019        e._item.setSecond(n);
[414]3020      } else {
3021        e._item.setFirst();
[416]3022        _digraph->firstIn(e._item.first(), n);
[414]3023      }
3024    }
3025
3026    void nextIn(Arc& e) const {
3027      if (!e._item.firstState()) {
[416]3028        e._item.setFirst(INVALID);
[414]3029      } else {
[416]3030        _digraph->nextIn(e._item.first());
[414]3031      }
3032    }
3033
3034    Node source(const Arc& e) const {
3035      if (e._item.firstState()) {
[416]3036        return Node(_digraph->source(e._item.first()), false);
[414]3037      } else {
[416]3038        return Node(e._item.second(), true);
[414]3039      }
3040    }
3041
3042    Node target(const Arc& e) const {
3043      if (e._item.firstState()) {
[416]3044        return Node(_digraph->target(e._item.first()), true);
[414]3045      } else {
[416]3046        return Node(e._item.second(), false);
[414]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 {
[416]3075      return std::max(_digraph->maxNodeId() << 1,
[414]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
[446]3116    typedef True ArcNumTag;
[414]3117    int arcNum() const {
3118      return countArcs(*_digraph) + countNodes(*_digraph);
3119    }
3120
[446]3121    typedef True FindArcTag;
[416]3122    Arc findArc(const Node& u, const Node& v,
3123                const Arc& prev = INVALID) const {
[449]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);
[414]3128        }
[449]3129      }
3130      else if (outNode(u) && inNode(v)) {
3131        return Arc(::lemon::findArc(*_digraph, u, v, prev));
[414]3132      }
3133      return INVALID;
3134    }
3135
3136  private:
[416]3137
[488]3138    template <typename V>
[416]3139    class NodeMapBase
[488]3140      : public MapTraits<typename Parent::template NodeMap<V> > {
3141      typedef typename Parent::template NodeMap<V> NodeImpl;
[414]3142    public:
3143      typedef Node Key;
[488]3144      typedef V Value;
[449]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;
[416]3150
[488]3151      NodeMapBase(const SplitNodesBase<DGR>& adaptor)
[416]3152        : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
[488]3153      NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
[416]3154        : _in_map(*adaptor._digraph, value),
3155          _out_map(*adaptor._digraph, value) {}
[414]3156
[488]3157      void set(const Node& key, const V& val) {
3158        if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); }
[416]3159        else {_out_map.set(key, val); }
[414]3160      }
[416]3161
[449]3162      ReturnValue operator[](const Node& key) {
[488]3163        if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; }
[416]3164        else { return _out_map[key]; }
[414]3165      }
3166
[449]3167      ConstReturnValue operator[](const Node& key) const {
[416]3168        if (Adaptor::inNode(key)) { return _in_map[key]; }
3169        else { return _out_map[key]; }
[414]3170      }
3171
3172    private:
3173      NodeImpl _in_map, _out_map;
3174    };
3175
[488]3176    template <typename V>
[416]3177    class ArcMapBase
[488]3178      : public MapTraits<typename Parent::template ArcMap<V> > {
3179      typedef typename Parent::template ArcMap<V> ArcImpl;
3180      typedef typename Parent::template NodeMap<V> NodeImpl;
[414]3181    public:
3182      typedef Arc Key;
[488]3183      typedef V Value;
[449]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;
[414]3189
[488]3190      ArcMapBase(const SplitNodesBase<DGR>& adaptor)
[416]3191        : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
[488]3192      ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
[416]3193        : _arc_map(*adaptor._digraph, value),
3194          _node_map(*adaptor._digraph, value) {}
[414]3195
[488]3196      void set(const Arc& key, const V& val) {
3197        if (SplitNodesBase<DGR>::origArc(key)) {
[495]3198          _arc_map.set(static_cast<const DigraphArc&>(key), val);
[414]3199        } else {
[495]3200          _node_map.set(static_cast<const DigraphNode&>(key), val);
[414]3201        }
3202      }
[416]3203
[449]3204      ReturnValue operator[](const Arc& key) {
[488]3205        if (SplitNodesBase<DGR>::origArc(key)) {
[495]3206          return _arc_map[static_cast<const DigraphArc&>(key)];
[414]3207        } else {
[495]3208          return _node_map[static_cast<const DigraphNode&>(key)];
[414]3209        }
3210      }
3211
[449]3212      ConstReturnValue operator[](const Arc& key) const {
[488]3213        if (SplitNodesBase<DGR>::origArc(key)) {
[495]3214          return _arc_map[static_cast<const DigraphArc&>(key)];
[414]3215        } else {
[495]3216          return _node_map[static_cast<const DigraphNode&>(key)];
[414]3217        }
3218      }
3219
3220    private:
3221      ArcImpl _arc_map;
3222      NodeImpl _node_map;
3223    };
3224
3225  public:
3226
[488]3227    template <typename V>
[416]3228    class NodeMap
[488]3229      : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> >
[414]3230    {
3231    public:
[488]3232      typedef V Value;
3233      typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<Value> > Parent;
3234
3235      NodeMap(const SplitNodesBase<DGR>& adaptor)
[416]3236        : Parent(adaptor) {}
3237
[488]3238      NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value)
[416]3239        : Parent(adaptor, value) {}
3240
[414]3241    private:
3242      NodeMap& operator=(const NodeMap& cmap) {
[416]3243        return operator=<NodeMap>(cmap);
[414]3244      }
[416]3245
[414]3246      template <typename CMap>
3247      NodeMap& operator=(const CMap& cmap) {
3248        Parent::operator=(cmap);
[416]3249        return *this;
[414]3250      }
3251    };
3252
[488]3253    template <typename V>
[416]3254    class ArcMap
[488]3255      : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> >
[414]3256    {
3257    public:
[488]3258      typedef V Value;
3259      typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<Value> > Parent;
3260
3261      ArcMap(const SplitNodesBase<DGR>& adaptor)
[416]3262        : Parent(adaptor) {}
3263
[488]3264      ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value)
[416]3265        : Parent(adaptor, value) {}
3266
[414]3267    private:
3268      ArcMap& operator=(const ArcMap& cmap) {
[416]3269        return operator=<ArcMap>(cmap);
[414]3270      }
[416]3271
[414]3272      template <typename CMap>
3273      ArcMap& operator=(const CMap& cmap) {
3274        Parent::operator=(cmap);
[416]3275        return *this;
[414]3276      }
3277    };
3278
3279  protected:
3280
[416]3281    SplitNodesBase() : _digraph(0) {}
[414]3282
[488]3283    DGR* _digraph;
3284
3285    void initialize(Digraph& digraph) {
[414]3286      _digraph = &digraph;
3287    }
[416]3288
[414]3289  };
3290
3291  /// \ingroup graph_adaptors
3292  ///
[451]3293  /// \brief Adaptor class for splitting the nodes of a digraph.
[416]3294  ///
[451]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.
[414]3304  ///
[451]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.
[414]3311  ///
[488]3312  /// \tparam DGR The type of the adapted digraph.
[451]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.
[488]3318  template <typename DGR>
[453]3319#ifdef DOXYGEN
3320  class SplitNodes {
3321#else
[416]3322  class SplitNodes
[488]3323    : public DigraphAdaptorExtender<SplitNodesBase<const DGR> > {
[453]3324#endif
[414]3325  public:
[488]3326    typedef DGR Digraph;
3327    typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent;
3328
3329    typedef typename DGR::Node DigraphNode;
3330    typedef typename DGR::Arc DigraphArc;
[415]3331
[414]3332    typedef typename Parent::Node Node;
3333    typedef typename Parent::Arc Arc;
3334
[451]3335    /// \brief Constructor
[414]3336    ///
3337    /// Constructor of the adaptor.
[488]3338    SplitNodes(const DGR& g) {
3339      Parent::initialize(g);
[414]3340    }
3341
[451]3342    /// \brief Returns \c true if the given node is an in-node.
[415]3343    ///
[451]3344    /// Returns \c true if the given node is an in-node.
[415]3345    static bool inNode(const Node& n) {
3346      return Parent::inNode(n);
3347    }
3348
[451]3349    /// \brief Returns \c true if the given node is an out-node.
[415]3350    ///
[451]3351    /// Returns \c true if the given node is an out-node.
[415]3352    static bool outNode(const Node& n) {
3353      return Parent::outNode(n);
3354    }
3355
[451]3356    /// \brief Returns \c true if the given arc is an original arc.
[415]3357    ///
[451]3358    /// Returns \c true if the given arc is one of the arcs in the
3359    /// original digraph.
[415]3360    static bool origArc(const Arc& a) {
3361      return Parent::origArc(a);
3362    }
3363
[451]3364    /// \brief Returns \c true if the given arc is a bind arc.
[415]3365    ///
[451]3366    /// Returns \c true if the given arc is a bind arc, i.e. it connects
3367    /// an in-node and an out-node.
[415]3368    static bool bindArc(const Arc& a) {
3369      return Parent::bindArc(a);
3370    }
3371
[451]3372    /// \brief Returns the in-node created from the given original node.
[415]3373    ///
[451]3374    /// Returns the in-node created from the given original node.
[415]3375    static Node inNode(const DigraphNode& n) {
3376      return Parent::inNode(n);
3377    }
3378
[451]3379    /// \brief Returns the out-node created from the given original node.
[415]3380    ///
[451]3381    /// Returns the out-node created from the given original node.
[415]3382    static Node outNode(const DigraphNode& n) {
3383      return Parent::outNode(n);
3384    }
3385
[451]3386    /// \brief Returns the bind arc that corresponds to the given
3387    /// original node.
[416]3388    ///
[451]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.
[415]3392    static Arc arc(const DigraphNode& n) {
3393      return Parent::arc(n);
3394    }
3395
[451]3396    /// \brief Returns the arc that corresponds to the given original arc.
[416]3397    ///
[451]3398    /// Returns the arc in the adaptor that corresponds to the given
3399    /// original arc.
[415]3400    static Arc arc(const DigraphArc& a) {
3401      return Parent::arc(a);
3402    }
3403
[451]3404    /// \brief Node map combined from two original node maps
[414]3405    ///
[451]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).
[414]3410    template <typename InNodeMap, typename OutNodeMap>
3411    class CombinedNodeMap {
3412    public:
3413
[451]3414      /// The key type of the map
[414]3415      typedef Node Key;
[451]3416      /// The value type of the map
[414]3417      typedef typename InNodeMap::Value Value;
3418
[449]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
[451]3425      /// Constructor
[416]3426      CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3427        : _in_map(in_map), _out_map(out_map) {}
[414]3428
[451]3429      /// Returns the value associated with the given key.
3430      Value operator[](const Key& key) const {
[488]3431        if (SplitNodesBase<const DGR>::inNode(key)) {
[451]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.
[414]3439      Value& operator[](const Key& key) {
[488]3440        if (SplitNodesBase<const DGR>::inNode(key)) {
[416]3441          return _in_map[key];
3442        } else {
3443          return _out_map[key];
3444        }
[414]3445      }
3446
[451]3447      /// Sets the value associated with the given key.
[414]3448      void set(const Key& key, const Value& value) {
[488]3449        if (SplitNodesBase<const DGR>::inNode(key)) {
[416]3450          _in_map.set(key, value);
3451        } else {
3452          _out_map.set(key, value);
3453        }
[414]3454      }
[416]3455
[414]3456    private:
[416]3457
[414]3458      InNodeMap& _in_map;
3459      OutNodeMap& _out_map;
[416]3460
[414]3461    };
3462
3463
[451]3464    /// \brief Returns a combined node map
[416]3465    ///
[451]3466    /// This function just returns a combined node map.
[414]3467    template <typename InNodeMap, typename OutNodeMap>
[416]3468    static CombinedNodeMap<InNodeMap, OutNodeMap>
[414]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>
[416]3474    static CombinedNodeMap<const InNodeMap, OutNodeMap>
[414]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>
[416]3480    static CombinedNodeMap<InNodeMap, const OutNodeMap>
[414]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>
[416]3486    static CombinedNodeMap<const InNodeMap, const OutNodeMap>
[414]3487    combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
[416]3488      return CombinedNodeMap<const InNodeMap,
[414]3489        const OutNodeMap>(in_map, out_map);
3490    }
3491
[451]3492    /// \brief Arc map combined from an arc map and a node map of the
3493    /// original digraph.
[414]3494    ///
[451]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
[453]3498    /// (\c ArcMap).
3499    template <typename ArcMap, typename NodeMap>
[414]3500    class CombinedArcMap {
3501    public:
[416]3502
[451]3503      /// The key type of the map
[414]3504      typedef Arc Key;
[451]3505      /// The value type of the map
[453]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;
[449]3513
[451]3514      /// Constructor
[453]3515      CombinedArcMap(ArcMap& arc_map, NodeMap& node_map)
[416]3516        : _arc_map(arc_map), _node_map(node_map) {}
[414]3517
[451]3518      /// Returns the value associated with the given key.
3519      Value operator[](const Key& arc) const {
[488]3520        if (SplitNodesBase<const DGR>::origArc(arc)) {
[451]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) {
[488]3529        if (SplitNodesBase<const DGR>::origArc(arc)) {
[451]3530          return _arc_map[arc];
3531        } else {
3532          return _node_map[arc];
3533        }
3534      }
3535
3536      /// Sets the value associated with the given key.
[414]3537      void set(const Arc& arc, const Value& val) {
[488]3538        if (SplitNodesBase<const DGR>::origArc(arc)) {
[416]3539          _arc_map.set(arc, val);
3540        } else {
3541          _node_map.set(arc, val);
3542        }
[414]3543      }
3544
3545    private:
[453]3546      ArcMap& _arc_map;
3547      NodeMap& _node_map;
[414]3548    };
[416]3549
[451]3550    /// \brief Returns a combined arc map
[416]3551    ///
[451]3552    /// This function just returns a combined arc map.
[453]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);
[414]3557    }
3558
[453]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);
[414]3563    }
3564
[453]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);
[414]3569    }
3570
[453]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);
[414]3575    }
3576
3577  };
3578
[451]3579  /// \brief Returns a (read-only) SplitNodes adaptor
[414]3580  ///
[451]3581  /// This function just returns a (read-only) \ref SplitNodes adaptor.
3582  /// \ingroup graph_adaptors
3583  /// \relates SplitNodes
[488]3584  template<typename DGR>
3585  SplitNodes<DGR>
3586  splitNodes(const DGR& digraph) {
3587    return SplitNodes<DGR>(digraph);
[414]3588  }
3589
[488]3590#undef LEMON_SCOPE_FIX
3591
[414]3592} //namespace lemon
3593
[416]3594#endif //LEMON_ADAPTORS_H
Note: See TracBrowser for help on using the repository browser.