 
         func reverse(bits: Int, in symbol: Int) -> Int {
             // Auxiliarly subfunction, which computes reversed order of bits in a number
-            // This is some weird magic
             var a = 1 << 0
             var b = 1 << (bits - 1)
             var z = 0

//

import Foundation

class HuffmanTreeNode: CustomStringConvertible {
    let node: HuffmanLength?
    var left: HuffmanTreeNode? = nil
    var right: HuffmanTreeNode? = nil

    var description: String {
        return "value: \(self.node)\n\tleft: \(left)\n\tright: \(right)"
    }

    init(_ node: HuffmanLength? = nil) {
        self.node = node
    }
}

class HuffmanTable: CustomStringConvertible {


            self.lengths[index].reversedSymbol = reverse(bits: loopBits, in: symbol)
        }

        let leafCount = Int(pow(Double(2), Double(self.lengths.last!.bits + 1)))
        self.reversedTree = Array(repeating: nil, count: leafCount)
        self.tree = Array(repeating: nil, count: leafCount)

        for length in self.lengths {
            var revSymbol = length.reversedSymbol!
            var symbol = length.symbol!

            let bits = length.bits

            var revIndex = 0
            var index = 0

            for _ in 0..<bits {
                let revBit = revSymbol & 1
                let bit = symbol & 1

                revIndex = revBit == 0 ? 2 * revIndex + 1 : 2 * revIndex + 2
                index = bit == 0 ? 2 * index + 1 : 2 * index + 2

                revSymbol >>= 1
                symbol >>= 1
            }
            self.reversedTree[revIndex] = length
            self.tree[index] = length
        }
    }

    convenience init(lengthsToOrder: [Int]) {
