use std::{mem, ops::ControlFlow};
use node::Node;
use crate::depot::{array::Index, queue::links};
pub(in crate::depot) mod node;
use lanes::entry;
use crate::depot::{capacity, index::Initialized, map::direct, queue::indexed::lanes};
pub struct Table<K: crate::depot::raw::Key, V, const N: usize> {
pub(in crate::depot) values: direct::Table<K, Node<V, N>>,
pub(in crate::depot) queues: [links::Chain; N],
}
unsafe impl<K: crate::depot::raw::Key, V, const N: usize> crate::depot::raw::Hooks
for direct::Table<K, Node<V, N>>
{
unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
&unsafe { self.value(index) }.hook
}
}
impl<K: crate::depot::raw::Key, V, const N: usize> Table<K, V, N> {
pub const NODE_BYTES: usize = mem::size_of::<direct::raw::Entry<K, Node<V, N>>>();
pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
const { assert!(N <= u8::MAX as usize) };
Ok(Self {
values: direct::Table::try_new(cap)?,
queues: [const { links::Chain::new() }; N],
})
}
pub const fn capacity(&self) -> usize {
self.values.capacity()
}
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
pub const fn len(&self) -> usize {
self.values.len()
}
pub fn get(&self, key: K) -> Option<&V> {
self.values.get(key).map(|node| &node.value)
}
pub fn entry_at(&self, index: usize) -> Option<(K, &V)> {
let (key, node) = self.values.entry_at(index)?;
Some((key, &node.value))
}
pub fn entry_at_mut(&mut self, index: usize) -> Option<entry::Occupied<'_, K, V, N>> {
let (key, row) = self.values.entry_row(index)?;
Some(entry::Slot {
root: self,
key,
row,
})
}
pub fn key_at(&self, index: usize) -> Option<K> {
self.values.key_at(index)
}
pub fn lane(&self, key: K) -> Option<Option<Index<N>>> {
Some(self.values.get(key)?.lane.get())
}
pub fn try_vacant(&mut self, key: K) -> Result<entry::Vacancy<'_, K, V, N>, direct::Error<K>> {
match self.select(key)? {
ControlFlow::Continue(vacant) => Ok(vacant),
ControlFlow::Break(occupied) => Err(direct::Error::Occupied {
key,
stored: occupied.key(),
}),
}
}
pub fn vacant(&mut self, key: K) -> Option<entry::Vacancy<'_, K, V, N>> {
self.select(key).ok()?.continue_value()
}
pub fn entry(&mut self, key: K) -> Option<entry::Occupied<'_, K, V, N>> {
self.select(key).ok()?.break_value()
}
pub fn select(
&mut self,
key: K,
) -> Result<
ControlFlow<entry::Occupied<'_, K, V, N>, entry::Vacancy<'_, K, V, N>>,
direct::Error<K>,
> {
Ok(match self.values.select_row(key)? {
ControlFlow::Break(row) => ControlFlow::Break(entry::Slot {
root: self,
key,
row,
}),
ControlFlow::Continue(row) => ControlFlow::Continue(entry::Slot {
root: self,
key,
row,
}),
})
}
pub fn front(&mut self, lane: Index<N>) -> Option<entry::Occupied<'_, K, V, N>> {
let index = lane.get(&self.queues).front()?;
let (key, row) = unsafe { direct::raw::Entry::occupied(&mut self.values, index) };
Some(entry::Slot {
root: self,
key,
row,
})
}
pub fn back(&self, lane: Index<N>) -> Option<K> {
let index = lane.get(&self.queues).last()?;
let live = unsafe {
crate::depot::raw::Proof::<(_, _), Initialized<()>>::new((&self.values, index))
};
Some(direct::Table::project(live).1)
}
pub fn is_vacant(&self, lane: Index<N>) -> bool {
lane.get(&self.queues).is_empty()
}
pub fn move_all(&mut self, source: Index<N>, target: Index<N>) -> usize {
move_all(&self.values, &self.queues, source, target)
}
pub fn clear(&mut self) {
if self.is_empty() {
return;
}
self.queues = [const { links::Chain::new() }; N];
self.values.clear();
}
}
pub(super) fn move_all<K: crate::depot::raw::Key, V, const N: usize>(
values: &direct::Table<K, Node<V, N>>,
queues: &[links::Chain; N],
source: Index<N>,
target: Index<N>,
) -> usize {
if source == target {
return 0;
}
let source = source.get(queues);
let Some(head) = source.first() else { return 0 };
let first = unsafe { values.value(head) };
let mut node = first;
let mut moved = 0;
loop {
node.lane.set(Some(target));
moved += 1;
let Some(next) = node.hook.next_index() else {
break;
};
node = unsafe { values.value(next) };
}
let target = target.get(queues);
let join = target
.last()
.map(|last| (&first.hook, last, &unsafe { values.value(last) }.hook));
unsafe { target.splice(source, head, join) };
moved
}