//! This crate is a stable sorting algorithm with O(n) worst-case storage //! requirements, O(n log n) worst-case comparisons, and O(n) comparisons //! on an already-sorted list, smoothly becoming O(n log n) as the sorted //! sections (runs) get smaller and smaller. #![cfg_attr(not(test), no_std)] extern crate alloc; mod find_run; mod gallop; mod insort; mod merge; mod sort; use core::cmp::Ordering; use core::convert::Infallible; use sort::try_sort_by as try_sort_by_cmp; type NeverResult = Result; #[inline(always)] fn never(x: Infallible) -> T { match x {} } pub fn try_sort_by_gt Result>( list: &mut [T], cmp: C, ) -> Result<(), E> { try_sort_by_cmp(list, cmp) } #[inline] pub fn try_sort_by Result>( list: &mut [T], cmp: C, ) -> Result<(), E> { try_sort_by_cmp(list, ord_comparator(cmp)) } #[inline] pub fn sort_by_gt bool>(list: &mut [T], is_greater: C) { try_sort_by_gt(list, move |a, b| -> NeverResult<_> { Ok(is_greater(a, b)) }) .unwrap_or_else(never) } #[inline] pub fn sort_by Ordering>(list: &mut [T], cmp: C) { try_sort_by(list, move |a, b| -> NeverResult<_> { Ok(cmp(a, b)) }).unwrap_or_else(never) } #[inline] pub fn sort(list: &mut [T]) { sort_by(list, Ord::cmp) } trait Comparator { type Error; fn is_gt(&self, lhs: &T, rhs: &T) -> Result; fn ordering(&self, lhs: &T, rhs: &T) -> Result { let ord = if self.is_gt(lhs, rhs)? { Ordering::Greater } else if self.is_gt(rhs, lhs)? { Ordering::Less } else { Ordering::Equal }; Ok(ord) } } impl Comparator for F where F: Fn(&T, &T) -> Result, { type Error = E; fn is_gt(&self, lhs: &T, rhs: &T) -> Result { self(lhs, rhs) } } // really weird, idk why this is necessary... #[cfg(test)] pub(crate) fn comparator( f: impl Fn(&T, &T) -> NeverResult, ) -> impl Comparator { f } #[cfg(test)] pub(crate) fn ord_t_comparator() -> impl Comparator { ord_comparator(|a: &T, b| Ok(a.cmp(b))) } pub(crate) fn ord_comparator Result>( f: F, ) -> impl Comparator { struct OrdComparator(F); impl Result> Comparator for OrdComparator { type Error = E; fn is_gt(&self, lhs: &T, rhs: &T) -> Result { (self.0)(lhs, rhs).map(|ord| ord == Ordering::Greater) } fn ordering(&self, lhs: &T, rhs: &T) -> Result { (self.0)(lhs, rhs) } } OrdComparator(f) }