mramine364

lz77 compression

Jun 25th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lz77_compress(str, dictlen, bufflen){
  2.     let dl=dictlen, drl=0, bl=bufflen;
  3.     let maxlen, maxa, maxk, output=[];
  4.     while( str.length>drl ){
  5.         maxlen=0;
  6.         maxk=0;
  7.         for(let i=Math.max(drl-dl,0); i<drl; i++){
  8.             let l=i, k=0;
  9.             while( drl+k<str.length-1 && k+1<bl && str[l]==str[drl+k] ){
  10.                 l++;
  11.                 k++;
  12.             }
  13.             if( l-i>maxlen ){
  14.                 maxlen=l-i;
  15.                 maxa=i;
  16.                 maxk=k;
  17.             }
  18.         }
  19.         if( maxlen!=0 ){
  20.             let ind = maxa-(drl-dl);
  21.             output.push( {
  22.                 index: ind, length: maxlen, char: str[drl+maxk]
  23.             } );
  24.         }else{
  25.             output.push( {
  26.                 index: 0, length: 0, char: str[drl+maxk]
  27.             } );
  28.         }
  29.         drl += maxlen+1;
  30.     }
  31.     return output;
  32. }
Add Comment
Please, Sign In to add comment