[Rcpp-commits] r211 - in pkg: . inst/examples/RcppInline src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Dec 26 19:44:11 CET 2009


Author: romain
Date: 2009-12-26 19:44:10 +0100 (Sat, 26 Dec 2009)
New Revision: 211

Modified:
   pkg/DESCRIPTION
   pkg/inst/examples/RcppInline/RcppSexpTests.r
   pkg/src/RcppSexp.cpp
   pkg/src/RcppSexp.h
Log:
added set<[int,double,Rbyte,string]> as accepted input types in RcppSexp, expanded inline example accordingly, bump version number (using the version number in RProtoBuf) 

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2009-12-23 21:10:11 UTC (rev 210)
+++ pkg/DESCRIPTION	2009-12-26 18:44:10 UTC (rev 211)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Rcpp R/C++ interface package
-Version: 0.7.0.1
+Version: 0.7.0.2
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek and David Reiss; based on code written during 

Modified: pkg/inst/examples/RcppInline/RcppSexpTests.r
===================================================================
--- pkg/inst/examples/RcppInline/RcppSexpTests.r	2009-12-23 21:10:11 UTC (rev 210)
+++ pkg/inst/examples/RcppInline/RcppSexpTests.r	2009-12-26 18:44:10 UTC (rev 211)
@@ -128,3 +128,49 @@
         '
 funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
 print(funx(x=c("foo", "bar")))
+
+### using std::set
+cat("\n=== set<int>\n")
+foo <- '
+std::set<int> iv ;
+iv.insert( 0 ) ;
+iv.insert( 1 ) ;
+iv.insert( 0 ) ;
+return RcppSexp( iv ).asSexp();'
+funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>" )
+print(res <- funx())
+stopifnot( identical( res, 0:1 ) )
+
+cat("\n=== set<double>\n")
+foo <- '
+std::set<double> ds;
+ds.insert( 0.0 ); 
+ds.insert( 1.0 );
+ds.insert( 0.0 );
+return(RcppSexp( iv ).asSexp()); '
+funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>")
+print( res <- funx() )
+stopifnot( identical( res, as.numeric(0:1)))
+
+cat("\n=== set<raw>\n")
+foo <- '
+std::set<Rbyte> bs ;
+bs.insert( (Rbyte)0 ) ;
+bs.insert( (Rbyte)1 ) ;
+bs.insert( (Rbyte)0 ) ;
+return(RcppSexp( bs ).asSexp()); '
+funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>")
+print( res <- funx() )
+stopifnot( identical( res, as.raw(0:1)))
+
+cat("\n=== set<string> \n")
+foo <- '
+std::set<std::string> ss ;
+ss.insert( "foo" ) ;
+ss.insert( "bar" ) ;
+ss.insert( "foo" ) ;
+return(RcppSexp( ss ).asSexp()); '
+funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, include = "#include <set>" )
+print( res <- funx() )
+stopifnot( identical( res, c("bar","foo")) )
+

Modified: pkg/src/RcppSexp.cpp
===================================================================
--- pkg/src/RcppSexp.cpp	2009-12-23 21:10:11 UTC (rev 210)
+++ pkg/src/RcppSexp.cpp	2009-12-26 18:44:10 UTC (rev 211)
@@ -77,13 +77,61 @@
     int n = v.size();
     m_sexp = Rf_allocVector(STRSXP, n);
     R_PreserveObject(m_sexp);
-    // maybe we can use transform here
-    // but this might involve creating an iterator over R character vector
-    for (int i = 0; i < n; i++) {
-	SET_STRING_ELT(m_sexp, i, Rf_mkChar(v[i].c_str()));
-    }	
+    int i=0; 
+    std::vector<std::string>::const_iterator it = v.begin() ;
+    while( i<n ){
+    	SET_STRING_ELT(m_sexp, i, Rf_mkChar(it->c_str()));
+    	i++ ;
+    	it++; 
+    }
 }
 
+/* sets */
+
+RcppSexp::RcppSexp(const std::set<int> & v) {
+    logTxt("RcppSexp from set<int>\n");
+    int n = v.size();
+    m_sexp = Rf_allocVector(INTSXP, n);
+    R_PreserveObject(m_sexp);
+    copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
+}
+
+RcppSexp::RcppSexp(const std::set<double> & v) {
+    logTxt("RcppSexp from set<double>\n");
+    int n = v.size();
+    m_sexp = Rf_allocVector(REALSXP, n);
+    R_PreserveObject(m_sexp);
+    copy( v.begin(), v.end(), REAL(m_sexp) ) ;
+}
+
+RcppSexp::RcppSexp(const std::set<Rbyte> & v) {
+    logTxt("RcppSexp from set<Rbyte> \n");
+    int n = v.size();
+    m_sexp = Rf_allocVector(RAWSXP, n);
+    R_PreserveObject(m_sexp);
+    // copy the content of the byte vector 
+    // into the raw vector
+    copy( v.begin(), v.end(), RAW(m_sexp) ) ;
+}
+
+RcppSexp::RcppSexp(const std::set<std::string> & v) {
+    logTxt("RcppSexp from set<string>\n");
+    int n = v.size();
+    m_sexp = Rf_allocVector(STRSXP, n);
+    R_PreserveObject(m_sexp);
+    int i=0; 
+    std::set<std::string>::iterator it = v.begin(); 
+    while( i<n ){
+    	SET_STRING_ELT(m_sexp, i, Rf_mkChar(it->c_str()));
+    	i++ ;
+    	it++; 
+    }
+}
+
+
+
+
+
 RcppSexp::~RcppSexp() {
     logTxt("dtor");
     R_ReleaseObject(m_sexp);

Modified: pkg/src/RcppSexp.h
===================================================================
--- pkg/src/RcppSexp.h	2009-12-23 21:10:11 UTC (rev 210)
+++ pkg/src/RcppSexp.h	2009-12-26 18:44:10 UTC (rev 211)
@@ -23,6 +23,7 @@
 #define RcppSexp_h
 
 #include <RcppCommon.h>
+#include <set>
 
 class RcppSexp {
 public:
@@ -36,6 +37,12 @@
     RcppSexp(const std::vector<double> & v);
     RcppSexp(const std::vector<std::string> & v);
     RcppSexp(const std::vector<Rbyte> & v);
+    
+    RcppSexp(const std::set<int> & v);
+    RcppSexp(const std::set<double> & v);
+    RcppSexp(const std::set<std::string> & v);
+    RcppSexp(const std::set<Rbyte> & v);
+    
     ~RcppSexp();
 
     double                   asDouble() const;



More information about the Rcpp-commits mailing list