forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeltaZigzagEncoding.java
More file actions
94 lines (78 loc) · 3.06 KB
/
Copy pathDeltaZigzagEncoding.java
File metadata and controls
94 lines (78 loc) · 3.06 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
/*
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*/
package me.lemire.integercompression;
/**
* Delta+Zigzag Encoding.
*
* @author MURAOKA Taro http://github.com/koron
*/
public final class DeltaZigzagEncoding {
static class Context {
int contextValue;
Context(int contextValue) {
this.contextValue = contextValue;
}
void setContextValue(int contextValue) {
this.contextValue = contextValue;
}
int getContextValue() {
return this.contextValue;
}
}
static class Encoder extends Context {
Encoder(int contextValue) {
super(contextValue);
}
int encodeInt(int value) {
int n = value - this.contextValue;
this.contextValue = value;
return (n << 1) ^ (n >> 31);
}
int[] encodeArray(int[] src, int srcoff, int length,
int[] dst, int dstoff) {
for (int i = 0; i < length; ++i) {
dst[dstoff + i] = encodeInt(src[srcoff + i]);
}
return dst;
}
int[] encodeArray(int[] src, int srcoff, int length,
int[] dst) {
return encodeArray(src, srcoff, length, dst, 0);
}
int[] encodeArray(int[] src, int offset, int length) {
return encodeArray(src, offset, length,
new int[length], 0);
}
int[] encodeArray(int[] src) {
return encodeArray(src, 0, src.length,
new int[src.length], 0);
}
}
static class Decoder extends Context {
Decoder(int contextValue) {
super(contextValue);
}
int decodeInt(int value) {
int n = (value >>> 1) ^ ((value << 31) >> 31);
n += this.contextValue;
this.contextValue = n;
return n;
}
int[] decodeArray(int[] src, int srcoff, int length,
int[] dst, int dstoff) {
for (int i = 0; i < length; ++i) {
dst[dstoff + i] = decodeInt(src[srcoff + i]);
}
return dst;
}
int[] decodeArray(int[] src, int offset, int length) {
return decodeArray(src, offset, length,
new int[length], 0);
}
int[] decodeArray(int[] src) {
return decodeArray(src, 0, src.length);
}
}
}