// Copyright (c) 2017 Timofey Solomko
// Licensed under MIT License
//
// See LICENSE for license information

import Foundation

class BurrowsWheeler {

    static func transform(bytes: [UInt8]) -> ([UInt8], Int) {
        let doubleBytes = bytes + bytes
        let suffixArray = SuffixArray.make(from: doubleBytes.map { $0.toInt() }, with: 256)
        var bwt = [UInt8]()
        var pointer = 0
        for i in 1..<suffixArray.count {
            if suffixArray[i] < bytes.count {
                if suffixArray[i] > 0 {
                    bwt.append(bytes[suffixArray[i] - 1])
                } else {
                    bwt.append(bytes.last!)
                }
            } else if suffixArray[i] == bytes.count {
                pointer = (i - 1) / 2
            }
        }
        return (bwt, pointer)
    }

}

// Copyright (c) 2017 Timofey Solomko
// Licensed under MIT License
//
// See LICENSE for license information

import Foundation

class BurrowsWheeler {

    static func transform(bytes: [UInt8]) -> ([UInt8], Int) {
        let suffixArray = SuffixArray.make(from: bytes, with: 256)
        var bwt = [UInt8]()
        var pointer = 0
        for i in 1..<suffixArray.count {
            if suffixArray[i] > 0 {
                bwt.append(bytes[suffixArray[i] - 1])
            } else {
                bwt.append(bytes.last!)
                pointer = i - 1
            }
        }
        return (bwt, pointer)
    }

}

--- 
+++ 
@@ -8,15 +8,19 @@
 class BurrowsWheeler {
 
     static func transform(bytes: [UInt8]) -> ([UInt8], Int) {
-        let suffixArray = SuffixArray.make(from: bytes, with: 256)
+        let doubleBytes = bytes + bytes
+        let suffixArray = SuffixArray.make(from: doubleBytes.map { $0.toInt() }, with: 256)
         var bwt = [UInt8]()
         var pointer = 0
         for i in 1..<suffixArray.count {
-            if suffixArray[i] > 0 {
-                bwt.append(bytes[suffixArray[i] - 1])
-            } else {
-                bwt.append(bytes.last!)
-                pointer = i - 1
+            if suffixArray[i] < bytes.count {
+                if suffixArray[i] > 0 {
+                    bwt.append(bytes[suffixArray[i] - 1])
+                } else {
+                    bwt.append(bytes.last!)
+                }
+            } else if suffixArray[i] == bytes.count {
+                pointer = (i - 1) / 2
             }
         }
         return (bwt, pointer)
Fix bw transformation from suffix array
