huffman: fix for inputs with one repeated byte
This commit is contained in:
parent
7f0c1ada52
commit
bf9212b79a
1 changed files with 13 additions and 1 deletions
|
|
@ -53,7 +53,12 @@ pub enum Node {
|
||||||
|
|
||||||
impl Node {
|
impl Node {
|
||||||
fn build(counts: &HashMap<u8, u32>) -> Option<Self> {
|
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`.
|
/// 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 {
|
impl fmt::Debug for Node {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue