c++ 如何使用XPtr

gz5pxeao  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(39)

我很难理解如何使用Rcpp::XPtr
我想将一个R整数向量列表传递给C++,将其转换为std::vector<std::unordered_set<int> >,将其作为Rcpp::XPtr返回给R,并在以后的C++函数中重用它。

// [[Rcpp::plugins(openmp)]]
#include <Rcpp.h>
#include <vector>
#include <unordered_set>
#include <omp.h>
#include <cstddef>

// [[Rcpp::export]]
Rcpp::XPtr<std::vector<std::unordered_set<int> > > convert_to_cpp_type(Rcpp::List& x) {
  std::size_t x_size = x.size();
  std::vector<std::unordered_set<int> > y(x_size);

  for(std::size_t i = 0; i < x_size; ++i) {
    Rcpp::IntegerVector x_i = x[i];
    y[i].insert(x_i.begin(), x_i.end());
  }

  Rcpp::XPtr<std::vector<std::unordered_set<int> > > z(y);
  return z;
}

// [[Rcpp::export]]
Rcpp::NumericVector use_xptr(SEXP a) {
  Rcpp::XPtr<std::vector<std::unordered_set<int> > > b(a);
  std::size_t b_size = (*b).size();
  std::vector<double> c (b_size);  

  #pragma omp parallel for num_threads(10)
  for(std::size_t i = 0; i < b_size; ++i) {
    c[i] = example_function((*b)[i]);
  }

  return Rcpp::wrap(c);
}

字符串
这段代码不能编译,但它应该提供了我的目标是做什么的想法。第二个函数中的a是第一个函数导出的XPtr
在这段代码中应该有多个错误。然而,即使在浏览了几个小时的Stack Overflow,Rcpp网站和其他各种网站之后,我也没有弄清楚如何正确实现这一点。

qv7cva1a

qv7cva1a1#

你的第一个函数不能编译,因为XPtr构造函数需要一个原始指针,而不是一个std::vector。然而,返回的指针将比无用更糟糕,因为它指向一个在函数返回后超出范围的局部变量。如果你稍后尝试使用它,那么你的会话将崩溃。
下面的函数将返回一个指向std::vector<std::unordered_set>>的有效指针:

library(Rcpp)

cppFunction("

Rcpp::XPtr<std::vector<std::unordered_set<int> > > 
convert_to_cpp_type(Rcpp::List x) {

  typedef std::vector<std::unordered_set<int>> obj;
  std::size_t x_size = x.size();
  obj* y = new obj;

  for(std::size_t i = 0; i < x_size; ++i) {
    Rcpp::IntegerVector x_i = x[i];
    std::unordered_set s(x_i.begin(), x_i.end());
    y->push_back(s);
  }

  Rcpp::XPtr<obj> z(y);
  return z;
  
}")

字符串
为了得到指针所索引的对象的内容,我们需要再次构建一个R列表:

cppFunction("

Rcpp::List use_xptr(SEXP a) {

  Rcpp::XPtr<std::vector<std::unordered_set<int>>> b(a);
  Rcpp::List out;
  for(std::size_t i = 0; i < b->size(); ++i) {
    Rcpp::NumericVector x_i((*b)[i].begin(), (*b)[i].end());
    out.push_back(x_i);
  }
  return out;
  
}")


为了完整起见,让我们创建一个函数,我们可以从R调用它来修改我们的C++对象(因为我们没有example_function

cppFunction("

void do_stuff(Rcpp::XPtr<std::vector<std::unordered_set<int>>> x) {

  x->push_back(std::unordered_set<int> {0, 1, 2, 3});
  return;

}")


在R中,我们可以做:

x <- list(1:10, 3:7)

x
#> [[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> [[2]]
#> [1] 3 4 5 6 7

xptr <- convert_to_cpp_type(x)

xptr
#> <pointer: 0x0000023659ab57c0>

do_stuff(xptr)

use_xptr(xptr)
#> [[1]]
#> [1] 10  9  8  7  6  5  4  3  2  1
#> 
#> [[2]]
#> [1] 7 6 5 4 3
#> 
#> [[3]]
#> [1] 3 2 1 0


这似乎是一个不必要的困难的方式来做的事情.在我看来,更有意义的是发送一个列表到C++,让它做你想要的所有计算,并返回结果.指针并没有真正帮助你在这里.

相关问题