Last active
December 4, 2016 13:06
-
-
Save artemklevtsov/5e1155551d90196adacb86ae80546588 to your computer and use it in GitHub Desktop.
rep functions family implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
using namespace Rcpp; | |
// [[Rcpp::plugins(cpp11)]] | |
template <int RTYPE> | |
Vector<RTYPE> rep_each_impl(const Vector<RTYPE>& x, std::size_t each) { | |
std::size_t n = x.size(); | |
Vector<RTYPE> res = no_init(n * each); | |
auto begin = res.begin(); | |
for (std::size_t i = 0; i < n; begin += each, ++i) | |
std::fill(begin, begin + each, x[i]); | |
return res; | |
} | |
// [[Rcpp::export]] | |
SEXP rep_each(SEXP x, std::size_t each) { | |
RCPP_RETURN_VECTOR(rep_each_impl, x, each); | |
} | |
template <int RTYPE> | |
Vector<RTYPE> rep_times_impl(const Vector<RTYPE>& x, std::size_t times) { | |
std::size_t n = x.size(); | |
Vector<RTYPE> res = no_init(n * times); | |
auto begin = x.begin(), end = x.end(); | |
auto dest = res.begin(); | |
for (std::size_t i = 0; i < times; dest += n, ++i) | |
std::copy(begin, end, dest); | |
return res; | |
} | |
// [[Rcpp::export]] | |
SEXP rep_times(SEXP x, std::size_t times) { | |
RCPP_RETURN_VECTOR(rep_times_impl, x, times); | |
} | |
template <int RTYPE> | |
Vector<RTYPE> rep_int_impl(const Vector<RTYPE>& x, const IntegerVector& times) { | |
std::size_t n = x.size(), ntimes = times.size(); | |
if (n == ntimes) { | |
std::size_t n_out = std::accumulate(times.begin(), times.end(), 0); | |
Vector<RTYPE> res = no_init(n_out); | |
auto begin = res.begin(); | |
for (std::size_t i = 0; i < n; begin += times[i], ++i) | |
std::fill(begin, begin + times[i], x[i]); | |
return res; | |
} else if (ntimes == 1) { | |
std::size_t n_out = n * times[0]; | |
Vector<RTYPE> res = no_init(n_out); | |
auto start = x.begin(), end = x.end(); | |
auto begin = res.begin(); | |
for (std::size_t i = 0; i < times[0]; begin += n, ++i) | |
std::copy(start, end, begin); | |
return res; | |
} else { | |
stop("Invalid 'times' value"); | |
} | |
return R_NilValue; | |
} | |
// [[Rcpp::export]] | |
SEXP rep_int(SEXP x, IntegerVector times) { | |
RCPP_RETURN_VECTOR(rep_int_impl, x, times); | |
} | |
/*** R | |
library(benchr) | |
identical2 <- function(...) { length(unique(list(...))) == 1L } | |
x <- 1:1000 | |
n <- 1000 | |
reps <- rep(length(x), n) | |
identical2(rep_each(x, n), rep(x, each = n), rep_int(x, reps), rep.int(x, reps)) | |
benchmark(rep_each(x, n), rep(x, each = n), rep_int(x, reps), rep.int(x, reps)) | |
identical2(rep_times(x, n), rep(x, times = n), rep_int(x, n), rep.int(x, n)) | |
benchmark(rep_times(x, n), rep(x, times = n), rep_int(x, n), rep.int(x, n)) | |
*/ |
Author
artemklevtsov
commented
Aug 6, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment