| 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 allocrust-analyzer cargo timsort 0.1.3 alloc/; |
| 9 | |
| 10 | mod find_runrust-analyzer cargo timsort 0.1.3 find_run/; |
| 11 | mod galloprust-analyzer cargo timsort 0.1.3 gallop/; |
| 12 | mod insortrust-analyzer cargo timsort 0.1.3 insort/; |
| 13 | mod mergerust-analyzer cargo timsort 0.1.3 merge/; |
| 14 | mod sortrust-analyzer cargo timsort 0.1.3 sort/; |
| 15 | |
| 16 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#; |
| 17 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#; |
| 18 | use sortrust-analyzer cargo timsort 0.1.3 sort/::try_sort_byrust-analyzer cargo timsort 0.1.3 sort/try_sort_by(). as try_sort_by_cmprust-analyzer cargo timsort 0.1.3 sort/try_sort_by().; |
| 19 | |
| 20 | type NeverResultrust-analyzer cargo timsort 0.1.3 NeverResult#<T> = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#>; |
| 21 | #[inline(always)] |
| 22 | fn neverrust-analyzer cargo timsort 0.1.3 never().<T>(x: Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#) -> T { |
| 23 | match x {} |
| 24 | } |
| 25 | |
| 26 | pub fn try_sort_by_gtrust-analyzer cargo timsort 0.1.3 try_sort_by_gt().<T, E, C: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, E>>( |
| 27 | list: &mut [T], |
| 28 | cmp: C, |
| 29 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E> { |
| 30 | try_sort_by_cmprust-analyzer cargo timsort 0.1.3 sort/try_sort_by().(list, cmp) |
| 31 | } |
| 32 | |
| 33 | #[inline] |
| 34 | pub fn try_sort_byrust-analyzer cargo timsort 0.1.3 try_sort_by().<T, E, C: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, E>>( |
| 35 | list: &mut [T], |
| 36 | cmp: C, |
| 37 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E> { |
| 38 | try_sort_by_cmprust-analyzer cargo timsort 0.1.3 sort/try_sort_by().(list, ord_comparatorrust-analyzer cargo timsort 0.1.3 ord_comparator().(cmp)) |
| 39 | } |
| 40 | |
| 41 | #[inline] |
| 42 | pub fn sort_by_gtrust-analyzer cargo timsort 0.1.3 sort_by_gt().<T, C: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> bool>(list: &mut [T], is_greater: C) { |
| 43 | try_sort_by_gtrust-analyzer cargo timsort 0.1.3 try_sort_by_gt().(list, move |a, b| -> NeverResultrust-analyzer cargo timsort 0.1.3 NeverResult#<_> { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(is_greater(a, b)) }) |
| 44 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(neverrust-analyzer cargo timsort 0.1.3 never().) |
| 45 | } |
| 46 | |
| 47 | #[inline] |
| 48 | pub fn sort_byrust-analyzer cargo timsort 0.1.3 sort_by().<T, C: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#>(list: &mut [T], cmp: C) { |
| 49 | try_sort_byrust-analyzer cargo timsort 0.1.3 try_sort_by().(list, move |a, b| -> NeverResultrust-analyzer cargo timsort 0.1.3 NeverResult#<_> { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(cmp(a, b)) }).unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(neverrust-analyzer cargo timsort 0.1.3 never().) |
| 50 | } |
| 51 | |
| 52 | #[inline] |
| 53 | pub fn sortrust-analyzer cargo timsort 0.1.3 sort().<T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#>(list: &mut [T]) { |
| 54 | sort_byrust-analyzer cargo timsort 0.1.3 sort_by().(list, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().) |
| 55 | } |
| 56 | |
| 57 | trait Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T> { |
| 58 | type Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#; |
| 59 | fn is_gtrust-analyzer cargo timsort 0.1.3 Comparator#is_gt().(&self, lhs: &T, rhs: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, Self::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#>; |
| 60 | fn orderingrust-analyzer cargo timsort 0.1.3 Comparator#ordering().(&self, lhs: &T, rhs: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, Self::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 61 | let ord = if self.is_gtrust-analyzer cargo timsort 0.1.3 Comparator#is_gt().(lhs, rhs)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 62 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater# |
| 63 | } else if self.is_gtrust-analyzer cargo timsort 0.1.3 Comparator#is_gt().(rhs, lhs)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 64 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# |
| 65 | } else { |
| 66 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# |
| 67 | }; |
| 68 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ord) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<F, T, E> Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T> for F |
| 73 | where |
| 74 | F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, E>, |
| 75 | { |
| 76 | type Errorrust-analyzer cargo timsort 0.1.3 impl#[F][`Comparator<T>`]Error# = E; |
| 77 | fn is_gtrust-analyzer cargo timsort 0.1.3 impl#[F][`Comparator<T>`]is_gt().(&self, lhs: &T, rhs: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, E> { |
| 78 | self(lhs, rhs) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // really weird, idk why this is necessary... |
| 83 | #[cfg(test)] |
| 84 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn comparatorrust-analyzer cargo timsort 0.1.3 comparator().<T>( |
| 85 | f: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> NeverResultrust-analyzer cargo timsort 0.1.3 NeverResult#<bool>, |
| 86 | ) -> impl Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T, Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error# = Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#> { |
| 87 | f |
| 88 | } |
| 89 | #[cfg(test)] |
| 90 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn ord_t_comparatorrust-analyzer cargo timsort 0.1.3 ord_t_comparator().<T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#>() -> impl Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T, Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error# = Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#> { |
| 91 | ord_comparatorrust-analyzer cargo timsort 0.1.3 ord_comparator().(|a: &T, b| Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(a.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().(b))) |
| 92 | } |
| 93 | |
| 94 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn ord_comparatorrust-analyzer cargo timsort 0.1.3 ord_comparator().<T, E, F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, E>>( |
| 95 | f: F, |
| 96 | ) -> impl Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T, Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error# = E> { |
| 97 | struct OrdComparatorrust-analyzer cargo timsort 0.1.3 OrdComparator#<F>(F); |
| 98 | impl<T, E, F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T, &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, E>> Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T> for OrdComparatorrust-analyzer cargo timsort 0.1.3 OrdComparator#<F> { |
| 99 | type Errorrust-analyzer cargo timsort 0.1.3 impl#[`OrdComparator<F>`][`Comparator<T>`]Error# = E; |
| 100 | fn is_gtrust-analyzer cargo timsort 0.1.3 impl#[`OrdComparator<F>`][`Comparator<T>`]is_gt().(&self, lhs: &T, rhs: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, E> { |
| 101 | (self.0rust-analyzer cargo timsort 0.1.3 OrdComparator#0.)(lhs, rhs).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|ord| ord rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq().Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater#) |
| 102 | } |
| 103 | fn orderingrust-analyzer cargo timsort 0.1.3 impl#[`OrdComparator<F>`][`Comparator<T>`]ordering().(&self, lhs: &T, rhs: &T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, Selfrust-analyzer cargo timsort 0.1.3 impl#[`OrdComparator<F>`][`Comparator<T>`]::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 104 | (self.0rust-analyzer cargo timsort 0.1.3 OrdComparator#0.)(lhs, rhs) |
| 105 | } |
| 106 | } |
| 107 | OrdComparatorrust-analyzer cargo timsort 0.1.3 OrdComparator#(f) |
| 108 | } |