use std::cell::Cell;
use crate::{
alloc,
depot::queue::{
indexed::shared,
links, resident,
round::{self, phase, values},
},
depot::{self, index},
};
type Value<T> = depot::raw::Node<Cell<Option<T>>>;
type Track = depot::raw::Node<links::raw::Chain>;
type Place<'a, T, const G: usize, const N: usize> = values::Snapshot<'a, T, G, values::Place, N>;
#[derive(Debug)]
pub struct Join<'a, H: ?Sized, L: ?Sized> {
pub hooks: &'a H,
pub lanes: &'a L,
pub entries: &'a links::raw::Chain,
pub active: &'a links::raw::Cycle,
pub index: links::Index,
}
impl<'a, H: depot::raw::Hooks + ?Sized, L: depot::raw::Hooks + ?Sized> Join<'a, H, L> {
pub fn insert(source: depot::raw::Proof<(Self, links::Index), phase::Vacant>) {
let (lane, index) = source.into_inner();
let vacant = unsafe { links::Detached::new(lane.hooks.hook_raw(index), index) };
unsafe { lane.push(vacant) };
}
pub(in crate::depot::queue) unsafe fn push(self, vacant: links::Detached<'a>) {
if let links::raw::Insertion::First = unsafe { self.entries.push(self.hooks, vacant) } {
unsafe { self.active.push(self.lanes, self.index) };
}
}
pub fn remove(
source: depot::raw::Proof<(Self, links::Index), phase::Linked>,
) -> links::Detached<'a> {
let (lane, index) = source.into_inner();
let hook = unsafe { lane.hooks.hook_raw(index) };
unsafe { lane.detach::<false, false, false>(index, hook) }
}
pub fn active<const ROTATE: bool>(
source: depot::raw::Proof<Self, phase::Active>,
) -> links::Detached<'a> {
let lane = source.into_inner();
let index = unsafe { lane.entries.front_key() };
let hook = unsafe { lane.hooks.hook_raw(index) };
unsafe { lane.detach::<true, true, ROTATE>(index, hook) }
}
pub fn head<const ROTATE: bool>(
source: depot::raw::Proof<(Self, links::Index), phase::Head>,
) -> links::Detached<'a> {
let (lane, index) = source.into_inner();
let hook = unsafe { lane.hooks.hook_raw(index) };
unsafe { lane.detach::<true, false, ROTATE>(index, hook) }
}
pub(in crate::depot::queue) unsafe fn detach<
const FRONT: bool,
const ACTIVE: bool,
const ROTATE: bool,
>(
self,
index: links::Index,
hook: &'a links::Hook,
) -> links::Detached<'a> {
let (vacant, last) = unsafe {
self.entries
.detach_hook::<FRONT, 0, _>(self.hooks, index, hook)
};
if last && (FRONT || self.entries.is_empty()) {
unsafe { self.active.unlink::<ACTIVE, 0>(self.lanes, self.index) };
} else if ROTATE {
unsafe { self.active.requeue::<ACTIVE, 0>(self.lanes, self.index) };
}
vacant
}
}
impl<'a, T: Copy> Join<'a, [Value<T>], [Track]> {
unsafe fn values<const G: usize, const N: usize>(
owner: &'a values::Values<T, G, N>,
index: links::Index,
) -> Self {
let lanes = owner.block.tail();
Self {
hooks: owner.block.as_ref(),
lanes,
entries: unsafe { resident::raw::View::new(lanes, index) }.into_ref(),
active: &owner.groups[index.get() % G],
index,
}
}
}
pub(super) struct Entry<'p, 'a, T: Copy, const G: usize, const N: usize> {
place: &'p Place<'a, T, G, N>,
node: &'a Value<T>,
}
impl<'p, 'a, T: Copy, const G: usize, const N: usize> Entry<'p, 'a, T, G, N> {
pub(super) fn try_index<const LANE: bool>(
owner: &values::Values<T, G, N>,
index: usize,
) -> Result<links::Index, shared::Bounds> {
let extent = owner.block.extent();
let capacity = if LANE {
alloc::raw::Shape::tail_len(extent)
} else {
alloc::raw::Shape::len(extent)
};
if index >= capacity {
return Err(shared::Bounds { index, capacity });
}
Ok(unsafe { links::Index::new_usize(index) })
}
pub(super) fn cell<const CLEAR: bool>(slot: &values::Slot<'a, T, G, N>) -> &'a Value<T> {
let index = slot.index;
let node = unsafe { resident::raw::View::new(slot.owner.block.as_ref(), index) }.into_ref();
if CLEAR && index.is_linked(node.hook()) {
let lane = unsafe { slot.owner.block.values().get_unchecked(index.get()) }.get();
unsafe {
Join::values(slot.owner, lane).detach::<false, false, false>(index, node.hook())
}
.idle();
}
node
}
pub(super) fn written(place: &'p Place<'a, T, G, N>, value: T) -> Self {
let node = Self::cell::<false>(&place.slot());
node.set(Some(value));
Self { place, node }
}
pub(super) fn initialized(
source: depot::raw::Proof<
&'p Place<'a, T, G, N>,
index::Initialized<values::Values<T, G, N>>,
>,
) -> Self {
let place = source.into_inner();
Self {
place,
node: Self::cell::<false>(&place.slot()),
}
}
pub(super) fn append<const REQUEUE: bool>(self) {
let owner = self.place.owner;
let round::Position { index, lane } = self.place.position;
let hook = self.node.hook();
let saved = unsafe { owner.block.values().get_unchecked(index.get()) };
let vacant = if index.is_linked(hook) {
let source = saved.get();
if !REQUEUE && source == lane {
return;
}
unsafe { Join::values(owner, source).detach::<false, false, false>(index, hook) }
} else {
unsafe { links::Detached::new(hook, index) }
};
unsafe { Join::values(owner, lane).push(vacant) };
saved.set(lane);
}
}
pub struct Head<'a, T: Copy, const G: usize, const N: usize = 0, const ACTIVE: bool = false> {
pub(super) owner: &'a values::Values<T, G, N>,
pub(super) position: round::Position,
}
impl<'a, T: Copy, const G: usize, const N: usize, const ACTIVE: bool> Head<'a, T, G, N, ACTIVE> {
pub(super) fn resolve(
snapshot: values::Snapshot<'a, T, G, values::Turn<ACTIVE>, N>,
) -> Option<Self> {
let owner = snapshot.owner;
let position = snapshot.position;
let lane = unsafe { resident::raw::View::new(owner.block.tail(), position.lane) };
(lane.first() == Some(position.index)
&& (!ACTIVE || owner.groups[position.lane.get() % G].first() == Some(position.lane)))
.then_some(Self { owner, position })
}
pub const fn index(&self) -> usize {
self.position.index.get()
}
fn node(&self) -> &Value<T> {
unsafe { resident::raw::View::new(self.owner.block.as_ref(), self.position.index) }
.into_ref()
}
pub fn get(&self) -> T {
unsafe { *crate::mem::raw::initialized(&self.node().get()) }
}
pub fn pop(self) -> T {
let node = self.node();
let lane = unsafe { Join::values(self.owner, self.position.lane) };
unsafe { lane.detach::<true, ACTIVE, ACTIVE>(self.position.index, node.hook()) }.idle();
unsafe { *crate::mem::raw::initialized(&node.take()) }
}
}
impl<'a, T: Copy, const G: usize, const N: usize> Head<'a, T, G, N> {
pub(super) fn front(source: &values::Lane<'a, T, G, N>) -> Option<Self> {
let owner = source.owner;
let lane = source.index;
let index = unsafe { resident::raw::View::new(owner.block.tail(), lane) }.first()?;
Some(Self {
owner,
position: round::Position { lane, index },
})
}
pub fn rotate(&mut self) {
let lane = unsafe { Join::values(self.owner, self.position.lane) };
let next = unsafe {
lane.entries
.rotate(lane.hooks, self.position.index, self.node().hook(), true)
};
unsafe { lane.active.requeue::<false, 0>(lane.lanes, lane.index) };
self.position.index = next;
}
}
impl<'a, T: Copy, const G: usize, const N: usize> Head<'a, T, G, N, true> {
pub(super) fn active(group: &values::Group<'a, T, G, N>) -> Option<Self> {
let owner = group.owner;
let lane = group.ring.first()?;
let index = unsafe { resident::raw::View::new(owner.block.tail(), lane).front_key() };
Some(Self {
owner,
position: round::Position { lane, index },
})
}
}