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