2 #ifndef HUGO_LEDA_GRAPH_H
3 #define HUGO_LEDA_GRAPH_H
5 #include <LEDA/graph.h>
6 #include <LEDA/node_array.h>
7 #include <LEDA/edge_array.h>
8 //#include <LEDA/graph_alg.h>
9 //#include <LEDA/dimacs.h>
11 //#if defined(LEDA_NAMESPACE)
12 //using namespace leda;
17 /// The namespace of HugoLib
20 // @defgroup empty_graph The LedaGraph class
23 /// An empty graph class.
25 /// This class provides all the common features of a grapf structure,
26 /// however completely without implementations or real data structures
27 /// behind the interface.
28 /// All graph algorithms should compile with this class, but it will not
29 /// run properly, of course.
31 /// It can be used for checking the interface compatibility,
32 /// or it can serve as a skeleton of a new graph structure.
34 /// Also, you will find here the full documentation of a certain graph
35 /// feature, the documentation of a real graph imlementation
36 /// like @ref ListGraph or
37 /// @ref SmartGraph will just refer to this structure.
38 template<typename Graph>
45 LedaGraph(Graph& __graph) : _graph(&__graph) { }
46 LedaGraph(const LedaGraph &G) : _graph(G._graph) { }
48 template <typename T> class NodeMap;
49 template <typename T> class EdgeMap;
51 /// The base type of the node iterators.
53 friend class LedaGraph;
56 friend class InEdgeIt;
57 friend class OutEdgeIt;
59 template <typename T> friend class NodeMap;
61 Node(leda_node __n) : _n(__n) { }
63 /// @warning The default constructor sets the iterator
64 /// to an undefined value.
66 /// Initialize the iterator to be invalid
67 Node(Invalid) : _n(0) { }
68 //Node(const Node &) {}
69 bool operator==(Node n) const { return _n==n._n; } //FIXME
70 bool operator!=(Node n) const { return _n!=n._n; } //FIXME
73 /// This iterator goes through each node.
74 class NodeIt : public Node {
76 /// @warning The default constructor sets the iterator
77 /// to an undefined value.
79 /// Initialize the iterator to be invalid
80 NodeIt(Invalid i) : Node(i) {}
81 /// Sets the iterator to the first node of \c G.
82 NodeIt(const LedaGraph &G) : Node(G._graph->first_node()) { }
83 //NodeIt(const NodeIt &) {} //FIXME
86 /// The base type of the edge iterators.
88 friend class LedaGraph;
90 template <typename T> friend class EdgeMap;
92 Edge(leda_edge __e) : _e(__e) { }
94 /// @warning The default constructor sets the iterator
95 /// to an undefined value.
97 /// Initialize the iterator to be invalid
98 Edge(Invalid) : _e(0) {}
99 //Edge(const Edge &) {}
100 bool operator==(Edge e) const { return _e==e._e; } //FIXME
101 bool operator!=(Edge e) const { return _e!=e._e; } //FIXME
104 /// This iterator goes trought the outgoing edges of a certain graph.
106 class OutEdgeIt : public Edge {
108 /// @warning The default constructor sets the iterator
109 /// to an undefined value.
111 /// Initialize the iterator to be invalid
112 OutEdgeIt(Invalid i) : Edge(i) {}
113 /// This constructor sets the iterator to first outgoing edge.
115 /// This constructor set the iterator to the first outgoing edge of
118 ///@param G the graph
119 OutEdgeIt(const LedaGraph & G, Node n) : Edge(G._graph->first_adj_edge(n._n)) { }
122 class InEdgeIt : public Edge {
124 /// @warning The default constructor sets the iterator
125 /// to an undefined value.
127 /// Initialize the iterator to be invalid
128 InEdgeIt(Invalid i) : Edge(i) {}
129 InEdgeIt(const LedaGraph & G, Node n) : Edge(G._graph->first_in_edge(n._n)) { }
132 // class SymEdgeIt : public Edge {};
133 class EdgeIt : public Edge {
135 /// @warning The default constructor sets the iterator
136 /// to an undefined value.
138 /// Initialize the iterator to be invalid
139 EdgeIt(Invalid i) : Edge(i) {}
140 EdgeIt(const LedaGraph & G) : Edge(G._graph->first_edge()) { }
143 /// First node of the graph.
145 /// \post \c i and the return value will be the first node.
147 NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; }
149 /// The first outgoing edge.
150 InEdgeIt &first(InEdgeIt &i, Node n) const {
151 i=InEdgeIt(*this, n);
154 /// The first incoming edge.
155 OutEdgeIt &first(OutEdgeIt &i, Node n) const {
156 i=OutEdgeIt(*this, n);
159 // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
160 /// The first edge of the Graph.
161 EdgeIt &first(EdgeIt &i) const {
165 // Node getNext(Node) const {}
166 // InEdgeIt getNext(InEdgeIt) const {}
167 // OutEdgeIt getNext(OutEdgeIt) const {}
168 // //SymEdgeIt getNext(SymEdgeIt) const {}
169 // EdgeIt getNext(EdgeIt) const {}
171 /// Go to the next node.
172 NodeIt &next(NodeIt &i) const {
173 i._n=_graph->succ_node(i._n);
176 /// Go to the next incoming edge.
177 InEdgeIt &next(InEdgeIt &i) const {
178 i._e=_graph->in_succ(i._e);
181 /// Go to the next outgoing edge.
182 OutEdgeIt &next(OutEdgeIt &i) const {
183 i._e=_graph->adj_succ(i._e);
186 //SymEdgeIt &next(SymEdgeIt &) const {}
187 /// Go to the next edge.
188 EdgeIt &next(EdgeIt &i) const {
189 i._e=_graph->succ_edge(i._e);
193 template< typename It >
200 template< typename It >
201 It first(Node v) const {
207 ///Gives back the head node of an edge.
208 Node head(Edge e) const {
209 return Node(_graph->target(e._e));
211 ///Gives back the tail node of an edge.
212 Node tail(Edge e) const {
213 return Node(_graph->source(e._e));
216 Node aNode(InEdgeIt e) const { return head(e); }
217 Node aNode(OutEdgeIt e) const { return tail(e); }
218 // Node aNode(SymEdgeIt) const {}
220 Node bNode(InEdgeIt e) const { return tail(e); }
221 Node bNode(OutEdgeIt e) const { return head(e); }
222 // Node bNode(SymEdgeIt) const {}
224 /// Checks if a node iterator is valid
225 bool valid(Node n) const { return n._n; }
226 /// Checks if an edge iterator is valid
227 bool valid(Edge e) const { return e._e; }
229 ///Gives back the \e id of a node.
230 int id(Node n) const { return n._n->id(); }
231 ///Gives back the \e id of an edge.
232 int id(Edge e) const { return e._e->id(); }
234 //void setInvalid(Node &) const {};
235 //void setInvalid(Edge &) const {};
237 Node addNode() const { return Node(_graph->new_node()); }
238 Edge addEdge(Node tail, Node head) const {
239 return Edge(_graph->new_edge(tail._n, head._n));
242 void erase(Node n) const { _graph->del_node(n._n); }
243 void erase(Edge e) const { _graph->del_edge(e._e); }
245 void clear() const { _graph->clear(); }
247 int nodeNum() const { return _graph->number_of_nodes(); }
248 int edgeNum() const { return _graph->number_of_edges(); }
250 ///Read/write map from the nodes to type \c T.
251 template<typename T> class NodeMap
253 leda_node_map<T> leda_stuff;
256 typedef Node KeyType;
258 NodeMap(const LedaGraph &G) : leda_stuff(*(G._graph)) {}
259 NodeMap(const LedaGraph &G, T t) : leda_stuff(*(G._graph), t) {}
261 void set(Node i, T t) { leda_stuff[i._n]=t; }
262 T get(Node i) const { return leda_stuff[i._n]; } //FIXME: Is it necessary
263 T &operator[](Node i) { return leda_stuff[i._n]; }
264 const T &operator[](Node i) const { return leda_stuff[i._n]; }
266 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
267 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); } //FIXME: Is it necessary
270 ///Read/write map from the edges to type \c T.
271 template<typename T> class EdgeMap
273 leda_edge_map<T> leda_stuff;
276 typedef Edge KeyType;
278 EdgeMap(const LedaGraph &G) : leda_stuff(*(G._graph)) {}
279 EdgeMap(const LedaGraph &G, T t) : leda_stuff(*(G._graph), t) {}
281 void set(Edge i, T t) { leda_stuff[i._e]=t; }
282 T get(Edge i) const { return leda_stuff[i._e]; } //FIXME: Is it necessary
283 T &operator[](Edge i) { return leda_stuff[i._e]; }
284 const T &operator[](Edge i) const { return leda_stuff[i._e]; }
286 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
287 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); } //FIXME: Is it necessary
298 // class EmptyBipGraph : public EmptyGraph
303 // ANode &next(ANode &) {}
304 // BNode &next(BNode &) {}
306 // ANode &getFirst(ANode &) const {}
307 // BNode &getFirst(BNode &) const {}
309 // enum NodeClass { A = 0, B = 1 };
310 // NodeClass getClass(Node n) {}
314 #endif // HUGO_LEDA_GRAPH_H