繁体   English   中英

使用 R(和 Rcpp),如何传递默认的 'std::vector<int> ' 数组成 function</int>

[英]Using R (and Rcpp), how to pass a default 'std::vector<int>' array into a function

我有一个 function 来排序:


// https://gallery.rcpp.org/articles/sorting/
// https://www.geeksforgeeks.org/sorting-a-vector-in-c/
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector cpp_sort_numeric_works(NumericVector arr, std::string dir = "ASC" )
    {
    NumericVector _arr = clone(arr);
    if(dir != "ASC")
        {
        std::sort(_arr.begin(), _arr.end(), std::greater<int>());   
        } 
        else    {
                std::sort(_arr.begin(), _arr.end());
                }
    return _arr;
    }

/*
NumericVector _partial_sort(NumericVector arr, int p, std::string dir = "ASC") 
    {
    NumericVector _arr = clone(arr);
    if(dir != "ASC")
        {
        std::nth_element(_arr.begin(), _arr.begin()+p-1, _arr.end(), std::greater<int>());  
        } 
        else    {
                std::nth_element(_arr.begin(), _arr.begin()+p-1, _arr.end());
                }
    return _arr;
    }

// [[Rcpp::export]]
NumericVector cpp_sort_numeric(NumericVector arr, std::string dir = "ASC", const std::vector<int>& partial={-1} )
    {
    NumericVector _arr = clone(arr);
    if(partial[0] == -1)  // only positive values allowed ... 
        {
        if(dir != "ASC")
            {
            std::sort(_arr.begin(), _arr.end(), std::greater<int>());   
            } 
            else    {
                    std::sort(_arr.begin(), _arr.end());
                    }
        }
        else    {
                for (auto& p : partial) 
                    {
                    _arr = _partial_sort(_arr, p, dir);
                    }
                }
    return _arr;
    }
*/








如果partial = {-1} ,我将在base R设置中将其视为 NULL 。

// [[Rcpp::export]]
NumericVector cpp_sort_numeric(NumericVector arr, std::string dir = "ASC", const std::vector<int>& partial={-1} )

function 在添加partial逻辑之前工作正常。 我可能会误解部分在做什么。

这个演示https://gallery.rcpp.org/articles/sorting/表明 partial 是一个标量,但我相信它是一个正 INTEGER 数组/向量。 所以我试图正确地应用它。

我收到Rcpp警告Warning: No function found for Rcpp::export attribute at sort.cpp指向行之前

NumericVector cpp_sort_numeric(NumericVector arr, std::string dir = "ASC", const std::vector<int>& partial={-1} )

这是一个至少可以编译和运行的版本。 我不太确定你想要的partial - 但你所拥有的只是在(记录在案,但我们已经知道你没有时间阅读我们提供的文档)接口合同之外,所以它当然没有构建。

代码

// https://gallery.rcpp.org/articles/sorting/
// https://www.geeksforgeeks.org/sorting-a-vector-in-c/
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector cpp_sort_numeric_works(NumericVector arr, std::string dir = "ASC" ) {
    NumericVector _arr = clone(arr);
    if(dir != "ASC") {
        std::sort(_arr.begin(), _arr.end(), std::greater<int>());
    } else {
        std::sort(_arr.begin(), _arr.end());
    }
    return _arr;
}

NumericVector _partial_sort(NumericVector arr, int p, std::string dir = "ASC") {
    NumericVector _arr = clone(arr);
    if(dir != "ASC") {
        std::nth_element(_arr.begin(), _arr.begin()+p-1, _arr.end(), std::greater<int>());
    } else {
        std::nth_element(_arr.begin(), _arr.begin()+p-1, _arr.end());
    }
    return _arr;
}

// [[Rcpp::export]]
NumericVector cpp_sort_numeric(NumericVector arr, NumericVector partial, std::string dir = "ASC") {
    NumericVector _arr = clone(arr);
    if (partial[0] == -1)  { // only positive values allowed ...
        if(dir != "ASC") {
            std::sort(_arr.begin(), _arr.end(), std::greater<int>());
        } else {
            std::sort(_arr.begin(), _arr.end());
        }
    } else {
        for (auto& p : partial) {
            _arr = _partial_sort(_arr, p, dir);
        }
    }
    return _arr;
}

/*** R
v <- c(1,2,3,2,1,0,-1,2)
cpp_sort_numeric_works(v)
cpp_sort_numeric_works(v, "DESC")
w <- v
w[1] <- -1
cpp_sort_numeric(v, w)
cpp_sort_numeric(v, w, "DESC")
*/

Output

> Rcpp::sourceCpp("~/git/stackoverflow/73222485/answer.cpp")

> v <- c(1,2,3,2,1,0,-1,2)

> cpp_sort_numeric_works(v)
[1] -1  0  1  1  2  2  2  3

> cpp_sort_numeric_works(v, "DESC")
[1]  3  2  2  2  1  1  0 -1

> w <- v

> w[1] <- -1

> cpp_sort_numeric(v, w)
[1] -1  0  1  1  2  2  2  3

> cpp_sort_numeric(v, w, "DESC")
[1]  3  2  2  2  1  1  0 -1
> 

暂无
暂无

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

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