max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 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 subview_field max@0: //! @{ max@0: max@0: max@0: template max@0: inline max@0: subview_field::~subview_field() max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: subview_field::subview_field max@0: ( max@0: const field& in_f, max@0: const uword in_row1, max@0: const uword in_col1, max@0: const uword in_n_rows, max@0: const uword in_n_cols max@0: ) max@0: : f(in_f) max@0: , f_ptr(0) max@0: , aux_row1(in_row1) max@0: , aux_col1(in_col1) max@0: , n_rows(in_n_rows) max@0: , n_cols(in_n_cols) max@0: , n_elem(in_n_rows*in_n_cols) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: subview_field::subview_field max@0: ( max@0: field& in_f, max@0: const uword in_row1, max@0: const uword in_col1, max@0: const uword in_n_rows, max@0: const uword in_n_cols max@0: ) max@0: : f(in_f) max@0: , f_ptr(&in_f) max@0: , aux_row1(in_row1) max@0: , aux_col1(in_col1) max@0: , n_rows(in_n_rows) max@0: , n_cols(in_n_cols) max@0: , n_elem(in_n_rows*in_n_cols) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_field::operator= (const field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: subview_field& t = *this; max@0: max@0: arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions"); max@0: max@0: for(uword col=0; col max@0: inline max@0: void max@0: subview_field::operator= (const subview_field& x_in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const bool overlap = check_overlap(x_in); max@0: max@0: field* tmp_field = overlap ? new field(x_in.f) : 0; max@0: const subview_field* tmp_subview = overlap ? new subview_field(*tmp_field, x_in.aux_row1, x_in.aux_col1, x_in.n_rows, x_in.n_cols) : 0; max@0: const subview_field& x = overlap ? (*tmp_subview) : x_in; max@0: max@0: subview_field& t = *this; max@0: max@0: arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions"); max@0: max@0: for(uword col=0; col max@0: arma_inline max@0: oT& max@0: subview_field::operator[](const uword i) max@0: { max@0: arma_check( (f_ptr == 0), "subview_field::operator[]: field is read-only"); max@0: max@0: const uword in_col = i / n_rows; max@0: const uword in_row = i % n_rows; max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *((*f_ptr).mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const oT& max@0: subview_field::operator[](const uword i) const max@0: { max@0: const uword in_col = i / n_rows; max@0: const uword in_row = i % n_rows; max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *(f.mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: oT& max@0: subview_field::operator()(const uword i) max@0: { max@0: arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only"); max@0: arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds"); max@0: max@0: const uword in_col = i / n_rows; max@0: const uword in_row = i % n_rows; max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *((*f_ptr).mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const oT& max@0: subview_field::operator()(const uword i) const max@0: { max@0: arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds"); max@0: max@0: const uword in_col = i / n_rows; max@0: const uword in_row = i % n_rows; max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *(f.mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: oT& max@0: subview_field::operator()(const uword in_row, const uword in_col) max@0: { max@0: arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only"); max@0: arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds"); max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *((*f_ptr).mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const oT& max@0: subview_field::operator()(const uword in_row, const uword in_col) const max@0: { max@0: arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds"); max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *(f.mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: oT& max@0: subview_field::at(const uword in_row, const uword in_col) max@0: { max@0: //arma_extra_debug_sigprint(); max@0: max@0: arma_check( (f_ptr == 0), "subview_field::at(): field is read-only"); max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *((*f_ptr).mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: const oT& max@0: subview_field::at(const uword in_row, const uword in_col) const max@0: { max@0: //arma_extra_debug_sigprint(); max@0: max@0: const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row; max@0: max@0: return *(f.mem[index]); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: subview_field::check_overlap(const subview_field& x) const max@0: { max@0: const subview_field& t = *this; max@0: max@0: if(&t.f != &x.f) max@0: { max@0: return false; max@0: } max@0: else max@0: { max@0: if( (t.n_elem == 0) || (x.n_elem == 0) ) max@0: { max@0: return false; max@0: } max@0: else max@0: { max@0: const uword t_row_start = t.aux_row1; max@0: const uword t_row_end_p1 = t_row_start + t.n_rows; max@0: max@0: const uword t_col_start = t.aux_col1; max@0: const uword t_col_end_p1 = t_col_start + t.n_cols; max@0: max@0: max@0: const uword x_row_start = x.aux_row1; max@0: const uword x_row_end_p1 = x_row_start + x.n_rows; max@0: max@0: const uword x_col_start = x.aux_col1; max@0: const uword x_col_end_p1 = x_col_start + x.n_cols; max@0: max@0: max@0: const bool outside_rows = ( (x_row_start >= t_row_end_p1) || (t_row_start >= x_row_end_p1) ); max@0: const bool outside_cols = ( (x_col_start >= t_col_end_p1) || (t_col_start >= x_col_end_p1) ); max@0: max@0: return ( (outside_rows == false) && (outside_cols == false) ); max@0: } max@0: } max@0: } max@0: max@0: max@0: max@0: //! X = Y.subfield(...) max@0: template max@0: inline max@0: void max@0: subview_field::extract(field& actual_out, const subview_field& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: // max@0: const bool alias = (&actual_out == &in.f); max@0: max@0: field* tmp = (alias) ? new field : 0; max@0: field& out = (alias) ? (*tmp) : actual_out; max@0: max@0: // max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: out.set_size(n_rows, n_cols); max@0: max@0: arma_extra_debug_print(arma_boost::format("out.n_rows = %d out.n_cols = %d in.m.n_rows = %d in.m.n_cols = %d") % out.n_rows % out.n_cols % in.f.n_rows % in.f.n_cols ); max@0: max@0: for(uword col = 0; col