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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 25 16:07:42 CEST 2010


Author: romain
Date: 2010-06-25 16:07:41 +0200 (Fri, 25 Jun 2010)
New Revision: 1729

Modified:
   pkg/Rcpp/inst/include/Rcpp/internal/wrap_forward.h
   pkg/Rcpp/inst/include/RcppFrame.h
   pkg/Rcpp/inst/include/RcppMatrix.h
   pkg/Rcpp/inst/include/RcppResultSet.h
   pkg/Rcpp/inst/include/RcppResultSet__backward.h
   pkg/Rcpp/src/RcppFrame.cpp
   pkg/Rcpp/src/RcppResultSet.cpp
Log:
resolving the segfault

Modified: pkg/Rcpp/inst/include/Rcpp/internal/wrap_forward.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/wrap_forward.h	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/inst/include/Rcpp/internal/wrap_forward.h	2010-06-25 14:07:41 UTC (rev 1729)
@@ -29,11 +29,11 @@
 // don't include it directly
 
 namespace Rcpp{
+	
+template<typename T> SEXP wrap_extra_steps( SEXP x ){
+	return x ;
+} 
 
-template <typename T> SEXP wrap_extra_steps( const SEXP object ){
-	return object ;
-}
-
 } // Rcpp
 
 #endif

Modified: pkg/Rcpp/inst/include/RcppFrame.h
===================================================================
--- pkg/Rcpp/inst/include/RcppFrame.h	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/inst/include/RcppFrame.h	2010-06-25 14:07:41 UTC (rev 1729)
@@ -45,8 +45,13 @@
     std::vector<std::string>& getColNames();
     std::vector<std::vector<ColDatum> >& getTableData();
     void addRow(std::vector<ColDatum> rowData);
-    int rows();
-    int cols();
+    int rows() const ;
+    int cols() const ;
+    
+    // the template is never defined, but 
+    // specializations are (in RcppFrame.cpp)
+    template <int COLUMN_TYPE> SEXP getColumn( int col ) const ;
+    
 };
 
 class ColDatum {
@@ -64,16 +69,16 @@
     void setDatetimeValue(RcppDatetime datetime);
     void setFactorValue(std::string *names, int numNames, int factorLevel);
 
-    double getDoubleValue();
-    int getIntValue();
-    int getLogicalValue();
-    std::string getStringValue();
-    RcppDate getDateValue();
-    double getDateRCode();
-    RcppDatetime getDatetimeValue();
-    void checkFactorType();
-    int getFactorNumLevels();
-    int getFactorLevel();
+    double getDoubleValue() const ;
+    int getIntValue() const ;
+    int getLogicalValue() const ;
+    std::string getStringValue() const ;
+    RcppDate getDateValue() const ;
+    double getDateRCode() const ;
+    RcppDatetime getDatetimeValue() const;
+    void checkFactorType() const ;
+    int getFactorNumLevels() const ;
+    int getFactorLevel() const;
     std::string *getFactorLevelNames();
     std::string getFactorLevelName();
     
@@ -89,7 +94,6 @@
 };
 
 namespace Rcpp{
-	// defined in RcppFrame__backward.h
 	template <> SEXP wrap<RcppFrame>( const RcppFrame& x) ;
 }
 

Modified: pkg/Rcpp/inst/include/RcppMatrix.h
===================================================================
--- pkg/Rcpp/inst/include/RcppMatrix.h	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/inst/include/RcppMatrix.h	2010-06-25 14:07:41 UTC (rev 1729)
@@ -42,7 +42,6 @@
     T **a;
 };
 
-
 template <typename T>
 RcppMatrix<T>::RcppMatrix(SEXP mat) {
 

Modified: pkg/Rcpp/inst/include/RcppResultSet.h
===================================================================
--- pkg/Rcpp/inst/include/RcppResultSet.h	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/inst/include/RcppResultSet.h	2010-06-25 14:07:41 UTC (rev 1729)
@@ -86,7 +86,8 @@
 private:
 	
 	inline void push_back( const std::string& name, SEXP x){
-		values.push_back( PAIR(name, x ) ) ;
+		values.push_back( PAIR(name, PROTECT(x) ) ) ;
+		numProtected++ ;
 	}
 	
 	// defined later because it needs wrap

Modified: pkg/Rcpp/inst/include/RcppResultSet__backward.h
===================================================================
--- pkg/Rcpp/inst/include/RcppResultSet__backward.h	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/inst/include/RcppResultSet__backward.h	2010-06-25 14:07:41 UTC (rev 1729)
@@ -24,7 +24,7 @@
 
 template <typename T> 
 void RcppResultSet::add__impl( const std::string& name, const T& t ){
-	push_back( name, Rcpp::wrap(t) );	
+	push_back( name, Rcpp::wrap(t) );
 }
 
 template <typename T> 

Modified: pkg/Rcpp/src/RcppFrame.cpp
===================================================================
--- pkg/Rcpp/src/RcppFrame.cpp	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/src/RcppFrame.cpp	2010-06-25 14:07:41 UTC (rev 1729)
@@ -94,57 +94,57 @@
     type = COLTYPE_FACTOR;
 }
 
-double ColDatum::getDoubleValue() { 
+double ColDatum::getDoubleValue() const { 
     if (type != COLTYPE_DOUBLE)
 	throw std::range_error("ColDatum::getDoubleValue: wrong data type in getDoubleValue");
     return x; 
 }
 
-int ColDatum::getIntValue() { 
+int ColDatum::getIntValue() const { 
     if (type != COLTYPE_INT)
 	throw std::range_error("ColDatum::getIntValue: wrong data type in getIntValue");
     return i; 
 }
 
-int ColDatum::getLogicalValue() { 
+int ColDatum::getLogicalValue() const { 
     if (type != COLTYPE_LOGICAL)
 	throw std::range_error("ColDatum::getLogicalValue: wrong data type in getLogicalValue");
     return i; 
 }
 
-std::string ColDatum::getStringValue() { 
+std::string ColDatum::getStringValue() const { 
     if (type != COLTYPE_STRING)
 	throw std::range_error("ColDatum::getStringValue: wrong data type in getStringValue");
     return s; 
 }
 
-RcppDate ColDatum::getDateValue() {
+RcppDate ColDatum::getDateValue() const {
     if (type != COLTYPE_DATE)
 	throw std::range_error("ColDatum::getDateValue: wrong data type in getDateValue");
     return d; 
 }
 
-double ColDatum::getDateRCode() { 
+double ColDatum::getDateRCode() const { 
     return (double)(d.getJDN() - RcppDate::Jan1970Offset); 
 }
  
-RcppDatetime ColDatum::getDatetimeValue() {
+RcppDatetime ColDatum::getDatetimeValue() const {
     if (type != COLTYPE_DATETIME)
 	throw std::range_error("ColDatum::getDatetimeValue: wrong data type in getDatetimeValue");
     return RcppDatetime(x); 
 }
 
-void ColDatum::checkFactorType() {
+void ColDatum::checkFactorType() const {
     if (type != COLTYPE_FACTOR)
 	throw std::range_error("ColDatun::checkFactorType: wrong data type in getFactor...");
 }
 
-int ColDatum::getFactorNumLevels() { 
+int ColDatum::getFactorNumLevels() const { 
     checkFactorType(); 
     return numLevels; 
 }
 
-int ColDatum::getFactorLevel() { 
+int ColDatum::getFactorLevel() const { 
     checkFactorType(); 
     return level; 
 }
@@ -159,12 +159,12 @@
     return levelNames[level-1];
 }
 
-RcppFrame::RcppFrame(std::vector<std::string> colNames_) : colNames(colNames_) {
+RcppFrame::RcppFrame(std::vector<std::string> colNames_) : colNames(colNames_), table() {
     if (colNames.size() == 0)
 	throw std::range_error("RcppFrame::RcppFrame: zero length colNames");
 }
 
-RcppFrame::RcppFrame(SEXP df) {
+RcppFrame::RcppFrame(SEXP df) : colNames(), table() {
     if (!Rf_isNewList(df))
 	throw std::range_error("RcppFrame::RcppFrame: invalid data frame.");
     int ncol = Rf_length(df);
@@ -264,11 +264,11 @@
     table.push_back(rowData);
 }
 
-int RcppFrame::rows() { 
+int RcppFrame::rows() const { 
     return table.size(); 
 }
 
-int RcppFrame::cols() { 
+int RcppFrame::cols() const { 
     return table[0].size(); 
 }
 
@@ -278,110 +278,140 @@
 		return Rcpp::wrap( names, names + nlevels ) ;
 	}
 }
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_DOUBLE>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(REALSXP,nr));
+	double* p = REAL(value);
+	for (int j=0; j < nr; j++,p++)
+	*p = table[j][i].getDoubleValue();
+	UNPROTECT(1) ;
+	return value ;
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_INT>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(INTSXP,nr));
+	int* p = INTEGER(value) ;
+	for (int j=0; j < nr; j++,p++)
+	*p = table[j][i].getIntValue();
+	UNPROTECT(1) ;
+	return value ;
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_FACTOR>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(INTSXP,nr));
+	int* p = INTEGER(value) ;
+	for (int j=0; j < nr; j++,p++) {
+	*p = table[j][i].getFactorLevel();
+	}
+	const ColDatum& first = table[0][i] ;  
+	Rf_setAttrib(value, R_LevelsSymbol, 
+		Rcpp::internal::factor_levels( 
+			const_cast<ColDatum&>(first).getFactorLevelNames(), 
+			first.getFactorNumLevels() )
+		);
+	Rf_setAttrib(value, R_ClassSymbol, Rf_mkString("factor") );
+	UNPROTECT(1);
+	return value ;                                                                 
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_STRING>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(STRSXP,nr));
+	for (int j=0; j < nr; j++) {
+	SET_STRING_ELT(value, j, Rf_mkChar(table[j][i].getStringValue().c_str()));
+	}
+	UNPROTECT(1) ;
+	return value ;
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_LOGICAL>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(LGLSXP,nr));
+	int* p = LOGICAL(value) ;
+	for (int j=0; j < nr; j++,p++) {
+	*p = table[j][i].getLogicalValue();
+	}
+	UNPROTECT(1) ;
+	return value ;
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_DATE>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(REALSXP,nr));
+	double* p = REAL(value) ;
+	for (int j=0; j < nr; j++,p++)
+	*p = table[j][i].getDateRCode();
+	Rf_setAttrib(value, R_ClassSymbol, Rf_mkString("Date"));
+	UNPROTECT(1) ;
+	return value ;
+}
+
+template<> 
+SEXP RcppFrame::getColumn<COLTYPE_DATETIME>( int i ) const {
+	int nr = rows() ;
+	SEXP value = PROTECT(Rf_allocVector(REALSXP,nr));
+	double* p = REAL(value) ;
+	for (int j=0; j < nr; j++,p++) {
+	// we could access the seconds as the internal double via getDouble but it's
+	// more proper to use the proper accessor (and if we ever added code ...)
+	*p = table[j][i].getDatetimeValue().getFractionalTimestamp();
+	}
+	Rf_setAttrib(value, R_ClassSymbol, Rcpp::internal::getPosixClasses() );
+	UNPROTECT(1) ;
+	return value ;
+}
+
+
+namespace Rcpp{
 	template <> SEXP wrap<RcppFrame>( const RcppFrame& frame){
 		
-		int numProtected = 0 ;
-	    std::vector<std::string> colNames = const_cast<RcppFrame&>(frame).getColNames();
+		std::vector<std::string> colNames = const_cast<RcppFrame&>(frame).getColNames();
 	    std::vector<std::vector<ColDatum> > table = const_cast<RcppFrame&>(frame).getTableData();
 	    int ncol = colNames.size();
-	    int nrow = table.size();
-	    int levels = 0 ;
-	    SEXP value = R_NilValue ;
-		
-	    Rcpp::List rl( ncol ) ;
-	    Rcpp::CharacterVector nm( ncol ) ;
 	    
+	    SEXP rl = PROTECT( Rf_allocVector( VECSXP, ncol ) ) ;
+	    SEXP nm = PROTECT( Rf_allocVector( STRSXP, ncol ) ) ;
+	    
 	    for (int i=0; i < ncol; i++) { 
-			numProtected = 0 ;
+			SET_STRING_ELT( nm, i, Rf_mkChar(colNames[i].c_str()) ) ;
 			
 			switch( table[0][i].getType() ){
-			case COLTYPE_DOUBLE :
-				{
-				value = PROTECT(Rf_allocVector(REALSXP,nrow));
-			    numProtected++;
-			    double* p = REAL(value);
-			    for (int j=0; j < nrow; j++,p++)
-				p[j] = table[j][i].getDoubleValue();
-				break ;
-				}
-			case COLTYPE_INT:
-				{
-				value = PROTECT(Rf_allocVector(INTSXP,nrow));
-			    numProtected++;
-			    int* p = INTEGER(value) ;
-			    for (int j=0; j < nrow; j++,p++)
-				INTEGER(value)[j] = table[j][i].getIntValue();
-				break ;
-				}
-			case COLTYPE_FACTOR:
-				{
-				value = PROTECT(Rf_allocVector(INTSXP,nrow));
-			    numProtected++;
-			    levels = table[0][i].getFactorNumLevels();
-			    int* p = INTEGER(value) ;
-			    for (int j=0; j < nrow; j++,p++) {
-				int level = table[j][i].getFactorLevel();
-				p[j] = level;
-			    }
-			    Rf_setAttrib(value, R_LevelsSymbol, 
-			    	internal::factor_levels( 
-			    		table[0][i].getFactorLevelNames(), levels)
-			    	);
-			    Rf_setAttrib(value, R_ClassSymbol, Rf_mkString("factor") );
-			    break ;
-			    }
-			case COLTYPE_STRING:
-				{
-			    value = PROTECT(Rf_allocVector(STRSXP,nrow));
-			    numProtected++;
-			    for (int j=0; j < nrow; j++) {
-				SET_STRING_ELT(value, j, Rf_mkChar(table[j][i].getStringValue().c_str()));
-			    }
-			    break;
-			    }
-			case COLTYPE_LOGICAL:
-				{
-				value = PROTECT(Rf_allocVector(LGLSXP,nrow));
-			    numProtected++;
-			    int* p = LOGICAL(value) ;
-			    for (int j=0; j < nrow; j++) {
-				p[j] = table[j][i].getLogicalValue();
-			    }
-			    break;
-			    }
-			case COLTYPE_DATE:  
-				{
-			    value = PROTECT(Rf_allocVector(REALSXP,nrow));
-			    numProtected++;
-			    double* p = REAL(value) ;
-			    for (int j=0; j < nrow; j++)
-				p[j] = table[j][i].getDateRCode();
-			    Rf_setAttrib(value, R_ClassSymbol, Rf_mkString("Date"));
-			    break; 
-			    }
-			case COLTYPE_DATETIME:
-				{
-			    value = PROTECT(Rf_allocVector(REALSXP,nrow));
-			    numProtected++;
-			    double* p = REAL(value) ;
-			    for (int j=0; j < nrow; j++) {
-				// we could access the seconds as the internal double via getDouble but it's
-				// more proper to use the proper accessor (and if we ever added code ...)
-				p[j] = table[j][i].getDatetimeValue().getFractionalTimestamp();
-			    }
-			    Rf_setAttrib(value, R_ClassSymbol, Rcpp::internal::getPosixClasses() );
-			    break; 
-			    }
+
+#undef RCPP_HANDLE_DF
+#define RCPP_HANDLE_DF(TYPE)                        \
+            case TYPE:                              \
+               SET_VECTOR_ELT( rl, i,  frame.getColumn<TYPE>(i) ) ;  \
+               break ;
+				
+			RCPP_HANDLE_DF(COLTYPE_DOUBLE)
+			RCPP_HANDLE_DF(COLTYPE_INT)
+			RCPP_HANDLE_DF(COLTYPE_FACTOR)
+			RCPP_HANDLE_DF(COLTYPE_STRING)
+			RCPP_HANDLE_DF(COLTYPE_LOGICAL)
+			RCPP_HANDLE_DF(COLTYPE_DATE)
+			RCPP_HANDLE_DF(COLTYPE_DATETIME)
+		
 			default:
 				// throw std::range_error("RcppResultSet::add invalid column type");
 				break ;
 				
 			}
-			rl[ i ] = value ;
-			nm[ i ] = colNames[i] ;
-			UNPROTECT( numProtected ) ;
-		} 
-		rl.names() = nm ;
+#undef RCPP_HANDLE_DF
+			
+		}
+		Rf_setAttrib( rl, Rf_install("names"), nm ) ;
+		
+		UNPROTECT(2) ;
 	    return rl ;			
 	}
 }	

Modified: pkg/Rcpp/src/RcppResultSet.cpp
===================================================================
--- pkg/Rcpp/src/RcppResultSet.cpp	2010-06-25 10:34:51 UTC (rev 1728)
+++ pkg/Rcpp/src/RcppResultSet.cpp	2010-06-25 14:07:41 UTC (rev 1729)
@@ -23,14 +23,14 @@
 
 #include <Rcpp.h>
 
-RcppResultSet::RcppResultSet() : numProtected(0) { }
+RcppResultSet::RcppResultSet() : numProtected(0), values() { }
 
 namespace Rcpp { 
 
     // template specialisation for wrap() on the date and datetime classes
     template <> SEXP wrap(const RcppDate &date) {
     return internal::new_date_object( date.getJDN() - RcppDate::Jan1970Offset ) ;
-    }
+    }                                                
 
     template <> SEXP wrap(const RcppDatetime &datetime) {
     return internal::new_posixt_object( datetime.getFractionalTimestamp() ) ;
@@ -165,8 +165,6 @@
 
 void RcppResultSet::add(std::string name, SEXP sexp, bool isProtected) {
     push_back(name, sexp);
-//    if (isProtected)
-//	numProtected++;
 }
 
 void RcppResultSet::add(std::string name, RcppFrame& frame) {
@@ -175,7 +173,7 @@
 
 SEXP RcppResultSet::getReturnList() {
     SEXP rl = PROTECT( Rcpp::wrap( values ) ) ;
-    UNPROTECT(numProtected+1);
+	UNPROTECT(numProtected+1);
     return rl;
 }
 



More information about the Rcpp-commits mailing list