Skip to content

Commit 4277fbc

Browse files
authored
feature: Add bit-shuffling interfaces for unshuffle with provided output array (#608)
1 parent 2661260 commit 4277fbc

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

src/main/java/org/xerial/snappy/BitShuffle.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,35 @@ public static int unshuffle(ByteBuffer shuffled, BitShuffleType type, ByteBuffer
208208
return numProcessed;
209209
}
210210

211+
/**
212+
* Convert the input bit-shuffled byte array into an original byte array.
213+
*
214+
* @param input
215+
* @return a byte array
216+
* @throws IOException
217+
*/
218+
public static byte[] unshuffleByteArray(byte[] input) throws IOException {
219+
byte[] output = new byte[input.length];
220+
int numProcessed = impl.unshuffle(input, 0, 1, input.length, output, 0);
221+
assert(numProcessed == input.length);
222+
return output;
223+
}
224+
225+
/**
226+
* Convert the input bit-shuffled byte array into an original byte array.
227+
*
228+
* @param input
229+
* @param output
230+
* @return byte size of the unshuffled data.
231+
* @throws IOException
232+
*/
233+
public static int unshuffleByteArray(byte[] input, byte[] output) throws IOException {
234+
assert(input.length == output.length);
235+
int numProcessed = impl.unshuffle(input, 0, 1, input.length, output, 0);
236+
assert(numProcessed == input.length);
237+
return numProcessed;
238+
}
239+
211240
/**
212241
* Convert the input bit-shuffled byte array into an original short array.
213242
*
@@ -222,6 +251,21 @@ public static short[] unshuffleShortArray(byte[] input) throws IOException {
222251
return output;
223252
}
224253

254+
/**
255+
* Convert the input bit-shuffled byte array into an original short array.
256+
*
257+
* @param input
258+
* @param output
259+
* @return byte size of the unshuffled data.
260+
* @throws IOException
261+
*/
262+
public static int unshuffleShortArray(byte[] input, short[] output) throws IOException {
263+
assert(input.length == output.length * 2);
264+
int numProcessed = impl.unshuffle(input, 0, 2, input.length, output, 0);
265+
assert(numProcessed == input.length);
266+
return numProcessed;
267+
}
268+
225269
/**
226270
* Convert the input bit-shuffled byte array into an original int array.
227271
*
@@ -236,6 +280,21 @@ public static int[] unshuffleIntArray(byte[] input) throws IOException {
236280
return output;
237281
}
238282

283+
/**
284+
* Convert the input bit-shuffled byte array into an original int array.
285+
*
286+
* @param input
287+
* @param output
288+
* @return byte size of the unshuffled data.
289+
* @throws IOException
290+
*/
291+
public static int unshuffleIntArray(byte[] input, int[] output) throws IOException {
292+
assert(input.length == output.length * 4);
293+
int numProcessed = impl.unshuffle(input, 0, 4, input.length, output, 0);
294+
assert(numProcessed == input.length);
295+
return numProcessed;
296+
}
297+
239298
/**
240299
* Convert the input bit-shuffled byte array into an original long array.
241300
*
@@ -250,6 +309,21 @@ public static long[] unshuffleLongArray(byte[] input) throws IOException {
250309
return output;
251310
}
252311

312+
/**
313+
* Convert the input bit-shuffled byte array into an original long array.
314+
*
315+
* @param input
316+
* @param output
317+
* @return byte size of the unshuffled data.
318+
* @throws IOException
319+
*/
320+
public static int unshuffleLongArray(byte[] input, long[] output) throws IOException {
321+
assert(input.length == output.length * 8);
322+
int numProcessed = impl.unshuffle(input, 0, 8, input.length, output, 0);
323+
assert(numProcessed == input.length);
324+
return numProcessed;
325+
}
326+
253327
/**
254328
* Convert the input bit-shuffled byte array into an original float array.
255329
*
@@ -264,6 +338,21 @@ public static float[] unshuffleFloatArray(byte[] input) throws IOException {
264338
return output;
265339
}
266340

341+
/**
342+
* Convert the input bit-shuffled byte array into an original float array.
343+
*
344+
* @param input
345+
* @param output
346+
* @return byte size of the unshuffled data.
347+
* @throws IOException
348+
*/
349+
public static int unshuffleFloatArray(byte[] input, float[] output) throws IOException {
350+
assert(input.length == output.length * 4);
351+
int numProcessed = impl.unshuffle(input, 0, 4, input.length, output, 0);
352+
assert(numProcessed == input.length);
353+
return numProcessed;
354+
}
355+
267356
/**
268357
* Convert the input bit-shuffled byte array into an original double array.
269358
*
@@ -277,4 +366,19 @@ public static double[] unshuffleDoubleArray(byte[] input) throws IOException {
277366
assert(numProcessed == input.length);
278367
return output;
279368
}
369+
370+
/**
371+
* Convert the input bit-shuffled byte array into an original double array.
372+
*
373+
* @param input
374+
* @param output
375+
* @return byte size of the unshuffled data.
376+
* @throws IOException
377+
*/
378+
public static int unshuffleDoubleArray(byte[] input, double[] output) throws IOException {
379+
assert(input.length == output.length * 8);
380+
int numProcessed = impl.unshuffle(input, 0, 8, input.length, output, 0);
381+
assert(numProcessed == input.length);
382+
return numProcessed;
383+
}
280384
}

src/test/java/org/xerial/snappy/BitShuffleTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ public void shuffleLongArray()
276276
byte[] shuffledData = BitShuffle.shuffle(data);
277277
long[] result = BitShuffle.unshuffleLongArray(shuffledData);
278278
assertArrayEquals(data, result);
279+
long[] output = new long[data.length];
280+
BitShuffle.unshuffleLongArray(shuffledData, output);
281+
assertArrayEquals(data, output);
282+
}
283+
284+
@Test
285+
public void shuffleByteArray()
286+
throws Exception
287+
{
288+
byte[] data = new byte[] {43, -32, 1, 3, 34, 43, 34, Byte.MAX_VALUE, -1};
289+
byte[] shuffledData = BitShuffle.shuffle(data);
290+
byte[] result = BitShuffle.unshuffleByteArray(shuffledData);
291+
assertArrayEquals(data, result);
292+
byte[] output = new byte[data.length];
293+
BitShuffle.unshuffleByteArray(shuffledData, output);
294+
assertArrayEquals(data, output);
279295
}
280296

281297
@Test
@@ -286,6 +302,9 @@ public void shuffleShortArray()
286302
byte[] shuffledData = BitShuffle.shuffle(data);
287303
short[] result = BitShuffle.unshuffleShortArray(shuffledData);
288304
assertArrayEquals(data, result);
305+
short[] output = new short[data.length];
306+
BitShuffle.unshuffleShortArray(shuffledData, output);
307+
assertArrayEquals(data, output);
289308
}
290309

291310
@Test
@@ -296,6 +315,9 @@ public void shuffleIntArray()
296315
byte[] shuffledData = BitShuffle.shuffle(data);
297316
int[] result = BitShuffle.unshuffleIntArray(shuffledData);
298317
assertArrayEquals(data, result);
318+
int[] output = new int[data.length];
319+
BitShuffle.unshuffleIntArray(shuffledData, output);
320+
assertArrayEquals(data, output);
299321
}
300322

301323
@Test
@@ -306,6 +328,9 @@ public void shuffleFloatArray()
306328
byte[] shuffledData = BitShuffle.shuffle(data);
307329
float[] result = BitShuffle.unshuffleFloatArray(shuffledData);
308330
assertArrayEquals(data, result, 0.0000001f);
331+
float[] output = new float[data.length];
332+
BitShuffle.unshuffleFloatArray(shuffledData, output);
333+
assertArrayEquals(data, output);
309334
}
310335

311336
@Test
@@ -316,5 +341,8 @@ public void shuffleDoubleArray()
316341
byte[] shuffledData = BitShuffle.shuffle(data);
317342
double[] result = BitShuffle.unshuffleDoubleArray(shuffledData);
318343
assertArrayEquals(data, result, 0.0000001f);
344+
double[] output = new double[data.length];
345+
BitShuffle.unshuffleDoubleArray(shuffledData, output);
346+
assertArrayEquals(data, output);
319347
}
320348
}

0 commit comments

Comments
 (0)