src/work/alpar/boolmap_iter.cc
author alpar
Fri, 08 Apr 2005 15:22:46 +0000
changeset 1327 ecc1cdea2ee7
parent 987 87f7c54892df
permissions -rw-r--r--
Disable shared libraries (makes the compilation a bit faster)
alpar@348
     1
#include <iostream>
alpar@348
     2
#include <vector>
alpar@1280
     3
#include <lemon/smart_graph.h>
alpar@1280
     4
#include <limits>
alpar@348
     5
alpar@921
     6
using namespace lemon;
alpar@348
     7
alpar@348
     8
///\todo This is only a static map!
alpar@1280
     9
///\param BaseMap is an interger map.
alpar@1280
    10
template<class BaseMap>
alpar@1280
    11
class BoolIterMap
alpar@348
    12
{
alpar@348
    13
public:
alpar@348
    14
  
alpar@1280
    15
  typedef typename BaseMap::Key Key;
alpar@987
    16
  typedef bool Value;
alpar@348
    17
  
alpar@348
    18
  friend class RefType;
alpar@1280
    19
  friend class FalseIt;
alpar@1280
    20
  friend class TrueIt;
alpar@348
    21
alpar@348
    22
private:
alpar@1280
    23
  BaseMap &cref;
alpar@1280
    24
  std::vector<Key> vals;
alpar@348
    25
  int sep;           //map[e] is true <=> cref[e]>=sep
alpar@348
    26
  
alpar@1280
    27
  bool isTrue(Key k) {return cref[k]>=sep;}
alpar@1280
    28
  void swap(Key k, int s) 
alpar@348
    29
  {
alpar@1280
    30
    int ti=cref[k];
alpar@1280
    31
    Key tk=vals[s];
alpar@1280
    32
    cref[k]=s; vals[s]=k;
alpar@1280
    33
    cref[tk]=ti; vals[ti]=tk;
alpar@348
    34
  }  
alpar@348
    35
alpar@1280
    36
  void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
alpar@1280
    37
  void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
alpar@348
    38
  
alpar@348
    39
public:
alpar@1280
    40
  ///\e
alpar@1280
    41
  void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
alpar@1280
    42
alpar@1280
    43
  ///\e
alpar@1280
    44
  class FalseIt
alpar@348
    45
  {
alpar@1280
    46
    const BoolIterMap &M;
alpar@348
    47
    int i;
alpar@348
    48
  public:
alpar@1280
    49
    explicit FalseIt(const BoolIterMap &_M) : M(_M), i(0) { }
alpar@1280
    50
    FalseIt(Invalid)
alpar@1280
    51
      : M(*((BoolIterMap*)(0))), i(std::numeric_limits<int>::max()) { }
alpar@1280
    52
    FalseIt &operator++() { ++i; return *this;}
alpar@1280
    53
    operator Key() { return i<M.sep ? M.vals[i] : INVALID; }
alpar@1280
    54
    bool operator !=(Invalid) { return i<M.sep; }
alpar@1280
    55
    bool operator ==(Invalid) { return i>=M.sep; }
alpar@348
    56
  };
alpar@1280
    57
  ///\e
alpar@1280
    58
  class TrueIt
alpar@348
    59
  {
alpar@1280
    60
    BoolIterMap &M;
alpar@348
    61
    int i;
alpar@348
    62
  public:
alpar@1280
    63
    explicit TrueIt(BoolIterMap &_M) : M(_M), i(M.vals.size()-1) { }
alpar@1280
    64
    TrueIt(Invalid)
alpar@1280
    65
      : M(*((BoolIterMap*)(0))), i(-1) { }
alpar@1280
    66
    TrueIt &operator++() { --i; return *this;}
alpar@1280
    67
    operator Key() { return i>=M.sep ? M.vals[i] : INVALID; }
alpar@1280
    68
    bool operator !=(Invalid) { return i>=M.sep; }
alpar@1280
    69
    bool operator ==(Invalid) { return i<M.sep; }
alpar@348
    70
  };
alpar@348
    71
  
alpar@1280
    72
  ///\e
alpar@1280
    73
  class RefType
alpar@348
    74
  {
alpar@1280
    75
    BoolIterMap &M;
alpar@1280
    76
    Key k;
alpar@348
    77
  public:
alpar@1280
    78
    RefType(BoolIterMap &_M,Key _k) : M(_M), k(_k) { }
alpar@348
    79
    
alpar@987
    80
    operator Value() const 
alpar@348
    81
    {
alpar@1280
    82
      return M.isTrue(k);
alpar@348
    83
    }
alpar@1280
    84
    Value operator = (Value v) const { M.set(k,v); return v; }
alpar@348
    85
  };
alpar@348
    86
  
alpar@348
    87
public:
alpar@1280
    88
  explicit BoolIterMap(BaseMap &_m) : cref(_m)
alpar@348
    89
  {
alpar@348
    90
    sep=0;
alpar@1280
    91
    for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
alpar@1280
    92
	i!=cref.mapSet().end();
alpar@1280
    93
	++i) {
alpar@1280
    94
      i->second=sep;
alpar@1280
    95
      vals.push_back(i->first);
alpar@348
    96
      sep++;
alpar@348
    97
    }
alpar@348
    98
  }
alpar@1280
    99
  RefType operator[] (Key k) { return RefType(*this,k);}  
alpar@1280
   100
  Value operator[] (Key k) const { return isTrue(k);}  
alpar@348
   101
};
alpar@348
   102
alpar@348
   103
int main()
alpar@348
   104
{
alpar@348
   105
  typedef SmartGraph Graph;
alpar@348
   106
  typedef Graph::NodeIt NodeIt;
alpar@348
   107
  typedef Graph::OutEdgeIt OutEdgeIt;
alpar@348
   108
  typedef Graph::EdgeIt EdgeIt;
alpar@348
   109
  
alpar@348
   110
  Graph G;
alpar@348
   111
alpar@348
   112
  for(int i=0;i<3;i++) G.addNode();
alpar@348
   113
alpar@1280
   114
  for(NodeIt n(G);n!=INVALID;++n)
alpar@1280
   115
    for(NodeIt m(G);m!=INVALID;++m) if(n!=m)
alpar@348
   116
      G.addEdge(n,m);
alpar@348
   117
alpar@348
   118
  //for(OutEdgeIt e(G,NodeIt(G));G.valid(e);G.next(e))
alpar@348
   119
    
alpar@1280
   120
  Graph::EdgeMap<int> tem(G);
alpar@1280
   121
  typedef BoolIterMap<Graph::EdgeMap<int> > BoolIterEdgeMap;
alpar@1280
   122
  BoolIterEdgeMap map(tem);
alpar@348
   123
alpar@348
   124
  bool b=true;
alpar@348
   125
  
alpar@1280
   126
  for(EdgeIt e(G);e!=INVALID;++e) {map[e]=b;b=!b;}
alpar@348
   127
  
alpar@348
   128
  std::cout << true << '\n';
alpar@348
   129
alpar@1280
   130
  for(EdgeIt e(G);e!=INVALID;++e) 
alpar@986
   131
    std::cout << G.id(G.source(e)) << "->" << G.id(G.target(e))
alpar@348
   132
      << ": " << map[e] << '\n';
alpar@348
   133
  std::cout << "True Edges:\n";
alpar@1280
   134
  for(BoolIterEdgeMap::TrueIt i(map);i!=INVALID;++i)
alpar@986
   135
    std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
alpar@348
   136
  std::cout << "False Edges:\n";
alpar@1280
   137
  for(BoolIterEdgeMap::FalseIt i(map);i!=INVALID;++i)
alpar@986
   138
    std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
alpar@348
   139
}
alpar@348
   140