// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flate

// Level 1 uses a single small table with 5 byte hashes.
type fastEncL1 struct {
	fastGen
	table [tableSize]tableEntry
}

func ( *fastEncL1) ( *tokens,  []byte) {
	const (
		            = 12 - 1
		 = 1 + 1 + 
		              = 5
	)

	// Protect against e.cur wraparound.
	for .cur >= bufferReset {
		if len(.hist) == 0 {
			clear(.table[:])
			.cur = maxMatchOffset
			break
		}
		// Shift down everything in the table that isn't already too far away.
		 := .cur + int32(len(.hist)) - maxMatchOffset
		for  := range .table[:] {
			 := .table[].offset
			if  <=  {
				 = 0
			} else {
				 =  - .cur + maxMatchOffset
			}
			.table[].offset = 
		}
		.cur = maxMatchOffset
	}

	 := .addBlock()

	if len() <  {
		// We do not fill the token table.
		// This will be picked up by caller.
		.n = uint16(len())
		return
	}

	// Override src
	 = .hist

	// nextEmit is where in src the next emitLiterals should start from.
	 := 

	// sLimit is when to stop looking for offset/length copies. The inputMargin
	// lets us use a fast path for emitLiterals in the main loop, while we are
	// looking for copies.
	 := int32(len() - )

	 := loadLE64(, )

	for {
		const  = 5
		const  = 2

		 := 
		var  tableEntry
		var  int32
		for {
			 := hashLen(, tableBits, )
			 = .table[]
			 =  +  + (-)>>
			if  >  {
				goto 
			}

			 := loadLE64(, )
			.table[] = tableEntry{offset:  + .cur}
			 = hashLen(, tableBits, )
			 = .offset - .cur
			if - < maxMatchOffset && uint32() == loadLE32(, ) {
				.table[] = tableEntry{offset:  + .cur}
				break
			}

			// Do one right away...
			 = 
			 = 
			++
			 = .table[]
			 >>= 8
			.table[] = tableEntry{offset:  + .cur}

			 = .offset - .cur
			if - < maxMatchOffset && uint32() == loadLE32(, ) {
				.table[] = tableEntry{offset:  + .cur}
				break
			}
			 = 
			 = 
		}

		// A 4-byte match has been found. We'll later see if more than 4 bytes
		// match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
		// them as literal bytes.
		for {
			// Invariant: we have a 4-byte match at s, and no need to emit any
			// literal bytes prior to s.

			// Extend the 4-byte match as long as possible.
			 := .matchLenLong(int(+4), int(+4), ) + 4

			// Extend backwards
			for  > 0 &&  >  && loadLE8(, -1) == loadLE8(, -1) {
				--
				--
				++
			}
			if  <  {
				for ,  := range [:] {
					.tokens[.n] = token()
					.litHist[]++
					.n++
				}
			}

			// Save the match found. Same as 'dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))'
			 := uint32( -  - baseMatchOffset)
			 := 
			 := offsetCode()
			 |=  << 16
			for  > 0 {
				 := 
				if  > 258 {
					if  > 258+baseMatchLength {
						 = 258
					} else {
						 = 258 - baseMatchLength
					}
				}
				 -= 
				 -= baseMatchLength
				.extraHist[lengthCodes1[uint8()]]++
				.offHist[]++
				.tokens[.n] = token(matchType | uint32()<<lengthShift | )
				.n++
			}
			 += 
			 = 
			if  >=  {
				 =  + 1
			}
			if  >=  {
				// Index first pair after match end.
				if int(++8) < len() {
					 := loadLE64(, )
					.table[hashLen(, tableBits, )] = tableEntry{offset:  + .cur}
				}
				goto 
			}

			// We could immediately start working at s now, but to improve
			// compression we first update the hash table at s-2 and at s. If
			// another emitCopy is not our next move, also calculate nextHash
			// at s+1. At least on GOARCH=amd64, these three hash calculations
			// are faster as one load64 call (with some shifts) instead of
			// three load32 calls.
			 := loadLE64(, -2)
			 := .cur +  - 2
			 := hashLen(, tableBits, )
			.table[] = tableEntry{offset: }
			 >>= 16
			 := hashLen(, tableBits, )
			 = .table[]
			.table[] = tableEntry{offset:  + 2}

			 = .offset - .cur
			if - > maxMatchOffset || uint32() != loadLE32(, ) {
				 =  >> 8
				++
				break
			}
		}
	}

:
	if int() < len() {
		// If nothing was added, don't encode literals.
		if .n == 0 {
			return
		}
		emitLiterals(, [:])
	}
}