/ac/model_order_0.go
package ac

type modelOrder0 struct {
	cumulativeCount []uint32
	total uint32
	n_symbols uint
}

func NewModelOrder0PreBias(n_sym uint, biases []uint32) Model {
	if len(biases) != int(n_sym) {
		panic("Wrong number of biases in order 0 model")
	}
	total := uint32(0)
	for _, n := range(biases) {
		total += n
	}
	obj := modelOrder0{
		biases,
		total,
		n_sym,
	}
	return &obj
}

func NewModelOrder0(n_sym uint) Model {
	biases := make([]uint32, n_sym)
	for i := uint(0); i < n_sym; i++ {
		biases[i] = 1
	}
	return NewModelOrder0PreBias(n_sym, biases)
}

func (self *modelOrder0) Encode(ac Codec, symbol uint32) {
	// Accumulate frequencies
	low_count := uint32(0)
	var j uint32
	for j = 0; j < symbol; j++ {
		low_count += self.cumulativeCount[j]
	}

	ac.Encode(low_count, low_count + self.cumulativeCount[j], self.total)

	self.cumulativeCount[symbol]++
	self.total++
}

func (self *modelOrder0) Decode(ac Codec) uint32 {
	var symbol uint32

	var value uint32

	value = ac.DecodeTarget(self.total)

	low_count := uint32(0)

	for symbol = 0; low_count + self.cumulativeCount[symbol] <= value; symbol++ {
		low_count += self.cumulativeCount[symbol]
	}

	ac.Decode(low_count, low_count + self.cumulativeCount[symbol])

	self.cumulativeCount[symbol]++
	self.total++

	return symbol
}