src/work/marci/leda/leda_graph_wrapper.h
author marci
Mon, 10 May 2004 16:31:48 +0000
changeset 597 a6e2b02f496a
parent 473 2cef25dcde3f
child 616 31879aac4dc3
permissions -rw-r--r--
bfs, dfs docs
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@189
    17
#include <invalid.h>
marci@189
    18
marci@189
    19
/// The namespace of HugoLib
marci@189
    20
namespace hugo {
marci@189
    21
marci@189
    22
  // @defgroup empty_graph The LedaGraphWrapper class
marci@189
    23
  // @{
marci@189
    24
marci@409
    25
  /// A graph wrapperstructure for wrapping LEDA graphs in HUGO.
marci@189
    26
  
marci@409
    27
  /// This graph wrapper class wraps LEDA graph and LEDA parametrized graph
marci@409
    28
  /// and then the generic algorithms and wrappers of HUGO can be used 
marci@409
    29
  /// with LEDA graphs. 
marci@189
    30
  /// This class provides all the common features of a grapf structure,
marci@189
    31
  /// however completely without implementations or real data structures
marci@189
    32
  /// behind the interface.
marci@189
    33
  /// All graph algorithms should compile with this class, but it will not
marci@189
    34
  /// run properly, of course.
marci@189
    35
  ///
marci@189
    36
  /// It can be used for checking the interface compatibility,
marci@189
    37
  /// or it can serve as a skeleton of a new graph structure.
marci@189
    38
  /// 
marci@189
    39
  /// Also, you will find here the full documentation of a certain graph
marci@189
    40
  /// feature, the documentation of a real graph imlementation
marci@189
    41
  /// like @ref ListGraph or
marci@189
    42
  /// @ref SmartGraph will just refer to this structure.
marci@189
    43
  template<typename Graph>
marci@189
    44
  class LedaGraphWrapper
marci@189
    45
  {
marci@473
    46
  protected:
marci@189
    47
    Graph* _graph;
marci@473
    48
    LedaGraphWrapper() : _graph(0) { }
marci@482
    49
    void setGraph(Graph& __graph) { _graph=&__graph; }
marci@189
    50
  public:
marci@189
    51
   
marci@189
    52
        //LedaGraphWrapper() { }
marci@189
    53
    LedaGraphWrapper(Graph& __graph) : _graph(&__graph) { }
marci@189
    54
    LedaGraphWrapper(const LedaGraphWrapper &G) : _graph(G._graph) { }
marci@189
    55
marci@189
    56
    template <typename T> class NodeMap;
marci@189
    57
    template <typename T> class EdgeMap;
marci@189
    58
marci@461
    59
    class Node;
marci@461
    60
    class NodeIt;
marci@461
    61
    class Edge;
marci@461
    62
    class EdgeIt;
marci@461
    63
    class OutEdgeIt;
marci@461
    64
    class InEdgeIt;
marci@461
    65
marci@189
    66
    /// The base type of the node iterators.
marci@189
    67
    class Node {
marci@461
    68
      friend class LedaGraphWrapper<Graph>;
marci@189
    69
      //friend class Edge;
marci@189
    70
      friend class EdgeIt;
marci@189
    71
      friend class InEdgeIt;
marci@189
    72
      friend class OutEdgeIt;
marci@189
    73
    protected:
marci@189
    74
      template <typename T> friend class NodeMap;
marci@189
    75
      leda_node _n;
marci@446
    76
    public: //FIXME
marci@189
    77
      Node(leda_node __n) : _n(__n) { } 
marci@189
    78
    public:
marci@189
    79
      /// @warning The default constructor sets the iterator
marci@189
    80
      /// to an undefined value.
marci@189
    81
      Node() {}   //FIXME
marci@189
    82
      /// Initialize the iterator to be invalid
marci@189
    83
      Node(Invalid) : _n(0) { }
marci@189
    84
      //Node(const Node &) {} 
marci@189
    85
      bool operator==(Node n) const { return _n==n._n; } //FIXME
marci@189
    86
      bool operator!=(Node n) const { return _n!=n._n; } //FIXME
marci@198
    87
      operator leda_node () { return _n; }
marci@189
    88
    };
marci@189
    89
    
marci@189
    90
    /// This iterator goes through each node.
marci@189
    91
    class NodeIt : public Node {
marci@189
    92
    public:
marci@189
    93
      /// @warning The default constructor sets the iterator
marci@189
    94
      /// to an undefined value.
marci@189
    95
      NodeIt() {} //FIXME
marci@189
    96
      /// Initialize the iterator to be invalid
marci@189
    97
      NodeIt(Invalid i) : Node(i) {}
marci@189
    98
      /// Sets the iterator to the first node of \c G.
marci@189
    99
      NodeIt(const LedaGraphWrapper &G) : Node(G._graph->first_node()) { }
marci@189
   100
      //NodeIt(const NodeIt &) {} //FIXME
marci@189
   101
    };
marci@189
   102
    
marci@189
   103
    /// The base type of the edge iterators.
marci@189
   104
    class Edge {
marci@189
   105
      friend class LedaGraphWrapper;
marci@189
   106
    protected:
marci@189
   107
      template <typename T> friend class EdgeMap;
marci@189
   108
      leda_edge _e;
marci@446
   109
    public: //FIXME
marci@189
   110
      Edge(leda_edge __e) : _e(__e) { } 
marci@189
   111
    public:
marci@189
   112
      /// @warning The default constructor sets the iterator
marci@189
   113
      /// to an undefined value.
marci@189
   114
      Edge() {}   //FIXME
marci@189
   115
      /// Initialize the iterator to be invalid
marci@189
   116
      Edge(Invalid) : _e(0) {}
marci@189
   117
      //Edge(const Edge &) {} 
marci@189
   118
      bool operator==(Edge e) const { return _e==e._e; } //FIXME
marci@198
   119
      bool operator!=(Edge e) const { return _e!=e._e; } //FIXME 
marci@198
   120
      operator leda_edge () { return _e; }
marci@189
   121
    };
marci@189
   122
    
marci@189
   123
    /// This iterator goes trought the outgoing edges of a certain graph.
marci@189
   124
    
marci@189
   125
    class OutEdgeIt : public Edge {
marci@189
   126
    public:
marci@189
   127
      /// @warning The default constructor sets the iterator
marci@189
   128
      /// to an undefined value.
marci@189
   129
      OutEdgeIt() {}
marci@189
   130
      /// Initialize the iterator to be invalid
marci@189
   131
      OutEdgeIt(Invalid i) : Edge(i) {}
marci@189
   132
      /// This constructor sets the iterator to first outgoing edge.
marci@189
   133
    
marci@189
   134
      /// This constructor set the iterator to the first outgoing edge of
marci@189
   135
      /// node
marci@189
   136
      ///@param n the node
marci@189
   137
      ///@param G the graph
marci@189
   138
      OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_adj_edge(n._n)) { }
marci@189
   139
    };
marci@189
   140
marci@189
   141
    class InEdgeIt : public Edge {
marci@189
   142
    public:
marci@189
   143
      /// @warning The default constructor sets the iterator
marci@189
   144
      /// to an undefined value.
marci@189
   145
      InEdgeIt() {}
marci@189
   146
      /// Initialize the iterator to be invalid
marci@189
   147
      InEdgeIt(Invalid i) : Edge(i) {}
marci@189
   148
      InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_in_edge(n._n)) { }
marci@189
   149
    };
marci@189
   150
marci@189
   151
    //  class SymEdgeIt : public Edge {};
marci@189
   152
    class EdgeIt : public Edge {
marci@189
   153
    public:
marci@189
   154
      /// @warning The default constructor sets the iterator
marci@189
   155
      /// to an undefined value.
marci@189
   156
      EdgeIt() {}
marci@189
   157
      /// Initialize the iterator to be invalid
marci@189
   158
      EdgeIt(Invalid i) : Edge(i) {}
marci@189
   159
      EdgeIt(const LedaGraphWrapper & G) : Edge(G._graph->first_edge()) { }
marci@189
   160
    };
marci@189
   161
marci@189
   162
    /// First node of the graph.
marci@189
   163
marci@189
   164
    /// \post \c i and the return value will be the first node.
marci@189
   165
    ///
marci@189
   166
    NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
marci@189
   167
marci@189
   168
    /// The first outgoing edge.
marci@189
   169
    InEdgeIt &first(InEdgeIt &i, Node n) const { 
marci@189
   170
      i=InEdgeIt(*this, n); 
marci@189
   171
      return i;
marci@189
   172
    }
marci@189
   173
    /// The first incoming edge.
marci@189
   174
    OutEdgeIt &first(OutEdgeIt &i, Node n) const { 
marci@189
   175
      i=OutEdgeIt(*this, n); 
marci@189
   176
      return i;
marci@189
   177
    }
marci@189
   178
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
marci@189
   179
    /// The first edge of the Graph.
marci@189
   180
    EdgeIt &first(EdgeIt &i) const {      
marci@189
   181
      i=EdgeIt(*this); 
marci@189
   182
      return i; }
marci@189
   183
marci@189
   184
//     Node getNext(Node) const {}
marci@189
   185
//     InEdgeIt getNext(InEdgeIt) const {}
marci@189
   186
//     OutEdgeIt getNext(OutEdgeIt) const {}
marci@189
   187
//     //SymEdgeIt getNext(SymEdgeIt) const {}
marci@189
   188
//     EdgeIt getNext(EdgeIt) const {}
marci@189
   189
marci@189
   190
    /// Go to the next node.
marci@189
   191
    NodeIt &next(NodeIt &i) const { 
marci@189
   192
      i._n=_graph->succ_node(i._n); 
marci@189
   193
      return i; 
marci@189
   194
    }
marci@189
   195
    /// Go to the next incoming edge.
marci@189
   196
    InEdgeIt &next(InEdgeIt &i) const { 
marci@189
   197
      i._e=_graph->in_succ(i._e); 
marci@189
   198
      return i;
marci@189
   199
    }
marci@189
   200
    /// Go to the next outgoing edge.
marci@189
   201
    OutEdgeIt &next(OutEdgeIt &i) const { 
marci@189
   202
      i._e=_graph->adj_succ(i._e); 
marci@189
   203
      return i;
marci@189
   204
    }
marci@189
   205
    //SymEdgeIt &next(SymEdgeIt &) const {}
marci@189
   206
    /// Go to the next edge.
marci@189
   207
    EdgeIt &next(EdgeIt &i) const {      
marci@189
   208
      i._e=_graph->succ_edge(i._e); 
marci@189
   209
      return i; 
marci@189
   210
    }
marci@189
   211
marci@409
   212
//     template< typename It >
marci@409
   213
//     It first() const { 
marci@409
   214
//       It e;
marci@409
   215
//       first(e);
marci@409
   216
//       return e; 
marci@409
   217
//     }
marci@189
   218
marci@409
   219
//     template< typename It >
marci@409
   220
//     It first(Node v) const { 
marci@409
   221
//       It e;
marci@409
   222
//       first(e, v);
marci@409
   223
//       return e; 
marci@409
   224
//     }
marci@189
   225
marci@189
   226
    ///Gives back the head node of an edge.
marci@189
   227
    Node head(Edge e) const { 
marci@189
   228
      return Node(_graph->target(e._e)); 
marci@189
   229
    }
marci@189
   230
    ///Gives back the tail node of an edge.
marci@189
   231
    Node tail(Edge e) const { 
marci@189
   232
      return Node(_graph->source(e._e)); 
marci@189
   233
    }
marci@189
   234
  
marci@189
   235
    Node aNode(InEdgeIt e) const { return head(e); }
marci@189
   236
    Node aNode(OutEdgeIt e) const { return tail(e); }
marci@189
   237
    //   Node aNode(SymEdgeIt) const {}
marci@189
   238
marci@189
   239
    Node bNode(InEdgeIt e) const { return tail(e); }
marci@189
   240
    Node bNode(OutEdgeIt e) const { return head(e); }
marci@189
   241
    //   Node bNode(SymEdgeIt) const {}
marci@189
   242
marci@189
   243
    /// Checks if a node iterator is valid
marci@189
   244
    bool valid(Node n) const { return n._n; }
marci@189
   245
    /// Checks if an edge iterator is valid
marci@189
   246
    bool valid(Edge e) const { return e._e; }
marci@189
   247
marci@189
   248
    ///Gives back the \e id of a node.
marci@189
   249
    int id(Node n) const { return n._n->id(); }
marci@189
   250
    ///Gives back the \e id of an edge.
marci@189
   251
    int id(Edge e) const { return e._e->id(); }
marci@189
   252
marci@189
   253
    //void setInvalid(Node &) const {};
marci@189
   254
    //void setInvalid(Edge &) const {};
marci@189
   255
  
marci@189
   256
    Node addNode() const { return Node(_graph->new_node()); }
marci@189
   257
    Edge addEdge(Node tail, Node head) const { 
marci@189
   258
      return Edge(_graph->new_edge(tail._n, head._n));
marci@189
   259
    }
marci@189
   260
    
marci@189
   261
    void erase(Node n) const { _graph->del_node(n._n); }
marci@189
   262
    void erase(Edge e) const { _graph->del_edge(e._e); }
marci@189
   263
marci@189
   264
    void clear() const { _graph->clear(); }
marci@189
   265
marci@189
   266
    int nodeNum() const { return _graph->number_of_nodes(); }
marci@189
   267
    int edgeNum() const { return _graph->number_of_edges(); }
marci@189
   268
marci@189
   269
    ///Read/write map from the nodes to type \c T.
marci@189
   270
    template<typename T> class NodeMap
marci@189
   271
    {
marci@189
   272
      leda_node_map<T> leda_stuff;
marci@189
   273
    public:
marci@189
   274
      typedef T ValueType;
marci@189
   275
      typedef Node KeyType;
marci@189
   276
marci@189
   277
      NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
marci@189
   278
      NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
marci@189
   279
marci@189
   280
      void set(Node i, T t) { leda_stuff[i._n]=t; }
marci@189
   281
      T get(Node i) const { return leda_stuff[i._n]; }  //FIXME: Is it necessary
marci@189
   282
      T &operator[](Node i) { return leda_stuff[i._n]; }
marci@189
   283
      const T &operator[](Node i) const { return leda_stuff[i._n]; }
marci@189
   284
marci@189
   285
      void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
marci@189
   286
      //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); }   //FIXME: Is it necessary
marci@189
   287
    };
marci@189
   288
marci@189
   289
    ///Read/write map from the edges to type \c T.
marci@189
   290
    template<typename T> class EdgeMap
marci@189
   291
    {
marci@189
   292
      leda_edge_map<T> leda_stuff;
marci@189
   293
    public:
marci@189
   294
      typedef T ValueType;
marci@189
   295
      typedef Edge KeyType;
marci@189
   296
marci@189
   297
      EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
marci@189
   298
      EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
marci@189
   299
marci@189
   300
      void set(Edge i, T t) { leda_stuff[i._e]=t; }
marci@189
   301
      T get(Edge i) const { return leda_stuff[i._e]; }  //FIXME: Is it necessary
marci@189
   302
      T &operator[](Edge i) { return leda_stuff[i._e]; }
marci@189
   303
      const T &operator[](Edge i) const { return leda_stuff[i._e]; }
marci@189
   304
marci@189
   305
      void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
marci@189
   306
      //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); }   //FIXME: Is it necessary
marci@189
   307
    };
marci@189
   308
marci@189
   309
  };
marci@189
   310
marci@473
   311
  template<typename Graph>
marci@473
   312
  class LedaGraph : public LedaGraphWrapper<Graph> {
marci@473
   313
    typedef LedaGraphWrapper<Graph> Parent;
marci@473
   314
  protected:
marci@473
   315
    Graph gr;
marci@473
   316
  public:
marci@473
   317
    LedaGraph() { 
marci@473
   318
      Parent::setGraph(gr); 
marci@473
   319
    }
marci@473
   320
  };
marci@473
   321
marci@189
   322
  // @}
marci@189
   323
marci@189
   324
} //namespace hugo
marci@189
   325
marci@189
   326
marci@189
   327
marci@189
   328
// class EmptyBipGraph : public EmptyGraph
marci@189
   329
// {
marci@189
   330
//   class ANode {};
marci@189
   331
//   class BNode {};
marci@189
   332
marci@189
   333
//   ANode &next(ANode &) {}
marci@189
   334
//   BNode &next(BNode &) {}
marci@189
   335
marci@189
   336
//   ANode &getFirst(ANode &) const {}
marci@189
   337
//   BNode &getFirst(BNode &) const {}
marci@189
   338
marci@189
   339
//   enum NodeClass { A = 0, B = 1 };
marci@189
   340
//   NodeClass getClass(Node n) {}
marci@189
   341
marci@189
   342
// }
marci@189
   343
marci@189
   344
#endif // HUGO_LEDA_GRAPH_WRAPPER_H