// 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 5 is similar to level 4, but for long matches two candidates are tested.
// Once a match is found, when it stops it will attempt to find a match that extends further.
type fastEncL5 struct {
	fastGen
	table  [tableSize]tableEntry
	bTable [tableSize]tableEntryPrev
}

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

	// Protect against e.cur wraparound.
	for .cur >= bufferReset {
		if len(.hist) == 0 {
			clear(.table[:])
			clear(.bTable[:])
			.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 = 
		}
		for  := range .bTable[:] {
			 := .bTable[]
			if .cur.offset <=  {
				.cur.offset = 0
				.prev.offset = 0
			} else {
				.cur.offset = .cur.offset - .cur + maxMatchOffset
				if .prev.offset <=  {
					.prev.offset = 0
				} else {
					.prev.offset = .prev.offset - .cur + maxMatchOffset
				}
			}
			.bTable[] = 
		}
		.cur = maxMatchOffset
	}

	 := .addBlock()

	// This check isn't in the Snappy implementation, but there, the caller
	// instead of the callee handles this case.
	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  = 6
		const  = 1

		 := 
		var  int32
		var  int32
		for {
			 := hashLen(, tableBits, )
			 := hashLen(, tableBits, hashLongBytes)

			 = 
			 =  +  + (-)>>
			if  >  {
				goto 
			}
			// Fetch a short+long candidate
			 := .table[]
			 := .bTable[]
			 := loadLE64(, )
			 := tableEntry{offset:  + .cur}
			.table[] = 
			 := &.bTable[]
			.cur, .prev = , .cur

			 = hashLen(, tableBits, )
			 = hashLen(, tableBits, hashLongBytes)

			 = .cur.offset - .cur
			if - < maxMatchOffset {
				if uint32() == loadLE32(, ) {
					// Store the next match
					.table[] = tableEntry{offset:  + .cur}
					 := &.bTable[]
					.cur, .prev = tableEntry{offset:  + .cur}, .cur

					 := .prev.offset - .cur
					if - < maxMatchOffset && uint32() == loadLE32(, ) {
						 = .matchLenLimited(int(+4), int(+4), ) + 4
						 := .matchLenLimited(int(+4), int(+4), ) + 4
						if  >  {
							 = 
							 = 
							break
						}
					}
					break
				}
				 = .prev.offset - .cur
				if - < maxMatchOffset && uint32() == loadLE32(, ) {
					// Store the next match
					.table[] = tableEntry{offset:  + .cur}
					 := &.bTable[]
					.cur, .prev = tableEntry{offset:  + .cur}, .cur
					break
				}
			}

			 = .offset - .cur
			if - < maxMatchOffset && uint32() == loadLE32(, ) {
				// Found a 4 match...
				 = .matchLenLimited(int(+4), int(+4), ) + 4
				 = .bTable[]
				// Store the next match

				.table[] = tableEntry{offset:  + .cur}
				 := &.bTable[]
				.cur, .prev = tableEntry{offset:  + .cur}, .cur

				// If the next long is a candidate, use that...
				 := .cur.offset - .cur
				if - < maxMatchOffset {
					if loadLE32(, ) == uint32() {
						 := .matchLenLimited(int(+4), int(+4), ) + 4
						if  >  {
							 = 
							 = 
							 = 
							break
						}
					}
					// If the previous long is a candidate, use that...
					 = .prev.offset - .cur
					if - < maxMatchOffset && loadLE32(, ) == uint32() {
						 := .matchLenLimited(int(+4), int(+4), ) + 4
						if  >  {
							 = 
							 = 
							 = 
							break
						}
					}
				}
				break
			}
			 = 
		}

		if  == 0 {
			// Extend the 4-byte match as long as possible.
			 = .matchLenLong(int(+4), int(+4), ) + 4
		} else if  == maxMatchLength {
			 += .matchLenLong(int(+), int(+), )
		}

		// Try to locate a better match by checking the end of best match...
		if  :=  + ;  < 30 &&  <  {
			// Allow some bytes at the beginning to mismatch.
			// Sweet spot is 2/3 bytes depending on input.
			// 3 is only a little better when it is but sometimes a lot worse.
			// The skipped bytes are tested in Extend backwards,
			// and still picked up as part of the match if they do.
			const  = 2
			 := .bTable[hashLen(loadLE64(, ), tableBits, hashLongBytes)].cur.offset
			 :=  - .cur -  + 
			 :=  + 
			 :=  - 
			if  >= 0 &&  < maxMatchOffset &&  > 0 {
				if  := .matchLenLong(int(), int(), );  >  {
					 = 
					 = 
					 = 
				}
			}
		}

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

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

		if  >=  {
			goto 
		}

		// Store every 3rd hash in-between.
		const  = 3
		 :=  -  + 1
		if  < -1 {
			 := loadLE64(, )
			 := tableEntry{offset:  + .cur}
			.table[hashLen(, tableBits, )] = 
			 := &.bTable[hashLen(, tableBits, hashLongBytes)]
			.cur, .prev = , .cur

			// Do an long at i+1
			 >>= 8
			 = tableEntry{offset: .offset + 1}
			 = &.bTable[hashLen(, tableBits, hashLongBytes)]
			.cur, .prev = , .cur

			// We only have enough bits for a short entry at i+2
			 >>= 8
			 = tableEntry{offset: .offset + 1}
			.table[hashLen(, tableBits, )] = 

			// Skip one - otherwise we risk hitting 's'
			 += 4
			for ;  < -1;  +=  {
				 := loadLE64(, )
				 := tableEntry{offset:  + .cur}
				 := tableEntry{offset: .offset + 1}
				 := &.bTable[hashLen(, tableBits, hashLongBytes)]
				.cur, .prev = , .cur
				.table[hashLen(>>8, tableBits, )] = 
			}
		}

		// We could immediately start working at s now, but to improve
		// compression we first update the hash table at s-1 and at s.
		 := loadLE64(, -1)
		 := .cur +  - 1
		 := hashLen(, tableBits, )
		 := hashLen(, tableBits, hashLongBytes)
		.table[] = tableEntry{offset: }
		 := &.bTable[]
		.cur, .prev = tableEntry{offset: }, .cur
		 =  >> 8
	}

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

		emitLiterals(, [:])
	}
}