From 4372f84e99f6b9fa925c64b879a506c99de45c16 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Tue, 11 Jul 2017 19:19:45 -0600 Subject: [PATCH] Use a std::unique_ptr instead of a raw pointer in ChunkSparseMatrix. --- include/deal.II/lac/chunk_sparse_matrix.h | 22 +++++++------ .../lac/chunk_sparse_matrix.templates.h | 31 +++++++------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/include/deal.II/lac/chunk_sparse_matrix.h b/include/deal.II/lac/chunk_sparse_matrix.h index 12bbd24433..53d9262ee7 100644 --- a/include/deal.II/lac/chunk_sparse_matrix.h +++ b/include/deal.II/lac/chunk_sparse_matrix.h @@ -24,6 +24,9 @@ #include #include +#include + + DEAL_II_NAMESPACE_OPEN template class Vector; @@ -1325,12 +1328,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 @@ -1495,8 +1499,8 @@ ChunkSparseMatrix::operator *= (const number factor) // the padding elements in chunks that overlap the boundaries of the actual // matrix -- but since multiplication with a number does not violate the // invariant of keeping these elements at zero nothing can happen - number *val_ptr = val; - const number *const end_ptr = val + + number *val_ptr = val.get(); + const number *const end_ptr = val.get() + cols->sparsity_pattern.n_nonzero_elements() * chunk_size * chunk_size; @@ -1525,8 +1529,8 @@ ChunkSparseMatrix::operator /= (const number factor) // the padding elements in chunks that overlap the boundaries of the actual // matrix -- but since multiplication with a number does not violate the // invariant of keeping these elements at zero nothing can happen - number *val_ptr = val; - const number *const end_ptr = val + + number *val_ptr = val.get(); + const number *const end_ptr = val.get() + cols->sparsity_pattern.n_nonzero_elements() * chunk_size * chunk_size; diff --git a/include/deal.II/lac/chunk_sparse_matrix.templates.h b/include/deal.II/lac/chunk_sparse_matrix.templates.h index 61ffd3bbf2..0dbb249ac9 100644 --- a/include/deal.II/lac/chunk_sparse_matrix.templates.h +++ b/include/deal.II/lac/chunk_sparse_matrix.templates.h @@ -213,7 +213,7 @@ namespace internal const size_type irregular_col = n/chunk_size; typename OutVector::iterator dst_ptr = dst.begin()+chunk_size*begin_row; - const number *val_ptr= &values[rowstart[begin_row]*chunk_size*chunk_size]; + const number *val_ptr = &values[rowstart[begin_row]*chunk_size*chunk_size]; const size_type *colnum_ptr = &colnums[rowstart[begin_row]]; for (unsigned int chunk_row=begin_row; chunk_row ChunkSparseMatrix::~ChunkSparseMatrix () { cols = nullptr; - - if (val != nullptr) - delete[] val; } @@ -411,7 +408,7 @@ ChunkSparseMatrix::operator = (const double d) std::bind(&internal::ChunkSparseMatrix::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)); @@ -448,9 +445,7 @@ ChunkSparseMatrix::reinit (const ChunkSparsityPattern &sparsity) if (cols->empty()) { - if (val != nullptr) - delete[] val; - val = nullptr; + val.reset (); max_len = 0; return; } @@ -462,9 +457,7 @@ ChunkSparseMatrix::reinit (const ChunkSparsityPattern &sparsity) chunk_size * chunk_size; if (N > max_len || max_len == 0) { - if (val != nullptr) - delete[] val; - val = new number[N]; + val.reset (new number[N]); max_len = N; } @@ -481,8 +474,7 @@ void ChunkSparseMatrix::clear () { cols = nullptr; - if (val) delete[] val; - val = nullptr; + val.reset (); max_len = 0; } @@ -721,7 +713,7 @@ ChunkSparseMatrix::vmult_add (OutVector &dst, , std::cref(*cols), std::placeholders::_1, std::placeholders::_2, - val, + val.get(), cols->sparsity_pattern.rowstart, cols->sparsity_pattern.colnums, std::cref(src), @@ -758,7 +750,7 @@ ChunkSparseMatrix::Tvmult_add (OutVector &dst, // like in vmult_add, but don't keep an iterator into dst around since we're // not traversing it sequentially this time - const number *val_ptr = val; + const number *val_ptr = val.get(); const size_type *colnum_ptr = cols->sparsity_pattern.colnums; for (size_type chunk_row=0; chunk_row::matrix_norm_square (const Vector &v) cons n_chunk_rows-1 : n_chunk_rows); - const number *val_ptr = val; + const number *val_ptr = val.get(); const size_type *colnum_ptr = cols->sparsity_pattern.colnums; typename Vector::const_iterator v_ptr = v.begin(); @@ -960,7 +952,7 @@ ChunkSparseMatrix::matrix_scalar_product (const Vector &u, n_chunk_rows-1 : n_chunk_rows); - const number *val_ptr = val; + const number *val_ptr = val.get(); const size_type *colnum_ptr = cols->sparsity_pattern.colnums; typename Vector::const_iterator u_ptr = u.begin(); @@ -1162,7 +1154,7 @@ ChunkSparseMatrix::residual (Vector &dst, n_chunk_rows-1 : n_chunk_rows); - const number *val_ptr = val; + const number *val_ptr = val.get(); const size_type *colnum_ptr = cols->sparsity_pattern.colnums; typename Vector::iterator dst_ptr = dst.begin(); @@ -1617,8 +1609,7 @@ ChunkSparseMatrix::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]), -- 2.39.5