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