Expand description
Manual Shannon-Fano compression and decompression helpers.
Shannon-Fano coding is a classic entropy coder that predates Huffman coding.
It starts from symbol frequencies, sorts symbols by descending probability,
and then recursively splits the list into two groups whose total weights are
as balanced as possible. The left group receives a 0 bit and the right
group receives a 1 bit. Repeating that process produces a prefix-free code
table that can be used to pack the input into bits.
§Format Overview
The compressed stream begins with the ASCII header SFAN. It is followed by
a stored static frequency table and then the packed payload bits:
- 4-byte magic header:
SFAN - 8-byte little-endian original length
- 2-byte little-endian symbol count
- repeated
symbol_counttimes:- 1 byte: symbol value
- 4 bytes little-endian: symbol frequency
- packed compressed bits in most-significant-bit-first order
As with the Huffman and arithmetic coders in this crate, the decoder is fully self-contained because it rebuilds the exact same model from the stored frequency table.
See the book entry Entropy coding.
Functions§
- shannon_
fano_ compress - Compress a byte slice with a manual Shannon-Fano encoder.
- shannon_
fano_ decompress - Decompress a byte slice previously produced by
shannon_fano_compress. - try_
shannon_ fano_ compress - Fallible variant of
shannon_fano_compress. - try_
shannon_ fano_ decompress - Fallible variant of
shannon_fano_decompress.