From f97456aab81410909beb482567b908ae2913f97d Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 12 Dec 2023 14:25:39 +0500 Subject: [PATCH] bit table gen algo fix --- src/output.zig | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/output.zig b/src/output.zig index c1dac8e..35cb73a 100644 --- a/src/output.zig +++ b/src/output.zig @@ -209,7 +209,6 @@ inline fn gen_codes(huff: *Huffman, huffman_meta: HuffmanMeta) !void { si += 1; } } - for (0..total_k) |x| { try huff.put(huffman_meta.huffval[x], HuffCode{ .n_bits = huffsize[x], @@ -244,17 +243,15 @@ inline fn gen_huffman(freqs: []u32) HuffmanMeta { bits[codesizes[i]] += 1; } } - var i: usize = 32; while (i > 16) { if (bits[i] > 0) { var j = i - 2; - while (bits[j] <= 0) : (j -= 1) { - bits[i] -= 2; - bits[i - 1] += 1; - bits[j + 1] += 2; - bits[j] -= 1; - } + while (bits[j] <= 0) : (j -= 1) {} + bits[i] -= 2; + bits[i - 1] += 1; + bits[j + 1] += 2; + bits[j] -= 1; } else { i -= 1; }