Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,17 @@ do_patterns = function(pat_sub, all_cols) {

return(matched)
}

any_na_integer = function(x) {
#x = sample(1e8)
#any_na_integer(x)
stopifnot(is.integer(x))
.Call(Cany_na_integerR, x)
}

list_any_na_integer = function(x) {
#x = list(sample(1e8), c(sample(1e8-1), NA), sample(1e8))
#list_any_na_integer(x)
stopifnot(is.list(x), all(sapply(x, is.integer)))
.Call(Clist_any_na_integerR, x)
}
5 changes: 5 additions & 0 deletions src/data.table.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,8 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP inplace, SEXP cols, SEXP verbo

// between.c
SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP bounds);

// utils.c
void any_na_integer(int *x, uint64_t nx, bool *ans, bool parallel);
SEXP any_na_integerR(SEXP x);
SEXP list_any_na_integerR(SEXP x);
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ R_CallMethodDef callMethods[] = {
{"CnafillR", (DL_FUNC) &nafillR, -1},
{"CcolnamesInt", (DL_FUNC) &colnamesInt, -1},
{"CinitLastUpdated", (DL_FUNC) &initLastUpdated, -1},
{"Cany_na_integerR", (DL_FUNC) &any_na_integerR, -1},
{"Clist_any_na_integerR", (DL_FUNC) &list_any_na_integerR, -1},
{NULL, NULL, 0}
};

Expand Down
61 changes: 61 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "data.table.h"
#include <Rdefines.h>

void any_na_integer(int *x, uint64_t nx, bool *ans, bool parallel) {
bool found=false;
#pragma omp parallel for if (parallel) num_threads(getDTthreads())
for (uint64_t i=0; i<nx; i++) {
if (found) continue;
if (x[i]==NA_INTEGER) found = true;
}
ans[0] = found;
}
/* catching first NA index option proposed by Pasha https://github.com/Rdatatable/data.table/pull/3609/files#r288277417
found_index = INTMAX;
#omp for dynamic(1) reduce(min:found_index)
for( i = 1:NBATCHES ) {
if (found) continue;
for (row = i*BATCH_SIZE:(i+1)*BATCH_SIZE) {
if (x[row] is na) {
found = true;
found_index = row;
break;
}
}
}
*/

SEXP any_na_integerR(SEXP x) {
int protecti=0;
int *ix = INTEGER(x);
R_xlen_t nx = xlength(x);
SEXP ans = PROTECT(allocVector(LGLSXP, 1)); protecti++;
bool bans;
any_na_integer(ix, nx, &bans, true);
LOGICAL(ans)[0] = (int) bans;
UNPROTECT(protecti);
return ans;
}

SEXP list_any_na_integerR(SEXP x) {
int protecti=0;
int nx = length(x);
SEXP ans;
ans = PROTECT(allocVector(LGLSXP, nx)); protecti++;
int *ians = LOGICAL(ans);
bool *bans = malloc(sizeof(bool)*nx);
if (!bans) error("%s: Unable to allocate memory answer", __func__); // # nocov
int* ix[nx];
uint64_t inx[nx];
for (R_len_t i=0; i<nx; i++) {
inx[i] = xlength(VECTOR_ELT(x, i));
ix[i] = INTEGER(VECTOR_ELT(x, i));
}
#pragma omp parallel for num_threads(getDTthreads())
for (R_len_t i=0; i<nx; i++) {
any_na_integer(ix[i], inx[i], &bans[i], false);
ians[i] = (int) bans[i];
}
UNPROTECT(protecti);
return ans;
}