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
//! `radsort` is a radix sort implementation for sorting by scalar keys
//! (integers, floats, chars, bools).
//! 
//! All built-in scalar types can be used as sorting keys: Booleans, characters,
//! integers, and floating point-numbers. To sort by multiple keys, put them in
//! a tuple, starting from the most significant key. See [`Key`] for a full list
//! of supported keys.
//! 
//! - best and worst-case running time is `O(n)` – see [benchmarks] for more
//! detailed performance characteristics
//! - space complexity is `O(n)` – direct sort allocates temporary storage the
//! size of the slice, for indirect see [`sort_by_cached_key`]
//! - stable, i.e. does not reorder equal elements
//! - uses `#![no_std]`, but needs an allocator
//! 
//! This sort can be several times faster than `slice::sort` and
//! `slice::sort_unstable`, typically on large slices (hundreds of elements or
//! more). It performs worse on short slices and when using wide keys
//! (16 bytes). See [benchmarks] to get a better picture of the performance
//! characteristics.
//! 
//! `radsort` is an implementation of LSB radix sort, using counting sort to
//! sort the slice by each digit (byte) of the key. As an optimization, the
//! slice is sorted only by digits which differ between the keys. See the
//! [`unopt`] module for more details and functions which don't use this
//! optimization.
//! 
//! This implementation is based on radix sort by Pierre Terdiman,
//! published at
//! [http://codercorner.com/RadixSortRevisited.htm](http://codercorner.com/RadixSortRevisited.htm),
//! with select optimizations published by Michael Herf at
//! [http://stereopsis.com/radix.html](http://stereopsis.com/radix.html).
//! 
//! # Floating-point numbers
//!
//! Floating-point number keys are effectively sorted according to their partial
//! order (see [`PartialOrd`]), with `NaN` values at the beginning (before the
//! negative infinity) and at the end (after the positive infinity), depending
//! on the sign bit of each `NaN`.
//! 
//! # Examples
//! 
//! Slices of scalar types (integers, floating-point numbers, Booleans, and
//! characters) can be sorted directly:
//! ```rust
//! let mut data = [2i32, -1, 1, 0, -2];
//! 
//! radsort::sort(&mut data);
//! 
//! assert_eq!(data, [-2, -1, 0, 1, 2]);
//! ```
//! 
//! Use a key extraction function to sort other types:
//! ```rust
//! let mut friends = ["Punchy", "Isabelle", "Sly", "Puddles", "Gladys"];
//! 
//! // sort by the length of the string in bytes
//! radsort::sort_by_key(&mut friends, |s| s.len());
//! 
//! assert_eq!(friends, ["Sly", "Punchy", "Gladys", "Puddles", "Isabelle"]);
//! ```
//! 
//! To sort by two or more keys, put them in a tuple, starting with the most
//! significant key:
//! ```rust
//! # #[derive(Debug, PartialEq)]
//! struct Height { feet: u8, inches: u8, }
//! 
//! let mut heights = [
//!     Height { feet: 6, inches: 1 },
//!     Height { feet: 5, inches: 9 },
//!     Height { feet: 6, inches: 0 },
//! ];
//! 
//! // sort by feet, if feet are equal, sort by inches
//! radsort::sort_by_key(&mut heights, |h| (h.feet, h.inches));
//! 
//! assert_eq!(heights, [
//!     Height { feet: 5, inches: 9 },
//!     Height { feet: 6, inches: 0 },
//!     Height { feet: 6, inches: 1 },
//! ]);
//! ```
//! 
//! [`Key`]: ./trait.Key.html
//! [`unopt`]: ./unopt/index.html
//! [benchmarks]: https://github.com/JakubValtar/radsort/wiki/Benchmarks
//! [`sort_by_cached_key`]: ./fn.sort_by_cached_key.html
//! [`PartialOrd`]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html

#![warn(clippy::all)]

#![no_std]

extern crate alloc;

use alloc::vec::Vec;

mod scalar;
mod sort;

use scalar::Scalar;
use sort::RadixKey;

/// Sorts the slice.
/// 
/// Slice elements can be any scalar type. See [`Key`] for a full list.
/// 
/// This sort is stable (i.e., does not reorder equal elements) and `O(w n)`,
/// where `w` is the size of the key in bytes.
/// 
/// Allocates temporary storage the size of the slice.
/// 
/// # Examples
/// ```rust
/// let mut data = [5i32, -1, 3, 15, -42];
/// 
/// radsort::sort(&mut data);
/// 
/// assert_eq!(data, [-42, -1, 3, 5, 15]);
/// ```
/// [`Key`]: trait.Key.html
#[inline]
pub fn sort<T: Key>(slice: &mut [T]) {
    Key::sort_by_key(slice, |v| *v, false);
}

/// Sorts the slice using a key extraction function.
/// 
/// Key can be any scalar type. See [`Key`] for a full list.
/// 
/// This sort is stable (i.e., does not reorder equal elements) and `O(w m n)`,
/// where the key function is `O(m)` and `w` is the size of the key in bytes.
/// 
/// Allocates temporary storage the size of the slice.
/// 
/// See [`sort_by_cached_key`] if you use expensive key function or if you need
/// to sort large elements.
/// 
/// # Panics
/// 
/// Can panic if the key function returns different keys for the same element
/// when called repeatedly. The panic is on a best-effort basis. In case of
/// panic, the order of elements in the slice is not specified.
/// 
/// # Examples
/// 
/// ```rust
/// let mut friends = ["Punchy", "Isabelle", "Sly", "Puddles", "Gladys"];
/// 
/// // sort by the length of the string in bytes
/// radsort::sort_by_key(&mut friends, |s| s.len());
/// 
/// assert_eq!(friends, ["Sly", "Punchy", "Gladys", "Puddles", "Isabelle"]);
/// ```
/// 
/// [`Key`]: trait.Key.html
/// [`sort_by_cached_key`]: fn.sort_by_cached_key.html
#[inline]
pub fn sort_by_key<T, F, K>(slice: &mut [T], mut key_fn: F)
    where F: FnMut(&T) -> K, K: Key
{
    Key::sort_by_key(slice, |t| key_fn(t), false);
}

/// Sorts the slice indirectly, using a key extraction function and caching the keys.
/// 
/// Key can be any scalar type. See [`Key`] for a full list.
/// 
/// This sort is stable (i.e., does not reorder equal elements) and
/// `O(m n + w n)`, where the key function is `O(m)`.
/// 
/// This function can be significantly faster for sorting by an expensive key
/// function or for sorting large elements. The keys are extracted, sorted, and
/// then the elements of the slice are reordered in-place. This saves CPU cycles
/// in case of an expensive key function and saves memory bandwidth in case of
/// large elements.
/// 
/// For sorting small elements by simple key functions (e.g., functions that are
/// property accesses or basic operations), [`sort_by_key`] is likely to be
/// faster.
/// 
/// In the worst case, allocates temporary storage in a `Vec<(K, usize)>` twice
/// the length of the slice.
/// 
/// # Examples
/// 
/// ```rust
/// let mut data = ["-6", "2", "15", "-1", "0"];
/// 
/// radsort::sort_by_cached_key(&mut data, |s| s.parse::<i32>().unwrap());
/// 
/// assert_eq!(data, ["-6", "-1", "0", "2", "15"]);
/// ```
/// 
/// [`Key`]: ./trait.Key.html
/// [`sort_by_key`]: fn.sort_by_key.html
#[inline]
pub fn sort_by_cached_key<T, F, K>(slice: &mut [T], key_fn: F)
    where F: FnMut(&T) -> K, K: Key
{
    sort_by_cached_key_internal(slice, key_fn, false);
}

/// Sorting functions which don't use optimizations based on the values
/// of the keys. Useful for benchmarks and consistent performance.
/// 
/// For each digit (byte) of the key, `radsort` reorders the slice once.
/// Functions in the crate root sort only by the bytes which differ between the
/// keys. This can lead to large differences in sorting time, based on the
/// values in the slice.
/// 
/// For example, sorting `u32` all less than `u8::MAX` will sort only by
/// the least significant byte and skip the three most significant bytes,
/// which are zero; this cuts the sorting time to roughly one quarter, plus
/// the overhead of analyzing the keys.
/// 
/// Unlike functions in the crate root, functions in this module don't use
/// this optimization and sort by all bytes of the key. This leads to worse but
/// more consistent performance. The effects of the CPU cache will still play a
/// role, but at least the number of executed instructions will not depend on
/// the values in the slice, only on the length of the slice and the width of
/// the key type.
pub mod unopt {

    use super::*;

    /// Version of [`sort`](../fn.sort.html) which does not skip digits (bytes).
    /// 
    /// See the [module documentation](./index.html) for more details.
    #[inline]
    pub fn sort<T: Key>(slice: &mut [T]) {
        Key::sort_by_key(slice, |v| *v, true);
    }
    
    /// Version of [`sort_by_key`](../fn.sort_by_key.html) which does not skip digits (bytes).
    /// 
    /// See the [module documentation](./index.html) for more details.
    #[inline]
    pub fn sort_by_key<T, F, K>(slice: &mut [T], mut key_fn: F)
    where F: FnMut(&T) -> K, K: Key
    {
        Key::sort_by_key(slice, |t| key_fn(t), true);
    }
    
    /// Version of [`sort_by_cached_key`](../fn.sort_by_cached_key.html) which does not skip digits (bytes).
    /// 
    /// See the [module documentation](./index.html) for more details.
    #[inline]
    pub fn sort_by_cached_key<T, F, K>(slice: &mut [T], key_fn: F)
        where F: FnMut(&T) -> K, K: Key
    {
        sort_by_cached_key_internal(slice, key_fn, true);
    }
}

#[inline]
fn sort_by_cached_key_internal<T, F, K>(slice: &mut [T], mut key_fn: F, unopt: bool)
    where F: FnMut(&T) -> K, K: Key
{
    // Adapted from std::slice::sort_by_cached_key

    macro_rules! radsort_by_cached_key {
        ($index:ty) => ({
            let mut indices: Vec<(K, $index)> = slice.iter()
                .map(|t| key_fn(t))
                .enumerate()
                .map(|(i, k)| (k, i as $index))
                .collect();

            Key::sort_by_key(&mut indices, |(k, _)| *k, unopt);

            for i in 0..slice.len() {
                let mut index = indices[i].1;
                while (index as usize) < i {
                    // The previous value was swapped somewhere else. The index to which
                    // the original value was swapped was marked into the index array.
                    // Follow the indices to find out where the original value ended up.
                    index = indices[index as usize].1;
                }
                // Mark down the index to which the current value goes
                indices[i].1 = index;
                slice.swap(i, index as usize);
            }
        })
    }

    let len = slice.len();
    if len < 2 {
        return;
    }

    let sz_u8    = core::mem::size_of::<(K, u8)>();
    let sz_u16   = core::mem::size_of::<(K, u16)>();
    let sz_u32   = core::mem::size_of::<(K, u32)>();
    let sz_usize = core::mem::size_of::<(K, usize)>();

    if sz_u8  < sz_u16   && len <= (core:: u8::MAX as usize + 1) { return radsort_by_cached_key!( u8) }
    if sz_u16 < sz_u32   && len <= (core::u16::MAX as usize + 1) { return radsort_by_cached_key!(u16) }
    if sz_u32 < sz_usize && len <= (core::u32::MAX as usize + 1) { return radsort_by_cached_key!(u32) }

    radsort_by_cached_key!(usize)
}

/// Types which can be used as sorting keys.
/// 
/// Implemented for all scalar types and their tuples.
/// 
/// Slices of types for which `Key` is implemented can be sorted directly using
/// [`sort`]. Slices of other types can be sorted using [`sort_by_key`] with a
/// key extraction function.
/// 
/// [`sort`]: fn.sort.html
/// [`sort_by_key`]: fn.sort_by_key.html
pub trait Key: Copy + private::Sealed {
    // If this crate didn't support tuples, this trait wouldn't be needed and
    // Scalar could be exposed directly to users as the `Key` trait.

    /// Sorts the slice using `Self` as the type of the key.
    /// 
    /// You shouldn't need to call this directly, use one of the functions in
    /// the [crate root](index.html#functions) instead.
    #[doc(hidden)]
    fn sort_by_key<T, F>(slice: &mut [T], key_fn: F, unopt: bool)
        where F: FnMut(&T) -> Self;
}

macro_rules! impl_for_scalar { ($($t:ty)*) => ($(
    impl Key for $t {
        #[doc(hidden)]
        #[inline]
        fn sort_by_key<T, F>(slice: &mut [T], mut key_fn: F, unopt: bool)
            where F: FnMut(&T) -> Self
        {
            RadixKey::radix_sort(slice, |t| key_fn(t).to_radix_key(), unopt);
        }
    }
)*) }

impl_for_scalar! {
    bool char
    u8 u16 u32 u64 u128 usize
    i8 i16 i32 i64 i128 isize
    f32 f64
}

impl<A: Key> Key for (A,) {
    #[doc(hidden)]
    #[inline]
    fn sort_by_key<T, F>(slice: &mut [T], mut key_fn: F, unopt: bool)
        where F: FnMut(&T) -> Self
    {
        A::sort_by_key(slice, |t| key_fn(t).0, unopt);
    }
}

impl<A: Key, B: Key> Key for (A, B) {
    #[doc(hidden)]
    #[inline]
    fn sort_by_key<T, F>(slice: &mut [T], mut key_fn: F, unopt: bool)
        where F: FnMut(&T) -> Self
    {
        B::sort_by_key(slice, |t| key_fn(t).1, unopt);
        A::sort_by_key(slice, |t| key_fn(t).0, unopt);
    }
}

impl<A: Key, B: Key, C: Key> Key for (A, B, C) {
    #[doc(hidden)]
    #[inline]
    fn sort_by_key<T, F>(slice: &mut [T], mut key_fn: F, unopt: bool)
        where F: FnMut(&T) -> Self
    {
        C::sort_by_key(slice, |t| key_fn(t).2, unopt);
        B::sort_by_key(slice, |t| key_fn(t).1, unopt);
        A::sort_by_key(slice, |t| key_fn(t).0, unopt);
    }
}

impl<A: Key, B: Key, C: Key, D: Key> Key for (A, B, C, D) {
    #[doc(hidden)]
    #[inline]
    fn sort_by_key<T, F>(slice: &mut [T], mut key_fn: F, unopt: bool)
        where F: FnMut(&T) -> Self
    {
        D::sort_by_key(slice, |t| key_fn(t).3, unopt);
        C::sort_by_key(slice, |t| key_fn(t).2, unopt);
        B::sort_by_key(slice, |t| key_fn(t).1, unopt);
        A::sort_by_key(slice, |t| key_fn(t).0, unopt);
    }
}

mod private {
    use super::*;
    /// This trait serves as a seal for the `Key` trait to prevent downstream
    /// implementations.
    pub trait Sealed {}
    macro_rules! sealed_impl { ($($t:ty)*) => ($(
        impl Sealed for $t {}
    )*) }
    sealed_impl! {
        ()
        bool char
        u8 u16 u32 u64 u128 usize
        i8 i16 i32 i64 i128 isize
        f32 f64
    }
    impl<A: Key> Sealed for (A,) {}
    impl<A: Key, B: Key> Sealed for (A, B) {}
    impl<A: Key, B: Key, C: Key> Sealed for (A, B, C) {}
    impl<A: Key, B: Key, C: Key, D: Key> Sealed for (A, B, C, D) {}
}