package flate
const (
l2TableBits = 17
l2TableSize = 1 << l2TableBits
)
type fastEncL2 struct {
fastGen
table [l2TableSize ]tableEntry
}
func (e *fastEncL2 ) encode (dst *tokens , src []byte ) {
const (
inputMargin = 12 - 1
minNonLiteralBlockSize = 1 + 1 + inputMargin
hashBytes = 5
)
for e .cur >= bufferReset {
if len (e .hist ) == 0 {
clear (e .table [:])
e .cur = maxMatchOffset
break
}
minOff := e .cur + int32 (len (e .hist )) - maxMatchOffset
for i := range e .table [:] {
v := e .table [i ].offset
if v <= minOff {
v = 0
} else {
v = v - e .cur + maxMatchOffset
}
e .table [i ].offset = v
}
e .cur = maxMatchOffset
}
s := e .addBlock (src )
if len (src ) < minNonLiteralBlockSize {
dst .n = uint16 (len (src ))
return
}
src = e .hist
nextEmit := s
sLimit := int32 (len (src ) - inputMargin )
cv := loadLE64 (src , s )
for {
const skipLog = 5
const doEvery = 2
nextS := s
var candidate tableEntry
for {
nextHash := hashLen (cv , l2TableBits , hashBytes )
s = nextS
nextS = s + doEvery + (s -nextEmit )>>skipLog
if nextS > sLimit {
goto emitRemainder
}
candidate = e .table [nextHash ]
now := loadLE64 (src , nextS )
e .table [nextHash ] = tableEntry {offset : s + e .cur }
nextHash = hashLen (now , l2TableBits , hashBytes )
offset := s - (candidate .offset - e .cur )
if offset < maxMatchOffset && uint32 (cv ) == loadLE32 (src , candidate .offset -e .cur ) {
e .table [nextHash ] = tableEntry {offset : nextS + e .cur }
break
}
cv = now
s = nextS
nextS ++
candidate = e .table [nextHash ]
now >>= 8
e .table [nextHash ] = tableEntry {offset : s + e .cur }
offset = s - (candidate .offset - e .cur )
if offset < maxMatchOffset && uint32 (cv ) == loadLE32 (src , candidate .offset -e .cur ) {
break
}
cv = now
}
for {
t := candidate .offset - e .cur
l := e .matchLenLong (int (s +4 ), int (t +4 ), src ) + 4
for t > 0 && s > nextEmit && src [t -1 ] == src [s -1 ] {
s --
t --
l ++
}
if nextEmit < s {
for _ , v := range src [nextEmit :s ] {
dst .tokens [dst .n ] = token (v )
dst .litHist [v ]++
dst .n ++
}
}
dst .AddMatchLong (l , uint32 (s -t -baseMatchOffset ))
s += l
nextEmit = s
if nextS >= s {
s = nextS + 1
}
if s >= sLimit {
if int (s +l +8 ) < len (src ) {
cv := loadLE64 (src , s )
e .table [hashLen (cv , l2TableBits , hashBytes )] = tableEntry {offset : s + e .cur }
}
goto emitRemainder
}
for i := s - l + 2 ; i < s -5 ; i += 7 {
x := loadLE64 (src , i )
nextHash := hashLen (x , l2TableBits , hashBytes )
e .table [nextHash ] = tableEntry {offset : e .cur + i }
x >>= 16
nextHash = hashLen (x , l2TableBits , hashBytes )
e .table [nextHash ] = tableEntry {offset : e .cur + i + 2 }
x >>= 16
nextHash = hashLen (x , l2TableBits , hashBytes )
e .table [nextHash ] = tableEntry {offset : e .cur + i + 4 }
}
x := loadLE64 (src , s -2 )
o := e .cur + s - 2
prevHash := hashLen (x , l2TableBits , hashBytes )
prevHash2 := hashLen (x >>8 , l2TableBits , hashBytes )
e .table [prevHash ] = tableEntry {offset : o }
e .table [prevHash2 ] = tableEntry {offset : o + 1 }
currHash := hashLen (x >>16 , l2TableBits , hashBytes )
candidate = e .table [currHash ]
e .table [currHash ] = tableEntry {offset : o + 2 }
offset := s - (candidate .offset - e .cur )
if offset > maxMatchOffset || uint32 (x >>16 ) != loadLE32 (src , candidate .offset -e .cur ) {
cv = x >> 24
s ++
break
}
}
}
emitRemainder :
if int (nextEmit ) < len (src ) {
if dst .n == 0 {
return
}
emitLiterals (dst , src [nextEmit :])
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .