macro_rules! const_quicksort {
($(@$depth:literal,)? $data:expr) => { ... };
($(@$depth:literal,)? $data:expr, |$a:ident, $b:ident| $cmp:expr) => { ... };
($(@$depth:literal,)? $data:expr, $cmp:expr) => { ... };
}Expand description
Sorts a const array or mutable slice as a const expression using quicksort.
Can be called with just the data to be sorted, in which case elements are compared by a < b resulting in the smallest element at
the head of the data and the largest at the tail.
use sort_const::const_quicksort;
const U8S: &[u8] = &const_quicksort!([3, 1, 2]);
assert_eq!(U8S, [1, 2, 3]);Can also be called with a lambda-like expression (e.g. |a, b| a < b) where true identifies that a should come before b.
use sort_const::const_quicksort;
#[derive(Debug, PartialEq)]
struct Foo(u8);
const FOOS_MUT_REF: &[Foo] = &{
let mut foo = [Foo(1), Foo(2), Foo(4), Foo(3)];
const_quicksort!(foo.split_last_mut().expect("failed").1, |a, b| a.0 > b.0);
foo
};
assert_eq!(FOOS_MUT_REF, [4, 2, 1, 3].map(Foo));Can also be called with the name of a const function which must return a boolean and which will be evaluated over &data[a], &data[b].
use sort_const::const_quicksort;
#[derive(Debug, PartialEq)]
struct Foo(u8);
const fn gt(lhs: &Foo, rhs: &Foo) -> bool {
lhs.0 > rhs.0
}
const FOOS_MUT_REF: &[Foo] = &{
let mut foo = [Foo(1), Foo(2), Foo(4), Foo(3)];
const_quicksort!(foo.split_last_mut().expect("failed").1, gt);
foo
};
assert_eq!(FOOS_MUT_REF, [4, 2, 1, 3].map(Foo));The @depth parameter should only be used if you encounter a scenario where “stack overflows” start occuring.
ⓘ
use sort_const::const_quicksort;
const SORTED_ARRAY: &[u32] = &{
let mut data = [0_u32; 1000];
let mut i = 0;
while i < data.len() {
if i & 1 == 0 {
data[i] = i as u32;
}
i += 1;
}
const_quicksort!(@8, &mut data);
data
};