| 1 | //! This crate is a stable sorting algorithm with O(n) worst-case storage |
| 2 | //! requirements, O(n log n) worst-case comparisons, and O(n) comparisons |
| 3 | //! on an already-sorted list, smoothly becoming O(n log n) as the sorted |
| 4 | //! sections (runs) get smaller and smaller. |
| 5 | |
| 6 | #![cfg_attr(not(test), no_std)] |
| 7 | |
| 8 | extern crate alloc; |
| 9 | |
| 10 | mod find_run; |
| 11 | mod gallop; |
| 12 | mod insort; |
| 13 | mod merge; |
| 14 | mod sort; |
| 15 | |
| 16 | use core::cmp::Ordering; |
| 17 | use core::convert::Infallible; |
| 18 | use sort::try_sort_by as try_sort_by_cmp; |
| 19 | |
| 20 | type NeverResult<T> = Result<T, Infallible>; |
| 21 | #[inline(always)] |
| 22 | fn never<T>(x: Infallible) -> T { |
| 23 | match x {} |
| 24 | } |
| 25 | |
| 26 | pub fn try_sort_by_gt<T, E, C: Fn(&T, &T) -> Result<bool, E>>( |
| 27 | list: &mut [T], |
| 28 | cmp: C, |
| 29 | ) -> Result<(), E> { |
| 30 | try_sort_by_cmp(list, cmp) |
| 31 | } |
| 32 | |
| 33 | #[inline] |
| 34 | pub fn try_sort_by<T, E, C: Fn(&T, &T) -> Result<Ordering, E>>( |
| 35 | list: &mut [T], |
| 36 | cmp: C, |
| 37 | ) -> Result<(), E> { |
| 38 | try_sort_by_cmp(list, ord_comparator(cmp)) |
| 39 | } |
| 40 | |
| 41 | #[inline] |
| 42 | pub fn sort_by_gt<T, C: Fn(&T, &T) -> bool>(list: &mut [T], is_greater: C) { |
| 43 | try_sort_by_gt(list, move |a, b| -> NeverResult<_> { Ok(is_greater(a, b)) }) |
| 44 | .unwrap_or_else(never) |
| 45 | } |
| 46 | |
| 47 | #[inline] |
| 48 | pub fn sort_by<T, C: Fn(&T, &T) -> Ordering>(list: &mut [T], cmp: C) { |
| 49 | try_sort_by(list, move |a, b| -> NeverResult<_> { Ok(cmp(a, b)) }).unwrap_or_else(never) |
| 50 | } |
| 51 | |
| 52 | #[inline] |
| 53 | pub fn sort<T: Ord>(list: &mut [T]) { |
| 54 | sort_by(list, Ord::cmp) |
| 55 | } |
| 56 | |
| 57 | trait Comparator<T> { |
| 58 | type Error; |
| 59 | fn is_gt(&self, lhs: &T, rhs: &T) -> Result<bool, Self::Error>; |
| 60 | fn ordering(&self, lhs: &T, rhs: &T) -> Result<Ordering, Self::Error> { |
| 61 | let ord = if self.is_gt(lhs, rhs)? { |
| 62 | Ordering::Greater |
| 63 | } else if self.is_gt(rhs, lhs)? { |
| 64 | Ordering::Less |
| 65 | } else { |
| 66 | Ordering::Equal |
| 67 | }; |
| 68 | Ok(ord) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<F, T, E> Comparator<T> for F |
| 73 | where |
| 74 | F: Fn(&T, &T) -> Result<bool, E>, |
| 75 | { |
| 76 | type Error = E; |
| 77 | fn is_gt(&self, lhs: &T, rhs: &T) -> Result<bool, E> { |
| 78 | self(lhs, rhs) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // really weird, idk why this is necessary... |
| 83 | #[cfg(test)] |
| 84 | pub(crate) fn comparator<T>( |
| 85 | f: impl Fn(&T, &T) -> NeverResult<bool>, |
| 86 | ) -> impl Comparator<T, Error = Infallible> { |
| 87 | f |
| 88 | } |
| 89 | #[cfg(test)] |
| 90 | pub(crate) fn ord_t_comparator<T: Ord>() -> impl Comparator<T, Error = Infallible> { |
| 91 | ord_comparator(|a: &T, b| Ok(a.cmp(b))) |
| 92 | } |
| 93 | |
| 94 | pub(crate) fn ord_comparator<T, E, F: Fn(&T, &T) -> Result<Ordering, E>>( |
| 95 | f: F, |
| 96 | ) -> impl Comparator<T, Error = E> { |
| 97 | struct OrdComparator<F>(F); |
| 98 | impl<T, E, F: Fn(&T, &T) -> Result<Ordering, E>> Comparator<T> for OrdComparator<F> { |
| 99 | type Error = E; |
| 100 | fn is_gt(&self, lhs: &T, rhs: &T) -> Result<bool, E> { |
| 101 | (self.0)(lhs, rhs).map(|ord| ord == Ordering::Greater) |
| 102 | } |
| 103 | fn ordering(&self, lhs: &T, rhs: &T) -> Result<Ordering, Self::Error> { |
| 104 | (self.0)(lhs, rhs) |
| 105 | } |
| 106 | } |
| 107 | OrdComparator(f) |
| 108 | } |