$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends 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 
117  template <unsigned n>
118  struct int_s
119  :
120  private metal::bool_<(n <= 32)>::check_t
121  ,
122  public Integer< int_s<n> >
123  ,
124  public internal::value_like_< int, // Equivalent.
125  typename internal::encoding_signed_<n>::ret, // Enc.
126  int, // Interoperation.
127  int_s<n> > // Exact.
128  {
130  int_s();
131 
133  int_s(int i);
134 
136  int_s(const mln::literal::zero_t&);
137  int_s& operator=(const mln::literal::zero_t&);
138  int_s(const mln::literal::one_t&);
139  int_s& operator=(const mln::literal::one_t&);
141 
143  operator int() const;
144 
146  int_s<n>& operator=(int i);
147 
149  static const int_s<n> zero;
150 
152  static const int_s<n> one;
153 
154  private:
155  typedef typename internal::encoding_signed_<n>::ret enc_;
156  };
157 
158 
159 
160  // Safety.
161  template <> struct int_s<0>;
162  template <> struct int_s<1>;
163 
164 
165 
173  template <unsigned n>
174  std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i);
175 
176 
177 
178 # ifndef MLN_INCLUDE_ONLY
179 
180  template <unsigned n>
181  inline
182  int_s<n>::int_s()
183  {
184  }
185 
186  template <unsigned n>
187  inline
188  int_s<n>::operator int() const
189  {
190  return this->v_;
191  }
192 
193  template <unsigned n>
194  inline
195  int_s<n>::int_s(int i)
196  {
197  static const int max = int(metal::math::pow_int<2, n-1>::value) - 1;
198  static const int min = - max;
199  mln_precondition(i >= min);
200  mln_precondition(i <= max);
201  (void) min;
202  (void) max;
203 
204  this->v_ = static_cast<enc_>(i);
205  }
206 
207  template <unsigned n>
208  inline
209  int_s<n>&
210  int_s<n>::operator=(int i)
211  {
212  static const int max = int(metal::math::pow_int<2, n-1>::value) - 1;
213  static const int min = - max;
214  mln_precondition(i >= min);
215  mln_precondition(i <= max);
216  (void) min;
217  (void) max;
218 
219  this->v_ = static_cast<enc_>(i);
220  return *this;
221  }
222 
223  template <unsigned n>
224  inline
225  int_s<n>::int_s(const mln::literal::zero_t&)
226  {
227  this->v_ = 0;
228  }
229 
230  template <unsigned n>
231  inline
232  int_s<n>&
234  {
235  this->v_ = 0;
236  return *this;
237  }
238 
239  template <unsigned n>
240  inline
242  {
243  this->v_ = 1;
244  }
245 
246  template <unsigned n>
247  inline
248  int_s<n>&
250  {
251  this->v_ = 1;
252  return *this;
253  }
254 
255  template <unsigned n>
256  const int_s<n> int_s<n>::zero = 0;
257 
258  template <unsigned n>
259  const int_s<n> int_s<n>::one = 1;
260 
261  template <unsigned n>
262  inline
263  std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i)
264  {
265  return ostr << debug::format(i.to_equiv());
266  }
267 
268 # endif // ! MLN_INCLUDE_ONLY
269 
270  } // end of namespace mln::value
271 
272 } // end of namespace mln
273 
274 
275 #endif // ! MLN_VALUE_INT_S_HH