A bool Edge Map with iterators that goes through the true or the false edges.
authoralpar
Sat, 17 Apr 2004 13:15:53 +0000
changeset 348b63ea19e502e
parent 347 e4ab32225f1c
child 349 42c660f58702
A bool Edge Map with iterators that goes through the true or the false edges.
src/work/alpar/boolmap_iter.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/alpar/boolmap_iter.cc	Sat Apr 17 13:15:53 2004 +0000
     1.3 @@ -0,0 +1,131 @@
     1.4 +#include <iostream>
     1.5 +#include <vector>
     1.6 +#include <smart_graph.h>
     1.7 +
     1.8 +using namespace hugo;
     1.9 +
    1.10 +///\todo This is only a static map!
    1.11 +///
    1.12 +template<class GG>
    1.13 +class BoolIterEdgeMap
    1.14 +{
    1.15 +public:
    1.16 +  typedef GG Graph;
    1.17 +  typedef typename GG::Edge Edge;
    1.18 +  
    1.19 +  typedef Edge KeyType;
    1.20 +  typedef bool ValueType;
    1.21 +  
    1.22 +  friend class RefType;
    1.23 +  friend class FalseIterator;
    1.24 +  friend class TrueIterator;
    1.25 +
    1.26 +private:
    1.27 +  Graph &G;
    1.28 +  typename Graph::EdgeMap<int> cref;
    1.29 +  std::vector<Edge> vals;
    1.30 +  int sep;           //map[e] is true <=> cref[e]>=sep
    1.31 +  
    1.32 +  bool isTrue(Edge e) {return cref[e]>=sep;}
    1.33 +  void swap(Edge e, int s) 
    1.34 +  {
    1.35 +    int ti=cref[e];
    1.36 +    Edge te=vals[s];
    1.37 +    cref[e]=s; vals[s]=e;
    1.38 +    cref[te]=ti; vals[ti]=te;
    1.39 +  }  
    1.40 +
    1.41 +  void setTrue(Edge e) { if(cref[e]<sep) { sep--; swap(e,sep); } }
    1.42 +  void setFalse(Edge e) { if(cref[e]>=sep) { swap(e,sep); sep++; } }
    1.43 +  
    1.44 +public:
    1.45 +  class FalseIterator 
    1.46 +  {
    1.47 +    BoolIterEdgeMap &M;
    1.48 +    int i;
    1.49 +  public:
    1.50 +    FalseIterator(BoolIterEdgeMap &_M) : M(_M), i(0) { }
    1.51 +    FalseIterator &operator++() { ++i; return *this;}
    1.52 +    operator Edge() { return i<M.sep ? M.vals[i] : INVALID; }
    1.53 +    operator bool() { return i<M.sep; }
    1.54 +  };
    1.55 +  class TrueIterator 
    1.56 +  {
    1.57 +    BoolIterEdgeMap &M;
    1.58 +    int i;
    1.59 +  public:
    1.60 +    TrueIterator(BoolIterEdgeMap &_M) : M(_M), i(M.vals.size()-1) { }
    1.61 +    TrueIterator &operator++() { --i; return *this;}
    1.62 +    operator Edge() { return i>=M.sep ? M.vals[i] : INVALID; }
    1.63 +    operator bool() { return i>=M.sep; }
    1.64 +  };
    1.65 +  
    1.66 +  class RefType 
    1.67 +  {
    1.68 +    BoolIterEdgeMap &M;
    1.69 +    Edge e;
    1.70 +  public:
    1.71 +    RefType(BoolIterEdgeMap &_M,Edge _e) : M(_M), e(_e) { }
    1.72 +    
    1.73 +    operator ValueType() const 
    1.74 +    {
    1.75 +      return M.isTrue(e);
    1.76 +      
    1.77 +    }
    1.78 +    ValueType operator = (ValueType v) const
    1.79 +    {
    1.80 +      if(v) M.setTrue(e); 
    1.81 +      else M.setFalse(e);
    1.82 +      return v;
    1.83 +    }
    1.84 +  };
    1.85 +  
    1.86 +public:
    1.87 +  BoolIterEdgeMap(Graph &_G) : G(_G), cref(G)
    1.88 +  {
    1.89 +    sep=0;
    1.90 +    for(typename Graph::EdgeIt e(G);G.valid(e);G.next(e)) {
    1.91 +      cref[e]=sep;
    1.92 +      vals.push_back(e);
    1.93 +      sep++;
    1.94 +    }
    1.95 +  }
    1.96 +  RefType operator[] (Edge e) { return RefType(*this,e);}  
    1.97 +};
    1.98 +
    1.99 +int main()
   1.100 +{
   1.101 +  typedef SmartGraph Graph;
   1.102 +  typedef Graph::NodeIt NodeIt;
   1.103 +  typedef Graph::OutEdgeIt OutEdgeIt;
   1.104 +  typedef Graph::EdgeIt EdgeIt;
   1.105 +  
   1.106 +  Graph G;
   1.107 +
   1.108 +  for(int i=0;i<3;i++) G.addNode();
   1.109 +
   1.110 +  for(NodeIt n(G);G.valid(n);G.next(n))
   1.111 +    for(NodeIt m(G);G.valid(m);G.next(m)) if(n!=m)
   1.112 +      G.addEdge(n,m);
   1.113 +
   1.114 +  //for(OutEdgeIt e(G,NodeIt(G));G.valid(e);G.next(e))
   1.115 +    
   1.116 +  BoolIterEdgeMap<Graph> map(G);
   1.117 +
   1.118 +  bool b=true;
   1.119 +  
   1.120 +  for(EdgeIt e(G);G.valid(e);G.next(e)) {map[e]=b;b=!b;}
   1.121 +  
   1.122 +  std::cout << true << '\n';
   1.123 +
   1.124 +  for(EdgeIt e(G);G.valid(e);G.next(e))
   1.125 +    std::cout << G.id(G.tail(e)) << "->" << G.id(G.head(e))
   1.126 +      << ": " << map[e] << '\n';
   1.127 +  std::cout << "True Edges:\n";
   1.128 +  for(BoolIterEdgeMap<Graph>::TrueIterator i(map);i;++i)
   1.129 +    std::cout << G.id(G.tail(i)) << "->" << G.id(G.head(i)) << '\n';
   1.130 +  std::cout << "False Edges:\n";
   1.131 +  for(BoolIterEdgeMap<Graph>::FalseIterator i(map);i;++i)
   1.132 +    std::cout << G.id(G.tail(i)) << "->" << G.id(G.head(i)) << '\n';
   1.133 +}
   1.134 +