[Rcpp-devel] Pointer troubles

Christian Gunning xian at unm.edu
Fri Aug 19 04:27:10 CEST 2011


> Comment: A concise example that uses an XPtr in a C++ function is
> still lacking.

Sorry I missed Manuel's example when I last posted.  I guess I was
thinking something like below, which is a direct analog of apply. This
concisely mirrors the semantics in RcppDE (interchangable work
function for a well-defined task).  Here, the user needs only change
inc.  Only thing is, this example seems annoyingly long for inclusion
in the quickref.  Also, I'm not sure whether using inline here is more
or less confusing to the overall idea...

Any thoughts on documentation priorities?
-xian

require(inline); require(Rcpp)

inc <- '
// Define a simple function
double myFun(SEXP xs) { // sum
    Rcpp::NumericVector x(xs);
    double sum = std::accumulate(x.begin(), x.end(), 0.0);
    return(sum);
}
'

src <- '
// Define a pointer to the above function
typedef double (*funcPtr)(SEXP);
return(XPtr<funcPtr>(new funcPtr(&myFun)));
'

src1 <- '
// Define a pointer to the above function
typedef double (*funcPtr)(SEXP);
XPtr<funcPtr> myptr(infun);
// apply myptr to rows/columns of x
NumericMatrix xx(x);
int margin = as<int>(margin_);
int nr = xx.nrow();
int nc = xx.ncol();
if ( margin == 1 ) {
    NumericVector ret(nr);
    for( int i = 0; i<nr; i++) {
        ret[i] = (*myptr)( wrap(xx(i,_)) );
    };
    return(ret);
};
if ( margin == 2 ) {
    NumericVector ret(nc);
    for( int i = 0; i<nc; i++) {
        ret[i] = (*myptr)( wrap(xx(_,i)) );
    };
    return(ret);
};
'

mksum <- cxxfunction(signature(), body = src, includes = inc, plugin="Rcpp")
myapply <- cxxfunction(signature(x='numeric', margin_='integer', infun='XPtr'),
                    body = src1, includes = inc, plugin="Rcpp")

aa = matrix(1:100, nrow=5)
res1 = myapply(aa, 1, mksum() )
res1R = apply(aa, 1, sum)
res2 = myapply(aa, 2, mksum() )
res2R = apply(aa, 2, sum)

-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!


More information about the Rcpp-devel mailing list