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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
type context struct {
table [256]byte
}
// Encodes data in place
func (c *context) encode(data []byte) {
for index, value := range data {
// Shortcut for sequential, equal values
if c.table[0] == value {
data[index] = 0
continue
}
// Loop over the MTF table
for j := byte(1); j != 0; j++ {
if c.table[j] == value {
// Output the value
data[index] = j
// Shift the table
copy(c.table[1:], c.table[:j])
// Restore the value in front and break
c.table[0] = value
break
}
}
}
}
// Decode data in place
func (c *context) decode(data []byte) {
for index, value := range data {
position := value
// Shortcut for sequential, equal values
if position == 0 {
data[index] = c.table[0]
continue
}
// Output the value
data[index] = c.table[position]
// Shift the table and restore the value in front
copy(c.table[1:], c.table[:position])
c.table[0] = data[index]
}
|
<
<
<
<
<
<
<
|
|
|
|
<
<
<
<
<
<
|
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
53
54
55
56
57
58
59
60
61
62
63
|
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
func (c *context) decode(data []byte) {
for index, value := range data {
position := value
// Output the value
data[index] = c.table[position]
// Shift the table and restore the value in front
copy(c.table[1:], c.table[:position])
c.table[0] = data[index]
}
|