-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The iterators needed to support copy assignment, and I also implemented part of a RandomAccessIterator so upper_bound was more efficient
- Loading branch information
Showing
10 changed files
with
249 additions
and
51 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <algorithm> | ||
#include "cpp11.hpp" | ||
using namespace cpp11; | ||
|
||
#include <Rcpp.h> | ||
#include <algorithm> | ||
using namespace Rcpp; | ||
|
||
[[cpp11::export]] SEXP remove_altrep(SEXP x) { | ||
SEXP out = PROTECT(Rf_allocVector(REALSXP, Rf_xlength(x))); | ||
memcpy(REAL(out), REAL(x), Rf_xlength(x)); | ||
|
||
UNPROTECT(1); | ||
return out; | ||
} | ||
|
||
[[cpp11::export]] double upper_bound(doubles x, doubles breaks) { | ||
auto pos = std::upper_bound(breaks.begin(), breaks.end(), x[0]); | ||
return std::distance(breaks.begin(), pos); | ||
} | ||
|
||
[[cpp11::export]] integers findInterval2(doubles x, doubles breaks) { | ||
writable::integers out(x.size()); | ||
auto out_it = out.begin(); | ||
|
||
for (auto&& val : x) { | ||
auto pos = std::upper_bound(breaks.begin(), breaks.end(), val); | ||
*out_it = std::distance(breaks.begin(), pos); | ||
++out_it; | ||
} | ||
return out; | ||
} | ||
[[cpp11::export]] integers findInterval2_5(doubles x, doubles breaks) { | ||
writable::integers out(x.size()); | ||
auto out_it = out.begin(); | ||
auto bb = breaks.begin(); | ||
auto be = breaks.end(); | ||
|
||
for (auto&& val : x) { | ||
auto pos = std::upper_bound(bb, be, val); | ||
*out_it = std::distance(bb, pos); | ||
++out_it; | ||
} | ||
return out; | ||
} | ||
|
||
// This version avoids the overhead of the cpp11 iterator types | ||
[[cpp11::export]] integers findInterval3(doubles x, doubles breaks) { | ||
writable::integers out(x.size()); | ||
auto out_it = out.begin(); | ||
auto b = REAL(breaks); | ||
auto e = REAL(breaks) + Rf_xlength(breaks); | ||
double* pos; | ||
|
||
for (auto&& val : x) { | ||
pos = std::upper_bound(b, e, val); | ||
*out_it = std::distance(b, pos); | ||
++out_it; | ||
} | ||
return out; | ||
} | ||
|
||
[[cpp11::export]] IntegerVector findInterval4(NumericVector x, NumericVector breaks) { | ||
IntegerVector out(x.size()); | ||
|
||
NumericVector::iterator it, it_end, pos, b, e; | ||
IntegerVector::iterator out_it; | ||
it_end = x.end(); | ||
b = breaks.begin(); | ||
e = breaks.end(); | ||
|
||
for (it = x.begin(), out_it = out.begin(); it != it_end; ++it, ++out_it) { | ||
pos = std::upper_bound(b, e, *it); | ||
*out_it = std::distance(b, pos); | ||
} | ||
|
||
return out; | ||
} |
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
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
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
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
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
Oops, something went wrong.