dsi-bitstream 0.9.2

A Rust implementation of read/write bit streams supporting several types of instantaneous codes
Documentation
#![doc(hidden)]
// THIS FILE HAS BEEN GENERATED BY THE SCRIPT gen_code_tables.py
// ~~~~~~~~~~~~~~~~~~~ DO NOT MODIFY ~~~~~~~~~~~~~~~~~~~~~~
// Methods for reading and writing values using precomputed tables for gamma codes
use crate::traits::{BE, BitRead, BitWrite, LE};
use num_primitive::PrimitiveNumber;
/// How many bits are needed to read the tables
pub const READ_BITS: usize = 9;
/// Maximum value writable using the table(s)
pub const WRITE_MAX: u64 = 63;

/// Reads a value using a decoding table.
///
/// If the result is `Some` the decoding was successful, and
/// the decoded value and the length of the code are returned.
#[inline(always)]
pub fn read_table_le<B: BitRead<LE>>(backend: &mut B) -> Option<(u64, usize)> {
    if let Ok(idx) = backend.peek_bits(READ_BITS) {
        let idx: usize = idx.as_to();
        let len = READ_LEN_LE[idx];
        if len != MISSING_VALUE_LEN_LE {
            backend.skip_bits_after_peek(len as usize);
            return Some((READ_LE[idx] as u64, len as usize));
        }
    }
    None
}

/// Writes a value using an encoding table.
///
/// If the result is `Some` the encoding was successful, and
/// length of the code is returned.
#[inline(always)]
pub fn write_table_le<B: BitWrite<LE>>(backend: &mut B, n: u64) -> Result<Option<usize>, B::Error> {
    // We cannot use .get() here because n is a u64
    if n >= WRITE_LE.len() as u64 {
        return Ok(None);
    }
    let n = n as usize;
    let len = WRITE_LEN_LE[n] as usize;
    backend.write_bits(WRITE_LE[n] as u64, len)?;
    Ok(Some(len))
}

/// Reads a value using a decoding table.
///
/// If the result is `Some` the decoding was successful, and
/// the decoded value and the length of the code are returned.
#[inline(always)]
pub fn read_table_be<B: BitRead<BE>>(backend: &mut B) -> Option<(u64, usize)> {
    if let Ok(idx) = backend.peek_bits(READ_BITS) {
        let idx: usize = idx.as_to();
        let len = READ_LEN_BE[idx];
        if len != MISSING_VALUE_LEN_BE {
            backend.skip_bits_after_peek(len as usize);
            return Some((READ_BE[idx] as u64, len as usize));
        }
    }
    None
}

/// Writes a value using an encoding table.
///
/// If the result is `Some` the encoding was successful, and
/// length of the code is returned.
#[inline(always)]
pub fn write_table_be<B: BitWrite<BE>>(backend: &mut B, n: u64) -> Result<Option<usize>, B::Error> {
    // We cannot use .get() here because n is a u64
    if n >= WRITE_BE.len() as u64 {
        return Ok(None);
    }
    let n = n as usize;
    let len = WRITE_LEN_BE[n] as usize;
    backend.write_bits(WRITE_BE[n] as u64, len)?;
    Ok(Some(len))
}
/// The len we assign to a code that cannot be decoded through the table
pub const MISSING_VALUE_LEN_BE: u8 = 10;
/// Precomputed table for reading gamma codes
pub const READ_BE: &[u8] = &[
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
    27, 28, 29, 30, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12,
    12, 13, 13, 13, 13, 14, 14, 14, 14, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1,
];
/// The len we assign to a code that cannot be decoded through the table
pub const MISSING_VALUE_LEN_LE: u8 = 10;
/// Precomputed table for reading gamma codes
pub const READ_LE: &[u8] = &[
    0, 0, 1, 0, 3, 0, 2, 0, 7, 0, 1, 0, 4, 0, 2, 0, 15, 0, 1, 0, 5, 0, 2, 0, 8, 0, 1, 0, 6, 0, 2,
    0, 0, 0, 1, 0, 3, 0, 2, 0, 9, 0, 1, 0, 4, 0, 2, 0, 16, 0, 1, 0, 5, 0, 2, 0, 10, 0, 1, 0, 6, 0,
    2, 0, 0, 0, 1, 0, 3, 0, 2, 0, 11, 0, 1, 0, 4, 0, 2, 0, 17, 0, 1, 0, 5, 0, 2, 0, 12, 0, 1, 0, 6,
    0, 2, 0, 0, 0, 1, 0, 3, 0, 2, 0, 13, 0, 1, 0, 4, 0, 2, 0, 18, 0, 1, 0, 5, 0, 2, 0, 14, 0, 1, 0,
    6, 0, 2, 0, 0, 0, 1, 0, 3, 0, 2, 0, 7, 0, 1, 0, 4, 0, 2, 0, 19, 0, 1, 0, 5, 0, 2, 0, 8, 0, 1,
    0, 6, 0, 2, 0, 0, 0, 1, 0, 3, 0, 2, 0, 9, 0, 1, 0, 4, 0, 2, 0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5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 9, 1, 3, 1, 5,
    1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 10, 1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 9, 1, 3, 1,
    5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 10, 1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 9, 1, 3,
    1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 10, 1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 9, 1,
    3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 10, 1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 9,
    1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1, 10, 1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1,
    9, 1, 3, 1, 5, 1, 3, 1, 7, 1, 3, 1, 5, 1, 3, 1,
];
/// Table used to speed up the writing of gamma codes
pub const WRITE_BE: &[u16] = &[
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
    27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
    51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
];
/// Table used to speed up the writing of gamma codes
pub const WRITE_LEN_BE: &[u16] = &[
    1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
    11, 11, 11, 11, 11, 11, 11, 11, 13,
];
/// Table used to speed up the writing of gamma codes
pub const WRITE_LE: &[u16] = &[
    1, 2, 6, 4, 12, 20, 28, 8, 24, 40, 56, 72, 88, 104, 120, 16, 48, 80, 112, 144, 176, 208, 240,
    272, 304, 336, 368, 400, 432, 464, 496, 32, 96, 160, 224, 288, 352, 416, 480, 544, 608, 672,
    736, 800, 864, 928, 992, 1056, 1120, 1184, 1248, 1312, 1376, 1440, 1504, 1568, 1632, 1696,
    1760, 1824, 1888, 1952, 2016, 64,
];
/// Table used to speed up the writing of gamma codes
pub const WRITE_LEN_LE: &[u16] = &[
    1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
    11, 11, 11, 11, 11, 11, 11, 11, 13,
];
/// Table used to speed up the skipping of gamma codes
pub const LEN: &[u8] = &[
    1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
    11, 11, 11, 11, 11, 11, 11, 11, 13,
];
/// Asserts at compile time that `peek_bits` is large enough for these tables.
pub const fn check_read_table(peek_bits: usize) {
    assert!(
        peek_bits >= READ_BITS,
        "BitRead peek word too small for gamma code read tables (9 bits required)"
    );
}