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 //LedaGraphWrapper() { }
38 LedaGraphWrapper(Graph& _l_graph) : l_graph(&_l_graph) { }
39 LedaGraphWrapper(const LedaGraphWrapper &G) : l_graph(G.l_graph) { }
41 template <typename T> class NodeMap;
42 template <typename T> class EdgeMap;
51 /// The base type of the node iterators.
53 friend class LedaGraphWrapper<Graph>;
56 friend class InEdgeIt;
57 friend class OutEdgeIt;
59 template <typename T> friend class NodeMap;
62 Node(leda_node _l_n) : l_n(_l_n) { }
64 /// @warning The default constructor sets the iterator
65 /// to an undefined value.
67 /// Initialize the iterator to be invalid
68 Node(Invalid) : l_n(0) { }
69 //Node(const Node &) {}
70 bool operator==(Node n) const { return l_n==n.l_n; } //FIXME
71 bool operator!=(Node n) const { return l_n!=n.l_n; } //FIXME
72 operator leda_node () { return l_n; }
75 /// This iterator goes through each node.
76 class NodeIt : public Node {
78 /// @warning The default constructor sets the iterator
79 /// to an undefined value.
81 /// Initialize the iterator to be invalid
82 NodeIt(Invalid i) : Node(i) {}
83 /// Sets the iterator to the first node of \c G.
84 NodeIt(const LedaGraphWrapper &G) : Node(G.l_graph->first_node()) { }
85 //NodeIt(const NodeIt &) {} //FIXME
88 /// The base type of the edge iterators.
90 friend class LedaGraphWrapper;
92 template <typename T> friend class EdgeMap;
95 Edge(leda_edge _l_e) : l_e(_l_e) { }
97 /// @warning The default constructor sets the iterator
98 /// to an undefined value.
100 /// Initialize the iterator to be invalid
101 Edge(Invalid) : l_e(0) {}
102 //Edge(const Edge &) {}
103 bool operator==(Edge e) const { return l_e==e.l_e; } //FIXME
104 bool operator!=(Edge e) const { return l_e!=e.l_e; } //FIXME
105 operator leda_edge () { return l_e; }
108 /// This iterator goes trought the outgoing edges of a certain graph.
110 class OutEdgeIt : public Edge {
112 /// @warning The default constructor sets the iterator
113 /// to an undefined value.
115 /// Initialize the iterator to be invalid
116 OutEdgeIt(Invalid i) : Edge(i) {}
117 /// This constructor sets the iterator to first outgoing edge.
119 /// This constructor set the iterator to the first outgoing edge of
122 ///@param G the graph
123 OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_adj_edge(n.l_n)) { }
126 class InEdgeIt : public Edge {
128 /// @warning The default constructor sets the iterator
129 /// to an undefined value.
131 /// Initialize the iterator to be invalid
132 InEdgeIt(Invalid i) : Edge(i) {}
133 InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_in_edge(n.l_n)) { }
136 // class SymEdgeIt : public Edge {};
137 class EdgeIt : public Edge {
139 /// @warning The default constructor sets the iterator
140 /// to an undefined value.
142 /// Initialize the iterator to be invalid
143 EdgeIt(Invalid i) : Edge(i) {}
144 EdgeIt(const LedaGraphWrapper & G) : Edge(G.l_graph->first_edge()) { }
147 /// First node of the graph.
149 /// \post \c i and the return value will be the first node.
151 NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
153 /// The first outgoing edge.
154 InEdgeIt &first(InEdgeIt &i, Node n) const {
155 i=InEdgeIt(*this, n);
158 /// The first incoming edge.
159 OutEdgeIt &first(OutEdgeIt &i, Node n) const {
160 i=OutEdgeIt(*this, n);
163 // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
164 /// The first edge of the Graph.
165 EdgeIt &first(EdgeIt &i) const {
169 // Node getNext(Node) const {}
170 // InEdgeIt getNext(InEdgeIt) const {}
171 // OutEdgeIt getNext(OutEdgeIt) const {}
172 // //SymEdgeIt getNext(SymEdgeIt) const {}
173 // EdgeIt getNext(EdgeIt) const {}
175 /// Go to the next node.
176 NodeIt &next(NodeIt &i) const {
177 i.l_n=l_graph->succ_node(i.l_n);
180 /// Go to the next incoming edge.
181 InEdgeIt &next(InEdgeIt &i) const {
182 i.l_e=l_graph->in_succ(i.l_e);
185 /// Go to the next outgoing edge.
186 OutEdgeIt &next(OutEdgeIt &i) const {
187 i.l_e=l_graph->adj_succ(i.l_e);
190 //SymEdgeIt &next(SymEdgeIt &) const {}
191 /// Go to the next edge.
192 EdgeIt &next(EdgeIt &i) const {
193 i.l_e=l_graph->succ_edge(i.l_e);
197 // template< typename It >
198 // It first() const {
204 // template< typename It >
205 // It first(Node v) const {
211 ///Gives back the head node of an edge.
212 Node head(Edge e) const {
213 return Node(l_graph->target(e.l_e));
215 ///Gives back the tail node of an edge.
216 Node tail(Edge e) const {
217 return Node(l_graph->source(e.l_e));
220 Node aNode(InEdgeIt e) const { return head(e); }
221 Node aNode(OutEdgeIt e) const { return tail(e); }
222 // Node aNode(SymEdgeIt) const {}
224 Node bNode(InEdgeIt e) const { return tail(e); }
225 Node bNode(OutEdgeIt e) const { return head(e); }
226 // Node bNode(SymEdgeIt) const {}
228 /// Checks if a node iterator is valid
229 bool valid(Node n) const { return n.l_n; }
230 /// Checks if an edge iterator is valid
231 bool valid(Edge e) const { return e.l_e; }
233 ///Gives back the \e id of a node.
234 int id(Node n) const { return n.l_n->id(); }
235 ///Gives back the \e id of an edge.
236 int id(Edge e) const { return e.l_e->id(); }
238 //void setInvalid(Node &) const {};
239 //void setInvalid(Edge &) const {};
241 Node addNode() const { return Node(l_graph->new_node()); }
242 Edge addEdge(Node tail, Node head) const {
243 return Edge(l_graph->new_edge(tail.l_n, head.l_n));
246 void erase(Node n) const { l_graph->del_node(n.l_n); }
247 void erase(Edge e) const { l_graph->del_edge(e.l_e); }
249 void clear() const { l_graph->clear(); }
251 int nodeNum() const { return l_graph->number_of_nodes(); }
252 int edgeNum() const { return l_graph->number_of_edges(); }
254 ///Read/write map from the nodes to type \c T.
255 template<typename T> class NodeMap
257 leda_node_map<T> leda_stuff;
260 typedef Node KeyType;
262 NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
263 NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
265 void set(Node i, T t) { leda_stuff[i.l_n]=t; }
266 T get(Node i) const { return leda_stuff[i.l_n]; } //FIXME: Is it necessary
267 T &operator[](Node i) { return leda_stuff[i.l_n]; }
268 const T &operator[](Node i) const { return leda_stuff[i.l_n]; }
270 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
271 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary
274 ///Read/write map from the edges to type \c T.
275 template<typename T> class EdgeMap
277 leda_edge_map<T> leda_stuff;
280 typedef Edge KeyType;
282 EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
283 EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
285 void set(Edge i, T t) { leda_stuff[i.l_e]=t; }
286 T get(Edge i) const { return leda_stuff[i.l_e]; } //FIXME: Is it necessary
287 T &operator[](Edge i) { return leda_stuff[i.l_e]; }
288 const T &operator[](Edge i) const { return leda_stuff[i.l_e]; }
290 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
291 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary
297 /// \brief LEDA graph template.
299 /// This graph stucture uses LEDA graphs for physical storage.
301 template<typename Graph>
302 class LedaGraph : public LedaGraphWrapper<Graph> {
303 typedef LedaGraphWrapper<Graph> Parent;
308 Parent::setGraph(gr);
314 #endif // HUGO_LEDA_GRAPH_WRAPPER_H