|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local utils = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function utils.binToDec(bin)
|
|
|
local dec = 0
|
|
|
for _, bValue in ipairs(bin) do
|
|
|
dec = bit.bor(bit.lshift(dec, 1), bValue)
|
|
|
end
|
|
|
return dec
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function utils.decToBin(dec, padding)
|
|
|
local bits = {}
|
|
|
local bitsCount = padding or math.max(1, select(2, math.frexp(dec)))
|
|
|
|
|
|
for b = bitsCount, 1, -1 do
|
|
|
bits[b] = math.fmod(dec, 2)
|
|
|
dec = math.floor((dec - bits[b]) / 2)
|
|
|
end
|
|
|
|
|
|
return bits
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function utils.tablesConcat(t1, ...)
|
|
|
for _, tn in ipairs({...}) do
|
|
|
for i = 1, #tn do
|
|
|
t1[#t1 + 1] = tn[i]
|
|
|
end
|
|
|
end
|
|
|
return t1
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function utils.tableLength(t)
|
|
|
local count = 0
|
|
|
for _ in pairs(t) do count = count + 1 end
|
|
|
return count
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function utils.getParity(n)
|
|
|
local parity = false
|
|
|
while n > 0 do
|
|
|
parity = not parity
|
|
|
n = bit.band(n, n - 1)
|
|
|
end
|
|
|
return parity
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function utils.padBinary(bin, bits)
|
|
|
local padded = bin
|
|
|
if #padded > math.abs(bits) then
|
|
|
if bits > 0 then
|
|
|
padded = {unpack(padded, #padded - math.abs(bits) + 1)}
|
|
|
else
|
|
|
padded = {unpack(padded, 1, math.abs(bits))}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
while #padded < math.abs(bits) do
|
|
|
if bits > 0 then
|
|
|
table.insert(padded, 1, 0)
|
|
|
else
|
|
|
padded[#padded + 1] = 0
|
|
|
end
|
|
|
end
|
|
|
|
|
|
return padded
|
|
|
end
|
|
|
|
|
|
function utils.switch(t)
|
|
|
t.case = function(self, x)
|
|
|
local f = self[x] or self.default
|
|
|
if f then
|
|
|
if type(f) == "function" then
|
|
|
f(x, self)
|
|
|
else
|
|
|
error("case " .. tostring(x) .. " not a function")
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
return t
|
|
|
end
|
|
|
|
|
|
return utils
|
|
|
|