Skip to content
Prev 4706 / 10988 Next

[Rcpp-devel] Dispatching based on SEXPTYPE

Like this?

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