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