-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
231 lines (185 loc) · 4.22 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package fibvec
import (
"math"
"reflect"
"unsafe"
"github.com/robskie/bit"
)
// Maximum and minimum value that can be encoded.
const (
MaxValue = math.MaxInt64 - 3
MinValue = -MaxValue
)
type decRecord struct {
// shift contains the size of
// the partially decoded value
shift uint8
// numbers contains the fully
// decoded values from a byte
numbers []uint8
// incomplete contains
// partially decoded value
incomplete uint8
}
type encRecord struct {
code uint8
length uint8
nmin uint
nmax uint
}
// rfibshift8 returns the right fibonacci shift
// of n., ie., V(F(n) >>f k) where n is V(F(n))
// as long as k is a multiple of 8.
func rfibshift8(n uint, shift int) uint {
const phi = 1.618033989
est := float64(n) / math.Pow(phi, float64(shift))
res := uint(est + 0.5)
res--
rec := fencTable[shift/8][res]
if n > rec.nmax {
res++
}
return res
}
// fibencode encodes n to
// its fibonacci coded value.
//
// See Fast Fibonnaci Encoding Algorithm
// by Platos et al. for more details.
func fibencode(n uint) ([]uint64, int) {
res := bit.NewArray(128)
res.Add(1, 1)
// Add 2 to n so that the minimum encoded
// value would be 011 to make sure that there
// is no more than 3 consecutive 1s in the bit
// array which makes it easier to count pairs of 1s.
n += 2
k := 8
f := fib[1:]
var code uint8
var rec encRecord
if n < f[k] {
rec = fencTable[0][n]
code = rec.code >> uint(8-rec.length)
res.Add(uint64(code), int(rec.length))
return res.Bits(), res.Len()
}
for n >= f[k+8] {
k += 8
}
i := rfibshift8(n, k)
rec = fencTable[k/8][i]
code = rec.code >> uint(8-rec.length)
res.Add(uint64(code), int(rec.length))
n -= rec.nmin
for k > 8 {
k -= 8
if n >= f[k] {
i = rfibshift8(n, k)
rec = fencTable[k/8][i]
res.Add(uint64(rec.code), 8)
n -= rec.nmin
} else {
res.Add(0, 8)
}
}
rec = fencTable[0][n]
res.Add(uint64(rec.code), 8)
return res.Bits(), res.Len()
}
// fibdecode decodes the input bytes given the
// number of decoded values to return.
//
// See Fast decoding algorithms for variable-length codes
// and Fast Fibonacci Decompression Algorithm by Platos et al.
func fibdecode(input []byte, count int) []int {
prevIn := input[0]
fbuffer := make([]byte, 0, 16)
prevRec := fdecTable[0][prevIn]
result := make([]int, 0, count)
for _, in := range input[1:] {
startWithOne := false
endWithOne := prevIn&0x80 != 0
rec := fdecTable[0][in]
if in&1 == 1 && rec.shift > 0 {
startWithOne = true
prevRec = fdecTable[1][prevIn]
}
prevIn = in
shift := int(prevRec.shift)
if shift > 0 {
fbuffer = append(fbuffer, prevRec.incomplete)
}
dec := uint(0)
for _, num := range prevRec.numbers {
if shift == 0 {
dec = decodeBuffer(fbuffer, 8)
} else {
dec = decodeBuffer(fbuffer, shift)
}
fbuffer = fbuffer[:0]
if dec > 1 {
// Subtract 2 to cancel out
// what is added during encoding
result = append(result, fromSignMagnitude(dec-2))
if len(result) == count {
return result
}
}
shift = 0
fbuffer = append(fbuffer, num)
}
if startWithOne && endWithOne {
dec = decodeBuffer(fbuffer, 7)
fbuffer = fbuffer[:0]
if dec > 1 {
result = append(result, fromSignMagnitude(dec-2))
if len(result) == count {
return result
}
}
}
prevRec = rec
}
return result
}
func decodeBuffer(fbuffer []byte, shift int) uint {
n := len(fbuffer)
if n == 0 {
return 0
}
sum := uint(fbuffer[n-1])
for i := n - 2; i >= 0; i-- {
fb := fbuffer[i]
sum += lfibshift(uint(fb), shift)
shift += 8
}
return sum
}
// lfibshift performs fibonacci left shift
// on n, ie., V(F(n) <<f k) where n is V(F(n))
func lfibshift(n uint, shift int) uint {
return (fib[shift] * n) + (fib[shift-1] * uint(vf1[n]))
}
func byteSliceFromUint64Slice(bits []uint64) []byte {
sh := &reflect.SliceHeader{}
sh.Cap = cap(bits) * 8
sh.Len = len(bits) * 8
sh.Data = (uintptr)(unsafe.Pointer(&bits[0]))
bytes := *(*[]uint8)(unsafe.Pointer(sh))
return bytes
}
func toSignMagnitude(v int) uint {
const mask = ^(^uint(0) >> 1)
if v < 0 {
return uint(-v) | mask
}
return uint(v)
}
func fromSignMagnitude(v uint) int {
const mask = ^(^uint(0) >> 1)
if v&mask == mask {
return -int(v & ^mask)
}
return int(v)
}