RedRLE run-length compression/decompression


see http://en.wikipedia.org/wiki/Run-length_encoding


performs well with highly repetitive data.  e.g. [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3]

performs very bad with data containing repeating patterns.  e.g. [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]

works with Integer, Float and Symbol.  can also take a String instead of an Array.


see also: RedBase64 RedHuffman RedLZ77 RedLZ78 RedLZSS RedLZW


*encode(array)

returns an array with length, value pairs.

*decode(array)

array must be length, value pairs.



//--

a= [0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 0.5, 0.5, 0.5, \abc, \abc, \abc];

b= RedRLE.encode(a);

b.size/a.size; //compressed to 55%

c= RedRLE.decode(b);

c==a



a= "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"; //string instead of array

a.size;

b= RedRLE.encode(a);

b.size/a.size; //compressed to 21%

c= RedRLE.decode(b);

c.size;

a==c.join;



a= "JOEYNJOEYNJOEYJOEYNJOEYNJOEYJOEYNJOEYNJOEYJOEYNJOEYNJOEY".ascii;

a.size;

b= RedRLE.encode(a);

b.size/c.size; //expanded to 167%.  performs really bad with non-repeating values

c= RedRLE.decode(b);

c.size;

a==c;

c.collect{|x| x.asAscii}.join;