5 unstable releases

0.2.1 Mar 31, 2024
0.2.0 May 17, 2020
0.1.1 Feb 19, 2020
0.1.0 Feb 19, 2020
0.0.0 Jul 10, 2018

#65 in Compression

Download history 239/week @ 2023-12-22 209/week @ 2023-12-29 380/week @ 2024-01-05 448/week @ 2024-01-12 333/week @ 2024-01-19 358/week @ 2024-01-26 230/week @ 2024-02-02 296/week @ 2024-02-09 422/week @ 2024-02-16 518/week @ 2024-02-23 426/week @ 2024-03-01 460/week @ 2024-03-08 314/week @ 2024-03-15 740/week @ 2024-03-22 779/week @ 2024-03-29 402/week @ 2024-04-05

2,283 downloads per month
Used in 8 crates (via ggrs)

MIT/Apache

15KB
172 lines

bitfield-rle

crates.io version build status downloads docs.rs docs

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;
extern crate sparse_bitfield;

use sparse_bitfield::Bitfield;

let mut bits = Bitfield::new(1024);
bits.set(400, true);

let enc = bitfield_rle::encode(bits);
assert_eq!(enc.len(), 6);

let dec = bitfield_rle::decode(enc);

let bits = Bitfield::from_bytes(dec);
assert_eq!(bits.get(400), true);

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

$ cargo add bitfield-rle

License

MIT OR Apache-2.0

Dependencies

~130KB