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

int_u.hh

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_U_HH
00028 # define MLN_VALUE_INT_U_HH
00029 
00033 
00034 # include <mln/trait/all.hh> // FIXME!
00035 
00036 # include <mln/value/ops.hh>
00037 
00038 # include <mln/metal/math/pow.hh>
00039 # include <mln/value/internal/value_like.hh>
00040 # include <mln/value/internal/encoding.hh>
00041 # include <mln/value/concept/integer.hh>
00042 # include <mln/trait/value_.hh>
00043 # include <mln/debug/format.hh>
00044 
00045 # include <mln/value/internal/make_generic_name.hh>
00046 
00047 
00048 namespace mln
00049 {
00050 
00051   namespace value
00052   {
00053     // Forward declaration.
00054     template <unsigned n> struct int_u;
00055   }
00056 
00057   namespace literal
00058   {
00059     // Forward declarations.
00060     struct zero_t;
00061     struct one_t;
00062   }
00063 
00064 
00065   namespace trait
00066   {
00067 
00068     template <unsigned n>
00069     struct set_precise_unary_< op::uminus, mln::value::int_u<n> >
00070     {
00071       typedef int ret;
00072     };
00073 
00074 
00075     template <unsigned n>
00076     struct value_< mln::value::int_u<n> >
00077     {
00078     private:
00079       typedef mln::value::int_u<n> self_;
00080       typedef typename mln::value::internal::encoding_unsigned_<n>::ret enc_;
00081     public:
00082 
00083       enum constants_ {
00084         dim = 1,
00085         nbits = n,
00086         card  = mln_value_card_from_(n)
00087       };
00088 
00089       typedef trait::value::nature::integer nature;
00090       typedef trait::value::kind::data      kind;
00091       typedef mln_value_quant_from_(card)   quant;
00092 
00093       static const self_ min() { return 0; }
00094       static const self_ max() { return mlc_pow_int(2, n) - 1; }
00095       static const self_ epsilon() { return 0; }
00096 
00097       typedef unsigned comp;
00098 
00099       typedef float sum;
00100 
00101       static const char* name()
00102       {
00103         static std::string
00104           s = mln::value::internal::make_generic_name("int_u", n);
00105         return s.c_str();
00106       }
00107 
00108     };
00109 
00110   } // end of namespace mln::trait
00111 
00112 
00113   namespace convert
00114   {
00115 
00116     namespace over_load
00117     {
00118 
00119       // int_u -> unsigned.
00120       template <unsigned n>
00121       void
00122       from_to_(const value::int_u<n>& from, unsigned& to_);
00123 
00124 
00125       // int_u -> bool.
00126       template <unsigned n>
00127       void
00128       from_to_(const value::int_u<n>& from, bool& to_);
00129 
00130 
00131     } // end of namespace mln::convert::over_load
00132 
00133   } // end of namespace mln::convert
00134 
00135 
00136 
00137   namespace value
00138   {
00139 
00143     template <unsigned n>
00144     struct int_u
00145       :
00146       public Integer< int_u<n> >,
00147 
00148       public internal::value_like_< unsigned,    // Equivalent.
00149                                     typename internal::encoding_unsigned_<n>::ret, // Enc.
00150                                     int,         // Interoperation.
00151                                     int_u<n> >   // Exact.
00152     {
00153     protected:
00155       typedef typename internal::encoding_unsigned_<n>::ret enc_;
00156 
00157     public:
00158 
00160       int_u();
00161 
00163       int_u(int i);
00164 
00166       int_u(const mln::literal::zero_t&);
00167       int_u& operator=(const mln::literal::zero_t&);
00168       int_u(const mln::literal::one_t&);
00169       int_u& operator=(const mln::literal::one_t&);
00171 
00173       operator unsigned() const;
00174 
00176       int operator-() const;
00177 
00179       int_u<n>& operator=(int i);
00180 
00182       int_u<n> next() const;
00183     };
00184 
00185 
00186     // Safety.
00187     template <> struct int_u<0>;
00188     template <> struct int_u<1>;
00189 
00190 
00191 
00199     template <unsigned n>
00200     std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i);
00201 
00202 
00203     // FIXME: Doc!
00204     template <unsigned n>
00205     std::istream& operator>>(std::istream& istr, int_u<n>& i);
00206 
00207   } // end of namespace mln::value
00208 
00209 # ifndef MLN_INCLUDE_ONLY
00210 
00211   namespace convert
00212   {
00213 
00214     namespace over_load
00215     {
00216 
00217       // int_u -> unsigned.
00218       template <unsigned n>
00219       inline
00220       void
00221       from_to_(const value::int_u<n>& from, unsigned& to_)
00222       {
00223         to_ = from;
00224       }
00225 
00226       // int_u -> bool.
00227       template <unsigned n>
00228       inline
00229       void
00230       from_to_(const value::int_u<n>& from, bool& to_)
00231       {
00232         to_ = (from != 0u);
00233       }
00234 
00235 
00236     } // end of namespace mln::convert::over_load
00237 
00238   } // end of namespace mln::convert
00239 
00240 
00241   namespace value
00242   {
00243 
00244     template <unsigned n>
00245     inline
00246     int_u<n>::int_u()
00247     {
00248     }
00249 
00250     template <unsigned n>
00251     inline
00252     int_u<n>::int_u(int i)
00253     {
00254       mln_precondition(i >= 0);
00255       mln_precondition(unsigned(i) <= mln_max(enc_));
00256       this->v_ = static_cast<enc_>(i);
00257     }
00258 
00259     template <unsigned n>
00260     inline
00261     int_u<n>::int_u(const mln::literal::zero_t&)
00262     {
00263       this->v_ = 0;
00264     }
00265 
00266     template <unsigned n>
00267     inline
00268     int_u<n>&
00269     int_u<n>::operator=(const mln::literal::zero_t&)
00270     {
00271       this->v_ = 0;
00272       return *this;
00273     }
00274 
00275     template <unsigned n>
00276     inline
00277     int_u<n>::int_u(const mln::literal::one_t&)
00278     {
00279       this->v_ = 1;
00280     }
00281 
00282     template <unsigned n>
00283     inline
00284     int_u<n>&
00285     int_u<n>::operator=(const mln::literal::one_t&)
00286     {
00287       this->v_ = 1;
00288       return *this;
00289     }
00290 
00291     template <unsigned n>
00292     inline
00293     int_u<n>::operator unsigned() const
00294     {
00295       return this->v_;
00296     }
00297 
00298     template <unsigned n>
00299     inline
00300     int
00301     int_u<n>::operator-() const
00302     {
00303       return - int(this->v_);
00304     }
00305 
00306     template <unsigned n>
00307     inline
00308     int_u<n>&
00309     int_u<n>::operator=(int i)
00310     {
00311       mln_precondition(i >= 0);
00312       mln_precondition(unsigned(i) <= mln_max(enc_));
00313       this->v_ = static_cast<enc_>(i);
00314       return *this;
00315     }
00316 
00317     template <unsigned n>
00318     inline
00319     int_u<n>
00320     int_u<n>::next() const
00321     {
00322       return this->v_ + 1;
00323     }
00324 
00325     template <unsigned n>
00326     inline
00327     std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i)
00328     {
00329       // FIXME: This code could be factored for almost every Value<*>...
00330       return ostr << debug::format(i.to_equiv()); // FIXME: is to_equiv OK?
00331     }
00332 
00333     template <unsigned n>
00334     inline
00335     std::istream& operator>>(std::istream& istr, int_u<n>& i)
00336     {
00337       return istr >> i.handle_();
00338     }
00339 
00340   } // end of namespace mln::value
00341 
00342 # endif // ! MLN_INCLUDE_ONLY
00343 
00344 } // end of namespace mln
00345 
00346 
00347 #endif // ! MLN_VALUE_INT_U_HH
00348 

Generated on Fri Oct 19 2012 04:15:55 for Milena (Olena) by  doxygen 1.7.1