Advertisement
Alakazard12

Huffman Coding

Apr 26th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. local function getL(tebr)
  2.   local teb = {}
  3.   for i,v in pairs(tebr) do
  4.     teb[i] = v
  5.   end
  6.  
  7.   local lowm
  8.   local low2m
  9.   local low
  10.   local low2
  11.   for i,v in pairs(teb) do
  12.     if not low then
  13.       low = i
  14.       lowm = v[1]
  15.     elseif v[1] < lowm then
  16.       low = i
  17.       lowm = v[1]
  18.     end
  19.   end
  20.  
  21.   teb[low] = nil
  22.  
  23.   for i,v in pairs(teb) do
  24.     if not low2 then
  25.       low2 = i
  26.       low2m = v[1]
  27.     elseif v[1] < low2m then
  28.       low2 = i
  29.       low2m = v[1]
  30.     end
  31.   end
  32.  
  33.  
  34.   if low2 < low then
  35.     local buf = low2
  36.     low2 = low
  37.     low = buf
  38.   end
  39.  
  40.   return low, low2
  41. end
  42.  
  43. local function it(com, cur, tab, hur)
  44.   for i,v in pairs(tab) do
  45.     if type(v[2]) == "table" then
  46.       it(com .. i - 1, cur, v[2], hur + 1)
  47.     else
  48.       cur[v[2]] = com .. i - 1
  49.       -- print(string.rep(" ", hur) .. v[2] .. " = " .. cur[v[2]])
  50.     end
  51.   end
  52.   return com, cur
  53. end
  54.  
  55. local function encode(text)
  56.   local newt = {}
  57.  
  58.   local freq = {}
  59.   for i = 1, #text do
  60.     local char = string.byte(text:sub(i, i))
  61.     if not freq[char] then
  62.       freq[char] = 0
  63.     end
  64.     freq[char] = freq[char] + 1
  65.   end
  66.  
  67.   for i,v in pairs(freq) do
  68.     table.insert(newt, {v, i})
  69.   end
  70.  
  71.   while #newt > 1 do
  72.     local l1, l2 = getL(newt)
  73.    
  74.     local l1m = newt[l1]
  75.     local l2m = newt[l2]
  76.    
  77.     table.remove(newt, l1)
  78.     table.remove(newt, l2 - 1)
  79.     table.insert(newt, {l1m[1] + l2m[1], {l1m, l2m}})
  80.   end
  81.  
  82.   local d ,t = it("", {}, newt[1][2], 0)
  83.  
  84.   local new = ""
  85.   for i = 1, #text do
  86.     new = new .. t[text:sub(i, i):byte()]
  87.   end
  88.  
  89.   return new, t
  90. end
  91.  
  92. local function decode(text, dic)
  93.   local new = ""
  94.  
  95.   for i = 1, #text do
  96.     local got = false
  97.     for m,v in pairs(dic) do
  98.       if text:sub(1, #v) == v then
  99.         new = new .. string.char(m)
  100.         text = text:sub(#v + 1)
  101.         break
  102.       end
  103.     end
  104.   end
  105.   return new
  106. end
  107.  
  108. local tArgs = {...}
  109.  
  110. if tArgs[1] == "e" then
  111.   local file = fs.open(tArgs[2], "r")
  112.   local dat = file.readAll()
  113.   file.close()
  114.  
  115.   local t, d = encode(dat)
  116.  
  117.   local ard = textutils.serialize(d)
  118.  
  119.   local bw = ""
  120.   local gof = {}
  121.   for i = 1, #t do
  122.     table.insert(gof, 1, tonumber(t:sub(i, i)))
  123.     if #gof == 8 then
  124.       local nby = 0
  125.       for m,s in pairs(gof) do
  126.         nby = nby + s * (2^(m - 1))
  127.       end
  128.       bw = bw .. string.char(nby)
  129.       gof = {}
  130.     end
  131.   end
  132.  
  133.   local nby = 0
  134.   for i = 1, 8-#gof do
  135.     table.insert(gof, 1, 0)
  136.   end
  137.   for m,s in pairs(gof) do
  138.     nby = nby + s * (2^(m - 1))
  139.   end
  140.   if nby ~= 0 then
  141.     bw = bw .. string.char(nby)
  142.   end
  143.  
  144.   file = fs.open(tArgs[3], "wb")
  145.   local tow = #ard .. "a" .. ard .. #t .. "b" .. bw
  146.   for i = 1, #tow do
  147.     file.write(tow:sub(i, i):byte())
  148.   end
  149.   file.close()
  150. elseif tArgs[1] == "d" then
  151.   local file = fs.open(tArgs[2], "rb")
  152.   local newc = ""
  153.   while true do
  154.     local by = file.read()
  155.     if by == nil then
  156.       break
  157.     else
  158.       newc = newc .. string.char(by)
  159.     end
  160.   end
  161.  
  162.   local num = ""
  163.   for i = 1, #newc do
  164.     if newc:sub(i, i) == "a" then
  165.       break
  166.     else
  167.       num = num .. newc:sub(i, i)
  168.     end
  169.   end
  170.  
  171.   num = tonumber(num)
  172.   local nom = #tostring(num) + 1
  173.   local toc = ""
  174.   for i = 1 + nom, num + nom do
  175.     toc = toc .. newc:sub(i, i)
  176.   end
  177.  
  178.   nom = nom + #toc
  179.  
  180.   local num2 = ""
  181.  
  182.   for i = nom + 1, #newc do
  183.     if newc:sub(i, i) == "b" then
  184.       break
  185.     else
  186.       num2 = num2 .. newc:sub(i, i)
  187.     end
  188.   end
  189.   num2 = tonumber(num2)
  190.   nom = nom + #tostring(num2) + 1
  191.  
  192.   local newl = ""
  193.  
  194.   for i = 1 + nom, #newc do
  195.     local by = newc:sub(i, i):byte()
  196.     local newr = ""
  197.     for i = 8, 1, -1 do
  198.       if by >= 2^(i - 1) then
  199.         newr = newr .. "1"
  200.         by = by - 2^(i - 1)
  201.       else
  202.         newr = newr .. "0"
  203.       end
  204.     end
  205.     newl = newl .. newr
  206.   end
  207.  
  208.   newl = newl:sub(1, num2)
  209.   local d = decode(newl, textutils.unserialize(toc))
  210.   print(d)
  211.  
  212.   file.close()
  213. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement