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   if(join_type == 0)
00040     {
00041     arma_debug_check( (A.n_cols != B.n_cols), "join_cols(): number of columns must be the same" );
00042     }
00043   else
00044     {
00045     arma_debug_check( (A.n_rows != B.n_rows), "join_rows(): number of rows must be the same" );
00046     }
00047   
00048   
00049   
00050   if( (&out != &A) && (&out != &B) )
00051     {
00052     if(join_type == 0)   // join columns (i.e. result matrix has more rows)
00053       {
00054       out.set_size(A.n_rows + B.n_rows, A.n_cols);
00055       
00056       out.submat(0,        0, A.n_rows-1,   out.n_cols-1) = A;
00057       out.submat(A.n_rows, 0, out.n_rows-1, out.n_cols-1) = B;
00058       }
00059     else   // join rows  (i.e. result matrix has more columns)
00060       {
00061       out.set_size(A.n_rows, A.n_cols + B.n_cols);
00062       
00063       out.submat(0, 0,        out.n_rows-1, A.n_cols-1  ) = A;
00064       out.submat(0, A.n_cols, out.n_rows-1, out.n_cols-1) = B;
00065       }
00066     }
00067   else  // we have aliasing
00068     {
00069     Mat<eT> C;
00070     
00071     if(join_type == 0)
00072       {
00073       C.set_size(A.n_rows + B.n_rows, A.n_cols);
00074       
00075       C.submat(0,        0, A.n_rows-1, C.n_cols-1) = A;
00076       C.submat(A.n_rows, 0, C.n_rows-1, C.n_cols-1) = B;
00077       }
00078     else
00079       {
00080       C.set_size(A.n_rows, A.n_cols + B.n_cols);
00081       
00082       C.submat(0, 0,        C.n_rows-1, A.n_cols-1) = A;
00083       C.submat(0, A.n_cols, C.n_rows-1, C.n_cols-1) = B;
00084       }
00085     
00086     if(C.n_elem > sizeof(C.mem_local)/sizeof(eT))
00087       {
00088       out.reset();
00089       
00090       access::rw(out.n_elem) = C.n_elem;
00091       access::rw(out.n_rows) = C.n_rows;
00092       access::rw(out.n_cols) = C.n_cols;
00093       access::rw(out.mem   ) = C.mem;
00094       
00095       access::rw(C.n_elem) = 0;
00096       access::rw(C.n_rows) = 0;
00097       access::rw(C.n_cols) = 0;
00098       }
00099     else
00100       {
00101       out = C;
00102       }
00103     }
00104   
00105   }
00106 
00107 
00108 
00109 //! @}