$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
p_vertices.hh
1 // Copyright (C) 2008, 2009, 2011 EPITA Research and Development Laboratory
2 // (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_CORE_SITE_SET_P_VERTICES_HH
28 # define MLN_CORE_SITE_SET_P_VERTICES_HH
29 
33 
34 # include <mln/core/concept/function.hh>
35 # include <mln/core/internal/site_set_base.hh>
36 # include <mln/core/site_set/p_graph_piter.hh>
37 # include <mln/core/site_set/p_vertices_psite.hh>
38 # include <mln/util/graph.hh>
39 # include <mln/util/internal/id2element.hh>
40 
41 
42 
43 namespace mln
44 {
45 
46  // Forward declaration.
47  template <typename G, typename F> class p_vertices;
48 
49 
50  namespace trait
51  {
52 
53  template <typename G, typename F>
54  struct site_set_< p_vertices<G,F> >
55  {
56  typedef trait::site_set::nsites::known nsites;
57  // FIXME: !
58  typedef trait::site_set::bbox::unknown bbox;
59  typedef trait::site_set::contents::fixed contents;
60  typedef trait::site_set::arity::unique arity;
61  };
62 
63  } // end of namespace mln::trait
64 
65 
69  //
70  template <typename G, typename F = util::internal::id2element<G,util::vertex<G> > >
71  class p_vertices
72  : public internal::site_set_base_< mln_result(F), p_vertices<G,F> >
73  {
74 
75  typedef p_vertices<G,F> self_;
77 
78  public:
79 
81  typedef G graph_t;
82 
84  typedef F fun_t;
85 
88 
89 
92 
93 
95  p_vertices();
96 
100  p_vertices(const Graph<G>& gr);
101 
105  p_vertices(const Graph<G>& gr, const Function<F>& f);
106 
111  template <typename F2>
112  p_vertices(const Graph<G>& gr, const Function<F2>& f);
114 
116  template <typename F2>
117  p_vertices(const p_vertices<G,F2>& other);
118 
121 
123  typedef mln_site(super_) element;
124 
126  typedef p_vertices_psite<G,F> psite;
127 
129  typedef p_graph_piter< self_, mln_vertex_fwd_iter(G) > fwd_piter;
130 
132  typedef p_graph_piter< self_, mln_vertex_bkd_iter(G) > bkd_piter;
133 
135  typedef fwd_piter piter;
136 
138 
139 
144  unsigned nsites() const;
145 
147  unsigned nvertices() const;
148 
150  bool is_valid() const;
151 
153  void invalidate();
154 
156  bool has(const psite& p) const;
157 
159  template <typename G2>
160  bool has(const util::vertex<G2>& v) const;
161 
165 
166  // FIXME: Dummy.
167  std::size_t memory_size() const;
168 
171  mln_result(F) operator()(const psite& p) const;
172  mln_result(F) operator()(const util::vertex<G>& p) const;
173  mln_result(F) operator()(unsigned id_v) const;
175 
179  const G& graph() const;
181  const F& function() const;
183 
184  private:
185  G g_;
186  F f_;
187  };
188 
189 
194  template <typename G, typename F>
195  bool
196  operator==(const p_vertices<G,F>& lhs, const p_vertices<G,F>& rhs);
197 
198 
199  /* FIXME: Extend the `ord' mechanism instead of this ill-defined
200  pseudo-order. */
201 
210  template <typename G, typename F>
211  bool
212  operator<=(const p_vertices<G,F>& lhs, const p_vertices<G,F>& rhs);
213 
214 
215 
216 # ifndef MLN_INCLUDE_ONLY
217 
218  template <typename G, typename F>
219  inline
221  {
222  }
223 
224  template <typename G, typename F>
225  inline
227  {
229  mlc_equal(F, F_REF)::check();
230 
231  mln_precondition(exact(g).is_valid());
232  g_ = exact(g);
234  }
235 
236  template <typename G, typename F>
237  inline
238  p_vertices<G,F>::p_vertices(const Graph<G>& g, const Function<F>& f)
239  {
240  mln_precondition(exact(g).is_valid());
241  g_ = exact(g);
242  f_ = exact(f);
243  }
244 
245  template <typename G, typename F>
246  template <typename F2>
247  inline
248  p_vertices<G,F>::p_vertices(const Graph<G>& g, const Function<F2>& f)
249  {
250  mln_precondition(exact(g).is_valid());
251  mlc_converts_to(F2,F)::check();
252 
253  g_ = exact(g);
254  convert::from_to(f, f_);
255  }
256 
257  template <typename G, typename F>
258  template <typename F2>
259  inline
260  p_vertices<G,F>::p_vertices(const p_vertices<G,F2>& other)
261  {
262  mln_precondition(other.is_valid());
263  mlc_converts_to(F2,F)::check();
264 
265  g_ = other.graph();
266  convert::from_to(other.function(), f_);
267  }
268 
269  template <typename G, typename F>
270  inline
271  unsigned
273  {
274  return nvertices();
275  }
276 
277  template <typename G, typename F>
278  inline
279  unsigned
281  {
282  return this->g_.v_nmax();
283  }
284 
285  template <typename G, typename F>
286  inline
287  bool
289  {
290  return g_.is_valid();
291  }
292 
293  template <typename G, typename F>
294  inline
295  void
297  {
298  g_.invalidate();
299  }
300 
301  template <typename G, typename F>
302  inline
303  bool
304  p_vertices<G,F>::has(const psite& p) const
305  {
306  mln_precondition(is_valid());
307  return has(p.v());
308  }
309 
310  template <typename G, typename F>
311  template <typename G2>
312  inline
313  bool
314  p_vertices<G,F>::has(const util::vertex<G2>& v) const
315  {
316  mln_precondition(is_valid());
317  return
318  // Check whether if the graph is 'compatible'.
319  v.graph().is_subgraph_of(g_) &&
320  g_.has(v) &&
321  // Check that the vertex id of P belongs to the range of valid
322  // vertex ids.
323  (v.is_valid());
324  }
325 
326 // template <typename G, typename F>
327 // inline
328 // bool
329 // p_vertices<G,F>::has(unsigned vertex_id) const
330 // {
331 // mln_precondition(is_valid());
332 // util::vertex<G> v(g_, vertex_id);
333 // return has(v);
334 // }
335 
336  template <typename G, typename F>
337  inline
338  std::size_t
340  {
341  // FIXME: Dummy; implement (see other site sets).
342  abort();
343  return 0;
344  }
345 
346  template <typename G, typename F>
347  inline
348  mln_result(F)
349  p_vertices<G,F>::operator()(const psite& p) const
350  {
351  mln_precondition(g_.has(p.v()));
352  return (*this)(p.v().id());
353  }
354 
355  template <typename G, typename F>
356  inline
357  mln_result(F)
358  p_vertices<G,F>::operator()(const util::vertex<G>& v) const
359  {
360  mln_precondition(g_.has_v(v));
361  return (*this)(v.id());
362  }
363 
364  template <typename G, typename F>
365  inline
366  mln_result(F)
367  p_vertices<G,F>::operator()(unsigned id_v) const
368  {
369  mln_precondition(g_.has_v(id_v));
370  return f_(id_v);
371  }
372 
373  template <typename G, typename F>
374  inline
375  const G&
376  p_vertices<G,F>::graph() const
377  {
378  mln_precondition(is_valid());
379  return g_;
380  }
381 
382  template <typename G, typename F>
383  inline
384  const F&
386  {
387  return f_;
388  }
389 
390  template <typename G, typename F>
391  bool
392  operator==(const p_vertices<G,F>& lhs, const p_vertices<G,F>& rhs)
393  {
394  return (lhs.graph()) == (rhs.graph());
395  }
396 
397  template <typename G, typename F>
398  bool
399  operator<=(const p_vertices<G,F>& lhs, const p_vertices<G,F>& rhs)
400  {
401  return lhs == rhs;
402  }
403 
404 # endif // ! MLN_INCLUDE_ONLY
405 
406 } // end of mln
407 
408 
409 #endif // ! MLN_CORE_SITE_SET_P_VERTICES_HH