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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 14 19:13:23 CET 2012


Author: lckarssen
Date: 2012-12-14 19:13:23 +0100 (Fri, 14 Dec 2012)
New Revision: 1056

Modified:
   pkg/ProbABEL/src/eigen_mematrix.cpp
   pkg/ProbABEL/src/mematri1.h
Log:
ProbABEL:
Fixed off-by-one error in the mematrix get() and put() functions. Both for the Eigen and non-Eigen variants of the functions. Now we indeed get an error message when addressing arrays/matrices out of their bounds. 


Modified: pkg/ProbABEL/src/eigen_mematrix.cpp
===================================================================
--- pkg/ProbABEL/src/eigen_mematrix.cpp	2012-12-13 17:12:18 UTC (rev 1055)
+++ pkg/ProbABEL/src/eigen_mematrix.cpp	2012-12-14 18:13:23 UTC (rev 1056)
@@ -30,6 +30,7 @@
     this->nelements = nr * nc;
     this->data.resize(nr, nc);
 }
+
 template<class DT>
 mematrix<DT>::mematrix(const mematrix<DT> & M)
 {
@@ -38,6 +39,7 @@
     nelements = M.nelements;
     data = M.data;
 }
+
 //
 // operators
 //
@@ -77,6 +79,7 @@
 //        temp.data[i] = data[i] + toadd;
 //    return temp;
 //}
+
 template<class DT>
 mematrix<DT> mematrix<DT>::operator+(const mematrix<DT> &M)
 {
@@ -103,6 +106,7 @@
     temp.data = data.array() - toadd;
     return temp;
 }
+
 template<class DT>
 mematrix<DT> mematrix<DT>::operator-(const mematrix<DT> &M)
 {
@@ -197,40 +201,42 @@
     data.resize(nr, nc);
     data.setZero();
 }
+
 template<class DT>
 DT mematrix<DT>::get(int nr, int nc)
 {
-#if !NDEBUG
-    if (nc < 0 || nc > ncol)
+#ifndef NDEBUG
+    if (nc < 0 || nc > ncol -1)
     {
-        std::cerr << "mematrix::get: column out of range: " << nc
-                  << " not in (0," << ncol << ")\n";
+        std::cerr << "mematrix::get: column out of range: " << nc + 1
+                  << " not between (1," << ncol << ")\n" << std::flush;
         exit(1);
     }
-    if (nr < 0 || nr > nrow)
+    if (nr < 0 || nr > nrow -1)
     {
-        std::cerr << "mematrix::get: row out of range: " << nr
-                  << " not in (0," << nrow << ")\n";
+        std::cerr << "mematrix::get: row out of range: " << nr + 1
+                  << " not between (1," << nrow << ")\n" << std::flush;
         exit(1);
     }
 #endif
     DT temp = data(nr, nc);
     return temp;
 }
+
 template<class DT>
 void mematrix<DT>::put(DT value, int nr, int nc)
 {
-#if !NDEBUG
-    if (nc < 0 || nc > ncol)
+#ifndef NDEBUG
+    if (nc < 0 || nc > ncol -1)
     {
-        std::cerr << "mematrix::put: column out of range: " << nc
-                  << " not in (0," << ncol << ")\n";
+        std::cerr << "mematrix::put: column out of range: " << nc + 1
+                  << " not between (1," << ncol << ")\n" << std::flush;
         exit(1);
     }
-    if (nr < 0 || nr > nrow)
+    if (nr < 0 || nr > nrow -1)
     {
-        std::cerr << "mematrix::put: row out of range: " << nr
-                  << " not in (0," << nrow << ")\n";
+        std::cerr << "mematrix::put: row out of range: " << nr + 1
+                  << " not between (1," << nrow << ")\n" << std::flush;
         exit(1);
     }
 #endif
@@ -257,6 +263,7 @@
     out.data = M.data.colwise().mean();
     return out;
 }
+
 template<class DT>
 void mematrix<DT>::print(void)
 {
@@ -270,9 +277,10 @@
         std::cout << "\n";
     }
 }
+
+//
 // other functions
 //
-
 template<class DT>
 mematrix<DT> transpose(const mematrix<DT> &M)
 {
@@ -307,8 +315,7 @@
     }
     return temp;
 }
-//
-//
+
 //template<class DT>
 //mematrix<double> todouble(mematrix<DT> &M)
 //{
@@ -317,10 +324,7 @@
 //        temp.data[i] = double(M.data[i]);
 //    return temp;
 //}
-//
 
-//
-
 template<class DT>
 mematrix<DT> invert(const mematrix<DT> &M)
 {

Modified: pkg/ProbABEL/src/mematri1.h
===================================================================
--- pkg/ProbABEL/src/mematri1.h	2012-12-13 17:12:18 UTC (rev 1055)
+++ pkg/ProbABEL/src/mematri1.h	2012-12-14 18:13:23 UTC (rev 1056)
@@ -235,16 +235,16 @@
 template<class DT>
 DT mematrix<DT>::get(int nr, int nc)
 {
-    if (nc < 0 || nc > ncol)
+    if (nc < 0 || nc > ncol -1)
     {
-        fprintf(stderr,
-                "mematrix::get: column out of range: %d not in (0,%d)\n", nc,
-                ncol);
+        std::cerr << "mematrix::get: column out of range: " << nc + 1
+                  << " not between (1," << ncol << ")\n" << std::flush;
         exit(1);
     }
-    if (nr < 0 || nr > nrow)
+    if (nr < 0 || nr > nrow -1)
     {
-        printf("mematrix::get: row out of range: %d not in (0,%d)\n", nr, nrow);
+        std::cerr << "mematrix::get: row out of range: " << nr + 1
+                  <<" not between (1," << nrow << ")\n" << std::flush;
         exit(1);
     }
     DT temp = data[nr * ncol + nc];
@@ -254,16 +254,16 @@
 template<class DT>
 void mematrix<DT>::put(DT value, int nr, int nc)
 {
-    if (nc < 0 || nc > ncol)
+    if (nc < 0 || nc > ncol -1)
     {
-        fprintf(stderr,
-                "mematrix::put: column out of range: %d not in (0,%d)\n", nc,
-                ncol);
+        std::cerr << "mematrix::put: column out of range: " << nc + 1
+                  << " not between (1," << ncol << ")\n" << std::flush;
         exit(1);
     }
-    if (nr < 0 || nr > nrow)
+    if (nr < 0 || nr > nrow -1)
     {
-        printf("mematrix::put: row out of range: %d not in (0,%d)\n", nr, nrow);
+        std::cerr << "mematrix::put: row out of range: " << nr + 1
+                  <<" not between (1," << nrow << ")\n" << std::flush;
         exit(1);
     }
     data[nr * ncol + nc] = value;



More information about the Genabel-commits mailing list