LZW compression: Difference between revisions
→{{header|EMal}}
Line 1,751:
=={{header|EMal}}==
<syntaxhighlight lang="emal">
type LzwCompression
fun compress
Map dictionary = text%int[].with(256, <int i|text%int(chr(i) => i))▼
text working ← Text.empty
▲ List result = int[]
for each text c in uncompressed
text
if
else
int i ← symbolTable[working]
▲ dictionary.insert(wc, dictionary.length)
output.append(i)
working ← c
end ▼
end
if not
int i ← symbolTable[working]
return result▼
output.append(i)
▲end
fun decompress = text by List compressed▼
Map dictionary = int%text[].with(256, <int i|int%text(i => chr(i)))▼
end
text w = chr(compressed[0])▼
for each int k in compressed.extract(1)▼
text entry▼
text output ← *working
if dictionary.has(k)▼
else if k == dictionary.length▼
s ← symbolTable[i]
s ← working + working[0]
else
error(65, "Error decompressing")
end
end
return
end
List compressed = compress("TOBEORNOTTOBEORTOBEORNOT")
|