COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/leda/leda_graph_wrapper.h @ 481:54d8feda437b

Last change on this file since 481:54d8feda437b was 473:2cef25dcde3f, checked in by marci, 17 years ago

ledagraph

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