use std::{
mem, ops,
ptr::{self, NonNull},
};
use crate::depot::{
array::{Index, init},
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 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 prior = slot
.zip(&owners)
.find_map(|(prior, input)| (input.1 == key).then_some(prior));
let binding = match prior {
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::Slots::<_, LANES, N>::from_source(&mut ledger.entries) }.into_parts();
Ok(Self {
ledger,
indices,
lanes: owners.map(|owner| owner.0),
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(self, values: [V; N]) {
self.insert_with(values, |_lane, _value| {});
}
pub fn insert_with<T: Into<V>>(
mut self,
values: [T; N],
mut stored: impl FnMut(Index<LANES>, &V),
) {
let mut index = 0;
let _: [(); N] = values.map(|value| {
let lane = self.lanes[index];
let slot = unsafe { self.value_ptr(index).cast::<mem::MaybeUninit<V>>().as_mut() };
stored(lane, slot.write(value.into()));
index += 1;
});
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 {
ptr::addr_of_mut!((*value).owner).write(map::Node::new(binding.owner.index()));
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;
}
}
}