use crate::node::ArtNode;
use crate::node48::Node48;
use crate::ArtKey;
use crate::Header;
const FULL_NODE_SIZE: u16 = 256;
pub(crate) struct Node256<K: ArtKey, V, const MAX_PARTIAL_LEN: usize> {
pub(crate) header: Header<MAX_PARTIAL_LEN>,
pub(crate) children: [ArtNode<K, V, MAX_PARTIAL_LEN>; 256],
pub(crate) prefixed_child: ArtNode<K, V, MAX_PARTIAL_LEN>,
}
impl<K: ArtKey, V, const MAX_PARTIAL_LEN: usize> Default for Node256<K, V, MAX_PARTIAL_LEN> {
fn default() -> Node256<K, V, MAX_PARTIAL_LEN> {
Node256 {
header: Default::default(),
prefixed_child: ArtNode::default(),
children: [
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
ArtNode::none(),
],
}
}
}
impl<K: ArtKey, V, const MAX_PARTIAL_LEN: usize> Node256<K, V, MAX_PARTIAL_LEN> {
#[inline(always)]
pub(crate) fn is_full(&self) -> bool {
self.header.non_null_children == FULL_NODE_SIZE
}
#[inline(always)]
pub(crate) fn minimum_child(&self) -> Option<&ArtNode<K, V, MAX_PARTIAL_LEN>> {
if !self.prefixed_child.is_none() {
return Some(&self.prefixed_child);
}
for node in self.children.iter() {
if !node.is_none() {
return Some(node);
}
}
return None;
}
#[inline]
pub(crate) fn get_child(&self, key: (u8, bool)) -> Option<&ArtNode<K, V, MAX_PARTIAL_LEN>> {
if !key.1 {
return Some(&self.prefixed_child);
}
return Some(&self.children[key.0 as usize]);
}
pub(crate) fn get_mut_child(
&mut self,
key: (u8, bool),
) -> Option<&mut ArtNode<K, V, MAX_PARTIAL_LEN>> {
if !key.1 {
return Some(&mut self.prefixed_child);
}
return Some(&mut self.children[key.0 as usize]);
}
pub(crate) fn insert_child(
&mut self,
key: (u8, bool),
mut new_child: ArtNode<K, V, MAX_PARTIAL_LEN>,
) {
assert_eq!(self.header.non_null_children, 48);
let mut assert_cnt = 0;
for child in self.children.iter() {
if !child.is_none() {
assert_cnt += 1;
}
}
assert_eq!(assert_cnt, self.header.non_null_children);
if !key.1 {
assert!(self.prefixed_child.is_none());
std::mem::swap(&mut self.prefixed_child, &mut new_child);
return;
}
self.header.non_null_children += 1;
std::mem::swap(&mut self.children[key.0 as usize], &mut new_child);
assert_eq!(new_child.is_none(), true);
assert_eq!(self.children[key.0 as usize].is_none(), false);
}
pub fn is_few(&self) -> bool {
self.header.non_null_children < 48
}
pub(crate) fn remove_child(
&mut self,
valid_key: (u8, bool),
) -> Option<ArtNode<K, V, MAX_PARTIAL_LEN>> {
if !valid_key.1 {
let child = std::mem::take(&mut self.prefixed_child);
return Some(child);
}
self.header.non_null_children -= 1;
Some(std::mem::take(&mut self.children[valid_key.0 as usize]))
}
pub(crate) fn shrink_to_fit(&mut self) -> Box<Node48<K, V, MAX_PARTIAL_LEN>> {
let mut node48: Box<Node48<K, V, MAX_PARTIAL_LEN>> = Box::default();
let mut node48_index = 0;
for (idx, child) in self.children.iter_mut().enumerate() {
if !child.is_none() {
std::mem::swap(child, &mut node48.children[node48_index]);
node48.child_index[idx] = node48_index as u8;
node48_index += 1;
}
}
std::mem::swap(&mut self.prefixed_child, &mut node48.prefixed_child);
node48.header.partial.clone_from(&self.header.partial);
node48.header.non_null_children = node48_index as u16;
node48
}
}