$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
edge.hh
1 // Copyright (C) 2008, 2009, 2010, 2013 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_EDGE_HH
28 # define MLN_UTIL_EDGE_HH
29 
33 
34 # include <iostream>
35 # include <mln/util/graph_ids.hh>
36 # include <mln/util/internal/edge_impl.hh>
37 # include <mln/core/concept/proxy.hh>
38 # include <mln/core/concept/site.hh>
39 # include <mln/core/internal/pseudo_site_base.hh>
40 
41 
42 namespace mln
43 {
44 
45  // Forward declaration.
46  namespace util { template<typename G> class edge; }
47 
48 
49 
51  template <typename E>
52  struct Edge
53  {
54  };
55 
56  template <>
57  struct Edge<void>
58  {
59  typedef Site<void> super;
60  };
61 
62 
63 
64  namespace util
65  {
66 
68  template <typename G>
69  class edge : public internal::edge_impl_<G>
70  {
71  public:
74 
76  typedef typename edge_id_t::value_t id_value_t;
77 
79  typedef edge_id_t id_t;
80 
82  typedef G graph_t;
83 
86  edge();
87  explicit edge(const G& g);
88  edge(const G& g, id_value_t id);
89  edge(const G& g, const edge_id_t& id);
91 
92 
96  bool is_valid() const;
98  void invalidate();
99 
101  edge_id_t id() const;
102 
104  void update_id(const edge_id_t& id);
105 
107  operator edge_id_t() const;
108 
110  const G& graph() const;
111 
113  void change_graph(const G& g);
115 
116 
121  v_other(const vertex_id_t& id_v) const;
123 
124 
128  vertex_id_t v1() const;
129 
131  vertex_id_t v2() const;
132 
134  size_t nmax_nbh_edges() const;
135 
137  edge_id_t ith_nbh_edge(unsigned i) const;
139 
140 
141  private:
142  G g_;
143  edge_id_t id_;
144  };
145 
146 
147  template <typename G>
148  std::ostream&
149  operator<<(std::ostream& ostr, const edge<G>& p);
150 
151  template <typename G>
152  bool
153  operator==(const edge<G>& lhs, const edge<G>& rhs);
154 
155  template <typename G>
156  bool
157  operator< (const edge<G>& lhs, const edge<G>& rhs);
158 
159  } // end of namespace mln::util
160 
161 
162 
163  namespace if_possible
164  {
165  template <typename G>
166  void change_target(mln::util::edge<G>& e, const G& new_target)
167  {
168  e.change_graph(new_target);
169  }
170 
171  } // end of namespace mln::if_possible
172 
173 
174  namespace internal
175  {
176 
181 
182 
183  template <typename G, typename E>
184  struct subject_impl< const util::edge<G>, E >
185  {
186  util::edge_id_t id() const;
187  const G& graph() const;
188 
190  v_other(const util::vertex_id_t& id_v) const;
191 
192  util::vertex_id_t v1() const;
193 
194  util::vertex_id_t v2() const;
195 
196  size_t nmax_nbh_edges() const;
197  util::edge_id_t ith_nbh_edge(unsigned i) const;
198 
199 
200  private:
201  const E& exact_() const;
202  };
203 
204  template <typename G, typename E>
205  struct subject_impl< util::edge<G>, E > :
206  subject_impl< const util::edge<G>, E >
207  {
208  void update_id(const util::edge_id_t& id);
209  void change_graph(const mlc_const(G)& g);
210  void invalidate();
211 
212  private:
213  E& exact_();
214  };
215 
217 
218  } // end of namespace mln::internal
219 
220 
221 
222 # ifndef MLN_INCLUDE_ONLY
223 
224  namespace util
225  {
226 
227  template <typename G>
228  inline
229  edge<G>::edge()
230  {
231  invalidate();
232  }
233 
234  template <typename G>
235  inline
236  edge<G>::edge(const G& g)
237  : g_(g)
238  {
239  invalidate();
240  }
241 
242  template <typename G>
243  inline
244  edge<G>::edge(const G& g, id_value_t id)
245  : g_(g), id_(id)
246  {
247  mln_precondition(g_.is_valid() && g.has_e(id));
248  }
249 
250  template <typename G>
251  inline
252  edge<G>::edge(const G& g, const edge_id_t& id)
253  : g_(g), id_(id)
254  {
255  mln_precondition(g_.is_valid() && g.has_e(id));
256  }
257 
258  template <typename G>
259  inline
260  edge_id_t
261  edge<G>::id() const
262  {
263  return id_;
264  }
265 
266  template <typename G>
267  inline
268  void
269  edge<G>::update_id(const edge_id_t& id)
270  {
271  id_ = id;
272  }
273 
274  template <typename G>
275  inline
276  edge<G>::operator edge_id_t() const
277  {
278  return id_;
279  }
280 
281  template <typename G>
282  inline
283  const G&
284  edge<G>::graph() const
285  {
286  return g_;
287  }
288 
289  template <typename G>
290  inline
291  void
292  edge<G>::change_graph(const G& g)
293  {
294  g_ = g;
295  }
296 
297  template <typename G>
298  inline
299  bool
300  edge<G>::is_valid() const
301  {
302  return g_.is_valid() && id_.is_valid() && g_.has_e(id_);
303  }
304 
305  template <typename G>
306  inline
307  void
308  edge<G>::invalidate()
309  {
310  id_.invalidate();
311  }
312 
313 
314  template <typename G>
315  inline
317  edge<G>::v_other(const vertex_id_t& id_v) const
318  {
319  mln_precondition(v1() == id_v || v2() == id_v);
320  return g_.v_other(id_, id_v);
321  }
322 
323  template <typename G>
324  inline
326  edge<G>::v1() const
327  {
328  mln_precondition(g_.has_e(id_));
329  return g_.v1(id_);
330  }
331 
332  template <typename G>
333  inline
335  edge<G>::v2() const
336  {
337  mln_precondition(g_.has_e(id_));
338  return g_.v2(id_);
339  }
340 
341  template <typename G>
342  inline
343  size_t
344  edge<G>::nmax_nbh_edges() const
345  {
346  mln_precondition(g_.has_e(id_));
347  return g_.e_nmax_nbh_edges(id_);
348  }
349 
350  template <typename G>
351  inline
352  edge_id_t
353  edge<G>::ith_nbh_edge(unsigned i) const
354  {
355  mln_precondition(g_.has_e(id_));
356  return g_.e_ith_nbh_edge(id_, i);
357  }
358 
359  template <typename G>
360  inline
361  std::ostream&
362  operator<<(std::ostream& ostr, const edge<G>& p)
363  {
364  return ostr << "(" << p.v1() << "," << p.v2() << ")";
365  }
366 
367  template <typename G>
368  inline
369  bool
370  operator==(const edge<G>& lhs, const edge<G>& rhs)
371  {
372  return lhs.id() == rhs.id()
373  && (lhs.graph().is_subgraph_of(rhs.graph())
374  || rhs.graph().is_subgraph_of(lhs.graph()));
375  }
376 
377  template <typename G>
378  inline
379  bool
380  operator<(const edge<G>& lhs, const edge<G>& rhs)
381  {
382  return lhs.id() < rhs.id();
383  }
384 
385  } // end of namespace mln::util
386 
387 
388 
389  namespace internal
390  {
391 
392  /*-----------------------------------------.
393  | subject_impl< const util::edge<G>, E >. |
394  `-----------------------------------------*/
395 
396  template <typename G, typename E>
397  inline
398  const E&
399  subject_impl< const util::edge<G>, E >::exact_() const
400  {
401  return internal::force_exact<const E>(*this);
402  }
403 
404  template <typename G, typename E>
405  inline
406  util::edge_id_t
407  subject_impl< const util::edge<G>, E >::id() const
408  {
409  return exact_().get_subject().id();
410  }
411 
412  template <typename G, typename E>
413  inline
414  const G&
415  subject_impl< const util::edge<G>, E >::graph() const
416  {
417  return exact_().get_subject().graph();
418  }
419 
420  template <typename G, typename E>
421  inline
422  util::vertex_id_t
423  subject_impl< const util::edge<G>, E >::v_other(const util::vertex_id_t& id_v) const
424  {
425  return exact_().get_subject().v_other(id_v);
426  }
427 
428  template <typename G, typename E>
429  inline
430  util::vertex_id_t
431  subject_impl< const util::edge<G>, E >::v1() const
432  {
433  return exact_().get_subject().v1();
434  }
435 
436  template <typename G, typename E>
437  inline
438  util::vertex_id_t
439  subject_impl< const util::edge<G>, E >::v2() const
440  {
441  return exact_().get_subject().v2();
442  }
443 
444  template <typename G, typename E>
445  inline
446  size_t
447  subject_impl< const util::edge<G>, E >::nmax_nbh_edges() const
448  {
449  return exact_().get_subject().nmax_nbh_edges();
450  }
451 
452  template <typename G, typename E>
453  inline
454  util::edge_id_t
455  subject_impl< const util::edge<G>, E >::ith_nbh_edge(unsigned i) const
456  {
457  return exact_().get_subject().ith_nbh_edge(i);
458  }
459 
460 
461  /*-----------------------------------.
462  | subject_impl< util::edge<G>, E >. |
463  `-----------------------------------*/
464 
465  template <typename G, typename E>
466  inline
467  void
468  subject_impl< util::edge<G>, E >::update_id(const util::edge_id_t& id)
469  {
470  return exact_().get_subject().update_id(id);
471  }
472 
473  template <typename G, typename E>
474  inline
475  void
476  subject_impl< util::edge<G>, E >::change_graph(const mlc_const(G)& g)
477  {
478  return exact_().get_subject().change_graph(g);
479  }
480 
481  template <typename G, typename E>
482  inline
483  void
484  subject_impl< util::edge<G>, E >::invalidate()
485  {
486  return exact_().get_subject().invalidate();
487  }
488 
489  } // end of namespace mln::internal
490 
491 # endif // ! MLN_INCLUDE_ONLY
492 
493 } // end of namespace mln
494 
495 
496 #endif // ! MLN_UTIL_EDGE_HH