Convert over Subs to use the new soa crate

This commit is contained in:
Richard Feldman 2024-10-10 00:17:59 -04:00
parent 586959780b
commit 2567c8b545
No known key found for this signature in database
GPG key ID: 5DE4EE30BB738EDF
17 changed files with 240 additions and 401 deletions

View file

@ -7,28 +7,34 @@ use crate::soa_slice::Slice;
///
/// Unlike a Rust pointer, this is a u32 offset
/// rather than usize.
pub struct Index<T> {
pub struct Index<Array, Elem> {
pub index: u32,
pub(crate) _marker: core::marker::PhantomData<T>,
pub(crate) _marker: core::marker::PhantomData<(Array, Elem)>,
}
impl<T> fmt::Debug for Index<T> {
impl<Array, Elem> fmt::Debug for Index<Array, Elem> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Index<{}>({})", core::any::type_name::<T>(), self.index)
write!(
f,
"Index<{}, {}>({})",
core::any::type_name::<Array>(),
core::any::type_name::<Elem>(),
self.index
)
}
}
// derive of copy and clone does not play well with PhantomData
impl<T> Copy for Index<T> {}
impl<Array, Elem> Copy for Index<Array, Elem> {}
impl<T> Clone for Index<T> {
impl<Array, Elem> Clone for Index<Array, Elem> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Index<T> {
impl<Array, Elem> Index<Array, Elem> {
pub const fn new(start: u32) -> Self {
Self {
index: start,
@ -36,7 +42,7 @@ impl<T> Index<T> {
}
}
pub fn push_new(vector: &mut Vec<T>, value: T) -> Self {
pub fn push_new(vector: &mut Vec<Elem>, value: Elem) -> Self {
let index = Self::new(vector.len() as _);
vector.push(value);
@ -44,7 +50,7 @@ impl<T> Index<T> {
index
}
pub const fn as_slice(self) -> Slice<T> {
pub const fn as_slice(self) -> Slice<Array, Elem> {
Slice::new(self.index, 1)
}
}

View file

@ -8,39 +8,42 @@ use crate::soa_index::Index;
///
/// Unlike a Rust slice, this is a u32 offset
/// rather than a pointer, and the length is u16.
pub struct Slice<T> {
pub struct Slice<Array, Elem> {
pub start: u32,
pub length: u16,
_marker: core::marker::PhantomData<T>,
_marker: core::marker::PhantomData<(Array, Elem)>,
}
impl<T> fmt::Debug for Slice<T> {
impl<T, U> fmt::Debug for Slice<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Slice {{ start: {}, length: {} }}",
self.start, self.length
"Slice<{}, {}> {{ start: {}, length: {} }}",
core::any::type_name::<T>(),
core::any::type_name::<U>(),
self.start,
self.length
)
}
}
// derive of copy and clone does not play well with PhantomData
impl<T> Copy for Slice<T> {}
impl<T, U> Copy for Slice<T, U> {}
impl<T> Clone for Slice<T> {
impl<T, U> Clone for Slice<T, U> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Default for Slice<T> {
impl<T, U> Default for Slice<T, U> {
fn default() -> Self {
Self::empty()
}
}
impl<T> Slice<T> {
impl<Array, Elem> Slice<Array, Elem> {
pub fn empty() -> Self {
Self {
start: 0,
@ -49,11 +52,11 @@ impl<T> Slice<T> {
}
}
pub fn get_slice<'a>(&self, slice: &'a [T]) -> &'a [T] {
pub fn get_slice<'a>(&self, slice: &'a [Elem]) -> &'a [Elem] {
&slice[self.indices()]
}
pub fn get_slice_mut<'a>(&self, slice: &'a mut [T]) -> &'a mut [T] {
pub fn get_slice_mut<'a>(&self, slice: &'a mut [Elem]) -> &'a mut [Elem] {
&mut slice[self.indices()]
}
@ -78,7 +81,7 @@ impl<T> Slice<T> {
}
}
pub fn extend_new(vec: &mut Vec<T>, it: impl IntoIterator<Item = T>) -> Self {
pub fn extend_new(vec: &mut Vec<Elem>, it: impl IntoIterator<Item = Elem>) -> Self {
let start = vec.len();
vec.extend(it);
@ -89,8 +92,8 @@ impl<T> Slice<T> {
}
}
impl<T> IntoIterator for Slice<T> {
type Item = Index<T>;
impl<Array, Elem> IntoIterator for Slice<Array, Elem> {
type Item = Index<Array, Elem>;
#[allow(clippy::type_complexity)]
type IntoIter = Map<core::ops::Range<u32>, fn(u32) -> Self::Item>;
@ -100,13 +103,13 @@ impl<T> IntoIterator for Slice<T> {
}
}
fn u32_to_index<T>(i: u32) -> Index<T> {
fn u32_to_index<T, U>(i: u32) -> Index<T, U> {
Index {
index: i,
_marker: core::marker::PhantomData,
}
}
pub trait GetSlice<T> {
fn get_slice(&self, slice: Slice<T>) -> &[T];
pub trait GetSlice<Array, Elem> {
fn get_slice(&self, slice: Slice<Array, Elem>) -> &[Elem];
}