September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
57
Task/LZW-compression/Phix/lzw-compression.phix
Normal file
57
Task/LZW-compression/Phix/lzw-compression.phix
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
function compress(string uncompressed)
|
||||
integer dict = new_dict()
|
||||
sequence result = {}
|
||||
integer dictSize = 255, c
|
||||
string word = ""
|
||||
for i=0 to 255 do
|
||||
setd(""&i,i,dict)
|
||||
end for
|
||||
for i=1 to length(uncompressed) do
|
||||
c = uncompressed[i]
|
||||
if getd_index(word&c,dict) then
|
||||
word &= c
|
||||
else
|
||||
result &= getd(word,dict)
|
||||
dictSize += 1
|
||||
setd(word&c,dictSize,dict)
|
||||
word = ""&c
|
||||
end if
|
||||
end for
|
||||
if word!="" then
|
||||
result &= getd(word,dict)
|
||||
end if
|
||||
destroy_dict(dict)
|
||||
return result
|
||||
end function
|
||||
|
||||
function decompress(sequence compressed)
|
||||
integer dict = new_dict()
|
||||
integer dictSize = 255, k, ki
|
||||
string dent = "", result = "", word = ""
|
||||
for i=0 to 255 do
|
||||
setd(i,""&i,dict)
|
||||
end for
|
||||
for i=1 to length(compressed) do
|
||||
k = compressed[i]
|
||||
ki = getd_index(k,dict)
|
||||
if ki then
|
||||
dent = getd_by_index(ki,dict)
|
||||
elsif k=dictSize then
|
||||
dent = word&word[1]
|
||||
else
|
||||
return {NULL,i}
|
||||
end if
|
||||
result &= dent
|
||||
setd(dictSize,word&dent[1],dict)
|
||||
dictSize += 1
|
||||
word = dent
|
||||
end for
|
||||
destroy_dict(dict)
|
||||
return result
|
||||
end function
|
||||
|
||||
constant example = "TOBEORNOTTOBEORTOBEORNOT"
|
||||
sequence com = compress(example)
|
||||
--?com
|
||||
pp(com)
|
||||
?decompress(com)
|
||||
Loading…
Add table
Add a link
Reference in a new issue