June 07, 2004 [Fix] Bad TypeInfo compare for int and uint | ||||
---|---|---|---|---|
| ||||
Last night I was experimenting with sort. It didn't take me long to notice that it wasn't sorting properly. It's a specific failure mode, whereby the array has effectively been cut in two and then pieced together the other way round.
Having looked at internal\qsort.d and finding that's not the problem, I then went into the TypeInfo stuff and found this:
int compare(void *p1, void *p2)
{
return *cast(int *)p1 - *cast(int *)p2;
}
No check for overflow. So indeed, sort has no reason to break the circle at int.min rather than any other value.
The fix is straightforward: do it in the same way as for longs.
For int:
int compare(void* p1, void* p2)
{
if (*cast(int*) p1 < *cast(int*) p2)
return -1;
else if (*cast(int*) p1 > *cast(int*) p2)
return 1;
return 0;
}
For uint:
int compare(void* p1, void* p2)
{
if (*cast(uint*) p1 < *cast(uint*) p2)
return -1;
else if (*cast(uint*) p1 > *cast(uint*) p2)
return 1;
return 0;
}
Stewart.
--
My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
|
Copyright © 1999-2021 by the D Language Foundation