简体   繁体   English

带有 NumericVector 的 Rcpp 交换函数

[英]Rcpp swap function with NumericVector

As I was exploring Rcpp I came to realization that the following swap function在探索 Rcpp 时,我意识到以下交换函数

// swap.cpp
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void swap(NumericVector x) {
  double tmp = x[0];
  x[0] = x[1];
  x[1] = tmp;
}

does not perform the swap when passed an integer vector.传递整数向量时不执行交换。 For example,例如,

x <- 1:2
str(x)
# int [1:2] 1 2
swap(x)
x
# [1] 1 2

However,然而,

y <- c(1,2)
str(y)
# num [1:2] 1 2
swap(y)
y
# [1] 2 1

works fine.工作正常。 My suspicion is that when swap is passed an integer vector x it is forced to make a copy of x that is converted to a NumericVector.我的怀疑是,当swap传递一个整数向量x它被迫制作x的副本,然后将其转换为 NumericVector。 Then anything performed on the copy of x does not effect the original variable that was passed.然后对x的副本执行的任何操作都不会影响传递的原始变量。 Is this reasoning correct?这个推理正确吗? If so, why does the conversion have to result in a copy?如果是这样,为什么转换必须产生副本? Is there a way to write a more robust swap function in which we wouldn't have to worry about accidentally passing an integer vector when we should be passing a numeric vector?有没有办法编写更健壮的swap函数,让我们不必担心在应该传递数字向量时意外传递整数向量?

I apologize if this question has been asked before, but I could not find a suitable answer.如果之前有人问过这个问题,我深表歉意,但我找不到合适的答案。

EDIT:编辑:

The code below does indeed show that a copy of the object is made when an integer vector is passed to swap instead of a numeric vector.下面的代码确实表明,当将整数向量而不是数字向量传递给swap时,会生成对象的副本。

// [[Rcpp::export]]
void where(SEXP x) {
  Rcout << x << std::endl;
}

// [[Rcpp::export]]
void swap(NumericVector x) {
  double tmp = x[0];
  x[0] = x[1];
  x[1] = tmp;
  Rcout << "During swap function: " << x << std::endl;
}

/*** R
test_swap <- function(x) {
  cat("Before the swap function: ") 
  cat(where(x))
  swap(x)
  cat("After the swap function: ") 
  cat(where(x))
}

y <- c(1, 2) // type num
x <- 1:2 // type int

test_swap(y) // swap works because type matches function
#> Before the swap function: 0x116017bf8
#> During swap function: 0x116017bf8
#> After the swap function: 0x116017bf8

test_swap(x) // swap does not work because type does not match function
#> Before the swap function: 0x10d88e468
#> During swap function: 0x116015708
#> After the swap function: 0x10d88e468
*/

Building on @r2evans' comments, here's a minimal implementation:基于@r2evans 的评论,这是一个最小的实现:

#include <Rcpp.h>

template <int T>
void swap_templ(Rcpp::Vector<T> x) {
  double tmp = x[0];
  x[0] = x[1];
  x[1] = tmp;
}
// [[Rcpp::export]]
void swap(SEXP x) {
  switch (TYPEOF(x)) {
  case INTSXP: 
    swap_templ<INTSXP>(x);
    break;
  case REALSXP:
    swap_templ<REALSXP>(x);
    break;
  default:
    Rcpp::Rcout <<
      "\nInput vector must be numeric or integer type" <<
      std::endl;
    break;
  }
}

/*** R
iv <- 1L:3L
dv <- 1:3 + 0.5

R> class(iv)
[1] "integer"

R> class(dv)
[1] "numeric"

R> swap(iv); iv
[1] 2 1 3

R> swap(dv); dv
[1] 2.5 1.5 3.5

R> class(iv)
[1] "integer"

R> class(dv)
[1] "numeric"
*/

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

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