Move the lengths bootstrapping function into the namespace of the CodeLength

This commit is contained in:
Timofey Solomko
2019-03-12 22:29:53 +03:00
parent 7f25dbf6e7
commit 1df768c09f
3 changed files with 21 additions and 21 deletions
+19
View File
@@ -10,6 +10,25 @@ struct CodeLength: Equatable {
let symbol: Int
let codeLength: Int
static func lengths(from bootstrap: [(symbol: Int, codeLength: Int)]) -> [CodeLength] {
// Fills the 'lengths' array with pairs of (symbol, codeLength) from a 'bootstrap'.
var lengths = [CodeLength]()
var start = bootstrap[0].symbol
var bits = bootstrap[0].codeLength
for pair in bootstrap[1..<bootstrap.count] {
let finish = pair.symbol
let endbits = pair.codeLength
if bits > 0 {
for i in start..<finish {
lengths.append(CodeLength(symbol: i, codeLength: bits))
}
}
start = finish
bits = endbits
}
return lengths
}
}
extension CodeLength: Comparable {
+2 -2
View File
@@ -9,8 +9,8 @@ extension Deflate {
struct Constants {
// Bootstraps for Static Huffman trees (first element in tuple is code, second is number of bits).
static let staticHuffmanBootstrap = Deflate.lengths(from: [(0, 8), (144, 9), (256, 7), (280, 8), (288, -1)])
static let staticHuffmanDistancesBootstrap = Deflate.lengths(from: [(0, 5), (32, -1)])
static let staticHuffmanBootstrap = CodeLength.lengths(from: [(0, 8), (144, 9), (256, 7), (280, 8), (288, -1)])
static let staticHuffmanDistancesBootstrap = CodeLength.lengths(from: [(0, 5), (32, -1)])
static let codeLengthOrders: [Int] =
[16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]
-19
View File
@@ -8,25 +8,6 @@ import Foundation
// Deflate specific functions for generation of HuffmanLength arrays from different inputs.
extension Deflate {
static func lengths(from bootstrap: [(symbol: Int, codeLength: Int)]) -> [CodeLength] {
// Fills the 'lengths' array with pairs of (symbol, codeLength) from a 'bootstrap'.
var lengths = [CodeLength]()
var start = bootstrap[0].symbol
var bits = bootstrap[0].codeLength
for pair in bootstrap[1..<bootstrap.count] {
let finish = pair.symbol
let endbits = pair.codeLength
if bits > 0 {
for i in start..<finish {
lengths.append(CodeLength(symbol: i, codeLength: bits))
}
}
start = finish
bits = endbits
}
return lengths
}
/// - Note: Skips zero codeLengths.
static func lengths(from orderedCodeLengths: [Int]) -> [CodeLength] {
var lengths = [CodeLength]()