src/work/marci/leda/leda_graph_wrapper.h
author ladanyi
Thu, 16 Sep 2004 19:51:28 +0000
changeset 875 fda944f15ca7
parent 646 bd7a69231cf8
child 921 818510fa3d99
permissions -rw-r--r--
Changed to conform to the new iterator style.
marci@189
     1
// -*- c++ -*-
marci@189
     2
#ifndef HUGO_LEDA_GRAPH_WRAPPER_H
marci@189
     3
#define HUGO_LEDA_GRAPH_WRAPPER_H
marci@189
     4
marci@189
     5
#include <LEDA/graph.h>
marci@189
     6
#include <LEDA/node_array.h>
marci@189
     7
#include <LEDA/edge_array.h>
marci@189
     8
#include <LEDA/node_map.h>
marci@189
     9
#include <LEDA/edge_map.h>
marci@189
    10
//#include <LEDA/graph_alg.h>
marci@189
    11
//#include <LEDA/dimacs.h>
marci@189
    12
marci@189
    13
//#if defined(LEDA_NAMESPACE)
marci@189
    14
//using namespace leda;
marci@189
    15
//#endif
marci@189
    16
marci@616
    17
#include <hugo/invalid.h>
marci@189
    18
marci@189
    19
namespace hugo {
marci@189
    20
marci@617
    21
  /// \brief A graph wrapper structure for wrapping LEDA graphs in HUGO.
marci@617
    22
  ///
marci@617
    23
  /// This graph wrapper class wraps LEDA graphs and LEDA parametrized graphs
marci@617
    24
  /// to satisfy HUGO graph concepts.
marci@617
    25
  /// Then the generic HUGOlib algorithms and wrappers can be used 
marci@409
    26
  /// with LEDA graphs. 
marci@617
    27
  /// \ingroup gwrapper
marci@189
    28
  template<typename Graph>
marci@189
    29
  class LedaGraphWrapper
marci@189
    30
  {
marci@473
    31
  protected:
marci@617
    32
    Graph* l_graph;
marci@617
    33
    LedaGraphWrapper() : l_graph(0) { }
marci@617
    34
    void setGraph(Graph& _l_graph) { l_graph=&_l_graph; }
marci@189
    35
  public:
marci@650
    36
marci@650
    37
    /// Constructor for wrapping LEDA graphs.
marci@617
    38
    LedaGraphWrapper(Graph& _l_graph) : l_graph(&_l_graph) { }
marci@650
    39
    /// Copy constructor
marci@650
    40
    LedaGraphWrapper(const LedaGraphWrapper &g) : l_graph(g.l_graph) { }
marci@189
    41
marci@189
    42
    template <typename T> class NodeMap;
marci@189
    43
    template <typename T> class EdgeMap;
marci@646
    44
    template <typename T> class NodeMapWrapper;
marci@646
    45
    template <typename T> class EdgeMapWrapper;
marci@189
    46
marci@461
    47
    class Node;
marci@461
    48
    class NodeIt;
marci@461
    49
    class Edge;
marci@461
    50
    class EdgeIt;
marci@461
    51
    class OutEdgeIt;
marci@461
    52
    class InEdgeIt;
marci@461
    53
marci@650
    54
    /// Trivial node-iterator
marci@189
    55
    class Node {
marci@461
    56
      friend class LedaGraphWrapper<Graph>;
marci@189
    57
      //friend class Edge;
marci@189
    58
      friend class EdgeIt;
marci@189
    59
      friend class InEdgeIt;
marci@189
    60
      friend class OutEdgeIt;
marci@189
    61
    protected:
marci@189
    62
      template <typename T> friend class NodeMap;
marci@617
    63
      leda_node l_n;
marci@446
    64
    public: //FIXME
marci@617
    65
      Node(leda_node _l_n) : l_n(_l_n) { } 
marci@189
    66
    public:
marci@189
    67
      /// @warning The default constructor sets the iterator
marci@189
    68
      /// to an undefined value.
marci@650
    69
      Node() { }   //FIXME
marci@189
    70
      /// Initialize the iterator to be invalid
marci@617
    71
      Node(Invalid) : l_n(0) { }
marci@189
    72
      //Node(const Node &) {} 
marci@617
    73
      bool operator==(Node n) const { return l_n==n.l_n; } //FIXME
marci@617
    74
      bool operator!=(Node n) const { return l_n!=n.l_n; } //FIXME
marci@617
    75
      operator leda_node () { return l_n; }
marci@189
    76
    };
marci@189
    77
    
marci@189
    78
    /// This iterator goes through each node.
marci@189
    79
    class NodeIt : public Node {
marci@189
    80
    public:
marci@189
    81
      /// @warning The default constructor sets the iterator
marci@189
    82
      /// to an undefined value.
marci@650
    83
      NodeIt() { } //FIXME
marci@189
    84
      /// Initialize the iterator to be invalid
marci@650
    85
      NodeIt(Invalid i) : Node(i) { }
marci@189
    86
      /// Sets the iterator to the first node of \c G.
marci@617
    87
      NodeIt(const LedaGraphWrapper &G) : Node(G.l_graph->first_node()) { }
marci@189
    88
      //NodeIt(const NodeIt &) {} //FIXME
marci@189
    89
    };
marci@189
    90
    
marci@650
    91
    /// Trivial edge-iterator.
marci@189
    92
    class Edge {
marci@189
    93
      friend class LedaGraphWrapper;
marci@189
    94
    protected:
marci@189
    95
      template <typename T> friend class EdgeMap;
marci@617
    96
      leda_edge l_e;
marci@446
    97
    public: //FIXME
marci@617
    98
      Edge(leda_edge _l_e) : l_e(_l_e) { } 
marci@189
    99
    public:
marci@189
   100
      /// @warning The default constructor sets the iterator
marci@189
   101
      /// to an undefined value.
marci@650
   102
      Edge() { }   //FIXME
marci@189
   103
      /// Initialize the iterator to be invalid
marci@650
   104
      Edge(Invalid) : l_e(0) { }
marci@189
   105
      //Edge(const Edge &) {} 
marci@617
   106
      bool operator==(Edge e) const { return l_e==e.l_e; } //FIXME
marci@617
   107
      bool operator!=(Edge e) const { return l_e!=e.l_e; } //FIXME 
marci@617
   108
      operator leda_edge () { return l_e; }
marci@189
   109
    };
marci@189
   110
    
marci@650
   111
    /// This iterator goes trought the outgoing edges of a certain node.
marci@189
   112
    class OutEdgeIt : public Edge {
marci@189
   113
    public:
marci@189
   114
      /// @warning The default constructor sets the iterator
marci@189
   115
      /// to an undefined value.
marci@650
   116
      OutEdgeIt() { }
marci@189
   117
      /// Initialize the iterator to be invalid
marci@650
   118
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@189
   119
      /// This constructor sets the iterator to first outgoing edge.
marci@189
   120
    
marci@189
   121
      /// This constructor set the iterator to the first outgoing edge of
marci@189
   122
      /// node
marci@189
   123
      ///@param n the node
marci@189
   124
      ///@param G the graph
marci@617
   125
      OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_adj_edge(n.l_n)) { }
marci@189
   126
    };
marci@189
   127
marci@650
   128
    /// This iterator goes trought the incoming edges of a certain node.
marci@189
   129
    class InEdgeIt : public Edge {
marci@189
   130
    public:
marci@189
   131
      /// @warning The default constructor sets the iterator
marci@189
   132
      /// to an undefined value.
marci@650
   133
      InEdgeIt() { }
marci@189
   134
      /// Initialize the iterator to be invalid
marci@650
   135
      InEdgeIt(Invalid i) : Edge(i) { }
marci@617
   136
      InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_in_edge(n.l_n)) { }
marci@189
   137
    };
marci@189
   138
marci@189
   139
    //  class SymEdgeIt : public Edge {};
marci@650
   140
    
marci@650
   141
    /// This iterator goes trought the edges of the graph.
marci@189
   142
    class EdgeIt : public Edge {
marci@189
   143
    public:
marci@189
   144
      /// @warning The default constructor sets the iterator
marci@189
   145
      /// to an undefined value.
marci@650
   146
      EdgeIt() { }
marci@189
   147
      /// Initialize the iterator to be invalid
marci@650
   148
      EdgeIt(Invalid i) : Edge(i) { }
marci@617
   149
      EdgeIt(const LedaGraphWrapper & G) : Edge(G.l_graph->first_edge()) { }
marci@189
   150
    };
marci@189
   151
marci@189
   152
    /// First node of the graph.
marci@650
   153
    ///
marci@189
   154
    /// \post \c i and the return value will be the first node.
marci@189
   155
    ///
marci@189
   156
    NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
marci@189
   157
marci@189
   158
    /// The first outgoing edge.
marci@189
   159
    InEdgeIt &first(InEdgeIt &i, Node n) const { 
marci@189
   160
      i=InEdgeIt(*this, n); 
marci@189
   161
      return i;
marci@189
   162
    }
marci@189
   163
    /// The first incoming edge.
marci@189
   164
    OutEdgeIt &first(OutEdgeIt &i, Node n) const { 
marci@189
   165
      i=OutEdgeIt(*this, n); 
marci@189
   166
      return i;
marci@189
   167
    }
marci@189
   168
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
marci@650
   169
    /// The first edge of the graph.
marci@189
   170
    EdgeIt &first(EdgeIt &i) const {      
marci@189
   171
      i=EdgeIt(*this); 
marci@189
   172
      return i; }
marci@189
   173
marci@189
   174
//     Node getNext(Node) const {}
marci@189
   175
//     InEdgeIt getNext(InEdgeIt) const {}
marci@189
   176
//     OutEdgeIt getNext(OutEdgeIt) const {}
marci@189
   177
//     //SymEdgeIt getNext(SymEdgeIt) const {}
marci@189
   178
//     EdgeIt getNext(EdgeIt) const {}
marci@189
   179
marci@189
   180
    /// Go to the next node.
marci@189
   181
    NodeIt &next(NodeIt &i) const { 
marci@617
   182
      i.l_n=l_graph->succ_node(i.l_n); 
marci@189
   183
      return i; 
marci@189
   184
    }
marci@189
   185
    /// Go to the next incoming edge.
marci@189
   186
    InEdgeIt &next(InEdgeIt &i) const { 
marci@617
   187
      i.l_e=l_graph->in_succ(i.l_e); 
marci@189
   188
      return i;
marci@189
   189
    }
marci@189
   190
    /// Go to the next outgoing edge.
marci@189
   191
    OutEdgeIt &next(OutEdgeIt &i) const { 
marci@617
   192
      i.l_e=l_graph->adj_succ(i.l_e); 
marci@189
   193
      return i;
marci@189
   194
    }
marci@189
   195
    //SymEdgeIt &next(SymEdgeIt &) const {}
marci@189
   196
    /// Go to the next edge.
marci@189
   197
    EdgeIt &next(EdgeIt &i) const {      
marci@617
   198
      i.l_e=l_graph->succ_edge(i.l_e); 
marci@189
   199
      return i; 
marci@189
   200
    }
marci@189
   201
marci@409
   202
//     template< typename It >
marci@409
   203
//     It first() const { 
marci@409
   204
//       It e;
marci@409
   205
//       first(e);
marci@409
   206
//       return e; 
marci@409
   207
//     }
marci@189
   208
marci@409
   209
//     template< typename It >
marci@409
   210
//     It first(Node v) const { 
marci@409
   211
//       It e;
marci@409
   212
//       first(e, v);
marci@409
   213
//       return e; 
marci@409
   214
//     }
marci@189
   215
marci@189
   216
    ///Gives back the head node of an edge.
marci@189
   217
    Node head(Edge e) const { 
marci@617
   218
      return Node(l_graph->target(e.l_e)); 
marci@189
   219
    }
marci@189
   220
    ///Gives back the tail node of an edge.
marci@189
   221
    Node tail(Edge e) const { 
marci@617
   222
      return Node(l_graph->source(e.l_e)); 
marci@189
   223
    }
marci@189
   224
  
marci@189
   225
    Node aNode(InEdgeIt e) const { return head(e); }
marci@189
   226
    Node aNode(OutEdgeIt e) const { return tail(e); }
marci@189
   227
    //   Node aNode(SymEdgeIt) const {}
marci@189
   228
marci@189
   229
    Node bNode(InEdgeIt e) const { return tail(e); }
marci@189
   230
    Node bNode(OutEdgeIt e) const { return head(e); }
marci@189
   231
    //   Node bNode(SymEdgeIt) const {}
marci@189
   232
marci@189
   233
    /// Checks if a node iterator is valid
marci@617
   234
    bool valid(Node n) const { return n.l_n; }
marci@189
   235
    /// Checks if an edge iterator is valid
marci@617
   236
    bool valid(Edge e) const { return e.l_e; }
marci@189
   237
marci@189
   238
    ///Gives back the \e id of a node.
marci@617
   239
    int id(Node n) const { return n.l_n->id(); }
marci@189
   240
    ///Gives back the \e id of an edge.
marci@617
   241
    int id(Edge e) const { return e.l_e->id(); }
marci@189
   242
marci@189
   243
    //void setInvalid(Node &) const {};
marci@189
   244
    //void setInvalid(Edge &) const {};
marci@189
   245
  
marci@617
   246
    Node addNode() const { return Node(l_graph->new_node()); }
marci@189
   247
    Edge addEdge(Node tail, Node head) const { 
marci@617
   248
      return Edge(l_graph->new_edge(tail.l_n, head.l_n));
marci@189
   249
    }
marci@189
   250
    
marci@617
   251
    void erase(Node n) const { l_graph->del_node(n.l_n); }
marci@617
   252
    void erase(Edge e) const { l_graph->del_edge(e.l_e); }
marci@189
   253
marci@617
   254
    void clear() const { l_graph->clear(); }
marci@189
   255
marci@617
   256
    int nodeNum() const { return l_graph->number_of_nodes(); }
marci@617
   257
    int edgeNum() const { return l_graph->number_of_edges(); }
marci@189
   258
marci@650
   259
    /// Read/write map from the nodes to type \c T.
marci@189
   260
    template<typename T> class NodeMap
marci@189
   261
    {
marci@189
   262
      leda_node_map<T> leda_stuff;
marci@189
   263
    public:
marci@189
   264
      typedef T ValueType;
marci@189
   265
      typedef Node KeyType;
marci@189
   266
marci@617
   267
      NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
marci@617
   268
      NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
marci@189
   269
marci@617
   270
      void set(Node i, T t) { leda_stuff[i.l_n]=t; }
marci@617
   271
      T get(Node i) const { return leda_stuff[i.l_n]; }  //FIXME: Is it necessary
marci@617
   272
      T &operator[](Node i) { return leda_stuff[i.l_n]; }
marci@617
   273
      const T &operator[](Node i) const { return leda_stuff[i.l_n]; }
marci@189
   274
marci@189
   275
      void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
marci@617
   276
      //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); }   //FIXME: Is it necessary
marci@189
   277
    };
marci@189
   278
marci@650
   279
    /// Read/write map from the edges to type \c T.
marci@189
   280
    template<typename T> class EdgeMap
marci@189
   281
    {
marci@189
   282
      leda_edge_map<T> leda_stuff;
marci@189
   283
    public:
marci@189
   284
      typedef T ValueType;
marci@189
   285
      typedef Edge KeyType;
marci@189
   286
marci@617
   287
      EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
marci@617
   288
      EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
marci@189
   289
marci@617
   290
      void set(Edge i, T t) { leda_stuff[i.l_e]=t; }
marci@617
   291
      T get(Edge i) const { return leda_stuff[i.l_e]; }  //FIXME: Is it necessary
marci@617
   292
      T &operator[](Edge i) { return leda_stuff[i.l_e]; }
marci@617
   293
      const T &operator[](Edge i) const { return leda_stuff[i.l_e]; }
marci@189
   294
marci@189
   295
      void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
marci@617
   296
      //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); }   //FIXME: Is it necessary
marci@189
   297
    };
marci@189
   298
marci@646
   299
marci@650
   300
    /// This class is to wrap existing 
marci@650
   301
    /// LEDA node-maps to HUGO ones.
marci@646
   302
    template<typename T> class NodeMapWrapper
marci@646
   303
    {
marci@650
   304
      leda_node_array<T>* leda_stuff;
marci@646
   305
    public:
marci@646
   306
      typedef T ValueType;
marci@646
   307
      typedef Node KeyType;
marci@646
   308
marci@650
   309
      NodeMapWrapper(leda_node_array<T>& _leda_stuff) : 
marci@646
   310
	leda_stuff(&_leda_stuff) { }
marci@646
   311
      //NodeMap(leda_node_map& &G, T t) : leda_stuff(*(G.l_graph), t) {}
marci@646
   312
marci@646
   313
      void set(Node i, T t) { (*leda_stuff)[i.l_n]=t; }
marci@650
   314
      //T get(Node i) const { return (*leda_stuff)[i.l_n]; }  //FIXME: Is it necessary
marci@646
   315
      T &operator[](Node i) { return (*leda_stuff)[i.l_n]; }
marci@646
   316
      const T &operator[](Node i) const { return (*leda_stuff)[i.l_n]; }
marci@646
   317
marci@646
   318
      void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
marci@646
   319
      //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); }   //FIXME: Is it necessary
marci@646
   320
    };
marci@646
   321
marci@650
   322
    /// This class is to wrap existing 
marci@650
   323
    /// LEDA edge-maps to HUGO ones.
marci@646
   324
    template<typename T> class EdgeMapWrapper
marci@646
   325
    {
marci@650
   326
      leda_edge_array<T>* leda_stuff;
marci@646
   327
    public:
marci@646
   328
      typedef T ValueType;
marci@646
   329
      typedef Edge KeyType;
marci@646
   330
marci@650
   331
      EdgeMapWrapper(leda_edge_array<T>& _leda_stuff) : 
marci@646
   332
	leda_stuff(_leda_stuff) { }
marci@646
   333
      //EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
marci@646
   334
marci@646
   335
      void set(Edge i, T t) { (*leda_stuff)[i.l_e]=t; }
marci@650
   336
      //T get(Edge i) const { return (*leda_stuff)[i.l_e]; }  //FIXME: Is it necessary
marci@646
   337
      T &operator[](Edge i) { return (*leda_stuff)[i.l_e]; }
marci@646
   338
      const T &operator[](Edge i) const { return (*leda_stuff)[i.l_e]; }
marci@646
   339
marci@646
   340
      void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
marci@646
   341
      //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); }   //FIXME: Is it necessary
marci@646
   342
    };
marci@646
   343
marci@650
   344
    /// This class is used for access node-data of 
marci@650
   345
    /// LEDA parametrized graphs.
marci@650
   346
    template<typename T> 
marci@650
   347
    class NodeDataMap : public NodeMapWrapper<T>
marci@650
   348
    {
marci@650
   349
    public:
marci@650
   350
      NodeDataMap(LedaGraphWrapper<Graph>& LGW) : 
marci@650
   351
	NodeMapWrapper<T>(*(LGW._g_graph).node_data()) { }
marci@650
   352
    };
marci@650
   353
marci@650
   354
    /// This class is used for access edge-data of 
marci@650
   355
    /// LEDA parametrized graphs.
marci@650
   356
    template<typename T> 
marci@650
   357
    class EdgeDataMap : public EdgeMapWrapper<T>
marci@650
   358
    {
marci@650
   359
    public:
marci@650
   360
      EdgeDataMap(LedaGraphWrapper<Graph>& LGW) : 
marci@650
   361
	EdgeMapWrapper<T>(*(LGW._g_graph).edge_data()) { }
marci@650
   362
    };
marci@650
   363
marci@189
   364
  };
marci@189
   365
marci@617
   366
marci@617
   367
  /// \brief LEDA graph template.
marci@617
   368
  ///
marci@617
   369
  /// This graph stucture uses LEDA graphs for physical storage.
marci@617
   370
  /// \ingroup graphs
marci@473
   371
  template<typename Graph>
marci@473
   372
  class LedaGraph : public LedaGraphWrapper<Graph> {
marci@473
   373
    typedef LedaGraphWrapper<Graph> Parent;
marci@473
   374
  protected:
marci@473
   375
    Graph gr;
marci@473
   376
  public:
marci@473
   377
    LedaGraph() { 
marci@473
   378
      Parent::setGraph(gr); 
marci@473
   379
    }
marci@473
   380
  };
marci@473
   381
marci@189
   382
} //namespace hugo
marci@189
   383
marci@189
   384
#endif // HUGO_LEDA_GRAPH_WRAPPER_H