use std::{cell::Cell, marker::PhantomData, mem, num::NonZero};
use crate::{
alloc,
base::num::bounded,
depot::{
self, capacity, index,
queue::{indexed::shared, links, resident, round, round::phase},
},
};
type Capacity<T> = resident::Capacity<Cell<Option<T>>, (), Cell<links::Index>>;
type Block<T> =
alloc::raw::Block<Capacity<T>, (), alloc::raw::Initialized, depot::raw::Node<Cell<Option<T>>>>;
pub struct Values<T: Copy, const G: usize, const N: usize = 0> {
pub(super) block: Block<T>,
pub(super) groups: [links::raw::Cycle; G],
}
pub struct Slot<'a, T: Copy, const G: usize, const N: usize = 0> {
pub(super) owner: &'a Values<T, G, N>,
pub(super) index: links::Index,
}
pub struct Lane<'a, T: Copy, const G: usize, const N: usize = 0> {
pub(super) owner: &'a Values<T, G, N>,
pub(super) index: links::Index,
}
pub struct Group<'a, T: Copy, const G: usize, const N: usize = 0> {
pub(super) owner: &'a Values<T, G, N>,
pub(super) ring: &'a links::raw::Cycle,
}
pub struct Snapshot<'a, T: Copy, const G: usize, P, const N: usize = 0> {
pub(super) owner: &'a Values<T, G, N>,
pub(super) position: round::Position,
phase: PhantomData<P>,
}
#[derive(Debug)]
pub struct Place;
#[derive(Debug)]
pub struct Turn<const FAIR: bool = true>;
impl<T: Copy, const G: usize> Values<T, G> {
pub fn try_new(capacity: usize, lanes: NonZero<usize>) -> Result<Self, alloc::Error> {
const {
assert!(G != 0, "round lanes require an active group");
assert!(
mem::align_of::<depot::raw::Node<Cell<Option<T>>>>()
>= mem::align_of::<depot::raw::Node<links::raw::Chain>>()
);
assert!(
mem::align_of::<depot::raw::Node<links::raw::Chain>>()
>= mem::align_of::<Cell<links::Index>>()
);
}
let count = capacity::Count::try_count(capacity, alloc::Overflow)?;
let lanes = capacity::Count::try_narrow(lanes, alloc::Overflow)?;
let extent = Capacity::try_layout(count, lanes)?;
let block = alloc::raw::Uninit::try_planes(
extent,
(),
|index| {
(
depot::raw::Node::vacant(Cell::new(None), index as u32, links::Index::END),
Cell::new(links::Index::from(NonZero::<u32>::MIN)),
)
},
|index| {
depot::raw::Node::vacant(links::raw::Chain::new(), index as u32, links::Index::END)
},
)?;
Ok(Self {
block,
groups: [const { links::raw::Cycle::new() }; G],
})
}
}
impl<T: Copy, const G: usize, const N: usize> Values<T, G, N> {
pub fn try_fixed(capacity: usize) -> Result<Self, alloc::Error> {
const { assert!(N != 0 && N <= u32::MAX as usize, "lanes") };
let Values { block, groups } =
Values::<T, G>::try_new(capacity, NonZero::<usize>::MIN.saturating_add(N - 1))?;
Ok(Self { block, groups })
}
pub fn try_slot(&self, index: usize) -> Result<Slot<'_, T, G, N>, shared::Bounds> {
Ok(Slot {
owner: self,
index: round::raw::Entry::try_index::<false>(self, index)?,
})
}
pub fn slot(
source: depot::raw::Proof<(&Self, links::Index), capacity::Physical>,
) -> Slot<'_, T, G, N> {
let (owner, index) = source.into_inner();
Slot { owner, index }
}
pub fn try_lane(&self, index: usize) -> Result<Lane<'_, T, G, N>, round::Bounds> {
Ok(Lane {
owner: self,
index: round::raw::Entry::try_index::<true>(self, index).map_err(
|shared::Bounds { index, capacity }| round::Bounds::Lane { index, capacity },
)?,
})
}
pub fn lane<const MAX: u32>(&self, index: bounded::raw::U32<0, MAX>) -> Lane<'_, T, G, N> {
const { assert!((MAX as usize) < N, "lane") };
Lane {
owner: self,
index: index.into(),
}
}
pub fn group<const I: usize>(&self) -> Group<'_, T, G, N> {
const { assert!(I < G, "round group index exceeds the group count") };
Group {
owner: self,
ring: &self.groups[I],
}
}
pub fn has_pending(&self) -> bool {
self.groups.iter().any(|ring| !ring.is_empty())
}
}
impl<'a, T: Copy, const G: usize, const N: usize> Slot<'a, T, G, N> {
pub const fn index(&self) -> links::Index {
self.index
}
pub fn get(&self) -> Option<T> {
round::raw::Entry::cell::<false>(self).get()
}
pub fn write(&self, value: T) {
round::raw::Entry::cell::<true>(self).set(Some(value));
}
pub fn take(&self) -> Option<T> {
round::raw::Entry::cell::<true>(self).take()
}
pub fn try_lane(&self, index: usize) -> Result<Snapshot<'a, T, G, Place, N>, round::Bounds> {
Ok(self.place(self.owner.try_lane(index)?))
}
pub fn lane<const MAX: u32>(
&self,
index: bounded::raw::U32<0, MAX>,
) -> Snapshot<'a, T, G, Place, N> {
self.place(self.owner.lane(index))
}
fn place(&self, lane: Lane<'a, T, G, N>) -> Snapshot<'a, T, G, Place, N> {
Snapshot::new(
self.owner,
round::Position {
lane: lane.index,
index: self.index,
},
)
}
}
impl<'a, T: Copy, const G: usize, P, const N: usize> Snapshot<'a, T, G, P, N> {
fn new(owner: &'a Values<T, G, N>, position: round::Position) -> Self {
Self {
owner,
position,
phase: PhantomData,
}
}
}
impl<'a, T: Copy, const G: usize, const N: usize> Snapshot<'a, T, G, Place, N> {
pub fn slot(&self) -> Slot<'a, T, G, N> {
Slot {
owner: self.owner,
index: self.position.index,
}
}
pub fn insert(source: depot::raw::Proof<&Self, index::Initialized<Values<T, G, N>>>) {
round::raw::Entry::initialized(source).append::<false>();
}
pub fn push(&self, value: T) {
round::raw::Entry::written(self, value).append::<true>();
}
}
impl<'a, T: Copy, const G: usize, const N: usize> Lane<'a, T, G, N> {
pub fn front(&self) -> Option<(Snapshot<'a, T, G, Turn<false>, N>, T)> {
let head = round::raw::Head::front(self)?;
Some((Snapshot::new(head.owner, head.position), head.get()))
}
pub fn pop_front(&self) -> Option<T> {
Some(round::raw::Head::front(self)?.pop())
}
}
impl<'a, T: Copy, const G: usize, const N: usize> Group<'a, T, G, N> {
pub fn active(&self) -> Option<(Snapshot<'a, T, G, Turn, N>, T)> {
let head = round::raw::Head::active(self)?;
Some((Snapshot::new(head.owner, head.position), head.get()))
}
pub fn next(&self) -> Option<T> {
Some(round::raw::Head::active(self)?.pop())
}
}
impl<'a, T: Copy, const G: usize, const N: usize> depot::raw::Admit
for depot::raw::Proof<Lane<'a, T, G, N>, phase::Ready>
{
type Output = Option<round::raw::Head<'a, T, G, N>>;
fn admit(self) -> Self::Output {
round::raw::Head::front(&self.into_inner())
}
}
impl<'a, T: Copy, const G: usize, const N: usize, const FAIR: bool> depot::raw::Admit
for depot::raw::Proof<Snapshot<'a, T, G, Turn<FAIR>, N>, phase::Ready>
{
type Output = Option<round::raw::Head<'a, T, G, N, FAIR>>;
fn admit(self) -> Self::Output {
round::raw::Head::resolve(self.into_inner())
}
}
const _: () = {
assert!(mem::size_of::<depot::raw::Node<links::raw::Chain>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Cell<links::Index>>() == mem::size_of::<u32>());
assert!(mem::size_of::<Slot<'_, (), 1>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Snapshot<'_, (), 1, Place>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Option<Snapshot<'_, (), 1, Place>>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Lane<'_, (), 1>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Option<Lane<'_, (), 1>>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<round::raw::Head<'_, (), 1>>() == 2 * mem::size_of::<usize>());
};