|
16 | 16 | import java.nio.file.Files; |
17 | 17 | import java.nio.file.Path; |
18 | 18 | import java.util.*; |
| 19 | +import java.util.concurrent.ThreadLocalRandom; |
19 | 20 | import java.util.concurrent.atomic.AtomicReferenceArray; |
20 | 21 | import java.util.concurrent.locks.*; |
21 | 22 |
|
@@ -212,7 +213,7 @@ public boolean add(TItem item) { |
212 | 213 | throw new IllegalArgumentException("Item does not have dimensionality of : " + dimensions); |
213 | 214 | } |
214 | 215 |
|
215 | | - int randomLevel = assignLevel(item.id(), this.levelLambda); |
| 216 | + int randomLevel = assignLevel(this.levelLambda); |
216 | 217 |
|
217 | 218 | IntArrayList[] connections = new IntArrayList[randomLevel + 1]; |
218 | 219 |
|
@@ -1120,23 +1121,8 @@ public static <TVector, TDistance> Builder<TVector, TDistance> newBuilder( |
1120 | 1121 | return new Builder<>(false, dimensions, distanceFunction, distanceComparator, maxItemCount); |
1121 | 1122 | } |
1122 | 1123 |
|
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; |
1140 | 1126 | return (int) r; |
1141 | 1127 | } |
1142 | 1128 |
|
|
0 commit comments