Crates.io | huffman-compress2 |
lib.rs | huffman-compress2 |
version | |
source | src |
created_at | 2024-10-11 17:21:01.883687 |
updated_at | 2024-10-11 17:21:01.883687 |
description | Huffman compression given a probability distribution over arbitrary symbols |
homepage | |
repository | https://github.com/thomas-daniels/rust-huffman-compress |
max_upload_size | |
id | 1405569 |
Cargo.toml error: | TOML parse error at line 21, column 1 | 21 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This is a fork of Niklas Fiekas's huffman-compress, which is no longer maintained.
Huffman compression given a probability distribution over arbitrary symbols.
use std::iter::FromIterator;
use std::collections::HashMap;
use bit_vec::BitVec;
use huffman_compress::{CodeBuilder, Book, Tree};
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) = CodeBuilder::from_iter(weights).finish();
// More frequent symbols will be encoded with fewer bits.
assert!(book.get("CG").map_or(0, |cg| cg.len()) <
book.get("AG").map_or(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);
Enable the serde
feature for (de)serialization of the Book
and Tree
structs.
bit-vec
0.8.#[deny(warnings)]
(a future
compatibility hazard in libraries).bit-vec
0.6.bit-vec
0.5.Tree::decoder()
to Tree::unbounded_decoder()
to avoid
surprises. A new Tree::decoder()
takes the maximum number of symbols to
decode.Saturating
from num-traits.CodeBuilder
.HashMap
. Thanks
@mernen.K: Ord
instead of K: Hash + Eq
for symbols and switch Book
internals from HashMap
to BTreeMap
.Book
.huffman-compress is dual licensed under the Apache 2.0 and MIT license, at your option.