package brotli

/* Copyright 2015 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Greedy block splitter for one block category (literal, command or distance).
 */
type blockSplitterLiteral struct {
	alphabet_size_     uint
	min_block_size_    uint
	split_threshold_   float64
	num_blocks_        uint
	split_             *blockSplit
	histograms_        []histogramLiteral
	histograms_size_   *uint
	target_block_size_ uint
	block_size_        uint
	curr_histogram_ix_ uint
	last_histogram_ix_ [2]uint
	last_entropy_      [2]float64
	merge_last_count_  uint
}

func initBlockSplitterLiteral( *blockSplitterLiteral,  uint,  uint,  float64,  uint,  *blockSplit,  *[]histogramLiteral,  *uint) {
	var  uint = / + 1
	var  uint = brotli_min_size_t(, maxNumberOfBlockTypes+1)
	/* We have to allocate one more histogram than the maximum number of block
	   types for the current histogram when the meta-block is too big. */
	.alphabet_size_ = 

	.min_block_size_ = 
	.split_threshold_ = 
	.num_blocks_ = 0
	.split_ = 
	.histograms_size_ = 
	.target_block_size_ = 
	.block_size_ = 0
	.curr_histogram_ix_ = 0
	.merge_last_count_ = 0
	brotli_ensure_capacity_uint8_t(&.types, &.types_alloc_size, )
	brotli_ensure_capacity_uint32_t(&.lengths, &.lengths_alloc_size, )
	.split_.num_blocks = 
	* = 
	if  == nil || cap(*) < int(*) {
		* = make([]histogramLiteral, *)
	} else {
		* = (*)[:*]
	}
	.histograms_ = *

	/* Clear only current histogram. */
	histogramClearLiteral(&.histograms_[0])

	.last_histogram_ix_[1] = 0
	.last_histogram_ix_[0] = .last_histogram_ix_[1]
}

/* Does either of three things:
   (1) emits the current block with a new block type;
   (2) emits the current block with the type of the second last block;
   (3) merges the current block with the last block. */
func blockSplitterFinishBlockLiteral( *blockSplitterLiteral,  bool) {
	var  *blockSplit = .split_
	var  []float64 = .last_entropy_[:]
	var  []histogramLiteral = .histograms_
	.block_size_ = brotli_max_size_t(.block_size_, .min_block_size_)
	if .num_blocks_ == 0 {
		/* Create first block. */
		.lengths[0] = uint32(.block_size_)

		.types[0] = 0
		[0] = bitsEntropy([0].data_[:], .alphabet_size_)
		[1] = [0]
		.num_blocks_++
		.num_types++
		.curr_histogram_ix_++
		if .curr_histogram_ix_ < *.histograms_size_ {
			histogramClearLiteral(&[.curr_histogram_ix_])
		}
		.block_size_ = 0
	} else if .block_size_ > 0 {
		var  float64 = bitsEntropy([.curr_histogram_ix_].data_[:], .alphabet_size_)
		var  [2]histogramLiteral
		var  [2]float64
		var  [2]float64
		var  uint
		for  = 0;  < 2; ++ {
			var  uint = .last_histogram_ix_[]
			[] = [.curr_histogram_ix_]
			histogramAddHistogramLiteral(&[], &[])
			[] = bitsEntropy([].data_[0:], .alphabet_size_)
			[] = [] -  - []
		}

		if .num_types < maxNumberOfBlockTypes && [0] > .split_threshold_ && [1] > .split_threshold_ {
			/* Create new block. */
			.lengths[.num_blocks_] = uint32(.block_size_)

			.types[.num_blocks_] = byte(.num_types)
			.last_histogram_ix_[1] = .last_histogram_ix_[0]
			.last_histogram_ix_[0] = uint(byte(.num_types))
			[1] = [0]
			[0] = 
			.num_blocks_++
			.num_types++
			.curr_histogram_ix_++
			if .curr_histogram_ix_ < *.histograms_size_ {
				histogramClearLiteral(&[.curr_histogram_ix_])
			}
			.block_size_ = 0
			.merge_last_count_ = 0
			.target_block_size_ = .min_block_size_
		} else if [1] < [0]-20.0 {
			.lengths[.num_blocks_] = uint32(.block_size_)
			.types[.num_blocks_] = .types[.num_blocks_-2]
			/* Combine this block with second last block. */

			var  uint = .last_histogram_ix_[0]
			.last_histogram_ix_[0] = .last_histogram_ix_[1]
			.last_histogram_ix_[1] = 
			[.last_histogram_ix_[0]] = [1]
			[1] = [0]
			[0] = [1]
			.num_blocks_++
			.block_size_ = 0
			histogramClearLiteral(&[.curr_histogram_ix_])
			.merge_last_count_ = 0
			.target_block_size_ = .min_block_size_
		} else {
			/* Combine this block with last block. */
			.lengths[.num_blocks_-1] += uint32(.block_size_)

			[.last_histogram_ix_[0]] = [0]
			[0] = [0]
			if .num_types == 1 {
				[1] = [0]
			}

			.block_size_ = 0
			histogramClearLiteral(&[.curr_histogram_ix_])
			.merge_last_count_++
			if .merge_last_count_ > 1 {
				.target_block_size_ += .min_block_size_
			}
		}
	}

	if  {
		*.histograms_size_ = .num_types
		.num_blocks = .num_blocks_
	}
}

/* Adds the next symbol to the current histogram. When the current histogram
   reaches the target size, decides on merging the block. */
func blockSplitterAddSymbolLiteral( *blockSplitterLiteral,  uint) {
	histogramAddLiteral(&.histograms_[.curr_histogram_ix_], )
	.block_size_++
	if .block_size_ == .target_block_size_ {
		blockSplitterFinishBlockLiteral(, false) /* is_final = */
	}
}