[Rcpp-devel] Debugging Rcpp code
Hadley Wickham
h.wickham at gmail.com
Fri Nov 16 16:34:55 CET 2012
> You might like to di something like:
>
> std::vector< std::vector<double> > groups( max(i) ) ;
>
> you'll pay for the traversal of the max, but then you don't need to resize.
I ended up going with:
NumericVector tapply3(NumericVector x, IntegerVector i, Function fun) {
std::map<int, std::vector<double> > groups;
NumericVector::iterator x_it;
IntegerVector::iterator i_it;
for(x_it = x.begin(), i_it = i.begin(); x_it != x.end(); ++x_it, ++i_it) {
groups[*i_it].push_back(*x_it);
}
NumericVector out(groups.size());
std::map<int, std::vector<double> >::const_iterator g_it = groups.begin();
NumericVector::iterator o_it = out.begin();
for(; g_it != groups.end(); ++g_it, ++o_it) {
NumericVector res = fun(g_it->second);
*o_it = res[0];
}
return out;
}
which I think is much easier to understand. It's slightly slower when
i is small and dense, but orders of magnitude faster when i is sparse.
> Calling fun() is going to be costly too (probably what will dominate).
> specially because of our internal::try_catch thing. See in Evaluator.cpp.
> that's a mess.
>
> We should have something for faster evaluations, so that we would make the
> call and just use Rf_eval.
That'll be nice - but here I'm more interested in showing off how to
use the STL, not in highly performant code (although this special case
is ~4x than tapply), so it's not a big deal.
> It will become more fun when we "apply" c++ functions. ^^
Definitely!
Hadley
--
RStudio / Rice University
http://had.co.nz/
More information about the Rcpp-devel
mailing list