[Rcpp-devel] [Rcpp-commits] r208 - in pkg: inst inst/examples/RcppInline src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Dec 22 10:48:29 CET 2009


Author: romain
Date: 2009-12-22 10:48:29 +0100 (Tue, 22 Dec 2009)
New Revision: 208

Modified:
   pkg/inst/ChangeLog
   pkg/inst/examples/RcppInline/RcppSexpTests.r
   pkg/src/RcppSexp.cpp
   pkg/src/RcppSexp.h
Log:
added raw vector support into RcppSexp

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2009-12-20 21:20:24 UTC (rev 207)
+++ pkg/inst/ChangeLog	2009-12-22 09:48:29 UTC (rev 208)
@@ -1,3 +1,9 @@
+2009-12-21  Romain Francois <francoisromain at free.fr>
+	* src/RcppSexp.{h,cpp} : support for raw vector added into RcppSexp
+
+	* inst/examples/RcppInline/RcppSexpTests.r: examples of raw vector
+	support
+
 2009-12-20  Dirk Eddelbuettel  <edd at debian.org>
 
         * inst/examples/RcppInline/RcppInlineWithLibsExamples.r: Minor
@@ -10,7 +16,7 @@
 	* DESCRIPTION: Descrition fields reworded and expanded to explicitly
 	  mention 'inlining' ability as well as simple SEXP support.
 
-	* src/RcppSexp.{h,cpp}: Switch to R_ProtectObject and R_ReleaseObject
+	* src/RcppSexp.{h,cpp}: Switch to R_PreserveObject and R_ReleaseObject
 	  with a big thanks to Romain for the most appropriate suggestion
 	* src/RcppSexp.{h,cpp}: Added converters for vectors of
 	  int, double, and std::string vectors

Modified: pkg/inst/examples/RcppInline/RcppSexpTests.r
===================================================================
--- pkg/inst/examples/RcppInline/RcppSexpTests.r	2009-12-20 21:20:24 UTC (rev 207)
+++ pkg/inst/examples/RcppInline/RcppSexpTests.r	2009-12-22 09:48:29 UTC (rev 208)
@@ -34,6 +34,16 @@
 funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
 cat(funx(x="abc"), "\n")
 
+cat("\n===Raw (bytes)\n")
+foo <- '
+        Rbyte i = RcppSexp(x).asRaw();
+	std::cout << "Returning twice the value of " << i << " : ";
+	return(RcppSexp( (Rbyte)(2*i) ).asSexp());
+        '
+funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
+cat( funx(x=as.raw(2)), "\n")
+
+
 cat("\n===Int Vector via RcppResultSet.getSEXP\n")
 foo <- '
         std::vector<int> iv = RcppSexp(x).asStdVectorInt();
@@ -73,6 +83,17 @@
 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
 print(funx(x=0.1+2:5))
 
+cat("\n===Raw Vector\n")
+foo <- '
+        std::vector<Rbyte> iv = RcppSexp(x).asStdVectorRaw();
+	std::cout << "Returning twice the value of vector : ";
+        for (size_t i=0; i<iv.size(); i++) {
+            iv[i] = 2*iv[i];
+        }
+ 	return(RcppSexp( iv ).asSexp());
+        '
+funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
+print(funx(x=as.raw(0:9)))
 
 cat("\n===String Vector\n")
 foo <- '

Modified: pkg/src/RcppSexp.cpp
===================================================================
--- pkg/src/RcppSexp.cpp	2009-12-20 21:20:24 UTC (rev 207)
+++ pkg/src/RcppSexp.cpp	2009-12-22 09:48:29 UTC (rev 208)
@@ -35,6 +35,12 @@
     INTEGER(m_sexp)[0] = v;
 }
 
+RcppSexp::RcppSexp(const Rbyte & v) {
+    logTxt("RcppSexp from raw\n");
+    m_sexp = Rf_ScalarRaw(v);
+    R_PreserveObject(m_sexp);
+}
+
 RcppSexp::RcppSexp(const std::string & v) {
     logTxt("RcppSexp from std::string\n");
     m_sexp = Rf_allocVector(STRSXP, 1);
@@ -62,6 +68,16 @@
     }	
 }
 
+RcppSexp::RcppSexp(const std::vector<Rbyte> & v) {
+    logTxt("RcppSexp from vector<Rbyte> \n");
+    int n = v.size();
+    m_sexp = Rf_allocVector(RAWSXP, n);
+    R_PreserveObject(m_sexp);
+    for (int i = 0; i < n; i++) {
+	RAW(m_sexp)[i] = v[i];
+    }	
+}
+
 RcppSexp::RcppSexp(const std::vector<std::string> & v) {
     logTxt("RcppSexp from std::string vector\n");
     int n = v.size();
@@ -111,6 +127,23 @@
     return 0; 	// never reached
 }
 
+Rbyte RcppSexp::asRaw() const {
+    if (Rf_length(m_sexp) != 1) {
+	throw std::range_error("RcppSexp::asRaw expects single value");
+    }
+    switch( TYPEOF(m_sexp) ){
+    	case RAWSXP:
+    		return RAW(m_sexp)[0] ;
+    	case INTSXP:
+    		return (Rbyte)INTEGER(m_sexp)[0] ;
+    	case REALSXP:
+    		return (Rbyte)REAL(m_sexp)[0] ;
+    	default:
+    		throw std::range_error("RcppSexp::asRaw expects raw, double or int");
+    }
+    return (Rbyte)0; 	// never reached
+}
+
 std::string RcppSexp::asStdString() const {
     if (Rf_length(m_sexp) != 1) {
 	throw std::range_error("RcppSexp::asStdString expects single value");
@@ -140,6 +173,34 @@
     return v;
 }
 
+std::vector<Rbyte> RcppSexp::asStdVectorRaw() const {
+    int n = Rf_length(m_sexp);
+    std::vector<Rbyte> v(n);
+    switch( TYPEOF(m_sexp) ){
+    case RAWSXP:
+    	{
+    		v.assign( RAW(m_sexp), RAW(m_sexp)+n ) ;
+    		break ;
+    	}
+    case REALSXP:
+    	{
+    		for (int i = 0; i < n; i++) {
+			    v[i] = (Rbyte)REAL(m_sexp)[i];
+			}
+			break ;
+    	}
+    case INTSXP:
+    	{
+    		for (int i = 0; i < n; i++) {
+			    v[i] = (Rbyte)INTEGER(m_sexp)[i];
+			}
+    		break;
+    	}
+    default:
+    	std::range_error("RcppSexp::asStdVectorRaw expects raw, double or int");
+    }
+    return v;
+}
 
 std::vector<double> RcppSexp::asStdVectorDouble() const {
     int n = Rf_length(m_sexp);

Modified: pkg/src/RcppSexp.h
===================================================================
--- pkg/src/RcppSexp.h	2009-12-20 21:20:24 UTC (rev 207)
+++ pkg/src/RcppSexp.h	2009-12-22 09:48:29 UTC (rev 208)
@@ -30,18 +30,22 @@
     RcppSexp() : m_sexp(R_NilValue) { }
     RcppSexp(const double & v);
     RcppSexp(const int & v);
+    RcppSexp(const Rbyte & v);
     RcppSexp(const std::string & v);
     RcppSexp(const std::vector<int> & v);
     RcppSexp(const std::vector<double> & v);
     RcppSexp(const std::vector<std::string> & v);
+    RcppSexp(const std::vector<Rbyte> & v);
     ~RcppSexp();
 
     double                   asDouble() const;
     int                      asInt() const;
+    Rbyte                    asRaw() const;
     std::string              asStdString() const;
     std::vector<int>         asStdVectorInt() const;
     std::vector<double>      asStdVectorDouble() const;
     std::vector<std::string> asStdVectorString() const;
+    std::vector<Rbyte>       asStdVectorRaw() const;
     SEXP                     asSexp() const;
     
 private:

_______________________________________________
Rcpp-commits mailing list
Rcpp-commits at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-commits


More information about the Rcpp-devel mailing list