Qu

huffman_encode

Builtin index · File I/O

result = huffman_encode(data)

Input: data · Output: result

What it is

Canonical Huffman coding of data (a Str, or a Vec of 0-255 byte values). Stores the code as one byte of code length per symbol, so the decoder rebuilds the tree rather than carrying it.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = huffman_encode(data)
data Str or Vecnot documented yet
lengths Veca length-256 Vec, the code length of each byte value, 0 for a value that never occurs
bits Veca Vec of 0-255 packed code bytes
bit_len Numa Num, how many of those bits are real
symbol_count Numa Num, the input length
Returns a Record with fields lengths (a length-256 Vec, the code length of each byte value, 0 for a value that never occurs), bits (a Vec of 0-255 packed code bytes), bit_len (a Num, how many of those bits are real) and symbol_count (a Num, the input length). Empty input gives none rather than a record: there is no data to encode, and none to decode back.

Examples

No documentation example calls this form yet. The File I/O chapter is where one belongs.

How it works

── general-purpose compression (2026-09-09) ───────────────── Three real, from-scratch entropy coders, each in its own module (huffman/range_coder/rans — see their own doc comments for the algorithms and why each one is shaped the way it is). All three share the same input convention via bytes_arg (a Str's UTF-8 bytes or a Vec of 0-255 byte values) and the same output convention via bytes_to_value (a byte Vec by default, or a Str with as_str=true). huffman_encode/range_encode return none for empty input (nothing to encode, nothing meaningful to decode back) rather than an empty-but-valid record — an empty record would still need every field present with sensible defaults, which is more surface area than the genuinely-degenerate "there was no data" case deserves.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.