Hello Thomas,
I checked again and you are correct the code is not broken, it just
iterates bit by bit twice on the first long and on the last long.
If you'd like to try below is the code that does not iterate bit by
bit at all. I did not test it with the H2 myself.
public int nextSetBit1(int fromIndex) {
int result = -1;
int dataIndex = fromIndex >> ADDRESS_BITS;
int bitIndex = fromIndex - (dataIndex << ADDRESS_BITS);
long l = data[dataIndex];
l = clearLowBits(l, bitIndex);
while(l==0) {
dataIndex++;
if (dataIndex >= data.length) break;
l = data[dataIndex];
}
if (l != 0)
result = (dataIndex << ADDRESS_BITS) + lowestSetBit(l);
return result;
}
private static int lowestSetBit(long data)
{
int result = 0;
data &= -data;
if ((data & 0xffffffff00000000L) != 0) result += 32;
if ((data & 0xffff0000ffff0000L) != 0) result += 16;
if ((data & 0xff00ff00ff00ff00L) != 0) result += 8;
if ((data & 0xf0f0f0f0f0f0f0f0L) != 0) result += 4;
if ((data & 0xccccccccccccccccL) != 0) result += 2;
if ((data & 0xaaaaaaaaaaaaaaaaL) != 0) result += 1;
return result;
}
private static long clearLowBits(long data, int cnt)
{
long result = data;
if (cnt > 0)
{
long mask = 1;
if (cnt > 1) mask = (0xAAAAAAAAAAAAAAAAL >>> (BITS-cnt)) |
(0xAAAAAAAAAAAAAAAAL
>>> (BITS-(cnt-1)));
result &= ~mask;
}
return result;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en
-~----------~----~----~----~------~----~------~--~---