<?xml version="1.0" encoding="windows-1252"?>
<node id="59718" title="Huffman encoder" created="2001-02-20 16:37:16" updated="2005-08-12 15:19:54">
<type id="1588">
obfuscated</type>
<author id="21732">
BooK</author>
<data>
<field name="doctext">
&lt;P&gt;[Falkkin] is working on a &lt;i&gt;Java&lt;/i&gt; Huffman encoder...&lt;/P&gt;
&lt;P&gt;Moved only by the will to serve, I propose him an
example of what he could do if he did that in Perl... &lt;tt&gt;:-)&lt;/tt&gt;&lt;/P&gt;
&lt;code&gt;$"=$/=$_;map{$0{$_}++}split//,$_=&lt;&gt;;@0=map{[$_,$0{$_},$_]}keys%0;
$/=1+1;until(@0==1){@0=sort{$$b[1]&lt;=&gt;$$a[1]}@0;$t=pop@0;$p=pop@0;
push@0,[[$$t[0],$$p[0]],$$t[1]+$$p[1],$$t[$/].$$p[$/]];map{map{$t
{$_}=$;%$/.$t{$_}}split//;$;++}$$t[$/],$$p[$/]}s/(.)/$t{$1}/gs;sub
_{my$t=pop;if(ref$t){my@t=map{if(!ref$$t[$_]){1,unpack"b8",$$t[$_]}
else{0,""}}0,1;"@t[0,$/,1]"._($$t[0])._($$t[1])."$t[-1]"}}print
pack"sB*",length,_($0[0][0]).$_
&lt;/code&gt;
&lt;P&gt;&lt;font size="-1"&gt;(This was one of my "helpers" for OPC5)&lt;/font&gt;&lt;BR&gt;
Corresponding [id://59888] to be posted soon.&lt;/P&gt;
</field>
</data>
</node>
