39 int n = LENGTH(PROTECT(x_ = Rf_coerceVector(x_, SXP_ans)));
40 Rboolean no_force = !Rf_asLogical(force_);
41 if (no_force && n < 3) {
42 UNPROTECT(1);
return R_NilValue;
45 register int ln, i,
c = 0;
46 int n2 = (no_force) ? n / 3 : n;
50 Type_x_ *x = STYP_x_(x_), *val;
52 const char *res_nms[] = {
"lengths",
"values",
""};
55 len = R_Calloc(n2,
int);
56 val = R_Calloc(n2, Type_x_);
60 for(i = 1; i < n; i++) {
67 if (no_force &&
c == n2) {
70 UNPROTECT(1);
return R_NilValue;
80 ans = PROTECT(Rf_mkNamed(VECSXP, res_nms));
81 SET_VECTOR_ELT(ans, 0, Rf_allocVector(INTSXP,
c));
82 SET_VECTOR_ELT(ans, 1, Rf_allocVector(SXP_ans,
c));
84 Memcpy(INTEGER(VECTOR_ELT(ans, 0)), len,
c);
85 Memcpy(STYP_x_(VECTOR_ELT(ans, 1)), val,
c);
87 Rf_setAttrib(ans, R_ClassSymbol, Rf_mkString(
"rle"));
89 if(n > 0) { R_Free(len); R_Free(val); }