From: Wolfgang Bangerth Date: Tue, 11 Jul 2017 23:30:33 +0000 (-0600) Subject: Use a std::unique_ptr instead of a raw pointer in SparseMatrix. X-Git-Tag: v9.0.0-rc1~1428^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d83f2fbb6b6cde2855ec5f4a6306a9d37e76b09;p=dealii.git Use a std::unique_ptr instead of a raw pointer in SparseMatrix. --- diff --git a/include/deal.II/lac/sparse_decomposition.templates.h b/include/deal.II/lac/sparse_decomposition.templates.h index b5827863f9..421d1e0d88 100644 --- a/include/deal.II/lac/sparse_decomposition.templates.h +++ b/include/deal.II/lac/sparse_decomposition.templates.h @@ -160,8 +160,8 @@ SparseLUDecomposition::copy_from (const SparseMatrix &matrix // pattern as the input matrix if (&this->get_sparsity_pattern() == &matrix.get_sparsity_pattern()) { - const somenumber *input_ptr = matrix.val; - number *this_ptr = this->val; + const somenumber *input_ptr = matrix.val.get(); + number *this_ptr = this->val.get(); const number *const end_ptr = this_ptr + this->n_nonzero_elements(); if (types_are_equal::value == true) std::memcpy (this_ptr, input_ptr, this->n_nonzero_elements()*sizeof(number)); diff --git a/include/deal.II/lac/sparse_ilu.templates.h b/include/deal.II/lac/sparse_ilu.templates.h index 53931fac33..9f72a0677c 100644 --- a/include/deal.II/lac/sparse_ilu.templates.h +++ b/include/deal.II/lac/sparse_ilu.templates.h @@ -62,7 +62,7 @@ void SparseILU::initialize (const SparseMatrix &matrix, const std::size_t *const ia = sparsity.rowstart; const size_type *const ja = sparsity.colnums; - number *luval = this->SparseMatrix::val; + number *luval = this->SparseMatrix::val.get(); const size_type N = this->m(); size_type jrow = 0; @@ -174,7 +174,7 @@ void SparseILU::vmult (Vector &dst, const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row]; somenumber dst_row = dst(row); - const number *luval = this->SparseMatrix::val + + const number *luval = this->SparseMatrix::val.get() + (rowstart - column_numbers); for (const size_type *col=rowstart; col!=first_after_diagonal; ++col, ++luval) dst_row -= *luval * dst(*col); @@ -198,7 +198,7 @@ void SparseILU::vmult (Vector &dst, const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row]; somenumber dst_row = dst(row); - const number *luval = this->SparseMatrix::val + + const number *luval = this->SparseMatrix::val.get() + (first_after_diagonal - column_numbers); for (const size_type *col=first_after_diagonal; col!=rowend; ++col, ++luval) dst_row -= *luval * dst(*col); @@ -251,7 +251,7 @@ void SparseILU::Tvmult (Vector &dst, const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row]; const somenumber dst_row = dst (row); - const number *luval = this->SparseMatrix::val + + const number *luval = this->SparseMatrix::val.get() + (first_after_diagonal - column_numbers); for (const size_type *col=first_after_diagonal; col!=rowend; ++col, ++luval) tmp(*col) += *luval * dst_row; @@ -278,7 +278,7 @@ void SparseILU::Tvmult (Vector &dst, const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row]; const somenumber dst_row = dst (row); - const number *luval = this->SparseMatrix::val + + const number *luval = this->SparseMatrix::val.get() + (rowstart - column_numbers); for (const size_type *col=rowstart; col!=first_after_diagonal; ++col, ++luval) tmp(*col) += *luval * dst_row; diff --git a/include/deal.II/lac/sparse_matrix.h b/include/deal.II/lac/sparse_matrix.h index c73d56a39e..02fbb6fc94 100644 --- a/include/deal.II/lac/sparse_matrix.h +++ b/include/deal.II/lac/sparse_matrix.h @@ -25,6 +25,9 @@ #include #include +#include + + DEAL_II_NAMESPACE_OPEN template class Vector; @@ -1593,12 +1596,13 @@ private: SmartPointer > cols; /** - * Array of values for all the nonzero entries. The position within the - * matrix, i.e. the row and column number for a given entry can only be - * deduced using the sparsity pattern. The same holds for the more common - * operation of finding an entry by its coordinates. + * Array of values for all the nonzero entries. The position of an + * entry within the matrix, i.e., the row and column number for a + * given value in this array can only be deduced using the sparsity + * pattern. The same holds for the more common operation of finding + * an entry by its coordinates. */ - number *val; + std::unique_ptr val; /** * Allocated size of #val. This can be larger than the actually used part if diff --git a/include/deal.II/lac/sparse_matrix.templates.h b/include/deal.II/lac/sparse_matrix.templates.h index 17fe90d137..edbe78b3eb 100644 --- a/include/deal.II/lac/sparse_matrix.templates.h +++ b/include/deal.II/lac/sparse_matrix.templates.h @@ -76,7 +76,7 @@ SparseMatrix::SparseMatrix (SparseMatrix &&m) : Subscriptor(std::move(m)), cols(m.cols), - val(m.val), + val(std::move(m.val)), max_len(m.max_len) { m.cols = nullptr; @@ -107,7 +107,7 @@ SparseMatrix & SparseMatrix::operator = (SparseMatrix &&m) { cols = m.cols; - val = m.val; + val = std::move(m.val); max_len = m.max_len; m.cols = nullptr; @@ -154,9 +154,6 @@ template SparseMatrix::~SparseMatrix () { cols = nullptr; - - if (val != nullptr) - delete[] val; } @@ -205,7 +202,7 @@ SparseMatrix::operator = (const double d) std::bind(&internal::SparseMatrix::template zero_subrange, std::placeholders::_1, std::placeholders::_2, - val), + val.get()), grain_size); else if (matrix_size > 0) std::memset (&val[0], 0, matrix_size*sizeof(number)); @@ -242,9 +239,7 @@ SparseMatrix::reinit (const SparsityPattern &sparsity) if (cols->empty()) { - if (val != nullptr) - delete[] val; - val = nullptr; + val.reset (); max_len = 0; return; } @@ -252,9 +247,7 @@ SparseMatrix::reinit (const SparsityPattern &sparsity) const std::size_t N = cols->n_nonzero_elements(); if (N > max_len || max_len == 0) { - if (val != nullptr) - delete[] val; - val = new number[N]; + val.reset (new number[N]); max_len = N; } @@ -268,8 +261,7 @@ void SparseMatrix::clear () { cols = nullptr; - if (val) delete[] val; - val = nullptr; + val.reset (); max_len = 0; } @@ -764,7 +756,7 @@ SparseMatrix::vmult (OutVector &dst, std::bind (&internal::SparseMatrix::vmult_on_subrange , std::placeholders::_1, std::placeholders::_2, - val, + val.get(), cols->rowstart, cols->colnums, std::cref(src), @@ -819,7 +811,7 @@ SparseMatrix::vmult_add (OutVector &dst, std::bind (&internal::SparseMatrix::vmult_on_subrange , std::placeholders::_1, std::placeholders::_2, - val, + val.get(), cols->rowstart, cols->colnums, std::cref(src), @@ -905,7 +897,7 @@ SparseMatrix::matrix_norm_square (const Vector &v) const (std::bind (&internal::SparseMatrix::matrix_norm_sqr_on_subrange >, std::placeholders::_1, std::placeholders::_2, - val, cols->rowstart, cols->colnums, + val.get(), cols->rowstart, cols->colnums, std::cref(v)), 0, m(), internal::SparseMatrix::minimum_parallel_grain_size); @@ -968,7 +960,7 @@ SparseMatrix::matrix_scalar_product (const Vector &u, (std::bind (&internal::SparseMatrix::matrix_scalar_product_on_subrange >, std::placeholders::_1, std::placeholders::_2, - val, cols->rowstart, cols->colnums, + val.get(), cols->rowstart, cols->colnums, std::cref(u), std::cref(v)), 0, m(), @@ -1355,7 +1347,7 @@ SparseMatrix::residual (Vector &dst, (std::bind (&internal::SparseMatrix::residual_sqr_on_subrange ,Vector >, std::placeholders::_1, std::placeholders::_2, - val, cols->rowstart, cols->colnums, + val.get(), cols->rowstart, cols->colnums, std::cref(u), std::cref(b), std::ref(dst)), @@ -2005,8 +1997,7 @@ SparseMatrix::block_read (std::istream &in) AssertThrow (c == '[', ExcIO()); // reallocate space - delete[] val; - val = new number[max_len]; + val.reset (new number[max_len]); // then read data in.read (reinterpret_cast(&val[0]),