Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/LZW-compression/Dylan/lzw-compression.dylan
Normal file
43
Task/LZW-compression/Dylan/lzw-compression.dylan
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
Module: LZW
|
||||
Synopsis: LZW implementation for Rosetta code
|
||||
|
||||
define method output(n :: <integer>)
|
||||
format-out("%d ", n);
|
||||
end;
|
||||
|
||||
define method contains?(dict, var)
|
||||
let x = element(dict, var, default: #f);
|
||||
x ~= #f;
|
||||
end;
|
||||
|
||||
define method byte->string(c)
|
||||
add("", as(<character>, c));
|
||||
end;
|
||||
|
||||
define method compress(input :: <string>) => <vector>;
|
||||
let result = make(<vector>);
|
||||
let dict = make(<string-table>);
|
||||
for (x from 0 to 255)
|
||||
dict[byte->string(x)] := x;
|
||||
end;
|
||||
|
||||
let next-code = 256;
|
||||
let cur-seq = "";
|
||||
for (c in input)
|
||||
let wc = add(cur-seq, c);
|
||||
if (contains?(dict, wc))
|
||||
cur-seq := wc;
|
||||
else
|
||||
result := add(result, dict[cur-seq]);
|
||||
dict[wc] := next-code;
|
||||
next-code := next-code + 1;
|
||||
cur-seq := add("", c);
|
||||
end
|
||||
end;
|
||||
unless (empty?(cur-seq))
|
||||
result := add(result, dict[cur-seq]);
|
||||
end;
|
||||
result
|
||||
end;
|
||||
|
||||
format-out("%=\n", compress("TOBEORNOTTOBEORTOBEORNOT"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue