$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
viter.hh
1 // Copyright (C) 2007, 2008, 2009, 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_VALUE_VITER_HH
28 # define MLN_VALUE_VITER_HH
29 
37 # include <mln/core/concept/value_iterator.hh>
38 # include <mln/core/concept/value_set.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace value
45  {
46 
47 
54  template <typename S>
55  struct fwd_viter_ : public Value_Iterator< fwd_viter_<S> >
56  {
58  typedef mln_value(S) value;
59 
61  fwd_viter_();
62 
64  fwd_viter_(const Value_Set<S>& s);
65 
66  void change_target(const S& s);
67 
69  bool is_valid() const;
70 
72  void invalidate();
73 
75  void start();
76 
78  void next_();
79 
81  operator mln_value(S) () const;
82 
84  unsigned index_() const;
85 
86  private:
87 
88  const S* s_;
89  unsigned i_;
90  };
91 
92 
93 
94 
101  template <typename S>
102  struct bkd_viter_ : public Value_Iterator< bkd_viter_<S> >
103  {
105  typedef mln_value(S) value;
106 
108  bkd_viter_();
109 
111  bkd_viter_(const Value_Set<S>& s);
112 
113  void change_target(const S& s);
114 
116  bool is_valid() const;
117 
119  void invalidate();
120 
122  void start();
123 
125  void next_();
126 
128  operator mln_value(S) () const;
129 
131  unsigned index_() const;
132 
133  private:
134 
135  const S* s_;
136  unsigned i_;
137  };
138 
139 
140 
141 # ifndef MLN_INCLUDE_ONLY
142 
143 
144  // fwd_viter_<S>
145 
146  template <typename S>
147  inline
149  : s_(0)
150  {
151  }
152 
153  template <typename S>
154  inline
156  {
157  change_target(exact(s));
158  }
159 
160  template <typename S>
161  inline
162  void
163  fwd_viter_<S>::change_target(const S& s)
164  {
165  s_ = &s;
166  invalidate();
167  }
168 
169  template <typename S>
170  inline
171  bool
173  {
174  return s_ != 0 && i_ < s_->nvalues();
175  }
176 
177  template <typename S>
178  inline
179  void
181  {
182  i_ = s_->nvalues();
183  }
184 
185  template <typename S>
186  inline
187  void
189  {
190  i_ = 0;
191  }
192 
193  template <typename S>
194  inline
195  void
197  {
198  ++i_;
199  }
200 
201  template <typename S>
202  inline
203  fwd_viter_<S>::operator mln_value(S) () const
204  {
205  mln_precondition(is_valid());
206  return (*s_)[i_];
207  }
208 
209  template <typename S>
210  inline
211  unsigned
212  fwd_viter_<S>::index_() const
213  {
214  return i_;
215  }
216 
217 
218  // bkd_viter_<S>
219 
220  template <typename S>
221  inline
223  : s_(0)
224  {
225  }
226 
227  template <typename S>
228  inline
229  bkd_viter_<S>::bkd_viter_(const Value_Set<S>& s)
230  {
231  change_target(exact(s));
232  }
233 
234  template <typename S>
235  inline
236  void
237  bkd_viter_<S>::change_target(const S& s)
238  {
239  s_ = &s;
240  invalidate();
241  }
242 
243  template <typename S>
244  inline
245  bool
247  {
248  return s_ != 0 && i_ != s_->nvalues();
249  }
250 
251  template <typename S>
252  inline
253  void
255  {
256  i_ = s_->nvalues();
257  }
258 
259  template <typename S>
260  inline
261  void
263  {
264  i_ = s_->nvalues() - 1;
265  }
266 
267  template <typename S>
268  inline
269  void
271  {
272  if (i_ == 0)
273  {
274  invalidate();
275  return;
276  }
277  --i_;
278  }
279 
280  template <typename S>
281  inline
282  bkd_viter_<S>::operator mln_value(S) () const
283  {
284  mln_precondition(is_valid());
285  return (*s_)[i_];
286  }
287 
288  template <typename S>
289  inline
290  unsigned
291  bkd_viter_<S>::index_() const
292  {
293  return i_;
294  }
295 
296 # endif // ! MLN_INCLUDE_ONLY
297 
298  } // end of namespace mln::value
299 
300 } // end of namespace mln
301 
302 
303 #endif // ! MLN_VALUE_VITER_HH