Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bit-shuffling interfaces for unshuffle with provided output array #608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/main/java/org/xerial/snappy/BitShuffle.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ public static int unshuffle(ByteBuffer shuffled, BitShuffleType type, ByteBuffer
return numProcessed;
}

/**
* Convert the input bit-shuffled byte array into an original byte array.
*
* @param input
* @return a byte array
* @throws IOException
*/
public static byte[] unshuffleByteArray(byte[] input) throws IOException {
byte[] output = new byte[input.length];
int numProcessed = impl.unshuffle(input, 0, 1, input.length, output, 0);
assert(numProcessed == input.length);
return output;
}

/**
* Convert the input bit-shuffled byte array into an original byte array.
*
* @param input
* @param output
* @return byte size of the unshuffled data.
* @throws IOException
*/
public static int unshuffleByteArray(byte[] input, byte[] output) throws IOException {
assert(input.length == output.length);
int numProcessed = impl.unshuffle(input, 0, 1, input.length, output, 0);
assert(numProcessed == input.length);
return numProcessed;
}

/**
* Convert the input bit-shuffled byte array into an original short array.
*
Expand All @@ -222,6 +251,21 @@ public static short[] unshuffleShortArray(byte[] input) throws IOException {
return output;
}

/**
* Convert the input bit-shuffled byte array into an original short array.
*
* @param input
* @param output
* @return byte size of the unshuffled data.
* @throws IOException
*/
public static int unshuffleShortArray(byte[] input, short[] output) throws IOException {
assert(input.length == output.length * 2);
int numProcessed = impl.unshuffle(input, 0, 2, input.length, output, 0);
assert(numProcessed == input.length);
return numProcessed;
}

/**
* Convert the input bit-shuffled byte array into an original int array.
*
Expand All @@ -236,6 +280,21 @@ public static int[] unshuffleIntArray(byte[] input) throws IOException {
return output;
}

/**
* Convert the input bit-shuffled byte array into an original int array.
*
* @param input
* @param output
* @return byte size of the unshuffled data.
* @throws IOException
*/
public static int unshuffleIntArray(byte[] input, int[] output) throws IOException {
assert(input.length == output.length * 4);
int numProcessed = impl.unshuffle(input, 0, 4, input.length, output, 0);
assert(numProcessed == input.length);
return numProcessed;
}

/**
* Convert the input bit-shuffled byte array into an original long array.
*
Expand All @@ -250,6 +309,21 @@ public static long[] unshuffleLongArray(byte[] input) throws IOException {
return output;
}

/**
* Convert the input bit-shuffled byte array into an original long array.
*
* @param input
* @param output
* @return byte size of the unshuffled data.
* @throws IOException
*/
public static int unshuffleLongArray(byte[] input, long[] output) throws IOException {
assert(input.length == output.length * 8);
int numProcessed = impl.unshuffle(input, 0, 8, input.length, output, 0);
assert(numProcessed == input.length);
return numProcessed;
}

/**
* Convert the input bit-shuffled byte array into an original float array.
*
Expand All @@ -264,6 +338,21 @@ public static float[] unshuffleFloatArray(byte[] input) throws IOException {
return output;
}

/**
* Convert the input bit-shuffled byte array into an original float array.
*
* @param input
* @param output
* @return byte size of the unshuffled data.
* @throws IOException
*/
public static int unshuffleFloatArray(byte[] input, float[] output) throws IOException {
assert(input.length == output.length * 4);
int numProcessed = impl.unshuffle(input, 0, 4, input.length, output, 0);
assert(numProcessed == input.length);
return numProcessed;
}

/**
* Convert the input bit-shuffled byte array into an original double array.
*
Expand All @@ -277,4 +366,19 @@ public static double[] unshuffleDoubleArray(byte[] input) throws IOException {
assert(numProcessed == input.length);
return output;
}

/**
* Convert the input bit-shuffled byte array into an original double array.
*
* @param input
* @param output
* @return byte size of the unshuffled data.
* @throws IOException
*/
public static int unshuffleDoubleArray(byte[] input, double[] output) throws IOException {
assert(input.length == output.length * 8);
int numProcessed = impl.unshuffle(input, 0, 8, input.length, output, 0);
assert(numProcessed == input.length);
return numProcessed;
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/xerial/snappy/BitShuffleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,22 @@ public void shuffleLongArray()
byte[] shuffledData = BitShuffle.shuffle(data);
long[] result = BitShuffle.unshuffleLongArray(shuffledData);
assertArrayEquals(data, result);
long[] output = new long[data.length];
BitShuffle.unshuffleLongArray(shuffledData, output);
assertArrayEquals(data, output);
}

@Test
public void shuffleByteArray()
throws Exception
{
byte[] data = new byte[] {43, -32, 1, 3, 34, 43, 34, Byte.MAX_VALUE, -1};
byte[] shuffledData = BitShuffle.shuffle(data);
byte[] result = BitShuffle.unshuffleByteArray(shuffledData);
assertArrayEquals(data, result);
byte[] output = new byte[data.length];
BitShuffle.unshuffleByteArray(shuffledData, output);
assertArrayEquals(data, output);
}

@Test
Expand All @@ -286,6 +302,9 @@ public void shuffleShortArray()
byte[] shuffledData = BitShuffle.shuffle(data);
short[] result = BitShuffle.unshuffleShortArray(shuffledData);
assertArrayEquals(data, result);
short[] output = new short[data.length];
BitShuffle.unshuffleShortArray(shuffledData, output);
assertArrayEquals(data, output);
}

@Test
Expand All @@ -296,6 +315,9 @@ public void shuffleIntArray()
byte[] shuffledData = BitShuffle.shuffle(data);
int[] result = BitShuffle.unshuffleIntArray(shuffledData);
assertArrayEquals(data, result);
int[] output = new int[data.length];
BitShuffle.unshuffleIntArray(shuffledData, output);
assertArrayEquals(data, output);
}

@Test
Expand All @@ -306,6 +328,9 @@ public void shuffleFloatArray()
byte[] shuffledData = BitShuffle.shuffle(data);
float[] result = BitShuffle.unshuffleFloatArray(shuffledData);
assertArrayEquals(data, result, 0.0000001f);
float[] output = new float[data.length];
BitShuffle.unshuffleFloatArray(shuffledData, output);
assertArrayEquals(data, output);
}

@Test
Expand All @@ -316,5 +341,8 @@ public void shuffleDoubleArray()
byte[] shuffledData = BitShuffle.shuffle(data);
double[] result = BitShuffle.unshuffleDoubleArray(shuffledData);
assertArrayEquals(data, result, 0.0000001f);
double[] output = new double[data.length];
BitShuffle.unshuffleDoubleArray(shuffledData, output);
assertArrayEquals(data, output);
}
}
Loading