// Copyright (C) 2008-2015 National ICT Australia (NICTA) // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // ------------------------------------------------------------------- // // Written by Conrad Sanderson - http://conradsanderson.id.au // Written by Ryan Curtin // Written by Matthew Amidon //! \addtogroup SpCol //! @{ //! construct an empty column vector template inline SpCol::SpCol() : SpMat(0, 1) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; } //! construct a column vector with the specified number of elements template inline SpCol::SpCol(const uword in_n_elem) : SpMat(in_n_elem, 1) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; } template inline SpCol::SpCol(const uword in_n_rows, const uword in_n_cols) : SpMat(in_n_rows, in_n_cols) { arma_extra_debug_sigprint(); arma_debug_check((in_n_cols != 1), "SpCol::SpCol(): must have only one column"); access::rw(SpMat::vec_state) = 1; } //! construct a column vector from specified text template inline SpCol::SpCol(const char* text) : SpMat(text) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; arma_debug_check((SpMat::n_cols != 1), "SpCol::SpCol(): must have only one column"); } //! construct a column vector from specified text template inline const SpCol& SpCol::operator=(const char* text) { arma_extra_debug_sigprint(); SpMat::init(std::string(text)); access::rw(SpMat::vec_state) = 1; return *this; } //! construct a column vector from specified text template inline SpCol::SpCol(const std::string& text) : SpMat(text) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; arma_debug_check((SpMat::n_cols != 1), "SpCol::SpCol(): must have only one column"); } //! construct a column vector from specified text template inline const SpCol& SpCol::operator=(const std::string& text) { arma_extra_debug_sigprint(); SpMat::init(std::string(text)); return *this; } template inline const SpCol& SpCol::operator=(const eT val) { arma_extra_debug_sigprint(); SpMat::operator=(val); return *this; } template template inline SpCol::SpCol(const Base& X) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; SpMat::operator=(X.get_ref()); } template template inline const SpCol& SpCol::operator=(const Base& X) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; SpMat::operator=(X.get_ref()); return *this; } template template inline SpCol::SpCol(const SpBase& X) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; SpMat::operator=(X.get_ref()); } template template inline const SpCol& SpCol::operator=(const SpBase& X) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; SpMat::operator=(X.get_ref()); return *this; } template template inline SpCol::SpCol ( const SpBase::pod_type, T1>& A, const SpBase::pod_type, T2>& B ) { arma_extra_debug_sigprint(); access::rw(SpMat::vec_state) = 1; SpMat::init(A,B); } //! remove specified row template inline void SpCol::shed_row(const uword row_num) { arma_extra_debug_sigprint(); arma_debug_check( row_num >= SpMat::n_rows, "SpCol::shed_row(): out of bounds"); shed_rows(row_num, row_num); } //! remove specified rows template inline void SpCol::shed_rows(const uword in_row1, const uword in_row2) { arma_extra_debug_sigprint(); arma_debug_check ( (in_row1 > in_row2) || (in_row2 >= SpMat::n_rows), "SpCol::shed_rows(): indices out of bounds or incorrectly used" ); const uword diff = (in_row2 - in_row1 + 1); // This is easy because everything is in one column. uword start = 0, end = 0; bool start_found = false, end_found = false; for(uword i = 0; i < SpMat::n_nonzero; ++i) { // Start position found? if (SpMat::row_indices[i] >= in_row1 && !start_found) { start = i; start_found = true; } // End position found? if (SpMat::row_indices[i] > in_row2) { end = i; end_found = true; break; } } if (!end_found) { end = SpMat::n_nonzero; } // Now we can make the copy. if (start != end) { const uword elem_diff = end - start; eT* new_values = memory::acquire_chunked (SpMat::n_nonzero - elem_diff); uword* new_row_indices = memory::acquire_chunked(SpMat::n_nonzero - elem_diff); // Copy before the section we are dropping (if it exists). if (start > 0) { arrayops::copy(new_values, SpMat::values, start); arrayops::copy(new_row_indices, SpMat::row_indices, start); } // Copy after the section we are dropping (if it exists). if (end != SpMat::n_nonzero) { arrayops::copy(new_values + start, SpMat::values + end, (SpMat::n_nonzero - end)); arrayops::copy(new_row_indices + start, SpMat::row_indices + end, (SpMat::n_nonzero - end)); arrayops::inplace_minus(new_row_indices + start, diff, (SpMat::n_nonzero - end)); } memory::release(SpMat::values); memory::release(SpMat::row_indices); access::rw(SpMat::values) = new_values; access::rw(SpMat::row_indices) = new_row_indices; access::rw(SpMat::n_nonzero) -= elem_diff; access::rw(SpMat::col_ptrs[1]) -= elem_diff; } access::rw(SpMat::n_rows) -= diff; access::rw(SpMat::n_elem) -= diff; } // //! insert N rows at the specified row position, // //! optionally setting the elements of the inserted rows to zero // template // inline // void // SpCol::insert_rows(const uword row_num, const uword N, const bool set_to_zero) // { // arma_extra_debug_sigprint(); // // arma_debug_check(set_to_zero == false, "SpCol::insert_rows(): cannot set nonzero values"); // // arma_debug_check((row_num > SpMat::n_rows), "SpCol::insert_rows(): out of bounds"); // // for(uword row = 0; row < SpMat::n_rows; ++row) // { // if (SpMat::row_indices[row] >= row_num) // { // access::rw(SpMat::row_indices[row]) += N; // } // } // // access::rw(SpMat::n_rows) += N; // access::rw(SpMat::n_elem) += N; // } template inline typename SpCol::row_iterator SpCol::begin_row(const uword row_num) { arma_extra_debug_sigprint(); arma_debug_check( (row_num >= SpMat::n_rows), "SpCol::begin_row(): index out of bounds"); return row_iterator(*this, row_num, 0); } template inline typename SpCol::const_row_iterator SpCol::begin_row(const uword row_num) const { arma_extra_debug_sigprint(); arma_debug_check( (row_num >= SpMat::n_rows), "SpCol::begin_row(): index out of bounds"); return const_row_iterator(*this, row_num, 0); } template inline typename SpCol::row_iterator SpCol::end_row(const uword row_num) { arma_extra_debug_sigprint(); arma_debug_check( (row_num >= SpMat::n_rows), "SpCol::end_row(): index out of bounds"); return row_iterator(*this, row_num + 1, 0); } template inline typename SpCol::const_row_iterator SpCol::end_row(const uword row_num) const { arma_extra_debug_sigprint(); arma_debug_check( (row_num >= SpMat::n_rows), "SpCol::end_row(): index out of bounds"); return const_row_iterator(*this, row_num + 1, 0); } #ifdef ARMA_EXTRA_SPCOL_MEAT #include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPCOL_MEAT) #endif //! @}