use crate::coords::*;
use std::num::NonZeroU32;
pub unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
::core::slice::from_raw_parts((p as *const T) as *const u8, ::core::mem::size_of::<T>())
}
pub type NodePtr = Option<NonZeroU32>;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ChunkPtr(i32);
impl core::fmt::Display for ChunkPtr {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("ChunkPtr({:?})", self.get()))
}
}
impl ChunkPtr {
#[inline]
pub(crate) fn get(self) -> Option<usize> {
match self.0 {
-1 => None,
_ => Some(self.0 as usize),
}
}
#[inline]
pub(crate) fn take(&mut self) -> Option<usize> {
let rv = match self.0 {
-1 => None,
_ => Some(self.0 as usize),
};
*self = Self::None;
rv
}
#[inline]
pub(crate) fn from(x: Option<usize>) -> Self {
match x {
Some(v) => Self(v as i32),
None => Self::None,
}
}
#[allow(non_upper_case_globals)]
pub const None: Self = ChunkPtr(-1);
}
#[derive(Clone, Debug)]
pub struct TreeNode<const B: usize> {
pub children: [NodePtr; B],
pub chunk: [ChunkPtr; B],
}
impl<const B: usize> TreeNode<B> {
#[inline]
pub(crate) fn new() -> Self {
Self {
children: [None; B],
chunk: [ChunkPtr::None; B],
}
}
#[inline]
pub fn iter_existing_chunks(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
self.chunk.iter().filter_map(|c| c.get()).enumerate()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.children.iter().all(|c| c.is_none()) && self.chunk.iter().all(|c| *c == ChunkPtr::None)
}
}
#[inline]
pub fn iter_treenode_children<const N: usize>(
children: &[NodePtr; N],
) -> impl Iterator<Item = (usize, usize)> + '_ {
children
.iter()
.filter_map(|c| Some((*c)?.get() as usize))
.enumerate()
}
#[derive(Clone, Debug)]
pub struct ChunkContainer<const N: usize, C: Sized, L: LodVec<N>> {
pub chunk: C,
pub(crate) position: L,
pub(crate) node_idx: u32,
pub(crate) child_idx: u8,
}
impl<const N: usize, C: Sized, L: LodVec<N>> ChunkContainer<N, C, L> {
#[inline(always)]
pub fn chunk_ptr(&mut self) -> *mut C {
&mut self.chunk as *mut C
}
#[inline(always)]
pub fn position(&self) -> L {
self.position
}
}
#[derive(Clone, Debug, Copy)]
pub struct TreePos<const N: usize, L: LodVec<N>> {
pub idx: usize,
pub pos: L,
}