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

array1d.hh

00001 // Copyright (C) 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_METAL_ARRAY1D_HH
00027 # define MLN_METAL_ARRAY1D_HH
00028 
00029 # include <mln/core/concept/object.hh>
00030 
00031 # include <mln/trait/all.hh>
00032 # include <mln/trait/value_.hh>
00033 
00034 # include <mln/value/ops.hh>
00035 
00036 namespace mln
00037 {
00038 
00039   // Fwd decls.
00040   namespace metal  {
00041     template <typename T, unsigned Size> struct array1d;
00042   }
00043   
00044   namespace trait
00045   {
00046     
00047     template <typename T, unsigned Size>
00048     struct value_< mln::metal::array1d<T,Size> >
00049     {
00050       typedef trait::value::nature::vectorial nature;
00051       typedef trait::value::kind::data        kind;
00052       
00053       enum {
00054         nbits = Size * mln_nbits(T),
00055         card  = Size * mln_card(T)
00056       };
00057       typedef mln_value_quant_from_(card)     quant;
00058 
00059       typedef metal::array1d<mln_sum(T),Size> sum;
00060     };
00061     
00062   } // end of namespace mln::trait
00063 
00064   
00065   namespace metal
00066   {
00067 
00068     template <typename T, unsigned Size>
00069     struct array1d : public Object< array1d<T,Size> >
00070     {
00071 
00072       //
00073       // Constructors
00074       //
00075 
00076       array1d();
00077       array1d(T* ptr);
00078 
00079       // Copy
00080 
00081       array1d(const array1d<T, Size>& rhs);
00082       array1d<T, Size>& operator=(const array1d<T, Size>& rhs);
00083 
00084       // Operators
00085 
00086       template <class U>
00087       array1d<T, Size> operator*(U w);
00088 
00089       template <class U>
00090       array1d<mln_trait_op_div(T,U), Size>
00091       operator/(U w);
00092 
00093       template <typename U>
00094       array1d<mln_trait_op_plus(T,U), Size>
00095       operator+(const array1d<U, Size>& rhs) const;
00096       array1d<T, Size>& operator+=(const array1d<T, Size>& rhs);
00097 
00098       template <typename U>
00099       array1d<mln_trait_op_minus(T,U), Size>
00100       operator-(const array1d<U, Size>& rhs) const;
00101       array1d<T, Size>&
00102       operator-=(const array1d<T, Size>& rhs);
00103 
00104       
00105       // dynamic accessors:
00106 
00107       T operator[](unsigned i) const {
00108         mln_precondition(i < Size);
00109         return buffer_[i];
00110       }
00111       T& operator[](unsigned i) {
00112         mln_precondition(i < Size);
00113         return buffer_[i];
00114       }
00115 
00116       // static accessor
00117 
00118       template<unsigned i>
00119       T get() const {
00120         return buffer_[i];
00121       }
00122       template<unsigned i>
00123       T& get() {
00124         return buffer_[i];
00125       }
00126 
00127       enum { length = Size };
00128     protected:
00129 
00130       T buffer_[Size];
00131     };
00132 
00133   }
00134 
00135   namespace trait
00136   {
00137 
00138     // For unary traits.
00139 
00140     template < template <class> class Name,
00141                unsigned n, typename T >
00142     struct set_precise_unary_< Name, metal::array1d<T, n> >
00143     {
00144       typedef mln_trait_unary(Name, T) V;
00145       typedef metal::array1d<V, n> ret;
00146     };
00147 
00148     // For binary traits.
00149 
00150     template < template <class, class> class Name,
00151                unsigned n, typename T,
00152                typename U >
00153     struct set_precise_binary_< Name,
00154                                 metal::array1d<T, n>, metal::array1d<U, n> >
00155     {
00156       typedef mln_trait_binary(Name, T, U) V;
00157       typedef metal::array1d<V, n> ret;
00158     };
00159 
00160     template < unsigned n, typename T,
00161                typename U >
00162     struct set_precise_binary_< op::times,
00163                                 metal::array1d<T, n>, metal::array1d<U, n> >
00164     {
00165       typedef mln_sum_product(T,U) ret;
00166     };
00167 
00168     template < template <class, class> class Name,
00169                unsigned n, typename T,
00170                typename S >
00171     struct set_precise_binary_< Name,
00172                                 metal::array1d<T, n>, mln::value::scalar_<S> >
00173     {
00174       typedef mln_trait_binary(Name, T, S) V;
00175       typedef metal::array1d<V, n> ret;
00176     };
00177 
00178     template < template<class, class> class Name,
00179                unsigned n, typename T,
00180                typename S >
00181     struct set_binary_< Name,
00182                         mln::Object, metal::array1d<T, n>,
00183                         mln::value::Scalar, S >
00184     {
00185       typedef mln_trait_binary(Name, T, S) V;
00186       typedef metal::array1d<T, n> ret;
00187     };
00188 
00189   } // end of namespace mln::trait
00190 
00191 
00192   namespace metal
00193   {
00194 
00195     //
00196     // Constructors
00197     //
00198 
00199     template <typename T, unsigned Size>
00200     array1d<T,Size>::array1d()
00201     {
00202     }
00203 
00204     template <typename T, unsigned Size>
00205     array1d<T,Size>::array1d(T* ptr)
00206     {
00207       for (unsigned i = 0; i < Size; ++i)
00208         buffer_[i] = *ptr++;
00209     }
00210 
00211     // Copy
00212 
00213     template <typename T, unsigned Size>
00214     array1d<T,Size>::array1d(const array1d<T, Size>& rhs)
00215     {
00216       for (unsigned i = 0; i < Size; ++i)
00217         buffer_[i] = rhs[i];
00218     }
00219     template <typename T, unsigned Size>
00220     array1d<T, Size>&
00221     array1d<T,Size>::operator=(const array1d<T, Size>& rhs)
00222     {
00223       for (unsigned i = 0; i < Size; ++i)
00224         buffer_[i] = rhs[i];
00225       return *this;
00226     }
00227 
00228     // Operators
00229 
00230     template <typename T, unsigned Size>
00231     template <class U>
00232     array1d<T, Size>
00233     array1d<T,Size>::operator*(U w)
00234     {
00235       //fixme mln_trait_op_mult<int,U>
00236       array1d<T, Size> tmp;
00237       for (unsigned i = 0; i < Size; ++i)
00238         tmp[i] = this->buffer_[i] * w;
00239       return tmp;
00240     }
00241 
00242     template <typename T, unsigned Size>
00243     template <class U>
00244     array1d<mln_trait_op_div(T,U), Size>
00245     array1d<T,Size>::operator/(U w)
00246     {
00247       array1d<T, Size> tmp;
00248       for (unsigned i = 0; i < Size; ++i)
00249         tmp[i] = this->buffer_[i] / w;
00250       return tmp;
00251     }
00252 
00253     template <typename T, unsigned Size>
00254     template <typename U>
00255     array1d<mln_trait_op_plus(T,U), Size>
00256     array1d<T,Size>::operator+(const array1d<U, Size>& rhs) const
00257     {
00258       array1d<T, Size> tmp;
00259       for (unsigned i = 0; i < Size; ++i)
00260         tmp[i] = this->buffer_[i] + rhs.buffer_[i];
00261       return tmp;
00262     }
00263     template <typename T, unsigned Size>
00264     array1d<T, Size>&
00265     array1d<T,Size>::operator+=(const array1d<T, Size>& rhs)
00266     {
00267       for (unsigned i = 0; i < Size; ++i)
00268         this->buffer_[i] += rhs.buffer_[i];
00269       return *this;
00270     }
00271 
00272     template <typename T, unsigned Size>
00273     template <typename U>
00274     array1d<mln_trait_op_minus(T,U), Size>
00275     array1d<T,Size>::operator-(const array1d<U, Size>& rhs) const
00276     {
00277       array1d<T, Size> tmp;
00278       for (unsigned i = 0; i < Size; ++i)
00279         tmp[i] = this->buffer_[i] - rhs.buffer_[i];
00280       return tmp;
00281     }
00282     template <typename T, unsigned Size>
00283     array1d<T, Size>&
00284     array1d<T,Size>::operator-=(const array1d<T, Size>& rhs)
00285     {
00286       for (unsigned i = 0; i < Size; ++i)
00287         this->buffer_[i] -= rhs.buffer_[i];
00288       return *this;
00289     }
00290 
00291   } // end of namespace metal
00292 
00293 } // end of namespace mln
00294 
00295 #endif // ! MLN_METAL_ARRAY1D_HH

Generated on Tue Oct 4 2011 15:23:24 for Milena (Olena) by  doxygen 1.7.1