max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson max@0: // Copyright (C) 2009-2010 Ian Cullinan 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 field max@0: //! @{ max@0: max@0: max@0: template max@0: inline max@0: field::~field() max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: delete_objects(); max@0: max@0: if(n_elem > sizeof(mem_local)/sizeof(oT*) ) max@0: { max@0: delete [] mem; max@0: } max@0: max@0: if(arma_config::debug == true) max@0: { max@0: // try to expose buggy user code that accesses deleted objects max@0: access::rw(n_rows) = 0; max@0: access::rw(n_cols) = 0; max@0: access::rw(n_elem) = 0; max@0: mem = 0; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field::field() max@0: : n_rows(0) max@0: , n_cols(0) 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: //! construct a field from a given field max@0: template max@0: inline max@0: field::field(const field& x) max@0: : n_rows(0) max@0: , n_cols(0) max@0: , n_elem(0) max@0: , mem(0) max@0: { max@0: arma_extra_debug_sigprint(arma_boost::format("this = %x x = %x") % this % &x); max@0: max@0: init(x); max@0: } max@0: max@0: max@0: max@0: //! construct a field from a given field max@0: template max@0: inline max@0: const field& max@0: field::operator=(const field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: init(x); max@0: return *this; max@0: } max@0: max@0: max@0: max@0: //! construct a field from subview_field (e.g. construct a field from a delayed subfield operation) max@0: template max@0: inline max@0: field::field(const subview_field& X) max@0: : n_rows(0) max@0: , n_cols(0) max@0: , n_elem(0) max@0: , mem(0) max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: this->operator=(X); max@0: } max@0: max@0: max@0: max@0: //! construct a field from subview_field (e.g. construct a field from a delayed subfield operation) max@0: template max@0: inline max@0: const field& max@0: field::operator=(const subview_field& X) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: subview_field::extract(*this, X); max@0: return *this; max@0: } max@0: max@0: max@0: max@0: //! construct the field with the specified number of elements, max@0: //! assuming a column-major layout max@0: template max@0: inline max@0: field::field(const uword n_elem_in) max@0: : n_rows(0) max@0: , n_cols(0) max@0: , n_elem(0) max@0: , mem(0) max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: init(n_elem_in, 1); max@0: } max@0: max@0: max@0: max@0: //! construct the field with the specified dimensions max@0: template max@0: inline max@0: field::field(const uword n_rows_in, const uword n_cols_in) max@0: : n_rows(0) max@0: , n_cols(0) max@0: , n_elem(0) max@0: , mem(0) max@0: { max@0: arma_extra_debug_sigprint_this(this); max@0: max@0: init(n_rows_in, n_cols_in); max@0: } max@0: max@0: max@0: max@0: //! change the field to have the specified number of elements, max@0: //! assuming a column-major layout (data is not preserved) max@0: template max@0: inline max@0: void max@0: field::set_size(const uword n_elem_in) max@0: { max@0: arma_extra_debug_sigprint(arma_boost::format("n_elem_in = %d") % n_elem_in); max@0: max@0: init(n_elem_in, 1); max@0: } max@0: max@0: max@0: max@0: //! change the field to have the specified dimensions (data is not preserved) max@0: template max@0: inline max@0: void max@0: field::set_size(const uword n_rows_in, const uword n_cols_in) max@0: { max@0: arma_extra_debug_sigprint(arma_boost::format("n_rows_in = %d, n_cols_in = %d") % n_rows_in % n_cols_in); max@0: max@0: init(n_rows_in, n_cols_in); max@0: } max@0: max@0: max@0: max@0: //! change the field to have the specified dimensions (data is not preserved) max@0: template max@0: template max@0: inline max@0: void max@0: field::copy_size(const field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: init(x.n_rows, x.n_cols); max@0: } max@0: max@0: max@0: max@0: //! linear element accessor (treats the field as a vector); no bounds check max@0: template max@0: arma_inline max@0: oT& max@0: field::operator[] (const uword i) max@0: { max@0: return (*mem[i]); max@0: } max@0: max@0: max@0: max@0: //! linear element accessor (treats the field as a vector); no bounds check max@0: template max@0: arma_inline max@0: const oT& max@0: field::operator[] (const uword i) const max@0: { max@0: return (*mem[i]); max@0: } max@0: max@0: max@0: max@0: //! linear element accessor (treats the field as a vector); no bounds check max@0: template max@0: arma_inline max@0: oT& max@0: field::at(const uword i) max@0: { max@0: return (*mem[i]); max@0: } max@0: max@0: max@0: max@0: //! linear element accessor (treats the field as a vector); no bounds check max@0: template max@0: arma_inline max@0: const oT& max@0: field::at(const uword i) const max@0: { max@0: return (*mem[i]); max@0: } max@0: max@0: max@0: max@0: //! linear element accessor (treats the field as a vector); bounds checking not done when ARMA_NO_DEBUG is defined max@0: template max@0: arma_inline max@0: oT& max@0: field::operator() (const uword i) max@0: { max@0: arma_debug_check( (i >= n_elem), "field::operator(): index out of bounds"); max@0: return (*mem[i]); max@0: } max@0: max@0: max@0: max@0: //! linear element accessor (treats the field as a vector); bounds checking not done when ARMA_NO_DEBUG is defined max@0: template max@0: arma_inline max@0: const oT& max@0: field::operator() (const uword i) const max@0: { max@0: arma_debug_check( (i >= n_elem), "field::operator(): index out of bounds"); max@0: return (*mem[i]); max@0: } max@0: max@0: max@0: max@0: //! element accessor; bounds checking not done when ARMA_NO_DEBUG is defined max@0: template max@0: arma_inline max@0: oT& max@0: field::operator() (const uword in_row, const uword in_col) max@0: { max@0: arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "field::operator(): index out of bounds"); max@0: return (*mem[in_row + in_col*n_rows]); max@0: } max@0: max@0: max@0: max@0: //! element accessor; bounds checking not done when ARMA_NO_DEBUG is defined max@0: template max@0: arma_inline max@0: const oT& max@0: 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)), "field::operator(): index out of bounds"); max@0: return (*mem[in_row + in_col*n_rows]); max@0: } max@0: max@0: max@0: max@0: //! element accessor; no bounds check max@0: template max@0: arma_inline max@0: oT& max@0: field::at(const uword in_row, const uword in_col) max@0: { max@0: return (*mem[in_row + in_col*n_rows]); max@0: } max@0: max@0: max@0: max@0: //! element accessor; no bounds check max@0: template max@0: arma_inline max@0: const oT& max@0: field::at(const uword in_row, const uword in_col) const max@0: { max@0: return (*mem[in_row + in_col*n_rows]); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field_injector< field > max@0: field::operator<<(const oT& val) max@0: { max@0: return field_injector< field >(*this, val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field_injector< field > max@0: field::operator<<(const injector_end_of_row& x) max@0: { max@0: return field_injector< field >(*this, x); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (row of a field) max@0: template max@0: inline max@0: subview_field max@0: field::row(const uword row_num) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check( (row_num >= n_rows), "field::row(): row out of bounds" ); max@0: max@0: return subview_field(*this, row_num, 0, 1, n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (row of a field) max@0: template max@0: inline max@0: const subview_field max@0: field::row(const uword row_num) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check( (row_num >= n_rows), "field::row(): row out of bounds" ); max@0: max@0: return subview_field(*this, row_num, 0, 1, n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (column of a field) max@0: template max@0: inline max@0: subview_field max@0: field::col(const uword col_num) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check( (col_num >= n_cols), "field::col(): out of bounds"); max@0: max@0: return subview_field(*this, 0, col_num, n_rows, 1); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (column of a field) max@0: template max@0: inline max@0: const subview_field max@0: field::col(const uword col_num) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check( (col_num >= n_cols), "field::col(): out of bounds"); max@0: max@0: return subview_field(*this, 0, col_num, n_rows, 1); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield comprised of specified rows) max@0: template max@0: inline max@0: subview_field max@0: field::rows(const uword in_row1, const uword in_row2) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check max@0: ( max@0: ( (in_row1 > in_row2) || (in_row2 >= n_rows) ), max@0: "field::rows(): indicies out of bounds or incorrectly used" max@0: ); max@0: max@0: const uword sub_n_rows = in_row2 - in_row1 + 1; max@0: max@0: return subview_field(*this, in_row1, 0, sub_n_rows, n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield comprised of specified rows) max@0: template max@0: inline max@0: const subview_field max@0: field::rows(const uword in_row1, const uword in_row2) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check max@0: ( max@0: ( (in_row1 > in_row2) || (in_row2 >= n_rows) ), max@0: "field::rows(): indicies out of bounds or incorrectly used" max@0: ); max@0: max@0: const uword sub_n_rows = in_row2 - in_row1 + 1; max@0: max@0: return subview_field(*this, in_row1, 0, sub_n_rows, n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield comprised of specified columns) max@0: template max@0: inline max@0: subview_field max@0: field::cols(const uword in_col1, const uword in_col2) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check max@0: ( max@0: ( (in_col1 > in_col2) || (in_col2 >= n_cols) ), max@0: "field::cols(): indicies out of bounds or incorrectly used" max@0: ); max@0: max@0: const uword sub_n_cols = in_col2 - in_col1 + 1; max@0: max@0: return subview_field(*this, 0, in_col1, n_rows, sub_n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield comprised of specified columns) max@0: template max@0: inline max@0: const subview_field max@0: field::cols(const uword in_col1, const uword in_col2) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check max@0: ( max@0: ( (in_col1 > in_col2) || (in_col2 >= n_cols) ), max@0: "field::cols(): indicies out of bounds or incorrectly used" max@0: ); max@0: max@0: const uword sub_n_cols = in_col2 - in_col1 + 1; max@0: max@0: return subview_field(*this, 0, in_col1, n_rows, sub_n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield with arbitrary dimensions) max@0: template max@0: inline max@0: subview_field max@0: field::subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check max@0: ( max@0: (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols), max@0: "field::subfield(): indices out of bounds or incorrectly used" max@0: ); max@0: max@0: const uword sub_n_rows = in_row2 - in_row1 + 1; max@0: const uword sub_n_cols = in_col2 - in_col1 + 1; max@0: max@0: return subview_field(*this, in_row1, in_col1, sub_n_rows, sub_n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield with arbitrary dimensions) max@0: template max@0: inline max@0: const subview_field max@0: field::subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_check max@0: ( max@0: (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols), max@0: "field::subfield(): indices out of bounds or incorrectly used" max@0: ); max@0: max@0: const uword sub_n_rows = in_row2 - in_row1 + 1; max@0: const uword sub_n_cols = in_col2 - in_col1 + 1; max@0: max@0: return subview_field(*this, in_row1, in_col1, sub_n_rows, sub_n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield with arbitrary dimensions) max@0: template max@0: inline max@0: subview_field max@0: field::subfield(const span& row_span, const span& col_span) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const bool row_all = row_span.whole; max@0: const bool col_all = col_span.whole; max@0: max@0: const uword local_n_rows = n_rows; max@0: const uword local_n_cols = n_cols; max@0: max@0: const uword in_row1 = row_all ? 0 : row_span.a; max@0: const uword in_row2 = row_span.b; max@0: const uword sub_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1; max@0: max@0: const uword in_col1 = col_all ? 0 : col_span.a; max@0: const uword in_col2 = col_span.b; max@0: const uword sub_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1; max@0: max@0: arma_debug_check max@0: ( max@0: ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) ) max@0: || max@0: ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) ) max@0: , max@0: "field::subfield(): indices out of bounds or incorrectly used" max@0: ); max@0: max@0: return subview_field(*this, in_row1, in_col1, sub_n_rows, sub_n_cols); max@0: } max@0: max@0: max@0: max@0: //! creation of subview_field (subfield with arbitrary dimensions) max@0: template max@0: inline max@0: const subview_field max@0: field::subfield(const span& row_span, const span& col_span) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const bool row_all = row_span.whole; max@0: const bool col_all = col_span.whole; max@0: max@0: const uword local_n_rows = n_rows; max@0: const uword local_n_cols = n_cols; max@0: max@0: const uword in_row1 = row_all ? 0 : row_span.a; max@0: const uword in_row2 = row_span.b; max@0: const uword sub_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1; max@0: max@0: const uword in_col1 = col_all ? 0 : col_span.a; max@0: const uword in_col2 = col_span.b; max@0: const uword sub_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1; max@0: max@0: arma_debug_check max@0: ( max@0: ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) ) max@0: || max@0: ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) ) max@0: , max@0: "field::subfield(): indices out of bounds or incorrectly used" max@0: ); max@0: max@0: return subview_field(*this, in_row1, in_col1, sub_n_rows, sub_n_cols); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: subview_field max@0: field::operator()(const span& row_span, const span& col_span) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (*this).subfield(row_span, col_span); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: const subview_field max@0: field::operator()(const span& row_span, const span& col_span) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (*this).subfield(row_span, col_span); max@0: } max@0: max@0: max@0: max@0: //! print contents of the field (to the cout stream), max@0: //! optionally preceding with a user specified line of text. max@0: //! the field class preserves the stream's flags max@0: //! but the associated operator<< function for type oT max@0: //! may still modify the stream's parameters. max@0: //! NOTE: this function assumes that type oT can be printed, max@0: //! i.e. the function "std::ostream& operator<< (std::ostream&, const oT&)" max@0: //! has been defined. max@0: max@0: template max@0: inline max@0: void max@0: field::print(const std::string extra_text) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(extra_text.length() != 0) max@0: { max@0: const std::streamsize orig_width = ARMA_DEFAULT_OSTREAM.width(); max@0: max@0: ARMA_DEFAULT_OSTREAM << extra_text << '\n'; max@0: max@0: ARMA_DEFAULT_OSTREAM.width(orig_width); max@0: } max@0: max@0: arma_ostream::print(ARMA_DEFAULT_OSTREAM, *this); max@0: } max@0: max@0: max@0: max@0: //! print contents of the field to a user specified stream, max@0: //! optionally preceding with a user specified line of text. max@0: //! the field class preserves the stream's flags max@0: //! but the associated operator<< function for type oT max@0: //! may still modify the stream's parameters. max@0: //! NOTE: this function assumes that type oT can be printed, max@0: //! i.e. the function "std::ostream& operator<< (std::ostream&, const oT&)" max@0: //! has been defined. max@0: max@0: template max@0: inline max@0: void max@0: field::print(std::ostream& user_stream, const std::string extra_text) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(extra_text.length() != 0) max@0: { max@0: const std::streamsize orig_width = user_stream.width(); max@0: max@0: user_stream << extra_text << '\n'; max@0: max@0: user_stream.width(orig_width); max@0: } max@0: max@0: arma_ostream::print(user_stream, *this); max@0: } max@0: max@0: max@0: max@0: //! fill the field with an object max@0: template max@0: inline max@0: void max@0: field::fill(const oT& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: field& t = *this; max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: field::reset() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: init(0,0); max@0: } max@0: max@0: max@0: max@0: //! reset each object max@0: template max@0: inline max@0: void max@0: field::reset_objects() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: field_aux::reset_objects(*this); max@0: } max@0: max@0: max@0: max@0: //! returns true if the field has no objects max@0: template max@0: arma_inline max@0: bool max@0: field::is_empty() const max@0: { max@0: return (n_elem == 0); max@0: } max@0: max@0: max@0: max@0: //! returns true if the given index is currently in range max@0: template max@0: arma_inline max@0: arma_warn_unused max@0: bool max@0: field::in_range(const uword i) const max@0: { max@0: return (i < n_elem); max@0: } max@0: max@0: max@0: max@0: //! returns true if the given start and end indices are currently in range max@0: template max@0: arma_inline max@0: arma_warn_unused max@0: bool max@0: field::in_range(const span& x) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(x.whole == true) max@0: { max@0: return true; max@0: } max@0: else max@0: { max@0: const uword a = x.a; max@0: const uword b = x.b; max@0: max@0: return ( (a <= b) && (b < n_elem) ); max@0: } max@0: } max@0: max@0: max@0: max@0: //! returns true if the given location is currently in range max@0: template max@0: arma_inline max@0: arma_warn_unused max@0: bool max@0: field::in_range(const uword in_row, const uword in_col) const max@0: { max@0: return ( (in_row < n_rows) && (in_col < n_cols) ); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: arma_warn_unused max@0: bool max@0: field::in_range(const span& row_span, const uword in_col) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(row_span.whole == true) max@0: { max@0: return (in_col < n_cols); max@0: } max@0: else max@0: { max@0: const uword in_row1 = row_span.a; max@0: const uword in_row2 = row_span.b; max@0: max@0: return ( (in_row1 <= in_row2) && (in_row2 < n_rows) && (in_col < n_cols) ); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: arma_warn_unused max@0: bool max@0: field::in_range(const uword in_row, const span& col_span) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(col_span.whole == true) max@0: { max@0: return (in_row < n_rows); max@0: } max@0: else max@0: { max@0: const uword in_col1 = col_span.a; max@0: const uword in_col2 = col_span.b; max@0: max@0: return ( (in_row < n_rows) && (in_col1 <= in_col2) && (in_col2 < n_cols) ); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: arma_warn_unused max@0: bool max@0: field::in_range(const span& row_span, const span& col_span) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword in_row1 = row_span.a; max@0: const uword in_row2 = row_span.b; max@0: max@0: const uword in_col1 = col_span.a; max@0: const uword in_col2 = col_span.b; max@0: max@0: const bool rows_ok = row_span.whole ? true : ( (in_row1 <= in_row2) && (in_row2 < n_rows) ); max@0: const bool cols_ok = col_span.whole ? true : ( (in_col1 <= in_col2) && (in_col2 < n_cols) ); max@0: max@0: return ( (rows_ok == true) && (cols_ok == true) ); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::save(const std::string name, const file_type type, const bool print_status) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: std::string err_msg; max@0: const bool save_okay = field_aux::save(*this, name, type, err_msg); max@0: max@0: if( (print_status == true) && (save_okay == false) ) max@0: { max@0: if(err_msg.length() > 0) max@0: { max@0: arma_warn(true, "field::save(): ", err_msg, name); max@0: } max@0: else max@0: { max@0: arma_warn(true, "field::save(): couldn't write to ", name); max@0: } max@0: } max@0: max@0: return save_okay; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::save(std::ostream& os, const file_type type, const bool print_status) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: std::string err_msg; max@0: const bool save_okay = field_aux::save(*this, os, type, err_msg); max@0: max@0: if( (print_status == true) && (save_okay == false) ) max@0: { max@0: if(err_msg.length() > 0) max@0: { max@0: arma_warn(true, "field::save(): ", err_msg, "[ostream]"); max@0: } max@0: else max@0: { max@0: arma_warn(true, "field::save(): couldn't write to [ostream]"); max@0: } max@0: } max@0: max@0: return save_okay; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::load(const std::string name, const file_type type, const bool print_status) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: std::string err_msg; max@0: const bool load_okay = field_aux::load(*this, name, type, err_msg); max@0: max@0: if( (print_status == true) && (load_okay == false) ) max@0: { max@0: if(err_msg.length() > 0) max@0: { max@0: arma_warn(true, "field::load(): ", err_msg, name); max@0: } max@0: else max@0: { max@0: arma_warn(true, "field::load(): couldn't read from ", name); max@0: } max@0: } max@0: max@0: if(load_okay == false) max@0: { max@0: (*this).reset(); max@0: } max@0: max@0: return load_okay; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::load(std::istream& is, const file_type type, const bool print_status) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: std::string err_msg; max@0: const bool load_okay = field_aux::load(*this, is, type, err_msg); max@0: max@0: if( (print_status == true) && (load_okay == false) ) max@0: { max@0: if(err_msg.length() > 0) max@0: { max@0: arma_warn(true, "field::load(): ", err_msg, "[istream]"); max@0: } max@0: else max@0: { max@0: arma_warn(true, "field::load(): couldn't read from [istream]"); max@0: } max@0: } max@0: max@0: if(load_okay == false) max@0: { max@0: (*this).reset(); max@0: } max@0: max@0: return load_okay; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::quiet_save(const std::string name, const file_type type) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (*this).save(name, type, false); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::quiet_save(std::ostream& os, const file_type type) const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (*this).save(os, type, false); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::quiet_load(const std::string name, const file_type type) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (*this).load(name, type, false); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::quiet_load(std::istream& is, const file_type type) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (*this).load(is, type, false); max@0: } max@0: max@0: max@0: max@0: //! construct a field from a given field max@0: template max@0: inline max@0: void max@0: field::init(const field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(this != &x) max@0: { max@0: init(x.n_rows, x.n_cols); max@0: max@0: field& t = *this; max@0: max@0: for(uword col=0; col max@0: inline max@0: void max@0: field::init(const uword n_rows_in, const uword n_cols_in) max@0: { max@0: arma_extra_debug_sigprint( arma_boost::format("n_rows_in = %d, n_cols_in = %d") % n_rows_in % n_cols_in ); max@0: max@0: const uword n_elem_new = n_rows_in * n_cols_in; max@0: max@0: if(n_elem == n_elem_new) max@0: { max@0: // delete_objects(); max@0: // create_objects(); max@0: access::rw(n_rows) = n_rows_in; max@0: access::rw(n_cols) = n_cols_in; max@0: } max@0: else max@0: { max@0: delete_objects(); max@0: max@0: if(n_elem > sizeof(mem_local)/sizeof(oT*) ) max@0: { max@0: delete [] mem; max@0: } max@0: max@0: if(n_elem_new <= sizeof(mem_local)/sizeof(oT*) ) max@0: { max@0: mem = mem_local; max@0: } max@0: else max@0: { max@0: mem = new(std::nothrow) oT* [n_elem_new]; max@0: arma_check_bad_alloc( (mem == 0), "field::init(): out of memory" ); max@0: } max@0: max@0: access::rw(n_elem) = n_elem_new; max@0: max@0: if(n_elem_new == 0) max@0: { max@0: access::rw(n_rows) = 0; max@0: access::rw(n_cols) = 0; max@0: } max@0: else max@0: { max@0: access::rw(n_rows) = n_rows_in; max@0: access::rw(n_cols) = n_cols_in; max@0: } max@0: max@0: create_objects(); max@0: max@0: } max@0: max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field::delete_objects() max@0: { max@0: arma_extra_debug_sigprint( arma_boost::format("n_elem = %d") % n_elem ); max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: field::create_objects() max@0: { max@0: arma_extra_debug_sigprint( arma_boost::format("n_elem = %d") % n_elem ); max@0: max@0: for(uword i=0; i max@0: inline max@0: field::iterator::iterator(field& in_M, const bool at_end) max@0: : M(in_M) max@0: , i( (at_end == false) ? 0 : in_M.n_elem ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: oT& max@0: field::iterator::operator*() max@0: { max@0: return M[i]; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::iterator& max@0: field::iterator::operator++() max@0: { max@0: ++i; max@0: max@0: return *this; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field::iterator::operator++(int) max@0: { max@0: operator++(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::iterator& max@0: field::iterator::operator--() max@0: { max@0: if(i > 0) max@0: { max@0: --i; max@0: } max@0: max@0: return *this; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field::iterator::operator--(int) max@0: { max@0: operator--(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::iterator::operator!=(const typename field::iterator& X) const max@0: { max@0: return (i != X.i); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::iterator::operator==(const typename field::iterator& X) const max@0: { max@0: return (i == X.i); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field::const_iterator::const_iterator(const field& in_M, const bool at_end) max@0: : M(in_M) max@0: , i( (at_end == false) ? 0 : in_M.n_elem ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: field::const_iterator::const_iterator(const typename field::iterator& X) max@0: : M(X.M) max@0: , i(X.i) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: const oT& max@0: field::const_iterator::operator*() const max@0: { max@0: return M[i]; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::const_iterator& max@0: field::const_iterator::operator++() max@0: { max@0: ++i; max@0: max@0: return *this; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field::const_iterator::operator++(int) max@0: { max@0: operator++(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::const_iterator& max@0: field::const_iterator::operator--() max@0: { max@0: if(i > 0) max@0: { max@0: --i; max@0: } max@0: max@0: return *this; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field::const_iterator::operator--(int) max@0: { max@0: operator--(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::const_iterator::operator!=(const typename field::const_iterator& X) const max@0: { max@0: return (i != X.i); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field::const_iterator::operator==(const typename field::const_iterator& X) const max@0: { max@0: return (i == X.i); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::iterator max@0: field::begin() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return field::iterator(*this); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::const_iterator max@0: field::begin() const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return field::const_iterator(*this); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::iterator max@0: field::end() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return field::iterator(*this, true); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename field::const_iterator max@0: field::end() const max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return field::const_iterator(*this, true); 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: void max@0: field_aux::reset_objects(field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: x.delete_objects(); max@0: x.create_objects(); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: field_aux::reset_objects(field< Mat >& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: field_aux::reset_objects(field< Col >& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: field_aux::reset_objects(field< Row >& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: field_aux::reset_objects(field< Cube >& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: for(uword i=0; i& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: for(uword i=0; i max@0: inline max@0: bool max@0: field_aux::save(const field& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = "; max@0: max@0: return false; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field& x, std::ostream& os, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = "; max@0: max@0: return false; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = "; max@0: max@0: return false; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field& x, std::istream& is, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = "; max@0: max@0: return false; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Mat >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, name); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, name); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Mat >& x, std::ostream& os, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, os); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, os); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Mat >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, name, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, name, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, name, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Mat >& x, std::istream& is, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, is, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, is, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, is, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Col >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, name); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, name); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Col >& x, std::ostream& os, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, os); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, os); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Col >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, name, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, name, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, name, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Col >& x, std::istream& is, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, is, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, is, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, is, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Row >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, name, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, name, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Row >& x, std::ostream& os, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, os, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, os, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Row >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, name, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, name, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, name, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Row >& x, std::istream& is, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, is, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, is, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, is, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Cube >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, name, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, name, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::save(const field< Cube >& x, std::ostream& os, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case arma_binary: max@0: return diskio::save_arma_binary(x, os, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::save_ppm_binary(x, os, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Cube >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, name, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, name, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, name, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: field_aux::load(field< Cube >& x, std::istream& is, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(type) max@0: { max@0: case auto_detect: max@0: return diskio::load_auto_detect(x, is, err_msg); max@0: break; max@0: max@0: case arma_binary: max@0: return diskio::load_arma_binary(x, is, err_msg); max@0: break; max@0: max@0: case ppm_binary: max@0: return diskio::load_ppm_binary(x, is, err_msg); max@0: break; max@0: max@0: default: max@0: err_msg = " [unsupported type] filename = "; max@0: return false; max@0: } max@0: } max@0: max@0: max@0: max@0: inline max@0: bool max@0: field_aux::save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_ignore(type); max@0: max@0: err_msg.clear(); max@0: max@0: return diskio::save_std_string(x, name); max@0: } max@0: max@0: max@0: max@0: inline max@0: bool max@0: field_aux::save(const field< std::string >& x, std::ostream& os, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_ignore(type); max@0: max@0: err_msg.clear(); max@0: max@0: return diskio::save_std_string(x, os); max@0: } max@0: max@0: max@0: max@0: inline max@0: bool max@0: field_aux::load(field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_ignore(type); max@0: max@0: return diskio::load_std_string(x, name, err_msg); max@0: } max@0: max@0: max@0: max@0: inline max@0: bool max@0: field_aux::load(field< std::string >& x, std::istream& is, const file_type type, std::string& err_msg) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_ignore(type); max@0: max@0: return diskio::load_std_string(x, is, err_msg); max@0: } max@0: max@0: max@0: max@0: #ifdef ARMA_EXTRA_FIELD_MEAT max@0: #include ARMA_INCFILE_WRAP(ARMA_EXTRA_FIELD_MEAT) max@0: #endif max@0: max@0: max@0: max@0: //! @}