how can I handle vectors without knowing the type in Rcpp

假如想象 提交于 2019-11-28 07:41:11

Don't use push_back on Rcpp types. The way Rcpp vectors are currently implemented this requires copying all of the data each time. This is a very expensive operation.

We have RCPP_RETURN_VECTOR for dispatching, this requires that you write a template function taking a Vector as input.

#include <Rcpp.h>
using namespace Rcpp ;

template <int RTYPE>
Vector<RTYPE> first_two_impl( Vector<RTYPE> xin){
    Vector<RTYPE> xout(2) ;
    for( int i=0; i<2; i++ ){
        xout[i] = xin[i] ;    
    }
    return xout ;
}

// [[Rcpp::export]]
SEXP first_two( SEXP xin ){
  RCPP_RETURN_VECTOR(first_two_impl, xin) ;
}

/*** R
    first_two( 1:3 )
    first_two( letters )
*/

Just sourceCpp this file, this will also run the R code which calls the two functions. Actually, the template could be simpler, this would work too:

template <typename T>
T first_two_impl( T xin){
    T xout(2) ;
    for( int i=0; i<2; i++ ){
        xout[i] = xin[i] ;    
    }
    return xout ;
}

The template parameter T only needs:

  • A constructor taking an int
  • An operator[](int)

Alternatively, this might be a job for dplyr vector visitors.

#include <dplyr.h>
// [[Rcpp::depends(dplyr,BH)]]

using namespace dplyr ;
using namespace Rcpp ;

// [[Rcpp::export]]
SEXP first_two( SEXP data ){
    VectorVisitor* v = visitor(data) ;
    IntegerVector idx = seq( 0, 1 ) ;
    Shield<SEXP> out( v->subset(idx) ) ;
    delete v ;
    return out ;
}

visitors let you do a set of things on a vector regardless of the type of data it holds.

> first_two(letters)
[1] "a" "b"

> first_two(1:10)
[1] 1 2

> first_two(rnorm(10))
[1] 0.4647190 0.9790888

You need to pick a type (ie do not use signature="SEXP" [ oh and you should look into Attributes anyway ]).

Or you keep the SEXP type, and dispatch internally. See for example this post on the Rcpp Gallery.

Edit: And C is of course statically typed. These very switches depending on the type are all over the R sources too. No free lunch here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!