This commit is contained in:
Edgar 2022-10-26 12:01:41 +02:00
parent f8e093a19b
commit 6c61ceb024
No known key found for this signature in database
2 changed files with 13 additions and 16 deletions

View file

@ -1,7 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rustyman::Huffman;
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("compress", |b| {
let huffman = Huffman::new_from_data(include_bytes!("../assets/bench_input.txt"));

View file

@ -223,10 +223,9 @@ impl Huffman {
for b in data.iter() {
self.traverse(
&mut bits,
*self
.indexes
.get(b)
.unwrap_or_else(|| panic!("frequency table did not contain this byte: {:?}", b)),
*self.indexes.get(b).unwrap_or_else(|| {
panic!("frequency table did not contain this byte: {:?}", b)
}),
None,
)
}
@ -250,7 +249,7 @@ impl Huffman {
for _ in 0..byte_count {
let mut index = root_index;
while self.tree[index].left.is_some() ||self.tree[index].right.is_some() {
while self.tree[index].left.is_some() || self.tree[index].right.is_some() {
let bit = bits_iter.next().expect("missing data");
if bit {
index = self.tree[index].left.expect("should have left index");
@ -302,14 +301,13 @@ mod tests {
assert_eq!(table.len(), 1);
assert_eq!(*table.get(&0).unwrap(), 1);
let table = Huffman::calculate_freq_table(&[0,1,2,2,3,3,3]);
let table = Huffman::calculate_freq_table(&[0, 1, 2, 2, 3, 3, 3]);
assert_eq!(table.len(), 4);
assert_eq!(*table.get(&0).unwrap(), 1);
assert_eq!(*table.get(&1).unwrap(), 1);
assert_eq!(*table.get(&2).unwrap(), 2);
assert_eq!(*table.get(&3).unwrap(), 3);
}
#[test]