pub fn compile_write_tree<E, T>(
values: Vec<(T, Vec<u8>)>,
) -> Result<WriteHuffmanTree<E, T>, HuffmanTreeError>
Expand description
Given a vector of symbol/code pairs, compiles a Huffman tree for writing.
Code must be 0 or 1 bits and are always written to the stream from least-significant in the list to most signficant (which makes them easier to read for humans).
If the same symbol occurs multiple times, the first code is used. Unlike in read trees, not all possible codes need to be assigned a symbol.
ยงExamples
use bitstream_io::huffman::compile_write_tree;
use bitstream_io::BigEndian;
assert!(compile_write_tree::<BigEndian,i32>(
vec![(1, vec![0]),
(2, vec![1, 0]),
(3, vec![1, 1])]).is_ok());
use std::io::Write;
use bitstream_io::{BigEndian, BitWriter, HuffmanWrite};
use bitstream_io::huffman::compile_write_tree;
let tree = compile_write_tree(
vec![('a', vec![0]),
('b', vec![1, 0]),
('c', vec![1, 1, 0]),
('d', vec![1, 1, 1])]).unwrap();
let mut data = Vec::new();
{
let mut writer = BitWriter::endian(&mut data, BigEndian);
writer.write_huffman(&tree, 'b').unwrap();
writer.write_huffman(&tree, 'c').unwrap();
writer.write_huffman(&tree, 'd').unwrap();
}
assert_eq!(data, [0b10110111]);