Skip to content

Commit 9920cbc

Browse files
authored
Use read-only accessors and const when possible (#7368)
* Changed many function calls to use read only variants when appropriate * implemented improvements as requested * minor update
1 parent 8d0faee commit 9920cbc

File tree

8 files changed

+35
-35
lines changed

8 files changed

+35
-35
lines changed

src/fastmean.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ SEXP fastmean(SEXP args)
3737
tmp = CADDR(args);
3838
if (!isLogical(tmp) || LENGTH(tmp)!=1 || LOGICAL(tmp)[0]==NA_LOGICAL)
3939
error(_("%s should be TRUE or FALSE"), "narm"); // # nocov ; [.data.table should construct the .External call correctly
40-
narm=LOGICAL(tmp)[0];
40+
narm=LOGICAL_RO(tmp)[0];
4141
}
4242
PROTECT(ans = allocNAVector(REALSXP, 1));
4343
copyMostAttrib(x, ans);
@@ -50,8 +50,8 @@ SEXP fastmean(SEXP args)
5050
case LGLSXP:
5151
case INTSXP:
5252
for (int i=0; i<l; ++i) {
53-
if(INTEGER(x)[i] == NA_INTEGER) continue;
54-
s += INTEGER(x)[i]; // no under/overflow here, s is long double not integer
53+
if(INTEGER_RO(x)[i] == NA_INTEGER) continue;
54+
s += INTEGER_RO(x)[i]; // no under/overflow here, s is long double not integer
5555
n++;
5656
}
5757
if (n>0)
@@ -61,8 +61,8 @@ SEXP fastmean(SEXP args)
6161
break;
6262
case REALSXP:
6363
for (int i=0; i<l; ++i) {
64-
if(ISNAN(REAL(x)[i])) continue; // TO DO: could drop this line and let NA propagate?
65-
s += REAL(x)[i];
64+
if(ISNAN(REAL_RO(x)[i])) continue; // TO DO: could drop this line and let NA propagate?
65+
s += REAL_RO(x)[i];
6666
n++;
6767
}
6868
if (n==0) {
@@ -72,8 +72,8 @@ SEXP fastmean(SEXP args)
7272
s /= n;
7373
if(R_FINITE((double)s)) {
7474
for (int i=0; i<l; ++i) {
75-
if(ISNAN(REAL(x)[i])) continue;
76-
t += (REAL(x)[i] - s);
75+
if(ISNAN(REAL_RO(x)[i])) continue;
76+
t += (REAL_RO(x)[i] - s);
7777
}
7878
s += t/n;
7979
}
@@ -87,21 +87,21 @@ SEXP fastmean(SEXP args)
8787
case LGLSXP:
8888
case INTSXP:
8989
for (int i=0; i<l; ++i) {
90-
if(INTEGER(x)[i] == NA_INTEGER) {UNPROTECT(1); return(ans);}
91-
s += INTEGER(x)[i];
90+
if(INTEGER_RO(x)[i] == NA_INTEGER) {UNPROTECT(1); return(ans);}
91+
s += INTEGER_RO(x)[i];
9292
}
9393
REAL(ans)[0] = (double) (s/l);
9494
break;
9595
case REALSXP:
9696
for (int i=0; i<l; ++i) {
97-
if(ISNAN(REAL(x)[i])) {UNPROTECT(1); return(ans);}
98-
s += REAL(x)[i];
97+
if(ISNAN(REAL_RO(x)[i])) {UNPROTECT(1); return(ans);}
98+
s += REAL_RO(x)[i];
9999
}
100100
s /= l;
101101
if(R_FINITE((double)s)) {
102102
for (int i=0; i<l; ++i) {
103103
// no NA if got this far
104-
t += (REAL(x)[i] - s);
104+
t += (REAL_RO(x)[i] - s);
105105
}
106106
s += t/LENGTH(x);
107107
}

src/fwrite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void writePOSIXct(const void *col, int64_t row, char **pch)
437437
int m = ((x - xi) * 10000000); // 7th digit used to round up if 9
438438
m += (m % 10); // 9 is numerical accuracy, 8 or less then we truncate to last microsecond
439439
m /= 10;
440-
int carry = m / 1000000; // Need to know if we rounded up to a whole second
440+
const int carry = m / 1000000; // Need to know if we rounded up to a whole second
441441
m -= carry * 1000000;
442442
xi += carry;
443443
if (xi >= 0) {

src/inrange.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
SEXP inrange(SEXP ansArg, SEXP xoArg, SEXP startsArg, SEXP lenArg)
55
{
66
int *ans = INTEGER(ansArg);
7-
const int *xo = INTEGER(xoArg);
8-
const int *starts = INTEGER(startsArg), *len = INTEGER(lenArg);
7+
const int *xo = INTEGER_RO(xoArg);
8+
const int *starts = INTEGER_RO(startsArg), *len = INTEGER_RO(lenArg);
99
const int n = length(startsArg), nxo = length(xoArg);
1010
for (int i = 0; i < n; i++) {
1111
for (int j = starts[i] - 1; j < starts[i] - 1 + len[i]; j++) {

src/programming.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static void substitute_call_arg_names(SEXP expr, SEXP env)
77
SEXP arg_names = getAttrib(expr, R_NamesSymbol);
88
if (!isNull(arg_names)) {
99
SEXP env_names = getAttrib(env, R_NamesSymbol);
10-
int *imatches = INTEGER(PROTECT(chmatch(arg_names, env_names, 0)));
10+
const int *imatches = INTEGER_RO(PROTECT(chmatch(arg_names, env_names, 0)));
1111
const SEXP *env_sub = SEXPPTR_RO(env);
1212
SEXP tmp = expr;
1313
for (int i = 0; i < length(arg_names); i++, tmp = CDR(tmp)) { // substitute call arg names

src/reorder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ SEXP reorder(SEXP x, SEXP order)
4242
int nprotect = 0;
4343
if (ALTREP(order)) { order=PROTECT(copyAsPlain(order)); nprotect++; } // TODO: if it's an ALTREP sequence some optimizations are possible rather than expand
4444

45-
const int *restrict idx = INTEGER(order);
45+
const int *restrict idx = INTEGER_RO(order);
4646
int i=0;
4747
while (i<nrow && idx[i] == i+1) ++i;
4848
const int start=i;

src/subset.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ void subsetVectorRaw(SEXP ans, SEXP source, SEXP idx, const bool anyNA)
4949

5050
switch(TYPEOF(source)) {
5151
case INTSXP: case LGLSXP: {
52-
int *sp = INTEGER(source);
52+
const int *sp = INTEGER_RO(source);
5353
int *ap = INTEGER(ans);
5454
PARLOOP(NA_INTEGER)
5555
} break;
5656
case REALSXP : {
5757
if (INHERITS(source, char_integer64)) {
58-
int64_t *sp = (int64_t *)REAL(source);
58+
const int64_t *sp = (int64_t *)REAL_RO(source);
5959
int64_t *ap = (int64_t *)REAL(ans);
6060
PARLOOP(INT64_MIN)
6161
} else {
62-
double *sp = REAL(source);
62+
const double *sp = REAL_RO(source);
6363
double *ap = REAL(ans);
6464
PARLOOP(NA_REAL)
6565
}
@@ -88,12 +88,12 @@ void subsetVectorRaw(SEXP ans, SEXP source, SEXP idx, const bool anyNA)
8888
}
8989
} break;
9090
case CPLXSXP : {
91-
Rcomplex *sp = COMPLEX(source);
91+
const Rcomplex *sp = COMPLEX_RO(source);
9292
Rcomplex *ap = COMPLEX(ans);
9393
PARLOOP(NA_CPLX)
9494
} break;
9595
case RAWSXP : {
96-
Rbyte *sp = RAW(source);
96+
const Rbyte *sp = RAW_RO(source);
9797
Rbyte *ap = RAW(ans);
9898
PARLOOP(0)
9999
} break;

src/transpose.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP listCo
99
error(_("l must be a list."));
1010
if (!length(l))
1111
return(copyAsPlain(l));
12-
if (!isLogical(ignoreArg) || LOGICAL(ignoreArg)[0] == NA_LOGICAL)
12+
if (!isLogical(ignoreArg) || LOGICAL_RO(ignoreArg)[0] == NA_LOGICAL)
1313
error(_("ignore.empty should be logical TRUE/FALSE."));
14-
bool ignore = LOGICAL(ignoreArg)[0];
14+
const bool ignore = LOGICAL_RO(ignoreArg)[0];
1515
if (!(isNull(keepNamesArg) || (isString(keepNamesArg) && LENGTH(keepNamesArg) == 1)))
1616
error(_("keep.names should be either NULL, or the name of the first column of the result in which to place the names of the input"));
17-
bool rn = !isNull(keepNamesArg);
17+
const bool rn = !isNull(keepNamesArg);
1818
if (length(fill) != 1)
1919
error(_("fill must be a length 1 vector, such as the default NA"));
20-
R_len_t ln = LENGTH(l);
20+
const R_len_t ln = LENGTH(l);
2121
if (!IS_TRUE_OR_FALSE(listColsArg))
2222
error(_("list.cols should be logical TRUE/FALSE."));
23-
bool listCol = LOGICAL(listColsArg)[0];
23+
const bool listCol = LOGICAL_RO(listColsArg)[0];
2424

2525
// preprocessing
2626
int maxlen = 0, zerolen = 0;
@@ -39,7 +39,7 @@ SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP listCo
3939
if (listCol) maxtype = VECSXP; // need to keep preprocessing for zerolen
4040
fill = PROTECT(coerceVector(fill, maxtype)); nprotect++;
4141
SEXP ans = PROTECT(allocVector(VECSXP, maxlen + rn)); nprotect++;
42-
int anslen = (ignore) ? (ln - zerolen) : ln;
42+
const int anslen = (ignore) ? (ln - zerolen) : ln;
4343
if (rn) {
4444
SEXP tt;
4545
SET_VECTOR_ELT(ans, 0, tt = allocVector(STRSXP, anslen));
@@ -61,22 +61,22 @@ SEXP transpose(SEXP l, SEXP fill, SEXP ignoreArg, SEXP keepNamesArg, SEXP listCo
6161
} else PROTECT(li); // extra PROTECT just to help rchk by avoiding two counter variables
6262
switch (maxtype) {
6363
case LGLSXP: {
64-
const int *ili = LOGICAL(li);
65-
const int ifill = LOGICAL(fill)[0];
64+
const int *ili = LOGICAL_RO(li);
65+
const int ifill = LOGICAL_RO(fill)[0];
6666
for (int j = 0; j < maxlen; j++) {
6767
LOGICAL(ansp[j + rn])[k] = j < len ? ili[j] : ifill;
6868
}
6969
} break;
7070
case INTSXP: {
71-
const int *ili = INTEGER(li);
72-
const int ifill = INTEGER(fill)[0];
71+
const int *ili = INTEGER_RO(li);
72+
const int ifill = INTEGER_RO(fill)[0];
7373
for (int j = 0; j < maxlen; j++) {
7474
INTEGER(ansp[j + rn])[k] = j < len ? ili[j] : ifill;
7575
}
7676
} break;
7777
case REALSXP: {
78-
const double *dli = REAL(li);
79-
const double dfill = REAL(fill)[0];
78+
const double *dli = REAL_RO(li);
79+
const double dfill = REAL_RO(fill)[0];
8080
for (int j = 0; j < maxlen; j++) {
8181
REAL(ansp[j + rn])[k] = j < len ? dli[j] : dfill;
8282
}

src/types.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ SEXP testMsgR(SEXP status, SEXP x, SEXP k) {
7373
internal_error(__func__, "status, nx, nk must be integer"); // # nocov
7474
int protecti = 0;
7575
const bool verbose = GetVerbose();
76-
int istatus = INTEGER(status)[0], nx = INTEGER(x)[0], nk = INTEGER(k)[0];
76+
int istatus = INTEGER_RO(status)[0], nx = INTEGER_RO(x)[0], nk = INTEGER_RO(k)[0];
7777

7878
// TODO below chunk into allocAnsList helper - not easy for variable length of inner vectors
7979
SEXP ans = PROTECT(allocVector(VECSXP, nk * nx)); protecti++;

0 commit comments

Comments
 (0)