Skip to content

Commit cb5c837

Browse files
committed
improved read onlyness and constness
1 parent 738779b commit cb5c837

23 files changed

+316
-309
lines changed

src/between.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, SEXP checkArg) {
1010
int nprotect = 0;
11-
R_len_t nx = length(x), nl = length(lower), nu = length(upper);
11+
const R_len_t nx = length(x), nl = length(lower), nu = length(upper);
1212
if (!nx || !nl || !nu)
1313
return (allocVector(LGLSXP, 0));
1414
const int longest = MAX(MAX(nx, nl), nu);
@@ -20,13 +20,13 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
2020
const int longestBound = MAX(nl, nu); // just for when check=TRUE
2121
if (!IS_TRUE_OR_FALSE(incbounds))
2222
error(_("%s must be TRUE or FALSE"), "incbounds");
23-
const bool open = !LOGICAL(incbounds)[0];
24-
if (!isLogical(NAboundsArg) || LOGICAL(NAboundsArg)[0]==FALSE)
23+
const bool open = !LOGICAL_RO(incbounds)[0];
24+
if (!isLogical(NAboundsArg) || LOGICAL_RO(NAboundsArg)[0]==FALSE)
2525
error(_("NAbounds must be TRUE or NA"));
26-
const bool NAbounds = LOGICAL(NAboundsArg)[0]==TRUE;
26+
const bool NAbounds = LOGICAL_RO(NAboundsArg)[0]==TRUE;
2727
if (!IS_TRUE_OR_FALSE(checkArg))
2828
error(_("%s must be TRUE or FALSE"), "check");
29-
const bool check = LOGICAL(checkArg)[0];
29+
const bool check = LOGICAL_RO(checkArg)[0];
3030
const bool verbose = GetVerbose();
3131

3232
// check before potential coercion which ignores methods, #7164
@@ -70,13 +70,13 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
7070
const int uppMask = recycleUpp ? 0 : INT_MAX;
7171
SEXP ans = PROTECT(allocVector(LGLSXP, longest)); nprotect++;
7272
int *restrict ansp = LOGICAL(ans);
73-
double tic=omp_get_wtime();
73+
const double tic=omp_get_wtime();
7474

7575
switch (TYPEOF(x)) {
7676
case INTSXP: {
77-
const int *lp = INTEGER(lower);
78-
const int *up = INTEGER(upper);
79-
const int *xp = INTEGER(x);
77+
const int *lp = INTEGER_RO(lower);
78+
const int *up = INTEGER_RO(upper);
79+
const int *xp = INTEGER_RO(x);
8080
if (check) for (int i=0; i<longestBound; ++i) {
8181
const int l=lp[i & lowMask], u=up[i & uppMask];
8282
if (l!=NA_INTEGER && u!=NA_INTEGER && l>u)
@@ -103,9 +103,9 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
103103

104104
case REALSXP:
105105
if (INHERITS(x, char_integer64)) {
106-
const int64_t *lp = (int64_t *)REAL(lower);
107-
const int64_t *up = (int64_t *)REAL(upper);
108-
const int64_t *xp = (int64_t *)REAL(x);
106+
const int64_t *lp = (const int64_t*)REAL_RO(lower);
107+
const int64_t* up = (const int64_t*)REAL_RO(upper);
108+
const int64_t* xp = (const int64_t*)REAL_RO(x);
109109
if (check) for (int i=0; i<longestBound; ++i) {
110110
const int64_t l=lp[i & lowMask], u=up[i & uppMask];
111111
if (l!=NA_INTEGER64 && u!=NA_INTEGER64 && l>u)
@@ -128,9 +128,9 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
128128
}
129129
if (verbose) Rprintf(_("between parallel processing of integer64 took %8.3fs\n"), omp_get_wtime()-tic);
130130
} else {
131-
const double *lp = REAL(lower);
132-
const double *up = REAL(upper);
133-
const double *xp = REAL(x);
131+
const double *lp = REAL_RO(lower);
132+
const double *up = REAL_RO(upper);
133+
const double *xp = REAL_RO(x);
134134
if (check) for (int i=0; i<longestBound; ++i) {
135135
const double l=lp[i & lowMask], u=up[i & uppMask];
136136
if (!isnan(l) && !isnan(u) && l>u)

src/bmerge.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Differences over standard binary search (e.g. bsearch in stdlib.h) :
2626
static const SEXP *idtVec, *xdtVec;
2727
static const int *icols, *xcols;
2828
static SEXP nqgrp;
29-
static int ncol, *o, *xo, *retFirst, *retLength, *retIndex, *allLen1, *allGrp1, *rollends, ilen, anslen;
29+
static int ncol, *o, *xo, *retFirst, *retLength, *retIndex, *allLen1, *allGrp1, ilen, anslen;
30+
static const int* rollends;
3031
static int *op, nqmaxgrp;
3132
static int ctr, nomatch; // populating matches for non-equi joins
3233
enum {ALL, FIRST, LAST, ERR} mult = ALL;
@@ -56,8 +57,8 @@ SEXP bmerge(SEXP idt, SEXP xdt, SEXP icolsArg, SEXP xcolsArg, SEXP xoArg, SEXP r
5657
if ((LENGTH(icolsArg)==0 || LENGTH(xcolsArg)==0) && LENGTH(idt)>0) // We let through LENGTH(i) == 0 for tests 2126.*
5758
internal_error(__func__, "icols and xcols must be non-empty integer vectors");
5859
if (LENGTH(icolsArg) > LENGTH(xcolsArg)) internal_error(__func__, "length(icols) [%d] > length(xcols) [%d]", LENGTH(icolsArg), LENGTH(xcolsArg)); // # nocov
59-
icols = INTEGER(icolsArg);
60-
xcols = INTEGER(xcolsArg);
60+
icols = INTEGER_RO(icolsArg);
61+
xcols = INTEGER_RO(xcolsArg);
6162
xN = LENGTH(xdt) ? LENGTH(VECTOR_ELT(xdt,0)) : 0;
6263
iN = ilen = anslen = LENGTH(idt) ? LENGTH(VECTOR_ELT(idt,0)) : 0;
6364
ncol = LENGTH(icolsArg); // there may be more sorted columns in x than involved in the join
@@ -94,7 +95,7 @@ SEXP bmerge(SEXP idt, SEXP xdt, SEXP icolsArg, SEXP xcolsArg, SEXP xoArg, SEXP r
9495
rollabs = fabs(roll);
9596
if (!isLogical(rollendsArg) || LENGTH(rollendsArg) != 2)
9697
error(_("rollends must be a length 2 logical vector"));
97-
rollends = LOGICAL(rollendsArg);
98+
rollends = LOGICAL_RO(rollendsArg);
9899

99100
if (isNull(nomatchArg)) {
100101
nomatch=0;

src/cj.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SEXP cj(SEXP base_list) {
2424
switch(TYPEOF(source)) {
2525
case LGLSXP:
2626
case INTSXP: {
27-
const int *restrict sourceP = INTEGER(source);
27+
const int *restrict sourceP = INTEGER_RO(source);
2828
int *restrict targetP = INTEGER(target);
2929
#pragma omp parallel for num_threads(getDTthreads(thislen*eachrep, true))
3030
// default static schedule so two threads won't write to same cache line in last column
@@ -40,7 +40,7 @@ SEXP cj(SEXP base_list) {
4040
}
4141
} break;
4242
case REALSXP: {
43-
const double *restrict sourceP = REAL(source);
43+
const double *restrict sourceP = REAL_RO(source);
4444
double *restrict targetP = REAL(target);
4545
#pragma omp parallel for num_threads(getDTthreads(thislen*eachrep, true))
4646
for (int i=0; i<thislen; ++i) {
@@ -54,7 +54,7 @@ SEXP cj(SEXP base_list) {
5454
}
5555
} break;
5656
case CPLXSXP: {
57-
const Rcomplex *restrict sourceP = COMPLEX(source);
57+
const Rcomplex *restrict sourceP = COMPLEX_RO(source);
5858
Rcomplex *restrict targetP = COMPLEX(target);
5959
#pragma omp parallel for num_threads(getDTthreads(thislen*eachrep, true))
6060
for (int i=0; i<thislen; ++i) {

src/coalesce.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ SEXP coalesce(SEXP x, SEXP inplaceArg, SEXP nan_is_na_arg) {
1010
if (TYPEOF(x)!=VECSXP) internal_error(__func__, "input is list(...) at R level"); // # nocov
1111
if (!IS_TRUE_OR_FALSE(inplaceArg)) internal_error(__func__, "argument 'inplaceArg' must be TRUE or FALSE"); // # nocov
1212
if (!IS_TRUE_OR_FALSE(nan_is_na_arg)) internal_error(__func__, "argument 'nan_is_na_arg' must be TRUE or FALSE"); // # nocov
13-
const bool inplace = LOGICAL(inplaceArg)[0];
14-
const bool nan_is_na = LOGICAL(nan_is_na_arg)[0];
13+
const bool inplace = LOGICAL_RO(inplaceArg)[0];
14+
const bool nan_is_na = LOGICAL_RO(nan_is_na_arg)[0];
1515
const bool verbose = GetVerbose();
1616
int nprotect = 0;
1717
if (length(x)==0 || isNull(VECTOR_ELT(x,0))) return R_NilValue; // coalesce(NULL, "foo") return NULL even though character type mismatches type NULL
@@ -63,7 +63,7 @@ SEXP coalesce(SEXP x, SEXP inplaceArg, SEXP nan_is_na_arg) {
6363
for (int j=0; j<nval; ++j) {
6464
SEXP item = VECTOR_ELT(x, j+off);
6565
if (length(item)==1) {
66-
int tt = INTEGER(item)[0];
66+
int tt = INTEGER_RO(item)[0];
6767
if (tt==NA_INTEGER) continue; // singleton NA can be skipped
6868
finalVal = tt;
6969
break; // stop early on the first singleton that is not NA; minimizes deepest loop body below
@@ -108,7 +108,7 @@ SEXP coalesce(SEXP x, SEXP inplaceArg, SEXP nan_is_na_arg) {
108108
for (int j=0; j<nval; ++j) {
109109
SEXP item = VECTOR_ELT(x, j+off);
110110
if (length(item)==1) {
111-
double tt = REAL(item)[0];
111+
double tt = REAL_RO(item)[0];
112112
if (ISNAN(tt)) continue;
113113
finalVal = tt;
114114
break;
@@ -127,7 +127,7 @@ SEXP coalesce(SEXP x, SEXP inplaceArg, SEXP nan_is_na_arg) {
127127
for (int j=0; j<nval; ++j) {
128128
SEXP item = VECTOR_ELT(x, j+off);
129129
if (length(item)==1) {
130-
double tt = REAL(item)[0];
130+
double tt = REAL_RO(item)[0];
131131
if (ISNA(tt)) continue;
132132
finalVal = tt;
133133
break;

0 commit comments

Comments
 (0)