Skip to content

Commit

Permalink
Fix the handling of hash collisions
Browse files Browse the repository at this point in the history
The code was generating probes but not using it.
Also, fix rounding issue when computing the mean temperature value
  • Loading branch information
abhinav-upadhyay committed Feb 1, 2024
1 parent 833ff01 commit 3429050
Showing 1 changed file with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ private static class Table {
private static final int TABLE_SIZE = 1 << 21; // 0x8000; // collisions with table smaller than this. Need a better hash function
private static final int TABLE_MASK = TABLE_SIZE - 1;
Row[] table = new Row[TABLE_SIZE];
byte[] array = new byte[256];
byte[] array = new byte[512];

public void put(long cityStartOffset, long cityLength, long nameHash, int temperature) {
final long uhash = nameHash ^ (nameHash >> 29);
final long uhash = nameHash;
int index = (int) uhash & TABLE_MASK;
Row row = table[index];
if (row == null) {
Expand All @@ -76,19 +76,21 @@ public void put(long cityStartOffset, long cityLength, long nameHash, int temper
}

while (row.hash != uhash) {
index = (int) ((index + (uhash)) & TABLE_MASK);
int i = 0;
for (; i < cityLength - 1;) {
array[i++] = UNSAFE.getByte(cityStartOffset++);
array[i++] = UNSAFE.getByte(cityStartOffset++);
}
for (; i < cityLength; i++) {
array[i] = UNSAFE.getByte(cityStartOffset++);
index = (int) ((index + (uhash | 1)) & TABLE_MASK);
row = table[index];
if (row == null) {
int i = 0;
for (; i < cityLength - 1;) {
array[i++] = UNSAFE.getByte(cityStartOffset++);
array[i++] = UNSAFE.getByte(cityStartOffset++);
}
for (; i < cityLength; i++) {
array[i] = UNSAFE.getByte(cityStartOffset++);
}
table[index] = new Row(new String(array, 0, i), temperature, temperature, 1, temperature, uhash);
return;
}
table[index] = new Row(new String(array, 0, i), temperature, temperature, 1, temperature, uhash);
return;
}

row.update(temperature);

}
Expand Down Expand Up @@ -124,9 +126,13 @@ void update(int temperature) {
}
}

private static double round(double value) {
return Math.round(value * 10.0) / 10.0;
}

@Override
public String toString() {
return String.format("%.1f/%.1f/%.1f", this.minTemp / 10.0, this.sum / (count * 10.0), maxTemp / 10.0);
return String.format("%.1f/%.1f/%.1f", this.minTemp / 10.0, round((((double) sum) / 10.0) / count), maxTemp / 10.0);
}

public Row update(Row value) {
Expand Down Expand Up @@ -186,13 +192,13 @@ private static Table readFile(long startAddress, long endAddress) {
}
int trailingZeros = Long.numberOfTrailingZeros(hasSemi);
int semiColonIndex = trailingZeros >> 3;
if (trailingZeros >= 8) {
int nonZeroBits = 64 - trailingZeros;
nameHash ^= ((word << nonZeroBits) >> nonZeroBits);
for (int i = 0; i < semiColonIndex; i++) {
byte b = (byte) ((word >> (i * 8)) & 0xff);
nameHash = nameHash * 31 + b;
nameHash += (nameHash << 10);
nameHash ^= (nameHash >> 6);
currentOffset += semiColonIndex;
}
currentOffset += semiColonIndex;
long cityLength = currentOffset - cityStart;
currentOffset++; // skip ;
int temperature = 0;
Expand Down

0 comments on commit 3429050

Please sign in to comment.