1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

LibCompress: Replace goto with simple recursion in DeflateCompressor

This is just a bit easier on the eyes :^)
This commit is contained in:
Idan Horowitz 2021-03-13 23:40:10 +02:00 committed by Andreas Kling
parent 1b7b503bae
commit b1e3176f9f
2 changed files with 3 additions and 6 deletions

View file

@ -578,15 +578,13 @@ ALWAYS_INLINE u8 DeflateCompressor::distance_to_base(u16 distance)
} }
template<size_t Size> template<size_t Size>
void DeflateCompressor::generate_huffman_lengths(Array<u8, Size>& lengths, const Array<u16, Size>& frequencies, size_t max_bit_length) void DeflateCompressor::generate_huffman_lengths(Array<u8, Size>& lengths, const Array<u16, Size>& frequencies, size_t max_bit_length, u16 frequency_cap)
{ {
VERIFY((1u << max_bit_length) >= Size); VERIFY((1u << max_bit_length) >= Size);
u16 heap_keys[Size]; // Used for O(n) heap construction u16 heap_keys[Size]; // Used for O(n) heap construction
u16 heap_values[Size]; u16 heap_values[Size];
u16 huffman_links[Size * 2 + 1] = { 0 }; u16 huffman_links[Size * 2 + 1] = { 0 };
u16 frequency_cap = UINT16_MAX;
try_again:
size_t non_zero_freqs = 0; size_t non_zero_freqs = 0;
for (size_t i = 0; i < Size; i++) { for (size_t i = 0; i < Size; i++) {
auto frequency = frequencies[i]; auto frequency = frequencies[i];
@ -644,8 +642,7 @@ try_again:
if (bit_length > max_bit_length) { if (bit_length > max_bit_length) {
VERIFY(frequency_cap != 1); VERIFY(frequency_cap != 1);
frequency_cap /= 2; return generate_huffman_lengths(lengths, frequencies, max_bit_length, frequency_cap / 2);
goto try_again; // FIXME: gotos are ugly, but i cant think of a good way to flatten this
} }
lengths[i] = bit_length; lengths[i] = bit_length;

View file

@ -188,7 +188,7 @@ private:
}; };
static u8 distance_to_base(u16 distance); static u8 distance_to_base(u16 distance);
template<size_t Size> template<size_t Size>
static void generate_huffman_lengths(Array<u8, Size>& lengths, const Array<u16, Size>& frequencies, size_t max_bit_length); static void generate_huffman_lengths(Array<u8, Size>& lengths, const Array<u16, Size>& frequencies, size_t max_bit_length, u16 frequency_cap = UINT16_MAX);
size_t huffman_block_length(const Array<u8, max_huffman_literals>& literal_bit_lengths, const Array<u8, max_huffman_distances>& distance_bit_lengths); size_t huffman_block_length(const Array<u8, max_huffman_literals>& literal_bit_lengths, const Array<u8, max_huffman_distances>& distance_bit_lengths);
void write_huffman(const CanonicalCode& literal_code, const CanonicalCode& distance_code); void write_huffman(const CanonicalCode& literal_code, const CanonicalCode& distance_code);
static size_t encode_huffman_lengths(const Array<u8, max_huffman_literals + max_huffman_distances>& lengths, size_t lengths_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths); static size_t encode_huffman_lengths(const Array<u8, max_huffman_literals + max_huffman_distances>& lengths, size_t lengths_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths);