pub struct Histogram {
pub counts: Vec<i32>,
pub total_count: usize,
/* private fields */
}Expand description
A histogram counting symbol occurrences.
This is the encoder-side histogram structure, corresponding to libjxl’s
Histogram class in enc_ans_params.h.
Fields§
§counts: Vec<i32>Symbol counts (aligned to HISTOGRAM_ROUNDING).
Uses i32 to match libjxl’s ANSHistBin type.
total_count: usizeSum of all counts.
Implementations§
Source§impl Histogram
impl Histogram
Sourcepub fn with_capacity(length: usize) -> Self
pub fn with_capacity(length: usize) -> Self
Creates a histogram with pre-allocated capacity for length symbols.
The capacity is rounded up to HISTOGRAM_ROUNDING.
Sourcepub fn from_counts(counts: &[i32]) -> Self
pub fn from_counts(counts: &[i32]) -> Self
Creates a histogram from a slice of counts.
Sourcepub fn ensure_capacity(&mut self, length: usize)
pub fn ensure_capacity(&mut self, length: usize)
Ensures the histogram can hold at least length symbols.
Sourcepub fn add_histogram(&mut self, other: &Histogram)
pub fn add_histogram(&mut self, other: &Histogram)
Add another histogram’s counts to this one.
Sourcepub fn condition(&mut self)
pub fn condition(&mut self)
Trim trailing zeros and update total_count.
Should be called after a sequence of fast_add calls.
Sourcepub fn shannon_entropy(&self) -> f32
pub fn shannon_entropy(&self) -> f32
Compute Shannon entropy: -sum(count * log2(count / total)). Result is in bits (not nats).
Formula: sum of -(count/total) * log2(count/total) * total = sum of -count * log2(count/total) = sum of -count * (log2(count) - log2(total)) = sum of -count * log2(count) + count * log2(total) = -sum(count * log2(count)) + total * log2(total)
libjxl uses: -count * log2(count / total), excluding when count == total.
Sourcepub fn cached_entropy(&self) -> f32
pub fn cached_entropy(&self) -> f32
Get the cached entropy value.
Call shannon_entropy() first to ensure it’s up-to-date.
Sourcepub fn set_cached_entropy(&self, entropy: f32)
pub fn set_cached_entropy(&self, entropy: f32)
Set the cached entropy value (used when loading from test data).
Sourcepub fn alphabet_size(&self) -> usize
pub fn alphabet_size(&self) -> usize
Alphabet size (highest non-zero symbol + 1).
Sourcepub fn max_symbol(&self) -> usize
pub fn max_symbol(&self) -> usize
Returns the index of the maximum symbol with non-zero count.