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;
17 #include <hugo/invalid.h>
21 /// \brief A graph wrapper structure for wrapping LEDA graphs in HUGO.
23 /// This graph wrapper class wraps LEDA graphs and LEDA parametrized graphs
24 /// to satisfy HUGO graph concepts.
25 /// Then the generic HUGOlib algorithms and wrappers can be used
28 template<typename Graph>
29 class LedaGraphWrapper
33 LedaGraphWrapper() : l_graph(0) { }
34 void setGraph(Graph& _l_graph) { l_graph=&_l_graph; }
37 /// Constructor for wrapping LEDA graphs.
38 LedaGraphWrapper(Graph& _l_graph) : l_graph(&_l_graph) { }
40 LedaGraphWrapper(const LedaGraphWrapper &g) : l_graph(g.l_graph) { }
42 template <typename T> class NodeMap;
43 template <typename T> class EdgeMap;
44 template <typename T> class NodeMapWrapper;
45 template <typename T> class EdgeMapWrapper;
54 /// Trivial node-iterator
56 friend class LedaGraphWrapper<Graph>;
59 friend class InEdgeIt;
60 friend class OutEdgeIt;
62 template <typename T> friend class NodeMap;
65 Node(leda_node _l_n) : l_n(_l_n) { }
67 /// @warning The default constructor sets the iterator
68 /// to an undefined value.
70 /// Initialize the iterator to be invalid
71 Node(Invalid) : l_n(0) { }
72 //Node(const Node &) {}
73 bool operator==(Node n) const { return l_n==n.l_n; } //FIXME
74 bool operator!=(Node n) const { return l_n!=n.l_n; } //FIXME
75 operator leda_node () { return l_n; }
78 /// This iterator goes through each node.
79 class NodeIt : public Node {
81 /// @warning The default constructor sets the iterator
82 /// to an undefined value.
84 /// Initialize the iterator to be invalid
85 NodeIt(Invalid i) : Node(i) { }
86 /// Sets the iterator to the first node of \c G.
87 NodeIt(const LedaGraphWrapper &G) : Node(G.l_graph->first_node()) { }
88 //NodeIt(const NodeIt &) {} //FIXME
91 /// Trivial edge-iterator.
93 friend class LedaGraphWrapper;
95 template <typename T> friend class EdgeMap;
98 Edge(leda_edge _l_e) : l_e(_l_e) { }
100 /// @warning The default constructor sets the iterator
101 /// to an undefined value.
103 /// Initialize the iterator to be invalid
104 Edge(Invalid) : l_e(0) { }
105 //Edge(const Edge &) {}
106 bool operator==(Edge e) const { return l_e==e.l_e; } //FIXME
107 bool operator!=(Edge e) const { return l_e!=e.l_e; } //FIXME
108 operator leda_edge () { return l_e; }
111 /// This iterator goes trought the outgoing edges of a certain node.
112 class OutEdgeIt : public Edge {
114 /// @warning The default constructor sets the iterator
115 /// to an undefined value.
117 /// Initialize the iterator to be invalid
118 OutEdgeIt(Invalid i) : Edge(i) { }
119 /// This constructor sets the iterator to first outgoing edge.
121 /// This constructor set the iterator to the first outgoing edge of
124 ///@param G the graph
125 OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_adj_edge(n.l_n)) { }
128 /// This iterator goes trought the incoming edges of a certain node.
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.l_graph->first_in_edge(n.l_n)) { }
139 // class SymEdgeIt : public Edge {};
141 /// This iterator goes trought the edges of the graph.
142 class EdgeIt : public Edge {
144 /// @warning The default constructor sets the iterator
145 /// to an undefined value.
147 /// Initialize the iterator to be invalid
148 EdgeIt(Invalid i) : Edge(i) { }
149 EdgeIt(const LedaGraphWrapper & G) : Edge(G.l_graph->first_edge()) { }
152 /// First node of the graph.
154 /// \post \c i and the return value will be the first node.
156 NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
158 /// The first outgoing edge.
159 InEdgeIt &first(InEdgeIt &i, Node n) const {
160 i=InEdgeIt(*this, n);
163 /// The first incoming edge.
164 OutEdgeIt &first(OutEdgeIt &i, Node n) const {
165 i=OutEdgeIt(*this, n);
168 // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
169 /// The first edge of the graph.
170 EdgeIt &first(EdgeIt &i) const {
174 // Node getNext(Node) const {}
175 // InEdgeIt getNext(InEdgeIt) const {}
176 // OutEdgeIt getNext(OutEdgeIt) const {}
177 // //SymEdgeIt getNext(SymEdgeIt) const {}
178 // EdgeIt getNext(EdgeIt) const {}
180 /// Go to the next node.
181 NodeIt &next(NodeIt &i) const {
182 i.l_n=l_graph->succ_node(i.l_n);
185 /// Go to the next incoming edge.
186 InEdgeIt &next(InEdgeIt &i) const {
187 i.l_e=l_graph->in_succ(i.l_e);
190 /// Go to the next outgoing edge.
191 OutEdgeIt &next(OutEdgeIt &i) const {
192 i.l_e=l_graph->adj_succ(i.l_e);
195 //SymEdgeIt &next(SymEdgeIt &) const {}
196 /// Go to the next edge.
197 EdgeIt &next(EdgeIt &i) const {
198 i.l_e=l_graph->succ_edge(i.l_e);
202 // template< typename It >
203 // It first() const {
209 // template< typename It >
210 // It first(Node v) const {
216 ///Gives back the head node of an edge.
217 Node head(Edge e) const {
218 return Node(l_graph->target(e.l_e));
220 ///Gives back the tail node of an edge.
221 Node tail(Edge e) const {
222 return Node(l_graph->source(e.l_e));
225 Node aNode(InEdgeIt e) const { return head(e); }
226 Node aNode(OutEdgeIt e) const { return tail(e); }
227 // Node aNode(SymEdgeIt) const {}
229 Node bNode(InEdgeIt e) const { return tail(e); }
230 Node bNode(OutEdgeIt e) const { return head(e); }
231 // Node bNode(SymEdgeIt) const {}
233 /// Checks if a node iterator is valid
234 bool valid(Node n) const { return n.l_n; }
235 /// Checks if an edge iterator is valid
236 bool valid(Edge e) const { return e.l_e; }
238 ///Gives back the \e id of a node.
239 int id(Node n) const { return n.l_n->id(); }
240 ///Gives back the \e id of an edge.
241 int id(Edge e) const { return e.l_e->id(); }
243 //void setInvalid(Node &) const {};
244 //void setInvalid(Edge &) const {};
246 Node addNode() const { return Node(l_graph->new_node()); }
247 Edge addEdge(Node tail, Node head) const {
248 return Edge(l_graph->new_edge(tail.l_n, head.l_n));
251 void erase(Node n) const { l_graph->del_node(n.l_n); }
252 void erase(Edge e) const { l_graph->del_edge(e.l_e); }
254 void clear() const { l_graph->clear(); }
256 int nodeNum() const { return l_graph->number_of_nodes(); }
257 int edgeNum() const { return l_graph->number_of_edges(); }
259 /// Read/write map from the nodes to type \c T.
260 template<typename T> class NodeMap
262 leda_node_map<T> leda_stuff;
265 typedef Node KeyType;
267 NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
268 NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
270 void set(Node i, T t) { leda_stuff[i.l_n]=t; }
271 T get(Node i) const { return leda_stuff[i.l_n]; } //FIXME: Is it necessary
272 T &operator[](Node i) { return leda_stuff[i.l_n]; }
273 const T &operator[](Node i) const { return leda_stuff[i.l_n]; }
275 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
276 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary
279 /// Read/write map from the edges to type \c T.
280 template<typename T> class EdgeMap
282 leda_edge_map<T> leda_stuff;
285 typedef Edge KeyType;
287 EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
288 EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
290 void set(Edge i, T t) { leda_stuff[i.l_e]=t; }
291 T get(Edge i) const { return leda_stuff[i.l_e]; } //FIXME: Is it necessary
292 T &operator[](Edge i) { return leda_stuff[i.l_e]; }
293 const T &operator[](Edge i) const { return leda_stuff[i.l_e]; }
295 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
296 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary
300 /// This class is to wrap existing
301 /// LEDA node-maps to HUGO ones.
302 template<typename T> class NodeMapWrapper
304 leda_node_array<T>* leda_stuff;
307 typedef Node KeyType;
309 NodeMapWrapper(leda_node_array<T>& _leda_stuff) :
310 leda_stuff(&_leda_stuff) { }
311 //NodeMap(leda_node_map& &G, T t) : leda_stuff(*(G.l_graph), t) {}
313 void set(Node i, T t) { (*leda_stuff)[i.l_n]=t; }
314 //T get(Node i) const { return (*leda_stuff)[i.l_n]; } //FIXME: Is it necessary
315 T &operator[](Node i) { return (*leda_stuff)[i.l_n]; }
316 const T &operator[](Node i) const { return (*leda_stuff)[i.l_n]; }
318 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
319 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary
322 /// This class is to wrap existing
323 /// LEDA edge-maps to HUGO ones.
324 template<typename T> class EdgeMapWrapper
326 leda_edge_array<T>* leda_stuff;
329 typedef Edge KeyType;
331 EdgeMapWrapper(leda_edge_array<T>& _leda_stuff) :
332 leda_stuff(_leda_stuff) { }
333 //EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
335 void set(Edge i, T t) { (*leda_stuff)[i.l_e]=t; }
336 //T get(Edge i) const { return (*leda_stuff)[i.l_e]; } //FIXME: Is it necessary
337 T &operator[](Edge i) { return (*leda_stuff)[i.l_e]; }
338 const T &operator[](Edge i) const { return (*leda_stuff)[i.l_e]; }
340 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
341 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary
344 /// This class is used for access node-data of
345 /// LEDA parametrized graphs.
347 class NodeDataMap : public NodeMapWrapper<T>
350 NodeDataMap(LedaGraphWrapper<Graph>& LGW) :
351 NodeMapWrapper<T>(*(LGW._g_graph).node_data()) { }
354 /// This class is used for access edge-data of
355 /// LEDA parametrized graphs.
357 class EdgeDataMap : public EdgeMapWrapper<T>
360 EdgeDataMap(LedaGraphWrapper<Graph>& LGW) :
361 EdgeMapWrapper<T>(*(LGW._g_graph).edge_data()) { }
367 /// \brief LEDA graph template.
369 /// This graph stucture uses LEDA graphs for physical storage.
371 template<typename Graph>
372 class LedaGraph : public LedaGraphWrapper<Graph> {
373 typedef LedaGraphWrapper<Graph> Parent;
378 Parent::setGraph(gr);
384 #endif // HUGO_LEDA_GRAPH_WRAPPER_H