use std::{cell::Cell, mem, pin::Pin, ptr::NonNull};
use crate::depot::{
self,
queue::{links, tagged},
};
type Pointer<T> = Option<NonNull<depot::raw::Entry<T>>>;
#[repr(C)]
pub struct Links<T> {
previous: Cell<Pointer<T>>,
next: Cell<Pointer<T>>,
}
impl<T> Default for Links<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Links<T> {
pub const fn new() -> Self {
Self {
previous: Cell::new(None),
next: Cell::new(None),
}
}
pub fn front(&self) -> Option<Pin<&depot::raw::Entry<T>>> {
Some(unsafe { Pin::new_unchecked(self.next.get()?.as_ref()) })
}
pub fn is_empty(&self) -> bool {
self.next.get().is_none()
}
unsafe fn neighbor(&self, item: Pointer<T>) -> &Self {
match item {
Some(node) => &unsafe { node.as_ref() }.links,
None => self,
}
}
pub unsafe fn push_back(&self, entry: Pin<&depot::raw::Entry<T>>) {
let node = entry.get_ref();
let item = Some(NonNull::from(node));
let previous = self.previous.get();
node.links.previous.set(previous);
node.links.next.set(None);
unsafe { self.neighbor(previous) }.next.set(item);
self.previous.set(item);
}
pub unsafe fn remove(&self, entry: Pin<&depot::raw::Entry<T>>) {
let previous = entry.links.previous.get();
let next = entry.links.next.get();
unsafe { self.neighbor(previous) }.next.set(next);
unsafe { self.neighbor(next) }.previous.set(previous);
}
}
const _: () = assert!(mem::size_of::<Links<usize>>() == 2 * mem::size_of::<usize>());
pub(in crate::depot) struct EndpointHook<'a> {
pub(in crate::depot) previous: &'a Cell<u32>,
pub(in crate::depot) next: &'a Cell<u32>,
}
impl<'a> From<&'a links::Hook> for EndpointHook<'a> {
fn from(hook: &'a links::Hook) -> Self {
Self {
previous: &hook.previous,
next: &hook.next,
}
}
}
impl<'a> EndpointHook<'a> {
pub(in crate::depot) unsafe fn unlink<
const PLANE: usize,
H: tagged::raw::Head,
S: crate::depot::raw::Hooks<PLANE> + ?Sized,
>(
self,
head: &'a H,
hooks: &S,
) {
let before = self.previous.get();
let after = self.next.get();
let neighbor = |word| match links::Index::decode(word) {
Some(index) => Neighbor::node(word, unsafe { hooks.hook_raw(index) }.into()),
None => Neighbor::head(word, head),
};
unsafe { Self::detach(neighbor(before), neighbor(after)) };
}
}
pub(in crate::depot) struct Neighbor<'a, H> {
link: u32,
target: Target<'a, H>,
}
enum Target<'a, H> {
Node(EndpointHook<'a>),
Head(&'a H),
}
impl<'a, H> Neighbor<'a, H> {
pub(in crate::depot) fn node(link: u32, hook: EndpointHook<'a>) -> Self {
Self {
link,
target: Target::Node(hook),
}
}
pub(in crate::depot) fn head(link: u32, head: &'a H) -> Self {
Self {
link,
target: Target::Head(head),
}
}
}
impl EndpointHook<'_> {
pub(in crate::depot) unsafe fn push<const FRONT: bool, H: tagged::raw::Head>(
self,
head: &H,
index: links::Index,
endpoint: u32,
adjacent: Option<(links::Index, EndpointHook<'_>)>,
) {
const { assert!(FRONT || H::TAIL) };
let adjacent = adjacent.map(|(adjacent, hook)| {
let neighbor = if FRONT { hook.previous } else { hook.next };
neighbor.set(index.raw());
adjacent.raw()
});
let word = adjacent.unwrap_or(endpoint);
self.previous.set(if FRONT { endpoint } else { word });
self.next.set(if FRONT { word } else { endpoint });
if adjacent.is_none() {
if FRONT {
if let Some(tail) = head.tail() {
tail.set_last(Some(index));
}
} else {
unsafe { head.set_first(Some(index)) };
}
}
if FRONT {
unsafe { head.set_first(Some(index)) };
} else if let Some(tail) = head.tail() {
tail.set_last(Some(index));
}
}
pub(in crate::depot) unsafe fn detach<H: tagged::raw::Head>(
previous: Neighbor<'_, H>,
next: Neighbor<'_, H>,
) {
let before = previous.link;
let after = next.link;
match previous.target {
Target::Node(hook) => hook.next.set(after),
Target::Head(head) => unsafe {
head.set_first((after != before).then(|| links::Index::new_raw(after)))
},
}
match next.target {
Target::Node(hook) => hook.previous.set(before),
Target::Head(head) => {
if let Some(tail) = head.tail() {
tail.set_last(
(before != after).then(|| unsafe { links::Index::new_raw(before) }),
);
}
}
}
}
pub(in crate::depot) unsafe fn clear(self, index: links::Index) {
self.previous.set(index.raw());
self.next.set(links::Index::END);
}
}