Skip to content

Commit

Permalink
More descriptive variable names for fcase counting variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Sep 2, 2024
1 parent 02ced80 commit 84e0932
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/fifelse.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
"Note that the default argument must be named explicitly, e.g., default=0"), narg - 2);
}
int nprotect=0, l;
int64_t len0=0, len1=0, len2=0;
int64_t n_ans=0, n_this_arg=0, n_undecided=0;
SEXP ans=R_NilValue, value0=R_NilValue, tracker=R_NilValue, whens=R_NilValue, thens=R_NilValue;
PROTECT_INDEX Iwhens, Ithens;
PROTECT_WITH_INDEX(whens, &Iwhens); nprotect++;
Expand All @@ -232,22 +232,22 @@ SEXP fcaseR(SEXP rho, SEXP args) {
const int *restrict pwhens = LOGICAL(whens);
l = 0;
if (i == 0) {
len0 = xlength(whens);
len2 = len0;
n_ans = xlength(whens);
n_undecided = n_ans;
type0 = TYPEOF(thens);
value0 = thens;
ans = PROTECT(allocVector(type0, len0)); nprotect++;
ans = PROTECT(allocVector(type0, n_ans)); nprotect++;
copyMostAttrib(thens, ans);
tracker = PROTECT(allocVector(INTSXP, len0)); nprotect++;
tracker = PROTECT(allocVector(INTSXP, n_ans)); nprotect++;
p = INTEGER(tracker);
} else {
imask = false;
naout = xlength(thens) == 1 && TYPEOF(thens) == LGLSXP && LOGICAL(thens)[0]==NA_LOGICAL;
if (xlength(whens) != len0) {
if (xlength(whens) != n_ans) {
// no need to check `idefault` here because the con for default is always `TRUE`
error(_("Argument #%d has length %lld which differs from that of argument #1 (%lld). "
"Please make sure all logical conditions have the same length."),
i*2+1, (long long)xlength(whens), (long long)len0);
i*2+1, (long long)xlength(whens), (long long)n_ans);
}
if (!naout && TYPEOF(thens) != type0) {
if (idefault) {
Expand Down Expand Up @@ -282,22 +282,22 @@ SEXP fcaseR(SEXP rho, SEXP args) {
UNPROTECT(2); // levels(value0), levels(thens)
}
}
len1 = xlength(thens);
if (len1 != len0 && len1 != 1) {
n_this_arg = xlength(thens);
if (n_this_arg != n_ans && n_this_arg != 1) {
if (idefault) {
error(_("Length of 'default' must be 1 or %lld."), (long long)len0);
error(_("Length of 'default' must be 1 or %lld."), (long long)n_ans);
} else {
error(_("Length of output value #%d (%lld) must either be 1 or match the length of the logical condition (%lld)."), i*2+2, (long long)len1, (long long)len0);
error(_("Length of output value #%d (%lld) must either be 1 or match the length of the logical condition (%lld)."), i*2+2, (long long)n_this_arg, (long long)n_ans);
}
}
int64_t thenMask = len1>1 ? INT64_MAX : 0;
int64_t thenMask = n_this_arg>1 ? INT64_MAX : 0;
switch(TYPEOF(ans)) {
case LGLSXP: {
const int *restrict pthens;
if (!naout) pthens = LOGICAL(thens); // the content is not useful if out is NA_LOGICAL scalar
int *restrict pans = LOGICAL(ans);
const int pna = NA_LOGICAL;
for (int64_t j=0; j<len2; ++j) {
for (int64_t j=0; j<n_undecided; ++j) {
const int64_t idx = imask ? j : p[j];
if (pwhens[idx]==1) {
pans[idx] = naout ? pna : pthens[idx & thenMask];
Expand All @@ -314,7 +314,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
if (!naout) pthens = INTEGER(thens); // the content is not useful if out is NA_LOGICAL scalar
int *restrict pans = INTEGER(ans);
const int pna = NA_INTEGER;
for (int64_t j=0; j<len2; ++j) {
for (int64_t j=0; j<n_undecided; ++j) {
const int64_t idx = imask ? j : p[j];
if (pwhens[idx]==1) {
pans[idx] = naout ? pna : pthens[idx & thenMask];
Expand All @@ -332,7 +332,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
double *restrict pans = REAL(ans);
const double na_double = INHERITS(ans, char_integer64) ? NA_INT64_D : NA_REAL;
const double pna = na_double;
for (int64_t j=0; j<len2; ++j) {
for (int64_t j=0; j<n_undecided; ++j) {
const int64_t idx = imask ? j : p[j];
if (pwhens[idx]==1) {
pans[idx] = naout ? pna : pthens[idx & thenMask];
Expand All @@ -349,7 +349,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
if (!naout) pthens = COMPLEX(thens); // the content is not useful if out is NA_LOGICAL scalar
Rcomplex *restrict pans = COMPLEX(ans);
const Rcomplex pna = NA_CPLX;
for (int64_t j=0; j<len2; ++j) {
for (int64_t j=0; j<n_undecided; ++j) {
const int64_t idx = imask ? j : p[j];
if (pwhens[idx]==1) {
pans[idx] = naout ? pna : pthens[idx & thenMask];
Expand All @@ -365,7 +365,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
const SEXP *restrict pthens;
if (!naout) pthens = STRING_PTR_RO(thens); // the content is not useful if out is NA_LOGICAL scalar
const SEXP pna = NA_STRING;
for (int64_t j=0; j<len2; ++j) {
for (int64_t j=0; j<n_undecided; ++j) {
const int64_t idx = imask ? j : p[j];
if (pwhens[idx]==1) {
SET_STRING_ELT(ans, idx, naout ? pna : pthens[idx & thenMask]);
Expand All @@ -382,7 +382,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
// assign the NA values as it does for other atomic types
const SEXP *restrict pthens;
if (!naout) pthens = SEXPPTR_RO(thens); // the content is not useful if out is NA_LOGICAL scalar
for (int64_t j=0; j<len2; ++j) {
for (int64_t j=0; j<n_undecided; ++j) {
const int64_t idx = imask ? j : p[j];
if (pwhens[idx]==1) {
if (!naout) SET_VECTOR_ELT(ans, idx, pthens[idx & thenMask]);
Expand All @@ -397,7 +397,7 @@ SEXP fcaseR(SEXP rho, SEXP args) {
if (l==0) {
break; // stop early as nothing left to do
}
len2 = l;
n_undecided = l;
}
UNPROTECT(nprotect); // whens, thens, ans, tracker
return ans;
Expand Down

0 comments on commit 84e0932

Please sign in to comment.