This commit is contained in:
Edgar 2022-10-26 15:24:36 +02:00
parent 7bbc325eb7
commit f05132f0ee
No known key found for this signature in database

View file

@ -26,9 +26,7 @@
#![deny(missing_docs)] #![deny(missing_docs)]
#![deny(rustdoc::missing_doc_code_examples)] #![deny(rustdoc::missing_doc_code_examples)]
use std::{ use std::collections::BinaryHeap;
collections::{BinaryHeap},
};
use bit_vec::BitVec; use bit_vec::BitVec;
@ -117,7 +115,7 @@ impl Huffman {
} }
/// Builds a binary tree, the root is the last node, the leafs are at the start. /// Builds a binary tree, the root is the last node, the leafs are at the start.
/// ///
/// Returns the root index. /// Returns the root index.
fn build_tree(tree: &mut [Node; TREE_SIZE], table: &[usize; MAX_SYMBOLS]) -> usize { fn build_tree(tree: &mut [Node; TREE_SIZE], table: &[usize; MAX_SYMBOLS]) -> usize {
let mut priority_queue: BinaryHeap<Node> = table let mut priority_queue: BinaryHeap<Node> = table
@ -125,7 +123,7 @@ impl Huffman {
.enumerate() .enumerate()
.map(|(index, v)| Node::new(index, *v)) .map(|(index, v)| Node::new(index, *v))
.collect(); .collect();
let mut tree_index = 256; let mut tree_index = 256;
while priority_queue.len() > 1 { while priority_queue.len() > 1 {
@ -179,11 +177,7 @@ impl Huffman {
let mut bits = BitVec::new(); let mut bits = BitVec::new();
for b in data.iter() { for b in data.iter() {
self.traverse( self.traverse(&mut bits, *b as usize, None)
&mut bits,
*b as usize,
None,
)
} }
bits.to_bytes() bits.to_bytes()