ts_bart/node/stride_ops/
mod.rs1use ts_bitset::Bitset256;
2
3use crate::{BaseIndex, node::Child};
4
5mod prefix;
6
7pub use prefix::{NodePrefixIter, PrefixOps, PrefixOpsExt, PrefixReadOps};
8
9#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
11pub struct Stats {
12 pub prefix_count: usize,
14 pub child_count: usize,
16 pub node_count: usize,
18 pub leaf_count: usize,
20 pub fringe_count: usize,
22}
23
24pub trait StrideBase {
26 type T: 'static;
28}
29
30impl<T> StrideBase for &T
31where
32 T: StrideBase + ?Sized,
33{
34 type T = T::T;
35}
36
37impl<T> StrideBase for &mut T
38where
39 T: StrideBase + ?Sized,
40{
41 type T = T::T;
42}
43
44pub trait StrideOps: Default + PrefixOps {
46 fn direct_prefixes(&self) -> impl Iterator<Item = (BaseIndex, &Self::T)>;
51
52 fn child_bitset(&self) -> &Bitset256;
54
55 fn get_child(&self, addr: u8) -> Option<Child<&Self, &Self::T>>;
57 fn get_child_mut(&mut self, addr: u8) -> Option<Child<&mut Self, &mut Self::T>>;
59 fn insert_child(
61 &mut self,
62 addr: u8,
63 child: Child<Self, Self::T>,
64 ) -> Option<Child<Self, Self::T>>;
65 fn remove_child(&mut self, addr: u8) -> Option<Child<Self, Self::T>>;
67 fn direct_children(&self) -> impl Iterator<Item = (u8, Child<&Self, &Self::T>)>;
69
70 #[inline]
72 fn child_count(&self) -> usize {
73 self.child_bitset().count_ones()
74 }
75
76 fn stats(&self) -> Stats;
78}
79
80mod private {
81 pub trait Sealed {}
82}
83
84pub trait StrideOpsExt: StrideOps + private::Sealed {
86 #[inline]
88 fn is_empty(&self) -> bool {
89 self.prefix_count() == 0 && self.child_count() == 0
90 }
91
92 #[inline]
108 fn with_child(mut self, addr: u8, child: impl Into<Child<Self, Self::T>>) -> Self {
109 self.insert_child(addr, child.into());
110 self
111 }
112
113 #[inline]
115 fn into_child(self) -> Child<Self, Self::T> {
116 Child::Path(self)
117 }
118}
119
120impl<T> private::Sealed for T where T: StrideOps {}
121impl<T> StrideOpsExt for T where T: StrideOps {}