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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/// O(n log n) implementation of the Center (Hodges-Lehmann) estimator.
/// Based on Monahan's Algorithm 616 (1984).
///
/// Internal implementation - not part of public API.
use crate::fnv1a::hash_f64_slice;
use crate::rng::Rng;
pub(crate) fn center_impl(values: &[f64], assume_sorted: bool) -> Result<f64, &'static str> {
let n = values.len();
if n == 0 {
return Err("Input slice cannot be empty");
}
if n == 1 {
return Ok(values[0]);
}
if n == 2 {
return Ok(0.5 * values[0] + 0.5 * values[1]);
}
// Validate for NaN/infinite values
if values.iter().any(|v| !v.is_finite()) {
return Err("Input contains NaN or infinite values");
}
let owned_sorted;
let sorted_values: &[f64] = if assume_sorted {
values
} else {
owned_sorted = {
let mut v = values.to_vec();
v.sort_unstable_by(|a, b| a.total_cmp(b));
v
};
&owned_sorted
};
// Calculate target median rank(s) among all pairwise sums
let total_pairs = (n * (n + 1)) / 2;
let median_rank_low = total_pairs.div_ceil(2); // 1-based rank
let median_rank_high = (total_pairs + 2) / 2;
// Initialize search bounds for each row (1-based indexing)
let mut left_bounds: Vec<usize> = (0..n).map(|i| i + 1).collect();
let mut right_bounds = vec![n; n];
// Start with a good pivot: sum of middle elements
let mut pivot = sorted_values[(n - 1) / 2] + sorted_values[n / 2];
let mut active_set_size = total_pairs;
let mut previous_count = 0;
let mut rng = Rng::from_seed(hash_f64_slice(values));
let mut partition_counts = vec![0; n];
// Bound the selection loop. On valid sorted input the Monahan selection
// converges in O(log n) iterations; this cap is far higher than ever
// needed for sorted input but guarantees termination on misuse (e.g.,
// assume_sorted=true on UNSORTED input, which is undefined behavior and
// would otherwise spin forever). The cap scales with n so large valid
// inputs are never starved. We also track no-progress (stall) on the
// active set to bail out deterministically with a plain error (NOT an
// assumption error).
const BASE_ITERATIONS: usize = 256;
const MAX_STALL: usize = 8;
let max_iterations = BASE_ITERATIONS + 4 * n;
let mut prev_active_set_size: i64 = -1;
let mut stall_count: usize = 0;
for _ in 0..max_iterations {
// === PARTITION STEP ===
let mut count_below_pivot = 0;
let mut current_column = n;
partition_counts.fill(0);
for row in 1..=n {
// Move left from current column until we find sums < pivot
while current_column >= row
&& sorted_values[row - 1] + sorted_values[current_column - 1] >= pivot
{
current_column -= 1;
}
// Count elements in this row that are < pivot
let elements_below = if current_column >= row {
current_column - row + 1
} else {
0
};
partition_counts[row - 1] = elements_below;
count_below_pivot += elements_below;
}
// === CONVERGENCE CHECK ===
if count_below_pivot == previous_count {
let mut min_active_sum = f64::INFINITY;
let mut max_active_sum = f64::NEG_INFINITY;
for i in 0..n {
if left_bounds[i] > right_bounds[i] {
continue;
}
let row_value = sorted_values[i];
let smallest_in_row = sorted_values[left_bounds[i] - 1] + row_value;
let largest_in_row = sorted_values[right_bounds[i] - 1] + row_value;
min_active_sum = min_active_sum.min(smallest_in_row);
max_active_sum = max_active_sum.max(largest_in_row);
}
pivot = 0.5 * min_active_sum + 0.5 * max_active_sum;
if pivot <= min_active_sum || pivot > max_active_sum {
pivot = max_active_sum;
}
if min_active_sum == max_active_sum || active_set_size <= 2 {
return Ok(pivot / 2.0);
}
continue;
}
// === TARGET CHECK ===
let at_target_rank =
count_below_pivot == median_rank_low || count_below_pivot == median_rank_high - 1;
if at_target_rank {
let mut largest_below_pivot = f64::NEG_INFINITY;
let mut smallest_at_or_above_pivot = f64::INFINITY;
for i in 0..n {
let count_in_row = partition_counts[i];
let row_value = sorted_values[i];
let total_in_row = n - i;
// Find largest sum in this row that's < pivot
if count_in_row > 0 {
let last_below_index = i + count_in_row;
let last_below_value = row_value + sorted_values[last_below_index - 1];
largest_below_pivot = largest_below_pivot.max(last_below_value);
}
// Find smallest sum in this row that's >= pivot
if count_in_row < total_in_row {
let first_at_or_above_index = i + count_in_row + 1;
let first_at_or_above_value =
row_value + sorted_values[first_at_or_above_index - 1];
smallest_at_or_above_pivot =
smallest_at_or_above_pivot.min(first_at_or_above_value);
}
}
// Calculate final result
if median_rank_low < median_rank_high {
// Even total: average the two middle values. Overflow-safe: quarter each
// pair-sum before summing (both operands can be near the double max).
return Ok(0.25 * smallest_at_or_above_pivot + 0.25 * largest_below_pivot);
} else {
// Odd total: return the single middle value
let need_largest = count_below_pivot == median_rank_low;
return Ok(if need_largest {
largest_below_pivot
} else {
smallest_at_or_above_pivot
} / 2.0);
}
}
// === UPDATE BOUNDS ===
if count_below_pivot < median_rank_low {
// Too few values below pivot - search higher
for i in 0..n {
left_bounds[i] = i + partition_counts[i] + 1;
}
} else {
// Too many values below pivot - search lower
for i in 0..n {
right_bounds[i] = i + partition_counts[i];
}
}
// === PREPARE NEXT ITERATION ===
previous_count = count_below_pivot;
// Recalculate active set size
active_set_size = left_bounds
.iter()
.zip(right_bounds.iter())
.map(|(l, r)| if r >= l { r - l + 1 } else { 0 })
.sum();
// Stall detection: on valid sorted input the active set strictly
// shrinks toward the target. If it fails to shrink for several
// consecutive iterations, the input is pathological (e.g.,
// assume_sorted=true on unsorted data) and we bail deterministically.
if active_set_size as i64 >= prev_active_set_size && prev_active_set_size >= 0 {
stall_count += 1;
if stall_count >= MAX_STALL {
return Err("Convergence failure (pathological input)");
}
} else {
stall_count = 0;
}
prev_active_set_size = active_set_size as i64;
// Choose next pivot
if active_set_size > 2 {
// Use randomized row median strategy
let target_index = rng.uniform_i64(0, active_set_size as i64) as usize;
let mut cumulative_size = 0;
let mut selected_row = 0;
for i in 0..n {
let row_size = if right_bounds[i] >= left_bounds[i] {
right_bounds[i] - left_bounds[i] + 1
} else {
0
};
if target_index < cumulative_size + row_size {
selected_row = i;
break;
}
cumulative_size += row_size;
}
// Use median element of the selected row as pivot
let median_column_in_row =
usize::midpoint(left_bounds[selected_row], right_bounds[selected_row]);
pivot = sorted_values[selected_row] + sorted_values[median_column_in_row - 1];
} else {
// Few elements remain - use midrange strategy
let mut min_remaining_sum = f64::INFINITY;
let mut max_remaining_sum = f64::NEG_INFINITY;
for i in 0..n {
if left_bounds[i] > right_bounds[i] {
continue;
}
let row_value = sorted_values[i];
let min_in_row = sorted_values[left_bounds[i] - 1] + row_value;
let max_in_row = sorted_values[right_bounds[i] - 1] + row_value;
min_remaining_sum = min_remaining_sum.min(min_in_row);
max_remaining_sum = max_remaining_sum.max(max_in_row);
}
pivot = 0.5 * min_remaining_sum + 0.5 * max_remaining_sum;
if pivot <= min_remaining_sum || pivot > max_remaining_sum {
pivot = max_remaining_sum;
}
if min_remaining_sum == max_remaining_sum {
return Ok(pivot / 2.0);
}
}
}
Err("Convergence failure (pathological input)")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sorted_input_converges() {
// Valid sorted input must converge well within the iteration cap.
let values: Vec<f64> = (0..101).map(|i| i as f64).collect();
let result = center_impl(&values, true).unwrap();
assert!((result - 50.0).abs() < 1e-9);
}
#[test]
fn unsorted_with_assume_sorted_does_not_hang() {
// assume_sorted=true on UNSORTED input violates the contract (UB).
// Without the iteration cap and stall detection, the Monahan selection
// loop could spin forever on such input (an unkillable process wedge).
// This test pins the guard: the call must terminate quickly with a
// deterministic convergence-failure error rather than hang.
//
// This adversarial input is crafted to defeat the selection invariant so
// the active set fails to shrink; we assert the bailout, not any value.
let values = vec![
0.0, 100.0, 1.0, 99.0, 2.0, 98.0, 3.0, 97.0, 50.0, 4.0, 96.0, 5.0, 95.0, 49.0, 51.0,
];
let result = center_impl(&values, true);
// The contract is "does not hang"; if the loop were uncapped this test
// would never complete. Assert the bailout unconditionally.
assert!(result.is_err());
assert!(
result.unwrap_err().contains("Convergence failure"),
"expected convergence-failure error"
);
}
#[test]
fn pathological_unsorted_returns_convergence_error() {
// A strongly anti-sorted sequence under assume_sorted=true. The stall
// guard detects that the active set fails to shrink and bails out with
// a deterministic convergence-failure error instead of spinning.
let n = 64;
let mut values = Vec::with_capacity(n);
for i in 0..n {
if i % 2 == 0 {
values.push((n - i) as f64);
} else {
values.push(i as f64);
}
}
let result = center_impl(&values, true);
assert_eq!(result, Err("Convergence failure (pathological input)"));
}
}