// 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

const (
	l2TableBits = 17               // Bits used in level 2 table
	l2TableSize = 1 << l2TableBits // Size of the level 2 table
)

// Level 2 uses a similar algorithm to level 1, but with a larger table.
type fastEncL2 struct {
	fastGen
	table [l2TableSize]tableEntry
}

func ( *fastEncL2) ( *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 {
		// When should we start skipping if we haven't found matches in a long while.
		const  = 5
		const  = 2

		 := 
		var  tableEntry
		for {
			 := hashLen(, l2TableBits, )
			 = 
			 =  +  + (-)>>
			if  >  {
				goto 
			}
			 = .table[]
			 := loadLE64(, )
			.table[] = tableEntry{offset:  + .cur}
			 = hashLen(, l2TableBits, )

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

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

			 =  - (.offset - .cur)
			if  < maxMatchOffset && uint32() == loadLE32(, .offset-.cur) {
				break
			}
			 = 
		}

		// A 4-byte match has been found. We'll later see if more than 4 bytes match.
		for {
			// Extend the 4-byte match as long as possible.
			 := .offset - .cur
			 := .matchLenLong(int(+4), int(+4), ) + 4

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

			.AddMatchLong(, uint32(--baseMatchOffset))
			 += 
			 = 
			if  >=  {
				 =  + 1
			}

			if  >=  {
				// Index first pair after match end.
				if int(++8) < len() {
					 := loadLE64(, )
					.table[hashLen(, l2TableBits, )] = tableEntry{offset:  + .cur}
				}
				goto 
			}

			// Store every second hash in-between, but offset by 1.
			for  :=  -  + 2;  < -5;  += 7 {
				 := loadLE64(, )
				 := hashLen(, l2TableBits, )
				.table[] = tableEntry{offset: .cur + }
				// Skip one
				 >>= 16
				 = hashLen(, l2TableBits, )
				.table[] = tableEntry{offset: .cur +  + 2}
				// Skip one
				 >>= 16
				 = hashLen(, l2TableBits, )
				.table[] = tableEntry{offset: .cur +  + 4}
			}

			// We could immediately start working at s now, but to improve
			// compression we first update the hash table at s-2 to s. If
			// another emitCopy is not our next move, also calculate nextHash
			// at s+1.
			 := loadLE64(, -2)
			 := .cur +  - 2
			 := hashLen(, l2TableBits, )
			 := hashLen(>>8, l2TableBits, )
			.table[] = tableEntry{offset: }
			.table[] = tableEntry{offset:  + 1}
			 := hashLen(>>16, l2TableBits, )
			 = .table[]
			.table[] = tableEntry{offset:  + 2}

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

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

		emitLiterals(, [:])
	}
}