[Rcpp-devel] Dispatching based on SEXPTYPE

JJ Allaire jj.allaire at gmail.com
Thu Nov 22 13:42:16 CET 2012


Would something like this work? (Romain you probably know best whether this
will actually work out over a large range of scenarios as well as if the
std::string specialization would work)


#include <Rcpp.h>
using namespace Rcpp;

template<int RTYPE>
RObject unique_generic(RObject x) {
    Vector<RTYPE> vector = as<Vector<RTYPE> >(x);
    std::tr1::unordered_set<typename Vector<RTYPE>::stored_type >
                                        set(vector.begin(), vector.end());
    return wrap(set);
}

template<> RObject unique_generic<CHARSXP>(RObject x) {
  // TODO: appropriate specialization for CHARSXP (std::string)
  return x;
}

#define DISPATCH_METHOD(method, x) \
   switch(x.sexp_type()) { \
    case REALSXP: \
      return method<REALSXP>(x); \
    case INTSXP: \
      return method<INTSXP>(x); \
    case CHARSXP: \
      return method<CHARSXP>(x); \
    case LGLSXP: \
      return method<LGLSXP>(x); \
    default: \
      Rf_error("Unsupported type"); \
      return x; \
  }


// [[Rcpp::export]]
RObject unique2(RObject x) {
  DISPATCH_METHOD(unique_generic, x)
}

/*** R

unique2(c(1.0,2.4,3.3,3.3,3.3,3.3,2,2,6))

unique2(c(1,5,5,6,7))

*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121122/29f70286/attachment.html>


More information about the Rcpp-devel mailing list