bitfield-rle
A run-length-encoder that compresses bitfields.
The encoder uses a compact format and will only run length encode sequences of bits if it compresses the bitfield. The encoded bitfield should therefore always be smaller or the same size as the original bitfield with the exception of a 1-6 byte header.
Since this uses run-length-encoding, you'll get the best compression results if you have longer sequences of the same bit in your bitfield.
Usage
extern crate bitfield_rle as rle;
extern crate sparse_bitfield as bitfield;
use Bitfield;
let mut bits = new;
bits.set;
let len = bits.byte_len;
let mut bytes = Vec with_capacity;
bits.to_bytes;
let len = encode_len;
let enc = Vec with_capacity;
encode;
assert_eq!;
let len = decode_len;
let dec = Vec with_capacity;
decode;
let bits = from_bytes;
assert_eq!;
Format
The encoded bitfield is a series of compressed and uncompressed bit sequences. All sequences start with a header that is a varint.
If the last bit is set in the varint (it is an odd number) then a header represents a compressed bit sequence.
S = varint([l << 2, b << 1, 1])
where
S = compressed sequence
l = byte length of sequence
b = bit
If the last bit is set to 0
then a header represents an uncompressed bit
sequence.
S = [varint([l << 1, 0]), b]
where
S = uncompressed sequence
l = byte length of bitfield
b = bitfield
Installation
License
MIT OR Apache-2.0