| Crates.io | deepmesa-encoding |
| lib.rs | deepmesa-encoding |
| version | 0.12.0 |
| created_at | 2021-06-26 17:58:26.601719+00 |
| updated_at | 2025-08-03 22:06:24.689271+00 |
| description | A collection of encoding algorithms designed for performance |
| homepage | https://arrsingh.com/deepmesa-encoding |
| repository | https://github.com/deepmesa/encoding-rs |
| max_upload_size | |
| id | 415220 |
| size | 69,241 |
High-performance encoding algorithms for Rust, providing efficient implementations of various prefix and variable-length encoding schemes.
deepmesa-encoding offers a comprehensive collection of encoding algorithms optimized for performance and designed for use in data compression, information theory, and storage systems. All encoders and decoders work with the BitVector data structure from deepmesa-collections and provide consistent APIs with built-in performance metrics.
BitVector for compact bit storageIterator traitSimple entropy encoding where a natural number n is represented as n ones followed by a zero.
Examples:
2 → 1105 → 111110Universal coding developed by Peter Elias. Each value is encoded in two parts: a length (in unary) and an offset (binary with leading 1 removed).
Examples:
2 → 10_013 → 1110_101Extended gamma coding where the length is encoded using gamma encoding instead of unary.
Examples:
2 → 100_013 → 10_1_101Parametric coding optimal for geometric distributions. Uses a tunable parameter to encode values as quotient (unary) and remainder (truncated binary).
Encodes unsigned values in 8-byte chunks where the MSB indicates continuation (0) or termination (1).
Examples:
2 → 10000010255 → 00000001_11111111Add this to your Cargo.toml:
[dependencies]
deepmesa-encoding = "0.11.0"
use deepmesa_encoding::prefix::{UnaryEncoder, UnaryDecoder};
// Create encoder and encode some values
let mut encoder = UnaryEncoder::new();
encoder.encode(2);
encoder.encode(5);
encoder.encode(1);
// Get encoding statistics
println!("Encoded {} elements", encoder.elements());
println!("Used {} bits", encoder.encoded_len());
println!("Compression ratio: {:.2}", encoder.comp_ratio());
// Create decoder and decode values
let mut decoder = UnaryDecoder::new(encoder.encoded());
assert_eq!(decoder.decode(), Some(2));
assert_eq!(decoder.decode(), Some(5));
assert_eq!(decoder.decode(), Some(1));
assert_eq!(decoder.decode(), None);
use deepmesa_encoding::prefix::{GammaEncoder, GammaDecoder};
let mut encoder = GammaEncoder::new();
encoder.encode(13);
encoder.encode(8);
let mut decoder = GammaDecoder::new(encoder.encoded());
assert_eq!(decoder.decode(), Some(13));
assert_eq!(decoder.decode(), Some(8));
use deepmesa_encoding::prefix::{DeltaEncoder, DeltaDecoder};
let mut encoder = DeltaEncoder::new();
encoder.encode(2);
encoder.encode(13);
// Decode using iterator
let decoder = DeltaDecoder::new(encoder.encoded());
let values: Vec<u128> = decoder.collect();
assert_eq!(values, vec![2, 13]);
use deepmesa_encoding::prefix::{GolombEncoder, GolombDecoder};
let param = 6; // Golomb parameter
let mut encoder = GolombEncoder::new(param);
encoder.encode(9);
encoder.encode(13);
let mut decoder = GolombDecoder::new(encoder.encoded(), param);
assert_eq!(decoder.decode(), Some(9));
assert_eq!(decoder.decode(), Some(13));
use deepmesa_encoding::prefix::{VarByteEncoder, VarByteDecoder};
let mut encoder = VarByteEncoder::new();
encoder.encode(255);
encoder.encode(2);
// Decode multiple values at once
let decoder = VarByteDecoder::new(encoder.encoded());
let values = decoder.decode_many(2);
assert_eq!(values, vec![255, 2]);
All decoders implement Rust's Iterator trait:
use deepmesa_encoding::prefix::{GammaEncoder, GammaDecoder};
let mut encoder = GammaEncoder::new();
encoder.encode(1);
encoder.encode(4);
encoder.encode(7);
let decoder = GammaDecoder::new(encoder.encoded());
for value in decoder {
println!("Decoded: {}", value);
}
All encoders provide built-in performance metrics:
use deepmesa_encoding::prefix::UnaryEncoder;
let mut encoder = UnaryEncoder::new();
encoder.encode(10);
encoder.encode(5);
println!("Elements encoded: {}", encoder.elements());
println!("Total bits used: {}", encoder.encoded_len());
println!("Average bits per element: {:.2}", encoder.comp_ratio());
All encoders provide these methods:
new() - Create with default capacity (1024 bits)with_capacity(bits) - Create with specified capacityencode(value) - Encode a u128 valueencoded() - Get reference to underlying BitVectorencoded_len() - Get length in bitselements() - Get count of encoded elementscomp_ratio() - Get compression ratio (bits per element)All decoders provide these methods:
new(bitvec) - Create decoder from BitVectordecode() - Decode next value (mutable)decode_many(n) - Decode up to n valuesiter() - Get iterator for decodingu128 for maximum range supportOption<u128> (None when no more data)| Algorithm | Best Use Case | Compression Ratio |
|---|---|---|
| Unary | Small integers, sparse data | Poor for large values |
| Gamma | Medium-range integers | Good for powers of 2 |
| Delta | Large integers | Better than Gamma for big values |
| Golomb | Geometric distributions | Optimal with correct parameter |
| VarByte | Mixed-size integers | Good for byte-aligned processing |
Dual Licensed under the MIT LICENSE or the Apache-2 LICENSE:
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Contact: rsingh@arrsingh.com Website: https://www.arrsingh.com/deepmesa-collections