Huffman coding: Difference between revisions

Content added Content deleted
(Updated D entry)
(Updated D entry)
Line 1,658: Line 1,658:
<lang d>import std.stdio, std.algorithm, std.typecons, std.container, std.array;
<lang d>import std.stdio, std.algorithm, std.typecons, std.container, std.array;


auto encode(T)(Group!("a == b", T[]) sf) /*pure nothrow @safe*/ {
auto encode(alias eq, R)(Group!(eq, R) sf) /*pure nothrow @safe*/ {
auto heap = sf.map!(s => tuple(s[1], [tuple(s[0], "")]))
auto heap = sf.map!(s => tuple(s[1], [tuple(s[0], "")]))
.array.heapify!q{b < a};
.array.heapify!q{b < a};
Line 1,674: Line 1,674:
void main() /*@safe*/ {
void main() /*@safe*/ {
immutable s = "this is an example for huffman encoding"d;
immutable s = "this is an example for huffman encoding"d;
foreach (const p; s.dup.sort().release.group.encode)
foreach (const p; s.dup.sort().group.encode)
writefln("'%s' %s", p[]);
writefln("'%s' %s", p[]);
}</lang>
}</lang>