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

import (
	
)

const (
	// Token is a compound value:
	// bits 0-16  xoffset = offset - MIN_OFFSET_SIZE, or literal - 16 bits
	// bits 16-22 offset code - 5 bits
	// bits 22-30 xlength = length - MIN_MATCH_LENGTH - 8 bits
	// bits 30-32 type, 0 = literal  1=EOF  2=Match   3=Unused - 2 bits
	lengthShift         = 22
	offsetMask          = 1<<lengthShift - 1
	typeMask            = 3 << 30
	matchType           = 1 << 30
	matchOffsetOnlyMask = 0xffff
)

// The length code for length X (MIN_MATCH_LENGTH <= X <= MAX_MATCH_LENGTH)
// is lengthCodes[length - MIN_MATCH_LENGTH]
var lengthCodes = [256]uint8{
	0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
	9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
	13, 13, 13, 13, 14, 14, 14, 14, 15, 15,
	15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
	17, 17, 17, 17, 17, 17, 17, 17, 18, 18,
	18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
	19, 19, 19, 19, 20, 20, 20, 20, 20, 20,
	20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
	21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
	21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
	22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
	22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
	23, 23, 23, 23, 23, 23, 23, 23, 24, 24,
	24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
	24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
	24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 26, 26, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 27, 28,
}

// lengthCodes1 is length codes, but starting at 1.
var lengthCodes1 = [256]uint8{
	1, 2, 3, 4, 5, 6, 7, 8, 9, 9,
	10, 10, 11, 11, 12, 12, 13, 13, 13, 13,
	14, 14, 14, 14, 15, 15, 15, 15, 16, 16,
	16, 16, 17, 17, 17, 17, 17, 17, 17, 17,
	18, 18, 18, 18, 18, 18, 18, 18, 19, 19,
	19, 19, 19, 19, 19, 19, 20, 20, 20, 20,
	20, 20, 20, 20, 21, 21, 21, 21, 21, 21,
	21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
	22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
	22, 22, 22, 22, 22, 22, 23, 23, 23, 23,
	23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
	23, 23, 24, 24, 24, 24, 24, 24, 24, 24,
	24, 24, 24, 24, 24, 24, 24, 24, 25, 25,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 28, 28, 28, 28, 28, 28,
	28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
	28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
	28, 28, 28, 28, 28, 29,
}

var offsetCodes = [256]uint32{
	0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
	8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
	12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
	12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
	13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
	13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
	14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
	14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
	14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
	14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
	15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
	15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
	15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
	15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
}

// offsetCodes14 are offsetCodes, but with 14 added.
var offsetCodes14 = [256]uint32{
	14, 15, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
	22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
	24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
	25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
	27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
	27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
	28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
	28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
	28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
	28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
	29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
	29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
	29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
	29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
}

// A token is a token that will be written to output stream.
// It is either a literal or a match with offset and length.
type token uint32

// tokens are compound values as described above.
// Histograms are created as tokens are added.
// A full block is allocated.
type tokens struct {
	extraHist [32]uint16  // codes 256->maxnumlit
	offHist   [32]uint16  // offset codes
	litHist   [256]uint16 // codes 0->255
	nFilled   int
	n         uint16 // Must be able to contain maxStoreBlockSize
	tokens    [65536]token
}

// Reset resets the tokens and histograms.
func ( *tokens) () {
	if .n == 0 {
		return
	}
	.n = 0
	.nFilled = 0
	clear(.litHist[:])
	clear(.extraHist[:])
	clear(.offHist[:])
}

// indexTokens creates tokens from a slice of unindexed tokens.
func indexTokens( []token) tokens {
	var  tokens
	.indexTokens()
	return 
}

// indexTokens clears and sets t from a slice of unindexed tokens.
func ( *tokens) ( []token) {
	.Reset()
	for ,  := range  {
		if  < matchType {
			.AddLiteral(.literal())
			continue
		}
		.AddMatch(uint32(.length()), .offset()&matchOffsetOnlyMask)
	}
}

// emitLiterals writes a literal chunk and returns the number of bytes written.
func emitLiterals( *tokens,  []byte) {
	for ,  := range  {
		.tokens[.n] = token()
		.litHist[]++
		.n++
	}
}

// AddLiteral adds a single literal to the tokens.
func ( *tokens) ( byte) {
	.tokens[.n] = token()
	.litHist[]++
	.n++
}

// mFastLog2 returns a fast approximation of log2(val).
// From https://stackoverflow.com/a/28730362.
func mFastLog2( float32) float32 {
	 := int32(math.Float32bits())
	 := (float32)((( >> 23) & 255) - 128)
	 &= -0x7f800001
	 += 127 << 23
	 := math.Float32frombits(uint32())
	 += ((-0.34484843)*+2.02466578)* - 0.67487759
	return 
}

// EstimatedBits returns an estimated minimum size for the
// optimal compression of t.
// Minimum 1 bit is assigned per symbol.
// Maximum 15 bits are assigned per symbol.
func ( *tokens) () int {
	 := float32(0)
	 := int(0)
	 := 0
	 := int(.n) + .nFilled
	if  > 0 {
		 := 1.0 / float32()
		for ,  := range .litHist[:] {
			if  > 0 {
				 := float32()
				 += min(15, max(1, -mFastLog2(*))) * 
			}
		}
		// Just add 15 for EOB
		 += 15
		for ,  := range .extraHist[1 : literalCount-256] {
			if  > 0 {
				 := float32()
				 += min(15, max(1, -mFastLog2(*))) * 
				 += int(lengthExtraBits[&31]) * int()
				 += int()
			}
		}
	}
	if  > 0 {
		 := 1.0 / float32()
		for ,  := range .offHist[:offsetCodeCount] {
			if  > 0 {
				 := float32()
				 += min(15, max(1, -mFastLog2(*))) * 
				 += int(offsetExtraBits[&31]) * int()
			}
		}
	}
	return int() + 
}

// AddMatch adds a match to the tokens.
// This function is very sensitive to inlining and right on the border.
func ( *tokens) ( uint32,  uint32) {
	 := offsetCode()
	 |=  << 16

	.extraHist[lengthCodes1[uint8()]]++
	.offHist[&31]++
	.tokens[.n] = token(matchType | <<lengthShift | )
	.n++
}

// AddMatchLong adds a match to the tokens, potentially longer than max match length.
// Length should NOT have the base subtracted, only offset should.
func ( *tokens) ( int32,  uint32) {
	 := offsetCode()
	 |=  << 16
	for  > 0 {
		 := 
		if  > 258 {
			// We need to have at least baseMatchLength left over for next loop.
			if  > 258+baseMatchLength {
				 = 258
			} else {
				 = 258 - baseMatchLength
			}
		}
		 -= 
		 -= baseMatchLength
		.extraHist[lengthCodes1[uint8()]]++
		.offHist[&31]++
		.tokens[.n] = token(matchType | uint32()<<lengthShift | )
		.n++
	}
}

// AddEOB adds an end of block marker to the tokens.
func ( *tokens) () {
	.tokens[.n] = token(endBlockMarker)
	.extraHist[0]++
	.n++
}

// Slice returns a slice of the tokens that references the tokens in t.
func ( *tokens) () []token {
	return .tokens[:.n]
}

// typ returns the type of a token.
func ( token) () uint32 { return uint32() & typeMask }

// literal returns the literal value of t.
func ( token) () uint8 { return uint8() }

// offset returns the offset of a match token.
func ( token) () uint32 { return uint32() & offsetMask }

// length returns the length of a match token.
func ( token) () uint8 { return uint8( >> lengthShift) }

// lengthCode converts a match length to its code.
func lengthCode( uint8) uint8 { return lengthCodes[] }

// offsetCode returns the offset code corresponding to a specific offset.
func offsetCode( uint32) uint32 {
	if  < uint32(len(offsetCodes)) {
		return offsetCodes[uint8()]
	}
	return offsetCodes14[uint8(>>7)]
}