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

int_u.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_U_HH
00027 # define MLN_VALUE_INT_U_HH
00028 
00032 
00033 # include <mln/trait/all.hh> // FIXME!
00034 
00035 # include <mln/value/ops.hh>
00036 
00037 # include <mln/metal/math/pow.hh>
00038 # include <mln/value/internal/value_like.hh>
00039 # include <mln/value/internal/encoding.hh>
00040 # include <mln/value/concept/integer.hh>
00041 # include <mln/trait/value_.hh>
00042 # include <mln/debug/format.hh>
00043 
00044 
00045 
00046 namespace mln
00047 {
00048 
00049   namespace value
00050   {
00051     // Forward declaration.
00052     template <unsigned n> struct int_u;
00053   }
00054 
00055   namespace literal
00056   {
00057     // Forward declarations.
00058     struct zero_t;
00059     struct one_t;
00060   }
00061 
00062 
00063   namespace trait
00064   {
00065 
00066     template <unsigned n>
00067     struct set_precise_unary_< op::uminus, mln::value::int_u<n> >
00068     {
00069       typedef int ret;
00070     };
00071 
00072 
00073     template <unsigned n>
00074     struct value_< mln::value::int_u<n> >
00075     {
00076     private:
00077       typedef mln::value::int_u<n> self_;
00078       typedef typename mln::value::internal::encoding_unsigned_<n>::ret enc_;
00079     public:
00080 
00081       enum constants_ {
00082         dim = 1,
00083         nbits = n,
00084         card  = mln_value_card_from_(n)
00085       };
00086 
00087       typedef trait::value::nature::integer nature;
00088       typedef trait::value::kind::data      kind;
00089       typedef mln_value_quant_from_(card)   quant;
00090 
00091       static const self_ min() { return 0; }
00092       static const self_ max() { return mlc_pow_int(2, n) - 1; }
00093       static const self_ epsilon() { return 0; }
00094 
00095       typedef unsigned comp;
00096 
00097       typedef float sum;
00098 
00099       static const char* name()
00100       {
00101         static std::string s = std::string("int_u").append(1, n + '0');
00102         return s.c_str();
00103       }
00104 
00105     };
00106 
00107   } // end of namespace mln::trait
00108 
00109 
00110 
00111   namespace value
00112   {
00113 
00117     template <unsigned n>
00118     struct int_u
00119       :
00120       public Integer< int_u<n> >,
00121 
00122       public internal::value_like_< unsigned,    // Equivalent.
00123                                     typename internal::encoding_unsigned_<n>::ret, // Enc.
00124                                     int,         // Interoperation.
00125                                     int_u<n> >   // Exact.
00126     {
00127     protected:
00129       typedef typename internal::encoding_unsigned_<n>::ret enc_;
00130 
00131     public:
00132 
00134       int_u();
00135 
00137       int_u(int i);
00138 
00140       int_u(const mln::literal::zero_t&);
00141       int_u& operator=(const mln::literal::zero_t&);
00142       int_u(const mln::literal::one_t&);
00143       int_u& operator=(const mln::literal::one_t&);
00145 
00147       operator unsigned() const;
00148 
00150       int operator-() const;
00151 
00153       int_u<n>& operator=(int i);
00154 
00156       int_u<n> next() const;
00157     };
00158 
00159 
00160     // Safety.
00161     template <> struct int_u<0>;
00162     template <> struct int_u<1>;
00163 
00164 
00165 
00173     template <unsigned n>
00174     std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i);
00175 
00176 
00177     // FIXME: Doc!
00178     template <unsigned n>
00179     std::istream& operator>>(std::istream& istr, int_u<n>& i);
00180 
00181 
00182 # ifndef MLN_INCLUDE_ONLY
00183 
00184     template <unsigned n>
00185     inline
00186     int_u<n>::int_u()
00187     {
00188     }
00189 
00190     template <unsigned n>
00191     inline
00192     int_u<n>::int_u(int i)
00193     {
00194       mln_precondition(i >= 0);
00195       mln_precondition(unsigned(i) <= mln_max(enc_));
00196       this->v_ = static_cast<enc_>(i);
00197     }
00198 
00199     template <unsigned n>
00200     inline
00201     int_u<n>::int_u(const mln::literal::zero_t&)
00202     {
00203       this->v_ = 0;
00204     }
00205 
00206     template <unsigned n>
00207     inline
00208     int_u<n>&
00209     int_u<n>::operator=(const mln::literal::zero_t&)
00210     {
00211       this->v_ = 0;
00212       return *this;
00213     }
00214 
00215     template <unsigned n>
00216     inline
00217     int_u<n>::int_u(const mln::literal::one_t&)
00218     {
00219       this->v_ = 1;
00220     }
00221 
00222     template <unsigned n>
00223     inline
00224     int_u<n>&
00225     int_u<n>::operator=(const mln::literal::one_t&)
00226     {
00227       this->v_ = 1;
00228       return *this;
00229     }
00230 
00231     template <unsigned n>
00232     inline
00233     int_u<n>::operator unsigned() const
00234     {
00235       return this->v_;
00236     }
00237 
00238     template <unsigned n>
00239     inline
00240     int
00241     int_u<n>::operator-() const
00242     {
00243       return - int(this->v_);
00244     }
00245 
00246     template <unsigned n>
00247     inline
00248     int_u<n>&
00249     int_u<n>::operator=(int i)
00250     {
00251       mln_precondition(i >= 0);
00252       mln_precondition(unsigned(i) <= mln_max(enc_));
00253       this->v_ = static_cast<enc_>(i);
00254       return *this;
00255     }
00256 
00257     template <unsigned n>
00258     inline
00259     int_u<n>
00260     int_u<n>::next() const
00261     {
00262       return this->v_ + 1;
00263     }
00264 
00265     template <unsigned n>
00266     inline
00267     std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i)
00268     {
00269       // FIXME: This code could be factored for almost every Value<*>...
00270       return ostr << debug::format(i.to_equiv()); // FIXME: is to_equiv OK?
00271     }
00272 
00273     template <unsigned n>
00274     inline
00275     std::istream& operator>>(std::istream& istr, int_u<n>& i)
00276     {
00277       return istr >> i.handle_();
00278     }
00279 
00280 # endif // ! MLN_INCLUDE_ONLY
00281 
00282   } // end of namespace mln::value
00283 
00284 } // end of namespace mln
00285 
00286 
00287 #endif // ! MLN_VALUE_INT_U_HH

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