use ts_bitset::Bitset256;
use crate::{BaseIndex, node::Child};
mod prefix;
pub use prefix::{NodePrefixIter, PrefixOps, PrefixOpsExt, PrefixReadOps};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Stats {
pub prefix_count: usize,
pub child_count: usize,
pub node_count: usize,
pub leaf_count: usize,
pub fringe_count: usize,
}
pub trait StrideBase {
type T: 'static;
}
impl<T> StrideBase for &T
where
T: StrideBase + ?Sized,
{
type T = T::T;
}
impl<T> StrideBase for &mut T
where
T: StrideBase + ?Sized,
{
type T = T::T;
}
pub trait StrideOps: Default + PrefixOps {
fn direct_prefixes(&self) -> impl Iterator<Item = (BaseIndex, &Self::T)>;
fn child_bitset(&self) -> &Bitset256;
fn get_child(&self, addr: u8) -> Option<Child<&Self, &Self::T>>;
fn get_child_mut(&mut self, addr: u8) -> Option<Child<&mut Self, &mut Self::T>>;
fn insert_child(
&mut self,
addr: u8,
child: Child<Self, Self::T>,
) -> Option<Child<Self, Self::T>>;
fn remove_child(&mut self, addr: u8) -> Option<Child<Self, Self::T>>;
fn direct_children(&self) -> impl Iterator<Item = (u8, Child<&Self, &Self::T>)>;
#[inline]
fn child_count(&self) -> usize {
self.child_bitset().count_ones()
}
fn stats(&self) -> Stats;
}
mod private {
pub trait Sealed {}
}
pub trait StrideOpsExt: StrideOps + private::Sealed {
#[inline]
fn is_empty(&self) -> bool {
self.prefix_count() == 0 && self.child_count() == 0
}
#[inline]
fn with_child(mut self, addr: u8, child: impl Into<Child<Self, Self::T>>) -> Self {
self.insert_child(addr, child.into());
self
}
#[inline]
fn into_child(self) -> Child<Self, Self::T> {
Child::Path(self)
}
}
impl<T> private::Sealed for T where T: StrideOps {}
impl<T> StrideOpsExt for T where T: StrideOps {}