max@0: // Copyright (C) 2010 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2010 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup injector max@0: //! @{ max@0: max@0: max@0: max@0: template max@0: inline max@0: mat_injector_row::mat_injector_row() max@0: : n_cols(0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: A.set_size( podarray_prealloc_n_elem::val ); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: mat_injector_row::insert(const eT val) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(n_cols < A.n_elem) max@0: { max@0: A[n_cols] = val; max@0: ++n_cols; max@0: } max@0: else max@0: { max@0: B.set_size(2 * A.n_elem); max@0: max@0: arrayops::copy(B.memptr(), A.memptr(), n_cols); max@0: max@0: B[n_cols] = val; max@0: ++n_cols; max@0: max@0: std::swap( access::rw(A.mem), access::rw(B.mem) ); max@0: std::swap( access::rw(A.n_elem), access::rw(B.n_elem) ); max@0: } max@0: } max@0: max@0: max@0: max@0: // max@0: // max@0: // max@0: max@0: max@0: max@0: template max@0: inline max@0: mat_injector::mat_injector(T1& in_X, const typename mat_injector::elem_type val) max@0: : X(in_X) max@0: , n_rows(1) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename mat_injector::elem_type eT; max@0: max@0: AA = new podarray< mat_injector_row* >; max@0: BB = new podarray< mat_injector_row* >; max@0: max@0: podarray< mat_injector_row* >& A = *AA; max@0: max@0: A.set_size(n_rows); max@0: max@0: for(uword row=0; row; max@0: } max@0: max@0: (*(A[0])).insert(val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: mat_injector::mat_injector(T1& in_X, const injector_end_of_row& x) max@0: : X(in_X) max@0: , n_rows(1) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(x); max@0: max@0: typedef typename mat_injector::elem_type eT; max@0: max@0: AA = new podarray< mat_injector_row* >; max@0: BB = new podarray< mat_injector_row* >; max@0: max@0: podarray< mat_injector_row* >& A = *AA; max@0: max@0: A.set_size(n_rows); max@0: max@0: for(uword row=0; row; max@0: } max@0: max@0: (*this).end_of_row(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: mat_injector::~mat_injector() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename mat_injector::elem_type eT; max@0: max@0: podarray< mat_injector_row* >& A = *AA; max@0: max@0: if(n_rows > 0) max@0: { max@0: uword max_n_cols = (*(A[0])).n_cols; max@0: max@0: for(uword row=1; row::value == true) max@0: { max@0: X.set_size(max_n_rows, max_n_cols); max@0: max@0: for(uword row=0; row::value == true) max@0: { max@0: arma_debug_check( (max_n_rows > 1), "matrix initialisation: incompatible dimensions" ); max@0: max@0: const uword n_cols = (*(A[0])).n_cols; max@0: max@0: X.set_size(1, n_cols); max@0: max@0: arrayops::copy( X.memptr(), (*(A[0])).A.memptr(), n_cols ); max@0: } max@0: else max@0: if(is_Col::value == true) max@0: { max@0: const bool is_vec = ( (max_n_rows == 1) || (max_n_cols == 1) ); max@0: max@0: arma_debug_check( (is_vec == false), "matrix initialisation: incompatible dimensions" ); max@0: max@0: const uword n_elem = (std::max)(max_n_rows, max_n_cols); max@0: max@0: X.set_size(n_elem, 1); max@0: max@0: uword i = 0; max@0: for(uword row=0; row max@0: inline max@0: void max@0: mat_injector::insert(const typename mat_injector::elem_type val) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename mat_injector::elem_type eT; max@0: max@0: podarray< mat_injector_row* >& A = *AA; max@0: max@0: (*(A[n_rows-1])).insert(val); max@0: } max@0: max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: mat_injector::end_of_row() const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename mat_injector::elem_type eT; max@0: max@0: podarray< mat_injector_row* >& A = *AA; max@0: podarray< mat_injector_row* >& B = *BB; max@0: max@0: B.set_size( n_rows+1 ); max@0: max@0: arrayops::copy(B.memptr(), A.memptr(), n_rows); max@0: max@0: for(uword row=n_rows; row<(n_rows+1); ++row) max@0: { max@0: B[row] = new mat_injector_row; max@0: } max@0: max@0: std::swap(AA, BB); max@0: max@0: n_rows += 1; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const mat_injector& max@0: operator<<(const mat_injector& ref, const typename mat_injector::elem_type val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: ref.insert(val); max@0: max@0: return ref; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const mat_injector& max@0: operator<<(const mat_injector& ref, const injector_end_of_row& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(x); max@0: max@0: ref.end_of_row(); max@0: max@0: return ref; max@0: } max@0: max@0: max@0: max@0: //// using a mixture of operator << and , doesn't work yet max@0: //// e.g. A << 1, 2, 3 << endr max@0: //// in the above "3 << endr" requires special handling. max@0: //// similarly, special handling is necessary for "endr << 3" max@0: //// max@0: // template max@0: // arma_inline max@0: // const mat_injector& max@0: // operator,(const mat_injector& ref, const typename mat_injector::elem_type val) max@0: // { max@0: // arma_extra_debug_sigprint(); max@0: // max@0: // ref.insert(val); max@0: // max@0: // return ref; max@0: // } max@0: max@0: max@0: max@0: // template max@0: // arma_inline max@0: // const mat_injector& max@0: // operator,(const mat_injector& ref, const injector_end_of_row& x) max@0: // { max@0: // arma_extra_debug_sigprint(); max@0: // arma_ignore(x); max@0: // max@0: // ref.end_of_row(); max@0: // max@0: // return ref; max@0: // } max@0: max@0: max@0: max@0: max@0: // max@0: // max@0: // max@0: max@0: max@0: max@0: template max@0: inline max@0: field_injector_row::field_injector_row() max@0: : n_cols(0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: AA = new field; max@0: BB = new field; max@0: max@0: field& A = *AA; max@0: max@0: A.set_size( field_prealloc_n_elem::val ); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field_injector_row::~field_injector_row() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: delete AA; max@0: delete BB; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field_injector_row::insert(const oT& val) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: field& A = *AA; max@0: field& B = *BB; max@0: max@0: if(n_cols < A.n_elem) max@0: { max@0: A[n_cols] = val; max@0: ++n_cols; max@0: } max@0: else max@0: { max@0: B.set_size(2 * A.n_elem); max@0: max@0: for(uword i=0; i max@0: inline max@0: field_injector::field_injector(T1& in_X, const typename field_injector::object_type& val) max@0: : X(in_X) max@0: , n_rows(1) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename field_injector::object_type oT; max@0: max@0: AA = new podarray< field_injector_row* >; max@0: BB = new podarray< field_injector_row* >; max@0: max@0: podarray< field_injector_row* >& A = *AA; max@0: max@0: A.set_size(n_rows); max@0: max@0: for(uword row=0; row; max@0: } max@0: max@0: (*(A[0])).insert(val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field_injector::field_injector(T1& in_X, const injector_end_of_row& x) max@0: : X(in_X) max@0: , n_rows(1) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(x); max@0: max@0: typedef typename field_injector::object_type oT; max@0: max@0: AA = new podarray< field_injector_row* >; max@0: BB = new podarray< field_injector_row* >; max@0: max@0: podarray< field_injector_row* >& A = *AA; max@0: max@0: A.set_size(n_rows); max@0: max@0: for(uword row=0; row; max@0: } max@0: max@0: (*this).end_of_row(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field_injector::~field_injector() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename field_injector::object_type oT; max@0: max@0: podarray< field_injector_row* >& A = *AA; max@0: max@0: if(n_rows > 0) max@0: { max@0: uword max_n_cols = (*(A[0])).n_cols; max@0: max@0: for(uword row=1; row& tmp = *((*(A[row])).AA); max@0: X.at(row,col) = tmp[col]; max@0: } max@0: max@0: for(uword col=n_cols; col max@0: inline max@0: void max@0: field_injector::insert(const typename field_injector::object_type& val) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename field_injector::object_type oT; max@0: max@0: podarray< field_injector_row* >& A = *AA; max@0: max@0: (*(A[n_rows-1])).insert(val); max@0: } max@0: max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field_injector::end_of_row() const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename field_injector::object_type oT; max@0: max@0: podarray< field_injector_row* >& A = *AA; max@0: podarray< field_injector_row* >& B = *BB; max@0: max@0: B.set_size( n_rows+1 ); max@0: max@0: for(uword row=0; row; max@0: } max@0: max@0: std::swap(AA, BB); max@0: max@0: n_rows += 1; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const field_injector& max@0: operator<<(const field_injector& ref, const typename field_injector::object_type& val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: ref.insert(val); max@0: max@0: return ref; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const field_injector& max@0: operator<<(const field_injector& ref, const injector_end_of_row& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(x); max@0: max@0: ref.end_of_row(); max@0: max@0: return ref; max@0: } max@0: max@0: max@0: max@0: //! @}