Skip to content

Commit

Permalink
Minor simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jan 20, 2024
1 parent 96813f9 commit 01bda2f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 5 additions & 3 deletions v2/entropy/ANSRangeCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (this *ANSRangeEncoder) updateFrequencies(frequencies []int, lr uint) (int,
}
}

if err = this.encodeHeader(alphabetSize, alphabet[:], f, lr); err != nil {
if err = this.encodeHeader(alphabet[0:alphabetSize], f, lr); err != nil {
break
}

Expand All @@ -221,11 +221,13 @@ func (this *ANSRangeEncoder) updateFrequencies(frequencies []int, lr uint) (int,
}

// Encodes alphabet and frequencies into the bitstream
func (this *ANSRangeEncoder) encodeHeader(alphabetSize int, alphabet []int, frequencies []int, lr uint) error {
if _, err := EncodeAlphabet(this.bitstream, alphabet[0:alphabetSize:256]); err != nil {
func (this *ANSRangeEncoder) encodeHeader(alphabet []int, frequencies []int, lr uint) error {
if _, err := EncodeAlphabet(this.bitstream, alphabet); err != nil {
return err
}

alphabetSize := len(alphabet)

if alphabetSize <= 1 {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions v2/entropy/EntropyUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (this sortByFreq) Swap(i, j int) {

// EncodeAlphabet writes the alphabet to the bitstream and return the number
// of symbols written or an error.
// alphabet must be sorted in increasing order
// alphabet must be composed of values in [0..255] sorted in increasing order
// alphabet size must be a power of 2 up to 256
func EncodeAlphabet(obs kanzi.OutputBitStream, alphabet []int) (int, error) {
alphabetSize := cap(alphabet)
Expand Down Expand Up @@ -164,8 +164,8 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in

// Shortcut
if totalFreq == scale {
for i, f := range freqs {
if f != 0 {
for i := 0; i < 256; i++ {
if freqs[i] != 0 {
alphabet[alphabetSize] = i
alphabetSize++
}
Expand Down
8 changes: 5 additions & 3 deletions v2/entropy/RangeCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ func (this *RangeEncoder) updateFrequencies(frequencies []int, size int, lr uint
}
}

err = this.encodeHeader(alphabetSize, this.alphabet[:], frequencies, lr)
err = this.encodeHeader(this.alphabet[0:alphabetSize], frequencies, lr)
return alphabetSize, err
}

func (this *RangeEncoder) encodeHeader(alphabetSize int, alphabet []int, frequencies []int, lr uint) error {
if _, err := EncodeAlphabet(this.bitstream, alphabet[0:alphabetSize]); err != nil {
func (this *RangeEncoder) encodeHeader(alphabet []int, frequencies []int, lr uint) error {
if _, err := EncodeAlphabet(this.bitstream, alphabet); err != nil {
return err
}

alphabetSize := len(alphabet)

if alphabetSize == 0 {
return nil
}
Expand Down

0 comments on commit 01bda2f

Please sign in to comment.