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