use std::{mem, num, ops::ControlFlow};
pub use bounds::Bounds;
use crate::depot::{
queue::{
indexed::{exact, fifo},
links,
},
raw::Live,
};
mod bounds;
impl<T: Copy + Eq, I: crate::depot::raw::Compact> fifo::List<T, I> {
pub fn try_sweep<C, E, B>(
&mut self,
limit: usize,
acquire: impl FnOnce(num::NonZero<usize>) -> Result<C, E>,
mut visit: impl FnMut(C, exact::Entry<'_, T, I>, T) -> ControlFlow<B, C>,
) -> Result<Option<ControlFlow<B, C>>, E> {
let Some(count) = num::NonZero::new(limit.min(self.len())) else {
return Ok(None);
};
Ok(Some((0..count.get()).try_fold(
acquire(count)?,
|context, _| {
let index = unsafe { self.store.chain::<0>().front_key() };
let value = unsafe { self.store.at(index.get()).take_head::<0>(index) };
let index = unsafe { I::from_raw(index.raw()) };
visit(context, exact::Entry { owner: self, index }, value)
},
)))
}
pub fn try_entry(&mut self, index: I) -> Result<exact::Entry<'_, T, I>, exact::Bounds<I>> {
let capacity = self.store.capacity();
if index.into_raw() as usize >= capacity {
return Err(exact::Bounds { index, capacity });
}
Ok(exact::Entry { owner: self, index })
}
pub fn front(&self) -> Option<T> {
self.store.front::<0>()
}
pub fn pop(&mut self) -> Option<(Live<&mut Self, I, ()>, T)> {
let (index, value) = self.pop_head()?;
Some((
unsafe { crate::depot::raw::Proof::new((self, I::from_raw(index.raw()))) },
value,
))
}
}
#[must_use]
pub struct Entry<'a, T: Copy + Eq, I: crate::depot::raw::Compact = u32> {
pub(in crate::depot::queue::indexed::exact) owner: &'a mut fifo::List<T, I>,
pub(in crate::depot::queue::indexed::exact) index: I,
}
impl<'a, T: Copy + Eq, I: crate::depot::raw::Compact> Entry<'a, T, I> {
pub fn index(&self) -> I {
self.index
}
pub fn from_source<C>(source: Admission<'a, T, I, C>) -> (Self, C) {
let ((owner, context), index) = source.into_inner();
(Self { owner, index }, context)
}
pub fn schedule(&mut self, value: T) {
self.replace::<true>(value);
}
fn position(&self) -> links::Index {
unsafe { links::Index::new_raw(self.index.into_raw()) }
}
fn replace<const KEEP_EQUAL: bool>(&mut self, value: T) {
let index = self.position();
let entry = unsafe { self.owner.store.at(index.get()) };
if entry.linked::<0>(index) {
if KEEP_EQUAL && unsafe { (*entry.value::<0>()).assume_init_read() } == value {
return;
}
unsafe { entry.write_back::<0, true>(index, value) };
} else {
unsafe { entry.write_back::<0, false>(index, value) };
}
}
pub fn unlink(&mut self, expected: T) {
let index = self.position();
let entry = unsafe { self.owner.store.at(index.get()) };
if entry.linked::<0>(index)
&& unsafe { (*entry.value::<0>()).assume_init_read() } == expected
{
unsafe { entry.unlink::<0>(index) };
}
}
pub fn requeue(&mut self, value: T) {
self.replace::<false>(value);
}
}
const _: () = {
assert!(mem::size_of::<Entry<'static, u64>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Option<Entry<'static, u64>>>() == 2 * mem::size_of::<usize>());
};
type Admission<'a, T, I, C> = Live<(&'a mut fifo::List<T, I>, C), I>;