$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph-iter.cc
1 #include <mln/core/concept/function.hh>
2 #include <mln/util/graph.hh>
3 
4 #include <vector>
5 
6 #include <doc/tools/sample_utils.hh>
7 
8 struct viota_t : public mln::Function_v2v< viota_t >
9 {
10  typedef unsigned result;
11 
12  viota_t(unsigned size)
13  {
14  v_.resize(size);
15  for(unsigned i = 0; i < size; ++i)
16  v_[i] = 10 + i;
17  }
18 
19  unsigned
20  operator()(const mln::util::vertex<mln::util::graph>& v) const
21  {
22  return v_[v.id()];
23  }
24 
25  protected:
26  std::vector<result> v_;
27 };
28 
29 int main()
30 {
31  using namespace mln;
32 
33  util::graph g;
34 
35  for (unsigned i = 0; i < 5; ++i)
36  g.add_vertex(); // Add vertex 'i';
37 
38  g.add_edge(0, 1); // Associated to edge 0.
39  g.add_edge(1, 2); // Associated to edge 1.
40  g.add_edge(1, 3); // Associated to edge 2.
41  g.add_edge(3, 4); // Associated to edge 3.
42  g.add_edge(4, 2); // Associated to edge 4.
43 
44 
45 
47  // \{
48  // Function which maps sites to data.
49  viota_t viota(g.v_nmax());
50 
51  // Iterator on vertices.
52  mln_vertex_iter_(util::graph) v(g);
53 
54  // Prints each vertex and its associated value.
55  for_all(v)
56  std::cout << v << " : " << viota(v) << std::endl;
57  // \}
58  doc::end_output();
59 
60 
61  {
63  // \{
64  // Iterator on vertices.
65  mln_vertex_iter_(util::graph) v(g);
66 
67  // Iterator on v's edges.
68  mln_vertex_nbh_edge_iter_(util::graph) e(v);
69 
70  // Prints the graph
71  // List all edges for each vertex.
72  for_all(v)
73  {
74  std::cout << v << " : ";
75  for_all(e)
76  std::cout << e << " ";
77  std::cout << std::endl;
78  }
79  // \}
80  doc::end_output();
81  }
82 
83 
84 
85  {
87  // \{
88  // Iterator on edges.
89  mln_edge_iter_(util::graph) e(g);
90 
91  // Iterator on edges adjacent to e.
92  mln_edge_nbh_edge_iter_(util::graph) ne(e);
93 
94  // Prints the graph
95  // List all adjacent edges for each edge.
96  for_all(e)
97  {
98  std::cout << e << " : ";
99  for_all(ne)
100  std::cout << ne << " ";
101  std::cout << std::endl;
102  }
103  // \}
104  doc::end_output();
105  }
106 
107 
108 
109  {
111  // \{
112  // Iterator on vertices.
113  mln_vertex_iter_(util::graph) v(g);
114 
115  // Iterator on vertices adjacent to v.
116  mln_vertex_nbh_vertex_iter_(util::graph) nv(v);
117 
118  // Prints the graph
119  // List all adjacent edges for each edge.
120  for_all(v)
121  {
122  std::cout << v << " : ";
123  for_all(nv)
124  std::cout << nv << " ";
125  std::cout << std::endl;
126  }
127  // \}
128  doc::end_output();
129  }
130 }