使用R中的引用调用将C数组指针转换为Rcpp
Convert C array pointers to Rcpp with call by reference in R
我在C中有以下代码。我是Rcpp的新手,我想将我的C代码转换为Rcpp。
C代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void calculate(const double *d, double *w, int col, int x) {
int i,j;
for (i = 0; i < col; i++){
for (j = 0; j < x; j++){
w[j * col + i]++;
}
}
}
int main(){
int i, col = 2, row = 6;
int x = 5, y = 3, a = 0;
double d[] = {1.0, 0.8, 0.2, 1.0, 0.4, 0.6, 0.6, 0.4, 0.8, 1.0, 1.0, 0.2};
double *w = (double*)calloc((row - a) * col * x, sizeof(double));
for (i = 0; i < row - a; i++) {
calculate(d + (i + a) * col, w + i * col * x, col, x);
}
}
Rcpp代码:
NumericVector calculate(NumericVector d, NumericVector w, int col, int x) {
int i,j;
for (i = 0; i < col; i++){
for (j = 0; j < x; j++){
w[j * col + i]++;
}
}
return w;
}
int i, col = 2, row = 6;
int x = 5, y = 3, a = 0;
NumericVector w((row - a) * col * x);
for (i = 0; i < row - a; i++) {
w = calculate(d + (i + a) * col, w + i * col * x, col, x);
}
这是我的转变,似乎不起作用。我的问题是如何将这些参数d + (i + a) * col
和w + i * col * x
作为指针传递到Rcpp中,因为它不是索引?
如果代码中的下一行按预期工作,
NumericVector w((row - a) * col * x);
为什么不在for
循环中执行同样的操作呢?
for (i = 0; i < row - a; i++) {
NumericVector nvx(d + (i + a) * col);
NumericVector nvy(w + i * col * x);
w = calculate(nvx, nvy, col, x);
}
我的问题是如何将这些参数
d + (i + a) * col
和w + i * col * x
作为指针传递到Rcpp中,因为它不是索引?
可以使用numericVector.begin()
获取指向第一个元素的指针,然后可以使用该指针。以下是的示例
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <memory> // unique_ptr
#include <algorithm> // fill
using namespace Rcpp;
void calculate(const double *d, double *w, int col, int x) {
for (int i = 0; i < col; i++)
for (int j = 0; j < x; j++)
w[j * col + i]++;
}
// [[Rcpp::export]]
void show_example(){
/* Rcpp version */
int const col = 2,
row = 6,
x = 5,
// y = 3, never used?
a = 0,
n_out = (row - a) * col * x;
NumericVector w_rcpp(n_out);
for(int i = 0; i < row - a; i++)
calculate(nullptr /* never used? */, w_rcpp.begin() + i * col * x, col, x);
/* old C++ version. (uses std::unique_ptr to take care of memory
* allocation) */
std::unique_ptr<double[]> w_cpp(new double[n_out]);
/* fill with zeros to get the same*/
std::fill(w_cpp.get(), w_cpp.get() + n_out, 0);
for (int i = 0; i < row - a; ++i)
calculate(nullptr /* never used? */, w_cpp.get() + i * col * x, col, x);
/* Compare the result */
Rcpp::Rcout << "Rcpp: " << w_rcpp << 'n'
<< "Cpp: ";
for(int i = 0; i < n_out; ++i)
Rcpp::Rcout << *(w_cpp.get() + i) << ' ';
Rcpp::Rcout << 'n';
}
/*** R
show_example()
#R> Rcpp: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#R> Cpp: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
不过,现在您没有任何绑定支票。
相关文章:
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空