Expand description
§compile_time_sort
This small crate provides functions for sorting arrays and slices of primitives in const contexts.
Arrays and slices of bools, u8s, and i8s are sorted with counting sort while other types
are sorted with introsort.
This implementation is usable on Rust version 1.56.0,
before the const_trait_impl feature is stabilized.
This means that it unfortunately can not be generic,
and so there are separate functions for every primitive type.
Functions with the naming convention into_sorted_*_array take an array by value,
and functions with the naming convention sort_*_slice take a mutable reference to a slice.
The functions that sort slices by reference are only available on Rust versions 1.83 and above,
as are the functions that sort floats as they need {float}::to_bits
to be const in order to generate a total ordering in accordance with {float}::total_cmp.
§Examples
Sort an array by value:
use compile_time_sort::into_sorted_i32_array;
const ARRAY: [i32; 5] = [-3, 3, 2, i32::MAX, 0];
const SORTED_ARRAY: [i32; 5] = into_sorted_i32_array(ARRAY);
assert_eq!(SORTED_ARRAY, [-3, 0, 2, 3, i32::MAX]);Sort by reference:
use compile_time_sort::sort_i32_slice;
const SORTED_ARRAY: [i32; 5] = {
let mut arr = [5, i32::MIN, 0, -2, 0];
sort_i32_slice(&mut arr);
arr
};
assert_eq!(SORTED_ARRAY, [i32::MIN, -2, 0, 0, 5]);§License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
§Features
nested: enables the functions that sort slices of slices and arrays of slices.
Functions§
- into_
sorted_ bool_ array - Sorts the given array of
bools using the counting sort algorithm and returns it. - into_
sorted_ bool_ slice_ array - Sorts the given array of
&[bool]s using the introsort algorithm and returns it. - into_
sorted_ char_ array - Sorts the given array of
chars using the introsort algorithm and returns it. - into_
sorted_ char_ slice_ array - Sorts the given array of
&[char]s using the introsort algorithm and returns it. - into_
sorted_ f32_ array - Sorts the given array of
f32s using the introsort algorithm and returns it. - into_
sorted_ f32_ slice_ array - Sorts the given array of
&[f32]s using the introsort algorithm and returns it. - into_
sorted_ f64_ array - Sorts the given array of
f64s using the introsort algorithm and returns it. - into_
sorted_ f64_ slice_ array - Sorts the given array of
&[f64]s using the introsort algorithm and returns it. - into_
sorted_ i8_ array - Sorts the given array of
i8s using the counting sort algorithm and returns it. - into_
sorted_ i8_ slice_ array - Sorts the given array of
&[i8]s using the introsort algorithm and returns it. - into_
sorted_ i16_ array - Sorts the given array of
i16s using the introsort algorithm and returns it. - into_
sorted_ i16_ slice_ array - Sorts the given array of
&[i16]s using the introsort algorithm and returns it. - into_
sorted_ i32_ array - Sorts the given array of
i32s using the introsort algorithm and returns it. - into_
sorted_ i32_ slice_ array - Sorts the given array of
&[i32]s using the introsort algorithm and returns it. - into_
sorted_ i64_ array - Sorts the given array of
i64s using the introsort algorithm and returns it. - into_
sorted_ i64_ slice_ array - Sorts the given array of
&[i64]s using the introsort algorithm and returns it. - into_
sorted_ i128_ array - Sorts the given array of
i128s using the introsort algorithm and returns it. - into_
sorted_ i128_ slice_ array - Sorts the given array of
&[i128]s using the introsort algorithm and returns it. - into_
sorted_ isize_ array - Sorts the given array of
isizes using the introsort algorithm and returns it. - into_
sorted_ isize_ slice_ array - Sorts the given array of
&[isize]s using the introsort algorithm and returns it. - into_
sorted_ str_ array - Sorts the given array of
strs using the introsort algorithm and returns it. - into_
sorted_ u8_ array - Sorts the given array of
u8s using the counting sort algorithm and returns it. - into_
sorted_ u8_ slice_ array - Sorts the given array of
&[u8]s using the introsort algorithm and returns it. - into_
sorted_ u16_ array - Sorts the given array of
u16s using the introsort algorithm and returns it. - into_
sorted_ u16_ slice_ array - Sorts the given array of
&[u16]s using the introsort algorithm and returns it. - into_
sorted_ u32_ array - Sorts the given array of
u32s using the introsort algorithm and returns it. - into_
sorted_ u32_ slice_ array - Sorts the given array of
&[u32]s using the introsort algorithm and returns it. - into_
sorted_ u64_ array - Sorts the given array of
u64s using the introsort algorithm and returns it. - into_
sorted_ u64_ slice_ array - Sorts the given array of
&[u64]s using the introsort algorithm and returns it. - into_
sorted_ u128_ array - Sorts the given array of
u128s using the introsort algorithm and returns it. - into_
sorted_ u128_ slice_ array - Sorts the given array of
&[u128]s using the introsort algorithm and returns it. - into_
sorted_ usize_ array - Sorts the given array of
usizes using the introsort algorithm and returns it. - into_
sorted_ usize_ slice_ array - Sorts the given array of
&[usize]s using the introsort algorithm and returns it. - sort_
bool_ slice - Sorts the given slice of
bools using the counting sort algorithm. - sort_
bool_ slice_ slice - Sorts the given slice of
&[bool]s using the introsort algorithm. - sort_
char_ slice - Sorts the given slice of
chars using the introsort algorithm. - sort_
char_ slice_ slice - Sorts the given slice of
&[char]s using the introsort algorithm. - sort_
f32_ slice - Sorts the given slice of
f32s using the introsort algorithm. - sort_
f32_ slice_ slice - Sorts the given slice of
&[f32]s using the introsort algorithm. - sort_
f64_ slice - Sorts the given slice of
f64s using the introsort algorithm. - sort_
f64_ slice_ slice - Sorts the given slice of
&[f64]s using the introsort algorithm. - sort_
i8_ slice - Sorts the given slice of
i8s using the counting sort algorithm. - sort_
i8_ slice_ slice - Sorts the given slice of
&[i8]s using the introsort algorithm. - sort_
i16_ slice - Sorts the given slice of
i16s using the introsort algorithm. - sort_
i16_ slice_ slice - Sorts the given slice of
&[i16]s using the introsort algorithm. - sort_
i32_ slice - Sorts the given slice of
i32s using the introsort algorithm. - sort_
i32_ slice_ slice - Sorts the given slice of
&[i32]s using the introsort algorithm. - sort_
i64_ slice - Sorts the given slice of
i64s using the introsort algorithm. - sort_
i64_ slice_ slice - Sorts the given slice of
&[i64]s using the introsort algorithm. - sort_
i128_ slice - Sorts the given slice of
i128s using the introsort algorithm. - sort_
i128_ slice_ slice - Sorts the given slice of
&[i128]s using the introsort algorithm. - sort_
isize_ slice - Sorts the given slice of
isizes using the introsort algorithm. - sort_
isize_ slice_ slice - Sorts the given slice of
&[isize]s using the introsort algorithm. - sort_
str_ slice - Sorts the given slice of
strs using the introsort algorithm. - sort_
u8_ slice - Sorts the given slice of
u8s using the counting sort algorithm. - sort_
u8_ slice_ slice - Sorts the given slice of
&[u8]s using the introsort algorithm. - sort_
u16_ slice - Sorts the given slice of
u16s using the introsort algorithm. - sort_
u16_ slice_ slice - Sorts the given slice of
&[u16]s using the introsort algorithm. - sort_
u32_ slice - Sorts the given slice of
u32s using the introsort algorithm. - sort_
u32_ slice_ slice - Sorts the given slice of
&[u32]s using the introsort algorithm. - sort_
u64_ slice - Sorts the given slice of
u64s using the introsort algorithm. - sort_
u64_ slice_ slice - Sorts the given slice of
&[u64]s using the introsort algorithm. - sort_
u128_ slice - Sorts the given slice of
u128s using the introsort algorithm. - sort_
u128_ slice_ slice - Sorts the given slice of
&[u128]s using the introsort algorithm. - sort_
usize_ slice - Sorts the given slice of
usizes using the introsort algorithm. - sort_
usize_ slice_ slice - Sorts the given slice of
&[usize]s using the introsort algorithm.