use std::{marker, ops::ControlFlow, ptr};
pub use error::Error;
pub use occupied::Occupied;
pub use vacant::Vacant;
use crate::{
alloc::array,
depot::{
capacity,
index::{Initialized, Stored},
map::direct,
queue::links,
},
};
mod error;
mod occupied;
pub(in crate::depot) mod raw;
mod vacant;
pub struct Table<K: crate::depot::raw::Key, V> {
pub(in crate::depot) block: array::Block<raw::Entry<K, V>, capacity::Count, capacity::Count>,
}
impl<K: crate::depot::raw::Key, V> Table<K, V> {
pub fn try_new(capacity: capacity::Count) -> Result<Self, crate::alloc::Error> {
let block = array::Block::try_new(capacity, capacity::Count::EMPTY)?
.repeat_with(raw::Entry::vacant);
Ok(Self { block })
}
pub const fn capacity(&self) -> usize {
self.block.extent().get()
}
pub const fn len(&self) -> usize {
self.block.state().get()
}
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<K: crate::depot::raw::Key, V> Table<K, V> {
pub(in crate::depot) unsafe fn value(&self, index: links::Index) -> &V {
unsafe {
self.block
.get_unchecked(index.get())
.value
.assume_init_ref()
}
}
pub(in crate::depot) unsafe fn publish(&mut self, row: ptr::NonNull<raw::Entry<K, V>>, key: K) {
unsafe { ptr::addr_of_mut!((*row.as_ptr()).key).write(key.into_raw()) };
let len = self.block.state_mut();
*len = capacity::Count::new((len.get() + 1) as u32);
}
pub(in crate::depot) unsafe fn vacate(
&mut self,
row: ptr::NonNull<raw::Entry<K, V>>,
) -> ptr::NonNull<V> {
let len = self.block.state_mut();
*len = capacity::Count::new((len.get() - 1) as u32);
unsafe {
ptr::addr_of_mut!((*row.as_ptr()).key).write(K::VACANT);
ptr::NonNull::new_unchecked(ptr::addr_of_mut!((*row.as_ptr()).value).cast())
}
}
pub fn get(&self, key: K) -> Option<&V> {
let slot = self.block.get(key.index())?;
(slot.key == key.into_raw()).then(|| unsafe { slot.value.assume_init_ref() })
}
pub fn entry_at(&self, index: usize) -> Option<(K, &V)> {
self.block.get(index)?.read()
}
pub(in crate::depot) fn entry_row(
&mut self,
index: usize,
) -> Option<(K, ptr::NonNull<raw::Entry<K, V>>)> {
let row = self.block.get_mut(index)?;
let key = row.read()?.0;
Some((key, ptr::NonNull::from(row)))
}
pub fn entry_at_mut(&mut self, index: usize) -> Option<direct::Occupied<'_, K, V>> {
let (key, _) = self.entry_row(index)?;
Some(direct::Occupied {
table: self,
key,
plane: marker::PhantomData,
})
}
pub fn key_at(&self, index: usize) -> Option<K> {
self.block.get(index)?.read().map(|(key, _)| key)
}
pub fn iter(&self) -> impl Iterator<Item = (K, &V)> {
self.block.iter().filter_map(raw::Entry::read)
}
pub fn project<P>(
live: crate::depot::raw::Proof<(&Self, links::Index), Initialized<P>>,
) -> (ptr::NonNull<V>, K) {
let (read, index) = live.into_inner();
unsafe {
let slot = read.block.as_ptr().add(index.get());
let key = K::from_raw(Stored::new(ptr::addr_of!((*slot).key).read()));
let value = ptr::NonNull::new_unchecked(ptr::addr_of!((*slot).value).cast_mut().cast());
(value, key)
}
}
}
impl<K: crate::depot::raw::Key, V> Table<K, V> {
pub fn entry(&mut self, key: K) -> Option<direct::Occupied<'_, K, V>> {
self.select(key).ok()?.break_value()
}
pub fn try_vacant(&mut self, key: K) -> Result<direct::Vacant<'_, K, V>, 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<direct::Vacant<'_, K, V>> {
self.select(key).ok()?.continue_value()
}
pub fn select(
&mut self,
key: K,
) -> Result<ControlFlow<direct::Occupied<'_, K, V>, direct::Vacant<'_, K, V>>, direct::Error<K>>
{
Ok(match self.select_row(key)? {
ControlFlow::Break(_) => ControlFlow::Break(direct::Occupied {
table: self,
key,
plane: marker::PhantomData,
}),
ControlFlow::Continue(_) => ControlFlow::Continue(direct::Vacant {
table: self,
key,
plane: marker::PhantomData,
}),
})
}
pub(in crate::depot) fn select_row(
&mut self,
key: K,
) -> Result<
ControlFlow<ptr::NonNull<raw::Entry<K, V>>, ptr::NonNull<raw::Entry<K, V>>>,
direct::Error<K>,
> {
let index = key.index();
let capacity = self.capacity();
let row = self.block.get_mut(index).ok_or(direct::Error::Outside {
key,
index,
capacity,
})?;
let stored = row.key;
let row = ptr::NonNull::from(row);
if stored == key.into_raw() {
return Ok(ControlFlow::Break(row));
}
if stored == K::VACANT {
return Ok(ControlFlow::Continue(row));
}
let stored = K::from_raw(unsafe { Stored::new(stored) });
Err(direct::Error::Occupied { key, stored })
}
pub fn clear(&mut self) {
if self.is_empty() {
return;
}
let (len, entries) = self.block.parts_mut();
for entry in entries {
if entry.key != K::VACANT {
*entry = raw::Entry::vacant();
}
}
*len = capacity::Count::EMPTY;
}
}