[Rcpp-devel] Dispatching based on SEXPTYPE

Hadley Wickham h.wickham at gmail.com
Tue Nov 20 18:04:32 CET 2012


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

-- 
RStudio / Rice University
http://had.co.nz/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unique.cpp
Type: text/x-c++src
Size: 1108 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121120/4002f77e/attachment.cpp>


More information about the Rcpp-devel mailing list