~/bend-docscommunity

bits.bend checks

raw source on the hub · import bend-kit-bits@0.1.0.0/bits.bend as Bits

Bit readers and writers over Bytes, LSB-first (DEFLATE) or MSB-first (JPEG), and canonical Huffman decode tables. Source: https://github.com/paymog/bend-kit/tree/main/bits

2 imports
import Base
import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes

Types

type Reader source · line 43 · raw

Type

A reader owns the buffer of len bytes; pos is the next bit.

type Writer source · line 112 · raw

Type

A writer's buf has 2^dep words, all 0 past bit pos.

type Huff source · line 182 · raw

Type

A canonical Huffman table: entry i of 2^max is (sym << 5) | len for the code that the first max stream bits i start with, or 0 when no code does. msb is the stream's bit order. ponytail: one level of 2^max entries; a two-level table if building it dominates short blocks.

type Code source · line 186 · raw

Data

One symbol and the length of its code.

Definitions

def mask source · line 10 · raw

@+n:U32 -> U32

def win.lsb.join source · line 13 · raw

@+s:U32 -> @+lo:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)

def win.msb.join source · line 17 · raw

@+s:U32 -> @+lo:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)

def win.lsb.at source · line 21 · raw

@+p:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)

def win.msb.at source · line 25 · raw

@+p:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)

def win source · line 31 · raw

@msb:Bool -> @a:Array<U32> -> @+p:U32 -> Pair(Array<U32>, U32)

The 32 stream bits from bit p, in stream order: first bit lowest LSB-first, highest MSB-first. Bits past the buffer's last word wrap to its first, so callers mask what lies past the end.

def field source · line 39 · raw

@+msb:Bool -> @+n:U32 -> @+w:U32 -> U32

The first n (at most 32) bits of a window as a number, first stream bit most significant MSB-first.

def Reader.new source · line 46 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Reader

def Reader.finish source · line 51 · raw

@r:Reader -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

The buffer back, and the bit position.

def Reader.left source · line 55 · raw

@r:Reader -> Pair(Reader, U32)

def read.got source · line 59 · raw

@+msb:Bool -> @+len:U32 -> @+pos:U32 -> @+n:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Reader, Maybe<&2, U32>)

def read.if source · line 63 · raw

@ok:Bool -> @+msb:Bool -> @+len:U32 -> @buf:Array<U32> -> @+pos:U32 -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)

def read source · line 70 · raw

@+msb:Bool -> @r:Reader -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)

def Reader.lsb source · line 76 · raw

@r:Reader -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)

The next n bits (0 to 32) as a number, and the reader past them. None, with the reader unchanged, when n > 32 or fewer than n bits are left.

def Reader.msb source · line 79 · raw

@r:Reader -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)

def peek.got source · line 83 · raw

@+msb:Bool -> @+len:U32 -> @+pos:U32 -> @+n:U32 -> @+avail:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Reader, U32)

Bits past the end read as 0: the low avail bits stay LSB-first, the high ones MSB-first.

def peek source · line 88 · raw

@+msb:Bool -> @r:Reader -> @+n0:U32 -> Pair(Reader, U32)

def Reader.peek.lsb source · line 94 · raw

@r:Reader -> @+n:U32 -> Pair(Reader, U32)

The next n bits (at most 32) without moving. Bits past the end read as 0.

def Reader.peek.msb source · line 97 · raw

@r:Reader -> @+n:U32 -> Pair(Reader, U32)

def Reader.skip source · line 101 · raw

@r:Reader -> @+n:U32 -> Pair(Reader, Bool)

Moves n bits on. False, with the reader unchanged, when fewer than n are left.

def Reader.align source · line 107 · raw

@r:Reader -> Reader

Moves to the next byte boundary, as a DEFLATE stored block or a JPEG marker needs.

def Writer.new source · line 115 · raw

Writer

def room source · line 119 · raw

@full:Bool -> @w:Writer -> Writer

A write adds at most 32 bits, so one doubling always makes room.

def or.of source · line 127 · raw

@+k:U32 -> @+x:U32 -> @r:Pair(Array<U32>, U32) -> Array<U32>

def or.if source · line 131 · raw

@some:Bool -> @a:Array<U32> -> @+k:U32 -> @+x:U32 -> Array<U32>

def parts source · line 139 · raw

@msb:Bool -> @+s:U32 -> @+n:U32 -> @+v:U32 -> Pair(U32, U32)

The parts of an n-bit v that land in word k and word k + 1, as stored words.

def write.put source · line 148 · raw

@+k:U32 -> @x:Pair(U32, U32) -> @a:Array<U32> -> Array<U32>

def write.at source · line 152 · raw

@+msb:Bool -> @w:Writer -> @+n:U32 -> @+v:U32 -> Writer

def write source · line 156 · raw

@+msb:Bool -> @w:Writer -> @+n0:U32 -> @+v:U32 -> Writer

def Writer.lsb source · line 163 · raw

@w:Writer -> @+n:U32 -> @+v:U32 -> Writer

Appends the low n bits of v (n is cut to 32). LSB-first sends v's lowest bit first; MSB-first sends bit n - 1 first.

def Writer.msb source · line 166 · raw

@w:Writer -> @+n:U32 -> @+v:U32 -> Writer

def Writer.align source · line 170 · raw

@w:Writer -> Writer

Pads with 0 bits to the next byte boundary.

def Writer.finish source · line 175 · raw

@w:Writer -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

The bytes written; 0 bits fill the last byte.

def lens.max source · line 189 · raw

@xs:List<&2, Code> -> @+m:U32 -> U32

def count.of source · line 196 · raw

@+len:U32 -> @r:Pair(Array<U32>, U32) -> Array<U32>

def count.if source · line 200 · raw

@zero:Bool -> @c:Array<U32> -> @+len:U32 -> Array<U32>

def count source · line 207 · raw

@xs:List<&2, Code> -> @c:Array<U32> -> Array<U32>

def next.go source · line 216 · raw

@k:Nat -> @+l:U32 -> @+code:U32 -> @+left:U32 -> @+ok:Bool -> @n:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Bool)

RFC 1951 §3.2.2: next[l] = (next[l-1] + count[l-1]) << 1. r holds count[l-1]; left counts the codes of length l - 1 still free, and a negative left means the lengths over-subscribe.

def fill source · line 226 · raw

@f:Nat -> @t:Array<U32> -> @+i:U32 -> @+step:U32 -> @+e:U32 -> Array<U32>

def rev source · line 233 · raw

@k:Nat -> @+v:U32 -> @+acc:U32 -> U32

def assign.put source · line 241 · raw

@+msb:Bool -> @+max:U32 -> @+sym:U32 -> @+len:U32 -> @r:Pair(Array<U32>, U32) -> @t:Array<U32> -> Pair(Array<U32>, Array<U32>)

MSB-first, a code fills a run of consecutive entries; LSB-first, its bits reversed fill every 2^len-th.

def assign.one source · line 248 · raw

@zero:Bool -> @+msb:Bool -> @+max:U32 -> @+sym:U32 -> @+len:U32 -> @st:Pair(Array<U32>, Array<U32>) -> Pair(Array<U32>, Array<U32>)

def assign source · line 256 · raw

@xs:List<&2, Code> -> @+msb:Bool -> @+max:U32 -> @st:Pair(Array<U32>, Array<U32>) -> Array<U32>

def build.next source · line 264 · raw

@+msb:Bool -> @+max:U32 -> @xs:List<&2, Code> -> @r:Pair(Array<U32>, Bool) -> Maybe<&1, Huff>

def build.short source · line 272 · raw

@ok:Bool -> @+msb:Bool -> @+max:U32 -> @+xs:List<&2, Code> -> Maybe<&1, Huff>

def Huff.from_codes source · line 282 · raw

@+msb:Bool -> @+xs:List<&2, Code> -> Maybe<&1, Huff>

A table from codes. Codes go shortest first, ties in list order; length 0 means no code. None when a length exceeds 16 or the lengths over-subscribe. An incomplete set is allowed, and decode answers None for its unused codes. Symbols must be below 2^27.

def codes source · line 286 · raw

@xs:List<&2, U32> -> @+i:U32 -> List<&2, Code>

def Huff.from_lens source · line 294 · raw

@+msb:Bool -> @+lens:List<&2, U32> -> Maybe<&1, Huff>

Symbol i has length lens[i], as DEFLATE sends them (RFC 1951 §3.2.2).

def decode.hit source · line 299 · raw

@+msb:Bool -> @+max:U32 -> @+len:U32 -> @+pos:U32 -> @+left:U32 -> @buf:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))

Bits past the end may be garbage in w: a code's entry repeats over every suffix, so only its own len bits pick it, and len <= left keeps them inside the stream.

def decode.look source · line 305 · raw

@+msb:Bool -> @+max:U32 -> @+len:U32 -> @+pos:U32 -> @t:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))

def Huff.decode source · line 311 · raw

@h:Huff -> @r:Reader -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))

One symbol, and the reader past its code. None, with the reader unchanged, when the next bits start no code or the input ends inside one.