Created
January 22, 2018 17:01
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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