Huffman coding: Difference between revisions
Content deleted Content added
m {{out}} |
Updated D entry |
||
Line 1,656:
=={{header|D}}==
<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 heap = sf.map!(s => tuple(s[1], [tuple(s[0], "")]))
.array.heapify!q{b < a};
Line 1,669:
heap.insert(tuple(lo[0] + hi[0], lo[1] ~ hi[1]));
}
return heap.front[1].schwartzSort!q{ tuple(a[1].length, a[0]) };
}
void main() /*@safe*/ {
foreach (const p; s.dup.sort().release.group.encode)
writefln("'%s' %s", p[]);
}</lang>
Line 1,697:
'c' 111110
'd' 111111</pre>
=={{header|Eiffel}}==
Adapted C# solution.
|