// Copyright 2019+ Klaus Post. All rights reserved.
// License information can be found in the LICENSE file.
// Based on work by Yann Collet, released under BSD License.

package zstd

import (
	
)

// history contains the information transferred between blocks.
type history struct {
	// Literal decompression
	huffTree *huff0.Scratch

	// Sequence decompression
	decoders      sequenceDecs
	recentOffsets [3]int

	// History buffer...
	b []byte

	// ignoreBuffer is meant to ignore a number of bytes
	// when checking for matches in history
	ignoreBuffer int

	windowSize       int
	allocFrameBuffer int // needed?
	error            bool
	dict             *dict
}

// reset will reset the history to initial state of a frame.
// The history must already have been initialized to the desired size.
func ( *history) () {
	.b = .b[:0]
	.ignoreBuffer = 0
	.error = false
	.recentOffsets = [3]int{1, 4, 8}
	.decoders.freeDecoders()
	.decoders = sequenceDecs{br: .decoders.br}
	.freeHuffDecoder()
	.huffTree = nil
	.dict = nil
	//printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
}

func ( *history) () {
	if .huffTree != nil {
		if .dict == nil || .dict.litEnc != .huffTree {
			huffDecoderPool.Put(.huffTree)
			.huffTree = nil
		}
	}
}

func ( *history) ( *dict) {
	if  == nil {
		return
	}
	.dict = 
	.decoders.litLengths = .llDec
	.decoders.offsets = .ofDec
	.decoders.matchLengths = .mlDec
	.decoders.dict = .content
	.recentOffsets = .offsets
	.huffTree = .litEnc
}

// append bytes to history.
// This function will make sure there is space for it,
// if the buffer has been allocated with enough extra space.
func ( *history) ( []byte) {
	if len() >= .windowSize {
		// Discard all history by simply overwriting
		.b = .b[:.windowSize]
		copy(.b, [len()-.windowSize:])
		return
	}

	// If there is space, append it.
	if len() < cap(.b)-len(.b) {
		.b = append(.b, ...)
		return
	}

	// Move data down so we only have window size left.
	// We know we have less than window size in b at this point.
	 := len() + len(.b) - .windowSize
	copy(.b, .b[:])
	.b = .b[:.windowSize]
	copy(.b[.windowSize-len():], )
}

// ensureBlock will ensure there is space for at least one block...
func ( *history) () {
	if cap(.b) < .allocFrameBuffer {
		.b = make([]byte, 0, .allocFrameBuffer)
		return
	}

	 := cap(.b) - len(.b)
	if  >= .windowSize ||  > maxCompressedBlockSize {
		return
	}
	// Move data down so we only have window size left.
	// We know we have less than window size in b at this point.
	 := len(.b) - .windowSize
	copy(.b, .b[:])
	.b = .b[:.windowSize]
}

// append bytes to history without ever discarding anything.
func ( *history) ( []byte) {
	.b = append(.b, ...)
}