Anonymous user
Run-length encoding: Difference between revisions
Updated two D versions
(Updated two of the four D versions) |
(Updated two D versions) |
||
Line 724:
=={{header|D}}==
===Short Functional Version===▼
alias group encode;▼
}▼
void main() {▼
auto s = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";▼
assert(decode(encode(s)) == s);▼
}</lang>▼
===Basic Imperative Version===
<lang d>import std.stdio, std.array, std.conv;
Line 774 ⟶ 787:
writeln("Encoded: ", encoded);
assert(txt == decode(encoded));
▲}</lang>
▲===Short Functional Version===
▲<lang d>import std.stdio, std.algorithm, std.conv, std.array;
▲alias group encode;
▲string decode(Range)(Range r) {
▲ return join(map!(t => replicate(""~cast(char)t[0], t[1]))(r));
▲}
▲void main() {
▲ auto s = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
▲ assert(decode(encode(s)) == s);
}</lang>
===UTF String Version===
D's native string is utf-encoded. This version work for utf string. This code use a [[Variable-length_quantity|Variable-length Quantity]] [[Variable-length_quantity#D|module]].
<lang d>import std.stdio, std.conv, std.utf, std.string;
import
struct RLE { // for utf string
ubyte[] encoded
RLE encode(const string s) {
validate(s)
encoded.length = 0
if (s.length == 0) return this
string last
VLQ count
for (int i = 0
auto k = s.stride(i)
auto ucode = cast(string)s[i .. i+k]
if (i == 0) last = ucode
if (ucode == last)
count++
else {
encoded ~= count.toVLQ ~ cast(ubyte[])
last = ucode
count = 1
}
i += k
}
encoded ~= VLQ(count).toVLQ ~ cast(ubyte[])
return this
}
int opApply(int delegate(ref ulong c, ref string u) dg) {
VLQ count
string ucode
for (int i =
if
if (k == 0xff)
} ▼
return 0;
string toString() {
string res
foreach (ref i, s ; this)
if (indexOf(
res ~=
else
res ~=
return res
}
string decode() {
string res
foreach (ref i, s
res ~= repeat(s, cast(uint)i)
return res
}
}
void main() {
RLE r
auto s = "尋尋覓覓冷冷清清淒淒慘慘戚戚\nWWWWWWWWWWWWBWWWWWWWWWWW" ~
"WBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW\n" ~
"11#222##333"
auto f = File("display.txt", "w")
f.writeln(s)
r.encode(s)
f.writefln("-----\n%s\n-----\n%s", r, r.decode)
auto sEncoded = RLE.init.encode(s).encoded ;
assert(s == RLE(sEncoded).decode , "Not work")
}</lang>
|