gpt4 book ai didi

c++ - 将 Rcpp 对象分配到 Rcpp 列表中会产生最后一个元素的拷贝

转载 作者:太空狗 更新时间:2023-10-29 20:10:44 27 4
gpt4 key购买 nike

我正在尝试使用 Rcpp::CharacterMatrix 并将每一行转换为 Rcpp::List 中它自己的元素。

但是,我为此编写的函数有一个奇怪的行为,即列表的每个条目都对应于矩阵的最后一行。为什么会这样?这是一些与指针相关的概念吗?请解释。

功能:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List char_expand_list(CharacterMatrix A) {
CharacterVector B(A.ncol());

List output;

for(int i=0;i<A.nrow();i++) {
for(int j=0;j<A.ncol();j++) {
B[j] = A(i,j);
}

output.push_back(B);
}

return output;
}

测试矩阵:

这是传递给上述函数的矩阵A

mat = structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L, 3L))
mat
# [,1] [,2] [,3]
# [1,] "a" "a" "a"
# [2,] "b" "b" "b"
# [3,] "c" "c" "c"

输出:

上面的函数应该将这个矩阵作为输入并返回矩阵的行列表,如下所示:

char_expand_list(mat)
# [[1]]
# [1] "a" "a" "a"
#
# [[2]]
# [1] "b" "b" "b"
#
# [[3]]
# [1] "c" "c" "c"

但是我得到了不同的东西:

char_expand_list(mat)
# [[1]]
# [1] "c" "c" "c"
#
# [[2]]
# [1] "c" "c" "c"
#
# [[3]]
# [1] "c" "c" "c"

可以看出,输出有最后一个元素,例如“c”的矩阵行,对第一个和第二个列表元素重复。为什么会这样?

最佳答案

这里发生的事情很大程度上是 Rcpp 对象工作方式的结果。特别是,CharacterVector充当指向内存位置的指针。通过在 for 之外定义此内存位置循环,结果是一个“全局”指针。也就是说,当更新到 B发生在循环中这随后更新了 B 的所有变体已方便地存储在 Rcpp::List 中.因此,"c" 的重复行自始至终名单。

话虽如此,使用 .push_back() 是一个非常、非常、非常糟糕的想法。在任何 Rcpp数据类型,因为您最终将在不断扩展的对象之间来回复制。复制将发生,因为 Rcpp 数据类型隐藏了底层 SEXP控制必须重新创建的 R 对象。因此,您应该尝试以下方法之一:

  • 重新排列 Rcpp::CharacterVector被创建在第一个 for 内循环和预分配 Rcpp::List空间。
  • 切换到仅使用 C++ 标准库对象并在最后转换为适当的类型。
    • std::liststd::vector<T>输入 T (即 std::string )
    • Rcpp::wrap(x)Rcpp::List 返回正确的对象或修改函数返回类型至 std::list<std::vector<T> > .
  • 预分配 Rcpp::List空间与使用std::vector<T>输入 T (即 std::string )。
  • 预分配 Rcpp::List空间并制作一个clone()将 Rcpp 对象存储在列表中之前。

选项 1

在这里,我们通过移动 B 的声明来重新排列函数。进入第一次循环,预分配列表空间,正常访问输出列表。

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::List char_expand_list_rearrange(Rcpp::CharacterMatrix A) {
Rcpp::List output(A.nrow());

for(int i = 0; i < A.nrow(); i++) {
Rcpp::CharacterVector B(A.ncol());

for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}

output[i] = B;
}

return output;
}

选项 2

这里我们删除了Rcpp::CharacterVector赞成std::vector<std::string>并替换为 Rcpp::List对于 std::list<std::vector<std::string> > .最后,我们将标准对象转换为 Rcpp::List。通过Rcpp::wrap() .

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::List char_expand_std_to_list(Rcpp::CharacterMatrix A) {
std::vector<std::string> B(A.ncol());

std::list<std::vector<std::string> > o;

for(int i = 0 ;i < A.nrow(); i++) {
for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}

o.push_back(B);
}

return Rcpp::wrap(o);
}

给予:

mat = structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L, 3L))
char_expand_std_to_list(mat)
# [[1]]
# [1] "a" "a" "a"
#
# [[2]]
# [1] "b" "b" "b"
#
# [[3]]
# [1] "c" "c" "c"

选项 3

或者,您可以旨在保留 Rcpp::List , 但只需声明大小它提前期待并且仍然使用std::vector<T>元素。

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::List char_expand_list_vec(Rcpp::CharacterMatrix A) {
std::vector<std::string> B(A.ncol());

Rcpp::List o(A.nrow());

for(int i = 0; i < A.nrow(); i++) {
for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}

o[i] = B;
}

return o;
}

选项 4

最后,通过为列表预定义的空间,有一个明确的克隆每次迭代的数据。

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::List char_expand_list_clone(Rcpp::CharacterMatrix A) {
Rcpp::CharacterVector B(A.ncol());
Rcpp::List output(A.nrow());

for(int i = 0; i < A.nrow(); i++) {

for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}

output[i] = clone(B);
}

return output;
}

基准

基准测试结果表明,选项 1 具有重新排列和预分配空间表现最好。亚军是选项 4,它涉及在将每个 vector 保存到 Rcpp::List 之前克隆每个 vector .

library("microbenchmark")
library("ggplot2")

mat = structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L, 3L))

micro_mat_to_list =
microbenchmark(char_expand_list_rearrange(mat),
char_expand_std_to_list(mat),
char_expand_list_vec(mat),
char_expand_list_clone(mat))
micro_mat_to_list
# Unit: microseconds
# expr min lq mean median uq max neval
# char_expand_list_rearrange(mat) 1.501 1.9255 3.22054 2.1965 4.8445 6.797 100
# char_expand_std_to_list(mat) 2.869 3.2035 4.90108 3.7740 6.4415 27.627 100
# char_expand_list_vec(mat) 1.948 2.2335 3.83939 2.7130 5.2585 24.814 100
# char_expand_list_clone(mat) 1.562 1.9225 3.60184 2.2370 4.8435 33.965 100

Benchmark plot

关于c++ - 将 Rcpp 对象分配到 Rcpp 列表中会产生最后一个元素的拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com