|
| 1 | +package simple8b |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/compression-algorithm-research-lab/go-zigzag" |
| 5 | + "github.com/golang-infrastructure/go-gtypes" |
| 6 | +) |
| 7 | + |
| 8 | +// CanZipToBits 判断可以压缩到几个bit |
| 9 | +func CanZipToBits[T gtypes.Integer](slice []T) int { |
| 10 | + var sum T |
| 11 | + for _, value := range slice { |
| 12 | + sum |= zigzag.ToZigZag(value) |
| 13 | + } |
| 14 | + bitCount := 0 |
| 15 | + for sum > 0 { |
| 16 | + bitCount++ |
| 17 | + sum >>= 1 |
| 18 | + } |
| 19 | + return bitCount |
| 20 | +} |
| 21 | + |
| 22 | +// Encode 编码 |
| 23 | +func Encode[T gtypes.Integer](slice []T) []byte { |
| 24 | + |
| 25 | + result := make([]byte, 0) |
| 26 | + |
| 27 | + // 先计算一下最少可以压缩到几位 |
| 28 | + bits := CanZipToBits(slice) |
| 29 | + blockSize := (bits + 7) / 8 |
| 30 | + result = append(result, uint8(blockSize)) |
| 31 | + |
| 32 | + // 然后就按照这个来存储了 |
| 33 | + for _, value := range slice { |
| 34 | + result = append(result, IntToBytes(zigzag.ToZigZag(value), blockSize)...) |
| 35 | + } |
| 36 | + |
| 37 | + return result |
| 38 | +} |
| 39 | + |
| 40 | +// DecodeE 解码 |
| 41 | +func DecodeE[T gtypes.Integer](bytes []byte) ([]T, error) { |
| 42 | + |
| 43 | + // 先读取是一个块 |
| 44 | + if len(bytes) == 0 { |
| 45 | + return nil, ErrFormatNotOk |
| 46 | + } |
| 47 | + blockSize := BytesToInt[int]([]byte{bytes[0]}) |
| 48 | + // 进行长度校验,康康块是不是都是合法的 |
| 49 | + if (len(bytes)-1)%blockSize != 0 { |
| 50 | + return nil, ErrFormatNotOk |
| 51 | + } |
| 52 | + |
| 53 | + // 然后就一块一块的读取 |
| 54 | + result := make([]T, 0) |
| 55 | + count := (len(bytes) - 1) / blockSize |
| 56 | + for i := 0; i < count; i++ { |
| 57 | + valueIndex := 1 + i*blockSize |
| 58 | + valueBytes := bytes[valueIndex : valueIndex+blockSize] |
| 59 | + result = append(result, zigzag.FromToZigZag(BytesToInt[T](valueBytes))) |
| 60 | + } |
| 61 | + return result, nil |
| 62 | +} |
| 63 | + |
| 64 | +// IntToBytes 把给定的整数的低n位转换为字节数组 |
| 65 | +func IntToBytes[T gtypes.Integer](value T, blockSize int) []byte { |
| 66 | + result := make([]byte, blockSize) |
| 67 | + for index := range result { |
| 68 | + byteValue := (uint64(0xFF) << index) & uint64(value) |
| 69 | + result[index] = uint8(byteValue) |
| 70 | + } |
| 71 | + return result |
| 72 | +} |
| 73 | + |
| 74 | +// BytesToInt 把字节转为整数 |
| 75 | +func BytesToInt[T gtypes.Integer](bytes []byte) T { |
| 76 | + var r uint64 |
| 77 | + weight := 0 |
| 78 | + for _, x := range bytes { |
| 79 | + r = r | (uint64(x) << weight) |
| 80 | + weight += 8 |
| 81 | + } |
| 82 | + return T(r) |
| 83 | +} |
0 commit comments