|
3 | 3 | // #include <signal.h> // the debugging machinery + breakpoint aidee |
4 | 4 | // raise(SIGINT); |
5 | 5 |
|
6 | | -SEXP dt_na(SEXP x, SEXP cols) { |
7 | | - int n=0, elem; |
8 | | - |
| 6 | +SEXP dt_na(SEXP x, SEXP cols) |
| 7 | +{ |
9 | 8 | if (!isNewList(x)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "x", "Cdt_na", type2char(TYPEOF(x)), "list"); // # nocov |
10 | 9 | 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)); |
16 | 19 | } |
17 | 20 | SEXP ans = PROTECT(allocVector(LGLSXP, n)); |
18 | 21 | 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); |
22 | 26 | if (!length(v) || isList(v)) continue; // like stats:::na.omit.data.frame, skip pairlist columns |
23 | 27 | if (n != length(v)) |
24 | 28 | 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