1.1 --- a/src/work/alpar/boolmap_iter.cc Sun Apr 17 18:57:22 2005 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,140 +0,0 @@
1.4 -#include <iostream>
1.5 -#include <vector>
1.6 -#include <lemon/smart_graph.h>
1.7 -#include <limits>
1.8 -
1.9 -using namespace lemon;
1.10 -
1.11 -///\todo This is only a static map!
1.12 -///\param BaseMap is an interger map.
1.13 -template<class BaseMap>
1.14 -class BoolIterMap
1.15 -{
1.16 -public:
1.17 -
1.18 - typedef typename BaseMap::Key Key;
1.19 - typedef bool Value;
1.20 -
1.21 - friend class RefType;
1.22 - friend class FalseIt;
1.23 - friend class TrueIt;
1.24 -
1.25 -private:
1.26 - BaseMap &cref;
1.27 - std::vector<Key> vals;
1.28 - int sep; //map[e] is true <=> cref[e]>=sep
1.29 -
1.30 - bool isTrue(Key k) {return cref[k]>=sep;}
1.31 - void swap(Key k, int s)
1.32 - {
1.33 - int ti=cref[k];
1.34 - Key tk=vals[s];
1.35 - cref[k]=s; vals[s]=k;
1.36 - cref[tk]=ti; vals[ti]=tk;
1.37 - }
1.38 -
1.39 - void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
1.40 - void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
1.41 -
1.42 -public:
1.43 - ///\e
1.44 - void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
1.45 -
1.46 - ///\e
1.47 - class FalseIt
1.48 - {
1.49 - const BoolIterMap &M;
1.50 - int i;
1.51 - public:
1.52 - explicit FalseIt(const BoolIterMap &_M) : M(_M), i(0) { }
1.53 - FalseIt(Invalid)
1.54 - : M(*((BoolIterMap*)(0))), i(std::numeric_limits<int>::max()) { }
1.55 - FalseIt &operator++() { ++i; return *this;}
1.56 - operator Key() { return i<M.sep ? M.vals[i] : INVALID; }
1.57 - bool operator !=(Invalid) { return i<M.sep; }
1.58 - bool operator ==(Invalid) { return i>=M.sep; }
1.59 - };
1.60 - ///\e
1.61 - class TrueIt
1.62 - {
1.63 - BoolIterMap &M;
1.64 - int i;
1.65 - public:
1.66 - explicit TrueIt(BoolIterMap &_M) : M(_M), i(M.vals.size()-1) { }
1.67 - TrueIt(Invalid)
1.68 - : M(*((BoolIterMap*)(0))), i(-1) { }
1.69 - TrueIt &operator++() { --i; return *this;}
1.70 - operator Key() { return i>=M.sep ? M.vals[i] : INVALID; }
1.71 - bool operator !=(Invalid) { return i>=M.sep; }
1.72 - bool operator ==(Invalid) { return i<M.sep; }
1.73 - };
1.74 -
1.75 - ///\e
1.76 - class RefType
1.77 - {
1.78 - BoolIterMap &M;
1.79 - Key k;
1.80 - public:
1.81 - RefType(BoolIterMap &_M,Key _k) : M(_M), k(_k) { }
1.82 -
1.83 - operator Value() const
1.84 - {
1.85 - return M.isTrue(k);
1.86 - }
1.87 - Value operator = (Value v) const { M.set(k,v); return v; }
1.88 - };
1.89 -
1.90 -public:
1.91 - explicit BoolIterMap(BaseMap &_m) : cref(_m)
1.92 - {
1.93 - sep=0;
1.94 - for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
1.95 - i!=cref.mapSet().end();
1.96 - ++i) {
1.97 - i->second=sep;
1.98 - vals.push_back(i->first);
1.99 - sep++;
1.100 - }
1.101 - }
1.102 - RefType operator[] (Key k) { return RefType(*this,k);}
1.103 - Value operator[] (Key k) const { return isTrue(k);}
1.104 -};
1.105 -
1.106 -int main()
1.107 -{
1.108 - typedef SmartGraph Graph;
1.109 - typedef Graph::NodeIt NodeIt;
1.110 - typedef Graph::OutEdgeIt OutEdgeIt;
1.111 - typedef Graph::EdgeIt EdgeIt;
1.112 -
1.113 - Graph G;
1.114 -
1.115 - for(int i=0;i<3;i++) G.addNode();
1.116 -
1.117 - for(NodeIt n(G);n!=INVALID;++n)
1.118 - for(NodeIt m(G);m!=INVALID;++m) if(n!=m)
1.119 - G.addEdge(n,m);
1.120 -
1.121 - //for(OutEdgeIt e(G,NodeIt(G));G.valid(e);G.next(e))
1.122 -
1.123 - Graph::EdgeMap<int> tem(G);
1.124 - typedef BoolIterMap<Graph::EdgeMap<int> > BoolIterEdgeMap;
1.125 - BoolIterEdgeMap map(tem);
1.126 -
1.127 - bool b=true;
1.128 -
1.129 - for(EdgeIt e(G);e!=INVALID;++e) {map[e]=b;b=!b;}
1.130 -
1.131 - std::cout << true << '\n';
1.132 -
1.133 - for(EdgeIt e(G);e!=INVALID;++e)
1.134 - std::cout << G.id(G.source(e)) << "->" << G.id(G.target(e))
1.135 - << ": " << map[e] << '\n';
1.136 - std::cout << "True Edges:\n";
1.137 - for(BoolIterEdgeMap::TrueIt i(map);i!=INVALID;++i)
1.138 - std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
1.139 - std::cout << "False Edges:\n";
1.140 - for(BoolIterEdgeMap::FalseIt i(map);i!=INVALID;++i)
1.141 - std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
1.142 -}
1.143 -