$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
depth_first_search.cc
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 #include <iostream>
27 #include <mln/util/graph.hh>
28 #include <mln/util/array.hh>
29 
30 #include <mln/canvas/browsing/depth_first_search.hh>
31 
32 struct Functor
33 {
34  template <typename G> void init(const mln::Graph<G>& g)
35  {
36  deja_vu_.resize(exact(g).v_nmax());
37  deja_vu_.fill(false);
38  order.resize(0);
39  }
40 
41  bool to_be_treated(unsigned id)
42  {
43  return !deja_vu_[id];
44  }
45 
46  void new_component_from_vertex(unsigned id)
47  {
48  deja_vu_[id] = true;
49  }
50 
51  void process_vertex(unsigned id)
52  {
53  order.append(id);
54  }
55 
56  bool to_be_queued(unsigned id)
57  {
58  return !deja_vu_[id];
59  }
60 
61  void added_to_queue(unsigned id)
62  {
63  deja_vu_[id] = true;
64  }
65 
66  void next_component()
67  {
68  }
69 
70  void final()
71  {
72  }
73 
75  mln::util::array<bool> deja_vu_;
76 };
77 
78 int main()
79 {
80  using namespace mln;
81 
82  typedef util::graph G;
83  G g;
84  g.add_vertices(5);
85  g.add_edge(0,4);
86  g.add_edge(1,2);
87  g.add_edge(1,3);
88  g.add_edge(4,3);
89  g.add_edge(4,2);
90 
91  unsigned result[] = {0, 4, 2, 1, 3};
92  Functor f;
93 
95 
96  for (unsigned i = 0; i < f.order.size(); ++i)
97  mln_assertion(f.order[i] == result[i]);
98 }