RedHuffman huffman coding
"to use fewer bits for more frequently occurring character"
see http://www.cs.duke.edu/csed/poop/huff/info/
works with Integer, Float and Symbol. can also take a String instead of an Array.
see also: RedBase64 RedLZ77 RedLZ78 RedLZSS RedLZW RedRLE
*compress(string)
compresses an array into a string of binary numbers.
creates a tree and a dict as well.
*decompress(string)
decompresses a binary string into an array.
make sure you use the right tree and dict as generated by the compress method.
*binaryStringToBytes(string)
converts a binary string into 8-bit bytes.
up to 7 zeros might be padded automatically at the end. the number of zeros is stored in pad.
pad is semi-private and used when converting back to bytes.
*bytesToBinaryString(array)
converts an array of 8-bit bytes into a binary string.
<>pad
semi-private counter for number of padded zeros.
set by binaryStringToBytes and read by bytesToBinaryString.
<>tree
multi-level array with values as leaves.
<>dict
look-up dictionary with values and binary strings.
//--
a= "000001111233333333".ascii;
b= RedHuffman.compress(a);
RedHuffman.binaryStringToBytes(b).size/a.size; //compressed to 28%
c= RedHuffman.decompress(b);
a==c;
a= "streets are stone stars are not".ascii;
b= RedHuffman.compress(a);
RedHuffman.binaryStringToBytes(b).size/a.size; //compressed to 39%
c= RedHuffman.decompress(b);
a==c
a= "go go gophers".ascii;
b= RedHuffman.compress(a);
RedHuffman.tree; //current tree
RedHuffman.dict; //current dict
c= RedHuffman.decompress(b);
a==c
a= "go go gophers"; //or with a string argument
b= RedHuffman.compress(a);
RedHuffman.tree; //current tree
RedHuffman.dict; //current dict
c= RedHuffman.decompress(b);
a==c.join
a= [0, 0, 0, 0, 1.5, 1.5, 1.5, \abc, \abc, \testing];
b= RedHuffman.compress(a);
RedHuffman.tree; //current tree
RedHuffman.dict; //current dict
c= RedHuffman.binaryStringToBytes(b); //this is the data you can save in a file
c.size/a.size; //30% smaller than the original
d= RedHuffman.bytesToBinaryString(c);
e= RedHuffman.decompress(d); //here is the data expanded again
a==e
a= "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.".ascii;
b= RedHuffman.compress(a);
c= RedHuffman.binaryStringToBytes(b);
c.size/a.size; //53% smaller than the original
d= RedHuffman.bytesToBinaryString(c);
e= RedHuffman.decompress(d);
e==a