Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2007, 2008, 2009, 2010 EPITA Research and Development 00002 // Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_VALUE_INT_S_HH 00028 # define MLN_VALUE_INT_S_HH 00029 00033 00034 # include <mln/value/ops.hh> 00035 00036 # include <mln/metal/math/pow.hh> 00037 # include <mln/value/internal/value_like.hh> 00038 # include <mln/value/concept/integer.hh> 00039 # include <mln/value/internal/encoding.hh> 00040 # include <mln/trait/value_.hh> 00041 # include <mln/trait/all.hh> 00042 # include <mln/debug/format.hh> 00043 00044 00045 namespace mln 00046 { 00047 00048 00049 namespace value 00050 { 00052 template <unsigned n> struct int_s; 00054 } 00055 00056 namespace literal 00057 { 00059 struct zero_t; 00060 struct one_t; 00062 } 00063 00064 00065 00066 namespace trait 00067 { 00068 00069 template <unsigned n> 00070 struct value_< mln::value::int_s<n> > 00071 { 00072 private: 00073 typedef mln::value::int_s<n> self_; 00074 public: 00075 00076 enum constants_ { 00077 dim = 1, 00078 nbits = n, 00079 card = mln_value_card_from_(n) - 1 00080 }; 00081 00082 typedef trait::value::nature::integer nature; 00083 typedef trait::value::kind::data kind; 00084 typedef mln_value_quant_from_(card) quant; 00085 00086 static const self_ max() { return mln_value_card_from_(n) / 2 - 1; } 00087 static const self_ min() { return - max(); } 00088 static const self_ epsilon() { return 0; } 00089 00090 typedef mln::value::int_s<n> comp; 00091 00092 typedef float sum; 00093 00094 static const char* name() 00095 { 00096 static std::string s = std::string("int_s").append(1, n + '0'); 00097 return s.c_str(); 00098 } 00099 00100 }; 00101 00102 } // end of namespace mln::trait 00103 00104 00105 00106 namespace value 00107 { 00108 00109 00114 template <unsigned n> 00115 struct int_s 00116 : 00117 private metal::bool_<(n <= 32)>::check_t 00118 , 00119 public Integer< int_s<n> > 00120 , 00121 public internal::value_like_< int, // Equivalent. 00122 typename internal::encoding_signed_<n>::ret, // Enc. 00123 int, // Interoperation. 00124 int_s<n> > // Exact. 00125 { 00127 int_s(); 00128 00130 int_s(int i); 00131 00133 int_s(const mln::literal::zero_t&); 00134 int_s& operator=(const mln::literal::zero_t&); 00135 int_s(const mln::literal::one_t&); 00136 int_s& operator=(const mln::literal::one_t&); 00138 00140 operator int() const; 00141 00143 int_s<n>& operator=(int i); 00144 00146 static const int_s<n> zero; 00147 00149 static const int_s<n> one; 00150 00151 private: 00152 typedef typename internal::encoding_signed_<n>::ret enc_; 00153 }; 00154 00155 00156 00157 // Safety. 00158 template <> struct int_s<0>; 00159 template <> struct int_s<1>; 00160 00161 00162 00170 template <unsigned n> 00171 std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i); 00172 00173 00174 00175 # ifndef MLN_INCLUDE_ONLY 00176 00177 template <unsigned n> 00178 inline 00179 int_s<n>::int_s() 00180 { 00181 } 00182 00183 template <unsigned n> 00184 inline 00185 int_s<n>::operator int() const 00186 { 00187 return this->v_; 00188 } 00189 00190 template <unsigned n> 00191 inline 00192 int_s<n>::int_s(int i) 00193 { 00194 static const int max = int(metal::math::pow_int<2, n-1>::value) - 1; 00195 static const int min = - max; 00196 mln_precondition(i >= min); 00197 mln_precondition(i <= max); 00198 (void) min; 00199 (void) max; 00200 00201 this->v_ = static_cast<enc_>(i); 00202 } 00203 00204 template <unsigned n> 00205 inline 00206 int_s<n>& 00207 int_s<n>::operator=(int i) 00208 { 00209 static const int max = int(metal::math::pow_int<2, n-1>::value) - 1; 00210 static const int min = - max; 00211 mln_precondition(i >= min); 00212 mln_precondition(i <= max); 00213 (void) min; 00214 (void) max; 00215 00216 this->v_ = static_cast<enc_>(i); 00217 return *this; 00218 } 00219 00220 template <unsigned n> 00221 inline 00222 int_s<n>::int_s(const mln::literal::zero_t&) 00223 { 00224 this->v_ = 0; 00225 } 00226 00227 template <unsigned n> 00228 inline 00229 int_s<n>& 00230 int_s<n>::operator=(const mln::literal::zero_t&) 00231 { 00232 this->v_ = 0; 00233 return *this; 00234 } 00235 00236 template <unsigned n> 00237 inline 00238 int_s<n>::int_s(const mln::literal::one_t&) 00239 { 00240 this->v_ = 1; 00241 } 00242 00243 template <unsigned n> 00244 inline 00245 int_s<n>& 00246 int_s<n>::operator=(const mln::literal::one_t&) 00247 { 00248 this->v_ = 1; 00249 return *this; 00250 } 00251 00252 template <unsigned n> 00253 const int_s<n> int_s<n>::zero = 0; 00254 00255 template <unsigned n> 00256 const int_s<n> int_s<n>::one = 1; 00257 00258 template <unsigned n> 00259 inline 00260 std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i) 00261 { 00262 return ostr << debug::format(i.to_equiv()); 00263 } 00264 00265 # endif // ! MLN_INCLUDE_ONLY 00266 00267 } // end of namespace mln::value 00268 00269 } // end of namespace mln 00270 00271 00272 #endif // ! MLN_VALUE_INT_S_HH