1 | // -*- mode:C++ -*- |
---|
2 | |
---|
3 | #ifndef HUGO_LIST_GRAPH_H |
---|
4 | #define HUGO_LIST_GRAPH_H |
---|
5 | |
---|
6 | ///\ingroup graphs |
---|
7 | ///\file |
---|
8 | ///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes. |
---|
9 | |
---|
10 | #include <vector> |
---|
11 | #include <limits.h> |
---|
12 | |
---|
13 | #include <hugo/invalid.h> |
---|
14 | |
---|
15 | namespace hugo { |
---|
16 | |
---|
17 | /// \addtogroup graphs |
---|
18 | /// @{ |
---|
19 | |
---|
20 | class SymListGraph; |
---|
21 | |
---|
22 | ///A list graph class. |
---|
23 | |
---|
24 | ///This is a simple and fast erasable graph implementation. |
---|
25 | /// |
---|
26 | ///It conforms to the graph interface documented under |
---|
27 | ///the description of \ref GraphSkeleton. |
---|
28 | ///\sa \ref GraphSkeleton. |
---|
29 | class ListGraph { |
---|
30 | |
---|
31 | //Nodes are double linked. |
---|
32 | //The free nodes are only single linked using the "next" field. |
---|
33 | struct NodeT |
---|
34 | { |
---|
35 | int first_in,first_out; |
---|
36 | int prev, next; |
---|
37 | // NodeT() {} |
---|
38 | }; |
---|
39 | //Edges are double linked. |
---|
40 | //The free edges are only single linked using the "next_in" field. |
---|
41 | struct EdgeT |
---|
42 | { |
---|
43 | int head, tail; |
---|
44 | int prev_in, prev_out; |
---|
45 | int next_in, next_out; |
---|
46 | //FIXME: is this necessary? |
---|
47 | // EdgeT() : next_in(-1), next_out(-1) prev_in(-1), prev_out(-1) {} |
---|
48 | }; |
---|
49 | |
---|
50 | std::vector<NodeT> nodes; |
---|
51 | //The first node |
---|
52 | int first_node; |
---|
53 | //The first free node |
---|
54 | int first_free_node; |
---|
55 | std::vector<EdgeT> edges; |
---|
56 | //The first free edge |
---|
57 | int first_free_edge; |
---|
58 | |
---|
59 | protected: |
---|
60 | |
---|
61 | template <typename Key> class DynMapBase |
---|
62 | { |
---|
63 | protected: |
---|
64 | const ListGraph* G; |
---|
65 | public: |
---|
66 | virtual void add(const Key k) = 0; |
---|
67 | virtual void erase(const Key k) = 0; |
---|
68 | DynMapBase(const ListGraph &_G) : G(&_G) {} |
---|
69 | virtual ~DynMapBase() {} |
---|
70 | friend class ListGraph; |
---|
71 | }; |
---|
72 | |
---|
73 | public: |
---|
74 | template <typename T> class EdgeMap; |
---|
75 | template <typename T> class NodeMap; |
---|
76 | |
---|
77 | class Node; |
---|
78 | class Edge; |
---|
79 | |
---|
80 | // protected: |
---|
81 | // HELPME: |
---|
82 | protected: |
---|
83 | ///\bug It must be public because of SymEdgeMap. |
---|
84 | /// |
---|
85 | mutable std::vector<DynMapBase<Node> * > dyn_node_maps; |
---|
86 | ///\bug It must be public because of SymEdgeMap. |
---|
87 | /// |
---|
88 | mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps; |
---|
89 | |
---|
90 | public: |
---|
91 | |
---|
92 | class NodeIt; |
---|
93 | class EdgeIt; |
---|
94 | class OutEdgeIt; |
---|
95 | class InEdgeIt; |
---|
96 | |
---|
97 | public: |
---|
98 | |
---|
99 | ListGraph() : nodes(), first_node(-1), |
---|
100 | first_free_node(-1), edges(), first_free_edge(-1) {} |
---|
101 | ListGraph(const ListGraph &_g) : nodes(_g.nodes), first_node(_g.first_node), |
---|
102 | first_free_node(_g.first_free_node), |
---|
103 | edges(_g.edges), |
---|
104 | first_free_edge(_g.first_free_edge) {} |
---|
105 | |
---|
106 | ~ListGraph() |
---|
107 | { |
---|
108 | for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
109 | i!=dyn_node_maps.end(); ++i) (**i).G=NULL; |
---|
110 | for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin(); |
---|
111 | i!=dyn_edge_maps.end(); ++i) (**i).G=NULL; |
---|
112 | } |
---|
113 | |
---|
114 | int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
---|
115 | int edgeNum() const { return edges.size(); } //FIXME: What is this? |
---|
116 | |
---|
117 | ///Set the expected number of edges |
---|
118 | |
---|
119 | ///With this function, it is possible to set the expected number of edges. |
---|
120 | ///The use of this fasten the building of the graph and makes |
---|
121 | ///it possible to avoid the superfluous memory allocation. |
---|
122 | void reserveEdge(int n) { edges.reserve(n); }; |
---|
123 | |
---|
124 | ///\bug This function does something different than |
---|
125 | ///its name would suggests... |
---|
126 | int maxNodeId() const { return nodes.size(); } //FIXME: What is this? |
---|
127 | ///\bug This function does something different than |
---|
128 | ///its name would suggests... |
---|
129 | int maxEdgeId() const { return edges.size(); } //FIXME: What is this? |
---|
130 | |
---|
131 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
132 | Node head(Edge e) const { return edges[e.n].head; } |
---|
133 | |
---|
134 | NodeIt& first(NodeIt& v) const { |
---|
135 | v=NodeIt(*this); return v; } |
---|
136 | EdgeIt& first(EdgeIt& e) const { |
---|
137 | e=EdgeIt(*this); return e; } |
---|
138 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
139 | e=OutEdgeIt(*this,v); return e; } |
---|
140 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
141 | e=InEdgeIt(*this,v); return e; } |
---|
142 | |
---|
143 | static int id(Node v) { return v.n; } |
---|
144 | static int id(Edge e) { return e.n; } |
---|
145 | |
---|
146 | /// Adds a new node to the graph. |
---|
147 | |
---|
148 | /// \todo It adds the nodes in a reversed order. |
---|
149 | /// (i.e. the lastly added node becomes the first.) |
---|
150 | Node addNode() { |
---|
151 | int n; |
---|
152 | |
---|
153 | if(first_free_node==-1) |
---|
154 | { |
---|
155 | n = nodes.size(); |
---|
156 | nodes.push_back(NodeT()); |
---|
157 | } |
---|
158 | else { |
---|
159 | n = first_free_node; |
---|
160 | first_free_node = nodes[n].next; |
---|
161 | } |
---|
162 | |
---|
163 | nodes[n].next = first_node; |
---|
164 | if(first_node != -1) nodes[first_node].prev = n; |
---|
165 | first_node = n; |
---|
166 | nodes[n].prev = -1; |
---|
167 | |
---|
168 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
169 | |
---|
170 | Node nn; nn.n=n; |
---|
171 | |
---|
172 | //Update dynamic maps |
---|
173 | for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
174 | i!=dyn_node_maps.end(); ++i) (**i).add(nn); |
---|
175 | |
---|
176 | return nn; |
---|
177 | } |
---|
178 | |
---|
179 | Edge addEdge(Node u, Node v) { |
---|
180 | int n; |
---|
181 | |
---|
182 | if(first_free_edge==-1) |
---|
183 | { |
---|
184 | n = edges.size(); |
---|
185 | edges.push_back(EdgeT()); |
---|
186 | } |
---|
187 | else { |
---|
188 | n = first_free_edge; |
---|
189 | first_free_edge = edges[n].next_in; |
---|
190 | } |
---|
191 | |
---|
192 | edges[n].tail = u.n; edges[n].head = v.n; |
---|
193 | |
---|
194 | edges[n].next_out = nodes[u.n].first_out; |
---|
195 | if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n; |
---|
196 | edges[n].next_in = nodes[v.n].first_in; |
---|
197 | if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n; |
---|
198 | edges[n].prev_in = edges[n].prev_out = -1; |
---|
199 | |
---|
200 | nodes[u.n].first_out = nodes[v.n].first_in = n; |
---|
201 | |
---|
202 | Edge e; e.n=n; |
---|
203 | |
---|
204 | //Update dynamic maps |
---|
205 | for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin(); |
---|
206 | i!=dyn_edge_maps.end(); ++i) (**i).add(e); |
---|
207 | |
---|
208 | return e; |
---|
209 | } |
---|
210 | |
---|
211 | /// Finds an edge between two nodes. |
---|
212 | |
---|
213 | /// Finds an edge from node \c u to node \c v. |
---|
214 | /// |
---|
215 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
216 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
217 | /// the next edge from \c u to \c v after \c prev. |
---|
218 | /// \return The found edge or INVALID if there is no such an edge. |
---|
219 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
220 | { |
---|
221 | int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; |
---|
222 | while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; |
---|
223 | prev.n=e; |
---|
224 | return prev; |
---|
225 | } |
---|
226 | |
---|
227 | private: |
---|
228 | void eraseEdge(int n) { |
---|
229 | |
---|
230 | if(edges[n].next_in!=-1) |
---|
231 | edges[edges[n].next_in].prev_in = edges[n].prev_in; |
---|
232 | if(edges[n].prev_in!=-1) |
---|
233 | edges[edges[n].prev_in].next_in = edges[n].next_in; |
---|
234 | else nodes[edges[n].head].first_in = edges[n].next_in; |
---|
235 | |
---|
236 | if(edges[n].next_out!=-1) |
---|
237 | edges[edges[n].next_out].prev_out = edges[n].prev_out; |
---|
238 | if(edges[n].prev_out!=-1) |
---|
239 | edges[edges[n].prev_out].next_out = edges[n].next_out; |
---|
240 | else nodes[edges[n].tail].first_out = edges[n].next_out; |
---|
241 | |
---|
242 | edges[n].next_in = first_free_edge; |
---|
243 | first_free_edge = n; |
---|
244 | |
---|
245 | //Update dynamic maps |
---|
246 | Edge e; e.n=n; |
---|
247 | for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin(); |
---|
248 | i!=dyn_edge_maps.end(); ++i) (**i).erase(e); |
---|
249 | } |
---|
250 | |
---|
251 | public: |
---|
252 | |
---|
253 | void erase(Node nn) { |
---|
254 | int n=nn.n; |
---|
255 | |
---|
256 | int m; |
---|
257 | while((m=nodes[n].first_in)!=-1) eraseEdge(m); |
---|
258 | while((m=nodes[n].first_out)!=-1) eraseEdge(m); |
---|
259 | |
---|
260 | if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; |
---|
261 | if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; |
---|
262 | else first_node = nodes[n].next; |
---|
263 | |
---|
264 | nodes[n].next = first_free_node; |
---|
265 | first_free_node = n; |
---|
266 | |
---|
267 | //Update dynamic maps |
---|
268 | for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
269 | i!=dyn_node_maps.end(); ++i) (**i).erase(nn); |
---|
270 | } |
---|
271 | |
---|
272 | void erase(Edge e) { eraseEdge(e.n); } |
---|
273 | |
---|
274 | ///\bug Dynamic maps must be updated! |
---|
275 | /// |
---|
276 | void clear() { |
---|
277 | nodes.clear();edges.clear(); |
---|
278 | first_node=first_free_node=first_free_edge=-1; |
---|
279 | } |
---|
280 | |
---|
281 | class Node { |
---|
282 | friend class ListGraph; |
---|
283 | template <typename T> friend class NodeMap; |
---|
284 | |
---|
285 | friend class Edge; |
---|
286 | friend class OutEdgeIt; |
---|
287 | friend class InEdgeIt; |
---|
288 | friend class SymEdge; |
---|
289 | |
---|
290 | protected: |
---|
291 | int n; |
---|
292 | friend int ListGraph::id(Node v); |
---|
293 | Node(int nn) {n=nn;} |
---|
294 | public: |
---|
295 | Node() {} |
---|
296 | Node (Invalid) { n=-1; } |
---|
297 | bool operator==(const Node i) const {return n==i.n;} |
---|
298 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
299 | bool operator<(const Node i) const {return n<i.n;} |
---|
300 | // ///Validity check |
---|
301 | // operator bool() { return n!=-1; } |
---|
302 | }; |
---|
303 | |
---|
304 | class NodeIt : public Node { |
---|
305 | const ListGraph *G; |
---|
306 | friend class ListGraph; |
---|
307 | public: |
---|
308 | NodeIt() : Node() { } |
---|
309 | NodeIt(Invalid i) : Node(i) { } |
---|
310 | NodeIt(const ListGraph& _G) : Node(_G.first_node), G(&_G) { } |
---|
311 | ///\todo Undocumented conversion Node -\> NodeIt. |
---|
312 | NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { } |
---|
313 | NodeIt &operator++() { |
---|
314 | n=G->nodes[n].next; |
---|
315 | return *this; |
---|
316 | } |
---|
317 | // ///Validity check |
---|
318 | // operator bool() { return Node::operator bool(); } |
---|
319 | }; |
---|
320 | |
---|
321 | class Edge { |
---|
322 | friend class ListGraph; |
---|
323 | template <typename T> friend class EdgeMap; |
---|
324 | |
---|
325 | //template <typename T> friend class SymListGraph::SymEdgeMap; |
---|
326 | //friend Edge SymListGraph::opposite(Edge) const; |
---|
327 | |
---|
328 | friend class Node; |
---|
329 | friend class NodeIt; |
---|
330 | protected: |
---|
331 | int n; |
---|
332 | friend int ListGraph::id(Edge e); |
---|
333 | |
---|
334 | public: |
---|
335 | /// An Edge with id \c n. |
---|
336 | |
---|
337 | /// \bug It should be |
---|
338 | /// obtained by a member function of the Graph. |
---|
339 | Edge(int nn) {n=nn;} |
---|
340 | |
---|
341 | Edge() { } |
---|
342 | Edge (Invalid) { n=-1; } |
---|
343 | bool operator==(const Edge i) const {return n==i.n;} |
---|
344 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
345 | bool operator<(const Edge i) const {return n<i.n;} |
---|
346 | ///\bug This is a workaround until somebody tells me how to |
---|
347 | ///make class \c SymListGraph::SymEdgeMap friend of Edge |
---|
348 | int &idref() {return n;} |
---|
349 | const int &idref() const {return n;} |
---|
350 | // ///Validity check |
---|
351 | // operator bool() { return n!=-1; } |
---|
352 | }; |
---|
353 | |
---|
354 | class EdgeIt : public Edge { |
---|
355 | const ListGraph *G; |
---|
356 | friend class ListGraph; |
---|
357 | public: |
---|
358 | EdgeIt(const ListGraph& _G) : Edge(), G(&_G) { |
---|
359 | int m; |
---|
360 | for(m=_G.first_node; |
---|
361 | m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next); |
---|
362 | n = (m==-1)?-1:_G.nodes[m].first_in; |
---|
363 | } |
---|
364 | EdgeIt (Invalid i) : Edge(i) { } |
---|
365 | EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
366 | EdgeIt() : Edge() { } |
---|
367 | ///\bug This is a workaround until somebody tells me how to |
---|
368 | ///make class \c SymListGraph::SymEdgeMap friend of Edge |
---|
369 | int &idref() {return n;} |
---|
370 | EdgeIt &operator++() { |
---|
371 | if(G->edges[n].next_in!=-1) n=G->edges[n].next_in; |
---|
372 | else { |
---|
373 | int nn; |
---|
374 | for(nn=G->nodes[G->edges[n].head].next; |
---|
375 | nn!=-1 && G->nodes[nn].first_in == -1; |
---|
376 | nn = G->nodes[nn].next) ; |
---|
377 | n = (nn==-1)?-1:G->nodes[nn].first_in; |
---|
378 | } |
---|
379 | return *this; |
---|
380 | } |
---|
381 | // ///Validity check |
---|
382 | // operator bool() { return Edge::operator bool(); } |
---|
383 | }; |
---|
384 | |
---|
385 | class OutEdgeIt : public Edge { |
---|
386 | const ListGraph *G; |
---|
387 | friend class ListGraph; |
---|
388 | public: |
---|
389 | OutEdgeIt() : Edge() { } |
---|
390 | OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
391 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
392 | |
---|
393 | OutEdgeIt(const ListGraph& _G,const Node v) |
---|
394 | : Edge(_G.nodes[v.n].first_out), G(&_G) {} |
---|
395 | OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; } |
---|
396 | // ///Validity check |
---|
397 | // operator bool() { return Edge::operator bool(); } |
---|
398 | }; |
---|
399 | |
---|
400 | class InEdgeIt : public Edge { |
---|
401 | const ListGraph *G; |
---|
402 | friend class ListGraph; |
---|
403 | public: |
---|
404 | InEdgeIt() : Edge() { } |
---|
405 | InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
406 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
407 | InEdgeIt(const ListGraph& _G,Node v) |
---|
408 | : Edge(_G.nodes[v.n].first_in), G(&_G) { } |
---|
409 | InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } |
---|
410 | // ///Validity check |
---|
411 | // operator bool() { return Edge::operator bool(); } |
---|
412 | }; |
---|
413 | |
---|
414 | template <typename T> class NodeMap : public DynMapBase<Node> |
---|
415 | { |
---|
416 | std::vector<T> container; |
---|
417 | |
---|
418 | public: |
---|
419 | typedef T ValueType; |
---|
420 | typedef Node KeyType; |
---|
421 | |
---|
422 | NodeMap(const ListGraph &_G) : |
---|
423 | DynMapBase<Node>(_G), container(_G.maxNodeId()) |
---|
424 | { |
---|
425 | G->dyn_node_maps.push_back(this); |
---|
426 | } |
---|
427 | NodeMap(const ListGraph &_G,const T &t) : |
---|
428 | DynMapBase<Node>(_G), container(_G.maxNodeId(),t) |
---|
429 | { |
---|
430 | G->dyn_node_maps.push_back(this); |
---|
431 | } |
---|
432 | |
---|
433 | NodeMap(const NodeMap<T> &m) : |
---|
434 | DynMapBase<Node>(*m.G), container(m.container) |
---|
435 | { |
---|
436 | G->dyn_node_maps.push_back(this); |
---|
437 | } |
---|
438 | |
---|
439 | template<typename TT> friend class NodeMap; |
---|
440 | |
---|
441 | ///\todo It can copy between different types. |
---|
442 | /// |
---|
443 | template<typename TT> NodeMap(const NodeMap<TT> &m) : |
---|
444 | DynMapBase<Node>(*m.G), container(m.container.size()) |
---|
445 | |
---|
446 | { |
---|
447 | G->dyn_node_maps.push_back(this); |
---|
448 | typename std::vector<TT>::const_iterator i; |
---|
449 | for(typename std::vector<TT>::const_iterator i=m.container.begin(); |
---|
450 | i!=m.container.end(); |
---|
451 | i++) |
---|
452 | container.push_back(*i); |
---|
453 | } |
---|
454 | ~NodeMap() |
---|
455 | { |
---|
456 | if(G) { |
---|
457 | std::vector<DynMapBase<Node>* >::iterator i; |
---|
458 | for(i=G->dyn_node_maps.begin(); |
---|
459 | i!=G->dyn_node_maps.end() && *i!=this; ++i) ; |
---|
460 | //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow... |
---|
461 | //A better way to do that: (Is this really important?) |
---|
462 | if(*i==this) { |
---|
463 | *i=G->dyn_node_maps.back(); |
---|
464 | G->dyn_node_maps.pop_back(); |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | void add(const Node k) |
---|
470 | { |
---|
471 | if(k.n>=int(container.size())) container.resize(k.n+1); |
---|
472 | } |
---|
473 | |
---|
474 | void erase(const Node) { } |
---|
475 | |
---|
476 | void set(Node n, T a) { container[n.n]=a; } |
---|
477 | //'T& operator[](Node n)' would be wrong here |
---|
478 | typename std::vector<T>::reference |
---|
479 | operator[](Node n) { return container[n.n]; } |
---|
480 | //'const T& operator[](Node n)' would be wrong here |
---|
481 | typename std::vector<T>::const_reference |
---|
482 | operator[](Node n) const { return container[n.n]; } |
---|
483 | |
---|
484 | ///\warning There is no safety check at all! |
---|
485 | ///Using operator = between maps attached to different graph may |
---|
486 | ///cause serious problem. |
---|
487 | ///\todo Is this really so? |
---|
488 | ///\todo It can copy between different types. |
---|
489 | const NodeMap<T>& operator=(const NodeMap<T> &m) |
---|
490 | { |
---|
491 | container = m.container; |
---|
492 | return *this; |
---|
493 | } |
---|
494 | template<typename TT> |
---|
495 | const NodeMap<T>& operator=(const NodeMap<TT> &m) |
---|
496 | { |
---|
497 | std::copy(m.container.begin(), m.container.end(), container.begin()); |
---|
498 | return *this; |
---|
499 | } |
---|
500 | |
---|
501 | void update() {} //Useless for Dynamic Maps |
---|
502 | void update(T a) {} //Useless for Dynamic Maps |
---|
503 | }; |
---|
504 | |
---|
505 | template <typename T> class EdgeMap : public DynMapBase<Edge> |
---|
506 | { |
---|
507 | protected: |
---|
508 | std::vector<T> container; |
---|
509 | |
---|
510 | public: |
---|
511 | typedef T ValueType; |
---|
512 | typedef Edge KeyType; |
---|
513 | |
---|
514 | EdgeMap(const ListGraph &_G) : |
---|
515 | DynMapBase<Edge>(_G), container(_G.maxEdgeId()) |
---|
516 | { |
---|
517 | //FIXME: What if there are empty Id's? |
---|
518 | //FIXME: Can I use 'this' in a constructor? |
---|
519 | G->dyn_edge_maps.push_back(this); |
---|
520 | } |
---|
521 | EdgeMap(const ListGraph &_G,const T &t) : |
---|
522 | DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t) |
---|
523 | { |
---|
524 | G->dyn_edge_maps.push_back(this); |
---|
525 | } |
---|
526 | EdgeMap(const EdgeMap<T> &m) : |
---|
527 | DynMapBase<Edge>(*m.G), container(m.container) |
---|
528 | { |
---|
529 | G->dyn_edge_maps.push_back(this); |
---|
530 | } |
---|
531 | |
---|
532 | template<typename TT> friend class EdgeMap; |
---|
533 | |
---|
534 | ///\todo It can copy between different types. |
---|
535 | /// |
---|
536 | template<typename TT> EdgeMap(const EdgeMap<TT> &m) : |
---|
537 | DynMapBase<Edge>(*m.G), container(m.container.size()) |
---|
538 | { |
---|
539 | G->dyn_edge_maps.push_back(this); |
---|
540 | typename std::vector<TT>::const_iterator i; |
---|
541 | for(typename std::vector<TT>::const_iterator i=m.container.begin(); |
---|
542 | i!=m.container.end(); |
---|
543 | i++) |
---|
544 | container.push_back(*i); |
---|
545 | } |
---|
546 | ~EdgeMap() |
---|
547 | { |
---|
548 | if(G) { |
---|
549 | std::vector<DynMapBase<Edge>* >::iterator i; |
---|
550 | for(i=G->dyn_edge_maps.begin(); |
---|
551 | i!=G->dyn_edge_maps.end() && *i!=this; ++i) ; |
---|
552 | //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow... |
---|
553 | //A better way to do that: (Is this really important?) |
---|
554 | if(*i==this) { |
---|
555 | *i=G->dyn_edge_maps.back(); |
---|
556 | G->dyn_edge_maps.pop_back(); |
---|
557 | } |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | void add(const Edge k) |
---|
562 | { |
---|
563 | if(k.n>=int(container.size())) container.resize(k.n+1); |
---|
564 | } |
---|
565 | void erase(const Edge) { } |
---|
566 | |
---|
567 | void set(Edge n, T a) { container[n.n]=a; } |
---|
568 | //T get(Edge n) const { return container[n.n]; } |
---|
569 | typename std::vector<T>::reference |
---|
570 | operator[](Edge n) { return container[n.n]; } |
---|
571 | typename std::vector<T>::const_reference |
---|
572 | operator[](Edge n) const { return container[n.n]; } |
---|
573 | |
---|
574 | ///\warning There is no safety check at all! |
---|
575 | ///Using operator = between maps attached to different graph may |
---|
576 | ///cause serious problem. |
---|
577 | ///\todo Is this really so? |
---|
578 | ///\todo It can copy between different types. |
---|
579 | const EdgeMap<T>& operator=(const EdgeMap<T> &m) |
---|
580 | { |
---|
581 | container = m.container; |
---|
582 | return *this; |
---|
583 | } |
---|
584 | template<typename TT> |
---|
585 | const EdgeMap<T>& operator=(const EdgeMap<TT> &m) |
---|
586 | { |
---|
587 | std::copy(m.container.begin(), m.container.end(), container.begin()); |
---|
588 | return *this; |
---|
589 | } |
---|
590 | |
---|
591 | void update() {} //Useless for DynMaps |
---|
592 | void update(T a) {} //Useless for DynMaps |
---|
593 | }; |
---|
594 | |
---|
595 | }; |
---|
596 | |
---|
597 | ///Graph for bidirectional edges. |
---|
598 | |
---|
599 | ///The purpose of this graph structure is to handle graphs |
---|
600 | ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair |
---|
601 | ///of oppositely directed edges. |
---|
602 | ///There is a new edge map type called |
---|
603 | ///\ref SymListGraph::SymEdgeMap "SymEdgeMap" |
---|
604 | ///that complements this |
---|
605 | ///feature by |
---|
606 | ///storing shared values for the edge pairs. The usual |
---|
607 | ///\ref GraphSkeleton::EdgeMap "EdgeMap" |
---|
608 | ///can be used |
---|
609 | ///as well. |
---|
610 | /// |
---|
611 | ///The oppositely directed edge can also be obtained easily |
---|
612 | ///using \ref opposite. |
---|
613 | /// |
---|
614 | ///Here erase(Edge) deletes a pair of edges. |
---|
615 | /// |
---|
616 | ///\todo this date structure need some reconsiderations. Maybe it |
---|
617 | ///should be implemented independently from ListGraph. |
---|
618 | |
---|
619 | class SymListGraph : public ListGraph |
---|
620 | { |
---|
621 | public: |
---|
622 | template<typename T> class SymEdgeMap; |
---|
623 | template<typename T> friend class SymEdgeMap; |
---|
624 | |
---|
625 | SymListGraph() : ListGraph() { } |
---|
626 | SymListGraph(const ListGraph &_g) : ListGraph(_g) { } |
---|
627 | ///Adds a pair of oppositely directed edges to the graph. |
---|
628 | Edge addEdge(Node u, Node v) |
---|
629 | { |
---|
630 | Edge e = ListGraph::addEdge(u,v); |
---|
631 | ListGraph::addEdge(v,u); |
---|
632 | return e; |
---|
633 | } |
---|
634 | |
---|
635 | void erase(Node n) { ListGraph::erase(n); } |
---|
636 | ///The oppositely directed edge. |
---|
637 | |
---|
638 | ///Returns the oppositely directed |
---|
639 | ///pair of the edge \c e. |
---|
640 | static Edge opposite(Edge e) |
---|
641 | { |
---|
642 | Edge f; |
---|
643 | f.idref() = e.idref() - 2*(e.idref()%2) + 1; |
---|
644 | return f; |
---|
645 | } |
---|
646 | |
---|
647 | ///Removes a pair of oppositely directed edges to the graph. |
---|
648 | void erase(Edge e) { |
---|
649 | ListGraph::erase(opposite(e)); |
---|
650 | ListGraph::erase(e); |
---|
651 | } |
---|
652 | |
---|
653 | ///Common data storage for the edge pairs. |
---|
654 | |
---|
655 | ///This map makes it possible to store data shared by the oppositely |
---|
656 | ///directed pairs of edges. |
---|
657 | template <typename T> class SymEdgeMap : public DynMapBase<Edge> |
---|
658 | { |
---|
659 | std::vector<T> container; |
---|
660 | |
---|
661 | public: |
---|
662 | typedef T ValueType; |
---|
663 | typedef Edge KeyType; |
---|
664 | |
---|
665 | SymEdgeMap(const SymListGraph &_G) : |
---|
666 | DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2) |
---|
667 | { |
---|
668 | static_cast<const SymListGraph*>(G)->dyn_edge_maps.push_back(this); |
---|
669 | } |
---|
670 | SymEdgeMap(const SymListGraph &_G,const T &t) : |
---|
671 | DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t) |
---|
672 | { |
---|
673 | G->dyn_edge_maps.push_back(this); |
---|
674 | } |
---|
675 | |
---|
676 | SymEdgeMap(const SymEdgeMap<T> &m) : |
---|
677 | DynMapBase<SymEdge>(*m.G), container(m.container) |
---|
678 | { |
---|
679 | G->dyn_node_maps.push_back(this); |
---|
680 | } |
---|
681 | |
---|
682 | // template<typename TT> friend class SymEdgeMap; |
---|
683 | |
---|
684 | ///\todo It can copy between different types. |
---|
685 | /// |
---|
686 | |
---|
687 | template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) : |
---|
688 | DynMapBase<SymEdge>(*m.G), container(m.container.size()) |
---|
689 | { |
---|
690 | G->dyn_node_maps.push_back(this); |
---|
691 | typename std::vector<TT>::const_iterator i; |
---|
692 | for(typename std::vector<TT>::const_iterator i=m.container.begin(); |
---|
693 | i!=m.container.end(); |
---|
694 | i++) |
---|
695 | container.push_back(*i); |
---|
696 | } |
---|
697 | |
---|
698 | ~SymEdgeMap() |
---|
699 | { |
---|
700 | if(G) { |
---|
701 | std::vector<DynMapBase<Edge>* >::iterator i; |
---|
702 | for(i=static_cast<const SymListGraph*>(G)->dyn_edge_maps.begin(); |
---|
703 | i!=static_cast<const SymListGraph*>(G)->dyn_edge_maps.end() |
---|
704 | && *i!=this; ++i) ; |
---|
705 | //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow... |
---|
706 | //A better way to do that: (Is this really important?) |
---|
707 | if(*i==this) { |
---|
708 | *i=static_cast<const SymListGraph*>(G)->dyn_edge_maps.back(); |
---|
709 | static_cast<const SymListGraph*>(G)->dyn_edge_maps.pop_back(); |
---|
710 | } |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | void add(const Edge k) |
---|
715 | { |
---|
716 | if(!k.idref()%2&&k.idref()/2>=int(container.size())) |
---|
717 | container.resize(k.idref()/2+1); |
---|
718 | } |
---|
719 | void erase(const Edge k) { } |
---|
720 | |
---|
721 | void set(Edge n, T a) { container[n.idref()/2]=a; } |
---|
722 | //T get(Edge n) const { return container[n.idref()/2]; } |
---|
723 | typename std::vector<T>::reference |
---|
724 | operator[](Edge n) { return container[n.idref()/2]; } |
---|
725 | typename std::vector<T>::const_reference |
---|
726 | operator[](Edge n) const { return container[n.idref()/2]; } |
---|
727 | |
---|
728 | ///\warning There is no safety check at all! |
---|
729 | ///Using operator = between maps attached to different graph may |
---|
730 | ///cause serious problem. |
---|
731 | ///\todo Is this really so? |
---|
732 | ///\todo It can copy between different types. |
---|
733 | const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m) |
---|
734 | { |
---|
735 | container = m.container; |
---|
736 | return *this; |
---|
737 | } |
---|
738 | template<typename TT> |
---|
739 | const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m) |
---|
740 | { |
---|
741 | std::copy(m.container.begin(), m.container.end(), container.begin()); |
---|
742 | return *this; |
---|
743 | } |
---|
744 | |
---|
745 | void update() {} //Useless for DynMaps |
---|
746 | void update(T a) {} //Useless for DynMaps |
---|
747 | |
---|
748 | }; |
---|
749 | |
---|
750 | }; |
---|
751 | |
---|
752 | |
---|
753 | ///A graph class containing only nodes. |
---|
754 | |
---|
755 | ///This class implements a graph structure without edges. |
---|
756 | ///The most useful application of this class is to be the node set of an |
---|
757 | ///\ref EdgeSet class. |
---|
758 | /// |
---|
759 | ///It conforms to the graph interface documented under |
---|
760 | ///the description of \ref GraphSkeleton with the exception that you cannot |
---|
761 | ///add (or delete) edges. The usual edge iterators are exists, but they are |
---|
762 | ///always \ref INVALID. |
---|
763 | ///\sa \ref GraphSkeleton |
---|
764 | ///\sa \ref EdgeSet |
---|
765 | class NodeSet { |
---|
766 | |
---|
767 | //Nodes are double linked. |
---|
768 | //The free nodes are only single linked using the "next" field. |
---|
769 | struct NodeT |
---|
770 | { |
---|
771 | int first_in,first_out; |
---|
772 | int prev, next; |
---|
773 | // NodeT() {} |
---|
774 | }; |
---|
775 | |
---|
776 | std::vector<NodeT> nodes; |
---|
777 | //The first node |
---|
778 | int first_node; |
---|
779 | //The first free node |
---|
780 | int first_free_node; |
---|
781 | |
---|
782 | protected: |
---|
783 | |
---|
784 | template <typename Key> class DynMapBase |
---|
785 | { |
---|
786 | protected: |
---|
787 | const NodeSet* G; |
---|
788 | public: |
---|
789 | virtual void add(const Key k) = 0; |
---|
790 | virtual void erase(const Key k) = 0; |
---|
791 | DynMapBase(const NodeSet &_G) : G(&_G) {} |
---|
792 | virtual ~DynMapBase() {} |
---|
793 | friend class NodeSet; |
---|
794 | }; |
---|
795 | |
---|
796 | public: |
---|
797 | template <typename T> class EdgeMap; |
---|
798 | template <typename T> class NodeMap; |
---|
799 | |
---|
800 | class Node; |
---|
801 | class Edge; |
---|
802 | |
---|
803 | // protected: |
---|
804 | // HELPME: |
---|
805 | protected: |
---|
806 | ///\bug It must be public because of SymEdgeMap. |
---|
807 | /// |
---|
808 | mutable std::vector<DynMapBase<Node> * > dyn_node_maps; |
---|
809 | //mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps; |
---|
810 | |
---|
811 | public: |
---|
812 | |
---|
813 | class NodeIt; |
---|
814 | class EdgeIt; |
---|
815 | class OutEdgeIt; |
---|
816 | class InEdgeIt; |
---|
817 | |
---|
818 | template <typename T> class NodeMap; |
---|
819 | template <typename T> class EdgeMap; |
---|
820 | |
---|
821 | public: |
---|
822 | |
---|
823 | ///Default constructor |
---|
824 | NodeSet() : nodes(), first_node(-1), |
---|
825 | first_free_node(-1) {} |
---|
826 | ///Copy constructor |
---|
827 | NodeSet(const NodeSet &_g) : nodes(_g.nodes), first_node(_g.first_node), |
---|
828 | first_free_node(_g.first_free_node) {} |
---|
829 | |
---|
830 | ~NodeSet() |
---|
831 | { |
---|
832 | for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
833 | i!=dyn_node_maps.end(); ++i) (**i).G=NULL; |
---|
834 | //for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin(); |
---|
835 | // i!=dyn_edge_maps.end(); ++i) (**i).G=NULL; |
---|
836 | } |
---|
837 | |
---|
838 | int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
---|
839 | int edgeNum() const { return 0; } //FIXME: What is this? |
---|
840 | |
---|
841 | ///\bug This function does something different than |
---|
842 | ///its name would suggests... |
---|
843 | int maxNodeId() const { return nodes.size(); } //FIXME: What is this? |
---|
844 | ///\bug This function does something different than |
---|
845 | ///its name would suggests... |
---|
846 | int maxEdgeId() const { return 0; } //FIXME: What is this? |
---|
847 | |
---|
848 | Node tail(Edge e) const { return INVALID; } |
---|
849 | Node head(Edge e) const { return INVALID; } |
---|
850 | |
---|
851 | NodeIt& first(NodeIt& v) const { |
---|
852 | v=NodeIt(*this); return v; } |
---|
853 | EdgeIt& first(EdgeIt& e) const { |
---|
854 | e=EdgeIt(*this); return e; } |
---|
855 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
856 | e=OutEdgeIt(*this,v); return e; } |
---|
857 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
858 | e=InEdgeIt(*this,v); return e; } |
---|
859 | |
---|
860 | int id(Node v) const { return v.n; } |
---|
861 | int id(Edge e) const { return -1; } |
---|
862 | |
---|
863 | /// Adds a new node to the graph. |
---|
864 | |
---|
865 | /// \todo It adds the nodes in a reversed order. |
---|
866 | /// (i.e. the lastly added node becomes the first.) |
---|
867 | Node addNode() { |
---|
868 | int n; |
---|
869 | |
---|
870 | if(first_free_node==-1) |
---|
871 | { |
---|
872 | n = nodes.size(); |
---|
873 | nodes.push_back(NodeT()); |
---|
874 | } |
---|
875 | else { |
---|
876 | n = first_free_node; |
---|
877 | first_free_node = nodes[n].next; |
---|
878 | } |
---|
879 | |
---|
880 | nodes[n].next = first_node; |
---|
881 | if(first_node != -1) nodes[first_node].prev = n; |
---|
882 | first_node = n; |
---|
883 | nodes[n].prev = -1; |
---|
884 | |
---|
885 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
886 | |
---|
887 | Node nn; nn.n=n; |
---|
888 | |
---|
889 | //Update dynamic maps |
---|
890 | for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
891 | i!=dyn_node_maps.end(); ++i) (**i).add(nn); |
---|
892 | |
---|
893 | return nn; |
---|
894 | } |
---|
895 | |
---|
896 | void erase(Node nn) { |
---|
897 | int n=nn.n; |
---|
898 | |
---|
899 | if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; |
---|
900 | if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; |
---|
901 | else first_node = nodes[n].next; |
---|
902 | |
---|
903 | nodes[n].next = first_free_node; |
---|
904 | first_free_node = n; |
---|
905 | |
---|
906 | //Update dynamic maps |
---|
907 | for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
908 | i!=dyn_node_maps.end(); ++i) (**i).erase(nn); |
---|
909 | } |
---|
910 | |
---|
911 | |
---|
912 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
913 | { |
---|
914 | return INVALID; |
---|
915 | } |
---|
916 | |
---|
917 | ///\bug Dynamic maps must be updated! |
---|
918 | /// |
---|
919 | void clear() { |
---|
920 | nodes.clear(); |
---|
921 | first_node = first_free_node = -1; |
---|
922 | } |
---|
923 | |
---|
924 | class Node { |
---|
925 | friend class NodeSet; |
---|
926 | template <typename T> friend class NodeMap; |
---|
927 | |
---|
928 | friend class Edge; |
---|
929 | friend class OutEdgeIt; |
---|
930 | friend class InEdgeIt; |
---|
931 | |
---|
932 | protected: |
---|
933 | int n; |
---|
934 | friend int NodeSet::id(Node v) const; |
---|
935 | Node(int nn) {n=nn;} |
---|
936 | public: |
---|
937 | Node() {} |
---|
938 | Node (Invalid i) { n=-1; } |
---|
939 | bool operator==(const Node i) const {return n==i.n;} |
---|
940 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
941 | bool operator<(const Node i) const {return n<i.n;} |
---|
942 | }; |
---|
943 | |
---|
944 | class NodeIt : public Node { |
---|
945 | const NodeSet *G; |
---|
946 | friend class NodeSet; |
---|
947 | public: |
---|
948 | NodeIt() : Node() { } |
---|
949 | NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { } |
---|
950 | NodeIt(Invalid i) : Node(i) { } |
---|
951 | NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { } |
---|
952 | NodeIt &operator++() { |
---|
953 | n=G->nodes[n].next; |
---|
954 | return *this; |
---|
955 | } |
---|
956 | }; |
---|
957 | |
---|
958 | class Edge { |
---|
959 | //friend class NodeSet; |
---|
960 | //template <typename T> friend class EdgeMap; |
---|
961 | |
---|
962 | //template <typename T> friend class SymNodeSet::SymEdgeMap; |
---|
963 | //friend Edge SymNodeSet::opposite(Edge) const; |
---|
964 | |
---|
965 | // friend class Node; |
---|
966 | // friend class NodeIt; |
---|
967 | protected: |
---|
968 | //friend int NodeSet::id(Edge e) const; |
---|
969 | // Edge(int nn) {} |
---|
970 | public: |
---|
971 | Edge() { } |
---|
972 | Edge (Invalid) { } |
---|
973 | bool operator==(const Edge i) const {return true;} |
---|
974 | bool operator!=(const Edge i) const {return false;} |
---|
975 | bool operator<(const Edge i) const {return false;} |
---|
976 | ///\bug This is a workaround until somebody tells me how to |
---|
977 | ///make class \c SymNodeSet::SymEdgeMap friend of Edge |
---|
978 | // int idref() {return -1;} |
---|
979 | // int idref() const {return -1;} |
---|
980 | }; |
---|
981 | |
---|
982 | class EdgeIt : public Edge { |
---|
983 | //friend class NodeSet; |
---|
984 | public: |
---|
985 | EdgeIt(const NodeSet& G) : Edge() { } |
---|
986 | EdgeIt(const NodeSet&, Edge) : Edge() { } |
---|
987 | EdgeIt (Invalid i) : Edge(i) { } |
---|
988 | EdgeIt() : Edge() { } |
---|
989 | ///\bug This is a workaround until somebody tells me how to |
---|
990 | ///make class \c SymNodeSet::SymEdgeMap friend of Edge |
---|
991 | // int idref() {return -1;} |
---|
992 | EdgeIt operator++() { return INVALID; } |
---|
993 | }; |
---|
994 | |
---|
995 | class OutEdgeIt : public Edge { |
---|
996 | friend class NodeSet; |
---|
997 | public: |
---|
998 | OutEdgeIt() : Edge() { } |
---|
999 | OutEdgeIt(const NodeSet&, Edge) : Edge() { } |
---|
1000 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
1001 | OutEdgeIt(const NodeSet& G,const Node v) : Edge() {} |
---|
1002 | OutEdgeIt operator++() { return INVALID; } |
---|
1003 | }; |
---|
1004 | |
---|
1005 | class InEdgeIt : public Edge { |
---|
1006 | friend class NodeSet; |
---|
1007 | public: |
---|
1008 | InEdgeIt() : Edge() { } |
---|
1009 | InEdgeIt(const NodeSet&, Edge) : Edge() { } |
---|
1010 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
1011 | InEdgeIt(const NodeSet& G,Node v) :Edge() {} |
---|
1012 | InEdgeIt operator++() { return INVALID; } |
---|
1013 | }; |
---|
1014 | |
---|
1015 | template <typename T> class NodeMap : public DynMapBase<Node> |
---|
1016 | { |
---|
1017 | std::vector<T> container; |
---|
1018 | |
---|
1019 | public: |
---|
1020 | typedef T ValueType; |
---|
1021 | typedef Node KeyType; |
---|
1022 | |
---|
1023 | NodeMap(const NodeSet &_G) : |
---|
1024 | DynMapBase<Node>(_G), container(_G.maxNodeId()) |
---|
1025 | { |
---|
1026 | G->dyn_node_maps.push_back(this); |
---|
1027 | } |
---|
1028 | NodeMap(const NodeSet &_G,const T &t) : |
---|
1029 | DynMapBase<Node>(_G), container(_G.maxNodeId(),t) |
---|
1030 | { |
---|
1031 | G->dyn_node_maps.push_back(this); |
---|
1032 | } |
---|
1033 | |
---|
1034 | NodeMap(const NodeMap<T> &m) : |
---|
1035 | DynMapBase<Node>(*m.G), container(m.container) |
---|
1036 | { |
---|
1037 | G->dyn_node_maps.push_back(this); |
---|
1038 | } |
---|
1039 | |
---|
1040 | template<typename TT> friend class NodeMap; |
---|
1041 | |
---|
1042 | ///\todo It can copy between different types. |
---|
1043 | /// |
---|
1044 | template<typename TT> NodeMap(const NodeMap<TT> &m) : |
---|
1045 | DynMapBase<Node>(*m.G), container(m.container.size()) |
---|
1046 | { |
---|
1047 | G->dyn_node_maps.push_back(this); |
---|
1048 | typename std::vector<TT>::const_iterator i; |
---|
1049 | for(typename std::vector<TT>::const_iterator i=m.container.begin(); |
---|
1050 | i!=m.container.end(); |
---|
1051 | i++) |
---|
1052 | container.push_back(*i); |
---|
1053 | } |
---|
1054 | ~NodeMap() |
---|
1055 | { |
---|
1056 | if(G) { |
---|
1057 | std::vector<DynMapBase<Node>* >::iterator i; |
---|
1058 | for(i=G->dyn_node_maps.begin(); |
---|
1059 | i!=G->dyn_node_maps.end() && *i!=this; ++i) ; |
---|
1060 | //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow... |
---|
1061 | //A better way to do that: (Is this really important?) |
---|
1062 | if(*i==this) { |
---|
1063 | *i=G->dyn_node_maps.back(); |
---|
1064 | G->dyn_node_maps.pop_back(); |
---|
1065 | } |
---|
1066 | } |
---|
1067 | } |
---|
1068 | |
---|
1069 | void add(const Node k) |
---|
1070 | { |
---|
1071 | if(k.n>=int(container.size())) container.resize(k.n+1); |
---|
1072 | } |
---|
1073 | |
---|
1074 | void erase(const Node) { } |
---|
1075 | |
---|
1076 | void set(Node n, T a) { container[n.n]=a; } |
---|
1077 | //'T& operator[](Node n)' would be wrong here |
---|
1078 | typename std::vector<T>::reference |
---|
1079 | operator[](Node n) { return container[n.n]; } |
---|
1080 | //'const T& operator[](Node n)' would be wrong here |
---|
1081 | typename std::vector<T>::const_reference |
---|
1082 | operator[](Node n) const { return container[n.n]; } |
---|
1083 | |
---|
1084 | ///\warning There is no safety check at all! |
---|
1085 | ///Using operator = between maps attached to different graph may |
---|
1086 | ///cause serious problem. |
---|
1087 | ///\todo Is this really so? |
---|
1088 | ///\todo It can copy between different types. |
---|
1089 | const NodeMap<T>& operator=(const NodeMap<T> &m) |
---|
1090 | { |
---|
1091 | container = m.container; |
---|
1092 | return *this; |
---|
1093 | } |
---|
1094 | template<typename TT> |
---|
1095 | const NodeMap<T>& operator=(const NodeMap<TT> &m) |
---|
1096 | { |
---|
1097 | std::copy(m.container.begin(), m.container.end(), container.begin()); |
---|
1098 | return *this; |
---|
1099 | } |
---|
1100 | |
---|
1101 | void update() {} //Useless for Dynamic Maps |
---|
1102 | void update(T a) {} //Useless for Dynamic Maps |
---|
1103 | }; |
---|
1104 | |
---|
1105 | template <typename T> class EdgeMap |
---|
1106 | { |
---|
1107 | public: |
---|
1108 | typedef T ValueType; |
---|
1109 | typedef Edge KeyType; |
---|
1110 | |
---|
1111 | EdgeMap(const NodeSet &) { } |
---|
1112 | EdgeMap(const NodeSet &,const T &) { } |
---|
1113 | EdgeMap(const EdgeMap<T> &) { } |
---|
1114 | // template<typename TT> friend class EdgeMap; |
---|
1115 | |
---|
1116 | ///\todo It can copy between different types. |
---|
1117 | /// |
---|
1118 | template<typename TT> EdgeMap(const EdgeMap<TT> &) { } |
---|
1119 | ~EdgeMap() { } |
---|
1120 | |
---|
1121 | void add(const Edge ) { } |
---|
1122 | void erase(const Edge) { } |
---|
1123 | |
---|
1124 | void set(Edge, T) { } |
---|
1125 | //T get(Edge n) const { return container[n.n]; } |
---|
1126 | ValueType &operator[](Edge) { return *((T*)(NULL)); } |
---|
1127 | const ValueType &operator[](Edge) const { return *((T*)(NULL)); } |
---|
1128 | |
---|
1129 | const EdgeMap<T>& operator=(const EdgeMap<T> &) { return *this; } |
---|
1130 | |
---|
1131 | template<typename TT> |
---|
1132 | const EdgeMap<T>& operator=(const EdgeMap<TT> &m) { return *this; } |
---|
1133 | |
---|
1134 | void update() {} |
---|
1135 | void update(T a) {} |
---|
1136 | }; |
---|
1137 | }; |
---|
1138 | |
---|
1139 | |
---|
1140 | |
---|
1141 | ///Graph structure using a node set of another graph. |
---|
1142 | |
---|
1143 | ///This structure can be used to establish another graph over a node set |
---|
1144 | /// of an existing one. The node iterator will go through the nodes of the |
---|
1145 | /// original graph, and the NodeMap's of both graphs will convert to |
---|
1146 | /// each other. |
---|
1147 | /// |
---|
1148 | ///\warning Adding or deleting nodes from the graph is not safe if an |
---|
1149 | ///\ref EdgeSet is currently attached to it! |
---|
1150 | /// |
---|
1151 | ///\todo Make it possible to add/delete edges from the base graph |
---|
1152 | ///(and from \ref EdgeSet, as well) |
---|
1153 | /// |
---|
1154 | ///\param GG The type of the graph which shares its node set with this class. |
---|
1155 | ///Its interface must conform with \ref GraphSkeleton. |
---|
1156 | /// |
---|
1157 | ///It conforms to the graph interface documented under |
---|
1158 | ///the description of \ref GraphSkeleton. |
---|
1159 | ///\sa \ref GraphSkeleton. |
---|
1160 | ///\sa \ref NodeSet. |
---|
1161 | template<typename GG> |
---|
1162 | class EdgeSet { |
---|
1163 | |
---|
1164 | typedef GG NodeGraphType; |
---|
1165 | |
---|
1166 | NodeGraphType &G; |
---|
1167 | |
---|
1168 | public: |
---|
1169 | class Node; |
---|
1170 | class Edge; |
---|
1171 | class OutEdgeIt; |
---|
1172 | class InEdgeIt; |
---|
1173 | class SymEdge; |
---|
1174 | int id(Node v) const; |
---|
1175 | |
---|
1176 | class Node : public NodeGraphType::Node { |
---|
1177 | friend class EdgeSet; |
---|
1178 | // template <typename T> friend class NodeMap; |
---|
1179 | |
---|
1180 | friend class Edge; |
---|
1181 | friend class OutEdgeIt; |
---|
1182 | friend class InEdgeIt; |
---|
1183 | friend class SymEdge; |
---|
1184 | |
---|
1185 | public: |
---|
1186 | friend int EdgeSet::id(Node v) const; |
---|
1187 | // Node(int nn) {n=nn;} |
---|
1188 | public: |
---|
1189 | Node() : NodeGraphType::Node() {} |
---|
1190 | Node (Invalid i) : NodeGraphType::Node(i) {} |
---|
1191 | Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {} |
---|
1192 | }; |
---|
1193 | |
---|
1194 | class NodeIt : public NodeGraphType::NodeIt { |
---|
1195 | friend class EdgeSet; |
---|
1196 | public: |
---|
1197 | NodeIt() : NodeGraphType::NodeIt() { } |
---|
1198 | NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { } |
---|
1199 | NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {} |
---|
1200 | NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { } |
---|
1201 | NodeIt(const typename NodeGraphType::NodeIt &n) |
---|
1202 | : NodeGraphType::NodeIt(n) {} |
---|
1203 | |
---|
1204 | operator Node() { return Node(*this);} |
---|
1205 | NodeIt &operator++() |
---|
1206 | { this->NodeGraphType::NodeIt::operator++(); return *this;} |
---|
1207 | }; |
---|
1208 | |
---|
1209 | private: |
---|
1210 | //Edges are double linked. |
---|
1211 | //The free edges are only single linked using the "next_in" field. |
---|
1212 | struct NodeT |
---|
1213 | { |
---|
1214 | int first_in,first_out; |
---|
1215 | NodeT() : first_in(-1), first_out(-1) { } |
---|
1216 | }; |
---|
1217 | |
---|
1218 | struct EdgeT |
---|
1219 | { |
---|
1220 | Node head, tail; |
---|
1221 | int prev_in, prev_out; |
---|
1222 | int next_in, next_out; |
---|
1223 | }; |
---|
1224 | |
---|
1225 | |
---|
1226 | typename NodeGraphType::template NodeMap<NodeT> nodes; |
---|
1227 | |
---|
1228 | std::vector<EdgeT> edges; |
---|
1229 | //The first free edge |
---|
1230 | int first_free_edge; |
---|
1231 | |
---|
1232 | protected: |
---|
1233 | |
---|
1234 | template <typename Key> class DynMapBase |
---|
1235 | { |
---|
1236 | protected: |
---|
1237 | const EdgeSet* G; |
---|
1238 | public: |
---|
1239 | virtual void add(const Key k) = 0; |
---|
1240 | virtual void erase(const Key k) = 0; |
---|
1241 | DynMapBase(const EdgeSet &_G) : G(&_G) {} |
---|
1242 | virtual ~DynMapBase() {} |
---|
1243 | friend class EdgeSet; |
---|
1244 | }; |
---|
1245 | |
---|
1246 | public: |
---|
1247 | //template <typename T> class NodeMap; |
---|
1248 | template <typename T> class EdgeMap; |
---|
1249 | |
---|
1250 | class Node; |
---|
1251 | class Edge; |
---|
1252 | |
---|
1253 | // protected: |
---|
1254 | // HELPME: |
---|
1255 | protected: |
---|
1256 | // mutable std::vector<DynMapBase<Node> * > dyn_node_maps; |
---|
1257 | ///\bug It must be public because of SymEdgeMap. |
---|
1258 | /// |
---|
1259 | mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps; |
---|
1260 | |
---|
1261 | public: |
---|
1262 | |
---|
1263 | class NodeIt; |
---|
1264 | class EdgeIt; |
---|
1265 | class OutEdgeIt; |
---|
1266 | class InEdgeIt; |
---|
1267 | |
---|
1268 | template <typename T> class NodeMap; |
---|
1269 | template <typename T> class EdgeMap; |
---|
1270 | |
---|
1271 | public: |
---|
1272 | |
---|
1273 | ///Constructor |
---|
1274 | |
---|
1275 | ///Construates a new graph based on the nodeset of an existing one. |
---|
1276 | ///\param _G the base graph. |
---|
1277 | ///\todo It looks like a copy constructor, but it isn't. |
---|
1278 | EdgeSet(NodeGraphType &_G) : G(_G), |
---|
1279 | nodes(_G), edges(), |
---|
1280 | first_free_edge(-1) { } |
---|
1281 | ///Copy constructor |
---|
1282 | |
---|
1283 | ///Makes a copy of an EdgeSet. |
---|
1284 | ///It will be based on the same graph. |
---|
1285 | EdgeSet(const EdgeSet &_g) : G(_g.G), nodes(_g.G), edges(_g.edges), |
---|
1286 | first_free_edge(_g.first_free_edge) { } |
---|
1287 | |
---|
1288 | ~EdgeSet() |
---|
1289 | { |
---|
1290 | // for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin(); |
---|
1291 | // i!=dyn_node_maps.end(); ++i) (**i).G=NULL; |
---|
1292 | for(typename std::vector<DynMapBase<Edge> * >::iterator |
---|
1293 | i=dyn_edge_maps.begin(); |
---|
1294 | i!=dyn_edge_maps.end(); ++i) (**i).G=NULL; |
---|
1295 | } |
---|
1296 | |
---|
1297 | int nodeNum() const { return G.nodeNum(); } //FIXME: What is this? |
---|
1298 | int edgeNum() const { return edges.size(); } //FIXME: What is this? |
---|
1299 | |
---|
1300 | ///\bug This function does something different than |
---|
1301 | ///its name would suggests... |
---|
1302 | int maxNodeId() const { return G.maxNodeId(); } //FIXME: What is this? |
---|
1303 | ///\bug This function does something different than |
---|
1304 | ///its name would suggests... |
---|
1305 | int maxEdgeId() const { return edges.size(); } //FIXME: What is this? |
---|
1306 | |
---|
1307 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
1308 | Node head(Edge e) const { return edges[e.n].head; } |
---|
1309 | |
---|
1310 | NodeIt& first(NodeIt& v) const { |
---|
1311 | v=NodeIt(*this); return v; } |
---|
1312 | EdgeIt& first(EdgeIt& e) const { |
---|
1313 | e=EdgeIt(*this); return e; } |
---|
1314 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
1315 | e=OutEdgeIt(*this,v); return e; } |
---|
1316 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
1317 | e=InEdgeIt(*this,v); return e; } |
---|
1318 | |
---|
1319 | int id(Edge e) const { return e.n; } |
---|
1320 | |
---|
1321 | /// Adds a new node to the graph. |
---|
1322 | Node addNode() { return G.addNode(); } |
---|
1323 | |
---|
1324 | Edge addEdge(Node u, Node v) { |
---|
1325 | int n; |
---|
1326 | |
---|
1327 | if(first_free_edge==-1) |
---|
1328 | { |
---|
1329 | n = edges.size(); |
---|
1330 | edges.push_back(EdgeT()); |
---|
1331 | } |
---|
1332 | else { |
---|
1333 | n = first_free_edge; |
---|
1334 | first_free_edge = edges[n].next_in; |
---|
1335 | } |
---|
1336 | |
---|
1337 | edges[n].tail = u; edges[n].head = v; |
---|
1338 | |
---|
1339 | edges[n].next_out = nodes[u].first_out; |
---|
1340 | if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n; |
---|
1341 | edges[n].next_in = nodes[v].first_in; |
---|
1342 | if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n; |
---|
1343 | edges[n].prev_in = edges[n].prev_out = -1; |
---|
1344 | |
---|
1345 | nodes[u].first_out = nodes[v].first_in = n; |
---|
1346 | |
---|
1347 | Edge e; e.n=n; |
---|
1348 | |
---|
1349 | //Update dynamic maps |
---|
1350 | for(typename std::vector<DynMapBase<Edge> * >::iterator |
---|
1351 | i=dyn_edge_maps.begin(); |
---|
1352 | i!=dyn_edge_maps.end(); ++i) (**i).add(e); |
---|
1353 | |
---|
1354 | return e; |
---|
1355 | } |
---|
1356 | |
---|
1357 | /// Finds an edge between two nodes. |
---|
1358 | |
---|
1359 | /// Finds an edge from node \c u to node \c v. |
---|
1360 | /// |
---|
1361 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
1362 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
1363 | /// the next edge from \c u to \c v after \c prev. |
---|
1364 | /// \return The found edge or INVALID if there is no such an edge. |
---|
1365 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
1366 | { |
---|
1367 | int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out; |
---|
1368 | while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out; |
---|
1369 | prev.n=e; |
---|
1370 | return prev; |
---|
1371 | } |
---|
1372 | |
---|
1373 | private: |
---|
1374 | void eraseEdge(int n) { |
---|
1375 | |
---|
1376 | if(edges[n].next_in!=-1) |
---|
1377 | edges[edges[n].next_in].prev_in = edges[n].prev_in; |
---|
1378 | if(edges[n].prev_in!=-1) |
---|
1379 | edges[edges[n].prev_in].next_in = edges[n].next_in; |
---|
1380 | else nodes[edges[n].head].first_in = edges[n].next_in; |
---|
1381 | |
---|
1382 | if(edges[n].next_out!=-1) |
---|
1383 | edges[edges[n].next_out].prev_out = edges[n].prev_out; |
---|
1384 | if(edges[n].prev_out!=-1) |
---|
1385 | edges[edges[n].prev_out].next_out = edges[n].next_out; |
---|
1386 | else nodes[edges[n].tail].first_out = edges[n].next_out; |
---|
1387 | |
---|
1388 | edges[n].next_in = first_free_edge; |
---|
1389 | first_free_edge = -1; |
---|
1390 | |
---|
1391 | //Update dynamic maps |
---|
1392 | Edge e; e.n=n; |
---|
1393 | for(typename std::vector<DynMapBase<Edge> * >::iterator |
---|
1394 | i=dyn_edge_maps.begin(); |
---|
1395 | i!=dyn_edge_maps.end(); ++i) (**i).erase(e); |
---|
1396 | } |
---|
1397 | |
---|
1398 | public: |
---|
1399 | |
---|
1400 | // void erase(Node nn) { |
---|
1401 | // int n=nn.n; |
---|
1402 | // int m; |
---|
1403 | // while((m=nodes[n].first_in)!=-1) eraseEdge(m); |
---|
1404 | // while((m=nodes[n].first_out)!=-1) eraseEdge(m); |
---|
1405 | // } |
---|
1406 | |
---|
1407 | void erase(Edge e) { eraseEdge(e.n); } |
---|
1408 | |
---|
1409 | ///Clear all edges. (Doesn't clear the nodes!) |
---|
1410 | void clear() { |
---|
1411 | edges.clear(); |
---|
1412 | first_free_edge=-1; |
---|
1413 | } |
---|
1414 | |
---|
1415 | |
---|
1416 | // //\bug Dynamic maps must be updated! |
---|
1417 | // // |
---|
1418 | // void clear() { |
---|
1419 | // nodes.clear();edges.clear(); |
---|
1420 | // first_node=first_free_node=first_free_edge=-1; |
---|
1421 | // } |
---|
1422 | |
---|
1423 | public: |
---|
1424 | template <typename T> class EdgeMap; |
---|
1425 | |
---|
1426 | /// |
---|
1427 | class Edge { |
---|
1428 | public: |
---|
1429 | friend class EdgeSet; |
---|
1430 | template <typename T> friend class EdgeMap; |
---|
1431 | |
---|
1432 | friend class Node; |
---|
1433 | friend class NodeIt; |
---|
1434 | public: |
---|
1435 | ///\bug It should be at least protected |
---|
1436 | /// |
---|
1437 | int n; |
---|
1438 | protected: |
---|
1439 | friend int EdgeSet::id(Edge e) const; |
---|
1440 | |
---|
1441 | Edge(int nn) {n=nn;} |
---|
1442 | public: |
---|
1443 | Edge() { } |
---|
1444 | Edge (Invalid) { n=-1; } |
---|
1445 | bool operator==(const Edge i) const {return n==i.n;} |
---|
1446 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
1447 | bool operator<(const Edge i) const {return n<i.n;} |
---|
1448 | ///\bug This is a workaround until somebody tells me how to |
---|
1449 | ///make class \c SymEdgeSet::SymEdgeMap friend of Edge |
---|
1450 | int &idref() {return n;} |
---|
1451 | const int &idref() const {return n;} |
---|
1452 | }; |
---|
1453 | |
---|
1454 | class EdgeIt : public Edge { |
---|
1455 | friend class EdgeSet; |
---|
1456 | template <typename T> friend class EdgeMap; |
---|
1457 | |
---|
1458 | const EdgeSet *G; |
---|
1459 | public: |
---|
1460 | EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) { |
---|
1461 | // typename NodeGraphType::Node m; |
---|
1462 | NodeIt m; |
---|
1463 | for(G->first(m); |
---|
1464 | m!=INVALID && G->nodes[m].first_in == -1; ++m); |
---|
1465 | ///\bug AJJAJ! This is a non sense!!!!!!! |
---|
1466 | this->n = m!=INVALID?-1:G->nodes[m].first_in; |
---|
1467 | } |
---|
1468 | EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } |
---|
1469 | EdgeIt (Invalid i) : Edge(i) { } |
---|
1470 | EdgeIt() : Edge() { } |
---|
1471 | ///. |
---|
1472 | |
---|
1473 | ///\bug UNIMPLEMENTED!!!!! |
---|
1474 | // |
---|
1475 | EdgeIt &operator++() { |
---|
1476 | return *this; |
---|
1477 | } |
---|
1478 | ///\bug This is a workaround until somebody tells me how to |
---|
1479 | ///make class \c SymEdgeSet::SymEdgeMap friend of Edge |
---|
1480 | int &idref() {return this->n;} |
---|
1481 | }; |
---|
1482 | |
---|
1483 | class OutEdgeIt : public Edge { |
---|
1484 | const EdgeSet *G; |
---|
1485 | friend class EdgeSet; |
---|
1486 | public: |
---|
1487 | OutEdgeIt() : Edge() { } |
---|
1488 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
1489 | OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } |
---|
1490 | |
---|
1491 | OutEdgeIt(const EdgeSet& _G,const Node v) : |
---|
1492 | Edge(_G.nodes[v].first_out), G(&_G) { } |
---|
1493 | OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; } |
---|
1494 | }; |
---|
1495 | |
---|
1496 | class InEdgeIt : public Edge { |
---|
1497 | const EdgeSet *G; |
---|
1498 | friend class EdgeSet; |
---|
1499 | public: |
---|
1500 | InEdgeIt() : Edge() { } |
---|
1501 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
1502 | InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } |
---|
1503 | InEdgeIt(const EdgeSet& _G,Node v) |
---|
1504 | : Edge(_G.nodes[v].first_in), G(&_G) { } |
---|
1505 | InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } |
---|
1506 | }; |
---|
1507 | |
---|
1508 | template <typename T> class NodeMap : |
---|
1509 | public NodeGraphType::template NodeMap<T> |
---|
1510 | { |
---|
1511 | //This is a must, the constructors need it. |
---|
1512 | typedef typename NodeGraphType::template NodeMap<T> ParentNodeMap; |
---|
1513 | public: |
---|
1514 | NodeMap(const EdgeSet &_G) : ParentNodeMap(_G.G) { } |
---|
1515 | NodeMap(const EdgeSet &_G,const T &t) : ParentNodeMap(_G.G,t) { } |
---|
1516 | //It is unnecessary |
---|
1517 | NodeMap(const typename NodeGraphType::template NodeMap<T> &m) : |
---|
1518 | ParentNodeMap(m) { } |
---|
1519 | |
---|
1520 | ///\todo It can copy between different types. |
---|
1521 | /// |
---|
1522 | template<typename TT> |
---|
1523 | NodeMap(const typename NodeGraphType::template NodeMap<TT> &m) |
---|
1524 | : ParentNodeMap(m) { } |
---|
1525 | }; |
---|
1526 | |
---|
1527 | /// |
---|
1528 | template <typename T> class EdgeMap : public DynMapBase<Edge> |
---|
1529 | { |
---|
1530 | protected: |
---|
1531 | public: |
---|
1532 | ///\bug It should be at least protected |
---|
1533 | /// |
---|
1534 | std::vector<T> container; |
---|
1535 | |
---|
1536 | public: |
---|
1537 | typedef T ValueType; |
---|
1538 | typedef Edge KeyType; |
---|
1539 | |
---|
1540 | EdgeMap(const EdgeSet &_G) : |
---|
1541 | DynMapBase<Edge>(_G), container(_G.maxEdgeId()) |
---|
1542 | { |
---|
1543 | //FIXME: What if there are empty Id's? |
---|
1544 | //FIXME: Can I use 'this' in a constructor? |
---|
1545 | this->G->dyn_edge_maps.push_back(this); |
---|
1546 | } |
---|
1547 | EdgeMap(const EdgeSet &_G,const T &t) : |
---|
1548 | DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t) |
---|
1549 | { |
---|
1550 | this->G->dyn_edge_maps.push_back(this); |
---|
1551 | } |
---|
1552 | EdgeMap(const EdgeMap<T> &m) : |
---|
1553 | DynMapBase<Edge>(*m.G), container(m.container) |
---|
1554 | { |
---|
1555 | this->G->dyn_edge_maps.push_back(this); |
---|
1556 | } |
---|
1557 | |
---|
1558 | ///\todo It can copy between different types. |
---|
1559 | /// |
---|
1560 | template<typename TT> EdgeMap(const EdgeMap<TT> &m) : |
---|
1561 | DynMapBase<Edge>(*m.G), container(m.container.size()) |
---|
1562 | { |
---|
1563 | this->G->dyn_edge_maps.push_back(this); |
---|
1564 | typename std::vector<TT>::const_iterator i; |
---|
1565 | for(typename std::vector<TT>::const_iterator i=m.container.begin(); |
---|
1566 | i!=m.container.end(); |
---|
1567 | i++) |
---|
1568 | container.push_back(*i); |
---|
1569 | } |
---|
1570 | ~EdgeMap() |
---|
1571 | { |
---|
1572 | if(this->G) { |
---|
1573 | typename std::vector<DynMapBase<Edge>* >::iterator i; |
---|
1574 | for(i=this->G->dyn_edge_maps.begin(); |
---|
1575 | i!=this->G->dyn_edge_maps.end() && *i!=this; ++i) ; |
---|
1576 | //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow... |
---|
1577 | //A better way to do that: (Is this really important?) |
---|
1578 | if(*i==this) { |
---|
1579 | *i=this->G->dyn_edge_maps.back(); |
---|
1580 | this->G->dyn_edge_maps.pop_back(); |
---|
1581 | } |
---|
1582 | } |
---|
1583 | } |
---|
1584 | |
---|
1585 | void add(const Edge k) |
---|
1586 | { |
---|
1587 | if(k.n>=int(container.size())) container.resize(k.n+1); |
---|
1588 | } |
---|
1589 | void erase(const Edge) { } |
---|
1590 | |
---|
1591 | ///\bug This doesn't work. Why? |
---|
1592 | /// void set(Edge n, T a) { container[n.n]=a; } |
---|
1593 | void set(Edge n, T a) { container[this->G->id(n)]=a; } |
---|
1594 | //T get(Edge n) const { return container[n.n]; } |
---|
1595 | typename std::vector<T>::reference |
---|
1596 | ///\bug This doesn't work. Why? |
---|
1597 | /// operator[](Edge n) { return container[n.n]; } |
---|
1598 | operator[](Edge n) { return container[this->G->id(n)]; } |
---|
1599 | typename std::vector<T>::const_reference |
---|
1600 | ///\bug This doesn't work. Why? |
---|
1601 | /// operator[](Edge n) const { return container[n.n]; } |
---|
1602 | operator[](Edge n) const { return container[this->G->id(n)]; } |
---|
1603 | |
---|
1604 | ///\warning There is no safety check at all! |
---|
1605 | ///Using operator = between maps attached to different graph may |
---|
1606 | ///cause serious problem. |
---|
1607 | ///\todo Is this really so? |
---|
1608 | ///\todo It can copy between different types. |
---|
1609 | const EdgeMap<T>& operator=(const EdgeMap<T> &m) |
---|
1610 | { |
---|
1611 | container = m.container; |
---|
1612 | return *this; |
---|
1613 | } |
---|
1614 | |
---|
1615 | template<typename TT> friend class EdgeMap; |
---|
1616 | |
---|
1617 | template<typename TT> |
---|
1618 | const EdgeMap<T>& operator=(const EdgeMap<TT> &m) |
---|
1619 | { |
---|
1620 | std::copy(m.container.begin(), m.container.end(), container.begin()); |
---|
1621 | return *this; |
---|
1622 | } |
---|
1623 | |
---|
1624 | void update() {} //Useless for DynMaps |
---|
1625 | void update(T a) {} //Useless for DynMaps |
---|
1626 | }; |
---|
1627 | |
---|
1628 | }; |
---|
1629 | |
---|
1630 | template<typename GG> |
---|
1631 | inline int EdgeSet<GG>::id(Node v) const { return G.id(v); } |
---|
1632 | |
---|
1633 | /// @} |
---|
1634 | |
---|
1635 | } //namespace hugo |
---|
1636 | |
---|
1637 | #endif //HUGO_LIST_GRAPH_H |
---|