// 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
	error      bool
	dict       *dict

	// ring backs b when streaming (see ensureBlockRing).
	// ext holds the part of the previous lap of ring still in the window,
	// which precedes b[0] in the stream; nil when b holds all history.
	// The previous lap is ring[extStart:extEnd].
	ring     []byte
	ext      []byte
	extStart int
	extEnd   int
}

// 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]
	.ext = nil
	.extStart, .extEnd = 0, 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
}

// ensureBlockRing makes room for the next block without moving history,
// as the reference decoder does. b is a prefix of ring capped to one block
// of space. When a block no longer fits, the data written so far becomes
// ext and writing restarts at the front of ring. ext is trimmed so the
// block space ahead of b never overlaps it; with ring sized to window plus
// two blocks of space, b and ext together always hold a full window.
func ( *history) () {
	// Room for the largest block plus the overrun of the fast copies.
	 := min(.windowSize, maxCompressedBlockSize) + compressedBlockOverAlloc
	 := .windowSize + 2*
	 := len(.b)
	if cap(.ring) <  || ( > 0 && &.b[0] != &.ring[0]) {
		// No ring yet, or b no longer lives in it: start one, keeping up
		// to a window of what was decoded so far as the previous lap.
		 := min(, .windowSize)
		if cap(.ring) <  {
			.ring = make([]byte, )
		}
		.ring = .ring[:]
		copy(.ring[-:], .b[-:])
		.extStart, .extEnd = -, 
		 = 0
	} else if + >  {
		// Wrap: everything written this lap becomes the previous lap.
		.extStart, .extEnd = 0, 
		 = 0
	}
	.ring = .ring[:]
	 :=  + 
	.b = .ring[::]
	.ext = nil
	if  := max(, .extStart);  < .extEnd {
		.ext = .ring[:.extEnd]
	}
}

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