Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,23 @@
fcn buildHuffman(text){ //-->(encode dictionary, decode dictionary)
ft:=Dictionary();
foreach c in (text){ ft[c]=ft.find(c,0)+1 } // leafs w/count
// build the tree, which is a list of lists of ...
tree:=ft.pump(List,fcn([(c,cnt)]){ //-->L( (cnt, ((sym,code))), ...)
L(cnt, L(L(c,"")))
}).copy(); // make it writable
while(tree.len()>1){ // fake up a [lame] priorty queue
tree=tree.sort(fcn(a,b){ a[0]>b[0] }); //prioritize high to low
a,b:=tree.pop(-2,2); //remove 2 least frequent symbols
mc:=fcn(n,c){ n[1] = c + n[1]; }; //(sym,code),"0"|"1"
a[1].apply2(mc,"0"); b[1].apply2(mc,"1"); // mc(a[1],"0")
tree.append( L(a[0]+b[0],a[1].extend(b[1])) ); //(a,b)-->new node
}//-->L(L(39, L( L(" ","000"),L("e","0010"),L("a","0011") ...
tree=tree[0][1].pump(List,fcn(i){ // flatten rather than traverse
if(T.isType(i))return(Void.Recurse,i,self.fcn); i });
encodeTable:=tree.toDictionary(); // symbol:Huffman code
decodeTable:=encodeTable.pump(Dictionary(),"reverse"); // code:symbol
return(encodeTable,decodeTable);
}

View file

@ -0,0 +1,8 @@
fcn encode(text,table){ text.pump(String,table.get) }
fcn decode(bits,table){ // this is a horrible decoder, for testing only
w:=bits.walker(); sink:=Sink(String);
try{ s:=""; while(1){
s+=w.next(); if(c:=table.find(s)) { sink.write(c); s=""; }
}}catch(TheEnd){}
sink.close();
}

View file

@ -0,0 +1,10 @@
text:="this is an example for huffman encoding";
encodeTable,decodeTable := buildHuffman(text);
encodeTable.pump(Console.println,fcn(kv){"%s : %s".fmt(kv.xplode())});
e:=encode(text,encodeTable);
"Encode %d characters (%d bits) to %d bits (%d bytes):"
.fmt(text.len(),text.len()*8,e.len(),(e.len()+7)/8).println();
println(e);
0'|Bits decoded to: "%s"|.fmt(decode(e,decodeTable)).println();