Skip to content

Commit e88a52d

Browse files
committed
Refactor median computation based on review feedback.
1 parent 29b2891 commit e88a52d

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

faiss/IndexLSH.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@ void IndexLSH::train(idx_t n, const float* x) {
8686

8787
for (idx_t i = 0; i < nbits; i++) {
8888
float* xi = transposed_x.get() + i * n;
89-
// Use nth_element (O(n)) instead of sort (O(n log n)) for median
90-
if (n % 2 == 1) {
91-
std::nth_element(xi, xi + n / 2, xi + n);
92-
thresholds[i] = xi[n / 2];
93-
} else {
94-
std::nth_element(xi, xi + n / 2, xi + n);
95-
float median_high = xi[n / 2];
89+
// Use nth_element (O(n)) instead of sort (O(n log n))
90+
std::nth_element(xi, xi + n / 2, xi + n);
91+
float median = xi[n / 2];
92+
if (n % 2 == 0) {
9693
std::nth_element(xi, xi + n / 2 - 1, xi + n);
97-
thresholds[i] = (xi[n / 2 - 1] + median_high) / 2;
94+
median = (median + xi[n / 2 - 1]) / 2;
9895
}
96+
thresholds[i] = median;
9997
}
10098
}
10199
is_trained = true;

0 commit comments

Comments
 (0)