$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_first_search.hh
1 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_CANVAS_BROWSING_INTERNAL_GRAPH_FIRST_SEARCH_HH
27 # define MLN_CANVAS_BROWSING_INTERNAL_GRAPH_FIRST_SEARCH_HH
28 
35 
68 # include <deque>
69 # include <queue>
70 # include <stack>
71 
72 # include <mln/core/concept/iterator.hh>
73 # include <mln/core/concept/browsing.hh>
74 # include <mln/core/concept/graph.hh>
75 # include <mln/util/vertex.hh>
76 
77 
78 namespace mln
79 {
80 
81  namespace canvas
82  {
83 
84  namespace browsing
85  {
86 
87  namespace internal
88  {
89 
92  template < typename E,
93  template <typename T, typename Seq> class C >
94  class graph_first_search_t : public Browsing< E >
95  {
96  typedef util::vertex_id_t T_;
97  typedef C< T_, std::deque<T_> > container_t;
98  public:
99  template <typename G, typename F>
100  void operator()(const Graph<G>&, F& f) const;
101  };
102 
103 
104 
105 # ifndef MLN_INCLUDE_ONLY
106 
107 
109 
110  template <typename T>
111  inline
113  next(const std::queue<T>& queue)
114  {
115  return queue.front();
116  }
117 
118  template <typename T>
119  inline
121  next(const std::stack<T>& stack)
122  {
123  return stack.top();
124  }
125 
126  template <typename S>
127  inline
129  next(const S& stack)
130  {
131  (void) stack;
132  mln_assertion(0);
134  // mlc_abort(S)::check();
135  return 0;
136  }
137 
138 
139 
140 
141  template <typename E,
142  template <typename T, typename Seq> class C>
143  template <typename G, typename F>
144  inline
145  void
146  graph_first_search_t<E, C>::operator()(const Graph<G>& g_, F& f) const
147  {
148  mln_trace("canvas::browsing::internal::graph_first_search");
149 
150  const G& g = exact(g_);
151  mln_precondition(g.is_valid());
152 
153  f.init(g); // <--- init
154 
155  mln_vertex_iter(G) v(g);
156  for_all(v)
157  if (f.to_be_treated(v.id())) // <--- to_be_treated
158  {
159  container_t q;
160  q.push(v.id());
161  f.new_component_from_vertex(v.id()); // <--- new_component_from_vertex
162  while (! q.empty())
163  {
164  util::vertex<G> current_v = g.vertex(next(q));
165  f.process_vertex(current_v.id()); // <--- process_vertex
166  q.pop();
167  for (unsigned nv = 0; nv < current_v.nmax_nbh_vertices(); ++nv)
168  if (f.to_be_queued(current_v.ith_nbh_vertex(nv))) // <--- to_be_queued
169  {
170  f.added_to_queue(current_v.ith_nbh_vertex(nv)); // <--- added_to_queue
171  q.push(current_v.ith_nbh_vertex(nv));
172  }
173  }
174  f.next_component(); // <-- next_component
175  }
176  f.final(); // <-- final
177 
178  }
179 
180 # endif // ! MLN_INCLUDE_ONLY
181 
182  } // end of namespace mln::canvas::browsing::internal
183 
184  } // end of namespace mln::canvas::browsing
185 
186  } // end of namespace mln::canvas
187 
188 } // end of namespace mln
189 
190 
191 #endif // ! MLN_CANVAS_BROWSING_INTERNAL_GRAPH_FIRST_SEARCH_HH