injector_meat.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA (www.nicta.com.au)
00002 // Copyright (C) 2010 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 
00019 template<typename eT>
00020 inline
00021 mat_injector_row<eT>::mat_injector_row()
00022   : n_cols(0)
00023   {
00024   arma_extra_debug_sigprint();
00025   
00026   A.set_size( podarray_prealloc_n_elem::val );
00027   }
00028 
00029 
00030 
00031 template<typename eT>
00032 inline
00033 void
00034 mat_injector_row<eT>::insert(const eT val) const
00035   {
00036   arma_extra_debug_sigprint();
00037   
00038   if(n_cols < A.n_elem)
00039     {
00040     A[n_cols] = val;
00041     ++n_cols;
00042     }
00043   else
00044     {
00045     B.set_size(2 * A.n_elem);
00046     
00047     arrayops::copy(B.memptr(), A.memptr(), n_cols);
00048     
00049     B[n_cols] = val;
00050     ++n_cols;
00051     
00052     std::swap( access::rw(A.mem),    access::rw(B.mem)    );
00053     std::swap( access::rw(A.n_elem), access::rw(B.n_elem) );
00054     }
00055   }
00056 
00057 
00058 
00059 //
00060 //
00061 //
00062 
00063 
00064 
00065 template<typename T1>
00066 inline
00067 mat_injector<T1>::mat_injector(T1& in_X, const typename mat_injector<T1>::elem_type val)
00068   : X(in_X)
00069   , n_rows(1)
00070   {
00071   arma_extra_debug_sigprint();
00072   
00073   typedef typename mat_injector<T1>::elem_type eT;
00074   
00075   AA = new podarray< mat_injector_row<eT>* >;
00076   BB = new podarray< mat_injector_row<eT>* >;
00077   
00078   podarray< mat_injector_row<eT>* >& A = *AA;
00079   
00080   A.set_size(n_rows);
00081   
00082   for(uword row=0; row<n_rows; ++row)
00083     {
00084     A[row] = new mat_injector_row<eT>;
00085     }
00086   
00087   (*(A[0])).insert(val);
00088   }
00089 
00090 
00091 
00092 template<typename T1>
00093 inline
00094 mat_injector<T1>::mat_injector(T1& in_X, const injector_end_of_row& x)
00095   : X(in_X)
00096   , n_rows(1)
00097   {
00098   arma_extra_debug_sigprint();
00099   arma_ignore(x);
00100   
00101   typedef typename mat_injector<T1>::elem_type eT;
00102   
00103   AA = new podarray< mat_injector_row<eT>* >;
00104   BB = new podarray< mat_injector_row<eT>* >;
00105   
00106   podarray< mat_injector_row<eT>* >& A = *AA;
00107   
00108   A.set_size(n_rows);
00109   
00110   for(uword row=0; row<n_rows; ++row)
00111     {
00112     A[row] = new mat_injector_row<eT>;
00113     }
00114   
00115   (*this).end_of_row();
00116   }
00117 
00118 
00119 
00120 template<typename T1>
00121 inline
00122 mat_injector<T1>::~mat_injector()
00123   {
00124   arma_extra_debug_sigprint();
00125   
00126   typedef typename mat_injector<T1>::elem_type eT;
00127   
00128   podarray< mat_injector_row<eT>* >& A = *AA;
00129   
00130   if(n_rows > 0)
00131     {
00132     uword max_n_cols = (*(A[0])).n_cols;
00133     
00134     for(uword row=1; row<n_rows; ++row)
00135       {
00136       const uword n_cols = (*(A[row])).n_cols;
00137       
00138       if(max_n_cols < n_cols)
00139         {
00140         max_n_cols = n_cols;
00141         }
00142       }
00143     
00144     const uword max_n_rows = ((*(A[n_rows-1])).n_cols == 0) ? n_rows-1 : n_rows;
00145     
00146     if(is_Mat_only<T1>::value == true)
00147       {
00148       X.set_size(max_n_rows, max_n_cols);
00149       
00150       for(uword row=0; row<max_n_rows; ++row)
00151         {
00152         const uword n_cols = (*(A[row])).n_cols;
00153         
00154         for(uword col=0; col<n_cols; ++col)
00155           {
00156           X.at(row,col) = (*(A[row])).A[col];
00157           }
00158         
00159         for(uword col=n_cols; col<max_n_cols; ++col)
00160           {
00161           X.at(row,col) = eT(0);
00162           }
00163         }
00164       }
00165     else
00166     if(is_Row<T1>::value == true)
00167       {
00168       arma_debug_check( (max_n_rows > 1), "matrix initialisation: incompatible dimensions" );
00169       
00170       const uword n_cols = (*(A[0])).n_cols;
00171       
00172       X.set_size(1, n_cols);
00173       
00174       arrayops::copy( X.memptr(), (*(A[0])).A.memptr(), n_cols );
00175       }
00176     else
00177     if(is_Col<T1>::value == true)
00178       {
00179       const bool is_vec = ( (max_n_rows == 1) || (max_n_cols == 1) );
00180       
00181       arma_debug_check( (is_vec == false), "matrix initialisation: incompatible dimensions" );
00182       
00183       const uword n_elem = (std::max)(max_n_rows, max_n_cols);
00184       
00185       X.set_size(n_elem, 1);
00186       
00187       uword i = 0;
00188       for(uword row=0; row<max_n_rows; ++row)
00189         {
00190         const uword n_cols = (*(A[0])).n_cols;
00191         
00192         for(uword col=0; col<n_cols; ++col)
00193           {
00194           X[i] = (*(A[row])).A[col];
00195           ++i;
00196           }
00197         
00198         for(uword col=n_cols; col<max_n_cols; ++col)
00199           {
00200           X[i] = eT(0);
00201           ++i;
00202           }
00203         }
00204       }
00205     }
00206   
00207   for(uword row=0; row<n_rows; ++row)
00208     {
00209     delete A[row];
00210     }
00211     
00212   delete AA;
00213   delete BB;
00214   }
00215 
00216 
00217 
00218 template<typename T1>
00219 inline
00220 void
00221 mat_injector<T1>::insert(const typename mat_injector<T1>::elem_type val) const
00222   {
00223   arma_extra_debug_sigprint();
00224   
00225   typedef typename mat_injector<T1>::elem_type eT;
00226   
00227   podarray< mat_injector_row<eT>* >& A = *AA;
00228   
00229   (*(A[n_rows-1])).insert(val);
00230   }
00231 
00232 
00233 
00234 
00235 template<typename T1>
00236 inline
00237 void
00238 mat_injector<T1>::end_of_row() const
00239   {
00240   arma_extra_debug_sigprint();
00241   
00242   typedef typename mat_injector<T1>::elem_type eT;
00243   
00244   podarray< mat_injector_row<eT>* >& A = *AA;
00245   podarray< mat_injector_row<eT>* >& B = *BB;
00246   
00247   B.set_size( n_rows+1 );
00248   
00249   arrayops::copy(B.memptr(), A.memptr(), n_rows);
00250   
00251   for(uword row=n_rows; row<(n_rows+1); ++row)
00252     {
00253     B[row] = new mat_injector_row<eT>;
00254     }
00255   
00256   std::swap(AA, BB);
00257   
00258   n_rows += 1;
00259   }
00260 
00261 
00262 
00263 template<typename T1>
00264 arma_inline
00265 const mat_injector<T1>&
00266 operator<<(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
00267   {
00268   arma_extra_debug_sigprint();
00269   
00270   ref.insert(val);
00271   
00272   return ref;
00273   }
00274 
00275 
00276 
00277 template<typename T1>
00278 arma_inline
00279 const mat_injector<T1>&
00280 operator<<(const mat_injector<T1>& ref, const injector_end_of_row& x)
00281   {
00282   arma_extra_debug_sigprint();
00283   arma_ignore(x);
00284   
00285   ref.end_of_row();
00286   
00287   return ref;
00288   }
00289 
00290 
00291 
00297 // template<typename T1>
00298 // arma_inline
00299 // const mat_injector<T1>&
00300 // operator,(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
00301 //   {
00302 //   arma_extra_debug_sigprint();
00303 //   
00304 //   ref.insert(val);
00305 //   
00306 //   return ref;
00307 //   }
00308 
00309 
00310 
00311 // template<typename T1>
00312 // arma_inline
00313 // const mat_injector<T1>&
00314 // operator,(const mat_injector<T1>& ref, const injector_end_of_row& x)
00315 //   {
00316 //   arma_extra_debug_sigprint();
00317 //   arma_ignore(x);
00318 //   
00319 //   ref.end_of_row();
00320 //   
00321 //   return ref;
00322 //   }
00323 
00324 
00325 
00326 
00327 //
00328 //
00329 //
00330 
00331 
00332 
00333 template<typename oT>
00334 inline
00335 field_injector_row<oT>::field_injector_row()
00336   : n_cols(0)
00337   {
00338   arma_extra_debug_sigprint();
00339   
00340   AA = new field<oT>;
00341   BB = new field<oT>;
00342   
00343   field<oT>& A = *AA;
00344   
00345   A.set_size( field_prealloc_n_elem::val );
00346   }
00347 
00348 
00349 
00350 template<typename oT>
00351 inline
00352 field_injector_row<oT>::~field_injector_row()
00353   {
00354   arma_extra_debug_sigprint();
00355   
00356   delete AA;
00357   delete BB;
00358   }
00359 
00360 
00361 
00362 template<typename oT>
00363 inline
00364 void
00365 field_injector_row<oT>::insert(const oT& val) const
00366   {
00367   arma_extra_debug_sigprint();
00368   
00369   field<oT>& A = *AA;
00370   field<oT>& B = *BB;
00371   
00372   if(n_cols < A.n_elem)
00373     {
00374     A[n_cols] = val;
00375     ++n_cols;
00376     }
00377   else
00378     {
00379     B.set_size(2 * A.n_elem);
00380     
00381     for(uword i=0; i<n_cols; ++i)
00382       {
00383       B[i] = A[i];
00384       }
00385     
00386     B[n_cols] = val;
00387     ++n_cols;
00388     
00389     std::swap(AA, BB);
00390     }
00391   }
00392 
00393 
00394 
00395 //
00396 //
00397 //
00398 
00399 
00400 template<typename T1>
00401 inline
00402 field_injector<T1>::field_injector(T1& in_X, const typename field_injector<T1>::object_type& val)
00403   : X(in_X)
00404   , n_rows(1)
00405   {
00406   arma_extra_debug_sigprint();
00407   
00408   typedef typename field_injector<T1>::object_type oT;
00409   
00410   AA = new podarray< field_injector_row<oT>* >;
00411   BB = new podarray< field_injector_row<oT>* >;
00412   
00413   podarray< field_injector_row<oT>* >& A = *AA;
00414   
00415   A.set_size(n_rows);
00416   
00417   for(uword row=0; row<n_rows; ++row)
00418     {
00419     A[row] = new field_injector_row<oT>;
00420     }
00421   
00422   (*(A[0])).insert(val);
00423   }
00424 
00425 
00426 
00427 template<typename T1>
00428 inline
00429 field_injector<T1>::field_injector(T1& in_X, const injector_end_of_row& x)
00430   : X(in_X)
00431   , n_rows(1)
00432   {
00433   arma_extra_debug_sigprint();
00434   arma_ignore(x);
00435   
00436   typedef typename field_injector<T1>::object_type oT;
00437   
00438   AA = new podarray< field_injector_row<oT>* >;
00439   BB = new podarray< field_injector_row<oT>* >;
00440   
00441   podarray< field_injector_row<oT>* >& A = *AA;
00442   
00443   A.set_size(n_rows);
00444   
00445   for(uword row=0; row<n_rows; ++row)
00446     {
00447     A[row] = new field_injector_row<oT>;
00448     }
00449   
00450   (*this).end_of_row();
00451   }
00452 
00453 
00454 
00455 template<typename T1>
00456 inline
00457 field_injector<T1>::~field_injector()
00458   {
00459   arma_extra_debug_sigprint();
00460   
00461   typedef typename field_injector<T1>::object_type oT;
00462   
00463   podarray< field_injector_row<oT>* >& A = *AA;
00464   
00465   if(n_rows > 0)
00466     {
00467     uword max_n_cols = (*(A[0])).n_cols;
00468     
00469     for(uword row=1; row<n_rows; ++row)
00470       {
00471       const uword n_cols = (*(A[row])).n_cols;
00472       
00473       if(max_n_cols < n_cols)
00474         {
00475         max_n_cols = n_cols;
00476         }
00477       }
00478       
00479     const uword max_n_rows = ((*(A[n_rows-1])).n_cols == 0) ? n_rows-1 : n_rows;
00480     
00481     X.set_size(max_n_rows, max_n_cols);
00482     
00483     for(uword row=0; row<max_n_rows; ++row)
00484       {
00485       const uword n_cols = (*(A[row])).n_cols;
00486       
00487       for(uword col=0; col<n_cols; ++col)
00488         {
00489         const field<oT>& tmp = *((*(A[row])).AA);
00490         X.at(row,col) = tmp[col];
00491         }
00492       
00493       for(uword col=n_cols; col<max_n_cols; ++col)
00494         {
00495         X.at(row,col) = oT();
00496         }
00497       }
00498     }
00499   
00500   
00501   for(uword row=0; row<n_rows; ++row)
00502     {
00503     delete A[row];
00504     }
00505   
00506   delete AA;
00507   delete BB;
00508   }
00509 
00510 
00511 
00512 template<typename T1>
00513 inline
00514 void
00515 field_injector<T1>::insert(const typename field_injector<T1>::object_type& val) const
00516   {
00517   arma_extra_debug_sigprint();
00518   
00519   typedef typename field_injector<T1>::object_type oT;
00520   
00521   podarray< field_injector_row<oT>* >& A = *AA;
00522   
00523   (*(A[n_rows-1])).insert(val);
00524   }
00525 
00526 
00527 
00528 
00529 template<typename T1>
00530 inline
00531 void
00532 field_injector<T1>::end_of_row() const
00533   {
00534   arma_extra_debug_sigprint();
00535   
00536   typedef typename field_injector<T1>::object_type oT;
00537   
00538   podarray< field_injector_row<oT>* >& A = *AA;
00539   podarray< field_injector_row<oT>* >& B = *BB;
00540   
00541   B.set_size( n_rows+1 );
00542   
00543   for(uword row=0; row<n_rows; ++row)
00544     {
00545     B[row] = A[row];
00546     }
00547   
00548   for(uword row=n_rows; row<(n_rows+1); ++row)
00549     {
00550     B[row] = new field_injector_row<oT>;
00551     }
00552   
00553   std::swap(AA, BB);
00554   
00555   n_rows += 1;
00556   }
00557 
00558 
00559 
00560 template<typename T1>
00561 arma_inline
00562 const field_injector<T1>&
00563 operator<<(const field_injector<T1>& ref, const typename field_injector<T1>::object_type& val)
00564   {
00565   arma_extra_debug_sigprint();
00566   
00567   ref.insert(val);
00568   
00569   return ref;
00570   }
00571 
00572 
00573 
00574 template<typename T1>
00575 arma_inline
00576 const field_injector<T1>&
00577 operator<<(const field_injector<T1>& ref, const injector_end_of_row& x)
00578   {
00579   arma_extra_debug_sigprint();
00580   arma_ignore(x);
00581   
00582   ref.end_of_row();
00583   
00584   return ref;
00585   }
00586 
00587 
00588 


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:04