From b1e3176f9f46b6126a93437764eef6914d7f23e5 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 13 Mar 2021 23:40:10 +0200 Subject: [PATCH] LibCompress: Replace goto with simple recursion in DeflateCompressor This is just a bit easier on the eyes :^) --- Userland/Libraries/LibCompress/Deflate.cpp | 7 ++----- Userland/Libraries/LibCompress/Deflate.h | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibCompress/Deflate.cpp b/Userland/Libraries/LibCompress/Deflate.cpp index b2bb4d0475..cf6115956c 100644 --- a/Userland/Libraries/LibCompress/Deflate.cpp +++ b/Userland/Libraries/LibCompress/Deflate.cpp @@ -578,15 +578,13 @@ ALWAYS_INLINE u8 DeflateCompressor::distance_to_base(u16 distance) } template -void DeflateCompressor::generate_huffman_lengths(Array& lengths, const Array& frequencies, size_t max_bit_length) +void DeflateCompressor::generate_huffman_lengths(Array& lengths, const Array& frequencies, size_t max_bit_length, u16 frequency_cap) { VERIFY((1u << max_bit_length) >= Size); u16 heap_keys[Size]; // Used for O(n) heap construction u16 heap_values[Size]; u16 huffman_links[Size * 2 + 1] = { 0 }; - u16 frequency_cap = UINT16_MAX; -try_again: size_t non_zero_freqs = 0; for (size_t i = 0; i < Size; i++) { auto frequency = frequencies[i]; @@ -644,8 +642,7 @@ try_again: if (bit_length > max_bit_length) { VERIFY(frequency_cap != 1); - frequency_cap /= 2; - goto try_again; // FIXME: gotos are ugly, but i cant think of a good way to flatten this + return generate_huffman_lengths(lengths, frequencies, max_bit_length, frequency_cap / 2); } lengths[i] = bit_length; diff --git a/Userland/Libraries/LibCompress/Deflate.h b/Userland/Libraries/LibCompress/Deflate.h index 05d0f3cfe0..b933f60fcb 100644 --- a/Userland/Libraries/LibCompress/Deflate.h +++ b/Userland/Libraries/LibCompress/Deflate.h @@ -188,7 +188,7 @@ private: }; static u8 distance_to_base(u16 distance); template - static void generate_huffman_lengths(Array& lengths, const Array& frequencies, size_t max_bit_length); + static void generate_huffman_lengths(Array& lengths, const Array& frequencies, size_t max_bit_length, u16 frequency_cap = UINT16_MAX); size_t huffman_block_length(const Array& literal_bit_lengths, const Array& distance_bit_lengths); void write_huffman(const CanonicalCode& literal_code, const CanonicalCode& distance_code); static size_t encode_huffman_lengths(const Array& lengths, size_t lengths_count, Array& encoded_lengths);