use std::{cell, mem, ptr};
use crate::{
alloc::array,
depot::{capacity, queue::links, raw},
};
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: raw::ChainCount = u32> {
block: array::Block<Node<T, P, N>, capacity::Count, [links::Chain<C>; N]>,
}
pub struct NodeEntry<'a, T, P = (), const N: usize = 1, C: raw::ChainCount = u32> {
store: &'a Store<T, P, N, C>,
node: &'a Node<T, P, N>,
}
impl<T, P, const N: usize, C: raw::ChainCount> Default for Store<T, P, N, C> {
fn default() -> Self {
const { assert!(N > 0) };
Self {
block: array::Block::empty(
unsafe { crate::alloc::raw::Empty::assume(capacity::Count::EMPTY) },
[const { links::Chain::new() }; N],
),
}
}
}
impl<T, P, const N: usize, C: raw::ChainCount> Store<T, P, N, C> {
pub(in crate::depot::queue::indexed) fn try_count(
count: capacity::Count,
mut seed: impl FnMut() -> P,
) -> Result<Self, crate::alloc::Error> {
const { assert!(N > 0) };
let block =
array::Block::try_new(count, [const { links::Chain::new() }; N])?.init(|index| Node {
state: seed(),
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: Copy, P, const N: usize, C: raw::ChainCount> Store<T, P, N, C> {
pub fn front<const Q: usize>(&self) -> Option<T> {
let index = self.chain::<Q>().first()?;
Some(unsafe { (*self.at(index.get()).value::<Q>()).assume_init_read() })
}
}
impl<T, P, const N: usize, C: raw::ChainCount> Store<T, P, N, C> {
pub(in crate::depot::queue::indexed) fn chain<const Q: usize>(&self) -> &links::Chain<C> {
const { assert!(Q < N) };
&self.block.state()[Q]
}
pub fn capacity(&self) -> usize {
self.block.extent().get()
}
pub fn get(&self, index: usize) -> Option<NodeEntry<'_, T, P, N, C>> {
Some(NodeEntry {
store: self,
node: self.block.get(index)?,
})
}
pub fn iter(&self) -> impl Iterator<Item = NodeEntry<'_, T, P, N, C>> {
self.block
.iter()
.map(|node| NodeEntry { store: self, node })
}
pub(in crate::depot) unsafe fn at(&self, index: usize) -> NodeEntry<'_, T, P, N, C> {
NodeEntry {
store: self,
node: unsafe { self.block.get_unchecked(index) },
}
}
}
impl<T, P, const N: usize, C: 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() };
}
}
}
}
impl<'a, T, P, const N: usize, C: raw::ChainCount> NodeEntry<'a, T, P, N, C> {
pub fn state(&self) -> &'a P {
&self.node.state
}
pub(in crate::depot::queue::indexed) fn position(&self) -> links::Index {
let index = unsafe { ptr::from_ref(self.node).offset_from(self.store.block.as_ptr()) };
unsafe { links::Index::new_usize(index as usize) }
}
pub(in crate::depot::queue::indexed) fn linked<const Q: usize>(
&self,
index: links::Index,
) -> bool {
const { assert!(Q < N) };
index.is_linked(&self.node.hooks[Q])
}
pub(in crate::depot::queue::indexed) fn value<const Q: usize>(
&self,
) -> *mut mem::MaybeUninit<T> {
const { assert!(Q < N) };
self.node.values[Q].as_ptr()
}
}
impl<T, P, const N: usize, C: raw::ChainCount> NodeEntry<'_, T, P, N, C> {
pub(in crate::depot::queue::indexed) unsafe fn take_head<const Q: usize>(
&self,
index: links::Index,
) -> T {
let hooks = Hooks::<_, _, N, C, Q>(self.store);
unsafe {
self.store
.chain::<Q>()
.detach_hook::<true, 0, _>(&hooks, index, &self.node.hooks[Q])
}
.0
.idle();
unsafe { (*self.value::<Q>()).assume_init_read() }
}
pub(in crate::depot::queue::indexed) unsafe fn unlink<const Q: usize>(
&self,
index: links::Index,
) {
let hooks = Hooks::<_, _, N, C, Q>(self.store);
unsafe {
self.store
.chain::<Q>()
.detach_hook::<false, 0, _>(&hooks, index, &self.node.hooks[Q])
}
.0
.idle();
}
pub(in crate::depot::queue::indexed) unsafe fn write_back<
const Q: usize,
const LINKED: bool,
>(
&self,
index: links::Index,
value: T,
) {
let hooks = Hooks::<_, _, N, C, Q>(self.store);
let chain = self.store.chain::<Q>();
let vacant = if LINKED {
unsafe { chain.detach_hook::<false, 0, _>(&hooks, index, &self.node.hooks[Q]) }.0
} else {
unsafe { links::Detached::new(&self.node.hooks[Q], index) }
};
self.node.values[Q].set(mem::MaybeUninit::new(value));
unsafe { chain.push(&hooks, vacant) };
}
}
struct Hooks<'a, T, P, const N: usize, C: raw::ChainCount, const Q: usize>(&'a Store<T, P, N, C>);
unsafe impl<T, P, const N: usize, C: raw::ChainCount, const Q: usize> raw::Hooks
for Hooks<'_, T, P, N, C, Q>
{
unsafe fn hook_raw(&self, index: links::Index) -> &'_ links::Hook {
const { assert!(Q < N) };
&unsafe { self.0.block.get_unchecked(index.get()) }.hooks[Q]
}
}