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
use crate::SIMD_LEN;
use std::simd::cmp::SimdPartialOrd;
use std::simd::Mask;
use std::simd::Simd;
use std::simd::SimdElement;
use std::slice;

pub trait IsSortedSimd<'a, T>
where
    T: SimdElement + std::cmp::PartialOrd,
    Simd<T, SIMD_LEN>: SimdPartialOrd<Mask = Mask<T::Mask, SIMD_LEN>>,
{
    fn is_sorted_simd(&self) -> bool;
}

impl<'a, T> IsSortedSimd<'a, T> for slice::Iter<'a, T>
where
    T: SimdElement + std::cmp::PartialOrd,
    Simd<T, SIMD_LEN>: SimdPartialOrd<Mask = Mask<T::Mask, SIMD_LEN>>,
{
    fn is_sorted_simd(&self) -> bool {
        let a = self.as_slice();

        if a.len() <= SIMD_LEN && !a.is_empty() {
            return a.is_sorted();
        }

        let chunks_a = a.chunks_exact(SIMD_LEN);
        let chunks_b = a[1..].chunks_exact(SIMD_LEN);
        let reminder_a_is_sorted = chunks_a.remainder().iter().is_sorted();
        let reminder_b_is_sorted = chunks_b.remainder().iter().is_sorted();

        // chunk:         [1,2,3,4]
        // offset_by_one: [2,3,4,5]
        // If for all chunk[i] <= offset[i] then the slice is sorted

        for (a, b) in chunks_a.zip(chunks_b) {
            let chunk = Simd::from_slice(a);
            let chunk_offset_by_one = Simd::from_slice(b);
            if chunk.simd_gt(chunk_offset_by_one).to_bitmask() != 0 {
                return false;
            }
        }
        reminder_a_is_sorted | reminder_b_is_sorted
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::distributions::Standard;
    use rand::prelude::Distribution;
    use rand::Rng;
    use std::fmt::Debug;

    fn test_integers<T>()
    where
        T: rand::distributions::uniform::SampleUniform
            + PartialEq
            + Debug
            + Copy
            + Default
            + SimdElement
            + std::cmp::PartialEq
            + std::cmp::PartialOrd
            + std::cmp::Ord,
        Simd<T, SIMD_LEN>: SimdPartialOrd<Mask = Mask<T::Mask, SIMD_LEN>>,
        Standard: Distribution<T>,
    {
        for len in 0..1000 {
            for _ in 0..50 {
                let mut v: Vec<T> = vec![T::default(); len];
                let mut rng = rand::thread_rng();
                for x in v.iter_mut() {
                    *x = rng.gen()
                }

                if rng.gen_bool(0.5) {
                    v.sort();
                }

                let ans = v.iter().is_sorted();
                let correct = v.iter().is_sorted();

                assert_eq!(
                    ans,
                    correct,
                    "Failed for length {} and type {:?}",
                    len,
                    std::any::type_name::<T>()
                );
            }
        }
    }
    #[test]
    fn test_f32() {
        for len in 0..1000 {
            for _ in 0..5 {
                let mut v: Vec<f32> = vec![0.0; len];
                let mut rng = rand::thread_rng();
                for x in v.iter_mut() {
                    *x = rng.gen()
                }
                if rng.gen_bool(0.5) {
                    v.sort_floats();
                }

                let ans = v.iter().is_sorted();
                let correct = v.iter().is_sorted();

                assert_eq!(
                    ans, correct,
                    "Failed for length {} and type {:?}",
                    len, "f32"
                );
            }
        }
    }
    #[test]
    fn test_f64() {
        for len in 0..1000 {
            for _ in 0..5 {
                let mut v: Vec<f64> = vec![0.0_f64; len];
                let mut rng = rand::thread_rng();
                for x in v.iter_mut() {
                    *x = rng.gen()
                }

                if rng.gen_bool(0.5) {
                    v.sort_floats();
                }

                let ans = v.iter().is_sorted();
                let correct = v.iter().is_sorted();

                assert_eq!(
                    ans, correct,
                    "Failed for length {} and type {:?}",
                    len, "f64"
                );
            }
        }
    }

    #[test]
    fn test_simd_is_sorted() {
        test_integers::<i8>();
        test_integers::<i16>();
        test_integers::<i32>();
        test_integers::<i64>();
        test_integers::<u8>();
        test_integers::<u16>();
        test_integers::<u32>();
        test_integers::<u64>();
        test_integers::<usize>();
        test_integers::<isize>();
    }
}