Last active
December 15, 2016 03:57
-
-
Save artemklevtsov/07742833c519359835d3e8119263a621 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
| #include <boost/sort/spreadsort/spreadsort.hpp> | |
| using namespace Rcpp; | |
| using namespace boost::sort::spreadsort; | |
| template <int RTYPE> | |
| Vector<RTYPE> sort_impl(const Vector<RTYPE> & x, bool decreasing = false) { | |
| Vector<RTYPE> res = no_init(x.size()); | |
| std::copy(x.begin(), x.end(), res.begin()); | |
| spreadsort(res.begin(), res.end()); | |
| if (decreasing) std::reverse(res.begin(), res.end()); | |
| return res; | |
| } | |
| template <> | |
| Vector<STRSXP> sort_impl(const Vector<STRSXP> & x, bool decreasing) { | |
| std::vector<std::string> res(x.size()); | |
| std::copy(x.begin(), x.end(), res.begin()); | |
| spreadsort(res.begin(), res.end()); | |
| if (decreasing) std::reverse(res.begin(), res.end()); | |
| return wrap(res); | |
| } | |
| // [[Rcpp::export]] | |
| SEXP sort2(SEXP x, bool decreasing = false) { | |
| switch(TYPEOF(x)) { | |
| case INTSXP: | |
| return sort_impl<INTSXP>(x, decreasing); | |
| case REALSXP: | |
| return sort_impl<REALSXP>(x, decreasing); | |
| case STRSXP: | |
| return sort_impl<STRSXP>(x, decreasing); | |
| default: | |
| stop("Unsupported type."); | |
| } | |
| } | |
| template <int RTYPE> | |
| Vector<RTYPE> sort_impl2(const Vector<RTYPE> & x, bool decreasing = false) { | |
| Vector<RTYPE> res = no_init(x.size()); | |
| std::copy(x.begin(), x.end(), res.begin()); | |
| std::sort(res.begin(), res.end()); | |
| if (decreasing) std::reverse(res.begin(), res.end()); | |
| return res; | |
| } | |
| // [[Rcpp::export]] | |
| SEXP sort3(SEXP x, bool decreasing = false) { | |
| switch(TYPEOF(x)) { | |
| case INTSXP: | |
| return sort_impl2<INTSXP>(x, decreasing); | |
| case REALSXP: | |
| return sort_impl2<REALSXP>(x, decreasing); | |
| case STRSXP: | |
| return sort_impl2<STRSXP>(x, decreasing); | |
| default: | |
| stop("Unsupported type."); | |
| } | |
| } | |
| /*** R | |
| n <- 1E6 | |
| int <- sample.int(1000, n, replace = TRUE) | |
| dbl <- runif(n) | |
| chr <- sample(letters, n, replace = TRUE) | |
| library(benchr) | |
| benchmark(sort(int, method = "radix"), | |
| sort(int, method = "shell"), | |
| sort(int, method = "quick"), | |
| sort2(int), | |
| sort3(int)) | |
| benchmark(sort(dbl, method = "radix"), | |
| sort(dbl, method = "shell"), | |
| sort(dbl, method = "quick"), | |
| sort2(dbl), | |
| sort3(dbl)) | |
| benchmark(sort(chr, method = "radix"), | |
| sort(chr, method = "shell"), | |
| sort(chr, method = "quick"), | |
| sort2(chr), | |
| sort3(chr)) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.