[Rcpp-devel] Dispatching based on SEXPTYPE

Romain Francois romain at r-enthusiasts.com
Wed Nov 21 10:33:11 CET 2012


Pretty much.

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).

I would advise to return include the call to wrap in your unique1.

Things will be different some day when we can use C++11 move semantics.

Romain

Le 20/11/12 18:04, Hadley Wickham a écrit :
> Hi all,
>
> I've attached a simple implementation of unique for numeric, integer,
> logical, and character vectors.  To do that I have four C++ function,
> along the lines of :
>
> std::tr1::unordered_set<double> unique1(NumericVector x) {
>    return std::tr1::unordered_set<double>(x.begin(), x.end());
> }
>
> and then one exported function:
>
> // [[Rcpp::export]]
> RObject unique2(RObject x) {
>    switch(x.sexp_type()) {
>      case REALSXP:
>        return wrap(unique1(as<NumericVector>(x)));
>        break;
>      case INTSXP:
>        return wrap(unique1(as<IntegerVector>(x)));
>        break;
>      case CHARSXP:
>        return wrap(unique1(as<CharacterVector>(x)));
>        break;
>      case LGLSXP:
>        return wrap(unique1(as<LogicalVector>(x)));
>        break;
>    }
>    Rf_error("Unsupported type");
>
> }
>
> Is this right way to go about it?  (I realise I could probably write a
> template for the individual unique1 methods, but I'd like to keep it
> simple for now).
>
> Thanks!
>
> Hadley


-- 
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