Fix HuffmanTree init crash when zero codeLength is encountered

This commit is contained in:
Timofey Solomko
2017-09-03 14:16:59 +03:00
parent a357837065
commit 49228a049f
2 changed files with 3 additions and 3 deletions
+1 -1
View File
@@ -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 {
+2 -2
View File
@@ -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]