alpar@105
|
1 |
// -*- mode:C++ -*-
|
alpar@105
|
2 |
|
alpar@185
|
3 |
#ifndef HUGO_SMART_GRAPH_H
|
alpar@185
|
4 |
#define HUGO_SMART_GRAPH_H
|
alpar@104
|
5 |
|
klao@491
|
6 |
///\ingroup graphs
|
alpar@242
|
7 |
///\file
|
alpar@242
|
8 |
///\brief SmartGraph and SymSmartGraph classes.
|
alpar@242
|
9 |
|
alpar@104
|
10 |
#include <vector>
|
alpar@129
|
11 |
#include <limits.h>
|
alpar@104
|
12 |
|
ladanyi@542
|
13 |
#include <hugo/invalid.h>
|
alpar@157
|
14 |
|
alpar@105
|
15 |
namespace hugo {
|
alpar@104
|
16 |
|
alpar@407
|
17 |
/// \addtogroup graphs
|
alpar@407
|
18 |
/// @{
|
alpar@185
|
19 |
class SymSmartGraph;
|
alpar@185
|
20 |
|
alpar@186
|
21 |
///A smart graph class.
|
alpar@186
|
22 |
|
alpar@186
|
23 |
///This is a simple and fast graph implementation.
|
alpar@186
|
24 |
///It is also quite memory efficient, but at the price
|
alpar@186
|
25 |
///that <b> it does not support node and edge deletion</b>.
|
alpar@242
|
26 |
///It conforms to the graph interface documented under
|
alpar@186
|
27 |
///the description of \ref GraphSkeleton.
|
alpar@186
|
28 |
///\sa \ref GraphSkeleton.
|
alpar@402
|
29 |
///
|
alpar@402
|
30 |
///\todo Some member functions could be \c static.
|
alpar@753
|
31 |
///
|
alpar@753
|
32 |
///\todo A possibly useful functionality: a function saveState() would
|
alpar@753
|
33 |
///give back a data sturcture X and then the function restoreState(X)
|
alpar@753
|
34 |
///would remove the nodes and edges added after the call of saveState().
|
alpar@753
|
35 |
///Of course it should be used as a stack. (Maybe X is not necessary.)
|
alpar@753
|
36 |
///
|
alpar@456
|
37 |
///\author Alpar Juttner
|
alpar@104
|
38 |
class SmartGraph {
|
alpar@104
|
39 |
|
alpar@104
|
40 |
struct NodeT
|
alpar@104
|
41 |
{
|
alpar@104
|
42 |
int first_in,first_out;
|
alpar@157
|
43 |
NodeT() : first_in(-1), first_out(-1) {}
|
alpar@104
|
44 |
};
|
alpar@104
|
45 |
struct EdgeT
|
alpar@104
|
46 |
{
|
alpar@104
|
47 |
int head, tail, next_in, next_out;
|
alpar@104
|
48 |
//FIXME: is this necessary?
|
alpar@157
|
49 |
EdgeT() : next_in(-1), next_out(-1) {}
|
alpar@104
|
50 |
};
|
alpar@104
|
51 |
|
alpar@104
|
52 |
std::vector<NodeT> nodes;
|
alpar@129
|
53 |
|
alpar@104
|
54 |
std::vector<EdgeT> edges;
|
alpar@104
|
55 |
|
alpar@185
|
56 |
protected:
|
alpar@185
|
57 |
|
alpar@108
|
58 |
template <typename Key> class DynMapBase
|
alpar@108
|
59 |
{
|
alpar@108
|
60 |
protected:
|
alpar@185
|
61 |
const SmartGraph* G;
|
alpar@108
|
62 |
public:
|
marci@415
|
63 |
virtual void add(const Key k) = 0;
|
marci@415
|
64 |
virtual void erase(const Key k) = 0;
|
alpar@157
|
65 |
DynMapBase(const SmartGraph &_G) : G(&_G) {}
|
alpar@108
|
66 |
virtual ~DynMapBase() {}
|
alpar@108
|
67 |
friend class SmartGraph;
|
alpar@108
|
68 |
};
|
alpar@185
|
69 |
|
alpar@104
|
70 |
public:
|
alpar@185
|
71 |
template <typename T> class EdgeMap;
|
alpar@590
|
72 |
template <typename T> class NodeMap;
|
alpar@104
|
73 |
|
alpar@164
|
74 |
class Node;
|
alpar@164
|
75 |
class Edge;
|
alpar@108
|
76 |
|
alpar@185
|
77 |
// protected:
|
alpar@185
|
78 |
// HELPME:
|
alpar@186
|
79 |
protected:
|
alpar@185
|
80 |
///\bug It must be public because of SymEdgeMap.
|
alpar@185
|
81 |
///
|
alpar@164
|
82 |
mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
|
alpar@185
|
83 |
///\bug It must be public because of SymEdgeMap.
|
alpar@185
|
84 |
///
|
alpar@164
|
85 |
mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
|
alpar@108
|
86 |
|
alpar@108
|
87 |
public:
|
alpar@108
|
88 |
|
alpar@503
|
89 |
|
alpar@164
|
90 |
class NodeIt;
|
alpar@164
|
91 |
class EdgeIt;
|
alpar@104
|
92 |
class OutEdgeIt;
|
alpar@104
|
93 |
class InEdgeIt;
|
alpar@104
|
94 |
|
alpar@105
|
95 |
template <typename T> class NodeMap;
|
alpar@104
|
96 |
template <typename T> class EdgeMap;
|
alpar@104
|
97 |
|
alpar@104
|
98 |
public:
|
alpar@104
|
99 |
|
alpar@104
|
100 |
SmartGraph() : nodes(), edges() { }
|
alpar@136
|
101 |
SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
|
alpar@104
|
102 |
|
alpar@108
|
103 |
~SmartGraph()
|
alpar@108
|
104 |
{
|
alpar@164
|
105 |
for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
|
alpar@108
|
106 |
i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
|
alpar@164
|
107 |
for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
|
alpar@108
|
108 |
i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
|
alpar@108
|
109 |
}
|
alpar@104
|
110 |
|
alpar@104
|
111 |
int nodeNum() const { return nodes.size(); } //FIXME: What is this?
|
alpar@104
|
112 |
int edgeNum() const { return edges.size(); } //FIXME: What is this?
|
alpar@104
|
113 |
|
alpar@186
|
114 |
///\bug This function does something different than
|
alpar@186
|
115 |
///its name would suggests...
|
alpar@108
|
116 |
int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
|
alpar@186
|
117 |
///\bug This function does something different than
|
alpar@186
|
118 |
///its name would suggests...
|
alpar@108
|
119 |
int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
|
alpar@108
|
120 |
|
alpar@164
|
121 |
Node tail(Edge e) const { return edges[e.n].tail; }
|
alpar@164
|
122 |
Node head(Edge e) const { return edges[e.n].head; }
|
alpar@104
|
123 |
|
alpar@164
|
124 |
NodeIt& first(NodeIt& v) const {
|
alpar@164
|
125 |
v=NodeIt(*this); return v; }
|
alpar@164
|
126 |
EdgeIt& first(EdgeIt& e) const {
|
alpar@164
|
127 |
e=EdgeIt(*this); return e; }
|
alpar@164
|
128 |
OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
|
alpar@104
|
129 |
e=OutEdgeIt(*this,v); return e; }
|
alpar@164
|
130 |
InEdgeIt& first(InEdgeIt& e, const Node v) const {
|
alpar@104
|
131 |
e=InEdgeIt(*this,v); return e; }
|
alpar@104
|
132 |
|
alpar@713
|
133 |
static int id(Node v) { return v.n; }
|
alpar@713
|
134 |
static int id(Edge e) { return e.n; }
|
alpar@104
|
135 |
|
alpar@164
|
136 |
Node addNode() {
|
alpar@164
|
137 |
Node n; n.n=nodes.size();
|
alpar@104
|
138 |
nodes.push_back(NodeT()); //FIXME: Hmmm...
|
alpar@108
|
139 |
|
alpar@164
|
140 |
for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
|
alpar@398
|
141 |
i!=dyn_node_maps.end(); ++i) (**i).add(n);
|
alpar@108
|
142 |
|
alpar@104
|
143 |
return n;
|
alpar@104
|
144 |
}
|
alpar@108
|
145 |
|
alpar@164
|
146 |
Edge addEdge(Node u, Node v) {
|
alpar@164
|
147 |
Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
|
alpar@104
|
148 |
edges[e.n].tail=u.n; edges[e.n].head=v.n;
|
alpar@104
|
149 |
edges[e.n].next_out=nodes[u.n].first_out;
|
alpar@104
|
150 |
edges[e.n].next_in=nodes[v.n].first_in;
|
alpar@104
|
151 |
nodes[u.n].first_out=nodes[v.n].first_in=e.n;
|
alpar@108
|
152 |
|
alpar@164
|
153 |
for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
|
alpar@157
|
154 |
i!=dyn_edge_maps.end(); ++i) (**i).add(e);
|
alpar@108
|
155 |
|
alpar@104
|
156 |
return e;
|
alpar@104
|
157 |
}
|
alpar@104
|
158 |
|
alpar@774
|
159 |
/// Finds an edge between two nodes.
|
alpar@774
|
160 |
|
alpar@774
|
161 |
/// Finds an edge from node \c u to node \c v.
|
alpar@774
|
162 |
///
|
alpar@774
|
163 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@774
|
164 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@774
|
165 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@774
|
166 |
/// \return The found edge or INVALID if there is no such an edge.
|
alpar@774
|
167 |
Edge findEdge(Node u,Node v, Edge prev = INVALID)
|
alpar@774
|
168 |
{
|
alpar@774
|
169 |
int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
|
alpar@774
|
170 |
while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
|
alpar@774
|
171 |
prev.n=e;
|
alpar@774
|
172 |
return prev;
|
alpar@774
|
173 |
}
|
alpar@774
|
174 |
|
alpar@104
|
175 |
void clear() {nodes.clear();edges.clear();}
|
alpar@104
|
176 |
|
alpar@164
|
177 |
class Node {
|
alpar@104
|
178 |
friend class SmartGraph;
|
alpar@104
|
179 |
template <typename T> friend class NodeMap;
|
alpar@104
|
180 |
|
alpar@164
|
181 |
friend class Edge;
|
alpar@104
|
182 |
friend class OutEdgeIt;
|
alpar@104
|
183 |
friend class InEdgeIt;
|
alpar@164
|
184 |
friend class SymEdge;
|
alpar@104
|
185 |
|
alpar@104
|
186 |
protected:
|
alpar@104
|
187 |
int n;
|
alpar@722
|
188 |
friend int SmartGraph::id(Node v);
|
alpar@164
|
189 |
Node(int nn) {n=nn;}
|
alpar@104
|
190 |
public:
|
alpar@164
|
191 |
Node() {}
|
alpar@503
|
192 |
Node (Invalid) { n=-1; }
|
alpar@164
|
193 |
bool operator==(const Node i) const {return n==i.n;}
|
alpar@164
|
194 |
bool operator!=(const Node i) const {return n!=i.n;}
|
alpar@164
|
195 |
bool operator<(const Node i) const {return n<i.n;}
|
alpar@774
|
196 |
// ///Validity check
|
alpar@774
|
197 |
// operator bool() { return n!=-1; }
|
alpar@104
|
198 |
};
|
alpar@104
|
199 |
|
alpar@164
|
200 |
class NodeIt : public Node {
|
alpar@774
|
201 |
const SmartGraph *G;
|
alpar@104
|
202 |
friend class SmartGraph;
|
alpar@104
|
203 |
public:
|
alpar@402
|
204 |
NodeIt() : Node() { }
|
alpar@774
|
205 |
NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
|
alpar@402
|
206 |
NodeIt(Invalid i) : Node(i) { }
|
alpar@774
|
207 |
NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
|
alpar@774
|
208 |
NodeIt &operator++() {
|
alpar@774
|
209 |
n=(n+2)%(G->nodes.size()+1)-1;
|
alpar@774
|
210 |
return *this;
|
alpar@774
|
211 |
}
|
alpar@774
|
212 |
// ///Validity check
|
alpar@774
|
213 |
// operator bool() { return Node::operator bool(); }
|
alpar@104
|
214 |
};
|
alpar@104
|
215 |
|
alpar@164
|
216 |
class Edge {
|
alpar@104
|
217 |
friend class SmartGraph;
|
alpar@104
|
218 |
template <typename T> friend class EdgeMap;
|
alpar@185
|
219 |
|
alpar@185
|
220 |
//template <typename T> friend class SymSmartGraph::SymEdgeMap;
|
alpar@185
|
221 |
//friend Edge SymSmartGraph::opposite(Edge) const;
|
alpar@104
|
222 |
|
alpar@164
|
223 |
friend class Node;
|
alpar@104
|
224 |
friend class NodeIt;
|
alpar@104
|
225 |
protected:
|
alpar@104
|
226 |
int n;
|
alpar@722
|
227 |
friend int SmartGraph::id(Edge e);
|
alpar@157
|
228 |
|
alpar@706
|
229 |
public:
|
alpar@706
|
230 |
/// An Edge with id \c n.
|
alpar@706
|
231 |
|
alpar@706
|
232 |
/// \bug It should be
|
alpar@706
|
233 |
/// obtained by a member function of the Graph.
|
alpar@164
|
234 |
Edge(int nn) {n=nn;}
|
alpar@164
|
235 |
Edge() { }
|
marci@174
|
236 |
Edge (Invalid) { n=-1; }
|
alpar@164
|
237 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@164
|
238 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@164
|
239 |
bool operator<(const Edge i) const {return n<i.n;}
|
alpar@185
|
240 |
///\bug This is a workaround until somebody tells me how to
|
alpar@185
|
241 |
///make class \c SymSmartGraph::SymEdgeMap friend of Edge
|
alpar@185
|
242 |
int &idref() {return n;}
|
alpar@774
|
243 |
const int &idref() const {return n;}
|
alpar@774
|
244 |
// ///Validity check
|
alpar@774
|
245 |
// operator bool() { return n!=-1; }
|
alpar@774
|
246 |
};
|
alpar@104
|
247 |
|
alpar@164
|
248 |
class EdgeIt : public Edge {
|
alpar@774
|
249 |
const SmartGraph *G;
|
alpar@104
|
250 |
friend class SmartGraph;
|
alpar@104
|
251 |
public:
|
alpar@774
|
252 |
EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
|
alpar@774
|
253 |
EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@164
|
254 |
EdgeIt (Invalid i) : Edge(i) { }
|
alpar@164
|
255 |
EdgeIt() : Edge() { }
|
alpar@185
|
256 |
///\bug This is a workaround until somebody tells me how to
|
alpar@185
|
257 |
///make class \c SymSmartGraph::SymEdgeMap friend of Edge
|
alpar@185
|
258 |
int &idref() {return n;}
|
alpar@774
|
259 |
EdgeIt &operator++() { --n; return *this; }
|
alpar@774
|
260 |
// ///Validity check
|
alpar@774
|
261 |
// operator bool() { return Edge::operator bool(); }
|
alpar@104
|
262 |
};
|
alpar@104
|
263 |
|
alpar@164
|
264 |
class OutEdgeIt : public Edge {
|
alpar@774
|
265 |
const SmartGraph *G;
|
alpar@104
|
266 |
friend class SmartGraph;
|
alpar@104
|
267 |
public:
|
alpar@164
|
268 |
OutEdgeIt() : Edge() { }
|
alpar@774
|
269 |
OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@164
|
270 |
OutEdgeIt (Invalid i) : Edge(i) { }
|
alpar@157
|
271 |
|
alpar@774
|
272 |
OutEdgeIt(const SmartGraph& _G,const Node v)
|
alpar@774
|
273 |
: Edge(_G.nodes[v.n].first_out), G(&_G) {}
|
alpar@774
|
274 |
OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
|
alpar@774
|
275 |
// ///Validity check
|
alpar@774
|
276 |
// operator bool() { return Edge::operator bool(); }
|
alpar@104
|
277 |
};
|
alpar@104
|
278 |
|
alpar@164
|
279 |
class InEdgeIt : public Edge {
|
alpar@774
|
280 |
const SmartGraph *G;
|
alpar@104
|
281 |
friend class SmartGraph;
|
alpar@104
|
282 |
public:
|
alpar@164
|
283 |
InEdgeIt() : Edge() { }
|
alpar@774
|
284 |
InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
|
alpar@164
|
285 |
InEdgeIt (Invalid i) : Edge(i) { }
|
alpar@774
|
286 |
InEdgeIt(const SmartGraph& _G,Node v)
|
alpar@774
|
287 |
: Edge(_G.nodes[v.n].first_in), G(&_G) { }
|
alpar@774
|
288 |
InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
|
alpar@774
|
289 |
// ///Validity check
|
alpar@774
|
290 |
// operator bool() { return Edge::operator bool(); }
|
alpar@104
|
291 |
};
|
alpar@105
|
292 |
|
alpar@185
|
293 |
template <typename T> class NodeMap : public DynMapBase<Node>
|
alpar@108
|
294 |
{
|
alpar@108
|
295 |
std::vector<T> container;
|
alpar@105
|
296 |
|
alpar@108
|
297 |
public:
|
alpar@108
|
298 |
typedef T ValueType;
|
alpar@164
|
299 |
typedef Node KeyType;
|
alpar@105
|
300 |
|
alpar@185
|
301 |
NodeMap(const SmartGraph &_G) :
|
alpar@164
|
302 |
DynMapBase<Node>(_G), container(_G.maxNodeId())
|
alpar@108
|
303 |
{
|
alpar@108
|
304 |
G->dyn_node_maps.push_back(this);
|
alpar@108
|
305 |
}
|
alpar@185
|
306 |
NodeMap(const SmartGraph &_G,const T &t) :
|
alpar@185
|
307 |
DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
|
alpar@185
|
308 |
{
|
alpar@185
|
309 |
G->dyn_node_maps.push_back(this);
|
alpar@185
|
310 |
}
|
alpar@185
|
311 |
|
alpar@185
|
312 |
NodeMap(const NodeMap<T> &m) :
|
alpar@185
|
313 |
DynMapBase<Node>(*m.G), container(m.container)
|
alpar@185
|
314 |
{
|
alpar@185
|
315 |
G->dyn_node_maps.push_back(this);
|
alpar@185
|
316 |
}
|
alpar@185
|
317 |
|
alpar@185
|
318 |
template<typename TT> friend class NodeMap;
|
alpar@185
|
319 |
|
alpar@185
|
320 |
///\todo It can copy between different types.
|
alpar@590
|
321 |
///\todo We could use 'copy'
|
alpar@185
|
322 |
template<typename TT> NodeMap(const NodeMap<TT> &m) :
|
alpar@590
|
323 |
DynMapBase<Node>(*m.G), container(m.container.size())
|
alpar@185
|
324 |
{
|
alpar@185
|
325 |
G->dyn_node_maps.push_back(this);
|
alpar@185
|
326 |
typename std::vector<TT>::const_iterator i;
|
alpar@185
|
327 |
for(typename std::vector<TT>::const_iterator i=m.container.begin();
|
alpar@185
|
328 |
i!=m.container.end();
|
alpar@185
|
329 |
i++)
|
alpar@185
|
330 |
container.push_back(*i);
|
alpar@185
|
331 |
}
|
alpar@185
|
332 |
~NodeMap()
|
alpar@108
|
333 |
{
|
alpar@108
|
334 |
if(G) {
|
alpar@164
|
335 |
std::vector<DynMapBase<Node>* >::iterator i;
|
alpar@108
|
336 |
for(i=G->dyn_node_maps.begin();
|
alpar@108
|
337 |
i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
|
alpar@115
|
338 |
//if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
|
alpar@115
|
339 |
//A better way to do that: (Is this really important?)
|
alpar@115
|
340 |
if(*i==this) {
|
alpar@116
|
341 |
*i=G->dyn_node_maps.back();
|
alpar@115
|
342 |
G->dyn_node_maps.pop_back();
|
alpar@115
|
343 |
}
|
alpar@108
|
344 |
}
|
alpar@108
|
345 |
}
|
alpar@105
|
346 |
|
alpar@164
|
347 |
void add(const Node k)
|
alpar@108
|
348 |
{
|
alpar@185
|
349 |
if(k.n>=int(container.size())) container.resize(k.n+1);
|
alpar@108
|
350 |
}
|
alpar@177
|
351 |
|
alpar@215
|
352 |
void erase(const Node) { }
|
alpar@108
|
353 |
|
alpar@164
|
354 |
void set(Node n, T a) { container[n.n]=a; }
|
alpar@285
|
355 |
//'T& operator[](Node n)' would be wrong here
|
alpar@215
|
356 |
typename std::vector<T>::reference
|
alpar@215
|
357 |
operator[](Node n) { return container[n.n]; }
|
alpar@285
|
358 |
//'const T& operator[](Node n)' would be wrong here
|
alpar@215
|
359 |
typename std::vector<T>::const_reference
|
alpar@215
|
360 |
operator[](Node n) const { return container[n.n]; }
|
alpar@108
|
361 |
|
alpar@185
|
362 |
///\warning There is no safety check at all!
|
alpar@185
|
363 |
///Using operator = between maps attached to different graph may
|
alpar@185
|
364 |
///cause serious problem.
|
alpar@185
|
365 |
///\todo Is this really so?
|
alpar@185
|
366 |
///\todo It can copy between different types.
|
alpar@185
|
367 |
const NodeMap<T>& operator=(const NodeMap<T> &m)
|
alpar@185
|
368 |
{
|
alpar@185
|
369 |
container = m.container;
|
alpar@185
|
370 |
return *this;
|
alpar@185
|
371 |
}
|
alpar@185
|
372 |
template<typename TT>
|
alpar@185
|
373 |
const NodeMap<T>& operator=(const NodeMap<TT> &m)
|
alpar@185
|
374 |
{
|
alpar@531
|
375 |
std::copy(m.container.begin(), m.container.end(), container.begin());
|
alpar@185
|
376 |
return *this;
|
alpar@185
|
377 |
}
|
alpar@185
|
378 |
|
alpar@285
|
379 |
void update() {} //Useless for Dynamic Maps
|
alpar@285
|
380 |
void update(T a) {} //Useless for Dynamic Maps
|
alpar@108
|
381 |
};
|
alpar@108
|
382 |
|
alpar@185
|
383 |
template <typename T> class EdgeMap : public DynMapBase<Edge>
|
alpar@108
|
384 |
{
|
alpar@108
|
385 |
std::vector<T> container;
|
alpar@108
|
386 |
|
alpar@108
|
387 |
public:
|
alpar@108
|
388 |
typedef T ValueType;
|
alpar@164
|
389 |
typedef Edge KeyType;
|
alpar@108
|
390 |
|
alpar@185
|
391 |
EdgeMap(const SmartGraph &_G) :
|
alpar@164
|
392 |
DynMapBase<Edge>(_G), container(_G.maxEdgeId())
|
alpar@108
|
393 |
{
|
alpar@108
|
394 |
//FIXME: What if there are empty Id's?
|
alpar@115
|
395 |
//FIXME: Can I use 'this' in a constructor?
|
alpar@108
|
396 |
G->dyn_edge_maps.push_back(this);
|
alpar@108
|
397 |
}
|
alpar@185
|
398 |
EdgeMap(const SmartGraph &_G,const T &t) :
|
alpar@185
|
399 |
DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
|
alpar@185
|
400 |
{
|
alpar@185
|
401 |
G->dyn_edge_maps.push_back(this);
|
alpar@185
|
402 |
}
|
alpar@185
|
403 |
EdgeMap(const EdgeMap<T> &m) :
|
alpar@185
|
404 |
DynMapBase<Edge>(*m.G), container(m.container)
|
alpar@185
|
405 |
{
|
alpar@503
|
406 |
G->dyn_edge_maps.push_back(this);
|
alpar@185
|
407 |
}
|
alpar@185
|
408 |
|
alpar@185
|
409 |
template<typename TT> friend class EdgeMap;
|
alpar@185
|
410 |
|
alpar@185
|
411 |
///\todo It can copy between different types.
|
alpar@590
|
412 |
template<typename TT> EdgeMap(const EdgeMap<TT> &m)
|
alpar@590
|
413 |
: DynMapBase<Edge>(*m.G), container(m.container.size())
|
alpar@185
|
414 |
{
|
alpar@503
|
415 |
G->dyn_edge_maps.push_back(this);
|
alpar@185
|
416 |
typename std::vector<TT>::const_iterator i;
|
alpar@185
|
417 |
for(typename std::vector<TT>::const_iterator i=m.container.begin();
|
alpar@185
|
418 |
i!=m.container.end();
|
alpar@185
|
419 |
i++)
|
alpar@185
|
420 |
container.push_back(*i);
|
alpar@185
|
421 |
}
|
alpar@185
|
422 |
~EdgeMap()
|
alpar@108
|
423 |
{
|
alpar@108
|
424 |
if(G) {
|
alpar@164
|
425 |
std::vector<DynMapBase<Edge>* >::iterator i;
|
alpar@108
|
426 |
for(i=G->dyn_edge_maps.begin();
|
alpar@108
|
427 |
i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
|
alpar@115
|
428 |
//if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
|
alpar@115
|
429 |
//A better way to do that: (Is this really important?)
|
alpar@115
|
430 |
if(*i==this) {
|
alpar@116
|
431 |
*i=G->dyn_edge_maps.back();
|
alpar@115
|
432 |
G->dyn_edge_maps.pop_back();
|
alpar@115
|
433 |
}
|
alpar@108
|
434 |
}
|
alpar@108
|
435 |
}
|
alpar@115
|
436 |
|
alpar@164
|
437 |
void add(const Edge k)
|
alpar@108
|
438 |
{
|
alpar@108
|
439 |
if(k.n>=int(container.size())) container.resize(k.n+1);
|
alpar@108
|
440 |
}
|
alpar@215
|
441 |
void erase(const Edge) { }
|
alpar@108
|
442 |
|
alpar@164
|
443 |
void set(Edge n, T a) { container[n.n]=a; }
|
alpar@209
|
444 |
//T get(Edge n) const { return container[n.n]; }
|
alpar@215
|
445 |
typename std::vector<T>::reference
|
alpar@215
|
446 |
operator[](Edge n) { return container[n.n]; }
|
alpar@215
|
447 |
typename std::vector<T>::const_reference
|
alpar@215
|
448 |
operator[](Edge n) const { return container[n.n]; }
|
alpar@108
|
449 |
|
alpar@185
|
450 |
///\warning There is no safety check at all!
|
alpar@185
|
451 |
///Using operator = between maps attached to different graph may
|
alpar@185
|
452 |
///cause serious problem.
|
alpar@185
|
453 |
///\todo Is this really so?
|
alpar@185
|
454 |
///\todo It can copy between different types.
|
alpar@185
|
455 |
const EdgeMap<T>& operator=(const EdgeMap<T> &m)
|
alpar@185
|
456 |
{
|
alpar@185
|
457 |
container = m.container;
|
alpar@185
|
458 |
return *this;
|
alpar@185
|
459 |
}
|
alpar@185
|
460 |
template<typename TT>
|
alpar@185
|
461 |
const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
|
alpar@185
|
462 |
{
|
alpar@531
|
463 |
std::copy(m.container.begin(), m.container.end(), container.begin());
|
alpar@185
|
464 |
return *this;
|
alpar@185
|
465 |
}
|
alpar@185
|
466 |
|
alpar@108
|
467 |
void update() {} //Useless for DynMaps
|
alpar@108
|
468 |
void update(T a) {} //Useless for DynMaps
|
alpar@108
|
469 |
};
|
alpar@185
|
470 |
|
alpar@104
|
471 |
};
|
alpar@185
|
472 |
|
alpar@185
|
473 |
///Graph for bidirectional edges.
|
alpar@185
|
474 |
|
alpar@185
|
475 |
///The purpose of this graph structure is to handle graphs
|
alpar@185
|
476 |
///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
|
alpar@186
|
477 |
///of oppositely directed edges.
|
alpar@186
|
478 |
///There is a new edge map type called
|
alpar@186
|
479 |
///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
|
alpar@186
|
480 |
///that complements this
|
alpar@186
|
481 |
///feature by
|
alpar@186
|
482 |
///storing shared values for the edge pairs. The usual
|
alpar@186
|
483 |
///\ref GraphSkeleton::EdgeMap "EdgeMap"
|
alpar@186
|
484 |
///can be used
|
alpar@185
|
485 |
///as well.
|
alpar@185
|
486 |
///
|
alpar@186
|
487 |
///The oppositely directed edge can also be obtained easily
|
alpar@186
|
488 |
///using \ref opposite.
|
alpar@186
|
489 |
///\warning It shares the similarity with \ref SmartGraph that
|
alpar@186
|
490 |
///it is not possible to delete edges or nodes from the graph.
|
alpar@186
|
491 |
//\sa \ref SmartGraph.
|
alpar@185
|
492 |
|
alpar@185
|
493 |
class SymSmartGraph : public SmartGraph
|
alpar@185
|
494 |
{
|
alpar@185
|
495 |
public:
|
alpar@186
|
496 |
template<typename T> class SymEdgeMap;
|
alpar@186
|
497 |
template<typename T> friend class SymEdgeMap;
|
alpar@186
|
498 |
|
alpar@185
|
499 |
SymSmartGraph() : SmartGraph() { }
|
alpar@185
|
500 |
SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
|
alpar@398
|
501 |
///Adds a pair of oppositely directed edges to the graph.
|
alpar@185
|
502 |
Edge addEdge(Node u, Node v)
|
alpar@185
|
503 |
{
|
alpar@185
|
504 |
Edge e = SmartGraph::addEdge(u,v);
|
alpar@185
|
505 |
SmartGraph::addEdge(v,u);
|
alpar@185
|
506 |
return e;
|
alpar@185
|
507 |
}
|
alpar@185
|
508 |
|
alpar@186
|
509 |
///The oppositely directed edge.
|
alpar@186
|
510 |
|
alpar@186
|
511 |
///Returns the oppositely directed
|
alpar@186
|
512 |
///pair of the edge \c e.
|
alpar@713
|
513 |
static Edge opposite(Edge e)
|
alpar@185
|
514 |
{
|
alpar@185
|
515 |
Edge f;
|
alpar@185
|
516 |
f.idref() = e.idref() - 2*(e.idref()%2) + 1;
|
alpar@185
|
517 |
return f;
|
alpar@185
|
518 |
}
|
alpar@185
|
519 |
|
alpar@186
|
520 |
///Common data storage for the edge pairs.
|
alpar@186
|
521 |
|
alpar@186
|
522 |
///This map makes it possible to store data shared by the oppositely
|
alpar@186
|
523 |
///directed pairs of edges.
|
alpar@185
|
524 |
template <typename T> class SymEdgeMap : public DynMapBase<Edge>
|
alpar@185
|
525 |
{
|
alpar@185
|
526 |
std::vector<T> container;
|
alpar@185
|
527 |
|
alpar@185
|
528 |
public:
|
alpar@185
|
529 |
typedef T ValueType;
|
alpar@185
|
530 |
typedef Edge KeyType;
|
alpar@185
|
531 |
|
alpar@186
|
532 |
SymEdgeMap(const SymSmartGraph &_G) :
|
alpar@185
|
533 |
DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
|
alpar@185
|
534 |
{
|
alpar@186
|
535 |
static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.push_back(this);
|
alpar@185
|
536 |
}
|
alpar@186
|
537 |
SymEdgeMap(const SymSmartGraph &_G,const T &t) :
|
alpar@185
|
538 |
DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
|
alpar@185
|
539 |
{
|
alpar@185
|
540 |
G->dyn_edge_maps.push_back(this);
|
alpar@185
|
541 |
}
|
alpar@185
|
542 |
|
alpar@185
|
543 |
SymEdgeMap(const SymEdgeMap<T> &m) :
|
alpar@185
|
544 |
DynMapBase<SymEdge>(*m.G), container(m.container)
|
alpar@185
|
545 |
{
|
alpar@185
|
546 |
G->dyn_node_maps.push_back(this);
|
alpar@185
|
547 |
}
|
alpar@185
|
548 |
|
alpar@185
|
549 |
// template<typename TT> friend class SymEdgeMap;
|
alpar@185
|
550 |
|
alpar@185
|
551 |
///\todo It can copy between different types.
|
alpar@185
|
552 |
///
|
alpar@185
|
553 |
|
alpar@590
|
554 |
template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m)
|
alpar@590
|
555 |
: DynMapBase<SymEdge>(*m.G), container(m.container.size())
|
alpar@185
|
556 |
{
|
alpar@185
|
557 |
G->dyn_node_maps.push_back(this);
|
alpar@185
|
558 |
typename std::vector<TT>::const_iterator i;
|
alpar@185
|
559 |
for(typename std::vector<TT>::const_iterator i=m.container.begin();
|
alpar@185
|
560 |
i!=m.container.end();
|
alpar@185
|
561 |
i++)
|
alpar@185
|
562 |
container.push_back(*i);
|
alpar@185
|
563 |
}
|
alpar@185
|
564 |
|
alpar@185
|
565 |
~SymEdgeMap()
|
alpar@185
|
566 |
{
|
alpar@185
|
567 |
if(G) {
|
alpar@185
|
568 |
std::vector<DynMapBase<Edge>* >::iterator i;
|
alpar@186
|
569 |
for(i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.begin();
|
alpar@186
|
570 |
i!=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.end()
|
alpar@186
|
571 |
&& *i!=this; ++i) ;
|
alpar@185
|
572 |
//if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
|
alpar@185
|
573 |
//A better way to do that: (Is this really important?)
|
alpar@185
|
574 |
if(*i==this) {
|
alpar@186
|
575 |
*i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.back();
|
alpar@186
|
576 |
static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.pop_back();
|
alpar@185
|
577 |
}
|
alpar@185
|
578 |
}
|
alpar@185
|
579 |
}
|
alpar@185
|
580 |
|
alpar@185
|
581 |
void add(const Edge k)
|
alpar@185
|
582 |
{
|
alpar@185
|
583 |
if(!k.idref()%2&&k.idref()/2>=int(container.size()))
|
alpar@185
|
584 |
container.resize(k.idref()/2+1);
|
alpar@185
|
585 |
}
|
alpar@185
|
586 |
void erase(const Edge k) { }
|
alpar@185
|
587 |
|
alpar@185
|
588 |
void set(Edge n, T a) { container[n.idref()/2]=a; }
|
alpar@209
|
589 |
//T get(Edge n) const { return container[n.idref()/2]; }
|
alpar@215
|
590 |
typename std::vector<T>::reference
|
alpar@215
|
591 |
operator[](Edge n) { return container[n.idref()/2]; }
|
alpar@215
|
592 |
typename std::vector<T>::const_reference
|
alpar@215
|
593 |
operator[](Edge n) const { return container[n.idref()/2]; }
|
alpar@185
|
594 |
|
alpar@185
|
595 |
///\warning There is no safety check at all!
|
alpar@185
|
596 |
///Using operator = between maps attached to different graph may
|
alpar@185
|
597 |
///cause serious problem.
|
alpar@185
|
598 |
///\todo Is this really so?
|
alpar@185
|
599 |
///\todo It can copy between different types.
|
alpar@185
|
600 |
const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
|
alpar@185
|
601 |
{
|
alpar@185
|
602 |
container = m.container;
|
alpar@185
|
603 |
return *this;
|
alpar@185
|
604 |
}
|
alpar@185
|
605 |
template<typename TT>
|
alpar@185
|
606 |
const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
|
alpar@185
|
607 |
{
|
alpar@531
|
608 |
std::copy(m.container.begin(), m.container.end(), container.begin());
|
alpar@185
|
609 |
return *this;
|
alpar@185
|
610 |
}
|
alpar@185
|
611 |
|
alpar@185
|
612 |
void update() {} //Useless for DynMaps
|
alpar@185
|
613 |
void update(T a) {} //Useless for DynMaps
|
alpar@185
|
614 |
|
alpar@185
|
615 |
};
|
alpar@185
|
616 |
|
alpar@185
|
617 |
};
|
alpar@185
|
618 |
|
alpar@407
|
619 |
/// @}
|
alpar@407
|
620 |
|
alpar@105
|
621 |
} //namespace hugo
|
alpar@104
|
622 |
|
alpar@157
|
623 |
|
alpar@157
|
624 |
|
alpar@157
|
625 |
|
alpar@590
|
626 |
#endif //HUGO_SMART_GRAPH_H
|