Skip to main content

Histogram

Struct Histogram 

Source
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: usize

Sum of all counts.

Implementations§

Source§

impl Histogram

Source

pub fn new() -> Self

Creates an empty histogram.

Source

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.

Source

pub fn from_counts(counts: &[i32]) -> Self

Creates a histogram from a slice of counts.

Source

pub fn flat(length: usize, total_count: usize) -> Self

Creates a flat (uniform) histogram.

Source

pub fn clear(&mut self)

Clears all counts.

Source

pub fn add(&mut self, symbol: usize)

Add one occurrence of a symbol.

Source

pub fn ensure_capacity(&mut self, length: usize)

Ensures the histogram can hold at least length symbols.

Source

pub fn fast_add(&mut self, symbol: usize)

Fast add (caller must ensure capacity first).

Source

pub fn add_histogram(&mut self, other: &Histogram)

Add another histogram’s counts to this one.

Source

pub fn condition(&mut self)

Trim trailing zeros and update total_count. Should be called after a sequence of fast_add calls.

Source

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.

Source

pub fn cached_entropy(&self) -> f32

Get the cached entropy value. Call shannon_entropy() first to ensure it’s up-to-date.

Source

pub fn set_cached_entropy(&self, entropy: f32)

Set the cached entropy value (used when loading from test data).

Source

pub fn alphabet_size(&self) -> usize

Alphabet size (highest non-zero symbol + 1).

Source

pub fn max_symbol(&self) -> usize

Returns the index of the maximum symbol with non-zero count.

Source

pub fn is_empty(&self) -> bool

Check if histogram is empty (all zeros).

Trait Implementations§

Source§

impl Clone for Histogram

Source§

fn clone(&self) -> Histogram

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Histogram

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Histogram

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.