[Rcpp-devel] passing a bit64 vector to C++ and returning it

Kevin Ushey kevinushey at gmail.com
Wed Jul 12 00:58:21 CEST 2017


My understanding is that `integer64` actually stores its contents in an R
numeric vector, so presumedly you should be able to handle this by
forcefully casting the contents as you access it, e.g.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector input) {

  R_xlen_t n = input.size();
  NumericVector output = no_init(n);

  long long* pInput  = (long long*) dataptr(input);
  long long* pOutput = (long long*) dataptr(output);
  for (R_xlen_t i = 0; i < n; i++)
    *pOutput++ = *pInput++ * 2;

  output.attr("class") = "integer64";
  return output;
}

/*** R
library(bit64)
object <- as.integer64(1:16)
timesTwo(object)
*/

Note that the use of 'long long' here requires a C++11 compiler, or a C++98
compiler with 'long long' provided as an extension.

It's not the nicest solution, but at least this should get you started --
just note that you need to create a NumericVector, set the 'integer64'
class, and fill it as though it were a vector of 'long long' (64-bit signed
integer)s.

Kevin

On Tue, Jul 11, 2017 at 3:44 PM, David Bellot <david.bellot at gmail.com>
wrote:

> Hi,
>
> it looks like I couldn't find a proper solution, even after googling every
> where. The question is simple:
>
> I have a bit64 vector of integers (timestamps of financial tick data, you
> bet ?) and I want to pass it to a C++ function. The good ol' method is to
> convert it to strings, pass it and cast strings back to 64 bits integers in
> C++. And vice and versa when I pass my result back. But the size of my
> vectors are in the range of millions of items, so the method is just a bit
> slow.
>
> Since Rcpp 0.9.8, there is no more support for the LongVector class.
> Papers like http://www.sciencedirect.com/science/article/pii/
> S0098300416307415, despite very interesting, were not very useful to
> solve my simple problem, so I was wondering if there is a better solution ?
>
> reinterpret_cast ?
> GenericVector or RawVector ?
> Anything so obvious that I just missed it ?
>
> And of course, same questions in the other direction: how to return my
> vector back to R and have a bit64 at the end ?
>
> Thanks for your help,
> David
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20170711/ce72e81b/attachment.html>


More information about the Rcpp-devel mailing list