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