diff --git a/Sources/BZip2/BZip2+Compress.swift b/Sources/BZip2/BZip2+Compress.swift index 96af5b3d..d46d764e 100644 --- a/Sources/BZip2/BZip2+Compress.swift +++ b/Sources/BZip2/BZip2+Compress.swift @@ -79,10 +79,8 @@ extension BZip2: CompressionAlgorithm { (out, pointer) = BurrowsWheeler.transform(bytes: out) let usedBytes = Set(out).sorted() - out = mtf(out, characters: usedBytes) - var maxSymbol = 0 - (out, maxSymbol) = rleOfMtf(out) + (out, maxSymbol) = mtfRle(out, characters: usedBytes) // First, we analyze data and create Huffman trees and selectors. // Then we will perform encoding itself. @@ -258,12 +256,16 @@ extension BZip2: CompressionAlgorithm { return out } - private static func rleOfMtf(_ array: [Int]) -> ([Int], Int) { + private static func mtfRle(_ array: [Int], characters: [Int]) -> ([Int], Int) { var out = [Int]() + /// Mutable copy of `characters`. + var dictionary = characters var lengthOfZerosRun = 0 var maxSymbol = 1 for i in 0..