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