1
1
//! This module contains the logic for pivot selection.
2
2
3
- use crate :: intrinsics;
3
+ use crate :: { hint , intrinsics} ;
4
4
5
5
// Recursively select a pseudomedian if above this threshold.
6
6
const PSEUDO_MEDIAN_REC_THRESHOLD : usize = 64 ;
@@ -9,6 +9,7 @@ const PSEUDO_MEDIAN_REC_THRESHOLD: usize = 64;
9
9
///
10
10
/// This chooses a pivot by sampling an adaptive amount of points, approximating
11
11
/// the quality of a median of sqrt(n) elements.
12
+ #[ inline]
12
13
pub fn choose_pivot < T , F : FnMut ( & T , & T ) -> bool > ( v : & [ T ] , is_less : & mut F ) -> usize {
13
14
// We use unsafe code and raw pointers here because we're dealing with
14
15
// heavy recursion. Passing safe slices around would involve a lot of
@@ -22,7 +23,7 @@ pub fn choose_pivot<T, F: FnMut(&T, &T) -> bool>(v: &[T], is_less: &mut F) -> us
22
23
// SAFETY: a, b, c point to initialized regions of len_div_8 elements,
23
24
// satisfying median3 and median3_rec's preconditions as v_base points
24
25
// to an initialized region of n = len elements.
25
- unsafe {
26
+ let index = unsafe {
26
27
let v_base = v. as_ptr ( ) ;
27
28
let len_div_8 = len / 8 ;
28
29
@@ -35,6 +36,11 @@ pub fn choose_pivot<T, F: FnMut(&T, &T) -> bool>(v: &[T], is_less: &mut F) -> us
35
36
} else {
36
37
median3_rec ( a, b, c, len_div_8, is_less) . offset_from_unsigned ( v_base)
37
38
}
39
+ } ;
40
+ // SAFETY: preconditions must have been met for offset_from_unsigned()
41
+ unsafe {
42
+ hint:: assert_unchecked ( index < v. len ( ) ) ;
43
+ index
38
44
}
39
45
}
40
46
0 commit comments