27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
type context struct {
table [256]byte
}
// Encodes data in place
func (c *context) encode(data []byte) {
for index, value := range data {
// Loop over the MTF table
for i := byte(0); i <= 255; i++ {
if c.table[i] == value {
// Output the value
data[index] = i
// Shift the table
copy(c.table[1:], c.table[:i])
// Restore the value in front and break
c.table[0] = value
break
}
}
}
}
// Decode data in place
|
|
|
|
|
|
|
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
type context struct {
table [256]byte
}
// Encodes data in place
func (c *context) encode(data []byte) {
for dataIndex, dataValue := range data {
// Loop over the MTF table
for tableIndex, tableValue := range c.table {
if tableValue == dataValue {
// Output the value
data[dataIndex] = byte(tableIndex)
// Shift the table
copy(c.table[1:], c.table[:tableIndex])
// Restore the value in front and break
c.table[0] = dataValue
break
}
}
}
}
// Decode data in place
|