lemon/bpugraph_adaptor.h
author deba
Mon, 03 Apr 2006 16:03:37 +0000
changeset 2032 18c08f9129e4
child 2040 c7bd55c0d820
permissions -rw-r--r--
Writeable extension of some maps
deba@2031
     1
/* -*- C++ -*-
deba@2031
     2
 *
deba@2031
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2031
     4
 *
deba@2031
     5
 * Copyright (C) 2003-2006
deba@2031
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2031
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2031
     8
 *
deba@2031
     9
 * Permission to use, modify and distribute this software is granted
deba@2031
    10
 * provided that this copyright notice appears in all copies. For
deba@2031
    11
 * precise terms see the accompanying LICENSE file.
deba@2031
    12
 *
deba@2031
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2031
    14
 * express or implied, and with no claim as to its suitability for any
deba@2031
    15
 * purpose.
deba@2031
    16
 *
deba@2031
    17
 */
deba@2031
    18
deba@2031
    19
#ifndef LEMON_BPUGRAPH_ADAPTOR_H
deba@2031
    20
#define LEMON_BPUGRAPH_ADAPTOR_H
deba@2031
    21
deba@2031
    22
#include <lemon/bits/invalid.h>
deba@2031
    23
#include <lemon/maps.h>
deba@2031
    24
deba@2031
    25
#include <lemon/bits/graph_adaptor_extender.h>
deba@2031
    26
deba@2031
    27
#include <lemon/bits/traits.h>
deba@2031
    28
deba@2031
    29
#include <iostream>
deba@2031
    30
deba@2031
    31
///\ingroup graph_adaptors
deba@2031
    32
///\file
deba@2031
    33
///\brief Several graph adaptors.
deba@2031
    34
///
deba@2031
    35
///This file contains several useful bpugraph adaptor functions.
deba@2031
    36
///
deba@2031
    37
///\author Balazs Dezso
deba@2031
    38
deba@2031
    39
namespace lemon {
deba@2031
    40
deba@2031
    41
  /// \ingroup graph_adaptors
deba@2031
    42
  ///
deba@2031
    43
  /// \brief Base type for the Bipartite Undirected Graph Adaptors
deba@2031
    44
  ///
deba@2031
    45
  /// This is the base type for most of LEMON bpugraph adaptors. 
deba@2031
    46
  /// This class implements a trivial graph adaptor i.e. it only wraps the 
deba@2031
    47
  /// functions and types of the graph. The purpose of this class is to 
deba@2031
    48
  /// make easier implementing graph adaptors. E.g. if an adaptor is 
deba@2031
    49
  /// considered which differs from the wrapped graph only in some of its 
deba@2031
    50
  /// functions or types, then it can be derived from BpUGraphAdaptor, and 
deba@2031
    51
  /// only the differences should be implemented.
deba@2031
    52
  ///
deba@2031
    53
  /// \author Balazs Dezso 
deba@2031
    54
  template<typename _BpUGraph>
deba@2031
    55
  class BpUGraphAdaptorBase {
deba@2031
    56
  public:
deba@2031
    57
    typedef _BpUGraph Graph;
deba@2031
    58
    typedef Graph ParentGraph;
deba@2031
    59
deba@2031
    60
  protected:
deba@2031
    61
    Graph* graph;
deba@2031
    62
deba@2031
    63
    BpUGraphAdaptorBase() : graph(0) {}
deba@2031
    64
deba@2031
    65
    void setGraph(Graph& _graph) { graph = &_graph; }
deba@2031
    66
deba@2031
    67
    Graph& getGraph() { return *graph; }
deba@2031
    68
    const Graph& getGraph() const { return *graph; }
deba@2031
    69
deba@2031
    70
  public:
deba@2031
    71
deba@2031
    72
    BpUGraphAdaptorBase(Graph& _graph) : graph(&_graph) {}
deba@2031
    73
 
deba@2031
    74
    typedef typename Graph::Node Node;
deba@2031
    75
    typedef typename Graph::ANode ANode;
deba@2031
    76
    typedef typename Graph::BNode BNode;
deba@2031
    77
    typedef typename Graph::Edge Edge;
deba@2031
    78
    typedef typename Graph::UEdge UEdge;
deba@2031
    79
   
deba@2031
    80
    void first(Node& i) const { graph->first(i); }
deba@2031
    81
    void firstANode(Node& i) const { graph->firstANode(i); }
deba@2031
    82
    void firstBNode(Node& i) const { graph->firstBNode(i); }
deba@2031
    83
    void first(Edge& i) const { graph->first(i); }
deba@2031
    84
    void first(UEdge& i) const { graph->first(i); }
deba@2031
    85
    void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
deba@2031
    86
    void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
deba@2031
    87
    void firstInc(UEdge &i, bool &d, const Node &n) const {
deba@2031
    88
      graph->firstInc(i, d, n);
deba@2031
    89
    }
deba@2031
    90
deba@2031
    91
    void next(Node& i) const { graph->next(i); }
deba@2031
    92
    void nextANode(Node& i) const { graph->nextANode(i); }
deba@2031
    93
    void nextBNode(Node& i) const { graph->nextBNode(i); }
deba@2031
    94
    void next(Edge& i) const { graph->next(i); }
deba@2031
    95
    void next(UEdge& i) const { graph->next(i); }
deba@2031
    96
    void nextIn(Edge& i) const { graph->nextIn(i); }
deba@2031
    97
    void nextOut(Edge& i) const { graph->nextOut(i); }
deba@2031
    98
    void nextInc(UEdge &i, bool &d) const { graph->nextInc(i, d); }
deba@2031
    99
deba@2031
   100
    Node source(const UEdge& e) const { return graph->source(e); }
deba@2031
   101
    Node target(const UEdge& e) const { return graph->target(e); }
deba@2031
   102
deba@2031
   103
    Node source(const Edge& e) const { return graph->source(e); }
deba@2031
   104
    Node target(const Edge& e) const { return graph->target(e); }
deba@2031
   105
deba@2031
   106
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
deba@2031
   107
    int nodeNum() const { return graph->nodeNum(); }
deba@2031
   108
    int aNodeNum() const { return graph->aNodeNum(); }
deba@2031
   109
    int bNodeNum() const { return graph->bNodeNum(); }
deba@2031
   110
    
deba@2031
   111
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
deba@2031
   112
    int edgeNum() const { return graph->edgeNum(); }
deba@2031
   113
    int uEdgeNum() const { return graph->uEdgeNum(); }
deba@2031
   114
deba@2031
   115
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@2031
   116
    Edge findEdge(const Node& source, const Node& target, 
deba@2031
   117
		  const Edge& prev = INVALID) {
deba@2031
   118
      return graph->findEdge(source, target, prev);
deba@2031
   119
    }
deba@2031
   120
    UEdge findUEdge(const Node& source, const Node& target, 
deba@2031
   121
                    const UEdge& prev = INVALID) {
deba@2031
   122
      return graph->findUEdge(source, target, prev);
deba@2031
   123
    }
deba@2031
   124
  
deba@2031
   125
    Node addNode() const { return graph->addNode(); }
deba@2031
   126
    UEdge addEdge(const Node& source, const Node& target) const { 
deba@2031
   127
      return graph->addEdge(source, target); 
deba@2031
   128
    }
deba@2031
   129
deba@2031
   130
    void erase(const Node& i) const { graph->erase(i); }
deba@2031
   131
    void erase(const UEdge& i) const { graph->erase(i); }
deba@2031
   132
  
deba@2031
   133
    void clear() const { graph->clear(); }
deba@2031
   134
deba@2031
   135
    bool direction(const Edge& e) const { return graph->direction(e); }
deba@2031
   136
    Edge direct(const UEdge& e, bool d) const { return graph->direct(e, d); }
deba@2031
   137
    
deba@2031
   138
    int id(const Node& v) const { return graph->id(v); }
deba@2031
   139
    int id(const ANode& v) const { return graph->id(v); }
deba@2031
   140
    int id(const BNode& v) const { return graph->id(v); }
deba@2031
   141
    int id(const Edge& e) const { return graph->id(e); }
deba@2031
   142
    int id(const UEdge& e) const { return graph->id(e); }
deba@2031
   143
deba@2031
   144
    Node fromNodeId(int id) const { return graph->fromNodeId(id); }
deba@2031
   145
    ANode fromANodeId(int id) const { return graph->fromANodeId(id); }
deba@2031
   146
    BNode fromBNodeId(int id) const { return graph->fromBNodeId(id); }
deba@2031
   147
    Edge fromEdgeId(int id) const { return graph->fromEdgeId(id); }
deba@2031
   148
    UEdge fromUEdgeId(int id) const { return graph->fromUEdgeId(id); }
deba@2031
   149
deba@2031
   150
    int maxNodeId() const { return graph->maxNodeId(); }
deba@2031
   151
    int maxANodeId() const { return graph->maxANodeId(); }
deba@2031
   152
    int maxBNodeId() const { return graph->maxBNodeId(); }
deba@2031
   153
    int maxEdgeId() const { return graph->maxEdgeId(); }
deba@2031
   154
    int maxUEdgeId() const { return graph->maxEdgeId(); }
deba@2031
   155
deba@2031
   156
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
deba@2031
   157
    NodeNotifier& getNotifier(Node) const {
deba@2031
   158
      return graph->getNotifier(Node()); 
deba@2031
   159
    } 
deba@2031
   160
deba@2031
   161
    typedef typename ItemSetTraits<Graph, ANode>::ItemNotifier ANodeNotifier;
deba@2031
   162
    ANodeNotifier& getNotifier(ANode) const {
deba@2031
   163
      return graph->getNotifier(ANode());
deba@2031
   164
    } 
deba@2031
   165
deba@2031
   166
    typedef typename ItemSetTraits<Graph, BNode>::ItemNotifier BNodeNotifier;
deba@2031
   167
    BNodeNotifier& getNotifier(BNode) const {
deba@2031
   168
      return graph->getNotifier(BNode());
deba@2031
   169
    } 
deba@2031
   170
deba@2031
   171
    typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
deba@2031
   172
    EdgeNotifier& getNotifier(Edge) const {
deba@2031
   173
      return graph->getNotifier(Edge());
deba@2031
   174
    } 
deba@2031
   175
deba@2031
   176
    typedef typename ItemSetTraits<Graph, UEdge>::ItemNotifier UEdgeNotifier;
deba@2031
   177
    UEdgeNotifier& getNotifier(UEdge) const {
deba@2031
   178
      return graph->getNotifier(UEdge());
deba@2031
   179
    } 
deba@2031
   180
deba@2031
   181
    template <typename _Value>
deba@2031
   182
    class NodeMap : public Graph::template NodeMap<_Value> {
deba@2031
   183
    public:
deba@2031
   184
      typedef typename Graph::template NodeMap<_Value> Parent;
deba@2031
   185
      explicit NodeMap(const BpUGraphAdaptorBase<Graph>& ga) 
deba@2031
   186
	: Parent(*ga.graph) {}
deba@2031
   187
      NodeMap(const BpUGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@2031
   188
	: Parent(*ga.graph, value) {}
deba@2031
   189
deba@2031
   190
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
   191
	return operator=<NodeMap>(cmap);
deba@2031
   192
      }
deba@2031
   193
deba@2031
   194
      template <typename CMap>
deba@2031
   195
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   196
        Parent::operator=(cmap);
deba@2031
   197
	return *this;
deba@2031
   198
      }
deba@2031
   199
    };
deba@2031
   200
deba@2031
   201
    template <typename _Value>
deba@2031
   202
    class ANodeMap : public Graph::template ANodeMap<_Value> {
deba@2031
   203
    public:
deba@2031
   204
      typedef typename Graph::template ANodeMap<_Value> Parent;
deba@2031
   205
      explicit ANodeMap(const BpUGraphAdaptorBase& ga) 
deba@2031
   206
	: Parent(*ga.graph) {}
deba@2031
   207
      ANodeMap(const BpUGraphAdaptorBase& ga, const _Value& value)
deba@2031
   208
	: Parent(*ga.graph, value) {}
deba@2031
   209
deba@2031
   210
      ANodeMap& operator=(const ANodeMap& cmap) {
deba@2031
   211
	return operator=<ANodeMap>(cmap);
deba@2031
   212
      }
deba@2031
   213
deba@2031
   214
      template <typename CMap>
deba@2031
   215
      ANodeMap& operator=(const CMap& cmap) {
deba@2031
   216
        Parent::operator=(cmap);
deba@2031
   217
	return *this;
deba@2031
   218
      }
deba@2031
   219
    };
deba@2031
   220
deba@2031
   221
    template <typename _Value>
deba@2031
   222
    class BNodeMap : public Graph::template BNodeMap<_Value> {
deba@2031
   223
    public:
deba@2031
   224
      typedef typename Graph::template BNodeMap<_Value> Parent;
deba@2031
   225
      explicit BNodeMap(const BpUGraphAdaptorBase<Graph>& ga) 
deba@2031
   226
	: Parent(*ga.graph) {}
deba@2031
   227
      BNodeMap(const BpUGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@2031
   228
	: Parent(*ga.graph, value) {}
deba@2031
   229
deba@2031
   230
      BNodeMap& operator=(const BNodeMap& cmap) {
deba@2031
   231
	return operator=<BNodeMap>(cmap);
deba@2031
   232
      }
deba@2031
   233
deba@2031
   234
      template <typename CMap>
deba@2031
   235
      BNodeMap& operator=(const CMap& cmap) {
deba@2031
   236
        Parent::operator=(cmap);
deba@2031
   237
	return *this;
deba@2031
   238
      }
deba@2031
   239
    };
deba@2031
   240
deba@2031
   241
    template <typename _Value>
deba@2031
   242
    class EdgeMap : public Graph::template EdgeMap<_Value> {
deba@2031
   243
    public:
deba@2031
   244
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@2031
   245
      explicit EdgeMap(const BpUGraphAdaptorBase<Graph>& ga) 
deba@2031
   246
	: Parent(*ga.graph) {}
deba@2031
   247
      EdgeMap(const BpUGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@2031
   248
	: Parent(*ga.graph, value) {}
deba@2031
   249
deba@2031
   250
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
   251
	return operator=<EdgeMap>(cmap);
deba@2031
   252
      }
deba@2031
   253
deba@2031
   254
      template <typename CMap>
deba@2031
   255
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   256
        Parent::operator=(cmap);
deba@2031
   257
	return *this;
deba@2031
   258
      }
deba@2031
   259
    };
deba@2031
   260
deba@2031
   261
    template <typename _Value>
deba@2031
   262
    class UEdgeMap : public Graph::template UEdgeMap<_Value> {
deba@2031
   263
    public:
deba@2031
   264
      typedef typename Graph::template UEdgeMap<_Value> Parent;
deba@2031
   265
      explicit UEdgeMap(const BpUGraphAdaptorBase<Graph>& ga) 
deba@2031
   266
	: Parent(*ga.graph) {}
deba@2031
   267
      UEdgeMap(const BpUGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@2031
   268
	: Parent(*ga.graph, value) {}
deba@2031
   269
deba@2031
   270
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2031
   271
	return operator=<UEdgeMap>(cmap);
deba@2031
   272
      }
deba@2031
   273
deba@2031
   274
      template <typename CMap>
deba@2031
   275
      UEdgeMap& operator=(const CMap& cmap) {
deba@2031
   276
        Parent::operator=(cmap);
deba@2031
   277
	return *this;
deba@2031
   278
      }
deba@2031
   279
    };
deba@2031
   280
deba@2031
   281
  };
deba@2031
   282
deba@2031
   283
  /// \ingroup graph_adaptors
deba@2031
   284
  template <typename _BpUGraph>
deba@2031
   285
  class BpUGraphAdaptor 
deba@2031
   286
    : public BpUGraphAdaptorExtender< BpUGraphAdaptorBase<_BpUGraph> > { 
deba@2031
   287
  public:
deba@2031
   288
    typedef _BpUGraph Graph;
deba@2031
   289
    typedef BpUGraphAdaptorExtender<BpUGraphAdaptorBase<_BpUGraph> > Parent;
deba@2031
   290
  protected:
deba@2031
   291
    BpUGraphAdaptor() : Parent() {}
deba@2031
   292
deba@2031
   293
  public:
deba@2031
   294
    explicit BpUGraphAdaptor(Graph& _graph) { setGraph(_graph); }
deba@2031
   295
  };
deba@2031
   296
deba@2031
   297
  template <typename _BpUGraph>
deba@2031
   298
  class SwapBpUGraphAdaptorBase : public BpUGraphAdaptorBase<_BpUGraph> {
deba@2031
   299
  public:
deba@2031
   300
    
deba@2031
   301
    typedef _BpUGraph Graph;
deba@2031
   302
    typedef BpUGraphAdaptorBase<_BpUGraph> Parent;
deba@2031
   303
deba@2031
   304
  protected:
deba@2031
   305
    
deba@2031
   306
    SwapBpUGraphAdaptorBase() {}
deba@2031
   307
deba@2031
   308
  public:
deba@2031
   309
deba@2031
   310
    typedef typename Parent::Node Node;
deba@2031
   311
    typedef typename Parent::BNode ANode;
deba@2031
   312
    typedef typename Parent::ANode BNode;
deba@2031
   313
deba@2031
   314
    void firstANode(Node& i) const { Parent::firstBNode(i); }
deba@2031
   315
    void firstBNode(Node& i) const { Parent::firstANode(i); }
deba@2031
   316
deba@2031
   317
    void nextANode(Node& i) const { Parent::nextBNode(i); }
deba@2031
   318
    void nextBNode(Node& i) const { Parent::nextANode(i); }
deba@2031
   319
deba@2031
   320
    int id(const ANode& v) const { return Parent::id(v); }
deba@2031
   321
    int id(const BNode& v) const { return Parent::id(v); }
deba@2031
   322
deba@2031
   323
    ANode fromANodeId(int id) const { return Parent::fromBNodeId(id); }
deba@2031
   324
    BNode fromBNodeId(int id) const { return Parent::fromANodeId(id); }
deba@2031
   325
deba@2031
   326
    int maxANodeId() const { return Parent::maxBNodeId(); }
deba@2031
   327
    int maxBNodeId() const { return Parent::maxANodeId(); }
deba@2031
   328
deba@2031
   329
    int aNodeNum() const { return Parent::bNodeNum(); }
deba@2031
   330
    int bNodeNum() const { return Parent::aNodeNum(); }
deba@2031
   331
deba@2031
   332
    typedef typename Parent::BNodeNotifier ANodeNotifier;
deba@2031
   333
    ANodeNotifier& getNotifier(ANode) const {
deba@2031
   334
      return Parent::getNotifier(typename Parent::BNode());
deba@2031
   335
    } 
deba@2031
   336
deba@2031
   337
    typedef typename Parent::ANodeNotifier BNodeNotifier;
deba@2031
   338
    BNodeNotifier& getNotifier(BNode) const {
deba@2031
   339
      return Parent::getNotifier(typename Parent::ANode());
deba@2031
   340
    } 
deba@2031
   341
deba@2031
   342
    template <typename _Value>
deba@2031
   343
    class ANodeMap : public Graph::template BNodeMap<_Value> {
deba@2031
   344
    public:
deba@2031
   345
      typedef typename Graph::template BNodeMap<_Value> Parent;
deba@2031
   346
      explicit ANodeMap(const SwapBpUGraphAdaptorBase& ga) 
deba@2031
   347
	: Parent(*ga.graph) {}
deba@2031
   348
      ANodeMap(const SwapBpUGraphAdaptorBase& ga, const _Value& value)
deba@2031
   349
	: Parent(*ga.graph, value) {}
deba@2031
   350
deba@2031
   351
      ANodeMap& operator=(const ANodeMap& cmap) {
deba@2031
   352
	return operator=<ANodeMap>(cmap);
deba@2031
   353
      }
deba@2031
   354
deba@2031
   355
      template <typename CMap>
deba@2031
   356
      ANodeMap& operator=(const CMap& cmap) {
deba@2031
   357
        Parent::operator=(cmap);
deba@2031
   358
	return *this;
deba@2031
   359
      }
deba@2031
   360
    };
deba@2031
   361
deba@2031
   362
    template <typename _Value>
deba@2031
   363
    class BNodeMap : public Graph::template ANodeMap<_Value> {
deba@2031
   364
    public:
deba@2031
   365
      typedef typename Graph::template ANodeMap<_Value> Parent;
deba@2031
   366
      explicit BNodeMap(const SwapBpUGraphAdaptorBase<Graph>& ga) 
deba@2031
   367
	: Parent(*ga.graph) {}
deba@2031
   368
      BNodeMap(const SwapBpUGraphAdaptorBase<Graph>& ga, const _Value& value)
deba@2031
   369
	: Parent(*ga.graph, value) {}
deba@2031
   370
deba@2031
   371
      BNodeMap& operator=(const BNodeMap& cmap) {
deba@2031
   372
	return operator=<BNodeMap>(cmap);
deba@2031
   373
      }
deba@2031
   374
deba@2031
   375
      template <typename CMap>
deba@2031
   376
      BNodeMap& operator=(const CMap& cmap) {
deba@2031
   377
        Parent::operator=(cmap);
deba@2031
   378
	return *this;
deba@2031
   379
      }
deba@2031
   380
    };
deba@2031
   381
    
deba@2031
   382
  };
deba@2031
   383
deba@2031
   384
  /// \ingroup graph_adaptors
deba@2031
   385
  ///
deba@2031
   386
  /// \brief Bipartite graph adaptor which swaps the two nodeset.
deba@2031
   387
  ///
deba@2031
   388
  /// Bipartite graph adaptor which swaps the two nodeset. The adaptor's
deba@2031
   389
  /// a-nodeset will be the original graph's b-nodeset and the adaptor's
deba@2031
   390
  /// b-nodeset will be the original graph's a-nodeset. 
deba@2031
   391
  template <typename _BpUGraph>
deba@2031
   392
  class SwapBpUGraphAdaptor 
deba@2031
   393
    : public BpUGraphAdaptorExtender<SwapBpUGraphAdaptorBase<_BpUGraph> > { 
deba@2031
   394
  public:
deba@2031
   395
deba@2031
   396
    typedef _BpUGraph Graph;
deba@2031
   397
    typedef BpUGraphAdaptorExtender<SwapBpUGraphAdaptorBase<_BpUGraph> > 
deba@2031
   398
    Parent;
deba@2031
   399
deba@2031
   400
  protected:
deba@2031
   401
    SwapBpUGraphAdaptor() : Parent() {}
deba@2031
   402
deba@2031
   403
  public:
deba@2031
   404
deba@2031
   405
    explicit SwapBpUGraphAdaptor(Graph& _graph) { setGraph(_graph); }
deba@2031
   406
deba@2031
   407
  };
deba@2031
   408
deba@2031
   409
deba@2031
   410
}
deba@2031
   411
deba@2031
   412
#endif