$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
line_graph.cc
1 // Copyright (C) 2009, 2011 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 
28 #include <mln/util/graph.hh>
29 #include <mln/util/line_graph.hh>
30 
31 int main()
32 {
33  using namespace mln;
34 
35  util::graph g;
36 
37  g.add_vertex(); // 0
38  g.add_vertex(); // 1
39  g.add_vertex(); // 2
40  g.add_vertex(); // 3
41  g.add_vertex(); // 4
42  g.add_vertex(); // 5
43  g.add_edge(0, 1);
44  g.add_edge(1, 0); // Not inserted twice
45  g.add_edge(0, 2);
46  g.add_edge(3, 4);
47  g.add_edge(4, 5);
48  g.add_edge(5, 4); // Not inserted twice
49  g.add_edge(5, 3);
50  g.add_edge(2, 1);
51 
53  LG lg(g);
54 
55  // Vertex and edge forward iterators.
56  {
57  unsigned i = 0;
58  mln_vertex_fwd_iter_(LG) v(lg);
59  for_all(v)
60  mln_assertion(i++ == v.id());
61  mln_assertion(i != 0);
62 
63  i = 0;
64  mln_edge_fwd_iter_(LG) e(lg);
65  for_all(e)
66  mln_assertion(i++ == e.id());
67  mln_assertion(i != 0);
68  }
69 
70  // Vertex and edge backward iterators.
71  {
72  unsigned i = lg.v_nmax() - 1;
73  mln_vertex_bkd_iter_(LG) v(lg);
74  for_all(v)
75  mln_assertion(i-- == v.id());
76  mln_assertion(i != lg.v_nmax() - 1);
77 
78  i = lg.e_nmax() - 1;
79  mln_edge_bkd_iter_(LG) e(lg);
80  for_all(e)
81  mln_assertion(i-- == e.id());
82  mln_assertion(i != lg.e_nmax() - 1);
83  }
84 
85  // Vertex and edge nbh forward iterators
86  {
87  mln_vertex_fwd_iter_(LG) v(lg);
88  mln_vertex_nbh_edge_fwd_iter_(LG) n(v);
89  for_all(v)
90  {
91  unsigned i = 0;
92  for_all(n)
93  mln_assertion(i++ == n.index());
94  mln_assertion(i != 0);
95  }
96  }
97 
98  // Vertex and edge nbh backward iterators.
99  {
100  mln_vertex_bkd_iter_(LG) v(lg);
101  mln_vertex_nbh_edge_bkd_iter_(LG) e(v);
102  for_all(v)
103  {
104  unsigned i = v.nmax_nbh_edges();
105  for_all(e)
106  mln_assertion(--i == e.index());
107  mln_assertion((v.nmax_nbh_edges() == 0 && i == 0)
108  || i != v.nmax_nbh_edges());
109  }
110  }
111  {
112  mln_edge_fwd_iter_(LG) e(lg);
113  mln_edge_nbh_edge_fwd_iter_(LG) n(e);
114  for_all(e)
115  {
116  unsigned i = 0;
117  for_all(n)
118  ++i;
119  // we check i == e.nmax_nbh_edges() - 2 since e is it's own
120  // neighboor and the iterator skip it.
121  mln_assertion((i == 0 && e.nmax_nbh_edges() < 2)
122  || i == e.nmax_nbh_edges() - 2);
123  }
124  }
125  {
126  mln_edge_bkd_iter_(LG) e(lg);
127  mln_edge_nbh_edge_bkd_iter_(LG) n(e);
128  for_all(e)
129  {
130  //std::cout << "== e.id() = " << e.id() << std::endl;
131  unsigned i = e.nmax_nbh_edges();
132  for_all(n)
133  --i;
134  // we check i == e.nmax_nbh_edges() - 2 since e is it's own
135  // neighboor and the iterator skip it.
136  mln_assertion((i == e.nmax_nbh_edges() && e.nmax_nbh_edges() < 2)
137  || i == 2);
138 
139  }
140  }
141  {
142  mln_vertex_fwd_iter_(LG) v(lg);
143  mln_vertex_nbh_vertex_fwd_iter_(LG) n(v);
144  for_all(v)
145  {
146  unsigned i = 0;
147  for_all(n)
148  ++i;
149  mln_assertion(i == v.nmax_nbh_vertices());
150  }
151  }
152  {
153  mln_vertex_bkd_iter_(LG) v(lg);
154  mln_vertex_nbh_vertex_bkd_iter_(LG) n(v);
155  for_all(v)
156  {
157  unsigned i = v.nmax_nbh_vertices();
158  for_all(n)
159  --i;
160  mln_assertion(i == 0);
161  }
162  }
163 }