use std::{marker, mem, ptr};
use crate::{
alloc,
depot::{capacity, heap, index::Position},
};
type Planes<E, I, R> = marker::PhantomData<fn() -> (E, I, R)>;
#[repr(transparent)]
pub struct Capacity<E, I, R, C = capacity::Count, const INDEXED: bool = false> {
count: C,
shape: Planes<E, I, R>,
}
impl<E, I, R, C: Copy, const INDEXED: bool> Clone for Capacity<E, I, R, C, INDEXED> {
fn clone(&self) -> Self {
*self
}
}
impl<E, I, R, C: Copy, const INDEXED: bool> Copy for Capacity<E, I, R, C, INDEXED> {}
impl<E, I, R, C: alloc::raw::Extent, const INDEXED: bool> Capacity<E, I, R, C, INDEXED> {
pub const fn count(self) -> C {
self.count
}
fn overflow(capacity: C, plane: heap::Plane, error: alloc::Overflow) -> heap::Error<C> {
use std::alloc::Layout;
heap::Error::Layout {
capacity,
plane,
entries: Layout::new::<E>(),
positions: Layout::new::<I>(),
residents: Layout::new::<R>(),
error,
}
}
}
impl<E, I: Position, R, C: alloc::raw::Extent, const INDEXED: bool> Capacity<E, I, R, C, INDEXED> {
pub fn try_new(count: C) -> Result<Self, heap::Error<C>> {
use alloc::raw::Rows;
let maximum = <I as crate::depot::raw::Word>::MAX;
if count.len() > maximum.get() {
return Err(heap::Error::Capacity {
requested: count,
maximum,
});
}
if count.len() > Self::ROOT_MAX {
return Err(Self::overflow(count, heap::Plane::Entries, alloc::Overflow));
}
if count.len() > Self::MAX {
return Err(Self::overflow(
count,
heap::Plane::Positions,
alloc::Overflow,
));
}
if count.len() > Self::EXTRA_MAX {
return Err(Self::overflow(
count,
heap::Plane::Residents,
alloc::Overflow,
));
}
Ok(Self {
count,
shape: marker::PhantomData,
})
}
}
unsafe impl<E, I: Copy, R, C: alloc::raw::Extent, const INDEXED: bool> alloc::raw::Shape
for Capacity<E, I, R, C, INDEXED>
{
type Root = E;
type Tail = I;
type Extra = R;
type Last = ();
fn len(&self) -> usize {
self.count.len()
}
}
unsafe impl<E, I: Copy, R, C: alloc::raw::Extent, const INDEXED: bool> alloc::raw::Rows
for Capacity<E, I, R, C, INDEXED>
{
}
impl<E, I: Position, R, C: alloc::raw::Extent, const INDEXED: bool>
heap::raw::Capacity<E, I, R, C, INDEXED>
{
pub(in crate::depot::heap) fn uninit(self) -> Result<alloc::raw::Uninit<Self>, heap::Error<C>> {
let mut memory =
alloc::raw::Uninit::try_new(self).map_err(move |error| heap::Error::Allocation {
capacity: self.count(),
layout: alloc::raw::Shape::layout(&self),
error,
})?;
memory.planes_mut().1.fill(mem::MaybeUninit::new(I::NONE));
Ok(memory)
}
pub fn allocate(self) -> Result<alloc::raw::Block<Self, u32>, heap::Error<C>> {
let memory = self.uninit()?;
Ok(unsafe { memory.finish(0, ()) })
}
pub fn repeat(
self,
resident: R,
) -> Result<alloc::raw::Block<Self, u32, ptr::NonNull<R>>, heap::Error<C>>
where
R: Copy,
{
let mut memory = self.uninit()?;
let values = memory.planes_mut().2;
values.fill(mem::MaybeUninit::new(resident));
let values = ptr::NonNull::from(values).cast();
Ok(unsafe { memory.finish(0, values) })
}
}
impl<K: crate::depot::raw::Address, P, R, C: alloc::raw::Extent, X, const INDEXED: bool>
alloc::raw::Block<heap::raw::Capacity<(K, P), u32, R, C, INDEXED>, u32, X>
{
pub unsafe fn heap_first(&self) -> Option<&(K, P)> {
if self.state == 0 {
None
} else {
Some(unsafe { self.heap_value(0) })
}
}
pub unsafe fn position(&self, key: K) -> u32 {
unsafe { *self.tail().get_unchecked(key.index()) }
}
pub unsafe fn heap_value(&self, position: usize) -> &(K, P) {
unsafe { self.as_ref().get_unchecked(position).assume_init_ref() }
}
}
impl<K: crate::depot::raw::Address, P, R, C: alloc::raw::Extent, const INDEXED: bool>
alloc::raw::Block<Capacity<(K, P), u32, R, C, INDEXED>, u32, ptr::NonNull<R>>
{
pub unsafe fn resident(&self, key: K) -> &R {
unsafe { self.values().get_unchecked(key.index()) }
}
pub unsafe fn resident_mut(&mut self, key: K) -> &mut R {
unsafe { self.values_mut().get_unchecked_mut(key.index()) }
}
}