Skip to content

Commit ed19fc4

Browse files
Jelmer Kuperusjelmerk
authored andcommitted
fix for issue 74
1 parent 8865dd5 commit ed19fc4

File tree

2 files changed

+4
-122
lines changed

2 files changed

+4
-122
lines changed

hnswlib-core/src/main/java/com/github/jelmerk/hnswlib/core/hnsw/HnswIndex.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
1818
import java.util.*;
19+
import java.util.concurrent.ThreadLocalRandom;
1920
import java.util.concurrent.atomic.AtomicReferenceArray;
2021
import java.util.concurrent.locks.*;
2122

@@ -212,7 +213,7 @@ public boolean add(TItem item) {
212213
throw new IllegalArgumentException("Item does not have dimensionality of : " + dimensions);
213214
}
214215

215-
int randomLevel = assignLevel(item.id(), this.levelLambda);
216+
int randomLevel = assignLevel(this.levelLambda);
216217

217218
IntArrayList[] connections = new IntArrayList[randomLevel + 1];
218219

@@ -1120,23 +1121,8 @@ public static <TVector, TDistance> Builder<TVector, TDistance> newBuilder(
11201121
return new Builder<>(false, dimensions, distanceFunction, distanceComparator, maxItemCount);
11211122
}
11221123

1123-
private int assignLevel(TId value, double lambda) {
1124-
1125-
// by relying on the external id to come up with the level, the graph construction should be a lot mor stable
1126-
// see : https://github.com/nmslib/hnswlib/issues/28
1127-
1128-
int hashCode = value.hashCode();
1129-
1130-
byte[] bytes = new byte[]{
1131-
(byte) (hashCode >> 24),
1132-
(byte) (hashCode >> 16),
1133-
(byte) (hashCode >> 8),
1134-
(byte) hashCode
1135-
};
1136-
1137-
double random = Math.abs((double) Murmur3.hash32(bytes) / (double) Integer.MAX_VALUE);
1138-
1139-
double r = -Math.log(random) * lambda;
1124+
public int assignLevel(double lambda) {
1125+
double r = -Math.log(ThreadLocalRandom.current().nextDouble()) * lambda;
11401126
return (int) r;
11411127
}
11421128

hnswlib-core/src/main/java/com/github/jelmerk/hnswlib/core/util/Murmur3.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)