huffman: fix for inputs with one repeated byte

This commit is contained in:
mehbark 2025-10-04 15:59:10 -04:00
parent 7f0c1ada52
commit bf9212b79a
Signed by: mbk
GPG key ID: E333EC1335FFCCDB

View file

@ -53,7 +53,12 @@ pub enum Node {
impl Node {
fn build(counts: &HashMap<u8, u32>) -> Option<Self> {
WeightedNode::build(counts).map(WeightedNode::unburden)
let branch = match WeightedNode::build(counts).map(WeightedNode::unburden)? {
leaf @ Node::Leaf { .. } => Self::join(leaf.clone(), leaf),
branch @ Node::Branch { .. } => branch,
};
Some(branch)
}
/// Write to `buf` the sequence of bits that this tree has assigned `byte`.
@ -120,6 +125,13 @@ impl Node {
}
}
}
fn join(path0: Self, path1: Self) -> Self {
Self::Branch {
path0: Box::new(path0),
path1: Box::new(path1),
}
}
}
impl fmt::Debug for Node {