Skip to content

Commit 12cd26d

Browse files
committed
init
0 parents  commit 12cd26d

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# simple8b的Go实现
2+
3+
TODO 2022-11-28 02:52:45 测试
4+
5+
6+

errors.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package simple8b
2+
3+
import "errors"
4+
5+
var ErrFormatNotOk = errors.New("format not ok")

go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/compression-algorithm-research-lab/go-simple8b
2+
3+
go 1.18
4+
5+
require (
6+
github.com/compression-algorithm-research-lab/go-varint v0.0.0-20221127141716-08e64be62856 // indirect
7+
github.com/compression-algorithm-research-lab/go-zigzag v0.0.0-20221127181654-f3180667f35f // indirect
8+
github.com/golang-infrastructure/go-gtypes v0.0.1 // indirect
9+
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/compression-algorithm-research-lab/go-varint v0.0.0-20221127141716-08e64be62856 h1:9JTAw9roxGXImEQFVhmrigg+GzpWK6ASyXuoYnskXgI=
2+
github.com/compression-algorithm-research-lab/go-varint v0.0.0-20221127141716-08e64be62856/go.mod h1:QzcFfo4K24GHMgyvTF0iQJDFYQG9LiMbRK3dFxCPOP0=
3+
github.com/compression-algorithm-research-lab/go-zigzag v0.0.0-20221127181654-f3180667f35f h1:tYJkI782TigIodHjv14pxCJwzX75/eFY0V5la+9Dkr4=
4+
github.com/compression-algorithm-research-lab/go-zigzag v0.0.0-20221127181654-f3180667f35f/go.mod h1:xEIqHDARrh2uoSNToTKjePD67VMdJrLjvBXeydujh3Y=
5+
github.com/golang-infrastructure/go-gtypes v0.0.1 h1:hnM1OYSwLPLGkZ4C6ecAxgmAUaPTjnhnUtRNmJj4p6c=
6+
github.com/golang-infrastructure/go-gtypes v0.0.1/go.mod h1:vFMCxFzxdMInvTtgLZRlWI1rS+mui88sMbL5I+zu1hg=

simple8b.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

simple8b_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package simple8b
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDecodeE(t *testing.T) {
8+
9+
slice := []int{
10+
1, 2, 3, 4, 100, -1, -2, -3, -4,
11+
}
12+
bytes := Encode(slice)
13+
t.Log(bytes)
14+
15+
decodeSlice, err := DecodeE[int](bytes)
16+
t.Log(err)
17+
t.Log(decodeSlice)
18+
19+
}

0 commit comments

Comments
 (0)