use std::{mem, ops, ptr::NonNull};
use crate::depot::{
array::{self, Index},
queue::{
group::{self, map},
indexed::slab::phase,
links,
},
};
pub struct Entry<'a, V, const LANES: usize, const N: usize = 1> {
ledger: &'a mut group::Ledger<V, LANES>,
indices: [links::Index; N],
lanes: [Index<LANES>; N],
bindings: [map::Binding; N],
}
impl<V, const LANES: usize, const N: usize> Drop for Entry<'_, V, LANES, N> {
fn drop(&mut self) {
for (&index, &lane) in self.indices.iter().zip(&self.lanes) {
drop(group::Vacant {
core: &mut self.ledger.entries,
index,
phase: phase::Target(lane),
});
}
for binding in self.bindings.iter().rev() {
if let map::Origin::Inserted(branch) = binding.origin {
let map = &mut self.ledger.map;
let key = map.get(&binding.owner).key;
map.remove(&binding.owner, branch, key);
}
}
}
}
impl<'a, V, const LANES: usize, const N: usize> Entry<'a, V, LANES, N> {
pub(super) fn new(
batch: group::Batch<'a, V, LANES, N>,
owners: [(Index<LANES>, u64); N],
) -> Result<Self, group::Error<LANES>> {
let ledger = batch.ledger;
let mut storage = mem::MaybeUninit::uninit();
let bound = match array::init::Guard::<map::Binding, N>::new(&mut storage).next() {
ops::ControlFlow::Break(full) => full,
ops::ControlFlow::Continue(slot) => {
let &(lane, key) = slot.get(&owners);
let (mut root, first) = ledger
.map
.intern(key)
.map_err(|cause| group::Error::owner(lane, key, cause))?;
let mut bindings = slot.insert(first);
loop {
let slot = match bindings.next() {
ops::ControlFlow::Break(full) => break full,
ops::ControlFlow::Continue(slot) => slot,
};
let &(lane, key) = slot.get(&owners);
let binding = match slot.zip(&owners).find(|(_, input)| input.1 == key) {
Some((prior, _)) => map::Binding::existing(&prior.owner),
None => match root.intern(key) {
Ok(binding) => binding,
Err(cause) => {
let map = root.into_inner();
for (prior, input) in slot.zip(&owners).rev() {
if let map::Origin::Inserted(branch) = prior.origin {
map.remove(&prior.owner, branch, input.1);
}
}
return Err(group::Error::owner(lane, key, cause));
}
},
};
bindings = slot.insert(binding);
}
}
};
let bindings = bound.into_array();
let (_, indices) =
unsafe { group::batch::Slots::<_, LANES, N>::new_unchecked(&mut ledger.entries) }
.into_parts();
Ok(Self {
ledger,
indices,
lanes: owners.map(|(lane, _)| lane),
bindings,
})
}
pub unsafe fn value_ptr(&mut self, index: usize) -> NonNull<V> {
let index = unsafe { self.indices.get_unchecked(index) };
unsafe {
self.ledger
.entries
.block
.values()
.add(index.get())
.cast::<group::Entry<V>>()
.byte_add(mem::offset_of!(group::Entry<V>, value))
.cast()
}
}
pub fn insert(mut self, values: [V; N]) {
for (index, value) in values.into_iter().enumerate() {
unsafe { self.value_ptr(index).as_ptr().write(value) };
}
unsafe { self.assume_init() };
}
pub unsafe fn assume_init(self) {
let mut this = mem::ManuallyDrop::new(self);
let Self {
ledger,
indices,
lanes,
bindings,
} = &mut *this;
for ((&index, &lane), binding) in indices.iter().zip(lanes.iter()).zip(bindings.iter()) {
let slot = group::Vacant {
core: &mut ledger.entries,
index,
phase: phase::Target(lane),
};
let value = slot.value().cast::<group::Entry<V>>();
unsafe {
std::ptr::addr_of_mut!((*value).owner).write(map::Node::new(binding.owner.index()));
std::ptr::addr_of_mut!((*value).owner_next).write(None);
}
let slot = unsafe { slot.assume_init() };
let key = slot.key();
let (core, _) = slot.into_owner();
let entry = ledger.map.get_mut(&binding.owner);
match lane
.get_mut(&mut entry.tails)
.replace(map::Node::inserted(key))
{
Some(previous) => {
previous.bind(core).as_mut().owner_next = Some(map::Node::inserted(key));
}
None => *lane.get_mut(&mut entry.heads) = Some(map::Node::inserted(key)),
}
*lane.get_mut(&mut ledger.lane_lengths) += 1;
}
}
}