1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/// Fast O(n log n) implementation of the Spread (Shamos) estimator.
/// Based on Monahan's selection algorithm adapted for pairwise differences.
///
/// Internal implementation - not part of public API.
use rand::Rng;
pub(crate) fn fast_spread(values: &[f64]) -> Result<f64, &'static str> {
let n = values.len();
if n <= 1 {
return Ok(0.0);
}
if n == 2 {
return Ok((values[1] - values[0]).abs());
}
// Sort the values
let mut a = values.to_vec();
a.sort_by(|x, y| x.partial_cmp(y).unwrap());
// Total number of pairwise differences with i < j
let total_pairs = (n * (n - 1)) / 2;
let k_low = total_pairs.div_ceil(2); // 1-based rank of lower middle
let k_high = (total_pairs + 2) / 2; // 1-based rank of upper middle
// Per-row active bounds over columns j (0-based indices)
// Row i allows j in [i+1, n-1] initially
let mut left_bounds: Vec<usize> = (0..n).map(|i| (i + 1).min(n)).collect();
let mut right_bounds = vec![n - 1; n];
for i in 0..n {
if left_bounds[i] > right_bounds[i] {
left_bounds[i] = 1;
right_bounds[i] = 0; // mark empty
}
}
let mut row_counts = vec![0; n];
// Initial pivot: a central gap
let mut pivot = a[n / 2] - a[(n - 1) / 2];
let mut prev_count_below = -1i64;
let mut rng = rand::thread_rng();
loop {
// === PARTITION: count how many differences are < pivot ===
let mut count_below = 0;
let mut largest_below = f64::NEG_INFINITY;
let mut smallest_at_or_above = f64::INFINITY;
let mut j = 1; // global two-pointer (non-decreasing across rows)
for i in 0..n - 1 {
if j < i + 1 {
j = i + 1;
}
while j < n && a[j] - a[i] < pivot {
j += 1;
}
let cnt_row = j.saturating_sub(i + 1);
row_counts[i] = cnt_row;
count_below += cnt_row;
// boundary elements for this row
if cnt_row > 0 {
let cand_below = a[j - 1] - a[i];
largest_below = largest_below.max(cand_below);
}
if j < n {
let cand_at_or_above = a[j] - a[i];
smallest_at_or_above = smallest_at_or_above.min(cand_at_or_above);
}
}
// === TARGET CHECK ===
let at_target = count_below == k_low || count_below == k_high - 1;
if at_target {
if k_low < k_high {
// Even N: average the two central order stats
return Ok(0.5 * (largest_below + smallest_at_or_above));
} else {
// Odd N: pick the single middle
let need_largest = count_below == k_low;
return Ok(if need_largest {
largest_below
} else {
smallest_at_or_above
});
}
}
// === STALL HANDLING ===
if count_below as i64 == prev_count_below {
let mut min_active = f64::INFINITY;
let mut max_active = f64::NEG_INFINITY;
let mut active = 0;
for i in 0..n - 1 {
let li = left_bounds[i];
let ri = right_bounds[i];
if li > ri {
continue;
}
let row_min = a[li] - a[i];
let row_max = a[ri] - a[i];
min_active = min_active.min(row_min);
max_active = max_active.max(row_max);
active += ri - li + 1;
}
if active == 0 {
if k_low < k_high {
return Ok(0.5 * (largest_below + smallest_at_or_above));
}
return Ok(if count_below >= k_low {
largest_below
} else {
smallest_at_or_above
});
}
if max_active <= min_active {
return Ok(min_active);
}
let mid = 0.5 * (min_active + max_active);
pivot = if mid > min_active && mid <= max_active {
mid
} else {
max_active
};
prev_count_below = count_below as i64;
continue;
}
// === SHRINK ACTIVE WINDOW ===
if count_below < k_low {
// Need larger differences: discard all strictly below pivot
for i in 0..n - 1 {
let new_left = i + 1 + row_counts[i];
if new_left > left_bounds[i] {
left_bounds[i] = new_left;
}
if left_bounds[i] > right_bounds[i] {
left_bounds[i] = 1;
right_bounds[i] = 0;
}
}
} else {
// Too many below: keep only those strictly below pivot
for i in 0..n - 1 {
let new_right = i + row_counts[i];
if new_right < right_bounds[i] {
right_bounds[i] = new_right;
}
if right_bounds[i] < i + 1 {
left_bounds[i] = 1;
right_bounds[i] = 0;
}
}
}
prev_count_below = count_below as i64;
// === CHOOSE NEXT PIVOT FROM ACTIVE SET ===
let active_size: usize = (0..n - 1)
.filter(|&i| left_bounds[i] <= right_bounds[i])
.map(|i| right_bounds[i] - left_bounds[i] + 1)
.sum();
if active_size <= 2 {
// Few candidates left: return midrange of remaining
let mut min_rem = f64::INFINITY;
let mut max_rem = f64::NEG_INFINITY;
for i in 0..n - 1 {
if left_bounds[i] > right_bounds[i] {
continue;
}
let lo = a[left_bounds[i]] - a[i];
let hi = a[right_bounds[i]] - a[i];
min_rem = min_rem.min(lo);
max_rem = max_rem.max(hi);
}
if active_size == 0 {
if k_low < k_high {
return Ok(0.5 * (largest_below + smallest_at_or_above));
}
return Ok(if count_below >= k_low {
largest_below
} else {
smallest_at_or_above
});
}
if k_low < k_high {
return Ok(0.5 * (min_rem + max_rem));
}
return Ok(
if ((k_low - 1) as i64 - count_below as i64).abs()
<= (count_below as i64 - k_low as i64).abs()
{
min_rem
} else {
max_rem
},
);
} else {
// Weighted random row selection
let t = rng.gen_range(0..active_size);
let mut acc = 0;
let mut row = 0;
for r in 0..n - 1 {
if left_bounds[r] > right_bounds[r] {
continue;
}
let size = right_bounds[r] - left_bounds[r] + 1;
if t < acc + size {
row = r;
break;
}
acc += size;
}
// Median column of the selected row
let col = (left_bounds[row] + right_bounds[row]) / 2;
pivot = a[col] - a[row];
}
}
}