use std::{convert::Infallible, marker::PhantomData, mem};
use crate::{
alloc::{self, BoxSliceExt as _, VecExt as _},
base::marker::ThreadBound,
depot::{
map,
map::resident::{self, Key, Occupied, Plan, Vacant, scoped},
queue::links,
},
};
pub struct Table<T, H, const MAX: u32> {
pub(in crate::depot::map::resident) records:
Vec<resident::raw::Record<mem::MaybeUninit<T>, MAX>>,
pub(in crate::depot::map::resident) buckets: Box<[Option<links::Index>]>,
pub(super) free: Option<links::Index>,
pub(super) available: u32,
pub(in crate::depot::map::resident) hash: H,
bound: PhantomData<ThreadBound>,
}
impl<T, H: map::Hasher<resident::Id>, const MAX: u32> Table<T, H, MAX> {
pub fn try_new(plan: Plan, hash: H) -> Result<Self, alloc::Error> {
let records = Vec::try_new(plan.records as usize)?;
let buckets = if plan.records == 0 {
0
} else {
plan.buckets.get() as usize
};
Ok(Self {
records,
buckets: Box::try_repeat(buckets, None)?,
free: None,
available: plan.records,
hash,
bound: PhantomData,
})
}
pub const fn available(&self) -> usize {
self.available as usize
}
pub fn get(&self, key: Key<MAX>) -> Option<&T> {
Some(&self.record(key, None)?.value)
}
pub fn lookup(&self, key: Key<MAX>, id: resident::Id) -> Option<&T> {
Some(&self.record(key, Some(id))?.value)
}
pub fn id(&self, key: Key<MAX>) -> Option<resident::Id> {
Some(self.record(key, None)?.id())
}
pub fn find(&self, id: resident::Id) -> Option<(Key<MAX>, &T)> {
let Ok(found) =
map::raw::scan::<_, Infallible, _, true>(self, self.hash.hash(&id), |cell| {
let row = unsafe { resident::raw::published(self, cell.1) };
Ok(row.has_id(id).then_some((cell.1, row)))
});
let map::Entry::Occupied((index, row)) = found? else {
return None;
};
Some((Key::new(index, row.generation), &row.value))
}
pub fn key(&self, id: resident::Id) -> Option<Key<MAX>> {
Some(self.find(id)?.0)
}
pub fn entry(
&mut self,
id: resident::Id,
) -> map::Entry<Occupied<'_, T, H, MAX>, Option<Vacant<'_, T, H, MAX>>> {
let Ok(found) =
map::raw::scan::<_, Infallible, _, true>(self, self.hash.hash(&id), |cell| {
let row = unsafe { resident::raw::published(self, cell.1) };
Ok(row.has_id(id).then_some(cell))
});
match found {
Some(map::Entry::Occupied((bucket, index))) => map::Entry::Occupied(Occupied {
table: self,
bucket: Some(bucket),
index,
}),
Some(map::Entry::Vacant(bucket)) if self.available != 0 => {
map::Entry::Vacant(Some(Vacant {
table: self,
bucket,
id,
}))
}
Some(map::Entry::Vacant(_)) | None => map::Entry::Vacant(None),
}
}
pub fn exact(
&mut self,
key: Key<MAX>,
id: resident::Id,
) -> Result<Occupied<'_, T, H, MAX>, &mut Self> {
if self.record(key, Some(id)).is_none() {
return Err(self);
}
Ok(Occupied {
table: self,
bucket: None,
index: resident::raw::slot(key),
})
}
pub fn scope<R>(
&mut self,
run: impl for<'id> FnOnce(scoped::Scope<'_, 'id, T, H, MAX>) -> R,
) -> R {
run(scoped::Scope::new(self))
}
fn record(
&self,
key: Key<MAX>,
id: Option<resident::Id>,
) -> Option<&resident::raw::Record<T, MAX>> {
let record = self.records.get(key.index() as usize)?;
let live = match id {
Some(id) => record.has_id(id),
None => record.is_live(),
};
if live && record.generation.get() == key.generation() {
Some(unsafe { record.live() })
} else {
None
}
}
}
impl<T, H, const MAX: u32> Drop for Table<T, H, MAX> {
fn drop(&mut self) {
if mem::needs_drop::<T>() {
for record in &mut self.records {
if record.is_live() {
unsafe { record.value.assume_init_drop() };
}
}
}
}
}