A generic map with value type [0, N) where N is a small integer.
Can enumerate keys with a given value.
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 /// An empty graph class.
27 /// This class provides all the common features of a grapf structure,
28 /// however completely without implementations or real data structures
29 /// behind the interface.
30 /// All graph algorithms should compile with this class, but it will not
31 /// run properly, of course.
33 /// It can be used for checking the interface compatibility,
34 /// or it can serve as a skeleton of a new graph structure.
36 /// Also, you will find here the full documentation of a certain graph
37 /// feature, the documentation of a real graph imlementation
38 /// like @ref ListGraph or
39 /// @ref SmartGraph will just refer to this structure.
40 template<typename Graph>
41 class LedaGraphWrapper
46 //LedaGraphWrapper() { }
47 LedaGraphWrapper(Graph& __graph) : _graph(&__graph) { }
48 LedaGraphWrapper(const LedaGraphWrapper &G) : _graph(G._graph) { }
50 template <typename T> class NodeMap;
51 template <typename T> class EdgeMap;
53 /// The base type of the node iterators.
55 friend class LedaGraphWrapper;
58 friend class InEdgeIt;
59 friend class OutEdgeIt;
61 template <typename T> friend class NodeMap;
63 Node(leda_node __n) : _n(__n) { }
65 /// @warning The default constructor sets the iterator
66 /// to an undefined value.
68 /// Initialize the iterator to be invalid
69 Node(Invalid) : _n(0) { }
70 //Node(const Node &) {}
71 bool operator==(Node n) const { return _n==n._n; } //FIXME
72 bool operator!=(Node n) const { return _n!=n._n; } //FIXME
73 operator leda_node () { return _n; }
76 /// This iterator goes through each node.
77 class NodeIt : public Node {
79 /// @warning The default constructor sets the iterator
80 /// to an undefined value.
82 /// Initialize the iterator to be invalid
83 NodeIt(Invalid i) : Node(i) {}
84 /// Sets the iterator to the first node of \c G.
85 NodeIt(const LedaGraphWrapper &G) : Node(G._graph->first_node()) { }
86 //NodeIt(const NodeIt &) {} //FIXME
89 /// The base type of the edge iterators.
91 friend class LedaGraphWrapper;
93 template <typename T> friend class EdgeMap;
95 Edge(leda_edge __e) : _e(__e) { }
97 /// @warning The default constructor sets the iterator
98 /// to an undefined value.
100 /// Initialize the iterator to be invalid
101 Edge(Invalid) : _e(0) {}
102 //Edge(const Edge &) {}
103 bool operator==(Edge e) const { return _e==e._e; } //FIXME
104 bool operator!=(Edge e) const { return _e!=e._e; } //FIXME
105 operator leda_edge () { return _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._graph->first_adj_edge(n._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._graph->first_in_edge(n._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._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._n=_graph->succ_node(i._n);
180 /// Go to the next incoming edge.
181 InEdgeIt &next(InEdgeIt &i) const {
182 i._e=_graph->in_succ(i._e);
185 /// Go to the next outgoing edge.
186 OutEdgeIt &next(OutEdgeIt &i) const {
187 i._e=_graph->adj_succ(i._e);
190 //SymEdgeIt &next(SymEdgeIt &) const {}
191 /// Go to the next edge.
192 EdgeIt &next(EdgeIt &i) const {
193 i._e=_graph->succ_edge(i._e);
197 template< typename It >
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(_graph->target(e._e));
215 ///Gives back the tail node of an edge.
216 Node tail(Edge e) const {
217 return Node(_graph->source(e._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._n; }
230 /// Checks if an edge iterator is valid
231 bool valid(Edge e) const { return e._e; }
233 ///Gives back the \e id of a node.
234 int id(Node n) const { return n._n->id(); }
235 ///Gives back the \e id of an edge.
236 int id(Edge e) const { return e._e->id(); }
238 //void setInvalid(Node &) const {};
239 //void setInvalid(Edge &) const {};
241 Node addNode() const { return Node(_graph->new_node()); }
242 Edge addEdge(Node tail, Node head) const {
243 return Edge(_graph->new_edge(tail._n, head._n));
246 void erase(Node n) const { _graph->del_node(n._n); }
247 void erase(Edge e) const { _graph->del_edge(e._e); }
249 void clear() const { _graph->clear(); }
251 int nodeNum() const { return _graph->number_of_nodes(); }
252 int edgeNum() const { return _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._graph)) {}
263 NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
265 void set(Node i, T t) { leda_stuff[i._n]=t; }
266 T get(Node i) const { return leda_stuff[i._n]; } //FIXME: Is it necessary
267 T &operator[](Node i) { return leda_stuff[i._n]; }
268 const T &operator[](Node i) const { return leda_stuff[i._n]; }
270 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
271 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._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._graph)) {}
283 EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
285 void set(Edge i, T t) { leda_stuff[i._e]=t; }
286 T get(Edge i) const { return leda_stuff[i._e]; } //FIXME: Is it necessary
287 T &operator[](Edge i) { return leda_stuff[i._e]; }
288 const T &operator[](Edge i) const { return leda_stuff[i._e]; }
290 void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
291 //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); } //FIXME: Is it necessary
302 // class EmptyBipGraph : public EmptyGraph
307 // ANode &next(ANode &) {}
308 // BNode &next(BNode &) {}
310 // ANode &getFirst(ANode &) const {}
311 // BNode &getFirst(BNode &) const {}
313 // enum NodeClass { A = 0, B = 1 };
314 // NodeClass getClass(Node n) {}
318 #endif // HUGO_LEDA_GRAPH_WRAPPER_H