-
Notifications
You must be signed in to change notification settings - Fork 13
/
vbyte.h
353 lines (319 loc) · 10.3 KB
/
vbyte.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (C) 2005-2016 Christoph Rupp (chris@crupp.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A fast implementation for the Variable Byte encoding.
*
* See the README.md file for more information, example code and references.
*
* Feel free to send comments/questions to chris@crupp.de. I am available
* for consulting.
*/
#ifndef VBYTE_H_ee452711_c856_416d_82f4_e12eef8a49be
#define VBYTE_H_ee452711_c856_416d_82f4_e12eef8a49be
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Calculates the size (in bytes) of a compressed stream of sorted 32bit
* integers.
*
* This calculation is relatively expensive. As a cheap estimate, simply
* multiply the number of integers by 5.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*/
extern size_t
vbyte_compressed_size_sorted32(const uint32_t *in, size_t length,
uint32_t previous);
/**
* Calculates the size (in bytes) of a compressed stream of sorted 64bit
* integers.
*
* This calculation is relatively expensive. As a cheap estimate, simply
* multiply the number of integers by 5.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*/
extern size_t
vbyte_compressed_size_sorted64(const uint64_t *in, size_t length,
uint64_t previous);
/**
* Calculates the size (in bytes) of a compressed stream of unsorted 32bit
* integers.
*
* This calculation is relatively expensive. As a cheap estimate, simply
* multiply the number of integers by 5.
*
* This function does NOT use delta encoding.
*/
extern size_t
vbyte_compressed_size_unsorted32(const uint32_t *in, size_t length);
/**
* Calculates the size (in bytes) of a compressed stream of unsorted 64bit
* integers.
*
* This calculation is relatively expensive. As a cheap estimate, simply
* multiply the number of integers by 10.
*
* This function does NOT use delta encoding.
*/
extern size_t
vbyte_compressed_size_unsorted64(const uint64_t *in, size_t length);
/**
* Compresses an unsorted sequence of |length| 32bit unsigned integers
* at |in| and stores the result in |out|.
*
* This function does NOT use delta encoding.
*/
extern size_t
vbyte_compress_unsorted32(const uint32_t *in, uint8_t *out, size_t length);
/**
* Compresses an unsorted sequence of |length| 64bit unsigned integers
* at |in| and stores the result in |out|.
*
* This function does NOT use delta encoding.
*/
extern size_t
vbyte_compress_unsorted64(const uint64_t *in, uint8_t *out, size_t length);
/**
* Compresses a sorted sequence of |length| 32bit unsigned integers
* at |in| and stores the result in |out|.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*/
extern size_t
vbyte_compress_sorted32(const uint32_t *in, uint8_t *out, uint32_t previous,
size_t length);
/**
* Compresses a sorted sequence of |length| 64bit unsigned integers
* at |in| and stores the result in |out|.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*/
extern size_t
vbyte_compress_sorted64(const uint64_t *in, uint8_t *out, uint64_t previous,
size_t length);
/**
* Uncompresses a sequence of |length| 32bit unsigned integers at |in|
* and stores the result in |out|.
*
* This is the equivalent of |vbyte_compress_unsorted32|. It does NOT use
* delta encoding.
*
* Returns the number of compressed bytes processed.
*/
extern size_t
vbyte_uncompress_unsorted32(const uint8_t *in, uint32_t *out, size_t length);
/**
* Uncompresses a sequence of |length| 64bit unsigned integers at |in|
* and stores the result in |out|.
*
* This is the equivalent of |vbyte_compress_unsorted64|. It does NOT use
* delta encoding.
*
* Returns the number of compressed bytes processed.
*/
extern size_t
vbyte_uncompress_unsorted64(const uint8_t *in, uint64_t *out, size_t length);
/**
* Uncompresses a sequence of |length| 32bit unsigned integers at |in|
* and stores the result in |out|.
*
* This is the equivalent of |vbyte_compress_sorted32|.
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*
* Returns the number of compressed bytes processed.
*/
extern size_t
vbyte_uncompress_sorted32(const uint8_t *in, uint32_t *out, uint32_t previous,
size_t length);
/**
* Uncompresses a sequence of |length| 64bit unsigned integers at |in|
* and stores the result in |out|.
*
* This is the equivalent of |vbyte_compress_sorted64|.
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*
* Returns the number of compressed bytes processed.
*/
extern size_t
vbyte_uncompress_sorted64(const uint8_t *in, uint64_t *out, uint64_t previous,
size_t length);
/**
* Returns the value at the given |index| from a sequence of compressed
* 32bit integers.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*
* |size| is the size of the byte array pointed to by |in|.
* Make sure that |index| does not exceed the length of the sequence.
*/
extern uint32_t
vbyte_select_sorted32(const uint8_t *in, size_t size, uint32_t previous,
size_t index);
/**
* Returns the value at the given |index| from a sequence of compressed
* 64bit integers.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*
* |size| is the size of the byte array pointed to by |in|.
* Make sure that |index| does not exceed the length of the sequence.
*/
extern uint64_t
vbyte_select_sorted64(const uint8_t *in, size_t size, uint64_t previous,
size_t index);
/**
* Returns the value at the given |index| from a sequence of compressed
* 32bit integers.
*
* This routine does NOT use delta compression.
*
* |size| is the size of the byte array pointed to by |in|.
* Make sure that |index| does not exceed the length of the sequence.
*/
extern uint32_t
vbyte_select_unsorted32(const uint8_t *in, size_t size, size_t index);
/**
* Returns the value at the given |index| from a sequence of compressed
* 64bit integers.
*
* This routine does NOT use delta compression.
*
* |size| is the size of the byte array pointed to by |in|.
* Make sure that |index| does not exceed the length of the sequence.
*/
extern uint64_t
vbyte_select_unsorted64(const uint8_t *in, size_t size, size_t index);
/**
* Performs a linear search for |value| in a sequence of compressed 32bit
* unsigned integers.
*
* This function does NOT use delta encoding.
*
* Returns the index of the found element, or |length| if the key was not
* found.
*/
extern size_t
vbyte_search_unsorted32(const uint8_t *in, size_t length, uint32_t value);
/**
* Performs a linear search for |value| in a sequence of compressed 64bit
* unsigned integers.
*
* This function does NOT use delta encoding.
*
* Returns the index of the found element, or |length| if the key was not
* found.
*/
extern size_t
vbyte_search_unsorted64(const uint8_t *in, size_t length, uint64_t value);
/**
* Performs a lower-bound search for |value| in a sequence of compressed 32bit
* unsigned integers.
*
* A lower bound search returns the first element in the sequence which does
* not compare less than |value|.
* The actual result is stored in |*actual|.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*
* Returns the index of the found element, or |length| if the key was not
* found.
*/
extern size_t
vbyte_search_lower_bound_sorted32(const uint8_t *in, size_t length,
uint32_t value, uint32_t previous, uint32_t *actual);
/**
* Performs a lower-bound search for |value| in a sequence of compressed 64bit
* unsigned integers.
*
* A lower bound search returns the first element in the sequence which does
* not compare less than |value|.
* The actual result is stored in |*actual|.
*
* This function uses delta encoding. Set |previous| to the initial value,
* or 0.
*
* Returns the index of the found element, or |length| if the key was not
* found.
*/
extern size_t
vbyte_search_lower_bound_sorted64(const uint8_t *in, size_t length,
uint64_t value, uint64_t previous, uint64_t *actual);
/**
* Appends |value| to a sequence of compressed 32bit unsigned integers.
*
* |end| is a pointer to the end of the compressed sequence (the first byte
* AFTER the compressed data).
* |previous| is the greatest encoded value in the sequence.
*
* This function uses delta encoding.
*
* Returns the number of bytes required to compress |value|.
*/
extern size_t
vbyte_append_sorted32(uint8_t *end, uint32_t previous, uint32_t value);
/**
* Appends |value| to a sequence of compressed 64bit unsigned integers.
*
* |end| is a pointer to the end of the compressed sequence (the first byte
* AFTER the compressed data).
* |previous| is the greatest encoded value in the sequence.
*
* This function uses delta encoding.
*
* Returns the number of bytes required to compress |value|.
*/
extern size_t
vbyte_append_sorted64(uint8_t *end, uint64_t previous, uint64_t value);
/**
* Appends |value| to a sequence of compressed 32bit unsigned integers.
*
* |end| is a pointer to the end of the compressed sequence (the first byte
* AFTER the compressed data).
*
* This function does NOT use encoding.
*
* Returns the number of bytes required to compress |value|.
*/
extern size_t
vbyte_append_unsorted32(uint8_t *end, uint32_t value);
/**
* Appends |value| to a sequence of compressed 64bit unsigned integers.
*
* |end| is a pointer to the end of the compressed sequence (the first byte
* AFTER the compressed data).
*
* This function does NOT use encoding.
*
* Returns the number of bytes required to compress |value|.
*/
extern size_t
vbyte_append_unsorted64(uint8_t *end, uint64_t value);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* VBYTE_H_ee452711_c856_416d_82f4_e12eef8a49be */