简体   繁体   English

如何使用R和Rcpp删除NumericVector中的元素以进行递归

[英]How to remove an element in NumericVector for a recursion using R and Rcpp

i was trying to learn more about how to use Rcpp package for R. So i started testing basic sorting algorithms using Rcpp. 我试图了解更多关于如何使用Rcpp包的信息。所以我开始使用Rcpp测试基本的排序算法。 I was starting at Hadley Wickham tutorial here . 我在这里开始了Hadley Wickham教程。

I successfully implemented insertion sort recursively this way: 我以这种方式递归地成功实现了插入排序:

library(Rcpp)

vetor<-sample(100)
vetor
cppFunction("
    NumericVector insertionsortRC(NumericVector vetor, int n) {
        double aux;
        int i;

        if(n>1) {
            insertionsortRC(vetor,n-1);
            aux=vetor[n-1];
            i=n-1;
            while(vetor[i-1]>aux && i>=0 ) {
                vetor[i]=vetor[i-1];
                i--;
                }
            vetor[i]=aux;
            }

        return vetor;
        }
    ")

But the function ask for 2 arguments, then i tried this way: 但函数要求2个参数,然后我尝试这样:

cppFunction("
    NumericVector insertionsortRC(NumericVector vetor) {
        int n = vetor.size();

        double aux;
        int i;

        if(n>1) {
            vetor.erase(n-1);
            insertionsortRC(vetor);
            aux=vetor[n-1];
            i=n-1;
            while(vetor[i-1]>aux && i>=0 ) {
                vetor[i]=vetor[i-1];
                i--;
                }
            vetor[i]=aux;
            }

        return vetor;
        }
    ")

I think erase was not a good idea here, it seem that i erase the element from memory and cant recover it later, after the recursion call. 我认为擦除在这里不是一个好主意,似乎我从内存中擦除了元素,并且在递归调用之后无法恢复它。 I thought also that the problem could be on vetor.erase(n-1); 我还想到问题可能在于vetor.erase(n-1); line, tried vetor.erase(n); line,试过vetor.erase(n); and it compiled, but didn't work at all. 它编译了,但根本没用。

With vetor.erase(n); 使用vetor.erase(n); i got the following error in R: 我在R中遇到以下错误:

insertionsortRC(vetor) * Error in `/usr/lib/R/bin/exec/R': malloc(): memory corruption: 0x098db548 * insertionsortRC(vetor) *`/ usr / lib / R / bin / exec / R'出错:malloc():内存损坏:0x098db548 *

with vetor.erase(n-1); with vetor.erase(n-1); it got a strange output: 它有一个奇怪的输出:

insertionsortRC(vetor) [1] 3.607393e-313 3.300000e+01 3.100000e+01 8.600000e+01 2.500000e+01 7.000000e+01 [7] 4.000000e+01 8.800000e+01 8.100000e+01 1.300000e+01 8.500000e+01 8.700000e+01 [13] 3.900000e+01 6.000000e+01 6.400000e+01 1.000000e+01 8.200000e+01 8.900000e+01 [19] 1.400000e+01 6.600000e+01 3.600000e+01 1.500000e+01 9.600000e+01 2.600000e+01 [25] 4.000000e+00 5.400000e+01 2.900000e+01 8.300000e+01 5.500000e+01 6.800000e+01 [31] 9.100000e+01 6.000000e+00 1.000000e+02 5.100000e+01 7.000000e+00 5.300000e+01 [37] 9.900000e+01 6.500000e+01 2.300000e+01 9.400000e+01 5.700000e+01 9.000000e+01 [43] 3.200000e+01 4.700000e+01 1.600000e+01 5.000000e+01 2.800000e+01 3.000000e+00 [49] 9.800000e+01 1.100000e+01 1.800000e+01 7.600000e+01 6.300000e+01 7.700000e+01 [55] 7.400000e+01 4.900000e+01 8.000000e+00 9.700000e+01 1.200000e+01 2.700000e+01 [61] 3.500000e+01 7.900000e+01 8.000000e+01 2.000000e+01 6.700000e+01 9.300000e+01 [67] 5.000000e+00 5.600000e+01 9.000000e+00 3.700000e+01 2.400000 insertionsortRC(vetor)[1] 3.607393e-313 3.300000e + 01 3.100000e + 01 8.600000e + 01 2.500000e + 01 7.000000e + 01 [7] 4.000000e + 01 8.800000e + 01 8.100000e + 01 1.300000e + 01 8.500000e + 01 8.700000e + 01 [13] 3.900000e + 01 6.000000e + 01 6.400000e + 01 1.000000e + 01 8.200000e + 01 8.900000e + 01 [19] 1.400000e + 01 6.600000e + 01 3.600000e + 01 1.500000e + 01 9.600000e + 01 2.600000e + 01 [25] 4.000000e + 00 5.400000e + 01 2.900000e + 01 8.300000e + 01 5.500000e + 01 6.800000e + 01 [31] 9.100000e + 01 6.000000e + 00 1.000000e + 02 5.100000e + 01 7.000000e + 00 5.300000e + 01 [37] 9.900000e + 01 6.500000e + 01 2.300000e + 01 9.400000e + 01 5.700000e + 01 9.000000e + 01 [43] 3.200000e + 01 4.700000e + 01 1.600000e + 01 5.000000e + 01 2.800000e + 01 3.000000e + 00 [49] 9.800000e + 01 1.100000e + 01 1.800000e + 01 7.600000e + 01 6.300000e + 01 7.700000e + 01 [55] 7.400000e + 01 4.900000e + 01 8.000000e + 00 9.700000e + 01 1.200000e + 01 2.700000e + 01 [61] 3.500000e + 01 7.900000e + 01 8.000000e + 01 2.000000e + 01 6.700000e + 01 9.300000e + 01 [67] 5.000000e + 00 5.600000e + 01 9.000000e + 00 3.700000e + 01 2.400000 e+01 9.200000e+01 [73] 6.900000e+01 3.800000e+01 4.400000e+01 1.700000e+01 4.600000e+01 4.300000e+01 [79] 3.400000e+01 1.900000e+01 2.000000e+00 9.500000e+01 7.200000e+01 1.000000e+00 [85] 6.100000e+01 4.100000e+01 6.200000e+01 2.200000e+01 4.200000e+01 2.100000e+01 [91] 8.400000e+01 4.800000e+01 7.800000e+01 7.300000e+01 3.000000e+01 5.900000e+01 [97] 5.800000e+01 5.200000e+01 7.500000e+01 e + 01 9.200000e + 01 [73] 6.900000e + 01 3.800000e + 01 4.400000e + 01 1.700000e + 01 4.600000e + 01 4.300000e + 01 [79] 3.400000e + 01 1.900000e + 01 2.000000e + 00 9.500000 e + 01 7.200000e + 01 1.000000e + 00 [85] 6.100000e + 01 4.100000e + 01 6.200000e + 01 2.200000e + 01 4.200000e + 01 2.100000e + 01 [91] 8.400000e + 01 4.800000e + 01 7.800000 e + 01 7.300000e + 01 3.000000e + 01 5.900000e + 01 [97] 5.800000e + 01 5.200000e + 01 7.500000e + 01

Could someone tell me if: 01. Is it possible to implement this code, like this, using Rcpp and R, calling the function with only one argument, the vector of data? 有人可以告诉我是否:01。是否可以使用Rcpp和R实现此代码,只使用一个参数调用函数,即数据向量? 02. How to do it correctly? 02.如何正确地做到这一点?

Briefly: 简述:

  1. The good news is that you have your compilation working. 好消息是你的编译工作正常。

  2. The not-so-good news is the segfault. 不太好的消息是段错误。 Likely a logic error in your code. 可能是代码中的逻辑错误。

  3. In general, adding or removing to NumericVector etc is a bad idea . 通常,添加或删除NumericVector等是一个坏主意 These are shallow types which directly connect to the R memory of the same object ("no copies"). 这些是浅类型,它们直接连接到同一对象的R存储器(“无副本”)。 This means extending or removing is costly. 这意味着延长或移除成本很高。 Consider using an STL std::vector<double> . 考虑使用STL std::vector<double> All this is documented. 所有这些都记录在案。

A few things: 一些东西:

vetor.erase(n) is undefined behavior. vetor.erase(n)是未定义的行为。 The first index is 0 , the last is n-1 . 第一个索引是0 ,最后一个是n-1 erase does not do bounds check because everybody would have to pay the price. erase不做边界检查,因为每个人都必须付出代价。 Instead it assumes, as it is common in the C++ world that the function is used correctly. 相反,它假设,正如在C ++世界中常见的那样,正确使用了该函数。

Learn about std::sort . 了解std::sort it is likely to be more efficient than home backed sort implementation, especially insertion sort. 它可能比家庭支持的排序实现更有效,尤其是插入排序。

Rcpp vectors have a sort method. Rcpp向量具有sort方法。 So a NumericVector can sort itself. 因此NumericVector可以对自己进行排序。

Learn about attributes, ie with the Rcpp-attributes vignette, this is simpler to use and this will give you a way to deal with default arguments. 了解属性,即使用Rcpp-attributes插图,这更容易使用,这将为您提供一种处理默认参数的方法。

Try: vector.erase(vector.begin()+desiredElement) 尝试: vector.erase(vector.begin()+desiredElement)

That should take care of your problem. 这应该照顾你的问题。

The NumericVector type seems to be related to std::vector at least in the way it handles insertion and removal of data, which means that you have to use iterators (However, I am not an Rcpp guru). NumericVector类型似乎与std::vector ,至少在它处理数据的插入和删除方式上,这意味着你必须使用迭代器(但是,我不是Rcpp大师)。

Brian 布赖恩

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

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