forked from fast-pack/CSharpFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimple16.cs
More file actions
210 lines (189 loc) · 7.53 KB
/
Simple16.cs
File metadata and controls
210 lines (189 loc) · 7.53 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
/**
* This is an implementation of the popular Simple16 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.
*
* <p>
* Adapted by D. Lemire from the Apache Lucene project.
* </p>
*/
using System;
namespace Genbox.CSharpFastPFOR
{
public class Simple16 : IntegerCODEC, SkippableIntegerCODEC
{
public void headlessCompress(int[] @in, IntWrapper inpos, int inlength, int[] @out, IntWrapper outpos)
{
int i_inpos = inpos.get();
int i_outpos = outpos.get();
int finalin = i_inpos + inlength;
while (i_inpos < finalin)
{
int inoffset = compressblock(@out, i_outpos++, @in, i_inpos, inlength);
if (inoffset == -1)
throw new Exception("Too big a number");
i_inpos += inoffset;
inlength -= inoffset;
}
inpos.set(i_inpos);
outpos.set(i_outpos);
}
/**
* Compress an integer array using Simple16
*
* @param out
* the compressed output
* @param outOffset
* the offset of the output in the number of integers
* @param in
* the integer input array
* @param inOffset
* the offset of the input in the number of integers
* @param n
* the number of elements to be compressed
* @return the number of compressed integers
*/
public static int compressblock(int[] @out, int outOffset, int[] @in, int inOffset, int n)
{
int numIdx, j, num, bits;
for (numIdx = 0; numIdx < S16_NUMSIZE; numIdx++)
{
@out[outOffset] = numIdx << S16_BITSSIZE;
num = (S16_NUM[numIdx] < n) ? S16_NUM[numIdx] : n;
for (j = 0, bits = 0; (j < num)
&& (@in[inOffset + j] < SHIFTED_S16_BITS[numIdx][j]);)
{
@out[outOffset] |= (@in[inOffset + j] << bits);
bits += S16_BITS[numIdx][j];
j++;
}
if (j == num)
{
return num;
}
}
return -1;
}
/**
* Decompress an integer array using Simple16
*
* @param out
* the decompressed output
* @param outOffset
* the offset of the output in the number of integers
* @param in
* the compressed input array
* @param inOffset
* the offset of the input in the number of integers
* @param n
* the number of elements to be compressed
* @return the number of processed integers
*/
public static int decompressblock(int[] @out, int outOffset, int[] @in, int inOffset, int n)
{
int numIdx, j = 0, bits = 0;
numIdx = (int)((uint)@in[inOffset] >> S16_BITSSIZE);
int num = S16_NUM[numIdx] < n ? S16_NUM[numIdx] : n;
for (j = 0, bits = 0; j < num; j++)
{
@out[outOffset + j] = (int)((uint)@in[inOffset] >> bits) & (int)((uint)0xffffffff >> (32 - S16_BITS[numIdx][j]));
bits += S16_BITS[numIdx][j];
}
return num;
}
public void headlessUncompress(int[] @in, IntWrapper inpos, int inlength, int[] @out, IntWrapper outpos, int num)
{
int i_inpos = inpos.get();
int i_outpos = outpos.get();
while (num > 0)
{
int howmany = decompressblock(@out, i_outpos, @in, i_inpos, num);
num -= howmany;
i_outpos += howmany;
i_inpos++;
}
inpos.set(i_inpos);
outpos.set(i_outpos);
}
/**
* Uncompress data from an array to another array.
*
* Both inpos and outpos parameters are modified to indicate new positions
* after read/write.
*
* @param in
* array containing data in compressed form
* @param tmpinpos
* where to start reading in the array
* @param inlength
* length of the compressed data (ignored by some schemes)
* @param out
* array where to write the compressed output
* @param currentPos
* where to write the compressed output in out
* @param outlength
* number of integers we want to decode
*/
public static void uncompress(int[] @in, int tmpinpos, int inlength, int[] @out, int currentPos, int outlength)
{
int pos = tmpinpos + inlength;
while (tmpinpos < pos)
{
int howmany = decompressblock(@out, currentPos, @in, tmpinpos,
outlength);
outlength -= howmany;
currentPos += howmany;
tmpinpos += 1;
}
}
private static int[][] shiftme(int[][] x)
{
int[][] answer = new int[x.Length][];
for (int k = 0; k < x.Length; ++k)
{
answer[k] = new int[x[k].Length];
for (int z = 0; z < answer[k].Length; ++z)
answer[k][z] = 1 << x[k][z];
}
return answer;
}
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);
}
public void uncompress(int[] @in, IntWrapper inpos, int inlength, int[] @out, IntWrapper outpos)
{
if (inlength == 0)
return;
int outlength = @in[inpos.get()];
inpos.increment();
headlessUncompress(@in, inpos, inlength, @out, outpos, outlength);
}
private const int S16_NUMSIZE = 16;
private const int S16_BITSSIZE = 28;
// the possible number of bits used to represent one integer
private static int[] S16_NUM = { 28, 21, 21, 21, 14, 9, 8, 7, 6, 6, 5, 5, 4, 3, 2, 1 };
// the corresponding number of elements for each value of the number of bits
private static int[][] S16_BITS = {
new[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1 },
new[]{ 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new[]{ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 },
new[]{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2 },
new[]{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
new[]{ 4, 3, 3, 3, 3, 3, 3, 3, 3 }, new[] { 3, 4, 4, 4, 4, 3, 3, 3 },
new[]{ 4, 4, 4, 4, 4, 4, 4 }, new[]{ 5, 5, 5, 5, 4, 4 },
new[]{ 4, 4, 5, 5, 5, 5 }, new[] { 6, 6, 6, 5, 5 }, new[] { 5, 5, 6, 6, 6 },
new[]{ 7, 7, 7, 7 }, new[] { 10, 9, 9, }, new[]{ 14, 14 }, new[]{ 28 } };
private static int[][] SHIFTED_S16_BITS = shiftme(S16_BITS);
public override string ToString()
{
return nameof(Simple16);
}
}
}