reth_trie_sparse/arena/
branch_child_idx.rs1use alloy_trie::{TrieMask, TrieMaskIter};
2use core::{
3 iter::Enumerate,
4 ops::{Index, IndexMut},
5};
6use smallvec::SmallVec;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub(super) struct BranchChildIdx(u8);
15
16impl BranchChildIdx {
17 pub(super) const fn new(state_mask: TrieMask, nibble: u8) -> Option<Self> {
22 if !state_mask.is_bit_set(nibble) {
23 return None;
24 }
25 Some(Self::new_unchecked(state_mask, nibble))
26 }
27
28 pub(super) const fn insertion_point(state_mask: TrieMask, nibble: u8) -> Self {
33 Self(Self::count_below(state_mask, nibble))
34 }
35
36 pub(super) const fn get(self) -> usize {
38 self.0 as usize
39 }
40
41 const fn count_below(state_mask: TrieMask, nibble: u8) -> u8 {
43 (state_mask.get() & ((1u16 << nibble) - 1)).count_ones() as u8
44 }
45
46 const fn new_unchecked(state_mask: TrieMask, nibble: u8) -> Self {
48 Self(Self::count_below(state_mask, nibble))
49 }
50}
51
52impl<T> Index<BranchChildIdx> for SmallVec<[T; 4]> {
53 type Output = T;
54
55 fn index(&self, idx: BranchChildIdx) -> &Self::Output {
56 &self.as_slice()[idx.get()]
57 }
58}
59
60impl<T> IndexMut<BranchChildIdx> for SmallVec<[T; 4]> {
61 fn index_mut(&mut self, idx: BranchChildIdx) -> &mut Self::Output {
62 &mut self.as_mut_slice()[idx.get()]
63 }
64}
65
66pub(super) struct BranchChildIter {
71 inner: Enumerate<TrieMaskIter>,
72}
73
74impl BranchChildIter {
75 pub(super) fn new(state_mask: TrieMask) -> Self {
77 Self { inner: state_mask.iter().enumerate() }
78 }
79}
80
81impl Iterator for BranchChildIter {
82 type Item = (BranchChildIdx, u8);
83
84 fn next(&mut self) -> Option<Self::Item> {
85 self.inner.next().map(|(dense, nibble)| (BranchChildIdx(dense as u8), nibble))
86 }
87
88 fn size_hint(&self) -> (usize, Option<usize>) {
89 self.inner.size_hint()
90 }
91}