1
0
Fork 0
This commit is contained in:
Paul-Nicolas Madelaine 2024-04-29 02:00:26 +02:00
commit faccfbc1c5
16 changed files with 5154 additions and 0 deletions

134
src/array_vec.rs Normal file
View file

@ -0,0 +1,134 @@
use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;
use std::mem::MaybeUninit;
#[derive(Clone)]
pub(crate) struct ArrayVec<T: Copy, const N: usize> {
len: usize,
array: [MaybeUninit<T>; N],
}
impl<T: Copy, const N: usize> ArrayVec<T, N> {
#[inline]
pub(crate) fn new() -> Self {
Self {
len: 0,
array: [const { MaybeUninit::uninit() }; N],
}
}
#[inline]
pub(crate) unsafe fn push_unchecked(&mut self, m: T) {
debug_assert!(self.len < N);
unsafe {
self.array.get_unchecked_mut(self.len).write(m);
self.len = self.len.unchecked_add(1);
}
}
#[inline]
pub(crate) fn len(&self) -> usize {
self.len
}
#[inline]
pub(crate) fn get(&self, index: usize) -> Option<&T> {
if index < self.len {
Some(unsafe { self.array.as_slice().get_unchecked(index).assume_init_ref() })
} else {
None
}
}
#[inline]
pub(crate) fn as_slice_mut(&mut self) -> &mut [T] {
unsafe { std::mem::transmute::<_, &mut [T]>(self.array.get_unchecked_mut(0..self.len)) }
}
}
impl<'l, T: Copy, const N: usize> IntoIterator for &'l ArrayVec<T, N> {
type Item = T;
type IntoIter = ArrayVecIter<'l, T, N>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
ArrayVecIter {
array: self,
index: 0,
}
}
}
pub(crate) struct ArrayVecIter<'l, T: Copy, const N: usize> {
array: &'l ArrayVec<T, N>,
index: usize,
}
impl<'l, T: Copy, const N: usize> Iterator for ArrayVecIter<'l, T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.array.len {
unsafe {
let item = self
.array
.array
.get_unchecked(self.index)
.assume_init_read();
self.index = self.index.unchecked_add(1);
Some(item)
}
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<'l, T: Copy, const N: usize> FusedIterator for ArrayVecIter<'l, T, N> {}
impl<'l, T: Copy, const N: usize> ExactSizeIterator for ArrayVecIter<'l, T, N> {
#[inline]
fn len(&self) -> usize {
unsafe { self.array.len().unchecked_sub(self.index) }
}
}
impl<T: Copy, const N: usize> IntoIterator for ArrayVec<T, N> {
type Item = T;
type IntoIter = ArrayVecIntoIter<T, N>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
ArrayVecIntoIter {
array: self,
index: 0,
}
}
}
pub(crate) struct ArrayVecIntoIter<T: Copy, const N: usize> {
array: ArrayVec<T, N>,
index: usize,
}
impl<T: Copy, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.array.len {
unsafe {
let item = self
.array
.array
.get_unchecked(self.index)
.assume_init_read();
self.index = self.index.unchecked_add(1);
Some(item)
}
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<T: Copy, const N: usize> FusedIterator for ArrayVecIntoIter<T, N> {}
impl<T: Copy, const N: usize> ExactSizeIterator for ArrayVecIntoIter<T, N> {
#[inline]
fn len(&self) -> usize {
unsafe { self.array.len().unchecked_sub(self.index) }
}
}