Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
vertex.hh
1 // Copyright (C) 2008, 2009, 2010 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_;
139  vertex_id_t id_;
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 
170 
171  template <typename G, typename E>
172  struct subject_impl< const util::vertex<G>, E >
173  {
174 // Can't be provided since there is an ambiguity with the iterator's
175 // member.
176 //
177 // FIXME: Check the above statement again, as a naive test does
178 // not exhibit its conclusion.
179 // bool is_valid() const;
180 
181  const G& graph() const;
182  const util::vertex_id_t& id() const;
183 
184  util::vertex_id_t other(const util::edge_id_t& id_e) const;
185  util::edge_id_t ith_nbh_edge(unsigned i) const;
186  unsigned nmax_nbh_edges() const;
187  util::vertex_id_t ith_nbh_vertex(unsigned i) const;
188  unsigned nmax_nbh_vertices() const;
189  util::edge<G> edge_with(const util::vertex<G>& v) const;
190 
191  private:
192  const E& exact_() const;
193  };
194 
195  template <typename G, typename E>
196  struct subject_impl< util::vertex<G>, E > :
197  subject_impl< const util::vertex<G>, E >
198  {
199  void invalidate();
200  void change_graph(const G& g);
201  void update_id(const util::vertex_id_t& id);
202 
203  private:
204  E& exact_();
205  };
206 
208 
209  } // end of namespace mln::internal
210 
211 } // end of namespace mln
212 
213 
214 
215 
216 # ifndef MLN_INCLUDE_ONLY
217 
218 namespace mln
219 {
220 
221  namespace util
222  {
223 
224  template <typename G>
225  inline
227  {
228  invalidate();
229  }
230 
231  template <typename G>
232  inline
233  vertex<G>::vertex(const G& g)
234  : g_(g)
235  {
236  invalidate();
237  }
238 
239  template<typename G>
240  inline
241  vertex<G>::vertex(const G& g, const id_value_t& id)
242  : g_(g), id_(id)
243  {
244  mln_assertion(is_valid());
245  }
246 
247  template<typename G>
248  inline
249  vertex<G>::vertex(const G& g, const vertex_id_t& id)
250  : g_(g), id_(id)
251  {
252  mln_assertion(is_valid());
253  }
254 
255  template<typename G>
256  inline
257  bool
259  {
260  return id_ != mln_max(unsigned) && g_.is_valid() && g_.has_v(id_);
261  }
262 
263  template<typename G>
264  inline
265  void
267  {
268  id_ = mln_max(unsigned);
269  }
270 
271  template<typename G>
272  inline
274  vertex<G>::other(const edge_id_t& id_e) const
275  {
276  mln_precondition(g_.has_v(id_));
277  mln_precondition(g_.has_e(id_e));
278  mln_precondition(g_.v1(id_e) == id_ || g_.v2(id_e) == id_);
279  return g_.v_other(id_e, id_);
280  }
281 
282  template<typename G>
283  inline
284  edge_id_t
285  vertex<G>::ith_nbh_edge(unsigned i) const
286  {
287  mln_precondition(g_.has_v(id_));
288  return g_.v_ith_nbh_edge(id_, i);
289  }
290 
291  template<typename G>
292  inline
293  unsigned
295  {
296  mln_precondition(g_.has_v(id_));
297  return g_.v_nmax_nbh_edges(id_);
298  }
299 
300  template<typename G>
301  inline
303  vertex<G>::ith_nbh_vertex(unsigned i) const
304  {
305  mln_precondition(g_.has_v(id_));
306  return g_.v_ith_nbh_vertex(id_, i);
307  }
308 
309  template<typename G>
310  inline
311  unsigned
313  {
314  mln_precondition(g_.has_v(id_));
315  return g_.v_nmax_nbh_vertices(id_);
316  }
317 
318  template<typename G>
319  inline
320  edge<G>
322  {
323  mln_precondition(g_.has_v(id_));
324  mln_precondition(g_.has_v(v));
325  return g_.edge(*this, v);
326  }
327 
328  template<typename G>
329  inline
330  void
332  {
333  mln_precondition(g.is_valid());
334  g_ = g;
335  }
336 
337  template<typename G>
338  inline
339  void
341  {
342  id_ = id;
343  }
344 
345  template<typename G>
346  inline
347  const G&
349  {
350  return g_;
351  }
352 
353  template<typename G>
354  inline
355  const vertex_id_t&
357  {
358  return id_;
359  }
360 
361  template<typename G>
362  inline
364  {
365  return id_;
366  }
367 
368 
369  template <typename G>
370  inline
371  std::ostream&
372  operator<<(std::ostream& ostr, const vertex<G>& v)
373  {
374  return ostr << v.id();
375  }
376 
377  template<typename G>
378  inline
379  bool
380  operator==(const vertex<G>& v1, const vertex<G>& v2)
381  {
382  return v1.id() == v2.id()
383  && (v1.graph().is_subgraph_of(v2.graph())
384  || v2.graph().is_subgraph_of(v1.graph()));
385  }
386 
387  template<typename G>
388  inline
389  bool
390  operator<(const vertex<G>& lhs, const vertex<G>& rhs)
391  {
392  return lhs.id() < rhs.id();
393  }
394 
395  } // end of namespace mln::util
396 
397 
398  namespace internal
399  {
400 
401  /*-------------------------------------------.
402  | subject_impl< const util::vertex<G>, E >. |
403  `-------------------------------------------*/
404 
405  template <typename G, typename E>
406  inline
407  const E&
408  subject_impl< const util::vertex<G>, E >::exact_() const
409  {
410  return internal::force_exact<const E>(*this);
411  }
412 
413  template <typename G, typename E>
414  inline
415  const G&
416  subject_impl< const util::vertex<G>, E >::graph() const
417  {
418  return exact_().get_subject().graph();
419  }
420 
421  template <typename G, typename E>
422  inline
423  const util::vertex_id_t&
424  subject_impl< const util::vertex<G>, E >::id() const
425  {
426  return exact_().get_subject().id();
427  };
428 
429 
430 
431  template <typename G, typename E>
432  inline
434  subject_impl< const util::vertex<G>, E >::other(const util::edge_id_t& id_e) const
435  {
436  return exact_().get_subject().other(id_e);
437  }
438 
439  template <typename G, typename E>
440  inline
441  util::edge_id_t
442  subject_impl< const util::vertex<G>, E >::ith_nbh_edge(unsigned i) const
443  {
444  return exact_().get_subject().ith_nbh_edge(i);
445  }
446 
447  template <typename G, typename E>
448  inline
449  unsigned
450  subject_impl< const util::vertex<G>, E >::nmax_nbh_edges() const
451  {
452  return exact_().get_subject().nmax_nbh_edges();
453  }
454 
455  template <typename G, typename E>
456  inline
458  subject_impl< const util::vertex<G>, E >::ith_nbh_vertex(unsigned i) const
459  {
460  return exact_().get_subject().ith_nbh_vertex(i);
461  }
462 
463  template <typename G, typename E>
464  inline
465  unsigned
466  subject_impl< const util::vertex<G>, E >::nmax_nbh_vertices() const
467  {
468  return exact_().get_subject().nmax_nbh_vertices();
469  }
470 
471  template <typename G, typename E>
472  inline
473  util::edge<G>
474  subject_impl< const util::vertex<G>, E >::edge_with(const util::vertex<G>& v) const
475  {
476  return exact_().get_subject().edge_with(v);
477  }
478 
479 
480  /*-------------------------------------.
481  | subject_impl< util::vertex<G>, E >. |
482  `-------------------------------------*/
483 
484  template <typename G, typename E>
485  inline
486  E&
487  subject_impl< util::vertex<G>, E >::exact_()
488  {
489  return internal::force_exact<E>(*this);
490  }
491 
492  template <typename G, typename E>
493  inline
494  void
495  subject_impl< util::vertex<G>, E >::invalidate()
496  {
497  exact_().get_subject().invalidate();
498  }
499 
500  template <typename G, typename E>
501  inline
502  void
503  subject_impl< util::vertex<G>, E >::change_graph(const G& g)
504  {
505  exact_().get_subject().change_graph(g);
506  }
507 
508  template <typename G, typename E>
509  inline
510  void
511  subject_impl< util::vertex<G>, E >::update_id(const util::vertex_id_t& id)
512  {
513  exact_().get_subject().update_id(id);
514  };
515 
516  } // end of namespace mln::internal
517 
518 } // end of namespace mln
519 
520 # endif // ! MLN_INCLUDE_ONLY
521 
522 
523 #endif // ! MLN_UTIL_VERTEX_HH