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

*/
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to