diff --git a/src/assign.c b/src/assign.c index eee00cc0f..c93f92452 100644 --- a/src/assign.c +++ b/src/assign.c @@ -23,7 +23,8 @@ static void finalizer(SEXP p) return; } -void setselfref(SEXP x) { +void setselfref(SEXP x) +{ if(!INHERITS(x, char_datatable)) return; // #5286 SEXP p; // Store pointer to itself so we can detect if the object has been copied. See @@ -106,7 +107,8 @@ Moved out of ?setkey Details section in 1.12.2 (Mar 2019). Revisit this w.r.t. t \code{identical()} and \code{object.size()}. */ -static int _selfrefok(SEXP x, Rboolean checkNames, Rboolean verbose) { +static int _selfrefok(SEXP x, Rboolean checkNames, Rboolean verbose) +{ SEXP v, p, tag, prot, names; v = getAttrib(x, SelfRefSymbol); if (v==R_NilValue || TYPEOF(v)!=EXTPTRSXP) { @@ -138,10 +140,12 @@ static int _selfrefok(SEXP x, Rboolean checkNames, Rboolean verbose) { return checkNames ? names==tag : x==R_ExternalPtrAddr(prot); } -static Rboolean selfrefok(SEXP x, Rboolean verbose) { // for readability +static Rboolean selfrefok(SEXP x, Rboolean verbose) // for readability +{ return(_selfrefok(x, FALSE, verbose)==1); } -static Rboolean selfrefnamesok(SEXP x, Rboolean verbose) { +static Rboolean selfrefnamesok(SEXP x, Rboolean verbose) +{ return(_selfrefok(x, TRUE, verbose)==1); } @@ -199,7 +203,8 @@ static SEXP shallow(SEXP dt, SEXP cols, R_len_t n) } // Wrapped in a function so the same message is issued for the data.frame case at the R level -void warn_matrix_column(/* 1-indexed */ int i) { +void warn_matrix_column(/* 1-indexed */ int i) +{ warning(_("Some columns are a multi-column type (such as a matrix column), for example column %d. setDT will retain these columns as-is but subsequent operations like grouping and joining may fail. Please consider as.data.table() instead which will create a new column for each embedded column."), i); } @@ -285,7 +290,8 @@ int checkOverAlloc(SEXP x) return ans; } -SEXP alloccolwrapper(SEXP dt, SEXP overAllocArg, SEXP verbose) { +SEXP alloccolwrapper(SEXP dt, SEXP overAllocArg, SEXP verbose) +{ if (!IS_TRUE_OR_FALSE(verbose)) error(_("%s must be TRUE or FALSE"), "verbose"); int overAlloc = checkOverAlloc(overAllocArg); @@ -305,7 +311,8 @@ SEXP alloccolwrapper(SEXP dt, SEXP overAllocArg, SEXP verbose) { return ans; } -SEXP shallowwrapper(SEXP dt, SEXP cols) { +SEXP shallowwrapper(SEXP dt, SEXP cols) +{ // selfref will be FALSE on manually created data.table, e.g., via dput() or structure() if (!selfrefok(dt, FALSE)) { int n = isNull(cols) ? length(dt) : length(cols); @@ -1258,7 +1265,8 @@ SEXP allocNAVector(SEXPTYPE type, R_len_t n) return(v); } -SEXP allocNAVectorLike(SEXP x, R_len_t n) { +SEXP allocNAVectorLike(SEXP x, R_len_t n) +{ // writeNA needs the attribute retained to write NA_INTEGER64, #3723 // TODO: remove allocNAVector above when usage in fastmean.c, fcast.c and fmelt.c can be adjusted; see comments in PR3724 SEXP v = PROTECT(allocVector(TYPEOF(x), n)); @@ -1271,7 +1279,8 @@ SEXP allocNAVectorLike(SEXP x, R_len_t n) { static SEXP *saveds=NULL; static R_len_t *savedtl=NULL, nalloc=0, nsaved=0; -void savetl_init(void) { +void savetl_init(void) +{ if (nsaved || nalloc || saveds || savedtl) { internal_error(__func__, "savetl_init checks failed (%d %d %p %p)", nsaved, nalloc, (void *)saveds, (void *)savedtl); // # nocov } @@ -1313,7 +1322,8 @@ void savetl(SEXP s) nsaved++; } -void savetl_end(void) { +void savetl_end(void) +{ // Can get called if nothing has been saved yet (nsaved==0), or even if _init() hasn't been called yet (pointers NULL). Such // as to clear up before error. Also, it might be that nothing needed to be saved anyway. for (int i=0; i #include -static bool anySpecialStatic(SEXP x) { +static bool anySpecialStatic(SEXP x) +{ // Special refers to special symbols .BY, .I, .N, and .GRP; see special-symbols.Rd // Static because these are like C static arrays which are the same memory for each group; e.g., dogroups // creates .SD for the largest group once up front, overwriting the contents for each group. Their diff --git a/src/fcast.c b/src/fcast.c index 334dfd7e8..92456404b 100644 --- a/src/fcast.c +++ b/src/fcast.c @@ -4,7 +4,8 @@ // raise(SIGINT); // TO DO: margins -SEXP fcast(SEXP lhs, SEXP val, SEXP nrowArg, SEXP ncolArg, SEXP idxArg, SEXP fill, SEXP fill_d, SEXP is_agg, SEXP some_fillArg) { +SEXP fcast(SEXP lhs, SEXP val, SEXP nrowArg, SEXP ncolArg, SEXP idxArg, SEXP fill, SEXP fill_d, SEXP is_agg, SEXP some_fillArg) +{ int nrows=INTEGER(nrowArg)[0], ncols=INTEGER(ncolArg)[0]; int nlhs=length(lhs), nval=length(val), *idx = INTEGER(idxArg); SEXP target; diff --git a/src/fifelse.c b/src/fifelse.c index 2c1055372..5e1536746 100644 --- a/src/fifelse.c +++ b/src/fifelse.c @@ -6,7 +6,8 @@ supplied logical vector based on the condition (test) and values provided for the remaining arguments (yes, no, and na). */ -SEXP fifelseR(SEXP l, SEXP a, SEXP b, SEXP na) { +SEXP fifelseR(SEXP l, SEXP a, SEXP b, SEXP na) +{ if (!isLogical(l)) { error(_("Argument 'test' must be logical.")); } @@ -207,7 +208,8 @@ SEXP fifelseR(SEXP l, SEXP a, SEXP b, SEXP na) { return ans; } -SEXP fcaseR(SEXP rho, SEXP args) { +SEXP fcaseR(SEXP rho, SEXP args) +{ const int narg=length(args); // `default` will take the last two positions if (narg % 2) { error(_("Received %d inputs; please supply an even number of arguments in ..., " diff --git a/src/fmelt.c b/src/fmelt.c index f47feae98..2fb20fdfa 100644 --- a/src/fmelt.c +++ b/src/fmelt.c @@ -4,7 +4,8 @@ // raise(SIGINT); // generate from 1 to n (a simple fun for melt, vecseq is convenient from R due to SEXP inputs) -SEXP seq_int(int n, int start) { +SEXP seq_int(int n, int start) +{ if (n <= 0) return(R_NilValue); SEXP ans = PROTECT(allocVector(INTSXP, n)); int *ians = INTEGER(ans); @@ -14,7 +15,8 @@ SEXP seq_int(int n, int start) { } // very specific "set_diff" for integers -SEXP set_diff(SEXP x, int n) { +SEXP set_diff(SEXP x, int n) +{ if (TYPEOF(x) != INTSXP) error(_("'x' must be an integer")); if (n <= 0) error(_("'n' must be a positive integer")); SEXP table = PROTECT(seq_int(n, 1)); // TODO: using match to 1:n seems odd here, why use match at all @@ -34,8 +36,8 @@ SEXP set_diff(SEXP x, int n) { return(ans); } -SEXP which(SEXP x, Rboolean val) { - +SEXP which(SEXP x, Rboolean val) +{ int j=0, n = length(x); SEXP ans; if (!isLogical(x)) error(_("Argument to 'which' must be logical")); @@ -55,13 +57,15 @@ SEXP which(SEXP x, Rboolean val) { } // whichwrapper for R -SEXP whichwrapper(SEXP x, SEXP val) { +SEXP whichwrapper(SEXP x, SEXP val) +{ // if (LOGICAL(val)[0] == NA_LOGICAL) // error(_("val should be logical TRUE/FALSE")); return which(x, LOGICAL(val)[0]); } -static const char *concat(SEXP vec, SEXP idx) { +static const char *concat(SEXP vec, SEXP idx) +{ if (!isString(vec)) error(_("concat: 'vec' must be a character vector")); if (!isInteger(idx)) error(_("concat: 'idx' must be an integer vector")); @@ -118,7 +122,8 @@ SEXP chmatch_na(SEXP x, SEXP table) } // deal with measure.vars of type VECSXP -SEXP measurelist(SEXP measure, SEXP dtnames) { +SEXP measurelist(SEXP measure, SEXP dtnames) +{ const int n=length(measure); SEXP ans = PROTECT(allocVector(VECSXP, n)); for (int i=0; ilmax = 0; data->totlen = 0; data->nrow = length(VECTOR_ELT(DT, 0)); @@ -449,7 +460,8 @@ static SEXP combineFactorLevels(SEXP factorLevels, SEXP target, int * factorType return ans; } -SEXP input_col_or_NULL(SEXP DT, struct processData* data, SEXP thisvaluecols, int out_col, int in_col) { +SEXP input_col_or_NULL(SEXP DT, struct processData* data, SEXP thisvaluecols, int out_col, int in_col) +{ if (in_col < data->leach[out_col]) { int input_column_num = INTEGER(thisvaluecols)[in_col]; if (input_column_num != NA_INTEGER) { @@ -459,7 +471,8 @@ SEXP input_col_or_NULL(SEXP DT, struct processData* data, SEXP thisvaluecols, in return R_NilValue; } -SEXP getvaluecols(SEXP DT, SEXP dtnames, Rboolean valfactor, Rboolean verbose, struct processData *data) { +SEXP getvaluecols(SEXP DT, SEXP dtnames, Rboolean valfactor, Rboolean verbose, struct processData *data) +{ for (int i=0; ilvalues; ++i) { SEXP thisvaluecols = VECTOR_ELT(data->valuecols, i); if (!data->isidentical[i]) @@ -589,7 +602,8 @@ SEXP getvaluecols(SEXP DT, SEXP dtnames, Rboolean valfactor, Rboolean verbose, s return(ansvals); } -SEXP getvarcols(SEXP DT, SEXP dtnames, Rboolean varfactor, Rboolean verbose, struct processData *data) { +SEXP getvarcols(SEXP DT, SEXP dtnames, Rboolean varfactor, Rboolean verbose, struct processData *data) +{ // reworked in PR#3455 to create character/factor directly for efficiency, and handle duplicates (#1754) // data->nrow * data->lmax == data->totlen int protecti=0; @@ -691,7 +705,8 @@ SEXP getvarcols(SEXP DT, SEXP dtnames, Rboolean varfactor, Rboolean verbose, str return(ansvars); } -SEXP getidcols(SEXP DT, SEXP dtnames, Rboolean verbose, struct processData *data) { +SEXP getidcols(SEXP DT, SEXP dtnames, Rboolean verbose, struct processData *data) +{ SEXP ansids = PROTECT(allocVector(VECSXP, data->lids)); for (int i=0; ilids; ++i) { int counter = 0; @@ -783,7 +798,8 @@ SEXP getidcols(SEXP DT, SEXP dtnames, Rboolean verbose, struct processData *data return (ansids); } -SEXP fmelt(SEXP DT, SEXP id, SEXP measure, SEXP varfactor, SEXP valfactor, SEXP varnames, SEXP valnames, SEXP narmArg, SEXP verboseArg) { +SEXP fmelt(SEXP DT, SEXP id, SEXP measure, SEXP varfactor, SEXP valfactor, SEXP varnames, SEXP valnames, SEXP narmArg, SEXP verboseArg) +{ SEXP dtnames, ansvals, ansvars, ansids, ansnames, ans; Rboolean narm=FALSE, verbose=FALSE; diff --git a/src/forder.c b/src/forder.c index a4564497b..b31f669a9 100644 --- a/src/forder.c +++ b/src/forder.c @@ -70,14 +70,16 @@ static char msg[1001]; * Therefore, using <> approach to cleanup() on error. */ -static void free_ustr(void) { +static void free_ustr(void) +{ for(int i=0; i0 || attr(idx, "anyinfnan")>0 -bool idxAnyNF(SEXP idx) { +bool idxAnyNF(SEXP idx) +{ return INTEGER(getAttrib(idx, sym_anyna))[0]>0 || INTEGER(getAttrib(idx, sym_anyinfnan))[0]>0; } // forder, re-use existing key or index if possible, otherwise call forder -SEXP forderReuseSorting(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsArg, SEXP ascArg, SEXP naArg, SEXP reuseSortingArg) { +SEXP forderReuseSorting(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsArg, SEXP ascArg, SEXP naArg, SEXP reuseSortingArg) +{ const bool verbose = GetVerbose(); int protecti = 0; double tic=0.0; diff --git a/src/frank.c b/src/frank.c index 997ec79e4..aac5497f4 100644 --- a/src/frank.c +++ b/src/frank.c @@ -109,7 +109,8 @@ SEXP dt_na(SEXP x, SEXP cols) return(ans); } -SEXP frank(SEXP xorderArg, SEXP xstartArg, SEXP xlenArg, SEXP ties_method) { +SEXP frank(SEXP xorderArg, SEXP xstartArg, SEXP xlenArg, SEXP ties_method) +{ const int *xstart = INTEGER(xstartArg), *xlen = INTEGER(xlenArg), *xorder = INTEGER(xorderArg); enum {MEAN, MAX, MIN, DENSE, SEQUENCE, LAST} ties=0; // RUNLENGTH @@ -186,7 +187,8 @@ SEXP frank(SEXP xorderArg, SEXP xstartArg, SEXP xlenArg, SEXP ties_method) { } // internal version of anyNA for data.tables -SEXP anyNA(SEXP x, SEXP cols) { +SEXP anyNA(SEXP x, SEXP cols) +{ int n=0; if (!isNewList(x)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "x", "CanyNA", type2char(TYPEOF(x)), "list"); // #nocov if (!isInteger(cols)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "cols", "CanyNA", type2char(TYPEOF(cols)), "integer"); // # nocov diff --git a/src/fread.c b/src/fread.c index 4b99f1148..9e4352eb7 100644 --- a/src/fread.c +++ b/src/fread.c @@ -238,7 +238,8 @@ static const char* strlim(const char *ch, char buf[static 500], size_t limit) static const char *typeLetter = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; -static char *typesAsString(int ncol) { +static char *typesAsString(int ncol) +{ const int nLetters = strlen(typeLetter); if (NUMTYPE > nLetters) INTERNAL_STOP("NUMTYPE(%d) > nLetters(%d)", NUMTYPE, nLetters); // # nocov static char str[101]; diff --git a/src/froll.c b/src/froll.c index 5b9b5b6a3..8d41e6b17 100644 --- a/src/froll.c +++ b/src/froll.c @@ -19,7 +19,8 @@ * algo = 1: exact * recalculate whole fun for each observation, for mean roundoff correction is adjusted */ -void frollfun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasnf, bool verbose, bool par) { +void frollfun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasnf, bool verbose, bool par) +{ double tic = 0; if (verbose) tic = omp_get_wtime(); @@ -166,7 +167,8 @@ void frollfun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, a * rollmean implemented as single pass sliding window for align="right" * if non-finite detected re-run rollmean implemented as single pass sliding window with NA support */ -void frollmeanFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollmeanFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmeanFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -229,7 +231,8 @@ void frollmeanFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, * rollmean implemented as mean of k obs for each observation for align="right" * if non-finite detected and na.rm=TRUE then re-run NF aware rollmean */ -void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmeanExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -356,7 +359,8 @@ void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill /* fast rolling sum - fast * same as mean fast */ -void frollsumFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollsumFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollsumFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -417,7 +421,8 @@ void frollsumFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, /* fast rolling sum - exact * same as mean exact */ -void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollsumExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -492,7 +497,8 @@ void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, } } -static inline void wmax(const double * restrict x, uint64_t o, int k, double * restrict w, uint64_t *iw, bool narm) { +static inline void wmax(const double * restrict x, uint64_t o, int k, double * restrict w, uint64_t *iw, bool narm) +{ if (narm) { for (int i=0; imessage[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmaxFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -629,7 +636,8 @@ void frollmaxFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, * otherwise we scan for NaN/NA and run either of two loops * has.nf=FALSE can give incorrect results if NAs provided, documented to be used with care */ -void frollmaxExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollmaxExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmaxExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -704,7 +712,8 @@ void frollmaxExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, } } -static inline void wmin(const double * restrict x, uint64_t o, int k, double * restrict w, uint64_t *iw, bool narm) { +static inline void wmin(const double * restrict x, uint64_t o, int k, double * restrict w, uint64_t *iw, bool narm) +{ if (narm) { for (int i=0; imessage[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollminFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -833,7 +843,8 @@ void frollminFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, /* fast rolling min - exact * see rolling max exact details */ -void frollminExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollminExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollminExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -979,7 +990,8 @@ void frollminExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, /* fast rolling prod - fast * same as mean fast */ -void frollprodFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollprodFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollprodFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -1065,7 +1077,8 @@ void frollprodFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, /* fast rolling prod - exact * same as mean exact */ -void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollprodExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -1146,7 +1159,8 @@ void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill no support for NFs, redirecting to exact Welford wmean and m2 would have to be recalculated on each NF element */ -void frollvarFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollvarFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollvarFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0 || k == 1) { // var(scalar) is also NA @@ -1212,7 +1226,8 @@ void frollvarFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, /* fast rolling var - exact */ -void frollvarExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollvarExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollvarExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0 || k == 1) { // var(scalar) is also NA @@ -1317,7 +1332,8 @@ void frollvarExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, /* fast rolling sd - fast */ -void frollsdFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollsdFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: calling sqrt(frollvarFast(...))\n"), "frollsdFast"); frollvarFast(x, nx, ans, k, fill, narm, hasnf, verbose); @@ -1328,7 +1344,8 @@ void frollsdFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, b /* fast rolling sd - exact */ -void frollsdExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) { +void frollsdExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: calling sqrt(frollvarExact(...))\n"), "frollsdExact"); frollvarExact(x, nx, ans, k, fill, narm, hasnf, verbose); @@ -1347,7 +1364,8 @@ void frollsdExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, #endif #define MIN(a,b) (((a)<(b))?(a):(b)) // initialize prev and next pointers for list L -static void setlinks(const int *o, int *next, int *prev, int tail) { +static void setlinks(const int *o, int *next, int *prev, int tail) +{ int p = tail; int k = tail; int q; @@ -1361,14 +1379,16 @@ static void setlinks(const int *o, int *next, int *prev, int tail) { prev[tail] = p; } // Return the first large element in block, or Inf if all elements are small -inline static double peek(const double *x, int m, int tail) { +inline static double peek(const double *x, int m, int tail) +{ return m == tail ? R_PosInf : x[m]; } #define PEEK(j) peek(&x[(j)*k], m[(j)], tail) // return median, for even k, from x_A[m_a], x_A[n_A], x_B[m_B], x_B[n_B] - having two sets of sorted 2 elements, take two smallest and give their mean #define MED(A, B) MIN(PEEK(A), PEEK(B)) // delete all elements from B in reverse order -static void unwind(int *prev, int *next, int *m, int *s, int k, int tail) { +static void unwind(int *prev, int *next, int *m, int *s, int k, int tail) +{ for (int i=k-1; i>=0; i--) { next[prev[i]] = next[i]; prev[next[i]] = prev[i]; @@ -1378,12 +1398,14 @@ static void unwind(int *prev, int *next, int *m, int *s, int k, int tail) { } #define UNWIND(j) unwind(&prev[(j)*(k+1)], &next[(j)*(k+1)], &m[(j)], &s[(j)], k, tail); // checks if object i is "small" - smaller than value(s) used for median answer -inline static bool small(int i, const double *x, int m, int tail) { +inline static bool small(int i, const double *x, int m, int tail) +{ return (m == tail) || (x[i] < x[m]) || ((x[i] == x[m]) && (i < m)); } #define SMALL(i) small((i), x, m[0], tail) // delete element from A -static void delete(int i, const double *x, int *prev, int *next, int *m, int *s, int tail) { +static void delete(int i, const double *x, int *prev, int *next, int *m, int *s, int tail) +{ next[prev[i]] = next[i]; prev[next[i]] = prev[i]; if (SMALL(i)) { @@ -1400,7 +1422,8 @@ static void delete(int i, const double *x, int *prev, int *next, int *m, int *s, } #define DELETE(j) delete(i, &x[(j)*k], &prev[(j)*(k+1)], &next[(j)*(k+1)], &m[(j)], &s[(j)], tail) // undelete element from B -static void undelete(int i, const double *x, int *prev, int *next, int *m, int tail) { +static void undelete(int i, const double *x, int *prev, int *next, int *m, int tail) +{ next[prev[i]] = i; prev[next[i]] = i; if (SMALL(i)) { @@ -1409,14 +1432,16 @@ static void undelete(int i, const double *x, int *prev, int *next, int *m, int t } #define UNDELETE(j) undelete(i, &x[(j)*k], &prev[(j)*(k+1)], &next[(j)*(k+1)], &m[(j)], tail) // advance - balance block after delete/undelete, fixed m (and n) pointers and s counter -static void advance(int *next, int* m, int *s) { +static void advance(int *next, int* m, int *s) +{ m[0] = next[m[0]]; s[0]++; } #define ADVANCE(j) advance(&next[(j)*(k+1)], &m[(j)], &s[(j)]) // same helper functions supporting any k #define PEEK2(j) peek(&x[(j)*k], n[(j)], tail) -static double med2(const double *xa, const double *xb, int ma, int na, int mb, int nb, int tail) { +static double med2(const double *xa, const double *xb, int ma, int na, int mb, int nb, int tail) +{ double xam = ma == tail ? R_PosInf : xa[ma]; double xan = na == tail ? R_PosInf : xa[na]; double xbm = mb == tail ? R_PosInf : xb[mb]; @@ -1438,7 +1463,8 @@ static double med2(const double *xa, const double *xb, int ma, int na, int mb, i } } #define MED2(A, B) med2(&x[(A)*k], &x[(B)*k], m[(A)], n[(A)], m[(B)], n[(B)], tail) -static void unwind2(int *prev, int *next, int *m, int *n, int *s, int k, int tail, bool even) { +static void unwind2(int *prev, int *next, int *m, int *n, int *s, int k, int tail, bool even) +{ for (int i=k-1; i>=0; i--) { next[prev[i]] = next[i]; prev[next[i]] = prev[i]; @@ -1450,7 +1476,8 @@ static void unwind2(int *prev, int *next, int *m, int *n, int *s, int k, int tai } #define UNWIND2(j) unwind2(&prev[(j)*(k+1)], &next[(j)*(k+1)], &m[(j)], &n[(j)], &s[(j)], k, tail, even); #define SMALL2(i) small((i), x, n[0], tail) -static void delete2(int i, const double *x, int *prev, int *next, int *m, int *n, int *s, int tail, bool even) { +static void delete2(int i, const double *x, int *prev, int *next, int *m, int *n, int *s, int tail, bool even) +{ next[prev[i]] = next[i]; prev[next[i]] = prev[i]; if (SMALL(i)) { @@ -1472,7 +1499,8 @@ static void delete2(int i, const double *x, int *prev, int *next, int *m, int *n } } #define DELETE2(j) delete2(i, &x[(j)*k], &prev[(j)*(k+1)], &next[(j)*(k+1)], &m[(j)], &n[(j)], &s[(j)], tail, even) -static void undelete2(int i, const double *x, int *prev, int *next, int *m, int *n, int tail, bool even) { +static void undelete2(int i, const double *x, int *prev, int *next, int *m, int *n, int tail, bool even) +{ next[prev[i]] = i; prev[next[i]] = i; if (SMALL(i)) { @@ -1484,7 +1512,8 @@ static void undelete2(int i, const double *x, int *prev, int *next, int *m, int } } #define UNDELETE2(j) undelete2(i, &x[(j)*k], &prev[(j)*(k+1)], &next[(j)*(k+1)], &m[(j)], &n[(j)], tail, even) -static void advance2(int *next, int* m, int* n, int *s, bool even) { +static void advance2(int *next, int* m, int* n, int *s, bool even) +{ m[0] = next[m[0]]; if (even) n[0] = next[n[0]]; @@ -1498,7 +1527,8 @@ static void advance2(int *next, int* m, int* n, int *s, bool even) { * finding order of blocks of input is made in parallel * NA handling redirected to algo="exact" */ -void frollmedianFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose, bool par) { +void frollmedianFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose, bool par) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmedianFast", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { @@ -1728,7 +1758,8 @@ void frollmedianFast(const double *x, uint64_t nx, ans_t *ans, int k, double fil * isna must be initialized to false * nc doesn't have to be initialized */ -static int frollNAFast(const double *x, uint64_t nx, int k, int *nc, bool *isna) { +static int frollNAFast(const double *x, uint64_t nx, int k, int *nc, bool *isna) +{ int w = 0; // rolling nc for (int i=0; imessage[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmedianExact", (uint64_t)nx, k, hasnf, (int)narm); if (k == 0) { diff --git a/src/frollR.c b/src/frollR.c index 71963e6f8..769138322 100644 --- a/src/frollR.c +++ b/src/frollR.c @@ -2,7 +2,8 @@ #include // validate and coerce to list of real -SEXP coerceX(SEXP obj) { +SEXP coerceX(SEXP obj) +{ // accept atomic/list of integer/logical/real returns list of real int protecti = 0; if (isVectorAtomic(obj)) { @@ -23,7 +24,8 @@ SEXP coerceX(SEXP obj) { return x; } // validate and coerce to integer or list of integer -SEXP coerceK(SEXP obj, bool adaptive) { +SEXP coerceK(SEXP obj, bool adaptive) +{ int protecti = 0; SEXP ans = R_NilValue; if (!adaptive) { @@ -78,7 +80,8 @@ SEXP coerceK(SEXP obj, bool adaptive) { return ans; } -SEXP frollfunR(SEXP fun, SEXP xobj, SEXP kobj, SEXP fill, SEXP algo, SEXP align, SEXP narm, SEXP hasnf, SEXP adaptive) { +SEXP frollfunR(SEXP fun, SEXP xobj, SEXP kobj, SEXP fill, SEXP algo, SEXP align, SEXP narm, SEXP hasnf, SEXP adaptive) +{ int protecti = 0; const bool verbose = GetVerbose(); @@ -224,7 +227,8 @@ SEXP frollfunR(SEXP fun, SEXP xobj, SEXP kobj, SEXP fill, SEXP algo, SEXP align, } // helper called from R to generate adaptive window for irregularly spaced time series -SEXP frolladapt(SEXP xobj, SEXP kobj, SEXP partial) { +SEXP frolladapt(SEXP xobj, SEXP kobj, SEXP partial) +{ bool p = LOGICAL(partial)[0]; int n = INTEGER(kobj)[0]; diff --git a/src/frolladaptive.c b/src/frolladaptive.c index 41faf1f30..2779a1d6d 100644 --- a/src/frolladaptive.c +++ b/src/frolladaptive.c @@ -7,7 +7,8 @@ * algo = 1: exact * recalculate whole fun for each observation, for mean roundoff correction is adjusted */ -void frolladaptivefun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivefun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ double tic = 0; if (verbose) tic = omp_get_wtime(); @@ -113,7 +114,8 @@ void frolladaptivefun(rollfun_t rfun, unsigned int algo, const double *x, uint64 * adaptive rollmean implemented as cumsum first pass, then diff cumsum by indexes `i` to `i-k[i]` * if NFs detected re-run rollmean implemented as cumsum with NF support */ -void frolladaptivemeanFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivemeanFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivemeanFast", (uint64_t)nx, hasnf, (int) narm); bool truehasnf = hasnf>0; // flag to re-run if NAs detected @@ -211,7 +213,8 @@ void frolladaptivemeanFast(const double *x, uint64_t nx, ans_t *ans, const int * * requires much more cpu * uses multiple cores */ -void frolladaptivemeanExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivemeanExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivemeanExact", (uint64_t)nx, hasnf, (int) narm); bool truehasnf = hasnf>0; // flag to re-run if NAs detected @@ -336,7 +339,8 @@ void frolladaptivemeanExact(const double *x, uint64_t nx, ans_t *ans, const int /* fast rolling adaptive sum - fast * same as adaptive mean fast */ -void frolladaptivesumFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivesumFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivesumFast", (uint64_t)nx, hasnf, (int) narm); bool truehasnf = hasnf>0; @@ -432,7 +436,8 @@ void frolladaptivesumFast(const double *x, uint64_t nx, ans_t *ans, const int *k /* fast rolling adaptive sum - exact * same as adaptive mean exact */ -void frolladaptivesumExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivesumExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivesumExact", (uint64_t)nx, hasnf, (int) narm); bool truehasnf = hasnf>0; @@ -507,7 +512,8 @@ void frolladaptivesumExact(const double *x, uint64_t nx, ans_t *ans, const int * /* fast rolling adaptive max - exact * for has.nf=FALSE it will not detect if any NAs were in the input, therefore could produce incorrect result, well documented */ -void frolladaptivemaxExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivemaxExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivemaxExact", (uint64_t)nx, hasnf, (int) narm); if (narm || hasnf==-1) { // fastest we can get for adaptive max as there is no algo='fast', therefore we drop any NA checks when has.nf=FALSE @@ -582,7 +588,8 @@ void frolladaptivemaxExact(const double *x, uint64_t nx, ans_t *ans, const int * /* fast rolling adaptive min - exact * for has.nf=FALSE it will not detect if any NAs were in the input, therefore could produce incorrect result, well documented */ -void frolladaptiveminExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptiveminExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptiveminExact", (uint64_t)nx, hasnf, (int) narm); if (narm || hasnf==-1) { // fastest we can get for adaptive max as there is no algo='fast', therefore we drop any NA checks when has.nf=FALSE @@ -668,7 +675,8 @@ void frolladaptiveminExact(const double *x, uint64_t nx, ans_t *ans, const int * /* fast rolling adaptive prod - exact * same as adaptive mean exact */ -void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptiveprodExact", (uint64_t)nx, hasnf, (int) narm); bool truehasnf = hasnf>0; @@ -742,7 +750,8 @@ void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int /* fast rolling adaptive var - exact */ -void frolladaptivevarExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivevarExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivevarExact", (uint64_t)nx, hasnf, (int) narm); bool truehasnf = hasnf>0; @@ -846,7 +855,8 @@ void frolladaptivevarExact(const double *x, uint64_t nx, ans_t *ans, const int * /* fast rolling adaptive sd - exact */ -void frolladaptivesdExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) { +void frolladaptivesdExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) +{ if (verbose) snprintf(end(ans->message[0]), 500, _("%s: calling sqrt(frolladaptivevarExact(...))\n"), "frolladaptivesdExact"); frolladaptivevarExact(x, nx, ans, k, fill, narm, hasnf, verbose); @@ -865,7 +875,8 @@ void frolladaptivesdExact(const double *x, uint64_t nx, ans_t *ans, const int *k * nc for rolling NA count * isna for NA mask */ -static int frolladaptiveNAFast(const double *x, uint64_t nx, const int *k, int *nc, bool *isna) { +static int frolladaptiveNAFast(const double *x, uint64_t nx, const int *k, int *nc, bool *isna) +{ int cnc = 0; // cumulative na count for (uint64_t i=0; imessage[0]), 500, _("%s: running in parallel for input length %"PRIu64", hasnf %d, narm %d\n"), "frolladaptivemedianExact", (uint64_t)nx, hasnf, (int) narm); int maxk = k[0]; // find largest window size diff --git a/src/frollapply.c b/src/frollapply.c index 1e04059c9..2f399c7e7 100644 --- a/src/frollapply.c +++ b/src/frollapply.c @@ -1,6 +1,7 @@ #include "data.table.h" -static inline void memcpy_sexp(SEXP dest, size_t offset, SEXP src, int count) { +static inline void memcpy_sexp(SEXP dest, size_t offset, SEXP src, int count) +{ switch (TYPEOF(dest)) { case INTSXP: { memcpy(INTEGER(dest), INTEGER_RO(src) + offset, count * sizeof(int)); @@ -30,12 +31,14 @@ static inline void memcpy_sexp(SEXP dest, size_t offset, SEXP src, int count) { * memcpy src data into preallocated window * we don't call memcpyVector from memcpyDT because they are called in tight loop and we don't want to have extra branches inside */ -SEXP memcpyVector(SEXP dest, SEXP src, SEXP offset, SEXP size) { +SEXP memcpyVector(SEXP dest, SEXP src, SEXP offset, SEXP size) +{ memcpy_sexp(dest, INTEGER_RO(offset)[0] - INTEGER_RO(size)[0], src, LENGTH(dest)); return dest; } // # nocov start ## does not seem to be reported to codecov most likely due to running in a fork, I manually debugged that it is being called when running froll.Rraw -SEXP memcpyDT(SEXP dest, SEXP src, SEXP offset, SEXP size) { +SEXP memcpyDT(SEXP dest, SEXP src, SEXP offset, SEXP size) +{ //Rprintf("%d",1); // manual code coverage to confirm it is reached when marking nocov const int ncol = LENGTH(dest); const int nrow = LENGTH(VECTOR_ELT(dest, 0)); @@ -46,7 +49,8 @@ SEXP memcpyDT(SEXP dest, SEXP src, SEXP offset, SEXP size) { } // # nocov end -SEXP memcpyVectoradaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) { +SEXP memcpyVectoradaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) +{ const size_t oi = INTEGER_RO(offset)[0]; const int nrow = INTEGER_RO(size)[oi - 1]; const size_t o = oi - nrow; // oi should always be bigger than nrow because we filter out incomplete window using ansMask @@ -57,7 +61,8 @@ SEXP memcpyVectoradaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) { return dest; } // # nocov start ## does not seem to be reported to codecov most likely due to running in a fork, I manually debugged that it is being called when running froll.Rraw -SEXP memcpyDTadaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) { +SEXP memcpyDTadaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) +{ //Rprintf("%d",2); // manual code coverage to confirm it is reached when marking nocov size_t oi = INTEGER_RO(offset)[0]; const int nrow = INTEGER_RO(size)[oi - 1]; @@ -75,7 +80,8 @@ SEXP memcpyDTadaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) { // # nocov end // needed in adaptive=TRUE -SEXP setgrowable(SEXP x) { +SEXP setgrowable(SEXP x) +{ if (!isNewList(x)) { SET_GROWABLE_BIT(x); SET_TRUELENGTH(x, LENGTH(x)); // important because gc() uses TRUELENGTH to keep counts diff --git a/src/fsort.c b/src/fsort.c index c43d69eee..737d22656 100644 --- a/src/fsort.c +++ b/src/fsort.c @@ -2,7 +2,8 @@ static const int INSERT_THRESH = 200; // TODO: expose via api and test -static void dinsert(double *x, const int n) { // TODO: if and when twiddled, double => ull +static void dinsert(double *x, const int n) // TODO: if and when twiddled, double => ull +{ if (n<2) return; for (int i=1; i0 if the element a goes after the element b // doesn't master if stable or not uint64_t x = qsort_data[*(int *)a]; @@ -96,7 +99,8 @@ int qsort_cmp(const void *a, const void *b) { return (xy); // largest first in a safe branchless way casting long to int } -static size_t shrinkMSB(size_t MSBsize, uint64_t *msbCounts, int *order, Rboolean verbose) { +static size_t shrinkMSB(size_t MSBsize, uint64_t *msbCounts, int *order, Rboolean verbose) +{ size_t oldMSBsize = MSBsize; while (MSBsize>0 && msbCounts[order[MSBsize-1]] < 2) MSBsize--; @@ -110,7 +114,8 @@ static size_t shrinkMSB(size_t MSBsize, uint64_t *msbCounts, int *order, Rboolea OpenMP is used here to find the range and distribution of data for efficient grouping and sorting. */ -SEXP fsort(SEXP x, SEXP verboseArg) { +SEXP fsort(SEXP x, SEXP verboseArg) +{ double t[10]; t[0] = wallclock(); if (!IS_TRUE_OR_FALSE(verboseArg)) diff --git a/src/fwrite.c b/src/fwrite.c index 4ac49a979..696ab0355 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -355,7 +355,8 @@ static inline void write_time(int32_t x, char **pch) *pch = ch; } -void writeITime(const void *col, int64_t row, char **pch) { +void writeITime(const void *col, int64_t row, char **pch) +{ write_time(((const int32_t*)col)[row], pch); } @@ -413,7 +414,8 @@ void writeDateInt32(const void *col, int64_t row, char **pch) { write_date(((const int32_t*)col)[row], pch); } -void writeDateFloat64(const void *col, int64_t row, char **pch) { +void writeDateFloat64(const void *col, int64_t row, char **pch) +{ double x = ((const double*)col)[row]; write_date(isfinite(x) ? (int)(x) : INT32_MIN, pch); } @@ -576,7 +578,8 @@ void writeCategString(const void *col, int64_t row, char **pch) } #ifndef NOZLIB -int init_stream(z_stream *stream) { +int init_stream(z_stream *stream) +{ stream->next_in = Z_NULL; stream->zalloc = Z_NULL; stream->zfree = Z_NULL; diff --git a/src/fwrite.h b/src/fwrite.h index bb10fb3ce..7a18c5c86 100644 --- a/src/fwrite.h +++ b/src/fwrite.h @@ -70,8 +70,7 @@ static const int writerMaxLen[] = { // same order as fun[] and WFs above; max f 0, //&writeList }; -typedef struct fwriteMainArgs -{ +typedef struct fwriteMainArgs { // Name of the file to open (a \0-terminated C string). If the file name // contains non-ASCII characters, it should be UTF-8 encoded (however fread // will not validate the encoding). diff --git a/src/fwriteR.c b/src/fwriteR.c index 9cf870a35..400c1208c 100644 --- a/src/fwriteR.c +++ b/src/fwriteR.c @@ -19,16 +19,19 @@ static const char *sep2start, *sep2end; // if there are no list columns, set sep2=='\0' // Non-agnostic helpers ... -const char *getString(const SEXP *col, int64_t row) { // TODO: inline for use in fwrite.c +const char *getString(const SEXP *col, int64_t row) // TODO: inline for use in fwrite.c +{ SEXP x = col[row]; return x == NA_STRING ? NULL : ENCODED_CHAR(x); } -int getStringLen(SEXP *col, int64_t row) { +int getStringLen(SEXP *col, int64_t row) +{ return LENGTH(col[row]); // LENGTH of CHARSXP is nchar } -int getMaxStringLen(const SEXP *col, const int64_t n) { +int getMaxStringLen(const SEXP *col, const int64_t n) +{ int max = 0; SEXP last = NULL; for (int64_t i = 0; i < n; i++) { @@ -41,13 +44,15 @@ int getMaxStringLen(const SEXP *col, const int64_t n) { return max; } -int getMaxCategLen(SEXP col) { +int getMaxCategLen(SEXP col) +{ col = getAttrib(col, R_LevelsSymbol); if (!isString(col)) internal_error(__func__, "col passed to getMaxCategLen is missing levels"); return getMaxStringLen(STRING_PTR_RO(col), LENGTH(col)); } -const char *getCategString(SEXP col, int64_t row) { +const char *getCategString(SEXP col, int64_t row) +{ // the only writer that needs to have the header of the SEXP column, to get to the levels int x = INTEGER(col)[row]; return x == NA_INTEGER ? NULL : ENCODED_CHAR(STRING_ELT(getAttrib(col, R_LevelsSymbol), x - 1)); @@ -73,7 +78,8 @@ writer_fun_t *funs[] = { static int32_t whichWriter(SEXP); -void writeList(const void *col, int64_t row, char **pch) { +void writeList(const void *col, int64_t row, char **pch) +{ SEXP v = ((const SEXP*)col)[row]; int32_t wf = whichWriter(v); if (TYPEOF(v) == VECSXP || wf == INT32_MIN || isFactor(v)) { @@ -92,7 +98,8 @@ void writeList(const void *col, int64_t row, char **pch) { *pch = ch; } -int getMaxListItemLen(const SEXP *col, const int64_t n) { +int getMaxListItemLen(const SEXP *col, const int64_t n) +{ int max = 0; SEXP last = NULL; for (int64_t i = 0; i < n; i++) { @@ -117,7 +124,8 @@ int getMaxListItemLen(const SEXP *col, const int64_t n) { return max; } -static int32_t whichWriter(SEXP column) { +static int32_t whichWriter(SEXP column) +{ // int32_t is returned here just so the caller can output nice context-full error message should INT32_MIN be returned // the caller then passes uint8_t to fwriteMain switch(TYPEOF(column)) { diff --git a/src/gsumm.c b/src/gsumm.c index 5970f5919..a9c057bcd 100644 --- a/src/gsumm.c +++ b/src/gsumm.c @@ -42,7 +42,8 @@ static int nbit(int n) grouped summaries over a large data.table. OpenMP is used here to parallelize operations involved in calculating common group-wise statistics. */ -SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg) { +SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg) +{ double started = wallclock(); const bool verbose = GetVerbose(); if (TYPEOF(env) != ENVSXP) error(_("env is not an environment")); @@ -866,7 +867,8 @@ SEXP gmax(SEXP x, SEXP narm) } // gmedian, always returns numeric type (to avoid as.numeric() wrap..) -SEXP gmedian(SEXP x, SEXP narmArg) { +SEXP gmedian(SEXP x, SEXP narmArg) +{ if (!IS_TRUE_OR_FALSE(narmArg)) error(_("%s must be TRUE or FALSE"), "na.rm"); if (!isVectorAtomic(x)) error(_("GForce median can only be applied to columns, not .SD or similar. To find median of all items in a list such as .SD, either add the prefix stats::median(.SD) or turn off GForce optimization using options(datatable.optimize=1). More likely, you may be looking for 'DT[,lapply(.SD,median),by=,.SDcols=]'")); @@ -920,7 +922,8 @@ SEXP gmedian(SEXP x, SEXP narmArg) { return ans; } -static SEXP gfirstlast(SEXP x, const bool first, const int w, const bool headw) { +static SEXP gfirstlast(SEXP x, const bool first, const int w, const bool headw) +{ // w: which item (1 other than for gnthvalue when could be >1) // headw: select 1:w of each group when first=true, and (n-w+1):n when first=false (i.e. tail) const bool nosubset = irowslen == -1; @@ -993,27 +996,32 @@ static SEXP gfirstlast(SEXP x, const bool first, const int w, const bool headw) return(ans); } -SEXP glast(SEXP x) { +SEXP glast(SEXP x) +{ return gfirstlast(x, false, 1, false); } -SEXP gfirst(SEXP x) { +SEXP gfirst(SEXP x) +{ return gfirstlast(x, true, 1, false); } -SEXP gtail(SEXP x, SEXP nArg) { +SEXP gtail(SEXP x, SEXP nArg) +{ if (!isInteger(nArg) || LENGTH(nArg)!=1 || INTEGER(nArg)[0]<1) internal_error(__func__, "gtail is only implemented for n>0. This should have been caught before"); // # nocov const int n=INTEGER(nArg)[0]; return n==1 ? glast(x) : gfirstlast(x, false, n, true); } -SEXP ghead(SEXP x, SEXP nArg) { +SEXP ghead(SEXP x, SEXP nArg) +{ if (!isInteger(nArg) || LENGTH(nArg)!=1 || INTEGER(nArg)[0]<1) internal_error(__func__, "gtail is only implemented for n>0. This should have been caught before"); // # nocov const int n=INTEGER(nArg)[0]; return n==1 ? gfirst(x) : gfirstlast(x, true, n, true); } -SEXP gnthvalue(SEXP x, SEXP nArg) { +SEXP gnthvalue(SEXP x, SEXP nArg) +{ if (!isInteger(nArg) || LENGTH(nArg)!=1 || INTEGER(nArg)[0]<1) internal_error(__func__, "`g[` (gnthvalue) is only implemented single value subsets with positive index, e.g., .SD[2]. This should have been caught before"); // # nocov return gfirstlast(x, true, INTEGER(nArg)[0], false); } @@ -1105,15 +1113,18 @@ static SEXP gvarsd1(SEXP x, SEXP narmArg, bool isSD) return ans; } -SEXP gvar(SEXP x, SEXP narm) { +SEXP gvar(SEXP x, SEXP narm) +{ return (gvarsd1(x, narm, FALSE)); } -SEXP gsd(SEXP x, SEXP narm) { +SEXP gsd(SEXP x, SEXP narm) +{ return (gvarsd1(x, narm, TRUE)); } -SEXP gprod(SEXP x, SEXP narmArg) { +SEXP gprod(SEXP x, SEXP narmArg) +{ if (!IS_TRUE_OR_FALSE(narmArg)) error(_("%s must be TRUE or FALSE"), "na.rm"); const bool narm=LOGICAL(narmArg)[0]; @@ -1192,7 +1203,8 @@ SEXP gprod(SEXP x, SEXP narmArg) { return ans; } -SEXP gshift(SEXP x, SEXP nArg, SEXP fillArg, SEXP typeArg) { +SEXP gshift(SEXP x, SEXP nArg, SEXP fillArg, SEXP typeArg) +{ const bool nosubset = irowslen == -1; const bool issorted = !isunsorted; const int n = nosubset ? length(x) : irowslen; diff --git a/src/idatetime.c b/src/idatetime.c index ec3ce3ed3..ecec28c66 100644 --- a/src/idatetime.c +++ b/src/idatetime.c @@ -7,7 +7,8 @@ static const int YEARS1 = 365; typedef enum { YDAY, WDAY, MDAY, WEEK, MONTH, QUARTER, YEAR, YEARMON, YEARQTR} datetype; -static inline bool isLeapYear(int year) { +static inline bool isLeapYear(int year) +{ return (year % 100 != 0 || year % 400 == 0) && year % 4 == 0; } diff --git a/src/ijoin.c b/src/ijoin.c index e81e8325f..feef07700 100644 --- a/src/ijoin.c +++ b/src/ijoin.c @@ -6,8 +6,8 @@ // TODO: rewrite/simplify logic -- took me ages to understand what I wrote!! // TODO: benchmark and parallelise slow regions // TODO: implement 'lookup' for 'gaps' and 'overlaps' arguments -SEXP lookup(SEXP ux, SEXP xlen, SEXP indices, SEXP gaps, SEXP overlaps, SEXP multArg, SEXP typeArg, SEXP verbose) { - +SEXP lookup(SEXP ux, SEXP xlen, SEXP indices, SEXP gaps, SEXP overlaps, SEXP multArg, SEXP typeArg, SEXP verbose) +{ SEXP vv, tt, lookup, type_lookup; R_len_t *idx,*count,*type_count,xrows=INTEGER(xlen)[0],uxrows=LENGTH(VECTOR_ELT(ux, 0)),uxcols=LENGTH(ux); int *from = INTEGER(VECTOR_ELT(indices, 0)); @@ -221,8 +221,8 @@ SEXP lookup(SEXP ux, SEXP xlen, SEXP indices, SEXP gaps, SEXP overlaps, SEXP mul return(R_NilValue); } -SEXP overlaps(SEXP ux, SEXP imatches, SEXP multArg, SEXP typeArg, SEXP nomatchArg, SEXP verbose) { - +SEXP overlaps(SEXP ux, SEXP imatches, SEXP multArg, SEXP typeArg, SEXP nomatchArg, SEXP verbose) +{ R_len_t uxcols=LENGTH(ux),rows=length(VECTOR_ELT(imatches,0)); int nomatch = INTEGER(nomatchArg)[0], totlen=0, thislen; int *from = INTEGER(VECTOR_ELT(imatches, 0)); diff --git a/src/init.c b/src/init.c index ef81a7a0e..50b668a37 100644 --- a/src/init.c +++ b/src/init.c @@ -169,7 +169,8 @@ R_ExternalMethodDef externalMethods[] = { {NULL, NULL, 0} }; -static void setSizes(void) { +static void setSizes(void) +{ for (int i=0; i<100; ++i) { r_type_sizes[i]=0; r_type_order[i]=0; } // only these types are currently allowed as column types : r_type_sizes[LGLSXP] = sizeof(int); r_type_order[LGLSXP] = 0; @@ -314,7 +315,8 @@ void attribute_visible R_init_data_table(DllInfo *info) avoid_openmp_hang_within_fork(); } -inline long long DtoLL(double x) { +inline long long DtoLL(double x) +{ // Type punning such as // *(long long *)&REAL(column)[i] // is undefined by C standards. This may have been the cause of 1.10.2 failing on 31 Jan 2017 @@ -330,13 +332,15 @@ inline long long DtoLL(double x) { return (long long)u.i64; } -inline double LLtoD(long long x) { +inline double LLtoD(long long x) +{ union {double d; int64_t i64;} u; u.i64 = (int64_t)x; return u.d; } -int GetVerbose(void) { +int GetVerbose(void) +{ // don't call repetitively; save first in that case SEXP opt = GetOption1(sym_verbose); if ((!isLogical(opt) && !isInteger(opt)) || LENGTH(opt)!=1 || INTEGER(opt)[0]==NA_INTEGER) @@ -345,8 +349,8 @@ int GetVerbose(void) { } // # nocov start -SEXP hasOpenMP(void) { - +SEXP hasOpenMP(void) +{ #if defined(_OPENMP) // gcc build of libomp return ScalarInteger(_OPENMP); // return the version; e.g. 201511 (i.e. 4.5) @@ -364,13 +368,15 @@ SEXP hasOpenMP(void) { extern int *_Last_updated; // assign.c -SEXP initLastUpdated(SEXP var) { +SEXP initLastUpdated(SEXP var) +{ if (!isInteger(var) || LENGTH(var)!=1) error(_(".Last.updated in namespace is not a length 1 integer")); _Last_updated = INTEGER(var); return R_NilValue; } -SEXP dllVersion(void) { +SEXP dllVersion(void) +{ // .onLoad calls this and checks the same as packageVersion() to ensure no R/C version mismatch, #3056 return(ScalarString(mkChar("1.17.99"))); } diff --git a/src/mergelist.c b/src/mergelist.c index 51f28d224..d0fcc754e 100644 --- a/src/mergelist.c +++ b/src/mergelist.c @@ -1,7 +1,8 @@ #include "data.table.h" // set(x, NULL, cols, copy(unclass(x)[cols])) ## but keeps the index -SEXP copyCols(SEXP x, SEXP cols) { +SEXP copyCols(SEXP x, SEXP cols) +{ // used in R/mergelist.R if (!isDataTable(x)) internal_error(__func__, "'x' must be a data.table"); // # nocov @@ -17,7 +18,8 @@ SEXP copyCols(SEXP x, SEXP cols) { return R_NilValue; } -void mergeIndexAttrib(SEXP to, SEXP from) { +void mergeIndexAttrib(SEXP to, SEXP from) +{ if (!isInteger(to) || LENGTH(to)!=0) internal_error(__func__, "'to' must be integer() already"); // # nocov if (isNull(from)) @@ -31,7 +33,8 @@ void mergeIndexAttrib(SEXP to, SEXP from) { } } -SEXP cbindlist(SEXP x, SEXP copyArg) { +SEXP cbindlist(SEXP x, SEXP copyArg) +{ if (!isNewList(x) || isDataFrame(x)) error(_("'%s' must be a list"), "x"); bool copy = (bool)LOGICAL(copyArg)[0]; diff --git a/src/nafill.c b/src/nafill.c index 5c9568efb..90fcddae6 100644 --- a/src/nafill.c +++ b/src/nafill.c @@ -1,6 +1,7 @@ #include "data.table.h" -void nafillDouble(double *x, uint_fast64_t nx, unsigned int type, double fill, bool nan_is_na, ans_t *ans, bool verbose) { +void nafillDouble(double *x, uint_fast64_t nx, unsigned int type, double fill, bool nan_is_na, ans_t *ans, bool verbose) +{ double tic=0.0; if (verbose) tic = omp_get_wtime(); @@ -42,7 +43,9 @@ void nafillDouble(double *x, uint_fast64_t nx, unsigned int type, double fill, b if (verbose) snprintf(ans->message[0], 500, _("%s: took %.3fs\n"), __func__, omp_get_wtime()-tic); } -void nafillInteger(int32_t *x, uint_fast64_t nx, unsigned int type, int32_t fill, ans_t *ans, bool verbose) { + +void nafillInteger(int32_t *x, uint_fast64_t nx, unsigned int type, int32_t fill, ans_t *ans, bool verbose) +{ double tic=0.0; if (verbose) tic = omp_get_wtime(); @@ -64,7 +67,9 @@ void nafillInteger(int32_t *x, uint_fast64_t nx, unsigned int type, int32_t fill if (verbose) snprintf(ans->message[0], 500, _("%s: took %.3fs\n"), __func__, omp_get_wtime()-tic); } -void nafillInteger64(int64_t *x, uint_fast64_t nx, unsigned int type, int64_t fill, ans_t *ans, bool verbose) { + +void nafillInteger64(int64_t *x, uint_fast64_t nx, unsigned int type, int64_t fill, ans_t *ans, bool verbose) +{ double tic=0.0; if (verbose) tic = omp_get_wtime(); @@ -92,7 +97,8 @@ void nafillInteger64(int64_t *x, uint_fast64_t nx, unsigned int type, int64_t fi over columns of the input data. This includes handling different data types and applying the designated filling method to each column in parallel. */ -SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, SEXP cols) { +SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, SEXP cols) +{ int protecti=0; const bool verbose = GetVerbose(); diff --git a/src/programming.c b/src/programming.c index e0e4f3983..589ba4d6e 100644 --- a/src/programming.c +++ b/src/programming.c @@ -25,7 +25,8 @@ static void substitute_call_arg_names(SEXP expr, SEXP env) } } -SEXP substitute_call_arg_namesR(SEXP expr, SEXP env) { +SEXP substitute_call_arg_namesR(SEXP expr, SEXP env) +{ SEXP ans = PROTECT(MAYBE_REFERENCED(expr) ? duplicate(expr) : expr); substitute_call_arg_names(ans, env); // updates in-place UNPROTECT(1); diff --git a/src/shellsort.c b/src/shellsort.c index 47a4fcf41..71b19c95f 100644 --- a/src/shellsort.c +++ b/src/shellsort.c @@ -22,7 +22,8 @@ static const int sedgewick1982[17] = { * uses sedgewick1982 gap sequence formula as it turned out to be the most efficient - tested various n, k (for rnorm only!) * currently used in frollmedian algo="fast" when no NAs */ -void shellsort(const double *x, int n, int *o) { +void shellsort(const double *x, int n, int *o) +{ for (int i=0; i < n; i++) o[i] = i; int gap = 0; while (sedgewick1982[gap] > n) gap++; diff --git a/src/subset.c b/src/subset.c index 1dd9f5f17..2e90a297f 100644 --- a/src/subset.c +++ b/src/subset.c @@ -275,7 +275,8 @@ static void checkCol(SEXP col, int colNum, int nrow, SEXP x) * * OpenMP is used here to parallelize the loops that perform the subsetting of vectors, with conditional checks and filtering of data. */ -SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) { // API change needs update NEWS.md and man/cdt.Rd +SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) // API change needs update NEWS.md and man/cdt.Rd +{ int nprotect=0; if (!isNewList(x)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "x", "CsubsetDT", type2char(TYPEOF(rows)), "list"); // # nocov if (!length(x)) return(x); // return empty list @@ -363,7 +364,8 @@ SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) { // API change needs update NEWS.md return ans; } -SEXP subsetVector(SEXP x, SEXP idx) { // idx is 1-based passed from R level +SEXP subsetVector(SEXP x, SEXP idx) // idx is 1-based passed from R level +{ bool anyNA=false, orderedSubset=false; int nprotect=0; if (isNull(x)) diff --git a/src/types.c b/src/types.c index 70578dc36..b6a9287e0 100644 --- a/src/types.c +++ b/src/types.c @@ -4,14 +4,16 @@ /* * find end of a string, used to append verbose messages or warnings */ -char *end(char *start) { +char *end(char *start) +{ return strchr(start, 0); } /* * logging status and messages, warnings, errors to ans_t */ -void ansSetMsg(ans_t *ans, uint8_t status, const char *msg, const char *func) { +void ansSetMsg(ans_t *ans, uint8_t status, const char *msg, const char *func) +{ if (status > ans->status) ans->status = status; snprintf(end(ans->message[status]), 500, _(msg), func); // func should be passed via ... really, thus this helper cannot replace all cases we need @@ -21,7 +23,8 @@ void ansSetMsg(ans_t *ans, uint8_t status, const char *msg, const char *func) { /* * function to print verbose messages, stderr messages, warnings and errors stored in ans_t struct */ -void ansGetMsgs(ans_t *ans, int n, bool verbose, const char *func) { +void ansGetMsgs(ans_t *ans, int n, bool verbose, const char *func) +{ for (int i=0; iint_v[0] = ans->status; } + /* This caters to internal tests (not user-facing), and OpenMP is being used here to test a message printing function inside a nested loop which has @@ -68,7 +73,8 @@ void testRaiseMsg(ans_t *ans, int istatus, bool verbose) { collapse(2), along with specification of dynamic scheduling for distributing the iterations in a way that can balance the workload among the threads. */ -SEXP testMsgR(SEXP status, SEXP x, SEXP k) { +SEXP testMsgR(SEXP status, SEXP x, SEXP k) +{ if (!isInteger(status) || !isInteger(x) || !isInteger(k)) internal_error(__func__, "status, nx, nk must be integer"); // # nocov int protecti = 0; diff --git a/src/uniqlist.c b/src/uniqlist.c index 333d6bc69..2fe672759 100644 --- a/src/uniqlist.c +++ b/src/uniqlist.c @@ -146,7 +146,8 @@ SEXP uniqlist(SEXP l, SEXP order) return(ans); } -SEXP uniqlengths(SEXP x, SEXP n) { +SEXP uniqlengths(SEXP x, SEXP n) +{ // seems very similar to rbindlist.c:uniq_lengths. TODO: centralize into common function if (TYPEOF(x) != INTSXP) error(_("Input argument 'x' to 'uniqlengths' must be an integer vector")); if (TYPEOF(n) != INTSXP || length(n) != 1) error(_("Input argument 'n' to 'uniqlengths' must be an integer vector of length 1")); @@ -167,7 +168,8 @@ SEXP uniqlengths(SEXP x, SEXP n) { // we could compute `uniqlist` and `uniqlengths` and then construct the result // but that seems unnecessary waste of memory and roundabout.. // so, we'll do it directly here.. -SEXP rleid(SEXP l, SEXP cols) { +SEXP rleid(SEXP l, SEXP cols) +{ R_xlen_t nrow = xlength(VECTOR_ELT(l, 0)); R_len_t ncol = length(l), lencols = length(cols); if (!nrow || !ncol) return(allocVector(INTSXP, 0)); @@ -256,7 +258,8 @@ SEXP rleid(SEXP l, SEXP cols) { return(ans); } -SEXP nestedid(SEXP l, SEXP cols, SEXP order, SEXP grps, SEXP resetvals, SEXP multArg) { +SEXP nestedid(SEXP l, SEXP cols, SEXP order, SEXP grps, SEXP resetvals, SEXP multArg) +{ Rboolean byorder = (length(order)>0); SEXP v, ans; if (!isNewList(l) || length(l) < 1) internal_error(__func__, "l is not a list length 1 or more"); // # nocov @@ -351,7 +354,8 @@ SEXP nestedid(SEXP l, SEXP cols, SEXP order, SEXP grps, SEXP resetvals, SEXP mul return(ans); } -SEXP uniqueNlogical(SEXP x, SEXP narmArg) { +SEXP uniqueNlogical(SEXP x, SEXP narmArg) +{ // single pass; short-circuit and return as soon as all 3 values are found if (!isLogical(x)) error(_("x is not a logical vector")); if (!IS_TRUE_OR_FALSE(narmArg)) diff --git a/src/utils.c b/src/utils.c index b159fa0e6..35ff7396d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,6 +1,7 @@ #include "data.table.h" -bool within_int32_repres(double x) { +bool within_int32_repres(double x) +{ // N.B. (int)2147483647.99 is not undefined behaviour since s 6.3.1.4 of the C // standard states that behaviour is undefined only if the integral part of a // finite value of standard floating type cannot be represented. @@ -8,13 +9,15 @@ bool within_int32_repres(double x) { return R_FINITE(x) && x < 2147483648 && x > -2147483648; } -bool within_int64_repres(double x) { +bool within_int64_repres(double x) +{ return R_FINITE(x) && x <= (double)INT64_MAX && x >= (double)INT64_MIN; } // used to error if not passed type double but this needed extra is.double() calls in calling R code // which needed a repeat of the argument. Hence simpler and more robust to return false when not type double. -bool fitsInInt32(SEXP x) { +bool fitsInInt32(SEXP x) +{ if (!isReal(x) || INHERITS(x, char_integer64)) return false; R_xlen_t n=xlength(x), i=0; @@ -27,11 +30,13 @@ bool fitsInInt32(SEXP x) { return i==n; } -SEXP fitsInInt32R(SEXP x) { +SEXP fitsInInt32R(SEXP x) +{ return ScalarLogical(fitsInInt32(x)); } -bool fitsInInt64(SEXP x) { +bool fitsInInt64(SEXP x) +{ if (!isReal(x) || INHERITS(x, char_integer64)) return false; R_xlen_t n=xlength(x), i=0; @@ -44,11 +49,13 @@ bool fitsInInt64(SEXP x) { return i==n; } -SEXP fitsInInt64R(SEXP x) { +SEXP fitsInInt64R(SEXP x) +{ return ScalarLogical(fitsInInt64(x)); } -bool allNA(SEXP x, bool errorForBadType) { +bool allNA(SEXP x, bool errorForBadType) +{ // less space and time than all(is.na(x)) at R level because that creates full size is.na(x) first before all() // whereas this allNA can often return early on testing the first value without reading the rest const int n = length(x); @@ -98,7 +105,8 @@ bool allNA(SEXP x, bool errorForBadType) { // https://github.com/Rdatatable/data.table/pull/3909#discussion_r329065950 } -SEXP allNAR(SEXP x) { +SEXP allNAR(SEXP x) +{ return ScalarLogical(allNA(x, /*errorForBadType=*/true)); } @@ -111,7 +119,8 @@ SEXP allNAR(SEXP x) { * optionally (check_dups) check for no duplicates * optionally (skip_absent) skip (return 0) for numbers outside the range or not naming extant columns */ -SEXP colnamesInt(SEXP x, SEXP cols, SEXP check_dups, SEXP skip_absent) { +SEXP colnamesInt(SEXP x, SEXP cols, SEXP check_dups, SEXP skip_absent) +{ if (!isNewList(x)) error(_("'x' argument must be data.table compatible")); if (!IS_TRUE_OR_FALSE(check_dups)) @@ -168,7 +177,8 @@ SEXP colnamesInt(SEXP x, SEXP cols, SEXP check_dups, SEXP skip_absent) { return ricols; } -inline bool INHERITS(SEXP x, SEXP char_) { +inline bool INHERITS(SEXP x, SEXP char_) +{ // Thread safe inherits() by pre-calling install() in init.c and then // passing those char_* in here for simple and fast non-API pointer compare. // The thread-safety aspect here is only currently actually needed for list columns in @@ -203,7 +213,8 @@ inline bool INHERITS(SEXP x, SEXP char_) { return false; } -SEXP copyAsPlain(SEXP x) { +SEXP copyAsPlain(SEXP x) +{ // v1.12.2 and before used standard R duplicate() to do this. But duplicate() is not guaranteed to not return an ALTREP. // e.g. ALTREP 'wrapper' on factor column (with materialized INTSXP) in package VIM under example(hotdeck) // .Internal(inspect(x[[5]])) @@ -263,7 +274,8 @@ SEXP copyAsPlain(SEXP x) { return ans; } -void copySharedColumns(SEXP x) { +void copySharedColumns(SEXP x) +{ const int ncol = length(x); if (!isNewList(x) || ncol==1) return; bool *shared = (bool *)R_alloc(ncol, sizeof(*shared)); // on R heap in case alloc fails @@ -314,23 +326,31 @@ void copySharedColumns(SEXP x) { // lock, unlock and islocked at C level : // 1) for speed to reduce overhead // 2) to avoid an R level wrapper which bumps MAYBE_SHARED; see the unlock after eval(jval) in data.table.R, #1341 #2245 -SEXP lock(SEXP DT) { +SEXP lock(SEXP DT) +{ setAttrib(DT, sym_datatable_locked, ScalarLogical(TRUE)); return DT; } -SEXP unlock(SEXP DT) { + +SEXP unlock(SEXP DT) +{ setAttrib(DT, sym_datatable_locked, R_NilValue); return DT; } -bool islocked(SEXP DT) { + +bool islocked(SEXP DT) +{ SEXP att = getAttrib(DT, sym_datatable_locked); return isLogical(att) && LENGTH(att)==1 && LOGICAL(att)[0]==1; } -SEXP islockedR(SEXP DT) { + +SEXP islockedR(SEXP DT) +{ return ScalarLogical(islocked(DT)); } -bool need2utf8(SEXP x) { +bool need2utf8(SEXP x) +{ const int xlen = length(x); const SEXP *xd = STRING_PTR_RO(x); for (int i=0; i #endif -SEXP dt_zlib_version(void) { + +SEXP dt_zlib_version(void) +{ char out[70]; #ifndef NOZLIB snprintf(out, sizeof(out), "zlibVersion()==%s ZLIB_VERSION==%s", zlibVersion(), ZLIB_VERSION); // # notranslate @@ -421,7 +446,9 @@ SEXP dt_zlib_version(void) { #endif return ScalarString(mkChar(out)); } -SEXP dt_has_zlib(void) { + +SEXP dt_has_zlib(void) +{ #ifndef NOZLIB return ScalarLogical(1); #else @@ -429,7 +456,8 @@ SEXP dt_has_zlib(void) { #endif } -SEXP startsWithAny(const SEXP x, const SEXP y, SEXP start) { +SEXP startsWithAny(const SEXP x, const SEXP y, SEXP start) +{ // for is_url in fread.R added in #5097 // basically any(startsWith()), short and simple ascii-only if (!isString(x) || !isString(y) || length(x)!=1 || length(y)<1 || !isLogical(start) || length(start)!=1 || LOGICAL(start)[0]==NA_LOGICAL) @@ -456,7 +484,8 @@ SEXP startsWithAny(const SEXP x, const SEXP y, SEXP start) { // if (length(x)) length(x[[1L]]) else 0L // used in src/mergelist.c and below in commented out set_row_names -int n_rows(SEXP x) { +int n_rows(SEXP x) +{ if (!LENGTH(x)) return 0; // # nocov. Not yet reached from anywhere, cbindlist uses it but escapes for !n_columns(x) return length(VECTOR_ELT(x, 0)); @@ -465,7 +494,8 @@ int n_rows(SEXP x) { // length(x) // used in src/mergelist.c // to be an abstraction layer on C level -int n_columns(SEXP x) { +int n_columns(SEXP x) +{ return LENGTH(x); } @@ -504,13 +534,15 @@ int n_columns(SEXP x) { }*/ // inherits(x, "data.table") -bool isDataTable(SEXP x) { +bool isDataTable(SEXP x) +{ return INHERITS(x, char_datatable); } // rectangular list; NB does not allow length-1 recycling // length(x) <= 1L || length(unique(lengths(x))) == 1L -static inline bool isRectangular(SEXP x) { +static inline bool isRectangular(SEXP x) +{ int n = LENGTH(x); if (n < 2) return true; @@ -524,7 +556,8 @@ static inline bool isRectangular(SEXP x) { // setDT()-friendly rectangular list, i.e. // a named list() with all entries of equal length() -bool isRectangularList(SEXP x) { +bool isRectangularList(SEXP x) +{ if (!isNewList(x)) return false; if (!LENGTH(x)) @@ -534,14 +567,18 @@ bool isRectangularList(SEXP x) { return isRectangular(x); } -bool perhapsDataTable(SEXP x) { +bool perhapsDataTable(SEXP x) +{ return isDataTable(x) || isDataFrame(x) || isRectangularList(x); } -SEXP perhapsDataTableR(SEXP x) { + +SEXP perhapsDataTableR(SEXP x) +{ return ScalarLogical(perhapsDataTable(x)); } -SEXP frev(SEXP x, SEXP copyArg) { +SEXP frev(SEXP x, SEXP copyArg) +{ if (INHERITS(x, char_dataframe)) error(_("'x' should not be data.frame or data.table.")); if (!IS_TRUE_OR_FALSE(copyArg)) @@ -649,7 +686,8 @@ SEXP frev(SEXP x, SEXP copyArg) { return x; } -void internal_error(const char *call_name, const char *format, ...) { +void internal_error(const char *call_name, const char *format, ...) +{ char buff[1024]; va_list args; va_start(args, format); diff --git a/src/wrappers.c b/src/wrappers.c index 2b26761bf..816821b81 100644 --- a/src/wrappers.c +++ b/src/wrappers.c @@ -38,8 +38,8 @@ SEXP setattrib(SEXP x, SEXP name, SEXP value) } // fix for #1142 - duplicated levels for factors -SEXP setlevels(SEXP x, SEXP levels, SEXP ulevels) { - +SEXP setlevels(SEXP x, SEXP levels, SEXP ulevels) +{ R_len_t nx = length(x); SEXP xchar, newx; xchar = PROTECT(allocVector(STRSXP, nx)); @@ -128,7 +128,8 @@ SEXP dim(SEXP x) return ans; } -SEXP warn_matrix_column_r(SEXP i) { +SEXP warn_matrix_column_r(SEXP i) +{ warn_matrix_column(INTEGER(i)[0]); return R_NilValue; }