353fef3ff6
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
64 lines
878 B
Go
64 lines
878 B
Go
package rlepluslazy
|
|
|
|
type rbitvec struct {
|
|
index int
|
|
|
|
bits uint64
|
|
bitCap byte
|
|
|
|
vec []byte
|
|
}
|
|
|
|
func readBitvec(vec []byte) *rbitvec {
|
|
bv := &rbitvec{vec: vec}
|
|
for n := 7; n >= 0; n-- {
|
|
var o uint64
|
|
if len(bv.vec) > n {
|
|
o = uint64(bv.vec[n])
|
|
}
|
|
|
|
bv.bits = bv.bits<<8 | o
|
|
bv.index++
|
|
}
|
|
bv.bitCap = 64
|
|
return bv
|
|
}
|
|
|
|
const (
|
|
minCap = 8
|
|
)
|
|
|
|
var bitMasks = [9]byte{
|
|
0x0,
|
|
0x1,
|
|
0x3,
|
|
0x7,
|
|
0xF,
|
|
0x1F,
|
|
0x3F,
|
|
0x7F,
|
|
0xFF,
|
|
}
|
|
|
|
func (bv *rbitvec) Get(count byte) byte {
|
|
res := byte(bv.bits) & bitMasks[count]
|
|
bv.bits = bv.bits >> count
|
|
bv.bitCap = bv.bitCap - count
|
|
|
|
if bv.bitCap < minCap {
|
|
var add uint64
|
|
for n := 6; n >= 0; n-- {
|
|
var o uint64
|
|
if len(bv.vec) > bv.index+n {
|
|
o = uint64(bv.vec[bv.index+n])
|
|
}
|
|
|
|
add = add<<8 | o
|
|
}
|
|
bv.index = bv.index + 7
|
|
bv.bits = bv.bits | add<<(bv.bitCap)
|
|
bv.bitCap = bv.bitCap + 7*8
|
|
}
|
|
return res
|
|
}
|