#sorting #const #const-sort

compile_time_sort

Sort arrays and slices of primitives in const contexts

8 releases

new 0.2.4 Dec 18, 2024
0.2.3 Dec 18, 2024
0.1.2 Dec 16, 2024

#687 in Algorithms

Download history 237/week @ 2024-12-11

237 downloads per month

MIT/Apache

21KB
485 lines

compile_time_sort

Crates.io Version Docs.rs Documentation Github Repository Link GitHub Actions Workflow Status

This 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 arrays of other types are sorted with quicksort.

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.

Examples

Sort an array by value:

use const_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 an array by reference:

use const_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]);

Features

sort_slices: enables the sort_*_slice functions and raises the MSRV of the crate to 1.83.0.


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.

No runtime deps

Features