Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
int_u.hh
1 // Copyright (C) 2007, 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_VALUE_INT_U_HH
28 # define MLN_VALUE_INT_U_HH
29 
33 
34 # include <mln/trait/all.hh> // FIXME!
35 
36 # include <mln/value/ops.hh>
37 
38 # include <mln/metal/math/pow.hh>
39 # include <mln/value/internal/value_like.hh>
40 # include <mln/value/internal/encoding.hh>
41 # include <mln/value/concept/integer.hh>
42 # include <mln/trait/value_.hh>
43 # include <mln/debug/format.hh>
44 
45 # include <mln/value/internal/make_generic_name.hh>
46 
47 
48 namespace mln
49 {
50 
51  namespace value
52  {
53  // Forward declaration.
54  template <unsigned n> struct int_u;
55  }
56 
57  namespace literal
58  {
59  // Forward declarations.
60  struct zero_t;
61  struct one_t;
62  }
63 
64 
65  namespace trait
66  {
67 
68  template <unsigned n>
69  struct set_precise_unary_< op::uminus, mln::value::int_u<n> >
70  {
71  typedef int ret;
72  };
73 
74 
75  template <unsigned n>
76  struct value_< mln::value::int_u<n> >
77  {
78  private:
79  typedef mln::value::int_u<n> self_;
80  typedef typename mln::value::internal::encoding_unsigned_<n>::ret enc_;
81  public:
82 
83  enum constants_ {
84  dim = 1,
85  nbits = n,
86  card = mln_value_card_from_(n)
87  };
88 
89  typedef trait::value::nature::integer nature;
90  typedef trait::value::kind::data kind;
91  typedef mln_value_quant_from_(card) quant;
92 
93  static const self_ min() { return 0; }
94  static const self_ max() { return mlc_pow_int(2, n) - 1; }
95  static const self_ epsilon() { return 0; }
96 
97  typedef unsigned comp;
98 
99  typedef float sum;
100 
101  static const char* name()
102  {
103  static std::string
104  s = mln::value::internal::make_generic_name("int_u", n);
105  return s.c_str();
106  }
107 
108  };
109 
110  } // end of namespace mln::trait
111 
112 
113  namespace convert
114  {
115 
116  namespace over_load
117  {
118 
119  // int_u -> unsigned.
120  template <unsigned n>
121  void
122  from_to_(const value::int_u<n>& from, unsigned& to_);
123 
124 
125  // int_u -> bool.
126  template <unsigned n>
127  void
128  from_to_(const value::int_u<n>& from, bool& to_);
129 
130 
131  // int_u -> float.
132  template <unsigned n>
133  void
134  from_to_(const value::int_u<n>& from, float& to_);
135 
136 
137  // int_u -> double.
138  template <unsigned n>
139  void
140  from_to_(const value::int_u<n>& from, double& to_);
141 
142 
143  } // end of namespace mln::convert::over_load
144 
145  } // end of namespace mln::convert
146 
147 
148 
149  namespace value
150  {
151 
155  template <unsigned n>
156  struct int_u
157  :
158  public Integer< int_u<n> >,
159 
160  public internal::value_like_< unsigned, // Equivalent.
161  typename internal::encoding_unsigned_<n>::ret, // Enc.
162  int, // Interoperation.
163  int_u<n> > // Exact.
164  {
165  protected:
167  typedef typename internal::encoding_unsigned_<n>::ret enc_;
168 
169  public:
170 
172  int_u();
173 
175  int_u(int i);
176 
178  int_u(const mln::literal::zero_t&);
179  int_u& operator=(const mln::literal::zero_t&);
180  int_u(const mln::literal::one_t&);
181  int_u& operator=(const mln::literal::one_t&);
183 
185  operator unsigned() const;
186 
188  int operator-() const;
189 
191  int_u<n>& operator=(int i);
192 
194  int_u<n> next() const;
195  };
196 
197 
198  // Safety.
199  template <> struct int_u<0>;
200  template <> struct int_u<1>;
201 
202 
203 
211  template <unsigned n>
212  std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i);
213 
214 
215  // FIXME: Doc!
216  template <unsigned n>
217  std::istream& operator>>(std::istream& istr, int_u<n>& i);
218 
219  } // end of namespace mln::value
220 
221 # ifndef MLN_INCLUDE_ONLY
222 
223  namespace convert
224  {
225 
226  namespace over_load
227  {
228 
229  // int_u -> unsigned.
230  template <unsigned n>
231  inline
232  void
233  from_to_(const value::int_u<n>& from, unsigned& to_)
234  {
235  to_ = from;
236  }
237 
238  // int_u -> bool.
239  template <unsigned n>
240  inline
241  void
242  from_to_(const value::int_u<n>& from, bool& to_)
243  {
244  to_ = (from != 0u);
245  }
246 
247  // int_u -> float.
248  template <unsigned n>
249  inline
250  void
251  from_to_(const value::int_u<n>& from, float& to_)
252  {
253  to_ = static_cast<float>(from);
254  }
255 
256  // int_u -> double.
257  template <unsigned n>
258  inline
259  void
260  from_to_(const value::int_u<n>& from, double& to_)
261  {
262  to_ = static_cast<double>(from);
263  }
264 
265 
266  } // end of namespace mln::convert::over_load
267 
268  } // end of namespace mln::convert
269 
270 
271  namespace value
272  {
273 
274  template <unsigned n>
275  inline
277  {
278  }
279 
280  template <unsigned n>
281  inline
283  {
284  mln_precondition(i >= 0);
285  mln_precondition(unsigned(i) <= mln_max(enc_));
286  this->v_ = static_cast<enc_>(i);
287  }
288 
289  template <unsigned n>
290  inline
292  {
293  this->v_ = 0;
294  }
295 
296  template <unsigned n>
297  inline
298  int_u<n>&
300  {
301  this->v_ = 0;
302  return *this;
303  }
304 
305  template <unsigned n>
306  inline
308  {
309  this->v_ = 1;
310  }
311 
312  template <unsigned n>
313  inline
314  int_u<n>&
315  int_u<n>::operator=(const mln::literal::one_t&)
316  {
317  this->v_ = 1;
318  return *this;
319  }
320 
321  template <unsigned n>
322  inline
323  int_u<n>::operator unsigned() const
324  {
325  return this->v_;
326  }
327 
328  template <unsigned n>
329  inline
330  int
332  {
333  return - int(this->v_);
334  }
335 
336  template <unsigned n>
337  inline
338  int_u<n>&
340  {
341  mln_precondition(i >= 0);
342  mln_precondition(unsigned(i) <= mln_max(enc_));
343  this->v_ = static_cast<enc_>(i);
344  return *this;
345  }
346 
347  template <unsigned n>
348  inline
349  int_u<n>
351  {
352  return this->v_ + 1;
353  }
354 
355  template <unsigned n>
356  inline
357  std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i)
358  {
359  // FIXME: This code could be factored for almost every Value<*>...
360  return ostr << debug::format(i.to_equiv()); // FIXME: is to_equiv OK?
361  }
362 
363  template <unsigned n>
364  inline
365  std::istream& operator>>(std::istream& istr, int_u<n>& i)
366  {
367  return istr >> i.handle_();
368  }
369 
370  } // end of namespace mln::value
371 
372 # endif // ! MLN_INCLUDE_ONLY
373 
374 } // end of namespace mln
375 
376 
377 #endif // ! MLN_VALUE_INT_U_HH
378