[Rd] unique.default problem (PR#12551)

prokaj at cs.elte.hu prokaj at cs.elte.hu
Sat Aug 16 14:20:17 CEST 2008


Full_Name: Vilmos Prokaj
Version: R 2.7.1
OS: windows
Submission from: (NULL) (213.181.195.84)


Dear developers,

The following line of code (produced by a mistake) caused an infinite loop

unique("a",c("a","b"))

or also

unique(1,1:2)

I made   a little investigation, and it seems to be that the following function
from unique.c is looping infinitely


static int isDuplicated(SEXP x, int indx, HashData *d)
{
    int i, *h;

    h = INTEGER(d->HashTable);
    i = d->hash(x, indx, d);
    while (h[i] != NIL) {
	if (d->equal(x, h[i], x, indx))
	    return h[i] >= 0 ? 1 : 0;
		i = (i + 1) % d->M;
	    }
	    h[i] = indx;
    return 0;
}
In this case h contains only one negative value, which causes d->equal(=requal)
to return 0.
	
static int requal(SEXP x, int i, SEXP y, int j)
{
    if (i < 0 || j < 0) return 0;
    if (!ISNAN(REAL(x)[i]) && !ISNAN(REAL(y)[j]))
	return (REAL(x)[i] == REAL(y)[j]);
    else if (R_IsNA(REAL(x)[i]) && R_IsNA(REAL(y)[j])) return 1;
    else if (R_IsNaN(REAL(x)[i]) && R_IsNaN(REAL(y)[j])) return 1;
    else return 0;
}

I do not claim that the situation above is frequent or even meaningful, however
it should not cause a crash of R.

Sincerely yours
Vilmos Prokaj



More information about the R-devel mailing list