From 73dec6720eeac98bae5283ae4e64d0331cf77ef6 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 22 May 2021 23:35:03 +0300 Subject: [PATCH] [BZip2] Combine MTF and RLE steps in compression --- Sources/BZip2/BZip2+Compress.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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..