deba@262
|
1 |
// -*- c++ -*-
|
deba@262
|
2 |
#ifndef HUGO_LIST_GRAPH_H
|
deba@262
|
3 |
#define HUGO_LIST_GRAPH_H
|
deba@262
|
4 |
|
deba@262
|
5 |
#include <iostream>
|
deba@262
|
6 |
#include <vector>
|
deba@262
|
7 |
|
deba@377
|
8 |
#include "invalid.h"
|
deba@262
|
9 |
|
deba@377
|
10 |
#include "vector_map.h"
|
deba@340
|
11 |
|
deba@262
|
12 |
namespace hugo {
|
deba@262
|
13 |
|
deba@262
|
14 |
template <typename It>
|
deba@262
|
15 |
int count(It it) {
|
deba@262
|
16 |
int i=0;
|
deba@262
|
17 |
for( ; it.valid(); ++it) { ++i; }
|
deba@262
|
18 |
return i;
|
deba@262
|
19 |
}
|
deba@262
|
20 |
|
deba@262
|
21 |
class ListGraph {
|
deba@262
|
22 |
class node_item;
|
deba@262
|
23 |
class edge_item;
|
deba@262
|
24 |
public:
|
deba@262
|
25 |
class Node;
|
deba@262
|
26 |
class NodeIt;
|
deba@262
|
27 |
class Edge;
|
deba@262
|
28 |
class EdgeIt;
|
deba@262
|
29 |
class OutEdgeIt;
|
deba@262
|
30 |
class InEdgeIt;
|
deba@262
|
31 |
class SymEdgeIt;
|
deba@262
|
32 |
|
deba@340
|
33 |
// template <typename T> class NodeMap;
|
deba@340
|
34 |
// template <typename T> class EdgeMap;
|
deba@262
|
35 |
private:
|
deba@340
|
36 |
// template <typename T> friend class NodeMap;
|
deba@340
|
37 |
// template <typename T> friend class EdgeMap;
|
deba@262
|
38 |
|
deba@377
|
39 |
private:
|
deba@377
|
40 |
|
deba@378
|
41 |
typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
|
deba@378
|
42 |
NodeMapRegistry node_maps;
|
deba@378
|
43 |
|
deba@378
|
44 |
typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
|
deba@378
|
45 |
EdgeMapRegistry edge_maps;
|
deba@377
|
46 |
|
deba@377
|
47 |
public:
|
deba@377
|
48 |
|
deba@262
|
49 |
|
deba@262
|
50 |
template <typename T>
|
deba@378
|
51 |
class NodeMap : public VectorMap<ListGraph, Node, NodeIt, T> {
|
deba@377
|
52 |
public:
|
deba@378
|
53 |
NodeMap(ListGraph& g) : VectorMap<ListGraph, Node, NodeIt, T>(g.node_maps) {}
|
deba@377
|
54 |
};
|
deba@340
|
55 |
|
deba@340
|
56 |
template <typename T>
|
deba@378
|
57 |
class EdgeMap : public VectorMap<ListGraph, Edge, EdgeIt, T> {
|
deba@378
|
58 |
public:
|
deba@378
|
59 |
EdgeMap(ListGraph& g) : VectorMap<ListGraph, Edge, EdgeIt, T>(g.edge_maps) {}
|
deba@378
|
60 |
};
|
deba@340
|
61 |
|
deba@262
|
62 |
|
deba@262
|
63 |
int node_id;
|
deba@262
|
64 |
int edge_id;
|
deba@262
|
65 |
int _node_num;
|
deba@262
|
66 |
int _edge_num;
|
deba@262
|
67 |
|
deba@262
|
68 |
node_item* _first_node;
|
deba@262
|
69 |
node_item* _last_node;
|
deba@262
|
70 |
|
deba@262
|
71 |
class node_item {
|
deba@262
|
72 |
friend class ListGraph;
|
deba@262
|
73 |
template <typename T> friend class NodeMap;
|
deba@262
|
74 |
|
deba@262
|
75 |
friend class Node;
|
deba@262
|
76 |
friend class NodeIt;
|
deba@262
|
77 |
friend class Edge;
|
deba@262
|
78 |
friend class EdgeIt;
|
deba@262
|
79 |
friend class OutEdgeIt;
|
deba@262
|
80 |
friend class InEdgeIt;
|
deba@262
|
81 |
friend class SymEdgeIt;
|
deba@262
|
82 |
friend std::ostream& operator<<(std::ostream& os, const Node& i);
|
deba@262
|
83 |
friend std::ostream& operator<<(std::ostream& os, const Edge& i);
|
deba@262
|
84 |
//ListGraph* G;
|
deba@262
|
85 |
int id;
|
deba@262
|
86 |
edge_item* _first_out_edge;
|
deba@262
|
87 |
edge_item* _last_out_edge;
|
deba@262
|
88 |
edge_item* _first_in_edge;
|
deba@262
|
89 |
edge_item* _last_in_edge;
|
deba@262
|
90 |
node_item* _next_node;
|
deba@262
|
91 |
node_item* _prev_node;
|
deba@262
|
92 |
public:
|
deba@262
|
93 |
node_item() { }
|
deba@262
|
94 |
};
|
deba@262
|
95 |
|
deba@262
|
96 |
class edge_item {
|
deba@262
|
97 |
friend class ListGraph;
|
deba@262
|
98 |
template <typename T> friend class EdgeMap;
|
deba@262
|
99 |
|
deba@262
|
100 |
friend class Node;
|
deba@262
|
101 |
friend class NodeIt;
|
deba@262
|
102 |
friend class Edge;
|
deba@262
|
103 |
friend class EdgeIt;
|
deba@262
|
104 |
friend class OutEdgeIt;
|
deba@262
|
105 |
friend class InEdgeIt;
|
deba@262
|
106 |
friend class SymEdgeIt;
|
deba@262
|
107 |
friend std::ostream& operator<<(std::ostream& os, const Edge& i);
|
deba@262
|
108 |
//ListGraph* G;
|
deba@262
|
109 |
int id;
|
deba@262
|
110 |
node_item* _tail;
|
deba@262
|
111 |
node_item* _head;
|
deba@262
|
112 |
edge_item* _next_out;
|
deba@262
|
113 |
edge_item* _prev_out;
|
deba@262
|
114 |
edge_item* _next_in;
|
deba@262
|
115 |
edge_item* _prev_in;
|
deba@262
|
116 |
public:
|
deba@262
|
117 |
edge_item() { }
|
deba@262
|
118 |
};
|
deba@262
|
119 |
|
deba@262
|
120 |
node_item* _add_node() {
|
deba@262
|
121 |
node_item* p=new node_item;
|
deba@262
|
122 |
p->id=node_id++;
|
deba@262
|
123 |
p->_first_out_edge=0;
|
deba@262
|
124 |
p->_last_out_edge=0;
|
deba@262
|
125 |
p->_first_in_edge=0;
|
deba@262
|
126 |
p->_last_in_edge=0;
|
deba@262
|
127 |
p->_prev_node=_last_node;
|
deba@262
|
128 |
p->_next_node=0;
|
deba@262
|
129 |
if (_last_node) _last_node->_next_node=p;
|
deba@262
|
130 |
_last_node=p;
|
deba@262
|
131 |
if (!_first_node) _first_node=p;
|
deba@262
|
132 |
|
deba@262
|
133 |
++_node_num;
|
deba@262
|
134 |
return p;
|
deba@262
|
135 |
}
|
deba@262
|
136 |
|
deba@262
|
137 |
edge_item* _add_edge(node_item* _tail, node_item* _head) {
|
deba@262
|
138 |
edge_item* e=new edge_item;
|
deba@262
|
139 |
e->id=edge_id++;
|
deba@262
|
140 |
e->_tail=_tail;
|
deba@262
|
141 |
e->_head=_head;
|
deba@262
|
142 |
|
deba@262
|
143 |
e->_prev_out=_tail->_last_out_edge;
|
deba@262
|
144 |
if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
|
deba@262
|
145 |
_tail->_last_out_edge=e;
|
deba@262
|
146 |
if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
|
deba@262
|
147 |
e->_next_out=0;
|
deba@262
|
148 |
|
deba@262
|
149 |
e->_prev_in=_head->_last_in_edge;
|
deba@262
|
150 |
if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
|
deba@262
|
151 |
_head->_last_in_edge=e;
|
deba@262
|
152 |
if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
|
deba@262
|
153 |
e->_next_in=0;
|
deba@262
|
154 |
|
deba@262
|
155 |
++_edge_num;
|
deba@262
|
156 |
return e;
|
deba@262
|
157 |
}
|
deba@262
|
158 |
|
deba@262
|
159 |
//deletes a node which has no out edge and no in edge
|
deba@262
|
160 |
void _delete_node(node_item* v) {
|
deba@262
|
161 |
if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
|
deba@262
|
162 |
_last_node=v->_prev_node;
|
deba@262
|
163 |
if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
|
deba@262
|
164 |
_first_node=v->_next_node;
|
deba@262
|
165 |
|
deba@262
|
166 |
delete v;
|
deba@262
|
167 |
--_node_num;
|
deba@262
|
168 |
}
|
deba@262
|
169 |
|
deba@262
|
170 |
void _delete_edge(edge_item* e) {
|
deba@262
|
171 |
if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
|
deba@262
|
172 |
(e->_tail)->_last_out_edge=e->_prev_out;
|
deba@262
|
173 |
if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
|
deba@262
|
174 |
(e->_tail)->_first_out_edge=e->_next_out;
|
deba@262
|
175 |
if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
|
deba@262
|
176 |
(e->_head)->_last_in_edge=e->_prev_in;
|
deba@262
|
177 |
if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
|
deba@262
|
178 |
(e->_head)->_first_in_edge=e->_next_in;
|
deba@262
|
179 |
|
deba@262
|
180 |
delete e;
|
deba@262
|
181 |
--_edge_num;
|
deba@262
|
182 |
}
|
deba@262
|
183 |
|
deba@262
|
184 |
void _set_tail(edge_item* e, node_item* _tail) {
|
deba@262
|
185 |
if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
|
deba@262
|
186 |
(e->_tail)->_last_out_edge=e->_prev_out;
|
deba@262
|
187 |
if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
|
deba@262
|
188 |
(e->_tail)->_first_out_edge=e->_next_out;
|
deba@262
|
189 |
|
deba@262
|
190 |
e->_tail=_tail;
|
deba@262
|
191 |
|
deba@262
|
192 |
e->_prev_out=_tail->_last_out_edge;
|
deba@262
|
193 |
if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
|
deba@262
|
194 |
_tail->_last_out_edge=e;
|
deba@262
|
195 |
if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
|
deba@262
|
196 |
e->_next_out=0;
|
deba@262
|
197 |
}
|
deba@262
|
198 |
|
deba@262
|
199 |
void _set_head(edge_item* e, node_item* _head) {
|
deba@262
|
200 |
if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
|
deba@262
|
201 |
(e->_head)->_last_in_edge=e->_prev_in;
|
deba@262
|
202 |
if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
|
deba@262
|
203 |
(e->_head)->_first_in_edge=e->_next_in;
|
deba@262
|
204 |
|
deba@262
|
205 |
e->_head=_head;
|
deba@262
|
206 |
|
deba@262
|
207 |
e->_prev_in=_head->_last_in_edge;
|
deba@262
|
208 |
if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
|
deba@262
|
209 |
_head->_last_in_edge=e;
|
deba@262
|
210 |
if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
|
deba@262
|
211 |
e->_next_in=0;
|
deba@262
|
212 |
}
|
deba@262
|
213 |
|
deba@262
|
214 |
public:
|
deba@262
|
215 |
|
deba@262
|
216 |
/* default constructor */
|
deba@262
|
217 |
|
deba@378
|
218 |
ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0),
|
deba@378
|
219 |
edge_maps(*this), node_maps(*this) { }
|
deba@262
|
220 |
|
deba@262
|
221 |
~ListGraph() {
|
deba@262
|
222 |
while (first<NodeIt>().valid()) erase(first<NodeIt>());
|
deba@262
|
223 |
}
|
deba@262
|
224 |
|
deba@262
|
225 |
int nodeNum() const { return _node_num; }
|
deba@262
|
226 |
int edgeNum() const { return _edge_num; }
|
deba@262
|
227 |
|
deba@262
|
228 |
/* functions to construct iterators from the graph, or from each other */
|
deba@262
|
229 |
|
deba@262
|
230 |
//NodeIt firstNode() const { return NodeIt(*this); }
|
deba@262
|
231 |
//EdgeIt firstEdge() const { return EdgeIt(*this); }
|
deba@262
|
232 |
|
deba@262
|
233 |
//OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
|
deba@262
|
234 |
//InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
|
deba@262
|
235 |
//SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
|
deba@262
|
236 |
Node tail(Edge e) const { return e.tailNode(); }
|
deba@262
|
237 |
Node head(Edge e) const { return e.headNode(); }
|
deba@262
|
238 |
|
deba@262
|
239 |
Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
|
deba@262
|
240 |
Node aNode(const InEdgeIt& e) const { return e.aNode(); }
|
deba@262
|
241 |
Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
|
deba@262
|
242 |
|
deba@262
|
243 |
Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
|
deba@262
|
244 |
Node bNode(const InEdgeIt& e) const { return e.bNode(); }
|
deba@262
|
245 |
Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
|
deba@262
|
246 |
|
deba@262
|
247 |
//Node invalid_node() { return Node(); }
|
deba@262
|
248 |
//Edge invalid_edge() { return Edge(); }
|
deba@262
|
249 |
//OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
|
deba@262
|
250 |
//InEdgeIt invalid_in_edge() { return InEdgeIt(); }
|
deba@262
|
251 |
//SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
|
deba@262
|
252 |
|
deba@262
|
253 |
/* same methods in other style */
|
deba@262
|
254 |
/* for experimental purpose */
|
deba@262
|
255 |
|
deba@262
|
256 |
NodeIt& /*getF*/first(NodeIt& v) const {
|
deba@262
|
257 |
v=NodeIt(*this); return v; }
|
deba@262
|
258 |
EdgeIt& /*getF*/first(EdgeIt& e) const {
|
deba@262
|
259 |
e=EdgeIt(*this); return e; }
|
deba@262
|
260 |
OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
|
deba@262
|
261 |
e=OutEdgeIt(*this, v); return e; }
|
deba@262
|
262 |
InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
|
deba@262
|
263 |
e=InEdgeIt(*this, v); return e; }
|
deba@262
|
264 |
SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
|
deba@262
|
265 |
e=SymEdgeIt(*this, v); return e; }
|
deba@262
|
266 |
//void getTail(Node& n, const Edge& e) const { n=tail(e); }
|
deba@262
|
267 |
//void getHead(Node& n, const Edge& e) const { n=head(e); }
|
deba@262
|
268 |
|
deba@262
|
269 |
//void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
|
deba@262
|
270 |
//void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
|
deba@262
|
271 |
//void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
|
deba@262
|
272 |
//void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
|
deba@262
|
273 |
//void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
|
deba@262
|
274 |
//void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
|
deba@262
|
275 |
//void get_invalid(Node& n) { n=Node(); }
|
deba@262
|
276 |
//void get_invalid(Edge& e) { e=Edge(); }
|
deba@262
|
277 |
//void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
|
deba@262
|
278 |
//void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
|
deba@262
|
279 |
//void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
|
deba@262
|
280 |
|
deba@262
|
281 |
template< typename It >
|
deba@262
|
282 |
It first() const {
|
deba@262
|
283 |
It e;
|
deba@262
|
284 |
/*getF*/first(e);
|
deba@262
|
285 |
return e;
|
deba@262
|
286 |
}
|
deba@262
|
287 |
|
deba@262
|
288 |
template< typename It >
|
deba@262
|
289 |
It first(Node v) const {
|
deba@262
|
290 |
It e;
|
deba@262
|
291 |
/*getF*/first(e, v);
|
deba@262
|
292 |
return e;
|
deba@262
|
293 |
}
|
deba@262
|
294 |
|
deba@262
|
295 |
bool valid(Node n) const { return n.valid(); }
|
deba@262
|
296 |
bool valid(Edge e) const { return e.valid(); }
|
deba@262
|
297 |
|
deba@262
|
298 |
template <typename It> It getNext(It it) const {
|
deba@262
|
299 |
It tmp(it); return next(tmp); }
|
deba@262
|
300 |
template <typename It> It& next(It& it) const { return ++it; }
|
deba@262
|
301 |
|
deba@262
|
302 |
|
deba@262
|
303 |
/* for getting id's of graph objects */
|
deba@262
|
304 |
/* these are important for the implementation of property vectors */
|
deba@262
|
305 |
|
deba@262
|
306 |
int id(Node v) const { return v.node->id; }
|
deba@262
|
307 |
int id(Edge e) const { return e.edge->id; }
|
deba@262
|
308 |
|
deba@262
|
309 |
/* adding nodes and edges */
|
deba@262
|
310 |
|
deba@340
|
311 |
Node addNode() {
|
deba@340
|
312 |
Node n = _add_node();
|
deba@340
|
313 |
node_maps.add(n);
|
deba@340
|
314 |
return n;
|
deba@340
|
315 |
}
|
deba@262
|
316 |
Edge addEdge(Node u, Node v) {
|
deba@340
|
317 |
Edge e = _add_edge(u.node, v.node);
|
deba@340
|
318 |
edge_maps.add(e);
|
deba@340
|
319 |
return e;
|
deba@262
|
320 |
}
|
deba@262
|
321 |
|
deba@262
|
322 |
void erase(Node i) {
|
deba@377
|
323 |
node_maps.erase(i);
|
deba@262
|
324 |
while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
|
deba@262
|
325 |
while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
|
deba@262
|
326 |
_delete_node(i.node);
|
deba@262
|
327 |
}
|
deba@262
|
328 |
|
deba@340
|
329 |
void erase(Edge e) {
|
deba@340
|
330 |
edge_maps.erase(e);
|
deba@340
|
331 |
_delete_edge(e.edge);
|
deba@340
|
332 |
}
|
deba@262
|
333 |
|
deba@262
|
334 |
void clear() {
|
deba@262
|
335 |
while (first<NodeIt>().valid()) erase(first<NodeIt>());
|
deba@262
|
336 |
}
|
deba@262
|
337 |
|
deba@262
|
338 |
void setTail(Edge e, Node tail) {
|
deba@262
|
339 |
_set_tail(e.edge, tail.node);
|
deba@262
|
340 |
}
|
deba@262
|
341 |
|
deba@262
|
342 |
void setHead(Edge e, Node head) {
|
deba@262
|
343 |
_set_head(e.edge, head.node);
|
deba@262
|
344 |
}
|
deba@262
|
345 |
|
deba@262
|
346 |
/* stream operations, for testing purpose */
|
deba@262
|
347 |
|
deba@262
|
348 |
friend std::ostream& operator<<(std::ostream& os, const Node& i) {
|
deba@262
|
349 |
os << i.node->id; return os;
|
deba@262
|
350 |
}
|
deba@262
|
351 |
friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
|
deba@262
|
352 |
os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
|
deba@262
|
353 |
return os;
|
deba@262
|
354 |
}
|
deba@262
|
355 |
|
deba@262
|
356 |
class Node {
|
deba@262
|
357 |
friend class ListGraph;
|
deba@262
|
358 |
template <typename T> friend class NodeMap;
|
deba@262
|
359 |
|
deba@262
|
360 |
friend class Edge;
|
deba@262
|
361 |
friend class OutEdgeIt;
|
deba@262
|
362 |
friend class InEdgeIt;
|
deba@262
|
363 |
friend class SymEdgeIt;
|
deba@262
|
364 |
//public: //FIXME: It is required by op= of NodeIt
|
deba@262
|
365 |
protected:
|
deba@262
|
366 |
node_item* node;
|
deba@262
|
367 |
protected:
|
deba@262
|
368 |
friend int ListGraph::id(Node v) const;
|
deba@262
|
369 |
public:
|
deba@262
|
370 |
Node() /*: node(0)*/ { }
|
deba@262
|
371 |
Node(const Invalid&) : node(0) { }
|
deba@262
|
372 |
protected:
|
deba@262
|
373 |
Node(node_item* _node) : node(_node) { }
|
deba@262
|
374 |
bool valid() const { return (node); }
|
deba@262
|
375 |
public:
|
deba@262
|
376 |
//void makeInvalid() { node=0; }
|
deba@262
|
377 |
friend bool operator==(Node u, Node v) { return v.node==u.node; }
|
deba@262
|
378 |
friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
|
deba@262
|
379 |
friend std::ostream& operator<<(std::ostream& os, const Node& i);
|
deba@262
|
380 |
};
|
deba@262
|
381 |
|
deba@262
|
382 |
class NodeIt : public Node {
|
deba@262
|
383 |
friend class ListGraph;
|
deba@262
|
384 |
//protected:
|
deba@262
|
385 |
public: //for everybody but marci
|
deba@262
|
386 |
NodeIt(const ListGraph& G) : Node(G._first_node) { }
|
deba@262
|
387 |
public:
|
deba@262
|
388 |
NodeIt() : Node() { }
|
deba@262
|
389 |
NodeIt(const Invalid& i) : Node(i) { }
|
deba@262
|
390 |
protected:
|
deba@262
|
391 |
NodeIt(node_item* v) : Node(v) { }
|
deba@262
|
392 |
NodeIt& operator++() { node=node->_next_node; return *this; }
|
deba@262
|
393 |
//FIXME::
|
deba@262
|
394 |
// NodeIt& operator=(const Node& e)
|
deba@262
|
395 |
// { node=e.node; return *this; }
|
deba@262
|
396 |
};
|
deba@262
|
397 |
|
deba@262
|
398 |
class Edge {
|
deba@262
|
399 |
friend class ListGraph;
|
deba@262
|
400 |
template <typename T> friend class EdgeMap;
|
deba@262
|
401 |
|
deba@262
|
402 |
friend class Node;
|
deba@262
|
403 |
friend class NodeIt;
|
deba@262
|
404 |
protected:
|
deba@262
|
405 |
edge_item* edge;
|
deba@262
|
406 |
friend int ListGraph::id(Edge e) const;
|
deba@262
|
407 |
public:
|
deba@262
|
408 |
Edge() /*: edge(0)*/ { }
|
deba@262
|
409 |
Edge(const Invalid&) : edge(0) { }
|
deba@262
|
410 |
//Edge() { }
|
deba@262
|
411 |
protected:
|
deba@262
|
412 |
Edge(edge_item* _edge) : edge(_edge) { }
|
deba@262
|
413 |
bool valid() const { return (edge); }
|
deba@262
|
414 |
public:
|
deba@262
|
415 |
//void makeInvalid() { edge=0; }
|
deba@262
|
416 |
friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
|
deba@262
|
417 |
friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
|
deba@262
|
418 |
protected:
|
deba@262
|
419 |
Node tailNode() const { return Node(edge->_tail); }
|
deba@262
|
420 |
Node headNode() const { return Node(edge->_head); }
|
deba@262
|
421 |
public:
|
deba@262
|
422 |
friend std::ostream& operator<<(std::ostream& os, const Edge& i);
|
deba@262
|
423 |
};
|
deba@262
|
424 |
|
deba@262
|
425 |
class EdgeIt : public Edge {
|
deba@262
|
426 |
friend class ListGraph;
|
deba@262
|
427 |
//protected:
|
deba@262
|
428 |
public: //for alpar
|
deba@262
|
429 |
EdgeIt(const ListGraph& G) {
|
deba@262
|
430 |
node_item* v=G._first_node;
|
deba@262
|
431 |
if (v) edge=v->_first_out_edge; else edge=0;
|
deba@262
|
432 |
while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
|
deba@262
|
433 |
}
|
deba@262
|
434 |
public:
|
deba@262
|
435 |
EdgeIt() : Edge() { }
|
deba@262
|
436 |
EdgeIt(const Invalid& i) : Edge(i) { }
|
deba@262
|
437 |
protected:
|
deba@262
|
438 |
EdgeIt(edge_item* _e) : Edge(_e) { }
|
deba@262
|
439 |
EdgeIt& operator++() {
|
deba@262
|
440 |
node_item* v=edge->_tail;
|
deba@262
|
441 |
edge=edge->_next_out;
|
deba@262
|
442 |
while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
|
deba@262
|
443 |
return *this;
|
deba@262
|
444 |
}
|
deba@262
|
445 |
};
|
deba@262
|
446 |
|
deba@262
|
447 |
class OutEdgeIt : public Edge {
|
deba@262
|
448 |
friend class ListGraph;
|
deba@262
|
449 |
//node_item* v;
|
deba@262
|
450 |
//protected:
|
deba@262
|
451 |
protected: //for alpar
|
deba@262
|
452 |
OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
|
deba@262
|
453 |
public:
|
deba@262
|
454 |
OutEdgeIt() : Edge()/*, v(0)*/ { }
|
deba@262
|
455 |
OutEdgeIt(const Invalid& i) : Edge(i) { }
|
deba@262
|
456 |
OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
|
deba@262
|
457 |
protected:
|
deba@262
|
458 |
OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
|
deba@262
|
459 |
protected:
|
deba@262
|
460 |
Node aNode() const { return Node(edge->_tail); }
|
deba@262
|
461 |
Node bNode() const { return Node(edge->_head); }
|
deba@262
|
462 |
};
|
deba@262
|
463 |
|
deba@262
|
464 |
class InEdgeIt : public Edge {
|
deba@262
|
465 |
friend class ListGraph;
|
deba@262
|
466 |
//node_item* v;
|
deba@262
|
467 |
//protected:
|
deba@262
|
468 |
protected: //for alpar
|
deba@262
|
469 |
InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
|
deba@262
|
470 |
public:
|
deba@262
|
471 |
InEdgeIt() : Edge()/*, v(0)*/ { }
|
deba@262
|
472 |
InEdgeIt(const Invalid& i) : Edge(i) { }
|
deba@262
|
473 |
InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
|
deba@262
|
474 |
protected:
|
deba@262
|
475 |
InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
|
deba@262
|
476 |
protected:
|
deba@262
|
477 |
Node aNode() const { return Node(edge->_head); }
|
deba@262
|
478 |
Node bNode() const { return Node(edge->_tail); }
|
deba@262
|
479 |
};
|
deba@262
|
480 |
|
deba@262
|
481 |
class SymEdgeIt : public Edge {
|
deba@262
|
482 |
friend class ListGraph;
|
deba@262
|
483 |
bool out_or_in; //1 iff out, 0 iff in
|
deba@262
|
484 |
//node_item* v;
|
deba@262
|
485 |
//protected:
|
deba@262
|
486 |
public: //for alpar
|
deba@262
|
487 |
SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
|
deba@262
|
488 |
out_or_in=1;
|
deba@262
|
489 |
edge=_v.node->_first_out_edge;
|
deba@262
|
490 |
if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
|
deba@262
|
491 |
}
|
deba@262
|
492 |
public:
|
deba@262
|
493 |
SymEdgeIt() : Edge() /*, v(0)*/ { }
|
deba@262
|
494 |
SymEdgeIt(const Invalid& i) : Edge(i) { }
|
deba@262
|
495 |
SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
|
deba@262
|
496 |
out_or_in=1;
|
deba@262
|
497 |
edge=_v.node->_first_out_edge;
|
deba@262
|
498 |
if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
|
deba@262
|
499 |
}
|
deba@262
|
500 |
protected:
|
deba@262
|
501 |
SymEdgeIt& operator++() {
|
deba@262
|
502 |
if (out_or_in) {
|
deba@262
|
503 |
node_item* v=edge->_tail;
|
deba@262
|
504 |
edge=edge->_next_out;
|
deba@262
|
505 |
if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
|
deba@262
|
506 |
} else {
|
deba@262
|
507 |
edge=edge->_next_in;
|
deba@262
|
508 |
}
|
deba@262
|
509 |
return *this;
|
deba@262
|
510 |
}
|
deba@262
|
511 |
protected:
|
deba@262
|
512 |
Node aNode() const {
|
deba@262
|
513 |
return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
|
deba@262
|
514 |
Node bNode() const {
|
deba@262
|
515 |
return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
|
deba@262
|
516 |
};
|
deba@262
|
517 |
|
deba@262
|
518 |
};
|
deba@262
|
519 |
|
deba@262
|
520 |
// template< typename T >
|
deba@262
|
521 |
// T ListGraph::first() const {
|
deba@262
|
522 |
// std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
|
deba@262
|
523 |
// return T();
|
deba@262
|
524 |
// }
|
deba@262
|
525 |
|
deba@262
|
526 |
// template<>
|
deba@262
|
527 |
// ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
|
deba@262
|
528 |
// return firstNode();
|
deba@262
|
529 |
// }
|
deba@262
|
530 |
|
deba@262
|
531 |
// template<>
|
deba@262
|
532 |
// ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
|
deba@262
|
533 |
// return firstEdge();
|
deba@262
|
534 |
// }
|
deba@262
|
535 |
|
deba@262
|
536 |
// template< typename T >
|
deba@262
|
537 |
// T ListGraph::first(ListGraph::Node v) const {
|
deba@262
|
538 |
// std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
|
deba@262
|
539 |
// return T();
|
deba@262
|
540 |
// }
|
deba@262
|
541 |
|
deba@262
|
542 |
// template<>
|
deba@262
|
543 |
// ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
|
deba@262
|
544 |
// return firstOutEdge(v);
|
deba@262
|
545 |
// }
|
deba@262
|
546 |
|
deba@262
|
547 |
// template<>
|
deba@262
|
548 |
// ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
|
deba@262
|
549 |
// return firstInEdge(v);
|
deba@262
|
550 |
// }
|
deba@262
|
551 |
|
deba@262
|
552 |
// template<>
|
deba@262
|
553 |
// ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
|
deba@262
|
554 |
// return firstSymEdge(v);
|
deba@262
|
555 |
// }
|
deba@262
|
556 |
|
deba@262
|
557 |
|
deba@262
|
558 |
} //namespace hugo
|
deba@262
|
559 |
|
deba@262
|
560 |
#endif //HUGO_LIST_GRAPH_H
|