use std::{cell, mem};
use crate::depot::{queue::links, raw};
#[repr(C)]
#[derive(Debug)]
pub struct Chain<C: Copy = ()> {
head: cell::Cell<u32>,
tail: cell::Cell<u32>,
count: cell::Cell<C>,
}
impl<C: crate::depot::raw::ChainCount> Default for Chain<C> {
fn default() -> Self {
Self::new()
}
}
impl<C: crate::depot::raw::ChainCount> Chain<C> {
pub const fn new() -> Self {
Self {
head: cell::Cell::new(links::Index::END),
tail: cell::Cell::new(links::Index::END),
count: cell::Cell::new(C::EMPTY),
}
}
pub fn first(&self) -> Option<links::Index> {
links::Index::decode(self.head.get())
}
pub fn front(&self) -> Option<usize> {
self.first().map(links::Index::get)
}
pub fn last(&self) -> Option<links::Index> {
links::Index::decode(self.tail.get())
}
pub unsafe fn front_key(&self) -> links::Index {
unsafe { links::Index::new_raw(self.head.get()) }
}
pub fn is_empty(&self) -> bool {
self.head.get() == links::Index::END
}
pub unsafe fn link_back<const PLANE: usize, H: crate::depot::raw::Hooks<PLANE> + ?Sized>(
&self,
hooks: &H,
index: links::Index,
hook: &links::Hook,
) {
let vacant = unsafe { links::Detached::new(hook, index) };
unsafe { self.push::<PLANE, _>(hooks, vacant) };
}
pub(in crate::depot) unsafe fn push<const PLANE: usize, H: raw::Hooks<PLANE> + ?Sized>(
&self,
hooks: &H,
vacant: links::Detached<'_>,
) -> bool {
let last = self.last();
let edge = unsafe { self.next_edge::<PLANE, _>(hooks, last) };
let count = C::storage(&self.count);
unsafe { self.attach(vacant, (links::Index::encode(last), edge)) };
if let Some(count) = count {
count.set(count.get() + 1);
}
last.is_none()
}
unsafe fn next_edge<'a, const PLANE: usize, H: raw::Hooks<PLANE> + ?Sized>(
&'a self,
hooks: &'a H,
before: Option<links::Index>,
) -> &'a cell::Cell<u32> {
before.map_or(&self.head, |i| &unsafe { hooks.hook_raw(i) }.next)
}
unsafe fn attach(&self, vacant: links::Detached<'_>, (last, edge): (u32, &cell::Cell<u32>)) {
let (hook, index) = vacant.into_parts();
edge.set(index.raw());
hook.previous.set(last);
hook.next.set(links::Index::END);
self.tail.set(index.raw());
}
pub(in crate::depot) unsafe fn unlink<
'a,
const PLANE: usize,
H: crate::depot::raw::Hooks<PLANE> + ?Sized,
>(
&'a self,
hooks: &'a H,
index: links::Index,
) -> links::Detached<'a> {
let hook = unsafe { hooks.hook_raw(index) };
unsafe { self.detach_hook::<false, PLANE, _>(hooks, index, hook) }.0
}
pub(in crate::depot) unsafe fn pop<'a, const PLANE: usize, H: raw::Hooks<PLANE> + ?Sized>(
&'a self,
hooks: &'a H,
index: links::Index,
) -> (links::Detached<'a>, bool) {
let hook = unsafe { hooks.hook_raw(index) };
unsafe { self.detach_hook::<true, PLANE, _>(hooks, index, hook) }
}
#[inline]
pub unsafe fn detach_hook<
'a,
const FRONT: bool,
const PLANE: usize,
H: raw::Hooks<PLANE> + ?Sized,
>(
&'a self,
hooks: &'a H,
index: links::Index,
hook: &'a links::Hook,
) -> (links::Detached<'a>, bool) {
let after = hook.next_index();
let vacant = unsafe { self.bypass::<PLANE, _>(hooks, index, hook, FRONT, after) };
if let Some(count) = C::storage(&self.count) {
count.set(count.get() - 1);
}
(vacant, after.is_none())
}
unsafe fn bypass<'a, const PLANE: usize, H: raw::Hooks<PLANE> + ?Sized>(
&self,
hooks: &H,
index: links::Index,
hook: &'a links::Hook,
front: bool,
after: Option<links::Index>,
) -> links::Detached<'a> {
let before = if front { None } else { hook.prev_index() };
let (left, right) = unsafe { self.neighbors::<PLANE, _>(hooks, before, after) };
left.set(links::Index::encode(after));
right.set(links::Index::encode(before));
unsafe { links::Detached::new(hook, index) }
}
unsafe fn neighbors<'a, const PLANE: usize, H: raw::Hooks<PLANE> + ?Sized>(
&'a self,
hooks: &'a H,
before: Option<links::Index>,
after: Option<links::Index>,
) -> (&'a cell::Cell<u32>, &'a cell::Cell<u32>) {
let left = unsafe { self.next_edge::<PLANE, _>(hooks, before) };
let right = after.map_or(&self.tail, |i| &unsafe { hooks.hook_raw(i) }.previous);
(left, right)
}
pub unsafe fn rotate<const PLANE: usize, H: raw::Hooks<PLANE> + ?Sized>(
&self,
hooks: &H,
index: links::Index,
hook: &links::Hook,
front: bool,
) {
let Some(next) = hook.next_index() else {
return;
};
let last = self.tail.get();
let edge = &unsafe { hooks.hook_raw(links::Index::new_raw(last)) }.next;
let vacant = unsafe { self.bypass::<PLANE, _>(hooks, index, hook, front, Some(next)) };
unsafe { self.attach(vacant, (last, edge)) };
}
}
impl Chain {
pub(in crate::depot) fn set_first(&self, index: Option<links::Index>) {
self.head.set(links::Index::encode(index));
}
pub(in crate::depot) fn set_last(&self, index: Option<links::Index>) {
self.tail.set(links::Index::encode(index));
}
pub unsafe fn splice(
&self,
source: &Self,
head: links::Index,
join: Option<(&links::Hook, links::Index, &links::Hook)>,
) {
if let Some((first, last, back)) = join {
back.next.set(head.raw());
first.previous.set(last.raw());
} else {
self.head.set(head.raw());
}
self.tail.set(source.tail.get());
source.head.set(links::Index::END);
source.tail.set(links::Index::END);
}
}
impl Chain<u32> {
pub fn len(&self) -> usize {
self.count.get() as usize
}
}
const _: () = assert!(mem::size_of::<Chain>() == 2 * mem::size_of::<u32>());