sort-const 1.0.1

Sort arrays and slices in const contexts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#![no_std]

//! A macro for sorting arrays and slices at compile time.
//!
//! ## Usage
//!
//! Just use the [`const_quicksort`] or [`const_shellsort`] macros.
//!
//! ```
//! use sort_const::const_quicksort;
//!
//! const fn lt(a: &u8, b: &u8) -> bool {
//!     *a < *b
//! }
//!
//! const A: &[u8] = &const_quicksort!([3, 1, 2]);
//! const B: &[u8] = &const_quicksort!([3, 1, 2], |a, b| *a < *b);
//! const C: &[u8] = &const_quicksort!([3, 1, 2], lt);
//!
//! assert_eq!(A, [1, 2, 3]);
//! assert_eq!(B, [1, 2, 3]);
//! assert_eq!(C, [1, 2, 3]);
//! ```

use core::mem::forget;

/// Used for our `call stack`
#[doc(hidden)]
pub use arrayvec_const::ArrayVec;

/// A helper to turn everything into mutable slices
#[doc(hidden)]
pub struct Wrapper<T>(pub T);
impl<T, const N: usize> Wrapper<[T; N]> {
    #[inline(always)]
    pub const fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.0
    }
}
impl<T> Wrapper<&'_ mut [T]> {
    #[inline(always)]
    pub const fn as_mut_slice(&mut self) -> &mut [T] {
        self.0
    }
}
impl<T, const N: usize> Wrapper<&'_ mut [T; N]> {
    #[inline(always)]
    pub const fn as_mut_slice(&mut self) -> &mut [T] {
        self.0
    }
}
impl<T, const N: usize> Wrapper<&'_ [T; N]> {
    #[inline(always)]
    pub const fn as_mut_slice(&mut self) -> &mut [T] {
        panic!("Cannot sort non-mut `&`. Did you mean to use `&mut`?")
    }
}

#[doc(hidden)]
#[track_caller]
#[inline]
pub const fn expect_push<T, const LEN: usize>(v: &mut ArrayVec<T, LEN>, element: T) {
    let res = v.try_push(element);
    if res.is_err() {
        panic!("ran out of capacity");
    }
    forget(res);
}

/// Some nice gaps for shellsort
#[doc(hidden)]
pub const A366726: [usize; 32] = [
    1,
    4,
    9,
    20,
    45,
    102,
    230,
    516,
    1158,
    2599,
    5831,
    13082,
    29351,
    65853,
    147748,
    331490,
    743735,
    1668650,
    3743800,
    8399623,
    18845471,
    42281871,
    94863989,
    212837706,
    477524607,
    1071378536,
    2403754591,
    5393085583,
    12099975682,
    27147615084,
    60908635199,
    136655165852,
];

/// 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.
/// ```compile_fail
/// 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
/// };
/// ```
///
#[macro_export]
macro_rules! const_quicksort {
    ($(@$depth:literal,)? $data:expr) => {{
        macro_rules! cmp {
            ($a:expr, $b:expr) => {{ $a < $b }};
        }
        $crate::const_quicksort_adv!($(@$depth,)? $data, cmp)
    }};
    ($(@$depth:literal,)? $data:expr, |$a:ident, $b:ident| $cmp:expr) => {{
        macro_rules! cmp {
            ($a_:expr, $b_:expr) => {{
                let ($a, $b) = (&$a_, &$b_);
                $cmp
            }};
        }
        $crate::const_quicksort_adv!($(@$depth,)? $data, cmp)
    }};
    ($(@$depth:literal,)? $data:expr, $cmp:expr) => {{
        macro_rules! cmp {
            ($a:expr, $b:expr) => {{ $cmp(&$a, &$b) }};
        }
        $crate::const_quicksort_adv!($(@$depth,)? $data, cmp)
    }};
}

/// Sorts a `const` array or mutable slice as a `const` expression using quicksort.
///
/// The optional `@` prefix parameter is the max stack size for recursion.
///
/// The first parameter is the data to be sorted.
///
/// The second parameter is the path identifying the macro which should be invoked to compare elements `cmp!(a, b)`
#[macro_export]
macro_rules! const_quicksort_adv {
    ($data:expr, $cmp:path) => { $crate::const_quicksort_adv!(@1024, $data, $cmp) };
    (@$depth:literal, $data:expr, $cmp:path) => {{
        let mut data = $crate::Wrapper($data);
        let mut slices = $crate::ArrayVec::<&mut [_], $depth>::new();
        $crate::expect_push(&mut slices, data.as_mut_slice());
        while let Some(data) = slices.pop() {
            match data.len() {
                0 | 1 => continue,
                2 => {
                    if !{$cmp!(data[0], data[1])} {
                        data.swap(0, 1);
                    }
                    continue;
                },
                _ => {}
            }


            let (pivot, rest) = data
                .split_first_mut()
                .expect("slice is not empty, as verified above");

            let mut left = 0;
            let mut right = rest.len() - 1;
            while left <= right {
                if {$cmp!(rest[left], *pivot)} {
                    left += 1;
                } else if !{$cmp!(rest[right], *pivot)} {
                    if right == 0 {
                        break;
                    }
                    right -= 1;
                } else {
                    rest.swap(left, right);
                    left += 1;
                    if right == 0 {
                        break;
                    }
                    right -= 1;
                }
            }

            data.swap(0, left);
            let (left, right) = data.split_at_mut(left);
            match right.split_first_mut() {
                None => {
                    $crate::expect_push(&mut slices, left);
                }
                Some((_pivot, right)) if left.len() >= right.len() => {
                    $crate::expect_push(&mut slices, left);
                    $crate::expect_push(&mut slices, right);
                }
                Some((_pivot, right)) => {
                    $crate::expect_push(&mut slices, right);
                    $crate::expect_push(&mut slices, left);
                }
            }
        }
        ::core::mem::forget(slices);

        data.0
    }};
}

/// 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_shellsort;
///
/// const U8S: &[u8] = &const_shellsort!([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_shellsort;
///
/// #[derive(Debug, PartialEq)]
/// struct Foo(u8);
///
/// const FOOS_MUT_REF: &[Foo] = &{
///     let mut foo = [Foo(1), Foo(2), Foo(4), Foo(3)];
///     const_shellsort!(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]`.
#[macro_export]
macro_rules! const_shellsort {
    ($(@$seq:expr,)? $data:expr) => {{
        macro_rules! cmp {
            ($a:expr, $b:expr) => {{ $a < $b }};
        }
        $crate::const_shellsort_adv!($(@$seq,)? $data, cmp)
    }};
    ($(@$seq:expr,)? $data:expr, |$a:ident, $b:ident| $cmp:expr) => {{
        macro_rules! cmp {
            ($a_:expr, $b_:expr) => {{
                let ($a, $b) = (&$a_, &$b_);
                $cmp
            }};
        }
        $crate::const_shellsort_adv!($(@$seq,)? $data, cmp)
    }};
    ($(@$seq:expr,)? $data:expr, $cmp:expr) => {{
        macro_rules! cmp {
            ($a:expr, $b:expr) => {{ $cmp(&$a, &$b) }};
        }
        $crate::const_shellsort_adv!($(@$seq,)? $data, cmp)
    }};
}

/// Sorts a `const` array or mutable slice as a `const` expression using shellsort.
///
/// The optional `@` prefix parameter is the sort.
///
/// The first parameter is the data to be sorted.
///
/// The second parameter is the path identifying the macro which should be invoked to compare elements `cmp!(a, b)`
#[macro_export]
macro_rules! const_shellsort_adv {
    ($data:expr, $cmp:path) => { $crate::const_shellsort_adv!(@$crate::A366726, $data, $cmp) };
    (@$seq:expr, $data:expr, $cmp:path) => {{
        const GAPS: &[usize] = &$seq;
        const GAPS_LEN: usize = GAPS.len();

        let mut data = $crate::Wrapper($data);

        let arr = data.as_mut_slice();
        let n = arr.len();
        let mut gaps = {
            let mut gaps = $crate::ArrayVec::<usize, GAPS_LEN>::new();
            let mut i = 0;
            while i < GAPS_LEN && GAPS[i] < arr.len() {
                $crate::expect_push(&mut gaps, GAPS[i]);
                i += 1;
            }
            gaps
        };

        // Start with the largest gap and work down to a gap of 1
        while let Some(gap) = gaps.pop() {
            // Do a gapped insertion sort for every element in gaps
            // Each loop leaves a[0..gap-1] in gapped order
            let mut i = gap;
            while i < arr.len() {
                // save a[i] in temp and make a hole at position i
                // This is safe because we are holding a temporary over a series of swaps.
                let temp = unsafe { ::core::ptr::read(&arr[i]) };

                // shift earlier gap-sorted elements up until the correct location for a[i] is found
                let mut j = i;
                while j >= gap && {$cmp!(temp, arr[j - gap])} {
                    // This is safe because the previous item is either saved away in `temp` or
                    // was previously copied to it's new location
                    unsafe { ::core::ptr::copy(&arr[j - gap], &mut arr[j], 1); }
                    j -= gap
                }

                // put temp (the original a[i]) in its correct location
                // This is safe because we writing the last swap.
                unsafe { ::core::ptr::write(&mut arr[j], temp); }
                i += 1;
            }
        }
        core::mem::forget(gaps);
        data.0
    }};
}

#[cfg(test)]
mod test {
    #[derive(Debug)]
    struct Foo(u8);

    const fn lt(a: &u8, b: &u8) -> bool {
        *a < *b
    }

    const U8S: &[u8] = &const_quicksort!([3, 2, 1]);
    const U8S_FN: &[u8] = &const_quicksort!([3, 2, 1], lt);
    const FOOS: &[Foo] = &const_quicksort!([Foo(1), Foo(2), Foo(4), Foo(3)], |a, b| a.0 > b.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, |a, b| a.0 > b.0);
        foo
    };

    #[test]
    fn test_u8() {
        assert_eq!(U8S, [1, 2, 3]);
    }

    #[test]
    fn test_u8_fn() {
        assert_eq!(U8S_FN, [1, 2, 3]);
    }

    #[test]
    fn test_foo() {
        assert!(FOOS.iter().map(|v| v.0).eq([4, 3, 2, 1]));
        assert!(FOOS_MUT_REF.iter().map(|v| v.0).eq([4, 2, 1, 3]));
    }
}