CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GraphWalker.h
Go to the documentation of this file.
1 #ifndef DATA_FORMATS_MATH_GRAPH_WALKER_H
2 #define DATA_FORMATS_MATH_GRAPH_WALKER_H
3 
5 #include <queue>
6 #include <vector>
7 
8 namespace math {
9 
11  template <class N, class E>
12  class GraphWalker {
13  public:
20 
21  // only a const-edge_range!
22  using edge_range = std::pair<const_edge_iterator, const_edge_iterator>;
23 
24  using stack_type = std::vector<edge_range>;
25  using bfs_type = std::queue<edge_type>;
26 
27  using result_type = bool;
29 
30  public:
32  GraphWalker(const Graph<N, E> &);
33 
35  GraphWalker(const Graph<N, E> &, const N &);
36 
37  GraphWalker() = delete;
38  // operations
39 
41 
43 
45 
46  result_type next();
47 
48  inline value_type current() const;
49 
51  value_type current_bfs() const;
52 
53  void reset();
54 
55  const stack_type &stack() const { return stack_; }
56 
57  protected:
58  // stack_.back().first corresponds to index of the current node!
59  stack_type stack_; // hierarchical stack used in navigation
60  bfs_type queue_; // breath first search queue
61  edge_list root_; // root of the walker
63  };
64 
65  template <class N, class E>
66  GraphWalker<N, E>::GraphWalker(const Graph<N, E> &g) : graph_(g) { // complexity = (no nodes) * (no edges)
67  graph_.findRoots(root_);
68  stack_.emplace_back(edge_range(root_.begin(), root_.end()));
69  if (!root_.empty()) {
70  queue_.push(root_[0]);
71  }
72  }
73 
74  template <class N, class E>
75  GraphWalker<N, E>::GraphWalker(const Graph<N, E> &g, const N &root) : graph_(g) {
76  index_result rr = graph_.nodeIndex(root);
77  if (!rr.second) // no such root node, no walker can be created!
78  throw root;
79 
80  root_.emplace_back(edge_type(rr.first, 0));
81  stack_.emplace_back(edge_range(root_.begin(), root_.end()));
82  queue_.push(root_[0]);
83  }
84 
85  template <class N, class E>
87  const edge_range &er = stack_.back();
88  return value_type(graph_.nodeData(er.first->first), graph_.edgeData(er.first->second));
89  }
90 
91  template <class N, class E>
93  const edge_type &e = queue_.front();
94  return value_type(graph_.nodeData(e.first), graph_.edgeData(e.second));
95  }
96 
97  template <class N, class E>
99  stack_.clear();
100  stack_.emplace_back(edge_range(root_.begin(), root_.end()));
101  queue_.clear();
102  if (root_.size()) {
103  queue_.push(root_[0]);
104  }
105  }
106 
107  template <class N, class E>
109  result_type result = false;
110  const edge_range &adjEdges = graph_.edges(stack_.back().first->first);
111  if (adjEdges.first != adjEdges.second) {
112  stack_.emplace_back(adjEdges);
113  result = true;
114  }
115  return result;
116  }
117 
118  template <class N, class E>
120  result_type result = false;
121  edge_range &siblings = stack_.back();
122  if (siblings.first != (siblings.second - 1)) {
123  ++siblings.first;
124  result = true;
125  }
126  return result;
127  }
128 
129  template <class N, class E>
131  result_type result = false;
132  if (stack_.size() > 1) {
133  stack_.pop_back();
134  result = true;
135  }
136  return result;
137  }
138 
139  template <class N, class E>
141  result_type result = false;
142  if (firstChild()) {
143  result = true;
144  } else if (stack_.size() > 1 && nextSibling()) {
145  result = true;
146  } else {
147  while (parent()) {
148  if (stack_.size() > 1 && nextSibling()) {
149  result = true;
150  break;
151  }
152  }
153  }
154  return result;
155  }
156 
157  template <class N, class E>
159  result_type result(false);
160  if (!queue_.empty()) {
161  const edge_type &e = queue_.front();
162  const edge_range &er = graph_.edges(e.first);
163  const_edge_iterator it(er.first), ed(er.second);
164  for (; it != ed; ++it) {
165  queue_.push(*it);
166  }
167  queue_.pop();
168  if (!queue_.empty()) {
169  result = true;
170  }
171  }
172  return result;
173  }
174 
175 } // namespace math
176 
177 #endif
const Graph< N, E > & graph_
Definition: GraphWalker.h:62
typename math::Graph< DDLogicalPart, DDPosData * >::edge_iterator edge_iterator
Definition: GraphWalker.h:18
std::vector< double >::size_type index_type
Definition: Graph.h:15
typename math::Graph< DDLogicalPart, DDPosData * >::index_result index_result
Definition: GraphWalker.h:15
std::pair< index_type, bool > index_result
Definition: Graph.h:117
typename math::Graph< DDLogicalPart, DDPosData * >::edge_type edge_type
Definition: GraphWalker.h:16
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
tuple result
Definition: mps_fire.py:311
result_type next_bfs()
Definition: GraphWalker.h:158
edge_list::iterator edge_iterator
Definition: Graph.h:113
stack_type stack_
Definition: GraphWalker.h:59
result_type parent()
Definition: GraphWalker.h:130
typename math::Graph< DDLogicalPart, DDPosData * >::value_type value_type
Definition: GraphWalker.h:28
result_type next()
Definition: GraphWalker.h:140
result_type firstChild()
Definition: GraphWalker.h:108
std::pair< const_edge_iterator, const_edge_iterator > edge_range
Definition: GraphWalker.h:22
value_type current_bfs() const
Definition: GraphWalker.h:92
result_type nextSibling()
Definition: GraphWalker.h:119
std::pair< index_type, index_type > edge_type
Definition: Graph.h:17
#define N
Definition: blowfish.cc:9
const stack_type & stack() const
Definition: GraphWalker.h:55
std::vector< edge_type > edge_list
Definition: Graph.h:19
typename math::Graph< DDLogicalPart, DDPosData * >::index_type index_type
Definition: GraphWalker.h:14
GraphWalker()=delete
edge_list root_
Definition: GraphWalker.h:61
static const char root_[]
edge_list::const_iterator const_edge_iterator
Definition: Graph.h:114
typename math::Graph< DDLogicalPart, DDPosData * >::edge_list edge_list
Definition: GraphWalker.h:17
value_type current() const
Definition: GraphWalker.h:86
typename math::Graph< DDLogicalPart, DDPosData * >::const_edge_iterator const_edge_iterator
Definition: GraphWalker.h:19