[Lemon-commits] [lemon_svn] alpar: r1712 - hugo/trunk/src/work/alpar

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:47:08 CET 2006


Author: alpar
Date: Thu Mar 31 11:33:52 2005
New Revision: 1712

Modified:
   hugo/trunk/src/work/alpar/boolmap_iter.cc

Log:
It works again


Modified: hugo/trunk/src/work/alpar/boolmap_iter.cc
==============================================================================
--- hugo/trunk/src/work/alpar/boolmap_iter.cc	(original)
+++ hugo/trunk/src/work/alpar/boolmap_iter.cc	Thu Mar 31 11:33:52 2005
@@ -1,96 +1,103 @@
 #include <iostream>
 #include <vector>
-#include <smart_graph.h>
+#include <lemon/smart_graph.h>
+#include <limits>
 
 using namespace lemon;
 
 ///\todo This is only a static map!
-///
-template<class GG>
-class BoolIterEdgeMap
+///\param BaseMap is an interger map.
+template<class BaseMap>
+class BoolIterMap
 {
 public:
-  typedef GG Graph;
-  typedef typename GG::Edge Edge;
   
-  typedef Edge Key;
+  typedef typename BaseMap::Key Key;
   typedef bool Value;
   
   friend class RefType;
-  friend class FalseIterator;
-  friend class TrueIterator;
+  friend class FalseIt;
+  friend class TrueIt;
 
 private:
-  Graph &G;
-  typename Graph::EdgeMap<int> cref;
-  std::vector<Edge> vals;
+  BaseMap &cref;
+  std::vector<Key> vals;
   int sep;           //map[e] is true <=> cref[e]>=sep
   
-  bool isTrue(Edge e) {return cref[e]>=sep;}
-  void swap(Edge e, int s) 
+  bool isTrue(Key k) {return cref[k]>=sep;}
+  void swap(Key k, int s) 
   {
-    int ti=cref[e];
-    Edge te=vals[s];
-    cref[e]=s; vals[s]=e;
-    cref[te]=ti; vals[ti]=te;
+    int ti=cref[k];
+    Key tk=vals[s];
+    cref[k]=s; vals[s]=k;
+    cref[tk]=ti; vals[ti]=tk;
   }  
 
-  void setTrue(Edge e) { if(cref[e]<sep) { sep--; swap(e,sep); } }
-  void setFalse(Edge e) { if(cref[e]>=sep) { swap(e,sep); sep++; } }
+  void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
+  void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
   
 public:
-  class FalseIterator 
+  ///\e
+  void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
+
+  ///\e
+  class FalseIt
   {
-    BoolIterEdgeMap &M;
+    const BoolIterMap &M;
     int i;
   public:
-    FalseIterator(BoolIterEdgeMap &_M) : M(_M), i(0) { }
-    FalseIterator &operator++() { ++i; return *this;}
-    operator Edge() { return i<M.sep ? M.vals[i] : INVALID; }
-    operator bool() { return i<M.sep; }
+    explicit FalseIt(const BoolIterMap &_M) : M(_M), i(0) { }
+    FalseIt(Invalid)
+      : M(*((BoolIterMap*)(0))), i(std::numeric_limits<int>::max()) { }
+    FalseIt &operator++() { ++i; return *this;}
+    operator Key() { return i<M.sep ? M.vals[i] : INVALID; }
+    bool operator !=(Invalid) { return i<M.sep; }
+    bool operator ==(Invalid) { return i>=M.sep; }
   };
-  class TrueIterator 
+  ///\e
+  class TrueIt
   {
-    BoolIterEdgeMap &M;
+    BoolIterMap &M;
     int i;
   public:
-    TrueIterator(BoolIterEdgeMap &_M) : M(_M), i(M.vals.size()-1) { }
-    TrueIterator &operator++() { --i; return *this;}
-    operator Edge() { return i>=M.sep ? M.vals[i] : INVALID; }
-    operator bool() { return i>=M.sep; }
+    explicit TrueIt(BoolIterMap &_M) : M(_M), i(M.vals.size()-1) { }
+    TrueIt(Invalid)
+      : M(*((BoolIterMap*)(0))), i(-1) { }
+    TrueIt &operator++() { --i; return *this;}
+    operator Key() { return i>=M.sep ? M.vals[i] : INVALID; }
+    bool operator !=(Invalid) { return i>=M.sep; }
+    bool operator ==(Invalid) { return i<M.sep; }
   };
   
-  class RefType 
+  ///\e
+  class RefType
   {
-    BoolIterEdgeMap &M;
-    Edge e;
+    BoolIterMap &M;
+    Key k;
   public:
-    RefType(BoolIterEdgeMap &_M,Edge _e) : M(_M), e(_e) { }
+    RefType(BoolIterMap &_M,Key _k) : M(_M), k(_k) { }
     
     operator Value() const 
     {
-      return M.isTrue(e);
-      
-    }
-    Value operator = (Value v) const
-    {
-      if(v) M.setTrue(e); 
-      else M.setFalse(e);
-      return v;
+      return M.isTrue(k);
     }
+    Value operator = (Value v) const { M.set(k,v); return v; }
   };
   
 public:
-  BoolIterEdgeMap(Graph &_G) : G(_G), cref(G)
+  explicit BoolIterMap(BaseMap &_m) : cref(_m)
   {
     sep=0;
-    for(typename Graph::EdgeIt e(G);G.valid(e);G.next(e)) {
-      cref[e]=sep;
-      vals.push_back(e);
+    for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
+	i!=cref.mapSet().end();
+	++i) {
+      i->second=sep;
+      vals.push_back(i->first);
       sep++;
     }
   }
-  RefType operator[] (Edge e) { return RefType(*this,e);}  
+  RefType operator[] (Key k) { return RefType(*this,k);}  
+  Value operator[] (Key k) const { return isTrue(k);}  
 };
 
 int main()
@@ -104,28 +111,30 @@
 
   for(int i=0;i<3;i++) G.addNode();
 
-  for(NodeIt n(G);G.valid(n);G.next(n))
-    for(NodeIt m(G);G.valid(m);G.next(m)) if(n!=m)
+  for(NodeIt n(G);n!=INVALID;++n)
+    for(NodeIt m(G);m!=INVALID;++m) if(n!=m)
       G.addEdge(n,m);
 
   //for(OutEdgeIt e(G,NodeIt(G));G.valid(e);G.next(e))
     
-  BoolIterEdgeMap<Graph> map(G);
+  Graph::EdgeMap<int> tem(G);
+  typedef BoolIterMap<Graph::EdgeMap<int> > BoolIterEdgeMap;
+  BoolIterEdgeMap map(tem);
 
   bool b=true;
   
-  for(EdgeIt e(G);G.valid(e);G.next(e)) {map[e]=b;b=!b;}
+  for(EdgeIt e(G);e!=INVALID;++e) {map[e]=b;b=!b;}
   
   std::cout << true << '\n';
 
-  for(EdgeIt e(G);G.valid(e);G.next(e))
+  for(EdgeIt e(G);e!=INVALID;++e) 
     std::cout << G.id(G.source(e)) << "->" << G.id(G.target(e))
       << ": " << map[e] << '\n';
   std::cout << "True Edges:\n";
-  for(BoolIterEdgeMap<Graph>::TrueIterator i(map);i;++i)
+  for(BoolIterEdgeMap::TrueIt i(map);i!=INVALID;++i)
     std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
   std::cout << "False Edges:\n";
-  for(BoolIterEdgeMap<Graph>::FalseIterator i(map);i;++i)
+  for(BoolIterEdgeMap::FalseIt i(map);i!=INVALID;++i)
     std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
 }
 



More information about the Lemon-commits mailing list