update ArrayVec
This commit is contained in:
parent
00261d95c1
commit
7d7884326c
2 changed files with 19 additions and 59 deletions
|
|
@ -2,12 +2,12 @@ use core::iter::ExactSizeIterator;
|
|||
use core::iter::FusedIterator;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ArrayVec<T: Copy, const N: usize> {
|
||||
pub(crate) struct ArrayVec<T, const N: usize> {
|
||||
len: usize,
|
||||
array: [MaybeUninit<T>; N],
|
||||
}
|
||||
impl<T: Copy, const N: usize> ArrayVec<T, N> {
|
||||
|
||||
impl<T, const N: usize> ArrayVec<T, N> {
|
||||
#[inline]
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
|
|
@ -36,57 +36,16 @@ impl<T: Copy, const N: usize> ArrayVec<T, N> {
|
|||
}
|
||||
}
|
||||
#[inline]
|
||||
pub(crate) fn as_slice_mut(&mut self) -> &mut [T] {
|
||||
pub(crate) fn as_slice(&self) -> &[T] {
|
||||
unsafe { core::mem::transmute::<_, &[T]>(self.array.get_unchecked(0..self.len)) }
|
||||
}
|
||||
#[inline]
|
||||
pub(crate) fn as_mut_slice(&mut self) -> &mut [T] {
|
||||
unsafe { core::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> {
|
||||
|
||||
impl<T, const N: usize> IntoIterator for ArrayVec<T, N> {
|
||||
type Item = T;
|
||||
type IntoIter = ArrayVecIntoIter<T, N>;
|
||||
#[inline]
|
||||
|
|
@ -97,11 +56,12 @@ impl<T: Copy, const N: usize> IntoIterator for ArrayVec<T, N> {
|
|||
}
|
||||
}
|
||||
}
|
||||
pub(crate) struct ArrayVecIntoIter<T: Copy, const N: usize> {
|
||||
|
||||
pub(crate) struct ArrayVecIntoIter<T, const N: usize> {
|
||||
array: ArrayVec<T, N>,
|
||||
index: usize,
|
||||
}
|
||||
impl<T: Copy, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
|
||||
impl<T, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
|
||||
type Item = T;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
|
@ -125,8 +85,8 @@ impl<T: Copy, const N: usize> Iterator for ArrayVecIntoIter<T, N> {
|
|||
(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> {
|
||||
impl<T, const N: usize> FusedIterator for ArrayVecIntoIter<T, N> {}
|
||||
impl<T, const N: usize> ExactSizeIterator for ArrayVecIntoIter<T, N> {
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
unsafe { self.array.len().unchecked_sub(self.index) }
|
||||
|
|
|
|||
|
|
@ -256,7 +256,7 @@ impl<'l> Moves<'l> {
|
|||
pub fn iter(&'l self) -> MovesIter<'l> {
|
||||
MovesIter {
|
||||
position: self.position,
|
||||
iter: (&self.array).into_iter(),
|
||||
iter: self.array.as_slice().into_iter(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -301,7 +301,7 @@ impl<'l> Moves<'l> {
|
|||
where
|
||||
F: FnMut(Move, Move) -> core::cmp::Ordering,
|
||||
{
|
||||
self.array.as_slice_mut().sort_unstable_by(|a, b| {
|
||||
self.array.as_mut_slice().sort_unstable_by(|a, b| {
|
||||
compare(
|
||||
Move {
|
||||
position: self.position,
|
||||
|
|
@ -319,7 +319,7 @@ impl<'l> Moves<'l> {
|
|||
/// An iterator over legal moves.
|
||||
pub struct MovesIter<'l> {
|
||||
position: &'l Position,
|
||||
iter: ArrayVecIter<'l, RawMove, MAX_LEGAL_MOVES>,
|
||||
iter: core::slice::Iter<'l, RawMove>,
|
||||
}
|
||||
impl<'l> Iterator for MovesIter<'l> {
|
||||
type Item = Move<'l>;
|
||||
|
|
@ -327,7 +327,7 @@ impl<'l> Iterator for MovesIter<'l> {
|
|||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.iter.next().map(|raw| Move {
|
||||
position: self.position,
|
||||
raw,
|
||||
raw: *raw,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue