#![allow(clippy::cast_possible_truncation)]
use super::CARTNode;
impl CARTNode {
pub(super) fn grow_to_node16(&self) -> Self {
match self {
Self::Node4 {
num_children,
keys,
children,
} => {
let mut new_keys = [0u8; 16];
let mut new_children: [Option<Box<CARTNode>>; 16] = Default::default();
let n = *num_children as usize;
let mut indices: Vec<usize> = (0..n).collect();
indices.sort_by_key(|&i| keys[i]);
for (new_idx, &old_idx) in indices.iter().enumerate() {
new_keys[new_idx] = keys[old_idx];
new_children[new_idx].clone_from(&children[old_idx]);
}
Self::Node16 {
num_children: *num_children,
keys: new_keys,
children: new_children,
}
}
_ => self.clone(),
}
}
pub(super) fn grow_to_node48(&self) -> Self {
match self {
Self::Node16 {
num_children,
keys,
children,
} => {
let mut new_keys = [255u8; 256];
let mut new_children: [Option<Box<CARTNode>>; 48] = std::array::from_fn(|_| None);
for i in 0..*num_children as usize {
new_keys[keys[i] as usize] = i as u8;
new_children[i].clone_from(&children[i]);
}
Self::Node48 {
num_children: *num_children,
keys: new_keys,
children: new_children,
}
}
_ => self.clone(),
}
}
pub(super) fn grow_to_node256(&self) -> Self {
match self {
Self::Node48 {
num_children,
keys,
children,
} => {
let mut new_children: [Option<Box<CARTNode>>; 256] = std::array::from_fn(|_| None);
for (byte, &slot) in keys.iter().enumerate() {
if slot != 255 {
new_children[byte].clone_from(&children[slot as usize]);
}
}
Self::Node256 {
num_children: u16::from(*num_children),
children: new_children,
}
}
_ => self.clone(),
}
}
}