Paste: bitreader (original)

Author: mrjbq7
Mode: factor
Date: Thu, 11 Dec 2014 01:24:42
Plain Text |
USING: kernel locals accessors math sequences math.bitwise ;

TUPLE: bitreader bytearray curpos curbit ;
: <bitreader> ( bytearray -- bitreader ) 0 0 bitreader boa ;

: advance ( reader -- reader ) 0 >>curbit [ 1 + ] change-curpos ;

: check-advance ( reader -- reader ) dup curbit>> 8 = [ advance ] when ;

:: readsinglevalue ( bits reader -- result ) reader curpos>> reader bytearray>> nth 
                                             reader curbit>> dup bits 1 - + swap bit-range
                                             reader [ bits + ] change-curbit check-advance drop ;

: bitsleft ( reader -- result ) curbit>> 8 swap - ;

DEFER: readbits

:: readmultivalue ( bits reader -- result ) reader bitsleft :> bitsextra
                                            bitsextra reader readsinglevalue
                                            bits bitsextra - reader readbits
                                            bitsextra shift + ;

:: readbits ( bits reader -- result ) reader curbit>> bits + 8 < [ bits reader readsinglevalue ] [ bits reader readmultivalue ] if ;

Annotation: first cleanup

Author: mrjbq7
Mode: factor
Date: Thu, 11 Dec 2014 01:24:57
Plain Text |
TUPLE: bitreader bytearray curpos curbit ;

: <bitreader> ( bytearray -- bitreader ) 0 0 bitreader boa ;

: advance ( reader -- reader )
    0 >>curbit [ 1 + ] change-curpos ;

: check-advance ( reader -- reader )
    dup curbit>> 8 = [ advance ] when ;

:: readsinglevalue ( bits reader -- result )
    reader curpos>> reader bytearray>> nth
    reader curbit>> dup bits 1 - + swap bit-range
    reader [ bits + ] change-curbit check-advance drop ;

: bitsleft ( reader -- result )
    curbit>> 8 swap - ;

DEFER: readbits

:: readmultivalue ( bits reader -- result )
    reader bitsleft :> bitsextra
    bitsextra reader readsinglevalue
    bits bitsextra - reader readbits
    bitsextra shift + ;

:: readbits ( bits reader -- result )
    reader curbit>> bits + 8 < [
        bits reader readsinglevalue
    ] [
        bits reader readmultivalue
    ] if ;

Annotation: second cleanup (add dashes and longer names)

Author: mrjbq7
Mode: factor
Date: Thu, 11 Dec 2014 01:27:42
Plain Text |
USING: kernel locals accessors math sequences math.bitwise ;

TUPLE: bit-reader byte-array current-pos current-bit ;

: <bit-reader> ( bytearray -- bitreader ) 0 0 bit-reader boa ;

: advance ( reader -- reader )
    0 >>current-bit [ 1 + ] change-current-pos ;

: check-advance ( reader -- reader )
    dup current-bit>> 8 = [ advance ] when ;

:: read-single-value ( bits reader -- result )
    reader current-pos>> reader byte-array>> nth
    reader current-bit>> dup bits 1 - + swap bit-range
    reader [ bits + ] change-current-bit check-advance drop ;

: bits-left ( reader -- result )
    current-bit>> 8 swap - ;

DEFER: read-bits

:: read-multi-value ( bits reader -- result )
    reader bits-left :> extra-bits
    extra-bits reader read-single-value
    bits extra-bits - reader read-bits
    extra-bits shift + ;

:: read-bits ( bits reader -- result )
    reader current-bit>> bits + 8 < [
        bits reader read-single-value
    ] [
        bits reader read-multi-value
    ] if ;

Annotation: slight cleanup, don't need locals in read-bits

Author: mrjbq7
Mode: factor
Date: Thu, 11 Dec 2014 01:35:28
Plain Text |
USING: kernel locals accessors math sequences math.bitwise ;

TUPLE: bit-reader byte-array current-pos current-bit ;

: <bit-reader> ( bytearray -- bitreader ) 0 0 bit-reader boa ;

: advance ( reader -- reader )
    0 >>current-bit [ 1 + ] change-current-pos ;

: ?advance ( reader -- reader )
    dup current-bit>> 8 = [ advance ] when ;

:: read-single-value ( bits reader -- result )
    reader current-pos>> reader byte-array>> nth
    reader current-bit>> [ bits 1 - + ] keep bit-range
    reader [ bits + ] change-current-bit ?advance drop ;

: bits-left ( reader -- n )
    current-bit>> 8 swap - ;

DEFER: read-bits

:: read-multi-value ( bits reader -- result )
    reader bits-left :> extra-bits
    extra-bits reader read-single-value
    bits extra-bits - reader read-bits
    extra-bits shift + ;

: read-bits ( bits reader -- result )
    2dup current-bit>> + 8 < [
        read-single-value
    ] [
        read-multi-value
    ] if ;

New Annotation

Summary:
Author:
Mode:
Body: