#[repr(transparent)]pub struct Sort<T>(pub T);
Expand description
Provides sorting methods for arrays and slices of T
, some of them const.
It implements the following methods for sorting exclusive slices:
bubble
,
counting
,
counting_buf
,
insertion
,
merge
,
quick_lomuto
,
quick_hoare
,
quick_3way
,
quick_selection
,
quick_shaker
.
§Features
When the corresponding feature _sort_f[32|64]
or _sort_[iu][8|16|32|64|128]
is enabled,
It implements the following const methods for sorting copied arrays of primitives:
bubble_array
, insertion_array
, selection_array
.
In the case of floating-point primitives it uses total ordering.
§Examples
Sort copied arrays of primitives:
let mut arr = [4i32, 7, -5, 1, -13, 0]; // signed primitives
assert_eq![Sort(arr).bubble_array(), [-13, -5, 0, 1, 4, 7]];
assert_eq![Sort(arr).insertion_array(), [-13, -5, 0, 1, 4, 7]];
assert_eq![Sort(arr).selection_array(), [-13, -5, 0, 1, 4, 7]];
let mut arr = [4u32, 7, 5, 1, 13, 0]; // unsigned primitives
assert_eq![Sort(arr).bubble_array(), [0, 1, 4, 5, 7, 13]];
assert_eq![Sort(arr).insertion_array(), [0, 1, 4, 5, 7, 13]];
assert_eq![Sort(arr).selection_array(), [0, 1, 4, 5, 7, 13]];
let mut arr = [4.01f32, 7.9, -5.4, 1.0, 0.0, -0.0]; // floating-point primitives
assert_eq![Sort(arr).bubble_array(), [-5.4, -0.0, 0.0, 1.0, 4.01, 7.9]];
assert_eq![Sort(arr).insertion_array(), [-5.4, -0.0, 0.0, 1.0, 4.01, 7.9]];
assert_eq![Sort(arr).selection_array(), [-5.4, -0.0, 0.0, 1.0, 4.01, 7.9]];
§Performance
The _array
suffixed methods calls the cswap
macro using the xor swap
algorithm, except for the floting-point version which uses a temporary variable.
Tuple Fields§
§0: T
Implementations§
Source§impl<T: Ord> Sort<&mut [T]>
impl<T: Ord> Sort<&mut [T]>
Sourcepub fn bubble(self)
pub fn bubble(self)
Sorts a slice using bubble sort.
§Examples
let mut data = [4, 7, -5, 1, -13, 0];
Sort(&mut data[..]).bubble();
assert_eq![data, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn counting(self) -> Vec<usize> ⓘwhere
T: Clone,
Available on crate feature alloc
only.
pub fn counting(self) -> Vec<usize> ⓘwhere
T: Clone,
alloc
only.Sorts a slice using counting sort, and returns the ordered frequencies.
Counting sort is particularly efficient when the range of input values is small compared to the number of elements to be sorted.
§Examples
let mut data = [4, 64, 4, 2, 4, 8, 8, 4, 8, 4, 2, 8, 64, 4, 8, 4, 2];
let freq = Sort(&mut data[..]).counting();
assert_eq![data, [2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 64, 64]];
assert_eq![freq, [3, 7, 5, 2]];
Sourcepub fn counting_buf(self, freq: &mut [T], values: &[T]) -> Option<()> ⓘ
pub fn counting_buf(self, freq: &mut [T], values: &[T]) -> Option<()> ⓘ
Sorts a slice using counting sort, and writes the frequencies, without allocating.
Counting sort is particularly efficient when the range of input values is small compared to the number of elements to be sorted.
This implementation makes the following assumptions:
values
contains all distinct values present inself
.freq
andvalues
are of the same length.freq
only contains zeros.
Returns None
if values
does not contain a value present in self
,
or if self
has more elements than freq
can accommodate.
Note that the frequencies in freq
will be in the order of the sorted
distinct elements in values
.
§Examples
let mut data = [4, 64, 4, 2, 4, 8, 8, 4, 8, 4, 2, 8, 64, 4, 8, 4, 2];
let values = [64, 4, 2, 8];
let mut freq = [0; 4];
Sort(&mut data[..]).counting_buf(&mut freq, &values);
assert_eq![data, [64, 64, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 8, 8, 8, 8, 8]];
assert_eq![freq, [2, 7, 3, 5]];
§Panics
Panics in debug if the length of freq
and values
is not the same.
Sourcepub fn insertion(self)
pub fn insertion(self)
Sorts a slice using insertion sort.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort(&mut arr[..]).insertion();
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn merge(self)where
T: Copy,
Available on crate feature alloc
only.
pub fn merge(self)where
T: Copy,
alloc
only.Sorts a slice
using merge sort.
It allocates one vector for the entire sort operation.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort(&mut arr[..]).merge();
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Source§impl<'a, T: Ord + 'a> Sort<&'a mut [T]>
impl<'a, T: Ord + 'a> Sort<&'a mut [T]>
Sourcepub fn quick_lomuto(slice: &mut [T])
pub fn quick_lomuto(slice: &mut [T])
Sorts a slice
using quick sort with the Lomuto partition scheme.
It performs more swaps compared to the Hoare partition scheme.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
// Sort(&mut arr[..]).quick_lomuto();
Sort::quick_lomuto(&mut arr[..]);
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn quick_3way(slice: &mut [T])where
T: Clone,
pub fn quick_3way(slice: &mut [T])where
T: Clone,
Sorts a slice
using quick sort with the Three way partition scheme.
It is more efficient when dealing with duplicate elements.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort::quick_3way(&mut arr);
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn quick_hoare(slice: &mut [T])where
T: Clone,
pub fn quick_hoare(slice: &mut [T])where
T: Clone,
Sorts a slice
using quick sort with the Hoare partition scheme.
It performs fewer swaps compared to the Lomuto partition scheme.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort::quick_hoare(&mut arr);
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Source§impl<const N: usize> Sort<[i8; N]>
Implement const sorting methods for arrays of primitives.
impl<const N: usize> Sort<[i8; N]>
Implement const sorting methods for arrays of primitives.
Sourcepub const fn bubble_array(self) -> [i8; N]
Available on crate feature _sort_i8
only.
pub const fn bubble_array(self) -> [i8; N]
_sort_i8
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [i8; N]
Available on crate feature _sort_i8
only.
pub const fn insertion_array(self) -> [i8; N]
_sort_i8
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [i8; N]
Available on crate feature _sort_i8
only.
pub const fn selection_array(self) -> [i8; N]
_sort_i8
only.Returns a copied sorted array using insertion sort.
Source§impl<const N: usize> Sort<[i16; N]>
Implement const sorting methods for arrays of primitives.
impl<const N: usize> Sort<[i16; N]>
Implement const sorting methods for arrays of primitives.
Sourcepub const fn bubble_array(self) -> [i16; N]
Available on crate feature _sort_i16
only.
pub const fn bubble_array(self) -> [i16; N]
_sort_i16
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [i16; N]
Available on crate feature _sort_i16
only.
pub const fn insertion_array(self) -> [i16; N]
_sort_i16
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [i16; N]
Available on crate feature _sort_i16
only.
pub const fn selection_array(self) -> [i16; N]
_sort_i16
only.Returns a copied sorted array using insertion sort.
Source§impl<const N: usize> Sort<[i32; N]>
Implement const sorting methods for arrays of primitives.
impl<const N: usize> Sort<[i32; N]>
Implement const sorting methods for arrays of primitives.
Sourcepub const fn bubble_array(self) -> [i32; N]
Available on crate feature _sort_i32
only.
pub const fn bubble_array(self) -> [i32; N]
_sort_i32
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [i32; N]
Available on crate feature _sort_i32
only.
pub const fn insertion_array(self) -> [i32; N]
_sort_i32
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [i32; N]
Available on crate feature _sort_i32
only.
pub const fn selection_array(self) -> [i32; N]
_sort_i32
only.Returns a copied sorted array using insertion sort.
Source§impl<const N: usize> Sort<[i64; N]>
Implement const sorting methods for arrays of primitives.
impl<const N: usize> Sort<[i64; N]>
Implement const sorting methods for arrays of primitives.
Sourcepub const fn bubble_array(self) -> [i64; N]
Available on crate feature _sort_i64
only.
pub const fn bubble_array(self) -> [i64; N]
_sort_i64
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [i64; N]
Available on crate feature _sort_i64
only.
pub const fn insertion_array(self) -> [i64; N]
_sort_i64
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [i64; N]
Available on crate feature _sort_i64
only.
pub const fn selection_array(self) -> [i64; N]
_sort_i64
only.Returns a copied sorted array using insertion sort.
Source§impl<const N: usize> Sort<[i128; N]>
Implement const sorting methods for arrays of primitives.
impl<const N: usize> Sort<[i128; N]>
Implement const sorting methods for arrays of primitives.
Sourcepub const fn bubble_array(self) -> [i128; N]
Available on crate feature _sort_i128
only.
pub const fn bubble_array(self) -> [i128; N]
_sort_i128
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [i128; N]
Available on crate feature _sort_i128
only.
pub const fn insertion_array(self) -> [i128; N]
_sort_i128
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [i128; N]
Available on crate feature _sort_i128
only.
pub const fn selection_array(self) -> [i128; N]
_sort_i128
only.Returns a copied sorted array using insertion sort.
Source§impl<const N: usize> Sort<[isize; N]>
Implement const sorting methods for arrays of primitives.
impl<const N: usize> Sort<[isize; N]>
Implement const sorting methods for arrays of primitives.
Sourcepub const fn bubble_array(self) -> [isize; N]
Available on crate feature _sort_isize
only.
pub const fn bubble_array(self) -> [isize; N]
_sort_isize
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [isize; N]
Available on crate feature _sort_isize
only.
pub const fn insertion_array(self) -> [isize; N]
_sort_isize
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [isize; N]
Available on crate feature _sort_isize
only.
pub const fn selection_array(self) -> [isize; N]
_sort_isize
only.Returns a copied sorted array using insertion sort.
Source§impl<const N: usize> Sort<[u8; N]>
impl<const N: usize> Sort<[u8; N]>
Sourcepub const fn bubble_array(self) -> [u8; N]
Available on crate feature _sort_u8
only.
pub const fn bubble_array(self) -> [u8; N]
_sort_u8
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [u8; N]
Available on crate feature _sort_u8
only.
pub const fn insertion_array(self) -> [u8; N]
_sort_u8
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [u8; N]
Available on crate feature _sort_u8
only.
pub const fn selection_array(self) -> [u8; N]
_sort_u8
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[u16; N]>
impl<const N: usize> Sort<[u16; N]>
Sourcepub const fn bubble_array(self) -> [u16; N]
Available on crate feature _sort_u16
only.
pub const fn bubble_array(self) -> [u16; N]
_sort_u16
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [u16; N]
Available on crate feature _sort_u16
only.
pub const fn insertion_array(self) -> [u16; N]
_sort_u16
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [u16; N]
Available on crate feature _sort_u16
only.
pub const fn selection_array(self) -> [u16; N]
_sort_u16
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[u32; N]>
impl<const N: usize> Sort<[u32; N]>
Sourcepub const fn bubble_array(self) -> [u32; N]
Available on crate feature _sort_u32
only.
pub const fn bubble_array(self) -> [u32; N]
_sort_u32
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [u32; N]
Available on crate feature _sort_u32
only.
pub const fn insertion_array(self) -> [u32; N]
_sort_u32
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [u32; N]
Available on crate feature _sort_u32
only.
pub const fn selection_array(self) -> [u32; N]
_sort_u32
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[u64; N]>
impl<const N: usize> Sort<[u64; N]>
Sourcepub const fn bubble_array(self) -> [u64; N]
Available on crate feature _sort_u64
only.
pub const fn bubble_array(self) -> [u64; N]
_sort_u64
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [u64; N]
Available on crate feature _sort_u64
only.
pub const fn insertion_array(self) -> [u64; N]
_sort_u64
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [u64; N]
Available on crate feature _sort_u64
only.
pub const fn selection_array(self) -> [u64; N]
_sort_u64
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[u128; N]>
impl<const N: usize> Sort<[u128; N]>
Sourcepub const fn bubble_array(self) -> [u128; N]
Available on crate feature _sort_u128
only.
pub const fn bubble_array(self) -> [u128; N]
_sort_u128
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [u128; N]
Available on crate feature _sort_u128
only.
pub const fn insertion_array(self) -> [u128; N]
_sort_u128
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [u128; N]
Available on crate feature _sort_u128
only.
pub const fn selection_array(self) -> [u128; N]
_sort_u128
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[usize; N]>
impl<const N: usize> Sort<[usize; N]>
Sourcepub const fn bubble_array(self) -> [usize; N]
Available on crate feature _sort_usize
only.
pub const fn bubble_array(self) -> [usize; N]
_sort_usize
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [usize; N]
Available on crate feature _sort_usize
only.
pub const fn insertion_array(self) -> [usize; N]
_sort_usize
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [usize; N]
Available on crate feature _sort_usize
only.
pub const fn selection_array(self) -> [usize; N]
_sort_usize
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[f32; N]>
impl<const N: usize> Sort<[f32; N]>
Sourcepub const fn bubble_array(self) -> [f32; N]
Available on crate feature _sort_f32
only.
pub const fn bubble_array(self) -> [f32; N]
_sort_f32
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [f32; N]
Available on crate feature _sort_f32
only.
pub const fn insertion_array(self) -> [f32; N]
_sort_f32
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [f32; N]
Available on crate feature _sort_f32
only.
pub const fn selection_array(self) -> [f32; N]
_sort_f32
only.Returns a copied sorted array using selection sort.
Source§impl<const N: usize> Sort<[f64; N]>
impl<const N: usize> Sort<[f64; N]>
Sourcepub const fn bubble_array(self) -> [f64; N]
Available on crate feature _sort_f64
only.
pub const fn bubble_array(self) -> [f64; N]
_sort_f64
only.Returns a copied sorted array using bubble sort.
Sourcepub const fn insertion_array(self) -> [f64; N]
Available on crate feature _sort_f64
only.
pub const fn insertion_array(self) -> [f64; N]
_sort_f64
only.Returns a copied sorted array using insertion sort.
Sourcepub const fn selection_array(self) -> [f64; N]
Available on crate feature _sort_f64
only.
pub const fn selection_array(self) -> [f64; N]
_sort_f64
only.Returns a copied sorted array using selection sort.
Auto Trait Implementations§
impl<T> Freeze for Sort<T>where
T: Freeze,
impl<T> RefUnwindSafe for Sort<T>where
T: RefUnwindSafe,
impl<T> Send for Sort<T>where
T: Send,
impl<T> Sync for Sort<T>where
T: Sync,
impl<T> Unpin for Sort<T>where
T: Unpin,
impl<T> UnwindSafe for Sort<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize ⓘ
fn byte_align(&self) -> usize ⓘ
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> usize ⓘ
Source§fn mem_size_of_val(&self) -> usize ⓘ
fn mem_size_of_val(&self) -> usize ⓘ
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.