-
Notifications
You must be signed in to change notification settings - Fork 5
/
for.h
293 lines (269 loc) · 9.42 KB
/
for.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
/*
* Copyright (C) 2005-2015 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 Frame of Reference 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 FOR_H_5580af15_4570_41f9_ba2b_8afb1400e81e
#define FOR_H_5580af15_4570_41f9_ba2b_8afb1400e81e
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Returns the size required to compress a sequence of |length| ints,
* each compressed with |bits| bits
*
* This function will NOT include any overhead required by
* for_compress_sorted() and for_compress_unsorted().
*
* Invariant: bits <= 32
*/
extern uint32_t
for_compressed_size_bits(uint32_t length, uint32_t bits);
/**
* Returns the size required to compress an unsorted sequence of |length| ints.
*
* This routine scans |in| for the min/max values and then calls
* for_compressed_size_bits().
*
* The returned size will include the overhead required for
* for_compress_sorted() and for_compressed_unsorted().
*/
extern uint32_t
for_compressed_size_unsorted(const uint32_t *in, uint32_t length);
/**
* Returns the size required to compress a sorted sequence of |length| ints.
*
* This routine extracts min/max values at the beginning and end of
* the sequence, then calls for_compressed_size_bits(). It is therefore
* slightly faster than for_compressed_size_unsorted().
*
* The returned size will include the overhead required for
* for_compress_sorted() and for_compressed_unsorted().
*/
extern uint32_t
for_compressed_size_sorted(const uint32_t *in, uint32_t length);
/**
* Compresses a sequence of |length| ints at |in| and stores the result
* in |out|.
*
* |base| is the "offset" (or common delta value) of all ints. It is usually
* set to the minimum value of the uncompressed sequence.
*
* |bits| are the bits required to store a single integer.
*
* Returns the number of bytes used for compression.
*
* This is for advanced users who opt for storing |base| and |bits| on their
* own. This function is called by for_compress_sorted() and
* for_compress_unsorted().
*
* Invariant: bits <= 32
*/
extern uint32_t
for_compress_bits(const uint32_t *in, uint8_t *out, uint32_t length,
uint32_t base, uint32_t bits);
/**
* Compresses an unsorted sequence of |length| ints at |in| and stores the
* result in |out|.
*
* This routine scans |in| for the min/max values and then calls
* for_compress_bits().
*
* The minimun value and the bits are stored as metadata in |out|.
*/
extern uint32_t
for_compress_unsorted(const uint32_t *in, uint8_t *out, uint32_t length);
/**
* Compresses a sorted sequence of |length| ints at |in| and stores the
* result in |out|.
*
* This routine extracts min/max values at the beginning and end of
* the sequence, then calls for_compress_bits().
*
* The minimun value and the bits are stored as metadata in |out|.
*/
extern uint32_t
for_compress_sorted(const uint32_t *in, uint8_t *out, uint32_t length);
/**
* Uncompresses a sequence of |length| ints at |in| and stores the
* result in |out|.
*
* |base| is the "offset" (or common delta value) of all ints. It is usually
* set to the minimum value of the uncompressed sequence.
*
* |bits| are the bits required to store a single integer.
*
* Returns the number of compressed bytes processed.
*
* This function is for advanced users. It is the counterpart of
* for_compress_bits().
*
* Invariant: bits <= 32
*/
extern uint32_t
for_uncompress_bits(const uint8_t *in, uint32_t *out, uint32_t length,
uint32_t base, uint32_t bits);
/**
* Uncompresses a sequence of |length| ints at |in| and stores the
* result in |out|.
*
* This function is a convenience wrapper for for_uncompress_bits(). It
* expects metadata at the beginning of |in|. Use in combination with
* for_compress_sorted() and for_compress_unsorted().
*
* Returns the number of compressed bytes processed.
*/
extern uint32_t
for_uncompress(const uint8_t *in, uint32_t *out, uint32_t length);
/**
* Appends a |value| to a compressed sequence of unsorted integers.
*
* This function is optimized for appending new values at the end of an
* encoded sequence. This is only possible if the new value (more precisely:
* the delta of the new value) can be stored in the same amount of bits that
* were used to encode the other integers.
*
* If this is not the case then memory is allocated, the whole sequence is
* decoded and re-encoded using more bits. This requires a heap allocation
* with malloc().
*
* Returns the size (in bytes) of the compressed data, or 0 if malloc() fails.
*/
extern uint32_t
for_append_unsorted(uint8_t *in, uint32_t length, uint32_t value);
/**
* Appends a |value| to a compressed sequence of sorted integers.
*
* This function is optimized for appending new values at the end of an
* encoded sequence. This is only possible if the new value (more precisely:
* the delta of the new value) can be stored in the same amount of bits that
* were used to encode the other integers.
*
* If this is not the case then memory is allocated, the whole sequence is
* decoded and re-encoded using more bits. This requires a heap allocation
* with malloc().
*
* Returns the size (in bytes) of the compressed data, or 0 if malloc() fails.
*/
extern uint32_t
for_append_sorted(uint8_t *in, uint32_t length, uint32_t value);
/**
* Appends a |value| to a compressed integer sequence.
*
* |base| is the "offset" (or common delta value) of all ints. It is usually
* set to the minimum value of the uncompressed sequence.
*
* |bits| are the bits required to store a single integer.
*
* Returns the size (in bytes) of the compressed data.
*
* Invariant: bits <= 32
* Invariant: the new |value| (more precisely: |value - base|) can be stored
* in |bits| bits. Details can be found in the implementation of
* for_append() in for.c.
*/
extern uint32_t
for_append_bits(uint8_t *in, uint32_t length, uint32_t base,
uint32_t bits, uint32_t value);
/**
* Returns the value at the given |index| from a compressed sequence.
*
* Make sure that |index| does not exceed the length of the sequence.
*
* |base| is the "offset" (or common delta value) of all ints. It is usually
* set to the minimum value of the uncompressed sequence.
*
* Invariant: bits <= 32
*/
extern uint32_t
for_select_bits(const uint8_t *in, uint32_t base, uint32_t bits,
uint32_t index);
/**
* Returns the value at the given |index| from a compressed sequence.
*
* Make sure that |index| does not exceed the length of the sequence.
*
* This function is a convenience wrapper for for_select_bits(). It
* expects metadata at the beginning of |in|. Use in combination with
* for_compress_sorted() and for_compress_unsorted().
*/
extern uint32_t
for_select(const uint8_t *in, uint32_t index);
/**
* Performs a linear search for |value|.
*
* Returns the index of the found element, or |length| if the key was not
* found.
*
* This function is a convenience wrapper for for_linear_search_bits(). It
* expects metadata at the beginning of |in|. Use in combination with
* for_compress_sorted() and for_compress_unsorted().
*/
extern uint32_t
for_linear_search(const uint8_t *in, uint32_t length, uint32_t value);
/**
* Performs a linear search for |value|.
*
* Returns the index of the found element, or |length| if the key was not
* found.
*
* |base| is the "offset" (or common delta value) of all ints. It is usually
* set to the minimum value of the uncompressed sequence.
*
* Invariant: bits <= 32
*/
extern uint32_t
for_linear_search_bits(const uint8_t *in, uint32_t length, uint32_t base,
uint32_t bits, uint32_t value);
/**
* Performs lower bound binary search search for |value|.
*
* 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 is a convenience wrapper for for_lower_bound_search_bits(). It
* expects metadata at the beginning of |in|. Use in combination with
* for_compress_sorted() and for_compress_unsorted().
*/
extern uint32_t
for_lower_bound_search(const uint8_t *in, uint32_t length, uint32_t value,
uint32_t *actual);
/**
* Performs lower bound binary search search for |value|.
*
* 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|.
*
* |base| is the "offset" (or common delta value) of all ints. It is usually
* set to the minimum value of the uncompressed sequence.
*
* Invariant: bits <= 32
*/
extern uint32_t
for_lower_bound_search_bits(const uint8_t *in, uint32_t length, uint32_t base,
uint32_t bits, uint32_t value, uint32_t *actual);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* FOR_H_5580af15_4570_41f9_ba2b_8afb1400e81e */