Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 00025 00026 #ifndef MLN_VALUE_INT_U_SAT_HH 00027 # define MLN_VALUE_INT_U_SAT_HH 00028 00033 00034 # include <mln/metal/math/pow.hh> 00035 # include <mln/value/internal/value_like.hh> 00036 # include <mln/value/concept/integer.hh> 00037 # include <mln/value/internal/encoding.hh> 00038 # include <mln/value/int_u.hh> 00039 # include <mln/trait/value_.hh> 00040 # include <mln/debug/format.hh> 00041 00042 00043 namespace mln 00044 { 00045 00046 namespace value 00047 { 00048 // Fwd decls. 00049 template <unsigned n> struct int_u_sat; 00050 } 00051 00052 namespace trait 00053 { 00054 00055 template <unsigned n> 00056 struct value_< mln::value::int_u_sat<n> > 00057 { 00058 enum { 00059 dim = 1, 00060 card = metal::math::pow_int<2, n>::value, 00061 nbits = n 00062 }; 00063 00064 // FIXME: Overhaul these traits (see other value traits). 00065 static const mln::value::int_u_sat<n> min() { return 0; } 00066 static const mln::value::int_u_sat<n> max() { return card - 1; } 00067 00068 typedef trait::value::nature::integer nature; 00069 typedef trait::value::kind::data kind; 00070 // FIXME: Is that right? 00071 typedef mln_value_quant_from_(card) quant; 00072 00073 typedef unsigned comp; 00074 00075 typedef float sum; 00076 }; 00077 00078 } // end of namespace mln::trait 00079 00080 00081 namespace value 00082 { 00083 00084 00089 template <unsigned n> 00090 struct int_u_sat 00091 : 00092 public Integer< int_u_sat<n> >, 00093 00094 public internal::value_like_< int_u<n>, // Equivalent. 00095 mln_enc(int_u<n>), // Encoding. 00096 unsigned, // Interoperation. 00097 int_u_sat<n> > // Exact. 00098 { 00100 int_u_sat(); 00101 00103 int_u_sat(int i); 00104 00106 operator int() const; 00107 00109 int_u_sat<n>& operator=(int i); 00110 00112 static const int_u_sat<n> zero; 00113 00115 static const int_u_sat<n> one; 00116 00118 int_u_sat<n>& operator+=(int i); 00119 00121 int_u_sat<n>& operator-=(int i); 00122 00123 private: 00124 typedef mln_enc(int_u<n>) enc_; 00125 }; 00126 00127 00128 // Safety. 00129 // FIXME: We shouldn't have to do that. 00130 template <> struct int_u_sat<0>; 00131 template <> struct int_u_sat<1>; 00132 00141 template <unsigned n> 00142 std::ostream& operator<<(std::ostream& ostr, const int_u_sat<n>& i); 00143 00144 00145 # ifndef MLN_INCLUDE_ONLY 00146 00147 template <unsigned n> 00148 inline 00149 int_u_sat<n>::int_u_sat() 00150 { 00151 } 00152 00153 template <unsigned n> 00154 inline 00155 int_u_sat<n>::int_u_sat(int i) 00156 { 00157 static const unsigned max_ = mln_max(int_u<n>); 00158 if (i < 0) 00159 this->v_ = 0; 00160 // Explicitly cast I to unsigned to avoid a warning between 00161 // signed and unsigned values from the compiler. 00162 else if (static_cast<unsigned>(i) > max_) 00163 this->v_ = static_cast<enc_>(max_); 00164 else 00165 this->v_ = static_cast<enc_>(i); 00166 } 00167 00168 template <unsigned n> 00169 inline 00170 int_u_sat<n>::operator int() const 00171 { 00172 return this->v_; 00173 } 00174 00175 template <unsigned n> 00176 inline 00177 int_u_sat<n>& 00178 int_u_sat<n>::operator=(int i) 00179 { 00180 static const unsigned max_ = mln_max(int_u<n>); 00181 if (i < 0) 00182 this->v_ = 0; 00183 // Explicitly cast I to unsigned to avoid a warning between 00184 // signed and unsigned values from the compiler. 00185 else if (static_cast<unsigned>(i) > max_) 00186 this->v_ = static_cast<enc_>(max_); 00187 else 00188 this->v_ = static_cast<enc_>(i); 00189 return *this; 00190 } 00191 00192 template <unsigned n> 00193 inline 00194 int_u_sat<n>& 00195 int_u_sat<n>::operator+=(int i) 00196 { 00197 int v = int(this->v_) + i; 00198 *this = v; 00199 return *this; 00200 } 00201 00202 template <unsigned n> 00203 inline 00204 int_u_sat<n>& 00205 int_u_sat<n>::operator-=(int i) 00206 { 00207 int v = int(this->v_) - i; 00208 *this = v; 00209 return *this; 00210 } 00211 00212 template <unsigned n> 00213 const int_u_sat<n> int_u_sat<n>::zero = 0; 00214 00215 template <unsigned n> 00216 const int_u_sat<n> int_u_sat<n>::one = 1; 00217 00218 template <unsigned n> 00219 inline 00220 std::ostream& operator<<(std::ostream& ostr, const int_u_sat<n>& i) 00221 { 00222 return ostr << debug::format(i.to_equiv()); 00223 } 00224 00225 # endif // ! MLN_INCLUDE_ONLY 00226 00227 } // end of namespace mln::value 00228 00229 } // end of namespace mln 00230 00231 00232 #endif // ! MLN_VALUE_INT_U_SAT_HH