[][src]Function bitstream_io::huffman::compile_read_tree

pub fn compile_read_tree<E, T>(
    values: Vec<(T, Vec<u8>)>
) -> Result<Box<[ReadHuffmanTree<E, T>]>, HuffmanTreeError> where
    E: Endianness,
    T: Clone

Given a vector of symbol/code pairs, compiles a Huffman tree for reading.

Code must be 0 or 1 bits and are always read from the stream from least-significant in the list to most signficant (which makes them easier to read for humans).

All possible codes must be assigned some symbol, and it is acceptable for the same symbol to occur multiple times.

Examples

use bitstream_io::huffman::compile_read_tree;
use bitstream_io::BigEndian;
assert!(compile_read_tree::<BigEndian,i32>(
    vec![(1, vec![0]),
         (2, vec![1, 0]),
         (3, vec![1, 1])]).is_ok());
use std::io::{Read, Cursor};
use bitstream_io::{BigEndian, BitReader};
use bitstream_io::huffman::compile_read_tree;
let tree = compile_read_tree(
    vec![('a', vec![0]),
         ('b', vec![1, 0]),
         ('c', vec![1, 1, 0]),
         ('d', vec![1, 1, 1])]).unwrap();
let data = [0b10110111];
let mut cursor = Cursor::new(&data);
let mut reader = BitReader::endian(&mut cursor, BigEndian);
assert_eq!(reader.read_huffman(&tree).unwrap(), 'b');
assert_eq!(reader.read_huffman(&tree).unwrap(), 'c');
assert_eq!(reader.read_huffman(&tree).unwrap(), 'd');