pub use crate::prelude::*;
pub struct FatPtrArray<'a, E, L = ()>
where
Self: 'a,
{
data: &'a mut FPArrayBlock<E, L>,
}
impl<'a, E> FatPtrArray<'a, E> {
#[inline]
pub fn new<F>(len: usize, mut func: F) -> Self
where
F: FnMut(usize) -> E,
{
Self::new_labelled((), len, |_, idx| func(idx))
}
}
impl<'a, E, L> FatPtrArray<'a, E, L> {
#[inline]
pub fn new_labelled<F>(label: L, len: usize, func: F) -> Self
where
F: FnMut(&mut L, usize) -> E,
{
Self {
data: FPArrayBlock::<E, L>::new_ptr(label, len, func),
}
}
#[inline]
pub unsafe fn new_labelled_unsafe(label: L, len: usize) -> Self {
let new_ptr = FPArrayBlock::<E, L>::new_ptr_unsafe(label, len);
Self { data: new_ptr }
}
#[inline]
pub unsafe fn from_raw_parts(ptr: &'a mut FPArrayBlock<E, L>) -> Self {
Self { data: ptr }
}
#[inline]
pub unsafe fn unchecked_access(&'a self, idx: usize) -> &'a mut E {
self.data.unchecked_access(idx)
}
pub unsafe fn to_null(&mut self) -> &mut FPArrayBlock<E, L> {
core::mem::replace(&mut *self, Self::null_ref()).data
}
pub unsafe fn null_ref() -> Self {
Self {
data: &mut *(FPArrayBlock::null_ptr()),
}
}
pub fn is_null(&self) -> bool {
self.data.is_null()
}
}
impl<'a, E> FatPtrArray<'a, E>
where
E: Default,
{
#[inline]
pub fn new_default(len: usize) -> Self {
Self::new_default_labelled((), len)
}
}
impl<'a, E, L> FatPtrArray<'a, E, L>
where
E: Default,
{
#[inline]
pub fn new_default_labelled(label: L, len: usize) -> Self {
Self {
data: FPArrayBlock::new_ptr_default(label, len),
}
}
}
impl<'a, E, L> Index<usize> for FatPtrArray<'a, E, L> {
type Output = E;
#[inline]
fn index(&self, idx: usize) -> &E {
&self.data[idx]
}
}
impl<'a, E, L> IndexMut<usize> for FatPtrArray<'a, E, L> {
#[inline]
fn index_mut(&mut self, idx: usize) -> &mut E {
&mut self.data[idx]
}
}
impl<'a, E, L> Clone for FatPtrArray<'a, E, L>
where
L: Clone,
E: Clone,
{
#[inline]
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
}
}
}
impl<'a, E, L> Container<(usize, E)> for FatPtrArray<'a, E, L> {
#[inline]
fn add(&mut self, elem: (usize, E)) {
self[elem.0] = elem.1;
}
#[inline]
fn len(&self) -> usize {
self.data.len()
}
}
impl<'a, E, L> CopyMap<'a, usize, E> for FatPtrArray<'a, E, L>
where
E: 'a,
{
#[inline]
fn get(&'a self, key: usize) -> Option<&'a E> {
if key > self.len() {
None
} else {
Some(&self[key])
}
}
#[inline]
fn get_mut(&'a mut self, key: usize) -> Option<&'a mut E> {
if key > self.len() {
None
} else {
Some(&mut self[key])
}
}
#[inline]
fn insert(&mut self, key: usize, value: E) -> Option<E> {
if key > self.len() {
None
} else {
Some(std::mem::replace(&mut self[key], value))
}
}
}
impl<'a, E, L> Array<'a, E> for FatPtrArray<'a, E, L> where E: 'a {}
impl<'a, E, L> LabelledArray<'a, E, L> for FatPtrArray<'a, E, L>
where
E: 'a,
{
#[inline]
fn get_label(&self) -> &L {
&self.data.label
}
#[inline]
fn get_label_mut(&mut self) -> &mut L {
&mut self.data.label
}
}