[Genabel-commits] r1699 - pkg/ProbABEL/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Apr 25 08:26:38 CEST 2014


Author: lckarssen
Date: 2014-04-25 08:26:38 +0200 (Fri, 25 Apr 2014)
New Revision: 1699

Modified:
   pkg/ProbABEL/src/maskedmatrix.cpp
   pkg/ProbABEL/src/maskedmatrix.h
   pkg/ProbABEL/src/reg1.h
   pkg/ProbABEL/src/regdata.cpp
   pkg/ProbABEL/src/regdata.h
Log:
Fixed the memory leaks introduced in SVN r1589. Instead of using
pointers (to arrays) we now use std::vector to store the mask. No need for deallocation of memory anymore: std::vector cleans up after itself.



Modified: pkg/ProbABEL/src/maskedmatrix.cpp
===================================================================
--- pkg/ProbABEL/src/maskedmatrix.cpp	2014-04-24 18:50:51 UTC (rev 1698)
+++ pkg/ProbABEL/src/maskedmatrix.cpp	2014-04-25 06:26:38 UTC (rev 1699)
@@ -27,72 +27,67 @@
 
 
 
-
+#include <vector>
 #include <algorithm>
 #include "maskedmatrix.h"
 #include "eigen_mematrix.h"
 #include "eigen_mematrix.cpp"
 
+
 masked_matrix::masked_matrix()
 {
     length_of_mask = 0;
     masked_data = NULL;
-    mask_of_old = NULL;
 }
 
+
 masked_matrix::masked_matrix(mematrix<double> M) : matrix_original(M)
 {
-//    matrix_original = M;
     masked_data = &matrix_original;
-    mask_of_old = new bool[M.nrow];
-    std::fill(mask_of_old, mask_of_old+M.nrow, 0);
-    length_of_mask = M.nrow;
+    mask_of_old = std::vector<bool> (M.nrow, false);
+    length_of_mask = mask_of_old.size();
 }
 
+
 void masked_matrix::set_matrix(const mematrix<double> &M)
 {
     matrix_original = M;
     masked_data = &matrix_original;
-    mask_of_old = new bool[M.nrow];
-    std::fill(mask_of_old, mask_of_old+M.nrow, 0);
-    length_of_mask = M.nrow;
+    mask_of_old = std::vector<bool> (M.nrow, false);
+    length_of_mask = mask_of_old.size();
 }
 
-masked_matrix::~masked_matrix()
-{
-    delete[] mask_of_old;
-}
 
-void masked_matrix::update_mask(bool *newmask)
+void masked_matrix::update_mask(std::vector<bool> newmask)
 {
-    //find length of masked matrix
-    int nmeasured=std::count (newmask, newmask+length_of_mask, 0);
+    // Find the number of non-masked entries in newmask
+    int nmeasured = std::count(newmask.begin(), newmask.end(), 0);
 
-    //Check update mask is the same as original matrix
+    // Check update mask is the same as original matrix
     if (nmeasured == length_of_mask)
     {
-        //masked matrix is the same as original matrix
+        // Masked matrix is the same as original matrix
         masked_data = &matrix_original;
     }
     else
     {
-        //Check update mask is the same as old matrix
-        if (std::equal(newmask, newmask+length_of_mask, mask_of_old))
+        // Check update mask is the same as old matrix
+        if (newmask == mask_of_old)
         {
-            //new mask is the same as old matrix
+            // New mask is the same as old matrix
             masked_data = &matrix_masked_data;
         }
         else
         {
-            // new mask differs from old matrix and create new.
-            // mask_of_old = newmask;
-            std::copy(newmask, newmask+length_of_mask, mask_of_old);
+            // New mask differs from old matrix and create new.
+            mask_of_old = newmask;
             mask_symmetric(nmeasured);
             masked_data = &matrix_masked_data;
         }
     }
 }
 
+
 void masked_matrix::mask_symmetric(int nmeasured)
 {
     // Mask a symmetric matrix: this matrix is always a square matrix and will

Modified: pkg/ProbABEL/src/maskedmatrix.h
===================================================================
--- pkg/ProbABEL/src/maskedmatrix.h	2014-04-24 18:50:51 UTC (rev 1698)
+++ pkg/ProbABEL/src/maskedmatrix.h	2014-04-25 06:26:38 UTC (rev 1699)
@@ -32,22 +32,28 @@
 #include "eigen_mematrix.h"
 #include "eigen_mematrix.cpp"
 
+
 class masked_matrix {
  public:
     masked_matrix();
     masked_matrix(mematrix<double> M);
-    void set_matrix(const mematrix<double> &M);
-    ~masked_matrix();
-    void update_mask(bool *newmask);
-//    mematrix<double>* get_matrix();
+    // ~masked_matrix();
+
+
     mematrix<double> matrix_original;
     mematrix<double> *masked_data;
     int length_of_mask;
 
+
+    void set_matrix(const mematrix<double> &M);
+    void update_mask(std::vector<bool> newmask);
+
+
  private:
     mematrix<double> matrix_masked_data;
-    bool *mask_of_old;
+    std::vector<bool> mask_of_old;
+
     void mask_symmetric(int nmeasured);
 };
 
-#endif//MASKEDMATRIX_H_
+#endif // MASKEDMATRIX_H_

Modified: pkg/ProbABEL/src/reg1.h
===================================================================
--- pkg/ProbABEL/src/reg1.h	2014-04-24 18:50:51 UTC (rev 1698)
+++ pkg/ProbABEL/src/reg1.h	2014-04-25 06:26:38 UTC (rev 1699)
@@ -83,13 +83,7 @@
 class linear_reg: public base_reg {
  public:
     linear_reg(regdata& rdatain);
-    ~linear_reg()
-    {
-        delete [] reg_data.masked_data;
-        //		delete beta;
-        //		delete sebeta;
-        //		delete residuals;
-    }
+    // ~linear_reg();
 
     void estimate(int verbose, double tol_chol, int model,
                   int interaction, int ngpreds,
@@ -102,13 +96,15 @@
 
  private:
     void mmscore_regression(const mematrix<double>& X,
-            const masked_matrix& W_masked, LDLT<MatrixXd>& Ch);
+                            const masked_matrix& W_masked,
+                            LDLT<MatrixXd>& Ch);
     void logLikelihood(const mematrix<double>& X);
-    void LeastSquaredRegression(mematrix<double> X,LDLT<MatrixXd>& Ch);
-    void RobustSEandCovariance(mematrix<double> X, mematrix<double> robust_sigma2,
-            MatrixXd tXX_inv, int offset);
+    void LeastSquaredRegression(mematrix<double> X, LDLT<MatrixXd>& Ch);
+    void RobustSEandCovariance(mematrix<double> X,
+                               mematrix<double> robust_sigma2,
+                               MatrixXd tXX_inv, int offset);
     void PlainSEandCovariance(double sigma2_internal, MatrixXd tXX_inv,
-            int offset);
+                              int offset);
 };
 
 class logistic_reg: public base_reg {
@@ -116,12 +112,7 @@
     int niter;
 
     logistic_reg(regdata& rdatain);
-    ~logistic_reg()
-    {
-        delete [] reg_data.masked_data;
-        //		delete beta;
-        //		delete sebeta;
-    }
+    // ~logistic_reg();
 
     void estimate(int verbose, int maxiter, double eps,
                   int model, int interaction, int ngpreds,
@@ -133,4 +124,4 @@
                masked_matrix& invvarmatrix, int nullmodel = 0);
 };
 
-#endif//REG1_H_
+#endif // REG1_H_

Modified: pkg/ProbABEL/src/regdata.cpp
===================================================================
--- pkg/ProbABEL/src/regdata.cpp	2014-04-24 18:50:51 UTC (rev 1698)
+++ pkg/ProbABEL/src/regdata.cpp	2014-04-25 06:26:38 UTC (rev 1699)
@@ -58,7 +58,6 @@
     ngpreds                 = 0;
     noutcomes               = 0;
     is_interaction_excluded = false;
-    masked_data             = NULL;
     gcount                  = 0;
     freq                    = 0;
 }
@@ -80,9 +79,7 @@
     gcount = obj.gcount;
     freq = obj.freq;
     is_interaction_excluded = obj.is_interaction_excluded;
-    masked_data = new bool[nids];
-
-    std::copy(obj.masked_data, obj.masked_data + nids, masked_data);
+    masked_data = obj.masked_data;
 }
 
 
@@ -105,9 +102,8 @@
     freq        = 0;
     gcount      = 0;
     nids        = gend.nids;
-    masked_data = new bool[nids];
+    masked_data = std::vector<bool>(nids, false);
 
-    std::fill(masked_data, masked_data + nids, 0);
     ngpreds = gend.ngpreds;
 
     if (snpnum >= 0)
@@ -178,13 +174,14 @@
     for (int j = 0; j < ngpreds; j++)
     {
         double *snpdata = new double[nids];
-        std::fill(masked_data, masked_data + nids, 0);
+        masked_data = std::vector<bool>(nids, false);
+
         gend->get_var(snpnum * ngpreds + j, snpdata);
 
         for (int i = 0; i < nids; i++) {
             X.put(snpdata[i], i, (ncov - j));
             if (std::isnan(snpdata[i])) {
-                masked_data[i] = 1;
+                masked_data[i] = true;
             } else {
                 // SNP not masked
                 // check for first predictor
@@ -257,8 +254,9 @@
  */
 regdata regdata::get_unmasked_data()
 {
-    regdata to;  // = regdata(*this);
-    int nmeasured = std::count(masked_data, masked_data + nids, 0);
+    regdata to;
+    int nmeasured = std::count(masked_data.begin(), masked_data.end(), 0);
+
     to.nids                    = nmeasured;
     to.ncov                    = ncov;
     to.ngpreds                 = ngpreds;
@@ -287,11 +285,7 @@
         }
     }
 
-    // delete [] to.masked_data;
-    const int arr_size = nids;
-    to.masked_data = new bool[arr_size];
-    std::copy(masked_data, masked_data + arr_size, to.masked_data);
-
+    to.masked_data = masked_data;
     return (to);
 }
 

Modified: pkg/ProbABEL/src/regdata.h
===================================================================
--- pkg/ProbABEL/src/regdata.h	2014-04-24 18:50:51 UTC (rev 1698)
+++ pkg/ProbABEL/src/regdata.h	2014-04-25 06:26:38 UTC (rev 1699)
@@ -91,7 +91,7 @@
      * ID/sample will be masked because the SNP data is NA for that
      * ID.
      */
-    bool *masked_data;
+    std::vector<bool> masked_data;
 
     /**
      * Number of non-masked genotypes.



More information about the Genabel-commits mailing list