use std::{mem, num::NonZero, ptr};
use crate::{
base::num::bounded,
depot::array::{self, Len, copy, sorted},
};
mod drain;
pub use drain::Drain;
pub unsafe trait BitIndex<const N: usize>: Copy {
fn index(self) -> usize;
unsafe fn from_index(index: usize) -> Self;
}
pub(super) fn prefix<T, const N: usize>(len: array::Len<N>, values: &[T; N]) -> &[T] {
unsafe { values.get_unchecked(..len.get()) }
}
pub(super) fn prefix_mut<T, const N: usize>(len: array::Len<N>, values: &mut [T; N]) -> &mut [T] {
unsafe { values.get_unchecked_mut(..len.get()) }
}
pub(super) fn at<'a, T, const N: usize>(slot: &array::Vacancy<N>, values: &'a [T; N]) -> &'a T {
unsafe { values.get_unchecked(slot.index()) }
}
pub(super) fn at_mut<'a, T, const N: usize>(
slot: &array::Vacancy<N>,
values: &'a mut [T; N],
) -> &'a mut T {
unsafe { values.get_unchecked_mut(slot.index()) }
}
pub(super) fn insert_at<'a, T: Copy, const N: usize, const M: usize>(
slot: &array::Vacancy<N>,
values: &[T; M],
value: T,
out: &'a mut mem::MaybeUninit<[T; N]>,
) -> &'a mut [T; N] {
const { assert!(M < N && M + 1 == N) };
let index = slot.index();
unsafe {
let target = out.as_mut_ptr().cast::<T>();
ptr::copy_nonoverlapping(values.as_ptr(), target, index);
target.add(index).write(value);
ptr::copy_nonoverlapping(values.as_ptr().add(index), target.add(index + 1), M - index);
out.assume_init_mut()
}
}
pub(super) fn bit_at<I: BitIndex<N>, const N: usize>(slot: array::Vacancy<N>) -> I {
const { assert!(N <= u16::BITS as usize) };
unsafe { I::from_index(slot.index()) }
}
pub(super) const fn compact_position<const N: usize>(slot: array::Vacancy<N>) -> NonZero<u8> {
const { assert!(N <= u8::MAX as usize) };
unsafe { NonZero::new_unchecked(slot.commit().get() as u8) }
}
pub(super) const fn position_u32<const N: usize, const MAX: u32>(
index: array::Index<N>,
) -> bounded::raw::U32<0, MAX> {
const { assert!(N <= u8::MAX as usize && N.saturating_sub(1) <= MAX as usize) };
unsafe { bounded::raw::U32::from_proven(index.index() as u32) }
}
pub(super) fn slice<T, const N: usize>(source: ©::Vec<T, N>) -> &[T] {
unsafe { source.len.prefix(&source.entries).assume_init_ref() }
}
pub(super) fn slice_mut<T, const N: usize>(source: &mut copy::Vec<T, N>) -> &mut [T] {
unsafe { source.len.prefix_mut(&mut source.entries).assume_init_mut() }
}
pub(super) fn retain<T: Copy, const N: usize>(
source: &mut copy::Vec<T, N>,
mut keep: impl FnMut(&T) -> bool,
) {
let values: &mut [T] = source;
let Some(mut written) = values.iter().position(|value| !keep(value)) else {
return;
};
for read in written + 1..values.len() {
if keep(&values[read]) {
let value = values[read];
unsafe { *values.get_unchecked_mut(written) = value };
written += 1;
}
}
source.len = Len(written as u32);
}
pub(super) fn pop<T: Copy, const N: usize>(source: &mut copy::Vec<T, N>) -> Option<T> {
let last = NonZero::new(source.len.0)?;
source.len = Len(last.get() - 1);
Some(unsafe {
source
.entries
.get_unchecked(source.len.get())
.assume_init_read()
})
}
pub(super) fn ordered<T: Copy + Ord, const N: usize>(
source: &mut copy::Vec<T, N>,
value: T,
) -> Result<(), sorted::Error<T>> {
let position = match source.binary_search(&value) {
Ok(index) => {
let existing = unsafe { *source.get_unchecked(index) };
return Err(sorted::Error::Duplicate { value, existing });
}
Err(index) => index,
};
let slot = source
.len
.vacancy()
.ok_or(sorted::Error::Full { value, capacity: N })?;
unsafe {
let start = source.entries.as_mut_ptr().add(position);
ptr::copy(start, start.add(1), source.len.get() - position);
start.write(mem::MaybeUninit::new(value));
}
source.len = slot.commit();
Ok(())
}
pub(super) fn next<T, const N: usize>(cursor: &mut copy::IntoIter<T, N>) -> Option<T> {
let front = cursor.front;
if front == cursor.back {
return None;
}
cursor.front = Len(front.0.wrapping_add(1));
Some(unsafe { cursor.entries.get_unchecked(front.get()).assume_init_read() })
}
pub(super) fn restore<T, const N: usize>(slot: array::Withdrawn<'_, T, N>, value: T) {
let cursor = &mut slot.source.raw;
let front = Len(cursor.front.0.wrapping_sub(1));
unsafe { cursor.entries.get_unchecked_mut(front.get()) }.write(value);
cursor.front = front;
}
pub(super) fn release<T, const N: usize>(cursor: &mut copy::IntoIter<T, N>) {
let front = mem::replace(&mut cursor.front, cursor.back);
unsafe {
cursor
.entries
.get_unchecked_mut(front.get()..cursor.back.get())
.assume_init_drop()
};
}