default_map_factory.h was missing.
3 #ifndef HUGO_SMART_GRAPH_H
4 #define HUGO_SMART_GRAPH_H
8 ///\brief SmartGraph and SymSmartGraph classes.
13 #include <hugo/invalid.h>
15 #include <hugo/default_map_factory.h>
16 #include <hugo/sym_map_factory.h>
17 #include <hugo/map_registry.h>
19 #include <hugo/map_defines.h>
23 /// \addtogroup graphs
25 // class SymSmartGraph;
27 ///A smart graph class.
29 ///This is a simple and fast graph implementation.
30 ///It is also quite memory efficient, but at the price
31 ///that <b> it does not support node and edge deletion</b>.
32 ///It conforms to the graph interface documented under
33 ///the description of \ref GraphSkeleton.
34 ///\sa \ref GraphSkeleton.
36 ///\todo Some member functions could be \c static.
38 ///\todo A possibly useful functionality: a function saveState() would
39 ///give back a data sturcture X and then the function restoreState(X)
40 ///would remove the nodes and edges added after the call of saveState().
41 ///Of course it should be used as a stack. (Maybe X is not necessary.)
43 ///\author Alpar Juttner
48 int first_in,first_out;
49 NodeT() : first_in(-1), first_out(-1) {}
53 int head, tail, next_in, next_out;
54 //FIXME: is this necessary?
55 EdgeT() : next_in(-1), next_out(-1) {}
58 std::vector<NodeT> nodes;
60 std::vector<EdgeT> edges;
65 typedef SmartGraph Graph;
75 CREATE_MAP_REGISTRIES;
76 CREATE_MAPS(DefaultMapFactory);
80 SmartGraph() : nodes(), edges() { }
81 SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
84 int nodeNum() const { return nodes.size(); }
86 int edgeNum() const { return edges.size(); }
92 int maxNodeId() const { return nodes.size()-1; }
97 int maxEdgeId() const { return edges.size()-1; }
99 Node tail(Edge e) const { return edges[e.n].tail; }
100 Node head(Edge e) const { return edges[e.n].head; }
102 NodeIt& first(NodeIt& v) const {
103 v=NodeIt(*this); return v; }
104 EdgeIt& first(EdgeIt& e) const {
105 e=EdgeIt(*this); return e; }
106 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
107 e=OutEdgeIt(*this,v); return e; }
108 InEdgeIt& first(InEdgeIt& e, const Node v) const {
109 e=InEdgeIt(*this,v); return e; }
113 /// The ID of a valid Node is a nonnegative integer not greater than
114 /// \ref maxNodeId(). The range of the ID's is not surely continuous
115 /// and the greatest node ID can be actually less then \ref maxNodeId().
117 /// The ID of the \ref INVALID node is -1.
118 ///\return The ID of the node \c v.
119 static int id(Node v) { return v.n; }
122 /// The ID of a valid Edge is a nonnegative integer not greater than
123 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
124 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
126 /// The ID of the \ref INVALID edge is -1.
127 ///\return The ID of the edge \c e.
128 static int id(Edge e) { return e.n; }
131 Node n; n.n=nodes.size();
132 nodes.push_back(NodeT()); //FIXME: Hmmm...
139 Edge addEdge(Node u, Node v) {
140 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
141 edges[e.n].tail=u.n; edges[e.n].head=v.n;
142 edges[e.n].next_out=nodes[u.n].first_out;
143 edges[e.n].next_in=nodes[v.n].first_in;
144 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
151 /// Finds an edge between two nodes.
153 /// Finds an edge from node \c u to node \c v.
155 /// If \c prev is \ref INVALID (this is the default value), then
156 /// It finds the first edge from \c u to \c v. Otherwise it looks for
157 /// the next edge from \c u to \c v after \c prev.
158 /// \return The found edge or INVALID if there is no such an edge.
159 Edge findEdge(Node u,Node v, Edge prev = INVALID)
161 int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
162 while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
175 friend class SmartGraph;
176 template <typename T> friend class NodeMap;
179 friend class OutEdgeIt;
180 friend class InEdgeIt;
181 friend class SymEdge;
185 friend int SmartGraph::id(Node v);
189 Node (Invalid) { n=-1; }
190 bool operator==(const Node i) const {return n==i.n;}
191 bool operator!=(const Node i) const {return n!=i.n;}
192 bool operator<(const Node i) const {return n<i.n;}
194 // operator bool() { return n!=-1; }
197 class NodeIt : public Node {
199 friend class SmartGraph;
201 NodeIt() : Node() { }
202 NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
203 NodeIt(Invalid i) : Node(i) { }
204 NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
205 NodeIt &operator++() {
206 n=(n+2)%(G->nodes.size()+1)-1;
210 // operator bool() { return Node::operator bool(); }
214 friend class SmartGraph;
215 template <typename T> friend class EdgeMap;
217 //template <typename T> friend class SymSmartGraph::SymEdgeMap;
218 //friend Edge SymSmartGraph::opposite(Edge) const;
224 friend int SmartGraph::id(Edge e);
227 /// An Edge with id \c n.
229 /// \bug It should be
230 /// obtained by a member function of the Graph.
233 Edge (Invalid) { n=-1; }
234 bool operator==(const Edge i) const {return n==i.n;}
235 bool operator!=(const Edge i) const {return n!=i.n;}
236 bool operator<(const Edge i) const {return n<i.n;}
237 ///\bug This is a workaround until somebody tells me how to
238 ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
239 int &idref() {return n;}
240 const int &idref() const {return n;}
242 // operator bool() { return n!=-1; }
245 class EdgeIt : public Edge {
247 friend class SmartGraph;
249 EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
250 EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
251 EdgeIt (Invalid i) : Edge(i) { }
252 EdgeIt() : Edge() { }
253 ///\bug This is a workaround until somebody tells me how to
254 ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
255 int &idref() {return n;}
256 EdgeIt &operator++() { --n; return *this; }
258 // operator bool() { return Edge::operator bool(); }
261 class OutEdgeIt : public Edge {
263 friend class SmartGraph;
265 OutEdgeIt() : Edge() { }
266 OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
267 OutEdgeIt (Invalid i) : Edge(i) { }
269 OutEdgeIt(const SmartGraph& _G,const Node v)
270 : Edge(_G.nodes[v.n].first_out), G(&_G) {}
271 OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
273 // operator bool() { return Edge::operator bool(); }
276 class InEdgeIt : public Edge {
278 friend class SmartGraph;
280 InEdgeIt() : Edge() { }
281 InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
282 InEdgeIt (Invalid i) : Edge(i) { }
283 InEdgeIt(const SmartGraph& _G,Node v)
284 : Edge(_G.nodes[v.n].first_in), G(&_G) { }
285 InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
287 // operator bool() { return Edge::operator bool(); }
292 ///Graph for bidirectional edges.
294 ///The purpose of this graph structure is to handle graphs
295 ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
296 ///of oppositely directed edges.
297 ///There is a new edge map type called
298 ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
299 ///that complements this
301 ///storing shared values for the edge pairs. The usual
302 ///\ref GraphSkeleton::EdgeMap "EdgeMap"
306 ///The oppositely directed edge can also be obtained easily
307 ///using \ref opposite.
308 ///\warning It shares the similarity with \ref SmartGraph that
309 ///it is not possible to delete edges or nodes from the graph.
310 //\sa \ref SmartGraph.
312 class SymSmartGraph : public SmartGraph
315 typedef SymSmartGraph Graph;
317 KEEP_NODE_MAP(SmartGraph);
318 KEEP_EDGE_MAP(SmartGraph);
320 CREATE_SYM_EDGE_MAP_REGISTRY;
321 CREATE_SYM_EDGE_MAP_FACTORY(DefaultMapFactory);
322 IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory);
324 SymSmartGraph() : SmartGraph() { }
325 SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
326 ///Adds a pair of oppositely directed edges to the graph.
327 Edge addEdge(Node u, Node v)
329 Edge e = SmartGraph::addEdge(u,v);
330 Edge f = SmartGraph::addEdge(v,u);
331 sym_edge_maps.add(e);
332 sym_edge_maps.add(f);
336 ///The oppositely directed edge.
338 ///Returns the oppositely directed
339 ///pair of the edge \c e.
340 static Edge opposite(Edge e)
343 f.idref() = e.idref() - 2*(e.idref()%2) + 1;
356 #endif //HUGO_SMART_GRAPH_H