$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vertex.hh
1 // Copyright (C) 2008, 2009, 2010, 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_VERTEX_HH
28 # define MLN_UTIL_VERTEX_HH
29 
30 # include <iostream>
31 # include <mln/util/graph_ids.hh>
32 # include <mln/util/internal/vertex_impl.hh>
33 # include <mln/core/concept/proxy.hh>
34 # include <mln/core/concept/site.hh>
35 # include <mln/util/graph_ids.hh>
36 # include <mln/util/edge.hh>
37 
41 
42 
43 
44 namespace mln
45 {
46 
47  // Forward declaration.
48  namespace util { template<typename G> class vertex; }
49  namespace util { template<typename G> class edge; }
50 
52  template <typename E>
53  struct Vertex
54  {
55  };
56 
57  template <>
58  struct Vertex<void>
59  {
60  typedef Site<void> super;
61  };
62 
63 
64 
65  namespace util
66  {
67 
69 
70  template<typename G>
71  class vertex
72  : public Site< vertex<G> >,
73  public internal::vertex_impl_<G>
74  {
75  public:
78 
80  typedef typename vertex_id_t::value_t id_value_t;
81 
83  typedef vertex_id_t id_t;
84 
86  typedef G graph_t;
87 
90  vertex();
91  explicit vertex(const G& g);
92  vertex(const G& g, const id_value_t& id);
93  vertex(const G& g, const vertex_id_t& id);
95 
97  bool is_valid() const;
99  void invalidate();
100 
102  vertex_id_t other(const edge_id_t& id_e) const;
103 
105  edge_id_t ith_nbh_edge(unsigned i) const;
106 
110  unsigned nmax_nbh_edges() const;
111 
113  vertex_id_t ith_nbh_vertex(unsigned i) const;
114 
116  unsigned nmax_nbh_vertices() const;
117 
119  edge<G> edge_with(const vertex<G>& v_id) const;
120 
122  void change_graph(const G& g);
123 
125  void update_id(const vertex_id_t& id);
126 
128  const G& graph() const;
129 
131  const vertex_id_t& id() const;
132 
135  operator vertex_id_t() const;
136 
137  protected:
138  G g_;
140  };
141 
142 
144  template <typename G>
145  std::ostream&
146  operator<<(std::ostream& ostr, const vertex<G>& v);
147 
150  template<typename G>
151  bool
152  operator==(const vertex<G>& v1, const vertex<G>& v2);
153 
154 
156  template<typename G>
157  bool
158  operator<(const vertex<G>& lhs, const vertex<G>& rhs);
159 
160 
161  } // end of namespace mln::util
162 
163 
164 
165  namespace internal
166  {
167 
172 
173 
174  template <typename G, typename E>
175  struct subject_impl< const util::vertex<G>, E >
176  {
177 // Can't be provided since there is an ambiguity with the iterator's
178 // member.
179 //
180 // FIXME: Check the above statement again, as a naive test does
181 // not exhibit its conclusion.
182 // bool is_valid() const;
183 
184  const G& graph() const;
185  const util::vertex_id_t& id() const;
186 
187  util::vertex_id_t other(const util::edge_id_t& id_e) const;
188  util::edge_id_t ith_nbh_edge(unsigned i) const;
189  unsigned nmax_nbh_edges() const;
190  util::vertex_id_t ith_nbh_vertex(unsigned i) const;
191  unsigned nmax_nbh_vertices() const;
192  util::edge<G> edge_with(const util::vertex<G>& v) const;
193 
194  private:
195  const E& exact_() const;
196  };
197 
198  template <typename G, typename E>
199  struct subject_impl< util::vertex<G>, E > :
200  subject_impl< const util::vertex<G>, E >
201  {
202  void invalidate();
203  void change_graph(const G& g);
204  void update_id(const util::vertex_id_t& id);
205 
206  private:
207  E& exact_();
208  };
209 
211 
212  } // end of namespace mln::internal
213 
214 } // end of namespace mln
215 
216 
217 
218 
219 # ifndef MLN_INCLUDE_ONLY
220 
221 namespace mln
222 {
223 
224  namespace util
225  {
226 
227  template <typename G>
228  inline
230  {
231  invalidate();
232  }
233 
234  template <typename G>
235  inline
236  vertex<G>::vertex(const G& g)
237  : g_(g)
238  {
239  invalidate();
240  }
241 
242  template<typename G>
243  inline
244  vertex<G>::vertex(const G& g, const id_value_t& id)
245  : g_(g), id_(id)
246  {
247  mln_assertion(is_valid());
248  }
249 
250  template<typename G>
251  inline
252  vertex<G>::vertex(const G& g, const vertex_id_t& id)
253  : g_(g), id_(id)
254  {
255  mln_assertion(is_valid());
256  }
257 
258  template<typename G>
259  inline
260  bool
261  vertex<G>::is_valid() const
262  {
263  return id_ != mln_max(unsigned) && g_.is_valid() && g_.has_v(id_);
264  }
265 
266  template<typename G>
267  inline
268  void
270  {
271  id_ = mln_max(unsigned);
272  }
273 
274  template<typename G>
275  inline
277  vertex<G>::other(const edge_id_t& id_e) const
278  {
279  mln_precondition(g_.has_v(id_));
280  mln_precondition(g_.has_e(id_e));
281  mln_precondition(g_.v1(id_e) == id_ || g_.v2(id_e) == id_);
282  return g_.v_other(id_e, id_);
283  }
284 
285  template<typename G>
286  inline
287  edge_id_t
288  vertex<G>::ith_nbh_edge(unsigned i) const
289  {
290  mln_precondition(g_.has_v(id_));
291  return g_.v_ith_nbh_edge(id_, i);
292  }
293 
294  template<typename G>
295  inline
296  unsigned
298  {
299  mln_precondition(g_.has_v(id_));
300  return g_.v_nmax_nbh_edges(id_);
301  }
302 
303  template<typename G>
304  inline
306  vertex<G>::ith_nbh_vertex(unsigned i) const
307  {
308  mln_precondition(g_.has_v(id_));
309  return g_.v_ith_nbh_vertex(id_, i);
310  }
311 
312  template<typename G>
313  inline
314  unsigned
316  {
317  mln_precondition(g_.has_v(id_));
318  return g_.v_nmax_nbh_vertices(id_);
319  }
320 
321  template<typename G>
322  inline
323  edge<G>
324  vertex<G>::edge_with(const vertex<G>& v) const
325  {
326  mln_precondition(g_.has_v(id_));
327  mln_precondition(g_.has_v(v));
328  return g_.edge(*this, v);
329  }
330 
331  template<typename G>
332  inline
333  void
334  vertex<G>::change_graph(const G& g)
335  {
336  mln_precondition(g.is_valid());
337  g_ = g;
338  }
339 
340  template<typename G>
341  inline
342  void
344  {
345  id_ = id;
346  }
347 
348  template<typename G>
349  inline
350  const G&
351  vertex<G>::graph() const
352  {
353  return g_;
354  }
355 
356  template<typename G>
357  inline
358  const vertex_id_t&
359  vertex<G>::id() const
360  {
361  return id_;
362  }
363 
364  template<typename G>
365  inline
366  vertex<G>::operator vertex_id_t() const
367  {
368  return id_;
369  }
370 
371 
372  template <typename G>
373  inline
374  std::ostream&
375  operator<<(std::ostream& ostr, const vertex<G>& v)
376  {
377  return ostr << v.id();
378  }
379 
380  template<typename G>
381  inline
382  bool
383  operator==(const vertex<G>& v1, const vertex<G>& v2)
384  {
385  return v1.id() == v2.id()
386  && (v1.graph().is_subgraph_of(v2.graph())
387  || v2.graph().is_subgraph_of(v1.graph()));
388  }
389 
390  template<typename G>
391  inline
392  bool
393  operator<(const vertex<G>& lhs, const vertex<G>& rhs)
394  {
395  return lhs.id() < rhs.id();
396  }
397 
398  } // end of namespace mln::util
399 
400 
401  namespace internal
402  {
403 
404  /*-------------------------------------------.
405  | subject_impl< const util::vertex<G>, E >. |
406  `-------------------------------------------*/
407 
408  template <typename G, typename E>
409  inline
410  const E&
411  subject_impl< const util::vertex<G>, E >::exact_() const
412  {
413  return internal::force_exact<const E>(*this);
414  }
415 
416  template <typename G, typename E>
417  inline
418  const G&
419  subject_impl< const util::vertex<G>, E >::graph() const
420  {
421  return exact_().get_subject().graph();
422  }
423 
424  template <typename G, typename E>
425  inline
426  const util::vertex_id_t&
428  {
429  return exact_().get_subject().id();
430  };
431 
432 
433 
434  template <typename G, typename E>
435  inline
437  subject_impl< const util::vertex<G>, E >::other(const util::edge_id_t& id_e) const
438  {
439  return exact_().get_subject().other(id_e);
440  }
441 
442  template <typename G, typename E>
443  inline
445  subject_impl< const util::vertex<G>, E >::ith_nbh_edge(unsigned i) const
446  {
447  return exact_().get_subject().ith_nbh_edge(i);
448  }
449 
450  template <typename G, typename E>
451  inline
452  unsigned
453  subject_impl< const util::vertex<G>, E >::nmax_nbh_edges() const
454  {
455  return exact_().get_subject().nmax_nbh_edges();
456  }
457 
458  template <typename G, typename E>
459  inline
461  subject_impl< const util::vertex<G>, E >::ith_nbh_vertex(unsigned i) const
462  {
463  return exact_().get_subject().ith_nbh_vertex(i);
464  }
465 
466  template <typename G, typename E>
467  inline
468  unsigned
469  subject_impl< const util::vertex<G>, E >::nmax_nbh_vertices() const
470  {
471  return exact_().get_subject().nmax_nbh_vertices();
472  }
473 
474  template <typename G, typename E>
475  inline
476  util::edge<G>
477  subject_impl< const util::vertex<G>, E >::edge_with(const util::vertex<G>& v) const
478  {
479  return exact_().get_subject().edge_with(v);
480  }
481 
482 
483  /*-------------------------------------.
484  | subject_impl< util::vertex<G>, E >. |
485  `-------------------------------------*/
486 
487  template <typename G, typename E>
488  inline
489  E&
490  subject_impl< util::vertex<G>, E >::exact_()
491  {
492  return internal::force_exact<E>(*this);
493  }
494 
495  template <typename G, typename E>
496  inline
497  void
498  subject_impl< util::vertex<G>, E >::invalidate()
499  {
500  exact_().get_subject().invalidate();
501  }
502 
503  template <typename G, typename E>
504  inline
505  void
506  subject_impl< util::vertex<G>, E >::change_graph(const G& g)
507  {
508  exact_().get_subject().change_graph(g);
509  }
510 
511  template <typename G, typename E>
512  inline
513  void
514  subject_impl< util::vertex<G>, E >::update_id(const util::vertex_id_t& id)
515  {
516  exact_().get_subject().update_id(id);
517  };
518 
519  } // end of namespace mln::internal
520 
521 } // end of namespace mln
522 
523 # endif // ! MLN_INCLUDE_ONLY
524 
525 
526 #endif // ! MLN_UTIL_VERTEX_HH