Skip to main content

CodeBook

Struct CodeBook 

Source
pub struct CodeBook<T: Hash + Eq> { /* private fields */ }
Expand description

A mapping of huffman codes to data symbols.

Implementations§

Source§

impl<T: Hash + Eq + Clone + Ord + Debug> CodeBook<T>

Source

pub fn from_data(data: impl AsRef<[T]>) -> Result<Self, Error>

Constructs a code book optimal over the given symbols.

The codebook will provide the optimal single-symbol prefix-free mapping, and can be used to encode or decode data according to that mapping.

§Arguments
  • data - A slice of symbols.
§Errors
  • Error::DataEmpty if data contains 0 or 1 distinct symbols, since there is no sense in encoding this
Source

pub fn from_mapping(mapping: impl AsRef<[(T, HfmnCode)]>) -> Self

Returns a codebook crated from a supplied mapping.

§Arguments
  • mapping - A slice of (symbol, code). This must contain each symbol at most once, and each ocde must be valid, and the set of codes must be correctly formed.
Source

pub fn encode_data( &self, data: impl AsRef<[T]>, ) -> Result<Vec<&HfmnCode>, Error>

Encodes symbols using the calculated Huffman codebook

Returns both the encoded version of the symbols and the total number of symbols encoded. This is needed by the decoder in order to accurately decode the codestream.

§Arguments
  • data - The symbols to be encoded
§Errors
  • Error::SymbolNotFound if any of the symbols in data are not in the codebook
Source

pub fn decode_data( &self, bytes: &[u8], num_symbols: usize, ) -> Result<Vec<&T>, Error>

Decode huffman codes

§Arguments
  • bytes - The bytes containing the codestream
  • num_symbols - The number of symbols that were encoded
§Errors
  • Error::InvalidCode if any of the codes read are not found in the codebook
Source§

impl<'a, T: Serialize + Deserialize<'a> + Hash + Ord + Clone + Debug> CodeBook<T>

Source

pub fn encode_book(self) -> Vec<u8>

Serialize the codebook to bytes

Source

pub fn decode_book(bytes: &'a [u8]) -> Result<Self, Error>

Deserialize a codebook from bytes

Note this function is not intended to provide deep validation on whether the codebook is valid or not.

§Arguments
  • bytes - The bytes to deserialize back into a codebook
§Errors
  • Error::BinaryCoding - If the bytes can’t be decoded into a trivially valid codebook

Trait Implementations§

Source§

impl<T: Clone + Hash + Eq> Clone for CodeBook<T>

Source§

fn clone(&self) -> CodeBook<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Hash + Eq> Debug for CodeBook<T>

Source§

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

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

impl<T: Debug + Hash + Eq> Display for CodeBook<T>

Source§

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

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

impl<T: Eq + Hash + Eq> Eq for CodeBook<T>

Source§

impl<T: PartialEq + Hash + Eq> PartialEq for CodeBook<T>

Source§

fn eq(&self, other: &CodeBook<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<T: PartialEq + Hash + Eq> StructuralPartialEq for CodeBook<T>

Auto Trait Implementations§

§

impl<T> Freeze for CodeBook<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for CodeBook<T>
where T: RefUnwindSafe,

§

impl<T> Send for CodeBook<T>
where T: Send,

§

impl<T> Sync for CodeBook<T>
where T: Sync,

§

impl<T> Unpin for CodeBook<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for CodeBook<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for CodeBook<T>
where T: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.