pub struct EntropyModel {
pub scale_bits: u8,
pub frequencies: Vec<u32>,
}Expand description
A normalized frequency table over the byte alphabet.
Fields§
§scale_bits: u8Number of scale bits; the table sums to 1 << scale_bits.
frequencies: Vec<u32>Exactly ALPHABET entries; sum == 1 << scale_bits.
Implementations§
Source§impl EntropyModel
impl EntropyModel
Sourcepub fn from_counts(counts: &[u64; 256], scale_bits: u8) -> Result<EntropyModel>
pub fn from_counts(counts: &[u64; 256], scale_bits: u8) -> Result<EntropyModel>
Canonical normalization of observed symbol counts into a frequency table.
Requirements (all hold):
- integer-only; no floating point anywhere;
- deterministic: same input yields identical output bytes;
- every symbol with
count > 0gets frequency>= 1; - symbols with
count == 0get frequency0; sum(frequencies) == 1 << scale_bitsexactly;- if all counts are
0, return the canonical uniform model.
Algorithm: after granting each present symbol a guaranteed minimum of
1, the remaining budget is apportioned by the largest-remainder (Hare)
method using u128 products, with ties broken by lower symbol index.
Sourcepub fn uniform(scale_bits: u8) -> Result<EntropyModel>
pub fn uniform(scale_bits: u8) -> Result<EntropyModel>
Uniform model with every symbol getting an equal frequency.
Requires scale_bits >= 8, i.e. 1 << scale_bits >= ALPHABET; below
that no integer table of 256 equal entries can sum to the target, so the
request is rejected as ErrorClass::InvalidModel.
Sourcepub fn encode(&self) -> Result<Vec<u8>>
pub fn encode(&self) -> Result<Vec<u8>>
Canonical wire encoding (little-endian):
[version u8 = 1][scale_bits u8][count u16 = 256][freq u16 x 256].
The model is validated before serialization so that encode cannot emit
a non-canonical table.
Sourcepub fn decode(bytes: &[u8]) -> Result<EntropyModel>
pub fn decode(bytes: &[u8]) -> Result<EntropyModel>
Parse and validate a canonical model; bytes must be exactly consumed.