File tree 1 file changed +6
-6
lines changed
Lempel_Ziv_Welch/Lua/Yonaba
1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change 4
4
local function lzw_encode (str )
5
5
local w = ' '
6
6
local result = {}
7
- local dict_size = 255
7
+ local dict_size = 256
8
8
9
9
-- Builds the dictionnary
10
10
local dict = {}
11
- for i = 0 , dict_size do
11
+ for i = 0 , dict_size - 1 do
12
12
dict [string.char (i )] = i
13
13
end
14
14
@@ -23,8 +23,8 @@ local function lzw_encode(str)
23
23
else
24
24
-- Add the match to the dictionary
25
25
table.insert (result , dict [w ])
26
- i = i + 1
27
26
dict [wc ] = i
27
+ i = i + 1
28
28
w = char
29
29
end
30
30
end
@@ -35,11 +35,11 @@ local function lzw_encode(str)
35
35
end
36
36
37
37
local function lzw_decode (str )
38
- local dict_size = 255
38
+ local dict_size = 256
39
39
40
40
-- Builds the dictionary
41
41
local dict = {}
42
- for i = 0 , dict_size do
42
+ for i = 0 , dict_size - 1 do
43
43
dict [i ] = string.char (i )
44
44
end
45
45
@@ -56,8 +56,8 @@ local function lzw_decode(str)
56
56
return nil -- No match found, decoding error
57
57
end
58
58
result = result .. entry
59
- dict_size = dict_size + 1
60
59
dict [dict_size ] = w .. entry :sub (1 ,1 )
60
+ dict_size = dict_size + 1
61
61
w = entry
62
62
end
63
63
return result
You can’t perform that action at this time.
0 commit comments