Anonymous user
LZW compression: Difference between revisions
D code converted to D2
m (→{{header|Java}}: WW Java 1.5, better syntax highlighting) |
(D code converted to D2) |
||
Line 671:
=={{header|D}}==
▲<lang d>import std.stdio: writefln;
int[] compress(string uncompressed) {
int[string] dictionary;
dictionary["" ~ cast(char)i] = i;
Line 690 ⟶ 687:
else {
result ~= dictionary[w];
w = "" ~ c;
}
}
if (w.length)
result ~= dictionary[w];
Line 703 ⟶ 696:
}
string decompress(int[] compressed) {
string[int] dictionary;
dictionary[i] = "" ~ cast(char)i;
Line 718 ⟶ 708:
if (k in dictionary)
entry = dictionary[k];
else if (k ==
entry = w ~ w[0];
else
Line 724 ⟶ 714:
result ~= entry;
w = entry;
}
Line 735 ⟶ 722:
void main() {
auto compressed = compress("TOBEORNOTTOBEORTOBEORNOT");
auto decompressed = decompress(compressed);
}</lang>
Output:
<lang d>[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT</lang>
|