COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/bipartite_graph_wrapper.h @ 497:500456d50d21

Last change on this file since 497:500456d50d21 was 497:500456d50d21, checked in by marci, 20 years ago

bipartite graph in bipartite_graph_wrapper.h

File size: 22.3 KB
RevLine 
[174]1// -*- c++ -*-
[496]2#ifndef HUGO_BIPARTITE_GRAPH_WRAPPER_H
3#define HUGO_BIPARTITE_GRAPH_WRAPPER_H
[76]4
[491]5///\ingroup gwrappers
[457]6///\file
7///\brief Several graph wrappers.
8///
9///This file contains several useful graph wrapper functions.
10///
11///\author Marton Makai
12
[174]13#include <invalid.h>
[368]14#include <iter_map.h>
[496]15#include <graph_wrapper.h>
[174]16
[105]17namespace hugo {
[76]18
[368]19  /// A wrapper for composing a bipartite graph.
[371]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
[368]22  /// reference containing the elements for the
[371]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.
[457]25  ///
26  ///\author Marton Makai
[368]27  template<typename Graph>
28  class BipartiteGraphWrapper : public GraphWrapper<Graph> {
[497]29  protected:
[389]30    typedef IterableBoolMap< typename Graph::template NodeMap<int> >
31    SFalseTTrueMap;
[368]32    SFalseTTrueMap* s_false_t_true_map;
[393]33
[497]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
[368]39  public:
[409]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;
[379]44   
[409]45    bool S_CLASS;
46    bool T_CLASS;
47
[368]48    BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
[409]49      : GraphWrapper<Graph>(_graph), s_false_t_true_map(&_s_false_t_true_map),
50      S_CLASS(false), T_CLASS(true) { }
[368]51    typedef typename GraphWrapper<Graph>::Node Node;
52    //using GraphWrapper<Graph>::NodeIt;
53    typedef typename GraphWrapper<Graph>::Edge Edge;
54    //using GraphWrapper<Graph>::EdgeIt;
[393]55    class ClassNodeIt;
56    friend class ClassNodeIt;
57    class OutEdgeIt;
58    friend class OutEdgeIt;
59    class InEdgeIt;
60    friend class InEdgeIt;
[379]61    class ClassNodeIt {
[393]62      friend class BipartiteGraphWrapper<Graph>;
63    protected:
[368]64      Node n;
65    public:
[379]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);
[368]70      }
[381]71      //FIXME needed in new concept, important here
72      ClassNodeIt(const Node& _n) : n(_n) { }
[368]73      operator Node() const { return n; }
74    };
[379]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//     };
[368]95    class OutEdgeIt {
[393]96      friend class BipartiteGraphWrapper<Graph>;
97    protected:
[368]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])
[379]104          e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
[368]105        else
106          e=INVALID;
107      }
108      operator Edge() const { return Edge(typename Graph::Edge(e)); }
109    };
110    class InEdgeIt {
[393]111      friend class BipartiteGraphWrapper<Graph>;
112    protected:
[368]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])
[379]119          e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
[368]120        else
121          e=INVALID;
122      }
123      operator Edge() const { return Edge(typename Graph::Edge(e)); }
124    };
125
126    using GraphWrapper<Graph>::first;
[379]127    ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
[393]128      n=ClassNodeIt(*this, _class) ; return n; }
[379]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    }
[368]137
138    using GraphWrapper<Graph>::next;
[379]139    ClassNodeIt& next(ClassNodeIt& n) const {
[393]140      this->s_false_t_true_map->next(n.n); return n;
[368]141    }
[379]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//     }
[389]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; }
[368]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    }
[379]176
177    bool inSClass(const Node& n) const {
[381]178      return !(*(this->s_false_t_true_map))[n];
[379]179    }
180    bool inTClass(const Node& n) const {
[381]181      return (*(this->s_false_t_true_map))[n];
[379]182    }
[368]183  };
184
[497]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
[435]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//   }
[341]240
[379]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.
[457]244  ///
245  ///\author Marton Makai
[379]246  template<typename Graph>
247  class stGraphWrapper : public GraphWrapper<Graph> {
248  public:
249    class Node;
[381]250    friend class Node;
[379]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;
[381]255    friend class NodeIt;
[379]256//GNI, int
257    class Edge;
[381]258    friend class Edge;
[379]259//GE, int, GN
260//0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
261//invalid: (invalid, 3, invalid)
262    class OutEdgeIt;
[381]263    friend class OutEdgeIt;
[379]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;
[381]269    friend class InEdgeIt;
[379]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;
[381]275    friend class EdgeIt;
[379]276//(first, 0, invalid) ...
277//(invalid, 1, vmi)
278//(invalid, 2, vmi)
279//invalid, 3, invalid)
280    template <typename T> class NodeMap;
[409]281    template <typename T, typename Parent> class EdgeMap;
[341]282
[379]283//    template <typename T> friend class NodeMap;
284//    template <typename T> friend class EdgeMap;
[341]285
[379]286    const Node S_NODE;
287    const Node T_NODE;
[341]288
[379]289    static const bool S_CLASS=false;
290    static const bool T_CLASS=true;
[341]291
[379]292    stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
293                                    S_NODE(INVALID, 1),
294                                    T_NODE(INVALID, 2) { }
[341]295
[435]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
[379]304    class Node : public Graph::Node {
305    protected:
306      friend class GraphWrapper<Graph>;
307      friend class stGraphWrapper<Graph>;
[389]308      template <typename T> friend class NodeMap;
[380]309      friend class Edge;
310      friend class OutEdgeIt;
311      friend class InEdgeIt;
312      friend class EdgeIt;
[379]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));
[409]328      }
[435]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);
[379]333      int getSpec() const { return spec; }
334    };
[409]335
[379]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) { }
[381]346      NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
[409]347        if (!_G.graph->valid(n)) spec=1;
[379]348      }
349      operator Node() const { return Node(n, spec); }
350    };
[409]351
[379]352    class Edge : public Graph::Edge {
353      friend class GraphWrapper<Graph>;
354      friend class stGraphWrapper<Graph>;
[409]355      template <typename T, typename Parent> friend class EdgeMap;
[379]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      }
[435]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);
[379]381      int getSpec() const { return spec; }
382    };
[409]383
[379]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) { }
[381]397      OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
[379]398        switch (_n.spec) {
399          case 0 :
[381]400            if (_G.graph->inSClass(_n)) { //S, van normalis kiel
[379]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    };
[409]427
[379]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) { }
[381]441      InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
[379]442        switch (_n.spec) {
443          case 0 :
[381]444            if (_G.graph->inTClass(_n)) { //T, van normalis beel
[379]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;
[409]460            break;
[379]461          case 2:
462            e=INVALID;
[409]463            spec=2;
[379]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    };
[409]471
[379]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) { }
[381]484      EdgeIt(const stGraphWrapper<Graph>& _G) :
[379]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    };
[341]500   
[379]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    }
[341]513
[379]514    NodeIt& next(NodeIt& i) const {
515      switch (i.spec) {
516        case 0:
[389]517          this->graph->next(i.n);
518          if (!this->graph->valid(i.n)) {
[379]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 {
[393]532      typename Graph::Node v;
[379]533      switch (i.spec) {
534        case 0: //normal edge
[409]535          v=this->graph->aNode(i.e);
[389]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
[379]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
[389]548          this->graph->next(i.n);
549          if (!this->graph->valid(i.n)) i.spec=3;
[379]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 {
[393]559      typename Graph::Node v;
[379]560      switch (i.spec) {
561        case 0: //normal edge
[393]562          v=this->graph->aNode(i.e);
[389]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
[379]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
[389]579          this->graph->next(i.n);
580          if (!this->graph->valid(i.n)) i.spec=3;
[379]581          break;
582      }
583      return i;
584    }
[341]585
[379]586    EdgeIt& next(EdgeIt& i) const {
587      switch (i.spec) {
588        case 0:
[389]589          this->graph->next(i.e);
590          if (!this->graph->valid(i.e)) {
[379]591            i.spec=1;
[389]592            this->graph->first(i.n, S_CLASS);
593            if (!this->graph->valid(i.n)) {
[379]594              i.spec=2;
[389]595              this->graph->first(i.n, T_CLASS);
596              if (!this->graph->valid(i.n)) i.spec=3;
[379]597            }
598          }
599          break;
600        case 1:
[389]601          this->graph->next(i.n);
602          if (!this->graph->valid(i.n)) {
[379]603            i.spec=2;
[389]604            this->graph->first(i.n, T_CLASS);
605            if (!this->graph->valid(i.n)) i.spec=3;
[379]606          }
607          break;
608        case 2:
[389]609          this->graph->next(i.n);
610          if (!this->graph->valid(i.n)) i.spec=3;
[379]611          break;
612      }
613      return i;
614    }   
[341]615
[379]616    Node tail(const Edge& e) const {
617      switch (e.spec) {
[393]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;
[379]628      }
629    }
630    Node head(const Edge& e) const {
631      switch (e.spec) {
[393]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;
[379]642      }
643    }
[341]644
[379]645    bool valid(const Node& n) const { return (n.spec<3); }
646    bool valid(const Edge& e) const { return (e.spec<3); }
647
[409]648    int nodeNum() const { return this->graph->nodeNum()+2; }
649    int edgeNum() const {
650      return this->graph->edgeNum()+this->graph->nodeNum();
651    }
[341]652 
[379]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); }
[409]657
658    void addNode() const { }
659    void addEdge() const { }
660   
[389]661//    Node addNode() const { return Node(this->graph->addNode()); }
[379]662//    Edge addEdge(const Node& tail, const Node& head) const {
[389]663//      return Edge(this->graph->addEdge(tail, head)); }
[341]664
[389]665//    void erase(const Node& i) const { this->graph->erase(i); }
666//    void erase(const Edge& i) const { this->graph->erase(i); }
[341]667 
[389]668//    void clear() const { this->graph->clear(); }
[341]669   
[389]670    template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
671      typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
[379]672      T s_value, t_value;
673    public:
[409]674      NodeMap(const stGraphWrapper<Graph>& _G) :  Parent(_G),
675                                                  s_value(),
676                                                  t_value() { }
[389]677      NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
678                                                      s_value(a),
679                                                      t_value(a) { }
[379]680      T operator[](const Node& n) const {
681        switch (n.spec) {
[393]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;
[379]692        }
693      }
694      void set(const Node& n, T t) {
695        switch (n.spec) {
[393]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;
[379]706        }
707      }
708    };
[341]709
[409]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;
[389]715      typename GraphWrapper<Graph>::template NodeMap<T> node_value;
[379]716    public:
[393]717      EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
718                                                 node_value(_G) { }
[389]719      EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
720                                                      node_value(_G, a) { }
[379]721      T operator[](const Edge& e) const {
722        switch (e.spec) {
[393]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;
[379]733        }
734      }
735      void set(const Edge& e, T t) {
736        switch (e.spec) {
[393]737        case 0:
[409]738          Parent::set(e, t);
[393]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;
[379]747        }
748      }
749    };
[409]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
[435]789//  template<typename G>
790    friend std::ostream&
791    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
[409]792      os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
793      return os;
794    }
[435]795//  template<typename G>
796    friend std::ostream&
797    operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
[409]798      os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
799        " node: " << i.n << ")";
800      return os;
801    }
802
[379]803  };
804
[406]805  ///@}
[341]806
[105]807} //namespace hugo
[76]808
[406]809
[259]810#endif //HUGO_GRAPH_WRAPPER_H
[76]811
Note: See TracBrowser for help on using the repository browser.