From 3dc02dcd1d106a2b202c717a6f9bca63c84f604c Mon Sep 17 00:00:00 2001 From: Nick Christofides <118103879+NicChr@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:02:41 +0200 Subject: [PATCH] Minor updates. --- src/lag.cpp | 50 +++++++++--------------------------------------- src/set_math.cpp | 36 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 59 deletions(-) diff --git a/src/lag.cpp b/src/lag.cpp index 02c38bb..d051b1a 100644 --- a/src/lag.cpp +++ b/src/lag.cpp @@ -8,18 +8,14 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { if (fill_size > 1){ Rf_error("fill size must be NULL or length 1"); } + R_xlen_t klag = k; switch(TYPEOF(x)){ case NILSXP: { return R_NilValue; } case LGLSXP: case INTSXP: { - if (k > size){ - k = size; - } - if (k < -size){ - k = -size; - } + k = k >= 0 ? std::min(size, klag) : std::max(-size, klag); int fill_value = NA_INTEGER; if (fill_size >= 1){ fill_value = Rf_asInteger(fill); @@ -77,8 +73,6 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { if (!Rf_isNull(Rf_getAttrib(x, R_NamesSymbol))){ SEXP old_names = Rf_protect(Rf_getAttrib(x, R_NamesSymbol)); ++n_protections; - // SEXP empty_char = Rf_protect(Rf_ScalarString(NA_STRING)); - // ++n_protections; SEXP new_names = Rf_protect(cpp_lag(old_names, k, R_NilValue, set, recursive)); ++n_protections; Rf_setAttrib(out, R_NamesSymbol, new_names); @@ -87,12 +81,7 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { return out; } case REALSXP: { - if (k > size){ - k = size; - } - if (k < -size){ - k = -size; - } + k = k >= 0 ? std::min(size, klag) : std::max(-size, klag); double fill_value = NA_REAL; if (fill_size >= 1){ fill_value = Rf_asReal(fill); @@ -158,12 +147,7 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { return out; } case CPLXSXP: { - if (k > size){ - k = size; - } - if (k < -size){ - k = -size; - } + k = k >= 0 ? std::min(size, klag) : std::max(-size, klag); SEXP fill_sexp = Rf_protect(Rf_allocVector(CPLXSXP, 1)); ++n_protections; Rcomplex *p_fill = COMPLEX(fill_sexp); @@ -232,12 +216,7 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { return out; } case STRSXP: { - if (k > size){ - k = size; - } - if (k < -size){ - k = -size; - } + k = k >= 0 ? std::min(size, klag) : std::max(-size, klag); SEXP fill_char = Rf_protect(fill_size >= 1 ? Rf_asChar(fill) : NA_STRING); ++n_protections; SEXP out = Rf_protect(set ? x : Rf_duplicate(x)); @@ -301,12 +280,7 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { return out; } case RAWSXP: { - if (k > size){ - k = size; - } - if (k < -size){ - k = -size; - } + k = k >= 0 ? std::min(size, klag) : std::max(-size, klag); SEXP raw_sexp = Rf_protect(Rf_coerceVector(fill, RAWSXP)); Rbyte fill_raw = fill_size == 0 ? RAW(Rf_ScalarRaw(0))[0] : RAW(raw_sexp)[0]; ++n_protections; @@ -370,23 +344,17 @@ SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive) { } case VECSXP: { if (recursive){ - R_xlen_t n_items = Rf_xlength(x); const SEXP *p_x = VECTOR_PTR_RO(x); - SEXP out = Rf_protect(Rf_allocVector(VECSXP, n_items)); + SEXP out = Rf_protect(Rf_allocVector(VECSXP, size)); ++n_protections; SHALLOW_DUPLICATE_ATTRIB(out, x); - for (R_xlen_t i = 0; i < n_items; ++i){ + for (R_xlen_t i = 0; i < size; ++i){ SET_VECTOR_ELT(out, i, cpp_lag(p_x[i], k, fill, set, true)); } Rf_unprotect(n_protections); return out; } else { - if (k > size){ - k = size; - } - if (k < -size){ - k = -size; - } + k = k >= 0 ? std::min(size, klag) : std::max(-size, klag); SEXP fill_value = Rf_protect(Rf_coerceVector(fill_size >= 1 ? fill : R_NilValue, VECSXP)); ++n_protections; SEXP out = Rf_protect(set ? x : Rf_allocVector(VECSXP, size)); diff --git a/src/set_math.cpp b/src/set_math.cpp index 4e7c7e7..a05a464 100644 --- a/src/set_math.cpp +++ b/src/set_math.cpp @@ -30,12 +30,12 @@ SEXP cpp_set_abs(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] != NA_INTEGER) p_x[i] = std::abs(p_x[i]); + p_x[i] = p_x[i] == NA_INTEGER ? p_x[i] : std::abs(p_x[i]); } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] != NA_INTEGER) p_x[i] = std::abs(p_x[i]); + p_x[i] = p_x[i] == NA_INTEGER ? p_x[i] : std::abs(p_x[i]); } } break; @@ -45,13 +45,13 @@ SEXP cpp_set_abs(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::fabs(p_x[i]); + p_x[i] = p_x[i] != p_x[i] ? p_x[i] : std::abs(p_x[i]); } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::fabs(p_x[i]); + p_x[i] = p_x[i] != p_x[i] ? p_x[i] : std::abs(p_x[i]); } } break; @@ -70,12 +70,12 @@ SEXP cpp_set_floor(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::floor(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::floor(p_x[i]) : p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::floor(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::floor(p_x[i]) : p_x[i]; } } } @@ -92,12 +92,12 @@ SEXP cpp_set_ceiling(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::ceil(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::ceil(p_x[i]) : p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::ceil(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::ceil(p_x[i]) : p_x[i]; } } } @@ -114,12 +114,12 @@ SEXP cpp_set_trunc(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::trunc(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::trunc(p_x[i]) : p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::trunc(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::trunc(p_x[i]) : p_x[i]; } } } @@ -137,12 +137,12 @@ SEXP cpp_set_change_sign(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] != NA_INTEGER) p_x[i] = -p_x[i]; + p_x[i] = (p_x[i] == NA_INTEGER) ? p_x[i] : -p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] != NA_INTEGER) p_x[i] = -p_x[i]; + p_x[i] = (p_x[i] == NA_INTEGER) ? p_x[i] : -p_x[i]; } } break; @@ -153,12 +153,12 @@ SEXP cpp_set_change_sign(SEXP x){ int n_cores = num_cores(); OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = -p_x[i]; + p_x[i] = (p_x[i] == p_x[i]) ? -p_x[i] : p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = -p_x[i]; + p_x[i] = (p_x[i] == p_x[i]) ? -p_x[i] : p_x[i]; } } break; @@ -178,12 +178,12 @@ SEXP cpp_set_exp(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::exp(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::exp(p_x[i]) : p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::exp(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::exp(p_x[i]) : p_x[i]; } } Rf_unprotect(1); @@ -201,12 +201,12 @@ SEXP cpp_set_sqrt(SEXP x){ if (n_cores > 1){ OMP_PARALLEL_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::sqrt(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::sqrt(p_x[i]) : p_x[i]; } } else { OMP_FOR_SIMD for (R_xlen_t i = 0; i < n; ++i) { - if (p_x[i] == p_x[i]) p_x[i] = std::sqrt(p_x[i]); + p_x[i] = (p_x[i] == p_x[i]) ? std::sqrt(p_x[i]) : p_x[i]; } } Rf_unprotect(1);