Skip to content

Instantly share code, notes, and snippets.

@agrieve
Created January 22, 2018 17:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save agrieve/25ecfe812a76e2ddbc245de2c9ed9143 to your computer and use it in GitHub Desktop.
func Decompress64(input []uint64, wordsize uint, apply func(uint)) {
var offset uint
if len(input) > 0 {
offset = uint(input[0])
input = input[1:]
apply(offset)
}
bits := uint(0)
for len(input) != 0 {
cur_word := uint(input[0])
input = input[1:]
is_rle := cur_word >> 62 == 0
is_bitmask := cur_word >> 63 == 1
if (is_rle) {
num_bits_in_pattern := (cur_word >> 56)
repeat_count := (cur_word >> 48) & 0xFF
bits = bits & ((1 << num_bits_in_pattern) - 1)
for i := uint(0); i < repeat_count; i++ {
offset = ApplyBitmask(offset, bits, num_bits_in_pattern, wordsize, apply)
}
bits = cur_word & ((1 << 48) - 1)
offset = ApplyBitmask(offset, bits, 48, wordsize, apply)
} else if (is_bitmask) {
bits = cur_word & (1<<63-1)
offset = ApplyBitmask(offset, bits, 63, wordsize, apply)
} else {
offset += (cur_word & ((1 << (wordsize * 8 - 2)) - 1)) - wordsize * 64
apply(offset)
}
}
}
func ApplyBitmask(offset uint, bits uint, num_bits uint, wordsize uint, apply func(uint)) uint {
mask := uint(1 << (num_bits - 1))
for i := uint(0); i < num_bits; i++ {
offset += wordsize
if bits & mask != 0 {
apply(offset)
}
bits <<= 1
}
return offset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment