[Rcpp-commits] r1528 - in pkg/Rcpp/inst: . include/Rcpp unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jun 13 21:37:25 CEST 2010


Author: romain
Date: 2010-06-13 21:37:25 +0200 (Sun, 13 Jun 2010)
New Revision: 1528

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Vector.h
   pkg/Rcpp/inst/unitTests/runit.NumericVector.R
Log:
added Vector::import and Vector::import_transform

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-06-13 18:12:56 UTC (rev 1527)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-13 19:37:25 UTC (rev 1528)
@@ -1,3 +1,8 @@
+2010-06-13  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/Vector.h: added new factory functions for Vector:
+	Vector::import and Vector::import_transform
+
 2010-06-12  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RcppDate.cpp: Simple RcppDate(SEXP) ctor added

Modified: pkg/Rcpp/inst/include/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Vector.h	2010-06-13 18:12:56 UTC (rev 1527)
+++ pkg/Rcpp/inst/include/Rcpp/Vector.h	2010-06-13 19:37:25 UTC (rev 1528)
@@ -725,6 +725,21 @@
 		Base::setSEXP( x) ;
 		UNPROTECT(1) ;
 	}
+
+	template <typename InputIterator>
+	static Vector import( InputIterator first, InputIterator last){
+		Vector v ;
+		v.assign( first , last ) ;
+		return v ;
+	}
+
+	template <typename InputIterator, typename F>
+	static Vector import_transform( InputIterator first, InputIterator last, F f){
+		int n = std::distance( first, last ) ;
+		Vector v( n ) ;
+		std::transform( first, last, v.begin(), f) ;
+		return v ;
+	}
 	
 	template <typename T>
 	void push_back( const T& object){

Modified: pkg/Rcpp/inst/unitTests/runit.NumericVector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.NumericVector.R	2010-06-13 18:12:56 UTC (rev 1527)
+++ pkg/Rcpp/inst/unitTests/runit.NumericVector.R	2010-06-13 19:37:25 UTC (rev 1528)
@@ -71,3 +71,34 @@
 	
 }
 
+test.NumericVector.import <- function(){
+	fx <- cxxfunction( signature(), '
+		std::vector<int> v(10) ;
+		for( int i=0; i<10; i++) v[i] = i ;
+		
+		return IntegerVector::import( v.begin(), v.end() ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkEquals( fx(), 0:9, msg = "IntegerVector::import" ) 
+	
+}
+
+test.NumericVector.import.transform <- function(){
+	
+	inc <- '
+	inline double square( double x){ return x*x; }
+	
+	'
+	fx <- cxxfunction( signature(), '
+		std::vector<double> v(10) ;
+		for( int i=0; i<10; i++) v[i] = i ;
+		
+		return NumericVector::import_transform( v.begin(), v.end(), square ) ;
+	
+	', include = inc, plugin = "Rcpp" )
+	
+	checkEquals( fx(), (0:9)^2, msg = "NumericVector::import_transform" )
+	
+}
+



More information about the Rcpp-commits mailing list