diff options
| author | Stjepan Glavina <stjepang@gmail.com> | 2017-03-21 02:38:03 +0100 |
|---|---|---|
| committer | Stjepan Glavina <stjepang@gmail.com> | 2017-03-21 20:46:20 +0100 |
| commit | a718051f63308638ecf40a7570dbd18f4fc99703 (patch) | |
| tree | 4a0c488b1fa8fcef586b4fc81459675435134e60 /src/libcore/slice | |
| parent | a18b2aa641da7c000fea9e9ac62d2da89fa034ad (diff) | |
| download | rust-a718051f63308638ecf40a7570dbd18f4fc99703.tar.gz rust-a718051f63308638ecf40a7570dbd18f4fc99703.zip | |
Unit test heapsort
Diffstat (limited to 'src/libcore/slice')
| -rw-r--r-- | src/libcore/slice/mod.rs | 9 | ||||
| -rw-r--r-- | src/libcore/slice/sort.rs | 4 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 53cbdd84c3a..6f8b199f886 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2253,6 +2253,15 @@ pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] { mem::transmute(Repr { data: p, len: len }) } +// This function is public only because there is no other way to unit test heapsort. +#[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "0")] +#[doc(hidden)] +pub fn heapsort<T, F>(v: &mut [T], mut is_less: F) + where F: FnMut(&T, &T) -> bool +{ + sort::heapsort(v, &mut is_less); +} + // // Comparison traits // diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index a015c8fc120..fdfba33f8a9 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -57,7 +57,7 @@ fn shift_head<T, F>(v: &mut [T], is_less: &mut F) ptr::copy_nonoverlapping(v.get_unchecked(1), v.get_unchecked_mut(0), 1); for i in 2..len { - if !is_less(&v[i], &tmp.value) { + if !is_less(v.get_unchecked(i), &tmp.value) { break; } @@ -159,7 +159,7 @@ fn insertion_sort<T, F>(v: &mut [T], is_less: &mut F) /// Sorts `v` using heapsort, which guarantees `O(n log n)` worst-case. #[cold] -fn heapsort<T, F>(v: &mut [T], is_less: &mut F) +pub fn heapsort<T, F>(v: &mut [T], is_less: &mut F) where F: FnMut(&T, &T) -> bool { // This binary heap respects the invariant `parent >= child`. |
