diff --git a/Sources/Common/DecodingHuffmanTree.swift b/Sources/Common/DecodingHuffmanTree.swift index 2a9ff316..c5052f2d 100644 --- a/Sources/Common/DecodingHuffmanTree.swift +++ b/Sources/Common/DecodingHuffmanTree.swift @@ -8,9 +8,9 @@ import BitByteData class DecodingHuffmanTree { - private var bitReader: BitReader + private let bitReader: BitReader - private var tree: [Int] + private let tree: [Int] private let leafCount: Int /// `lengths` don't have to be properly sorted, but there must not be any 0 code lengths. @@ -22,7 +22,7 @@ class DecodingHuffmanTree { // Calculate maximum amount of leaves possible in a tree. self.leafCount = 1 << (sortedLengths.last!.codeLength + 1) - self.tree = Array(repeating: -1, count: leafCount) + var tree = Array(repeating: -1, count: leafCount) // Calculates symbols for each length in 'sortedLengths' array and put them in the tree. var loopBits = -1 @@ -46,8 +46,9 @@ class DecodingHuffmanTree { index = bit == 0 ? 2 * index + 1 : 2 * index + 2 treeCode >>= 1 } - self.tree[index] = length.symbol + tree[index] = length.symbol } + self.tree = tree } func findNextSymbol() -> Int { diff --git a/Sources/Common/EncodingHuffmanTree.swift b/Sources/Common/EncodingHuffmanTree.swift index 3ad5f1ca..f7bd5627 100644 --- a/Sources/Common/EncodingHuffmanTree.swift +++ b/Sources/Common/EncodingHuffmanTree.swift @@ -8,9 +8,9 @@ import BitByteData class EncodingHuffmanTree { - private var bitWriter: BitWriter + private let bitWriter: BitWriter - private var codingIndices: [[Int]] + private let codingIndices: [[Int]] /// `lengths` don't have to be properly sorted, but there must not be any 0 code lengths. /// If `reverseCodes` is true, then bit order of tree codes will be reversed. Necessary for Deflate. @@ -20,7 +20,7 @@ class EncodingHuffmanTree { // Sort `lengths` array to calculate canonical Huffman code. let sortedLengths = lengths.sorted() - self.codingIndices = Array(repeating: [-1, -1], count: sortedLengths.count) + var codingIndices = Array(repeating: [-1, -1], count: sortedLengths.count) // Calculates symbols for each length in 'sortedLengths' array and put them in the tree. var loopBits = -1 @@ -36,8 +36,9 @@ class EncodingHuffmanTree { } // Then we reverse bit order of the symbol, if necessary. let treeCode = reverseCodes ? symbol.reversed(bits: loopBits) : symbol - self.codingIndices[length.symbol] = [treeCode, bits] + codingIndices[length.symbol] = [treeCode, bits] } + self.codingIndices = codingIndices } func code(symbol: Int) {