alpar@395
|
1 |
// -*- mode:C++ -*-
|
alpar@395
|
2 |
|
alpar@405
|
3 |
#ifndef HUGO_LIST_GRAPH_H
|
alpar@405
|
4 |
#define HUGO_LIST_GRAPH_H
|
alpar@395
|
5 |
|
klao@491
|
6 |
///\ingroup graphs
|
alpar@395
|
7 |
///\file
|
alpar@405
|
8 |
///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
|
alpar@395
|
9 |
|
alpar@395
|
10 |
#include <vector>
|
deba@782
|
11 |
#include <climits>
|
alpar@395
|
12 |
|
ladanyi@542
|
13 |
#include <hugo/invalid.h>
|
alpar@395
|
14 |
|
deba@782
|
15 |
#include <hugo/map_registry.h>
|
deba@822
|
16 |
#include <hugo/default_map.h>
|
deba@782
|
17 |
|
deba@822
|
18 |
#include <hugo/sym_map.h>
|
deba@782
|
19 |
|
deba@782
|
20 |
#include <hugo/map_defines.h>
|
deba@782
|
21 |
|
deba@782
|
22 |
|
alpar@395
|
23 |
namespace hugo {
|
alpar@395
|
24 |
|
alpar@406
|
25 |
/// \addtogroup graphs
|
alpar@406
|
26 |
/// @{
|
alpar@406
|
27 |
|
alpar@401
|
28 |
///A list graph class.
|
alpar@395
|
29 |
|
alpar@397
|
30 |
///This is a simple and fast erasable graph implementation.
|
alpar@397
|
31 |
///
|
alpar@880
|
32 |
///It conforms to the
|
alpar@880
|
33 |
///\ref skeleton::ErasableGraph "ErasableGraph" concept.
|
alpar@880
|
34 |
///\sa skeleton::ErasableGraph.
|
alpar@397
|
35 |
class ListGraph {
|
alpar@395
|
36 |
|
alpar@397
|
37 |
//Nodes are double linked.
|
alpar@397
|
38 |
//The free nodes are only single linked using the "next" field.
|
alpar@395
|
39 |
struct NodeT
|
alpar@395
|
40 |
{
|
alpar@397
|
41 |
int first_in,first_out;
|
alpar@397
|
42 |
int prev, next;
|
alpar@395
|
43 |
};
|
alpar@397
|
44 |
//Edges are double linked.
|
alpar@397
|
45 |
//The free edges are only single linked using the "next_in" field.
|
alpar@395
|
46 |
struct EdgeT
|
alpar@395
|
47 |
{
|
alpar@397
|
48 |
int head, tail;
|
alpar@397
|
49 |
int prev_in, prev_out;
|
alpar@397
|
50 |
int next_in, next_out;
|
alpar@395
|
51 |
};
|
alpar@395
|
52 |
|
alpar@395
|
53 |
std::vector<NodeT> nodes;
|
alpar@397
|
54 |
//The first node
|
alpar@397
|
55 |
int first_node;
|
alpar@397
|
56 |
//The first free node
|
alpar@397
|
57 |
int first_free_node;
|
alpar@395
|
58 |
std::vector<EdgeT> edges;
|
alpar@397
|
59 |
//The first free edge
|
alpar@397
|
60 |
int first_free_edge;
|
alpar@395
|
61 |
|
deba@782
|
62 |
public:
|
alpar@395
|
63 |
|
deba@782
|
64 |
typedef ListGraph Graph;
|
alpar@397
|
65 |
|
alpar@395
|
66 |
class Node;
|
alpar@395
|
67 |
class Edge;
|
alpar@395
|
68 |
|
alpar@395
|
69 |
|
alpar@395
|
70 |
public:
|
alpar@395
|
71 |
|
alpar@395
|
72 |
class NodeIt;
|
alpar@395
|
73 |
class EdgeIt;
|
alpar@395
|
74 |
class OutEdgeIt;
|
alpar@395
|
75 |
class InEdgeIt;
|
deba@782
|
76 |
|
deba@822
|
77 |
/// Creating map registries.
|
deba@782
|
78 |
CREATE_MAP_REGISTRIES;
|
deba@822
|
79 |
/// Creating node and edge maps.
|
alpar@827
|
80 |
|
alpar@827
|
81 |
/// \todo
|
alpar@827
|
82 |
/// It apears in the documentation as if it were a function definition.
|
deba@822
|
83 |
CREATE_MAPS(DefaultMap);
|
deba@782
|
84 |
|
alpar@395
|
85 |
public:
|
alpar@395
|
86 |
|
deba@782
|
87 |
ListGraph()
|
deba@782
|
88 |
: nodes(), first_node(-1),
|
deba@782
|
89 |
first_free_node(-1), edges(), first_free_edge(-1) {}
|
deba@782
|
90 |
|
deba@782
|
91 |
ListGraph(const ListGraph &_g)
|
deba@782
|
92 |
: nodes(_g.nodes), first_node(_g.first_node),
|
deba@782
|
93 |
first_free_node(_g.first_free_node), edges(_g.edges),
|
deba@782
|
94 |
first_free_edge(_g.first_free_edge) {}
|
alpar@395
|
95 |
|
alpar@813
|
96 |
///Number of nodes.
|
alpar@813
|
97 |
int nodeNum() const { return nodes.size(); }
|
alpar@813
|
98 |
///Number of edges.
|
alpar@813
|
99 |
int edgeNum() const { return edges.size(); }
|
alpar@395
|
100 |
|
alpar@813
|
101 |
///Set the expected maximum number of edges.
|
alpar@695
|
102 |
|
alpar@695
|
103 |
///With this function, it is possible to set the expected number of edges.
|
alpar@695
|
104 |
///The use of this fasten the building of the graph and makes
|
alpar@695
|
105 |
///it possible to avoid the superfluous memory allocation.
|
alpar@695
|
106 |
void reserveEdge(int n) { edges.reserve(n); };
|
alpar@695
|
107 |
|
alpar@813
|
108 |
/// Maximum node ID.
|
alpar@813
|
109 |
|
alpar@813
|
110 |
/// Maximum node ID.
|
alpar@813
|
111 |
///\sa id(Node)
|
alpar@813
|
112 |
int maxNodeId() const { return nodes.size()-1; }
|
alpar@813
|
113 |
/// Maximum edge ID.
|
alpar@813
|
114 |
|
alpar@813
|
115 |
/// Maximum edge ID.
|
alpar@813
|
116 |
///\sa id(Edge)
|
alpar@813
|
117 |
int maxEdgeId() const { return edges.size()-1; }
|
alpar@395
|
118 |
|
alpar@395
|
119 |
Node tail(Edge e) const { return edges[e.n].tail; }
|
alpar@395
|
120 |
Node head(Edge e) const { return edges[e.n].head; }
|
alpar@395
|
121 |
|
alpar@713
|
122 |
NodeIt& first(NodeIt& v) const {
|
alpar@395
|
123 |
v=NodeIt(*this); return v; }
|
alpar@713
|
124 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@395
|
125 |
e=EdgeIt(*this); return e; }
|
alpar@713
|
126 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@395
|
127 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@713
|
128 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@395
|
129 |
e=InEdgeIt(*this,v); return e; }
|
alpar@395
|
130 |
|
alpar@813
|
131 |
/// Node ID.
|
alpar@813
|
132 |
|
alpar@813
|
133 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
alpar@813
|
134 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
alpar@813
|
135 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
alpar@813
|
136 |
///
|
alpar@813
|
137 |
/// The ID of the \ref INVALID node is -1.
|
alpar@813
|
138 |
///\return The ID of the node \c v.
|
alpar@713
|
139 |
static int id(Node v) { return v.n; }
|
alpar@813
|
140 |
/// Edge ID.
|
alpar@813
|
141 |
|
alpar@813
|
142 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
alpar@813
|
143 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
alpar@813
|
144 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
alpar@813
|
145 |
///
|
alpar@813
|
146 |
/// The ID of the \ref INVALID edge is -1.
|
alpar@813
|
147 |
///\return The ID of the edge \c e.
|
alpar@713
|
148 |
static int id(Edge e) { return e.n; }
|
alpar@395
|
149 |
|
alpar@397
|
150 |
/// Adds a new node to the graph.
|
alpar@397
|
151 |
|
alpar@813
|
152 |
/// \warning It adds the new node to the front of the list.
|
alpar@397
|
153 |
/// (i.e. the lastly added node becomes the first.)
|
alpar@395
|
154 |
Node addNode() {
|
alpar@397
|
155 |
int n;
|
alpar@397
|
156 |
|
alpar@397
|
157 |
if(first_free_node==-1)
|
alpar@397
|
158 |
{
|
alpar@397
|
159 |
n = nodes.size();
|
alpar@397
|
160 |
nodes.push_back(NodeT());
|
alpar@397
|
161 |
}
|
alpar@397
|
162 |
else {
|
alpar@397
|
163 |
n = first_free_node;
|
alpar@397
|
164 |
first_free_node = nodes[n].next;
|
alpar@397
|
165 |
}
|
alpar@397
|
166 |
|
alpar@397
|
167 |
nodes[n].next = first_node;
|
alpar@397
|
168 |
if(first_node != -1) nodes[first_node].prev = n;
|
alpar@397
|
169 |
first_node = n;
|
alpar@397
|
170 |
nodes[n].prev = -1;
|
alpar@397
|
171 |
|
alpar@397
|
172 |
nodes[n].first_in = nodes[n].first_out = -1;
|
alpar@397
|
173 |
|
alpar@397
|
174 |
Node nn; nn.n=n;
|
alpar@395
|
175 |
|
alpar@397
|
176 |
//Update dynamic maps
|
deba@782
|
177 |
node_maps.add(nn);
|
alpar@395
|
178 |
|
alpar@397
|
179 |
return nn;
|
alpar@395
|
180 |
}
|
alpar@395
|
181 |
|
alpar@395
|
182 |
Edge addEdge(Node u, Node v) {
|
alpar@397
|
183 |
int n;
|
alpar@397
|
184 |
|
alpar@397
|
185 |
if(first_free_edge==-1)
|
alpar@397
|
186 |
{
|
alpar@397
|
187 |
n = edges.size();
|
alpar@397
|
188 |
edges.push_back(EdgeT());
|
alpar@397
|
189 |
}
|
alpar@397
|
190 |
else {
|
alpar@397
|
191 |
n = first_free_edge;
|
alpar@397
|
192 |
first_free_edge = edges[n].next_in;
|
alpar@397
|
193 |
}
|
alpar@397
|
194 |
|
alpar@397
|
195 |
edges[n].tail = u.n; edges[n].head = v.n;
|
alpar@395
|
196 |
|
alpar@397
|
197 |
edges[n].next_out = nodes[u.n].first_out;
|
alpar@397
|
198 |
if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
|
alpar@397
|
199 |
edges[n].next_in = nodes[v.n].first_in;
|
alpar@397
|
200 |
if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
|
alpar@397
|
201 |
edges[n].prev_in = edges[n].prev_out = -1;
|
alpar@397
|
202 |
|
alpar@397
|
203 |
nodes[u.n].first_out = nodes[v.n].first_in = n;
|
alpar@397
|
204 |
|
alpar@397
|
205 |
Edge e; e.n=n;
|
alpar@397
|
206 |
|
alpar@397
|
207 |
//Update dynamic maps
|
deba@782
|
208 |
edge_maps.add(e);
|
alpar@395
|
209 |
|
alpar@395
|
210 |
return e;
|
alpar@395
|
211 |
}
|
alpar@774
|
212 |
|
alpar@774
|
213 |
/// Finds an edge between two nodes.
|
alpar@395
|
214 |
|
alpar@774
|
215 |
/// Finds an edge from node \c u to node \c v.
|
alpar@774
|
216 |
///
|
alpar@774
|
217 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@774
|
218 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@774
|
219 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@774
|
220 |
/// \return The found edge or INVALID if there is no such an edge.
|
alpar@774
|
221 |
Edge findEdge(Node u,Node v, Edge prev = INVALID)
|
alpar@774
|
222 |
{
|
alpar@774
|
223 |
int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
|
alpar@774
|
224 |
while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
|
alpar@774
|
225 |
prev.n=e;
|
alpar@774
|
226 |
return prev;
|
alpar@774
|
227 |
}
|
alpar@774
|
228 |
|
alpar@397
|
229 |
private:
|
alpar@397
|
230 |
void eraseEdge(int n) {
|
alpar@397
|
231 |
|
alpar@397
|
232 |
if(edges[n].next_in!=-1)
|
alpar@397
|
233 |
edges[edges[n].next_in].prev_in = edges[n].prev_in;
|
alpar@397
|
234 |
if(edges[n].prev_in!=-1)
|
alpar@397
|
235 |
edges[edges[n].prev_in].next_in = edges[n].next_in;
|
alpar@397
|
236 |
else nodes[edges[n].head].first_in = edges[n].next_in;
|
alpar@397
|
237 |
|
alpar@397
|
238 |
if(edges[n].next_out!=-1)
|
alpar@397
|
239 |
edges[edges[n].next_out].prev_out = edges[n].prev_out;
|
alpar@397
|
240 |
if(edges[n].prev_out!=-1)
|
alpar@397
|
241 |
edges[edges[n].prev_out].next_out = edges[n].next_out;
|
alpar@397
|
242 |
else nodes[edges[n].tail].first_out = edges[n].next_out;
|
alpar@397
|
243 |
|
alpar@397
|
244 |
edges[n].next_in = first_free_edge;
|
alpar@695
|
245 |
first_free_edge = n;
|
alpar@397
|
246 |
|
alpar@397
|
247 |
//Update dynamic maps
|
alpar@397
|
248 |
Edge e; e.n=n;
|
deba@782
|
249 |
edge_maps.erase(e);
|
deba@782
|
250 |
|
alpar@397
|
251 |
}
|
alpar@397
|
252 |
|
alpar@397
|
253 |
public:
|
alpar@397
|
254 |
|
alpar@397
|
255 |
void erase(Node nn) {
|
alpar@397
|
256 |
int n=nn.n;
|
alpar@397
|
257 |
|
alpar@397
|
258 |
int m;
|
alpar@397
|
259 |
while((m=nodes[n].first_in)!=-1) eraseEdge(m);
|
alpar@397
|
260 |
while((m=nodes[n].first_out)!=-1) eraseEdge(m);
|
alpar@397
|
261 |
|
alpar@397
|
262 |
if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
|
alpar@397
|
263 |
if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
|
alpar@397
|
264 |
else first_node = nodes[n].next;
|
alpar@397
|
265 |
|
alpar@397
|
266 |
nodes[n].next = first_free_node;
|
alpar@397
|
267 |
first_free_node = n;
|
alpar@397
|
268 |
|
alpar@397
|
269 |
//Update dynamic maps
|
deba@782
|
270 |
node_maps.erase(nn);
|
deba@782
|
271 |
|
alpar@397
|
272 |
}
|
alpar@397
|
273 |
|
alpar@397
|
274 |
void erase(Edge e) { eraseEdge(e.n); }
|
alpar@397
|
275 |
|
alpar@397
|
276 |
void clear() {
|
deba@782
|
277 |
edge_maps.clear();
|
deba@782
|
278 |
edges.clear();
|
deba@782
|
279 |
node_maps.clear();
|
deba@782
|
280 |
nodes.clear();
|
alpar@397
|
281 |
first_node=first_free_node=first_free_edge=-1;
|
alpar@397
|
282 |
}
|
alpar@395
|
283 |
|
alpar@395
|
284 |
class Node {
|
alpar@397
|
285 |
friend class ListGraph;
|
alpar@395
|
286 |
template <typename T> friend class NodeMap;
|
alpar@400
|
287 |
|
alpar@395
|
288 |
friend class Edge;
|
alpar@395
|
289 |
friend class OutEdgeIt;
|
alpar@395
|
290 |
friend class InEdgeIt;
|
alpar@395
|
291 |
friend class SymEdge;
|
alpar@395
|
292 |
|
alpar@395
|
293 |
protected:
|
alpar@395
|
294 |
int n;
|
alpar@722
|
295 |
friend int ListGraph::id(Node v);
|
alpar@395
|
296 |
Node(int nn) {n=nn;}
|
alpar@395
|
297 |
public:
|
alpar@395
|
298 |
Node() {}
|
alpar@503
|
299 |
Node (Invalid) { n=-1; }
|
alpar@395
|
300 |
bool operator==(const Node i) const {return n==i.n;}
|
alpar@395
|
301 |
bool operator!=(const Node i) const {return n!=i.n;}
|
alpar@395
|
302 |
bool operator<(const Node i) const {return n<i.n;}
|
alpar@774
|
303 |
// ///Validity check
|
alpar@774
|
304 |
// operator bool() { return n!=-1; }
|
alpar@395
|
305 |
};
|
alpar@395
|
306 |
|
alpar@395
|
307 |
class NodeIt : public Node {
|
alpar@774
|
308 |
const ListGraph *G;
|
alpar@397
|
309 |
friend class ListGraph;
|
alpar@395
|
310 |
public:
|
alpar@400
|
311 |
NodeIt() : Node() { }
|
alpar@400
|
312 |
NodeIt(Invalid i) : Node(i) { }
|
alpar@774
|
313 |
NodeIt(const ListGraph& _G) : Node(_G.first_node), G(&_G) { }
|
alpar@774
|
314 |
NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { }
|
alpar@774
|
315 |
NodeIt &operator++() {
|
alpar@774
|
316 |
n=G->nodes[n].next;
|
alpar@774
|
317 |
return *this;
|
alpar@774
|
318 |
}
|
alpar@774
|
319 |
// ///Validity check
|
alpar@774
|
320 |
// operator bool() { return Node::operator bool(); }
|
alpar@395
|
321 |
};
|
alpar@395
|
322 |
|
alpar@395
|
323 |
class Edge {
|
alpar@397
|
324 |
friend class ListGraph;
|
alpar@395
|
325 |
template <typename T> friend class EdgeMap;
|
alpar@395
|
326 |
|
alpar@397
|
327 |
//template <typename T> friend class SymListGraph::SymEdgeMap;
|
alpar@397
|
328 |
//friend Edge SymListGraph::opposite(Edge) const;
|
alpar@395
|
329 |
|
alpar@395
|
330 |
friend class Node;
|
alpar@395
|
331 |
friend class NodeIt;
|
alpar@395
|
332 |
protected:
|
alpar@395
|
333 |
int n;
|
alpar@722
|
334 |
friend int ListGraph::id(Edge e);
|
alpar@395
|
335 |
|
alpar@706
|
336 |
public:
|
alpar@706
|
337 |
/// An Edge with id \c n.
|
alpar@706
|
338 |
|
alpar@706
|
339 |
/// \bug It should be
|
alpar@706
|
340 |
/// obtained by a member function of the Graph.
|
alpar@395
|
341 |
Edge(int nn) {n=nn;}
|
alpar@706
|
342 |
|
alpar@395
|
343 |
Edge() { }
|
alpar@395
|
344 |
Edge (Invalid) { n=-1; }
|
alpar@395
|
345 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@395
|
346 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@395
|
347 |
bool operator<(const Edge i) const {return n<i.n;}
|
alpar@395
|
348 |
///\bug This is a workaround until somebody tells me how to
|
alpar@397
|
349 |
///make class \c SymListGraph::SymEdgeMap friend of Edge
|
alpar@395
|
350 |
int &idref() {return n;}
|
alpar@774
|
351 |
const int &idref() const {return n;}
|
alpar@774
|
352 |
// ///Validity check
|
alpar@774
|
353 |
// operator bool() { return n!=-1; }
|
alpar@774
|
354 |
};
|
alpar@395
|
355 |
|
alpar@395
|
356 |
class EdgeIt : public Edge {
|
alpar@774
|
357 |
const ListGraph *G;
|
alpar@397
|
358 |
friend class ListGraph;
|
alpar@395
|
359 |
public:
|
alpar@774
|
360 |
EdgeIt(const ListGraph& _G) : Edge(), G(&_G) {
|
alpar@397
|
361 |
int m;
|
alpar@774
|
362 |
for(m=_G.first_node;
|
alpar@774
|
363 |
m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next);
|
alpar@774
|
364 |
n = (m==-1)?-1:_G.nodes[m].first_in;
|
alpar@397
|
365 |
}
|
alpar@395
|
366 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@774
|
367 |
EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@395
|
368 |
EdgeIt() : Edge() { }
|
alpar@395
|
369 |
///\bug This is a workaround until somebody tells me how to
|
alpar@397
|
370 |
///make class \c SymListGraph::SymEdgeMap friend of Edge
|
alpar@395
|
371 |
int &idref() {return n;}
|
alpar@774
|
372 |
EdgeIt &operator++() {
|
alpar@774
|
373 |
if(G->edges[n].next_in!=-1) n=G->edges[n].next_in;
|
alpar@774
|
374 |
else {
|
alpar@774
|
375 |
int nn;
|
alpar@774
|
376 |
for(nn=G->nodes[G->edges[n].head].next;
|
alpar@774
|
377 |
nn!=-1 && G->nodes[nn].first_in == -1;
|
alpar@774
|
378 |
nn = G->nodes[nn].next) ;
|
alpar@774
|
379 |
n = (nn==-1)?-1:G->nodes[nn].first_in;
|
alpar@774
|
380 |
}
|
alpar@774
|
381 |
return *this;
|
alpar@774
|
382 |
}
|
alpar@774
|
383 |
// ///Validity check
|
alpar@774
|
384 |
// operator bool() { return Edge::operator bool(); }
|
alpar@395
|
385 |
};
|
alpar@395
|
386 |
|
alpar@395
|
387 |
class OutEdgeIt : public Edge {
|
alpar@774
|
388 |
const ListGraph *G;
|
alpar@397
|
389 |
friend class ListGraph;
|
alpar@395
|
390 |
public:
|
alpar@395
|
391 |
OutEdgeIt() : Edge() { }
|
alpar@774
|
392 |
OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@395
|
393 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@395
|
394 |
|
alpar@774
|
395 |
OutEdgeIt(const ListGraph& _G,const Node v)
|
alpar@774
|
396 |
: Edge(_G.nodes[v.n].first_out), G(&_G) {}
|
alpar@774
|
397 |
OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
|
alpar@774
|
398 |
// ///Validity check
|
alpar@774
|
399 |
// operator bool() { return Edge::operator bool(); }
|
alpar@395
|
400 |
};
|
alpar@395
|
401 |
|
alpar@395
|
402 |
class InEdgeIt : public Edge {
|
alpar@774
|
403 |
const ListGraph *G;
|
alpar@397
|
404 |
friend class ListGraph;
|
alpar@395
|
405 |
public:
|
alpar@395
|
406 |
InEdgeIt() : Edge() { }
|
alpar@774
|
407 |
InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@395
|
408 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@774
|
409 |
InEdgeIt(const ListGraph& _G,Node v)
|
alpar@774
|
410 |
: Edge(_G.nodes[v.n].first_in), G(&_G) { }
|
alpar@774
|
411 |
InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
|
alpar@774
|
412 |
// ///Validity check
|
alpar@774
|
413 |
// operator bool() { return Edge::operator bool(); }
|
alpar@395
|
414 |
};
|
alpar@395
|
415 |
};
|
alpar@395
|
416 |
|
alpar@395
|
417 |
///Graph for bidirectional edges.
|
alpar@395
|
418 |
|
alpar@395
|
419 |
///The purpose of this graph structure is to handle graphs
|
alpar@395
|
420 |
///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
|
alpar@395
|
421 |
///of oppositely directed edges.
|
alpar@395
|
422 |
///There is a new edge map type called
|
alpar@397
|
423 |
///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
|
alpar@395
|
424 |
///that complements this
|
alpar@395
|
425 |
///feature by
|
alpar@395
|
426 |
///storing shared values for the edge pairs. The usual
|
alpar@880
|
427 |
///\ref Graph::EdgeMap "EdgeMap"
|
alpar@395
|
428 |
///can be used
|
alpar@395
|
429 |
///as well.
|
alpar@395
|
430 |
///
|
alpar@395
|
431 |
///The oppositely directed edge can also be obtained easily
|
alpar@395
|
432 |
///using \ref opposite.
|
alpar@397
|
433 |
///
|
alpar@397
|
434 |
///Here erase(Edge) deletes a pair of edges.
|
alpar@397
|
435 |
///
|
alpar@397
|
436 |
///\todo this date structure need some reconsiderations. Maybe it
|
alpar@397
|
437 |
///should be implemented independently from ListGraph.
|
deba@782
|
438 |
|
alpar@397
|
439 |
class SymListGraph : public ListGraph
|
alpar@395
|
440 |
{
|
alpar@395
|
441 |
public:
|
deba@782
|
442 |
|
deba@782
|
443 |
typedef SymListGraph Graph;
|
deba@782
|
444 |
|
deba@822
|
445 |
/// Creating symmetric map registry.
|
deba@782
|
446 |
CREATE_SYM_EDGE_MAP_REGISTRY;
|
deba@822
|
447 |
/// Creating symmetric edge map.
|
deba@822
|
448 |
CREATE_SYM_EDGE_MAP(DefaultMap);
|
alpar@395
|
449 |
|
alpar@397
|
450 |
SymListGraph() : ListGraph() { }
|
alpar@397
|
451 |
SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
|
alpar@397
|
452 |
///Adds a pair of oppositely directed edges to the graph.
|
alpar@395
|
453 |
Edge addEdge(Node u, Node v)
|
alpar@395
|
454 |
{
|
alpar@397
|
455 |
Edge e = ListGraph::addEdge(u,v);
|
deba@782
|
456 |
Edge f = ListGraph::addEdge(v,u);
|
deba@782
|
457 |
sym_edge_maps.add(e);
|
deba@782
|
458 |
sym_edge_maps.add(f);
|
deba@782
|
459 |
|
alpar@395
|
460 |
return e;
|
alpar@395
|
461 |
}
|
alpar@395
|
462 |
|
deba@782
|
463 |
void erase(Node n) { ListGraph::erase(n);}
|
alpar@395
|
464 |
///The oppositely directed edge.
|
alpar@395
|
465 |
|
alpar@395
|
466 |
///Returns the oppositely directed
|
alpar@395
|
467 |
///pair of the edge \c e.
|
alpar@713
|
468 |
static Edge opposite(Edge e)
|
alpar@395
|
469 |
{
|
alpar@395
|
470 |
Edge f;
|
alpar@395
|
471 |
f.idref() = e.idref() - 2*(e.idref()%2) + 1;
|
alpar@395
|
472 |
return f;
|
alpar@395
|
473 |
}
|
alpar@395
|
474 |
|
alpar@397
|
475 |
///Removes a pair of oppositely directed edges to the graph.
|
alpar@397
|
476 |
void erase(Edge e) {
|
deba@782
|
477 |
Edge f = opposite(e);
|
deba@782
|
478 |
sym_edge_maps.erase(e);
|
deba@782
|
479 |
sym_edge_maps.erase(f);
|
deba@782
|
480 |
ListGraph::erase(f);
|
alpar@397
|
481 |
ListGraph::erase(e);
|
deba@782
|
482 |
}
|
deba@782
|
483 |
};
|
alpar@395
|
484 |
|
alpar@400
|
485 |
|
alpar@401
|
486 |
///A graph class containing only nodes.
|
alpar@400
|
487 |
|
alpar@401
|
488 |
///This class implements a graph structure without edges.
|
alpar@401
|
489 |
///The most useful application of this class is to be the node set of an
|
alpar@401
|
490 |
///\ref EdgeSet class.
|
alpar@400
|
491 |
///
|
alpar@880
|
492 |
///It conforms to
|
alpar@880
|
493 |
///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept
|
alpar@880
|
494 |
///with the exception that you cannot
|
alpar@401
|
495 |
///add (or delete) edges. The usual edge iterators are exists, but they are
|
alpar@401
|
496 |
///always \ref INVALID.
|
alpar@880
|
497 |
///\sa skeleton::ExtendableGraph
|
alpar@880
|
498 |
///\sa EdgeSet
|
alpar@400
|
499 |
class NodeSet {
|
alpar@400
|
500 |
|
alpar@400
|
501 |
//Nodes are double linked.
|
alpar@400
|
502 |
//The free nodes are only single linked using the "next" field.
|
alpar@400
|
503 |
struct NodeT
|
alpar@400
|
504 |
{
|
alpar@400
|
505 |
int first_in,first_out;
|
alpar@400
|
506 |
int prev, next;
|
alpar@400
|
507 |
// NodeT() {}
|
alpar@400
|
508 |
};
|
alpar@400
|
509 |
|
alpar@400
|
510 |
std::vector<NodeT> nodes;
|
alpar@400
|
511 |
//The first node
|
alpar@400
|
512 |
int first_node;
|
alpar@400
|
513 |
//The first free node
|
alpar@400
|
514 |
int first_free_node;
|
alpar@400
|
515 |
|
alpar@400
|
516 |
public:
|
deba@782
|
517 |
|
deba@782
|
518 |
typedef NodeSet Graph;
|
alpar@400
|
519 |
|
alpar@400
|
520 |
class Node;
|
alpar@400
|
521 |
class Edge;
|
alpar@400
|
522 |
|
alpar@400
|
523 |
public:
|
alpar@400
|
524 |
|
alpar@400
|
525 |
class NodeIt;
|
alpar@400
|
526 |
class EdgeIt;
|
alpar@400
|
527 |
class OutEdgeIt;
|
alpar@400
|
528 |
class InEdgeIt;
|
alpar@400
|
529 |
|
deba@822
|
530 |
/// Creating node map registry.
|
deba@822
|
531 |
CREATE_NODE_MAP_REGISTRY;
|
deba@822
|
532 |
/// Creating node maps.
|
deba@822
|
533 |
CREATE_NODE_MAP(DefaultMap);
|
deba@822
|
534 |
|
deba@822
|
535 |
/// Creating empty map structure for edges.
|
deba@822
|
536 |
template <typename Value>
|
deba@822
|
537 |
class EdgeMap {
|
deba@822
|
538 |
public:
|
deba@822
|
539 |
EdgeMap() {}
|
deba@822
|
540 |
EdgeMap(const Graph&) {}
|
deba@822
|
541 |
EdgeMap(const Graph&, const Value&) {}
|
deba@822
|
542 |
|
deba@822
|
543 |
EdgeMap(const EdgeMap&) {}
|
deba@822
|
544 |
template <typename CMap> EdgeMap(const CMap&) {}
|
deba@822
|
545 |
|
deba@822
|
546 |
EdgeMap& operator=(const EdgeMap&) {}
|
deba@822
|
547 |
template <typename CMap> EdgeMap& operator=(const CMap&) {}
|
deba@822
|
548 |
|
deba@822
|
549 |
class ConstIterator {
|
deba@822
|
550 |
public:
|
deba@822
|
551 |
bool operator==(const ConstIterator&) {return true;}
|
deba@822
|
552 |
bool operator!=(const ConstIterator&) {return false;}
|
deba@822
|
553 |
};
|
deba@822
|
554 |
|
deba@822
|
555 |
typedef ConstIterator Iterator;
|
deba@822
|
556 |
|
deba@822
|
557 |
Iterator begin() { return Iterator();}
|
deba@822
|
558 |
Iterator end() { return Iterator();}
|
deba@822
|
559 |
|
deba@822
|
560 |
ConstIterator begin() const { return ConstIterator();}
|
deba@822
|
561 |
ConstIterator end() const { return ConstIterator();}
|
deba@822
|
562 |
|
deba@822
|
563 |
};
|
alpar@400
|
564 |
|
alpar@400
|
565 |
public:
|
alpar@400
|
566 |
|
alpar@408
|
567 |
///Default constructor
|
deba@782
|
568 |
NodeSet()
|
deba@782
|
569 |
: nodes(), first_node(-1), first_free_node(-1) {}
|
alpar@408
|
570 |
///Copy constructor
|
deba@782
|
571 |
NodeSet(const NodeSet &_g)
|
deba@782
|
572 |
: nodes(_g.nodes), first_node(_g.first_node),
|
deba@782
|
573 |
first_free_node(_g.first_free_node) {}
|
alpar@400
|
574 |
|
alpar@813
|
575 |
///Number of nodes.
|
alpar@813
|
576 |
int nodeNum() const { return nodes.size(); }
|
alpar@813
|
577 |
///Number of edges.
|
alpar@813
|
578 |
int edgeNum() const { return 0; }
|
alpar@400
|
579 |
|
alpar@813
|
580 |
/// Maximum node ID.
|
alpar@813
|
581 |
|
alpar@813
|
582 |
/// Maximum node ID.
|
alpar@813
|
583 |
///\sa id(Node)
|
alpar@813
|
584 |
int maxNodeId() const { return nodes.size()-1; }
|
alpar@813
|
585 |
/// Maximum edge ID.
|
alpar@813
|
586 |
|
alpar@813
|
587 |
/// Maximum edge ID.
|
alpar@813
|
588 |
///\sa id(Edge)
|
alpar@813
|
589 |
int maxEdgeId() const { return 0; }
|
alpar@400
|
590 |
|
alpar@400
|
591 |
Node tail(Edge e) const { return INVALID; }
|
alpar@400
|
592 |
Node head(Edge e) const { return INVALID; }
|
alpar@400
|
593 |
|
alpar@400
|
594 |
NodeIt& first(NodeIt& v) const {
|
alpar@400
|
595 |
v=NodeIt(*this); return v; }
|
alpar@400
|
596 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@400
|
597 |
e=EdgeIt(*this); return e; }
|
alpar@400
|
598 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@400
|
599 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@400
|
600 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@400
|
601 |
e=InEdgeIt(*this,v); return e; }
|
alpar@400
|
602 |
|
alpar@813
|
603 |
/// Node ID.
|
alpar@813
|
604 |
|
alpar@813
|
605 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
alpar@813
|
606 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
alpar@813
|
607 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
alpar@813
|
608 |
///
|
alpar@813
|
609 |
/// The ID of the \ref INVALID node is -1.
|
alpar@813
|
610 |
///\return The ID of the node \c v.
|
alpar@400
|
611 |
int id(Node v) const { return v.n; }
|
alpar@813
|
612 |
/// Edge ID.
|
alpar@813
|
613 |
|
alpar@813
|
614 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
alpar@813
|
615 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
alpar@813
|
616 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
alpar@813
|
617 |
///
|
alpar@813
|
618 |
/// The ID of the \ref INVALID edge is -1.
|
alpar@813
|
619 |
///\return The ID of the edge \c e.
|
alpar@400
|
620 |
int id(Edge e) const { return -1; }
|
alpar@400
|
621 |
|
alpar@400
|
622 |
/// Adds a new node to the graph.
|
alpar@400
|
623 |
|
alpar@813
|
624 |
/// \warning It adds the new node to the front of the list.
|
alpar@400
|
625 |
/// (i.e. the lastly added node becomes the first.)
|
alpar@400
|
626 |
Node addNode() {
|
alpar@400
|
627 |
int n;
|
alpar@400
|
628 |
|
alpar@400
|
629 |
if(first_free_node==-1)
|
alpar@400
|
630 |
{
|
alpar@400
|
631 |
n = nodes.size();
|
alpar@400
|
632 |
nodes.push_back(NodeT());
|
alpar@400
|
633 |
}
|
alpar@400
|
634 |
else {
|
alpar@400
|
635 |
n = first_free_node;
|
alpar@400
|
636 |
first_free_node = nodes[n].next;
|
alpar@400
|
637 |
}
|
alpar@400
|
638 |
|
alpar@400
|
639 |
nodes[n].next = first_node;
|
alpar@400
|
640 |
if(first_node != -1) nodes[first_node].prev = n;
|
alpar@400
|
641 |
first_node = n;
|
alpar@400
|
642 |
nodes[n].prev = -1;
|
alpar@400
|
643 |
|
alpar@400
|
644 |
nodes[n].first_in = nodes[n].first_out = -1;
|
alpar@400
|
645 |
|
alpar@400
|
646 |
Node nn; nn.n=n;
|
alpar@400
|
647 |
|
alpar@400
|
648 |
//Update dynamic maps
|
deba@782
|
649 |
node_maps.add(nn);
|
alpar@400
|
650 |
|
alpar@400
|
651 |
return nn;
|
alpar@400
|
652 |
}
|
alpar@400
|
653 |
|
alpar@400
|
654 |
void erase(Node nn) {
|
alpar@400
|
655 |
int n=nn.n;
|
alpar@400
|
656 |
|
alpar@400
|
657 |
if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
|
alpar@400
|
658 |
if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
|
alpar@400
|
659 |
else first_node = nodes[n].next;
|
alpar@400
|
660 |
|
alpar@400
|
661 |
nodes[n].next = first_free_node;
|
alpar@400
|
662 |
first_free_node = n;
|
alpar@400
|
663 |
|
alpar@400
|
664 |
//Update dynamic maps
|
deba@782
|
665 |
node_maps.erase(nn);
|
alpar@400
|
666 |
}
|
alpar@400
|
667 |
|
alpar@774
|
668 |
|
alpar@774
|
669 |
Edge findEdge(Node u,Node v, Edge prev = INVALID)
|
alpar@774
|
670 |
{
|
alpar@774
|
671 |
return INVALID;
|
alpar@774
|
672 |
}
|
alpar@774
|
673 |
|
alpar@400
|
674 |
void clear() {
|
deba@782
|
675 |
node_maps.clear();
|
alpar@400
|
676 |
nodes.clear();
|
alpar@400
|
677 |
first_node = first_free_node = -1;
|
alpar@400
|
678 |
}
|
alpar@400
|
679 |
|
alpar@400
|
680 |
class Node {
|
alpar@400
|
681 |
friend class NodeSet;
|
alpar@400
|
682 |
template <typename T> friend class NodeMap;
|
alpar@400
|
683 |
|
alpar@400
|
684 |
friend class Edge;
|
alpar@400
|
685 |
friend class OutEdgeIt;
|
alpar@400
|
686 |
friend class InEdgeIt;
|
alpar@400
|
687 |
|
alpar@400
|
688 |
protected:
|
alpar@400
|
689 |
int n;
|
alpar@400
|
690 |
friend int NodeSet::id(Node v) const;
|
alpar@400
|
691 |
Node(int nn) {n=nn;}
|
alpar@400
|
692 |
public:
|
alpar@400
|
693 |
Node() {}
|
alpar@400
|
694 |
Node (Invalid i) { n=-1; }
|
alpar@400
|
695 |
bool operator==(const Node i) const {return n==i.n;}
|
alpar@400
|
696 |
bool operator!=(const Node i) const {return n!=i.n;}
|
alpar@400
|
697 |
bool operator<(const Node i) const {return n<i.n;}
|
alpar@400
|
698 |
};
|
alpar@400
|
699 |
|
alpar@400
|
700 |
class NodeIt : public Node {
|
alpar@774
|
701 |
const NodeSet *G;
|
alpar@400
|
702 |
friend class NodeSet;
|
alpar@400
|
703 |
public:
|
alpar@579
|
704 |
NodeIt() : Node() { }
|
alpar@774
|
705 |
NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { }
|
alpar@579
|
706 |
NodeIt(Invalid i) : Node(i) { }
|
alpar@774
|
707 |
NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { }
|
alpar@774
|
708 |
NodeIt &operator++() {
|
alpar@774
|
709 |
n=G->nodes[n].next;
|
alpar@774
|
710 |
return *this;
|
alpar@774
|
711 |
}
|
alpar@400
|
712 |
};
|
alpar@400
|
713 |
|
alpar@400
|
714 |
class Edge {
|
alpar@400
|
715 |
//friend class NodeSet;
|
alpar@400
|
716 |
//template <typename T> friend class EdgeMap;
|
alpar@400
|
717 |
|
alpar@400
|
718 |
//template <typename T> friend class SymNodeSet::SymEdgeMap;
|
alpar@400
|
719 |
//friend Edge SymNodeSet::opposite(Edge) const;
|
alpar@400
|
720 |
|
alpar@400
|
721 |
// friend class Node;
|
alpar@400
|
722 |
// friend class NodeIt;
|
alpar@400
|
723 |
protected:
|
alpar@400
|
724 |
//friend int NodeSet::id(Edge e) const;
|
alpar@400
|
725 |
// Edge(int nn) {}
|
alpar@400
|
726 |
public:
|
alpar@400
|
727 |
Edge() { }
|
alpar@400
|
728 |
Edge (Invalid) { }
|
alpar@400
|
729 |
bool operator==(const Edge i) const {return true;}
|
alpar@400
|
730 |
bool operator!=(const Edge i) const {return false;}
|
alpar@400
|
731 |
bool operator<(const Edge i) const {return false;}
|
alpar@400
|
732 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
733 |
///make class \c SymNodeSet::SymEdgeMap friend of Edge
|
alpar@400
|
734 |
// int idref() {return -1;}
|
alpar@400
|
735 |
// int idref() const {return -1;}
|
alpar@400
|
736 |
};
|
alpar@400
|
737 |
|
alpar@400
|
738 |
class EdgeIt : public Edge {
|
alpar@400
|
739 |
//friend class NodeSet;
|
alpar@400
|
740 |
public:
|
alpar@400
|
741 |
EdgeIt(const NodeSet& G) : Edge() { }
|
alpar@774
|
742 |
EdgeIt(const NodeSet&, Edge) : Edge() { }
|
alpar@400
|
743 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
744 |
EdgeIt() : Edge() { }
|
alpar@400
|
745 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
746 |
///make class \c SymNodeSet::SymEdgeMap friend of Edge
|
alpar@400
|
747 |
// int idref() {return -1;}
|
alpar@774
|
748 |
EdgeIt operator++() { return INVALID; }
|
alpar@400
|
749 |
};
|
alpar@400
|
750 |
|
alpar@400
|
751 |
class OutEdgeIt : public Edge {
|
alpar@400
|
752 |
friend class NodeSet;
|
alpar@400
|
753 |
public:
|
alpar@400
|
754 |
OutEdgeIt() : Edge() { }
|
alpar@774
|
755 |
OutEdgeIt(const NodeSet&, Edge) : Edge() { }
|
alpar@400
|
756 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
757 |
OutEdgeIt(const NodeSet& G,const Node v) : Edge() {}
|
alpar@774
|
758 |
OutEdgeIt operator++() { return INVALID; }
|
alpar@400
|
759 |
};
|
alpar@400
|
760 |
|
alpar@400
|
761 |
class InEdgeIt : public Edge {
|
alpar@400
|
762 |
friend class NodeSet;
|
alpar@400
|
763 |
public:
|
alpar@400
|
764 |
InEdgeIt() : Edge() { }
|
alpar@774
|
765 |
InEdgeIt(const NodeSet&, Edge) : Edge() { }
|
alpar@400
|
766 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
767 |
InEdgeIt(const NodeSet& G,Node v) :Edge() {}
|
alpar@774
|
768 |
InEdgeIt operator++() { return INVALID; }
|
alpar@400
|
769 |
};
|
alpar@400
|
770 |
|
alpar@400
|
771 |
};
|
alpar@400
|
772 |
|
alpar@400
|
773 |
|
alpar@400
|
774 |
|
alpar@401
|
775 |
///Graph structure using a node set of another graph.
|
alpar@401
|
776 |
|
alpar@401
|
777 |
///This structure can be used to establish another graph over a node set
|
alpar@401
|
778 |
/// of an existing one. The node iterator will go through the nodes of the
|
alpar@401
|
779 |
/// original graph, and the NodeMap's of both graphs will convert to
|
alpar@401
|
780 |
/// each other.
|
alpar@401
|
781 |
///
|
alpar@404
|
782 |
///\warning Adding or deleting nodes from the graph is not safe if an
|
alpar@404
|
783 |
///\ref EdgeSet is currently attached to it!
|
alpar@404
|
784 |
///
|
alpar@404
|
785 |
///\todo Make it possible to add/delete edges from the base graph
|
alpar@404
|
786 |
///(and from \ref EdgeSet, as well)
|
alpar@404
|
787 |
///
|
alpar@401
|
788 |
///\param GG The type of the graph which shares its node set with this class.
|
alpar@880
|
789 |
///Its interface must conform to the
|
alpar@880
|
790 |
///\ref skeleton::StaticGraph "StaticGraph" concept.
|
alpar@400
|
791 |
///
|
alpar@880
|
792 |
///It conforms to the
|
alpar@880
|
793 |
///\ref skeleton::ExtendableGraph "ExtendableGraph" concept.
|
alpar@880
|
794 |
///\sa skeleton::ExtendableGraph.
|
alpar@880
|
795 |
///\sa NodeSet.
|
alpar@400
|
796 |
template<typename GG>
|
alpar@400
|
797 |
class EdgeSet {
|
alpar@400
|
798 |
|
alpar@400
|
799 |
typedef GG NodeGraphType;
|
alpar@400
|
800 |
|
alpar@400
|
801 |
NodeGraphType &G;
|
alpar@400
|
802 |
|
alpar@515
|
803 |
public:
|
deba@782
|
804 |
|
alpar@400
|
805 |
class Node;
|
alpar@705
|
806 |
class Edge;
|
alpar@705
|
807 |
class OutEdgeIt;
|
alpar@705
|
808 |
class InEdgeIt;
|
alpar@705
|
809 |
class SymEdge;
|
deba@782
|
810 |
|
deba@782
|
811 |
typedef EdgeSet Graph;
|
deba@782
|
812 |
|
alpar@531
|
813 |
int id(Node v) const;
|
alpar@531
|
814 |
|
alpar@531
|
815 |
class Node : public NodeGraphType::Node {
|
alpar@531
|
816 |
friend class EdgeSet;
|
alpar@531
|
817 |
// template <typename T> friend class NodeMap;
|
alpar@531
|
818 |
|
alpar@531
|
819 |
friend class Edge;
|
alpar@531
|
820 |
friend class OutEdgeIt;
|
alpar@531
|
821 |
friend class InEdgeIt;
|
alpar@531
|
822 |
friend class SymEdge;
|
alpar@531
|
823 |
|
alpar@531
|
824 |
public:
|
alpar@531
|
825 |
friend int EdgeSet::id(Node v) const;
|
alpar@531
|
826 |
// Node(int nn) {n=nn;}
|
alpar@531
|
827 |
public:
|
alpar@531
|
828 |
Node() : NodeGraphType::Node() {}
|
alpar@531
|
829 |
Node (Invalid i) : NodeGraphType::Node(i) {}
|
alpar@531
|
830 |
Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
|
alpar@531
|
831 |
};
|
alpar@531
|
832 |
|
alpar@531
|
833 |
class NodeIt : public NodeGraphType::NodeIt {
|
alpar@531
|
834 |
friend class EdgeSet;
|
alpar@531
|
835 |
public:
|
alpar@531
|
836 |
NodeIt() : NodeGraphType::NodeIt() { }
|
alpar@774
|
837 |
NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { }
|
alpar@531
|
838 |
NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
|
alpar@531
|
839 |
NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
|
alpar@531
|
840 |
NodeIt(const typename NodeGraphType::NodeIt &n)
|
alpar@531
|
841 |
: NodeGraphType::NodeIt(n) {}
|
alpar@579
|
842 |
|
alpar@531
|
843 |
operator Node() { return Node(*this);}
|
alpar@774
|
844 |
NodeIt &operator++()
|
alpar@774
|
845 |
{ this->NodeGraphType::NodeIt::operator++(); return *this;}
|
alpar@531
|
846 |
};
|
alpar@515
|
847 |
|
alpar@515
|
848 |
private:
|
alpar@400
|
849 |
//Edges are double linked.
|
alpar@400
|
850 |
//The free edges are only single linked using the "next_in" field.
|
alpar@400
|
851 |
struct NodeT
|
alpar@400
|
852 |
{
|
alpar@400
|
853 |
int first_in,first_out;
|
alpar@400
|
854 |
NodeT() : first_in(-1), first_out(-1) { }
|
alpar@400
|
855 |
};
|
alpar@400
|
856 |
|
alpar@400
|
857 |
struct EdgeT
|
alpar@400
|
858 |
{
|
alpar@400
|
859 |
Node head, tail;
|
alpar@400
|
860 |
int prev_in, prev_out;
|
alpar@400
|
861 |
int next_in, next_out;
|
alpar@400
|
862 |
};
|
alpar@400
|
863 |
|
alpar@400
|
864 |
|
alpar@515
|
865 |
typename NodeGraphType::template NodeMap<NodeT> nodes;
|
alpar@400
|
866 |
|
alpar@400
|
867 |
std::vector<EdgeT> edges;
|
alpar@400
|
868 |
//The first free edge
|
alpar@400
|
869 |
int first_free_edge;
|
alpar@400
|
870 |
|
alpar@400
|
871 |
public:
|
alpar@400
|
872 |
|
alpar@400
|
873 |
class Node;
|
alpar@400
|
874 |
class Edge;
|
alpar@400
|
875 |
|
alpar@400
|
876 |
class NodeIt;
|
alpar@400
|
877 |
class EdgeIt;
|
alpar@400
|
878 |
class OutEdgeIt;
|
alpar@400
|
879 |
class InEdgeIt;
|
deba@782
|
880 |
|
deba@782
|
881 |
|
deba@877
|
882 |
/// Creates edge map registry.
|
deba@782
|
883 |
CREATE_EDGE_MAP_REGISTRY;
|
deba@877
|
884 |
/// Creates edge maps.
|
deba@822
|
885 |
CREATE_EDGE_MAP(DefaultMap);
|
deba@822
|
886 |
|
deba@877
|
887 |
/// Imports node maps from the NodeGraphType.
|
deba@822
|
888 |
IMPORT_NODE_MAP(NodeGraphType, graph.G, EdgeSet, graph);
|
alpar@400
|
889 |
|
alpar@400
|
890 |
|
alpar@400
|
891 |
public:
|
alpar@400
|
892 |
|
alpar@408
|
893 |
///Constructor
|
alpar@408
|
894 |
|
alpar@408
|
895 |
///Construates a new graph based on the nodeset of an existing one.
|
alpar@408
|
896 |
///\param _G the base graph.
|
alpar@880
|
897 |
explicit EdgeSet(NodeGraphType &_G)
|
deba@782
|
898 |
: G(_G), nodes(_G), edges(),
|
deba@782
|
899 |
first_free_edge(-1) {}
|
alpar@408
|
900 |
///Copy constructor
|
alpar@408
|
901 |
|
alpar@408
|
902 |
///Makes a copy of an EdgeSet.
|
alpar@408
|
903 |
///It will be based on the same graph.
|
alpar@880
|
904 |
explicit EdgeSet(const EdgeSet &_g)
|
deba@782
|
905 |
: G(_g.G), nodes(_g.G), edges(_g.edges),
|
deba@782
|
906 |
first_free_edge(_g.first_free_edge) {}
|
alpar@400
|
907 |
|
alpar@813
|
908 |
///Number of nodes.
|
alpar@813
|
909 |
int nodeNum() const { return G.nodeNum(); }
|
alpar@813
|
910 |
///Number of edges.
|
alpar@813
|
911 |
int edgeNum() const { return edges.size(); }
|
alpar@400
|
912 |
|
alpar@813
|
913 |
/// Maximum node ID.
|
alpar@813
|
914 |
|
alpar@813
|
915 |
/// Maximum node ID.
|
alpar@813
|
916 |
///\sa id(Node)
|
alpar@813
|
917 |
int maxNodeId() const { return G.maxNodeId(); }
|
alpar@813
|
918 |
/// Maximum edge ID.
|
alpar@813
|
919 |
|
alpar@813
|
920 |
/// Maximum edge ID.
|
alpar@813
|
921 |
///\sa id(Edge)
|
alpar@813
|
922 |
int maxEdgeId() const { return edges.size()-1; }
|
alpar@400
|
923 |
|
alpar@400
|
924 |
Node tail(Edge e) const { return edges[e.n].tail; }
|
alpar@400
|
925 |
Node head(Edge e) const { return edges[e.n].head; }
|
alpar@400
|
926 |
|
alpar@400
|
927 |
NodeIt& first(NodeIt& v) const {
|
alpar@400
|
928 |
v=NodeIt(*this); return v; }
|
alpar@400
|
929 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@400
|
930 |
e=EdgeIt(*this); return e; }
|
alpar@400
|
931 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@400
|
932 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@400
|
933 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@400
|
934 |
e=InEdgeIt(*this,v); return e; }
|
alpar@400
|
935 |
|
alpar@813
|
936 |
/// Node ID.
|
alpar@813
|
937 |
|
alpar@813
|
938 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
alpar@813
|
939 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
alpar@813
|
940 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
alpar@813
|
941 |
///
|
alpar@813
|
942 |
/// The ID of the \ref INVALID node is -1.
|
alpar@813
|
943 |
///\return The ID of the node \c v.
|
alpar@813
|
944 |
int id(Node v) { return G.id(v); }
|
alpar@813
|
945 |
/// Edge ID.
|
alpar@813
|
946 |
|
alpar@813
|
947 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
alpar@813
|
948 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
alpar@813
|
949 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
alpar@813
|
950 |
///
|
alpar@813
|
951 |
/// The ID of the \ref INVALID edge is -1.
|
alpar@813
|
952 |
///\return The ID of the edge \c e.
|
alpar@400
|
953 |
int id(Edge e) const { return e.n; }
|
alpar@400
|
954 |
|
alpar@400
|
955 |
/// Adds a new node to the graph.
|
alpar@579
|
956 |
Node addNode() { return G.addNode(); }
|
alpar@400
|
957 |
|
alpar@400
|
958 |
Edge addEdge(Node u, Node v) {
|
alpar@400
|
959 |
int n;
|
alpar@400
|
960 |
|
alpar@400
|
961 |
if(first_free_edge==-1)
|
alpar@400
|
962 |
{
|
alpar@400
|
963 |
n = edges.size();
|
alpar@400
|
964 |
edges.push_back(EdgeT());
|
alpar@400
|
965 |
}
|
alpar@400
|
966 |
else {
|
alpar@400
|
967 |
n = first_free_edge;
|
alpar@400
|
968 |
first_free_edge = edges[n].next_in;
|
alpar@400
|
969 |
}
|
alpar@400
|
970 |
|
alpar@401
|
971 |
edges[n].tail = u; edges[n].head = v;
|
alpar@400
|
972 |
|
alpar@401
|
973 |
edges[n].next_out = nodes[u].first_out;
|
alpar@401
|
974 |
if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
|
alpar@401
|
975 |
edges[n].next_in = nodes[v].first_in;
|
alpar@401
|
976 |
if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
|
alpar@400
|
977 |
edges[n].prev_in = edges[n].prev_out = -1;
|
alpar@400
|
978 |
|
alpar@401
|
979 |
nodes[u].first_out = nodes[v].first_in = n;
|
alpar@400
|
980 |
|
alpar@400
|
981 |
Edge e; e.n=n;
|
alpar@400
|
982 |
|
alpar@400
|
983 |
//Update dynamic maps
|
deba@782
|
984 |
edge_maps.add(e);
|
alpar@400
|
985 |
|
alpar@400
|
986 |
return e;
|
alpar@400
|
987 |
}
|
alpar@400
|
988 |
|
alpar@774
|
989 |
/// Finds an edge between two nodes.
|
alpar@774
|
990 |
|
alpar@774
|
991 |
/// Finds an edge from node \c u to node \c v.
|
alpar@774
|
992 |
///
|
alpar@774
|
993 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@774
|
994 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@774
|
995 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@774
|
996 |
/// \return The found edge or INVALID if there is no such an edge.
|
alpar@774
|
997 |
Edge findEdge(Node u,Node v, Edge prev = INVALID)
|
alpar@774
|
998 |
{
|
alpar@774
|
999 |
int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out;
|
alpar@774
|
1000 |
while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out;
|
alpar@774
|
1001 |
prev.n=e;
|
alpar@774
|
1002 |
return prev;
|
alpar@774
|
1003 |
}
|
alpar@774
|
1004 |
|
alpar@400
|
1005 |
private:
|
alpar@400
|
1006 |
void eraseEdge(int n) {
|
alpar@400
|
1007 |
|
alpar@400
|
1008 |
if(edges[n].next_in!=-1)
|
alpar@400
|
1009 |
edges[edges[n].next_in].prev_in = edges[n].prev_in;
|
alpar@400
|
1010 |
if(edges[n].prev_in!=-1)
|
alpar@400
|
1011 |
edges[edges[n].prev_in].next_in = edges[n].next_in;
|
alpar@400
|
1012 |
else nodes[edges[n].head].first_in = edges[n].next_in;
|
alpar@400
|
1013 |
|
alpar@400
|
1014 |
if(edges[n].next_out!=-1)
|
alpar@400
|
1015 |
edges[edges[n].next_out].prev_out = edges[n].prev_out;
|
alpar@400
|
1016 |
if(edges[n].prev_out!=-1)
|
alpar@400
|
1017 |
edges[edges[n].prev_out].next_out = edges[n].next_out;
|
alpar@400
|
1018 |
else nodes[edges[n].tail].first_out = edges[n].next_out;
|
alpar@400
|
1019 |
|
alpar@400
|
1020 |
edges[n].next_in = first_free_edge;
|
alpar@400
|
1021 |
first_free_edge = -1;
|
alpar@400
|
1022 |
|
alpar@400
|
1023 |
//Update dynamic maps
|
deba@782
|
1024 |
Edge e; e.n = n;
|
deba@782
|
1025 |
edge_maps.erase(e);
|
alpar@400
|
1026 |
}
|
alpar@400
|
1027 |
|
alpar@400
|
1028 |
public:
|
alpar@400
|
1029 |
|
alpar@400
|
1030 |
// void erase(Node nn) {
|
alpar@400
|
1031 |
// int n=nn.n;
|
alpar@400
|
1032 |
// int m;
|
alpar@400
|
1033 |
// while((m=nodes[n].first_in)!=-1) eraseEdge(m);
|
alpar@400
|
1034 |
// while((m=nodes[n].first_out)!=-1) eraseEdge(m);
|
alpar@400
|
1035 |
// }
|
alpar@400
|
1036 |
|
alpar@400
|
1037 |
void erase(Edge e) { eraseEdge(e.n); }
|
alpar@400
|
1038 |
|
alpar@579
|
1039 |
///Clear all edges. (Doesn't clear the nodes!)
|
alpar@579
|
1040 |
void clear() {
|
deba@782
|
1041 |
edge_maps.clear();
|
alpar@579
|
1042 |
edges.clear();
|
alpar@579
|
1043 |
first_free_edge=-1;
|
alpar@579
|
1044 |
}
|
alpar@579
|
1045 |
|
alpar@579
|
1046 |
|
alpar@400
|
1047 |
class Edge {
|
alpar@579
|
1048 |
public:
|
alpar@400
|
1049 |
friend class EdgeSet;
|
alpar@400
|
1050 |
template <typename T> friend class EdgeMap;
|
alpar@400
|
1051 |
|
alpar@400
|
1052 |
friend class Node;
|
alpar@400
|
1053 |
friend class NodeIt;
|
alpar@579
|
1054 |
public:
|
alpar@774
|
1055 |
///\bug It should be at least protected
|
alpar@579
|
1056 |
///
|
alpar@579
|
1057 |
int n;
|
alpar@400
|
1058 |
protected:
|
alpar@400
|
1059 |
friend int EdgeSet::id(Edge e) const;
|
alpar@400
|
1060 |
|
alpar@400
|
1061 |
Edge(int nn) {n=nn;}
|
alpar@400
|
1062 |
public:
|
alpar@400
|
1063 |
Edge() { }
|
alpar@400
|
1064 |
Edge (Invalid) { n=-1; }
|
alpar@400
|
1065 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@400
|
1066 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@400
|
1067 |
bool operator<(const Edge i) const {return n<i.n;}
|
alpar@400
|
1068 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
1069 |
///make class \c SymEdgeSet::SymEdgeMap friend of Edge
|
alpar@400
|
1070 |
int &idref() {return n;}
|
alpar@400
|
1071 |
const int &idref() const {return n;}
|
alpar@400
|
1072 |
};
|
alpar@400
|
1073 |
|
alpar@400
|
1074 |
class EdgeIt : public Edge {
|
alpar@400
|
1075 |
friend class EdgeSet;
|
alpar@579
|
1076 |
template <typename T> friend class EdgeMap;
|
alpar@579
|
1077 |
|
alpar@774
|
1078 |
const EdgeSet *G;
|
alpar@400
|
1079 |
public:
|
alpar@774
|
1080 |
EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) {
|
alpar@503
|
1081 |
// typename NodeGraphType::Node m;
|
alpar@503
|
1082 |
NodeIt m;
|
alpar@774
|
1083 |
for(G->first(m);
|
alpar@774
|
1084 |
m!=INVALID && G->nodes[m].first_in == -1; ++m);
|
alpar@774
|
1085 |
///\bug AJJAJ! This is a non sense!!!!!!!
|
alpar@774
|
1086 |
this->n = m!=INVALID?-1:G->nodes[m].first_in;
|
alpar@400
|
1087 |
}
|
alpar@774
|
1088 |
EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@400
|
1089 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
1090 |
EdgeIt() : Edge() { }
|
alpar@774
|
1091 |
///.
|
alpar@774
|
1092 |
|
alpar@774
|
1093 |
///\bug UNIMPLEMENTED!!!!!
|
alpar@774
|
1094 |
//
|
alpar@774
|
1095 |
EdgeIt &operator++() {
|
alpar@774
|
1096 |
return *this;
|
alpar@774
|
1097 |
}
|
alpar@774
|
1098 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
1099 |
///make class \c SymEdgeSet::SymEdgeMap friend of Edge
|
alpar@515
|
1100 |
int &idref() {return this->n;}
|
alpar@400
|
1101 |
};
|
alpar@400
|
1102 |
|
alpar@400
|
1103 |
class OutEdgeIt : public Edge {
|
alpar@774
|
1104 |
const EdgeSet *G;
|
alpar@400
|
1105 |
friend class EdgeSet;
|
alpar@400
|
1106 |
public:
|
alpar@400
|
1107 |
OutEdgeIt() : Edge() { }
|
alpar@400
|
1108 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@774
|
1109 |
OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@400
|
1110 |
|
alpar@774
|
1111 |
OutEdgeIt(const EdgeSet& _G,const Node v) :
|
alpar@774
|
1112 |
Edge(_G.nodes[v].first_out), G(&_G) { }
|
deba@844
|
1113 |
OutEdgeIt &operator++() {
|
deba@844
|
1114 |
Edge::n = G->edges[Edge::n].next_out;
|
deba@844
|
1115 |
return *this;
|
deba@844
|
1116 |
}
|
alpar@400
|
1117 |
};
|
alpar@400
|
1118 |
|
alpar@400
|
1119 |
class InEdgeIt : public Edge {
|
alpar@774
|
1120 |
const EdgeSet *G;
|
alpar@400
|
1121 |
friend class EdgeSet;
|
alpar@400
|
1122 |
public:
|
alpar@400
|
1123 |
InEdgeIt() : Edge() { }
|
alpar@400
|
1124 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@774
|
1125 |
InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@774
|
1126 |
InEdgeIt(const EdgeSet& _G,Node v)
|
alpar@774
|
1127 |
: Edge(_G.nodes[v].first_in), G(&_G) { }
|
deba@844
|
1128 |
InEdgeIt &operator++() {
|
deba@844
|
1129 |
Edge::n = G->edges[Edge::n].next_in;
|
deba@844
|
1130 |
return *this;
|
deba@844
|
1131 |
}
|
alpar@400
|
1132 |
};
|
deba@782
|
1133 |
|
alpar@400
|
1134 |
};
|
alpar@406
|
1135 |
|
alpar@579
|
1136 |
template<typename GG>
|
alpar@579
|
1137 |
inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
|
alpar@531
|
1138 |
|
alpar@406
|
1139 |
/// @}
|
alpar@406
|
1140 |
|
alpar@395
|
1141 |
} //namespace hugo
|
alpar@395
|
1142 |
|
alpar@405
|
1143 |
#endif //HUGO_LIST_GRAPH_H
|