Cyan4973 / FiniteStateEntropy

New generation entropy codecs : Finite State Entropy and Huff0
BSD 2-Clause "Simplified" License
1.32k stars 141 forks source link

Why Huffman code in reverse order? #110

Open huhu0823 opened 1 year ago

huhu0823 commented 1 year ago

I'm very interested in compression.Huffman coding can be performed sequentially,I can't understand why huffman code is executed in reverse order in the code.

Cyan4973 commented 1 year ago

FSE needs to encode and decode in reverse directions. There are multiple conventions possible, this implementation selects to write the compressed bitstream forward, and read backward. This results in a bitstream format, which is built around the assumption of forward writing, and backward reading.

For simplicity, the Huffman implementation in this repository employs the same bitstream. Since the compressed bitstream will be read backward, but we still want the decoded symbols to be written forward, it follows that they must be written in the compressed bitstream in reverse order.

Obviously, other conventions could have been used. And it's also possible to read + write huffman forward, which is probably easier to follow. This would "just" require another bitstream implementation.

huhu0823 commented 1 year ago

FSE needs to encode and decode in reverse directions. There are multiple conventions possible, this implementation selects to write the compressed bitstream forward, and read backward. This results in a bitstream format, which is built around the assumption of forward writing, and backward reading.

For simplicity, the Huffman implementation in this repository employs the same bitstream. Since the compressed bitstream will be read backward, but we still want the decoded symbols to be written forward, it follows that they must be written in the compressed bitstream in reverse order.

Obviously, other conventions could have been used. And it's also possible to read + write huffman forward, which is probably easier to follow. This would "just" require another bitstream implementation.

Thank you for your reply!Your answer made me suddenly understand.