LZW compression: Difference between revisions
Added Arturo implementation
(Updated to work with Nim 1.4: removed test to "nil". Several other minor changes.) |
Drkameleon (talk | contribs) (Added Arturo implementation) |
||
Line 209:
end;
end Test;</lang>
=={{header|Arturo}}==
<lang rebol>compress: function [str][
dict: #[]
loop 0..255 'i -> dict\[to :string to :char i]: i
w: ""
result: new []
loop str 'c [
wc: w ++ c
if? key? dict wc -> w: wc
else [
'result ++ dict\[w]
dict\[wc]: size dict
w: to :string c
]
]
if 0 < size w -> 'result ++ dict\[w]
return result
]
decompress: function [compressed][
dict: #[]
arr: new compressed
loop 0..255 'i -> dict\[i]: to :string to :char i
w: dict\[first arr]
remove 'arr .index 0
result: w
loop arr 'k [
entry: ""
if? key? dict k -> entry: dict\[k]
else [
if? k = size dict -> entry: w ++ first w
else -> panic ~"Error with compressed: |k|"
]
'result ++ entry
dict\[size dict]: w ++ first entry
w: entry
]
return result
]
compressed: compress "TOBEORNOTTOBEORTOBEORNOT"
print "Compressed:"
print compressed
print ""
decompressed: decompress compressed
print "Decompressed:"
print decompressed</lang>
{{out}}
<pre>Compressed:
84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263
Decompressed:
TOBEORNOTOBEORNOTOOBEORTOOBEORNOT</pre>
=={{header|BaCon}}==
|