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>
impl<T: Hash + Eq + Clone + Ord + Debug> CodeBook<T>
Sourcepub fn from_data(data: impl AsRef<[T]>) -> Result<Self, Error>
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::DataEmptyifdatacontains 0 or 1 distinct symbols, since there is no sense in encoding this
Sourcepub fn from_mapping(mapping: impl AsRef<[(T, HfmnCode)]>) -> Self
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.
Sourcepub fn encode_data(
&self,
data: impl AsRef<[T]>,
) -> Result<Vec<&HfmnCode>, Error>
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::SymbolNotFoundif any of the symbols in data are not in the codebook
Trait Implementations§
impl<T: Eq + Hash + Eq> Eq for CodeBook<T>
Source§impl<T: PartialEq + Hash + Eq> PartialEq for CodeBook<T>
impl<T: PartialEq + Hash + Eq> PartialEq for CodeBook<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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