$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_nbh_iter_base.hh
1 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_UTIL_INTERNAL_GRAPH_NBH_ITER_BASE_HH
27 # define MLN_UTIL_INTERNAL_GRAPH_NBH_ITER_BASE_HH
28 
29 # include <mln/core/concept/proxy.hh>
30 
34 
35 namespace mln
36 {
37 
38  // Forward declaration.
39  template <typename S> class p_indexed_psite;
40 
41  namespace internal
42  {
43 
44  template <typename G, typename C, typename Elt, typename E>
46  : public Proxy< E >,
47  public internal::proxy_impl< const Elt&, E >
48  {
49  public:
50 
54  bool is_valid() const;
56  void invalidate();
57 
59  void start();
60 
62  void next();
63 
66  unsigned index() const;
67 
69  typename Elt::id_t id() const;
70 
72  operator typename Elt::id_t() const;
73 
76  operator typename Elt::id_value_t() const;
77 
79  const C& center() const;
80 
82  template <typename S>
83  void center_at(const p_indexed_psite<S>& c);
84 
85  template <typename C2>
86  void center_at(const C2& c);
87 
89  void change_target(const G& g);
90 
92  const Elt& element() const;
93 
95  const Elt& elt_hook_() const;
96 
98 
102  const Elt& subj_();
104 
105  protected:
109  template <typename C2>
110  nbh_iterator_base(const C2& c);
112 
113  const C* c_; // Center
114  Elt elt_;
115  unsigned i_;
116  };
117 
118 # ifndef MLN_INCLUDE_ONLY
119 
120  template <typename G, typename C, typename Elt, typename E>
121  inline
123  {
124  }
125 
126  template <typename G, typename C, typename Elt, typename E>
127  template <typename C2>
128  inline
130  : elt_(c.graph()), i_(0)
131  {
132  //FIXME: Check if typeof(c.graph()) == G
133  center_at(c);
134  }
135 
136  template <typename G, typename C, typename Elt, typename E>
137  inline
138  bool
139  nbh_iterator_base<G,C,Elt,E>::is_valid() const
140  {
141  return exact(this)->is_valid_();
142  }
143 
144  template <typename G, typename C, typename Elt, typename E>
145  inline
146  void
147  nbh_iterator_base<G,C,Elt,E>::invalidate()
148  {
149  i_ = mln_max(unsigned);
150  }
151 
152  template <typename G, typename C, typename Elt, typename E>
153  inline
154  void
155  nbh_iterator_base<G,C,Elt,E>::start()
156  {
157  i_ = exact(this)->start_id_();
158  if (is_valid())
159  exact(this)->update_();
160  }
161 
162  template <typename G, typename C, typename Elt, typename E>
163  inline
164  void
165  nbh_iterator_base<G,C,Elt,E>::next()
166  {
167  mln_precondition(is_valid());
168  mln_precondition(c_->is_valid());
169 
170  i_ = exact(this)->next_id_();
171  if (is_valid())
172  exact(this)->update_();
173  }
174 
175  template <typename G, typename C, typename Elt, typename E>
176  inline
177  unsigned
178  nbh_iterator_base<G,C,Elt,E>::index() const
179  {
180  return i_;
181  }
182 
183  template <typename G, typename C, typename Elt, typename E>
184  inline
185  typename Elt::id_t
186  nbh_iterator_base<G,C,Elt,E>::id() const
187  {
188  return elt_.id();
189  }
190 
191  template <typename G, typename C, typename Elt, typename E>
192  inline
193  nbh_iterator_base<G,C,Elt,E>::operator typename Elt::id_t() const
194  {
195  return elt_.id();
196  }
197 
198  template <typename G, typename C, typename Elt, typename E>
199  inline
200  nbh_iterator_base<G,C,Elt,E>::operator typename Elt::id_value_t() const
201  {
202  return elt_.id();
203  }
204 
205  template <typename G, typename C, typename Elt, typename E>
206  inline
207  const C&
208  nbh_iterator_base<G,C,Elt,E>::center() const
209  {
210  mln_precondition(c_ != 0);
211  return *c_;
212  }
213 
214  template <typename G, typename C, typename Elt, typename E>
215  inline
216  const Elt&
217  nbh_iterator_base<G,C,Elt,E>::subj_()
218  {
219  return elt_;
220  }
221 
222  template <typename G, typename C, typename Elt, typename E>
223  template <typename S>
224  inline
225  void
226  nbh_iterator_base<G,C,Elt,E>::center_at(const p_indexed_psite<S>& c)
227  {
228  //FIXME: p_indexed_psite does not have a conversion operator towards a
229  // p_edges/vertices_psite or a vertex/edge.
230  c_ = & static_cast< const C& >(c.unproxy_());
231 
232  //FIXME: c_->graph() may not be the right graph!
233  // The target may not be initialized before this call...
234  // See core/neighb.hh in center_at(), i.center_at().
235  elt_.change_graph(c_->graph());
236 
237  invalidate();
238  }
239 
240  template <typename G, typename C, typename Elt, typename E>
241  template <typename C2>
242  inline
243  void
244  nbh_iterator_base<G,C,Elt,E>::center_at(const C2& c)
245  {
246  mlc_converts_to(C2, const C&)::check();
247  c_ = & static_cast< const C& >(exact(c));
248 
249  //FIXME: c_->graph() may not be the right graph!
250  // The target may not be initialized before this call...
251  // See core/neighb.hh in center_at(), i.center_at().
252  elt_.change_graph(c_->graph());
253 
254  invalidate();
255  }
256 
257  template <typename G, typename C, typename Elt, typename E>
258  inline
259  void
260  nbh_iterator_base<G,C,Elt,E>::change_target(const G& g)
261  {
262  elt_.change_graph(g);
263  }
264 
265  template <typename G, typename C, typename Elt, typename E>
266  inline
267  const Elt&
268  nbh_iterator_base<G,C,Elt,E>::element() const
269  {
270  return elt_;
271  }
272 
273  template <typename G, typename C, typename Elt, typename E>
274  inline
275  const Elt&
276  nbh_iterator_base<G,C,Elt,E>::elt_hook_() const
277  {
278  return elt_;
279  }
280 
281 # endif // !MLN_INCLUDE_ONLY
282 
283  } // End of namespace mln::internal
284 
285 } // End of namespace mln
286 
287 
288 #endif // ! MLN_UTIL_INTERNAL_GRAPH_NBH_ITER_BASE_HH