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