@@ -4,7 +4,7 @@ use std::{io, ops::Range};
44
55use bitstream_io:: BitRead ;
66
7- use crate :: { BitStore , Model } ;
7+ use crate :: { common , BitStore , Model } ;
88
99// this algorithm is derived from this article - https://marknelson.us/posts/2014/10/19/data-compression-with-arithmetic-coding.html
1010
@@ -185,9 +185,7 @@ where
185185 B : BitStore ,
186186 R : BitRead ,
187187{
188- precision : u32 ,
189- low : B ,
190- high : B ,
188+ state : common:: State < B > ,
191189 input : R ,
192190 x : B ,
193191 uninitialised : bool ,
@@ -200,54 +198,41 @@ where
200198{
201199 /// todo
202200 pub fn new ( precision : u32 , input : R ) -> Self {
203- let low = B :: ZERO ;
204- let high = B :: ONE << precision;
201+ let state = common:: State :: new ( precision) ;
205202 let x = B :: ZERO ;
206203
207204 Self {
208- precision,
209- low,
210- high,
205+ state,
211206 input,
212207 x,
213208 uninitialised : true ,
214209 }
215210 }
216211
217- fn half ( & self ) -> B {
218- B :: ONE << ( self . precision - 1 )
219- }
220-
221- fn quarter ( & self ) -> B {
222- B :: ONE << ( self . precision - 2 )
223- }
224-
225- fn three_quarter ( & self ) -> B {
226- self . half ( ) + self . quarter ( )
227- }
228-
229212 fn normalise ( & mut self ) -> io:: Result < ( ) > {
230- while self . high < self . half ( ) || self . low >= self . half ( ) {
231- if self . high < self . half ( ) {
232- self . high <<= 1 ;
233- self . low <<= 1 ;
213+ while self . state . high < self . state . half ( ) || self . state . low >= self . state . half ( ) {
214+ if self . state . high < self . state . half ( ) {
215+ self . state . high <<= 1 ;
216+ self . state . low <<= 1 ;
234217 self . x <<= 1 ;
235218 } else {
236219 // self.low >= self.half()
237- self . low = ( self . low - self . half ( ) ) << 1 ;
238- self . high = ( self . high - self . half ( ) ) << 1 ;
239- self . x = ( self . x - self . half ( ) ) << 1 ;
220+ self . state . low = ( self . state . low - self . state . half ( ) ) << 1 ;
221+ self . state . high = ( self . state . high - self . state . half ( ) ) << 1 ;
222+ self . x = ( self . x - self . state . half ( ) ) << 1 ;
240223 }
241224
242225 if self . input . next_bit ( ) ? == Some ( true ) {
243226 self . x += B :: ONE ;
244227 }
245228 }
246229
247- while self . low >= self . quarter ( ) && self . high < ( self . three_quarter ( ) ) {
248- self . low = ( self . low - self . quarter ( ) ) << 1 ;
249- self . high = ( self . high - self . quarter ( ) ) << 1 ;
250- self . x = ( self . x - self . quarter ( ) ) << 1 ;
230+ while self . state . low >= self . state . quarter ( )
231+ && self . state . high < ( self . state . three_quarter ( ) )
232+ {
233+ self . state . low = ( self . state . low - self . state . quarter ( ) ) << 1 ;
234+ self . state . high = ( self . state . high - self . state . quarter ( ) ) << 1 ;
235+ self . x = ( self . x - self . state . quarter ( ) ) << 1 ;
251236
252237 if self . input . next_bit ( ) ? == Some ( true ) {
253238 self . x += B :: ONE ;
@@ -258,21 +243,17 @@ where
258243 }
259244
260245 fn scale ( & mut self , p : Range < B > , denominator : B ) -> io:: Result < ( ) > {
261- let range = self . high - self . low + B :: ONE ;
262-
263- self . high = self . low + ( range * p. end ) / denominator - B :: ONE ;
264- self . low += ( range * p. start ) / denominator;
265-
246+ self . state . scale ( p, denominator) ;
266247 self . normalise ( )
267248 }
268249
269250 fn value ( & self , denominator : B ) -> B {
270- let range = self . high - self . low + B :: ONE ;
271- ( ( self . x - self . low + B :: ONE ) * denominator - B :: ONE ) / range
251+ let range = self . state . high - self . state . low + B :: ONE ;
252+ ( ( self . x - self . state . low + B :: ONE ) * denominator - B :: ONE ) / range
272253 }
273254
274255 fn fill ( & mut self ) -> io:: Result < ( ) > {
275- for _ in 0 ..self . precision {
256+ for _ in 0 ..self . state . precision {
276257 self . x <<= 1 ;
277258 if self . input . next_bit ( ) ? == Some ( true ) {
278259 self . x += B :: ONE ;
0 commit comments