fn decode_symbol<D>(
        &mut self,
        model: D,
    ) -> Result<D::Symbol, CoderError<Self::FrontendError, Self::BackendError>>
    where
        D: DecoderModel<PRECISION>,
        D::Probability: Into<Self::Word>,
        Self::Word: AsPrimitive<D::Probability>,
    {
        // We maintain the following invariant (*):
        //   point (-) lower < range
        // where (-) denotes wrapping subtraction (in `Self::State`).

        let scale = self.state.range.get() >> PRECISION;
        let quantile = self.point.wrapping_sub(&self.state.lower) / scale;
        if quantile >= State::one() << PRECISION {
            return Err(CoderError::Frontend(DecoderFrontendError::InvalidData));
        }

        let (symbol, left_sided_cumulative, probability) =
            model.quantile_function(quantile.as_().as_());

        // Update `state` in the same way as we do in `encode_symbol` (see comments there):
        self.state.lower = self
            .state
            .lower
            .wrapping_add(&(scale * left_sided_cumulative.into().into()));
        self.state.range = (scale * probability.get().into().into())
            .into_nonzero()
            .expect("TODO");

        // Invariant (*) is still satisfied at this point because:
        //   (point (-) lower) / scale = (point (-) old_lower) / scale (-) left_sided_cumulative
        //                             = quantile (-) left_sided_cumulative
        //                             < probability
        // Therefore, we have:
        //   point (-) lower < scale * probability <= range

        if self.state.range.get() < State::one() << (State::BITS - Word::BITS) {
            // First update `state` in the same way as we do in `encode_symbol`:
            self.state.lower = self.state.lower << Word::BITS;
            self.state.range = unsafe {
                // SAFETY:
                // - `range` is nonzero because it is a `State::NonZero`
                // - Shifting `range` left by `Word::BITS` bits doesn't truncate
                //   because we checked that `range < 1 << (State::BITS - Word::Bits)`.
                (self.state.range.get() << Word::BITS).into_nonzero_unchecked()
            };

            // Then update `point`, which restores invariant (*):
            self.point = self.point << Word::BITS;
            if let Some(word) = self.bulk.read()? {
                self.point = self.point | word.into();
            }

            // TODO: register reads past end?
        }

        Ok(symbol)
    }
/// Decodes a single symbol and pops it off the compressed data.
///
/// This is a low level method. You usually probably want to call a batch method
/// like [`decode_symbols`](#method.decode_symbols) or
/// [`decode_iid_symbols`](#method.decode_iid_symbols) instead.
///
/// This method is called `decode_symbol` rather than `decode_symbol` to stress the
/// fact that the `Coder` is a stack: `decode_symbol` will return the *last* symbol
/// that was previously encoded via [`encode_symbol`](#method.encode_symbol).
///
/// Note that this method cannot fail. It will still produce symbols in a
/// deterministic way even if the coder is empty, but such symbols will not
/// recover any previously encoded data and will generally have low entropy.
/// Still, being able to pop off an arbitrary number of symbols can sometimes be
/// useful in edge cases of, e.g., the bits-back algorithm.
Decodes a single symbol and pops it off the compressed data.
This is a low level method. You usually probably want to call a batch method
like [`decode_symbols`](#method.decode_symbols) or
[`decode_iid_symbols`](#method.decode_iid_symbols) instead.



Note that this method cannot fail. It will still produce symbols in a
deterministic way even if the coder is empty, but such symbols will not
recover any previously encoded data and will generally have low entropy.
Still, being able to pop off an arbitrary number of symbols can sometimes be
useful in edge cases of, e.g., the bits-back algorithm.
Decodes a single symbol and pops it off the compressed data . This is a low level method . You usually probably want to call a batch method like [ ` decode_symbols ` ] ( #method . decode_symbols ) or [ ` decode_iid_symbols ` ] ( #method . decode_iid_symbols ) instead . Note that this method cannot fail . It will still produce symbols in a deterministic way even if the coder is empty but such symbols will not recover any previously encoded data and will generally have low entropy . Still being able to pop off an arbitrary number of symbols can sometimes be useful in edge cases of e . g . the bits - back algorithm .
fn decode_symbol<D>(
        &mut self,
        model: D,
    ) -> Result<D::Symbol, CoderError<Self::FrontendError, Self::BackendError>>
    where
        D: DecoderModel<PRECISION>,
        D::Probability: Into<Self::Word>,
        Self::Word: AsPrimitive<D::Probability>,
    {
        // We maintain the following invariant (*):
        //   point (-) lower < range
        // where (-) denotes wrapping subtraction (in `Self::State`).
        let scale = self.state.range.get() >> PRECISION;
        let quantile = self.point.wrapping_sub(&self.state.lower) / scale;
        if quantile >= State::one() << PRECISION {
            return Err(CoderError::Frontend(DecoderFrontendError::InvalidData));
        }
        let (symbol, left_sided_cumulative, probability) =
            model.quantile_function(quantile.as_().as_());
        // Update `state` in the same way as we do in `encode_symbol` (see comments there):
        self.state.lower = self
            .state
            .lower
            .wrapping_add(&(scale * left_sided_cumulative.into().into()));
        self.state.range = (scale * probability.get().into().into())
            .into_nonzero()
            .expect("TODO");
        // Invariant (*) is still satisfied at this point because:
        //   (point (-) lower) / scale = (point (-) old_lower) / scale (-) left_sided_cumulative
        //                             = quantile (-) left_sided_cumulative
        //                             < probability
        // Therefore, we have:
        //   point (-) lower < scale * probability <= range
        if self.state.range.get() < State::one() << (State::BITS - Word::BITS) {
            // First update `state` in the same way as we do in `encode_symbol`:
            self.state.lower = self.state.lower << Word::BITS;
            self.state.range = unsafe {
                // SAFETY:
                // - `range` is nonzero because it is a `State::NonZero`
                // - Shifting `range` left by `Word::BITS` bits doesn't truncate
                //   because we checked that `range < 1 << (State::BITS - Word::Bits)`.
                (self.state.range.get() << Word::BITS).into_nonzero_unchecked()
            };
            // Then update `point`, which restores invariant (*):
            self.point = self.point << Word::BITS;
            if let Some(word) = self.bulk.read()? {
                self.point = self.point | word.into();
            }
            // TODO: register reads past end?
        }
        Ok(symbol)
    }
fn decode_symbol < D > ( & mut self , model : D , ) -> Result < D :: Symbol , CoderError < Self :: FrontendError , Self :: BackendError > > where D : DecoderModel < PRECISION > , D :: Probability : Into < Self :: Word > , Self :: Word : AsPrimitive < D :: Probability > , { // We maintain the following invariant (*): //   point (-) lower < range // where (-) denotes wrapping subtraction (in `Self::State`). let scale = self . state . range . get ( ) >> PRECISION ; let quantile = self . point . wrapping_sub ( & self . state . lower ) / scale ; if quantile >= State :: one ( ) << PRECISION { return Err ( CoderError :: Frontend ( DecoderFrontendError :: InvalidData ) ) ; } let ( symbol , left_sided_cumulative , probability ) = model . quantile_function ( quantile . as_ ( ) . as_ ( ) ) ; // Update `state` in the same way as we do in `encode_symbol` (see comments there): self . state . lower = self . state . lower . wrapping_add ( & ( scale * left_sided_cumulative . into ( ) . into ( ) ) ) ; self . state . range = ( scale * probability . get ( ) . into ( ) . into ( ) ) . into_nonzero ( ) . expect ( " " ) ; // Invariant (*) is still satisfied at this point because: //   (point (-) lower) / scale = (point (-) old_lower) / scale (-) left_sided_cumulative //                             = quantile (-) left_sided_cumulative //                             < probability // Therefore, we have: //   point (-) lower < scale * probability <= range if self . state . range . get ( ) < State :: one ( ) << ( State :: BITS - Word :: BITS ) { // First update `state` in the same way as we do in `encode_symbol`: self . state . lower = self . state . lower << Word :: BITS ; self . state . range = unsafe { // SAFETY: // - `range` is nonzero because it is a `State::NonZero` // - Shifting `range` left by `Word::BITS` bits doesn't truncate //   because we checked that `range < 1 << (State::BITS - Word::Bits)`. ( self . state . range . get ( ) << Word :: BITS ) . into_nonzero_unchecked ( ) } ; // Then update `point`, which restores invariant (*): self . point = self . point << Word :: BITS ; if let Some ( word ) = self . bulk . read ( ) ? { self . point = self . point | word . into ( ) ; } // TODO: register reads past end? } Ok ( symbol ) }
Decodes a single symbol and pops it off the compressed data.
Decodes a single symbol and pops it off the compressed data .
&mut self
model
D
&mut self
model
D