max@0: // Copyright (C) 2008-2010 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-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 podarray max@0: //! @{ max@0: max@0: max@0: template max@0: inline max@0: podarray::~podarray() max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: if(n_elem > sizeof(mem_local)/sizeof(eT) ) max@0: { max@0: delete [] mem; max@0: } max@0: max@0: if(arma_config::debug == true) max@0: { max@0: access::rw(n_elem) = 0; max@0: access::rw(mem) = 0; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: podarray::podarray() max@0: : n_elem(0) max@0: , mem (0) max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: podarray::podarray(const podarray& x) max@0: : n_elem(0) max@0: , mem (0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: this->operator=(x); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: const podarray& max@0: podarray::operator=(const podarray& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(this != &x) max@0: { max@0: init(x.n_elem); max@0: max@0: arrayops::copy( memptr(), x.memptr(), n_elem ); max@0: } max@0: max@0: return *this; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: podarray::podarray(const uword new_n_elem) max@0: : n_elem(0) max@0: , mem (0) max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: init(new_n_elem); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: podarray::podarray(const eT* X, const uword new_n_elem) max@0: : n_elem(0) max@0: , mem (0) max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: init(new_n_elem); max@0: max@0: arrayops::copy( memptr(), X, new_n_elem ); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: podarray::operator[] (const uword i) const max@0: { max@0: return mem[i]; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: podarray::operator[] (const uword i) max@0: { max@0: return access::rw(mem[i]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: podarray::operator() (const uword i) const max@0: { max@0: arma_debug_check( (i >= n_elem), "podarray::operator(): index out of bounds"); max@0: return mem[i]; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: podarray::operator() (const uword i) max@0: { max@0: arma_debug_check( (i >= n_elem), "podarray::operator(): index out of bounds"); max@0: return access::rw(mem[i]); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: podarray::set_size(const uword new_n_elem) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: init(new_n_elem); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: podarray::reset() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: init(0); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: podarray::fill(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arrayops::inplace_set(memptr(), val, n_elem); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: podarray::zeros() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: fill(eT(0)); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: podarray::zeros(const uword new_n_elem) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: init(new_n_elem); max@0: fill(eT(0)); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT* max@0: podarray::memptr() max@0: { max@0: return const_cast(mem); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const eT* max@0: podarray::memptr() const max@0: { max@0: return mem; max@0: } max@0: max@0: max@0: max@0: template max@0: arma_hot max@0: inline max@0: void max@0: podarray::copy_row(const Mat& A, const uword row) max@0: { max@0: const uword cols = A.n_cols; max@0: max@0: // note: this function assumes that the podarray has been set to the correct size beforehand max@0: eT* out = memptr(); max@0: max@0: switch(cols) max@0: { max@0: default: max@0: { max@0: uword i,j; max@0: for(i=0, j=1; j < cols; i+=2, j+=2) max@0: { max@0: const eT tmp_i = A.at(row, i); max@0: const eT tmp_j = A.at(row, j); max@0: max@0: out[i] = tmp_i; max@0: out[j] = tmp_j; max@0: } max@0: max@0: if(i < cols) max@0: { max@0: out[i] = A.at(row, i); max@0: } max@0: } max@0: break; max@0: max@0: case 8: max@0: out[7] = A.at(row, 7); max@0: max@0: case 7: max@0: out[6] = A.at(row, 6); max@0: max@0: case 6: max@0: out[5] = A.at(row, 5); max@0: max@0: case 5: max@0: out[4] = A.at(row, 4); max@0: max@0: case 4: max@0: out[3] = A.at(row, 3); max@0: max@0: case 3: max@0: out[2] = A.at(row, 2); max@0: max@0: case 2: max@0: out[1] = A.at(row, 1); max@0: max@0: case 1: max@0: out[0] = A.at(row, 0); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: podarray::init(const uword new_n_elem) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(n_elem == new_n_elem) max@0: { max@0: return; max@0: } max@0: max@0: if(n_elem > sizeof(mem_local)/sizeof(eT) ) max@0: { max@0: delete [] mem; max@0: } max@0: max@0: if(new_n_elem <= sizeof(mem_local)/sizeof(eT) ) max@0: { max@0: access::rw(mem) = mem_local; max@0: } max@0: else max@0: { max@0: access::rw(mem) = new eT[new_n_elem]; max@0: } max@0: max@0: access::rw(n_elem) = new_n_elem; max@0: } max@0: max@0: max@0: max@0: //! @}