Struct arithmetic_coding::Encoder
source · [−]Expand description
An arithmetic encoder
An arithmetic decoder converts a stream of symbols into a stream of bits,
using a predictive Model
.
Implementations
sourceimpl<'a, M, W> Encoder<'a, M, W> where
M: Model,
W: BitWrite,
impl<'a, M, W> Encoder<'a, M, W> where
M: Model,
W: BitWrite,
sourcepub fn new(model: M, bitwriter: &'a mut W) -> Self
pub fn new(model: M, bitwriter: &'a mut W) -> Self
Construct a new Encoder
.
The ‘precision’ of the encoder is maximised, based on the number of bits
needed to represent the Model::denominator
. ‘precision’ bits is
equal to BitStore::BITS
- Model::denominator
bits. If you need
to set the precision manually, use Encoder::with_precision
.
Panics
The calculation of the number of bits used for ‘precision’ is subject to the following constraints:
- The total available bits is
BitStore::BITS
- The precision must use at least 2 more bits than that needed to
represent
Model::denominator
If these constraints cannot be satisfied this method will panic in debug builds
sourcepub fn with_precision(model: M, bitwriter: &'a mut W, precision: u32) -> Self
pub fn with_precision(model: M, bitwriter: &'a mut W, precision: u32) -> Self
Construct a new Encoder
with a custom precision.
Panics
The calculation of the number of bits used for ‘precision’ is subject to the following constraints:
- The total available bits is
BitStore::BITS
- The precision must use at least 2 more bits than that needed to
represent
Model::denominator
If these constraints cannot be satisfied this method will panic in debug builds
sourcepub fn encode_all(
&mut self,
symbols: impl IntoIterator<Item = M::Symbol>
) -> Result<(), Error<M::ValueError>>
pub fn encode_all(
&mut self,
symbols: impl IntoIterator<Item = M::Symbol>
) -> Result<(), Error<M::ValueError>>
Encode a stream of symbols into the provided output.
This method will encode all the symbols in the iterator, followed by EOF
(None
), and then call Encoder::flush
.
Errors
This method can fail if the underlying BitWrite
cannot be written
to.
sourcepub fn encode(
&mut self,
symbol: Option<&M::Symbol>
) -> Result<(), Error<M::ValueError>>
pub fn encode(
&mut self,
symbol: Option<&M::Symbol>
) -> Result<(), Error<M::ValueError>>
Encode a symbol into the provided output.
When you finish encoding symbols, you must manually encode an EOF symbol
by calling Encoder::encode
with None
.
The internal buffer must be manually flushed using Encoder::flush
.
Errors
This method can fail if the underlying BitWrite
cannot be written
to.
sourcepub fn flush(&mut self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
Flush any pending bits from the buffer
This method must be called when you finish writing symbols to a stream
of bits. This is called automatically when you use
Encoder::encode_all
.
Errors
This method can fail if the underlying BitWrite
cannot be written
to.
Trait Implementations
Auto Trait Implementations
impl<'a, M, W> RefUnwindSafe for Encoder<'a, M, W> where
M: RefUnwindSafe,
W: RefUnwindSafe,
<M as Model>::B: RefUnwindSafe,
impl<'a, M, W> Send for Encoder<'a, M, W> where
M: Send,
W: Send,
<M as Model>::B: Send,
impl<'a, M, W> Sync for Encoder<'a, M, W> where
M: Sync,
W: Sync,
<M as Model>::B: Sync,
impl<'a, M, W> Unpin for Encoder<'a, M, W> where
M: Unpin,
<M as Model>::B: Unpin,
impl<'a, M, W> !UnwindSafe for Encoder<'a, M, W>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more