r-lib / rray

Simple Arrays
https://rray.r-lib.org
GNU General Public License v3.0
129 stars 12 forks source link

`rray_cube()` integer overflow #182

Closed DavisVaughan closed 5 years ago

DavisVaughan commented 5 years ago

Use xt::cube(xt::cast<double>(x)) which only results in 1 memory allocation but still has the nice properties of not integer overflowing. Base R always returns a double anyways

DavisVaughan commented 5 years ago
template <typename T>
Rcpp::RObject rray__cube_impl(const xt::rarray<T>& x) {
  xt::rarray<double> xt_out = xt::cube(xt::cast<double>(x));

  Rcpp::RObject out = SEXP(xt_out);
  out.attr("dimnames") = rray__dim_names(SEXP(x));

  return out;
}

Rcpp::RObject rray__cube_impl(const xt::rarray<double>& x) {
  xt::rarray<double> xt_out = xt::cube(x);

  Rcpp::RObject out = SEXP(xt_out);
  out.attr("dimnames") = rray__dim_names(SEXP(x));

  return out;
}

// [[Rcpp::export(rng = false)]]
Rcpp::RObject rray__cube(Rcpp::RObject x) {
  DISPATCH_UNARY(rray__cube_impl, x);
}