COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/alpar/boolmap_iter.cc @ 1280:f2255b96c19c

Last change on this file since 1280:f2255b96c19c was 1280:f2255b96c19c, checked in by Alpar Juttner, 19 years ago

It works again

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