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
// use std::ops::Sub;
// use std::cmp::Ordering;
// use anyhow::{Result,bail};
use ;
/// Median of a &[T] slice by sorting
/// Works slowly but gives exact results
/// Sorts its mutable slice argument as a side effect
/// # Example
/// ```
/// use medians::naive_median;
/// let mut v = vec![1_u8,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
/// let res = naive_median(&mut v);
/// assert_eq!(res,8_f64);
/// ```
/// Exactly the same as naive_median, except uses hashsort,
/// which is about 25% faster for >1K items.
/// used by testing.rs to measure errors
/// Iterative move towards the median.
/// Returns ( positive imbalance, number of items equal to x,
/// increment of x position towards the median )
/// Iterative median based on the modified 1D case
/// of the modified nD Weiszfeld algorithm.
/// Can sometimes fail to give the best answer
/*
/// swap two slice items if they are out of ascending order
fn compswap<T>(s: &mut [T], i1: usize, i2: usize)
where T: PartialOrd { if s[i1] > s[i2] { s.swap(i1,i2) } }
/// N recursive hash sort.
/// Sorts mutable first argument (slice) in place
/// Requires [min,max], the data range, that must enclose all its values.
/// The range is often known in advance. If not, it can be obtained with `minmaxt`.
pub fn h_median<T>(s: &mut [T], min:f64, max:f64) -> f64
where T: PartialOrd + Copy, f64:From<T> {
if min >= max { panic!("{} data range must be min < max",here!()); };
let n = s.len();
match n {
0 => panic!("{} empty input",here!()),
1 => f64::from(s[0]),
2 => (f64::from(s[0])+f64::from(s[1]))/2.0,
3 => {
compswap(s,0,1);
compswap(s,1,2);
compswap(s,0,1);
return f64::from(s[1])
},
_ => if (n & 1) == 0 { h_medr_even(s,0,n,min,max) }
else { h_medr_odd(s,0,n,min,max) }
}
}
fn h_medr_odd<T>(s:&mut [T], i:usize, n:usize, min:f64, max:f64) -> f64
where T: PartialOrd+Copy, f64:From<T>
{
if n == 0 { panic!("{} unexpected zero length",here!())};
// hash is a constant s.t. (x-min)*hash is in [0,n)
// subtracting a small constant stops subscripts quite reaching n
let hash = (n as f64 - 1e-10 ) / (max-min);
let mut freqvec:Vec<Vec<T>> = vec![Vec::new();n];
// group current index items into buckets by their associated s[] values
for &xi in s.iter().skip(i).take(n) {
freqvec[(hash*(f64::from(xi)-min)).floor() as usize].push(xi);
};
// count the items in buckets
let mut isub = i;
for v in freqvec.iter() {
let vlen = v.len();
if vlen == 0 { continue; };
isub += vlen;
if isub <= n/2 { continue; };
match vlen {
1 => return f64::from(v[0]),
2 => {
if isub == n/2 { return f64::from(v[1]) }
else
},
3 => {
s[isub] = v[0]; s[isub+1] = v[1]; s[isub+2] = v[2];
compswap(s,isub,isub+1);
compswap(s,isub+1,isub+2);
compswap(s,isub,isub+1);
isub += 3;
},
x if x == n => {
// this bucket alone is populated,
// items in it are most likely all equal
// we need not copy v back as no sorting took place
let mx = minmax_slice(s, isub, vlen);
if mx.minindex < mx.maxindex { // not all the same
let mut hold = s[i]; // swap minindex to the front
s[i] = s[mx.minindex];
s[mx.minindex] = hold;
hold = s[i+n-1]; // swap maxindex to the end
s[i+n-1] = s[mx.maxindex];
s[mx.maxindex] = hold;
// recurse to sort the rest, within the new reduced range
hashsortr(s,i+1,n-2,f64::from(mx.min),f64::from(mx.max));
};
return; // all items were equal, or are now sorted
},
_ => {
// first fill the index with the grouped items from v
let isubprev = isub;
for &item in v { s[isub] = item; isub += 1; };
let mx = minmax_slice(s, isubprev, vlen);
if mx.minindex < mx.maxindex { // else are all equal
let mut hold = s[isubprev]; // swap minindex to the front
s[isubprev] = s[mx.minindex];
s[mx.minindex] = hold;
hold = s[isub-1]; // swap maxindex to the end
s[isub-1] = s[mx.maxindex];
s[mx.maxindex] = hold;
// recurse to sort the rest
hashsortr(s,isubprev+1,vlen-2,f64::from(mx.min),f64::from(mx.max));
}; // the items in this bucket were equal or are now sorted but there are more buckets
}
} // end of match
} // end of for v
}
*/