[Rcpp-devel] More support of STL algorithms

Romain Francois romain at r-enthusiasts.com
Thu Feb 18 15:11:45 CET 2010


Hi,

I just commited Rcpp::fixed_call to support uses of STL algorithms such 
as std::generate.

Here is an example that mimics :

 > lapply( 1:10, function(n) rnorm(10) )

using Rcpp API :

funx <- cfunction(signature(), '
	
	Language call( Function("rnorm"), 10 ) ;
	std::vector< std::vector<double> > result(10) ;
	std::generate(
		result.begin(), result.end(),
		fixed_call< std::vector<double> >(call)
		) ;
	return wrap( result );
	', Rcpp = TRUE, verbose = FALSE, includes = "using namespace Rcpp;" )

Note that fixed_call embeds the return type, in that case 
std::vector<double> which internally triggers an explicit as.


Here is the actual code of fixed_call:

template <typename OUT=SEXP>
class fixed_call {
public:
	typedef OUT result_type ;
	
	fixed_call( Language call_ ) : call(call_){}
	fixed_call( Function fun ) : call(fun){}
	
	OUT operator()(){
		return as<OUT>( call.eval() ) ;
	}
	
private:
	Language call ;
} ;

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5



More information about the Rcpp-devel mailing list