def next.go

bend-kit-bits@0.1.0.1 · bits.bend:214

import bend-kit-bits@0.1.0.1/bits.bend as Bits
import Base

214# RFC 1951 §3.2.2: next[l] = (next[l-1] + count[l-1]) << 1. r holds count[l-1]; left counts
215# the codes of length l - 1 still free, and a negative left means the lengths over-subscribe.
216def next.go(k: Nat, +l: U32, +code: U32, +left: U32, +ok: Bool, n: Array<U32>, r: Array<U32> & U32) -> Array<U32> & Bool:
217  match k:
218    case 0n:
219      (c, v) = r
220      (n, ok)
221    case 1n+p:
222      (c, +v) = r
223      +nx = ((code + v) << 1n : U32)
224      next.go(p, (l + 1 : U32), nx, ((left - v) * 2 : U32), Bool.and(ok, U32.is_le(v, left)), Array.set(U32, n, l, nx), Array.get(U32, c, l))

Uses (14)

Used by (1)