-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
127 lines (111 loc) · 5.16 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright (c) 2020 Brian M. Kessler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
Package streamvbyte implements integer compression using the Stream VByte algorithm.
"Stream VByte: Faster Byte-Oriented Integer Compression"
Daniel Lemire, Nathan Kurz, Christoph Rupp
Information Processing Letters
Volume 130, February 2018, Pages 1-6
https://arxiv.org/abs/1709.08990
The focus of this method is fast decoding speed on processors with
SIMD instructions. As such, this package contains fast assembly implementations
for decoding on amd64 processors with SSE3 instructions. Other processors
will use the slower pure go implementation.
Assembly implementations were generated using the excellent avo package https://github.com/mmcloughlin/avo
*/
package streamvbyte
// MaxSize32 returns the maximum possible size of an encoded
// slice of 32-bit integers. Usage:
//
// encoded := make([]byte, MaxSize32(len(data)))
//
// This will ensure that the slice is large enough to hold the
// encoded data in the worst case of no compression.
func MaxSize32(length int) int {
numControlBytes := (length + 3) / 4
maxNumDataBytes := 4 * length
return numControlBytes + maxNumDataBytes
}
// EncodeUint32 encodes data using the Stream VByte
// algorithm into encoded and returns the encoded size.
// This function assumes that the size of encoded is sufficient to hold the
// compressed data. Use
// encoded := make([]byte, MaxSize32(len(data)))
// to obtain a worst case size.
func EncodeUint32(encoded []byte, data []uint32) int {
return encodeUint32scalar(encoded, data)
}
// DecodeUint32 decodes len(data) uint32 from encoded using the Stream
// Vbyte algorithm. encoded must contain exactly len(data) encoded uint32.
func DecodeUint32(data []uint32, encoded []byte) {
decodeUint32(data, encoded)
}
// EncodeDeltaUint32 encodes data using the Stream VByte
// algorithm and delta encoding with a step size of 1, i.e. it encodes
// delta[n] = data[n] - data[n-1],
// where the initial value
// data[-1] := previous
// The return value is the encoded size. This function assumes
// that the size of encoded is sufficient to hold the
// compressed data. Use
// encoded := make([]byte, MaxSize32(len(data)))
// to obtain a worst case size.
func EncodeDeltaUint32(encoded []byte, data []uint32, previous uint32) int {
return encodeDeltaUint32scalar(encoded, data, previous)
}
// DecodeDeltaUint32 decodes len(data) uint32 from encoded using the Stream
// Vbyte algorithm with delta encoding using the initial value previous.
// encoded must contain exactly len(data) encoded uint32.
func DecodeDeltaUint32(data []uint32, encoded []byte, previous uint32) {
decodeDeltaUint32(data, encoded, previous)
}
// EncodeInt32 encodes data using the Stream VByte
// algorithm with zigzag encoding into encoded and returns the encoded size.
// This function assumes that the size of encoded is sufficient to hold the
// compressed data. Use
// encoded := make([]byte, MaxSize32(len(data)))
// to obtain a worst case size.
func EncodeInt32(encoded []byte, data []int32) int {
return encodeInt32scalar(encoded, data)
}
// DecodeInt32 decodes len(data) int32 from encoded using the Stream
// Vbyte algorithm. encoded must contain exactly len(data) encoded int32.
func DecodeInt32(data []int32, encoded []byte) {
decodeInt32(data, encoded)
}
// EncodeDeltaInt32 encodes data using the Stream VByte
// algorithm and delta encoding with a step size of 1, i.e. it encodes
// delta[n] = data[n] - data[n-1]
// where the initial value
// data[-1] := previous
// followed by zigzag encoding the deltas.
// The return value is the encoded size. This function assumes
// that the size of encoded is sufficient to hold the
// compressed data. Use
// encoded := make([]byte, MaxSize32(len(data)))
// to obtain a worst case size.
func EncodeDeltaInt32(encoded []byte, data []int32, previous int32) int {
return encodeDeltaInt32scalar(encoded, data, previous)
}
// DecodeDeltaInt32 decodes len(data) int32 from encoded using the Stream
// Vbyte algorithm with delta and zigzag encoding using the initial value previous.
// encoded must contain exactly len(data) encoded uint32.
func DecodeDeltaInt32(data []int32, encoded []byte, previous int32) {
decodeDeltaInt32(data, encoded, previous)
}