[Rcpp-devel] Dispatching based on SEXPTYPE

Romain Francois romain at r-enthusiasts.com
Wed Nov 21 22:42:46 CET 2012


Le 21/11/12 22:35, Hadley Wickham a écrit :
>>> I would perhaps advise you to have the unique1 overloads not to return the
>>> stl object, as you are generating copies (although some may argue about
>>> compiler optimizations).
>>
>> Ah, ok.  I don't have much intuition for when copies occur in C++.
>>
>>> I would advise to return include the call to wrap in your unique1.
>>
>> You mean just inline them in unique1, and not have the individual
>> unique2 functions?
>
> Like this?

Something like this yes. I was thinking of something that still kept the 
logical split between the dispatcher "unique3" and the workers unique1, 
so essentially have all the unique1 overloads returning SEXP instead of 
stl containers.

something like :

SEXP unique1(NumericVector x) {
   return wrap( std::tr1::unordered_set<double>(x.begin(), x.end()) );
}

> // [[Rcpp::export]]
> RObject unique3(RObject x) {
>    NumericVector y1;
>    IntegerVector y2;
>    LogicalVector y3;
>
>    std::tr1::unordered_set<double> set1;
>    std::tr1::unordered_set<int> set2;
>    std::tr1::unordered_set<bool> set3;
>
>    switch(x.sexp_type()) {
>      case REALSXP:
>        y1 = as<NumericVector>(x);
>        set1.insert(y1.begin(), y1.end());
>        return wrap(set1);
>      case INTSXP:
>        y2 = as<IntegerVector>(x);
>        set2.insert(y2.begin(), y2.end());
>        return wrap(set2);
>      case LGLSXP:
>        y3 = as<LogicalVector>(x);
>        set3.insert(y3.begin(), y3.end());
>        return wrap(set3);
>      default:
>        Rf_error("Unsupported type");
>    }
> }
>
> It doesn't make any difference to the speed, apart from the logical
> case, which I find surprising given that it should have the least
> copying to do.
>
> Hadley

Then I was guessing wrong and the compiler performs optimization.



-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible



More information about the Rcpp-devel mailing list