-
-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rcpp::algorithm proposal #426
Comments
👍 |
So I'll plan to take a look at this and have a simple PR with a couple of algos sometime soon. We can then discuss what all we would like to see implemented like this. |
Sounds good to me. |
Mostly an implementation detail, but since some of the sugar operations ( #include <Rcpp.h>
namespace algorithm {
namespace Functors {
template <typename T>
class Cummax : public std::unary_function<T, T> {
public:
enum { RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype };
private:
bool unset;
bool na_seen;
T current;
public:
Cummax() : unset(true), na_seen(false) {}
inline T operator()(T value) {
if (na_seen) {
return Rcpp::traits::get_na<RTYPE>();
}
if (Rcpp::traits::is_na<RTYPE>(value)) {
na_seen = true;
return Rcpp::traits::get_na<RTYPE>();
}
if (!unset) {
current = (value > current) ? value : current;
return current;
}
unset = false;
current = value;
return current;
}
};
} // Functors
template <typename InputIt, typename OutputIt>
void cummax(InputIt first, InputIt last, OutputIt dest) {
typedef typename std::iterator_traits<InputIt>::value_type type;
std::transform(first, last, dest, Functors::Cummax<type>());
}
} // algorithm
// [[Rcpp::export]]
Rcpp::NumericVector myCummax(Rcpp::NumericVector xx) {
Rcpp::NumericVector x = Rcpp::clone(xx);
algorithm::cummax(xx.begin(), xx.end(), x.begin());
return x;
}
/*** R
set.seed(123); xx <- rpois(7, 25)
all.equal(cummax(xx), myCummax(xx))
#[1] TRUE
xx[5] <- NA
all.equal(cummax(xx), myCummax(xx))
#[1] TRUE
*/ |
As opposed to just naming them things like |
Anyhow, we merged the first version of this. Probably I should write up some documentation. Can anyone give me some direction on this? Otherwise it will just sit around anonymously and be unused. If people actually get some documentation and a chance to use it it may develop a reason to implement more things. |
How can we help? Do you want to know how to write vignettes? How to extend existing vignettes? Were you thinking of help pages? External examples in the Rcpp Gallery? We have done any and all of the above. |
Not sure...what's the best "promotional" place to put it? I think it's a good idea, but without some promo work, it will likely die a horrible death. I'm willing to explain why it's a good thing to use, but I need a place where that is appropriate. Perhaps Rcpp Gallery? |
I agree the Rcpp gallery would be a good place. https://github.com/RcppCore/rcpp-gallery/wiki/Contributing-to-the-Rcpp-Gallery should help you get started. |
A thought about how we could give the end user some more flexibility with respect to sugar (and additionally reimplement sugar with the more flexible functions). This is a proposal for an addition and not for changing any existing functionality. It goes something like this...If we add an include file that looks something like this:
Then we can write functions that look like this:
Which produce output like this:
Currently, most sugar functions only work on stuff that IS a
Rcpp::Vector
. This excludes, for instance,Rcpp::Matrix::Row
. Creating range based algorithms would simply give more flexibility to the end user without removing any of the current functionality.The text was updated successfully, but these errors were encountered: