integer64 and Rcpp compatibility

十年热恋 提交于 2019-12-07 08:28:47

问题


I will need 64 bits integer in my package in a close future. I'm studying the feasibility based on the bit64 package. Basically I plan to have one or more columns in a data.table with an interger64 S3 class and I plan to pass this table to C++ functions using Rcpp.

The following nanotime example from Rcpp gallery explains clearly how a vector of 64 bits int is built upon a vector of double and explain how to create an integer64 object from C++ to R.

I'm now wondering how to deal with an interger64 from R to C++. I guess I can invert the principle.

void useInt64(NumericVector v) 
{
    double len = v.size();
    std::vector<int64_t> n(len);

    // transfers values 'keeping bits' but changing type
    // using reinterpret_cast would get us a warning
    std::memcpy(&(n[0]), &(v[0]), len * sizeof(double));

    // use n in further computations
}

Is that correct? Is there another way to do that? Can we use a wrapper as<std::vector<int64_t>>(v)? For this last question I guess the conversion is not based on a bit to bit copy.

来源:https://stackoverflow.com/questions/49015493/integer64-and-rcpp-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!