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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 25 12:29:58 CEST 2010


Author: romain
Date: 2010-06-25 12:29:58 +0200 (Fri, 25 Jun 2010)
New Revision: 1726

Modified:
   pkg/Rcpp/inst/include/Rcpp.h
   pkg/Rcpp/inst/include/RcppFrame.h
   pkg/Rcpp/src/RcppFrame.cpp
   pkg/Rcpp/src/RcppResultSet.cpp
Log:
wrap( RcppFrame ) factored out of RcppResultSet

Modified: pkg/Rcpp/inst/include/Rcpp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp.h	2010-06-25 09:12:14 UTC (rev 1725)
+++ pkg/Rcpp/inst/include/Rcpp.h	2010-06-25 10:29:58 UTC (rev 1726)
@@ -81,5 +81,6 @@
 #include <Rcpp/sugar/sugar.h>
 
 #include <RcppResultSet__backward.h>
+#include <RcppList__backward.h>
 
 #endif

Modified: pkg/Rcpp/inst/include/RcppFrame.h
===================================================================
--- pkg/Rcpp/inst/include/RcppFrame.h	2010-06-25 09:12:14 UTC (rev 1725)
+++ pkg/Rcpp/inst/include/RcppFrame.h	2010-06-25 10:29:58 UTC (rev 1726)
@@ -49,7 +49,6 @@
     int cols();
 };
 
-
 class ColDatum {
 public:
     ColDatum();  
@@ -77,7 +76,7 @@
     int getFactorLevel();
     std::string *getFactorLevelNames();
     std::string getFactorLevelName();
-
+    
 private:
     ColType type;
     std::string s;
@@ -89,5 +88,9 @@
     RcppDate d;
 };
 
+namespace Rcpp{
+	// defined in RcppFrame__backward.h
+	template <> SEXP wrap<RcppFrame>( const RcppFrame& x) ;
+}
 
 #endif

Modified: pkg/Rcpp/src/RcppFrame.cpp
===================================================================
--- pkg/Rcpp/src/RcppFrame.cpp	2010-06-25 09:12:14 UTC (rev 1725)
+++ pkg/Rcpp/src/RcppFrame.cpp	2010-06-25 10:29:58 UTC (rev 1726)
@@ -20,7 +20,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <RcppFrame.h>
+#include <Rcpp.h>
 
 ColDatum::ColDatum() : type(COLTYPE_UNKNOWN), level(0) { }
 
@@ -272,4 +272,105 @@
     return table[0].size(); 
 }
 
+namespace Rcpp{
+namespace internal {
+	inline SEXP factor_levels( std::string* names, int nlevels){
+		return Rcpp::wrap( names, names + nlevels ) ;
+	}
+}
+	template <> SEXP wrap<RcppFrame>( const RcppFrame& frame){
+		
+		int numProtected = 0 ;
+	    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 ) ;
+	    
+	    for (int i=0; i < ncol; i++) { 
+			numProtected = 0 ;
+			
+			switch( table[0][i].getType() ){
+			case COLTYPE_DOUBLE :
+				value = PROTECT(Rf_allocVector(REALSXP,nrow));
+			    numProtected++;
+			    for (int j=0; j < nrow; j++)
+				REAL(value)[j] = table[j][i].getDoubleValue();
+				break ;
+				
+			case COLTYPE_INT:
+				value = PROTECT(Rf_allocVector(INTSXP,nrow));
+			    numProtected++;
+			    for (int j=0; j < nrow; j++)
+				INTEGER(value)[j] = table[j][i].getIntValue();
+				break ;
+				
+			case COLTYPE_FACTOR:
+				value = PROTECT(Rf_allocVector(INTSXP,nrow));
+			    numProtected++;
+			    levels = table[0][i].getFactorNumLevels();
+			    for (int k=0; k < levels; k++)
+				for (int j=0; j < nrow; j++) {
+				int level = table[j][i].getFactorLevel();
+				INTEGER(value)[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++;
+			    for (int j=0; j < nrow; j++) {
+				LOGICAL(value)[j] = table[j][i].getLogicalValue();
+			    }
+			    break;
+			    
+			case COLTYPE_DATE:    
+			    value = PROTECT(Rf_allocVector(REALSXP,nrow));
+			    numProtected++;
+			    for (int j=0; j < nrow; j++)
+				REAL(value)[j] = table[j][i].getDateRCode();
+			    Rf_setAttrib(value, R_ClassSymbol, Rf_mkString("Date"));
+			    break; 
+			    
+			case COLTYPE_DATETIME:
+			    value = PROTECT(Rf_allocVector(REALSXP,nrow));
+			    numProtected++;
+			    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 ...)
+				REAL(value)[j] = table[j][i].getDatetimeValue().getFractionalTimestamp();
+			    }
+			    Rf_setAttrib(value, R_ClassSymbol, Rcpp::internal::getPosixClasses() );
+			    break; 
+			    
+			default:
+				// throw std::range_error("RcppResultSet::add invalid column type");
+				break ;
+				
+			}
+			rl[ i ] = value ;
+			nm[ i ] = colNames[i] ;
+			UNPROTECT( numProtected ) ;
+		} 
+		rl.names() = nm ;
+	    return rl ;			
+	}
+}	
 

Modified: pkg/Rcpp/src/RcppResultSet.cpp
===================================================================
--- pkg/Rcpp/src/RcppResultSet.cpp	2010-06-25 09:12:14 UTC (rev 1725)
+++ pkg/Rcpp/src/RcppResultSet.cpp	2010-06-25 10:29:58 UTC (rev 1726)
@@ -165,87 +165,12 @@
 
 void RcppResultSet::add(std::string name, SEXP sexp, bool isProtected) {
     push_back(name, sexp);
-    if (isProtected)
-	numProtected++;
+//    if (isProtected)
+//	numProtected++;
 }
 
-// FIXME: this code belongs to RcppFrame::operator SEXP
 void RcppResultSet::add(std::string name, RcppFrame& frame) {
-    std::vector<std::string> colNames = frame.getColNames();
-    std::vector<std::vector<ColDatum> > table = frame.getTableData();
-    int ncol = colNames.size();
-    int nrow = table.size();
-    SEXP rl = PROTECT(Rf_allocVector(VECSXP,ncol));
-    SEXP nm = PROTECT(Rf_allocVector(STRSXP,ncol));
-    numProtected += 2;
-    for (int i=0; i < ncol; i++) {
-	SEXP value, names;
-	if (table[0][i].getType() == COLTYPE_DOUBLE) {
-	    value = PROTECT(Rf_allocVector(REALSXP,nrow));
-	    numProtected++;
-	    for (int j=0; j < nrow; j++)
-		REAL(value)[j] = table[j][i].getDoubleValue();
-	} else if (table[0][i].getType() == COLTYPE_INT) {
-	    value = PROTECT(Rf_allocVector(INTSXP,nrow));
-	    numProtected++;
-	    for (int j=0; j < nrow; j++)
-		INTEGER(value)[j] = table[j][i].getIntValue();
-	} else if (table[0][i].getType() == COLTYPE_FACTOR) {
-	    value = PROTECT(Rf_allocVector(INTSXP,nrow));
-	    numProtected++;
-	    int levels = table[0][i].getFactorNumLevels();
-	    names = PROTECT(Rf_allocVector(STRSXP,levels));
-	    numProtected++;
-	    std::string *levelNames = table[0][i].getFactorLevelNames();
-	    for (int k=0; k < levels; k++)
-		SET_STRING_ELT(names, k, Rf_mkChar(levelNames[k].c_str()));
-	    for (int j=0; j < nrow; j++) {
-		int level = table[j][i].getFactorLevel();
-		INTEGER(value)[j] = level;
-	    }
-	    Rf_setAttrib(value, R_LevelsSymbol, names);
-	    SEXP factorclass = PROTECT(Rf_allocVector(STRSXP,1));
-	    numProtected++;
-	    SET_STRING_ELT(factorclass, 0, Rf_mkChar("factor"));
-	    Rf_setAttrib(value, R_ClassSymbol, factorclass); 
-	} else if (table[0][i].getType() == 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()));
-	    }
-	} else if (table[0][i].getType() == COLTYPE_LOGICAL) {
-	    value = PROTECT(Rf_allocVector(LGLSXP,nrow));
-	    numProtected++;
-	    for (int j=0; j < nrow; j++) {
-		LOGICAL(value)[j] = table[j][i].getLogicalValue();
-	    }
-	} else if (table[0][i].getType() == COLTYPE_DATE) {
-	    value = PROTECT(Rf_allocVector(REALSXP,nrow));
-	    numProtected++;
-	    for (int j=0; j < nrow; j++)
-		REAL(value)[j] = table[j][i].getDateRCode();
-	    SEXP dateclass = PROTECT(Rf_allocVector(STRSXP,1));
-	    numProtected++;
-	    SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
-	    Rf_setAttrib(value, R_ClassSymbol, dateclass); 
-	} else if (table[0][i].getType() == COLTYPE_DATETIME) {
-	    value = PROTECT(Rf_allocVector(REALSXP,nrow));
-	    numProtected++;
-	    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 ...)
-		REAL(value)[j] = table[j][i].getDatetimeValue().getFractionalTimestamp();
-	    }
-	    Rf_setAttrib(value, R_ClassSymbol, Rcpp::internal::getPosixClasses() );
-	} else {
-	    throw std::range_error("RcppResultSet::add invalid column type");
-	}
-	SET_VECTOR_ELT(rl, i, value);
-	SET_STRING_ELT(nm, i, Rf_mkChar(colNames[i].c_str()));
-    }
-    Rf_setAttrib(rl, R_NamesSymbol, nm);
-    push_back(name, rl);
+    add__impl( name, frame ) ;
 }
 
 SEXP RcppResultSet::getReturnList() {



More information about the Rcpp-commits mailing list