$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_base.hh
1 // Copyright (C) 2007, 2008, 2009, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_UTIL_INTERNAL_GRAPH_BASE_HH
28 # define MLN_UTIL_INTERNAL_GRAPH_BASE_HH
29 
33 
34 # include <cstddef>
35 # include <algorithm>
36 # include <vector>
37 # include <set>
38 # include <iostream>
39 
40 # include <mln/core/concept/object.hh>
41 # include <mln/core/concept/graph.hh>
42 # include <mln/core/concept/proxy.hh>
43 # include <mln/core/internal/data.hh>
44 
45 # include <mln/util/edge.hh>
46 # include <mln/util/vertex.hh>
47 # include <mln/util/ord_pair.hh>
48 # include <mln/util/tracked_ptr.hh>
49 
50 
51 namespace mln
52 {
53 
54  namespace util
55  {
56 
57  /*-------------.
58  | Graph base. |
59  `-------------*/
60 
61  namespace internal
62  {
67  template<typename E>
68  class graph_base : public Graph<E>
69  {
70 
71  public:
76 
78  typedef std::vector<edge_id_t> vertex_data_t;
79 
82 
83  public:
87  const void* id() const;
89 
93  bool has(const util::vertex<E>& v) const;
94 
96 
100  bool has(const util::edge<E>& e) const;
102 
103 
107  vertex_id_t v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const;
108 
110  bool is_valid() const;
112  void invalidate();
113 
115 
116  // FIXME: We might want to externalize this in routine of
117  // namespace mln::debug.
121  void print_debug(std::ostream& ostr) const;
122 
125 
126  protected:
127 
130 
132  graph_base<E>();
133 
134  };
135 
136  } // end of namespace mln::util::internal
137 
138  } // End of namespace mln::util
139 
140  template <typename E>
141  bool
144 
145 # ifndef MLN_INCLUDE_ONLY
146 
147  namespace util
148  {
149 
150  namespace internal
151  {
152 
153  /*--------------------------------------------.
154  | Construction, assignments and destruction. |
155  `--------------------------------------------*/
156 
157  template<typename E>
158  inline
160  {
161  }
162 
163  /*-------------.
164  | Misc methods |
165  `-------------*/
166 
167  template<typename E>
168  inline
169  const void*
170  graph_base<E>::id() const
171  {
172  return static_cast<const void*>(data_.ptr_);
173  }
174 
175  /*-------------------------.
176  | Vertex and edges related |
177  `-------------------------*/
178 
179  template<typename E>
180  inline
182  graph_base<E>::v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const
183  {
184  const E *g = exact(this);
185  mln_precondition(g->has_e(id_e));
186  mln_precondition(g->has_v(id_v));
187  mln_precondition(g->v1(id_e) == id_v
188  || g->v2(id_e) == id_v);
189 
190  if (g->v1(id_e) == id_v)
191  return g->v2(id_e);
192  return g->v1(id_e);
193  }
194 
195  /*---------------.
196  | Vertex related |
197  `---------------*/
198 
199  template<typename E>
200  inline
201  bool
202  graph_base<E>::has(const util::vertex<E>& v) const
203  {
204  return exact(this)->has_v(v.id());
205  }
206 
207  /*--------------.
208  | Edges related |
209  `---------------*/
210 
211  template<typename E>
212  inline
213  bool
214  graph_base<E>::has(const util::edge<E>& e) const
215  {
216  return exact(this)->has_e(e.id());
217  }
218 
219 
220  template <typename E>
221  inline
222  bool
224  {
225  return data_ != 0;
226  }
227 
228  template <typename E>
229  inline
230  void
232  {
233  data_.clean_();
234  }
235 
236 
237  /*--------.
238  | Debug. |
239  `--------*/
240 
241  template<typename E>
242  inline
243  void
244  graph_base<E>::print_debug (std::ostream& ostr) const
245  {
246  const E *g = exact(this);
247 
248  ostr << "graph: " << std::endl;
249  for (unsigned v = 0; v < g->v_nmax(); ++v)
250  {
251  ostr << "vertex: " << v << std::endl << " -- adjacent vertices: ";
252  for (unsigned n = 0; n < g->v_nmax_nbh_vertices(v); ++n)
253  ostr << g->v_ith_nbh_vertex(v, n) << " ";
254  ostr << std::endl;
255  }
256  ostr << std::endl;
257 
258  ostr << "edges:" << std::endl;
259  for (unsigned i = 0; i < g->e_nmax(); ++i)
260  ostr << "edge " << i << ": ("
261  << g->v1(i) << ", "
262  << g->v2(i) << " )"
263  << std::endl;
264  }
265 
266  template<typename E>
267  inline
268  const util::tracked_ptr< mln::internal::data<E> >&
270  {
271  return data_;
272  }
273 
274  } // end of namespace mln::util::internal
275 
276  } // end of namespace mln::util
277 
278 # endif // ! MLN_INCLUDE_ONLY
279 
280  template <typename E>
281  inline
282  bool
285  {
286  return lhs.id() == rhs.id();
287  }
288 
289 } // end of namespace mln
290 
291 #endif // ! MLN_UTIL_INTERNAL_GRAPH_BASE_HH