Jump to content

LZW compression: Difference between revisions

Line 1,751:
=={{header|EMal}}==
<syntaxhighlight lang="emal">
type LzwCompression
fun compress = List by text uncompressed
Map dictionary = text%int[].with(256, <int i|text%int(chr(i) => i))
List resultoutput = int[]
text w
text working ← Text.empty
List result = int[]
Map dictionarysymbolTable = text%int[].with(256, <int i|text%int(chr(i) => i))
for each text c in uncompressed
text wcaugmented = wworking + c
if dictionarysymbolTable.has(wcaugmented)
wworking = wcaugmented
else
dictionarysymbolTable.insert(wcaugmented, dictionarysymbolTable.length)
result.append(dictionary[w])
int i ← symbolTable[working]
dictionary.insert(wc, dictionary.length)
output.append(i)
w = c
working ← c
end
end
end
if not wworking.isEmpty() do result.append(dictionary[w]) end
int i ← symbolTable[working]
return result
output.append(i)
end
end
fun decompress = text by List compressed
return resultoutput
Map dictionary = int%text[].with(256, <int i|int%text(i => chr(i)))
end
text w = chr(compressed[0])
fun decompress = text by List compressed
text result = *w
Map dictionarysymbolTable = int%text[].with(256, <int i|int%text(i => chr(i)))
for each int k in compressed.extract(1)
text wworking = chr(symbolTable[compressed[0])]
text entry
text output ← *working
if dictionary.has(k)
for each int ki in compressed.extract(1)
entry = dictionary[k]
text entrys
else if k == dictionary.length
if dictionarysymbolTable.has(ki)
entry = w + w[0]
s ← symbolTable[i]
else if ki ==æ dictionarysymbolTable.length # cScSc problem
s ← working + working[0]
else
error(65, "Error decompressing")
end
resultoutput.append(entrys)
dictionarysymbolTable.insert(dictionarysymbolTable.length, wworking + entrys[0])
wworking = entrys
end
return resultoutput
end
List compressed = compress("TOBEORNOTTOBEORTOBEORNOT")
226

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.