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