Skip to main content

simply_simd/
simd_array.rs

1use std::borrow::{Borrow, BorrowMut};
2use std::fmt::Debug;
3use std::ops::{Index, IndexMut};
4// use std::hash::Hash;
5
6// This is unfortunately needed to avoid generic const expr or excessive bounding issues. TODO: Come up with better solution.
7pub trait SimdToArray<T>:
8    Clone +
9    Debug +
10    // PartialEq +
11    // Eq +
12    // PartialOrd +
13    Index<usize, Output = T> +
14    IndexMut<usize, Output = T> +
15    AsRef<[T]> +
16    AsMut<[T]> +
17    Borrow<[T]> +
18    BorrowMut<[T]> +
19    IntoIterator<Item = T> +
20    Sized
21{
22    const LEN: usize;
23
24    // ---- Construction ----
25    fn from_fn(f: impl FnMut(usize) -> T) -> Self;
26    fn zeroed() -> Self where T: Default { Self::from_fn(|_| T::default()) }
27
28    // ---- Slice access ----
29    fn as_slice(&self) -> &[T];
30    fn as_mut_slice(&mut self) -> &mut [T];
31
32    // ---- Element access ----
33    fn get(&self, i: usize) -> Option<&T>                { self.as_slice().get(i) }
34    fn get_mut(&mut self, i: usize) -> Option<&mut T>    { self.as_mut_slice().get_mut(i) }
35    fn first(&self) -> Option<&T>                        { self.as_slice().first() }
36    fn first_mut(&mut self) -> Option<&mut T>            { self.as_mut_slice().first_mut() }
37    fn last(&self) -> Option<&T>                         { self.as_slice().last() }
38    fn last_mut(&mut self) -> Option<&mut T>             { self.as_mut_slice().last_mut() }
39
40    // ---- Info ----
41    fn len(&self) -> usize                               { Self::LEN }
42    fn is_empty(&self) -> bool                           { Self::LEN == 0 }
43
44    // ---- Iteration ----
45    fn iter(&self) -> std::slice::Iter<'_, T>            { self.as_slice().iter() }
46    fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> { self.as_mut_slice().iter_mut() }
47    fn windows(&self, size: usize) -> std::slice::Windows<'_, T>          { self.as_slice().windows(size) }
48    fn chunks(&self, size: usize) -> std::slice::Chunks<'_, T>            { self.as_slice().chunks(size) }
49    fn chunks_exact(&self, size: usize) -> std::slice::ChunksExact<'_, T> { self.as_slice().chunks_exact(size) }
50    fn rchunks(&self, size: usize) -> std::slice::RChunks<'_, T>          { self.as_slice().rchunks(size) }
51    fn rchunks_exact(&self, size: usize) -> std::slice::RChunksExact<'_, T> { self.as_slice().rchunks_exact(size) }
52
53    // ---- Search ----
54    fn contains(&self, x: &T) -> bool where T: PartialEq        { self.as_slice().contains(x) }
55    fn starts_with(&self, needle: &[T]) -> bool where T: PartialEq { self.as_slice().starts_with(needle) }
56    fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq { self.as_slice().ends_with(needle) }
57    fn binary_search(&self, x: &T) -> Result<usize, usize> where T: Ord { self.as_slice().binary_search(x) }
58    fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
59        where F: FnMut(&'a T) -> std::cmp::Ordering, T: 'a  { self.as_slice().binary_search_by(f) }
60
61    fn binary_search_by_key<'a, B: Ord, F: FnMut(&'a T) -> B>(&'a self, b: &B, f: F) -> Result<usize, usize>
62        where T: 'a                                          { self.as_slice().binary_search_by_key(b, f) }
63    fn position(&self, f: impl FnMut(&T) -> bool) -> Option<usize> { self.as_slice().iter().position(f) }
64
65    // ---- Mutation ----
66    fn fill(&mut self, value: T) where T: Clone          { self.as_mut_slice().fill(value) }
67    fn fill_with(&mut self, f: impl FnMut() -> T)        { self.as_mut_slice().fill_with(f) }
68    fn swap(&mut self, a: usize, b: usize)               { self.as_mut_slice().swap(a, b) }
69    fn reverse(&mut self)                                { self.as_mut_slice().reverse() }
70    fn rotate_left(&mut self, mid: usize)                { self.as_mut_slice().rotate_left(mid) }
71    fn rotate_right(&mut self, mid: usize)               { self.as_mut_slice().rotate_right(mid) }
72    fn copy_from_slice(&mut self, src: &[T]) where T: Copy { self.as_mut_slice().copy_from_slice(src) }
73    fn clone_from_slice(&mut self, src: &[T]) where T: Clone { self.as_mut_slice().clone_from_slice(src) }
74    fn swap_with_slice(&mut self, other: &mut [T])       { self.as_mut_slice().swap_with_slice(other) }
75
76    // ---- Sorting ----
77    fn sort(&mut self) where T: Ord                      { self.as_mut_slice().sort() }
78    fn sort_by(&mut self, f: impl FnMut(&T, &T) -> std::cmp::Ordering) { self.as_mut_slice().sort_by(f) }
79    fn sort_by_key<K: Ord>(&mut self, f: impl FnMut(&T) -> K) { self.as_mut_slice().sort_by_key(f) }
80    fn sort_unstable(&mut self) where T: Ord             { self.as_mut_slice().sort_unstable() }
81    fn sort_unstable_by(&mut self, f: impl FnMut(&T, &T) -> std::cmp::Ordering) { self.as_mut_slice().sort_unstable_by(f) }
82    fn sort_unstable_by_key<K: Ord>(&mut self, f: impl FnMut(&T) -> K) { self.as_mut_slice().sort_unstable_by_key(f) }
83    fn is_sorted(&self) -> bool where T: PartialOrd      { self.as_slice().is_sorted() }
84    fn is_sorted_by(&self, f: impl FnMut(&T, &T) -> bool) -> bool { self.as_slice().is_sorted_by(f) }
85    fn is_sorted_by_key<K: PartialOrd>(&self, f: impl FnMut(&T) -> K) -> bool { self.as_slice().is_sorted_by_key(f) }
86
87    // ---- Pointers ----
88    fn as_ptr(&self) -> *const T { self.as_slice().as_ptr() }
89    fn as_mut_ptr(&mut self) -> *mut T { self.as_mut_slice().as_mut_ptr() }
90}
91
92impl<const N: usize, T> SimdToArray<T> for [T; N]
93where
94    T: Clone + Debug,
95{
96    const LEN: usize = N;
97    #[inline(always)]
98    fn from_fn(f: impl FnMut(usize) -> T) -> Self {
99        std::array::from_fn(f)
100    }
101    #[inline(always)]
102    fn as_slice(&self) -> &[T] {
103        self.as_slice()
104    }
105    #[inline(always)]
106    fn as_mut_slice(&mut self) -> &mut [T] {
107        self.as_mut_slice()
108    }
109}
110