forge
Explore
Log in

Don't you dare go Hollow

B12: ec-vorbis codebook: total_cmp in huffman sort, empty book yields one entry instead of assert

Browse files at this commit

15e454e548cd01bc7cb7a50331f9e76a649349c2Ahmet Kaan GümüşParent: 22f0f3f
partial_cmp().unwrap() replaced by total_cmp; assert!(n > 0) replaced by n <= 1 -> [1].
Encoder-only path (CodebookSpec::huffman); decoder output unchanged. Verify: vorbis encode golden.
cargo check and clippy ec-vorbis clean.
crates/ec-vorbis/src/codebook.rsmodified+2 -3
@@ -392,8 +392,7 @@ impl CodebookSpec {
392 /// does the cost is a fraction of a bit on the rarest symbol.392 /// does the cost is a fraction of a bit on the rarest symbol.
393 fn huffman_lengths(weights: &[f64]) -> Vec<u8> {393 fn huffman_lengths(weights: &[f64]) -> Vec<u8> {
394 let n = weights.len();394 let n = weights.len();
395- assert!(n > 0, "a codebook needs at least one entry");395+ if n <= 1 {
396- if n == 1 {
397 return vec![1];396 return vec![1];
398 }397 }
399 let floor = weights.iter().cloned().fold(0.0f64, f64::max) / 8_388_608.0;398 let floor = weights.iter().cloned().fold(0.0f64, f64::max) / 8_388_608.0;
@@ -405,7 +404,7 @@ fn huffman_lengths(weights: &[f64]) -> Vec<u8> {
405 // Leaves first, then merged nodes, tracked as a forest over `parent`.404 // Leaves first, then merged nodes, tracked as a forest over `parent`.
406 let mut parent = vec![usize::MAX; n];405 let mut parent = vec![usize::MAX; n];
407 let mut heap: Vec<(f64, usize)> = nodes.clone();406 let mut heap: Vec<(f64, usize)> = nodes.clone();
408- heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap());407+ heap.sort_by(|a, b| b.0.total_cmp(&a.0));
409 let mut merged: Vec<(f64, usize)> = Vec::new();408 let mut merged: Vec<(f64, usize)> = Vec::new();
410 let mut next_id = n;409 let mut next_id = n;
411 while heap.len() + merged.len() > 1 {410 while heap.len() + merged.len() > 1 {