lempel ziv 77 logic

[dictionarySize, searchSize, ...]からlz77 codesを計算。

define Uint8Array.prototype

1. declare index
let i=b?32768:0

2. declare length
const l=this.length

3. declare seal
,seal=(i,dictionarySize)=>{
    let lI=i,length=0,dist=0
    const v0=this[i],v1=this[1+i],v2=this[2+i],fL=(i-32768).limit(false,0),tL=(l-i).limit(true,v)
    while(dictionarySize--){
        if(1>lI||(lI=this.lastIndexOf(v0,lI-1))<fL)break
        if(v1===this[1+lI]&&v2===this[2+lI]){
            let l=3
            while(l<tL&&this[l+i]===this[l+lI])l++
            if(length<l&&(dist=i-lI)&&v===(length=l))break
        }
    }
    return 2<length&&0<dist?length<<16|dist:0
}

4. call
if(dictionarySize&&searchSize){
    const tL=l-2,sealed=[]
    while(i<tL){
        forEach(false,(l-i).limit(true,1+searchSize)-sealed.length,()=>sealed.push(seal(sealed.length+i,dictionarySize)))
        const v=sealed[0]
        if(v){
            const length=v>>>16
            if(!all(false,sealed,(v,i)=>i&&length<v>>>16)&&(i+=length)){
                c(v)
                sealed.length=reduce(false,sealed,(a,v,i)=>{
                    if(!(i<length))sealed[a++]=v
                    return a
                },0)
                continue
            }
        }
        c(this[i++])
        sealed.shift()
    }
}else{
    const tL=l-2
    while(i<tL){
        const v=seal(i,dictionarySize)
        if(v){
            const length=v>>>16
            if((i+=length)){
                c(v)
                continue
            }
        }
        c(this[i++])
    }
}
for(;i<l;i++)c(this[i])