summaryrefslogtreecommitdiff
path: root/kattis-kth/alginda-quicksort/src/radix_sort.rs
blob: 2f8d32a5b8cd1f17688935864659eb7d12c19919 (plain) (blame)
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
use std::mem;

pub trait RadixFilters: 'static {
    const FILTERS: &'static [fn(&Self) -> u8];
}

impl RadixFilters for u32 {
    const FILTERS: &'static [fn(&Self) -> u8] = &[
        |x| ((x & 0x000000ff) >> (8 * 0)) as _,
        |x| ((x & 0x0000ff00) >> (8 * 1)) as _,
        |x| ((x & 0x00ff0000) >> (8 * 2)) as _,
        |x| ((x & 0xff000000) >> (8 * 3)) as _,
    ];
}

impl RadixFilters for i32 {
    const FILTERS: &'static [fn(&Self) -> u8] = &[
        |x| ((x & 0x000000ff) >> (8 * 0)) as _,
        |x| ((x & 0x0000ff00) >> (8 * 1)) as _,
        |x| ((x & 0x00ff0000) >> (8 * 2)) as _,
        |x| (((x & 0xff000000u32 as i32) ^ (1 << 31)) >> (8 * 3)) as _,
    ];
}

impl RadixFilters for u64 {
    const FILTERS: &'static [fn(&Self) -> u8] = &[
        |x| ((x & 0x00000000000000ff) >> (8 * 0)) as _,
        |x| ((x & 0x000000000000ff00) >> (8 * 1)) as _,
        |x| ((x & 0x0000000000ff0000) >> (8 * 2)) as _,
        |x| ((x & 0x00000000ff000000) >> (8 * 3)) as _,
        |x| ((x & 0x000000ff00000000) >> (8 * 4)) as _,
        |x| ((x & 0x0000ff0000000000) >> (8 * 5)) as _,
        |x| ((x & 0x00ff000000000000) >> (8 * 6)) as _,
        |x| ((x & 0xff00000000000000) >> (8 * 7)) as _,
    ];
}

pub fn radix_sort_with<T>(xs: &mut [T], temp: &mut [T])
where
    T: Default + Copy + Clone + RadixFilters,
{
    // The first thing that happens it that these two are swapped so thats why this seems backwards
    let mut output = xs;
    let mut input = temp;
    debug_assert!(T::FILTERS.len() % 2 == 0);
    for f in T::FILTERS {
        mem::swap(&mut input, &mut output);

        let mut counts = [0; 256];
        for x in input.iter() {
            counts[f(x) as usize] += 1;
        }
        let mut sum = 0;
        for c in &mut counts {
            sum += *c;
            *c = sum;
        }
        for x in input.iter_mut().rev() {
            let fx = f(x) as usize;
            counts[fx] -= 1;
            output[counts[fx]] = *x;
        }
    }
}

pub fn radix_sort<T>(xs: &mut [T])
where
    T: Default + Copy + Clone + RadixFilters,
{
    let n = xs.len();
    radix_sort_with(xs, vec![Default::default(); n].as_mut_slice());
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_rand() {
        let mut r = rand_pcg::Pcg32::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7);
        let mut xs: Vec<u32> = (0..1_000_000).map(|_| r.next_u32()).collect();

        radix_sort(&mut xs);
        for w in xs.as_slice().windows(2) {
            assert!(w[0] <= w[1]);
        }
    }

    #[test]
    fn negatives() {
        let mut r = rand_pcg::Pcg32::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7);
        let mut xs: Vec<i32> = (0..1_000_000).map(|_| r.next_u32() as i32).collect();

        radix_sort(&mut xs);
        for w in xs.as_slice().windows(2) {
            assert!(w[0] <= w[1], "{} <= {}", w[0], w[1]);
        }
    }

    extern crate test;
    use rand::RngCore;
    use test::{black_box, Bencher};

    #[bench]
    fn std_u32_x1e6(b: &mut Bencher) {
        let mut r = rand_pcg::Pcg32::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7);
        let xs: Vec<u32> = (0..1_000_000).map(|_| r.next_u32()).collect();

        b.iter(|| {
            let mut xs = black_box(xs.clone());
            xs.sort();
            black_box(xs);
        });
    }

    #[bench]
    fn radix_u32_x1e6(b: &mut Bencher) {
        let mut r = rand_pcg::Pcg32::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7);
        let xs: Vec<u32> = (0..1_000_000).map(|_| r.next_u32()).collect();

        b.iter(|| {
            let mut xs = black_box(xs.clone());
            radix_sort(&mut xs);
            black_box(xs);
        });
    }

    #[bench]
    fn std_u64_x1e6(b: &mut Bencher) {
        let mut r = rand_pcg::Pcg32::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7);
        let xs: Vec<u64> = (0..1_000_000).map(|_| r.next_u64()).collect();

        b.iter(|| {
            let mut xs = black_box(xs.clone());
            xs.sort();
            black_box(xs);
        });
    }

    #[bench]
    fn radix_u64_x1e6(b: &mut Bencher) {
        let mut r = rand_pcg::Pcg32::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7);
        let xs: Vec<u64> = (0..1_000_000).map(|_| r.next_u64()).collect();

        b.iter(|| {
            let mut xs = black_box(xs.clone());
            radix_sort(&mut xs);
            black_box(xs);
        });
    }
}