def huff.insert
gralha-bytes@0.1.0.1 · bytes.bend:145
import gralha-bytes@0.1.0.1/bytes.bend as Bytes
import Base
import gralha-text-scan@0.1.0.1/scan.bend as U
145def huff.insert(+node: Huffman, +depth: Nat, +codeword: U32, +symbol: U32) -> Huffman:
146 match node:
147 case HuffBad{}: HuffBad{}
148 case HuffLeaf{old}: HuffBad{}
149 case HuffEmpty{}:
150 match depth:
151 case 0n: HuffLeaf{symbol}
152 case 1n+d: huff.insert(HuffNode{HuffEmpty{}, HuffEmpty{}}, 1n+d, codeword, symbol)
153 case HuffNode{zero, one}:
154 match depth:
155 case 0n: HuffBad{}
156 case 1n+d:
157 +dd = d
158 U.branch(Huffman, U32.is_eq(U32.and(codeword, U32.shln(1, dd)), 0), _ => HuffNode{huff.insert(zero, dd, codeword, symbol), one}, _ => HuffNode{zero, huff.insert(one, dd, codeword, symbol)})