mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
Fix HuffmanTree init crash when zero codeLength is encountered
This commit is contained in:
@@ -17,7 +17,7 @@ class DecodingHuffmanTree {
|
||||
self.bitReader = bitReader
|
||||
|
||||
// Sort `lengths` array to calculate canonical Huffman code.
|
||||
let sortedLengths = lengths.sorted { (left: HuffmanLength, right: HuffmanLength) -> Bool in
|
||||
let sortedLengths = lengths.filter { $0.codeLength > 0 }.sorted { (left: HuffmanLength, right: HuffmanLength) -> Bool in
|
||||
if left.codeLength == right.codeLength {
|
||||
return left.symbol < right.symbol
|
||||
} else {
|
||||
|
||||
@@ -16,7 +16,7 @@ class EncodingHuffmanTree {
|
||||
self.bitWriter = bitWriter
|
||||
|
||||
// Sort `lengths` array to calculate canonical Huffman code.
|
||||
let sortedLengths = lengths.sorted { (left: HuffmanLength, right: HuffmanLength) -> Bool in
|
||||
let sortedLengths = lengths.filter { $0.codeLength > 0 }.sorted { (left: HuffmanLength, right: HuffmanLength) -> Bool in
|
||||
if left.codeLength == right.codeLength {
|
||||
return left.symbol < right.symbol
|
||||
} else {
|
||||
@@ -146,7 +146,7 @@ class EncodingHuffmanTree {
|
||||
|
||||
func bitSize(for stats: [(Int, Int)]) -> Int {
|
||||
var totalSize = 0
|
||||
for (symbol, count) in stats {
|
||||
for (symbol, count) in stats where count > 0 {
|
||||
guard symbol < self.codingIndices.count
|
||||
else { fatalError("Symbol is not found.") }
|
||||
let codingIndex = self.codingIndices[symbol]
|
||||
|
||||
Reference in New Issue
Block a user