Skip to content

Commit c0e03ed

Browse files
delete multiple output column else branch
1 parent ed1712e commit c0e03ed

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

src/fmelt.c

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -634,39 +634,27 @@ SEXP getvarcols(SEXP DT, SEXP dtnames, Rboolean varfactor, Rboolean verbose, str
634634
for (int k=0; k<thislen; ++k) td[ansloc++] = j+1;
635635
}
636636
} else { // non-list measure.vars keeps legacy name-based levels
637-
if (data->lvalues == 1) {
638-
SEXP thisvaluecols = VECTOR_ELT(data->valuecols, 0);
639-
int len = length(thisvaluecols);
640-
levels = PROTECT(allocVector(STRSXP, len)); protecti++;
641-
const int *vd = INTEGER(thisvaluecols);
642-
for (int j=0; j<len; ++j) SET_STRING_ELT(levels, j, STRING_ELT(dtnames, vd[j]-1));
643-
SEXP m = PROTECT(chmatch(levels, levels, 0)); protecti++; // do we have any dups?
644-
int numRemove = 0; // remove dups and any for which narm and all-NA
645-
int *md = INTEGER(m);
646-
for (int j=0; j<len; ++j) {
647-
if (md[j]!=j+1 /*dup*/ || (data->narm && length(VECTOR_ELT(data->not_NA_indices, j))==0)) { numRemove++; md[j]=0; }
648-
}
649-
if (numRemove) {
650-
SEXP newlevels = PROTECT(allocVector(STRSXP, len-numRemove)); protecti++;
651-
for (int i=0, loc=0; i<len; ++i) if (md[i]!=0) { SET_STRING_ELT(newlevels, loc++, STRING_ELT(levels, i)); }
652-
m = PROTECT(chmatch(levels, newlevels, 0)); protecti++; // budge up the gaps
653-
md = INTEGER(m);
654-
levels = newlevels;
655-
}
656-
for (int j=0, ansloc=0; j<data->lmax; ++j) {
657-
const int thislen = data->narm ? length(VECTOR_ELT(data->not_NA_indices, j)) : data->nrow;
658-
for (int k=0; k<thislen; ++k) td[ansloc++] = md[j];
659-
}
660-
} else {//multiple output columns.
661-
int nlevel=0;
662-
levels = PROTECT(allocVector(STRSXP, data->lmax)); protecti++;
663-
for (int j=0, ansloc=0; j<data->lmax; ++j) {
664-
const int thislen = data->narm ? length(VECTOR_ELT(data->not_NA_indices, j)) : data->nrow;
665-
char buff[20];
666-
snprintf(buff, sizeof(buff), "%d", nlevel + 1); // # notranslate
667-
SET_STRING_ELT(levels, nlevel++, mkChar(buff)); // generate levels = 1:nlevels
668-
for (int k=0; k<thislen; ++k) td[ansloc++] = nlevel;
669-
}
637+
SEXP thisvaluecols = VECTOR_ELT(data->valuecols, 0);
638+
int len = length(thisvaluecols);
639+
levels = PROTECT(allocVector(STRSXP, len)); protecti++;
640+
const int *vd = INTEGER(thisvaluecols);
641+
for (int j=0; j<len; ++j) SET_STRING_ELT(levels, j, STRING_ELT(dtnames, vd[j]-1));
642+
SEXP m = PROTECT(chmatch(levels, levels, 0)); protecti++; // do we have any dups?
643+
int numRemove = 0; // remove dups and any for which narm and all-NA
644+
int *md = INTEGER(m);
645+
for (int j=0; j<len; ++j) {
646+
if (md[j]!=j+1 /*dup*/ || (data->narm && length(VECTOR_ELT(data->not_NA_indices, j))==0)) { numRemove++; md[j]=0; }
647+
}
648+
if (numRemove) {
649+
SEXP newlevels = PROTECT(allocVector(STRSXP, len-numRemove)); protecti++;
650+
for (int i=0, loc=0; i<len; ++i) if (md[i]!=0) { SET_STRING_ELT(newlevels, loc++, STRING_ELT(levels, i)); }
651+
m = PROTECT(chmatch(levels, newlevels, 0)); protecti++; // budge up the gaps
652+
md = INTEGER(m);
653+
levels = newlevels;
654+
}
655+
for (int j=0, ansloc=0; j<data->lmax; ++j) {
656+
const int thislen = data->narm ? length(VECTOR_ELT(data->not_NA_indices, j)) : data->nrow;
657+
for (int k=0; k<thislen; ++k) td[ansloc++] = md[j];
670658
}
671659
}
672660
setAttrib(target, R_LevelsSymbol, levels);

0 commit comments

Comments
 (0)