use std::{marker, num::NonZero};
use crate::depot::{capacity::Physical, index::bitmap};
pub struct Row<S, P = (), M = (), W = usize> {
pub(in crate::depot::index::bitmap) phase: Phase<M, W>,
pub(in crate::depot::index::bitmap) plane: P,
pub(in crate::depot) source: S,
pub(in crate::depot::index::bitmap) index: NonZero<u32>,
}
impl<S: Copy, W> Clone for Row<S, (), (), W> {
fn clone(&self) -> Self {
*self
}
}
impl<S: Copy, W> Copy for Row<S, (), (), W> {}
impl<W: bitmap::Width, S: crate::depot::raw::Words<W>> Row<S, (), (), W> {
pub fn from_source(source: crate::depot::raw::Proof<(S, NonZero<u32>), Physical>) -> Self {
let (source, index) = source.into_inner();
Self {
source,
index,
plane: (),
phase: marker::PhantomData,
}
}
pub fn try_new(source: S, index: usize) -> Result<Self, bitmap::Error> {
let capacity = source.shape().bits();
if index >= capacity {
return Err(bitmap::Error { index, capacity });
}
Ok(unsafe { Self::new(source, index) })
}
pub(in crate::depot) unsafe fn new(source: S, index: usize) -> Self {
let index = unsafe { NonZero::new_unchecked(index as u32 + 1) };
Self::from_source(unsafe { crate::depot::raw::Proof::new((source, index)) })
}
}
impl<W: bitmap::Width, S: crate::depot::raw::Words<W>, P, M> Row<S, P, M, W> {
pub unsafe fn source_mut(&mut self) -> &mut S {
&mut self.source
}
pub(in crate::depot) const fn plane(&self) -> P
where
P: Copy,
{
self.plane
}
pub(in crate::depot::index::bitmap) fn borrow(&self) -> Row<&S, (), (), W> {
Row {
source: &self.source,
index: self.index,
plane: (),
phase: marker::PhantomData,
}
}
pub const fn index(&self) -> usize {
self.index.get() as usize - 1
}
pub fn contains(&self, plane: S::Plane) -> bool {
let index = self.index();
let words = S::row(self.borrow(), plane);
let word = unsafe { words.get_unchecked(index / W::BITS as usize) };
word.get() & (1usize << (index % W::BITS as usize)) != 0
}
}
impl<W: bitmap::Width, S: crate::depot::raw::Words<W>, P> Row<S, P, (), W> {
pub const fn source(&self) -> &S {
&self.source
}
}
impl<'a, W: bitmap::Width, S: crate::depot::raw::Bitmap<W>> Row<&'a mut S, (), (), W> {
pub fn occupied(
self,
plane: S::Plane,
) -> Result<Mutable<'a, S, Occupied, W>, Mutable<'a, S, Absent, W>> {
let entry: Row<_, _, (), W> = Row {
source: self.source,
index: self.index,
plane,
phase: marker::PhantomData,
};
if entry.contains(plane) {
Ok(entry.with_phase())
} else {
Err(entry.with_phase())
}
}
}
impl<W: bitmap::Width, S: crate::depot::raw::Words<W, Size = NonZero<u32>>> Row<S, (), (), W> {
pub fn clamp(source: S, index: usize) -> Self {
let capacity = source.shape().bits;
let index = index.min(capacity.get() as usize - 1);
unsafe { Self::new(source, index) }
}
}
impl<S, P, M, W> Row<S, P, M, W> {
pub const fn position(&self) -> NonZero<u32> {
self.index
}
pub(in crate::depot) fn with_phase<N>(self) -> Row<S, P, N, W> {
Row {
source: self.source,
index: self.index,
plane: self.plane,
phase: marker::PhantomData,
}
}
}
type Mutable<'a, S, P, W> = Row<&'a mut S, <S as crate::depot::raw::Words<W>>::Plane, P, W>;
type Phase<M, W> = marker::PhantomData<fn(M, W) -> (M, W)>;
const _: () = {
assert!(size_of::<Row<&()>>() == 2 * size_of::<usize>());
assert!(size_of::<Option<Row<&()>>>() == size_of::<Row<&()>>());
};
pub struct Occupied;
impl<'a, W: bitmap::Width, S: crate::depot::raw::Bitmap<W>> Row<&'a mut S, S::Plane, Occupied, W> {
pub fn remove(self) -> Row<&'a mut S, S::Plane, Removed, W> {
let row: Row<_, _, (), W> = self.with_phase();
row.update::<false>(row.plane());
row.with_phase()
}
}
pub struct Absent;
pub struct Removed;
pub(in crate::depot::index::bitmap) struct Level<'a, W: bitmap::Width> {
words: &'a [bitmap::Word<W>],
bits: usize,
}
impl<'a, W: bitmap::Width> Level<'a, W> {
pub(in crate::depot::index::bitmap) fn new<S: crate::depot::raw::Words<W>>(
source: &'a S,
plane: S::Plane,
) -> Self {
Self {
words: source.words(plane),
bits: source.shape().bits(),
}
}
pub(in crate::depot::index::bitmap) fn find(
&self,
mut start: usize,
) -> Option<(NonZero<u32>, W)> {
let mut count = NonZero::new(self.bits)?;
let mut offset = 0;
let mut shift = 0;
let radix = NonZero::<usize>::MIN.saturating_add(W::BITS as usize - 1);
loop {
if start >= count.get() {
return None;
}
let mut index = start / radix;
let word = unsafe { self.words.get_unchecked(offset + index) };
let mut current = word.value.get();
let bits = (current & (!W::ZERO << (start % radix))).offset();
if let Some(mut bits) = NonZero::new(bits) {
loop {
if shift == 0 {
let position = index * radix.get() + bits.trailing_zeros() as usize;
return Some((
unsafe { NonZero::new_unchecked(position as u32 + 1) },
current,
));
}
index = index * radix.get() + bits.trailing_zeros() as usize;
offset -= ((self.bits - 1) >> shift) + 1;
shift -= W::BITS.trailing_zeros();
let word = unsafe { self.words.get_unchecked(offset + index) };
current = word.value.get();
bits = unsafe { NonZero::new_unchecked(current.offset()) };
}
}
let words = count.div_ceil(radix);
if words.get() == 1 {
return None;
}
offset += words.get();
count = words;
start = index + 1;
shift += W::BITS.trailing_zeros();
}
}
unsafe fn update<const SET: bool>(&self, index: usize) {
let word = unsafe { self.words.get_unchecked(index / W::BITS as usize) };
unsafe { self.write::<SET>(index, word.value.get()) };
}
pub(in crate::depot::index::bitmap) unsafe fn write<const SET: bool>(
&self,
mut index: usize,
mut current: W,
) {
let mut words = self.words;
let mut count = unsafe { NonZero::new_unchecked(self.bits) };
let radix = NonZero::<usize>::MIN.saturating_add(W::BITS as usize - 1);
count = count.div_ceil(radix);
loop {
let offset = index / W::BITS as usize;
let word = unsafe { words.get_unchecked(offset) };
let mask = unsafe { W::from_offset(1) } << (index % W::BITS as usize);
let next = if SET { current | mask } else { current & !mask };
word.value.set(next);
let stable = if SET {
current != W::ZERO
} else {
next != W::ZERO
};
if stable || count.get() == 1 {
return;
}
words = unsafe { words.get_unchecked(count.get()..) };
count = count.div_ceil(radix);
index = offset;
current = unsafe { words.get_unchecked(index / W::BITS as usize) }
.value
.get();
}
}
}
impl<W: bitmap::Width, S: crate::depot::raw::Words<W>, P> Row<S, P, (), W> {
pub fn update<const SET: bool>(&self, plane: S::Plane) {
let level = Level {
words: S::row(self.borrow(), plane),
bits: self.source.shape().bits(),
};
unsafe { level.update::<SET>(self.index()) };
}
}