Huffman compression given a probability distribution over arbitrary symbols.
```rust extern crate bitvec; extern crate huffmancompress;
use std::collections::HashMap; use bitvec::BitVec; use huffmancompress::{ Book, Tree, codebook };
let mut weights = HashMap::new(); weights.insert("CG", 293); weights.insert("AG", 34); weights.insert("AT", 4); weights.insert("CT", 4); weights.insert("TG", 1);
// Construct a Huffman code based on the weights (e.g. counts or relative // frequencies). let (book, tree) = codebook(&weights);
// More frequent symbols will be encoded with fewer bits. assert!(book.get("CG").mapor(0, |cg| cg.len()) < book.get("TG").mapor(0, |ag| ag.len()));
// Encode some symbols using the book. let mut buffer = BitVec::new(); let example = vec!["AT", "CG", "AT", "TG", "AG", "CT", "CT", "AG", "CG"]; for symbol in &example { book.encode(&mut buffer, symbol); }
// Decode the symbols using the tree. let decoded: Vec<&str> = tree.decoder(&buffer).collect(); assert_eq!(decoded, example); ```
huffman-compress is dual licensed under the Apache 2.0 and MIT license, at your option.