简体   繁体   English

Rcpp中的Rcpp Quicksort错误?

[英]Rcpp Quicksort error in R?

I am using RCPP to build a C++ function quicksort for quicksort. 我正在使用RCPP为quicksort构建C ++函数quicksort。 I am trying to compile and load this function through the cxxfunction in R. I need to use the cxxFunction. 我正在尝试通过R中的cxxfunction编译和加载此函数。我需要使用cxxFunction。 I am getting the following message in R: 我在R中收到以下消息:

error: cannot convert 'Rcpp::IntegerVector {aka Rcpp::Vector<13, Rcpp::PreserveStorage>}' to 'int*' for argument '1' to 'int quicksort(int*, int, int)' make: *** [file15ecb338ec.o] Error 1 错误:无法将参数'1'的'Rcpp :: IntegerVector {aka Rcpp :: Vector <13,Rcpp :: PreserveStorage>}'转换为'int *',使'int quicksort(int *,int,int)'使: *** [file15ecb338ec.o]错误1

Can anyone tell me what is wrong with this? 谁能告诉我这是怎么了?

incl <- 'int quicksort(int xx[], int left, int right) {

int i = left; 
int j = right;
int tmp;
int pivot = xx[(left + right) / 2];

/* partition */
while (i <= j) {
while (xx[i] < pivot)
i++;
while (xx[j] > pivot)
j--;
if (i <= j) {
tmp = xx[i];
xx[i] = xx[j];
xx[j] = tmp;
i++;
j--;
}
}

/* recursion */
if (left < j){
quicksort(xx, left, j);
}
if (i < right){
quicksort(xx, i, right);
}

return (quicksort(xx,i,j));
}
'
sortCpp <- cxxfunction(signature( x = "integer",left = "integer",
                                  right = "integer"),
                       body = 'IntegerVector arr(x);
                       int a = as<int>(left);
                       int b = as<int>(right);
                       return wrap(quicksort(arr,a,b));',
                       include = incl,
                       plugin = "Rcpp",
                       verbose = TRUE)

Just to see if I could call a recursive function using cxxfunction I created a factorial function and called this through cxxfunction and it worked fine. 只是为了看看我是否可以使用cxxfunction调用递归函数,我创建了一个阶乘函数,并通过cxxfunction进行了调用,它运行良好。

incl <- 'int factorial(int n)
{
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
'


factcpp <- cxxfunction(signature( x = "integer"),
                       plugin = "Rcpp",
                       include = incl,
                       body = 'int xnew = as<int>(x);
                       return wrap( factorial(x) );
                       ',
                       verbose = TRUE)

The problem is exactly what the compiler says. 问题恰恰是编译器所说的。 Your function expects an int* but you are passing it an Rcpp::IntegerVector . 您的函数需要一个int*但您要传递给它一个Rcpp::IntegerVector Looking at the documentation for Rcpp::IntegerVector , you can retrieve the raw pointer by .begin() , so you would call via quicksort(arr.begin(), a, b) . 查看Rcpp::IntegerVector文档 ,您可以通过.begin()检索原始指针,因此可以通过quicksort(arr.begin(), a, b)调用。

Also, unless you really need to roll your own quicksort, I would just use the Standard Library's std::sort : 另外,除非您真的需要滚动自己的quicksort,否则我将只使用标准库的std::sort

src <- 'Rcpp::IntegerVector v = Rcpp::IntegerVector(vp);
        std::sort(v.begin(), v.end());
        return wrap(v);'
fun <- cxxfunction(signature(vp="integer"), body=src, plugin="Rcpp")
fun( c(5, 2, 7, -3) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM