2 #ifndef HUGO_LEDA_GRAPH_WRAPPER_H
3 #define HUGO_LEDA_GRAPH_WRAPPER_H
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>
13 //#if defined(LEDA_NAMESPACE)
14 //using namespace leda;
19 /// The namespace of HugoLib
22 // @defgroup empty_graph The LedaGraphWrapper class
25 /// A graph wrapperstructure for wrapping LEDA graphs in HUGO.
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
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.
36 /// It can be used for checking the interface compatibility,
37 /// or it can serve as a skeleton of a new graph structure.
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
49 //LedaGraphWrapper() { }
50 LedaGraphWrapper(Graph& __graph) : _graph(&__graph) { }
51 LedaGraphWrapper(const LedaGraphWrapper &G) : _graph(G._graph) { }
53 template <typename T> class NodeMap;
54 template <typename T> class EdgeMap;
56 /// The base type of the node iterators.
58 friend class LedaGraphWrapper;
61 friend class InEdgeIt;
62 friend class OutEdgeIt;
64 template <typename T> friend class NodeMap;
66 Node(leda_node __n) : _n(__n) { }
68 /// @warning The default constructor sets the iterator
69 /// to an undefined value.
71 /// Initialize the iterator to be invalid
72 Node(Invalid) : _n(0) { }
73 //Node(const Node &) {}
74 bool operator==(Node n) const { return _n==n._n; } //FIXME
75 bool operator!=(Node n) const { return _n!=n._n; } //FIXME
76 operator leda_node () { return _n; }
79 /// This iterator goes through each node.
80 class NodeIt : public Node {
82 /// @warning The default constructor sets the iterator
83 /// to an undefined value.
85 /// Initialize the iterator to be invalid
86 NodeIt(Invalid i) : Node(i) {}
87 /// Sets the iterator to the first node of \c G.
88 NodeIt(const LedaGraphWrapper &G) : Node(G._graph->first_node()) { }
89 //NodeIt(const NodeIt &) {} //FIXME
92 /// The base type of the edge iterators.
94 friend class LedaGraphWrapper;
96 template <typename T> friend class EdgeMap;
98 Edge(leda_edge __e) : _e(__e) { }
100 /// @warning The default constructor sets the iterator
101 /// to an undefined value.
103 /// Initialize the iterator to be invalid
104 Edge(Invalid) : _e(0) {}
105 //Edge(const Edge &) {}
106 bool operator==(Edge e) const { return _e==e._e; } //FIXME
107 bool operator!=(Edge e) const { return _e!=e._e; } //FIXME
108 operator leda_edge () { return _e; }
111 /// This iterator goes trought the outgoing edges of a certain graph.
113 class OutEdgeIt : public Edge {
115 /// @warning The default constructor sets the iterator
116 /// to an undefined value.
118 /// Initialize the iterator to be invalid
119 OutEdgeIt(Invalid i) : Edge(i) {}
120 /// This constructor sets the iterator to first outgoing edge.
122 /// This constructor set the iterator to the first outgoing edge of
125 ///@param G the graph
126 OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_adj_edge(n._n)) { }
129 class InEdgeIt : public Edge {
131 /// @warning The default constructor sets the iterator
132 /// to an undefined value.
134 /// Initialize the iterator to be invalid
135 InEdgeIt(Invalid i) : Edge(i) {}
136 InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_in_edge(n._n)) { }
139 // class SymEdgeIt : public Edge {};
140 class EdgeIt : public Edge {
142 /// @warning The default constructor sets the iterator
143 /// to an undefined value.
145 /// Initialize the iterator to be invalid
146 EdgeIt(Invalid i) : Edge(i) {}
147 EdgeIt(const LedaGraphWrapper & G) : Edge(G._graph->first_edge()) { }
150 /// First node of the graph.
152 /// \post \c i and the return value will be the first node.
154 NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
156 /// The first outgoing edge.
157 InEdgeIt &first(InEdgeIt &i, Node n) const {
158 i=InEdgeIt(*this, n);
161 /// The first incoming edge.
162 OutEdgeIt &first(OutEdgeIt &i, Node n) const {
163 i=OutEdgeIt(*this, n);
166 // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
167 /// The first edge of the Graph.
168 EdgeIt &first(EdgeIt &i) const {
172 // Node getNext(Node) const {}
173 // InEdgeIt getNext(InEdgeIt) const {}
174 // OutEdgeIt getNext(OutEdgeIt) const {}
175 // //SymEdgeIt getNext(SymEdgeIt) const {}
176 // EdgeIt getNext(EdgeIt) const {}
178 /// Go to the next node.
179 NodeIt &next(NodeIt &i) const {
180 i._n=_graph->succ_node(i._n);
183 /// Go to the next incoming edge.
184 InEdgeIt &next(InEdgeIt &i) const {
185 i._e=_graph->in_succ(i._e);
188 /// Go to the next outgoing edge.
189 OutEdgeIt &next(OutEdgeIt &i) const {
190 i._e=_graph->adj_succ(i._e);
193 //SymEdgeIt &next(SymEdgeIt &) const {}
194 /// Go to the next edge.
195 EdgeIt &next(EdgeIt &i) const {
196 i._e=_graph->succ_edge(i._e);
200 // template< typename It >
201 // It first() const {
207 // template< typename It >
208 // It first(Node v) const {
214 ///Gives back the head node of an edge.
215 Node head(Edge e) const {
216 return Node(_graph->target(e._e));
218 ///Gives back the tail node of an edge.
219 Node tail(Edge e) const {
220 return Node(_graph->source(e._e));
223 Node aNode(InEdgeIt e) const { return head(e); }
224 Node aNode(OutEdgeIt e) const { return tail(e); }
225 // Node aNode(SymEdgeIt) const {}
227 Node bNode(InEdgeIt e) const { return tail(e); }
228 Node bNode(OutEdgeIt e) const { return head(e); }
229 // Node bNode(SymEdgeIt) const {}
231 /// Checks if a node iterator is valid
232 bool valid(Node n) const { return n._n; }
233 /// Checks if an edge iterator is valid
234 bool valid(Edge e) const { return e._e; }
236 ///Gives back the \e id of a node.
237 int id(Node n) const { return n._n->id(); }
238 ///Gives back the \e id of an edge.
239 int id(Edge e) const { return e._e->id(); }
241 //void setInvalid(Node &) const {};
242 //void setInvalid(Edge &) const {};
244 Node addNode() const { return Node(_graph->new_node()); }
245 Edge addEdge(Node tail, Node head) const {
246 return Edge(_graph->new_edge(tail._n, head._n));
249 void erase(Node n) const { _graph->del_node(n._n); }
250 void erase(Edge e) const { _graph->del_edge(e._e); }
252 void clear() const { _graph->clear(); }
254 int nodeNum() const { return _graph->number_of_nodes(); }
255 int edgeNum() const { return _graph->number_of_edges(); }
257 ///Read/write map from the nodes to type \c T.
258 template<typename T> class NodeMap
260 leda_node_map<T> leda_stuff;
263 typedef Node KeyType;
265 NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
266 NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
268 void set(Node i, T t) { leda_stuff[i._n]=t; }
269 T get(Node i) const { return leda_stuff[i._n]; } //FIXME: Is it necessary
270 T &operator[](Node i) { return leda_stuff[i._n]; }
271 const T &operator[](Node i) const { return leda_stuff[i._n]; }
273 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
274 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); } //FIXME: Is it necessary
277 ///Read/write map from the edges to type \c T.
278 template<typename T> class EdgeMap
280 leda_edge_map<T> leda_stuff;
283 typedef Edge KeyType;
285 EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
286 EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
288 void set(Edge i, T t) { leda_stuff[i._e]=t; }
289 T get(Edge i) const { return leda_stuff[i._e]; } //FIXME: Is it necessary
290 T &operator[](Edge i) { return leda_stuff[i._e]; }
291 const T &operator[](Edge i) const { return leda_stuff[i._e]; }
293 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
294 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); } //FIXME: Is it necessary
305 // class EmptyBipGraph : public EmptyGraph
310 // ANode &next(ANode &) {}
311 // BNode &next(BNode &) {}
313 // ANode &getFirst(ANode &) const {}
314 // BNode &getFirst(BNode &) const {}
316 // enum NodeClass { A = 0, B = 1 };
317 // NodeClass getClass(Node n) {}
321 #endif // HUGO_LEDA_GRAPH_WRAPPER_H