mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
Move the lengths bootstrapping function into the namespace of the CodeLength
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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]()
|
||||
|
||||
Reference in New Issue
Block a user