Put bitshuffle functions in a new class file

This commit is contained in:
Takeshi YAMAMURO 2016-03-31 00:50:13 +09:00
parent 87c11d9034
commit b695e378b1
4 changed files with 275 additions and 200 deletions

View File

@ -0,0 +1,214 @@
/*--------------------------------------------------------------------------
* Copyright 2011 Taro L. Saito
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*--------------------------------------------------------------------------*/
//--------------------------------------
// snappy-java Project
//
// BitShuffle.java
// Since: 2016/03/31
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import java.io.IOException;
public class BitShuffle
{
static {
try {
impl = SnappyLoader.load();
}
catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* An instance of SnappyNative
*/
private static SnappyNative impl;
/**
* Apply a bit-shuffling filter into the input short array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(short[] input) throws IOException {
byte[] output = new byte[input.length * 2];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 2, input.length * 2, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length * 2, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input int array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(int[] input) throws IOException {
byte[] output = new byte[input.length * 4];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 4, input.length * 4, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length * 4, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input long array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(long[] input) throws IOException {
byte[] output = new byte[input.length * 8];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 8, input.length * 8, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length * 8, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input float array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(float[] input) throws IOException {
byte[] output = new byte[input.length * 4];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 4, input.length * 4, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length * 4, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input double array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(double[] input) throws IOException {
byte[] output = new byte[input.length * 8];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 8, input.length * 8, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length * 8, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original short array.
*
* @param input
* @return a short array
* @throws IOException
*/
public static short[] bitUnShuffleShortArray(byte[] input) throws IOException {
short[] output = new short[input.length / 2];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 2, input.length, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original int array.
*
* @param input
* @return an int array
* @throws IOException
*/
public static int[] bitUnShuffleIntArray(byte[] input) throws IOException {
int[] output = new int[input.length / 4];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 4, input.length, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original long array.
*
* @param input
* @return a long array
* @throws IOException
*/
public static long[] bitUnShuffleLongArray(byte[] input) throws IOException {
long[] output = new long[input.length / 8];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 8, input.length, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original float array.
*
* @param input
* @return an float array
* @throws IOException
*/
public static float[] bitUnShuffleFloatArray(byte[] input) throws IOException {
float[] output = new float[input.length / 4];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 4, input.length, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original double array.
*
* @param input
* @return a double array
* @throws IOException
*/
public static double[] bitUnShuffleDoubleArray(byte[] input) throws IOException {
double[] output = new double[input.length / 8];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 8, input.length, output, 0);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
}

View File

@ -903,174 +903,4 @@ public class Snappy
byte[] uncompressed = uncompress(input);
return new String(uncompressed, encoding);
}
/**
* Apply a bit-shuffling filter into the input short array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(short[] input) throws IOException {
byte[] output = new byte[input.length * 2];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 2, input.length * 2, output, 0);
} else {
arrayCopy(input, 0, input.length * 2, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input int array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(int[] input) throws IOException {
byte[] output = new byte[input.length * 4];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 4, input.length * 4, output, 0);
} else {
arrayCopy(input, 0, input.length * 4, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input long array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(long[] input) throws IOException {
byte[] output = new byte[input.length * 8];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 8, input.length * 8, output, 0);
} else {
arrayCopy(input, 0, input.length * 8, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input float array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(float[] input) throws IOException {
byte[] output = new byte[input.length * 4];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 4, input.length * 4, output, 0);
} else {
arrayCopy(input, 0, input.length * 4, output, 0);
}
return output;
}
/**
* Apply a bit-shuffling filter into the input double array.
*
* @param input
* @return bit-shuffled byte array
* @throws IOException
*/
public static byte[] bitShuffle(double[] input) throws IOException {
byte[] output = new byte[input.length * 8];
if (impl.supportBitSuffle()) {
impl.bitShuffle(input, 0, 8, input.length * 8, output, 0);
} else {
arrayCopy(input, 0, input.length * 8, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original short array.
*
* @param input
* @return a short array
* @throws IOException
*/
public static short[] bitUnShuffleShortArray(byte[] input) throws IOException {
short[] output = new short[input.length / 2];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 2, input.length, output, 0);
} else {
arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original int array.
*
* @param input
* @return an int array
* @throws IOException
*/
public static int[] bitUnShuffleIntArray(byte[] input) throws IOException {
int[] output = new int[input.length / 4];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 4, input.length, output, 0);
} else {
arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original long array.
*
* @param input
* @return a long array
* @throws IOException
*/
public static long[] bitUnShuffleLongArray(byte[] input) throws IOException {
long[] output = new long[input.length / 8];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 8, input.length, output, 0);
} else {
arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original float array.
*
* @param input
* @return an float array
* @throws IOException
*/
public static float[] bitUnShuffleFloatArray(byte[] input) throws IOException {
float[] output = new float[input.length / 4];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 4, input.length, output, 0);
} else {
arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
/**
* Convert the input bit-shuffled byte array into an original double array.
*
* @param input
* @return a double array
* @throws IOException
*/
public static double[] bitUnShuffleDoubleArray(byte[] input) throws IOException {
double[] output = new double[input.length / 8];
if (impl.supportBitSuffle()) {
impl.bitUnShuffle(input, 0, 8, input.length, output, 0);
} else {
arrayCopy(input, 0, input.length, output, 0);
}
return output;
}
}

View File

@ -0,0 +1,61 @@
/*--------------------------------------------------------------------------
* Copyright 2011 Taro L. Saito
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*--------------------------------------------------------------------------*/
//--------------------------------------
// snappy-java Project
//
// BitShuffleTest.java
// Since: 2016/03/31
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import static org.junit.Assert.*;
import org.junit.Test;
public class BitShuffleTest {
@Test
public void bitShuffleLongArray()
throws Exception
{
long[] data = new long[] {2, 3, 15, 4234, 43251531412342342L, 23423422342L};
byte[] shuffledData = BitShuffle.bitShuffle(data);
long[] result = BitShuffle.bitUnShuffleLongArray(shuffledData);
assertArrayEquals(data, result);
}
@Test
public void bitShuffleShortArray()
throws Exception
{
short[] data = new short[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1};
byte[] shuffledData = BitShuffle.bitShuffle(data);
short[] result = BitShuffle.bitUnShuffleShortArray(shuffledData);
assertArrayEquals(data, result);
}
@Test
public void bitShuffleIntArray()
throws Exception
{
int[] data = new int[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1, Integer.MAX_VALUE, 3424, 43};
byte[] shuffledData = BitShuffle.bitShuffle(data);
int[] result = BitShuffle.bitUnShuffleIntArray(shuffledData);
assertArrayEquals(data, result);
}
}

View File

@ -329,34 +329,4 @@ public class SnappyTest
_logger.debug(e);
}
}
@Test
public void bitShuffleLongArray()
throws Exception
{
long[] data = new long[] {2, 3, 15, 4234, 43251531412342342L, 23423422342L};
byte[] shuffledData = Snappy.bitShuffle(data);
long[] result = Snappy.bitUnShuffleLongArray(shuffledData);
assertArrayEquals(data, result);
}
@Test
public void bitShuffleShortArray()
throws Exception
{
short[] data = new short[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1};
byte[] shuffledData = Snappy.bitShuffle(data);
short[] result = Snappy.bitUnShuffleShortArray(shuffledData);
assertArrayEquals(data, result);
}
@Test
public void bitShuffleIntArray()
throws Exception
{
int[] data = new int[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1, Integer.MAX_VALUE, 3424, 43};
byte[] shuffledData = Snappy.bitShuffle(data);
int[] result = Snappy.bitUnShuffleIntArray(shuffledData);
assertArrayEquals(data, result);
}
}