src/work/alpar/boolmap_iter.cc
author marci
Tue, 31 Aug 2004 17:59:33 +0000
changeset 778 08a1d1e3070d
child 921 818510fa3d99
permissions -rw-r--r--
.
alpar@348
     1
#include <iostream>
alpar@348
     2
#include <vector>
alpar@348
     3
#include <smart_graph.h>
alpar@348
     4
alpar@348
     5
using namespace hugo;
alpar@348
     6
alpar@348
     7
///\todo This is only a static map!
alpar@348
     8
///
alpar@348
     9
template<class GG>
alpar@348
    10
class BoolIterEdgeMap
alpar@348
    11
{
alpar@348
    12
public:
alpar@348
    13
  typedef GG Graph;
alpar@348
    14
  typedef typename GG::Edge Edge;
alpar@348
    15
  
alpar@348
    16
  typedef Edge KeyType;
alpar@348
    17
  typedef bool ValueType;
alpar@348
    18
  
alpar@348
    19
  friend class RefType;
alpar@348
    20
  friend class FalseIterator;
alpar@348
    21
  friend class TrueIterator;
alpar@348
    22
alpar@348
    23
private:
alpar@348
    24
  Graph &G;
alpar@348
    25
  typename Graph::EdgeMap<int> cref;
alpar@348
    26
  std::vector<Edge> vals;
alpar@348
    27
  int sep;           //map[e] is true <=> cref[e]>=sep
alpar@348
    28
  
alpar@348
    29
  bool isTrue(Edge e) {return cref[e]>=sep;}
alpar@348
    30
  void swap(Edge e, int s) 
alpar@348
    31
  {
alpar@348
    32
    int ti=cref[e];
alpar@348
    33
    Edge te=vals[s];
alpar@348
    34
    cref[e]=s; vals[s]=e;
alpar@348
    35
    cref[te]=ti; vals[ti]=te;
alpar@348
    36
  }  
alpar@348
    37
alpar@348
    38
  void setTrue(Edge e) { if(cref[e]<sep) { sep--; swap(e,sep); } }
alpar@348
    39
  void setFalse(Edge e) { if(cref[e]>=sep) { swap(e,sep); sep++; } }
alpar@348
    40
  
alpar@348
    41
public:
alpar@348
    42
  class FalseIterator 
alpar@348
    43
  {
alpar@348
    44
    BoolIterEdgeMap &M;
alpar@348
    45
    int i;
alpar@348
    46
  public:
alpar@348
    47
    FalseIterator(BoolIterEdgeMap &_M) : M(_M), i(0) { }
alpar@348
    48
    FalseIterator &operator++() { ++i; return *this;}
alpar@348
    49
    operator Edge() { return i<M.sep ? M.vals[i] : INVALID; }
alpar@348
    50
    operator bool() { return i<M.sep; }
alpar@348
    51
  };
alpar@348
    52
  class TrueIterator 
alpar@348
    53
  {
alpar@348
    54
    BoolIterEdgeMap &M;
alpar@348
    55
    int i;
alpar@348
    56
  public:
alpar@348
    57
    TrueIterator(BoolIterEdgeMap &_M) : M(_M), i(M.vals.size()-1) { }
alpar@348
    58
    TrueIterator &operator++() { --i; return *this;}
alpar@348
    59
    operator Edge() { return i>=M.sep ? M.vals[i] : INVALID; }
alpar@348
    60
    operator bool() { return i>=M.sep; }
alpar@348
    61
  };
alpar@348
    62
  
alpar@348
    63
  class RefType 
alpar@348
    64
  {
alpar@348
    65
    BoolIterEdgeMap &M;
alpar@348
    66
    Edge e;
alpar@348
    67
  public:
alpar@348
    68
    RefType(BoolIterEdgeMap &_M,Edge _e) : M(_M), e(_e) { }
alpar@348
    69
    
alpar@348
    70
    operator ValueType() const 
alpar@348
    71
    {
alpar@348
    72
      return M.isTrue(e);
alpar@348
    73
      
alpar@348
    74
    }
alpar@348
    75
    ValueType operator = (ValueType v) const
alpar@348
    76
    {
alpar@348
    77
      if(v) M.setTrue(e); 
alpar@348
    78
      else M.setFalse(e);
alpar@348
    79
      return v;
alpar@348
    80
    }
alpar@348
    81
  };
alpar@348
    82
  
alpar@348
    83
public:
alpar@348
    84
  BoolIterEdgeMap(Graph &_G) : G(_G), cref(G)
alpar@348
    85
  {
alpar@348
    86
    sep=0;
alpar@348
    87
    for(typename Graph::EdgeIt e(G);G.valid(e);G.next(e)) {
alpar@348
    88
      cref[e]=sep;
alpar@348
    89
      vals.push_back(e);
alpar@348
    90
      sep++;
alpar@348
    91
    }
alpar@348
    92
  }
alpar@348
    93
  RefType operator[] (Edge e) { return RefType(*this,e);}  
alpar@348
    94
};
alpar@348
    95
alpar@348
    96
int main()
alpar@348
    97
{
alpar@348
    98
  typedef SmartGraph Graph;
alpar@348
    99
  typedef Graph::NodeIt NodeIt;
alpar@348
   100
  typedef Graph::OutEdgeIt OutEdgeIt;
alpar@348
   101
  typedef Graph::EdgeIt EdgeIt;
alpar@348
   102
  
alpar@348
   103
  Graph G;
alpar@348
   104
alpar@348
   105
  for(int i=0;i<3;i++) G.addNode();
alpar@348
   106
alpar@348
   107
  for(NodeIt n(G);G.valid(n);G.next(n))
alpar@348
   108
    for(NodeIt m(G);G.valid(m);G.next(m)) if(n!=m)
alpar@348
   109
      G.addEdge(n,m);
alpar@348
   110
alpar@348
   111
  //for(OutEdgeIt e(G,NodeIt(G));G.valid(e);G.next(e))
alpar@348
   112
    
alpar@348
   113
  BoolIterEdgeMap<Graph> map(G);
alpar@348
   114
alpar@348
   115
  bool b=true;
alpar@348
   116
  
alpar@348
   117
  for(EdgeIt e(G);G.valid(e);G.next(e)) {map[e]=b;b=!b;}
alpar@348
   118
  
alpar@348
   119
  std::cout << true << '\n';
alpar@348
   120
alpar@348
   121
  for(EdgeIt e(G);G.valid(e);G.next(e))
alpar@348
   122
    std::cout << G.id(G.tail(e)) << "->" << G.id(G.head(e))
alpar@348
   123
      << ": " << map[e] << '\n';
alpar@348
   124
  std::cout << "True Edges:\n";
alpar@348
   125
  for(BoolIterEdgeMap<Graph>::TrueIterator i(map);i;++i)
alpar@348
   126
    std::cout << G.id(G.tail(i)) << "->" << G.id(G.head(i)) << '\n';
alpar@348
   127
  std::cout << "False Edges:\n";
alpar@348
   128
  for(BoolIterEdgeMap<Graph>::FalseIterator i(map);i;++i)
alpar@348
   129
    std::cout << G.id(G.tail(i)) << "->" << G.id(G.head(i)) << '\n';
alpar@348
   130
}
alpar@348
   131