Giter VIP home page Giter VIP logo

rust-huffman-compress's Introduction

huffman-compress

Huffman compression given a probability distribution over arbitrary symbols.

Build Status crates.io docs.rs No Maintenance Intended

Alternatives

This project has limited real-world utility. It may be useful to experiment with or learn about Huffman coding (for example, when working on bespoke chess game compression for lichess.org), but there are better entropy coders (both in terms of compression ratio and performance) and better implementations.

See constriction for composable entropy coders, models and streams.

See arcode for a standalone implementation of arithmetic coding.

Examples

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);

Documentation

Read the documentation

Changelog

  • 0.6.1
    • Fix deprecation warning and remove #[deny(warnings)] (a future compatibility hazard in libraries).
  • 0.6.0
    • Update to bit-vec 0.6.
  • 0.5.0
    • Update to bit-vec 0.5.
  • 0.4.0
    • Renamed Tree::decoder() to Tree::unbounded_decoder() to avoid surprises. A new Tree::decoder() takes the maximum number of symbols to decode.
    • No longer reexporting Saturating from num-traits.
  • 0.3.2
    • Preallocate arena space for Huffman tree.
  • 0.3.1
    • Update num-traits to 0.2 (semver compatible).
  • 0.3.0
    • Introduce CodeBuilder.
    • Changes tie breaking order.
  • 0.2.0
    • Allow initialization from iterators without creating a HashMap. Thanks @mernen.
    • Require K: Ord instead of K: Hash + Eq for symbols and switch Book internals from HashMap to BTreeMap.
    • Specify stability guarantees.
  • 0.1.1
    • Expose more methods on Book.
  • 0.1.0
    • Initial release.

License

huffman-compress is dual licensed under the Apache 2.0 and MIT license, at your option.

rust-huffman-compress's People

Contributors

mernen avatar niklasf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rust-huffman-compress's Issues

Creating book/tree from existing Huffman code

Hello Niklas,

I'd love to have to a method to create a book/tree from an existing Huffman code. This is useful for decoding files, where the Huffman table is saved in the file.
Are you open for such a function in your code and/or can you implement it?

Cheers,
Raui

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.