Add a huffman_decoder.consts field.

This commit is contained in:
Nigel Tao committed 2017-07-21 11:48:15 +10:00
1 parent 34198f3de5
commit 02cd837d9d
6 files changed
+47 -33

No files matched your search

+11 -6
View File
@@ -117,7 +117,7 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
this.bits = bits
this.n_bits = n_bits
this.init_huffman?(n_codes:19)
this.init_huffman?(which:0, n_codes:19)
// TODO: implement.
this.wip0 = hlit
@@ -132,20 +132,24 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
// TODO: skip generating PUFFS_COROUTINE_SUSPENSION_POINTs for this function,
// and its callers, as it can only return errors, not suspensions.
pri func decoder.init_huffman?(n_codes u32[..289])() {
var counts[16] u32[..289]
pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() {
var i u32
while i < 16 {
this.huff[in.which].counts[i] = 0
i += 1
}
i = 0
while i < in.n_codes {
assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes)
// TODO: this if should be unnecessary. Have some way to assert that,
// for all j, counts[j] <= i, and thus counts[j]++ will not overflow.
if counts[this.code_lengths[i]] >= 289 {
if this.huff[in.which].counts[this.code_lengths[i]] >= 289 {
return error "inconsistent Huffman table"
}
counts[this.code_lengths[i]] += 1
this.huff[in.which].counts[this.code_lengths[i]] += 1
i += 1
}
if counts[0] == in.n_codes {
if (this.huff[in.which].counts[0] as u32) == in.n_codes {
// There are no codes.
return error "inconsistent Huffman table"
}
@@ -160,4 +164,5 @@ pri func decoder.init_huffman?(n_codes u32[..289])() {
}
pri struct huffman_decoder(
counts[16] u16[..289],
)