test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
graphwalker.h
Go to the documentation of this file.
1 #ifndef x_graphwalker_h
2 #define x_graphwalker_h
3 
5 
6 #include <vector>
7 #include <queue>
8 
9 //#include <iostream> // debug
10 
12 template <class N, class E>
14 {
15 public:
17 
19 
20  typedef typename graph<N,E>::edge_type edge_type;
21 
22  typedef typename graph<N,E>::edge_list edge_list;
23 
25 
27 
28  // only a const-edge_range!
29  typedef typename std::pair<const_edge_iterator, const_edge_iterator> edge_range;
30 
31  //typedef std::pair<edge_range,edge_range> stacked_element;
32 
33  typedef std::vector<edge_range> stack_type;
34  typedef std::queue<edge_type> bfs_type;
35 
36  typedef bool /*std::pair<const N &, bool>*/ result_type;
37 
39 
40 
41 public:
43  graphwalker(const graph<N,E> &);
44 
46  graphwalker(const graph<N,E> &, const N & );
47 
48  // operations
49 
51 
53 
55 
56  result_type next();
57 
58  inline value_type current() const;
59 
61  value_type current_bfs() const;
62 
63  void reset();
64 
65  const stack_type & stack() const { return stack_;}
66 
67 protected:
68  // stack_.back().first corresponds to index of the current node!
69  stack_type stack_; // hierarchical stack used in navigation
70  bfs_type queue_; // breath first search queue
71  edge_list root_; // root of the walker
72  //std::vector<N> rootCandidates_;
73  const graph<N,E> & graph_;
74  //jklsdfjklsdfkljsdfakjl;
75 private:
76  graphwalker();
77 
78 };
79 
80 
81 
82 template<class N, class E>
84  : graph_(g)
85 { // complexity = (no nodes) * (no edges)
86  graph_.findRoots(root_);
87  stack_.push_back(edge_range(root_.begin(),root_.end()));
88  if (root_.size()) {
89  queue_.push(root_[0]);
90  }
91 }
92 
93 
94 template<class N, class E>
96  : graph_(g)
97 {
98  index_result rr = graph_.nodeIndex(root);
99  if (!rr.second) // no such root node, no walker can be created!
100  throw root;
101 
102  root_.push_back(edge_type(rr.first, 0));
103  stack_.push_back(edge_range(root_.begin(),root_.end()));
104  queue_.push(root_[0]);
105 }
106 
107 
108 template<class N, class E>
110 {
111  const edge_range & er = stack_.back();
112 /*
113  const N & n = graph_.nodeData(er.first->first);
114  const E & e = er.first->first;
115  return value_type(n,e);
116 */
117  return value_type(graph_.nodeData(er.first->first), graph_.edgeData(er.first->second));
118 }
119 
120 
121 template<class N, class E>
123 {
124  const edge_type & e = queue_.front();
125  return value_type(graph_.nodeData(e.first), graph_.edgeData(e.second));
126 }
127 
128 
129 template<class N, class E>
131 {
132  //std::cout << "graphwalker::reset" << std::endl;
133  stack_.clear();
134  stack_.push_back(edge_range(root_.begin(),root_.end()));
135  queue_.clear();
136  if (root_.size()) {
137  queue_.push(root_[0]);
138  }
139 }
140 
141 
142 template<class N, class E>
144 {
145  result_type result = false;
146  const edge_range & adjEdges
147  = graph_.edges(stack_.back().first->first);
148  if (adjEdges.first != adjEdges.second) {
149  stack_.push_back(adjEdges);
150  result = true;
151  }
152  return result;
153 }
154 
155 
156 template<class N, class E>
158 {
159  result_type result = false;
160  //if (stack_.size() > 1) { only if single-root should be enforced ...
161  edge_range & siblings = stack_.back();
162  if (siblings.first != (siblings.second - 1) ) {
163  ++siblings.first;
164  result = true;
165  }
166  //}
167  return result;
168 }
169 
170 
171 template<class N, class E>
173 {
174  //std::cout << "graphwalker::parent()" << std::endl;
175  result_type result = false;
176  if (stack_.size()>1) {
177  stack_.pop_back();
178  result = true;
179  }
180  return result;
181 }
182 
183 
184 template<class N, class E>
186 {
187  result_type result = false;
188  if (firstChild()) {
189  result = true;
190  }
191  else if(stack_.size()>1 && nextSibling()) {
192  result = true;
193  }
194  else {
195  while(parent()) {
196  if(stack_.size()>1 && nextSibling()) {
197  result = true;
198  break;
199  }
200  }
201  }
202  return result;
203 }
204 
205 template<class N, class E>
207 {
208  result_type result(false);
209  if (!queue_.empty()) {
210  const edge_type & e = queue_.front();
211  const edge_range & er = graph_.edges(e.first);
212  const_edge_iterator it(er.first), ed(er.second);
213  for (; it != ed; ++it) {
214  queue_.push(*it);
215  }
216  queue_.pop();
217  if (!queue_.empty()) {
218  result=true;
219  }
220  }
221  return result;
222 }
223 #endif
stack_type stack_
Definition: graphwalker.h:69
graph< N, E >::edge_iterator edge_iterator
Definition: graphwalker.h:24
const graph< N, E > & graph_
Definition: graphwalker.h:73
graph< N, E >::index_type index_type
Definition: graphwalker.h:16
std::pair< index_type, bool > index_result
Definition: adjgraph.h:142
result_type parent()
Definition: graphwalker.h:172
result_type next_bfs()
Definition: graphwalker.h:206
bool result_type
Definition: graphwalker.h:36
graph< N, E >::const_edge_iterator const_edge_iterator
Definition: graphwalker.h:26
std::vector< double >::size_type index_type
Definition: adjgraph.h:15
const stack_type & stack() const
Definition: graphwalker.h:65
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
value_type current_bfs() const
Definition: graphwalker.h:122
std::pair< index_type, index_type > edge_type
Definition: adjgraph.h:17
tuple result
Definition: mps_fire.py:83
graph< N, E >::edge_list edge_list
Definition: graphwalker.h:22
std::vector< edge_type > edge_list
Definition: adjgraph.h:19
std::queue< edge_type > bfs_type
Definition: graphwalker.h:34
bfs_type queue_
Definition: graphwalker.h:70
graph< N, E >::value_type value_type
Definition: graphwalker.h:38
std::vector< edge_range > stack_type
Definition: graphwalker.h:33
void reset()
Definition: graphwalker.h:130
std::pair< const_edge_iterator, const_edge_iterator > edge_range
Definition: graphwalker.h:29
result_type nextSibling()
Definition: graphwalker.h:157
result_type next()
Definition: graphwalker.h:185
Definition: adjgraph.h:12
#define N
Definition: blowfish.cc:9
edge_list::iterator edge_iterator
Definition: adjgraph.h:134
result_type firstChild()
Definition: graphwalker.h:143
value_type current() const
Definition: graphwalker.h:109
graph< N, E >::edge_type edge_type
Definition: graphwalker.h:20
edge_list::const_iterator const_edge_iterator
Definition: adjgraph.h:136
static const char root_[]
graph< N, E >::index_result index_result
Definition: graphwalker.h:18
edge_list root_
Definition: graphwalker.h:71