Skip to content

Commit

Permalink
fix hash collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
JurenIvan committed Feb 1, 2024
1 parent 8bd4652 commit 9858f47
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private static long[] getSegments(int segmentCount) throws IOException {
for (int i = 1; i < segmentCount; i++) {
long chunkOffset = chunks[i - 1] + segmentSize;
raf.seek(chunkOffset);
raf.readLine();
while (raf.readByte() != '\n') {
}
chunks[i] = raf.getFilePointer();
}
chunks[segmentCount] = fileSize;
Expand All @@ -136,7 +137,7 @@ void put(int hash, byte[] key, int len, int temperature) {

int i = index;
while (hashTable[i] != null) {
if (keyIsEqual(key, hashTable[index].city, len)) { // handling hash collisions
if (keyIsEqual(key, hashTable[i].city, len)) { // handling hash collisions
hashTable[i].add(temperature);
return;
}
Expand All @@ -148,10 +149,12 @@ void put(int hash, byte[] key, int len, int temperature) {

var cityArr = new byte[len];
System.arraycopy(key, 0, cityArr, 0, len);
hashTable[index] = new Measurement(cityArr, hash, temperature, temperature, 1, temperature);
hashTable[i] = new Measurement(cityArr, hash, temperature, temperature, 1, temperature);
}

private boolean keyIsEqual(byte[] one, byte[] other, int len) {
if (len != other.length)
return false;
for (int i = 0; i < len; i++) {
if (one[i] != other[i]) {
return false;
Expand Down

0 comments on commit 9858f47

Please sign in to comment.