Struct constriction::symbol::exp_golomb::ExpGolomb
source · pub struct ExpGolomb<N> { /* private fields */ }
Expand description
Zero-sized marker struct for Exponential-Golomb Coding.
See Wikipedia.
This is not a super practical code. It’s provided here mainly to show that the symbol code traits can be used for codes with (essentially) infinitely sized codebooks.
Example
use constriction::{
symbol::{DefaultQueueEncoder, WriteBitStream, ReadBitStream},
symbol::exp_golomb::ExpGolomb,
UnwrapInfallible,
};
let codebook = ExpGolomb::<u32>::new();
let mut encoder = DefaultQueueEncoder::new();
encoder.encode_iid_symbols(&[3, 7, 0, 1], &codebook).unwrap();
let mut decoder = encoder.into_decoder().unwrap_infallible();
let bit_string = decoder.clone().map(
|bit| if bit.unwrap_infallible() { '1' } else { '0' }
).collect::<String>();
// (Note that the `DefaultQueueEncoder` pads to full words with zeros.
// This is not be a problem since we're using a prefix code.)
assert_eq!(bit_string, "00100000100010100000000000000000");
let decoded = decoder.decode_iid_symbols(4, &codebook).collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(decoded, [3, 7, 0, 1]);
Implementations§
Trait Implementations§
source§impl<N: Unsigned + PrimInt + WrappingAdd + WrappingSub> DecoderCodebook for ExpGolomb<N>
impl<N: Unsigned + PrimInt + WrappingAdd + WrappingSub> DecoderCodebook for ExpGolomb<N>
type InvalidCodeword = InvalidCodeword
fn decode_symbol<BackendError>( &self, source: impl Iterator<Item = Result<bool, BackendError>> ) -> Result<Self::Symbol, CoderError<SymbolCodeError<Self::InvalidCodeword>, BackendError>>
source§impl<N: Unsigned + PrimInt + WrappingAdd + WrappingSub> EncoderCodebook for ExpGolomb<N>
impl<N: Unsigned + PrimInt + WrappingAdd + WrappingSub> EncoderCodebook for ExpGolomb<N>
fn encode_symbol_prefix<BackendError>( &self, symbol: impl Borrow<Self::Symbol>, emit: impl FnMut(bool) -> Result<(), BackendError> ) -> Result<(), CoderError<DefaultEncoderFrontendError, BackendError>>
fn encode_symbol_suffix<BackendError>( &self, symbol: impl Borrow<Self::Symbol>, emit: impl FnMut(bool) -> Result<(), BackendError> ) -> Result<(), CoderError<DefaultEncoderFrontendError, BackendError>>
Auto Trait Implementations§
impl<N> RefUnwindSafe for ExpGolomb<N>where
N: RefUnwindSafe,
impl<N> Send for ExpGolomb<N>where
N: Send,
impl<N> Sync for ExpGolomb<N>where
N: Sync,
impl<N> Unpin for ExpGolomb<N>where
N: Unpin,
impl<N> UnwindSafe for ExpGolomb<N>where
N: 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