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@698
|
11 |
#include <climits>
|
alpar@395
|
12 |
|
deba@698
|
13 |
#include "invalid.h"
|
deba@698
|
14 |
|
deba@698
|
15 |
#include "vector_map_factory.h"
|
deba@698
|
16 |
#include "map_registry.h"
|
deba@698
|
17 |
|
deba@698
|
18 |
#include "map_defines.h"
|
alpar@395
|
19 |
|
alpar@395
|
20 |
namespace hugo {
|
alpar@395
|
21 |
|
alpar@406
|
22 |
/// \addtogroup graphs
|
alpar@406
|
23 |
/// @{
|
alpar@406
|
24 |
|
alpar@401
|
25 |
///A list graph class.
|
alpar@395
|
26 |
|
alpar@397
|
27 |
///This is a simple and fast erasable graph implementation.
|
alpar@397
|
28 |
///
|
alpar@395
|
29 |
///It conforms to the graph interface documented under
|
alpar@395
|
30 |
///the description of \ref GraphSkeleton.
|
alpar@395
|
31 |
///\sa \ref GraphSkeleton.
|
alpar@397
|
32 |
class ListGraph {
|
alpar@395
|
33 |
|
alpar@397
|
34 |
//Nodes are double linked.
|
alpar@397
|
35 |
//The free nodes are only single linked using the "next" field.
|
alpar@395
|
36 |
struct NodeT
|
alpar@395
|
37 |
{
|
alpar@397
|
38 |
int first_in,first_out;
|
alpar@397
|
39 |
int prev, next;
|
alpar@397
|
40 |
// NodeT() {}
|
alpar@395
|
41 |
};
|
alpar@397
|
42 |
//Edges are double linked.
|
alpar@397
|
43 |
//The free edges are only single linked using the "next_in" field.
|
alpar@395
|
44 |
struct EdgeT
|
alpar@395
|
45 |
{
|
alpar@397
|
46 |
int head, tail;
|
alpar@397
|
47 |
int prev_in, prev_out;
|
alpar@397
|
48 |
int next_in, next_out;
|
alpar@395
|
49 |
//FIXME: is this necessary?
|
alpar@397
|
50 |
// EdgeT() : next_in(-1), next_out(-1) prev_in(-1), prev_out(-1) {}
|
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 |
|
alpar@397
|
62 |
protected:
|
alpar@395
|
63 |
|
alpar@395
|
64 |
public:
|
alpar@397
|
65 |
|
alpar@395
|
66 |
class Node;
|
alpar@395
|
67 |
class Edge;
|
alpar@395
|
68 |
|
deba@698
|
69 |
typedef ListGraph Graph;
|
deba@698
|
70 |
|
alpar@395
|
71 |
public:
|
alpar@395
|
72 |
|
alpar@395
|
73 |
class NodeIt;
|
alpar@395
|
74 |
class EdgeIt;
|
alpar@395
|
75 |
class OutEdgeIt;
|
alpar@395
|
76 |
class InEdgeIt;
|
alpar@395
|
77 |
|
deba@698
|
78 |
CREATE_MAP_REGISTRIES;
|
deba@698
|
79 |
CREATE_MAPS(VectorMapFactory);
|
deba@698
|
80 |
|
alpar@395
|
81 |
public:
|
alpar@395
|
82 |
|
alpar@397
|
83 |
ListGraph() : nodes(), first_node(-1),
|
alpar@397
|
84 |
first_free_node(-1), edges(), first_free_edge(-1) {}
|
alpar@397
|
85 |
ListGraph(const ListGraph &_g) : nodes(_g.nodes), first_node(_g.first_node),
|
alpar@397
|
86 |
first_free_node(_g.first_free_node),
|
alpar@397
|
87 |
edges(_g.edges),
|
alpar@397
|
88 |
first_free_edge(_g.first_free_edge) {}
|
alpar@395
|
89 |
|
alpar@395
|
90 |
|
alpar@395
|
91 |
int nodeNum() const { return nodes.size(); } //FIXME: What is this?
|
alpar@395
|
92 |
int edgeNum() const { return edges.size(); } //FIXME: What is this?
|
alpar@395
|
93 |
|
alpar@695
|
94 |
///Set the expected number of edges
|
alpar@695
|
95 |
|
alpar@695
|
96 |
///With this function, it is possible to set the expected number of edges.
|
alpar@695
|
97 |
///The use of this fasten the building of the graph and makes
|
alpar@695
|
98 |
///it possible to avoid the superfluous memory allocation.
|
alpar@695
|
99 |
void reserveEdge(int n) { edges.reserve(n); };
|
alpar@695
|
100 |
|
alpar@395
|
101 |
///\bug This function does something different than
|
alpar@395
|
102 |
///its name would suggests...
|
alpar@395
|
103 |
int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
|
alpar@395
|
104 |
///\bug This function does something different than
|
alpar@395
|
105 |
///its name would suggests...
|
alpar@395
|
106 |
int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
|
alpar@395
|
107 |
|
alpar@395
|
108 |
Node tail(Edge e) const { return edges[e.n].tail; }
|
alpar@395
|
109 |
Node head(Edge e) const { return edges[e.n].head; }
|
alpar@395
|
110 |
|
alpar@395
|
111 |
Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
|
alpar@395
|
112 |
Node aNode(InEdgeIt e) const { return edges[e.n].head; }
|
alpar@395
|
113 |
|
alpar@395
|
114 |
Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
|
alpar@395
|
115 |
Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
|
alpar@395
|
116 |
|
alpar@395
|
117 |
NodeIt& first(NodeIt& v) const {
|
alpar@395
|
118 |
v=NodeIt(*this); return v; }
|
alpar@395
|
119 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@395
|
120 |
e=EdgeIt(*this); return e; }
|
alpar@395
|
121 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@395
|
122 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@395
|
123 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@395
|
124 |
e=InEdgeIt(*this,v); return e; }
|
alpar@395
|
125 |
|
alpar@395
|
126 |
// template< typename It >
|
alpar@395
|
127 |
// It first() const { It e; first(e); return e; }
|
alpar@395
|
128 |
|
alpar@395
|
129 |
// template< typename It >
|
alpar@395
|
130 |
// It first(Node v) const { It e; first(e,v); return e; }
|
alpar@395
|
131 |
|
alpar@395
|
132 |
bool valid(Edge e) const { return e.n!=-1; }
|
alpar@395
|
133 |
bool valid(Node n) const { return n.n!=-1; }
|
alpar@395
|
134 |
|
alpar@395
|
135 |
void setInvalid(Edge &e) { e.n=-1; }
|
alpar@395
|
136 |
void setInvalid(Node &n) { n.n=-1; }
|
alpar@395
|
137 |
|
alpar@395
|
138 |
template <typename It> It getNext(It it) const
|
alpar@395
|
139 |
{ It tmp(it); return next(tmp); }
|
alpar@395
|
140 |
|
alpar@395
|
141 |
NodeIt& next(NodeIt& it) const {
|
alpar@397
|
142 |
it.n=nodes[it.n].next;
|
alpar@395
|
143 |
return it;
|
alpar@395
|
144 |
}
|
alpar@395
|
145 |
OutEdgeIt& next(OutEdgeIt& it) const
|
alpar@395
|
146 |
{ it.n=edges[it.n].next_out; return it; }
|
alpar@395
|
147 |
InEdgeIt& next(InEdgeIt& it) const
|
alpar@395
|
148 |
{ it.n=edges[it.n].next_in; return it; }
|
alpar@397
|
149 |
EdgeIt& next(EdgeIt& it) const {
|
alpar@397
|
150 |
if(edges[it.n].next_in!=-1) {
|
alpar@397
|
151 |
it.n=edges[it.n].next_in;
|
alpar@397
|
152 |
}
|
alpar@397
|
153 |
else {
|
alpar@397
|
154 |
int n;
|
alpar@397
|
155 |
for(n=nodes[edges[it.n].head].next;
|
alpar@397
|
156 |
n!=-1 && nodes[n].first_in == -1;
|
alpar@397
|
157 |
n = nodes[n].next) ;
|
alpar@397
|
158 |
it.n = (n==-1)?-1:nodes[n].first_in;
|
alpar@397
|
159 |
}
|
alpar@397
|
160 |
return it;
|
alpar@397
|
161 |
}
|
alpar@395
|
162 |
|
alpar@395
|
163 |
int id(Node v) const { return v.n; }
|
alpar@395
|
164 |
int id(Edge e) const { return e.n; }
|
alpar@395
|
165 |
|
alpar@397
|
166 |
/// Adds a new node to the graph.
|
alpar@397
|
167 |
|
alpar@397
|
168 |
/// \todo It adds the nodes in a reversed order.
|
alpar@397
|
169 |
/// (i.e. the lastly added node becomes the first.)
|
alpar@395
|
170 |
Node addNode() {
|
alpar@397
|
171 |
int n;
|
alpar@397
|
172 |
|
alpar@397
|
173 |
if(first_free_node==-1)
|
alpar@397
|
174 |
{
|
alpar@397
|
175 |
n = nodes.size();
|
alpar@397
|
176 |
nodes.push_back(NodeT());
|
alpar@397
|
177 |
}
|
alpar@397
|
178 |
else {
|
alpar@397
|
179 |
n = first_free_node;
|
alpar@397
|
180 |
first_free_node = nodes[n].next;
|
alpar@397
|
181 |
}
|
alpar@397
|
182 |
|
alpar@397
|
183 |
nodes[n].next = first_node;
|
alpar@397
|
184 |
if(first_node != -1) nodes[first_node].prev = n;
|
alpar@397
|
185 |
first_node = n;
|
alpar@397
|
186 |
nodes[n].prev = -1;
|
alpar@397
|
187 |
|
alpar@397
|
188 |
nodes[n].first_in = nodes[n].first_out = -1;
|
alpar@397
|
189 |
|
alpar@397
|
190 |
Node nn; nn.n=n;
|
alpar@395
|
191 |
|
alpar@397
|
192 |
//Update dynamic maps
|
deba@698
|
193 |
node_maps.add(nn);
|
alpar@395
|
194 |
|
alpar@397
|
195 |
return nn;
|
alpar@395
|
196 |
}
|
alpar@395
|
197 |
|
alpar@395
|
198 |
Edge addEdge(Node u, Node v) {
|
alpar@397
|
199 |
int n;
|
alpar@397
|
200 |
|
alpar@397
|
201 |
if(first_free_edge==-1)
|
alpar@397
|
202 |
{
|
alpar@397
|
203 |
n = edges.size();
|
alpar@397
|
204 |
edges.push_back(EdgeT());
|
alpar@397
|
205 |
}
|
alpar@397
|
206 |
else {
|
alpar@397
|
207 |
n = first_free_edge;
|
alpar@397
|
208 |
first_free_edge = edges[n].next_in;
|
alpar@397
|
209 |
}
|
alpar@397
|
210 |
|
alpar@397
|
211 |
edges[n].tail = u.n; edges[n].head = v.n;
|
alpar@395
|
212 |
|
alpar@397
|
213 |
edges[n].next_out = nodes[u.n].first_out;
|
alpar@397
|
214 |
if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
|
alpar@397
|
215 |
edges[n].next_in = nodes[v.n].first_in;
|
alpar@397
|
216 |
if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
|
alpar@397
|
217 |
edges[n].prev_in = edges[n].prev_out = -1;
|
alpar@397
|
218 |
|
alpar@397
|
219 |
nodes[u.n].first_out = nodes[v.n].first_in = n;
|
alpar@397
|
220 |
|
alpar@397
|
221 |
Edge e; e.n=n;
|
alpar@397
|
222 |
|
alpar@397
|
223 |
//Update dynamic maps
|
deba@698
|
224 |
edge_maps.add(e);
|
alpar@395
|
225 |
|
alpar@395
|
226 |
return e;
|
alpar@395
|
227 |
}
|
alpar@395
|
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;
|
alpar@397
|
249 |
}
|
alpar@397
|
250 |
|
alpar@397
|
251 |
public:
|
alpar@397
|
252 |
|
alpar@397
|
253 |
void erase(Node nn) {
|
alpar@397
|
254 |
int n=nn.n;
|
alpar@397
|
255 |
|
alpar@397
|
256 |
int m;
|
alpar@397
|
257 |
while((m=nodes[n].first_in)!=-1) eraseEdge(m);
|
alpar@397
|
258 |
while((m=nodes[n].first_out)!=-1) eraseEdge(m);
|
alpar@397
|
259 |
|
alpar@397
|
260 |
if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
|
alpar@397
|
261 |
if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
|
alpar@397
|
262 |
else first_node = nodes[n].next;
|
alpar@397
|
263 |
|
alpar@397
|
264 |
nodes[n].next = first_free_node;
|
alpar@397
|
265 |
first_free_node = n;
|
alpar@397
|
266 |
|
alpar@397
|
267 |
//Update dynamic maps
|
deba@698
|
268 |
node_maps.erase(nn);
|
deba@698
|
269 |
}
|
deba@698
|
270 |
|
deba@698
|
271 |
void erase(Edge e) {
|
deba@698
|
272 |
edge_maps.erase(e);
|
deba@698
|
273 |
eraseEdge(e.n);
|
alpar@397
|
274 |
}
|
alpar@397
|
275 |
|
alpar@397
|
276 |
///\bug Dynamic maps must be updated!
|
alpar@397
|
277 |
///
|
alpar@397
|
278 |
void clear() {
|
alpar@397
|
279 |
nodes.clear();edges.clear();
|
alpar@397
|
280 |
first_node=first_free_node=first_free_edge=-1;
|
alpar@397
|
281 |
}
|
alpar@395
|
282 |
|
alpar@395
|
283 |
class Node {
|
alpar@397
|
284 |
friend class ListGraph;
|
alpar@395
|
285 |
template <typename T> friend class NodeMap;
|
alpar@400
|
286 |
|
alpar@395
|
287 |
friend class Edge;
|
alpar@395
|
288 |
friend class OutEdgeIt;
|
alpar@395
|
289 |
friend class InEdgeIt;
|
alpar@395
|
290 |
friend class SymEdge;
|
alpar@395
|
291 |
|
alpar@395
|
292 |
protected:
|
alpar@395
|
293 |
int n;
|
alpar@397
|
294 |
friend int ListGraph::id(Node v) const;
|
alpar@395
|
295 |
Node(int nn) {n=nn;}
|
alpar@395
|
296 |
public:
|
alpar@395
|
297 |
Node() {}
|
alpar@503
|
298 |
Node (Invalid) { n=-1; }
|
alpar@395
|
299 |
bool operator==(const Node i) const {return n==i.n;}
|
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 |
};
|
alpar@395
|
303 |
|
alpar@395
|
304 |
class NodeIt : public Node {
|
alpar@397
|
305 |
friend class ListGraph;
|
alpar@395
|
306 |
public:
|
alpar@400
|
307 |
NodeIt() : Node() { }
|
alpar@400
|
308 |
NodeIt(Invalid i) : Node(i) { }
|
alpar@397
|
309 |
NodeIt(const ListGraph& G) : Node(G.first_node) { }
|
alpar@579
|
310 |
///\todo Undocumented conversion Node -\> NodeIt.
|
alpar@579
|
311 |
NodeIt(const ListGraph& G, const Node &n) : Node(n) { }
|
alpar@395
|
312 |
};
|
alpar@395
|
313 |
|
alpar@395
|
314 |
class Edge {
|
alpar@397
|
315 |
friend class ListGraph;
|
alpar@395
|
316 |
template <typename T> friend class EdgeMap;
|
alpar@395
|
317 |
|
alpar@397
|
318 |
//template <typename T> friend class SymListGraph::SymEdgeMap;
|
alpar@397
|
319 |
//friend Edge SymListGraph::opposite(Edge) const;
|
alpar@395
|
320 |
|
alpar@395
|
321 |
friend class Node;
|
alpar@395
|
322 |
friend class NodeIt;
|
alpar@395
|
323 |
protected:
|
alpar@395
|
324 |
int n;
|
alpar@397
|
325 |
friend int ListGraph::id(Edge e) const;
|
alpar@395
|
326 |
|
alpar@395
|
327 |
Edge(int nn) {n=nn;}
|
alpar@395
|
328 |
public:
|
alpar@395
|
329 |
Edge() { }
|
alpar@395
|
330 |
Edge (Invalid) { n=-1; }
|
alpar@395
|
331 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@395
|
332 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@395
|
333 |
bool operator<(const Edge i) const {return n<i.n;}
|
alpar@395
|
334 |
///\bug This is a workaround until somebody tells me how to
|
alpar@397
|
335 |
///make class \c SymListGraph::SymEdgeMap friend of Edge
|
alpar@395
|
336 |
int &idref() {return n;}
|
alpar@395
|
337 |
const int &idref() const {return n;}
|
alpar@395
|
338 |
};
|
alpar@395
|
339 |
|
alpar@395
|
340 |
class EdgeIt : public Edge {
|
alpar@397
|
341 |
friend class ListGraph;
|
alpar@395
|
342 |
public:
|
alpar@397
|
343 |
EdgeIt(const ListGraph& G) : Edge() {
|
alpar@397
|
344 |
int m;
|
alpar@397
|
345 |
for(m=G.first_node;
|
alpar@397
|
346 |
m!=-1 && G.nodes[m].first_in == -1; m = G.nodes[m].next);
|
alpar@397
|
347 |
n = (m==-1)?-1:G.nodes[m].first_in;
|
alpar@397
|
348 |
}
|
alpar@395
|
349 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@395
|
350 |
EdgeIt() : Edge() { }
|
alpar@395
|
351 |
///\bug This is a workaround until somebody tells me how to
|
alpar@397
|
352 |
///make class \c SymListGraph::SymEdgeMap friend of Edge
|
alpar@395
|
353 |
int &idref() {return n;}
|
alpar@395
|
354 |
};
|
alpar@395
|
355 |
|
alpar@395
|
356 |
class OutEdgeIt : public Edge {
|
alpar@397
|
357 |
friend class ListGraph;
|
alpar@395
|
358 |
public:
|
alpar@395
|
359 |
OutEdgeIt() : Edge() { }
|
alpar@395
|
360 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@395
|
361 |
|
alpar@397
|
362 |
OutEdgeIt(const ListGraph& G,const Node v)
|
alpar@395
|
363 |
: Edge(G.nodes[v.n].first_out) {}
|
alpar@395
|
364 |
};
|
alpar@395
|
365 |
|
alpar@395
|
366 |
class InEdgeIt : public Edge {
|
alpar@397
|
367 |
friend class ListGraph;
|
alpar@395
|
368 |
public:
|
alpar@395
|
369 |
InEdgeIt() : Edge() { }
|
alpar@395
|
370 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@681
|
371 |
InEdgeIt(const ListGraph& G,Node v) :Edge(G.nodes[v.n].first_in) {}
|
alpar@395
|
372 |
};
|
alpar@395
|
373 |
|
alpar@395
|
374 |
};
|
alpar@395
|
375 |
|
alpar@395
|
376 |
///Graph for bidirectional edges.
|
alpar@395
|
377 |
|
alpar@395
|
378 |
///The purpose of this graph structure is to handle graphs
|
alpar@395
|
379 |
///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
|
alpar@395
|
380 |
///of oppositely directed edges.
|
alpar@395
|
381 |
///There is a new edge map type called
|
alpar@397
|
382 |
///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
|
alpar@395
|
383 |
///that complements this
|
alpar@395
|
384 |
///feature by
|
alpar@395
|
385 |
///storing shared values for the edge pairs. The usual
|
alpar@395
|
386 |
///\ref GraphSkeleton::EdgeMap "EdgeMap"
|
alpar@395
|
387 |
///can be used
|
alpar@395
|
388 |
///as well.
|
alpar@395
|
389 |
///
|
alpar@395
|
390 |
///The oppositely directed edge can also be obtained easily
|
alpar@395
|
391 |
///using \ref opposite.
|
alpar@397
|
392 |
///
|
alpar@397
|
393 |
///Here erase(Edge) deletes a pair of edges.
|
alpar@397
|
394 |
///
|
alpar@397
|
395 |
///\todo this date structure need some reconsiderations. Maybe it
|
alpar@397
|
396 |
///should be implemented independently from ListGraph.
|
alpar@395
|
397 |
|
alpar@395
|
398 |
};
|
alpar@395
|
399 |
|
alpar@400
|
400 |
|
alpar@401
|
401 |
///A graph class containing only nodes.
|
alpar@400
|
402 |
|
alpar@401
|
403 |
///This class implements a graph structure without edges.
|
alpar@401
|
404 |
///The most useful application of this class is to be the node set of an
|
alpar@401
|
405 |
///\ref EdgeSet class.
|
alpar@400
|
406 |
///
|
alpar@400
|
407 |
///It conforms to the graph interface documented under
|
alpar@401
|
408 |
///the description of \ref GraphSkeleton with the exception that you cannot
|
alpar@401
|
409 |
///add (or delete) edges. The usual edge iterators are exists, but they are
|
alpar@401
|
410 |
///always \ref INVALID.
|
alpar@401
|
411 |
///\sa \ref GraphSkeleton
|
alpar@508
|
412 |
///\sa \ref EdgeSet
|
alpar@400
|
413 |
class NodeSet {
|
alpar@400
|
414 |
|
alpar@400
|
415 |
//Nodes are double linked.
|
alpar@400
|
416 |
//The free nodes are only single linked using the "next" field.
|
alpar@400
|
417 |
struct NodeT
|
alpar@400
|
418 |
{
|
alpar@400
|
419 |
int first_in,first_out;
|
alpar@400
|
420 |
int prev, next;
|
alpar@400
|
421 |
// NodeT() {}
|
alpar@400
|
422 |
};
|
alpar@400
|
423 |
|
alpar@400
|
424 |
std::vector<NodeT> nodes;
|
alpar@400
|
425 |
//The first node
|
alpar@400
|
426 |
int first_node;
|
alpar@400
|
427 |
//The first free node
|
alpar@400
|
428 |
int first_free_node;
|
alpar@400
|
429 |
|
alpar@400
|
430 |
protected:
|
alpar@400
|
431 |
|
alpar@400
|
432 |
template <typename Key> class DynMapBase
|
alpar@400
|
433 |
{
|
alpar@400
|
434 |
protected:
|
alpar@400
|
435 |
const NodeSet* G;
|
alpar@400
|
436 |
public:
|
alpar@515
|
437 |
virtual void add(const Key k) = 0;
|
alpar@515
|
438 |
virtual void erase(const Key k) = 0;
|
alpar@400
|
439 |
DynMapBase(const NodeSet &_G) : G(&_G) {}
|
alpar@400
|
440 |
virtual ~DynMapBase() {}
|
alpar@400
|
441 |
friend class NodeSet;
|
alpar@400
|
442 |
};
|
alpar@400
|
443 |
|
alpar@400
|
444 |
public:
|
alpar@400
|
445 |
template <typename T> class EdgeMap;
|
alpar@400
|
446 |
template <typename T> class NodeMap;
|
alpar@400
|
447 |
|
alpar@400
|
448 |
class Node;
|
alpar@400
|
449 |
class Edge;
|
alpar@400
|
450 |
|
alpar@400
|
451 |
// protected:
|
alpar@400
|
452 |
// HELPME:
|
alpar@400
|
453 |
protected:
|
alpar@400
|
454 |
///\bug It must be public because of SymEdgeMap.
|
alpar@400
|
455 |
///
|
alpar@400
|
456 |
mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
|
alpar@400
|
457 |
//mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
|
alpar@400
|
458 |
|
alpar@400
|
459 |
public:
|
alpar@400
|
460 |
|
alpar@400
|
461 |
class NodeIt;
|
alpar@400
|
462 |
class EdgeIt;
|
alpar@400
|
463 |
class OutEdgeIt;
|
alpar@400
|
464 |
class InEdgeIt;
|
alpar@400
|
465 |
|
alpar@400
|
466 |
template <typename T> class NodeMap;
|
alpar@400
|
467 |
template <typename T> class EdgeMap;
|
alpar@400
|
468 |
|
alpar@400
|
469 |
public:
|
alpar@400
|
470 |
|
alpar@408
|
471 |
///Default constructor
|
alpar@400
|
472 |
NodeSet() : nodes(), first_node(-1),
|
alpar@400
|
473 |
first_free_node(-1) {}
|
alpar@408
|
474 |
///Copy constructor
|
alpar@400
|
475 |
NodeSet(const NodeSet &_g) : nodes(_g.nodes), first_node(_g.first_node),
|
alpar@400
|
476 |
first_free_node(_g.first_free_node) {}
|
alpar@400
|
477 |
|
alpar@400
|
478 |
~NodeSet()
|
alpar@400
|
479 |
{
|
alpar@400
|
480 |
for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
|
alpar@400
|
481 |
i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
|
alpar@400
|
482 |
//for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
|
alpar@400
|
483 |
// i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
|
alpar@400
|
484 |
}
|
alpar@400
|
485 |
|
alpar@400
|
486 |
int nodeNum() const { return nodes.size(); } //FIXME: What is this?
|
alpar@400
|
487 |
int edgeNum() const { return 0; } //FIXME: What is this?
|
alpar@400
|
488 |
|
alpar@400
|
489 |
///\bug This function does something different than
|
alpar@400
|
490 |
///its name would suggests...
|
alpar@400
|
491 |
int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
|
alpar@400
|
492 |
///\bug This function does something different than
|
alpar@400
|
493 |
///its name would suggests...
|
alpar@400
|
494 |
int maxEdgeId() const { return 0; } //FIXME: What is this?
|
alpar@400
|
495 |
|
alpar@400
|
496 |
Node tail(Edge e) const { return INVALID; }
|
alpar@400
|
497 |
Node head(Edge e) const { return INVALID; }
|
alpar@400
|
498 |
|
alpar@400
|
499 |
Node aNode(OutEdgeIt e) const { return INVALID; }
|
alpar@400
|
500 |
Node aNode(InEdgeIt e) const { return INVALID; }
|
alpar@400
|
501 |
|
alpar@400
|
502 |
Node bNode(OutEdgeIt e) const { return INVALID; }
|
alpar@400
|
503 |
Node bNode(InEdgeIt e) const { return INVALID; }
|
alpar@400
|
504 |
|
alpar@400
|
505 |
NodeIt& first(NodeIt& v) const {
|
alpar@400
|
506 |
v=NodeIt(*this); return v; }
|
alpar@400
|
507 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@400
|
508 |
e=EdgeIt(*this); return e; }
|
alpar@400
|
509 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@400
|
510 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@400
|
511 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@400
|
512 |
e=InEdgeIt(*this,v); return e; }
|
alpar@400
|
513 |
|
alpar@400
|
514 |
// template< typename It >
|
alpar@400
|
515 |
// It first() const { It e; first(e); return e; }
|
alpar@400
|
516 |
|
alpar@400
|
517 |
// template< typename It >
|
alpar@400
|
518 |
// It first(Node v) const { It e; first(e,v); return e; }
|
alpar@400
|
519 |
|
alpar@400
|
520 |
bool valid(Edge e) const { return false; }
|
alpar@400
|
521 |
bool valid(Node n) const { return n.n!=-1; }
|
alpar@400
|
522 |
|
alpar@400
|
523 |
void setInvalid(Edge &e) { }
|
alpar@400
|
524 |
void setInvalid(Node &n) { n.n=-1; }
|
alpar@400
|
525 |
|
alpar@400
|
526 |
template <typename It> It getNext(It it) const
|
alpar@400
|
527 |
{ It tmp(it); return next(tmp); }
|
alpar@400
|
528 |
|
alpar@400
|
529 |
NodeIt& next(NodeIt& it) const {
|
alpar@400
|
530 |
it.n=nodes[it.n].next;
|
alpar@400
|
531 |
return it;
|
alpar@400
|
532 |
}
|
alpar@400
|
533 |
OutEdgeIt& next(OutEdgeIt& it) const { return it; }
|
alpar@400
|
534 |
InEdgeIt& next(InEdgeIt& it) const { return it; }
|
alpar@400
|
535 |
EdgeIt& next(EdgeIt& it) const { return it; }
|
alpar@400
|
536 |
|
alpar@400
|
537 |
int id(Node v) const { return v.n; }
|
alpar@400
|
538 |
int id(Edge e) const { return -1; }
|
alpar@400
|
539 |
|
alpar@400
|
540 |
/// Adds a new node to the graph.
|
alpar@400
|
541 |
|
alpar@400
|
542 |
/// \todo It adds the nodes in a reversed order.
|
alpar@400
|
543 |
/// (i.e. the lastly added node becomes the first.)
|
alpar@400
|
544 |
Node addNode() {
|
alpar@400
|
545 |
int n;
|
alpar@400
|
546 |
|
alpar@400
|
547 |
if(first_free_node==-1)
|
alpar@400
|
548 |
{
|
alpar@400
|
549 |
n = nodes.size();
|
alpar@400
|
550 |
nodes.push_back(NodeT());
|
alpar@400
|
551 |
}
|
alpar@400
|
552 |
else {
|
alpar@400
|
553 |
n = first_free_node;
|
alpar@400
|
554 |
first_free_node = nodes[n].next;
|
alpar@400
|
555 |
}
|
alpar@400
|
556 |
|
alpar@400
|
557 |
nodes[n].next = first_node;
|
alpar@400
|
558 |
if(first_node != -1) nodes[first_node].prev = n;
|
alpar@400
|
559 |
first_node = n;
|
alpar@400
|
560 |
nodes[n].prev = -1;
|
alpar@400
|
561 |
|
alpar@400
|
562 |
nodes[n].first_in = nodes[n].first_out = -1;
|
alpar@400
|
563 |
|
alpar@400
|
564 |
Node nn; nn.n=n;
|
alpar@400
|
565 |
|
alpar@400
|
566 |
//Update dynamic maps
|
alpar@400
|
567 |
for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
|
alpar@400
|
568 |
i!=dyn_node_maps.end(); ++i) (**i).add(nn);
|
alpar@400
|
569 |
|
alpar@400
|
570 |
return nn;
|
alpar@400
|
571 |
}
|
alpar@400
|
572 |
|
alpar@400
|
573 |
void erase(Node nn) {
|
alpar@400
|
574 |
int n=nn.n;
|
alpar@400
|
575 |
|
alpar@400
|
576 |
if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
|
alpar@400
|
577 |
if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
|
alpar@400
|
578 |
else first_node = nodes[n].next;
|
alpar@400
|
579 |
|
alpar@400
|
580 |
nodes[n].next = first_free_node;
|
alpar@400
|
581 |
first_free_node = n;
|
alpar@400
|
582 |
|
alpar@400
|
583 |
//Update dynamic maps
|
alpar@400
|
584 |
for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
|
alpar@400
|
585 |
i!=dyn_node_maps.end(); ++i) (**i).erase(nn);
|
alpar@400
|
586 |
}
|
alpar@400
|
587 |
|
alpar@400
|
588 |
///\bug Dynamic maps must be updated!
|
alpar@400
|
589 |
///
|
alpar@400
|
590 |
void clear() {
|
alpar@400
|
591 |
nodes.clear();
|
alpar@400
|
592 |
first_node = first_free_node = -1;
|
alpar@400
|
593 |
}
|
alpar@400
|
594 |
|
alpar@400
|
595 |
class Node {
|
alpar@400
|
596 |
friend class NodeSet;
|
alpar@400
|
597 |
template <typename T> friend class NodeMap;
|
alpar@400
|
598 |
|
alpar@400
|
599 |
friend class Edge;
|
alpar@400
|
600 |
friend class OutEdgeIt;
|
alpar@400
|
601 |
friend class InEdgeIt;
|
alpar@400
|
602 |
|
alpar@400
|
603 |
protected:
|
alpar@400
|
604 |
int n;
|
alpar@400
|
605 |
friend int NodeSet::id(Node v) const;
|
alpar@400
|
606 |
Node(int nn) {n=nn;}
|
alpar@400
|
607 |
public:
|
alpar@400
|
608 |
Node() {}
|
alpar@400
|
609 |
Node (Invalid i) { n=-1; }
|
alpar@400
|
610 |
bool operator==(const Node i) const {return n==i.n;}
|
alpar@400
|
611 |
bool operator!=(const Node i) const {return n!=i.n;}
|
alpar@400
|
612 |
bool operator<(const Node i) const {return n<i.n;}
|
alpar@400
|
613 |
};
|
alpar@400
|
614 |
|
alpar@400
|
615 |
class NodeIt : public Node {
|
alpar@400
|
616 |
friend class NodeSet;
|
alpar@400
|
617 |
public:
|
alpar@579
|
618 |
NodeIt() : Node() { }
|
alpar@579
|
619 |
NodeIt(Invalid i) : Node(i) { }
|
alpar@400
|
620 |
NodeIt(const NodeSet& G) : Node(G.first_node) { }
|
alpar@579
|
621 |
///\todo Undocumented conversion Node -\> NodeIt.
|
alpar@579
|
622 |
NodeIt(const NodeSet& G, const Node &n) : Node(n) { }
|
alpar@579
|
623 |
|
alpar@400
|
624 |
};
|
alpar@400
|
625 |
|
alpar@400
|
626 |
class Edge {
|
alpar@400
|
627 |
//friend class NodeSet;
|
alpar@400
|
628 |
//template <typename T> friend class EdgeMap;
|
alpar@400
|
629 |
|
alpar@400
|
630 |
//template <typename T> friend class SymNodeSet::SymEdgeMap;
|
alpar@400
|
631 |
//friend Edge SymNodeSet::opposite(Edge) const;
|
alpar@400
|
632 |
|
alpar@400
|
633 |
// friend class Node;
|
alpar@400
|
634 |
// friend class NodeIt;
|
alpar@400
|
635 |
protected:
|
alpar@400
|
636 |
//friend int NodeSet::id(Edge e) const;
|
alpar@400
|
637 |
// Edge(int nn) {}
|
alpar@400
|
638 |
public:
|
alpar@400
|
639 |
Edge() { }
|
alpar@400
|
640 |
Edge (Invalid) { }
|
alpar@400
|
641 |
bool operator==(const Edge i) const {return true;}
|
alpar@400
|
642 |
bool operator!=(const Edge i) const {return false;}
|
alpar@400
|
643 |
bool operator<(const Edge i) const {return false;}
|
alpar@400
|
644 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
645 |
///make class \c SymNodeSet::SymEdgeMap friend of Edge
|
alpar@400
|
646 |
// int idref() {return -1;}
|
alpar@400
|
647 |
// int idref() const {return -1;}
|
alpar@400
|
648 |
};
|
alpar@400
|
649 |
|
alpar@400
|
650 |
class EdgeIt : public Edge {
|
alpar@400
|
651 |
//friend class NodeSet;
|
alpar@400
|
652 |
public:
|
alpar@400
|
653 |
EdgeIt(const NodeSet& G) : Edge() { }
|
alpar@400
|
654 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
655 |
EdgeIt() : Edge() { }
|
alpar@400
|
656 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
657 |
///make class \c SymNodeSet::SymEdgeMap friend of Edge
|
alpar@400
|
658 |
// int idref() {return -1;}
|
alpar@400
|
659 |
};
|
alpar@400
|
660 |
|
alpar@400
|
661 |
class OutEdgeIt : public Edge {
|
alpar@400
|
662 |
friend class NodeSet;
|
alpar@400
|
663 |
public:
|
alpar@400
|
664 |
OutEdgeIt() : Edge() { }
|
alpar@400
|
665 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
666 |
OutEdgeIt(const NodeSet& G,const Node v) : Edge() {}
|
alpar@400
|
667 |
};
|
alpar@400
|
668 |
|
alpar@400
|
669 |
class InEdgeIt : public Edge {
|
alpar@400
|
670 |
friend class NodeSet;
|
alpar@400
|
671 |
public:
|
alpar@400
|
672 |
InEdgeIt() : Edge() { }
|
alpar@400
|
673 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
674 |
InEdgeIt(const NodeSet& G,Node v) :Edge() {}
|
alpar@400
|
675 |
};
|
alpar@400
|
676 |
|
alpar@400
|
677 |
template <typename T> class NodeMap : public DynMapBase<Node>
|
alpar@400
|
678 |
{
|
alpar@400
|
679 |
std::vector<T> container;
|
alpar@400
|
680 |
|
alpar@400
|
681 |
public:
|
alpar@400
|
682 |
typedef T ValueType;
|
alpar@400
|
683 |
typedef Node KeyType;
|
alpar@400
|
684 |
|
alpar@400
|
685 |
NodeMap(const NodeSet &_G) :
|
alpar@400
|
686 |
DynMapBase<Node>(_G), container(_G.maxNodeId())
|
alpar@400
|
687 |
{
|
alpar@400
|
688 |
G->dyn_node_maps.push_back(this);
|
alpar@400
|
689 |
}
|
alpar@400
|
690 |
NodeMap(const NodeSet &_G,const T &t) :
|
alpar@400
|
691 |
DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
|
alpar@400
|
692 |
{
|
alpar@400
|
693 |
G->dyn_node_maps.push_back(this);
|
alpar@400
|
694 |
}
|
alpar@400
|
695 |
|
alpar@400
|
696 |
NodeMap(const NodeMap<T> &m) :
|
alpar@400
|
697 |
DynMapBase<Node>(*m.G), container(m.container)
|
alpar@400
|
698 |
{
|
alpar@400
|
699 |
G->dyn_node_maps.push_back(this);
|
alpar@400
|
700 |
}
|
alpar@400
|
701 |
|
alpar@400
|
702 |
template<typename TT> friend class NodeMap;
|
alpar@400
|
703 |
|
alpar@400
|
704 |
///\todo It can copy between different types.
|
alpar@400
|
705 |
///
|
alpar@400
|
706 |
template<typename TT> NodeMap(const NodeMap<TT> &m) :
|
alpar@590
|
707 |
DynMapBase<Node>(*m.G), container(m.container.size())
|
alpar@400
|
708 |
{
|
alpar@400
|
709 |
G->dyn_node_maps.push_back(this);
|
alpar@400
|
710 |
typename std::vector<TT>::const_iterator i;
|
alpar@400
|
711 |
for(typename std::vector<TT>::const_iterator i=m.container.begin();
|
alpar@400
|
712 |
i!=m.container.end();
|
alpar@400
|
713 |
i++)
|
alpar@400
|
714 |
container.push_back(*i);
|
alpar@400
|
715 |
}
|
alpar@400
|
716 |
~NodeMap()
|
alpar@400
|
717 |
{
|
alpar@400
|
718 |
if(G) {
|
alpar@400
|
719 |
std::vector<DynMapBase<Node>* >::iterator i;
|
alpar@400
|
720 |
for(i=G->dyn_node_maps.begin();
|
alpar@400
|
721 |
i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
|
alpar@400
|
722 |
//if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
|
alpar@400
|
723 |
//A better way to do that: (Is this really important?)
|
alpar@400
|
724 |
if(*i==this) {
|
alpar@400
|
725 |
*i=G->dyn_node_maps.back();
|
alpar@400
|
726 |
G->dyn_node_maps.pop_back();
|
alpar@400
|
727 |
}
|
alpar@400
|
728 |
}
|
alpar@400
|
729 |
}
|
alpar@400
|
730 |
|
alpar@400
|
731 |
void add(const Node k)
|
alpar@400
|
732 |
{
|
alpar@400
|
733 |
if(k.n>=int(container.size())) container.resize(k.n+1);
|
alpar@400
|
734 |
}
|
alpar@400
|
735 |
|
alpar@400
|
736 |
void erase(const Node) { }
|
alpar@400
|
737 |
|
alpar@400
|
738 |
void set(Node n, T a) { container[n.n]=a; }
|
alpar@400
|
739 |
//'T& operator[](Node n)' would be wrong here
|
alpar@400
|
740 |
typename std::vector<T>::reference
|
alpar@400
|
741 |
operator[](Node n) { return container[n.n]; }
|
alpar@400
|
742 |
//'const T& operator[](Node n)' would be wrong here
|
alpar@400
|
743 |
typename std::vector<T>::const_reference
|
alpar@400
|
744 |
operator[](Node n) const { return container[n.n]; }
|
alpar@400
|
745 |
|
alpar@400
|
746 |
///\warning There is no safety check at all!
|
alpar@400
|
747 |
///Using operator = between maps attached to different graph may
|
alpar@400
|
748 |
///cause serious problem.
|
alpar@400
|
749 |
///\todo Is this really so?
|
alpar@400
|
750 |
///\todo It can copy between different types.
|
alpar@400
|
751 |
const NodeMap<T>& operator=(const NodeMap<T> &m)
|
alpar@400
|
752 |
{
|
alpar@400
|
753 |
container = m.container;
|
alpar@400
|
754 |
return *this;
|
alpar@400
|
755 |
}
|
alpar@400
|
756 |
template<typename TT>
|
alpar@400
|
757 |
const NodeMap<T>& operator=(const NodeMap<TT> &m)
|
alpar@400
|
758 |
{
|
alpar@531
|
759 |
std::copy(m.container.begin(), m.container.end(), container.begin());
|
alpar@400
|
760 |
return *this;
|
alpar@400
|
761 |
}
|
alpar@400
|
762 |
|
alpar@400
|
763 |
void update() {} //Useless for Dynamic Maps
|
alpar@400
|
764 |
void update(T a) {} //Useless for Dynamic Maps
|
alpar@400
|
765 |
};
|
alpar@400
|
766 |
|
alpar@400
|
767 |
template <typename T> class EdgeMap
|
alpar@400
|
768 |
{
|
alpar@400
|
769 |
public:
|
alpar@400
|
770 |
typedef T ValueType;
|
alpar@400
|
771 |
typedef Edge KeyType;
|
alpar@400
|
772 |
|
alpar@400
|
773 |
EdgeMap(const NodeSet &) { }
|
alpar@400
|
774 |
EdgeMap(const NodeSet &,const T &) { }
|
alpar@400
|
775 |
EdgeMap(const EdgeMap<T> &) { }
|
alpar@400
|
776 |
// template<typename TT> friend class EdgeMap;
|
alpar@400
|
777 |
|
alpar@400
|
778 |
///\todo It can copy between different types.
|
alpar@400
|
779 |
///
|
alpar@400
|
780 |
template<typename TT> EdgeMap(const EdgeMap<TT> &) { }
|
alpar@400
|
781 |
~EdgeMap() { }
|
alpar@400
|
782 |
|
alpar@400
|
783 |
void add(const Edge ) { }
|
alpar@400
|
784 |
void erase(const Edge) { }
|
alpar@400
|
785 |
|
alpar@400
|
786 |
void set(Edge, T) { }
|
alpar@400
|
787 |
//T get(Edge n) const { return container[n.n]; }
|
alpar@400
|
788 |
ValueType &operator[](Edge) { return *((T*)(NULL)); }
|
alpar@400
|
789 |
const ValueType &operator[](Edge) const { return *((T*)(NULL)); }
|
alpar@400
|
790 |
|
alpar@400
|
791 |
const EdgeMap<T>& operator=(const EdgeMap<T> &) { return *this; }
|
alpar@400
|
792 |
|
alpar@400
|
793 |
template<typename TT>
|
alpar@400
|
794 |
const EdgeMap<T>& operator=(const EdgeMap<TT> &m) { return *this; }
|
alpar@400
|
795 |
|
alpar@400
|
796 |
void update() {}
|
alpar@400
|
797 |
void update(T a) {}
|
alpar@400
|
798 |
};
|
alpar@400
|
799 |
};
|
alpar@400
|
800 |
|
alpar@400
|
801 |
|
alpar@400
|
802 |
|
alpar@401
|
803 |
///Graph structure using a node set of another graph.
|
alpar@401
|
804 |
|
alpar@401
|
805 |
///This structure can be used to establish another graph over a node set
|
alpar@401
|
806 |
/// of an existing one. The node iterator will go through the nodes of the
|
alpar@401
|
807 |
/// original graph, and the NodeMap's of both graphs will convert to
|
alpar@401
|
808 |
/// each other.
|
alpar@401
|
809 |
///
|
alpar@404
|
810 |
///\warning Adding or deleting nodes from the graph is not safe if an
|
alpar@404
|
811 |
///\ref EdgeSet is currently attached to it!
|
alpar@404
|
812 |
///
|
alpar@404
|
813 |
///\todo Make it possible to add/delete edges from the base graph
|
alpar@404
|
814 |
///(and from \ref EdgeSet, as well)
|
alpar@404
|
815 |
///
|
alpar@401
|
816 |
///\param GG The type of the graph which shares its node set with this class.
|
alpar@401
|
817 |
///Its interface must conform with \ref GraphSkeleton.
|
alpar@400
|
818 |
///
|
alpar@400
|
819 |
///It conforms to the graph interface documented under
|
alpar@400
|
820 |
///the description of \ref GraphSkeleton.
|
alpar@400
|
821 |
///\sa \ref GraphSkeleton.
|
alpar@401
|
822 |
///\sa \ref NodeSet.
|
alpar@400
|
823 |
template<typename GG>
|
alpar@400
|
824 |
class EdgeSet {
|
alpar@400
|
825 |
|
alpar@400
|
826 |
typedef GG NodeGraphType;
|
alpar@400
|
827 |
|
alpar@400
|
828 |
NodeGraphType &G;
|
alpar@400
|
829 |
|
alpar@515
|
830 |
public:
|
alpar@400
|
831 |
class Node;
|
alpar@531
|
832 |
int id(Node v) const;
|
alpar@531
|
833 |
|
alpar@531
|
834 |
class Node : public NodeGraphType::Node {
|
alpar@531
|
835 |
friend class EdgeSet;
|
alpar@531
|
836 |
// template <typename T> friend class NodeMap;
|
alpar@531
|
837 |
|
alpar@531
|
838 |
friend class Edge;
|
alpar@531
|
839 |
friend class OutEdgeIt;
|
alpar@531
|
840 |
friend class InEdgeIt;
|
alpar@531
|
841 |
friend class SymEdge;
|
alpar@531
|
842 |
|
alpar@531
|
843 |
public:
|
alpar@531
|
844 |
friend int EdgeSet::id(Node v) const;
|
alpar@531
|
845 |
// Node(int nn) {n=nn;}
|
alpar@531
|
846 |
public:
|
alpar@531
|
847 |
Node() : NodeGraphType::Node() {}
|
alpar@531
|
848 |
Node (Invalid i) : NodeGraphType::Node(i) {}
|
alpar@531
|
849 |
Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
|
alpar@531
|
850 |
};
|
alpar@531
|
851 |
|
alpar@531
|
852 |
class NodeIt : public NodeGraphType::NodeIt {
|
alpar@531
|
853 |
friend class EdgeSet;
|
alpar@531
|
854 |
public:
|
alpar@531
|
855 |
NodeIt() : NodeGraphType::NodeIt() { }
|
alpar@531
|
856 |
NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
|
alpar@531
|
857 |
NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
|
alpar@531
|
858 |
NodeIt(const typename NodeGraphType::NodeIt &n)
|
alpar@531
|
859 |
: NodeGraphType::NodeIt(n) {}
|
alpar@579
|
860 |
///\todo Undocumented conversion Node -\> NodeIt.
|
alpar@579
|
861 |
NodeIt(const EdgeSet& _G, const Node &n)
|
alpar@579
|
862 |
: NodeGraphType::NodeIt(_G.G,n) { }
|
alpar@579
|
863 |
|
alpar@531
|
864 |
operator Node() { return Node(*this);}
|
alpar@531
|
865 |
};
|
alpar@515
|
866 |
|
alpar@515
|
867 |
private:
|
alpar@400
|
868 |
//Edges are double linked.
|
alpar@400
|
869 |
//The free edges are only single linked using the "next_in" field.
|
alpar@400
|
870 |
struct NodeT
|
alpar@400
|
871 |
{
|
alpar@400
|
872 |
int first_in,first_out;
|
alpar@400
|
873 |
NodeT() : first_in(-1), first_out(-1) { }
|
alpar@400
|
874 |
};
|
alpar@400
|
875 |
|
alpar@400
|
876 |
struct EdgeT
|
alpar@400
|
877 |
{
|
alpar@400
|
878 |
Node head, tail;
|
alpar@400
|
879 |
int prev_in, prev_out;
|
alpar@400
|
880 |
int next_in, next_out;
|
alpar@400
|
881 |
};
|
alpar@400
|
882 |
|
alpar@400
|
883 |
|
alpar@515
|
884 |
typename NodeGraphType::template NodeMap<NodeT> nodes;
|
alpar@400
|
885 |
|
alpar@400
|
886 |
std::vector<EdgeT> edges;
|
alpar@400
|
887 |
//The first free edge
|
alpar@400
|
888 |
int first_free_edge;
|
alpar@400
|
889 |
|
alpar@400
|
890 |
protected:
|
alpar@400
|
891 |
|
alpar@400
|
892 |
template <typename Key> class DynMapBase
|
alpar@400
|
893 |
{
|
alpar@400
|
894 |
protected:
|
alpar@400
|
895 |
const EdgeSet* G;
|
alpar@400
|
896 |
public:
|
alpar@515
|
897 |
virtual void add(const Key k) = 0;
|
alpar@515
|
898 |
virtual void erase(const Key k) = 0;
|
alpar@400
|
899 |
DynMapBase(const EdgeSet &_G) : G(&_G) {}
|
alpar@400
|
900 |
virtual ~DynMapBase() {}
|
alpar@400
|
901 |
friend class EdgeSet;
|
alpar@400
|
902 |
};
|
alpar@400
|
903 |
|
alpar@400
|
904 |
public:
|
alpar@400
|
905 |
//template <typename T> class NodeMap;
|
alpar@400
|
906 |
template <typename T> class EdgeMap;
|
alpar@400
|
907 |
|
alpar@400
|
908 |
class Node;
|
alpar@400
|
909 |
class Edge;
|
alpar@400
|
910 |
|
alpar@400
|
911 |
// protected:
|
alpar@400
|
912 |
// HELPME:
|
alpar@400
|
913 |
protected:
|
alpar@400
|
914 |
// mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
|
alpar@400
|
915 |
///\bug It must be public because of SymEdgeMap.
|
alpar@400
|
916 |
///
|
alpar@400
|
917 |
mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
|
alpar@400
|
918 |
|
alpar@400
|
919 |
public:
|
alpar@400
|
920 |
|
alpar@400
|
921 |
class NodeIt;
|
alpar@400
|
922 |
class EdgeIt;
|
alpar@400
|
923 |
class OutEdgeIt;
|
alpar@400
|
924 |
class InEdgeIt;
|
alpar@400
|
925 |
|
alpar@400
|
926 |
template <typename T> class NodeMap;
|
alpar@400
|
927 |
template <typename T> class EdgeMap;
|
alpar@400
|
928 |
|
alpar@400
|
929 |
public:
|
alpar@400
|
930 |
|
alpar@408
|
931 |
///Constructor
|
alpar@408
|
932 |
|
alpar@408
|
933 |
///Construates a new graph based on the nodeset of an existing one.
|
alpar@408
|
934 |
///\param _G the base graph.
|
alpar@408
|
935 |
///\todo It looks like a copy constructor, but it isn't.
|
alpar@401
|
936 |
EdgeSet(NodeGraphType &_G) : G(_G),
|
alpar@401
|
937 |
nodes(_G), edges(),
|
alpar@401
|
938 |
first_free_edge(-1) { }
|
alpar@408
|
939 |
///Copy constructor
|
alpar@408
|
940 |
|
alpar@408
|
941 |
///Makes a copy of an EdgeSet.
|
alpar@408
|
942 |
///It will be based on the same graph.
|
alpar@400
|
943 |
EdgeSet(const EdgeSet &_g) : G(_g.G), nodes(_g.G), edges(_g.edges),
|
alpar@401
|
944 |
first_free_edge(_g.first_free_edge) { }
|
alpar@400
|
945 |
|
alpar@400
|
946 |
~EdgeSet()
|
alpar@400
|
947 |
{
|
alpar@400
|
948 |
// for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
|
alpar@400
|
949 |
// i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
|
alpar@400
|
950 |
for(typename std::vector<DynMapBase<Edge> * >::iterator
|
alpar@400
|
951 |
i=dyn_edge_maps.begin();
|
alpar@400
|
952 |
i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
|
alpar@400
|
953 |
}
|
alpar@400
|
954 |
|
alpar@400
|
955 |
int nodeNum() const { return G.nodeNum(); } //FIXME: What is this?
|
alpar@400
|
956 |
int edgeNum() const { return edges.size(); } //FIXME: What is this?
|
alpar@400
|
957 |
|
alpar@400
|
958 |
///\bug This function does something different than
|
alpar@400
|
959 |
///its name would suggests...
|
alpar@400
|
960 |
int maxNodeId() const { return G.maxNodeId(); } //FIXME: What is this?
|
alpar@400
|
961 |
///\bug This function does something different than
|
alpar@400
|
962 |
///its name would suggests...
|
alpar@400
|
963 |
int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
|
alpar@400
|
964 |
|
alpar@400
|
965 |
Node tail(Edge e) const { return edges[e.n].tail; }
|
alpar@400
|
966 |
Node head(Edge e) const { return edges[e.n].head; }
|
alpar@400
|
967 |
|
alpar@400
|
968 |
Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
|
alpar@400
|
969 |
Node aNode(InEdgeIt e) const { return edges[e.n].head; }
|
alpar@400
|
970 |
|
alpar@400
|
971 |
Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
|
alpar@400
|
972 |
Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
|
alpar@400
|
973 |
|
alpar@400
|
974 |
NodeIt& first(NodeIt& v) const {
|
alpar@400
|
975 |
v=NodeIt(*this); return v; }
|
alpar@400
|
976 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@400
|
977 |
e=EdgeIt(*this); return e; }
|
alpar@400
|
978 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@400
|
979 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@400
|
980 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@400
|
981 |
e=InEdgeIt(*this,v); return e; }
|
alpar@400
|
982 |
|
alpar@400
|
983 |
// template< typename It >
|
alpar@400
|
984 |
// It first() const { It e; first(e); return e; }
|
alpar@400
|
985 |
|
alpar@400
|
986 |
// template< typename It >
|
alpar@400
|
987 |
// It first(Node v) const { It e; first(e,v); return e; }
|
alpar@400
|
988 |
|
alpar@400
|
989 |
bool valid(Edge e) const { return e.n!=-1; }
|
alpar@400
|
990 |
bool valid(Node n) const { return G.valid(n); }
|
alpar@400
|
991 |
|
alpar@400
|
992 |
void setInvalid(Edge &e) { e.n=-1; }
|
alpar@400
|
993 |
void setInvalid(Node &n) { G.setInvalid(n); }
|
alpar@400
|
994 |
|
alpar@400
|
995 |
template <typename It> It getNext(It it) const
|
alpar@400
|
996 |
{ It tmp(it); return next(tmp); }
|
alpar@400
|
997 |
|
alpar@400
|
998 |
NodeIt& next(NodeIt& it) const { G.next(it); return it; }
|
alpar@400
|
999 |
OutEdgeIt& next(OutEdgeIt& it) const
|
alpar@400
|
1000 |
{ it.n=edges[it.n].next_out; return it; }
|
alpar@400
|
1001 |
InEdgeIt& next(InEdgeIt& it) const
|
alpar@400
|
1002 |
{ it.n=edges[it.n].next_in; return it; }
|
alpar@400
|
1003 |
EdgeIt& next(EdgeIt& it) const {
|
alpar@400
|
1004 |
if(edges[it.n].next_in!=-1) {
|
alpar@400
|
1005 |
it.n=edges[it.n].next_in;
|
alpar@400
|
1006 |
}
|
alpar@400
|
1007 |
else {
|
alpar@579
|
1008 |
NodeIt n(*this,edges[it.n].head);
|
alpar@579
|
1009 |
for(n=next(n);
|
alpar@503
|
1010 |
valid(n) && nodes[n].first_in == -1;
|
alpar@503
|
1011 |
next(n)) ;
|
alpar@503
|
1012 |
it.n = (valid(n))?-1:nodes[n].first_in;
|
alpar@400
|
1013 |
}
|
alpar@400
|
1014 |
return it;
|
alpar@400
|
1015 |
}
|
alpar@400
|
1016 |
|
alpar@400
|
1017 |
int id(Edge e) const { return e.n; }
|
alpar@400
|
1018 |
|
alpar@400
|
1019 |
/// Adds a new node to the graph.
|
alpar@579
|
1020 |
Node addNode() { return G.addNode(); }
|
alpar@400
|
1021 |
|
alpar@400
|
1022 |
Edge addEdge(Node u, Node v) {
|
alpar@400
|
1023 |
int n;
|
alpar@400
|
1024 |
|
alpar@400
|
1025 |
if(first_free_edge==-1)
|
alpar@400
|
1026 |
{
|
alpar@400
|
1027 |
n = edges.size();
|
alpar@400
|
1028 |
edges.push_back(EdgeT());
|
alpar@400
|
1029 |
}
|
alpar@400
|
1030 |
else {
|
alpar@400
|
1031 |
n = first_free_edge;
|
alpar@400
|
1032 |
first_free_edge = edges[n].next_in;
|
alpar@400
|
1033 |
}
|
alpar@400
|
1034 |
|
alpar@401
|
1035 |
edges[n].tail = u; edges[n].head = v;
|
alpar@400
|
1036 |
|
alpar@401
|
1037 |
edges[n].next_out = nodes[u].first_out;
|
alpar@401
|
1038 |
if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
|
alpar@401
|
1039 |
edges[n].next_in = nodes[v].first_in;
|
alpar@401
|
1040 |
if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
|
alpar@400
|
1041 |
edges[n].prev_in = edges[n].prev_out = -1;
|
alpar@400
|
1042 |
|
alpar@401
|
1043 |
nodes[u].first_out = nodes[v].first_in = n;
|
alpar@400
|
1044 |
|
alpar@400
|
1045 |
Edge e; e.n=n;
|
alpar@400
|
1046 |
|
alpar@400
|
1047 |
//Update dynamic maps
|
alpar@400
|
1048 |
for(typename std::vector<DynMapBase<Edge> * >::iterator
|
alpar@400
|
1049 |
i=dyn_edge_maps.begin();
|
alpar@400
|
1050 |
i!=dyn_edge_maps.end(); ++i) (**i).add(e);
|
alpar@400
|
1051 |
|
alpar@400
|
1052 |
return e;
|
alpar@400
|
1053 |
}
|
alpar@400
|
1054 |
|
alpar@400
|
1055 |
private:
|
alpar@400
|
1056 |
void eraseEdge(int n) {
|
alpar@400
|
1057 |
|
alpar@400
|
1058 |
if(edges[n].next_in!=-1)
|
alpar@400
|
1059 |
edges[edges[n].next_in].prev_in = edges[n].prev_in;
|
alpar@400
|
1060 |
if(edges[n].prev_in!=-1)
|
alpar@400
|
1061 |
edges[edges[n].prev_in].next_in = edges[n].next_in;
|
alpar@400
|
1062 |
else nodes[edges[n].head].first_in = edges[n].next_in;
|
alpar@400
|
1063 |
|
alpar@400
|
1064 |
if(edges[n].next_out!=-1)
|
alpar@400
|
1065 |
edges[edges[n].next_out].prev_out = edges[n].prev_out;
|
alpar@400
|
1066 |
if(edges[n].prev_out!=-1)
|
alpar@400
|
1067 |
edges[edges[n].prev_out].next_out = edges[n].next_out;
|
alpar@400
|
1068 |
else nodes[edges[n].tail].first_out = edges[n].next_out;
|
alpar@400
|
1069 |
|
alpar@400
|
1070 |
edges[n].next_in = first_free_edge;
|
alpar@400
|
1071 |
first_free_edge = -1;
|
alpar@400
|
1072 |
|
alpar@400
|
1073 |
//Update dynamic maps
|
alpar@400
|
1074 |
Edge e; e.n=n;
|
alpar@400
|
1075 |
for(typename std::vector<DynMapBase<Edge> * >::iterator
|
alpar@400
|
1076 |
i=dyn_edge_maps.begin();
|
alpar@400
|
1077 |
i!=dyn_edge_maps.end(); ++i) (**i).erase(e);
|
alpar@400
|
1078 |
}
|
alpar@400
|
1079 |
|
alpar@400
|
1080 |
public:
|
alpar@400
|
1081 |
|
alpar@400
|
1082 |
// void erase(Node nn) {
|
alpar@400
|
1083 |
// int n=nn.n;
|
alpar@400
|
1084 |
// int m;
|
alpar@400
|
1085 |
// while((m=nodes[n].first_in)!=-1) eraseEdge(m);
|
alpar@400
|
1086 |
// while((m=nodes[n].first_out)!=-1) eraseEdge(m);
|
alpar@400
|
1087 |
// }
|
alpar@400
|
1088 |
|
alpar@400
|
1089 |
void erase(Edge e) { eraseEdge(e.n); }
|
alpar@400
|
1090 |
|
alpar@579
|
1091 |
///Clear all edges. (Doesn't clear the nodes!)
|
alpar@579
|
1092 |
void clear() {
|
alpar@579
|
1093 |
edges.clear();
|
alpar@579
|
1094 |
first_free_edge=-1;
|
alpar@579
|
1095 |
}
|
alpar@579
|
1096 |
|
alpar@579
|
1097 |
|
alpar@400
|
1098 |
// //\bug Dynamic maps must be updated!
|
alpar@400
|
1099 |
// //
|
alpar@400
|
1100 |
// void clear() {
|
alpar@400
|
1101 |
// nodes.clear();edges.clear();
|
alpar@400
|
1102 |
// first_node=first_free_node=first_free_edge=-1;
|
alpar@400
|
1103 |
// }
|
alpar@400
|
1104 |
|
alpar@579
|
1105 |
public:
|
alpar@579
|
1106 |
template <typename T> class EdgeMap;
|
alpar@579
|
1107 |
|
alpar@579
|
1108 |
///
|
alpar@400
|
1109 |
class Edge {
|
alpar@579
|
1110 |
public:
|
alpar@400
|
1111 |
friend class EdgeSet;
|
alpar@400
|
1112 |
template <typename T> friend class EdgeMap;
|
alpar@400
|
1113 |
|
alpar@400
|
1114 |
friend class Node;
|
alpar@400
|
1115 |
friend class NodeIt;
|
alpar@579
|
1116 |
public:
|
alpar@579
|
1117 |
///\bug It shoud be at least protected
|
alpar@579
|
1118 |
///
|
alpar@579
|
1119 |
int n;
|
alpar@400
|
1120 |
protected:
|
alpar@400
|
1121 |
friend int EdgeSet::id(Edge e) const;
|
alpar@400
|
1122 |
|
alpar@400
|
1123 |
Edge(int nn) {n=nn;}
|
alpar@400
|
1124 |
public:
|
alpar@400
|
1125 |
Edge() { }
|
alpar@400
|
1126 |
Edge (Invalid) { n=-1; }
|
alpar@400
|
1127 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@400
|
1128 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@400
|
1129 |
bool operator<(const Edge i) const {return n<i.n;}
|
alpar@400
|
1130 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
1131 |
///make class \c SymEdgeSet::SymEdgeMap friend of Edge
|
alpar@400
|
1132 |
int &idref() {return n;}
|
alpar@400
|
1133 |
const int &idref() const {return n;}
|
alpar@400
|
1134 |
};
|
alpar@400
|
1135 |
|
alpar@400
|
1136 |
class EdgeIt : public Edge {
|
alpar@400
|
1137 |
friend class EdgeSet;
|
alpar@579
|
1138 |
template <typename T> friend class EdgeMap;
|
alpar@579
|
1139 |
|
alpar@579
|
1140 |
|
alpar@400
|
1141 |
public:
|
alpar@400
|
1142 |
EdgeIt(const EdgeSet& G) : Edge() {
|
alpar@503
|
1143 |
// typename NodeGraphType::Node m;
|
alpar@503
|
1144 |
NodeIt m;
|
alpar@400
|
1145 |
for(G.first(m);
|
alpar@503
|
1146 |
G.valid(m) && G.nodes[m].first_in == -1; G.next(m));
|
alpar@515
|
1147 |
//AJJAJ! This is a non sense!!!!!!!
|
alpar@515
|
1148 |
this->n = G.valid(m)?-1:G.nodes[m].first_in;
|
alpar@400
|
1149 |
}
|
alpar@400
|
1150 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
1151 |
EdgeIt() : Edge() { }
|
alpar@400
|
1152 |
///\bug This is a workaround until somebody tells me how to
|
alpar@400
|
1153 |
///make class \c SymEdgeSet::SymEdgeMap friend of Edge
|
alpar@515
|
1154 |
int &idref() {return this->n;}
|
alpar@400
|
1155 |
};
|
alpar@400
|
1156 |
|
alpar@400
|
1157 |
class OutEdgeIt : public Edge {
|
alpar@400
|
1158 |
friend class EdgeSet;
|
alpar@400
|
1159 |
public:
|
alpar@400
|
1160 |
OutEdgeIt() : Edge() { }
|
alpar@400
|
1161 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@400
|
1162 |
|
alpar@579
|
1163 |
OutEdgeIt(const EdgeSet& G,const Node v) : Edge(G.nodes[v].first_out) { }
|
alpar@400
|
1164 |
};
|
alpar@400
|
1165 |
|
alpar@400
|
1166 |
class InEdgeIt : public Edge {
|
alpar@400
|
1167 |
friend class EdgeSet;
|
alpar@400
|
1168 |
public:
|
alpar@400
|
1169 |
InEdgeIt() : Edge() { }
|
alpar@400
|
1170 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@579
|
1171 |
InEdgeIt(const EdgeSet& G,Node v) :Edge(G.nodes[v].first_in) { }
|
alpar@400
|
1172 |
};
|
alpar@400
|
1173 |
|
alpar@515
|
1174 |
template <typename T> class NodeMap :
|
alpar@515
|
1175 |
public NodeGraphType::template NodeMap<T>
|
alpar@400
|
1176 |
{
|
alpar@579
|
1177 |
//This is a must, the constructors need it.
|
alpar@579
|
1178 |
typedef typename NodeGraphType::template NodeMap<T> ParentNodeMap;
|
alpar@400
|
1179 |
public:
|
alpar@579
|
1180 |
NodeMap(const EdgeSet &_G) : ParentNodeMap(_G.G) { }
|
alpar@579
|
1181 |
NodeMap(const EdgeSet &_G,const T &t) : ParentNodeMap(_G.G,t) { }
|
alpar@400
|
1182 |
//It is unnecessary
|
alpar@515
|
1183 |
NodeMap(const typename NodeGraphType::template NodeMap<T> &m) :
|
alpar@579
|
1184 |
ParentNodeMap(m) { }
|
alpar@400
|
1185 |
|
alpar@400
|
1186 |
///\todo It can copy between different types.
|
alpar@400
|
1187 |
///
|
alpar@401
|
1188 |
template<typename TT>
|
alpar@515
|
1189 |
NodeMap(const typename NodeGraphType::template NodeMap<TT> &m)
|
alpar@579
|
1190 |
: ParentNodeMap(m) { }
|
alpar@400
|
1191 |
};
|
alpar@400
|
1192 |
|
alpar@579
|
1193 |
///
|
alpar@400
|
1194 |
template <typename T> class EdgeMap : public DynMapBase<Edge>
|
alpar@400
|
1195 |
{
|
alpar@579
|
1196 |
protected:
|
alpar@579
|
1197 |
public:
|
alpar@579
|
1198 |
///\bug It should be at least protected
|
alpar@579
|
1199 |
///
|
alpar@400
|
1200 |
std::vector<T> container;
|
alpar@400
|
1201 |
|
alpar@400
|
1202 |
public:
|
alpar@400
|
1203 |
typedef T ValueType;
|
alpar@400
|
1204 |
typedef Edge KeyType;
|
alpar@400
|
1205 |
|
alpar@400
|
1206 |
EdgeMap(const EdgeSet &_G) :
|
alpar@400
|
1207 |
DynMapBase<Edge>(_G), container(_G.maxEdgeId())
|
alpar@400
|
1208 |
{
|
alpar@400
|
1209 |
//FIXME: What if there are empty Id's?
|
alpar@400
|
1210 |
//FIXME: Can I use 'this' in a constructor?
|
alpar@400
|
1211 |
G->dyn_edge_maps.push_back(this);
|
alpar@400
|
1212 |
}
|
alpar@400
|
1213 |
EdgeMap(const EdgeSet &_G,const T &t) :
|
alpar@400
|
1214 |
DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
|
alpar@400
|
1215 |
{
|
alpar@400
|
1216 |
G->dyn_edge_maps.push_back(this);
|
alpar@400
|
1217 |
}
|
alpar@400
|
1218 |
EdgeMap(const EdgeMap<T> &m) :
|
alpar@400
|
1219 |
DynMapBase<Edge>(*m.G), container(m.container)
|
alpar@400
|
1220 |
{
|
alpar@503
|
1221 |
G->dyn_edge_maps.push_back(this);
|
alpar@400
|
1222 |
}
|
alpar@400
|
1223 |
|
alpar@400
|
1224 |
template<typename TT> friend class EdgeMap;
|
alpar@400
|
1225 |
|
alpar@400
|
1226 |
///\todo It can copy between different types.
|
alpar@400
|
1227 |
///
|
alpar@400
|
1228 |
template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
|
alpar@590
|
1229 |
DynMapBase<Edge>(*m.G), container(m.container.size())
|
alpar@400
|
1230 |
{
|
alpar@503
|
1231 |
G->dyn_edge_maps.push_back(this);
|
alpar@400
|
1232 |
typename std::vector<TT>::const_iterator i;
|
alpar@400
|
1233 |
for(typename std::vector<TT>::const_iterator i=m.container.begin();
|
alpar@400
|
1234 |
i!=m.container.end();
|
alpar@400
|
1235 |
i++)
|
alpar@400
|
1236 |
container.push_back(*i);
|
alpar@400
|
1237 |
}
|
alpar@400
|
1238 |
~EdgeMap()
|
alpar@400
|
1239 |
{
|
alpar@400
|
1240 |
if(G) {
|
alpar@400
|
1241 |
typename std::vector<DynMapBase<Edge>* >::iterator i;
|
alpar@400
|
1242 |
for(i=G->dyn_edge_maps.begin();
|
alpar@400
|
1243 |
i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
|
alpar@400
|
1244 |
//if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
|
alpar@400
|
1245 |
//A better way to do that: (Is this really important?)
|
alpar@400
|
1246 |
if(*i==this) {
|
alpar@400
|
1247 |
*i=G->dyn_edge_maps.back();
|
alpar@400
|
1248 |
G->dyn_edge_maps.pop_back();
|
alpar@400
|
1249 |
}
|
alpar@400
|
1250 |
}
|
alpar@400
|
1251 |
}
|
alpar@400
|
1252 |
|
alpar@400
|
1253 |
void add(const Edge k)
|
alpar@400
|
1254 |
{
|
alpar@400
|
1255 |
if(k.n>=int(container.size())) container.resize(k.n+1);
|
alpar@400
|
1256 |
}
|
alpar@400
|
1257 |
void erase(const Edge) { }
|
alpar@400
|
1258 |
|
alpar@579
|
1259 |
///\bug This doesn't work. Why?
|
alpar@579
|
1260 |
/// void set(Edge n, T a) { container[n.n]=a; }
|
alpar@579
|
1261 |
void set(Edge n, T a) { container[G->id(n)]=a; }
|
alpar@400
|
1262 |
//T get(Edge n) const { return container[n.n]; }
|
alpar@400
|
1263 |
typename std::vector<T>::reference
|
alpar@579
|
1264 |
///\bug This doesn't work. Why?
|
alpar@579
|
1265 |
/// operator[](Edge n) { return container[n.n]; }
|
alpar@579
|
1266 |
operator[](Edge n) { return container[G->id(n)]; }
|
alpar@400
|
1267 |
typename std::vector<T>::const_reference
|
alpar@579
|
1268 |
///\bug This doesn't work. Why?
|
alpar@579
|
1269 |
/// operator[](Edge n) const { return container[n.n]; }
|
alpar@579
|
1270 |
operator[](Edge n) const { return container[G->id(n)]; }
|
alpar@400
|
1271 |
|
alpar@400
|
1272 |
///\warning There is no safety check at all!
|
alpar@400
|
1273 |
///Using operator = between maps attached to different graph may
|
alpar@400
|
1274 |
///cause serious problem.
|
alpar@400
|
1275 |
///\todo Is this really so?
|
alpar@400
|
1276 |
///\todo It can copy between different types.
|
alpar@400
|
1277 |
const EdgeMap<T>& operator=(const EdgeMap<T> &m)
|
alpar@400
|
1278 |
{
|
alpar@400
|
1279 |
container = m.container;
|
alpar@400
|
1280 |
return *this;
|
alpar@400
|
1281 |
}
|
alpar@579
|
1282 |
|
alpar@579
|
1283 |
template<typename TT> friend class EdgeMap;
|
alpar@579
|
1284 |
|
alpar@400
|
1285 |
template<typename TT>
|
alpar@400
|
1286 |
const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
|
alpar@400
|
1287 |
{
|
alpar@531
|
1288 |
std::copy(m.container.begin(), m.container.end(), container.begin());
|
alpar@400
|
1289 |
return *this;
|
alpar@400
|
1290 |
}
|
alpar@400
|
1291 |
|
alpar@400
|
1292 |
void update() {} //Useless for DynMaps
|
alpar@400
|
1293 |
void update(T a) {} //Useless for DynMaps
|
alpar@400
|
1294 |
};
|
alpar@400
|
1295 |
|
alpar@400
|
1296 |
};
|
alpar@406
|
1297 |
|
alpar@579
|
1298 |
template<typename GG>
|
alpar@579
|
1299 |
inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
|
alpar@531
|
1300 |
|
alpar@406
|
1301 |
/// @}
|
alpar@406
|
1302 |
|
alpar@395
|
1303 |
} //namespace hugo
|
alpar@395
|
1304 |
|
alpar@405
|
1305 |
#endif //HUGO_LIST_GRAPH_H
|