// 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 (
	l3TableBits = 16               // Bits used in level 3 table
	l3TableSize = 1 << l3TableBits // Size of the level 3 table
)

// Level 3 uses a similar algorithm to level 2, with a smaller table,
// but will check up two candidates for each iteration with more
// entries added to the table.
type fastEncL3 struct {
	fastGen
	table [l3TableSize]tableEntryPrev
}

func ( *fastEncL3) ( *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[]
			if .cur.offset <=  {
				.cur.offset = 0
			} else {
				.cur.offset = .cur.offset - .cur + maxMatchOffset
			}
			if .prev.offset <=  {
				.prev.offset = 0
			} else {
				.prev.offset = .prev.offset - .cur + maxMatchOffset
			}
			.table[] = 
		}
		.cur = maxMatchOffset
	}

	 := .addBlock()

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

	// Override src
	 = .hist
	 := 

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

	// nextEmit is where in src the next emitLiterals should start from.
	 := loadLE64(, )
	for {
		const  = 7
		 := 
		var  tableEntry
		for {
			 := hashLen(, l3TableBits, )
			 = 
			 =  + 1 + (-)>>
			if  >  {
				goto 
			}
			 := .table[]
			 := loadLE64(, )

			// Safe offset distance until s + 4...
			 := .cur +  - (maxMatchOffset - 4)
			.table[] = tableEntryPrev{prev: .cur, cur: tableEntry{offset:  + .cur}}

			// Check both candidates
			 = .cur
			if .offset <  {
				 = 
				// Previous will also be invalid, we have nothing.
				continue
			}

			if uint32() == loadLE32(, .offset-.cur) {
				if .prev.offset <  || uint32() != loadLE32(, .prev.offset-.cur) {
					break
				}
				// Both match and are valid, pick longest.
				 :=  - (.offset - .cur)
				 :=  - (.prev.offset - .cur)
				,  := matchLen([+4:], [-+4:]), matchLen([+4:], [-+4:])
				if  >  {
					 = .prev
				}
				break
			} else {
				// We only check if value mismatches.
				// Offset will always be invalid in other cases.
				 = .prev
				if .offset >  && uint32() == loadLE32(, .offset-.cur) {
					break
				}
			}
			 = 
		}

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

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

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

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

			// Store every 5th hash in-between.
			for  :=  -  + 2;  < -5;  += 6 {
				 := hashLen(loadLE64(, ), l3TableBits, )
				.table[] = tableEntryPrev{
					prev: .table[].cur,
					cur:  tableEntry{offset: .cur + }}
			}
			// We could immediately start working at s now, but to improve
			// compression we first update the hash table at s-2 to s.
			 := loadLE64(, -2)
			 := hashLen(, l3TableBits, )

			.table[] = tableEntryPrev{
				prev: .table[].cur,
				cur:  tableEntry{offset: .cur +  - 2},
			}
			 >>= 8
			 = hashLen(, l3TableBits, )

			.table[] = tableEntryPrev{
				prev: .table[].cur,
				cur:  tableEntry{offset: .cur +  - 1},
			}
			 >>= 8
			 := hashLen(, l3TableBits, )
			 := .table[]
			 = 
			.table[] = tableEntryPrev{
				prev: .cur,
				cur:  tableEntry{offset:  + .cur},
			}

			// Check both candidates
			 = .cur
			 := .cur +  - (maxMatchOffset - 4)

			if .offset >  {
				if uint32() == loadLE32(, .offset-.cur) {
					// Found a match...
					continue
				}
				 = .prev
				if .offset >  && uint32() == loadLE32(, .offset-.cur) {
					// Match at prev...
					continue
				}
			}
			 =  >> 8
			++
			break
		}
	}

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

		emitLiterals(, [:])
	}
}