Skip to content

Commit

Permalink
undo line splitting so I can see what logic changes are; pending C sa…
Browse files Browse the repository at this point in the history
…me-line-branch codecov #4301 (comment)
  • Loading branch information
mattdowle committed Aug 21, 2021
1 parent 1c7e3d9 commit 7434ed6
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions src/subset.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,17 @@ SEXP convertNegAndZeroIdx(SEXP idx, SEXP maxArg, SEXP allowOverMax, SEXP nomatch
stop = true;
}
}
if (!stop)
return(idx); // most common case to return early: no 0, no negative; all idx either NA (for !nomatch) or in range [1-max]
if (!stop) return(idx); // most common case to return early: no 0, no negative; all idx either NA (for !nomatch) or in range [1-max]

// ---------
// else massage the input to a standard idx where all items are either NA (for !nomatch) or in range [1,max] ...

int countNeg=0, countZero=0, countNA=0, countOverMax=0, firstOverMax=0;
for (int i=0; i<n; i++) {
int elem = idxp[i];
if (elem==NA_INTEGER)
countNA++;
else if (elem<0)
countNeg++;
else if (elem==0)
countZero++;
if (elem==NA_INTEGER) countNA++;
else if (elem<0) countNeg++;
else if (elem==0) countZero++;
else if (elem>max) {
countOverMax++;
if (firstOverMax==0)
Expand All @@ -190,10 +186,8 @@ SEXP convertNegAndZeroIdx(SEXP idx, SEXP maxArg, SEXP allowOverMax, SEXP nomatch
int i=0, firstNeg=0, firstPos=0;
while (i<n && (firstNeg==0 || firstPos==0)) {
int elem = idxp[i];
if (firstPos==0 && elem>0)
firstPos=i+1;
if (firstNeg==0 && elem<0 && elem!=NA_INTEGER)
firstNeg=i+1;
if (firstPos==0 && elem>0) firstPos=i+1;
if (firstNeg==0 && elem<0 && elem!=NA_INTEGER) firstNeg=i+1;
i++;
}
error(_("Item %d of i is %d and item %d is %d. Cannot mix positives and negatives."), firstNeg, idxp[firstNeg-1], firstPos, idxp[firstPos-1]);
Expand All @@ -202,10 +196,8 @@ SEXP convertNegAndZeroIdx(SEXP idx, SEXP maxArg, SEXP allowOverMax, SEXP nomatch
int i=0, firstNeg=0, firstNA=0;
while (i<n && (firstNeg==0 || firstNA==0)) {
int elem = idxp[i];
if (firstNeg==0 && elem<0 && elem!=NA_INTEGER)
firstNeg=i+1;
if (firstNA==0 && elem==NA_INTEGER)
firstNA=i+1;
if (firstNeg==0 && elem<0 && elem!=NA_INTEGER) firstNeg=i+1;
if (firstNA==0 && elem==NA_INTEGER) firstNA=i+1;
i++;
}
error(_("Item %d of i is %d and item %d is NA. Cannot mix negatives and NA."), firstNeg, idxp[firstNeg-1], firstNA);
Expand Down Expand Up @@ -236,24 +228,20 @@ SEXP convertNegAndZeroIdx(SEXP idx, SEXP maxArg, SEXP allowOverMax, SEXP nomatch
} else {
// idx is all negative without any NA but perhaps some zeros
bool *keep = (bool *)R_alloc(max, sizeof(bool)); // 4 times less memory that INTSXP in src/main/subscript.c
for (int i=0; i<max; i++)
keep[i] = true;
for (int i=0; i<max; i++) keep[i] = true;
int countRemoved=0, countDup=0, countBeyond=0; // idx=c(-10,-5,-10) removing row 10 twice
int firstBeyond=0, firstDup=0;
for (int i=0; i<n; i++) {
int elem = -idxp[i];
if (elem==0)
continue;
if (elem==0) continue;
if (elem>max) {
countBeyond++;
if (firstBeyond==0)
firstBeyond=i+1;
if (firstBeyond==0) firstBeyond=i+1;
continue;
}
if (!keep[elem-1]) {
countDup++;
if (firstDup==0)
firstDup=i+1;
if (firstDup==0) firstDup=i+1;
} else {
keep[elem-1] = false;
countRemoved++;
Expand All @@ -267,8 +255,7 @@ SEXP convertNegAndZeroIdx(SEXP idx, SEXP maxArg, SEXP allowOverMax, SEXP nomatch
ans = PROTECT(allocVector(INTSXP, ansn));
int *ansp = INTEGER(ans);
for (int i=0, ansi=0; i<max; i++) {
if (keep[i])
ansp[ansi++] = i+1;
if (keep[i]) ansp[ansi++] = i+1;
}
}
UNPROTECT(1);
Expand Down

0 comments on commit 7434ed6

Please sign in to comment.