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