alpar@105
|
1 |
// -*- mode:C++ -*-
|
alpar@105
|
2 |
|
alpar@104
|
3 |
#ifndef SMART_GRAPH_H
|
alpar@104
|
4 |
#define SMART_GRAPH_H
|
alpar@104
|
5 |
|
alpar@104
|
6 |
#include <iostream>
|
alpar@104
|
7 |
#include <vector>
|
alpar@104
|
8 |
|
alpar@105
|
9 |
namespace hugo {
|
alpar@104
|
10 |
|
alpar@104
|
11 |
class SmartGraph {
|
alpar@104
|
12 |
|
alpar@104
|
13 |
static const int INVALID=-1;
|
alpar@104
|
14 |
|
alpar@104
|
15 |
struct NodeT
|
alpar@104
|
16 |
{
|
alpar@104
|
17 |
int first_in,first_out;
|
alpar@104
|
18 |
NodeT() : first_in(INVALID), first_out(INVALID) {}
|
alpar@104
|
19 |
};
|
alpar@104
|
20 |
struct EdgeT
|
alpar@104
|
21 |
{
|
alpar@104
|
22 |
int head, tail, next_in, next_out;
|
alpar@104
|
23 |
//FIXME: is this necessary?
|
alpar@104
|
24 |
EdgeT() : next_in(INVALID), next_out(INVALID) {}
|
alpar@104
|
25 |
};
|
alpar@104
|
26 |
|
alpar@104
|
27 |
std::vector<NodeT> nodes;
|
alpar@104
|
28 |
std::vector<EdgeT> edges;
|
alpar@104
|
29 |
|
alpar@108
|
30 |
template <typename Key> class DynMapBase
|
alpar@108
|
31 |
{
|
alpar@108
|
32 |
protected:
|
alpar@108
|
33 |
SmartGraph* G;
|
alpar@108
|
34 |
public:
|
alpar@108
|
35 |
virtual void add(const Key k) = NULL;
|
alpar@108
|
36 |
virtual void erase(const Key k) = NULL;
|
alpar@108
|
37 |
DynMapBase(SmartGraph &_G) : G(&_G) {}
|
alpar@108
|
38 |
virtual ~DynMapBase() {}
|
alpar@108
|
39 |
friend class SmartGraph;
|
alpar@108
|
40 |
};
|
alpar@104
|
41 |
|
alpar@104
|
42 |
public:
|
alpar@108
|
43 |
template <typename T> class DynEdgeMap;
|
alpar@108
|
44 |
template <typename T> class DynEdgeMap;
|
alpar@104
|
45 |
|
alpar@104
|
46 |
class NodeIt;
|
alpar@108
|
47 |
class EdgeIt;
|
alpar@108
|
48 |
|
alpar@108
|
49 |
protected:
|
alpar@108
|
50 |
std::vector<DynMapBase<NodeIt> * > dyn_node_maps;
|
alpar@108
|
51 |
std::vector<DynMapBase<EdgeIt> * > dyn_edge_maps;
|
alpar@108
|
52 |
|
alpar@108
|
53 |
public:
|
alpar@108
|
54 |
|
alpar@104
|
55 |
class EachNodeIt;
|
alpar@104
|
56 |
class EachEdgeIt;
|
alpar@104
|
57 |
class OutEdgeIt;
|
alpar@104
|
58 |
class InEdgeIt;
|
alpar@104
|
59 |
|
alpar@105
|
60 |
// class NodeIt { int n; };
|
alpar@105
|
61 |
// class EachNodeIt : public NodeIt { };
|
alpar@105
|
62 |
// class EdgeIt { int n; };
|
alpar@105
|
63 |
// class EachEdgeIt : public EdgeIt {};
|
alpar@105
|
64 |
// class OutEdgeIt : public EdgeIt {};
|
alpar@105
|
65 |
// class InEdgeIt : public EdgeIt {};
|
alpar@104
|
66 |
// class SymEdgeIt;
|
alpar@105
|
67 |
|
alpar@105
|
68 |
template <typename T> class NodeMap;
|
alpar@104
|
69 |
template <typename T> class EdgeMap;
|
alpar@104
|
70 |
|
alpar@104
|
71 |
public:
|
alpar@104
|
72 |
|
alpar@104
|
73 |
/* default constructor */
|
alpar@104
|
74 |
|
alpar@104
|
75 |
SmartGraph() : nodes(), edges() { }
|
alpar@104
|
76 |
|
alpar@108
|
77 |
~SmartGraph()
|
alpar@108
|
78 |
{
|
alpar@108
|
79 |
for(std::vector<DynMapBase<NodeIt> * >::iterator i=dyn_node_maps.begin();
|
alpar@108
|
80 |
i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
|
alpar@108
|
81 |
for(std::vector<DynMapBase<EdgeIt> * >::iterator i=dyn_edge_maps.begin();
|
alpar@108
|
82 |
i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
|
alpar@108
|
83 |
}
|
alpar@104
|
84 |
|
alpar@104
|
85 |
int nodeNum() const { return nodes.size(); } //FIXME: What is this?
|
alpar@104
|
86 |
int edgeNum() const { return edges.size(); } //FIXME: What is this?
|
alpar@104
|
87 |
|
alpar@108
|
88 |
int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
|
alpar@108
|
89 |
int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
|
alpar@108
|
90 |
|
alpar@108
|
91 |
|
alpar@104
|
92 |
NodeIt tail(EdgeIt e) const { return edges[e.n].tail; }
|
alpar@104
|
93 |
NodeIt head(EdgeIt e) const { return edges[e.n].head; }
|
alpar@104
|
94 |
|
alpar@104
|
95 |
NodeIt aNode(const OutEdgeIt& e) const { return tail(e); }
|
alpar@104
|
96 |
NodeIt aNode(const InEdgeIt& e) const { return head(e); }
|
alpar@104
|
97 |
//NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
|
alpar@104
|
98 |
|
alpar@104
|
99 |
NodeIt bNode(const OutEdgeIt& e) const { return head(e); }
|
alpar@104
|
100 |
NodeIt bNode(const InEdgeIt& e) const { return tail(e); }
|
alpar@104
|
101 |
//NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
|
alpar@104
|
102 |
|
alpar@104
|
103 |
EachNodeIt& getFirst(EachNodeIt& v) const {
|
alpar@104
|
104 |
v=EachNodeIt(*this); return v; }
|
alpar@104
|
105 |
EachEdgeIt& getFirst(EachEdgeIt& e) const {
|
alpar@104
|
106 |
e=EachEdgeIt(*this); return e; }
|
alpar@104
|
107 |
OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt v) const {
|
alpar@104
|
108 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@104
|
109 |
InEdgeIt& getFirst(InEdgeIt& e, const NodeIt v) const {
|
alpar@104
|
110 |
e=InEdgeIt(*this,v); return e; }
|
alpar@104
|
111 |
|
alpar@104
|
112 |
template< typename It >
|
alpar@104
|
113 |
It first() const {
|
alpar@104
|
114 |
It e;
|
alpar@104
|
115 |
getFirst(e);
|
alpar@104
|
116 |
return e;
|
alpar@104
|
117 |
}
|
alpar@104
|
118 |
|
alpar@104
|
119 |
template< typename It >
|
alpar@104
|
120 |
It first(NodeIt v) const {
|
alpar@104
|
121 |
It e;
|
alpar@104
|
122 |
getFirst(e, v);
|
alpar@104
|
123 |
return e;
|
alpar@104
|
124 |
}
|
alpar@104
|
125 |
|
alpar@104
|
126 |
bool valid(EdgeIt e) const { return e.n!=INVALID; }
|
alpar@104
|
127 |
bool valid(EachEdgeIt e) const { return e.n<int(edges.size()); }
|
alpar@104
|
128 |
bool valid(NodeIt n) const { return n.n<int(nodes.size()); }
|
alpar@104
|
129 |
|
alpar@105
|
130 |
template <typename It> It next(It it) const
|
alpar@105
|
131 |
// { It tmp(it); return goNext(tmp); }
|
alpar@105
|
132 |
{ It tmp; tmp.n=it.n+1; return tmp; }
|
alpar@104
|
133 |
|
alpar@104
|
134 |
NodeIt& goNext(NodeIt& it) const { ++it.n; return it; }
|
alpar@104
|
135 |
OutEdgeIt& goNext(OutEdgeIt& it) const
|
alpar@104
|
136 |
{ it.n=edges[it.n].next_out; return it; }
|
alpar@104
|
137 |
InEdgeIt& goNext(InEdgeIt& it) const
|
alpar@104
|
138 |
{ it.n=edges[it.n].next_in; return it; }
|
alpar@104
|
139 |
EachEdgeIt& goNext(EachEdgeIt& it) const { ++it.n; return it; }
|
alpar@104
|
140 |
|
alpar@104
|
141 |
int id(NodeIt v) const { return v.n; }
|
alpar@104
|
142 |
int id(EdgeIt e) const { return e.n; }
|
alpar@104
|
143 |
|
alpar@104
|
144 |
NodeIt addNode() {
|
alpar@104
|
145 |
NodeIt n; n.n=nodes.size();
|
alpar@104
|
146 |
nodes.push_back(NodeT()); //FIXME: Hmmm...
|
alpar@108
|
147 |
|
alpar@108
|
148 |
for(std::vector<DynMapBase<NodeIt> * >::iterator i=dyn_node_maps.begin();
|
alpar@108
|
149 |
i!=dyn_node_maps.end(); ++i) (**i).add(n.n);
|
alpar@108
|
150 |
|
alpar@104
|
151 |
return n;
|
alpar@104
|
152 |
}
|
alpar@108
|
153 |
|
alpar@104
|
154 |
EdgeIt addEdge(NodeIt u, NodeIt v) {
|
alpar@104
|
155 |
EdgeIt e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
|
alpar@104
|
156 |
edges[e.n].tail=u.n; edges[e.n].head=v.n;
|
alpar@104
|
157 |
edges[e.n].next_out=nodes[u.n].first_out;
|
alpar@104
|
158 |
edges[e.n].next_in=nodes[v.n].first_in;
|
alpar@104
|
159 |
nodes[u.n].first_out=nodes[v.n].first_in=e.n;
|
alpar@108
|
160 |
|
alpar@108
|
161 |
for(std::vector<DynMapBase<EdgeIt> * >::iterator i=dyn_edge_maps.begin();
|
alpar@108
|
162 |
i!=dyn_edge_maps.end(); ++i) (**i).add(e.n);
|
alpar@108
|
163 |
|
alpar@104
|
164 |
return e;
|
alpar@104
|
165 |
}
|
alpar@104
|
166 |
|
alpar@104
|
167 |
void clear() {nodes.clear();edges.clear();}
|
alpar@104
|
168 |
|
alpar@104
|
169 |
class NodeIt {
|
alpar@104
|
170 |
friend class SmartGraph;
|
alpar@104
|
171 |
template <typename T> friend class NodeMap;
|
alpar@108
|
172 |
template <typename T> friend class DynNodeMap;
|
alpar@104
|
173 |
|
alpar@104
|
174 |
friend class EdgeIt;
|
alpar@104
|
175 |
friend class OutEdgeIt;
|
alpar@104
|
176 |
friend class InEdgeIt;
|
alpar@104
|
177 |
friend class SymEdgeIt;
|
alpar@104
|
178 |
|
alpar@104
|
179 |
protected:
|
alpar@104
|
180 |
int n;
|
alpar@104
|
181 |
friend int SmartGraph::id(NodeIt v) const;
|
alpar@104
|
182 |
public:
|
alpar@104
|
183 |
NodeIt() {}
|
alpar@104
|
184 |
NodeIt(int nn) {n=nn;}
|
alpar@104
|
185 |
bool operator==(const NodeIt i) const {return n==i.n;}
|
alpar@104
|
186 |
bool operator!=(const NodeIt i) const {return n!=i.n;}
|
alpar@104
|
187 |
};
|
alpar@104
|
188 |
|
alpar@104
|
189 |
class EachNodeIt : public NodeIt {
|
alpar@104
|
190 |
friend class SmartGraph;
|
alpar@104
|
191 |
public:
|
alpar@104
|
192 |
EachNodeIt(const SmartGraph& G) : NodeIt(0) { }
|
alpar@104
|
193 |
EachNodeIt() : NodeIt() { }
|
alpar@104
|
194 |
};
|
alpar@104
|
195 |
|
alpar@104
|
196 |
class EdgeIt {
|
alpar@104
|
197 |
friend class SmartGraph;
|
alpar@104
|
198 |
template <typename T> friend class EdgeMap;
|
alpar@108
|
199 |
template <typename T> friend class DynEdgeMap;
|
alpar@104
|
200 |
|
alpar@104
|
201 |
friend class NodeIt;
|
alpar@104
|
202 |
friend class EachNodeIt;
|
alpar@104
|
203 |
protected:
|
alpar@104
|
204 |
int n;
|
alpar@104
|
205 |
friend int SmartGraph::id(EdgeIt e) const;
|
alpar@104
|
206 |
public:
|
alpar@104
|
207 |
EdgeIt() { }
|
alpar@104
|
208 |
EdgeIt(int nn) {n=nn;}
|
alpar@104
|
209 |
bool operator==(const EdgeIt i) const {return n==i.n;}
|
alpar@104
|
210 |
bool operator!=(const EdgeIt i) const {return n!=i.n;}
|
alpar@104
|
211 |
};
|
alpar@104
|
212 |
|
alpar@104
|
213 |
class EachEdgeIt : public EdgeIt {
|
alpar@104
|
214 |
friend class SmartGraph;
|
alpar@104
|
215 |
public:
|
alpar@104
|
216 |
EachEdgeIt(const SmartGraph& G) : EdgeIt(0) { }
|
alpar@104
|
217 |
EachEdgeIt() : EdgeIt() { }
|
alpar@104
|
218 |
};
|
alpar@104
|
219 |
|
alpar@104
|
220 |
class OutEdgeIt : public EdgeIt {
|
alpar@104
|
221 |
friend class SmartGraph;
|
alpar@104
|
222 |
public:
|
alpar@104
|
223 |
OutEdgeIt() : EdgeIt() { }
|
alpar@104
|
224 |
OutEdgeIt(const SmartGraph& G,const NodeIt v)
|
alpar@104
|
225 |
: EdgeIt(G.nodes[v.n].first_out) {}
|
alpar@104
|
226 |
};
|
alpar@104
|
227 |
|
alpar@104
|
228 |
class InEdgeIt : public EdgeIt {
|
alpar@104
|
229 |
friend class SmartGraph;
|
alpar@104
|
230 |
public:
|
alpar@104
|
231 |
InEdgeIt() : EdgeIt() { }
|
alpar@104
|
232 |
InEdgeIt(const SmartGraph& G,NodeIt v) :EdgeIt(G.nodes[v.n].first_in){}
|
alpar@104
|
233 |
};
|
alpar@105
|
234 |
|
alpar@105
|
235 |
// Map types
|
alpar@105
|
236 |
|
alpar@105
|
237 |
template <typename T>
|
alpar@105
|
238 |
class NodeMap {
|
alpar@105
|
239 |
const SmartGraph& G;
|
alpar@105
|
240 |
std::vector<T> container;
|
alpar@105
|
241 |
public:
|
alpar@105
|
242 |
typedef T ValueType;
|
alpar@105
|
243 |
typedef NodeIt KeyType;
|
alpar@108
|
244 |
NodeMap(const SmartGraph& _G) : G(_G), container(G.maxNodeId()) { }
|
alpar@105
|
245 |
NodeMap(const SmartGraph& _G, T a) :
|
alpar@108
|
246 |
G(_G), container(G.maxNodeId(), a) { }
|
alpar@105
|
247 |
void set(NodeIt n, T a) { container[n.n]=a; }
|
alpar@105
|
248 |
T get(NodeIt n) const { return container[n.n]; }
|
alpar@105
|
249 |
T& operator[](NodeIt n) { return container[n.n]; }
|
alpar@105
|
250 |
const T& operator[](NodeIt n) const { return container[n.n]; }
|
alpar@108
|
251 |
void update() { container.resize(G.maxNodeId()); }
|
alpar@108
|
252 |
void update(T a) { container.resize(G.maxNodeId(), a); }
|
alpar@105
|
253 |
};
|
alpar@105
|
254 |
|
alpar@105
|
255 |
template <typename T>
|
alpar@105
|
256 |
class EdgeMap {
|
alpar@105
|
257 |
const SmartGraph& G;
|
alpar@105
|
258 |
std::vector<T> container;
|
alpar@105
|
259 |
public:
|
alpar@105
|
260 |
typedef T ValueType;
|
alpar@105
|
261 |
typedef EdgeIt KeyType;
|
alpar@108
|
262 |
EdgeMap(const SmartGraph& _G) : G(_G), container(G.maxEdgeId()) { }
|
alpar@105
|
263 |
EdgeMap(const SmartGraph& _G, T a) :
|
alpar@108
|
264 |
G(_G), container(G.maxEdgeId(), a) { }
|
alpar@105
|
265 |
void set(EdgeIt e, T a) { container[e.n]=a; }
|
alpar@105
|
266 |
T get(EdgeIt e) const { return container[e.n]; }
|
alpar@105
|
267 |
T& operator[](EdgeIt e) { return container[e.n]; }
|
alpar@105
|
268 |
const T& operator[](EdgeIt e) const { return container[e.n]; }
|
alpar@108
|
269 |
void update() { container.resize(G.maxEdgeId()); }
|
alpar@108
|
270 |
void update(T a) { container.resize(G.maxEdgeId(), a); }
|
alpar@105
|
271 |
};
|
alpar@105
|
272 |
|
alpar@108
|
273 |
template <typename T> class DynNodeMap : public DynMapBase<NodeIt>
|
alpar@108
|
274 |
{
|
alpar@108
|
275 |
std::vector<T> container;
|
alpar@105
|
276 |
|
alpar@108
|
277 |
public:
|
alpar@108
|
278 |
typedef T ValueType;
|
alpar@108
|
279 |
typedef NodeIt KeyType;
|
alpar@105
|
280 |
|
alpar@108
|
281 |
DynNodeMap(SmartGraph &_G) :
|
alpar@108
|
282 |
DynMapBase<NodeIt>(_G), container(_G.maxNodeId())
|
alpar@108
|
283 |
{
|
alpar@108
|
284 |
//FIXME: What if there are empty Id's?
|
alpar@115
|
285 |
//FIXME: Can I use 'this' in a constructor?
|
alpar@108
|
286 |
G->dyn_node_maps.push_back(this);
|
alpar@108
|
287 |
}
|
alpar@108
|
288 |
~DynNodeMap()
|
alpar@108
|
289 |
{
|
alpar@108
|
290 |
if(G) {
|
alpar@108
|
291 |
std::vector<DynMapBase<NodeIt>* >::iterator i;
|
alpar@108
|
292 |
for(i=G->dyn_node_maps.begin();
|
alpar@108
|
293 |
i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
|
alpar@115
|
294 |
//if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
|
alpar@115
|
295 |
//A better way to do that: (Is this really important?)
|
alpar@115
|
296 |
if(*i==this) {
|
alpar@116
|
297 |
*i=G->dyn_node_maps.back();
|
alpar@115
|
298 |
G->dyn_node_maps.pop_back();
|
alpar@115
|
299 |
}
|
alpar@108
|
300 |
}
|
alpar@108
|
301 |
}
|
alpar@105
|
302 |
|
alpar@108
|
303 |
void add(const NodeIt k)
|
alpar@108
|
304 |
{
|
alpar@108
|
305 |
if(k.n>=container.size()) container.resize(k.n+1);
|
alpar@108
|
306 |
}
|
alpar@108
|
307 |
void erase(const NodeIt k)
|
alpar@108
|
308 |
{
|
alpar@108
|
309 |
//FIXME: Please implement me.
|
alpar@108
|
310 |
}
|
alpar@108
|
311 |
|
alpar@108
|
312 |
void set(NodeIt n, T a) { container[n.n]=a; }
|
alpar@108
|
313 |
T get(NodeIt n) const { return container[n.n]; }
|
alpar@108
|
314 |
T& operator[](NodeIt n) { return container[n.n]; }
|
alpar@108
|
315 |
const T& operator[](NodeIt n) const { return container[n.n]; }
|
alpar@108
|
316 |
|
alpar@108
|
317 |
void update() {} //Useless for DynMaps
|
alpar@108
|
318 |
void update(T a) {} //Useless for DynMaps
|
alpar@108
|
319 |
};
|
alpar@108
|
320 |
|
alpar@108
|
321 |
template <typename T> class DynEdgeMap : public DynMapBase<EdgeIt>
|
alpar@108
|
322 |
{
|
alpar@108
|
323 |
std::vector<T> container;
|
alpar@108
|
324 |
|
alpar@108
|
325 |
public:
|
alpar@108
|
326 |
typedef T ValueType;
|
alpar@108
|
327 |
typedef EdgeIt KeyType;
|
alpar@108
|
328 |
|
alpar@108
|
329 |
DynEdgeMap(SmartGraph &_G) :
|
alpar@108
|
330 |
DynMapBase<EdgeIt>(_G), container(_G.maxEdgeId())
|
alpar@108
|
331 |
{
|
alpar@108
|
332 |
//FIXME: What if there are empty Id's?
|
alpar@115
|
333 |
//FIXME: Can I use 'this' in a constructor?
|
alpar@108
|
334 |
G->dyn_edge_maps.push_back(this);
|
alpar@108
|
335 |
}
|
alpar@108
|
336 |
~DynEdgeMap()
|
alpar@108
|
337 |
{
|
alpar@108
|
338 |
if(G) {
|
alpar@108
|
339 |
std::vector<DynMapBase<EdgeIt>* >::iterator i;
|
alpar@108
|
340 |
for(i=G->dyn_edge_maps.begin();
|
alpar@108
|
341 |
i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
|
alpar@115
|
342 |
//if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
|
alpar@115
|
343 |
//A better way to do that: (Is this really important?)
|
alpar@115
|
344 |
if(*i==this) {
|
alpar@116
|
345 |
*i=G->dyn_edge_maps.back();
|
alpar@115
|
346 |
G->dyn_edge_maps.pop_back();
|
alpar@115
|
347 |
}
|
alpar@108
|
348 |
}
|
alpar@108
|
349 |
}
|
alpar@115
|
350 |
|
alpar@108
|
351 |
void add(const EdgeIt k)
|
alpar@108
|
352 |
{
|
alpar@108
|
353 |
if(k.n>=int(container.size())) container.resize(k.n+1);
|
alpar@108
|
354 |
}
|
alpar@108
|
355 |
void erase(const EdgeIt k)
|
alpar@108
|
356 |
{
|
alpar@108
|
357 |
//FIXME: Please implement me.
|
alpar@108
|
358 |
}
|
alpar@108
|
359 |
|
alpar@108
|
360 |
void set(EdgeIt n, T a) { container[n.n]=a; }
|
alpar@108
|
361 |
T get(EdgeIt n) const { return container[n.n]; }
|
alpar@108
|
362 |
T& operator[](EdgeIt n) { return container[n.n]; }
|
alpar@108
|
363 |
const T& operator[](EdgeIt n) const { return container[n.n]; }
|
alpar@108
|
364 |
|
alpar@108
|
365 |
void update() {} //Useless for DynMaps
|
alpar@108
|
366 |
void update(T a) {} //Useless for DynMaps
|
alpar@108
|
367 |
};
|
alpar@108
|
368 |
|
alpar@104
|
369 |
};
|
alpar@105
|
370 |
} //namespace hugo
|
alpar@104
|
371 |
|
alpar@104
|
372 |
#endif //SMART_GRAPH_H
|