Daniel C. Dillon — written Jun 25, 2016 — source
A while back, I saw a post on StackOverflow where the user was trying to use
Rcpp::sugar::sum()
on an RcppParallel::RVector
. Obviously, this does not
work (as Rcpp Sugar pertains to Rcpp types, but not RcppParallel which cannot
rely on SEXP
-based representation to allow multi-threaded execution). It
raised the question “Why doesn’t something more generic exist to
provide functions with R semantics that can be used on arbitrary data
structures?” As a result, I set out to create a set of such functions
in Rcpp::algorithm
which follow the pattern of std::algorithm
.
Currently Rcpp::algorithm
contains only a few simple
functions. If these are found to be useful, more will be added. Examples
of using the currently implemented iterator-based functions are below.
Through the coding of these simple “algorithms”, a few needs arose.
First, the ability to deduce the appropriate C
numeric type given an Rcpp
iterator was necessary. This gave birth to the
Rcpp::algorithm::helpers::decays_to_ctype
and
Rcpp::algorithm::helpers::ctype
type traits. Given a type, these allow you
to determine whether it can be cast to a C
numeric type and which type that
would be.
Second, the need arose for more information about R
types. This gave birth
to the Rcpp::algorithm::helpers::rtype
traits. These are defined as
follows:
These additional benefits may actually prove more useful than the algorithms themselves. Only time will tell.
There are now some simple iterator-based algorithms that can be used with any
structure that supports iterators. They apply the same semantics as the
analogous Rcpp::sugar
functions, but give us more flexibility in their
usage. If you find these to be useful, feel free to request more.
tags: sugar
Tweet