Skip to content

Commit a29bff0

Browse files
committed
Added naive decode function
Prettified source
1 parent 312af32 commit a29bff0

1 file changed

Lines changed: 47 additions & 13 deletions

File tree

  • Burrows_Wheeler_Transform/Lua/Yonaba
Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,58 @@
11
--Burrows-Wheeler Transform
22

3-
function encode(word)
4-
local t={word}
5-
local pos = 1
3+
--Burrows-Wheeler Transform
64

7-
local cat = string.len(word)
8-
for i=cat-1,1,-1 do
9-
local tmp = string.sub(word,1,i)
10-
local comp = string.sub(word,i+1,cat)
11-
local str = comp..tmp
125

13-
table.insert(t,str)
14-
end
15-
table.sort(t)
6+
function encode(word)
7+
local t={word}
8+
local pos = 1
9+
local cat = string.len(word)
10+
11+
for i=cat-1,1,-1 do
12+
local tmp = string.sub(word,1,i)
13+
local comp = string.sub(word,i+1,cat)
14+
local str = comp..tmp
15+
table.insert(t,str)
16+
end
17+
18+
table.sort(t)
1619

17-
local retStr = ""
20+
local retStr = ""
1821
for k in ipairs(t) do
1922
if t[k]==word then pos = k end
2023
retStr = retStr..string.sub(t[k],string.len(t[k]),string.len(t[k]))
2124
end
2225

23-
return retStr
26+
return retStr
2427
end
28+
29+
30+
function decode(encword)
31+
--print('decode',encword)
32+
local size = string.len(encword)
33+
local offset = tonumber(string.sub(encword,1,1))
34+
local encword = string.sub(encword,2,size)
35+
36+
local t={}
37+
for n=0,string.len(encword),1 do
38+
for i=1,string.len(encword),1 do
39+
t[i] = not t[i] and '' or string.sub(encword,i,i)..t[i]
40+
end
41+
table.sort(t)
42+
end
43+
44+
return (t[offset])
45+
end
46+
47+
--Sample Test
48+
local str = "SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES"
49+
local out = ''
50+
--for w in string.gfind(str, "%w+") do
51+
local enc = encode(str)
52+
local dec = decode(enc)
53+
out = out .. '.'..enc
54+
print("\nChaine originale: "..str)
55+
print("Transformée B-W: "..enc)
56+
print("Transformée Décodée: "..dec)
57+
--end
58+
print(out)

0 commit comments

Comments
 (0)