All properties in huffman trees are now constant

This commit is contained in:
Timofey Solomko
2018-03-31 13:50:50 +03:00
parent d4b728125b
commit 9293210d35
2 changed files with 10 additions and 8 deletions
+5 -4
View File
@@ -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 {
+5 -4
View File
@@ -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) {