lemon/ugraph_adaptor.h
author deba
Wed, 07 Mar 2007 12:00:59 +0000
changeset 2400 b199ded24c19
parent 2386 81b47fc5c444
child 2422 77ed2b97abbd
permissions -rw-r--r--
Steiner 2-approximation demo
deba@1979
     1
/* -*- C++ -*-
deba@1979
     2
 *
deba@1979
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@1979
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
deba@1979
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1979
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1979
     8
 *
deba@1979
     9
 * Permission to use, modify and distribute this software is granted
deba@1979
    10
 * provided that this copyright notice appears in all copies. For
deba@1979
    11
 * precise terms see the accompanying LICENSE file.
deba@1979
    12
 *
deba@1979
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1979
    14
 * express or implied, and with no claim as to its suitability for any
deba@1979
    15
 * purpose.
deba@1979
    16
 *
deba@1979
    17
 */
deba@1979
    18
deba@1979
    19
#ifndef LEMON_UGRAPH_ADAPTOR_H
deba@1979
    20
#define LEMON_UGRAPH_ADAPTOR_H
deba@1979
    21
deba@1979
    22
///\ingroup graph_adaptors
deba@1979
    23
///\file
deba@1979
    24
///\brief Several graph adaptors.
deba@1979
    25
///
deba@1979
    26
///This file contains several useful ugraph adaptor functions.
deba@1979
    27
///
deba@1979
    28
///\author Balazs Dezso
deba@1979
    29
deba@1993
    30
#include <lemon/bits/invalid.h>
deba@1979
    31
#include <lemon/maps.h>
deba@1979
    32
deba@1979
    33
#include <lemon/bits/graph_adaptor_extender.h>
deba@1979
    34
deba@1993
    35
#include <lemon/bits/traits.h>
deba@1979
    36
deba@1979
    37
#include <iostream>
deba@1979
    38
deba@1979
    39
namespace lemon {
deba@1979
    40
deba@1979
    41
  /// \brief Base type for the Graph Adaptors
deba@1979
    42
  ///
deba@1979
    43
  /// This is the base type for most of LEMON graph adaptors. 
deba@1979
    44
  /// This class implements a trivial graph adaptor i.e. it only wraps the 
deba@1979
    45
  /// functions and types of the graph. The purpose of this class is to 
deba@1979
    46
  /// make easier implementing graph adaptors. E.g. if an adaptor is 
deba@1979
    47
  /// considered which differs from the wrapped graph only in some of its 
deba@1979
    48
  /// functions or types, then it can be derived from GraphAdaptor, and only 
deba@1979
    49
  /// the differences should be implemented.
deba@1979
    50
  ///
deba@1979
    51
  /// \author Balazs Dezso 
deba@1979
    52
  template<typename _UGraph>
deba@1979
    53
  class UGraphAdaptorBase {
deba@1979
    54
  public:
deba@1979
    55
    typedef _UGraph Graph;
deba@1979
    56
    typedef Graph ParentGraph;
deba@1979
    57
deba@1979
    58
  protected:
deba@1979
    59
    Graph* graph;
deba@1979
    60
deba@1979
    61
    UGraphAdaptorBase() : graph(0) {}
deba@1979
    62
deba@1979
    63
    void setGraph(Graph& _graph) { graph=&_graph; }
deba@1979
    64
deba@1979
    65
  public:
deba@1979
    66
    UGraphAdaptorBase(Graph& _graph) : graph(&_graph) {}
deba@1979
    67
 
deba@1979
    68
    typedef typename Graph::Node Node;
deba@1979
    69
    typedef typename Graph::Edge Edge;
deba@1979
    70
    typedef typename Graph::UEdge UEdge;
deba@1979
    71
   
deba@1979
    72
    void first(Node& i) const { graph->first(i); }
deba@1979
    73
    void first(Edge& i) const { graph->first(i); }
deba@1979
    74
    void first(UEdge& i) const { graph->first(i); }
deba@1979
    75
    void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
deba@1979
    76
    void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
deba@1979
    77
    void firstInc(UEdge &i, bool &d, const Node &n) const {
deba@1979
    78
      graph->firstInc(i, d, n);
deba@1979
    79
    }
deba@1979
    80
deba@1979
    81
    void next(Node& i) const { graph->next(i); }
deba@1979
    82
    void next(Edge& i) const { graph->next(i); }
deba@1979
    83
    void next(UEdge& i) const { graph->next(i); }
deba@1979
    84
    void nextIn(Edge& i) const { graph->nextIn(i); }
deba@1979
    85
    void nextOut(Edge& i) const { graph->nextOut(i); }
deba@1979
    86
    void nextInc(UEdge &i, bool &d) const { graph->nextInc(i, d); }
deba@1979
    87
deba@1979
    88
    Node source(const UEdge& e) const { return graph->source(e); }
deba@1979
    89
    Node target(const UEdge& e) const { return graph->target(e); }
deba@1979
    90
deba@1979
    91
    Node source(const Edge& e) const { return graph->source(e); }
deba@1979
    92
    Node target(const Edge& e) const { return graph->target(e); }
deba@1979
    93
deba@1979
    94
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
deba@1979
    95
    int nodeNum() const { return graph->nodeNum(); }
deba@1979
    96
    
deba@1979
    97
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
deba@1979
    98
    int edgeNum() const { return graph->edgeNum(); }
deba@2031
    99
    int uEdgeNum() const { return graph->uEdgeNum(); }
deba@1979
   100
deba@1979
   101
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@2386
   102
    Edge findEdge(const Node& u, const Node& v, 
deba@1979
   103
		  const Edge& prev = INVALID) {
deba@2386
   104
      return graph->findEdge(u, v, prev);
deba@1979
   105
    }
deba@2386
   106
    UEdge findUEdge(const Node& u, const Node& v, 
deba@1979
   107
                    const UEdge& prev = INVALID) {
deba@2386
   108
      return graph->findUEdge(u, v, prev);
deba@1979
   109
    }
deba@1979
   110
  
deba@1979
   111
    Node addNode() const { return graph->addNode(); }
deba@2386
   112
    UEdge addEdge(const Node& u, const Node& v) const { 
deba@2386
   113
      return graph->addEdge(u, v); 
deba@1979
   114
    }
deba@1979
   115
deba@1979
   116
    void erase(const Node& i) const { graph->erase(i); }
deba@2031
   117
    void erase(const UEdge& i) const { graph->erase(i); }
deba@1979
   118
  
deba@1979
   119
    void clear() const { graph->clear(); }
deba@1979
   120
    
deba@2031
   121
    bool direction(const Edge& e) const { return graph->direction(e); }
deba@2031
   122
    Edge direct(const UEdge& e, bool d) const { return graph->direct(e, d); }
deba@2031
   123
deba@1979
   124
    int id(const Node& v) const { return graph->id(v); }
deba@2031
   125
    int id(const Edge& e) const { return graph->id(e); }
deba@1979
   126
    int id(const UEdge& e) const { return graph->id(e); }
deba@1979
   127
deba@2386
   128
    Node fromNodeId(int ix) const {
deba@2386
   129
      return graph->fromNodeId(ix);
deba@2031
   130
    }
deba@2031
   131
deba@2386
   132
    Edge fromEdgeId(int ix) const {
deba@2386
   133
      return graph->fromEdgeId(ix);
deba@2031
   134
    }
deba@2031
   135
deba@2386
   136
    UEdge fromUEdgeId(int ix) const {
deba@2386
   137
      return graph->fromUEdgeId(ix);
deba@2031
   138
    }
deba@1991
   139
deba@1991
   140
    int maxNodeId() const {
deba@1991
   141
      return graph->maxNodeId();
deba@1979
   142
    }
deba@1979
   143
deba@1991
   144
    int maxEdgeId() const {
deba@1991
   145
      return graph->maxEdgeId();
deba@1979
   146
    }
deba@1979
   147
deba@1991
   148
    int maxUEdgeId() const {
deba@1991
   149
      return graph->maxEdgeId();
deba@1979
   150
    }
deba@1979
   151
deba@1991
   152
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
deba@1991
   153
deba@2381
   154
    NodeNotifier& notifier(Node) const {
deba@2381
   155
      return graph->notifier(Node());
deba@1991
   156
    } 
deba@1991
   157
deba@1991
   158
    typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
deba@1991
   159
deba@2381
   160
    EdgeNotifier& notifier(Edge) const {
deba@2381
   161
      return graph->notifier(Edge());
deba@1991
   162
    } 
deba@1991
   163
deba@1991
   164
    typedef typename ItemSetTraits<Graph, UEdge>::ItemNotifier UEdgeNotifier;
deba@1991
   165
deba@2381
   166
    UEdgeNotifier& notifier(UEdge) const {
deba@2381
   167
      return graph->notifier(UEdge());
deba@1991
   168
    } 
deba@1979
   169
deba@1979
   170
    template <typename _Value>
deba@1979
   171
    class NodeMap : public Graph::template NodeMap<_Value> {
deba@1979
   172
    public:
deba@1979
   173
      typedef typename Graph::template NodeMap<_Value> Parent;
deba@1979
   174
      explicit NodeMap(const UGraphAdaptorBase<Graph>& ga) 
deba@1979
   175
	: Parent(*ga.graph) {}
deba@1979
   176
      NodeMap(const UGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@1979
   177
	: Parent(*ga.graph, value) {}
deba@1979
   178
deba@1979
   179
      NodeMap& operator=(const NodeMap& cmap) {
deba@1979
   180
	return operator=<NodeMap>(cmap);
deba@1979
   181
      }
deba@1979
   182
deba@1979
   183
      template <typename CMap>
deba@1979
   184
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   185
        Parent::operator=(cmap);
deba@2031
   186
        return *this;
deba@1979
   187
      }
deba@2031
   188
deba@1979
   189
    };
deba@1979
   190
deba@1979
   191
    template <typename _Value>
deba@1979
   192
    class EdgeMap : public Graph::template EdgeMap<_Value> {
deba@1979
   193
    public:
deba@1979
   194
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@1979
   195
      explicit EdgeMap(const UGraphAdaptorBase<Graph>& ga) 
deba@1979
   196
	: Parent(*ga.graph) {}
deba@1979
   197
      EdgeMap(const UGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@1979
   198
	: Parent(*ga.graph, value) {}
deba@1979
   199
deba@1979
   200
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@1979
   201
	return operator=<EdgeMap>(cmap);
deba@1979
   202
      }
deba@1979
   203
deba@1979
   204
      template <typename CMap>
deba@1979
   205
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   206
        Parent::operator=(cmap);
deba@1979
   207
	return *this;
deba@1979
   208
      }
deba@1979
   209
    };
deba@1979
   210
deba@1979
   211
    template <typename _Value>
deba@1979
   212
    class UEdgeMap : public Graph::template UEdgeMap<_Value> {
deba@1979
   213
    public:
deba@1979
   214
      typedef typename Graph::template UEdgeMap<_Value> Parent;
deba@1979
   215
      explicit UEdgeMap(const UGraphAdaptorBase<Graph>& ga) 
deba@1979
   216
	: Parent(*ga.graph) {}
deba@1979
   217
      UEdgeMap(const UGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@1979
   218
	: Parent(*ga.graph, value) {}
deba@1979
   219
deba@1979
   220
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@1979
   221
	return operator=<UEdgeMap>(cmap);
deba@1979
   222
      }
deba@1979
   223
deba@1979
   224
      template <typename CMap>
deba@1979
   225
      UEdgeMap& operator=(const CMap& cmap) {
deba@2031
   226
        Parent::operator=(cmap);
deba@2031
   227
        return *this;
deba@1979
   228
      }
deba@1979
   229
    };
deba@1979
   230
deba@1979
   231
  };
deba@1979
   232
deba@1979
   233
  /// \ingroup graph_adaptors
deba@2084
   234
  ///
deba@2084
   235
  /// \brief Trivial undirected graph adaptor
deba@2084
   236
  ///
deba@2084
   237
  /// This class is an adaptor which does not change the adapted undirected
deba@2084
   238
  /// graph. It can be used only to test the undirected graph adaptors.
deba@1979
   239
  template <typename _UGraph>
deba@1979
   240
  class UGraphAdaptor 
deba@1979
   241
    : public UGraphAdaptorExtender< UGraphAdaptorBase<_UGraph> > { 
deba@1979
   242
  public:
deba@1979
   243
    typedef _UGraph Graph;
deba@1979
   244
    typedef UGraphAdaptorExtender<UGraphAdaptorBase<_UGraph> > Parent;
deba@1979
   245
  protected:
deba@1979
   246
    UGraphAdaptor() : Parent() {}
deba@1979
   247
deba@1979
   248
  public:
deba@1979
   249
    explicit UGraphAdaptor(Graph& _graph) { setGraph(_graph); }
deba@1979
   250
  };
deba@1979
   251
deba@1979
   252
  template <typename _UGraph, typename NodeFilterMap, 
deba@1979
   253
	    typename UEdgeFilterMap, bool checked = true>
deba@1979
   254
  class SubUGraphAdaptorBase : public UGraphAdaptorBase<_UGraph> {
deba@1979
   255
  public:
deba@1979
   256
    typedef _UGraph Graph;
deba@2031
   257
    typedef SubUGraphAdaptorBase Adaptor;
deba@1979
   258
    typedef UGraphAdaptorBase<_UGraph> Parent;
deba@1979
   259
  protected:
deba@1979
   260
deba@1979
   261
    NodeFilterMap* node_filter_map;
deba@1979
   262
    UEdgeFilterMap* uedge_filter_map;
deba@1979
   263
deba@1979
   264
    SubUGraphAdaptorBase() 
deba@1979
   265
      : Parent(), node_filter_map(0), uedge_filter_map(0) { }
deba@1979
   266
deba@1979
   267
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
deba@1979
   268
      node_filter_map=&_node_filter_map;
deba@1979
   269
    }
deba@1979
   270
    void setUEdgeFilterMap(UEdgeFilterMap& _uedge_filter_map) {
deba@1979
   271
      uedge_filter_map=&_uedge_filter_map;
deba@1979
   272
    }
deba@1979
   273
deba@1979
   274
  public:
deba@1979
   275
deba@1979
   276
    typedef typename Parent::Node Node;
deba@1979
   277
    typedef typename Parent::Edge Edge;
deba@1979
   278
    typedef typename Parent::UEdge UEdge;
deba@1979
   279
deba@1979
   280
    void first(Node& i) const { 
deba@1979
   281
      Parent::first(i); 
deba@1979
   282
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
deba@1979
   283
    }
deba@1979
   284
deba@1979
   285
    void first(Edge& i) const { 
deba@1979
   286
      Parent::first(i); 
deba@1979
   287
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   288
	     || !(*node_filter_map)[Parent::source(i)]
deba@1979
   289
	     || !(*node_filter_map)[Parent::target(i)])) Parent::next(i); 
deba@1979
   290
    }
deba@1979
   291
deba@1979
   292
    void first(UEdge& i) const { 
deba@1979
   293
      Parent::first(i); 
deba@1979
   294
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   295
	     || !(*node_filter_map)[Parent::source(i)]
deba@1979
   296
	     || !(*node_filter_map)[Parent::target(i)])) Parent::next(i); 
deba@1979
   297
    }
deba@1979
   298
deba@1979
   299
    void firstIn(Edge& i, const Node& n) const { 
deba@1979
   300
      Parent::firstIn(i, n); 
deba@1979
   301
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   302
	     || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i); 
deba@1979
   303
    }
deba@1979
   304
deba@1979
   305
    void firstOut(Edge& i, const Node& n) const { 
deba@1979
   306
      Parent::firstOut(i, n); 
deba@1979
   307
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   308
	     || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i); 
deba@1979
   309
    }
deba@1979
   310
deba@1979
   311
    void firstInc(UEdge& i, bool& d, const Node& n) const { 
deba@1979
   312
      Parent::firstInc(i, d, n); 
deba@1979
   313
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@2381
   314
            || !(*node_filter_map)[Parent::source(i)]
deba@1979
   315
            || !(*node_filter_map)[Parent::target(i)])) Parent::nextInc(i, d); 
deba@1979
   316
    }
deba@1979
   317
deba@1979
   318
    void next(Node& i) const { 
deba@1979
   319
      Parent::next(i); 
deba@1979
   320
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
deba@1979
   321
    }
deba@1979
   322
deba@1979
   323
    void next(Edge& i) const { 
deba@1979
   324
      Parent::next(i); 
deba@1979
   325
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   326
	     || !(*node_filter_map)[Parent::source(i)]
deba@1979
   327
	     || !(*node_filter_map)[Parent::target(i)])) Parent::next(i); 
deba@1979
   328
    }
deba@1979
   329
deba@1979
   330
    void next(UEdge& i) const { 
deba@1979
   331
      Parent::next(i); 
deba@1979
   332
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   333
	     || !(*node_filter_map)[Parent::source(i)]
deba@1979
   334
	     || !(*node_filter_map)[Parent::target(i)])) Parent::next(i); 
deba@1979
   335
    }
deba@1979
   336
deba@1979
   337
    void nextIn(Edge& i) const { 
deba@1979
   338
      Parent::nextIn(i); 
deba@1979
   339
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   340
	     || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i); 
deba@1979
   341
    }
deba@1979
   342
deba@1979
   343
    void nextOut(Edge& i) const { 
deba@1979
   344
      Parent::nextOut(i); 
deba@1979
   345
      while (i!=INVALID && (!(*uedge_filter_map)[i] 
deba@1979
   346
	     || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i); 
deba@1979
   347
    }
deba@1979
   348
deba@1979
   349
    void nextInc(UEdge& i, bool& d) const { 
deba@1979
   350
      Parent::nextInc(i, d); 
deba@2381
   351
      while (i!=INVALID && (!(*uedge_filter_map)[i]
deba@2381
   352
            || !(*node_filter_map)[Parent::source(i)]
deba@2381
   353
            || !(*node_filter_map)[Parent::target(i)])) Parent::nextInc(i, d); 
deba@1979
   354
    }
deba@1979
   355
deba@2084
   356
    /// \brief Hide the given node in the graph.
deba@2084
   357
    ///
deba@1979
   358
    /// This function hides \c n in the graph, i.e. the iteration 
deba@1979
   359
    /// jumps over it. This is done by simply setting the value of \c n  
deba@1979
   360
    /// to be false in the corresponding node-map.
deba@1979
   361
    void hide(const Node& n) const { node_filter_map->set(n, false); }
deba@1979
   362
deba@2084
   363
    /// \brief Hide the given undirected edge in the graph.
deba@2084
   364
    ///
deba@1979
   365
    /// This function hides \c e in the graph, i.e. the iteration 
deba@1979
   366
    /// jumps over it. This is done by simply setting the value of \c e  
deba@2084
   367
    /// to be false in the corresponding uedge-map.
deba@1979
   368
    void hide(const UEdge& e) const { uedge_filter_map->set(e, false); }
deba@1979
   369
deba@2084
   370
    /// \brief Unhide the given node in the graph.
deba@2084
   371
    ///
deba@1979
   372
    /// The value of \c n is set to be true in the node-map which stores 
deba@1979
   373
    /// hide information. If \c n was hidden previuosly, then it is shown 
deba@1979
   374
    /// again
deba@1979
   375
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
deba@1979
   376
deba@2084
   377
    /// \brief Hide the given undirected edge in the graph.
deba@2084
   378
    ///
deba@2084
   379
    /// The value of \c e is set to be true in the uedge-map which stores 
deba@1979
   380
    /// hide information. If \c e was hidden previuosly, then it is shown 
deba@1979
   381
    /// again
deba@1979
   382
    void unHide(const UEdge& e) const { uedge_filter_map->set(e, true); }
deba@1979
   383
deba@2084
   384
    /// \brief Returns true if \c n is hidden.
deba@2084
   385
    ///
deba@1979
   386
    /// Returns true if \c n is hidden.
deba@1979
   387
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
deba@1979
   388
deba@2084
   389
    /// \brief Returns true if \c e is hidden.
deba@1979
   390
    ///
deba@2084
   391
    /// Returns true if \c e is hidden.
deba@1979
   392
    bool hidden(const UEdge& e) const { return !(*uedge_filter_map)[e]; }
deba@1979
   393
deba@1979
   394
    typedef False NodeNumTag;
deba@1979
   395
    typedef False EdgeNumTag;
deba@1991
   396
deba@1991
   397
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@2386
   398
    Edge findEdge(const Node& u, const Node& v, 
deba@1991
   399
		  const Edge& prev = INVALID) {
deba@2386
   400
      if (!(*node_filter_map)[u] || !(*node_filter_map)[v]) {
deba@1991
   401
        return INVALID;
deba@1991
   402
      }
deba@2386
   403
      Edge edge = Parent::findEdge(u, v, prev);
deba@1991
   404
      while (edge != INVALID && !(*uedge_filter_map)[edge]) {
deba@2386
   405
        edge = Parent::findEdge(u, v, edge);
deba@1991
   406
      }
deba@1991
   407
      return edge;
deba@1991
   408
    }
deba@2386
   409
    UEdge findUEdge(const Node& u, const Node& v, 
deba@1991
   410
		  const UEdge& prev = INVALID) {
deba@2386
   411
      if (!(*node_filter_map)[u] || !(*node_filter_map)[v]) {
deba@1991
   412
        return INVALID;
deba@1991
   413
      }
deba@2386
   414
      UEdge uedge = Parent::findUEdge(u, v, prev);
deba@1991
   415
      while (uedge != INVALID && !(*uedge_filter_map)[uedge]) {
deba@2386
   416
        uedge = Parent::findUEdge(u, v, uedge);
deba@1991
   417
      }
deba@1991
   418
      return uedge;
deba@1991
   419
    }
deba@2031
   420
deba@2031
   421
    template <typename _Value>
deba@2031
   422
    class NodeMap 
deba@2031
   423
      : public SubMapExtender<Adaptor, 
deba@2031
   424
                              typename Parent::template NodeMap<_Value> > 
deba@2031
   425
    {
deba@2031
   426
    public:
deba@2031
   427
      typedef Adaptor Graph;
deba@2031
   428
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   429
                             template NodeMap<_Value> > Parent;
deba@2031
   430
    
deba@2386
   431
      NodeMap(const Graph& g) 
deba@2386
   432
	: Parent(g) {}
deba@2386
   433
      NodeMap(const Graph& g, const _Value& v) 
deba@2386
   434
	: Parent(g, v) {}
deba@2031
   435
    
deba@2031
   436
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
   437
	return operator=<NodeMap>(cmap);
deba@2031
   438
      }
deba@2031
   439
    
deba@2031
   440
      template <typename CMap>
deba@2031
   441
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   442
        Parent::operator=(cmap);
deba@2031
   443
	return *this;
deba@2031
   444
      }
deba@2031
   445
    };
deba@2031
   446
deba@2031
   447
    template <typename _Value>
deba@2031
   448
    class EdgeMap 
deba@2031
   449
      : public SubMapExtender<Adaptor, 
deba@2031
   450
                              typename Parent::template EdgeMap<_Value> > 
deba@2031
   451
    {
deba@2031
   452
    public:
deba@2031
   453
      typedef Adaptor Graph;
deba@2031
   454
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   455
                             template EdgeMap<_Value> > Parent;
deba@2031
   456
    
deba@2386
   457
      EdgeMap(const Graph& g) 
deba@2386
   458
	: Parent(g) {}
deba@2386
   459
      EdgeMap(const Graph& g, const _Value& v) 
deba@2386
   460
	: Parent(g, v) {}
deba@2031
   461
    
deba@2031
   462
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
   463
	return operator=<EdgeMap>(cmap);
deba@2031
   464
      }
deba@2031
   465
    
deba@2031
   466
      template <typename CMap>
deba@2031
   467
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   468
        Parent::operator=(cmap);
deba@2031
   469
	return *this;
deba@2031
   470
      }
deba@2031
   471
    };
deba@2031
   472
deba@2031
   473
    template <typename _Value>
deba@2031
   474
    class UEdgeMap 
deba@2031
   475
      : public SubMapExtender<Adaptor, 
deba@2031
   476
                              typename Parent::template UEdgeMap<_Value> > 
deba@2031
   477
    {
deba@2031
   478
    public:
deba@2031
   479
      typedef Adaptor Graph;
deba@2031
   480
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   481
                             template UEdgeMap<_Value> > Parent;
deba@2031
   482
    
deba@2386
   483
      UEdgeMap(const Graph& g) 
deba@2386
   484
	: Parent(g) {}
deba@2386
   485
      UEdgeMap(const Graph& g, const _Value& v) 
deba@2386
   486
	: Parent(g, v) {}
deba@2031
   487
    
deba@2031
   488
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2031
   489
	return operator=<UEdgeMap>(cmap);
deba@2031
   490
      }
deba@2031
   491
    
deba@2031
   492
      template <typename CMap>
deba@2031
   493
      UEdgeMap& operator=(const CMap& cmap) {
deba@2031
   494
        Parent::operator=(cmap);
deba@2031
   495
	return *this;
deba@2031
   496
      }
deba@2031
   497
    };
deba@2031
   498
deba@1979
   499
  };
deba@1979
   500
deba@1979
   501
  template <typename _UGraph, typename NodeFilterMap, typename UEdgeFilterMap>
deba@1979
   502
  class SubUGraphAdaptorBase<_UGraph, NodeFilterMap, UEdgeFilterMap, false> 
deba@1979
   503
    : public UGraphAdaptorBase<_UGraph> {
deba@1979
   504
  public:
deba@1979
   505
    typedef _UGraph Graph;
deba@2031
   506
    typedef SubUGraphAdaptorBase Adaptor;
deba@1979
   507
    typedef UGraphAdaptorBase<_UGraph> Parent;
deba@1979
   508
  protected:
deba@1979
   509
    NodeFilterMap* node_filter_map;
deba@1979
   510
    UEdgeFilterMap* uedge_filter_map;
deba@1979
   511
    SubUGraphAdaptorBase() : Parent(), 
deba@1979
   512
			    node_filter_map(0), uedge_filter_map(0) { }
deba@1979
   513
deba@1979
   514
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
deba@1979
   515
      node_filter_map=&_node_filter_map;
deba@1979
   516
    }
deba@1979
   517
    void setUEdgeFilterMap(UEdgeFilterMap& _uedge_filter_map) {
deba@1979
   518
      uedge_filter_map=&_uedge_filter_map;
deba@1979
   519
    }
deba@1979
   520
deba@1979
   521
  public:
deba@1979
   522
deba@1979
   523
    typedef typename Parent::Node Node;
deba@1979
   524
    typedef typename Parent::Edge Edge;
deba@1979
   525
    typedef typename Parent::UEdge UEdge;
deba@1979
   526
deba@1979
   527
    void first(Node& i) const { 
deba@1979
   528
      Parent::first(i); 
deba@1979
   529
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
deba@1979
   530
    }
deba@1979
   531
deba@1979
   532
    void first(Edge& i) const { 
deba@1979
   533
      Parent::first(i); 
deba@1979
   534
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::next(i); 
deba@1979
   535
    }
deba@1979
   536
deba@1979
   537
    void first(UEdge& i) const { 
deba@1979
   538
      Parent::first(i); 
deba@1979
   539
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::next(i); 
deba@1979
   540
    }
deba@1979
   541
deba@1979
   542
    void firstIn(Edge& i, const Node& n) const { 
deba@1979
   543
      Parent::firstIn(i, n); 
deba@1979
   544
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::nextIn(i); 
deba@1979
   545
    }
deba@1979
   546
deba@1979
   547
    void firstOut(Edge& i, const Node& n) const { 
deba@1979
   548
      Parent::firstOut(i, n); 
deba@1979
   549
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::nextOut(i); 
deba@1979
   550
    }
deba@1979
   551
deba@1979
   552
    void firstInc(UEdge& i, bool& d, const Node& n) const { 
deba@1979
   553
      Parent::firstInc(i, d, n); 
deba@1979
   554
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::nextInc(i, d); 
deba@1979
   555
    }
deba@1979
   556
deba@1979
   557
    void next(Node& i) const { 
deba@1979
   558
      Parent::next(i); 
deba@1979
   559
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
deba@1979
   560
    }
deba@1979
   561
    void next(Edge& i) const { 
deba@1979
   562
      Parent::next(i); 
deba@1979
   563
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::next(i); 
deba@1979
   564
    }
deba@1979
   565
    void next(UEdge& i) const { 
deba@1979
   566
      Parent::next(i); 
deba@1979
   567
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::next(i); 
deba@1979
   568
    }
deba@1979
   569
    void nextIn(Edge& i) const { 
deba@1979
   570
      Parent::nextIn(i); 
deba@1979
   571
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::nextIn(i); 
deba@1979
   572
    }
deba@1979
   573
deba@1979
   574
    void nextOut(Edge& i) const { 
deba@1979
   575
      Parent::nextOut(i); 
deba@1979
   576
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::nextOut(i); 
deba@1979
   577
    }
deba@1979
   578
    void nextInc(UEdge& i, bool& d) const { 
deba@1979
   579
      Parent::nextInc(i, d); 
deba@1979
   580
      while (i!=INVALID && !(*uedge_filter_map)[i]) Parent::nextInc(i, d); 
deba@1979
   581
    }
deba@1979
   582
deba@2084
   583
    /// \brief Hide the given node in the graph.
deba@2084
   584
    ///
deba@1979
   585
    /// This function hides \c n in the graph, i.e. the iteration 
deba@1979
   586
    /// jumps over it. This is done by simply setting the value of \c n  
deba@1979
   587
    /// to be false in the corresponding node-map.
deba@1979
   588
    void hide(const Node& n) const { node_filter_map->set(n, false); }
deba@1979
   589
deba@2084
   590
    /// \brief Hide the given undirected edge in the graph.
deba@2084
   591
    ///
deba@1979
   592
    /// This function hides \c e in the graph, i.e. the iteration 
deba@1979
   593
    /// jumps over it. This is done by simply setting the value of \c e  
deba@2084
   594
    /// to be false in the corresponding uedge-map.
deba@1979
   595
    void hide(const UEdge& e) const { uedge_filter_map->set(e, false); }
deba@1979
   596
deba@2084
   597
    /// \brief Unhide the given node in the graph.
deba@2084
   598
    ///
deba@1979
   599
    /// The value of \c n is set to be true in the node-map which stores 
deba@1979
   600
    /// hide information. If \c n was hidden previuosly, then it is shown 
deba@1979
   601
    /// again
deba@1979
   602
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
deba@1979
   603
deba@2084
   604
    /// \brief Hide the given undirected edge in the graph.
deba@2084
   605
    ///
deba@2084
   606
    /// The value of \c e is set to be true in the uedge-map which stores 
deba@1979
   607
    /// hide information. If \c e was hidden previuosly, then it is shown 
deba@1979
   608
    /// again
deba@1979
   609
    void unHide(const UEdge& e) const { uedge_filter_map->set(e, true); }
deba@1979
   610
deba@2084
   611
    /// \brief Returns true if \c n is hidden.
deba@2084
   612
    ///
deba@1979
   613
    /// Returns true if \c n is hidden.
deba@1979
   614
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
deba@1979
   615
deba@2084
   616
    /// \brief Returns true if \c e is hidden.
deba@1979
   617
    ///
deba@2084
   618
    /// Returns true if \c e is hidden.
deba@1979
   619
    bool hidden(const UEdge& e) const { return !(*uedge_filter_map)[e]; }
deba@1979
   620
deba@1979
   621
    typedef False NodeNumTag;
deba@1979
   622
    typedef False EdgeNumTag;
deba@1991
   623
deba@1991
   624
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@2386
   625
    Edge findEdge(const Node& u, const Node& v, 
deba@1991
   626
		  const Edge& prev = INVALID) {
deba@2386
   627
      Edge edge = Parent::findEdge(u, v, prev);
deba@1991
   628
      while (edge != INVALID && !(*uedge_filter_map)[edge]) {
deba@2386
   629
        edge = Parent::findEdge(u, v, edge);
deba@1991
   630
      }
deba@1991
   631
      return edge;
deba@1991
   632
    }
deba@2386
   633
    UEdge findUEdge(const Node& u, const Node& v, 
deba@1991
   634
		  const UEdge& prev = INVALID) {
deba@2386
   635
      UEdge uedge = Parent::findUEdge(u, v, prev);
deba@1991
   636
      while (uedge != INVALID && !(*uedge_filter_map)[uedge]) {
deba@2386
   637
        uedge = Parent::findUEdge(u, v, uedge);
deba@1991
   638
      }
deba@1991
   639
      return uedge;
deba@1991
   640
    }
deba@2031
   641
deba@2031
   642
    template <typename _Value>
deba@2031
   643
    class NodeMap 
deba@2031
   644
      : public SubMapExtender<Adaptor, 
deba@2031
   645
                              typename Parent::template NodeMap<_Value> > 
deba@2031
   646
    {
deba@2031
   647
    public:
deba@2031
   648
      typedef Adaptor Graph;
deba@2031
   649
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   650
                             template NodeMap<_Value> > Parent;
deba@2031
   651
    
deba@2386
   652
      NodeMap(const Graph& g) 
deba@2386
   653
	: Parent(g) {}
deba@2386
   654
      NodeMap(const Graph& g, const _Value& v) 
deba@2386
   655
	: Parent(g, v) {}
deba@2031
   656
    
deba@2031
   657
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
   658
	return operator=<NodeMap>(cmap);
deba@2031
   659
      }
deba@2031
   660
    
deba@2031
   661
      template <typename CMap>
deba@2031
   662
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   663
        Parent::operator=(cmap);
deba@2031
   664
	return *this;
deba@2031
   665
      }
deba@2031
   666
    };
deba@2031
   667
deba@2031
   668
    template <typename _Value>
deba@2031
   669
    class EdgeMap 
deba@2031
   670
      : public SubMapExtender<Adaptor, 
deba@2031
   671
                              typename Parent::template EdgeMap<_Value> > 
deba@2031
   672
    {
deba@2031
   673
    public:
deba@2031
   674
      typedef Adaptor Graph;
deba@2031
   675
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   676
                             template EdgeMap<_Value> > Parent;
deba@2031
   677
    
deba@2386
   678
      EdgeMap(const Graph& g) 
deba@2386
   679
	: Parent(g) {}
deba@2386
   680
      EdgeMap(const Graph& g, const _Value& v) 
deba@2386
   681
	: Parent(g, v) {}
deba@2031
   682
    
deba@2031
   683
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
   684
	return operator=<EdgeMap>(cmap);
deba@2031
   685
      }
deba@2031
   686
    
deba@2031
   687
      template <typename CMap>
deba@2031
   688
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   689
        Parent::operator=(cmap);
deba@2031
   690
	return *this;
deba@2031
   691
      }
deba@2031
   692
    };
deba@2031
   693
deba@2031
   694
    template <typename _Value>
deba@2031
   695
    class UEdgeMap 
deba@2031
   696
      : public SubMapExtender<Adaptor, 
deba@2031
   697
                              typename Parent::template UEdgeMap<_Value> > 
deba@2031
   698
    {
deba@2031
   699
    public:
deba@2031
   700
      typedef Adaptor Graph;
deba@2031
   701
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   702
                             template UEdgeMap<_Value> > Parent;
deba@2031
   703
    
deba@2386
   704
      UEdgeMap(const Graph& g) 
deba@2386
   705
	: Parent(g) {}
deba@2386
   706
      UEdgeMap(const Graph& g, const _Value& v) 
deba@2386
   707
	: Parent(g, v) {}
deba@2031
   708
    
deba@2031
   709
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2031
   710
	return operator=<UEdgeMap>(cmap);
deba@2031
   711
      }
deba@2031
   712
    
deba@2031
   713
      template <typename CMap>
deba@2031
   714
      UEdgeMap& operator=(const CMap& cmap) {
deba@2031
   715
        Parent::operator=(cmap);
deba@2031
   716
	return *this;
deba@2031
   717
      }
deba@2031
   718
    };
deba@1979
   719
  };
deba@1979
   720
deba@1979
   721
  /// \ingroup graph_adaptors
deba@1979
   722
  ///
deba@1979
   723
  /// \brief A graph adaptor for hiding nodes and edges from an undirected 
deba@1979
   724
  /// graph.
deba@1979
   725
  /// 
deba@1979
   726
  /// SubUGraphAdaptor shows the undirected graph with filtered node-set and 
deba@1979
   727
  /// edge-set. If the \c checked parameter is true then it filters the edgeset
deba@1979
   728
  /// to do not get invalid edges without source or target.
deba@1979
   729
  /// 
deba@1979
   730
  /// If the \c checked template parameter is false then we have to note that 
deba@1979
   731
  /// the node-iterator cares only the filter on the node-set, and the 
deba@1979
   732
  /// edge-iterator cares only the filter on the edge-set.
deba@1979
   733
  /// This way the edge-map
deba@1979
   734
  /// should filter all edges which's source or target is filtered by the 
deba@1979
   735
  /// node-filter.
deba@1979
   736
  ///
deba@1979
   737
  template<typename _UGraph, typename NodeFilterMap, 
deba@1979
   738
	   typename UEdgeFilterMap, bool checked = true>
deba@1979
   739
  class SubUGraphAdaptor : 
deba@1979
   740
    public UGraphAdaptorExtender<
deba@1979
   741
    SubUGraphAdaptorBase<_UGraph, NodeFilterMap, UEdgeFilterMap, checked> > {
deba@1979
   742
  public:
deba@1979
   743
    typedef _UGraph Graph;
deba@1979
   744
    typedef UGraphAdaptorExtender<
deba@1979
   745
      SubUGraphAdaptorBase<_UGraph, NodeFilterMap, UEdgeFilterMap> > Parent;
deba@1979
   746
  protected:
deba@1979
   747
    SubUGraphAdaptor() { }
deba@1979
   748
  public:
deba@1979
   749
    SubUGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map, 
deba@1979
   750
		    UEdgeFilterMap& _uedge_filter_map) { 
deba@1979
   751
      setGraph(_graph);
deba@1979
   752
      setNodeFilterMap(_node_filter_map);
deba@1979
   753
      setUEdgeFilterMap(_uedge_filter_map);
deba@1979
   754
    }
deba@1979
   755
  };
deba@1979
   756
deba@1980
   757
  template<typename UGraph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1980
   758
  SubUGraphAdaptor<const UGraph, NodeFilterMap, EdgeFilterMap>
deba@1980
   759
  subUGraphAdaptor(const UGraph& graph, 
deba@1980
   760
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1980
   761
    return SubUGraphAdaptor<const UGraph, NodeFilterMap, EdgeFilterMap>
deba@1980
   762
      (graph, nfm, efm);
deba@1980
   763
  }
deba@1980
   764
deba@1980
   765
  template<typename UGraph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1980
   766
  SubUGraphAdaptor<const UGraph, const NodeFilterMap, EdgeFilterMap>
deba@1980
   767
  subUGraphAdaptor(const UGraph& graph, 
deba@1980
   768
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1980
   769
    return SubUGraphAdaptor<const UGraph, const NodeFilterMap, EdgeFilterMap>
deba@1980
   770
      (graph, nfm, efm);
deba@1980
   771
  }
deba@1980
   772
deba@1980
   773
  template<typename UGraph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1980
   774
  SubUGraphAdaptor<const UGraph, NodeFilterMap, const EdgeFilterMap>
deba@1980
   775
  subUGraphAdaptor(const UGraph& graph, 
deba@1980
   776
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1980
   777
    return SubUGraphAdaptor<const UGraph, NodeFilterMap, const EdgeFilterMap>
deba@1980
   778
      (graph, nfm, efm);
deba@1980
   779
  }
deba@1980
   780
deba@1980
   781
  template<typename UGraph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1980
   782
  SubUGraphAdaptor<const UGraph, const NodeFilterMap, const EdgeFilterMap>
deba@1980
   783
  subUGraphAdaptor(const UGraph& graph, 
deba@1980
   784
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1980
   785
    return SubUGraphAdaptor<const UGraph, const NodeFilterMap, 
deba@1980
   786
      const EdgeFilterMap>(graph, nfm, efm);
deba@1980
   787
  }
deba@1980
   788
deba@1979
   789
  /// \ingroup graph_adaptors
deba@1979
   790
  ///
deba@1980
   791
  /// \brief An adaptor for hiding nodes from an undirected graph.
deba@1979
   792
  ///
deba@1979
   793
  /// An adaptor for hiding nodes from an undirected graph.
deba@1979
   794
  /// This adaptor specializes SubUGraphAdaptor in the way that only
deba@1979
   795
  /// the node-set 
deba@1979
   796
  /// can be filtered. In usual case the checked parameter is true, we get the
deba@1979
   797
  /// induced subgraph. But if the checked parameter is false then we can only
deba@1979
   798
  /// filter only isolated nodes.
deba@1979
   799
  template<typename _UGraph, typename NodeFilterMap, bool checked = true>
deba@1979
   800
  class NodeSubUGraphAdaptor : 
deba@1979
   801
    public SubUGraphAdaptor<_UGraph, NodeFilterMap, 
deba@1979
   802
                            ConstMap<typename _UGraph::UEdge, bool>, checked> {
deba@1979
   803
  public:
deba@1979
   804
    typedef SubUGraphAdaptor<_UGraph, NodeFilterMap, 
deba@1979
   805
                             ConstMap<typename _UGraph::UEdge, bool> > Parent;
deba@1979
   806
    typedef _UGraph Graph;
deba@1979
   807
  protected:
deba@1985
   808
    ConstMap<typename _UGraph::UEdge, bool> const_true_map;
deba@1991
   809
deba@1991
   810
    NodeSubUGraphAdaptor() : const_true_map(true) {
deba@1991
   811
      Parent::setUEdgeFilterMap(const_true_map);
deba@1991
   812
    }
deba@1991
   813
deba@1979
   814
  public:
deba@1979
   815
    NodeSubUGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) : 
deba@1979
   816
      Parent(), const_true_map(true) { 
deba@1979
   817
      Parent::setGraph(_graph);
deba@1979
   818
      Parent::setNodeFilterMap(_node_filter_map);
deba@1979
   819
      Parent::setUEdgeFilterMap(const_true_map);
deba@1979
   820
    }
deba@1979
   821
  };
deba@1979
   822
deba@1979
   823
  template<typename UGraph, typename NodeFilterMap>
deba@1979
   824
  NodeSubUGraphAdaptor<const UGraph, NodeFilterMap>
deba@1979
   825
  nodeSubUGraphAdaptor(const UGraph& graph, NodeFilterMap& nfm) {
deba@1979
   826
    return NodeSubUGraphAdaptor<const UGraph, NodeFilterMap>(graph, nfm);
deba@1979
   827
  }
deba@1979
   828
deba@1979
   829
  template<typename UGraph, typename NodeFilterMap>
deba@1979
   830
  NodeSubUGraphAdaptor<const UGraph, const NodeFilterMap>
deba@1979
   831
  nodeSubUGraphAdaptor(const UGraph& graph, const NodeFilterMap& nfm) {
deba@1979
   832
    return NodeSubUGraphAdaptor<const UGraph, const NodeFilterMap>(graph, nfm);
deba@1979
   833
  }
deba@1979
   834
deba@2042
   835
  /// \ingroup graph_adaptors
deba@2042
   836
  ///
deba@1979
   837
  /// \brief An adaptor for hiding undirected edges from an undirected graph.
deba@1979
   838
  ///
deba@1979
   839
  /// \warning Graph adaptors are in even more experimental state
deba@1979
   840
  /// than the other parts of the lib. Use them at you own risk.
deba@1979
   841
  ///
deba@1979
   842
  /// An adaptor for hiding undirected edges from an undirected graph.
deba@1979
   843
  /// This adaptor specializes SubUGraphAdaptor in the way that
deba@1979
   844
  /// only the edge-set 
deba@1979
   845
  /// can be filtered.
deba@1979
   846
  template<typename _UGraph, typename UEdgeFilterMap>
deba@1979
   847
  class EdgeSubUGraphAdaptor : 
deba@1979
   848
    public SubUGraphAdaptor<_UGraph, ConstMap<typename _UGraph::Node,bool>, 
deba@1979
   849
                            UEdgeFilterMap, false> {
deba@1979
   850
  public:
deba@1979
   851
    typedef SubUGraphAdaptor<_UGraph, ConstMap<typename _UGraph::Node,bool>, 
deba@1979
   852
                             UEdgeFilterMap, false> Parent;
deba@1979
   853
    typedef _UGraph Graph;
deba@1979
   854
  protected:
deba@1979
   855
    ConstMap<typename Graph::Node, bool> const_true_map;
deba@1991
   856
deba@1991
   857
    EdgeSubUGraphAdaptor() : const_true_map(true) {
deba@1991
   858
      Parent::setNodeFilterMap(const_true_map);
deba@1991
   859
    }
deba@1991
   860
deba@1979
   861
  public:
deba@2031
   862
deba@1979
   863
    EdgeSubUGraphAdaptor(Graph& _graph, UEdgeFilterMap& _uedge_filter_map) : 
deba@1979
   864
      Parent(), const_true_map(true) { 
deba@1979
   865
      Parent::setGraph(_graph);
deba@1979
   866
      Parent::setNodeFilterMap(const_true_map);
deba@1979
   867
      Parent::setUEdgeFilterMap(_uedge_filter_map);
deba@1979
   868
    }
deba@2031
   869
deba@1979
   870
  };
deba@1979
   871
deba@1979
   872
  template<typename UGraph, typename EdgeFilterMap>
deba@1979
   873
  EdgeSubUGraphAdaptor<const UGraph, EdgeFilterMap>
deba@1979
   874
  edgeSubUGraphAdaptor(const UGraph& graph, EdgeFilterMap& efm) {
deba@1979
   875
    return EdgeSubUGraphAdaptor<const UGraph, EdgeFilterMap>(graph, efm);
deba@1979
   876
  }
deba@1979
   877
deba@1979
   878
  template<typename UGraph, typename EdgeFilterMap>
deba@1979
   879
  EdgeSubUGraphAdaptor<const UGraph, const EdgeFilterMap>
deba@1979
   880
  edgeSubUGraphAdaptor(const UGraph& graph, const EdgeFilterMap& efm) {
deba@1979
   881
    return EdgeSubUGraphAdaptor<const UGraph, const EdgeFilterMap>(graph, efm);
deba@1979
   882
  }
deba@1979
   883
deba@2087
   884
  /// \brief Base of direct undirected graph adaptor
deba@2087
   885
  ///
deba@2087
   886
  /// Base class of the direct undirected graph adaptor. All public member
deba@2087
   887
  /// of this class can be used with the DirUGraphAdaptor too.
deba@2087
   888
  /// \sa DirUGraphAdaptor
deba@1979
   889
  template <typename _UGraph, typename _DirectionMap>
deba@1980
   890
  class DirUGraphAdaptorBase {
deba@1979
   891
  public:
deba@1979
   892
    
deba@1979
   893
    typedef _UGraph Graph;
deba@1979
   894
    typedef _DirectionMap DirectionMap;
deba@1979
   895
deba@1979
   896
    typedef typename _UGraph::Node Node;
deba@1979
   897
    typedef typename _UGraph::UEdge Edge;
deba@1979
   898
   
deba@2087
   899
    /// \brief Reverse edge
deba@2087
   900
    /// 
deba@2087
   901
    /// It reverse the given edge. It simply negate the direction in the map.
deba@1991
   902
    void reverseEdge(const Edge& edge) {
deba@1991
   903
      direction->set(edge, !(*direction)[edge]);
deba@1991
   904
    }
deba@1991
   905
deba@1979
   906
    void first(Node& i) const { graph->first(i); }
deba@1979
   907
    void first(Edge& i) const { graph->first(i); }
deba@1979
   908
    void firstIn(Edge& i, const Node& n) const {
deba@1979
   909
      bool d;
deba@1979
   910
      graph->firstInc(i, d, n);
deba@1979
   911
      while (i != INVALID && d == (*direction)[i]) graph->nextInc(i, d);
deba@1979
   912
    }
deba@1979
   913
    void firstOut(Edge& i, const Node& n ) const { 
deba@1979
   914
      bool d;
deba@1979
   915
      graph->firstInc(i, d, n);
deba@1979
   916
      while (i != INVALID && d != (*direction)[i]) graph->nextInc(i, d);
deba@1979
   917
    }
deba@1979
   918
deba@1979
   919
    void next(Node& i) const { graph->next(i); }
deba@1979
   920
    void next(Edge& i) const { graph->next(i); }
deba@1979
   921
    void nextIn(Edge& i) const {
deba@1979
   922
      bool d = !(*direction)[i];
deba@1979
   923
      graph->nextInc(i, d);
deba@1979
   924
      while (i != INVALID && d == (*direction)[i]) graph->nextInc(i, d);
deba@1979
   925
    }
deba@1979
   926
    void nextOut(Edge& i) const {
deba@1979
   927
      bool d = (*direction)[i];
deba@1979
   928
      graph->nextInc(i, d);
deba@1979
   929
      while (i != INVALID && d != (*direction)[i]) graph->nextInc(i, d);
deba@1979
   930
    }
deba@1979
   931
deba@1979
   932
    Node source(const Edge& e) const { 
deba@1979
   933
      return (*direction)[e] ? graph->source(e) : graph->target(e); 
deba@1979
   934
    }
deba@1979
   935
    Node target(const Edge& e) const { 
deba@1979
   936
      return (*direction)[e] ? graph->target(e) : graph->source(e); 
deba@1979
   937
    }
deba@1979
   938
deba@1979
   939
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
deba@1979
   940
    int nodeNum() const { return graph->nodeNum(); }
deba@1979
   941
    
deba@1979
   942
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
deba@1979
   943
    int edgeNum() const { return graph->uEdgeNum(); }
deba@1979
   944
deba@1979
   945
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@2386
   946
    Edge findEdge(const Node& u, const Node& v, 
deba@1979
   947
		  const Edge& prev = INVALID) {
deba@1979
   948
      Edge edge = prev;
deba@1979
   949
      bool d = edge == INVALID ? true : (*direction)[edge];
deba@1979
   950
      if (d) {
deba@2386
   951
        edge = graph->findUEdge(u, v, edge);
deba@1979
   952
        while (edge != INVALID && !(*direction)[edge]) {
deba@2386
   953
          graph->findUEdge(u, v, edge);
deba@1979
   954
        }
deba@1979
   955
        if (edge != INVALID) return edge;
deba@1979
   956
      }
deba@2386
   957
      graph->findUEdge(v, u, edge);
deba@1979
   958
      while (edge != INVALID && (*direction)[edge]) {
deba@2386
   959
        graph->findUEdge(u, v, edge);
deba@1979
   960
      }
deba@1979
   961
      return edge;
deba@1979
   962
    }
deba@1979
   963
  
deba@1979
   964
    Node addNode() const { 
deba@1979
   965
      return Node(graph->addNode()); 
deba@1979
   966
    }
deba@1979
   967
deba@2386
   968
    Edge addEdge(const Node& u, const Node& v) const {
deba@2386
   969
      Edge edge = graph->addEdge(u, v);
deba@2386
   970
      direction->set(edge, graph->source(edge) == u);
deba@1979
   971
      return edge; 
deba@1979
   972
    }
deba@1979
   973
deba@1979
   974
    void erase(const Node& i) const { graph->erase(i); }
deba@1979
   975
    void erase(const Edge& i) const { graph->erase(i); }
deba@1979
   976
  
deba@1979
   977
    void clear() const { graph->clear(); }
deba@1979
   978
    
deba@1979
   979
    int id(const Node& v) const { return graph->id(v); }
deba@1979
   980
    int id(const Edge& e) const { return graph->id(e); }
deba@1979
   981
deba@1991
   982
    int maxNodeId() const {
deba@1991
   983
      return graph->maxNodeId();
deba@1979
   984
    }
deba@1979
   985
deba@1991
   986
    int maxEdgeId() const {
deba@1991
   987
      return graph->maxEdgeId();
deba@1991
   988
    }
deba@1991
   989
deba@1991
   990
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
deba@1991
   991
deba@2381
   992
    NodeNotifier& notifier(Node) const {
deba@2381
   993
      return graph->notifier(Node());
deba@1991
   994
    } 
deba@1991
   995
deba@1991
   996
    typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
deba@1991
   997
deba@2381
   998
    EdgeNotifier& notifier(Edge) const {
deba@2381
   999
      return graph->notifier(Edge());
deba@1991
  1000
    } 
deba@1991
  1001
deba@1979
  1002
    template <typename _Value>
deba@1979
  1003
    class NodeMap : public _UGraph::template NodeMap<_Value> {
deba@1979
  1004
    public:
deba@2031
  1005
deba@1979
  1006
      typedef typename _UGraph::template NodeMap<_Value> Parent;
deba@2031
  1007
deba@1980
  1008
      explicit NodeMap(const DirUGraphAdaptorBase& ga) 
deba@2031
  1009
	: Parent(*ga.graph) {}
deba@2031
  1010
deba@1980
  1011
      NodeMap(const DirUGraphAdaptorBase& ga, const _Value& value)
deba@2031
  1012
	: Parent(*ga.graph, value) {}
deba@2031
  1013
deba@2031
  1014
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
  1015
        return operator=<NodeMap>(cmap);
deba@2031
  1016
      }
deba@2031
  1017
deba@2031
  1018
      template <typename CMap>
deba@2031
  1019
      NodeMap& operator=(const CMap& cmap) {
deba@2031
  1020
        Parent::operator=(cmap);
deba@2031
  1021
        return *this;
deba@2031
  1022
      }
deba@2031
  1023
deba@1979
  1024
    };
deba@1979
  1025
deba@1979
  1026
    template <typename _Value>
deba@1979
  1027
    class EdgeMap : public _UGraph::template UEdgeMap<_Value> {
deba@1979
  1028
    public:
deba@2031
  1029
deba@1980
  1030
      typedef typename _UGraph::template UEdgeMap<_Value> Parent;
deba@2031
  1031
deba@1980
  1032
      explicit EdgeMap(const DirUGraphAdaptorBase& ga) 
deba@1979
  1033
	: Parent(*ga.graph) { }
deba@2031
  1034
deba@1980
  1035
      EdgeMap(const DirUGraphAdaptorBase& ga, const _Value& value)
deba@1979
  1036
	: Parent(*ga.graph, value) { }
deba@2031
  1037
deba@2031
  1038
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
  1039
        return operator=<EdgeMap>(cmap);
deba@2031
  1040
      }
deba@2031
  1041
deba@2031
  1042
      template <typename CMap>
deba@2031
  1043
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
  1044
        Parent::operator=(cmap);
deba@2031
  1045
        return *this;
deba@2031
  1046
      }
deba@1979
  1047
    };
deba@1979
  1048
deba@1979
  1049
    
deba@1979
  1050
deba@1979
  1051
  protected:
deba@1979
  1052
    Graph* graph;
deba@1979
  1053
    DirectionMap* direction;
deba@1979
  1054
deba@1979
  1055
    void setDirectionMap(DirectionMap& _direction) {
deba@1979
  1056
      direction = &_direction;
deba@1979
  1057
    }
deba@1979
  1058
deba@1979
  1059
    void setGraph(Graph& _graph) {
deba@1979
  1060
      graph = &_graph;
deba@1979
  1061
    }
deba@1979
  1062
deba@1979
  1063
  };
deba@1979
  1064
deba@1979
  1065
deba@1980
  1066
  /// \ingroup graph_adaptors
deba@1991
  1067
  ///
deba@1991
  1068
  /// \brief A directed graph is made from an undirected graph by an adaptor
deba@1980
  1069
  ///
deba@2087
  1070
  /// This adaptor gives a direction for each uedge in the undirected
deba@2087
  1071
  /// graph. The direction of the edges stored in the
deba@2087
  1072
  /// DirectionMap. This map is a bool map on the undirected edges. If
deba@2087
  1073
  /// the uedge is mapped to true then the direction of the directed
deba@2087
  1074
  /// edge will be the same as the default direction of the uedge. The
deba@2087
  1075
  /// edges can be easily reverted by the \ref
deba@2087
  1076
  /// DirUGraphAdaptorBase::reverseEdge "reverseEdge()" member in the
deba@2087
  1077
  /// adaptor.
deba@2087
  1078
  ///
deba@2087
  1079
  /// It can be used to solve orientation problems on directed graphs.
deba@2087
  1080
  /// By example how can we orient an undirected graph to get the minimum
deba@2087
  1081
  /// number of strongly connected components. If we orient the edges with
deba@2087
  1082
  /// the dfs algorithm out from the source then we will get such an 
deba@2087
  1083
  /// orientation. 
deba@2087
  1084
  ///
deba@2087
  1085
  /// We use the \ref DfsVisitor "visitor" interface of the 
deba@2087
  1086
  /// \ref DfsVisit "dfs" algorithm:
deba@2087
  1087
  ///\code
deba@2087
  1088
  /// template <typename DirMap>
deba@2087
  1089
  /// class OrientVisitor : public DfsVisitor<UGraph> {
deba@2087
  1090
  /// public:
deba@2087
  1091
  ///
deba@2087
  1092
  ///   OrientVisitor(const UGraph& ugraph, DirMap& dirMap)
deba@2087
  1093
  ///     : _ugraph(ugraph), _dirMap(dirMap), _processed(ugraph, false) {}
deba@2087
  1094
  ///
deba@2087
  1095
  ///   void discover(const Edge& edge) {
deba@2087
  1096
  ///     _processed.set(edge, true);
deba@2087
  1097
  ///     _dirMap.set(edge, _ugraph.direction(edge));
deba@2087
  1098
  ///   }
deba@2087
  1099
  ///
deba@2087
  1100
  ///   void examine(const Edge& edge) {
deba@2087
  1101
  ///     if (_processed[edge]) return;
deba@2087
  1102
  ///     _processed.set(edge, true);
deba@2087
  1103
  ///     _dirMap.set(edge, _ugraph.direction(edge));
deba@2087
  1104
  ///   }  
deba@2087
  1105
  /// 
deba@2087
  1106
  /// private:
deba@2087
  1107
  ///   const UGraph& _ugraph;  
deba@2087
  1108
  ///   DirMap& _dirMap;
deba@2087
  1109
  ///   UGraph::UEdgeMap<bool> _processed;
deba@2087
  1110
  /// };
deba@2087
  1111
  ///\endcode
deba@2087
  1112
  ///
deba@2087
  1113
  /// And now we can use the orientation:
deba@2087
  1114
  ///\code
deba@2087
  1115
  /// UGraph::UEdgeMap<bool> dmap(ugraph);
deba@2087
  1116
  ///
deba@2087
  1117
  /// typedef OrientVisitor<UGraph::UEdgeMap<bool> > Visitor;
deba@2087
  1118
  /// Visitor visitor(ugraph, dmap);
deba@2087
  1119
  ///
deba@2087
  1120
  /// DfsVisit<UGraph, Visitor> dfs(ugraph, visitor);
deba@2087
  1121
  ///
deba@2087
  1122
  /// dfs.run();
deba@2087
  1123
  ///
deba@2087
  1124
  /// typedef DirUGraphAdaptor<UGraph> DGraph;
deba@2087
  1125
  /// DGraph dgraph(ugraph, dmap);
deba@2087
  1126
  ///
deba@2087
  1127
  /// LEMON_ASSERT(countStronglyConnectedComponents(dgraph) == 
deba@2087
  1128
  ///              countBiEdgeConnectedComponents(ugraph), "Wrong Orientation");
deba@2087
  1129
  ///\endcode
deba@2087
  1130
  ///
deba@2087
  1131
  /// The number of the bi-connected components is a lower bound for
deba@2087
  1132
  /// the number of the strongly connected components in the directed
deba@2087
  1133
  /// graph because if we contract the bi-connected components to
deba@2087
  1134
  /// nodes we will get a tree therefore we cannot orient edges in
deba@2087
  1135
  /// both direction between bi-connected components. In the other way
deba@2087
  1136
  /// the algorithm will orient one component to be strongly
deba@2087
  1137
  /// connected. The two relations proof that the assertion will
deba@2087
  1138
  /// be always true and the found solution is optimal.
deba@2087
  1139
  ///
deba@2087
  1140
  /// \sa DirUGraphAdaptorBase
deba@2087
  1141
  /// \sa dirUGraphAdaptor
deba@1980
  1142
  template<typename _Graph, 
deba@1980
  1143
           typename DirectionMap = typename _Graph::template UEdgeMap<bool> > 
deba@1980
  1144
  class DirUGraphAdaptor : 
deba@1979
  1145
    public GraphAdaptorExtender<
deba@1980
  1146
    DirUGraphAdaptorBase<_Graph, DirectionMap> > {
deba@1979
  1147
  public:
deba@1979
  1148
    typedef _Graph Graph;
deba@1979
  1149
    typedef GraphAdaptorExtender<
deba@1980
  1150
      DirUGraphAdaptorBase<_Graph, DirectionMap> > Parent;
deba@1979
  1151
  protected:
deba@1980
  1152
    DirUGraphAdaptor() { }
deba@1979
  1153
  public:
deba@2087
  1154
    
deba@2087
  1155
    /// \brief Constructor of the adaptor
deba@2087
  1156
    ///
deba@2087
  1157
    /// Constructor of the adaptor
deba@1980
  1158
    DirUGraphAdaptor(_Graph& _graph, DirectionMap& _direction_map) { 
deba@1979
  1159
      setGraph(_graph);
deba@1979
  1160
      setDirectionMap(_direction_map);
deba@1979
  1161
    }
deba@1979
  1162
  };
deba@1979
  1163
deba@2087
  1164
  /// \brief Just gives back a DirUGraphAdaptor
deba@2087
  1165
  ///
deba@2087
  1166
  /// Just gives back a DirUGraphAdaptor
deba@1979
  1167
  template<typename UGraph, typename DirectionMap>
deba@1980
  1168
  DirUGraphAdaptor<const UGraph, DirectionMap>
deba@1980
  1169
  dirUGraphAdaptor(const UGraph& graph, DirectionMap& dm) {
deba@1980
  1170
    return DirUGraphAdaptor<const UGraph, DirectionMap>(graph, dm);
deba@1979
  1171
  }
deba@1979
  1172
deba@1979
  1173
  template<typename UGraph, typename DirectionMap>
deba@1980
  1174
  DirUGraphAdaptor<const UGraph, const DirectionMap>
deba@1980
  1175
  dirUGraphAdaptor(const UGraph& graph, const DirectionMap& dm) {
deba@1980
  1176
    return DirUGraphAdaptor<const UGraph, const DirectionMap>(graph, dm);
deba@1979
  1177
  }
deba@1979
  1178
deba@1979
  1179
}
deba@1979
  1180
deba@1979
  1181
#endif