use std::{cell, ptr};
use crate::depot::{
array::Index,
map::direct,
queue::{
indexed::lanes::{self, table},
links,
},
};
pub struct Slot<'a, K: crate::depot::raw::Key, V, const N: usize, const LIVE: bool> {
pub(in crate::depot::queue::indexed::lanes) root: &'a mut lanes::Table<K, V, N>,
pub(in crate::depot::queue::indexed::lanes) key: K,
pub(in crate::depot::queue::indexed::lanes) row:
ptr::NonNull<direct::raw::Entry<K, lanes::Node<V, N>>>,
}
impl<'a, K: crate::depot::raw::Key, V, const N: usize> Slot<'a, K, V, N, true> {
fn node(&self) -> &lanes::Node<V, N> {
unsafe { self.row.as_ref().value.assume_init_ref() }
}
pub fn get_mut(&mut self) -> &mut V {
&mut unsafe { self.row.as_mut().value.assume_init_mut() }.value
}
pub fn into_mut(self) -> &'a mut V {
&mut unsafe { (*self.row.as_ptr()).value.assume_init_mut() }.value
}
pub fn move_to_back(&mut self, target: Index<N>) {
let index = unsafe { links::Index::new_usize(self.key.index()) };
let hooks = &self.root.values;
let node = self.node();
let queue = target.get(&self.root.queues);
let vacant = match node.lane.get() {
Some(source) if source == target => {
unsafe { queue.rotate(hooks, index, &node.hook, false) };
return;
}
Some(source) => {
let chain = source.get(&self.root.queues);
unsafe { chain.detach_hook::<false, 0, _>(hooks, index, &node.hook) }.0
}
None => unsafe { links::Detached::new(&node.hook, index) },
};
unsafe { queue.push(hooks, vacant) };
node.lane.set(Some(target));
}
pub fn move_all(&mut self, source: Index<N>, target: Index<N>) -> usize {
table::move_all(&self.root.values, &self.root.queues, source, target)
}
pub fn unlink<const RETIRE: bool>(self) {
if RETIRE {
self.detach::<false>();
unsafe { self.root.values.vacate(self.row).as_ptr().drop_in_place() };
} else {
self.detach::<true>();
}
}
pub fn lane(&self) -> Option<Index<N>> {
self.node().lane.get()
}
pub fn remove(self) -> V {
self.detach::<false>();
let node = unsafe { self.root.values.vacate(self.row) };
unsafe { ptr::addr_of!((*node.as_ptr()).value).read() }
}
}
impl<'a, K: crate::depot::raw::Key, V, const N: usize> Slot<'a, K, V, N, false> {
pub fn insert(mut self, value: V) -> Slot<'a, K, V, N, true> {
unsafe { self.as_mut_ptr().as_ptr().write(value) };
unsafe { self.assume_init() }
}
}
impl<'a, K: crate::depot::raw::Key, V, const N: usize, const LIVE: bool> Slot<'a, K, V, N, LIVE> {
pub fn key(&self) -> K {
self.key
}
pub fn as_mut_ptr(&mut self) -> ptr::NonNull<V> {
let node =
unsafe { ptr::addr_of_mut!((*self.row.as_ptr()).value).cast::<lanes::Node<V, N>>() };
unsafe { ptr::NonNull::new_unchecked(ptr::addr_of_mut!((*node).value)) }
}
fn detach<const IDLE: bool>(&self) {
let (current, hook) = unsafe {
let node = ptr::addr_of!((*self.row.as_ptr()).value).cast::<lanes::Node<V, N>>();
(&*ptr::addr_of!((*node).lane), &*ptr::addr_of!((*node).hook))
};
if let Some(lane) = current.get() {
let index = unsafe { links::Index::new_usize(self.key.index()) };
let detached = unsafe {
lane.get(&self.root.queues).detach_hook::<false, 0, _>(
&self.root.values,
index,
hook,
)
}
.0;
if IDLE {
detached.idle();
current.set(None);
}
}
}
pub fn into_uninit(self) -> Slot<'a, K, V, N, false> {
if LIVE {
self.detach::<false>();
unsafe { self.root.values.vacate(self.row) };
}
Slot {
root: self.root,
key: self.key,
row: self.row,
}
}
pub unsafe fn assume_init(self) -> Slot<'a, K, V, N, true> {
if !LIVE {
let index = unsafe { links::Index::new_usize(self.key.index()) };
let node = unsafe {
ptr::addr_of_mut!((*self.row.as_ptr()).value).cast::<lanes::Node<V, N>>()
};
unsafe {
ptr::addr_of_mut!((*node).lane).write(cell::Cell::new(None));
ptr::addr_of_mut!((*node).hook).write(links::Hook::new(index));
self.root.values.publish(self.row, self.key);
}
}
Slot {
root: self.root,
key: self.key,
row: self.row,
}
}
}