On 14 March 2013 at 12:24, Smith, Dale wrote:
| Write it up for Rcpp Gallery.
Yup. Our thoughts too. Here is a slightly modified version which works as a
single cpp file thanks to the wonders of sourceCpp() --- bigmemory headers
are found automagically, and the example is executed too.
Dirk
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
#include <Rcpp.h>
// The next line is all it takes to find the bigmemory
// headers -- thanks to the magic of Rcpp attributes
// [[Rcpp::depends(bigmemory)]]
#include <bigmemory/MatrixAccessor.hpp>
// We define a simple function, and pass the incoming XPtr as a SEXP;
// we could also pass a templated XPtr. Function returns only a bool.
// [[Rcpp::export]]
bool fun(SEXP A) {
Rcpp::XPtr<BigMatrix> bigMat(A);
MatrixAccessor<int> Am(*bigMat);
int nrows = bigMat->nrow();
int ncolumns = bigMat->ncol();
for (int j = 0; j < ncolumns; j++){
for (int i = 1; i < nrows; i++){
Am[j][i] = Am[j][i] + Am[j][i-1];
}
}
return Rcpp::wrap(true);
}