forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeltaZigzagVariableByte.java
More file actions
130 lines (118 loc) · 4.84 KB
/
Copy pathDeltaZigzagVariableByte.java
File metadata and controls
130 lines (118 loc) · 4.84 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
/*
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*/
package me.lemire.integercompression;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
/**
* VariableByte with Delta+Zigzag Encoding.
*
* @author MURAOKA Taro http://github.com/koron
*/
public final class DeltaZigzagVariableByte implements IntegerCODEC {
@Override
public String toString() {
return DeltaZigzagVariableByte.class.getSimpleName();
}
@Override
public void compress(int[] inBuf, IntWrapper inPos, int inLen,
int[] outBuf, IntWrapper outPos) {
if (inLen == 0) {
return;
}
ByteBuffer byteBuf = ByteBuffer.allocateDirect(inLen * 5 + 3);
DeltaZigzagEncoding.Encoder ctx = new DeltaZigzagEncoding.Encoder(0);
// Delta+Zigzag+VariableByte encoding.
int ip = inPos.get();
final int inPosLast = ip + inLen;
for (; ip < inPosLast; ++ip) {
// Filter with delta+zigzag encoding.
int n = ctx.encodeInt(inBuf[ip]);
// Variable byte encoding.
switch (Integer.numberOfLeadingZeros(n)) {
case 0:
case 1:
case 2:
case 3:
byteBuf.put((byte) (((n >>> 28) & 0x7F) | 0x80));
// through.
//$FALL-THROUGH$
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
byteBuf.put((byte) (((n >>> 21) & 0x7F) | 0x80));
// through.
//$FALL-THROUGH$
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
byteBuf.put((byte) (((n >>> 14) & 0x7F) | 0x80));
// through.
//$FALL-THROUGH$
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
byteBuf.put((byte) (((n >>> 7) & 0x7F) | 0x80));
// through.
//$FALL-THROUGH$
default:
byteBuf.put((byte) (n & 0x7F));
}
}
// Padding buffer to considerable as IntBuffer.
for (int i = (4 - (byteBuf.position() % 4)) % 4; i > 0; --i) {
byteBuf.put((byte) (0x80));
}
int outLen = byteBuf.position() / 4;
byteBuf.flip();
IntBuffer intBuf = byteBuf.asIntBuffer();
/*
* System.out.println(String.format(
* "inLen=%d pos=%d limit=%d outLen=%d outBuf.len=%d", inLen,
* intBuf.position(), intBuf.limit(), outLen, outBuf.length));
*/
intBuf.get(outBuf, outPos.get(), outLen);
inPos.add(inLen);
outPos.add(outLen);
}
@Override
public void uncompress(int[] inBuf, IntWrapper inPos, int inLen,
int[] outBuf, IntWrapper outPos) {
DeltaZigzagEncoding.Decoder ctx = new DeltaZigzagEncoding.Decoder(0);
int ip = inPos.get();
int op = outPos.get();
int vbcNum = 0, vbcShift = 24; // Varialbe Byte Context.
final int inPosLast = ip + inLen;
while (ip < inPosLast) {
// Fetch a byte value.
int n = (inBuf[ip] >>> vbcShift) & 0xFF;
if (vbcShift > 0) {
vbcShift -= 8;
} else {
vbcShift = 24;
ip++;
}
// Decode variable byte and delta+zigzag.
vbcNum = (vbcNum << 7) + (n & 0x7F);
if ((n & 0x80) == 0) {
outBuf[op++] = ctx.decodeInt(vbcNum);
vbcNum = 0;
}
}
outPos.set(op);
inPos.set(inPosLast);
}
}