]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Free the memory no more needed for the colnums array upon compress().
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 18 Nov 1999 10:00:00 +0000 (10:00 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 18 Nov 1999 10:00:00 +0000 (10:00 +0000)
git-svn-id: https://svn.dealii.org/trunk@1888 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/Todo
deal.II/lac/include/lac/sparse_matrix.h
deal.II/lac/source/sparse_matrix.cc

index 253c5e218a610f3de1cbc0ae4ff90c7601729913..9859d5f085c0ca285925b1f5d0e5dde001f2264b 100644 (file)
@@ -10,18 +10,17 @@ Use unsigned integers for the colnums array in dSMatrixStruct. This
   eliminating the need for conversions.
 
 
-Let dSMatrixStruct::compress free the memory of colnums which is
-  no longer needed.
-
-
 Make ILU decomposition faster by following the comment in the
   innermost loop (about the better use of the index j).
 
+
 Change in-class defined SolverSelector functions out-of-class to
   conform to general style guide. (Ralf?)
 
+
 Why not replace Vector::scale by Vector::operator *= ?
 
+
 Use the commented-out version in PreconditionBlock::invert_diagblocks
   using the try-catch clauses
 
index 561f76765444759d1d01cc5adc7670161a3b5e42..d23b50f6efb8356d3fba52ebac1e9d291a7fc536 100644 (file)
@@ -209,6 +209,9 @@ class SparseMatrixStruct : public Subscriptor
                                      * entry of square matrices, which is
                                      * always the first entry of each row.
                                      *
+                                     * The memory which is no more
+                                     * needed is released.
+                                     *
                                      * #SparseMatrix# objects require the
                                      * #SparseMatrixStruct# objects they are
                                      * initialized with to be compressed, to
index 5c3264d0d5f938df1d50e6115f35ff5238d53648..89710fcf78ca044bd5b581a8a8acdc82aceb6432 100644 (file)
@@ -309,6 +309,18 @@ SparseMatrixStruct::compress ()
                next_row_start = 0,
                    row_length = 0;
 
+                                  // first find out how many non-zero
+                                  // elements there are, in order to
+                                  // allocate the right amount of
+                                  // memory
+  const unsigned int
+    nonzero_elements = count_if (&colnums[rowstart[0]],
+                                &colnums[rowstart[rows]],
+                                bind2nd(not_equal_to<int>(), -1));
+                                  // now allocate the respective memory
+  int *new_colnums = new int[nonzero_elements];
+
+  
                                   // reserve temporary storage to
                                   // store the entries of one row
   vector<int> tmp_entries (max_row_length);
@@ -337,10 +349,10 @@ SparseMatrixStruct::compress ()
       sort ((rows==cols) ? &tmp_entries[1] : &tmp_entries[0],
            &tmp_entries[row_length]);
 
-                                      // Re-insert column numbers
-                                      // into the field
+                                      // insert column numbers
+                                      // into the new field
       for (unsigned int j=0; j<row_length; ++j)
-       colnums[next_free_entry++] = tmp_entries[j];
+       new_colnums[next_free_entry++] = tmp_entries[j];
 
                                       // note new start of this and
                                       // the next row
@@ -349,7 +361,7 @@ SparseMatrixStruct::compress ()
 
                                       // some internal checks
       Assert ((rows!=cols) ||
-             (colnums[rowstart[line]] == static_cast<signed int>(line)),
+             (new_colnums[rowstart[line]] == static_cast<signed int>(line)),
              ExcInternalError());
                                       // assert that the first entry
                                       // does not show up in
@@ -358,18 +370,35 @@ SparseMatrixStruct::compress ()
                                       // among themselves (this handles
                                       // both cases, quadratic and
                                       // rectangular matrices)
-      Assert (find (&colnums[rowstart[line]+1],
-                   &colnums[next_row_start],
-                   colnums[rowstart[line]]) ==
-             &colnums[next_row_start],
+      Assert (find (&new_colnums[rowstart[line]+1],
+                   &new_colnums[next_row_start],
+                   new_colnums[rowstart[line]]) ==
+             &new_colnums[next_row_start],
              ExcInternalError());
-      Assert (adjacent_find(&colnums[rowstart[line]+1],
-                           &colnums[next_row_start]) ==
-             &colnums[next_row_start],
+      Assert (adjacent_find(&new_colnums[rowstart[line]+1],
+                           &new_colnums[next_row_start]) ==
+             &new_colnums[next_row_start],
              ExcInternalError());
     };
-  
+
+                                  // assert that we have used all
+                                  // allocated space, no more and no
+                                  // less
+  Assert (next_free_entry == nonzero_elements,
+         ExcInternalError());
+
+                                  // set iterator-past-the-end
   rowstart[rows] = next_row_start;
+
+                                  // set colnums to the newly
+                                  // allocated array and delete the
+                                  // old one
+  delete[] colnums;
+  colnums = new_colnums;
+
+                                  // store the size
+  max_vec_len = nonzero_elements;
+  
   compressed = true;
 };
 

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.