glue_join_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup glue_join
00018 //! @{
00019 
00020 
00021 
00022 template<typename T1, typename T2>
00023 inline
00024 void
00025 glue_join::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join>& X)
00026   {
00027   arma_extra_debug_sigprint();
00028   
00029   typedef typename T1::elem_type eT;
00030 
00031   const unwrap<T1> A_tmp(X.A);
00032   const unwrap<T2> B_tmp(X.B);
00033   
00034   const Mat<eT>& A = A_tmp.M;
00035   const Mat<eT>& B = B_tmp.M;
00036   
00037   const u32 join_type = X.aux_u32;
00038   
00039   
00040   if(A.n_elem == 0)
00041     {
00042     out = B;
00043     return;
00044     }
00045   
00046   if(B.n_elem == 0)
00047     {
00048     out = A;
00049     return;
00050     }
00051   
00052   
00053   if(join_type == 0)
00054     {
00055     arma_debug_check( (A.n_cols != B.n_cols), "join_cols(): number of columns must be the same" );
00056     }
00057   else
00058     {
00059     arma_debug_check( (A.n_rows != B.n_rows), "join_rows(): number of rows must be the same" );
00060     }
00061   
00062   
00063   
00064   if( (&out != &A) && (&out != &B) )
00065     {
00066     if(join_type == 0)   // join columns (i.e. result matrix has more rows)
00067       {
00068       out.set_size(A.n_rows + B.n_rows, A.n_cols);
00069       
00070       out.submat(0,        0, A.n_rows-1,   out.n_cols-1) = A;
00071       out.submat(A.n_rows, 0, out.n_rows-1, out.n_cols-1) = B;
00072       }
00073     else   // join rows  (i.e. result matrix has more columns)
00074       {
00075       out.set_size(A.n_rows, A.n_cols + B.n_cols);
00076       
00077       out.submat(0, 0,        out.n_rows-1, A.n_cols-1  ) = A;
00078       out.submat(0, A.n_cols, out.n_rows-1, out.n_cols-1) = B;
00079       }
00080     }
00081   else  // we have aliasing
00082     {
00083     Mat<eT> C;
00084     
00085     if(join_type == 0)
00086       {
00087       C.set_size(A.n_rows + B.n_rows, A.n_cols);
00088       
00089       C.submat(0,        0, A.n_rows-1, C.n_cols-1) = A;
00090       C.submat(A.n_rows, 0, C.n_rows-1, C.n_cols-1) = B;
00091       }
00092     else
00093       {
00094       C.set_size(A.n_rows, A.n_cols + B.n_cols);
00095       
00096       C.submat(0, 0,        C.n_rows-1, A.n_cols-1) = A;
00097       C.submat(0, A.n_cols, C.n_rows-1, C.n_cols-1) = B;
00098       }
00099     
00100     if(C.n_elem > sizeof(C.mem_local)/sizeof(eT))
00101       {
00102       out.reset();
00103       
00104       access::rw(out.n_elem) = C.n_elem;
00105       access::rw(out.n_rows) = C.n_rows;
00106       access::rw(out.n_cols) = C.n_cols;
00107       access::rw(out.mem   ) = C.mem;
00108       
00109       access::rw(C.n_elem) = 0;
00110       access::rw(C.n_rows) = 0;
00111       access::rw(C.n_cols) = 0;
00112       }
00113     else
00114       {
00115       out = C;
00116       }
00117     }
00118   
00119   }
00120 
00121 
00122 
00123 //! @}