Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
int_s.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_S_HH
28 # define MLN_VALUE_INT_S_HH
29 
33 
34 # include <mln/value/ops.hh>
35 
36 # include <mln/metal/math/pow.hh>
37 # include <mln/value/internal/value_like.hh>
38 # include <mln/value/concept/integer.hh>
39 # include <mln/value/internal/encoding.hh>
40 # include <mln/trait/value_.hh>
41 # include <mln/trait/all.hh>
42 # include <mln/debug/format.hh>
43 
44 
45 namespace mln
46 {
47 
48 
49  namespace value
50  {
52  template <unsigned n> struct int_s;
54  }
55 
56  namespace literal
57  {
59  struct zero_t;
60  struct one_t;
62  }
63 
64 
65 
66  namespace trait
67  {
68 
69  template <unsigned n>
70  struct value_< mln::value::int_s<n> >
71  {
72  private:
73  typedef mln::value::int_s<n> self_;
74  public:
75 
76  enum constants_ {
77  dim = 1,
78  nbits = n,
79  card = mln_value_card_from_(n) - 1
80  };
81 
82  typedef trait::value::nature::integer nature;
83  typedef trait::value::kind::data kind;
84  typedef mln_value_quant_from_(card) quant;
85 
86  static const self_ max() { return mln_value_card_from_(n) / 2 - 1; }
87  static const self_ min() { return - max(); }
88  static const self_ epsilon() { return 0; }
89 
90  typedef mln::value::int_s<n> comp;
91 
92  typedef float sum;
93 
94  static const char* name()
95  {
96  static std::string s = std::string("int_s").append(1, n + '0');
97  return s.c_str();
98  }
99 
100  };
101 
102  } // end of namespace mln::trait
103 
104 
105 
106  namespace value
107  {
108 
109 
114  template <unsigned n>
115  struct int_s
116  :
117  private metal::bool_<(n <= 32)>::check_t
118  ,
119  public Integer< int_s<n> >
120  ,
121  public internal::value_like_< int, // Equivalent.
122  typename internal::encoding_signed_<n>::ret, // Enc.
123  int, // Interoperation.
124  int_s<n> > // Exact.
125  {
127  int_s();
128 
130  int_s(int i);
131 
133  int_s(const mln::literal::zero_t&);
134  int_s& operator=(const mln::literal::zero_t&);
135  int_s(const mln::literal::one_t&);
136  int_s& operator=(const mln::literal::one_t&);
138 
140  operator int() const;
141 
143  int_s<n>& operator=(int i);
144 
146  static const int_s<n> zero;
147 
149  static const int_s<n> one;
150 
151  private:
152  typedef typename internal::encoding_signed_<n>::ret enc_;
153  };
154 
155 
156 
157  // Safety.
158  template <> struct int_s<0>;
159  template <> struct int_s<1>;
160 
161 
162 
170  template <unsigned n>
171  std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i);
172 
173 
174 
175 # ifndef MLN_INCLUDE_ONLY
176 
177  template <unsigned n>
178  inline
179  int_s<n>::int_s()
180  {
181  }
182 
183  template <unsigned n>
184  inline
185  int_s<n>::operator int() const
186  {
187  return this->v_;
188  }
189 
190  template <unsigned n>
191  inline
192  int_s<n>::int_s(int i)
193  {
194  static const int max = int(metal::math::pow_int<2, n-1>::value) - 1;
195  static const int min = - max;
196  mln_precondition(i >= min);
197  mln_precondition(i <= max);
198  (void) min;
199  (void) max;
200 
201  this->v_ = static_cast<enc_>(i);
202  }
203 
204  template <unsigned n>
205  inline
206  int_s<n>&
207  int_s<n>::operator=(int i)
208  {
209  static const int max = int(metal::math::pow_int<2, n-1>::value) - 1;
210  static const int min = - max;
211  mln_precondition(i >= min);
212  mln_precondition(i <= max);
213  (void) min;
214  (void) max;
215 
216  this->v_ = static_cast<enc_>(i);
217  return *this;
218  }
219 
220  template <unsigned n>
221  inline
222  int_s<n>::int_s(const mln::literal::zero_t&)
223  {
224  this->v_ = 0;
225  }
226 
227  template <unsigned n>
228  inline
229  int_s<n>&
231  {
232  this->v_ = 0;
233  return *this;
234  }
235 
236  template <unsigned n>
237  inline
239  {
240  this->v_ = 1;
241  }
242 
243  template <unsigned n>
244  inline
245  int_s<n>&
246  int_s<n>::operator=(const mln::literal::one_t&)
247  {
248  this->v_ = 1;
249  return *this;
250  }
251 
252  template <unsigned n>
253  const int_s<n> int_s<n>::zero = 0;
254 
255  template <unsigned n>
256  const int_s<n> int_s<n>::one = 1;
257 
258  template <unsigned n>
259  inline
260  std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i)
261  {
262  return ostr << debug::format(i.to_equiv());
263  }
264 
265 # endif // ! MLN_INCLUDE_ONLY
266 
267  } // end of namespace mln::value
268 
269 } // end of namespace mln
270 
271 
272 #endif // ! MLN_VALUE_INT_S_HH