1 use crate::io::bitreader::{BitReader, BitReaderError, BitReaderResult};
4 pub enum UintCodeType {
9 LimitedUnary(u32, u32),
16 pub enum IntCodeType {
23 pub trait IntCodeReader {
24 fn read_code(&mut self, t: UintCodeType) -> BitReaderResult<u32>;
25 fn read_code_signed(&mut self, t: IntCodeType) -> BitReaderResult<i32>;
28 fn read_unary(br: &mut BitReader, terminator: u32) -> BitReaderResult<u32> {
31 if br.read(1)? == terminator { return Ok(res); }
36 fn read_unary_lim(br: &mut BitReader, len: u32, terminator: u32) -> BitReaderResult<u32> {
39 if br.read(1)? == terminator { return Ok(res); }
41 if res == len { return Ok(res); }
45 fn read_unary210(br: &mut BitReader) -> BitReaderResult<u32> {
46 let val = read_unary_lim(br, 2, 0)?;
50 fn read_golomb(br: &mut BitReader, m: u8) -> BitReaderResult<u32> {
51 if m == 0 { return Err(BitReaderError::InvalidValue); }
52 let nbits = (8 - m.leading_zeros()) as u8;
53 if (m & (m - 1)) == 0 { return read_rice(br, nbits); }
54 let cutoff = u32::from((1 << nbits) - m);
55 let pfx = read_unary(br, 0)?;
56 let tail = br.read(nbits - 1)?;
58 let res = pfx * u32::from(m) + tail;
61 let add = br.read(1)?;
62 let res = pfx * u32::from(m) + (tail - cutoff) * 2 + add + cutoff;
67 fn read_rice(br: &mut BitReader, k: u8) -> BitReaderResult<u32> {
68 let pfx = read_unary(br, 1)?;
69 let ret = (pfx << k) + br.read(k)?;
73 fn read_gamma(br: &mut BitReader) -> BitReaderResult<u32> {
75 while br.read(1)? != 1 {
76 ret = (ret << 1) | br.read(1)?;
81 fn read_gammap(br: &mut BitReader) -> BitReaderResult<u32> {
82 let pfx = read_unary(br, 1)?;
83 if pfx > 32 { return Err(BitReaderError::InvalidValue); }
84 let ret = (1 << pfx) + br.read(pfx as u8)?;
88 fn uval_to_sval0mp(uval: u32) -> i32 {
89 if (uval & 1) != 0 { -((uval >> 1) as i32) }
90 else { (uval >> 1) as i32 }
93 fn uval_to_sval0pm(uval: u32) -> i32 {
94 if (uval & 1) != 0 { ((uval + 1) >> 1) as i32 }
95 else { -((uval >> 1) as i32) }
98 impl<'a> IntCodeReader for BitReader<'a> {
100 fn read_code(&mut self, t: UintCodeType) -> BitReaderResult<u32> {
102 UintCodeType::UnaryOnes => read_unary(self, 0),
103 UintCodeType::UnaryZeroes => read_unary(self, 1),
104 UintCodeType::LimitedUnary(len, term) => read_unary_lim(self, len, term),
105 UintCodeType::Unary012 => read_unary_lim(self, 2, 0),
106 UintCodeType::Unary210 => read_unary210(self),
107 UintCodeType::Golomb(m) => read_golomb(self, m),
108 UintCodeType::Rice(k) => read_rice(self, k),
109 UintCodeType::Gamma => read_gamma(self),
110 UintCodeType::GammaP => read_gammap(self),
113 #[allow(unused_variables)]
114 fn read_code_signed(&mut self, t: IntCodeType) -> BitReaderResult<i32> {
117 IntCodeType::Golomb(m) => read_golomb(self, m)?,
118 IntCodeType::Rice(k) => read_rice(self, k)?,
119 IntCodeType::Gamma => read_gamma(self)?,
120 IntCodeType::GammaP => read_gammap(self)?,
123 IntCodeType::Golomb(m) => Ok(uval_to_sval0mp(uval)),
124 IntCodeType::Rice(k) => Ok(uval_to_sval0mp(uval)),
125 IntCodeType::Gamma => Ok(uval_to_sval0pm(uval)),
126 IntCodeType::GammaP => Ok(uval_to_sval0pm(uval)),
134 use crate::io::bitreader::*;
138 const GDATA: [u8; 6] = [0b000_001_01, 0b0_0110_011, 0b1_1000_100, 0b1_1010_101, 0b10_10111_1, 0b1000_0000];
140 let mut br = BitReader::new(src, src.len(), BitReaderMode::BE);
142 assert_eq!(br.read_code(UintCodeType::Golomb(5)).unwrap(), i);