mercredi 3 août 2022

Using R (and Rcpp), how to pass a default 'std::vector

I have a function to SORT:


// 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;
    }
*/








If partial = {-1}, I will treat this like NULL in the base R setup.

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

The function worked fine before adding the partial logic. I could be misunderstanding what the partial is doing.

This demo https://gallery.rcpp.org/articles/sorting/ suggests that partial is a scalar, but I believe it is a positive INTEGER array / vector. So I am trying to apply it correctly.

I am getting an Rcpp warning Warning: No function found for Rcpp::export attribute at sort.cpp pointing to the line PRIOR to

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

Aucun commentaire:

Enregistrer un commentaire