deepmesa-encoding
High-performance encoding algorithms for Rust, providing efficient implementations of various prefix and variable-length encoding schemes.
Overview
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.
Features
- High Performance: Hand-crafted implementations optimized for speed
- Consistent API: Uniform interface across all encoding algorithms
- Memory Efficient: Uses
BitVector
for compact bit storage - Comprehensive: Supports both encoding and decoding operations
- Iterator Support: All decoders implement Rust's
Iterator
trait - Performance Metrics: Built-in compression ratio and statistics tracking
Supported Encoding Algorithms
Unary Encoding
Simple entropy encoding where a natural number n
is represented as n
ones followed by a zero.
Examples:
2
→110
5
→111110
Gamma Encoding
Universal 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_0
13
→1110_101
Delta Encoding
Extended gamma coding where the length is encoded using gamma encoding instead of unary.
Examples:
2
→100_0
13
→10_1_101
Golomb Encoding
Parametric coding optimal for geometric distributions. Uses a tunable parameter to encode values as quotient (unary) and remainder (truncated binary).
Variable Byte Encoding
Encodes unsigned values in 8-byte chunks where the MSB indicates continuation (0) or termination (1).
Examples:
2
→10000010
255
→00000001_11111111
Usage
Add this to your Cargo.toml
:
[]
= "0.11.0"
Basic Encoding/Decoding Example
use ;
// Create encoder and encode some values
let mut encoder = new;
encoder.encode;
encoder.encode;
encoder.encode;
// Get encoding statistics
println!;
println!;
println!;
// Create decoder and decode values
let mut decoder = new;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Gamma Encoding Example
use ;
let mut encoder = new;
encoder.encode;
encoder.encode;
let mut decoder = new;
assert_eq!;
assert_eq!;
Delta Encoding Example
use ;
let mut encoder = new;
encoder.encode;
encoder.encode;
// Decode using iterator
let decoder = new;
let values: = decoder.collect;
assert_eq!;
Golomb Encoding Example
use ;
let param = 6; // Golomb parameter
let mut encoder = new;
encoder.encode;
encoder.encode;
let mut decoder = new;
assert_eq!;
assert_eq!;
Variable Byte Encoding Example
use ;
let mut encoder = new;
encoder.encode;
encoder.encode;
// Decode multiple values at once
let decoder = new;
let values = decoder.decode_many;
assert_eq!;
Iterator Usage
All decoders implement Rust's Iterator
trait:
use ;
let mut encoder = new;
encoder.encode;
encoder.encode;
encoder.encode;
let decoder = new;
for value in decoder
Performance Monitoring
All encoders provide built-in performance metrics:
use UnaryEncoder;
let mut encoder = new;
encoder.encode;
encoder.encode;
println!;
println!;
println!;
API Reference
Encoder Methods
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)
Decoder Methods
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 decoding
Special Cases
- GolombEncoder/GolombDecoder: Require parameter in constructor
- All values are
u128
for maximum range support - Decoders return
Option<u128>
(None when no more data)
Performance Characteristics
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 |
License
Dual Licensed under the MIT LICENSE or the Apache-2 LICENSE:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License
Contribution
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