B12: ec-vorbis codebook: total_cmp in huffman sort, empty book yields one entry instead of assert
Browse files at this commit15e454e548cd01bc7cb7a50331f9e76a649349c2Parent: 22f0f3f1 changed file +2 -3
crates/ec-vorbis/src/codebook.rsmodified+2 -3
| @@ -392,8 +392,7 @@ impl CodebookSpec { | ||
| 392 | 392 | /// does the cost is a fraction of a bit on the rarest symbol. |
| 393 | 393 | fn huffman_lengths(weights: &[f64]) -> Vec<u8> { |
| 394 | 394 | let n = weights.len(); |
| 395 | - assert!(n > 0, "a codebook needs at least one entry"); | |
| 396 | - if n == 1 { | |
| 395 | + if n <= 1 { | |
| 397 | 396 | return vec![1]; |
| 398 | 397 | } |
| 399 | 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 | 404 | // Leaves first, then merged nodes, tracked as a forest over `parent`. |
| 406 | 405 | let mut parent = vec![usize::MAX; n]; |
| 407 | 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 | 408 | let mut merged: Vec<(f64, usize)> = Vec::new(); |
| 410 | 409 | let mut next_id = n; |
| 411 | 410 | while heap.len() + merged.len() > 1 { |