Skip to content

Commit d9f3525

Browse files
authored
inline object "elem" + other various improvements (#7234)
1 parent 8039a23 commit d9f3525

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/frank.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
// #include <signal.h> // the debugging machinery + breakpoint aidee
44
// raise(SIGINT);
55

6-
SEXP dt_na(SEXP x, SEXP cols) {
7-
int n=0, elem;
8-
6+
SEXP dt_na(SEXP x, SEXP cols)
7+
{
98
if (!isNewList(x)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "x", "Cdt_na", type2char(TYPEOF(x)), "list"); // # nocov
109
if (!isInteger(cols)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "cols", "Cdt_na", type2char(TYPEOF(cols)), "integer"); // # nocov
11-
for (int i=0; i<LENGTH(cols); ++i) {
12-
elem = INTEGER(cols)[i];
13-
if (elem<1 || elem>LENGTH(x))
14-
error(_("Item %d of 'cols' is %d which is outside 1-based range [1,ncol(x)=%d]"), i+1, elem, LENGTH(x));
15-
if (!n) n = length(VECTOR_ELT(x, elem-1));
10+
11+
int n = 0;
12+
const int numCols = LENGTH(cols);
13+
const int* col_ints = INTEGER_RO(cols);
14+
for (int i = 0; i < numCols; i++) {
15+
const int elem = col_ints[i];
16+
if (elem < 1 || elem > LENGTH(x))
17+
error(_("Item %d of 'cols' is %d which is outside 1-based range [1,ncol(x)=%d]"), i + 1, elem, LENGTH(x));
18+
if (!n) n = length(VECTOR_ELT(x, elem - 1));
1619
}
1720
SEXP ans = PROTECT(allocVector(LGLSXP, n));
1821
int *ians = LOGICAL(ans);
19-
for (int i=0; i<n; ++i) ians[i]=0;
20-
for (int i=0; i<LENGTH(cols); ++i) {
21-
SEXP v = VECTOR_ELT(x, INTEGER(cols)[i]-1);
22+
memset(ians, 0, n * sizeof(int));
23+
24+
for (int i = 0; i < numCols; i++) {
25+
SEXP v = VECTOR_ELT(x, col_ints[i]-1);
2226
if (!length(v) || isList(v)) continue; // like stats:::na.omit.data.frame, skip pairlist columns
2327
if (n != length(v))
2428
error(_("Column %d of input list x is length %d, inconsistent with first column of that item which is length %d."), i+1,length(v),n);

0 commit comments

Comments
 (0)