subview_field_meat.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
00002 // Copyright (C) 2008-2011 Conrad Sanderson
00003 // 
00004 // This file is part of the Armadillo C++ library.
00005 // It is provided without any warranty of fitness
00006 // for any purpose. You can redistribute this file
00007 // and/or modify it under the terms of the GNU
00008 // Lesser General Public License (LGPL) as published
00009 // by the Free Software Foundation, either version 3
00010 // of the License or (at your option) any later version.
00011 // (see http://www.opensource.org/licenses for more info)
00012 
00013 
00016 
00017 
00018 template<typename oT>
00019 inline
00020 subview_field<oT>::~subview_field()
00021   {
00022   arma_extra_debug_sigprint();
00023   }
00024 
00025 
00026 
00027 template<typename oT>
00028 arma_inline
00029 subview_field<oT>::subview_field
00030   (
00031   const field<oT>& in_f,
00032   const uword        in_row1,
00033   const uword        in_col1,
00034   const uword        in_n_rows,
00035   const uword        in_n_cols
00036   )
00037   : f(in_f)
00038   , f_ptr(0)
00039   , aux_row1(in_row1)
00040   , aux_col1(in_col1)
00041   , n_rows(in_n_rows)
00042   , n_cols(in_n_cols)
00043   , n_elem(in_n_rows*in_n_cols)
00044   {
00045   arma_extra_debug_sigprint();
00046   }
00047 
00048 
00049 
00050 template<typename oT>
00051 arma_inline
00052 subview_field<oT>::subview_field
00053   (
00054         field<oT>& in_f,
00055   const uword        in_row1,
00056   const uword        in_col1,
00057   const uword        in_n_rows,
00058   const uword        in_n_cols
00059   )
00060   : f(in_f)
00061   , f_ptr(&in_f)
00062   , aux_row1(in_row1)
00063   , aux_col1(in_col1)
00064   , n_rows(in_n_rows)
00065   , n_cols(in_n_cols)
00066   , n_elem(in_n_rows*in_n_cols)
00067   {
00068   arma_extra_debug_sigprint();
00069   }
00070 
00071 
00072 
00073 template<typename oT>
00074 inline
00075 void
00076 subview_field<oT>::operator= (const field<oT>& x)
00077   {
00078   arma_extra_debug_sigprint();
00079   
00080   subview_field<oT>& t = *this;
00081   
00082   arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions");
00083   
00084   for(uword col=0; col<t.n_cols; ++col)
00085     {
00086     for(uword row=0; row<t.n_rows; ++row)
00087       {
00088       t.at(row,col) = x.at(row,col);
00089       }
00090     }
00091   }
00092 
00093 
00094 
00096 template<typename oT>
00097 inline
00098 void
00099 subview_field<oT>::operator= (const subview_field<oT>& x_in)
00100   {
00101   arma_extra_debug_sigprint();
00102   
00103   const bool overlap = check_overlap(x_in);
00104         
00105         field<oT>*         tmp_field   = overlap ? new field<oT>(x_in.f) : 0;
00106   const subview_field<oT>* tmp_subview = overlap ? new subview_field<oT>(*tmp_field, x_in.aux_row1, x_in.aux_col1, x_in.n_rows, x_in.n_cols) : 0;
00107   const subview_field<oT>& x           = overlap ? (*tmp_subview) : x_in;
00108   
00109   subview_field<oT>& t = *this;
00110   
00111   arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions");
00112   
00113   for(uword col=0; col<t.n_cols; ++col)
00114     {
00115     for(uword row=0; row<t.n_rows; ++row)
00116       {
00117       t.at(row,col) = x.at(row,col);
00118       }
00119     }
00120     
00121   if(overlap)
00122     {
00123     delete tmp_subview;
00124     delete tmp_field;
00125     }
00126   }
00127 
00128 
00129 
00130 template<typename oT>
00131 arma_inline
00132 oT&
00133 subview_field<oT>::operator[](const uword i)
00134   {
00135   arma_check( (f_ptr == 0), "subview_field::operator[]: field is read-only");
00136   
00137   const uword in_col = i / n_rows;
00138   const uword in_row = i % n_rows;
00139     
00140   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00141   
00142   return *((*f_ptr).mem[index]);
00143   }
00144 
00145 
00146 
00147 template<typename oT>
00148 arma_inline
00149 const oT&
00150 subview_field<oT>::operator[](const uword i) const
00151   {
00152   const uword in_col = i / n_rows;
00153   const uword in_row = i % n_rows;
00154   
00155   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00156   
00157   return *(f.mem[index]);
00158   }
00159 
00160 
00161 
00162 template<typename oT>
00163 arma_inline
00164 oT&
00165 subview_field<oT>::operator()(const uword i)
00166   {
00167   arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only");
00168   arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds");
00169     
00170   const uword in_col = i / n_rows;
00171   const uword in_row = i % n_rows;
00172   
00173   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00174   
00175   return *((*f_ptr).mem[index]);
00176   }
00177 
00178 
00179 
00180 template<typename oT>
00181 arma_inline
00182 const oT&
00183 subview_field<oT>::operator()(const uword i) const
00184   {
00185   arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds");
00186   
00187   const uword in_col = i / n_rows;
00188   const uword in_row = i % n_rows;
00189   
00190   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00191   
00192   return *(f.mem[index]);
00193   }
00194 
00195 
00196 
00197 template<typename oT>
00198 arma_inline
00199 oT&
00200 subview_field<oT>::operator()(const uword in_row, const uword in_col)
00201   {
00202   arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only");
00203   arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds");
00204   
00205   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00206   
00207   return *((*f_ptr).mem[index]);
00208   }
00209 
00210 
00211 
00212 template<typename oT>
00213 arma_inline
00214 const oT&
00215 subview_field<oT>::operator()(const uword in_row, const uword in_col) const
00216   {
00217   arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds");
00218   
00219   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00220   
00221   return *(f.mem[index]);
00222   }
00223 
00224 
00225 
00226 template<typename oT>
00227 arma_inline
00228 oT&
00229 subview_field<oT>::at(const uword in_row, const uword in_col)
00230   {
00231   //arma_extra_debug_sigprint();
00232   
00233   arma_check( (f_ptr == 0), "subview_field::at(): field is read-only");
00234   
00235   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00236   
00237   return *((*f_ptr).mem[index]);
00238   }
00239 
00240 
00241 
00242 template<typename oT>
00243 arma_inline
00244 const oT&
00245 subview_field<oT>::at(const uword in_row, const uword in_col) const
00246   {
00247   //arma_extra_debug_sigprint();
00248   
00249   const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
00250   
00251   return *(f.mem[index]);
00252   }
00253 
00254 
00255 
00256 template<typename oT>
00257 inline
00258 bool
00259 subview_field<oT>::check_overlap(const subview_field<oT>& x) const
00260   {
00261   const subview_field<oT>& t = *this;
00262   
00263   if(&t.f != &x.f)
00264     {
00265     return false;
00266     }
00267   else
00268     {
00269     if( (t.n_elem == 0) || (x.n_elem == 0) )
00270       {
00271       return false;
00272       }
00273     else
00274       {
00275       const uword t_row_start  = t.aux_row1;
00276       const uword t_row_end_p1 = t_row_start + t.n_rows;
00277       
00278       const uword t_col_start  = t.aux_col1;
00279       const uword t_col_end_p1 = t_col_start + t.n_cols;
00280       
00281       
00282       const uword x_row_start  = x.aux_row1;
00283       const uword x_row_end_p1 = x_row_start + x.n_rows;
00284       
00285       const uword x_col_start  = x.aux_col1;
00286       const uword x_col_end_p1 = x_col_start + x.n_cols;
00287       
00288       
00289       const bool outside_rows = ( (x_row_start >= t_row_end_p1) || (t_row_start >= x_row_end_p1) );
00290       const bool outside_cols = ( (x_col_start >= t_col_end_p1) || (t_col_start >= x_col_end_p1) );
00291       
00292       return ( (outside_rows == false) && (outside_cols == false) );
00293       }
00294     }
00295   }
00296 
00297 
00298 
00300 template<typename oT>
00301 inline
00302 void
00303 subview_field<oT>::extract(field<oT>& actual_out, const subview_field<oT>& in)
00304   {
00305   arma_extra_debug_sigprint();
00306   
00307   //
00308   const bool alias = (&actual_out == &in.f);
00309   
00310   field<oT>* tmp = (alias) ? new field<oT> : 0;
00311   field<oT>& out = (alias) ? (*tmp)        : actual_out;
00312   
00313   //
00314   
00315   const uword n_rows = in.n_rows;
00316   const uword n_cols = in.n_cols;
00317   
00318   out.set_size(n_rows, n_cols);
00319   
00320   arma_extra_debug_print(arma_boost::format("out.n_rows = %d   out.n_cols = %d    in.m.n_rows = %d  in.m.n_cols = %d") % out.n_rows % out.n_cols % in.f.n_rows % in.f.n_cols );
00321   
00322   for(uword col = 0; col<n_cols; ++col)
00323     {
00324     for(uword row = 0; row<n_rows; ++row)
00325       {
00326       out.at(row,col) = in.at(row,col);
00327       }
00328     }
00329   
00330   
00331   if(alias)
00332     {
00333     actual_out = out;
00334     delete tmp;
00335     }
00336   
00337   }
00338 
00339 
00340 


armadillo_matrix
Author(s): Conrad Sanderson - NICTA (www.nicta.com.au), (Wrapper by Sjoerd van den Dries)
autogenerated on Tue Jan 7 2014 11:42:06