COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/alpar/boolmap_iter.cc @ 1271:40e5d0d44a65

Last change on this file since 1271:40e5d0d44a65 was 987:87f7c54892df, checked in by Alpar Juttner, 20 years ago

Naming changes:

File size: 2.9 KB
RevLine 
[348]1#include <iostream>
2#include <vector>
3#include <smart_graph.h>
4
[921]5using namespace lemon;
[348]6
7///\todo This is only a static map!
8///
9template<class GG>
10class BoolIterEdgeMap
11{
12public:
13  typedef GG Graph;
14  typedef typename GG::Edge Edge;
15 
[987]16  typedef Edge Key;
17  typedef bool Value;
[348]18 
19  friend class RefType;
20  friend class FalseIterator;
21  friend class TrueIterator;
22
23private:
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 
41public:
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   
[987]70    operator Value() const
[348]71    {
72      return M.isTrue(e);
73     
74    }
[987]75    Value operator = (Value v) const
[348]76    {
77      if(v) M.setTrue(e);
78      else M.setFalse(e);
79      return v;
80    }
81  };
82 
83public:
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
96int 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))
[986]122    std::cout << G.id(G.source(e)) << "->" << G.id(G.target(e))
[348]123      << ": " << map[e] << '\n';
124  std::cout << "True Edges:\n";
125  for(BoolIterEdgeMap<Graph>::TrueIterator i(map);i;++i)
[986]126    std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
[348]127  std::cout << "False Edges:\n";
128  for(BoolIterEdgeMap<Graph>::FalseIterator i(map);i;++i)
[986]129    std::cout << G.id(G.source(i)) << "->" << G.id(G.target(i)) << '\n';
[348]130}
131
Note: See TracBrowser for help on using the repository browser.