Enum HuffmanTree

Source
pub enum HuffmanTree {
    Leaf(u8, u8),
    Node(Box<HuffmanTree>, Box<HuffmanTree>),
}
Expand description

HuffmanTree is a simple tree structure used convert encoded words to decoded words and vice versa.

Each leaf of the tree represents a single code word. Their probability is saved as single byte where 255 represents the highest probability, and 0 means the value does not appear.

You most likely don’t want to construct this tree yourself, so look for the 2 methods for constructing the tree for you.

§Examples

extern crate huffman_coding;

let fake_data = vec![1, 1, 0, 0, 2];
let tree = huffman_coding::HuffmanTree::from_data(&fake_data[..]);
let probability = tree.get_byte_prob(1);
assert!(probability.is_some());
assert_eq!(probability.unwrap(), 255);

Variants§

Implementations§

Source§

impl HuffmanTree

Source

pub fn from_table(data: &[u8]) -> Self

Method to read the probability of all 256 possible u8 values from a slice containing 256 elements.

This method can be used to construct a new tree from a list of probabilities. The first element in the slice will be interpreted as the probability of the 0 value appearing, the second as the probability of the 1 value, etc.

§Examples
extern crate huffman_coding;

let mut table_data: [u8; 256] = [0; 256];
table_data[0] = 255;
table_data[1] = 128;
table_data[2] = 128;
let tree = huffman_coding::HuffmanTree::from_table(&table_data[..]);

let test_query = tree.get_byte_prob(1);
assert!(test_query.is_some());
assert_eq!(test_query.unwrap(), 128);
§Panics

If data contains less than 256 elements

Source

pub fn from_data(data: &[u8]) -> Self

Reads all of data and constructs a huffman tree according to the provided sample data

§Examples
extern crate huffman_coding;
let pseudo_data = vec![0, 0, 1, 2, 2];
let tree = huffman_coding::HuffmanTree::from_data(&pseudo_data[..]);

let test_query = tree.get_byte_prob(0);
assert!(test_query.is_some());
assert_eq!(test_query.unwrap(), 255);
Source

pub fn to_table(&self) -> [u8; 256]

Convert an existing huffman tree into an array where each element represents the probability of the index byte to appear according to the huffman tree

This can be used to transmit the encoding scheme via byte buffer

Source

pub fn get_byte_prob(&self, byte: u8) -> Option<u8>

Return the probability of the given byte to appear according to the tree

If this returns None, then the byte should not appear according to the huffman tree If this returns Some, it will be between 255 meaning highest probability, and 1, meaning lowest probability

Trait Implementations§

Source§

impl Debug for HuffmanTree

Source§

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

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

impl Ord for HuffmanTree

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for HuffmanTree

Source§

fn eq(&self, other: &HuffmanTree) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for HuffmanTree

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for HuffmanTree

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> 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, 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.