Skip to content

Instantly share code, notes, and snippets.

@jjallaire
Created December 28, 2014 20:46
Show Gist options
  • Save jjallaire/9e9912a109e1e12fe97b to your computer and use it in GitHub Desktop.
Save jjallaire/9e9912a109e1e12fe97b to your computer and use it in GitHub Desktop.
Rcpp modules with sourceCpp
#include <Rcpp.h>
using namespace Rcpp;
using namespace Rcpp;
class Uniform {
public:
Uniform(double min_, double max_) : min(min_), max(max_) {}
NumericVector draw(int n) const {
RNGScope scope;
return runif( n, min, max );
}
double min, max;
};
double uniformRange( Uniform* w) {
return w->max - w->min;
}
RCPP_MODULE(unif_module) {
class_<Uniform>( "Uniform" )
.constructor<double,double>()
.field( "min", &Uniform::min )
.field( "max", &Uniform::max )
.method( "draw", &Uniform::draw )
.method( "range", &uniformRange )
;
}
/*** R
u <- new( Uniform, 0, 10 )
u$draw(10L)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment