Skip to main content

Module shannon_fano

Module shannon_fano 

Source
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_count times:
    • 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.