]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use a std::unique_ptr instead of a raw pointer in SparseMatrix.
authorWolfgang Bangerth <bangerth@colostate.edu>
Tue, 11 Jul 2017 23:30:33 +0000 (17:30 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 12 Jul 2017 01:19:39 +0000 (19:19 -0600)
include/deal.II/lac/sparse_decomposition.templates.h
include/deal.II/lac/sparse_ilu.templates.h
include/deal.II/lac/sparse_matrix.h
include/deal.II/lac/sparse_matrix.templates.h

index b5827863f9bf386bc2db8d128ce3338bc9a10632..421d1e0d88853e30368419d90e72ea59a81cd22f 100644 (file)
@@ -160,8 +160,8 @@ SparseLUDecomposition<number>::copy_from (const SparseMatrix<somenumber> &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<somenumber, number>::value == true)
         std::memcpy (this_ptr, input_ptr, this->n_nonzero_elements()*sizeof(number));
index 53931fac33fc330f4d4524ad4a1ced2728f65612..9f72a0677caf35cf50b1e8594f82720b2333fdc6 100644 (file)
@@ -62,7 +62,7 @@ void SparseILU<number>::initialize (const SparseMatrix<somenumber> &matrix,
   const std::size_t *const ia    = sparsity.rowstart;
   const size_type *const ja      = sparsity.colnums;
 
-  number *luval = this->SparseMatrix<number>::val;
+  number *luval = this->SparseMatrix<number>::val.get();
 
   const size_type N = this->m();
   size_type jrow = 0;
@@ -174,7 +174,7 @@ void SparseILU<number>::vmult (Vector<somenumber>       &dst,
       const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row];
 
       somenumber dst_row = dst(row);
-      const number *luval = this->SparseMatrix<number>::val +
+      const number *luval = this->SparseMatrix<number>::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<number>::vmult (Vector<somenumber>       &dst,
       const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row];
 
       somenumber dst_row = dst(row);
-      const number *luval = this->SparseMatrix<number>::val +
+      const number *luval = this->SparseMatrix<number>::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<number>::Tvmult (Vector<somenumber>       &dst,
       const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row];
 
       const somenumber dst_row = dst (row);
-      const number *luval = this->SparseMatrix<number>::val +
+      const number *luval = this->SparseMatrix<number>::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<number>::Tvmult (Vector<somenumber>       &dst,
       const size_type *const first_after_diagonal = this->prebuilt_lower_bound[row];
 
       const somenumber dst_row = dst (row);
-      const number *luval = this->SparseMatrix<number>::val +
+      const number *luval = this->SparseMatrix<number>::val.get() +
                             (rowstart - column_numbers);
       for (const size_type *col=rowstart; col!=first_after_diagonal; ++col, ++luval)
         tmp(*col) += *luval * dst_row;
index c73d56a39e8aefb70b2561332510bd509f0a9914..02fbb6fc94d4460b1781814ac2542f0306629c8e 100644 (file)
@@ -25,6 +25,9 @@
 #include <deal.II/lac/exceptions.h>
 #include <deal.II/lac/vector.h>
 
+#include <memory>
+
+
 DEAL_II_NAMESPACE_OPEN
 
 template <typename number> class Vector;
@@ -1593,12 +1596,13 @@ private:
   SmartPointer<const SparsityPattern,SparseMatrix<number> > 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<number[]> val;
 
   /**
    * Allocated size of #val. This can be larger than the actually used part if
index 17fe90d137de37494cb3fa01686191a9416adfd7..edbe78b3eb7dfb8181a711d57d8fa9e232d41e50 100644 (file)
@@ -76,7 +76,7 @@ SparseMatrix<number>::SparseMatrix (SparseMatrix<number> &&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<number> &
 SparseMatrix<number>::operator = (SparseMatrix<number> &&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 <typename number>
 SparseMatrix<number>::~SparseMatrix ()
 {
   cols = nullptr;
-
-  if (val != nullptr)
-    delete[] val;
 }
 
 
@@ -205,7 +202,7 @@ SparseMatrix<number>::operator = (const double d)
                                   std::bind(&internal::SparseMatrix::template
                                             zero_subrange<number>,
                                             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<number>::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<number>::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<number>::clear ()
 {
   cols = nullptr;
-  if (val) delete[] val;
-  val = nullptr;
+  val.reset ();
   max_len = 0;
 }
 
@@ -764,7 +756,7 @@ SparseMatrix<number>::vmult (OutVector &dst,
                                 std::bind (&internal::SparseMatrix::vmult_on_subrange
                                            <number,InVector,OutVector>,
                                            std::placeholders::_1, std::placeholders::_2,
-                                           val,
+                                           val.get(),
                                            cols->rowstart,
                                            cols->colnums,
                                            std::cref(src),
@@ -819,7 +811,7 @@ SparseMatrix<number>::vmult_add (OutVector &dst,
                                 std::bind (&internal::SparseMatrix::vmult_on_subrange
                                            <number,InVector,OutVector>,
                                            std::placeholders::_1, std::placeholders::_2,
-                                           val,
+                                           val.get(),
                                            cols->rowstart,
                                            cols->colnums,
                                            std::cref(src),
@@ -905,7 +897,7 @@ SparseMatrix<number>::matrix_norm_square (const Vector<somenumber> &v) const
     (std::bind (&internal::SparseMatrix::matrix_norm_sqr_on_subrange
                 <number,Vector<somenumber> >,
                 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<number>::matrix_scalar_product (const Vector<somenumber> &u,
     (std::bind (&internal::SparseMatrix::matrix_scalar_product_on_subrange
                 <number,Vector<somenumber> >,
                 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<number>::residual (Vector<somenumber>       &dst,
                (std::bind (&internal::SparseMatrix::residual_sqr_on_subrange
                            <number,Vector<somenumber>,Vector<somenumber> >,
                            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<number>::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<char *>(&val[0]),

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.