forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple9.java
More file actions
305 lines (289 loc) · 12.2 KB
/
Simple9.java
File metadata and controls
305 lines (289 loc) · 12.2 KB
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
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
package me.lemire.integercompression;
/**
* This is an implementation of the popular Simple9 scheme. It is limited to
* 28-bit integers (between 0 and 2^28-1).
*
* Note that this does not use differential coding: if you are working on sorted
* lists, you must compute the deltas separately.
*
* @author Daniel Lemire
*
*/
public final class Simple9 implements IntegerCODEC, SkippableIntegerCODEC {
@Override
public void headlessCompress(int[] in, IntWrapper inpos, int inlength, int out[], IntWrapper outpos) {
int tmpoutpos = outpos.get();
int currentPos = inpos.get();
final int finalin = currentPos + inlength;
outer: while (currentPos < finalin - 28) {
mainloop: for (int selector = 0; selector < 8; selector++) {
int res = 0;
int compressedNum = codeNum[selector];
int b = bitLength[selector];
int max = 1 << b;
int i = 0;
for (; i < compressedNum; i++) {
if (max <= in[currentPos + i])
continue mainloop;
res = (res << b) + in[currentPos + i];
}
res |= selector << 28;
out[tmpoutpos++] = res;
currentPos += compressedNum;
continue outer;
}
final int selector = 8;
if (in[currentPos] >= 1 << bitLength[selector])
throw new RuntimeException("Too big a number");
out[tmpoutpos++] = in[currentPos++] | (selector << 28);
}
outer: while (currentPos < finalin) {
mainloop: for (int selector = 0; selector < 8; selector++) {
int res = 0;
int compressedNum = codeNum[selector];
if (finalin <= currentPos + compressedNum - 1)
compressedNum = finalin - currentPos;
int b = bitLength[selector];
int max = 1 << b;
int i = 0;
for (; i < compressedNum; i++) {
if (max <= in[currentPos + i])
continue mainloop;
res = (res << b) + in[currentPos + i];
}
if (compressedNum != codeNum[selector])
res <<= (codeNum[selector] - compressedNum) * b;
res |= selector << 28;
out[tmpoutpos++] = res;
currentPos += compressedNum;
continue outer;
}
final int selector = 8;
if (in[currentPos] >= 1 << bitLength[selector])
throw new RuntimeException("Too big a number");
out[tmpoutpos++] = in[currentPos++] | (selector << 28);
}
inpos.set(currentPos);
outpos.set(tmpoutpos);
}
@Override
public void headlessUncompress(int[] in, IntWrapper inpos, int inlength, int[] out, IntWrapper outpos,
int outlength) {
int currentPos = outpos.get();
int tmpinpos = inpos.get();
final int finalout = currentPos + outlength;
while (currentPos < finalout - 28) {
int val = in[tmpinpos++];
int header = val >>> 28;
switch (header) {
case 0: { // number : 28, bitwidth : 1
out[currentPos++] = (val << 4) >>> 31;
out[currentPos++] = (val << 5) >>> 31;
out[currentPos++] = (val << 6) >>> 31;
out[currentPos++] = (val << 7) >>> 31;
out[currentPos++] = (val << 8) >>> 31;
out[currentPos++] = (val << 9) >>> 31;
out[currentPos++] = (val << 10) >>> 31;
out[currentPos++] = (val << 11) >>> 31;
out[currentPos++] = (val << 12) >>> 31;
out[currentPos++] = (val << 13) >>> 31; // 10
out[currentPos++] = (val << 14) >>> 31;
out[currentPos++] = (val << 15) >>> 31;
out[currentPos++] = (val << 16) >>> 31;
out[currentPos++] = (val << 17) >>> 31;
out[currentPos++] = (val << 18) >>> 31;
out[currentPos++] = (val << 19) >>> 31;
out[currentPos++] = (val << 20) >>> 31;
out[currentPos++] = (val << 21) >>> 31;
out[currentPos++] = (val << 22) >>> 31;
out[currentPos++] = (val << 23) >>> 31; // 20
out[currentPos++] = (val << 24) >>> 31;
out[currentPos++] = (val << 25) >>> 31;
out[currentPos++] = (val << 26) >>> 31;
out[currentPos++] = (val << 27) >>> 31;
out[currentPos++] = (val << 28) >>> 31;
out[currentPos++] = (val << 29) >>> 31;
out[currentPos++] = (val << 30) >>> 31;
out[currentPos++] = (val << 31) >>> 31;
break;
}
case 1: { // number : 14, bitwidth : 2
out[currentPos++] = (val << 4) >>> 30;
out[currentPos++] = (val << 6) >>> 30;
out[currentPos++] = (val << 8) >>> 30;
out[currentPos++] = (val << 10) >>> 30;
out[currentPos++] = (val << 12) >>> 30;
out[currentPos++] = (val << 14) >>> 30;
out[currentPos++] = (val << 16) >>> 30;
out[currentPos++] = (val << 18) >>> 30;
out[currentPos++] = (val << 20) >>> 30;
out[currentPos++] = (val << 22) >>> 30; // 10
out[currentPos++] = (val << 24) >>> 30;
out[currentPos++] = (val << 26) >>> 30;
out[currentPos++] = (val << 28) >>> 30;
out[currentPos++] = (val << 30) >>> 30;
break;
}
case 2: { // number : 9, bitwidth : 3
out[currentPos++] = (val << 5) >>> 29;
out[currentPos++] = (val << 8) >>> 29;
out[currentPos++] = (val << 11) >>> 29;
out[currentPos++] = (val << 14) >>> 29;
out[currentPos++] = (val << 17) >>> 29;
out[currentPos++] = (val << 20) >>> 29;
out[currentPos++] = (val << 23) >>> 29;
out[currentPos++] = (val << 26) >>> 29;
out[currentPos++] = (val << 29) >>> 29;
break;
}
case 3: { // number : 7, bitwidth : 4
out[currentPos++] = (val << 4) >>> 28;
out[currentPos++] = (val << 8) >>> 28;
out[currentPos++] = (val << 12) >>> 28;
out[currentPos++] = (val << 16) >>> 28;
out[currentPos++] = (val << 20) >>> 28;
out[currentPos++] = (val << 24) >>> 28;
out[currentPos++] = (val << 28) >>> 28;
break;
}
case 4: { // number : 5, bitwidth : 5
out[currentPos++] = (val << 7) >>> 27;
out[currentPos++] = (val << 12) >>> 27;
out[currentPos++] = (val << 17) >>> 27;
out[currentPos++] = (val << 22) >>> 27;
out[currentPos++] = (val << 27) >>> 27;
break;
}
case 5: { // number : 4, bitwidth : 7
out[currentPos++] = (val << 4) >>> 25;
out[currentPos++] = (val << 11) >>> 25;
out[currentPos++] = (val << 18) >>> 25;
out[currentPos++] = (val << 25) >>> 25;
break;
}
case 6: { // number : 3, bitwidth : 9
out[currentPos++] = (val << 5) >>> 23;
out[currentPos++] = (val << 14) >>> 23;
out[currentPos++] = (val << 23) >>> 23;
break;
}
case 7: { // number : 2, bitwidth : 14
out[currentPos++] = (val << 4) >>> 18;
out[currentPos++] = (val << 18) >>> 18;
break;
}
case 8: { // number : 1, bitwidth : 28
out[currentPos++] = (val << 4) >>> 4;
break;
}
default: {
throw new RuntimeException("shouldn't happen: limited to 28-bit integers");
}
}
}
while (currentPos < finalout) {
int val = in[tmpinpos++];
int header = val >>> 28;
switch (header) {
case 0: { // number : 28, bitwidth : 1
final int howmany = finalout - currentPos;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (k + 4)) >>> 31;
}
break;
}
case 1: { // number : 14, bitwidth : 2
final int howmany = finalout - currentPos < 14 ? finalout - currentPos : 14;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (2 * k + 4)) >>> 30;
}
break;
}
case 2: { // number : 9, bitwidth : 3
final int howmany = finalout - currentPos < 9 ? finalout - currentPos : 9;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (3 * k + 5)) >>> 29;
}
break;
}
case 3: { // number : 7, bitwidth : 4
final int howmany = finalout - currentPos < 7 ? finalout - currentPos : 7;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (4 * k + 4)) >>> 28;
}
break;
}
case 4: { // number : 5, bitwidth : 5
final int howmany = finalout - currentPos < 5 ? finalout - currentPos : 5;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (5 * k + 7)) >>> 27;
}
break;
}
case 5: { // number : 4, bitwidth : 7
final int howmany = finalout - currentPos < 4 ? finalout - currentPos : 4;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (7 * k + 4)) >>> 25;
}
break;
}
case 6: { // number : 3, bitwidth : 9
final int howmany = finalout - currentPos < 3 ? finalout - currentPos : 3;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (9 * k + 5)) >>> 23;
}
break;
}
case 7: { // number : 2, bitwidth : 14
final int howmany = finalout - currentPos < 2 ? finalout - currentPos : 2;
for (int k = 0; k < howmany; ++k) {
out[currentPos++] = (val << (14 * k + 4)) >>> 18;
}
break;
}
case 8: { // number : 1, bitwidth : 28
out[currentPos++] = (val << 4) >>> 4;
break;
}
default: {
throw new RuntimeException("shouldn't happen");
}
}
}
outpos.set(currentPos);
inpos.set(tmpinpos);
}
@Override
public int maxHeadlessCompressedLength(IntWrapper compressedPositions, int inlength) {
compressedPositions.add(inlength);
return inlength;
}
@Override
public void compress(int[] in, IntWrapper inpos, int inlength, int[] out, IntWrapper outpos) {
if (inlength == 0)
return;
out[outpos.get()] = inlength;
outpos.increment();
headlessCompress(in, inpos, inlength, out, outpos);
}
@Override
public void uncompress(int[] in, IntWrapper inpos, int inlength, int[] out, IntWrapper outpos) {
if (inlength == 0)
return;
final int outlength = in[inpos.get()];
inpos.increment();
headlessUncompress(in, inpos, inlength, out, outpos, outlength);
}
private final static int bitLength[] = { 1, 2, 3, 4, 5, 7, 9, 14, 28 };
private final static int codeNum[] = { 28, 14, 9, 7, 5, 4, 3, 2, 1 };
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}