pub use core::Core;
mod core;
use std::{mem::MaybeUninit, num::NonZero};
use crate::depot::{
arena::{
hash::{
self, errors,
runtime::{self, entry},
},
slab::key,
},
map,
queue::links,
};
pub(super) fn get<T, Tag>(
core: &Core<T, Tag>,
key: key::Handle<runtime::Domain<Tag>>,
) -> Option<&T> {
let index = core.lookup(key).ok()?;
Some(unsafe { core.value(index) })
}
pub(super) fn pair<T, Tag>(
core: &Core<T, Tag>,
index: usize,
) -> Option<(key::Handle<runtime::Domain<Tag>>, &T)> {
let (index, stamp) = core.live_at(index)?;
Some((key::Handle::new(index.get(), stamp), unsafe {
core.value(index)
}))
}
pub(super) fn values<T, Tag>(core: &Core<T, Tag>) -> impl Iterator<Item = &T> {
core.dense()
.iter()
.map(|&index| unsafe { core.value(index) })
}
pub(super) fn entry<T, Tag, P>(
map: &mut runtime::Map<T, Tag, P>,
key: key::Handle<runtime::Domain<Tag>>,
) -> Result<entry::Live<'_, T, Tag, P>, errors::Lookup<Tag>> {
let index = map.core.lookup(key)?;
Ok(unsafe { map.core.live(index) })
}
pub(super) fn entry_at<T, Tag, P>(
map: &mut runtime::Map<T, Tag, P>,
index: usize,
) -> Option<entry::Live<'_, T, Tag, P>> {
let (index, _) = map.core.live_at(index)?;
Some(unsafe { map.core.live(index) })
}
pub(super) fn phase<T, Tag>(entry: entry::Live<'_, T, Tag, ()>) -> entry::Phase<'_, T, Tag> {
unsafe {
if entry.core.meta(entry.index).bucket.is_some() {
entry::Phase::Indexed(entry.core.live(entry.index))
} else {
entry::Phase::Staged(entry.core.live(entry.index))
}
}
}
pub(super) fn stage<T, Tag>(
slot: hash::Vacant<'_, T, Tag>,
value: T,
) -> entry::Live<'_, T, Tag, entry::Staged> {
let hash::Vacant { core, index } = slot;
let position = core.len;
unsafe {
let next = core.meta(index).next();
core.values
.add(index.into_usize())
.write(MaybeUninit::new(value));
core.dense.add(position as usize).write(index);
core.meta_mut(index).slot = position;
core.free = next;
core.len += 1;
core.live(index)
}
}
unsafe fn publish<T, Tag>(
entry: entry::Live<'_, T, Tag, entry::Staged>,
bucket: map::raw::Vacant,
hash: u64,
) -> entry::Live<'_, T, Tag, entry::Indexed> {
let index = bucket.index();
unsafe {
entry.core.directory.insert(bucket, hash, entry.index);
entry.core.meta_mut(entry.index).bucket = Some(map::raw::Occupied::new(index));
entry.core.live(entry.index)
}
}
fn retire<T, Tag, const RETAIN: bool>(
entry: entry::Live<'_, T, Tag>,
) -> entry::Live<'_, T, Tag, entry::Staged> {
unsafe {
let meta = entry.core.meta_mut(entry.index);
let bucket = *crate::mem::raw::initialized(&meta.bucket);
if RETAIN {
meta.bucket = None;
}
map::raw::Hole::retire(&mut *entry.core, bucket);
entry.core.live(entry.index)
}
}
fn release<T, Tag>(entry: entry::Live<'_, T, Tag, entry::Staged>) -> &mut MaybeUninit<T> {
let core = entry.core;
let index = entry.index;
unsafe {
let position = core.meta(index).slot;
let last = NonZero::new_unchecked(core.len).get() - 1;
core.len = last;
if position != last {
let moved = core.dense.add(last as usize).read();
core.dense.add(position as usize).write(moved);
core.meta_mut(moved).slot = position;
}
let free = core.free;
let meta = core.meta_mut(index);
if let Some(next) = meta.generation.checked_add(1) {
meta.generation = next;
meta.slot = core::VACANT | free.map_or(core::Meta::FREE, links::Index::raw);
core.free = Some(index.into());
} else {
meta.slot = core::VACANT | core::Meta::FREE;
}
core.values.add(index.into_usize()).as_mut()
}
}
pub(super) fn discard<T, Tag>(entry: entry::Live<'_, T, Tag, entry::Staged>) {
unsafe { release(entry).assume_init_drop() };
}
pub(super) fn unindex<T, Tag>(
entry: entry::Live<'_, T, Tag, entry::Indexed>,
) -> entry::Live<'_, T, Tag, entry::Staged> {
retire::<_, _, true>(entry.occupied())
}
pub(super) fn erase<T, Tag>(entry: entry::Live<'_, T, Tag>) {
discard(retire::<_, _, false>(entry));
}
pub(super) fn remove<T, Tag>(entry: entry::Live<'_, T, Tag>) -> T {
unsafe { release(retire::<_, _, false>(entry)).assume_init_read() }
}
pub(super) fn reserve<'a, T, Tag, Q: Eq + ?Sized>(
entry: entry::Live<'a, T, Tag, entry::Staged>,
hash: u64,
key: &Q,
) -> entry::Reservation<entry::Live<'a, T, Tag, entry::Staged>, Tag>
where
T: AsRef<Q>,
{
match unsafe { map::raw::spare(entry, hash, |value| value.as_ref() == key) } {
map::Entry::Vacant(vacant) => Ok(vacant.with_phase(hash)),
map::Entry::Occupied(occupied) => Err(occupied),
}
}
pub(super) fn commit<T, Tag>(
slot: map::Vacancy<entry::Live<'_, T, Tag, entry::Staged>, u64>,
) -> entry::Live<'_, T, Tag, entry::Indexed> {
let (entry, bucket, hash) = slot.into_parts();
unsafe { publish(entry, bucket, hash) }
}
pub(super) fn insert<T, Tag>(
slot: entry::Vacancy<'_, T, Tag>,
value: T,
) -> entry::Live<'_, T, Tag> {
let entry = hash::Vacant {
core: slot.core,
index: slot.index,
}
.insert(value);
unsafe { publish(entry, slot.bucket, slot.hash) }.occupied()
}
pub(super) fn key<T, Tag, P>(
entry: &entry::Live<'_, T, Tag, P>,
) -> key::Handle<runtime::Domain<Tag>> {
unsafe { entry.core.key_for(entry.index) }
}
pub(super) fn value<'a, T, Tag, P>(entry: &'a entry::Live<'_, T, Tag, P>) -> &'a T {
unsafe { entry.core.value(entry.index) }
}
pub(super) fn value_mut<'a, T, Tag, P>(entry: &'a mut entry::Live<'_, T, Tag, P>) -> &'a mut T {
unsafe { entry.core.value_mut(entry.index) }
}
pub(super) fn into_value<T, Tag, P>(entry: entry::Live<'_, T, Tag, P>) -> &mut T {
unsafe { entry.core.value_mut(entry.index) }
}
pub(super) fn selected<T, Tag>(entry: &entry::Live<'_, T, Tag>) -> *mut T {
entry
.core
.values
.as_ptr()
.wrapping_add(entry.index.into_usize())
.cast::<T>()
}