That's what I wanted.
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
48 LedaGraphWrapper() : _graph(0) { }
49 void setGraph(Graph& __graph) { _graph=&__graph; }
52 //LedaGraphWrapper() { }
53 LedaGraphWrapper(Graph& __graph) : _graph(&__graph) { }
54 LedaGraphWrapper(const LedaGraphWrapper &G) : _graph(G._graph) { }
56 template <typename T> class NodeMap;
57 template <typename T> class EdgeMap;
66 /// The base type of the node iterators.
68 friend class LedaGraphWrapper<Graph>;
71 friend class InEdgeIt;
72 friend class OutEdgeIt;
74 template <typename T> friend class NodeMap;
77 Node(leda_node __n) : _n(__n) { }
79 /// @warning The default constructor sets the iterator
80 /// to an undefined value.
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; }
90 /// This iterator goes through each node.
91 class NodeIt : public Node {
93 /// @warning The default constructor sets the iterator
94 /// to an undefined value.
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
103 /// The base type of the edge iterators.
105 friend class LedaGraphWrapper;
107 template <typename T> friend class EdgeMap;
110 Edge(leda_edge __e) : _e(__e) { }
112 /// @warning The default constructor sets the iterator
113 /// to an undefined value.
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; }
123 /// This iterator goes trought the outgoing edges of a certain graph.
125 class OutEdgeIt : public Edge {
127 /// @warning The default constructor sets the iterator
128 /// to an undefined value.
130 /// Initialize the iterator to be invalid
131 OutEdgeIt(Invalid i) : Edge(i) {}
132 /// This constructor sets the iterator to first outgoing edge.
134 /// This constructor set the iterator to the first outgoing edge of
137 ///@param G the graph
138 OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_adj_edge(n._n)) { }
141 class InEdgeIt : public Edge {
143 /// @warning The default constructor sets the iterator
144 /// to an undefined value.
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)) { }
151 // class SymEdgeIt : public Edge {};
152 class EdgeIt : public Edge {
154 /// @warning The default constructor sets the iterator
155 /// to an undefined value.
157 /// Initialize the iterator to be invalid
158 EdgeIt(Invalid i) : Edge(i) {}
159 EdgeIt(const LedaGraphWrapper & G) : Edge(G._graph->first_edge()) { }
162 /// First node of the graph.
164 /// \post \c i and the return value will be the first node.
166 NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
168 /// The first outgoing edge.
169 InEdgeIt &first(InEdgeIt &i, Node n) const {
170 i=InEdgeIt(*this, n);
173 /// The first incoming edge.
174 OutEdgeIt &first(OutEdgeIt &i, Node n) const {
175 i=OutEdgeIt(*this, n);
178 // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
179 /// The first edge of the Graph.
180 EdgeIt &first(EdgeIt &i) const {
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 {}
190 /// Go to the next node.
191 NodeIt &next(NodeIt &i) const {
192 i._n=_graph->succ_node(i._n);
195 /// Go to the next incoming edge.
196 InEdgeIt &next(InEdgeIt &i) const {
197 i._e=_graph->in_succ(i._e);
200 /// Go to the next outgoing edge.
201 OutEdgeIt &next(OutEdgeIt &i) const {
202 i._e=_graph->adj_succ(i._e);
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);
212 // template< typename It >
213 // It first() const {
219 // template< typename It >
220 // It first(Node v) const {
226 ///Gives back the head node of an edge.
227 Node head(Edge e) const {
228 return Node(_graph->target(e._e));
230 ///Gives back the tail node of an edge.
231 Node tail(Edge e) const {
232 return Node(_graph->source(e._e));
235 Node aNode(InEdgeIt e) const { return head(e); }
236 Node aNode(OutEdgeIt e) const { return tail(e); }
237 // Node aNode(SymEdgeIt) const {}
239 Node bNode(InEdgeIt e) const { return tail(e); }
240 Node bNode(OutEdgeIt e) const { return head(e); }
241 // Node bNode(SymEdgeIt) const {}
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; }
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(); }
253 //void setInvalid(Node &) const {};
254 //void setInvalid(Edge &) const {};
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));
261 void erase(Node n) const { _graph->del_node(n._n); }
262 void erase(Edge e) const { _graph->del_edge(e._e); }
264 void clear() const { _graph->clear(); }
266 int nodeNum() const { return _graph->number_of_nodes(); }
267 int edgeNum() const { return _graph->number_of_edges(); }
269 ///Read/write map from the nodes to type \c T.
270 template<typename T> class NodeMap
272 leda_node_map<T> leda_stuff;
275 typedef Node KeyType;
277 NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
278 NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
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]; }
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
289 ///Read/write map from the edges to type \c T.
290 template<typename T> class EdgeMap
292 leda_edge_map<T> leda_stuff;
295 typedef Edge KeyType;
297 EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
298 EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
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]; }
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
311 template<typename Graph>
312 class LedaGraph : public LedaGraphWrapper<Graph> {
313 typedef LedaGraphWrapper<Graph> Parent;
318 Parent::setGraph(gr);
328 // class EmptyBipGraph : public EmptyGraph
333 // ANode &next(ANode &) {}
334 // BNode &next(BNode &) {}
336 // ANode &getFirst(ANode &) const {}
337 // BNode &getFirst(BNode &) const {}
339 // enum NodeClass { A = 0, B = 1 };
340 // NodeClass getClass(Node n) {}
344 #endif // HUGO_LEDA_GRAPH_WRAPPER_H