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