diff --git a/Sources/Deflate.swift b/Sources/Deflate.swift index ecefc546..0708dcb2 100644 --- a/Sources/Deflate.swift +++ b/Sources/Deflate.swift @@ -213,7 +213,7 @@ public class Deflate: DecompressionAlgorithm { throw DeflateError.UnknownBlockType } - // End the cycle if it was the last block + // End the cycle if it was the last block. if isLastBit == 1 { break } } diff --git a/Sources/HuffmanLength.swift b/Sources/HuffmanLength.swift index a82c9356..13f578cb 100644 --- a/Sources/HuffmanLength.swift +++ b/Sources/HuffmanLength.swift @@ -11,10 +11,9 @@ import Foundation struct HuffmanLength: Comparable, CustomStringConvertible { let code: Int let bits: Int - var symbol: Int? = nil var description: String { - return "(code: \(code), bits: \(bits), symbol: \(symbol)" + return "(code: \(code), bits: \(bits)" } static func < (left: HuffmanLength, right: HuffmanLength) -> Bool { diff --git a/Sources/HuffmanTree.swift b/Sources/HuffmanTree.swift index 78682d15..e2efbfd3 100644 --- a/Sources/HuffmanTree.swift +++ b/Sources/HuffmanTree.swift @@ -33,7 +33,7 @@ class HuffmanTree: CustomStringConvertible { private let leafCount: Int init(bootstrap: [Array]) { - // Fills the 'lengths' array with numerous HuffmanLengths from a 'bootstrap' + // Fills the 'lengths' array with numerous HuffmanLengths from a 'bootstrap'. var lengths: [HuffmanLength] = [] var start = bootstrap[0][0] var bits = bootstrap[0][1] @@ -42,16 +42,16 @@ class HuffmanTree: CustomStringConvertible { let endbits = pair[1] if bits > 0 { lengths.append(contentsOf: - (start.. Int { - // Auxiliarly function, which generates reversed order of bits in a number + // Auxiliarly function, which generates reversed order of bits in a number. var a = 1 << 0 var b = 1 << (bits - 1) var z = 0 @@ -64,34 +64,30 @@ class HuffmanTree: CustomStringConvertible { return z } - // Calculates symbols for each length in 'lengths' array + // Calculate maximum amount of leaves possible in a tree. + self.leafCount = Int(pow(Double(2), Double(lengths.last!.bits + 1))) + // Create a tree (array, actually) with all leaves equal nil. + self.tree = Array(repeating: nil, count: leafCount) + + // Calculates symbols for each length in 'lengths' array and put them in the tree. var loopBits = -1 var symbol = -1 - for index in 0..>= 1 + treeCode >>= 1 } self.tree[index] = length.code }