Skip to main content

encode

Function encode 

Source
pub fn encode(
    symbols: &[u32],
    table: &FrequencyTable,
) -> Result<Vec<u8>, AnsError>
Expand description

Encode symbols into a byte stream using rANS with the given frequency table.

The output byte vector is in stack format: the final 4 bytes hold the rANS state, and any earlier bytes were emitted during renormalization. The decoder consumes these bytes LIFO (from the end toward the front).

ยงExample

use ans::{encode, decode, FrequencyTable};

let table = FrequencyTable::from_counts(&[3, 7], 12)?;
let message = [0u32, 1, 1, 0];
let bytes = encode(&message, &table)?;
let recovered = decode(&bytes, &table, message.len())?;
assert_eq!(recovered, message);