use std::{cell, mem, ptr};
mod sealed;
pub use sealed::entries::{Entry, Plane, Resident};
pub(in crate::depot::queue::indexed) type Membership<'a, T, P, const N: usize, const Q: usize> =
crate::depot::map::Entry<Plane<'a, T, P, N, Q, true>, Plane<'a, T, P, N, Q, false>>;
use crate::{
alloc::array,
depot::{
capacity,
queue::{indexed::shared, links},
},
};
struct Node<T, P, const N: usize> {
state: P,
values: [cell::Cell<mem::MaybeUninit<T>>; N],
hooks: [links::Hook; N],
}
#[repr(transparent)]
pub struct Store<T, P = (), const N: usize = 1, C: crate::depot::raw::ChainCount = u32> {
block: array::raw::Block<Node<T, P, N>, capacity::Count, [links::raw::Chain<C>; N]>,
}
impl<T, P, const N: usize, C: crate::depot::raw::ChainCount> Default for Store<T, P, N, C> {
fn default() -> Self {
const { assert!(N > 0) };
Self {
block: array::raw::Block::empty(
crate::alloc::raw::Empty::EMPTY,
[const { links::raw::Chain::new() }; N],
),
}
}
}
impl<T, P, const N: usize, C: crate::depot::raw::ChainCount> Store<T, P, N, C> {
pub(in crate::depot::queue::indexed) fn try_count(
count: capacity::Count,
mut seed: impl FnMut(usize) -> P,
) -> Result<Self, crate::alloc::Error> {
const { assert!(N > 0) };
let block = array::raw::Block::try_new(count, [const { links::raw::Chain::new() }; N])?
.init(|index| Node {
state: seed(index),
values: [const { cell::Cell::new(mem::MaybeUninit::uninit()) }; N],
hooks: [(); N].map(|()| links::Hook::vacant(index as u32, links::Index::END)),
});
Ok(Self { block })
}
}
impl<T, P, const N: usize, C: crate::depot::raw::ChainCount> Store<T, P, N, C> {
pub fn capacity(&self) -> usize {
self.block.extent().get()
}
pub fn get(&self, index: usize) -> Option<Entry<'_, T, P, N, C>> {
if index >= self.capacity() {
return None;
}
Some(Entry { store: self, index })
}
pub fn iter(&self) -> impl Iterator<Item = Entry<'_, T, P, N, C>> {
(0..self.capacity()).map(|index| Entry { store: self, index })
}
pub(in crate::depot) unsafe fn at(&self, index: usize) -> Entry<'_, T, P, N, C> {
Entry { store: self, index }
}
}
impl<T, P, const N: usize, C: crate::depot::raw::ChainCount> Drop for Store<T, P, N, C> {
fn drop(&mut self) {
if !mem::needs_drop::<T>() {
return;
}
for (plane, chain) in self.block.state().iter().enumerate() {
let mut head = chain.first();
while let Some(index) = head {
let node = unsafe { self.block.get_unchecked(index.get()) };
head = node.hooks[plane].next_index();
unsafe { (*node.values[plane].as_ptr()).assume_init_drop() };
}
}
}
}
pub struct Lane<'a, T, P, const N: usize, C: crate::depot::raw::ChainCount, const Q: usize>(
&'a Store<T, P, N, C>,
);
unsafe impl<T, P, const N: usize, C: crate::depot::raw::ChainCount, const Q: usize>
crate::depot::raw::Hooks for Lane<'_, T, P, N, C, Q>
{
unsafe fn hook_raw(&self, index: links::Index) -> &'_ links::Hook {
const { assert!(Q < N) };
&Entry {
store: self.0,
index: index.get(),
}
.hooks()[Q]
}
}
impl<T, P, const N: usize, C: crate::depot::raw::ChainCount> Store<T, P, N, C> {
pub(in crate::depot::queue::indexed) unsafe fn resident_ptr(
&mut self,
index: links::Index,
) -> *mut P {
let row = self.block.as_mut_ptr().wrapping_add(index.get());
unsafe { ptr::addr_of_mut!((*row).state) }
}
}
impl<P, const N: usize> Store<(), P, N> {
pub fn try_seed(
capacity: usize,
seed: impl FnMut(usize) -> P,
) -> Result<Self, crate::alloc::Error> {
Self::try_count(
capacity::Count::try_count(capacity, crate::alloc::Error::Overflow)?,
seed,
)
}
pub fn get_mut(&mut self, index: usize) -> Option<&mut P> {
Some(&mut self.block.get_mut(index)?.state)
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut P> {
self.block.iter_mut().map(|node| &mut node.state)
}
pub fn entry(&mut self, index: usize) -> Option<Resident<'_, P, N>> {
if index >= self.capacity() {
return None;
}
Some(Resident { store: self, index })
}
pub fn front_entry<const Q: usize>(
&mut self,
) -> Option<(&mut P, Plane<'_, (), P, N, Q, true, true>)> {
let index = self.queue::<Q>().chain().first()?;
let resident = unsafe { self.resident_ptr(index) };
Some((
unsafe { &mut *resident },
Plane(Entry {
store: self,
index: index.get(),
}),
))
}
pub fn clear<const Q: usize>(&mut self) {
while let Some(head) = self.queue::<Q>().head() {
unsafe { head.unlink::<Q, true>() };
}
}
}
struct Selected<'a, P, const N: usize> {
store: &'a Store<(), P, N>,
plane: usize,
}
unsafe impl<P, const N: usize> crate::depot::raw::Hooks for Selected<'_, P, N> {
unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
unsafe {
Entry {
store: self.store,
index: index.get(),
}
.hooks()
.get_unchecked(self.plane)
}
}
}
impl<T: Copy, P: Default, const N: usize> Store<T, P, N, ()> {
pub fn try_new(capacity: usize) -> Result<Self, crate::alloc::Error> {
Self::try_count(
capacity::Count::try_count(capacity, crate::alloc::Error::Overflow)?,
|_index| P::default(),
)
}
}
impl<T: Copy, P, const N: usize> Store<T, P, N, ()> {
pub fn try_entry(&self, index: usize) -> Result<Entry<'_, T, P, N, ()>, shared::Bounds> {
self.get(index).ok_or(shared::Bounds {
index,
capacity: self.capacity(),
})
}
}
impl<T, P, const N: usize, C: crate::depot::raw::ChainCount> Store<T, P, N, C> {
pub fn queue<const Q: usize>(&self) -> Lane<'_, T, P, N, C, Q> {
const { assert!(Q < N) };
Lane(self)
}
}
impl<'a, T, P, const N: usize, C: crate::depot::raw::ChainCount, const Q: usize>
Lane<'a, T, P, N, C, Q>
{
pub(in crate::depot::queue) fn chain(&self) -> &links::raw::Chain<C> {
&self.0.block.state()[Q]
}
pub(in crate::depot::queue) fn head(&self) -> Option<Entry<'a, T, P, N, C>> {
Some(Entry {
store: self.0,
index: self.chain().first()?.get(),
})
}
pub fn is_empty(&self) -> bool {
self.chain().is_empty()
}
}
impl<'a, T: Copy, P, const N: usize, C: crate::depot::raw::ChainCount, const Q: usize>
Lane<'a, T, P, N, C, Q>
{
pub fn front(&self) -> Option<T> {
let head = self.head()?;
Some(unsafe { (*head.value::<Q>()).assume_init_read() })
}
}
impl<'a, P, const N: usize, const Q: usize> Lane<'a, (), P, N, u32, Q> {
pub fn first(&self) -> Option<&'a P> {
Some(self.head()?.state())
}
pub fn len(&self) -> usize {
self.chain().len()
}
}
impl<'a, T: Copy, P, const N: usize, const Q: usize> Lane<'a, T, P, N, (), Q> {
pub fn next(&self) -> Option<(Entry<'a, T, P, N, ()>, T)> {
let head = self.head()?;
let value = unsafe { head.take::<Q, true>() };
Some((head, value))
}
pub fn pop_front(&self) -> Option<(usize, &'a P, T)> {
let (head, value) = self.next()?;
Some((head.index(), head.state(), value))
}
}
const _: () = {
assert!(mem::size_of::<Store<u64, (), 1, ()>>() == 24);
assert!(mem::size_of::<Store<u64, (), 2, ()>>() == 32);
assert!(mem::size_of::<Entry<'_, u64, (), 1, ()>>() == 16);
assert!(mem::size_of::<Option<Entry<'_, u64, (), 1, ()>>>() == 16);
assert!(mem::size_of::<Entry<'_, u64, (), 2, ()>>() == 16);
assert!(mem::size_of::<Option<Entry<'_, u64, (), 2, ()>>>() == 16);
};