pub struct Coding<ValueType, D = BitsPerFragment> {
pub values: Box<[ValueType]>,
pub internal_nodes_count: Box<[u32]>,
pub degree: D,
}Expand description
Succinct representation of minimum-redundancy coding (huffman tree of some degree in the canonical form).
Fields§
§values: Box<[ValueType]>Values, from the most frequent to the least.
internal_nodes_count: Box<[u32]>Number of the internal nodes of each tree level. The root is not counted. Contains exactly one zero at the end.
degree: DSize of the fragment given as bits per fragment or the degree of the Huffman tree.
Implementations§
Source§impl<ValueType, D: TreeDegree> Coding<ValueType, D>
impl<ValueType, D: TreeDegree> Coding<ValueType, D>
Sourcepub fn from_frequencies<F: Frequencies<Value = ValueType>>(
degree: D,
frequencies: F,
) -> Self
pub fn from_frequencies<F: Frequencies<Value = ValueType>>( degree: D, frequencies: F, ) -> Self
Constructs coding for given frequencies of values and degree of the Huffman tree.
Sourcepub fn from_frequencies_cloned<F: Frequencies<Value = ValueType>>(
degree: D,
frequencies: &F,
) -> Self
pub fn from_frequencies_cloned<F: Frequencies<Value = ValueType>>( degree: D, frequencies: &F, ) -> Self
Constructs coding for given frequencies of values and degree of the Huffman tree.
Values are cloned from frequencies.
Sourcepub fn from_iter<Iter>(degree: D, iter: Iter) -> Self
pub fn from_iter<Iter>(degree: D, iter: Iter) -> Self
Counts occurrences of all values exposed by iter and constructs coding for obtained
frequencies of values and degree of the Huffman tree.
Sourcepub fn total_fragments_count(&self) -> usize
pub fn total_fragments_count(&self) -> usize
Returns total (summarized) number of code fragments of all values.
The algorithm runs in O(L) time and O(1) memory, where L is the number of fragments in the longest codeword.
Sourcepub fn decoder(&self) -> Decoder<'_, ValueType, D>
pub fn decoder(&self) -> Decoder<'_, ValueType, D>
Returns decoder that allows for decoding a value.
Sourcepub fn from_sorted<W>(
degree: D,
values: Box<[ValueType]>,
freq: &mut [W],
) -> Selfwhere
W: Weight,
pub fn from_sorted<W>(
degree: D,
values: Box<[ValueType]>,
freq: &mut [W],
) -> Selfwhere
W: Weight,
Construct coding (of given degree) for the given values, where
freq is an array of numbers of occurrences of corresponding values.
freq has to be in non-descending order and of the same length as values.
The algorithm runs in O(values.len) time,
in-place (it uses and changes freq and move values to the returned Coding object).
Sourcepub fn from_unsorted<W>(
degree: D,
values: Box<[ValueType]>,
freq: &mut [W],
) -> Selfwhere
W: Weight,
pub fn from_unsorted<W>(
degree: D,
values: Box<[ValueType]>,
freq: &mut [W],
) -> Selfwhere
W: Weight,
Construct coding (of the given degree) for the given values, where
freq has to be of the same length as values and contain number of occurrences of corresponding values.
The algorithm runs in O(values.len * log(values.len)) time.
Sourcepub fn write_internal_nodes_count_bytes(&self) -> usize
pub fn write_internal_nodes_count_bytes(&self) -> usize
Returns number of bytes which write_internal_nodes_count will write.
Sourcepub fn write_internal_nodes_count(&self, output: &mut dyn Write) -> Result<()>
pub fn write_internal_nodes_count(&self, output: &mut dyn Write) -> Result<()>
Writes internal_nodes_count to output as the following internal_nodes_count.len(), VByte values:
internal_nodes_count.len()-1 (=l), internal_nodes_count[0], internal_nodes_count[1], …, internal_nodes_count[l]
Sourcepub fn read_internal_nodes_count(input: &mut dyn Read) -> Result<Box<[u32]>>
pub fn read_internal_nodes_count(input: &mut dyn Read) -> Result<Box<[u32]>>
Reads (written by write_internal_nodes_count) internal_nodes_count from input.
Sourcepub fn write_values_size_bytes(
&self,
value_size: ValueSize<'_, ValueType>,
) -> usize
pub fn write_values_size_bytes( &self, value_size: ValueSize<'_, ValueType>, ) -> usize
Returns number of bytes which write_values will write,
assuming that each call to write_value writes the number of bytes pointed by value_size.
Sourcepub fn write_values<F>(
&self,
output: &mut dyn Write,
write_value: F,
) -> Result<()>
pub fn write_values<F>( &self, output: &mut dyn Write, write_value: F, ) -> Result<()>
Writes values to the given output, using write_value to write each value.
Sourcepub fn read_values<F>(
input: &mut dyn Read,
read_value: F,
) -> Result<Box<[ValueType]>>
pub fn read_values<F>( input: &mut dyn Read, read_value: F, ) -> Result<Box<[ValueType]>>
Reads values from the given input, using read_value to read each value.
Sourcepub fn write_size_bytes(&self, value_size: ValueSize<'_, ValueType>) -> usize
pub fn write_size_bytes(&self, value_size: ValueSize<'_, ValueType>) -> usize
Returns number of bytes which write will write,
assuming that each call to write_value writes the number of bytes pointed by value_size.
Sourcepub fn write<F>(&self, output: &mut dyn Write, write_value: F) -> Result<()>
pub fn write<F>(&self, output: &mut dyn Write, write_value: F) -> Result<()>
Writes self to the given output, using write_value to write each value.
Sourcepub fn read<F>(input: &mut dyn Read, read_value: F) -> Result<Self>
pub fn read<F>(input: &mut dyn Read, read_value: F) -> Result<Self>
Reads Coding from the given input, using read_value to read each value.
Sourcepub fn levels(&self) -> LevelIterator<'_, ValueType, D> ⓘ
pub fn levels(&self) -> LevelIterator<'_, ValueType, D> ⓘ
Returns iterator over the levels of the huffman tree.
Sourcepub fn reverse_code(&self, codeword: &mut Code)
pub fn reverse_code(&self, codeword: &mut Code)
Reverse the codeword.
Sourcepub fn reversed_code(&self, codeword: Code) -> Code
pub fn reversed_code(&self, codeword: Code) -> Code
Returns reversed copy of the given codeword.
Sourcepub fn codes(&self) -> CodesIterator<'_, ValueType, D> ⓘ
pub fn codes(&self) -> CodesIterator<'_, ValueType, D> ⓘ
Returns iterator over value-codeword pairs.
Sourcepub fn reversed_codes(&self) -> ReversedCodesIterator<'_, ValueType, D> ⓘ
pub fn reversed_codes(&self) -> ReversedCodesIterator<'_, ValueType, D> ⓘ
Returns iterator over value-codeword pairs with reversed codewords.
Source§impl<ValueType: Hash + Eq, D: TreeDegree> Coding<ValueType, D>
impl<ValueType: Hash + Eq, D: TreeDegree> Coding<ValueType, D>
Sourcepub fn code_lengths_ref(&self) -> HashMap<&ValueType, u32>
pub fn code_lengths_ref(&self) -> HashMap<&ValueType, u32>
Returns a map from (references to) values to the lengths of their codes.
Sourcepub fn codes_for_values_ref(&self) -> HashMap<&ValueType, Code>
pub fn codes_for_values_ref(&self) -> HashMap<&ValueType, Code>
Returns a map from (references to) values to their codes.
Sourcepub fn reversed_codes_for_values_ref(&self) -> HashMap<&ValueType, Code>
pub fn reversed_codes_for_values_ref(&self) -> HashMap<&ValueType, Code>
Returns a map from (references to) values to their reversed codes.
Source§impl<ValueType: Hash + Eq + Clone, D: TreeDegree> Coding<ValueType, D>
impl<ValueType: Hash + Eq + Clone, D: TreeDegree> Coding<ValueType, D>
Sourcepub fn code_lengths(&self) -> HashMap<ValueType, u32>
pub fn code_lengths(&self) -> HashMap<ValueType, u32>
Returns a map from (clones of) values to the lengths of their codes.
Sourcepub fn codes_for_values(&self) -> HashMap<ValueType, Code>
pub fn codes_for_values(&self) -> HashMap<ValueType, Code>
Returns a map from (clones of) values to their codes.
Sourcepub fn reversed_codes_for_values(&self) -> HashMap<ValueType, Code>
pub fn reversed_codes_for_values(&self) -> HashMap<ValueType, Code>
Returns a map from (clones of) values to their reversed codes.
Source§impl<D: TreeDegree> Coding<u8, D>
impl<D: TreeDegree> Coding<u8, D>
Sourcepub fn code_lengths_array(&self) -> [u32; 256]
pub fn code_lengths_array(&self) -> [u32; 256]
Returns array indexed by values that contains the lengths of their codes.
Sourcepub fn codes_for_values_array(&self) -> [Code; 256]
pub fn codes_for_values_array(&self) -> [Code; 256]
Returns array indexed by values that contains their codes.
Sourcepub fn reversed_codes_for_values_array(&self) -> [Code; 256]
pub fn reversed_codes_for_values_array(&self) -> [Code; 256]
Returns array indexed by values that contains their reversed codes.
Trait Implementations§
Source§impl<ValueType: GetSize, D> GetSize for Coding<ValueType, D>
impl<ValueType: GetSize, D> GetSize for Coding<ValueType, D>
Source§const USES_DYN_MEM: bool = true
const USES_DYN_MEM: bool = true
true if and only if the variables of this type can use dynamic (heap) memory.Source§fn size_bytes_dyn(&self) -> usize
fn size_bytes_dyn(&self) -> usize
self.
Same as self.size_bytes() - std::mem::size_of_val(self).Source§fn size_bytes_content_dyn(&self) -> usize
fn size_bytes_content_dyn(&self) -> usize
self content.
It usually equals to size_bytes_dyn().
However, sometimes it is smaller by the amount of memory reserved but not yet used
(e.g., size_bytes_content_dyn() only takes into account the length of the vector and not its capacity).Source§fn size_bytes(&self) -> usize
fn size_bytes(&self) -> usize
self.