Skip to content

Commit 1f50aaf

Browse files
committed
whitespace, and unrelated to this PR localized more i,j in loops (to protect from i,j being used accidentally outside those loops, not really for speed reasons)
1 parent 02180c0 commit 1f50aaf

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/fmelt.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,22 @@ static SEXP unlist_(SEXP xint) {
139139
}
140140

141141
bool invalid_measure(int i, int ncol) {
142-
return i != NA_INTEGER && (i <= 0 || i > ncol);
142+
return (i<=0 && i!=NA_INTEGER) || i>ncol;
143143
}
144144

145145
SEXP checkVars(SEXP DT, SEXP id, SEXP measure, Rboolean verbose) {
146-
int i, ncol=LENGTH(DT), targetcols=0, protecti=0, u=0, v=0;
146+
int ncol=LENGTH(DT), targetcols=0, protecti=0, u=0, v=0;
147147
SEXP thiscol, idcols = R_NilValue, valuecols = R_NilValue, tmp, tmp2, booltmp, unqtmp, ans;
148148
SEXP dtnames = PROTECT(getAttrib(DT, R_NamesSymbol)); protecti++;
149149

150150
if (isNull(id) && isNull(measure)) {
151-
for (i=0; i<ncol; i++) {
151+
for (int i=0; i<ncol; ++i) {
152152
thiscol = VECTOR_ELT(DT, i);
153153
if ((isInteger(thiscol) || isNumeric(thiscol) || isLogical(thiscol)) && !isFactor(thiscol)) targetcols++;
154154
}
155155
idcols = PROTECT(allocVector(INTSXP, ncol-targetcols)); protecti++;
156156
tmp = PROTECT(allocVector(INTSXP, targetcols)); protecti++;
157-
for (i=0; i<ncol; i++) {
157+
for (int i=0; i<ncol; ++i) {
158158
thiscol = VECTOR_ELT(DT, i);
159159
if ((isInteger(thiscol) || isNumeric(thiscol) || isLogical(thiscol)) && !isFactor(thiscol)) {
160160
INTEGER(tmp)[u++] = i+1;
@@ -172,15 +172,15 @@ SEXP checkVars(SEXP DT, SEXP id, SEXP measure, Rboolean verbose) {
172172
default : error(_("Unknown 'id.vars' type %s, must be character or integer vector"), type2char(TYPEOF(id)));
173173
}
174174
booltmp = PROTECT(duplicated(tmp, FALSE)); protecti++;
175-
for (i=0; i<length(tmp); i++) {
175+
for (int i=0; i<length(tmp); ++i) {
176176
if (INTEGER(tmp)[i] <= 0 || INTEGER(tmp)[i] > ncol)
177177
error(_("One or more values in 'id.vars' is invalid."));
178178
else if (!LOGICAL(booltmp)[i]) targetcols++;
179179
else continue;
180180
}
181181
unqtmp = PROTECT(allocVector(INTSXP, targetcols)); protecti++;
182182
u = 0;
183-
for (i=0; i<length(booltmp); i++) {
183+
for (int i=0; i<length(booltmp); ++i) {
184184
if (!LOGICAL(booltmp)[i]) {
185185
INTEGER(unqtmp)[u++] = INTEGER(tmp)[i];
186186
}
@@ -206,15 +206,15 @@ SEXP checkVars(SEXP DT, SEXP id, SEXP measure, Rboolean verbose) {
206206
tmp = PROTECT(unlist_(tmp2)); protecti++;
207207
}
208208
booltmp = PROTECT(duplicated(tmp, FALSE)); protecti++;
209-
for (i=0; i<length(tmp); i++) {
209+
for (int i=0; i<length(tmp); ++i) {
210210
if (invalid_measure(INTEGER(tmp)[i], ncol))
211211
error(_("One or more values in 'measure.vars' is invalid."));
212212
else if (!LOGICAL(booltmp)[i]) targetcols++;
213213
else continue;
214214
}
215215
unqtmp = PROTECT(allocVector(INTSXP, targetcols)); protecti++;
216216
u = 0;
217-
for (i=0; i<length(booltmp); i++) {
217+
for (int i=0; i<length(booltmp); ++i) {
218218
if (!LOGICAL(booltmp)[i]) {
219219
INTEGER(unqtmp)[u++] = INTEGER(tmp)[i];
220220
}
@@ -236,7 +236,7 @@ SEXP checkVars(SEXP DT, SEXP id, SEXP measure, Rboolean verbose) {
236236
case INTSXP : tmp = id; break;
237237
default : error(_("Unknown 'id.vars' type %s, must be character or integer vector"), type2char(TYPEOF(id)));
238238
}
239-
for (i=0; i<length(tmp); i++) {
239+
for (int i=0; i<length(tmp); ++i) {
240240
if (INTEGER(tmp)[i] <= 0 || INTEGER(tmp)[i] > ncol)
241241
error(_("One or more values in 'id.vars' is invalid."));
242242
}
@@ -252,7 +252,7 @@ SEXP checkVars(SEXP DT, SEXP id, SEXP measure, Rboolean verbose) {
252252
if (isNewList(measure)) {
253253
tmp = PROTECT(unlist_(tmp2)); protecti++;
254254
}
255-
for (i=0; i<length(tmp); i++) {
255+
for (int i=0; i<length(tmp); ++i) {
256256
if (invalid_measure(INTEGER(tmp)[i], ncol))
257257
error(_("One or more values in 'measure.vars' is invalid."));
258258
}
@@ -270,28 +270,26 @@ SEXP checkVars(SEXP DT, SEXP id, SEXP measure, Rboolean verbose) {
270270
}
271271

272272
struct processData {
273-
SEXP RCHK; // a 2 item list holding vars (result of checkVars) and naidx. PROTECTed up in fmelt so that preprocess() doesn't need to PROTECT. To pass rchk, #2865
274-
SEXP idcols,
275-
valuecols, // list with one element per output/value column, each
276-
// element is an integer vector.
277-
naidx; // convenience pointers into RCHK[0][0], RCHK[0][1] and RCHK[1] respectively
273+
SEXP RCHK; // a 2 item list holding vars (result of checkVars) and naidx. PROTECTed up in fmelt so that preprocess() doesn't need to PROTECT. To pass rchk, #2865
274+
SEXP idcols, // convenience pointers into RCHK[0][0], RCHK[0][1] and RCHK[1] respectively
275+
valuecols, // list with one element per output/value column, each element is an integer vector.
276+
naidx;
278277
int *isfactor,
279-
*leach, // length of each element of the valuecols(measure.vars) list.
278+
*leach, // length of each element of the valuecols(measure.vars) list.
280279
*isidentical; // are all inputs for this value column the same type?
281-
int lids, // number of id columns.
282-
lvalues, // number of value columns.
283-
lmax, //max length of valuecols elements / number of times to repeat ids.
284-
totlen, // of output/long DT result of melt operation.
285-
nrow; // of input/wide DT to be melted.
280+
int lids, // number of id columns.
281+
lvalues, // number of value columns.
282+
lmax, // max length of valuecols elements / number of times to repeat ids.
283+
totlen, // of output/long DT result of melt operation.
284+
nrow; // of input/wide DT to be melted.
286285
SEXPTYPE *maxtype;
287-
Rboolean narm; // remove missing values?
286+
Rboolean narm; // remove missing values?
288287
};
289288

290289
static void preprocess(SEXP DT, SEXP id, SEXP measure, SEXP varnames, SEXP valnames, Rboolean narm, Rboolean verbose, struct processData *data) {
291290

292291
SEXP vars,tmp,thiscol;
293292
SEXPTYPE type;
294-
int i,j;
295293
data->lmax = 0; data->totlen = 0; data->nrow = length(VECTOR_ELT(DT, 0));
296294
SET_VECTOR_ELT(data->RCHK, 0, vars = checkVars(DT, id, measure, verbose));
297295
data->idcols = VECTOR_ELT(vars, 0);
@@ -310,14 +308,14 @@ static void preprocess(SEXP DT, SEXP id, SEXP measure, SEXP varnames, SEXP valna
310308
data->isfactor = (int *)R_alloc(data->lvalues, sizeof(int));
311309
data->maxtype = (SEXPTYPE *)R_alloc(data->lvalues, sizeof(SEXPTYPE));
312310
// first find max type of each output column.
313-
for (i=0; i<data->lvalues; i++) { // for each output column.
311+
for (int i=0; i<data->lvalues; ++i) { // for each output column.
314312
tmp = VECTOR_ELT(data->valuecols, i);
315313
data->leach[i] = length(tmp);
316314
data->isidentical[i] = 1; // TODO - why 1 and not Rboolean TRUE?
317315
data->isfactor[i] = 0; // seems to hold 2 below, so not an Rboolean FALSE here. TODO - better name for variable?
318316
data->maxtype[i] = 0; // R_alloc doesn't initialize so careful to here, relied on below
319317
data->lmax = (data->lmax > data->leach[i]) ? data->lmax : data->leach[i];
320-
for (j=0; j<data->leach[i]; j++) { // for each input column.
318+
for (int j=0; j<data->leach[i]; ++j) { // for each input column.
321319
int this_col_num = INTEGER(tmp)[j];
322320
if(this_col_num != NA_INTEGER){
323321
thiscol = VECTOR_ELT(DT, this_col_num-1);
@@ -330,7 +328,7 @@ static void preprocess(SEXP DT, SEXP id, SEXP measure, SEXP varnames, SEXP valna
330328
}
331329
}
332330
}
333-
for (j=0; j<data->leach[i]; j++) {
331+
for (int j=0; j<data->leach[i]; ++j) {
334332
int this_col_num = INTEGER(tmp)[j];
335333
if(this_col_num != NA_INTEGER){
336334
thiscol = VECTOR_ELT(DT, this_col_num-1);

0 commit comments

Comments
 (0)