[Rcpp-commits] r1304 - in pkg/Rcpp: inst inst/include src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 24 22:16:16 CEST 2010


Author: edd
Date: 2010-05-24 22:16:16 +0200 (Mon, 24 May 2010)
New Revision: 1304

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/RcppMatrix.h
   pkg/Rcpp/inst/include/RcppMatrixView.h
   pkg/Rcpp/inst/include/RcppVector.h
   pkg/Rcpp/inst/include/RcppVectorView.h
   pkg/Rcpp/src/RcppMatrix.cpp
   pkg/Rcpp/src/RcppMatrixView.cpp
   pkg/Rcpp/src/RcppVector.cpp
   pkg/Rcpp/src/RcppVectorView.cpp
Log:
moved template code from src/*cpp to inst/include/*h


Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/inst/ChangeLog	2010-05-24 20:16:16 UTC (rev 1304)
@@ -1,3 +1,8 @@
+2010-05-24  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/Rcpp*.cpp: Moved some template code from the older API to
+	headers in inst/include/*.h
+
 2010-05-21  Romain Francois <romain at r-enthusiasts.com>
 
 	* R/exceptions.R: rework rcpp_tryCatch to prevent evaluating the expression
@@ -2,3 +7,3 @@
 	too early (reported by Doug Bates on Rcpp-devel)
-	
+
 	* src/Evaluator.cpp: rework Evaluator::run() so that it correctly evaluates
@@ -10,13 +15,13 @@
 
 	* inst/include/Rcpp/Vector.h : correct throw specs for vector_from_string
 	(reported by Brian Ripley from solaris)
-	
+
 	* inst/include/Rcpp/internal/Proxy_Iterator.h: fixed constness of several
 	operators in Proxy_Iterator to try to suite suncc/solaris
-	
+
 	* inst/include/Rcpp/config.h: define RCPP_ENABLE_MODULES to hide
 	the experimental module features from the official api
-	
+
 	* R/getDLL.R: removed and promoted to getDynLib in inline
 
 2010-05-19  Romain Francois <romain at r-enthusiasts.com>

Modified: pkg/Rcpp/inst/include/RcppMatrix.h
===================================================================
--- pkg/Rcpp/inst/include/RcppMatrix.h	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/inst/include/RcppMatrix.h	2010-05-24 20:16:16 UTC (rev 1304)
@@ -42,4 +42,102 @@
     T **a;
 };
 
+
+template <typename T>
+RcppMatrix<T>::RcppMatrix(SEXP mat) {
+
+    if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
+	throw std::range_error("RcppMatrix: invalid numeric matrix in constructor");
+
+    // Get matrix dimensions
+    SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
+    dim1 = INTEGER(dimAttr)[0];
+    dim2 = INTEGER(dimAttr)[1];
+
+    // We guard against  the possibility that R might pass an integer matrix.
+    // Can be prevented using R code: temp <- as.double(a), dim(temp) <- dim(a)
+    int i,j;
+    int isInt = Rf_isInteger(mat);
+    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
+    a = (T **)R_alloc(dim1, sizeof(T *));
+    for (i = 0; i < dim1; i++)
+	a[i] = m + i*dim2;
+    if (isInt) {
+	for (i=0; i < dim1; i++)
+	    for (j=0; j < dim2; j++)
+		a[i][j] = (T)(INTEGER(mat)[i+dim1*j]);
+    }	
+    else {
+	for (i=0; i < dim1; i++)
+	    for (j=0; j < dim2; j++)
+		a[i][j] = (T)(REAL(mat)[i+dim1*j]);
+    }	
+}
+
+template <typename T>
+RcppMatrix<T>::RcppMatrix(int _dim1, int _dim2) {
+    dim1 = _dim1;
+    dim2 = _dim2;
+    int i,j;
+    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
+    a = (T **)R_alloc(dim1, sizeof(T *));
+    for (i = 0; i < dim1; i++)
+	a[i] = m + i*dim2;
+    for (i=0; i < dim1; i++)
+	for (j=0; j < dim2; j++)
+	    a[i][j] = 0;
+}
+
+template <typename T> int RcppMatrix<T>::getDim1() const { 
+    return dim1; 
+}
+
+template <typename T> int RcppMatrix<T>::getDim2() const { 
+    return dim2; 
+}
+
+template <typename T> int RcppMatrix<T>::rows() const { 
+    return dim1; 
+}
+
+template <typename T> int RcppMatrix<T>::cols() const { 
+    return dim2; 
+}
+
+template <typename T>
+T& RcppMatrix<T>::operator()(int i, int j) const {
+    if (i < 0 || i >= dim1 || j < 0 || j >= dim2) {
+	std::ostringstream oss;
+	oss << "RcppMatrix: subscripts out of range: " << i << ", " << j;
+	throw std::range_error(oss.str());
+    }
+    return a[i][j];
+}
+
+template <typename T>
+T **RcppMatrix<T>::cMatrix() {
+    int i,j;
+    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
+    T **tmp = (T **)R_alloc(dim1, sizeof(T *));
+    for (i = 0; i < dim1; i++)
+	tmp[i] = m + i*dim2;
+    for (i=0; i < dim1; i++)
+	for (j=0; j < dim2; j++)
+	    tmp[i][j] = a[i][j];
+    return tmp;
+}
+
+template <typename T>
+std::vector<std::vector<T> > RcppMatrix<T>::stlMatrix() {
+    int i,j;
+    std::vector<std::vector<T> > temp;
+    for (i = 0; i < dim1; i++) {
+	temp.push_back(std::vector<T>(dim2));
+    }
+    for (i = 0; i < dim1; i++)
+	for (j = 0; j < dim2; j++)
+	    temp[i][j] = a[i][j];
+    return temp;
+}
+
 #endif

Modified: pkg/Rcpp/inst/include/RcppMatrixView.h
===================================================================
--- pkg/Rcpp/inst/include/RcppMatrixView.h	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/inst/include/RcppMatrixView.h	2010-05-24 20:16:16 UTC (rev 1304)
@@ -39,4 +39,49 @@
     T *a;
 };
 
+template <typename T>
+RcppMatrixView<T>::RcppMatrixView(SEXP mat) {
+    if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
+	throw std::range_error("RcppMatrixView: invalid numeric matrix in constructor");
+    // Get matrix dimensions
+    SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
+    d1 = INTEGER(dimAttr)[0];
+    d2 = INTEGER(dimAttr)[1];
+    if (Rf_isInteger(mat)) {
+	a = (T *)(INTEGER(mat));
+    } else if (Rf_isReal(mat)) {
+	a = (T *)(REAL(mat));
+    }
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::dim1() const { 
+    return d1; 
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::dim2() const { 
+    return d2; 
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::rows() const { 
+    return d1; 
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::cols() const { 
+    return d2; 
+}
+
+template <typename T>
+inline T RcppMatrixView<T>::operator()(int i, int j) const {
+    if (i < 0 || i >= d1 || j < 0 || j >= d2) {
+	std::ostringstream oss;
+	oss << "RcppMatrixView: subscripts out of range: " << i << ", " << j;
+	throw std::range_error(oss.str());
+    }
+    return a[i + d1 * j];
+}
+
 #endif

Modified: pkg/Rcpp/inst/include/RcppVector.h
===================================================================
--- pkg/Rcpp/inst/include/RcppVector.h	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/inst/include/RcppVector.h	2010-05-24 20:16:16 UTC (rev 1304)
@@ -39,4 +39,67 @@
     T *v;
 };
 
+template <typename T> 
+RcppVector<T>::RcppVector(SEXP vec) {
+    int i;
+
+    // The function Rf_isVector returns TRUE for vectors AND
+    // matrices, so it does not distinguish. We could
+    // check the dim attribute here to be sure that it
+    // is not present (i.e., dimAttr == R_NilValue, not 0!).
+    // But it is easier to simply check if it is set via
+    // Rf_isMatrix (in which case we don't have a vector).
+    if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
+	throw std::range_error("RcppVector: invalid numeric vector in constructor");
+    len = Rf_length(vec);
+    v = (T *) R_alloc(len, sizeof(T));
+    if (Rf_isInteger(vec)) {
+	for (i = 0; i < len; i++)
+	    v[i] = (T)(INTEGER(vec)[i]);
+    }	
+    else if (Rf_isReal(vec)) {
+	for (i = 0; i < len; i++)
+	    v[i] = (T)(REAL(vec)[i]);
+    }
+}
+
+template <typename T>
+RcppVector<T>::RcppVector(int _len) {
+    len = _len;
+    v = (T *) R_alloc(len, sizeof(T));
+    for (int i = 0; i < len; i++)
+	v[i] = 0;
+}
+
+template <typename T>
+int RcppVector<T>::size() const { 
+    return len; 
+}
+
+template <typename T>
+inline T& RcppVector<T>::operator()(int i) const {
+    if (i < 0 || i >= len) {
+	std::ostringstream oss;
+	oss << "RcppVector: subscript out of range: " << i;
+	throw std::range_error(oss.str());
+    }
+    return v[i];
+}
+
+template <typename T>
+T *RcppVector<T>::cVector() const {
+    T* tmp = (T *)R_alloc(len, sizeof(T));
+    for (int i = 0; i < len; i++)
+	tmp[i] = v[i];
+    return tmp;
+}
+
+template <typename T>
+std::vector<T> RcppVector<T>::stlVector() const {
+    std::vector<T> tmp(len);
+    for (int i = 0; i < len; i++)
+	tmp[i] = v[i];
+    return tmp;
+}
+
 #endif

Modified: pkg/Rcpp/inst/include/RcppVectorView.h
===================================================================
--- pkg/Rcpp/inst/include/RcppVectorView.h	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/inst/include/RcppVectorView.h	2010-05-24 20:16:16 UTC (rev 1304)
@@ -36,4 +36,31 @@
     T *v;
 };
 
+template <typename T>
+RcppVectorView<T>::RcppVectorView(SEXP vec) {
+    if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
+	throw std::range_error("RcppVectorView: invalid numeric vector in constructor");
+    len = Rf_length(vec);
+    if (Rf_isInteger(vec)) {
+	v = (T *)(INTEGER(vec));
+    } else if (Rf_isReal(vec)) {
+	v = (T *)(REAL(vec));
+    }
+}
+
+template <typename T>
+inline int RcppVectorView<T>::size() const { 
+    return len; 
+}
+
+template <typename T>
+inline T RcppVectorView<T>::operator()(int i) const {
+    if (i < 0 || i >= len) {
+	std::ostringstream oss;
+	oss << "RcppVectorView: subscript out of range: " << i;
+	throw std::range_error(oss.str());
+    }
+    return v[i];
+}
+
 #endif

Modified: pkg/Rcpp/src/RcppMatrix.cpp
===================================================================
--- pkg/Rcpp/src/RcppMatrix.cpp	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/src/RcppMatrix.cpp	2010-05-24 20:16:16 UTC (rev 1304)
@@ -22,103 +22,6 @@
 
 #include <RcppMatrix.h>
 
-template <typename T>
-RcppMatrix<T>::RcppMatrix(SEXP mat) {
-
-    if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
-	throw std::range_error("RcppMatrix: invalid numeric matrix in constructor");
-
-    // Get matrix dimensions
-    SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
-    dim1 = INTEGER(dimAttr)[0];
-    dim2 = INTEGER(dimAttr)[1];
-
-    // We guard against  the possibility that R might pass an integer matrix.
-    // Can be prevented using R code: temp <- as.double(a), dim(temp) <- dim(a)
-    int i,j;
-    int isInt = Rf_isInteger(mat);
-    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
-    a = (T **)R_alloc(dim1, sizeof(T *));
-    for (i = 0; i < dim1; i++)
-	a[i] = m + i*dim2;
-    if (isInt) {
-	for (i=0; i < dim1; i++)
-	    for (j=0; j < dim2; j++)
-		a[i][j] = (T)(INTEGER(mat)[i+dim1*j]);
-    }	
-    else {
-	for (i=0; i < dim1; i++)
-	    for (j=0; j < dim2; j++)
-		a[i][j] = (T)(REAL(mat)[i+dim1*j]);
-    }	
-}
-
-template <typename T>
-RcppMatrix<T>::RcppMatrix(int _dim1, int _dim2) {
-    dim1 = _dim1;
-    dim2 = _dim2;
-    int i,j;
-    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
-    a = (T **)R_alloc(dim1, sizeof(T *));
-    for (i = 0; i < dim1; i++)
-	a[i] = m + i*dim2;
-    for (i=0; i < dim1; i++)
-	for (j=0; j < dim2; j++)
-	    a[i][j] = 0;
-}
-
-template <typename T> int RcppMatrix<T>::getDim1() const { 
-    return dim1; 
-}
-
-template <typename T> int RcppMatrix<T>::getDim2() const { 
-    return dim2; 
-}
-
-template <typename T> int RcppMatrix<T>::rows() const { 
-    return dim1; 
-}
-
-template <typename T> int RcppMatrix<T>::cols() const { 
-    return dim2; 
-}
-
-template <typename T>
-T& RcppMatrix<T>::operator()(int i, int j) const {
-    if (i < 0 || i >= dim1 || j < 0 || j >= dim2) {
-	std::ostringstream oss;
-	oss << "RcppMatrix: subscripts out of range: " << i << ", " << j;
-	throw std::range_error(oss.str());
-    }
-    return a[i][j];
-}
-
-template <typename T>
-T **RcppMatrix<T>::cMatrix() {
-    int i,j;
-    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
-    T **tmp = (T **)R_alloc(dim1, sizeof(T *));
-    for (i = 0; i < dim1; i++)
-	tmp[i] = m + i*dim2;
-    for (i=0; i < dim1; i++)
-	for (j=0; j < dim2; j++)
-	    tmp[i][j] = a[i][j];
-    return tmp;
-}
-
-template <typename T>
-std::vector<std::vector<T> > RcppMatrix<T>::stlMatrix() {
-    int i,j;
-    std::vector<std::vector<T> > temp;
-    for (i = 0; i < dim1; i++) {
-	temp.push_back(std::vector<T>(dim2));
-    }
-    for (i = 0; i < dim1; i++)
-	for (j = 0; j < dim2; j++)
-	    temp[i][j] = a[i][j];
-    return temp;
-}
-
 // Explicit instantiation (required for external linkage)
 template class RcppMatrix<int>;
 template class RcppMatrix<double>;

Modified: pkg/Rcpp/src/RcppMatrixView.cpp
===================================================================
--- pkg/Rcpp/src/RcppMatrixView.cpp	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/src/RcppMatrixView.cpp	2010-05-24 20:16:16 UTC (rev 1304)
@@ -22,51 +22,6 @@
 
 #include <RcppMatrixView.h>
 
-template <typename T>
-RcppMatrixView<T>::RcppMatrixView(SEXP mat) {
-    if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
-	throw std::range_error("RcppMatrixView: invalid numeric matrix in constructor");
-    // Get matrix dimensions
-    SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
-    d1 = INTEGER(dimAttr)[0];
-    d2 = INTEGER(dimAttr)[1];
-    if (Rf_isInteger(mat)) {
-	a = (T *)(INTEGER(mat));
-    } else if (Rf_isReal(mat)) {
-	a = (T *)(REAL(mat));
-    }
-}
-
-template <typename T>
-inline int RcppMatrixView<T>::dim1() const { 
-    return d1; 
-}
-
-template <typename T>
-inline int RcppMatrixView<T>::dim2() const { 
-    return d2; 
-}
-
-template <typename T>
-inline int RcppMatrixView<T>::rows() const { 
-    return d1; 
-}
-
-template <typename T>
-inline int RcppMatrixView<T>::cols() const { 
-    return d2; 
-}
-
-template <typename T>
-inline T RcppMatrixView<T>::operator()(int i, int j) const {
-    if (i < 0 || i >= d1 || j < 0 || j >= d2) {
-	std::ostringstream oss;
-	oss << "RcppMatrixView: subscripts out of range: " << i << ", " << j;
-	throw std::range_error(oss.str());
-    }
-    return a[i + d1 * j];
-}
-
 template class RcppMatrixView<int>;
 template class RcppMatrixView<double>;
 

Modified: pkg/Rcpp/src/RcppVector.cpp
===================================================================
--- pkg/Rcpp/src/RcppVector.cpp	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/src/RcppVector.cpp	2010-05-24 20:16:16 UTC (rev 1304)
@@ -22,69 +22,6 @@
 
 #include <RcppVector.h>
 
-template <typename T> 
-RcppVector<T>::RcppVector(SEXP vec) {
-    int i;
-
-    // The function Rf_isVector returns TRUE for vectors AND
-    // matrices, so it does not distinguish. We could
-    // check the dim attribute here to be sure that it
-    // is not present (i.e., dimAttr == R_NilValue, not 0!).
-    // But it is easier to simply check if it is set via
-    // Rf_isMatrix (in which case we don't have a vector).
-    if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
-	throw std::range_error("RcppVector: invalid numeric vector in constructor");
-    len = Rf_length(vec);
-    v = (T *) R_alloc(len, sizeof(T));
-    if (Rf_isInteger(vec)) {
-	for (i = 0; i < len; i++)
-	    v[i] = (T)(INTEGER(vec)[i]);
-    }	
-    else if (Rf_isReal(vec)) {
-	for (i = 0; i < len; i++)
-	    v[i] = (T)(REAL(vec)[i]);
-    }
-}
-
-template <typename T>
-RcppVector<T>::RcppVector(int _len) {
-    len = _len;
-    v = (T *) R_alloc(len, sizeof(T));
-    for (int i = 0; i < len; i++)
-	v[i] = 0;
-}
-
-template <typename T>
-int RcppVector<T>::size() const { 
-    return len; 
-}
-
-template <typename T>
-inline T& RcppVector<T>::operator()(int i) const {
-    if (i < 0 || i >= len) {
-	std::ostringstream oss;
-	oss << "RcppVector: subscript out of range: " << i;
-	throw std::range_error(oss.str());
-    }
-    return v[i];
-}
-
-template <typename T>
-T *RcppVector<T>::cVector() const {
-    T* tmp = (T *)R_alloc(len, sizeof(T));
-    for (int i = 0; i < len; i++)
-	tmp[i] = v[i];
-    return tmp;
-}
-
-template <typename T>
-std::vector<T> RcppVector<T>::stlVector() const {
-    std::vector<T> tmp(len);
-    for (int i = 0; i < len; i++)
-	tmp[i] = v[i];
-    return tmp;
-}
-
 // Explicit instantiation (required for external linkage)
 template class RcppVector<int>;
 template class RcppVector<double>;

Modified: pkg/Rcpp/src/RcppVectorView.cpp
===================================================================
--- pkg/Rcpp/src/RcppVectorView.cpp	2010-05-21 09:48:31 UTC (rev 1303)
+++ pkg/Rcpp/src/RcppVectorView.cpp	2010-05-24 20:16:16 UTC (rev 1304)
@@ -22,33 +22,6 @@
 
 #include <RcppVectorView.h>
 
-template <typename T>
-RcppVectorView<T>::RcppVectorView(SEXP vec) {
-    if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
-	throw std::range_error("RcppVectorView: invalid numeric vector in constructor");
-    len = Rf_length(vec);
-    if (Rf_isInteger(vec)) {
-	v = (T *)(INTEGER(vec));
-    } else if (Rf_isReal(vec)) {
-	v = (T *)(REAL(vec));
-    }
-}
-
-template <typename T>
-inline int RcppVectorView<T>::size() const { 
-    return len; 
-}
-
-template <typename T>
-inline T RcppVectorView<T>::operator()(int i) const {
-    if (i < 0 || i >= len) {
-	std::ostringstream oss;
-	oss << "RcppVectorView: subscript out of range: " << i;
-	throw std::range_error(oss.str());
-    }
-    return v[i];
-}
-
 template class RcppVectorView<int>;
 template class RcppVectorView<double>;
 



More information about the Rcpp-commits mailing list