1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
7
// moveToFrontDecoder implements a move-to-front list. Such a list is an
8
// efficient way to transform a string with repeating elements into one with
9
// many small valued numbers, which is suitable for entropy encoding. It works
10
// by starting with an initial list of symbols and references symbols by their
11
// index into that list. When a symbol is referenced, it's moved to the front
12
// of the list. Thus, a repeated symbol ends up being encoded with many zeros,
13
// as the symbol will be at the front of the list after the first access.
14
type moveToFrontDecoder struct {
15
// Rather than actually keep the list in memory, the symbols are stored
16
// as a circular, double linked list with the symbol indexed by head
17
// at the front of the list.
24
// newMTFDecoder creates a move-to-front decoder with an explicit initial list
26
func newMTFDecoder(symbols []byte) *moveToFrontDecoder {
27
if len(symbols) > 256 {
28
panic("too many symbols")
31
m := &moveToFrontDecoder{
33
next: make([]uint8, len(symbols)),
34
prev: make([]uint8, len(symbols)),
41
// newMTFDecoderWithRange creates a move-to-front decoder with an initial
42
// symbol list of 0...n-1.
43
func newMTFDecoderWithRange(n int) *moveToFrontDecoder {
45
panic("newMTFDecoderWithRange: cannot have > 256 symbols")
48
m := &moveToFrontDecoder{
49
symbols: make([]uint8, n),
50
next: make([]uint8, n),
51
prev: make([]uint8, n),
54
for i := 0; i < n; i++ {
55
m.symbols[i] = byte(i)
62
// threadLinkedList creates the initial linked-list pointers.
63
func (m *moveToFrontDecoder) threadLinkedList() {
64
if len(m.symbols) == 0 {
68
m.prev[0] = uint8(len(m.symbols) - 1)
70
for i := 0; i < len(m.symbols)-1; i++ {
71
m.next[i] = uint8(i + 1)
72
m.prev[i+1] = uint8(i)
75
m.next[len(m.symbols)-1] = 0
78
func (m *moveToFrontDecoder) Decode(n int) (b byte) {
79
// Most of the time, n will be zero so it's worth dealing with this
82
return m.symbols[m.head]
86
for j := 0; j < n; j++ {
91
m.next[m.prev[i]] = m.next[i]
92
m.prev[m.next[i]] = m.prev[i]
94
m.prev[i] = m.prev[m.head]
95
m.next[m.prev[m.head]] = i
102
// First returns the symbol at the front of the list.
103
func (m *moveToFrontDecoder) First() byte {
104
return m.symbols[m.head]