From bf9212b79ae7b68648838edddde16a793952a244 Mon Sep 17 00:00:00 2001 From: mehbark Date: Sat, 4 Oct 2025 15:59:10 -0400 Subject: [PATCH] huffman: fix for inputs with one repeated byte --- src/huffman.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/huffman.rs b/src/huffman.rs index 061fc42..7d80e75 100644 --- a/src/huffman.rs +++ b/src/huffman.rs @@ -53,7 +53,12 @@ pub enum Node { impl Node { fn build(counts: &HashMap) -> Option { - 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 {