• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

int_s.hh

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_S_HH
00027 # define MLN_VALUE_INT_S_HH
00028 
00032 
00033 # include <mln/value/ops.hh>
00034 
00035 # include <mln/metal/math/pow.hh>
00036 # include <mln/value/internal/value_like.hh>
00037 # include <mln/value/concept/integer.hh>
00038 # include <mln/value/internal/encoding.hh>
00039 # include <mln/trait/value_.hh>
00040 # include <mln/trait/all.hh>
00041 # include <mln/debug/format.hh>
00042 
00043 
00044 namespace mln
00045 {
00046 
00047 
00048   namespace value
00049   {
00051     template <unsigned n> struct int_s;
00053   }
00054 
00055   namespace literal
00056   { 
00058     struct zero_t;
00059     struct one_t;
00061   }
00062 
00063 
00064 
00065   namespace trait
00066   {
00067 
00068     template <unsigned n>
00069     struct value_< mln::value::int_s<n> >
00070     {
00071     private:
00072       typedef mln::value::int_s<n> self_;
00073     public:
00074 
00075       enum constants_ {
00076         dim = 1,
00077         nbits = n,
00078         card  = mln_value_card_from_(n) - 1
00079       };
00080 
00081       typedef trait::value::nature::integer nature;
00082       typedef trait::value::kind::data      kind;
00083       typedef mln_value_quant_from_(card)   quant;
00084 
00085       static const self_ max() { return mln_value_card_from_(n) / 2 - 1; }
00086       static const self_ min() { return - max(); }
00087       static const self_ epsilon() { return 0; }
00088 
00089       typedef mln::value::int_s<n> comp;
00090 
00091       typedef float sum;
00092 
00093       static const char* name()
00094       {
00095         static std::string s = std::string("int_s").append(1, n + '0');
00096         return s.c_str();
00097       }
00098 
00099     };
00100 
00101   } // end of namespace mln::trait
00102 
00103 
00104 
00105   namespace value
00106   {
00107 
00108 
00113     template <unsigned n>
00114     struct int_s
00115       :
00116       private metal::bool_<(n <= 32)>::check_t
00117       ,
00118       public Integer< int_s<n> >
00119       ,
00120       public internal::value_like_< int,       // Equivalent.
00121                                     typename internal::encoding_signed_<n>::ret, // Enc.
00122                                     int,       // Interoperation.
00123                                     int_s<n> > // Exact.
00124     {
00126       int_s();
00127 
00129       int_s(int i);
00130 
00132       int_s(const mln::literal::zero_t&);
00133       int_s& operator=(const mln::literal::zero_t&);
00134       int_s(const mln::literal::one_t&);
00135       int_s& operator=(const mln::literal::one_t&);
00137 
00139       operator int() const;
00140 
00142       int_s<n>& operator=(int i);
00143 
00145       static const int_s<n> zero;
00146 
00148       static const int_s<n> one;
00149 
00150     private:
00151       typedef typename internal::encoding_signed_<n>::ret enc_;
00152     };
00153 
00154 
00155 
00156     // Safety.
00157     template <> struct int_s<0>;
00158     template <> struct int_s<1>;
00159 
00160 
00161 
00169     template <unsigned n>
00170     std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i);
00171 
00172 
00173 
00174 # ifndef MLN_INCLUDE_ONLY
00175 
00176     template <unsigned n>
00177     inline
00178     int_s<n>::int_s()
00179     {
00180     }
00181 
00182     template <unsigned n>
00183     inline
00184     int_s<n>::operator int() const
00185     {
00186       return this->v_;
00187     }
00188 
00189     template <unsigned n>
00190     inline
00191     int_s<n>::int_s(int i)
00192     {
00193       static const int max = int(metal::math::pow_int<2, n-1>::value) - 1;
00194       static const int min = - max;
00195       mln_precondition(i >= min);
00196       mln_precondition(i <= max);
00197       this->v_ = static_cast<enc_>(i);
00198     }
00199 
00200     template <unsigned n>
00201     inline
00202     int_s<n>&
00203     int_s<n>::operator=(int i)
00204     {
00205       static const int max = int(metal::math::pow_int<2, n-1>::value) - 1;
00206       static const int min = - max;
00207       mln_precondition(i >= min);
00208       mln_precondition(i <= max);
00209       this->v_ = static_cast<enc_>(i);
00210       return *this;
00211     }
00212 
00213     template <unsigned n>
00214     inline
00215     int_s<n>::int_s(const mln::literal::zero_t&)
00216     {
00217       this->v_ = 0;
00218     }
00219 
00220     template <unsigned n>
00221     inline
00222     int_s<n>&
00223     int_s<n>::operator=(const mln::literal::zero_t&)
00224     {
00225       this->v_ = 0;
00226       return *this;
00227     }
00228 
00229     template <unsigned n>
00230     inline
00231     int_s<n>::int_s(const mln::literal::one_t&)
00232     {
00233       this->v_ = 1;
00234     }
00235 
00236     template <unsigned n>
00237     inline
00238     int_s<n>&
00239     int_s<n>::operator=(const mln::literal::one_t&)
00240     {
00241       this->v_ = 1;
00242       return *this;
00243     }
00244 
00245     template <unsigned n>
00246     const int_s<n> int_s<n>::zero = 0;
00247 
00248     template <unsigned n>
00249     const int_s<n> int_s<n>::one = 1;
00250 
00251     template <unsigned n>
00252     inline
00253     std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i)
00254     {
00255       return ostr << debug::format(i.to_equiv());
00256     }
00257 
00258 # endif // ! MLN_INCLUDE_ONLY
00259 
00260   } // end of namespace mln::value
00261 
00262 } // end of namespace mln
00263 
00264 
00265 #endif // ! MLN_VALUE_INT_S_HH

Generated on Thu Sep 8 2011 18:32:00 for Milena (Olena) by  doxygen 1.7.1