[Genabel-commits] r1713 - pkg/ProbABEL/src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Apr 28 22:36:11 CEST 2014
Author: lckarssen
Date: 2014-04-28 22:36:10 +0200 (Mon, 28 Apr 2014)
New Revision: 1713
Modified:
pkg/ProbABEL/src/eigen_mematrix.cpp
pkg/ProbABEL/src/eigen_mematrix.h
Log:
Added more 'constness' to the mematrix class.
- Converted the one version of the overloaded subscript operator [] to two versions: one for read access (const) and one for write access (non-const). This allows more consts in other functions declarations.
- Removed some commented code and minor changes to the code layout
Modified: pkg/ProbABEL/src/eigen_mematrix.cpp
===================================================================
--- pkg/ProbABEL/src/eigen_mematrix.cpp 2014-04-28 16:59:21 UTC (rev 1712)
+++ pkg/ProbABEL/src/eigen_mematrix.cpp 2014-04-28 20:36:10 UTC (rev 1713)
@@ -80,7 +80,7 @@
}
template<class DT>
-DT & mematrix<DT>::operator[](const int i)
+const DT & mematrix<DT>::operator[](const int i) const
{
if (i < 0 || i >= (ncol * nrow))
{
@@ -94,16 +94,23 @@
return data(row, column);
}
-// template<class DT>
-// mematrix<DT> mematrix<DT>::operator+(DT toadd)
-// {
-// mematrix<DT> temp(nrow, ncol);
-// for (int i = 0; i < nelements; i++)
-// temp.data[i] = data[i] + toadd;
-// return temp;
-// }
template<class DT>
+DT & mematrix<DT>::operator[](const int i)
+{
+ if (i < 0 || i >= (ncol * nrow))
+ {
+ std::cerr << "mematrix[]: " << i << " out of bounds (0,"
+ << nrow * ncol - 1 << ")\n";
+ exit(1);
+ }
+ int column = i % ncol;
+ int row = static_cast<int>( floor(static_cast<double>(i / ncol)) );
+
+ return data(row, column);
+}
+
+template<class DT>
mematrix<DT> mematrix<DT>::operator+(const mematrix<DT> &M)
{
if (ncol != M.ncol || nrow != M.nrow)
Modified: pkg/ProbABEL/src/eigen_mematrix.h
===================================================================
--- pkg/ProbABEL/src/eigen_mematrix.h 2014-04-28 16:59:21 UTC (rev 1712)
+++ pkg/ProbABEL/src/eigen_mematrix.h 2014-04-28 20:36:10 UTC (rev 1713)
@@ -39,6 +39,8 @@
int nelements;
Matrix<DT, Dynamic, Dynamic, RowMajor> data;
+
+ // Constructors and destructors
mematrix()
{
nrow = ncol = nelements = 0;
@@ -46,15 +48,13 @@
}
mematrix(const int nr, const int nc);
mematrix(const mematrix &M);
- ~mematrix()
- {
-// if (nelements > 0)
-// delete data;
- }
+ // ~mematrix()
+
+ // Operator overloading
mematrix & operator=(const mematrix &M);
- DT & operator[](const int i);
-// mematrix operator+(DT toadd);
+ const DT & operator[](const int i) const; /* Subscript for reading */
+ DT & operator[](const int i); /* Subscript for writing */
mematrix operator+(const mematrix &M);
mematrix operator-(const DT toadd);
mematrix operator-(const mematrix &M);
@@ -62,6 +62,8 @@
mematrix operator*(const mematrix &M);
mematrix operator*(const mematrix *M);
+
+ // Other member functions
void delete_column(const int delcol);
void delete_row(const int delrow);
More information about the Genabel-commits
mailing list