1 | // -*- c++ -*- |
---|
2 | #ifndef HUGO_BIPARTITE_GRAPH_WRAPPER_H |
---|
3 | #define HUGO_BIPARTITE_GRAPH_WRAPPER_H |
---|
4 | |
---|
5 | ///\ingroup gwrappers |
---|
6 | ///\file |
---|
7 | ///\brief Several graph wrappers. |
---|
8 | /// |
---|
9 | ///This file contains several useful graph wrapper functions. |
---|
10 | /// |
---|
11 | ///\author Marton Makai |
---|
12 | |
---|
13 | #include <invalid.h> |
---|
14 | #include <iter_map.h> |
---|
15 | #include <graph_wrapper.h> |
---|
16 | |
---|
17 | namespace hugo { |
---|
18 | |
---|
19 | /// A wrapper for composing a bipartite graph. |
---|
20 | /// \c _graph have to be a reference to a graph of type \c Graph |
---|
21 | /// and \c _s_false_t_true_map is an \c IterableBoolMap |
---|
22 | /// reference containing the elements for the |
---|
23 | /// color classes S and T. \c _graph is to be referred to an undirected |
---|
24 | /// graph or a directed graph with edges oriented from S to T. |
---|
25 | /// |
---|
26 | ///\author Marton Makai |
---|
27 | template<typename Graph> |
---|
28 | class BipartiteGraphWrapper : public GraphWrapper<Graph> { |
---|
29 | protected: |
---|
30 | typedef IterableBoolMap< typename Graph::template NodeMap<int> > |
---|
31 | SFalseTTrueMap; |
---|
32 | SFalseTTrueMap* s_false_t_true_map; |
---|
33 | |
---|
34 | BipartiteGraphWrapper() : GraphWrapper<Graph>(0) { } |
---|
35 | void setSFalseTTrueMap(SFalseTTrueMap& _s_false_t_true_map) { |
---|
36 | s_false_t_true_map=_s_false_t_true_map; |
---|
37 | } |
---|
38 | |
---|
39 | public: |
---|
40 | //marci |
---|
41 | //FIXME vhogy igy kellene, csak az en forditom nem eszi meg |
---|
42 | //static const bool S_CLASS=false; |
---|
43 | //static const bool T_CLASS=true; |
---|
44 | |
---|
45 | bool S_CLASS; |
---|
46 | bool T_CLASS; |
---|
47 | |
---|
48 | BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map) |
---|
49 | : GraphWrapper<Graph>(_graph), s_false_t_true_map(&_s_false_t_true_map), |
---|
50 | S_CLASS(false), T_CLASS(true) { } |
---|
51 | typedef typename GraphWrapper<Graph>::Node Node; |
---|
52 | //using GraphWrapper<Graph>::NodeIt; |
---|
53 | typedef typename GraphWrapper<Graph>::Edge Edge; |
---|
54 | //using GraphWrapper<Graph>::EdgeIt; |
---|
55 | class ClassNodeIt; |
---|
56 | friend class ClassNodeIt; |
---|
57 | class OutEdgeIt; |
---|
58 | friend class OutEdgeIt; |
---|
59 | class InEdgeIt; |
---|
60 | friend class InEdgeIt; |
---|
61 | class ClassNodeIt { |
---|
62 | friend class BipartiteGraphWrapper<Graph>; |
---|
63 | protected: |
---|
64 | Node n; |
---|
65 | public: |
---|
66 | ClassNodeIt() { } |
---|
67 | ClassNodeIt(const Invalid& i) : n(i) { } |
---|
68 | ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) { |
---|
69 | _G.s_false_t_true_map->first(n, _class); |
---|
70 | } |
---|
71 | //FIXME needed in new concept, important here |
---|
72 | ClassNodeIt(const Node& _n) : n(_n) { } |
---|
73 | operator Node() const { return n; } |
---|
74 | }; |
---|
75 | // class SNodeIt { |
---|
76 | // Node n; |
---|
77 | // public: |
---|
78 | // SNodeIt() { } |
---|
79 | // SNodeIt(const Invalid& i) : n(i) { } |
---|
80 | // SNodeIt(const BipartiteGraphWrapper<Graph>& _G) { |
---|
81 | // _G.s_false_t_true_map->first(n, false); |
---|
82 | // } |
---|
83 | // operator Node() const { return n; } |
---|
84 | // }; |
---|
85 | // class TNodeIt { |
---|
86 | // Node n; |
---|
87 | // public: |
---|
88 | // TNodeIt() { } |
---|
89 | // TNodeIt(const Invalid& i) : n(i) { } |
---|
90 | // TNodeIt(const BipartiteGraphWrapper<Graph>& _G) { |
---|
91 | // _G.s_false_t_true_map->first(n, true); |
---|
92 | // } |
---|
93 | // operator Node() const { return n; } |
---|
94 | // }; |
---|
95 | class OutEdgeIt { |
---|
96 | friend class BipartiteGraphWrapper<Graph>; |
---|
97 | protected: |
---|
98 | typename Graph::OutEdgeIt e; |
---|
99 | public: |
---|
100 | OutEdgeIt() { } |
---|
101 | OutEdgeIt(const Invalid& i) : e(i) { } |
---|
102 | OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) { |
---|
103 | if (!(*(_G.s_false_t_true_map))[_n]) |
---|
104 | e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n)); |
---|
105 | else |
---|
106 | e=INVALID; |
---|
107 | } |
---|
108 | operator Edge() const { return Edge(typename Graph::Edge(e)); } |
---|
109 | }; |
---|
110 | class InEdgeIt { |
---|
111 | friend class BipartiteGraphWrapper<Graph>; |
---|
112 | protected: |
---|
113 | typename Graph::InEdgeIt e; |
---|
114 | public: |
---|
115 | InEdgeIt() { } |
---|
116 | InEdgeIt(const Invalid& i) : e(i) { } |
---|
117 | InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) { |
---|
118 | if ((*(_G.s_false_t_true_map))[_n]) |
---|
119 | e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n)); |
---|
120 | else |
---|
121 | e=INVALID; |
---|
122 | } |
---|
123 | operator Edge() const { return Edge(typename Graph::Edge(e)); } |
---|
124 | }; |
---|
125 | |
---|
126 | using GraphWrapper<Graph>::first; |
---|
127 | ClassNodeIt& first(ClassNodeIt& n, bool _class) const { |
---|
128 | n=ClassNodeIt(*this, _class) ; return n; } |
---|
129 | // SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; } |
---|
130 | // TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; } |
---|
131 | OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { |
---|
132 | i=OutEdgeIt(*this, p); return i; |
---|
133 | } |
---|
134 | InEdgeIt& first(InEdgeIt& i, const Node& p) const { |
---|
135 | i=InEdgeIt(*this, p); return i; |
---|
136 | } |
---|
137 | |
---|
138 | using GraphWrapper<Graph>::next; |
---|
139 | ClassNodeIt& next(ClassNodeIt& n) const { |
---|
140 | this->s_false_t_true_map->next(n.n); return n; |
---|
141 | } |
---|
142 | // SNodeIt& next(SNodeIt& n) const { |
---|
143 | // this->s_false_t_true_map->next(n); return n; |
---|
144 | // } |
---|
145 | // TNodeIt& next(TNodeIt& n) const { |
---|
146 | // this->s_false_t_true_map->next(n); return n; |
---|
147 | // } |
---|
148 | OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; } |
---|
149 | InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; } |
---|
150 | |
---|
151 | Node tail(const Edge& e) { |
---|
152 | if (!(*(this->s_false_t_true_map))[this->graph->tail(e)]) |
---|
153 | return Node(this->graph->tail(e)); |
---|
154 | else |
---|
155 | return Node(this->graph->head(e)); |
---|
156 | } |
---|
157 | Node head(const Edge& e) { |
---|
158 | if (!(*(this->s_false_t_true_map))[this->graph->tail(e)]) |
---|
159 | return Node(this->graph->head(e)); |
---|
160 | else |
---|
161 | return Node(this->graph->tail(e)); |
---|
162 | } |
---|
163 | |
---|
164 | Node aNode(const OutEdgeIt& e) const { |
---|
165 | return Node(this->graph->aNode(e.e)); |
---|
166 | } |
---|
167 | Node aNode(const InEdgeIt& e) const { |
---|
168 | return Node(this->graph->aNode(e.e)); |
---|
169 | } |
---|
170 | Node bNode(const OutEdgeIt& e) const { |
---|
171 | return Node(this->graph->bNode(e.e)); |
---|
172 | } |
---|
173 | Node bNode(const InEdgeIt& e) const { |
---|
174 | return Node(this->graph->bNode(e.e)); |
---|
175 | } |
---|
176 | |
---|
177 | bool inSClass(const Node& n) const { |
---|
178 | return !(*(this->s_false_t_true_map))[n]; |
---|
179 | } |
---|
180 | bool inTClass(const Node& n) const { |
---|
181 | return (*(this->s_false_t_true_map))[n]; |
---|
182 | } |
---|
183 | }; |
---|
184 | |
---|
185 | ///\bug Do not use this while the bipartitemap augmentation |
---|
186 | /// does not work well. |
---|
187 | template<typename Graph> |
---|
188 | class BipartiteGraph : public BipartiteGraphWrapper<Graph> { |
---|
189 | typedef IterableBoolMap< typename Graph::template NodeMap<int> > |
---|
190 | SFalseTTrueMap; |
---|
191 | typedef BipartiteGraphWrapper<Graph> Parent; |
---|
192 | protected: |
---|
193 | Graph gr; |
---|
194 | typename Graph::template NodeMap<int> bipartite_map; |
---|
195 | SFalseTTrueMap s_false_t_true_map; |
---|
196 | public: |
---|
197 | typedef typename Parent::Node Node; |
---|
198 | typedef typename Parent::Edge Edge; |
---|
199 | BipartiteGraph() : BipartiteGraphWrapper<Graph>(0), |
---|
200 | gr(), bipartite_map(gr), |
---|
201 | s_false_t_true_map(bipartite_map) { |
---|
202 | Parent::setGraph(gr); |
---|
203 | Parent::setSFalseTTrueMap(bipartite_map); |
---|
204 | } |
---|
205 | |
---|
206 | /// the \c bool parameter which can be \c S_Class or \c T_Class shows |
---|
207 | /// the color class where the new node is to be inserted. |
---|
208 | void addNode(bool); |
---|
209 | |
---|
210 | /// A new edge is inserted. |
---|
211 | ///\pre \c tail have to be in \c S_Class and \c head in \c T_Class. |
---|
212 | void addEdge(const Node& tail, const Node& head); |
---|
213 | |
---|
214 | void erase(const Node&); |
---|
215 | void erase(const Edge&); |
---|
216 | |
---|
217 | void clear() { |
---|
218 | FOR_EACH_LOC(typename Parent::EdgeIt, e, G) erase(e); |
---|
219 | FOR_EACH_LOC(typename Parent::NodeIt, n, G) erase(n); |
---|
220 | } |
---|
221 | }; |
---|
222 | |
---|
223 | |
---|
224 | template<typename Graph> |
---|
225 | class stGraphWrapper; |
---|
226 | |
---|
227 | // template<typename Graph> |
---|
228 | // std::ostream& |
---|
229 | // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) { |
---|
230 | // os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")"; |
---|
231 | // return os; |
---|
232 | // } |
---|
233 | // template<typename Graph> |
---|
234 | // std::ostream& |
---|
235 | // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) { |
---|
236 | // os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec << |
---|
237 | // " node: " << i.n << ")"; |
---|
238 | // return os; |
---|
239 | // } |
---|
240 | |
---|
241 | /// experimentral, do not try it. |
---|
242 | /// It eats a bipartite graph, oriented from S to T. |
---|
243 | /// Such one can be made e.g. by the above wrapper. |
---|
244 | /// |
---|
245 | ///\author Marton Makai |
---|
246 | template<typename Graph> |
---|
247 | class stGraphWrapper : public GraphWrapper<Graph> { |
---|
248 | public: |
---|
249 | class Node; |
---|
250 | friend class Node; |
---|
251 | //GN, int |
---|
252 | //0 normalis, 1 s, 2, true, ez az iteralasi sorrend, |
---|
253 | //es a vege a false azaz (invalid, 3) |
---|
254 | class NodeIt; |
---|
255 | friend class NodeIt; |
---|
256 | //GNI, int |
---|
257 | class Edge; |
---|
258 | friend class Edge; |
---|
259 | //GE, int, GN |
---|
260 | //0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend, |
---|
261 | //invalid: (invalid, 3, invalid) |
---|
262 | class OutEdgeIt; |
---|
263 | friend class OutEdgeIt; |
---|
264 | //GO, int, GNI |
---|
265 | //normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid) |
---|
266 | //s-bol (invalid, 1, first), ... (invalid, 3, invalid) |
---|
267 | //t-bol (invalid, 3, invalid) |
---|
268 | class InEdgeIt; |
---|
269 | friend class InEdgeIt; |
---|
270 | //GI, int, GNI |
---|
271 | //normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid) |
---|
272 | //s-be (invalid, 3, invalid) |
---|
273 | //t-be (invalid, 2, first), ... (invalid, 3, invalid) |
---|
274 | class EdgeIt; |
---|
275 | friend class EdgeIt; |
---|
276 | //(first, 0, invalid) ... |
---|
277 | //(invalid, 1, vmi) |
---|
278 | //(invalid, 2, vmi) |
---|
279 | //invalid, 3, invalid) |
---|
280 | template <typename T> class NodeMap; |
---|
281 | template <typename T, typename Parent> class EdgeMap; |
---|
282 | |
---|
283 | // template <typename T> friend class NodeMap; |
---|
284 | // template <typename T> friend class EdgeMap; |
---|
285 | |
---|
286 | const Node S_NODE; |
---|
287 | const Node T_NODE; |
---|
288 | |
---|
289 | static const bool S_CLASS=false; |
---|
290 | static const bool T_CLASS=true; |
---|
291 | |
---|
292 | stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) , |
---|
293 | S_NODE(INVALID, 1), |
---|
294 | T_NODE(INVALID, 2) { } |
---|
295 | |
---|
296 | |
---|
297 | // std::ostream& |
---|
298 | // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i); |
---|
299 | // friend std::ostream& |
---|
300 | // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i); |
---|
301 | // friend std::ostream& |
---|
302 | // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i); |
---|
303 | |
---|
304 | class Node : public Graph::Node { |
---|
305 | protected: |
---|
306 | friend class GraphWrapper<Graph>; |
---|
307 | friend class stGraphWrapper<Graph>; |
---|
308 | template <typename T> friend class NodeMap; |
---|
309 | friend class Edge; |
---|
310 | friend class OutEdgeIt; |
---|
311 | friend class InEdgeIt; |
---|
312 | friend class EdgeIt; |
---|
313 | int spec; |
---|
314 | public: |
---|
315 | Node() { } |
---|
316 | Node(const typename Graph::Node& _n, int _spec=0) : |
---|
317 | Graph::Node(_n), spec(_spec) { } |
---|
318 | Node(const Invalid& i) : Graph::Node(i), spec(3) { } |
---|
319 | friend bool operator==(const Node& u, const Node& v) { |
---|
320 | return (u.spec==v.spec && |
---|
321 | static_cast<typename Graph::Node>(u)== |
---|
322 | static_cast<typename Graph::Node>(v)); |
---|
323 | } |
---|
324 | friend bool operator!=(const Node& u, const Node& v) { |
---|
325 | return (v.spec!=u.spec || |
---|
326 | static_cast<typename Graph::Node>(u)!= |
---|
327 | static_cast<typename Graph::Node>(v)); |
---|
328 | } |
---|
329 | // template<typename G> |
---|
330 | // friend std::ostream& |
---|
331 | // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i); |
---|
332 | friend std::ostream& operator<< (std::ostream& os, const Node& i); |
---|
333 | int getSpec() const { return spec; } |
---|
334 | }; |
---|
335 | |
---|
336 | class NodeIt { |
---|
337 | friend class GraphWrapper<Graph>; |
---|
338 | friend class stGraphWrapper<Graph>; |
---|
339 | typename Graph::NodeIt n; |
---|
340 | int spec; |
---|
341 | public: |
---|
342 | NodeIt() { } |
---|
343 | NodeIt(const typename Graph::NodeIt& _n, int _spec) : |
---|
344 | n(_n), spec(_spec) { } |
---|
345 | NodeIt(const Invalid& i) : n(i), spec(3) { } |
---|
346 | NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) { |
---|
347 | if (!_G.graph->valid(n)) spec=1; |
---|
348 | } |
---|
349 | operator Node() const { return Node(n, spec); } |
---|
350 | }; |
---|
351 | |
---|
352 | class Edge : public Graph::Edge { |
---|
353 | friend class GraphWrapper<Graph>; |
---|
354 | friend class stGraphWrapper<Graph>; |
---|
355 | template <typename T, typename Parent> friend class EdgeMap; |
---|
356 | int spec; |
---|
357 | typename Graph::Node n; |
---|
358 | public: |
---|
359 | Edge() { } |
---|
360 | Edge(const typename Graph::Edge& _e, int _spec, |
---|
361 | const typename Graph::Node& _n) : |
---|
362 | Graph::Edge(_e), spec(_spec), n(_n) { |
---|
363 | } |
---|
364 | Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { } |
---|
365 | friend bool operator==(const Edge& u, const Edge& v) { |
---|
366 | return (u.spec==v.spec && |
---|
367 | static_cast<typename Graph::Edge>(u)== |
---|
368 | static_cast<typename Graph::Edge>(v) && |
---|
369 | u.n==v.n); |
---|
370 | } |
---|
371 | friend bool operator!=(const Edge& u, const Edge& v) { |
---|
372 | return (v.spec!=u.spec || |
---|
373 | static_cast<typename Graph::Edge>(u)!= |
---|
374 | static_cast<typename Graph::Edge>(v) || |
---|
375 | u.n!=v.n); |
---|
376 | } |
---|
377 | // template<typename G> |
---|
378 | // friend std::ostream& |
---|
379 | // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i); |
---|
380 | friend std::ostream& operator<< (std::ostream& os, const Edge& i); |
---|
381 | int getSpec() const { return spec; } |
---|
382 | }; |
---|
383 | |
---|
384 | class OutEdgeIt { |
---|
385 | friend class GraphWrapper<Graph>; |
---|
386 | friend class stGraphWrapper<Graph>; |
---|
387 | typename Graph::OutEdgeIt e; |
---|
388 | int spec; |
---|
389 | typename Graph::ClassNodeIt n; |
---|
390 | public: |
---|
391 | OutEdgeIt() { } |
---|
392 | OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec, |
---|
393 | const typename Graph::ClassNodeIt& _n) : |
---|
394 | e(_e), spec(_spec), n(_n) { |
---|
395 | } |
---|
396 | OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { } |
---|
397 | OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) { |
---|
398 | switch (_n.spec) { |
---|
399 | case 0 : |
---|
400 | if (_G.graph->inSClass(_n)) { //S, van normalis kiel |
---|
401 | e=typename Graph::OutEdgeIt(*(_G.graph), |
---|
402 | typename Graph::Node(_n)); |
---|
403 | spec=0; |
---|
404 | n=INVALID; |
---|
405 | if (!_G.graph->valid(e)) spec=3; |
---|
406 | } else { //T, specko kiel van |
---|
407 | e=INVALID; |
---|
408 | spec=2; |
---|
409 | n=_n; |
---|
410 | } |
---|
411 | break; |
---|
412 | case 1: |
---|
413 | e=INVALID; |
---|
414 | spec=1; |
---|
415 | _G.graph->first(n, S_CLASS); //s->vmi; |
---|
416 | if (!_G.graph->valid(n)) spec=3; //Ha S ures |
---|
417 | break; |
---|
418 | case 2: |
---|
419 | e=INVALID; |
---|
420 | spec=3; |
---|
421 | n=INVALID; |
---|
422 | break; |
---|
423 | } |
---|
424 | } |
---|
425 | operator Edge() const { return Edge(e, spec, n); } |
---|
426 | }; |
---|
427 | |
---|
428 | class InEdgeIt { |
---|
429 | friend class GraphWrapper<Graph>; |
---|
430 | friend class stGraphWrapper<Graph>; |
---|
431 | typename Graph::InEdgeIt e; |
---|
432 | int spec; |
---|
433 | typename Graph::ClassNodeIt n; |
---|
434 | public: |
---|
435 | InEdgeIt() { } |
---|
436 | InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec, |
---|
437 | const typename Graph::ClassNodeIt& _n) : |
---|
438 | e(_e), spec(_spec), n(_n) { |
---|
439 | } |
---|
440 | InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { } |
---|
441 | InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) { |
---|
442 | switch (_n.spec) { |
---|
443 | case 0 : |
---|
444 | if (_G.graph->inTClass(_n)) { //T, van normalis beel |
---|
445 | e=typename Graph::InEdgeIt(*(_G.graph), |
---|
446 | typename Graph::Node(_n)); |
---|
447 | spec=0; |
---|
448 | n=INVALID; |
---|
449 | if (!_G.graph->valid(e)) spec=3; |
---|
450 | } else { //S, specko beel van |
---|
451 | e=INVALID; |
---|
452 | spec=1; |
---|
453 | n=_n; |
---|
454 | } |
---|
455 | break; |
---|
456 | case 1: |
---|
457 | e=INVALID; |
---|
458 | spec=3; |
---|
459 | n=INVALID; |
---|
460 | break; |
---|
461 | case 2: |
---|
462 | e=INVALID; |
---|
463 | spec=2; |
---|
464 | _G.graph->first(n, T_CLASS); //vmi->t; |
---|
465 | if (!_G.graph->valid(n)) spec=3; //Ha T ures |
---|
466 | break; |
---|
467 | } |
---|
468 | } |
---|
469 | operator Edge() const { return Edge(e, spec, n); } |
---|
470 | }; |
---|
471 | |
---|
472 | class EdgeIt { |
---|
473 | friend class GraphWrapper<Graph>; |
---|
474 | friend class stGraphWrapper<Graph>; |
---|
475 | typename Graph::EdgeIt e; |
---|
476 | int spec; |
---|
477 | typename Graph::ClassNodeIt n; |
---|
478 | public: |
---|
479 | EdgeIt() { } |
---|
480 | EdgeIt(const typename Graph::EdgeIt& _e, int _spec, |
---|
481 | const typename Graph::ClassNodeIt& _n) : |
---|
482 | e(_e), spec(_spec), n(_n) { } |
---|
483 | EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { } |
---|
484 | EdgeIt(const stGraphWrapper<Graph>& _G) : |
---|
485 | e(*(_G.graph)), spec(0), n(INVALID) { |
---|
486 | if (!_G.graph->valid(e)) { |
---|
487 | spec=1; |
---|
488 | _G.graph->first(n, S_CLASS); |
---|
489 | if (!_G.graph->valid(n)) { //Ha S ures |
---|
490 | spec=2; |
---|
491 | _G.graph->first(n, T_CLASS); |
---|
492 | if (!_G.graph->valid(n)) { //Ha T ures |
---|
493 | spec=3; |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|
497 | } |
---|
498 | operator Edge() const { return Edge(e, spec, n); } |
---|
499 | }; |
---|
500 | |
---|
501 | NodeIt& first(NodeIt& i) const { |
---|
502 | i=NodeIt(*this); return i; |
---|
503 | } |
---|
504 | OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { |
---|
505 | i=OutEdgeIt(*this, p); return i; |
---|
506 | } |
---|
507 | InEdgeIt& first(InEdgeIt& i, const Node& p) const { |
---|
508 | i=InEdgeIt(*this, p); return i; |
---|
509 | } |
---|
510 | EdgeIt& first(EdgeIt& i) const { |
---|
511 | i=EdgeIt(*this); return i; |
---|
512 | } |
---|
513 | |
---|
514 | NodeIt& next(NodeIt& i) const { |
---|
515 | switch (i.spec) { |
---|
516 | case 0: |
---|
517 | this->graph->next(i.n); |
---|
518 | if (!this->graph->valid(i.n)) { |
---|
519 | i.spec=1; |
---|
520 | } |
---|
521 | break; |
---|
522 | case 1: |
---|
523 | i.spec=2; |
---|
524 | break; |
---|
525 | case 2: |
---|
526 | i.spec=3; |
---|
527 | break; |
---|
528 | } |
---|
529 | return i; |
---|
530 | } |
---|
531 | OutEdgeIt& next(OutEdgeIt& i) const { |
---|
532 | typename Graph::Node v; |
---|
533 | switch (i.spec) { |
---|
534 | case 0: //normal edge |
---|
535 | v=this->graph->aNode(i.e); |
---|
536 | this->graph->next(i.e); |
---|
537 | if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk |
---|
538 | if (this->graph->inSClass(v)) { //S, nincs kiel |
---|
539 | i.spec=3; |
---|
540 | i.n=INVALID; |
---|
541 | } else { //T, van kiel |
---|
542 | i.spec=2; |
---|
543 | i.n=v; |
---|
544 | } |
---|
545 | } |
---|
546 | break; |
---|
547 | case 1: //s->vmi |
---|
548 | this->graph->next(i.n); |
---|
549 | if (!this->graph->valid(i.n)) i.spec=3; |
---|
550 | break; |
---|
551 | case 2: //vmi->t |
---|
552 | i.spec=3; |
---|
553 | i.n=INVALID; |
---|
554 | break; |
---|
555 | } |
---|
556 | return i; |
---|
557 | } |
---|
558 | InEdgeIt& next(InEdgeIt& i) const { |
---|
559 | typename Graph::Node v; |
---|
560 | switch (i.spec) { |
---|
561 | case 0: //normal edge |
---|
562 | v=this->graph->aNode(i.e); |
---|
563 | this->graph->next(i.e); |
---|
564 | if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk |
---|
565 | if (this->graph->inTClass(v)) { //S, nincs beel |
---|
566 | i.spec=3; |
---|
567 | i.n=INVALID; |
---|
568 | } else { //S, van beel |
---|
569 | i.spec=1; |
---|
570 | i.n=v; |
---|
571 | } |
---|
572 | } |
---|
573 | break; |
---|
574 | case 1: //s->vmi |
---|
575 | i.spec=3; |
---|
576 | i.n=INVALID; |
---|
577 | break; |
---|
578 | case 2: //vmi->t |
---|
579 | this->graph->next(i.n); |
---|
580 | if (!this->graph->valid(i.n)) i.spec=3; |
---|
581 | break; |
---|
582 | } |
---|
583 | return i; |
---|
584 | } |
---|
585 | |
---|
586 | EdgeIt& next(EdgeIt& i) const { |
---|
587 | switch (i.spec) { |
---|
588 | case 0: |
---|
589 | this->graph->next(i.e); |
---|
590 | if (!this->graph->valid(i.e)) { |
---|
591 | i.spec=1; |
---|
592 | this->graph->first(i.n, S_CLASS); |
---|
593 | if (!this->graph->valid(i.n)) { |
---|
594 | i.spec=2; |
---|
595 | this->graph->first(i.n, T_CLASS); |
---|
596 | if (!this->graph->valid(i.n)) i.spec=3; |
---|
597 | } |
---|
598 | } |
---|
599 | break; |
---|
600 | case 1: |
---|
601 | this->graph->next(i.n); |
---|
602 | if (!this->graph->valid(i.n)) { |
---|
603 | i.spec=2; |
---|
604 | this->graph->first(i.n, T_CLASS); |
---|
605 | if (!this->graph->valid(i.n)) i.spec=3; |
---|
606 | } |
---|
607 | break; |
---|
608 | case 2: |
---|
609 | this->graph->next(i.n); |
---|
610 | if (!this->graph->valid(i.n)) i.spec=3; |
---|
611 | break; |
---|
612 | } |
---|
613 | return i; |
---|
614 | } |
---|
615 | |
---|
616 | Node tail(const Edge& e) const { |
---|
617 | switch (e.spec) { |
---|
618 | case 0: |
---|
619 | return Node(this->graph->tail(e)); |
---|
620 | break; |
---|
621 | case 1: |
---|
622 | return S_NODE; |
---|
623 | break; |
---|
624 | case 2: |
---|
625 | default: |
---|
626 | return Node(e.n); |
---|
627 | break; |
---|
628 | } |
---|
629 | } |
---|
630 | Node head(const Edge& e) const { |
---|
631 | switch (e.spec) { |
---|
632 | case 0: |
---|
633 | return Node(this->graph->head(e)); |
---|
634 | break; |
---|
635 | case 1: |
---|
636 | return Node(e.n); |
---|
637 | break; |
---|
638 | case 2: |
---|
639 | default: |
---|
640 | return T_NODE; |
---|
641 | break; |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | bool valid(const Node& n) const { return (n.spec<3); } |
---|
646 | bool valid(const Edge& e) const { return (e.spec<3); } |
---|
647 | |
---|
648 | int nodeNum() const { return this->graph->nodeNum()+2; } |
---|
649 | int edgeNum() const { |
---|
650 | return this->graph->edgeNum()+this->graph->nodeNum(); |
---|
651 | } |
---|
652 | |
---|
653 | Node aNode(const OutEdgeIt& e) const { return tail(e); } |
---|
654 | Node aNode(const InEdgeIt& e) const { return head(e); } |
---|
655 | Node bNode(const OutEdgeIt& e) const { return head(e); } |
---|
656 | Node bNode(const InEdgeIt& e) const { return tail(e); } |
---|
657 | |
---|
658 | void addNode() const { } |
---|
659 | void addEdge() const { } |
---|
660 | |
---|
661 | // Node addNode() const { return Node(this->graph->addNode()); } |
---|
662 | // Edge addEdge(const Node& tail, const Node& head) const { |
---|
663 | // return Edge(this->graph->addEdge(tail, head)); } |
---|
664 | |
---|
665 | // void erase(const Node& i) const { this->graph->erase(i); } |
---|
666 | // void erase(const Edge& i) const { this->graph->erase(i); } |
---|
667 | |
---|
668 | // void clear() const { this->graph->clear(); } |
---|
669 | |
---|
670 | template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> { |
---|
671 | typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent; |
---|
672 | T s_value, t_value; |
---|
673 | public: |
---|
674 | NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G), |
---|
675 | s_value(), |
---|
676 | t_value() { } |
---|
677 | NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a), |
---|
678 | s_value(a), |
---|
679 | t_value(a) { } |
---|
680 | T operator[](const Node& n) const { |
---|
681 | switch (n.spec) { |
---|
682 | case 0: |
---|
683 | return Parent::operator[](n); |
---|
684 | break; |
---|
685 | case 1: |
---|
686 | return s_value; |
---|
687 | break; |
---|
688 | case 2: |
---|
689 | default: |
---|
690 | return t_value; |
---|
691 | break; |
---|
692 | } |
---|
693 | } |
---|
694 | void set(const Node& n, T t) { |
---|
695 | switch (n.spec) { |
---|
696 | case 0: |
---|
697 | GraphWrapper<Graph>::template NodeMap<T>::set(n, t); |
---|
698 | break; |
---|
699 | case 1: |
---|
700 | s_value=t; |
---|
701 | break; |
---|
702 | case 2: |
---|
703 | default: |
---|
704 | t_value=t; |
---|
705 | break; |
---|
706 | } |
---|
707 | } |
---|
708 | }; |
---|
709 | |
---|
710 | template<typename T, |
---|
711 | typename Parent= |
---|
712 | typename GraphWrapper<Graph>::template EdgeMap<T> > |
---|
713 | class EdgeMap : public Parent { |
---|
714 | //typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent; |
---|
715 | typename GraphWrapper<Graph>::template NodeMap<T> node_value; |
---|
716 | public: |
---|
717 | EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G), |
---|
718 | node_value(_G) { } |
---|
719 | EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a), |
---|
720 | node_value(_G, a) { } |
---|
721 | T operator[](const Edge& e) const { |
---|
722 | switch (e.spec) { |
---|
723 | case 0: |
---|
724 | return Parent::operator[](e); |
---|
725 | break; |
---|
726 | case 1: |
---|
727 | return node_value[e.n]; |
---|
728 | break; |
---|
729 | case 2: |
---|
730 | default: |
---|
731 | return node_value[e.n]; |
---|
732 | break; |
---|
733 | } |
---|
734 | } |
---|
735 | void set(const Edge& e, T t) { |
---|
736 | switch (e.spec) { |
---|
737 | case 0: |
---|
738 | Parent::set(e, t); |
---|
739 | break; |
---|
740 | case 1: |
---|
741 | node_value.set(e.n, t); |
---|
742 | break; |
---|
743 | case 2: |
---|
744 | default: |
---|
745 | node_value.set(e.n, t); |
---|
746 | break; |
---|
747 | } |
---|
748 | } |
---|
749 | }; |
---|
750 | |
---|
751 | // template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> { |
---|
752 | // typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent; |
---|
753 | // typename GraphWrapper<Graph>::template NodeMap<T> node_value; |
---|
754 | // public: |
---|
755 | // EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G), |
---|
756 | // node_value(_G) { } |
---|
757 | // EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a), |
---|
758 | // node_value(_G, a) { } |
---|
759 | // T operator[](const Edge& e) const { |
---|
760 | // switch (e.spec) { |
---|
761 | // case 0: |
---|
762 | // return Parent::operator[](e); |
---|
763 | // break; |
---|
764 | // case 1: |
---|
765 | // return node_value[e.n]; |
---|
766 | // break; |
---|
767 | // case 2: |
---|
768 | // default: |
---|
769 | // return node_value[e.n]; |
---|
770 | // break; |
---|
771 | // } |
---|
772 | // } |
---|
773 | // void set(const Edge& e, T t) { |
---|
774 | // switch (e.spec) { |
---|
775 | // case 0: |
---|
776 | // GraphWrapper<Graph>::template EdgeMap<T>::set(e, t); |
---|
777 | // break; |
---|
778 | // case 1: |
---|
779 | // node_value.set(e.n, t); |
---|
780 | // break; |
---|
781 | // case 2: |
---|
782 | // default: |
---|
783 | // node_value.set(e.n, t); |
---|
784 | // break; |
---|
785 | // } |
---|
786 | // } |
---|
787 | // }; |
---|
788 | |
---|
789 | // template<typename G> |
---|
790 | friend std::ostream& |
---|
791 | operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) { |
---|
792 | os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")"; |
---|
793 | return os; |
---|
794 | } |
---|
795 | // template<typename G> |
---|
796 | friend std::ostream& |
---|
797 | operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) { |
---|
798 | os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec << |
---|
799 | " node: " << i.n << ")"; |
---|
800 | return os; |
---|
801 | } |
---|
802 | |
---|
803 | }; |
---|
804 | |
---|
805 | ///@} |
---|
806 | |
---|
807 | } //namespace hugo |
---|
808 | |
---|
809 | |
---|
810 | #endif //HUGO_GRAPH_WRAPPER_H |
---|
811 | |
---|